|
1 | | -// dear imgui: wrapper to use FreeType (instead of stb_truetype) |
2 | | -// Get latest version at https://github.com/ocornut/imgui/tree/master/misc/freetype |
3 | | -// Original code by @Vuhdo (Aleksei Skriabin), maintained by @ocornut |
| 1 | +// dear imgui: FreeType font builder (used as a replacement for the stb_truetype builder) |
| 2 | +// (headers) |
4 | 3 |
|
5 | 4 | #pragma once |
| 5 | +#include "imgui.h" // IMGUI_API |
| 6 | +#ifndef IMGUI_DISABLE |
6 | 7 |
|
7 | | -#include "imgui.h" // IMGUI_API, ImFontAtlas |
| 8 | +// Usage: |
| 9 | +// - Add '#define IMGUI_ENABLE_FREETYPE' in your imconfig to automatically enable support |
| 10 | +// for imgui_freetype in imgui. It is equivalent to selecting the default loader with: |
| 11 | +// io.Fonts->SetFontLoader(ImGuiFreeType::GetFontLoader()) |
| 12 | + |
| 13 | +// Optional support for OpenType SVG fonts: |
| 14 | +// - Add '#define IMGUI_ENABLE_FREETYPE_PLUTOSVG' to use plutosvg (not provided). See #7927. |
| 15 | +// - Add '#define IMGUI_ENABLE_FREETYPE_LUNASVG' to use lunasvg (not provided). See #6591. |
| 16 | + |
| 17 | +// Forward declarations |
| 18 | +struct ImFontAtlas; |
| 19 | +struct ImFontLoader; |
| 20 | + |
| 21 | +// Hinting greatly impacts visuals (and glyph sizes). |
| 22 | +// - By default, hinting is enabled and the font's native hinter is preferred over the auto-hinter. |
| 23 | +// - When disabled, FreeType generates blurrier glyphs, more or less matches the stb_truetype.h |
| 24 | +// - The Default hinting mode usually looks good, but may distort glyphs in an unusual way. |
| 25 | +// - The Light hinting mode generates fuzzier glyphs but better matches Microsoft's rasterizer. |
| 26 | +// You can set those flags globally in ImFontAtlas::FontLoaderFlags |
| 27 | +// You can set those flags on a per font basis in ImFontConfig::FontLoaderFlags |
| 28 | +typedef unsigned int ImGuiFreeTypeLoaderFlags; |
| 29 | +enum ImGuiFreeTypeLoaderFlags_ |
| 30 | +{ |
| 31 | + ImGuiFreeTypeLoaderFlags_NoHinting = 1 << 0, // Disable hinting. This generally generates 'blurrier' bitmap glyphs when the glyph are rendered in any of the anti-aliased modes. |
| 32 | + ImGuiFreeTypeLoaderFlags_NoAutoHint = 1 << 1, // Disable auto-hinter. |
| 33 | + ImGuiFreeTypeLoaderFlags_ForceAutoHint = 1 << 2, // Indicates that the auto-hinter is preferred over the font's native hinter. |
| 34 | + ImGuiFreeTypeLoaderFlags_LightHinting = 1 << 3, // A lighter hinting algorithm for gray-level modes. Many generated glyphs are fuzzier but better resemble their original shape. This is achieved by snapping glyphs to the pixel grid only vertically (Y-axis), as is done by Microsoft's ClearType and Adobe's proprietary font renderer. This preserves inter-glyph spacing in horizontal text. |
| 35 | + ImGuiFreeTypeLoaderFlags_MonoHinting = 1 << 4, // Strong hinting algorithm that should only be used for monochrome output. |
| 36 | + ImGuiFreeTypeLoaderFlags_Bold = 1 << 5, // Styling: Should we artificially embolden the font? |
| 37 | + ImGuiFreeTypeLoaderFlags_Oblique = 1 << 6, // Styling: Should we slant the font, emulating italic style? |
| 38 | + ImGuiFreeTypeLoaderFlags_Monochrome = 1 << 7, // Disable anti-aliasing. Combine this with MonoHinting for best results! |
| 39 | + ImGuiFreeTypeLoaderFlags_LoadColor = 1 << 8, // Enable FreeType color-layered glyphs |
| 40 | + ImGuiFreeTypeLoaderFlags_Bitmap = 1 << 9, // Enable FreeType bitmap glyphs |
| 41 | + |
| 42 | +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
| 43 | + ImGuiFreeTypeBuilderFlags_NoHinting = ImGuiFreeTypeLoaderFlags_NoHinting, |
| 44 | + ImGuiFreeTypeBuilderFlags_NoAutoHint = ImGuiFreeTypeLoaderFlags_NoAutoHint, |
| 45 | + ImGuiFreeTypeBuilderFlags_ForceAutoHint = ImGuiFreeTypeLoaderFlags_ForceAutoHint, |
| 46 | + ImGuiFreeTypeBuilderFlags_LightHinting = ImGuiFreeTypeLoaderFlags_LightHinting, |
| 47 | + ImGuiFreeTypeBuilderFlags_MonoHinting = ImGuiFreeTypeLoaderFlags_MonoHinting, |
| 48 | + ImGuiFreeTypeBuilderFlags_Bold = ImGuiFreeTypeLoaderFlags_Bold, |
| 49 | + ImGuiFreeTypeBuilderFlags_Oblique = ImGuiFreeTypeLoaderFlags_Oblique, |
| 50 | + ImGuiFreeTypeBuilderFlags_Monochrome = ImGuiFreeTypeLoaderFlags_Monochrome, |
| 51 | + ImGuiFreeTypeBuilderFlags_LoadColor = ImGuiFreeTypeLoaderFlags_LoadColor, |
| 52 | + ImGuiFreeTypeBuilderFlags_Bitmap = ImGuiFreeTypeLoaderFlags_Bitmap, |
| 53 | +#endif |
| 54 | +}; |
| 55 | + |
| 56 | +// Obsolete names (will be removed) |
| 57 | +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
| 58 | +typedef ImGuiFreeTypeLoaderFlags_ ImGuiFreeTypeBuilderFlags_; |
| 59 | +#endif |
8 | 60 |
|
9 | 61 | namespace ImGuiFreeType |
10 | 62 | { |
11 | | - // Hinting greatly impacts visuals (and glyph sizes). |
12 | | - // When disabled, FreeType generates blurrier glyphs, more or less matches the stb's output. |
13 | | - // The Default hinting mode usually looks good, but may distort glyphs in an unusual way. |
14 | | - // The Light hinting mode generates fuzzier glyphs but better matches Microsoft's rasterizer. |
15 | | - |
16 | | - // You can set those flags on a per font basis in ImFontConfig::RasterizerFlags. |
17 | | - // Use the 'extra_flags' parameter of BuildFontAtlas() to force a flag on all your fonts. |
18 | | - enum RasterizerFlags |
19 | | - { |
20 | | - // By default, hinting is enabled and the font's native hinter is preferred over the auto-hinter. |
21 | | - NoHinting = 1 << 0, // Disable hinting. This generally generates 'blurrier' bitmap glyphs when the glyph are rendered in any of the anti-aliased modes. |
22 | | - NoAutoHint = 1 << 1, // Disable auto-hinter. |
23 | | - ForceAutoHint = 1 << 2, // Indicates that the auto-hinter is preferred over the font's native hinter. |
24 | | - LightHinting = 1 << 3, // A lighter hinting algorithm for gray-level modes. Many generated glyphs are fuzzier but better resemble their original shape. This is achieved by snapping glyphs to the pixel grid only vertically (Y-axis), as is done by Microsoft's ClearType and Adobe's proprietary font renderer. This preserves inter-glyph spacing in horizontal text. |
25 | | - MonoHinting = 1 << 4, // Strong hinting algorithm that should only be used for monochrome output. |
26 | | - Bold = 1 << 5, // Styling: Should we artificially embolden the font? |
27 | | - Oblique = 1 << 6, // Styling: Should we slant the font, emulating italic style? |
28 | | - Monochrome = 1 << 7 // Disable anti-aliasing. Combine this with MonoHinting for best results! |
29 | | - }; |
30 | | - |
31 | | - IMGUI_API bool BuildFontAtlas(ImFontAtlas* atlas, unsigned int extra_flags = 0); |
32 | | - |
33 | | - // By default ImGuiFreeType will use IM_ALLOC()/IM_FREE(). |
34 | | - // However, as FreeType does lots of allocations we provide a way for the user to redirect it to a separate memory heap if desired: |
35 | | - IMGUI_API void SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data = NULL); |
| 63 | + // This is automatically assigned when using '#define IMGUI_ENABLE_FREETYPE'. |
| 64 | + // If you need to dynamically select between multiple builders: |
| 65 | + // - you can manually assign this builder with 'atlas->SetFontLoader(ImGuiFreeType::GetFontLoader())' |
| 66 | + // - prefer deep-copying this into your own ImFontLoader instance if you use hot-reloading that messes up static data. |
| 67 | + IMGUI_API const ImFontLoader* GetFontLoader(); |
| 68 | + |
| 69 | + // Override allocators. By default ImGuiFreeType will use IM_ALLOC()/IM_FREE() |
| 70 | + // However, as FreeType does lots of allocations we provide a way for the user to redirect it to a separate memory heap if desired. |
| 71 | + IMGUI_API void SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data = nullptr); |
| 72 | + |
| 73 | + // Display UI to edit ImFontAtlas::FontLoaderFlags (shared) or ImFontConfig::FontLoaderFlags (single source) |
| 74 | + IMGUI_API bool DebugEditFontLoaderFlags(ImGuiFreeTypeLoaderFlags* p_font_loader_flags); |
| 75 | + |
| 76 | + // Obsolete names (will be removed) |
| 77 | +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
| 78 | + //IMGUI_API const ImFontBuilderIO* GetBuilderForFreeType(); // Renamed/changed in 1.92. Change 'io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()' to 'io.Fonts->SetFontLoader(ImGuiFreeType::GetFontLoader())' if you need runtime selection. |
| 79 | + //static inline bool BuildFontAtlas(ImFontAtlas* atlas, unsigned int flags = 0) { atlas->FontBuilderIO = GetBuilderForFreeType(); atlas->FontLoaderFlags = flags; return atlas->Build(); } // Prefer using '#define IMGUI_ENABLE_FREETYPE' |
| 80 | +#endif |
36 | 81 | } |
| 82 | + |
| 83 | +#endif // #ifndef IMGUI_DISABLE |
0 commit comments