mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 14:28:12 +00:00
LibWeb: Implement painting for svg text
The implementation of painting for SVG text follows the same pattern as the implementation of painting for SVG geometries. However, instead of reusing the existing PaintableWithLines to draw text, a new class called SVGTextPaintable is introduced. because everything that is painted inside an SVG is expected to inherit from SVGGraphicsPaintable. Therefore reusing the text painting from regular text nodes would require significant refactoring.
This commit is contained in:
parent
81a6976e90
commit
0d8d7ae94e
9 changed files with 216 additions and 0 deletions
56
Userland/Libraries/LibWeb/Painting/SVGTextPaintable.cpp
Normal file
56
Userland/Libraries/LibWeb/Painting/SVGTextPaintable.cpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* 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)
|
||||
{
|
||||
}
|
||||
|
||||
Optional<HitTestResult> SVGTextPaintable::hit_test(CSSPixelPoint position, HitTestType type) const
|
||||
{
|
||||
(void)position;
|
||||
(void)type;
|
||||
return {};
|
||||
}
|
||||
|
||||
void SVGTextPaintable::paint(PaintContext& context, PaintPhase phase) const
|
||||
{
|
||||
if (!is_visible())
|
||||
return;
|
||||
|
||||
SVGGraphicsPaintable::paint(context, phase);
|
||||
|
||||
if (phase != PaintPhase::Foreground)
|
||||
return;
|
||||
|
||||
auto& painter = context.painter();
|
||||
|
||||
Gfx::PainterStateSaver save_painter { painter };
|
||||
auto& svg_context = context.svg_context();
|
||||
auto svg_context_offset = context.floored_device_point(svg_context.svg_element_position()).to_type<int>();
|
||||
painter.translate(svg_context_offset);
|
||||
|
||||
auto const& dom_node = layout_box().dom_node();
|
||||
|
||||
auto child_text_content = dom_node.child_text_content();
|
||||
|
||||
auto transform = layout_box().layout_transform();
|
||||
auto& scaled_font = layout_node().scaled_font(context);
|
||||
auto text_offset = context.floored_device_point(dom_node.get_offset().transformed(*transform).to_type<CSSPixels>());
|
||||
painter.draw_text_run(text_offset.to_type<int>(), Utf8View { child_text_content }, scaled_font, layout_node().computed_values().fill()->as_color());
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue