-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbitmap_text.c
338 lines (284 loc) · 8.81 KB
/
bitmap_text.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright (c) 2021 Justin Meiners
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bitmap_text.h"
#define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h" /* http://nothings.org/stb/stb_truetype.h */
#include <wchar.h>
#include <ctype.h>
#include <wctype.h>
// - Don't modify the string, just give ranges (we only take away).
// - Every character should be printed, except:
// 1. new lines
// 2. spaces which have been replaced with new lines by break.
typedef struct {
int start;
int end;
int width;
} LineRange;
int text_wordwrap(
LineRange* lines,
int max_lines,
const wchar_t* text,
int width,
int (*advance)(const wchar_t* str, int i, void* ctx),
void* ctx)
{
int n = 0;
int i = 0;
int line_start = 0;
int line_width = 0;
// INVARIANTS
// Every line ends with a word (possibly empty) and whitespace (possibly empty).
// Every line starts with a word, or just, after a line break.
// Therefore: only leading space after newlines
while (1)
{
int space_width = 0;
int space_width_without_last = 0;
while (iswspace(text[i]))
{
if (text[i] == '\n' || text[i] == '\r')
{
if (lines && n < max_lines)
{
lines[n].start = line_start;
lines[n].end = i;
lines[n].width = line_width + space_width;
}
++n;
++i;
if (text[i] + text[i + 1] == '\n' + '\r') {
++i;
}
line_start = i;
line_width = 0;
space_width = 0;
space_width_without_last = 0;
}
else
{
space_width_without_last = space_width;
space_width += advance(text, i, ctx);
++i;
}
}
if (!text[i]) {
line_width += space_width;
break;
}
int word_start = i;
int word_width = 0;
while (text[i] && !iswspace(text[i]))
{
word_width += advance(text, i, ctx);
++i;
}
if (line_width + space_width + word_width > width) {
// break at end of last word
if (lines && n < max_lines)
{
lines[n].start = line_start;
lines[n].end = word_start;
lines[n].width = line_width + space_width_without_last;
}
++n;
line_start = word_start;
line_width = word_width;
if (word_width > width)
{
// word is longer than a line
// just make a line for it
if (lines && n < max_lines)
{
lines[n].start = line_start;
lines[n].end = i;
lines[n].width = word_width;
}
++n;
line_start = i;
line_width = 0;
}
} else {
line_width += space_width + word_width;
}
assert(line_width <= width);
}
if (lines && n < max_lines)
{
lines[n].start = line_start;
lines[n].end = i;
lines[n].width = line_width;
}
++n;
return n;
#undef I
}
static
int test_advance_(const wchar_t* text, int i, void* ctx) {
return 1;
}
void test_text_wordwrap(void)
{
const wchar_t* text = L"the quick\nbrown fox, jumped over the lazy dog.";
int width = 30;
LineRange lines[40];
int n = text_wordwrap(lines, 40, text, width, test_advance_, NULL);
printf("%d\n", n);
for (int i = 0; i < n; ++i)
{
printf("%d, %d\n", lines[i].start, lines[i].end);
}
wchar_t buffer[1024];
for (int i = 0; i < n; ++i)
{
LineRange r = lines[i];
size_t l = r.end - r.start;
memcpy(buffer, text + r.start, l * sizeof(wchar_t));
buffer[l] = '\0';
printf("%ls\n", buffer);
}
}
static
void render_(
const stbtt_fontinfo* font_info,
int line_height,
CcTextAlign alignment,
uint32_t color,
const wchar_t* text,
const LineRange* lines,
int line_count,
CcBitmap* bitmap
)
{
float scale = stbtt_ScaleForPixelHeight(font_info, line_height);
int ascent, descent, lineGap;
stbtt_GetFontVMetrics(font_info, &ascent, &descent, &lineGap);
ascent = (int)roundf(ascent * scale);
descent = (int)roundf(descent * scale);
CcBitmap glyph_map = {
.w = line_height * 3,
.h = line_height
};
cc_bitmap_alloc(&glyph_map);
cc_bitmap_clear(&glyph_map, color);
CcGrayBitmap glyph_mask = {
.w = glyph_map.w,
.h = glyph_map.h,
};
cc_gray_bitmap_alloc(&glyph_mask);
int i = 0;
int y = 0;
while (i < line_count && y + line_height < bitmap->h)
{
LineRange r = lines[i];
int j = lines[i].start;
int end = lines[i].end;
int x = 0;
if (alignment == TEXT_ALIGN_CENTER)
{
x = (bitmap->w - r.width) / 2;
}
else if (alignment == TEXT_ALIGN_RIGHT)
{
x = bitmap->w - r.width;
}
int glyph_index = stbtt_FindGlyphIndex(font_info, text[j]);
while (j != end)
{
int ax;
int lsb;
stbtt_GetGlyphHMetrics(font_info, glyph_index, &ax, &lsb);
if (!iswspace(text[j]))
{
/* get bounding box for character (may be offset to account for chars that dip above or below the line */
int c_x1, c_y1, c_x2, c_y2;
stbtt_GetGlyphBitmapBox(font_info, glyph_index, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);
stbtt_MakeGlyphBitmap(
font_info,
(unsigned char *)glyph_mask.data,
c_x2 - c_x1,
c_y2 - c_y1,
glyph_mask.stride,
scale,
scale,
glyph_index
);
// to properly handle character overlap
// we alpha composite
assert(c_y2 - c_y1 <= glyph_map.w);
assert(c_x2 - c_x1 <= glyph_map.h);
cc_bitmap_copy_channel(&glyph_map, 0, &glyph_mask);
cc_bitmap_blit(
&glyph_map,
bitmap,
0,
0,
x + roundf(lsb * scale),
y + ascent + c_y1,
c_x2 - c_x1,
c_y2 - c_y1,
COLOR_BLEND_FULL
);
}
int next_glyph_index = stbtt_FindGlyphIndex(font_info, text[j + 1]);
int kern = stbtt_GetGlyphKernAdvance(font_info, glyph_index, next_glyph_index);
x += roundf((ax + kern) * scale);
j += 1;
glyph_index = next_glyph_index;
}
y += line_height;
i += 1;
}
cc_gray_bitmap_free(&glyph_mask);
cc_bitmap_free(&glyph_map);
}
typedef struct
{
const stbtt_fontinfo* font_info;
float scale;
} WordWrapData;
static
int kern_advance_(const wchar_t* text, int i, void* ctx)
{
const WordWrapData* a = ctx;
const stbtt_fontinfo* font_info = a->font_info;
float scale = a->scale;
int ax;
int lsb;
stbtt_GetCodepointHMetrics(font_info, text[i], &ax, &lsb);
int kern = stbtt_GetCodepointKernAdvance(font_info, text[i], text[i + 1]);
return (int)roundf((ax + kern) * scale);
}
void cc_text_render(
CcBitmap* bitmap,
const wchar_t* text,
const stbtt_fontinfo* font_info,
int font_size,
CcTextAlign align,
uint32_t font_color
)
{
if (!text || text[0] == '\0') {
return;
}
int line_height = (font_size * 2);
WordWrapData d;
d.font_info = font_info;
d.scale = stbtt_ScaleForPixelHeight(font_info, line_height);
LineRange lines[2048];
int n = text_wordwrap(lines, 2048, text, bitmap->w, kern_advance_, &d);
render_(font_info, line_height, align, font_color, text, lines, n, bitmap);
}