Skip to content

Commit 6235224

Browse files
committed
[opentype] CPAL support
1 parent 1a4dc32 commit 6235224

File tree

3 files changed

+27
-60
lines changed

3 files changed

+27
-60
lines changed

font/opentype/tables/glyphs_color_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,19 @@ func TestCOLR(t *testing.T) {
3535
tu.Assert(t, g1 == BaseGlyph{0, 0, 11} && g2 == BaseGlyph{2, 11, 18})
3636
tu.Assert(t, colr.LayerRecords[0].PaletteIndex == 4)
3737
}
38+
39+
func TestCPAL(t *testing.T) {
40+
ft := readFontFile(t, "color/NotoColorEmoji-Regular.ttf")
41+
cpal, _, err := ParseCPAL(readTable(t, ft, "CPAL"))
42+
tu.AssertNoErr(t, err)
43+
tu.Assert(t, cpal.Version == 0)
44+
tu.Assert(t, cpal.numPaletteEntries == 5921)
45+
tu.Assert(t, cpal.numPalettes == 1 && len(cpal.ColorRecordIndices) == 1)
46+
47+
ft = readFontFile(t, "color/CoralPixels-Regular.ttf")
48+
cpal, _, err = ParseCPAL(readTable(t, ft, "CPAL"))
49+
tu.AssertNoErr(t, err)
50+
tu.Assert(t, cpal.Version == 0)
51+
tu.Assert(t, cpal.numPaletteEntries == 32)
52+
tu.Assert(t, cpal.numPalettes == 2 && len(cpal.ColorRecordIndices) == 2)
53+
}

font/opentype/tables/glyphs_cpal_gen.go

Lines changed: 6 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

font/opentype/tables/glyphs_cpal_src.go

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,15 @@
11
package tables
22

3-
import "fmt"
4-
5-
func ParseCPAL(src []byte) (CPAL1, error) {
6-
header, _, err := parseCpal0(src)
7-
if err != nil {
8-
return CPAL1{}, err
9-
}
10-
switch header.Version {
11-
case 0:
12-
return CPAL1{cpal0: header}, nil
13-
case 1:
14-
out, _, err := ParseCPAL1(src)
15-
return out, err
16-
default:
17-
return CPAL1{}, fmt.Errorf("unsupported version for CPAL: %d", header.Version)
18-
}
19-
}
20-
213
// https://learn.microsoft.com/en-us/typography/opentype/spec/cpal
22-
type cpal0 struct {
4+
//
5+
// For now, only the CPAL version 0 is supported.
6+
type CPAL struct {
237
Version uint16 // Table version number
248
numPaletteEntries uint16 // Number of palette entries in each palette.
259
numPalettes uint16 // Number of palettes in the table.
2610
numColorRecords uint16 // Total number of color records, combined for all palettes.
27-
ColorRecordsArray []ColorRecord `arrayCount:"ComputedField-numColorRecords" offsetSize:"Offset32"` // Offset from the beginning of CPAL table to the first ColorRecord.
28-
ColorRecordIndices []uint16 `arrayCount:"ComputedField-numPalettes"` //[numPalettes] Index of each palette’s first color record in the combined color record array.
29-
}
30-
31-
type CPAL1 struct {
32-
cpal0
33-
paletteTypesArrayOffset Offset32 // Offset from the beginning of CPAL table to the Palette Types Array. Set to 0 if no array is provided.
34-
paletteLabelsArrayOffset Offset32 // Offset from the beginning of CPAL table to the Palette Labels Array. Set to 0 if no array is provided.
35-
paletteEntryLabelsArrayOffset Offset32 // Offset from the beginning of CPAL table to the Palette Entry Labels Array. Set to 0 if no array is provided.
11+
ColorRecordsArray []ColorRecord `arrayCount:"ComputedField-numColorRecords" offsetSize:"Offset32"` // Offset from the beginning of CPAL table to the first ColorRecord.
12+
ColorRecordIndices []uint16 `arrayCount:"ComputedField-numPalettes"` // [numPalettes] Index of each palette’s first color record in the combined color record array.
3613
}
3714

3815
type ColorRecord struct {

0 commit comments

Comments
 (0)