The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

FontCache.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/FontCache.vala.
Remove debug info
1 /* 2 Copyright (C) 2014 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 namespace BirdFont { 18 19 /** Thread specific font cache. */ 20 public class FontCache { 21 public static FallbackFont fallback_font; 22 23 static FontCache? default_cache = null; 24 Gee.HashMap<string, CachedFont> fonts; 25 CachedFont fallback; 26 27 public FontCache () { 28 if (is_null (fallback_font)) { 29 fallback_font = new FallbackFont (); 30 } 31 32 fallback = new CachedFont (null); 33 fonts = new Gee.HashMap<string, CachedFont> (); 34 fonts.set ("", fallback); 35 } 36 37 public void reload_font (string file_name) { 38 Font? f = get_font (file_name).font; 39 40 if (f != null) { 41 ((!) f).load (); 42 } 43 } 44 45 public CachedFont get_font (string file_name) { 46 CachedFont c; 47 Font f; 48 bool ok; 49 50 if (fonts.has_key (file_name)) { 51 return fonts.get (file_name); 52 } 53 54 if (file_name == "") { 55 return fallback; 56 } 57 58 f = new Font (); 59 f.set_file (file_name); 60 ok = f.load (); 61 if (!ok) { 62 stderr.printf ("Can't load %s\n", file_name); 63 return new CachedFont (null); 64 } 65 66 c = new CachedFont (f); 67 fonts.set (file_name, c); 68 69 return c; 70 } 71 72 public static FontCache get_default_cache () { 73 if (default_cache == null) { 74 default_cache = new FontCache (); 75 } 76 77 return (!) default_cache; 78 } 79 80 public CachedFont get_fallback () { 81 return fallback; 82 } 83 84 public class CachedFont : GLib.Object { 85 public Font? font; 86 87 // FIXME: move fallback glyphs in to fond boundaries 88 public double top_limit = 84; 89 public double base_line = 0; 90 public double bottom_limit = -27; 91 92 public static int cached = 0; 93 94 public CachedFont (Font? font) { 95 this.font = font; 96 cached++; 97 98 warning (@"$cached cached fonts\n"); 99 } 100 101 ~CachedFont () { 102 cached--; 103 warning (@"$cached cached fonts\n"); 104 } 105 106 public Glyph? get_glyph_by_name (string name) { 107 Font f = new Font (); 108 Glyph? g = null; 109 110 if (font != null) { 111 g = ((!) font).get_glyph_by_name (name); 112 } 113 114 if (g == null && name.char_count () == 1) { 115 f = fallback_font.get_single_glyph_font (name.get_char (0)); 116 g = f.get_glyph_by_name (name); 117 118 if (g == null) { 119 return null; 120 } 121 122 top_limit = f.top_limit; 123 base_line = f.base_line; 124 bottom_limit = f.bottom_limit; 125 } 126 127 return g; 128 } 129 130 public GlyphCollection get_not_def_character () { 131 Font f; 132 133 if (font != null) { 134 return ((!) font).get_not_def_character (); 135 } 136 137 f = new Font (); 138 139 return f.get_not_def_character (); 140 } 141 } 142 } 143 144 } 145