Files
palemoon27/layout/mathml/nsMathMLmrootFrame.cpp
T
roytam1 212144c57f import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1153936 - nsIHttpChannelInteral attribute to opt out of alt-svc on per channel basis r=hurley (0e5667321)
- Bug 1137287 - Part 2: Send non-200/404 synthesized responses via the parent HTTP implementation for proper processing. r=mayhemer (3c38a1908)
- Bug 1137287 - Part 0: Test for synthesized redirects. r=jdm (6f12f95de)
- Bug 1137287 - Build fix. rs=KWierso for a CLOSED TREE (85694256a)
- Bug 1095098 - move do_QueryObject templates into their own header; r=froydnj (5d349c248)
- Bug 1095098 - followup - add back some static analysis attributes lost in a rebase; r=me (917a6b5da)
- Bug 1159303 - Reduce noise due to sort operations warnings. r=bent (cac3c3e12)
- Bug 1164559 - Part 1: Remove instances of #ifdef PR_LOGGING in storage. r=froydnj (5dd0f92b0)
- Bug 1164559 - Part 2: Wrap expensive calls in PR_LOG_TEST. r=froydnj (54e94d1ce)
- Bug 1148833 part 1 - Remove nsMathMLContainerFrame::WillReflow, reset the NS_MATHML_ERROR bit at the start of Reflow instead. r=roc (803abb8d9)
- Bug 1148833 part 2 - Makes sure gLogModule is initialized by calling GetLogModuleInfo(). r=roc (722e2019d)
- Bug 1148833 part 3 - Remove nsIFrame::WillReflow and add a non-virtual MarkInReflow method instead that sets NS_FRAME_IN_REFLOW. Call it at the start of Reflow(). r=roc (9cceb221d)
- Bug 1148833 part 4 - Fix indentation of some Reflow params (white-space changes only). (b2ba3e18a)
- Bug 1158546 - Remove nsDisplayHeaderFooter::mFrame in favor of nsDisplayItem::mFrame; r=roc (f6a20967f)
- Bug 1162673 - Part 1: Remove instances of #ifdef PR_LOGGING in layout. r=froydnj (453a452fc)
- Bug 1162673 - Part 2: Wrap expensive calls in PR_LOG_TEST. r=froydnj (d745eab19)
- Bug 1144031 - fix use of uninitialized variable, r=mcmanus (327dc1af9)
- Bug 1144270 : Update remaining callers of newChannel to newChannel2 in netwerk/ (r=mcmanus) (60c21d7cb)
- Bug 1161558 cleanup some nsIObserver shutdown paths r=bagder (eb8441bb9)
- Bug 1162336 - Part 1: Remove instances of #ifdef PR_LOGGING in netwerk. r=froydnj (8cadc40a2)
- Bug 1162336 - Part 2: Wrap expensive calls in PR_LOG_TEST. r=froydnj (4e26e4886)
- Bug 1163194 - Part 1: Remove instances of #ifdef PR_LOGGING in dom/xul. r=froydnj (4f840564b)
- Bug 1163194 - Part 2: Wrap expensive calls in PR_LOG_TEST. r=froydnj (653707f34)
- Bug 1153737: Avoid unnecessary uses of mozilla::pkix::ScopedPtr, r=keeler (fd9eb9aa2)
- Bug 1038072 - signature verification for JAR files unpacked into a directory. r=keeler (32469e1dd)
- Bug 1124076 - Properly detect certs when loaded and prompt to import them. r=sworkman/dkeeler (d860e3cac)
- Bug 1124076 followup - fix the build when PR_LOGGING is not defined. r=mrbkap (657b18bf2)
- Bug 1162691 - Part 1: Remove instances of #ifdef PR_LOGGING in security. r=froydnj (98a916e82)
- Bug 1162691 - Part 2: Wrap expensive calls in PR_LOG_TEST. r=froydnj (000c2fe42)
- bug 1147085 - remove nsINSSCertCache (replace it with nsIX509CertDB.getCerts()) r=Cykesiopka (88f7eba23)
- Bug 1149888 - Make PLDHashTable::mRecursionLevel atomic, r=froydnj. Pushing on CLOSED TREE with a=ryanvm. (25d8e2da1)
- Bug 1050035 (part 1, attempt 2) - Lazily allocate PLDHashTable::mEntryStore. r=froydnj. (195615f16)
- Bug 1159972 - Remove the fallible version of PL_DHashTableInit(). r=froydnj. (d31806eeb)
2020-05-30 12:49:06 +08:00

