1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 05: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

@ -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&);
};
}