1 #include "rive/math/mat2d.hpp"
2 #include "rive/renderer.hpp"
6 Mat2D rive::computeAlignment(Fit fit, Alignment alignment, const AABB& frame, const AABB& content) {
7 float contentWidth = content.width();
8 float contentHeight = content.height();
9 float x = -content.left() - contentWidth * 0.5f - (alignment.x() * contentWidth * 0.5f);
10 float y = -content.top() - contentHeight * 0.5f - (alignment.y() * contentHeight * 0.5f);
12 float scaleX = 1.0f, scaleY = 1.0f;
16 scaleX = frame.width() / contentWidth;
17 scaleY = frame.height() / contentHeight;
22 std::fmin(frame.width() / contentWidth, frame.height() / contentHeight);
23 scaleX = scaleY = minScale;
28 std::fmax(frame.width() / contentWidth, frame.height() / contentHeight);
29 scaleX = scaleY = maxScale;
32 case Fit::fitHeight: {
33 float minScale = frame.height() / contentHeight;
34 scaleX = scaleY = minScale;
38 float minScale = frame.width() / contentWidth;
39 scaleX = scaleY = minScale;
43 scaleX = scaleY = 1.0f;
46 case Fit::scaleDown: {
48 std::fmin(frame.width() / contentWidth, frame.height() / contentHeight);
49 scaleX = scaleY = minScale < 1.0f ? minScale : 1.0f;
55 translation[4] = frame.left() + frame.width() * 0.5f + (alignment.x() * frame.width() * 0.5f);
56 translation[5] = frame.top() + frame.height() * 0.5f + (alignment.y() * frame.height() * 0.5f);
58 return translation * Mat2D::fromScale(scaleX, scaleY) * Mat2D::fromTranslate(x, y);
61 void Renderer::translate(float tx, float ty) { this->transform(Mat2D(1, 0, 0, 1, tx, ty)); }
63 void Renderer::scale(float sx, float sy) { this->transform(Mat2D(sx, 0, 0, sy, 0, 0)); }
65 void Renderer::rotate(float radians) {
66 const float s = std::sin(radians);
67 const float c = std::cos(radians);
68 this->transform(Mat2D(c, s, -s, c, 0, 0));