Files
palemoon27/layout/generic/nsFontInflationData.cpp
T
roytam1 75dd89485e import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1176414 - Clean up/rename various column index variables in nsTableRowFrame and nsTableFrame. r=dholbert (d8e33d549)
- Bug 1176105 - Remove the (largely gutted) nsTableIterator class, and replace with simple frame-list iteration. r=dholbert (46a15df19)
- Bug 1176555 - Replace explicit bit-twiddling of frame state flags by human-readable nsIFrame state-manipulation methods in table layout code. r=dholbert (aef9fb035)
- Bug 1176354 - Rename nsTableRowFrame::GetBSize to differentiate better from nsIFrame::BSize. r=dholbert (9b67203a0)
- Bug 1177600 - Properly adjust table row and cell positions when their containing block-size changes in vertical-rl writing mode. r=dholbert (413148d55)
- Bug 1177925: Use range-based for loops for nsFrameList iteration, in various places in layout. r=tn (6db450285)
- Bug 1148582 - Recycle mask layers attached to FrameMetrics. r=mattwoodrow (91360fbd8)
- Bug 1148582 - Apply async transforms to (ancestor) mask layers correctly. r=botond (d6fca66fb)
- Bug 1143575. Add some #includes to avoid unified-build issues on Windows. r=nical (ff78b76a3)
- Bug 1143575. Add some #includes to avoid more unified-build issues on Windows. r=nical (99e0548f4)
- Bug 1181832 - Keep gfxPrefs.h out of header files. r=kats (341f0aea0)
- Bug 1181832 - Include gfxPrefs.h from nsImageLoadingContet.cpp to fix bustage on a CLOSED TREE. r=bustage (d61a18ab9)
- Bug 1137557 - Part 0: TextEventDispatcher shouldn't forward keyboard events coming from TextInputProcessor to the parent process. r=smaug (87f70fcc4)
- Bug 1172405 - Fix build error when adding files to dom/workers and gfx/layers. r=nical (8e114b8bb)
- Bug 1061393 - Export display list info to layer scope viewer. r=kamidphish (60fd247f0)
- Bug 1072313 - Check for more leaks in the layers code. r=mstange (aacbb2e1c)
- Bug 1198979 - GC unused IPDL struct member. r=jmuizelaar (79c3113fb)
- add missing semicolon (17ba58b45)
- Bug 1153348 - Add an analysis to prohibit operator bools which aren't marked as either explicit or MOZ_IMPLICIT; r=jrmuizel (903dca970)
- Bug 1170388 - Restrict the static analysis error given about raw pointers to refcounted objects inside a lambda, to the case where the raw pointer is captured. r=ehsan (cbbd3174c)
- Bug 1180993 - Part 1: Add an analysis to help catch unused return values of specific types. r=ehsan (2420face4)
- Bug 1180993 - Part 2: Add the MOZ_MUST_USE attribute to mfbt, and use it to verify usage of already_AddRefed. r=ehsan (b72785a60)
- Bug 1123907 - Part 1: Add an analysis to ensure that a class marked MOZ_NEEDS_NO_VTABLE_TYPE cannot be instantiated by a class with a VTable; r=ehsan (0f5730c04)
- Bug 1123907 - Part 2: Add MOZ_NEEDS_NO_VTABLE_TYPE to Attributes.h, and use it to verify the EntryType argument of nsTHashtable; r=ehsan (2e80fc581)
- Bug 1159433 - Part 1: Add an analysis to ensure that some template arguments cannot be non-memmovable types; r=ehsan (3602e950f)
- uniform MOZ_MUST_USE, it will be renamed in patch 1267550 (3b5ebd4c5)
- add missing part of Bug 1187073 - Use MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS to validate the usage of AlignedStorage2. r=ehsan (3b451bfb7)
- Bug 1159433 - Part 2: Add the MOZ_NON_MEMMOVABLE and MOZ_NEEDS_MEMMOVABLE_TYPE macros to MFBT; r=froydnj (171c23ec4)
- Bug 1159433 - Part 3: Make nsTArray_CopyChooser only accept memmovable argument types by default; r=froydnj (40cb4aa67)
- Bug 1159433 - Part 4: Mark nsTAutoString_CharT and nsAutoArrayBase as non-memmovable; r=froydnj (7dc6bd220)
2020-12-24 10:05:34 +08:00

383 lines
13 KiB
C++

