annotate mde/resource/font.d @ 44:07bd1a09e161

Started implementing text rendering. Can now position glyphs accurately and render them, in a very basic way. A basic TextWidget. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 16 May 2008 12:22:10 +0100
parents
children 0fd51d2c6c8a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
1 /* LICENSE BLOCK
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
2 Part of mde: a Modular D game-oriented Engine
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
3 Copyright © 2007-2008 Diggory Hardy
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
4
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify it under the terms
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
6 of the GNU General Public License as published by the Free Software Foundation, either
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
7 version 2 of the License, or (at your option) any later version.
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
8
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
11 See the GNU General Public License for more details.
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
12
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
13 You should have received a copy of the GNU General Public License
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
15
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
16 /// Sets up freetype (in a basic way).
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
17 module mde.resource.font;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
18
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
19 import mde.resource.exception;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
20
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
21 import derelict.freetype.ft;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
22 import derelict.opengl.gl;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
23
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
24 import tango.stdc.stringz;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
25 import tango.util.log.Log : Log, Logger;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
26
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
27 private Logger logger;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
28 static this () {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
29 logger = Log.getLogger ("mde.resource.font");
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
30 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
32 /** Font class.
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
33 *
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
34 * Particular to a font and size. (Maybe not size?) */
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35 class Font
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
36 {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
37 //BEGIN Static: manager
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
38 static {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
39 /** Load the freetype library. */
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
40 void initialize () {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
41 if (FT_Init_FreeType (&library))
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
42 throw new fontException ("error initialising the FreeType library");
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
43 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
45 //FIXME: don't use GC for Font resources
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
46 /** Cleanup: delete all fonts. */
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
47 void cleanup () {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
48 if (font)
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
49 delete font;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
50
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
51 FT_Done_FreeType (library);
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
52 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
53
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
54 /** Get a font.
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
55 *
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
56 * Later specify font/size.
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
57 *
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
58 * Throws:
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
59 * fontLoadException when unable to load the font. */
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
60 Font get(char[] path) {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
61 if (font is null) font = new Font(path);
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
62 return font;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
63 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
64
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
65 private:
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
66 FT_Library library;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
67 Font font;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
68 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
69 //END Static
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
70
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
71
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
72 /** Load & cache a new font. */
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
73 this (char[] path)
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
74 in {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
75 assert (library !is null, "font: library is null");
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
76 } body {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
77 if (FT_New_Face (library, toStringz(path), 0, &face))
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
78 throw new fontLoadException ("Unable to read font: "~path);
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
79
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
80 if (FT_Set_Pixel_Sizes (face, 12,12))
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
81 throw new fontLoadException ("Unable to set pixel size");
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
82 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
83
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
84 void drawStr (int x, int y, char[] str) {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
85 FT_Vector pen = { x*64, y*64 };
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
86 auto g = face.glyph;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
87
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
88 FT_Matrix m;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
89 m.xx = 0x10000;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
90 m.xy = m.yx = 0;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
91 m.yy = -0x10000;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
92
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
93 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
94
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
95 FT_Pos y_adj = 0; // y adjustment (for height)
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
96
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
97 foreach (chr; str) {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
98 FT_Set_Transform(face, &m, &pen);
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
99 if (FT_Load_Char(face, chr, FT_LOAD_RENDER))
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
100 return; // give up
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
101
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
102 if (y_adj < g.metrics.height) y_adj = g.metrics.height;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
103
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
104 auto b = g.bitmap;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
105 if (b.pixel_mode != FT_Pixel_Mode.FT_PIXEL_MODE_GRAY || b.num_grays != 256) {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
106 char[128] tmp;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
107 logger.warn (logger.format (tmp,"Unsupported freetype bitmap format: {}, {}", b.pixel_mode, b.num_grays));
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
108 return;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
109 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
110 if (b.pitch != b.width)
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
111 logger.info ("b.pitch != b.width");
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
112
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
113 //NOTE: y direction!
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
114 glRasterPos2i (g.bitmap_left,g.bitmap_top + y_adj/64);
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
115 glDrawPixels (b.width, b.rows, GL_LUMINANCE, GL_UNSIGNED_BYTE, cast(void*) b.buffer);
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
116
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
117 pen.x += g.advance.x;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
118 pen.y += g.advance.y;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
119 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
120 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
121
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
122 ~this () {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
123 FT_Done_Face (face);
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
124 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
125
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
126 private:
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
127 FT_Face face;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
128 }