FontFaceSet.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // FIXME: Do the actual spec steps
  82. return m_set_entries->set_remove(face);
  83. }
  84. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-clear
  85. void FontFaceSet::clear()
  86. {
  87. // FIXME: Do the actual spec steps
  88. m_set_entries->set_clear();
  89. }
  90. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloading
  91. void FontFaceSet::set_onloading(WebIDL::CallbackType* event_handler)
  92. {
  93. set_event_handler_attribute(HTML::EventNames::loading, event_handler);
  94. }
  95. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloading
  96. WebIDL::CallbackType* FontFaceSet::onloading()
  97. {
  98. return event_handler_attribute(HTML::EventNames::loading);
  99. }
  100. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingdone
  101. void FontFaceSet::set_onloadingdone(WebIDL::CallbackType* event_handler)
  102. {
  103. set_event_handler_attribute(HTML::EventNames::loadingdone, event_handler);
  104. }
  105. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingdone
  106. WebIDL::CallbackType* FontFaceSet::onloadingdone()
  107. {
  108. return event_handler_attribute(HTML::EventNames::loadingdone);
  109. }
  110. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingerror
  111. void FontFaceSet::set_onloadingerror(WebIDL::CallbackType* event_handler)
  112. {
  113. set_event_handler_attribute(HTML::EventNames::loadingerror, event_handler);
  114. }
  115. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingerror
  116. WebIDL::CallbackType* FontFaceSet::onloadingerror()
  117. {
  118. return event_handler_attribute(HTML::EventNames::loadingerror);
  119. }
  120. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-load
  121. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> FontFaceSet::load(String const&, String const&)
  122. {
  123. // FIXME: Do the steps
  124. auto promise = WebIDL::create_rejected_promise(realm(), WebIDL::NotSupportedError::create(realm(), "FontFaceSet::load is not yet implemented"_fly_string));
  125. return verify_cast<JS::Promise>(*promise->promise());
  126. }
  127. // https://drafts.csswg.org/css-font-loading/#font-face-set-ready
  128. JS::NonnullGCPtr<JS::Promise> FontFaceSet::ready() const
  129. {
  130. return verify_cast<JS::Promise>(*m_ready_promise->promise());
  131. }
  132. void FontFaceSet::resolve_ready_promise()
  133. {
  134. WebIDL::resolve_promise(realm(), *m_ready_promise);
  135. }
  136. }