|
@@ -0,0 +1,62 @@
|
|
|
|
|
+package net.ranides.assira.ui.parsers;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.experimental.UtilityClass;
|
|
|
|
|
+import net.ranides.assira.grammar.G4Node;
|
|
|
|
|
+import net.ranides.assira.grammar.G4Selector;
|
|
|
|
|
+import net.ranides.assira.grammar.G4SelectorBuilder;
|
|
|
|
|
+import net.ranides.grammars.generated.UiGrammarParser;
|
|
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
|
|
+
|
|
|
|
|
+import javax.swing.*;
|
|
|
|
|
+import java.awt.*;
|
|
|
|
|
+
|
|
|
|
|
+@UtilityClass
|
|
|
|
|
+public class ParseCssFont {
|
|
|
|
|
+
|
|
|
|
|
+ public static final G4Selector<Integer> FONT_STYLE = new G4SelectorBuilder<Integer>()
|
|
|
|
|
+ .withName("CssFont:FONT_STYLE")
|
|
|
|
|
+ .withParser(GrammarBase.PARSER)
|
|
|
|
|
+ .valueForToken(UiGrammarParser.SWING_FONT_STYLE_PLAIN, Font.PLAIN)
|
|
|
|
|
+ .valueForToken(UiGrammarParser.SWING_FONT_STYLE_BOLD, Font.BOLD)
|
|
|
|
|
+ .valueForToken(UiGrammarParser.SWING_FONT_STYLE_ITALIC, Font.ITALIC)
|
|
|
|
|
+ .valueForToken(UiGrammarParser.SWING_FONT_STYLE_BOLD_ITALIC, Font.BOLD | Font.ITALIC)
|
|
|
|
|
+ .prepare();
|
|
|
|
|
+
|
|
|
|
|
+ public static final G4Selector<Font> FONT = new G4SelectorBuilder<Font>()
|
|
|
|
|
+ .withName("CssColor:FONT")
|
|
|
|
|
+ .withParser(GrammarBase.PARSER)
|
|
|
|
|
+ .mapRule("fontSwingLong", node -> {
|
|
|
|
|
+ return createFont(node);
|
|
|
|
|
+ })
|
|
|
|
|
+ .mapRule("fontSwingShort", node -> {
|
|
|
|
|
+ return createFont(node);
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ .prepare();
|
|
|
|
|
+
|
|
|
|
|
+ @NotNull
|
|
|
|
|
+ private static Font createFont(G4Node node) {
|
|
|
|
|
+ String name = node.child("@name").text();
|
|
|
|
|
+
|
|
|
|
|
+ int style = node.children("@style")
|
|
|
|
|
+ .map(FONT_STYLE::single).first()
|
|
|
|
|
+ .orElse(Font.PLAIN);
|
|
|
|
|
+
|
|
|
|
|
+ int size = node.children("@size")
|
|
|
|
|
+ .map(ParsePrimitives.INT::single).first()
|
|
|
|
|
+ .orElseGet( () -> GrammarBase.component(node).getFont().getSize());
|
|
|
|
|
+
|
|
|
|
|
+ return new Font(name, style, size);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public static Font font(String value) {
|
|
|
|
|
+ return GrammarBase.parseSingle(FONT, "root_font", value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Font font(String value, JComponent component) {
|
|
|
|
|
+ return GrammarBase.parseSingle(FONT, "root_font", component, value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|