File tree Expand file tree Collapse file tree 2 files changed +20
-8
lines changed
Expand file tree Collapse file tree 2 files changed +20
-8
lines changed Original file line number Diff line number Diff line change @@ -77,28 +77,34 @@ func (f *Font) GetYAdvance() uint8 {
7777}
7878
7979// GetGlyph returns the glyph corresponding to the specified rune in the font.
80+ // If the rune does not exist in the font, the glyph at index 0 will be returned as a fallback.
8081// Since there is only one glyph used for the return value in this package,
8182// concurrent access is not allowed. Normally, there is no issue when using it from tinyfont.
8283func (font * Font ) GetGlyph (r rune ) tinyfont.Glypher {
8384 s := 0
8485 e := len (font .OffsetMap )/ 6 - 1
86+ found := false
8587
8688 for s <= e {
8789 m := (s + e ) / 2
88-
8990 r2 := rune (font .OffsetMap [m * 6 ])<< 16 + rune (font .OffsetMap [m * 6 + 1 ])<< 8 + rune (font .OffsetMap [m * 6 + 2 ])
90- if r2 < r {
91+ if r2 == r {
92+ s = m
93+ found = true
94+ break
95+ } else if r2 < r {
9196 s = m + 1
9297 } else {
9398 e = m - 1
9499 }
95100 }
96101
97- if s > len ( font . OffsetMap ) / 6 - 1 {
102+ if ! found {
98103 s = 0
99104 }
100105
101106 offset := uint32 (font .OffsetMap [s * 6 + 3 ])<< 16 + uint32 (font .OffsetMap [s * 6 + 4 ])<< 8 + uint32 (font .OffsetMap [s * 6 + 5 ])
107+
102108 sz := uint32 (len (font .Data [offset + 5 :]))
103109 if s * 6 + 6 < len (font .OffsetMap ) {
104110 sz = uint32 (font .OffsetMap [s * 6 + 9 ])<< 16 + uint32 (font .OffsetMap [s * 6 + 10 ])<< 8 + uint32 (font .OffsetMap [s * 6 + 11 ]) - offset
@@ -111,5 +117,5 @@ func (font *Font) GetGlyph(r rune) tinyfont.Glypher {
111117 font .glyph .XOffset = int8 (font .Data [offset + 3 ])
112118 font .glyph .YOffset = int8 (font .Data [offset + 4 ])
113119 font .glyph .Bitmaps = []byte (font .Data [offset + 5 : offset + 5 + sz ])
114- return & ( font .glyph )
120+ return & font .glyph
115121}
Original file line number Diff line number Diff line change @@ -83,28 +83,34 @@ func (f *Font) GetYAdvance() uint8 {
8383}
8484
8585// GetGlyph returns the glyph corresponding to the specified rune in the font.
86+ // If the rune does not exist in the font, the glyph at index 0 will be returned as a fallback.
8687// Since there is only one glyph used for the return value in this package,
8788// concurrent access is not allowed. Normally, there is no issue when using it from tinyfont.
8889func (font * Font ) GetGlyph (r rune ) tinyfont.Glypher {
8990 s := 0
9091 e := len (font .OffsetMap )/ 6 - 1
92+ found := false
9193
9294 for s <= e {
9395 m := (s + e ) / 2
94-
9596 r2 := rune (font .OffsetMap [m * 6 ])<< 16 + rune (font .OffsetMap [m * 6 + 1 ])<< 8 + rune (font .OffsetMap [m * 6 + 2 ])
96- if r2 < r {
97+ if r2 == r {
98+ s = m
99+ found = true
100+ break
101+ } else if r2 < r {
97102 s = m + 1
98103 } else {
99104 e = m - 1
100105 }
101106 }
102107
103- if s > len ( font . OffsetMap ) / 6 - 1 {
108+ if ! found {
104109 s = 0
105110 }
106111
107112 offset := uint32 (font .OffsetMap [s * 6 + 3 ])<< 16 + uint32 (font .OffsetMap [s * 6 + 4 ])<< 8 + uint32 (font .OffsetMap [s * 6 + 5 ])
113+
108114 sz := uint32 (len (font .Data [offset + 5 :]))
109115 if s * 6 + 6 < len (font .OffsetMap ) {
110116 sz = uint32 (font .OffsetMap [s * 6 + 9 ])<< 16 + uint32 (font .OffsetMap [s * 6 + 10 ])<< 8 + uint32 (font .OffsetMap [s * 6 + 11 ]) - offset
@@ -117,5 +123,5 @@ func (font *Font) GetGlyph(r rune) tinyfont.Glypher {
117123 font .glyph .XOffset = int8 (font .Data [offset + 3 ])
118124 font .glyph .YOffset = int8 (font .Data [offset + 4 ])
119125 font .glyph .Bitmaps = []byte (font .Data [offset + 5 : offset + 5 + sz ])
120- return & ( font .glyph )
126+ return & font .glyph
121127}
You can’t perform that action at this time.
0 commit comments