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
This commit is contained in:
Jinny You 2024-09-09 12:03:59 +09:00 committed by GitHub
parent 4b46337516
commit b59e833c65
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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));