/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Per-block-formatting-context manager of font size inflation for pan and zoom UI. */
#include "nsFontInflationData.h"
#include "FramePropertyTable.h"
#include "nsTextControlFrame.h"
#include "nsListControlFrame.h"
#include "nsComboboxControlFrame.h"
#include "nsHTMLReflowState.h"
#include "nsTextFrameUtils.h"
using namespace mozilla;
using namespace mozilla::layout;
NS_DECLARE_FRAME_PROPERTY(FontInflationDataProperty,
DeleteValue<nsFontInflationData>)
/* static */ nsFontInflationData*
nsFontInflationData::FindFontInflationDataFor(const nsIFrame *aFrame)
{
// We have one set of font inflation data per block formatting context.
const nsIFrame *bfc = FlowRootFor(aFrame);
NS_ASSERTION(bfc->GetStateBits() & NS_FRAME_FONT_INFLATION_FLOW_ROOT,
"should have found a flow root");
return static_cast<nsFontInflationData*>(
bfc->Properties().Get(FontInflationDataProperty()));
}
/* static */ bool
nsFontInflationData::UpdateFontInflationDataISizeFor(const nsHTMLReflowState& aReflowState)
{
nsIFrame *bfc = aReflowState.frame;
NS_ASSERTION(bfc->GetStateBits() & NS_FRAME_FONT_INFLATION_FLOW_ROOT,
"should have been given a flow root");
FrameProperties bfcProps(bfc->Properties());
nsFontInflationData *data = static_cast<nsFontInflationData*>(
bfcProps.Get(FontInflationDataProperty()));
bool oldInflationEnabled;
nscoord oldNCAISize;
if (data) {
oldNCAISize = data->mNCAISize;
oldInflationEnabled = data->mInflationEnabled;
} else {
data = new nsFontInflationData(bfc);
bfcProps.Set(FontInflationDataProperty(), data);
oldNCAISize = -1;
oldInflationEnabled = true; /* not relevant */
}
data->UpdateISize(aReflowState);
if (oldInflationEnabled != data->mInflationEnabled)
return true;
return oldInflationEnabled &&
oldNCAISize != data->mNCAISize;
}
/* static */ void
nsFontInflationData::MarkFontInflationDataTextDirty(nsIFrame *aBFCFrame)
{
NS_ASSERTION(aBFCFrame->GetStateBits() & NS_FRAME_FONT_INFLATION_FLOW_ROOT,
"should have been given a flow root");
FrameProperties bfcProps(aBFCFrame->Properties());
nsFontInflationData *data = static_cast<nsFontInflationData*>(
bfcProps.Get(FontInflationDataProperty()));
if (data) {
data->MarkTextDirty();
}
}
nsFontInflationData::nsFontInflationData(nsIFrame *aBFCFrame)
: mBFCFrame(aBFCFrame)
, mNCAISize(0)
, mTextAmount(0)
, mTextThreshold(0)
, mInflationEnabled(false)
, mTextDirty(true)
{
}
/**
* Find the closest common ancestor between aFrame1 and aFrame2, except
* treating the parent of a frame as the first-in-flow of its parent (so
* the result doesn't change when breaking changes).
*
* aKnownCommonAncestor is a known common ancestor of both.
*/
static nsIFrame*
NearestCommonAncestorFirstInFlow(nsIFrame *aFrame1, nsIFrame *aFrame2,
nsIFrame *aKnownCommonAncestor)
{
aFrame1 = aFrame1->FirstInFlow();
aFrame2 = aFrame2->FirstInFlow();
aKnownCommonAncestor = aKnownCommonAncestor->FirstInFlow();
nsAutoTArray<nsIFrame*, 32> ancestors1, ancestors2;
for (nsIFrame *f = aFrame1; f != aKnownCommonAncestor;
(f = f->GetParent()) && (f = f->FirstInFlow())) {
ancestors1.AppendElement(f);
}
for (nsIFrame *f = aFrame2; f != aKnownCommonAncestor;
(f = f->GetParent()) && (f = f->FirstInFlow())) {
ancestors2.AppendElement(f);
}
nsIFrame *result = aKnownCommonAncestor;
uint32_t i1 = ancestors1.Length(),
i2 = ancestors2.Length();
while (i1-- != 0 && i2-- != 0) {
if (ancestors1[i1] != ancestors2[i2]) {
break;
}
result = ancestors1[i1];
}
return result;
}
static nscoord
ComputeDescendantISize(const nsHTMLReflowState& aAncestorReflowState,
nsIFrame *aDescendantFrame)
{
nsIFrame *ancestorFrame = aAncestorReflowState.frame->FirstInFlow();
if (aDescendantFrame == ancestorFrame) {
return aAncestorReflowState.ComputedISize();
}
AutoInfallibleTArray<nsIFrame*, 16> frames;
for (nsIFrame *f = aDescendantFrame; f != ancestorFrame;
f = f->GetParent()->FirstInFlow()) {
frames.AppendElement(f);
}
// This ignores the inline-size contributions made by scrollbars, though in
// reality we don't have any scrollbars on the sorts of devices on
// which we use font inflation, so it's not a problem. But it may
// occasionally cause problems when writing tests on desktop.
uint32_t len = frames.Length();
nsHTMLReflowState *reflowStates = static_cast<nsHTMLReflowState*>
(moz_xmalloc(sizeof(nsHTMLReflowState) * len));
nsPresContext *presContext = aDescendantFrame->PresContext();
for (uint32_t i = 0; i < len; ++i) {
const nsHTMLReflowState &parentReflowState =
(i == 0) ? aAncestorReflowState : reflowStates[i - 1];
nsIFrame *frame = frames[len - i - 1];
WritingMode wm = frame->GetWritingMode();
LogicalSize availSize = parentReflowState.ComputedSize(wm);
availSize.BSize(wm) = NS_UNCONSTRAINEDSIZE;
MOZ_ASSERT(frame->GetParent()->FirstInFlow() ==
parentReflowState.frame->FirstInFlow(),
"bad logic in this function");
new (reflowStates + i) nsHTMLReflowState(presContext, parentReflowState,
frame, availSize);
}
MOZ_ASSERT(reflowStates[len - 1].frame == aDescendantFrame,
"bad logic in this function");
nscoord result = reflowStates[len - 1].ComputedISize();
for (uint32_t i = len; i-- != 0; ) {
reflowStates[i].~nsHTMLReflowState();
}
moz_free(reflowStates);
return result;
}
void
nsFontInflationData::UpdateISize(const nsHTMLReflowState &aReflowState)
{
nsIFrame *bfc = aReflowState.frame;
NS_ASSERTION(bfc->GetStateBits() & NS_FRAME_FONT_INFLATION_FLOW_ROOT,
"must be block formatting context");
nsIFrame *firstInflatableDescendant =
FindEdgeInflatableFrameIn(bfc, eFromStart);
if (!firstInflatableDescendant) {
mTextAmount = 0;
mTextThreshold = 0; // doesn't matter
mTextDirty = false;
mInflationEnabled = false;
return;
}
nsIFrame *lastInflatableDescendant =
FindEdgeInflatableFrameIn(bfc, eFromEnd);
MOZ_ASSERT(!firstInflatableDescendant == !lastInflatableDescendant,
"null-ness should match; NearestCommonAncestorFirstInFlow"
" will crash when passed null");
// Particularly when we're computing for the root BFC, the inline-size of
// nca might differ significantly for the inline-size of bfc.
nsIFrame *nca = NearestCommonAncestorFirstInFlow(firstInflatableDescendant,
lastInflatableDescendant,
bfc);
while (!nca->IsContainerForFontSizeInflation()) {
nca = nca->GetParent()->FirstInFlow();
}
nscoord newNCAISize = ComputeDescendantISize(aReflowState, nca);
// See comment above "font.size.inflation.lineThreshold" in
// modules/libpref/src/init/all.js .
nsIPresShell* presShell = bfc->PresContext()->PresShell();
uint32_t lineThreshold = presShell->FontSizeInflationLineThreshold();
nscoord newTextThreshold = (newNCAISize * lineThreshold) / 100;
if (mTextThreshold <= mTextAmount && mTextAmount < newTextThreshold) {
// Because we truncate our scan when we hit sufficient text, we now
// need to rescan.
mTextDirty = true;
}
mNCAISize = newNCAISize;
mTextThreshold = newTextThreshold;
mInflationEnabled = mTextAmount >= mTextThreshold;
}
/* static */ nsIFrame*
nsFontInflationData::FindEdgeInflatableFrameIn(nsIFrame* aFrame,
SearchDirection aDirection)
{
// NOTE: This function has a similar structure to ScanTextIn!
// FIXME: Should probably only scan the text that's actually going to
// be inflated!
nsIFormControlFrame* fcf = do_QueryFrame(aFrame);
if (fcf) {
return aFrame;
}
// FIXME: aDirection!
nsAutoTArray<FrameChildList, 4> lists;
aFrame->GetChildLists(&lists);
for (uint32_t i = 0, len = lists.Length(); i < len; ++i) {
const nsFrameList& list =
lists[(aDirection == eFromStart) ? i : len - i - 1].mList;
for (nsIFrame *kid = (aDirection == eFromStart) ? list.FirstChild()
: list.LastChild();
kid;
kid = (aDirection == eFromStart) ? kid->GetNextSibling()
: kid->GetPrevSibling()) {
if (kid->GetStateBits() & NS_FRAME_FONT_INFLATION_FLOW_ROOT) {
// Goes in a different set of inflation data.
continue;
}
if (kid->GetType() == nsGkAtoms::textFrame) {
nsIContent *content = kid->GetContent();
if (content && kid == content->GetPrimaryFrame()) {
uint32_t len = nsTextFrameUtils::
ComputeApproximateLengthWithWhitespaceCompression(
content, kid->StyleText());
if (len != 0) {
return kid;
}
}
} else {
nsIFrame *kidResult =
FindEdgeInflatableFrameIn(kid, aDirection);
if (kidResult) {
return kidResult;
}
}
}
}
return nullptr;
}
void
nsFontInflationData::ScanText()
{
mTextDirty = false;
mTextAmount = 0;
ScanTextIn(mBFCFrame);
mInflationEnabled = mTextAmount >= mTextThreshold;
}
static uint32_t
DoCharCountOfLargestOption(nsIFrame *aContainer)
{
uint32_t result = 0;
for (nsIFrame* option : aContainer->PrincipalChildList()) {
uint32_t optionResult;
if (option->GetContent()->IsHTMLElement(nsGkAtoms::optgroup)) {
optionResult = DoCharCountOfLargestOption(option);
} else {
// REVIEW: Check the frame structure for this!
optionResult = 0;
for (nsIFrame* optionChild : option->PrincipalChildList()) {
if (optionChild->GetType() == nsGkAtoms::textFrame) {
optionResult += nsTextFrameUtils::
ComputeApproximateLengthWithWhitespaceCompression(
optionChild->GetContent(), optionChild->StyleText());
}
}
}
if (optionResult > result) {
result = optionResult;
}
}
return result;
}
static uint32_t
CharCountOfLargestOption(nsIFrame *aListControlFrame)
{
return DoCharCountOfLargestOption(
static_cast<nsListControlFrame*>(aListControlFrame)->GetOptionsContainer());
}
void
nsFontInflationData::ScanTextIn(nsIFrame *aFrame)
{
// NOTE: This function has a similar structure to FindEdgeInflatableFrameIn!
// FIXME: Should probably only scan the text that's actually going to
// be inflated!
nsIFrame::ChildListIterator lists(aFrame);
for (; !lists.IsDone(); lists.Next()) {
nsFrameList::Enumerator kids(lists.CurrentList());
for (; !kids.AtEnd(); kids.Next()) {
nsIFrame *kid = kids.get();
if (kid->GetStateBits() & NS_FRAME_FONT_INFLATION_FLOW_ROOT) {
// Goes in a different set of inflation data.
continue;
}
nsIAtom *fType = kid->GetType();
if (fType == nsGkAtoms::textFrame) {
nsIContent *content = kid->GetContent();
if (content && kid == content->GetPrimaryFrame()) {
uint32_t len = nsTextFrameUtils::
ComputeApproximateLengthWithWhitespaceCompression(
content, kid->StyleText());
if (len != 0) {
nscoord fontSize = kid->StyleFont()->mFont.size;
if (fontSize > 0) {
mTextAmount += fontSize * len;
}
}
}
} else if (fType == nsGkAtoms::textInputFrame) {
// We don't want changes to the amount of text in a text input
// to change what we count towards inflation.
nscoord fontSize = kid->StyleFont()->mFont.size;
int32_t charCount = static_cast<nsTextControlFrame*>(kid)->GetCols();
mTextAmount += charCount * fontSize;
} else if (fType == nsGkAtoms::comboboxControlFrame) {
// See textInputFrame above (with s/amount of text/selected option/).
// Don't just recurse down to the list control inside, since we
// need to exclude the display frame.
nscoord fontSize = kid->StyleFont()->mFont.size;
int32_t charCount = CharCountOfLargestOption(
static_cast<nsComboboxControlFrame*>(kid)->GetDropDown());
mTextAmount += charCount * fontSize;
} else if (fType == nsGkAtoms::listControlFrame) {
// See textInputFrame above (with s/amount of text/selected option/).
nscoord fontSize = kid->StyleFont()->mFont.size;
int32_t charCount = CharCountOfLargestOption(kid);
mTextAmount += charCount * fontSize;
} else {
// recursive step
ScanTextIn(kid);
}
if (mTextAmount >= mTextThreshold) {
return;
}
}
}
}