mirror of
https://github.com/QB64-Phoenix-Edition/QB64pe.git
synced 2024-07-06 00:00:22 +00:00
Check for SVG start tag and add tests
This commit is contained in:
parent
753ecb9609
commit
bbfd228597
|
@ -202,20 +202,16 @@ static uint32_t *image_svg_load_from_file(const char *fileName, int32_t *xOut, i
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
svgString[size] = '\0'; // must be null terminated
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
// Bail if we have binary data
|
// Check if it has a valid SVG start tag
|
||||||
for (size_t i = 0; i < size; i++) {
|
if (!strstr(svgString, "<svg")) {
|
||||||
uint8_t c = svgString[i];
|
free(svgString);
|
||||||
if (c < 32 && c != '\0' && c != '\t' && c != '\r' && c != '\n') {
|
return nullptr;
|
||||||
free(svgString);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
svgString[size] = '\0'; // must be null terminated
|
|
||||||
|
|
||||||
auto image = nsvgParse(svgString, "px", 96.0f); // important note: changes the string
|
auto image = nsvgParse(svgString, "px", 96.0f); // important note: changes the string
|
||||||
if (!image) {
|
if (!image) {
|
||||||
free(svgString);
|
free(svgString);
|
||||||
|
@ -242,17 +238,15 @@ static uint32_t *image_svg_load_from_memory(const uint8_t *buffer, size_t size,
|
||||||
if (!svgString)
|
if (!svgString)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
// Bail if we have binary data. We'll also copy the data while doing the check to avoid another pass
|
memcpy(svgString, buffer, size);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
svgString[size] = '\0'; // must be null terminated
|
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
|
auto image = nsvgParse(svgString, "px", 96.0f); // important note: changes the string
|
||||||
if (!image) {
|
if (!image) {
|
||||||
free(svgString);
|
free(svgString);
|
||||||
|
|
|
@ -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__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,
|
static void nsvg__flattenCubicBez(NSVGrasterizer* r,
|
||||||
float x1, float y1, float x2, float y2,
|
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);
|
// STBTT_assert(e->y0 <= start_point);
|
||||||
// round dx down to avoid going too far
|
// round dx down to avoid going too far
|
||||||
if (dxdy < 0)
|
if (dxdy < 0)
|
||||||
z->dx = (int)(-floorf(NSVG__FIX * -dxdy));
|
z->dx = (int)(-nsvg__roundf(NSVG__FIX * -dxdy));
|
||||||
else
|
else
|
||||||
z->dx = (int)floorf(NSVG__FIX * dxdy);
|
z->dx = (int)nsvg__roundf(NSVG__FIX * dxdy);
|
||||||
z->x = (int)floorf(NSVG__FIX * (e->x0 + dxdy * (startPoint - e->y0)));
|
z->x = (int)nsvg__roundf(NSVG__FIX * (e->x0 + dxdy * (startPoint - e->y0)));
|
||||||
// z->x -= off_x * FIX;
|
// z->x -= off_x * FIX;
|
||||||
z->ey = e->y1;
|
z->ey = e->y1;
|
||||||
z->next = 0;
|
z->next = 0;
|
||||||
|
@ -1282,7 +1283,7 @@ static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opac
|
||||||
if (grad->nstops == 0) {
|
if (grad->nstops == 0) {
|
||||||
for (i = 0; i < 256; i++)
|
for (i = 0; i < 256; i++)
|
||||||
cache->colors[i] = 0;
|
cache->colors[i] = 0;
|
||||||
} if (grad->nstops == 1) {
|
} else if (grad->nstops == 1) {
|
||||||
for (i = 0; i < 256; i++)
|
for (i = 0; i < 256; i++)
|
||||||
cache->colors[i] = nsvg__applyOpacity(grad->stops[i].color, opacity);
|
cache->colors[i] = nsvg__applyOpacity(grad->stops[i].color, opacity);
|
||||||
} else {
|
} else {
|
||||||
|
|
1
tests/compile_tests/image/bogus1.svg
Normal file
1
tests/compile_tests/image/bogus1.svg
Normal 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.
|
BIN
tests/compile_tests/image/bogus2.svg
Normal file
BIN
tests/compile_tests/image/bogus2.svg
Normal file
Binary file not shown.
730
tests/compile_tests/image/good1.svg
Normal file
730
tests/compile_tests/image/good1.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 94 KiB |
729
tests/compile_tests/image/good2.svg
Normal file
729
tests/compile_tests/image/good2.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 94 KiB |
26
tests/compile_tests/image/svg_test.bas
Normal file
26
tests/compile_tests/image/svg_test.bas
Normal 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
|
4
tests/compile_tests/image/svg_test.output
Normal file
4
tests/compile_tests/image/svg_test.output
Normal 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!
|
Loading…
Reference in a new issue