lottie: text range selector++

Change in the algorithm for selecting characters included
in the range selector. This is the first step towards adding
support for maxAmount, smoothness, and easing.

@Issue: https://github.com/thorvg/thorvg/issues/2178
This commit is contained in:
Mira Grudzinska 2024-10-29 11:42:08 +07:00 committed by Hermet Park
parent 1528ae3729
commit 4ee73be95b
3 changed files with 26 additions and 17 deletions

View file

@ -1095,15 +1095,13 @@ void LottieBuilder::updateText(LottieLayer* layer, float frameNo)
//text range process
for (auto s = text->ranges.begin(); s < text->ranges.end(); ++s) {
float start, end;
(*s)->range(frameNo, float(totalChars), start, end);
auto basedIdx = idx;
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 f = (*s)->factor(frameNo, float(totalChars), (float)basedIdx);
if (tvg::zero(f)) continue;
needGroup = true;
translation = translation + (*s)->style.position(frameNo);
@ -1145,13 +1143,13 @@ void LottieBuilder::updateText(LottieLayer* layer, float frameNo)
//center pivoting
textGroupMatrix.e13 += (pivotX * textGroupMatrix.e11 + pivotX * textGroupMatrix.e12);
textGroupMatrix.e23 += (pivotY * textGroupMatrix.e21 + pivotY * textGroupMatrix.e22);
textGroup->transform(textGroupMatrix);
}
auto& matrix = shape->transform();
identity(&matrix);
translate(&matrix, (translation.x / scale + cursor.x) - textGroupMatrix.e13, (translation.y / scale + cursor.y) - textGroupMatrix.e23);
translate(&matrix, translation.x / scale + cursor.x - textGroupMatrix.e13, translation.y / scale + cursor.y - textGroupMatrix.e23);
tvg::scale(&matrix, scaling.x, scaling.y);
shape->transform(matrix);
}

View file

@ -115,21 +115,32 @@ void LottieSlot::assign(LottieObject* target)
}
void LottieTextRange::range(float frameNo, float totalLen, float& start, float& end)
float LottieTextRange::factor(float frameNo, float totalLen, float idx)
{
auto offset = this->offset(frameNo);
auto start = this->start(frameNo) + offset;
auto end = this->end(frameNo) + offset;
if (random > 0) {
auto range = end - start;
auto len = (rangeUnit == Unit::Percent) ? 100.0f : totalLen;
start = static_cast<float>(random % int(len - range));
end = start + range;
}
auto divisor = (rangeUnit == Unit::Percent) ? (100.0f / totalLen) : 1.0f;
auto offset = this->offset(frameNo) / divisor;
start = nearbyintf(this->start(frameNo) / divisor) + offset;
end = nearbyintf(this->end(frameNo) / divisor) + offset;
start /= divisor;
end /= divisor;
if (start > end) std::swap(start, end);
auto f = 0.0f;
if (random == 0) return;
if (idx >= std::floor(start)) {
auto diff = idx - start;
f = diff < 0.0f ? std::min(end, 1.0f) + diff : end - idx;
clamp(f, 0.0f, 1.0f);
}
auto range = end - start;
auto len = (rangeUnit == Unit::Percent) ? 100.0f : totalLen;
start = static_cast<float>(random % int(len - range));
end = start + range;
return f;
}

View file

@ -228,7 +228,7 @@ struct LottieTextRange
uint8_t random = 0;
bool expressible = false;
void range(float frameNo, float totalLen, float& start, float& end);
float factor(float frameNo, float totalLen, float idx);
};