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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//! The colour-emoji test fonts under `crates/crcbl-ui/tests/fixtures/emoji`,
//! parsed once each.
//!
//! Each is a few glyphs on a 16-unit em, 12 units up and 4 down, every glyph
//! 16 units wide, so at 16 pixels per em a unit is a pixel and a test can
//! work out every pixel by hand:
//!
//! * `text.ttf` — an outline heart at U+2764, and an outline digit one and
//! combining keycap (U+20E3), and nothing in colour.
//! * `cbdt.ttf` — CBDT/CBLC strikes at 16 and 8 pixels per em: U+1F600 in
//! both (red and blue halves at 16, green at 8), U+2764 only at 16
//! (magenta), and U+1F4A5 at 16 with a PNG that does not decode.
//! * `sbix.ttf` — sbix strikes at 16 (yellow) and 8 (cyan) for U+1F600, each
//! with its origin a quarter of the strike below the baseline.
//! * `colrv0.ttf` — COLR v0 and CPAL: U+1F600 is three overlapping squares in
//! red, blue and half-transparent green, and U+1F601 three layers of one.
//! * `colrv1.ttf` — COLR v1 paint graphs from U+1F7E5: a solid fill, linear,
//! radial and sweep gradients, a translation, a composite, a nested clip,
//! and forty nested transforms.
//! * `shaped.ttf` — a space, outlines for U+2764, U+1F525 and the regional
//! indicators U and S, a zero-width joiner, a `ccmp` GSUB feature joining
//! heart, joiner and fire, heart and fire, and U and S each into one COLR v0
//! glyph — a ZWJ sequence, a ligature of two clusters and a flag whose
//! components have no colour, as Segoe UI Emoji's flags have none — and a
//! GPOS `kern` feature kerning heart before heart by 3 units, heart before
//! space by 4 and space before heart by 2; and outlines for the digit one,
//! VS16 and the combining keycap, which `ccmp` joins (with or without the
//! selector) into a COLR v0 keycap, as Segoe UI Emoji's keycaps are: the
//! digit alone has no colour.
//!
//! They were built by this script, run as `python -I make_fixtures.py
//! crates/crcbl-ui/tests/fixtures/emoji`:
//!
//! ```text
//! """crcbl-ui's colour-emoji test fonts: python -I make_fixtures.py <out dir>.
//!
//! fontTools 4.67 and Pillow 12.3. Every font is 16 units to the em, 12 up and
//! 4 down, and every glyph 16 units wide.
//! """
//!
//! import io
//! import sys
//!
//! from fontTools.colorLib.builder import buildCOLR, buildCPAL
//! from fontTools.feaLib.builder import addOpenTypeFeaturesFromString
//! from fontTools.fontBuilder import FontBuilder
//! from fontTools.pens.ttGlyphPen import TTGlyphPen
//! from fontTools.ttLib import newTable
//! from fontTools.ttLib.tables import otTables as ot
//! from fontTools.ttLib.tables.BitmapGlyphMetrics import SmallGlyphMetrics
//! from fontTools.ttLib.tables.C_B_D_T_ import cbdt_bitmap_format_17
//! from fontTools.ttLib.tables.E_B_L_C_ import (
//! BitmapSizeTable, SbitLineMetrics, Strike, eblc_index_sub_table_1)
//! from fontTools.ttLib.tables.sbixGlyph import Glyph as SbixGlyph
//! from fontTools.ttLib.tables.sbixStrike import Strike as SbixStrike
//! from PIL import Image
//!
//! UPEM, ASCENT, DESCENT, ADVANCE = 16, 12, -4, 16
//! LINE_FIELDS = ("caretSlopeNumerator", "caretSlopeDenominator", "caretOffset",
//! "minOriginSB", "minAdvanceSB", "maxBeforeBL", "minAfterBL",
//! "pad1", "pad2")
//!
//!
//! def square(x0, y0, x1, y1):
//! pen = TTGlyphPen(None)
//! pen.moveTo((x0, y0))
//! pen.lineTo((x0, y1))
//! pen.lineTo((x1, y1))
//! pen.lineTo((x1, y0))
//! pen.closePath()
//! return pen.glyph()
//!
//!
//! def empty():
//! return TTGlyphPen(None).glyph()
//!
//!
//! def build(name, glyphs, cmap):
//! """A font of `glyphs` (name: outline) mapped by `cmap`."""
//! order = [".notdef"] + list(glyphs)
//! fb = FontBuilder(UPEM, isTTF=True)
//! fb.setupGlyphOrder(order)
//! fb.setupCharacterMap(cmap)
//! fb.setupGlyf({g: glyphs.get(g, empty()) for g in order})
//! glyf = fb.font["glyf"]
//! for g in order:
//! glyf[g].recalcBounds(glyf)
//! fb.setupHorizontalMetrics(
//! {g: (ADVANCE, getattr(glyf[g], "xMin", 0)) for g in order})
//! fb.setupHorizontalHeader(ascent=ASCENT, descent=DESCENT)
//! fb.setupNameTable({"familyName": name, "styleName": "Regular"})
//! fb.setupOS2(sTypoAscender=ASCENT, sTypoDescender=DESCENT,
//! usWinAscent=ASCENT, usWinDescent=-DESCENT)
//! fb.setupPost()
//! return fb.font
//!
//!
//! def png(size, fill):
//! """A `size`-square PNG, `fill(x, y)` each pixel's RGBA."""
//! image = Image.new("RGBA", (size, size))
//! for y in range(size):
//! for x in range(size):
//! image.putpixel((x, y), fill(x, y))
//! out = io.BytesIO()
//! image.save(out, "PNG")
//! return out.getvalue()
//!
//!
//! RED, BLUE = (255, 0, 0, 255), (0, 0, 255, 255)
//! SMILE16 = png(16, lambda x, y: RED if x < 8 else BLUE)
//! SMILE8 = png(8, lambda x, y: (0, 255, 0, 255))
//! HEART16 = png(16, lambda x, y: (255, 0, 255, 255))
//! # A PNG signature and the IHDR size an sbix reader peeks at, then garbage.
//! BROKEN = b"\x89PNG\r\n\x1a\n\0\0\0\x0dIHDR" + b"\0\0\0\x10" * 2 + b"not a png"
//! YELLOW16 = png(16, lambda x, y: (255, 255, 0, 255))
//! CYAN8 = png(8, lambda x, y: (0, 255, 255, 255))
//!
//!
//! def text_font():
//! return build("Text", {"space": empty(), "heart": square(2, 0, 14, 12),
//! "one": square(6, 0, 10, 12), "keycap": empty()},
//! {0x20: "space", 0x2764: "heart", 0x31: "one", 0x20E3: "keycap"})
//!
//!
//! def cbdt_strike(font, ppem, images):
//! """One CBLC strike of `images` (glyph: PNG) and its CBDT glyphs."""
//! data = {}
//! for glyph, image in images.items():
//! bitmap = cbdt_bitmap_format_17(None, None)
//! metrics = bitmap.metrics = SmallGlyphMetrics()
//! metrics.width = metrics.height = metrics.Advance = ppem
//! metrics.BearingX, metrics.BearingY = 0, ppem * ASCENT // UPEM
//! bitmap.imageData = image
//! data[glyph] = bitmap
//! strike = Strike()
//! size = strike.bitmapSizeTable = BitmapSizeTable()
//! for direction in ("hori", "vert"):
//! line = SbitLineMetrics()
//! line.ascender = ppem * ASCENT // UPEM
//! line.descender = ppem * DESCENT // UPEM
//! line.widthMax = ppem
//! for field in LINE_FIELDS:
//! setattr(line, field, 0)
//! setattr(size, direction, line)
//! size.colorRef, size.bitDepth, size.flags = 0, 32, 1
//! size.ppemX = size.ppemY = ppem
//! ids = sorted(font.getGlyphID(g) for g in images)
//! if ids != list(range(ids[0], ids[-1] + 1)):
//! raise ValueError("a strike's glyphs must be consecutive")
//! size.startGlyphIndex, size.endGlyphIndex = ids[0], ids[-1]
//! index = eblc_index_sub_table_1(None, None)
//! index.indexFormat, index.imageFormat = 1, 17
//! index.firstGlyphIndex, index.lastGlyphIndex = ids[0], ids[-1]
//! index.names = [font.getGlyphName(g) for g in ids]
//! strike.indexSubTables = [index]
//! return strike, data
//!
//!
//! def cbdt_font():
//! font = build("Cbdt", {"smile": empty(), "heart": empty(), "broken": empty()},
//! {0x1F600: "smile", 0x2764: "heart", 0x1F4A5: "broken"})
//! cblc, cbdt = newTable("CBLC"), newTable("CBDT")
//! cblc.version = cbdt.version = 3.0
//! cblc.strikes, cbdt.strikeData = [], []
//! strikes = {16: {"smile": SMILE16, "heart": HEART16, "broken": BROKEN},
//! 8: {"smile": SMILE8}}
//! for ppem, images in strikes.items():
//! strike, data = cbdt_strike(font, ppem, images)
//! cblc.strikes.append(strike)
//! cbdt.strikeData.append(data)
//! font["CBDT"], font["CBLC"] = cbdt, cblc
//! return font
//!
//!
//! def sbix_font():
//! font = build("Sbix", {"smile": empty()}, {0x1F600: "smile"})
//! sbix = newTable("sbix")
//! sbix.version, sbix.flags = 1, 1
//! for ppem, image in ((16, YELLOW16), (8, CYAN8)):
//! strike = SbixStrike(ppem=ppem, resolution=72)
//! for glyph in font.getGlyphOrder():
//! strike.glyphs[glyph] = SbixGlyph(glyphName=glyph)
//! strike.glyphs["smile"] = SbixGlyph(
//! glyphName="smile", graphicType="png ", imageData=image,
//! originOffsetX=0, originOffsetY=ppem * DESCENT // UPEM)
//! sbix.strikes[ppem] = strike
//! font["sbix"] = sbix
//! return font
//!
//!
//! def colrv0_font():
//! glyphs = {"smile": square(0, 0, 12, 12), "many": square(0, 0, 12, 12),
//! "la": square(0, 0, 8, 8), "lb": square(4, 4, 12, 12),
//! "lc": square(2, 2, 6, 6)}
//! font = build("Colr0", glyphs, {0x1F600: "smile", 0x1F601: "many"})
//! layers = {"smile": [("la", 0), ("lb", 1), ("lc", 2)], "many": [("la", 0)] * 3}
//! font["COLR"] = buildCOLR(layers, version=0, glyphMap=font.getReverseGlyphMap())
//! font["CPAL"] = buildCPAL([[(1, 0, 0, 1), (0, 0, 1, 1), (0, 1, 0, 128 / 255)]])
//! return font
//!
//!
//! def solid(index):
//! return {"Format": ot.PaintFormat.PaintSolid, "PaletteIndex": index, "Alpha": 1.0}
//!
//!
//! def glyph(name, paint):
//! return {"Format": ot.PaintFormat.PaintGlyph, "Paint": paint, "Glyph": name}
//!
//!
//! def red_to_blue():
//! stops = [{"StopOffset": o, "PaletteIndex": o, "Alpha": 1.0} for o in (0, 1)]
//! return {"Extend": "pad", "ColorStop": stops}
//!
//!
//! def colrv1_font():
//! names = ["solid", "linear", "radial", "sweep", "moved", "composite",
//! "clipped", "deep"]
//! glyphs = {"sq": square(0, 0, 12, 12), "la": square(0, 0, 8, 8),
//! "lb": square(4, 4, 12, 12), "dot": square(0, 0, 4, 4)}
//! glyphs.update({name: empty() for name in names})
//! font = build("Colr1", glyphs, {0x1F7E5 + i: n for i, n in enumerate(names)})
//! Format = ot.PaintFormat
//! deep = glyph("sq", solid(0))
//! for _ in range(40):
//! deep = {"Format": Format.PaintTranslate, "Paint": deep, "dx": 0, "dy": 0}
//! paints = {
//! "solid": glyph("sq", solid(0)),
//! "linear": glyph("sq", {
//! "Format": Format.PaintLinearGradient, "ColorLine": red_to_blue(),
//! "x0": 0, "y0": 0, "x1": 12, "y1": 0, "x2": 0, "y2": 12}),
//! "radial": glyph("sq", {
//! "Format": Format.PaintRadialGradient, "ColorLine": red_to_blue(),
//! "x0": 6, "y0": 6, "r0": 0, "x1": 6, "y1": 6, "r1": 6}),
//! "sweep": glyph("sq", {
//! "Format": Format.PaintSweepGradient, "ColorLine": red_to_blue(),
//! "centerX": 6, "centerY": 6, "startAngle": 0, "endAngle": 90}),
//! "moved": {"Format": Format.PaintTranslate, "Paint": glyph("dot", solid(1)),
//! "dx": 8, "dy": 4},
//! "composite": {"Format": Format.PaintComposite,
//! "SourcePaint": glyph("lb", solid(1)),
//! "CompositeMode": "src_in",
//! "BackdropPaint": glyph("la", solid(0))},
//! "clipped": glyph("la", glyph("lb", solid(2))),
//! "deep": deep,
//! }
//! font["COLR"] = buildCOLR(paints, version=1, glyphMap=font.getReverseGlyphMap())
//! font["CPAL"] = buildCPAL([[(1, 0, 0, 1), (0, 0, 1, 1), (0, 1, 0, 1)]])
//! return font
//!
//!
//! def shaped_font():
//! glyphs = {"space": empty(), "heart": square(2, 0, 14, 12), "fire": square(4, 0, 12, 12),
//! "zwj": empty(), "riu": square(0, 0, 6, 12), "ris": square(10, 0, 16, 12),
//! "heartfire": square(0, 0, 16, 12), "flagus": square(0, 0, 16, 12),
//! "la": square(0, 0, 16, 6), "lb": square(0, 6, 16, 12),
//! "one": square(6, 0, 10, 12), "vs16": empty(), "keycap": empty(),
//! "keycapone": square(0, 0, 16, 12)}
//! font = build("Shaped", glyphs, {0x20: "space", 0x2764: "heart", 0x1F525: "fire",
//! 0x200D: "zwj", 0x1F1FA: "riu", 0x1F1F8: "ris",
//! 0x31: "one", 0xFE0F: "vs16", 0x20E3: "keycap"})
//! addOpenTypeFeaturesFromString(font, """
//! languagesystem DFLT dflt;
//! feature ccmp {
//! sub heart zwj fire by heartfire;
//! sub riu ris by flagus;
//! sub heart fire by heartfire;
//! sub one vs16 keycap by keycapone;
//! sub one keycap by keycapone;
//! } ccmp;
//! feature kern {
//! pos heart heart -3;
//! pos heart space -4;
//! pos space heart -2;
//! } kern;
//! """)
//! layers = {"heartfire": [("la", 0), ("lb", 1)], "flagus": [("la", 1), ("lb", 0)],
//! "keycapone": [("la", 0), ("lb", 0)]}
//! font["COLR"] = buildCOLR(layers, version=0, glyphMap=font.getReverseGlyphMap())
//! font["CPAL"] = buildCPAL([[(1, 0, 0, 1), (0, 0, 1, 1)]])
//! return font
//!
//!
//! for name, make in (("text", text_font), ("cbdt", cbdt_font), ("sbix", sbix_font),
//! ("colrv0", colrv0_font), ("colrv1", colrv1_font),
//! ("shaped", shaped_font)):
//! make().save(f"{sys.argv[1]}/{name}.ttf")
//! ```
use OnceLock;
use Font;
/// Parses `bytes` the first time `cell` is asked, and clones it after.
/// `text.ttf`.
pub
/// `cbdt.ttf`.
pub
/// `sbix.ttf`.
pub
/// `colrv0.ttf`.
pub
/// `colrv1.ttf`.
pub
/// `shaped.ttf`.
pub