Bug 1635702 [wpt PR 23413] - Move some internal scroll anchoring tests to wpt, a...
[gecko.git] / layout / style / nsFontFaceUtils.cpp
blob5dbadc496f7afab83944c607cfe969635f2ddb8c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsFontFaceUtils.h"
9 #include "gfxUserFontSet.h"
10 #include "mozilla/PresShell.h"
11 #include "mozilla/RestyleManager.h"
12 #include "nsFontMetrics.h"
13 #include "nsIFrame.h"
14 #include "nsLayoutUtils.h"
15 #include "nsPlaceholderFrame.h"
16 #include "nsTArray.h"
17 #include "SVGTextFrame.h"
19 using namespace mozilla;
21 enum class FontUsageKind {
22 // The frame did not use the given font.
23 None = 0,
24 // The frame uses the given font, but doesn't use font-metric-dependent units,
25 // which means that its style doesn't depend on this font.
26 Frame,
27 // The frame uses this font and also has some font-metric-dependent units.
28 // This means that its style depends on this font, and we need to restyle the
29 // element the frame came from.
30 FrameAndFontMetrics,
33 static FontUsageKind StyleFontUsage(ComputedStyle* aComputedStyle,
34 nsPresContext* aPresContext,
35 const gfxUserFontSet* aUserFontSet,
36 const gfxUserFontEntry* aFont,
37 const nsAString& aFamilyName) {
38 MOZ_ASSERT(NS_ConvertUTF8toUTF16(aFont->FamilyName()) == aFamilyName);
40 // first, check if the family name is in the fontlist
41 if (!aComputedStyle->StyleFont()->mFont.fontlist.Contains(aFamilyName)) {
42 return FontUsageKind::None;
45 // family name is in the fontlist, check to see if the font group
46 // associated with the frame includes the specific userfont
47 RefPtr<nsFontMetrics> fm = nsLayoutUtils::GetFontMetricsForComputedStyle(
48 aComputedStyle, aPresContext, 1.0f);
50 if (!fm->GetThebesFontGroup()->ContainsUserFont(aFont)) {
51 return FontUsageKind::None;
54 if (aComputedStyle->DependsOnFontMetrics()) {
55 MOZ_ASSERT(aPresContext->UsesExChUnits());
56 return FontUsageKind::FrameAndFontMetrics;
59 return FontUsageKind::Frame;
62 static FontUsageKind FrameFontUsage(nsIFrame* aFrame,
63 nsPresContext* aPresContext,
64 const gfxUserFontEntry* aFont,
65 const nsAString& aFamilyName) {
66 // check the style of the frame
67 gfxUserFontSet* ufs = aPresContext->GetUserFontSet();
68 FontUsageKind kind =
69 StyleFontUsage(aFrame->Style(), aPresContext, ufs, aFont, aFamilyName);
70 if (kind == FontUsageKind::FrameAndFontMetrics) {
71 return kind;
74 // check additional styles
75 int32_t contextIndex = 0;
76 for (ComputedStyle* extraContext;
77 (extraContext = aFrame->GetAdditionalComputedStyle(contextIndex));
78 ++contextIndex) {
79 kind = std::max(kind, StyleFontUsage(extraContext, aPresContext, ufs, aFont,
80 aFamilyName));
81 if (kind == FontUsageKind::FrameAndFontMetrics) {
82 break;
86 return kind;
89 // TODO(emilio): Can we use the restyle-hint machinery instead of this?
90 static void ScheduleReflow(PresShell* aPresShell, nsIFrame* aFrame) {
91 nsIFrame* f = aFrame;
92 if (f->IsFrameOfType(nsIFrame::eSVG) || nsSVGUtils::IsInSVGTextSubtree(f)) {
93 // SVG frames (and the non-SVG descendants of an SVGTextFrame) need special
94 // reflow handling. We need to search upwards for the first displayed
95 // nsSVGOuterSVGFrame or non-SVG frame, which is the frame we can call
96 // FrameNeedsReflow on. (This logic is based on
97 // nsSVGUtils::ScheduleReflowSVG and
98 // SVGTextFrame::ScheduleReflowSVGNonDisplayText.)
99 if (f->GetStateBits() & NS_FRAME_IS_NONDISPLAY) {
100 while (f) {
101 if (!(f->GetStateBits() & NS_FRAME_IS_NONDISPLAY)) {
102 if (NS_SUBTREE_DIRTY(f)) {
103 // This is a displayed frame, so if it is already dirty, we
104 // will be reflowed soon anyway. No need to call
105 // FrameNeedsReflow again, then.
106 return;
108 if (f->IsSVGOuterSVGFrame() || !(f->IsFrameOfType(nsIFrame::eSVG) ||
109 nsSVGUtils::IsInSVGTextSubtree(f))) {
110 break;
112 f->AddStateBits(NS_FRAME_HAS_DIRTY_CHILDREN);
114 f = f->GetParent();
116 MOZ_ASSERT(f, "should have found an ancestor frame to reflow");
120 aPresShell->FrameNeedsReflow(f, IntrinsicDirty::StyleChange,
121 NS_FRAME_IS_DIRTY);
124 enum class ReflowAlreadyScheduled {
126 Yes,
129 /* static */
130 void nsFontFaceUtils::MarkDirtyForFontChange(nsIFrame* aSubtreeRoot,
131 const gfxUserFontEntry* aFont) {
132 MOZ_ASSERT(aFont);
133 AutoTArray<nsIFrame*, 4> subtrees;
134 subtrees.AppendElement(aSubtreeRoot);
136 nsPresContext* pc = aSubtreeRoot->PresContext();
137 PresShell* presShell = pc->PresShell();
139 // gfxFontFamilyList::Contains expects a UTF-16 string. Convert it once
140 // here rather than on each call.
141 NS_ConvertUTF8toUTF16 familyName(aFont->FamilyName());
143 // check descendants, iterating over subtrees that may include
144 // additional subtrees associated with placeholders
145 do {
146 nsIFrame* subtreeRoot = subtrees.PopLastElement();
148 // Check all descendants to see if they use the font
149 AutoTArray<std::pair<nsIFrame*, ReflowAlreadyScheduled>, 32> stack;
150 stack.AppendElement(
151 std::make_pair(subtreeRoot, ReflowAlreadyScheduled::No));
153 do {
154 auto pair = stack.PopLastElement();
155 nsIFrame* f = pair.first;
156 ReflowAlreadyScheduled alreadyScheduled = pair.second;
158 // if this frame uses the font, mark its descendants dirty
159 // and skip checking its children
160 FontUsageKind kind = FrameFontUsage(f, pc, aFont, familyName);
161 if (kind != FontUsageKind::None) {
162 if (alreadyScheduled == ReflowAlreadyScheduled::No) {
163 ScheduleReflow(presShell, f);
164 alreadyScheduled = ReflowAlreadyScheduled::Yes;
166 if (kind == FontUsageKind::FrameAndFontMetrics) {
167 MOZ_ASSERT(f->GetContent() && f->GetContent()->IsElement(),
168 "How could we target a non-element with selectors?");
169 f->PresContext()->RestyleManager()->PostRestyleEvent(
170 dom::Element::FromNode(f->GetContent()),
171 RestyleHint::RECASCADE_SELF, nsChangeHint(0));
175 if (alreadyScheduled == ReflowAlreadyScheduled::No ||
176 pc->UsesExChUnits()) {
177 if (f->IsPlaceholderFrame()) {
178 nsIFrame* oof = nsPlaceholderFrame::GetRealFrameForPlaceholder(f);
179 if (!nsLayoutUtils::IsProperAncestorFrame(subtreeRoot, oof)) {
180 // We have another distinct subtree we need to mark.
181 subtrees.AppendElement(oof);
185 for (const auto& childList : f->ChildLists()) {
186 for (nsIFrame* kid : childList.mList) {
187 stack.AppendElement(std::make_pair(kid, alreadyScheduled));
191 } while (!stack.IsEmpty());
192 } while (!subtrees.IsEmpty());