SVGImageElement.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <LibCore/Timer.h>
  8. #include <LibJS/Heap/Heap.h>
  9. #include <LibWeb/Bindings/SVGImageElementPrototype.h>
  10. #include <LibWeb/DOM/DocumentObserver.h>
  11. #include <LibWeb/DOM/Event.h>
  12. #include <LibWeb/HTML/PotentialCORSRequest.h>
  13. #include <LibWeb/HTML/SharedResourceRequest.h>
  14. #include <LibWeb/Layout/SVGImageBox.h>
  15. #include <LibWeb/Painting/Paintable.h>
  16. #include <LibWeb/SVG/SVGDecodedImageData.h>
  17. namespace Web::SVG {
  18. SVGImageElement::SVGImageElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  19. : SVGGraphicsElement(document, move(qualified_name))
  20. {
  21. m_animation_timer = Core::Timer::try_create().release_value_but_fixme_should_propagate_errors();
  22. m_animation_timer->on_timeout = [this] { animate(); };
  23. }
  24. void SVGImageElement::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGImageElement);
  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. }
  39. void SVGImageElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
  40. {
  41. SVGGraphicsElement::attribute_changed(name, old_value, value);
  42. if (name == SVG::AttributeNames::x) {
  43. auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
  44. MUST(x()->base_val()->set_value(parsed_value.value_or(0)));
  45. } else if (name == SVG::AttributeNames::y) {
  46. auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
  47. MUST(y()->base_val()->set_value(parsed_value.value_or(0)));
  48. } else if (name == SVG::AttributeNames::width) {
  49. auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
  50. MUST(width()->base_val()->set_value(parsed_value.value_or(0)));
  51. } else if (name == SVG::AttributeNames::height) {
  52. auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
  53. MUST(height()->base_val()->set_value(parsed_value.value_or(0)));
  54. } else if (name == SVG::AttributeNames::href) {
  55. process_the_url(value);
  56. }
  57. }
  58. // https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__x
  59. JS::NonnullGCPtr<SVG::SVGAnimatedLength> SVGImageElement::x()
  60. {
  61. if (!m_x) {
  62. auto& realm = this->realm();
  63. m_x = SVGAnimatedLength::create(realm, SVGLength::create(realm, 0, 0), SVGLength::create(realm, 0, 0));
  64. }
  65. return *m_x;
  66. }
  67. // https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__y
  68. JS::NonnullGCPtr<SVG::SVGAnimatedLength> SVGImageElement::y()
  69. {
  70. if (!m_y) {
  71. auto& realm = this->realm();
  72. m_y = SVGAnimatedLength::create(realm, SVGLength::create(realm, 0, 0), SVGLength::create(realm, 0, 0));
  73. }
  74. return *m_y;
  75. }
  76. // https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__width
  77. JS::NonnullGCPtr<SVG::SVGAnimatedLength> SVGImageElement::width()
  78. {
  79. if (!m_width) {
  80. auto& realm = this->realm();
  81. m_width = SVGAnimatedLength::create(realm, SVGLength::create(realm, 0, intrinsic_width().value_or(0).to_double()), SVGLength::create(realm, 0, 0));
  82. }
  83. return *m_width;
  84. }
  85. // https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__height
  86. JS::NonnullGCPtr<SVG::SVGAnimatedLength> SVGImageElement::height()
  87. {
  88. if (!m_height) {
  89. auto& realm = this->realm();
  90. m_height = SVGAnimatedLength::create(realm, SVGLength::create(realm, 0, intrinsic_height().value_or(0).to_double()), SVGLength::create(realm, 0, 0));
  91. }
  92. return *m_height;
  93. }
  94. Gfx::Rect<CSSPixels> SVGImageElement::bounding_box() const
  95. {
  96. Optional<CSSPixels> width;
  97. if (attribute(HTML::AttributeNames::width).has_value())
  98. width = CSSPixels { m_width->base_val()->value() };
  99. Optional<CSSPixels> height;
  100. if (attribute(HTML::AttributeNames::height).has_value())
  101. height = CSSPixels { m_height->base_val()->value() };
  102. if (!height.has_value() && width.has_value() && intrinsic_aspect_ratio().has_value())
  103. height = width.value() / intrinsic_aspect_ratio().value();
  104. if (!width.has_value() && height.has_value() && intrinsic_aspect_ratio().has_value())
  105. width = height.value() * intrinsic_aspect_ratio().value();
  106. if (!width.has_value() && intrinsic_width().has_value())
  107. width = intrinsic_width();
  108. if (!height.has_value() && intrinsic_height().has_value())
  109. height = intrinsic_height();
  110. return {
  111. CSSPixels { m_x ? m_x->base_val()->value() : 0 },
  112. CSSPixels { m_y ? m_y->base_val()->value() : 0 },
  113. width.value_or(0),
  114. height.value_or(0),
  115. };
  116. }
  117. // https://www.w3.org/TR/SVG2/linking.html#processingURL
  118. void SVGImageElement::process_the_url(Optional<String> const& href)
  119. {
  120. m_href = document().url().complete_url(href.value_or(String {}));
  121. if (!m_href.is_valid())
  122. return;
  123. fetch_the_document(m_href);
  124. }
  125. // https://svgwg.org/svg2-draft/linking.html#processingURL-fetch
  126. void SVGImageElement::fetch_the_document(URL::URL const& url)
  127. {
  128. m_load_event_delayer.emplace(document());
  129. m_resource_request = HTML::SharedResourceRequest::get_or_create(realm(), document().page(), url);
  130. m_resource_request->add_callbacks(
  131. [this] {
  132. m_load_event_delayer.clear();
  133. auto image_data = m_resource_request->image_data();
  134. if (image_data->is_animated() && image_data->frame_count() > 1) {
  135. m_current_frame_index = 0;
  136. m_animation_timer->set_interval(image_data->frame_duration(0));
  137. m_animation_timer->start();
  138. }
  139. set_needs_style_update(true);
  140. document().set_needs_layout();
  141. dispatch_event(DOM::Event::create(realm(), HTML::EventNames::load));
  142. },
  143. [this] {
  144. m_load_event_delayer.clear();
  145. dispatch_event(DOM::Event::create(realm(), HTML::EventNames::error));
  146. });
  147. if (m_resource_request->needs_fetching()) {
  148. auto request = HTML::create_potential_CORS_request(vm(), url, Fetch::Infrastructure::Request::Destination::Image, HTML::CORSSettingAttribute::NoCORS);
  149. request->set_client(&document().relevant_settings_object());
  150. m_resource_request->fetch_resource(realm(), request);
  151. }
  152. }
  153. JS::GCPtr<Layout::Node> SVGImageElement::create_layout_node(CSS::StyleProperties style)
  154. {
  155. return heap().allocate<Layout::SVGImageBox>(document(), *this, move(style));
  156. }
  157. bool SVGImageElement::is_image_available() const
  158. {
  159. return m_resource_request && m_resource_request->image_data();
  160. }
  161. Optional<CSSPixels> SVGImageElement::intrinsic_width() const
  162. {
  163. if (!m_resource_request)
  164. return {};
  165. if (auto image_data = m_resource_request->image_data())
  166. return image_data->intrinsic_width();
  167. return {};
  168. }
  169. Optional<CSSPixels> SVGImageElement::intrinsic_height() const
  170. {
  171. if (!m_resource_request)
  172. return {};
  173. if (auto image_data = m_resource_request->image_data())
  174. return image_data->intrinsic_height();
  175. return {};
  176. }
  177. Optional<CSSPixelFraction> SVGImageElement::intrinsic_aspect_ratio() const
  178. {
  179. if (!m_resource_request)
  180. return {};
  181. if (auto image_data = m_resource_request->image_data())
  182. return image_data->intrinsic_aspect_ratio();
  183. return {};
  184. }
  185. RefPtr<Gfx::ImmutableBitmap> SVGImageElement::current_image_bitmap(Gfx::IntSize size) const
  186. {
  187. if (!m_resource_request)
  188. return {};
  189. if (auto data = m_resource_request->image_data())
  190. return data->bitmap(m_current_frame_index, size);
  191. return {};
  192. }
  193. void SVGImageElement::animate()
  194. {
  195. auto image_data = m_resource_request->image_data();
  196. if (!image_data) {
  197. return;
  198. }
  199. m_current_frame_index = (m_current_frame_index + 1) % image_data->frame_count();
  200. auto current_frame_duration = image_data->frame_duration(m_current_frame_index);
  201. if (current_frame_duration != m_animation_timer->interval()) {
  202. m_animation_timer->restart(current_frame_duration);
  203. }
  204. if (m_current_frame_index == image_data->frame_count() - 1) {
  205. ++m_loops_completed;
  206. if (m_loops_completed > 0 && m_loops_completed == image_data->loop_count()) {
  207. m_animation_timer->stop();
  208. }
  209. }
  210. if (paintable())
  211. paintable()->set_needs_display();
  212. }
  213. }