SVGTextPaintable.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. void SVGTextPaintable::paint(PaintContext& context, PaintPhase phase) const
  18. {
  19. if (!is_visible())
  20. return;
  21. if (!layout_node().computed_values().fill().has_value())
  22. return;
  23. if (layout_node().computed_values().fill()->is_url()) {
  24. dbgln("FIXME: Using url() as fill is not supported for svg text");
  25. return;
  26. }
  27. SVGGraphicsPaintable::paint(context, phase);
  28. if (phase != PaintPhase::Foreground)
  29. return;
  30. auto& painter = context.painter();
  31. auto const& dom_node = layout_box().dom_node();
  32. auto paint_transform = computed_transforms().svg_to_device_pixels_transform(context);
  33. auto& scaled_font = layout_box().scaled_font(paint_transform.x_scale());
  34. auto text_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>();
  35. auto text_contents = dom_node.text_contents();
  36. painter.draw_text_run(text_rect.bottom_left(), Utf8View { text_contents }, scaled_font, layout_node().computed_values().fill()->as_color(), text_rect);
  37. }
  38. }