REVIEWED: LoadImageSvg()

This commit is contained in:
Ray 2023-09-02 19:20:56 +02:00
parent d6f3891009
commit 67a693fc5b

View file

@ -324,24 +324,35 @@ Image LoadImageSvg(const char *fileNameOrString, int width, int height)
Image image = { 0 }; Image image = { 0 };
bool isSvgStringValid = false; bool isSvgStringValid = false;
// TODO: Validate fileName or string // Validate fileName or string
if (fileNameOrString != NULL) if (fileNameOrString != NULL)
{ {
int dataSize = 0;
unsigned char *fileData = NULL;
if (FileExists(fileNameOrString)) if (FileExists(fileNameOrString))
{ {
int dataSize = 0; fileData = LoadFileData(fileNameOrString, &dataSize);
unsigned char *fileData = LoadFileData(fileNameOrString, &dataSize);
isSvgStringValid = true; isSvgStringValid = true;
} }
else else
{ {
// TODO: Validate it's a valid SVG string // Validate fileData as valid SVG string data
//<svg xmlns="http://www.w3.org/2000/svg" width="2500" height="2484" viewBox="0 0 192.756 191.488">
if ((fileNameOrString != NULL) &&
(fileNameOrString[0] == '<') &&
(fileNameOrString[1] == 's') &&
(fileNameOrString[2] == 'v') &&
(fileNameOrString[3] == 'g'))
{
fileData = fileNameOrString;
isSvgStringValid = true; isSvgStringValid = true;
} }
}
if (isSvgStringValid != NULL) if (isSvgStringValid)
{ {
struct NSVGimage *svgImage = nsvgParse(fileNameOrString, "px", 96.0f); struct NSVGimage *svgImage = nsvgParse(fileData, "px", 96.0f);
unsigned char *img = RL_MALLOC(width*height*4); unsigned char *img = RL_MALLOC(width*height*4);
@ -372,6 +383,8 @@ Image LoadImageSvg(const char *fileNameOrString, int width, int height)
// Free used memory // Free used memory
nsvgDelete(svgImage); nsvgDelete(svgImage);
} }
if (isSvgStringValid && (fileData != fileNameOrString)) UnloadFileData(fileData);
} }
return image; return image;
@ -499,6 +512,8 @@ Image LoadImageFromMemory(const char *fileType, const unsigned char *fileData, i
#endif #endif
#if defined(SUPPORT_FILEFORMAT_QOI) #if defined(SUPPORT_FILEFORMAT_QOI)
else if ((strcmp(fileType, ".qoi") == 0) || (strcmp(fileType, ".QOI") == 0)) else if ((strcmp(fileType, ".qoi") == 0) || (strcmp(fileType, ".QOI") == 0))
{
if (fileData != NULL)
{ {
qoi_desc desc = { 0 }; qoi_desc desc = { 0 };
image.data = qoi_decode(fileData, dataSize, &desc, 4); image.data = qoi_decode(fileData, dataSize, &desc, 4);
@ -507,12 +522,19 @@ Image LoadImageFromMemory(const char *fileType, const unsigned char *fileData, i
image.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8; image.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
image.mipmaps = 1; image.mipmaps = 1;
} }
}
#endif #endif
#if defined(SUPPORT_FILEFORMAT_SVG) #if defined(SUPPORT_FILEFORMAT_SVG)
else if ((strcmp(fileType, ".svg") == 0) || (strcmp(fileType, ".SVG") == 0)) else if ((strcmp(fileType, ".svg") == 0) || (strcmp(fileType, ".SVG") == 0))
{ {
// TODO: Validate fileData as valid SVG string data // Validate fileData as valid SVG string data
//<svg xmlns="http://www.w3.org/2000/svg" width="2500" height="2484" viewBox="0 0 192.756 191.488">
if ((fileData != NULL) &&
(fileData[0] == '<') &&
(fileData[1] == 's') &&
(fileData[2] == 'v') &&
(fileData[3] == 'g'))
{
struct NSVGimage *svgImage = nsvgParse(fileData, "px", 96.0f); struct NSVGimage *svgImage = nsvgParse(fileData, "px", 96.0f);
unsigned char *img = RL_MALLOC(svgImage->width*svgImage->height*4); unsigned char *img = RL_MALLOC(svgImage->width*svgImage->height*4);
@ -529,6 +551,7 @@ Image LoadImageFromMemory(const char *fileType, const unsigned char *fileData, i
nsvgDelete(svgImage); nsvgDelete(svgImage);
} }
}
#endif #endif
#if defined(SUPPORT_FILEFORMAT_DDS) #if defined(SUPPORT_FILEFORMAT_DDS)
else if ((strcmp(fileType, ".dds") == 0) || (strcmp(fileType, ".DDS") == 0)) else if ((strcmp(fileType, ".dds") == 0) || (strcmp(fileType, ".DDS") == 0))