| 1 | // LAF Gfx Library |
| 2 | // Copyright (c) 2020 Igara Studio S.A. |
| 3 | // |
| 4 | // This file is released under the terms of the MIT license. |
| 5 | // Read LICENSE.txt for more information. |
| 6 | |
| 7 | #ifndef GFX_MATRIX_SKIA_H_INCLUDED |
| 8 | #define GFX_MATRIX_SKIA_H_INCLUDED |
| 9 | #pragma once |
| 10 | |
| 11 | #include "gfx/point.h" |
| 12 | #include "gfx/rect.h" |
| 13 | |
| 14 | #include "include/core/SkMatrix.h" |
| 15 | |
| 16 | namespace gfx { |
| 17 | |
| 18 | // Simple wrapper for SkMatrix |
| 19 | class Matrix { |
| 20 | public: |
| 21 | constexpr Matrix() { } |
| 22 | constexpr Matrix(const SkMatrix& skMatrix) : m_skMatrix(skMatrix) { } |
| 23 | |
| 24 | static Matrix MakeScale(float sx, float sy) { |
| 25 | return Matrix(SkMatrix::Scale(sx, sy)); |
| 26 | } |
| 27 | |
| 28 | static Matrix MakeScale(float scale) { |
| 29 | return Matrix(SkMatrix::Scale(scale, scale)); |
| 30 | } |
| 31 | |
| 32 | static Matrix MakeTrans(float x, float y) { |
| 33 | return Matrix(SkMatrix::Translate(x, y)); |
| 34 | } |
| 35 | |
| 36 | static Matrix MakeAll(float scaleX, float skewX, float transX, |
| 37 | float skewY, float scaleY, float transY, |
| 38 | float pers0, float pers1, float pers2) { |
| 39 | return Matrix(SkMatrix::MakeAll(scaleX, skewX, transX, |
| 40 | skewY, scaleY, transY, |
| 41 | pers0, pers1, pers2)); |
| 42 | } |
| 43 | |
| 44 | Matrix& reset() { |
| 45 | m_skMatrix.reset(); |
| 46 | return *this; |
| 47 | } |
| 48 | |
| 49 | bool isIdentity() const { return m_skMatrix.isIdentity(); } |
| 50 | bool isScaleTranslate() const { return m_skMatrix.isScaleTranslate(); } |
| 51 | bool isTranslate() const { return m_skMatrix.isTranslate(); } |
| 52 | |
| 53 | float getScaleX() const { return m_skMatrix.getScaleX(); } |
| 54 | float getScaleY() const { return m_skMatrix.getScaleY(); } |
| 55 | float getSkewY() const { return m_skMatrix.getSkewY(); } |
| 56 | float getSkewX() const { return m_skMatrix.getSkewX(); } |
| 57 | float getTranslateX() const { return m_skMatrix.getTranslateX(); } |
| 58 | float getTranslateY() const { return m_skMatrix.getTranslateY(); } |
| 59 | float getPerspX() const { return m_skMatrix.getPerspX(); } |
| 60 | float getPerspY() const { return m_skMatrix.getPerspY(); } |
| 61 | |
| 62 | Matrix& setIdentity() { |
| 63 | m_skMatrix.setIdentity(); |
| 64 | return *this; |
| 65 | } |
| 66 | |
| 67 | Matrix& setTranslate(float dx, float dy) { |
| 68 | m_skMatrix.setTranslate(dx, dy); |
| 69 | return *this; |
| 70 | } |
| 71 | |
| 72 | void setScale(float sx, float sy, float px, float py) { |
| 73 | m_skMatrix.setScale(sx, sy, px, py); |
| 74 | } |
| 75 | |
| 76 | void setScale(float sx, float sy) { |
| 77 | m_skMatrix.setScale(sx, sy); |
| 78 | } |
| 79 | |
| 80 | void setRotate(float degrees, float px, float py) { |
| 81 | m_skMatrix.setRotate(degrees, px, py); |
| 82 | } |
| 83 | |
| 84 | void setRotate(float degrees) { |
| 85 | m_skMatrix.setRotate(degrees); |
| 86 | } |
| 87 | |
| 88 | void setScaleTranslate(float sx, float sy, float tx, float ty) { |
| 89 | m_skMatrix.setScaleTranslate(sx, sy, tx, ty); |
| 90 | } |
| 91 | |
| 92 | Matrix& preTranslate(float dx, float dy) { |
| 93 | m_skMatrix.preTranslate(dx, dy); |
| 94 | return *this; |
| 95 | } |
| 96 | |
| 97 | Matrix& postTranslate(float dx, float dy) { |
| 98 | m_skMatrix.postTranslate(dx, dy); |
| 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | RectF mapRect(const RectF& src) const { |
| 103 | SkRect dst; |
| 104 | m_skMatrix.mapRect(&dst, SkRect::MakeXYWH(SkScalar(src.x), SkScalar(src.y), |
| 105 | SkScalar(src.w), SkScalar(src.h))); |
| 106 | return RectF(dst.x(), dst.y(), dst.width(), dst.height()); |
| 107 | } |
| 108 | |
| 109 | const SkMatrix& skMatrix() const { return m_skMatrix; } |
| 110 | SkMatrix& skMatrix() { return m_skMatrix; } |
| 111 | |
| 112 | private: |
| 113 | SkMatrix m_skMatrix; |
| 114 | }; |
| 115 | |
| 116 | } // namespace gfx |
| 117 | |
| 118 | #endif |
| 119 | |