Back to index

lightning-sunbird  0.9+nobinonly
nsFontMetricsPango.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* vim:expandtab:shiftwidth=4:tabstop=4:
00003  */
00004 /* ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is mozilla.org code.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Christopher Blizzard <blizzard@mozilla.org>.  
00021  * Portions created by the Initial Developer are Copyright (C) 2002
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * 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 #include "nsIFontMetrics.h"
00041 #include "nsIFontEnumerator.h"
00042 #include "nsCRT.h"
00043 #include "nsIAtom.h"
00044 #include "nsString.h"
00045 #include "nsVoidArray.h"
00046 #include "nsIFontMetricsGTK.h"
00047 
00048 #include <pango/pango.h>
00049 
00050 class nsFontMetricsPango : public nsIFontMetricsGTK
00051 {
00052 public:
00053     nsFontMetricsPango();
00054     virtual ~nsFontMetricsPango();
00055 
00056     NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00057 
00058     // nsISupports
00059     NS_DECL_ISUPPORTS
00060 
00061     // nsIFontMetrics
00062     NS_IMETHOD  Init                 (const nsFont& aFont, nsIAtom* aLangGroup,
00063                                       nsIDeviceContext *aContext);
00064     NS_IMETHOD  Destroy();
00065     NS_IMETHOD  GetLangGroup         (nsIAtom** aLangGroup);
00066     NS_IMETHOD  GetFontHandle        (nsFontHandle &aHandle);
00067 
00068     NS_IMETHOD  GetXHeight           (nscoord& aResult)
00069                                      { aResult = mXHeight; return NS_OK; };
00070 
00071     NS_IMETHOD GetSuperscriptOffset  (nscoord& aResult)
00072                                      { aResult = mSuperscriptOffset;
00073                                        return NS_OK; };
00074 
00075     NS_IMETHOD GetSubscriptOffset    (nscoord& aResult)
00076                                      { aResult = mSubscriptOffset;
00077                                        return NS_OK; };
00078                               
00079     NS_IMETHOD GetStrikeout          (nscoord& aOffset, nscoord& aSize)
00080                                      { aOffset = mStrikeoutOffset;
00081                                        aSize = mStrikeoutSize; 
00082                                        return NS_OK; };
00083 
00084     NS_IMETHOD GetUnderline          (nscoord& aOffset, nscoord& aSize)
00085                                      { aOffset = mUnderlineOffset;
00086                                        aSize = mUnderlineSize; 
00087                                        return NS_OK; };
00088 
00089     NS_IMETHOD GetHeight             (nscoord &aHeight)
00090                                      { aHeight = mMaxHeight; 
00091                                        return NS_OK; };
00092 
00093     NS_IMETHOD GetNormalLineHeight   (nscoord &aHeight)
00094                                      { aHeight = mEmHeight + mLeading;
00095                                        return NS_OK; };
00096 
00097     NS_IMETHOD GetLeading            (nscoord &aLeading)
00098                                      { aLeading = mLeading; 
00099                                        return NS_OK; };
00100 
00101     NS_IMETHOD GetEmHeight           (nscoord &aHeight)
00102                                      { aHeight = mEmHeight; 
00103                                        return NS_OK; };
00104 
00105     NS_IMETHOD GetEmAscent           (nscoord &aAscent)
00106                                      { aAscent = mEmAscent;
00107                                        return NS_OK; };
00108 
00109     NS_IMETHOD GetEmDescent          (nscoord &aDescent)
00110                                      { aDescent = mEmDescent;
00111                                        return NS_OK; };
00112 
00113     NS_IMETHOD GetMaxHeight          (nscoord &aHeight)
00114                                      { aHeight = mMaxHeight;
00115                                        return NS_OK; };
00116 
00117     NS_IMETHOD GetMaxAscent          (nscoord &aAscent)
00118                                      { aAscent = mMaxAscent;
00119                                        return NS_OK; };
00120 
00121     NS_IMETHOD GetMaxDescent         (nscoord &aDescent)
00122                                      { aDescent = mMaxDescent;
00123                                        return NS_OK; };
00124 
00125     NS_IMETHOD GetMaxAdvance         (nscoord &aAdvance)
00126                                      { aAdvance = mMaxAdvance;
00127                                        return NS_OK; };
00128 
00129     NS_IMETHOD GetSpaceWidth         (nscoord &aSpaceCharWidth)
00130                                      { aSpaceCharWidth = mSpaceWidth;
00131                                        return NS_OK; };
00132 
00133     NS_IMETHOD GetAveCharWidth       (nscoord &aAveCharWidth)
00134                                      { aAveCharWidth = mAveCharWidth;
00135                                        return NS_OK; };
00136 
00137     PRInt32 GetMaxStringLength() { return mMaxStringLength; }
00138 
00139     // nsIFontMetricsGTK (calls from the font rendering layer)
00140     virtual nsresult GetWidth(const char* aString, PRUint32 aLength,
00141                               nscoord& aWidth,
00142                               nsRenderingContextGTK *aContext);
00143     virtual nsresult GetWidth(const PRUnichar* aString, PRUint32 aLength,
00144                               nscoord& aWidth, PRInt32 *aFontID,
00145                               nsRenderingContextGTK *aContext);
00146 
00147     virtual nsresult GetTextDimensions(const PRUnichar* aString,
00148                                        PRUint32 aLength,
00149                                        nsTextDimensions& aDimensions, 
00150                                        PRInt32* aFontID,
00151                                        nsRenderingContextGTK *aContext);
00152     virtual nsresult GetTextDimensions(const char*         aString,
00153                                        PRInt32             aLength,
00154                                        PRInt32             aAvailWidth,
00155                                        PRInt32*            aBreaks,
00156                                        PRInt32             aNumBreaks,
00157                                        nsTextDimensions&   aDimensions,
00158                                        PRInt32&            aNumCharsFit,
00159                                        nsTextDimensions&   aLastWordDimensions,
00160                                        PRInt32*            aFontID,
00161                                        nsRenderingContextGTK *aContext);
00162     virtual nsresult GetTextDimensions(const PRUnichar*    aString,
00163                                        PRInt32             aLength,
00164                                        PRInt32             aAvailWidth,
00165                                        PRInt32*            aBreaks,
00166                                        PRInt32             aNumBreaks,
00167                                        nsTextDimensions&   aDimensions,
00168                                        PRInt32&            aNumCharsFit,
00169                                        nsTextDimensions&   aLastWordDimensions,
00170                                        PRInt32*            aFontID,
00171                                        nsRenderingContextGTK *aContext);
00172 
00173     virtual nsresult DrawString(const char *aString, PRUint32 aLength,
00174                                 nscoord aX, nscoord aY,
00175                                 const nscoord* aSpacing,
00176                                 nsRenderingContextGTK *aContext,
00177                                 nsDrawingSurfaceGTK *aSurface);
00178     virtual nsresult DrawString(const PRUnichar* aString, PRUint32 aLength,
00179                                 nscoord aX, nscoord aY,
00180                                 PRInt32 aFontID,
00181                                 const nscoord* aSpacing,
00182                                 nsRenderingContextGTK *aContext,
00183                                 nsDrawingSurfaceGTK *aSurface);
00184 
00185 #ifdef MOZ_MATHML
00186     virtual nsresult GetBoundingMetrics(const char *aString, PRUint32 aLength,
00187                                         nsBoundingMetrics &aBoundingMetrics,
00188                                         nsRenderingContextGTK *aContext);
00189     virtual nsresult GetBoundingMetrics(const PRUnichar *aString,
00190                                         PRUint32 aLength,
00191                                         nsBoundingMetrics &aBoundingMetrics,
00192                                         PRInt32 *aFontID,
00193                                         nsRenderingContextGTK *aContext);
00194 #endif /* MOZ_MATHML */
00195 
00196     virtual GdkFont* GetCurrentGDKFont(void);
00197 
00198     virtual nsresult SetRightToLeftText(PRBool aIsRTL);
00199     virtual PRBool GetRightToLeftText();
00200 
00201     virtual nsresult GetClusterInfo(const PRUnichar *aText,
00202                                     PRUint32 aLength,
00203                                     PRUint8 *aClusterStarts);
00204 
00205     virtual PRInt32 GetPosition(const PRUnichar *aText,
00206                                 PRUint32 aLength,
00207                                 nsPoint aPt);
00208 
00209     virtual nsresult GetRangeWidth(const PRUnichar *aText,
00210                                    PRUint32 aLength,
00211                                    PRUint32 aStart,
00212                                    PRUint32 aEnd,
00213                                    PRUint32 &aWidth);
00214 
00215     virtual nsresult GetRangeWidth(const char *aText,
00216                                    PRUint32 aLength,
00217                                    PRUint32 aStart,
00218                                    PRUint32 aEnd,
00219                                    PRUint32 &aWidth);
00220 
00221     // get hints for the font
00222     static PRUint32    GetHints     (void);
00223 
00224     // drawing surface methods
00225     static nsresult FamilyExists    (nsIDeviceContext *aDevice,
00226                                      const nsString &aName);
00227 
00228 private:
00229 
00230     // generic font metrics class bits
00231     nsCStringArray       mFontList;
00232     nsAutoVoidArray      mFontIsGeneric;
00233 
00234     nsIDeviceContext    *mDeviceContext;
00235     nsCOMPtr<nsIAtom>    mLangGroup;
00236     nsCString           *mGenericFont;
00237     float                mPointSize;
00238 
00239     nsCAutoString        mDefaultFont;
00240 
00241     // Pango-related items
00242     PangoFontDescription *mPangoFontDesc;
00243     PangoContext         *mPangoContext;
00244     PangoContext         *mLTRPangoContext;
00245     PangoContext         *mRTLPangoContext;
00246     PangoAttrList        *mPangoAttrList;
00247     PRBool                mIsRTL;
00248 
00249     // Cached font metrics
00250     nscoord                  mXHeight;
00251     nscoord                  mSuperscriptOffset;
00252     nscoord                  mSubscriptOffset;
00253     nscoord                  mStrikeoutOffset;
00254     nscoord                  mStrikeoutSize;
00255     nscoord                  mUnderlineOffset;
00256     nscoord                  mUnderlineSize;
00257     nscoord                  mMaxHeight;
00258     nscoord                  mLeading;
00259     nscoord                  mEmHeight;
00260     nscoord                  mEmAscent;
00261     nscoord                  mEmDescent;
00262     nscoord                  mMaxAscent;
00263     nscoord                  mMaxDescent;
00264     nscoord                  mMaxAdvance;
00265     nscoord                  mSpaceWidth;
00266     nscoord                  mPangoSpaceWidth;
00267     nscoord                  mAveCharWidth;
00268     PRInt32                  mMaxStringLength;
00269 
00270     // Private methods
00271     nsresult RealizeFont(void);
00272     nsresult CacheFontMetrics(void);
00273 
00274     static PRBool EnumFontCallback(const nsString &aFamily,
00275                                    PRBool aIsGeneric, void *aData);
00276 
00277     void     DrawStringSlowly(const gchar *aText,
00278                               const PRUnichar *aOrigString,
00279                               PRUint32 aLength,
00280                               GdkDrawable *aDrawable,
00281                               GdkGC *aGC, gint aX, gint aY,
00282                               PangoLayoutLine *aLine,
00283                               const nscoord *aSpacing);
00284 
00285     nsresult GetTextDimensionsInternal(const gchar*        aString,
00286                                        PRInt32             aLength,
00287                                        PRInt32             aAvailWidth,
00288                                        PRInt32*            aBreaks,
00289                                        PRInt32             aNumBreaks,
00290                                        nsTextDimensions&   aDimensions,
00291                                        PRInt32&            aNumCharsFit,
00292                                        nsTextDimensions&   aLastWordDimensions,
00293                                        nsRenderingContextGTK *aContext);
00294 
00295     void FixupSpaceWidths (PangoLayout *aLayout, const char *aString);
00296 };
00297 
00298 class nsFontEnumeratorPango : public nsIFontEnumerator
00299 {
00300 public:
00301     nsFontEnumeratorPango();
00302     NS_DECL_ISUPPORTS
00303     NS_DECL_NSIFONTENUMERATOR
00304 };