1 /**
2  * Rendering for simple bitmap fonts.
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.ui.video.bmfont;
15 
16 import ae.ui.video.renderer;
17 import ae.utils.array;
18 import ae.utils.graphics.fonts.draw;
19 
20 final class FontTextureSource(Font) : ProceduralTextureSource
21 {
22 	Font font;
23 	Renderer.COLOR color;
24 
25 	this(Font font, Renderer.COLOR color)
26 	{
27 		this.font = font;
28 		this.color = color;
29 	}
30 
31 	override void getSize(out int width, out int height)
32 	{
33 		width = font.maxWidth;
34 		height = font.maxGlyph * font.height;
35 	}
36 
37 	override void drawTo(TextureCanvas dest)
38 	{
39 		foreach (g; 0..font.maxGlyph)
40 		{
41 			dchar c = g;
42 			dest.drawText(0, g * font.height, c.toArray, font, color);
43 		}
44 	}
45 
46 	void drawText(S)(Renderer r, int x, int y, S s)
47 	{
48 		foreach (c; s)
49 		{
50 			if (font.hasGlyph(c))
51 			{
52 				auto g = font.getGlyph(c);
53 				auto v = c * font.height;
54 				r.draw(x, y, this, 0, v, g.width, v + font.height);
55 				x += g.width;
56 			}
57 		}
58 	}
59 }
60 
61 unittest
62 {
63 	// Test instantiation
64 	if (false)
65 	{
66 		Renderer r;
67 		import ae.utils.graphics.fonts.font8x8;
68 		FontTextureSource!Font8x8 f;
69 		f.drawText(r, 0, 0, "foo");
70 	}
71 }