FontFaceSet.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/ExceptionOrUtils.h>
  11. #include <LibWeb/Bindings/FontFaceSetPrototype.h>
  12. #include <LibWeb/Bindings/Intrinsics.h>
  13. #include <LibWeb/CSS/FontFace.h>
  14. #include <LibWeb/CSS/FontFaceSet.h>
  15. #include <LibWeb/CSS/Parser/Parser.h>
  16. #include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  19. #include <LibWeb/DOM/Document.h>
  20. #include <LibWeb/HTML/EventNames.h>
  21. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  22. #include <LibWeb/Platform/EventLoopPlugin.h>
  23. #include <LibWeb/WebIDL/Promise.h>
  24. namespace Web::CSS {
  25. JS_DEFINE_ALLOCATOR(FontFaceSet);
  26. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-fontfaceset
  27. JS::NonnullGCPtr<FontFaceSet> FontFaceSet::construct_impl(JS::Realm& realm, Vector<JS::Handle<FontFace>> const& initial_faces)
  28. {
  29. auto ready_promise = WebIDL::create_promise(realm);
  30. auto set_entries = JS::Set::create(realm);
  31. // The FontFaceSet constructor, when called, must iterate its initialFaces argument and add each value to its set entries.
  32. for (auto const& face : initial_faces)
  33. set_entries->set_add(face);
  34. return realm.heap().allocate<FontFaceSet>(realm, realm, ready_promise, set_entries);
  35. }
  36. JS::NonnullGCPtr<FontFaceSet> FontFaceSet::create(JS::Realm& realm)
  37. {
  38. return construct_impl(realm, {});
  39. }
  40. FontFaceSet::FontFaceSet(JS::Realm& realm, JS::NonnullGCPtr<WebIDL::Promise> ready_promise, JS::NonnullGCPtr<JS::Set> set_entries)
  41. : DOM::EventTarget(realm)
  42. , m_set_entries(set_entries)
  43. , m_ready_promise(ready_promise)
  44. , m_status(Bindings::FontFaceSetLoadStatus::Loaded)
  45. {
  46. }
  47. void FontFaceSet::initialize(JS::Realm& realm)
  48. {
  49. Base::initialize(realm);
  50. WEB_SET_PROTOTYPE_FOR_INTERFACE(FontFaceSet);
  51. }
  52. void FontFaceSet::visit_edges(Cell::Visitor& visitor)
  53. {
  54. Base::visit_edges(visitor);
  55. visitor.visit(m_set_entries);
  56. visitor.visit(m_ready_promise);
  57. visitor.visit(m_loading_fonts);
  58. visitor.visit(m_loaded_fonts);
  59. visitor.visit(m_failed_fonts);
  60. }
  61. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-add
  62. WebIDL::ExceptionOr<JS::NonnullGCPtr<FontFaceSet>>
  63. FontFaceSet::add(JS::Handle<FontFace> face)
  64. {
  65. // 1. If font is already in the FontFaceSet’s set entries, skip to the last step of this algorithm immediately.
  66. if (m_set_entries->set_has(face))
  67. return JS::NonnullGCPtr<FontFaceSet>(*this);
  68. // 2. If font is CSS-connected, throw an InvalidModificationError exception and exit this algorithm immediately.
  69. if (face->is_css_connected()) {
  70. return WebIDL::InvalidModificationError::create(realm(), "Cannot add a CSS-connected FontFace to a FontFaceSet"_string);
  71. }
  72. // 3. Add the font argument to the FontFaceSet’s set entries.
  73. m_set_entries->set_add(face);
  74. // 4. If font’s status attribute is "loading"
  75. if (face->status() == Bindings::FontFaceLoadStatus::Loading) {
  76. // 1. If the FontFaceSet’s [[LoadingFonts]] list is empty, switch the FontFaceSet to loading.
  77. if (m_loading_fonts.is_empty()) {
  78. m_status = Bindings::FontFaceSetLoadStatus::Loading;
  79. }
  80. // 2. Append font to the FontFaceSet’s [[LoadingFonts]] list.
  81. m_loading_fonts.append(*face);
  82. }
  83. // 5. Return the FontFaceSet.
  84. return JS::NonnullGCPtr<FontFaceSet>(*this);
  85. }
  86. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-delete
  87. bool FontFaceSet::delete_(JS::Handle<FontFace> face)
  88. {
  89. // 1. If font is CSS-connected, return false and exit this algorithm immediately.
  90. if (face->is_css_connected()) {
  91. return false;
  92. }
  93. // 2. Let deleted be the result of removing font from the FontFaceSet’s set entries.
  94. bool deleted = m_set_entries->set_remove(face);
  95. // 3. If font is present in the FontFaceSet’s [[LoadedFonts]], or [[FailedFonts]] lists, remove it.
  96. m_loaded_fonts.remove_all_matching([face](auto const& entry) { return entry == face; });
  97. m_failed_fonts.remove_all_matching([face](auto const& entry) { return entry == face; });
  98. // 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.
  99. m_loading_fonts.remove_all_matching([face](auto const& entry) { return entry == face; });
  100. if (m_loading_fonts.is_empty()) {
  101. m_status = Bindings::FontFaceSetLoadStatus::Loaded;
  102. }
  103. return deleted;
  104. }
  105. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-clear
  106. void FontFaceSet::clear()
  107. {
  108. // FIXME: Do the actual spec steps
  109. m_set_entries->set_clear();
  110. }
  111. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloading
  112. void FontFaceSet::set_onloading(WebIDL::CallbackType* event_handler)
  113. {
  114. set_event_handler_attribute(HTML::EventNames::loading, event_handler);
  115. }
  116. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloading
  117. WebIDL::CallbackType* FontFaceSet::onloading()
  118. {
  119. return event_handler_attribute(HTML::EventNames::loading);
  120. }
  121. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingdone
  122. void FontFaceSet::set_onloadingdone(WebIDL::CallbackType* event_handler)
  123. {
  124. set_event_handler_attribute(HTML::EventNames::loadingdone, event_handler);
  125. }
  126. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingdone
  127. WebIDL::CallbackType* FontFaceSet::onloadingdone()
  128. {
  129. return event_handler_attribute(HTML::EventNames::loadingdone);
  130. }
  131. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingerror
  132. void FontFaceSet::set_onloadingerror(WebIDL::CallbackType* event_handler)
  133. {
  134. set_event_handler_attribute(HTML::EventNames::loadingerror, event_handler);
  135. }
  136. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-onloadingerror
  137. WebIDL::CallbackType* FontFaceSet::onloadingerror()
  138. {
  139. return event_handler_attribute(HTML::EventNames::loadingerror);
  140. }
  141. // https://drafts.csswg.org/css-font-loading/#find-the-matching-font-faces
  142. static WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Set>> find_matching_font_faces(JS::Realm& realm, FontFaceSet& font_face_set, String const& font, String const&)
  143. {
  144. // 1. Parse font using the CSS value syntax of the font property. If a syntax error occurs, return a syntax error.
  145. auto parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext(realm), font);
  146. auto property = parser.parse_as_css_value(PropertyID::Font);
  147. if (!property)
  148. return WebIDL::SyntaxError::create(realm, "Unable to parse font"_string);
  149. // If the parsed value is a CSS-wide keyword, return a syntax error.
  150. if (property->is_css_wide_keyword())
  151. return WebIDL::SyntaxError::create(realm, "Parsed font is a CSS-wide keyword"_string);
  152. // FIXME: Absolutize all relative lengths against the initial values of the corresponding properties. (For example, a
  153. // relative font weight like bolder is evaluated against the initial value normal.)
  154. // FIXME: 2. If text was not explicitly provided, let it be a string containing a single space character (U+0020 SPACE).
  155. // 3. Let font family list be the list of font families parsed from font, and font style be the other font style
  156. // attributes parsed from font.
  157. auto const& font_family_list = property->as_shorthand().longhand(PropertyID::FontFamily)->as_value_list();
  158. // 4. Let available font faces be the available font faces within source. If the allow system fonts flag is specified,
  159. // add all system fonts to available font faces.
  160. auto available_font_faces = font_face_set.set_entries();
  161. // 5. Let matched font faces initially be an empty list.
  162. auto matched_font_faces = JS::Set::create(realm);
  163. // 6. For each family in font family list, use the font matching rules to select the font faces from available font
  164. // faces that match the font style, and add them to matched font faces. The use of the unicodeRange attribute means
  165. // that this may be more than just a single font face.
  166. for (auto const& font_family : font_family_list.values()) {
  167. // FIXME: The matching below is super basic. We currently just match font family names by their string value.
  168. if (!font_family->is_string())
  169. continue;
  170. auto const& font_family_name = font_family->as_string().string_value();
  171. for (auto font_face_value : *available_font_faces) {
  172. auto& font_face = verify_cast<FontFace>(font_face_value.key.as_object());
  173. if (font_face.family() != font_family_name)
  174. continue;
  175. matched_font_faces->set_add(font_face_value.key);
  176. }
  177. }
  178. // FIXME: 7. If matched font faces is empty, set the found faces flag to false. Otherwise, set it to true.
  179. // FIXME: 8. For each font face in matched font faces, if its defined unicode-range does not include the codepoint of at
  180. // least one character in text, remove it from the list.
  181. // 9. Return matched font faces and the found faces flag.
  182. return matched_font_faces;
  183. }
  184. // https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-load
  185. JS::ThrowCompletionOr<JS::NonnullGCPtr<WebIDL::Promise>> FontFaceSet::load(String const& font, String const& text)
  186. {
  187. auto& realm = this->realm();
  188. // 1. Let font face set be the FontFaceSet object this method was called on. Let promise be a newly-created promise object.
  189. JS::NonnullGCPtr font_face_set = *this;
  190. auto promise = WebIDL::create_promise(realm);
  191. Platform::EventLoopPlugin::the().deferred_invoke(JS::create_heap_function(realm.heap(), [&realm, font_face_set, promise, font, text]() mutable {
  192. // 3. Find the matching font faces from font face set using the font and text arguments passed to the function,
  193. // and let font face list be the return value (ignoring the found faces flag). If a syntax error was returned,
  194. // reject promise with a SyntaxError exception and terminate these steps.
  195. auto result = find_matching_font_faces(realm, font_face_set, font, text);
  196. if (result.is_error()) {
  197. HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
  198. WebIDL::reject_promise(realm, promise, Bindings::dom_exception_to_throw_completion(realm.vm(), result.release_error()).release_value().value());
  199. return;
  200. }
  201. auto matched_font_faces = result.release_value();
  202. // 4. Queue a task to run the following steps synchronously:
  203. HTML::queue_a_task(HTML::Task::Source::FontLoading, nullptr, nullptr, JS::create_heap_function(realm.heap(), [&realm, promise, matched_font_faces] {
  204. JS::MarkedVector<JS::NonnullGCPtr<WebIDL::Promise>> promises(realm.heap());
  205. // 1. For all of the font faces in the font face list, call their load() method.
  206. for (auto font_face_value : *matched_font_faces) {
  207. auto& font_face = verify_cast<FontFace>(font_face_value.key.as_object());
  208. font_face.load();
  209. promises.append(font_face.font_status_promise());
  210. }
  211. // 2. Resolve promise with the result of waiting for all of the [[FontStatusPromise]]s of each font face in
  212. // the font face list, in order.
  213. HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
  214. WebIDL::wait_for_all(
  215. realm, promises,
  216. [&realm, promise](auto const&) {
  217. HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
  218. WebIDL::resolve_promise(realm, promise);
  219. },
  220. [&realm, promise](auto error) {
  221. HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
  222. WebIDL::reject_promise(realm, promise, error);
  223. });
  224. }));
  225. }));
  226. // 2. Return promise. Complete the rest of these steps asynchronously.
  227. return promise;
  228. }
  229. // https://drafts.csswg.org/css-font-loading/#font-face-set-ready
  230. JS::NonnullGCPtr<WebIDL::Promise> FontFaceSet::ready() const
  231. {
  232. return m_ready_promise;
  233. }
  234. void FontFaceSet::resolve_ready_promise()
  235. {
  236. WebIDL::resolve_promise(realm(), m_ready_promise);
  237. }
  238. }