Skip to content

Commit

Permalink
Use ImGuiFreeType for better font rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
GPSnoopy committed May 9, 2021
1 parent ec71f48 commit efb71ef
Show file tree
Hide file tree
Showing 7 changed files with 828 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ if (UNIX)
endif ()

find_package(Boost REQUIRED COMPONENTS exception program_options)
find_package(freetype CONFIG REQUIRED)
find_package(glfw3 CONFIG REQUIRED)
find_package(glm CONFIG REQUIRED)
find_package(imgui CONFIG REQUIRED)
Expand Down
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ set(src_files_assets
)

set(src_files_imgui
ImGui/imgui_freetype.cpp
ImGui/imgui_freetype.h
ImGui/imgui_impl_glfw.cpp
ImGui/imgui_impl_glfw.h
ImGui/imgui_impl_vulkan.cpp
Expand Down Expand Up @@ -166,4 +168,4 @@ add_dependencies(${exe_name} Assets)
set_target_properties(${exe_name} PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
target_include_directories(${exe_name} PRIVATE . ${Boost_INCLUDE_DIRS} ${glfw3_INCLUDE_DIRS} ${glm_INCLUDE_DIRS} ${STB_INCLUDE_DIRS} ${Vulkan_INCLUDE_DIRS})
target_link_directories(${exe_name} PRIVATE ${Vulkan_LIBRARY})
target_link_libraries(${exe_name} PRIVATE ${Boost_LIBRARIES} glfw glm::glm imgui::imgui tinyobjloader::tinyobjloader ${Vulkan_LIBRARIES} ${extra_libs})
target_link_libraries(${exe_name} PRIVATE ${Boost_LIBRARIES} freetype glfw glm::glm imgui::imgui tinyobjloader::tinyobjloader ${Vulkan_LIBRARIES} ${extra_libs})
769 changes: 769 additions & 0 deletions src/ImGui/imgui_freetype.cpp

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions src/ImGui/imgui_freetype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// dear imgui: FreeType font builder (used as a replacement for the stb_truetype builder)
// (headers)

#pragma once

#include "imgui.h" // IMGUI_API

// Forward declarations
struct ImFontAtlas;
struct ImFontBuilderIO;

// Hinting greatly impacts visuals (and glyph sizes).
// - By default, hinting is enabled and the font's native hinter is preferred over the auto-hinter.
// - When disabled, FreeType generates blurrier glyphs, more or less matches the stb_truetype.h
// - The Default hinting mode usually looks good, but may distort glyphs in an unusual way.
// - The Light hinting mode generates fuzzier glyphs but better matches Microsoft's rasterizer.
// You can set those flags globaly in ImFontAtlas::FontBuilderFlags
// You can set those flags on a per font basis in ImFontConfig::FontBuilderFlags
enum ImGuiFreeTypeBuilderFlags
{
ImGuiFreeTypeBuilderFlags_NoHinting = 1 << 0, // Disable hinting. This generally generates 'blurrier' bitmap glyphs when the glyph are rendered in any of the anti-aliased modes.
ImGuiFreeTypeBuilderFlags_NoAutoHint = 1 << 1, // Disable auto-hinter.
ImGuiFreeTypeBuilderFlags_ForceAutoHint = 1 << 2, // Indicates that the auto-hinter is preferred over the font's native hinter.
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.
ImGuiFreeTypeBuilderFlags_MonoHinting = 1 << 4, // Strong hinting algorithm that should only be used for monochrome output.
ImGuiFreeTypeBuilderFlags_Bold = 1 << 5, // Styling: Should we artificially embolden the font?
ImGuiFreeTypeBuilderFlags_Oblique = 1 << 6, // Styling: Should we slant the font, emulating italic style?
ImGuiFreeTypeBuilderFlags_Monochrome = 1 << 7, // Disable anti-aliasing. Combine this with MonoHinting for best results!
ImGuiFreeTypeBuilderFlags_LoadColor = 1 << 8, // Enable FreeType color-layered glyphs
ImGuiFreeTypeBuilderFlags_Bitmap = 1 << 9 // Enable FreeType bitmap glyphs
};

namespace ImGuiFreeType
{
// This is automatically assigned when using '#define IMGUI_ENABLE_FREETYPE'.
// If you need to dynamically select between multiple builders:
// - you can manually assign this builder with 'atlas->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()'
// - prefer deep-copying this into your own ImFontBuilderIO instance if you use hot-reloading that messes up static data.
IMGUI_API const ImFontBuilderIO* GetBuilderForFreeType();

// Override allocators. By default ImGuiFreeType will use IM_ALLOC()/IM_FREE()
// However, as FreeType does lots of allocations we provide a way for the user to redirect it to a separate memory heap if desired.
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);

// Obsolete names (will be removed soon)
// Prefer using '#define IMGUI_ENABLE_FREETYPE'
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
static inline bool BuildFontAtlas(ImFontAtlas* atlas, unsigned int flags = 0) { atlas->FontBuilderIO = GetBuilderForFreeType(); atlas->FontBuilderFlags = flags; return atlas->Build(); }
#endif
}
4 changes: 3 additions & 1 deletion src/UserInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Vulkan/Window.hpp"

#include "imgui.h"
#include "ImGui/imgui_freetype.h"
#include "ImGui/imgui_impl_glfw.h"
#include "ImGui/imgui_impl_vulkan.h"

Expand Down Expand Up @@ -87,7 +88,8 @@ UserInterface::UserInterface(
ImGui::StyleColorsDark();
ImGui::GetStyle().ScaleAllSizes(scaleFactor);

// Upload ImGui fonts
// Upload ImGui fonts (use ImGuiFreeType for better font rendering, see https://github.com/ocornut/imgui/tree/master/misc/freetype).
io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType();
if (!io.Fonts->AddFontFromFileTTF("../assets/fonts/Cousine-Regular.ttf", 13 * scaleFactor))
{
Throw(std::runtime_error("failed to load ImGui font"));
Expand Down
1 change: 1 addition & 0 deletions vcpkg_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ git checkout 2021.04.30
boost-exception:x64-linux \
boost-program-options:x64-linux \
boost-stacktrace:x64-linux \
freetype:x64-linux \
glfw3:x64-linux \
glm:x64-linux \
imgui:x64-linux \
Expand Down
1 change: 1 addition & 0 deletions vcpkg_windows.bat
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ vcpkg.exe install ^
boost-exception:x64-windows-static ^
boost-program-options:x64-windows-static ^
boost-stacktrace:x64-windows-static ^
freetype:x64-windows-static ^
glfw3:x64-windows-static ^
glm:x64-windows-static ^
imgui:x64-windows-static ^
Expand Down

0 comments on commit efb71ef

Please sign in to comment.