FreeRDP/rdtk/librdtk/rdtk_font.c

754 lines
12 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2014-09-29 22:07:48 +04:00
#include <winpr/crt.h>
#include <winpr/path.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
2014-09-30 00:08:08 +04:00
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;
int nSrcPad;
int nDstPad;
BYTE* pSrcData;
BYTE* pSrcPixel;
BYTE* pDstData;
BYTE* pDstPixel;
BYTE A, R, G, B;
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;
nSrcPad = (nSrcStep - (nWidth * 4));
nDstPad = (nDstStep - (nWidth * 4));
pSrcPixel = &pSrcData[(nYSrc * nSrcStep) + (nXSrc * 4)];
pDstPixel = &pDstData[(nYDst * nDstStep) + (nXDst * 4)];
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;
}
pSrcPixel += nSrcPad;
pDstPixel += nDstPad;
}
return 1;
}
2014-09-30 00:08:08 +04:00
int rdtk_font_draw_text(rdtkSurface* surface, int nXDst, int nYDst, rdtkFont* font, const char* text)
2014-09-29 22:07:48 +04:00
{
int index;
int 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
2014-10-01 20:18:17 +04:00
int rdtk_font_text_draw_size(rdtkFont* font, int* width, int* height, const char* text)
{
int index;
int length;
int glyphIndex;
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;
}
2014-09-30 00:08:08 +04:00
char* rdtk_font_load_descriptor_file(const char* filename, int* pSize)
2014-09-29 05:41:12 +04:00
{
BYTE* buffer;
FILE* fp = NULL;
size_t readSize;
size_t fileSize;
fp = fopen(filename, "r");
if (!fp)
return NULL;
fseek(fp, 0, SEEK_END);
fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (fileSize < 1)
{
fclose(fp);
return NULL;
}
buffer = (BYTE*) malloc(fileSize + 2);
if (!buffer)
{
fclose(fp);
return NULL;
}
readSize = fread(buffer, fileSize, 1, fp);
if (!readSize)
{
if (!ferror(fp))
readSize = fileSize;
}
fclose(fp);
if (readSize < 1)
{
free(buffer);
return NULL;
}
buffer[fileSize] = '\0';
buffer[fileSize + 1] = '\0';
*pSize = (int) fileSize;
return (char*) buffer;
}
2014-09-30 00:08:08 +04:00
int rdtk_font_convert_descriptor_code_to_utf8(const char* str, BYTE* utf8)
2014-09-29 05:41:12 +04:00
{
int len = strlen(str);
*((UINT32*) utf8) = 0;
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;
}
2014-09-30 00:08:08 +04:00
int rdtk_font_parse_descriptor_buffer(rdtkFont* font, BYTE* buffer, int 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;
2014-09-29 05:41:12 +04:00
2014-09-30 00:08:08 +04: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';
font->size = atoi(p);
*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';
font->height = atoi(p);
*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;
2014-09-30 00:08:08 +04:00
//printf("size: %d family: %s height: %d style: %s\n",
// 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;
if (count > 0)
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';
glyph->width = atoi(p);
*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;
glyph->offsetX = atoi(tok[0]);
glyph->offsetY = atoi(tok[1]);
*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;
glyph->rectX = atoi(tok[0]);
glyph->rectY = atoi(tok[1]);
glyph->rectWidth = atoi(tok[2]);
glyph->rectHeight = atoi(tok[3]);
*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 = '"';
p = q + 1;
/* finish parsing glyph */
p = r + sizeof("/>");
*r = '/';
index++;
}
return 1;
}
2014-09-30 00:08:08 +04:00
int rdtk_font_load_descriptor(rdtkFont* font, const char* filename)
{
int size;
char* buffer;
buffer = rdtk_font_load_descriptor_file(filename, &size);
if (!buffer)
return -1;
return rdtk_font_parse_descriptor_buffer(font, (BYTE*) buffer, size);
}
rdtkFont* rdtk_font_new(rdtkEngine* engine, const char* path, const char* file)
2014-09-29 05:41:12 +04:00
{
int status;
2014-09-29 22:07:48 +04:00
int 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);
fontImageFile = (char*) malloc(length + 8);
if (!fontImageFile)
2014-11-17 01:37:02 +03:00
goto cleanup;
2014-09-29 22:07:48 +04:00
strcpy(fontImageFile, fontBaseFile);
strcpy(&fontImageFile[length], ".png");
fontDescriptorFile = (char*) malloc(length + 8);
2014-11-17 01:37:02 +03:00
if (!fontDescriptorFile)
goto cleanup;
2014-09-29 22:07:48 +04:00
strcpy(fontDescriptorFile, fontBaseFile);
strcpy(&fontDescriptorFile[length], ".xml");
free(fontBaseFile);
if (!PathFileExistsA(fontImageFile))
2014-11-17 01:37:02 +03:00
goto cleanup;
2014-09-29 22:07:48 +04:00
if (!PathFileExistsA(fontDescriptorFile))
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
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
free(fontImageFile);
free(fontDescriptorFile);
2014-09-29 05:41:12 +04:00
return font;
2014-11-17 01:37:02 +03:00
cleanup:
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);
2014-11-17 01:37:02 +03:00
free (font);
}
return NULL;
2014-09-29 05:41:12 +04:00
}
rdtkFont* rdtk_embedded_font_new(rdtkEngine* engine, BYTE* imageData, int imageSize, BYTE* descriptorData, int descriptorSize)
2014-09-30 00:08:08 +04:00
{
2014-10-01 20:18:17 +04:00
int size;
2014-09-30 00:08:08 +04:00
int status;
2014-10-01 20:18:17 +04:00
BYTE* buffer;
2014-09-30 00:08:08 +04:00
rdtkFont* font;
font = (rdtkFont*) calloc(1, sizeof(rdtkFont));
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;
buffer = (BYTE*) malloc(size);
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
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
}
2014-09-30 00:08:08 +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
{
int imageSize;
int descriptorSize;
BYTE* imageData = NULL;
BYTE* descriptorData = NULL;
imageSize = rdtk_get_embedded_resource_file("source_serif_pro_regular_12.png", &imageData);
descriptorSize = rdtk_get_embedded_resource_file("source_serif_pro_regular_12.xml", &descriptorData);
if ((imageSize < 0) || (descriptorSize < 0))
return -1;
engine->font = rdtk_embedded_font_new(engine, imageData, imageSize, descriptorData, 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;
}