Mirai's Miscellaneous Misadventures
M28 / core / text.c
1
2
3
4#include <mimimi/engines.h>
5#include <mimimi/chapters.h>
6#include <mimimi/fonts.h>
7#include <mimimi/ground.h>
8#include <mimimi/assets.h>
9
10static void mimimi_stamp_glyph(struct mimimi_image *target, int x0, int y0, struct mimimi_image *source)
11{
12 for (int y1 = 0 ; y1 < source->height ; y1++)
13 for (int x1 = 0 ; x1 < source->width ; x1++)
14 {
15 int x = x0 + x1;
16 int y = y0 + y1;
17
18 if (x < 0) continue;
19 if (y < 0) continue;
20
21 if (x >= target->width) continue;
22 if (y >= target->height) continue;
23
24 unsigned char color = source->colors[x1 + y1 * source->width];
25 if (color == 0) continue;
26
27 target->colors[x + y * target->width] = color;
28 }
29}
30
31int mimimi_measure_text(struct mimimi_image *glyphs, char *text)
32{
33 int x = 0;
34 while (*text != 0)
35 {
36 char ch = *text++;
37
38 if (ch == 0x20)
39 {
40 x += 4;
41 continue;
42 }
43
44 if (ch <= 0x20) continue;
45 if (ch >= 0x7F) continue;
46
47 struct mimimi_image *glyph = glyphs + (ch - 0x20);
48 x += glyph->width - 1;
49 }
50
51 return x;
52}
53
54int mimimi_text(struct mimimi_image *image, struct mimimi_image *glyphs, int x, int y, char *text)
55{
56 while (*text != 0)
57 {
58 char ch = *text++;
59
60 if (ch == 0x20)
61 {
62 x += 4;
63 continue;
64 }
65
66 if (ch <= 0x20) continue;
67 if (ch >= 0x7F) continue;
68
69 struct mimimi_image *glyph = glyphs + (ch - 0x20);
70
71 mimimi_stamp_glyph(image, x - 1, y - glyph->height + 6, glyph);
72 x += glyph->width - 1;
73 }
74
75 return x;
76}