Skip to content

Commit efb71ef

Browse files
committed
Use ImGuiFreeType for better font rendering.
1 parent ec71f48 commit efb71ef

7 files changed

Lines changed: 828 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ if (UNIX)
4848
endif ()
4949

5050
find_package(Boost REQUIRED COMPONENTS exception program_options)
51+
find_package(freetype CONFIG REQUIRED)
5152
find_package(glfw3 CONFIG REQUIRED)
5253
find_package(glm CONFIG REQUIRED)
5354
find_package(imgui CONFIG REQUIRED)

src/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ set(src_files_assets
2121
)
2222

2323
set(src_files_imgui
24+
ImGui/imgui_freetype.cpp
25+
ImGui/imgui_freetype.h
2426
ImGui/imgui_impl_glfw.cpp
2527
ImGui/imgui_impl_glfw.h
2628
ImGui/imgui_impl_vulkan.cpp
@@ -166,4 +168,4 @@ add_dependencies(${exe_name} Assets)
166168
set_target_properties(${exe_name} PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
167169
target_include_directories(${exe_name} PRIVATE . ${Boost_INCLUDE_DIRS} ${glfw3_INCLUDE_DIRS} ${glm_INCLUDE_DIRS} ${STB_INCLUDE_DIRS} ${Vulkan_INCLUDE_DIRS})
168170
target_link_directories(${exe_name} PRIVATE ${Vulkan_LIBRARY})
169-
target_link_libraries(${exe_name} PRIVATE ${Boost_LIBRARIES} glfw glm::glm imgui::imgui tinyobjloader::tinyobjloader ${Vulkan_LIBRARIES} ${extra_libs})
171+
target_link_libraries(${exe_name} PRIVATE ${Boost_LIBRARIES} freetype glfw glm::glm imgui::imgui tinyobjloader::tinyobjloader ${Vulkan_LIBRARIES} ${extra_libs})

src/ImGui/imgui_freetype.cpp

Lines changed: 769 additions & 0 deletions
Large diffs are not rendered by default.

src/ImGui/imgui_freetype.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// dear imgui: FreeType font builder (used as a replacement for the stb_truetype builder)
2+
// (headers)
3+
4+
#pragma once
5+
6+
#include "imgui.h" // IMGUI_API
7+
8+
// Forward declarations
9+
struct ImFontAtlas;
10+
struct ImFontBuilderIO;
11+
12+
// Hinting greatly impacts visuals (and glyph sizes).
13+
// - By default, hinting is enabled and the font's native hinter is preferred over the auto-hinter.
14+
// - When disabled, FreeType generates blurrier glyphs, more or less matches the stb_truetype.h
15+
// - The Default hinting mode usually looks good, but may distort glyphs in an unusual way.
16+
// - The Light hinting mode generates fuzzier glyphs but better matches Microsoft's rasterizer.
17+
// You can set those flags globaly in ImFontAtlas::FontBuilderFlags
18+
// You can set those flags on a per font basis in ImFontConfig::FontBuilderFlags
19+
enum ImGuiFreeTypeBuilderFlags
20+
{
21+
ImGuiFreeTypeBuilderFlags_NoHinting = 1 << 0, // Disable hinting. This generally generates 'blurrier' bitmap glyphs when the glyph are rendered in any of the anti-aliased modes.
22+
ImGuiFreeTypeBuilderFlags_NoAutoHint = 1 << 1, // Disable auto-hinter.
23+
ImGuiFreeTypeBuilderFlags_ForceAutoHint = 1 << 2, // Indicates that the auto-hinter is preferred over the font's native hinter.
24+
ImGuiFreeTypeBuilderFlags_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+
ImGuiFreeTypeBuilderFlags_MonoHinting = 1 << 4, // Strong hinting algorithm that should only be used for monochrome output.
26+
ImGuiFreeTypeBuilderFlags_Bold = 1 << 5, // Styling: Should we artificially embolden the font?
27+
ImGuiFreeTypeBuilderFlags_Oblique = 1 << 6, // Styling: Should we slant the font, emulating italic style?
28+
ImGuiFreeTypeBuilderFlags_Monochrome = 1 << 7, // Disable anti-aliasing. Combine this with MonoHinting for best results!
29+
ImGuiFreeTypeBuilderFlags_LoadColor = 1 << 8, // Enable FreeType color-layered glyphs
30+
ImGuiFreeTypeBuilderFlags_Bitmap = 1 << 9 // Enable FreeType bitmap glyphs
31+
};
32+
33+
namespace ImGuiFreeType
34+
{
35+
// This is automatically assigned when using '#define IMGUI_ENABLE_FREETYPE'.
36+
// If you need to dynamically select between multiple builders:
37+
// - you can manually assign this builder with 'atlas->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()'
38+
// - prefer deep-copying this into your own ImFontBuilderIO instance if you use hot-reloading that messes up static data.
39+
IMGUI_API const ImFontBuilderIO* GetBuilderForFreeType();
40+
41+
// Override allocators. By default ImGuiFreeType will use IM_ALLOC()/IM_FREE()
42+
// However, as FreeType does lots of allocations we provide a way for the user to redirect it to a separate memory heap if desired.
43+
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);
44+
45+
// Obsolete names (will be removed soon)
46+
// Prefer using '#define IMGUI_ENABLE_FREETYPE'
47+
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
48+
static inline bool BuildFontAtlas(ImFontAtlas* atlas, unsigned int flags = 0) { atlas->FontBuilderIO = GetBuilderForFreeType(); atlas->FontBuilderFlags = flags; return atlas->Build(); }
49+
#endif
50+
}

src/UserInterface.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Vulkan/Window.hpp"
1414

1515
#include "imgui.h"
16+
#include "ImGui/imgui_freetype.h"
1617
#include "ImGui/imgui_impl_glfw.h"
1718
#include "ImGui/imgui_impl_vulkan.h"
1819

@@ -87,7 +88,8 @@ UserInterface::UserInterface(
8788
ImGui::StyleColorsDark();
8889
ImGui::GetStyle().ScaleAllSizes(scaleFactor);
8990

90-
// Upload ImGui fonts
91+
// Upload ImGui fonts (use ImGuiFreeType for better font rendering, see https://github.com/ocornut/imgui/tree/master/misc/freetype).
92+
io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType();
9193
if (!io.Fonts->AddFontFromFileTTF("../assets/fonts/Cousine-Regular.ttf", 13 * scaleFactor))
9294
{
9395
Throw(std::runtime_error("failed to load ImGui font"));

vcpkg_linux.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ git checkout 2021.04.30
1212
boost-exception:x64-linux \
1313
boost-program-options:x64-linux \
1414
boost-stacktrace:x64-linux \
15+
freetype:x64-linux \
1516
glfw3:x64-linux \
1617
glm:x64-linux \
1718
imgui:x64-linux \

vcpkg_windows.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ vcpkg.exe install ^
99
boost-exception:x64-windows-static ^
1010
boost-program-options:x64-windows-static ^
1111
boost-stacktrace:x64-windows-static ^
12+
freetype:x64-windows-static ^
1213
glfw3:x64-windows-static ^
1314
glm:x64-windows-static ^
1415
imgui:x64-windows-static ^

0 commit comments

Comments
 (0)