Last active
January 17, 2023 23:20
-
-
Save Constellation/4946675 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // list available fonts | |
| // | |
| // compile with | |
| // clang++ fonts.cc -o fonts -std=c++11 -stdlib=libc++ -framework CoreText -framework CoreFoundation -lc++ | |
| // | |
| #include <iostream> | |
| #include <string> | |
| #include <vector> | |
| #include <CoreFoundation/CoreFoundation.h> | |
| #include <CoreText/CoreText.h> | |
| std::string to_string(CFStringRef str) { | |
| const std::size_t size = CFStringGetLength(str); | |
| if (size == 0) { | |
| return std::string(); | |
| } | |
| const CFRange range = CFRangeMake(0, size); | |
| CFIndex osize = 0; | |
| if (CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false, nullptr, 0, &osize) == 0) { | |
| return std::string(); | |
| } | |
| if (osize == 0) { | |
| return std::string(); | |
| } | |
| std::vector<char> vec(osize); | |
| if (CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false, reinterpret_cast<UInt8*>(vec.data()), vec.size(), nullptr) == 0) { | |
| return std::string(); | |
| } | |
| return std::string(vec.data(), vec.size()); | |
| } | |
| int main(int argc, char** argv) { | |
| using std::to_string; | |
| CTFontCollectionRef collection = CTFontCollectionCreateFromAvailableFonts(nullptr); | |
| CFArrayRef array = CTFontCollectionCreateMatchingFontDescriptors(collection); | |
| for (int i = 0, iz = CFArrayGetCount(array); i < iz; ++i) { | |
| CTFontDescriptorRef font = reinterpret_cast<CTFontDescriptorRef>(CFArrayGetValueAtIndex(array, i)); | |
| if (!font) { | |
| continue; | |
| } | |
| CFURLRef url = reinterpret_cast<CFURLRef>(CTFontDescriptorCopyAttribute(font, kCTFontURLAttribute)); | |
| CFStringRef str = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle); | |
| const std::string path = to_string(str); | |
| CFRelease(str); | |
| CFRelease(url); | |
| std::cout << path << std::endl; | |
| } | |
| CFRelease(array); | |
| CFRelease(collection); | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result in my environment