FontFaceSet.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Heap/Heap.h>
  8. #include <LibJS/Runtime/Realm.h>
  9. #include <LibJS/Runtime/Set.h>
  10. #include <LibWeb/Bindings/FontFaceSetPrototype.h>
  11. #include <LibWeb/Bindings/Intrinsics.h>
  12. #include <LibWeb/CSS/FontFace.h>
  13. #include <LibWeb/CSS/FontFaceSet.h>
  14. #include <LibWeb/HTML/EventNames.h>
  15. #include <LibWeb/WebIDL/Promise.h>
  16. namespace Web::CSS {
  17. JS_DEFINE_ALLOCATOR(FontFaceSet);
  18. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-fontfaceset
  19. JS::NonnullGCPtr<FontFaceSet> FontFaceSet::construct_impl(JS::Realm& realm, Vector<JS::Handle<FontFace>> const& initial_faces)
  20. {
  21. auto ready_promise = WebIDL::create_promise(realm);
  22. auto set_entries = JS::Set::create(realm);
  23. // The FontFaceSet constructor, when called, must iterate its initialFaces argument and add each value to its set entries.
  24. for (auto const& face : initial_faces)
  25. set_entries->set_add(face);
  26. return realm.heap().allocate<FontFaceSet>(realm, realm, ready_promise, set_entries);
  27. }
  28. JS::NonnullGCPtr<FontFaceSet> FontFaceSet::create(JS::Realm& realm)
  29. {
  30. return construct_impl(realm, {});
  31. }
  32. FontFaceSet::FontFaceSet(JS::Realm& realm, JS::NonnullGCPtr<WebIDL::Promise> ready_promise, JS::NonnullGCPtr<JS::Set> set_entries)
  33. : DOM::EventTarget(realm)
  34. , m_set_entries(set_entries)
  35. , m_ready_promise(ready_promise)
  36. , m_status(Bindings::FontFaceSetLoadStatus::Loaded)
  37. {
  38. }
  39. void FontFaceSet::initialize(JS::Realm& realm)
  40. {
  41. Base::initialize(realm);
  42. WEB_SET_PROTOTYPE_FOR_INTERFACE(FontFaceSet);
  43. }
  44. void FontFaceSet::visit_edges(Cell::Visitor& visitor)
  45. {
  46. Base::visit_edges(visitor);
  47. visitor.visit(m_set_entries);
  48. visitor.visit(m_ready_promise);
  49. visitor.visit(m_loading_fonts);
  50. visitor.visit(m_loaded_fonts);
  51. visitor.visit(m_failed_fonts);
  52. }
  53. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-add
  54. WebIDL::ExceptionOr<JS::NonnullGCPtr<FontFaceSet>>
  55. FontFaceSet::add(JS::Handle<FontFace> face)
  56. {
  57. // 1. If font is already in the FontFaceSet’s set entries, skip to the last step of this algorithm immediately.
  58. if (m_set_entries->set_has(face))
  59. return JS::NonnullGCPtr<FontFaceSet>(*this);
  60. // 2. If font is CSS-connected, throw an InvalidModificationError exception and exit this algorithm immediately.
  61. if (face->is_css_connected()) {
  62. return WebIDL::InvalidModificationError::create(realm(), "Cannot add a CSS-connected FontFace to a FontFaceSet"_fly_string);
  63. }
  64. // 3. Add the font argument to the FontFaceSet’s set entries.
  65. m_set_entries->set_add(face);
  66. // 4. If font’s status attribute is "loading"
  67. if (face->status() == Bindings::FontFaceLoadStatus::Loading) {
  68. // 1. If the FontFaceSet’s [[LoadingFonts]] list is empty, switch the FontFaceSet to loading.
  69. if (m_loading_fonts.is_empty()) {
  70. m_status = Bindings::FontFaceSetLoadStatus::Loading;
  71. }
  72. // 2. Append font to the FontFaceSet’s [[LoadingFonts]] list.
  73. m_loading_fonts.append(*face);
  74. }
  75. // 5. Return the FontFaceSet.
  76. return JS::NonnullGCPtr<FontFaceSet>(*this);
  77. }
  78. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-delete
  79. bool FontFaceSet::delete_(JS::Handle<FontFace> face)
  80. {
  81. // 1. If font is CSS-connected, return false and exit this algorithm immediately.
  82. if (face->is_css_connected()) {
  83. return false;
  84. }
  85. // 2. Let deleted be the result of removing font from the FontFaceSet’s set entries.
  86. bool deleted = m_set_entries->set_remove(face);
  87. // 3. If font is present in the FontFaceSet’s [[LoadedFonts]], or [[FailedFonts]] lists, remove it.
  88. m_loaded_fonts.remove_all_matching([face](auto const& entry) { return entry == face; });
  89. m_failed_fonts.remove_all_matching([face](auto const& entry) { return entry == face; });
  90. // 4. If font is present in the FontFaceSet’s [[LoadingFonts]] list, remove it. If font was the last item in that list (and so the list is now empty), switch the FontFaceSet to loaded.
  91. m_loading_fonts.remove_all_matching([face](auto const& entry) { return entry == face; });
  92. if (m_loading_fonts.is_empty()) {
  93. m_status = Bindings::FontFaceSetLoadStatus::Loaded;
  94. }
  95. return deleted;
  96. }
  97. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-clear
  98. void FontFaceSet::clear()
  99. {
  100. // FIXME: Do the actual spec steps
  101. m_set_entries->set_clear();
  102. }
  103. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloading
  104. void FontFaceSet::set_onloading(WebIDL::CallbackType* event_handler)
  105. {
  106. set_event_handler_attribute(HTML::EventNames::loading, event_handler);
  107. }
  108. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloading
  109. WebIDL::CallbackType* FontFaceSet::onloading()
  110. {
  111. return event_handler_attribute(HTML::EventNames::loading);
  112. }
  113. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingdone
  114. void FontFaceSet::set_onloadingdone(WebIDL::CallbackType* event_handler)
  115. {
  116. set_event_handler_attribute(HTML::EventNames::loadingdone, event_handler);
  117. }
  118. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingdone
  119. WebIDL::CallbackType* FontFaceSet::onloadingdone()
  120. {
  121. return event_handler_attribute(HTML::EventNames::loadingdone);
  122. }
  123. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingerror
  124. void FontFaceSet::set_onloadingerror(WebIDL::CallbackType* event_handler)
  125. {
  126. set_event_handler_attribute(HTML::EventNames::loadingerror, event_handler);
  127. }
  128. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingerror
  129. WebIDL::CallbackType* FontFaceSet::onloadingerror()
  130. {
  131. return event_handler_attribute(HTML::EventNames::loadingerror);
  132. }
  133. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-load
  134. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> FontFaceSet::load(String const&, String const&)
  135. {
  136. // FIXME: Do the steps
  137. auto promise = WebIDL::create_rejected_promise(realm(), WebIDL::NotSupportedError::create(realm(), "FontFaceSet::load is not yet implemented"_fly_string));
  138. return verify_cast<JS::Promise>(*promise->promise());
  139. }
  140. // https://drafts.csswg.org/css-font-loading/#font-face-set-ready
  141. JS::NonnullGCPtr<JS::Promise> FontFaceSet::ready() const
  142. {
  143. return verify_cast<JS::Promise>(*m_ready_promise->promise());
  144. }
  145. void FontFaceSet::resolve_ready_promise()
  146. {
  147. WebIDL::resolve_promise(realm(), *m_ready_promise);
  148. }
  149. }