comparison mde/font/font.d @ 86:79d816b3e2d2

New InitStage system, Screen & Screen.Drawable, separate testing and guiDemo binaries. This (and the previous) commit are the result of several quite significant changes to mde. All the unittests run, but it hasn't had a huge amount of testing so don't be surprised if bugs show up.
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 12 Sep 2008 17:36:14 +0100
parents 56c0ddd90193
children 97e6dce08037
comparison
equal deleted inserted replaced
85:56c0ddd90193 86:79d816b3e2d2
128 } 128 }
129 129
130 //FIXME: don't use GC for FontStyle resources 130 //FIXME: don't use GC for FontStyle resources
131 /** Cleanup: delete all fonts. */ 131 /** Cleanup: delete all fonts. */
132 StageState cleanup () { 132 StageState cleanup () {
133 // Clear loaded fonts: 133 // Clear loaded fonts (each has an FT_Face object needing to be freed):
134 foreach (fs; fonts) 134 foreach (fs; fonts)
135 delete fs; 135 delete fs;
136 fonts = null; 136
137 delete fallback;
138
139 delete fontTex; // clear texture
140 FT_Done_FreeType (library); // free the library 137 FT_Done_FreeType (library); // free the library
141 138
142 return StageState.INACTIVE; 139 return StageState.INACTIVE;
143 } 140 }
144 141
224 * The only use of this is to get the text block's size ahead of rendering, via TextBlock's w 221 * The only use of this is to get the text block's size ahead of rendering, via TextBlock's w
225 * and h properties. 222 * and h properties.
226 * 223 *
227 * This function will only actually update the cache if it is invalid, caused either by the 224 * This function will only actually update the cache if it is invalid, caused either by the
228 * font being changed or if cache.cacheVer < 0. */ 225 * font being changed or if cache.cacheVer < 0. */
229 void updateBlock (char[] str, ref TextBlock cache) { 226 void updateBlock (char[] str, ref TextBlock cache)
227 in {
228 debug assert (face, "FontStyle: face is null");
229 } body {
230 try { 230 try {
231 fontTex.updateCache (face, str, cache); 231 fontTex.updateCache (face, str, cache);
232 } catch (Exception e) { 232 } catch (Exception e) {
233 logger.warn ("Exception while drawing text: "~e.msg); 233 logger.warn ("Exception while drawing text: "~e.msg);
234 } 234 }
257 * 257 *
258 * The TextBlock's w and h properties are set to the size (in pixels) of the text block; other 258 * The TextBlock's w and h properties are set to the size (in pixels) of the text block; other
259 * than this cache only serves as a small optimisation. However, the only way to get the size 259 * than this cache only serves as a small optimisation. However, the only way to get the size
260 * of a text block is to use a TextBlock cache and update it, either with this function or with 260 * of a text block is to use a TextBlock cache and update it, either with this function or with
261 * the updateBlock function. */ 261 * the updateBlock function. */
262 void textBlock (int x, int y, char[] str, ref TextBlock cache, Colour col) { 262 void textBlock (int x, int y, char[] str, ref TextBlock cache, Colour col)
263 in {
264 debug assert (face, "FontStyle: face is null");
265 } body {
263 try { 266 try {
264 fontTex.drawCache (face, str, cache, x, y, col); 267 fontTex.drawCache (face, str, cache, x, y, col);
265 } catch (Exception e) { 268 } catch (Exception e) {
266 logger.warn ("Exception while drawing text: "~e.msg); 269 logger.warn ("Exception while drawing text: "~e.msg);
267 } 270 }
268 } 271 }
269 /** ditto */ 272 /** ditto */
270 void textBlock (int x, int y, char[] str, Colour col) { 273 void textBlock (int x, int y, char[] str, Colour col)
274 in {
275 debug assert (face, "FontStyle: face is null");
276 } body {
271 try { 277 try {
272 // Using the cache method for one-time use is slightly less than optimal, but doing so 278 // Using the cache method for one-time use is slightly less than optimal, but doing so
273 // isn't really recommended anyway (and maintaining two versions of fontTex.drawText 279 // isn't really recommended anyway (and maintaining two versions of fontTex.drawText
274 // would be horrible). 280 // would be horrible).
275 TextBlock cache; 281 TextBlock cache;
281 287
282 /** A variation of textBlock for transparency. 288 /** A variation of textBlock for transparency.
283 * 289 *
284 * Set the alpha by calling glColor*() first. See FontTexture.drawCacheA()'s documentation for 290 * Set the alpha by calling glColor*() first. See FontTexture.drawCacheA()'s documentation for
285 * details. */ 291 * details. */
286 void textBlockA (int x, int y, char[] str, ref TextBlock cache, Colour col) { 292 void textBlockA (int x, int y, char[] str, ref TextBlock cache, Colour col)
293 in {
294 debug assert (face, "FontStyle: face is null");
295 } body {
287 try { 296 try {
288 fontTex.drawCacheA (face, str, cache, x, y, col); 297 fontTex.drawCacheA (face, str, cache, x, y, col);
289 } catch (Exception e) { 298 } catch (Exception e) {
290 logger.warn ("Exception while drawing text: "~e.msg); 299 logger.warn ("Exception while drawing text: "~e.msg);
291 } 300 }
295 int getLineSeparation () { 304 int getLineSeparation () {
296 return face.size.metrics.height >> 6; 305 return face.size.metrics.height >> 6;
297 } 306 }
298 307
299 ~this () { 308 ~this () {
300 debug logger.trace ("{}.~this: start", this);
301 FT_Done_Face (face); 309 FT_Done_Face (face);
302 debug logger.trace ("{}.~this: done", this);
303 } 310 }
304 311
305 private: 312 private:
306 char[] path; // path to font file 313 char[] path; // path to font file
307 int size; // font size 314 int size; // font size