1 /**
2  * Draw a bitmap font.
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.utils.graphics.fonts.draw;
15 
16 import ae.utils.graphics.draw;
17 import ae.utils.graphics.view;
18 
19 /// Draw text using a bitmap font.
20 void drawText(V, FONT, S, COLOR)(auto ref V v, xy_t x, xy_t y, S s, ref FONT font, COLOR color)
21 	if (isWritableView!V && is(COLOR : ViewColor!V))
22 {
23 	auto x0 = x;
24 	foreach (c; s)
25 	{
26 		if (c == '\r')
27 			x = x0;
28 		else
29 		if (c == '\n')
30 		{
31 			x = x0;
32 			y += font.height;
33 		}
34 		else
35 		{
36 			auto glyph = font.getGlyph(font.hasGlyph(c) ? c : ' ');
37 			foreach (cy; 0..font.height)
38 				foreach (cx; 0..glyph.width)
39 					if (glyph.rows[cy] & (1 << cx))
40 						v.safePut(x+cx, y+cy, color);
41 			x += glyph.width;
42 		}
43 	}
44 }
45 
46 version(unittest)
47 {
48 	import ae.utils.graphics.image;
49 	import ae.utils.graphics.fonts.font8x8;
50 }
51 
52 unittest
53 {
54 	auto v = Image!ubyte(100, 8);
55 	v.drawText(0, 0, "Hello World!", font8x8, ubyte(255));
56 	//v.toPNG.toFile("test.png");
57 }