imgui_freetype.h 5.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // dear imgui: FreeType font builder (used as a replacement for the stb_truetype builder)
  2. // (headers)
  3. #pragma once
  4. #include "imgui.h" // IMGUI_API
  5. #ifndef IMGUI_DISABLE
  6. // Usage:
  7. // - Add '#define IMGUI_ENABLE_FREETYPE' in your imconfig to automatically enable support
  8. // for imgui_freetype in imgui. It is equivalent to selecting the default loader with:
  9. // io.Fonts->SetFontLoader(ImGuiFreeType::GetFontLoader())
  10. // Optional support for OpenType SVG fonts:
  11. // - Add '#define IMGUI_ENABLE_FREETYPE_PLUTOSVG' to use plutosvg (not provided). See #7927.
  12. // - Add '#define IMGUI_ENABLE_FREETYPE_LUNASVG' to use lunasvg (not provided). See #6591.
  13. // Forward declarations
  14. struct ImFontAtlas;
  15. struct ImFontLoader;
  16. // Hinting greatly impacts visuals (and glyph sizes).
  17. // - By default, hinting is enabled and the font's native hinter is preferred over the auto-hinter.
  18. // - When disabled, FreeType generates blurrier glyphs, more or less matches the stb_truetype.h
  19. // - The Default hinting mode usually looks good, but may distort glyphs in an unusual way.
  20. // - The Light hinting mode generates fuzzier glyphs but better matches Microsoft's rasterizer.
  21. // You can set those flags globally in ImFontAtlas::FontLoaderFlags
  22. // You can set those flags on a per font basis in ImFontConfig::FontLoaderFlags
  23. typedef unsigned int ImGuiFreeTypeLoaderFlags;
  24. enum ImGuiFreeTypeLoaderFlags_
  25. {
  26. ImGuiFreeTypeLoaderFlags_NoHinting = 1 << 0, // Disable hinting. This generally generates 'blurrier' bitmap glyphs when the glyph are rendered in any of the anti-aliased modes.
  27. ImGuiFreeTypeLoaderFlags_NoAutoHint = 1 << 1, // Disable auto-hinter.
  28. ImGuiFreeTypeLoaderFlags_ForceAutoHint = 1 << 2, // Indicates that the auto-hinter is preferred over the font's native hinter.
  29. 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.
  30. ImGuiFreeTypeLoaderFlags_MonoHinting = 1 << 4, // Strong hinting algorithm that should only be used for monochrome output.
  31. ImGuiFreeTypeLoaderFlags_Bold = 1 << 5, // Styling: Should we artificially embolden the font?
  32. ImGuiFreeTypeLoaderFlags_Oblique = 1 << 6, // Styling: Should we slant the font, emulating italic style?
  33. ImGuiFreeTypeLoaderFlags_Monochrome = 1 << 7, // Disable anti-aliasing. Combine this with MonoHinting for best results!
  34. ImGuiFreeTypeLoaderFlags_LoadColor = 1 << 8, // Enable FreeType color-layered glyphs
  35. ImGuiFreeTypeLoaderFlags_Bitmap = 1 << 9, // Enable FreeType bitmap glyphs
  36. #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
  37. ImGuiFreeTypeBuilderFlags_NoHinting = ImGuiFreeTypeLoaderFlags_NoHinting,
  38. ImGuiFreeTypeBuilderFlags_NoAutoHint = ImGuiFreeTypeLoaderFlags_NoAutoHint,
  39. ImGuiFreeTypeBuilderFlags_ForceAutoHint = ImGuiFreeTypeLoaderFlags_ForceAutoHint,
  40. ImGuiFreeTypeBuilderFlags_LightHinting = ImGuiFreeTypeLoaderFlags_LightHinting,
  41. ImGuiFreeTypeBuilderFlags_MonoHinting = ImGuiFreeTypeLoaderFlags_MonoHinting,
  42. ImGuiFreeTypeBuilderFlags_Bold = ImGuiFreeTypeLoaderFlags_Bold,
  43. ImGuiFreeTypeBuilderFlags_Oblique = ImGuiFreeTypeLoaderFlags_Oblique,
  44. ImGuiFreeTypeBuilderFlags_Monochrome = ImGuiFreeTypeLoaderFlags_Monochrome,
  45. ImGuiFreeTypeBuilderFlags_LoadColor = ImGuiFreeTypeLoaderFlags_LoadColor,
  46. ImGuiFreeTypeBuilderFlags_Bitmap = ImGuiFreeTypeLoaderFlags_Bitmap,
  47. #endif
  48. };
  49. // Obsolete names (will be removed)
  50. #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
  51. typedef ImGuiFreeTypeLoaderFlags_ ImGuiFreeTypeBuilderFlags_;
  52. #endif
  53. namespace ImGuiFreeType
  54. {
  55. // This is automatically assigned when using '#define IMGUI_ENABLE_FREETYPE'.
  56. // If you need to dynamically select between multiple builders:
  57. // - you can manually assign this builder with 'atlas->SetFontLoader(ImGuiFreeType::GetFontLoader())'
  58. // - prefer deep-copying this into your own ImFontLoader instance if you use hot-reloading that messes up static data.
  59. IMGUI_API const ImFontLoader* GetFontLoader();
  60. // Override allocators. By default ImGuiFreeType will use IM_ALLOC()/IM_FREE()
  61. // However, as FreeType does lots of allocations we provide a way for the user to redirect it to a separate memory heap if desired.
  62. 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);
  63. // Display UI to edit ImFontAtlas::FontLoaderFlags (shared) or ImFontConfig::FontLoaderFlags (single source)
  64. IMGUI_API bool DebugEditFontLoaderFlags(ImGuiFreeTypeLoaderFlags* p_font_loader_flags);
  65. // Obsolete names (will be removed)
  66. #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
  67. //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.
  68. //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'
  69. #endif
  70. }
  71. #endif // #ifndef IMGUI_DISABLE