2022-03-10 14:02:25 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2023-04-10 12:32:55 +01:00
|
|
|
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
2022-03-10 14:02:25 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibGfx/AntiAliasingPainter.h>
|
|
|
|
#include <LibWeb/Layout/ImageBox.h>
|
|
|
|
#include <LibWeb/Painting/SVGGeometryPaintable.h>
|
|
|
|
#include <LibWeb/SVG/SVGSVGElement.h>
|
|
|
|
|
|
|
|
namespace Web::Painting {
|
|
|
|
|
2023-01-11 12:51:49 +01:00
|
|
|
JS::NonnullGCPtr<SVGGeometryPaintable> SVGGeometryPaintable::create(Layout::SVGGeometryBox const& layout_box)
|
2022-03-10 14:02:25 +01:00
|
|
|
{
|
2023-01-11 12:51:49 +01:00
|
|
|
return layout_box.heap().allocate_without_realm<SVGGeometryPaintable>(layout_box);
|
2022-03-10 14:02:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SVGGeometryPaintable::SVGGeometryPaintable(Layout::SVGGeometryBox const& layout_box)
|
|
|
|
: SVGGraphicsPaintable(layout_box)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Layout::SVGGeometryBox const& SVGGeometryPaintable::layout_box() const
|
|
|
|
{
|
2022-03-10 15:50:57 +01:00
|
|
|
return static_cast<Layout::SVGGeometryBox const&>(layout_node());
|
2022-03-10 14:02:25 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 12:35:33 +01:00
|
|
|
Optional<HitTestResult> SVGGeometryPaintable::hit_test(CSSPixelPoint position, HitTestType type) const
|
|
|
|
{
|
|
|
|
auto result = SVGGraphicsPaintable::hit_test(position, type);
|
2023-10-29 17:08:35 +00:00
|
|
|
if (!result.has_value() || !path_data().has_value())
|
|
|
|
return {};
|
|
|
|
auto transformed_bounding_box = path_data()->svg_to_css_pixels_transform().map_to_quad(path_data()->computed_path().bounding_box());
|
|
|
|
if (!transformed_bounding_box.contains(position.to_type<float>()))
|
2023-04-10 12:35:33 +01:00
|
|
|
return {};
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-06-11 16:43:46 +01:00
|
|
|
static Gfx::Painter::WindingRule to_gfx_winding_rule(SVG::FillRule fill_rule)
|
|
|
|
{
|
|
|
|
switch (fill_rule) {
|
|
|
|
case SVG::FillRule::Nonzero:
|
|
|
|
return Gfx::Painter::WindingRule::Nonzero;
|
|
|
|
case SVG::FillRule::Evenodd:
|
|
|
|
return Gfx::Painter::WindingRule::EvenOdd;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 14:02:25 +01:00
|
|
|
void SVGGeometryPaintable::paint(PaintContext& context, PaintPhase phase) const
|
|
|
|
{
|
2023-10-29 17:08:35 +00:00
|
|
|
if (!is_visible() || !path_data().has_value())
|
2022-03-10 14:02:25 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
SVGGraphicsPaintable::paint(context, phase);
|
|
|
|
|
|
|
|
if (phase != PaintPhase::Foreground)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto& geometry_element = layout_box().dom_node();
|
|
|
|
|
2023-07-01 21:24:04 +01:00
|
|
|
auto const* svg_element = geometry_element.shadow_including_first_ancestor_of_type<SVG::SVGSVGElement>();
|
|
|
|
auto svg_element_rect = svg_element->paintable_box()->absolute_rect();
|
|
|
|
|
2023-04-10 12:34:39 +01:00
|
|
|
// FIXME: This should not be trucated to an int.
|
2023-10-15 04:27:48 +02:00
|
|
|
RecordingPainterStateSaver save_painter { context.painter() };
|
|
|
|
|
2023-07-01 21:24:04 +01:00
|
|
|
auto offset = context.floored_device_point(svg_element_rect.location()).to_type<int>().to_type<float>();
|
2023-07-31 18:37:44 +02:00
|
|
|
auto maybe_view_box = geometry_element.view_box();
|
2022-03-10 14:02:25 +01:00
|
|
|
|
2023-10-29 17:08:35 +00:00
|
|
|
auto paint_transform = path_data()->svg_to_device_pixels_transform(context, context.svg_transform());
|
|
|
|
Gfx::Path path = path_data()->computed_path().copy_transformed(paint_transform);
|
2022-03-10 14:02:25 +01:00
|
|
|
|
2023-06-24 16:49:28 +01:00
|
|
|
// Fills are computed as though all subpaths are closed (https://svgwg.org/svg2-draft/painting.html#FillProperties)
|
2023-04-23 01:31:17 +01:00
|
|
|
auto closed_path = [&] {
|
2022-03-10 14:02:25 +01:00
|
|
|
// We need to fill the path before applying the stroke, however the filled
|
|
|
|
// path must be closed, whereas the stroke path may not necessary be closed.
|
|
|
|
// Copy the path and close it for filling, but use the previous path for stroke
|
2023-04-23 01:31:17 +01:00
|
|
|
auto copy = path;
|
2023-06-24 16:49:28 +01:00
|
|
|
copy.close_all_subpaths();
|
2023-04-23 01:31:17 +01:00
|
|
|
return copy;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note: This is assuming .x_scale() == .y_scale() (which it does currently).
|
|
|
|
auto viewbox_scale = paint_transform.x_scale();
|
|
|
|
|
|
|
|
auto svg_viewport = [&] {
|
|
|
|
if (maybe_view_box.has_value())
|
|
|
|
return Gfx::FloatRect { maybe_view_box->min_x, maybe_view_box->min_y, maybe_view_box->width, maybe_view_box->height };
|
2023-08-07 22:12:21 +01:00
|
|
|
return Gfx::FloatRect { { 0, 0 }, svg_element_rect.size().to_type<float>() };
|
2023-04-23 01:31:17 +01:00
|
|
|
}();
|
|
|
|
|
|
|
|
SVG::SVGPaintContext paint_context {
|
|
|
|
.viewport = svg_viewport,
|
2023-10-29 17:08:35 +00:00
|
|
|
.path_bounding_box = path_data()->computed_path().bounding_box(),
|
2023-04-23 01:31:17 +01:00
|
|
|
.transform = paint_transform
|
|
|
|
};
|
|
|
|
|
2023-07-01 21:24:04 +01:00
|
|
|
auto fill_opacity = geometry_element.fill_opacity().value_or(1);
|
|
|
|
auto winding_rule = to_gfx_winding_rule(geometry_element.fill_rule().value_or(SVG::FillRule::Nonzero));
|
2023-04-23 01:31:17 +01:00
|
|
|
if (auto paint_style = geometry_element.fill_paint_style(paint_context); paint_style.has_value()) {
|
2023-10-15 04:27:48 +02:00
|
|
|
context.painter().fill_path({
|
|
|
|
.path = closed_path(),
|
|
|
|
.paint_style = *paint_style,
|
|
|
|
.winding_rule = winding_rule,
|
|
|
|
.opacity = fill_opacity,
|
|
|
|
.translation = offset,
|
|
|
|
});
|
2023-07-01 21:24:04 +01:00
|
|
|
} else if (auto fill_color = geometry_element.fill_color(); fill_color.has_value()) {
|
2023-10-15 04:27:48 +02:00
|
|
|
context.painter().fill_path({
|
|
|
|
.path = closed_path(),
|
|
|
|
.color = fill_color->with_opacity(fill_opacity),
|
|
|
|
.winding_rule = winding_rule,
|
|
|
|
.translation = offset,
|
|
|
|
});
|
2022-03-10 14:02:25 +01:00
|
|
|
}
|
|
|
|
|
2023-07-01 21:24:04 +01:00
|
|
|
auto stroke_opacity = geometry_element.stroke_opacity().value_or(1);
|
2023-06-06 20:40:10 +01:00
|
|
|
|
|
|
|
// Note: This is assuming .x_scale() == .y_scale() (which it does currently).
|
2023-07-01 21:24:04 +01:00
|
|
|
float stroke_thickness = geometry_element.stroke_width().value_or(1) * viewbox_scale;
|
2023-06-06 20:40:10 +01:00
|
|
|
|
|
|
|
if (auto paint_style = geometry_element.stroke_paint_style(paint_context); paint_style.has_value()) {
|
2023-10-15 04:27:48 +02:00
|
|
|
context.painter().stroke_path({
|
|
|
|
.path = path,
|
|
|
|
.paint_style = *paint_style,
|
|
|
|
.thickness = stroke_thickness,
|
|
|
|
.opacity = stroke_opacity,
|
|
|
|
.translation = offset,
|
|
|
|
});
|
2023-07-01 21:24:04 +01:00
|
|
|
} else if (auto stroke_color = geometry_element.stroke_color(); stroke_color.has_value()) {
|
2023-10-15 04:27:48 +02:00
|
|
|
context.painter().stroke_path({
|
|
|
|
.path = path,
|
|
|
|
.color = stroke_color->with_opacity(stroke_opacity),
|
|
|
|
.thickness = stroke_thickness,
|
|
|
|
.translation = offset,
|
|
|
|
});
|
2022-03-10 14:02:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|