/* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021-2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Web::CSS { struct MatchingRule { JS::GCPtr rule; JS::GCPtr sheet; size_t style_sheet_index { 0 }; size_t rule_index { 0 }; size_t selector_index { 0 }; u32 specificity { 0 }; bool contains_pseudo_element { false }; }; class PropertyDependencyNode : public RefCounted { public: static NonnullRefPtr create(String name) { return adopt_ref(*new PropertyDependencyNode(move(name))); } void add_child(NonnullRefPtr); bool has_cycles(); private: explicit PropertyDependencyNode(String name); String m_name; Vector> m_children; bool m_marked { false }; }; struct FontFaceKey { FlyString family_name; int weight { 0 }; int slope { 0 }; [[nodiscard]] u32 hash() const { return pair_int_hash(family_name.hash(), pair_int_hash(weight, slope)); } [[nodiscard]] bool operator==(FontFaceKey const&) const = default; }; class StyleComputer { public: explicit StyleComputer(DOM::Document&); ~StyleComputer(); DOM::Document& document() { return m_document; } DOM::Document const& document() const { return m_document; } FontCache& font_cache() const { return m_font_cache; } NonnullRefPtr create_document_style() const; ErrorOr> compute_style(DOM::Element&, Optional = {}) const; ErrorOr> compute_pseudo_element_style_if_needed(DOM::Element&, Optional) const; // https://www.w3.org/TR/css-cascade/#origin enum class CascadeOrigin { Author, User, UserAgent, Animation, Transition, }; Vector collect_matching_rules(DOM::Element const&, CascadeOrigin, Optional) const; void invalidate_rule_cache(); Gfx::Font const& initial_font() const; void did_load_font(FlyString const& family_name); void load_fonts_from_sheet(CSSStyleSheet const&); RefPtr compute_font_for_style_values(DOM::Element const* element, Optional pseudo_element, StyleValue const& font_family, StyleValue const& font_size, StyleValue const& font_style, StyleValue const& font_weight, StyleValue const& font_stretch) const; struct AnimationKey { CSS::CSSStyleDeclaration const* source_declaration; DOM::Element const* element; }; struct AnimationTiming { struct Linear { }; struct CubicBezier { // Regular parameters double x1; double y1; double x2; double y2; struct CachedSample { double x; double y; double t; }; mutable Vector m_cached_x_samples = {}; CachedSample sample_around(double x) const; bool operator==(CubicBezier const& other) const { return x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2; } }; struct Steps { size_t number_of_steps; bool jump_at_start; bool jump_at_end; }; Variant timing_function; }; private: enum class ComputeStyleMode { Normal, CreatePseudoElementStyleIfNeeded, }; class FontLoader; struct MatchingFontCandidate; ErrorOr> compute_style_impl(DOM::Element&, Optional, ComputeStyleMode) const; ErrorOr compute_cascaded_values(StyleProperties&, DOM::Element&, Optional, bool& did_match_any_pseudo_element_rules, ComputeStyleMode) const; static RefPtr find_matching_font_weight_ascending(Vector const& candidates, int target_weight, float font_size_in_pt, bool inclusive); static RefPtr find_matching_font_weight_descending(Vector const& candidates, int target_weight, float font_size_in_pt, bool inclusive); RefPtr font_matching_algorithm(FontFaceKey const& key, float font_size_in_pt) const; void compute_font(StyleProperties&, DOM::Element const*, Optional) const; void compute_defaulted_values(StyleProperties&, DOM::Element const*, Optional) const; void absolutize_values(StyleProperties&, DOM::Element const*, Optional) const; void transform_box_type_if_needed(StyleProperties&, DOM::Element const&, Optional) const; void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID, Optional) const; RefPtr resolve_unresolved_style_value(DOM::Element&, Optional, PropertyID, UnresolvedStyleValue const&) const; bool expand_variables(DOM::Element&, Optional, StringView property_name, HashMap>& dependencies, Parser::TokenStream& source, Vector& dest) const; bool expand_unresolved_values(DOM::Element&, StringView property_name, Parser::TokenStream& source, Vector& dest) const; void set_all_properties(DOM::Element&, Optional, StyleProperties&, StyleValue const&, DOM::Document&, CSS::CSSStyleDeclaration const*, StyleProperties::PropertyValues const& properties_for_revert) const; template void for_each_stylesheet(CascadeOrigin, Callback) const; CSSPixelRect viewport_rect() const; [[nodiscard]] Length::FontMetrics calculate_root_element_font_metrics(StyleProperties const&) const; CSSPixels parent_or_root_element_line_height(DOM::Element const*, Optional) const; struct MatchingRuleSet { Vector user_agent_rules; Vector author_rules; }; void cascade_declarations(StyleProperties&, DOM::Element&, Optional, Vector const&, CascadeOrigin, Important) const; void build_rule_cache(); void build_rule_cache_if_needed() const; JS::NonnullGCPtr m_document; struct AnimationKeyFrameSet { struct ResolvedKeyFrame { struct UseInitial { }; Array>, to_underlying(last_property_id) + 1> resolved_properties {}; }; RedBlackTree keyframes_by_key; }; struct RuleCache { HashMap> rules_by_id; HashMap> rules_by_class; HashMap> rules_by_tag_name; Vector other_rules; HashMap> rules_by_animation_keyframes; }; NonnullOwnPtr make_rule_cache_for_cascade_origin(CascadeOrigin); RuleCache const& rule_cache_for_cascade_origin(CascadeOrigin) const; void ensure_animation_timer() const; OwnPtr m_author_rule_cache; OwnPtr m_user_agent_rule_cache; mutable FontCache m_font_cache; HashMap> m_loaded_fonts; Length::FontMetrics m_default_font_metrics; Length::FontMetrics m_root_element_font_metrics; constexpr static u64 AnimationKeyFrameKeyScaleFactor = 1000; // 0..100000 enum class AnimationStepTransition { NoTransition, IdleOrBeforeToActive, IdleOrBeforeToAfter, ActiveToBefore, ActiveToActiveChangingTheIteration, ActiveToAfter, AfterToActive, AfterToBefore, Cancelled, }; enum class AnimationState { Before, After, Idle, Active, }; struct AnimationStateSnapshot { Array, to_underlying(last_property_id) + 1> state; }; struct Animation { String name; Optional duration; // "auto" if not set. CSS::Time delay; Optional iteration_count; // Infinite if not set. AnimationTiming timing_function; CSS::AnimationDirection direction; CSS::AnimationFillMode fill_mode; WeakPtr owning_element; CSS::Percentage progress { 0 }; CSS::Time remaining_delay { 0, CSS::Time::Type::Ms }; AnimationState current_state { AnimationState::Before }; size_t current_iteration { 1 }; mutable AnimationStateSnapshot initial_state {}; mutable OwnPtr active_state_if_fill_forward {}; AnimationStepTransition step(CSS::Time const& time_step); ErrorOr collect_into(StyleProperties&, RuleCache const&) const; bool is_done() const; private: float compute_output_progress(float input_progress) const; bool is_animating_backwards() const; }; mutable HashMap> m_active_animations; mutable HashMap> m_finished_animations; // If fill-mode is forward/both, this is non-null and contains the final state. mutable RefPtr m_animation_driver_timer; }; } template<> struct AK::Traits : public AK::GenericTraits { static unsigned hash(Web::CSS::StyleComputer::AnimationKey const& k) { return pair_int_hash(ptr_hash(k.source_declaration), ptr_hash(k.element)); } static bool equals(Web::CSS::StyleComputer::AnimationKey const& a, Web::CSS::StyleComputer::AnimationKey const& b) { return a.element == b.element && a.source_declaration == b.source_declaration; } };