Fray Engine
Loading...
Searching...
No Matches
system_fonts.hpp
Go to the documentation of this file.
1#ifndef FRAY_SYSTEM_FONTS_HPP
2#define FRAY_SYSTEM_FONTS_HPP
3
4#include <algorithm>
5#include <cctype>
6#include <string>
7#include <vector>
8
9#if defined(__APPLE__)
10#include <CoreFoundation/CoreFoundation.h>
11#include <CoreText/CoreText.h>
12#elif defined(_WIN32)
13#ifndef NOMINMAX
14#define NOMINMAX
15#endif
16#include <windows.h>
17#endif
18
19namespace fray::fonts
20{
25 inline void sortFamilies(
26 std::vector<std::string>& families)
27 {
28 const auto lower = [](const std::string& text)
29 {
30 std::string folded = text;
31 std::transform(folded.begin(), folded.end(), folded.begin(),
32 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
33 return folded;
34 };
35 std::sort(families.begin(), families.end(), [&lower](const std::string& a, const std::string& b)
36 {
37 const std::string foldedA = lower(a);
38 const std::string foldedB = lower(b);
39 return foldedA == foldedB ? a < b : foldedA < foldedB;
40 });
41 families.erase(std::unique(families.begin(), families.end()), families.end());
42 }
43
44#if defined(_WIN32)
51 inline int CALLBACK collectFamily(
52 const LOGFONTW* font,
53 const TEXTMETRICW*,
54 DWORD,
55 LPARAM lParam)
56 {
57 const std::wstring name = font->lfFaceName;
58 // Names starting with '@' are the vertical-writing duplicates of real families.
59 if (name.empty() || name[0] == L'@') return 1;
60 const int size = WideCharToMultiByte(CP_UTF8, 0, name.c_str(), static_cast<int>(name.size()), nullptr, 0, nullptr, nullptr);
61 if (size <= 0) return 1;
62 std::string utf8(static_cast<std::size_t>(size), '\0');
63 WideCharToMultiByte(CP_UTF8, 0, name.c_str(), static_cast<int>(name.size()), utf8.data(), size, nullptr, nullptr);
64 reinterpret_cast<std::vector<std::string>*>(lParam)->push_back(std::move(utf8));
65 return 1;
66 }
67#endif
68
74 inline std::vector<std::string> installedFamilies()
75 {
76 std::vector<std::string> families;
77#if defined(__APPLE__)
78 CFArrayRef names = CTFontManagerCopyAvailableFontFamilyNames();
79 if (names)
80 {
81 const CFIndex count = CFArrayGetCount(names);
82 for (CFIndex index = 0; index < count; ++index)
83 {
84 const auto name = static_cast<CFStringRef>(CFArrayGetValueAtIndex(names, index));
85 std::string utf8(static_cast<std::size_t>(CFStringGetMaximumSizeForEncoding(CFStringGetLength(name), kCFStringEncodingUTF8)) + 1, '\0');
86 if (!CFStringGetCString(name, utf8.data(), static_cast<CFIndex>(utf8.size()), kCFStringEncodingUTF8)) continue;
87 utf8.resize(std::char_traits<char>::length(utf8.c_str()));
88 // Names starting with '.' are private system faces that are not meant to be chosen.
89 if (!utf8.empty() && utf8[0] != '.') families.push_back(std::move(utf8));
90 }
91 CFRelease(names);
92 }
93#elif defined(_WIN32)
94 HDC context = GetDC(nullptr);
95 if (context)
96 {
97 LOGFONTW filter{};
98 filter.lfCharSet = DEFAULT_CHARSET;
99 EnumFontFamiliesExW(context, &filter, reinterpret_cast<FONTENUMPROCW>(collectFamily),
100 reinterpret_cast<LPARAM>(&families), 0);
101 ReleaseDC(nullptr, context);
102 }
103#endif
104 sortFamilies(families);
105 return families;
106 }
107}
108
109#endif
std::vector< std::string > installedFamilies()
Lists the font families installed on this computer. Safe to call from any thread; it can take a while...
void sortFamilies(std::vector< std::string > &families)
Sorts family names ignoring case and removes duplicates.