SVGDecodedImageData.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibWeb/Bindings/MainThreadVM.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  10. #include <LibWeb/HTML/BrowsingContext.h>
  11. #include <LibWeb/HTML/DocumentState.h>
  12. #include <LibWeb/HTML/NavigationParams.h>
  13. #include <LibWeb/HTML/Parser/HTMLParser.h>
  14. #include <LibWeb/HTML/TraversableNavigable.h>
  15. #include <LibWeb/Layout/Viewport.h>
  16. #include <LibWeb/Page/Page.h>
  17. #include <LibWeb/Painting/CommandExecutorCPU.h>
  18. #include <LibWeb/Painting/PaintContext.h>
  19. #include <LibWeb/Painting/ViewportPaintable.h>
  20. #include <LibWeb/SVG/SVGDecodedImageData.h>
  21. #include <LibWeb/SVG/SVGSVGElement.h>
  22. namespace Web::SVG {
  23. JS_DEFINE_ALLOCATOR(SVGDecodedImageData);
  24. ErrorOr<JS::NonnullGCPtr<SVGDecodedImageData>> SVGDecodedImageData::create(JS::Realm& realm, JS::NonnullGCPtr<Page> host_page, URL::URL const& url, ByteBuffer data)
  25. {
  26. auto page_client = SVGPageClient::create(Bindings::main_thread_vm(), host_page);
  27. auto page = Page::create(Bindings::main_thread_vm(), *page_client);
  28. page_client->m_svg_page = page.ptr();
  29. page->set_top_level_traversable(MUST(Web::HTML::TraversableNavigable::create_a_new_top_level_traversable(*page, nullptr, {})));
  30. JS::NonnullGCPtr<HTML::Navigable> navigable = page->top_level_traversable();
  31. auto response = Fetch::Infrastructure::Response::create(navigable->vm());
  32. response->url_list().append(url);
  33. HTML::NavigationParams navigation_params {
  34. .id = {},
  35. .navigable = navigable,
  36. .request = nullptr,
  37. .response = response,
  38. .fetch_controller = nullptr,
  39. .commit_early_hints = nullptr,
  40. .coop_enforcement_result = HTML::CrossOriginOpenerPolicyEnforcementResult {},
  41. .reserved_environment = {},
  42. .origin = HTML::Origin {},
  43. .policy_container = HTML::PolicyContainer {},
  44. .final_sandboxing_flag_set = HTML::SandboxingFlagSet {},
  45. .cross_origin_opener_policy = HTML::CrossOriginOpenerPolicy {},
  46. .about_base_url = {},
  47. };
  48. // FIXME: Use Navigable::navigate() instead of manually replacing the navigable's document.
  49. auto document = DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html"_string, navigation_params).release_value_but_fixme_should_propagate_errors();
  50. navigable->set_ongoing_navigation({});
  51. navigable->active_document()->destroy();
  52. navigable->active_session_history_entry()->document_state()->set_document(document);
  53. auto parser = HTML::HTMLParser::create_with_uncertain_encoding(document, data);
  54. parser->run(document->url());
  55. // Perform some DOM surgery to make the SVG root element be the first child of the Document.
  56. // FIXME: This is a huge hack until we figure out how to actually parse separate SVG files.
  57. auto* svg_root = document->body()->first_child_of_type<SVG::SVGSVGElement>();
  58. if (!svg_root)
  59. return Error::from_string_literal("SVGDecodedImageData: Invalid SVG input");
  60. svg_root->remove();
  61. document->remove_all_children();
  62. MUST(document->append_child(*svg_root));
  63. return realm.heap().allocate<SVGDecodedImageData>(realm, page, page_client, document, *svg_root);
  64. }
  65. SVGDecodedImageData::SVGDecodedImageData(JS::NonnullGCPtr<Page> page, JS::NonnullGCPtr<SVGPageClient> page_client, JS::NonnullGCPtr<DOM::Document> document, JS::NonnullGCPtr<SVG::SVGSVGElement> root_element)
  66. : m_page(page)
  67. , m_page_client(page_client)
  68. , m_document(document)
  69. , m_root_element(root_element)
  70. {
  71. }
  72. SVGDecodedImageData::~SVGDecodedImageData() = default;
  73. void SVGDecodedImageData::visit_edges(Cell::Visitor& visitor)
  74. {
  75. Base::visit_edges(visitor);
  76. visitor.visit(m_page);
  77. visitor.visit(m_document);
  78. visitor.visit(m_page_client);
  79. visitor.visit(m_root_element);
  80. }
  81. RefPtr<Gfx::Bitmap> SVGDecodedImageData::render(Gfx::IntSize size) const
  82. {
  83. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, size).release_value_but_fixme_should_propagate_errors();
  84. VERIFY(m_document->navigable());
  85. m_document->navigable()->set_viewport_rect({ 0, 0, size.width(), size.height() });
  86. m_document->update_layout();
  87. Painting::CommandList painting_commands;
  88. Painting::RecordingPainter recording_painter(painting_commands);
  89. Painting::CommandExecutorCPU executor { *bitmap };
  90. m_document->navigable()->paint(recording_painter, {});
  91. painting_commands.execute(executor);
  92. return bitmap;
  93. }
  94. RefPtr<Gfx::ImmutableBitmap> SVGDecodedImageData::bitmap(size_t, Gfx::IntSize size) const
  95. {
  96. if (size.is_empty())
  97. return nullptr;
  98. if (auto it = m_cached_rendered_bitmaps.find(size); it != m_cached_rendered_bitmaps.end())
  99. return it->value;
  100. // Prevent the cache from growing too big.
  101. // FIXME: Evict least used entries.
  102. if (m_cached_rendered_bitmaps.size() > 10)
  103. m_cached_rendered_bitmaps.remove(m_cached_rendered_bitmaps.begin());
  104. auto immutable_bitmap = Gfx::ImmutableBitmap::create(*render(size));
  105. m_cached_rendered_bitmaps.set(size, immutable_bitmap);
  106. return immutable_bitmap;
  107. }
  108. Optional<CSSPixels> SVGDecodedImageData::intrinsic_width() const
  109. {
  110. // https://www.w3.org/TR/SVG2/coords.html#SizingSVGInCSS
  111. m_document->update_style();
  112. auto const* root_element_style = m_root_element->computed_css_values();
  113. VERIFY(root_element_style);
  114. auto const& width_value = root_element_style->size_value(CSS::PropertyID::Width);
  115. if (width_value.is_length() && width_value.length().is_absolute())
  116. return width_value.length().absolute_length_to_px();
  117. return {};
  118. }
  119. Optional<CSSPixels> SVGDecodedImageData::intrinsic_height() const
  120. {
  121. // https://www.w3.org/TR/SVG2/coords.html#SizingSVGInCSS
  122. m_document->update_style();
  123. auto const* root_element_style = m_root_element->computed_css_values();
  124. VERIFY(root_element_style);
  125. auto const& height_value = root_element_style->size_value(CSS::PropertyID::Height);
  126. if (height_value.is_length() && height_value.length().is_absolute())
  127. return height_value.length().absolute_length_to_px();
  128. return {};
  129. }
  130. Optional<CSSPixelFraction> SVGDecodedImageData::intrinsic_aspect_ratio() const
  131. {
  132. // https://www.w3.org/TR/SVG2/coords.html#SizingSVGInCSS
  133. auto width = intrinsic_width();
  134. auto height = intrinsic_height();
  135. if (height.has_value() && *height == 0)
  136. return {};
  137. if (width.has_value() && height.has_value())
  138. return *width / *height;
  139. if (auto const& viewbox = m_root_element->view_box(); viewbox.has_value())
  140. return CSSPixels::nearest_value_for(viewbox->width) / CSSPixels::nearest_value_for(viewbox->height);
  141. return {};
  142. }
  143. void SVGDecodedImageData::SVGPageClient::visit_edges(Visitor& visitor)
  144. {
  145. Base::visit_edges(visitor);
  146. visitor.visit(m_host_page);
  147. visitor.visit(m_svg_page);
  148. }
  149. }