GCC Code Coverage Report


Directory: ../src/
File: extensions/ttf_extensions.c
Date: 2026-09-06 17:54:43
Coverage Exec Excl Total
Lines: 87.1% 479 0 550
Functions: 100.0% 28 0 28
Branches: 63.3% 181 0 286

Line Branch Exec Source
1 /*
2 Copyright 2025 Joel Svensson svenssonjoel@yahoo.se
3
4 LispBM is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 LispBM is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <extensions/ttf_extensions.h>
19 #include <extensions.h>
20 #include <buffer.h>
21 #include "ttf_backend.h"
22
23 #if defined(LBM_USE_TTF_FREETYPE) || defined(LBM_TTF_USE_FREETYPE)
24 #include "ttf_backend_freetype.h"
25 #else
26 #include "schrift.h"
27 #endif
28
29 #ifdef LBM_OPT_TTF_EXTENSIONS_SIZE
30 #pragma GCC optimize ("-Os")
31 #endif
32 #ifdef LBM_OPT_TTF_EXTENSIONS_SIZE_AGGRESSIVE
33 #pragma GCC optimize ("-Oz")
34 #endif
35
36 // The font object does not own the font data pointer
37 // GC will free it once it knows it can do so.
38 // SFT_Font objects life-span is atomic in relation to GC.
39 //
40 // mk_font_raw is internal and only called with valid arrays.
41 // No sensible "array is correct" check can be done here.
42 15 static bool mk_font_raw(SFT_Font *ft, lbm_value font_val) {
43 15 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(font_val);
44
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (arr) {
45 15 ft->memory = (uint8_t*)arr->data;
46 15 ft->size = (uint_fast32_t)arr->size;
47 15 return (init_font(ft) >= 0) ? true : false;
48 }
49 ✗ return false; // Really unreachable but makes Static analysis happy.
50 }
51
52 14 static SFT mk_sft(SFT_Font *ft, float x_scale, float y_scale) {
53 SFT sft;
54 14 sft.font = ft;
55 14 sft.xScale = x_scale;
56 14 sft.yScale = y_scale;
57 14 sft.xOffset = 0;
58 14 sft.yOffset = 0;
59 14 sft.flags = SFT_DOWNWARD_Y;
60
61 14 return sft;
62 }
63
64 // If we are not bin searching then sorting the UTF32 codes is not needed.
65
66 #define FONT_MAX_ID_STRING_LENGTH 10
67 #define FONT_VERSION 0
68 #define FONT_MAGIC_STRING "font"
69 #define FONT_LINE_METRICS_STRING "lmtx"
70 #define FONT_KERNING_STRING "kern"
71 #define FONT_GLYPHS_STRING "glyphs"
72
73 // sizeof when used on string literals include the the terminating 0
74 #define FONT_PREAMBLE_SIZE (sizeof(uint16_t) * 2 + sizeof(FONT_MAGIC_STRING))
75 #define FONT_LINE_METRICS_SIZE (sizeof(uint32_t) + (sizeof(float) * 3) + sizeof(FONT_LINE_METRICS_STRING))
76
77 // "header sizes" excluding data payload
78 #define FONT_KERN_PAIR_SIZE (uint32_t)(4 + 4 + 4)
79 #define FONT_KERN_ROW_SIZE (uint32_t)(4 + 4)
80 #define FONT_KERN_TABLE_SIZE (uint32_t)(sizeof(FONT_KERNING_STRING) + 4 + 4)
81 #define FONT_GLYPH_TABLE_SIZE (uint32_t)(sizeof(FONT_GLYPHS_STRING) + 4 + 4 + 4)
82 #define FONT_GLYPH_SIZE (uint32_t)(6*4)
83
84 295 static int num_kern_pairs_row(SFT *sft, uint32_t utf32, uint32_t *codes, uint32_t num_codes) {
85
86 295 int num = 0;
87
88 SFT_Glyph lgid;
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 295 times.
295 if (sft_lookup(sft, utf32, &lgid) < 0) {
90 ✗ return -1;
91 }
92
93
2/2
✓ Branch 0 taken 4405 times.
✓ Branch 1 taken 295 times.
4700 for (uint32_t i = 0; i < num_codes; i ++) {
94 4405 uint32_t right_utf32 = codes[i];
95 SFT_Kerning kern;
96 4405 kern.xShift = 0.0;
97 4405 kern.yShift = 0.0;
98
99 SFT_Glyph rgid;
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4405 times.
4405 if (sft_lookup(sft, right_utf32, &rgid) < 0) {
101 ✗ return -1;
102 }
103
104 #if !defined(LBM_USE_TTF_FREETYPE) && !defined(LBM_TTF_USE_FREETYPE)
105
1/2
✓ Branch 0 taken 4405 times.
✗ Branch 1 not taken.
4405 if (sft->font->pairAdjustOffset) {
106 4405 sft_gpos_kerning(sft, lgid, rgid, &kern);
107 }
108
2/4
✓ Branch 0 taken 4405 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4405 times.
✗ Branch 3 not taken.
4405 if (kern.xShift == 0.0 && kern.yShift == 0.0) {
109 4405 sft_kerning(sft, lgid, rgid, &kern);
110 }
111 #else
112 sft_kerning(sft, lgid, rgid, &kern);
113 #endif
114
3/4
✓ Branch 0 taken 3991 times.
✓ Branch 1 taken 414 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3991 times.
4405 if (kern.xShift != 0.0 || kern.yShift != 0.0) {
115 414 num++;
116 }
117 }
118 295 return num;
119 }
120
121 26 static bool kern_table_dims(SFT *sft, uint32_t *codes, uint32_t num_codes, int *rows, int *tot_pairs) {
122
123 26 int num_rows = 0;
124 26 int tot_kern_pairs = 0;
125
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 26 times.
224 for (uint32_t i = 0; i < num_codes; i ++) {
126 198 int r = num_kern_pairs_row(sft, codes[i], codes, num_codes);
127
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 128 times.
198 if (r > 0) {
128 70 num_rows ++;
129 70 tot_kern_pairs += r;
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 } else if (r < 0) {
131 ✗ return false;
132 }
133 }
134 26 *rows = num_rows;
135 26 *tot_pairs = tot_kern_pairs;
136 26 return true;
137 }
138
139 14 static int kern_table_size_bytes(SFT *sft, uint32_t *codes, uint32_t num_codes) {
140 14 int rows = 0;
141 14 int tot_pairs = 0;
142
143 int size_bytes;
144
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (kern_table_dims(sft, codes, num_codes, &rows, &tot_pairs)) {
145 14 size_bytes =
146 14 (int)(FONT_KERN_PAIR_SIZE * (uint32_t)tot_pairs +
147 14 FONT_KERN_ROW_SIZE * (uint32_t)rows +
148 FONT_KERN_TABLE_SIZE);
149 } else {
150 ✗ return -1;
151 }
152 14 return size_bytes;
153 }
154
155
156 48 static void buffer_append_string(uint8_t *buffer, char *str, int32_t *index) {
157 48 size_t n = strlen(str);
158 48 memcpy(&buffer[*index], str, n + 1); // include the 0
159 48 *index = *index + (int32_t)n + 1;
160 48 }
161
162 12 static void buffer_append_font_preamble(uint8_t *buffer, int32_t *index) {
163 12 buffer_append_uint16(buffer, 0, index); // 2 leading zero bytes
164 12 buffer_append_uint16(buffer, 0, index); // version 0
165 12 buffer_append_string(buffer, FONT_MAGIC_STRING, index);
166 12 }
167
168 12 static void buffer_append_line_metrics(uint8_t *buffer, float ascender, float descender, float line_gap, int32_t *index) {
169 12 buffer_append_string(buffer, FONT_LINE_METRICS_STRING, index);
170 12 buffer_append_uint32(buffer, sizeof(float) * 3, index);
171 12 buffer_append_float32_auto(buffer, ascender, index);
172 12 buffer_append_float32_auto(buffer, descender, index);
173 12 buffer_append_float32_auto(buffer, line_gap, index);
174 12 }
175
176
177 12 static bool buffer_append_kerning_table(uint8_t *buffer, SFT *sft, uint32_t *codes, uint32_t num_codes, int32_t *index) {
178
179 12 int num_rows = 0;
180 12 int tot_pairs = 0;
181
182
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 if (kern_table_dims(sft, codes, num_codes, &num_rows, &tot_pairs)) {
183
184 // TODO: compute size of "payload" only
185 12 uint32_t size_bytes =
186 12 FONT_KERN_PAIR_SIZE * (uint32_t)tot_pairs +
187 12 FONT_KERN_ROW_SIZE * (uint32_t)num_rows +
188 + 4; // number of rows field
189
190 12 buffer_append_string(buffer, FONT_KERNING_STRING, index);
191 12 buffer_append_uint32(buffer, size_bytes, index); // distance to jump ahead from index if not interested in kerning.
192 12 buffer_append_uint32(buffer, (uint32_t)num_rows, index);
193
194
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 12 times.
109 for (uint32_t left_ix = 0; left_ix < num_codes; left_ix ++) { // loop over all codes
195 97 int32_t row_len = num_kern_pairs_row(sft, codes[left_ix], codes, num_codes);
196
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 62 times.
97 if ( row_len > 0) {
197 SFT_Glyph lgid;
198
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
35 if (sft_lookup(sft, codes[left_ix], &lgid) < 0) {
199 ✗ return false;
200 }
201
202 // format kerning table row
203 // - UTF32 : leftGlyph
204 // - uint32 : numKernPairs
205 // - KernPair[]
206
207 35 buffer_append_uint32(buffer, codes[left_ix],index);
208 35 buffer_append_uint32(buffer, (uint32_t)row_len, index);
209
210
2/2
✓ Branch 0 taken 639 times.
✓ Branch 1 taken 35 times.
674 for (uint32_t right_ix = 0; right_ix < num_codes; right_ix ++) { // and all codes
211 639 uint32_t right_utf32 = codes[right_ix];
212 SFT_Kerning kern;
213 639 kern.xShift = 0.0;
214 639 kern.yShift = 0.0;
215
216 // format KernPair
217 // - UTF32 : rightGlyph
218 // - float : xShift
219 // - float : yShift
220
221 SFT_Glyph rgid;
222
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 639 times.
639 if (sft_lookup(sft, right_utf32, &rgid) < 0) {
223 ✗ return false;
224 }
225
226 #if !defined(LBM_USE_TTF_FREETYPE) && !defined(LBM_TTF_USE_FREETYPE)
227 // Schrift has separate GPOS and kern table handling
228
1/2
✓ Branch 0 taken 639 times.
✗ Branch 1 not taken.
639 if (sft->font->pairAdjustOffset) {
229 639 sft_gpos_kerning(sft, lgid, rgid, &kern);
230 }
231
2/4
✓ Branch 0 taken 639 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 639 times.
✗ Branch 3 not taken.
639 if (kern.xShift == 0.0 && kern.yShift == 0.0) {
232 #endif
233 639 sft_kerning(sft, lgid, rgid, &kern);
234 #if !defined(LBM_USE_TTF_FREETYPE) && !defined(LBM_TTF_USE_FREETYPE)
235 }
236 #endif
237
3/4
✓ Branch 0 taken 501 times.
✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 501 times.
639 if (kern.xShift != 0.0 || kern.yShift != 0.0) {
238 138 buffer_append_uint32(buffer, right_utf32, index);
239 138 buffer_append_float32_auto(buffer, kern.xShift, index);
240 138 buffer_append_float32_auto(buffer, kern.yShift, index);
241 }
242 }
243 }
244 }
245 }
246 12 return true;
247 }
248
249 26 int glyphs_img_data_size(SFT *sft, color_format_t fmt, uint32_t *codes, uint32_t num_codes) {
250 26 int total_size = 0;
251
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 26 times.
224 for (uint32_t i = 0; i < num_codes; i ++) {
252 SFT_Glyph gid;
253
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
198 if (sft_lookup(sft, codes[i], &gid) < 0) return -1;
254 SFT_GMetrics gmtx;
255
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
198 if (sft_gmetrics(sft, gid, &gmtx) < 0) return -1;
256 198 total_size += (int)image_dims_to_size_bytes(fmt, (uint16_t)gmtx.minWidth, (uint16_t)gmtx.minHeight);
257 }
258 26 return total_size;
259 }
260
261 97 static int buffer_append_glyph(uint8_t *buffer, SFT *sft, color_format_t fmt, uint32_t utf32, int32_t *index){
262 SFT_Glyph gid;
263
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
97 if (sft_lookup(sft, utf32, &gid) < 0) return -1;
264 SFT_GMetrics gmtx;
265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
97 if (sft_gmetrics(sft, gid, &gmtx) < 0) return -1;
266
267 97 buffer_append_uint32(buffer, utf32, index);
268 97 buffer_append_float32_auto(buffer, gmtx.advanceWidth, index);
269 97 buffer_append_float32_auto(buffer, gmtx.leftSideBearing, index);
270 97 buffer_append_int32(buffer,gmtx.yOffset,index);
271 97 buffer_append_int32(buffer,gmtx.minWidth, index);
272 97 buffer_append_int32(buffer,gmtx.minHeight, index);
273
274 image_buffer_t img;
275 97 img.width = (uint16_t)gmtx.minWidth;
276 97 img.height = (uint16_t)gmtx.minHeight;
277 97 img.fmt = fmt;
278 97 img.mem_base = &buffer[*index];
279 97 img.data = &buffer[*index];
280
281 97 int r = sft_render(sft, gid, &img);
282 97 *index += (int32_t)image_dims_to_size_bytes(fmt, (uint16_t)gmtx.minWidth, (uint16_t)gmtx.minHeight);
283 97 return r;
284 }
285
286 12 static int buffer_append_glyph_table(uint8_t *buffer, SFT *sft, color_format_t fmt, uint32_t *codes, uint32_t num_codes, int32_t *index) {
287
288 12 uint32_t size_bytes =
289 4 + // number of glyphs
290 4 + // image format
291 24 num_codes * 24 + // glyph metrics
292 12 (uint32_t)glyphs_img_data_size(sft,fmt,codes,num_codes);
293
294 12 buffer_append_string(buffer, FONT_GLYPHS_STRING, index);
295 12 buffer_append_uint32(buffer, size_bytes, index); // distance to jump ahead from index if not interested in kerning.
296 12 buffer_append_uint32(buffer, num_codes, index);
297 12 buffer_append_uint32(buffer, (uint32_t)fmt, index);
298
299 12 int r = 0;
300
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 12 times.
109 for (uint32_t i = 0; i < num_codes; i ++) {
301 97 r = buffer_append_glyph(buffer,sft,fmt,codes[i], index);
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97 times.
97 if (r < 0) return r;
303 }
304 12 return r;
305 }
306
307 //returns the increment for n
308 106 static int insert_nub(uint32_t *arr, uint32_t n, uint32_t new_elt) {
309 uint32_t i;
310
2/2
✓ Branch 0 taken 487 times.
✓ Branch 1 taken 50 times.
537 for (i = 0; i < n; i ++) {
311
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 482 times.
487 if (arr[i] == new_elt) return 0;
312
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 431 times.
482 if (arr[i] > new_elt) {
313 51 memmove(&arr[i+1], &arr[i], (n - i) * 4);
314 51 arr[i] = new_elt;
315 51 return 1;
316 }
317 }
318 50 arr[i] = new_elt;
319 50 return 1;
320 }
321
322 // (ttf-prepare-bin font font-scale img-fmt chars-string)
323 16 lbm_value ext_ttf_prepare_bin(lbm_value *args, lbm_uint argn) {
324
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
31 if (argn == 4 &&
325
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
30 lbm_is_array_r(args[0]) && // font file data
326
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
30 lbm_is_number(args[1]) &&
327
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
30 lbm_is_symbol(args[2]) &&
328 15 lbm_is_array_r(args[3])) {
329
330 15 float x_scale = lbm_dec_as_float(args[1]);
331 15 float y_scale = x_scale;
332
333 15 color_format_t fmt = sym_to_color_format(args[2]);
334
335 15 lbm_value result_array_cell = lbm_heap_allocate_cell(LBM_TYPE_CONS, ENC_SYM_NIL, ENC_SYM_ARRAY_TYPE);
336
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (result_array_cell == ENC_SYM_MERROR) return result_array_cell;
338 15 lbm_array_header_t *result_array_header = (lbm_array_header_t *)lbm_malloc(sizeof(lbm_array_header_t));
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!result_array_header) return ENC_SYM_MERROR;
340
341 15 lbm_array_header_t *utf8_array_header = (lbm_array_header_t*)(lbm_car(args[3]));
342
343 // Try to keep the utf8 array as nubbed as possible or there will be waste of mem.
344 // Unfortunate dynamic tmp storage...
345 15 uint32_t* unique_utf32 = lbm_malloc(utf8_array_header->size * sizeof(uint32_t));
346
347
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (unique_utf32) {
348
349 SFT_Font ft;
350
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 14 times.
15 if (!mk_font_raw(&ft,args[0])) {
351 1 free_font(&ft);
352 1 lbm_free(unique_utf32);
353 1 return ENC_SYM_EERROR;
354 }
355 14 SFT sft = mk_sft(&ft, x_scale, y_scale);
356
357 // We know which glyphs to prerender...
358 // So time to start collecting information to put into the binary prerender format
359 // and to figure out how much prerender space to allocate!
360
361 14 uint32_t i = 0;
362 14 uint32_t next_i = 0;
363 uint32_t utf32;
364 14 uint32_t n = 0;
365
366
2/2
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 14 times.
120 while (get_utf32((uint8_t*)utf8_array_header->data, &utf32, i, &next_i)) {
367 106 n += (uint32_t)insert_nub(unique_utf32, n, utf32);
368 106 i = next_i;
369 }
370
371 // There could be zero kerning pairs and then we dont
372 // need the kerning table at all.
373 // TODO: Fix this.
374 14 int kern_tab_bytes = kern_table_size_bytes(&sft, unique_utf32, n);
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (kern_tab_bytes <= 0) {
376 ✗ free_font(&ft);
377 ✗ lbm_free(unique_utf32);
378 ✗ return ENC_SYM_EERROR;
379 }
380
381 14 int glyph_gfx_size = glyphs_img_data_size(&sft, fmt, unique_utf32, n);
382
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 if (glyph_gfx_size <= 0) {
383 2 free_font(&ft);
384 2 lbm_free(unique_utf32);
385 2 return ENC_SYM_EERROR;
386 }
387
388 12 uint32_t bytes_required =
389 12 (uint32_t)(FONT_PREAMBLE_SIZE +
390 FONT_LINE_METRICS_SIZE +
391 12 (uint32_t)kern_tab_bytes +
392 12 FONT_GLYPH_TABLE_SIZE +
393 12 n * FONT_GLYPH_SIZE + // per glyph metrics
394 (uint32_t)glyph_gfx_size);
395
396 12 uint8_t *buffer = (uint8_t*)lbm_malloc(bytes_required);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!buffer) {
398 ✗ free_font(&ft);
399 ✗ lbm_free(unique_utf32);
400 ✗ return ENC_SYM_MERROR;
401 }
402 12 memset(buffer,0, bytes_required);
403
404 SFT_LMetrics lmtx;
405
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (sft_lmetrics(&sft, &lmtx) < 0) {
406 ✗ free_font(&ft);
407 ✗ lbm_free(unique_utf32);
408 ✗ return ENC_SYM_EERROR;
409 }
410 12 int32_t index = 0;
411
412 12 buffer_append_font_preamble(buffer, &index);
413 12 buffer_append_line_metrics(buffer,
414 lmtx.ascender,
415 lmtx.descender,
416 lmtx.lineGap,
417 &index);
418 12 buffer_append_kerning_table(buffer, &sft, unique_utf32, n, &index);
419
420 12 int r = buffer_append_glyph_table(buffer, &sft, fmt, unique_utf32, n, &index);
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if ( r == SFT_MEM_ERROR) {
422 ✗ free_font(&ft);
423 ✗ lbm_free(unique_utf32);
424 ✗ lbm_free(buffer);
425 ✗ lbm_set_car_and_cdr(result_array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
426 ✗ return ENC_SYM_MERROR;
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 } else if (r < 0) {
428 ✗ free_font(&ft);
429 ✗ lbm_free(unique_utf32);
430 ✗ lbm_free(buffer);
431 ✗ lbm_set_car_and_cdr(result_array_cell, ENC_SYM_NIL, ENC_SYM_NIL);
432 ✗ return ENC_SYM_EERROR;
433 }
434 12 free_font(&ft);
435 12 lbm_free(unique_utf32); // tmp data nolonger needed
436 12 result_array_header->size = (lbm_uint)index;
437 12 result_array_header->data = (lbm_uint*)buffer;
438 12 lbm_set_car(result_array_cell, (lbm_uint)result_array_header);
439 12 result_array_cell = lbm_set_ptr_type(result_array_cell, LBM_TYPE_ARRAY);
440 12 return result_array_cell;
441 } else {
442 ✗ return ENC_SYM_MERROR;
443 }
444 }
445 1 return ENC_SYM_TERROR;
446 }
447
448 39 bool buffer_get_font_preamble(uint8_t* buffer, uint16_t *version, int32_t *index) {
449
450 39 uint16_t zero = buffer_get_uint16(buffer, index);
451
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (zero == 0) {
452 39 *version = buffer_get_uint16(buffer, index);
453
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (strncmp((const char *)&buffer[*index], "font", 4) == 0) {
454 39 *index += (int32_t)sizeof("font"); // includes 0 for constant string
455 39 return true;
456 }
457 }
458 ✗ return false;
459 }
460
461 38 static bool font_get_line_metrics(uint8_t *buffer, int32_t buffer_size, float *ascender, float *descender, float *line_gap ,int32_t index) {
462
463
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 while(index < buffer_size) {
464 38 char *str = (char*)&buffer[index];
465
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (strncmp(str, "lmtx", 4) == 0) {
466 38 int32_t i = index + 5 + 4; // skip over string and size field;
467 38 *ascender = buffer_get_float32_auto(buffer, &i);
468 38 *descender = buffer_get_float32_auto(buffer, &i);
469 38 *line_gap = buffer_get_float32_auto(buffer, &i);
470 38 return true;
471 }
472 ✗ index += (int32_t)(strlen(str) + 1);
473 ✗ index += (int32_t)buffer_get_uint32(buffer,&index); // just to next position
474 }
475 ✗ return false;
476 }
477
478 34 static bool font_get_kerning_table_index(uint8_t *buffer, int32_t buffer_size, int32_t *res_index, int32_t index) {
479
480
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 while (index < buffer_size) {
481 68 char *str = (char*)&buffer[index];
482
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 34 times.
68 if (strncmp(str, "kern", 4) == 0) {
483 34 *res_index = index + 5 + 4;
484 34 return true;
485 }
486 34 index += (int32_t)(strlen(str) + 1);
487 34 index += (int32_t)buffer_get_uint32(buffer,&index); // jump to next position
488 }
489 ✗ return false;
490 }
491
492 35 static bool font_get_glyphs_table_index(uint8_t *buffer, int32_t buffer_size, int32_t *res_index, uint32_t *num_codes, uint32_t *fmt, int32_t index) {
493
1/2
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
105 while (index < buffer_size) {
494 105 char *str = (char*)&buffer[index];
495
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 70 times.
105 if (strncmp(str, "glyphs", 6) == 0) {
496 35 int32_t i = index + 7 + 4;
497 35 *num_codes = buffer_get_uint32(buffer,&i);
498 35 *fmt = buffer_get_uint32(buffer,&i);
499 35 *res_index = i;
500 35 return true;
501 }
502 70 index += (int32_t)(strlen(str) + 1);
503 70 index += (int32_t)buffer_get_uint32(buffer,&index);
504 }
505 ✗ return false;
506 }
507
508 268 static bool font_get_glyph(uint8_t *buffer,
509 float *advance_width,
510 float *left_side_bearing,
511 int32_t *y_offset,
512 int32_t *width,
513 int32_t *height,
514 uint8_t **gfx,
515 uint32_t utf32,
516 uint32_t num_codes,
517 color_format_t fmt,
518 int32_t index) {
519
520 268 uint32_t i = 0;
521
1/2
✓ Branch 0 taken 2640 times.
✗ Branch 1 not taken.
2640 while (i < num_codes) {
522 2640 uint32_t c = buffer_get_uint32(buffer, &index);
523
2/2
✓ Branch 0 taken 268 times.
✓ Branch 1 taken 2372 times.
2640 if (c == utf32) {
524 268 *advance_width = buffer_get_float32_auto(buffer, &index);
525 268 *left_side_bearing = buffer_get_float32_auto(buffer, &index);
526 268 *y_offset = buffer_get_int32(buffer, &index);
527 268 *width = buffer_get_int32(buffer, &index);
528 268 *height = buffer_get_int32(buffer,&index);
529 268 *gfx = &buffer[index];
530 268 return true;
531 } else {
532 2372 index += 12;
533 2372 int32_t w = buffer_get_int32(buffer, &index);
534 2372 int32_t h = buffer_get_int32(buffer, &index);
535 2372 index += (int32_t)image_dims_to_size_bytes(fmt, (uint16_t)w, (uint16_t)h);
536 }
537 2372 i++;
538 }
539 ✗ return false;
540 }
541
542 236 bool font_get_kerning(uint8_t *buffer, uint32_t left, uint32_t right, float *x_shift, float *y_shift, int32_t index) {
543
544 236 uint32_t num_rows = buffer_get_uint32(buffer, &index);
545
546
2/2
✓ Branch 0 taken 2169 times.
✓ Branch 1 taken 195 times.
2364 for (uint32_t row = 0; row < num_rows; row ++) {
547
548 2169 uint32_t row_code = buffer_get_uint32(buffer, &index);
549 2169 uint32_t row_len = buffer_get_uint32(buffer, &index);
550
551
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 2001 times.
2169 if (row_code == left) {
552
2/2
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 127 times.
382 for (uint32_t col = 0; col < row_len; col ++) {
553 255 uint32_t col_code = buffer_get_uint32(buffer, &index);
554
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 214 times.
255 if (col_code == right) {
555 41 *x_shift = buffer_get_float32_auto(buffer, &index);
556 41 *y_shift = buffer_get_float32_auto(buffer, &index);
557 41 return true;
558 } else {
559 214 index += 8;
560 }
561 }
562 } else {
563 2001 index += (int32_t)(row_len * FONT_KERN_PAIR_SIZE);
564 }
565 }
566 195 return false;
567 }
568
569 36 lbm_value ttf_text_bin(lbm_value *args, lbm_uint argn) {
570 36 lbm_value res = ENC_SYM_TERROR;
571 lbm_array_header_t *img_arr;
572 lbm_value font;
573 char *utf8_str;
574 uint32_t colors[16];
575 36 uint32_t next_arg = 0;
576
3/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 2 times.
72 if (argn >= 6 &&
577
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
70 (img_arr = get_image_buffer(args[0])) &&
578
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
68 lbm_is_number(args[1]) && // x position
579
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
68 lbm_is_number(args[2]) && // y position
580
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
68 lbm_is_cons(args[3]) && // list of colors
581
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 1 time.
68 lbm_is_array_r(args[4]) && // Binary font
582 34 lbm_is_array_r(args[5])) { // sequence of utf8 characters
583 33 lbm_value curr = args[3];
584 33 int i = 0;
585
3/4
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 132 times.
✗ Branch 4 not taken.
165 while(lbm_is_cons(curr) && i < 16) {
586 132 colors[i] = lbm_dec_as_u32(lbm_car(curr));
587 132 curr = lbm_cdr(curr);
588 132 i ++;
589 }
590 33 font = args[4];
591 33 utf8_str = lbm_dec_str(args[5]);
592 33 next_arg = 6;
593 } else {
594 3 return res;
595 }
596
597 33 int x_pos = lbm_dec_as_i32(args[1]);
598 33 int y_pos = lbm_dec_as_i32(args[2]);
599
600 33 float line_spacing = 1.0f;
601 33 bool up = false;
602 33 bool down = false;
603
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 33 times.
58 for (uint32_t i = next_arg; i < argn; i ++) {
604
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 1 time.
25 if (lbm_is_symbol(args[i])) {
605 24 up = display_is_symbol_up(args[i]);
606 24 down = display_is_symbol_down(args[i]);
607
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 } else if (lbm_is_number(args[i])) {
608 1 line_spacing = lbm_dec_as_float(args[i]);
609 }
610 }
611
612 33 lbm_array_header_t *font_arr = lbm_dec_array_r(font);
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (font_arr->size < 10) return ENC_SYM_EERROR;
614
615 33 int32_t index = 0;
616 uint16_t version;
617
618
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (!buffer_get_font_preamble((uint8_t*)font_arr->data, &version, &index)) {
619 ✗ return ENC_SYM_EERROR;
620 }
621
622 float ascender;
623 float descender;
624 float line_gap;
625
626
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if(!font_get_line_metrics((uint8_t*)font_arr->data, (int32_t)font_arr->size, &ascender, &descender, &line_gap , index)) {
627 ✗ return ENC_SYM_EERROR;
628 }
629
630 33 int32_t kern_index = 0;
631
632
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (!font_get_kerning_table_index((uint8_t*)font_arr->data, (int32_t)font_arr->size, &kern_index, index)) {
633 ✗ return ENC_SYM_EERROR;
634 }
635
636 33 int32_t glyphs_index = 0;
637 uint32_t num_codes;
638 uint32_t color_fmt;
639
640
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (!font_get_glyphs_table_index((uint8_t*)font_arr->data, (int32_t)font_arr->size, &glyphs_index, &num_codes, &color_fmt, index)) {
641 ✗ return ENC_SYM_EERROR;
642 }
643
644 33 color_format_t fmt = (color_format_t)color_fmt;
645 33 float x = 0.0;
646 33 float y = 0.0;
647
648 image_buffer_t tgt;
649 33 tgt.width = image_buffer_width((uint8_t*)img_arr->data);
650 33 tgt.height = image_buffer_height((uint8_t*)img_arr->data);
651 33 tgt.fmt = image_buffer_format((uint8_t*)img_arr->data);
652 33 tgt.mem_base = (uint8_t*)img_arr->data;
653 33 tgt.data = image_buffer_data((uint8_t*)img_arr->data);
654
655 uint32_t utf32;
656 uint32_t prev;
657 33 bool has_prev = false;
658 33 uint32_t i = 0;
659 33 uint32_t next_i = 0;
660
2/2
✓ Branch 1 taken 263 times.
✓ Branch 2 taken 33 times.
296 while (get_utf32((uint8_t*)utf8_str, &utf32, i, &next_i)) {
661
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 262 times.
263 if (utf32 == '\n') {
662 1 x = 0.0;
663 1 y += line_spacing * (ascender - descender + line_gap);
664 1 i++;
665 1 continue; // next iteration
666 }
667
668 262 float x_n = x;
669 262 float y_n = y;
670
671 float advance_width;
672 float left_side_bearing;
673 int32_t y_offset;
674 int32_t width;
675 int32_t height;
676 uint8_t *gfx;
677
678
1/2
✓ Branch 1 taken 262 times.
✗ Branch 2 not taken.
262 if (font_get_glyph((uint8_t*)font_arr->data,
679 &advance_width,
680 &left_side_bearing,
681 &y_offset,
682 &width,
683 &height,
684 &gfx,
685 utf32,
686 num_codes,
687 fmt,
688 glyphs_index)) {
689
690 262 float x_shift = 0;
691 262 float y_shift = 0;
692
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 30 times.
262 if (has_prev) {
693 232 font_get_kerning((uint8_t*)font_arr->data,
694 prev,
695 utf32,
696 &x_shift,
697 &y_shift,
698 kern_index);
699 }
700 262 x_n += x_shift;
701 262 y_n += y_shift;
702 262 y_n += (float)y_offset;
703
704 image_buffer_t src;
705 262 src.width = (uint16_t)width;
706 262 src.height = (uint16_t)height;
707 262 src.fmt = fmt;
708 //src.mem_base = gfx;
709 262 src.data = gfx;
710
711 262 uint32_t num_colors = 1 << src.fmt;
712
2/2
✓ Branch 0 taken 5406 times.
✓ Branch 1 taken 262 times.
5668 for (int j = 0; j < src.height; j++) {
713
2/2
✓ Branch 0 taken 90134 times.
✓ Branch 1 taken 5406 times.
95540 for (int k = 0; k < src.width; k ++) {
714 // the bearing should not be accumulated into the advances
715
716 90134 uint32_t p = getpixel(&src, k, j);
717
2/2
✓ Branch 0 taken 37931 times.
✓ Branch 1 taken 52203 times.
90134 if (p) { // only draw colored
718 37931 uint32_t c = colors[p & (num_colors-1)]; // ceiled
719
2/2
✓ Branch 0 taken 12256 times.
✓ Branch 1 taken 25675 times.
37931 if (up) {
720 12256 putpixel(&tgt, x_pos + (j + (int)y_n), y_pos - (k + (int)(x_n + left_side_bearing)), c);
721
2/2
✓ Branch 0 taken 15604 times.
✓ Branch 1 taken 10071 times.
25675 } else if (down) {
722 15604 putpixel(&tgt, x_pos - (j + (int)y_n), y_pos + (k + (int)(x_n + left_side_bearing)), c);
723 } else {
724 10071 putpixel(&tgt, x_pos + (k + (int)(x_n + left_side_bearing)), y_pos + (j + (int)y_n), c);
725 }
726 }
727 }
728 }
729 } else {
730 ✗ lbm_set_error_reason("Character is not one of those listed in ttf-prepare\n");
731 ✗ return ENC_SYM_EERROR;
732 }
733 262 x = x_n + advance_width;
734 262 i = next_i;
735 262 prev = utf32;
736 262 has_prev = true;
737 }
738 33 return ENC_SYM_TRUE;
739 }
740
741 2 lbm_value ext_ttf_wh(lbm_value *args, lbm_uint argn) {
742 2 lbm_value res = ENC_SYM_TERROR;
743 lbm_value font;
744 char *utf8_str;
745 2 uint32_t next_arg = 0;
746
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 if (argn >= 2 &&
747
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 lbm_is_array_r(args[0]) && // Binary font
748 2 lbm_is_array_r(args[1])) { // sequence of utf8 characters
749 2 font = args[0];
750 2 utf8_str = lbm_dec_str(args[1]);
751 2 next_arg = 2;
752 } else {
753 ✗ return res;
754 }
755
756 2 float line_spacing = 1.0f;
757 2 bool up = false;
758 2 bool down = false;
759
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 for (uint32_t i = next_arg; i < argn; i ++) {
760 ✗ if (lbm_is_symbol(args[i])) {
761 ✗ up = display_is_symbol_up(args[i]);
762 ✗ down = display_is_symbol_down(args[i]);
763 ✗ } else if (lbm_is_number(args[i])) {
764 ✗ line_spacing = lbm_dec_as_float(args[i]);
765 }
766 }
767
768 2 lbm_value r_list = lbm_heap_allocate_list(2);
769
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (lbm_is_symbol(r_list)) return r_list;
770
771 2 lbm_array_header_t *font_arr = lbm_dec_array_r(font);
772
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (font_arr->size < 10) return ENC_SYM_EERROR;
773
774 1 int32_t index = 0;
775 uint16_t version;
776
777
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!buffer_get_font_preamble((uint8_t*)font_arr->data, &version, &index)) {
778 ✗ return ENC_SYM_EERROR;
779 }
780
781 float ascender;
782 float descender;
783 float line_gap;
784
785
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if(!font_get_line_metrics((uint8_t*)font_arr->data, (int32_t)font_arr->size, &ascender, &descender, &line_gap , index)) {
786 ✗ return ENC_SYM_EERROR;
787 }
788
789 1 int32_t kern_index = 0;
790
791
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!font_get_kerning_table_index((uint8_t*)font_arr->data, (int32_t)font_arr->size, &kern_index, index)) {
792 ✗ return ENC_SYM_EERROR;
793 }
794
795 1 int32_t glyphs_index = 0;
796 uint32_t num_codes;
797 uint32_t color_fmt;
798
799
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!font_get_glyphs_table_index((uint8_t*)font_arr->data, (int32_t)font_arr->size, &glyphs_index, &num_codes, &color_fmt, index)) {
800 ✗ return ENC_SYM_EERROR;
801 }
802
803 1 float x = 0.0;
804 1 float y = 0.0;
805 1 float max_x = 0.0;
806
807 uint32_t utf32;
808 uint32_t prev;
809 1 bool has_prev = false;
810 1 uint32_t i = 0;
811 1 uint32_t next_i = 0;
812
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1 time.
6 while (get_utf32((uint8_t*)utf8_str, &utf32, i, &next_i)) {
813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (utf32 == '\n') {
814 ✗ if (x > max_x) max_x = x;
815 ✗ x = 0.0;
816 ✗ y += line_spacing * (ascender - descender + line_gap);
817 ✗ i++;
818 ✗ continue; // next iteration
819 }
820
821 5 float x_n = x;
822
823 float advance_width;
824 float left_side_bearing;
825 int32_t y_offset;
826 int32_t width;
827 int32_t height;
828 uint8_t *gfx;
829
830
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 if (font_get_glyph((uint8_t*)font_arr->data,
831 &advance_width,
832 &left_side_bearing,
833 &y_offset,
834 &width,
835 &height,
836 &gfx,
837 utf32,
838 num_codes,
839 (color_format_t)color_fmt,
840 glyphs_index)) {
841
842 5 float x_shift = 0;
843 5 float y_shift = 0;
844
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 if (has_prev) {
845 4 font_get_kerning((uint8_t*)font_arr->data,
846 prev,
847 utf32,
848 &x_shift,
849 &y_shift,
850 kern_index);
851 }
852 5 x_n += x_shift;
853 } else {
854 ✗ return ENC_SYM_EERROR;
855 }
856 5 x = x_n + advance_width;
857 5 i = next_i;
858 5 prev = utf32;
859 5 has_prev = true;
860 }
861
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (max_x < x) max_x = x;
862 1 lbm_value rest = lbm_cdr(r_list);
863
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 if (up || down) {
864 ✗ lbm_set_car(r_list, lbm_enc_u((uint32_t)(y + line_spacing * (ascender - descender + line_gap))));
865 ✗ lbm_set_car(rest, lbm_enc_u((uint32_t)max_x));
866 } else {
867 1 lbm_set_car(r_list, lbm_enc_u((uint32_t)max_x));
868 1 lbm_set_car(rest, lbm_enc_u((uint32_t)(y + line_spacing * (ascender - descender + line_gap))));
869 }
870 1 return r_list;
871 }
872
873 1 lbm_value ext_ttf_glyph_dims(lbm_value *args, lbm_uint argn) {
874
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (argn == 2 &&
875
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_array_r(args[0]) &&
876 1 lbm_is_array_r(args[1])) { // string utf8,
877
878 1 lbm_array_header_t *font_arr = lbm_dec_array_r(args[0]);
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
2 if (!font_arr) return ENC_SYM_FATAL_ERROR;
880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (font_arr->size < 10) return ENC_SYM_EERROR;
881
882 1 int32_t index = 0;
883 uint16_t version;
884
885
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!buffer_get_font_preamble((uint8_t*)font_arr->data, &version, &index)) {
886 ✗ return ENC_SYM_EERROR;
887 }
888
889 1 int32_t glyphs_index = 0;
890 uint32_t num_codes;
891 uint32_t color_fmt;
892
893
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!font_get_glyphs_table_index((uint8_t*)font_arr->data, (int32_t)font_arr->size, &glyphs_index, &num_codes, &color_fmt, index)) {
894 ✗ return ENC_SYM_EERROR;
895 }
896
897 1 lbm_array_header_t *utf8_array_header = (lbm_array_header_t*)(lbm_car(args[1]));
898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!utf8_array_header) return ENC_SYM_FATAL_ERROR;
899
900 1 uint32_t next_i = 0;
901 1 uint32_t utf32 = 0;
902 1 get_utf32((uint8_t*)utf8_array_header->data, &utf32, 0, &next_i);
903
904 float advance_width;
905 float left_side_bearing;
906 int32_t y_offset;
907 int32_t width;
908 int32_t height;
909 uint8_t *gfx;
910
911
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (font_get_glyph((uint8_t*)font_arr->data,
912 &advance_width,
913 &left_side_bearing,
914 &y_offset,
915 &width,
916 &height,
917 &gfx,
918 utf32,
919 num_codes,
920 (color_format_t)color_fmt,
921 glyphs_index)) {
922
923 1 return lbm_heap_allocate_list_init(2,
924 ✗ lbm_enc_u((uint32_t)(width)),
925 ✗ lbm_enc_u((uint32_t)height));
926 }
927 }
928 ✗ return ENC_SYM_TERROR;
929 }
930
931 2 lbm_value ext_ttf_line_height(lbm_value *args, lbm_uint argn) {
932 2 lbm_value res = ENC_SYM_TERROR;
933
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 if (argn == 1 &&
934 2 lbm_is_array_r(args[0])) {
935
936 2 lbm_array_header_t *font_arr = lbm_dec_array_r(args[0]);
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
3 if (!font_arr) return ENC_SYM_FATAL_ERROR;
938
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (font_arr->size < 10) return ENC_SYM_EERROR;
939
940 1 int32_t index = 0;
941 uint16_t version;
942
943
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!buffer_get_font_preamble((uint8_t*)font_arr->data, &version, &index)) {
944 ✗ return ENC_SYM_EERROR;
945 }
946
947 float ascender;
948 float descender;
949 float line_gap;
950
951
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if(!font_get_line_metrics((uint8_t*)font_arr->data, (int32_t)font_arr->size, &ascender, &descender, &line_gap , index)) {
952 ✗ return ENC_SYM_EERROR;
953 }
954
955 1 res = lbm_enc_float(ascender - descender + line_gap);
956 }
957 1 return res;
958 }
959
960 1 lbm_value ext_ttf_ascender(lbm_value *args, lbm_uint argn) {
961 1 lbm_value res = ENC_SYM_TERROR;
962
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (argn == 1 &&
963 1 lbm_is_array_r(args[0])) {
964
965 1 lbm_array_header_t *font_arr = lbm_dec_array_r(args[0]);
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!font_arr) return ENC_SYM_FATAL_ERROR;
967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (font_arr->size < 10) return ENC_SYM_EERROR;
968
969 1 int32_t index = 0;
970 uint16_t version;
971
972
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!buffer_get_font_preamble((uint8_t*)font_arr->data, &version, &index)) {
973 ✗ return ENC_SYM_EERROR;
974 }
975
976 float ascender;
977 float descender;
978 float line_gap;
979
980
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if(!font_get_line_metrics((uint8_t*)font_arr->data, (int32_t)font_arr->size, &ascender, &descender, &line_gap , index)) {
981 ✗ return ENC_SYM_EERROR;
982 }
983
984 1 res = lbm_enc_float(ascender);
985 }
986 1 return res;
987 }
988
989 1 lbm_value ext_ttf_descender(lbm_value *args, lbm_uint argn) {
990 1 lbm_value res = ENC_SYM_TERROR;
991
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (argn == 1 &&
992 1 lbm_is_array_r(args[0])) {
993
994 1 lbm_array_header_t *font_arr = lbm_dec_array_r(args[0]);
995
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!font_arr) return ENC_SYM_FATAL_ERROR;
996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (font_arr->size < 10) return ENC_SYM_EERROR;
997
998 1 int32_t index = 0;
999 uint16_t version;
1000
1001
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!buffer_get_font_preamble((uint8_t*)font_arr->data, &version, &index)) {
1002 ✗ return ENC_SYM_EERROR;
1003 }
1004
1005 float ascender;
1006 float descender;
1007 float line_gap;
1008
1009
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if(!font_get_line_metrics((uint8_t*)font_arr->data, (int32_t)font_arr->size, &ascender, &descender, &line_gap , index)) {
1010 ✗ return ENC_SYM_EERROR;
1011 }
1012
1013 1 res = lbm_enc_float(descender);
1014 }
1015 1 return res;
1016 }
1017
1018 1 lbm_value ext_ttf_line_gap(lbm_value *args, lbm_uint argn) {
1019 1 lbm_value res = ENC_SYM_TERROR;
1020
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (argn == 1 &&
1021 1 lbm_is_array_r(args[0])) {
1022
1023 1 lbm_array_header_t *font_arr = lbm_dec_array_r(args[0]);
1024
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!font_arr) return ENC_SYM_FATAL_ERROR;
1025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (font_arr->size < 10) return ENC_SYM_EERROR;
1026
1027 1 int32_t index = 0;
1028 uint16_t version;
1029
1030
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!buffer_get_font_preamble((uint8_t*)font_arr->data, &version, &index)) {
1031 ✗ return ENC_SYM_EERROR;
1032 }
1033
1034 float ascender;
1035 float descender;
1036 float line_gap;
1037
1038
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if(!font_get_line_metrics((uint8_t*)font_arr->data, (int32_t)font_arr->size, &ascender, &descender, &line_gap , index)) {
1039 ✗ return ENC_SYM_EERROR;
1040 }
1041
1042 1 res = lbm_enc_float(line_gap);
1043 }
1044 1 return res;
1045 }
1046
1047 711 void lbm_ttf_extensions_init(void) {
1048
1049 // metrics
1050 711 lbm_add_extension("ttf-line-height", ext_ttf_line_height);
1051 711 lbm_add_extension("ttf-ascender", ext_ttf_ascender);
1052 711 lbm_add_extension("ttf-descender", ext_ttf_descender);
1053 711 lbm_add_extension("ttf-line-gap", ext_ttf_line_gap);
1054 711 lbm_add_extension("ttf-text-dims",ext_ttf_wh);
1055 711 lbm_add_extension("ttf-glyph-dims",ext_ttf_glyph_dims);
1056
1057 // Prepare
1058 711 lbm_add_extension("ttf-prepare", ext_ttf_prepare_bin);
1059
1060 // Draw text.
1061 711 lbm_add_extension("ttf-text", ttf_text_bin);
1062 711 }
1063