Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion include/SDL3_ttf/SDL_ttf.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ extern SDL_DECLSPEC bool SDLCALL TTF_GetFontDPI(TTF_Font *font, int *hdpi, int *
* SDL_ttf. A combination of these flags can be used with functions that set
* or query font style, such as TTF_SetFontStyle or TTF_GetFontStyle.
*
* \since This function is available since SDL_ttf 3.0.0.
* \since This datatype is available since SDL_ttf 3.0.0.
*
* \sa TTF_SetFontStyle
* \sa TTF_GetFontStyle
Expand Down Expand Up @@ -665,6 +665,30 @@ extern SDL_DECLSPEC bool SDLCALL TTF_SetFontSDF(TTF_Font *font, bool enabled);
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetFontSDF(const TTF_Font *font);

/**
* Query a font's weight, in terms of the lightness/heaviness of the strokes.
*
* \param font the font to query.
* \returns the font's current weight
*
* \threadsafety This function should be called on the thread that created the
* font.
*
* \since This function is available since SDL_ttf 3.4.0.
*/
extern SDL_DECLSPEC int SDLCALL TTF_GetFontWeight(const TTF_Font *font);

#define TTF_FONT_WEIGHT_THIN 100 /**< Thin (100) named font weight value */
#define TTF_FONT_WEIGHT_EXTRA_LIGHT 200 /**< ExtraLight (200) named font weight value */
#define TTF_FONT_WEIGHT_LIGHT 300 /**< Light (300) named font weight value */
#define TTF_FONT_WEIGHT_NORMAL 400 /**< Normal (400) named font weight value */
#define TTF_FONT_WEIGHT_MEDIUM 500 /**< Medium (500) named font weight value */
#define TTF_FONT_WEIGHT_SEMI_BOLD 600 /**< SemiBold (600) named font weight value */
#define TTF_FONT_WEIGHT_BOLD 700 /**< Bold (700) named font weight value */
#define TTF_FONT_WEIGHT_EXTRA_BOLD 800 /**< ExtraBold (800) named font weight value */
#define TTF_FONT_WEIGHT_BLACK 900 /**< Black (900) named font weight value */
#define TTF_FONT_WEIGHT_EXTRA_BLACK 950 /**< ExtraBlack (950) named font weight value */

/**
* The horizontal alignment used when rendering wrapped text.
*
Expand Down
20 changes: 20 additions & 0 deletions src/SDL_ttf.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include FT_STROKER_H
#include FT_GLYPH_H
#include FT_TRUETYPE_IDS_H
#include FT_TRUETYPE_TABLES_H
#include FT_IMAGE_H

/* Enable rendering with color
Expand Down Expand Up @@ -306,6 +307,7 @@ struct TTF_Font {

// The font style
TTF_FontStyleFlags style;
int weight;
int outline;
FT_Stroker stroker;

Expand Down Expand Up @@ -2129,6 +2131,7 @@ TTF_Font *TTF_OpenFontWithProperties(SDL_PropertiesID props)
// Set the default font style
if (existing_font) {
font->style = existing_font->style;
font->weight = existing_font->weight;
font->outline = existing_font->outline;
font->ft_load_target = existing_font->ft_load_target;
font->enable_kerning = existing_font->enable_kerning;
Expand All @@ -2137,6 +2140,16 @@ TTF_Font *TTF_OpenFontWithProperties(SDL_PropertiesID props)
font->outline = 0;
font->ft_load_target = FT_LOAD_TARGET_NORMAL;
TTF_SetFontKerning(font, true);

// Retrieve the weight from the OS2 TrueType table
const TT_OS2 *os2_table = (const TT_OS2 *)FT_Get_Sfnt_Table(face, FT_SFNT_OS2);
if (os2_table != NULL && os2_table->usWeightClass != 0) {
font->weight = os2_table->usWeightClass;
} else if (face->style_flags & FT_STYLE_FLAG_BOLD) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The font weight should probably be recalculated when font styles change.

Copy link
Contributor Author

@TriangulumDesire TriangulumDesire Mar 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that the weight will be affected by any style changes. Unless I'm missing something, the dependent values os2_table->usWeightClass and face->style_flags will remain constant, regardless of any changes to the font's style.

However, looking into this, I realised that SDL_ttf's custom TTF_STYLE_BOLD rendering will output glyphs whose weight is different than that of the font's. If we did want to reflect this boldness in the weight value, then I'd definitely have to (re)calculate an adjusted weight value when the font style changes for any fonts with the TTF_STYLE_BOLD flag. However, I don't think it is typographically quantifiable, since the custom boldness appears to render the glyphs twice with a horizontal offset, instead of changing the ‘weight’ of the rendered glyphs.

Personally, I don't think that the weight value should change. It moreso represents the static weight class of the underlying OpenType font data, rather than any dynamic weights. What are your thoughts?

font->weight = TTF_FONT_WEIGHT_BOLD;
} else {
font->weight = TTF_FONT_WEIGHT_NORMAL;
}
}

#if TTF_USE_HARFBUZZ
Expand Down Expand Up @@ -5794,6 +5807,13 @@ bool TTF_GetFontSDF(const TTF_Font *font)
return font->render_sdf;
}

int TTF_GetFontWeight(const TTF_Font *font)
{
TTF_CHECK_FONT(font, -1);

return font->weight;
}

void TTF_SetFontWrapAlignment(TTF_Font *font, TTF_HorizontalAlignment align)
{
TTF_CHECK_FONT(font,);
Expand Down
1 change: 1 addition & 0 deletions src/SDL_ttf.sym
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SDL3_ttf_0.0.0 {
TTF_GetFontSize;
TTF_GetFontStyle;
TTF_GetFontStyleName;
TTF_GetFontWeight;
TTF_GetFontWrapAlignment;
TTF_GetFreeTypeVersion;
TTF_GetGlyphImage;
Expand Down
Loading