19#include <rdtk/config.h>
23#include <winpr/config.h>
24#include <winpr/wtypes.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>
32#include "rdtk_engine.h"
33#include "rdtk_resources.h"
34#include "rdtk_surface.h"
38#if defined(WINPR_WITH_PNG)
44static int rdtk_font_draw_glyph(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst,
45 rdtkFont* font, rdtkGlyph* glyph)
47 WINPR_ASSERT(surface);
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;
62 for (
size_t y = 0; y < nHeight; y++)
64 const uint8_t* pSrcPixel = &pSrcData[((1ULL * nYSrc + y) * nSrcStep) + (4ULL * nXSrc)];
65 uint8_t* pDstPixel = &pDstData[((1ULL * nYDst + y) * nDstStep) + (4ULL * nXDst)];
67 for (
size_t x = 0; x < nWidth; x++)
69 uint8_t B = pSrcPixel[0];
70 uint8_t G = pSrcPixel[1];
71 uint8_t R = pSrcPixel[2];
72 uint8_t A = pSrcPixel[3];
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;
107int rdtk_font_draw_text(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, rdtkFont* font,
110 WINPR_ASSERT(surface);
114 const size_t length = strlen(text);
115 for (
size_t index = 0; index < length; index++)
117 rdtkGlyph* glyph = &font->glyphs[text[index] - 32];
118 rdtk_font_draw_glyph(surface, nXDst, nYDst, font, glyph);
119 nXDst += (glyph->width + 1);
125int rdtk_font_text_draw_size(rdtkFont* font, uint16_t* width, uint16_t* height,
const char* text)
129 WINPR_ASSERT(height);
134 const size_t length = strlen(text);
135 for (
size_t index = 0; index < length; index++)
137 const size_t glyphIndex = WINPR_ASSERTING_INT_CAST(
size_t, text[index] - 32);
139 if (glyphIndex < font->glyphCount)
141 rdtkGlyph* glyph = &font->glyphs[glyphIndex];
142 *width += (glyph->width + 1);
146 *height = font->height + 2;
150WINPR_ATTR_MALLOC(free, 1)
151static
char* rdtk_font_load_descriptor_file(const
char* filename,
size_t* pSize)
153 WINPR_ASSERT(filename);
161 FILE* fp = winpr_fopen(filename,
"r");
166 if (_fseeki64(fp, 0, SEEK_END) != 0)
168 fileSize.i64 = _ftelli64(fp);
169 if (_fseeki64(fp, 0, SEEK_SET) != 0)
172 if (fileSize.i64 < 1)
175 char* buffer = (
char*)calloc(fileSize.s + 4,
sizeof(
char));
180 size_t readSize = fread(buffer, fileSize.s, 1, fp);
184 readSize = fileSize.s;
195 buffer[fileSize.s] =
'\0';
196 buffer[fileSize.s + 1] =
'\0';
205static int rdtk_font_convert_descriptor_code_to_utf8(
const char* str, uint8_t* utf8)
210 const size_t len = strlen(str);
211 *((uint32_t*)utf8) = 0;
218 if ((str[0] > 31) && (str[0] < 127))
220 utf8[0] = WINPR_ASSERTING_INT_CAST(uint8_t, str[0] & 0xFF);
227 const char* acc = &str[1];
229 if (strcmp(acc,
"quot;") == 0)
231 else if (strcmp(acc,
"amp;") == 0)
233 else if (strcmp(acc,
"lt;") == 0)
235 else if (strcmp(acc,
"gt;") == 0)
243static int rdtk_font_parse_descriptor_buffer(rdtkFont* font,
char* buffer,
244 WINPR_ATTR_UNUSED
size_t size)
250 const char xmlversion[] =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>";
251 const char xmlfont[] =
"<Font ";
253 char* p = strstr(buffer, xmlversion);
258 p +=
sizeof(xmlversion) - 1;
259 p = strstr(p, xmlfont);
264 p +=
sizeof(xmlfont) - 1;
267 char* end = strstr(p,
"</Font>");
273 p = strstr(p,
"size=\"");
278 p +=
sizeof(
"size=\"") - 1;
279 char* q = strchr(p,
'"');
287 long val = strtol(p, NULL, 0);
289 if ((errno != 0) || (val == 0) || (val > UINT32_MAX))
292 font->size = (UINT32)val;
301 p = strstr(p,
"family=\"");
306 p +=
sizeof(
"family=\"") - 1;
313 font->family = _strdup(p);
321 p = strstr(p,
"height=\"");
326 p +=
sizeof(
"height=\"") - 1;
335 const unsigned long val = strtoul(p, NULL, 0);
337 if ((errno != 0) || (val > UINT16_MAX))
340 font->height = (uint16_t)val;
344 if (font->height <= 0)
349 p = strstr(p,
"style=\"");
354 p +=
sizeof(
"style=\"") - 1;
361 font->style = _strdup(p);
375 p = strstr(p,
"<Char ");
380 p +=
sizeof(
"<Char ") - 1;
381 char* r = strstr(p,
"/>");
387 p = r +
sizeof(
"/>");
392 if (count > UINT16_MAX)
395 font->glyphCount = (uint16_t)count;
399 font->glyphs = (rdtkGlyph*)calloc(font->glyphCount,
sizeof(rdtkGlyph));
409 p = strstr(p,
"<Char ");
414 p +=
sizeof(
"<Char ") - 1;
415 char* r = strstr(p,
"/>");
422 if (index >= font->glyphCount)
425 rdtkGlyph* glyph = &font->glyphs[index];
427 p = strstr(p,
"width=\"");
432 p +=
sizeof(
"width=\"") - 1;
441 long val = strtol(p, NULL, 0);
443 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
446 glyph->width = (INT32)val;
450 if (glyph->width < 0)
455 p = strstr(p,
"offset=\"");
460 p +=
sizeof(
"offset=\"") - 1;
466 char* tok[4] = { 0 };
469 p = strchr(tok[0] + 1,
' ');
478 long val = strtol(tok[0], NULL, 0);
480 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
483 glyph->offsetX = (INT32)val;
486 long val = strtol(tok[1], NULL, 0);
488 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
491 glyph->offsetY = (INT32)val;
496 p = strstr(p,
"rect=\"");
501 p +=
sizeof(
"rect=\"") - 1;
509 p = strchr(tok[0] + 1,
' ');
516 p = strchr(tok[1] + 1,
' ');
523 p = strchr(tok[2] + 1,
' ');
532 long val = strtol(tok[0], NULL, 0);
534 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
537 glyph->rectX = (INT32)val;
540 long val = strtol(tok[1], NULL, 0);
542 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
545 glyph->rectY = (INT32)val;
548 long val = strtol(tok[2], NULL, 0);
550 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
553 glyph->rectWidth = (INT32)val;
556 long val = strtol(tok[3], NULL, 0);
558 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
561 glyph->rectHeight = (INT32)val;
566 p = strstr(p,
"code=\"");
571 p +=
sizeof(
"code=\"") - 1;
578 rdtk_font_convert_descriptor_code_to_utf8(p, glyph->code);
581 p = r +
sizeof(
"/>");
593static int rdtk_font_load_descriptor(rdtkFont* font,
const char* filename)
598 char* buffer = rdtk_font_load_descriptor_file(filename, &size);
603 return rdtk_font_parse_descriptor_buffer(font, buffer, size);
606rdtkFont* rdtk_font_new(rdtkEngine* engine,
const char* path,
const char* file)
609 rdtkFont* font = NULL;
610 char* fontImageFile = NULL;
611 char* fontDescriptorFile = NULL;
613 WINPR_ASSERT(engine);
617 char* fontBaseFile = GetCombinedPath(path, file);
621 winpr_asprintf(&fontImageFile, &length,
"%s." FILE_EXT, fontBaseFile);
625 winpr_asprintf(&fontDescriptorFile, &length,
"%s.xml", fontBaseFile);
626 if (!fontDescriptorFile)
629 if (!winpr_PathFileExists(fontImageFile))
632 if (!winpr_PathFileExists(fontDescriptorFile))
635 font = (rdtkFont*)calloc(1,
sizeof(rdtkFont));
640 font->engine = engine;
641 font->image = winpr_image_new();
646 const int status = winpr_image_read(font->image, fontImageFile);
650 const int status2 = rdtk_font_load_descriptor(font, fontDescriptorFile);
656 free(fontDescriptorFile);
661 free(fontDescriptorFile);
663 rdtk_font_free(font);
667static rdtkFont* rdtk_embedded_font_new(rdtkEngine* engine,
const uint8_t* imageData,
668 size_t imageSize,
const uint8_t* descriptorData,
669 size_t descriptorSize)
673 WINPR_ASSERT(engine);
675 rdtkFont* font = (rdtkFont*)calloc(1,
sizeof(rdtkFont));
680 font->engine = engine;
681 font->image = winpr_image_new();
689 const int status = winpr_image_read_buffer(font->image, imageData, imageSize);
692 winpr_image_free(font->image, TRUE);
697 size = descriptorSize;
698 char* buffer = (
char*)calloc(size + 4,
sizeof(
char));
703 CopyMemory(buffer, descriptorData, size);
704 const int status2 = rdtk_font_parse_descriptor_buffer(font, buffer, size);
712 rdtk_font_free(font);
716void rdtk_font_free(rdtkFont* font)
722 winpr_image_free(font->image, TRUE);
727int rdtk_font_engine_init(rdtkEngine* engine)
729 WINPR_ASSERT(engine);
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);
739 if ((imageSize < 0) || (descriptorSize < 0))
742 engine->font = rdtk_embedded_font_new(engine, imageData, (
size_t)imageSize, descriptorData,
743 (
size_t)descriptorSize);
751int rdtk_font_engine_uninit(rdtkEngine* engine)
753 WINPR_ASSERT(engine);
756 rdtk_font_free(engine->font);