1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-07-03 12:21:20 +00:00

Check for SVG start tag and add tests

This commit is contained in:
Samuel Gomes 2023-12-05 09:20:25 +05:30
parent 753ecb9609
commit bbfd228597
8 changed files with 1507 additions and 22 deletions

View file

@ -202,20 +202,16 @@ static uint32_t *image_svg_load_from_file(const char *fileName, int32_t *xOut, i
fclose(fp);
return nullptr;
}
svgString[size] = '\0'; // must be null terminated
fclose(fp);
// Bail if we have binary data
for (size_t i = 0; i < size; i++) {
uint8_t c = svgString[i];
if (c < 32 && c != '\0' && c != '\t' && c != '\r' && c != '\n') {
free(svgString);
return nullptr;
}
// Check if it has a valid SVG start tag
if (!strstr(svgString, "<svg")) {
free(svgString);
return nullptr;
}
svgString[size] = '\0'; // must be null terminated
auto image = nsvgParse(svgString, "px", 96.0f); // important note: changes the string
if (!image) {
free(svgString);
@ -242,17 +238,15 @@ static uint32_t *image_svg_load_from_memory(const uint8_t *buffer, size_t size,
if (!svgString)
return nullptr;
// Bail if we have binary data. We'll also copy the data while doing the check to avoid another pass
for (size_t i = 0; i < size; i++) {
uint8_t c = svgString[i] = buffer[i];
if (c < 32 && c != '\0' && c != '\t' && c != '\r' && c != '\n') {
free(svgString);
return nullptr;
}
}
memcpy(svgString, buffer, size);
svgString[size] = '\0'; // must be null terminated
// Check if it has a valid SVG start tag
if (!strstr(svgString, "<svg")) {
free(svgString);
return nullptr;
}
auto image = nsvgParse(svgString, "px", 96.0f); // important note: changes the string
if (!image) {
free(svgString);

View file

@ -331,6 +331,7 @@ static float nsvg__normalize(float *x, float* y)
}
static float nsvg__absf(float x) { return x < 0 ? -x : x; }
static float nsvg__roundf(float x) { return (x >= 0) ? floorf(x + 0.5) : ceilf(x - 0.5); }
static void nsvg__flattenCubicBez(NSVGrasterizer* r,
float x1, float y1, float x2, float y2,
@ -872,10 +873,10 @@ static NSVGactiveEdge* nsvg__addActive(NSVGrasterizer* r, NSVGedge* e, float sta
// STBTT_assert(e->y0 <= start_point);
// round dx down to avoid going too far
if (dxdy < 0)
z->dx = (int)(-floorf(NSVG__FIX * -dxdy));
z->dx = (int)(-nsvg__roundf(NSVG__FIX * -dxdy));
else
z->dx = (int)floorf(NSVG__FIX * dxdy);
z->x = (int)floorf(NSVG__FIX * (e->x0 + dxdy * (startPoint - e->y0)));
z->dx = (int)nsvg__roundf(NSVG__FIX * dxdy);
z->x = (int)nsvg__roundf(NSVG__FIX * (e->x0 + dxdy * (startPoint - e->y0)));
// z->x -= off_x * FIX;
z->ey = e->y1;
z->next = 0;
@ -1282,7 +1283,7 @@ static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opac
if (grad->nstops == 0) {
for (i = 0; i < 256; i++)
cache->colors[i] = 0;
} if (grad->nstops == 1) {
} else if (grad->nstops == 1) {
for (i = 0; i < 256; i++)
cache->colors[i] = nsvg__applyOpacity(grad->stops[i].color, opacity);
} else {

View file

@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Binary file not shown.

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 94 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 94 KiB

View file

@ -0,0 +1,26 @@
$CONSOLE:ONLY
OPTION _EXPLICIT
DIM fileName AS STRING
READ fileName
WHILE LEN(fileName) > 0
DIM img AS LONG: img = _LOADIMAGE(fileName)
IF img < -1 THEN
PRINT fileName; ": ("; _WIDTH(img); "x"; _HEIGHT(img); ") pixels"
_FREEIMAGE img
ELSE
PRINT fileName; " is not a valid image file!"
END IF
READ fileName
WEND
SYSTEM
DATA good1.svg
DATA good2.svg
DATA bogus1.svg
DATA bogus2.svg
DATA

View file

@ -0,0 +1,4 @@
good1.svg: ( 493 x 800 ) pixels
good2.svg: ( 493 x 800 ) pixels
bogus1.svg is not a valid image file!
bogus2.svg is not a valid image file!