SVGTextPaintable.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. SVGGraphicsPaintable::paint(context, phase);
  28. if (phase != PaintPhase::Foreground)
  29. return;
  30. auto& painter = context.painter();
  31. Gfx::PainterStateSaver save_painter { painter };
  32. auto& svg_context = context.svg_context();
  33. auto svg_context_offset = context.floored_device_point(svg_context.svg_element_position()).to_type<int>();
  34. painter.translate(svg_context_offset);
  35. auto const& dom_node = layout_box().dom_node();
  36. auto child_text_content = dom_node.child_text_content();
  37. auto transform = layout_box().layout_transform();
  38. auto& scaled_font = layout_node().scaled_font(context);
  39. auto text_offset = context.floored_device_point(dom_node.get_offset().transformed(*transform).to_type<CSSPixels>());
  40. painter.draw_text_run(text_offset.to_type<int>(), Utf8View { child_text_content }, scaled_font, layout_node().computed_values().fill()->as_color());
  41. }
  42. }