| •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.nodes.text; |
| 23 |
|
| 24 |
import java.awt.geom.AffineTransform; |
| 25 |
|
| 26 |
import org.jetbrains.annotations.NotNull; |
| 27 |
|
| 28 |
import com.github.weisj.jsvg.attributes.text.LengthAdjust; |
| 29 |
import com.google.errorprone.annotations.Immutable; |
| 30 |
|
| 31 |
@Immutable |
| 32 |
final class GlyphAdvancement { |
| 33 |
private final float spacingAdjustment; |
| 34 |
private final float absoluteSpacingAdjustment; |
| 35 |
private final float glyphAdjustment; |
| 36 |
|
| 37 |
private final boolean needsLastSpacing; |
| 38 |
|
| 39 |
private GlyphAdvancement() { |
| 40 |
spacingAdjustment = 1; |
| 41 |
absoluteSpacingAdjustment = 0; |
| 42 |
glyphAdjustment = 1; |
| 43 |
needsLastSpacing = false; |
| 44 |
} |
| 45 |
|
| 46 |
GlyphAdvancement(@NotNull TextMetrics textMetrics, float desiredLength, @NotNull LengthAdjust lengthAdjust) { |
| 47 |
double totalSpace = desiredLength - textMetrics.fixedGlyphLength(); |
| 48 |
switch (lengthAdjust) { |
| 49 |
default: |
| 50 |
case Spacing: |
| 51 |
spacingAdjustment = 1; |
| 52 |
glyphAdjustment = 1; |
| 53 |
if (textMetrics.glyphCount() == 0) { |
| 54 |
absoluteSpacingAdjustment = 0; |
| 55 |
needsLastSpacing = false; |
| 56 |
} else { |
| 57 |
float adjustableSpace = (float) (totalSpace - textMetrics.glyphLength()); |
| 58 |
int spacingCount = textMetrics.controllableLetterSpacingCount(); |
| 59 |
if (spacingCount == 0) { |
| 60 |
absoluteSpacingAdjustment = adjustableSpace; |
| 61 |
needsLastSpacing = true; |
| 62 |
} else { |
| 63 |
absoluteSpacingAdjustment = adjustableSpace / spacingCount; |
| 64 |
needsLastSpacing = false; |
| 65 |
} |
| 66 |
} |
| 67 |
break; |
| 68 |
case SpacingAndGlyphs: |
| 69 |
absoluteSpacingAdjustment = 0; |
| 70 |
needsLastSpacing = false; |
| 71 |
spacingAdjustment = (float) (totalSpace / textMetrics.totalAdjustableLength()); |
| 72 |
glyphAdjustment = spacingAdjustment; |
| 73 |
break; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
static GlyphAdvancement defaultAdvancement() { |
| 78 |
return new GlyphAdvancement(); |
| 79 |
} |
| 80 |
|
| 81 |
public float maxLookBehind() { |
| 82 |
return -Math.min(0, absoluteSpacingAdjustment); |
| 83 |
} |
| 84 |
|
| 85 |
float spacingAdvancement(float letterSpacing) { |
| 86 |
return letterSpacing * spacingAdjustment + absoluteSpacingAdjustment; |
| 87 |
} |
| 88 |
|
| 89 |
float glyphAdvancement(@NotNull Glyph glyph) { |
| 90 |
return glyph.advance() * glyphAdjustment; |
| 91 |
} |
| 92 |
|
| 93 |
boolean shouldSkipLastSpacing() { |
| 94 |
if (needsLastSpacing) return false; |
| 95 |
return absoluteSpacingAdjustment != 0 || spacingAdjustment != 1 || glyphAdjustment != 1; |
| 96 |
} |
| 97 |
|
| 98 |
@NotNull |
| 99 |
AffineTransform glyphTransform(@NotNull AffineTransform transform) { |
| 100 |
if (glyphAdjustment == 1) return transform; |
| 101 |
AffineTransform glyphTransform = new AffineTransform(transform); |
| 102 |
glyphTransform.scale(glyphAdjustment, 1); |
| 103 |
return glyphTransform; |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public String toString() { |
| 108 |
return "GlyphAdvancement{" + |
| 109 |
"spacingAdjustment=" + spacingAdjustment + |
| 110 |
", absoluteSpacingAdjustment=" + absoluteSpacingAdjustment + |
| 111 |
", glyphAdjustment=" + glyphAdjustment + |
| 112 |
", needsLastSpacing=" + needsLastSpacing + |
| 113 |
'}'; |
| 114 |
} |
| 115 |
} |