Mercurial > projects > mde
comparison mde/font/FontTexture.d @ 100:0ea4a3e651ae
There is now a position marker for text editing.
Changed the way fonts are configured. Actually, not much of the new way exists yet.
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Sat, 15 Nov 2008 17:39:14 +0000 |
parents | 49e7cfed4b34 |
children | ee209602770d |
comparison
equal
deleted
inserted
replaced
99:5de5810e3516 | 100:0ea4a3e651ae |
---|---|
58 } | 58 } |
59 cachedGlyphs = null; | 59 cachedGlyphs = null; |
60 ++cacheVer; | 60 ++cacheVer; |
61 } | 61 } |
62 | 62 |
63 | |
64 /** Cache informatation for rendering a block of text. | 63 /** Cache informatation for rendering a block of text. |
65 * | 64 * |
66 * Recognises '\r', '\n' and "\r\n" as end-of-line markers. */ | 65 * Recognises '\r', '\n' and "\r\n" as end-of-line markers. */ |
67 void updateCache (FT_Face face, char[] str, ref TextBlock cache) | 66 void updateCache (FT_Face face, char[] str, ref TextBlock cache) |
68 { | 67 { |
71 | 70 |
72 if (cache.cacheVer == cacheVer) // Existing cache is up-to-date | 71 if (cache.cacheVer == cacheVer) // Existing cache is up-to-date |
73 return; | 72 return; |
74 | 73 |
75 cache.cacheVer = cacheVer; | 74 cache.cacheVer = cacheVer; |
75 cache.w = cache.h = 0; // reset | |
76 | 76 |
77 /* Convert the string to an array of character codes (which is equivalent to decoding UTF8 | 77 /* Convert the string to an array of character codes (which is equivalent to decoding UTF8 |
78 * to UTF32 since no character code is ever > dchar.max). */ | 78 * to UTF32 since no character code is ever > dchar.max). */ |
79 static dchar[] chrs; // keep memory for future calls (NOTE: change for threading) | 79 static dchar[] chrs; // keep memory for future calls (NOTE: change for threading) |
80 chrs = Utf.toString32 (str, chrs); | 80 chrs = Utf.toString32 (str, chrs); |
148 * invalid) | 148 * invalid) |
149 * str = Text to render (only needed if the cache is invalid) | 149 * str = Text to render (only needed if the cache is invalid) |
150 * cache = Cache used to speed up CPU-side rendering code | 150 * cache = Cache used to speed up CPU-side rendering code |
151 * x = Smaller x-coordinate of position | 151 * x = Smaller x-coordinate of position |
152 * y = Smaller y-coordinate of position | 152 * y = Smaller y-coordinate of position |
153 * col = Text colour (note: currently limited to black or white) */ | 153 * col = Text colour (note: currently limited to black or white) |
154 void drawCache (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col ) { | 154 * index = Index of edit position, or size_t.max if no idet position. */ |
155 void drawCache (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col, size_t index) { | |
155 updateCache (face, str, cache); // update if necessary | 156 updateCache (face, str, cache); // update if necessary |
156 debug scope (failure) | 157 debug scope (failure) |
157 logger.error ("drawTextCache failed"); | 158 logger.error ("drawTextCache failed"); |
158 | 159 |
159 // opaque (GL_DECAL would be equivalent) | 160 // opaque (GL_DECAL would be equivalent) |
160 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); | 161 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
161 | 162 |
162 drawCacheImpl (cache, x, y, col); | 163 drawCacheImpl (face, cache, x, y, col, index); |
163 } | 164 } |
164 /** A variation of drawCache, for transparent text. | 165 /** A variation of drawCache, for transparent text. |
165 * | 166 * |
166 * Instead of passing the alpha value(s) as arguments, set the openGL colour prior to calling: | 167 * Instead of passing the alpha value(s) as arguments, set the openGL colour prior to calling: |
167 * --- | 168 * --- |
171 * glColor3ub (0, 255, 127); // alpha 0 for red, 1 for green, half for blue | 172 * glColor3ub (0, 255, 127); // alpha 0 for red, 1 for green, half for blue |
172 * drawCacheA (face, ...); | 173 * drawCacheA (face, ...); |
173 * --- | 174 * --- |
174 * | 175 * |
175 * The overhead of the transparency is minimal. */ | 176 * The overhead of the transparency is minimal. */ |
176 void drawCacheA (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col/+ = Colour.WHITE+/) { | 177 void drawCacheA (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col, size_t index) { |
177 updateCache (face, str, cache); // update if necessary | 178 updateCache (face, str, cache); // update if necessary |
178 debug scope (failure) | 179 debug scope (failure) |
179 logger.error ("drawTextCache failed"); | 180 logger.error ("drawTextCache failed"); |
180 | 181 |
181 // transparency alpha | 182 // transparency alpha |
182 // alpha is current colour, per component | 183 // alpha is current colour, per component |
183 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); | 184 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
184 | 185 |
185 drawCacheImpl (cache, x, y, col); | 186 drawCacheImpl (face, cache, x, y, col, index); |
186 } | 187 } |
187 | 188 |
188 private void drawCacheImpl (ref TextBlock cache, int x, int y, Colour col) { | 189 private void drawCacheImpl (FT_Face face, ref TextBlock cache, int x, int y, Colour col, size_t index) { |
189 if (DerelictGL.availableVersion() >= GLVersion.Version14) { | 190 if (DerelictGL.availableVersion() >= GLVersion.Version14) { |
190 glBlendFunc (GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR); | 191 glBlendFunc (GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR); |
191 glBlendColor(col.r, col.g, col.b, 1f); // text colour | 192 glBlendColor(col.r, col.g, col.b, 1f); // text colour |
192 } else | 193 } else |
193 glBlendFunc (col.nearestGLConst, GL_ONE_MINUS_SRC_COLOR); | 194 glBlendFunc (col.nearestGLConst, GL_ONE_MINUS_SRC_COLOR); |
214 glTexCoord2f (tx2, ty1); glVertex2i (x2, y1); | 215 glTexCoord2f (tx2, ty1); glVertex2i (x2, y1); |
215 glTexCoord2f (tx2, ty2); glVertex2i (x2, y2); | 216 glTexCoord2f (tx2, ty2); glVertex2i (x2, y2); |
216 glTexCoord2f (tx1, ty2); glVertex2i (x1, y2); | 217 glTexCoord2f (tx1, ty2); glVertex2i (x1, y2); |
217 glEnd (); | 218 glEnd (); |
218 } | 219 } |
219 | 220 glDisable(GL_TEXTURE_2D); |
220 glDisable(GL_TEXTURE_2D); | |
221 glDisable(GL_BLEND); | 221 glDisable(GL_BLEND); |
222 | |
223 if (index <= cache.chars.length) { // draw edit position | |
224 int x1; | |
225 if (index == 0) | |
226 x1 = x + 1; | |
227 else { | |
228 auto c = cache.chars[index-1]; | |
229 x1 = x + c.xPos + c.ga.advanceX; | |
230 } | |
231 glColor3f(col.r, col.g, col.b); | |
232 glRecti(x1 - 1, y, x1, y + (face.size.metrics.height >> 6)); | |
233 } | |
222 } | 234 } |
223 | 235 |
224 void addGlyph (FT_Face face, dchar chr) { | 236 void addGlyph (FT_Face face, dchar chr) { |
225 debug scope (failure) | 237 debug scope (failure) |
226 logger.error ("FontTexture.addGlyph failed!"); | 238 logger.error ("FontTexture.addGlyph failed!"); |
444 * FT_LOAD_TARGET_LCD_V (0x40000) | 456 * FT_LOAD_TARGET_LCD_V (0x40000) |
445 * The mode FT_LOAD_TARGET_MONO (0x20000) is unsupported. | 457 * The mode FT_LOAD_TARGET_MONO (0x20000) is unsupported. |
446 * | 458 * |
447 * lcdFilter should come from enum FT_LcdFilter: | 459 * lcdFilter should come from enum FT_LcdFilter: |
448 * FT_LCD_FILTER_NONE (0), FT_LCD_FILTER_DEFAULT (1), FT_LCD_FILTER_LIGHT (2) */ | 460 * FT_LCD_FILTER_NONE (0), FT_LCD_FILTER_DEFAULT (1), FT_LCD_FILTER_LIGHT (2) */ |
449 mixin (impl!("int renderMode, lcdFilter;")); | 461 mixin (impl!("int renderMode, lcdFilter, defaultSize; char[] defaultFont;")); |
462 | |
463 void validate() { | |
464 } | |
450 | 465 |
451 static this() { | 466 static this() { |
452 fontOpts = new FontOptions; | 467 fontOpts = new FontOptions; |
453 Options.addOptionsClass (fontOpts, "FontOptions"); | 468 Options.addOptionsClass (fontOpts, "FontOptions"); |
454 } | 469 } |
467 | 482 |
468 /** Cached information for drawing a block of text. | 483 /** Cached information for drawing a block of text. |
469 * | 484 * |
470 * Struct should be stored externally and updated via references. */ | 485 * Struct should be stored externally and updated via references. */ |
471 struct TextBlock { | 486 struct TextBlock { |
472 CharCache[] chars; // All chars. They hold x & y pos. info, so don't need to know about lines. | 487 CharCache[] chars; /// All chars. They hold x & y pos. info, so don't need to know about lines. |
473 int cacheVer = -1; // this is checked on access, and must equal for cache to be valid. | 488 int cacheVer = -1; /// Checked on access; must equalFontTexture.cacheVer or the cache is rebuilt. |
474 int w, h; /// Size of the block. Likely the only fields of use outside the library. | 489 int w, h; /// Size of the block. |
475 } | 490 } |
476 struct CharCache { | 491 struct CharCache { |
477 GlyphAttribs* ga; // character | 492 GlyphAttribs* ga; /// The character |
478 int xPos, yPos; // x,y position | 493 int xPos, yPos; /// Character's x,y position |
479 } | 494 } |