StyleComputer.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <AK/Optional.h>
  10. #include <AK/OwnPtr.h>
  11. #include <LibWeb/Animations/KeyframeEffect.h>
  12. #include <LibWeb/CSS/CSSFontFaceRule.h>
  13. #include <LibWeb/CSS/CSSKeyframesRule.h>
  14. #include <LibWeb/CSS/CSSStyleDeclaration.h>
  15. #include <LibWeb/CSS/Selector.h>
  16. #include <LibWeb/CSS/StyleProperties.h>
  17. #include <LibWeb/Forward.h>
  18. namespace Web::CSS {
  19. // https://www.w3.org/TR/css-cascade/#origin
  20. enum class CascadeOrigin {
  21. Author,
  22. User,
  23. UserAgent,
  24. Animation,
  25. Transition,
  26. };
  27. struct MatchingRule {
  28. CascadeOrigin cascade_origin;
  29. JS::GCPtr<DOM::ShadowRoot const> shadow_root;
  30. JS::GCPtr<CSSStyleRule const> rule;
  31. JS::GCPtr<CSSStyleSheet const> sheet;
  32. size_t style_sheet_index { 0 };
  33. size_t rule_index { 0 };
  34. size_t selector_index { 0 };
  35. u32 specificity { 0 };
  36. bool contains_pseudo_element { false };
  37. bool contains_root_pseudo_class { false };
  38. };
  39. struct FontFaceKey {
  40. FlyString family_name;
  41. int weight { 0 };
  42. int slope { 0 };
  43. [[nodiscard]] u32 hash() const { return pair_int_hash(family_name.hash(), pair_int_hash(weight, slope)); }
  44. [[nodiscard]] bool operator==(FontFaceKey const&) const = default;
  45. };
  46. class StyleComputer {
  47. public:
  48. static void for_each_property_expanding_shorthands(PropertyID, StyleValue const&, Function<void(PropertyID, StyleValue const&)> const& set_longhand_property);
  49. static void set_property_expanding_shorthands(StyleProperties&, PropertyID, StyleValue const&, CSS::CSSStyleDeclaration const*, StyleProperties::PropertyValues const& properties_for_revert, StyleProperties::Important = StyleProperties::Important::No);
  50. static NonnullRefPtr<StyleValue const> get_inherit_value(JS::Realm& initial_value_context_realm, CSS::PropertyID, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type> = {});
  51. explicit StyleComputer(DOM::Document&);
  52. ~StyleComputer();
  53. DOM::Document& document() { return m_document; }
  54. DOM::Document const& document() const { return m_document; }
  55. NonnullRefPtr<StyleProperties> create_document_style() const;
  56. NonnullRefPtr<StyleProperties> compute_style(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type> = {}) const;
  57. RefPtr<StyleProperties> compute_pseudo_element_style_if_needed(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>) const;
  58. Vector<MatchingRule> collect_matching_rules(DOM::Element const&, CascadeOrigin, Optional<CSS::Selector::PseudoElement::Type>) const;
  59. void invalidate_rule_cache();
  60. Gfx::Font const& initial_font() const;
  61. void did_load_font(FlyString const& family_name);
  62. void load_fonts_from_sheet(CSSStyleSheet const&);
  63. RefPtr<Gfx::FontCascadeList const> compute_font_for_style_values(DOM::Element const* element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, StyleValue const& font_family, StyleValue const& font_size, StyleValue const& font_style, StyleValue const& font_weight, StyleValue const& font_stretch, int math_depth = 0) const;
  64. void set_viewport_rect(Badge<DOM::Document>, CSSPixelRect const& viewport_rect) { m_viewport_rect = viewport_rect; }
  65. enum class AnimationRefresh {
  66. No,
  67. Yes,
  68. };
  69. void collect_animation_into(JS::NonnullGCPtr<Animations::KeyframeEffect> animation, StyleProperties& style_properties, AnimationRefresh = AnimationRefresh::No) const;
  70. private:
  71. enum class ComputeStyleMode {
  72. Normal,
  73. CreatePseudoElementStyleIfNeeded,
  74. };
  75. class FontLoader;
  76. struct MatchingFontCandidate;
  77. RefPtr<StyleProperties> compute_style_impl(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, ComputeStyleMode) const;
  78. void compute_cascaded_values(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, bool& did_match_any_pseudo_element_rules, ComputeStyleMode) const;
  79. static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_ascending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
  80. static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_descending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
  81. RefPtr<Gfx::FontCascadeList const> font_matching_algorithm(FontFaceKey const& key, float font_size_in_pt) const;
  82. void compute_font(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  83. void compute_math_depth(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  84. void compute_defaulted_values(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  85. void absolutize_values(StyleProperties&) const;
  86. void resolve_effective_overflow_values(StyleProperties&) const;
  87. void transform_box_type_if_needed(StyleProperties&, DOM::Element const&, Optional<CSS::Selector::PseudoElement::Type>) const;
  88. void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID, Optional<CSS::Selector::PseudoElement::Type>) const;
  89. void set_all_properties(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, StyleProperties&, StyleValue const&, DOM::Document&, CSS::CSSStyleDeclaration const*, StyleProperties::PropertyValues const& properties_for_revert, StyleProperties::Important = StyleProperties::Important::No) const;
  90. template<typename Callback>
  91. void for_each_stylesheet(CascadeOrigin, Callback) const;
  92. [[nodiscard]] CSSPixelRect viewport_rect() const { return m_viewport_rect; }
  93. [[nodiscard]] Length::FontMetrics calculate_root_element_font_metrics(StyleProperties const&) const;
  94. struct MatchingRuleSet {
  95. Vector<MatchingRule> user_agent_rules;
  96. Vector<MatchingRule> user_rules;
  97. Vector<MatchingRule> author_rules;
  98. };
  99. void cascade_declarations(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, Vector<MatchingRule> const&, CascadeOrigin, Important) const;
  100. void build_rule_cache();
  101. void build_rule_cache_if_needed() const;
  102. JS::NonnullGCPtr<DOM::Document> m_document;
  103. struct RuleCache {
  104. HashMap<FlyString, Vector<MatchingRule>> rules_by_id;
  105. HashMap<FlyString, Vector<MatchingRule>> rules_by_class;
  106. HashMap<FlyString, Vector<MatchingRule>> rules_by_tag_name;
  107. HashMap<FlyString, Vector<MatchingRule>, AK::ASCIICaseInsensitiveFlyStringTraits> rules_by_attribute_name;
  108. Vector<MatchingRule> pseudo_element_rules;
  109. Vector<MatchingRule> root_rules;
  110. Vector<MatchingRule> other_rules;
  111. HashMap<FlyString, NonnullRefPtr<Animations::KeyframeEffect::KeyFrameSet>> rules_by_animation_keyframes;
  112. };
  113. NonnullOwnPtr<RuleCache> make_rule_cache_for_cascade_origin(CascadeOrigin);
  114. RuleCache const& rule_cache_for_cascade_origin(CascadeOrigin) const;
  115. OwnPtr<RuleCache> m_author_rule_cache;
  116. OwnPtr<RuleCache> m_user_rule_cache;
  117. OwnPtr<RuleCache> m_user_agent_rule_cache;
  118. JS::Handle<CSSStyleSheet> m_user_style_sheet;
  119. using FontLoaderList = Vector<NonnullOwnPtr<FontLoader>>;
  120. HashMap<FontFaceKey, FontLoaderList> m_loaded_fonts;
  121. Length::FontMetrics m_default_font_metrics;
  122. Length::FontMetrics m_root_element_font_metrics;
  123. CSSPixelRect m_viewport_rect;
  124. };
  125. }