FontFaceSet.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. }
  53. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-add
  54. JS::NonnullGCPtr<FontFaceSet> FontFaceSet::add(JS::Handle<FontFace> face)
  55. {
  56. // FIXME: Do the actual spec steps
  57. m_set_entries->set_add(face);
  58. return *this;
  59. }
  60. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-delete
  61. bool FontFaceSet::delete_(JS::Handle<FontFace> face)
  62. {
  63. // FIXME: Do the actual spec steps
  64. return m_set_entries->set_remove(face);
  65. }
  66. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-clear
  67. void FontFaceSet::clear()
  68. {
  69. // FIXME: Do the actual spec steps
  70. m_set_entries->set_clear();
  71. }
  72. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloading
  73. void FontFaceSet::set_onloading(WebIDL::CallbackType* event_handler)
  74. {
  75. set_event_handler_attribute(HTML::EventNames::loading, event_handler);
  76. }
  77. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloading
  78. WebIDL::CallbackType* FontFaceSet::onloading()
  79. {
  80. return event_handler_attribute(HTML::EventNames::loading);
  81. }
  82. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingdone
  83. void FontFaceSet::set_onloadingdone(WebIDL::CallbackType* event_handler)
  84. {
  85. set_event_handler_attribute(HTML::EventNames::loadingdone, event_handler);
  86. }
  87. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingdone
  88. WebIDL::CallbackType* FontFaceSet::onloadingdone()
  89. {
  90. return event_handler_attribute(HTML::EventNames::loadingdone);
  91. }
  92. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingerror
  93. void FontFaceSet::set_onloadingerror(WebIDL::CallbackType* event_handler)
  94. {
  95. set_event_handler_attribute(HTML::EventNames::loadingerror, event_handler);
  96. }
  97. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingerror
  98. WebIDL::CallbackType* FontFaceSet::onloadingerror()
  99. {
  100. return event_handler_attribute(HTML::EventNames::loadingerror);
  101. }
  102. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-load
  103. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> FontFaceSet::load(String const&, String const&)
  104. {
  105. // FIXME: Do the steps
  106. auto promise = WebIDL::create_rejected_promise(realm(), WebIDL::NotSupportedError::create(realm(), "FontFaceSet::load is not yet implemented"_fly_string));
  107. return verify_cast<JS::Promise>(*promise->promise());
  108. }
  109. // https://drafts.csswg.org/css-font-loading/#font-face-set-ready
  110. JS::NonnullGCPtr<JS::Promise> FontFaceSet::ready() const
  111. {
  112. return verify_cast<JS::Promise>(*m_ready_promise->promise());
  113. }
  114. }