Commit 5f75537
Load font metrix in for each fallback glyph
Changed files (3)
modifiedlibbirdfont/OverViewItem.valamodifiedlibbirdfont/Renderer/FallbackFont.valamodifiedlibbirdfont/Renderer/FontCache.vala
Diff
@@ -156,6 +156,7 @@ public class OverViewItem : GLib.Object { private void draw_thumbnail (Context cr, GlyphCollection? gl, double x, double y) { Glyph g;+ Glyph? glyph; Font font; double gx, gy; double x1, x2, y1, y2;@@ -167,7 +168,6 @@ public class OverViewItem : GLib.Object { OverView o; Color color = Color.black (); - font = BirdFont.get_current_font (); w = width; h = height; @@ -177,30 +177,41 @@ public class OverViewItem : GLib.Object { c = new Context (s); if (gl != null) {+ font = BirdFont.get_current_font (); g = ((!) gl).get_current ();- } else {- o = MainWindow.get_overview ();- g = FontCache.fallback_font.get_glyph (character);- color = Theme.get_color ("Overview Glyph");- }- - g.boundaries (out x1, out y1, out x2, out y2);- - glyph_width = x2 - x1;- glyph_height = y2 - y1;++ c.save ();+ g.boundaries (out x1, out y1, out x2, out y2); - gx = ((w / glyph_scale) - glyph_width) / 2;- gy = (h / glyph_scale) - 25 / glyph_scale;+ glyph_width = x2 - x1;+ glyph_height = y2 - y1;+ + gx = ((w / glyph_scale) - glyph_width) / 2;+ gy = (h / glyph_scale) - 25 / glyph_scale; - c.save ();- c.scale (glyph_scale, glyph_scale); + c.save ();+ c.scale (glyph_scale, glyph_scale); - g.add_help_lines ();- - c.translate (gx - g.get_lsb () - Glyph.xc (), g.get_baseline () + gy - Glyph.yc ());- - g.draw_paths (c, color);- c.restore ();+ g.add_help_lines ();+ + c.translate (gx - g.get_lsb () - Glyph.xc (), g.get_baseline () + gy - Glyph.yc ());+ + g.draw_paths (c, color);+ c.restore ();+ } else {+ c.save ();+ Text fallback = new Text ();+ Theme.text_color (fallback, "Overview Glyph");+ fallback.set_text ((!) character.to_string ());+ double font_size = height * 0.8;+ fallback.set_font_size (font_size);++ gx = (width - fallback.get_extent ()) / 2.0;+ gy = height - 30;+ fallback.set_font_size (font_size);+ fallback.draw_at_baseline (c, gx, gy);+ c.restore ();+ } cr.save (); cr.set_source_surface (s, x, y - h);@@ -110,39 +110,36 @@ public class FallbackFont : GLib.Object { } } - public Glyph get_glyph (unichar c) {- return get_glyph_from_ttf (c);+ public Font get_single_glyph_font (unichar c) {+ return load_glyph_from_ttf (c); } - public Glyph get_glyph_from_ttf (unichar c) {- Glyph? g = null;+ public Font load_glyph_from_ttf (unichar c) {+ Font? bf_font; File f; FontFace* font; + bf_font = new Font (); for (int i = fallback_fonts.size - 1; i >= 0; i--) { f = fallback_fonts.get (i); font = open_font ((!) f.get_path ());- - if (font != null) {- g = get_glyph_in_font ((!) font, c);- }- + bf_font = get_glyph_in_font ((!) font, c); close_font (font); - if (g != null) {- return (!) g;+ if (bf_font != null) {+ return (!) bf_font; } } - return new Glyph ("");+ return bf_font != null ? (!) bf_font : new Font (); } - public Glyph? get_glyph_in_font (FontFace font, unichar c) {+ public Font? get_glyph_in_font (FontFace font, unichar c) { StringBuilder? glyph_data = null; GlyphCollection gc; BirdFontFile bf_parser;- Font bf_font;+ Font bf_font = new Font (); gc = new GlyphCollection (c, (!)c.to_string ()); glyph_data = load_glyph (font, (uint) c);@@ -151,11 +148,10 @@ public class FallbackFont : GLib.Object { return null; } - bf_font = new Font (); bf_parser = new BirdFontFile (bf_font); bf_parser.load_data (((!) glyph_data).str); - return bf_font.get_glyph_by_name ((!) c.to_string ());+ return bf_font; } public File get_database_file () {@@ -21,14 +21,17 @@ public class FontCache { public static FallbackFont fallback_font; static FontCache? default_cache = null;- Gee.HashMap<string, Font> fonts;+ Gee.HashMap<string, CachedFont> fonts;+ CachedFont fallback; public FontCache () {- fonts = new Gee.HashMap<string, Font> ();- if (is_null (fallback_font)) { fallback_font = new FallbackFont (); }+ + fallback = new CachedFont (null);+ fonts = new Gee.HashMap<string, CachedFont> ();+ fonts.set ("", fallback); } public void reload_font (string file_name) {@@ -40,16 +43,16 @@ public class FontCache { } public CachedFont get_font (string file_name) {+ CachedFont c; Font f; bool ok;- - if (file_name == "") {- stderr.printf ("No file name.\n");- return new CachedFont (null);++ if (fonts.has_key (file_name)) {+ return fonts.get (file_name); } - if (fonts.has_key (file_name)) {- return new CachedFont (fonts.get (file_name));+ if (file_name == "") {+ return fallback; } f = new Font ();@@ -60,9 +63,10 @@ public class FontCache { return new CachedFont (null); } - fonts.set (file_name, f);+ c = new CachedFont (f);+ fonts.set (file_name, c); - return new CachedFont (f);+ return c; } public static FontCache get_default_cache () {@@ -74,7 +78,7 @@ public class FontCache { } public CachedFont get_fallback () {- return new CachedFont (null);+ return fallback; } public class CachedFont : GLib.Object {@@ -84,12 +88,23 @@ public class FontCache { public double top_limit = 84; public double base_line = 0; public double bottom_limit = -27;- + + public static int cached = 0; + public CachedFont (Font? font) { this.font = font;+ cached++;+ + warning (@"$cached cached fonts\n");+ }+ + ~CachedFont () {+ cached--;+ warning (@"$cached cached fonts\n"); } public Glyph? get_glyph_by_name (string name) {+ Font f; Glyph? g = null; if (font != null) {@@ -97,7 +112,11 @@ public class FontCache { } if (g == null && name.char_count () == 1) {- g = fallback_font.get_glyph (name.get_char (0));+ f = fallback_font.get_single_glyph_font (name.get_char (0));+ g = f.get_glyph_by_name (name);+ top_limit = f.top_limit;+ base_line = f.base_line;+ bottom_limit = f.bottom_limit; } return g;