Skip to content

Commit d168428

Browse files
authored
🤖 Merge PR DefinitelyTyped#74385 Fontkit: add COLR, CPAL and Glyph .layers by @danmarshall
1 parent 9eb58e5 commit d168428

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

‎types/fontkit/fontkit-tests.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,30 @@ openV2.then(font => {
9090
font.getFont("hhe"); // $ExpectType Font | null
9191
}
9292
});
93+
94+
// ----------------------------
95+
// API: Font.COLR and Font.CPAL
96+
// ----------------------------
97+
openV2.then(font => {
98+
if (!isCollection(font)) {
99+
font.COLR?.version; // $ExpectType number | undefined
100+
font.COLR?.baseGlyphRecord[0].gid; // $ExpectType number | undefined
101+
font.COLR?.layerRecords[0].paletteIndex; // $ExpectType number | undefined
102+
103+
font.CPAL?.numPalettes; // $ExpectType number | undefined
104+
font.CPAL?.colorRecords[0].red; // $ExpectType number | undefined
105+
font.CPAL?.colorRecords[0].alpha; // $ExpectType number | undefined
106+
}
107+
});
108+
109+
// ------------------
110+
// API: Glyph.layers
111+
// ------------------
112+
openV2.then(font => {
113+
if (!isCollection(font)) {
114+
const glyph = font.glyphForCodePoint(0x1F600);
115+
glyph.layers?.[0].glyph; // $ExpectType Glyph | undefined
116+
glyph.layers?.[0].color.red; // $ExpectType number | undefined
117+
glyph.layers?.[0].color.alpha; // $ExpectType number | undefined
118+
}
119+
});

‎types/fontkit/index.d.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export interface Font {
3838
"OS/2": Os2Table;
3939
/** the font's horizontal header table consisting of information needed to layout fonts with horizontal characters */
4040
hhea: HHEA;
41+
/** the color table containing layer records for color glyphs (optional, present in fonts with color glyphs) */
42+
COLR?: COLRTable;
43+
/** the color palette table containing color definitions (optional, present in fonts with color glyphs) */
44+
CPAL?: CPALTable;
4145

4246
/** the number of glyphs in the font */
4347
numGlyphs: number;
@@ -243,6 +247,12 @@ export interface Glyph {
243247

244248
/** The glyph's name. Commonly the character, or 'space' or UTF**** */
245249
name: string;
250+
251+
/**
252+
* For COLR glyphs, an array of objects containing the glyph and color for each layer.
253+
* Each layer has a `glyph` property with the glyph object and a `color` property with RGBA values.
254+
*/
255+
layers?: Array<{ glyph: Glyph; color: { red: number; green: number; blue: number; alpha: number } }>;
246256
}
247257

248258
/**
@@ -373,6 +383,35 @@ export interface HHEA {
373383
numberOfMetrics: number;
374384
}
375385

386+
export interface COLRTable {
387+
version: number;
388+
numBaseGlyphRecords: number;
389+
baseGlyphRecord: Array<{
390+
gid: number;
391+
firstLayerIndex: number;
392+
numLayers: number;
393+
}>;
394+
layerRecords: Array<{
395+
gid: number;
396+
paletteIndex: number;
397+
}>;
398+
numLayerRecords: number;
399+
}
400+
401+
export interface CPALTable {
402+
version: number;
403+
numPaletteEntries: number;
404+
numPalettes: number;
405+
numColorRecords: number;
406+
colorRecords: Array<{
407+
red: number;
408+
green: number;
409+
blue: number;
410+
alpha: number;
411+
}>;
412+
colorRecordIndices: number[];
413+
}
414+
376415
export interface FontCollection {
377416
type: "TTC" | "DFont";
378417
getFont(name: string): Font | null;

0 commit comments

Comments
 (0)