Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2024 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.FontRenderContext;
26 import java.awt.font.GlyphMetrics;
27 import java.awt.font.GlyphVector;
28 import java.awt.font.LineMetrics;
29 import java.util.HashMap;
30
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33
34 import com.github.weisj.jsvg.geometry.size.Length;
35 import com.github.weisj.jsvg.nodes.text.EmojiGlyph;
36 import com.github.weisj.jsvg.nodes.text.Glyph;
37
38 public final class AWTSVGFont implements SVGFont {
39
40 private final @NotNull Font font;
41 private final FontRenderContext frc = new FontRenderContext(null, true, true);
42 private final HashMap<String, Glyph> glyphCache;
43
44 private @Nullable LineMetrics lineMetrics;
45 private float[] baselineOffsets;
46 private float exHeight = Length.UNSPECIFIED_RAW;
47 private float mathBaseline = Length.UNSPECIFIED_RAW;
48
49 public AWTSVGFont(@NotNull Font font) {
50 this.font = font;
51 this.glyphCache = new HashMap<>();
52 }
53
54 public @NotNull Font font() {
55 return font;
56 }
57
58 @Override
59 public @NotNull Glyph codepointGlyph(@NotNull String codepoint) {
60 Glyph glyph = glyphCache.get(codepoint);
61 if (glyph != null) return glyph;
62 glyph = createGlyph(codepoint.toCharArray());
63 glyphCache.put(codepoint, glyph);
64 return glyph;
65 }
66
67 @Override
68 public @NotNull String family() {
69 return font.getFamily();
70 }
71
72 @Override
73 public int size() {
74 return font.getSize();
75 }
76
77 private @NotNull LineMetrics lineMetrics() {
78 if (lineMetrics == null) {
79 lineMetrics = font.getLineMetrics("Ax-", 0, 1, frc);
80 }
81 return lineMetrics;
82 }
83
84 @Override
85 public float effectiveExHeight() {
86 if (Length.isUnspecified(exHeight)) {
87 exHeight = (float) codepointGlyph("x").glyphOutline().getBounds2D().getHeight();
88 }
89 return exHeight;
90 }
91
92 @Override
93 public float effectiveEmHeight() {
94 return font.getSize2D();
95 }
96
97 @Override
98 public float mathematicalBaseline() {
99 if (Length.isUnspecified(mathBaseline)) {
100 mathBaseline = -effectiveExHeight() / 2;
101 }
102 return mathBaseline;
103 }
104
105 private float[] baselineOffsets() {
106 if (baselineOffsets == null) baselineOffsets = lineMetrics().getBaselineOffsets();
107 return baselineOffsets;
108 }
109
110 @Override
111 public float hangingBaseline() {
112 return baselineOffsets()[Font.HANGING_BASELINE];
113 }
114
115 @Override
116 public float romanBaseline() {
117 return baselineOffsets()[Font.ROMAN_BASELINE];
118 }
119
120 @Override
121 public float centerBaseline() {
122 return baselineOffsets()[Font.CENTER_BASELINE];
123 }
124
125 @Override
126 public float middleBaseline() {
127 return (romanBaseline() - effectiveExHeight()) / 2;
128 }
129
130 @Override
131 public float textUnderBaseline() {
132 return lineMetrics().getUnderlineOffset();
133 }
134
135 @Override
136 public float textOverBaseline() {
137 return textUnderBaseline() - effectiveEmHeight();
138 }
139
140 @NotNull
141 private Glyph createGlyph(char @NotNull [] codepoint) {
142 GlyphVector glyphVector = font.createGlyphVector(frc, codepoint);
143 GlyphMetrics gm = glyphVector.getGlyphMetrics(0);
144 float advance = gm.getAdvanceX();
145
146 if (isPossibleEmoji(codepoint)) {
147 return new EmojiGlyph(String.valueOf(codepoint), advance);
148 }
149
150 Shape shape = glyphVector.getOutline(0, 0);
151 return new Glyph(shape, advance, gm.getBounds2D().isEmpty());
152 }
153
154 private static boolean isPossibleEmoji(char @NotNull [] codepoint) {
155 for (char c : codepoint) {
156 if (Character.isHighSurrogate(c) || Character.isLowSurrogate(c)) {
157 return true;
158 }
159 }
160 return false;
161 }
162 }