Gave the option to choose between the Em Box font size metric and the ascent font size metric

This commit is contained in:
ObedH 2026-07-03 10:47:29 -04:00
commit b52626074c
2 changed files with 1021 additions and 990 deletions

View file

@ -913,6 +913,12 @@ typedef enum {
FONT_SDF // SDF font generation, requires external shader FONT_SDF // SDF font generation, requires external shader
} FontType; } FontType;
// Font scaling metric
typedef enum {
FONT_SIZE_METRIC_ASCENT = 0, // Measures font size in pixels from the baseline to the ascent line
FONT_SIZE_METRIC_EM_BOX // Measures font size using a ratio of the font's EM size (typically 2048 pixels)
} FontSizeMetric;
// Color blending modes (pre-defined) // Color blending modes (pre-defined)
typedef enum { typedef enum {
BLEND_ALPHA = 0, // Blend textures considering alpha (default) BLEND_ALPHA = 0, // Blend textures considering alpha (default)
@ -1540,6 +1546,9 @@ RLAPI int GetCodepointNext(const char *text, int *codepointSize);
RLAPI int GetCodepointPrevious(const char *text, int *codepointSize); // Get previous codepoint in a UTF-8 encoded string, 0x3f('?') is returned on failure RLAPI int GetCodepointPrevious(const char *text, int *codepointSize); // Get previous codepoint in a UTF-8 encoded string, 0x3f('?') is returned on failure
RLAPI const char *CodepointToUTF8(int codepoint, int *utf8Size); // Encode one codepoint into UTF-8 byte array (array length returned as parameter) RLAPI const char *CodepointToUTF8(int codepoint, int *utf8Size); // Encode one codepoint into UTF-8 byte array (array length returned as parameter)
// Font scaling metric options
RLAPI void SetFontSizeMetric(FontSizeMetric metric); // Sets the global font size metric
// Text strings management functions (no UTF-8 strings, only byte chars) // Text strings management functions (no UTF-8 strings, only byte chars)
// WARNING 1: Most of these functions use internal static buffers[], it's recommended to store returned data on user-side for re-use // WARNING 1: Most of these functions use internal static buffers[], it's recommended to store returned data on user-side for re-use
// WARNING 2: Some functions allocate memory internally for the returned strings, those strings must be freed by user using MemFree() // WARNING 2: Some functions allocate memory internally for the returned strings, those strings must be freed by user using MemFree()

View file

@ -130,6 +130,9 @@ static Font defaultFont = { 0 };
// Text vertical line spacing in pixels (between lines) // Text vertical line spacing in pixels (between lines)
static int textLineSpacing = 2; static int textLineSpacing = 2;
// The desired font scaling metric to use for calculating the font's ascent height
static FontSizeMetric fontSizeMetric = FONT_SIZE_METRIC_ASCENT;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Other Modules Functions Declaration (required by text) // Other Modules Functions Declaration (required by text)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -639,8 +642,18 @@ GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSiz
if (stbtt_InitFont(&fontInfo, (unsigned char *)fileData, 0)) // Initialize font for data reading if (stbtt_InitFont(&fontInfo, (unsigned char *)fileData, 0)) // Initialize font for data reading
{ {
// Calculate font scale factor // Calculate font scale factor using desired font size metric
float scaleFactor = stbtt_ScaleForPixelHeight(&fontInfo, (float)fontSize); float scaleFactor = 0.0f;
if(fontSizeMetric == FONT_SIZE_METRIC_ASCENT) {
scaleFactor = stbtt_ScaleForPixelHeight(&fontInfo, (float)fontSize);
}
else if(fontSizeMetric == FONT_SIZE_METRIC_EM_BOX) {
scaleFactor = stbtt_ScaleForMappingEmToPixels(&fontInfo, (float)fontSize);
}
else {
TRACELOG(LOG_WARNING, "FONT: Invalid font size metric [%d], assuming FONT_METRIC_ASCENT", fontSizeMetric);
scaleFactor = stbtt_ScaleForPixelHeight(&fontInfo, (float)fontSize);
}
// Calculate font basic metrics // Calculate font basic metrics
// NOTE: ascent is equivalent to font baseline // NOTE: ascent is equivalent to font baseline
@ -2407,6 +2420,15 @@ const char *CodepointToUTF8(int codepoint, int *utf8Size)
return utf8; return utf8;
} }
// Allows the user to choose which font size metric they want (either FONT_SIZE_METRIC_ASCENT or FONT_SIZE_METRIC_EM_BOX)
void SetFontSizeMetric(FontSizeMetric metric) {
if(metric != FONT_SIZE_METRIC_ASCENT && metric != FONT_SIZE_METRIC_EM_BOX) {
TRACELOG(LOG_WARNING, "TEXT: Attempting to set font size metric to invalid enum [%d]");
return;
}
fontSizeMetric = metric;
}
// Get next codepoint in a UTF-8 encoded text, scanning until '\0' is found // Get next codepoint in a UTF-8 encoded text, scanning until '\0' is found
// When an invalid UTF-8 byte is encountered, exit as soon as possible and a '?'(0x3f) codepoint is returned // When an invalid UTF-8 byte is encountered, exit as soon as possible and a '?'(0x3f) codepoint is returned
// Total number of bytes processed are returned as a parameter // Total number of bytes processed are returned as a parameter