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 "nsICairoFontMetrics.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 nsICairoFontMetrics
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     // nsIFontMetricsGTK (calls from the font rendering layer)
00147     virtual nsresult GetWidth(const char* aString, PRUint32 aLength,
00148                               nscoord& aWidth);
00149     virtual nsresult GetWidth(const PRUnichar* aString, PRUint32 aLength,
00150                               nscoord& aWidth, PRInt32 *aFontID);
00151 
00152     virtual nsresult GetTextDimensions(const PRUnichar* aString,
00153                                        PRUint32 aLength,
00154                                        nsTextDimensions& aDimensions, 
00155                                        PRInt32* aFontID);
00156     virtual nsresult GetTextDimensions(const char*         aString,
00157                                        PRInt32             aLength,
00158                                        PRInt32             aAvailWidth,
00159                                        PRInt32*            aBreaks,
00160                                        PRInt32             aNumBreaks,
00161                                        nsTextDimensions&   aDimensions,
00162                                        PRInt32&            aNumCharsFit,
00163                                        nsTextDimensions&   aLastWordDimensions,
00164                                        PRInt32*            aFontID);
00165     virtual nsresult GetTextDimensions(const PRUnichar*    aString,
00166                                        PRInt32             aLength,
00167                                        PRInt32             aAvailWidth,
00168                                        PRInt32*            aBreaks,
00169                                        PRInt32             aNumBreaks,
00170                                        nsTextDimensions&   aDimensions,
00171                                        PRInt32&            aNumCharsFit,
00172                                        nsTextDimensions&   aLastWordDimensions,
00173                                        PRInt32*            aFontID);
00174 
00175     virtual nsresult DrawString(const char *aString, PRUint32 aLength,
00176                                 nscoord aX, nscoord aY,
00177                                 const nscoord* aSpacing,
00178                                 nsCairoRenderingContext *aContext,
00179                                 nsCairoDrawingSurface *aSurface);
00180     virtual nsresult DrawString(const PRUnichar* aString, PRUint32 aLength,
00181                                 nscoord aX, nscoord aY,
00182                                 PRInt32 aFontID,
00183                                 const nscoord* aSpacing,
00184                                 nsCairoRenderingContext *aContext,
00185                                 nsCairoDrawingSurface *aSurface);
00186 
00187 #ifdef MOZ_MATHML
00188     virtual nsresult GetBoundingMetrics(const char *aString, PRUint32 aLength,
00189                                         nsBoundingMetrics &aBoundingMetrics,
00190                                         nsCairoRenderingContext *aContext);
00191     virtual nsresult GetBoundingMetrics(const PRUnichar *aString,
00192                                         PRUint32 aLength,
00193                                         nsBoundingMetrics &aBoundingMetrics,
00194                                         PRInt32 *aFontID,
00195                                         nsCairoRenderingContext *aContext);
00196 #endif /* MOZ_MATHML */
00197 
00198     virtual nsresult SetRightToLeftText(PRBool aIsRTL);
00199 
00200     // get hints for the font
00201     static PRUint32    GetHints  (void);
00202 
00203     // drawing surface methods
00204     static nsresult FamilyExists (nsIDeviceContext *aDevice,
00205                                   const nsString &aName);
00206 
00207     nsresult    DrawStringCallback      (const FcChar32 *aString, PRUint32 aLen,
00208                                          nsFontXft *aFont, void *aData);
00209     nsresult    TextDimensionsCallback  (const FcChar32 *aString, PRUint32 aLen,
00210                                          nsFontXft *aFont, void *aData);
00211     nsresult    GetWidthCallback        (const FcChar32 *aString, PRUint32 aLen,
00212                                          nsFontXft *aFont, void *aData);
00213 #ifdef MOZ_MATHML
00214     nsresult    BoundingMetricsCallback (const FcChar32 *aString, PRUint32 aLen,
00215                                          nsFontXft *aFont, void *aData);
00216 #endif /* MOZ_MATHML */
00217 
00218 private:
00219     enum FontMatch {
00220         eNoMatch,
00221         eBestMatch,
00222         eAllMatching
00223     };
00224 
00225     // local methods
00226     nsresult    RealizeFont        (void);
00227     nsresult    CacheFontMetrics   (void);
00228     nsFontXft  *FindFont           (PRUint32);
00229     void        SetupFCPattern     (void);
00230     void        DoMatch            (PRBool aMatchAll);
00231 
00232     int         RawGetWidth        (const PRUnichar* aString,
00233                                     PRUint32         aLength);
00234     nsresult    SetupMiniFont      (void);
00235     nsresult    DrawUnknownGlyph   (FcChar32   aChar,
00236                                     nscoord    aX,
00237                                     nscoord    aY,
00238                                     XftColor  *aColor,
00239                                     XftDraw   *aDraw);
00240     nsresult    EnumerateXftGlyphs (const FcChar32 *aString,
00241                                     PRUint32  aLen,
00242                                     GlyphEnumeratorCallback aCallback,
00243                                     void     *aCallbackData);
00244     nsresult    EnumerateGlyphs    (const char *aString,
00245                                     PRUint32  aLen,
00246                                     GlyphEnumeratorCallback aCallback,
00247                                     void     *aCallbackData);
00248     nsresult    EnumerateGlyphs    (const PRUnichar *aString,
00249                                     PRUint32  aLen,
00250                                     GlyphEnumeratorCallback aCallback,
00251                                     void     *aCallbackData);
00252     void        PrepareToDraw      (nsCairoRenderingContext *aContext,
00253                                     nsCairoDrawingSurface *aSurface,
00254                                     XftDraw **aDraw, XftColor &aColor);
00255 
00256     // called when enumerating font families
00257     static PRBool   EnumFontCallback (const nsString &aFamily,
00258                                       PRBool aIsGeneric, void *aData);
00259 
00260 
00261     // generic font metrics class bits
00262     nsCStringArray       mFontList;
00263     nsAutoVoidArray      mFontIsGeneric;
00264 
00265     nsIDeviceContext    *mDeviceContext;
00266     nsCOMPtr<nsIAtom>    mLangGroup;
00267     nsCString           *mGenericFont;
00268     float                mPixelSize;
00269 
00270     nsCAutoString        mDefaultFont;
00271 
00272     // private to DoMatch and FindFont; this array may contain fonts
00273     // for which |GetXftFont| returns null (which are not allowed outside
00274     // of those two functions).
00275     nsVoidArray          mLoadedFonts;
00276 
00277     // Xft-related items
00278     nsFontXft           *mWesternFont;
00279     FcPattern           *mPattern;
00280     FontMatch            mMatchType;
00281 
00282     // for rendering unknown fonts
00283     XftFont                 *mMiniFont;
00284     nscoord                  mMiniFontWidth;
00285     nscoord                  mMiniFontHeight;
00286     nscoord                  mMiniFontPadding;
00287     nscoord                  mMiniFontYOffset;
00288     nscoord                  mMiniFontAscent;
00289     nscoord                  mMiniFontDescent;
00290 
00291     // Cached font metrics
00292     nscoord                  mXHeight;
00293     nscoord                  mSuperscriptOffset;
00294     nscoord                  mSubscriptOffset;
00295     nscoord                  mStrikeoutOffset;
00296     nscoord                  mStrikeoutSize;
00297     nscoord                  mUnderlineOffset;
00298     nscoord                  mUnderlineSize;
00299     nscoord                  mMaxHeight;
00300     nscoord                  mLeading;
00301     nscoord                  mEmHeight;
00302     nscoord                  mEmAscent;
00303     nscoord                  mEmDescent;
00304     nscoord                  mMaxAscent;
00305     nscoord                  mMaxDescent;
00306     nscoord                  mMaxAdvance;
00307     nscoord                  mSpaceWidth;
00308     nscoord                  mAveCharWidth;
00309 };
00310 
00311 class nsFontEnumeratorXft : public nsIFontEnumerator
00312 {
00313 public:
00314     nsFontEnumeratorXft();
00315     NS_DECL_ISUPPORTS
00316     NS_DECL_NSIFONTENUMERATOR
00317 };