CanvasTransform.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. flush_transform();
  24. }
  25. void translate(float tx, float ty)
  26. {
  27. dbgln_if(CANVAS_RENDERING_CONTEXT_2D_DEBUG, "CanvasTransform::translate({}, {})", tx, ty);
  28. my_drawing_state().transform.translate(tx, ty);
  29. flush_transform();
  30. }
  31. void rotate(float radians)
  32. {
  33. dbgln_if(CANVAS_RENDERING_CONTEXT_2D_DEBUG, "CanvasTransform::rotate({})", radians);
  34. my_drawing_state().transform.rotate_radians(radians);
  35. flush_transform();
  36. }
  37. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-transform
  38. void transform(double a, double b, double c, double d, double e, double f)
  39. {
  40. // 1. If any of the arguments are infinite or NaN, then return.
  41. if (!isfinite(a) || !isfinite(b) || !isfinite(c) || !isfinite(d) || !isfinite(e) || !isfinite(f))
  42. return;
  43. // 2. Replace the current transformation matrix with the result of multiplying the current transformation matrix with the matrix described by:
  44. // a c e
  45. // b d f
  46. // 0 0 1
  47. 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) });
  48. flush_transform();
  49. }
  50. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-gettransform
  51. WebIDL::ExceptionOr<JS::NonnullGCPtr<Geometry::DOMMatrix>> get_transform()
  52. {
  53. auto& realm = static_cast<IncludingClass&>(*this).realm();
  54. auto transform = my_drawing_state().transform;
  55. Geometry::DOMMatrix2DInit init = { transform.a(), transform.b(), transform.c(), transform.d(), transform.e(), transform.f(), {}, {}, {}, {}, {}, {} };
  56. return Geometry::DOMMatrix::create_from_dom_matrix_2d_init(realm, init);
  57. }
  58. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-settransform
  59. void set_transform(double a, double b, double c, double d, double e, double f)
  60. {
  61. // 1. If any of the arguments are infinite or NaN, then return.
  62. if (!isfinite(a) || !isfinite(b) || !isfinite(c) || !isfinite(d) || !isfinite(e) || !isfinite(f))
  63. return;
  64. // 2. Reset the current transformation matrix to the identity matrix.
  65. my_drawing_state().transform = {};
  66. flush_transform();
  67. // 3. Invoke the transform(a, b, c, d, e, f) method with the same arguments.
  68. transform(a, b, c, d, e, f);
  69. }
  70. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-settransform-matrix
  71. WebIDL::ExceptionOr<void> set_transform(Geometry::DOMMatrix2DInit& init)
  72. {
  73. // 1. Let matrix be the result of creating a DOMMatrix from the 2D dictionary transform.
  74. auto& realm = static_cast<IncludingClass&>(*this).realm();
  75. auto matrix = TRY(Geometry::DOMMatrix::create_from_dom_matrix_2d_init(realm, init));
  76. // 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.
  77. if (!isfinite(matrix->m11()) || !isfinite(matrix->m12()) || !isfinite(matrix->m21()) || !isfinite(matrix->m22()) || !isfinite(matrix->m41()) || !isfinite(matrix->m42()))
  78. return {};
  79. // 3. Reset the current transformation matrix to matrix.
  80. 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()) };
  81. my_drawing_state().transform = transform;
  82. flush_transform();
  83. return {};
  84. }
  85. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-resettransform
  86. void reset_transform()
  87. {
  88. // The resetTransform() method, when invoked, must reset the current transformation matrix to the identity matrix.
  89. my_drawing_state().transform = {};
  90. flush_transform();
  91. }
  92. void flush_transform()
  93. {
  94. if (auto* painter = static_cast<IncludingClass&>(*this).painter())
  95. painter->set_transform(my_drawing_state().transform);
  96. }
  97. protected:
  98. CanvasTransform() = default;
  99. private:
  100. CanvasState::DrawingState& my_drawing_state() { return reinterpret_cast<IncludingClass&>(*this).drawing_state(); }
  101. CanvasState::DrawingState const& my_drawing_state() const { return reinterpret_cast<IncludingClass const&>(*this).drawing_state(); }
  102. };
  103. }