LibWeb: Implement initial CSSFontFaceRule and FontFace classes

For now, this is the bare minimum that's needed: font-family and src.
This commit is contained in:
Sam Atkins 2022-03-28 20:30:26 +01:00 committed by Andreas Kling
parent 1dcde57922
commit 804b8c85e8
Notes: sideshowbarker 2024-07-17 16:37:03 +09:00
13 changed files with 173 additions and 6 deletions

View File

@ -5,9 +5,11 @@
*/
#include <AK/TypeCasts.h>
#include <LibWeb/Bindings/CSSFontFaceRuleWrapper.h>
#include <LibWeb/Bindings/CSSRuleWrapper.h>
#include <LibWeb/Bindings/CSSRuleWrapperFactory.h>
#include <LibWeb/Bindings/CSSStyleRuleWrapper.h>
#include <LibWeb/CSS/CSSFontFaceRule.h>
#include <LibWeb/CSS/CSSStyleRule.h>
namespace Web::Bindings {
@ -19,6 +21,8 @@ CSSRuleWrapper* wrap(JS::GlobalObject& global_object, CSS::CSSRule& rule)
if (is<CSS::CSSStyleRule>(rule))
return static_cast<CSSRuleWrapper*>(wrap_impl(global_object, verify_cast<CSS::CSSStyleRule>(rule)));
if (is<CSS::CSSFontFaceRule>(rule))
return static_cast<CSSRuleWrapper*>(wrap_impl(global_object, verify_cast<CSS::CSSFontFaceRule>(rule)));
return static_cast<CSSRuleWrapper*>(wrap_impl(global_object, rule));
}

View File

@ -15,6 +15,8 @@
#include <LibWeb/Bindings/AbstractRangeConstructor.h>
#include <LibWeb/Bindings/AbstractRangePrototype.h>
#include <LibWeb/Bindings/AudioConstructor.h>
#include <LibWeb/Bindings/CSSFontFaceRuleConstructor.h>
#include <LibWeb/Bindings/CSSFontFaceRulePrototype.h>
#include <LibWeb/Bindings/CSSRuleConstructor.h>
#include <LibWeb/Bindings/CSSRuleListConstructor.h>
#include <LibWeb/Bindings/CSSRuleListPrototype.h>
@ -340,6 +342,7 @@
ADD_WINDOW_OBJECT_INTERFACE(AbortSignal) \
ADD_WINDOW_OBJECT_INTERFACE(AbstractRange) \
ADD_WINDOW_OBJECT_INTERFACE(Crypto) \
ADD_WINDOW_OBJECT_INTERFACE(CSSFontFaceRule) \
ADD_WINDOW_OBJECT_INTERFACE(CSSRule) \
ADD_WINDOW_OBJECT_INTERFACE(CSSRuleList) \
ADD_WINDOW_OBJECT_INTERFACE(CSSStyleDeclaration) \

View File

