fcft.zig (4950B)
1 const pixman = @import("pixman"); 2 3 pub const Error = error{ 4 NoFont, 5 NoGlyph, 6 NoGrapheme, 7 NoTextRun, 8 }; 9 10 pub const EmojiPresentation = enum(c_int) { 11 default, 12 text, 13 emoji, 14 }; 15 16 // Note that this is ignored if antialiasing has been disabled. 17 pub const Subpixel = enum(c_int) { 18 default, 19 none, 20 horizontal_rgb, 21 horizontal_bgr, 22 vertical_rgb, 23 vertical_bgr, 24 }; 25 26 pub const Glyph = extern struct { 27 cp: u32, 28 cols: c_int, 29 30 // Name of the font the glyph was loaded from. 31 // Note that it is always null in text-runs glyphs. 32 font_name: ?[*:0]const u8, 33 pix: *pixman.Image, 34 35 x: c_int, 36 y: c_int, 37 width: c_int, 38 height: c_int, 39 40 advance: extern struct { 41 x: c_int, 42 y: c_int, 43 }, 44 }; 45 46 pub const Grapheme = extern struct { 47 cols: c_int, 48 count: usize, 49 glyphs: [*]*const Glyph, 50 }; 51 52 pub const TextRun = extern struct { 53 glyphs: [*]*const Glyph, 54 cluster: [*]c_int, 55 count: usize, 56 57 extern fn fcft_text_run_destroy(self: *const TextRun) void; 58 pub const destroy = fcft_text_run_destroy; 59 }; 60 61 pub const Font = extern struct { 62 // Name of the primary font only. 63 name: ?[*:0]const u8, 64 65 height: c_int, 66 descent: c_int, 67 ascent: c_int, 68 69 // Width/height of font's widest glyph. 70 max_advance: extern struct { 71 x: c_int, 72 y: c_int, 73 }, 74 75 underline: extern struct { 76 position: c_int, 77 thickness: c_int, 78 }, 79 80 strikeout: extern struct { 81 position: c_int, 82 thickness: c_int, 83 }, 84 85 antialias: bool, 86 87 subpixel: Subpixel, 88 89 extern fn fcft_from_name( 90 count: usize, 91 names: [*][*:0]const u8, 92 attributes: ?[*:0]const u8, 93 ) ?*Font; 94 pub fn fromName(names: [][*:0]const u8, attributes: ?[*:0]const u8) !*Font { 95 return fcft_from_name(names.len, names.ptr, attributes) orelse error.NoFont; 96 } 97 98 extern fn fcft_clone(self: *const Font) ?*Font; 99 pub const clone = fcft_clone; 100 101 extern fn fcft_destroy(self: *Font) void; 102 pub const destroy = fcft_destroy; 103 104 extern fn fcft_rasterize_char_utf32(self: *Font, cp: u32, subpixel: Subpixel) ?*const Glyph; 105 pub fn rasterizeCharUtf32(self: *Font, cp: u32, subpixel: Subpixel) !*const Glyph { 106 return fcft_rasterize_char_utf32(self, cp, subpixel) orelse error.NoGlyph; 107 } 108 109 extern fn fcft_rasterize_grapheme_utf32( 110 self: *Font, 111 len: usize, 112 grapheme_cluster: [*]const u32, 113 subpixel: Subpixel, 114 ) ?*const Grapheme; 115 pub fn rasterizeGraphemeUtf32( 116 self: *Font, 117 grapheme_cluster: []const u32, 118 subpixel: Subpixel, 119 ) !*const Grapheme { 120 return fcft_rasterize_grapheme_utf32( 121 self, 122 grapheme_cluster.len, 123 grapheme_cluster.ptr, 124 subpixel, 125 ) orelse error.NoGrapheme; 126 } 127 128 extern fn fcft_rasterize_text_run_utf32( 129 self: *Font, 130 len: usize, 131 text: [*]const u32, 132 subpixel: Subpixel, 133 ) ?*const TextRun; 134 pub fn rasterizeTextRunUtf32(self: *Font, text: []const u32, subpixel: Subpixel) !*const TextRun { 135 return fcft_rasterize_text_run_utf32(self, text.len, text.ptr, subpixel) orelse error.NoTextRun; 136 } 137 138 extern fn fcft_kerning( 139 self: *Font, 140 left: u32, 141 right: u32, 142 noalias x: ?*c_long, 143 noalias y: ?*c_long, 144 ) bool; 145 pub const kerning = fcft_kerning; 146 147 extern fn fcft_precompose( 148 self: *const Font, 149 base: u32, 150 comb: u32, 151 base_is_from_primary: bool, 152 comb_is_from_primary: bool, 153 composed_is_from_primary: bool, 154 ) u32; 155 pub const precompose = fcft_precompose; 156 157 // Note: this function does not clear the glyph or grapheme caches, call 158 // before rasterizing any glyphs. 159 extern fn fcft_set_emoji_presentation(self: *Font, presentation: EmojiPresentation) void; 160 pub const setEmojiPresentation = fcft_set_emoji_presentation; 161 }; 162 163 pub const LogColorize = enum(c_int) { 164 never, 165 always, 166 auto, 167 }; 168 169 pub const LogClass = enum(c_int) { 170 none, 171 err, 172 warning, 173 info, 174 debug, 175 }; 176 177 // Must be called before instantiating fonts. 178 extern fn fcft_init(colorize: LogColorize, do_syslog: bool, log_level: LogClass) bool; 179 pub const init = fcft_init; 180 181 // Optional, but needed for clean valgrind runs. 182 extern fn fcft_fini() void; 183 pub const fini = fcft_fini; 184 185 pub const Capabilities = struct { 186 pub const grapheme_shaping = 1 << 0; 187 pub const text_run_shaping = 1 << 1; 188 }; 189 190 extern fn fcft_capabilities() u32; 191 pub const capabilities = fcft_capabilities; 192 193 pub const ScalingFilter = enum(c_int) { 194 none, 195 nearest, 196 bilinear, 197 cubic, 198 lanczos3, 199 }; 200 201 // Note: this function does not clear any caches, call before 202 // rasterizing any glyphs. 203 extern fn fcft_set_scaling_filter(filter: ScalingFilter) bool; 204 pub const setScalingFilter = fcft_set_scaling_filter;