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 {
176 char* buffer = (char*)calloc(fileSize.s + 4, sizeof(char));
177 if (!buffer)
178 goto fail;
179
180 {
181 size_t readSize = fread(buffer, fileSize.s, 1, fp);
182 if (readSize == 0)
183 {
184 if (!ferror(fp))
185 readSize = fileSize.s;
186 }
187
188 (void)fclose(fp);
189
190 if (readSize < 1)
191 {
192 free(buffer);
193 return NULL;
194 }
195 }
196
197 buffer[fileSize.s] = '\0';
198 buffer[fileSize.s + 1] = '\0';
199 *pSize = fileSize.s;
200 return buffer;
201 }
202
203fail:
204 (void)fclose(fp);
205 return NULL;
206}
207
208static int rdtk_font_convert_descriptor_code_to_utf8(const char* str, uint8_t* utf8)
209{
210 WINPR_ASSERT(str);
211 WINPR_ASSERT(utf8);
212
213 const size_t len = strlen(str);
214 *((uint32_t*)utf8) = 0;
215
216 if (len < 1)
217 return 1;
218
219 if (len == 1)
220 {
221 if ((str[0] > 31) && (str[0] < 127))
222 {
223 utf8[0] = WINPR_ASSERTING_INT_CAST(uint8_t, str[0] & 0xFF);
224 }
225 }
226 else
227 {
228 if (str[0] == '&')
229 {
230 const char* acc = &str[1];
231
232 if (strcmp(acc, "quot;") == 0)
233 utf8[0] = '"';
234 else if (strcmp(acc, "amp;") == 0)
235 utf8[0] = '&';
236 else if (strcmp(acc, "lt;") == 0)
237 utf8[0] = '<';
238 else if (strcmp(acc, "gt;") == 0)
239 utf8[0] = '>';
240 }
241 }
242
243 return 1;
244}
245
246static int rdtk_font_parse_descriptor_buffer(rdtkFont* font, char* buffer,
247 WINPR_ATTR_UNUSED size_t size)
248{
249 int rc = -1;
250
251 WINPR_ASSERT(font);
252
253 const char xmlversion[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
254 const char xmlfont[] = "<Font ";
255
256 char* p = strstr(buffer, xmlversion);
257
258 if (!p)
259 goto fail;
260
261 p += sizeof(xmlversion) - 1;
262 p = strstr(p, xmlfont);
263
264 if (!p)
265 goto fail;
266
267 p += sizeof(xmlfont) - 1;
268
269 /* find closing font tag */
270 {
271 char* end = strstr(p, "</Font>");
272 if (!end)
273 goto fail;
274
275 /* parse font size */
276 p = strstr(p, "size=\"");
277
278 if (!p)
279 goto fail;
280
281 p += sizeof("size=\"") - 1;
282
283 {
284 char* q = strchr(p, '"');
285 if (!q)
286 goto fail;
287
288 *q = '\0';
289 errno = 0;
290 {
291 long val = strtol(p, NULL, 0);
292
293 if ((errno != 0) || (val == 0) || (val > UINT32_MAX))
294 goto fail;
295
296 font->size = (UINT32)val;
297 }
298 *q = '"';
299
300 if (font->size <= 0)
301 goto fail;
302
303 p = q + 1;
304 }
305 /* parse font family */
306 p = strstr(p, "family=\"");
307
308 if (!p)
309 goto fail;
310
311 p += sizeof("family=\"") - 1;
312 {
313 char* q = strchr(p, '"');
314
315 if (!q)
316 goto fail;
317
318 *q = '\0';
319 font->family = _strdup(p);
320 *q = '"';
321
322 if (!font->family)
323 goto fail;
324
325 p = q + 1;
326 }
327 /* parse font height */
328 p = strstr(p, "height=\"");
329
330 if (!p)
331 goto fail;
332
333 p += sizeof("height=\"") - 1;
334 {
335 char* q = strchr(p, '"');
336
337 if (!q)
338 goto fail;
339
340 *q = '\0';
341 errno = 0;
342 {
343 const unsigned long val = strtoul(p, NULL, 0);
344
345 if ((errno != 0) || (val > UINT16_MAX))
346 goto fail;
347
348 font->height = (uint16_t)val;
349 }
350 *q = '"';
351
352 if (font->height <= 0)
353 goto fail;
354
355 p = q + 1;
356 }
357
358 /* parse font style */
359 p = strstr(p, "style=\"");
360
361 if (!p)
362 goto fail;
363
364 p += sizeof("style=\"") - 1;
365 {
366 char* q = strchr(p, '"');
367
368 if (!q)
369 goto fail;
370
371 *q = '\0';
372 font->style = _strdup(p);
373 *q = '"';
374
375 if (!font->style)
376 goto fail;
377
378 p = q + 1;
379 }
380
381 // printf("size: %d family: %s height: %d style: %s\n",
382 // font->size, font->family, font->height, font->style);
383 {
384 char* beg = p;
385 size_t count = 0;
386
387 while (p < end)
388 {
389 p = strstr(p, "<Char ");
390
391 if (!p)
392 goto fail;
393
394 p += sizeof("<Char ") - 1;
395 char* r = strstr(p, "/>");
396
397 if (!r)
398 goto fail;
399
400 *r = '\0';
401 p = r + sizeof("/>");
402 *r = '/';
403 count++;
404 }
405
406 if (count > UINT16_MAX)
407 goto fail;
408
409 font->glyphCount = (uint16_t)count;
410 font->glyphs = NULL;
411
412 if (count > 0)
413 font->glyphs = (rdtkGlyph*)calloc(font->glyphCount, sizeof(rdtkGlyph));
414
415 if (!font->glyphs)
416 goto fail;
417
418 p = beg;
419 }
420
421 {
422 size_t index = 0;
423 while (p < end)
424 {
425 p = strstr(p, "<Char ");
426
427 if (!p)
428 goto fail;
429
430 p += sizeof("<Char ") - 1;
431 char* r = strstr(p, "/>");
432
433 if (!r)
434 goto fail;
435
436 *r = '\0';
437 /* start parsing glyph */
438 if (index >= font->glyphCount)
439 goto fail;
440
441 rdtkGlyph* glyph = &font->glyphs[index];
442 /* parse glyph width */
443 p = strstr(p, "width=\"");
444
445 if (!p)
446 goto fail;
447
448 p += sizeof("width=\"") - 1;
449 {
450 char* q = strchr(p, '"');
451
452 if (!q)
453 goto fail;
454
455 *q = '\0';
456 errno = 0;
457 {
458 long val = strtol(p, NULL, 0);
459
460 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
461 goto fail;
462
463 glyph->width = (INT32)val;
464 }
465 *q = '"';
466
467 if (glyph->width < 0)
468 goto fail;
469
470 p = q + 1;
471 }
472
473 /* parse glyph offset x,y */
474 p = strstr(p, "offset=\"");
475
476 if (!p)
477 goto fail;
478
479 p += sizeof("offset=\"") - 1;
480
481 char* tok[4] = { 0 };
482 {
483 char* q = strchr(p, '"');
484
485 if (!q)
486 goto fail;
487
488 *q = '\0';
489 tok[0] = p;
490 p = strchr(tok[0] + 1, ' ');
491
492 if (!p)
493 goto fail;
494
495 *p = 0;
496 tok[1] = p + 1;
497 errno = 0;
498 {
499 long val = strtol(tok[0], NULL, 0);
500
501 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
502 goto fail;
503
504 glyph->offsetX = (INT32)val;
505 }
506 {
507 long val = strtol(tok[1], NULL, 0);
508
509 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
510 goto fail;
511
512 glyph->offsetY = (INT32)val;
513 }
514 *q = '"';
515 p = q + 1;
516 }
517 /* parse glyph rect x,y,w,h */
518 p = strstr(p, "rect=\"");
519
520 if (!p)
521 goto fail;
522
523 p += sizeof("rect=\"") - 1;
524 {
525 char* q = strchr(p, '"');
526
527 if (!q)
528 goto fail;
529
530 *q = '\0';
531
532 tok[0] = p;
533 p = strchr(tok[0] + 1, ' ');
534
535 if (!p)
536 goto fail;
537
538 *p = 0;
539 tok[1] = p + 1;
540 p = strchr(tok[1] + 1, ' ');
541
542 if (!p)
543 goto fail;
544
545 *p = 0;
546 tok[2] = p + 1;
547 p = strchr(tok[2] + 1, ' ');
548
549 if (!p)
550 goto fail;
551
552 *p = 0;
553 tok[3] = p + 1;
554 errno = 0;
555 {
556 long val = strtol(tok[0], NULL, 0);
557
558 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
559 goto fail;
560
561 glyph->rectX = (INT32)val;
562 }
563 {
564 long val = strtol(tok[1], NULL, 0);
565
566 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
567 goto fail;
568
569 glyph->rectY = (INT32)val;
570 }
571 {
572 long val = strtol(tok[2], NULL, 0);
573
574 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
575 goto fail;
576
577 glyph->rectWidth = (INT32)val;
578 }
579 {
580 long val = strtol(tok[3], NULL, 0);
581
582 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
583 goto fail;
584
585 glyph->rectHeight = (INT32)val;
586 }
587 *q = '"';
588 p = q + 1;
589 }
590 /* parse code */
591 p = strstr(p, "code=\"");
592
593 if (!p)
594 goto fail;
595
596 p += sizeof("code=\"") - 1;
597 {
598 char* q = strchr(p, '"');
599
600 if (!q)
601 goto fail;
602
603 *q = '\0';
604 rdtk_font_convert_descriptor_code_to_utf8(p, glyph->code);
605 *q = '"';
606 }
607 /* finish parsing glyph */
608 p = r + sizeof("/>");
609 *r = '/';
610 index++;
611 }
612 }
613 }
614
615 rc = 1;
616
617fail:
618 free(buffer);
619 return rc;
620}
621
622static int rdtk_font_load_descriptor(rdtkFont* font, const char* filename)
623{
624 size_t size = 0;
625
626 WINPR_ASSERT(font);
627 char* buffer = rdtk_font_load_descriptor_file(filename, &size);
628
629 if (!buffer)
630 return -1;
631
632 return rdtk_font_parse_descriptor_buffer(font, buffer, size);
633}
634
635rdtkFont* rdtk_font_new(rdtkEngine* engine, const char* path, const char* file)
636{
637 size_t length = 0;
638 rdtkFont* font = NULL;
639 char* fontImageFile = NULL;
640 char* fontDescriptorFile = NULL;
641
642 WINPR_ASSERT(engine);
643 WINPR_ASSERT(path);
644 WINPR_ASSERT(file);
645
646 char* fontBaseFile = GetCombinedPath(path, file);
647 if (!fontBaseFile)
648 goto cleanup;
649
650 winpr_asprintf(&fontImageFile, &length, "%s." FILE_EXT, fontBaseFile);
651 if (!fontImageFile)
652 goto cleanup;
653
654 winpr_asprintf(&fontDescriptorFile, &length, "%s.xml", fontBaseFile);
655 if (!fontDescriptorFile)
656 goto cleanup;
657
658 if (!winpr_PathFileExists(fontImageFile))
659 goto cleanup;
660
661 if (!winpr_PathFileExists(fontDescriptorFile))
662 goto cleanup;
663
664 font = (rdtkFont*)calloc(1, sizeof(rdtkFont));
665
666 if (!font)
667 goto cleanup;
668
669 font->engine = engine;
670 font->image = winpr_image_new();
671
672 if (!font->image)
673 goto cleanup;
674
675 {
676 const int status = winpr_image_read(font->image, fontImageFile);
677 if (status < 0)
678 goto cleanup;
679 }
680
681 {
682 const int status2 = rdtk_font_load_descriptor(font, fontDescriptorFile);
683 if (status2 < 0)
684 goto cleanup;
685 }
686
687 free(fontBaseFile);
688 free(fontImageFile);
689 free(fontDescriptorFile);
690 return font;
691cleanup:
692 free(fontBaseFile);
693 free(fontImageFile);
694 free(fontDescriptorFile);
695
696 rdtk_font_free(font);
697 return NULL;
698}
699
700static rdtkFont* rdtk_embedded_font_new(rdtkEngine* engine, const uint8_t* imageData,
701 size_t imageSize, const uint8_t* descriptorData,
702 size_t descriptorSize)
703{
704 size_t size = 0;
705
706 WINPR_ASSERT(engine);
707
708 rdtkFont* font = (rdtkFont*)calloc(1, sizeof(rdtkFont));
709
710 if (!font)
711 return NULL;
712
713 font->engine = engine;
714 font->image = winpr_image_new();
715
716 if (!font->image)
717 {
718 free(font);
719 return NULL;
720 }
721
722 const int status = winpr_image_read_buffer(font->image, imageData, imageSize);
723 if (status < 0)
724 {
725 winpr_image_free(font->image, TRUE);
726 free(font);
727 return NULL;
728 }
729
730 size = descriptorSize;
731 char* buffer = (char*)calloc(size + 4, sizeof(char));
732
733 if (!buffer)
734 goto fail;
735
736 CopyMemory(buffer, descriptorData, size);
737 {
738 const int status2 = rdtk_font_parse_descriptor_buffer(font, buffer, size);
739 if (status2 < 0)
740 goto fail;
741 }
742
743 return font;
744
745fail:
746 rdtk_font_free(font);
747 return NULL;
748}
749
750void rdtk_font_free(rdtkFont* font)
751{
752 if (font)
753 {
754 free(font->family);
755 free(font->style);
756 winpr_image_free(font->image, TRUE);
757 free(font->glyphs);
758 free(font);
759 }
760}
761int rdtk_font_engine_init(rdtkEngine* engine)
762{
763 WINPR_ASSERT(engine);
764 if (!engine->font)
765 {
766 const uint8_t* imageData = NULL;
767 const uint8_t* descriptorData = NULL;
768 const SSIZE_T imageSize =
769 rdtk_get_embedded_resource_file("source_serif_pro_regular_12." FILE_EXT, &imageData);
770 const SSIZE_T descriptorSize =
771 rdtk_get_embedded_resource_file("source_serif_pro_regular_12.xml", &descriptorData);
772
773 if ((imageSize < 0) || (descriptorSize < 0))
774 return -1;
775
776 engine->font = rdtk_embedded_font_new(engine, imageData, (size_t)imageSize, descriptorData,
777 (size_t)descriptorSize);
778 if (!engine->font)
779 return -1;
780 }
781
782 return 1;
783}
784
785int rdtk_font_engine_uninit(rdtkEngine* engine)
786{
787 WINPR_ASSERT(engine);
788 if (engine->font)
789 {
790 rdtk_font_free(engine->font);
791 engine->font = NULL;
792 }
793
794 return 1;
795}