1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla MathML Project.
17 * The Initial Developer of the Original Code is
18 * The University Of Queensland.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
23 * Roger B. Sidje <rbs@maths.uq.edu.au>
24 * David J. Fiddes <D.J.Fiddes@hw.ac.uk>
25 * Shyjan Mahamud <mahamud@cs.cmu.edu>
26 * Pierre Phaneuf <pp@ludusdesign.com>
28 * Alternatively, the contents of this file may be used under the terms of
29 * either of the GNU General Public License Version 2 or later (the "GPL"),
30 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
44 #include "nsPresContext.h"
45 #include "nsStyleContext.h"
46 #include "nsStyleConsts.h"
47 #include "nsIRenderingContext.h"
48 #include "nsIFontMetrics.h"
49 #include "nsContentUtils.h"
51 #include "nsIDOMText.h"
53 #include "nsMathMLmoFrame.h"
56 // <mo> -- operator, fence, or separator - implementation
59 // additional style context to be used by our MathMLChar.
60 #define NS_MATHML_CHAR_STYLE_CONTEXT_INDEX 0
63 NS_NewMathMLmoFrame(nsIPresShell
* aPresShell
, nsStyleContext
*aContext
)
65 return new (aPresShell
) nsMathMLmoFrame(aContext
);
68 nsMathMLmoFrame::~nsMathMLmoFrame()
72 static const PRUnichar kInvisibleComma
= PRUnichar(0x200B); // a.k.a. ZERO WIDTH SPACE
73 static const PRUnichar kApplyFunction
= PRUnichar(0x2061);
74 static const PRUnichar kInvisibleTimes
= PRUnichar(0x2062);
75 static const PRUnichar kNullCh
= PRUnichar('\0');
78 nsMathMLmoFrame::GetMathMLFrameType()
80 return NS_MATHML_OPERATOR_IS_INVISIBLE(mFlags
)
81 ? eMathMLFrameType_OperatorInvisible
82 : eMathMLFrameType_OperatorOrdinary
;
85 // since a mouse click implies selection, we cannot just rely on the
86 // frame's state bit in our child text frame. So we will first check
87 // its selected state bit, and use this little helper to double check.
89 nsMathMLmoFrame::IsFrameInSelection(nsIFrame
* aFrame
)
91 NS_ASSERTION(aFrame
, "null arg");
95 PRBool isSelected
= PR_FALSE
;
96 aFrame
->GetSelected(&isSelected
);
100 const nsFrameSelection
* frameSelection
= aFrame
->GetConstFrameSelection();
101 SelectionDetails
* details
=
102 frameSelection
->LookUpSelection(aFrame
->GetContent(), 0, 1, PR_TRUE
);
108 SelectionDetails
* next
= details
->mNext
;
116 nsMathMLmoFrame::UseMathMLChar()
118 return (NS_MATHML_OPERATOR_GET_FORM(mFlags
) &&
119 NS_MATHML_OPERATOR_IS_MUTABLE(mFlags
)) ||
120 NS_MATHML_OPERATOR_IS_CENTERED(mFlags
) ||
121 NS_MATHML_OPERATOR_IS_INVISIBLE(mFlags
);
125 nsMathMLmoFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
126 const nsRect
& aDirtyRect
,
127 const nsDisplayListSet
& aLists
)
130 PRBool useMathMLChar
= UseMathMLChar();
132 if (!useMathMLChar
) {
133 // let the base class do everything
134 rv
= nsMathMLTokenFrame::BuildDisplayList(aBuilder
, aDirtyRect
, aLists
);
135 NS_ENSURE_SUCCESS(rv
, rv
);
137 rv
= DisplayBorderBackgroundOutline(aBuilder
, aLists
);
138 NS_ENSURE_SUCCESS(rv
, rv
);
140 // make our char selected if our inner child text frame is selected
141 PRBool isSelected
= PR_FALSE
;
143 nsIFrame
* firstChild
= mFrames
.FirstChild();
144 if (IsFrameInSelection(firstChild
)) {
145 selectedRect
= firstChild
->GetRect();
146 isSelected
= PR_TRUE
;
148 rv
= mMathMLChar
.Display(aBuilder
, this, aLists
, isSelected
? &selectedRect
: nsnull
);
149 NS_ENSURE_SUCCESS(rv
, rv
);
151 #if defined(NS_DEBUG) && defined(SHOW_BOUNDING_BOX)
153 rv
= DisplayBoundingMetrics(aBuilder
, this, mReference
, mBoundingMetrics
, aLists
);
159 // get the text that we enclose and setup our nsMathMLChar
161 nsMathMLmoFrame::ProcessTextData()
166 nsContentUtils::GetNodeTextContent(mContent
, PR_FALSE
, data
);
167 PRInt32 length
= data
.Length();
168 PRUnichar ch
= (length
== 0) ? kNullCh
: data
[0];
171 (ch
== kInvisibleComma
||
172 ch
== kApplyFunction
||
173 ch
== kInvisibleTimes
)) {
174 mFlags
|= NS_MATHML_OPERATOR_INVISIBLE
;
177 // don't bother doing anything special if we don't have a
178 // single child with a visible text content
179 nsPresContext
* presContext
= PresContext();
180 if (NS_MATHML_OPERATOR_IS_INVISIBLE(mFlags
) || mFrames
.GetLength() != 1) {
181 data
.Truncate(); // empty data to reset the char
182 mMathMLChar
.SetData(presContext
, data
);
183 ResolveMathMLCharStyle(presContext
, mContent
, mStyleContext
, &mMathMLChar
, PR_FALSE
);
187 // special... in math mode, the usual minus sign '-' looks too short, so
188 // what we do here is to remap <mo>-</mo> to the official Unicode minus
189 // sign (U+2212) which looks much better. For background on this, see
190 // http://groups.google.com/groups?hl=en&th=66488daf1ade7635&rnum=1
191 if (1 == length
&& ch
== '-') {
196 // cache the special bits: mutable, accent, movablelimits, centered.
197 // we need to do this in anticipation of other requirements, and these
198 // bits don't change. Do not reset these bits unless the text gets changed.
200 // lookup all the forms under which the operator is listed in the dictionary,
201 // and record whether the operator has accent="true" or movablelimits="true"
202 nsOperatorFlags flags
[4];
203 float lspace
[4], rspace
[4];
204 nsMathMLOperators::LookupOperators(data
, flags
, lspace
, rspace
);
205 nsOperatorFlags allFlags
=
206 flags
[NS_MATHML_OPERATOR_FORM_INFIX
] |
207 flags
[NS_MATHML_OPERATOR_FORM_POSTFIX
] |
208 flags
[NS_MATHML_OPERATOR_FORM_PREFIX
];
210 mFlags
|= allFlags
& NS_MATHML_OPERATOR_ACCENT
;
211 mFlags
|= allFlags
& NS_MATHML_OPERATOR_MOVABLELIMITS
;
214 NS_MATHML_OPERATOR_IS_STRETCHY(allFlags
) ||
215 NS_MATHML_OPERATOR_IS_LARGEOP(allFlags
);
217 mFlags
|= NS_MATHML_OPERATOR_MUTABLE
;
219 // see if this is an operator that should be centered to cater for
220 // fonts that are not math-aware
222 if ((ch
== '+') || (ch
== '=') || (ch
== '*') ||
223 (ch
== 0x2212) || // −
224 (ch
== 0x2264) || // ≤
225 (ch
== 0x2265) || // ≥
226 (ch
== 0x00D7)) { // ×
227 mFlags
|= NS_MATHML_OPERATOR_CENTERED
;
231 // cache the operator
232 mMathMLChar
.SetData(presContext
, data
);
233 ResolveMathMLCharStyle(presContext
, mContent
, mStyleContext
, &mMathMLChar
, isMutable
);
235 // cache the native direction -- beware of bug 133429...
236 // mEmbellishData.direction must always retain our native direction, whereas
237 // mMathMLChar.GetStretchDirection() may change later, when Stretch() is called
238 mEmbellishData
.direction
= mMathMLChar
.GetStretchDirection();
241 // get our 'form' and lookup in the Operator Dictionary to fetch
242 // our default data that may come from there. Then complete our setup
243 // using attributes that we may have. To stay in sync, this function is
244 // called very often. We depend on many things that may change around us.
245 // However, we re-use unchanged values.
247 nsMathMLmoFrame::ProcessOperatorData()
249 // if we have been here before, we will just use our cached form
250 nsOperatorFlags form
= NS_MATHML_OPERATOR_GET_FORM(mFlags
);
253 // special bits are always kept in mFlags.
254 // remember the mutable bit from ProcessTextData().
255 // Some chars are listed under different forms in the dictionary,
256 // and there could be a form under which the char is mutable.
257 // If the char is the core of an embellished container, we will keep
258 // it mutable irrespective of the form of the embellished container.
259 // Also remember the other special bits that we want to carry forward.
260 mFlags
&= NS_MATHML_OPERATOR_MUTABLE
|
261 NS_MATHML_OPERATOR_ACCENT
|
262 NS_MATHML_OPERATOR_MOVABLELIMITS
|
263 NS_MATHML_OPERATOR_CENTERED
|
264 NS_MATHML_OPERATOR_INVISIBLE
;
266 if (!mEmbellishData
.coreFrame
) {
267 // i.e., we haven't been here before, the default form is infix
268 form
= NS_MATHML_OPERATOR_FORM_INFIX
;
270 // reset everything so that we don't keep outdated values around
271 // in case of dynamic changes
272 mEmbellishData
.flags
= 0;
273 mEmbellishData
.coreFrame
= nsnull
;
274 mEmbellishData
.leftSpace
= 0;
275 mEmbellishData
.rightSpace
= 0;
276 if (mMathMLChar
.Length() != 1)
277 mEmbellishData
.direction
= NS_STRETCH_DIRECTION_UNSUPPORTED
;
278 // else... retain the native direction obtained in ProcessTextData()
280 if (!mFrames
.FirstChild()) {
284 mEmbellishData
.flags
|= NS_MATHML_EMBELLISH_OPERATOR
;
285 mEmbellishData
.coreFrame
= this;
287 // there are two particular things that we also need to record so that if our
288 // parent is <mover>, <munder>, or <munderover>, they will treat us properly:
289 // 1) do we have accent="true"
290 // 2) do we have movablelimits="true"
292 // they need the extra information to decide how to treat their scripts/limits
293 // (note: <mover>, <munder>, or <munderover> need not necessarily be our
294 // direct parent -- case of embellished operators)
296 // default values from the Operator Dictionary were obtained in ProcessTextData()
297 // and these special bits are always kept in mFlags
298 if (NS_MATHML_OPERATOR_IS_ACCENT(mFlags
))
299 mEmbellishData
.flags
|= NS_MATHML_EMBELLISH_ACCENT
;
300 if (NS_MATHML_OPERATOR_IS_MOVABLELIMITS(mFlags
))
301 mEmbellishData
.flags
|= NS_MATHML_EMBELLISH_MOVABLELIMITS
;
303 // see if the accent attribute is there
304 GetAttribute(mContent
, mPresentationData
.mstyle
, nsGkAtoms::accent_
,
306 if (value
.EqualsLiteral("true"))
307 mEmbellishData
.flags
|= NS_MATHML_EMBELLISH_ACCENT
;
308 else if (value
.EqualsLiteral("false"))
309 mEmbellishData
.flags
&= ~NS_MATHML_EMBELLISH_ACCENT
;
311 // see if the movablelimits attribute is there
312 GetAttribute(mContent
, mPresentationData
.mstyle
,
313 nsGkAtoms::movablelimits_
, value
);
314 if (value
.EqualsLiteral("true"))
315 mEmbellishData
.flags
|= NS_MATHML_EMBELLISH_MOVABLELIMITS
;
316 else if (value
.EqualsLiteral("false"))
317 mEmbellishData
.flags
&= ~NS_MATHML_EMBELLISH_MOVABLELIMITS
;
319 // ---------------------------------------------------------------------
320 // we will be called again to re-sync the rest of our state next time...
321 // (nobody needs the other values below at this stage)
326 nsPresContext
* presContext
= PresContext();
328 // beware of bug 133814 - there is a two-way dependency in the
329 // embellished hierarchy: our embellished ancestors need to set
330 // their flags based on some of our state (set above), and here we
331 // need to re-sync our 'form' depending on our outermost embellished
332 // container. A null form here means that an earlier attempt to stretch
333 // our mMathMLChar failed, in which case we don't bother re-stretching again
335 // get our outermost embellished container and its parent.
336 // (we ensure that we are the core, not just a sibling of the core)
337 nsIFrame
* embellishAncestor
= this;
338 nsEmbellishData embellishData
;
339 nsIFrame
* parentAncestor
= this;
341 embellishAncestor
= parentAncestor
;
342 parentAncestor
= embellishAncestor
->GetParent();
343 GetEmbellishDataFrom(parentAncestor
, embellishData
);
344 } while (embellishData
.coreFrame
== this);
346 // flag if we have an embellished ancestor
347 if (embellishAncestor
!= this)
348 mFlags
|= NS_MATHML_OPERATOR_EMBELLISH_ANCESTOR
;
350 mFlags
&= ~NS_MATHML_OPERATOR_EMBELLISH_ANCESTOR
;
352 // find the position of our outermost embellished container w.r.t
353 // its siblings (frames are singly-linked together).
354 nsFrameList
frameList(parentAncestor
->GetFirstChild(nsnull
));
356 nsIFrame
* nextSibling
= embellishAncestor
->GetNextSibling();
357 nsIFrame
* prevSibling
= frameList
.GetPrevSiblingFor(embellishAncestor
);
359 // flag to distinguish from a real infix
360 if (!prevSibling
&& !nextSibling
)
361 mFlags
|= NS_MATHML_OPERATOR_EMBELLISH_ISOLATED
;
363 mFlags
&= ~NS_MATHML_OPERATOR_EMBELLISH_ISOLATED
;
366 form
= NS_MATHML_OPERATOR_FORM_INFIX
;
367 GetAttribute(mContent
, mPresentationData
.mstyle
, nsGkAtoms::form
,
369 if (!value
.IsEmpty()) {
370 if (value
.EqualsLiteral("prefix"))
371 form
= NS_MATHML_OPERATOR_FORM_PREFIX
;
372 else if (value
.EqualsLiteral("postfix"))
373 form
= NS_MATHML_OPERATOR_FORM_POSTFIX
;
376 // set our form flag depending on the position
377 if (!prevSibling
&& nextSibling
)
378 form
= NS_MATHML_OPERATOR_FORM_PREFIX
;
379 else if (prevSibling
&& !nextSibling
)
380 form
= NS_MATHML_OPERATOR_FORM_POSTFIX
;
382 mFlags
&= ~NS_MATHML_OPERATOR_FORM
; // clear the old form bits
385 // lookup the operator dictionary
389 mMathMLChar
.GetData(data
);
390 PRBool found
= nsMathMLOperators::LookupOperator(data
, form
, &mFlags
, &lspace
, &rspace
);
391 if (found
&& (lspace
|| rspace
)) {
392 // cache the default values of lspace & rspace that we get from the dictionary.
393 // since these values are relative to the 'em' unit, convert to twips now
395 nsCOMPtr
<nsIFontMetrics
> fm
=
396 presContext
->GetMetricsFor(GetStyleFont()->mFont
);
399 mEmbellishData
.leftSpace
= NSToCoordRound(lspace
* em
);
400 mEmbellishData
.rightSpace
= NSToCoordRound(rspace
* em
);
402 // tuning if we don't want too much extra space when we are a script.
403 // (with its fonts, TeX sets lspace=0 & rspace=0 as soon as scriptlevel>0.
404 // Our fonts can be anything, so...)
405 if (GetStyleFont()->mScriptLevel
> 0) {
406 if (NS_MATHML_OPERATOR_EMBELLISH_IS_ISOLATED(mFlags
)) {
407 // could be an isolated accent or script, e.g., x^{+}, just zero out
408 mEmbellishData
.leftSpace
= 0;
409 mEmbellishData
.rightSpace
= 0;
411 else if (!NS_MATHML_OPERATOR_HAS_EMBELLISH_ANCESTOR(mFlags
)) {
412 mEmbellishData
.leftSpace
/= 2;
413 mEmbellishData
.rightSpace
/= 2;
419 // If we are an accent without explicit lspace="." or rspace=".",
420 // we will ignore our default left/right space
422 // lspace = number h-unit | namedspace
423 nscoord leftSpace
= mEmbellishData
.leftSpace
;
424 GetAttribute(mContent
, mPresentationData
.mstyle
, nsGkAtoms::lspace_
,
426 if (!value
.IsEmpty()) {
428 if (ParseNumericValue(value
, cssValue
) ||
429 ParseNamedSpaceValue(mPresentationData
.mstyle
, value
, cssValue
))
431 if ((eCSSUnit_Number
== cssValue
.GetUnit()) && !cssValue
.GetFloatValue())
433 else if (cssValue
.IsLengthUnit())
434 leftSpace
= CalcLength(presContext
, mStyleContext
, cssValue
);
435 mFlags
|= NS_MATHML_OPERATOR_LEFTSPACE_ATTR
;
439 // rspace = number h-unit | namedspace
440 nscoord rightSpace
= mEmbellishData
.rightSpace
;
441 GetAttribute(mContent
, mPresentationData
.mstyle
, nsGkAtoms::rspace_
,
443 if (!value
.IsEmpty()) {
445 if (ParseNumericValue(value
, cssValue
) ||
446 ParseNamedSpaceValue(mPresentationData
.mstyle
, value
, cssValue
))
448 if ((eCSSUnit_Number
== cssValue
.GetUnit()) && !cssValue
.GetFloatValue())
450 else if (cssValue
.IsLengthUnit())
451 rightSpace
= CalcLength(presContext
, mStyleContext
, cssValue
);
452 mFlags
|= NS_MATHML_OPERATOR_RIGHTSPACE_ATTR
;
456 // little extra tuning to round lspace & rspace to at least a pixel so that
457 // operators don't look as if they are colliding with their operands
458 if (leftSpace
|| rightSpace
) {
459 nscoord onePixel
= nsPresContext::CSSPixelsToAppUnits(1);
460 if (leftSpace
&& leftSpace
< onePixel
)
461 leftSpace
= onePixel
;
462 if (rightSpace
&& rightSpace
< onePixel
)
463 rightSpace
= onePixel
;
466 // the values that we get from our attributes override the dictionary
467 mEmbellishData
.leftSpace
= leftSpace
;
468 mEmbellishData
.rightSpace
= rightSpace
;
470 // Now see if there are user-defined attributes that override the dictionary.
471 // XXX If an attribute can be forced to be true when it is false in the
472 // dictionary, then the following code has to change...
474 // For each attribute overriden by the user, turn off its bit flag.
475 // symmetric|movablelimits|separator|largeop|accent|fence|stretchy|form
476 // special: accent and movablelimits are handled above,
477 // don't process them here
479 if (NS_MATHML_OPERATOR_IS_STRETCHY(mFlags
)) {
480 GetAttribute(mContent
, mPresentationData
.mstyle
,
481 nsGkAtoms::stretchy_
, value
);
482 if (value
.EqualsLiteral("false"))
483 mFlags
&= ~NS_MATHML_OPERATOR_STRETCHY
;
485 if (NS_MATHML_OPERATOR_IS_FENCE(mFlags
)) {
486 GetAttribute(mContent
, mPresentationData
.mstyle
,
487 nsGkAtoms::fence_
, value
);
488 if (value
.EqualsLiteral("false"))
489 mFlags
&= ~NS_MATHML_OPERATOR_FENCE
;
491 if (NS_MATHML_OPERATOR_IS_LARGEOP(mFlags
)) {
492 GetAttribute(mContent
, mPresentationData
.mstyle
,
493 nsGkAtoms::largeop_
, value
);
494 if (value
.EqualsLiteral("false"))
495 mFlags
&= ~NS_MATHML_OPERATOR_LARGEOP
;
497 if (NS_MATHML_OPERATOR_IS_SEPARATOR(mFlags
)) {
498 GetAttribute(mContent
, mPresentationData
.mstyle
,
499 nsGkAtoms::separator_
, value
);
500 if (value
.EqualsLiteral("false"))
501 mFlags
&= ~NS_MATHML_OPERATOR_SEPARATOR
;
503 GetAttribute(mContent
, mPresentationData
.mstyle
, nsGkAtoms::symmetric_
,
505 if (value
.EqualsLiteral("false"))
506 mFlags
&= ~NS_MATHML_OPERATOR_SYMMETRIC
;
507 else if (value
.EqualsLiteral("true"))
508 mFlags
|= NS_MATHML_OPERATOR_SYMMETRIC
;
510 // minsize = number [ v-unit | h-unit ] | namedspace
512 GetAttribute(mContent
, mPresentationData
.mstyle
, nsGkAtoms::minsize_
,
514 if (!value
.IsEmpty()) {
516 if (ParseNumericValue(value
, cssValue
) ||
517 ParseNamedSpaceValue(mPresentationData
.mstyle
, value
, cssValue
))
519 nsCSSUnit unit
= cssValue
.GetUnit();
520 if (eCSSUnit_Number
== unit
)
521 mMinSize
= cssValue
.GetFloatValue();
522 else if (eCSSUnit_Percent
== unit
)
523 mMinSize
= cssValue
.GetPercentValue();
524 else if (eCSSUnit_Null
!= unit
) {
525 mMinSize
= float(CalcLength(presContext
, mStyleContext
, cssValue
));
526 mFlags
|= NS_MATHML_OPERATOR_MINSIZE_ABSOLUTE
;
529 if ((eCSSUnit_Number
== unit
) || (eCSSUnit_Percent
== unit
)) {
530 // see if the multiplicative inheritance should be from <mstyle>
531 GetAttribute(nsnull
, mPresentationData
.mstyle
,
532 nsGkAtoms::minsize_
, value
);
533 if (!value
.IsEmpty()) {
534 if (ParseNumericValue(value
, cssValue
)) {
535 if (cssValue
.IsLengthUnit()) {
536 mMinSize
*= float(CalcLength(presContext
, mStyleContext
, cssValue
));
537 mFlags
|= NS_MATHML_OPERATOR_MINSIZE_ABSOLUTE
;
545 // maxsize = number [ v-unit | h-unit ] | namedspace | infinity
546 mMaxSize
= NS_MATHML_OPERATOR_SIZE_INFINITY
;
547 GetAttribute(mContent
, mPresentationData
.mstyle
, nsGkAtoms::maxsize_
,
549 if (!value
.IsEmpty()) {
551 if (ParseNumericValue(value
, cssValue
) ||
552 ParseNamedSpaceValue(mPresentationData
.mstyle
, value
, cssValue
))
554 nsCSSUnit unit
= cssValue
.GetUnit();
555 if (eCSSUnit_Number
== unit
)
556 mMaxSize
= cssValue
.GetFloatValue();
557 else if (eCSSUnit_Percent
== unit
)
558 mMaxSize
= cssValue
.GetPercentValue();
559 else if (eCSSUnit_Null
!= unit
) {
560 mMaxSize
= float(CalcLength(presContext
, mStyleContext
, cssValue
));
561 mFlags
|= NS_MATHML_OPERATOR_MAXSIZE_ABSOLUTE
;
564 if ((eCSSUnit_Number
== unit
) || (eCSSUnit_Percent
== unit
)) {
565 // see if the multiplicative inheritance should be from <mstyle>
566 GetAttribute(nsnull
, mPresentationData
.mstyle
,
567 nsGkAtoms::maxsize_
, value
);
568 if (!value
.IsEmpty()) {
569 if (ParseNumericValue(value
, cssValue
)) {
570 if (cssValue
.IsLengthUnit()) {
571 mMaxSize
*= float(CalcLength(presContext
, mStyleContext
, cssValue
));
572 mFlags
|= NS_MATHML_OPERATOR_MAXSIZE_ABSOLUTE
;
582 GetStretchHint(nsOperatorFlags aFlags
, nsPresentationData aPresentationData
,
585 PRUint32 stretchHint
= NS_STRETCH_NONE
;
586 // See if it is okay to stretch,
587 // starting from what the Operator Dictionary said
588 if (NS_MATHML_OPERATOR_IS_MUTABLE(aFlags
)) {
589 // set the largeop or largeopOnly flags to suitably cover all the
590 // 8 possible cases depending on whether displaystyle, largeop,
591 // stretchy are true or false (see bug 69325).
592 // . largeopOnly is taken if largeop=true and stretchy=false
593 // . largeop is taken if largeop=true and stretchy=true
594 if (NS_MATHML_IS_DISPLAYSTYLE(aPresentationData
.flags
) &&
595 NS_MATHML_OPERATOR_IS_LARGEOP(aFlags
)) {
596 stretchHint
= NS_STRETCH_LARGEOP
; // (largeopOnly, not mask!)
597 if (NS_MATHML_OPERATOR_IS_STRETCHY(aFlags
)) {
598 stretchHint
|= NS_STRETCH_NEARER
| NS_STRETCH_LARGER
;
601 else if(NS_MATHML_OPERATOR_IS_STRETCHY(aFlags
)) {
603 // TeX hint. Can impact some sloppy markups missing <mrow></mrow>
604 stretchHint
= NS_STRETCH_NEARER
;
607 stretchHint
= NS_STRETCH_NORMAL
;
610 // else if the stretchy and largeop attributes have been disabled,
611 // the operator is not mutable
616 // NOTE: aDesiredStretchSize is an IN/OUT parameter
617 // On input - it contains our current size
618 // On output - the same size or the new size that we want
620 nsMathMLmoFrame::Stretch(nsIRenderingContext
& aRenderingContext
,
621 nsStretchDirection aStretchDirection
,
622 nsBoundingMetrics
& aContainerSize
,
623 nsHTMLReflowMetrics
& aDesiredStretchSize
)
625 if (NS_MATHML_STRETCH_WAS_DONE(mPresentationData
.flags
)) {
626 NS_WARNING("it is wrong to fire stretch more than once on a frame");
629 mPresentationData
.flags
|= NS_MATHML_STRETCH_DONE
;
631 nsIFrame
* firstChild
= mFrames
.FirstChild();
633 // get the axis height;
634 nsCOMPtr
<nsIFontMetrics
> fm
;
635 aRenderingContext
.SetFont(GetStyleFont()->mFont
, nsnull
,
636 PresContext()->GetUserFontSet());
637 aRenderingContext
.GetFontMetrics(*getter_AddRefs(fm
));
638 nscoord axisHeight
, height
;
639 GetAxisHeight(aRenderingContext
, fm
, axisHeight
);
641 // get the leading to be left at the top and the bottom of the stretched char
642 // this seems more reliable than using fm->GetLeading() on suspicious fonts
645 nscoord leading
= NSToCoordRound(0.2f
* em
);
647 // Operators that are stretchy, or those that are to be centered
648 // to cater for fonts that are not math-aware, are handled by the MathMLChar
649 // ('form' is reset if stretch fails -- i.e., we don't bother to stretch next time)
650 PRBool useMathMLChar
= UseMathMLChar();
652 nsBoundingMetrics charSize
;
653 nsBoundingMetrics container
= aDesiredStretchSize
.mBoundingMetrics
;
654 PRBool isVertical
= PR_FALSE
;
656 nsBoundingMetrics initialSize
= aDesiredStretchSize
.mBoundingMetrics
;
658 if (((aStretchDirection
== NS_STRETCH_DIRECTION_VERTICAL
) ||
659 (aStretchDirection
== NS_STRETCH_DIRECTION_DEFAULT
)) &&
660 (mEmbellishData
.direction
== NS_STRETCH_DIRECTION_VERTICAL
)) {
661 isVertical
= PR_TRUE
;
664 PRUint32 stretchHint
=
665 GetStretchHint(mFlags
, mPresentationData
, isVertical
);
667 if (stretchHint
!= NS_STRETCH_NONE
) {
669 container
= aContainerSize
;
671 // some adjustments if the operator is symmetric and vertical
673 if (isVertical
&& NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags
)) {
674 // we need to center about the axis
675 nscoord delta
= PR_MAX(container
.ascent
- axisHeight
,
676 container
.descent
+ axisHeight
);
677 container
.ascent
= delta
+ axisHeight
;
678 container
.descent
= delta
- axisHeight
;
680 // get ready in case we encounter user-desired min-max size
681 delta
= PR_MAX(initialSize
.ascent
- axisHeight
,
682 initialSize
.descent
+ axisHeight
);
683 initialSize
.ascent
= delta
+ axisHeight
;
684 initialSize
.descent
= delta
- axisHeight
;
687 // check for user-desired min-max size
689 if (mMaxSize
!= NS_MATHML_OPERATOR_SIZE_INFINITY
&& mMaxSize
> 0.0f
) {
690 // if we are here, there is a user defined maxsize ...
691 //XXX Set stretchHint = NS_STRETCH_NORMAL? to honor the maxsize as close as possible?
692 if (NS_MATHML_OPERATOR_MAXSIZE_IS_ABSOLUTE(mFlags
)) {
693 // there is an explicit value like maxsize="20pt"
694 // try to maintain the aspect ratio of the char
695 float aspect
= mMaxSize
/ float(initialSize
.ascent
+ initialSize
.descent
);
697 PR_MIN(container
.ascent
, nscoord(initialSize
.ascent
* aspect
));
699 PR_MIN(container
.descent
, nscoord(initialSize
.descent
* aspect
));
700 // below we use a type cast instead of a conversion to avoid a VC++ bug
701 // see http://support.microsoft.com/support/kb/articles/Q115/7/05.ASP
703 PR_MIN(container
.width
, (nscoord
)mMaxSize
);
705 else { // multiplicative value
707 PR_MIN(container
.ascent
, nscoord(initialSize
.ascent
* mMaxSize
));
709 PR_MIN(container
.descent
, nscoord(initialSize
.descent
* mMaxSize
));
711 PR_MIN(container
.width
, nscoord(initialSize
.width
* mMaxSize
));
714 if (isVertical
&& !NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags
)) {
715 // re-adjust to align the char with the bottom of the initial container
716 height
= container
.ascent
+ container
.descent
;
717 container
.descent
= aContainerSize
.descent
;
718 container
.ascent
= height
- container
.descent
;
722 if (mMinSize
> 0.0f
) {
723 // if we are here, there is a user defined minsize ...
724 // always allow the char to stretch in its natural direction,
725 // even if it is different from the caller's direction
726 if (aStretchDirection
!= NS_STRETCH_DIRECTION_DEFAULT
&&
727 aStretchDirection
!= mEmbellishData
.direction
) {
728 aStretchDirection
= NS_STRETCH_DIRECTION_DEFAULT
;
729 // but when we are not honoring the requested direction
730 // we should not use the caller's container size either
731 container
= initialSize
;
733 if (NS_MATHML_OPERATOR_MINSIZE_IS_ABSOLUTE(mFlags
)) {
734 // there is an explicit value like minsize="20pt"
735 // try to maintain the aspect ratio of the char
736 float aspect
= mMinSize
/ float(initialSize
.ascent
+ initialSize
.descent
);
738 PR_MAX(container
.ascent
, nscoord(initialSize
.ascent
* aspect
));
740 PR_MAX(container
.descent
, nscoord(initialSize
.descent
* aspect
));
742 PR_MAX(container
.width
, (nscoord
)mMinSize
);
744 else { // multiplicative value
746 PR_MAX(container
.ascent
, nscoord(initialSize
.ascent
* mMinSize
));
748 PR_MAX(container
.descent
, nscoord(initialSize
.descent
* mMinSize
));
750 PR_MAX(container
.width
, nscoord(initialSize
.width
* mMinSize
));
753 if (isVertical
&& !NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags
)) {
754 // re-adjust to align the char with the bottom of the initial container
755 height
= container
.ascent
+ container
.descent
;
756 container
.descent
= aContainerSize
.descent
;
757 container
.ascent
= height
- container
.descent
;
762 // let the MathMLChar stretch itself...
763 nsresult res
= mMathMLChar
.Stretch(PresContext(), aRenderingContext
,
764 aStretchDirection
, container
, charSize
, stretchHint
);
765 if (NS_FAILED(res
)) {
766 // gracefully handle cases where stretching the char failed (i.e., GetBoundingMetrics failed)
767 // clear our 'form' to behave as if the operator wasn't in the dictionary
768 mFlags
&= ~NS_MATHML_OPERATOR_FORM
;
769 useMathMLChar
= PR_FALSE
;
773 // Child frames of invisble operators are not reflowed
774 if (!NS_MATHML_OPERATOR_IS_INVISIBLE(mFlags
)) {
775 // Place our children using the default method
776 // This will allow our child text frame to get its DidReflow()
777 nsresult rv
= Place(aRenderingContext
, PR_TRUE
, aDesiredStretchSize
);
778 if (NS_MATHML_HAS_ERROR(mPresentationData
.flags
) || NS_FAILED(rv
)) {
779 // Make sure the child frames get their DidReflow() calls.
780 DidReflowChildren(mFrames
.FirstChild());
785 // update our bounding metrics... it becomes that of our MathML char
786 mBoundingMetrics
= charSize
;
788 // if the returned direction is 'unsupported', the char didn't actually change.
789 // So we do the centering only if necessary
790 if (mMathMLChar
.GetStretchDirection() != NS_STRETCH_DIRECTION_UNSUPPORTED
||
791 NS_MATHML_OPERATOR_IS_CENTERED(mFlags
)) {
793 if (isVertical
|| NS_MATHML_OPERATOR_IS_CENTERED(mFlags
)) {
794 // the desired size returned by mMathMLChar maybe different
795 // from the size of the container.
796 // the mMathMLChar.mRect.y calculation is subtle, watch out!!!
798 height
= mBoundingMetrics
.ascent
+ mBoundingMetrics
.descent
;
799 if (NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags
) ||
800 NS_MATHML_OPERATOR_IS_CENTERED(mFlags
)) {
801 // For symmetric and vertical operators, or for operators that are always
802 // centered ('+', '*', etc) we want to center about the axis of the container
803 mBoundingMetrics
.descent
= height
/2 - axisHeight
;
806 // Otherwise, align the char with the bottom of the container
807 mBoundingMetrics
.descent
= container
.descent
;
809 mBoundingMetrics
.ascent
= height
- mBoundingMetrics
.descent
;
814 // Fixup for the final height.
815 // On one hand, our stretchy height can sometimes be shorter than surrounding
816 // ASCII chars, e.g., arrow symbols have |mBoundingMetrics.ascent + leading|
817 // that is smaller than the ASCII's ascent, hence when painting the background
818 // later, it won't look uniform along the line.
819 // On the other hand, sometimes we may leave too much gap when our glyph happens
820 // to come from a font with tall glyphs. For example, since CMEX10 has very tall
821 // glyphs, its natural font metrics are large, even if we pick a small glyph
822 // whose size is comparable to the size of a normal ASCII glyph.
823 // So to avoid uneven spacing in either of these two cases, we use the height
824 // of the ASCII font as a reference and try to match it if possible.
826 // special case for accents... keep them short to improve mouse operations...
827 // an accent can only be the non-first child of <mover>, <munder>, <munderover>
829 NS_MATHML_EMBELLISH_IS_ACCENT(mEmbellishData
.flags
);
831 nsEmbellishData parentData
;
832 GetEmbellishDataFrom(mParent
, parentData
);
834 (NS_MATHML_EMBELLISH_IS_ACCENTOVER(parentData
.flags
) ||
835 NS_MATHML_EMBELLISH_IS_ACCENTUNDER(parentData
.flags
)) &&
836 parentData
.coreFrame
!= this;
838 if (isAccent
&& firstChild
) {
839 // see bug 188467 for what is going on here
840 nscoord dy
= aDesiredStretchSize
.ascent
- (mBoundingMetrics
.ascent
+ leading
);
841 aDesiredStretchSize
.ascent
= mBoundingMetrics
.ascent
+ leading
;
842 aDesiredStretchSize
.height
= aDesiredStretchSize
.ascent
+ mBoundingMetrics
.descent
;
844 firstChild
->SetPosition(firstChild
->GetPosition() - nsPoint(0, dy
));
846 else if (useMathMLChar
) {
847 nscoord ascent
, descent
;
848 fm
->GetMaxAscent(ascent
);
849 fm
->GetMaxDescent(descent
);
850 aDesiredStretchSize
.ascent
= PR_MAX(mBoundingMetrics
.ascent
+ leading
, ascent
);
851 aDesiredStretchSize
.height
= aDesiredStretchSize
.ascent
+
852 PR_MAX(mBoundingMetrics
.descent
+ leading
, descent
);
854 aDesiredStretchSize
.width
= mBoundingMetrics
.width
;
855 aDesiredStretchSize
.mBoundingMetrics
= mBoundingMetrics
;
857 mReference
.y
= aDesiredStretchSize
.ascent
;
858 // Place our mMathMLChar, its origin is in our coordinate system
860 nscoord dy
= aDesiredStretchSize
.ascent
- mBoundingMetrics
.ascent
;
861 mMathMLChar
.SetRect(nsRect(0, dy
, charSize
.width
, charSize
.ascent
+ charSize
.descent
));
864 // Before we leave... there is a last item in the check-list:
865 // If our parent is not embellished, it means we are the outermost embellished
866 // container and so we put the spacing, otherwise we don't include the spacing,
867 // the outermost embellished container will take care of it.
869 if (!NS_MATHML_OPERATOR_HAS_EMBELLISH_ANCESTOR(mFlags
)) {
871 // Account the spacing if we are not an accent with explicit attributes
872 nscoord leftSpace
= mEmbellishData
.leftSpace
;
873 if (isAccent
&& !NS_MATHML_OPERATOR_HAS_LEFTSPACE_ATTR(mFlags
)) {
876 nscoord rightSpace
= mEmbellishData
.rightSpace
;
877 if (isAccent
&& !NS_MATHML_OPERATOR_HAS_RIGHTSPACE_ATTR(mFlags
)) {
881 mBoundingMetrics
.width
+= leftSpace
+ rightSpace
;
882 aDesiredStretchSize
.width
= mBoundingMetrics
.width
;
883 aDesiredStretchSize
.mBoundingMetrics
.width
= mBoundingMetrics
.width
;
886 // adjust the offsets
887 mBoundingMetrics
.leftBearing
+= leftSpace
;
888 mBoundingMetrics
.rightBearing
+= leftSpace
;
889 aDesiredStretchSize
.mBoundingMetrics
.leftBearing
+= leftSpace
;
890 aDesiredStretchSize
.mBoundingMetrics
.rightBearing
+= leftSpace
;
894 mMathMLChar
.GetRect(rect
);
895 mMathMLChar
.SetRect(nsRect(rect
.x
+ leftSpace
, rect
.y
, rect
.width
, rect
.height
));
898 nsIFrame
* childFrame
= firstChild
;
900 childFrame
->SetPosition(childFrame
->GetPosition()
901 + nsPoint(leftSpace
, 0));
902 childFrame
= childFrame
->GetNextSibling();
908 // Finished with these:
909 ClearSavedChildMetrics();
910 // Set our overflow area
911 GatherAndStoreOverflow(&aDesiredStretchSize
);
913 // There used to be code here to change the height of the child frame to
914 // change the caret height, but the text frame that manages the caret is now
915 // not a direct child but wrapped in a block frame. See also bug 412033.
921 nsMathMLmoFrame::InheritAutomaticData(nsIFrame
* aParent
)
923 // retain our native direction, it only changes if our text content changes
924 nsStretchDirection direction
= mEmbellishData
.direction
;
925 nsMathMLTokenFrame::InheritAutomaticData(aParent
);
926 mEmbellishData
.direction
= direction
;
931 nsMathMLmoFrame::TransmitAutomaticData()
933 // this will cause us to re-sync our flags from scratch
934 // but our returned 'form' is still not final (bug 133429), it will
935 // be recomputed to its final value during the next call in Reflow()
936 mEmbellishData
.coreFrame
= nsnull
;
937 ProcessOperatorData();
942 nsMathMLmoFrame::Reflow(nsPresContext
* aPresContext
,
943 nsHTMLReflowMetrics
& aDesiredSize
,
944 const nsHTMLReflowState
& aReflowState
,
945 nsReflowStatus
& aStatus
)
947 // certain values use units that depend on our style context, so
948 // it is safer to just process the whole lot here
949 ProcessOperatorData();
951 // play safe by not passing invisible operators to the font subsystem because
952 // some platforms risk selecting strange glyphs for them and give bad inter-space
953 if (NS_MATHML_OPERATOR_IS_INVISIBLE(mFlags
)) {
954 // return empty space for now, but this is not yet final since there
955 // can be lspace and rspace attributes that reclaim some room.
956 // These will be dealt with later in Stretch().
957 aDesiredSize
.width
= 0;
958 aDesiredSize
.height
= 0;
959 aDesiredSize
.ascent
= 0;
960 aDesiredSize
.mBoundingMetrics
.Clear();
961 aStatus
= NS_FRAME_COMPLETE
;
963 NS_FRAME_SET_TRUNCATION(aStatus
, aReflowState
, aDesiredSize
);
967 return nsMathMLTokenFrame::Reflow(aPresContext
, aDesiredSize
,
968 aReflowState
, aStatus
);
972 nsMathMLmoFrame::MarkIntrinsicWidthsDirty()
974 // if we get this, it may mean that something changed in the text
975 // content. So blow away everything an re-build the automatic data
976 // from the parent of our outermost embellished container (we ensure
977 // that we are the core, not just a sibling of the core)
981 nsIFrame
* target
= this;
982 nsEmbellishData embellishData
;
984 target
= target
->GetParent();
985 GetEmbellishDataFrom(target
, embellishData
);
986 } while (embellishData
.coreFrame
== this);
988 // we have automatic data to update in the children of the target frame
989 // XXXldb This should really be marking dirty rather than rebuilding
990 // so that we don't rebuild multiple times for the same change.
991 RebuildAutomaticDataForChildren(target
);
993 nsMathMLContainerFrame::MarkIntrinsicWidthsDirty();
996 /* virtual */ nscoord
997 nsMathMLmoFrame::GetIntrinsicWidth(nsIRenderingContext
*aRenderingContext
)
999 ProcessOperatorData();
1001 if (UseMathMLChar()) {
1002 PRUint32 stretchHint
= GetStretchHint(mFlags
, mPresentationData
, PR_TRUE
);
1003 width
= mMathMLChar
.
1004 GetMaxWidth(PresContext(), *aRenderingContext
,
1005 stretchHint
, mMaxSize
,
1006 NS_MATHML_OPERATOR_MAXSIZE_IS_ABSOLUTE(mFlags
));
1009 width
= nsMathMLTokenFrame::GetIntrinsicWidth(aRenderingContext
);
1012 // leftSpace and rightSpace are actually applied to the outermost
1013 // embellished container but for determining total intrinsic width it should
1014 // be safe to include it for the core here instead.
1015 width
+= mEmbellishData
.leftSpace
+ mEmbellishData
.rightSpace
;
1021 nsMathMLmoFrame::AttributeChanged(PRInt32 aNameSpaceID
,
1022 nsIAtom
* aAttribute
,
1025 // check if this is an attribute that can affect the embellished hierarchy
1026 // in a significant way and re-layout the entire hierarchy.
1027 if (nsGkAtoms::accent_
== aAttribute
||
1028 nsGkAtoms::movablelimits_
== aAttribute
) {
1030 // set the target as the parent of our outermost embellished container
1031 // (we ensure that we are the core, not just a sibling of the core)
1032 nsIFrame
* target
= this;
1033 nsEmbellishData embellishData
;
1035 target
= target
->GetParent();
1036 GetEmbellishDataFrom(target
, embellishData
);
1037 } while (embellishData
.coreFrame
== this);
1039 // we have automatic data to update in the children of the target frame
1040 return ReLayoutChildren(target
, NS_FRAME_IS_DIRTY
);
1043 return nsMathMLTokenFrame::
1044 AttributeChanged(aNameSpaceID
, aAttribute
, aModType
);
1047 // ----------------------
1048 // No need to tract the style context given to our MathML char.
1049 // the Style System will use these to pass the proper style context to our MathMLChar
1051 nsMathMLmoFrame::GetAdditionalStyleContext(PRInt32 aIndex
) const
1054 case NS_MATHML_CHAR_STYLE_CONTEXT_INDEX
:
1055 return mMathMLChar
.GetStyleContext();
1062 nsMathMLmoFrame::SetAdditionalStyleContext(PRInt32 aIndex
,
1063 nsStyleContext
* aStyleContext
)
1066 case NS_MATHML_CHAR_STYLE_CONTEXT_INDEX
:
1067 mMathMLChar
.SetStyleContext(aStyleContext
);