Skip to content

Commit

Permalink
rdtk: Fixed warnings, added assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
akallabeth committed Jun 18, 2021
1 parent cbb3970 commit 0b0b716
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 139 deletions.
37 changes: 17 additions & 20 deletions rdtk/include/rdtk/rdtk.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@
#ifndef RDTK_H
#define RDTK_H

#include <stdint.h>
#include <rdtk/api.h>

#include <winpr/crt.h>
#include <winpr/windows.h>

#include <freerdp/codec/color.h>
#include <freerdp/codec/region.h>

typedef struct rdtk_engine rdtkEngine;
typedef struct rdtk_font rdtkFont;
typedef struct rdtk_glyph rdtkGlyph;
Expand All @@ -43,38 +38,40 @@ extern "C"

/* Engine */

RDTK_EXPORT rdtkEngine* rdtk_engine_new();
RDTK_EXPORT rdtkEngine* rdtk_engine_new(void);
RDTK_EXPORT void rdtk_engine_free(rdtkEngine* engine);

/* Surface */

RDTK_EXPORT int rdtk_surface_fill(rdtkSurface* surface, int x, int y, int width, int height,
UINT32 color);
RDTK_EXPORT int rdtk_surface_fill(rdtkSurface* surface, uint16_t x, uint16_t y, uint16_t width,
uint16_t height, uint32_t color);

RDTK_EXPORT rdtkSurface* rdtk_surface_new(rdtkEngine* engine, BYTE* data, int width, int height,
int scanline);
RDTK_EXPORT rdtkSurface* rdtk_surface_new(rdtkEngine* engine, uint8_t* data, uint16_t width,
uint16_t height, uint32_t scanline);
RDTK_EXPORT void rdtk_surface_free(rdtkSurface* surface);

/* Font */

RDTK_EXPORT int rdtk_font_draw_text(rdtkSurface* surface, int nXDst, int nYDst, rdtkFont* font,
const char* text);
RDTK_EXPORT int rdtk_font_draw_text(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst,
rdtkFont* font, const char* text);

/* Button */

RDTK_EXPORT int rdtk_button_draw(rdtkSurface* surface, int nXDst, int nYDst, int nWidth,
int nHeight, rdtkButton* button, const char* text);
RDTK_EXPORT int rdtk_button_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst,
uint16_t nWidth, uint16_t nHeight, rdtkButton* button,
const char* text);

/* Label */

RDTK_EXPORT int rdtk_label_draw(rdtkSurface* surface, int nXDst, int nYDst, int nWidth,
int nHeight, rdtkLabel* label, const char* text, int hAlign,
int vAlign);
RDTK_EXPORT int rdtk_label_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst,
uint16_t nWidth, uint16_t nHeight, rdtkLabel* label,
const char* text, uint16_t hAlign, uint16_t vAlign);

/* TextField */

RDTK_EXPORT int rdtk_text_field_draw(rdtkSurface* surface, int nXDst, int nYDst, int nWidth,
int nHeight, rdtkTextField* textField, const char* text);
RDTK_EXPORT int rdtk_text_field_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst,
uint16_t nWidth, uint16_t nHeight,
rdtkTextField* textField, const char* text);

#ifdef __cplusplus
}
Expand Down
16 changes: 8 additions & 8 deletions rdtk/librdtk/rdtk_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@

#include "rdtk_button.h"

