HOG2
MonoFont.cpp
Go to the documentation of this file.
1 /*
2  * $Id: MonoFont.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 04/12/04.
6  * Modified by Nathan Sturtevant on 06/27/21.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #include <string.h>
13 #include "MonoFont.h"
14 
20 void MonoFont::GetTextLines(std::vector<Graphics::Display::lineInfo> &lines,
21  Graphics::point location,
22  const char *text, float height,
23  const rgbColor &color,
25 {
26  lines.resize(0);
27  Graphics::point loc = location;
28  // 1. Get character width / space size / text length
29  auto length = strlen(text);
30  float charLength = 5.0f*height/9.0f + 0.5f*height/9.0f;
31  float textLength = length*charLength-0.5f*height/9.0f;
32 // float charLength = height/2.0f + height/4.0f;
33 // float textLength = length*charLength-height/4.0f;
34 
35  // 2. Get top/left location of text
36  loc = location;
37  switch (base)
38  {
40  break;
42  loc.y -= height*0.5f;
43  break;
45  loc.y -= height;
46  break;
47  }
48  switch (align)
49  {
51  loc.x -= textLength*0.5f;
52  break;
54  break;
56  loc.x -= textLength;
57  break;
58  }
59 
60  // 3. Draw text
61  for (int x = 0; x < length; x++)
62  {
63  DrawChar(lines, text[x], loc, height, color);
64  loc.x += charLength;
65  }
66 }
67 
74  Graphics::point location,
75  const char *text, float height,
76  const rgbColor &color,
78 {
79  Graphics::point loc = location;
80  // 1. Get character width / space size / text length
81  auto length = strlen(text);
82  float charLength = 5.0f*height/9.0f + 0.5f*height/9.0f;
83  float textLength = length*charLength-0.5f*height/9.0f;
84 
85  // 2. Get top/left location of text
86  loc = location;
87  switch (base)
88  {
90  break;
92  loc.y -= height*0.5f;
93  break;
95  loc.y -= height;
96  break;
97  }
98  switch (align)
99  {
101  loc.x -= textLength*0.5f;
102  break;
104  break;
106  loc.x -= textLength;
107  break;
108  }
109 
110  // 3. Draw text
111  for (int x = 0; x < length; x++)
112  {
113  DrawChar(display, text[x], loc, height, color);
114  loc.x += charLength;
115  }
116 }
117 /*
118  * _ _ 0, 1
119  * |X|X| 2, 3/, 4\, 5, 6/, 7\, 8
120  * |\|/| 9, 10, 11, 12, 13
121  * - - 14, 15
122  * |/|\| 16, 17, 18, 19, 20
123  * |X|X| 21, 22/, 23\, 24, 25/, 26\, 27
124  * - - 28, 29
125  *
126  */
127 
128 void MonoFont::DrawChar(Graphics::Display &display, char c, point3d where, float height, rgbColor color)
129 {
130  uint32_t bMap = GetBitmap(toupper(c));
131  for (int x = 0; x < 30; x++)
132  {
133  if (bMap&0x1)
134  {
135  switch (x) {
136  case 0: DrawLine(display, where, 0, 0, 1, 0, height, color); break;
137  case 1: DrawLine(display, where, 1, 0, 1, 0, height, color); break;
138  case 2: DrawLine(display, where, 0, 0, 0, 1, height, color); break;
139  case 3: DrawLine(display, where, 0, 1, 1, -1, height, color); break;
140  case 4: DrawLine(display, where, 0, 0, 1, 1, height, color); break;
141  case 5: DrawLine(display, where, 1, 0, 0, 1, height, color); break;
142  case 6: DrawLine(display, where, 1, 1, 1, -1, height, color); break;
143  case 7: DrawLine(display, where, 1, 0, 1, 1, height, color); break;
144  case 8: DrawLine(display, where, 2, 0, 0, 1, height, color); break;
145  case 9: DrawLine(display, where, 0, 1, 0, 1, height, color); break;
146  case 10: DrawLine(display, where, 0, 1, 1, 1, height, color); break;
147  case 11: DrawLine(display, where, 1, 1, 0, 1, height, color); break;
148  case 12: DrawLine(display, where, 1, 2, 1, -1, height, color); break;
149  case 13: DrawLine(display, where, 2, 1, 0, 1, height, color); break;
150  case 14: DrawLine(display, where, 0, 2, 1, 0, height, color); break;
151  case 15: DrawLine(display, where, 1, 2, 1, 0, height, color); break;
152  case 16: DrawLine(display, where, 0, 2, 0, 1, height, color); break;
153  case 17: DrawLine(display, where, 0, 3, 1, -1, height, color); break;
154  case 18: DrawLine(display, where, 1, 2, 0, 1, height, color); break;
155  case 19: DrawLine(display, where, 1, 2, 1, 1, height, color); break;
156  case 20: DrawLine(display, where, 2, 2, 0, 1, height, color); break;
157  case 21: DrawLine(display, where, 0, 3, 0, 1, height, color); break;
158  case 22: DrawLine(display, where, 0, 4, 1, -1, height, color); break;
159  case 23: DrawLine(display, where, 0, 3, 1, 1, height, color); break;
160  case 24: DrawLine(display, where, 1, 3, 0, 1, height, color); break;
161  case 25: DrawLine(display, where, 1, 4, 1, -1, height, color); break;
162  case 26: DrawLine(display, where, 1, 3, 1, 1, height, color); break;
163  case 27: DrawLine(display, where, 2, 3, 0, 1, height, color); break;
164  case 28: DrawLine(display, where, 0, 4, 1, 0, height, color); break;
165  case 29: DrawLine(display, where, 1, 4, 1, 0, height, color); break;
166  }
167  }
168  bMap >>= 1;
169  }
170 }
171 
172 void MonoFont::DrawChar(std::vector<Graphics::Display::lineInfo> &display, char c, point3d where, float height, rgbColor color)
173 {
174  uint32_t bMap = GetBitmap(toupper(c));
175  for (int x = 0; x < 30; x++)
176  {
177  if (bMap&0x1)
178  {
179  switch (x) {
180  case 0: DrawLine(display, where, 0, 0, 1, 0, height, color); break;
181  case 1: DrawLine(display, where, 1, 0, 1, 0, height, color); break;
182  case 2: DrawLine(display, where, 0, 0, 0, 1, height, color); break;
183  case 3: DrawLine(display, where, 0, 1, 1, -1, height, color); break;
184  case 4: DrawLine(display, where, 0, 0, 1, 1, height, color); break;
185  case 5: DrawLine(display, where, 1, 0, 0, 1, height, color); break;
186  case 6: DrawLine(display, where, 1, 1, 1, -1, height, color); break;
187  case 7: DrawLine(display, where, 1, 0, 1, 1, height, color); break;
188  case 8: DrawLine(display, where, 2, 0, 0, 1, height, color); break;
189  case 9: DrawLine(display, where, 0, 1, 0, 1, height, color); break;
190  case 10: DrawLine(display, where, 0, 1, 1, 1, height, color); break;
191  case 11: DrawLine(display, where, 1, 1, 0, 1, height, color); break;
192  case 12: DrawLine(display, where, 1, 2, 1, -1, height, color); break;
193  case 13: DrawLine(display, where, 2, 1, 0, 1, height, color); break;
194  case 14: DrawLine(display, where, 0, 2, 1, 0, height, color); break;
195  case 15: DrawLine(display, where, 1, 2, 1, 0, height, color); break;
196  case 16: DrawLine(display, where, 0, 2, 0, 1, height, color); break;
197  case 17: DrawLine(display, where, 0, 3, 1, -1, height, color); break;
198  case 18: DrawLine(display, where, 1, 2, 0, 1, height, color); break;
199  case 19: DrawLine(display, where, 1, 2, 1, 1, height, color); break;
200  case 20: DrawLine(display, where, 2, 2, 0, 1, height, color); break;
201  case 21: DrawLine(display, where, 0, 3, 0, 1, height, color); break;
202  case 22: DrawLine(display, where, 0, 4, 1, -1, height, color); break;
203  case 23: DrawLine(display, where, 0, 3, 1, 1, height, color); break;
204  case 24: DrawLine(display, where, 1, 3, 0, 1, height, color); break;
205  case 25: DrawLine(display, where, 1, 4, 1, -1, height, color); break;
206  case 26: DrawLine(display, where, 1, 3, 1, 1, height, color); break;
207  case 27: DrawLine(display, where, 2, 3, 0, 1, height, color); break;
208  case 28: DrawLine(display, where, 0, 4, 1, 0, height, color); break;
209  case 29: DrawLine(display, where, 1, 4, 1, 0, height, color); break;
210  }
211  }
212  bMap >>= 1;
213  }
214 }
215 
216 uint32_t MonoFont::GetBitmap(char c)
217 {
218  uint32_t index[128] = {
219  0x0, // 0
220  0x0, // 1
221  0x0, // 2
222  0x0, // 3
223  0x0, // 4
224  0x0, // 5
225  0x0, // 6
226  0x0, // 7
227  0x0, // 8
228  0x0, // 9
229  0x0, // 10
230  0x0, // 11
231  0x0, // 12
232  0x0, // 13
233  0x0, // 14
234  0x0, // 15
235  0x0, // 16
236  0x0, // 17
237  0x0, // 18
238  0x0, // 19
239  0x0, // 20
240  0x0, // 21
241  0x0, // 22
242  0x0, // 23
243  0x0, // 24
244  0x0, // 25
245  0x0, // 26
246  0x0, // 27
247  0x0, // 28
248  0x0, // 29
249  0x0, // 30
250  0x0, // 31
251  0x0, // 32
252  (1<<5)|(1<<11)|(1<<24), // 33 !
253  (1<<5)|(1<<8), // 34 "
254  0x0, // 35 #
255  0x0, // 36 $
256  0x0, // 37 %
257  0x0, // 38 &
258  (1<<5), // 39 '
259  (1<<3)|(1<<9)|(1<<16)|(1<<23), // 40 (
260  (1<<7)|(1<<13)|(1<<20)|(1<<25), // 41 )
261  (1<<10)|(1<<11)|(1<<12)|(1<<14)|(1<<15)|(1<<17)|(1<<18)|(1<<19), // 42 *
262  0x0, // 43 +
263  (1<<22), // 44 ,
264  (1<<15)|(1<<14), // 45 -
265  (1<<22)|(1<<23), // 46 .
266  (1<<8)|(1<<12)|(1<<18)|(1<<22), // 47 /
267  // 48 0
268  (1<<3)|(1<<7)|(1<<9)|(1<<12)|(1<<13)|(1<<16)|(1<<17)|(1<<20)|(1<<23)|(1<<25),
269  // 49 1
270  (1<<3)|(1<<5)|(1<<11)|(1<<18)|(1<<24)|(1<<28)|(1<<29),
271  // 50 2
272  (1<<3)|(1<<1)|(1<<8)|(1<<13)|(1<<15)|(1<<17)|(1<<21)|(1<<28)|(1<<29),
273  // 51 3
274  (1<<0)|(1<<7)|(1<<13)|(1<<15)|(1<<20)|(1<<25)|(1<<28),
275  // 52 4
276  (1<<3)|(1<<9)|(1<<14)|(1<<15)|(1<<11)|(1<<18)|(1<<24),
277  // 53 5
278  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<14)|(1<<19)|(1<<27)|(1<<28)|(1<<29),
279  // 54 6
280  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<14)|(1<<19)|(1<<27)|(1<<28)|(1<<29)|(1<<16)|(1<<21),
281  // 55 7
282  (1<<0)|(1<<1)|(1<<8)|(1<<12)|(1<<18)|(1<<24),
283  // 56 8
284  (1<<0)|(1<<1)|(1<<2)|(1<<8)|(1<<9)|(1<<13)|(1<<14)|(1<<15)|(1<<16)|(1<<20)|(1<<21)|(1<<27)|(1<<28)|(1<<29),
285  // 57 9
286  (1<<0)|(1<<1)|(1<<2)|(1<<8)|(1<<9)|(1<<13)|(1<<14)|(1<<15)|(1<<20)|(1<<27)|(1<<28)|(1<<29),
287  (1<<24)|(1<<11), // 58 :
288  (1<<22)|(1<<11), // 59 ;
289  0x0, // 60 <
290  0x0, // 61 =
291  0x0, // 62 >
292  (1<<2)|(1<<1)|(1<<0)|(1<<8)|(1<<12)|(1<<24), // 63 ?
293  0x0, // 64 @
294  // 65 A
295  (1<<1)|(1<<3)|(1<<8)|(1<<9)|(1<<13)|(1<<14)|(1<<15)|(1<<16)|(1<<20)|(1<<21)|(1<<27),
296  // 66 B
297  (1<<0)|(1<<2)|(1<<7)|(1<<9)|(1<<12)|(1<<14)|(1<<16)|(1<<19)|(1<<21)|(1<<27)|(1<<28)|(1<<29),
298  // 67 C
299  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<28)|(1<<29),
300  // 68 D
301  (1<<0)|(1<<2)|(1<<7)|(1<<9)|(1<<13)|(1<<16)|(1<<20)|(1<<21)|(1<<25)|(1<<28),
302  // 69 E
303  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<14)|(1<<16)|(1<<21)|(1<<28)|(1<<29),
304  // 70 F
305  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<14)|(1<<16)|(1<<21),
306  // 71 G
307  (1<<0)|(1<<1)|(1<<2)|(1<<8)|(1<<9)|(1<<15)|(1<<16)|(1<<20)|(1<<21)|(1<<25)|(1<<28),
308  // 72 H
309  (1<<2)|(1<<8)|(1<<9)|(1<<13)|(1<<14)|(1<<15)|(1<<16)|(1<<20)|(1<<21)|(1<<27),
310  // 73 I
311  (1<<0)|(1<<1)|(1<<5)|(1<<11)|(1<<18)|(1<<24)|(1<<28)|(1<<29),
312  // 74 J
313  (1<<1)|(1<<8)|(1<<13)|(1<<20)|(1<<21)|(1<<25)|(1<<28),
314  // 75 K
315  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<14)|(1<<12)|(1<<19)|(1<<8)|(1<<27),
316  // 76 L
317  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<28)|(1<<29),
318  // 77 M
319  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<27)|(1<<0)|(1<<1)|(1<<5)|(1<<11),
320  // 78 N
321  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<0)|(1<<7)|(1<<13)|(1<<20)|(1<<27),
322  // 79 O
323  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<27)|(1<<0)|(1<<1)|(1<<28)|(1<<29),
324  // 80 P
325  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<0)|(1<<14)|(1<<7)|(1<<12),
326  // 81 Q
327  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<0)|(1<<1)|(1<<28)|(1<<25)|(1<<26),
328  // 82 R
329  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<0)|(1<<14)|(1<<7)|(1<<13)|(1<<15)|(1<<19)|(1<<27),
330  // 83 S
331  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<14)|(1<<15)|(1<<20)|(1<<27)|(1<<28)|(1<<29),
332  // 84 T
333  (1<<0)|(1<<1)|(1<<5)|(1<<11)|(1<<18)|(1<<24),
334  // 85 U
335  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<27)|(1<<28)|(1<<29),
336  // 86 V
337  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<25)|(1<<28),
338  // 87 W
339  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<25)|(1<<28)|(1<<18)|(1<<24),
340  // 88 X
341  (1<<2)|(1<<21)|(1<<10)|(1<<17)|(1<<12)|(1<<19)|(1<<8)|(1<<27),
342  // 89 Y
343  (1<<2)|(1<<10)|(1<<18)|(1<<12)|(1<<24)|(1<<8),
344  // 90 Z
345  (1<<0)|(1<<1)|(1<<8)|(1<<12)|(1<<17)|(1<<21)|(1<<28)|(1<<29),
346  // 91
347  (1<<0)|(1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<28), // 91 [ // 0 2 9 16 21 28 // 1 8 13 20 27 29
348  0x0, // 92 (backslash)
349  (1<<1)|(1<<8)|(1<<13)|(1<<20)|(1<<27)|(1<<29), // 93 ]
350  0x0, // 94 ^
351  (1<<28)|(1<<29), // 95 _
352  0x0, // 96 `
353  0x0, // 97 a
354  0x0, // 98 b
355  0x0, // 99 c
356  0x0, // 100 d
357  0x0, // 101 e
358  0x0, // 102 f
359  0x0, // 103 g
360  0x0, // 104 h
361  0x0, // 105 i
362  0x0, // 106 j
363  0x0, // 107 k
364  0x0, // 108 l
365  0x0, // 109 m
366  0x0, // 110 n
367  0x0, // 111 o
368  0x0, // 112 p
369  0x0, // 113 q
370  0x0, // 114 r
371  0x0, // 115 s
372  0x0, // 116 t
373  0x0, // 117 u
374  0x0, // 118 v
375  0x0, // 119 w
376  0x0, // 120 x
377  0x0, // 121 y
378  0x0, // 122 z
379  0x0, // 123 {
380  0x0, // 124 |
381  0x0, // 125 }
382  0x0, // 126 ~
383  0x0, // 127 ESC
384  };
385  return index[(int)c];
386 }
387 
392 void MonoFont::DrawLine(Graphics::Display &display, point3d where, int startx, int starty, int offsetx, int offsety, float scale, rgbColor color)
393 {
394  //float unit = scale/21;
395 // float edgeLength = scale/4.0f;
396 // float lineWidth = scale/9.0f;
397  float lineWidth = scale/9.0f;
398  float edgeLength = 2.0f*lineWidth;//scale/4.0f;
399  Graphics::point p1(where.x+startx*edgeLength+lineWidth/2,
400  where.y+starty*edgeLength+lineWidth/2);
401  Graphics::point p2(where.x+startx*edgeLength+lineWidth/2+offsetx*edgeLength,
402  where.y+starty*edgeLength+lineWidth/2+offsety*edgeLength);
403  display.DrawLine(p1, p2, lineWidth, color);
404  // double unit = scale/4;
405  // glVertex2f(where.x+startx*unit, where.y-starty*unit);
406  // glVertex2f(where.x+startx*unit+offsetx*unit, where.y-starty*unit-offsety*unit);
407 }
408 
409 
410 
411 //struct lineInfo {
412 // point start, end;
413 // rgbColor c;
414 // float width;
415 // bool arrow;
416 //};
417 void MonoFont::DrawLine(std::vector<Graphics::Display::lineInfo> &lines, point3d where, int startx, int starty, int offsetx, int offsety, float scale, rgbColor color)
418 {
419  //float unit = scale/21;
420  float lineWidth = scale/9.0f;
421  float edgeLength = 2.0f*lineWidth;//scale/4.0f;
422  Graphics::point p1(where.x+startx*edgeLength+lineWidth/2,
423  where.y+starty*edgeLength+lineWidth/2);
424  Graphics::point p2(where.x+startx*edgeLength+lineWidth/2+offsetx*edgeLength,
425  where.y+starty*edgeLength+lineWidth/2+offsety*edgeLength);
426  lines.push_back({p1, p2, color, lineWidth, false});
427 }
428 
Graphics::Display::DrawLine
void DrawLine(point start, point end, float lineWidth, rgbColor c)
Definition: Graphics.cpp:184
Graphics::point
Definition: Graphics.h:32
loc::x
int x
Definition: MapGenerators.cpp:296
MonoFont.h
rgbColor
A color; r/g/b are between 0...1.
Definition: Colors.h:17
loc::y
int y
Definition: MapGenerators.cpp:296
Graphics::textBaseline
textBaseline
Definition: Graphics.h:25
Graphics::textAlignRight
@ textAlignRight
Definition: Graphics.h:22
Graphics::textBaselineMiddle
@ textBaselineMiddle
Definition: Graphics.h:27
loc
Definition: MapGenerators.cpp:296
MonoFont::DrawChar
void DrawChar(Graphics::Display &display, char c, point3d where, float height, rgbColor color)
Definition: MonoFont.cpp:128
point3d
#define point3d
Definition: GLUtil.h:123
MonoFont::DrawText
void DrawText(Graphics::Display &display, Graphics::point location, const char *text, float height, const rgbColor &color=Colors::black, Graphics::textAlign align=Graphics::textAlignLeft, Graphics::textBaseline base=Graphics::textBaselineBottom)
Definition: MonoFont.cpp:73
MonoFont::DrawLine
void DrawLine(Graphics::Display &display, point3d where, int startx, int starty, int offsetx, int offsety, float scale, rgbColor color)
Definition: MonoFont.cpp:392
Graphics::Display
Definition: Graphics.h:146
height
int height
Definition: SFML_HOG.cpp:54
Graphics::textAlignCenter
@ textAlignCenter
Definition: Graphics.h:20
Graphics::textBaselineTop
@ textBaselineTop
Definition: Graphics.h:26
MonoFont::GetBitmap
uint32_t GetBitmap(char c)
Definition: MonoFont.cpp:216
Graphics::textAlign
textAlign
Definition: Graphics.h:19
Graphics::textAlignLeft
@ textAlignLeft
Definition: Graphics.h:21
Graphics::textBaselineBottom
@ textBaselineBottom
Definition: Graphics.h:28
MonoFont::GetTextLines
void GetTextLines(std::vector< Graphics::Display::lineInfo > &lines, Graphics::point location, const char *text, float height, const rgbColor &color=Colors::black, Graphics::textAlign align=Graphics::textAlignLeft, Graphics::textBaseline base=Graphics::textBaselineBottom)
Definition: MonoFont.cpp:20