CanvasTransform.h 5.3 KB

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