19 #include <rdtk/config.h>
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>
32 #include "rdtk_engine.h"
33 #include "rdtk_resources.h"
34 #include "rdtk_surface.h"
36 #include "rdtk_font.h"
38 #if defined(WINPR_WITH_PNG)
39 #define FILE_EXT "png"
41 #define FILE_EXT "bmp"
44 static 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;
107 int 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);
125 int 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;
150 WINPR_ATTR_MALLOC(free, 1)
151 static
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';
205 static 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)
243 static int rdtk_font_parse_descriptor_buffer(rdtkFont* font,
char* buffer,
size_t size)
249 const char xmlversion[] =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>";
250 const char xmlfont[] =
"<Font ";
252 char* p = strstr(buffer, xmlversion);
257 p +=
sizeof(xmlversion) - 1;
258 p = strstr(p, xmlfont);
263 p +=
sizeof(xmlfont) - 1;
266 char* end = strstr(p,
"</Font>");
272 p = strstr(p,
"size=\"");
277 p +=
sizeof(
"size=\"") - 1;
278 char* q = strchr(p,
'"');
286 long val = strtol(p, NULL, 0);
288 if ((errno != 0) || (val == 0) || (val > UINT32_MAX))
291 font->size = (UINT32)val;
300 p = strstr(p,
"family=\"");
305 p +=
sizeof(
"family=\"") - 1;
312 font->family = _strdup(p);
320 p = strstr(p,
"height=\"");
325 p +=
sizeof(
"height=\"") - 1;
334 const unsigned long val = strtoul(p, NULL, 0);
336 if ((errno != 0) || (val > UINT16_MAX))
339 font->height = (uint16_t)val;
343 if (font->height <= 0)
348 p = strstr(p,
"style=\"");
353 p +=
sizeof(
"style=\"") - 1;
360 font->style = _strdup(p);
374 p = strstr(p,
"<Char ");
379 p +=
sizeof(
"<Char ") - 1;
380 char* r = strstr(p,
"/>");
386 p = r +
sizeof(
"/>");
391 if (count > UINT16_MAX)
394 font->glyphCount = (uint16_t)count;
398 font->glyphs = (rdtkGlyph*)calloc(font->glyphCount,
sizeof(rdtkGlyph));
408 p = strstr(p,
"<Char ");
413 p +=
sizeof(
"<Char ") - 1;
414 char* r = strstr(p,
"/>");
421 if (index >= font->glyphCount)
424 rdtkGlyph* glyph = &font->glyphs[index];
426 p = strstr(p,
"width=\"");
431 p +=
sizeof(
"width=\"") - 1;
440 long val = strtol(p, NULL, 0);
442 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
445 glyph->width = (INT32)val;
449 if (glyph->width < 0)
454 p = strstr(p,
"offset=\"");
459 p +=
sizeof(
"offset=\"") - 1;
465 char* tok[4] = { 0 };
468 p = strchr(tok[0] + 1,
' ');
477 long val = strtol(tok[0], NULL, 0);
479 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
482 glyph->offsetX = (INT32)val;
485 long val = strtol(tok[1], NULL, 0);
487 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
490 glyph->offsetY = (INT32)val;
495 p = strstr(p,
"rect=\"");
500 p +=
sizeof(
"rect=\"") - 1;
508 p = strchr(tok[0] + 1,
' ');
515 p = strchr(tok[1] + 1,
' ');
522 p = strchr(tok[2] + 1,
' ');
531 long val = strtol(tok[0], NULL, 0);
533 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
536 glyph->rectX = (INT32)val;
539 long val = strtol(tok[1], NULL, 0);
541 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
544 glyph->rectY = (INT32)val;
547 long val = strtol(tok[2], NULL, 0);
549 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
552 glyph->rectWidth = (INT32)val;
555 long val = strtol(tok[3], NULL, 0);
557 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
560 glyph->rectHeight = (INT32)val;
565 p = strstr(p,
"code=\"");
570 p +=
sizeof(
"code=\"") - 1;
577 rdtk_font_convert_descriptor_code_to_utf8(p, glyph->code);
580 p = r +
sizeof(
"/>");
592 static int rdtk_font_load_descriptor(rdtkFont* font,
const char* filename)
597 char* buffer = rdtk_font_load_descriptor_file(filename, &size);
602 return rdtk_font_parse_descriptor_buffer(font, buffer, size);
605 rdtkFont* rdtk_font_new(rdtkEngine* engine,
const char* path,
const char* file)
608 rdtkFont* font = NULL;
609 char* fontImageFile = NULL;
610 char* fontDescriptorFile = NULL;
612 WINPR_ASSERT(engine);
616 char* fontBaseFile = GetCombinedPath(path, file);
620 winpr_asprintf(&fontImageFile, &length,
"%s." FILE_EXT, fontBaseFile);
624 winpr_asprintf(&fontDescriptorFile, &length,
"%s.xml", fontBaseFile);
625 if (!fontDescriptorFile)
628 if (!winpr_PathFileExists(fontImageFile))
631 if (!winpr_PathFileExists(fontDescriptorFile))
634 font = (rdtkFont*)calloc(1,
sizeof(rdtkFont));
639 font->engine = engine;
640 font->image = winpr_image_new();
645 const int status = winpr_image_read(font->image, fontImageFile);
649 const int status2 = rdtk_font_load_descriptor(font, fontDescriptorFile);
655 free(fontDescriptorFile);
660 free(fontDescriptorFile);
662 rdtk_font_free(font);
666 static rdtkFont* rdtk_embedded_font_new(rdtkEngine* engine,
const uint8_t* imageData,
667 size_t imageSize,
const uint8_t* descriptorData,
668 size_t descriptorSize)
672 WINPR_ASSERT(engine);
674 rdtkFont* font = (rdtkFont*)calloc(1,
sizeof(rdtkFont));
679 font->engine = engine;
680 font->image = winpr_image_new();
688 const int status = winpr_image_read_buffer(font->image, imageData, imageSize);
691 winpr_image_free(font->image, TRUE);
696 size = descriptorSize;
697 char* buffer = (
char*)calloc(size + 4,
sizeof(
char));
702 CopyMemory(buffer, descriptorData, size);
703 const int status2 = rdtk_font_parse_descriptor_buffer(font, buffer, size);
711 rdtk_font_free(font);
715 void rdtk_font_free(rdtkFont* font)
721 winpr_image_free(font->image, TRUE);
726 int rdtk_font_engine_init(rdtkEngine* engine)
728 WINPR_ASSERT(engine);
731 const uint8_t* imageData = NULL;
732 const uint8_t* descriptorData = NULL;
733 const SSIZE_T imageSize =
734 rdtk_get_embedded_resource_file(
"source_serif_pro_regular_12." FILE_EXT, &imageData);
735 const SSIZE_T descriptorSize =
736 rdtk_get_embedded_resource_file(
"source_serif_pro_regular_12.xml", &descriptorData);
738 if ((imageSize < 0) || (descriptorSize < 0))
741 engine->font = rdtk_embedded_font_new(engine, imageData, (
size_t)imageSize, descriptorData,
742 (
size_t)descriptorSize);
750 int rdtk_font_engine_uninit(rdtkEngine* engine)
752 WINPR_ASSERT(engine);
755 rdtk_font_free(engine->font);