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)
176 char* buffer = (
char*)calloc(fileSize.s + 4,
sizeof(
char));
181 size_t readSize = fread(buffer, fileSize.s, 1, fp);
185 readSize = fileSize.s;
197 buffer[fileSize.s] =
'\0';
198 buffer[fileSize.s + 1] =
'\0';
208static int rdtk_font_convert_descriptor_code_to_utf8(
const char* str, uint8_t* utf8)
213 const size_t len = strlen(str);
214 *((uint32_t*)utf8) = 0;
221 if ((str[0] > 31) && (str[0] < 127))
223 utf8[0] = WINPR_ASSERTING_INT_CAST(uint8_t, str[0] & 0xFF);
230 const char* acc = &str[1];
232 if (strcmp(acc,
"quot;") == 0)
234 else if (strcmp(acc,
"amp;") == 0)
236 else if (strcmp(acc,
"lt;") == 0)
238 else if (strcmp(acc,
"gt;") == 0)
246static int rdtk_font_parse_descriptor_buffer(rdtkFont* font,
char* buffer,
247 WINPR_ATTR_UNUSED
size_t size)
253 const char xmlversion[] =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>";
254 const char xmlfont[] =
"<Font ";
256 char* p = strstr(buffer, xmlversion);
261 p +=
sizeof(xmlversion) - 1;
262 p = strstr(p, xmlfont);
267 p +=
sizeof(xmlfont) - 1;
271 char* end = strstr(p,
"</Font>");
276 p = strstr(p,
"size=\"");
281 p +=
sizeof(
"size=\"") - 1;
284 char* q = strchr(p,
'"');
291 long val = strtol(p, NULL, 0);
293 if ((errno != 0) || (val == 0) || (val > UINT32_MAX))
296 font->size = (UINT32)val;
306 p = strstr(p,
"family=\"");
311 p +=
sizeof(
"family=\"") - 1;
313 char* q = strchr(p,
'"');
319 font->family = _strdup(p);
328 p = strstr(p,
"height=\"");
333 p +=
sizeof(
"height=\"") - 1;
335 char* q = strchr(p,
'"');
343 const unsigned long val = strtoul(p, NULL, 0);
345 if ((errno != 0) || (val > UINT16_MAX))
348 font->height = (uint16_t)val;
352 if (font->height <= 0)
359 p = strstr(p,
"style=\"");
364 p +=
sizeof(
"style=\"") - 1;
366 char* q = strchr(p,
'"');
372 font->style = _strdup(p);
389 p = strstr(p,
"<Char ");
394 p +=
sizeof(
"<Char ") - 1;
395 char* r = strstr(p,
"/>");
401 p = r +
sizeof(
"/>");
406 if (count > UINT16_MAX)
409 font->glyphCount = (uint16_t)count;
413 font->glyphs = (rdtkGlyph*)calloc(font->glyphCount,
sizeof(rdtkGlyph));
425 p = strstr(p,
"<Char ");
430 p +=
sizeof(
"<Char ") - 1;
431 char* r = strstr(p,
"/>");
438 if (index >= font->glyphCount)
441 rdtkGlyph* glyph = &font->glyphs[index];
443 p = strstr(p,
"width=\"");
448 p +=
sizeof(
"width=\"") - 1;
450 char* q = strchr(p,
'"');
458 long val = strtol(p, NULL, 0);
460 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
463 glyph->width = (INT32)val;
467 if (glyph->width < 0)
474 p = strstr(p,
"offset=\"");
479 p +=
sizeof(
"offset=\"") - 1;
481 char* tok[4] = { 0 };
483 char* q = strchr(p,
'"');
490 p = strchr(tok[0] + 1,
' ');
499 long val = strtol(tok[0], NULL, 0);
501 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
504 glyph->offsetX = (INT32)val;
507 long val = strtol(tok[1], NULL, 0);
509 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
512 glyph->offsetY = (INT32)val;
518 p = strstr(p,
"rect=\"");
523 p +=
sizeof(
"rect=\"") - 1;
525 char* q = strchr(p,
'"');
533 p = strchr(tok[0] + 1,
' ');
540 p = strchr(tok[1] + 1,
' ');
547 p = strchr(tok[2] + 1,
' ');
556 long val = strtol(tok[0], NULL, 0);
558 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
561 glyph->rectX = (INT32)val;
564 long val = strtol(tok[1], NULL, 0);
566 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
569 glyph->rectY = (INT32)val;
572 long val = strtol(tok[2], NULL, 0);
574 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
577 glyph->rectWidth = (INT32)val;
580 long val = strtol(tok[3], NULL, 0);
582 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
585 glyph->rectHeight = (INT32)val;
591 p = strstr(p,
"code=\"");
596 p +=
sizeof(
"code=\"") - 1;
598 char* q = strchr(p,
'"');
604 rdtk_font_convert_descriptor_code_to_utf8(p, glyph->code);
608 p = r +
sizeof(
"/>");
622static int rdtk_font_load_descriptor(rdtkFont* font,
const char* filename)
627 char* buffer = rdtk_font_load_descriptor_file(filename, &size);
632 return rdtk_font_parse_descriptor_buffer(font, buffer, size);
635rdtkFont* rdtk_font_new(rdtkEngine* engine,
const char* path,
const char* file)
638 rdtkFont* font = NULL;
639 char* fontImageFile = NULL;
640 char* fontDescriptorFile = NULL;
642 WINPR_ASSERT(engine);
646 char* fontBaseFile = GetCombinedPath(path, file);
650 winpr_asprintf(&fontImageFile, &length,
"%s." FILE_EXT, fontBaseFile);
654 winpr_asprintf(&fontDescriptorFile, &length,
"%s.xml", fontBaseFile);
655 if (!fontDescriptorFile)
658 if (!winpr_PathFileExists(fontImageFile))
661 if (!winpr_PathFileExists(fontDescriptorFile))
664 font = (rdtkFont*)calloc(1,
sizeof(rdtkFont));
669 font->engine = engine;
670 font->image = winpr_image_new();
676 const int status = winpr_image_read(font->image, fontImageFile);
682 const int status2 = rdtk_font_load_descriptor(font, fontDescriptorFile);
689 free(fontDescriptorFile);
694 free(fontDescriptorFile);
696 rdtk_font_free(font);
700static rdtkFont* rdtk_embedded_font_new(rdtkEngine* engine,
const uint8_t* imageData,
701 size_t imageSize,
const uint8_t* descriptorData,
702 size_t descriptorSize)
706 WINPR_ASSERT(engine);
708 rdtkFont* font = (rdtkFont*)calloc(1,
sizeof(rdtkFont));
713 font->engine = engine;
714 font->image = winpr_image_new();
722 const int status = winpr_image_read_buffer(font->image, imageData, imageSize);
725 winpr_image_free(font->image, TRUE);
730 size = descriptorSize;
731 char* buffer = (
char*)calloc(size + 4,
sizeof(
char));
736 CopyMemory(buffer, descriptorData, size);
738 const int status2 = rdtk_font_parse_descriptor_buffer(font, buffer, size);
746 rdtk_font_free(font);
750void rdtk_font_free(rdtkFont* font)
756 winpr_image_free(font->image, TRUE);
761int rdtk_font_engine_init(rdtkEngine* engine)
763 WINPR_ASSERT(engine);
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);
773 if ((imageSize < 0) || (descriptorSize < 0))
776 engine->font = rdtk_embedded_font_new(engine, imageData, (
size_t)imageSize, descriptorData,
777 (
size_t)descriptorSize);
785int rdtk_font_engine_uninit(rdtkEngine* engine)
787 WINPR_ASSERT(engine);
790 rdtk_font_free(engine->font);