SVGDecodedImageData.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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::CPUWithExperimentalTransformSupport:
  88. case PaintingCommandExecutorType::GPU: { // GPU painter does not have any path rasterization support so we always fall back to CPU painter
  89. Painting::CommandExecutorCPU executor { *bitmap };
  90. painting_commands.execute(executor);
  91. break;
  92. }
  93. case PaintingCommandExecutorType::Skia: {
  94. Painting::CommandExecutorSkia executor { *bitmap };
  95. painting_commands.execute(executor);
  96. break;
  97. }
  98. default:
  99. VERIFY_NOT_REACHED();
  100. }
  101. return bitmap;
  102. }
  103. RefPtr<Gfx::ImmutableBitmap> SVGDecodedImageData::bitmap(size_t, Gfx::IntSize size) const
  104. {
  105. if (size.is_empty())
  106. return nullptr;
  107. if (auto it = m_cached_rendered_bitmaps.find(size); it != m_cached_rendered_bitmaps.end())
  108. return it->value;
  109. // Prevent the cache from growing too big.
  110. // FIXME: Evict least used entries.
  111. if (m_cached_rendered_bitmaps.size() > 10)
  112. m_cached_rendered_bitmaps.remove(m_cached_rendered_bitmaps.begin());
  113. auto immutable_bitmap = Gfx::ImmutableBitmap::create(*render(size));
  114. m_cached_rendered_bitmaps.set(size, immutable_bitmap);
  115. return immutable_bitmap;
  116. }
  117. Optional<CSSPixels> SVGDecodedImageData::intrinsic_width() const
  118. {
  119. // https://www.w3.org/TR/SVG2/coords.html#SizingSVGInCSS
  120. m_document->update_style();
  121. auto const* root_element_style = m_root_element->computed_css_values();
  122. VERIFY(root_element_style);
  123. auto const& width_value = root_element_style->size_value(CSS::PropertyID::Width);
  124. if (width_value.is_length() && width_value.length().is_absolute())
  125. return width_value.length().absolute_length_to_px();
  126. return {};
  127. }
  128. Optional<CSSPixels> SVGDecodedImageData::intrinsic_height() const
  129. {
  130. // https://www.w3.org/TR/SVG2/coords.html#SizingSVGInCSS
  131. m_document->update_style();
  132. auto const* root_element_style = m_root_element->computed_css_values();
  133. VERIFY(root_element_style);
  134. auto const& height_value = root_element_style->size_value(CSS::PropertyID::Height);
  135. if (height_value.is_length() && height_value.length().is_absolute())
  136. return height_value.length().absolute_length_to_px();
  137. return {};
  138. }
  139. Optional<CSSPixelFraction> SVGDecodedImageData::intrinsic_aspect_ratio() const
  140. {
  141. // https://www.w3.org/TR/SVG2/coords.html#SizingSVGInCSS
  142. auto width = intrinsic_width();
  143. auto height = intrinsic_height();
  144. if (height.has_value() && *height == 0)
  145. return {};
  146. if (width.has_value() && height.has_value())
  147. return *width / *height;
  148. if (auto const& viewbox = m_root_element->view_box(); viewbox.has_value()) {
  149. auto viewbox_width = CSSPixels::nearest_value_for(viewbox->width);
  150. if (viewbox_width == 0)
  151. return {};
  152. auto viewbox_height = CSSPixels::nearest_value_for(viewbox->height);
  153. if (viewbox_height == 0)
  154. return {};
  155. return viewbox_width / viewbox_height;
  156. }
  157. return {};
  158. }
  159. void SVGDecodedImageData::SVGPageClient::visit_edges(Visitor& visitor)
  160. {
  161. Base::visit_edges(visitor);
  162. visitor.visit(m_host_page);
  163. visitor.visit(m_svg_page);
  164. }
  165. }