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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is Mozilla MathML Project.
16 : *
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.
21 : *
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> (added TeX rendering rules)
26 : *
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.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsCOMPtr.h"
42 : #include "nsFrame.h"
43 : #include "nsPresContext.h"
44 : #include "nsStyleContext.h"
45 : #include "nsStyleConsts.h"
46 :
47 : #include "nsMathMLmsupFrame.h"
48 :
49 : //
50 : // <msup> -- attach a superscript to a base - implementation
51 : //
52 :
53 : nsIFrame*
54 0 : NS_NewMathMLmsupFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
55 : {
56 0 : return new (aPresShell) nsMathMLmsupFrame(aContext);
57 : }
58 :
59 0 : NS_IMPL_FRAMEARENA_HELPERS(nsMathMLmsupFrame)
60 :
61 0 : nsMathMLmsupFrame::~nsMathMLmsupFrame()
62 : {
63 0 : }
64 :
65 : NS_IMETHODIMP
66 0 : nsMathMLmsupFrame::TransmitAutomaticData()
67 : {
68 : // if our base is an embellished operator, its flags bubble to us
69 0 : mPresentationData.baseFrame = mFrames.FirstChild();
70 0 : GetEmbellishDataFrom(mPresentationData.baseFrame, mEmbellishData);
71 :
72 : // 1. The REC says:
73 : // The <msup> element increments scriptlevel by 1, and sets displaystyle to
74 : // "false", within superscript, but leaves both attributes unchanged within base.
75 : // 2. The TeXbook (Ch 17. p.141) says the superscript *inherits* the compression,
76 : // so we don't set the compression flag. Our parent will propagate its own.
77 : UpdatePresentationDataFromChildAt(1, -1,
78 : ~NS_MATHML_DISPLAYSTYLE,
79 0 : NS_MATHML_DISPLAYSTYLE);
80 :
81 0 : return NS_OK;
82 : }
83 :
84 : /* virtual */ nsresult
85 0 : nsMathMLmsupFrame::Place(nsRenderingContext& aRenderingContext,
86 : bool aPlaceOrigin,
87 : nsHTMLReflowMetrics& aDesiredSize)
88 : {
89 : // extra spacing after sup/subscript
90 0 : nscoord scriptSpace = nsPresContext::CSSPointsToAppUnits(0.5f); // 0.5pt as in plain TeX
91 :
92 : // check if the superscriptshift attribute is there
93 0 : nsAutoString value;
94 0 : nscoord supScriptShift = 0;
95 : GetAttribute(mContent, mPresentationData.mstyle,
96 0 : nsGkAtoms::superscriptshift_, value);
97 0 : if (!value.IsEmpty()) {
98 0 : nsCSSValue cssValue;
99 0 : if (ParseNumericValue(value, cssValue) && cssValue.IsLengthUnit()) {
100 0 : supScriptShift = CalcLength(PresContext(), mStyleContext, cssValue);
101 : }
102 : }
103 :
104 : return nsMathMLmsupFrame::PlaceSuperScript(PresContext(),
105 : aRenderingContext,
106 : aPlaceOrigin,
107 : aDesiredSize,
108 : this,
109 : supScriptShift,
110 0 : scriptSpace);
111 : }
112 :
113 : // exported routine that both mover and msup share.
114 : // mover uses this when movablelimits is set.
115 : nsresult
116 0 : nsMathMLmsupFrame::PlaceSuperScript(nsPresContext* aPresContext,
117 : nsRenderingContext& aRenderingContext,
118 : bool aPlaceOrigin,
119 : nsHTMLReflowMetrics& aDesiredSize,
120 : nsMathMLContainerFrame* aFrame,
121 : nscoord aUserSupScriptShift,
122 : nscoord aScriptSpace)
123 : {
124 : // force the scriptSpace to be at least 1 pixel
125 0 : nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);
126 0 : aScriptSpace = NS_MAX(onePixel, aScriptSpace);
127 :
128 : ////////////////////////////////////
129 : // Get the children's desired sizes
130 :
131 0 : nsHTMLReflowMetrics baseSize;
132 0 : nsHTMLReflowMetrics supScriptSize;
133 0 : nsBoundingMetrics bmBase, bmSupScript;
134 0 : nsIFrame* supScriptFrame = nsnull;
135 0 : nsIFrame* baseFrame = aFrame->GetFirstPrincipalChild();
136 0 : if (baseFrame)
137 0 : supScriptFrame = baseFrame->GetNextSibling();
138 0 : if (!baseFrame || !supScriptFrame || supScriptFrame->GetNextSibling()) {
139 : // report an error, encourage people to get their markups in order
140 0 : return aFrame->ReflowError(aRenderingContext, aDesiredSize);
141 : }
142 0 : GetReflowAndBoundingMetricsFor(baseFrame, baseSize, bmBase);
143 0 : GetReflowAndBoundingMetricsFor(supScriptFrame, supScriptSize, bmSupScript);
144 :
145 : // get the supdrop from the supscript font
146 : nscoord supDrop;
147 0 : GetSupDropFromChild(supScriptFrame, supDrop);
148 : // parameter u, Rule 18a, App. G, TeXbook
149 0 : nscoord minSupScriptShift = bmBase.ascent - supDrop;
150 :
151 : //////////////////
152 : // Place Children
153 :
154 : // get min supscript shift limit from x-height
155 : // = d(x) + 1/4 * sigma_5, Rule 18c, App. G, TeXbook
156 0 : nscoord xHeight = 0;
157 0 : nsRefPtr<nsFontMetrics> fm;
158 0 : nsLayoutUtils::GetFontMetricsForFrame(baseFrame, getter_AddRefs(fm));
159 :
160 0 : xHeight = fm->XHeight();
161 : nscoord minShiftFromXHeight = (nscoord)
162 0 : (bmSupScript.descent + (1.0f/4.0f) * xHeight);
163 : nscoord italicCorrection;
164 0 : GetItalicCorrection(bmBase, italicCorrection);
165 :
166 : // supScriptShift{1,2,3}
167 : // = minimum amount to shift the supscript up
168 : // = sup{1,2,3} in TeX
169 : // supScriptShift1 = superscriptshift attribute * x-height
170 : // Note that there are THREE values for supscript shifts depending
171 : // on the current style
172 : nscoord supScriptShift1, supScriptShift2, supScriptShift3;
173 : // Set supScriptShift{1,2,3} default from font
174 0 : GetSupScriptShifts (fm, supScriptShift1, supScriptShift2, supScriptShift3);
175 :
176 0 : if (0 < aUserSupScriptShift) {
177 : // the user has set the superscriptshift attribute
178 0 : float scaler2 = ((float) supScriptShift2) / supScriptShift1;
179 0 : float scaler3 = ((float) supScriptShift3) / supScriptShift1;
180 : supScriptShift1 =
181 0 : NS_MAX(supScriptShift1, aUserSupScriptShift);
182 0 : supScriptShift2 = NSToCoordRound(scaler2 * supScriptShift1);
183 0 : supScriptShift3 = NSToCoordRound(scaler3 * supScriptShift1);
184 : }
185 :
186 : // get sup script shift depending on current script level and display style
187 : // Rule 18c, App. G, TeXbook
188 : nscoord supScriptShift;
189 0 : nsPresentationData presentationData;
190 0 : aFrame->GetPresentationData (presentationData);
191 0 : if ( aFrame->GetStyleFont()->mScriptLevel == 0 &&
192 : NS_MATHML_IS_DISPLAYSTYLE(presentationData.flags) &&
193 0 : !NS_MATHML_IS_COMPRESSED(presentationData.flags)) {
194 : // Style D in TeXbook
195 0 : supScriptShift = supScriptShift1;
196 : }
197 0 : else if (NS_MATHML_IS_COMPRESSED(presentationData.flags)) {
198 : // Style C' in TeXbook = D',T',S',SS'
199 0 : supScriptShift = supScriptShift3;
200 : }
201 : else {
202 : // everything else = T,S,SS
203 0 : supScriptShift = supScriptShift2;
204 : }
205 :
206 : // get actual supscriptshift to be used
207 : // Rule 18c, App. G, TeXbook
208 : nscoord actualSupScriptShift =
209 0 : NS_MAX(minSupScriptShift,NS_MAX(supScriptShift,minShiftFromXHeight));
210 :
211 : // bounding box
212 0 : nsBoundingMetrics boundingMetrics;
213 : boundingMetrics.ascent =
214 0 : NS_MAX(bmBase.ascent, (bmSupScript.ascent + actualSupScriptShift));
215 : boundingMetrics.descent =
216 0 : NS_MAX(bmBase.descent, (bmSupScript.descent - actualSupScriptShift));
217 :
218 : // leave aScriptSpace after superscript
219 : // add italicCorrection between base and superscript
220 : // add "a little to spare" as well (see TeXbook Ch.11, p.64), as we
221 : // estimate the italic creation ourselves and it isn't the same as TeX
222 0 : italicCorrection += onePixel;
223 : boundingMetrics.width = bmBase.width + italicCorrection +
224 0 : bmSupScript.width + aScriptSpace;
225 0 : boundingMetrics.leftBearing = bmBase.leftBearing;
226 : boundingMetrics.rightBearing = bmBase.width + italicCorrection +
227 0 : bmSupScript.rightBearing;
228 0 : aFrame->SetBoundingMetrics(boundingMetrics);
229 :
230 : // reflow metrics
231 : aDesiredSize.ascent =
232 0 : NS_MAX(baseSize.ascent, (supScriptSize.ascent + actualSupScriptShift));
233 : aDesiredSize.height = aDesiredSize.ascent +
234 : NS_MAX(baseSize.height - baseSize.ascent,
235 0 : (supScriptSize.height - supScriptSize.ascent - actualSupScriptShift));
236 0 : aDesiredSize.width = boundingMetrics.width;
237 0 : aDesiredSize.mBoundingMetrics = boundingMetrics;
238 :
239 0 : aFrame->SetReference(nsPoint(0, aDesiredSize.ascent));
240 :
241 0 : if (aPlaceOrigin) {
242 : nscoord dx, dy;
243 : // now place the base ...
244 0 : dx = aFrame->MirrorIfRTL(aDesiredSize.width, baseSize.width, 0);
245 0 : dy = aDesiredSize.ascent - baseSize.ascent;
246 0 : FinishReflowChild (baseFrame, aPresContext, nsnull, baseSize, dx, dy, 0);
247 : // ... and supscript
248 : dx = aFrame->MirrorIfRTL(aDesiredSize.width, supScriptSize.width,
249 0 : bmBase.width + italicCorrection);
250 0 : dy = aDesiredSize.ascent - (supScriptSize.ascent + actualSupScriptShift);
251 0 : FinishReflowChild (supScriptFrame, aPresContext, nsnull, supScriptSize, dx, dy, 0);
252 : }
253 :
254 0 : return NS_OK;
255 : }
|