annotate mde/resource/FontTexture.d @ 53:f000d6cd0f74

Changes to paths, command line arguments and font LCD rendering. Use "./" instead of "" as default install dir on windows. Implemented a command-line argument parser. Changes to LCD filter/render-mode option handling after testing what actually happens. Changed some FontTexture messages and internals.
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 05 Jun 2008 17:16:52 +0100
parents 387a80724c35
children 9e1f05fbbcef
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. */
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
25 module mde.resource.FontTexture;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
26
50
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
27 import mde.Options;
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
28 import mde.resource.exception;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
29
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
30 import derelict.freetype.ft;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31 import derelict.opengl.gl;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
32
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
33 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
34 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
35
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
36 private Logger logger;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
37 static this () {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
38 logger = Log.getLogger ("mde.resource.FontTexture");
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
39 }
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 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
42 const wFactor = 1f / dimW;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
43 const hFactor = 1f / dimH;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
44
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
45 /** 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
46 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
47 * 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
48 * rendering fonts to the screen.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
49 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
50 * 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
51 class FontTexture
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
52 {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
53 this () {}
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 foreach (t; tex) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
56 glDeleteTextures (1, &(t.texID));
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
57 }
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 // 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
61 void clear () {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
62 foreach (t; tex) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
63 glDeleteTextures (1, &(t.texID));
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 cachedGlyphs = null;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
66 ++cacheVer;
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
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 /** 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
71 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
72 * 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
73 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
74 {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
75 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
76 logger.error ("updateCache failed");
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
77
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
78 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
79 return;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
80
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
81 cache.cacheVer = cacheVer;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
82
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
83 /* 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
84 * 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
85 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
86 chrs = Utf.toString32 (str, chrs);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
87
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
88 // Allocate space.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
89 // 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
90 cache.chars.length = chrs.length;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
91 cache.chars.length = 0;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
92
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
93 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
94 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
95 int y = 0;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
96 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
97
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
98 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
99 {
49
bca7e2342d77 Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 48
diff changeset
100 // 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
101 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
102 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
103 {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
104 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
105 break;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
106
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
107 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
108 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
109 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
110 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
111 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
112 }
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 if (ga.top > yMax)
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
115 yMax = ga.top;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
116 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
117 y += yMax;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
118
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
119 // Now for the current line:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
120 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
121 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
122 for (; i < chrs.length; ++i)
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
123 {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
124 // 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
125 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
126 ++i;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
127 break;
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 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
130 break;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
131 }
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 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
134 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
135
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
136 // Kerning
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
137 if (hasKerning && (gi_prev != 0)) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
138 FT_Vector delta;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
139 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
140 x += delta.x >> 6;
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 // 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
144 // 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
145 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
146 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
147 x += cc.ga.advanceX;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
148
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
149 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
150
bca7e2342d77 Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 48
diff changeset
151 // 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
152 // 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
153 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
154 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
155 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
156 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
157 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
158 // 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
159 y += lineSep - yMax;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
160 }
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 /** Render a block of text using a cache. Updates the cache if necessary. */
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
164 void drawTextCache (FT_Face face, char[] str, ref TextBlock cache, int x, int y) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
165 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
166 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
167 logger.error ("drawTextCache failed");
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
168
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
169 glEnable (GL_TEXTURE_2D);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
170 glEnable(GL_BLEND);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
171 glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_COLOR);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
172
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
173 foreach (chr; cache.chars) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
174 GlyphAttribs* ga = chr.ga;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
175
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
176 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
177
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
178 int x1 = x + chr.xPos;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
179 int y1 = y + chr.yPos;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
180 int x2 = x1 + ga.w;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
181 int y2 = y1 + ga.h;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
182 float tx1 = ga.x * wFactor;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
183 float ty1 = ga.y * hFactor;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
184 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
185 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
186
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
187 glBegin (GL_QUADS);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
188 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
189 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
190 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
191 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
192 glEnd ();
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
193 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
194
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
195 glDisable(GL_BLEND);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
196 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
197
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
198 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
199 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
200 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
201
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
202 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
203 auto g = face.glyph;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
204
50
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
205 // 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
206 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
207 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
208
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
209 auto b = g.bitmap;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
210 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
211 //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
212
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
213 GlyphAttribs ga;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
214 ga.w = b.width;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
215 ga.h = b.rows;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
216 ga.left = g.bitmap_left;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
217 ga.top = g.bitmap_top;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
218 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
219 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
220 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
221 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
222 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
223 ga.h /= 3;
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
224
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
225 foreach (ref t; tex) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
226 if (t.addGlyph (ga))
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
227 goto gotTexSpace;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
228 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
229 // 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
230 // 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
231 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
232 tex ~= TexPacker.create();
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
233 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
234
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
235 gotTexSpace:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
236 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
237 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
238 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
239 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
240 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
241 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
242 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
243 } 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
244 // 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
245 /* 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
246 * 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
247 * 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
248 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
249 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
250 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
251 {
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
252 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
253 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
254 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
255 }
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
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 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
258 } 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
259 // 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
260 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
261 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
262 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
263 {
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
264 // 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
265 // 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
266 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
267 }
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
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
269 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
270 } else
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
271 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
272
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
273 glTexSubImage2D(GL_TEXTURE_2D, 0,
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
274 ga.x, ga.y,
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
275 ga.w, ga.h,
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
276 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
277 cast(void*) buffer.ptr);
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
278
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
279 cachedGlyphs[chr] = ga;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
280 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
281
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
282 void drawTexture () { // temp func
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
283 if (tex.length == 0) return;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
284 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
285 glEnable (GL_TEXTURE_2D);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
286 glEnable(GL_BLEND);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
287 glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_COLOR);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
288
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
289 glBegin (GL_QUADS);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
290 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
291 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
292 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
293 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
294 glEnd ();
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
295
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
296 glDisable(GL_BLEND);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
297 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
298
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
299 private:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
300 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
301
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
302 GlyphAttribs[dchar] cachedGlyphs;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
303 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
304 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
305
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
306 // 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
307 alias LinePacker TexPacker;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
308
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
309 /** 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
310 struct LinePacker
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
311 {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
312 // create a new texture
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
313 static LinePacker create () {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
314 LinePacker p;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
315 //FIXME: check for error?
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
316 //glGenTextures (1, &(p.texID));
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
317 p.texID = 0;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
318 //FIXME: why do I get a blank texture when using bind?
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
319 glBindTexture(GL_TEXTURE_2D, p.texID);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
320
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
321 // add a pretty background to the texture
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
322 static if (false) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
323 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
324 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
325 ubyte[3][dimH][dimW] testTex;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
326 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
327 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
328 {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
329 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
330 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
331 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
332 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
333 void* ptr = testTex.ptr;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
334 } else
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
335 const void* ptr = null;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
336
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
337 // Create a texture without initialising values.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
338 glTexImage2D(GL_TEXTURE_2D, 0, 3,
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
339 dimW, dimH, 0,
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
340 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
341 return p;
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
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
344 /** 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
345 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
346 * 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
347 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
348 * 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
349 * 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
350 * 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
351 bool addGlyph (ref GlyphAttribs attr) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
352 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
353 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
354
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
355 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
356 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
357
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
358 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
359 foreach (ref line; lines) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
360 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
361 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
362 (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
363 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
364 { // then use this line
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
365 attr.x = line.length;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
366 attr.y = line.yPos;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
367 attr.texID = texID;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
368 line.length += attr.w;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
369 return true;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
370 }
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 // 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
373 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
374 return false;
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 // 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
377 // 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
378 Line line;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
379 line.yPos = nextYPos;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
380 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
381 line.length = attr.w;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
382 size_t i = 0;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
383 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
384 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
385 nextYPos += line.height;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
386
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
387 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
388 attr.y = line.yPos;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
389 return true;
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 // Publically accessible data:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
393 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
394
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
395 private:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
396 const WASTE_H = 1.3;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
397 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
398 struct Line {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
399 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
400 int height;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
401 int length;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
402 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
403 Line[] lines;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
404 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
405 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
406
50
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
407 // 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
408 enum { RENDER_LCD_BGR = 1 << 30 }
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
409 OptionsFont fontOpts;
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
410 class OptionsFont : Options {
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
411 /* 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
412 * 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
413 * FT_LOAD_TARGET_MONO is unsupported.
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
414 *
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
415 * 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
416 * 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
417 mixin (impl!("int renderMode, lcdFilter;"));
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
418
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
419 static this() {
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
420 fontOpts = new OptionsFont;
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
421 Options.addOptionsClass (fontOpts, "font");
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
422 }
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
423 }
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
424
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
425 struct GlyphAttribs {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
426 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
427 int w, h; // bitmap size
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
428
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
429 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
430 int advanceX; // horizontal advance distance
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
431 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
432
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
433 uint texID; // gl tex identifier
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
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
436 /** 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
437 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
438 * 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
439 struct TextBlock {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
440 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
441 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
442 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
443 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
444 struct CharCache {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
445 GlyphAttribs* ga; // character
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
446 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
447 }