CanvasTransform.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Debug.h>
  10. #include <LibGfx/Painter.h>
  11. #include <LibWeb/Geometry/DOMMatrix.h>
  12. #include <LibWeb/HTML/Canvas/CanvasState.h>
  13. namespace Web::HTML {
  14. // https://html.spec.whatwg.org/multipage/canvas.html#canvastransform
  15. template<typename IncludingClass>
  16. class CanvasTransform {
  17. public:
  18. ~CanvasTransform() = default;
  19. void scale(float sx, float sy)
  20. {
  21. dbgln_if(CANVAS_RENDERING_CONTEXT_2D_DEBUG, "CanvasTransform::scale({}, {})", sx, sy);
  22. my_drawing_state().transform.scale(sx, sy);
  23. }
  24. void translate(float tx, float ty)
  25. {
  26. dbgln_if(CANVAS_RENDERING_CONTEXT_2D_DEBUG, "CanvasTransform::translate({}, {})", tx, ty);
  27. my_drawing_state().transform.translate(tx, ty);
  28. }
  29. void rotate(float radians)
  30. {
  31. dbgln_if(CANVAS_RENDERING_CONTEXT_2D_DEBUG, "CanvasTransform::rotate({})", radians);
  32. my_drawing_state().transform.rotate_radians(radians);
  33. }
  34. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-transform
  35. void transform(double a, double b, double c, double d, double e, double f)
  36. {
  37. // 1. If any of the arguments are infinite or NaN, then return.
  38. if (!isfinite(a) || !isfinite(b) || !isfinite(c) || !isfinite(d) || !isfinite(e) || !isfinite(f))
  39. return;
  40. // 2. Replace the current transformation matrix with the result of multiplying the current transformation matrix with the matrix described by:
  41. // a c e
  42. // b d f
  43. // 0 0 1
  44. my_drawing_state().transform.multiply({ static_cast<float>(a), static_cast<float>(b), static_cast<float>(c), static_cast<float>(d), static_cast<float>(e), static_cast<float>(f) });
  45. }
  46. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-gettransform
  47. WebIDL::ExceptionOr<JS::NonnullGCPtr<Geometry::DOMMatrix>> get_transform()
  48. {
  49. auto& realm = static_cast<IncludingClass&>(*this).realm();
  50. auto transform = my_drawing_state().transform;
  51. Geometry::DOMMatrix2DInit init = { transform.a(), transform.b(), transform.c(), transform.d(), transform.e(), transform.f(), {}, {}, {}, {}, {}, {} };
  52. return Geometry::DOMMatrix::create_from_dom_matrix_2d_init(realm, init);
  53. }
  54. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-settransform
  55. void set_transform(double a, double b, double c, double d, double e, double f)
  56. {
  57. // 1. If any of the arguments are infinite or NaN, then return.
  58. if (!isfinite(a) || !isfinite(b) || !isfinite(c) || !isfinite(d) || !isfinite(e) || !isfinite(f))
  59. return;
  60. // 2. Reset the current transformation matrix to the identity matrix.
  61. my_drawing_state().transform = {};
  62. if (auto* painter = static_cast<IncludingClass&>(*this).painter())
  63. painter->set_transform({});
  64. // 3. Invoke the transform(a, b, c, d, e, f) method with the same arguments.
  65. transform(a, b, c, d, e, f);
  66. }
  67. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-settransform-matrix
  68. WebIDL::ExceptionOr<void> set_transform(Geometry::DOMMatrix2DInit& init)
  69. {
  70. // 1. Let matrix be the result of creating a DOMMatrix from the 2D dictionary transform.
  71. auto& realm = static_cast<IncludingClass&>(*this).realm();
  72. auto matrix = TRY(Geometry::DOMMatrix::create_from_dom_matrix_2d_init(realm, init));
  73. // 2. If one or more of matrix's m11 element, m12 element, m21 element, m22 element, m41 element, or m42 element are infinite or NaN, then return.
  74. if (!isfinite(matrix->m11()) || !isfinite(matrix->m12()) || !isfinite(matrix->m21()) || !isfinite(matrix->m22()) || !isfinite(matrix->m41()) || !isfinite(matrix->m42()))
  75. return {};
  76. // 3. Reset the current transformation matrix to matrix.
  77. auto transform = Gfx::AffineTransform { static_cast<float>(matrix->a()), static_cast<float>(matrix->b()), static_cast<float>(matrix->c()), static_cast<float>(matrix->d()), static_cast<float>(matrix->e()), static_cast<float>(matrix->f()) };
  78. my_drawing_state().transform = transform;
  79. if (auto* painter = static_cast<IncludingClass&>(*this).painter())
  80. painter->set_transform(transform);
  81. return {};
  82. }
  83. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-resettransform
  84. void reset_transform()
  85. {
  86. // The resetTransform() method, when invoked, must reset the current transformation matrix to the identity matrix.
  87. my_drawing_state().transform = {};
  88. if (auto* painter = static_cast<IncludingClass&>(*this).painter())
  89. painter->set_transform({});
  90. }
  91. protected:
  92. CanvasTransform() = default;
  93. private:
  94. CanvasState::DrawingState& my_drawing_state() { return reinterpret_cast<IncludingClass&>(*this).drawing_state(); }
  95. CanvasState::DrawingState const& my_drawing_state() const { return reinterpret_cast<IncludingClass const&>(*this).drawing_state(); }
  96. };
  97. }