text_fonts

source of system::coretext

1// Fallback on macOS (decisions/F10.md, question 1A): Core Text2// picks the font for a character from the system UI font's cascade for3// the run's language, and gives its file's URL and PostScript name, by4// which a collection's face is found. Core Text and Core Foundation are5// loaded while running, through pkg::alloy::objc's helpers.67import std::ffi;8import std::option;9import std::string;10import std::vector;11import pkg::alloy::objc;1213import fallback;1415// CFRange, by value16type Range = struct {17    location: i64,18    length: i64,19};2021const UTF8: u32 = 0x08000100; // kCFStringEncodingUTF822const SYSTEM_FONT: u32 = 2; // kCTFontUIFontSystem2324pub type CoreText = struct {25    text: Library,26    foundation: Library,27    url_attribute: c_pointer, // kCTFontURLAttribute28    string_create: extern (c_pointer, c_pointer, i64, u32, bool) -> c_pointer,29    string_get: extern (c_pointer, c_pointer, i64, u32) -> bool,30    release: extern (c_pointer),31    ui_font: extern (u32, f64, c_pointer) -> c_pointer,32    font_for_string: extern (c_pointer, c_pointer, Range) -> c_pointer,33    copy_attribute: extern (c_pointer, c_pointer) -> c_pointer,34    postscript_name: extern (c_pointer) -> c_pointer,35    file_path: extern (c_pointer, bool, c_pointer, i64) -> bool,36};3738pub fn CoreText::load() -> Option<CoreText> {39    const text = if (40        Library::open(&"/System/Library/Frameworks/CoreText.framework/CoreText") is ::Some |l|41    ) l else {42        return ::None;43    };44    const foundation = if (45        Library::open(46            &"/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"47        ) is ::Some |l|48    ) l else {49        return ::None;50    };51    return ::Some(CoreText {52        .url_attribute = pointer_at(&text, &"kCTFontURLAttribute"),53        .string_create = required(&foundation, &"CFStringCreateWithBytes"),54        .string_get = required(&foundation, &"CFStringGetCString"),55        .release = required(&foundation, &"CFRelease"),56        .ui_font = required(&text, &"CTFontCreateUIFontForLanguage"),57        .font_for_string = required(&text, &"CTFontCreateForString"),58        .copy_attribute = required(&text, &"CTFontCopyAttribute"),59        .postscript_name = required(&text, &"CTFontCopyPostScriptName"),60        .file_path = required(&foundation, &"CFURLGetFileSystemRepresentation"),61        .text = text,62        .foundation = foundation63    });64}6566fn string(self ct: &CoreText, text: &[u8]) -> c_pointer {67    return ct.string_create(0, c_pointer::of(&text), text.length() to i64, UTF8, false);68}6970// The file of the font Core Text picks for the character, and its face's71// PostScript name, which finds it in a collection.72pub fn font_for(self ct: &CoreText, c: u32, language: &[u8]) -> Option<SystemFont> {73    var bytes = Vector<u8>::empty();74    push_utf8(&var bytes, c);75    const language_string = if (language.length() > 0) ct.string(&language) else 0;76    const base = ct.ui_font(SYSTEM_FONT, 0.0, language_string);77    if (language_string != 0) {78        ct.release(language_string);79    }80    if (base == 0) {81        return ::None;82    }83    const string = ct.string(&bytes.view());84    // the range in UTF-16 units85    const font = ct.font_for_string(86        base,87        string,88        Range { .location = 0, .length = if (c >= 0x10000) 2 else 1 }89    );90    ct.release(string);91    ct.release(base);92    if (font == 0) {93        return ::None;94    }95    var found: Option<SystemFont> = ::None;96    const url = ct.copy_attribute(font, ct.url_attribute);97    if (url != 0) {98        var path: [u8 : 1024] = [0 : 1024];99        if (ct.file_path(url, true, c_pointer::of(&path[..]), 1024)) {100            var name = String::empty();101            const ps = ct.postscript_name(font);102            if (ps != 0) {103                var buffer: [u8 : 256] = [0 : 256];104                if (ct.string_get(ps, c_pointer::of(&buffer[..]), 256, UTF8)) {105                    name = String::from(&c_pointer::of(&buffer[..]).c_text());106                }107                ct.release(ps);108            }109            found = ::Some(SystemFont {110                .path = String::from(&c_pointer::of(&path[..]).c_text()),111                .index = 0,112                .name = move name113            });114        }115        ct.release(url);116    }117    ct.release(font);118    return move found;119}120121fn push_utf8(out: &var Vector<u8>, c: u32) {122    if (c < 0x80) {123        out.push(c to u8);124    } else if (c < 0x800) {125        out.push((0xC0 | (c >> 6)) to u8);126        out.push((0x80 | (c & 0x3F)) to u8);127    } else if (c < 0x10000) {128        out.push((0xE0 | (c >> 12)) to u8);129        out.push((0x80 | ((c >> 6) & 0x3F)) to u8);130        out.push((0x80 | (c & 0x3F)) to u8);131    } else {132        out.push((0xF0 | (c >> 18)) to u8);133        out.push((0x80 | ((c >> 12) & 0x3F)) to u8);134        out.push((0x80 | ((c >> 6) & 0x3F)) to u8);135        out.push((0x80 | (c & 0x3F)) to u8);136    }137}