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 :
42 : #include "nsCOMPtr.h"
43 : #include "nsFrame.h"
44 : #include "nsPresContext.h"
45 : #include "nsStyleContext.h"
46 : #include "nsStyleConsts.h"
47 :
48 : #include "nsMathMLmsubFrame.h"
49 :
50 : //
51 : // <msub> -- attach a subscript to a base - implementation
52 : //
53 :
54 : nsIFrame*
55 0 : NS_NewMathMLmsubFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
56 : {
57 0 : return new (aPresShell) nsMathMLmsubFrame(aContext);
58 : }
59 :
60 0 : NS_IMPL_FRAMEARENA_HELPERS(nsMathMLmsubFrame)
61 :
62 0 : nsMathMLmsubFrame::~nsMathMLmsubFrame()
63 : {
64 0 : }
65 :
66 : NS_IMETHODIMP
67 0 : nsMathMLmsubFrame::TransmitAutomaticData()
68 : {
69 : // if our base is an embellished operator, let its state bubble to us
70 0 : mPresentationData.baseFrame = mFrames.FirstChild();
71 0 : GetEmbellishDataFrom(mPresentationData.baseFrame, mEmbellishData);
72 :
73 : // 1. The REC says:
74 : // The <msub> element increments scriptlevel by 1, and sets displaystyle to
75 : // "false", within subscript, but leaves both attributes unchanged within base.
76 : // 2. The TeXbook (Ch 17. p.141) says the subscript is compressed
77 : UpdatePresentationDataFromChildAt(1, -1,
78 : ~NS_MATHML_DISPLAYSTYLE | NS_MATHML_COMPRESSED,
79 0 : NS_MATHML_DISPLAYSTYLE | NS_MATHML_COMPRESSED);
80 :
81 0 : return NS_OK;
82 : }
83 :
84 : /* virtual */ nsresult
85 0 : nsMathMLmsubFrame::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 subscriptshift attribute is there
93 0 : nscoord subScriptShift = 0;
94 0 : nsAutoString value;
95 : GetAttribute(mContent, mPresentationData.mstyle,
96 0 : nsGkAtoms::subscriptshift_, value);
97 0 : if (!value.IsEmpty()) {
98 0 : nsCSSValue cssValue;
99 0 : if (ParseNumericValue(value, cssValue) && cssValue.IsLengthUnit()) {
100 0 : subScriptShift = CalcLength(PresContext(), mStyleContext, cssValue);
101 : }
102 : }
103 :
104 : return nsMathMLmsubFrame::PlaceSubScript(PresContext(),
105 : aRenderingContext,
106 : aPlaceOrigin,
107 : aDesiredSize,
108 : this,
109 : subScriptShift,
110 0 : scriptSpace);
111 : }
112 :
113 : // exported routine that both munder and msub share.
114 : // munder uses this when movablelimits is set.
115 : nsresult
116 0 : nsMathMLmsubFrame::PlaceSubScript (nsPresContext* aPresContext,
117 : nsRenderingContext& aRenderingContext,
118 : bool aPlaceOrigin,
119 : nsHTMLReflowMetrics& aDesiredSize,
120 : nsMathMLContainerFrame* aFrame,
121 : nscoord aUserSubScriptShift,
122 : nscoord aScriptSpace)
123 : {
124 : // force the scriptSpace to be atleast 1 pixel
125 0 : aScriptSpace = NS_MAX(nsPresContext::CSSPixelsToAppUnits(1), aScriptSpace);
126 :
127 : ////////////////////////////////////
128 : // Get the children's desired sizes
129 :
130 0 : nsBoundingMetrics bmBase, bmSubScript;
131 0 : nsHTMLReflowMetrics baseSize;
132 0 : nsHTMLReflowMetrics subScriptSize;
133 0 : nsIFrame* baseFrame = aFrame->GetFirstPrincipalChild();
134 0 : nsIFrame* subScriptFrame = nsnull;
135 0 : if (baseFrame)
136 0 : subScriptFrame = baseFrame->GetNextSibling();
137 0 : if (!baseFrame || !subScriptFrame || subScriptFrame->GetNextSibling()) {
138 : // report an error, encourage people to get their markups in order
139 0 : return aFrame->ReflowError(aRenderingContext, aDesiredSize);
140 : }
141 0 : GetReflowAndBoundingMetricsFor(baseFrame, baseSize, bmBase);
142 0 : GetReflowAndBoundingMetricsFor(subScriptFrame, subScriptSize, bmSubScript);
143 :
144 : // get the subdrop from the subscript font
145 : nscoord subDrop;
146 0 : GetSubDropFromChild(subScriptFrame, subDrop);
147 : // parameter v, Rule 18a, App. G, TeXbook
148 0 : nscoord minSubScriptShift = bmBase.descent + subDrop;
149 :
150 : //////////////////
151 : // Place Children
152 :
153 : // get min subscript shift limit from x-height
154 : // = h(x) - 4/5 * sigma_5, Rule 18b, App. G, TeXbook
155 0 : nscoord xHeight = 0;
156 0 : nsRefPtr<nsFontMetrics> fm;
157 0 : nsLayoutUtils::GetFontMetricsForFrame(baseFrame, getter_AddRefs(fm));
158 :
159 0 : xHeight = fm->XHeight();
160 : nscoord minShiftFromXHeight = (nscoord)
161 0 : (bmSubScript.ascent - (4.0f/5.0f) * xHeight);
162 :
163 : // subScriptShift
164 : // = minimum amount to shift the subscript down set by user or from the font
165 : // = sub1 in TeX
166 : // = subscriptshift attribute * x-height
167 : nscoord subScriptShift, dummy;
168 : // get subScriptShift default from font
169 0 : GetSubScriptShifts (fm, subScriptShift, dummy);
170 :
171 : subScriptShift =
172 0 : NS_MAX(subScriptShift, aUserSubScriptShift);
173 :
174 : // get actual subscriptshift to be used
175 : // Rule 18b, App. G, TeXbook
176 : nscoord actualSubScriptShift =
177 0 : NS_MAX(minSubScriptShift,NS_MAX(subScriptShift,minShiftFromXHeight));
178 : // get bounding box for base + subscript
179 0 : nsBoundingMetrics boundingMetrics;
180 : boundingMetrics.ascent =
181 0 : NS_MAX(bmBase.ascent, bmSubScript.ascent - actualSubScriptShift);
182 : boundingMetrics.descent =
183 0 : NS_MAX(bmBase.descent, bmSubScript.descent + actualSubScriptShift);
184 :
185 : // add aScriptSpace to the subscript's width
186 0 : boundingMetrics.width = bmBase.width + bmSubScript.width + aScriptSpace;
187 0 : boundingMetrics.leftBearing = bmBase.leftBearing;
188 : boundingMetrics.rightBearing = NS_MAX(bmBase.rightBearing, bmBase.width +
189 0 : NS_MAX(bmSubScript.width + aScriptSpace, bmSubScript.rightBearing));
190 0 : aFrame->SetBoundingMetrics (boundingMetrics);
191 :
192 : // reflow metrics
193 : aDesiredSize.ascent =
194 0 : NS_MAX(baseSize.ascent, subScriptSize.ascent - actualSubScriptShift);
195 : aDesiredSize.height = aDesiredSize.ascent +
196 : NS_MAX(baseSize.height - baseSize.ascent,
197 0 : subScriptSize.height - subScriptSize.ascent + actualSubScriptShift);
198 0 : aDesiredSize.width = boundingMetrics.width;
199 0 : aDesiredSize.mBoundingMetrics = boundingMetrics;
200 :
201 0 : aFrame->SetReference(nsPoint(0, aDesiredSize.ascent));
202 :
203 0 : if (aPlaceOrigin) {
204 : nscoord dx, dy;
205 : // now place the base ...
206 0 : dx = aFrame->MirrorIfRTL(aDesiredSize.width, baseSize.width, 0);
207 0 : dy = aDesiredSize.ascent - baseSize.ascent;
208 0 : FinishReflowChild (baseFrame, aPresContext, nsnull, baseSize, dx, dy, 0);
209 : // ... and subscript
210 : dx = aFrame->MirrorIfRTL(aDesiredSize.width, subScriptSize.width,
211 0 : bmBase.width);
212 0 : dy = aDesiredSize.ascent - (subScriptSize.ascent - actualSubScriptShift);
213 0 : FinishReflowChild (subScriptFrame, aPresContext, nsnull, subScriptSize, dx, dy, 0);
214 : }
215 :
216 0 : return NS_OK;
217 : }
|