421 lines
15 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "nsMathMLmrootFrame.h"
#include "nsPresContext.h"
#include "nsRenderingContext.h"
#include <algorithm>
using namespace mozilla;
//
// <mroot> -- form a radical - implementation
//
// additional style context to be used by our MathMLChar.
#define NS_SQR_CHAR_STYLE_CONTEXT_INDEX 0
static const char16_t kSqrChar = char16_t(0x221A);
nsIFrame*
NS_NewMathMLmrootFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
{
return new (aPresShell) nsMathMLmrootFrame(aContext);
}
NS_IMPL_FRAMEARENA_HELPERS(nsMathMLmrootFrame)
nsMathMLmrootFrame::nsMathMLmrootFrame(nsStyleContext* aContext) :
nsMathMLContainerFrame(aContext),
mSqrChar(),
mBarRect()
{
}
nsMathMLmrootFrame::~nsMathMLmrootFrame()
{
}
void
nsMathMLmrootFrame::Init(nsIContent* aContent,
nsContainerFrame* aParent,
nsIFrame* aPrevInFlow)
{
nsMathMLContainerFrame::Init(aContent, aParent, aPrevInFlow);
nsPresContext *presContext = PresContext();
// No need to track the style context given to our MathML char.
// The Style System will use Get/SetAdditionalStyleContext() to keep it
// up-to-date if dynamic changes arise.
nsAutoString sqrChar; sqrChar.Assign(kSqrChar);
mSqrChar.SetData(presContext, sqrChar);
ResolveMathMLCharStyle(presContext, mContent, mStyleContext, &mSqrChar);
}
NS_IMETHODIMP
nsMathMLmrootFrame::TransmitAutomaticData()
{
// 1. The REC says:
// The <mroot> element increments scriptlevel by 2, and sets displaystyle to
// "false", within index, but leaves both attributes unchanged within base.
// 2. The TeXbook (Ch 17. p.141) says \sqrt is compressed
UpdatePresentationDataFromChildAt(1, 1,
NS_MATHML_COMPRESSED,
NS_MATHML_COMPRESSED);
UpdatePresentationDataFromChildAt(0, 0,
NS_MATHML_COMPRESSED, NS_MATHML_COMPRESSED);
PropagateFrameFlagFor(mFrames.LastChild(),
NS_FRAME_MATHML_SCRIPT_DESCENDANT);
return NS_OK;
}
void
nsMathMLmrootFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsRect& aDirtyRect,
const nsDisplayListSet& aLists)
{
/////////////
// paint the content we are square-rooting
nsMathMLContainerFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
/////////////
// paint the sqrt symbol
if (!NS_MATHML_HAS_ERROR(mPresentationData.flags)) {
mSqrChar.Display(aBuilder, this, aLists, 0);
DisplayBar(aBuilder, this, mBarRect, aLists);
#if defined(DEBUG) && defined(SHOW_BOUNDING_BOX)
// for visual debug
nsRect rect;
mSqrChar.GetRect(rect);
nsBoundingMetrics bm;
mSqrChar.GetBoundingMetrics(bm);
DisplayBoundingMetrics(aBuilder, this, rect.TopLeft(), bm, aLists);
#endif
}
}
void
nsMathMLmrootFrame::GetRadicalXOffsets(nscoord aIndexWidth, nscoord aSqrWidth,
nsFontMetrics* aFontMetrics,
nscoord* aIndexOffset,
nscoord* aSqrOffset)
{
// The index is tucked in closer to the radical while making sure
// that the kern does not make the index and radical collide
nscoord dxIndex, dxSqr;
nscoord xHeight = aFontMetrics->XHeight();
nscoord indexRadicalKern = NSToCoordRound(1.35f * xHeight);
nscoord oneDevPixel = aFontMetrics->AppUnitsPerDevPixel();
gfxFont* mathFont = aFontMetrics->GetThebesFontGroup()->GetFirstMathFont();
if (mathFont) {
indexRadicalKern =
mathFont->GetMathConstant(gfxFontEntry::RadicalKernAfterDegree,
oneDevPixel);
indexRadicalKern = -indexRadicalKern;
}
if (indexRadicalKern > aIndexWidth) {
dxIndex = indexRadicalKern - aIndexWidth;
dxSqr = 0;
}
else {
dxIndex = 0;
dxSqr = aIndexWidth - indexRadicalKern;
}
if (mathFont) {
// add some kern before the radical index
nscoord indexRadicalKernBefore = 0;
indexRadicalKernBefore =
mathFont->GetMathConstant(gfxFontEntry::RadicalKernBeforeDegree,
oneDevPixel);
dxIndex += indexRadicalKernBefore;
dxSqr += indexRadicalKernBefore;
} else {
// avoid collision by leaving a minimum space between index and radical
nscoord minimumClearance = aSqrWidth / 2;
if (dxIndex + aIndexWidth + minimumClearance > dxSqr + aSqrWidth) {
if (aIndexWidth + minimumClearance < aSqrWidth) {
dxIndex = aSqrWidth - (aIndexWidth + minimumClearance);
dxSqr = 0;
}
else {
dxIndex = 0;
dxSqr = (aIndexWidth + minimumClearance) - aSqrWidth;
}
}
}
if (aIndexOffset)
*aIndexOffset = dxIndex;
if (aSqrOffset)
*aSqrOffset = dxSqr;
}
void
nsMathMLmrootFrame::Reflow(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus)
{
MarkInReflow();
nsReflowStatus childStatus;
mPresentationData.flags &= ~NS_MATHML_ERROR;
aDesiredSize.ClearSize();
aDesiredSize.SetBlockStartAscent(0);
nsBoundingMetrics bmSqr, bmBase, bmIndex;
nsRenderingContext& renderingContext = *aReflowState.rendContext;
//////////////////
// Reflow Children
int32_t count = 0;
nsIFrame* baseFrame = nullptr;
nsIFrame* indexFrame = nullptr;
nsHTMLReflowMetrics baseSize(aReflowState);
nsHTMLReflowMetrics indexSize(aReflowState);
nsIFrame* childFrame = mFrames.FirstChild();
while (childFrame) {
// ask our children to compute their bounding metrics
nsHTMLReflowMetrics childDesiredSize(aReflowState,
aDesiredSize.mFlags
| NS_REFLOW_CALC_BOUNDING_METRICS);
WritingMode wm = childFrame->GetWritingMode();
LogicalSize availSize = aReflowState.ComputedSize(wm);
availSize.BSize(wm) = NS_UNCONSTRAINEDSIZE;
nsHTMLReflowState childReflowState(aPresContext, aReflowState,
childFrame, availSize);
ReflowChild(childFrame, aPresContext,
childDesiredSize, childReflowState, childStatus);
//NS_ASSERTION(NS_FRAME_IS_COMPLETE(childStatus), "bad status");
if (0 == count) {
// base
baseFrame = childFrame;
baseSize = childDesiredSize;
bmBase = childDesiredSize.mBoundingMetrics;
}
else if (1 == count) {
// index
indexFrame = childFrame;
indexSize = childDesiredSize;
bmIndex = childDesiredSize.mBoundingMetrics;
}
count++;
childFrame = childFrame->GetNextSibling();
}
if (2 != count) {
// report an error, encourage people to get their markups in order
ReportChildCountError();
ReflowError(renderingContext, aDesiredSize);
aStatus = NS_FRAME_COMPLETE;
NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
// Call DidReflow() for the child frames we successfully did reflow.
DidReflowChildren(mFrames.FirstChild(), childFrame);
return;
}
////////////
// Prepare the radical symbol and the overline bar
nsRefPtr<nsFontMetrics> fm;
float fontSizeInflation = nsLayoutUtils::FontSizeInflationFor(this);
nsLayoutUtils::GetFontMetricsForFrame(this, getter_AddRefs(fm),
fontSizeInflation);
nscoord ruleThickness, leading, psi;
GetRadicalParameters(fm, StyleFont()->mMathDisplay ==
NS_MATHML_DISPLAYSTYLE_BLOCK,
ruleThickness, leading, psi);
// built-in: adjust clearance psi to emulate \mathstrut using '1' (TexBook, p.131)
char16_t one = '1';
nsBoundingMetrics bmOne =
nsLayoutUtils::AppUnitBoundsOfString(&one, 1, *fm, renderingContext);
if (bmOne.ascent > bmBase.ascent)
psi += bmOne.ascent - bmBase.ascent;
// make sure that the rule appears on on screen
nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);
if (ruleThickness < onePixel) {
ruleThickness = onePixel;
}
// adjust clearance psi to get an exact number of pixels -- this
// gives a nicer & uniform look on stacked radicals (bug 130282)
nscoord delta = psi % onePixel;
if (delta)
psi += onePixel - delta; // round up
// Stretch the radical symbol to the appropriate height if it is not big enough.
nsBoundingMetrics contSize = bmBase;
contSize.descent = bmBase.ascent + bmBase.descent + psi;
contSize.ascent = ruleThickness;
// height(radical) should be >= height(base) + psi + ruleThickness
nsBoundingMetrics radicalSize;
mSqrChar.Stretch(aPresContext, renderingContext,
fontSizeInflation,
NS_STRETCH_DIRECTION_VERTICAL,
contSize, radicalSize,
NS_STRETCH_LARGER,
StyleVisibility()->mDirection);
// radicalSize have changed at this point, and should match with
// the bounding metrics of the char
mSqrChar.GetBoundingMetrics(bmSqr);
// Update the desired size for the container (like msqrt, index is not yet included)
// the baseline will be that of the base.
mBoundingMetrics.ascent = bmBase.ascent + psi + ruleThickness;
mBoundingMetrics.descent =
std::max(bmBase.descent,
(bmSqr.ascent + bmSqr.descent - mBoundingMetrics.ascent));
mBoundingMetrics.width = bmSqr.width + bmBase.width;
mBoundingMetrics.leftBearing = bmSqr.leftBearing;
mBoundingMetrics.rightBearing = bmSqr.width +
std::max(bmBase.width, bmBase.rightBearing); // take also care of the rule
aDesiredSize.SetBlockStartAscent(mBoundingMetrics.ascent + leading);
aDesiredSize.Height() = aDesiredSize.BlockStartAscent() +
std::max(baseSize.Height() - baseSize.BlockStartAscent(),
mBoundingMetrics.descent + ruleThickness);
aDesiredSize.Width() = mBoundingMetrics.width;
/////////////
// Re-adjust the desired size to include the index.
// the index is raised by some fraction of the height
// of the radical, see \mroot macro in App. B, TexBook
float raiseIndexPercent = 0.6f;
gfxFont* mathFont = fm->GetThebesFontGroup()->GetFirstMathFont();
if (mathFont) {
raiseIndexPercent =
mathFont->GetMathConstant(gfxFontEntry::RadicalDegreeBottomRaisePercent);
}
nscoord raiseIndexDelta = NSToCoordRound(raiseIndexPercent *
(bmSqr.ascent + bmSqr.descent));
nscoord indexRaisedAscent = mBoundingMetrics.ascent // top of radical
- (bmSqr.ascent + bmSqr.descent) // to bottom of radical
+ raiseIndexDelta + bmIndex.ascent + bmIndex.descent; // to top of raised index
nscoord indexClearance = 0;
if (mBoundingMetrics.ascent < indexRaisedAscent) {
indexClearance =
indexRaisedAscent - mBoundingMetrics.ascent; // excess gap introduced by a tall index
mBoundingMetrics.ascent = indexRaisedAscent;
nscoord descent = aDesiredSize.Height() - aDesiredSize.BlockStartAscent();
aDesiredSize.SetBlockStartAscent(mBoundingMetrics.ascent + leading);
aDesiredSize.Height() = aDesiredSize.BlockStartAscent() + descent;
}
nscoord dxIndex, dxSqr;
GetRadicalXOffsets(bmIndex.width, bmSqr.width, fm, &dxIndex, &dxSqr);
mBoundingMetrics.width = dxSqr + bmSqr.width + bmBase.width;
mBoundingMetrics.leftBearing =
std::min(dxIndex + bmIndex.leftBearing, dxSqr + bmSqr.leftBearing);
mBoundingMetrics.rightBearing = dxSqr + bmSqr.width +
std::max(bmBase.width, bmBase.rightBearing);
aDesiredSize.Width() = mBoundingMetrics.width;
aDesiredSize.mBoundingMetrics = mBoundingMetrics;
GatherAndStoreOverflow(&aDesiredSize);
// place the index
nscoord dx = dxIndex;
nscoord dy = aDesiredSize.BlockStartAscent() -
(indexRaisedAscent + indexSize.BlockStartAscent() - bmIndex.ascent);
FinishReflowChild(indexFrame, aPresContext, indexSize, nullptr,
MirrorIfRTL(aDesiredSize.Width(), indexSize.Width(), dx),
dy, 0);
// place the radical symbol and the radical bar
dx = dxSqr;
dy = indexClearance + leading; // leave a leading at the top
mSqrChar.SetRect(nsRect(MirrorIfRTL(aDesiredSize.Width(), bmSqr.width, dx),
dy, bmSqr.width, bmSqr.ascent + bmSqr.descent));
dx += bmSqr.width;
mBarRect.SetRect(MirrorIfRTL(aDesiredSize.Width(), bmBase.width, dx),
dy, bmBase.width, ruleThickness);
// place the base
dy = aDesiredSize.BlockStartAscent() - baseSize.BlockStartAscent();
FinishReflowChild(baseFrame, aPresContext, baseSize, nullptr,
MirrorIfRTL(aDesiredSize.Width(), baseSize.Width(), dx),
dy, 0);
mReference.x = 0;
mReference.y = aDesiredSize.BlockStartAscent();
aStatus = NS_FRAME_COMPLETE;
NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
}
/* virtual */ void
nsMathMLmrootFrame::GetIntrinsicISizeMetrics(nsRenderingContext* aRenderingContext, nsHTMLReflowMetrics& aDesiredSize)
{
nsIFrame* baseFrame = mFrames.FirstChild();
nsIFrame* indexFrame = nullptr;
if (baseFrame)
indexFrame = baseFrame->GetNextSibling();
if (!indexFrame || indexFrame->GetNextSibling()) {
ReflowError(*aRenderingContext, aDesiredSize);
return;
}
float fontSizeInflation = nsLayoutUtils::FontSizeInflationFor(this);
nscoord baseWidth =
nsLayoutUtils::IntrinsicForContainer(aRenderingContext, baseFrame,
nsLayoutUtils::PREF_ISIZE);
nscoord indexWidth =
nsLayoutUtils::IntrinsicForContainer(aRenderingContext, indexFrame,
nsLayoutUtils::PREF_ISIZE);
nscoord sqrWidth = mSqrChar.GetMaxWidth(PresContext(), *aRenderingContext,
fontSizeInflation);
nscoord dxSqr;
nsRefPtr<nsFontMetrics> fm;
nsLayoutUtils::GetFontMetricsForFrame(this, getter_AddRefs(fm),
fontSizeInflation);
GetRadicalXOffsets(indexWidth, sqrWidth, fm, nullptr, &dxSqr);
nscoord width = dxSqr + sqrWidth + baseWidth;
aDesiredSize.Width() = width;
aDesiredSize.mBoundingMetrics.width = width;
aDesiredSize.mBoundingMetrics.leftBearing = 0;
aDesiredSize.mBoundingMetrics.rightBearing = width;
}
// ----------------------
// the Style System will use these to pass the proper style context to our MathMLChar
nsStyleContext*
nsMathMLmrootFrame::GetAdditionalStyleContext(int32_t aIndex) const
{
switch (aIndex) {
case NS_SQR_CHAR_STYLE_CONTEXT_INDEX:
return mSqrChar.GetStyleContext();
break;
default:
return nullptr;
}
}
void
nsMathMLmrootFrame::SetAdditionalStyleContext(int32_t aIndex,
nsStyleContext* aStyleContext)
{
switch (aIndex) {
case NS_SQR_CHAR_STYLE_CONTEXT_INDEX:
mSqrChar.SetStyleContext(aStyleContext);
break;
}
}