Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2022-2023 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 java.awt.geom.AffineTransform;
25
26 import org.jetbrains.annotations.NotNull;
27
28 import com.github.weisj.jsvg.attributes.text.LengthAdjust;
29 import com.google.errorprone.annotations.Immutable;
30
31 @Immutable
32 final class GlyphAdvancement {
33 private final float spacingAdjustment;
34 private final float absoluteSpacingAdjustment;
35 private final float glyphAdjustment;
36
37 private final boolean needsLastSpacing;
38
39 private GlyphAdvancement() {
40 spacingAdjustment = 1;
41 absoluteSpacingAdjustment = 0;
42 glyphAdjustment = 1;
43 needsLastSpacing = false;
44 }
45
46 GlyphAdvancement(@NotNull TextMetrics textMetrics, float desiredLength, @NotNull LengthAdjust lengthAdjust) {
47 double totalSpace = desiredLength - textMetrics.fixedGlyphLength();
48 switch (lengthAdjust) {
49 default:
50 case Spacing:
51 spacingAdjustment = 1;
52 glyphAdjustment = 1;
53 if (textMetrics.glyphCount() == 0) {
54 absoluteSpacingAdjustment = 0;
55 needsLastSpacing = false;
56 } else {
57 float adjustableSpace = (float) (totalSpace - textMetrics.glyphLength());
58 int spacingCount = textMetrics.controllableLetterSpacingCount();
59 if (spacingCount == 0) {
60 absoluteSpacingAdjustment = adjustableSpace;
61 needsLastSpacing = true;
62 } else {
63 absoluteSpacingAdjustment = adjustableSpace / spacingCount;
64 needsLastSpacing = false;
65 }
66 }
67 break;
68 case SpacingAndGlyphs:
69 absoluteSpacingAdjustment = 0;
70 needsLastSpacing = false;
71 spacingAdjustment = (float) (totalSpace / textMetrics.totalAdjustableLength());
72 glyphAdjustment = spacingAdjustment;
73 break;
74 }
75 }
76
77 static GlyphAdvancement defaultAdvancement() {
78 return new GlyphAdvancement();
79 }
80
81 public float maxLookBehind() {
82 return -Math.min(0, absoluteSpacingAdjustment);
83 }
84
85 float spacingAdvancement(float letterSpacing) {
86 return letterSpacing * spacingAdjustment + absoluteSpacingAdjustment;
87 }
88
89 float glyphAdvancement(@NotNull Glyph glyph) {
90 return glyph.advance() * glyphAdjustment;
91 }
92
93 boolean shouldSkipLastSpacing() {
94 if (needsLastSpacing) return false;
95 return absoluteSpacingAdjustment != 0 || spacingAdjustment != 1 || glyphAdjustment != 1;
96 }
97
98 @NotNull
99 AffineTransform glyphTransform(@NotNull AffineTransform transform) {
100 if (glyphAdjustment == 1) return transform;
101 AffineTransform glyphTransform = new AffineTransform(transform);
102 glyphTransform.scale(glyphAdjustment, 1);
103 return glyphTransform;
104 }
105
106 @Override
107 public String toString() {
108 return "GlyphAdvancement{" +
109 "spacingAdjustment=" + spacingAdjustment +
110 ", absoluteSpacingAdjustment=" + absoluteSpacingAdjustment +
111 ", glyphAdjustment=" + glyphAdjustment +
112 ", needsLastSpacing=" + needsLastSpacing +
113 '}';
114 }
115 }