Debian Codegraph @

src/com/github/weisj/jsvg/nodes/text/TextMetrics.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2022-2026 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.nodes.text;
23
24 import org.jetbrains.annotations.NotNull;
25
26 import com.github.weisj.jsvg.attributes.font.SVGFont;
27 import com.github.weisj.jsvg.geometry.size.Length;
28 import com.github.weisj.jsvg.nodes.prototype.Renderable;
29 import com.github.weisj.jsvg.nodes.text.TextSegment.RenderableSegment;
30 import com.github.weisj.jsvg.nodes.text.TextSegment.RenderableSegment.UseTextLengthForCalculation;
31 import com.github.weisj.jsvg.renderer.RenderContext;
32 import com.github.weisj.jsvg.renderer.impl.NodeRenderer;
33 import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor;
34
35 public final class TextMetrics {
36 private final double letterSpacingLength;
37 private final double glyphLength;
38 private final double fixedGlyphLength;
39 private final int glyphCount;
40 private final int controllableLetterSpacingCount;
41
42 public TextMetrics(double letterSpacingLength, double visibleCodepointLength, int glyphCount,
43 double fixedGlyphLength, int controllableLetterSpacingCount) {
44 this.letterSpacingLength = letterSpacingLength;
45 this.glyphLength = visibleCodepointLength;
46 this.glyphCount = glyphCount;
47 this.fixedGlyphLength = fixedGlyphLength;
48 this.controllableLetterSpacingCount = controllableLetterSpacingCount;
49 }
50
51 static @NotNull TextMetrics computeTextMetrics(@NotNull TextLayoutGroup layoutGroup,
52 @NotNull RenderContext context, @NotNull UseTextLengthForCalculation flag) {
53 if (flag == UseTextLengthForCalculation.YES) {
54 Length fixedLength = layoutGroup.fixedLength();
55 if (fixedLength != null) {
56 return new TextMetrics(0, 0, 0,
57 fixedLength.resolve(context.measureContext()), 0);
58 }
59 }
60
61 RenderContextAccessor.Accessor accessor = RenderContextAccessor.instance();
62 SVGFont font = accessor.font(context);
63 float letterSpacing = accessor.fontRenderContext(context).letterSpacing().resolve(context.measureContext());
64
65 IntermediateTextMetrics metrics = new IntermediateTextMetrics();
66
67 int index = 0;
68 for (TextSegment segment : layoutGroup.segments()) {
69 RenderContext currentContext = context;
70 if (segment instanceof Renderable) {
71 currentContext = NodeRenderer.setupRenderContext(segment, context);
72 }
73 if (segment instanceof StringTextSegment) {
74 StringTextSegment stringTextSegment = (StringTextSegment) segment;
75 accumulateSegmentMetrics(layoutGroup, metrics, stringTextSegment, font, letterSpacing, index);
76 } else if (segment instanceof RenderableSegment) {
77 accumulateRenderableSegmentMetrics((RenderableSegment) segment, metrics, currentContext);
78 } else {
79 throw new IllegalStateException("Unexpected segment " + segment);
80 }
81 index++;
82 }
83 return new TextMetrics(metrics.letterSpacingLength, metrics.glyphLength, metrics.glyphCount,
84 metrics.fixedGlyphLength, metrics.controllableLetterSpacingCount);
85 }
86
87 private static void accumulateRenderableSegmentMetrics(@NotNull RenderableSegment segment,
88 @NotNull IntermediateTextMetrics metrics, @NotNull RenderContext currentContext) {
89 TextMetrics textMetrics = segment.computeTextMetrics(currentContext, UseTextLengthForCalculation.YES);
90 metrics.letterSpacingLength += textMetrics.letterSpacingLength();
91 metrics.glyphLength += textMetrics.glyphLength();
92 metrics.glyphCount += textMetrics.glyphCount();
93 metrics.fixedGlyphLength += textMetrics.fixedGlyphLength();
94 metrics.controllableLetterSpacingCount += textMetrics.controllableLetterSpacingCount();
95 }
96
97 private static void accumulateSegmentMetrics(@NotNull TextLayoutGroup layoutGroup,
98 @NotNull IntermediateTextMetrics metrics,
99 @NotNull StringTextSegment segment,
100 @NotNull SVGFont font, float letterSpacing, int index) {
101 int glyphCount = segment.codepoints().size();
102
103 boolean lastSegment = index == layoutGroup.segments().size() - 1;
104 int whiteSpaceCount = lastSegment ? (glyphCount - 1) : glyphCount;
105
106 metrics.glyphCount += glyphCount;
107 metrics.letterSpacingLength += whiteSpaceCount * letterSpacing;
108 metrics.controllableLetterSpacingCount += whiteSpaceCount;
109
110 for (String codepoint : segment.codepoints()) {
111 metrics.glyphLength += font.codepointGlyph(codepoint).advance();
112 }
113 }
114
115 public double letterSpacingLength() {
116 return letterSpacingLength;
117 }
118
119 public double glyphLength() {
120 return glyphLength;
121 }
122
123 public double fixedGlyphLength() {
124 return fixedGlyphLength;
125 }
126
127 public double totalAdjustableLength() {
128 return glyphLength() + letterSpacingLength();
129 }
130
131 public int glyphCount() {
132 return glyphCount;
133 }
134
135 public int controllableLetterSpacingCount() {
136 return controllableLetterSpacingCount;
137 }
138
139 @Override
140 public String toString() {
141 return "TextMetrics{" +
142 "whiteSpaceLength=" + letterSpacingLength +
143 ", glyphLength=" + glyphLength +
144 ", glyphCount=" + glyphCount +
145 ", fixedGlyphLength=" + fixedGlyphLength +
146 '}';
147 }
148
149 private static final class IntermediateTextMetrics {
150 double letterSpacingLength = 0;
151 double glyphLength = 0;
152 double fixedGlyphLength = 0;
153
154 int glyphCount = 0;
155 int controllableLetterSpacingCount = 0;
156 }
157 }