annotate mde/font/font.d @ 79:61ea26abe4dd

Moved mde/mergetag/parse/parse(To/From) to mde/mergetag/(de)serialize. Implemented (de)serialization of structs.
author Diggory Hardy <diggory.hardy@gmail.com>
date Tue, 05 Aug 2008 11:51:51 +0100
parents 65780e0e48e6
children d8fccaa45d5f 2813ac68576f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
1 /* LICENSE BLOCK
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
2 Part of mde: a Modular D game-oriented Engine
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
3 Copyright © 2007-2008 Diggory Hardy
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
4
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify it under the terms
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
6 of the GNU General Public License as published by the Free Software Foundation, either
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
7 version 2 of the License, or (at your option) any later version.
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
8
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
11 See the GNU General Public License for more details.
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
12
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
13 You should have received a copy of the GNU General Public License
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
15
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
16 /// Sets up freetype (in a basic way).
63
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
17 module mde.font.font;
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
18
63
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
19 public import mde.types.Colour;
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
20 import mde.lookup.Options;
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
21 import mde.font.FontTexture;
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
22 import mde.font.exception;
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
23
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
24 import mde.mergetag.Reader;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
25 import mde.mergetag.DataSet;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
26 import mde.mergetag.exception;
63
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
27 import mde.setup.paths;
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
28
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
29 import derelict.freetype.ft;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
30 import derelict.opengl.gl;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31
79
61ea26abe4dd Moved mde/mergetag/parse/parse(To/From) to mde/mergetag/(de)serialize. Implemented (de)serialization of structs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 76
diff changeset
32 import mde.mergetag.deserialize;
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
33 import tango.stdc.stringz;
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
34 import Util = tango.text.Util;
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35 import tango.util.log.Log : Log, Logger;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
36
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
37 // "Publically import" this symbol:
63
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
38 alias mde.font.FontTexture.TextBlock TextBlock;
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
39
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
40 private Logger logger;
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
41 static this () {
63
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 61
diff changeset
42 logger = Log.getLogger ("mde.font.font");
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
43 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
44
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
45 /** FontStyle class.
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
46 *
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
47 * Particular to a font and size, and any other effects like bold/italic if ever implemented.
45
0fd51d2c6c8a Several changes to resising windows and layout widgets. This commit still has some bugs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 44
diff changeset
48 *
0fd51d2c6c8a Several changes to resising windows and layout widgets. This commit still has some bugs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 44
diff changeset
49 * Note: it is not currently intended to be thread-safe. */
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
50 class FontStyle : IDataSection
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
51 {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
52 //BEGIN Static: manager
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
53 static {
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
54 debug (drawGlyphCache) void drawTexture() {
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
55 if (fontTex !is null)
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
56 fontTex.drawTexture;
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
57 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
58
74
cee261eba249 Minor tweaks.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 70
diff changeset
59 /** Load the freetype library from the file fileName. */
cee261eba249 Minor tweaks.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 70
diff changeset
60 private const fileName = "fonts";
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
61 void initialize () {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
62 if (!confDir.exists (fileName))
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
63 throw new fontException ("No font settings file (fonts.[mtt|mtb])");
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
64
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
65 if (FT_Init_FreeType (&library))
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
66 throw new fontException ("error initialising the FreeType library");
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
67
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
68 // Check version
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
69 FT_Int maj, min, patch;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
70 FT_Library_Version (library, &maj, &min, &patch);
67
108d123238c0 Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 64
diff changeset
71 if (maj != 2 || min != 3)
108d123238c0 Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 64
diff changeset
72 logger.warn ("Using an untested FreeType version: {}.{}.{}", maj, min, patch);
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
73
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
74 // Set LCD filtering method if LCD rendering is enabled.
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
75 const RMF = FT_LOAD_TARGET_LCD | FT_LOAD_TARGET_LCD_V;
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
76 if (fontOpts.renderMode & RMF &&
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
77 FT_Library_SetLcdFilter(library, cast(FT_LcdFilter)fontOpts.lcdFilter)) {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
78 /* An error occurred, presumably because LCD rendering support
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
79 * is not compiled into the library. */
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
80 logger.warn ("Bad/unsupported LCD filter option; disabling LCD font rendering.");
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
81 logger.warn ("Your FreeType 2 library may compiled without support for LCD/sub-pixel rendering.");
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
82
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
83 // Reset the default filter (in case an invalid value was set in config files).
64
cc3763817b8a Overhauled Options so that it now uses templates and mixins for type-specific internals, and supported types can be adjusted via just one list.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
84 fontOpts.set!(int) ("lcdFilter", FT_LcdFilter.FT_LCD_FILTER_DEFAULT);
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
85
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
86 /* If no support for LCD filtering, then LCD rendering only emulates NORMAL with 3
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 51
diff changeset
87 * times wider glyphs. So disable and save the extra work. */
64
cc3763817b8a Overhauled Options so that it now uses templates and mixins for type-specific internals, and supported types can be adjusted via just one list.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
88 fontOpts.set!(int) ("renderMode", FT_LOAD_TARGET_NORMAL);
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
89 }
50
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
90
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
91 /* Load font settings
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
92 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
93 * Each mergetag section corresponds to a font; each is loaded whether used or not
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
94 * (however the actual font files are only loaded on use). A fallback id must be
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
95 * provided in the header which must match a loaded font name; if a non-existant font
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
96 * is requested a warning will be logged and this font returned. */
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
97 char[] fallbackName;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
98 try {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
99 IReader reader;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
100 reader = confDir.makeMTReader (fileName, PRIORITY.LOW_HIGH, null, true);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
101 reader.dataSecCreator = delegate IDataSection(ID id) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
102 auto f = new FontStyle;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
103 fonts[id] = f;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
104 return f;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
105 };
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
106 reader.read;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
107
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
108 // get fallback name
76
65780e0e48e6 Re-enabled click event passing in the gui to make ButtonWidget work.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 74
diff changeset
109 char[]* p = "fallback" in reader.dataset.header._charA;
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
110 if (p is null)
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
111 throw new fontException ("No fallback font style specified");
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
112 fallbackName = *p;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
113 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
114 catch (MTException e) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
115 throw new fontException ("Mergetag exception: "~e.msg);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
116 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
117
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
118 // Find the fallback
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
119 FontStyle* p = fallbackName in fonts;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
120 if (p is null)
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
121 throw new fontException ("Fallback font style specified is not found");
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
122 fallback = *p;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
123 // Load the fallback now, to ensure it's available.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
124 // Also note that get() doesn't make sure the fallback is loaded before returning it.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
125 fallback.load;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
126 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
127
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
128 //FIXME: don't use GC for FontStyle resources
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
129 /** Cleanup: delete all fonts. */
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
130 void cleanup () {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
131 FT_Done_FreeType (library);
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
132 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
133
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
134 /** Get a FontStyle instance, for a section in the fonts.mtt file.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
135 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
136 * Also loads the font if it's not already loaded, so the first call may take some time.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
137 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
138 * Uses fallback font-style if the desired style isn't known about or fails to load, so
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
139 * this function should never fail or throw, in theory (unless out of memory). The
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
140 * fallback should already be loaded. */
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
141 FontStyle get(char[] name) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
142 FontStyle* p = name in fonts;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
143 if (p is null) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
144 logger.warn ("Font style "~name~" requested but not found; reverting to the fallback style.");
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
145 fonts[name] = fallback; // set to prevent another warning getting logged
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
146 return fallback;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
147 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
148 // Got it, but we need to make sure it's loaded:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
149 try {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
150 p.load;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
151 } catch (Exception e) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
152 logger.warn ("Font style "~name~" failed to load; reverting to the fallback style.");
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
153 return fallback;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
154 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
155 return *p;
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
156 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
157
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
158 private:
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
159 FT_Library library;
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
160 FontTexture fontTex;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
161 FontStyle[ID] fonts; // all font styles known about; not necessarily loaded
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
162 FontStyle fallback; // used when requested font isn't in fonts
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
163 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
164 //END Static
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
165
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
166 this() {}
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
167
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
168 //BEGIN Mergetag code
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
169 //NOTE: would it be better not to use a new mergetag file for this?
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
170 //FIXME: revise when gui can set options
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
171 void addTag (char[] tp, ID id, char[] dt) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
172 if (tp == "char[]") {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
173 if (id == "path")
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
174 path = parseTo!(char[]) (dt);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
175 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
176 else if (tp == "int") {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
177 if (id == "size")
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
178 size = parseTo!(int) (dt);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
179 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
180 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
181 void writeAll (ItemDelg) {} // no writing the config for now
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
182 //END Mergetag code
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
183
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
184 /** Load the font file.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
185 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
186 * Even if the same font is used at multiple sizes, multiple copies of FT_Face are used.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
187 * Sharing an FT_Face would require calling FT_Set_Pixel_Sizes each time a glyph is rendered or
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
188 * swapping the size information (face.size)? */
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
189 void load ()
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
190 in {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
191 assert (library !is null, "font: library is null");
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
192 } body {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
193 if (FT_New_Face (library, toStringz(path), 0, &face))
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
194 throw new fontLoadException ("Unable to read font: "~path);
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
195
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
196 if (!FT_IS_SCALABLE (face))
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
197 throw new fontLoadException ("Currently no support for non-scalable fonts (which " ~
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
198 path ~ " is). Please report if you want to see support.");
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
199 /* The following will need to be addressed when adding support for non-scalables:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
200 * Use of face.size.metrics.height property.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
201 */
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
202
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
203 if (FT_Set_Pixel_Sizes (face, 0,size))
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
204 throw new fontLoadException ("Unable to set pixel size");
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
205
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
206 // Create if necessary:
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
207 if (fontTex is null)
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
208 fontTex = new FontTexture;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
209 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
210
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
211 /** Update a TextBlock cache, as used by the textBlock function.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
212 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
213 * The only use of this is to get the text block's size ahead of rendering, via TextBlock's w
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
214 * and h properties.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
215 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
216 * This function will only actually update the cache if it is invalid, caused either by the
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
217 * font being changed or if cache.cacheVer < 0. */
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
218 void updateBlock (char[] str, ref TextBlock cache) {
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
219 try {
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 fontTex.updateCache (face, str, cache);
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 } catch (Exception e) {
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 logger.warn ("Exception while drawing text: "~e.msg);
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 }
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
224 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
225
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
226 /** Draw a block of text (may inlcude new-lines).
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
227 *
49
bca7e2342d77 Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 48
diff changeset
228 * The text block is drawn with top-left corner at x,y. To put the text's baseline at a given
bca7e2342d77 Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 48
diff changeset
229 * y coordinate would require some changes. Line height is currently variable, depending on the
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
230 * highest glyph in the line (should probably be fixed: FIXME).
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
231 *
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
232 * Specify the text's colour with col; currently this is only Colour.WHITE or Colour.BLACK
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
233 * (FIXME). FIXME: add alpha support.
49
bca7e2342d77 Enabled getting the size of a text block (before or after rendering).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 48
diff changeset
234 *
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
235 * As a CPU-side code optimisation, store a TextBlock (unique to str) and pass a reference as
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
236 * the cache argument. This is the recommended method, although for one-time calls when you
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
237 * don't need to know the size, the other version of textBlock may be used.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
238 * ---------------------------------
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
239 * char[] str;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
240 * TextBlock strCache;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
241 * textBlock (x, y, str, strCache);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
242 * ---------------------------------
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
243 * The TextBlock cache will be updated as necessary. Besides the initial update, this will only
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
244 * be if the font changes, or it is manually invalidated. This can be done by setting the
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
245 * TextBlock's cacheVer property to -1, which should be done if str is changed.
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
246 *
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
247 * The TextBlock's w and h properties are set to the size (in pixels) of the text block; other
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
248 * than this cache only serves as a small optimisation. However, the only way to get the size
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
249 * of a text block is to use a TextBlock cache and update it, either with this function or with
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
250 * the updateBlock function. */
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
251 void textBlock (int x, int y, char[] str, ref TextBlock cache, Colour col) {
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
252 try {
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
253 fontTex.drawCache (face, str, cache, x, y, col);
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
254 } catch (Exception e) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
255 logger.warn ("Exception while drawing text: "~e.msg);
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
256 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
257 }
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
258 /** ditto */
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
259 void textBlock (int x, int y, char[] str, Colour col) {
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
260 try {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
261 // Using the cache method for one-time use is slightly less than optimal, but doing so
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
262 // isn't really recommended anyway (and maintaining two versions of fontTex.drawText
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
263 // would be horrible).
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
264 TextBlock cache;
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
265 fontTex.drawCache (face, str, cache, x, y, col);
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
266 } catch (Exception e) {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
267 logger.warn ("Exception while drawing text: "~e.msg);
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
268 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
269 }
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
270
57
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
271 /** A variation of textBlock for transparency.
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
272 *
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
273 * Set the alpha by calling glColor*() first. See FontTexture.drawCacheA()'s documentation for
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
274 * details. */
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
275 void textBlockA (int x, int y, char[] str, ref TextBlock cache, Colour col) {
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
276 try {
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
277 fontTex.drawCacheA (face, str, cache, x, y, col);
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
278 } catch (Exception e) {
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
279 logger.warn ("Exception while drawing text: "~e.msg);
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
280 }
9e1f05fbbcef Coloured and alpha-blended text is now supported.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
281 }
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
282
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
283 /** The font-specified vertical distance between the baseline of consecutive lines. */
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
284 int getLineSeparation () {
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
285 return face.size.metrics.height >> 6;
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
286 }
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
287
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
288 ~this () {
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
289 FT_Done_Face (face);
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
290 }
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
291
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
292 private:
48
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
293 char[] path; // path to font file
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
294 int size; // font size
a98ffb64f066 Implemented font rendering (grayscale only; i.e. non-LCD).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
295
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
296 FT_Face face;
79
61ea26abe4dd Moved mde/mergetag/parse/parse(To/From) to mde/mergetag/(de)serialize. Implemented (de)serialization of structs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 76
diff changeset
297
61ea26abe4dd Moved mde/mergetag/parse/parse(To/From) to mde/mergetag/(de)serialize. Implemented (de)serialization of structs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 76
diff changeset
298 debug(mdeUnitTest) unittest {
61ea26abe4dd Moved mde/mergetag/parse/parse(To/From) to mde/mergetag/(de)serialize. Implemented (de)serialization of structs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 76
diff changeset
299 // Don't do a unittest since font relies on loading the freetype library dynamically,
61ea26abe4dd Moved mde/mergetag/parse/parse(To/From) to mde/mergetag/(de)serialize. Implemented (de)serialization of structs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 76
diff changeset
300 // normally done by Init. Also font is mostly visual and many problems will be obvious.
61ea26abe4dd Moved mde/mergetag/parse/parse(To/From) to mde/mergetag/(de)serialize. Implemented (de)serialization of structs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 76
diff changeset
301 }
44
07bd1a09e161 Started implementing text rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
302 }
50
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
303
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
304 /+class OptionsFont : Options {
f68ae1d667f9 Options: impl template & new OptionsFont class.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 49
diff changeset
305 alias store!(+/