Back to index

lightning-sunbird  0.9+nobinonly
nsMathMLmsqrtFrame.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 "nsMathMLmsqrtFrame.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_NewMathMLmsqrtFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
00073 {
00074   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00075   if (nsnull == aNewFrame) {
00076     return NS_ERROR_NULL_POINTER;
00077   }
00078   nsMathMLmsqrtFrame* it = new (aPresShell) nsMathMLmsqrtFrame;
00079   if (nsnull == it) {
00080     return NS_ERROR_OUT_OF_MEMORY;
00081   }
00082   *aNewFrame = it;
00083   return NS_OK;
00084 }
00085 
00086 nsMathMLmsqrtFrame::nsMathMLmsqrtFrame() :
00087   mSqrChar(),
00088   mBarRect()
00089 {
00090 }
00091 
00092 nsMathMLmsqrtFrame::~nsMathMLmsqrtFrame()
00093 {
00094 }
00095 
00096 NS_IMETHODIMP
00097 nsMathMLmsqrtFrame::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 nsMathMLmsqrtFrame::InheritAutomaticData(nsIFrame* aParent)
00118 {
00119   // let the base class get the default from our parent
00120   nsMathMLContainerFrame::InheritAutomaticData(aParent);
00121 
00122   mPresentationData.flags |= NS_MATHML_STRETCH_ALL_CHILDREN_VERTICALLY;
00123 
00124   return NS_OK;
00125 }
00126 
00127 
00128 NS_IMETHODIMP
00129 nsMathMLmsqrtFrame::TransmitAutomaticData()
00130 {
00131   // 1. The REC says:
00132   //    The <msqrt> element leaves both attributes [displaystyle and scriptlevel]
00133   //    unchanged within all its arguments.
00134   // 2. The TeXBook (Ch 17. p.141) says that \sqrt is cramped 
00135   UpdatePresentationDataFromChildAt(0, -1, 0,
00136      NS_MATHML_COMPRESSED,
00137      NS_MATHML_COMPRESSED);
00138 
00139   return NS_OK;
00140 }
00141 
00142 NS_IMETHODIMP
00143 nsMathMLmsqrtFrame::Paint(nsPresContext*      aPresContext,
00144                           nsIRenderingContext& aRenderingContext,
00145                           const nsRect&        aDirtyRect,
00146                           nsFramePaintLayer    aWhichLayer,
00147                           PRUint32             aFlags)
00148 {
00150   // paint the content we are square-rooting
00151   nsresult rv = nsMathMLContainerFrame::Paint(aPresContext, aRenderingContext, 
00152                                               aDirtyRect, aWhichLayer);
00154   // paint the sqrt symbol
00155   if (!NS_MATHML_HAS_ERROR(mPresentationData.flags)) {
00156     mSqrChar.Paint(aPresContext, aRenderingContext,
00157                    aDirtyRect, aWhichLayer, this);
00158 
00159     if (NS_FRAME_PAINT_LAYER_FOREGROUND == aWhichLayer &&
00160         mStyleContext->GetStyleVisibility()->IsVisible() &&
00161         !mBarRect.IsEmpty()) {
00162       // paint the overline bar
00163       const nsStyleColor* color = GetStyleColor();
00164       aRenderingContext.SetColor(color->mColor);
00165       aRenderingContext.FillRect(mBarRect);
00166     }
00167 
00168 #if defined(NS_DEBUG) && defined(SHOW_BOUNDING_BOX)
00169     // for visual debug
00170     if (NS_MATHML_PAINT_BOUNDING_METRICS(mPresentationData.flags)) {
00171       nsRect rect;
00172       mSqrChar.GetRect(rect);
00173 
00174       nsBoundingMetrics bm;
00175       mSqrChar.GetBoundingMetrics(bm);
00176 
00177       aRenderingContext.SetColor(NS_RGB(255,0,0));
00178       nscoord x = rect.x + bm.leftBearing;
00179       nscoord y = rect.y;
00180       nscoord w = bm.rightBearing - bm.leftBearing;
00181       nscoord h = bm.ascent + bm.descent;
00182       aRenderingContext.DrawRect(x,y,w,h);
00183     }
00184 #endif
00185   }
00186 
00187   return rv;
00188 }
00189 
00190 NS_IMETHODIMP
00191 nsMathMLmsqrtFrame::Reflow(nsPresContext*          aPresContext,
00192                            nsHTMLReflowMetrics&     aDesiredSize,
00193                            const nsHTMLReflowState& aReflowState,
00194                            nsReflowStatus&          aStatus)
00195 {
00197   // Let the base class format our content like an inferred mrow
00198   nsHTMLReflowMetrics baseSize(aDesiredSize);
00199   nsresult rv = nsMathMLContainerFrame::Reflow(aPresContext, baseSize,
00200                                                aReflowState, aStatus);
00201   //NS_ASSERTION(NS_FRAME_IS_COMPLETE(aStatus), "bad status");
00202   if (NS_FAILED(rv)) return rv;
00203 
00204   nsBoundingMetrics bmSqr, bmBase;
00205   bmBase = baseSize.mBoundingMetrics;
00206 
00208   // Prepare the radical symbol and the overline bar
00209 
00210   nsIRenderingContext& renderingContext = *aReflowState.rendContext;
00211   renderingContext.SetFont(GetStyleFont()->mFont, nsnull);
00212   nsCOMPtr<nsIFontMetrics> fm;
00213   renderingContext.GetFontMetrics(*getter_AddRefs(fm));
00214 
00215   nscoord ruleThickness, leading, em;
00216   GetRuleThickness(renderingContext, fm, ruleThickness);
00217 
00218   nsBoundingMetrics bmOne;
00219   renderingContext.GetBoundingMetrics(NS_LITERAL_STRING("1").get(), 1, bmOne);
00220 
00221   // get the leading to be left at the top of the resulting frame
00222   // this seems more reliable than using fm->GetLeading() on suspicious fonts               
00223   GetEmHeight(fm, em);
00224   leading = nscoord(0.2f * em); 
00225 
00226   // Rule 11, App. G, TeXbook
00227   // psi = clearance between rule and content
00228   nscoord phi = 0, psi = 0;
00229   if (NS_MATHML_IS_DISPLAYSTYLE(mPresentationData.flags))
00230     fm->GetXHeight(phi);
00231   else
00232     phi = ruleThickness;
00233   psi = ruleThickness + phi/4;
00234 
00235   // built-in: adjust clearance psi to emulate \mathstrut using '1' (TexBook, p.131)
00236   if (bmOne.ascent > bmBase.ascent)
00237     psi += bmOne.ascent - bmBase.ascent;
00238 
00239   // Stretch the radical symbol to the appropriate height if it is not big enough.
00240   nsBoundingMetrics contSize = bmBase;
00241   contSize.ascent = ruleThickness;
00242   contSize.descent = bmBase.ascent + bmBase.descent + psi;
00243 
00244   // height(radical) should be >= height(base) + psi + ruleThickness
00245   nsBoundingMetrics radicalSize;
00246   mSqrChar.Stretch(aPresContext, renderingContext,
00247                    NS_STRETCH_DIRECTION_VERTICAL, 
00248                    contSize, radicalSize,
00249                    NS_STRETCH_LARGER);
00250   // radicalSize have changed at this point, and should match with
00251   // the bounding metrics of the char
00252   mSqrChar.GetBoundingMetrics(bmSqr);
00253 
00254   // According to TeX, the ascent of the returned radical should be
00255   // the thickness of the overline
00256   ruleThickness = bmSqr.ascent;
00257   // make sure that the rule appears on the screen
00258   nscoord onePixel = aPresContext->IntScaledPixelsToTwips(1);
00259   if (ruleThickness < onePixel) {
00260     ruleThickness = onePixel;
00261   }
00262 
00263   // adjust clearance psi to get an exact number of pixels -- this
00264   // gives a nicer & uniform look on stacked radicals (bug 130282)
00265   nscoord delta = psi % onePixel;
00266   if (delta)
00267     psi += onePixel - delta; // round up
00268 
00269   nscoord dx = 0, dy = 0;
00270   // place the radical symbol and the radical bar
00271   dy = leading; // leave a leading at the top
00272   mSqrChar.SetRect(nsRect(dx, dy, bmSqr.width, bmSqr.ascent + bmSqr.descent));
00273   dx = bmSqr.width;
00274   mBarRect.SetRect(dx, dy, bmBase.width, ruleThickness);
00275 
00276   // Update the desired size for the container.
00277   // the baseline will be that of the base.
00278   mBoundingMetrics.ascent = bmBase.ascent + psi + ruleThickness;
00279   mBoundingMetrics.descent = 
00280     PR_MAX(bmBase.descent, (bmSqr.descent - (bmBase.ascent + psi)));
00281   mBoundingMetrics.width = bmSqr.width + bmBase.width;
00282   mBoundingMetrics.leftBearing = bmSqr.leftBearing;
00283   mBoundingMetrics.rightBearing = bmSqr.width + 
00284     PR_MAX(bmBase.width, bmBase.rightBearing); // take also care of the rule
00285 
00286   aDesiredSize.ascent = mBoundingMetrics.ascent + leading;
00287   aDesiredSize.descent =
00288     PR_MAX(baseSize.descent, (mBoundingMetrics.descent + ruleThickness));
00289   aDesiredSize.height = aDesiredSize.ascent + aDesiredSize.descent;
00290   aDesiredSize.width = mBoundingMetrics.width;
00291   aDesiredSize.mBoundingMetrics = mBoundingMetrics;
00292 
00293   mReference.x = 0;
00294   mReference.y = aDesiredSize.ascent;
00295 
00297   // Adjust the origins to leave room for the sqrt char and the overline bar
00298 
00299   dx = radicalSize.width;
00300   dy = aDesiredSize.ascent - baseSize.ascent;
00301   nsIFrame* childFrame = mFrames.FirstChild();
00302   while (childFrame) {
00303     childFrame->SetPosition(childFrame->GetPosition() + nsPoint(dx, dy));
00304     childFrame = childFrame->GetNextSibling();
00305   }
00306 
00307   if (aDesiredSize.mComputeMEW) {
00308     aDesiredSize.mMaxElementWidth = aDesiredSize.width;
00309   }
00310   aDesiredSize.mBoundingMetrics = mBoundingMetrics;
00311   aStatus = NS_FRAME_COMPLETE;
00312   NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
00313   return NS_OK;
00314 }
00315 
00316 nscoord
00317 nsMathMLmsqrtFrame::FixInterFrameSpacing(nsHTMLReflowMetrics& aDesiredSize)
00318 {
00319   nscoord gap = nsMathMLContainerFrame::FixInterFrameSpacing(aDesiredSize);
00320   if (!gap) return 0;
00321 
00322   nsRect rect;
00323   mSqrChar.GetRect(rect);
00324   rect.MoveBy(gap, 0);
00325   mSqrChar.SetRect(rect);
00326   mBarRect.MoveBy(gap, 0);
00327   return gap;
00328 }
00329 
00330 // ----------------------
00331 // the Style System will use these to pass the proper style context to our MathMLChar
00332 nsStyleContext*
00333 nsMathMLmsqrtFrame::GetAdditionalStyleContext(PRInt32 aIndex) const
00334 {
00335   switch (aIndex) {
00336   case NS_SQR_CHAR_STYLE_CONTEXT_INDEX:
00337     return mSqrChar.GetStyleContext();
00338     break;
00339   default:
00340     return nsnull;
00341   }
00342 }
00343 
00344 void
00345 nsMathMLmsqrtFrame::SetAdditionalStyleContext(PRInt32          aIndex, 
00346                                               nsStyleContext*  aStyleContext)
00347 {
00348   switch (aIndex) {
00349   case NS_SQR_CHAR_STYLE_CONTEXT_INDEX:
00350     mSqrChar.SetStyleContext(aStyleContext);
00351     break;
00352   }
00353 }