Skip to content

Commit

Permalink
ttf: add GlyphIsProvided32() (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorg authored Feb 20, 2025
1 parent d33fe82 commit 7f43f67
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 26 deletions.
73 changes: 47 additions & 26 deletions ttf/sdl_ttf.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ static inline int TTF_GlyphIsProvided(TTF_Font *font, Uint16 ch)
#endif
#if !SDL_TTF_VERSION_ATLEAST(2,0,18)
#if defined(WARN_OUTDATED)
#pragma message("TTF_GlyphIsProvided32 is not supported before SDL_ttf 2.0.18")
#endif
static inline int TTF_GlyphIsProvided32(TTF_Font *font, Uint32 ch)
{
return 0;
}
#endif
*/
import "C"
import (
Expand Down Expand Up @@ -629,45 +641,54 @@ func (f *Font) SetScriptName(script string) error {
return nil
}

// GlyphIsProvided checks whether a glyph is provided by the font for a 16-bit codepoint.
// Note that this version of the function takes a 16-bit character code, which covers the Basic Multilingual Plane, but is insufficient to cover the entire set of possible Unicode values, including emoji glyphs. You should use GlyphIsProvided32() instead, which offers the same functionality but takes a 32-bit codepoint instead.
func (f *Font) GlyphIsProvided(ch uint16) bool {
_ch := C.Uint16(ch)
return C.TTF_GlyphIsProvided(f.f, _ch) != 0
_ch := C.Uint16(ch)
return C.TTF_GlyphIsProvided(f.f, _ch) != 0
}

// GlyphIsProvided32 checks whether a glyph is provided by the font for a 32-bit codepoint.
// Note that this is the same as GlyphIsProvided(), but takes a 32-bit character instead of 16-bit, and thus can query a larger range. There's no reason not to use this function exclusively.
func (f *Font) GlyphIsProvided32(ch uint32) bool {
_ch := C.Uint32(ch)
return C.TTF_GlyphIsProvided32(f.f, _ch) != 0
}

func (f *Font) MeasureText(text string, measureWidth int) (extent, count int, err error) {
_text := C.CString(text)
defer C.free(unsafe.Pointer(_text))
_measureWidth := C.int(measureWidth)
_extend := (*C.int)(unsafe.Pointer(&extent))
_count := (*C.int)(unsafe.Pointer(&count))
ret := C.TTF_MeasureText(f.f, _text, _measureWidth, _extend, _count)
if ret != 0 {
err = GetError()
}
return
_measureWidth := C.int(measureWidth)
_extend := (*C.int)(unsafe.Pointer(&extent))
_count := (*C.int)(unsafe.Pointer(&count))
ret := C.TTF_MeasureText(f.f, _text, _measureWidth, _extend, _count)
if ret != 0 {
err = GetError()
}
return
}

func (f *Font) MeasureUTF8(text string, measureWidth int) (extent, count int, err error) {
_text := C.CString(text)
defer C.free(unsafe.Pointer(_text))
_measureWidth := C.int(measureWidth)
_extend := (*C.int)(unsafe.Pointer(&extent))
_count := (*C.int)(unsafe.Pointer(&count))
ret := C.TTF_MeasureUTF8(f.f, _text, _measureWidth, _extend, _count)
if ret != 0 {
err = GetError()
}
return
_measureWidth := C.int(measureWidth)
_extend := (*C.int)(unsafe.Pointer(&extent))
_count := (*C.int)(unsafe.Pointer(&count))
ret := C.TTF_MeasureUTF8(f.f, _text, _measureWidth, _extend, _count)
if ret != 0 {
err = GetError()
}
return
}

func (f *Font) MeasureUNICODE(text []uint16, measureWidth int) (extent, count int, err error) {
_text := (*C.Uint16)(&text[0])
_measureWidth := C.int(measureWidth)
_extend := (*C.int)(unsafe.Pointer(&extent))
_count := (*C.int)(unsafe.Pointer(&count))
ret := C.TTF_MeasureUNICODE(f.f, _text, _measureWidth, _extend, _count)
if ret != 0 {
err = GetError()
}
return
_measureWidth := C.int(measureWidth)
_extend := (*C.int)(unsafe.Pointer(&extent))
_count := (*C.int)(unsafe.Pointer(&count))
ret := C.TTF_MeasureUNICODE(f.f, _text, _measureWidth, _extend, _count)
if ret != 0 {
err = GetError()
}
return
}
33 changes: 33 additions & 0 deletions ttf/sdl_ttf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,36 @@ func TestTTF(t *testing.T) {
t.Errorf("GlyphMetrics got %v - want %v", *gm, expectMetrics)
}
}

func TestGlyphIsProvided(t *testing.T) {
var font *Font
var err error

if err := Init(); err != nil {
t.Errorf("Failed to initialize TTF: %s\n", err)
}

if font, err = OpenFont("../.go-sdl2-examples/assets/test.ttf", 32); err != nil {
t.Errorf("Failed to open font: %s\n", err)
}

asciiRune := 'A'

if !font.GlyphIsProvided(uint16(asciiRune)) {
t.Errorf("GlyphIsProvided(): rune not found in font that includes glyph")
}

if !font.GlyphIsProvided32(uint32(asciiRune)) {
t.Errorf("GlyphIsProvided32(): rune not found in font that includes glyph")
}

emojiRune := '🤖'

if font.GlyphIsProvided(uint16(emojiRune)) {
t.Errorf("GlyphIsProvided(): rune found in font that doesn't support emojis")
}

if font.GlyphIsProvided32(uint32(emojiRune)) {
t.Errorf("GlyphIsProvided32(): rune found in font that doesn't support emojis")
}
}

0 comments on commit 7f43f67

Please sign in to comment.