2021-09-17 23:12:16 +02:00
|
|
|
|
/*
|
2023-04-18 19:32:00 +02:00
|
|
|
|
* Copyright (c) 2021-2023, Andreas Kling <kling@serenityos.org>
|
2022-02-16 16:32:18 +00:00
|
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
2022-02-27 21:00:04 +01:00
|
|
|
|
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
|
2023-04-10 12:28:55 +01:00
|
|
|
|
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
2021-09-17 23:12:16 +02:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2023-05-31 11:48:29 +02:00
|
|
|
|
#include <AK/Debug.h>
|
2023-11-12 20:47:39 +00:00
|
|
|
|
#include <LibGfx/BoundingBox.h>
|
2022-11-10 13:54:57 +01:00
|
|
|
|
#include <LibWeb/Layout/BlockFormattingContext.h>
|
2021-09-17 23:12:16 +02:00
|
|
|
|
#include <LibWeb/Layout/SVGFormattingContext.h>
|
2022-02-11 12:37:22 +00:00
|
|
|
|
#include <LibWeb/Layout/SVGGeometryBox.h>
|
2023-04-18 19:32:00 +02:00
|
|
|
|
#include <LibWeb/Layout/SVGSVGBox.h>
|
2023-06-08 21:32:33 +03:00
|
|
|
|
#include <LibWeb/Layout/SVGTextBox.h>
|
2022-11-10 13:54:57 +01:00
|
|
|
|
#include <LibWeb/SVG/SVGForeignObjectElement.h>
|
2023-11-12 20:51:23 +00:00
|
|
|
|
#include <LibWeb/SVG/SVGGElement.h>
|
|
|
|
|
#include <LibWeb/SVG/SVGMaskElement.h>
|
2022-02-27 21:00:04 +01:00
|
|
|
|
#include <LibWeb/SVG/SVGSVGElement.h>
|
2023-08-19 19:42:31 +02:00
|
|
|
|
#include <LibWeb/SVG/SVGSymbolElement.h>
|
|
|
|
|
#include <LibWeb/SVG/SVGUseElement.h>
|
2021-09-17 23:12:16 +02:00
|
|
|
|
|
|
|
|
|
namespace Web::Layout {
|
|
|
|
|
|
2022-07-16 23:30:32 +02:00
|
|
|
|
SVGFormattingContext::SVGFormattingContext(LayoutState& state, Box const& box, FormattingContext* parent)
|
2022-02-19 20:13:47 +01:00
|
|
|
|
: FormattingContext(Type::SVG, state, box, parent)
|
2021-09-17 23:12:16 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
|
SVGFormattingContext::~SVGFormattingContext() = default;
|
2021-09-17 23:12:16 +02:00
|
|
|
|
|
2023-03-19 09:57:31 +01:00
|
|
|
|
CSSPixels SVGFormattingContext::automatic_content_width() const
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 17:46:10 +00:00
|
|
|
|
CSSPixels SVGFormattingContext::automatic_content_height() const
|
2022-09-24 13:39:43 +02:00
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 01:20:24 +01:00
|
|
|
|
struct ViewBoxTransform {
|
|
|
|
|
CSSPixelPoint offset;
|
2023-05-24 10:50:57 +02:00
|
|
|
|
double scale_factor;
|
2023-04-17 01:20:24 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://svgwg.org/svg2-draft/coords.html#PreserveAspectRatioAttribute
|
|
|
|
|
static ViewBoxTransform scale_and_align_viewbox_content(SVG::PreserveAspectRatio const& preserve_aspect_ratio,
|
|
|
|
|
SVG::ViewBox const& view_box, Gfx::FloatSize viewbox_scale, auto const& svg_box_state)
|
|
|
|
|
{
|
|
|
|
|
ViewBoxTransform viewbox_transform {};
|
|
|
|
|
|
|
|
|
|
switch (preserve_aspect_ratio.meet_or_slice) {
|
|
|
|
|
case SVG::PreserveAspectRatio::MeetOrSlice::Meet:
|
|
|
|
|
// meet (the default) - Scale the graphic such that:
|
|
|
|
|
// - aspect ratio is preserved
|
|
|
|
|
// - the entire ‘viewBox’ is visible within the SVG viewport
|
|
|
|
|
// - the ‘viewBox’ is scaled up as much as possible, while still meeting the other criteria
|
|
|
|
|
viewbox_transform.scale_factor = min(viewbox_scale.width(), viewbox_scale.height());
|
|
|
|
|
break;
|
|
|
|
|
case SVG::PreserveAspectRatio::MeetOrSlice::Slice:
|
|
|
|
|
// slice - Scale the graphic such that:
|
|
|
|
|
// aspect ratio is preserved
|
|
|
|
|
// the entire SVG viewport is covered by the ‘viewBox’
|
|
|
|
|
// the ‘viewBox’ is scaled down as much as possible, while still meeting the other criteria
|
|
|
|
|
viewbox_transform.scale_factor = max(viewbox_scale.width(), viewbox_scale.height());
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle X alignment:
|
|
|
|
|
if (svg_box_state.has_definite_width()) {
|
|
|
|
|
switch (preserve_aspect_ratio.align) {
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMinYMin:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMinYMid:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMinYMax:
|
|
|
|
|
// Align the <min-x> of the element's ‘viewBox’ with the smallest X value of the SVG viewport.
|
|
|
|
|
viewbox_transform.offset.translate_by(0, 0);
|
|
|
|
|
break;
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::None: {
|
|
|
|
|
// Do not force uniform scaling. Scale the graphic content of the given element non-uniformly
|
|
|
|
|
// if necessary such that the element's bounding box exactly matches the SVG viewport rectangle.
|
|
|
|
|
// FIXME: None is unimplemented (treat as xMidYMid)
|
|
|
|
|
[[fallthrough]];
|
|
|
|
|
}
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMidYMin:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMidYMid:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMidYMax:
|
|
|
|
|
// Align the midpoint X value of the element's ‘viewBox’ with the midpoint X value of the SVG viewport.
|
2023-08-26 15:57:31 +01:00
|
|
|
|
viewbox_transform.offset.translate_by((svg_box_state.content_width() - CSSPixels::nearest_value_for(view_box.width * viewbox_transform.scale_factor)) / 2, 0);
|
2023-04-17 01:20:24 +01:00
|
|
|
|
break;
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMaxYMin:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMaxYMid:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMaxYMax:
|
|
|
|
|
// Align the <min-x>+<width> of the element's ‘viewBox’ with the maximum X value of the SVG viewport.
|
2023-08-26 15:57:31 +01:00
|
|
|
|
viewbox_transform.offset.translate_by((svg_box_state.content_width() - CSSPixels::nearest_value_for(view_box.width * viewbox_transform.scale_factor)), 0);
|
2023-04-17 01:20:24 +01:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (svg_box_state.has_definite_width()) {
|
|
|
|
|
switch (preserve_aspect_ratio.align) {
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMinYMin:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMidYMin:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMaxYMin:
|
|
|
|
|
// Align the <min-y> of the element's ‘viewBox’ with the smallest Y value of the SVG viewport.
|
|
|
|
|
viewbox_transform.offset.translate_by(0, 0);
|
|
|
|
|
break;
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::None: {
|
|
|
|
|
// Do not force uniform scaling. Scale the graphic content of the given element non-uniformly
|
|
|
|
|
// if necessary such that the element's bounding box exactly matches the SVG viewport rectangle.
|
|
|
|
|
// FIXME: None is unimplemented (treat as xMidYMid)
|
|
|
|
|
[[fallthrough]];
|
|
|
|
|
}
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMinYMid:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMidYMid:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMaxYMid:
|
|
|
|
|
// Align the midpoint Y value of the element's ‘viewBox’ with the midpoint Y value of the SVG viewport.
|
2023-08-26 15:57:31 +01:00
|
|
|
|
viewbox_transform.offset.translate_by(0, (svg_box_state.content_height() - CSSPixels::nearest_value_for(view_box.height * viewbox_transform.scale_factor)) / 2);
|
2023-04-17 01:20:24 +01:00
|
|
|
|
break;
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMinYMax:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMidYMax:
|
|
|
|
|
case SVG::PreserveAspectRatio::Align::xMaxYMax:
|
|
|
|
|
// Align the <min-y>+<height> of the element's ‘viewBox’ with the maximum Y value of the SVG viewport.
|
2023-08-26 15:57:31 +01:00
|
|
|
|
viewbox_transform.offset.translate_by(0, (svg_box_state.content_height() - CSSPixels::nearest_value_for(view_box.height * viewbox_transform.scale_factor)));
|
2023-04-17 01:20:24 +01:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return viewbox_transform;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 20:51:23 +00:00
|
|
|
|
static bool is_container_element(Node const& node)
|
2023-08-19 19:42:31 +02:00
|
|
|
|
{
|
2023-11-12 20:51:23 +00:00
|
|
|
|
// https://svgwg.org/svg2-draft/struct.html#GroupsOverview
|
|
|
|
|
auto* dom_node = node.dom_node();
|
|
|
|
|
if (!dom_node)
|
|
|
|
|
return false;
|
|
|
|
|
if (is<SVG::SVGUseElement>(dom_node))
|
|
|
|
|
return true;
|
|
|
|
|
if (is<SVG::SVGSymbolElement>(dom_node))
|
|
|
|
|
return true;
|
|
|
|
|
if (is<SVG::SVGGElement>(dom_node))
|
|
|
|
|
return true;
|
|
|
|
|
if (is<SVG::SVGMaskElement>(dom_node))
|
2023-08-19 19:42:31 +02:00
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 19:32:00 +02:00
|
|
|
|
void SVGFormattingContext::run(Box const& box, LayoutMode layout_mode, AvailableSpace const& available_space)
|
2021-09-17 23:12:16 +02:00
|
|
|
|
{
|
2022-07-11 00:27:54 +02:00
|
|
|
|
auto& svg_svg_element = verify_cast<SVG::SVGSVGElement>(*box.dom_node());
|
|
|
|
|
|
2023-04-10 12:28:55 +01:00
|
|
|
|
auto svg_box_state = m_state.get(box);
|
|
|
|
|
auto root_offset = svg_box_state.offset;
|
2022-11-10 13:54:57 +01:00
|
|
|
|
|
|
|
|
|
box.for_each_child_of_type<BlockContainer>([&](BlockContainer const& child_box) {
|
|
|
|
|
if (is<SVG::SVGForeignObjectElement>(child_box.dom_node())) {
|
|
|
|
|
Layout::BlockFormattingContext bfc(m_state, child_box, this);
|
|
|
|
|
bfc.run(child_box, LayoutMode::Normal, available_space);
|
|
|
|
|
|
|
|
|
|
auto& child_state = m_state.get_mutable(child_box);
|
|
|
|
|
child_state.set_content_offset(child_state.offset.translated(root_offset));
|
|
|
|
|
}
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
|
2023-10-29 19:11:46 +00:00
|
|
|
|
auto compute_viewbox_transform = [&](auto const& viewbox) -> Gfx::AffineTransform {
|
|
|
|
|
if (!viewbox.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
// FIXME: This should allow just one of width or height to be specified.
|
|
|
|
|
// E.g. We should be able to layout <svg width="100%"> where height is unspecified/auto.
|
|
|
|
|
if (!svg_box_state.has_definite_width() || !svg_box_state.has_definite_height()) {
|
|
|
|
|
dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Attempting to layout indefinitely sized SVG with a viewbox -- this likely won't work!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto scale_width = svg_box_state.has_definite_width() ? svg_box_state.content_width() / viewbox->width : 1;
|
|
|
|
|
auto scale_height = svg_box_state.has_definite_height() ? svg_box_state.content_height() / viewbox->height : 1;
|
|
|
|
|
|
|
|
|
|
// The initial value for preserveAspectRatio is xMidYMid meet.
|
|
|
|
|
auto preserve_aspect_ratio = svg_svg_element.preserve_aspect_ratio().value_or(SVG::PreserveAspectRatio {});
|
|
|
|
|
auto viewbox_offset_and_scale = scale_and_align_viewbox_content(preserve_aspect_ratio, *viewbox, { scale_width, scale_height }, svg_box_state);
|
|
|
|
|
|
|
|
|
|
CSSPixelPoint offset = viewbox_offset_and_scale.offset;
|
|
|
|
|
return Gfx::AffineTransform {}.translate(offset.to_type<float>()).scale(viewbox_offset_and_scale.scale_factor, viewbox_offset_and_scale.scale_factor).translate({ -viewbox->min_x, -viewbox->min_y });
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-19 19:42:31 +02:00
|
|
|
|
box.for_each_in_subtree([&](Node const& descendant) {
|
2023-10-29 19:11:46 +00:00
|
|
|
|
if (is<SVGGraphicsBox>(descendant)) {
|
|
|
|
|
auto const& graphics_box = static_cast<SVGGraphicsBox const&>(descendant);
|
|
|
|
|
auto& dom_node = const_cast<SVGGraphicsBox&>(graphics_box).dom_node();
|
2023-11-11 21:11:27 +00:00
|
|
|
|
auto viewbox = dom_node.view_box();
|
|
|
|
|
|
|
|
|
|
// https://svgwg.org/svg2-draft/coords.html#ViewBoxAttribute
|
|
|
|
|
if (viewbox.has_value()) {
|
|
|
|
|
if (viewbox->width < 0 || viewbox->height < 0) {
|
|
|
|
|
// A negative value for <width> or <height> is an error and invalidates the ‘viewBox’ attribute.
|
|
|
|
|
viewbox = {};
|
|
|
|
|
} else if (viewbox->width == 0 || viewbox->height == 0) {
|
|
|
|
|
// A value of zero disables rendering of the element.
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-27 21:00:04 +01:00
|
|
|
|
|
2023-11-11 21:11:27 +00:00
|
|
|
|
auto& graphics_box_state = m_state.get_mutable(graphics_box);
|
2023-10-29 17:08:35 +00:00
|
|
|
|
auto svg_transform = dom_node.get_transform();
|
2023-11-11 21:11:27 +00:00
|
|
|
|
|
|
|
|
|
Gfx::AffineTransform viewbox_transform = compute_viewbox_transform(viewbox);
|
2023-10-29 19:11:46 +00:00
|
|
|
|
graphics_box_state.set_computed_svg_transforms(Painting::SVGGraphicsPaintable::ComputedTransforms(viewbox_transform, svg_transform));
|
|
|
|
|
auto to_css_pixels_transform = Gfx::AffineTransform {}.multiply(viewbox_transform).multiply(svg_transform);
|
2023-04-17 01:20:24 +01:00
|
|
|
|
|
2023-11-04 21:31:35 +00:00
|
|
|
|
Gfx::Path path;
|
2023-10-29 19:11:46 +00:00
|
|
|
|
if (is<SVGGeometryBox>(descendant)) {
|
2023-11-04 21:31:35 +00:00
|
|
|
|
path = static_cast<SVG::SVGGeometryElement&>(dom_node).get_path();
|
2023-10-29 19:11:46 +00:00
|
|
|
|
} else if (is<SVGTextBox>(descendant)) {
|
|
|
|
|
auto& text_element = static_cast<SVG::SVGTextPositioningElement&>(dom_node);
|
2023-04-10 12:28:55 +01:00
|
|
|
|
|
2023-12-09 23:42:02 +01:00
|
|
|
|
auto& font = graphics_box.first_available_font();
|
2023-10-29 19:11:46 +00:00
|
|
|
|
auto text_contents = text_element.text_contents();
|
2023-11-04 21:31:35 +00:00
|
|
|
|
Utf8View text_utf8 { text_contents };
|
|
|
|
|
auto text_width = font.width(text_utf8);
|
2023-10-29 17:08:35 +00:00
|
|
|
|
|
2023-11-04 21:31:35 +00:00
|
|
|
|
auto text_offset = text_element.get_offset();
|
2023-10-29 19:11:46 +00:00
|
|
|
|
// https://svgwg.org/svg2-draft/text.html#TextAnchoringProperties
|
|
|
|
|
switch (text_element.text_anchor().value_or(SVG::TextAnchor::Start)) {
|
|
|
|
|
case SVG::TextAnchor::Start:
|
|
|
|
|
// The rendered characters are aligned such that the start of the resulting rendered text is at the initial
|
|
|
|
|
// current text position.
|
|
|
|
|
break;
|
|
|
|
|
case SVG::TextAnchor::Middle: {
|
|
|
|
|
// The rendered characters are shifted such that the geometric middle of the resulting rendered text
|
|
|
|
|
// (determined from the initial and final current text position before applying the text-anchor property)
|
|
|
|
|
// is at the initial current text position.
|
|
|
|
|
text_offset.translate_by(-text_width / 2, 0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SVG::TextAnchor::End: {
|
|
|
|
|
// The rendered characters are shifted such that the end of the resulting rendered text (final current text
|
|
|
|
|
// position before applying the text-anchor property) is at the initial current text position.
|
|
|
|
|
text_offset.translate_by(-text_width, 0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-04 21:31:35 +00:00
|
|
|
|
path.move_to(text_offset);
|
|
|
|
|
path.text(text_utf8, font);
|
2023-10-29 19:11:46 +00:00
|
|
|
|
}
|
2023-11-04 21:31:35 +00:00
|
|
|
|
|
|
|
|
|
auto path_bounding_box = to_css_pixels_transform.map(path.bounding_box()).to_type<CSSPixels>();
|
|
|
|
|
// Stroke increases the path's size by stroke_width/2 per side.
|
|
|
|
|
CSSPixels stroke_width = CSSPixels::nearest_value_for(graphics_box.dom_node().visible_stroke_width() * viewbox_transform.x_scale());
|
|
|
|
|
path_bounding_box.inflate(stroke_width, stroke_width);
|
|
|
|
|
graphics_box_state.set_content_offset(path_bounding_box.top_left());
|
|
|
|
|
graphics_box_state.set_content_width(path_bounding_box.width());
|
|
|
|
|
graphics_box_state.set_content_height(path_bounding_box.height());
|
|
|
|
|
graphics_box_state.set_computed_svg_path(move(path));
|
2023-04-18 19:32:00 +02:00
|
|
|
|
} else if (is<SVGSVGBox>(descendant)) {
|
2023-08-19 19:42:31 +02:00
|
|
|
|
SVGFormattingContext nested_context(m_state, static_cast<SVGSVGBox const&>(descendant), this);
|
|
|
|
|
nested_context.run(static_cast<SVGSVGBox const&>(descendant), layout_mode, available_space);
|
2021-09-18 00:06:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
2023-08-16 17:45:49 +02:00
|
|
|
|
|
|
|
|
|
// https://svgwg.org/svg2-draft/struct.html#Groups
|
|
|
|
|
// 5.2. Grouping: the ‘g’ element
|
|
|
|
|
// The ‘g’ element is a container element for grouping together related graphics elements.
|
|
|
|
|
box.for_each_in_subtree_of_type<Box>([&](Box const& descendant) {
|
2023-11-12 20:51:23 +00:00
|
|
|
|
if (is_container_element(descendant)) {
|
2023-11-12 20:47:39 +00:00
|
|
|
|
Gfx::BoundingBox<CSSPixels> bounding_box;
|
2023-08-16 17:45:49 +02:00
|
|
|
|
descendant.for_each_in_subtree_of_type<Box>([&](Box const& child_of_svg_container) {
|
2023-11-12 20:47:39 +00:00
|
|
|
|
auto& box_state = m_state.get(child_of_svg_container);
|
|
|
|
|
bounding_box.add_point(box_state.offset);
|
|
|
|
|
bounding_box.add_point(box_state.offset.translated(box_state.content_width(), box_state.content_height()));
|
2023-08-16 17:45:49 +02:00
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-12 20:51:23 +00:00
|
|
|
|
auto& box_state = m_state.get_mutable(descendant);
|
|
|
|
|
box_state.set_content_x(bounding_box.x());
|
|
|
|
|
box_state.set_content_y(bounding_box.y());
|
|
|
|
|
box_state.set_content_width(bounding_box.width());
|
|
|
|
|
box_state.set_content_height(bounding_box.height());
|
2023-08-16 17:45:49 +02:00
|
|
|
|
}
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
2021-09-17 23:12:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|