@ -22,6 +22,7 @@ set(SOURCES
CSS/CSSConditionRule.cpp
CSS/CSSGroupingRule.cpp
CSS/CSSImportRule.cpp
CSS/CSSFontFaceRule.cpp
CSS/CSSMediaRule.cpp
CSS/CSSRule.cpp
CSS/CSSRuleList.cpp
@ -31,6 +32,7 @@ set(SOURCES
CSS/CSSSupportsRule.cpp
CSS/DefaultStyleSheetSource.cpp
CSS/Display.cpp
CSS/FontFace.cpp
CSS/Frequency.cpp
CSS/Length.cpp
CSS/MediaFeatureID.cpp
@ -433,6 +435,7 @@ endfunction()
libweb_js_wrapper(Crypto/Crypto)
libweb_js_wrapper(Crypto/SubtleCrypto)
libweb_js_wrapper(CSS/CSSFontFaceRule)
libweb_js_wrapper(CSS/CSSRule)
libweb_js_wrapper(CSS/CSSRuleList)
libweb_js_wrapper(CSS/CSSStyleDeclaration)

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/CSSFontFaceRule.h>
namespace Web::CSS {
CSSFontFaceRule::CSSFontFaceRule(FontFace&& font_face)
: m_font_face(move(font_face))
{
}
CSSStyleDeclaration* CSSFontFaceRule::style()
{
// FIXME: Return a CSSStyleDeclaration subclass that directs changes to the FontFace.
return nullptr;
}
// https://www.w3.org/TR/cssom/#ref-for-cssfontfacerule
String CSSFontFaceRule::serialized() const
{
// FIXME: Implement this!
return "@font-face { /* FIXME: Implement CSSFontFaceRule::serialized()! */ }";
}
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/FontFace.h>
namespace Web::CSS {
class CSSFontFaceRule final : public CSSRule {
AK_MAKE_NONCOPYABLE(CSSFontFaceRule);
AK_MAKE_NONMOVABLE(CSSFontFaceRule);
public:
using WrapperType = Bindings::CSSFontFaceRuleWrapper;
static NonnullRefPtr<CSSFontFaceRule> create(FontFace&& font_face)
{
return adopt_ref(*new CSSFontFaceRule(move(font_face)));
}
virtual ~CSSFontFaceRule() override = default;
virtual StringView class_name() const override { return "CSSFontFaceRule"; }
virtual Type type() const override { return Type::FontFace; }
FontFace const& font_face() const { return m_font_face; }
CSSStyleDeclaration* style();
private:
explicit CSSFontFaceRule(FontFace&&);
virtual String serialized() const override;
FontFace m_font_face;
};
template<>
inline bool CSSRule::fast_is<CSSFontFaceRule>() const { return type() == CSSRule::Type::FontFace; }
}

View File

@ -0,0 +1,7 @@
#import <CSS/CSSRule.idl>
#import <CSS/CSSStyleDeclaration.idl>
[Exposed=Window]
interface CSSFontFaceRule : CSSRule {
readonly attribute CSSStyleDeclaration style;
};

View File

@ -29,6 +29,7 @@ public:
Import,
Media,
Supports,
FontFace,
__Count,
};

View File

@ -78,8 +78,7 @@ void CSSRuleList::for_each_effective_style_rule(Function<void(CSSStyleRule const
{
for (auto const& rule : m_rules) {
switch (rule.type()) {
case CSSRule::Type::Style:
callback(static_cast<CSSStyleRule const&>(rule));
case CSSRule::Type::FontFace:
break;
case CSSRule::Type::Import: {
auto const& import_rule = static_cast<CSSImportRule const&>(rule);
@ -90,6 +89,9 @@ void CSSRuleList::for_each_effective_style_rule(Function<void(CSSStyleRule const
case CSSRule::Type::Media:
static_cast<CSSMediaRule const&>(rule).for_each_effective_style_rule(callback);
break;
case CSSRule::Type::Style:
callback(static_cast<CSSStyleRule const&>(rule));
break;
case CSSRule::Type::Supports:
static_cast<CSSSupportsRule const&>(rule).for_each_effective_style_rule(callback);
break;
@ -105,7 +107,7 @@ bool CSSRuleList::evaluate_media_queries(HTML::Window const& window)
for (auto& rule : m_rules) {
switch (rule.type()) {
case CSSRule::Type::Style:
case CSSRule::Type::FontFace:
break;
case CSSRule::Type::Import: {
auto& import_rule = verify_cast<CSSImportRule>(rule);
@ -123,6 +125,8 @@ bool CSSRuleList::evaluate_media_queries(HTML::Window const& window)
any_media_queries_changed_match_state = true;
break;
}
case CSSRule::Type::Style:
break;
case CSSRule::Type::Supports: {
auto& supports_rule = verify_cast<CSSSupportsRule>(rule);
if (supports_rule.condition_matches() && supports_rule.css_rules().evaluate_media_queries(window))

View File

@ -0,0 +1,17 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/FontFace.h>
namespace Web::CSS {
FontFace::FontFace(FlyString font_family, Vector<Source> sources)
: m_font_family(move(font_family))
, m_sources(move(sources))
{
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <AK/URL.h>
namespace Web::CSS {
class FontFace {
public:
struct Source {
AK::URL url;
};
FontFace(FlyString font_family, Vector<Source> sources);
~FontFace() = default;
FlyString font_family() const { return m_font_family; }
Vector<Source> const& sources() const { return m_sources; }
// FIXME: font-style, font-weight, font-stretch, unicode-range, font-feature-settings
private:
FlyString m_font_family;
Vector<Source> m_sources;
};
}

View File

@ -8,6 +8,7 @@
#include <AK/QuickSort.h>
#include <AK/StringBuilder.h>
#include <AK/Utf8View.h>
#include <LibWeb/CSS/CSSFontFaceRule.h>
#include <LibWeb/CSS/CSSImportRule.h>
#include <LibWeb/CSS/CSSMediaRule.h>
#include <LibWeb/CSS/CSSRule.h>
@ -547,8 +548,8 @@ void dump_rule(StringBuilder& builder, CSS::CSSRule const& rule, int indent_leve
builder.appendff("{}:\n", rule.class_name());
switch (rule.type()) {
case CSS::CSSRule::Type::Style:
dump_style_rule(builder, verify_cast<CSS::CSSStyleRule const>(rule), indent_levels);
case CSS::CSSRule::Type::FontFace:
dump_font_face_rule(builder, verify_cast<CSS::CSSFontFaceRule const>(rule), indent_levels);
break;
case CSS::CSSRule::Type::Import:
dump_import_rule(builder, verify_cast<CSS::CSSImportRule const>(rule), indent_levels);
@ -556,6 +557,9 @@ void dump_rule(StringBuilder& builder, CSS::CSSRule const& rule, int indent_leve
case CSS::CSSRule::Type::Media:
dump_media_rule(builder, verify_cast<CSS::CSSMediaRule const>(rule), indent_levels);
break;
case CSS::CSSRule::Type::Style:
dump_style_rule(builder, verify_cast<CSS::CSSStyleRule const>(rule), indent_levels);
break;
case CSS::CSSRule::Type::Supports:
dump_supports_rule(builder, verify_cast<CSS::CSSSupportsRule const>(rule), indent_levels);
break;
@ -564,6 +568,20 @@ void dump_rule(StringBuilder& builder, CSS::CSSRule const& rule, int indent_leve
}
}
void dump_font_face_rule(StringBuilder& builder, CSS::CSSFontFaceRule const& rule, int indent_levels)
{
auto& font_face = rule.font_face();
indent(builder, indent_levels + 1);
builder.appendff("font-family: {}\n", font_face.font_family());
indent(builder, indent_levels + 1);
builder.append("sources:\n");
for (auto const& source : font_face.sources()) {
indent(builder, indent_levels + 2);
builder.appendff("{}\n", source.url);
}
}
void dump_import_rule(StringBuilder& builder, CSS::CSSImportRule const& rule, int indent_levels)
{
indent(builder, indent_levels);

View File

@ -20,9 +20,10 @@ void dump_sheet(StringBuilder&, CSS::StyleSheet const&);
void dump_sheet(CSS::StyleSheet const&);
void dump_rule(StringBuilder&, CSS::CSSRule const&, int indent_levels = 0);
void dump_rule(CSS::CSSRule const&);
void dump_style_rule(StringBuilder&, CSS::CSSStyleRule const&, int indent_levels = 0);
void dump_font_face_rule(StringBuilder&, CSS::CSSFontFaceRule const&, int indent_levels = 0);
void dump_import_rule(StringBuilder&, CSS::CSSImportRule const&, int indent_levels = 0);
void dump_media_rule(StringBuilder&, CSS::CSSMediaRule const&, int indent_levels = 0);
void dump_style_rule(StringBuilder&, CSS::CSSStyleRule const&, int indent_levels = 0);
void dump_supports_rule(StringBuilder&, CSS::CSSSupportsRule const&, int indent_levels = 0);
void dump_selector(StringBuilder&, CSS::Selector const&);
void dump_selector(CSS::Selector const&);

View File

@ -31,6 +31,7 @@ class CalculatedStyleValue;
class ColorStyleValue;
class ContentStyleValue;
class CSSImportRule;
class CSSFontFaceRule;
class CSSMediaRule;
class CSSRule;
class CSSRuleList;
@ -42,6 +43,7 @@ class Display;
class ElementInlineCSSStyleDeclaration;
class FlexFlowStyleValue;
class FlexStyleValue;
class FontFace;
class FontStyleValue;
class Frequency;
class FrequencyPercentage;
@ -381,6 +383,7 @@ class CharacterDataWrapper;
class CloseEventWrapper;
class CommentWrapper;
class CryptoWrapper;
class CSSFontFaceRuleWrapper;
class CSSRuleListWrapper;
class CSSRuleWrapper;
class CSSStyleDeclarationWrapper;