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 "nsMathMLmsubsupFrame.h"
53 // <msubsup> -- attach a subscript-superscript pair to a base - implementation
57 NS_NewMathMLmsubsupFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
59 return new (aPresShell
) nsMathMLmsubsupFrame(aContext
);
62 NS_IMPL_FRAMEARENA_HELPERS(nsMathMLmsubsupFrame
)
64 nsMathMLmsubsupFrame::~nsMathMLmsubsupFrame()
69 nsMathMLmsubsupFrame::TransmitAutomaticData()
71 // if our base is an embellished operator, let its state bubble to us
72 mPresentationData
.baseFrame
= mFrames
.FirstChild();
73 GetEmbellishDataFrom(mPresentationData
.baseFrame
, mEmbellishData
);
76 // The <msubsup> element increments scriptlevel by 1, and sets displaystyle to
77 // "false", within subscript and superscript, but leaves both attributes
78 // unchanged within base.
79 // 2. The TeXbook (Ch 17. p.141) says the superscript inherits the compression
80 // while the subscript is compressed
81 UpdatePresentationDataFromChildAt(1, -1,
82 ~NS_MATHML_DISPLAYSTYLE
,
83 NS_MATHML_DISPLAYSTYLE
);
84 UpdatePresentationDataFromChildAt(1, 1,
86 NS_MATHML_COMPRESSED
);
91 /* virtual */ nsresult
92 nsMathMLmsubsupFrame::Place(nsIRenderingContext
& aRenderingContext
,
94 nsHTMLReflowMetrics
& aDesiredSize
)
96 // extra spacing between base and sup/subscript
97 nscoord scriptSpace
= 0;
99 // check if the subscriptshift attribute is there
101 nscoord subScriptShift
= 0;
102 GetAttribute(mContent
, mPresentationData
.mstyle
,
103 nsGkAtoms::subscriptshift_
, value
);
104 if (!value
.IsEmpty()) {
106 if (ParseNumericValue(value
, cssValue
) && cssValue
.IsLengthUnit()) {
107 subScriptShift
= CalcLength(PresContext(), mStyleContext
, cssValue
);
110 // check if the superscriptshift attribute is there
111 nscoord supScriptShift
= 0;
112 GetAttribute(mContent
, mPresentationData
.mstyle
,
113 nsGkAtoms::superscriptshift_
, value
);
114 if (!value
.IsEmpty()) {
116 if (ParseNumericValue(value
, cssValue
) && cssValue
.IsLengthUnit()) {
117 supScriptShift
= CalcLength(PresContext(), mStyleContext
, cssValue
);
121 return nsMathMLmsubsupFrame::PlaceSubSupScript(PresContext(),
131 // exported routine that both munderover and msubsup share.
132 // munderover uses this when movablelimits is set.
134 nsMathMLmsubsupFrame::PlaceSubSupScript(nsPresContext
* aPresContext
,
135 nsIRenderingContext
& aRenderingContext
,
137 nsHTMLReflowMetrics
& aDesiredSize
,
138 nsMathMLContainerFrame
* aFrame
,
139 nscoord aUserSubScriptShift
,
140 nscoord aUserSupScriptShift
,
141 nscoord aScriptSpace
)
143 // force the scriptSpace to be atleast 1 pixel
144 nscoord onePixel
= nsPresContext::CSSPixelsToAppUnits(1);
145 aScriptSpace
= NS_MAX(onePixel
, aScriptSpace
);
147 ////////////////////////////////////
148 // Get the children's desired sizes
150 nsHTMLReflowMetrics baseSize
;
151 nsHTMLReflowMetrics subScriptSize
;
152 nsHTMLReflowMetrics supScriptSize
;
153 nsBoundingMetrics bmBase
, bmSubScript
, bmSupScript
;
154 nsIFrame
* subScriptFrame
= nsnull
;
155 nsIFrame
* supScriptFrame
= nsnull
;
156 nsIFrame
* baseFrame
= aFrame
->GetFirstChild(nsnull
);
158 subScriptFrame
= baseFrame
->GetNextSibling();
160 supScriptFrame
= subScriptFrame
->GetNextSibling();
161 if (!baseFrame
|| !subScriptFrame
|| !supScriptFrame
||
162 supScriptFrame
->GetNextSibling()) {
163 // report an error, encourage people to get their markups in order
164 return aFrame
->ReflowError(aRenderingContext
, aDesiredSize
);
166 GetReflowAndBoundingMetricsFor(baseFrame
, baseSize
, bmBase
);
167 GetReflowAndBoundingMetricsFor(subScriptFrame
, subScriptSize
, bmSubScript
);
168 GetReflowAndBoundingMetricsFor(supScriptFrame
, supScriptSize
, bmSupScript
);
170 // get the subdrop from the subscript font
172 GetSubDropFromChild(subScriptFrame
, subDrop
);
173 // parameter v, Rule 18a, App. G, TeXbook
174 nscoord minSubScriptShift
= bmBase
.descent
+ subDrop
;
176 // get the supdrop from the supscript font
178 GetSupDropFromChild(supScriptFrame
, supDrop
);
179 // parameter u, Rule 18a, App. G, TeXbook
180 nscoord minSupScriptShift
= bmBase
.ascent
- supDrop
;
186 //////////////////////////////////////////////////
187 // Get subscript shift
188 // slightly different from nsMathMLmsubFrame.cpp
189 //////////////////////////////////////////////////
191 // subScriptShift{1,2}
192 // = minimum amount to shift the subscript down
193 // = sub{1,2} in TeXbook
194 // subScriptShift1 = subscriptshift attribute * x-height
195 nscoord subScriptShift1
, subScriptShift2
;
197 aRenderingContext
.SetFont(baseFrame
->GetStyleFont()->mFont
,
198 aPresContext
->GetUserFontSet());
199 nsCOMPtr
<nsIFontMetrics
> fm
;
200 aRenderingContext
.GetFontMetrics(*getter_AddRefs(fm
));
202 // get x-height (an ex)
204 fm
->GetXHeight (xHeight
);
207 GetRuleThickness (aRenderingContext
, fm
, ruleSize
);
209 // Get subScriptShift{1,2} default from font
210 GetSubScriptShifts (fm
, subScriptShift1
, subScriptShift2
);
212 if (0 < aUserSubScriptShift
) {
213 // the user has set the subscriptshift attribute
214 float scaler
= ((float) subScriptShift2
) / subScriptShift1
;
215 subScriptShift1
= NS_MAX(subScriptShift1
, aUserSubScriptShift
);
216 subScriptShift2
= NSToCoordRound(scaler
* subScriptShift1
);
219 // get a tentative value for subscriptshift
220 // Rule 18d, App. G, TeXbook
221 nscoord subScriptShift
=
222 NS_MAX(minSubScriptShift
,NS_MAX(subScriptShift1
,subScriptShift2
));
224 //////////////////////////////////////////////////
225 // Get supscript shift
226 // same code from nsMathMLmsupFrame.cpp
227 //////////////////////////////////////////////////
229 // get min supscript shift limit from x-height
230 // = d(x) + 1/4 * sigma_5, Rule 18c, App. G, TeXbook
231 nscoord minShiftFromXHeight
= (nscoord
)
232 (bmSupScript
.descent
+ (1.0f
/4.0f
) * xHeight
);
234 // supScriptShift{1,2,3}
235 // = minimum amount to shift the supscript up
236 // = sup{1,2,3} in TeX
237 // supScriptShift1 = superscriptshift attribute * x-height
238 // Note that there are THREE values for supscript shifts depending
239 // on the current style
240 nscoord supScriptShift1
, supScriptShift2
, supScriptShift3
;
241 // Set supScriptShift{1,2,3} default from font
242 GetSupScriptShifts (fm
, supScriptShift1
, supScriptShift2
, supScriptShift3
);
243 if (0 < aUserSupScriptShift
) {
244 // the user has set the superscriptshift attribute
245 float scaler2
= ((float) supScriptShift2
) / supScriptShift1
;
246 float scaler3
= ((float) supScriptShift3
) / supScriptShift1
;
247 supScriptShift1
= NS_MAX(supScriptShift1
, aUserSupScriptShift
);
248 supScriptShift2
= NSToCoordRound(scaler2
* supScriptShift1
);
249 supScriptShift3
= NSToCoordRound(scaler3
* supScriptShift1
);
252 // get sup script shift depending on current script level and display style
253 // Rule 18c, App. G, TeXbook
254 nscoord supScriptShift
;
255 nsPresentationData presentationData
;
256 aFrame
->GetPresentationData(presentationData
);
257 if ( aFrame
->GetStyleFont()->mScriptLevel
== 0 &&
258 NS_MATHML_IS_DISPLAYSTYLE(presentationData
.flags
) &&
259 !NS_MATHML_IS_COMPRESSED(presentationData
.flags
)) {
260 // Style D in TeXbook
261 supScriptShift
= supScriptShift1
;
263 else if (NS_MATHML_IS_COMPRESSED(presentationData
.flags
)) {
264 // Style C' in TeXbook = D',T',S',SS'
265 supScriptShift
= supScriptShift3
;
268 // everything else = T,S,SS
269 supScriptShift
= supScriptShift2
;
272 // get tentative value for superscriptshift
273 // Rule 18c, App. G, TeXbook
275 NS_MAX(minSupScriptShift
,NS_MAX(supScriptShift
,minShiftFromXHeight
));
277 //////////////////////////////////////////////////
278 // Negotiate between supScriptShift and subScriptShift
279 // so that there will be enough gap between them
280 // Rule 18e, App. G, TeXbook
281 //////////////////////////////////////////////////
284 (supScriptShift
- bmSupScript
.descent
) -
285 (bmSubScript
.ascent
- subScriptShift
);
286 if (gap
< 4.0f
* ruleSize
) {
287 // adjust subScriptShift to get a gap of (4.0 * ruleSize)
288 subScriptShift
+= NSToCoordRound ((4.0f
* ruleSize
) - gap
);
291 // next we want to ensure that the bottom of the superscript
292 // will be > (4/5) * x-height above baseline
293 gap
= NSToCoordRound ((4.0f
/5.0f
) * xHeight
-
294 (supScriptShift
- bmSupScript
.descent
));
296 supScriptShift
+= gap
;
297 subScriptShift
-= gap
;
300 //////////////////////////////////////////////////
302 //////////////////////////////////////////////////
304 // get bounding box for base + subscript + superscript
305 nsBoundingMetrics boundingMetrics
;
306 boundingMetrics
.ascent
=
307 NS_MAX(bmBase
.ascent
, (bmSupScript
.ascent
+ supScriptShift
));
308 boundingMetrics
.descent
=
309 NS_MAX(bmBase
.descent
, (bmSubScript
.descent
+ subScriptShift
));
311 // leave aScriptSpace after both super/subscript
312 // add italicCorrection between base and superscript
313 // add "a little to spare" as well (see TeXbook Ch.11, p.64), as we
314 // estimate the italic creation ourselves and it isn't the same as TeX
315 nscoord italicCorrection
;
316 GetItalicCorrection(bmBase
, italicCorrection
);
317 italicCorrection
+= onePixel
;
318 boundingMetrics
.width
= bmBase
.width
+ aScriptSpace
+
319 NS_MAX((italicCorrection
+ bmSupScript
.width
), bmSubScript
.width
);
320 boundingMetrics
.leftBearing
= bmBase
.leftBearing
;
321 boundingMetrics
.rightBearing
= bmBase
.width
+
322 NS_MAX((italicCorrection
+ bmSupScript
.rightBearing
), bmSubScript
.rightBearing
);
323 aFrame
->SetBoundingMetrics(boundingMetrics
);
326 aDesiredSize
.ascent
=
327 NS_MAX(baseSize
.ascent
,
328 NS_MAX(subScriptSize
.ascent
- subScriptShift
,
329 supScriptSize
.ascent
+ supScriptShift
));
330 aDesiredSize
.height
= aDesiredSize
.ascent
+
331 NS_MAX(baseSize
.height
- baseSize
.ascent
,
332 NS_MAX(subScriptSize
.height
- subScriptSize
.ascent
+ subScriptShift
,
333 supScriptSize
.height
- subScriptSize
.ascent
- supScriptShift
));
334 aDesiredSize
.width
= boundingMetrics
.width
;
335 aDesiredSize
.mBoundingMetrics
= boundingMetrics
;
337 aFrame
->SetReference(nsPoint(0, aDesiredSize
.ascent
));
341 // now place the base ...
342 dx
= 0; dy
= aDesiredSize
.ascent
- baseSize
.ascent
;
343 FinishReflowChild(baseFrame
, aPresContext
, nsnull
,
344 baseSize
, dx
, dy
, 0);
347 dy
= aDesiredSize
.ascent
- (subScriptSize
.ascent
- subScriptShift
);
348 FinishReflowChild(subScriptFrame
, aPresContext
, nsnull
,
349 subScriptSize
, dx
, dy
, 0);
350 // ... and the superscript
351 dx
= bmBase
.width
+ italicCorrection
;
352 dy
= aDesiredSize
.ascent
- (supScriptSize
.ascent
+ supScriptShift
);
353 FinishReflowChild(supScriptFrame
, aPresContext
, nsnull
,
354 supScriptSize
, dx
, dy
, 0);