diff --git a/src/loaders/lottie/tvgLottieModel.h b/src/loaders/lottie/tvgLottieModel.h index 6e821330..54fa8d42 100644 --- a/src/loaders/lottie/tvgLottieModel.h +++ b/src/loaders/lottie/tvgLottieModel.h @@ -241,6 +241,8 @@ struct LottieGlyph Array children; //glyph shapes. float width; char* code; + char* family = nullptr; + char* style = nullptr; uint16_t size; uint8_t len; diff --git a/src/loaders/lottie/tvgLottieParser.cpp b/src/loaders/lottie/tvgLottieParser.cpp index 29a7576b..0aa0be12 100644 --- a/src/loaders/lottie/tvgLottieParser.cpp +++ b/src/loaders/lottie/tvgLottieParser.cpp @@ -956,26 +956,19 @@ void LottieParser::parseAssets() } -void LottieParser::parseChars() +void LottieParser::parseChars(Array& glyphes) { - if (comp->fonts.count == 0) { - TVGERR("LOTTIE", "No font data?"); - return; - } - enterArray(); while (nextArrayValue()) { enterObject(); //a new glyph auto glyph = new LottieGlyph; - char* style = nullptr; - char* family = nullptr; while (auto key = nextObjectKey()) { if (!strcmp("ch", key)) glyph->code = getStringCopy(); else if (!strcmp("size", key)) glyph->size = static_cast(getFloat()); - else if (!strcmp("style", key)) style = const_cast(getString()); + else if (!strcmp("style", key)) glyph->style = getStringCopy(); else if (!strcmp("w", key)) glyph->width = getFloat(); - else if (!strcmp("fFamily", key)) family = const_cast(getString()); + else if (!strcmp("fFamily", key)) glyph->family = getStringCopy(); else if (!strcmp("data", key)) { //glyph shapes enterObject(); @@ -984,13 +977,8 @@ void LottieParser::parseChars() } } else skip(key); } - //aggregate the font characters - for (uint32_t i = 0; i < comp->fonts.count; ++i) { - auto& font = comp->fonts[i]; - if (!strcmp(font->family, family) && !strcmp(font->style, style)) font->chars.push(glyph); - else TVGERR("LOTTIE", "No font data?"); - } glyph->prepare(); + glyphes.push(glyph); } } @@ -1240,6 +1228,24 @@ LottieLayer* LottieParser::parseLayers() } +void LottieParser::postProcess(Array& glyphes) +{ + //aggregate font characters + for (uint32_t g = 0; g < glyphes.count; ++g) { + auto glyph = glyphes[g]; + for (uint32_t i = 0; i < comp->fonts.count; ++i) { + auto& font = comp->fonts[i]; + if (!strcmp(font->family, glyph->family) && !strcmp(font->style, glyph->style)) { + font->chars.push(glyph); + free(glyph->family); + free(glyph->style); + break; + } + } + } +} + + /************************************************************************/ /* External Class Implementation */ /************************************************************************/ @@ -1255,6 +1261,8 @@ bool LottieParser::parse() comp = new LottieComposition; if (!comp) return false; + Array glyphes; + //assign parsing context LottieParser::Context context; this->context = &context; @@ -1270,7 +1278,7 @@ bool LottieParser::parse() else if (!strcmp(key, "assets")) parseAssets(); else if (!strcmp(key, "layers")) comp->root = parseLayers(); else if (!strcmp(key, "fonts")) parseFonts(); - else if (!strcmp(key, "chars")) parseChars(); + else if (!strcmp(key, "chars")) parseChars(glyphes); else skip(key); } @@ -1278,5 +1286,8 @@ bool LottieParser::parse() comp->root->inFrame = comp->startFrame; comp->root->outFrame = comp->endFrame; + + postProcess(glyphes); + return true; } \ No newline at end of file diff --git a/src/loaders/lottie/tvgLottieParser.h b/src/loaders/lottie/tvgLottieParser.h index 2583c6a1..2986b48e 100644 --- a/src/loaders/lottie/tvgLottieParser.h +++ b/src/loaders/lottie/tvgLottieParser.h @@ -99,7 +99,8 @@ private: void parseTextRange(LottieText* text); void parseAssets(); void parseFonts(); - void parseChars(); + void parseChars(Array& glyphes); + void postProcess(Array& glyphes); //Current parsing context struct Context {