SVGFormattingContext.cpp 24 KB

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