Debian Codegraph @

src/com/github/weisj/jsvg/attributes/font/MeasurableFontSpec.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.util.Arrays;
25 import java.util.Objects;
26
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import com.github.weisj.jsvg.geometry.size.Length;
31 import com.github.weisj.jsvg.geometry.size.Percentage;
32 import com.github.weisj.jsvg.geometry.size.Unit;
33 import com.github.weisj.jsvg.renderer.MeasureContext;
34
35 public final class MeasurableFontSpec extends FontSpec {
36 private final int currentWeight;
37 private final @NotNull Length currentSize;
38
39 MeasurableFontSpec(@NotNull String[] families, @Nullable FontStyle style, @Nullable Length sizeAdjust,
40 @NotNull Percentage stretch, int currentWeight, @NotNull Length currentSize) {
41 super(families, style, sizeAdjust, stretch);
42 this.currentWeight = currentWeight;
43 this.currentSize = currentSize;
44 }
45
46 public static @NotNull MeasurableFontSpec createDefault() {
47 return new MeasurableFontSpec(
48 // Ensure there is always a valid family available.
49 new String[] {SVGFont.defaultFontFamily()},
50 FontStyle.normal(),
51 null,
52 FontStretch.Normal.percentage(),
53 PredefinedFontWeight.NORMAL_WEIGHT,
54 Unit.RAW.valueOf(SVGFont.defaultFontSize()));
55 }
56
57 public @NotNull String[] families() {
58 return families;
59 }
60
61 public @NotNull FontStyle style() {
62 assert style != null;
63 return style;
64 }
65
66 public @NotNull Percentage stretch() {
67 return stretch;
68 }
69
70 public int currentWeight() {
71 return currentWeight;
72 }
73
74 public float emSize(@NotNull MeasureContext context) {
75 return currentSize.resolveFontSize(context);
76 }
77
78 public float effectiveSize(@NotNull MeasureContext context) {
79 // TODO: Choose the size of the font so that its lowercase letters
80 // (as determined by the x-height of the font)
81 // are the specified number times the font-size.
82 float emSize = emSize(context);
83 if (sizeAdjust != null) {
84 return SVGFont.emFromEx(emSize * sizeAdjust.resolveFontSize(context));
85 }
86 return emSize;
87 }
88
89 public @NotNull MeasurableFontSpec withFontSize(@Nullable FontSize size, @Nullable Length sizeAdjust) {
90 if (size == null && sizeAdjust == null) return this;
91 return new MeasurableFontSpec(families, style, sizeAdjust != null ? sizeAdjust : this.sizeAdjust, stretch,
92 currentWeight, size != null ? size.size(currentSize) : this.currentSize);
93 }
94
95 public @NotNull MeasurableFontSpec derive(@Nullable AttributeFontSpec other) {
96 if (other == null) return this;
97 String[] newFamilies = other.families != null && other.families.length > 0
98 ? other.families
99 : this.families;
100 FontStyle newStyle = other.style != null
101 ? other.style
102 : this.style;
103 int newWeight = other.weight() != null
104 ? other.weight().weight(currentWeight)
105 : this.currentWeight;
106 Length newSize = other.size() != null
107 ? other.size().size(currentSize)
108 : this.currentSize;
109 Length newSizeAdjust = other.sizeAdjust != null
110 ? other.sizeAdjust
111 : this.sizeAdjust;
112 Percentage newStretch = other.stretch.isSpecified()
113 ? other.stretch
114 : this.stretch;
115 return new MeasurableFontSpec(newFamilies, newStyle, newSizeAdjust, newStretch, newWeight, newSize);
116 }
117
118 @Override
119 public String toString() {
120 return "MeasurableFontSpec{" +
121 "families=" + Arrays.toString(families) +
122 ", style=" + style +
123 ", sizeAdjust=" + sizeAdjust +
124 ", stretch=" + stretch +
125 ", currentWeight=" + currentWeight +
126 ", currentSize=" + currentSize +
127 '}';
128 }
129
130 @Override
131 public boolean equals(Object o) {
132 if (this == o) return true;
133 if (!(o instanceof MeasurableFontSpec)) return false;
134 if (!super.equals(o)) return false;
135 MeasurableFontSpec fontSpec = (MeasurableFontSpec) o;
136 return currentWeight == fontSpec.currentWeight && currentSize.equals(fontSpec.currentSize);
137 }
138
139 @Override
140 public int hashCode() {
141 return Objects.hash(super.hashCode(), currentWeight, currentSize);
142 }
143 }