SVGRectElement.cpp 9.0 KB

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