Skip to content

Commit e9a617b

Browse files
committed
Moved imgui_freetype from imgui_club (#618)
1 parent 147ec8d commit e9a617b

File tree

2 files changed

+400
-0
lines changed

2 files changed

+400
-0
lines changed

misc/freetype/imgui_freetype.cpp

+369
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
// Wrapper to use Freetype (instead of stb_truetype) for Dear ImGui
2+
// Get latest version at http://www.github.com/ocornut/imgui_club
3+
// Original code by @Vuhdo (Aleksei Skriabin)
4+
5+
// Changelog:
6+
// - v0.50: (2017/08/16) imported from https://github.com/Vuhdo/imgui_freetype, updated for latest changes in ImFontAtlas, minor tweaks.
7+
// - v0.51: (2017/08/26) cleanup, optimizations, support for ImFontConfig::RasterizerFlags, ImFontConfig::RasterizerMultiply.
8+
// - v0.52: (2017/09/26) fixes for imgui internal changes
9+
// - v0.53: (2017/10/22) minor inconsequential change to match change in master (removed an unnecessary statement)
10+
// - v0.54: (2018/01/22) fix for addition of ImFontAtlas::TexUvscale member
11+
12+
// Todo/Bugs:
13+
// - Font size has lots of waste.
14+
// - FreeType's memory allocator is not overridden.
15+
16+
#include "imgui_freetype.h"
17+
#include "imgui_internal.h" // ImMin,ImMax,ImFontAtlasBuild*,
18+
#include <stdint.h>
19+
#include <ft2build.h>
20+
#include FT_FREETYPE_H
21+
#include FT_GLYPH_H
22+
#include FT_SYNTHESIS_H
23+
24+
#ifdef _MSC_VER
25+
#pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
26+
#endif
27+
28+
#if defined(__GNUC__)
29+
#pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used
30+
#endif
31+
32+
namespace
33+
{
34+
// Glyph metrics:
35+
// --------------
36+
//
37+
// xmin xmax
38+
// | |
39+
// |<-------- width -------->|
40+
// | |
41+
// | +-------------------------+----------------- ymax
42+
// | | ggggggggg ggggg | ^ ^
43+
// | | g:::::::::ggg::::g | | |
44+
// | | g:::::::::::::::::g | | |
45+
// | | g::::::ggggg::::::gg | | |
46+
// | | g:::::g g:::::g | | |
47+
// offsetX -|-------->| g:::::g g:::::g | offsetY |
48+
// | | g:::::g g:::::g | | |
49+
// | | g::::::g g:::::g | | |
50+
// | | g:::::::ggggg:::::g | | |
51+
// | | g::::::::::::::::g | | height
52+
// | | gg::::::::::::::g | | |
53+
// baseline ---*---------|---- gggggggg::::::g-----*-------- |
54+
// / | | g:::::g | |
55+
// origin | | gggggg g:::::g | |
56+
// | | g:::::gg gg:::::g | |
57+
// | | g::::::ggg:::::::g | |
58+
// | | gg:::::::::::::g | |
59+
// | | ggg::::::ggg | |
60+
// | | gggggg | v
61+
// | +-------------------------+----------------- ymin
62+
// | |
63+
// |------------- advanceX ----------->|
64+
65+
/// A structure that describe a glyph.
66+
struct GlyphInfo
67+
{
68+
float Width; // Glyph's width in pixels.
69+
float Height; // Glyph's height in pixels.
70+
float OffsetX; // The distance from the origin ("pen position") to the left of the glyph.
71+
float OffsetY; // The distance from the origin to the top of the glyph. This is usually a value < 0.
72+
float AdvanceX; // The distance from the origin to the origin of the next glyph. This is usually a value > 0.
73+
};
74+
75+
// Font parameters and metrics.
76+
struct FontInfo
77+
{
78+
uint32_t PixelHeight; // Size this font was generated with.
79+
float Ascender; // The pixel extents above the baseline in pixels (typically positive).
80+
float Descender; // The extents below the baseline in pixels (typically negative).
81+
float LineSpacing; // The baseline-to-baseline distance. Note that it usually is larger than the sum of the ascender and descender taken as absolute values. There is also no guarantee that no glyphs extend above or below subsequent baselines when using this distance. Think of it as a value the designer of the font finds appropriate.
82+
float LineGap; // The spacing in pixels between one row's descent and the next row's ascent.
83+
float MaxAdvanceWidth; // This field gives the maximum horizontal cursor advance for all glyphs in the font.
84+
};
85+
86+
// FreeType glyph rasterizer.
87+
// NB: No ctor/dtor, explicitly call Init()/Shutdown()
88+
struct FreeTypeFont
89+
{
90+
bool Init(const ImFontConfig& cfg, unsigned int extra_user_flags); // Initialize from an external data buffer. Doesn't copy data, and you must ensure it stays valid up to this object lifetime.
91+
void Shutdown();
92+
void SetPixelHeight(int pixel_height); // Change font pixel size. All following calls to RasterizeGlyph() will use this size
93+
94+
bool CalcGlyphInfo(uint32_t codepoint, GlyphInfo& glyph_info, FT_Glyph& ft_glyph, FT_BitmapGlyph& ft_bitmap);
95+
void BlitGlyph(FT_BitmapGlyph ft_bitmap, uint8_t* dst, uint32_t dst_pitch, unsigned char* multiply_table = NULL);
96+
97+
// [Internals]
98+
FontInfo Info; // Font descriptor of the current font.
99+
unsigned int UserFlags; // = ImFontConfig::RasterizerFlags
100+
FT_Library FreetypeLibrary;
101+
FT_Face FreetypeFace;
102+
FT_Int32 FreetypeLoadFlags;
103+
};
104+
105+
// From SDL_ttf: Handy routines for converting from fixed point
106+
#define FT_CEIL(X) (((X + 63) & -64) / 64)
107+
108+
bool FreeTypeFont::Init(const ImFontConfig& cfg, unsigned int extra_user_flags)
109+
{
110+
// FIXME: substitute allocator
111+
FT_Error error = FT_Init_FreeType(&FreetypeLibrary);
112+
if (error != 0)
113+
return false;
114+
error = FT_New_Memory_Face(FreetypeLibrary, (uint8_t*)cfg.FontData, (uint32_t)cfg.FontDataSize, (uint32_t)cfg.FontNo, &FreetypeFace);
115+
if (error != 0)
116+
return false;
117+
error = FT_Select_Charmap(FreetypeFace, FT_ENCODING_UNICODE);
118+
if (error != 0)
119+
return false;
120+
121+
memset(&Info, 0, sizeof(Info));
122+
SetPixelHeight((uint32_t)cfg.SizePixels);
123+
124+
// Convert to freetype flags (nb: Bold and Oblique are processed separately)
125+
UserFlags = cfg.RasterizerFlags | extra_user_flags;
126+
FreetypeLoadFlags = FT_LOAD_NO_BITMAP;
127+
if (UserFlags & ImGuiFreeType::NoHinting) FreetypeLoadFlags |= FT_LOAD_NO_HINTING;
128+
if (UserFlags & ImGuiFreeType::NoAutoHint) FreetypeLoadFlags |= FT_LOAD_NO_AUTOHINT;
129+
if (UserFlags & ImGuiFreeType::ForceAutoHint) FreetypeLoadFlags |= FT_LOAD_FORCE_AUTOHINT;
130+
if (UserFlags & ImGuiFreeType::LightHinting)
131+
FreetypeLoadFlags |= FT_LOAD_TARGET_LIGHT;
132+
else if (UserFlags & ImGuiFreeType::MonoHinting)
133+
FreetypeLoadFlags |= FT_LOAD_TARGET_MONO;
134+
else
135+
FreetypeLoadFlags |= FT_LOAD_TARGET_NORMAL;
136+
137+
return true;
138+
}
139+
140+
void FreeTypeFont::Shutdown()
141+
{
142+
if (FreetypeFace)
143+
{
144+
FT_Done_Face(FreetypeFace);
145+
FreetypeFace = NULL;
146+
FT_Done_FreeType(FreetypeLibrary);
147+
FreetypeLibrary = NULL;
148+
}
149+
}
150+
151+
void FreeTypeFont::SetPixelHeight(int pixel_height)
152+
{
153+
// I'm not sure how to deal with font sizes properly.
154+
// As far as I understand, currently ImGui assumes that the 'pixel_height' is a maximum height of an any given glyph,
155+
// i.e. it's the sum of font's ascender and descender. Seems strange to me.
156+
FT_Size_RequestRec req;
157+
req.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
158+
req.width = 0;
159+
req.height = (uint32_t)pixel_height * 64;
160+
req.horiResolution = 0;
161+
req.vertResolution = 0;
162+
FT_Request_Size(FreetypeFace, &req);
163+
164+
// update font info
165+
FT_Size_Metrics metrics = FreetypeFace->size->metrics;
166+
Info.PixelHeight = (uint32_t)pixel_height;
167+
Info.Ascender = (float)FT_CEIL(metrics.ascender);
168+
Info.Descender = (float)FT_CEIL(metrics.descender);
169+
Info.LineSpacing = (float)FT_CEIL(metrics.height);
170+
Info.LineGap = (float)FT_CEIL(metrics.height - metrics.ascender + metrics.descender);
171+
Info.MaxAdvanceWidth = (float)FT_CEIL(metrics.max_advance);
172+
}
173+
174+
bool FreeTypeFont::CalcGlyphInfo(uint32_t codepoint, GlyphInfo &glyph_info, FT_Glyph& ft_glyph, FT_BitmapGlyph& ft_bitmap)
175+
{
176+
uint32_t glyph_index = FT_Get_Char_Index(FreetypeFace, codepoint);
177+
FT_Error error = FT_Load_Glyph(FreetypeFace, glyph_index, FreetypeLoadFlags);
178+
if (error)
179+
return false;
180+
181+
// Need an outline for this to work
182+
FT_GlyphSlot slot = FreetypeFace->glyph;
183+
IM_ASSERT(slot->format == FT_GLYPH_FORMAT_OUTLINE);
184+
185+
if (UserFlags & ImGuiFreeType::Bold)
186+
FT_GlyphSlot_Embolden(slot);
187+
if (UserFlags & ImGuiFreeType::Oblique)
188+
FT_GlyphSlot_Oblique(slot);
189+
190+
// Retrieve the glyph
191+
error = FT_Get_Glyph(slot, &ft_glyph);
192+
if (error != 0)
193+
return false;
194+
195+
// Rasterize
196+
error = FT_Glyph_To_Bitmap(&ft_glyph, FT_RENDER_MODE_NORMAL, NULL, true);
197+
if (error != 0)
198+
return false;
199+
200+
ft_bitmap = (FT_BitmapGlyph)ft_glyph;
201+
glyph_info.AdvanceX = (float)FT_CEIL(slot->advance.x);
202+
glyph_info.OffsetX = (float)ft_bitmap->left;
203+
glyph_info.OffsetY = -(float)ft_bitmap->top;
204+
glyph_info.Width = (float)ft_bitmap->bitmap.width;
205+
glyph_info.Height = (float)ft_bitmap->bitmap.rows;
206+
207+
return true;
208+
}
209+
210+
void FreeTypeFont::BlitGlyph(FT_BitmapGlyph ft_bitmap, uint8_t* dst, uint32_t dst_pitch, unsigned char* multiply_table)
211+
{
212+
IM_ASSERT(ft_bitmap != NULL);
213+
214+
const uint32_t w = ft_bitmap->bitmap.width;
215+
const uint32_t h = ft_bitmap->bitmap.rows;
216+
const uint8_t* src = ft_bitmap->bitmap.buffer;
217+
const uint32_t src_pitch = ft_bitmap->bitmap.pitch;
218+
219+
if (multiply_table == NULL)
220+
{
221+
for (uint32_t y = 0; y < h; y++, src += src_pitch, dst += dst_pitch)
222+
memcpy(dst, src, w);
223+
}
224+
else
225+
{
226+
for (uint32_t y = 0; y < h; y++, src += src_pitch, dst += dst_pitch)
227+
for (uint32_t x = 0; x < w; x++)
228+
dst[x] = multiply_table[src[x]];
229+
}
230+
}
231+
}
232+
233+
#define STBRP_ASSERT(x) IM_ASSERT(x)
234+
#define STBRP_STATIC
235+
#define STB_RECT_PACK_IMPLEMENTATION
236+
#include "stb_rect_pack.h"
237+
238+
bool ImGuiFreeType::BuildFontAtlas(ImFontAtlas* atlas, unsigned int extra_flags)
239+
{
240+
IM_ASSERT(atlas->ConfigData.Size > 0);
241+
IM_ASSERT(atlas->TexGlyphPadding == 1); // Not supported
242+
243+
ImFontAtlasBuildRegisterDefaultCustomRects(atlas);
244+
245+
atlas->TexID = NULL;
246+
atlas->TexWidth = atlas->TexHeight = 0;
247+
atlas->TexUvScale = ImVec2(0.0f, 0.0f);
248+
atlas->TexUvWhitePixel = ImVec2(0.0f, 0.0f);
249+
atlas->ClearTexData();
250+
251+
ImVector<FreeTypeFont> fonts;
252+
fonts.resize(atlas->ConfigData.Size);
253+
254+
ImVec2 max_glyph_size(1.0f, 1.0f);
255+
256+
// Count glyphs/ranges, initialize font
257+
int total_glyphs_count = 0;
258+
int total_ranges_count = 0;
259+
for (int input_i = 0; input_i < atlas->ConfigData.Size; input_i++)
260+
{
261+
ImFontConfig& cfg = atlas->ConfigData[input_i];
262+
FreeTypeFont& font_face = fonts[input_i];
263+
IM_ASSERT(cfg.DstFont && (!cfg.DstFont->IsLoaded() || cfg.DstFont->ContainerAtlas == atlas));
264+
265+
if (!font_face.Init(cfg, extra_flags))
266+
return false;
267+
268+
max_glyph_size.x = ImMax(max_glyph_size.x, font_face.Info.MaxAdvanceWidth);
269+
max_glyph_size.y = ImMax(max_glyph_size.y, font_face.Info.Ascender - font_face.Info.Descender);
270+
271+
if (!cfg.GlyphRanges)
272+
cfg.GlyphRanges = atlas->GetGlyphRangesDefault();
273+
for (const ImWchar* in_range = cfg.GlyphRanges; in_range[0] && in_range[ 1 ]; in_range += 2, total_ranges_count++)
274+
total_glyphs_count += (in_range[1] - in_range[0]) + 1;
275+
}
276+
277+
// We need a width for the skyline algorithm. Using a dumb heuristic here to decide of width. User can override TexDesiredWidth and TexGlyphPadding if they wish.
278+
// Width doesn't really matter much, but some API/GPU have texture size limitations and increasing width can decrease height.
279+
atlas->TexWidth = (atlas->TexDesiredWidth > 0) ? atlas->TexDesiredWidth : (total_glyphs_count > 4000) ? 4096 : (total_glyphs_count > 2000) ? 2048 : (total_glyphs_count > 1000) ? 1024 : 512;
280+
281+
// We don't do the original first pass to determine texture height, but just rough estimate.
282+
// Looks ugly inaccurate and excessive, but AFAIK with FreeType we actually need to render glyphs to get exact sizes.
283+
// Alternatively, we could just render all glyphs into a big shadow buffer, get their sizes, do the rectangle packing and just copy back from the
284+
// shadow buffer to the texture buffer. Will give us an accurate texture height, but eat a lot of temp memory. Probably no one will notice.)
285+
const int total_rects = total_glyphs_count + atlas->CustomRects.size();
286+
float min_rects_per_row = ceilf((atlas->TexWidth / (max_glyph_size.x + 1.0f)));
287+
float min_rects_per_column = ceilf(total_rects / min_rects_per_row);
288+
atlas->TexHeight = (int)(min_rects_per_column * (max_glyph_size.y + 1.0f));
289+
290+
// Create texture
291+
atlas->TexHeight = ImUpperPowerOfTwo(atlas->TexHeight);
292+
atlas->TexUvScale = ImVec2(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight);
293+
atlas->TexPixelsAlpha8 = (unsigned char*)ImGui::MemAlloc(atlas->TexWidth * atlas->TexHeight);
294+
memset(atlas->TexPixelsAlpha8, 0, atlas->TexWidth * atlas->TexHeight);
295+
296+
// Start packing
297+
ImVector<stbrp_node> pack_nodes;
298+
pack_nodes.resize(total_rects);
299+
stbrp_context context;
300+
stbrp_init_target(&context, atlas->TexWidth, atlas->TexHeight, pack_nodes.Data, total_rects);
301+
302+
// Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values).
303+
ImFontAtlasBuildPackCustomRects(atlas, &context);
304+
305+
// Render characters, setup ImFont and glyphs for runtime
306+
for (int input_i = 0; input_i < atlas->ConfigData.Size; input_i++)
307+
{
308+
ImFontConfig& cfg = atlas->ConfigData[input_i];
309+
FreeTypeFont& font_face = fonts[input_i];
310+
ImFont* dst_font = cfg.DstFont;
311+
312+
const float ascent = font_face.Info.Ascender;
313+
const float descent = font_face.Info.Descender;
314+
ImFontAtlasBuildSetupFont(atlas, dst_font, &cfg, ascent, descent);
315+
const float off_x = cfg.GlyphOffset.x;
316+
const float off_y = cfg.GlyphOffset.y + (float)(int)(dst_font->Ascent + 0.5f);
317+
318+
bool multiply_enabled = (cfg.RasterizerMultiply != 1.0f);
319+
unsigned char multiply_table[256];
320+
if (multiply_enabled)
321+
ImFontAtlasBuildMultiplyCalcLookupTable(multiply_table, cfg.RasterizerMultiply);
322+
323+
for (const ImWchar* in_range = cfg.GlyphRanges; in_range[0] && in_range[1]; in_range += 2)
324+
{
325+
for (uint32_t codepoint = in_range[0]; codepoint <= in_range[1]; ++codepoint)
326+
{
327+
if (cfg.MergeMode && dst_font->FindGlyph((unsigned short)codepoint))
328+
continue;
329+
330+
FT_Glyph ft_glyph = NULL;
331+
FT_BitmapGlyph ft_glyph_bitmap = NULL; // NB: will point to bitmap within FT_Glyph
332+
GlyphInfo glyph_info;
333+
if (!font_face.CalcGlyphInfo(codepoint, glyph_info, ft_glyph, ft_glyph_bitmap))
334+
continue;
335+
336+
// Pack rectangle
337+
stbrp_rect rect;
338+
rect.w = (uint16_t)glyph_info.Width + 1; // Account for texture filtering
339+
rect.h = (uint16_t)glyph_info.Height + 1;
340+
stbrp_pack_rects(&context, &rect, 1);
341+
342+
// Copy rasterized pixels to main texture
343+
uint8_t* blit_dst = atlas->TexPixelsAlpha8 + rect.y * atlas->TexWidth + rect.x;
344+
font_face.BlitGlyph(ft_glyph_bitmap, blit_dst, atlas->TexWidth, multiply_enabled ? multiply_table : NULL);
345+
FT_Done_Glyph(ft_glyph);
346+
347+
// Register glyph
348+
dst_font->AddGlyph((ImWchar)codepoint,
349+
glyph_info.OffsetX + off_x,
350+
glyph_info.OffsetY + off_y,
351+
glyph_info.OffsetX + off_x + glyph_info.Width,
352+
glyph_info.OffsetY + off_y + glyph_info.Height,
353+
rect.x / (float)atlas->TexWidth,
354+
rect.y / (float)atlas->TexHeight,
355+
(rect.x + glyph_info.Width) / (float)atlas->TexWidth,
356+
(rect.y + glyph_info.Height) / (float)atlas->TexHeight,
357+
glyph_info.AdvanceX);
358+
}
359+
}
360+
}
361+
362+
// Cleanup
363+
for (int n = 0; n < fonts.Size; n++)
364+
fonts[n].Shutdown();
365+
366+
ImFontAtlasBuildFinish(atlas);
367+
368+
return true;
369+
}

0 commit comments

Comments
 (0)