SVGRectElement.cpp 8.9 KB

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