Bumping manifests a=b2g-bump
[gecko.git] / layout / mathml / nsMathMLmoFrame.cpp
blobdf0f443cb52a3a38d5330584941617c51df0f673
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsMathMLmoFrame.h"
7 #include "nsPresContext.h"
8 #include "nsRenderingContext.h"
9 #include "nsContentUtils.h"
10 #include "nsFrameSelection.h"
11 #include "nsMathMLElement.h"
12 #include <algorithm>
15 // <mo> -- operator, fence, or separator - implementation
18 // additional style context to be used by our MathMLChar.
19 #define NS_MATHML_CHAR_STYLE_CONTEXT_INDEX 0
21 nsIFrame*
22 NS_NewMathMLmoFrame(nsIPresShell* aPresShell, nsStyleContext *aContext)
24 return new (aPresShell) nsMathMLmoFrame(aContext);
27 NS_IMPL_FRAMEARENA_HELPERS(nsMathMLmoFrame)
29 nsMathMLmoFrame::~nsMathMLmoFrame()
33 static const char16_t kApplyFunction = char16_t(0x2061);
34 static const char16_t kInvisibleTimes = char16_t(0x2062);
35 static const char16_t kInvisibleSeparator = char16_t(0x2063);
36 static const char16_t kInvisiblePlus = char16_t(0x2064);
38 eMathMLFrameType
39 nsMathMLmoFrame::GetMathMLFrameType()
41 return NS_MATHML_OPERATOR_IS_INVISIBLE(mFlags)
42 ? eMathMLFrameType_OperatorInvisible
43 : eMathMLFrameType_OperatorOrdinary;
46 // since a mouse click implies selection, we cannot just rely on the
47 // frame's state bit in our child text frame. So we will first check
48 // its selected state bit, and use this little helper to double check.
49 bool
50 nsMathMLmoFrame::IsFrameInSelection(nsIFrame* aFrame)
52 NS_ASSERTION(aFrame, "null arg");
53 if (!aFrame || !aFrame->IsSelected())
54 return false;
56 const nsFrameSelection* frameSelection = aFrame->GetConstFrameSelection();
57 SelectionDetails* details =
58 frameSelection->LookUpSelection(aFrame->GetContent(), 0, 1, true);
60 if (!details)
61 return false;
63 while (details) {
64 SelectionDetails* next = details->mNext;
65 delete details;
66 details = next;
68 return true;
71 bool
72 nsMathMLmoFrame::UseMathMLChar()
74 return (NS_MATHML_OPERATOR_GET_FORM(mFlags) &&
75 NS_MATHML_OPERATOR_IS_MUTABLE(mFlags)) ||
76 NS_MATHML_OPERATOR_IS_CENTERED(mFlags);
79 void
80 nsMathMLmoFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
81 const nsRect& aDirtyRect,
82 const nsDisplayListSet& aLists)
84 bool useMathMLChar = UseMathMLChar();
86 if (!useMathMLChar) {
87 // let the base class do everything
88 nsMathMLTokenFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
89 } else {
90 DisplayBorderBackgroundOutline(aBuilder, aLists);
92 // make our char selected if our inner child text frame is selected
93 bool isSelected = false;
94 nsRect selectedRect;
95 nsIFrame* firstChild = mFrames.FirstChild();
96 if (IsFrameInSelection(firstChild)) {
97 mMathMLChar.GetRect(selectedRect);
98 // add a one pixel border (it renders better for operators like minus)
99 selectedRect.Inflate(nsPresContext::CSSPixelsToAppUnits(1));
100 isSelected = true;
102 mMathMLChar.Display(aBuilder, this, aLists, 0, isSelected ? &selectedRect : nullptr);
104 #if defined(DEBUG) && defined(SHOW_BOUNDING_BOX)
105 // for visual debug
106 DisplayBoundingMetrics(aBuilder, this, mReference, mBoundingMetrics, aLists);
107 #endif
111 // get the text that we enclose and setup our nsMathMLChar
112 void
113 nsMathMLmoFrame::ProcessTextData()
115 mFlags = 0;
117 nsAutoString data;
118 if (!nsContentUtils::GetNodeTextContent(mContent, false, data)) {
119 NS_RUNTIMEABORT("OOM");
122 data.CompressWhitespace();
123 int32_t length = data.Length();
124 char16_t ch = (length == 0) ? char16_t('\0') : data[0];
126 if ((length == 1) &&
127 (ch == kApplyFunction ||
128 ch == kInvisibleSeparator ||
129 ch == kInvisiblePlus ||
130 ch == kInvisibleTimes)) {
131 mFlags |= NS_MATHML_OPERATOR_INVISIBLE;
134 // don't bother doing anything special if we don't have a single child
135 nsPresContext* presContext = PresContext();
136 if (mFrames.GetLength() != 1) {
137 data.Truncate(); // empty data to reset the char
138 mMathMLChar.SetData(presContext, data);
139 ResolveMathMLCharStyle(presContext, mContent, mStyleContext, &mMathMLChar);
140 return;
143 // special... in math mode, the usual minus sign '-' looks too short, so
144 // what we do here is to remap <mo>-</mo> to the official Unicode minus
145 // sign (U+2212) which looks much better. For background on this, see
146 // http://groups.google.com/groups?hl=en&th=66488daf1ade7635&rnum=1
147 if (1 == length && ch == '-') {
148 ch = 0x2212;
149 data = ch;
152 // cache the special bits: mutable, accent, movablelimits, centered.
153 // we need to do this in anticipation of other requirements, and these
154 // bits don't change. Do not reset these bits unless the text gets changed.
156 // lookup all the forms under which the operator is listed in the dictionary,
157 // and record whether the operator has accent="true" or movablelimits="true"
158 nsOperatorFlags flags[4];
159 float lspace[4], rspace[4];
160 nsMathMLOperators::LookupOperators(data, flags, lspace, rspace);
161 nsOperatorFlags allFlags =
162 flags[NS_MATHML_OPERATOR_FORM_INFIX] |
163 flags[NS_MATHML_OPERATOR_FORM_POSTFIX] |
164 flags[NS_MATHML_OPERATOR_FORM_PREFIX];
166 mFlags |= allFlags & NS_MATHML_OPERATOR_ACCENT;
167 mFlags |= allFlags & NS_MATHML_OPERATOR_MOVABLELIMITS;
169 // see if this is an operator that should be centered to cater for
170 // fonts that are not math-aware
171 if (1 == length) {
172 if ((ch == '+') || (ch == '=') || (ch == '*') ||
173 (ch == 0x2212) || // &minus;
174 (ch == 0x2264) || // &le;
175 (ch == 0x2265) || // &ge;
176 (ch == 0x00D7)) { // &times;
177 mFlags |= NS_MATHML_OPERATOR_CENTERED;
181 // cache the operator
182 mMathMLChar.SetData(presContext, data);
184 // cache the native direction -- beware of bug 133429...
185 // mEmbellishData.direction must always retain our native direction, whereas
186 // mMathMLChar.GetStretchDirection() may change later, when Stretch() is called
187 mEmbellishData.direction = mMathMLChar.GetStretchDirection();
189 bool isMutable =
190 NS_MATHML_OPERATOR_IS_LARGEOP(allFlags) ||
191 (mEmbellishData.direction != NS_STRETCH_DIRECTION_UNSUPPORTED);
192 if (isMutable)
193 mFlags |= NS_MATHML_OPERATOR_MUTABLE;
195 ResolveMathMLCharStyle(presContext, mContent, mStyleContext, &mMathMLChar);
198 // get our 'form' and lookup in the Operator Dictionary to fetch
199 // our default data that may come from there. Then complete our setup
200 // using attributes that we may have. To stay in sync, this function is
201 // called very often. We depend on many things that may change around us.
202 // However, we re-use unchanged values.
203 void
204 nsMathMLmoFrame::ProcessOperatorData()
206 // if we have been here before, we will just use our cached form
207 nsOperatorFlags form = NS_MATHML_OPERATOR_GET_FORM(mFlags);
208 nsAutoString value;
209 float fontSizeInflation = nsLayoutUtils::FontSizeInflationFor(this);
211 // special bits are always kept in mFlags.
212 // remember the mutable bit from ProcessTextData().
213 // Some chars are listed under different forms in the dictionary,
214 // and there could be a form under which the char is mutable.
215 // If the char is the core of an embellished container, we will keep
216 // it mutable irrespective of the form of the embellished container.
217 // Also remember the other special bits that we want to carry forward.
218 mFlags &= NS_MATHML_OPERATOR_MUTABLE |
219 NS_MATHML_OPERATOR_ACCENT |
220 NS_MATHML_OPERATOR_MOVABLELIMITS |
221 NS_MATHML_OPERATOR_CENTERED |
222 NS_MATHML_OPERATOR_INVISIBLE;
224 if (!mEmbellishData.coreFrame) {
225 // i.e., we haven't been here before, the default form is infix
226 form = NS_MATHML_OPERATOR_FORM_INFIX;
228 // reset everything so that we don't keep outdated values around
229 // in case of dynamic changes
230 mEmbellishData.flags = 0;
231 mEmbellishData.coreFrame = nullptr;
232 mEmbellishData.leadingSpace = 0;
233 mEmbellishData.trailingSpace = 0;
234 if (mMathMLChar.Length() != 1)
235 mEmbellishData.direction = NS_STRETCH_DIRECTION_UNSUPPORTED;
236 // else... retain the native direction obtained in ProcessTextData()
238 if (!mFrames.FirstChild()) {
239 return;
242 mEmbellishData.flags |= NS_MATHML_EMBELLISH_OPERATOR;
243 mEmbellishData.coreFrame = this;
245 // there are two particular things that we also need to record so that if our
246 // parent is <mover>, <munder>, or <munderover>, they will treat us properly:
247 // 1) do we have accent="true"
248 // 2) do we have movablelimits="true"
250 // they need the extra information to decide how to treat their scripts/limits
251 // (note: <mover>, <munder>, or <munderover> need not necessarily be our
252 // direct parent -- case of embellished operators)
254 // default values from the Operator Dictionary were obtained in ProcessTextData()
255 // and these special bits are always kept in mFlags
256 if (NS_MATHML_OPERATOR_IS_ACCENT(mFlags))
257 mEmbellishData.flags |= NS_MATHML_EMBELLISH_ACCENT;
258 if (NS_MATHML_OPERATOR_IS_MOVABLELIMITS(mFlags))
259 mEmbellishData.flags |= NS_MATHML_EMBELLISH_MOVABLELIMITS;
261 // see if the accent attribute is there
262 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::accent_, value);
263 if (value.EqualsLiteral("true"))
264 mEmbellishData.flags |= NS_MATHML_EMBELLISH_ACCENT;
265 else if (value.EqualsLiteral("false"))
266 mEmbellishData.flags &= ~NS_MATHML_EMBELLISH_ACCENT;
268 // see if the movablelimits attribute is there
269 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::movablelimits_, value);
270 if (value.EqualsLiteral("true"))
271 mEmbellishData.flags |= NS_MATHML_EMBELLISH_MOVABLELIMITS;
272 else if (value.EqualsLiteral("false"))
273 mEmbellishData.flags &= ~NS_MATHML_EMBELLISH_MOVABLELIMITS;
275 // ---------------------------------------------------------------------
276 // we will be called again to re-sync the rest of our state next time...
277 // (nobody needs the other values below at this stage)
278 mFlags |= form;
279 return;
282 nsPresContext* presContext = PresContext();
284 // beware of bug 133814 - there is a two-way dependency in the
285 // embellished hierarchy: our embellished ancestors need to set
286 // their flags based on some of our state (set above), and here we
287 // need to re-sync our 'form' depending on our outermost embellished
288 // container. A null form here means that an earlier attempt to stretch
289 // our mMathMLChar failed, in which case we don't bother re-stretching again
290 if (form) {
291 // get our outermost embellished container and its parent.
292 // (we ensure that we are the core, not just a sibling of the core)
293 nsIFrame* embellishAncestor = this;
294 nsEmbellishData embellishData;
295 nsIFrame* parentAncestor = this;
296 do {
297 embellishAncestor = parentAncestor;
298 parentAncestor = embellishAncestor->GetParent();
299 GetEmbellishDataFrom(parentAncestor, embellishData);
300 } while (embellishData.coreFrame == this);
302 // flag if we have an embellished ancestor
303 if (embellishAncestor != this)
304 mFlags |= NS_MATHML_OPERATOR_EMBELLISH_ANCESTOR;
305 else
306 mFlags &= ~NS_MATHML_OPERATOR_EMBELLISH_ANCESTOR;
308 // find the position of our outermost embellished container w.r.t
309 // its siblings.
311 nsIFrame* nextSibling = embellishAncestor->GetNextSibling();
312 nsIFrame* prevSibling = embellishAncestor->GetPrevSibling();
314 // flag to distinguish from a real infix. Set for (embellished) operators
315 // that live in (inferred) mrows.
316 nsIMathMLFrame* mathAncestor = do_QueryFrame(parentAncestor);
317 bool zeroSpacing = false;
318 if (mathAncestor) {
319 zeroSpacing = !mathAncestor->IsMrowLike();
320 } else {
321 nsMathMLmathBlockFrame* blockFrame = do_QueryFrame(parentAncestor);
322 if (blockFrame) {
323 zeroSpacing = !blockFrame->IsMrowLike();
326 if (zeroSpacing) {
327 mFlags |= NS_MATHML_OPERATOR_EMBELLISH_ISOLATED;
328 } else {
329 mFlags &= ~NS_MATHML_OPERATOR_EMBELLISH_ISOLATED;
332 // find our form
333 form = NS_MATHML_OPERATOR_FORM_INFIX;
334 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::form, value);
335 if (!value.IsEmpty()) {
336 if (value.EqualsLiteral("prefix"))
337 form = NS_MATHML_OPERATOR_FORM_PREFIX;
338 else if (value.EqualsLiteral("postfix"))
339 form = NS_MATHML_OPERATOR_FORM_POSTFIX;
341 else {
342 // set our form flag depending on the position
343 if (!prevSibling && nextSibling)
344 form = NS_MATHML_OPERATOR_FORM_PREFIX;
345 else if (prevSibling && !nextSibling)
346 form = NS_MATHML_OPERATOR_FORM_POSTFIX;
348 mFlags &= ~NS_MATHML_OPERATOR_FORM; // clear the old form bits
349 mFlags |= form;
351 // Use the default value suggested by the MathML REC.
352 // http://www.w3.org/TR/MathML/chapter3.html#presm.mo.attrs
353 // thickmathspace = 5/18em
354 float lspace = 5.0f/18.0f;
355 float rspace = 5.0f/18.0f;
356 // lookup the operator dictionary
357 nsAutoString data;
358 mMathMLChar.GetData(data);
359 nsMathMLOperators::LookupOperator(data, form, &mFlags, &lspace, &rspace);
360 // Spacing is zero if our outermost embellished operator is not in an
361 // inferred mrow.
362 if (!NS_MATHML_OPERATOR_EMBELLISH_IS_ISOLATED(mFlags) &&
363 (lspace || rspace)) {
364 // Cache the default values of lspace and rspace.
365 // since these values are relative to the 'em' unit, convert to twips now
366 nscoord em;
367 nsRefPtr<nsFontMetrics> fm;
368 nsLayoutUtils::GetFontMetricsForFrame(this, getter_AddRefs(fm),
369 fontSizeInflation);
370 GetEmHeight(fm, em);
372 mEmbellishData.leadingSpace = NSToCoordRound(lspace * em);
373 mEmbellishData.trailingSpace = NSToCoordRound(rspace * em);
375 // tuning if we don't want too much extra space when we are a script.
376 // (with its fonts, TeX sets lspace=0 & rspace=0 as soon as scriptlevel>0.
377 // Our fonts can be anything, so...)
378 if (StyleFont()->mScriptLevel > 0 &&
379 !NS_MATHML_OPERATOR_HAS_EMBELLISH_ANCESTOR(mFlags)) {
380 mEmbellishData.leadingSpace /= 2;
381 mEmbellishData.trailingSpace /= 2;
386 // If we are an accent without explicit lspace="." or rspace=".",
387 // we will ignore our default leading/trailing space
389 // lspace
391 // "Specifies the leading space appearing before the operator"
393 // values: length
394 // default: set by dictionary (thickmathspace)
396 // XXXfredw Support for negative and relative values is not implemented
397 // (bug 805926).
398 // Relative values will give a multiple of the current leading space,
399 // which is not necessarily the default one.
401 nscoord leadingSpace = mEmbellishData.leadingSpace;
402 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::lspace_, value);
403 if (!value.IsEmpty()) {
404 nsCSSValue cssValue;
405 if (nsMathMLElement::ParseNumericValue(value, cssValue, 0,
406 mContent->OwnerDoc())) {
407 if ((eCSSUnit_Number == cssValue.GetUnit()) && !cssValue.GetFloatValue())
408 leadingSpace = 0;
409 else if (cssValue.IsLengthUnit())
410 leadingSpace = CalcLength(presContext, mStyleContext, cssValue,
411 fontSizeInflation);
412 mFlags |= NS_MATHML_OPERATOR_LSPACE_ATTR;
416 // rspace
418 // "Specifies the trailing space appearing after the operator"
420 // values: length
421 // default: set by dictionary (thickmathspace)
423 // XXXfredw Support for negative and relative values is not implemented
424 // (bug 805926).
425 // Relative values will give a multiple of the current leading space,
426 // which is not necessarily the default one.
428 nscoord trailingSpace = mEmbellishData.trailingSpace;
429 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::rspace_, value);
430 if (!value.IsEmpty()) {
431 nsCSSValue cssValue;
432 if (nsMathMLElement::ParseNumericValue(value, cssValue, 0,
433 mContent->OwnerDoc())) {
434 if ((eCSSUnit_Number == cssValue.GetUnit()) && !cssValue.GetFloatValue())
435 trailingSpace = 0;
436 else if (cssValue.IsLengthUnit())
437 trailingSpace = CalcLength(presContext, mStyleContext, cssValue,
438 fontSizeInflation);
439 mFlags |= NS_MATHML_OPERATOR_RSPACE_ATTR;
443 // little extra tuning to round lspace & rspace to at least a pixel so that
444 // operators don't look as if they are colliding with their operands
445 if (leadingSpace || trailingSpace) {
446 nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);
447 if (leadingSpace && leadingSpace < onePixel)
448 leadingSpace = onePixel;
449 if (trailingSpace && trailingSpace < onePixel)
450 trailingSpace = onePixel;
453 // the values that we get from our attributes override the dictionary
454 mEmbellishData.leadingSpace = leadingSpace;
455 mEmbellishData.trailingSpace = trailingSpace;
457 // Now see if there are user-defined attributes that override the dictionary.
458 // XXX If an attribute can be forced to be true when it is false in the
459 // dictionary, then the following code has to change...
461 // For each attribute overriden by the user, turn off its bit flag.
462 // symmetric|movablelimits|separator|largeop|accent|fence|stretchy|form
463 // special: accent and movablelimits are handled above,
464 // don't process them here
466 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::stretchy_, value);
467 if (value.EqualsLiteral("false")) {
468 mFlags &= ~NS_MATHML_OPERATOR_STRETCHY;
469 } else if (value.EqualsLiteral("true")) {
470 mFlags |= NS_MATHML_OPERATOR_STRETCHY;
472 if (NS_MATHML_OPERATOR_IS_FENCE(mFlags)) {
473 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::fence_, value);
474 if (value.EqualsLiteral("false"))
475 mFlags &= ~NS_MATHML_OPERATOR_FENCE;
477 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::largeop_, value);
478 if (value.EqualsLiteral("false")) {
479 mFlags &= ~NS_MATHML_OPERATOR_LARGEOP;
480 } else if (value.EqualsLiteral("true")) {
481 mFlags |= NS_MATHML_OPERATOR_LARGEOP;
483 if (NS_MATHML_OPERATOR_IS_SEPARATOR(mFlags)) {
484 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::separator_, value);
485 if (value.EqualsLiteral("false"))
486 mFlags &= ~NS_MATHML_OPERATOR_SEPARATOR;
488 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::symmetric_, value);
489 if (value.EqualsLiteral("false"))
490 mFlags &= ~NS_MATHML_OPERATOR_SYMMETRIC;
491 else if (value.EqualsLiteral("true"))
492 mFlags |= NS_MATHML_OPERATOR_SYMMETRIC;
495 // minsize
497 // "Specifies the minimum size of the operator when stretchy"
499 // values: length
500 // default: set by dictionary (1em)
502 // We don't allow negative values.
503 // Note: Contrary to other "length" values, unitless and percentage do not
504 // give a multiple of the defaut value but a multiple of the operator at
505 // normal size.
507 mMinSize = 0;
508 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::minsize_, value);
509 if (!value.IsEmpty()) {
510 nsCSSValue cssValue;
511 if (nsMathMLElement::ParseNumericValue(value, cssValue,
512 nsMathMLElement::
513 PARSE_ALLOW_UNITLESS,
514 mContent->OwnerDoc())) {
515 nsCSSUnit unit = cssValue.GetUnit();
516 if (eCSSUnit_Number == unit)
517 mMinSize = cssValue.GetFloatValue();
518 else if (eCSSUnit_Percent == unit)
519 mMinSize = cssValue.GetPercentValue();
520 else if (eCSSUnit_Null != unit) {
521 mMinSize = float(CalcLength(presContext, mStyleContext, cssValue,
522 fontSizeInflation));
523 mFlags |= NS_MATHML_OPERATOR_MINSIZE_ABSOLUTE;
528 // maxsize
530 // "Specifies the maximum size of the operator when stretchy"
532 // values: length | "infinity"
533 // default: set by dictionary (infinity)
535 // We don't allow negative values.
536 // Note: Contrary to other "length" values, unitless and percentage do not
537 // give a multiple of the defaut value but a multiple of the operator at
538 // normal size.
540 mMaxSize = NS_MATHML_OPERATOR_SIZE_INFINITY;
541 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::maxsize_, value);
542 if (!value.IsEmpty()) {
543 nsCSSValue cssValue;
544 if (nsMathMLElement::ParseNumericValue(value, cssValue,
545 nsMathMLElement::
546 PARSE_ALLOW_UNITLESS,
547 mContent->OwnerDoc())) {
548 nsCSSUnit unit = cssValue.GetUnit();
549 if (eCSSUnit_Number == unit)
550 mMaxSize = cssValue.GetFloatValue();
551 else if (eCSSUnit_Percent == unit)
552 mMaxSize = cssValue.GetPercentValue();
553 else if (eCSSUnit_Null != unit) {
554 mMaxSize = float(CalcLength(presContext, mStyleContext, cssValue,
555 fontSizeInflation));
556 mFlags |= NS_MATHML_OPERATOR_MAXSIZE_ABSOLUTE;
562 static uint32_t
563 GetStretchHint(nsOperatorFlags aFlags, nsPresentationData aPresentationData,
564 bool aIsVertical, const nsStyleFont* aStyleFont)
566 uint32_t stretchHint = NS_STRETCH_NONE;
567 // See if it is okay to stretch,
568 // starting from what the Operator Dictionary said
569 if (NS_MATHML_OPERATOR_IS_MUTABLE(aFlags)) {
570 // set the largeop or largeopOnly flags to suitably cover all the
571 // 8 possible cases depending on whether displaystyle, largeop,
572 // stretchy are true or false (see bug 69325).
573 // . largeopOnly is taken if largeop=true and stretchy=false
574 // . largeop is taken if largeop=true and stretchy=true
575 if (aStyleFont->mMathDisplay == NS_MATHML_DISPLAYSTYLE_BLOCK &&
576 NS_MATHML_OPERATOR_IS_LARGEOP(aFlags)) {
577 stretchHint = NS_STRETCH_LARGEOP; // (largeopOnly, not mask!)
578 if (NS_MATHML_OPERATOR_IS_INTEGRAL(aFlags)) {
579 stretchHint |= NS_STRETCH_INTEGRAL;
581 if (NS_MATHML_OPERATOR_IS_STRETCHY(aFlags)) {
582 stretchHint |= NS_STRETCH_NEARER | NS_STRETCH_LARGER;
585 else if(NS_MATHML_OPERATOR_IS_STRETCHY(aFlags)) {
586 if (aIsVertical) {
587 // TeX hint. Can impact some sloppy markups missing <mrow></mrow>
588 stretchHint = NS_STRETCH_NEARER;
590 else {
591 stretchHint = NS_STRETCH_NORMAL;
594 // else if the stretchy and largeop attributes have been disabled,
595 // the operator is not mutable
597 return stretchHint;
600 // NOTE: aDesiredStretchSize is an IN/OUT parameter
601 // On input - it contains our current size
602 // On output - the same size or the new size that we want
603 NS_IMETHODIMP
604 nsMathMLmoFrame::Stretch(nsRenderingContext& aRenderingContext,
605 nsStretchDirection aStretchDirection,
606 nsBoundingMetrics& aContainerSize,
607 nsHTMLReflowMetrics& aDesiredStretchSize)
609 if (NS_MATHML_STRETCH_WAS_DONE(mPresentationData.flags)) {
610 NS_WARNING("it is wrong to fire stretch more than once on a frame");
611 return NS_OK;
613 mPresentationData.flags |= NS_MATHML_STRETCH_DONE;
615 nsIFrame* firstChild = mFrames.FirstChild();
617 // get the axis height;
618 float fontSizeInflation = nsLayoutUtils::FontSizeInflationFor(this);
619 nsRefPtr<nsFontMetrics> fm;
620 nsLayoutUtils::GetFontMetricsForFrame(this, getter_AddRefs(fm),
621 fontSizeInflation);
622 nscoord axisHeight, height;
623 GetAxisHeight(aRenderingContext, fm, axisHeight);
625 // get the leading to be left at the top and the bottom of the stretched char
626 // this seems more reliable than using fm->GetLeading() on suspicious fonts
627 nscoord em;
628 GetEmHeight(fm, em);
629 nscoord leading = NSToCoordRound(0.2f * em);
631 // Operators that are stretchy, or those that are to be centered
632 // to cater for fonts that are not math-aware, are handled by the MathMLChar
633 // ('form' is reset if stretch fails -- i.e., we don't bother to stretch next time)
634 bool useMathMLChar = UseMathMLChar();
636 nsBoundingMetrics charSize;
637 nsBoundingMetrics container = aDesiredStretchSize.mBoundingMetrics;
638 bool isVertical = false;
640 if (((aStretchDirection == NS_STRETCH_DIRECTION_VERTICAL) ||
641 (aStretchDirection == NS_STRETCH_DIRECTION_DEFAULT)) &&
642 (mEmbellishData.direction == NS_STRETCH_DIRECTION_VERTICAL)) {
643 isVertical = true;
646 uint32_t stretchHint =
647 GetStretchHint(mFlags, mPresentationData, isVertical, StyleFont());
649 if (useMathMLChar) {
650 nsBoundingMetrics initialSize = aDesiredStretchSize.mBoundingMetrics;
652 if (stretchHint != NS_STRETCH_NONE) {
654 container = aContainerSize;
656 // some adjustments if the operator is symmetric and vertical
658 if (isVertical && NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags)) {
659 // we need to center about the axis
660 nscoord delta = std::max(container.ascent - axisHeight,
661 container.descent + axisHeight);
662 container.ascent = delta + axisHeight;
663 container.descent = delta - axisHeight;
665 // get ready in case we encounter user-desired min-max size
666 delta = std::max(initialSize.ascent - axisHeight,
667 initialSize.descent + axisHeight);
668 initialSize.ascent = delta + axisHeight;
669 initialSize.descent = delta - axisHeight;
672 // check for user-desired min-max size
674 if (mMaxSize != NS_MATHML_OPERATOR_SIZE_INFINITY && mMaxSize > 0.0f) {
675 // if we are here, there is a user defined maxsize ...
676 //XXX Set stretchHint = NS_STRETCH_NORMAL? to honor the maxsize as close as possible?
677 if (NS_MATHML_OPERATOR_MAXSIZE_IS_ABSOLUTE(mFlags)) {
678 // there is an explicit value like maxsize="20pt"
679 // try to maintain the aspect ratio of the char
680 float aspect = mMaxSize / float(initialSize.ascent + initialSize.descent);
681 container.ascent =
682 std::min(container.ascent, nscoord(initialSize.ascent * aspect));
683 container.descent =
684 std::min(container.descent, nscoord(initialSize.descent * aspect));
685 // below we use a type cast instead of a conversion to avoid a VC++ bug
686 // see http://support.microsoft.com/support/kb/articles/Q115/7/05.ASP
687 container.width =
688 std::min(container.width, (nscoord)mMaxSize);
690 else { // multiplicative value
691 container.ascent =
692 std::min(container.ascent, nscoord(initialSize.ascent * mMaxSize));
693 container.descent =
694 std::min(container.descent, nscoord(initialSize.descent * mMaxSize));
695 container.width =
696 std::min(container.width, nscoord(initialSize.width * mMaxSize));
699 if (isVertical && !NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags)) {
700 // re-adjust to align the char with the bottom of the initial container
701 height = container.ascent + container.descent;
702 container.descent = aContainerSize.descent;
703 container.ascent = height - container.descent;
707 if (mMinSize > 0.0f) {
708 // if we are here, there is a user defined minsize ...
709 // always allow the char to stretch in its natural direction,
710 // even if it is different from the caller's direction
711 if (aStretchDirection != NS_STRETCH_DIRECTION_DEFAULT &&
712 aStretchDirection != mEmbellishData.direction) {
713 aStretchDirection = NS_STRETCH_DIRECTION_DEFAULT;
714 // but when we are not honoring the requested direction
715 // we should not use the caller's container size either
716 container = initialSize;
718 if (NS_MATHML_OPERATOR_MINSIZE_IS_ABSOLUTE(mFlags)) {
719 // there is an explicit value like minsize="20pt"
720 // try to maintain the aspect ratio of the char
721 float aspect = mMinSize / float(initialSize.ascent + initialSize.descent);
722 container.ascent =
723 std::max(container.ascent, nscoord(initialSize.ascent * aspect));
724 container.descent =
725 std::max(container.descent, nscoord(initialSize.descent * aspect));
726 container.width =
727 std::max(container.width, (nscoord)mMinSize);
729 else { // multiplicative value
730 container.ascent =
731 std::max(container.ascent, nscoord(initialSize.ascent * mMinSize));
732 container.descent =
733 std::max(container.descent, nscoord(initialSize.descent * mMinSize));
734 container.width =
735 std::max(container.width, nscoord(initialSize.width * mMinSize));
738 if (isVertical && !NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags)) {
739 // re-adjust to align the char with the bottom of the initial container
740 height = container.ascent + container.descent;
741 container.descent = aContainerSize.descent;
742 container.ascent = height - container.descent;
747 // let the MathMLChar stretch itself...
748 nsresult res = mMathMLChar.Stretch(PresContext(), aRenderingContext,
749 fontSizeInflation,
750 aStretchDirection, container, charSize,
751 stretchHint,
752 StyleVisibility()->mDirection);
753 if (NS_FAILED(res)) {
754 // gracefully handle cases where stretching the char failed (i.e., GetBoundingMetrics failed)
755 // clear our 'form' to behave as if the operator wasn't in the dictionary
756 mFlags &= ~NS_MATHML_OPERATOR_FORM;
757 useMathMLChar = false;
761 // Place our children using the default method
762 // This will allow our child text frame to get its DidReflow()
763 nsresult rv = Place(aRenderingContext, true, aDesiredStretchSize);
764 if (NS_MATHML_HAS_ERROR(mPresentationData.flags) || NS_FAILED(rv)) {
765 // Make sure the child frames get their DidReflow() calls.
766 DidReflowChildren(mFrames.FirstChild());
769 if (useMathMLChar) {
770 // update our bounding metrics... it becomes that of our MathML char
771 mBoundingMetrics = charSize;
773 // if the returned direction is 'unsupported', the char didn't actually change.
774 // So we do the centering only if necessary
775 if (mMathMLChar.GetStretchDirection() != NS_STRETCH_DIRECTION_UNSUPPORTED ||
776 NS_MATHML_OPERATOR_IS_CENTERED(mFlags)) {
778 bool largeopOnly =
779 (NS_STRETCH_LARGEOP & stretchHint) != 0 &&
780 (NS_STRETCH_VARIABLE_MASK & stretchHint) == 0;
782 if (isVertical || NS_MATHML_OPERATOR_IS_CENTERED(mFlags)) {
783 // the desired size returned by mMathMLChar maybe different
784 // from the size of the container.
785 // the mMathMLChar.mRect.y calculation is subtle, watch out!!!
787 height = mBoundingMetrics.ascent + mBoundingMetrics.descent;
788 if (NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags) ||
789 NS_MATHML_OPERATOR_IS_CENTERED(mFlags)) {
790 // For symmetric and vertical operators, or for operators that are always
791 // centered ('+', '*', etc) we want to center about the axis of the container
792 mBoundingMetrics.descent = height/2 - axisHeight;
793 } else if (!largeopOnly) {
794 // Align the center of the char with the center of the container
795 mBoundingMetrics.descent = height/2 +
796 (container.ascent + container.descent)/2 - container.ascent;
797 } // else align the baselines
798 mBoundingMetrics.ascent = height - mBoundingMetrics.descent;
803 // Fixup for the final height.
804 // On one hand, our stretchy height can sometimes be shorter than surrounding
805 // ASCII chars, e.g., arrow symbols have |mBoundingMetrics.ascent + leading|
806 // that is smaller than the ASCII's ascent, hence when painting the background
807 // later, it won't look uniform along the line.
808 // On the other hand, sometimes we may leave too much gap when our glyph happens
809 // to come from a font with tall glyphs. For example, since CMEX10 has very tall
810 // glyphs, its natural font metrics are large, even if we pick a small glyph
811 // whose size is comparable to the size of a normal ASCII glyph.
812 // So to avoid uneven spacing in either of these two cases, we use the height
813 // of the ASCII font as a reference and try to match it if possible.
815 // special case for accents... keep them short to improve mouse operations...
816 // an accent can only be the non-first child of <mover>, <munder>, <munderover>
817 bool isAccent =
818 NS_MATHML_EMBELLISH_IS_ACCENT(mEmbellishData.flags);
819 if (isAccent) {
820 nsEmbellishData parentData;
821 GetEmbellishDataFrom(GetParent(), parentData);
822 isAccent =
823 (NS_MATHML_EMBELLISH_IS_ACCENTOVER(parentData.flags) ||
824 NS_MATHML_EMBELLISH_IS_ACCENTUNDER(parentData.flags)) &&
825 parentData.coreFrame != this;
827 if (isAccent && firstChild) {
828 // see bug 188467 for what is going on here
829 nscoord dy = aDesiredStretchSize.BlockStartAscent() -
830 (mBoundingMetrics.ascent + leading);
831 aDesiredStretchSize.SetBlockStartAscent(mBoundingMetrics.ascent + leading);
832 aDesiredStretchSize.Height() = aDesiredStretchSize.BlockStartAscent() +
833 mBoundingMetrics.descent;
835 firstChild->SetPosition(firstChild->GetPosition() - nsPoint(0, dy));
837 else if (useMathMLChar) {
838 nscoord ascent = fm->MaxAscent();
839 nscoord descent = fm->MaxDescent();
840 aDesiredStretchSize.SetBlockStartAscent(std::max(mBoundingMetrics.ascent + leading, ascent));
841 aDesiredStretchSize.Height() = aDesiredStretchSize.BlockStartAscent() +
842 std::max(mBoundingMetrics.descent + leading, descent);
844 aDesiredStretchSize.Width() = mBoundingMetrics.width;
845 aDesiredStretchSize.mBoundingMetrics = mBoundingMetrics;
846 mReference.x = 0;
847 mReference.y = aDesiredStretchSize.BlockStartAscent();
848 // Place our mMathMLChar, its origin is in our coordinate system
849 if (useMathMLChar) {
850 nscoord dy = aDesiredStretchSize.BlockStartAscent() - mBoundingMetrics.ascent;
851 mMathMLChar.SetRect(nsRect(0, dy, charSize.width, charSize.ascent + charSize.descent));
854 // Before we leave... there is a last item in the check-list:
855 // If our parent is not embellished, it means we are the outermost embellished
856 // container and so we put the spacing, otherwise we don't include the spacing,
857 // the outermost embellished container will take care of it.
859 if (!NS_MATHML_OPERATOR_HAS_EMBELLISH_ANCESTOR(mFlags)) {
861 // Account the spacing if we are not an accent with explicit attributes
862 nscoord leadingSpace = mEmbellishData.leadingSpace;
863 if (isAccent && !NS_MATHML_OPERATOR_HAS_LSPACE_ATTR(mFlags)) {
864 leadingSpace = 0;
866 nscoord trailingSpace = mEmbellishData.trailingSpace;
867 if (isAccent && !NS_MATHML_OPERATOR_HAS_RSPACE_ATTR(mFlags)) {
868 trailingSpace = 0;
871 mBoundingMetrics.width += leadingSpace + trailingSpace;
872 aDesiredStretchSize.Width() = mBoundingMetrics.width;
873 aDesiredStretchSize.mBoundingMetrics.width = mBoundingMetrics.width;
875 nscoord dx = (StyleVisibility()->mDirection ?
876 trailingSpace : leadingSpace);
877 if (dx) {
878 // adjust the offsets
879 mBoundingMetrics.leftBearing += dx;
880 mBoundingMetrics.rightBearing += dx;
881 aDesiredStretchSize.mBoundingMetrics.leftBearing += dx;
882 aDesiredStretchSize.mBoundingMetrics.rightBearing += dx;
884 if (useMathMLChar) {
885 nsRect rect;
886 mMathMLChar.GetRect(rect);
887 mMathMLChar.SetRect(nsRect(rect.x + dx, rect.y,
888 rect.width, rect.height));
890 else {
891 nsIFrame* childFrame = firstChild;
892 while (childFrame) {
893 childFrame->SetPosition(childFrame->GetPosition() +
894 nsPoint(dx, 0));
895 childFrame = childFrame->GetNextSibling();
901 // Finished with these:
902 ClearSavedChildMetrics();
903 // Set our overflow area
904 GatherAndStoreOverflow(&aDesiredStretchSize);
906 // There used to be code here to change the height of the child frame to
907 // change the caret height, but the text frame that manages the caret is now
908 // not a direct child but wrapped in a block frame. See also bug 412033.
910 return NS_OK;
913 NS_IMETHODIMP
914 nsMathMLmoFrame::InheritAutomaticData(nsIFrame* aParent)
916 // retain our native direction, it only changes if our text content changes
917 nsStretchDirection direction = mEmbellishData.direction;
918 nsMathMLTokenFrame::InheritAutomaticData(aParent);
919 ProcessTextData();
920 mEmbellishData.direction = direction;
921 return NS_OK;
924 NS_IMETHODIMP
925 nsMathMLmoFrame::TransmitAutomaticData()
927 // this will cause us to re-sync our flags from scratch
928 // but our returned 'form' is still not final (bug 133429), it will
929 // be recomputed to its final value during the next call in Reflow()
930 mEmbellishData.coreFrame = nullptr;
931 ProcessOperatorData();
932 return NS_OK;
935 void
936 nsMathMLmoFrame::SetInitialChildList(ChildListID aListID,
937 nsFrameList& aChildList)
939 // First, let the parent class do its work
940 nsMathMLTokenFrame::SetInitialChildList(aListID, aChildList);
941 ProcessTextData();
944 void
945 nsMathMLmoFrame::Reflow(nsPresContext* aPresContext,
946 nsHTMLReflowMetrics& aDesiredSize,
947 const nsHTMLReflowState& aReflowState,
948 nsReflowStatus& aStatus)
950 // certain values use units that depend on our style context, so
951 // it is safer to just process the whole lot here
952 ProcessOperatorData();
954 nsMathMLTokenFrame::Reflow(aPresContext, aDesiredSize,
955 aReflowState, aStatus);
958 nsresult
959 nsMathMLmoFrame::Place(nsRenderingContext& aRenderingContext,
960 bool aPlaceOrigin,
961 nsHTMLReflowMetrics& aDesiredSize)
963 nsresult rv = nsMathMLTokenFrame::Place(aRenderingContext, aPlaceOrigin,
964 aDesiredSize);
966 if (NS_FAILED(rv)) {
967 return rv;
970 /* Special behaviour for largeops.
971 In MathML "stretchy" and displaystyle "largeop" are different notions,
972 even if we use the same technique to draw them (picking size variants).
973 So largeop display operators should be considered "non-stretchy" and
974 thus their sizes should be taken into account for the stretch size of
975 other elements.
977 This is a preliminary stretch - exact sizing/placement is handled by the
978 Stretch() method.
981 if (!aPlaceOrigin &&
982 StyleFont()->mMathDisplay == NS_MATHML_DISPLAYSTYLE_BLOCK &&
983 NS_MATHML_OPERATOR_IS_LARGEOP(mFlags) && UseMathMLChar()) {
984 nsBoundingMetrics newMetrics;
985 rv = mMathMLChar.Stretch(PresContext(), aRenderingContext,
986 nsLayoutUtils::FontSizeInflationFor(this),
987 NS_STRETCH_DIRECTION_VERTICAL,
988 aDesiredSize.mBoundingMetrics, newMetrics,
989 NS_STRETCH_LARGEOP, StyleVisibility()->mDirection);
991 if (NS_FAILED(rv)) {
992 // Just use the initial size
993 return NS_OK;
996 aDesiredSize.mBoundingMetrics = newMetrics;
997 /* Treat the ascent/descent values calculated in the TokenFrame place
998 calculations as the minimum for aDesiredSize calculations, rather
999 than fetching them from font metrics again.
1001 aDesiredSize.SetBlockStartAscent(std::max(mBoundingMetrics.ascent,
1002 newMetrics.ascent));
1003 aDesiredSize.Height() = aDesiredSize.BlockStartAscent() +
1004 std::max(mBoundingMetrics.descent,
1005 newMetrics.descent);
1006 aDesiredSize.Width() = newMetrics.width;
1007 mBoundingMetrics = newMetrics;
1009 return NS_OK;
1012 /* virtual */ void
1013 nsMathMLmoFrame::MarkIntrinsicISizesDirty()
1015 // if we get this, it may mean that something changed in the text
1016 // content. So blow away everything an re-build the automatic data
1017 // from the parent of our outermost embellished container (we ensure
1018 // that we are the core, not just a sibling of the core)
1020 ProcessTextData();
1022 nsIFrame* target = this;
1023 nsEmbellishData embellishData;
1024 do {
1025 target = target->GetParent();
1026 GetEmbellishDataFrom(target, embellishData);
1027 } while (embellishData.coreFrame == this);
1029 // we have automatic data to update in the children of the target frame
1030 // XXXldb This should really be marking dirty rather than rebuilding
1031 // so that we don't rebuild multiple times for the same change.
1032 RebuildAutomaticDataForChildren(target);
1034 nsMathMLContainerFrame::MarkIntrinsicISizesDirty();
1037 /* virtual */ void
1038 nsMathMLmoFrame::GetIntrinsicISizeMetrics(nsRenderingContext *aRenderingContext, nsHTMLReflowMetrics& aDesiredSize)
1040 ProcessOperatorData();
1041 if (UseMathMLChar()) {
1042 uint32_t stretchHint = GetStretchHint(mFlags, mPresentationData, true,
1043 StyleFont());
1044 aDesiredSize.Width() = mMathMLChar.
1045 GetMaxWidth(PresContext(), *aRenderingContext,
1046 nsLayoutUtils::FontSizeInflationFor(this),
1047 stretchHint, mMaxSize,
1048 NS_MATHML_OPERATOR_MAXSIZE_IS_ABSOLUTE(mFlags));
1050 else {
1051 nsMathMLTokenFrame::GetIntrinsicISizeMetrics(aRenderingContext,
1052 aDesiredSize);
1055 // leadingSpace and trailingSpace are actually applied to the outermost
1056 // embellished container but for determining total intrinsic width it should
1057 // be safe to include it for the core here instead.
1058 bool isRTL = StyleVisibility()->mDirection;
1059 aDesiredSize.Width() +=
1060 mEmbellishData.leadingSpace + mEmbellishData.trailingSpace;
1061 aDesiredSize.mBoundingMetrics.width = aDesiredSize.Width();
1062 if (isRTL) {
1063 aDesiredSize.mBoundingMetrics.leftBearing += mEmbellishData.trailingSpace;
1064 aDesiredSize.mBoundingMetrics.rightBearing += mEmbellishData.trailingSpace;
1065 } else {
1066 aDesiredSize.mBoundingMetrics.leftBearing += mEmbellishData.leadingSpace;
1067 aDesiredSize.mBoundingMetrics.rightBearing += mEmbellishData.leadingSpace;
1071 nsresult
1072 nsMathMLmoFrame::AttributeChanged(int32_t aNameSpaceID,
1073 nsIAtom* aAttribute,
1074 int32_t aModType)
1076 // check if this is an attribute that can affect the embellished hierarchy
1077 // in a significant way and re-layout the entire hierarchy.
1078 if (nsGkAtoms::accent_ == aAttribute ||
1079 nsGkAtoms::movablelimits_ == aAttribute) {
1081 // set the target as the parent of our outermost embellished container
1082 // (we ensure that we are the core, not just a sibling of the core)
1083 nsIFrame* target = this;
1084 nsEmbellishData embellishData;
1085 do {
1086 target = target->GetParent();
1087 GetEmbellishDataFrom(target, embellishData);
1088 } while (embellishData.coreFrame == this);
1090 // we have automatic data to update in the children of the target frame
1091 return ReLayoutChildren(target);
1094 return nsMathMLTokenFrame::
1095 AttributeChanged(aNameSpaceID, aAttribute, aModType);
1098 // ----------------------
1099 // No need to track the style context given to our MathML char.
1100 // the Style System will use these to pass the proper style context to our MathMLChar
1101 nsStyleContext*
1102 nsMathMLmoFrame::GetAdditionalStyleContext(int32_t aIndex) const
1104 switch (aIndex) {
1105 case NS_MATHML_CHAR_STYLE_CONTEXT_INDEX:
1106 return mMathMLChar.GetStyleContext();
1107 default:
1108 return nullptr;
1112 void
1113 nsMathMLmoFrame::SetAdditionalStyleContext(int32_t aIndex,
1114 nsStyleContext* aStyleContext)
1116 switch (aIndex) {
1117 case NS_MATHML_CHAR_STYLE_CONTEXT_INDEX:
1118 mMathMLChar.SetStyleContext(aStyleContext);
1119 break;