mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:37:35 +00:00
LibWeb: Create LayoutNodes for each SVG element
This brings the SVG API closer to the rest of LibWeb
This commit is contained in:
parent
f2055bb509
commit
455ce0b9c3
16 changed files with 402 additions and 122 deletions
|
@ -32,7 +32,7 @@ class SVGContext {
|
|||
public:
|
||||
SVGContext()
|
||||
{
|
||||
push_state();
|
||||
m_states.append(State());
|
||||
}
|
||||
|
||||
const Gfx::Color& fill_color() const { return state().fill_color; }
|
||||
|
@ -43,9 +43,8 @@ public:
|
|||
void set_stroke_color(Gfx::Color color) { state().stroke_color = color; }
|
||||
void set_stroke_width(float width) { state().stroke_width = width; }
|
||||
|
||||
void push_state() { m_states.append(State()); }
|
||||
|
||||
void pop_state() { m_states.take_last(); }
|
||||
void save() { m_states.append(m_states.last()); }
|
||||
void restore() { m_states.take_last(); }
|
||||
|
||||
private:
|
||||
struct State {
|
||||
|
|
|
@ -48,13 +48,4 @@ void SVGGraphicsElement::parse_attribute(const FlyString& name, const String& va
|
|||
}
|
||||
}
|
||||
|
||||
SVGPaintingContext SVGGraphicsElement::make_painting_context_from(const SVGPaintingContext& context)
|
||||
{
|
||||
return SVGPaintingContext {
|
||||
m_fill_color.value_or(context.fill_color),
|
||||
m_stroke_color.value_or(context.stroke_color),
|
||||
m_stroke_width.value_or(context.stroke_width),
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,18 +33,6 @@
|
|||
|
||||
namespace Web::SVG {
|
||||
|
||||
struct SVGPaintingContext {
|
||||
Gfx::Color fill_color;
|
||||
Gfx::Color stroke_color;
|
||||
float stroke_width;
|
||||
};
|
||||
|
||||
static const SVGPaintingContext default_painting_context = {
|
||||
Gfx::Color::Black,
|
||||
Gfx::Color::Black,
|
||||
1.0f
|
||||
};
|
||||
|
||||
class SVGGraphicsElement : public SVGElement {
|
||||
public:
|
||||
using WrapperType = Bindings::SVGGraphicsElementWrapper;
|
||||
|
@ -53,9 +41,9 @@ public:
|
|||
|
||||
virtual void parse_attribute(const FlyString& name, const String& value) override;
|
||||
|
||||
virtual void paint(Gfx::Painter& painter, const SVGPaintingContext& context) = 0;
|
||||
|
||||
SVGPaintingContext make_painting_context_from(const SVGPaintingContext& context);
|
||||
const Optional<Gfx::Color>& fill_color() const { return m_fill_color; }
|
||||
const Optional<Gfx::Color>& stroke_color() const { return m_stroke_color; }
|
||||
const Optional<float>& stroke_width() const { return m_stroke_width; }
|
||||
|
||||
protected:
|
||||
Optional<Gfx::Color> m_fill_color;
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include <LibGfx/Path.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/Event.h>
|
||||
#include <LibWeb/Layout/LayoutSVGPath.h>
|
||||
#include <LibWeb/SVG/SVGPathElement.h>
|
||||
#include <ctype.h>
|
||||
|
||||
|
@ -429,6 +430,14 @@ SVGPathElement::SVGPathElement(DOM::Document& document, const FlyString& tag_nam
|
|||
{
|
||||
}
|
||||
|
||||
RefPtr<LayoutNode> SVGPathElement::create_layout_node(const CSS::StyleProperties* parent_style)
|
||||
{
|
||||
auto style = document().style_resolver().resolve_style(*this, parent_style);
|
||||
if (style->display() == CSS::Display::None)
|
||||
return nullptr;
|
||||
return adopt(*new LayoutSVGPath(document(), *this, move(style)));
|
||||
}
|
||||
|
||||
void SVGPathElement::parse_attribute(const FlyString& name, const String& value)
|
||||
{
|
||||
SVGGeometryElement::parse_attribute(name, value);
|
||||
|
@ -437,8 +446,11 @@ void SVGPathElement::parse_attribute(const FlyString& name, const String& value)
|
|||
m_instructions = PathDataParser(value).parse();
|
||||
}
|
||||
|
||||
void SVGPathElement::paint(Gfx::Painter& painter, const SVGPaintingContext& context)
|
||||
Gfx::Path& SVGPathElement::get_path()
|
||||
{
|
||||
if (m_path.has_value())
|
||||
return m_path.value();
|
||||
|
||||
Gfx::Path path;
|
||||
|
||||
for (auto& instruction : m_instructions) {
|
||||
|
@ -640,15 +652,8 @@ void SVGPathElement::paint(Gfx::Painter& painter, const SVGPaintingContext& cont
|
|||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
auto closed_path = path;
|
||||
closed_path.close();
|
||||
|
||||
// Fills are computed as though all paths are closed (https://svgwg.org/svg2-draft/painting.html#FillProperties)
|
||||
painter.fill_path(closed_path, m_fill_color.value_or(context.fill_color), Gfx::Painter::WindingRule::EvenOdd);
|
||||
painter.stroke_path(path, m_stroke_color.value_or(context.stroke_color), m_stroke_width.value_or(context.stroke_width));
|
||||
m_path = path;
|
||||
return m_path.value();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -109,12 +109,16 @@ public:
|
|||
SVGPathElement(DOM::Document&, const FlyString& tag_name);
|
||||
virtual ~SVGPathElement() override = default;
|
||||
|
||||
virtual RefPtr<LayoutNode> create_layout_node(const CSS::StyleProperties* parent_style) override;
|
||||
|
||||
virtual void parse_attribute(const FlyString& name, const String& value) override;
|
||||
virtual void paint(Gfx::Painter& painter, const SVGPaintingContext& context) override;
|
||||
|
||||
Gfx::Path& get_path();
|
||||
|
||||
private:
|
||||
Vector<PathInstruction> m_instructions;
|
||||
Gfx::FloatPoint m_previous_control_point = {};
|
||||
Optional<Gfx::Path> m_path;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -28,15 +28,13 @@
|
|||
#include <LibWeb/CSS/StyleResolver.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/Event.h>
|
||||
#include <LibWeb/Layout/LayoutSVG.h>
|
||||
#include <LibWeb/Layout/LayoutSVGSVG.h>
|
||||
#include <LibWeb/SVG/SVGPathElement.h>
|
||||
#include <LibWeb/SVG/SVGSVGElement.h>
|
||||
#include <ctype.h>
|
||||
|
||||
namespace Web::SVG {
|
||||
|
||||
static constexpr auto max_svg_area = 16384 * 16384;
|
||||
|
||||
SVGSVGElement::SVGSVGElement(DOM::Document& document, const FlyString& tag_name)
|
||||
: SVGGraphicsElement(document, tag_name)
|
||||
{
|
||||
|
@ -47,43 +45,7 @@ RefPtr<LayoutNode> SVGSVGElement::create_layout_node(const CSS::StyleProperties*
|
|||
auto style = document().style_resolver().resolve_style(*this, parent_style);
|
||||
if (style->display() == CSS::Display::None)
|
||||
return nullptr;
|
||||
return adopt(*new LayoutSVG(document(), *this, move(style)));
|
||||
}
|
||||
|
||||
static Gfx::IntSize bitmap_size_for_canvas(const SVGSVGElement& canvas)
|
||||
{
|
||||
auto width = canvas.width();
|
||||
auto height = canvas.height();
|
||||
|
||||
Checked<size_t> area = width;
|
||||
area *= height;
|
||||
|
||||
if (area.has_overflow()) {
|
||||
dbg() << "Refusing to create " << width << "x" << height << " svg (overflow)";
|
||||
return {};
|
||||
}
|
||||
if (area.value() > max_svg_area) {
|
||||
dbg() << "Refusing to create " << width << "x" << height << " svg (exceeds maximum size)";
|
||||
return {};
|
||||
}
|
||||
return Gfx::IntSize(width, height);
|
||||
}
|
||||
|
||||
bool SVGSVGElement::create_bitmap_as_top_level_svg_element()
|
||||
{
|
||||
auto size = bitmap_size_for_canvas(*this);
|
||||
if (size.is_empty()) {
|
||||
m_bitmap = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_bitmap || m_bitmap->size() != size)
|
||||
m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, size);
|
||||
|
||||
Gfx::Painter painter(*m_bitmap);
|
||||
paint(painter, make_painting_context_from(default_painting_context));
|
||||
|
||||
return m_bitmap;
|
||||
return adopt(*new LayoutSVGSVG(document(), *this, move(style)));
|
||||
}
|
||||
|
||||
unsigned SVGSVGElement::width() const
|
||||
|
@ -96,13 +58,4 @@ unsigned SVGSVGElement::height() const
|
|||
return attribute(HTML::AttributeNames::height).to_uint().value_or(150);
|
||||
}
|
||||
|
||||
void SVGSVGElement::paint(Gfx::Painter& painter, const SVGPaintingContext& context)
|
||||
{
|
||||
for_each_child([&](Node& child) {
|
||||
if (is<SVGGraphicsElement>(child)) {
|
||||
downcast<SVGGraphicsElement>(child).paint(painter, make_painting_context_from(context));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -39,11 +39,6 @@ public:
|
|||
|
||||
virtual RefPtr<LayoutNode> create_layout_node(const CSS::StyleProperties* parent_style) override;
|
||||
|
||||
const RefPtr<Gfx::Bitmap> bitmap() const { return m_bitmap; }
|
||||
bool create_bitmap_as_top_level_svg_element();
|
||||
|
||||
virtual void paint(Gfx::Painter& painter, const SVGPaintingContext& context) override;
|
||||
|
||||
unsigned width() const;
|
||||
unsigned height() const;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue