Debian Codegraph @

src/com/github/weisj/jsvg/attributes/font/FontParser.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.attributes.font;
23
24 import java.awt.*;
25 import java.util.Locale;
26
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import com.github.weisj.jsvg.attributes.value.PercentageDimension;
31 import com.github.weisj.jsvg.geometry.size.Length;
32 import com.github.weisj.jsvg.geometry.size.Percentage;
33 import com.github.weisj.jsvg.parser.impl.AttributeNode;
34 import com.github.weisj.jsvg.parser.impl.SeparatorMode;
35
36 public final class FontParser {
37 private FontParser() {}
38
39 // Todo: font-variant
40 public static @NotNull AttributeFontSpec parseFontSpec(@NotNull AttributeNode node) {
41 String[] fontFamilies = node.getStringList("font-family", SeparatorMode.COMMA_ONLY);
42 canonicalizeFontFamily(fontFamilies);
43
44 // Todo: https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#fallback_weights
45 @Nullable FontWeight weight = parseWeight(node);
46 @Nullable FontSize size = parseFontSize(node);
47 @Nullable Length sizeAdjust = parseSizeAdjust(node);
48 @Nullable FontStyle style = parseFontStyle(node);
49 @NotNull Percentage stretch = parseStretch(node);
50
51 return new AttributeFontSpec(fontFamilies, style, sizeAdjust, stretch, size, weight);
52 }
53
54 private static @NotNull String stripQuotes(@NotNull String str, char quoteChar) {
55 String quoteStr = String.valueOf(quoteChar);
56 if (str.length() > 2 && str.startsWith(quoteStr) && str.endsWith(quoteStr)) {
57 return str.substring(1, str.length() - 1);
58 }
59 return str;
60 }
61
62 private static void canonicalizeFontFamily(@NotNull String[] fontFamilies) {
63 for (int i = 0; i < fontFamilies.length; i++) {
64 String family = fontFamilies[i].toLowerCase(Locale.US);
65 switch (family) {
66 case "sans-serif":
67 family = Font.SANS_SERIF;
68 break;
69 case "serif":
70 family = Font.SERIF;
71 break;
72 case "monospace":
73 family = Font.MONOSPACED;
74 break;
75 default:
76 family = stripQuotes(family, '\'');
77 family = stripQuotes(family, '"');
78 break;
79 }
80 fontFamilies[i] = family;
81 }
82 }
83
84 public static @Nullable FontWeight parseWeight(@NotNull AttributeNode node) {
85 final String fontWeightKey = "font-weight";
86 FontWeight weight = node.getEnum(fontWeightKey, PredefinedFontWeight.Number);
87 if (weight == PredefinedFontWeight.Number) {
88 if (node.hasAttribute(fontWeightKey)) {
89 weight = new NumberFontWeight(
90 Math.max(1, Math.min(1000, node.getFloat(fontWeightKey,
91 PredefinedFontWeight.NORMAL_WEIGHT))));
92 } else {
93 weight = null;
94 }
95 }
96 return weight;
97 }
98
99 public static @NotNull Percentage parseStretch(@NotNull AttributeNode node) {
100 FontStretch stretch = node.getEnum("font-stretch", FontStretch.Percentage);
101 return stretch == FontStretch.Percentage
102 ? node.parser().parsePercentage(node.getValue("font-stretch"),
103 Percentage.UNSPECIFIED, 0.5f, 2f)
104 : stretch.percentage();
105 }
106
107 public static @Nullable FontSize parseFontSize(@NotNull AttributeNode node) {
108 FontSize fontSize = node.getEnum("font-size", PredefinedFontSize.Number);
109 if (fontSize == PredefinedFontSize.Number) {
110 Length size = node.getLength("font-size", PercentageDimension.CUSTOM, Length.UNSPECIFIED);
111 fontSize = size.isSpecified()
112 ? new LengthFontSize(size)
113 : null;
114 }
115 return fontSize;
116 }
117
118 public static @Nullable Length parseSizeAdjust(@NotNull AttributeNode node) {
119 return node.getLength("font-size-adjust", PercentageDimension.NONE);
120 }
121
122 static @Nullable FontStyle parseFontStyle(@NotNull AttributeNode node) {
123 FontStyle style = null;
124 String styleStr = node.getValue("font-style");
125 if ("normal".equalsIgnoreCase(styleStr)) {
126 style = FontStyle.normal();
127 } else if ("italic".equalsIgnoreCase(styleStr)) {
128 style = FontStyle.italic();
129 } else if (styleStr != null && styleStr.startsWith("oblique")) {
130 String[] comps = styleStr.split(" ", 2);
131 if (comps.length == 2) {
132 style = new FontStyle.Oblique(
133 node.parser().parseAngle(comps[1], FontStyle.Oblique.DEFAULT_ANGLE));
134 } else {
135 style = FontStyle.oblique();
136 }
137 }
138 return style;
139 }
140 }