SVGTextBox.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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::SVGTextPositioningElement& 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. // FIXME: Since text layout boxes are currently 0x0 it is not possible handle viewBox scaling here.
  24. auto& geometry_element = dom_node();
  25. auto transform = geometry_element.get_transform();
  26. auto* svg_box = geometry_element.first_ancestor_of_type<SVG::SVGSVGElement>();
  27. auto origin = viewbox_origin().to_type<double>().to_type<float>();
  28. Gfx::FloatPoint paint_offset = {};
  29. if (svg_box && svg_box->view_box().has_value())
  30. paint_offset = svg_box->paintable_box()->absolute_rect().location().to_type<double>().to_type<float>();
  31. return Gfx::AffineTransform {}.translate(paint_offset).translate(-origin).multiply(transform);
  32. }
  33. JS::GCPtr<Painting::Paintable> SVGTextBox::create_paintable() const
  34. {
  35. return Painting::SVGTextPaintable::create(*this);
  36. }
  37. }