| •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.*; |
| 25 |
import java.awt.geom.AffineTransform; |
| 26 |
import java.awt.geom.Path2D; |
| 27 |
import java.awt.geom.Point2D; |
| 28 |
import java.awt.geom.Rectangle2D; |
| 29 |
import java.util.ArrayList; |
| 30 |
import java.util.Collections; |
| 31 |
import java.util.List; |
| 32 |
import java.util.Set; |
| 33 |
|
| 34 |
import org.jetbrains.annotations.NotNull; |
| 35 |
|
| 36 |
import com.github.weisj.jsvg.attributes.PaintOrder; |
| 37 |
import com.github.weisj.jsvg.attributes.VectorEffect; |
| 38 |
import com.github.weisj.jsvg.attributes.font.SVGFont; |
| 39 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 40 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 41 |
import com.github.weisj.jsvg.renderer.impl.ShapeRenderer; |
| 42 |
import com.github.weisj.jsvg.renderer.impl.context.FontRenderContext; |
| 43 |
import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor; |
| 44 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 45 |
import com.github.weisj.jsvg.renderer.output.TextOutput; |
| 46 |
import com.github.weisj.jsvg.util.ShapeUtil; |
| 47 |
|
| 48 |
final class GlyphRenderer { |
| 49 |
private static final boolean DEBUG = false; |
| 50 |
|
| 51 |
private GlyphRenderer() {} |
| 52 |
|
| 53 |
static void prepareGlyphRun(@NotNull StringTextSegment segment, @NotNull GlyphCursor cursor, @NotNull SVGFont font, |
| 54 |
@NotNull RenderContext context, @NotNull TextOutput textOutput) { |
| 55 |
GlyphRun glyphRun = layoutGlyphRun(segment, cursor, font, context, textOutput); |
| 56 |
|
| 57 |
cursor.completeGlyphRunMetrics.union(glyphRun.metrics()); |
| 58 |
segment.currentGlyphRun = glyphRun; |
| 59 |
segment.currentRenderContext = context; |
| 60 |
} |
| 61 |
|
| 62 |
static void renderGlyphRun(@NotNull Output output, @NotNull PaintOrder paintOrder, |
| 63 |
@NotNull Set<VectorEffect> vectorEffects, @NotNull StringTextSegment segment) { |
| 64 |
RenderContext context = segment.currentRenderContext; |
| 65 |
assert context != null; |
| 66 |
|
| 67 |
GlyphRun glyphRun = segment.currentGlyphRun; |
| 68 |
assert glyphRun != null; |
| 69 |
|
| 70 |
AbstractGlyphRun.Metrics metrics = glyphRun.metrics(); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
Stroke stroke = context.stroke(1f); |
| 75 |
|
| 76 |
|
| 77 |
ShapeRenderer.renderWithPaintOrder(output, true, paintOrder, |
| 78 |
new ShapeRenderer.ShapePaintContext(context, vectorEffects, stroke, null), |
| 79 |
new ShapeRenderer.PaintShape(glyphRun.shape(), metrics.paintBounds), |
| 80 |
null); |
| 81 |
|
| 82 |
|
| 83 |
SVGFont font = RenderContextAccessor.instance().font(context); |
| 84 |
Output.SafeState safeState = output.safeState(); |
| 85 |
for (AbstractGlyphRun.PaintableEmoji emoji : glyphRun.emojis()) { |
| 86 |
emoji.render(output, font); |
| 87 |
safeState.restore(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
segment.currentRenderContext = null; |
| 92 |
segment.currentGlyphRun = null; |
| 93 |
} |
| 94 |
|
| 95 |
static @NotNull GlyphRun layoutGlyphRun(@NotNull StringTextSegment segment, @NotNull GlyphCursor cursor, |
| 96 |
@NotNull SVGFont font, @NotNull RenderContext context, |
| 97 |
@NotNull TextOutput textOutput) { |
| 98 |
boolean segmentVisible = segment.isSegmentVisible(context); |
| 99 |
MeasureContext measure = context.measureContext(); |
| 100 |
FontRenderContext fontRenderContext = RenderContextAccessor.instance().fontRenderContext(context); |
| 101 |
float letterSpacing = fontRenderContext.letterSpacing().resolve(measure); |
| 102 |
|
| 103 |
Path2D glyphPath = new Path2D.Float(); |
| 104 |
Point2D.Float layoutStart = cursor.currentLocation(measure); |
| 105 |
List<AbstractGlyphRun.PaintableEmoji> emojis = null; |
| 106 |
|
| 107 |
boolean isLastSegment = segment.isLastSegmentInParent(); |
| 108 |
boolean shouldSkipLastSpacing = isLastSegment && cursor.advancement().shouldSkipLastSpacing(); |
| 109 |
|
| 110 |
textOutput.glyphRunBreak(); |
| 111 |
|
| 112 |
List<String> codepoints = segment.codepoints(); |
| 113 |
for (int i = 0, count = codepoints.size(); i < count; i++) { |
| 114 |
String codepoint = codepoints.get(i); |
| 115 |
|
| 116 |
boolean lastCodepoint = i == count - 1; |
| 117 |
|
| 118 |
Glyph glyph = font.codepointGlyph(codepoint); |
| 119 |
|
| 120 |
if (i > 0 && !cursor.isCurrentGlyphAutoLayout()) { |
| 121 |
textOutput.glyphRunBreak(); |
| 122 |
} |
| 123 |
|
| 124 |
AffineTransform glyphTransform = cursor.advance(measure, glyph); |
| 125 |
|
| 126 |
boolean skipSpacing = lastCodepoint && shouldSkipLastSpacing; |
| 127 |
if (!skipSpacing) cursor.advanceSpacing(letterSpacing); |
| 128 |
|
| 129 |
if (!segmentVisible || !cursor.shouldRenderCurrentGlyph()) continue; |
| 130 |
|
| 131 |
|
| 132 |
if (glyphTransform == null) break; |
| 133 |
|
| 134 |
if (glyph.isRendered()) { |
| 135 |
float baselineOffset = computeBaselineOffset(font, fontRenderContext); |
| 136 |
glyphTransform.translate(0, -baselineOffset); |
| 137 |
|
| 138 |
if (glyph instanceof EmojiGlyph) { |
| 139 |
if (emojis == null) { |
| 140 |
emojis = new ArrayList<>(); |
| 141 |
} |
| 142 |
emojis.add(new AbstractGlyphRun.PaintableEmoji( |
| 143 |
(EmojiGlyph) glyph, |
| 144 |
new AffineTransform(glyphTransform))); |
| 145 |
} else { |
| 146 |
Shape glyphOutline = glyph.glyphOutline(); |
| 147 |
Shape renderPath = ShapeUtil.transformShape(glyphOutline, glyphTransform); |
| 148 |
glyphPath.append(renderPath, false); |
| 149 |
|
| 150 |
if (DEBUG) { |
| 151 |
glyphPath.append( |
| 152 |
ShapeUtil.transformShape(glyphOutline.getBounds2D(), glyphTransform), false); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
textOutput.codepoint(codepoint, glyphTransform, context); |
| 158 |
} |
| 159 |
|
| 160 |
Rectangle2D paintBounds = glyphPath.getBounds2D(); |
| 161 |
Point2D.Float layoutEnd = cursor.currentLocation(measure); |
| 162 |
Rectangle2D layoutBounds = new Rectangle2D.Double( |
| 163 |
layoutStart.x, paintBounds.getY(), |
| 164 |
layoutEnd.x - layoutStart.x, paintBounds.getHeight()); |
| 165 |
return new GlyphRun(glyphPath, |
| 166 |
new AbstractGlyphRun.Metrics(paintBounds, layoutBounds), |
| 167 |
emojis != null ? emojis : Collections.emptyList()); |
| 168 |
} |
| 169 |
|
| 170 |
private static float computeBaselineOffset(@NotNull SVGFont font, @NotNull FontRenderContext fontRenderContext) { |
| 171 |
switch (fontRenderContext.dominantBaseline()) { |
| 172 |
default: |
| 173 |
case Auto: |
| 174 |
|
| 175 |
case Alphabetic: |
| 176 |
return font.romanBaseline(); |
| 177 |
case Hanging: |
| 178 |
return font.hangingBaseline(); |
| 179 |
case Central: |
| 180 |
return font.centerBaseline(); |
| 181 |
case Middle: |
| 182 |
return font.middleBaseline(); |
| 183 |
case Mathematical: |
| 184 |
return font.mathematicalBaseline(); |
| 185 |
case Ideographic: |
| 186 |
case TextAfterEdge: |
| 187 |
case TextBottom: |
| 188 |
return font.textUnderBaseline(); |
| 189 |
case TextBeforeEdge: |
| 190 |
case TextTop: |
| 191 |
return font.textOverBaseline(); |
| 192 |
} |
| 193 |
} |
| 194 |
} |