Back to index

lightning-sunbird  0.9+nobinonly
nsMathMLChar.h
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  *   Shyjan Mahamud <mahamud@cs.cmu.edu>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef nsMathMLChar_h___
00040 #define nsMathMLChar_h___
00041 
00042 #include "nsMathMLOperators.h"
00043 #include "nsMathMLFrame.h"
00044 
00045 class nsGlyphTable;
00046 
00047 // Hints for Stretch() to indicate criteria for stretching
00048 #define NS_STRETCH_NORMAL  0x00000001 // try to stretch to requested size - DEFAULT
00049 #define NS_STRETCH_NEARER  0x00000002 // stretch very close to requested size
00050 #define NS_STRETCH_SMALLER 0x00000004 // don't stretch more than requested size
00051 #define NS_STRETCH_LARGER  0x00000008 // don't stretch less than requested size
00052 #define NS_STRETCH_LARGEOP 0x00000010 // for a largeop in displaystyle
00053 
00054 // A single glyph in our internal representation is characterized by a 'code@font' 
00055 // pair. The 'code' is interpreted as a Unicode point or as the direct glyph index
00056 // (depending on the type of nsGlyphTable where this comes from). The 'font' is a
00057 // numeric identifier given to the font to which the glyph belongs.
00058 struct nsGlyphCode {
00059   PRUnichar code; 
00060   PRInt32   font;
00061 
00062   // conversion operator to just return the code point that we generally want
00063   operator PRUnichar () {
00064     return code;
00065   }
00066 };
00067 
00068 // Class used to handle stretchy symbols (accent, delimiter and boundary symbols).
00069 // There are composite characters that need to be built recursively from other
00070 // characters. Since these are rare we use a light-weight mechanism to handle
00071 // them. Specifically, as need arises we append a singly-linked list of child
00072 // chars with their mParent pointing to the first element in the list, except in
00073 // the originating first element itself where it points to null. mSibling points
00074 // to the next element in the list. Since the originating first element is the
00075 // parent of the others, we call it the "root" char of the list. Testing !mParent
00076 // tells whether you are that "root" during the recursion. The parent delegates
00077 // most of the tasks to the children.
00078 class nsMathMLChar
00079 {
00080 public:
00081   // constructor and destructor
00082   nsMathMLChar(nsMathMLChar* aParent = nsnull) {
00083     MOZ_COUNT_CTOR(nsMathMLChar);
00084     mStyleContext = nsnull;
00085     mSibling = nsnull;
00086     mParent = aParent;
00087   }
00088 
00089   ~nsMathMLChar() { // not a virtual destructor: this class is not intended to be subclassed
00090     MOZ_COUNT_DTOR(nsMathMLChar);
00091     // there is only one style context owned by the "root" char
00092     // and it may be used by child chars as well
00093     if (!mParent && mStyleContext) { // only the "root" need to release it
00094       mStyleContext->Release();
00095     }
00096     if (mSibling) {
00097       delete mSibling;
00098     }
00099   }
00100 
00101   nsresult
00102   Paint(nsPresContext*      aPresContext,
00103         nsIRenderingContext& aRenderingContext,
00104         const nsRect&        aDirtyRect,
00105         nsFramePaintLayer    aWhichLayer,
00106         nsIFrame*            aForFrame,
00107         const nsRect*        aSelectedRect = nsnull);
00108 
00109   // This is the method called to ask the char to stretch itself.
00110   // @param aContainerSize - IN - suggested size for the stretched char
00111   // @param aDesiredStretchSize - OUT - the size that the char wants
00112   nsresult
00113   Stretch(nsPresContext*      aPresContext,
00114           nsIRenderingContext& aRenderingContext,
00115           nsStretchDirection   aStretchDirection,
00116           nsBoundingMetrics&   aContainerSize,
00117           nsBoundingMetrics&   aDesiredStretchSize,
00118           PRUint32             aStretchHint = NS_STRETCH_NORMAL);
00119 
00120   void
00121   SetData(nsPresContext* aPresContext,
00122           nsString&       aData);
00123 
00124   void
00125   GetData(nsString& aData) {
00126     aData = mData;
00127   }
00128 
00129   PRInt32
00130   Length() {
00131     return mData.Length();
00132   }
00133 
00134   nsStretchDirection
00135   GetStretchDirection() {
00136     return mDirection;
00137   }
00138 
00139   // Sometimes we only want to pass the data to another routine,
00140   // this function helps to avoid copying
00141   const PRUnichar*
00142   get() {
00143     return mData.get();
00144   }
00145 
00146   void
00147   GetRect(nsRect& aRect) {
00148     aRect = mRect;
00149   }
00150 
00151   void
00152   SetRect(const nsRect& aRect) {
00153     mRect = aRect;
00154     // shift the orgins of child chars if any 
00155     if (!mParent && mSibling) { // only a "root" having child chars can enter here
00156       for (nsMathMLChar* child = mSibling; child; child = child->mSibling) {
00157         nsRect rect; 
00158         child->GetRect(rect);
00159         rect.MoveBy(mRect.x, mRect.y);
00160         child->SetRect(rect);
00161       }
00162     }
00163   }
00164 
00165   // Metrics that _exactly_ enclose the char. The char *must* have *already*
00166   // being stretched before you can call the GetBoundingMetrics() method.
00167   // IMPORTANT: since chars have their own style contexts, and may be rendered
00168   // with glyphs that are not in the parent font, just calling the default
00169   // aRenderingContext.GetBoundingMetrics(aChar) can give incorrect results.
00170   void
00171   GetBoundingMetrics(nsBoundingMetrics& aBoundingMetrics) {
00172     aBoundingMetrics = mBoundingMetrics;
00173   }
00174 
00175   void
00176   SetBoundingMetrics(nsBoundingMetrics& aBoundingMetrics) {
00177     mBoundingMetrics = aBoundingMetrics;
00178   }
00179 
00180   // Hooks to access the extra leaf style contexts given to the MathMLChars.
00181   // They provide an interface to make them acessible to the Style System via
00182   // the Get/Set AdditionalStyleContext() APIs. Owners of MathMLChars
00183   // should honor these APIs.
00184   nsStyleContext* GetStyleContext() const;
00185 
00186   void SetStyleContext(nsStyleContext* aStyleContext);
00187 
00188 protected:
00189   friend class nsGlyphTable;
00190   nsString           mData;
00191 
00192   // support for handling composite stretchy chars like TeX over/under braces
00193   nsMathMLChar*      mSibling;
00194   nsMathMLChar*      mParent;
00195 
00196 private:
00197   nsRect             mRect;
00198   PRInt32            mOperator;
00199   nsStretchDirection mDirection;
00200   nsBoundingMetrics  mBoundingMetrics;
00201   nsStyleContext*    mStyleContext;
00202   nsGlyphTable*      mGlyphTable;
00203   nsGlyphCode        mGlyph;
00204 
00205   // helper methods
00206   nsresult
00207   ComposeChildren(nsPresContext*      aPresContext,
00208                   nsIRenderingContext& aRenderingContext,
00209                   nsGlyphTable*        aGlyphTable,
00210                   nsBoundingMetrics&   aContainerSize,
00211                   nsBoundingMetrics&   aCompositeSize,
00212                   PRUint32             aStretchHint);
00213 
00214   static nsresult
00215   PaintVertically(nsPresContext*      aPresContext,
00216                   nsIRenderingContext& aRenderingContext,
00217                   nsFont&              aFont,
00218                   nsStyleContext*      aStyleContext,
00219                   nsGlyphTable*        aGlyphTable,
00220                   nsMathMLChar*        aChar,
00221                   nsRect&              aRect);
00222 
00223   static nsresult
00224   PaintHorizontally(nsPresContext*      aPresContext,
00225                     nsIRenderingContext& aRenderingContext,
00226                     nsFont&              aFont,
00227                     nsStyleContext*      aStyleContext,
00228                     nsGlyphTable*        aGlyphTable,
00229                     nsMathMLChar*        aChar,
00230                     nsRect&              aRect);
00231 };
00232 
00233 #endif /* nsMathMLChar_h___ */