FreeRDP/rdtk/librdtk/rdtk_font.c

761 lines
13 KiB
C
Raw Normal View History

2014-09-29 05:41:12 +04:00
/**
2014-09-30 00:08:08 +04:00
* RdTk: Remote Desktop Toolkit
2014-09-29 05:41:12 +04:00
*
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-02-16 13:10:11 +03:00
#include <rdtk/config.h>
2014-09-29 05:41:12 +04:00
2017-11-14 18:10:52 +03:00
#include <errno.h>
#include <winpr/wtypes.h>
2014-09-29 22:07:48 +04:00
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/file.h>
2014-09-30 00:08:08 +04:00
#include <winpr/print.h>
2014-09-29 22:07:48 +04:00
#include "rdtk_engine.h"
2014-09-30 00:08:08 +04:00
#include "rdtk_resources.h"
#include "rdtk_surface.h"
2014-09-29 05:41:12 +04:00
2014-09-30 00:08:08 +04:00
#include "rdtk_font.h"
2014-09-29 05:41:12 +04:00
static int rdtk_font_draw_glyph(rdtkSurface* surface, int nXDst, int nYDst, rdtkFont* font,
rdtkGlyph* glyph)
2014-09-29 22:07:48 +04:00
{
int x, y;
int nXSrc;
int nYSrc;
int nWidth;
int nHeight;
int nSrcStep;
int nDstStep;
2021-06-18 11:01:52 +03:00
uint8_t* pSrcData;
uint8_t* pSrcPixel;
uint8_t* pDstData;
uint8_t* pDstPixel;
uint8_t A, R, G, B;
2014-09-29 22:07:48 +04:00
nXDst += glyph->offsetX;
nYDst += glyph->offsetY;
nXSrc = glyph->rectX;
nYSrc = glyph->rectY;
nWidth = glyph->rectWidth;
nHeight = glyph->rectHeight;
nSrcStep = font->image->scanline;
pSrcData = font->image->data;
pDstData = surface->data;
nDstStep = surface->scanline;
for (y = 0; y < nHeight; y++)
{
pSrcPixel = &pSrcData[((nYSrc + y) * nSrcStep) + (nXSrc * 4)];
pDstPixel = &pDstData[((nYDst + y) * nDstStep) + (nXDst * 4)];
for (x = 0; x < nWidth; x++)
{
B = pSrcPixel[0];
G = pSrcPixel[1];
R = pSrcPixel[2];
A = pSrcPixel[3];
pSrcPixel += 4;
if (1)
{
/* tint black */
R = 255 - R;
G = 255 - G;
B = 255 - B;
}
if (A == 255)
{
pDstPixel[0] = B;
pDstPixel[1] = G;
pDstPixel[2] = R;
}
else
{
R = (R * A) / 255;
G = (G * A) / 255;
B = (B * A) / 255;
pDstPixel[0] = B + (pDstPixel[0] * (255 - A) + (255 / 2)) / 255;
pDstPixel[1] = G + (pDstPixel[1] * (255 - A) + (255 / 2)) / 255;
pDstPixel[2] = R + (pDstPixel[2] * (255 - A) + (255 / 2)) / 255;
}
pDstPixel[3] = 0xFF;
pDstPixel += 4;
}
}
return 1;
}
2021-06-18 11:01:52 +03:00
int rdtk_font_draw_text(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, rdtkFont* font,
const char* text)
2014-09-29 22:07:48 +04:00
{
size_t index;
size_t length;
2014-09-30 00:08:08 +04:00
rdtkGlyph* glyph;
font = surface->engine->font;
2014-09-29 22:07:48 +04:00
length = strlen(text);
for (index = 0; index < length; index++)
{
glyph = &font->glyphs[text[index] - 32];
2014-09-30 00:08:08 +04:00
rdtk_font_draw_glyph(surface, nXDst, nYDst, font, glyph);
2014-09-29 22:07:48 +04:00
nXDst += (glyph->width + 1);
}
return 1;
}
2014-09-29 05:41:12 +04:00
2021-06-18 11:01:52 +03:00
int rdtk_font_text_draw_size(rdtkFont* font, uint16_t* width, uint16_t* height, const char* text)
2014-10-01 20:18:17 +04:00
{
size_t index;
size_t length;
2021-06-18 11:01:52 +03:00
size_t glyphIndex;
2014-10-01 20:18:17 +04:00
rdtkGlyph* glyph;
*width = 0;
*height = 0;
length = strlen(text);
for (index = 0; index < length; index++)
{
glyphIndex = text[index] - 32;
if (glyphIndex < font->glyphCount)
{
glyph = &font->glyphs[glyphIndex];
*width += (glyph->width + 1);
}
}
*height = font->height + 2;
return 1;
}
2021-06-18 11:01:52 +03:00
static char* rdtk_font_load_descriptor_file(const char* filename, size_t* pSize)
2014-09-29 05:41:12 +04:00
{
2021-06-18 11:01:52 +03:00
uint8_t* buffer;
2014-09-29 05:41:12 +04:00
FILE* fp = NULL;
size_t readSize;
2021-06-18 11:01:52 +03:00
union
{
size_t s;
INT64 i64;
} fileSize;
fp = winpr_fopen(filename, "r");
2014-09-29 05:41:12 +04:00
if (!fp)
return NULL;
_fseeki64(fp, 0, SEEK_END);
2021-06-18 11:01:52 +03:00
fileSize.i64 = _ftelli64(fp);
_fseeki64(fp, 0, SEEK_SET);
2014-09-29 05:41:12 +04:00
2021-06-18 11:01:52 +03:00
if (fileSize.i64 < 1)
2014-09-29 05:41:12 +04:00
{
fclose(fp);
return NULL;
}
2021-06-18 11:01:52 +03:00
buffer = (uint8_t*)malloc(fileSize.s + 2);
2014-09-29 05:41:12 +04:00
if (!buffer)
{
fclose(fp);
return NULL;
}
2021-06-18 11:01:52 +03:00
readSize = fread(buffer, fileSize.s, 1, fp);
2014-09-29 05:41:12 +04:00
2021-06-18 11:01:52 +03:00
if (readSize == 0)
2014-09-29 05:41:12 +04:00
{
if (!ferror(fp))
2021-06-18 11:01:52 +03:00
readSize = fileSize.s;
2014-09-29 05:41:12 +04:00
}
fclose(fp);
if (readSize < 1)
{
free(buffer);
return NULL;
}
2021-06-18 11:01:52 +03:00
buffer[fileSize.s] = '\0';
buffer[fileSize.s + 1] = '\0';
*pSize = fileSize.s;
2019-11-06 17:24:51 +03:00
return (char*)buffer;
2014-09-29 05:41:12 +04:00
}
2021-06-18 11:01:52 +03:00
static int rdtk_font_convert_descriptor_code_to_utf8(const char* str, uint8_t* utf8)
2014-09-29 05:41:12 +04:00
{
size_t len = strlen(str);
2021-06-18 11:01:52 +03:00
*((uint32_t*)utf8) = 0;
2014-09-29 05:41:12 +04:00
2014-09-29 22:07:48 +04:00
if (len < 1)
return 1;
2014-09-29 05:41:12 +04:00
if (len == 1)
{
if ((str[0] > 31) && (str[0] < 127))
{
utf8[0] = str[0];
}
}
2014-09-29 22:07:48 +04:00
else
{
if (str[0] == '&')
{
const char* acc = &str[1];
if (strcmp(acc, "quot;") == 0)
utf8[0] = '"';
else if (strcmp(acc, "amp;") == 0)
utf8[0] = '&';
else if (strcmp(acc, "lt;") == 0)
utf8[0] = '<';
else if (strcmp(acc, "gt;") == 0)
utf8[0] = '>';
}
}
2014-09-29 05:41:12 +04:00
return 1;
}
2021-06-18 11:01:52 +03:00
static int rdtk_font_parse_descriptor_buffer(rdtkFont* font, uint8_t* buffer, size_t size)
2014-09-29 05:41:12 +04:00
{
char* p;
char* q;
char* r;
char* beg;
char* end;
char* tok[4];
int index;
int count;
2014-09-30 00:08:08 +04:00
rdtkGlyph* glyph;
2019-11-06 17:24:51 +03:00
p = strstr((char*)buffer, "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
2014-09-29 05:41:12 +04:00
if (!p)
return -1;
p += sizeof("<?xml version=\"1.0\" encoding=\"utf-8\"?>") - 1;
p = strstr(p, "<Font ");
if (!p)
return -1;
p += sizeof("<Font ") - 1;
/* find closing font tag */
end = strstr(p, "</Font>");
if (!end)
return -1;
/* parse font size */
p = strstr(p, "size=\"");
if (!p)
return -1;
p += sizeof("size=\"") - 1;
q = strchr(p, '"');
if (!q)
return -1;
*q = '\0';
2017-11-14 18:10:52 +03:00
errno = 0;
{
long val = strtol(p, NULL, 0);
if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
return -1;
font->size = val;
}
2014-09-29 05:41:12 +04:00
*q = '"';
if (font->size <= 0)
return -1;
p = q + 1;
/* parse font family */
p = strstr(p, "family=\"");
if (!p)
return -1;
p += sizeof("family=\"") - 1;
q = strchr(p, '"');
if (!q)
return -1;
*q = '\0';
font->family = _strdup(p);
*q = '"';
if (!font->family)
return -1;
p = q + 1;
/* parse font height */
p = strstr(p, "height=\"");
if (!p)
return -1;
p += sizeof("height=\"") - 1;
q = strchr(p, '"');
if (!q)
return -1;
*q = '\0';
2017-11-14 18:10:52 +03:00
errno = 0;
{
long val = strtol(p, NULL, 0);
if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
return -1;
font->height = val;
}
2014-09-29 05:41:12 +04:00
*q = '"';
if (font->height <= 0)
return -1;
p = q + 1;
/* parse font style */
p = strstr(p, "style=\"");
if (!p)
return -1;
p += sizeof("style=\"") - 1;
q = strchr(p, '"');
if (!q)
return -1;
*q = '\0';
font->style = _strdup(p);
*q = '"';
if (!font->style)
return -1;
p = q + 1;
2019-11-06 17:24:51 +03:00
// printf("size: %d family: %s height: %d style: %s\n",
2014-09-30 00:08:08 +04:00
// font->size, font->family, font->height, font->style);
2014-09-29 05:41:12 +04:00
beg = p;
count = 0;
while (p < end)
{
p = strstr(p, "<Char ");
if (!p)
return -1;
p += sizeof("<Char ") - 1;
r = strstr(p, "/>");
if (!r)
return -1;
*r = '\0';
p = r + sizeof("/>");
*r = '/';
count++;
}
font->glyphCount = count;
2015-06-23 13:12:59 +03:00
font->glyphs = NULL;
2015-06-23 13:12:59 +03:00
if (count > 0)
2019-11-06 17:24:51 +03:00
font->glyphs = (rdtkGlyph*)calloc(font->glyphCount, sizeof(rdtkGlyph));
2014-09-29 05:41:12 +04:00
if (!font->glyphs)
return -1;
p = beg;
index = 0;
while (p < end)
{
p = strstr(p, "<Char ");
if (!p)
return -1;
p += sizeof("<Char ") - 1;
r = strstr(p, "/>");
if (!r)
return -1;
*r = '\0';
/* start parsing glyph */
glyph = &font->glyphs[index];
/* parse glyph width */
p = strstr(p, "width=\"");
if (!p)
return -1;
p += sizeof("width=\"") - 1;
q = strchr(p, '"');
if (!q)
return -1;
*q = '\0';
2017-11-14 18:10:52 +03:00
errno = 0;
{
long val = strtoul(p, NULL, 0);
if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
return -1;
glyph->width = val;
}
2014-09-29 05:41:12 +04:00
*q = '"';
if (glyph->width < 0)
return -1;
p = q + 1;
/* parse glyph offset x,y */
p = strstr(p, "offset=\"");
if (!p)
return -1;
p += sizeof("offset=\"") - 1;
q = strchr(p, '"');
if (!q)
return -1;
*q = '\0';
tok[0] = p;
p = strchr(tok[0] + 1, ' ');
if (!p)
return -1;
*p = 0;
tok[1] = p + 1;
2017-11-14 18:10:52 +03:00
errno = 0;
{
long val = strtol(tok[0], NULL, 0);
if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
return -1;
glyph->offsetX = val;
}
{
long val = strtol(tok[1], NULL, 0);
if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
return -1;
glyph->offsetY = val;
}
2014-09-29 05:41:12 +04:00
*q = '"';
p = q + 1;
/* parse glyph rect x,y,w,h */
p = strstr(p, "rect=\"");
if (!p)
return -1;
p += sizeof("rect=\"") - 1;
q = strchr(p, '"');
if (!q)
return -1;
*q = '\0';
tok[0] = p;
p = strchr(tok[0] + 1, ' ');
if (!p)
return -1;
*p = 0;
tok[1] = p + 1;
p = strchr(tok[1] + 1, ' ');
if (!p)
return -1;
*p = 0;
tok[2] = p + 1;
p = strchr(tok[2] + 1, ' ');
if (!p)
return -1;
*p = 0;
tok[3] = p + 1;
2017-11-14 18:10:52 +03:00
errno = 0;
{
long val = strtol(tok[0], NULL, 0);
if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
return -1;
glyph->rectX = val;
}
{
long val = strtol(tok[1], NULL, 0);
if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
return -1;
glyph->rectY = val;
}
{
long val = strtol(tok[2], NULL, 0);
if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
return -1;
glyph->rectWidth = val;
}
{
long val = strtol(tok[3], NULL, 0);
if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
return -1;
glyph->rectHeight = val;
}
2014-09-29 05:41:12 +04:00
*q = '"';
p = q + 1;
/* parse code */
p = strstr(p, "code=\"");
if (!p)
return -1;
p += sizeof("code=\"") - 1;
q = strchr(p, '"');
if (!q)
return -1;
*q = '\0';
2014-09-30 00:08:08 +04:00
rdtk_font_convert_descriptor_code_to_utf8(p, glyph->code);
2014-09-29 05:41:12 +04:00
*q = '"';
/* finish parsing glyph */
p = r + sizeof("/>");
*r = '/';
index++;
}
return 1;
}
static int rdtk_font_load_descriptor(rdtkFont* font, const char* filename)
2014-09-30 00:08:08 +04:00
{
2021-06-18 11:01:52 +03:00
size_t size;
2014-09-30 00:08:08 +04:00
char* buffer;
buffer = rdtk_font_load_descriptor_file(filename, &size);
if (!buffer)
return -1;
2021-06-18 11:01:52 +03:00
return rdtk_font_parse_descriptor_buffer(font, (uint8_t*)buffer, size);
2014-09-30 00:08:08 +04:00
}
2021-06-18 11:01:52 +03:00
rdtkFont* rdtk_font_new(rdtkEngine* engine, const char* path, const char* file)
2014-09-29 05:41:12 +04:00
{
int status;
size_t length;
2014-11-17 01:37:02 +03:00
rdtkFont* font = NULL;
char* fontBaseFile = NULL;
char* fontImageFile = NULL;
char* fontDescriptorFile = NULL;
2014-09-29 22:07:48 +04:00
fontBaseFile = GetCombinedPath(path, file);
if (!fontBaseFile)
2014-11-17 01:37:02 +03:00
goto cleanup;
2014-09-29 22:07:48 +04:00
length = strlen(fontBaseFile);
2019-11-06 17:24:51 +03:00
fontImageFile = (char*)malloc(length + 8);
2014-09-29 22:07:48 +04:00
if (!fontImageFile)
2014-11-17 01:37:02 +03:00
goto cleanup;
2014-09-29 22:07:48 +04:00
2018-08-24 10:54:25 +03:00
sprintf_s(fontImageFile, length + 8, "%s.png", fontBaseFile);
2019-11-06 17:24:51 +03:00
fontDescriptorFile = (char*)malloc(length + 8);
2014-09-29 22:07:48 +04:00
2014-11-17 01:37:02 +03:00
if (!fontDescriptorFile)
goto cleanup;
2014-09-29 22:07:48 +04:00
2018-08-24 10:54:25 +03:00
sprintf_s(fontDescriptorFile, length + 8, "%s.xml", fontBaseFile);
2014-09-29 22:07:48 +04:00
if (!winpr_PathFileExists(fontImageFile))
2014-11-17 01:37:02 +03:00
goto cleanup;
2014-09-29 22:07:48 +04:00
if (!winpr_PathFileExists(fontDescriptorFile))
2014-11-17 01:37:02 +03:00
goto cleanup;
2014-09-29 05:41:12 +04:00
2019-11-06 17:24:51 +03:00
font = (rdtkFont*)calloc(1, sizeof(rdtkFont));
2014-09-29 05:41:12 +04:00
if (!font)
2014-11-17 01:37:02 +03:00
goto cleanup;
2014-09-29 05:41:12 +04:00
font->engine = engine;
2014-09-29 05:41:12 +04:00
font->image = winpr_image_new();
if (!font->image)
2014-11-17 01:37:02 +03:00
goto cleanup;
2014-09-29 05:41:12 +04:00
2014-09-29 22:07:48 +04:00
status = winpr_image_read(font->image, fontImageFile);
2014-09-29 05:41:12 +04:00
if (status < 0)
2014-11-17 01:37:02 +03:00
goto cleanup;
2014-09-29 05:41:12 +04:00
2014-09-30 00:08:08 +04:00
status = rdtk_font_load_descriptor(font, fontDescriptorFile);
2014-09-29 22:07:48 +04:00
if (status < 0)
goto cleanup;
free(fontBaseFile);
2014-09-29 22:07:48 +04:00
free(fontImageFile);
free(fontDescriptorFile);
2014-09-29 05:41:12 +04:00
return font;
2014-11-17 01:37:02 +03:00
cleanup:
free(fontBaseFile);
2015-05-11 10:07:39 +03:00
free(fontImageFile);
free(fontDescriptorFile);
2014-11-17 01:37:02 +03:00
if (font)
{
if (font->image)
2014-11-17 02:17:06 +03:00
winpr_image_free(font->image, TRUE);
free(font);
2014-11-17 01:37:02 +03:00
}
return NULL;
2014-09-29 05:41:12 +04:00
}
2021-06-18 11:01:52 +03:00
static rdtkFont* rdtk_embedded_font_new(rdtkEngine* engine, const uint8_t* imageData,
size_t imageSize, const uint8_t* descriptorData,
size_t descriptorSize)
2014-09-30 00:08:08 +04:00
{
2021-06-18 11:01:52 +03:00
size_t size;
2014-09-30 00:08:08 +04:00
int status;
2021-06-18 11:01:52 +03:00
uint8_t* buffer;
2014-09-30 00:08:08 +04:00
rdtkFont* font;
2019-11-06 17:24:51 +03:00
font = (rdtkFont*)calloc(1, sizeof(rdtkFont));
2014-09-30 00:08:08 +04:00
if (!font)
return NULL;
font->engine = engine;
2014-09-30 00:08:08 +04:00
font->image = winpr_image_new();
if (!font->image)
2014-11-17 02:54:41 +03:00
{
free(font);
2014-09-30 00:08:08 +04:00
return NULL;
2014-11-17 02:54:41 +03:00
}
2014-09-30 00:08:08 +04:00
status = winpr_image_read_buffer(font->image, imageData, imageSize);
if (status < 0)
2014-11-17 02:54:41 +03:00
{
winpr_image_free(font->image, TRUE);
free(font);
2014-09-30 00:08:08 +04:00
return NULL;
2014-11-17 02:54:41 +03:00
}
2014-09-30 00:08:08 +04:00
2014-10-01 20:18:17 +04:00
size = descriptorSize;
2021-06-18 11:01:52 +03:00
buffer = (uint8_t*)malloc(size);
2014-10-01 20:18:17 +04:00
if (!buffer)
2014-11-17 02:54:41 +03:00
{
winpr_image_free(font->image, TRUE);
free(font);
2014-10-01 20:18:17 +04:00
return NULL;
2014-11-17 02:54:41 +03:00
}
2014-10-01 20:18:17 +04:00
CopyMemory(buffer, descriptorData, size);
status = rdtk_font_parse_descriptor_buffer(font, buffer, size);
free(buffer);
2014-09-30 00:08:08 +04:00
if (status < 0)
{
winpr_image_free(font->image, TRUE);
free(font);
return NULL;
}
2014-09-30 00:08:08 +04:00
return font;
}
void rdtk_font_free(rdtkFont* font)
2014-09-29 05:41:12 +04:00
{
if (font)
{
free(font->family);
free(font->style);
winpr_image_free(font->image, TRUE);
free(font->glyphs);
free(font);
}
2014-09-29 05:41:12 +04:00
}
int rdtk_font_engine_init(rdtkEngine* engine)
2014-09-30 00:08:08 +04:00
{
if (!engine->font)
2014-09-30 00:08:08 +04:00
{
2021-06-18 11:01:52 +03:00
SSIZE_T imageSize;
SSIZE_T descriptorSize;
const uint8_t* imageData = NULL;
const uint8_t* descriptorData = NULL;
2014-09-30 00:08:08 +04:00
imageSize = rdtk_get_embedded_resource_file("source_serif_pro_regular_12.png", &imageData);
2019-11-06 17:24:51 +03:00
descriptorSize =
rdtk_get_embedded_resource_file("source_serif_pro_regular_12.xml", &descriptorData);
2014-09-30 00:08:08 +04:00
if ((imageSize < 0) || (descriptorSize < 0))
return -1;
2021-06-18 11:01:52 +03:00
engine->font = rdtk_embedded_font_new(engine, imageData, (size_t)imageSize, descriptorData,
(size_t)descriptorSize);
2014-09-30 00:08:08 +04:00
}
return 1;
}
2014-10-01 20:18:17 +04:00
int rdtk_font_engine_uninit(rdtkEngine* engine)
{
if (engine->font)
{
rdtk_font_free(engine->font);
engine->font = NULL;
}
return 1;
}