mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-08 01:00:05 +00:00
LibWeb/CSS: Parse FontFace parameters as descriptors
This also rearranges the code to follow the spec better: We create an empty FontFace first and then fill it in, instead of creating it fully-formed at the end.
This commit is contained in:
parent
0383ecaa3d
commit
ff491843b6
2 changed files with 65 additions and 57 deletions
|
@ -5,6 +5,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/ByteBuffer.h>
|
||||||
#include <LibCore/Promise.h>
|
#include <LibCore/Promise.h>
|
||||||
#include <LibGC/Heap.h>
|
#include <LibGC/Heap.h>
|
||||||
#include <LibGfx/Font/Typeface.h>
|
#include <LibGfx/Font/Typeface.h>
|
||||||
|
@ -66,63 +67,82 @@ GC::Ref<FontFace> FontFace::construct_impl(JS::Realm& realm, String family, Font
|
||||||
|
|
||||||
// 1. Let font face be a fresh FontFace object. Set font face’s status attribute to "unloaded",
|
// 1. Let font face be a fresh FontFace object. Set font face’s status attribute to "unloaded",
|
||||||
// Set its internal [[FontStatusPromise]] slot to a fresh pending Promise object.
|
// Set its internal [[FontStatusPromise]] slot to a fresh pending Promise object.
|
||||||
auto promise = WebIDL::create_promise(realm);
|
auto font_face = realm.create<FontFace>(realm, WebIDL::create_promise(realm));
|
||||||
|
|
||||||
// FIXME: Parse the family argument, and the members of the descriptors argument,
|
// Parse the family argument, and the members of the descriptors argument,
|
||||||
// according to the grammars of the corresponding descriptors of the CSS @font-face rule.
|
// according to the grammars of the corresponding descriptors of the CSS @font-face rule.
|
||||||
// If the source argument is a CSSOMString, parse it according to the grammar of the CSS src descriptor of the @font-face rule.
|
// If the source argument is a CSSOMString, parse it according to the grammar of the CSS src descriptor of the @font-face rule.
|
||||||
// If any of them fail to parse correctly, reject font face’s [[FontStatusPromise]] with a DOMException named "SyntaxError",
|
// If any of them fail to parse correctly, reject font face’s [[FontStatusPromise]] with a DOMException named "SyntaxError",
|
||||||
// set font face’s corresponding attributes to the empty string, and set font face’s status attribute to "error".
|
// set font face’s corresponding attributes to the empty string, and set font face’s status attribute to "error".
|
||||||
// Otherwise, set font face’s corresponding attributes to the serialization of the parsed values.
|
// Otherwise, set font face’s corresponding attributes to the serialization of the parsed values.
|
||||||
|
|
||||||
// 2. (Out of order) If the source argument was a CSSOMString, set font face’s internal [[Urls]]
|
Parser::ParsingParams parsing_params { realm, base_url };
|
||||||
// slot to the string.
|
auto try_parse_descriptor = [&parsing_params, &font_face, &realm](DescriptorID descriptor_id, String const& string) -> String {
|
||||||
// If the source argument was a BinaryData, set font face’s internal [[Data]] slot
|
auto result = parse_css_descriptor(parsing_params, AtRuleID::FontFace, descriptor_id, string);
|
||||||
// to the passed argument.
|
if (!result) {
|
||||||
Vector<CSS::ParsedFontFace::Source> sources;
|
font_face->reject_status_promise(WebIDL::SyntaxError::create(realm, MUST(String::formatted("FontFace constructor: Invalid {}", to_string(descriptor_id)))));
|
||||||
ByteBuffer buffer;
|
return {};
|
||||||
if (auto* string = source.get_pointer<String>()) {
|
}
|
||||||
auto parser = CSS::Parser::Parser::create(CSS::Parser::ParsingParams(realm, base_url), *string);
|
return result->to_string(CSSStyleValue::SerializationMode::Normal);
|
||||||
sources = parser.parse_as_font_face_src();
|
};
|
||||||
if (sources.is_empty())
|
font_face->m_family = try_parse_descriptor(DescriptorID::FontFamily, family);
|
||||||
WebIDL::reject_promise(realm, promise, WebIDL::SyntaxError::create(realm, "FontFace constructor: Invalid source string"_string));
|
font_face->m_style = try_parse_descriptor(DescriptorID::FontStyle, descriptors.style);
|
||||||
|
font_face->m_weight = try_parse_descriptor(DescriptorID::FontWeight, descriptors.weight);
|
||||||
|
font_face->m_stretch = try_parse_descriptor(DescriptorID::FontWidth, descriptors.stretch);
|
||||||
|
font_face->m_unicode_range = try_parse_descriptor(DescriptorID::UnicodeRange, descriptors.unicode_range);
|
||||||
|
font_face->m_feature_settings = try_parse_descriptor(DescriptorID::FontFeatureSettings, descriptors.feature_settings);
|
||||||
|
font_face->m_variation_settings = try_parse_descriptor(DescriptorID::FontVariationSettings, descriptors.variation_settings);
|
||||||
|
font_face->m_display = try_parse_descriptor(DescriptorID::FontDisplay, descriptors.display);
|
||||||
|
font_face->m_ascent_override = try_parse_descriptor(DescriptorID::AscentOverride, descriptors.ascent_override);
|
||||||
|
font_face->m_descent_override = try_parse_descriptor(DescriptorID::DescentOverride, descriptors.descent_override);
|
||||||
|
font_face->m_line_gap_override = try_parse_descriptor(DescriptorID::LineGapOverride, descriptors.line_gap_override);
|
||||||
|
RefPtr<CSSStyleValue> parsed_source;
|
||||||
|
if (auto* source_string = source.get_pointer<String>()) {
|
||||||
|
parsed_source = parse_css_descriptor(parsing_params, AtRuleID::FontFace, DescriptorID::Src, *source_string);
|
||||||
|
if (!parsed_source) {
|
||||||
|
font_face->reject_status_promise(WebIDL::SyntaxError::create(realm, MUST(String::formatted("FontFace constructor: Invalid {}", to_string(DescriptorID::Src)))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Return font face. If font face’s status is "error", terminate this algorithm;
|
||||||
|
// otherwise, complete the rest of these steps asynchronously.
|
||||||
|
// FIXME: Do the rest of this asynchronously.
|
||||||
|
if (font_face->status() == Bindings::FontFaceLoadStatus::Error)
|
||||||
|
return font_face;
|
||||||
|
|
||||||
|
// 2. If the source argument was a CSSOMString, set font face’s internal [[Urls]] slot to the string.
|
||||||
|
// If the source argument was a BinaryData, set font face’s internal [[Data]] slot to the passed argument.
|
||||||
|
if (source.has<String>()) {
|
||||||
|
font_face->m_urls = ParsedFontFace::sources_from_style_value(*parsed_source);
|
||||||
} else {
|
} else {
|
||||||
auto buffer_source = source.get<GC::Root<WebIDL::BufferSource>>();
|
auto buffer_source = source.get<GC::Root<WebIDL::BufferSource>>();
|
||||||
auto maybe_buffer = WebIDL::get_buffer_source_copy(buffer_source->raw_object());
|
auto maybe_buffer = WebIDL::get_buffer_source_copy(buffer_source->raw_object());
|
||||||
if (maybe_buffer.is_error()) {
|
if (maybe_buffer.is_error()) {
|
||||||
VERIFY(maybe_buffer.error().code() == ENOMEM);
|
VERIFY(maybe_buffer.error().code() == ENOMEM);
|
||||||
auto throw_completion = vm.throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory));
|
auto throw_completion = vm.throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory));
|
||||||
WebIDL::reject_promise(realm, promise, throw_completion.value());
|
font_face->reject_status_promise(throw_completion.value());
|
||||||
} else {
|
} else {
|
||||||
buffer = maybe_buffer.release_value();
|
font_face->m_binary_data = maybe_buffer.release_value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer.is_empty() && sources.is_empty())
|
if (font_face->m_binary_data.is_empty() && font_face->m_urls.is_empty())
|
||||||
WebIDL::reject_promise(realm, promise, WebIDL::SyntaxError::create(realm, "FontFace constructor: Invalid font source"_string));
|
font_face->reject_status_promise(WebIDL::SyntaxError::create(realm, "FontFace constructor: Invalid font source"_string));
|
||||||
|
|
||||||
auto font = realm.create<FontFace>(realm, promise, move(sources), move(buffer), move(family), descriptors);
|
|
||||||
|
|
||||||
// 1. (continued) Return font face. If font face’s status is "error", terminate this algorithm;
|
|
||||||
// otherwise, complete the rest of these steps asynchronously.
|
|
||||||
if (font->status() == Bindings::FontFaceLoadStatus::Error)
|
|
||||||
return font;
|
|
||||||
|
|
||||||
// 3. If font face’s [[Data]] slot is not null, queue a task to run the following steps synchronously:
|
// 3. If font face’s [[Data]] slot is not null, queue a task to run the following steps synchronously:
|
||||||
if (font->m_binary_data.is_empty())
|
if (font_face->m_binary_data.is_empty())
|
||||||
return font;
|
return font_face;
|
||||||
|
|
||||||
HTML::queue_global_task(HTML::Task::Source::FontLoading, HTML::relevant_global_object(*font), GC::create_function(vm.heap(), [&realm, font] {
|
HTML::queue_global_task(HTML::Task::Source::FontLoading, HTML::relevant_global_object(*font_face), GC::create_function(vm.heap(), [&realm, font_face] {
|
||||||
// 1. Set font face’s status attribute to "loading".
|
// 1. Set font face’s status attribute to "loading".
|
||||||
font->m_status = Bindings::FontFaceLoadStatus::Loading;
|
font_face->m_status = Bindings::FontFaceLoadStatus::Loading;
|
||||||
|
|
||||||
// 2. FIXME: For each FontFaceSet font face is in:
|
// 2. FIXME: For each FontFaceSet font face is in:
|
||||||
|
|
||||||
// 3. Asynchronously, attempt to parse the data in it as a font.
|
// 3. Asynchronously, attempt to parse the data in it as a font.
|
||||||
// When this is completed, successfully or not, queue a task to run the following steps synchronously:
|
// When this is completed, successfully or not, queue a task to run the following steps synchronously:
|
||||||
font->m_font_load_promise = load_vector_font(realm, font->m_binary_data);
|
font_face->m_font_load_promise = load_vector_font(realm, font_face->m_binary_data);
|
||||||
|
|
||||||
font->m_font_load_promise->when_resolved([font = GC::make_root(font)](auto const& vector_font) -> ErrorOr<void> {
|
font_face->m_font_load_promise->when_resolved([font = GC::make_root(font_face)](auto const& vector_font) -> ErrorOr<void> {
|
||||||
HTML::queue_global_task(HTML::Task::Source::FontLoading, HTML::relevant_global_object(*font), GC::create_function(font->heap(), [font = GC::Ref(*font), vector_font] {
|
HTML::queue_global_task(HTML::Task::Source::FontLoading, HTML::relevant_global_object(*font), GC::create_function(font->heap(), [font = GC::Ref(*font), vector_font] {
|
||||||
HTML::TemporaryExecutionContext context(font->realm(), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
|
HTML::TemporaryExecutionContext context(font->realm(), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
|
||||||
// 1. If the load was successful, font face now represents the parsed font;
|
// 1. If the load was successful, font face now represents the parsed font;
|
||||||
|
@ -139,13 +159,12 @@ GC::Ref<FontFace> FontFace::construct_impl(JS::Realm& realm, String family, Font
|
||||||
}));
|
}));
|
||||||
return {};
|
return {};
|
||||||
});
|
});
|
||||||
font->m_font_load_promise->when_rejected([font = GC::make_root(font)](auto const& error) {
|
font_face->m_font_load_promise->when_rejected([font = GC::make_root(font_face)](auto const& error) {
|
||||||
HTML::queue_global_task(HTML::Task::Source::FontLoading, HTML::relevant_global_object(*font), GC::create_function(font->heap(), [font = GC::Ref(*font), error = Error::copy(error)] {
|
HTML::queue_global_task(HTML::Task::Source::FontLoading, HTML::relevant_global_object(*font), GC::create_function(font->heap(), [font = GC::Ref(*font), error = Error::copy(error)] {
|
||||||
HTML::TemporaryExecutionContext context(font->realm(), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
|
HTML::TemporaryExecutionContext context(font->realm(), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
|
||||||
// 2. Otherwise, reject font face’s [[FontStatusPromise]] with a DOMException named "SyntaxError"
|
// 2. Otherwise, reject font face’s [[FontStatusPromise]] with a DOMException named "SyntaxError"
|
||||||
// and set font face’s status attribute to "error".
|
// and set font face’s status attribute to "error".
|
||||||
font->m_status = Bindings::FontFaceLoadStatus::Error;
|
font->reject_status_promise(WebIDL::SyntaxError::create(font->realm(), MUST(String::formatted("Failed to load font: {}", error))));
|
||||||
WebIDL::reject_promise(font->realm(), font->m_font_status_promise, WebIDL::SyntaxError::create(font->realm(), MUST(String::formatted("Failed to load font: {}", error))));
|
|
||||||
|
|
||||||
// FIXME: For each FontFaceSet font face is in:
|
// FIXME: For each FontFaceSet font face is in:
|
||||||
|
|
||||||
|
@ -154,33 +173,13 @@ GC::Ref<FontFace> FontFace::construct_impl(JS::Realm& realm, String family, Font
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return font;
|
return font_face;
|
||||||
}
|
}
|
||||||
|
|
||||||
FontFace::FontFace(JS::Realm& realm, GC::Ref<WebIDL::Promise> font_status_promise, Vector<ParsedFontFace::Source> urls, ByteBuffer data, String font_family, FontFaceDescriptors const& descriptors)
|
FontFace::FontFace(JS::Realm& realm, GC::Ref<WebIDL::Promise> font_status_promise)
|
||||||
: Bindings::PlatformObject(realm)
|
: Bindings::PlatformObject(realm)
|
||||||
, m_font_status_promise(font_status_promise)
|
, m_font_status_promise(font_status_promise)
|
||||||
, m_urls(move(urls))
|
|
||||||
, m_binary_data(move(data))
|
|
||||||
{
|
{
|
||||||
m_family = move(font_family);
|
|
||||||
m_style = descriptors.style;
|
|
||||||
m_weight = descriptors.weight;
|
|
||||||
m_stretch = descriptors.stretch;
|
|
||||||
m_unicode_range = descriptors.unicode_range;
|
|
||||||
m_feature_settings = descriptors.feature_settings;
|
|
||||||
m_variation_settings = descriptors.variation_settings;
|
|
||||||
m_display = descriptors.display;
|
|
||||||
m_ascent_override = descriptors.ascent_override;
|
|
||||||
m_descent_override = descriptors.descent_override;
|
|
||||||
m_line_gap_override = descriptors.line_gap_override;
|
|
||||||
|
|
||||||
// FIXME: Parse from descriptor
|
|
||||||
// FIXME: Have gettter reflect this member instead of the string
|
|
||||||
m_unicode_ranges.empend(0x0u, 0x10FFFFu);
|
|
||||||
|
|
||||||
if (as<JS::Promise>(*m_font_status_promise->promise()).state() == JS::Promise::State::Rejected)
|
|
||||||
m_status = Bindings::FontFaceLoadStatus::Error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FontFace::~FontFace() = default;
|
FontFace::~FontFace() = default;
|
||||||
|
@ -204,6 +203,14 @@ GC::Ref<WebIDL::Promise> FontFace::loaded() const
|
||||||
return m_font_status_promise;
|
return m_font_status_promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FontFace::reject_status_promise(JS::Value reason)
|
||||||
|
{
|
||||||
|
if (m_status != Bindings::FontFaceLoadStatus::Error) {
|
||||||
|
WebIDL::reject_promise(realm(), m_font_status_promise, reason);
|
||||||
|
m_status = Bindings::FontFaceLoadStatus::Error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// https://drafts.csswg.org/css-font-loading/#dom-fontface-family
|
// https://drafts.csswg.org/css-font-loading/#dom-fontface-family
|
||||||
WebIDL::ExceptionOr<void> FontFace::set_family(String const& string)
|
WebIDL::ExceptionOr<void> FontFace::set_family(String const& string)
|
||||||
{
|
{
|
||||||
|
|
|
@ -80,10 +80,11 @@ public:
|
||||||
GC::Ref<WebIDL::Promise> font_status_promise() { return m_font_status_promise; }
|
GC::Ref<WebIDL::Promise> font_status_promise() { return m_font_status_promise; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FontFace(JS::Realm&, GC::Ref<WebIDL::Promise> font_status_promise, Vector<ParsedFontFace::Source> urls, ByteBuffer data, String family, FontFaceDescriptors const& descriptors);
|
FontFace(JS::Realm&, GC::Ref<WebIDL::Promise> font_status_promise);
|
||||||
|
|
||||||
virtual void initialize(JS::Realm&) override;
|
virtual void initialize(JS::Realm&) override;
|
||||||
virtual void visit_edges(Visitor&) override;
|
virtual void visit_edges(Visitor&) override;
|
||||||
|
void reject_status_promise(JS::Value reason);
|
||||||
|
|
||||||
// FIXME: Should we be storing StyleValues instead?
|
// FIXME: Should we be storing StyleValues instead?
|
||||||
String m_family;
|
String m_family;
|
||||||
|
@ -104,7 +105,7 @@ private:
|
||||||
|
|
||||||
GC::Ref<WebIDL::Promise> m_font_status_promise; // [[FontStatusPromise]]
|
GC::Ref<WebIDL::Promise> m_font_status_promise; // [[FontStatusPromise]]
|
||||||
Vector<ParsedFontFace::Source> m_urls; // [[Urls]]
|
Vector<ParsedFontFace::Source> m_urls; // [[Urls]]
|
||||||
ByteBuffer m_binary_data; // [[Data]]
|
ByteBuffer m_binary_data {}; // [[Data]]
|
||||||
|
|
||||||
RefPtr<Gfx::Typeface> m_parsed_font;
|
RefPtr<Gfx::Typeface> m_parsed_font;
|
||||||
RefPtr<Core::Promise<NonnullRefPtr<Gfx::Typeface>>> m_font_load_promise;
|
RefPtr<Core::Promise<NonnullRefPtr<Gfx::Typeface>>> m_font_load_promise;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue