From b59e833c65e6fb3ae78f29c086dc7e39dcc84d8c Mon Sep 17 00:00:00 2001 From: Jinny You Date: Mon, 9 Sep 2024 12:03:59 +0900 Subject: [PATCH] lottie/text: Support Text Based Added support for Text Based of Text Range Selector, by processing further properties: 2. Character Excluding Spaces 3. Words 4. Lines see: https://github.com/thorvg/thorvg/issues/2178 --- src/loaders/lottie/tvgLottieBuilder.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/loaders/lottie/tvgLottieBuilder.cpp b/src/loaders/lottie/tvgLottieBuilder.cpp index e3b0ffbc..5a7b5156 100644 --- a/src/loaders/lottie/tvgLottieBuilder.cpp +++ b/src/loaders/lottie/tvgLottieBuilder.cpp @@ -1002,6 +1002,7 @@ void LottieBuilder::updateText(LottieLayer* layer, float frameNo) Point cursor = {0.0f, 0.0f}; auto scene = Scene::gen(); int line = 0; + int space = 0; //text string int idx = 0; @@ -1033,6 +1034,9 @@ void LottieBuilder::updateText(LottieLayer* layer, float frameNo) cursor.y = ++line * (doc.height / scale); continue; } + + if (*p == ' ') ++space; + //find the glyph bool found = false; for (auto g = text->font->chars.begin(); g < text->font->chars.end(); ++g) { @@ -1060,6 +1064,7 @@ void LottieBuilder::updateText(LottieLayer* layer, float frameNo) //text range process for (auto s = text->ranges.begin(); s < text->ranges.end(); ++s) { + auto basedIdx = idx; float divisor = (*s)->rangeUnit == LottieTextRange::Unit::Percent ? (100.0f / totalChars) : 1; auto offset = (*s)->offset(frameNo) / divisor; auto start = nearbyintf((*s)->start(frameNo) / divisor) + offset; @@ -1067,7 +1072,11 @@ void LottieBuilder::updateText(LottieLayer* layer, float frameNo) if (start > end) std::swap(start, end); - if (idx < start || idx >= end) continue; + if ((*s)->based == LottieTextRange::Based::CharsExcludingSpaces) basedIdx = idx - space; + else if ((*s)->based == LottieTextRange::Based::Words) basedIdx = line + space; + else if ((*s)->based == LottieTextRange::Based::Lines) basedIdx = line; + + if (basedIdx < start || basedIdx >= end) continue; auto matrix = shape->transform(); shape->opacity((*s)->style.opacity(frameNo));