SVGRectElement.cpp 8.5 KB

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