Skip to content

Commit dee597a

Browse files
TriangulumDesireslouken
authored andcommitted
Add font weight functionality
(cherry picked from commit 78c290f)
1 parent 98ce7d5 commit dee597a

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

include/SDL3_ttf/SDL_ttf.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ extern SDL_DECLSPEC bool SDLCALL TTF_GetFontDPI(TTF_Font *font, int *hdpi, int *
445445
* SDL_ttf. A combination of these flags can be used with functions that set
446446
* or query font style, such as TTF_SetFontStyle or TTF_GetFontStyle.
447447
*
448-
* \since This function is available since SDL_ttf 3.0.0.
448+
* \since This datatype is available since SDL_ttf 3.0.0.
449449
*
450450
* \sa TTF_SetFontStyle
451451
* \sa TTF_GetFontStyle
@@ -667,6 +667,30 @@ extern SDL_DECLSPEC bool SDLCALL TTF_SetFontSDF(TTF_Font *font, bool enabled);
667667
*/
668668
extern SDL_DECLSPEC bool SDLCALL TTF_GetFontSDF(const TTF_Font *font);
669669

670+
/**
671+
* Query a font's weight, in terms of the lightness/heaviness of the strokes.
672+
*
673+
* \param font the font to query.
674+
* \returns the font's current weight
675+
*
676+
* \threadsafety This function should be called on the thread that created the
677+
* font.
678+
*
679+
* \since This function is available since SDL_ttf 3.4.0.
680+
*/
681+
extern SDL_DECLSPEC int SDLCALL TTF_GetFontWeight(const TTF_Font *font);
682+
683+
#define TTF_FONT_WEIGHT_THIN 100 /**< Thin (100) named font weight value */
684+
#define TTF_FONT_WEIGHT_EXTRA_LIGHT 200 /**< ExtraLight (200) named font weight value */
685+
#define TTF_FONT_WEIGHT_LIGHT 300 /**< Light (300) named font weight value */
686+
#define TTF_FONT_WEIGHT_NORMAL 400 /**< Normal (400) named font weight value */
687+
#define TTF_FONT_WEIGHT_MEDIUM 500 /**< Medium (500) named font weight value */
688+
#define TTF_FONT_WEIGHT_SEMI_BOLD 600 /**< SemiBold (600) named font weight value */
689+
#define TTF_FONT_WEIGHT_BOLD 700 /**< Bold (700) named font weight value */
690+
#define TTF_FONT_WEIGHT_EXTRA_BOLD 800 /**< ExtraBold (800) named font weight value */
691+
#define TTF_FONT_WEIGHT_BLACK 900 /**< Black (900) named font weight value */
692+
#define TTF_FONT_WEIGHT_EXTRA_BLACK 950 /**< ExtraBlack (950) named font weight value */
693+
670694
/**
671695
* The horizontal alignment used when rendering wrapped text.
672696
*

src/SDL_ttf.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include FT_STROKER_H
3232
#include FT_GLYPH_H
3333
#include FT_TRUETYPE_IDS_H
34+
#include FT_TRUETYPE_TABLES_H
3435
#include FT_IMAGE_H
3536

3637
/* Enable rendering with color
@@ -306,6 +307,7 @@ struct TTF_Font {
306307

307308
// The font style
308309
TTF_FontStyleFlags style;
310+
int weight;
309311
int outline;
310312
FT_Stroker stroker;
311313

@@ -2129,6 +2131,7 @@ TTF_Font *TTF_OpenFontWithProperties(SDL_PropertiesID props)
21292131
// Set the default font style
21302132
if (existing_font) {
21312133
font->style = existing_font->style;
2134+
font->weight = existing_font->weight;
21322135
font->outline = existing_font->outline;
21332136
font->ft_load_target = existing_font->ft_load_target;
21342137
font->enable_kerning = existing_font->enable_kerning;
@@ -2137,6 +2140,16 @@ TTF_Font *TTF_OpenFontWithProperties(SDL_PropertiesID props)
21372140
font->outline = 0;
21382141
font->ft_load_target = FT_LOAD_TARGET_NORMAL;
21392142
TTF_SetFontKerning(font, true);
2143+
2144+
// Retrieve the weight from the OS2 TrueType table
2145+
const TT_OS2 *os2_table = (const TT_OS2 *)FT_Get_Sfnt_Table(face, FT_SFNT_OS2);
2146+
if (os2_table != NULL && os2_table->usWeightClass != 0) {
2147+
font->weight = os2_table->usWeightClass;
2148+
} else if (face->style_flags & FT_STYLE_FLAG_BOLD) {
2149+
font->weight = TTF_FONT_WEIGHT_BOLD;
2150+
} else {
2151+
font->weight = TTF_FONT_WEIGHT_NORMAL;
2152+
}
21402153
}
21412154

21422155
#if TTF_USE_HARFBUZZ
@@ -5794,6 +5807,13 @@ bool TTF_GetFontSDF(const TTF_Font *font)
57945807
return font->render_sdf;
57955808
}
57965809

5810+
int TTF_GetFontWeight(const TTF_Font *font)
5811+
{
5812+
TTF_CHECK_FONT(font, -1);
5813+
5814+
return font->weight;
5815+
}
5816+
57975817
void TTF_SetFontWrapAlignment(TTF_Font *font, TTF_HorizontalAlignment align)
57985818
{
57995819
TTF_CHECK_FONT(font,);

src/SDL_ttf.sym

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ SDL3_ttf_0.0.0 {
3838
TTF_GetFontSize;
3939
TTF_GetFontStyle;
4040
TTF_GetFontStyleName;
41+
TTF_GetFontWeight;
4142
TTF_GetFontWrapAlignment;
4243
TTF_GetFreeTypeVersion;
4344
TTF_GetGlyphImage;

0 commit comments

Comments
 (0)