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.
Load font metrix in for each fallback glyph
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, Font> fonts; 25 26 public FontCache () { 27 fonts = new Gee.HashMap<string, Font> (); 28 29 if (is_null (fallback_font)) { 30 fallback_font = new FallbackFont (); 31 } 32 } 33 34 public void reload_font (string file_name) { 35 Font? f = get_font (file_name).font; 36 37 if (f != null) { 38 ((!) f).load (); 39 } 40 } 41 42 public CachedFont get_font (string file_name) { 43 Font f; 44 bool ok; 45 46 if (file_name == "") { 47 stderr.printf ("No file name.\n"); 48 return new CachedFont (null); 49 } 50 51 if (fonts.has_key (file_name)) { 52 return new CachedFont (fonts.get (file_name)); 53 } 54 55 f = new Font (); 56 f.set_file (file_name); 57 ok = f.load (); 58 if (!ok) { 59 stderr.printf ("Can't load %s\n", file_name); 60 return new CachedFont (null); 61 } 62 63 fonts.set (file_name, f); 64 65 return new CachedFont (f); 66 } 67 68 public static FontCache get_default_cache () { 69 if (default_cache == null) { 70 default_cache = new FontCache (); 71 } 72 73 return (!) default_cache; 74 } 75 76 public CachedFont get_fallback () { 77 return new CachedFont (null); 78 } 79 80 public class CachedFont : GLib.Object { 81 public Font? font; 82 83 // FIXME: move fallback glyphs in to fond boundaries 84 public double top_limit = 84; 85 public double base_line = 0; 86 public double bottom_limit = -27; 87 88 public CachedFont (Font? font) { 89 this.font = font; 90 } 91 92 public Glyph? get_glyph_by_name (string name) { 93 Glyph? g = null; 94 95 if (font != null) { 96 g = ((!) font).get_glyph_by_name (name); 97 } 98 99 if (g == null && name.char_count () == 1) { 100 g = fallback_font.get_glyph (name.get_char (0)); 101 } 102 103 return g; 104 } 105 106 public GlyphCollection get_not_def_character () { 107 Font f; 108 109 if (font != null) { 110 return ((!) font).get_not_def_character (); 111 } 112 113 f = new Font (); 114 115 return f.get_not_def_character (); 116 } 117 } 118 } 119 120 } 121