Back to index

lightning-sunbird  0.9+nobinonly
nsFontMetricsXft.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 <X11/Xlib.h>
00049 #include <X11/Xft/Xft.h>
00050 
00051 class nsFontXft;
00052 class nsFontMetricsXft;
00053 
00054 typedef nsresult (nsFontMetricsXft::*GlyphEnumeratorCallback)
00055                                             (const FcChar32 *aString, 
00056                                              PRUint32 aLen, nsFontXft *aFont, 
00057                                              void *aData);
00058 
00059 class nsFontMetricsXft : public nsIFontMetricsGTK
00060 {
00061 public:
00062     nsFontMetricsXft();
00063     virtual ~nsFontMetricsXft();
00064 
00065     NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00066 
00067     // nsISupports
00068     NS_DECL_ISUPPORTS
00069 
00070     // nsIFontMetrics
00071     NS_IMETHOD  Init                 (const nsFont& aFont, nsIAtom* aLangGroup,
00072                                       nsIDeviceContext *aContext);
00073     NS_IMETHOD  Destroy();
00074     NS_IMETHOD  GetLangGroup         (nsIAtom** aLangGroup);
00075     NS_IMETHOD  GetFontHandle        (nsFontHandle &aHandle);
00076 
00077     NS_IMETHOD  GetXHeight           (nscoord& aResult)
00078                                      { aResult = mXHeight; return NS_OK; };
00079 
00080     NS_IMETHOD GetSuperscriptOffset  (nscoord& aResult)
00081                                      { aResult = mSuperscriptOffset;
00082                                        return NS_OK; };
00083 
00084     NS_IMETHOD GetSubscriptOffset    (nscoord& aResult)
00085                                      { aResult = mSubscriptOffset;
00086                                        return NS_OK; };
00087                               
00088     NS_IMETHOD GetStrikeout          (nscoord& aOffset, nscoord& aSize)
00089                                      { aOffset = mStrikeoutOffset;
00090                                        aSize = mStrikeoutSize; 
00091                                        return NS_OK; };
00092 
00093     NS_IMETHOD GetUnderline          (nscoord& aOffset, nscoord& aSize)
00094                                      { aOffset = mUnderlineOffset;
00095                                        aSize = mUnderlineSize; 
00096                                        return NS_OK; };
00097 
00098     NS_IMETHOD GetHeight             (nscoord &aHeight)
00099                                      { aHeight = mMaxHeight; 
00100                                        return NS_OK; };
00101 
00102     NS_IMETHOD GetNormalLineHeight   (nscoord &aHeight)
00103                                      { aHeight = mEmHeight + mLeading;
00104                                        return NS_OK; };
00105 
00106     NS_IMETHOD GetLeading            (nscoord &aLeading)
00107                                      { aLeading = mLeading; 
00108                                        return NS_OK; };
00109 
00110     NS_IMETHOD GetEmHeight           (nscoord &aHeight)
00111                                      { aHeight = mEmHeight; 
00112                                        return NS_OK; };
00113 
00114     NS_IMETHOD GetEmAscent           (nscoord &aAscent)
00115                                      { aAscent = mEmAscent;
00116                                        return NS_OK; };
00117 
00118     NS_IMETHOD GetEmDescent          (nscoord &aDescent)
00119                                      { aDescent = mEmDescent;
00120                                        return NS_OK; };
00121 
00122     NS_IMETHOD GetMaxHeight          (nscoord &aHeight)
00123                                      { aHeight = mMaxHeight;
00124                                        return NS_OK; };
00125 
00126     NS_IMETHOD GetMaxAscent          (nscoord &aAscent)
00127                                      { aAscent = mMaxAscent;
00128                                        return NS_OK; };
00129 
00130     NS_IMETHOD GetMaxDescent         (nscoord &aDescent)
00131                                      { aDescent = mMaxDescent;
00132                                        return NS_OK; };
00133 
00134     NS_IMETHOD GetMaxAdvance         (nscoord &aAdvance)
00135                                      { aAdvance = mMaxAdvance;
00136                                        return NS_OK; };
00137 
00138     NS_IMETHOD GetSpaceWidth         (nscoord &aSpaceCharWidth)
00139                                      { aSpaceCharWidth = mSpaceWidth;
00140                                        return NS_OK; };
00141 
00142     NS_IMETHOD GetAveCharWidth       (nscoord &aAveCharWidth)
00143                                      { aAveCharWidth = mAveCharWidth;
00144                                        return NS_OK; };
00145 
00146     PRInt32 GetMaxStringLength() { return mMaxStringLength; }
00147 
00148     // nsIFontMetricsGTK (calls from the font rendering layer)
00149     virtual nsresult GetWidth(const char* aString, PRUint32 aLength,
00150                               nscoord& aWidth,
00151                               nsRenderingContextGTK *aContext);
00152     virtual nsresult GetWidth(const PRUnichar* aString, PRUint32 aLength,
00153                               nscoord& aWidth, PRInt32 *aFontID,
00154                               nsRenderingContextGTK *aContext);
00155 
00156     virtual nsresult GetTextDimensions(const PRUnichar* aString,
00157                                        PRUint32 aLength,
00158                                        nsTextDimensions& aDimensions, 
00159                                        PRInt32* aFontID,
00160                                        nsRenderingContextGTK *aContext);
00161     virtual nsresult GetTextDimensions(const char*         aString,
00162                                        PRInt32             aLength,
00163                                        PRInt32             aAvailWidth,
00164                                        PRInt32*            aBreaks,
00165                                        PRInt32             aNumBreaks,
00166                                        nsTextDimensions&   aDimensions,
00167                                        PRInt32&            aNumCharsFit,
00168                                        nsTextDimensions&   aLastWordDimensions,
00169                                        PRInt32*            aFontID,
00170                                        nsRenderingContextGTK *aContext);
00171     virtual nsresult GetTextDimensions(const PRUnichar*    aString,
00172                                        PRInt32             aLength,
00173                                        PRInt32             aAvailWidth,
00174                                        PRInt32*            aBreaks,
00175                                        PRInt32             aNumBreaks,
00176                                        nsTextDimensions&   aDimensions,
00177                                        PRInt32&            aNumCharsFit,
00178                                        nsTextDimensions&   aLastWordDimensions,
00179                                        PRInt32*            aFontID,
00180                                        nsRenderingContextGTK *aContext);
00181 
00182     virtual nsresult DrawString(const char *aString, PRUint32 aLength,
00183                                 nscoord aX, nscoord aY,
00184                                 const nscoord* aSpacing,
00185                                 nsRenderingContextGTK *aContext,
00186                                 nsDrawingSurfaceGTK *aSurface);
00187     virtual nsresult DrawString(const PRUnichar* aString, PRUint32 aLength,
00188                                 nscoord aX, nscoord aY,
00189                                 PRInt32 aFontID,
00190                                 const nscoord* aSpacing,
00191                                 nsRenderingContextGTK *aContext,
00192                                 nsDrawingSurfaceGTK *aSurface);
00193 
00194 #ifdef MOZ_MATHML
00195     virtual nsresult GetBoundingMetrics(const char *aString, PRUint32 aLength,
00196                                         nsBoundingMetrics &aBoundingMetrics,
00197                                         nsRenderingContextGTK *aContext);
00198     virtual nsresult GetBoundingMetrics(const PRUnichar *aString,
00199                                         PRUint32 aLength,
00200                                         nsBoundingMetrics &aBoundingMetrics,
00201                                         PRInt32 *aFontID,
00202                                         nsRenderingContextGTK *aContext);
00203 #endif /* MOZ_MATHML */
00204 
00205     virtual GdkFont* GetCurrentGDKFont(void);
00206 
00207     virtual nsresult SetRightToLeftText(PRBool aIsRTL);
00208     virtual PRBool GetRightToLeftText();
00209 
00210     virtual nsresult GetClusterInfo(const PRUnichar *aText,
00211                                     PRUint32 aLength,
00212                                     PRUint8 *aClusterStarts);
00213 
00214     virtual PRInt32 GetPosition(const PRUnichar *aText,
00215                                 PRUint32 aLength,
00216                                 nsPoint aPt);
00217 
00218     virtual nsresult GetRangeWidth(const PRUnichar *aText,
00219                                    PRUint32 aLength,
00220                                    PRUint32 aStart,
00221                                    PRUint32 aEnd,
00222                                    PRUint32 &aWidth);
00223 
00224     virtual nsresult GetRangeWidth(const char *aText,
00225                                    PRUint32 aLength,
00226                                    PRUint32 aStart,
00227                                    PRUint32 aEnd,
00228                                    PRUint32 &aWidth);
00229 
00230     // get hints for the font
00231     static PRUint32    GetHints  (void);
00232 
00233     // drawing surface methods
00234     static nsresult FamilyExists (nsIDeviceContext *aDevice,
00235                                   const nsString &aName);
00236 
00237     nsresult    DrawStringCallback      (const FcChar32 *aString, PRUint32 aLen,
00238                                          nsFontXft *aFont, void *aData);
00239     nsresult    TextDimensionsCallback  (const FcChar32 *aString, PRUint32 aLen,
00240                                          nsFontXft *aFont, void *aData);
00241     nsresult    GetWidthCallback        (const FcChar32 *aString, PRUint32 aLen,
00242                                          nsFontXft *aFont, void *aData);
00243 #ifdef MOZ_MATHML
00244     nsresult    BoundingMetricsCallback (const FcChar32 *aString, PRUint32 aLen,
00245                                          nsFontXft *aFont, void *aData);
00246 #endif /* MOZ_MATHML */
00247 
00248 private:
00249     enum FontMatch {
00250         eNoMatch,
00251         eBestMatch,
00252         eAllMatching
00253     };
00254 
00255     // local methods
00256     nsresult    RealizeFont        (void);
00257     nsresult    CacheFontMetrics   (void);
00258     // Guaranteed to return either null or a font on which |GetXftFont|
00259     // returns non-null.
00260     nsFontXft  *FindFont           (PRUint32);
00261     void        SetupFCPattern     (void);
00262     void        DoMatch            (PRBool aMatchAll);
00263 
00264     gint        RawGetWidth        (const PRUnichar* aString,
00265                                     PRUint32         aLength);
00266     nsresult    SetupMiniFont      (void);
00267     nsresult    DrawUnknownGlyph   (FcChar32   aChar,
00268                                     nscoord    aX,
00269                                     nscoord    aY,
00270                                     XftColor  *aColor,
00271                                     XftDraw   *aDraw);
00272     nsresult    EnumerateXftGlyphs (const FcChar32 *aString,
00273                                     PRUint32  aLen,
00274                                     GlyphEnumeratorCallback aCallback,
00275                                     void     *aCallbackData);
00276     nsresult    EnumerateGlyphs    (const char *aString,
00277                                     PRUint32  aLen,
00278                                     GlyphEnumeratorCallback aCallback,
00279                                     void     *aCallbackData);
00280     nsresult    EnumerateGlyphs    (const PRUnichar *aString,
00281                                     PRUint32  aLen,
00282                                     GlyphEnumeratorCallback aCallback,
00283                                     void     *aCallbackData);
00284     void        PrepareToDraw      (nsRenderingContextGTK *aContext,
00285                                     nsDrawingSurfaceGTK *aSurface,
00286                                     XftDraw **aDraw, XftColor &aColor);
00287 
00288     // called when enumerating font families
00289     static PRBool   EnumFontCallback (const nsString &aFamily,
00290                                       PRBool aIsGeneric, void *aData);
00291 
00292 
00293     // generic font metrics class bits
00294     nsCStringArray       mFontList;
00295     nsAutoVoidArray      mFontIsGeneric;
00296 
00297     nsIDeviceContext    *mDeviceContext;
00298     nsCOMPtr<nsIAtom>    mLangGroup;
00299     nsCString           *mGenericFont;
00300     float                mPixelSize;
00301 
00302     nsCAutoString        mDefaultFont;
00303 
00304     // private to DoMatch and FindFont; this array may contain fonts
00305     // for which |GetXftFont| returns null (which are not allowed outside
00306     // of those two functions).
00307     nsVoidArray          mLoadedFonts;
00308 
00309     // Xft-related items
00310     nsFontXft           *mWesternFont;
00311     FcPattern           *mPattern;
00312     FontMatch            mMatchType;
00313 
00314     // for rendering unknown fonts
00315     XftFont                 *mMiniFont;
00316     nscoord                  mMiniFontWidth;
00317     nscoord                  mMiniFontHeight;
00318     nscoord                  mMiniFontPadding;
00319     nscoord                  mMiniFontYOffset;
00320     nscoord                  mMiniFontAscent;
00321     nscoord                  mMiniFontDescent;
00322 
00323     // Cached font metrics
00324     nscoord                  mXHeight;
00325     nscoord                  mSuperscriptOffset;
00326     nscoord                  mSubscriptOffset;
00327     nscoord                  mStrikeoutOffset;
00328     nscoord                  mStrikeoutSize;
00329     nscoord                  mUnderlineOffset;
00330     nscoord                  mUnderlineSize;
00331     nscoord                  mMaxHeight;
00332     nscoord                  mLeading;
00333     nscoord                  mEmHeight;
00334     nscoord                  mEmAscent;
00335     nscoord                  mEmDescent;
00336     nscoord                  mMaxAscent;
00337     nscoord                  mMaxDescent;
00338     nscoord                  mMaxAdvance;
00339     nscoord                  mSpaceWidth;
00340     nscoord                  mAveCharWidth;
00341     PRInt32                  mMaxStringLength;
00342 };
00343 
00344 class nsFontEnumeratorXft : public nsIFontEnumerator
00345 {
00346 public:
00347     nsFontEnumeratorXft();
00348     NS_DECL_ISUPPORTS
00349     NS_DECL_NSIFONTENUMERATOR
00350 };