SVGFormattingContext.cpp 22 KB

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