The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

FallbackFont.vala in libbirdfont/Renderer

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/Renderer/FallbackFont.vala.
Fix warning about missing version list
1 /* 2 Copyright (C) 2015 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Gee; 16 17 [SimpleType] 18 [CCode (has_type_id = false)] 19 public extern struct FcConfig { 20 } 21 22 [CCode (cname = "FcInitLoadConfigAndFonts")] 23 public extern FcConfig* FcInitLoadConfigAndFonts (); 24 25 [CCode (cname = "FcConfigAppFontAddDir")] 26 public extern string* FcConfigAppFontAddDir (FcConfig* config, string path); 27 28 [CCode (cname = "FcConfigSetSysRoot")] 29 public extern void FcConfigSetSysRoot (FcConfig* config, string path); 30 31 [CCode (cname = "FcConfigParseAndLoad")] 32 public extern bool FcConfigParseAndLoad (FcConfig* config, string path, bool complain); 33 34 [CCode (cname = "FcConfigSetCurrent")] 35 public extern void FcConfigSetCurrent (FcConfig* config); 36 37 [CCode (cname = "FcConfigCreate")] 38 public extern FcConfig* FcConfigCreate (); 39 40 [CCode (cname = "FcConfigFilename")] 41 public extern string FcConfigFilename (string path); 42 43 [CCode (cname = "find_font")] 44 public extern string? find_font (FcConfig* font_config, string characters); 45 46 [CCode (cname = "find_font_family")] 47 public extern string? find_font_family (FcConfig* font_config, string characters); 48 49 [CCode (cname = "find_font_file")] 50 public extern string? find_font_file (FcConfig* font_config, string font_name); 51 52 namespace BirdFont { 53 54 // TODO: use font config 55 public class FallbackFont : GLib.Object { 56 Gee.ArrayList<File> font_directories; 57 58 FontFace* default_font = null; 59 public static FcConfig* font_config = null; 60 static bool font_config_started = false; 61 62 string default_font_file_name = "Roboto-Regular.ttf"; 63 string default_font_family_name = "Roboto"; 64 65 Gee.HashMap<unichar, CachePair> glyphs; 66 Gee.ArrayList<CachePair> cached; 67 68 public int max_cached_fonts = 300; 69 70 string? default_font_file = null; 71 72 public FallbackFont () { 73 string home = Environment.get_home_dir (); 74 font_directories = new Gee.ArrayList<File> (); 75 76 if (!font_config_started) { 77 font_config_started = true; 78 79 IdleSource idle = new IdleSource (); 80 idle.set_callback (() => { 81 Task t = new Task (init_font_config); 82 MainWindow.native_window.run_non_blocking_background_thread (t); 83 return false; 84 }); 85 idle.attach (null); 86 } 87 88 add_font_folder ("/usr/share/fonts/"); 89 add_font_folder ("/usr/local/share/fonts/"); 90 add_font_folder (home + "/.local/share/fonts"); 91 add_font_folder (home + "/.fonts"); 92 add_font_folder ("C:\\Windows\\Fonts"); 93 add_font_folder (home + "/Library/Fonts"); 94 add_font_folder ("/Library/Fonts"); 95 add_font_folder ("/Network/Library/Fonts"); 96 add_font_folder ("/System/Library/Fonts"); 97 add_font_folder ("/System Folder/Fonts"); 98 99 glyphs = new Gee.HashMap<unichar, CachePair> (); 100 cached = new Gee.ArrayList<CachePair> (); 101 102 open_default_font (); 103 } 104 105 ~FallbackFont () { 106 if (default_font != null) { 107 close_font (default_font); 108 } 109 } 110 111 public void init_font_config () { 112 FcConfig* config; 113 114 #if MAC 115 config = FcConfigCreate(); 116 117 string bundle = (!) BirdFont.get_settings_directory ().get_path (); 118 FcConfigSetSysRoot(config, bundle); 119 120 string path = FcConfigFilename((!) SearchPaths.search_file(null, "fontconfig.settings").get_path ()); 121 bool loaded = FcConfigParseAndLoad(config, path, true); 122 123 if (!loaded) { 124 warning ("Fontconfig initialization failed."); 125 } 126 127 FcConfigSetCurrent (config); 128 #else 129 config = FcInitLoadConfigAndFonts (); 130 #endif 131 132 IdleSource idle = new IdleSource (); 133 134 idle.set_callback (() => { 135 font_config = config; 136 printd("Fontconfig loaded\n"); 137 return false; 138 }); 139 idle.attach (null); 140 } 141 142 public Font get_single_glyph_font (unichar c) { 143 Font f; 144 unichar last; 145 CachePair p; 146 147 if (likely (glyphs.has_key (c))) { 148 p = glyphs.get (c); 149 150 if (p.referenced < int.MAX) { 151 p.referenced++; 152 } 153 154 return p.font; 155 } 156 157 // remove glyphs from cache if it is full 158 if (cached.size > max_cached_fonts - 100) { 159 160 cached.sort ((a, b) => { 161 CachePair pa = (CachePair) a; 162 CachePair pb = (CachePair) b; 163 return pb.referenced - pa.referenced; 164 }); 165 166 int j = 0; 167 for (int i = cached.size - 1; i > 0; i--) { 168 if (j > 100) { 169 break; 170 } 171 172 j++; 173 174 last = cached.get (i).character; 175 glyphs.unset (last); 176 cached.remove_at (i); 177 } 178 } 179 180 f = get_single_fallback_glyph_font (c); 181 p = new CachePair (f, c); 182 183 glyphs.set (c, p); 184 cached.add (p); 185 186 return (Font) f; 187 } 188 189 Font get_single_fallback_glyph_font (unichar c) { 190 string? font_file; 191 BirdFontFile bf_parser; 192 Font bf_font; 193 StringBuilder? glyph_data; 194 FontFace* font; 195 196 bf_font = new Font (); 197 font_file = null; 198 glyph_data = null; 199 200 // don't use fallback font in private use area 201 if (0xe000 <= c <= 0xf8ff) { 202 return bf_font; 203 } 204 205 // control characters 206 if (c <= 0x001f || (0x007f <= c <= 0x008d)) { 207 return bf_font; 208 } 209 210 // check if glyph is available in roboto 211 if (default_font != null) { 212 glyph_data = get_glyph_in_font ((!) default_font, c); 213 } 214 215 // use fontconfig to find a fallback font 216 if (glyph_data == null) { 217 font_file = find_font (font_config, (!) c.to_string ()); 218 if (font_file != null) { 219 font = open_font ((!) font_file); 220 glyph_data = get_glyph_in_font (font, c); 221 close_font (font); 222 } 223 } 224 225 if (glyph_data != null) { 226 bf_parser = new BirdFontFile (bf_font); 227 bf_parser.load_data (((!) glyph_data).str); 228 } 229 230 return bf_font; 231 } 232 233 public StringBuilder? get_glyph_in_font (FontFace* font, unichar c) { 234 StringBuilder? glyph_data = null; 235 GlyphCollection gc; 236 237 gc = new GlyphCollection (c, (!)c.to_string ()); 238 glyph_data = load_glyph (font, (uint) c); 239 240 return glyph_data; 241 } 242 243 void add_font_folder (string f) { 244 File folder = File.new_for_path (f); 245 FileInfo? file_info; 246 string fn; 247 string file_attributes; 248 try { 249 if (folder.query_exists ()) { 250 font_directories.add (folder); 251 252 file_attributes = FileAttribute.STANDARD_NAME; 253 file_attributes += ","; 254 file_attributes += FileAttribute.STANDARD_TYPE; 255 var enumerator = folder.enumerate_children (file_attributes, 0); 256 257 while ((file_info = enumerator.next_file ()) != null) { 258 fn = ((!) file_info).get_name (); 259 260 if (((!)file_info).get_file_type () == FileType.DIRECTORY) { 261 add_font_folder ((!) get_child (folder, fn).get_path ()); 262 } 263 } 264 } 265 } catch (GLib.Error e) { 266 warning (e.message); 267 } 268 } 269 270 File search_font_file (string font_file) { 271 File d, f; 272 273 for (int i = font_directories.size - 1; i >= 0; i--) { 274 d = font_directories.get (i); 275 f = get_child (d, font_file); 276 277 if (f.query_exists ()) { 278 return f; 279 } 280 } 281 282 warning (@"The font $font_file not found"); 283 return File.new_for_path (font_file); 284 } 285 286 public string? get_default_font_file () { 287 File font_file; 288 string? fn = null; 289 290 if (likely (default_font_file != null)) { 291 return default_font_file; 292 } 293 294 font_file = SearchPaths.search_file (null, default_font_file_name); 295 296 if (font_file.query_exists ()) { 297 fn = (!) font_file.get_path (); 298 } else { 299 font_file = search_font_file (default_font_file_name); 300 301 if (font_file.query_exists ()) { 302 fn = (!) font_file.get_path (); 303 } else { 304 fn = find_font_file (font_config, default_font_family_name); 305 } 306 } 307 308 if (likely (fn != null)) { 309 default_font_file = fn; 310 return fn; 311 } 312 313 warning(default_font_family_name + " not found"); 314 return null; 315 } 316 317 void open_default_font () { 318 string? fn = get_default_font_file (); 319 320 if (fn != null) { 321 default_font = open_font ((!) fn); 322 } 323 } 324 325 class CachePair : GLib.Object { 326 public Font font; 327 public unichar character; 328 public int referenced = 1; 329 330 public CachePair (Font f, unichar c) { 331 font = f; 332 character = c; 333 } 334 } 335 } 336 337 } 338