1 /*
2 Copyright (c) 2013 Andrey Penechko
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license the "Software" to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module anchovy.graphics.font.font;
30 
31 import std.stdio;
32 import std..string: toStringz;
33 
34 import derelict.freetype.ft;
35 
36 import anchovy.core.types;
37 import anchovy.graphics.texture;
38 import anchovy.graphics.bitmap;
39 
40 import anchovy.graphics.font.fterrors;
41 import anchovy.graphics.font.textureatlas;
42 
43 struct Glyph
44 {
45 	uvec2 atlasPosition;
46 	GlyphMetrics metrics;
47 }
48 
49 struct GlyphMetrics
50 {
51 	uint width;
52 	uint height;
53 	int offsetX;
54 	int offsetY;
55 	uint advanceX;
56 	uint advanceY;
57 }
58 
59 class Font
60 {
61 public:
62 
63 	this(in string filename, TextureAtlas texAtlas, in dstring chars, in uint size)
64 	{
65 		FT_Library	library;
66 		FT_Face		face;
67 
68 		try
69 		{
70 			checkFtError(
71 				FT_Init_FreeType(&library));
72 			checkFtError(
73 				FT_New_Face(library, toStringz(filename), 0, &face));
74 			checkFtError(
75 				FT_Select_Charmap(face, FT_ENCODING_UNICODE));
76 			checkFtError(
77 				//FT_Set_Char_Size(face, cast(int)(size*64), 0, 72, 0));
78 				FT_Set_Pixel_Sizes(face, 0, size));
79 		}
80 		catch(FreeTypeException e)
81 		{
82 			writeln("Can not load font '"~filename~"'");
83 			writeln(e.msg);
84 		}
85 
86 		loadGlyphs(chars, library, face, texAtlas, size);
87 		_ascender = face.ascender/64;
88 		_descender = face.descender/64;
89 		_height = face.height/64;
90 		_size = size;
91 		FT_Done_Face(face);
92 		FT_Done_FreeType(library);
93 	}
94 
95 	uint height() @property const
96 	{
97 		return _height;
98 	}
99 
100 	uint size() @property const
101 	{
102 		return _size;
103 	}
104 
105 	uint lineGap() @property const
106 	{
107 		return _lineGap;
108 	}
109 
110 	uint ascender() @property const
111 	{
112 		return _ascender;
113 	}
114 
115 	uint descender() @property const
116 	{
117 		return _descender;
118 	}
119 
120 	int verticalOffset() @property const
121 	{
122 		return _verticalOffset;
123 	}
124 
125 	void verticalOffset(int value) @property
126 	{
127 		_verticalOffset = value;
128 	}
129 
130 	uint getKerning(in dchar leftGlyph, in dchar rightGlyph)
131 	{
132 		uint[dchar] rightGlyps = *(leftGlyph in kerningTable);
133 		uint kerning = *(rightGlyph in rightGlyps);
134 		return kerning;
135 	}
136 
137 	Glyph* getGlyph(in dchar chr)
138 	{
139 		if (auto glyph = chr in glyphs)
140 		{
141 			return glyph;
142 		}
143 		else
144 			return '?' in glyphs;//TODO: Add loading for nonexisting glyphs
145 	}
146 
147 private:
148 
149 	private void loadGlyphs(in dstring chars, in FT_Library library, ref FT_Face face, TextureAtlas atlas, in uint size)
150 	{
151 		if (chars.length == 0) return;
152 
153 		FT_Bitmap		ftBitmap;
154 		FT_Error		ftError;
155 		FT_Glyph		ftGlyph;
156 		FT_UInt			ftGlyphIndex;
157 		FT_GlyphSlot	ftGlyphSlot;
158 
159 		uint missed = 0;
160 		Glyph currentGlyph;
161 
162 		foreach(dchar chr; chars)
163 		{
164 			if (chr in glyphs) continue;
165 			//checkFtError(
166 				//FT_Set_Char_Size(face, cast(int)(size*64), 0, 96, 0));
167 				//FT_Set_Pixel_Sizes(face, 0, size));
168 			checkFtError(
169 				FT_Load_Char(face, chr, FT_LOAD_RENDER));
170 			currentGlyph.metrics = loadGlyphMetrics(face);
171 			currentGlyph.atlasPosition = putBitmapToAtlas(atlas,
172 			                                              face.glyph.bitmap,
173 			                                              currentGlyph.metrics.width,
174 			                                              currentGlyph.metrics.height);
175 			glyphs[chr] = currentGlyph;
176 		}
177 		glyphs.rehash;
178 	}
179 
180 	private uvec2 putBitmapToAtlas(ref TextureAtlas atlas, in FT_Bitmap ftBitmap, in uint width, in uint height)
181 	{
182 		uvec2 pos = atlas.insert(width, height);
183 		Bitmap atlasBitmap = atlas.getBitmap();
184 		atlasBitmap.putCustomRect(pos, ftBitmap.buffer, 1, width, height);
185 
186 		return pos;
187 	}
188 
189 	private GlyphMetrics loadGlyphMetrics(in FT_Face face)
190 	{
191 		GlyphMetrics gm;
192 		FT_Glyph_Metrics ftgm = face.glyph.metrics;
193 		gm.width	= cast(uint)ftgm.width / 64;
194 		gm.height	= cast(uint)ftgm.height / 64;
195 		gm.offsetX	= cast(uint)ftgm.horiBearingX / 64;
196 		gm.offsetY	= cast(uint)ftgm.horiBearingY / 64;
197 		gm.advanceX	= cast(uint)ftgm.horiAdvance / 64;
198 		gm.advanceY	= cast(uint)ftgm.vertAdvance / 64;
199 		return gm;
200 	}
201 
202 private:
203 
204 	uint _height;
205 	uint _size;
206 	uint _lineGap;
207 	uint _ascender;
208 	uint _descender;
209 	int  _verticalOffset; // Can be used to manually adjust vertical position of text.
210 
211 	Glyph[dchar] glyphs;
212 	//Glyph[dchar] boldGlyphs;
213 	//Glyph[dchar] italicGlyphs;
214 
215 	/**
216 	 * Usage:
217 	 * -----
218 	 * uint kern = kerningTable[left][right];
219 	 * kerningTable[left][right] = kern;
220 	 * -----
221 	 */
222 	uint[dchar][dchar] kerningTable;  //Not yet implemented
223 	bool kerningEnabled = true;
224 }
225