From 7cc4314b2ed9ac231366d77ea9121f060cc251f6 Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Thu, 18 Jul 2024 17:08:28 -0500 Subject: [PATCH 1/4] feat: Enumerate fonts on Linux using Fontconfig when available --- main/gui/CMakeLists.txt | 9 +++++++ main/gui/source/window/linux_window.cpp | 36 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/main/gui/CMakeLists.txt b/main/gui/CMakeLists.txt index c5a6e0362dc62..8effe48a32d9e 100644 --- a/main/gui/CMakeLists.txt +++ b/main/gui/CMakeLists.txt @@ -63,6 +63,15 @@ if (WIN32) target_link_libraries(main PRIVATE usp10 wsock32 ws2_32 Dwmapi.lib Winmm.lib) else () target_link_libraries(main PRIVATE pthread) + + if (NOT APPLE) + find_package(Fontconfig) + if (TARGET Fontconfig::Fontconfig) + message(STATUS "Using Fontconfig version: ${Fontconfig_VERSION}") + target_link_libraries(main PRIVATE Fontconfig::Fontconfig) + target_compile_definitions(main PRIVATE USE_FONTCONFIG) + endif () + endif () endif () precompileHeaders(main ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/main/gui/source/window/linux_window.cpp b/main/gui/source/window/linux_window.cpp index 60fbbdd8e7e3b..5d30e3d3ca0e1 100644 --- a/main/gui/source/window/linux_window.cpp +++ b/main/gui/source/window/linux_window.cpp @@ -22,6 +22,10 @@ #include #include + #ifdef USE_FONTCONFIG + #include + #endif + namespace hex { bool isFileInPath(const std::fs::path &filename) { @@ -48,7 +52,39 @@ namespace hex { } // Hopefully one of these commands is installed } +#ifdef USE_FONTCONFIG + static bool enumerateFontConfig() { + if (!FcInit()) + return false; + + auto fonts = FcConfigGetFonts(nullptr, FcSetSystem); + if (!fonts) + return false; + + for (auto i = 0; i < fonts->nfont; ++i) { + auto font = fonts->fonts[i]; + FcChar8 *file, *fullName; + if (FcPatternGetString(font, FC_FILE, 0, &file) != FcResultMatch) { + continue; + } + if (FcPatternGetString(font, FC_FULLNAME, 0, &fullName) != FcResultMatch + && FcPatternGetString(font, FC_FAMILY, 0, &fullName) != FcResultMatch) { + continue; + } + registerFont(reinterpret_cast(fullName), reinterpret_cast(file)); + } + + FcFini(); + return true; + } +#endif + void enumerateFonts() { +#ifdef USE_FONTCONFIG + if (enumerateFontConfig()) + return; +#endif + const std::array FontDirectories = { "/usr/share/fonts", "/usr/local/share/fonts", From c32f007a133a31017bc812f0da5cde24394f4c58 Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Thu, 18 Jul 2024 18:09:06 -0500 Subject: [PATCH 2/4] impr: Add fontconfig dependency to dist scripts --- dist/AppImageBuilder.yml | 1 + dist/Arch/Dockerfile | 1 + dist/Arch/PKGBUILD | 2 +- dist/DEBIAN/control.in | 2 +- dist/get_deps_archlinux.sh | 1 + dist/get_deps_debian.sh | 1 + dist/get_deps_fedora.sh | 1 + dist/get_deps_tumbleweed.sh | 1 + dist/rpm/imhex.spec | 1 + 9 files changed, 9 insertions(+), 2 deletions(-) diff --git a/dist/AppImageBuilder.yml b/dist/AppImageBuilder.yml index 4f9eb6a7bd773..2497e0d4c7bdb 100644 --- a/dist/AppImageBuilder.yml +++ b/dist/AppImageBuilder.yml @@ -30,6 +30,7 @@ AppDir: - libbz2-1.0:amd64 - libcap2:amd64 - libdbus-1-3:amd64 + - libfontconfig1:amd64 - libgpg-error0:amd64 - liblzma5:amd64 - libnss-mdns:amd64 diff --git a/dist/Arch/Dockerfile b/dist/Arch/Dockerfile index 16aa72acaf57c..dbfeb3840f80d 100644 --- a/dist/Arch/Dockerfile +++ b/dist/Arch/Dockerfile @@ -13,6 +13,7 @@ RUN pacman -S --needed --noconfirm \ glfw-x11 \ file \ mbedtls \ + fontconfig \ freetype2 \ curl \ dbus \ diff --git a/dist/Arch/PKGBUILD b/dist/Arch/PKGBUILD index 10240701c8e1a..59bc3a7ec2cc4 100644 --- a/dist/Arch/PKGBUILD +++ b/dist/Arch/PKGBUILD @@ -8,7 +8,7 @@ pkgdesc="A Hex Editor for Reverse Engineers, Programmers and people who value th arch=("x86_64") url="https://github.com/WerWolv/ImHex" license=('GPL2') -depends=(glfw mbedtls freetype2 libglvnd dbus gtk3 curl fmt yara nlohmann-json zlib bzip2 xz zstd) +depends=(glfw mbedtls fontconfig freetype2 libglvnd dbus gtk3 curl fmt yara nlohmann-json zlib bzip2 xz zstd) makedepends=(git) provides=(imhex) conflicts=(imhex) diff --git a/dist/DEBIAN/control.in b/dist/DEBIAN/control.in index 263e7ed539be7..96c43fc1373ba 100644 --- a/dist/DEBIAN/control.in +++ b/dist/DEBIAN/control.in @@ -4,7 +4,7 @@ Section: editors Priority: optional Architecture: amd64 License: GNU GPL-2 -Depends: libglfw3 | libglfw3-wayland, libmagic1, libmbedtls14, libfreetype6, libopengl0, libdbus-1-3, xdg-desktop-portal +Depends: libfontconfig1, libglfw3 | libglfw3-wayland, libmagic1, libmbedtls14, libfreetype6, libopengl0, libdbus-1-3, xdg-desktop-portal Maintainer: WerWolv Description: ImHex Hex Editor A Hex Editor for Reverse Engineers, Programmers and diff --git a/dist/get_deps_archlinux.sh b/dist/get_deps_archlinux.sh index 15377e87689bb..8d5f3f4a6390a 100755 --- a/dist/get_deps_archlinux.sh +++ b/dist/get_deps_archlinux.sh @@ -5,6 +5,7 @@ pacman -S $@ --needed \ gcc \ lld \ glfw \ + fontconfig \ file \ mbedtls \ freetype2 \ diff --git a/dist/get_deps_debian.sh b/dist/get_deps_debian.sh index 286b42aa17183..a0449dbad9d20 100755 --- a/dist/get_deps_debian.sh +++ b/dist/get_deps_debian.sh @@ -18,6 +18,7 @@ apt install -y \ libglm-dev \ libmagic-dev \ libmbedtls-dev \ + libfontconfig-dev \ libfreetype-dev \ libdbus-1-dev \ libcurl4-gnutls-dev \ diff --git a/dist/get_deps_fedora.sh b/dist/get_deps_fedora.sh index 683ddd2d4849f..c5f006e7be148 100755 --- a/dist/get_deps_fedora.sh +++ b/dist/get_deps_fedora.sh @@ -4,6 +4,7 @@ dnf install -y \ cmake \ dbus-devel \ file-devel \ + fontconfig-devel \ freetype-devel \ libcurl-devel \ gcc-c++ \ diff --git a/dist/get_deps_tumbleweed.sh b/dist/get_deps_tumbleweed.sh index cbb8813ea51dd..61bd4159a9b9c 100755 --- a/dist/get_deps_tumbleweed.sh +++ b/dist/get_deps_tumbleweed.sh @@ -5,6 +5,7 @@ zypper install \ ninja \ gcc12 \ gcc12-c++ \ + fontconfig-devel \ freetype2-devel \ libcurl-devel \ dbus-1-devel \ diff --git a/dist/rpm/imhex.spec b/dist/rpm/imhex.spec index 5e56b62000daa..17ce0ad973714 100644 --- a/dist/rpm/imhex.spec +++ b/dist/rpm/imhex.spec @@ -16,6 +16,7 @@ BuildRequires: cmake BuildRequires: desktop-file-utils BuildRequires: dbus-devel BuildRequires: file-devel +BuildRequires: fontconfig-devel BuildRequires: freetype-devel BuildRequires: fmt-devel BuildRequires: gcc-c++ From d1d7ecbae75532f294b205fca2a9b719f3d8f6c4 Mon Sep 17 00:00:00 2001 From: Nik Date: Sun, 21 Jul 2024 20:22:42 +0200 Subject: [PATCH 3/4] Style fixes --- main/gui/CMakeLists.txt | 2 +- main/gui/source/window/linux_window.cpp | 60 +++++++++++++------------ 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/main/gui/CMakeLists.txt b/main/gui/CMakeLists.txt index 8effe48a32d9e..d2f8a835f3eb0 100644 --- a/main/gui/CMakeLists.txt +++ b/main/gui/CMakeLists.txt @@ -69,7 +69,7 @@ else () if (TARGET Fontconfig::Fontconfig) message(STATUS "Using Fontconfig version: ${Fontconfig_VERSION}") target_link_libraries(main PRIVATE Fontconfig::Fontconfig) - target_compile_definitions(main PRIVATE USE_FONTCONFIG) + target_compile_definitions(main PRIVATE IMHEX_HAS_FONTCONFIG) endif () endif () endif () diff --git a/main/gui/source/window/linux_window.cpp b/main/gui/source/window/linux_window.cpp index 5d30e3d3ca0e1..d28326062b10e 100644 --- a/main/gui/source/window/linux_window.cpp +++ b/main/gui/source/window/linux_window.cpp @@ -22,8 +22,8 @@ #include #include - #ifdef USE_FONTCONFIG - #include + #if defined(IMHEX_HAS_FONTCONFIG) + #include #endif namespace hex { @@ -52,38 +52,42 @@ namespace hex { } // Hopefully one of these commands is installed } -#ifdef USE_FONTCONFIG - static bool enumerateFontConfig() { - if (!FcInit()) - return false; + #if defined(IMHEX_HAS_FONTCONFIG) + static bool enumerateFontConfig() { + if (!FcInit()) + return false; - auto fonts = FcConfigGetFonts(nullptr, FcSetSystem); - if (!fonts) - return false; + ON_SCOPE_EXIT { FcFini(); }; - for (auto i = 0; i < fonts->nfont; ++i) { - auto font = fonts->fonts[i]; - FcChar8 *file, *fullName; - if (FcPatternGetString(font, FC_FILE, 0, &file) != FcResultMatch) { - continue; - } - if (FcPatternGetString(font, FC_FULLNAME, 0, &fullName) != FcResultMatch - && FcPatternGetString(font, FC_FAMILY, 0, &fullName) != FcResultMatch) { - continue; + auto fonts = FcConfigGetFonts(nullptr, FcSetSystem); + if (fonts == nullptr) + return false; + + for (u32 i = 0; i < fonts->nfont; ++i) { + auto font = fonts->fonts[i]; + FcChar8 *file, *fullName; + + if (FcPatternGetString(font, FC_FILE, 0, &file) != FcResultMatch) { + continue; + } + + if (FcPatternGetString(font, FC_FULLNAME, 0, &fullName) != FcResultMatch + && FcPatternGetString(font, FC_FAMILY, 0, &fullName) != FcResultMatch) { + continue; + } + + registerFont(reinterpret_cast(fullName), reinterpret_cast(file)); } - registerFont(reinterpret_cast(fullName), reinterpret_cast(file)); - } - FcFini(); - return true; - } -#endif + return true; + } + #endif void enumerateFonts() { -#ifdef USE_FONTCONFIG - if (enumerateFontConfig()) - return; -#endif + #if defined(IMHEX_HAS_FONTCONFIG) + if (enumerateFontConfig()) + return; + #endif const std::array FontDirectories = { "/usr/share/fonts", From e2c57d011a934ae0f6cc4e707d2f1527bea070ac Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 21 Jul 2024 20:28:05 +0200 Subject: [PATCH 4/4] Build fix --- main/gui/source/window/linux_window.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/gui/source/window/linux_window.cpp b/main/gui/source/window/linux_window.cpp index d28326062b10e..93c54911e182e 100644 --- a/main/gui/source/window/linux_window.cpp +++ b/main/gui/source/window/linux_window.cpp @@ -63,7 +63,7 @@ namespace hex { if (fonts == nullptr) return false; - for (u32 i = 0; i < fonts->nfont; ++i) { + for (int i = 0; i < fonts->nfont; ++i) { auto font = fonts->fonts[i]; FcChar8 *file, *fullName;