Skip to content

Commit 1a4dc32

Browse files
committed
[opentype] initial CPAL support
1 parent 93d7609 commit 1a4dc32

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

font/opentype/tables/glyphs_cpal_gen.go

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tables
2+
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+
21+
// https://learn.microsoft.com/en-us/typography/opentype/spec/cpal
22+
type cpal0 struct {
23+
Version uint16 // Table version number
24+
numPaletteEntries uint16 // Number of palette entries in each palette.
25+
numPalettes uint16 // Number of palettes in the table.
26+
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.
36+
}
37+
38+
type ColorRecord struct {
39+
Blue uint8 // Blue value (B0).
40+
Green uint8 // Green value (B1).
41+
Red uint8 // Red value (B2).
42+
Alpha uint8 // Alpha value (B3).
43+
}

0 commit comments

Comments
 (0)