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/path.h>
28 #include <winpr/file.h>
29 #include <winpr/print.h>
31 #include "rdtk_engine.h"
32 #include "rdtk_resources.h"
33 #include "rdtk_surface.h"
35 #include "rdtk_font.h"
37 #if defined(WINPR_WITH_PNG)
38 #define FILE_EXT "png"
40 #define FILE_EXT "bmp"
43 static int rdtk_font_draw_glyph(rdtkSurface* surface,
int nXDst,
int nYDst, rdtkFont* font,
46 WINPR_ASSERT(surface);
50 nXDst += glyph->offsetX;
51 nYDst += glyph->offsetY;
52 const int nXSrc = glyph->rectX;
53 const int nYSrc = glyph->rectY;
54 const int nWidth = glyph->rectWidth;
55 const int nHeight = glyph->rectHeight;
56 const uint32_t nSrcStep = font->image->scanline;
57 const uint8_t* pSrcData = font->image->data;
58 uint8_t* pDstData = surface->data;
59 const uint32_t nDstStep = surface->scanline;
61 for (
int y = 0; y < nHeight; y++)
63 const uint8_t* pSrcPixel = &pSrcData[((nYSrc + y) * nSrcStep) + (nXSrc * 4)];
64 uint8_t* pDstPixel = &pDstData[((nYDst + y) * nDstStep) + (nXDst * 4)];
66 for (
int x = 0; x < nWidth; x++)
68 uint8_t B = pSrcPixel[0];
69 uint8_t G = pSrcPixel[1];
70 uint8_t R = pSrcPixel[2];
71 uint8_t A = pSrcPixel[3];
93 pDstPixel[0] = B + (pDstPixel[0] * (255 - A) + (255 / 2)) / 255;
94 pDstPixel[1] = G + (pDstPixel[1] * (255 - A) + (255 / 2)) / 255;
95 pDstPixel[2] = R + (pDstPixel[2] * (255 - A) + (255 / 2)) / 255;
106 int rdtk_font_draw_text(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, rdtkFont* font,
109 WINPR_ASSERT(surface);
113 const size_t length = strlen(text);
114 for (
size_t index = 0; index < length; index++)
116 rdtkGlyph* glyph = &font->glyphs[text[index] - 32];
117 rdtk_font_draw_glyph(surface, nXDst, nYDst, font, glyph);
118 nXDst += (glyph->width + 1);
124 int rdtk_font_text_draw_size(rdtkFont* font, uint16_t* width, uint16_t* height,
const char* text)
128 WINPR_ASSERT(height);
133 const size_t length = strlen(text);
134 for (
size_t index = 0; index < length; index++)
136 const size_t glyphIndex = text[index] - 32;
138 if (glyphIndex < font->glyphCount)
140 rdtkGlyph* glyph = &font->glyphs[glyphIndex];
141 *width += (glyph->width + 1);
145 *height = font->height + 2;
149 static char* rdtk_font_load_descriptor_file(
const char* filename,
size_t* pSize)
151 WINPR_ASSERT(filename);
159 FILE* fp = winpr_fopen(filename,
"r");
164 if (_fseeki64(fp, 0, SEEK_END) != 0)
166 fileSize.i64 = _ftelli64(fp);
167 if (_fseeki64(fp, 0, SEEK_SET) != 0)
170 if (fileSize.i64 < 1)
173 uint8_t* buffer = (uint8_t*)malloc(fileSize.s + 2);
178 size_t readSize = fread(buffer, fileSize.s, 1, fp);
182 readSize = fileSize.s;
193 buffer[fileSize.s] =
'\0';
194 buffer[fileSize.s + 1] =
'\0';
196 return (
char*)buffer;
203 static int rdtk_font_convert_descriptor_code_to_utf8(
const char* str, uint8_t* utf8)
208 const size_t len = strlen(str);
209 *((uint32_t*)utf8) = 0;
216 if ((str[0] > 31) && (str[0] < 127))
225 const char* acc = &str[1];
227 if (strcmp(acc,
"quot;") == 0)
229 else if (strcmp(acc,
"amp;") == 0)
231 else if (strcmp(acc,
"lt;") == 0)
233 else if (strcmp(acc,
"gt;") == 0)
241 static int rdtk_font_parse_descriptor_buffer(rdtkFont* font, uint8_t* buffer,
size_t size)
245 char* p = strstr((
char*)buffer,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>");
250 p +=
sizeof(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>") - 1;
251 p = strstr(p,
"<Font ");
256 p +=
sizeof(
"<Font ") - 1;
258 char* end = strstr(p,
"</Font>");
264 p = strstr(p,
"size=\"");
269 p +=
sizeof(
"size=\"") - 1;
270 char* q = strchr(p,
'"');
278 long val = strtol(p, NULL, 0);
280 if ((errno != 0) || (val == 0) || (val > UINT32_MAX))
283 font->size = (UINT32)val;
292 p = strstr(p,
"family=\"");
297 p +=
sizeof(
"family=\"") - 1;
304 font->family = _strdup(p);
312 p = strstr(p,
"height=\"");
317 p +=
sizeof(
"height=\"") - 1;
326 long val = strtol(p, NULL, 0);
328 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
335 if (font->height <= 0)
340 p = strstr(p,
"style=\"");
345 p +=
sizeof(
"style=\"") - 1;
352 font->style = _strdup(p);
366 p = strstr(p,
"<Char ");
371 p +=
sizeof(
"<Char ") - 1;
372 char* r = strstr(p,
"/>");
378 p = r +
sizeof(
"/>");
383 if (count > UINT16_MAX)
386 font->glyphCount = (uint16_t)count;
390 font->glyphs = (rdtkGlyph*)calloc(font->glyphCount,
sizeof(rdtkGlyph));
400 p = strstr(p,
"<Char ");
405 p +=
sizeof(
"<Char ") - 1;
406 char* r = strstr(p,
"/>");
413 if (index >= font->glyphCount)
416 rdtkGlyph* glyph = &font->glyphs[index];
418 p = strstr(p,
"width=\"");
423 p +=
sizeof(
"width=\"") - 1;
432 long val = strtol(p, NULL, 0);
434 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
437 glyph->width = (INT32)val;
441 if (glyph->width < 0)
446 p = strstr(p,
"offset=\"");
451 p +=
sizeof(
"offset=\"") - 1;
457 char* tok[4] = { 0 };
460 p = strchr(tok[0] + 1,
' ');
469 long val = strtol(tok[0], NULL, 0);
471 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
474 glyph->offsetX = (INT32)val;
477 long val = strtol(tok[1], NULL, 0);
479 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
482 glyph->offsetY = (INT32)val;
487 p = strstr(p,
"rect=\"");
492 p +=
sizeof(
"rect=\"") - 1;
500 p = strchr(tok[0] + 1,
' ');
507 p = strchr(tok[1] + 1,
' ');
514 p = strchr(tok[2] + 1,
' ');
523 long val = strtol(tok[0], NULL, 0);
525 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
528 glyph->rectX = (INT32)val;
531 long val = strtol(tok[1], NULL, 0);
533 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
536 glyph->rectY = (INT32)val;
539 long val = strtol(tok[2], NULL, 0);
541 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
544 glyph->rectWidth = (INT32)val;
547 long val = strtol(tok[3], NULL, 0);
549 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
552 glyph->rectHeight = (INT32)val;
557 p = strstr(p,
"code=\"");
562 p +=
sizeof(
"code=\"") - 1;
569 rdtk_font_convert_descriptor_code_to_utf8(p, glyph->code);
572 p = r +
sizeof(
"/>");
580 static int rdtk_font_load_descriptor(rdtkFont* font,
const char* filename)
585 char* buffer = rdtk_font_load_descriptor_file(filename, &size);
590 return rdtk_font_parse_descriptor_buffer(font, (uint8_t*)buffer, size);
593 rdtkFont* rdtk_font_new(rdtkEngine* engine,
const char* path,
const char* file)
596 rdtkFont* font = NULL;
597 char* fontImageFile = NULL;
598 char* fontDescriptorFile = NULL;
600 WINPR_ASSERT(engine);
604 char* fontBaseFile = GetCombinedPath(path, file);
608 winpr_asprintf(&fontImageFile, &length,
"%s." FILE_EXT, fontBaseFile);
612 winpr_asprintf(&fontDescriptorFile, &length,
"%s.xml", fontBaseFile);
613 if (!fontDescriptorFile)
616 if (!winpr_PathFileExists(fontImageFile))
619 if (!winpr_PathFileExists(fontDescriptorFile))
622 font = (rdtkFont*)calloc(1,
sizeof(rdtkFont));
627 font->engine = engine;
628 font->image = winpr_image_new();
633 const int status = winpr_image_read(font->image, fontImageFile);
637 const int status2 = rdtk_font_load_descriptor(font, fontDescriptorFile);
643 free(fontDescriptorFile);
648 free(fontDescriptorFile);
650 rdtk_font_free(font);
654 static rdtkFont* rdtk_embedded_font_new(rdtkEngine* engine,
const uint8_t* imageData,
655 size_t imageSize,
const uint8_t* descriptorData,
656 size_t descriptorSize)
659 uint8_t* buffer = NULL;
661 WINPR_ASSERT(engine);
663 rdtkFont* font = (rdtkFont*)calloc(1,
sizeof(rdtkFont));
668 font->engine = engine;
669 font->image = winpr_image_new();
677 const int status = winpr_image_read_buffer(font->image, imageData, imageSize);
680 winpr_image_free(font->image, TRUE);
685 size = descriptorSize;
686 buffer = (uint8_t*)malloc(size);
691 CopyMemory(buffer, descriptorData, size);
692 const int status2 = rdtk_font_parse_descriptor_buffer(font, buffer, size);
701 rdtk_font_free(font);
705 void rdtk_font_free(rdtkFont* font)
711 winpr_image_free(font->image, TRUE);
716 int rdtk_font_engine_init(rdtkEngine* engine)
718 WINPR_ASSERT(engine);
721 const uint8_t* imageData = NULL;
722 const uint8_t* descriptorData = NULL;
723 const SSIZE_T imageSize =
724 rdtk_get_embedded_resource_file(
"source_serif_pro_regular_12." FILE_EXT, &imageData);
725 const SSIZE_T descriptorSize =
726 rdtk_get_embedded_resource_file(
"source_serif_pro_regular_12.xml", &descriptorData);
728 if ((imageSize < 0) || (descriptorSize < 0))
731 engine->font = rdtk_embedded_font_new(engine, imageData, (
size_t)imageSize, descriptorData,
732 (
size_t)descriptorSize);
740 int rdtk_font_engine_uninit(rdtkEngine* engine)
742 WINPR_ASSERT(engine);
745 rdtk_font_free(engine->font);