1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/PresShell.h"
#include "nsLayoutUtils.h"
#include "nsPresContext.h"
#include <algorithm>
#include "gfxContext.h"
#include "gfxMathTable.h"
using namespace mozilla;
//
// <mroot> -- form a radical - implementation
//
static const char16_t kSqrChar = char16_t(0x221A);
nsIFrame* NS_NewMathMLmrootFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
return new (aPresShell)
nsMathMLmrootFrame(aStyle, aPresShell->GetPresContext());
}
NS_IMPL_FRAMEARENA_HELPERS(nsMathMLmrootFrame)
nsMathMLmrootFrame::nsMathMLmrootFrame(ComputedStyle* aStyle,
nsPresContext* aPresContext)
: nsMathMLContainerFrame(aStyle, aPresContext, kClassID) {}
nsMathMLmrootFrame::~nsMathMLmrootFrame() = default;
void nsMathMLmrootFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
nsIFrame* aPrevInFlow) {
nsMathMLContainerFrame::Init(aContent, aParent, aPrevInFlow);
nsAutoString sqrChar;
sqrChar.Assign(kSqrChar);
mSqrChar.SetData(sqrChar);
mSqrChar.SetComputedStyle(Style());
}
bool nsMathMLmrootFrame::ShouldUseRowFallback() {
bool isRootWithIndex = GetContent()->IsMathMLElement(nsGkAtoms::mroot);
if (!isRootWithIndex) {
return false;
}
// An mroot element expects exactly two children.
nsIFrame* baseFrame = mFrames.FirstChild();
if (!baseFrame) {
return true;
}
nsIFrame* indexFrame = baseFrame->GetNextSibling();
return !indexFrame || indexFrame->GetNextSibling();
}
bool nsMathMLmrootFrame::IsMrowLike() {
bool isRootWithIndex = GetContent()->IsMathMLElement(nsGkAtoms::mroot);
if (isRootWithIndex) {
return false;
}
return mFrames.FirstChild() != mFrames.LastChild() || !mFrames.FirstChild();
}
NS_IMETHODIMP
nsMathMLmrootFrame::InheritAutomaticData(nsIFrame* aParent) {
nsMathMLContainerFrame::InheritAutomaticData(aParent);
bool isRootWithIndex = GetContent()->IsMathMLElement(nsGkAtoms::mroot);
if (!isRootWithIndex) {
mPresentationData.flags |= NS_MATHML_STRETCH_ALL_CHILDREN_VERTICALLY;
}
return NS_OK;
}
NS_IMETHODIMP
nsMathMLmrootFrame::TransmitAutomaticData() {
bool isRootWithIndex = GetContent()->IsMathMLElement(nsGkAtoms::mroot);
if (isRootWithIndex) {
// 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);
} else {
// The TeXBook (Ch 17. p.141) says that \sqrt is cramped
UpdatePresentationDataFromChildAt(0, -1, NS_MATHML_COMPRESSED,
NS_MATHML_COMPRESSED);
}
return NS_OK;
}
void nsMathMLmrootFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsDisplayListSet& aLists) {
/////////////
// paint the content we are square-rooting
nsMathMLContainerFrame::BuildDisplayList(aBuilder, aLists);
if (ShouldUseRowFallback()) {
return;
}
/////////////
// paint the sqrt symbol
mSqrChar.Display(aBuilder, this, aLists, 0);
DisplayBar(aBuilder, this, mBarRect, aLists);
}
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, radicalKernBeforeDegree, radicalKernAfterDegree;
nscoord oneDevPixel = aFontMetrics->AppUnitsPerDevPixel();
RefPtr<gfxFont> mathFont =
aFontMetrics->GetThebesFontGroup()->GetFirstMathFont();
if (mathFont) {
radicalKernBeforeDegree = mathFont->MathTable()->Constant(
gfxMathTable::RadicalKernBeforeDegree, oneDevPixel);
radicalKernAfterDegree = mathFont->MathTable()->Constant(
gfxMathTable::RadicalKernAfterDegree, oneDevPixel);
} else {
nscoord em;
GetEmHeight(aFontMetrics, em);
radicalKernBeforeDegree = NSToCoordRound(5.0f * em / 18);
radicalKernAfterDegree = NSToCoordRound(-10.0f * em / 18);
}
// Clamp radical kern degrees according to spec:
// https://w3c.github.io/mathml-core/#root-with-index
radicalKernBeforeDegree = std::max(0, radicalKernBeforeDegree);
radicalKernAfterDegree = std::max(-aIndexWidth, radicalKernAfterDegree);
dxIndex = radicalKernBeforeDegree;
dxSqr = radicalKernBeforeDegree + aIndexWidth + radicalKernAfterDegree;
if (aIndexOffset) {
*aIndexOffset = dxIndex;
}
if (aSqrOffset) {
*aSqrOffset = dxSqr;
}
}
nsresult nsMathMLmrootFrame::Place(DrawTarget* aDrawTarget,
const PlaceFlags& aFlags,
ReflowOutput& aDesiredSize) {
if (ShouldUseRowFallback()) {
// report an error, encourage people to get their markups in order
if (!aFlags.contains(PlaceFlag::MeasureOnly)) {
ReportChildCountError();
}
return PlaceAsMrow(aDrawTarget, aFlags, aDesiredSize);
}
const bool isRootWithIndex = GetContent()->IsMathMLElement(nsGkAtoms::mroot);
nsBoundingMetrics bmSqr, bmBase, bmIndex;
nsIFrame *baseFrame = nullptr, *indexFrame = nullptr;
nsMargin baseMargin, indexMargin;
ReflowOutput baseSize(aDesiredSize.GetWritingMode());
ReflowOutput indexSize(aDesiredSize.GetWritingMode());
if (isRootWithIndex) {
baseFrame = mFrames.FirstChild();
indexFrame = baseFrame->GetNextSibling();
baseMargin = GetMarginForPlace(aFlags, baseFrame);
indexMargin = GetMarginForPlace(aFlags, indexFrame);
GetReflowAndBoundingMetricsFor(baseFrame, baseSize, bmBase);
GetReflowAndBoundingMetricsFor(indexFrame, indexSize, bmIndex);
} else {
// Format our content as an mrow without border/padding to obtain the
// square root base. The metrics/frame for the index are ignored.
PlaceFlags flags = aFlags + PlaceFlag::MeasureOnly +
PlaceFlag::IgnoreBorderPadding +
PlaceFlag::DoNotAdjustForWidthAndHeight;
nsresult rv = nsMathMLContainerFrame::Place(aDrawTarget, flags, baseSize);
if (NS_FAILED(rv)) {
DidReflowChildren(PrincipalChildList().FirstChild());
return rv;
}
bmBase = baseSize.mBoundingMetrics;
}
////////////
// Prepare the radical symbol and the overline bar
float fontSizeInflation = nsLayoutUtils::FontSizeInflationFor(this);
RefPtr<nsFontMetrics> fm =
nsLayoutUtils::GetFontMetricsForFrame(this, fontSizeInflation);
nscoord ruleThickness, leading, psi;
GetRadicalParameters(fm, StyleFont()->mMathStyle == StyleMathStyle::Normal,
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, aDrawTarget);
if (bmOne.ascent > bmBase.ascent + baseMargin.top) {
psi += bmOne.ascent - bmBase.ascent - baseMargin.top;
}
// 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 + baseMargin.TopBottom() + psi;
contSize.ascent = ruleThickness;
// height(radical) should be >= height(base) + psi + ruleThickness
nsBoundingMetrics radicalSize;
if (aFlags.contains(PlaceFlag::IntrinsicSize)) {
nscoord radical_width =
mSqrChar.GetMaxWidth(this, aDrawTarget, fontSizeInflation);
bmSqr.leftBearing = 0;
bmSqr.rightBearing = radical_width;
bmSqr.width = radical_width;
bmSqr.ascent = bmSqr.descent = 0;
} else {
mSqrChar.Stretch(this, aDrawTarget, fontSizeInflation,
NS_STRETCH_DIRECTION_VERTICAL, contSize, radicalSize,
NS_STRETCH_LARGER,
StyleVisibility()->mDirection == StyleDirection::Rtl);
// 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 + baseMargin.top + psi + ruleThickness;
mBoundingMetrics.descent =
std::max(bmBase.descent + baseMargin.bottom,
(bmSqr.ascent + bmSqr.descent - mBoundingMetrics.ascent));
mBoundingMetrics.width = bmSqr.width + bmBase.width + baseMargin.LeftRight();
mBoundingMetrics.leftBearing = bmSqr.leftBearing;
mBoundingMetrics.rightBearing =
bmSqr.width +
std::max(
bmBase.width + baseMargin.LeftRight(),
bmBase.rightBearing + baseMargin.left); // 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;
nscoord indexClearance = 0, dxIndex = 0, dxSqr = 0, indexRaisedAscent = 0;
if (isRootWithIndex) {
/////////////
// 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;
RefPtr<gfxFont> mathFont = fm->GetThebesFontGroup()->GetFirstMathFont();
if (mathFont) {
raiseIndexPercent = mathFont->MathTable()->Constant(
gfxMathTable::RadicalDegreeBottomRaisePercent);
}
nscoord raiseIndexDelta =
NSToCoordRound(raiseIndexPercent * (bmSqr.ascent + bmSqr.descent));
indexRaisedAscent = mBoundingMetrics.ascent // top of radical
-
(bmSqr.ascent + bmSqr.descent) // to bottom of radical
+ raiseIndexDelta + bmIndex.ascent + bmIndex.descent +
indexMargin.TopBottom(); // to top of raised index
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;
}
GetRadicalXOffsets(bmIndex.width + indexMargin.LeftRight(), bmSqr.width, fm,
&dxIndex, &dxSqr);
mBoundingMetrics.width =
dxSqr + bmSqr.width + bmBase.width + baseMargin.LeftRight();
mBoundingMetrics.leftBearing =
std::min(dxIndex + bmIndex.leftBearing, dxSqr + bmSqr.leftBearing);
mBoundingMetrics.rightBearing =
dxSqr + bmSqr.width +
std::max(bmBase.width + baseMargin.LeftRight(),
bmBase.rightBearing + baseMargin.left);
aDesiredSize.Width() = mBoundingMetrics.width;
}
aDesiredSize.mBoundingMetrics = mBoundingMetrics;
// Apply width/height to math content box.
const PlaceFlags flags;
auto sizes = GetWidthAndHeightForPlaceAdjustment(flags);
nscoord shiftX = ApplyAdjustmentForWidthAndHeight(flags, sizes, aDesiredSize,
mBoundingMetrics);
// Add padding+border around the final layout.
auto borderPadding = GetBorderPaddingForPlace(aFlags);
InflateReflowAndBoundingMetrics(borderPadding, aDesiredSize,
mBoundingMetrics);
if (!aFlags.contains(PlaceFlag::MeasureOnly)) {
nsPresContext* presContext = PresContext();
const bool isRTL = StyleVisibility()->mDirection == StyleDirection::Rtl;
nscoord borderPaddingInlineStart =
isRTL ? borderPadding.right : borderPadding.left;
nscoord dx, dy;
if (isRootWithIndex) {
// place the index
dx = borderPaddingInlineStart + dxIndex +
indexMargin.Side(isRTL ? eSideRight : eSideLeft);
dy = aDesiredSize.BlockStartAscent() -
(indexRaisedAscent + indexSize.BlockStartAscent() - bmIndex.ascent);
FinishReflowChild(
indexFrame, presContext, indexSize, nullptr,
MirrorIfRTL(aDesiredSize.Width(), indexSize.Width(), dx),
dy + indexMargin.top, ReflowChildFlags::Default);
}
// place the radical symbol and the radical bar
dx = borderPaddingInlineStart + dxSqr;
dy = borderPadding.top + 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 + baseMargin.LeftRight(), dx),
dy, bmBase.width + baseMargin.LeftRight(), ruleThickness);
// place the base
if (isRootWithIndex) {
dx += isRTL ? baseMargin.right : baseMargin.left;
dy = aDesiredSize.BlockStartAscent() - baseSize.BlockStartAscent();
FinishReflowChild(baseFrame, presContext, baseSize, nullptr,
MirrorIfRTL(aDesiredSize.Width(), baseSize.Width(), dx),
dy, ReflowChildFlags::Default);
} else {
nscoord dx_left = borderPadding.left + shiftX;
if (!isRTL) {
dx_left += bmSqr.width;
}
PositionRowChildFrames(dx_left, aDesiredSize.BlockStartAscent());
}
}
mReference.x = 0;
mReference.y = aDesiredSize.BlockStartAscent();
return NS_OK;
}
void nsMathMLmrootFrame::DidSetComputedStyle(ComputedStyle* aOldStyle) {
nsMathMLContainerFrame::DidSetComputedStyle(aOldStyle);
mSqrChar.SetComputedStyle(Style());
}
|