Debian Codegraph @

src/com/github/weisj/jsvg/nodes/text/AbstractGlyphRun.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2024-2026 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 // The JDK text pipeline on macOS can only render Emoji up to 100pt.
108 // In this case we have to paint to a buffer which is then scaled up.
109 int maxFontSize = SystemUtil.isMacOS ? 100 : Integer.MAX_VALUE;
110
111 if (output.transform().getScaleY() * fontSize > maxFontSize) {
112 // Guess where the baseline ought to be for descenders to be fully visible. Very hackish.
113 float baselinePosition = 0.9f;
114 if (glyph.largeBitmap == null) {
115 // Size of bitmap is guessed, as there is now reliable way to get the size of the glyph.
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 }