| •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.awt.*; |
| 25 |
import java.awt.font.FontRenderContext; |
| 26 |
import java.awt.font.GlyphMetrics; |
| 27 |
import java.awt.font.GlyphVector; |
| 28 |
import java.awt.font.LineMetrics; |
| 29 |
import java.util.HashMap; |
| 30 |
|
| 31 |
import org.jetbrains.annotations.NotNull; |
| 32 |
import org.jetbrains.annotations.Nullable; |
| 33 |
|
| 34 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 35 |
import com.github.weisj.jsvg.nodes.text.EmojiGlyph; |
| 36 |
import com.github.weisj.jsvg.nodes.text.Glyph; |
| 37 |
|
| 38 |
public final class AWTSVGFont implements SVGFont { |
| 39 |
|
| 40 |
private final @NotNull Font font; |
| 41 |
private final FontRenderContext frc = new FontRenderContext(null, true, true); |
| 42 |
private final HashMap<String, Glyph> glyphCache; |
| 43 |
|
| 44 |
private @Nullable LineMetrics lineMetrics; |
| 45 |
private float[] baselineOffsets; |
| 46 |
private float exHeight = Length.UNSPECIFIED_RAW; |
| 47 |
private float mathBaseline = Length.UNSPECIFIED_RAW; |
| 48 |
|
| 49 |
public AWTSVGFont(@NotNull Font font) { |
| 50 |
this.font = font; |
| 51 |
this.glyphCache = new HashMap<>(); |
| 52 |
} |
| 53 |
|
| 54 |
public @NotNull Font font() { |
| 55 |
return font; |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public @NotNull Glyph codepointGlyph(@NotNull String codepoint) { |
| 60 |
Glyph glyph = glyphCache.get(codepoint); |
| 61 |
if (glyph != null) return glyph; |
| 62 |
glyph = createGlyph(codepoint.toCharArray()); |
| 63 |
glyphCache.put(codepoint, glyph); |
| 64 |
return glyph; |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public @NotNull String family() { |
| 69 |
return font.getFamily(); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public int size() { |
| 74 |
return font.getSize(); |
| 75 |
} |
| 76 |
|
| 77 |
private @NotNull LineMetrics lineMetrics() { |
| 78 |
if (lineMetrics == null) { |
| 79 |
lineMetrics = font.getLineMetrics("Ax-", 0, 1, frc); |
| 80 |
} |
| 81 |
return lineMetrics; |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public float effectiveExHeight() { |
| 86 |
if (Length.isUnspecified(exHeight)) { |
| 87 |
exHeight = (float) codepointGlyph("x").glyphOutline().getBounds2D().getHeight(); |
| 88 |
} |
| 89 |
return exHeight; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public float effectiveEmHeight() { |
| 94 |
return font.getSize2D(); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public float mathematicalBaseline() { |
| 99 |
if (Length.isUnspecified(mathBaseline)) { |
| 100 |
mathBaseline = -effectiveExHeight() / 2; |
| 101 |
} |
| 102 |
return mathBaseline; |
| 103 |
} |
| 104 |
|
| 105 |
private float[] baselineOffsets() { |
| 106 |
if (baselineOffsets == null) baselineOffsets = lineMetrics().getBaselineOffsets(); |
| 107 |
return baselineOffsets; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public float hangingBaseline() { |
| 112 |
return baselineOffsets()[Font.HANGING_BASELINE]; |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public float romanBaseline() { |
| 117 |
return baselineOffsets()[Font.ROMAN_BASELINE]; |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public float centerBaseline() { |
| 122 |
return baselineOffsets()[Font.CENTER_BASELINE]; |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public float middleBaseline() { |
| 127 |
return (romanBaseline() - effectiveExHeight()) / 2; |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public float textUnderBaseline() { |
| 132 |
return lineMetrics().getUnderlineOffset(); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public float textOverBaseline() { |
| 137 |
return textUnderBaseline() - effectiveEmHeight(); |
| 138 |
} |
| 139 |
|
| 140 |
@NotNull |
| 141 |
private Glyph createGlyph(char @NotNull [] codepoint) { |
| 142 |
GlyphVector glyphVector = font.createGlyphVector(frc, codepoint); |
| 143 |
GlyphMetrics gm = glyphVector.getGlyphMetrics(0); |
| 144 |
float advance = gm.getAdvanceX(); |
| 145 |
|
| 146 |
if (isPossibleEmoji(codepoint)) { |
| 147 |
return new EmojiGlyph(String.valueOf(codepoint), advance); |
| 148 |
} |
| 149 |
|
| 150 |
Shape shape = glyphVector.getOutline(0, 0); |
| 151 |
return new Glyph(shape, advance, gm.getBounds2D().isEmpty()); |
| 152 |
} |
| 153 |
|
| 154 |
private static boolean isPossibleEmoji(char @NotNull [] codepoint) { |
| 155 |
for (char c : codepoint) { |
| 156 |
if (Character.isHighSurrogate(c) || Character.isLowSurrogate(c)) { |
| 157 |
return true; |
| 158 |
} |
| 159 |
} |
| 160 |
return false; |
| 161 |
} |
| 162 |
} |