Skip to content

Implement TTF_SetFontCharSpacing()#578

Merged
slouken merged 4 commits into
libsdl-org:mainfrom
ivan-mogilko:feature--characterspacing
Aug 12, 2025
Merged

Implement TTF_SetFontCharSpacing()#578
slouken merged 4 commits into
libsdl-org:mainfrom
ivan-mogilko:feature--characterspacing

Conversation

@ivan-mogilko

@ivan-mogilko ivan-mogilko commented Aug 12, 2025

Copy link
Copy Markdown
Contributor

Resolves #561

This is an experimental proposal, which adds an optional uniform character spacing, applied after each drawn glyph in addition to the normal glyph's advance. Spacing may be both positive or negative. Setting this to 0 ensures that the font is drawn using only its kerning.

Two new functions declared as:

bool TTF_SetFontCharSpacing(TTF_Font *font, int char_spacing);
int TTF_GetFontCharSpacing(TTF_Font *font);

Character spacing value is in pixels on input, but stored as a 26.6 FP in the TTF_Font struct, for compatibility with GlyphPosition's fields. When calculating glyph positions, "char_spacing" is added to each pos's x_advance.

Additionally, added two new key controls to the "showfont" example:
Ctrl + , - decreases character spacing
Ctrl + . - increases character spacing

This lets configure an optional uniform character spacing, that is applied after each drawn glyph in addition to the normal glyph's advance.
Ctrl + , - decreases character spacing
Ctrl + . - increases character spacing
@ivan-mogilko ivan-mogilko force-pushed the feature--characterspacing branch from d61f630 to 94049fd Compare August 12, 2025 16:05
@1bsyl

1bsyl commented Aug 12, 2025

Copy link
Copy Markdown
Contributor

I think 1 glyph isn't one character in all languages.
This works for latin, but not for more complicated language arabic, etc.
also ligature chars ?

HarfBuzz should do the letter-spacing ..
maybe that's the current state: typst/typst#6161
edit: the link below isn't harbuzz
but this one.
harfbuzz/harfbuzz#358
I haven't read the whole thing

Comment thread include/SDL3_ttf/SDL_ttf.h Outdated
Comment thread include/SDL3_ttf/SDL_ttf.h Outdated
@slouken slouken merged commit 8edca18 into libsdl-org:main Aug 12, 2025
@slouken

slouken commented Aug 12, 2025

Copy link
Copy Markdown
Collaborator

Looks good, thanks!

@slouken

slouken commented Aug 12, 2025

Copy link
Copy Markdown
Collaborator

I think 1 glyph isn't one character in all languages. This works for latin, but not for more complicated language arabic, etc. also ligature chars ?

HarfBuzz should do the letter-spacing .. maybe that's the current state: typst/typst#6161

Oh, that's a really good point. @ivan-mogilko, can you take a look at this?

@ivan-mogilko

Copy link
Copy Markdown
Contributor Author

I think 1 glyph isn't one character in all languages. This works for latin, but not for more complicated language arabic, etc. also ligature chars ?
HarfBuzz should do the letter-spacing .. maybe that's the current state: typst/typst#6161

Oh, that's a really good point. @ivan-mogilko, can you take a look at this?

Right, I was wondering about ligatures too...
I will try to investigate this following the suggested link, but I cannot make promises, because I don't have much experience with these fonts.

@slouken , on a separate note, there was another mine PR (#538) opened few months ago made for SDL2 branch, but there was no update to it for a while. Should I also remake this on the main branch instead?

@ivan-mogilko ivan-mogilko deleted the feature--characterspacing branch August 12, 2025 17:20
@slouken

slouken commented Aug 12, 2025

Copy link
Copy Markdown
Collaborator

@slouken , on a separate note, there was another mine PR (#538) opened few months ago made for SDL2 branch, but there was no update to it for a while. Should I also remake this on the main branch instead?

We're not planning to add new functions to the SDL2 API at this point.

Separately from that, I was concerned that people might be confused about which function they should call to get measurement information, and I haven't sat down and thoroughly thought it through. Could you provide a use case for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Question] A custom Character Spacing

3 participants