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/. */
6
7 #include "nsFontFaceUtils.h"
8
9 #include "gfxTextRun.h"
10 #include "gfxUserFontSet.h"
11 #include "mozilla/PresShell.h"
12 #include "mozilla/RestyleManager.h"
13 #include "mozilla/SVGUtils.h"
14 #include "nsFontMetrics.h"
15 #include "nsIFrame.h"
16 #include "nsLayoutUtils.h"
17 #include "nsPlaceholderFrame.h"
18 #include "nsTArray.h"
19
20 using namespace mozilla;
21
22 enum class FontUsageKind {
23 // The frame did not use the given font.
24 None = 0,
25 // The frame uses the given font, but doesn't use font-metric-dependent units,
26 // which means that its style doesn't depend on this font.
27 Frame = 1 << 0,
28 // The frame uses has some font-metric-dependent units on this font.
29 // This means that its style depends on this font, and we need to restyle the
30 // element the frame came from.
31 FontMetrics = 1 << 1,
32
33 Max = Frame | FontMetrics,
34 };
35
36 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(FontUsageKind);
37
IsFontReferenced(const ComputedStyle & aStyle,const nsAString & aFamilyName)38 static bool IsFontReferenced(const ComputedStyle& aStyle,
39 const nsAString& aFamilyName) {
40 for (const auto& family :
41 aStyle.StyleFont()->mFont.family.families.list.AsSpan()) {
42 if (family.IsNamedFamily(aFamilyName)) {
43 return true;
44 }
45 }
46 return false;
47 }
48
StyleFontUsage(nsIFrame * aFrame,ComputedStyle * aStyle,nsPresContext * aPresContext,const gfxUserFontEntry * aFont,const nsAString & aFamilyName,bool aIsExtraStyle)49 static FontUsageKind StyleFontUsage(nsIFrame* aFrame, ComputedStyle* aStyle,
50 nsPresContext* aPresContext,
51 const gfxUserFontEntry* aFont,
52 const nsAString& aFamilyName,
53 bool aIsExtraStyle) {
54 MOZ_ASSERT(NS_ConvertUTF8toUTF16(aFont->FamilyName()) == aFamilyName);
55
56 auto FontIsUsed = [&aFont, &aPresContext,
57 &aFamilyName](ComputedStyle* aStyle) {
58 if (!IsFontReferenced(*aStyle, aFamilyName)) {
59 return false;
60 }
61
62 // family name is in the fontlist, check to see if the font group
63 // associated with the frame includes the specific userfont.
64 //
65 // TODO(emilio): Is this check really useful? I guess it's useful for
66 // different font faces of the same font?
67 RefPtr<nsFontMetrics> fm = nsLayoutUtils::GetFontMetricsForComputedStyle(
68 aStyle, aPresContext, 1.0f);
69 return fm->GetThebesFontGroup()->ContainsUserFont(aFont);
70 };
71
72 auto usage = FontUsageKind::None;
73
74 if (FontIsUsed(aStyle)) {
75 usage |= FontUsageKind::Frame;
76 if (aStyle->DependsOnSelfFontMetrics()) {
77 usage |= FontUsageKind::FontMetrics;
78 }
79 }
80
81 if (aStyle->DependsOnInheritedFontMetrics() &&
82 !(usage & FontUsageKind::FontMetrics)) {
83 ComputedStyle* parentStyle = nullptr;
84 if (aIsExtraStyle) {
85 parentStyle = aFrame->Style();
86 } else {
87 nsIFrame* provider = nullptr;
88 parentStyle = aFrame->GetParentComputedStyle(&provider);
89 }
90
91 if (parentStyle && FontIsUsed(parentStyle)) {
92 usage |= FontUsageKind::FontMetrics;
93 }
94 }
95
96 return usage;
97 }
98
FrameFontUsage(nsIFrame * aFrame,nsPresContext * aPresContext,const gfxUserFontEntry * aFont,const nsAString & aFamilyName)99 static FontUsageKind FrameFontUsage(nsIFrame* aFrame,
100 nsPresContext* aPresContext,
101 const gfxUserFontEntry* aFont,
102 const nsAString& aFamilyName) {
103 // check the style of the frame
104 FontUsageKind kind = StyleFontUsage(aFrame, aFrame->Style(), aPresContext,
105 aFont, aFamilyName, /* extra = */ false);
106 if (kind == FontUsageKind::Max) {
107 return kind;
108 }
109
110 // check additional styles
111 int32_t contextIndex = 0;
112 for (ComputedStyle* extraContext;
113 (extraContext = aFrame->GetAdditionalComputedStyle(contextIndex));
114 ++contextIndex) {
115 kind |= StyleFontUsage(aFrame, extraContext, aPresContext, aFont,
116 aFamilyName, /* extra = */ true);
117 if (kind == FontUsageKind::Max) {
118 break;
119 }
120 }
121
122 return kind;
123 }
124
125 // TODO(emilio): Can we use the restyle-hint machinery instead of this?
ScheduleReflow(PresShell * aPresShell,nsIFrame * aFrame)126 static void ScheduleReflow(PresShell* aPresShell, nsIFrame* aFrame) {
127 nsIFrame* f = aFrame;
128 if (f->IsFrameOfType(nsIFrame::eSVG) || SVGUtils::IsInSVGTextSubtree(f)) {
129 // SVG frames (and the non-SVG descendants of an SVGTextFrame) need special
130 // reflow handling. We need to search upwards for the first displayed
131 // SVGOuterSVGFrame or non-SVG frame, which is the frame we can call
132 // FrameNeedsReflow on. (This logic is based on
133 // SVGUtils::ScheduleReflowSVG and
134 // SVGTextFrame::ScheduleReflowSVGNonDisplayText.)
135 if (f->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
136 while (f) {
137 if (!f->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
138 if (f->IsSubtreeDirty()) {
139 // This is a displayed frame, so if it is already dirty, we
140 // will be reflowed soon anyway. No need to call
141 // FrameNeedsReflow again, then.
142 return;
143 }
144 if (f->IsSVGOuterSVGFrame() || !(f->IsFrameOfType(nsIFrame::eSVG) ||
145 SVGUtils::IsInSVGTextSubtree(f))) {
146 break;
147 }
148 f->AddStateBits(NS_FRAME_HAS_DIRTY_CHILDREN);
149 }
150 f = f->GetParent();
151 }
152 MOZ_ASSERT(f, "should have found an ancestor frame to reflow");
153 }
154 }
155
156 aPresShell->FrameNeedsReflow(f, IntrinsicDirty::StyleChange,
157 NS_FRAME_IS_DIRTY);
158 }
159
160 enum class ReflowAlreadyScheduled {
161 No,
162 Yes,
163 };
164
165 /* static */
MarkDirtyForFontChange(nsIFrame * aSubtreeRoot,const gfxUserFontEntry * aFont)166 void nsFontFaceUtils::MarkDirtyForFontChange(nsIFrame* aSubtreeRoot,
167 const gfxUserFontEntry* aFont) {
168 MOZ_ASSERT(aFont);
169 AutoTArray<nsIFrame*, 4> subtrees;
170 subtrees.AppendElement(aSubtreeRoot);
171
172 nsPresContext* pc = aSubtreeRoot->PresContext();
173 PresShell* presShell = pc->PresShell();
174
175 // StyleSingleFontFamily::IsNamedFamily expects a UTF-16 string. Convert it
176 // once here rather than on each call.
177 NS_ConvertUTF8toUTF16 familyName(aFont->FamilyName());
178
179 // check descendants, iterating over subtrees that may include
180 // additional subtrees associated with placeholders
181 do {
182 nsIFrame* subtreeRoot = subtrees.PopLastElement();
183
184 // Check all descendants to see if they use the font
185 AutoTArray<std::pair<nsIFrame*, ReflowAlreadyScheduled>, 32> stack;
186 stack.AppendElement(
187 std::make_pair(subtreeRoot, ReflowAlreadyScheduled::No));
188
189 do {
190 auto pair = stack.PopLastElement();
191 nsIFrame* f = pair.first;
192 ReflowAlreadyScheduled alreadyScheduled = pair.second;
193
194 // if this frame uses the font, mark its descendants dirty
195 // and skip checking its children
196 FontUsageKind kind = FrameFontUsage(f, pc, aFont, familyName);
197 if (kind != FontUsageKind::None) {
198 if ((kind & FontUsageKind::Frame) &&
199 alreadyScheduled == ReflowAlreadyScheduled::No) {
200 ScheduleReflow(presShell, f);
201 alreadyScheduled = ReflowAlreadyScheduled::Yes;
202 }
203 if (kind & FontUsageKind::FontMetrics) {
204 MOZ_ASSERT(f->GetContent() && f->GetContent()->IsElement(),
205 "How could we target a non-element with selectors?");
206 f->PresContext()->RestyleManager()->PostRestyleEvent(
207 dom::Element::FromNode(f->GetContent()),
208 RestyleHint::RECASCADE_SELF, nsChangeHint(0));
209 }
210 }
211
212 if (alreadyScheduled == ReflowAlreadyScheduled::No ||
213 pc->UsesExChUnits()) {
214 if (f->IsPlaceholderFrame()) {
215 nsIFrame* oof = nsPlaceholderFrame::GetRealFrameForPlaceholder(f);
216 if (!nsLayoutUtils::IsProperAncestorFrame(subtreeRoot, oof)) {
217 // We have another distinct subtree we need to mark.
218 subtrees.AppendElement(oof);
219 }
220 }
221
222 for (const auto& childList : f->ChildLists()) {
223 for (nsIFrame* kid : childList.mList) {
224 stack.AppendElement(std::make_pair(kid, alreadyScheduled));
225 }
226 }
227 }
228 } while (!stack.IsEmpty());
229 } while (!subtrees.IsEmpty());
230 }
231