Back to index

lightning-sunbird  0.9+nobinonly
nsMathMLmrootFrame.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  *   Vilya Harvey <vilya@nag.co.uk>
00025  *   Shyjan Mahamud <mahamud@cs.cmu.edu>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 
00042 #include "nsCOMPtr.h"
00043 #include "nsFrame.h"
00044 #include "nsPresContext.h"
00045 #include "nsUnitConversion.h"
00046 #include "nsStyleContext.h"
00047 #include "nsStyleConsts.h"
00048 #include "nsIRenderingContext.h"
00049 #include "nsIFontMetrics.h"
00050 
00051 #include "nsMathMLmrootFrame.h"
00052 
00053 //
00054 // <msqrt> and <mroot> -- form a radical - implementation
00055 //
00056 
00057 //NOTE:
00058 //  The code assumes that TeX fonts are picked.
00059 //  There is no fall-back to draw the branches of the sqrt explicitly
00060 //  in the case where TeX fonts are not there. In general, there are no
00061 //  fall-back(s) in MathML when some (freely-downloadable) fonts are missing.
00062 //  Otherwise, this will add much work and unnecessary complexity to the core
00063 //  MathML  engine. Assuming that authors have the free fonts is part of the
00064 //  deal. We are not responsible for cases of misconfigurations out there.
00065 
00066 // additional style context to be used by our MathMLChar.
00067 #define NS_SQR_CHAR_STYLE_CONTEXT_INDEX   0
00068 
00069 static const PRUnichar kSqrChar = PRUnichar(0x221A);
00070 
00071 nsresult
00072 NS_NewMathMLmrootFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
00073 {
00074   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00075   if (nsnull == aNewFrame) {
00076     return NS_ERROR_NULL_POINTER;
00077   }
00078   nsMathMLmrootFrame* it = new (aPresShell) nsMathMLmrootFrame;
00079   if (nsnull == it) {
00080     return NS_ERROR_OUT_OF_MEMORY;
00081   }
00082   *aNewFrame = it;
00083   return NS_OK;
00084 }
00085 
00086 nsMathMLmrootFrame::nsMathMLmrootFrame() :
00087   mSqrChar(),
00088   mBarRect()
00089 {
00090 }
00091 
00092 nsMathMLmrootFrame::~nsMathMLmrootFrame()
00093 {
00094 }
00095 
00096 NS_IMETHODIMP
00097 nsMathMLmrootFrame::Init(nsPresContext*  aPresContext,
00098                          nsIContent*      aContent,
00099                          nsIFrame*        aParent,
00100                          nsStyleContext*  aContext,
00101                          nsIFrame*        aPrevInFlow)
00102 {
00103   nsresult rv = nsMathMLContainerFrame::Init(aPresContext, aContent, aParent,
00104                                              aContext, aPrevInFlow);
00105 
00106   // No need to tract the style context given to our MathML char. 
00107   // The Style System will use Get/SetAdditionalStyleContext() to keep it
00108   // up-to-date if dynamic changes arise.
00109   nsAutoString sqrChar; sqrChar.Assign(kSqrChar);
00110   mSqrChar.SetData(aPresContext, sqrChar);
00111   ResolveMathMLCharStyle(aPresContext, mContent, mStyleContext, &mSqrChar, PR_TRUE);
00112 
00113   return rv;
00114 }
00115 
00116 NS_IMETHODIMP
00117 nsMathMLmrootFrame::TransmitAutomaticData()
00118 {
00119   // 1. The REC says:
00120   //    The <mroot> element increments scriptlevel by 2, and sets displaystyle to
00121   //    "false", within index, but leaves both attributes unchanged within base.
00122   // 2. The TeXbook (Ch 17. p.141) says \sqrt is compressed
00123   UpdatePresentationDataFromChildAt(1, 1, 2,
00124     ~NS_MATHML_DISPLAYSTYLE | NS_MATHML_COMPRESSED,
00125      NS_MATHML_DISPLAYSTYLE | NS_MATHML_COMPRESSED);
00126   UpdatePresentationDataFromChildAt(0, 0, 0,
00127      NS_MATHML_COMPRESSED, NS_MATHML_COMPRESSED);
00128 
00129   return NS_OK;
00130 }
00131 
00132 NS_IMETHODIMP
00133 nsMathMLmrootFrame::Paint(nsPresContext*      aPresContext,
00134                           nsIRenderingContext& aRenderingContext,
00135                           const nsRect&        aDirtyRect,
00136                           nsFramePaintLayer    aWhichLayer,
00137                           PRUint32             aFlags)
00138 {
00140   // paint the content we are square-rooting
00141   nsresult rv = nsMathMLContainerFrame::Paint(aPresContext, aRenderingContext, 
00142                                               aDirtyRect, aWhichLayer);
00144   // paint the sqrt symbol
00145   if (!NS_MATHML_HAS_ERROR(mPresentationData.flags)) {
00146     mSqrChar.Paint(aPresContext, aRenderingContext,
00147                    aDirtyRect, aWhichLayer, this);
00148 
00149     if (NS_FRAME_PAINT_LAYER_FOREGROUND == aWhichLayer &&
00150         mStyleContext->GetStyleVisibility()->IsVisible() &&
00151         !mBarRect.IsEmpty()) {
00152       // paint the overline bar
00153       const nsStyleColor* color = GetStyleColor();
00154       aRenderingContext.SetColor(color->mColor);
00155       aRenderingContext.FillRect(mBarRect);
00156     }
00157 
00158 #if defined(NS_DEBUG) && defined(SHOW_BOUNDING_BOX)
00159     // for visual debug
00160     if (NS_MATHML_PAINT_BOUNDING_METRICS(mPresentationData.flags)) {
00161       nsRect rect;
00162       mSqrChar.GetRect(rect);
00163 
00164       nsBoundingMetrics bm;
00165       mSqrChar.GetBoundingMetrics(bm);
00166 
00167       aRenderingContext.SetColor(NS_RGB(255,0,0));
00168       nscoord x = rect.x + bm.leftBearing;
00169       nscoord y = rect.y;
00170       nscoord w = bm.rightBearing - bm.leftBearing;
00171       nscoord h = bm.ascent + bm.descent;
00172       aRenderingContext.DrawRect(x,y,w,h);
00173     }
00174 #endif
00175   }
00176 
00177   return rv;
00178 }
00179 
00180 NS_IMETHODIMP
00181 nsMathMLmrootFrame::Reflow(nsPresContext*          aPresContext,
00182                            nsHTMLReflowMetrics&     aDesiredSize,
00183                            const nsHTMLReflowState& aReflowState,
00184                            nsReflowStatus&          aStatus)
00185 {
00186   nsresult rv = NS_OK;
00187   // ask our children to compute their bounding metrics 
00188   nsHTMLReflowMetrics childDesiredSize(aDesiredSize.mComputeMEW,
00189                       aDesiredSize.mFlags | NS_REFLOW_CALC_BOUNDING_METRICS);
00190   nsSize availSize(aReflowState.mComputedWidth, aReflowState.mComputedHeight);
00191   nsReflowStatus childStatus;
00192 
00193   aDesiredSize.width = aDesiredSize.height = 0;
00194   aDesiredSize.ascent = aDesiredSize.descent = 0;
00195 
00196   nsBoundingMetrics bmSqr, bmBase, bmIndex;
00197   nsIRenderingContext& renderingContext = *aReflowState.rendContext;
00198 
00200   // Reflow Children
00201 
00202   PRInt32 count = 0;
00203   nsIFrame* baseFrame = nsnull;
00204   nsIFrame* indexFrame = nsnull;
00205   nsHTMLReflowMetrics baseSize(nsnull);
00206   nsHTMLReflowMetrics indexSize(nsnull);
00207   nsIFrame* childFrame = mFrames.FirstChild();
00208   while (childFrame) {
00209     nsReflowReason reason = (childFrame->GetStateBits() & NS_FRAME_FIRST_REFLOW)
00210       ? eReflowReason_Initial : aReflowState.reason;
00211     nsHTMLReflowState childReflowState(aPresContext, aReflowState,
00212                                        childFrame, availSize, reason);
00213     rv = ReflowChild(childFrame, aPresContext,
00214                      childDesiredSize, childReflowState, childStatus);
00215     //NS_ASSERTION(NS_FRAME_IS_COMPLETE(childStatus), "bad status");
00216     if (NS_FAILED(rv)) return rv;
00217     if (0 == count) {
00218       // base 
00219       baseFrame = childFrame;
00220       baseSize = childDesiredSize;
00221       bmBase = childDesiredSize.mBoundingMetrics;
00222     }
00223     else if (1 == count) {
00224       // index
00225       indexFrame = childFrame;
00226       indexSize = childDesiredSize;
00227       bmIndex = childDesiredSize.mBoundingMetrics;
00228     }
00229     count++;
00230     childFrame = childFrame->GetNextSibling();
00231   }
00232   if (aDesiredSize.mComputeMEW) {
00233     aDesiredSize.mMaxElementWidth = childDesiredSize.mMaxElementWidth;
00234   }
00235   if (2 != count) {
00236     // report an error, encourage people to get their markups in order
00237     NS_WARNING("invalid markup");
00238     rv = ReflowError(renderingContext, aDesiredSize);
00239     aStatus = NS_FRAME_COMPLETE;
00240     NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
00241     return rv;
00242   }
00243 
00245   // Prepare the radical symbol and the overline bar
00246 
00247   renderingContext.SetFont(GetStyleFont()->mFont, nsnull);
00248   nsCOMPtr<nsIFontMetrics> fm;
00249   renderingContext.GetFontMetrics(*getter_AddRefs(fm));
00250 
00251   nscoord ruleThickness, leading, em;
00252   GetRuleThickness(renderingContext, fm, ruleThickness);
00253 
00254   nsBoundingMetrics bmOne;
00255   renderingContext.GetBoundingMetrics(NS_LITERAL_STRING("1").get(), 1, bmOne);
00256 
00257   // get the leading to be left at the top of the resulting frame
00258   // this seems more reliable than using fm->GetLeading() on suspicious fonts               
00259   GetEmHeight(fm, em);
00260   leading = nscoord(0.2f * em); 
00261 
00262   // Rule 11, App. G, TeXbook
00263   // psi = clearance between rule and content
00264   nscoord phi = 0, psi = 0;
00265   if (NS_MATHML_IS_DISPLAYSTYLE(mPresentationData.flags))
00266     fm->GetXHeight(phi);
00267   else
00268     phi = ruleThickness;
00269   psi = ruleThickness + phi/4;
00270 
00271   // built-in: adjust clearance psi to emulate \mathstrut using '1' (TexBook, p.131)
00272   if (bmOne.ascent > bmBase.ascent)
00273     psi += bmOne.ascent - bmBase.ascent;
00274 
00275   // Stretch the radical symbol to the appropriate height if it is not big enough.
00276   nsBoundingMetrics contSize = bmBase;
00277   contSize.descent = bmBase.ascent + bmBase.descent + psi;
00278   contSize.ascent = ruleThickness;
00279 
00280   // height(radical) should be >= height(base) + psi + ruleThickness
00281   nsBoundingMetrics radicalSize;
00282   mSqrChar.Stretch(aPresContext, renderingContext,
00283                    NS_STRETCH_DIRECTION_VERTICAL, 
00284                    contSize, radicalSize,
00285                    NS_STRETCH_LARGER);
00286   // radicalSize have changed at this point, and should match with
00287   // the bounding metrics of the char
00288   mSqrChar.GetBoundingMetrics(bmSqr);
00289 
00290   // According to TeX, the ascent of the returned radical should be
00291   // the thickness of the overline
00292   ruleThickness = bmSqr.ascent;
00293   // make sure that the rule appears on on screen
00294   nscoord onePixel = aPresContext->IntScaledPixelsToTwips(1);
00295   if (ruleThickness < onePixel) {
00296     ruleThickness = onePixel;
00297   }
00298 
00299   // adjust clearance psi to get an exact number of pixels -- this
00300   // gives a nicer & uniform look on stacked radicals (bug 130282)
00301   nscoord delta = psi % onePixel;
00302   if (delta)
00303     psi += onePixel - delta; // round up
00304 
00305   // Update the desired size for the container (like msqrt, index is not yet included)
00306   // the baseline will be that of the base.
00307   mBoundingMetrics.ascent = bmBase.ascent + psi + ruleThickness;
00308   mBoundingMetrics.descent = 
00309     PR_MAX(bmBase.descent, (bmSqr.descent - (bmBase.ascent + psi)));
00310   mBoundingMetrics.width = bmSqr.width + bmBase.width;
00311   mBoundingMetrics.leftBearing = bmSqr.leftBearing;
00312   mBoundingMetrics.rightBearing = bmSqr.width + 
00313     PR_MAX(bmBase.width, bmBase.rightBearing); // take also care of the rule
00314 
00315   aDesiredSize.ascent = mBoundingMetrics.ascent + leading;
00316   aDesiredSize.descent =
00317     PR_MAX(baseSize.descent, (mBoundingMetrics.descent + ruleThickness));
00318   aDesiredSize.height = aDesiredSize.ascent + aDesiredSize.descent;
00319   aDesiredSize.width = mBoundingMetrics.width;
00320 
00322   // Re-adjust the desired size to include the index.
00323   
00324   // the index is raised by some fraction of the height
00325   // of the radical, see \mroot macro in App. B, TexBook
00326   nscoord raiseIndexDelta = NSToCoordRound(0.6f * (bmSqr.ascent + bmSqr.descent));
00327   nscoord indexRaisedAscent = mBoundingMetrics.ascent // top of radical 
00328     - (bmSqr.ascent + bmSqr.descent) // to bottom of radical
00329     + raiseIndexDelta + bmIndex.ascent + bmIndex.descent; // to top of raised index
00330 
00331   nscoord indexClearance = 0;
00332   if (mBoundingMetrics.ascent < indexRaisedAscent) {
00333     indexClearance = 
00334       indexRaisedAscent - mBoundingMetrics.ascent; // excess gap introduced by a tall index 
00335     mBoundingMetrics.ascent = indexRaisedAscent;
00336     aDesiredSize.ascent = mBoundingMetrics.ascent + leading;
00337     aDesiredSize.height = aDesiredSize.ascent + aDesiredSize.descent;
00338   }
00339 
00340   // the index is tucked in closer to the radical while making sure
00341   // that the kern does not make the index and radical collide
00342   nscoord dxIndex, dxSqr, dx, dy;
00343   nscoord xHeight = 0;
00344   fm->GetXHeight(xHeight);
00345   nscoord indexRadicalKern = NSToCoordRound(1.35f * xHeight);
00346   if (indexRadicalKern > bmIndex.width) {
00347     dxIndex = indexRadicalKern - bmIndex.width;
00348     dxSqr = 0;
00349   }
00350   else {
00351     dxIndex = 0;
00352     dxSqr = bmIndex.width - indexRadicalKern;
00353   }
00354   // avoid collision by leaving a minimun space between index and radical
00355   nscoord minimumClearance = bmSqr.width/2;
00356   if (dxIndex + bmIndex.width + minimumClearance > dxSqr + bmSqr.width) {
00357     if (bmIndex.width + minimumClearance < bmSqr.width) {
00358       dxIndex = bmSqr.width - (bmIndex.width + minimumClearance);
00359       dxSqr = 0;
00360     }
00361     else {
00362       dxIndex = 0;
00363       dxSqr = (bmIndex.width + minimumClearance) - bmSqr.width;
00364     }
00365   }
00366 
00367   // place the index
00368   dx = dxIndex;
00369   dy = aDesiredSize.ascent - (indexRaisedAscent + indexSize.ascent - bmIndex.ascent);
00370   FinishReflowChild(indexFrame, aPresContext, nsnull, indexSize, dx, dy, 0);
00371 
00372   // place the radical symbol and the radical bar
00373   dx = dxSqr;
00374   dy = indexClearance + leading; // leave a leading at the top
00375   mSqrChar.SetRect(nsRect(dx, dy, bmSqr.width, bmSqr.ascent + bmSqr.descent));
00376   dx += bmSqr.width;
00377   mBarRect.SetRect(dx, dy, bmBase.width, ruleThickness);
00378 
00379   // place the base
00380   dy = aDesiredSize.ascent - baseSize.ascent;
00381   FinishReflowChild(baseFrame, aPresContext, nsnull, baseSize, dx, dy, 0);
00382 
00383   mReference.x = 0;
00384   mReference.y = aDesiredSize.ascent;
00385 
00386   mBoundingMetrics.width = dx + bmBase.width;
00387   mBoundingMetrics.leftBearing = 
00388     PR_MIN(dxIndex + bmIndex.leftBearing, dxSqr + bmSqr.leftBearing);
00389   mBoundingMetrics.rightBearing = dx +
00390     PR_MAX(bmBase.width, bmBase.rightBearing);
00391 
00392   aDesiredSize.width = mBoundingMetrics.width;
00393   aDesiredSize.mBoundingMetrics = mBoundingMetrics;
00394 
00395   if (aDesiredSize.mComputeMEW) {
00396     aDesiredSize.mMaxElementWidth = aDesiredSize.width;
00397   }
00398   aStatus = NS_FRAME_COMPLETE;
00399   NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
00400   return NS_OK;
00401 }
00402 
00403 
00404 // ----------------------
00405 // the Style System will use these to pass the proper style context to our MathMLChar
00406 nsStyleContext*
00407 nsMathMLmrootFrame::GetAdditionalStyleContext(PRInt32 aIndex) const
00408 {
00409   switch (aIndex) {
00410   case NS_SQR_CHAR_STYLE_CONTEXT_INDEX:
00411     return mSqrChar.GetStyleContext();
00412     break;
00413   default:
00414     return nsnull;
00415   }
00416 }
00417 
00418 void
00419 nsMathMLmrootFrame::SetAdditionalStyleContext(PRInt32          aIndex, 
00420                                               nsStyleContext*  aStyleContext)
00421 {
00422   switch (aIndex) {
00423   case NS_SQR_CHAR_STYLE_CONTEXT_INDEX:
00424     mSqrChar.SetStyleContext(aStyleContext);
00425     break;
00426   }
00427 }