1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 07:27:45 +00:00

LibWeb: Support transforms, stroking, gradients, etc for SVG <text>

This makes use of the new Gfx::Path::text() to handle SVG text elements,
with this text is just a regular path, and can be manipulated like any
other graphics element.

This removes the SVGTextPaintable and makes both <text> and geometry
elements use a new (shared) SVGPathPaintable. This is identical to the
old SVGGeometryPaintable. This simplifies painting as once something is
resolved to a Gfx::Path, the painting logic is the same.
This commit is contained in:
MacDue 2023-11-04 21:31:35 +00:00 committed by Alexander Kalenik
parent 50d33f79fa
commit 4c5d48f861
30 changed files with 144 additions and 180 deletions

View file

@ -491,11 +491,10 @@ set(SOURCES
Painting/ProgressPaintable.cpp
Painting/RadioButtonPaintable.cpp
Painting/RecordingPainter.cpp
Painting/SVGGeometryPaintable.cpp
Painting/SVGPathPaintable.cpp
Painting/SVGGraphicsPaintable.cpp
Painting/SVGPaintable.cpp
Painting/SVGSVGPaintable.cpp
Painting/SVGTextPaintable.cpp
Painting/ShadowPainting.cpp
Painting/StackingContext.cpp
Painting/TableBordersPainting.cpp

View file

@ -10,7 +10,7 @@
#include <LibWeb/Layout/LayoutState.h>
#include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Painting/SVGGeometryPaintable.h>
#include <LibWeb/Painting/SVGPathPaintable.h>
namespace Web::Layout {
@ -272,8 +272,8 @@ void LayoutState::commit(Box& root)
svg_graphics_paintable.set_computed_transforms(*used_values.computed_svg_transforms());
}
if (used_values.computed_svg_path().has_value() && is<Painting::SVGGeometryPaintable>(paintable_box)) {
auto& svg_geometry_paintable = static_cast<Painting::SVGGeometryPaintable&>(paintable_box);
if (used_values.computed_svg_path().has_value() && is<Painting::SVGPathPaintable>(paintable_box)) {
auto& svg_geometry_paintable = static_cast<Painting::SVGPathPaintable&>(paintable_box);
svg_geometry_paintable.set_computed_path(move(*used_values.computed_svg_path()));
}
}

View file

@ -196,27 +196,20 @@ void SVGFormattingContext::run(Box const& box, LayoutMode layout_mode, Available
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);
Gfx::Path path;
if (is<SVGGeometryBox>(descendant)) {
auto path = static_cast<SVG::SVGGeometryElement&>(dom_node).get_path();
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));
path = static_cast<SVG::SVGGeometryElement&>(dom_node).get_path();
} else if (is<SVGTextBox>(descendant)) {
auto& text_element = static_cast<SVG::SVGTextPositioningElement&>(dom_node);
// FIXME: Support arbitrary path transforms for fonts.
// FIMXE: This assumes transform->x_scale() == transform->y_scale().
auto& scaled_font = graphics_box.scaled_font(to_css_pixels_transform.x_scale());
auto& font = graphics_box.font();
auto text_contents = text_element.text_contents();
Utf8View text_utf8 { text_contents };
auto text_width = font.width(text_utf8);
auto text_offset = text_element.get_offset().transformed(to_css_pixels_transform).to_type<CSSPixels>();
auto text_width = CSSPixels::nearest_value_for(scaled_font.width(Utf8View { text_contents }));
auto text_offset = text_element.get_offset();
// https://svgwg.org/svg2-draft/text.html#TextAnchoringProperties
switch (text_element.text_anchor().value_or(SVG::TextAnchor::Start)) {
case SVG::TextAnchor::Start:
@ -240,11 +233,18 @@ void SVGFormattingContext::run(Box const& box, LayoutMode layout_mode, Available
VERIFY_NOT_REACHED();
}
auto text_height = CSSPixels::nearest_value_for(scaled_font.pixel_size());
graphics_box_state.set_content_offset(text_offset.translated(0, -text_height));
graphics_box_state.set_content_width(text_width);
graphics_box_state.set_content_height(text_height);
path.move_to(text_offset);
path.text(text_utf8, font);
}
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));
} else if (is<SVGSVGBox>(descendant)) {
SVGFormattingContext nested_context(m_state, static_cast<SVGSVGBox const&>(descendant), this);
nested_context.run(static_cast<SVGSVGBox const&>(descendant), layout_mode, available_space);

View file

@ -6,7 +6,7 @@
*/
#include <LibWeb/Layout/SVGGeometryBox.h>
#include <LibWeb/Painting/SVGGeometryPaintable.h>
#include <LibWeb/Painting/SVGPathPaintable.h>
#include <LibWeb/SVG/SVGPathElement.h>
#include <LibWeb/SVG/SVGSVGElement.h>
@ -19,7 +19,7 @@ SVGGeometryBox::SVGGeometryBox(DOM::Document& document, SVG::SVGGeometryElement&
JS::GCPtr<Painting::Paintable> SVGGeometryBox::create_paintable() const
{
return Painting::SVGGeometryPaintable::create(*this);
return Painting::SVGPathPaintable::create(*this);
}
}

View file

@ -5,7 +5,7 @@
*/
#include <LibWeb/Layout/SVGTextBox.h>
#include <LibWeb/Painting/SVGTextPaintable.h>
#include <LibWeb/Painting/SVGPathPaintable.h>
#include <LibWeb/SVG/SVGSVGElement.h>
namespace Web::Layout {
@ -17,7 +17,7 @@ SVGTextBox::SVGTextBox(DOM::Document& document, SVG::SVGTextPositioningElement&
JS::GCPtr<Painting::Paintable> SVGTextBox::create_paintable() const
{
return Painting::SVGTextPaintable::create(*this);
return Painting::SVGPathPaintable::create(*this);
}
}

View file

@ -6,27 +6,27 @@
*/
#include <LibGfx/AntiAliasingPainter.h>
#include <LibWeb/Painting/SVGGeometryPaintable.h>
#include <LibWeb/Painting/SVGPathPaintable.h>
#include <LibWeb/SVG/SVGSVGElement.h>
namespace Web::Painting {
JS::NonnullGCPtr<SVGGeometryPaintable> SVGGeometryPaintable::create(Layout::SVGGeometryBox const& layout_box)
JS::NonnullGCPtr<SVGPathPaintable> SVGPathPaintable::create(Layout::SVGGraphicsBox const& layout_box)
{
return layout_box.heap().allocate_without_realm<SVGGeometryPaintable>(layout_box);
return layout_box.heap().allocate_without_realm<SVGPathPaintable>(layout_box);
}
SVGGeometryPaintable::SVGGeometryPaintable(Layout::SVGGeometryBox const& layout_box)
SVGPathPaintable::SVGPathPaintable(Layout::SVGGraphicsBox const& layout_box)
: SVGGraphicsPaintable(layout_box)
{
}
Layout::SVGGeometryBox const& SVGGeometryPaintable::layout_box() const
Layout::SVGGraphicsBox const& SVGPathPaintable::layout_box() const
{
return static_cast<Layout::SVGGeometryBox const&>(layout_node());
return static_cast<Layout::SVGGraphicsBox const&>(layout_node());
}
Optional<HitTestResult> SVGGeometryPaintable::hit_test(CSSPixelPoint position, HitTestType type) const
Optional<HitTestResult> SVGPathPaintable::hit_test(CSSPixelPoint position, HitTestType type) const
{
auto result = SVGGraphicsPaintable::hit_test(position, type);
if (!result.has_value() || !computed_path().has_value())
@ -49,7 +49,7 @@ static Gfx::Painter::WindingRule to_gfx_winding_rule(SVG::FillRule fill_rule)
}
}
void SVGGeometryPaintable::paint(PaintContext& context, PaintPhase phase) const
void SVGPathPaintable::paint(PaintContext& context, PaintPhase phase) const
{
if (!is_visible() || !computed_path().has_value())
return;

View file

@ -6,22 +6,22 @@
#pragma once
#include <LibWeb/Layout/SVGGeometryBox.h>
#include <LibWeb/Layout/SVGGraphicsBox.h>
#include <LibWeb/Painting/SVGGraphicsPaintable.h>
namespace Web::Painting {
class SVGGeometryPaintable final : public SVGGraphicsPaintable {
JS_CELL(SVGGeometryPaintable, SVGGraphicsPaintable);
class SVGPathPaintable final : public SVGGraphicsPaintable {
JS_CELL(SVGPathPaintable, SVGGraphicsPaintable);
public:
static JS::NonnullGCPtr<SVGGeometryPaintable> create(Layout::SVGGeometryBox const&);
static JS::NonnullGCPtr<SVGPathPaintable> create(Layout::SVGGraphicsBox const&);
virtual Optional<HitTestResult> hit_test(CSSPixelPoint, HitTestType) const override;
virtual void paint(PaintContext&, PaintPhase) const override;
Layout::SVGGeometryBox const& layout_box() const;
Layout::SVGGraphicsBox const& layout_box() const;
void set_computed_path(Gfx::Path path)
{
@ -31,7 +31,7 @@ public:
Optional<Gfx::Path> const& computed_path() const { return m_computed_path; }
protected:
SVGGeometryPaintable(Layout::SVGGeometryBox const&);
SVGPathPaintable(Layout::SVGGraphicsBox const&);
Optional<Gfx::Path> m_computed_path = {};
};

View file

@ -1,50 +0,0 @@
/*
* Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Painting/SVGTextPaintable.h>
#include <LibWeb/SVG/SVGSVGElement.h>
namespace Web::Painting {
JS::NonnullGCPtr<SVGTextPaintable> SVGTextPaintable::create(Layout::SVGTextBox const& layout_box)
{
return layout_box.heap().allocate_without_realm<SVGTextPaintable>(layout_box);
}
SVGTextPaintable::SVGTextPaintable(Layout::SVGTextBox const& layout_box)
: SVGGraphicsPaintable(layout_box)
{
}
void SVGTextPaintable::paint(PaintContext& context, PaintPhase phase) const
{
if (!is_visible())
return;
if (!layout_node().computed_values().fill().has_value())
return;
if (layout_node().computed_values().fill()->is_url()) {
dbgln("FIXME: Using url() as fill is not supported for svg text");
return;
}
SVGGraphicsPaintable::paint(context, phase);
if (phase != PaintPhase::Foreground)
return;
auto& painter = context.painter();
auto const& dom_node = layout_box().dom_node();
auto paint_transform = computed_transforms().svg_to_device_pixels_transform(context);
auto& scaled_font = layout_box().scaled_font(paint_transform.x_scale());
auto text_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>();
auto text_contents = dom_node.text_contents();
painter.draw_text_run(text_rect.bottom_left(), Utf8View { text_contents }, scaled_font, layout_node().computed_values().fill()->as_color(), text_rect);
}
}

View file

@ -1,31 +0,0 @@
/*
* Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Layout/SVGTextBox.h>
#include <LibWeb/Painting/SVGGraphicsPaintable.h>
namespace Web::Painting {
class SVGTextPaintable final : public SVGGraphicsPaintable {
JS_CELL(SVGTextPaintable, SVGGraphicsPaintable);
public:
static JS::NonnullGCPtr<SVGTextPaintable> create(Layout::SVGTextBox const&);
virtual void paint(PaintContext&, PaintPhase) const override;
Layout::SVGTextBox const& layout_box() const
{
return static_cast<Layout::SVGTextBox const&>(layout_node());
}
protected:
SVGTextPaintable(Layout::SVGTextBox const&);
};
}