[rdtk] fix integer casts

This commit is contained in:
akallabeth 2024-12-19 11:25:50 +01:00
parent e061c02747
commit f901357a3d
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5
5 changed files with 95 additions and 90 deletions

View File

@ -19,6 +19,9 @@
#ifndef RDTK_H
#define RDTK_H
#include <winpr/assert.h>
#include <winpr/winpr.h>
#include <stdint.h>
#include <rdtk/api.h>
@ -38,9 +41,11 @@ extern "C"
/* Engine */
RDTK_EXPORT rdtkEngine* rdtk_engine_new(void);
RDTK_EXPORT void rdtk_engine_free(rdtkEngine* engine);
WINPR_ATTR_MALLOC(rdtk_engine_free, 1)
RDTK_EXPORT rdtkEngine* rdtk_engine_new(void);
/* Surface */
RDTK_EXPORT int rdtk_surface_fill(rdtkSurface* surface, uint16_t x, uint16_t y, uint16_t width,

View File

@ -17,6 +17,7 @@
*/
#include <winpr/assert.h>
#include <winpr/cast.h>
#include <rdtk/config.h>
@ -27,12 +28,8 @@
int rdtk_button_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, uint16_t nWidth,
uint16_t nHeight, rdtkButton* button, const char* text)
{
uint16_t offsetX = 0;
uint16_t offsetY = 0;
uint16_t textWidth = 0;
uint16_t textHeight = 0;
uint16_t fillWidth = 0;
uint16_t fillHeight = 0;
WINPR_ASSERT(surface);
WINPR_ASSERT(button);
@ -48,21 +45,35 @@ int rdtk_button_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, uint1
if ((textWidth > 0) && (textHeight > 0))
{
fillWidth = nWidth - (ninePatch->width - ninePatch->fillWidth);
fillHeight = nHeight - (ninePatch->height - ninePatch->fillHeight);
const int wd = (ninePatch->width - ninePatch->fillWidth);
const int hd = (ninePatch->height - ninePatch->fillHeight);
offsetX = ninePatch->fillLeft;
offsetY = ninePatch->fillTop;
const uint16_t fillWidth = nWidth - WINPR_ASSERTING_INT_CAST(uint16_t, wd);
const uint16_t fillHeight = nHeight - WINPR_ASSERTING_INT_CAST(uint16_t, hd);
uint16_t offsetX = WINPR_ASSERTING_INT_CAST(UINT16, ninePatch->fillLeft);
uint16_t offsetY = WINPR_ASSERTING_INT_CAST(UINT16, ninePatch->fillTop);
if (textWidth < fillWidth)
offsetX = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
{
const int twd = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
}
else if (textWidth < ninePatch->width)
offsetX = ((ninePatch->width - textWidth) / 2);
{
const int twd = ((ninePatch->width - textWidth) / 2);
offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
}
if (textHeight < fillHeight)
offsetY = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
{
const int twd = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
}
else if (textHeight < ninePatch->height)
offsetY = ((ninePatch->height - textHeight) / 2);
{
const int twd = ((ninePatch->height - textHeight) / 2);
offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
}
rdtk_font_draw_text(surface, nXDst + offsetX, nYDst + offsetY, font, text);
}

View File

@ -24,6 +24,7 @@
#include <winpr/wtypes.h>
#include <winpr/crt.h>
#include <winpr/assert.h>
#include <winpr/cast.h>
#include <winpr/path.h>
#include <winpr/file.h>
#include <winpr/print.h>
@ -40,8 +41,8 @@
#define FILE_EXT "bmp"
#endif
static int rdtk_font_draw_glyph(rdtkSurface* surface, int nXDst, int nYDst, rdtkFont* font,
rdtkGlyph* glyph)
static int rdtk_font_draw_glyph(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst,
rdtkFont* font, rdtkGlyph* glyph)
{
WINPR_ASSERT(surface);
WINPR_ASSERT(font);
@ -49,21 +50,21 @@ static int rdtk_font_draw_glyph(rdtkSurface* surface, int nXDst, int nYDst, rdtk
nXDst += glyph->offsetX;
nYDst += glyph->offsetY;
const int nXSrc = glyph->rectX;
const int nYSrc = glyph->rectY;
const int nWidth = glyph->rectWidth;
const int nHeight = glyph->rectHeight;
const size_t nXSrc = WINPR_ASSERTING_INT_CAST(size_t, glyph->rectX);
const size_t nYSrc = WINPR_ASSERTING_INT_CAST(size_t, glyph->rectY);
const size_t nWidth = WINPR_ASSERTING_INT_CAST(size_t, glyph->rectWidth);
const size_t nHeight = WINPR_ASSERTING_INT_CAST(size_t, glyph->rectHeight);
const uint32_t nSrcStep = font->image->scanline;
const uint8_t* pSrcData = font->image->data;
uint8_t* pDstData = surface->data;
const uint32_t nDstStep = surface->scanline;
for (int y = 0; y < nHeight; y++)
for (size_t y = 0; y < nHeight; y++)
{
const uint8_t* pSrcPixel = &pSrcData[((nYSrc + y) * nSrcStep) + (nXSrc * 4)];
uint8_t* pDstPixel = &pDstData[((nYDst + y) * nDstStep) + (nXDst * 4)];
const uint8_t* pSrcPixel = &pSrcData[((1ULL * nYSrc + y) * nSrcStep) + (4ULL * nXSrc)];
uint8_t* pDstPixel = &pDstData[((1ULL * nYDst + y) * nDstStep) + (4ULL * nXDst)];
for (int x = 0; x < nWidth; x++)
for (size_t x = 0; x < nWidth; x++)
{
uint8_t B = pSrcPixel[0];
uint8_t G = pSrcPixel[1];
@ -133,7 +134,7 @@ int rdtk_font_text_draw_size(rdtkFont* font, uint16_t* width, uint16_t* height,
const size_t length = strlen(text);
for (size_t index = 0; index < length; index++)
{
const size_t glyphIndex = text[index] - 32;
const size_t glyphIndex = WINPR_ASSERTING_INT_CAST(size_t, text[index] - 32);
if (glyphIndex < font->glyphCount)
{
@ -216,7 +217,7 @@ static int rdtk_font_convert_descriptor_code_to_utf8(const char* str, uint8_t* u
{
if ((str[0] > 31) && (str[0] < 127))
{
utf8[0] = str[0];
utf8[0] = WINPR_ASSERTING_INT_CAST(uint8_t, str[0] & 0xFF);
}
}
else
@ -330,12 +331,12 @@ static int rdtk_font_parse_descriptor_buffer(rdtkFont* font, char* buffer, size_
*q = '\0';
errno = 0;
{
long val = strtol(p, NULL, 0);
const unsigned long val = strtoul(p, NULL, 0);
if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
if ((errno != 0) || (val > UINT16_MAX))
goto fail;
font->height = val;
font->height = (uint16_t)val;
}
*q = '"';

View File

@ -17,6 +17,7 @@
*/
#include <winpr/assert.h>
#include <winpr/cast.h>
#include <rdtk/config.h>
@ -27,24 +28,17 @@
int rdtk_text_field_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, uint16_t nWidth,
uint16_t nHeight, rdtkTextField* textField, const char* text)
{
uint16_t offsetX = 0;
uint16_t offsetY = 0;
uint16_t textWidth = 0;
uint16_t textHeight = 0;
uint16_t fillWidth = 0;
uint16_t fillHeight = 0;
rdtkFont* font = NULL;
rdtkEngine* engine = NULL;
rdtkNinePatch* ninePatch = NULL;
WINPR_ASSERT(surface);
WINPR_ASSERT(textField);
WINPR_ASSERT(text);
engine = surface->engine;
font = engine->font;
rdtkEngine* engine = surface->engine;
rdtkFont* font = engine->font;
textField = surface->engine->textField;
ninePatch = textField->ninePatch;
rdtkNinePatch* ninePatch = textField->ninePatch;
rdtk_font_text_draw_size(font, &textWidth, &textHeight, text);
@ -52,21 +46,35 @@ int rdtk_text_field_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, u
if ((textWidth > 0) && (textHeight > 0))
{
fillWidth = nWidth - (ninePatch->width - ninePatch->fillWidth);
fillHeight = nHeight - (ninePatch->height - ninePatch->fillHeight);
const int fwd = (ninePatch->width - ninePatch->fillWidth);
const int fhd = (ninePatch->height - ninePatch->fillHeight);
offsetX = ninePatch->fillLeft;
offsetY = ninePatch->fillTop;
uint16_t fillWidth = nWidth - WINPR_ASSERTING_INT_CAST(uint16_t, fwd);
uint16_t fillHeight = nHeight - WINPR_ASSERTING_INT_CAST(uint16_t, fhd);
uint16_t offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, ninePatch->fillLeft);
uint16_t offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, ninePatch->fillTop);
if (textWidth < fillWidth)
offsetX = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
{
const int wd = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
}
else if (textWidth < ninePatch->width)
offsetX = ((ninePatch->width - textWidth) / 2);
{
const int wd = ((ninePatch->width - textWidth) / 2);
offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
}
if (textHeight < fillHeight)
offsetY = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
{
const int wd = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
}
else if (textHeight < ninePatch->height)
offsetY = ((ninePatch->height - textHeight) / 2);
{
const int wd = ((ninePatch->height - textHeight) / 2);
offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
}
rdtk_font_draw_text(surface, nXDst + offsetX, nYDst + offsetY, font, text);
}

View File

@ -21,6 +21,8 @@
#include <stdint.h>
#include <winpr/wlog.h>
#include <winpr/assert.h>
#include <winpr/cast.h>
#include <rdtk/rdtk.h>
#include <X11/Xlib.h>
@ -31,36 +33,17 @@
int main(int argc, char** argv)
{
int rc = 1;
GC gc = NULL;
int depth = 0;
int x = 0;
int y = 0;
int width = 0;
int height = 0;
uint8_t* buffer = NULL;
int scanline = 0;
int pf_count = 0;
XEvent event;
XImage* image = NULL;
Pixmap pixmap = 0;
Screen* screen = NULL;
Visual* visual = NULL;
int scanline_pad = 0;
int screen_number = 0;
Display* display = NULL;
Window window = 0;
Window root_window = 0;
rdtkEngine* engine = NULL;
rdtkSurface* surface = NULL;
unsigned long border = 0;
unsigned long background = 0;
XPixmapFormatValues* pf = NULL;
XPixmapFormatValues* pfs = NULL;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
display = XOpenDisplay(NULL);
Display* display = XOpenDisplay(NULL);
if (!display)
{
@ -68,27 +51,27 @@ int main(int argc, char** argv)
return 1;
}
x = 10;
y = 10;
width = 640;
height = 480;
const INT32 x = 10;
const INT32 y = 10;
const UINT32 width = 640;
const UINT32 height = 480;
screen_number = DefaultScreen(display);
screen = ScreenOfDisplay(display, screen_number);
visual = DefaultVisual(display, screen_number);
gc = DefaultGC(display, screen_number);
depth = DefaultDepthOfScreen(screen);
root_window = RootWindow(display, screen_number);
border = BlackPixel(display, screen_number);
background = WhitePixel(display, screen_number);
const int screen_number = DefaultScreen(display);
const Screen* screen = ScreenOfDisplay(display, screen_number);
Visual* visual = DefaultVisual(display, screen_number);
const GC gc = DefaultGC(display, screen_number);
const int depth = DefaultDepthOfScreen(screen);
const Window root_window = RootWindow(display, screen_number);
const unsigned long border = BlackPixel(display, screen_number);
const unsigned long background = WhitePixel(display, screen_number);
scanline_pad = 0;
int scanline_pad = 0;
pfs = XListPixmapFormats(display, &pf_count);
XPixmapFormatValues* pfs = XListPixmapFormats(display, &pf_count);
for (int index = 0; index < pf_count; index++)
{
pf = &pfs[index];
XPixmapFormatValues* pf = &pfs[index];
if (pf->depth == depth)
{
@ -99,13 +82,10 @@ int main(int argc, char** argv)
XFree(pfs);
engine = rdtk_engine_new();
if (!engine)
goto fail;
scanline = width * 4;
buffer = (uint8_t*)calloc(height, scanline);
if (!buffer)
rdtkEngine* engine = rdtk_engine_new();
const size_t scanline = width * 4ULL;
uint8_t* buffer = (uint8_t*)calloc(height, scanline);
if (!engine || !buffer || (depth < 0))
goto fail;
surface = rdtk_surface_new(engine, buffer, width, height, scanline);
@ -123,9 +103,9 @@ int main(int argc, char** argv)
XSetFunction(display, gc, GXcopy);
XSetFillStyle(display, gc, FillSolid);
pixmap = XCreatePixmap(display, window, width, height, depth);
pixmap = XCreatePixmap(display, window, width, height, (unsigned)depth);
image = XCreateImage(display, visual, depth, ZPixmap, 0, (char*)buffer, width, height,
image = XCreateImage(display, visual, (unsigned)depth, ZPixmap, 0, (char*)buffer, width, height,
scanline_pad, 0);
while (1)