comparison mde/font/font.d @ 89:97e6dce08037

Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
author Diggory Hardy <diggory.hardy@gmail.com>
date Mon, 29 Sep 2008 18:27:17 +0100
parents 79d816b3e2d2
children 30470bc19ca4
comparison
equal deleted inserted replaced
88:01f4f5f1acc9 89:97e6dce08037
125 fallback.load; 125 fallback.load;
126 126
127 return StageState.ACTIVE; 127 return StageState.ACTIVE;
128 } 128 }
129 129
130 //FIXME: don't use GC for FontStyle resources
131 /** Cleanup: delete all fonts. */ 130 /** Cleanup: delete all fonts. */
132 StageState cleanup () { 131 StageState cleanup () {
133 // Clear loaded fonts (each has an FT_Face object needing to be freed): 132 // Clear loaded fonts (each has an FT_Face object needing to be freed):
134 foreach (fs; fonts) 133 foreach (fs; fonts)
135 delete fs; 134 fs.freeFace;
136 135
137 FT_Done_FreeType (library); // free the library 136 FT_Done_FreeType (library); // free the library
138 137
139 return StageState.INACTIVE; 138 return StageState.INACTIVE;
140 } 139 }
235 } 234 }
236 235
237 /** Draw a block of text (may inlcude new-lines). 236 /** Draw a block of text (may inlcude new-lines).
238 * 237 *
239 * The text block is drawn with top-left corner at x,y. To put the text's baseline at a given 238 * The text block is drawn with top-left corner at x,y. To put the text's baseline at a given
240 * y coordinate would require some changes. Line height is currently variable, depending on the 239 * y coordinate would require some changes. Line height is fixed based on largest glyph.
241 * highest glyph in the line (should probably be fixed: FIXME). 240 * Due to hinter, glyphs are not guaranteed to lie within the "bounding box" defined by cache.
242 * 241 * Can be changed to test size of each glyph if necessary.
243 * Specify the text's colour with col; currently this is only Colour.WHITE or Colour.BLACK 242 *
244 * (FIXME). FIXME: add alpha support. 243 * Specify the text's colour with col. Use textBlockA() instead for transparent text.
245 * 244 *
246 * As a CPU-side code optimisation, store a TextBlock (unique to str) and pass a reference as 245 * As a CPU-side code optimisation, store a TextBlock (unique to str) and pass a reference as
247 * the cache argument. This is the recommended method, although for one-time calls when you 246 * the cache argument. This is the recommended method, although for one-time calls when you
248 * don't need to know the size, the other version of textBlock may be used. 247 * don't need to know the size, the other version of textBlock may be used.
249 * --------------------------------- 248 * ---------------------------------
259 * than this cache only serves as a small optimisation. However, the only way to get the size 258 * 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 259 * of a text block is to use a TextBlock cache and update it, either with this function or with
261 * the updateBlock function. */ 260 * the updateBlock function. */
262 void textBlock (int x, int y, char[] str, ref TextBlock cache, Colour col) 261 void textBlock (int x, int y, char[] str, ref TextBlock cache, Colour col)
263 in { 262 in {
264 debug assert (face, "FontStyle: face is null"); 263 assert (face, "FontStyle: face is null");
265 } body { 264 } body {
266 try { 265 try {
267 fontTex.drawCache (face, str, cache, x, y, col); 266 fontTex.drawCache (face, str, cache, x, y, col);
268 } catch (Exception e) { 267 } catch (Exception e) {
269 logger.warn ("Exception while drawing text: "~e.msg); 268 logger.warn ("Exception while drawing text: "~e.msg);
270 } 269 }
271 } 270 }
272 /** ditto */ 271 /** ditto */
273 void textBlock (int x, int y, char[] str, Colour col) 272 void textBlock (int x, int y, char[] str, Colour col)
274 in { 273 in {
275 debug assert (face, "FontStyle: face is null"); 274 assert (face, "FontStyle: face is null");
276 } body { 275 } body {
277 try { 276 try {
278 // Using the cache method for one-time use is slightly less than optimal, but doing so 277 // Using the cache method for one-time use is slightly less than optimal, but doing so
279 // isn't really recommended anyway (and maintaining two versions of fontTex.drawText 278 // isn't really recommended anyway (and maintaining two versions of fontTex.drawText
280 // would be horrible). 279 // would be horrible).
289 * 288 *
290 * Set the alpha by calling glColor*() first. See FontTexture.drawCacheA()'s documentation for 289 * Set the alpha by calling glColor*() first. See FontTexture.drawCacheA()'s documentation for
291 * details. */ 290 * details. */
292 void textBlockA (int x, int y, char[] str, ref TextBlock cache, Colour col) 291 void textBlockA (int x, int y, char[] str, ref TextBlock cache, Colour col)
293 in { 292 in {
294 debug assert (face, "FontStyle: face is null"); 293 assert (face, "FontStyle: face is null");
295 } body { 294 } body {
296 try { 295 try {
297 fontTex.drawCacheA (face, str, cache, x, y, col); 296 fontTex.drawCacheA (face, str, cache, x, y, col);
298 } catch (Exception e) { 297 } catch (Exception e) {
299 logger.warn ("Exception while drawing text: "~e.msg); 298 logger.warn ("Exception while drawing text: "~e.msg);
300 } 299 }
301 } 300 }
302 301
303 /** The font-specified vertical distance between the baseline of consecutive lines. */ 302 /** The font-specified vertical distance between the baseline of consecutive lines. */
304 int getLineSeparation () { 303 int getLineSeparation ()
304 in {
305 assert (face, "FontStyle: face is null");
306 } body {
305 return face.size.metrics.height >> 6; 307 return face.size.metrics.height >> 6;
306 } 308 }
307 309
308 ~this () { 310 void freeFace () {
309 FT_Done_Face (face); 311 FT_Done_Face (face);
312 face = null; // functions using face use assertions on face to check its validity.
310 } 313 }
311 314
312 private: 315 private:
313 char[] path; // path to font file 316 char[] path; // path to font file
314 int size; // font size 317 int size; // font size