Debian Codegraph @

src/com/github/weisj/jsvg/renderer/impl/context/FontRenderContext.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.renderer.impl.context;
23
24 import java.util.Objects;
25
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28
29 import com.github.weisj.jsvg.attributes.text.DominantBaseline;
30 import com.github.weisj.jsvg.attributes.text.TextAnchor;
31 import com.github.weisj.jsvg.attributes.value.PercentageDimension;
32 import com.github.weisj.jsvg.geometry.size.Length;
33 import com.github.weisj.jsvg.nodes.text.TextPath;
34 import com.github.weisj.jsvg.nodes.text.TextSpan;
35 import com.github.weisj.jsvg.parser.impl.AttributeNode;
36
37 public final class FontRenderContext {
38 // Note: An unspecified value is different from 0.
39 // Unlike 0 it allows us to use spacing different from 0 if needed.
40 private final @Nullable Length letterSpacing;
41 private final @Nullable DominantBaseline dominantBaseline;
42 private final @Nullable TextAnchor textAnchor;
43
44 public FontRenderContext(@Nullable Length letterSpacing, @Nullable DominantBaseline dominantBaseline,
45 @Nullable TextAnchor textAnchor) {
46 this.letterSpacing = letterSpacing;
47 this.dominantBaseline = dominantBaseline;
48 this.textAnchor = textAnchor;
49 }
50
51 public @NotNull Length letterSpacing() {
52 return letterSpacing != null ? letterSpacing : Length.ZERO;
53 }
54
55 public @NotNull TextAnchor textAnchor() {
56 return textAnchor != null ? textAnchor : TextAnchor.Start;
57 }
58
59 public @NotNull DominantBaseline dominantBaseline() {
60 return dominantBaseline != null ? dominantBaseline : DominantBaseline.Auto;
61 }
62
63 @Override
64 public boolean equals(Object o) {
65 if (this == o) return true;
66 if (!(o instanceof FontRenderContext)) return false;
67 FontRenderContext that = (FontRenderContext) o;
68 return Objects.equals(letterSpacing, that.letterSpacing)
69 && dominantBaseline == that.dominantBaseline
70 && textAnchor == that.textAnchor;
71 }
72
73 @Override
74 public int hashCode() {
75 return Objects.hash(letterSpacing, dominantBaseline, textAnchor);
76 }
77
78 public static @NotNull FontRenderContext createDefault() {
79 return new FontRenderContext(null, null, null);
80 }
81
82 public static @NotNull FontRenderContext parse(@NotNull AttributeNode attributeNode) {
83 DominantBaseline dominantBaseline = attributeNode.getEnum("dominant-baseline", DominantBaseline.Auto);
84 if (dominantBaseline == DominantBaseline.Auto && attributeNode.tagIsOneOf(TextPath.TAG, TextSpan.TAG)) {
85 // On 'textPath' and 'tspan' elements the parent baseline should be used, which is the default
86 // behaviour when the value is null.
87 dominantBaseline = null;
88 }
89 return new FontRenderContext(
90 attributeNode.getLength("letter-spacing", PercentageDimension.NONE),
91 dominantBaseline,
92 attributeNode.getEnumNullable("text-anchor", TextAnchor.class));
93 }
94
95 public @NotNull FontRenderContext derive(@Nullable FontRenderContext frc) {
96 if (frc == null || frc.equals(this)) return this;
97 return new FontRenderContext(
98 frc.letterSpacing != null ? frc.letterSpacing : letterSpacing,
99 frc.dominantBaseline != null ? frc.dominantBaseline : dominantBaseline,
100 frc.textAnchor != null ? frc.textAnchor : textAnchor);
101 }
102
103 @Override
104 public String toString() {
105 return "FontRenderContext{" +
106 "letterSpacing=" + letterSpacing +
107 ", dominantBaseline=" + dominantBaseline +
108 ", textAnchor=" + textAnchor +
109 '}';
110 }
111 }