Back to index

lightning-sunbird  0.9+nobinonly
nsMathMLmsubsupFrame.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla MathML Project.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * The University Of Queensland.
00018  * Portions created by the Initial Developer are Copyright (C) 1999
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Roger B. Sidje <rbs@maths.uq.edu.au>
00023  *   David J. Fiddes <D.J.Fiddes@hw.ac.uk>
00024  *   Shyjan Mahamud <mahamud@cs.cmu.edu> (added TeX rendering rules)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 
00041 #include "nsCOMPtr.h"
00042 #include "nsFrame.h"
00043 #include "nsPresContext.h"
00044 #include "nsUnitConversion.h"
00045 #include "nsStyleContext.h"
00046 #include "nsStyleConsts.h"
00047 #include "nsIRenderingContext.h"
00048 #include "nsIFontMetrics.h"
00049 
00050 #include "nsMathMLmsubsupFrame.h"
00051 
00052 //
00053 // <msubsup> -- attach a subscript-superscript pair to a base - implementation
00054 //
00055 
00056 nsresult
00057 NS_NewMathMLmsubsupFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
00058 {
00059   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00060   if (nsnull == aNewFrame) {
00061     return NS_ERROR_NULL_POINTER;
00062   }
00063   nsMathMLmsubsupFrame* it = new (aPresShell) nsMathMLmsubsupFrame;
00064   if (nsnull == it) {
00065     return NS_ERROR_OUT_OF_MEMORY;
00066   }
00067   *aNewFrame = it;
00068   return NS_OK;
00069 }
00070 
00071 nsMathMLmsubsupFrame::nsMathMLmsubsupFrame()
00072 {
00073 }
00074 
00075 nsMathMLmsubsupFrame::~nsMathMLmsubsupFrame()
00076 {
00077 }
00078 
00079 NS_IMETHODIMP
00080 nsMathMLmsubsupFrame::TransmitAutomaticData()
00081 {
00082   // if our base is an embellished operator, let its state bubble to us
00083   mPresentationData.baseFrame = mFrames.FirstChild();
00084   GetEmbellishDataFrom(mPresentationData.baseFrame, mEmbellishData);
00085 
00086   // 1. The REC says:
00087   //    The <msubsup> element increments scriptlevel by 1, and sets displaystyle to
00088   //    "false", within subscript and superscript, but leaves both attributes
00089   //    unchanged within base.
00090   // 2. The TeXbook (Ch 17. p.141) says the superscript inherits the compression
00091   //    while the subscript is compressed
00092   UpdatePresentationDataFromChildAt(1, -1, 1,
00093     ~NS_MATHML_DISPLAYSTYLE,
00094      NS_MATHML_DISPLAYSTYLE);
00095   UpdatePresentationDataFromChildAt(1,  1, 0,
00096      NS_MATHML_COMPRESSED,
00097      NS_MATHML_COMPRESSED);
00098 
00099   return NS_OK;
00100 }
00101 
00102 NS_IMETHODIMP
00103 nsMathMLmsubsupFrame::Place(nsIRenderingContext& aRenderingContext,
00104                             PRBool               aPlaceOrigin,
00105                             nsHTMLReflowMetrics& aDesiredSize)
00106 {
00107   // extra spacing between base and sup/subscript
00108   nscoord scriptSpace = 0;
00109 
00110   // check if the subscriptshift attribute is there
00111   nsAutoString value;
00112   nscoord subScriptShift = 0;
00113   if (NS_CONTENT_ATTR_HAS_VALUE == GetAttribute(mContent, mPresentationData.mstyle,
00114                    nsMathMLAtoms::subscriptshift_, value)) {
00115     nsCSSValue cssValue;
00116     if (ParseNumericValue(value, cssValue) && cssValue.IsLengthUnit()) {
00117       subScriptShift = CalcLength(GetPresContext(), mStyleContext, cssValue);
00118     }
00119   }
00120   // check if the superscriptshift attribute is there
00121   nscoord supScriptShift = 0;
00122   if (NS_CONTENT_ATTR_HAS_VALUE == GetAttribute(mContent, mPresentationData.mstyle,
00123                    nsMathMLAtoms::superscriptshift_, value)) {
00124     nsCSSValue cssValue;
00125     if (ParseNumericValue(value, cssValue) && cssValue.IsLengthUnit()) {
00126       supScriptShift = CalcLength(GetPresContext(), mStyleContext, cssValue);
00127     }
00128   }
00129 
00130   return nsMathMLmsubsupFrame::PlaceSubSupScript(GetPresContext(),
00131                                                  aRenderingContext,
00132                                                  aPlaceOrigin,
00133                                                  aDesiredSize,
00134                                                  this,
00135                                                  subScriptShift,
00136                                                  supScriptShift,
00137                                                  scriptSpace);
00138 }
00139 
00140 // exported routine that both munderover and msubsup share.
00141 // munderover uses this when movablelimits is set.
00142 nsresult
00143 nsMathMLmsubsupFrame::PlaceSubSupScript(nsPresContext*      aPresContext,
00144                                         nsIRenderingContext& aRenderingContext,
00145                                         PRBool               aPlaceOrigin,
00146                                         nsHTMLReflowMetrics& aDesiredSize,
00147                                         nsIFrame*            aFrame,
00148                                         nscoord              aUserSubScriptShift,
00149                                         nscoord              aUserSupScriptShift,
00150                                         nscoord              aScriptSpace)
00151 {
00152   // the caller better be a mathml frame
00153   nsIMathMLFrame* mathMLFrame;
00154   aFrame->QueryInterface(NS_GET_IID(nsIMathMLFrame), (void**)&mathMLFrame);
00155   if (!mathMLFrame) return NS_ERROR_INVALID_ARG;
00156 
00157   // force the scriptSpace to be atleast 1 pixel
00158   nscoord onePixel = aPresContext->IntScaledPixelsToTwips(1);
00159   aScriptSpace = PR_MAX(onePixel, aScriptSpace);
00160 
00162   // Get the children's desired sizes
00163 
00164   nsHTMLReflowMetrics baseSize (nsnull);
00165   nsHTMLReflowMetrics subScriptSize (nsnull);
00166   nsHTMLReflowMetrics supScriptSize (nsnull);
00167   nsBoundingMetrics bmBase, bmSubScript, bmSupScript;
00168   nsIFrame* subScriptFrame = nsnull;
00169   nsIFrame* supScriptFrame = nsnull;
00170   nsIFrame* baseFrame = aFrame->GetFirstChild(nsnull);
00171   if (baseFrame)
00172     subScriptFrame = baseFrame->GetNextSibling();
00173   if (subScriptFrame)
00174     supScriptFrame = subScriptFrame->GetNextSibling();
00175   if (!baseFrame || !subScriptFrame || !supScriptFrame ||
00176       supScriptFrame->GetNextSibling()) {
00177     // report an error, encourage people to get their markups in order
00178     NS_WARNING("invalid markup");
00179     return NS_STATIC_CAST(nsMathMLContainerFrame*,
00180                           aFrame)->ReflowError(aRenderingContext,
00181                                                aDesiredSize);
00182   }
00183   GetReflowAndBoundingMetricsFor(baseFrame, baseSize, bmBase);
00184   GetReflowAndBoundingMetricsFor(subScriptFrame, subScriptSize, bmSubScript);
00185   GetReflowAndBoundingMetricsFor(supScriptFrame, supScriptSize, bmSupScript);
00186 
00187   // get the subdrop from the subscript font
00188   nscoord subDrop;
00189   GetSubDropFromChild(subScriptFrame, subDrop);
00190   // parameter v, Rule 18a, App. G, TeXbook
00191   nscoord minSubScriptShift = bmBase.descent + subDrop;
00192 
00193   // get the supdrop from the supscript font
00194   nscoord supDrop;
00195   GetSupDropFromChild(supScriptFrame, supDrop);
00196   // parameter u, Rule 18a, App. G, TeXbook
00197   nscoord minSupScriptShift = bmBase.ascent - supDrop;
00198 
00200   // Place Children
00202 
00204   // Get subscript shift
00205   // slightly different from nsMathMLmsubFrame.cpp
00207 
00208   // subScriptShift{1,2}
00209   // = minimum amount to shift the subscript down
00210   // = sub{1,2} in TeXbook
00211   // subScriptShift1 = subscriptshift attribute * x-height
00212   nscoord subScriptShift1, subScriptShift2;
00213 
00214   aRenderingContext.SetFont(baseFrame->GetStyleFont()->mFont, nsnull);
00215   nsCOMPtr<nsIFontMetrics> fm;
00216   aRenderingContext.GetFontMetrics(*getter_AddRefs(fm));
00217 
00218   // get x-height (an ex)
00219   nscoord xHeight;
00220   fm->GetXHeight (xHeight);
00221 
00222   nscoord ruleSize;
00223   GetRuleThickness (aRenderingContext, fm, ruleSize);
00224 
00225   // Get subScriptShift{1,2} default from font
00226   GetSubScriptShifts (fm, subScriptShift1, subScriptShift2);
00227 
00228   if (0 < aUserSubScriptShift) {
00229     // the user has set the subscriptshift attribute
00230     float scaler = ((float) subScriptShift2) / subScriptShift1;
00231     subScriptShift1 = PR_MAX(subScriptShift1, aUserSubScriptShift);
00232     subScriptShift2 = NSToCoordRound(scaler * subScriptShift1);
00233   }
00234 
00235   // get a tentative value for subscriptshift
00236   // Rule 18d, App. G, TeXbook
00237   nscoord subScriptShift =
00238     PR_MAX(minSubScriptShift,PR_MAX(subScriptShift1,subScriptShift2));
00239 
00241   // Get supscript shift
00242   // same code from nsMathMLmsupFrame.cpp
00244 
00245   // get min supscript shift limit from x-height
00246   // = d(x) + 1/4 * sigma_5, Rule 18c, App. G, TeXbook
00247   nscoord minShiftFromXHeight = (nscoord)
00248     (bmSupScript.descent + (1.0f/4.0f) * xHeight);
00249 
00250   // supScriptShift{1,2,3}
00251   // = minimum amount to shift the supscript up
00252   // = sup{1,2,3} in TeX
00253   // supScriptShift1 = superscriptshift attribute * x-height
00254   // Note that there are THREE values for supscript shifts depending
00255   // on the current style
00256   nscoord supScriptShift1, supScriptShift2, supScriptShift3;
00257   // Set supScriptShift{1,2,3} default from font
00258   GetSupScriptShifts (fm, supScriptShift1, supScriptShift2, supScriptShift3);
00259   if (0 < aUserSupScriptShift) {
00260     // the user has set the superscriptshift attribute
00261     float scaler2 = ((float) supScriptShift2) / supScriptShift1;
00262     float scaler3 = ((float) supScriptShift3) / supScriptShift1;
00263     supScriptShift1 = PR_MAX(supScriptShift1, aUserSupScriptShift);
00264     supScriptShift2 = NSToCoordRound(scaler2 * supScriptShift1);
00265     supScriptShift3 = NSToCoordRound(scaler3 * supScriptShift1);
00266   }
00267 
00268   // get sup script shift depending on current script level and display style
00269   // Rule 18c, App. G, TeXbook
00270   nscoord supScriptShift;
00271   nsPresentationData presentationData;
00272   mathMLFrame->GetPresentationData(presentationData);
00273   if ( presentationData.scriptLevel == 0 &&
00274        NS_MATHML_IS_DISPLAYSTYLE(presentationData.flags) &&
00275       !NS_MATHML_IS_COMPRESSED(presentationData.flags)) {
00276     // Style D in TeXbook
00277     supScriptShift = supScriptShift1;
00278   }
00279   else if (NS_MATHML_IS_COMPRESSED(presentationData.flags)) {
00280     // Style C' in TeXbook = D',T',S',SS'
00281     supScriptShift = supScriptShift3;
00282   }
00283   else {
00284     // everything else = T,S,SS
00285     supScriptShift = supScriptShift2;
00286   }
00287 
00288   // get tentative value for superscriptshift
00289   // Rule 18c, App. G, TeXbook
00290   supScriptShift =
00291     PR_MAX(minSupScriptShift,PR_MAX(supScriptShift,minShiftFromXHeight));
00292 
00294   // Negotiate between supScriptShift and subScriptShift
00295   // so that there will be enough gap between them
00296   // Rule 18e, App. G, TeXbook
00298 
00299   nscoord gap =
00300     (supScriptShift - bmSupScript.descent) -
00301     (bmSubScript.ascent - subScriptShift);
00302   if (gap < 4.0f * ruleSize) {
00303     // adjust subScriptShift to get a gap of (4.0 * ruleSize)
00304     subScriptShift += NSToCoordRound ((4.0f * ruleSize) - gap);
00305   }
00306 
00307   // next we want to ensure that the bottom of the superscript
00308   // will be > (4/5) * x-height above baseline
00309   gap = NSToCoordRound ((4.0f/5.0f) * xHeight -
00310                         (supScriptShift - bmSupScript.descent));
00311   if (gap > 0) {
00312     supScriptShift += gap;
00313     subScriptShift -= gap;
00314   }
00315 
00317   // Do the Placing
00319 
00320   // get bounding box for base + subscript + superscript
00321   nsBoundingMetrics boundingMetrics;
00322   boundingMetrics.ascent =
00323     PR_MAX(bmBase.ascent, (bmSupScript.ascent + supScriptShift));
00324   boundingMetrics.descent =
00325    PR_MAX(bmBase.descent, (bmSubScript.descent + subScriptShift));
00326 
00327   // leave aScriptSpace after both super/subscript
00328   // add italicCorrection between base and superscript
00329   // add "a little to spare" as well (see TeXbook Ch.11, p.64), as we
00330   // estimate the italic creation ourselves and it isn't the same as TeX 
00331   nscoord italicCorrection;
00332   GetItalicCorrection(bmBase, italicCorrection);
00333   italicCorrection += onePixel;
00334   boundingMetrics.width = bmBase.width + aScriptSpace +
00335     PR_MAX((italicCorrection + bmSupScript.width), bmSubScript.width);
00336   boundingMetrics.leftBearing = bmBase.leftBearing;
00337   boundingMetrics.rightBearing = bmBase.width +
00338     PR_MAX((italicCorrection + bmSupScript.rightBearing), bmSubScript.rightBearing);
00339   mathMLFrame->SetBoundingMetrics(boundingMetrics);
00340 
00341   // reflow metrics
00342   aDesiredSize.ascent =
00343     PR_MAX(baseSize.ascent, 
00344        PR_MAX(subScriptSize.ascent - subScriptShift,
00345               supScriptSize.ascent + supScriptShift));
00346   aDesiredSize.descent =
00347     PR_MAX(baseSize.descent,
00348        PR_MAX(subScriptSize.descent + subScriptShift, 
00349               supScriptSize.descent - supScriptShift));
00350   aDesiredSize.height = aDesiredSize.ascent + aDesiredSize.descent;
00351   aDesiredSize.width = boundingMetrics.width;
00352   aDesiredSize.mBoundingMetrics = boundingMetrics;
00353 
00354   mathMLFrame->SetReference(nsPoint(0, aDesiredSize.ascent));
00355 
00356   if (aPlaceOrigin) {
00357     nscoord dx, dy;
00358     // now place the base ...
00359     dx = 0; dy = aDesiredSize.ascent - baseSize.ascent;
00360     FinishReflowChild(baseFrame, aPresContext, nsnull,
00361                       baseSize, dx, dy, 0);
00362     // ... and subscript
00363     dx = bmBase.width;
00364     dy = aDesiredSize.ascent - (subScriptSize.ascent - subScriptShift);
00365     FinishReflowChild(subScriptFrame, aPresContext, nsnull,
00366                       subScriptSize, dx, dy, 0);
00367     // ... and the superscript
00368     dx = bmBase.width + italicCorrection;
00369     dy = aDesiredSize.ascent - (supScriptSize.ascent + supScriptShift);
00370     FinishReflowChild(supScriptFrame, aPresContext, nsnull,
00371                       supScriptSize, dx, dy, 0);
00372   }
00373 
00374   return NS_OK;
00375 }