annotate mde/font/FontTexture.d @ 179:1f9d00f392bd default tip

Fixed a bug where (non-resizible) widgets wouldn't get shrunk when minimal size decreases, meaning optional context menus are hiden properly now. Optimised when ServiceContentList.opCall is called, I think without breaking anything.
author Diggory Hardy <diggory.hardy@gmail.com>
date Tue, 15 Sep 2009 20:09:59 +0200
parents e45226d3deae
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
137
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
28 import mde.content.AStringContent;
63
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 // 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
55 void clear () {
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 cachedGlyphs = null;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
60 ++cacheVer;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
61 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
62
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
63 /** 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
64 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
65 * 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
66 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
67 {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
68 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
69 logger.error ("updateCache failed");
48
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 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
72 return;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
73
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
74 cache.cacheVer = cacheVer;
100
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
75 cache.w = cache.h = 0; // reset
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
76
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
77 /* 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
78 * 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
79 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
80 chrs = Utf.toString32 (str, chrs);
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 // Allocate space.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
83 // 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
84 cache.chars.length = chrs.length;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
85 cache.chars.length = 0;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
86
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
87 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
88 bool hasKerning = (FT_HAS_KERNING (face) != 0);
89
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
89 int y = face.size.metrics.ascender >> 6;
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
90 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
91
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
92 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
93 {
89
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
94 // For each line:
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
95 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
96 uint gi_prev = 0; // previous glyph index (needed for kerning)
89
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
97
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
98 for (; i < chrs.length; ++i)
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
99 {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
100 // 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
101 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
102 ++i;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
103 break;
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[i] == '\n' || chrs[i] == '\r') {
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
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
109 cc.ga = chrs[i] in cachedGlyphs;
89
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
110 if (cc.ga is null) { // Not cached
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
111 addGlyph (face, chrs[i]); // so render it
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
112 cc.ga = chrs[i] in cachedGlyphs; // get the ref of the copy we've stored
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
113 assert (cc.ga !is null, "Unable to cache glyph!");
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
114 }
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
115
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
116 // Kerning
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
117 if (hasKerning && (gi_prev != 0)) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
118 FT_Vector delta;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
119 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
120 x += delta.x >> 6;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
121 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
122
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
123 // 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
124 // 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
125 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
126 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
127 x += cc.ga.advanceX;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
128
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
129 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
130
bca7e2342d77 Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 48
diff changeset
131 // 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
132 // 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
133 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
134 cache.w = cc.xPos + cc.ga.w;
89
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
135 /+if (cc.yPos + cc.ga.h > cache.h)
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
136 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
137 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
138 // Now increment i and continue with the next line if there is one.
89
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
139 y += lineSep;
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
140 cache.h += lineSep;
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
141 }
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
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
144 /** 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
145 *
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
146 * Params:
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
147 * 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
148 * invalid)
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
149 * 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
150 * 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
151 * 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
152 * y = Smaller y-coordinate of position
100
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
153 * col = Text colour (note: currently limited to black or white)
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
154 * index = Index of edit position, or size_t.max if no idet position. */
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
155 void drawCache (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col, size_t index) {
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
156 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
157 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
158 logger.error ("drawTextCache failed");
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
159
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
160 // 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
161 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
162
100
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
163 drawCacheImpl (face, cache, x, y, col, index);
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
164 }
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
165 /** 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
166 *
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
167 * 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
168 * ---
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
169 * 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
170 * drawCacheA (face, ...);
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
171 *
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
172 * 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
173 * drawCacheA (face, ...);
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
174 * ---
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
175 *
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
176 * The overhead of the transparency is minimal. */
100
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
177 void drawCacheA (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col, size_t index) {
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
178 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
179 debug scope (failure)
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
180 logger.error ("drawTextCache failed");
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 // transparency alpha
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
183 // 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
184 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
185
100
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
186 drawCacheImpl (face, cache, x, y, col, index);
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
187 }
100
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
188
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
189 private void drawCacheImpl (FT_Face face, ref TextBlock cache, int x, int y, Colour col, size_t index) {
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
190 if (DerelictGL.availableVersion() >= GLVersion.Version14) {
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
191 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
192 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
193 } else
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
194 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
195
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
196 glEnable (GL_TEXTURE_2D);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
197 glEnable(GL_BLEND);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
198
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
199 foreach (chr; cache.chars) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
200 GlyphAttribs* ga = chr.ga;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
201
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
202 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
203
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
204 int x1 = x + chr.xPos;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
205 int y1 = y + chr.yPos;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
206 int x2 = x1 + ga.w;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
207 int y2 = y1 + ga.h;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
208 float tx1 = ga.x * wFactor;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
209 float ty1 = ga.y * hFactor;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
210 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
211 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
212
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
213 glBegin (GL_QUADS);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
214 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
215 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
216 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
217 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
218 glEnd ();
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
219 }
100
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
220 glDisable(GL_TEXTURE_2D);
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
221 glDisable(GL_BLEND);
100
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
222
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
223 if (index <= cache.chars.length) { // draw edit position
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
224 int x1;
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
225 if (index == 0)
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
226 x1 = x + 1;
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
227 else {
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
228 auto c = cache.chars[index-1];
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
229 x1 = x + c.xPos + c.ga.advanceX;
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
230 }
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
231 glColor3f(col.r, col.g, col.b);
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
232 glRecti(x1 - 1, y, x1, y + (face.size.metrics.height >> 6));
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
233 }
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
234 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
235
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
236 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
237 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
238 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
239
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
240 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
241 auto g = face.glyph;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
242
137
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
243 if (FT_Load_Glyph (face, gi, FT_LOAD_RENDER | fontOpts.modeFlag))
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
244 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
245
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
246 auto b = g.bitmap;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
247 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
248 //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
249
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
250 GlyphAttribs ga;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
251 ga.w = b.width;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
252 ga.h = b.rows;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
253 ga.left = g.bitmap_left;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
254 ga.top = g.bitmap_top;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
255 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
256 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
257 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
258 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
259 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
260 ga.h /= 3;
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
261
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
262 foreach (ref t; tex) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
263 if (t.addGlyph (ga))
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
264 goto gotTexSpace;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
265 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
266 // 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
267 // NOTE: check if using more than one texture impacts performance due to texture switching
96
dbf332403c6e Improvements to FloatingAreaWidget: positioning, passing click events and draw order.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 94
diff changeset
268 debug logger.trace ("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
269 tex ~= TexPacker.create();
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
270 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
271
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
272 gotTexSpace:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
273 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
274 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
275 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
276 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
277 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
278 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
279 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
280 } 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
281 // 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
282 /* 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
283 * 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
284 * 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
285 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
286 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
287 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
288 {
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 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
290 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
291 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
292 }
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
137
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
294 format = (fontOpts.mode() >= 4) ? GL_BGR : GL_RGB;
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
295 } else if (b.pixel_mode == FT_Pixel_Mode.FT_PIXEL_MODE_LCD_V) {
83
2813ac68576f Start of creating a separate gui demo module and leaving mde.d for testing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
296 // NOTE: Notes above apply. But in this case converting the buffers seems essential.
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
297 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
298 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
299 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
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 // 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
302 // 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
303 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
304 }
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
137
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
306 format = (fontOpts.mode() >= 4) ? GL_BGR : GL_RGB;
50
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
307 } else
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
308 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
309
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
310 glTexSubImage2D(GL_TEXTURE_2D, 0,
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
311 ga.x, ga.y,
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
312 ga.w, ga.h,
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
313 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
314 cast(void*) buffer.ptr);
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
315
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
316 cachedGlyphs[chr] = ga;
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
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
319 // 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
320 debug (drawGlyphCache) void drawTexture () {
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
321 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
322 glEnable (GL_TEXTURE_2D);
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
323 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
324 glEnable(GL_BLEND);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
325 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
326 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
327 glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Cc.ptr);
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
328
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
329 glBegin (GL_QUADS);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
330 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
331 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
332 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
333 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
334 glEnd ();
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
335
97
30470bc19ca4 Floating widgets now work nicely: customizable borders added, resizing, moving.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 96
diff changeset
336 glDisable(GL_TEXTURE_2D);
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
337 glDisable(GL_BLEND);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
338 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
339
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
340 private:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
341 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
342
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
343 GlyphAttribs[dchar] cachedGlyphs;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
344 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
345 }
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 // 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
348 alias LinePacker TexPacker;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
349
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
350 /** 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
351 struct LinePacker
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
352 {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
353 // create a new texture
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
354 static LinePacker create () {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
355 LinePacker p;
89
97e6dce08037 Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 86
diff changeset
356 //FIXME (gl): 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
357 //glGenTextures (1, &(p.texID));
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
358 p.texID = 0;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
359
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
360 // 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
361 debug (drawGlyphCache) {
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
362 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
363 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
364 ubyte[3][dimH][dimW] testTex;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
365 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
366 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
367 {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
368 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
369 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
370 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
371 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
372 void* ptr = testTex.ptr;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
373 } else
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
374 const void* ptr = null;
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 // 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
377 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
378 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
379 dimW, dimH, 0,
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
380 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
381 return p;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
382 }
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 /** 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
385 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
386 * 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
387 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
388 * 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
389 * 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
390 * 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
391 bool addGlyph (ref GlyphAttribs attr) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
392 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
393 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
394
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
395 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
396 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
397
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
398 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
399 foreach (ref line; lines) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
400 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
401 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
402 (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
403 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
404 { // then use this line
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
405 attr.x = line.length;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
406 attr.y = line.yPos;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
407 attr.texID = texID;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
408 line.length += attr.w;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
409 return true;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
410 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
411 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
412 // 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
413 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
414 return false;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
415
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
416 // 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
417 // 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
418 Line line;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
419 line.yPos = nextYPos;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
420 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
421 line.length = attr.w;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
422 size_t i = 0;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
423 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
424 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
425 nextYPos += line.height;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
426
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
427 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
428 attr.y = line.yPos;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
429 return true;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
430 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
431
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
432 // Publically accessible data:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
433 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
434
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
435 private:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
436 const WASTE_H = 1.3;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
437 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
438 struct Line {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
439 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
440 int height;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
441 int length;
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 Line[] lines;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
444 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
445 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
446
137
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
447 struct fontOpts {
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
448 static:
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
449 EnumContent mode;
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
450 int modeFlag;
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
451 /* lcdFilter should come from enum FT_LcdFilter:
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
452 * FT_LCD_FILTER_NONE (0), FT_LCD_FILTER_DEFAULT (1), FT_LCD_FILTER_LIGHT (2) */
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
453 EnumContent lcdFilter;
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
454 IntContent defaultSize;
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
455 StringContent defaultFont;
50
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
456
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
457 static this() {
137
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
458 mode = new EnumContent ("Font.mode", ["normal"[],"light","lcd","lcd_v","lcd_bgr","lcd_bgr_v"]);
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
459 lcdFilter = new EnumContent ("Font.lcdFilter", ["none"[],"default","light"]);
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
460 defaultSize = new IntContent ("Font.defaultSize");
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
461 defaultFont = new StringContent ("Font.defaultFont");
170
e45226d3deae Context menu services not applicable to the current type can now be hidden.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 137
diff changeset
462 mode.addCallback (delegate void(IContent) {
137
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
463 /* modeFlag should have one of the following values:
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
464 * FT_LOAD_TARGET_NORMAL (0x00000)
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
465 * FT_LOAD_TARGET_LIGHT (0x10000)
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
466 * FT_LOAD_TARGET_LCD (0x30000)
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
467 * FT_LOAD_TARGET_LCD_V (0x40000)
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
468 * The mode FT_LOAD_TARGET_MONO (0x20000) is unsupported. */
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
469 modeFlag = fontOpts.mode();
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
470 if (modeFlag == 2 || modeFlag == 4) modeFlag = 0x30000;
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
471 else if (modeFlag == 3 || modeFlag == 5) modeFlag = 0x40000;
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
472 else modeFlag <<= 16;
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 104
diff changeset
473 });
50
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
474 }
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
475 }
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
476
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
477 struct GlyphAttribs {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
478 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
479 int w, h; // bitmap size
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 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
482 int advanceX; // horizontal advance distance
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
483 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
484
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
485 uint texID; // gl tex identifier
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
486 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
487
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
488 /** 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
489 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
490 * 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
491 struct TextBlock {
100
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
492 CharCache[] chars; /// All chars. They hold x & y pos. info, so don't need to know about lines.
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
493 int cacheVer = -1; /// Checked on access; must equalFontTexture.cacheVer or the cache is rebuilt.
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
494 int w, h; /// Size of the block.
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
495 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
496 struct CharCache {
100
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
497 GlyphAttribs* ga; /// The character
0ea4a3e651ae There is now a position marker for text editing.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 98
diff changeset
498 int xPos, yPos; /// Character's x,y position
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
499 }