SVGGeometryBox.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. float SVGGeometryBox::viewbox_scaling() 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 1;
  21. auto view_box = svg_box->view_box().value();
  22. bool has_specified_width = svg_box->has_attribute(HTML::AttributeNames::width);
  23. auto specified_width = paint_box()->content_width();
  24. bool has_specified_height = svg_box->has_attribute(HTML::AttributeNames::height);
  25. auto specified_height = paint_box()->content_height();
  26. auto scale_width = has_specified_width ? specified_width / view_box.width : 1;
  27. auto scale_height = has_specified_height ? specified_height / view_box.height : 1;
  28. return min(scale_width, scale_height);
  29. }
  30. Gfx::FloatPoint SVGGeometryBox::viewbox_origin() const
  31. {
  32. auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
  33. if (!svg_box || !svg_box->view_box().has_value())
  34. return { 0, 0 };
  35. return { svg_box->view_box().value().min_x, svg_box->view_box().value().min_y };
  36. }
  37. RefPtr<Painting::Paintable> SVGGeometryBox::create_paintable() const
  38. {
  39. return Painting::SVGGeometryPaintable::create(*this);
  40. }
  41. }