1
0
mirror of synced 2024-09-24 03:28:24 +02:00

feat: Enumerate fonts on Linux using Fontconfig when available (#1821)

### Problem description

The fonts list on Linux does not show all system fonts, and does not
show font names at all.

### Implementation description

Use Fontconfig to make the list less bad if Fontconfig headers are
available.

### Additional things

I like fonts.

---------

Co-authored-by: Nik <werwolv98@gmail.com>
This commit is contained in:
Colin Snover 2024-07-21 13:28:37 -05:00 committed by GitHub
parent 09b6c2ab5b
commit c1c51e0baf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 58 additions and 2 deletions

View File

@ -30,6 +30,7 @@ AppDir:
- libbz2-1.0:amd64 - libbz2-1.0:amd64
- libcap2:amd64 - libcap2:amd64
- libdbus-1-3:amd64 - libdbus-1-3:amd64
- libfontconfig1:amd64
- libgpg-error0:amd64 - libgpg-error0:amd64
- liblzma5:amd64 - liblzma5:amd64
- libnss-mdns:amd64 - libnss-mdns:amd64

View File

@ -13,6 +13,7 @@ RUN pacman -S --needed --noconfirm \
glfw-x11 \ glfw-x11 \
file \ file \
mbedtls \ mbedtls \
fontconfig \
freetype2 \ freetype2 \
curl \ curl \
dbus \ dbus \

2
dist/Arch/PKGBUILD vendored
View File

@ -8,7 +8,7 @@ pkgdesc="A Hex Editor for Reverse Engineers, Programmers and people who value th
arch=("x86_64") arch=("x86_64")
url="https://github.com/WerWolv/ImHex" url="https://github.com/WerWolv/ImHex"
license=('GPL2') 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) makedepends=(git)
provides=(imhex) provides=(imhex)
conflicts=(imhex) conflicts=(imhex)

View File

@ -4,7 +4,7 @@ Section: editors
Priority: optional Priority: optional
Architecture: amd64 Architecture: amd64
License: GNU GPL-2 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 <hey@werwolv.net> Maintainer: WerWolv <hey@werwolv.net>
Description: ImHex Hex Editor Description: ImHex Hex Editor
A Hex Editor for Reverse Engineers, Programmers and A Hex Editor for Reverse Engineers, Programmers and

View File

@ -5,6 +5,7 @@ pacman -S $@ --needed \
gcc \ gcc \
lld \ lld \
glfw \ glfw \
fontconfig \
file \ file \
mbedtls \ mbedtls \
freetype2 \ freetype2 \

View File

@ -18,6 +18,7 @@ apt install -y \
libglm-dev \ libglm-dev \
libmagic-dev \ libmagic-dev \
libmbedtls-dev \ libmbedtls-dev \
libfontconfig-dev \
libfreetype-dev \ libfreetype-dev \
libdbus-1-dev \ libdbus-1-dev \
libcurl4-gnutls-dev \ libcurl4-gnutls-dev \

View File

@ -4,6 +4,7 @@ dnf install -y \
cmake \ cmake \
dbus-devel \ dbus-devel \
file-devel \ file-devel \
fontconfig-devel \
freetype-devel \ freetype-devel \
libcurl-devel \ libcurl-devel \
gcc-c++ \ gcc-c++ \

View File

@ -5,6 +5,7 @@ zypper install \
ninja \ ninja \
gcc12 \ gcc12 \
gcc12-c++ \ gcc12-c++ \
fontconfig-devel \
freetype2-devel \ freetype2-devel \
libcurl-devel \ libcurl-devel \
dbus-1-devel \ dbus-1-devel \

1
dist/rpm/imhex.spec vendored
View File

@ -16,6 +16,7 @@ BuildRequires: cmake
BuildRequires: desktop-file-utils BuildRequires: desktop-file-utils
BuildRequires: dbus-devel BuildRequires: dbus-devel
BuildRequires: file-devel BuildRequires: file-devel
BuildRequires: fontconfig-devel
BuildRequires: freetype-devel BuildRequires: freetype-devel
BuildRequires: fmt-devel BuildRequires: fmt-devel
BuildRequires: gcc-c++ BuildRequires: gcc-c++

View File

@ -63,6 +63,15 @@ if (WIN32)
target_link_libraries(main PRIVATE usp10 wsock32 ws2_32 Dwmapi.lib Winmm.lib) target_link_libraries(main PRIVATE usp10 wsock32 ws2_32 Dwmapi.lib Winmm.lib)
else () else ()
target_link_libraries(main PRIVATE pthread) 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 IMHEX_HAS_FONTCONFIG)
endif ()
endif ()
endif () endif ()
precompileHeaders(main ${CMAKE_CURRENT_SOURCE_DIR}/include) precompileHeaders(main ${CMAKE_CURRENT_SOURCE_DIR}/include)

View File

@ -22,6 +22,10 @@
#include <string.h> #include <string.h>
#include <ranges> #include <ranges>
#if defined(IMHEX_HAS_FONTCONFIG)
#include <fontconfig/fontconfig.h>
#endif
namespace hex { namespace hex {
bool isFileInPath(const std::fs::path &filename) { bool isFileInPath(const std::fs::path &filename) {
@ -48,7 +52,43 @@ namespace hex {
} // Hopefully one of these commands is installed } // Hopefully one of these commands is installed
} }
#if defined(IMHEX_HAS_FONTCONFIG)
static bool enumerateFontConfig() {
if (!FcInit())
return false;
ON_SCOPE_EXIT { FcFini(); };
auto fonts = FcConfigGetFonts(nullptr, FcSetSystem);
if (fonts == nullptr)
return false;
for (int 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<const char *>(fullName), reinterpret_cast<const char *>(file));
}
return true;
}
#endif
void enumerateFonts() { void enumerateFonts() {
#if defined(IMHEX_HAS_FONTCONFIG)
if (enumerateFontConfig())
return;
#endif
const std::array FontDirectories = { const std::array FontDirectories = {
"/usr/share/fonts", "/usr/share/fonts",
"/usr/local/share/fonts", "/usr/local/share/fonts",