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.
Fix memory leak
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 (file_name == "") { 51 return fallback; 52 } 53 54 if (fonts.has_key (file_name)) { 55 return fonts.get (file_name); 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 99 ~CachedFont () { 100 cached--; 101 } 102 103 public Glyph? get_glyph_by_name (string name) { 104 Font f = new Font (); 105 Glyph? g = null; 106 107 if (font != null) { 108 g = ((!) font).get_glyph_by_name (name); 109 } 110 111 if (g == null && name.char_count () == 1) { 112 f = fallback_font.get_single_glyph_font (name.get_char (0)); 113 g = f.get_glyph_by_name (name); 114 115 if (g == null) { 116 return null; 117 } 118 119 top_limit = f.top_limit; 120 base_line = f.base_line; 121 bottom_limit = f.bottom_limit; 122 } 123 124 return g; 125 } 126 127 public GlyphCollection get_not_def_character () { 128 Font f; 129 130 if (font != null) { 131 return ((!) font).get_not_def_character (); 132 } 133 134 f = new Font (); 135 136 return f.get_not_def_character (); 137 } 138 } 139 } 140 141 } 142