Skip to content

Commit 446fe94

Browse files
committed
Add charmap iteration API
This commit adds a set of APIs for iterating over the set of codepoints for which the font provides a glyph. This implementation calls FreeType directly to iterate over the charmap of font->face. This is intended to be used in for loops as follows: for (Uint32 ch = TTF_GetFirstFontChar(font); ch != UINT32_MAX; ch = TTF_GetNextFontChar(font)) { /* do something... */ }
1 parent 2a69525 commit 446fe94

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

include/SDL3_ttf/SDL_ttf.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,38 @@ extern SDL_DECLSPEC bool SDLCALL TTF_SetFontLanguage(TTF_Font *font, const char
11321132
*/
11331133
extern SDL_DECLSPEC bool SDLCALL TTF_FontHasGlyph(TTF_Font *font, Uint32 ch);
11341134

1135+
/**
1136+
* Get the first UNICODE codepoint for which the font provides a glyph.
1137+
*
1138+
* \param font the font to query.
1139+
* \returns the first codepoint, or UINT32_MAX if the font's charmap is empty.
1140+
*
1141+
* \threadsafety This function should be called on the thread that created the
1142+
* font.
1143+
*
1144+
* \since This function is available since SDL_ttf 3.4.0.
1145+
*
1146+
* \sa TTF_GetNextFontChar
1147+
*/
1148+
extern SDL_DECLSPEC Uint32 SDLCALL TTF_GetFirstFontChar(TTF_Font *font);
1149+
1150+
/**
1151+
* Get the next UNICODE codepoint for which the font provides a glyph.
1152+
*
1153+
* \param font the font to query.
1154+
* \param ch the current codepoint.
1155+
* \returns the next codepoint, or UINT32_MAX if the end of the font's charmap
1156+
* is reached.
1157+
*
1158+
* \threadsafety This function should be called on the thread that created the
1159+
* font.
1160+
*
1161+
* \since This function is available since SDL_ttf 3.4.0.
1162+
*
1163+
* \sa TTF_GetFirstFontChar
1164+
*/
1165+
extern SDL_DECLSPEC Uint32 SDLCALL TTF_GetNextFontChar(TTF_Font *font, Uint32 ch);
1166+
11351167
/**
11361168
* The type of data in a glyph image
11371169
*

src/SDL_ttf.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,6 +3160,32 @@ bool TTF_FontHasGlyph(TTF_Font *font, Uint32 ch)
31603160
return (get_char_index_fallback(font, ch, NULL, NULL) > 0);
31613161
}
31623162

3163+
Uint32 TTF_GetFirstFontChar(TTF_Font *font)
3164+
{
3165+
TTF_CHECK_FONT(font, UINT32_MAX);
3166+
3167+
FT_UInt idx;
3168+
Uint32 ch = (Uint32)FT_Get_First_Char(font->face, &idx);
3169+
3170+
if (idx == 0) {
3171+
return UINT32_MAX;
3172+
}
3173+
return ch;
3174+
}
3175+
3176+
Uint32 TTF_GetNextFontChar(TTF_Font *font, Uint32 ch)
3177+
{
3178+
TTF_CHECK_FONT(font, UINT32_MAX);
3179+
3180+
FT_UInt idx;
3181+
Uint32 next = (Uint32)FT_Get_Next_Char(font->face, ch, &idx);
3182+
3183+
if (idx == 0) {
3184+
return UINT32_MAX;
3185+
}
3186+
return next;
3187+
}
3188+
31633189
SDL_Surface *TTF_GetGlyphImage(TTF_Font *font, Uint32 ch, TTF_ImageType *image_type)
31643190
{
31653191
FT_UInt idx;

src/SDL_ttf.exports

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,6 @@ _TTF_Version
118118
_TTF_WasInit
119119
_TTF_SetFontCharSpacing
120120
_TTF_GetFontCharSpacing
121+
_TTF_GetFirstFontChar
122+
_TTF_GetNextFontChar
121123
# extra symbols go here (don't modify this line)

src/SDL_ttf.sym

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ SDL3_ttf_0.0.0 {
119119
TTF_WasInit;
120120
TTF_SetFontCharSpacing;
121121
TTF_GetFontCharSpacing;
122+
TTF_GetFirstFontChar;
123+
TTF_GetNextFontChar;
122124
# extra symbols go here (don't modify this line)
123125
local: *;
124126
};

0 commit comments

Comments
 (0)