SVGTextBox.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Layout/SVGTextBox.h>
  7. #include <LibWeb/Painting/SVGTextPaintable.h>
  8. #include <LibWeb/SVG/SVGSVGElement.h>
  9. namespace Web::Layout {
  10. SVGTextBox::SVGTextBox(DOM::Document& document, SVG::SVGTextContentElement& element, NonnullRefPtr<CSS::StyleProperties> properties)
  11. : SVGGraphicsBox(document, element, properties)
  12. {
  13. }
  14. CSSPixelPoint SVGTextBox::viewbox_origin() const
  15. {
  16. auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
  17. if (!svg_box || !svg_box->view_box().has_value())
  18. return { 0, 0 };
  19. return { svg_box->view_box().value().min_x, svg_box->view_box().value().min_y };
  20. }
  21. Optional<Gfx::AffineTransform> SVGTextBox::layout_transform() const
  22. {
  23. auto& geometry_element = dom_node();
  24. auto transform = geometry_element.get_transform();
  25. auto* svg_box = geometry_element.first_ancestor_of_type<SVG::SVGSVGElement>();
  26. auto origin = viewbox_origin().to_type<double>().to_type<float>();
  27. Gfx::FloatPoint paint_offset = {};
  28. if (svg_box && svg_box->view_box().has_value())
  29. paint_offset = svg_box->paintable_box()->absolute_rect().location().to_type<double>().to_type<float>();
  30. return Gfx::AffineTransform {}.translate(paint_offset).translate(-origin).multiply(transform);
  31. }
  32. JS::GCPtr<Painting::Paintable> SVGTextBox::create_paintable() const
  33. {
  34. return Painting::SVGTextPaintable::create(*this);
  35. }
  36. }