Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / rive-cpp / src / renderer.cpp
1 #include "rive/math/mat2d.hpp"
2 #include "rive/renderer.hpp"
3
4 using namespace rive;
5
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);
11
12     float scaleX = 1.0f, scaleY = 1.0f;
13
14     switch (fit) {
15         case Fit::fill: {
16             scaleX = frame.width() / contentWidth;
17             scaleY = frame.height() / contentHeight;
18             break;
19         }
20         case Fit::contain: {
21             float minScale =
22                 std::fmin(frame.width() / contentWidth, frame.height() / contentHeight);
23             scaleX = scaleY = minScale;
24             break;
25         }
26         case Fit::cover: {
27             float maxScale =
28                 std::fmax(frame.width() / contentWidth, frame.height() / contentHeight);
29             scaleX = scaleY = maxScale;
30             break;
31         }
32         case Fit::fitHeight: {
33             float minScale = frame.height() / contentHeight;
34             scaleX = scaleY = minScale;
35             break;
36         }
37         case Fit::fitWidth: {
38             float minScale = frame.width() / contentWidth;
39             scaleX = scaleY = minScale;
40             break;
41         }
42         case Fit::none: {
43             scaleX = scaleY = 1.0f;
44             break;
45         }
46         case Fit::scaleDown: {
47             float minScale =
48                 std::fmin(frame.width() / contentWidth, frame.height() / contentHeight);
49             scaleX = scaleY = minScale < 1.0f ? minScale : 1.0f;
50             break;
51         }
52     }
53
54     Mat2D translation;
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);
57
58     return translation * Mat2D::fromScale(scaleX, scaleY) * Mat2D::fromTranslate(x, y);
59 }
60
61 void Renderer::translate(float tx, float ty) { this->transform(Mat2D(1, 0, 0, 1, tx, ty)); }
62
63 void Renderer::scale(float sx, float sy) { this->transform(Mat2D(sx, 0, 0, sy, 0, 0)); }
64
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));
69 }