Skip to content

Commit 00be9f7

Browse files
rdon-keyrdon
andauthored
fix: GetGlyph falls back to index 0 when rune is not found (#59)
* fix: GetGlyph falls back to index 0 when rune is not found * chore: code format --------- Co-authored-by: rdon <you@example.com>
1 parent a3b43b4 commit 00be9f7

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

const1bit/const1bit.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,29 @@ 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.
8283
func (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

const2bit/const2bit.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,29 @@ 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.
8889
func (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

0 commit comments

Comments
 (0)