11package tables
22
3- import "fmt"
3+ import (
4+ "fmt"
5+ "sort"
6+ )
47
58func ParseCOLR (src []byte ) (COLR1 , error ) {
69 header , _ , err := parseColr0 (src )
@@ -27,19 +30,17 @@ type colr0 struct {
2730 numLayerRecords uint16 // Number of Layer records.
2831}
2932
30- func (cl colr0 ) paintForGlyph (g GlyphID ) (PaintColrLayersResolved , bool ) {
31- for i , j := 0 , len (cl .baseGlyphRecords ); i < j ; {
32- h := i + (j - i )/ 2
33- entry := cl .baseGlyphRecords [h ]
34- if g < entry .GlyphID {
35- j = h
36- } else if entry .GlyphID < g {
37- i = h + 1
38- } else {
39- return cl .layerRecords [entry .FirstLayerIndex : entry .FirstLayerIndex + entry .NumLayers ], true
40- }
33+ func (cl colr0 ) paintForGlyph (gi GlyphID ) (PaintColrLayersResolved , bool ) {
34+ num := len (cl .baseGlyphRecords )
35+ idx := sort .Search (num , func (i int ) bool { return gi <= cl .baseGlyphRecords [i ].GlyphID })
36+ if idx >= num {
37+ return nil , false
4138 }
42- return nil , false
39+ entry := cl .baseGlyphRecords [idx ]
40+ if gi != entry .GlyphID {
41+ return nil , false
42+ }
43+ return cl .layerRecords [entry .FirstLayerIndex : entry .FirstLayerIndex + entry .NumLayers ], true
4344}
4445
4546type COLR1 struct {
@@ -79,20 +80,17 @@ type baseGlyphList struct {
7980 paintRecords []baseGlyphPaintRecord `arrayCount:"FirstUint32"` // numBaseGlyphPaintRecords
8081}
8182
82- func (bl baseGlyphList ) paintForGlyph (g GlyphID ) (PaintTable , bool ) {
83- // binary search
84- for i , j := 0 , len (bl .paintRecords ); i < j ; {
85- h := i + (j - i )/ 2
86- entry := bl .paintRecords [h ]
87- if g < entry .GlyphID {
88- j = h
89- } else if entry .GlyphID < g {
90- i = h + 1
91- } else {
92- return entry .Paint , true
93- }
83+ func (bl baseGlyphList ) paintForGlyph (gi GlyphID ) (PaintTable , bool ) {
84+ num := len (bl .paintRecords )
85+ idx := sort .Search (num , func (i int ) bool { return gi <= bl .paintRecords [i ].GlyphID })
86+ if idx >= num {
87+ return nil , false
9488 }
95- return nil , false
89+ entry := bl .paintRecords [idx ]
90+ if gi != entry .GlyphID {
91+ return nil , false
92+ }
93+ return entry .Paint , true
9694}
9795
9896type baseGlyphPaintRecord struct {
0 commit comments