Skip to content

Commit 0f63d3e

Browse files
committedJul 2, 2024
Internals: added FontScale storage.
1 parent dadf9cd commit 0f63d3e

File tree

5 files changed

+10
-4
lines changed

5 files changed

+10
-4
lines changed
 

‎imgui.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -3505,7 +3505,7 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con
35053505

35063506
const ImFont* font = draw_list->_Data->Font;
35073507
const float font_size = draw_list->_Data->FontSize;
3508-
const float font_scale = font_size / font->FontSize;
3508+
const float font_scale = draw_list->_Data->FontScale;
35093509
const char* text_end_ellipsis = NULL;
35103510
const float ellipsis_width = font->EllipsisWidth * font_scale;
35113511

@@ -3903,6 +3903,7 @@ static void SetCurrentWindow(ImGuiWindow* window)
39033903
if (window)
39043904
{
39053905
g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize();
3906+
g.FontScale = g.FontSize / g.Font->FontSize;
39063907
ImGui::NavUpdateCurrentWindowIsScrollPushableX();
39073908
}
39083909
}
@@ -7507,12 +7508,14 @@ void ImGui::SetCurrentFont(ImFont* font)
75077508
g.Font = font;
75087509
g.FontBaseSize = ImMax(1.0f, g.IO.FontGlobalScale * g.Font->FontSize * g.Font->Scale);
75097510
g.FontSize = g.CurrentWindow ? g.CurrentWindow->CalcFontSize() : 0.0f;
7511+
g.FontScale = g.FontSize / g.FontSize;
75107512

75117513
ImFontAtlas* atlas = g.Font->ContainerAtlas;
75127514
g.DrawListSharedData.TexUvWhitePixel = atlas->TexUvWhitePixel;
75137515
g.DrawListSharedData.TexUvLines = atlas->TexUvLines;
75147516
g.DrawListSharedData.Font = g.Font;
75157517
g.DrawListSharedData.FontSize = g.FontSize;
7518+
g.DrawListSharedData.FontScale = g.FontScale;
75167519
}
75177520

75187521
void ImGui::PushFont(ImFont* font)
@@ -8057,6 +8060,7 @@ void ImGui::SetWindowFontScale(float scale)
80578060
ImGuiWindow* window = GetCurrentWindow();
80588061
window->FontWindowScale = scale;
80598062
g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize();
8063+
g.FontScale = g.DrawListSharedData.FontScale = g.FontSize / g.Font->FontSize;
80608064
}
80618065

80628066
void ImGui::PushFocusScope(ImGuiID id)

‎imgui.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3203,7 +3203,7 @@ struct ImFont
32033203
float EllipsisCharStep; // 4 // out // Step between characters when EllipsisCount > 0
32043204
bool DirtyLookupTables; // 1 // out //
32053205
float Scale; // 4 // in // = 1.f // Base font scale, multiplied by the per-window font scale which you can adjust with SetWindowFontScale()
3206-
float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize]
3206+
float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] (unscaled)
32073207
int MetricsTotalSurface;// 4 // out // // Total surface in pixels to get an idea of the font rasterization/texture cost (not exact, we approximate the cost of padding between glyphs)
32083208
ImU8 Used4kPagesMap[(IM_UNICODE_CODEPOINT_MAX+1)/4096/8]; // 2 bytes if ImWchar=ImWchar16, 34 bytes if ImWchar==ImWchar32. Store 1-bit for each block of 4K codepoints that has one active glyph. This is mainly used to facilitate iterations across all used codepoints.
32093209

‎imgui_internal.h

+2
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ struct IMGUI_API ImDrawListSharedData
775775
ImVec2 TexUvWhitePixel; // UV of white pixel in the atlas
776776
ImFont* Font; // Current/default font (optional, for simplified AddText overload)
777777
float FontSize; // Current/default font size (optional, for simplified AddText overload)
778+
float FontScale; // Current/default font scale (== FontSize / Font->FontSize)
778779
float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo()
779780
float CircleSegmentMaxError; // Number of circle segments to use per pixel of radius for AddCircle() etc
780781
ImVec4 ClipRectFullscreen; // Value for PushClipRectFullscreen()
@@ -1927,6 +1928,7 @@ struct ImGuiContext
19271928
ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back()
19281929
float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize(). Text height for current window.
19291930
float FontBaseSize; // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Base text height.
1931+
float FontScale; // == FontSize / Font->FontSize
19301932
float CurrentDpiScale; // Current window/viewport DpiScale
19311933
ImDrawListSharedData DrawListSharedData;
19321934
double Time;

‎imgui_tables.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3269,7 +3269,7 @@ void ImGui::TableAngledHeadersRowEx(ImGuiID row_id, float angle, float max_label
32693269
ButtonBehavior(row_r, row_id, NULL, NULL);
32703270
KeepAliveID(row_id);
32713271

3272-
const float ascent_scaled = g.Font->Ascent * (g.FontSize / g.Font->FontSize); // FIXME: Standardize those scaling factors better
3272+
const float ascent_scaled = g.Font->Ascent * g.FontScale; // FIXME: Standardize those scaling factors better
32733273
const float line_off_for_ascent_x = (ImMax((g.FontSize - ascent_scaled) * 0.5f, 0.0f) / -sin_a) * (flip_label ? -1.0f : 1.0f);
32743274
const ImVec2 padding = g.Style.CellPadding; // We will always use swapped component
32753275
const ImVec2 align = g.Style.TableAngledHeadersTextAlign;

‎imgui_widgets.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3778,7 +3778,7 @@ namespace ImStb
37783778

37793779
static int STB_TEXTEDIT_STRINGLEN(const ImGuiInputTextState* obj) { return obj->CurLenW; }
37803780
static ImWchar STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { IM_ASSERT(idx <= obj->CurLenW); return obj->TextW[idx]; }
3781-
static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return IMSTB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.Font->GetCharAdvance(c) * (g.FontSize / g.Font->FontSize); }
3781+
static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return IMSTB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.Font->GetCharAdvance(c) * g.FontScale; }
37823782
static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x200000 ? 0 : key; }
37833783
static ImWchar STB_TEXTEDIT_NEWLINE = '\n';
37843784
static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* obj, int line_start_idx)

0 commit comments

Comments
 (0)
Please sign in to comment.