| •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.Rectangle2D; |
| 27 |
import java.awt.image.BufferedImage; |
| 28 |
import java.util.List; |
| 29 |
|
| 30 |
import org.jetbrains.annotations.NotNull; |
| 31 |
|
| 32 |
import com.github.weisj.jsvg.attributes.font.SVGFont; |
| 33 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 34 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 35 |
import com.github.weisj.jsvg.renderer.output.impl.Graphics2DOutput; |
| 36 |
import com.github.weisj.jsvg.util.ImageUtil; |
| 37 |
import com.github.weisj.jsvg.util.SystemUtil; |
| 38 |
|
| 39 |
class AbstractGlyphRun<T extends Shape> { |
| 40 |
|
| 41 |
static class Metrics { |
| 42 |
final @NotNull Rectangle2D paintBounds; |
| 43 |
final @NotNull Rectangle2D layoutBounds; |
| 44 |
|
| 45 |
Metrics(@NotNull Rectangle2D paintBounds, @NotNull Rectangle2D layoutBounds) { |
| 46 |
this.paintBounds = paintBounds; |
| 47 |
this.layoutBounds = layoutBounds; |
| 48 |
} |
| 49 |
|
| 50 |
static @NotNull Metrics createDefault() { |
| 51 |
return new Metrics( |
| 52 |
new Rectangle2D.Float(Length.UNSPECIFIED_RAW, Length.UNSPECIFIED_RAW, 0, 0), |
| 53 |
new Rectangle2D.Float(Length.UNSPECIFIED_RAW, Length.UNSPECIFIED_RAW, 0, 0)); |
| 54 |
} |
| 55 |
|
| 56 |
void union(@NotNull Metrics metrics) { |
| 57 |
if (Length.isUnspecified((float) paintBounds.getX())) { |
| 58 |
paintBounds.setRect(metrics.paintBounds); |
| 59 |
layoutBounds.setRect(metrics.layoutBounds); |
| 60 |
return; |
| 61 |
} |
| 62 |
Rectangle2D.union(paintBounds, metrics.paintBounds, paintBounds); |
| 63 |
Rectangle2D.union(layoutBounds, metrics.layoutBounds, layoutBounds); |
| 64 |
} |
| 65 |
|
| 66 |
@NotNull |
| 67 |
Metrics copy() { |
| 68 |
return new Metrics(this.paintBounds, this.layoutBounds); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
private final @NotNull T shape; |
| 73 |
private final @NotNull Metrics metrics; |
| 74 |
private final @NotNull List<@NotNull PaintableEmoji> emojis; |
| 75 |
|
| 76 |
public AbstractGlyphRun(@NotNull T shape, @NotNull Metrics metrics, @NotNull List<@NotNull PaintableEmoji> emojis) { |
| 77 |
this.shape = shape; |
| 78 |
this.metrics = metrics; |
| 79 |
this.emojis = emojis; |
| 80 |
} |
| 81 |
|
| 82 |
public @NotNull T shape() { |
| 83 |
return shape; |
| 84 |
} |
| 85 |
|
| 86 |
public @NotNull Metrics metrics() { |
| 87 |
return metrics; |
| 88 |
} |
| 89 |
|
| 90 |
public @NotNull List<@NotNull PaintableEmoji> emojis() { |
| 91 |
return emojis; |
| 92 |
} |
| 93 |
|
| 94 |
public static class PaintableEmoji { |
| 95 |
private final @NotNull EmojiGlyph glyph; |
| 96 |
private final @NotNull AffineTransform transform; |
| 97 |
|
| 98 |
PaintableEmoji(@NotNull EmojiGlyph glyph, @NotNull AffineTransform transform) { |
| 99 |
this.glyph = glyph; |
| 100 |
this.transform = transform; |
| 101 |
} |
| 102 |
|
| 103 |
public void render(@NotNull Output output, @NotNull SVGFont font) { |
| 104 |
output.applyTransform(transform); |
| 105 |
int fontSize = font.size(); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
int maxFontSize = SystemUtil.isMacOS ? 100 : Integer.MAX_VALUE; |
| 110 |
|
| 111 |
if (output.transform().getScaleY() * fontSize > maxFontSize) { |
| 112 |
|
| 113 |
float baselinePosition = 0.9f; |
| 114 |
if (glyph.largeBitmap == null) { |
| 115 |
|
| 116 |
BufferedImage bitmap = ImageUtil.createCompatibleTransparentImage(maxFontSize, maxFontSize); |
| 117 |
Graphics g = bitmap.getGraphics(); |
| 118 |
g.setFont(g.getFont().deriveFont((float) maxFontSize)); |
| 119 |
|
| 120 |
g.drawString(glyph.codepoint(), 0, (int) (baselinePosition * maxFontSize)); |
| 121 |
g.dispose(); |
| 122 |
glyph.largeBitmap = bitmap; |
| 123 |
} |
| 124 |
output.scale((double) fontSize / maxFontSize, (double) fontSize / maxFontSize); |
| 125 |
output.translate(0, -(int) (baselinePosition * maxFontSize)); |
| 126 |
output.drawImage(glyph.largeBitmap); |
| 127 |
} else { |
| 128 |
if (output instanceof Graphics2DOutput) { |
| 129 |
Graphics2D g = ((Graphics2DOutput) output).graphics(); |
| 130 |
g.setFont(g.getFont().deriveFont((float) fontSize)); |
| 131 |
g.drawString(glyph.codepoint(), 0, 0); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |