StyleComputer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. struct MatchingRule {
  20. JS::GCPtr<CSSStyleRule const> rule;
  21. JS::GCPtr<CSSStyleSheet const> sheet;
  22. size_t style_sheet_index { 0 };
  23. size_t rule_index { 0 };
  24. size_t selector_index { 0 };
  25. u32 specificity { 0 };
  26. bool contains_pseudo_element { false };
  27. };
  28. struct FontFaceKey {
  29. FlyString family_name;
  30. int weight { 0 };
  31. int slope { 0 };
  32. [[nodiscard]] u32 hash() const { return pair_int_hash(family_name.hash(), pair_int_hash(weight, slope)); }
  33. [[nodiscard]] bool operator==(FontFaceKey const&) const = default;
  34. };
  35. class StyleComputer {
  36. public:
  37. explicit StyleComputer(DOM::Document&);
  38. ~StyleComputer();
  39. DOM::Document& document() { return m_document; }
  40. DOM::Document const& document() const { return m_document; }
  41. NonnullRefPtr<StyleProperties> create_document_style() const;
  42. ErrorOr<NonnullRefPtr<StyleProperties>> compute_style(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type> = {}) const;
  43. ErrorOr<RefPtr<StyleProperties>> compute_pseudo_element_style_if_needed(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>) const;
  44. // https://www.w3.org/TR/css-cascade/#origin
  45. enum class CascadeOrigin {
  46. Author,
  47. User,
  48. UserAgent,
  49. Animation,
  50. Transition,
  51. };
  52. Vector<MatchingRule> collect_matching_rules(DOM::Element const&, CascadeOrigin, Optional<CSS::Selector::PseudoElement::Type>) const;
  53. void invalidate_rule_cache();
  54. Gfx::Font const& initial_font() const;
  55. void did_load_font(FlyString const& family_name);
  56. void load_fonts_from_sheet(CSSStyleSheet const&);
  57. 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;
  58. void set_viewport_rect(Badge<DOM::Document>, CSSPixelRect const& viewport_rect) { m_viewport_rect = viewport_rect; }
  59. private:
  60. enum class ComputeStyleMode {
  61. Normal,
  62. CreatePseudoElementStyleIfNeeded,
  63. };
  64. class FontLoader;
  65. struct MatchingFontCandidate;
  66. ErrorOr<RefPtr<StyleProperties>> compute_style_impl(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, ComputeStyleMode) const;
  67. ErrorOr<void> compute_cascaded_values(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, bool& did_match_any_pseudo_element_rules, ComputeStyleMode) const;
  68. static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_ascending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
  69. static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_descending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
  70. RefPtr<Gfx::FontCascadeList const> font_matching_algorithm(FontFaceKey const& key, float font_size_in_pt) const;
  71. void compute_font(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  72. void compute_math_depth(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  73. void compute_defaulted_values(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  74. void absolutize_values(StyleProperties&) const;
  75. void resolve_effective_overflow_values(StyleProperties&) const;
  76. void transform_box_type_if_needed(StyleProperties&, DOM::Element const&, Optional<CSS::Selector::PseudoElement::Type>) const;
  77. void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID, Optional<CSS::Selector::PseudoElement::Type>) const;
  78. 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;
  79. template<typename Callback>
  80. void for_each_stylesheet(CascadeOrigin, Callback) const;
  81. [[nodiscard]] CSSPixelRect viewport_rect() const { return m_viewport_rect; }
  82. [[nodiscard]] Length::FontMetrics calculate_root_element_font_metrics(StyleProperties const&) const;
  83. struct MatchingRuleSet {
  84. Vector<MatchingRule> user_agent_rules;
  85. Vector<MatchingRule> user_rules;
  86. Vector<MatchingRule> author_rules;
  87. };
  88. void cascade_declarations(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, Vector<MatchingRule> const&, CascadeOrigin, Important) const;
  89. void build_rule_cache();
  90. void build_rule_cache_if_needed() const;
  91. JS::NonnullGCPtr<DOM::Document> m_document;
  92. struct RuleCache {
  93. HashMap<FlyString, Vector<MatchingRule>> rules_by_id;
  94. HashMap<FlyString, Vector<MatchingRule>> rules_by_class;
  95. HashMap<FlyString, Vector<MatchingRule>> rules_by_tag_name;
  96. Vector<MatchingRule> other_rules;
  97. HashMap<FlyString, NonnullRefPtr<Animations::KeyframeEffect::KeyFrameSet>> rules_by_animation_keyframes;
  98. };
  99. NonnullOwnPtr<RuleCache> make_rule_cache_for_cascade_origin(CascadeOrigin);
  100. RuleCache const& rule_cache_for_cascade_origin(CascadeOrigin) const;
  101. ErrorOr<void> collect_animation_into(JS::NonnullGCPtr<Animations::KeyframeEffect> animation, StyleProperties& style_properties) const;
  102. OwnPtr<RuleCache> m_author_rule_cache;
  103. OwnPtr<RuleCache> m_user_rule_cache;
  104. OwnPtr<RuleCache> m_user_agent_rule_cache;
  105. JS::Handle<CSSStyleSheet> m_user_style_sheet;
  106. using FontLoaderList = Vector<NonnullOwnPtr<FontLoader>>;
  107. HashMap<FontFaceKey, FontLoaderList> m_loaded_fonts;
  108. Length::FontMetrics m_default_font_metrics;
  109. Length::FontMetrics m_root_element_font_metrics;
  110. CSSPixelRect m_viewport_rect;
  111. };
  112. }