1// dear imgui, v1.90.0
2// (drawing and font code)
3
4/*
5
6Index of this file:
7
8// [SECTION] STB libraries implementation
9// [SECTION] Style functions
10// [SECTION] ImDrawList
11// [SECTION] ImDrawListSplitter
12// [SECTION] ImDrawData
13// [SECTION] Helpers ShadeVertsXXX functions
14// [SECTION] ImFontConfig
15// [SECTION] ImFontAtlas
16// [SECTION] ImFontAtlas glyph ranges helpers
17// [SECTION] ImFontGlyphRangesBuilder
18// [SECTION] ImFont
19// [SECTION] ImGui Internal Render Helpers
20// [SECTION] Decompression code
21// [SECTION] Default font data (ProggyClean.ttf)
22
23*/
24
25#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
26#define _CRT_SECURE_NO_WARNINGS
27#endif
28
29#ifndef IMGUI_DEFINE_MATH_OPERATORS
30#define IMGUI_DEFINE_MATH_OPERATORS
31#endif
32
33#include "imgui.h"
34#ifndef IMGUI_DISABLE
35#include "imgui_internal.h"
36#ifdef IMGUI_ENABLE_FREETYPE
37#include "misc/freetype/imgui_freetype.h"
38#endif
39
40#include <stdio.h> // vsnprintf, sscanf, printf
41
42// Visual Studio warnings
43#ifdef _MSC_VER
44#pragma warning (disable: 4127) // condition expression is constant
45#pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
46#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
47#pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2).
48#pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). [MSVC Static Analyzer)
49#endif
50
51// Clang/GCC warnings with -Weverything
52#if defined(__clang__)
53#if __has_warning("-Wunknown-warning-option")
54#pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' // not all warnings are known by all Clang versions and they tend to be rename-happy.. so ignoring warnings triggers new warnings on some configuration. Great!
55#endif
56#pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx'
57#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
58#pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants ok.
59#pragma clang diagnostic ignored "-Wglobal-constructors" // warning: declaration requires a global destructor // similar to above, not sure what the exact difference is.
60#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
61#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0
62#pragma clang diagnostic ignored "-Wcomma" // warning: possible misuse of comma operator here
63#pragma clang diagnostic ignored "-Wreserved-id-macro" // warning: macro name is a reserved identifier
64#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double.
65#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
66#pragma clang diagnostic ignored "-Wreserved-identifier" // warning: identifier '_Xxx' is reserved because it starts with '_' followed by a capital letter
67#elif defined(__GNUC__)
68#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind
69#pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used
70#pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function
71#pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value
72#pragma GCC diagnostic ignored "-Wstack-protector" // warning: stack protector not protecting local variables: variable length buffer
73#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead
74#endif
75
76//-------------------------------------------------------------------------
77// [SECTION] STB libraries implementation (for stb_truetype and stb_rect_pack)
78//-------------------------------------------------------------------------
79
80// Compile time options:
81//#define IMGUI_STB_NAMESPACE ImStb
82//#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h"
83//#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h"
84//#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
85//#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
86
87#ifdef IMGUI_STB_NAMESPACE
88namespace IMGUI_STB_NAMESPACE
89{
90#endif
91
92#ifdef _MSC_VER
93#pragma warning (push)
94#pragma warning (disable: 4456) // declaration of 'xx' hides previous local declaration
95#pragma warning (disable: 6011) // (stb_rectpack) Dereferencing NULL pointer 'cur->next'.
96#pragma warning (disable: 6385) // (stb_truetype) Reading invalid data from 'buffer': the readable size is '_Old_3`kernel_width' bytes, but '3' bytes may be read.
97#pragma warning (disable: 28182) // (stb_rectpack) Dereferencing NULL pointer. 'cur' contains the same NULL value as 'cur->next' did.
98#endif
99
100#if defined(__clang__)
101#pragma clang diagnostic push
102#pragma clang diagnostic ignored "-Wunused-function"
103#pragma clang diagnostic ignored "-Wmissing-prototypes"
104#pragma clang diagnostic ignored "-Wimplicit-fallthrough"
105#pragma clang diagnostic ignored "-Wcast-qual" // warning: cast from 'const xxxx *' to 'xxx *' drops const qualifier
106#endif
107
108#if defined(__GNUC__)
109#pragma GCC diagnostic push
110#pragma GCC diagnostic ignored "-Wtype-limits" // warning: comparison is always true due to limited range of data type [-Wtype-limits]
111#pragma GCC diagnostic ignored "-Wcast-qual" // warning: cast from type 'const xxxx *' to type 'xxxx *' casts away qualifiers
112#endif
113
114#ifndef STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds)
115#ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in another compilation unit
116#define STBRP_STATIC
117#define STBRP_ASSERT(x) do { IM_ASSERT(x); } while (0)
118#define STBRP_SORT ImQsort
119#define STB_RECT_PACK_IMPLEMENTATION
120#endif
121#ifdef IMGUI_STB_RECT_PACK_FILENAME
122#include IMGUI_STB_RECT_PACK_FILENAME
123#else
124#include "imstb_rectpack.h"
125#endif
126#endif
127
128#ifdef IMGUI_ENABLE_STB_TRUETYPE
129#ifndef STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds)
130#ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in another compilation unit
131#define STBTT_malloc(x,u) ((void)(u), IM_ALLOC(x))
132#define STBTT_free(x,u) ((void)(u), IM_FREE(x))
133#define STBTT_assert(x) do { IM_ASSERT(x); } while(0)
134#define STBTT_fmod(x,y) ImFmod(x,y)
135#define STBTT_sqrt(x) ImSqrt(x)
136#define STBTT_pow(x,y) ImPow(x,y)
137#define STBTT_fabs(x) ImFabs(x)
138#define STBTT_ifloor(x) ((int)ImFloor(x))
139#define STBTT_iceil(x) ((int)ImCeil(x))
140#define STBTT_STATIC
141#define STB_TRUETYPE_IMPLEMENTATION
142#else
143#define STBTT_DEF extern
144#endif
145#ifdef IMGUI_STB_TRUETYPE_FILENAME
146#include IMGUI_STB_TRUETYPE_FILENAME
147#else
148#include "imstb_truetype.h"
149#endif
150#endif
151#endif // IMGUI_ENABLE_STB_TRUETYPE
152
153#if defined(__GNUC__)
154#pragma GCC diagnostic pop
155#endif
156
157#if defined(__clang__)
158#pragma clang diagnostic pop
159#endif
160
161#if defined(_MSC_VER)
162#pragma warning (pop)
163#endif
164
165#ifdef IMGUI_STB_NAMESPACE
166} // namespace ImStb
167using namespace IMGUI_STB_NAMESPACE;
168#endif
169
170//-----------------------------------------------------------------------------
171// [SECTION] Style functions
172//-----------------------------------------------------------------------------
173
174void ImGui::StyleColorsDark(ImGuiStyle* dst)
175{
176 ImGuiStyle* style = dst ? dst : &ImGui::GetStyle();
177 ImVec4* colors = style->Colors;
178
179 colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
180 colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
181 colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f);
182 colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
183 colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f);
184 colors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f);
185 colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
186 colors[ImGuiCol_FrameBg] = ImVec4(0.16f, 0.29f, 0.48f, 0.54f);
187 colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
188 colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
189 colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f);
190 colors[ImGuiCol_TitleBgActive] = ImVec4(0.16f, 0.29f, 0.48f, 1.00f);
191 colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f);
192 colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
193 colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f);
194 colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f);
195 colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f);
196 colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f);
197 colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
198 colors[ImGuiCol_SliderGrab] = ImVec4(0.24f, 0.52f, 0.88f, 1.00f);
199 colors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
200 colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
201 colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
202 colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f);
203 colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f);
204 colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f);
205 colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
206 colors[ImGuiCol_Separator] = colors[ImGuiCol_Border];
207 colors[ImGuiCol_SeparatorHovered] = ImVec4(0.10f, 0.40f, 0.75f, 0.78f);
208 colors[ImGuiCol_SeparatorActive] = ImVec4(0.10f, 0.40f, 0.75f, 1.00f);
209 colors[ImGuiCol_ResizeGrip] = ImVec4(0.26f, 0.59f, 0.98f, 0.20f);
210 colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
211 colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);
212 colors[ImGuiCol_Tab] = ImLerp(a: colors[ImGuiCol_Header], b: colors[ImGuiCol_TitleBgActive], t: 0.80f);
213 colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered];
214 colors[ImGuiCol_TabActive] = ImLerp(a: colors[ImGuiCol_HeaderActive], b: colors[ImGuiCol_TitleBgActive], t: 0.60f);
215 colors[ImGuiCol_TabUnfocused] = ImLerp(a: colors[ImGuiCol_Tab], b: colors[ImGuiCol_TitleBg], t: 0.80f);
216 colors[ImGuiCol_TabUnfocusedActive] = ImLerp(a: colors[ImGuiCol_TabActive], b: colors[ImGuiCol_TitleBg], t: 0.40f);
217 colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f);
218 colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);
219 colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
220 colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
221 colors[ImGuiCol_TableHeaderBg] = ImVec4(0.19f, 0.19f, 0.20f, 1.00f);
222 colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.35f, 1.00f); // Prefer using Alpha=1.0 here
223 colors[ImGuiCol_TableBorderLight] = ImVec4(0.23f, 0.23f, 0.25f, 1.00f); // Prefer using Alpha=1.0 here
224 colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
225 colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f);
226 colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);
227 colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f);
228 colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
229 colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);
230 colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);
231 colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f);
232}
233
234void ImGui::StyleColorsClassic(ImGuiStyle* dst)
235{
236 ImGuiStyle* style = dst ? dst : &ImGui::GetStyle();
237 ImVec4* colors = style->Colors;
238
239 colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
240 colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);
241 colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.85f);
242 colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
243 colors[ImGuiCol_PopupBg] = ImVec4(0.11f, 0.11f, 0.14f, 0.92f);
244 colors[ImGuiCol_Border] = ImVec4(0.50f, 0.50f, 0.50f, 0.50f);
245 colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
246 colors[ImGuiCol_FrameBg] = ImVec4(0.43f, 0.43f, 0.43f, 0.39f);
247 colors[ImGuiCol_FrameBgHovered] = ImVec4(0.47f, 0.47f, 0.69f, 0.40f);
248 colors[ImGuiCol_FrameBgActive] = ImVec4(0.42f, 0.41f, 0.64f, 0.69f);
249 colors[ImGuiCol_TitleBg] = ImVec4(0.27f, 0.27f, 0.54f, 0.83f);
250 colors[ImGuiCol_TitleBgActive] = ImVec4(0.32f, 0.32f, 0.63f, 0.87f);
251 colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.40f, 0.80f, 0.20f);
252 colors[ImGuiCol_MenuBarBg] = ImVec4(0.40f, 0.40f, 0.55f, 0.80f);
253 colors[ImGuiCol_ScrollbarBg] = ImVec4(0.20f, 0.25f, 0.30f, 0.60f);
254 colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.40f, 0.40f, 0.80f, 0.30f);
255 colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.80f, 0.40f);
256 colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.41f, 0.39f, 0.80f, 0.60f);
257 colors[ImGuiCol_CheckMark] = ImVec4(0.90f, 0.90f, 0.90f, 0.50f);
258 colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);
259 colors[ImGuiCol_SliderGrabActive] = ImVec4(0.41f, 0.39f, 0.80f, 0.60f);
260 colors[ImGuiCol_Button] = ImVec4(0.35f, 0.40f, 0.61f, 0.62f);
261 colors[ImGuiCol_ButtonHovered] = ImVec4(0.40f, 0.48f, 0.71f, 0.79f);
262 colors[ImGuiCol_ButtonActive] = ImVec4(0.46f, 0.54f, 0.80f, 1.00f);
263 colors[ImGuiCol_Header] = ImVec4(0.40f, 0.40f, 0.90f, 0.45f);
264 colors[ImGuiCol_HeaderHovered] = ImVec4(0.45f, 0.45f, 0.90f, 0.80f);
265 colors[ImGuiCol_HeaderActive] = ImVec4(0.53f, 0.53f, 0.87f, 0.80f);
266 colors[ImGuiCol_Separator] = ImVec4(0.50f, 0.50f, 0.50f, 0.60f);
267 colors[ImGuiCol_SeparatorHovered] = ImVec4(0.60f, 0.60f, 0.70f, 1.00f);
268 colors[ImGuiCol_SeparatorActive] = ImVec4(0.70f, 0.70f, 0.90f, 1.00f);
269 colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.10f);
270 colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.78f, 0.82f, 1.00f, 0.60f);
271 colors[ImGuiCol_ResizeGripActive] = ImVec4(0.78f, 0.82f, 1.00f, 0.90f);
272 colors[ImGuiCol_Tab] = ImLerp(a: colors[ImGuiCol_Header], b: colors[ImGuiCol_TitleBgActive], t: 0.80f);
273 colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered];
274 colors[ImGuiCol_TabActive] = ImLerp(a: colors[ImGuiCol_HeaderActive], b: colors[ImGuiCol_TitleBgActive], t: 0.60f);
275 colors[ImGuiCol_TabUnfocused] = ImLerp(a: colors[ImGuiCol_Tab], b: colors[ImGuiCol_TitleBg], t: 0.80f);
276 colors[ImGuiCol_TabUnfocusedActive] = ImLerp(a: colors[ImGuiCol_TabActive], b: colors[ImGuiCol_TitleBg], t: 0.40f);
277 colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
278 colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
279 colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
280 colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
281 colors[ImGuiCol_TableHeaderBg] = ImVec4(0.27f, 0.27f, 0.38f, 1.00f);
282 colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.45f, 1.00f); // Prefer using Alpha=1.0 here
283 colors[ImGuiCol_TableBorderLight] = ImVec4(0.26f, 0.26f, 0.28f, 1.00f); // Prefer using Alpha=1.0 here
284 colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
285 colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.07f);
286 colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f);
287 colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f);
288 colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered];
289 colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);
290 colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);
291 colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);
292}
293
294// Those light colors are better suited with a thicker font than the default one + FrameBorder
295void ImGui::StyleColorsLight(ImGuiStyle* dst)
296{
297 ImGuiStyle* style = dst ? dst : &ImGui::GetStyle();
298 ImVec4* colors = style->Colors;
299
300 colors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
301 colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);
302 colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f);
303 colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
304 colors[ImGuiCol_PopupBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.98f);
305 colors[ImGuiCol_Border] = ImVec4(0.00f, 0.00f, 0.00f, 0.30f);
306 colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
307 colors[ImGuiCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
308 colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
309 colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
310 colors[ImGuiCol_TitleBg] = ImVec4(0.96f, 0.96f, 0.96f, 1.00f);
311 colors[ImGuiCol_TitleBgActive] = ImVec4(0.82f, 0.82f, 0.82f, 1.00f);
312 colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 1.00f, 1.00f, 0.51f);
313 colors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f);
314 colors[ImGuiCol_ScrollbarBg] = ImVec4(0.98f, 0.98f, 0.98f, 0.53f);
315 colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.69f, 0.69f, 0.69f, 0.80f);
316 colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.49f, 0.49f, 0.49f, 0.80f);
317 colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.49f, 0.49f, 0.49f, 1.00f);
318 colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
319 colors[ImGuiCol_SliderGrab] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);
320 colors[ImGuiCol_SliderGrabActive] = ImVec4(0.46f, 0.54f, 0.80f, 0.60f);
321 colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
322 colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
323 colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f);
324 colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f);
325 colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f);
326 colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
327 colors[ImGuiCol_Separator] = ImVec4(0.39f, 0.39f, 0.39f, 0.62f);
328 colors[ImGuiCol_SeparatorHovered] = ImVec4(0.14f, 0.44f, 0.80f, 0.78f);
329 colors[ImGuiCol_SeparatorActive] = ImVec4(0.14f, 0.44f, 0.80f, 1.00f);
330 colors[ImGuiCol_ResizeGrip] = ImVec4(0.35f, 0.35f, 0.35f, 0.17f);
331 colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
332 colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);
333 colors[ImGuiCol_Tab] = ImLerp(a: colors[ImGuiCol_Header], b: colors[ImGuiCol_TitleBgActive], t: 0.90f);
334 colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered];
335 colors[ImGuiCol_TabActive] = ImLerp(a: colors[ImGuiCol_HeaderActive], b: colors[ImGuiCol_TitleBgActive], t: 0.60f);
336 colors[ImGuiCol_TabUnfocused] = ImLerp(a: colors[ImGuiCol_Tab], b: colors[ImGuiCol_TitleBg], t: 0.80f);
337 colors[ImGuiCol_TabUnfocusedActive] = ImLerp(a: colors[ImGuiCol_TabActive], b: colors[ImGuiCol_TitleBg], t: 0.40f);
338 colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);
339 colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);
340 colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
341 colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.45f, 0.00f, 1.00f);
342 colors[ImGuiCol_TableHeaderBg] = ImVec4(0.78f, 0.87f, 0.98f, 1.00f);
343 colors[ImGuiCol_TableBorderStrong] = ImVec4(0.57f, 0.57f, 0.64f, 1.00f); // Prefer using Alpha=1.0 here
344 colors[ImGuiCol_TableBorderLight] = ImVec4(0.68f, 0.68f, 0.74f, 1.00f); // Prefer using Alpha=1.0 here
345 colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
346 colors[ImGuiCol_TableRowBgAlt] = ImVec4(0.30f, 0.30f, 0.30f, 0.09f);
347 colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);
348 colors[ImGuiCol_DragDropTarget] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);
349 colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered];
350 colors[ImGuiCol_NavWindowingHighlight] = ImVec4(0.70f, 0.70f, 0.70f, 0.70f);
351 colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.20f);
352 colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);
353}
354
355//-----------------------------------------------------------------------------
356// [SECTION] ImDrawList
357//-----------------------------------------------------------------------------
358
359ImDrawListSharedData::ImDrawListSharedData()
360{
361 memset(s: this, c: 0, n: sizeof(*this));
362 for (int i = 0; i < IM_ARRAYSIZE(ArcFastVtx); i++)
363 {
364 const float a = ((float)i * 2 * IM_PI) / (float)IM_ARRAYSIZE(ArcFastVtx);
365 ArcFastVtx[i] = ImVec2(ImCos(a), ImSin(a));
366 }
367 ArcFastRadiusCutoff = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_R(IM_DRAWLIST_ARCFAST_SAMPLE_MAX, CircleSegmentMaxError);
368}
369
370void ImDrawListSharedData::SetCircleTessellationMaxError(float max_error)
371{
372 if (CircleSegmentMaxError == max_error)
373 return;
374
375 IM_ASSERT(max_error > 0.0f);
376 CircleSegmentMaxError = max_error;
377 for (int i = 0; i < IM_ARRAYSIZE(CircleSegmentCounts); i++)
378 {
379 const float radius = (float)i;
380 CircleSegmentCounts[i] = (ImU8)((i > 0) ? IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, CircleSegmentMaxError) : IM_DRAWLIST_ARCFAST_SAMPLE_MAX);
381 }
382 ArcFastRadiusCutoff = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_R(IM_DRAWLIST_ARCFAST_SAMPLE_MAX, CircleSegmentMaxError);
383}
384
385// Initialize before use in a new frame. We always have a command ready in the buffer.
386void ImDrawList::_ResetForNewFrame()
387{
388 // Verify that the ImDrawCmd fields we want to memcmp() are contiguous in memory.
389 IM_STATIC_ASSERT(offsetof(ImDrawCmd, ClipRect) == 0);
390 IM_STATIC_ASSERT(offsetof(ImDrawCmd, TextureId) == sizeof(ImVec4));
391 IM_STATIC_ASSERT(offsetof(ImDrawCmd, VtxOffset) == sizeof(ImVec4) + sizeof(ImTextureID));
392 if (_Splitter._Count > 1)
393 _Splitter.Merge(draw_list: this);
394
395 CmdBuffer.resize(new_size: 0);
396 IdxBuffer.resize(new_size: 0);
397 VtxBuffer.resize(new_size: 0);
398 Flags = _Data->InitialFlags;
399 memset(s: &_CmdHeader, c: 0, n: sizeof(_CmdHeader));
400 _VtxCurrentIdx = 0;
401 _VtxWritePtr = NULL;
402 _IdxWritePtr = NULL;
403 _ClipRectStack.resize(new_size: 0);
404 _TextureIdStack.resize(new_size: 0);
405 _Path.resize(new_size: 0);
406 _Splitter.Clear();
407 CmdBuffer.push_back(v: ImDrawCmd());
408 _FringeScale = 1.0f;
409}
410
411void ImDrawList::_ClearFreeMemory()
412{
413 CmdBuffer.clear();
414 IdxBuffer.clear();
415 VtxBuffer.clear();
416 Flags = ImDrawListFlags_None;
417 _VtxCurrentIdx = 0;
418 _VtxWritePtr = NULL;
419 _IdxWritePtr = NULL;
420 _ClipRectStack.clear();
421 _TextureIdStack.clear();
422 _Path.clear();
423 _Splitter.ClearFreeMemory();
424}
425
426ImDrawList* ImDrawList::CloneOutput() const
427{
428 ImDrawList* dst = IM_NEW(ImDrawList(_Data));
429 dst->CmdBuffer = CmdBuffer;
430 dst->IdxBuffer = IdxBuffer;
431 dst->VtxBuffer = VtxBuffer;
432 dst->Flags = Flags;
433 return dst;
434}
435
436void ImDrawList::AddDrawCmd()
437{
438 ImDrawCmd draw_cmd;
439 draw_cmd.ClipRect = _CmdHeader.ClipRect; // Same as calling ImDrawCmd_HeaderCopy()
440 draw_cmd.TextureId = _CmdHeader.TextureId;
441 draw_cmd.VtxOffset = _CmdHeader.VtxOffset;
442 draw_cmd.IdxOffset = IdxBuffer.Size;
443
444 IM_ASSERT(draw_cmd.ClipRect.x <= draw_cmd.ClipRect.z && draw_cmd.ClipRect.y <= draw_cmd.ClipRect.w);
445 CmdBuffer.push_back(v: draw_cmd);
446}
447
448// Pop trailing draw command (used before merging or presenting to user)
449// Note that this leaves the ImDrawList in a state unfit for further commands, as most code assume that CmdBuffer.Size > 0 && CmdBuffer.back().UserCallback == NULL
450void ImDrawList::_PopUnusedDrawCmd()
451{
452 while (CmdBuffer.Size > 0)
453 {
454 ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];
455 if (curr_cmd->ElemCount != 0 || curr_cmd->UserCallback != NULL)
456 return;// break;
457 CmdBuffer.pop_back();
458 }
459}
460
461void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data)
462{
463 IM_ASSERT_PARANOID(CmdBuffer.Size > 0);
464 ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];
465 IM_ASSERT(curr_cmd->UserCallback == NULL);
466 if (curr_cmd->ElemCount != 0)
467 {
468 AddDrawCmd();
469 curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];
470 }
471 curr_cmd->UserCallback = callback;
472 curr_cmd->UserCallbackData = callback_data;
473
474 AddDrawCmd(); // Force a new command after us (see comment below)
475}
476
477// Compare ClipRect, TextureId and VtxOffset with a single memcmp()
478#define ImDrawCmd_HeaderSize (offsetof(ImDrawCmd, VtxOffset) + sizeof(unsigned int))
479#define ImDrawCmd_HeaderCompare(CMD_LHS, CMD_RHS) (memcmp(CMD_LHS, CMD_RHS, ImDrawCmd_HeaderSize)) // Compare ClipRect, TextureId, VtxOffset
480#define ImDrawCmd_HeaderCopy(CMD_DST, CMD_SRC) (memcpy(CMD_DST, CMD_SRC, ImDrawCmd_HeaderSize)) // Copy ClipRect, TextureId, VtxOffset
481#define ImDrawCmd_AreSequentialIdxOffset(CMD_0, CMD_1) (CMD_0->IdxOffset + CMD_0->ElemCount == CMD_1->IdxOffset)
482
483// Try to merge two last draw commands
484void ImDrawList::_TryMergeDrawCmds()
485{
486 IM_ASSERT_PARANOID(CmdBuffer.Size > 0);
487 ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];
488 ImDrawCmd* prev_cmd = curr_cmd - 1;
489 if (ImDrawCmd_HeaderCompare(curr_cmd, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && curr_cmd->UserCallback == NULL && prev_cmd->UserCallback == NULL)
490 {
491 prev_cmd->ElemCount += curr_cmd->ElemCount;
492 CmdBuffer.pop_back();
493 }
494}
495
496// Our scheme may appears a bit unusual, basically we want the most-common calls AddLine AddRect etc. to not have to perform any check so we always have a command ready in the stack.
497// The cost of figuring out if a new command has to be added or if we can merge is paid in those Update** functions only.
498void ImDrawList::_OnChangedClipRect()
499{
500 // If current command is used with different settings we need to add a new command
501 IM_ASSERT_PARANOID(CmdBuffer.Size > 0);
502 ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];
503 if (curr_cmd->ElemCount != 0 && memcmp(s1: &curr_cmd->ClipRect, s2: &_CmdHeader.ClipRect, n: sizeof(ImVec4)) != 0)
504 {
505 AddDrawCmd();
506 return;
507 }
508 IM_ASSERT(curr_cmd->UserCallback == NULL);
509
510 // Try to merge with previous command if it matches, else use current command
511 ImDrawCmd* prev_cmd = curr_cmd - 1;
512 if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && prev_cmd->UserCallback == NULL)
513 {
514 CmdBuffer.pop_back();
515 return;
516 }
517
518 curr_cmd->ClipRect = _CmdHeader.ClipRect;
519}
520
521void ImDrawList::_OnChangedTextureID()
522{
523 // If current command is used with different settings we need to add a new command
524 IM_ASSERT_PARANOID(CmdBuffer.Size > 0);
525 ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];
526 if (curr_cmd->ElemCount != 0 && curr_cmd->TextureId != _CmdHeader.TextureId)
527 {
528 AddDrawCmd();
529 return;
530 }
531 IM_ASSERT(curr_cmd->UserCallback == NULL);
532
533 // Try to merge with previous command if it matches, else use current command
534 ImDrawCmd* prev_cmd = curr_cmd - 1;
535 if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && prev_cmd->UserCallback == NULL)
536 {
537 CmdBuffer.pop_back();
538 return;
539 }
540
541 curr_cmd->TextureId = _CmdHeader.TextureId;
542}
543
544void ImDrawList::_OnChangedVtxOffset()
545{
546 // We don't need to compare curr_cmd->VtxOffset != _CmdHeader.VtxOffset because we know it'll be different at the time we call this.
547 _VtxCurrentIdx = 0;
548 IM_ASSERT_PARANOID(CmdBuffer.Size > 0);
549 ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];
550 //IM_ASSERT(curr_cmd->VtxOffset != _CmdHeader.VtxOffset); // See #3349
551 if (curr_cmd->ElemCount != 0)
552 {
553 AddDrawCmd();
554 return;
555 }
556 IM_ASSERT(curr_cmd->UserCallback == NULL);
557 curr_cmd->VtxOffset = _CmdHeader.VtxOffset;
558}
559
560int ImDrawList::_CalcCircleAutoSegmentCount(float radius) const
561{
562 // Automatic segment count
563 const int radius_idx = (int)(radius + 0.999999f); // ceil to never reduce accuracy
564 if (radius_idx >= 0 && radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts))
565 return _Data->CircleSegmentCounts[radius_idx]; // Use cached value
566 else
567 return IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, _Data->CircleSegmentMaxError);
568}
569
570// Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling)
571void ImDrawList::PushClipRect(const ImVec2& cr_min, const ImVec2& cr_max, bool intersect_with_current_clip_rect)
572{
573 ImVec4 cr(cr_min.x, cr_min.y, cr_max.x, cr_max.y);
574 if (intersect_with_current_clip_rect)
575 {
576 ImVec4 current = _CmdHeader.ClipRect;
577 if (cr.x < current.x) cr.x = current.x;
578 if (cr.y < current.y) cr.y = current.y;
579 if (cr.z > current.z) cr.z = current.z;
580 if (cr.w > current.w) cr.w = current.w;
581 }
582 cr.z = ImMax(lhs: cr.x, rhs: cr.z);
583 cr.w = ImMax(lhs: cr.y, rhs: cr.w);
584
585 _ClipRectStack.push_back(v: cr);
586 _CmdHeader.ClipRect = cr;
587 _OnChangedClipRect();
588}
589
590void ImDrawList::PushClipRectFullScreen()
591{
592 PushClipRect(cr_min: ImVec2(_Data->ClipRectFullscreen.x, _Data->ClipRectFullscreen.y), cr_max: ImVec2(_Data->ClipRectFullscreen.z, _Data->ClipRectFullscreen.w));
593}
594
595void ImDrawList::PopClipRect()
596{
597 _ClipRectStack.pop_back();
598 _CmdHeader.ClipRect = (_ClipRectStack.Size == 0) ? _Data->ClipRectFullscreen : _ClipRectStack.Data[_ClipRectStack.Size - 1];
599 _OnChangedClipRect();
600}
601
602void ImDrawList::PushTextureID(ImTextureID texture_id)
603{
604 _TextureIdStack.push_back(v: texture_id);
605 _CmdHeader.TextureId = texture_id;
606 _OnChangedTextureID();
607}
608
609void ImDrawList::PopTextureID()
610{
611 _TextureIdStack.pop_back();
612 _CmdHeader.TextureId = (_TextureIdStack.Size == 0) ? (ImTextureID)NULL : _TextureIdStack.Data[_TextureIdStack.Size - 1];
613 _OnChangedTextureID();
614}
615
616// Reserve space for a number of vertices and indices.
617// You must finish filling your reserved data before calling PrimReserve() again, as it may reallocate or
618// submit the intermediate results. PrimUnreserve() can be used to release unused allocations.
619void ImDrawList::PrimReserve(int idx_count, int vtx_count)
620{
621 // Large mesh support (when enabled)
622 IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0);
623 if (sizeof(ImDrawIdx) == 2 && (_VtxCurrentIdx + vtx_count >= (1 << 16)) && (Flags & ImDrawListFlags_AllowVtxOffset))
624 {
625 // FIXME: In theory we should be testing that vtx_count <64k here.
626 // In practice, RenderText() relies on reserving ahead for a worst case scenario so it is currently useful for us
627 // to not make that check until we rework the text functions to handle clipping and large horizontal lines better.
628 _CmdHeader.VtxOffset = VtxBuffer.Size;
629 _OnChangedVtxOffset();
630 }
631
632 ImDrawCmd* draw_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];
633 draw_cmd->ElemCount += idx_count;
634
635 int vtx_buffer_old_size = VtxBuffer.Size;
636 VtxBuffer.resize(new_size: vtx_buffer_old_size + vtx_count);
637 _VtxWritePtr = VtxBuffer.Data + vtx_buffer_old_size;
638
639 int idx_buffer_old_size = IdxBuffer.Size;
640 IdxBuffer.resize(new_size: idx_buffer_old_size + idx_count);
641 _IdxWritePtr = IdxBuffer.Data + idx_buffer_old_size;
642}
643
644// Release the a number of reserved vertices/indices from the end of the last reservation made with PrimReserve().
645void ImDrawList::PrimUnreserve(int idx_count, int vtx_count)
646{
647 IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0);
648
649 ImDrawCmd* draw_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];
650 draw_cmd->ElemCount -= idx_count;
651 VtxBuffer.shrink(new_size: VtxBuffer.Size - vtx_count);
652 IdxBuffer.shrink(new_size: IdxBuffer.Size - idx_count);
653}
654
655// Fully unrolled with inline call to keep our debug builds decently fast.
656void ImDrawList::PrimRect(const ImVec2& a, const ImVec2& c, ImU32 col)
657{
658 ImVec2 b(c.x, a.y), d(a.x, c.y), uv(_Data->TexUvWhitePixel);
659 ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx;
660 _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2);
661 _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3);
662 _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col;
663 _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col;
664 _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col;
665 _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col;
666 _VtxWritePtr += 4;
667 _VtxCurrentIdx += 4;
668 _IdxWritePtr += 6;
669}
670
671void ImDrawList::PrimRectUV(const ImVec2& a, const ImVec2& c, const ImVec2& uv_a, const ImVec2& uv_c, ImU32 col)
672{
673 ImVec2 b(c.x, a.y), d(a.x, c.y), uv_b(uv_c.x, uv_a.y), uv_d(uv_a.x, uv_c.y);
674 ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx;
675 _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2);
676 _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3);
677 _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col;
678 _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col;
679 _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col;
680 _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col;
681 _VtxWritePtr += 4;
682 _VtxCurrentIdx += 4;
683 _IdxWritePtr += 6;
684}
685
686void ImDrawList::PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, const ImVec2& uv_a, const ImVec2& uv_b, const ImVec2& uv_c, const ImVec2& uv_d, ImU32 col)
687{
688 ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx;
689 _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2);
690 _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3);
691 _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col;
692 _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col;
693 _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col;
694 _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col;
695 _VtxWritePtr += 4;
696 _VtxCurrentIdx += 4;
697 _IdxWritePtr += 6;
698}
699
700// On AddPolyline() and AddConvexPolyFilled() we intentionally avoid using ImVec2 and superfluous function calls to optimize debug/non-inlined builds.
701// - Those macros expects l-values and need to be used as their own statement.
702// - Those macros are intentionally not surrounded by the 'do {} while (0)' idiom because even that translates to runtime with debug compilers.
703#define IM_NORMALIZE2F_OVER_ZERO(VX,VY) { float d2 = VX*VX + VY*VY; if (d2 > 0.0f) { float inv_len = ImRsqrt(d2); VX *= inv_len; VY *= inv_len; } } (void)0
704#define IM_FIXNORMAL2F_MAX_INVLEN2 100.0f // 500.0f (see #4053, #3366)
705#define IM_FIXNORMAL2F(VX,VY) { float d2 = VX*VX + VY*VY; if (d2 > 0.000001f) { float inv_len2 = 1.0f / d2; if (inv_len2 > IM_FIXNORMAL2F_MAX_INVLEN2) inv_len2 = IM_FIXNORMAL2F_MAX_INVLEN2; VX *= inv_len2; VY *= inv_len2; } } (void)0
706
707// TODO: Thickness anti-aliased lines cap are missing their AA fringe.
708// We avoid using the ImVec2 math operators here to reduce cost to a minimum for debug/non-inlined builds.
709void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 col, ImDrawFlags flags, float thickness)
710{
711 if (points_count < 2 || (col & IM_COL32_A_MASK) == 0)
712 return;
713
714 const bool closed = (flags & ImDrawFlags_Closed) != 0;
715 const ImVec2 opaque_uv = _Data->TexUvWhitePixel;
716 const int count = closed ? points_count : points_count - 1; // The number of line segments we need to draw
717 const bool thick_line = (thickness > _FringeScale);
718
719 if (Flags & ImDrawListFlags_AntiAliasedLines)
720 {
721 // Anti-aliased stroke
722 const float AA_SIZE = _FringeScale;
723 const ImU32 col_trans = col & ~IM_COL32_A_MASK;
724
725 // Thicknesses <1.0 should behave like thickness 1.0
726 thickness = ImMax(lhs: thickness, rhs: 1.0f);
727 const int integer_thickness = (int)thickness;
728 const float fractional_thickness = thickness - integer_thickness;
729
730 // Do we want to draw this line using a texture?
731 // - For now, only draw integer-width lines using textures to avoid issues with the way scaling occurs, could be improved.
732 // - If AA_SIZE is not 1.0f we cannot use the texture path.
733 const bool use_texture = (Flags & ImDrawListFlags_AntiAliasedLinesUseTex) && (integer_thickness < IM_DRAWLIST_TEX_LINES_WIDTH_MAX) && (fractional_thickness <= 0.00001f) && (AA_SIZE == 1.0f);
734
735 // We should never hit this, because NewFrame() doesn't set ImDrawListFlags_AntiAliasedLinesUseTex unless ImFontAtlasFlags_NoBakedLines is off
736 IM_ASSERT_PARANOID(!use_texture || !(_Data->Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoBakedLines));
737
738 const int idx_count = use_texture ? (count * 6) : (thick_line ? count * 18 : count * 12);
739 const int vtx_count = use_texture ? (points_count * 2) : (thick_line ? points_count * 4 : points_count * 3);
740 PrimReserve(idx_count, vtx_count);
741
742 // Temporary buffer
743 // The first <points_count> items are normals at each line point, then after that there are either 2 or 4 temp points for each line point
744 _Data->TempBuffer.reserve_discard(new_capacity: points_count * ((use_texture || !thick_line) ? 3 : 5));
745 ImVec2* temp_normals = _Data->TempBuffer.Data;
746 ImVec2* temp_points = temp_normals + points_count;
747
748 // Calculate normals (tangents) for each line segment
749 for (int i1 = 0; i1 < count; i1++)
750 {
751 const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1;
752 float dx = points[i2].x - points[i1].x;
753 float dy = points[i2].y - points[i1].y;
754 IM_NORMALIZE2F_OVER_ZERO(dx, dy);
755 temp_normals[i1].x = dy;
756 temp_normals[i1].y = -dx;
757 }
758 if (!closed)
759 temp_normals[points_count - 1] = temp_normals[points_count - 2];
760
761 // If we are drawing a one-pixel-wide line without a texture, or a textured line of any width, we only need 2 or 3 vertices per point
762 if (use_texture || !thick_line)
763 {
764 // [PATH 1] Texture-based lines (thick or non-thick)
765 // [PATH 2] Non texture-based lines (non-thick)
766
767 // The width of the geometry we need to draw - this is essentially <thickness> pixels for the line itself, plus "one pixel" for AA.
768 // - In the texture-based path, we don't use AA_SIZE here because the +1 is tied to the generated texture
769 // (see ImFontAtlasBuildRenderLinesTexData() function), and so alternate values won't work without changes to that code.
770 // - In the non texture-based paths, we would allow AA_SIZE to potentially be != 1.0f with a patch (e.g. fringe_scale patch to
771 // allow scaling geometry while preserving one-screen-pixel AA fringe).
772 const float half_draw_size = use_texture ? ((thickness * 0.5f) + 1) : AA_SIZE;
773
774 // If line is not closed, the first and last points need to be generated differently as there are no normals to blend
775 if (!closed)
776 {
777 temp_points[0] = points[0] + temp_normals[0] * half_draw_size;
778 temp_points[1] = points[0] - temp_normals[0] * half_draw_size;
779 temp_points[(points_count-1)*2+0] = points[points_count-1] + temp_normals[points_count-1] * half_draw_size;
780 temp_points[(points_count-1)*2+1] = points[points_count-1] - temp_normals[points_count-1] * half_draw_size;
781 }
782
783 // Generate the indices to form a number of triangles for each line segment, and the vertices for the line edges
784 // This takes points n and n+1 and writes into n+1, with the first point in a closed line being generated from the final one (as n+1 wraps)
785 // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer.
786 unsigned int idx1 = _VtxCurrentIdx; // Vertex index for start of line segment
787 for (int i1 = 0; i1 < count; i1++) // i1 is the first point of the line segment
788 {
789 const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1; // i2 is the second point of the line segment
790 const unsigned int idx2 = ((i1 + 1) == points_count) ? _VtxCurrentIdx : (idx1 + (use_texture ? 2 : 3)); // Vertex index for end of segment
791
792 // Average normals
793 float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f;
794 float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f;
795 IM_FIXNORMAL2F(dm_x, dm_y);
796 dm_x *= half_draw_size; // dm_x, dm_y are offset to the outer edge of the AA area
797 dm_y *= half_draw_size;
798
799 // Add temporary vertexes for the outer edges
800 ImVec2* out_vtx = &temp_points[i2 * 2];
801 out_vtx[0].x = points[i2].x + dm_x;
802 out_vtx[0].y = points[i2].y + dm_y;
803 out_vtx[1].x = points[i2].x - dm_x;
804 out_vtx[1].y = points[i2].y - dm_y;
805
806 if (use_texture)
807 {
808 // Add indices for two triangles
809 _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 1); // Right tri
810 _IdxWritePtr[3] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[4] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 0); // Left tri
811 _IdxWritePtr += 6;
812 }
813 else
814 {
815 // Add indexes for four triangles
816 _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 2); // Right tri 1
817 _IdxWritePtr[3] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[4] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 0); // Right tri 2
818 _IdxWritePtr[6] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[7] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[8] = (ImDrawIdx)(idx1 + 0); // Left tri 1
819 _IdxWritePtr[9] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[10] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[11] = (ImDrawIdx)(idx2 + 1); // Left tri 2
820 _IdxWritePtr += 12;
821 }
822
823 idx1 = idx2;
824 }
825
826 // Add vertexes for each point on the line
827 if (use_texture)
828 {
829 // If we're using textures we only need to emit the left/right edge vertices
830 ImVec4 tex_uvs = _Data->TexUvLines[integer_thickness];
831 /*if (fractional_thickness != 0.0f) // Currently always zero when use_texture==false!
832 {
833 const ImVec4 tex_uvs_1 = _Data->TexUvLines[integer_thickness + 1];
834 tex_uvs.x = tex_uvs.x + (tex_uvs_1.x - tex_uvs.x) * fractional_thickness; // inlined ImLerp()
835 tex_uvs.y = tex_uvs.y + (tex_uvs_1.y - tex_uvs.y) * fractional_thickness;
836 tex_uvs.z = tex_uvs.z + (tex_uvs_1.z - tex_uvs.z) * fractional_thickness;
837 tex_uvs.w = tex_uvs.w + (tex_uvs_1.w - tex_uvs.w) * fractional_thickness;
838 }*/
839 ImVec2 tex_uv0(tex_uvs.x, tex_uvs.y);
840 ImVec2 tex_uv1(tex_uvs.z, tex_uvs.w);
841 for (int i = 0; i < points_count; i++)
842 {
843 _VtxWritePtr[0].pos = temp_points[i * 2 + 0]; _VtxWritePtr[0].uv = tex_uv0; _VtxWritePtr[0].col = col; // Left-side outer edge
844 _VtxWritePtr[1].pos = temp_points[i * 2 + 1]; _VtxWritePtr[1].uv = tex_uv1; _VtxWritePtr[1].col = col; // Right-side outer edge
845 _VtxWritePtr += 2;
846 }
847 }
848 else
849 {
850 // If we're not using a texture, we need the center vertex as well
851 for (int i = 0; i < points_count; i++)
852 {
853 _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col; // Center of line
854 _VtxWritePtr[1].pos = temp_points[i * 2 + 0]; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col_trans; // Left-side outer edge
855 _VtxWritePtr[2].pos = temp_points[i * 2 + 1]; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col_trans; // Right-side outer edge
856 _VtxWritePtr += 3;
857 }
858 }
859 }
860 else
861 {
862 // [PATH 2] Non texture-based lines (thick): we need to draw the solid line core and thus require four vertices per point
863 const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
864
865 // If line is not closed, the first and last points need to be generated differently as there are no normals to blend
866 if (!closed)
867 {
868 const int points_last = points_count - 1;
869 temp_points[0] = points[0] + temp_normals[0] * (half_inner_thickness + AA_SIZE);
870 temp_points[1] = points[0] + temp_normals[0] * (half_inner_thickness);
871 temp_points[2] = points[0] - temp_normals[0] * (half_inner_thickness);
872 temp_points[3] = points[0] - temp_normals[0] * (half_inner_thickness + AA_SIZE);
873 temp_points[points_last * 4 + 0] = points[points_last] + temp_normals[points_last] * (half_inner_thickness + AA_SIZE);
874 temp_points[points_last * 4 + 1] = points[points_last] + temp_normals[points_last] * (half_inner_thickness);
875 temp_points[points_last * 4 + 2] = points[points_last] - temp_normals[points_last] * (half_inner_thickness);
876 temp_points[points_last * 4 + 3] = points[points_last] - temp_normals[points_last] * (half_inner_thickness + AA_SIZE);
877 }
878
879 // Generate the indices to form a number of triangles for each line segment, and the vertices for the line edges
880 // This takes points n and n+1 and writes into n+1, with the first point in a closed line being generated from the final one (as n+1 wraps)
881 // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer.
882 unsigned int idx1 = _VtxCurrentIdx; // Vertex index for start of line segment
883 for (int i1 = 0; i1 < count; i1++) // i1 is the first point of the line segment
884 {
885 const int i2 = (i1 + 1) == points_count ? 0 : (i1 + 1); // i2 is the second point of the line segment
886 const unsigned int idx2 = (i1 + 1) == points_count ? _VtxCurrentIdx : (idx1 + 4); // Vertex index for end of segment
887
888 // Average normals
889 float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f;
890 float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f;
891 IM_FIXNORMAL2F(dm_x, dm_y);
892 float dm_out_x = dm_x * (half_inner_thickness + AA_SIZE);
893 float dm_out_y = dm_y * (half_inner_thickness + AA_SIZE);
894 float dm_in_x = dm_x * half_inner_thickness;
895 float dm_in_y = dm_y * half_inner_thickness;
896
897 // Add temporary vertices
898 ImVec2* out_vtx = &temp_points[i2 * 4];
899 out_vtx[0].x = points[i2].x + dm_out_x;
900 out_vtx[0].y = points[i2].y + dm_out_y;
901 out_vtx[1].x = points[i2].x + dm_in_x;
902 out_vtx[1].y = points[i2].y + dm_in_y;
903 out_vtx[2].x = points[i2].x - dm_in_x;
904 out_vtx[2].y = points[i2].y - dm_in_y;
905 out_vtx[3].x = points[i2].x - dm_out_x;
906 out_vtx[3].y = points[i2].y - dm_out_y;
907
908 // Add indexes
909 _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 2);
910 _IdxWritePtr[3] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[4] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 1);
911 _IdxWritePtr[6] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[7] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[8] = (ImDrawIdx)(idx1 + 0);
912 _IdxWritePtr[9] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[10] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[11] = (ImDrawIdx)(idx2 + 1);
913 _IdxWritePtr[12] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[13] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[14] = (ImDrawIdx)(idx1 + 3);
914 _IdxWritePtr[15] = (ImDrawIdx)(idx1 + 3); _IdxWritePtr[16] = (ImDrawIdx)(idx2 + 3); _IdxWritePtr[17] = (ImDrawIdx)(idx2 + 2);
915 _IdxWritePtr += 18;
916
917 idx1 = idx2;
918 }
919
920 // Add vertices
921 for (int i = 0; i < points_count; i++)
922 {
923 _VtxWritePtr[0].pos = temp_points[i * 4 + 0]; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col_trans;
924 _VtxWritePtr[1].pos = temp_points[i * 4 + 1]; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col;
925 _VtxWritePtr[2].pos = temp_points[i * 4 + 2]; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col;
926 _VtxWritePtr[3].pos = temp_points[i * 4 + 3]; _VtxWritePtr[3].uv = opaque_uv; _VtxWritePtr[3].col = col_trans;
927 _VtxWritePtr += 4;
928 }
929 }
930 _VtxCurrentIdx += (ImDrawIdx)vtx_count;
931 }
932 else
933 {
934 // [PATH 4] Non texture-based, Non anti-aliased lines
935 const int idx_count = count * 6;
936 const int vtx_count = count * 4; // FIXME-OPT: Not sharing edges
937 PrimReserve(idx_count, vtx_count);
938
939 for (int i1 = 0; i1 < count; i1++)
940 {
941 const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1;
942 const ImVec2& p1 = points[i1];
943 const ImVec2& p2 = points[i2];
944
945 float dx = p2.x - p1.x;
946 float dy = p2.y - p1.y;
947 IM_NORMALIZE2F_OVER_ZERO(dx, dy);
948 dx *= (thickness * 0.5f);
949 dy *= (thickness * 0.5f);
950
951 _VtxWritePtr[0].pos.x = p1.x + dy; _VtxWritePtr[0].pos.y = p1.y - dx; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col;
952 _VtxWritePtr[1].pos.x = p2.x + dy; _VtxWritePtr[1].pos.y = p2.y - dx; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col;
953 _VtxWritePtr[2].pos.x = p2.x - dy; _VtxWritePtr[2].pos.y = p2.y + dx; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col;
954 _VtxWritePtr[3].pos.x = p1.x - dy; _VtxWritePtr[3].pos.y = p1.y + dx; _VtxWritePtr[3].uv = opaque_uv; _VtxWritePtr[3].col = col;
955 _VtxWritePtr += 4;
956
957 _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx + 1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx + 2);
958 _IdxWritePtr[3] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[4] = (ImDrawIdx)(_VtxCurrentIdx + 2); _IdxWritePtr[5] = (ImDrawIdx)(_VtxCurrentIdx + 3);
959 _IdxWritePtr += 6;
960 _VtxCurrentIdx += 4;
961 }
962 }
963}
964
965// - We intentionally avoid using ImVec2 and its math operators here to reduce cost to a minimum for debug/non-inlined builds.
966// - Filled shapes must always use clockwise winding order. The anti-aliasing fringe depends on it. Counter-clockwise shapes will have "inward" anti-aliasing.
967void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_count, ImU32 col)
968{
969 if (points_count < 3 || (col & IM_COL32_A_MASK) == 0)
970 return;
971
972 const ImVec2 uv = _Data->TexUvWhitePixel;
973
974 if (Flags & ImDrawListFlags_AntiAliasedFill)
975 {
976 // Anti-aliased Fill
977 const float AA_SIZE = _FringeScale;
978 const ImU32 col_trans = col & ~IM_COL32_A_MASK;
979 const int idx_count = (points_count - 2)*3 + points_count * 6;
980 const int vtx_count = (points_count * 2);
981 PrimReserve(idx_count, vtx_count);
982
983 // Add indexes for fill
984 unsigned int vtx_inner_idx = _VtxCurrentIdx;
985 unsigned int vtx_outer_idx = _VtxCurrentIdx + 1;
986 for (int i = 2; i < points_count; i++)
987 {
988 _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + ((i - 1) << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_inner_idx + (i << 1));
989 _IdxWritePtr += 3;
990 }
991
992 // Compute normals
993 _Data->TempBuffer.reserve_discard(new_capacity: points_count);
994 ImVec2* temp_normals = _Data->TempBuffer.Data;
995 for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++)
996 {
997 const ImVec2& p0 = points[i0];
998 const ImVec2& p1 = points[i1];
999 float dx = p1.x - p0.x;
1000 float dy = p1.y - p0.y;
1001 IM_NORMALIZE2F_OVER_ZERO(dx, dy);
1002 temp_normals[i0].x = dy;
1003 temp_normals[i0].y = -dx;
1004 }
1005
1006 for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++)
1007 {
1008 // Average normals
1009 const ImVec2& n0 = temp_normals[i0];
1010 const ImVec2& n1 = temp_normals[i1];
1011 float dm_x = (n0.x + n1.x) * 0.5f;
1012 float dm_y = (n0.y + n1.y) * 0.5f;
1013 IM_FIXNORMAL2F(dm_x, dm_y);
1014 dm_x *= AA_SIZE * 0.5f;
1015 dm_y *= AA_SIZE * 0.5f;
1016
1017 // Add vertices
1018 _VtxWritePtr[0].pos.x = (points[i1].x - dm_x); _VtxWritePtr[0].pos.y = (points[i1].y - dm_y); _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; // Inner
1019 _VtxWritePtr[1].pos.x = (points[i1].x + dm_x); _VtxWritePtr[1].pos.y = (points[i1].y + dm_y); _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col_trans; // Outer
1020 _VtxWritePtr += 2;
1021
1022 // Add indexes for fringes
1023 _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + (i0 << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1));
1024 _IdxWritePtr[3] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1)); _IdxWritePtr[4] = (ImDrawIdx)(vtx_outer_idx + (i1 << 1)); _IdxWritePtr[5] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1));
1025 _IdxWritePtr += 6;
1026 }
1027 _VtxCurrentIdx += (ImDrawIdx)vtx_count;
1028 }
1029 else
1030 {
1031 // Non Anti-aliased Fill
1032 const int idx_count = (points_count - 2)*3;
1033 const int vtx_count = points_count;
1034 PrimReserve(idx_count, vtx_count);
1035 for (int i = 0; i < vtx_count; i++)
1036 {
1037 _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col;
1038 _VtxWritePtr++;
1039 }
1040 for (int i = 2; i < points_count; i++)
1041 {
1042 _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx + i - 1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx + i);
1043 _IdxWritePtr += 3;
1044 }
1045 _VtxCurrentIdx += (ImDrawIdx)vtx_count;
1046 }
1047}
1048
1049void ImDrawList::_PathArcToFastEx(const ImVec2& center, float radius, int a_min_sample, int a_max_sample, int a_step)
1050{
1051 if (radius < 0.5f)
1052 {
1053 _Path.push_back(v: center);
1054 return;
1055 }
1056
1057 // Calculate arc auto segment step size
1058 if (a_step <= 0)
1059 a_step = IM_DRAWLIST_ARCFAST_SAMPLE_MAX / _CalcCircleAutoSegmentCount(radius);
1060
1061 // Make sure we never do steps larger than one quarter of the circle
1062 a_step = ImClamp(v: a_step, mn: 1, IM_DRAWLIST_ARCFAST_TABLE_SIZE / 4);
1063
1064 const int sample_range = ImAbs(x: a_max_sample - a_min_sample);
1065 const int a_next_step = a_step;
1066
1067 int samples = sample_range + 1;
1068 bool extra_max_sample = false;
1069 if (a_step > 1)
1070 {
1071 samples = sample_range / a_step + 1;
1072 const int overstep = sample_range % a_step;
1073
1074 if (overstep > 0)
1075 {
1076 extra_max_sample = true;
1077 samples++;
1078
1079 // When we have overstep to avoid awkwardly looking one long line and one tiny one at the end,
1080 // distribute first step range evenly between them by reducing first step size.
1081 if (sample_range > 0)
1082 a_step -= (a_step - overstep) / 2;
1083 }
1084 }
1085
1086 _Path.resize(new_size: _Path.Size + samples);
1087 ImVec2* out_ptr = _Path.Data + (_Path.Size - samples);
1088
1089 int sample_index = a_min_sample;
1090 if (sample_index < 0 || sample_index >= IM_DRAWLIST_ARCFAST_SAMPLE_MAX)
1091 {
1092 sample_index = sample_index % IM_DRAWLIST_ARCFAST_SAMPLE_MAX;
1093 if (sample_index < 0)
1094 sample_index += IM_DRAWLIST_ARCFAST_SAMPLE_MAX;
1095 }
1096
1097 if (a_max_sample >= a_min_sample)
1098 {
1099 for (int a = a_min_sample; a <= a_max_sample; a += a_step, sample_index += a_step, a_step = a_next_step)
1100 {
1101 // a_step is clamped to IM_DRAWLIST_ARCFAST_SAMPLE_MAX, so we have guaranteed that it will not wrap over range twice or more
1102 if (sample_index >= IM_DRAWLIST_ARCFAST_SAMPLE_MAX)
1103 sample_index -= IM_DRAWLIST_ARCFAST_SAMPLE_MAX;
1104
1105 const ImVec2 s = _Data->ArcFastVtx[sample_index];
1106 out_ptr->x = center.x + s.x * radius;
1107 out_ptr->y = center.y + s.y * radius;
1108 out_ptr++;
1109 }
1110 }
1111 else
1112 {
1113 for (int a = a_min_sample; a >= a_max_sample; a -= a_step, sample_index -= a_step, a_step = a_next_step)
1114 {
1115 // a_step is clamped to IM_DRAWLIST_ARCFAST_SAMPLE_MAX, so we have guaranteed that it will not wrap over range twice or more
1116 if (sample_index < 0)
1117 sample_index += IM_DRAWLIST_ARCFAST_SAMPLE_MAX;
1118
1119 const ImVec2 s = _Data->ArcFastVtx[sample_index];
1120 out_ptr->x = center.x + s.x * radius;
1121 out_ptr->y = center.y + s.y * radius;
1122 out_ptr++;
1123 }
1124 }
1125
1126 if (extra_max_sample)
1127 {
1128 int normalized_max_sample = a_max_sample % IM_DRAWLIST_ARCFAST_SAMPLE_MAX;
1129 if (normalized_max_sample < 0)
1130 normalized_max_sample += IM_DRAWLIST_ARCFAST_SAMPLE_MAX;
1131
1132 const ImVec2 s = _Data->ArcFastVtx[normalized_max_sample];
1133 out_ptr->x = center.x + s.x * radius;
1134 out_ptr->y = center.y + s.y * radius;
1135 out_ptr++;
1136 }
1137
1138 IM_ASSERT_PARANOID(_Path.Data + _Path.Size == out_ptr);
1139}
1140
1141void ImDrawList::_PathArcToN(const ImVec2& center, float radius, float a_min, float a_max, int num_segments)
1142{
1143 if (radius < 0.5f)
1144 {
1145 _Path.push_back(v: center);
1146 return;
1147 }
1148
1149 // Note that we are adding a point at both a_min and a_max.
1150 // If you are trying to draw a full closed circle you don't want the overlapping points!
1151 _Path.reserve(new_capacity: _Path.Size + (num_segments + 1));
1152 for (int i = 0; i <= num_segments; i++)
1153 {
1154 const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min);
1155 _Path.push_back(v: ImVec2(center.x + ImCos(a) * radius, center.y + ImSin(a) * radius));
1156 }
1157}
1158
1159// 0: East, 3: South, 6: West, 9: North, 12: East
1160void ImDrawList::PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12)
1161{
1162 if (radius < 0.5f)
1163 {
1164 _Path.push_back(v: center);
1165 return;
1166 }
1167 _PathArcToFastEx(center, radius, a_min_sample: a_min_of_12 * IM_DRAWLIST_ARCFAST_SAMPLE_MAX / 12, a_max_sample: a_max_of_12 * IM_DRAWLIST_ARCFAST_SAMPLE_MAX / 12, a_step: 0);
1168}
1169
1170void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments)
1171{
1172 if (radius < 0.5f)
1173 {
1174 _Path.push_back(v: center);
1175 return;
1176 }
1177
1178 if (num_segments > 0)
1179 {
1180 _PathArcToN(center, radius, a_min, a_max, num_segments);
1181 return;
1182 }
1183
1184 // Automatic segment count
1185 if (radius <= _Data->ArcFastRadiusCutoff)
1186 {
1187 const bool a_is_reverse = a_max < a_min;
1188
1189 // We are going to use precomputed values for mid samples.
1190 // Determine first and last sample in lookup table that belong to the arc.
1191 const float a_min_sample_f = IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_min / (IM_PI * 2.0f);
1192 const float a_max_sample_f = IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_max / (IM_PI * 2.0f);
1193
1194 const int a_min_sample = a_is_reverse ? (int)ImFloor(f: a_min_sample_f) : (int)ImCeil(a_min_sample_f);
1195 const int a_max_sample = a_is_reverse ? (int)ImCeil(a_max_sample_f) : (int)ImFloor(f: a_max_sample_f);
1196 const int a_mid_samples = a_is_reverse ? ImMax(lhs: a_min_sample - a_max_sample, rhs: 0) : ImMax(lhs: a_max_sample - a_min_sample, rhs: 0);
1197
1198 const float a_min_segment_angle = a_min_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX;
1199 const float a_max_segment_angle = a_max_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX;
1200 const bool a_emit_start = ImAbs(x: a_min_segment_angle - a_min) >= 1e-5f;
1201 const bool a_emit_end = ImAbs(x: a_max - a_max_segment_angle) >= 1e-5f;
1202
1203 _Path.reserve(new_capacity: _Path.Size + (a_mid_samples + 1 + (a_emit_start ? 1 : 0) + (a_emit_end ? 1 : 0)));
1204 if (a_emit_start)
1205 _Path.push_back(v: ImVec2(center.x + ImCos(a_min) * radius, center.y + ImSin(a_min) * radius));
1206 if (a_mid_samples > 0)
1207 _PathArcToFastEx(center, radius, a_min_sample, a_max_sample, a_step: 0);
1208 if (a_emit_end)
1209 _Path.push_back(v: ImVec2(center.x + ImCos(a_max) * radius, center.y + ImSin(a_max) * radius));
1210 }
1211 else
1212 {
1213 const float arc_length = ImAbs(x: a_max - a_min);
1214 const int circle_segment_count = _CalcCircleAutoSegmentCount(radius);
1215 const int arc_segment_count = ImMax(lhs: (int)ImCeil(circle_segment_count * arc_length / (IM_PI * 2.0f)), rhs: (int)(2.0f * IM_PI / arc_length));
1216 _PathArcToN(center, radius, a_min, a_max, num_segments: arc_segment_count);
1217 }
1218}
1219
1220void ImDrawList::PathEllipticalArcTo(const ImVec2& center, float radius_x, float radius_y, float rot, float a_min, float a_max, int num_segments)
1221{
1222 if (num_segments <= 0)
1223 num_segments = _CalcCircleAutoSegmentCount(radius: ImMax(lhs: radius_x, rhs: radius_y)); // A bit pessimistic, maybe there's a better computation to do here.
1224
1225 _Path.reserve(new_capacity: _Path.Size + (num_segments + 1));
1226
1227 const float cos_rot = ImCos(rot);
1228 const float sin_rot = ImSin(rot);
1229 for (int i = 0; i <= num_segments; i++)
1230 {
1231 const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min);
1232 ImVec2 point(ImCos(a) * radius_x, ImSin(a) * radius_y);
1233 const float rel_x = (point.x * cos_rot) - (point.y * sin_rot);
1234 const float rel_y = (point.x * sin_rot) + (point.y * cos_rot);
1235 point.x = rel_x + center.x;
1236 point.y = rel_y + center.y;
1237 _Path.push_back(v: point);
1238 }
1239}
1240
1241ImVec2 ImBezierCubicCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t)
1242{
1243 float u = 1.0f - t;
1244 float w1 = u * u * u;
1245 float w2 = 3 * u * u * t;
1246 float w3 = 3 * u * t * t;
1247 float w4 = t * t * t;
1248 return ImVec2(w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x, w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y);
1249}
1250
1251ImVec2 ImBezierQuadraticCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, float t)
1252{
1253 float u = 1.0f - t;
1254 float w1 = u * u;
1255 float w2 = 2 * u * t;
1256 float w3 = t * t;
1257 return ImVec2(w1 * p1.x + w2 * p2.x + w3 * p3.x, w1 * p1.y + w2 * p2.y + w3 * p3.y);
1258}
1259
1260// Closely mimics ImBezierCubicClosestPointCasteljau() in imgui.cpp
1261static void PathBezierCubicCurveToCasteljau(ImVector<ImVec2>* path, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level)
1262{
1263 float dx = x4 - x1;
1264 float dy = y4 - y1;
1265 float d2 = (x2 - x4) * dy - (y2 - y4) * dx;
1266 float d3 = (x3 - x4) * dy - (y3 - y4) * dx;
1267 d2 = (d2 >= 0) ? d2 : -d2;
1268 d3 = (d3 >= 0) ? d3 : -d3;
1269 if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy))
1270 {
1271 path->push_back(v: ImVec2(x4, y4));
1272 }
1273 else if (level < 10)
1274 {
1275 float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f;
1276 float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f;
1277 float x34 = (x3 + x4) * 0.5f, y34 = (y3 + y4) * 0.5f;
1278 float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f;
1279 float x234 = (x23 + x34) * 0.5f, y234 = (y23 + y34) * 0.5f;
1280 float x1234 = (x123 + x234) * 0.5f, y1234 = (y123 + y234) * 0.5f;
1281 PathBezierCubicCurveToCasteljau(path, x1, y1, x2: x12, y2: y12, x3: x123, y3: y123, x4: x1234, y4: y1234, tess_tol, level: level + 1);
1282 PathBezierCubicCurveToCasteljau(path, x1: x1234, y1: y1234, x2: x234, y2: y234, x3: x34, y3: y34, x4, y4, tess_tol, level: level + 1);
1283 }
1284}
1285
1286static void PathBezierQuadraticCurveToCasteljau(ImVector<ImVec2>* path, float x1, float y1, float x2, float y2, float x3, float y3, float tess_tol, int level)
1287{
1288 float dx = x3 - x1, dy = y3 - y1;
1289 float det = (x2 - x3) * dy - (y2 - y3) * dx;
1290 if (det * det * 4.0f < tess_tol * (dx * dx + dy * dy))
1291 {
1292 path->push_back(v: ImVec2(x3, y3));
1293 }
1294 else if (level < 10)
1295 {
1296 float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f;
1297 float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f;
1298 float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f;
1299 PathBezierQuadraticCurveToCasteljau(path, x1, y1, x2: x12, y2: y12, x3: x123, y3: y123, tess_tol, level: level + 1);
1300 PathBezierQuadraticCurveToCasteljau(path, x1: x123, y1: y123, x2: x23, y2: y23, x3, y3, tess_tol, level: level + 1);
1301 }
1302}
1303
1304void ImDrawList::PathBezierCubicCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments)
1305{
1306 ImVec2 p1 = _Path.back();
1307 if (num_segments == 0)
1308 {
1309 IM_ASSERT(_Data->CurveTessellationTol > 0.0f);
1310 PathBezierCubicCurveToCasteljau(path: &_Path, x1: p1.x, y1: p1.y, x2: p2.x, y2: p2.y, x3: p3.x, y3: p3.y, x4: p4.x, y4: p4.y, tess_tol: _Data->CurveTessellationTol, level: 0); // Auto-tessellated
1311 }
1312 else
1313 {
1314 float t_step = 1.0f / (float)num_segments;
1315 for (int i_step = 1; i_step <= num_segments; i_step++)
1316 _Path.push_back(v: ImBezierCubicCalc(p1, p2, p3, p4, t: t_step * i_step));
1317 }
1318}
1319
1320void ImDrawList::PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, int num_segments)
1321{
1322 ImVec2 p1 = _Path.back();
1323 if (num_segments == 0)
1324 {
1325 IM_ASSERT(_Data->CurveTessellationTol > 0.0f);
1326 PathBezierQuadraticCurveToCasteljau(path: &_Path, x1: p1.x, y1: p1.y, x2: p2.x, y2: p2.y, x3: p3.x, y3: p3.y, tess_tol: _Data->CurveTessellationTol, level: 0);// Auto-tessellated
1327 }
1328 else
1329 {
1330 float t_step = 1.0f / (float)num_segments;
1331 for (int i_step = 1; i_step <= num_segments; i_step++)
1332 _Path.push_back(v: ImBezierQuadraticCalc(p1, p2, p3, t: t_step * i_step));
1333 }
1334}
1335
1336static inline ImDrawFlags FixRectCornerFlags(ImDrawFlags flags)
1337{
1338 /*
1339 IM_STATIC_ASSERT(ImDrawFlags_RoundCornersTopLeft == (1 << 4));
1340#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
1341 // Obsoleted in 1.82 (from February 2021). This code was stripped/simplified and mostly commented in 1.90 (from September 2023)
1342 // - Legacy Support for hard coded ~0 (used to be a suggested equivalent to ImDrawCornerFlags_All)
1343 if (flags == ~0) { return ImDrawFlags_RoundCornersAll; }
1344 // - Legacy Support for hard coded 0x01 to 0x0F (matching 15 out of 16 old flags combinations). Read details in older version of this code.
1345 if (flags >= 0x01 && flags <= 0x0F) { return (flags << 4); }
1346 // We cannot support hard coded 0x00 with 'float rounding > 0.0f' --> replace with ImDrawFlags_RoundCornersNone or use 'float rounding = 0.0f'
1347#endif
1348 */
1349 // If this assert triggers, please update your code replacing hardcoded values with new ImDrawFlags_RoundCorners* values.
1350 // Note that ImDrawFlags_Closed (== 0x01) is an invalid flag for AddRect(), AddRectFilled(), PathRect() etc. anyway.
1351 // See details in 1.82 Changelog as well as 2021/03/12 and 2023/09/08 entries in "API BREAKING CHANGES" section.
1352 IM_ASSERT((flags & 0x0F) == 0 && "Misuse of legacy hardcoded ImDrawCornerFlags values!");
1353
1354 if ((flags & ImDrawFlags_RoundCornersMask_) == 0)
1355 flags |= ImDrawFlags_RoundCornersAll;
1356
1357 return flags;
1358}
1359
1360void ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, ImDrawFlags flags)
1361{
1362 if (rounding >= 0.5f)
1363 {
1364 flags = FixRectCornerFlags(flags);
1365 rounding = ImMin(lhs: rounding, ImFabs(b.x - a.x) * (((flags & ImDrawFlags_RoundCornersTop) == ImDrawFlags_RoundCornersTop) || ((flags & ImDrawFlags_RoundCornersBottom) == ImDrawFlags_RoundCornersBottom) ? 0.5f : 1.0f) - 1.0f);
1366 rounding = ImMin(lhs: rounding, ImFabs(b.y - a.y) * (((flags & ImDrawFlags_RoundCornersLeft) == ImDrawFlags_RoundCornersLeft) || ((flags & ImDrawFlags_RoundCornersRight) == ImDrawFlags_RoundCornersRight) ? 0.5f : 1.0f) - 1.0f);
1367 }
1368 if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone)
1369 {
1370 PathLineTo(pos: a);
1371 PathLineTo(pos: ImVec2(b.x, a.y));
1372 PathLineTo(pos: b);
1373 PathLineTo(pos: ImVec2(a.x, b.y));
1374 }
1375 else
1376 {
1377 const float rounding_tl = (flags & ImDrawFlags_RoundCornersTopLeft) ? rounding : 0.0f;
1378 const float rounding_tr = (flags & ImDrawFlags_RoundCornersTopRight) ? rounding : 0.0f;
1379 const float rounding_br = (flags & ImDrawFlags_RoundCornersBottomRight) ? rounding : 0.0f;
1380 const float rounding_bl = (flags & ImDrawFlags_RoundCornersBottomLeft) ? rounding : 0.0f;
1381 PathArcToFast(center: ImVec2(a.x + rounding_tl, a.y + rounding_tl), radius: rounding_tl, a_min_of_12: 6, a_max_of_12: 9);
1382 PathArcToFast(center: ImVec2(b.x - rounding_tr, a.y + rounding_tr), radius: rounding_tr, a_min_of_12: 9, a_max_of_12: 12);
1383 PathArcToFast(center: ImVec2(b.x - rounding_br, b.y - rounding_br), radius: rounding_br, a_min_of_12: 0, a_max_of_12: 3);
1384 PathArcToFast(center: ImVec2(a.x + rounding_bl, b.y - rounding_bl), radius: rounding_bl, a_min_of_12: 3, a_max_of_12: 6);
1385 }
1386}
1387
1388void ImDrawList::AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float thickness)
1389{
1390 if ((col & IM_COL32_A_MASK) == 0)
1391 return;
1392 PathLineTo(pos: p1 + ImVec2(0.5f, 0.5f));
1393 PathLineTo(pos: p2 + ImVec2(0.5f, 0.5f));
1394 PathStroke(col, flags: 0, thickness);
1395}
1396
1397// p_min = upper-left, p_max = lower-right
1398// Note we don't render 1 pixels sized rectangles properly.
1399void ImDrawList::AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawFlags flags, float thickness)
1400{
1401 if ((col & IM_COL32_A_MASK) == 0)
1402 return;
1403 if (Flags & ImDrawListFlags_AntiAliasedLines)
1404 PathRect(a: p_min + ImVec2(0.50f, 0.50f), b: p_max - ImVec2(0.50f, 0.50f), rounding, flags);
1405 else
1406 PathRect(a: p_min + ImVec2(0.50f, 0.50f), b: p_max - ImVec2(0.49f, 0.49f), rounding, flags); // Better looking lower-right corner and rounded non-AA shapes.
1407 PathStroke(col, flags: ImDrawFlags_Closed, thickness);
1408}
1409
1410void ImDrawList::AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawFlags flags)
1411{
1412 if ((col & IM_COL32_A_MASK) == 0)
1413 return;
1414 if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone)
1415 {
1416 PrimReserve(idx_count: 6, vtx_count: 4);
1417 PrimRect(a: p_min, c: p_max, col);
1418 }
1419 else
1420 {
1421 PathRect(a: p_min, b: p_max, rounding, flags);
1422 PathFillConvex(col);
1423 }
1424}
1425
1426// p_min = upper-left, p_max = lower-right
1427void ImDrawList::AddRectFilledMultiColor(const ImVec2& p_min, const ImVec2& p_max, ImU32 col_upr_left, ImU32 col_upr_right, ImU32 col_bot_right, ImU32 col_bot_left)
1428{
1429 if (((col_upr_left | col_upr_right | col_bot_right | col_bot_left) & IM_COL32_A_MASK) == 0)
1430 return;
1431
1432 const ImVec2 uv = _Data->TexUvWhitePixel;
1433 PrimReserve(idx_count: 6, vtx_count: 4);
1434 PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx + 1)); PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx + 2));
1435 PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx + 2)); PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx + 3));
1436 PrimWriteVtx(pos: p_min, uv, col: col_upr_left);
1437 PrimWriteVtx(pos: ImVec2(p_max.x, p_min.y), uv, col: col_upr_right);
1438 PrimWriteVtx(pos: p_max, uv, col: col_bot_right);
1439 PrimWriteVtx(pos: ImVec2(p_min.x, p_max.y), uv, col: col_bot_left);
1440}
1441
1442void ImDrawList::AddQuad(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness)
1443{
1444 if ((col & IM_COL32_A_MASK) == 0)
1445 return;
1446
1447 PathLineTo(pos: p1);
1448 PathLineTo(pos: p2);
1449 PathLineTo(pos: p3);
1450 PathLineTo(pos: p4);
1451 PathStroke(col, flags: ImDrawFlags_Closed, thickness);
1452}
1453
1454void ImDrawList::AddQuadFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col)
1455{
1456 if ((col & IM_COL32_A_MASK) == 0)
1457 return;
1458
1459 PathLineTo(pos: p1);
1460 PathLineTo(pos: p2);
1461 PathLineTo(pos: p3);
1462 PathLineTo(pos: p4);
1463 PathFillConvex(col);
1464}
1465
1466void ImDrawList::AddTriangle(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness)
1467{
1468 if ((col & IM_COL32_A_MASK) == 0)
1469 return;
1470
1471 PathLineTo(pos: p1);
1472 PathLineTo(pos: p2);
1473 PathLineTo(pos: p3);
1474 PathStroke(col, flags: ImDrawFlags_Closed, thickness);
1475}
1476
1477void ImDrawList::AddTriangleFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col)
1478{
1479 if ((col & IM_COL32_A_MASK) == 0)
1480 return;
1481
1482 PathLineTo(pos: p1);
1483 PathLineTo(pos: p2);
1484 PathLineTo(pos: p3);
1485 PathFillConvex(col);
1486}
1487
1488void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness)
1489{
1490 if ((col & IM_COL32_A_MASK) == 0 || radius < 0.5f)
1491 return;
1492
1493 if (num_segments <= 0)
1494 {
1495 // Use arc with automatic segment count
1496 _PathArcToFastEx(center, radius: radius - 0.5f, a_min_sample: 0, IM_DRAWLIST_ARCFAST_SAMPLE_MAX, a_step: 0);
1497 _Path.Size--;
1498 }
1499 else
1500 {
1501 // Explicit segment count (still clamp to avoid drawing insanely tessellated shapes)
1502 num_segments = ImClamp(v: num_segments, mn: 3, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX);
1503
1504 // Because we are filling a closed shape we remove 1 from the count of segments/points
1505 const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments;
1506 PathArcTo(center, radius: radius - 0.5f, a_min: 0.0f, a_max, num_segments: num_segments - 1);
1507 }
1508
1509 PathStroke(col, flags: ImDrawFlags_Closed, thickness);
1510}
1511
1512void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments)
1513{
1514 if ((col & IM_COL32_A_MASK) == 0 || radius < 0.5f)
1515 return;
1516
1517 if (num_segments <= 0)
1518 {
1519 // Use arc with automatic segment count
1520 _PathArcToFastEx(center, radius, a_min_sample: 0, IM_DRAWLIST_ARCFAST_SAMPLE_MAX, a_step: 0);
1521 _Path.Size--;
1522 }
1523 else
1524 {
1525 // Explicit segment count (still clamp to avoid drawing insanely tessellated shapes)
1526 num_segments = ImClamp(v: num_segments, mn: 3, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX);
1527
1528 // Because we are filling a closed shape we remove 1 from the count of segments/points
1529 const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments;
1530 PathArcTo(center, radius, a_min: 0.0f, a_max, num_segments: num_segments - 1);
1531 }
1532
1533 PathFillConvex(col);
1534}
1535
1536// Guaranteed to honor 'num_segments'
1537void ImDrawList::AddNgon(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness)
1538{
1539 if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2)
1540 return;
1541
1542 // Because we are filling a closed shape we remove 1 from the count of segments/points
1543 const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments;
1544 PathArcTo(center, radius: radius - 0.5f, a_min: 0.0f, a_max, num_segments: num_segments - 1);
1545 PathStroke(col, flags: ImDrawFlags_Closed, thickness);
1546}
1547
1548// Guaranteed to honor 'num_segments'
1549void ImDrawList::AddNgonFilled(const ImVec2& center, float radius, ImU32 col, int num_segments)
1550{
1551 if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2)
1552 return;
1553
1554 // Because we are filling a closed shape we remove 1 from the count of segments/points
1555 const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments;
1556 PathArcTo(center, radius, a_min: 0.0f, a_max, num_segments: num_segments - 1);
1557 PathFillConvex(col);
1558}
1559
1560// Ellipse
1561void ImDrawList::AddEllipse(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot, int num_segments, float thickness)
1562{
1563 if ((col & IM_COL32_A_MASK) == 0)
1564 return;
1565
1566 if (num_segments <= 0)
1567 num_segments = _CalcCircleAutoSegmentCount(radius: ImMax(lhs: radius_x, rhs: radius_y)); // A bit pessimistic, maybe there's a better computation to do here.
1568
1569 // Because we are filling a closed shape we remove 1 from the count of segments/points
1570 const float a_max = IM_PI * 2.0f * ((float)num_segments - 1.0f) / (float)num_segments;
1571 PathEllipticalArcTo(center, radius_x, radius_y, rot, a_min: 0.0f, a_max, num_segments: num_segments - 1);
1572 PathStroke(col, flags: true, thickness);
1573}
1574
1575void ImDrawList::AddEllipseFilled(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot, int num_segments)
1576{
1577 if ((col & IM_COL32_A_MASK) == 0)
1578 return;
1579
1580 if (num_segments <= 0)
1581 num_segments = _CalcCircleAutoSegmentCount(radius: ImMax(lhs: radius_x, rhs: radius_y)); // A bit pessimistic, maybe there's a better computation to do here.
1582
1583 // Because we are filling a closed shape we remove 1 from the count of segments/points
1584 const float a_max = IM_PI * 2.0f * ((float)num_segments - 1.0f) / (float)num_segments;
1585 PathEllipticalArcTo(center, radius_x, radius_y, rot, a_min: 0.0f, a_max, num_segments: num_segments - 1);
1586 PathFillConvex(col);
1587}
1588
1589// Cubic Bezier takes 4 controls points
1590void ImDrawList::AddBezierCubic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments)
1591{
1592 if ((col & IM_COL32_A_MASK) == 0)
1593 return;
1594
1595 PathLineTo(pos: p1);
1596 PathBezierCubicCurveTo(p2, p3, p4, num_segments);
1597 PathStroke(col, flags: 0, thickness);
1598}
1599
1600// Quadratic Bezier takes 3 controls points
1601void ImDrawList::AddBezierQuadratic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness, int num_segments)
1602{
1603 if ((col & IM_COL32_A_MASK) == 0)
1604 return;
1605
1606 PathLineTo(pos: p1);
1607 PathBezierQuadraticCurveTo(p2, p3, num_segments);
1608 PathStroke(col, flags: 0, thickness);
1609}
1610
1611void ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end, float wrap_width, const ImVec4* cpu_fine_clip_rect)
1612{
1613 if ((col & IM_COL32_A_MASK) == 0)
1614 return;
1615
1616 if (text_end == NULL)
1617 text_end = text_begin + strlen(s: text_begin);
1618 if (text_begin == text_end)
1619 return;
1620
1621 // Pull default font/size from the shared ImDrawListSharedData instance
1622 if (font == NULL)
1623 font = _Data->Font;
1624 if (font_size == 0.0f)
1625 font_size = _Data->FontSize;
1626
1627 IM_ASSERT(font->ContainerAtlas->TexID == _CmdHeader.TextureId); // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font.
1628
1629 ImVec4 clip_rect = _CmdHeader.ClipRect;
1630 if (cpu_fine_clip_rect)
1631 {
1632 clip_rect.x = ImMax(lhs: clip_rect.x, rhs: cpu_fine_clip_rect->x);
1633 clip_rect.y = ImMax(lhs: clip_rect.y, rhs: cpu_fine_clip_rect->y);
1634 clip_rect.z = ImMin(lhs: clip_rect.z, rhs: cpu_fine_clip_rect->z);
1635 clip_rect.w = ImMin(lhs: clip_rect.w, rhs: cpu_fine_clip_rect->w);
1636 }
1637 font->RenderText(draw_list: this, size: font_size, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip: cpu_fine_clip_rect != NULL);
1638}
1639
1640void ImDrawList::AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end)
1641{
1642 AddText(NULL, font_size: 0.0f, pos, col, text_begin, text_end);
1643}
1644
1645void ImDrawList::AddImage(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col)
1646{
1647 if ((col & IM_COL32_A_MASK) == 0)
1648 return;
1649
1650 const bool push_texture_id = user_texture_id != _CmdHeader.TextureId;
1651 if (push_texture_id)
1652 PushTextureID(texture_id: user_texture_id);
1653
1654 PrimReserve(idx_count: 6, vtx_count: 4);
1655 PrimRectUV(a: p_min, c: p_max, uv_a: uv_min, uv_c: uv_max, col);
1656
1657 if (push_texture_id)
1658 PopTextureID();
1659}
1660
1661void ImDrawList::AddImageQuad(ImTextureID user_texture_id, const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& uv1, const ImVec2& uv2, const ImVec2& uv3, const ImVec2& uv4, ImU32 col)
1662{
1663 if ((col & IM_COL32_A_MASK) == 0)
1664 return;
1665
1666 const bool push_texture_id = user_texture_id != _CmdHeader.TextureId;
1667 if (push_texture_id)
1668 PushTextureID(texture_id: user_texture_id);
1669
1670 PrimReserve(idx_count: 6, vtx_count: 4);
1671 PrimQuadUV(a: p1, b: p2, c: p3, d: p4, uv_a: uv1, uv_b: uv2, uv_c: uv3, uv_d: uv4, col);
1672
1673 if (push_texture_id)
1674 PopTextureID();
1675}
1676
1677void ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawFlags flags)
1678{
1679 if ((col & IM_COL32_A_MASK) == 0)
1680 return;
1681
1682 flags = FixRectCornerFlags(flags);
1683 if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone)
1684 {
1685 AddImage(user_texture_id, p_min, p_max, uv_min, uv_max, col);
1686 return;
1687 }
1688
1689 const bool push_texture_id = user_texture_id != _CmdHeader.TextureId;
1690 if (push_texture_id)
1691 PushTextureID(texture_id: user_texture_id);
1692
1693 int vert_start_idx = VtxBuffer.Size;
1694 PathRect(a: p_min, b: p_max, rounding, flags);
1695 PathFillConvex(col);
1696 int vert_end_idx = VtxBuffer.Size;
1697 ImGui::ShadeVertsLinearUV(draw_list: this, vert_start_idx, vert_end_idx, a: p_min, b: p_max, uv_a: uv_min, uv_b: uv_max, clamp: true);
1698
1699 if (push_texture_id)
1700 PopTextureID();
1701}
1702
1703
1704//-----------------------------------------------------------------------------
1705// [SECTION] ImDrawListSplitter
1706//-----------------------------------------------------------------------------
1707// FIXME: This may be a little confusing, trying to be a little too low-level/optimal instead of just doing vector swap..
1708//-----------------------------------------------------------------------------
1709
1710void ImDrawListSplitter::ClearFreeMemory()
1711{
1712 for (int i = 0; i < _Channels.Size; i++)
1713 {
1714 if (i == _Current)
1715 memset(s: &_Channels[i], c: 0, n: sizeof(_Channels[i])); // Current channel is a copy of CmdBuffer/IdxBuffer, don't destruct again
1716 _Channels[i]._CmdBuffer.clear();
1717 _Channels[i]._IdxBuffer.clear();
1718 }
1719 _Current = 0;
1720 _Count = 1;
1721 _Channels.clear();
1722}
1723
1724void ImDrawListSplitter::Split(ImDrawList* draw_list, int channels_count)
1725{
1726 IM_UNUSED(draw_list);
1727 IM_ASSERT(_Current == 0 && _Count <= 1 && "Nested channel splitting is not supported. Please use separate instances of ImDrawListSplitter.");
1728 int old_channels_count = _Channels.Size;
1729 if (old_channels_count < channels_count)
1730 {
1731 _Channels.reserve(new_capacity: channels_count); // Avoid over reserving since this is likely to stay stable
1732 _Channels.resize(new_size: channels_count);
1733 }
1734 _Count = channels_count;
1735
1736 // Channels[] (24/32 bytes each) hold storage that we'll swap with draw_list->_CmdBuffer/_IdxBuffer
1737 // The content of Channels[0] at this point doesn't matter. We clear it to make state tidy in a debugger but we don't strictly need to.
1738 // When we switch to the next channel, we'll copy draw_list->_CmdBuffer/_IdxBuffer into Channels[0] and then Channels[1] into draw_list->CmdBuffer/_IdxBuffer
1739 memset(s: &_Channels[0], c: 0, n: sizeof(ImDrawChannel));
1740 for (int i = 1; i < channels_count; i++)
1741 {
1742 if (i >= old_channels_count)
1743 {
1744 IM_PLACEMENT_NEW(&_Channels[i]) ImDrawChannel();
1745 }
1746 else
1747 {
1748 _Channels[i]._CmdBuffer.resize(new_size: 0);
1749 _Channels[i]._IdxBuffer.resize(new_size: 0);
1750 }
1751 }
1752}
1753
1754void ImDrawListSplitter::Merge(ImDrawList* draw_list)
1755{
1756 // Note that we never use or rely on _Channels.Size because it is merely a buffer that we never shrink back to 0 to keep all sub-buffers ready for use.
1757 if (_Count <= 1)
1758 return;
1759
1760 SetCurrentChannel(draw_list, channel_idx: 0);
1761 draw_list->_PopUnusedDrawCmd();
1762
1763 // Calculate our final buffer sizes. Also fix the incorrect IdxOffset values in each command.
1764 int new_cmd_buffer_count = 0;
1765 int new_idx_buffer_count = 0;
1766 ImDrawCmd* last_cmd = (_Count > 0 && draw_list->CmdBuffer.Size > 0) ? &draw_list->CmdBuffer.back() : NULL;
1767 int idx_offset = last_cmd ? last_cmd->IdxOffset + last_cmd->ElemCount : 0;
1768 for (int i = 1; i < _Count; i++)
1769 {
1770 ImDrawChannel& ch = _Channels[i];
1771 if (ch._CmdBuffer.Size > 0 && ch._CmdBuffer.back().ElemCount == 0 && ch._CmdBuffer.back().UserCallback == NULL) // Equivalent of PopUnusedDrawCmd()
1772 ch._CmdBuffer.pop_back();
1773
1774 if (ch._CmdBuffer.Size > 0 && last_cmd != NULL)
1775 {
1776 // Do not include ImDrawCmd_AreSequentialIdxOffset() in the compare as we rebuild IdxOffset values ourselves.
1777 // Manipulating IdxOffset (e.g. by reordering draw commands like done by RenderDimmedBackgroundBehindWindow()) is not supported within a splitter.
1778 ImDrawCmd* next_cmd = &ch._CmdBuffer[0];
1779 if (ImDrawCmd_HeaderCompare(last_cmd, next_cmd) == 0 && last_cmd->UserCallback == NULL && next_cmd->UserCallback == NULL)
1780 {
1781 // Merge previous channel last draw command with current channel first draw command if matching.
1782 last_cmd->ElemCount += next_cmd->ElemCount;
1783 idx_offset += next_cmd->ElemCount;
1784 ch._CmdBuffer.erase(it: ch._CmdBuffer.Data); // FIXME-OPT: Improve for multiple merges.
1785 }
1786 }
1787 if (ch._CmdBuffer.Size > 0)
1788 last_cmd = &ch._CmdBuffer.back();
1789 new_cmd_buffer_count += ch._CmdBuffer.Size;
1790 new_idx_buffer_count += ch._IdxBuffer.Size;
1791 for (int cmd_n = 0; cmd_n < ch._CmdBuffer.Size; cmd_n++)
1792 {
1793 ch._CmdBuffer.Data[cmd_n].IdxOffset = idx_offset;
1794 idx_offset += ch._CmdBuffer.Data[cmd_n].ElemCount;
1795 }
1796 }
1797 draw_list->CmdBuffer.resize(new_size: draw_list->CmdBuffer.Size + new_cmd_buffer_count);
1798 draw_list->IdxBuffer.resize(new_size: draw_list->IdxBuffer.Size + new_idx_buffer_count);
1799
1800 // Write commands and indices in order (they are fairly small structures, we don't copy vertices only indices)
1801 ImDrawCmd* cmd_write = draw_list->CmdBuffer.Data + draw_list->CmdBuffer.Size - new_cmd_buffer_count;
1802 ImDrawIdx* idx_write = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size - new_idx_buffer_count;
1803 for (int i = 1; i < _Count; i++)
1804 {
1805 ImDrawChannel& ch = _Channels[i];
1806 if (int sz = ch._CmdBuffer.Size) { memcpy(dest: cmd_write, src: ch._CmdBuffer.Data, n: sz * sizeof(ImDrawCmd)); cmd_write += sz; }
1807 if (int sz = ch._IdxBuffer.Size) { memcpy(dest: idx_write, src: ch._IdxBuffer.Data, n: sz * sizeof(ImDrawIdx)); idx_write += sz; }
1808 }
1809 draw_list->_IdxWritePtr = idx_write;
1810
1811 // Ensure there's always a non-callback draw command trailing the command-buffer
1812 if (draw_list->CmdBuffer.Size == 0 || draw_list->CmdBuffer.back().UserCallback != NULL)
1813 draw_list->AddDrawCmd();
1814
1815 // If current command is used with different settings we need to add a new command
1816 ImDrawCmd* curr_cmd = &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1];
1817 if (curr_cmd->ElemCount == 0)
1818 ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset
1819 else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0)
1820 draw_list->AddDrawCmd();
1821
1822 _Count = 1;
1823}
1824
1825void ImDrawListSplitter::SetCurrentChannel(ImDrawList* draw_list, int idx)
1826{
1827 IM_ASSERT(idx >= 0 && idx < _Count);
1828 if (_Current == idx)
1829 return;
1830
1831 // Overwrite ImVector (12/16 bytes), four times. This is merely a silly optimization instead of doing .swap()
1832 memcpy(dest: &_Channels.Data[_Current]._CmdBuffer, src: &draw_list->CmdBuffer, n: sizeof(draw_list->CmdBuffer));
1833 memcpy(dest: &_Channels.Data[_Current]._IdxBuffer, src: &draw_list->IdxBuffer, n: sizeof(draw_list->IdxBuffer));
1834 _Current = idx;
1835 memcpy(dest: &draw_list->CmdBuffer, src: &_Channels.Data[idx]._CmdBuffer, n: sizeof(draw_list->CmdBuffer));
1836 memcpy(dest: &draw_list->IdxBuffer, src: &_Channels.Data[idx]._IdxBuffer, n: sizeof(draw_list->IdxBuffer));
1837 draw_list->_IdxWritePtr = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size;
1838
1839 // If current command is used with different settings we need to add a new command
1840 ImDrawCmd* curr_cmd = (draw_list->CmdBuffer.Size == 0) ? NULL : &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1];
1841 if (curr_cmd == NULL)
1842 draw_list->AddDrawCmd();
1843 else if (curr_cmd->ElemCount == 0)
1844 ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset
1845 else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0)
1846 draw_list->AddDrawCmd();
1847}
1848
1849//-----------------------------------------------------------------------------
1850// [SECTION] ImDrawData
1851//-----------------------------------------------------------------------------
1852
1853void ImDrawData::Clear()
1854{
1855 Valid = false;
1856 CmdListsCount = TotalIdxCount = TotalVtxCount = 0;
1857 CmdLists.resize(new_size: 0); // The ImDrawList are NOT owned by ImDrawData but e.g. by ImGuiContext, so we don't clear them.
1858 DisplayPos = DisplaySize = FramebufferScale = ImVec2(0.0f, 0.0f);
1859 OwnerViewport = NULL;
1860}
1861
1862// Important: 'out_list' is generally going to be draw_data->CmdLists, but may be another temporary list
1863// as long at it is expected that the result will be later merged into draw_data->CmdLists[].
1864void ImGui::AddDrawListToDrawDataEx(ImDrawData* draw_data, ImVector<ImDrawList*>* out_list, ImDrawList* draw_list)
1865{
1866 if (draw_list->CmdBuffer.Size == 0)
1867 return;
1868 if (draw_list->CmdBuffer.Size == 1 && draw_list->CmdBuffer[0].ElemCount == 0 && draw_list->CmdBuffer[0].UserCallback == NULL)
1869 return;
1870
1871 // Draw list sanity check. Detect mismatch between PrimReserve() calls and incrementing _VtxCurrentIdx, _VtxWritePtr etc.
1872 // May trigger for you if you are using PrimXXX functions incorrectly.
1873 IM_ASSERT(draw_list->VtxBuffer.Size == 0 || draw_list->_VtxWritePtr == draw_list->VtxBuffer.Data + draw_list->VtxBuffer.Size);
1874 IM_ASSERT(draw_list->IdxBuffer.Size == 0 || draw_list->_IdxWritePtr == draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size);
1875 if (!(draw_list->Flags & ImDrawListFlags_AllowVtxOffset))
1876 IM_ASSERT((int)draw_list->_VtxCurrentIdx == draw_list->VtxBuffer.Size);
1877
1878 // Check that draw_list doesn't use more vertices than indexable (default ImDrawIdx = unsigned short = 2 bytes = 64K vertices per ImDrawList = per window)
1879 // If this assert triggers because you are drawing lots of stuff manually:
1880 // - First, make sure you are coarse clipping yourself and not trying to draw many things outside visible bounds.
1881 // Be mindful that the lower-level ImDrawList API doesn't filter vertices. Use the Metrics/Debugger window to inspect draw list contents.
1882 // - If you want large meshes with more than 64K vertices, you can either:
1883 // (A) Handle the ImDrawCmd::VtxOffset value in your renderer backend, and set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset'.
1884 // Most example backends already support this from 1.71. Pre-1.71 backends won't.
1885 // Some graphics API such as GL ES 1/2 don't have a way to offset the starting vertex so it is not supported for them.
1886 // (B) Or handle 32-bit indices in your renderer backend, and uncomment '#define ImDrawIdx unsigned int' line in imconfig.h.
1887 // Most example backends already support this. For example, the OpenGL example code detect index size at compile-time:
1888 // glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset);
1889 // Your own engine or render API may use different parameters or function calls to specify index sizes.
1890 // 2 and 4 bytes indices are generally supported by most graphics API.
1891 // - If for some reason neither of those solutions works for you, a workaround is to call BeginChild()/EndChild() before reaching
1892 // the 64K limit to split your draw commands in multiple draw lists.
1893 if (sizeof(ImDrawIdx) == 2)
1894 IM_ASSERT(draw_list->_VtxCurrentIdx < (1 << 16) && "Too many vertices in ImDrawList using 16-bit indices. Read comment above");
1895
1896 // Add to output list + records state in ImDrawData
1897 out_list->push_back(v: draw_list);
1898 draw_data->CmdListsCount++;
1899 draw_data->TotalVtxCount += draw_list->VtxBuffer.Size;
1900 draw_data->TotalIdxCount += draw_list->IdxBuffer.Size;
1901}
1902
1903void ImDrawData::AddDrawList(ImDrawList* draw_list)
1904{
1905 IM_ASSERT(CmdLists.Size == CmdListsCount);
1906 draw_list->_PopUnusedDrawCmd();
1907 ImGui::AddDrawListToDrawDataEx(draw_data: this, out_list: &CmdLists, draw_list);
1908}
1909
1910// For backward compatibility: convert all buffers from indexed to de-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering!
1911void ImDrawData::DeIndexAllBuffers()
1912{
1913 ImVector<ImDrawVert> new_vtx_buffer;
1914 TotalVtxCount = TotalIdxCount = 0;
1915 for (int i = 0; i < CmdListsCount; i++)
1916 {
1917 ImDrawList* cmd_list = CmdLists[i];
1918 if (cmd_list->IdxBuffer.empty())
1919 continue;
1920 new_vtx_buffer.resize(new_size: cmd_list->IdxBuffer.Size);
1921 for (int j = 0; j < cmd_list->IdxBuffer.Size; j++)
1922 new_vtx_buffer[j] = cmd_list->VtxBuffer[cmd_list->IdxBuffer[j]];
1923 cmd_list->VtxBuffer.swap(rhs&: new_vtx_buffer);
1924 cmd_list->IdxBuffer.resize(new_size: 0);
1925 TotalVtxCount += cmd_list->VtxBuffer.Size;
1926 }
1927}
1928
1929// Helper to scale the ClipRect field of each ImDrawCmd.
1930// Use if your final output buffer is at a different scale than draw_data->DisplaySize,
1931// or if there is a difference between your window resolution and framebuffer resolution.
1932void ImDrawData::ScaleClipRects(const ImVec2& fb_scale)
1933{
1934 for (ImDrawList* draw_list : CmdLists)
1935 for (ImDrawCmd& cmd : draw_list->CmdBuffer)
1936 cmd.ClipRect = ImVec4(cmd.ClipRect.x * fb_scale.x, cmd.ClipRect.y * fb_scale.y, cmd.ClipRect.z * fb_scale.x, cmd.ClipRect.w * fb_scale.y);
1937}
1938
1939//-----------------------------------------------------------------------------
1940// [SECTION] Helpers ShadeVertsXXX functions
1941//-----------------------------------------------------------------------------
1942
1943// Generic linear color gradient, write to RGB fields, leave A untouched.
1944void ImGui::ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1)
1945{
1946 ImVec2 gradient_extent = gradient_p1 - gradient_p0;
1947 float gradient_inv_length2 = 1.0f / ImLengthSqr(lhs: gradient_extent);
1948 ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx;
1949 ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx;
1950 const int col0_r = (int)(col0 >> IM_COL32_R_SHIFT) & 0xFF;
1951 const int col0_g = (int)(col0 >> IM_COL32_G_SHIFT) & 0xFF;
1952 const int col0_b = (int)(col0 >> IM_COL32_B_SHIFT) & 0xFF;
1953 const int col_delta_r = ((int)(col1 >> IM_COL32_R_SHIFT) & 0xFF) - col0_r;
1954 const int col_delta_g = ((int)(col1 >> IM_COL32_G_SHIFT) & 0xFF) - col0_g;
1955 const int col_delta_b = ((int)(col1 >> IM_COL32_B_SHIFT) & 0xFF) - col0_b;
1956 for (ImDrawVert* vert = vert_start; vert < vert_end; vert++)
1957 {
1958 float d = ImDot(a: vert->pos - gradient_p0, b: gradient_extent);
1959 float t = ImClamp(v: d * gradient_inv_length2, mn: 0.0f, mx: 1.0f);
1960 int r = (int)(col0_r + col_delta_r * t);
1961 int g = (int)(col0_g + col_delta_g * t);
1962 int b = (int)(col0_b + col_delta_b * t);
1963 vert->col = (r << IM_COL32_R_SHIFT) | (g << IM_COL32_G_SHIFT) | (b << IM_COL32_B_SHIFT) | (vert->col & IM_COL32_A_MASK);
1964 }
1965}
1966
1967// Distribute UV over (a, b) rectangle
1968void ImGui::ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, bool clamp)
1969{
1970 const ImVec2 size = b - a;
1971 const ImVec2 uv_size = uv_b - uv_a;
1972 const ImVec2 scale = ImVec2(
1973 size.x != 0.0f ? (uv_size.x / size.x) : 0.0f,
1974 size.y != 0.0f ? (uv_size.y / size.y) : 0.0f);
1975
1976 ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx;
1977 ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx;
1978 if (clamp)
1979 {
1980 const ImVec2 min = ImMin(lhs: uv_a, rhs: uv_b);
1981 const ImVec2 max = ImMax(lhs: uv_a, rhs: uv_b);
1982 for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex)
1983 vertex->uv = ImClamp(v: uv_a + ImMul(lhs: ImVec2(vertex->pos.x, vertex->pos.y) - a, rhs: scale), mn: min, mx: max);
1984 }
1985 else
1986 {
1987 for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex)
1988 vertex->uv = uv_a + ImMul(lhs: ImVec2(vertex->pos.x, vertex->pos.y) - a, rhs: scale);
1989 }
1990}
1991
1992void ImGui::ShadeVertsTransformPos(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& pivot_in, float cos_a, float sin_a, const ImVec2& pivot_out)
1993{
1994 ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx;
1995 ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx;
1996 for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex)
1997 vertex->pos = ImRotate(v: vertex->pos- pivot_in, cos_a, sin_a) + pivot_out;
1998}
1999
2000//-----------------------------------------------------------------------------
2001// [SECTION] ImFontConfig
2002//-----------------------------------------------------------------------------
2003
2004ImFontConfig::ImFontConfig()
2005{
2006 memset(s: this, c: 0, n: sizeof(*this));
2007 FontDataOwnedByAtlas = true;
2008 OversampleH = 2;
2009 OversampleV = 1;
2010 GlyphMaxAdvanceX = FLT_MAX;
2011 RasterizerMultiply = 1.0f;
2012 RasterizerDensity = 1.0f;
2013 EllipsisChar = (ImWchar)-1;
2014}
2015
2016//-----------------------------------------------------------------------------
2017// [SECTION] ImFontAtlas
2018//-----------------------------------------------------------------------------
2019
2020// A work of art lies ahead! (. = white layer, X = black layer, others are blank)
2021// The 2x2 white texels on the top left are the ones we'll use everywhere in Dear ImGui to render filled shapes.
2022// (This is used when io.MouseDrawCursor = true)
2023const int FONT_ATLAS_DEFAULT_TEX_DATA_W = 122; // Actual texture will be 2 times that + 1 spacing.
2024const int FONT_ATLAS_DEFAULT_TEX_DATA_H = 27;
2025static const char FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[FONT_ATLAS_DEFAULT_TEX_DATA_W * FONT_ATLAS_DEFAULT_TEX_DATA_H + 1] =
2026{
2027 "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX- XX - XX XX "
2028 "..- -X.....X- X.X - X.X -X.....X - X.....X- X..X -X..X X..X"
2029 "--- -XXX.XXX- X...X - X...X -X....X - X....X- X..X -X...X X...X"
2030 "X - X.X - X.....X - X.....X -X...X - X...X- X..X - X...X X...X "
2031 "XX - X.X -X.......X- X.......X -X..X.X - X.X..X- X..X - X...X...X "
2032 "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X- X..XXX - X.....X "
2033 "X..X - X.X - X.X - X.X -XX X.X - X.X XX- X..X..XXX - X...X "
2034 "X...X - X.X - X.X - XX X.X XX - X.X - X.X - X..X..X..XX - X.X "
2035 "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X - X..X..X..X.X - X...X "
2036 "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X -XXX X..X..X..X..X- X.....X "
2037 "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X -X..XX........X..X- X...X...X "
2038 "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X -X...X...........X- X...X X...X "
2039 "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X - X..............X-X...X X...X"
2040 "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X - X.............X-X..X X..X"
2041 "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X - X.............X- XX XX "
2042 "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X - X............X--------------"
2043 "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX - X...........X - "
2044 "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------- X..........X - "
2045 "X.X X..X - -X.......X- X.......X - XX XX - - X..........X - "
2046 "XX X..X - - X.....X - X.....X - X.X X.X - - X........X - "
2047 " X..X - - X...X - X...X - X..X X..X - - X........X - "
2048 " XX - - X.X - X.X - X...XXXXXXXXXXXXX...X - - XXXXXXXXXX - "
2049 "------------- - X - X -X.....................X- ------------------- "
2050 " ----------------------------------- X...XXXXXXXXXXXXX...X - "
2051 " - X..X X..X - "
2052 " - X.X X.X - "
2053 " - XX XX - "
2054};
2055
2056static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3] =
2057{
2058 // Pos ........ Size ......... Offset ......
2059 { ImVec2( 0,3), ImVec2(12,19), ImVec2( 0, 0) }, // ImGuiMouseCursor_Arrow
2060 { ImVec2(13,0), ImVec2( 7,16), ImVec2( 1, 8) }, // ImGuiMouseCursor_TextInput
2061 { ImVec2(31,0), ImVec2(23,23), ImVec2(11,11) }, // ImGuiMouseCursor_ResizeAll
2062 { ImVec2(21,0), ImVec2( 9,23), ImVec2( 4,11) }, // ImGuiMouseCursor_ResizeNS
2063 { ImVec2(55,18),ImVec2(23, 9), ImVec2(11, 4) }, // ImGuiMouseCursor_ResizeEW
2064 { ImVec2(73,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNESW
2065 { ImVec2(55,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNWSE
2066 { ImVec2(91,0), ImVec2(17,22), ImVec2( 5, 0) }, // ImGuiMouseCursor_Hand
2067 { ImVec2(109,0),ImVec2(13,15), ImVec2( 6, 7) }, // ImGuiMouseCursor_NotAllowed
2068};
2069
2070ImFontAtlas::ImFontAtlas()
2071{
2072 memset(s: this, c: 0, n: sizeof(*this));
2073 TexGlyphPadding = 1;
2074 PackIdMouseCursors = PackIdLines = -1;
2075}
2076
2077ImFontAtlas::~ImFontAtlas()
2078{
2079 IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
2080 Clear();
2081}
2082
2083void ImFontAtlas::ClearInputData()
2084{
2085 IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
2086 for (ImFontConfig& font_cfg : ConfigData)
2087 if (font_cfg.FontData && font_cfg.FontDataOwnedByAtlas)
2088 {
2089 IM_FREE(font_cfg.FontData);
2090 font_cfg.FontData = NULL;
2091 }
2092
2093 // When clearing this we lose access to the font name and other information used to build the font.
2094 for (ImFont* font : Fonts)
2095 if (font->ConfigData >= ConfigData.Data && font->ConfigData < ConfigData.Data + ConfigData.Size)
2096 {
2097 font->ConfigData = NULL;
2098 font->ConfigDataCount = 0;
2099 }
2100 ConfigData.clear();
2101 CustomRects.clear();
2102 PackIdMouseCursors = PackIdLines = -1;
2103 // Important: we leave TexReady untouched
2104}
2105
2106void ImFontAtlas::ClearTexData()
2107{
2108 IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
2109 if (TexPixelsAlpha8)
2110 IM_FREE(TexPixelsAlpha8);
2111 if (TexPixelsRGBA32)
2112 IM_FREE(TexPixelsRGBA32);
2113 TexPixelsAlpha8 = NULL;
2114 TexPixelsRGBA32 = NULL;
2115 TexPixelsUseColors = false;
2116 // Important: we leave TexReady untouched
2117}
2118
2119void ImFontAtlas::ClearFonts()
2120{
2121 IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
2122 Fonts.clear_delete();
2123 TexReady = false;
2124}
2125
2126void ImFontAtlas::Clear()
2127{
2128 ClearInputData();
2129 ClearTexData();
2130 ClearFonts();
2131}
2132
2133void ImFontAtlas::GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel)
2134{
2135 // Build atlas on demand
2136 if (TexPixelsAlpha8 == NULL)
2137 Build();
2138
2139 *out_pixels = TexPixelsAlpha8;
2140 if (out_width) *out_width = TexWidth;
2141 if (out_height) *out_height = TexHeight;
2142 if (out_bytes_per_pixel) *out_bytes_per_pixel = 1;
2143}
2144
2145void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel)
2146{
2147 // Convert to RGBA32 format on demand
2148 // Although it is likely to be the most commonly used format, our font rendering is 1 channel / 8 bpp
2149 if (!TexPixelsRGBA32)
2150 {
2151 unsigned char* pixels = NULL;
2152 GetTexDataAsAlpha8(out_pixels: &pixels, NULL, NULL);
2153 if (pixels)
2154 {
2155 TexPixelsRGBA32 = (unsigned int*)IM_ALLOC((size_t)TexWidth * (size_t)TexHeight * 4);
2156 const unsigned char* src = pixels;
2157 unsigned int* dst = TexPixelsRGBA32;
2158 for (int n = TexWidth * TexHeight; n > 0; n--)
2159 *dst++ = IM_COL32(255, 255, 255, (unsigned int)(*src++));
2160 }
2161 }
2162
2163 *out_pixels = (unsigned char*)TexPixelsRGBA32;
2164 if (out_width) *out_width = TexWidth;
2165 if (out_height) *out_height = TexHeight;
2166 if (out_bytes_per_pixel) *out_bytes_per_pixel = 4;
2167}
2168
2169ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg)
2170{
2171 IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
2172 IM_ASSERT(font_cfg->FontData != NULL && font_cfg->FontDataSize > 0);
2173 IM_ASSERT(font_cfg->SizePixels > 0.0f);
2174
2175 // Create new font
2176 if (!font_cfg->MergeMode)
2177 Fonts.push_back(IM_NEW(ImFont));
2178 else
2179 IM_ASSERT(!Fonts.empty() && "Cannot use MergeMode for the first font"); // When using MergeMode make sure that a font has already been added before. You can use ImGui::GetIO().Fonts->AddFontDefault() to add the default imgui font.
2180
2181 ConfigData.push_back(v: *font_cfg);
2182 ImFontConfig& new_font_cfg = ConfigData.back();
2183 if (new_font_cfg.DstFont == NULL)
2184 new_font_cfg.DstFont = Fonts.back();
2185 if (!new_font_cfg.FontDataOwnedByAtlas)
2186 {
2187 new_font_cfg.FontData = IM_ALLOC(new_font_cfg.FontDataSize);
2188 new_font_cfg.FontDataOwnedByAtlas = true;
2189 memcpy(dest: new_font_cfg.FontData, src: font_cfg->FontData, n: (size_t)new_font_cfg.FontDataSize);
2190 }
2191
2192 if (new_font_cfg.DstFont->EllipsisChar == (ImWchar)-1)
2193 new_font_cfg.DstFont->EllipsisChar = font_cfg->EllipsisChar;
2194
2195 ImFontAtlasUpdateConfigDataPointers(atlas: this);
2196
2197 // Invalidate texture
2198 TexReady = false;
2199 ClearTexData();
2200 return new_font_cfg.DstFont;
2201}
2202
2203// Default font TTF is compressed with stb_compress then base85 encoded (see misc/fonts/binary_to_compressed_c.cpp for encoder)
2204static unsigned int stb_decompress_length(const unsigned char* input);
2205static unsigned int stb_decompress(unsigned char* output, const unsigned char* input, unsigned int length);
2206static const char* GetDefaultCompressedFontDataTTFBase85();
2207static unsigned int Decode85Byte(char c) { return c >= '\\' ? c-36 : c-35; }
2208static void Decode85(const unsigned char* src, unsigned char* dst)
2209{
2210 while (*src)
2211 {
2212 unsigned int tmp = Decode85Byte(c: src[0]) + 85 * (Decode85Byte(c: src[1]) + 85 * (Decode85Byte(c: src[2]) + 85 * (Decode85Byte(c: src[3]) + 85 * Decode85Byte(c: src[4]))));
2213 dst[0] = ((tmp >> 0) & 0xFF); dst[1] = ((tmp >> 8) & 0xFF); dst[2] = ((tmp >> 16) & 0xFF); dst[3] = ((tmp >> 24) & 0xFF); // We can't assume little-endianness.
2214 src += 5;
2215 dst += 4;
2216 }
2217}
2218
2219// Load embedded ProggyClean.ttf at size 13, disable oversampling
2220ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template)
2221{
2222 ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
2223 if (!font_cfg_template)
2224 {
2225 font_cfg.OversampleH = font_cfg.OversampleV = 1;
2226 font_cfg.PixelSnapH = true;
2227 }
2228 if (font_cfg.SizePixels <= 0.0f)
2229 font_cfg.SizePixels = 13.0f * 1.0f;
2230 if (font_cfg.Name[0] == '\0')
2231 ImFormatString(buf: font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), fmt: "ProggyClean.ttf, %dpx", (int)font_cfg.SizePixels);
2232 font_cfg.EllipsisChar = (ImWchar)0x0085;
2233 font_cfg.GlyphOffset.y = 1.0f * IM_TRUNC(font_cfg.SizePixels / 13.0f); // Add +1 offset per 13 units
2234
2235 const char* ttf_compressed_base85 = GetDefaultCompressedFontDataTTFBase85();
2236 const ImWchar* glyph_ranges = font_cfg.GlyphRanges != NULL ? font_cfg.GlyphRanges : GetGlyphRangesDefault();
2237 ImFont* font = AddFontFromMemoryCompressedBase85TTF(compressed_font_data_base85: ttf_compressed_base85, size_pixels: font_cfg.SizePixels, font_cfg: &font_cfg, glyph_ranges);
2238 return font;
2239}
2240
2241ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)
2242{
2243 IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
2244 size_t data_size = 0;
2245 void* data = ImFileLoadToMemory(filename, mode: "rb", out_file_size: &data_size, padding_bytes: 0);
2246 if (!data)
2247 {
2248 IM_ASSERT_USER_ERROR(0, "Could not load font file!");
2249 return NULL;
2250 }
2251 ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
2252 if (font_cfg.Name[0] == '\0')
2253 {
2254 // Store a short copy of filename into into the font name for convenience
2255 const char* p;
2256 for (p = filename + strlen(s: filename); p > filename && p[-1] != '/' && p[-1] != '\\'; p--) {}
2257 ImFormatString(buf: font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), fmt: "%s, %.0fpx", p, size_pixels);
2258 }
2259 return AddFontFromMemoryTTF(font_data: data, font_data_size: (int)data_size, size_pixels, font_cfg: &font_cfg, glyph_ranges);
2260}
2261
2262// NB: Transfer ownership of 'ttf_data' to ImFontAtlas, unless font_cfg_template->FontDataOwnedByAtlas == false. Owned TTF buffer will be deleted after Build().
2263ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* font_data, int font_data_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)
2264{
2265 IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
2266 ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
2267 IM_ASSERT(font_cfg.FontData == NULL);
2268 IM_ASSERT(font_data_size > 100 && "Incorrect value for font_data_size!"); // Heuristic to prevent accidentally passing a wrong value to font_data_size.
2269 font_cfg.FontData = font_data;
2270 font_cfg.FontDataSize = font_data_size;
2271 font_cfg.SizePixels = size_pixels > 0.0f ? size_pixels : font_cfg.SizePixels;
2272 if (glyph_ranges)
2273 font_cfg.GlyphRanges = glyph_ranges;
2274 return AddFont(font_cfg: &font_cfg);
2275}
2276
2277ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)
2278{
2279 const unsigned int buf_decompressed_size = stb_decompress_length(input: (const unsigned char*)compressed_ttf_data);
2280 unsigned char* buf_decompressed_data = (unsigned char*)IM_ALLOC(buf_decompressed_size);
2281 stb_decompress(output: buf_decompressed_data, input: (const unsigned char*)compressed_ttf_data, length: (unsigned int)compressed_ttf_size);
2282
2283 ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
2284 IM_ASSERT(font_cfg.FontData == NULL);
2285 font_cfg.FontDataOwnedByAtlas = true;
2286 return AddFontFromMemoryTTF(font_data: buf_decompressed_data, font_data_size: (int)buf_decompressed_size, size_pixels, font_cfg_template: &font_cfg, glyph_ranges);
2287}
2288
2289ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges)
2290{
2291 int compressed_ttf_size = (((int)strlen(s: compressed_ttf_data_base85) + 4) / 5) * 4;
2292 void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size);
2293 Decode85(src: (const unsigned char*)compressed_ttf_data_base85, dst: (unsigned char*)compressed_ttf);
2294 ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf_data: compressed_ttf, compressed_ttf_size, size_pixels, font_cfg_template: font_cfg, glyph_ranges);
2295 IM_FREE(compressed_ttf);
2296 return font;
2297}
2298
2299int ImFontAtlas::AddCustomRectRegular(int width, int height)
2300{
2301 IM_ASSERT(width > 0 && width <= 0xFFFF);
2302 IM_ASSERT(height > 0 && height <= 0xFFFF);
2303 ImFontAtlasCustomRect r;
2304 r.Width = (unsigned short)width;
2305 r.Height = (unsigned short)height;
2306 CustomRects.push_back(v: r);
2307 return CustomRects.Size - 1; // Return index
2308}
2309
2310int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset)
2311{
2312#ifdef IMGUI_USE_WCHAR32
2313 IM_ASSERT(id <= IM_UNICODE_CODEPOINT_MAX);
2314#endif
2315 IM_ASSERT(font != NULL);
2316 IM_ASSERT(width > 0 && width <= 0xFFFF);
2317 IM_ASSERT(height > 0 && height <= 0xFFFF);
2318 ImFontAtlasCustomRect r;
2319 r.Width = (unsigned short)width;
2320 r.Height = (unsigned short)height;
2321 r.GlyphID = id;
2322 r.GlyphAdvanceX = advance_x;
2323 r.GlyphOffset = offset;
2324 r.Font = font;
2325 CustomRects.push_back(v: r);
2326 return CustomRects.Size - 1; // Return index
2327}
2328
2329void ImFontAtlas::CalcCustomRectUV(const ImFontAtlasCustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max) const
2330{
2331 IM_ASSERT(TexWidth > 0 && TexHeight > 0); // Font atlas needs to be built before we can calculate UV coordinates
2332 IM_ASSERT(rect->IsPacked()); // Make sure the rectangle has been packed
2333 *out_uv_min = ImVec2((float)rect->X * TexUvScale.x, (float)rect->Y * TexUvScale.y);
2334 *out_uv_max = ImVec2((float)(rect->X + rect->Width) * TexUvScale.x, (float)(rect->Y + rect->Height) * TexUvScale.y);
2335}
2336
2337bool ImFontAtlas::GetMouseCursorTexData(ImGuiMouseCursor cursor_type, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2])
2338{
2339 if (cursor_type <= ImGuiMouseCursor_None || cursor_type >= ImGuiMouseCursor_COUNT)
2340 return false;
2341 if (Flags & ImFontAtlasFlags_NoMouseCursors)
2342 return false;
2343
2344 IM_ASSERT(PackIdMouseCursors != -1);
2345 ImFontAtlasCustomRect* r = GetCustomRectByIndex(index: PackIdMouseCursors);
2346 ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][0] + ImVec2((float)r->X, (float)r->Y);
2347 ImVec2 size = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][1];
2348 *out_size = size;
2349 *out_offset = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][2];
2350 out_uv_border[0] = (pos) * TexUvScale;
2351 out_uv_border[1] = (pos + size) * TexUvScale;
2352 pos.x += FONT_ATLAS_DEFAULT_TEX_DATA_W + 1;
2353 out_uv_fill[0] = (pos) * TexUvScale;
2354 out_uv_fill[1] = (pos + size) * TexUvScale;
2355 return true;
2356}
2357
2358bool ImFontAtlas::Build()
2359{
2360 IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
2361
2362 // Default font is none are specified
2363 if (ConfigData.Size == 0)
2364 AddFontDefault();
2365
2366 // Select builder
2367 // - Note that we do not reassign to atlas->FontBuilderIO, since it is likely to point to static data which
2368 // may mess with some hot-reloading schemes. If you need to assign to this (for dynamic selection) AND are
2369 // using a hot-reloading scheme that messes up static data, store your own instance of ImFontBuilderIO somewhere
2370 // and point to it instead of pointing directly to return value of the GetBuilderXXX functions.
2371 const ImFontBuilderIO* builder_io = FontBuilderIO;
2372 if (builder_io == NULL)
2373 {
2374#ifdef IMGUI_ENABLE_FREETYPE
2375 builder_io = ImGuiFreeType::GetBuilderForFreeType();
2376#elif defined(IMGUI_ENABLE_STB_TRUETYPE)
2377 builder_io = ImFontAtlasGetBuilderForStbTruetype();
2378#else
2379 IM_ASSERT(0); // Invalid Build function
2380#endif
2381 }
2382
2383 // Build
2384 return builder_io->FontBuilder_Build(this);
2385}
2386
2387void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_brighten_factor)
2388{
2389 for (unsigned int i = 0; i < 256; i++)
2390 {
2391 unsigned int value = (unsigned int)(i * in_brighten_factor);
2392 out_table[i] = value > 255 ? 255 : (value & 0xFF);
2393 }
2394}
2395
2396void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride)
2397{
2398 IM_ASSERT_PARANOID(w <= stride);
2399 unsigned char* data = pixels + x + y * stride;
2400 for (int j = h; j > 0; j--, data += stride - w)
2401 for (int i = w; i > 0; i--, data++)
2402 *data = table[*data];
2403}
2404
2405#ifdef IMGUI_ENABLE_STB_TRUETYPE
2406// Temporary data for one source font (multiple source fonts can be merged into one destination ImFont)
2407// (C++03 doesn't allow instancing ImVector<> with function-local types so we declare the type here.)
2408struct ImFontBuildSrcData
2409{
2410 stbtt_fontinfo FontInfo;
2411 stbtt_pack_range PackRange; // Hold the list of codepoints to pack (essentially points to Codepoints.Data)
2412 stbrp_rect* Rects; // Rectangle to pack. We first fill in their size and the packer will give us their position.
2413 stbtt_packedchar* PackedChars; // Output glyphs
2414 const ImWchar* SrcRanges; // Ranges as requested by user (user is allowed to request too much, e.g. 0x0020..0xFFFF)
2415 int DstIndex; // Index into atlas->Fonts[] and dst_tmp_array[]
2416 int GlyphsHighest; // Highest requested codepoint
2417 int GlyphsCount; // Glyph count (excluding missing glyphs and glyphs already set by an earlier source font)
2418 ImBitVector GlyphsSet; // Glyph bit map (random access, 1-bit per codepoint. This will be a maximum of 8KB)
2419 ImVector<int> GlyphsList; // Glyph codepoints list (flattened version of GlyphsSet)
2420};
2421
2422// Temporary data for one destination ImFont* (multiple source fonts can be merged into one destination ImFont)
2423struct ImFontBuildDstData
2424{
2425 int SrcCount; // Number of source fonts targeting this destination font.
2426 int GlyphsHighest;
2427 int GlyphsCount;
2428 ImBitVector GlyphsSet; // This is used to resolve collision when multiple sources are merged into a same destination font.
2429};
2430
2431static void UnpackBitVectorToFlatIndexList(const ImBitVector* in, ImVector<int>* out)
2432{
2433 IM_ASSERT(sizeof(in->Storage.Data[0]) == sizeof(int));
2434 const ImU32* it_begin = in->Storage.begin();
2435 const ImU32* it_end = in->Storage.end();
2436 for (const ImU32* it = it_begin; it < it_end; it++)
2437 if (ImU32 entries_32 = *it)
2438 for (ImU32 bit_n = 0; bit_n < 32; bit_n++)
2439 if (entries_32 & ((ImU32)1 << bit_n))
2440 out->push_back(v: (int)(((it - it_begin) << 5) + bit_n));
2441}
2442
2443static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)
2444{
2445 IM_ASSERT(atlas->ConfigData.Size > 0);
2446
2447 ImFontAtlasBuildInit(atlas);
2448
2449 // Clear atlas
2450 atlas->TexID = (ImTextureID)NULL;
2451 atlas->TexWidth = atlas->TexHeight = 0;
2452 atlas->TexUvScale = ImVec2(0.0f, 0.0f);
2453 atlas->TexUvWhitePixel = ImVec2(0.0f, 0.0f);
2454 atlas->ClearTexData();
2455
2456 // Temporary storage for building
2457 ImVector<ImFontBuildSrcData> src_tmp_array;
2458 ImVector<ImFontBuildDstData> dst_tmp_array;
2459 src_tmp_array.resize(new_size: atlas->ConfigData.Size);
2460 dst_tmp_array.resize(new_size: atlas->Fonts.Size);
2461 memset(s: src_tmp_array.Data, c: 0, n: (size_t)src_tmp_array.size_in_bytes());
2462 memset(s: dst_tmp_array.Data, c: 0, n: (size_t)dst_tmp_array.size_in_bytes());
2463
2464 // 1. Initialize font loading structure, check font data validity
2465 for (int src_i = 0; src_i < atlas->ConfigData.Size; src_i++)
2466 {
2467 ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
2468 ImFontConfig& cfg = atlas->ConfigData[src_i];
2469 IM_ASSERT(cfg.DstFont && (!cfg.DstFont->IsLoaded() || cfg.DstFont->ContainerAtlas == atlas));
2470
2471 // Find index from cfg.DstFont (we allow the user to set cfg.DstFont. Also it makes casual debugging nicer than when storing indices)
2472 src_tmp.DstIndex = -1;
2473 for (int output_i = 0; output_i < atlas->Fonts.Size && src_tmp.DstIndex == -1; output_i++)
2474 if (cfg.DstFont == atlas->Fonts[output_i])
2475 src_tmp.DstIndex = output_i;
2476 if (src_tmp.DstIndex == -1)
2477 {
2478 IM_ASSERT(src_tmp.DstIndex != -1); // cfg.DstFont not pointing within atlas->Fonts[] array?
2479 return false;
2480 }
2481 // Initialize helper structure for font loading and verify that the TTF/OTF data is correct
2482 const int font_offset = stbtt_GetFontOffsetForIndex(data: (unsigned char*)cfg.FontData, index: cfg.FontNo);
2483 IM_ASSERT(font_offset >= 0 && "FontData is incorrect, or FontNo cannot be found.");
2484 if (!stbtt_InitFont(info: &src_tmp.FontInfo, data: (unsigned char*)cfg.FontData, offset: font_offset))
2485 {
2486 IM_ASSERT(0 && "stbtt_InitFont(): failed to parse FontData. It is correct and complete? Check FontDataSize.");
2487 return false;
2488 }
2489
2490 // Measure highest codepoints
2491 ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex];
2492 src_tmp.SrcRanges = cfg.GlyphRanges ? cfg.GlyphRanges : atlas->GetGlyphRangesDefault();
2493 for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2)
2494 {
2495 // Check for valid range. This may also help detect *some* dangling pointers, because a common
2496 // user error is to setup ImFontConfig::GlyphRanges with a pointer to data that isn't persistent.
2497 IM_ASSERT(src_range[0] <= src_range[1]);
2498 src_tmp.GlyphsHighest = ImMax(lhs: src_tmp.GlyphsHighest, rhs: (int)src_range[1]);
2499 }
2500 dst_tmp.SrcCount++;
2501 dst_tmp.GlyphsHighest = ImMax(lhs: dst_tmp.GlyphsHighest, rhs: src_tmp.GlyphsHighest);
2502 }
2503
2504 // 2. For every requested codepoint, check for their presence in the font data, and handle redundancy or overlaps between source fonts to avoid unused glyphs.
2505 int total_glyphs_count = 0;
2506 for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
2507 {
2508 ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
2509 ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex];
2510 src_tmp.GlyphsSet.Create(sz: src_tmp.GlyphsHighest + 1);
2511 if (dst_tmp.GlyphsSet.Storage.empty())
2512 dst_tmp.GlyphsSet.Create(sz: dst_tmp.GlyphsHighest + 1);
2513
2514 for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2)
2515 for (unsigned int codepoint = src_range[0]; codepoint <= src_range[1]; codepoint++)
2516 {
2517 if (dst_tmp.GlyphsSet.TestBit(n: codepoint)) // Don't overwrite existing glyphs. We could make this an option for MergeMode (e.g. MergeOverwrite==true)
2518 continue;
2519 if (!stbtt_FindGlyphIndex(info: &src_tmp.FontInfo, unicode_codepoint: codepoint)) // It is actually in the font?
2520 continue;
2521
2522 // Add to avail set/counters
2523 src_tmp.GlyphsCount++;
2524 dst_tmp.GlyphsCount++;
2525 src_tmp.GlyphsSet.SetBit(codepoint);
2526 dst_tmp.GlyphsSet.SetBit(codepoint);
2527 total_glyphs_count++;
2528 }
2529 }
2530
2531 // 3. Unpack our bit map into a flat list (we now have all the Unicode points that we know are requested _and_ available _and_ not overlapping another)
2532 for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
2533 {
2534 ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
2535 src_tmp.GlyphsList.reserve(new_capacity: src_tmp.GlyphsCount);
2536 UnpackBitVectorToFlatIndexList(in: &src_tmp.GlyphsSet, out: &src_tmp.GlyphsList);
2537 src_tmp.GlyphsSet.Clear();
2538 IM_ASSERT(src_tmp.GlyphsList.Size == src_tmp.GlyphsCount);
2539 }
2540 for (int dst_i = 0; dst_i < dst_tmp_array.Size; dst_i++)
2541 dst_tmp_array[dst_i].GlyphsSet.Clear();
2542 dst_tmp_array.clear();
2543
2544 // Allocate packing character data and flag packed characters buffer as non-packed (x0=y0=x1=y1=0)
2545 // (We technically don't need to zero-clear buf_rects, but let's do it for the sake of sanity)
2546 ImVector<stbrp_rect> buf_rects;
2547 ImVector<stbtt_packedchar> buf_packedchars;
2548 buf_rects.resize(new_size: total_glyphs_count);
2549 buf_packedchars.resize(new_size: total_glyphs_count);
2550 memset(s: buf_rects.Data, c: 0, n: (size_t)buf_rects.size_in_bytes());
2551 memset(s: buf_packedchars.Data, c: 0, n: (size_t)buf_packedchars.size_in_bytes());
2552
2553 // 4. Gather glyphs sizes so we can pack them in our virtual canvas.
2554 int total_surface = 0;
2555 int buf_rects_out_n = 0;
2556 int buf_packedchars_out_n = 0;
2557 for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
2558 {
2559 ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
2560 if (src_tmp.GlyphsCount == 0)
2561 continue;
2562
2563 src_tmp.Rects = &buf_rects[buf_rects_out_n];
2564 src_tmp.PackedChars = &buf_packedchars[buf_packedchars_out_n];
2565 buf_rects_out_n += src_tmp.GlyphsCount;
2566 buf_packedchars_out_n += src_tmp.GlyphsCount;
2567
2568 // Convert our ranges in the format stb_truetype wants
2569 ImFontConfig& cfg = atlas->ConfigData[src_i];
2570 src_tmp.PackRange.font_size = cfg.SizePixels * cfg.RasterizerDensity;
2571 src_tmp.PackRange.first_unicode_codepoint_in_range = 0;
2572 src_tmp.PackRange.array_of_unicode_codepoints = src_tmp.GlyphsList.Data;
2573 src_tmp.PackRange.num_chars = src_tmp.GlyphsList.Size;
2574 src_tmp.PackRange.chardata_for_range = src_tmp.PackedChars;
2575 src_tmp.PackRange.h_oversample = (unsigned char)cfg.OversampleH;
2576 src_tmp.PackRange.v_oversample = (unsigned char)cfg.OversampleV;
2577
2578 // Gather the sizes of all rectangles we will need to pack (this loop is based on stbtt_PackFontRangesGatherRects)
2579 const float scale = (cfg.SizePixels > 0.0f) ? stbtt_ScaleForPixelHeight(info: &src_tmp.FontInfo, height: cfg.SizePixels * cfg.RasterizerDensity) : stbtt_ScaleForMappingEmToPixels(info: &src_tmp.FontInfo, pixels: -cfg.SizePixels * cfg.RasterizerDensity);
2580 const int padding = atlas->TexGlyphPadding;
2581 for (int glyph_i = 0; glyph_i < src_tmp.GlyphsList.Size; glyph_i++)
2582 {
2583 int x0, y0, x1, y1;
2584 const int glyph_index_in_font = stbtt_FindGlyphIndex(info: &src_tmp.FontInfo, unicode_codepoint: src_tmp.GlyphsList[glyph_i]);
2585 IM_ASSERT(glyph_index_in_font != 0);
2586 stbtt_GetGlyphBitmapBoxSubpixel(font: &src_tmp.FontInfo, glyph: glyph_index_in_font, scale_x: scale * cfg.OversampleH, scale_y: scale * cfg.OversampleV, shift_x: 0, shift_y: 0, ix0: &x0, iy0: &y0, ix1: &x1, iy1: &y1);
2587 src_tmp.Rects[glyph_i].w = (stbrp_coord)(x1 - x0 + padding + cfg.OversampleH - 1);
2588 src_tmp.Rects[glyph_i].h = (stbrp_coord)(y1 - y0 + padding + cfg.OversampleV - 1);
2589 total_surface += src_tmp.Rects[glyph_i].w * src_tmp.Rects[glyph_i].h;
2590 }
2591 }
2592
2593 // We need a width for the skyline algorithm, any width!
2594 // The exact width doesn't really matter much, but some API/GPU have texture size limitations and increasing width can decrease height.
2595 // User can override TexDesiredWidth and TexGlyphPadding if they wish, otherwise we use a simple heuristic to select the width based on expected surface.
2596 const int surface_sqrt = (int)ImSqrt((float)total_surface) + 1;
2597 atlas->TexHeight = 0;
2598 if (atlas->TexDesiredWidth > 0)
2599 atlas->TexWidth = atlas->TexDesiredWidth;
2600 else
2601 atlas->TexWidth = (surface_sqrt >= 4096 * 0.7f) ? 4096 : (surface_sqrt >= 2048 * 0.7f) ? 2048 : (surface_sqrt >= 1024 * 0.7f) ? 1024 : 512;
2602
2603 // 5. Start packing
2604 // Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values).
2605 const int TEX_HEIGHT_MAX = 1024 * 32;
2606 stbtt_pack_context spc = {};
2607 stbtt_PackBegin(spc: &spc, NULL, pw: atlas->TexWidth, ph: TEX_HEIGHT_MAX, stride_in_bytes: 0, padding: atlas->TexGlyphPadding, NULL);
2608 ImFontAtlasBuildPackCustomRects(atlas, stbrp_context_opaque: spc.pack_info);
2609
2610 // 6. Pack each source font. No rendering yet, we are working with rectangles in an infinitely tall texture at this point.
2611 for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
2612 {
2613 ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
2614 if (src_tmp.GlyphsCount == 0)
2615 continue;
2616
2617 stbrp_pack_rects(context: (stbrp_context*)spc.pack_info, rects: src_tmp.Rects, num_rects: src_tmp.GlyphsCount);
2618
2619 // Extend texture height and mark missing glyphs as non-packed so we won't render them.
2620 // FIXME: We are not handling packing failure here (would happen if we got off TEX_HEIGHT_MAX or if a single if larger than TexWidth?)
2621 for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++)
2622 if (src_tmp.Rects[glyph_i].was_packed)
2623 atlas->TexHeight = ImMax(lhs: atlas->TexHeight, rhs: src_tmp.Rects[glyph_i].y + src_tmp.Rects[glyph_i].h);
2624 }
2625
2626 // 7. Allocate texture
2627 atlas->TexHeight = (atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) ? (atlas->TexHeight + 1) : ImUpperPowerOfTwo(v: atlas->TexHeight);
2628 atlas->TexUvScale = ImVec2(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight);
2629 atlas->TexPixelsAlpha8 = (unsigned char*)IM_ALLOC(atlas->TexWidth * atlas->TexHeight);
2630 memset(s: atlas->TexPixelsAlpha8, c: 0, n: atlas->TexWidth * atlas->TexHeight);
2631 spc.pixels = atlas->TexPixelsAlpha8;
2632 spc.height = atlas->TexHeight;
2633
2634 // 8. Render/rasterize font characters into the texture
2635 for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
2636 {
2637 ImFontConfig& cfg = atlas->ConfigData[src_i];
2638 ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
2639 if (src_tmp.GlyphsCount == 0)
2640 continue;
2641
2642 stbtt_PackFontRangesRenderIntoRects(spc: &spc, info: &src_tmp.FontInfo, ranges: &src_tmp.PackRange, num_ranges: 1, rects: src_tmp.Rects);
2643
2644 // Apply multiply operator
2645 if (cfg.RasterizerMultiply != 1.0f)
2646 {
2647 unsigned char multiply_table[256];
2648 ImFontAtlasBuildMultiplyCalcLookupTable(out_table: multiply_table, in_brighten_factor: cfg.RasterizerMultiply);
2649 stbrp_rect* r = &src_tmp.Rects[0];
2650 for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++, r++)
2651 if (r->was_packed)
2652 ImFontAtlasBuildMultiplyRectAlpha8(table: multiply_table, pixels: atlas->TexPixelsAlpha8, x: r->