common: neat & clean code++

This commit is contained in:
Hermet Park 2025-01-17 00:40:16 +09:00 committed by Hermet Park
parent 61028747c6
commit dfa05a5c43
5 changed files with 34 additions and 42 deletions

View file

@ -114,38 +114,38 @@ static inline void identity(Matrix* m)
}
static inline void scale(Matrix* m, float sx, float sy)
static inline void scale(Matrix* m, const Point& p)
{
m->e11 *= sx;
m->e22 *= sy;
m->e11 *= p.x;
m->e22 *= p.y;
}
static inline void scaleR(Matrix* m, float x, float y)
static inline void scaleR(Matrix* m, const Point& p)
{
if (x != 1.0f) {
m->e11 *= x;
m->e21 *= x;
if (p.x != 1.0f) {
m->e11 *= p.x;
m->e21 *= p.x;
}
if (y != 1.0f) {
m->e22 *= y;
m->e12 *= y;
if (p.y != 1.0f) {
m->e22 *= p.y;
m->e12 *= p.y;
}
}
static inline void translate(Matrix* m, float x, float y)
static inline void translate(Matrix* m, const Point& p)
{
m->e13 += x;
m->e23 += y;
m->e13 += p.x;
m->e23 += p.y;
}
static inline void translateR(Matrix* m, float x, float y)
static inline void translateR(Matrix* m, const Point& p)
{
if (x == 0.0f && y == 0.0f) return;
m->e13 += (x * m->e11 + y * m->e12);
m->e23 += (x * m->e21 + y * m->e22);
if (p.x == 0.0f && p.y == 0.0f) return;
m->e13 += (p.x * m->e11 + p.y * m->e12);
m->e23 += (p.x * m->e21 + p.y * m->e22);
}

View file

@ -110,12 +110,8 @@ static bool _updateTransform(LottieTransform* transform, float frameNo, bool aut
return false;
}
if (transform->coords) {
translate(&matrix, transform->coords->x(frameNo, exps), transform->coords->y(frameNo, exps));
} else {
auto position = transform->position(frameNo, exps);
translate(&matrix, position.x, position.y);
}
if (transform->coords) translate(&matrix, {transform->coords->x(frameNo, exps), transform->coords->y(frameNo, exps)});
else translate(&matrix, transform->position(frameNo, exps));
auto angle = 0.0f;
if (autoOrient) angle = transform->position.angle(frameNo);
@ -133,11 +129,10 @@ static bool _updateTransform(LottieTransform* transform, float frameNo, bool aut
}
auto scale = transform->scale(frameNo, exps);
scaleR(&matrix, scale.x * 0.01f, scale.y * 0.01f);
scaleR(&matrix, scale * 0.01f);
//Lottie specific anchor transform.
auto anchor = transform->anchor(frameNo, exps);
translateR(&matrix, -anchor.x, -anchor.y);
translateR(&matrix, -transform->anchor(frameNo, exps));
//invisible just in case.
if (scale.x == 0.0f || scale.y == 0.0f) opacity = 0;
@ -345,10 +340,10 @@ static void _repeat(LottieGroup* parent, Shape* path, RenderContext* ctx)
Matrix m;
identity(&m);
translate(&m, repeater->position.x * multiplier + repeater->anchor.x, repeater->position.y * multiplier + repeater->anchor.y);
scale(&m, powf(repeater->scale.x * 0.01f, multiplier), powf(repeater->scale.y * 0.01f, multiplier));
translate(&m, repeater->position * multiplier + repeater->anchor);
scale(&m, {powf(repeater->scale.x * 0.01f, multiplier), powf(repeater->scale.y * 0.01f, multiplier)});
rotate(&m, repeater->rotation * multiplier);
translateR(&m, -repeater->anchor.x, -repeater->anchor.y);
translateR(&m, -repeater->anchor);
m = repeater->transform * m;
Matrix inv;
@ -791,8 +786,7 @@ void LottieBuilder::updatePolystar(LottieGroup* parent, LottieObject** child, fl
//Optimize: Can we skip the individual coords transform?
Matrix matrix;
identity(&matrix);
auto position = star->position(frameNo, exps);
translate(&matrix, position.x, position.y);
translate(&matrix, star->position(frameNo, exps));
rotate(&matrix, star->rotation(frameNo, exps));
if (ctx->transform) matrix = *ctx->transform * matrix;
@ -1162,7 +1156,7 @@ void LottieBuilder::updateText(LottieLayer* layer, float frameNo)
// TextGroup transformation is performed once
if (textGroup->paints().size() == 0 && needGroup) {
identity(&textGroupMatrix);
translate(&textGroupMatrix, cursor.x, cursor.y);
translate(&textGroupMatrix, cursor);
auto alignment = text->alignOption.anchor(frameNo);
@ -1172,20 +1166,18 @@ void LottieBuilder::updateText(LottieLayer* layer, float frameNo)
rotate(&textGroupMatrix, rotation);
auto pivotX = alignment.x * -1;
auto pivotY = alignment.y * -1;
//center pivoting
textGroupMatrix.e13 += (pivotX * textGroupMatrix.e11 + pivotX * textGroupMatrix.e12);
textGroupMatrix.e23 += (pivotY * textGroupMatrix.e21 + pivotY * textGroupMatrix.e22);
auto pivot = alignment * -1;
textGroupMatrix.e13 += (pivot.x * textGroupMatrix.e11 + pivot.x * textGroupMatrix.e12);
textGroupMatrix.e23 += (pivot.y * textGroupMatrix.e21 + pivot.y * 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);
tvg::scale(&matrix, scaling.x * capScale, scaling.y * capScale);
translate(&matrix, (translation / scale + cursor) - Point{textGroupMatrix.e13, textGroupMatrix.e23});
tvg::scale(&matrix, scaling * capScale);
shape->transform(matrix);
}

View file

@ -771,7 +771,7 @@ static Paint* _textBuildHelper(SvgLoaderData& loaderData, const SvgNode* node, c
if (node->transform) textTransform = *node->transform;
else textTransform = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};
translateR(&textTransform, node->node.text.x, node->node.text.y - textNode->fontSize);
translateR(&textTransform, {node->node.text.x, node->node.text.y - textNode->fontSize});
text->transform(textTransform);
//TODO: handle def values of font and size as used in a system?

View file

@ -56,7 +56,7 @@ void GlRenderPass::getMatrix(float *dst, const Matrix &matrix) const
Matrix postMatrix{};
identity(&postMatrix);
translate(&postMatrix, -vp.x, -vp.y);
translate(&postMatrix, {(float)-vp.x, (float)-vp.y});
auto m = postMatrix * matrix;

View file

@ -72,7 +72,7 @@ namespace tvg
m.e31 = 0.0f;
m.e32 = 0.0f;
m.e33 = 1.0f;
tvg::scale(&m, scale, scale);
tvg::scale(&m, {scale, scale});
tvg::rotate(&m, degree);
}
} tr;