SVGGeometryBox.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 <LibGfx/AntiAliasingPainter.h>
  8. #include <LibGfx/Painter.h>
  9. #include <LibWeb/Layout/SVGGeometryBox.h>
  10. #include <LibWeb/Painting/SVGGeometryPaintable.h>
  11. #include <LibWeb/SVG/SVGPathElement.h>
  12. #include <LibWeb/SVG/SVGSVGElement.h>
  13. namespace Web::Layout {
  14. SVGGeometryBox::SVGGeometryBox(DOM::Document& document, SVG::SVGGeometryElement& element, NonnullRefPtr<CSS::StyleProperties> properties)
  15. : SVGGraphicsBox(document, element, properties)
  16. {
  17. }
  18. float SVGGeometryBox::viewbox_scaling() const
  19. {
  20. auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
  21. if (!svg_box || !svg_box->view_box().has_value())
  22. return 1;
  23. auto view_box = svg_box->view_box().value();
  24. bool has_specified_width = svg_box->has_attribute(HTML::AttributeNames::width);
  25. auto specified_width = paint_box()->content_width();
  26. bool has_specified_height = svg_box->has_attribute(HTML::AttributeNames::height);
  27. auto specified_height = paint_box()->content_height();
  28. auto scale_width = has_specified_width ? specified_width / view_box.width : 1;
  29. auto scale_height = has_specified_height ? specified_height / view_box.height : 1;
  30. return min(scale_width, scale_height);
  31. }
  32. Gfx::FloatPoint SVGGeometryBox::viewbox_origin() const
  33. {
  34. auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
  35. if (!svg_box || !svg_box->view_box().has_value())
  36. return { 0, 0 };
  37. return { svg_box->view_box().value().min_x, svg_box->view_box().value().min_y };
  38. }
  39. RefPtr<Painting::Paintable> SVGGeometryBox::create_paintable() const
  40. {
  41. return Painting::SVGGeometryPaintable::create(*this);
  42. }
  43. }