From a60ab1ac972b0a6535fa90642074909e2a39bd0e Mon Sep 17 00:00:00 2001 From: rdon-key Date: Wed, 3 Sep 2025 23:23:47 +0900 Subject: [PATCH 1/2] fix: GetGlyph falls back to index 0 when rune is not found --- const1bit/const1bit.go | 14 ++++++++++---- const2bit/const2bit.go | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/const1bit/const1bit.go b/const1bit/const1bit.go index 96f55f3..1565b69 100644 --- a/const1bit/const1bit.go +++ b/const1bit/const1bit.go @@ -77,28 +77,34 @@ func (f *Font) GetYAdvance() uint8 { } // GetGlyph returns the glyph corresponding to the specified rune in the font. +// If the rune does not exist in the font, the glyph at index 0 will be returned as a fallback. // Since there is only one glyph used for the return value in this package, // concurrent access is not allowed. Normally, there is no issue when using it from tinyfont. func (font *Font) GetGlyph(r rune) tinyfont.Glypher { s := 0 e := len(font.OffsetMap)/6 - 1 + found := false for s <= e { m := (s + e) / 2 - r2 := rune(font.OffsetMap[m*6])<<16 + rune(font.OffsetMap[m*6+1])<<8 + rune(font.OffsetMap[m*6+2]) - if r2 < r { + if r2 == r { + s = m + found = true + break + } else if r2 < r { s = m + 1 } else { e = m - 1 } } - if s > len(font.OffsetMap)/6-1 { + if !found { s = 0 } offset := uint32(font.OffsetMap[s*6+3])<<16 + uint32(font.OffsetMap[s*6+4])<<8 + uint32(font.OffsetMap[s*6+5]) + sz := uint32(len(font.Data[offset+5:])) if s*6+6 < len(font.OffsetMap) { 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 { font.glyph.XOffset = int8(font.Data[offset+3]) font.glyph.YOffset = int8(font.Data[offset+4]) font.glyph.Bitmaps = []byte(font.Data[offset+5 : offset+5+sz]) - return &(font.glyph) + return &font.glyph } diff --git a/const2bit/const2bit.go b/const2bit/const2bit.go index 53ff8ee..323e60f 100644 --- a/const2bit/const2bit.go +++ b/const2bit/const2bit.go @@ -83,28 +83,34 @@ func (f *Font) GetYAdvance() uint8 { } // GetGlyph returns the glyph corresponding to the specified rune in the font. +// If the rune does not exist in the font, the glyph at index 0 will be returned as a fallback. // Since there is only one glyph used for the return value in this package, // concurrent access is not allowed. Normally, there is no issue when using it from tinyfont. func (font *Font) GetGlyph(r rune) tinyfont.Glypher { s := 0 e := len(font.OffsetMap)/6 - 1 + found := false for s <= e { m := (s + e) / 2 - r2 := rune(font.OffsetMap[m*6])<<16 + rune(font.OffsetMap[m*6+1])<<8 + rune(font.OffsetMap[m*6+2]) - if r2 < r { + if r2 == r { + s = m + found = true + break + } else if r2 < r { s = m + 1 } else { e = m - 1 } } - if s > len(font.OffsetMap)/6-1 { + if !found { s = 0 } offset := uint32(font.OffsetMap[s*6+3])<<16 + uint32(font.OffsetMap[s*6+4])<<8 + uint32(font.OffsetMap[s*6+5]) + sz := uint32(len(font.Data[offset+5:])) if s*6+6 < len(font.OffsetMap) { 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 { font.glyph.XOffset = int8(font.Data[offset+3]) font.glyph.YOffset = int8(font.Data[offset+4]) font.glyph.Bitmaps = []byte(font.Data[offset+5 : offset+5+sz]) - return &(font.glyph) + return &font.glyph } From 0fc3f92008371d43285080ece0ba4cea26b78dd0 Mon Sep 17 00:00:00 2001 From: rdon Date: Mon, 10 Nov 2025 13:31:24 +0900 Subject: [PATCH 2/2] chore: code format --- const1bit/const1bit.go | 3 +-- const2bit/const2bit.go | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/const1bit/const1bit.go b/const1bit/const1bit.go index 1565b69..a622e9a 100644 --- a/const1bit/const1bit.go +++ b/const1bit/const1bit.go @@ -104,7 +104,6 @@ func (font *Font) GetGlyph(r rune) tinyfont.Glypher { } offset := uint32(font.OffsetMap[s*6+3])<<16 + uint32(font.OffsetMap[s*6+4])<<8 + uint32(font.OffsetMap[s*6+5]) - sz := uint32(len(font.Data[offset+5:])) if s*6+6 < len(font.OffsetMap) { sz = uint32(font.OffsetMap[s*6+9])<<16 + uint32(font.OffsetMap[s*6+10])<<8 + uint32(font.OffsetMap[s*6+11]) - offset @@ -117,5 +116,5 @@ func (font *Font) GetGlyph(r rune) tinyfont.Glypher { font.glyph.XOffset = int8(font.Data[offset+3]) font.glyph.YOffset = int8(font.Data[offset+4]) font.glyph.Bitmaps = []byte(font.Data[offset+5 : offset+5+sz]) - return &font.glyph + return &(font.glyph) } diff --git a/const2bit/const2bit.go b/const2bit/const2bit.go index 323e60f..e590c9a 100644 --- a/const2bit/const2bit.go +++ b/const2bit/const2bit.go @@ -110,7 +110,6 @@ func (font *Font) GetGlyph(r rune) tinyfont.Glypher { } offset := uint32(font.OffsetMap[s*6+3])<<16 + uint32(font.OffsetMap[s*6+4])<<8 + uint32(font.OffsetMap[s*6+5]) - sz := uint32(len(font.Data[offset+5:])) if s*6+6 < len(font.OffsetMap) { sz = uint32(font.OffsetMap[s*6+9])<<16 + uint32(font.OffsetMap[s*6+10])<<8 + uint32(font.OffsetMap[s*6+11]) - offset @@ -123,5 +122,5 @@ func (font *Font) GetGlyph(r rune) tinyfont.Glypher { font.glyph.XOffset = int8(font.Data[offset+3]) font.glyph.YOffset = int8(font.Data[offset+4]) font.glyph.Bitmaps = []byte(font.Data[offset+5 : offset+5+sz]) - return &font.glyph + return &(font.glyph) }