mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-09 06:04:03 +00:00
common: neat & clean code++
This commit is contained in:
parent
61028747c6
commit
dfa05a5c43
5 changed files with 34 additions and 42 deletions
|
@ -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->e11 *= p.x;
|
||||||
m->e22 *= sy;
|
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) {
|
if (p.x != 1.0f) {
|
||||||
m->e11 *= x;
|
m->e11 *= p.x;
|
||||||
m->e21 *= x;
|
m->e21 *= p.x;
|
||||||
}
|
}
|
||||||
if (y != 1.0f) {
|
if (p.y != 1.0f) {
|
||||||
m->e22 *= y;
|
m->e22 *= p.y;
|
||||||
m->e12 *= 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->e13 += p.x;
|
||||||
m->e23 += y;
|
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;
|
if (p.x == 0.0f && p.y == 0.0f) return;
|
||||||
m->e13 += (x * m->e11 + y * m->e12);
|
m->e13 += (p.x * m->e11 + p.y * m->e12);
|
||||||
m->e23 += (x * m->e21 + y * m->e22);
|
m->e23 += (p.x * m->e21 + p.y * m->e22);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -110,12 +110,8 @@ static bool _updateTransform(LottieTransform* transform, float frameNo, bool aut
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (transform->coords) {
|
if (transform->coords) translate(&matrix, {transform->coords->x(frameNo, exps), transform->coords->y(frameNo, exps)});
|
||||||
translate(&matrix, transform->coords->x(frameNo, exps), transform->coords->y(frameNo, exps));
|
else translate(&matrix, transform->position(frameNo, exps));
|
||||||
} else {
|
|
||||||
auto position = transform->position(frameNo, exps);
|
|
||||||
translate(&matrix, position.x, position.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto angle = 0.0f;
|
auto angle = 0.0f;
|
||||||
if (autoOrient) angle = transform->position.angle(frameNo);
|
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);
|
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.
|
//Lottie specific anchor transform.
|
||||||
auto anchor = transform->anchor(frameNo, exps);
|
translateR(&matrix, -transform->anchor(frameNo, exps));
|
||||||
translateR(&matrix, -anchor.x, -anchor.y);
|
|
||||||
|
|
||||||
//invisible just in case.
|
//invisible just in case.
|
||||||
if (scale.x == 0.0f || scale.y == 0.0f) opacity = 0;
|
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;
|
Matrix m;
|
||||||
identity(&m);
|
identity(&m);
|
||||||
translate(&m, repeater->position.x * multiplier + repeater->anchor.x, repeater->position.y * multiplier + repeater->anchor.y);
|
translate(&m, repeater->position * multiplier + repeater->anchor);
|
||||||
scale(&m, powf(repeater->scale.x * 0.01f, multiplier), powf(repeater->scale.y * 0.01f, multiplier));
|
scale(&m, {powf(repeater->scale.x * 0.01f, multiplier), powf(repeater->scale.y * 0.01f, multiplier)});
|
||||||
rotate(&m, repeater->rotation * multiplier);
|
rotate(&m, repeater->rotation * multiplier);
|
||||||
translateR(&m, -repeater->anchor.x, -repeater->anchor.y);
|
translateR(&m, -repeater->anchor);
|
||||||
m = repeater->transform * m;
|
m = repeater->transform * m;
|
||||||
|
|
||||||
Matrix inv;
|
Matrix inv;
|
||||||
|
@ -791,8 +786,7 @@ void LottieBuilder::updatePolystar(LottieGroup* parent, LottieObject** child, fl
|
||||||
//Optimize: Can we skip the individual coords transform?
|
//Optimize: Can we skip the individual coords transform?
|
||||||
Matrix matrix;
|
Matrix matrix;
|
||||||
identity(&matrix);
|
identity(&matrix);
|
||||||
auto position = star->position(frameNo, exps);
|
translate(&matrix, star->position(frameNo, exps));
|
||||||
translate(&matrix, position.x, position.y);
|
|
||||||
rotate(&matrix, star->rotation(frameNo, exps));
|
rotate(&matrix, star->rotation(frameNo, exps));
|
||||||
|
|
||||||
if (ctx->transform) matrix = *ctx->transform * matrix;
|
if (ctx->transform) matrix = *ctx->transform * matrix;
|
||||||
|
@ -1162,7 +1156,7 @@ void LottieBuilder::updateText(LottieLayer* layer, float frameNo)
|
||||||
// TextGroup transformation is performed once
|
// TextGroup transformation is performed once
|
||||||
if (textGroup->paints().size() == 0 && needGroup) {
|
if (textGroup->paints().size() == 0 && needGroup) {
|
||||||
identity(&textGroupMatrix);
|
identity(&textGroupMatrix);
|
||||||
translate(&textGroupMatrix, cursor.x, cursor.y);
|
translate(&textGroupMatrix, cursor);
|
||||||
|
|
||||||
auto alignment = text->alignOption.anchor(frameNo);
|
auto alignment = text->alignOption.anchor(frameNo);
|
||||||
|
|
||||||
|
@ -1172,20 +1166,18 @@ void LottieBuilder::updateText(LottieLayer* layer, float frameNo)
|
||||||
|
|
||||||
rotate(&textGroupMatrix, rotation);
|
rotate(&textGroupMatrix, rotation);
|
||||||
|
|
||||||
auto pivotX = alignment.x * -1;
|
|
||||||
auto pivotY = alignment.y * -1;
|
|
||||||
|
|
||||||
//center pivoting
|
//center pivoting
|
||||||
textGroupMatrix.e13 += (pivotX * textGroupMatrix.e11 + pivotX * textGroupMatrix.e12);
|
auto pivot = alignment * -1;
|
||||||
textGroupMatrix.e23 += (pivotY * textGroupMatrix.e21 + pivotY * textGroupMatrix.e22);
|
textGroupMatrix.e13 += (pivot.x * textGroupMatrix.e11 + pivot.x * textGroupMatrix.e12);
|
||||||
|
textGroupMatrix.e23 += (pivot.y * textGroupMatrix.e21 + pivot.y * textGroupMatrix.e22);
|
||||||
|
|
||||||
textGroup->transform(textGroupMatrix);
|
textGroup->transform(textGroupMatrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& matrix = shape->transform();
|
auto& matrix = shape->transform();
|
||||||
identity(&matrix);
|
identity(&matrix);
|
||||||
translate(&matrix, translation.x / scale + cursor.x - textGroupMatrix.e13, translation.y / scale + cursor.y - textGroupMatrix.e23);
|
translate(&matrix, (translation / scale + cursor) - Point{textGroupMatrix.e13, textGroupMatrix.e23});
|
||||||
tvg::scale(&matrix, scaling.x * capScale, scaling.y * capScale);
|
tvg::scale(&matrix, scaling * capScale);
|
||||||
shape->transform(matrix);
|
shape->transform(matrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -771,7 +771,7 @@ static Paint* _textBuildHelper(SvgLoaderData& loaderData, const SvgNode* node, c
|
||||||
if (node->transform) textTransform = *node->transform;
|
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};
|
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);
|
text->transform(textTransform);
|
||||||
|
|
||||||
//TODO: handle def values of font and size as used in a system?
|
//TODO: handle def values of font and size as used in a system?
|
||||||
|
|
|
@ -56,7 +56,7 @@ void GlRenderPass::getMatrix(float *dst, const Matrix &matrix) const
|
||||||
|
|
||||||
Matrix postMatrix{};
|
Matrix postMatrix{};
|
||||||
identity(&postMatrix);
|
identity(&postMatrix);
|
||||||
translate(&postMatrix, -vp.x, -vp.y);
|
translate(&postMatrix, {(float)-vp.x, (float)-vp.y});
|
||||||
|
|
||||||
auto m = postMatrix * matrix;
|
auto m = postMatrix * matrix;
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ namespace tvg
|
||||||
m.e31 = 0.0f;
|
m.e31 = 0.0f;
|
||||||
m.e32 = 0.0f;
|
m.e32 = 0.0f;
|
||||||
m.e33 = 1.0f;
|
m.e33 = 1.0f;
|
||||||
tvg::scale(&m, scale, scale);
|
tvg::scale(&m, {scale, scale});
|
||||||
tvg::rotate(&m, degree);
|
tvg::rotate(&m, degree);
|
||||||
}
|
}
|
||||||
} tr;
|
} tr;
|
||||||
|
|
Loading…
Add table
Reference in a new issue