SVGFormattingContext.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (c) 2021-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  5. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/Debug.h>
  10. #include <LibGfx/BoundingBox.h>
  11. #include <LibGfx/Font/ScaledFont.h>
  12. #include <LibGfx/Path.h>
  13. #include <LibGfx/TextLayout.h>
  14. #include <LibWeb/Layout/BlockFormattingContext.h>
  15. #include <LibWeb/Layout/SVGClipBox.h>
  16. #include <LibWeb/Layout/SVGFormattingContext.h>
  17. #include <LibWeb/Layout/SVGGeometryBox.h>
  18. #include <LibWeb/Layout/SVGImageBox.h>
  19. #include <LibWeb/Layout/SVGMaskBox.h>
  20. #include <LibWeb/SVG/SVGAElement.h>
  21. #include <LibWeb/SVG/SVGClipPathElement.h>
  22. #include <LibWeb/SVG/SVGForeignObjectElement.h>
  23. #include <LibWeb/SVG/SVGGElement.h>
  24. #include <LibWeb/SVG/SVGImageElement.h>
  25. #include <LibWeb/SVG/SVGMaskElement.h>
  26. #include <LibWeb/SVG/SVGSVGElement.h>
  27. #include <LibWeb/SVG/SVGSymbolElement.h>
  28. #include <LibWeb/SVG/SVGUseElement.h>
  29. namespace Web::Layout {
  30. SVGFormattingContext::SVGFormattingContext(LayoutState& state, Box const& box, FormattingContext* parent, Gfx::AffineTransform parent_viewbox_transform)
  31. : FormattingContext(Type::SVG, state, box, parent)
  32. , m_parent_viewbox_transform(parent_viewbox_transform)
  33. {
  34. }
  35. SVGFormattingContext::~SVGFormattingContext() = default;
  36. CSSPixels SVGFormattingContext::automatic_content_width() const
  37. {
  38. return 0;
  39. }
  40. CSSPixels SVGFormattingContext::automatic_content_height() const
  41. {
  42. return 0;
  43. }
  44. struct ViewBoxTransform {
  45. CSSPixelPoint offset;
  46. double scale_factor_x;
  47. double scale_factor_y;
  48. };
  49. // https://svgwg.org/svg2-draft/coords.html#PreserveAspectRatioAttribute
  50. static ViewBoxTransform scale_and_align_viewbox_content(SVG::PreserveAspectRatio const& preserve_aspect_ratio,
  51. SVG::ViewBox const& view_box, Gfx::FloatSize viewbox_scale, auto const& svg_box_state)
  52. {
  53. ViewBoxTransform viewbox_transform {};
  54. if (preserve_aspect_ratio.align == SVG::PreserveAspectRatio::Align::None) {
  55. viewbox_transform.scale_factor_x = viewbox_scale.width();
  56. viewbox_transform.scale_factor_y = viewbox_scale.height();
  57. viewbox_transform.offset = {};
  58. return viewbox_transform;
  59. }
  60. switch (preserve_aspect_ratio.meet_or_slice) {
  61. case SVG::PreserveAspectRatio::MeetOrSlice::Meet:
  62. // meet (the default) - Scale the graphic such that:
  63. // - aspect ratio is preserved
  64. // - the entire ‘viewBox’ is visible within the SVG viewport
  65. // - the ‘viewBox’ is scaled up as much as possible, while still meeting the other criteria
  66. viewbox_transform.scale_factor_x = viewbox_transform.scale_factor_y = min(viewbox_scale.width(), viewbox_scale.height());
  67. break;
  68. case SVG::PreserveAspectRatio::MeetOrSlice::Slice:
  69. // slice - Scale the graphic such that:
  70. // aspect ratio is preserved
  71. // the entire SVG viewport is covered by the ‘viewBox’
  72. // the ‘viewBox’ is scaled down as much as possible, while still meeting the other criteria
  73. viewbox_transform.scale_factor_x = viewbox_transform.scale_factor_y = max(viewbox_scale.width(), viewbox_scale.height());
  74. break;
  75. default:
  76. VERIFY_NOT_REACHED();
  77. }
  78. // Handle X alignment:
  79. if (svg_box_state.has_definite_width()) {
  80. switch (preserve_aspect_ratio.align) {
  81. case SVG::PreserveAspectRatio::Align::xMinYMin:
  82. case SVG::PreserveAspectRatio::Align::xMinYMid:
  83. case SVG::PreserveAspectRatio::Align::xMinYMax:
  84. // Align the <min-x> of the element's ‘viewBox’ with the smallest X value of the SVG viewport.
  85. viewbox_transform.offset.translate_by(0, 0);
  86. break;
  87. case SVG::PreserveAspectRatio::Align::None: {
  88. // Do not force uniform scaling. Scale the graphic content of the given element non-uniformly
  89. // if necessary such that the element's bounding box exactly matches the SVG viewport rectangle.
  90. // FIXME: None is unimplemented (treat as xMidYMid)
  91. [[fallthrough]];
  92. }
  93. case SVG::PreserveAspectRatio::Align::xMidYMin:
  94. case SVG::PreserveAspectRatio::Align::xMidYMid:
  95. case SVG::PreserveAspectRatio::Align::xMidYMax:
  96. // Align the midpoint X value of the element's ‘viewBox’ with the midpoint X value of the SVG viewport.
  97. viewbox_transform.offset.translate_by((svg_box_state.content_width() - CSSPixels::nearest_value_for(view_box.width * viewbox_transform.scale_factor_x)) / 2, 0);
  98. break;
  99. case SVG::PreserveAspectRatio::Align::xMaxYMin:
  100. case SVG::PreserveAspectRatio::Align::xMaxYMid:
  101. case SVG::PreserveAspectRatio::Align::xMaxYMax:
  102. // Align the <min-x>+<width> of the element's ‘viewBox’ with the maximum X value of the SVG viewport.
  103. viewbox_transform.offset.translate_by((svg_box_state.content_width() - CSSPixels::nearest_value_for(view_box.width * viewbox_transform.scale_factor_x)), 0);
  104. break;
  105. default:
  106. VERIFY_NOT_REACHED();
  107. }
  108. }
  109. if (svg_box_state.has_definite_width()) {
  110. switch (preserve_aspect_ratio.align) {
  111. case SVG::PreserveAspectRatio::Align::xMinYMin:
  112. case SVG::PreserveAspectRatio::Align::xMidYMin:
  113. case SVG::PreserveAspectRatio::Align::xMaxYMin:
  114. // Align the <min-y> of the element's ‘viewBox’ with the smallest Y value of the SVG viewport.
  115. viewbox_transform.offset.translate_by(0, 0);
  116. break;
  117. case SVG::PreserveAspectRatio::Align::None: {
  118. // Do not force uniform scaling. Scale the graphic content of the given element non-uniformly
  119. // if necessary such that the element's bounding box exactly matches the SVG viewport rectangle.
  120. // FIXME: None is unimplemented (treat as xMidYMid)
  121. [[fallthrough]];
  122. }
  123. case SVG::PreserveAspectRatio::Align::xMinYMid:
  124. case SVG::PreserveAspectRatio::Align::xMidYMid:
  125. case SVG::PreserveAspectRatio::Align::xMaxYMid:
  126. // Align the midpoint Y value of the element's ‘viewBox’ with the midpoint Y value of the SVG viewport.
  127. viewbox_transform.offset.translate_by(0, (svg_box_state.content_height() - CSSPixels::nearest_value_for(view_box.height * viewbox_transform.scale_factor_y)) / 2);
  128. break;
  129. case SVG::PreserveAspectRatio::Align::xMinYMax:
  130. case SVG::PreserveAspectRatio::Align::xMidYMax:
  131. case SVG::PreserveAspectRatio::Align::xMaxYMax:
  132. // Align the <min-y>+<height> of the element's ‘viewBox’ with the maximum Y value of the SVG viewport.
  133. viewbox_transform.offset.translate_by(0, (svg_box_state.content_height() - CSSPixels::nearest_value_for(view_box.height * viewbox_transform.scale_factor_y)));
  134. break;
  135. default:
  136. VERIFY_NOT_REACHED();
  137. }
  138. }
  139. return viewbox_transform;
  140. }
  141. static bool is_container_element(Node const& node)
  142. {
  143. // https://svgwg.org/svg2-draft/struct.html#GroupsOverview
  144. auto* dom_node = node.dom_node();
  145. if (!dom_node)
  146. return false;
  147. if (is<SVG::SVGAElement>(dom_node))
  148. return true;
  149. if (is<SVG::SVGUseElement>(dom_node))
  150. return true;
  151. if (is<SVG::SVGSymbolElement>(dom_node))
  152. return true;
  153. if (is<SVG::SVGGElement>(dom_node))
  154. return true;
  155. if (is<SVG::SVGMaskElement>(dom_node))
  156. return true;
  157. return false;
  158. }
  159. void SVGFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const& available_space)
  160. {
  161. // NOTE: SVG doesn't have a "formatting context" in the spec, but this is the most
  162. // obvious way to drive SVG layout in our engine at the moment.
  163. auto& svg_viewport = dynamic_cast<SVG::SVGViewport const&>(*box.dom_node());
  164. auto& svg_box_state = m_state.get_mutable(box);
  165. // NOTE: We consider all SVG root elements to have definite size in both axes.
  166. // I'm not sure if this is good or bad, but our viewport transform logic depends on it.
  167. svg_box_state.set_has_definite_width(true);
  168. svg_box_state.set_has_definite_height(true);
  169. auto viewbox = svg_viewport.view_box();
  170. // https://svgwg.org/svg2-draft/coords.html#ViewBoxAttribute
  171. if (viewbox.has_value()) {
  172. if (viewbox->width < 0 || viewbox->height < 0) {
  173. // A negative value for <width> or <height> is an error and invalidates the ‘viewBox’ attribute.
  174. viewbox = {};
  175. } else if (viewbox->width == 0 || viewbox->height == 0) {
  176. // A value of zero disables rendering of the element.
  177. return;
  178. }
  179. }
  180. m_current_viewbox_transform = m_parent_viewbox_transform;
  181. if (viewbox.has_value()) {
  182. // FIXME: This should allow just one of width or height to be specified.
  183. // E.g. We should be able to layout <svg width="100%"> where height is unspecified/auto.
  184. if (!svg_box_state.has_definite_width() || !svg_box_state.has_definite_height()) {
  185. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Attempting to layout indefinitely sized SVG with a viewbox -- this likely won't work!");
  186. }
  187. auto scale_width = svg_box_state.has_definite_width() ? svg_box_state.content_width() / viewbox->width : 1;
  188. auto scale_height = svg_box_state.has_definite_height() ? svg_box_state.content_height() / viewbox->height : 1;
  189. // The initial value for preserveAspectRatio is xMidYMid meet.
  190. auto preserve_aspect_ratio = svg_viewport.preserve_aspect_ratio().value_or(SVG::PreserveAspectRatio {});
  191. auto viewbox_offset_and_scale = scale_and_align_viewbox_content(preserve_aspect_ratio, *viewbox, { scale_width, scale_height }, svg_box_state);
  192. CSSPixelPoint offset = viewbox_offset_and_scale.offset;
  193. m_current_viewbox_transform = Gfx::AffineTransform { m_current_viewbox_transform }.multiply(Gfx::AffineTransform {}
  194. .translate(offset.to_type<float>())
  195. .scale(viewbox_offset_and_scale.scale_factor_x, viewbox_offset_and_scale.scale_factor_y)
  196. .translate({ -viewbox->min_x, -viewbox->min_y }));
  197. }
  198. if (svg_box_state.has_definite_width() && svg_box_state.has_definite_height()) {
  199. // Scale the box of the viewport based on the parent's viewBox transform.
  200. // The viewBox transform is always just a simple scale + offset.
  201. // FIXME: Avoid converting SVG box to floats.
  202. Gfx::FloatRect svg_rect = { svg_box_state.offset.to_type<float>(),
  203. { float(svg_box_state.content_width()), float(svg_box_state.content_height()) } };
  204. svg_rect = m_parent_viewbox_transform.map(svg_rect);
  205. svg_box_state.set_content_offset(svg_rect.location().to_type<CSSPixels>());
  206. svg_box_state.set_content_width(CSSPixels(svg_rect.width()));
  207. svg_box_state.set_content_height(CSSPixels(svg_rect.height()));
  208. svg_box_state.set_has_definite_width(true);
  209. svg_box_state.set_has_definite_height(true);
  210. }
  211. auto viewport_width = [&] {
  212. if (viewbox.has_value())
  213. return CSSPixels::nearest_value_for(viewbox->width);
  214. if (svg_box_state.has_definite_width())
  215. return svg_box_state.content_width();
  216. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Failed to resolve width of SVG viewport!");
  217. return CSSPixels {};
  218. }();
  219. auto viewport_height = [&] {
  220. if (viewbox.has_value())
  221. return CSSPixels::nearest_value_for(viewbox->height);
  222. if (svg_box_state.has_definite_height())
  223. return svg_box_state.content_height();
  224. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Failed to resolve height of SVG viewport!");
  225. return CSSPixels {};
  226. }();
  227. m_available_space = available_space;
  228. m_svg_offset = svg_box_state.offset;
  229. m_viewport_size = { viewport_width, viewport_height };
  230. box.for_each_child_of_type<Box>([&](Box const& child) {
  231. layout_svg_element(child);
  232. return IterationDecision::Continue;
  233. });
  234. }
  235. void SVGFormattingContext::layout_svg_element(Box const& child)
  236. {
  237. if (is<SVG::SVGViewport>(child.dom_node())) {
  238. layout_nested_viewport(child);
  239. } else if (is<SVG::SVGForeignObjectElement>(child.dom_node()) && is<BlockContainer>(child)) {
  240. Layout::BlockFormattingContext bfc(m_state, static_cast<BlockContainer const&>(child), this);
  241. bfc.run(child, LayoutMode::Normal, *m_available_space);
  242. auto& child_state = m_state.get_mutable(child);
  243. child_state.set_content_offset(child_state.offset.translated(m_svg_offset));
  244. child.for_each_child_of_type<SVGMaskBox>([&](SVGMaskBox const& child) {
  245. layout_svg_element(child);
  246. return IterationDecision::Continue;
  247. });
  248. } else if (is<SVGGraphicsBox>(child)) {
  249. layout_graphics_element(static_cast<SVGGraphicsBox const&>(child));
  250. }
  251. }
  252. void SVGFormattingContext::layout_nested_viewport(Box const& viewport)
  253. {
  254. // Layout for a nested SVG viewport.
  255. // https://svgwg.org/svg2-draft/coords.html#EstablishingANewSVGViewport.
  256. SVGFormattingContext nested_context(m_state, viewport, this, m_current_viewbox_transform);
  257. auto& nested_viewport_state = m_state.get_mutable(viewport);
  258. auto resolve_dimension = [](auto& node, auto size, auto reference_value) {
  259. // The value auto for width and height on the ‘svg’ element is treated as 100%.
  260. // https://svgwg.org/svg2-draft/geometry.html#Sizing
  261. if (size.is_auto())
  262. return reference_value;
  263. return size.to_px(node, reference_value);
  264. };
  265. auto nested_viewport_x = viewport.computed_values().x().to_px(viewport, m_viewport_size.width());
  266. auto nested_viewport_y = viewport.computed_values().y().to_px(viewport, m_viewport_size.height());
  267. auto nested_viewport_width = resolve_dimension(viewport, viewport.computed_values().width(), m_viewport_size.width());
  268. auto nested_viewport_height = resolve_dimension(viewport, viewport.computed_values().height(), m_viewport_size.height());
  269. nested_viewport_state.set_content_offset({ nested_viewport_x, nested_viewport_y });
  270. nested_viewport_state.set_content_width(nested_viewport_width);
  271. nested_viewport_state.set_content_height(nested_viewport_height);
  272. nested_viewport_state.set_has_definite_width(true);
  273. nested_viewport_state.set_has_definite_height(true);
  274. nested_context.run(static_cast<Box const&>(viewport), LayoutMode::Normal, *m_available_space);
  275. }
  276. Gfx::Path SVGFormattingContext::compute_path_for_text(SVGTextBox const& text_box)
  277. {
  278. auto& text_element = static_cast<SVG::SVGTextPositioningElement const&>(text_box.dom_node());
  279. auto& font = text_box.first_available_font();
  280. auto text_contents = text_element.text_contents();
  281. Utf8View text_utf8 { text_contents };
  282. auto text_width = font.width(text_utf8);
  283. auto text_offset = text_element.get_offset(m_viewport_size);
  284. // https://svgwg.org/svg2-draft/text.html#TextAnchoringProperties
  285. switch (text_element.text_anchor().value_or(SVG::TextAnchor::Start)) {
  286. case SVG::TextAnchor::Start:
  287. // The rendered characters are aligned such that the start of the resulting rendered text is at the initial
  288. // current text position.
  289. break;
  290. case SVG::TextAnchor::Middle: {
  291. // The rendered characters are shifted such that the geometric middle of the resulting rendered text
  292. // (determined from the initial and final current text position before applying the text-anchor property)
  293. // is at the initial current text position.
  294. text_offset.translate_by(-text_width / 2, 0);
  295. break;
  296. }
  297. case SVG::TextAnchor::End: {
  298. // The rendered characters are shifted such that the end of the resulting rendered text (final current text
  299. // position before applying the text-anchor property) is at the initial current text position.
  300. text_offset.translate_by(-text_width, 0);
  301. break;
  302. }
  303. default:
  304. VERIFY_NOT_REACHED();
  305. }
  306. Gfx::Path path;
  307. path.move_to(text_offset);
  308. path.text(text_utf8, font);
  309. return path;
  310. }
  311. Gfx::Path SVGFormattingContext::compute_path_for_text_path(SVGTextPathBox const& text_path_box)
  312. {
  313. auto& text_path_element = static_cast<SVG::SVGTextPathElement const&>(text_path_box.dom_node());
  314. auto path_or_shape = text_path_element.path_or_shape();
  315. if (!path_or_shape)
  316. return {};
  317. auto& font = text_path_box.first_available_font();
  318. auto text_contents = text_path_element.text_contents();
  319. Utf8View text_utf8 { text_contents };
  320. auto shape_path = const_cast<SVG::SVGGeometryElement&>(*path_or_shape).get_path(m_viewport_size);
  321. return shape_path.place_text_along(text_utf8, font);
  322. }
  323. void SVGFormattingContext::layout_path_like_element(SVGGraphicsBox const& graphics_box)
  324. {
  325. auto& graphics_box_state = m_state.get_mutable(graphics_box);
  326. VERIFY(graphics_box_state.computed_svg_transforms().has_value());
  327. auto to_css_pixels_transform = Gfx::AffineTransform {}
  328. .multiply(m_current_viewbox_transform)
  329. .multiply(graphics_box_state.computed_svg_transforms()->svg_transform());
  330. Gfx::Path path;
  331. if (is<SVGGeometryBox>(graphics_box)) {
  332. auto& geometry_box = static_cast<SVGGeometryBox const&>(graphics_box);
  333. path = const_cast<SVGGeometryBox&>(geometry_box).dom_node().get_path(m_viewport_size);
  334. } else if (is<SVGTextBox>(graphics_box)) {
  335. auto& text_box = static_cast<SVGTextBox const&>(graphics_box);
  336. path = compute_path_for_text(text_box);
  337. // <text> and <tspan> elements can contain more text elements.
  338. text_box.for_each_child_of_type<SVGGraphicsBox>([&](auto& child) {
  339. if (is<SVGTextBox>(child) || is<SVGTextPathBox>(child))
  340. layout_graphics_element(child);
  341. return IterationDecision::Continue;
  342. });
  343. } else if (is<SVGTextPathBox>(graphics_box)) {
  344. // FIXME: Support <tspan> in <textPath>.
  345. path = compute_path_for_text_path(static_cast<SVGTextPathBox const&>(graphics_box));
  346. }
  347. auto path_bounding_box = to_css_pixels_transform.map(path.bounding_box()).to_type<CSSPixels>();
  348. // Stroke increases the path's size by stroke_width/2 per side.
  349. CSSPixels stroke_width = CSSPixels::nearest_value_for(graphics_box.dom_node().visible_stroke_width() * m_current_viewbox_transform.x_scale());
  350. path_bounding_box.inflate(stroke_width, stroke_width);
  351. graphics_box_state.set_content_offset(path_bounding_box.top_left());
  352. graphics_box_state.set_content_width(path_bounding_box.width());
  353. graphics_box_state.set_content_height(path_bounding_box.height());
  354. graphics_box_state.set_has_definite_width(true);
  355. graphics_box_state.set_has_definite_height(true);
  356. graphics_box_state.set_computed_svg_path(move(path));
  357. }
  358. void SVGFormattingContext::layout_graphics_element(SVGGraphicsBox const& graphics_box)
  359. {
  360. auto& graphics_box_state = m_state.get_mutable(graphics_box);
  361. auto svg_transform = const_cast<SVGGraphicsBox&>(graphics_box).dom_node().get_transform();
  362. graphics_box_state.set_computed_svg_transforms(Painting::SVGGraphicsPaintable::ComputedTransforms(m_current_viewbox_transform, svg_transform));
  363. if (is_container_element(graphics_box)) {
  364. // https://svgwg.org/svg2-draft/struct.html#Groups
  365. // 5.2. Grouping: the ‘g’ element
  366. // The ‘g’ element is a container element for grouping together related graphics elements.
  367. layout_container_element(graphics_box);
  368. } else if (is<SVGImageBox>(graphics_box)) {
  369. layout_image_element(static_cast<SVGImageBox const&>(graphics_box));
  370. } else {
  371. // Assume this is a path-like element.
  372. layout_path_like_element(graphics_box);
  373. }
  374. if (auto* mask_box = graphics_box.first_child_of_type<SVGMaskBox>())
  375. layout_mask_or_clip(*mask_box);
  376. if (auto* clip_box = graphics_box.first_child_of_type<SVGClipBox>())
  377. layout_mask_or_clip(*clip_box);
  378. }
  379. void SVGFormattingContext::layout_image_element(SVGImageBox const& image_box)
  380. {
  381. auto& box_state = m_state.get_mutable(image_box);
  382. auto bounding_box = image_box.dom_node().bounding_box();
  383. box_state.set_content_x(bounding_box.x());
  384. box_state.set_content_y(bounding_box.y());
  385. box_state.set_content_width(bounding_box.width());
  386. box_state.set_content_height(bounding_box.height());
  387. box_state.set_has_definite_width(true);
  388. box_state.set_has_definite_height(true);
  389. }
  390. void SVGFormattingContext::layout_mask_or_clip(SVGBox const& mask_or_clip)
  391. {
  392. SVG::SVGUnits content_units {};
  393. if (is<SVGMaskBox>(mask_or_clip))
  394. content_units = static_cast<SVGMaskBox const&>(mask_or_clip).dom_node().mask_content_units();
  395. else if (is<SVGClipBox>(mask_or_clip))
  396. content_units = static_cast<SVGClipBox const&>(mask_or_clip).dom_node().clip_path_units();
  397. else
  398. VERIFY_NOT_REACHED();
  399. // FIXME: Somehow limit <clipPath> contents to: shape elements, <text>, and <use>.
  400. auto& layout_state = m_state.get_mutable(mask_or_clip);
  401. auto parent_viewbox_transform = m_current_viewbox_transform;
  402. if (content_units == SVG::SVGUnits::ObjectBoundingBox) {
  403. auto* parent_node = mask_or_clip.parent();
  404. auto& parent_node_state = m_state.get(*parent_node);
  405. layout_state.set_content_width(parent_node_state.content_width());
  406. layout_state.set_content_height(parent_node_state.content_height());
  407. parent_viewbox_transform = Gfx::AffineTransform {}.translate(parent_node_state.offset.to_type<float>());
  408. } else {
  409. layout_state.set_content_width(m_viewport_size.width());
  410. layout_state.set_content_height(m_viewport_size.height());
  411. }
  412. // Pretend masks/clips are a viewport so we can scale the contents depending on the `contentUnits`.
  413. SVGFormattingContext nested_context(m_state, mask_or_clip, this, parent_viewbox_transform);
  414. layout_state.set_has_definite_width(true);
  415. layout_state.set_has_definite_height(true);
  416. nested_context.run(static_cast<Box const&>(mask_or_clip), LayoutMode::Normal, *m_available_space);
  417. }
  418. void SVGFormattingContext::layout_container_element(SVGBox const& container)
  419. {
  420. auto& box_state = m_state.get_mutable(container);
  421. Gfx::BoundingBox<CSSPixels> bounding_box;
  422. container.for_each_child_of_type<Box>([&](Box const& child) {
  423. // Masks/clips do not change the bounding box of their parents.
  424. if (is<SVGMaskBox>(child) || is<SVGClipBox>(child))
  425. return IterationDecision::Continue;
  426. layout_svg_element(child);
  427. auto& child_state = m_state.get(child);
  428. bounding_box.add_point(child_state.offset);
  429. bounding_box.add_point(child_state.offset.translated(child_state.content_width(), child_state.content_height()));
  430. return IterationDecision::Continue;
  431. });
  432. box_state.set_content_x(bounding_box.x());
  433. box_state.set_content_y(bounding_box.y());
  434. box_state.set_content_width(bounding_box.width());
  435. box_state.set_content_height(bounding_box.height());
  436. box_state.set_has_definite_width(true);
  437. box_state.set_has_definite_height(true);
  438. }
  439. }