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 }
35
36 public CachedFont get_font (string file_name) {
37 CachedFont c;
38 Font f;
39 bool ok;
40
41 if (file_name == "") {
42 return fallback;
43 }
44
45 if (fonts.has_key (file_name)) {
46 c = fonts.get (file_name);
47 return c;
48 }
49
50 f = new Font ();
51 f.set_file (file_name);
52 ok = f.load ();
53 if (!ok) {
54 stderr.printf ("Can't load %s\n", file_name);
55 return new CachedFont (null);
56 }
57
58 c = new CachedFont (f);
59
60 if (file_name == "") {
61 warning ("No file.");
62 return c;
63 }
64
65 fonts.set (file_name, c);
66 return c;
67 }
68
69 public static FontCache get_default_cache () {
70 if (default_cache == null) {
71 default_cache = new FontCache ();
72 }
73
74 return (!) default_cache;
75 }
76
77 public CachedFont get_fallback () {
78 return fallback;
79 }
80
81 }
82
83 }
84