SVGRectElement.cpp 9.0 KB

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