Skip to content

Commit

Permalink
Fix font smoothing detection on macOS
Browse files Browse the repository at this point in the history
Fixes #62.
  • Loading branch information
erikolofsson committed Feb 21, 2024
1 parent e59e1d0 commit dc997c3
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/darwin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Font rendering based on CoreText.
use std::collections::HashMap;
use std::ffi::c_char;
use std::ffi::CStr;
use std::iter;
use std::path::PathBuf;
use std::ptr;

use cocoa::base::{id, nil};
use cocoa::foundation::{NSString, NSUserDefaults};
use cocoa::foundation::{NSInteger, NSString, NSUserDefaults};

use core_foundation::array::{CFArray, CFIndex};
use core_foundation::base::{CFType, ItemRef, TCFType};
Expand Down Expand Up @@ -277,26 +278,30 @@ static FONT_SMOOTHING_ENABLED: Lazy<bool> = Lazy::new(|| {
let key = NSString::alloc(nil).init_str("AppleFontSmoothing");
let value: id = msg_send![id::standardUserDefaults(), objectForKey: key];

if !msg_send![value, isKindOfClass: class!(NSNumber)] {
return true;
}
if msg_send![value, isKindOfClass: class!(NSNumber)] {
let num_type: *const c_char = msg_send![value, objCType];
if num_type.is_null() {
return true;
}

let num_type: id = msg_send![value, objCType];
if num_type == nil {
return true;
}
// NSNumber's objCType method returns one of these strings depending on the size:
// q = quad (long long), l = long, i = int, s = short.
// This is done to reject booleans, which are NSNumbers with an objCType of "c", but
// macOS does not treat them the same as an integer 0 or 1 for this setting,
// it just ignores it.
let int_specifiers: [&[u8]; 4] = [b"q", b"l", b"i", b"s"];
if !int_specifiers.contains(&CStr::from_ptr(num_type).to_bytes()) {
return true;
}

// NSNumber's objCType method returns one of these strings depending on the size:
// q = quad (long long), l = long, i = int, s = short.
// This is done to reject booleans, which are NSNumbers with an objCType of "c", but macOS
// does not treat them the same as an integer 0 or 1 for this setting, it just ignores it.
let int_specifiers: [&[u8]; 4] = [b"q", b"l", b"i", b"s"];
if !int_specifiers.contains(&CStr::from_ptr(num_type as *const i8).to_bytes()) {
return true;
let smoothing: NSInteger = msg_send![value, integerValue];
return smoothing != 0;
} else if msg_send![value, isKindOfClass: class!(NSString)] {
let smoothing: NSInteger = msg_send![value, integerValue];
return smoothing != 0;
}

let smoothing: id = msg_send![value, integerValue];
smoothing as i64 != 0
true
})
});

Expand Down

0 comments on commit dc997c3

Please sign in to comment.