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 <ae@cy.md>
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 /// Adapter from a font (as in `ae.utils.graphics.fonts`)
21 /// and `ProceduralTextureSource` .
22 final class FontTextureSource(Font) : ProceduralTextureSource
23 {
24 	this(Font font, Renderer.COLOR color)
25 	{
26 		this.font = font;
27 		this.color = color;
28 	} ///
29 
30 	/// Draw a string.
31 	void drawText(S)(Renderer r, int x, int y, S s)
32 	{
33 		foreach (c; s)
34 		{
35 			if (font.hasGlyph(c))
36 			{
37 				auto g = font.getGlyph(c);
38 				auto v = c * font.height;
39 				r.draw(x, y, this, 0, v, g.width, v + font.height);
40 				x += g.width;
41 			}
42 		}
43 	}
44 
45 protected:
46 	Font font;
47 	Renderer.COLOR color;
48 
49 	override void getSize(out int width, out int height)
50 	{
51 		width = font.maxWidth;
52 		height = font.maxGlyph * font.height;
53 	}
54 
55 	override void drawTo(TextureCanvas dest)
56 	{
57 		foreach (g; 0..font.maxGlyph)
58 		{
59 			dchar c = g;
60 			dest.drawText(0, g * font.height, c.asSlice, font, color);
61 		}
62 	}
63 }
64 
65 unittest
66 {
67 	// Test instantiation
68 	if (false)
69 	{
70 		Renderer r;
71 		import ae.utils.graphics.fonts.font8x8;
72 		FontTextureSource!Font8x8 f;
73 		f.drawText(r, 0, 0, "foo");
74 	}
75 }