-
Notifications
You must be signed in to change notification settings - Fork 18
COLR table #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
COLR table #196
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5f6d414
[opentype/tables] first pass at parsing COLR table
benoitkugler bd0029b
bump dev deps
benoitkugler 55334a4
fix dev dep
benoitkugler 93d7609
[opentype] support for COLRv1 table
benoitkugler 1a4dc32
[opentype] initial CPAL support
benoitkugler 6235224
[opentype] CPAL support
benoitkugler f2aace7
[font] expose COLR API
benoitkugler edf19c3
[opentype/tables] use sort.Search
benoitkugler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package font | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/go-text/typesetting/font/opentype/tables" | ||
| ) | ||
|
|
||
| // Support for COLR and CPAL tables | ||
|
|
||
| // CPAL is the 'CPAL' table, | ||
| // with [numPalettes]x[numPaletteEntries] colors. | ||
| // CPAL[0] is the default palette | ||
| type CPAL [][]tables.ColorRecord | ||
|
|
||
| func newCPAL(table tables.CPAL) (CPAL, error) { | ||
| numPalettes := len(table.ColorRecordIndices) | ||
| numColors := len(table.ColorRecordsArray) | ||
|
|
||
| // "The first palette, palette index 0, is the default palette. | ||
| // A minimum of one palette must be provided in the CPAL table if the table is present. | ||
| // Palettes must have a minimum of one color record. An empty CPAL table, | ||
| // with no palettes and no color records is not permitted." | ||
| if numPalettes == 0 { | ||
| return nil, errors.New("empty CPAL table") | ||
| } | ||
| out := make(CPAL, numPalettes) | ||
| for i, startIndex := range table.ColorRecordIndices { | ||
| endIndex := int(startIndex) + int(table.NumPaletteEntries) | ||
| if endIndex > numColors { | ||
| return nil, fmt.Errorf("invalid CPAL table (expected at least %d colors, got %d)", endIndex, numColors) | ||
| } | ||
| out[i] = table.ColorRecordsArray[startIndex:endIndex] | ||
| } | ||
| return out, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package tables | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| tu "github.com/go-text/typesetting/testutils" | ||
| ) | ||
|
|
||
| func TestCOLR(t *testing.T) { | ||
| ft := readFontFile(t, "color/NotoColorEmoji-Regular.ttf") | ||
| colr, err := ParseCOLR(readTable(t, ft, "COLR")) | ||
| tu.AssertNoErr(t, err) | ||
| tu.Assert(t, len(colr.baseGlyphRecords) == 0) | ||
| tu.Assert(t, len(colr.layerRecords) == 0) | ||
| tu.Assert(t, len(colr.baseGlyphList.paintRecords) == 3845) | ||
| tu.Assert(t, colr.baseGlyphList.paintRecords[0].Paint == PaintColrLayers{1, 3, 47625}) | ||
| tu.Assert(t, colr.ClipList.clips[0].ClipBox == ClipBoxFormat1{1, 480, 192, 800, 512}) | ||
| tu.Assert(t, colr.VarIndexMap == nil && colr.ItemVariationStore == nil) | ||
| tu.Assert(t, len(colr.LayerList.paintTables) == 69264) | ||
|
|
||
| clipBox, ok := colr.ClipList.Search(87) | ||
| tu.Assert(t, ok && clipBox == ClipBoxFormat1{1, 64, -224, 1216, 928}) | ||
|
|
||
| // reference from fonttools | ||
| paint := colr.LayerList.paintTables[6] | ||
| transform, ok := paint.(PaintTransform) | ||
| tu.Assert(t, ok) | ||
| _, innerOK := transform.Paint.(PaintGlyph) | ||
| tu.Assert(t, transform.Transform == Affine2x3{1, 0, 0, 1, 4.3119965, 0.375}) | ||
| tu.Assert(t, innerOK) | ||
|
|
||
| _, ok = colr.Search(1) | ||
| tu.Assert(t, !ok) | ||
| _, ok = colr.Search(0xFFFF) | ||
| tu.Assert(t, !ok) | ||
|
|
||
| pt, ok := colr.Search(12) | ||
| asColrLayers, ok2 := pt.(PaintColrLayers) | ||
| tu.Assert(t, ok && ok2) | ||
| tu.Assert(t, asColrLayers == PaintColrLayers{1, 9, 2427}) | ||
|
|
||
| for _, paint := range colr.baseGlyphList.paintRecords { | ||
| if layers, ok := paint.Paint.(PaintColrLayers); ok { | ||
| l, err := colr.LayerList.Resolve(layers) | ||
| tu.AssertNoErr(t, err) | ||
| tu.Assert(t, len(l) == int(layers.NumLayers)) | ||
| } | ||
| } | ||
|
|
||
| ft = readFontFile(t, "color/CoralPixels-Regular.ttf") | ||
| colr, err = ParseCOLR(readTable(t, ft, "COLR")) | ||
| tu.AssertNoErr(t, err) | ||
| tu.Assert(t, len(colr.baseGlyphRecords) == 335) | ||
| tu.Assert(t, len(colr.layerRecords) == 5603) | ||
| g1, g2 := colr.baseGlyphRecords[0], colr.baseGlyphRecords[1] | ||
| tu.Assert(t, g1 == baseGlyph{0, 0, 11} && g2 == baseGlyph{2, 11, 18}) | ||
| tu.Assert(t, colr.layerRecords[0].PaletteIndex == 4) | ||
|
|
||
| _, ok = colr.Search(1) | ||
| tu.Assert(t, !ok) | ||
| _, ok = colr.Search(0xFFFF) | ||
| tu.Assert(t, !ok) | ||
|
|
||
| pt, ok = colr.Search(0) | ||
| asLayers, ok2 := pt.(PaintColrLayersResolved) | ||
| tu.Assert(t, ok && ok2) | ||
| tu.Assert(t, len(asLayers) == 11) | ||
| tu.Assert(t, asLayers[0].PaletteIndex == 4) | ||
| tu.Assert(t, asLayers[10].PaletteIndex == 11) | ||
| } | ||
|
|
||
| func TestCPAL(t *testing.T) { | ||
| ft := readFontFile(t, "color/NotoColorEmoji-Regular.ttf") | ||
| cpal, _, err := ParseCPAL(readTable(t, ft, "CPAL")) | ||
| tu.AssertNoErr(t, err) | ||
| tu.Assert(t, cpal.Version == 0) | ||
| tu.Assert(t, cpal.NumPaletteEntries == 5921) | ||
| tu.Assert(t, cpal.numPalettes == 1 && len(cpal.ColorRecordIndices) == 1) | ||
|
|
||
| ft = readFontFile(t, "color/CoralPixels-Regular.ttf") | ||
| cpal, _, err = ParseCPAL(readTable(t, ft, "CPAL")) | ||
| tu.AssertNoErr(t, err) | ||
| tu.Assert(t, cpal.Version == 0) | ||
| tu.Assert(t, cpal.NumPaletteEntries == 32) | ||
| tu.Assert(t, cpal.numPalettes == 2 && len(cpal.ColorRecordIndices) == 2) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't see those files in this PR or the repo before the PR - apologies if I am missing something obvious.
But given that I cannot understand how the tests are passing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are loading the test files from the typesetting-utils module, so that typesetting remains lightweight.