SVGRectElement.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SVGRectElement.h"
  7. #include <LibWeb/Bindings/SVGRectElementPrototype.h>
  8. #include <LibWeb/HTML/Window.h>
  9. #include <LibWeb/SVG/AttributeNames.h>
  10. #include <LibWeb/SVG/AttributeParser.h>
  11. #include <LibWeb/SVG/SVGAnimatedLength.h>
  12. #include <LibWeb/SVG/SVGLength.h>
  13. namespace Web::SVG {
  14. SVGRectElement::SVGRectElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  15. : SVGGeometryElement(document, qualified_name)
  16. {
  17. set_prototype(&window().ensure_web_prototype<Bindings::SVGRectElementPrototype>("SVGRectElement"));
  18. }
  19. void SVGRectElement::parse_attribute(FlyString const& name, String const& value)
  20. {
  21. SVGGeometryElement::parse_attribute(name, value);
  22. if (name == SVG::AttributeNames::x) {
  23. m_x = AttributeParser::parse_coordinate(value);
  24. m_path.clear();
  25. } else if (name == SVG::AttributeNames::y) {
  26. m_y = AttributeParser::parse_coordinate(value);
  27. m_path.clear();
  28. } else if (name == SVG::AttributeNames::width) {
  29. m_width = AttributeParser::parse_positive_length(value);
  30. m_path.clear();
  31. } else if (name == SVG::AttributeNames::height) {
  32. m_height = AttributeParser::parse_positive_length(value);
  33. m_path.clear();
  34. } else if (name == SVG::AttributeNames::rx) {
  35. m_radius_x = AttributeParser::parse_length(value);
  36. m_path.clear();
  37. } else if (name == SVG::AttributeNames::ry) {
  38. m_radius_y = AttributeParser::parse_length(value);
  39. m_path.clear();
  40. }
  41. }
  42. Gfx::Path& SVGRectElement::get_path()
  43. {
  44. if (m_path.has_value())
  45. return m_path.value();
  46. float width = m_width.value_or(0);
  47. float height = m_height.value_or(0);
  48. float x = m_x.value_or(0);
  49. float y = m_y.value_or(0);
  50. Gfx::Path path;
  51. // If width or height is zero, rendering is disabled.
  52. if (width == 0 && height == 0) {
  53. m_path = move(path);
  54. return m_path.value();
  55. }
  56. auto corner_radii = calculate_used_corner_radius_values();
  57. float rx = corner_radii.x();
  58. float ry = corner_radii.y();
  59. // 1. perform an absolute moveto operation to location (x+rx,y);
  60. path.move_to({ x + rx, y });
  61. // 2, perform an absolute horizontal lineto with parameter x+width-rx;
  62. path.horizontal_line_to(x + width - rx);
  63. // 3. if both rx and ry are greater than zero,
  64. // perform an absolute elliptical arc operation to coordinate (x+width,y+ry),
  65. // where rx and ry are used as the equivalent parameters to the elliptical arc command,
  66. // the x-axis-rotation and large-arc-flag are set to zero,
  67. // the sweep-flag is set to one;
  68. double x_axis_rotation = 0;
  69. bool large_arc_flag = false;
  70. bool sweep_flag = true;
  71. if (rx > 0 && ry > 0)
  72. path.elliptical_arc_to({ x + width, y + ry }, corner_radii, x_axis_rotation, large_arc_flag, sweep_flag);
  73. // 4. perform an absolute vertical lineto parameter y+height-ry;
  74. path.vertical_line_to(y + height - ry);
  75. // 5. if both rx and ry are greater than zero,
  76. // perform an absolute elliptical arc operation to coordinate (x+width-rx,y+height),
  77. // using the same parameters as previously;
  78. if (rx > 0 && ry > 0)
  79. path.elliptical_arc_to({ x + width - rx, y + height }, corner_radii, x_axis_rotation, large_arc_flag, sweep_flag);
  80. // 6. perform an absolute horizontal lineto parameter x+rx;
  81. path.horizontal_line_to(x + rx);
  82. // 7. if both rx and ry are greater than zero,
  83. // perform an absolute elliptical arc operation to coordinate (x,y+height-ry),
  84. // using the same parameters as previously;
  85. if (rx > 0 && ry > 0)
  86. path.elliptical_arc_to({ x, y + height - ry }, corner_radii, x_axis_rotation, large_arc_flag, sweep_flag);
  87. // 8. perform an absolute vertical lineto parameter y+ry
  88. path.vertical_line_to(y + ry);
  89. // 9. if both rx and ry are greater than zero,
  90. // perform an absolute elliptical arc operation with a segment-completing close path operation,
  91. // using the same parameters as previously.
  92. if (rx > 0 && ry > 0)
  93. path.elliptical_arc_to({ x + rx, y }, corner_radii, x_axis_rotation, large_arc_flag, sweep_flag);
  94. m_path = move(path);
  95. return m_path.value();
  96. }
  97. Gfx::FloatPoint SVGRectElement::calculate_used_corner_radius_values()
  98. {
  99. // 1. Let rx and ry be length values.
  100. float rx = 0;
  101. float ry = 0;
  102. // 2. If neither ‘rx’ nor ‘ry’ are properly specified, then set both rx and ry to 0. (This will result in square corners.)
  103. if (!m_radius_x.has_value() && !m_radius_y.has_value()) {
  104. rx = 0;
  105. ry = 0;
  106. }
  107. // 3. Otherwise, if a properly specified value is provided for ‘rx’, but not for ‘ry’, then set both rx and ry to the value of ‘rx’.
  108. else if (m_radius_x.has_value()) {
  109. rx = m_radius_x.value();
  110. ry = m_radius_x.value();
  111. }
  112. // 4. Otherwise, if a properly specified value is provided for ‘ry’, but not for ‘rx’, then set both rx and ry to the value of ‘ry’.
  113. else if (m_radius_y.has_value()) {
  114. rx = m_radius_y.value();
  115. ry = m_radius_y.value();
  116. }
  117. // 5. Otherwise, both ‘rx’ and ‘ry’ were specified properly. Set rx to the value of ‘rx’ and ry to the value of ‘ry’.
  118. else {
  119. rx = m_radius_x.value();
  120. ry = m_radius_y.value();
  121. }
  122. // 6. If rx is greater than half of ‘width’, then set rx to half of ‘width’.
  123. auto half_width = m_width.value_or(0) / 2;
  124. if (rx > half_width)
  125. rx = half_width;
  126. // 7. If ry is greater than half of ‘height’, then set ry to half of ‘height’.
  127. auto half_height = m_height.value_or(0) / 2;
  128. if (ry > half_height)
  129. ry = half_height;
  130. // 8. The effective values of ‘rx’ and ‘ry’ are rx and ry, respectively.
  131. return Gfx::FloatPoint { rx, ry };
  132. }
  133. // https://www.w3.org/TR/SVG11/shapes.html#RectElementXAttribute
  134. JS::NonnullGCPtr<SVGAnimatedLength> SVGRectElement::x() const
  135. {
  136. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  137. // FIXME: Create a proper animated value when animations are supported.
  138. auto base_length = SVGLength::create(window(), 0, m_x.value_or(0));
  139. auto anim_length = SVGLength::create(window(), 0, m_x.value_or(0));
  140. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  141. }
  142. // https://www.w3.org/TR/SVG11/shapes.html#RectElementYAttribute
  143. JS::NonnullGCPtr<SVGAnimatedLength> SVGRectElement::y() const
  144. {
  145. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  146. // FIXME: Create a proper animated value when animations are supported.
  147. auto base_length = SVGLength::create(window(), 0, m_y.value_or(0));
  148. auto anim_length = SVGLength::create(window(), 0, m_y.value_or(0));
  149. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  150. }
  151. // https://www.w3.org/TR/SVG11/shapes.html#RectElementWidthAttribute
  152. JS::NonnullGCPtr<SVGAnimatedLength> SVGRectElement::width() const
  153. {
  154. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  155. // FIXME: Create a proper animated value when animations are supported.
  156. auto base_length = SVGLength::create(window(), 0, m_width.value_or(0));
  157. auto anim_length = SVGLength::create(window(), 0, m_width.value_or(0));
  158. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  159. }
  160. // https://www.w3.org/TR/SVG11/shapes.html#RectElementHeightAttribute
  161. JS::NonnullGCPtr<SVGAnimatedLength> SVGRectElement::height() const
  162. {
  163. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  164. // FIXME: Create a proper animated value when animations are supported.
  165. auto base_length = SVGLength::create(window(), 0, m_height.value_or(0));
  166. auto anim_length = SVGLength::create(window(), 0, m_height.value_or(0));
  167. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  168. }
  169. // https://www.w3.org/TR/SVG11/shapes.html#RectElementRXAttribute
  170. JS::NonnullGCPtr<SVGAnimatedLength> SVGRectElement::rx() const
  171. {
  172. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  173. // FIXME: Create a proper animated value when animations are supported.
  174. auto base_length = SVGLength::create(window(), 0, m_radius_x.value_or(0));
  175. auto anim_length = SVGLength::create(window(), 0, m_radius_x.value_or(0));
  176. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  177. }
  178. // https://www.w3.org/TR/SVG11/shapes.html#RectElementRYAttribute
  179. JS::NonnullGCPtr<SVGAnimatedLength> SVGRectElement::ry() const
  180. {
  181. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  182. // FIXME: Create a proper animated value when animations are supported.
  183. auto base_length = SVGLength::create(window(), 0, m_radius_y.value_or(0));
  184. auto anim_length = SVGLength::create(window(), 0, m_radius_y.value_or(0));
  185. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  186. }
  187. }