SVGFormattingContext.cpp 22 KB

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