SVGRectElement.cpp 9.9 KB

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