lottie: correct the text alignment

When the Lottie Text Justify sets to center, text alignment should also be center.
This commit is contained in:
Jinny You 2024-04-25 19:37:50 +09:00 committed by Hermet Park
parent 535f2165a0
commit dd121af20d

View file

@ -546,9 +546,36 @@ static void _updateText(LottieGroup* parent, LottieObject** child, float frameNo
float spacing = text->spacing(frameNo) / scale; float spacing = text->spacing(frameNo) / scale;
Point cursor = {0.0f, 0.0f}; Point cursor = {0.0f, 0.0f};
auto scene = Scene::gen(); auto scene = Scene::gen();
int line = 0;
//text string //text string
while (*p != '\0') { while (true) {
//TODO: remove nested scenes.
//end of text, new line of the cursor position
if (*p == 13 || *p == 3 || *p == '\0') {
//text layout position
auto ascent = text->font->ascent * scale;
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
scene->translate(layout.x, layout.y);
scene->scale(scale);
parent->scene->push(std::move(scene));
if (*p == '\0') break;
++p;
//new text group, single scene for each line
scene = Scene::gen();
cursor.x = 0.0f;
cursor.y = ++line * (doc.height / scale);
}
//find the glyph //find the glyph
for (auto g = text->font->chars.begin(); g < text->font->chars.end(); ++g) { for (auto g = text->font->chars.begin(); g < text->font->chars.end(); ++g) {
auto glyph = *g; auto glyph = *g;
@ -577,33 +604,12 @@ static void _updateText(LottieGroup* parent, LottieObject** child, float frameNo
p += glyph->len; p += glyph->len;
//end of text, new line of the cursor position
if (*p == 13 || *p == 3) {
cursor.x = 0.0f;
cursor.y += (doc.height / scale);
++p;
//advance the cursor position horizontally //advance the cursor position horizontally
} else {
cursor.x += glyph->width + spacing; cursor.x += glyph->width + spacing;
}
break; break;
} }
} }
} }
//text layout position
auto ascent = text->font->ascent * scale;
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
scene->translate(layout.x, layout.y);
scene->scale(scale);
parent->scene->push(std::move(scene));
} }