| •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.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 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 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 |
} |