1// Fallback on Linux (decisions/F10.md, question 1A): fontconfig,2// loaded while running as window loads libwayland-client, sorts the3// system's fonts for a character, the run's language and weight (and,4// for emoji, its `emoji` family in color), and the first font covering5// the character is the one read.67import std::ffi;8import std::option;9import std::string;1011import fallback;1213// fontconfig's functions; its FcConfig is the current one throughout14pub type Fontconfig = struct {15 library: Library,16 pattern_create: extern () -> c_pointer,17 pattern_destroy: extern (c_pointer),18 add_string: extern (c_pointer, c_string, c_string) -> i32,19 add_integer: extern (c_pointer, c_string, i32) -> i32,20 add_bool: extern (c_pointer, c_string, i32) -> i32,21 add_charset: extern (c_pointer, c_string, c_pointer) -> i32,22 charset_create: extern () -> c_pointer,23 charset_add: extern (c_pointer, u32) -> i32,24 charset_has: extern (c_pointer, u32) -> i32,25 charset_destroy: extern (c_pointer),26 substitute: extern (c_pointer, c_pointer, i32) -> i32,27 default_substitute: extern (c_pointer),28 sort: extern (c_pointer, c_pointer, i32, c_pointer, c_pointer) -> c_pointer,29 set_destroy: extern (c_pointer),30 get_charset: extern (c_pointer, c_string, i32, c_pointer) -> i32,31 get_string: extern (c_pointer, c_string, i32, c_pointer) -> i32,32 get_integer: extern (c_pointer, c_string, i32, c_pointer) -> i32,33 weight_from_open_type: extern (i32) -> i32,34};3536// fontconfig, None where it cannot be loaded.37pub fn Fontconfig::load() -> Option<Fontconfig> {38 const library = if (Library::open(&"libfontconfig.so.1") is ::Some |l|) l else {39 return ::None;40 };41 // FcWeightFromOpenType, the newest function used, came with 2.11.91:42 // an older fontconfig is not asked43 if (library.symbol<extern (i32) -> i32>(&"FcWeightFromOpenType") is ::None) {44 return ::None;45 }46 return ::Some(Fontconfig {47 .pattern_create = function(&library, &"FcPatternCreate"),48 .pattern_destroy = function(&library, &"FcPatternDestroy"),49 .add_string = function(&library, &"FcPatternAddString"),50 .add_integer = function(&library, &"FcPatternAddInteger"),51 .add_bool = function(&library, &"FcPatternAddBool"),52 .add_charset = function(&library, &"FcPatternAddCharSet"),53 .charset_create = function(&library, &"FcCharSetCreate"),54 .charset_add = function(&library, &"FcCharSetAddChar"),55 .charset_has = function(&library, &"FcCharSetHasChar"),56 .charset_destroy = function(&library, &"FcCharSetDestroy"),57 .substitute = function(&library, &"FcConfigSubstitute"),58 .default_substitute = function(&library, &"FcDefaultSubstitute"),59 .sort = function(&library, &"FcFontSort"),60 .set_destroy = function(&library, &"FcFontSetDestroy"),61 .get_charset = function(&library, &"FcPatternGetCharSet"),62 .get_string = function(&library, &"FcPatternGetString"),63 .get_integer = function(&library, &"FcPatternGetInteger"),64 .weight_from_open_type = function(&library, &"FcWeightFromOpenType"),65 .library = library66 });67}6869fn function<F: CFunction>(library: &Library, name: &[u8]) -> F {70 if (library.symbol<F>(&name) is ::Some |move f|) {71 return move f;72 }73 const message = f"pkg::alloy::text: fontconfig lacks {name}";74 panic message.view();75}7677// FcFontSet's fields78type FontSet = struct {79 count: i32,80 capacity: i32,81 fonts: c_pointer,82};8384// The file and face index of the first font fontconfig sorts for the85// character that covers it: in a sans-serif face, of the weight (as86// OpenType gives it) and language (BCP 47, none for the user's), or in87// its color `emoji` family.88pub fn font_for(89 self fc: &Fontconfig,90 c: u32,91 language: &[u8],92 weight: u32,93 emoji: bool94) -> Option<SystemFont> {95 const pattern = fc.pattern_create();96 if (emoji) {97 fc.add_string(pattern, &"family\0", &"emoji\0");98 fc.add_bool(pattern, &"color\0", 1);99 } else {100 fc.add_string(pattern, &"family\0", &"sans-serif\0");101 }102 fc.add_integer(pattern, &"weight\0", fc.weight_from_open_type(weight to i32));103 if (language.length() > 0) {104 var lang = String::from(&language);105 lang.push(0);106 fc.add_string(pattern, &"lang\0", &lang.view());107 }108 const wanted = fc.charset_create();109 fc.charset_add(wanted, c);110 fc.add_charset(pattern, &"charset\0", wanted);111 fc.charset_destroy(wanted);112 // FcMatchPattern113 fc.substitute(0, pattern, 0);114 fc.default_substitute(pattern);115 var result: i32 = 0;116 const set = fc.sort(0, pattern, 1, 0, c_pointer::from(&result));117 fc.pattern_destroy(pattern);118 if (set == 0) {119 return ::None;120 }121 var found: Option<SystemFont> = ::None;122 const fonts = set.as_ref<FontSet>();123 const list = &fonts.fonts.view<c_pointer>(fonts.count to u64);124 for (list) |font| {125 var charset: c_pointer = 0;126 if (127 fc.get_charset(font, &"charset\0", 0, c_pointer::from(&charset)) != 0128 || fc.charset_has(charset, c) == 0129 ) {130 continue;131 }132 var file: c_pointer = 0;133 var index: i32 = 0;134 if (fc.get_string(font, &"file\0", 0, c_pointer::from(&file)) == 0) {135 fc.get_integer(font, &"index\0", 0, c_pointer::from(&index));136 // a named instance's number is in the high half137 found = ::Some(SystemFont {138 .path = String::from(&file.c_text()),139 .index = (index & 0xFFFF) to u32140 });141 break;142 }143 }144 fc.set_destroy(set);145 return move found;146}