text_fonts

source of system::directwrite

1// Fallback on Windows (decisions/F10.md, question 1A):2// DirectWrite's system font fallback maps a character, in the run's3// language (the user's when it has none), to a font of the system's4// collection, whose file is local; its path and face index are read5// through the font's face and file loader. DirectWrite is loaded while6// running, and its objects reached through pkg::alloy::com; the slots7// are checked against MinGW's headers (tests/windows/com_layout.c).89import std::ffi;10import std::option;11import std::string;12import std::vector;13import pkg::alloy::com;1415import fallback;1617extern GetUserDefaultLocaleName(out: c_pointer, size: i32) -> i32;1819const IID_FACTORY2: [u8 : 16] = guid(&"0439fc60-ca44-4994-8dee-3a9af7b732ec");20const IID_ANALYSIS_SOURCE: [u8 : 16] = guid(&"688e1a58-5094-47c8-adc8-fbcea60ae92b");21const IID_LOCAL_LOADER: [u8 : 16] = guid(&"b2d9f3ec-c9fe-4a11-a2ec-d86208f7c0a2");2223// the slots called, after IUnknown's three24const GET_SYSTEM_FONT_FALLBACK: u64 = 26; // IDWriteFactory2's25const MAP_CHARACTERS: u64 = 3; // IDWriteFontFallback's26const CREATE_FONT_FACE: u64 = 13; // IDWriteFont's27const GET_FILES: u64 = 4; // IDWriteFontFace's28const GET_INDEX: u64 = 5;29const GET_REFERENCE_KEY: u64 = 3; // IDWriteFontFile's30const GET_LOADER: u64 = 4;31const GET_PATH_LENGTH: u64 = 4; // IDWriteLocalFontFileLoader's32const GET_PATH: u64 = 5;3334type OutFunction = extern (c_pointer, c_pointer) -> i32;35type QueryFunction = extern (c_pointer, c_pointer, c_pointer) -> i32;36type MapFunction = extern (37    c_pointer,38    c_pointer,39    u32,40    u32,41    c_pointer,42    c_pointer,43    u32,44    u32,45    u32,46    c_pointer,47    c_pointer,48    c_pointer49) -> i32;50type CountFunction = extern (c_pointer) -> u32;51type TextFunction = extern (c_pointer, u32, c_pointer, c_pointer) -> i32;52type KeyFunction = extern (c_pointer, c_pointer, u32, c_pointer) -> i32;53type PathFunction = extern (c_pointer, c_pointer, u32, c_pointer, u32) -> i32;5455// DirectWrite's factory and its system font fallback.56pub type DirectWrite = struct {57    library: Library,58    factory: c_pointer, // IDWriteFactory259    fallback: c_pointer, // IDWriteFontFallback60};6162// DirectWrite, None where it cannot be loaded or lacks the system font63// fallback (before Windows 8.1).64pub fn DirectWrite::load() -> Option<DirectWrite> {65    const library = if (Library::open(&"dwrite.dll") is ::Some |l|) l else {66        return ::None;67    };68    const create = if (69        library.symbol<extern (70            u32,71            c_pointer,72            c_pointer73        ) -> i32>(&"DWriteCreateFactory") is ::Some |f|74    ) f else {75        return ::None;76    };77    var factory: c_pointer = 0;78    // DWRITE_FACTORY_TYPE_SHARED79    if (80        create(0, c_pointer::of(&IID_FACTORY2[..]), c_pointer::from(&factory)) != S_OK81        || factory == 082    ) {83        return ::None;84    }85    var fallback: c_pointer = 0;86    if (87        method<OutFunction>(factory, GET_SYSTEM_FONT_FALLBACK)(88            factory,89            c_pointer::from(&fallback)90        ) != S_OK91        || fallback == 092    ) {93        release(factory);94        return ::None;95    }96    return ::Some(DirectWrite { .library = library, .factory = factory, .fallback = fallback });97}9899// The text DirectWrite maps, as an IDWriteTextAnalysisSource: its table,100// a count of references, the text and the locale, in UTF-16.101type Source = struct {102    table: c_pointer,103    count: u32,104    text: c_pointer,105    length: u32,106    locale: c_pointer,107};108109// The file and face index of the font the system's fallback maps the110// character to, from Segoe UI at the weight.111pub fn font_for(self dw: &DirectWrite, c: u32, language: &[u8], weight: u32) -> Option<SystemFont> {112    var units: [u16 : 2] = [0, 0];113    var length: u32 = 1;114    if (c >= 0x10000) {115        units[0] = (0xD800 + ((c - 0x10000) >> 10)) to u16;116        units[1] = (0xDC00 + ((c - 0x10000) & 0x3FF)) to u16;117        length = 2;118    } else {119        units[0] = c to u16;120    }121    var locale = wide(&language);122    if (language.length() == 0) {123        var user: [u16 : 85] = [0 : 85];124        GetUserDefaultLocaleName(c_pointer::of(&user[..]), 85);125        locale = Vector<u16>::from(&user[..]);126    }127    const query_slot: QueryFunction = query;128    const add_slot: CountFunction = add_reference;129    const release_slot: CountFunction = release_reference;130    const at_slot: TextFunction = text_at;131    const before_slot: TextFunction = text_before;132    const direction_slot: CountFunction = reading_direction;133    const locale_slot: TextFunction = locale_name;134    const number_slot: TextFunction = number_substitution;135    var slots: [c_pointer : 8] = [136        query_slot as c_pointer,137        add_slot as c_pointer,138        release_slot as c_pointer,139        at_slot as c_pointer,140        before_slot as c_pointer,141        direction_slot as c_pointer,142        locale_slot as c_pointer,143        number_slot as c_pointer144    ];145    var source = Source {146        .table = c_pointer::of(&slots[..]),147        .count = 1,148        .text = c_pointer::of(&units[..]),149        .length = length,150        .locale = c_pointer::of(&locale.view())151    };152    const family = wide(&"Segoe UI");153    var mapped_length: u32 = 0;154    var font: c_pointer = 0;155    var scale: f32 = 1.0;156    // DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL157    const result = method<MapFunction>(dw.fallback, MAP_CHARACTERS)(158        dw.fallback,159        c_pointer::from(&source),160        0,161        length,162        0,163        c_pointer::of(&family.view()),164        weight,165        0,166        5,167        c_pointer::from(&mapped_length),168        c_pointer::from(&font),169        c_pointer::from(&scale)170    );171    if (result != S_OK || font == 0) {172        return ::None;173    }174    var found: Option<SystemFont> = ::None;175    var face: c_pointer = 0;176    if (method<OutFunction>(font, CREATE_FONT_FACE)(font, c_pointer::from(&face)) == S_OK) {177        found = path_of(face);178        release(face);179    }180    release(font);181    return move found;182}183184// The local file a font face is read from, and its index there.185fn path_of(face: c_pointer) -> Option<SystemFont> {186    var count: u32 = 0;187    const get_files = method<QueryFunction>(face, GET_FILES);188    if (get_files(face, c_pointer::from(&count), 0) != S_OK || count == 0) {189        return ::None;190    }191    var files = Vector<c_pointer>::filled(0, count to u64);192    if (get_files(face, c_pointer::from(&count), c_pointer::of(&files.view())) != S_OK) {193        return ::None;194    }195    const index = method<extern (c_pointer) -> u32>(face, GET_INDEX)(face);196    var found: Option<SystemFont> = ::None;197    const file = files.get(0);198    var key: c_pointer = 0;199    var key_size: u32 = 0;200    var loader: c_pointer = 0;201    if (202        method<QueryFunction>(file, GET_REFERENCE_KEY)(203            file,204            c_pointer::from(&key),205            c_pointer::from(&key_size)206        ) == S_OK207        && method<OutFunction>(file, GET_LOADER)(file, c_pointer::from(&loader)) == S_OK208    ) {209        var local: c_pointer = 0;210        if (211            method<QueryFunction>(loader, 0)(212                loader,213                c_pointer::of(&IID_LOCAL_LOADER[..]),214                c_pointer::from(&local)215            ) == S_OK216        ) {217            var length: u32 = 0;218            if (219                method<KeyFunction>(local, GET_PATH_LENGTH)(220                    local,221                    key,222                    key_size,223                    c_pointer::from(&length)224                ) == S_OK225            ) {226                var path = Vector<u16>::filled(0, length to u64 + 1);227                if (228                    method<PathFunction>(local, GET_PATH)(229                        local,230                        key,231                        key_size,232                        c_pointer::of(&path.view()),233                        length + 1234                    )235                    == S_OK236                ) {237                    found = ::Some(SystemFont {238                        .path = narrow(&path.view()[..length to u64], false),239                        .index = index240                    });241                }242            }243            release(local);244        }245        release(loader);246    }247    for (files.view()) |f| {248        release(f);249    }250    return move found;251}252253// IDWriteTextAnalysisSource's functions, over a Source that lives as long254// as the call to MapCharacters.255256fn query(this: c_pointer, id: c_pointer, out: c_pointer) -> i32 {257    if (is_guid(id, &IID_UNKNOWN) || is_guid(id, &IID_ANALYSIS_SOURCE)) {258        this.as_var<Source>().count += 1;259        put(out, this);260        return S_OK;261    }262    put(out, 0);263    return E_NOINTERFACE;264}265266fn add_reference(this: c_pointer) -> u32 {267    const s = &var this.as_var<Source>();268    s.count += 1;269    return s.count;270}271272fn release_reference(this: c_pointer) -> u32 {273    const s = &var this.as_var<Source>();274    s.count -= 1;275    return s.count;276}277278fn text_at(this: c_pointer, position: u32, text: c_pointer, length: c_pointer) -> i32 {279    const s = &this.as_ref<Source>();280    const inside = position < s.length;281    put(text, if (inside) s.text + position to c_pointer * 2 else 0);282    length.view_var<u32>(1)[0] = if (inside) s.length - position else 0;283    return S_OK;284}285286fn text_before(this: c_pointer, position: u32, text: c_pointer, length: c_pointer) -> i32 {287    const s = &this.as_ref<Source>();288    const inside = position > 0 && position <= s.length;289    put(text, if (inside) s.text else 0);290    length.view_var<u32>(1)[0] = if (inside) position else 0;291    return S_OK;292}293294// DWRITE_READING_DIRECTION_LEFT_TO_RIGHT295fn reading_direction(this: c_pointer) -> u32 => 0;296297fn locale_name(this: c_pointer, position: u32, length: c_pointer, locale: c_pointer) -> i32 {298    const s = &this.as_ref<Source>();299    length.view_var<u32>(1)[0] = if (position < s.length) s.length - position else 0;300    put(locale, s.locale);301    return S_OK;302}303304fn number_substitution(305    this: c_pointer,306    position: u32,307    length: c_pointer,308    substitution: c_pointer309) -> i32 {310    const s = &this.as_ref<Source>();311    length.view_var<u32>(1)[0] = if (position < s.length) s.length - position else 0;312    put(substitution, 0);313    return S_OK;314}