SVGGeometryBox.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Layout/SVGGeometryBox.h>
  8. #include <LibWeb/Painting/SVGGeometryPaintable.h>
  9. #include <LibWeb/SVG/SVGPathElement.h>
  10. #include <LibWeb/SVG/SVGSVGElement.h>
  11. namespace Web::Layout {
  12. SVGGeometryBox::SVGGeometryBox(DOM::Document& document, SVG::SVGGeometryElement& element, NonnullRefPtr<CSS::StyleProperties> properties)
  13. : SVGGraphicsBox(document, element, properties)
  14. {
  15. }
  16. CSSPixelPoint SVGGeometryBox::viewbox_origin() const
  17. {
  18. auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
  19. if (!svg_box || !svg_box->view_box().has_value())
  20. return { 0, 0 };
  21. return { svg_box->view_box().value().min_x, svg_box->view_box().value().min_y };
  22. }
  23. Optional<Gfx::AffineTransform> SVGGeometryBox::layout_transform() const
  24. {
  25. auto& geometry_element = dom_node();
  26. auto transform = geometry_element.get_transform();
  27. auto* svg_box = geometry_element.first_ancestor_of_type<SVG::SVGSVGElement>();
  28. float scaling = 1;
  29. auto origin = viewbox_origin().to_type<float>();
  30. Gfx::FloatPoint paint_offset = {};
  31. if (svg_box && svg_box->view_box().has_value()) {
  32. // Note: SVGFormattingContext has already done the scaling based on the viewbox,
  33. // we now have to derive what it was from the original bounding box size.
  34. // FIXME: It would be nice if we could store the transform from layout somewhere, so we don't have to solve for it here.
  35. auto original_bounding_box = Gfx::AffineTransform {}.translate(-origin).multiply(transform).map(const_cast<SVG::SVGGeometryElement&>(geometry_element).get_path().bounding_box());
  36. float stroke_width = geometry_element.visible_stroke_width();
  37. original_bounding_box.inflate(stroke_width, stroke_width);
  38. // If the transform (or path) results in a empty box we can't display this.
  39. if (original_bounding_box.is_empty())
  40. return {};
  41. auto scaled_width = paintable_box()->content_width().value();
  42. auto scaled_height = paintable_box()->content_height().value();
  43. scaling = min(scaled_width / original_bounding_box.width(), scaled_height / original_bounding_box.height());
  44. auto scaled_bounding_box = original_bounding_box.scaled(scaling, scaling);
  45. paint_offset = (paintable_box()->absolute_rect().location() - svg_box->paintable_box()->absolute_rect().location()).to_type<float>() - scaled_bounding_box.location();
  46. }
  47. return Gfx::AffineTransform {}.translate(paint_offset).scale(scaling, scaling).translate(-origin).multiply(transform);
  48. }
  49. JS::GCPtr<Painting::Paintable> SVGGeometryBox::create_paintable() const
  50. {
  51. return Painting::SVGGeometryPaintable::create(*this);
  52. }
  53. }