1
- // Wrapper to use Freetype (instead of stb_truetype) for Dear ImGui
1
+ // Wrapper to use FreeType (instead of stb_truetype) for Dear ImGui
2
2
// Get latest version at https://github.com/ocornut/imgui/tree/master/misc/freetype
3
3
// Original code by @Vuhdo (Aleksei Skriabin). Improvements by @mikesart. Maintained by @ocornut
4
4
20
20
// TODO:
21
21
// - Output texture has excessive resolution (lots of vertical waste).
22
22
// - FreeType's memory allocator is not overridden.
23
- // - cfg.OversampleH, OversampleV are ignored (but perhaps not so necessary with this rasterizer).
23
+ // - cfg.OversampleH, OversampleV are not supported (but perhaps not so necessary with this rasterizer).
24
24
25
25
#include " imgui_freetype.h"
26
26
#include " imgui_internal.h" // ImMin,ImMax,ImFontAtlasBuild*,
27
27
#include < stdint.h>
28
28
#include < ft2build.h>
29
- #include FT_FREETYPE_H
30
- #include FT_GLYPH_H
31
- #include FT_SYNTHESIS_H
29
+ #include FT_FREETYPE_H // <freetype/freetype.h>
30
+ #include FT_GLYPH_H // <freetype/ftglyph.h>
31
+ #include FT_SYNTHESIS_H // <freetype/ftsynth.h>
32
32
33
33
#ifdef _MSC_VER
34
34
#pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
@@ -96,8 +96,8 @@ namespace
96
96
// NB: No ctor/dtor, explicitly call Init()/Shutdown()
97
97
struct FreeTypeFont
98
98
{
99
- 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.
100
- void Shutdown ();
99
+ bool Create (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.
100
+ void Destroy ();
101
101
void SetPixelHeight (int pixel_height); // Change font pixel size. All following calls to RasterizeGlyph() will use this size
102
102
103
103
bool CalcGlyphInfo (uint32_t codepoint, GlyphInfo& glyph_info, FT_Glyph& ft_glyph, FT_BitmapGlyph& ft_bitmap);
@@ -114,7 +114,7 @@ namespace
114
114
// From SDL_ttf: Handy routines for converting from fixed point
115
115
#define FT_CEIL (X ) (((X + 63 ) & -64 ) / 64 )
116
116
117
- bool FreeTypeFont::Init (const ImFontConfig& cfg, unsigned int extra_user_flags)
117
+ bool FreeTypeFont::Create (const ImFontConfig& cfg, unsigned int extra_user_flags)
118
118
{
119
119
// FIXME: substitute allocator
120
120
FT_Error error = FT_Init_FreeType (&FreetypeLibrary);
@@ -130,7 +130,7 @@ namespace
130
130
memset (&Info, 0 , sizeof (Info));
131
131
SetPixelHeight ((uint32_t )cfg.SizePixels );
132
132
133
- // Convert to freetype flags (nb : Bold and Oblique are processed separately)
133
+ // Convert to FreeType flags (NB : Bold and Oblique are processed separately)
134
134
UserFlags = cfg.RasterizerFlags | extra_user_flags;
135
135
FreetypeLoadFlags = FT_LOAD_NO_BITMAP;
136
136
if (UserFlags & ImGuiFreeType::NoHinting) FreetypeLoadFlags |= FT_LOAD_NO_HINTING;
@@ -146,7 +146,7 @@ namespace
146
146
return true ;
147
147
}
148
148
149
- void FreeTypeFont::Shutdown ()
149
+ void FreeTypeFont::Destroy ()
150
150
{
151
151
if (FreetypeFace)
152
152
{
@@ -162,6 +162,7 @@ namespace
162
162
// I'm not sure how to deal with font sizes properly.
163
163
// As far as I understand, currently ImGui assumes that the 'pixel_height' is a maximum height of an any given glyph,
164
164
// i.e. it's the sum of font's ascender and descender. Seems strange to me.
165
+ // FT_Set_Pixel_Sizes() doesn't seem to get us the same result.
165
166
FT_Size_RequestRec req;
166
167
req.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
167
168
req.width = 0 ;
@@ -180,7 +181,7 @@ namespace
180
181
Info.MaxAdvanceWidth = (float )FT_CEIL (metrics.max_advance );
181
182
}
182
183
183
- bool FreeTypeFont::CalcGlyphInfo (uint32_t codepoint, GlyphInfo & glyph_info, FT_Glyph& ft_glyph, FT_BitmapGlyph& ft_bitmap)
184
+ bool FreeTypeFont::CalcGlyphInfo (uint32_t codepoint, GlyphInfo& glyph_info, FT_Glyph& ft_glyph, FT_BitmapGlyph& ft_bitmap)
184
185
{
185
186
uint32_t glyph_index = FT_Get_Char_Index (FreetypeFace, codepoint);
186
187
if (glyph_index == 0 )
@@ -253,7 +254,8 @@ bool ImGuiFreeType::BuildFontAtlas(ImFontAtlas* atlas, unsigned int extra_flags)
253
254
254
255
ImFontAtlasBuildRegisterDefaultCustomRects (atlas);
255
256
256
- atlas->TexID = NULL ;
257
+ // Clear atlas
258
+ atlas->TexID = (ImTextureID)NULL ;
257
259
atlas->TexWidth = atlas->TexHeight = 0 ;
258
260
atlas->TexUvScale = ImVec2 (0 .0f , 0 .0f );
259
261
atlas->TexUvWhitePixel = ImVec2 (0 .0f , 0 .0f );
@@ -273,7 +275,7 @@ bool ImGuiFreeType::BuildFontAtlas(ImFontAtlas* atlas, unsigned int extra_flags)
273
275
FreeTypeFont& font_face = fonts[input_i];
274
276
IM_ASSERT (cfg.DstFont && (!cfg.DstFont ->IsLoaded () || cfg.DstFont ->ContainerAtlas == atlas));
275
277
276
- if (!font_face.Init (cfg, extra_flags))
278
+ if (!font_face.Create (cfg, extra_flags))
277
279
return false ;
278
280
279
281
max_glyph_size.x = ImMax (max_glyph_size.x , font_face.Info .MaxAdvanceWidth );
@@ -380,7 +382,7 @@ bool ImGuiFreeType::BuildFontAtlas(ImFontAtlas* atlas, unsigned int extra_flags)
380
382
381
383
// Cleanup
382
384
for (int n = 0 ; n < fonts.Size ; n++)
383
- fonts[n].Shutdown ();
385
+ fonts[n].Destroy ();
384
386
385
387
ImFontAtlasBuildFinish (atlas);
386
388
0 commit comments