| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 39 |
|
| 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 |
|
| 86 |
|
| 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 |
} |