SVGTextPaintable.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // FIXME: Once SVGFormattingContext does text layout this logic should move there.
  54. // https://svgwg.org/svg2-draft/text.html#TextAnchoringProperties
  55. switch (text_element.text_anchor().value_or(SVG::TextAnchor::Start)) {
  56. case SVG::TextAnchor::Start:
  57. // The rendered characters are aligned such that the start of the resulting rendered text is at the initial
  58. // current text position.
  59. break;
  60. case SVG::TextAnchor::Middle: {
  61. // The rendered characters are shifted such that the geometric middle of the resulting rendered text
  62. // (determined from the initial and final current text position before applying the text-anchor property)
  63. // is at the initial current text position.
  64. text_offset.translate_by(-scaled_font.width(text_content) / 2, 0);
  65. break;
  66. }
  67. case SVG::TextAnchor::End: {
  68. // The rendered characters are shifted such that the end of the resulting rendered text (final current text
  69. // position before applying the text-anchor property) is at the initial current text position.
  70. text_offset.translate_by(-scaled_font.width(text_content), 0);
  71. break;
  72. }
  73. default:
  74. VERIFY_NOT_REACHED();
  75. }
  76. painter.draw_text_run(text_offset.to_type<int>(), text_content, scaled_font, layout_node().computed_values().fill()->as_color());
  77. }
  78. }