Skip to content

Commit

Permalink
Work on CMake / freetype
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Jan 2, 2024
1 parent be83017 commit 7d1b921
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
13 changes: 10 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,23 @@ option(HELLOIMGUI_USE_SDL_DIRECTX12 "Build HelloImGui for SDL2+DirectX12" OFF)
#------------------------------------------------------------------------------
# Options / Freetype
#------------------------------------------------------------------------------
option(HELLOIMGUI_USE_FREETYPE "Use freetype for text rendering" OFF)
option(HELLOIMGUI_USE_FREETYPE "Use freetype for text rendering" ON)


#------------------------------------------------------------------------------
# Automatic download of Glfw3 and SDL2 (provided as a convenience)
# Automatic download of Glfw3, SDL2, and Freetype (provided as a convenience)
# (disabled by default on Linux, which prefers to use the system libraries,
# enabled by default on other platforms)
# Note: SDL and Glfw3 will be downloaded if:
# Note:
# - SDL and Glfw3 will be downloaded if:
# - HELLOIMGUI_DOWNLOAD_GLFW_IF_NEEDED or HELLOIMGUI_DOWNLOAD_SDL_IF_NEEDED is ON
# - HELLOIMGUI_USE_SDL_XXXX or HELLOIMGUI_USE_GLFW_XXXX is ON
# - find_package(glfw3 or SDL2) fails. If a system library is found, or a user library provided
# in a path added to CMAKE_PREFIX_PATH, it will be used instead
# - In the case of Glfw3, if a target named glfw is already defined, it will be used instead
# - Freetype will be downloaded if:
# - HELLOIMGUI_DOWNLOAD_FREETYPE is ON, HELLOIMGUI_USE_FREETYPE is ON, and find_package(freetype) fails
# - Or of HELLOIMGUI_FREETYPE_STATIC is ON
#------------------------------------------------------------------------------
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(autodownload_default OFF)
Expand All @@ -104,6 +108,9 @@ else()
endif()
option(HELLOIMGUI_DOWNLOAD_GLFW_IF_NEEDED "Download and build GLFW if needed" ${autodownload_default})
option(HELLOIMGUI_DOWNLOAD_SDL_IF_NEEDED "Download and build GLFW if needed" ${autodownload_default})
option(HELLOIMGUI_DOWNLOAD_FREETYPE_IF_NEEDED "Download and build Freetype if needed" ${autodownload_default})

option(HELLOIMGUI_FREETYPE_STATIC "Force static linking of freetype (only used for python bindings)" OFF)


#------------------------------------------------------------------------------
Expand Down
58 changes: 43 additions & 15 deletions hello_imgui_cmake/hello_imgui_build_lib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,50 @@ function(_him_add_freetype_to_imgui)

# Note: also change add_imgui.cmake in bundle!

include(FetchContent)
#
# 1. Build or find freetype (if downloaded, make sure it is static)
#
set(download_freetype OFF)
if (HELLOIMGUI_DOWNLOAD_FREETYPE_IF_NEEDED)
find_package(freetype QUIET)
if (NOT freetype_FOUND)
set(download_freetype ON)
endif()
endif()
if(HELLOIMGUI_FREETYPE_STATIC)
set(download_freetype ON)
endif()

# Get freetype
include(FetchContent)
FetchContent_Declare(
freetype
GIT_REPOSITORY https://gitlab.freedesktop.org/freetype/freetype.git
GIT_TAG VER-2-13-2
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(freetype)
target_link_libraries(imgui PUBLIC freetype)
if (download_freetype)
message(STATUS "HelloImGui: downloading and building freetype")

set(backup_shared_lib ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)

# find_package(Freetype REQUIRED)
# target_link_libraries(imgui PUBLIC Freetype::Freetype)
include(FetchContent)
FetchContent_Declare(
freetype
GIT_REPOSITORY https://gitlab.freedesktop.org/freetype/freetype.git
GIT_TAG VER-2-13-2
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(freetype)
set(freetype_linked_library freetype)

set(BUILD_SHARED_LIBS ${backup_shared_lib} CACHE BOOL "" FORCE)
else()
find_package(Freetype REQUIRED)
set(freetype_linked_library Freetype::Freetype)
endif()

target_link_libraries(imgui PUBLIC ${freetype_linked_library})

#
# 2. Build lunasvg (static)
#
# Fetch and build lunasvg
set(backup_shared_lib ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
include(FetchContent)
FetchContent_Declare(lunasvg
GIT_REPOSITORY https://github.com/sammycage/lunasvg
Expand All @@ -117,13 +143,15 @@ function(_him_add_freetype_to_imgui)
)
FetchContent_MakeAvailable(lunasvg)
target_link_libraries(imgui PUBLIC lunasvg)
set(BUILD_SHARED_LIBS ${backup_shared_lib} CACHE BOOL "" FORCE)

# Add freetype support to imgui
#
# 3. Add freetype and LunaSvg support to imgui
# with support for wchar32 (for emojis, and other unicode characters)
target_sources(imgui PRIVATE
${HELLOIMGUI_IMGUI_SOURCE_DIR}/misc/freetype/imgui_freetype.cpp
${HELLOIMGUI_IMGUI_SOURCE_DIR}/misc/freetype/imgui_freetype.h)
target_compile_definitions(imgui PUBLIC IMGUI_ENABLE_FREETYPE IMGUI_ENABLE_FREETYPE_LUNASVG)

target_compile_definitions(imgui PUBLIC IMGUI_USE_WCHAR32)
endfunction()

Expand Down

0 comments on commit 7d1b921

Please sign in to comment.