From 48d03a68e9fbca28e41afee29ed4b55fefdfd74a Mon Sep 17 00:00:00 2001 From: MacDue Date: Thu, 20 Jul 2023 20:31:01 +0100 Subject: [PATCH] LibWeb: Allow font-size as an SVG attribute and handle font scaling --- .../Libraries/LibWeb/Painting/SVGTextPaintable.cpp | 11 +++++++++-- Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/Painting/SVGTextPaintable.cpp b/Userland/Libraries/LibWeb/Painting/SVGTextPaintable.cpp index 262ce2a295..d764c74b4f 100644 --- a/Userland/Libraries/LibWeb/Painting/SVGTextPaintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/SVGTextPaintable.cpp @@ -59,9 +59,16 @@ void SVGTextPaintable::paint(PaintContext& context, PaintPhase phase) const auto child_text_content = dom_node.child_text_content(); auto transform = layout_box().layout_transform(); - auto& scaled_font = layout_node().scaled_font(context); + if (!transform.has_value()) + return; + + // FIXME: Support arbitrary path transforms for fonts. + // FIMXE: This assumes transform->x_scale() == transform->y_scale(). + auto& scaled_font = layout_node().scaled_font(static_cast(context.device_pixels_per_css_pixel()) * transform->x_scale()); + + Utf8View text_content { child_text_content }; auto text_offset = context.floored_device_point(dom_node.get_offset().transformed(*transform).to_type()); - painter.draw_text_run(text_offset.to_type(), Utf8View { child_text_content }, scaled_font, layout_node().computed_values().fill()->as_color()); + painter.draw_text_run(text_offset.to_type(), text_content, scaled_font, layout_node().computed_values().fill()->as_color()); } } diff --git a/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp b/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp index 41d54983d5..45b46a5727 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp +++ b/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp @@ -142,6 +142,12 @@ void SVGGraphicsElement::apply_presentational_hints(CSS::StyleProperties& style) } else if (name.equals_ignoring_ascii_case(SVG::AttributeNames::opacity)) { if (auto stroke_opacity_value = parse_css_value(parsing_context, value, CSS::PropertyID::Opacity).release_value_but_fixme_should_propagate_errors()) style.set_property(CSS::PropertyID::Opacity, stroke_opacity_value.release_nonnull()); + } else if (name.equals_ignoring_ascii_case("text-anchor"sv)) { + if (auto text_anchor_value = parse_css_value(parsing_context, value, CSS::PropertyID::TextAnchor).release_value_but_fixme_should_propagate_errors()) + style.set_property(CSS::PropertyID::TextAnchor, text_anchor_value.release_nonnull()); + } else if (name.equals_ignoring_ascii_case("font-size"sv)) { + if (auto font_size_value = parse_css_value(parsing_context, value, CSS::PropertyID::FontSize).release_value_but_fixme_should_propagate_errors()) + style.set_property(CSS::PropertyID::FontSize, font_size_value.release_nonnull()); } }); }