Mercurial > projects > mde
annotate mde/font/FontTexture.d @ 63:66d555da083e
Moved many modules/packages to better reflect usage.
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Fri, 27 Jun 2008 18:35:33 +0100 |
parents | mde/resource/FontTexture.d@7cab2af4ba21 |
children | 2813ac68576f |
rev | line source |
---|---|
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
1 /* LICENSE BLOCK |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
2 Part of mde: a Modular D game-oriented Engine |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
3 Copyright © 2007-2008 Diggory Hardy |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
4 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
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 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
6 of the GNU General Public License as published by the Free Software Foundation, either |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
7 version 2 of the License, or (at your option) any later version. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
8 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
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; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
11 See the GNU General Public License for more details. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
12 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
13 You should have received a copy of the GNU General Public License |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
15 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
16 /** Font caching system. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
17 * |
50
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
18 * This module also serves as the internals to the font module and shouldn't be used except through |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
19 * the font module. The two modules could be combined, at a cost to readability. |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
20 * |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
21 * Three types of coordinates get used in the system: FreeType coordinates for each glyph, texture |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
22 * coordinates, and OpenGL's model/world coordinates (for rendering). The freetype and texture |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
23 * coords are cartesian (i.e. y increases upwards), although largely this is too abstract to |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
24 * matter. However, for the model/world coords, y increases downwards. */ |
63
66d555da083e
Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
61
diff
changeset
|
25 module mde.font.FontTexture; |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
26 |
63
66d555da083e
Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
61
diff
changeset
|
27 import mde.types.Colour; |
66d555da083e
Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
61
diff
changeset
|
28 import mde.lookup.Options; |
66d555da083e
Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
61
diff
changeset
|
29 import mde.font.exception; |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
30 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
31 import derelict.freetype.ft; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
32 import derelict.opengl.gl; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
33 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
34 import Utf = tango.text.convert.Utf; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
35 import tango.util.log.Log : Log, Logger; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
36 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
37 private Logger logger; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
38 static this () { |
63
66d555da083e
Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
61
diff
changeset
|
39 logger = Log.getLogger ("mde.font.FontTexture"); |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
40 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
41 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
42 static const int dimW = 256, dimH = 256; // Texture size |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
43 const wFactor = 1f / dimW; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
44 const hFactor = 1f / dimH; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
45 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
46 /** A FontTexture is basically a cache of all font glyphs rendered so far. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
47 * |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
48 * This class should be limited to code for rendering to (and otherwise handling) textures and |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
49 * rendering fonts to the screen. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
50 * |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
51 * Technically, there's no reason it shouldn't be a static part of the FontStyle class. */ |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
52 class FontTexture |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
53 { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
54 this () {} |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
55 ~this () { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
56 foreach (t; tex) { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
57 glDeleteTextures (1, &(t.texID)); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
58 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
59 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
60 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
61 // Call if font(s) have been changed and glyphs must be recached. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
62 void clear () { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
63 foreach (t; tex) { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
64 glDeleteTextures (1, &(t.texID)); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
65 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
66 cachedGlyphs = null; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
67 ++cacheVer; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
68 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
69 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
70 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
71 /** Cache informatation for rendering a block of text. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
72 * |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
73 * Recognises '\r', '\n' and "\r\n" as end-of-line markers. */ |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
74 void updateCache (FT_Face face, char[] str, ref TextBlock cache) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
75 { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
76 debug scope (failure) |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
51
diff
changeset
|
77 logger.error ("updateCache failed"); |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
78 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
79 if (cache.cacheVer == cacheVer) // Existing cache is up-to-date |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
80 return; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
81 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
82 cache.cacheVer = cacheVer; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
83 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
84 /* Convert the string to an array of character codes (which is equivalent to decoding UTF8 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
85 * to UTF32 since no character code is ever > dchar.max). */ |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
86 static dchar[] chrs; // keep memory for future calls (NOTE: change for threading) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
87 chrs = Utf.toString32 (str, chrs); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
88 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
89 // Allocate space. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
90 // Since end-of-line chars get excluded, will often be slightly larger than necessary. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
91 cache.chars.length = chrs.length; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
92 cache.chars.length = 0; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
93 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
94 int lineSep = face.size.metrics.height >> 6; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
95 bool hasKerning = (FT_HAS_KERNING (face) != 0); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
96 int y = 0; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
97 CharCache cc; // struct; reused for each character |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
98 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
99 for (size_t i = 0; i < chrs.length; ++i) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
100 { |
49
bca7e2342d77
Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
48
diff
changeset
|
101 // First, find yMax for the current line. |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
102 int yMax = 0; // Maximal glyph height above baseline. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
103 for (size_t j = i; j < chrs.length; ++j) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
104 { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
105 if (chrs[j] == '\n' || chrs[j] == '\r') // end of line |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
106 break; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
107 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
108 GlyphAttribs* ga = chrs[j] in cachedGlyphs; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
109 if (ga is null) { // Not cached |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
110 addGlyph (face, chrs[j]); // so render it |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
111 ga = chrs[j] in cachedGlyphs; // get the ref of the copy we've stored |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
112 assert (ga !is null, "ga is null: 1"); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
113 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
114 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
115 if (ga.top > yMax) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
116 yMax = ga.top; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
117 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
118 y += yMax; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
119 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
120 // Now for the current line: |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
121 int x = 0; // x pos for next glyph |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
122 uint gi_prev = 0; // previous glyph index (needed for kerning) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
123 for (; i < chrs.length; ++i) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
124 { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
125 // If end-of-line, break to find yMax for next line. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
126 if (chrs.length >= i+2 && chrs[i..i+2] == "\r\n"d) { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
127 ++i; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
128 break; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
129 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
130 if (chrs[i] == '\n' || chrs[i] == '\r') { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
131 break; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
132 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
133 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
134 cc.ga = chrs[i] in cachedGlyphs; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
135 assert (cc.ga !is null, "ga is null: 2"); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
136 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
137 // Kerning |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
138 if (hasKerning && (gi_prev != 0)) { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
139 FT_Vector delta; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
140 FT_Get_Kerning (face, gi_prev, cc.ga.index, FT_Kerning_Mode.FT_KERNING_DEFAULT, &delta); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
141 x += delta.x >> 6; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
142 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
143 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
144 // ga.left component: adding this slightly improves glyph layout. Note that the |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
145 // left-most glyph on a line may not start right on the edge, but this looks best. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
146 cc.xPos = x + cc.ga.left; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
147 cc.yPos = y - cc.ga.top; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
148 x += cc.ga.advanceX; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
149 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
150 cache.chars ~= cc; |
49
bca7e2342d77
Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
48
diff
changeset
|
151 |
bca7e2342d77
Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
48
diff
changeset
|
152 // Update rect total size. Top and left coords should be zero, so make width and |
bca7e2342d77
Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
48
diff
changeset
|
153 // height maximal x and y coordinates. |
bca7e2342d77
Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
48
diff
changeset
|
154 if (cc.xPos + cc.ga.w > cache.w) |
bca7e2342d77
Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
48
diff
changeset
|
155 cache.w = cc.xPos + cc.ga.w; |
bca7e2342d77
Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
48
diff
changeset
|
156 if (cc.yPos + cc.ga.h > cache.h) |
bca7e2342d77
Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
48
diff
changeset
|
157 cache.h = cc.yPos + cc.ga.h; |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
158 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
159 // Now increment i and continue with the next line if there is one. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
160 y += lineSep - yMax; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
161 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
162 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
163 |
57
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
164 /** Render a block of text using a cache. Updates the cache if necessary. |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
165 * |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
166 * Params: |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
167 * face = Current typeface pointer; must be passed from font.d (only needed if the cache is |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
168 * invalid) |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
169 * str = Text to render (only needed if the cache is invalid) |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
170 * cache = Cache used to speed up CPU-side rendering code |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
171 * x = Smaller x-coordinate of position |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
172 * y = Smaller y-coordinate of position |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
173 * col = Text colour (note: currently limited to black or white) */ |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
174 void drawCache (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col ) { |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
175 updateCache (face, str, cache); // update if necessary |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
176 debug scope (failure) |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
51
diff
changeset
|
177 logger.error ("drawTextCache failed"); |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
178 |
57
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
179 // opaque (GL_DECAL would be equivalent) |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
180 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
181 |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
182 drawCacheImpl (cache, x, y, col); |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
183 } |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
184 /** A variation of drawCache, for transparent text. |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
185 * |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
186 * Instead of passing the alpha value(s) as arguments, set the openGL colour prior to calling: |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
187 * --- |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
188 * glColor3f (.5f, .5f, .5f); // set alpha to half |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
189 * drawCacheA (face, ...); |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
190 * |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
191 * glColor3ub (0, 255, 127); // alpha 0 for red, 1 for green, half for blue |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
192 * drawCacheA (face, ...); |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
193 * --- |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
194 * |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
195 * The overhead of the transparency is minimal. */ |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
196 void drawCacheA (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col/+ = Colour.WHITE+/) { |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
197 updateCache (face, str, cache); // update if necessary |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
198 debug scope (failure) |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
199 logger.error ("drawTextCache failed"); |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
200 |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
201 // transparency alpha |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
202 // alpha is current colour, per component |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
203 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
204 |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
205 drawCacheImpl (cache, x, y, col); |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
206 } |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
207 |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
208 private void drawCacheImpl (ref TextBlock cache, int x, int y, Colour col) { |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
209 if (DerelictGL.availableVersion() >= GLVersion.Version14) { |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
210 glBlendFunc (GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR); |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
211 glBlendColor(col.r, col.g, col.b, 1f); // text colour |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
212 } else |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
213 glBlendFunc (col.nearestGLConst, GL_ONE_MINUS_SRC_COLOR); |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
214 |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
215 glEnable (GL_TEXTURE_2D); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
216 glEnable(GL_BLEND); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
217 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
218 foreach (chr; cache.chars) { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
219 GlyphAttribs* ga = chr.ga; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
220 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
221 glBindTexture(GL_TEXTURE_2D, ga.texID); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
222 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
223 int x1 = x + chr.xPos; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
224 int y1 = y + chr.yPos; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
225 int x2 = x1 + ga.w; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
226 int y2 = y1 + ga.h; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
227 float tx1 = ga.x * wFactor; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
228 float ty1 = ga.y * hFactor; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
229 float tx2 = (ga.x + ga.w) * wFactor; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
230 float ty2 = (ga.y + ga.h) * hFactor; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
231 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
232 glBegin (GL_QUADS); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
233 glTexCoord2f (tx1, ty1); glVertex2i (x1, y1); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
234 glTexCoord2f (tx2, ty1); glVertex2i (x2, y1); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
235 glTexCoord2f (tx2, ty2); glVertex2i (x2, y2); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
236 glTexCoord2f (tx1, ty2); glVertex2i (x1, y2); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
237 glEnd (); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
238 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
239 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
240 glDisable(GL_BLEND); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
241 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
242 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
243 void addGlyph (FT_Face face, dchar chr) { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
244 debug scope (failure) |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
51
diff
changeset
|
245 logger.error ("FontTexture.addGlyph failed!"); |
49
bca7e2342d77
Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
48
diff
changeset
|
246 |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
247 auto gi = FT_Get_Char_Index (face, chr); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
248 auto g = face.glyph; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
249 |
50
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
250 // Use renderMode from options, masking bits which are allowable: |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
251 if (FT_Load_Glyph (face, gi, FT_LOAD_RENDER | (fontOpts.renderMode & 0xF0000))) |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
252 throw new fontGlyphException ("Unable to render glyph"); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
253 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
254 auto b = g.bitmap; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
255 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
51
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
256 //glPixelStorei (GL_UNPACK_ROW_LENGTH, b.pitch); |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
257 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
258 GlyphAttribs ga; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
259 ga.w = b.width; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
260 ga.h = b.rows; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
261 ga.left = g.bitmap_left; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
262 ga.top = g.bitmap_top; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
263 ga.advanceX = g.advance.x >> 6; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
264 ga.index = gi; |
51
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
265 if (b.pixel_mode == FT_Pixel_Mode.FT_PIXEL_MODE_LCD) |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
266 ga.w /= 3; |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
267 if (b.pixel_mode == FT_Pixel_Mode.FT_PIXEL_MODE_LCD_V) |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
268 ga.h /= 3; |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
269 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
270 foreach (ref t; tex) { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
271 if (t.addGlyph (ga)) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
272 goto gotTexSpace; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
273 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
274 // if here, no existing texture had the room for the glyph so create a new texture |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
275 // NOTE: check if using more than one texture impacts performance due to texture switching |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
51
diff
changeset
|
276 logger.info ("Creating a new font texture."); |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
277 tex ~= TexPacker.create(); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
278 assert (tex[$-1].addGlyph (ga), "Failed to fit glyph in a new texture but addGlyph didn't throw"); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
279 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
280 gotTexSpace: |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
281 glBindTexture(GL_TEXTURE_2D, ga.texID); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
282 GLenum format; |
51
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
283 ubyte[] buffer; // use our own pointer, since for LCD modes we need to perform a conversion |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
284 if (b.pixel_mode == FT_Pixel_Mode.FT_PIXEL_MODE_GRAY && b.num_grays == 256) { |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
51
diff
changeset
|
285 assert (b.pitch == b.width, "Have assumed b.pitch == b.width for gray glyphs."); |
51
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
286 buffer = b.buffer[0..b.pitch*b.rows]; |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
287 format = GL_LUMINANCE; |
51
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
288 } else if (b.pixel_mode == FT_Pixel_Mode.FT_PIXEL_MODE_LCD) { |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
289 // NOTE: Can't seem to get OpenGL to read freetype's RGB buffers properly, so convent. |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
290 /* NOTE: Sub-pixel rendering probably also needs filtering. I haven't tried, since it's |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
291 * disabled in my build of the library. For a tutorial on the filtering, see: |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
292 * http://dmedia.dprogramming.com/?n=Tutorials.TextRendering1 */ |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
293 buffer = new ubyte[b.width*b.rows]; |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
294 for (uint i = 0; i < b.rows; ++i) |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
295 for (uint j = 0; j < b.width; j+= 3) |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
296 { |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
297 buffer[i*b.width + j + 0] = b.buffer[i*b.pitch + j + 0]; |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
298 buffer[i*b.width + j + 1] = b.buffer[i*b.pitch + j + 1]; |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
299 buffer[i*b.width + j + 2] = b.buffer[i*b.pitch + j + 2]; |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
300 } |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
301 |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
302 format = (fontOpts.renderMode & RENDER_LCD_BGR) ? GL_BGR : GL_RGB; |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
303 } else if (b.pixel_mode == FT_Pixel_Mode.FT_PIXEL_MODE_LCD_V) { |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
304 // NOTE: Notes above apply. Only in this case converting the buffers seems essential. |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
305 buffer = new ubyte[b.width*b.rows]; |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
306 for (uint i = 0; i < b.rows; ++i) |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
307 for (uint j = 0; j < b.width; ++j) |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
308 { |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
309 // i/3 is the "real" row, b.width*3 is our width (with subpixels), j is column, |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
310 // i%3 is sub-pixel (R/G/B). i/3*3 necessary to round to multiple of 3 |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
311 buffer[i/3*b.width*3 + 3*j + i%3] = b.buffer[i*b.pitch + j]; |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
312 } |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
313 |
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
314 format = (fontOpts.renderMode & RENDER_LCD_BGR) ? GL_BGR : GL_RGB; |
50
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
315 } else |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
316 throw new fontGlyphException ("Unsupported freetype bitmap format"); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
317 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
318 glTexSubImage2D(GL_TEXTURE_2D, 0, |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
319 ga.x, ga.y, |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
320 ga.w, ga.h, |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
321 format, GL_UNSIGNED_BYTE, |
51
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
322 cast(void*) buffer.ptr); |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
323 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
324 cachedGlyphs[chr] = ga; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
325 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
326 |
61
7cab2af4ba21
A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
57
diff
changeset
|
327 // Draw the first glyph cache texture in the upper-left corner of the screen. |
7cab2af4ba21
A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
57
diff
changeset
|
328 debug (drawGlyphCache) void drawTexture () { |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
329 if (tex.length == 0) return; |
57
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
330 glEnable (GL_TEXTURE_2D); |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
331 glBindTexture(GL_TEXTURE_2D, tex[0].texID); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
332 glEnable(GL_BLEND); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
333 glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_COLOR); |
57
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
334 float[4] Cc = [ 1.0f, 1f, 1f, 1f ]; |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
335 glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Cc.ptr); |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
336 glColor3f (1f, 0f, 0f); |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
337 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
338 glBegin (GL_QUADS); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
339 glTexCoord2f (0f, 0f); glVertex2i (0, 0); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
340 glTexCoord2f (1f, 0f); glVertex2i (dimW, 0); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
341 glTexCoord2f (1f, 1f); glVertex2i (dimW, dimH); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
342 glTexCoord2f (0f, 1f); glVertex2i (0, dimH); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
343 glEnd (); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
344 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
345 glDisable(GL_BLEND); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
346 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
347 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
348 private: |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
349 TexPacker[] tex; // contains the gl texture id and packing data |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
350 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
351 GlyphAttribs[dchar] cachedGlyphs; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
352 int cacheVer = 0; // version of cache, used to make sure TextBlock caches are current. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
353 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
354 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
355 // Use LinePacker for our texture packer: |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
356 alias LinePacker TexPacker; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
357 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
358 /** Represents one gl texture; packs glyphs into lines. */ |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
359 struct LinePacker |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
360 { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
361 // create a new texture |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
362 static LinePacker create () { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
363 LinePacker p; |
57
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
364 //FIXME: why do I get a blank texture when binding to non-0? |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
365 //glGenTextures (1, &(p.texID)); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
366 p.texID = 0; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
367 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
368 // add a pretty background to the texture |
61
7cab2af4ba21
A little bit of progress on the content handling system (relevent code is likely to be revised).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
57
diff
changeset
|
369 debug (drawGlyphCache) { |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
370 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
371 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
372 ubyte[3][dimH][dimW] testTex; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
373 for (size_t i = 0; i < dimW; ++i) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
374 for (size_t j = 0; j < dimH; ++j) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
375 { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
376 testTex[i][j][0] = cast(ubyte) (i + j); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
377 testTex[i][j][1] = cast(ubyte) i; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
378 testTex[i][j][2] = cast(ubyte) j; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
379 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
380 void* ptr = testTex.ptr; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
381 } else |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
382 const void* ptr = null; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
383 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
384 // Create a texture without initialising values. |
57
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
385 glBindTexture(GL_TEXTURE_2D, p.texID); |
9e1f05fbbcef
Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
53
diff
changeset
|
386 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
387 dimW, dimH, 0, |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
388 GL_RGB, GL_UNSIGNED_BYTE, ptr); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
389 return p; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
390 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
391 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
392 /** Find space for a glyph of size attr.w, attr.h within the texture. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
393 * |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
394 * Throws: fontGlyphException if glyph dimensions are larger than the texture. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
395 * |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
396 * Returns false if unable to fit the glyph into the texture, true if successful. If |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
397 * successful, attr's x and y are set to suitible positions such that the rect given by attr's |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
398 * x, y, w & h is a valid subregion of the texture. */ |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
399 bool addGlyph (ref GlyphAttribs attr) { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
400 if (attr.w > dimW || attr.h > dimH) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
401 throw new fontGlyphException ("Glyph too large to fit texture!"); |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
402 |
53
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
51
diff
changeset
|
403 attr.texID = texID; // Set now. Possibly reset if new texture is needed. |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
51
diff
changeset
|
404 if (attr.w == 0) return true; // 0 sized glyph; x and y are unimportant. |
f000d6cd0f74
Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
51
diff
changeset
|
405 |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
406 bool cantFitExtraLine = nextYPos + attr.h >= dimH; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
407 foreach (ref line; lines) { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
408 if (line.length + attr.w <= dimW && // if sufficient length and |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
409 line.height >= attr.h && // sufficient height and |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
410 (cantFitExtraLine || // either there's not room for another line |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
411 line.height <= attr.h * WASTE_H)) // or we're not wasting much vertical space |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
412 { // then use this line |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
413 attr.x = line.length; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
414 attr.y = line.yPos; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
415 attr.texID = texID; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
416 line.length += attr.w; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
417 return true; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
418 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
419 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
420 // If we didn't return, we didn't use an existing line. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
421 if (cantFitExtraLine) // run out of room |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
422 return false; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
423 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
424 // Still room: add a new line. The new line has the largest yPos (furthest down texture), |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
425 // but the lines array must remain ordered by line height (lowest to heighest). |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
426 Line line; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
427 line.yPos = nextYPos; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
428 line.height = attr.h * EXTRA_H; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
429 line.length = attr.w; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
430 size_t i = 0; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
431 while (i < lines.length && lines[i].height < line.height) ++i; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
432 lines = lines[0..i] ~ line ~ lines[i..$]; // keep lines sorted by height |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
433 nextYPos += line.height; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
434 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
435 attr.x = 0; // first glyph in the line |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
436 attr.y = line.yPos; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
437 return true; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
438 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
439 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
440 // Publically accessible data: |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
441 uint texID; // OpenGL texture identifier (for BindTexture) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
442 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
443 private: |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
444 const WASTE_H = 1.3; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
445 const EXTRA_H = 1; // can be float/double, just experimenting with 1 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
446 struct Line { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
447 int yPos; // y position (xPos is always 0) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
448 int height; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
449 int length; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
450 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
451 Line[] lines; |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
452 int nextYPos = 0; // y position for next created line (0 for first line) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
453 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
454 |
50
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
455 // this bit of renderMode, if set, means read glyph as BGR not RGB when using LCD rendering |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
456 enum { RENDER_LCD_BGR = 1 << 30 } |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
457 OptionsFont fontOpts; |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
458 class OptionsFont : Options { |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
459 /* renderMode should be FT_LOAD_TARGET_NORMAL, FT_LOAD_TARGET_LIGHT, FT_LOAD_TARGET_LCD or |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
460 * FT_LOAD_TARGET_LCD_V, possibly with bit 31 set (see RENDER_LCD_BGR). |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
461 * FT_LOAD_TARGET_MONO is unsupported. |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
462 * |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
463 * lcdFilter should come from enum FT_LcdFilter: |
51
387a80724c35
Enabled LCD font rendering mode (only tested with freetype's sub-pixel rendering disabled).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
50
diff
changeset
|
464 * FT_LCD_FILTER_NONE = 0, FT_LCD_FILTER_DEFAULT = 1, FT_LCD_FILTER_LIGHT = 2 */ |
50
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
465 mixin (impl!("int renderMode, lcdFilter;")); |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
466 |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
467 static this() { |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
468 fontOpts = new OptionsFont; |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
469 Options.addOptionsClass (fontOpts, "font"); |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
470 } |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
471 } |
f68ae1d667f9
Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
49
diff
changeset
|
472 |
48
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
473 struct GlyphAttribs { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
474 int x, y; // position within texture |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
475 int w, h; // bitmap size |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
476 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
477 int left, top; // bitmap_left, bitmap_top fields |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
478 int advanceX; // horizontal advance distance |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
479 uint index; // glyph index (within face) |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
480 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
481 uint texID; // gl tex identifier |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
482 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
483 |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
484 /** Cached information for drawing a block of text. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
485 * |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
486 * Struct should be stored externally and updated via references. */ |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
487 struct TextBlock { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
488 CharCache[] chars; // All chars. They hold x & y pos. info, so don't need to know about lines. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
489 int cacheVer = -1; // this is checked on access, and must equal for cache to be valid. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
490 int w, h; /// Size of the block. Likely the only fields of use outside the library. |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
491 } |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
492 struct CharCache { |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
493 GlyphAttribs* ga; // character |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
494 int xPos, yPos; // x,y position |
a98ffb64f066
Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
495 } |