.
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 (fonts.has_key (file_name)) {
51 return fonts.get (file_name);
52 }
53
54 if (file_name == "") {
55 return fallback;
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 warning (@"$cached cached fonts\n");
99 }
100
101 ~CachedFont () {
102 cached--;
103 warning (@"$cached cached fonts\n");
104 }
105
106 public Glyph? get_glyph_by_name (string name) {
107 Font f = new Font ();
108 Glyph? g = null;
109
110 if (font != null) {
111 g = ((!) font).get_glyph_by_name (name);
112 }
113
114 if (g == null && name.char_count () == 1) {
115 return null; //FIXME
116
117 f = fallback_font.get_single_glyph_font (name.get_char (0));
118 g = f.get_glyph_by_name (name);
119
120 if (g == null) {
121 return null;
122 }
123
124 top_limit = f.top_limit;
125 base_line = f.base_line;
126 bottom_limit = f.bottom_limit;
127
128 GlyphCollection? gc = f.get_glyph_collection (name);
129 if (gc != null) {
130 print (@"GC BEFORE UNREF: $(((!)gc).ref_count)\n");
131 }
132
133 print (@"before bad unref: $(((!)g).ref_count)\n");
134 //((!)g).unref (); //FIXME:
135 f = new Font (); // FIXME DELTE
136 print (@"after bad unref: $(((!)g).ref_count)\n");
137
138 if (gc != null) {
139 print (@"GC AFTER UNREF: $(((!)gc).ref_count)\n");
140 }
141
142 }
143
144 print (@"font in get_glyph_by_name: $(f.ref_count)\n");
145
146
147 if (g != null) {
148 print (@"b: $(((!)g).ref_count)\n");
149 }
150
151
152 Glyph tg;
153
154 // FIXME: DELETE
155 tg = new Glyph ("");
156 print (@"tg: $(tg.ref_count)\n");
157
158 {
159 GlyphCollection tgc = new GlyphCollection ('\0', "");
160 tgc.add_glyph (tg);
161
162 print (@"after add tg: $(tg.ref_count)\n");
163
164 tgc.versions.glyphs.clear ();
165 }
166
167 print (@"DONE tg: $(tg.ref_count)\n");
168
169 return g;
170 }
171
172 public GlyphCollection get_not_def_character () {
173 Font f;
174
175 if (font != null) {
176 return ((!) font).get_not_def_character ();
177 }
178
179 f = new Font ();
180
181 return f.get_not_def_character ();
182 }
183 }
184 }
185
186 }
187