FontFaceSet.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. if (set_entries->set_size() == 0)
  27. WebIDL::resolve_promise(realm, *ready_promise);
  28. return realm.heap().allocate<FontFaceSet>(realm, realm, ready_promise, set_entries);
  29. }
  30. JS::NonnullGCPtr<FontFaceSet> FontFaceSet::create(JS::Realm& realm)
  31. {
  32. return construct_impl(realm, {});
  33. }
  34. FontFaceSet::FontFaceSet(JS::Realm& realm, JS::NonnullGCPtr<WebIDL::Promise> ready_promise, JS::NonnullGCPtr<JS::Set> set_entries)
  35. : DOM::EventTarget(realm)
  36. , m_set_entries(set_entries)
  37. , m_ready_promise(ready_promise)
  38. {
  39. bool const is_ready = ready()->state() == JS::Promise::State::Fulfilled;
  40. m_status = is_ready ? Bindings::FontFaceSetLoadStatus::Loaded : Bindings::FontFaceSetLoadStatus::Loading;
  41. }
  42. void FontFaceSet::initialize(JS::Realm& realm)
  43. {
  44. Base::initialize(realm);
  45. WEB_SET_PROTOTYPE_FOR_INTERFACE(FontFaceSet);
  46. }
  47. void FontFaceSet::visit_edges(Cell::Visitor& visitor)
  48. {
  49. Base::visit_edges(visitor);
  50. visitor.visit(m_set_entries);
  51. visitor.visit(m_ready_promise);
  52. visitor.visit(m_loading_fonts);
  53. visitor.visit(m_loaded_fonts);
  54. visitor.visit(m_failed_fonts);
  55. }
  56. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-add
  57. WebIDL::ExceptionOr<JS::NonnullGCPtr<FontFaceSet>>
  58. FontFaceSet::add(JS::Handle<FontFace> face)
  59. {
  60. // 1. If font is already in the FontFaceSet’s set entries, skip to the last step of this algorithm immediately.
  61. if (m_set_entries->set_has(face))
  62. return JS::NonnullGCPtr<FontFaceSet>(*this);
  63. // 2. If font is CSS-connected, throw an InvalidModificationError exception and exit this algorithm immediately.
  64. if (face->is_css_connected()) {
  65. return WebIDL::InvalidModificationError::create(realm(), "Cannot add a CSS-connected FontFace to a FontFaceSet"_fly_string);
  66. }
  67. // 3. Add the font argument to the FontFaceSet’s set entries.
  68. m_set_entries->set_add(face);
  69. // 4. If font’s status attribute is "loading"
  70. if (face->status() == Bindings::FontFaceLoadStatus::Loading) {
  71. // 1. If the FontFaceSet’s [[LoadingFonts]] list is empty, switch the FontFaceSet to loading.
  72. if (m_loading_fonts.is_empty()) {
  73. m_status = Bindings::FontFaceSetLoadStatus::Loading;
  74. }
  75. // 2. Append font to the FontFaceSet’s [[LoadingFonts]] list.
  76. m_loading_fonts.append(*face);
  77. }
  78. // 5. Return the FontFaceSet.
  79. return JS::NonnullGCPtr<FontFaceSet>(*this);
  80. }
  81. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-delete
  82. bool FontFaceSet::delete_(JS::Handle<FontFace> face)
  83. {
  84. // FIXME: Do the actual spec steps
  85. return m_set_entries->set_remove(face);
  86. }
  87. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-clear
  88. void FontFaceSet::clear()
  89. {
  90. // FIXME: Do the actual spec steps
  91. m_set_entries->set_clear();
  92. }
  93. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloading
  94. void FontFaceSet::set_onloading(WebIDL::CallbackType* event_handler)
  95. {
  96. set_event_handler_attribute(HTML::EventNames::loading, event_handler);
  97. }
  98. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloading
  99. WebIDL::CallbackType* FontFaceSet::onloading()
  100. {
  101. return event_handler_attribute(HTML::EventNames::loading);
  102. }
  103. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingdone
  104. void FontFaceSet::set_onloadingdone(WebIDL::CallbackType* event_handler)
  105. {
  106. set_event_handler_attribute(HTML::EventNames::loadingdone, event_handler);
  107. }
  108. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingdone
  109. WebIDL::CallbackType* FontFaceSet::onloadingdone()
  110. {
  111. return event_handler_attribute(HTML::EventNames::loadingdone);
  112. }
  113. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingerror
  114. void FontFaceSet::set_onloadingerror(WebIDL::CallbackType* event_handler)
  115. {
  116. set_event_handler_attribute(HTML::EventNames::loadingerror, event_handler);
  117. }
  118. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingerror
  119. WebIDL::CallbackType* FontFaceSet::onloadingerror()
  120. {
  121. return event_handler_attribute(HTML::EventNames::loadingerror);
  122. }
  123. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-load
  124. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> FontFaceSet::load(String const&, String const&)
  125. {
  126. // FIXME: Do the steps
  127. auto promise = WebIDL::create_rejected_promise(realm(), WebIDL::NotSupportedError::create(realm(), "FontFaceSet::load is not yet implemented"_fly_string));
  128. return verify_cast<JS::Promise>(*promise->promise());
  129. }
  130. // https://drafts.csswg.org/css-font-loading/#font-face-set-ready
  131. JS::NonnullGCPtr<JS::Promise> FontFaceSet::ready() const
  132. {
  133. return verify_cast<JS::Promise>(*m_ready_promise->promise());
  134. }
  135. }