-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInlineSvgIconExample.java
More file actions
178 lines (161 loc) · 8.45 KB
/
Copy pathInlineSvgIconExample.java
File metadata and controls
178 lines (161 loc) · 8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.demcha.examples.features.text;
import com.demcha.compose.GraphCompose;
import com.demcha.compose.document.api.DocumentPageSize;
import com.demcha.compose.document.api.DocumentSession;
import com.demcha.compose.document.dsl.RichText;
import com.demcha.compose.document.dsl.SectionBuilder;
import com.demcha.compose.document.style.DocumentColor;
import com.demcha.compose.document.style.DocumentInsets;
import com.demcha.compose.document.style.DocumentTextStyle;
import com.demcha.compose.document.svg.SvgIcon;
import com.demcha.compose.document.theme.BusinessTheme;
import com.demcha.compose.font.FontName;
import com.demcha.examples.support.ExampleOutputPaths;
import java.nio.file.Path;
import java.util.function.Consumer;
/**
* Runnable showcase for inline SVG-icon runs ({@code @since 1.9.0}).
*
* <p>Parsed {@link SvgIcon}s are placed on the text baseline with
* {@code RichText.svgIcon(icon, size)} / {@code ParagraphBuilder.inlineSvgIcon(...)},
* so multi-colour vector glyphs flow inside a line of text — crisp at any zoom,
* carrying their own colours, with no dependence on the active font's glyph
* coverage. This is the engine path for vector colour emoji: a {@code :rocket:}
* shortcode becomes a Twemoji SVG dropped inline. The glyphs below are
* hand-authored stand-ins (gold star, green check badge, gradient orb, info and
* warning marks) until the {@code graph-compose-emoji} Twemoji pack ships.</p>
*/
public final class InlineSvgIconExample {
private static final BusinessTheme THEME = BusinessTheme.modern();
private static final DocumentColor MUTED = DocumentColor.rgb(112, 116, 128);
private static final DocumentColor BRAND = DocumentColor.rgb(20, 80, 95);
private static final DocumentColor PANEL = DocumentColor.rgb(248, 244, 234);
/** Gold five-point star. */
private static final SvgIcon STAR = SvgIcon.parse("""
<svg viewBox="0 0 24 24">
<polygon points="12,2 15,9 22,9 16,14 18,21 12,17 6,21 8,14 2,9 9,9" fill="#F5A623"/>
</svg>
""");
/** Green disc with a white tick — a two-layer "done" badge. */
private static final SvgIcon CHECK = SvgIcon.parse("""
<svg viewBox="0 0 24 24">
<circle cx="12" cy="12" r="11" fill="#22C55E"/>
<path d="M7 12 L11 16 L17 8" fill="none" stroke="#FFFFFF" stroke-width="2.6"/>
</svg>
""");
/** Violet gradient orb — exercises the inline gradient paint path. */
private static final SvgIcon ORB = SvgIcon.parse("""
<svg viewBox="0 0 24 24">
<defs>
<linearGradient id="g" gradientUnits="userSpaceOnUse" x1="2" y1="2" x2="22" y2="22">
<stop offset="0" stop-color="#A78BFA"/>
<stop offset="1" stop-color="#6128D9"/>
</linearGradient>
</defs>
<circle cx="12" cy="12" r="11" fill="url(#g)"/>
</svg>
""");
/** Blue info badge — disc, dot and stem. */
private static final SvgIcon INFO = SvgIcon.parse("""
<svg viewBox="0 0 24 24">
<circle cx="12" cy="12" r="11" fill="#3B82F6"/>
<circle cx="12" cy="7" r="1.6" fill="#FFFFFF"/>
<rect x="10.6" y="10" width="2.8" height="8" fill="#FFFFFF"/>
</svg>
""");
/** Amber warning triangle. */
private static final SvgIcon WARN = SvgIcon.parse("""
<svg viewBox="0 0 24 24">
<polygon points="12,2 23,21 1,21" fill="#F59E0B"/>
<rect x="10.8" y="8" width="2.4" height="7" fill="#FFFFFF"/>
<rect x="10.8" y="16.4" width="2.4" height="2.4" fill="#FFFFFF"/>
</svg>
""");
private InlineSvgIconExample() {
}
public static Path generate() throws Exception {
Path outputFile = ExampleOutputPaths.prepare("features/text", "inline-svg-icons.pdf");
try (DocumentSession document = GraphCompose.document(outputFile)
.pageSize(DocumentPageSize.A4)
.pageBackground(THEME.pageBackground())
.margin(34, 34, 34, 34)
.create()) {
document.pageFlow()
.name("InlineSvgIconShowcase")
.spacing(14)
.addSection("Hero", section -> section
.softPanel(THEME.palette().surfaceMuted(), 10, 16)
.accentLeft(DocumentColor.rgb(97, 40, 217), 4)
.spacing(6)
.addParagraph(p -> p
.text("Inline SVG icons")
.textStyle(THEME.text().h1())
.margin(DocumentInsets.zero()))
.addRich(rich -> rich
.plain("Multi-colour vector glyphs drawn on the text baseline ")
.accent("from SVG, not font glyphs", BRAND)
.plain(" — the engine path for vector colour emoji. ")
.svgIcon(STAR, 11).plain(" ").svgIcon(CHECK, 11)
.plain(" ").svgIcon(ORB, 11)))
.addSection("Status", section -> labelledRow(section,
"svgIcon(icon, size) — a coloured glyph between words",
rich -> rich
.svgIcon(CHECK, 10).plain(" Deploy succeeded ")
.svgIcon(WARN, 10).plain(" Disk almost full ")
.svgIcon(INFO, 10).plain(" 3 updates available")))
.addSection("Bullets", section -> labelledRow(section,
"any SvgIcon as a list bullet",
rich -> rich
.svgIcon(STAR, 10).plain(" Crisp at any zoom — true vector ")
.svgIcon(ORB, 10).plain(" Gradients render inline ")
.svgIcon(CHECK, 10).plain(" No font glyph required")))
.addSection("Sizing", section -> section
.softPanel(PANEL, 6, 12)
.spacing(5)
.addParagraph(p -> p
.text("size is the glyph's height in points; width follows the icon's aspect ratio")
.textStyle(caption())
.margin(DocumentInsets.zero()))
.addRich(rich -> rich
.plain("Scales with the text ")
.svgIcon(ORB, 8).plain(" ")
.svgIcon(ORB, 12).plain(" ")
.svgIcon(ORB, 16).plain(" ")
.svgIcon(ORB, 22)))
.addSection("Footer", section -> section
.accentTop(THEME.palette().rule(), 0.6)
.padding(new DocumentInsets(8, 0, 0, 0))
.addRich(rich -> rich
.plain("Source: ")
.style("examples/.../InlineSvgIconExample.java",
DocumentTextStyle.builder()
.fontName(FontName.COURIER)
.size(8)
.color(MUTED)
.build())))
.build();
document.buildPdf();
}
return outputFile;
}
public static void main(String[] args) throws Exception {
System.out.println("Generated: " + generate());
}
private static void labelledRow(SectionBuilder section, String label, Consumer<RichText> body) {
section
.softPanel(PANEL, 6, 12)
.spacing(4)
.addParagraph(p -> p
.text(label)
.textStyle(caption())
.margin(DocumentInsets.zero()))
.addRich(body::accept);
}
private static DocumentTextStyle caption() {
return DocumentTextStyle.builder()
.fontName(FontName.HELVETICA_BOLD)
.size(8.5)
.color(MUTED)
.build();
}
}