Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / layout / mathml / nsMathMLmoFrame.cpp
blobec69b08b4812ef93ad3e2f9b4f74f89456d8679d
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
13 * License.
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.
22 * Contributor(s):
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>
27 * Frederic Wang <fred.wang@free.fr>
29 * Alternatively, the contents of this file may be used under the terms of
30 * either of the GNU General Public License Version 2 or later (the "GPL"),
31 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 * in which case the provisions of the GPL or the LGPL are applicable instead
33 * of those above. If you wish to allow use of your version of this file only
34 * under the terms of either the GPL or the LGPL, and not to allow others to
35 * use your version of this file under the terms of the MPL, indicate your
36 * decision by deleting the provisions above and replace them with the notice
37 * and other provisions required by the GPL or the LGPL. If you do not delete
38 * the provisions above, a recipient may use your version of this file under
39 * the terms of any one of the MPL, the GPL or the LGPL.
41 * ***** END LICENSE BLOCK ***** */
43 #include "nsCOMPtr.h"
44 #include "nsFrame.h"
45 #include "nsPresContext.h"
46 #include "nsStyleContext.h"
47 #include "nsStyleConsts.h"
48 #include "nsIRenderingContext.h"
49 #include "nsIFontMetrics.h"
50 #include "nsContentUtils.h"
52 #include "nsIDOMText.h"
54 #include "nsMathMLmoFrame.h"
57 // <mo> -- operator, fence, or separator - implementation
60 // additional style context to be used by our MathMLChar.
61 #define NS_MATHML_CHAR_STYLE_CONTEXT_INDEX 0
63 nsIFrame*
64 NS_NewMathMLmoFrame(nsIPresShell* aPresShell, nsStyleContext *aContext)
66 return new (aPresShell) nsMathMLmoFrame(aContext);
69 NS_IMPL_FRAMEARENA_HELPERS(nsMathMLmoFrame)
71 nsMathMLmoFrame::~nsMathMLmoFrame()
75 static const PRUnichar kInvisibleComma = PRUnichar(0x200B); // a.k.a. ZERO WIDTH SPACE
76 static const PRUnichar kApplyFunction = PRUnichar(0x2061);
77 static const PRUnichar kInvisibleTimes = PRUnichar(0x2062);
78 static const PRUnichar kNullCh = PRUnichar('\0');
80 eMathMLFrameType
81 nsMathMLmoFrame::GetMathMLFrameType()
83 return NS_MATHML_OPERATOR_IS_INVISIBLE(mFlags)
84 ? eMathMLFrameType_OperatorInvisible
85 : eMathMLFrameType_OperatorOrdinary;
88 // since a mouse click implies selection, we cannot just rely on the
89 // frame's state bit in our child text frame. So we will first check
90 // its selected state bit, and use this little helper to double check.
91 PRBool
92 nsMathMLmoFrame::IsFrameInSelection(nsIFrame* aFrame)
94 NS_ASSERTION(aFrame, "null arg");
95 if (!aFrame)
96 return PR_FALSE;
98 PRBool isSelected = PR_FALSE;
99 aFrame->GetSelected(&isSelected);
100 if (!isSelected)
101 return PR_FALSE;
103 const nsFrameSelection* frameSelection = aFrame->GetConstFrameSelection();
104 SelectionDetails* details =
105 frameSelection->LookUpSelection(aFrame->GetContent(), 0, 1, PR_TRUE);
107 if (!details)
108 return PR_FALSE;
110 while (details) {
111 SelectionDetails* next = details->mNext;
112 delete details;
113 details = next;
115 return PR_TRUE;
118 PRBool
119 nsMathMLmoFrame::UseMathMLChar()
121 return (NS_MATHML_OPERATOR_GET_FORM(mFlags) &&
122 NS_MATHML_OPERATOR_IS_MUTABLE(mFlags)) ||
123 NS_MATHML_OPERATOR_IS_CENTERED(mFlags) ||
124 NS_MATHML_OPERATOR_IS_INVISIBLE(mFlags);
127 NS_IMETHODIMP
128 nsMathMLmoFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
129 const nsRect& aDirtyRect,
130 const nsDisplayListSet& aLists)
132 nsresult rv = NS_OK;
133 PRBool useMathMLChar = UseMathMLChar();
135 if (!useMathMLChar) {
136 // let the base class do everything
137 rv = nsMathMLTokenFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
138 NS_ENSURE_SUCCESS(rv, rv);
139 } else {
140 rv = DisplayBorderBackgroundOutline(aBuilder, aLists);
141 NS_ENSURE_SUCCESS(rv, rv);
143 // make our char selected if our inner child text frame is selected
144 PRBool isSelected = PR_FALSE;
145 nsRect selectedRect;
146 nsIFrame* firstChild = mFrames.FirstChild();
147 if (IsFrameInSelection(firstChild)) {
148 selectedRect = firstChild->GetRect();
149 isSelected = PR_TRUE;
151 rv = mMathMLChar.Display(aBuilder, this, aLists, isSelected ? &selectedRect : nsnull);
152 NS_ENSURE_SUCCESS(rv, rv);
154 #if defined(NS_DEBUG) && defined(SHOW_BOUNDING_BOX)
155 // for visual debug
156 rv = DisplayBoundingMetrics(aBuilder, this, mReference, mBoundingMetrics, aLists);
157 #endif
159 return rv;
162 // get the text that we enclose and setup our nsMathMLChar
163 void
164 nsMathMLmoFrame::ProcessTextData()
166 mFlags = 0;
168 nsAutoString data;
169 nsContentUtils::GetNodeTextContent(mContent, PR_FALSE, data);
170 PRInt32 length = data.Length();
171 PRUnichar ch = (length == 0) ? kNullCh : data[0];
173 if ((length == 1) &&
174 (ch == kInvisibleComma ||
175 ch == kApplyFunction ||
176 ch == kInvisibleTimes)) {
177 mFlags |= NS_MATHML_OPERATOR_INVISIBLE;
180 // don't bother doing anything special if we don't have a
181 // single child with a visible text content
182 nsPresContext* presContext = PresContext();
183 if (NS_MATHML_OPERATOR_IS_INVISIBLE(mFlags) || mFrames.GetLength() != 1) {
184 data.Truncate(); // empty data to reset the char
185 mMathMLChar.SetData(presContext, data);
186 ResolveMathMLCharStyle(presContext, mContent, mStyleContext, &mMathMLChar, PR_FALSE);
187 return;
190 // special... in math mode, the usual minus sign '-' looks too short, so
191 // what we do here is to remap <mo>-</mo> to the official Unicode minus
192 // sign (U+2212) which looks much better. For background on this, see
193 // http://groups.google.com/groups?hl=en&th=66488daf1ade7635&rnum=1
194 if (1 == length && ch == '-') {
195 ch = 0x2212;
196 data = ch;
199 // cache the special bits: mutable, accent, movablelimits, centered.
200 // we need to do this in anticipation of other requirements, and these
201 // bits don't change. Do not reset these bits unless the text gets changed.
203 // lookup all the forms under which the operator is listed in the dictionary,
204 // and record whether the operator has accent="true" or movablelimits="true"
205 nsOperatorFlags flags[4];
206 float lspace[4], rspace[4];
207 nsMathMLOperators::LookupOperators(data, flags, lspace, rspace);
208 nsOperatorFlags allFlags =
209 flags[NS_MATHML_OPERATOR_FORM_INFIX] |
210 flags[NS_MATHML_OPERATOR_FORM_POSTFIX] |
211 flags[NS_MATHML_OPERATOR_FORM_PREFIX];
213 mFlags |= allFlags & NS_MATHML_OPERATOR_ACCENT;
214 mFlags |= allFlags & NS_MATHML_OPERATOR_MOVABLELIMITS;
216 PRBool isMutable =
217 NS_MATHML_OPERATOR_IS_STRETCHY(allFlags) ||
218 NS_MATHML_OPERATOR_IS_LARGEOP(allFlags);
219 if (isMutable)
220 mFlags |= NS_MATHML_OPERATOR_MUTABLE;
222 // see if this is an operator that should be centered to cater for
223 // fonts that are not math-aware
224 if (1 == length) {
225 if ((ch == '+') || (ch == '=') || (ch == '*') ||
226 (ch == 0x2212) || // &minus;
227 (ch == 0x2264) || // &le;
228 (ch == 0x2265) || // &ge;
229 (ch == 0x00D7)) { // &times;
230 mFlags |= NS_MATHML_OPERATOR_CENTERED;
234 // cache the operator
235 mMathMLChar.SetData(presContext, data);
236 ResolveMathMLCharStyle(presContext, mContent, mStyleContext, &mMathMLChar, isMutable);
238 // cache the native direction -- beware of bug 133429...
239 // mEmbellishData.direction must always retain our native direction, whereas
240 // mMathMLChar.GetStretchDirection() may change later, when Stretch() is called
241 mEmbellishData.direction = mMathMLChar.GetStretchDirection();
244 // get our 'form' and lookup in the Operator Dictionary to fetch
245 // our default data that may come from there. Then complete our setup
246 // using attributes that we may have. To stay in sync, this function is
247 // called very often. We depend on many things that may change around us.
248 // However, we re-use unchanged values.
249 void
250 nsMathMLmoFrame::ProcessOperatorData()
252 // if we have been here before, we will just use our cached form
253 nsOperatorFlags form = NS_MATHML_OPERATOR_GET_FORM(mFlags);
254 nsAutoString value;
256 // special bits are always kept in mFlags.
257 // remember the mutable bit from ProcessTextData().
258 // Some chars are listed under different forms in the dictionary,
259 // and there could be a form under which the char is mutable.
260 // If the char is the core of an embellished container, we will keep
261 // it mutable irrespective of the form of the embellished container.
262 // Also remember the other special bits that we want to carry forward.
263 mFlags &= NS_MATHML_OPERATOR_MUTABLE |
264 NS_MATHML_OPERATOR_ACCENT |
265 NS_MATHML_OPERATOR_MOVABLELIMITS |
266 NS_MATHML_OPERATOR_CENTERED |
267 NS_MATHML_OPERATOR_INVISIBLE;
269 if (!mEmbellishData.coreFrame) {
270 // i.e., we haven't been here before, the default form is infix
271 form = NS_MATHML_OPERATOR_FORM_INFIX;
273 // reset everything so that we don't keep outdated values around
274 // in case of dynamic changes
275 mEmbellishData.flags = 0;
276 mEmbellishData.coreFrame = nsnull;
277 mEmbellishData.leftSpace = 0;
278 mEmbellishData.rightSpace = 0;
279 if (mMathMLChar.Length() != 1)
280 mEmbellishData.direction = NS_STRETCH_DIRECTION_UNSUPPORTED;
281 // else... retain the native direction obtained in ProcessTextData()
283 if (!mFrames.FirstChild()) {
284 return;
287 mEmbellishData.flags |= NS_MATHML_EMBELLISH_OPERATOR;
288 mEmbellishData.coreFrame = this;
290 // there are two particular things that we also need to record so that if our
291 // parent is <mover>, <munder>, or <munderover>, they will treat us properly:
292 // 1) do we have accent="true"
293 // 2) do we have movablelimits="true"
295 // they need the extra information to decide how to treat their scripts/limits
296 // (note: <mover>, <munder>, or <munderover> need not necessarily be our
297 // direct parent -- case of embellished operators)
299 // default values from the Operator Dictionary were obtained in ProcessTextData()
300 // and these special bits are always kept in mFlags
301 if (NS_MATHML_OPERATOR_IS_ACCENT(mFlags))
302 mEmbellishData.flags |= NS_MATHML_EMBELLISH_ACCENT;
303 if (NS_MATHML_OPERATOR_IS_MOVABLELIMITS(mFlags))
304 mEmbellishData.flags |= NS_MATHML_EMBELLISH_MOVABLELIMITS;
306 // see if the accent attribute is there
307 GetAttribute(mContent, mPresentationData.mstyle, nsGkAtoms::accent_,
308 value);
309 if (value.EqualsLiteral("true"))
310 mEmbellishData.flags |= NS_MATHML_EMBELLISH_ACCENT;
311 else if (value.EqualsLiteral("false"))
312 mEmbellishData.flags &= ~NS_MATHML_EMBELLISH_ACCENT;
314 // see if the movablelimits attribute is there
315 GetAttribute(mContent, mPresentationData.mstyle,
316 nsGkAtoms::movablelimits_, value);
317 if (value.EqualsLiteral("true"))
318 mEmbellishData.flags |= NS_MATHML_EMBELLISH_MOVABLELIMITS;
319 else if (value.EqualsLiteral("false"))
320 mEmbellishData.flags &= ~NS_MATHML_EMBELLISH_MOVABLELIMITS;
322 // ---------------------------------------------------------------------
323 // we will be called again to re-sync the rest of our state next time...
324 // (nobody needs the other values below at this stage)
325 mFlags |= form;
326 return;
329 nsPresContext* presContext = PresContext();
331 // beware of bug 133814 - there is a two-way dependency in the
332 // embellished hierarchy: our embellished ancestors need to set
333 // their flags based on some of our state (set above), and here we
334 // need to re-sync our 'form' depending on our outermost embellished
335 // container. A null form here means that an earlier attempt to stretch
336 // our mMathMLChar failed, in which case we don't bother re-stretching again
337 if (form) {
338 // get our outermost embellished container and its parent.
339 // (we ensure that we are the core, not just a sibling of the core)
340 nsIFrame* embellishAncestor = this;
341 nsEmbellishData embellishData;
342 nsIFrame* parentAncestor = this;
343 do {
344 embellishAncestor = parentAncestor;
345 parentAncestor = embellishAncestor->GetParent();
346 GetEmbellishDataFrom(parentAncestor, embellishData);
347 } while (embellishData.coreFrame == this);
349 // flag if we have an embellished ancestor
350 if (embellishAncestor != this)
351 mFlags |= NS_MATHML_OPERATOR_EMBELLISH_ANCESTOR;
352 else
353 mFlags &= ~NS_MATHML_OPERATOR_EMBELLISH_ANCESTOR;
355 // find the position of our outermost embellished container w.r.t
356 // its siblings.
358 nsIFrame* nextSibling = embellishAncestor->GetNextSibling();
359 nsIFrame* prevSibling = embellishAncestor->GetPrevSibling();
361 // flag to distinguish from a real infix
362 if (!prevSibling && !nextSibling)
363 mFlags |= NS_MATHML_OPERATOR_EMBELLISH_ISOLATED;
364 else
365 mFlags &= ~NS_MATHML_OPERATOR_EMBELLISH_ISOLATED;
367 // find our form
368 form = NS_MATHML_OPERATOR_FORM_INFIX;
369 GetAttribute(mContent, mPresentationData.mstyle, nsGkAtoms::form,
370 value);
371 if (!value.IsEmpty()) {
372 if (value.EqualsLiteral("prefix"))
373 form = NS_MATHML_OPERATOR_FORM_PREFIX;
374 else if (value.EqualsLiteral("postfix"))
375 form = NS_MATHML_OPERATOR_FORM_POSTFIX;
377 else {
378 // set our form flag depending on the position
379 if (!prevSibling && nextSibling)
380 form = NS_MATHML_OPERATOR_FORM_PREFIX;
381 else if (prevSibling && !nextSibling)
382 form = NS_MATHML_OPERATOR_FORM_POSTFIX;
384 mFlags &= ~NS_MATHML_OPERATOR_FORM; // clear the old form bits
385 mFlags |= form;
387 // lookup the operator dictionary
388 float lspace = 0.0f;
389 float rspace = 0.0f;
390 nsAutoString data;
391 mMathMLChar.GetData(data);
392 PRBool found = nsMathMLOperators::LookupOperator(data, form, &mFlags, &lspace, &rspace);
393 if (found && (lspace || rspace)) {
394 // cache the default values of lspace & rspace that we get from the dictionary.
395 // since these values are relative to the 'em' unit, convert to twips now
396 nscoord em;
397 nsCOMPtr<nsIFontMetrics> fm =
398 presContext->GetMetricsFor(GetStyleFont()->mFont);
399 GetEmHeight(fm, em);
401 mEmbellishData.leftSpace = NSToCoordRound(lspace * em);
402 mEmbellishData.rightSpace = NSToCoordRound(rspace * em);
404 // tuning if we don't want too much extra space when we are a script.
405 // (with its fonts, TeX sets lspace=0 & rspace=0 as soon as scriptlevel>0.
406 // Our fonts can be anything, so...)
407 if (GetStyleFont()->mScriptLevel > 0) {
408 if (NS_MATHML_OPERATOR_EMBELLISH_IS_ISOLATED(mFlags)) {
409 // could be an isolated accent or script, e.g., x^{+}, just zero out
410 mEmbellishData.leftSpace = 0;
411 mEmbellishData.rightSpace = 0;
413 else if (!NS_MATHML_OPERATOR_HAS_EMBELLISH_ANCESTOR(mFlags)) {
414 mEmbellishData.leftSpace /= 2;
415 mEmbellishData.rightSpace /= 2;
421 // If we are an accent without explicit lspace="." or rspace=".",
422 // we will ignore our default left/right space
424 // lspace = number h-unit | namedspace
425 nscoord leftSpace = mEmbellishData.leftSpace;
426 GetAttribute(mContent, mPresentationData.mstyle, nsGkAtoms::lspace_,
427 value);
428 if (!value.IsEmpty()) {
429 nsCSSValue cssValue;
430 if (ParseNumericValue(value, cssValue) ||
431 ParseNamedSpaceValue(mPresentationData.mstyle, value, cssValue))
433 if ((eCSSUnit_Number == cssValue.GetUnit()) && !cssValue.GetFloatValue())
434 leftSpace = 0;
435 else if (cssValue.IsLengthUnit())
436 leftSpace = CalcLength(presContext, mStyleContext, cssValue);
437 mFlags |= NS_MATHML_OPERATOR_LEFTSPACE_ATTR;
441 // rspace = number h-unit | namedspace
442 nscoord rightSpace = mEmbellishData.rightSpace;
443 GetAttribute(mContent, mPresentationData.mstyle, nsGkAtoms::rspace_,
444 value);
445 if (!value.IsEmpty()) {
446 nsCSSValue cssValue;
447 if (ParseNumericValue(value, cssValue) ||
448 ParseNamedSpaceValue(mPresentationData.mstyle, value, cssValue))
450 if ((eCSSUnit_Number == cssValue.GetUnit()) && !cssValue.GetFloatValue())
451 rightSpace = 0;
452 else if (cssValue.IsLengthUnit())
453 rightSpace = CalcLength(presContext, mStyleContext, cssValue);
454 mFlags |= NS_MATHML_OPERATOR_RIGHTSPACE_ATTR;
458 // little extra tuning to round lspace & rspace to at least a pixel so that
459 // operators don't look as if they are colliding with their operands
460 if (leftSpace || rightSpace) {
461 nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);
462 if (leftSpace && leftSpace < onePixel)
463 leftSpace = onePixel;
464 if (rightSpace && rightSpace < onePixel)
465 rightSpace = onePixel;
468 // the values that we get from our attributes override the dictionary
469 mEmbellishData.leftSpace = leftSpace;
470 mEmbellishData.rightSpace = rightSpace;
472 // Now see if there are user-defined attributes that override the dictionary.
473 // XXX If an attribute can be forced to be true when it is false in the
474 // dictionary, then the following code has to change...
476 // For each attribute overriden by the user, turn off its bit flag.
477 // symmetric|movablelimits|separator|largeop|accent|fence|stretchy|form
478 // special: accent and movablelimits are handled above,
479 // don't process them here
481 GetAttribute(mContent, mPresentationData.mstyle,
482 nsGkAtoms::stretchy_, value);
483 if (value.EqualsLiteral("false")) {
484 mFlags &= ~NS_MATHML_OPERATOR_STRETCHY;
485 } else if (value.EqualsLiteral("true")) {
486 mFlags |= NS_MATHML_OPERATOR_STRETCHY;
488 if (NS_MATHML_OPERATOR_IS_FENCE(mFlags)) {
489 GetAttribute(mContent, mPresentationData.mstyle,
490 nsGkAtoms::fence_, value);
491 if (value.EqualsLiteral("false"))
492 mFlags &= ~NS_MATHML_OPERATOR_FENCE;
494 GetAttribute(mContent, mPresentationData.mstyle,
495 nsGkAtoms::largeop_, value);
496 if (value.EqualsLiteral("false")) {
497 mFlags &= ~NS_MATHML_OPERATOR_LARGEOP;
498 } else if (value.EqualsLiteral("true")) {
499 mFlags |= NS_MATHML_OPERATOR_LARGEOP;
501 if (NS_MATHML_OPERATOR_IS_SEPARATOR(mFlags)) {
502 GetAttribute(mContent, mPresentationData.mstyle,
503 nsGkAtoms::separator_, value);
504 if (value.EqualsLiteral("false"))
505 mFlags &= ~NS_MATHML_OPERATOR_SEPARATOR;
507 GetAttribute(mContent, mPresentationData.mstyle, nsGkAtoms::symmetric_,
508 value);
509 if (value.EqualsLiteral("false"))
510 mFlags &= ~NS_MATHML_OPERATOR_SYMMETRIC;
511 else if (value.EqualsLiteral("true"))
512 mFlags |= NS_MATHML_OPERATOR_SYMMETRIC;
514 // minsize = number [ v-unit | h-unit ] | namedspace
515 mMinSize = 0.0;
516 GetAttribute(mContent, mPresentationData.mstyle, nsGkAtoms::minsize_,
517 value);
518 if (!value.IsEmpty()) {
519 nsCSSValue cssValue;
520 if (ParseNumericValue(value, cssValue) ||
521 ParseNamedSpaceValue(mPresentationData.mstyle, value, cssValue))
523 nsCSSUnit unit = cssValue.GetUnit();
524 if (eCSSUnit_Number == unit)
525 mMinSize = cssValue.GetFloatValue();
526 else if (eCSSUnit_Percent == unit)
527 mMinSize = cssValue.GetPercentValue();
528 else if (eCSSUnit_Null != unit) {
529 mMinSize = float(CalcLength(presContext, mStyleContext, cssValue));
530 mFlags |= NS_MATHML_OPERATOR_MINSIZE_ABSOLUTE;
533 if ((eCSSUnit_Number == unit) || (eCSSUnit_Percent == unit)) {
534 // see if the multiplicative inheritance should be from <mstyle>
535 GetAttribute(nsnull, mPresentationData.mstyle,
536 nsGkAtoms::minsize_, value);
537 if (!value.IsEmpty()) {
538 if (ParseNumericValue(value, cssValue)) {
539 if (cssValue.IsLengthUnit()) {
540 mMinSize *= float(CalcLength(presContext, mStyleContext, cssValue));
541 mFlags |= NS_MATHML_OPERATOR_MINSIZE_ABSOLUTE;
549 // maxsize = number [ v-unit | h-unit ] | namedspace | infinity
550 mMaxSize = NS_MATHML_OPERATOR_SIZE_INFINITY;
551 GetAttribute(mContent, mPresentationData.mstyle, nsGkAtoms::maxsize_,
552 value);
553 if (!value.IsEmpty()) {
554 nsCSSValue cssValue;
555 if (ParseNumericValue(value, cssValue) ||
556 ParseNamedSpaceValue(mPresentationData.mstyle, value, cssValue))
558 nsCSSUnit unit = cssValue.GetUnit();
559 if (eCSSUnit_Number == unit)
560 mMaxSize = cssValue.GetFloatValue();
561 else if (eCSSUnit_Percent == unit)
562 mMaxSize = cssValue.GetPercentValue();
563 else if (eCSSUnit_Null != unit) {
564 mMaxSize = float(CalcLength(presContext, mStyleContext, cssValue));
565 mFlags |= NS_MATHML_OPERATOR_MAXSIZE_ABSOLUTE;
568 if ((eCSSUnit_Number == unit) || (eCSSUnit_Percent == unit)) {
569 // see if the multiplicative inheritance should be from <mstyle>
570 GetAttribute(nsnull, mPresentationData.mstyle,
571 nsGkAtoms::maxsize_, value);
572 if (!value.IsEmpty()) {
573 if (ParseNumericValue(value, cssValue)) {
574 if (cssValue.IsLengthUnit()) {
575 mMaxSize *= float(CalcLength(presContext, mStyleContext, cssValue));
576 mFlags |= NS_MATHML_OPERATOR_MAXSIZE_ABSOLUTE;
585 static PRUint32
586 GetStretchHint(nsOperatorFlags aFlags, nsPresentationData aPresentationData,
587 PRBool aIsVertical)
589 PRUint32 stretchHint = NS_STRETCH_NONE;
590 // See if it is okay to stretch,
591 // starting from what the Operator Dictionary said
592 if (NS_MATHML_OPERATOR_IS_MUTABLE(aFlags)) {
593 // set the largeop or largeopOnly flags to suitably cover all the
594 // 8 possible cases depending on whether displaystyle, largeop,
595 // stretchy are true or false (see bug 69325).
596 // . largeopOnly is taken if largeop=true and stretchy=false
597 // . largeop is taken if largeop=true and stretchy=true
598 if (NS_MATHML_IS_DISPLAYSTYLE(aPresentationData.flags) &&
599 NS_MATHML_OPERATOR_IS_LARGEOP(aFlags)) {
600 stretchHint = NS_STRETCH_LARGEOP; // (largeopOnly, not mask!)
601 if (NS_MATHML_OPERATOR_IS_INTEGRAL(aFlags)) {
602 stretchHint |= NS_STRETCH_INTEGRAL;
604 if (NS_MATHML_OPERATOR_IS_STRETCHY(aFlags)) {
605 stretchHint |= NS_STRETCH_NEARER | NS_STRETCH_LARGER;
608 else if(NS_MATHML_OPERATOR_IS_STRETCHY(aFlags)) {
609 if (aIsVertical) {
610 // TeX hint. Can impact some sloppy markups missing <mrow></mrow>
611 stretchHint = NS_STRETCH_NEARER;
613 else {
614 stretchHint = NS_STRETCH_NORMAL;
617 // else if the stretchy and largeop attributes have been disabled,
618 // the operator is not mutable
620 return stretchHint;
623 // NOTE: aDesiredStretchSize is an IN/OUT parameter
624 // On input - it contains our current size
625 // On output - the same size or the new size that we want
626 NS_IMETHODIMP
627 nsMathMLmoFrame::Stretch(nsIRenderingContext& aRenderingContext,
628 nsStretchDirection aStretchDirection,
629 nsBoundingMetrics& aContainerSize,
630 nsHTMLReflowMetrics& aDesiredStretchSize)
632 if (NS_MATHML_STRETCH_WAS_DONE(mPresentationData.flags)) {
633 NS_WARNING("it is wrong to fire stretch more than once on a frame");
634 return NS_OK;
636 mPresentationData.flags |= NS_MATHML_STRETCH_DONE;
638 nsIFrame* firstChild = mFrames.FirstChild();
640 // get the axis height;
641 nsCOMPtr<nsIFontMetrics> fm;
642 aRenderingContext.SetFont(GetStyleFont()->mFont,
643 PresContext()->GetUserFontSet());
644 aRenderingContext.GetFontMetrics(*getter_AddRefs(fm));
645 nscoord axisHeight, height;
646 GetAxisHeight(aRenderingContext, fm, axisHeight);
648 // get the leading to be left at the top and the bottom of the stretched char
649 // this seems more reliable than using fm->GetLeading() on suspicious fonts
650 nscoord em;
651 GetEmHeight(fm, em);
652 nscoord leading = NSToCoordRound(0.2f * em);
654 // Operators that are stretchy, or those that are to be centered
655 // to cater for fonts that are not math-aware, are handled by the MathMLChar
656 // ('form' is reset if stretch fails -- i.e., we don't bother to stretch next time)
657 PRBool useMathMLChar = UseMathMLChar();
659 nsBoundingMetrics charSize;
660 nsBoundingMetrics container = aDesiredStretchSize.mBoundingMetrics;
661 PRBool isVertical = PR_FALSE;
663 if (((aStretchDirection == NS_STRETCH_DIRECTION_VERTICAL) ||
664 (aStretchDirection == NS_STRETCH_DIRECTION_DEFAULT)) &&
665 (mEmbellishData.direction == NS_STRETCH_DIRECTION_VERTICAL)) {
666 isVertical = PR_TRUE;
669 PRUint32 stretchHint =
670 GetStretchHint(mFlags, mPresentationData, isVertical);
672 if (useMathMLChar) {
673 nsBoundingMetrics initialSize = aDesiredStretchSize.mBoundingMetrics;
675 if (stretchHint != NS_STRETCH_NONE) {
677 container = aContainerSize;
679 // some adjustments if the operator is symmetric and vertical
681 if (isVertical && NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags)) {
682 // we need to center about the axis
683 nscoord delta = NS_MAX(container.ascent - axisHeight,
684 container.descent + axisHeight);
685 container.ascent = delta + axisHeight;
686 container.descent = delta - axisHeight;
688 // get ready in case we encounter user-desired min-max size
689 delta = NS_MAX(initialSize.ascent - axisHeight,
690 initialSize.descent + axisHeight);
691 initialSize.ascent = delta + axisHeight;
692 initialSize.descent = delta - axisHeight;
695 // check for user-desired min-max size
697 if (mMaxSize != NS_MATHML_OPERATOR_SIZE_INFINITY && mMaxSize > 0.0f) {
698 // if we are here, there is a user defined maxsize ...
699 //XXX Set stretchHint = NS_STRETCH_NORMAL? to honor the maxsize as close as possible?
700 if (NS_MATHML_OPERATOR_MAXSIZE_IS_ABSOLUTE(mFlags)) {
701 // there is an explicit value like maxsize="20pt"
702 // try to maintain the aspect ratio of the char
703 float aspect = mMaxSize / float(initialSize.ascent + initialSize.descent);
704 container.ascent =
705 NS_MIN(container.ascent, nscoord(initialSize.ascent * aspect));
706 container.descent =
707 NS_MIN(container.descent, nscoord(initialSize.descent * aspect));
708 // below we use a type cast instead of a conversion to avoid a VC++ bug
709 // see http://support.microsoft.com/support/kb/articles/Q115/7/05.ASP
710 container.width =
711 NS_MIN(container.width, (nscoord)mMaxSize);
713 else { // multiplicative value
714 container.ascent =
715 NS_MIN(container.ascent, nscoord(initialSize.ascent * mMaxSize));
716 container.descent =
717 NS_MIN(container.descent, nscoord(initialSize.descent * mMaxSize));
718 container.width =
719 NS_MIN(container.width, nscoord(initialSize.width * mMaxSize));
722 if (isVertical && !NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags)) {
723 // re-adjust to align the char with the bottom of the initial container
724 height = container.ascent + container.descent;
725 container.descent = aContainerSize.descent;
726 container.ascent = height - container.descent;
730 if (mMinSize > 0.0f) {
731 // if we are here, there is a user defined minsize ...
732 // always allow the char to stretch in its natural direction,
733 // even if it is different from the caller's direction
734 if (aStretchDirection != NS_STRETCH_DIRECTION_DEFAULT &&
735 aStretchDirection != mEmbellishData.direction) {
736 aStretchDirection = NS_STRETCH_DIRECTION_DEFAULT;
737 // but when we are not honoring the requested direction
738 // we should not use the caller's container size either
739 container = initialSize;
741 if (NS_MATHML_OPERATOR_MINSIZE_IS_ABSOLUTE(mFlags)) {
742 // there is an explicit value like minsize="20pt"
743 // try to maintain the aspect ratio of the char
744 float aspect = mMinSize / float(initialSize.ascent + initialSize.descent);
745 container.ascent =
746 NS_MAX(container.ascent, nscoord(initialSize.ascent * aspect));
747 container.descent =
748 NS_MAX(container.descent, nscoord(initialSize.descent * aspect));
749 container.width =
750 NS_MAX(container.width, (nscoord)mMinSize);
752 else { // multiplicative value
753 container.ascent =
754 NS_MAX(container.ascent, nscoord(initialSize.ascent * mMinSize));
755 container.descent =
756 NS_MAX(container.descent, nscoord(initialSize.descent * mMinSize));
757 container.width =
758 NS_MAX(container.width, nscoord(initialSize.width * mMinSize));
761 if (isVertical && !NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags)) {
762 // re-adjust to align the char with the bottom of the initial container
763 height = container.ascent + container.descent;
764 container.descent = aContainerSize.descent;
765 container.ascent = height - container.descent;
770 // let the MathMLChar stretch itself...
771 nsresult res = mMathMLChar.Stretch(PresContext(), aRenderingContext,
772 aStretchDirection, container, charSize, stretchHint);
773 if (NS_FAILED(res)) {
774 // gracefully handle cases where stretching the char failed (i.e., GetBoundingMetrics failed)
775 // clear our 'form' to behave as if the operator wasn't in the dictionary
776 mFlags &= ~NS_MATHML_OPERATOR_FORM;
777 useMathMLChar = PR_FALSE;
781 // Child frames of invisble operators are not reflowed
782 if (!NS_MATHML_OPERATOR_IS_INVISIBLE(mFlags)) {
783 // Place our children using the default method
784 // This will allow our child text frame to get its DidReflow()
785 nsresult rv = Place(aRenderingContext, PR_TRUE, aDesiredStretchSize);
786 if (NS_MATHML_HAS_ERROR(mPresentationData.flags) || NS_FAILED(rv)) {
787 // Make sure the child frames get their DidReflow() calls.
788 DidReflowChildren(mFrames.FirstChild());
792 if (useMathMLChar) {
793 // update our bounding metrics... it becomes that of our MathML char
794 mBoundingMetrics = charSize;
796 // if the returned direction is 'unsupported', the char didn't actually change.
797 // So we do the centering only if necessary
798 if (mMathMLChar.GetStretchDirection() != NS_STRETCH_DIRECTION_UNSUPPORTED ||
799 NS_MATHML_OPERATOR_IS_CENTERED(mFlags)) {
801 PRBool largeopOnly =
802 (NS_STRETCH_LARGEOP & stretchHint) != 0 &&
803 (NS_STRETCH_VARIABLE_MASK & stretchHint) == 0;
805 if (isVertical || NS_MATHML_OPERATOR_IS_CENTERED(mFlags)) {
806 // the desired size returned by mMathMLChar maybe different
807 // from the size of the container.
808 // the mMathMLChar.mRect.y calculation is subtle, watch out!!!
810 height = mBoundingMetrics.ascent + mBoundingMetrics.descent;
811 if (NS_MATHML_OPERATOR_IS_SYMMETRIC(mFlags) ||
812 NS_MATHML_OPERATOR_IS_CENTERED(mFlags)) {
813 // For symmetric and vertical operators, or for operators that are always
814 // centered ('+', '*', etc) we want to center about the axis of the container
815 mBoundingMetrics.descent = height/2 - axisHeight;
816 } else if (!largeopOnly) {
817 // Align the center of the char with the center of the container
818 mBoundingMetrics.descent = height/2 +
819 (container.ascent + container.descent)/2 - container.ascent;
820 } // else align the baselines
821 mBoundingMetrics.ascent = height - mBoundingMetrics.descent;
826 // Fixup for the final height.
827 // On one hand, our stretchy height can sometimes be shorter than surrounding
828 // ASCII chars, e.g., arrow symbols have |mBoundingMetrics.ascent + leading|
829 // that is smaller than the ASCII's ascent, hence when painting the background
830 // later, it won't look uniform along the line.
831 // On the other hand, sometimes we may leave too much gap when our glyph happens
832 // to come from a font with tall glyphs. For example, since CMEX10 has very tall
833 // glyphs, its natural font metrics are large, even if we pick a small glyph
834 // whose size is comparable to the size of a normal ASCII glyph.
835 // So to avoid uneven spacing in either of these two cases, we use the height
836 // of the ASCII font as a reference and try to match it if possible.
838 // special case for accents... keep them short to improve mouse operations...
839 // an accent can only be the non-first child of <mover>, <munder>, <munderover>
840 PRBool isAccent =
841 NS_MATHML_EMBELLISH_IS_ACCENT(mEmbellishData.flags);
842 if (isAccent) {
843 nsEmbellishData parentData;
844 GetEmbellishDataFrom(mParent, parentData);
845 isAccent =
846 (NS_MATHML_EMBELLISH_IS_ACCENTOVER(parentData.flags) ||
847 NS_MATHML_EMBELLISH_IS_ACCENTUNDER(parentData.flags)) &&
848 parentData.coreFrame != this;
850 if (isAccent && firstChild) {
851 // see bug 188467 for what is going on here
852 nscoord dy = aDesiredStretchSize.ascent - (mBoundingMetrics.ascent + leading);
853 aDesiredStretchSize.ascent = mBoundingMetrics.ascent + leading;
854 aDesiredStretchSize.height = aDesiredStretchSize.ascent + mBoundingMetrics.descent;
856 firstChild->SetPosition(firstChild->GetPosition() - nsPoint(0, dy));
858 else if (useMathMLChar) {
859 nscoord ascent, descent;
860 fm->GetMaxAscent(ascent);
861 fm->GetMaxDescent(descent);
862 aDesiredStretchSize.ascent = NS_MAX(mBoundingMetrics.ascent + leading, ascent);
863 aDesiredStretchSize.height = aDesiredStretchSize.ascent +
864 NS_MAX(mBoundingMetrics.descent + leading, descent);
866 aDesiredStretchSize.width = mBoundingMetrics.width;
867 aDesiredStretchSize.mBoundingMetrics = mBoundingMetrics;
868 mReference.x = 0;
869 mReference.y = aDesiredStretchSize.ascent;
870 // Place our mMathMLChar, its origin is in our coordinate system
871 if (useMathMLChar) {
872 nscoord dy = aDesiredStretchSize.ascent - mBoundingMetrics.ascent;
873 mMathMLChar.SetRect(nsRect(0, dy, charSize.width, charSize.ascent + charSize.descent));
876 // Before we leave... there is a last item in the check-list:
877 // If our parent is not embellished, it means we are the outermost embellished
878 // container and so we put the spacing, otherwise we don't include the spacing,
879 // the outermost embellished container will take care of it.
881 if (!NS_MATHML_OPERATOR_HAS_EMBELLISH_ANCESTOR(mFlags)) {
883 // Account the spacing if we are not an accent with explicit attributes
884 nscoord leftSpace = mEmbellishData.leftSpace;
885 if (isAccent && !NS_MATHML_OPERATOR_HAS_LEFTSPACE_ATTR(mFlags)) {
886 leftSpace = 0;
888 nscoord rightSpace = mEmbellishData.rightSpace;
889 if (isAccent && !NS_MATHML_OPERATOR_HAS_RIGHTSPACE_ATTR(mFlags)) {
890 rightSpace = 0;
893 mBoundingMetrics.width += leftSpace + rightSpace;
894 aDesiredStretchSize.width = mBoundingMetrics.width;
895 aDesiredStretchSize.mBoundingMetrics.width = mBoundingMetrics.width;
897 if (leftSpace) {
898 // adjust the offsets
899 mBoundingMetrics.leftBearing += leftSpace;
900 mBoundingMetrics.rightBearing += leftSpace;
901 aDesiredStretchSize.mBoundingMetrics.leftBearing += leftSpace;
902 aDesiredStretchSize.mBoundingMetrics.rightBearing += leftSpace;
904 if (useMathMLChar) {
905 nsRect rect;
906 mMathMLChar.GetRect(rect);
907 mMathMLChar.SetRect(nsRect(rect.x + leftSpace, rect.y, rect.width, rect.height));
909 else {
910 nsIFrame* childFrame = firstChild;
911 while (childFrame) {
912 childFrame->SetPosition(childFrame->GetPosition()
913 + nsPoint(leftSpace, 0));
914 childFrame = childFrame->GetNextSibling();
920 // Finished with these:
921 ClearSavedChildMetrics();
922 // Set our overflow area
923 GatherAndStoreOverflow(&aDesiredStretchSize);
925 // There used to be code here to change the height of the child frame to
926 // change the caret height, but the text frame that manages the caret is now
927 // not a direct child but wrapped in a block frame. See also bug 412033.
929 return NS_OK;
932 NS_IMETHODIMP
933 nsMathMLmoFrame::InheritAutomaticData(nsIFrame* aParent)
935 // retain our native direction, it only changes if our text content changes
936 nsStretchDirection direction = mEmbellishData.direction;
937 nsMathMLTokenFrame::InheritAutomaticData(aParent);
938 mEmbellishData.direction = direction;
939 return NS_OK;
942 NS_IMETHODIMP
943 nsMathMLmoFrame::TransmitAutomaticData()
945 // this will cause us to re-sync our flags from scratch
946 // but our returned 'form' is still not final (bug 133429), it will
947 // be recomputed to its final value during the next call in Reflow()
948 mEmbellishData.coreFrame = nsnull;
949 ProcessOperatorData();
950 return NS_OK;
953 NS_IMETHODIMP
954 nsMathMLmoFrame::Reflow(nsPresContext* aPresContext,
955 nsHTMLReflowMetrics& aDesiredSize,
956 const nsHTMLReflowState& aReflowState,
957 nsReflowStatus& aStatus)
959 // certain values use units that depend on our style context, so
960 // it is safer to just process the whole lot here
961 ProcessOperatorData();
963 // play safe by not passing invisible operators to the font subsystem because
964 // some platforms risk selecting strange glyphs for them and give bad inter-space
965 if (NS_MATHML_OPERATOR_IS_INVISIBLE(mFlags)) {
966 // return empty space for now, but this is not yet final since there
967 // can be lspace and rspace attributes that reclaim some room.
968 // These will be dealt with later in Stretch().
969 aDesiredSize.width = 0;
970 aDesiredSize.height = 0;
971 aDesiredSize.ascent = 0;
972 aDesiredSize.mBoundingMetrics.Clear();
973 aStatus = NS_FRAME_COMPLETE;
975 NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
976 return NS_OK;
979 return nsMathMLTokenFrame::Reflow(aPresContext, aDesiredSize,
980 aReflowState, aStatus);
983 /* virtual */ void
984 nsMathMLmoFrame::MarkIntrinsicWidthsDirty()
986 // if we get this, it may mean that something changed in the text
987 // content. So blow away everything an re-build the automatic data
988 // from the parent of our outermost embellished container (we ensure
989 // that we are the core, not just a sibling of the core)
991 ProcessTextData();
993 nsIFrame* target = this;
994 nsEmbellishData embellishData;
995 do {
996 target = target->GetParent();
997 GetEmbellishDataFrom(target, embellishData);
998 } while (embellishData.coreFrame == this);
1000 // we have automatic data to update in the children of the target frame
1001 // XXXldb This should really be marking dirty rather than rebuilding
1002 // so that we don't rebuild multiple times for the same change.
1003 RebuildAutomaticDataForChildren(target);
1005 nsMathMLContainerFrame::MarkIntrinsicWidthsDirty();
1008 /* virtual */ nscoord
1009 nsMathMLmoFrame::GetIntrinsicWidth(nsIRenderingContext *aRenderingContext)
1011 ProcessOperatorData();
1012 nscoord width;
1013 if (UseMathMLChar()) {
1014 PRUint32 stretchHint = GetStretchHint(mFlags, mPresentationData, PR_TRUE);
1015 width = mMathMLChar.
1016 GetMaxWidth(PresContext(), *aRenderingContext,
1017 stretchHint, mMaxSize,
1018 NS_MATHML_OPERATOR_MAXSIZE_IS_ABSOLUTE(mFlags));
1020 else {
1021 width = nsMathMLTokenFrame::GetIntrinsicWidth(aRenderingContext);
1024 // leftSpace and rightSpace are actually applied to the outermost
1025 // embellished container but for determining total intrinsic width it should
1026 // be safe to include it for the core here instead.
1027 width += mEmbellishData.leftSpace + mEmbellishData.rightSpace;
1029 return width;
1032 NS_IMETHODIMP
1033 nsMathMLmoFrame::AttributeChanged(PRInt32 aNameSpaceID,
1034 nsIAtom* aAttribute,
1035 PRInt32 aModType)
1037 // check if this is an attribute that can affect the embellished hierarchy
1038 // in a significant way and re-layout the entire hierarchy.
1039 if (nsGkAtoms::accent_ == aAttribute ||
1040 nsGkAtoms::movablelimits_ == aAttribute) {
1042 // set the target as the parent of our outermost embellished container
1043 // (we ensure that we are the core, not just a sibling of the core)
1044 nsIFrame* target = this;
1045 nsEmbellishData embellishData;
1046 do {
1047 target = target->GetParent();
1048 GetEmbellishDataFrom(target, embellishData);
1049 } while (embellishData.coreFrame == this);
1051 // we have automatic data to update in the children of the target frame
1052 return ReLayoutChildren(target);
1055 return nsMathMLTokenFrame::
1056 AttributeChanged(aNameSpaceID, aAttribute, aModType);
1059 // ----------------------
1060 // No need to track the style context given to our MathML char.
1061 // the Style System will use these to pass the proper style context to our MathMLChar
1062 nsStyleContext*
1063 nsMathMLmoFrame::GetAdditionalStyleContext(PRInt32 aIndex) const
1065 switch (aIndex) {
1066 case NS_MATHML_CHAR_STYLE_CONTEXT_INDEX:
1067 return mMathMLChar.GetStyleContext();
1068 default:
1069 return nsnull;
1073 void
1074 nsMathMLmoFrame::SetAdditionalStyleContext(PRInt32 aIndex,
1075 nsStyleContext* aStyleContext)
1077 switch (aIndex) {
1078 case NS_MATHML_CHAR_STYLE_CONTEXT_INDEX:
1079 mMathMLChar.SetStyleContext(aStyleContext);
1080 break;