feat: modules moved and engine moved to submodule

This commit is contained in:
Jan van der Weide 2025-04-12 18:40:44 +02:00
parent dfb5e645cd
commit c33d2130cc
5136 changed files with 225275 additions and 64485 deletions

View file

@ -405,6 +405,27 @@ static char* _idFromUrl(const char* url)
}
static size_t _srcFromUrl(const char* url, char*& src)
{
src = (char*)strchr(url, '(');
auto close = strchr(url, ')');
if (!src || !close || src >= close) return 0;
src = strchr(src, '\'');
if (!src || src >= close) return 0;
++src;
close = strchr(src, '\'');
if (!close || close == src) return 0;
--close;
while (src < close && *src == ' ') ++src;
while (src < close && *close == ' ') --close;
return close - src + 1;
}
static unsigned char _parseColor(const char* value, char** end)
{
float r;
@ -2005,6 +2026,42 @@ static SvgNode* _createImageNode(SvgLoaderData* loader, SvgNode* parent, const c
}
static char* _unquote(const char* str)
{
auto len = str ? strlen(str) : 0;
if (len >= 2 && str[0] == '\'' && str[len - 1] == '\'') return strDuplicate(str + 1, len - 2);
return strdup(str);
}
static bool _attrParseFontFace(void* data, const char* key, const char* value)
{
if (!key || !value) return false;
key = _skipSpace(key, nullptr);
value = _skipSpace(value, nullptr);
auto loader = (SvgLoaderData*)data;
auto& font = loader->fonts.last();
if (!strcmp(key, "font-family")) {
if (font.name) free(font.name);
font.name = _unquote(value);
} else if (!strcmp(key, "src")) {
font.srcLen = _srcFromUrl(value, font.src);
}
return true;
}
static void _createFontFace(SvgLoaderData* loader, const char* buf, unsigned bufLength, parseAttributes func)
{
loader->fonts.push(FontFace());
func(buf, bufLength, _attrParseFontFace, loader);
}
static SvgNode* _getDefsNode(SvgNode* node)
{
if (!node) return nullptr;
@ -3382,6 +3439,7 @@ static void _svgLoaderParserXmlOpen(SvgLoaderData* loader, const char* content,
if (node->type != SvgNodeType::Defs || !empty) {
loader->stack.push(node);
}
loader->latestGradient = nullptr;
} else if ((method = _findGraphicsFactory(tagName))) {
if (loader->stack.count > 0) parent = loader->stack.last();
else parent = loader->doc;
@ -3392,6 +3450,7 @@ static void _svgLoaderParserXmlOpen(SvgLoaderData* loader, const char* content,
loader->stack.push(defs);
loader->currentGraphicsNode = node;
}
loader->latestGradient = nullptr;
} else if ((gradientMethod = _findGradientFactory(tagName))) {
SvgStyleGradient* gradient;
gradient = gradientMethod(loader, attrs, attrsLength);
@ -3417,8 +3476,9 @@ static void _svgLoaderParserXmlOpen(SvgLoaderData* loader, const char* content,
loader->svgParse->flags = SvgStopStyleFlags::StopDefault;
simpleXmlParseAttributes(attrs, attrsLength, _attrParseStops, loader);
loader->latestGradient->stops.push(loader->svgParse->gradStop);
} else if (!isIgnoreUnsupportedLogElements(tagName)) {
TVGLOG("SVG", "Unsupported elements used [Elements: %s]", tagName);
} else {
loader->latestGradient = nullptr;
if (!isIgnoreUnsupportedLogElements(tagName)) TVGLOG("SVG", "Unsupported elements used [Elements: %s]", tagName);
}
}
@ -3452,6 +3512,8 @@ static void _svgLoaderParserXmlCssStyle(SvgLoaderData* loader, const char* conte
TVGLOG("SVG", "Unsupported elements used in the internal CSS style sheets [Elements: %s]", tag);
} else if (!strcmp(tag, "all")) {
if ((node = _createCssStyleNode(loader, loader->cssStyle, attrs, attrsLength, simpleXmlParseW3CAttribute))) node->id = _copyId(name);
} else if (!strcmp(tag, "@font-face")) { //css at-rule specifying font
_createFontFace(loader, attrs, attrsLength, simpleXmlParseW3CAttribute);
} else if (!isIgnoreUnsupportedLogElements(tag)) {
TVGLOG("SVG", "Unsupported elements used in the internal CSS style sheets [Elements: %s]", tag);
}
@ -3826,7 +3888,7 @@ SvgLoader::SvgLoader() : ImageLoader(FileType::Svg)
SvgLoader::~SvgLoader()
{
this->done();
done();
clear();
}

View file

@ -555,6 +555,14 @@ struct SvgNodeIdPair
char *id;
};
struct FontFace
{
char* name = nullptr;
char* src = nullptr;
size_t srcLen = 0;
char* decoded = nullptr;
};
enum class OpenedTagType : uint8_t
{
Other = 0,
@ -574,6 +582,7 @@ struct SvgLoaderData
Array<SvgNodeIdPair> cloneNodes;
Array<SvgNodeIdPair> nodesToStyle;
Array<char*> images; //embedded images
Array<FontFace> fonts;
int level = 0;
bool result = false;
OpenedTagType openedTag = OpenedTagType::Other;

View file

@ -573,8 +573,6 @@ static unique_ptr<Picture> _imageBuildHelper(SvgLoaderData& loaderData, SvgNode*
if (!node->node.image.href || !strlen(node->node.image.href)) return nullptr;
auto picture = Picture::gen();
TaskScheduler::async(false); //force to load a picture on the same thread
const char* href = node->node.image.href;
if (!strncmp(href, "data:", sizeof("data:") - 1)) {
href += sizeof("data:") - 1;
@ -586,14 +584,12 @@ static unique_ptr<Picture> _imageBuildHelper(SvgLoaderData& loaderData, SvgNode*
auto size = b64Decode(href, strlen(href), &decoded);
if (picture->load(decoded, size, mimetype, false) != Result::Success) {
free(decoded);
TaskScheduler::async(true);
return nullptr;
}
} else {
auto size = svgUtilURLDecode(href, &decoded);
if (picture->load(decoded, size, mimetype, false) != Result::Success) {
free(decoded);
TaskScheduler::async(true);
return nullptr;
}
}
@ -605,7 +601,6 @@ static unique_ptr<Picture> _imageBuildHelper(SvgLoaderData& loaderData, SvgNode*
const char *dot = strrchr(href, '.');
if (dot && !strcmp(dot, ".svg")) {
TVGLOG("SVG", "Embedded svg file is disabled.");
TaskScheduler::async(true);
return nullptr;
}
string imagePath = href;
@ -614,13 +609,10 @@ static unique_ptr<Picture> _imageBuildHelper(SvgLoaderData& loaderData, SvgNode*
imagePath = svgPath.substr(0, (last == string::npos ? 0 : last + 1)) + imagePath;
}
if (picture->load(imagePath) != Result::Success) {
TaskScheduler::async(true);
return nullptr;
}
}
TaskScheduler::async(true);
float w, h;
Matrix m = {1, 0, 0, 0, 1, 0, 0, 0, 1};
if (picture->size(&w, &h) == Result::Success && w > 0 && h > 0) {
@ -900,6 +892,41 @@ static void _updateInvalidViewSize(const Scene* scene, Box& vBox, float& w, floa
if (!validHeight) h *= vBox.h;
}
static void _loadFonts(Array<FontFace>& fonts)
{
if (fonts.empty()) return;
static constexpr struct {
const char* prefix;
size_t len;
} prefixes[] = {
{"data:font/ttf;base64,", sizeof("data:font/ttf;base64,") - 1},
{"data:application/font-ttf;base64,", sizeof("data:application/font-ttf;base64,") - 1}
};
for (uint32_t i = 0; i < fonts.count; ++i) {
auto p = &fonts[i];
if (!p->name) continue;
size_t shift = 0;
for (const auto& prefix : prefixes) {
if (p->srcLen > prefix.len && !memcmp(p->src, prefix.prefix, prefix.len)) {
shift = prefix.len;
break;
}
}
if (shift == 0) {
TVGLOG("SVG", "The embedded font \"%s\" data not loaded properly.", p->name);
continue;
}
auto size = b64Decode(p->src + shift, p->srcLen - shift, &p->decoded);
if (Text::load(p->name, p->decoded, size) != Result::Success) TVGERR("SVG", "Error while loading the ttf font named \"%s\".", p->name);
}
}
/************************************************************************/
/* External Class Implementation */
/************************************************************************/
@ -910,6 +937,8 @@ Scene* svgSceneBuild(SvgLoaderData& loaderData, Box vBox, float w, float h, Aspe
if (!loaderData.doc || (loaderData.doc->type != SvgNodeType::Doc)) return nullptr;
_loadFonts(loaderData.fonts);
auto docNode = _sceneBuildHelper(loaderData, loaderData.doc, vBox, svgPath, false, 0);
if (!(viewFlag & SvgViewFlag::Viewbox)) _updateInvalidViewSize(docNode.get(), vBox, w, h, viewFlag);

View file

@ -483,6 +483,14 @@ bool simpleXmlParseW3CAttribute(const char* buf, unsigned bufLength, simpleXMLAt
do {
char* sep = (char*)strchr(buf, ':');
next = (char*)strchr(buf, ';');
if (auto src = strstr(buf, "src")) {//src tag from css font-face contains extra semicolon
if (src < sep) {
if (next + 1 < end) next = (char*)strchr(next + 1, ';');
else return true;
}
}
if (sep >= end) {
next = nullptr;
sep = nullptr;