Browse Source

Fonts: if IMGUI_ENABLE_FREETYPE, use library by default for font rasterization
Also renamed IMGUI_DISABLE_STB_TRUETYPE to IMGUI_ENABLE_STB_TRUETYPE

Louis Schnellbach 4 years ago
parent
commit
9417acc20f
5 changed files with 46 additions and 26 deletions
  1. 3 0
      docs/CHANGELOG.txt
  2. 8 2
      imconfig.h
  3. 1 0
      imgui.h
  4. 29 24
      imgui_draw.cpp
  5. 5 0
      imgui_internal.h

+ 3 - 0
docs/CHANGELOG.txt

@@ -142,6 +142,9 @@ Other Changes:
 - Style: Changed default style.WindowRounding value to 0.0f (matches default for multi-viewports).
 - Style: Changed default style.WindowRounding value to 0.0f (matches default for multi-viewports).
 - Style: Reduced the size of the resizing grip, made alpha less prominent.
 - Style: Reduced the size of the resizing grip, made alpha less prominent.
 - Style: Classic: Increase the default alpha value of WindowBg to be closer to other styles.
 - Style: Classic: Increase the default alpha value of WindowBg to be closer to other styles.
+- Fonts: Ease Freetype integration with IMGUI_ENABLE_FREETYPE: When enabled, it Will use the library to rasterize font by default.
+  Also renamed IMGUI_DISABLE_STB_TRUETYPE to IMGUI_ENABLE_STB_TRUETYPE, and making it the default rasterizer
+  if IMGUI_ENABLE_FREETYPE is not defined. [@Xipiryon]
 - Demo: Clarify usage of right-aligned items in Demo>Layout>Widgets Width.
 - Demo: Clarify usage of right-aligned items in Demo>Layout>Widgets Width.
 - Backends: OpenGL3: Use glGetString(GL_VERSION) query instead of glGetIntegerv(GL_MAJOR_VERSION, ...)
 - Backends: OpenGL3: Use glGetString(GL_VERSION) query instead of glGetIntegerv(GL_MAJOR_VERSION, ...)
   when the later returns zero (e.g. Desktop GL 2.x). (#3530) [@xndcn]
   when the later returns zero (e.g. Desktop GL 2.x). (#3530) [@xndcn]

+ 8 - 2
imconfig.h

@@ -56,13 +56,19 @@
 // By default the embedded implementations are declared static and not available outside of imgui cpp files.
 // By default the embedded implementations are declared static and not available outside of imgui cpp files.
 //#define IMGUI_STB_TRUETYPE_FILENAME   "my_folder/stb_truetype.h"
 //#define IMGUI_STB_TRUETYPE_FILENAME   "my_folder/stb_truetype.h"
 //#define IMGUI_STB_RECT_PACK_FILENAME  "my_folder/stb_rect_pack.h"
 //#define IMGUI_STB_RECT_PACK_FILENAME  "my_folder/stb_rect_pack.h"
-//#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
-//#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
 
 
 //---- Use stb_printf's faster implementation of vsnprintf instead of the one from libc (unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined)
 //---- Use stb_printf's faster implementation of vsnprintf instead of the one from libc (unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined)
 // Requires 'stb_sprintf.h' to be available in the include path. Compatibility checks of arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by STB sprintf.
 // Requires 'stb_sprintf.h' to be available in the include path. Compatibility checks of arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by STB sprintf.
 // #define IMGUI_USE_STB_SPRINTF
 // #define IMGUI_USE_STB_SPRINTF
 
 
+//---- Use Freetype to build and rasterize the font atlas (instead of stb_truetype which is embedded by default in Dear ImGui)
+// Requires Freetype headers to be available in the include path. Requires program to be compiled with 'misc/freetype/imgui_freetype.cpp' (in this repository) + the Freetype library (not provided)
+//#define IMGUI_ENABLE_FREETYPE
+
+//---- Use stb_truetype to build and rasterize the font atlas (this is enabled by default)
+// The only purpose of this this define is if you want force compilation of the stb_truetype backend ALONG with the Freetype backend.
+//#define IMGUI_ENABLE_STB_TRUETYPE
+
 //---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4.
 //---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4.
 // This will be inlined as part of ImVec2 and ImVec4 class declarations.
 // This will be inlined as part of ImVec2 and ImVec4 class declarations.
 /*
 /*

+ 1 - 0
imgui.h

@@ -2634,6 +2634,7 @@ struct ImFontAtlas
     ImTextureID                 TexID;              // User data to refer to the texture once it has been uploaded to user's graphic systems. It is passed back to you during rendering via the ImDrawCmd structure.
     ImTextureID                 TexID;              // User data to refer to the texture once it has been uploaded to user's graphic systems. It is passed back to you during rendering via the ImDrawCmd structure.
     int                         TexDesiredWidth;    // Texture width desired by user before Build(). Must be a power-of-two. If have many glyphs your graphics API have texture size restrictions you may want to increase texture width to decrease height.
     int                         TexDesiredWidth;    // Texture width desired by user before Build(). Must be a power-of-two. If have many glyphs your graphics API have texture size restrictions you may want to increase texture width to decrease height.
     int                         TexGlyphPadding;    // Padding between glyphs within texture in pixels. Defaults to 1. If your rendering method doesn't rely on bilinear filtering you may set this to 0.
     int                         TexGlyphPadding;    // Padding between glyphs within texture in pixels. Defaults to 1. If your rendering method doesn't rely on bilinear filtering you may set this to 0.
+    const char*                 Builder;            // Select font builder/rasterizer. Default to "stb_truetype". Set to "freetype" to enable Freetype (default if IMGUI_ENABLE_FREETYPE is defined).
 
 
     // [Internal]
     // [Internal]
     // NB: Access texture data via GetTexData*() calls! Which will setup a default font for you.
     // NB: Access texture data via GetTexData*() calls! Which will setup a default font for you.

+ 29 - 24
imgui_draw.cpp

@@ -32,7 +32,11 @@ Index of this file:
 #ifndef IMGUI_DEFINE_MATH_OPERATORS
 #ifndef IMGUI_DEFINE_MATH_OPERATORS
 #define IMGUI_DEFINE_MATH_OPERATORS
 #define IMGUI_DEFINE_MATH_OPERATORS
 #endif
 #endif
+
 #include "imgui_internal.h"
 #include "imgui_internal.h"
+#ifdef IMGUI_ENABLE_FREETYPE
+#include "misc/freetype/imgui_freetype.h"
+#endif
 
 
 #include <stdio.h>      // vsnprintf, sscanf, printf
 #include <stdio.h>      // vsnprintf, sscanf, printf
 #if !defined(alloca)
 #if !defined(alloca)
@@ -131,6 +135,7 @@ namespace IMGUI_STB_NAMESPACE
 #endif
 #endif
 #endif
 #endif
 
 
+#ifdef  IMGUI_ENABLE_STB_TRUETYPE
 #ifndef STB_TRUETYPE_IMPLEMENTATION                         // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds)
 #ifndef STB_TRUETYPE_IMPLEMENTATION                         // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds)
 #ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
 #ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
 #define STBTT_malloc(x,u)   ((void)(u), IM_ALLOC(x))
 #define STBTT_malloc(x,u)   ((void)(u), IM_ALLOC(x))
@@ -153,6 +158,7 @@ namespace IMGUI_STB_NAMESPACE
 #include "imstb_truetype.h"
 #include "imstb_truetype.h"
 #endif
 #endif
 #endif
 #endif
+#endif
 
 
 #if defined(__GNUC__)
 #if defined(__GNUC__)
 #pragma GCC diagnostic pop
 #pragma GCC diagnostic pop
@@ -1710,25 +1716,13 @@ void ImGui::ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int ve
 
 
 ImFontConfig::ImFontConfig()
 ImFontConfig::ImFontConfig()
 {
 {
-    FontData = NULL;
-    FontDataSize = 0;
+    memset(this, 0, sizeof(*this));
     FontDataOwnedByAtlas = true;
     FontDataOwnedByAtlas = true;
-    FontNo = 0;
-    SizePixels = 0.0f;
     OversampleH = 3; // FIXME: 2 may be a better default?
     OversampleH = 3; // FIXME: 2 may be a better default?
     OversampleV = 1;
     OversampleV = 1;
-    PixelSnapH = false;
-    GlyphExtraSpacing = ImVec2(0.0f, 0.0f);
-    GlyphOffset = ImVec2(0.0f, 0.0f);
-    GlyphRanges = NULL;
-    GlyphMinAdvanceX = 0.0f;
     GlyphMaxAdvanceX = FLT_MAX;
     GlyphMaxAdvanceX = FLT_MAX;
-    MergeMode = false;
-    RasterizerFlags = 0x00;
     RasterizerMultiply = 1.0f;
     RasterizerMultiply = 1.0f;
     EllipsisChar = (ImWchar)-1;
     EllipsisChar = (ImWchar)-1;
-    memset(Name, 0, sizeof(Name));
-    DstFont = NULL;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -1785,18 +1779,17 @@ static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3
 
 
 ImFontAtlas::ImFontAtlas()
 ImFontAtlas::ImFontAtlas()
 {
 {
-    Locked = false;
-    Flags = ImFontAtlasFlags_None;
-    TexID = (ImTextureID)NULL;
-    TexDesiredWidth = 0;
+    memset(this, 0, sizeof(*this));
     TexGlyphPadding = 1;
     TexGlyphPadding = 1;
-
-    TexPixelsAlpha8 = NULL;
-    TexPixelsRGBA32 = NULL;
-    TexWidth = TexHeight = 0;
-    TexUvScale = ImVec2(0.0f, 0.0f);
-    TexUvWhitePixel = ImVec2(0.0f, 0.0f);
     PackIdMouseCursors = PackIdLines = -1;
     PackIdMouseCursors = PackIdLines = -1;
+
+#ifdef IMGUI_ENABLE_FREETYPE
+    Builder = "freetype";
+#else
+#   ifdef IMGUI_ENABLE_STB_TRUETYPE
+    Builder = "stb_truetype";
+#   endif
+#endif
 }
 }
 
 
 ImFontAtlas::~ImFontAtlas()
 ImFontAtlas::~ImFontAtlas()
@@ -2081,7 +2074,17 @@ bool ImFontAtlas::GetMouseCursorTexData(ImGuiMouseCursor cursor_type, ImVec2* ou
 bool    ImFontAtlas::Build()
 bool    ImFontAtlas::Build()
 {
 {
     IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
     IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
-    return ImFontAtlasBuildWithStbTruetype(this);
+#ifdef IMGUI_ENABLE_FREETYPE
+    if (strcmp(Builder, "freetype") == 0)
+        return ImGuiFreeType::BuildFontAtlas(this, 0);
+#endif
+    // Not doing "#else" here, since we could have both
+    // IMGUI_ENABLE_FREETYPE and IMGUI_ENABLE_STB_TRUETYPE defined.
+#ifdef IMGUI_ENABLE_STB_TRUETYPE
+    if (strcmp(Builder, "stb_truetype") == 0)
+        return ImFontAtlasBuildWithStbTruetype(this);
+#endif
+    return false;
 }
 }
 
 
 void    ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_brighten_factor)
 void    ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_brighten_factor)
@@ -2101,6 +2104,7 @@ void    ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsig
             data[i] = table[data[i]];
             data[i] = table[data[i]];
 }
 }
 
 
+#ifdef IMGUI_ENABLE_STB_TRUETYPE
 // Temporary data for one source font (multiple source fonts can be merged into one destination ImFont)
 // Temporary data for one source font (multiple source fonts can be merged into one destination ImFont)
 // (C++03 doesn't allow instancing ImVector<> with function-local types so we declare the type here.)
 // (C++03 doesn't allow instancing ImVector<> with function-local types so we declare the type here.)
 struct ImFontBuildSrcData
 struct ImFontBuildSrcData
@@ -2390,6 +2394,7 @@ bool    ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)
     ImFontAtlasBuildFinish(atlas);
     ImFontAtlasBuildFinish(atlas);
     return true;
     return true;
 }
 }
+#endif
 
 
 void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent)
 void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent)
 {
 {

+ 5 - 0
imgui_internal.h

@@ -83,6 +83,11 @@ Index of this file:
 #error Use IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS
 #error Use IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS
 #endif
 #endif
 
 
+// Enable stb_truetype by default unless Freetype is enable. User can compile both by defining both IMGUI_ENABLE_FREETYPE and IMGUI_ENABLE_STB_TRUETYPE
+#ifndef IMGUI_ENABLE_FREETYPE
+#define IMGUI_ENABLE_STB_TRUETYPE
+#endif
+
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 // [SECTION] Forward declarations
 // [SECTION] Forward declarations
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------