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