FreeRDP
Loading...
Searching...
No Matches
rdtk_font.c
1
19#include <rdtk/config.h>
20
21#include <errno.h>
22
23#include <winpr/config.h>
24#include <winpr/wtypes.h>
25#include <winpr/crt.h>
26#include <winpr/assert.h>
27#include <winpr/cast.h>
28#include <winpr/path.h>
29#include <winpr/file.h>
30#include <winpr/print.h>
31
32#include "rdtk_engine.h"
33#include "rdtk_resources.h"
34#include "rdtk_surface.h"
35
36#include "rdtk_font.h"
37
38#if defined(WINPR_WITH_PNG)
39#define FILE_EXT "png"
40#else
41#define FILE_EXT "bmp"
42#endif
43
44static int rdtk_font_draw_glyph(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst,
45 rdtkFont* font, rdtkGlyph* glyph)
46{
47 WINPR_ASSERT(surface);
48 WINPR_ASSERT(font);
49 WINPR_ASSERT(glyph);
50
51 nXDst += glyph->offsetX;
52 nYDst += glyph->offsetY;
53 const size_t nXSrc = WINPR_ASSERTING_INT_CAST(size_t, glyph->rectX);
54 const size_t nYSrc = WINPR_ASSERTING_INT_CAST(size_t, glyph->rectY);
55 const size_t nWidth = WINPR_ASSERTING_INT_CAST(size_t, glyph->rectWidth);
56 const size_t nHeight = WINPR_ASSERTING_INT_CAST(size_t, glyph->rectHeight);
57 const uint32_t nSrcStep = font->image->scanline;
58 const uint8_t* pSrcData = font->image->data;
59 uint8_t* pDstData = surface->data;
60 const uint32_t nDstStep = surface->scanline;
61
62 for (size_t y = 0; y < nHeight; y++)
63 {
64 const uint8_t* pSrcPixel = &pSrcData[((1ULL * nYSrc + y) * nSrcStep) + (4ULL * nXSrc)];
65 uint8_t* pDstPixel = &pDstData[((1ULL * nYDst + y) * nDstStep) + (4ULL * nXDst)];
66
67 for (size_t x = 0; x < nWidth; x++)
68 {
69 uint8_t B = pSrcPixel[0];
70 uint8_t G = pSrcPixel[1];
71 uint8_t R = pSrcPixel[2];
72 uint8_t A = pSrcPixel[3];
73 pSrcPixel += 4;
74
75 if (1)
76 {
77 /* tint black */
78 R = 255 - R;
79 G = 255 - G;
80 B = 255 - B;
81 }
82
83 if (A == 255)
84 {
85 pDstPixel[0] = B;
86 pDstPixel[1] = G;
87 pDstPixel[2] = R;
88 }
89 else
90 {
91 R = (R * A) / 255;
92 G = (G * A) / 255;
93 B = (B * A) / 255;
94 pDstPixel[0] = B + (pDstPixel[0] * (255 - A) + (255 / 2)) / 255;
95 pDstPixel[1] = G + (pDstPixel[1] * (255 - A) + (255 / 2)) / 255;
96 pDstPixel[2] = R + (pDstPixel[2] * (255 - A) + (255 / 2)) / 255;
97 }
98
99 pDstPixel[3] = 0xFF;
100 pDstPixel += 4;
101 }
102 }
103
104 return 1;
105}
106
107int rdtk_font_draw_text(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, rdtkFont* font,
108 const char* text)
109{
110 WINPR_ASSERT(surface);
111 WINPR_ASSERT(font);
112 WINPR_ASSERT(text);
113
114 const size_t length = strlen(text);
115 for (size_t index = 0; index < length; index++)
116 {
117 rdtkGlyph* glyph = &font->glyphs[text[index] - 32];
118 rdtk_font_draw_glyph(surface, nXDst, nYDst, font, glyph);
119 nXDst += (glyph->width + 1);
120 }
121
122 return 1;
123}
124
125int rdtk_font_text_draw_size(rdtkFont* font, uint16_t* width, uint16_t* height, const char* text)
126{
127 WINPR_ASSERT(font);
128 WINPR_ASSERT(width);
129 WINPR_ASSERT(height);
130 WINPR_ASSERT(text);
131
132 *width = 0;
133 *height = 0;
134 const size_t length = strlen(text);
135 for (size_t index = 0; index < length; index++)
136 {
137 const size_t glyphIndex = WINPR_ASSERTING_INT_CAST(size_t, text[index] - 32);
138
139 if (glyphIndex < font->glyphCount)
140 {
141 rdtkGlyph* glyph = &font->glyphs[glyphIndex];
142 *width += (glyph->width + 1);
143 }
144 }
145
146 *height = font->height + 2;
147 return 1;
148}
149
150WINPR_ATTR_MALLOC(free, 1)
151static char* rdtk_font_load_descriptor_file(const char* filename, size_t* pSize)
152{
153 WINPR_ASSERT(filename);
154 WINPR_ASSERT(pSize);
155
156 union
157 {
158 size_t s;
159 INT64 i64;
160 } fileSize;
161 FILE* fp = winpr_fopen(filename, "r");
162
163 if (!fp)
164 return NULL;
165
166 if (_fseeki64(fp, 0, SEEK_END) != 0)
167 goto fail;
168 fileSize.i64 = _ftelli64(fp);
169 if (_fseeki64(fp, 0, SEEK_SET) != 0)
170 goto fail;
171
172 if (fileSize.i64 < 1)
173 goto fail;
174
175 char* buffer = (char*)calloc(fileSize.s + 4, sizeof(char));
176
177 if (!buffer)
178 goto fail;
179
180 size_t readSize = fread(buffer, fileSize.s, 1, fp);
181 if (readSize == 0)
182 {
183 if (!ferror(fp))
184 readSize = fileSize.s;
185 }
186
187 (void)fclose(fp);
188
189 if (readSize < 1)
190 {
191 free(buffer);
192 return NULL;
193 }
194
195 buffer[fileSize.s] = '\0';
196 buffer[fileSize.s + 1] = '\0';
197 *pSize = fileSize.s;
198 return buffer;
199
200fail:
201 (void)fclose(fp);
202 return NULL;
203}
204
205static int rdtk_font_convert_descriptor_code_to_utf8(const char* str, uint8_t* utf8)
206{
207 WINPR_ASSERT(str);
208 WINPR_ASSERT(utf8);
209
210 const size_t len = strlen(str);
211 *((uint32_t*)utf8) = 0;
212
213 if (len < 1)
214 return 1;
215
216 if (len == 1)
217 {
218 if ((str[0] > 31) && (str[0] < 127))
219 {
220 utf8[0] = WINPR_ASSERTING_INT_CAST(uint8_t, str[0] & 0xFF);
221 }
222 }
223 else
224 {
225 if (str[0] == '&')
226 {
227 const char* acc = &str[1];
228
229 if (strcmp(acc, "quot;") == 0)
230 utf8[0] = '"';
231 else if (strcmp(acc, "amp;") == 0)
232 utf8[0] = '&';
233 else if (strcmp(acc, "lt;") == 0)
234 utf8[0] = '<';
235 else if (strcmp(acc, "gt;") == 0)
236 utf8[0] = '>';
237 }
238 }
239
240 return 1;
241}
242
243static int rdtk_font_parse_descriptor_buffer(rdtkFont* font, char* buffer,
244 WINPR_ATTR_UNUSED size_t size)
245{
246 int rc = -1;
247
248 WINPR_ASSERT(font);
249
250 const char xmlversion[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
251 const char xmlfont[] = "<Font ";
252
253 char* p = strstr(buffer, xmlversion);
254
255 if (!p)
256 goto fail;
257
258 p += sizeof(xmlversion) - 1;
259 p = strstr(p, xmlfont);
260
261 if (!p)
262 goto fail;
263
264 p += sizeof(xmlfont) - 1;
265
266 /* find closing font tag */
267 char* end = strstr(p, "</Font>");
268
269 if (!end)
270 goto fail;
271
272 /* parse font size */
273 p = strstr(p, "size=\"");
274
275 if (!p)
276 goto fail;
277
278 p += sizeof("size=\"") - 1;
279 char* q = strchr(p, '"');
280
281 if (!q)
282 goto fail;
283
284 *q = '\0';
285 errno = 0;
286 {
287 long val = strtol(p, NULL, 0);
288
289 if ((errno != 0) || (val == 0) || (val > UINT32_MAX))
290 goto fail;
291
292 font->size = (UINT32)val;
293 }
294 *q = '"';
295
296 if (font->size <= 0)
297 goto fail;
298
299 p = q + 1;
300 /* parse font family */
301 p = strstr(p, "family=\"");
302
303 if (!p)
304 goto fail;
305
306 p += sizeof("family=\"") - 1;
307 q = strchr(p, '"');
308
309 if (!q)
310 goto fail;
311
312 *q = '\0';
313 font->family = _strdup(p);
314 *q = '"';
315
316 if (!font->family)
317 goto fail;
318
319 p = q + 1;
320 /* parse font height */
321 p = strstr(p, "height=\"");
322
323 if (!p)
324 goto fail;
325
326 p += sizeof("height=\"") - 1;
327 q = strchr(p, '"');
328
329 if (!q)
330 goto fail;
331
332 *q = '\0';
333 errno = 0;
334 {
335 const unsigned long val = strtoul(p, NULL, 0);
336
337 if ((errno != 0) || (val > UINT16_MAX))
338 goto fail;
339
340 font->height = (uint16_t)val;
341 }
342 *q = '"';
343
344 if (font->height <= 0)
345 goto fail;
346
347 p = q + 1;
348 /* parse font style */
349 p = strstr(p, "style=\"");
350
351 if (!p)
352 goto fail;
353
354 p += sizeof("style=\"") - 1;
355 q = strchr(p, '"');
356
357 if (!q)
358 goto fail;
359
360 *q = '\0';
361 font->style = _strdup(p);
362 *q = '"';
363
364 if (!font->style)
365 goto fail;
366
367 p = q + 1;
368 // printf("size: %d family: %s height: %d style: %s\n",
369 // font->size, font->family, font->height, font->style);
370 char* beg = p;
371 size_t count = 0;
372
373 while (p < end)
374 {
375 p = strstr(p, "<Char ");
376
377 if (!p)
378 goto fail;
379
380 p += sizeof("<Char ") - 1;
381 char* r = strstr(p, "/>");
382
383 if (!r)
384 goto fail;
385
386 *r = '\0';
387 p = r + sizeof("/>");
388 *r = '/';
389 count++;
390 }
391
392 if (count > UINT16_MAX)
393 goto fail;
394
395 font->glyphCount = (uint16_t)count;
396 font->glyphs = NULL;
397
398 if (count > 0)
399 font->glyphs = (rdtkGlyph*)calloc(font->glyphCount, sizeof(rdtkGlyph));
400
401 if (!font->glyphs)
402 goto fail;
403
404 p = beg;
405 size_t index = 0;
406
407 while (p < end)
408 {
409 p = strstr(p, "<Char ");
410
411 if (!p)
412 goto fail;
413
414 p += sizeof("<Char ") - 1;
415 char* r = strstr(p, "/>");
416
417 if (!r)
418 goto fail;
419
420 *r = '\0';
421 /* start parsing glyph */
422 if (index >= font->glyphCount)
423 goto fail;
424
425 rdtkGlyph* glyph = &font->glyphs[index];
426 /* parse glyph width */
427 p = strstr(p, "width=\"");
428
429 if (!p)
430 goto fail;
431
432 p += sizeof("width=\"") - 1;
433 q = strchr(p, '"');
434
435 if (!q)
436 goto fail;
437
438 *q = '\0';
439 errno = 0;
440 {
441 long val = strtol(p, NULL, 0);
442
443 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
444 goto fail;
445
446 glyph->width = (INT32)val;
447 }
448 *q = '"';
449
450 if (glyph->width < 0)
451 goto fail;
452
453 p = q + 1;
454 /* parse glyph offset x,y */
455 p = strstr(p, "offset=\"");
456
457 if (!p)
458 goto fail;
459
460 p += sizeof("offset=\"") - 1;
461 q = strchr(p, '"');
462
463 if (!q)
464 goto fail;
465
466 char* tok[4] = { 0 };
467 *q = '\0';
468 tok[0] = p;
469 p = strchr(tok[0] + 1, ' ');
470
471 if (!p)
472 goto fail;
473
474 *p = 0;
475 tok[1] = p + 1;
476 errno = 0;
477 {
478 long val = strtol(tok[0], NULL, 0);
479
480 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
481 goto fail;
482
483 glyph->offsetX = (INT32)val;
484 }
485 {
486 long val = strtol(tok[1], NULL, 0);
487
488 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
489 goto fail;
490
491 glyph->offsetY = (INT32)val;
492 }
493 *q = '"';
494 p = q + 1;
495 /* parse glyph rect x,y,w,h */
496 p = strstr(p, "rect=\"");
497
498 if (!p)
499 goto fail;
500
501 p += sizeof("rect=\"") - 1;
502 q = strchr(p, '"');
503
504 if (!q)
505 goto fail;
506
507 *q = '\0';
508 tok[0] = p;
509 p = strchr(tok[0] + 1, ' ');
510
511 if (!p)
512 goto fail;
513
514 *p = 0;
515 tok[1] = p + 1;
516 p = strchr(tok[1] + 1, ' ');
517
518 if (!p)
519 goto fail;
520
521 *p = 0;
522 tok[2] = p + 1;
523 p = strchr(tok[2] + 1, ' ');
524
525 if (!p)
526 goto fail;
527
528 *p = 0;
529 tok[3] = p + 1;
530 errno = 0;
531 {
532 long val = strtol(tok[0], NULL, 0);
533
534 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
535 goto fail;
536
537 glyph->rectX = (INT32)val;
538 }
539 {
540 long val = strtol(tok[1], NULL, 0);
541
542 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
543 goto fail;
544
545 glyph->rectY = (INT32)val;
546 }
547 {
548 long val = strtol(tok[2], NULL, 0);
549
550 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
551 goto fail;
552
553 glyph->rectWidth = (INT32)val;
554 }
555 {
556 long val = strtol(tok[3], NULL, 0);
557
558 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
559 goto fail;
560
561 glyph->rectHeight = (INT32)val;
562 }
563 *q = '"';
564 p = q + 1;
565 /* parse code */
566 p = strstr(p, "code=\"");
567
568 if (!p)
569 goto fail;
570
571 p += sizeof("code=\"") - 1;
572 q = strchr(p, '"');
573
574 if (!q)
575 goto fail;
576
577 *q = '\0';
578 rdtk_font_convert_descriptor_code_to_utf8(p, glyph->code);
579 *q = '"';
580 /* finish parsing glyph */
581 p = r + sizeof("/>");
582 *r = '/';
583 index++;
584 }
585
586 rc = 1;
587
588fail:
589 free(buffer);
590 return rc;
591}
592
593static int rdtk_font_load_descriptor(rdtkFont* font, const char* filename)
594{
595 size_t size = 0;
596
597 WINPR_ASSERT(font);
598 char* buffer = rdtk_font_load_descriptor_file(filename, &size);
599
600 if (!buffer)
601 return -1;
602
603 return rdtk_font_parse_descriptor_buffer(font, buffer, size);
604}
605
606rdtkFont* rdtk_font_new(rdtkEngine* engine, const char* path, const char* file)
607{
608 size_t length = 0;
609 rdtkFont* font = NULL;
610 char* fontImageFile = NULL;
611 char* fontDescriptorFile = NULL;
612
613 WINPR_ASSERT(engine);
614 WINPR_ASSERT(path);
615 WINPR_ASSERT(file);
616
617 char* fontBaseFile = GetCombinedPath(path, file);
618 if (!fontBaseFile)
619 goto cleanup;
620
621 winpr_asprintf(&fontImageFile, &length, "%s." FILE_EXT, fontBaseFile);
622 if (!fontImageFile)
623 goto cleanup;
624
625 winpr_asprintf(&fontDescriptorFile, &length, "%s.xml", fontBaseFile);
626 if (!fontDescriptorFile)
627 goto cleanup;
628
629 if (!winpr_PathFileExists(fontImageFile))
630 goto cleanup;
631
632 if (!winpr_PathFileExists(fontDescriptorFile))
633 goto cleanup;
634
635 font = (rdtkFont*)calloc(1, sizeof(rdtkFont));
636
637 if (!font)
638 goto cleanup;
639
640 font->engine = engine;
641 font->image = winpr_image_new();
642
643 if (!font->image)
644 goto cleanup;
645
646 const int status = winpr_image_read(font->image, fontImageFile);
647 if (status < 0)
648 goto cleanup;
649
650 const int status2 = rdtk_font_load_descriptor(font, fontDescriptorFile);
651 if (status2 < 0)
652 goto cleanup;
653
654 free(fontBaseFile);
655 free(fontImageFile);
656 free(fontDescriptorFile);
657 return font;
658cleanup:
659 free(fontBaseFile);
660 free(fontImageFile);
661 free(fontDescriptorFile);
662
663 rdtk_font_free(font);
664 return NULL;
665}
666
667static rdtkFont* rdtk_embedded_font_new(rdtkEngine* engine, const uint8_t* imageData,
668 size_t imageSize, const uint8_t* descriptorData,
669 size_t descriptorSize)
670{
671 size_t size = 0;
672
673 WINPR_ASSERT(engine);
674
675 rdtkFont* font = (rdtkFont*)calloc(1, sizeof(rdtkFont));
676
677 if (!font)
678 return NULL;
679
680 font->engine = engine;
681 font->image = winpr_image_new();
682
683 if (!font->image)
684 {
685 free(font);
686 return NULL;
687 }
688
689 const int status = winpr_image_read_buffer(font->image, imageData, imageSize);
690 if (status < 0)
691 {
692 winpr_image_free(font->image, TRUE);
693 free(font);
694 return NULL;
695 }
696
697 size = descriptorSize;
698 char* buffer = (char*)calloc(size + 4, sizeof(char));
699
700 if (!buffer)
701 goto fail;
702
703 CopyMemory(buffer, descriptorData, size);
704 const int status2 = rdtk_font_parse_descriptor_buffer(font, buffer, size);
705
706 if (status2 < 0)
707 goto fail;
708
709 return font;
710
711fail:
712 rdtk_font_free(font);
713 return NULL;
714}
715
716void rdtk_font_free(rdtkFont* font)
717{
718 if (font)
719 {
720 free(font->family);
721 free(font->style);
722 winpr_image_free(font->image, TRUE);
723 free(font->glyphs);
724 free(font);
725 }
726}
727int rdtk_font_engine_init(rdtkEngine* engine)
728{
729 WINPR_ASSERT(engine);
730 if (!engine->font)
731 {
732 const uint8_t* imageData = NULL;
733 const uint8_t* descriptorData = NULL;
734 const SSIZE_T imageSize =
735 rdtk_get_embedded_resource_file("source_serif_pro_regular_12." FILE_EXT, &imageData);
736 const SSIZE_T descriptorSize =
737 rdtk_get_embedded_resource_file("source_serif_pro_regular_12.xml", &descriptorData);
738
739 if ((imageSize < 0) || (descriptorSize < 0))
740 return -1;
741
742 engine->font = rdtk_embedded_font_new(engine, imageData, (size_t)imageSize, descriptorData,
743 (size_t)descriptorSize);
744 if (!engine->font)
745 return -1;
746 }
747
748 return 1;
749}
750
751int rdtk_font_engine_uninit(rdtkEngine* engine)
752{
753 WINPR_ASSERT(engine);
754 if (engine->font)
755 {
756 rdtk_font_free(engine->font);
757 engine->font = NULL;
758 }
759
760 return 1;
761}