The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

CachedFont.vala in libbirdfont/TextRendering

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/TextRendering/CachedFont.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 namespace BirdFont { 18 19 public class CachedFont : GLib.Object { 20 public Font? font; 21 22 public double top_limit { 23 get { return _top_limit; } 24 set { _top_limit = value; } 25 } 26 27 public double bottom_limit { 28 get { return _bottom_limit; } 29 set { _bottom_limit = value; } 30 } 31 32 public double base_line = 0; 33 double _top_limit = 92.77; // FIXME: load before first glyph 34 double _bottom_limit = -24.4; 35 36 static FallbackFont fallback_font { 37 get { 38 if (_fallback_font == null) { 39 _fallback_font = new FallbackFont (); 40 } 41 42 return (!) _fallback_font; 43 } 44 } 45 static FallbackFont? _fallback_font = null; 46 47 public CachedFont (Font? font) { 48 Glyph? g; 49 Glyph glyph; 50 51 this.font = font; 52 53 g = get_glyph_by_name ("a"); 54 if (g != null) { 55 glyph = (!) g; 56 base_line = glyph.baseline; 57 top_limit = glyph.top_limit; 58 bottom_limit = glyph.bottom_limit; 59 } else { 60 warning("No default chararacter found in font."); 61 } 62 } 63 64 public Glyph? get_glyph_by_name (string name) { 65 Glyph? g = null; 66 Font f; 67 Glyph glyph; 68 69 if (font != null) { 70 f = (!) font; 71 g = f.get_glyph_by_name (name); 72 73 if (g != null) { 74 glyph = (!) g; 75 glyph.top_limit = f.top_limit; 76 glyph.baseline = f.base_line; 77 glyph.bottom_limit = f.bottom_limit; 78 } 79 } 80 81 if (g == null && name.char_count () == 1) { 82 f = fallback_font.get_single_glyph_font (name.get_char (0)); 83 g = f.get_glyph_by_name (name); 84 85 if (g == null) { 86 return null; 87 } 88 89 glyph = (!) g; 90 glyph.top_limit = f.top_limit; 91 glyph.baseline = f.base_line; 92 glyph.bottom_limit = f.bottom_limit; 93 } 94 95 return g; 96 } 97 } 98 99 } 100