1 /*
2 Copyright (c) 2021 Oleg Baharev, Timur Gafarov
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 /**
30  * Draw ASCII text
31  *
32  * Copyright: Oleg Baharev, Timur Gafarov 2021.
33  * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
34  * Authors: Oleg Baharev, Timur Gafarov
35  */
36 module dlib.image.render.text;
37 
38 import dlib.image.color;
39 import dlib.image.image;
40 
41 /// Draw text on an image
42 void drawText(SuperImage img, string s, int x, int y, Color4f color)
43 {
44     foreach(i, c; s)
45     {
46         foreach(cx; 0..5)
47         foreach(cy; 0..8)
48         {
49             Color4f col = sampleChar(c, cx, cy, color);
50             uint px = x + 6 * cast(int)i + cx;
51             uint py = y + cy;
52             img[px, py] = alphaOver(img[px, py], col);
53         }
54     }
55 }
56 
57 Color4f sampleChar(char c, int x, int y, Color4f color)
58 {
59     uint index = cast(uint)c * 5;
60     if (x < 0) x = 0;
61     if (x >= 5) x = 4;
62     if (y < 0) y = 0;
63     if (y >= 5 * 8) y = 5 * 8 - 1;
64     ubyte col = font[index + x];
65     auto val = col & (0x01 << y);
66     if (val)
67         return color;
68     else
69         return Color4f(0, 0, 0, 0);
70 }
71 
72 /* 
73  * 5x8 ASCII character data.
74  * Each 5 byte chunk represents one character (5 columns, 8 bits per column)
75  */
76 immutable(ubyte)[] font =
77 [
78     0x00, 0x00, 0x00, 0x00, 0x00,
79     0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
80     0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
81     0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
82     0x18, 0x3C, 0x7E, 0x3C, 0x18,
83     0x1C, 0x57, 0x7D, 0x57, 0x1C,
84     0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
85     0x00, 0x18, 0x3C, 0x18, 0x00,
86     0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
87     0x00, 0x18, 0x24, 0x18, 0x00,
88     0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
89     0x30, 0x48, 0x3A, 0x06, 0x0E,
90     0x26, 0x29, 0x79, 0x29, 0x26,
91     0x40, 0x7F, 0x05, 0x05, 0x07,
92     0x40, 0x7F, 0x05, 0x25, 0x3F,
93     0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
94     0x7F, 0x3E, 0x1C, 0x1C, 0x08,
95     0x08, 0x1C, 0x1C, 0x3E, 0x7F,
96     0x14, 0x22, 0x7F, 0x22, 0x14,
97     0x5F, 0x5F, 0x00, 0x5F, 0x5F,
98     0x06, 0x09, 0x7F, 0x01, 0x7F,
99     0x00, 0x66, 0x89, 0x95, 0x6A,
100     0x60, 0x60, 0x60, 0x60, 0x60,
101     0x94, 0xA2, 0xFF, 0xA2, 0x94,
102     0x08, 0x04, 0x7E, 0x04, 0x08,
103     0x10, 0x20, 0x7E, 0x20, 0x10,
104     0x08, 0x08, 0x2A, 0x1C, 0x08,
105     0x08, 0x1C, 0x2A, 0x08, 0x08,
106     0x1E, 0x10, 0x10, 0x10, 0x10,
107     0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
108     0x30, 0x38, 0x3E, 0x38, 0x30,
109     0x06, 0x0E, 0x3E, 0x0E, 0x06,
110     0x00, 0x00, 0x00, 0x00, 0x00,
111     0x00, 0x00, 0x5F, 0x00, 0x00,
112     0x00, 0x07, 0x00, 0x07, 0x00,
113     0x14, 0x7F, 0x14, 0x7F, 0x14,
114     0x24, 0x2A, 0x7F, 0x2A, 0x12,
115     0x23, 0x13, 0x08, 0x64, 0x62,
116     0x36, 0x49, 0x56, 0x20, 0x50,
117     0x00, 0x08, 0x07, 0x03, 0x00,
118     0x00, 0x1C, 0x22, 0x41, 0x00,
119     0x00, 0x41, 0x22, 0x1C, 0x00,
120     0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
121     0x08, 0x08, 0x3E, 0x08, 0x08,
122     0x00, 0x80, 0x70, 0x30, 0x00,
123     0x08, 0x08, 0x08, 0x08, 0x08,
124     0x00, 0x00, 0x60, 0x60, 0x00,
125     0x20, 0x10, 0x08, 0x04, 0x02,
126     0x3E, 0x51, 0x49, 0x45, 0x3E,
127     0x00, 0x42, 0x7F, 0x40, 0x00,
128     0x72, 0x49, 0x49, 0x49, 0x46,
129     0x21, 0x41, 0x49, 0x4D, 0x33,
130     0x18, 0x14, 0x12, 0x7F, 0x10,
131     0x27, 0x45, 0x45, 0x45, 0x39,
132     0x3C, 0x4A, 0x49, 0x49, 0x31,
133     0x41, 0x21, 0x11, 0x09, 0x07,
134     0x36, 0x49, 0x49, 0x49, 0x36,
135     0x46, 0x49, 0x49, 0x29, 0x1E,
136     0x00, 0x00, 0x14, 0x00, 0x00,
137     0x00, 0x40, 0x34, 0x00, 0x00,
138     0x00, 0x08, 0x14, 0x22, 0x41,
139     0x14, 0x14, 0x14, 0x14, 0x14,
140     0x00, 0x41, 0x22, 0x14, 0x08,
141     0x02, 0x01, 0x59, 0x09, 0x06,
142     0x3E, 0x41, 0x5D, 0x59, 0x4E,
143     0x7C, 0x12, 0x11, 0x12, 0x7C,
144     0x7F, 0x49, 0x49, 0x49, 0x36,
145     0x3E, 0x41, 0x41, 0x41, 0x22,
146     0x7F, 0x41, 0x41, 0x41, 0x3E,
147     0x7F, 0x49, 0x49, 0x49, 0x41,
148     0x7F, 0x09, 0x09, 0x09, 0x01,
149     0x3E, 0x41, 0x41, 0x51, 0x73,
150     0x7F, 0x08, 0x08, 0x08, 0x7F,
151     0x00, 0x41, 0x7F, 0x41, 0x00,
152     0x20, 0x40, 0x41, 0x3F, 0x01,
153     0x7F, 0x08, 0x14, 0x22, 0x41,
154     0x7F, 0x40, 0x40, 0x40, 0x40,
155     0x7F, 0x02, 0x1C, 0x02, 0x7F,
156     0x7F, 0x04, 0x08, 0x10, 0x7F,
157     0x3E, 0x41, 0x41, 0x41, 0x3E,
158     0x7F, 0x09, 0x09, 0x09, 0x06,
159     0x3E, 0x41, 0x51, 0x21, 0x5E,
160     0x7F, 0x09, 0x19, 0x29, 0x46,
161     0x26, 0x49, 0x49, 0x49, 0x32,
162     0x03, 0x01, 0x7F, 0x01, 0x03,
163     0x3F, 0x40, 0x40, 0x40, 0x3F,
164     0x1F, 0x20, 0x40, 0x20, 0x1F,
165     0x3F, 0x40, 0x38, 0x40, 0x3F,
166     0x63, 0x14, 0x08, 0x14, 0x63,
167     0x03, 0x04, 0x78, 0x04, 0x03,
168     0x61, 0x59, 0x49, 0x4D, 0x43,
169     0x00, 0x7F, 0x41, 0x41, 0x41,
170     0x02, 0x04, 0x08, 0x10, 0x20,
171     0x00, 0x41, 0x41, 0x41, 0x7F,
172     0x04, 0x02, 0x01, 0x02, 0x04,
173     0x40, 0x40, 0x40, 0x40, 0x40,
174     0x00, 0x03, 0x07, 0x08, 0x00,
175     0x20, 0x54, 0x54, 0x78, 0x40,
176     0x7F, 0x28, 0x44, 0x44, 0x38,
177     0x38, 0x44, 0x44, 0x44, 0x28,
178     0x38, 0x44, 0x44, 0x28, 0x7F,
179     0x38, 0x54, 0x54, 0x54, 0x18,
180     0x00, 0x08, 0x7E, 0x09, 0x02,
181     0x18, 0xA4, 0xA4, 0x9C, 0x78,
182     0x7F, 0x08, 0x04, 0x04, 0x78,
183     0x00, 0x44, 0x7D, 0x40, 0x00,
184     0x20, 0x40, 0x40, 0x3D, 0x00,
185     0x7F, 0x10, 0x28, 0x44, 0x00,
186     0x00, 0x41, 0x7F, 0x40, 0x00,
187     0x7C, 0x04, 0x78, 0x04, 0x78,
188     0x7C, 0x08, 0x04, 0x04, 0x78,
189     0x38, 0x44, 0x44, 0x44, 0x38,
190     0xFC, 0x18, 0x24, 0x24, 0x18,
191     0x18, 0x24, 0x24, 0x18, 0xFC,
192     0x7C, 0x08, 0x04, 0x04, 0x08,
193     0x48, 0x54, 0x54, 0x54, 0x24,
194     0x04, 0x04, 0x3F, 0x44, 0x24,
195     0x3C, 0x40, 0x40, 0x20, 0x7C,
196     0x1C, 0x20, 0x40, 0x20, 0x1C,
197     0x3C, 0x40, 0x30, 0x40, 0x3C,
198     0x44, 0x28, 0x10, 0x28, 0x44,
199     0x4C, 0x90, 0x90, 0x90, 0x7C,
200     0x44, 0x64, 0x54, 0x4C, 0x44,
201     0x00, 0x08, 0x36, 0x41, 0x00,
202     0x00, 0x00, 0x77, 0x00, 0x00,
203     0x00, 0x41, 0x36, 0x08, 0x00,
204     0x02, 0x01, 0x02, 0x04, 0x02,
205     0x3C, 0x26, 0x23, 0x26, 0x3C,
206     0x1E, 0xA1, 0xA1, 0x61, 0x12,
207     0x3A, 0x40, 0x40, 0x20, 0x7A,
208     0x38, 0x54, 0x54, 0x55, 0x59,
209     0x21, 0x55, 0x55, 0x79, 0x41,
210     0x22, 0x54, 0x54, 0x78, 0x42,
211     0x21, 0x55, 0x54, 0x78, 0x40,
212     0x20, 0x54, 0x55, 0x79, 0x40,
213     0x0C, 0x1E, 0x52, 0x72, 0x12,
214     0x39, 0x55, 0x55, 0x55, 0x59,
215     0x39, 0x54, 0x54, 0x54, 0x59,
216     0x39, 0x55, 0x54, 0x54, 0x58,
217     0x00, 0x00, 0x45, 0x7C, 0x41,
218     0x00, 0x02, 0x45, 0x7D, 0x42,
219     0x00, 0x01, 0x45, 0x7C, 0x40,
220     0x7D, 0x12, 0x11, 0x12, 0x7D,
221     0xF0, 0x28, 0x25, 0x28, 0xF0,
222     0x7C, 0x54, 0x55, 0x45, 0x00,
223     0x20, 0x54, 0x54, 0x7C, 0x54,
224     0x7C, 0x0A, 0x09, 0x7F, 0x49,
225     0x32, 0x49, 0x49, 0x49, 0x32,
226     0x3A, 0x44, 0x44, 0x44, 0x3A,
227     0x32, 0x4A, 0x48, 0x48, 0x30,
228     0x3A, 0x41, 0x41, 0x21, 0x7A,
229     0x3A, 0x42, 0x40, 0x20, 0x78,
230     0x00, 0x9D, 0xA0, 0xA0, 0x7D,
231     0x3D, 0x42, 0x42, 0x42, 0x3D,
232     0x3D, 0x40, 0x40, 0x40, 0x3D,
233     0x3C, 0x24, 0xFF, 0x24, 0x24,
234     0x48, 0x7E, 0x49, 0x43, 0x66,
235     0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
236     0xFF, 0x09, 0x29, 0xF6, 0x20,
237     0xC0, 0x88, 0x7E, 0x09, 0x03,
238     0x20, 0x54, 0x54, 0x79, 0x41,
239     0x00, 0x00, 0x44, 0x7D, 0x41,
240     0x30, 0x48, 0x48, 0x4A, 0x32,
241     0x38, 0x40, 0x40, 0x22, 0x7A,
242     0x00, 0x7A, 0x0A, 0x0A, 0x72,
243     0x7D, 0x0D, 0x19, 0x31, 0x7D,
244     0x26, 0x29, 0x29, 0x2F, 0x28,
245     0x26, 0x29, 0x29, 0x29, 0x26,
246     0x30, 0x48, 0x4D, 0x40, 0x20,
247     0x38, 0x08, 0x08, 0x08, 0x08,
248     0x08, 0x08, 0x08, 0x08, 0x38,
249     0x2F, 0x10, 0xC8, 0xAC, 0xBA,
250     0x2F, 0x10, 0x28, 0x34, 0xFA,
251     0x00, 0x00, 0x7B, 0x00, 0x00,
252     0x08, 0x14, 0x2A, 0x14, 0x22,
253     0x22, 0x14, 0x2A, 0x14, 0x08,
254     0x55, 0x00, 0x55, 0x00, 0x55,
255     0xAA, 0x55, 0xAA, 0x55, 0xAA,
256     0xFF, 0x55, 0xFF, 0x55, 0xFF,
257     0x00, 0x00, 0x00, 0xFF, 0x00,
258     0x10, 0x10, 0x10, 0xFF, 0x00,
259     0x14, 0x14, 0x14, 0xFF, 0x00,
260     0x10, 0x10, 0xFF, 0x00, 0xFF,
261     0x10, 0x10, 0xF0, 0x10, 0xF0,
262     0x14, 0x14, 0x14, 0xFC, 0x00,
263     0x14, 0x14, 0xF7, 0x00, 0xFF,
264     0x00, 0x00, 0xFF, 0x00, 0xFF,
265     0x14, 0x14, 0xF4, 0x04, 0xFC,
266     0x14, 0x14, 0x17, 0x10, 0x1F,
267     0x10, 0x10, 0x1F, 0x10, 0x1F,
268     0x14, 0x14, 0x14, 0x1F, 0x00,
269     0x10, 0x10, 0x10, 0xF0, 0x00,
270     0x00, 0x00, 0x00, 0x1F, 0x10,
271     0x10, 0x10, 0x10, 0x1F, 0x10,
272     0x10, 0x10, 0x10, 0xF0, 0x10,
273     0x00, 0x00, 0x00, 0xFF, 0x10,
274     0x10, 0x10, 0x10, 0x10, 0x10,
275     0x10, 0x10, 0x10, 0xFF, 0x10,
276     0x00, 0x00, 0x00, 0xFF, 0x14,
277     0x00, 0x00, 0xFF, 0x00, 0xFF,
278     0x00, 0x00, 0x1F, 0x10, 0x17,
279     0x00, 0x00, 0xFC, 0x04, 0xF4,
280     0x14, 0x14, 0x17, 0x10, 0x17,
281     0x14, 0x14, 0xF4, 0x04, 0xF4,
282     0x00, 0x00, 0xFF, 0x00, 0xF7,
283     0x14, 0x14, 0x14, 0x14, 0x14,
284     0x14, 0x14, 0xF7, 0x00, 0xF7,
285     0x14, 0x14, 0x14, 0x17, 0x14,
286     0x10, 0x10, 0x1F, 0x10, 0x1F,
287     0x14, 0x14, 0x14, 0xF4, 0x14,
288     0x10, 0x10, 0xF0, 0x10, 0xF0,
289     0x00, 0x00, 0x1F, 0x10, 0x1F,
290     0x00, 0x00, 0x00, 0x1F, 0x14,
291     0x00, 0x00, 0x00, 0xFC, 0x14,
292     0x00, 0x00, 0xF0, 0x10, 0xF0,
293     0x10, 0x10, 0xFF, 0x10, 0xFF,
294     0x14, 0x14, 0x14, 0xFF, 0x14,
295     0x10, 0x10, 0x10, 0x1F, 0x00,
296     0x00, 0x00, 0x00, 0xF0, 0x10,
297     0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
298     0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
299     0xFF, 0xFF, 0xFF, 0x00, 0x00,
300     0x00, 0x00, 0x00, 0xFF, 0xFF,
301     0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
302     0x38, 0x44, 0x44, 0x38, 0x44,
303     0xFC, 0x4A, 0x4A, 0x4A, 0x34,
304     0x7E, 0x02, 0x02, 0x06, 0x06,
305     0x02, 0x7E, 0x02, 0x7E, 0x02,
306     0x63, 0x55, 0x49, 0x41, 0x63,
307     0x38, 0x44, 0x44, 0x3C, 0x04,
308     0x40, 0x7E, 0x20, 0x1E, 0x20,
309     0x06, 0x02, 0x7E, 0x02, 0x02,
310     0x99, 0xA5, 0xE7, 0xA5, 0x99,
311     0x1C, 0x2A, 0x49, 0x2A, 0x1C,
312     0x4C, 0x72, 0x01, 0x72, 0x4C,
313     0x30, 0x4A, 0x4D, 0x4D, 0x30,
314     0x30, 0x48, 0x78, 0x48, 0x30,
315     0xBC, 0x62, 0x5A, 0x46, 0x3D,
316     0x3E, 0x49, 0x49, 0x49, 0x00,
317     0x7E, 0x01, 0x01, 0x01, 0x7E,
318     0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
319     0x44, 0x44, 0x5F, 0x44, 0x44,
320     0x40, 0x51, 0x4A, 0x44, 0x40,
321     0x40, 0x44, 0x4A, 0x51, 0x40,
322     0x00, 0x00, 0xFF, 0x01, 0x03,
323     0xE0, 0x80, 0xFF, 0x00, 0x00,
324     0x08, 0x08, 0x6B, 0x6B, 0x08,
325     0x36, 0x12, 0x36, 0x24, 0x36,
326     0x06, 0x0F, 0x09, 0x0F, 0x06,
327     0x00, 0x00, 0x18, 0x18, 0x00,
328     0x00, 0x00, 0x10, 0x10, 0x00,
329     0x30, 0x40, 0xFF, 0x01, 0x01,
330     0x00, 0x1F, 0x01, 0x01, 0x1E,
331     0x00, 0x19, 0x1D, 0x17, 0x12,
332     0x00, 0x3C, 0x3C, 0x3C, 0x3C
333 ];