13#if defined(MAYAFLUX_PLATFORM_LINUX)
15std::optional<std::string>
find_font(std::string_view family, std::string_view style)
17 FcConfig* cfg = FcInitLoadConfigAndFonts();
23 FcPattern* pat = FcNameParse(
24 reinterpret_cast<const FcChar8*
>(std::string(family).c_str()));
26 FcPatternAddString(pat, FC_STYLE,
27 reinterpret_cast<const FcChar8*
>(std::string(style).c_str()));
29 FcConfigSubstitute(cfg, pat, FcMatchPattern);
30 FcDefaultSubstitute(pat);
33 FcPattern* match = FcFontMatch(cfg, pat, &result);
35 std::optional<std::string> out;
37 FcChar8* file_path {};
38 if (FcPatternGetString(match, FC_FILE, 0, &file_path) == FcResultMatch) {
39 out =
reinterpret_cast<const char*
>(file_path);
42 FcPatternDestroy(match);
47 FcPatternDestroy(pat);
52#elif defined(MAYAFLUX_PLATFORM_MACOS)
54namespace fs = std::filesystem;
56std::optional<std::string>
find_font(std::string_view family, std::string_view style)
58 std::string query = std::string(family);
60 query +=
":style=" + std::string(style);
63 const std::string
cmd =
"fc-match --format='%{file}' '" + query +
"' 2>/dev/null";
67 if (!result.empty() && fs::exists(result)) {
72 const std::vector<fs::path> search_dirs = {
75 "/System/Library/Fonts",
76 "/System/Library/Fonts/Supplemental"
79 std::string lower_family(family);
80 std::ranges::transform(lower_family,
81 lower_family.begin(), ::tolower);
83 for (
const auto& dir : search_dirs) {
84 if (!fs::exists(dir)) {
87 for (
const auto& entry : fs::directory_iterator(dir)) {
88 const std::string ext = entry.path().extension().string();
89 if (ext !=
".ttf" && ext !=
".otf") {
92 std::string lower_name = entry.path().filename().string();
93 std::ranges::transform(lower_name,
94 lower_name.begin(), ::tolower);
95 if (lower_name.find(lower_family) != std::string::npos) {
97 return entry.path().string();
106#elif defined(MAYAFLUX_PLATFORM_WINDOWS)
113namespace fs = std::filesystem;
115std::optional<std::string>
find_font(std::string_view family, std::string_view )
117 char windir[MAX_PATH] {};
118 GetWindowsDirectoryA(windir, MAX_PATH);
119 const fs::path fonts_dir = fs::path(windir) /
"Fonts";
121 if (!fs::exists(fonts_dir)) {
126 std::string lower_family(family);
127 std::ranges::transform(lower_family, lower_family.begin(),
128 [](
unsigned char c) { return std::tolower(c); });
129 std::erase_if(lower_family,
130 [](
unsigned char c) {
return std::isspace(c); });
132 for (
const auto& entry : fs::directory_iterator(fonts_dir)) {
133 const std::string ext = entry.path().extension().string();
134 if (ext !=
".ttf" && ext !=
".otf") {
137 std::string lower_name = entry.path().filename().string();
138 std::ranges::transform(lower_name, lower_name.begin(),
139 [](
unsigned char c) { return std::tolower(c); });
140 if (lower_name.find(lower_family) != std::string::npos) {
142 return entry.path().string();
MAYAFLUX_API std::optional< std::string > find_font(std::string_view family, std::string_view style={})
Locate a system font file by family name and optional style.