FontFaceSet.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibJS/Runtime/Set.h>
  9. #include <LibWeb/Bindings/FontFaceSetPrototype.h>
  10. #include <LibWeb/Bindings/Intrinsics.h>
  11. #include <LibWeb/CSS/FontFace.h>
  12. #include <LibWeb/CSS/FontFaceSet.h>
  13. #include <LibWeb/WebIDL/Promise.h>
  14. namespace Web::CSS {
  15. JS_DEFINE_ALLOCATOR(FontFaceSet);
  16. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-fontfaceset
  17. JS::NonnullGCPtr<FontFaceSet> FontFaceSet::construct_impl(JS::Realm& realm, Vector<JS::Handle<FontFace>> const& initial_faces)
  18. {
  19. auto ready_promise = WebIDL::create_promise(realm);
  20. auto set_entries = JS::Set::create(realm);
  21. // The FontFaceSet constructor, when called, must iterate its initialFaces argument and add each value to its set entries.
  22. for (auto const& face : initial_faces)
  23. set_entries->set_add(face);
  24. if (set_entries->set_size() == 0)
  25. WebIDL::resolve_promise(realm, *ready_promise);
  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. : Bindings::PlatformObject(realm)
  34. , m_set_entries(set_entries)
  35. , m_ready_promise(ready_promise)
  36. {
  37. bool const is_ready = ready()->state() == JS::Promise::State::Fulfilled;
  38. m_status = is_ready ? Bindings::FontFaceSetLoadStatus::Loaded : Bindings::FontFaceSetLoadStatus::Loading;
  39. }
  40. void FontFaceSet::initialize(JS::Realm& realm)
  41. {
  42. Base::initialize(realm);
  43. WEB_SET_PROTOTYPE_FOR_INTERFACE(FontFaceSet);
  44. }
  45. void FontFaceSet::visit_edges(Cell::Visitor& visitor)
  46. {
  47. Base::visit_edges(visitor);
  48. visitor.visit(m_set_entries);
  49. visitor.visit(m_ready_promise);
  50. }
  51. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-add
  52. JS::NonnullGCPtr<FontFaceSet> FontFaceSet::add(JS::Handle<FontFace> face)
  53. {
  54. // FIXME: Do the actual spec steps
  55. m_set_entries->set_add(face);
  56. return *this;
  57. }
  58. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-delete
  59. bool FontFaceSet::delete_(JS::Handle<FontFace> face)
  60. {
  61. // FIXME: Do the actual spec steps
  62. return m_set_entries->set_remove(face);
  63. }
  64. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-clear
  65. void FontFaceSet::clear()
  66. {
  67. // FIXME: Do the actual spec steps
  68. m_set_entries->set_clear();
  69. }
  70. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-load
  71. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> FontFaceSet::load(String const&, String const&)
  72. {
  73. // FIXME: Do the steps
  74. auto promise = WebIDL::create_rejected_promise(realm(), WebIDL::NotSupportedError::create(realm(), "FontFaceSet::load is not yet implemented"_fly_string));
  75. return verify_cast<JS::Promise>(*promise->promise());
  76. }
  77. // https://drafts.csswg.org/css-font-loading/#font-face-set-ready
  78. JS::NonnullGCPtr<JS::Promise> FontFaceSet::ready() const
  79. {
  80. return verify_cast<JS::Promise>(*m_ready_promise->promise());
  81. }
  82. }