Skip to content

Commit

Permalink
guard against bad CTFontManagerCreateFontDescriptorsFromURL return va…
Browse files Browse the repository at this point in the history
…lue (#114)
  • Loading branch information
thomasp85 authored May 13, 2024
1 parent 31e30df commit 199e4ec
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
`match_fonts()`
* Two internal functions for converting weight and width names to integers have
been exported
* Fix a segfault on macOS when the system encounters a corrupted font collection
(#113)

# systemfonts 1.0.6

Expand Down
1 change: 1 addition & 0 deletions cleanup
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/bin/sh
rm -f src/Makevars configure.log
rm -Rf .deps
23 changes: 14 additions & 9 deletions src/mac/FontManagerMac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,26 @@ void addFontIndex(FontDescriptor* font) { @autoreleasepool {
NSString *font_path = [NSString stringWithUTF8String:font->path];
NSURL *font_url = [NSURL fileURLWithPath: font_path];
CFArrayRef font_descriptors = CTFontManagerCreateFontDescriptorsFromURL((CFURLRef) font_url);
int n_fonts = CFArrayGetCount(font_descriptors);
if (n_fonts == 1) {
if (font_descriptors == NULL) {
font_no = 0;
font_index[font_name] = 0;
} else {
for (int i = 0; i < n_fonts; i++) {
CTFontDescriptorRef font_at_i = (CTFontDescriptorRef) CFArrayGetValueAtIndex(font_descriptors, i);
std::string font_name_at_i = [(__bridge_transfer NSString *) CTFontDescriptorCopyAttribute(font_at_i, kCTFontNameAttribute) UTF8String];
font_index[font_name_at_i] = i;
if (font_name.compare(font_name_at_i) == 0) {
font_no = i;
int n_fonts = CFArrayGetCount(font_descriptors);
if (n_fonts == 1) {
font_no = 0;
font_index[font_name] = 0;
} else {
for (int i = 0; i < n_fonts; i++) {
CTFontDescriptorRef font_at_i = (CTFontDescriptorRef) CFArrayGetValueAtIndex(font_descriptors, i);
std::string font_name_at_i = [(__bridge_transfer NSString *) CTFontDescriptorCopyAttribute(font_at_i, kCTFontNameAttribute) UTF8String];
font_index[font_name_at_i] = i;
if (font_name.compare(font_name_at_i) == 0) {
font_no = i;
}
}
}
CFRelease(font_descriptors);
}
CFRelease(font_descriptors);
} else {
font_no = (*it).second;
}
Expand Down

0 comments on commit 199e4ec

Please sign in to comment.