SVGTextPaintable.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Painting/SVGTextPaintable.h>
  7. #include <LibWeb/SVG/SVGSVGElement.h>
  8. namespace Web::Painting {
  9. JS::NonnullGCPtr<SVGTextPaintable> SVGTextPaintable::create(Layout::SVGTextBox const& layout_box)
  10. {
  11. return layout_box.heap().allocate_without_realm<SVGTextPaintable>(layout_box);
  12. }
  13. SVGTextPaintable::SVGTextPaintable(Layout::SVGTextBox const& layout_box)
  14. : SVGGraphicsPaintable(layout_box)
  15. {
  16. }
  17. Optional<HitTestResult> SVGTextPaintable::hit_test(CSSPixelPoint position, HitTestType type) const
  18. {
  19. (void)position;
  20. (void)type;
  21. return {};
  22. }
  23. void SVGTextPaintable::paint(PaintContext& context, PaintPhase phase) const
  24. {
  25. if (!is_visible())
  26. return;
  27. if (!layout_node().computed_values().fill().has_value())
  28. return;
  29. if (layout_node().computed_values().fill()->is_url()) {
  30. dbgln("FIXME: Using url() as fill is not supported for svg text");
  31. return;
  32. }
  33. SVGGraphicsPaintable::paint(context, phase);
  34. if (phase != PaintPhase::Foreground)
  35. return;
  36. auto& painter = context.painter();
  37. auto& text_element = layout_box().dom_node();
  38. auto const* svg_element = text_element.shadow_including_first_ancestor_of_type<SVG::SVGSVGElement>();
  39. auto svg_element_rect = svg_element->paintable_box()->absolute_rect();
  40. Gfx::PainterStateSaver save_painter { painter };
  41. auto svg_context_offset = context.floored_device_point(svg_element_rect.location()).to_type<int>();
  42. painter.translate(svg_context_offset);
  43. auto const& dom_node = layout_box().dom_node();
  44. auto child_text_content = dom_node.child_text_content();
  45. auto transform = layout_box().layout_transform();
  46. if (!transform.has_value())
  47. return;
  48. // FIXME: Support arbitrary path transforms for fonts.
  49. // FIMXE: This assumes transform->x_scale() == transform->y_scale().
  50. auto& scaled_font = layout_node().scaled_font(static_cast<float>(context.device_pixels_per_css_pixel()) * transform->x_scale());
  51. Utf8View text_content { child_text_content };
  52. auto text_offset = context.floored_device_point(dom_node.get_offset().transformed(*transform).to_type<CSSPixels>());
  53. painter.draw_text_run(text_offset.to_type<int>(), text_content, scaled_font, layout_node().computed_values().fill()->as_color());
  54. }
  55. }