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> (added TeX rendering rules)
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
44 #include "nsPresContext.h"
45 #include "nsStyleContext.h"
46 #include "nsStyleConsts.h"
47 #include "nsIRenderingContext.h"
48 #include "nsIFontMetrics.h"
50 #include "nsMathMLmmultiscriptsFrame.h"
53 // <mmultiscripts> -- attach prescripts and tensor indices to a base - implementation
57 NS_NewMathMLmmultiscriptsFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
59 return new (aPresShell
) nsMathMLmmultiscriptsFrame(aContext
);
62 nsMathMLmmultiscriptsFrame::~nsMathMLmmultiscriptsFrame()
67 nsMathMLmmultiscriptsFrame::TransmitAutomaticData()
69 // if our base is an embellished operator, let its state bubble to us
70 mPresentationData
.baseFrame
= mFrames
.FirstChild();
71 GetEmbellishDataFrom(mPresentationData
.baseFrame
, mEmbellishData
);
74 // The <mmultiscripts> element increments scriptlevel by 1, and sets
75 // displaystyle to "false", within each of its arguments except base
76 UpdatePresentationDataFromChildAt(1, -1,
77 ~NS_MATHML_DISPLAYSTYLE
, NS_MATHML_DISPLAYSTYLE
);
79 // The TeXbook (Ch 17. p.141) says the superscript inherits the compression
80 // while the subscript is compressed. So here we collect subscripts and set
81 // the compression flag in them.
83 PRBool isSubScript
= PR_FALSE
;
84 nsAutoVoidArray subScriptFrames
;
85 nsIFrame
* childFrame
= mFrames
.FirstChild();
87 if (childFrame
->GetContent()->Tag() == nsGkAtoms::mprescripts_
) {
90 else if (0 == count
) {
94 // super/subscript block
97 subScriptFrames
.AppendElement(childFrame
);
102 isSubScript
= !isSubScript
;
105 childFrame
= childFrame
->GetNextSibling();
107 for (PRInt32 i
= subScriptFrames
.Count() - 1; i
>= 0; i
--) {
108 childFrame
= (nsIFrame
*)subScriptFrames
[i
];
109 PropagatePresentationDataFor(childFrame
,
110 NS_MATHML_COMPRESSED
, NS_MATHML_COMPRESSED
);
117 nsMathMLmmultiscriptsFrame::ProcessAttributes()
122 // check if the subscriptshift attribute is there
124 GetAttribute(mContent
, mPresentationData
.mstyle
,
125 nsGkAtoms::subscriptshift_
, value
);
126 if (!value
.IsEmpty()) {
128 if (ParseNumericValue(value
, cssValue
) && cssValue
.IsLengthUnit()) {
129 mSubScriptShift
= CalcLength(PresContext(), mStyleContext
, cssValue
);
132 // check if the superscriptshift attribute is there
133 GetAttribute(mContent
, mPresentationData
.mstyle
,
134 nsGkAtoms::superscriptshift_
, value
);
135 if (!value
.IsEmpty()) {
137 if (ParseNumericValue(value
, cssValue
) && cssValue
.IsLengthUnit()) {
138 mSupScriptShift
= CalcLength(PresContext(), mStyleContext
, cssValue
);
143 /* virtual */ nsresult
144 nsMathMLmmultiscriptsFrame::Place(nsIRenderingContext
& aRenderingContext
,
146 nsHTMLReflowMetrics
& aDesiredSize
)
148 ////////////////////////////////////
149 // Get the children's desired sizes
151 nscoord minShiftFromXHeight
, subDrop
, supDrop
;
153 ////////////////////////////////////////
154 // Initialize super/sub shifts that
155 // depend only on the current font
156 ////////////////////////////////////////
160 // get x-height (an ex)
161 const nsStyleFont
* font
= GetStyleFont();
162 aRenderingContext
.SetFont(font
->mFont
, nsnull
,
163 PresContext()->GetUserFontSet());
164 nsCOMPtr
<nsIFontMetrics
> fm
;
165 aRenderingContext
.GetFontMetrics(*getter_AddRefs(fm
));
168 fm
->GetXHeight (xHeight
);
171 GetRuleThickness (aRenderingContext
, fm
, ruleSize
);
173 // scriptspace from TeX for extra spacing after sup/subscript (0.5pt in plain TeX)
174 // forced to be at least 1 pixel here
175 nscoord onePixel
= nsPresContext::CSSPixelsToAppUnits(1);
176 nscoord scriptSpace
= PR_MAX(PresContext()->PointsToAppUnits(0.5f
), onePixel
);
178 /////////////////////////////////////
179 // first the shift for the subscript
181 // subScriptShift{1,2}
182 // = minimum amount to shift the subscript down
183 // = sub{1,2} in TeXbook
184 // subScriptShift1 = subscriptshift attribute * x-height
185 nscoord subScriptShift1
, subScriptShift2
;
187 // Get subScriptShift{1,2} default from font
188 GetSubScriptShifts (fm
, subScriptShift1
, subScriptShift2
);
189 if (0 < mSubScriptShift
) {
190 // the user has set the subscriptshift attribute
191 float scaler
= ((float) subScriptShift2
) / subScriptShift1
;
192 subScriptShift1
= PR_MAX(subScriptShift1
, mSubScriptShift
);
193 subScriptShift2
= NSToCoordRound(scaler
* subScriptShift1
);
195 // the font dependent shift
196 nscoord subScriptShift
= PR_MAX(subScriptShift1
,subScriptShift2
);
198 /////////////////////////////////////
199 // next the shift for the superscript
201 // supScriptShift{1,2,3}
202 // = minimum amount to shift the supscript up
203 // = sup{1,2,3} in TeX
204 // supScriptShift1 = superscriptshift attribute * x-height
205 // Note that there are THREE values for supscript shifts depending
206 // on the current style
207 nscoord supScriptShift1
, supScriptShift2
, supScriptShift3
;
208 // Set supScriptShift{1,2,3} default from font
209 GetSupScriptShifts (fm
, supScriptShift1
, supScriptShift2
, supScriptShift3
);
210 if (0 < mSupScriptShift
) {
211 // the user has set the superscriptshift attribute
212 float scaler2
= ((float) supScriptShift2
) / supScriptShift1
;
213 float scaler3
= ((float) supScriptShift3
) / supScriptShift1
;
214 supScriptShift1
= PR_MAX(supScriptShift1
, mSupScriptShift
);
215 supScriptShift2
= NSToCoordRound(scaler2
* supScriptShift1
);
216 supScriptShift3
= NSToCoordRound(scaler3
* supScriptShift1
);
219 // get sup script shift depending on current script level and display style
220 // Rule 18c, App. G, TeXbook
221 nscoord supScriptShift
;
222 if ( font
->mScriptLevel
== 0 &&
223 NS_MATHML_IS_DISPLAYSTYLE(mPresentationData
.flags
) &&
224 !NS_MATHML_IS_COMPRESSED(mPresentationData
.flags
)) {
225 // Style D in TeXbook
226 supScriptShift
= supScriptShift1
;
228 else if (NS_MATHML_IS_COMPRESSED(mPresentationData
.flags
)) {
229 // Style C' in TeXbook = D',T',S',SS'
230 supScriptShift
= supScriptShift3
;
233 // everything else = T,S,SS
234 supScriptShift
= supScriptShift2
;
237 ////////////////////////////////////
238 // Get the children's sizes
239 ////////////////////////////////////
241 nscoord width
= 0, prescriptsWidth
= 0, rightBearing
= 0;
242 nsIFrame
* mprescriptsFrame
= nsnull
; // frame of <mprescripts/>, if there.
243 PRBool isSubScript
= PR_FALSE
;
244 nscoord minSubScriptShift
= 0, minSupScriptShift
= 0;
245 nscoord trySubScriptShift
= subScriptShift
;
246 nscoord trySupScriptShift
= supScriptShift
;
247 nscoord maxSubScriptShift
= subScriptShift
;
248 nscoord maxSupScriptShift
= supScriptShift
;
250 nsHTMLReflowMetrics baseSize
;
251 nsHTMLReflowMetrics subScriptSize
;
252 nsHTMLReflowMetrics supScriptSize
;
253 nsIFrame
* baseFrame
= nsnull
;
254 nsIFrame
* subScriptFrame
= nsnull
;
255 nsIFrame
* supScriptFrame
= nsnull
;
257 PRBool firstPrescriptsPair
= PR_FALSE
;
258 nsBoundingMetrics bmBase
, bmSubScript
, bmSupScript
;
259 nscoord italicCorrection
= 0;
261 mBoundingMetrics
.width
= 0;
262 mBoundingMetrics
.ascent
= mBoundingMetrics
.descent
= -0x7FFFFFFF;
263 nscoord ascent
= -0x7FFFFFFF, descent
= -0x7FFFFFFF;
264 aDesiredSize
.width
= aDesiredSize
.height
= 0;
266 nsIFrame
* childFrame
= mFrames
.FirstChild();
268 if (childFrame
->GetContent()->Tag() == nsGkAtoms::mprescripts_
) {
269 if (mprescriptsFrame
) {
270 // duplicate <mprescripts/> found
271 // report an error, encourage people to get their markups in order
272 return ReflowError(aRenderingContext
, aDesiredSize
);
274 mprescriptsFrame
= childFrame
;
275 firstPrescriptsPair
= PR_TRUE
;
280 baseFrame
= childFrame
;
281 GetReflowAndBoundingMetricsFor(baseFrame
, baseSize
, bmBase
);
282 GetItalicCorrection(bmBase
, italicCorrection
);
283 // for the superscript, we always add "a little to spare"
284 italicCorrection
+= onePixel
;
286 // we update mBoundingMetrics.{ascent,descent} with that
287 // of the baseFrame only after processing all the sup/sub pairs
288 // XXX need italic correction only *if* there are postscripts ?
289 mBoundingMetrics
.width
= bmBase
.width
+ italicCorrection
;
290 mBoundingMetrics
.rightBearing
= bmBase
.rightBearing
;
291 mBoundingMetrics
.leftBearing
= bmBase
.leftBearing
; // until overwritten
294 // super/subscript block
297 subScriptFrame
= childFrame
;
298 GetReflowAndBoundingMetricsFor(subScriptFrame
, subScriptSize
, bmSubScript
);
299 // get the subdrop from the subscript font
300 GetSubDropFromChild (subScriptFrame
, subDrop
);
301 // parameter v, Rule 18a, App. G, TeXbook
302 minSubScriptShift
= bmBase
.descent
+ subDrop
;
303 trySubScriptShift
= PR_MAX(minSubScriptShift
,subScriptShift
);
304 mBoundingMetrics
.descent
=
305 PR_MAX(mBoundingMetrics
.descent
,bmSubScript
.descent
);
306 descent
= PR_MAX(descent
,subScriptSize
.height
- subScriptSize
.ascent
);
307 width
= bmSubScript
.width
+ scriptSpace
;
308 rightBearing
= bmSubScript
.rightBearing
;
312 supScriptFrame
= childFrame
;
313 GetReflowAndBoundingMetricsFor(supScriptFrame
, supScriptSize
, bmSupScript
);
314 // get the supdrop from the supscript font
315 GetSupDropFromChild (supScriptFrame
, supDrop
);
316 // parameter u, Rule 18a, App. G, TeXbook
317 minSupScriptShift
= bmBase
.ascent
- supDrop
;
318 // get min supscript shift limit from x-height
319 // = d(x) + 1/4 * sigma_5, Rule 18c, App. G, TeXbook
320 minShiftFromXHeight
= NSToCoordRound
321 ((bmSupScript
.descent
+ (1.0f
/4.0f
) * xHeight
));
323 PR_MAX(minSupScriptShift
,PR_MAX(minShiftFromXHeight
,supScriptShift
));
324 mBoundingMetrics
.ascent
=
325 PR_MAX(mBoundingMetrics
.ascent
,bmSupScript
.ascent
);
326 ascent
= PR_MAX(ascent
,supScriptSize
.ascent
);
327 width
= PR_MAX(width
, bmSupScript
.width
+ scriptSpace
);
328 rightBearing
= PR_MAX(rightBearing
, bmSupScript
.rightBearing
);
330 if (!mprescriptsFrame
) { // we are still looping over base & postscripts
331 mBoundingMetrics
.rightBearing
= mBoundingMetrics
.width
+ rightBearing
;
332 mBoundingMetrics
.width
+= width
;
335 prescriptsWidth
+= width
;
336 if (firstPrescriptsPair
) {
337 firstPrescriptsPair
= PR_FALSE
;
338 mBoundingMetrics
.leftBearing
=
339 PR_MIN(bmSubScript
.leftBearing
, bmSupScript
.leftBearing
);
342 width
= rightBearing
= 0;
344 // negotiate between the various shifts so that
345 // there is enough gap between the sup and subscripts
346 // Rule 18e, App. G, TeXbook
348 (trySupScriptShift
- bmSupScript
.descent
) -
349 (bmSubScript
.ascent
- trySubScriptShift
);
350 if (gap
< 4.0f
* ruleSize
) {
351 // adjust trySubScriptShift to get a gap of (4.0 * ruleSize)
352 trySubScriptShift
+= NSToCoordRound ((4.0f
* ruleSize
) - gap
);
355 // next we want to ensure that the bottom of the superscript
356 // will be > (4/5) * x-height above baseline
357 gap
= NSToCoordRound ((4.0f
/5.0f
) * xHeight
-
358 (trySupScriptShift
- bmSupScript
.descent
));
360 trySupScriptShift
+= gap
;
361 trySubScriptShift
-= gap
;
364 maxSubScriptShift
= PR_MAX(maxSubScriptShift
, trySubScriptShift
);
365 maxSupScriptShift
= PR_MAX(maxSupScriptShift
, trySupScriptShift
);
367 trySubScriptShift
= subScriptShift
;
368 trySupScriptShift
= supScriptShift
;
372 isSubScript
= !isSubScript
;
375 childFrame
= childFrame
->GetNextSibling();
377 // note: width=0 if all sup-sub pairs match correctly
378 if ((0 != width
) || !baseFrame
|| !subScriptFrame
|| !supScriptFrame
) {
379 // report an error, encourage people to get their markups in order
380 return ReflowError(aRenderingContext
, aDesiredSize
);
383 // we left out the width of prescripts, so ...
384 mBoundingMetrics
.rightBearing
+= prescriptsWidth
;
385 mBoundingMetrics
.width
+= prescriptsWidth
;
387 // we left out the base during our bounding box updates, so ...
388 mBoundingMetrics
.ascent
=
389 PR_MAX(mBoundingMetrics
.ascent
+maxSupScriptShift
,bmBase
.ascent
);
390 mBoundingMetrics
.descent
=
391 PR_MAX(mBoundingMetrics
.descent
+maxSubScriptShift
,bmBase
.descent
);
393 // get the reflow metrics ...
394 aDesiredSize
.ascent
=
395 PR_MAX(ascent
+maxSupScriptShift
,baseSize
.ascent
);
396 aDesiredSize
.height
= aDesiredSize
.ascent
+
397 PR_MAX(descent
+maxSubScriptShift
,baseSize
.height
- baseSize
.ascent
);
398 aDesiredSize
.width
= mBoundingMetrics
.width
;
399 aDesiredSize
.mBoundingMetrics
= mBoundingMetrics
;
402 mReference
.y
= aDesiredSize
.ascent
;
407 // Place prescripts, followed by base, and then postscripts.
408 // The list of frames is in the order: {base} {postscripts} {prescripts}
409 // We go over the list in a circular manner, starting at <prescripts/>
412 nscoord dx
= 0, dy
= 0;
415 childFrame
= mprescriptsFrame
;
417 if (!childFrame
) { // end of prescripts,
418 // place the base ...
419 childFrame
= baseFrame
;
420 dy
= aDesiredSize
.ascent
- baseSize
.ascent
;
421 FinishReflowChild (baseFrame
, PresContext(), nsnull
, baseSize
, dx
, dy
, 0);
422 dx
+= bmBase
.width
+ italicCorrection
;
424 else if (mprescriptsFrame
!= childFrame
) {
425 // process each sup/sub pair
427 subScriptFrame
= childFrame
;
430 else if (1 == count
) {
431 supScriptFrame
= childFrame
;
434 // get the ascent/descent of sup/subscripts stored in their rects
435 // rect.x = descent, rect.y = ascent
436 GetReflowAndBoundingMetricsFor(subScriptFrame
, subScriptSize
, bmSubScript
);
437 GetReflowAndBoundingMetricsFor(supScriptFrame
, supScriptSize
, bmSupScript
);
439 // center w.r.t. largest width
440 width
= PR_MAX(subScriptSize
.width
, supScriptSize
.width
);
442 dy
= aDesiredSize
.ascent
- subScriptSize
.ascent
+
444 FinishReflowChild (subScriptFrame
, PresContext(), nsnull
, subScriptSize
,
445 dx
+ (width
-subScriptSize
.width
)/2, dy
, 0);
447 dy
= aDesiredSize
.ascent
- supScriptSize
.ascent
-
449 FinishReflowChild (supScriptFrame
, PresContext(), nsnull
, supScriptSize
,
450 dx
+ (width
-supScriptSize
.width
)/2, dy
, 0);
452 dx
+= width
+ scriptSpace
;
455 childFrame
= childFrame
->GetNextSibling();
456 } while (mprescriptsFrame
!= childFrame
);