Skip to content

Commit

Permalink
Update mac_fonts.py
Browse files Browse the repository at this point in the history
  • Loading branch information
moi15moi committed Jan 22, 2024
1 parent 3515caa commit c47e4d7
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions find_system_fonts_filename/mac_fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,35 @@ def get_system_fonts_filename() -> Set[str]:

fonts_filename = set()


font_collection = MacFonts._core_text.CTFontCollectionCreateFromAvailableFonts(None)
font_array = MacFonts._core_text.CTFontCollectionCreateMatchingFontDescriptors(font_collection)
font_count = MacFonts._core_foundation.CFArrayGetCount(font_array)


for i in range(font_count):
font_descriptor = MacFonts._core_foundation.CFArrayGetValueAtIndex(font_array, i)

font_format_ptr = MacFonts._core_text.CTFontDescriptorCopyAttribute(font_descriptor, MacFonts._kCTFontFormatAttribute)
font_format = MacFonts._cfnumber_to_uint32(font_format_ptr)
font_format = CTFontFormat(MacFonts._cfnumber_to_uint32(font_format_ptr))
MacFonts._core_foundation.CFRelease(font_format_ptr)


if font_format not in (
CTFontFormat.kCTFontFormatOpenTypePostScript,
CTFontFormat.kCTFontFormatOpenTypeTrueType,
CTFontFormat.kCTFontFormatTrueType,
CTFontFormat.kCTFontFormatPostScript
):
continue

url_ptr = MacFonts._core_text.CTFontDescriptorCopyAttribute(font_descriptor, MacFonts._kCTFontURLAttribute)
path_ptr = MacFonts._core_text.CFURLCopyFileSystemPath(url_ptr, CFURLPathStyle.kCFURLPOSIXPathStyle)
filename = MacFonts._cfstring_to_string(path_ptr)
MacFonts._core_foundation.CFRelease(path_ptr)
MacFonts._core_foundation.CFRelease(url_ptr)

if Path(filename).suffix.lstrip(".").strip().lower() not in MacFonts.VALID_FONT_FORMATS:
continue

fonts_filename.add(filename)
print(f"{CTFontFormat(font_format).name} {filename}")



MacFonts._core_foundation.CFRelease(font_array)
MacFonts._core_foundation.CFRelease(font_collection)
Expand Down Expand Up @@ -196,10 +201,6 @@ def _load_core_library():
# https://developer.apple.com/documentation/corefoundation/1543114-cfnumbergetvalue?language=objc
MacFonts._core_foundation.CFNumberGetValue.restype = c_bool
MacFonts._core_foundation.CFNumberGetValue.argtypes = [CFNumberRef, CFNumberType, c_void_p]

# https://developer.apple.com/documentation/coretext/1499478-ctfontmanagercopyavailablefontur?language=objc
MacFonts._core_text.CTFontManagerCopyAvailableFontURLs.restype = c_void_p
MacFonts._core_text.CTFontManagerCopyAvailableFontURLs.argtypes = []

# https://developer.apple.com/documentation/coretext/1509907-ctfontcollectioncreatefromavaila?language=objc
MacFonts._core_text.CTFontCollectionCreateFromAvailableFonts.restype = c_void_p
Expand All @@ -217,12 +218,10 @@ def _load_core_library():
MacFonts._core_text.CFURLCopyFileSystemPath.restype = c_void_p
MacFonts._core_text.CFURLCopyFileSystemPath.argtypes = [c_void_p, CFIndex]


MacFonts._kCTFontURLAttribute = c_void_p.in_dll(MacFonts._core_text, "kCTFontURLAttribute")
MacFonts._kCTFontFormatAttribute = c_void_p.in_dll(MacFonts._core_text, "kCTFontFormatAttribute")



class MacVersionHelpers:
@staticmethod
def is_mac_version_or_greater(minimum_major: int, minimum_minor: int) -> bool:
Expand Down

0 comments on commit c47e4d7

Please sign in to comment.