1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include "til_fb.h"
#include "ascii/ascii.h"
#include "txt.h"
struct txt_t {
int len, width, height;
char str[];
};
/* compute the rectangle dimensions of the string in rendered pixels */
static void measure_str(const char *str, int *res_width, int *res_height)
{
int rows = 1, cols = 0, col = 0;
char c;
assert(str);
assert(res_width);
assert(res_height);
while ((c = *str)) {
switch (c) {
case ' '...'~':
col++;
break;
case '\n':
if (col > cols)
cols = col;
col = 0;
rows++;
break;
default:
break;
}
str++;
}
*res_height = 1 + rows * (ASCII_HEIGHT + 1);
*res_width = 1 + cols * (ASCII_WIDTH + 1);
}
txt_t * txt_new(const char *str)
{
txt_t *txt;
int len;
assert(str);
len = strlen(str);
txt = calloc(1, sizeof(txt_t) + len + 1);
if (!txt)
return NULL;
txt->len = len;
memcpy(txt->str, str, len);
measure_str(txt->str, &txt->width, &txt->height);
return txt;
}
txt_t * txt_newf(const char *fmt, ...)
{
char buf[1024] = {}; /* XXX: it's not expected there will be long strings */
va_list ap;
assert(fmt);
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
va_end(ap);
return txt_new(buf);
}
txt_t * txt_free(txt_t *txt)
{
free(txt);
return NULL;
}
/* Adjusts x and y according to alignment, width, and height. Returning the adjusted x and y
* in res_x, res_y.
*
* res_x,res_y will be the coordinate of the upper left corner of the rect
* described by width,height when aligned relative to x,y according to the
* specified alignment.
*
* e.g. if an alignment of TXT_HALIGN_LEFT,TXT_VALIGN_TOP is supplied, x,y is returned verbatim
* in res_x,res_y.
* an alignment of TXT_HALIGN_CENTER,TXT_VALIGN_CENTER returns x-width/2 and y-width/2.
*/
static void justify(txt_align_t alignment, int x, int y, unsigned width, unsigned height, int *res_x, int *res_y)
{
assert(res_x);
assert(res_y);
switch (alignment.horiz) {
case TXT_HALIGN_CENTER:
x -= width >> 1;
break;
case TXT_HALIGN_LEFT:
break;
case TXT_HALIGN_RIGHT:
x -= width;
break;
default:
assert(0);
}
switch (alignment.vert) {
case TXT_VALIGN_CENTER:
y -= height >> 1;
break;
case TXT_VALIGN_TOP:
break;
case TXT_VALIGN_BOTTOM:
y -= height;
break;
default:
assert(0);
}
*res_x = x;
*res_y = y;
}
static int overlaps(int x1, int y1, unsigned w1, unsigned h1, int x2, int y2, unsigned w2, unsigned h2)
{
/* TODO */
return 1;
}
static inline void draw_char(til_fb_fragment_t *fragment, uint32_t color, int x, int y, char c)
{
/* TODO: this could be optimized to skip characters with no overlap */
for (int i = 0; i < ASCII_HEIGHT; i++) {
for (int j = 0; j < ASCII_WIDTH; j++) {
if (ascii_chars[c][i * ASCII_WIDTH + j])
til_fb_fragment_put_pixel_checked(fragment, 0, x + j, y + i, color);
}
}
}
void txt_render_fragment(txt_t *txt, til_fb_fragment_t *fragment, uint32_t color, int x, int y, txt_align_t alignment)
{
int jx, jy, col, row;
char c, *str;
assert(txt);
assert(fragment);
justify(alignment, x, y, txt->width, txt->height, &jx, &jy);
if (!overlaps(jx, jy, txt->width, txt->height,
fragment->x, fragment->y,
fragment->width, fragment->height))
return;
for (col = 0, row = 0, str = txt->str; *str; str++) {
switch (*str) {
case ' '...'~':
draw_char(fragment, color, jx + 1 + col * (ASCII_WIDTH + 1), jy + 1 + row * (ASCII_HEIGHT + 1), *str);
col++;
break;
case '\n':
col = 0;
row++;
break;
default:
break;
}
}
}
|