Skip to content

Commit

Permalink
feat(Service): init FontMangager
Browse files Browse the repository at this point in the history
  • Loading branch information
Dituon committed Jul 30, 2024
1 parent e3021f1 commit 631a660
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.swing.text.StyleContext;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.Map;

Expand All @@ -22,23 +23,24 @@ public class GraphicsAttributedString extends AttributedString {
protected int strokeSize;
protected Color strokeColor;

public static Map<? extends java.text.AttributedCharacterIterator.Attribute, ?> asAttributeMap(TextData data) {
public static Map<? extends AttributedCharacterIterator.Attribute, ?> asAttributeMap(TextTemplate data) {
Font font = new Font(data.getFont(), data.getStyle().getValue(), data.getSize());
return Map.of(TextAttribute.FONT, font);
}

public GraphicsAttributedString(TextData data) {
public GraphicsAttributedString(TextTemplate data) {
this(data.getText(), data);
}

public GraphicsAttributedString(String text, TextData data) {
public GraphicsAttributedString(String text, TextTemplate data) {
super(text, asAttributeMap(data));
this.text = text;
this.font = StyleContext.getDefaultStyleContext().getFont(
data.getFont(), data.getStyle().getValue(), data.getSize()
);
this.color = data.getAwtColor();
this.align = data.getAlign();
//TODO
// this.baseline = data.getBaseline();
this.baseline = TextBaseline.TOP;
this.wrap = data.getWrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class TextBuilder {
public static final List<String> EXPR_LIST = List.of("$txt", "$from", "$to", "$group");

@Getter
protected final TextData data;
protected final TextTemplate data;
@Getter
protected final boolean dynamic;
protected GraphicsParagraph staticParagraph;

public TextBuilder(TextData data) {
public TextBuilder(TextTemplate data) {
this.data = data;
var text = data.getText();
this.dynamic = EXPR_LIST.stream().anyMatch(text::contains);
Expand All @@ -36,11 +36,11 @@ public GraphicsParagraph buildParagraph(GraphicsAttributedString string, int wid
return buildParagraph(string, data, width);
}

public static GraphicsParagraph buildParagraph(GraphicsAttributedString string, TextData data) {
public static GraphicsParagraph buildParagraph(GraphicsAttributedString string, TextTemplate data) {
return buildParagraph(string, data.getWrap(), data.getAlign(), data.getBaseline());
}

public static GraphicsParagraph buildParagraph(GraphicsAttributedString string, TextData data, int width) {
public static GraphicsParagraph buildParagraph(GraphicsAttributedString string, TextTemplate data, int width) {
return buildParagraph(string, data.getWrap(), data.getAlign(), data.getBaseline(), width);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class TextDynamicModel extends TextModel {
public static final Pattern TEXT_VAR_REGEX = Pattern.compile("\\$txt([1-9]\\d*)\\[(.*?)]"); //$txt(num)[(xxx)]

protected String text;
protected TextData data;
protected TextTemplate data;

public TextDynamicModel(TextData textData, TextExtraData extraInfo) {
public TextDynamicModel(TextTemplate textData, TextExtraData extraInfo) {
this.data = textData;
text = extraInfo != null ? buildText(
textData.getText(), extraInfo, textData.getGreedy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TextStaticModel extends TextModel {
protected int y;
protected final GraphicsParagraph paragraph;

public TextStaticModel(TextData textData, GraphicsParagraph paragraph) {
public TextStaticModel(TextTemplate textData, GraphicsParagraph paragraph) {
var pos = textData.getPos();
x = pos[0];
y = pos[1];
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/moe/dituon/petpet/share/element/text/TextTemplate.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package moe.dituon.petpet.share.element.text

import kotlinx.serialization.Serializable
import moe.dituon.petpet.share.*
import java.awt.Color

@Serializable
data class TextTemplate(
var text: String,
var pos: IntArray = intArrayOf(0, 0),
var angle: Short = 0,
var color: String = TextModel.DEFAULT_COLOR_STR,
var font: String = "simsun",
var size: Int = 16,
var align: TextAlign = TextAlign.LEFT,
var baseline: TextBaseline = TextBaseline.TOP,
var wrap: TextWrap = TextWrap.NONE,
var style: TextStyle = TextStyle.PLAIN,
var position: List<Position>? = listOf(Position.LEFT, Position.TOP),
var origin: TransformOrigin = TransformOrigin.DEFAULT,
var strokeColor: String = TextModel.DEFAULT_STROKE_COLOR_STR,
var strokeSize: Short = 0,
var greedy: Boolean = false
) {
fun getAwtColor(): Color {
if (color == moe.dituon.petpet.share.TextModel.DEFAULT_COLOR_STR) return moe.dituon.petpet.share.TextModel.DEFAULT_COLOR
return decodeColor(color)
}

fun getStrokeAwtColor(): Color {
if (strokeColor == moe.dituon.petpet.share.TextModel.DEFAULT_STROKE_COLOR_STR) return moe.dituon.petpet.share.TextModel.DEFAULT_STROKE_COLOR
return decodeColor(strokeColor)
}
}
68 changes: 68 additions & 0 deletions src/main/java/moe/dituon/petpet/share/service/FontManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package moe.dituon.petpet.share.service;

import java.awt.*;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

public class FontManager {
protected static class FontManagerInstance {
private static final FontManager INSTANCE = new FontManager();
}

public static FontManager getInstance() {
return FontManagerInstance.INSTANCE;
}

final GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
public final List<SupportedLanguage> supportedLanguageList = List.of(
new SupportedLanguage("en", "Latin", new char[]{0x0061}),
new SupportedLanguage("zh", "CJK Unified Ideographs", new char[]{0x4E00}),
new SupportedLanguage("ja", "Hiragana", new char[]{0x3041}),
new SupportedLanguage("ko", "Hangul Syllables", new char[]{0xAC00}),
new SupportedLanguage("th", "Thai", new char[]{0x0E01}),
new SupportedLanguage("ru", "Cyrillic", new char[]{0x0410}),
new SupportedLanguage("ar", "Arabic", new char[]{0x0621}),
new SupportedLanguage("he", "Hebrew", new char[]{0x05D0}),
new SupportedLanguage("vi", "Vietnamese", new char[]{0x0102})
// , new SupportedLanguage("emoji", "Emoji", Character.toChars(0x1F600))
);
final Map<Font, Set<SupportedLanguage>> fontSupportedLanguageMap = new HashMap<>(256);
final Map<SupportedLanguage, Set<Font>> supportedLanguageFontMap = new HashMap<>(256);

protected FontManager() {
for (Font font : environment.getAllFonts()) {
addFont(font);
}
}

public void addFont(Font font) {
var langSet = fontSupportedLanguageMap.compute(font, (f, s) ->
s == null ? new HashSet<>(supportedLanguageList.size()) : s
);
for (SupportedLanguage language : supportedLanguageList) {
boolean flag = true;
for (char point : language.testPoints) {
if (!font.canDisplay(point)) flag = false;
}
if (flag) {
langSet.add(language);
supportedLanguageFontMap.compute(language, (l, s) ->
s == null ? new HashSet<>(8) : s
).add(font);
}
}
}

public static class SupportedLanguage {
protected char[] testPoints;
public final String name;
public final String desc;

protected SupportedLanguage(String name, String desc, char[] testPoints) {
this.name = name;
this.desc = desc;
this.testPoints = testPoints;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import moe.dituon.petpet.share.Type;
import moe.dituon.petpet.share.template.PetpetTemplate;
import moe.dituon.petpet.share.template.TemplateBuilder;
import moe.dituon.petpet.share.template.TemplateType;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -36,7 +37,7 @@ public void pushBasePath(File basePath) throws IOException {

var background = new BackgroundResource(file);
resourceManager.pushBackground(id, background);
if (templateConfig.getType() == Type.IMG && background.files.length > 1) {
if (templateConfig.getType() == TemplateType.IMG && background.files.length > 1) {
background.randomFlag = true;
}
var templateBuilder = new TemplateBuilder(templateConfig, background);
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/moe/dituon/petpet/share/template/PetpetTemplate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import moe.dituon.petpet.share.BackgroundData
import moe.dituon.petpet.share.TextData
import moe.dituon.petpet.share.Type
import moe.dituon.petpet.share.element.avatar.AvatarTemplate
import moe.dituon.petpet.share.element.text.TextTemplate
import moe.dituon.petpet.share.script.LuaTableSerializer
import org.luaj.vm2.LuaTable

enum class TemplateType {
GIF, IMG
}

@Serializable
data class PetpetTemplate(
val type: Type,
val type: TemplateType,
val avatar: List<AvatarTemplate> = emptyList(),
val text: List<TextData> = emptyList(),
val text: List<TextTemplate> = emptyList(),
val background: BackgroundData? = null,
val delay: Int = 65,
val alias: List<String> = emptyList(),
Expand Down

0 comments on commit 631a660

Please sign in to comment.