SVGRectElement.cpp 9.0 KB

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