SVGDecodedImageData.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/CommandExecutorSkia.h>
  19. #include <LibWeb/Painting/PaintContext.h>
  20. #include <LibWeb/Painting/ViewportPaintable.h>
  21. #include <LibWeb/SVG/SVGDecodedImageData.h>
  22. #include <LibWeb/SVG/SVGSVGElement.h>
  23. namespace Web::SVG {
  24. JS_DEFINE_ALLOCATOR(SVGDecodedImageData);
  25. JS_DEFINE_ALLOCATOR(SVGDecodedImageData::SVGPageClient);
  26. ErrorOr<JS::NonnullGCPtr<SVGDecodedImageData>> SVGDecodedImageData::create(JS::Realm& realm, JS::NonnullGCPtr<Page> host_page, URL::URL const& url, ByteBuffer data)
  27. {
  28. auto page_client = SVGPageClient::create(Bindings::main_thread_vm(), host_page);
  29. auto page = Page::create(Bindings::main_thread_vm(), *page_client);
  30. page_client->m_svg_page = page.ptr();
  31. page->set_top_level_traversable(MUST(Web::HTML::TraversableNavigable::create_a_new_top_level_traversable(*page, nullptr, {})));
  32. JS::NonnullGCPtr<HTML::Navigable> navigable = page->top_level_traversable();
  33. auto response = Fetch::Infrastructure::Response::create(navigable->vm());
  34. response->url_list().append(url);
  35. auto navigation_params = navigable->heap().allocate_without_realm<HTML::NavigationParams>();
  36. navigation_params->navigable = navigable;
  37. navigation_params->response = response;
  38. navigation_params->origin = HTML::Origin {};
  39. navigation_params->policy_container = HTML::PolicyContainer {};
  40. navigation_params->final_sandboxing_flag_set = HTML::SandboxingFlagSet {};
  41. navigation_params->cross_origin_opener_policy = HTML::CrossOriginOpenerPolicy {};
  42. // FIXME: Use Navigable::navigate() instead of manually replacing the navigable's document.
  43. auto document = DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html"_string, navigation_params).release_value_but_fixme_should_propagate_errors();
  44. navigable->set_ongoing_navigation({});
  45. navigable->active_document()->destroy();
  46. navigable->active_session_history_entry()->document_state()->set_document(document);
  47. auto parser = HTML::HTMLParser::create_with_uncertain_encoding(document, data);
  48. parser->run(document->url());
  49. // Perform some DOM surgery to make the SVG root element be the first child of the Document.
  50. // FIXME: This is a huge hack until we figure out how to actually parse separate SVG files.
  51. auto* svg_root = document->body()->first_child_of_type<SVG::SVGSVGElement>();
  52. if (!svg_root)
  53. return Error::from_string_literal("SVGDecodedImageData: Invalid SVG input");
  54. svg_root->remove();
  55. document->remove_all_children();
  56. MUST(document->append_child(*svg_root));
  57. return realm.heap().allocate<SVGDecodedImageData>(realm, page, page_client, document, *svg_root);
  58. }
  59. SVGDecodedImageData::SVGDecodedImageData(JS::NonnullGCPtr<Page> page, JS::NonnullGCPtr<SVGPageClient> page_client, JS::NonnullGCPtr<DOM::Document> document, JS::NonnullGCPtr<SVG::SVGSVGElement> root_element)
  60. : m_page(page)
  61. , m_page_client(page_client)
  62. , m_document(document)
  63. , m_root_element(root_element)
  64. {
  65. }
  66. SVGDecodedImageData::~SVGDecodedImageData() = default;
  67. void SVGDecodedImageData::visit_edges(Cell::Visitor& visitor)
  68. {
  69. Base::visit_edges(visitor);
  70. visitor.visit(m_page);
  71. visitor.visit(m_document);
  72. visitor.visit(m_page_client);
  73. visitor.visit(m_root_element);
  74. }
  75. RefPtr<Gfx::Bitmap> SVGDecodedImageData::render(Gfx::IntSize size) const
  76. {
  77. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, size).release_value_but_fixme_should_propagate_errors();
  78. VERIFY(m_document->navigable());
  79. m_document->navigable()->set_viewport_size(size.to_type<CSSPixels>());
  80. m_document->update_layout();
  81. Painting::CommandList painting_commands;
  82. Painting::RecordingPainter recording_painter(painting_commands);
  83. m_document->navigable()->record_painting_commands(recording_painter, {});
  84. auto painting_command_executor_type = m_page_client->painting_command_executor_type();
  85. switch (painting_command_executor_type) {
  86. case PaintingCommandExecutorType::CPU:
  87. case PaintingCommandExecutorType::GPU: { // GPU painter does not have any path rasterization support so we always fall back to CPU painter
  88. Painting::CommandExecutorCPU executor { *bitmap };
  89. painting_commands.execute(executor);
  90. break;
  91. }
  92. case PaintingCommandExecutorType::Skia: {
  93. Painting::CommandExecutorSkia executor { *bitmap };
  94. painting_commands.execute(executor);
  95. break;
  96. }
  97. default:
  98. VERIFY_NOT_REACHED();
  99. }
  100. return bitmap;
  101. }
  102. RefPtr<Gfx::ImmutableBitmap> SVGDecodedImageData::bitmap(size_t, Gfx::IntSize size) const
  103. {
  104. if (size.is_empty())
  105. return nullptr;
  106. if (auto it = m_cached_rendered_bitmaps.find(size); it != m_cached_rendered_bitmaps.end())
  107. return it->value;
  108. // Prevent the cache from growing too big.
  109. // FIXME: Evict least used entries.
  110. if (m_cached_rendered_bitmaps.size() > 10)
  111. m_cached_rendered_bitmaps.remove(m_cached_rendered_bitmaps.begin());
  112. auto immutable_bitmap = Gfx::ImmutableBitmap::create(*render(size));
  113. m_cached_rendered_bitmaps.set(size, immutable_bitmap);
  114. return immutable_bitmap;
  115. }
  116. Optional<CSSPixels> SVGDecodedImageData::intrinsic_width() const
  117. {
  118. // https://www.w3.org/TR/SVG2/coords.html#SizingSVGInCSS
  119. m_document->update_style();
  120. auto const* root_element_style = m_root_element->computed_css_values();
  121. VERIFY(root_element_style);
  122. auto const& width_value = root_element_style->size_value(CSS::PropertyID::Width);
  123. if (width_value.is_length() && width_value.length().is_absolute())
  124. return width_value.length().absolute_length_to_px();
  125. return {};
  126. }
  127. Optional<CSSPixels> SVGDecodedImageData::intrinsic_height() const
  128. {
  129. // https://www.w3.org/TR/SVG2/coords.html#SizingSVGInCSS
  130. m_document->update_style();
  131. auto const* root_element_style = m_root_element->computed_css_values();
  132. VERIFY(root_element_style);
  133. auto const& height_value = root_element_style->size_value(CSS::PropertyID::Height);
  134. if (height_value.is_length() && height_value.length().is_absolute())
  135. return height_value.length().absolute_length_to_px();
  136. return {};
  137. }
  138. Optional<CSSPixelFraction> SVGDecodedImageData::intrinsic_aspect_ratio() const
  139. {
  140. // https://www.w3.org/TR/SVG2/coords.html#SizingSVGInCSS
  141. auto width = intrinsic_width();
  142. auto height = intrinsic_height();
  143. if (height.has_value() && *height == 0)
  144. return {};
  145. if (width.has_value() && height.has_value())
  146. return *width / *height;
  147. if (auto const& viewbox = m_root_element->view_box(); viewbox.has_value()) {
  148. auto viewbox_width = CSSPixels::nearest_value_for(viewbox->width);
  149. if (viewbox_width == 0)
  150. return {};
  151. auto viewbox_height = CSSPixels::nearest_value_for(viewbox->height);
  152. if (viewbox_height == 0)
  153. return {};
  154. return viewbox_width / viewbox_height;
  155. }
  156. return {};
  157. }
  158. void SVGDecodedImageData::SVGPageClient::visit_edges(Visitor& visitor)
  159. {
  160. Base::visit_edges(visitor);
  161. visitor.visit(m_host_page);
  162. visitor.visit(m_svg_page);
  163. }
  164. }