CanvasPath.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ExtraMathConstants.h>
  8. #include <LibWeb/HTML/Canvas/CanvasPath.h>
  9. namespace Web::HTML {
  10. Gfx::AffineTransform CanvasPath::active_transform() const
  11. {
  12. if (m_canvas_state.has_value())
  13. return m_canvas_state->drawing_state().transform;
  14. return {};
  15. }
  16. void CanvasPath::close_path()
  17. {
  18. m_path.close();
  19. }
  20. void CanvasPath::move_to(float x, float y)
  21. {
  22. m_path.move_to(active_transform().map(Gfx::FloatPoint { x, y }));
  23. }
  24. void CanvasPath::line_to(float x, float y)
  25. {
  26. m_path.line_to(active_transform().map(Gfx::FloatPoint { x, y }));
  27. }
  28. void CanvasPath::quadratic_curve_to(float cx, float cy, float x, float y)
  29. {
  30. auto transform = active_transform();
  31. m_path.quadratic_bezier_curve_to(transform.map(Gfx::FloatPoint { cx, cy }), transform.map(Gfx::FloatPoint { x, y }));
  32. }
  33. void CanvasPath::bezier_curve_to(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
  34. {
  35. auto transform = active_transform();
  36. m_path.cubic_bezier_curve_to(
  37. transform.map(Gfx::FloatPoint { cp1x, cp1y }), transform.map(Gfx::FloatPoint { cp2x, cp2y }), transform.map(Gfx::FloatPoint { x, y }));
  38. }
  39. WebIDL::ExceptionOr<void> CanvasPath::arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise)
  40. {
  41. if (radius < 0)
  42. return WebIDL::IndexSizeError::create(m_self->realm(), DeprecatedString::formatted("The radius provided ({}) is negative.", radius));
  43. return ellipse(x, y, radius, radius, 0, start_angle, end_angle, counter_clockwise);
  44. }
  45. WebIDL::ExceptionOr<void> CanvasPath::ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise)
  46. {
  47. if (radius_x < 0)
  48. return WebIDL::IndexSizeError::create(m_self->realm(), DeprecatedString::formatted("The major-axis radius provided ({}) is negative.", radius_x));
  49. if (radius_y < 0)
  50. return WebIDL::IndexSizeError::create(m_self->realm(), DeprecatedString::formatted("The minor-axis radius provided ({}) is negative.", radius_y));
  51. if (constexpr float tau = M_TAU; (!counter_clockwise && (end_angle - start_angle) >= tau)
  52. || (counter_clockwise && (start_angle - end_angle) >= tau)) {
  53. start_angle = 0;
  54. // FIXME: elliptical_arc_to() incorrectly handles the case where the start/end points are very close.
  55. // So we slightly fudge the numbers here to correct for that.
  56. end_angle = tau * 0.9999f;
  57. } else {
  58. start_angle = fmodf(start_angle, tau);
  59. end_angle = fmodf(end_angle, tau);
  60. }
  61. // Then, figure out where the ends of the arc are.
  62. // To do so, we can pretend that the center of this ellipse is at (0, 0),
  63. // and the whole coordinate system is rotated `rotation` radians around the x axis, centered on `center`.
  64. // The sign of the resulting relative positions is just whether our angle is on one of the left quadrants.
  65. float sin_rotation;
  66. float cos_rotation;
  67. AK::sincos(rotation, sin_rotation, cos_rotation);
  68. auto resolve_point_with_angle = [&](float angle) {
  69. auto tan_relative = tanf(angle);
  70. auto tan2 = tan_relative * tan_relative;
  71. auto ab = radius_x * radius_y;
  72. auto a2 = radius_x * radius_x;
  73. auto b2 = radius_y * radius_y;
  74. auto sqrt = sqrtf(b2 + a2 * tan2);
  75. auto relative_x_position = ab / sqrt;
  76. auto relative_y_position = ab * tan_relative / sqrt;
  77. // Make sure to set the correct sign
  78. // -1 if 0 ≤ θ < 90° or 270°< θ ≤ 360°
  79. // 1 if 90° < θ< 270°
  80. float sn = cosf(angle) >= 0 ? 1 : -1;
  81. relative_x_position *= sn;
  82. relative_y_position *= sn;
  83. // Now rotate it (back) around the center point by 'rotation' radians, then move it back to our actual origin.
  84. auto relative_rotated_x_position = relative_x_position * cos_rotation - relative_y_position * sin_rotation;
  85. auto relative_rotated_y_position = relative_x_position * sin_rotation + relative_y_position * cos_rotation;
  86. return Gfx::FloatPoint { relative_rotated_x_position + x, relative_rotated_y_position + y };
  87. };
  88. auto start_point = resolve_point_with_angle(start_angle);
  89. auto end_point = resolve_point_with_angle(end_angle);
  90. auto delta_theta = end_angle - start_angle;
  91. auto transform = active_transform();
  92. m_path.move_to(transform.map(start_point));
  93. m_path.elliptical_arc_to(
  94. transform.map(Gfx::FloatPoint { end_point }),
  95. transform.map(Gfx::FloatSize { radius_x, radius_y }),
  96. rotation + transform.rotation(),
  97. delta_theta > AK::Pi<float>, !counter_clockwise);
  98. return {};
  99. }
  100. void CanvasPath::rect(float x, float y, float width, float height)
  101. {
  102. auto transform = active_transform();
  103. m_path.move_to(transform.map(Gfx::FloatPoint { x, y }));
  104. if (width == 0 || height == 0)
  105. return;
  106. m_path.line_to(transform.map(Gfx::FloatPoint { x + width, y }));
  107. m_path.line_to(transform.map(Gfx::FloatPoint { x + width, y + height }));
  108. m_path.line_to(transform.map(Gfx::FloatPoint { x, y + height }));
  109. m_path.close();
  110. }
  111. }