int rdtk_button_draw(rdtkSurface* surface, int nXDst, int nYDst, int nWidth, int nHeight,
rdtkButton* button, const char* text)
int rdtk_button_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, uint16_t nWidth,
uint16_t nHeight, rdtkButton* button, const char* text)
{
int offsetX;
int offsetY;
int textWidth;
int textHeight;
int fillWidth;
int fillHeight;
uint16_t offsetX;
uint16_t offsetY;
uint16_t textWidth;
uint16_t textHeight;
uint16_t fillWidth;
uint16_t fillHeight;
rdtkFont* font;
rdtkEngine* engine;
rdtkNinePatch* ninePatch;
Expand Down
79 changes: 43 additions & 36 deletions rdtk/librdtk/rdtk_font.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ static int rdtk_font_draw_glyph(rdtkSurface* surface, int nXDst, int nYDst, rdtk
int nHeight;
int nSrcStep;
int nDstStep;
BYTE* pSrcData;
BYTE* pSrcPixel;
BYTE* pDstData;
BYTE* pDstPixel;
BYTE A, R, G, B;
uint8_t* pSrcData;
uint8_t* pSrcPixel;
uint8_t* pDstData;
uint8_t* pDstPixel;
uint8_t A, R, G, B;
nXDst += glyph->offsetX;
nYDst += glyph->offsetY;
nXSrc = glyph->rectX;
Expand Down Expand Up @@ -105,7 +105,7 @@ static int rdtk_font_draw_glyph(rdtkSurface* surface, int nXDst, int nYDst, rdtk
return 1;
}

int rdtk_font_draw_text(rdtkSurface* surface, int nXDst, int nYDst, rdtkFont* font,
int rdtk_font_draw_text(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, rdtkFont* font,
const char* text)
{
size_t index;
Expand All @@ -124,11 +124,11 @@ int rdtk_font_draw_text(rdtkSurface* surface, int nXDst, int nYDst, rdtkFont* fo
return 1;
}

int rdtk_font_text_draw_size(rdtkFont* font, int* width, int* height, const char* text)
int rdtk_font_text_draw_size(rdtkFont* font, uint16_t* width, uint16_t* height, const char* text)
{
size_t index;
size_t length;
int glyphIndex;
size_t glyphIndex;
rdtkGlyph* glyph;
*width = 0;
*height = 0;
Expand All @@ -149,41 +149,45 @@ int rdtk_font_text_draw_size(rdtkFont* font, int* width, int* height, const char
return 1;
}

static char* rdtk_font_load_descriptor_file(const char* filename, int* pSize)
static char* rdtk_font_load_descriptor_file(const char* filename, size_t* pSize)
{
BYTE* buffer;
uint8_t* buffer;
FILE* fp = NULL;
size_t readSize;
size_t fileSize;
union
{
size_t s;
INT64 i64;
} fileSize;
fp = winpr_fopen(filename, "r");

if (!fp)
return NULL;

_fseeki64(fp, 0, SEEK_END);
fileSize = _ftelli64(fp);
fileSize.i64 = _ftelli64(fp);
_fseeki64(fp, 0, SEEK_SET);

if (fileSize < 1)
if (fileSize.i64 < 1)
{
fclose(fp);
return NULL;
}

buffer = (BYTE*)malloc(fileSize + 2);
buffer = (uint8_t*)malloc(fileSize.s + 2);

if (!buffer)
{
fclose(fp);
return NULL;
}

readSize = fread(buffer, fileSize, 1, fp);
readSize = fread(buffer, fileSize.s, 1, fp);

if (!readSize)
if (readSize == 0)
{
if (!ferror(fp))
readSize = fileSize;
readSize = fileSize.s;
}

fclose(fp);
Expand All @@ -194,16 +198,16 @@ static char* rdtk_font_load_descriptor_file(const char* filename, int* pSize)
return NULL;
}

buffer[fileSize] = '\0';
buffer[fileSize + 1] = '\0';
*pSize = (int)fileSize;
buffer[fileSize.s] = '\0';
buffer[fileSize.s + 1] = '\0';
*pSize = fileSize.s;
return (char*)buffer;
}

static int rdtk_font_convert_descriptor_code_to_utf8(const char* str, BYTE* utf8)
static int rdtk_font_convert_descriptor_code_to_utf8(const char* str, uint8_t* utf8)
{
size_t len = strlen(str);
*((UINT32*)utf8) = 0;
*((uint32_t*)utf8) = 0;

if (len < 1)
return 1;
Expand Down Expand Up @@ -235,7 +239,7 @@ static int rdtk_font_convert_descriptor_code_to_utf8(const char* str, BYTE* utf8
return 1;
}

static int rdtk_font_parse_descriptor_buffer(rdtkFont* font, BYTE* buffer, int size)
static int rdtk_font_parse_descriptor_buffer(rdtkFont* font, uint8_t* buffer, size_t size)
{
char* p;
char* q;
Expand Down Expand Up @@ -576,15 +580,16 @@ static int rdtk_font_parse_descriptor_buffer(rdtkFont* font, BYTE* buffer, int s

static int rdtk_font_load_descriptor(rdtkFont* font, const char* filename)
{
int size;
size_t 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);
return rdtk_font_parse_descriptor_buffer(font, (uint8_t*)buffer, size);
}

rdtkFont* rdtk_font_new(rdtkEngine* engine, const char* path, const char* file)
{
int status;
Expand Down Expand Up @@ -658,12 +663,14 @@ rdtkFont* rdtk_font_new(rdtkEngine* engine, const char* path, const char* file)

return NULL;
}
static rdtkFont* rdtk_embedded_font_new(rdtkEngine* engine, BYTE* imageData, int imageSize,
BYTE* descriptorData, int descriptorSize)

static rdtkFont* rdtk_embedded_font_new(rdtkEngine* engine, const uint8_t* imageData,
size_t imageSize, const uint8_t* descriptorData,
size_t descriptorSize)
{
int size;
size_t size;
int status;
BYTE* buffer;
uint8_t* buffer;
rdtkFont* font;
font = (rdtkFont*)calloc(1, sizeof(rdtkFont));

Expand All @@ -689,7 +696,7 @@ static rdtkFont* rdtk_embedded_font_new(rdtkEngine* engine, BYTE* imageData, int
}

size = descriptorSize;
buffer = (BYTE*)malloc(size);
buffer = (uint8_t*)malloc(size);

if (!buffer)
{
Expand Down Expand Up @@ -726,19 +733,19 @@ int rdtk_font_engine_init(rdtkEngine* engine)
{
if (!engine->font)
{
int imageSize;
int descriptorSize;
BYTE* imageData = NULL;
BYTE* descriptorData = NULL;
SSIZE_T imageSize;
SSIZE_T descriptorSize;
const uint8_t* imageData = NULL;
const uint8_t* 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);
engine->font = rdtk_embedded_font_new(engine, imageData, (size_t)imageSize, descriptorData,
(size_t)descriptorSize);
}

return 1;
Expand Down
15 changes: 8 additions & 7 deletions rdtk/librdtk/rdtk_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
#ifndef RDTK_FONT_PRIVATE_H
#define RDTK_FONT_PRIVATE_H

#include <stdint.h>

#include <rdtk/rdtk.h>

#include <winpr/crt.h>
#include <winpr/synch.h>
#include <winpr/image.h>

#include "rdtk_engine.h"
Expand All @@ -36,19 +36,19 @@ struct rdtk_glyph
int rectY;
int rectWidth;
int rectHeight;
BYTE code[4];
uint8_t code[4];
};

struct rdtk_font
{
rdtkEngine* engine;

int size;
int height;
uint32_t size;
uint16_t height;
char* family;
char* style;
wImage* image;
int glyphCount;
uint16_t glyphCount;
rdtkGlyph* glyphs;
};

Expand All @@ -57,7 +57,8 @@ extern "C"
{
#endif

int rdtk_font_text_draw_size(rdtkFont* font, int* width, int* height, const char* text);
int rdtk_font_text_draw_size(rdtkFont* font, uint16_t* width, uint16_t* height,
const char* text);

int rdtk_font_engine_init(rdtkEngine* engine);
int rdtk_font_engine_uninit(rdtkEngine* engine);
Expand Down
13 changes: 7 additions & 6 deletions rdtk/librdtk/rdtk_label.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@

#include "rdtk_label.h"

int rdtk_label_draw(rdtkSurface* surface, int nXDst, int nYDst, int nWidth, int nHeight,
rdtkLabel* label, const char* text, int hAlign, int vAlign)
int rdtk_label_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, uint16_t nWidth,
uint16_t nHeight, rdtkLabel* label, const char* text, uint16_t hAlign,
uint16_t vAlign)
{
int offsetX;
int offsetY;
int textWidth;
int textHeight;
uint16_t offsetX;
uint16_t offsetY;
uint16_t textWidth;
uint16_t textHeight;
rdtkFont* font;
rdtkEngine* engine;

Expand Down
Loading

0 comments on commit 0b0b716

Please sign in to comment.