|
| 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