Debian Codegraph @

src/com/github/weisj/jsvg/attributes/font/FontResolver.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.awt.font.TextAttribute;
26 import java.awt.geom.AffineTransform;
27 import java.text.AttributedCharacterIterator;
28 import java.util.*;
29 import java.util.List;
30
31 import org.jetbrains.annotations.NotNull;
32
33 import com.github.weisj.jsvg.renderer.MeasureContext;
34
35 public final class FontResolver {
36 private FontResolver() {}
37
38 public static void clearFontCache() {
39 FontCache.INSTANCE.cache.clear();
40 }
41
42 public static @NotNull SVGFont resolve(@NotNull MeasurableFontSpec fontSpec,
43 @NotNull MeasureContext measureContext, @NotNull String defaultFontFamily) {
44 FontCache.CacheKey key = new FontCache.CacheKey(fontSpec, measureContext);
45 SVGFont cachedFont = FontCache.INSTANCE.cache.get(key);
46 if (cachedFont != null) return cachedFont;
47 SVGFont resolvedFont = resolveWithoutCache(fontSpec, measureContext, defaultFontFamily);
48 FontCache.INSTANCE.cache.put(key, resolvedFont);
49 return resolvedFont;
50 }
51
52 public static @NotNull SVGFont resolveWithoutCache(@NotNull MeasurableFontSpec fontSpec,
53 @NotNull MeasureContext measureContext, @NotNull String defaultFontFamily) {
54 String family = findSupportedFontFamily(fontSpec, defaultFontFamily);
55
56 FontStyle style = fontSpec.style();
57
58 float weight = cssWeightToAwtWeight(fontSpec.currentWeight());
59 float size = fontSpec.effectiveSize(measureContext);
60 float stretch = fontSpec.stretch().orElseIfUnspecified(1).value();
61
62 Map<AttributedCharacterIterator.Attribute, Object> attributes = new HashMap<>(5, 1f);
63 attributes.put(TextAttribute.FAMILY, family);
64 attributes.put(TextAttribute.SIZE, size);
65 attributes.put(TextAttribute.WEIGHT, weight);
66 attributes.put(TextAttribute.WIDTH, stretch);
67
68
69 if (style instanceof FontStyle.Normal) {
70 attributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
71 } else if (style instanceof FontStyle.Italic) {
72 attributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
73 } else {
74 AffineTransform transform = style.transform();
75 if (transform != null) attributes.put(TextAttribute.TRANSFORM, transform);
76 }
77
78 Font font = new Font(attributes);
79 return new AWTSVGFont(font);
80 }
81
82 private static float cssWeightToAwtWeight(float weight) {
83 int normalWeight = PredefinedFontWeight.NORMAL_WEIGHT;
84 float currentWeight = weight;
85 if (currentWeight > normalWeight) {
86 // The bold weight for css and awt differ. We compensate for this difference to ensure
87 // that bold css fonts correspond to bold awt fonts, as this is the most commonly supported
88 // font variation.
89 float awtWeightCompensationFactor =
90 (TextAttribute.WEIGHT_BOLD * normalWeight) / PredefinedFontWeight.BOLD_WEIGHT;
91 currentWeight *= awtWeightCompensationFactor;
92 }
93 return currentWeight / normalWeight;
94 }
95
96 private static @NotNull String findSupportedFontFamily(@NotNull MeasurableFontSpec fontSpec,
97 @NotNull String defaultFontFamily) {
98 String[] families = fontSpec.families();
99 for (String family : families) {
100 if (FontFamiliesCache.INSTANCE.isSupportedFontFamily(family)) return family;
101 }
102 return defaultFontFamily;
103 }
104
105 public static @NotNull List<@NotNull String> supportedFonts() {
106 return Collections.unmodifiableList(Arrays.asList(FontFamiliesCache.INSTANCE.supportedFonts));
107 }
108
109 @SuppressWarnings("ImmutableEnumChecker")
110 private enum FontFamiliesCache {
111 INSTANCE;
112
113 private final @NotNull String[] supportedFonts;
114
115 FontFamiliesCache() {
116 supportedFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
117 }
118
119 boolean isSupportedFontFamily(final @NotNull String fontName) {
120 for (String supportedFont : supportedFonts) {
121 if (supportedFont.equalsIgnoreCase(fontName)) return true;
122 }
123 return false;
124 }
125 }
126
127 @SuppressWarnings("ImmutableEnumChecker")
128 private enum FontCache {
129 INSTANCE;
130
131 private final HashMap<CacheKey, SVGFont> cache = new HashMap<>();
132
133 private static final class CacheKey {
134 private final @NotNull MeasurableFontSpec spec;
135 private final @NotNull MeasureContext context;
136
137 private CacheKey(@NotNull MeasurableFontSpec spec, @NotNull MeasureContext context) {
138 this.spec = spec;
139 this.context = context;
140 }
141
142 @Override
143 public String toString() {
144 return "CacheKey{" +
145 "spec=" + spec +
146 ", context=" + context +
147 '}';
148 }
149
150 @Override
151 public boolean equals(Object o) {
152 if (this == o) return true;
153 if (!(o instanceof CacheKey)) return false;
154 CacheKey cacheKey = (CacheKey) o;
155 return spec.equals(cacheKey.spec) && context.equals(cacheKey.context);
156 }
157
158 @Override
159 public int hashCode() {
160 return Objects.hash(spec, context);
161 }
162 }
163 }
164 }