SVGRectElement.cpp 8.8 KB

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