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.
Merge ../birdfont-2.x
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 ("Cannot load fontconfig."); 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 return false; 137 }); 138 idle.attach (null); 139 } 140 141 public Font get_single_glyph_font (unichar c) { 142 Font f; 143 unichar last; 144 CachePair p; 145 146 if (likely (glyphs.has_key (c))) { 147 p = glyphs.get (c); 148 149 if (p.referenced < int.MAX) { 150 p.referenced++; 151 } 152 153 return p.font; 154 } 155 156 // remove glyphs from cache if it is full 157 if (cached.size > max_cached_fonts - 100) { 158 159 cached.sort ((a, b) => { 160 CachePair pa = (CachePair) a; 161 CachePair pb = (CachePair) b; 162 return pb.referenced - pa.referenced; 163 }); 164 165 int j = 0; 166 for (int i = cached.size - 1; i > 0; i--) { 167 if (j > 100) { 168 break; 169 } 170 171 j++; 172 173 last = cached.get (i).character; 174 glyphs.unset (last); 175 cached.remove_at (i); 176 } 177 } 178 179 f = get_single_fallback_glyph_font (c); 180 p = new CachePair (f, c); 181 182 glyphs.set (c, p); 183 cached.add (p); 184 185 return (Font) f; 186 } 187 188 Font get_single_fallback_glyph_font (unichar c) { 189 string? font_file; 190 BirdFontFile bf_parser; 191 Font bf_font; 192 StringBuilder? glyph_data; 193 FontFace* font; 194 195 bf_font = new Font (); 196 font_file = null; 197 glyph_data = null; 198 199 // don't use fallback font in private use area 200 if (0xe000 <= c <= 0xf8ff) { 201 return bf_font; 202 } 203 204 // control characters 205 if (c <= 0x001f || (0x007f <= c <= 0x008d)) { 206 return bf_font; 207 } 208 209 // check if glyph is available in roboto 210 if (default_font != null) { 211 glyph_data = get_glyph_in_font ((!) default_font, c); 212 } 213 214 // use fontconfig to find a fallback font 215 if (glyph_data == null) { 216 font_file = find_font (font_config, (!) c.to_string ()); 217 if (font_file != null) { 218 font = open_font ((!) font_file); 219 glyph_data = get_glyph_in_font (font, c); 220 close_font (font); 221 } 222 } 223 224 if (glyph_data != null) { 225 bf_parser = new BirdFontFile (bf_font); 226 bf_parser.load_data (((!) glyph_data).str); 227 } 228 229 return bf_font; 230 } 231 232 public StringBuilder? get_glyph_in_font (FontFace* font, unichar c) { 233 StringBuilder? glyph_data = null; 234 GlyphCollection gc; 235 236 gc = new GlyphCollection (c, (!)c.to_string ()); 237 glyph_data = load_glyph (font, (uint) c); 238 239 return glyph_data; 240 } 241 242 void add_font_folder (string f) { 243 File folder = File.new_for_path (f); 244 FileInfo? file_info; 245 string fn; 246 string file_attributes; 247 try { 248 if (folder.query_exists ()) { 249 font_directories.add (folder); 250 251 file_attributes = FileAttribute.STANDARD_NAME; 252 file_attributes += ","; 253 file_attributes += FileAttribute.STANDARD_TYPE; 254 var enumerator = folder.enumerate_children (file_attributes, 0); 255 256 while ((file_info = enumerator.next_file ()) != null) { 257 fn = ((!) file_info).get_name (); 258 259 if (((!)file_info).get_file_type () == FileType.DIRECTORY) { 260 add_font_folder ((!) get_child (folder, fn).get_path ()); 261 } 262 } 263 } 264 } catch (GLib.Error e) { 265 warning (e.message); 266 } 267 } 268 269 File search_font_file (string font_file) { 270 File d, f; 271 272 for (int i = font_directories.size - 1; i >= 0; i--) { 273 d = font_directories.get (i); 274 f = get_child (d, font_file); 275 276 if (f.query_exists ()) { 277 return f; 278 } 279 } 280 281 warning (@"The font $font_file not found"); 282 return File.new_for_path (font_file); 283 } 284 285 public string? get_default_font_file () { 286 File font_file; 287 string? fn = null; 288 289 if (likely (default_font_file != null)) { 290 return default_font_file; 291 } 292 293 font_file = SearchPaths.search_file (null, default_font_file_name); 294 295 if (font_file.query_exists ()) { 296 fn = (!) font_file.get_path (); 297 } else { 298 font_file = search_font_file (default_font_file_name); 299 300 if (font_file.query_exists ()) { 301 fn = (!) font_file.get_path (); 302 } else { 303 fn = find_font_file (font_config, default_font_family_name); 304 } 305 } 306 307 if (likely (fn != null)) { 308 default_font_file = fn; 309 return fn; 310 } 311 312 warning(default_font_family_name + " not found"); 313 return null; 314 } 315 316 void open_default_font () { 317 string? fn = get_default_font_file (); 318 319 if (fn != null) { 320 default_font = open_font ((!) fn); 321 } 322 } 323 324 class CachePair : GLib.Object { 325 public Font font; 326 public unichar character; 327 public int referenced = 1; 328 329 public CachePair (Font f, unichar c) { 330 font = f; 331 character = c; 332 } 333 } 334 } 335 336 } 337