Skip to content

Commit 1742dd1

Browse files
committed
make font size be the em square's size when ImFontCfg::SizePixels is <= 0.0f
1 parent 172f00e commit 1742dd1

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

imgui_draw.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4573,7 +4573,7 @@ static bool ImGui_ImplStbTrueType_FontSrcInit(ImFontAtlas* atlas, ImFontConfig*
45734573
if (src->MergeMode && src->SizePixels == 0.0f)
45744574
src->SizePixels = ref_size;
45754575

4576-
if (src->SizePixels >= 0.0f)
4576+
if (src->SizePixels > 0.0f)
45774577
bd_font_data->ScaleFactor = stbtt_ScaleForPixelHeight(&bd_font_data->FontInfo, 1.0f);
45784578
else
45794579
bd_font_data->ScaleFactor = stbtt_ScaleForMappingEmToPixels(&bd_font_data->FontInfo, 1.0f);
@@ -4610,13 +4610,12 @@ static bool ImGui_ImplStbTrueType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig
46104610
if (src->MergeMode == false)
46114611
{
46124612
// FIXME-NEWFONTS: reevaluate how to use sizing metrics
4613-
// FIXME-NEWFONTS: make use of line gap value
46144613
float scale_for_layout = bd_font_data->ScaleFactor * baked->Size;
46154614
int unscaled_ascent, unscaled_descent, unscaled_line_gap;
46164615
stbtt_GetFontVMetrics(&bd_font_data->FontInfo, &unscaled_ascent, &unscaled_descent, &unscaled_line_gap);
46174616
baked->Ascent = ImCeil(unscaled_ascent * scale_for_layout);
46184617
baked->Descent = ImFloor(unscaled_descent * scale_for_layout);
4619-
baked->LineHeight = baked->Size;
4618+
baked->LineHeight = src->SizePixels > 0.0f ? baked->Size : baked->Ascent - baked->Descent + ImFloor(unscaled_line_gap * scale_for_layout);
46204619
}
46214620
return true;
46224621
}

misc/freetype/imgui_freetype.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ bool ImGui_ImplFreeType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig* src, ImF
442442
// (FT_Set_Pixel_Sizes() essentially calls FT_Request_Size() with FT_SIZE_REQUEST_TYPE_NOMINAL)
443443
const float rasterizer_density = src->RasterizerDensity * baked->RasterizerDensity;
444444
FT_Size_RequestRec req;
445-
req.type = (bd_font_data->UserFlags & ImGuiFreeTypeLoaderFlags_Bitmap) ? FT_SIZE_REQUEST_TYPE_NOMINAL : FT_SIZE_REQUEST_TYPE_REAL_DIM;
445+
req.type = src->SizePixels <= 0.0f ? FT_SIZE_REQUEST_TYPE_NOMINAL : FT_SIZE_REQUEST_TYPE_REAL_DIM;
446446
req.width = 0;
447447
req.height = (uint32_t)(size * 64 * rasterizer_density);
448448
req.horiResolution = 0;
@@ -455,10 +455,10 @@ bool ImGui_ImplFreeType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig* src, ImF
455455
// Read metrics
456456
FT_Size_Metrics metrics = bd_baked_data->FtSize->metrics;
457457
const float scale = 1.0f / rasterizer_density;
458-
baked->Ascent = (float)FT_CEIL(metrics.ascender) * scale; // The pixel extents above the baseline in pixels (typically positive).
459-
baked->Descent = (float)FT_CEIL(metrics.descender) * scale; // The extents below the baseline in pixels (typically negative).
460-
baked->LineHeight = baked->Size; // (float)FT_CEIL(metrics.height) * scale; // The baseline-to-baseline distance. Note that it usually is larger than the sum of the ascender and descender taken as absolute values. There is also no guarantee that no glyphs extend above or below subsequent baselines when using this distance. Think of it as a value the designer of the font finds appropriate.
461-
//LineGap = (float)FT_CEIL(metrics.height - metrics.ascender + metrics.descender) * scale; // The spacing in pixels between one row's descent and the next row's ascent.
458+
baked->Ascent = (float)FT_CEIL(metrics.ascender) * scale; // The pixel extents above the baseline in pixels (typically positive).
459+
baked->Descent = (float)(metrics.descender >> 6) * scale; // The extents below the baseline in pixels (typically negative).
460+
// FreeType rounds metrics.height when GRID_FIT_METRICS is defined which may undesirably be 1px smaller than Ascent-Descent
461+
baked->LineHeight = src->SizePixels > 0.0f ? baked->Size : (float)FT_CEIL(FT_MulFix(bd_font_data->FtFace->height, metrics.y_scale)) * scale; // The baseline-to-baseline distance. Note that it usually is larger than the sum of the ascender and descender taken as absolute values. There is also no guarantee that no glyphs extend above or below subsequent baselines when using this distance. Think of it as a value the designer of the font finds appropriate.
462462
//MaxAdvanceWidth = (float)FT_CEIL(metrics.max_advance) * scale; // This field gives the maximum horizontal cursor advance for all glyphs in the font.
463463
}
464464
return true;

0 commit comments

Comments
 (0)