SVGImageElement.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2024, Tim Ledbetter <tim.ledbetter@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SVGImageElement.h"
  7. #include <LibJS/Heap/Heap.h>
  8. #include <LibWeb/Bindings/SVGImageElementPrototype.h>
  9. #include <LibWeb/DOM/DocumentObserver.h>
  10. #include <LibWeb/HTML/PotentialCORSRequest.h>
  11. #include <LibWeb/HTML/SharedResourceRequest.h>
  12. #include <LibWeb/Layout/SVGImageBox.h>
  13. #include <LibWeb/SVG/SVGDecodedImageData.h>
  14. namespace Web::SVG {
  15. SVGImageElement::SVGImageElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  16. : SVGGraphicsElement(document, move(qualified_name))
  17. {
  18. }
  19. void SVGImageElement::initialize(JS::Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGImageElement);
  23. m_document_observer = realm.heap().allocate<DOM::DocumentObserver>(realm, realm, document());
  24. m_document_observer->set_document_completely_loaded([this]() mutable {
  25. // FIXME: This is a hack to make SVG images appear when the document has finished loading
  26. document().invalidate_layout();
  27. });
  28. }
  29. void SVGImageElement::visit_edges(Cell::Visitor& visitor)
  30. {
  31. Base::visit_edges(visitor);
  32. SVGURIReferenceMixin::visit_edges(visitor);
  33. visitor.visit(m_x);
  34. visitor.visit(m_y);
  35. visitor.visit(m_width);
  36. visitor.visit(m_height);
  37. visitor.visit(m_resource_request);
  38. visitor.visit(m_document_observer);
  39. }
  40. void SVGImageElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
  41. {
  42. SVGGraphicsElement::attribute_changed(name, old_value, value);
  43. if (name == SVG::AttributeNames::x) {
  44. auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
  45. MUST(x()->base_val()->set_value(parsed_value.value_or(0)));
  46. } else if (name == SVG::AttributeNames::y) {
  47. auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
  48. MUST(y()->base_val()->set_value(parsed_value.value_or(0)));
  49. } else if (name == SVG::AttributeNames::width) {
  50. auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
  51. MUST(width()->base_val()->set_value(parsed_value.value_or(0)));
  52. } else if (name == SVG::AttributeNames::height) {
  53. auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
  54. MUST(height()->base_val()->set_value(parsed_value.value_or(0)));
  55. } else if (name == SVG::AttributeNames::href) {
  56. process_the_url(value);
  57. }
  58. }
  59. // https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__x
  60. JS::NonnullGCPtr<SVG::SVGAnimatedLength> SVGImageElement::x()
  61. {
  62. if (!m_x) {
  63. auto& realm = this->realm();
  64. m_x = SVGAnimatedLength::create(realm, SVGLength::create(realm, 0, 0), SVGLength::create(realm, 0, 0));
  65. }
  66. return *m_x;
  67. }
  68. // https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__y
  69. JS::NonnullGCPtr<SVG::SVGAnimatedLength> SVGImageElement::y()
  70. {
  71. if (!m_y) {
  72. auto& realm = this->realm();
  73. m_y = SVGAnimatedLength::create(realm, SVGLength::create(realm, 0, 0), SVGLength::create(realm, 0, 0));
  74. }
  75. return *m_y;
  76. }
  77. // https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__width
  78. JS::NonnullGCPtr<SVG::SVGAnimatedLength> SVGImageElement::width()
  79. {
  80. if (!m_width) {
  81. auto& realm = this->realm();
  82. m_width = SVGAnimatedLength::create(realm, SVGLength::create(realm, 0, intrinsic_width().value_or(0).to_double()), SVGLength::create(realm, 0, 0));
  83. }
  84. return *m_width;
  85. }
  86. // https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__height
  87. JS::NonnullGCPtr<SVG::SVGAnimatedLength> SVGImageElement::height()
  88. {
  89. if (!m_height) {
  90. auto& realm = this->realm();
  91. m_height = SVGAnimatedLength::create(realm, SVGLength::create(realm, 0, intrinsic_height().value_or(0).to_double()), SVGLength::create(realm, 0, 0));
  92. }
  93. return *m_height;
  94. }
  95. Gfx::Rect<CSSPixels> SVGImageElement::bounding_box() const
  96. {
  97. Optional<CSSPixels> width;
  98. if (attribute(HTML::AttributeNames::width).has_value())
  99. width = CSSPixels { m_width->base_val()->value() };
  100. Optional<CSSPixels> height;
  101. if (attribute(HTML::AttributeNames::height).has_value())
  102. height = CSSPixels { m_height->base_val()->value() };
  103. if (!height.has_value() && width.has_value() && intrinsic_aspect_ratio().has_value())
  104. height = width.value() / intrinsic_aspect_ratio().value();
  105. if (!width.has_value() && height.has_value() && intrinsic_aspect_ratio().has_value())
  106. width = height.value() * intrinsic_aspect_ratio().value();
  107. if (!width.has_value() && intrinsic_width().has_value())
  108. width = intrinsic_width();
  109. if (!height.has_value() && intrinsic_height().has_value())
  110. height = intrinsic_height();
  111. return {
  112. CSSPixels { m_x ? m_x->base_val()->value() : 0 },
  113. CSSPixels { m_y ? m_y->base_val()->value() : 0 },
  114. width.value_or(0),
  115. height.value_or(0),
  116. };
  117. }
  118. // https://www.w3.org/TR/SVG2/linking.html#processingURL
  119. void SVGImageElement::process_the_url(Optional<String> const& href)
  120. {
  121. m_href = document().url().complete_url(href.value_or(String {}));
  122. if (!m_href.is_valid())
  123. return;
  124. fetch_the_document(m_href);
  125. }
  126. // https://svgwg.org/svg2-draft/linking.html#processingURL-fetch
  127. void SVGImageElement::fetch_the_document(URL::URL const& url)
  128. {
  129. m_load_event_delayer.emplace(document());
  130. m_resource_request = HTML::SharedResourceRequest::get_or_create(realm(), document().page(), url);
  131. m_resource_request->add_callbacks(
  132. [this] {
  133. m_load_event_delayer.clear();
  134. },
  135. [this] {
  136. m_load_event_delayer.clear();
  137. });
  138. if (m_resource_request->needs_fetching()) {
  139. auto request = HTML::create_potential_CORS_request(vm(), url, Fetch::Infrastructure::Request::Destination::Image, HTML::CORSSettingAttribute::NoCORS);
  140. request->set_client(&document().relevant_settings_object());
  141. m_resource_request->fetch_resource(realm(), request);
  142. }
  143. }
  144. JS::GCPtr<Layout::Node> SVGImageElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  145. {
  146. return heap().allocate_without_realm<Layout::SVGImageBox>(document(), *this, move(style));
  147. }
  148. bool SVGImageElement::is_image_available() const
  149. {
  150. return m_resource_request && m_resource_request->image_data();
  151. }
  152. Optional<CSSPixels> SVGImageElement::intrinsic_width() const
  153. {
  154. if (!m_resource_request)
  155. return {};
  156. if (auto image_data = m_resource_request->image_data())
  157. return image_data->intrinsic_width();
  158. return {};
  159. }
  160. Optional<CSSPixels> SVGImageElement::intrinsic_height() const
  161. {
  162. if (!m_resource_request)
  163. return {};
  164. if (auto image_data = m_resource_request->image_data())
  165. return image_data->intrinsic_height();
  166. return {};
  167. }
  168. Optional<CSSPixelFraction> SVGImageElement::intrinsic_aspect_ratio() const
  169. {
  170. if (!m_resource_request)
  171. return {};
  172. if (auto image_data = m_resource_request->image_data())
  173. return image_data->intrinsic_aspect_ratio();
  174. return {};
  175. }
  176. RefPtr<Gfx::ImmutableBitmap> SVGImageElement::current_image_bitmap(Gfx::IntSize size) const
  177. {
  178. if (!m_resource_request)
  179. return {};
  180. if (auto data = m_resource_request->image_data())
  181. return data->bitmap(0, size);
  182. return {};
  183. }
  184. }