StyleComputer.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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);
  50. explicit StyleComputer(DOM::Document&);
  51. ~StyleComputer();
  52. DOM::Document& document() { return m_document; }
  53. DOM::Document const& document() const { return m_document; }
  54. NonnullRefPtr<StyleProperties> create_document_style() const;
  55. NonnullRefPtr<StyleProperties> compute_style(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type> = {}) const;
  56. RefPtr<StyleProperties> compute_pseudo_element_style_if_needed(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>) const;
  57. Vector<MatchingRule> collect_matching_rules(DOM::Element const&, CascadeOrigin, Optional<CSS::Selector::PseudoElement::Type>) const;
  58. void invalidate_rule_cache();
  59. Gfx::Font const& initial_font() const;
  60. void did_load_font(FlyString const& family_name);
  61. void load_fonts_from_sheet(CSSStyleSheet const&);
  62. 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;
  63. void set_viewport_rect(Badge<DOM::Document>, CSSPixelRect const& viewport_rect) { m_viewport_rect = viewport_rect; }
  64. private:
  65. enum class ComputeStyleMode {
  66. Normal,
  67. CreatePseudoElementStyleIfNeeded,
  68. };
  69. class FontLoader;
  70. struct MatchingFontCandidate;
  71. RefPtr<StyleProperties> compute_style_impl(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, ComputeStyleMode) const;
  72. void compute_cascaded_values(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, bool& did_match_any_pseudo_element_rules, ComputeStyleMode) const;
  73. static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_ascending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
  74. static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_descending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
  75. RefPtr<Gfx::FontCascadeList const> font_matching_algorithm(FontFaceKey const& key, float font_size_in_pt) const;
  76. void compute_font(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  77. void compute_math_depth(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  78. void compute_defaulted_values(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  79. void absolutize_values(StyleProperties&) const;
  80. void resolve_effective_overflow_values(StyleProperties&) const;
  81. void transform_box_type_if_needed(StyleProperties&, DOM::Element const&, Optional<CSS::Selector::PseudoElement::Type>) const;
  82. void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID, Optional<CSS::Selector::PseudoElement::Type>) const;
  83. 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) const;
  84. template<typename Callback>
  85. void for_each_stylesheet(CascadeOrigin, Callback) const;
  86. [[nodiscard]] CSSPixelRect viewport_rect() const { return m_viewport_rect; }
  87. [[nodiscard]] Length::FontMetrics calculate_root_element_font_metrics(StyleProperties const&) const;
  88. struct MatchingRuleSet {
  89. Vector<MatchingRule> user_agent_rules;
  90. Vector<MatchingRule> user_rules;
  91. Vector<MatchingRule> author_rules;
  92. };
  93. void cascade_declarations(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, Vector<MatchingRule> const&, CascadeOrigin, Important) const;
  94. void build_rule_cache();
  95. void build_rule_cache_if_needed() const;
  96. JS::NonnullGCPtr<DOM::Document> m_document;
  97. struct RuleCache {
  98. HashMap<FlyString, Vector<MatchingRule>> rules_by_id;
  99. HashMap<FlyString, Vector<MatchingRule>> rules_by_class;
  100. HashMap<FlyString, Vector<MatchingRule>> rules_by_tag_name;
  101. Vector<MatchingRule> pseudo_element_rules;
  102. Vector<MatchingRule> root_rules;
  103. Vector<MatchingRule> other_rules;
  104. HashMap<FlyString, NonnullRefPtr<Animations::KeyframeEffect::KeyFrameSet>> rules_by_animation_keyframes;
  105. };
  106. NonnullOwnPtr<RuleCache> make_rule_cache_for_cascade_origin(CascadeOrigin);
  107. RuleCache const& rule_cache_for_cascade_origin(CascadeOrigin) const;
  108. void collect_animation_into(JS::NonnullGCPtr<Animations::KeyframeEffect> animation, StyleProperties& style_properties) const;
  109. OwnPtr<RuleCache> m_author_rule_cache;
  110. OwnPtr<RuleCache> m_user_rule_cache;
  111. OwnPtr<RuleCache> m_user_agent_rule_cache;
  112. JS::Handle<CSSStyleSheet> m_user_style_sheet;
  113. using FontLoaderList = Vector<NonnullOwnPtr<FontLoader>>;
  114. HashMap<FontFaceKey, FontLoaderList> m_loaded_fonts;
  115. Length::FontMetrics m_default_font_metrics;
  116. Length::FontMetrics m_root_element_font_metrics;
  117. CSSPixelRect m_viewport_rect;
  118. };
  119. }