NavigableContainerViewport.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/HTMLObjectElement.h>
  9. #include <LibWeb/Layout/NavigableContainerViewport.h>
  10. #include <LibWeb/Layout/Viewport.h>
  11. #include <LibWeb/Painting/NavigableContainerViewportPaintable.h>
  12. #include <LibWeb/SVG/SVGSVGElement.h>
  13. namespace Web::Layout {
  14. GC_DEFINE_ALLOCATOR(NavigableContainerViewport);
  15. NavigableContainerViewport::NavigableContainerViewport(DOM::Document& document, HTML::NavigableContainer& element, GC::Ref<CSS::ComputedProperties> style)
  16. : ReplacedBox(document, element, move(style))
  17. {
  18. }
  19. NavigableContainerViewport::~NavigableContainerViewport() = default;
  20. void NavigableContainerViewport::prepare_for_replaced_layout()
  21. {
  22. if (is<HTML::HTMLObjectElement>(dom_node())) {
  23. if (auto const* content_document = dom_node().content_document_without_origin_check()) {
  24. if (auto const* root_element = content_document->document_element(); root_element && root_element->is_svg_svg_element()) {
  25. auto natural_metrics = SVG::SVGSVGElement::negotiate_natural_metrics(static_cast<SVG::SVGSVGElement const&>(*root_element));
  26. set_natural_width(natural_metrics.width);
  27. set_natural_height(natural_metrics.height);
  28. set_natural_aspect_ratio(natural_metrics.aspect_ratio);
  29. return;
  30. }
  31. }
  32. }
  33. // FIXME: Do proper error checking, etc.
  34. set_natural_width(dom_node().get_attribute_value(HTML::AttributeNames::width).to_number<int>().value_or(300));
  35. set_natural_height(dom_node().get_attribute_value(HTML::AttributeNames::height).to_number<int>().value_or(150));
  36. }
  37. void NavigableContainerViewport::did_set_content_size()
  38. {
  39. ReplacedBox::did_set_content_size();
  40. if (dom_node().content_navigable())
  41. dom_node().content_navigable()->set_viewport_size(paintable_box()->content_size());
  42. }
  43. GC::Ptr<Painting::Paintable> NavigableContainerViewport::create_paintable() const
  44. {
  45. return Painting::NavigableContainerViewportPaintable::create(*this);
  46. }
  47. }