lottie: revise the text justify logic

removed the duplicate logic,
improved for neat and easy maintainance
This commit is contained in:
Hermet Park 2025-03-04 15:03:38 +09:00 committed by Hermet Park
parent 716311f44f
commit 3c836b1b2a
3 changed files with 11 additions and 13 deletions

View file

@ -929,13 +929,7 @@ static void _fontText(LottieText* text, Scene* scene, float frameNo, LottieExpre
float width;
txt->bounds(nullptr, nullptr, &width, nullptr, false);
float cursorX = 0.0f;
if (doc.justify == 1) {
cursorX = width * -1;
} else if (doc.justify == 2) {
cursorX = width * -0.5f;
}
auto cursorX = width * doc.justify;
txt->translate(cursorX, -doc.size * 100.0f);
scene->push(txt);
}
@ -977,9 +971,8 @@ void LottieBuilder::updateText(LottieLayer* layer, float frameNo)
if (ascent > doc.bbox.size.y) ascent = doc.bbox.size.y;
Point layout = {doc.bbox.pos.x, doc.bbox.pos.y + ascent - doc.shift};
//adjust the layout
if (doc.justify == 1) layout.x += doc.bbox.size.x - (cursor.x * scale); //right aligned
else if (doc.justify == 2) layout.x += (doc.bbox.size.x * 0.5f) - (cursor.x * 0.5f * scale); //center aligned
//horizontal alignment
layout.x += -1.0f * doc.bbox.size.x * doc.justify + (cursor.x * scale) * doc.justify;
//new text group, single scene based on text-grouping
scene->push(textGroup);

View file

@ -67,8 +67,8 @@ struct TextDocument
char* name = nullptr;
float size;
float tracking = 0.0f;
uint8_t justify = 0;
uint8_t caps = 0; //0: Regular, 1: AllCaps, 2: SmallCaps
float justify = 0.0f; //horizontal alignment
uint8_t caps = 0; //0: Regular, 1: AllCaps, 2: SmallCaps
};

View file

@ -126,7 +126,12 @@ bool LottieParser::getValue(TextDocument& doc)
if (KEY_AS("s")) doc.size = getFloat() * 0.01f;
else if (KEY_AS("f")) doc.name = getStringCopy();
else if (KEY_AS("t")) doc.text = getStringCopy();
else if (KEY_AS("j")) doc.justify = getInt();
else if (KEY_AS("j"))
{
auto val = getInt();
if (val == 1) doc.justify = -1.0f; //right align
else if (val == 2) doc.justify = -0.5f; //center align
}
else if (KEY_AS("ca")) doc.caps = getInt();
else if (KEY_AS("tr")) doc.tracking = getFloat() * 0.1f;
else if (KEY_AS("lh")) doc.height = getFloat();