Back to index

lightning-sunbird  0.9+nobinonly
nsFontMetricsGTK.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsFontMetricsGTK_h__
00039 #define nsFontMetricsGTK_h__
00040 
00041 #include "nsDeviceContextGTK.h"
00042 #include "nsIFontMetrics.h"
00043 #include "nsIFontEnumerator.h"
00044 #include "nsFont.h"
00045 #include "nsString.h"
00046 #include "nsUnitConversion.h"
00047 #include "nsIDeviceContext.h"
00048 #include "nsCRT.h"
00049 #include "nsCOMPtr.h"
00050 #include "nsRenderingContextGTK.h"
00051 #include "nsICharRepresentable.h"
00052 #include "nsCompressedCharMap.h"
00053 #include "nsIFontMetricsGTK.h"
00054 #ifdef MOZ_ENABLE_FREETYPE2
00055 #include "nsIFontCatalogService.h"
00056 #endif
00057 
00058 #include <gdk/gdk.h>
00059 #include <gdk/gdkx.h>
00060 
00061 #undef FONT_HAS_GLYPH
00062 #define FONT_HAS_GLYPH(map, char) IS_REPRESENTABLE(map, char)
00063 #define WEIGHT_INDEX(weight) (((weight) / 100) - 1)
00064 
00065 typedef struct nsFontCharSetInfo nsFontCharSetInfo;
00066 
00067 typedef gint (*nsFontCharSetConverter)(nsFontCharSetInfo* aSelf,
00068   XFontStruct* aFont, const PRUnichar* aSrcBuf, PRInt32 aSrcLen,
00069   char* aDestBuf, PRInt32 aDestLen);
00070 
00071 struct nsFontCharSet;
00072 struct nsFontFamily;
00073 struct nsFontNode;
00074 struct nsFontStretch;
00075 struct nsFontWeight;
00076 class nsXFont;
00077 
00078 class nsFontGTKUserDefined;
00079 class nsFontMetricsGTK;
00080 class nsFreeTypeFace;
00081 class nsFontGTK;
00082 
00083 struct nsFontStretch
00084 {
00085   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00086 
00087   void SortSizes(void);
00088 
00089   nsFontGTK**        mSizes;
00090   PRUint16           mSizesAlloc;
00091   PRUint16           mSizesCount;
00092 
00093   char*              mScalable;
00094   PRBool             mOutlineScaled;
00095   nsVoidArray        mScaledFonts;
00096 #ifdef MOZ_ENABLE_FREETYPE2
00097   nsITrueTypeFontCatalogEntry*   mFreeTypeFaceID;
00098 #endif
00099 };
00100 
00101 struct nsFontStyle
00102 {
00103   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00104 
00105   void FillWeightHoles(void);
00106 
00107   nsFontWeight* mWeights[9];
00108 };
00109 
00110 struct nsFontWeight
00111 {
00112   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00113 
00114   void FillStretchHoles(void);
00115 
00116   nsFontStretch* mStretches[9];
00117 };
00118 
00119 struct nsFontNode
00120 {
00121   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00122 
00123   void FillStyleHoles(void);
00124 
00125   nsCAutoString      mName;
00126   nsFontCharSetInfo* mCharSetInfo;
00127   nsFontStyle*       mStyles[3];
00128   PRUint8            mHolesFilled;
00129   PRUint8            mDummy;
00130 };
00131 
00132 class nsFontNodeArray : public nsAutoVoidArray
00133 {
00134 public:
00135   nsFontNodeArray() {};
00136 
00137   nsFontNode* GetElement(PRInt32 aIndex)
00138   {
00139     return (nsFontNode*) ElementAt(aIndex);
00140   };
00141 };
00142 
00143 /*
00144  * Font Language Groups
00145  *
00146  * These Font Language Groups (FLG) indicate other related
00147  * encodings to look at when searching for glyphs
00148  *
00149  */
00150 typedef struct nsFontLangGroup {
00151   const char *mFontLangGroupName;
00152   nsIAtom*    mFontLangGroupAtom;
00153 } nsFontLangGroup;
00154 
00155 struct nsFontCharSetMap
00156 {
00157   const char*        mName;
00158   nsFontLangGroup*   mFontLangGroup;
00159   nsFontCharSetInfo* mInfo;
00160 };
00161 
00162 class nsFontGTK
00163 {
00164 public:
00165   nsFontGTK();
00166   nsFontGTK(nsFontGTK*);
00167   virtual ~nsFontGTK();
00168   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00169 
00170   void LoadFont(void);
00171   PRBool IsEmptyFont(XFontStruct*);
00172 
00173   inline int SupportsChar(PRUint32 aChar)
00174     { return mCCMap && CCMAP_HAS_CHAR_EXT(mCCMap, aChar); };
00175 
00176   virtual GdkFont* GetGDKFont(void);
00177   virtual nsXFont* GetXFont(void);
00178   virtual PRBool   GetXFontIs10646(void);
00179   virtual PRBool   IsFreeTypeFont(void);
00180   virtual gint GetWidth(const PRUnichar* aString, PRUint32 aLength) = 0;
00181   virtual gint DrawString(nsRenderingContextGTK* aContext,
00182                           nsDrawingSurfaceGTK* aSurface, nscoord aX,
00183                           nscoord aY, const PRUnichar* aString,
00184                           PRUint32 aLength) = 0;
00185 #ifdef MOZ_MATHML
00186   // bounding metrics for a string 
00187   // remember returned values are not in app units 
00188   // - to emulate GetWidth () above
00189   virtual nsresult
00190   GetBoundingMetrics(const PRUnichar*   aString,
00191                      PRUint32           aLength,
00192                      nsBoundingMetrics& aBoundingMetrics) = 0;
00193 #endif
00194 
00195   PRUint16*              mCCMap;
00196   nsFontCharSetInfo*     mCharSetInfo;
00197   char*                  mName;
00198   nsFontGTKUserDefined*  mUserDefinedFont;
00199   PRUint16               mSize;
00200   PRUint16               mAABaseSize;
00201   PRInt16                mBaselineAdjust;
00202 
00203   // these values are not in app units, they need to be scaled with 
00204   // nsIDeviceContext::DevUnitsToAppUnits()
00205   PRInt16                mMaxAscent;
00206   PRInt16                mMaxDescent;
00207 
00208 protected:
00209   GdkFont*               mFont;
00210   GdkFont*               mFontHolder;
00211   nsXFont*               mXFont;
00212   PRBool                 mAlreadyCalledLoadFont;
00213 };
00214 
00215 struct nsFontSwitchGTK {
00216   // Simple wrapper on top of nsFontGTK for the moment
00217   // Could hold other attributes of the font
00218   nsFontGTK* mFontGTK;
00219 };
00220 
00221 typedef PRBool (*PR_CALLBACK nsFontSwitchCallbackGTK)
00222                (const nsFontSwitchGTK *aFontSwitch,
00223                 const PRUnichar       *aSubstring,
00224                 PRUint32               aSubstringLength,
00225                 void                  *aData);
00226 
00227 class nsFontMetricsGTK : public nsIFontMetricsGTK
00228 {
00229 public:
00230   nsFontMetricsGTK();
00231   virtual ~nsFontMetricsGTK();
00232 
00233   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00234 
00235   NS_DECL_ISUPPORTS
00236 
00237   NS_IMETHOD  Init(const nsFont& aFont, nsIAtom* aLangGroup,
00238                    nsIDeviceContext* aContext);
00239   NS_IMETHOD  Destroy();
00240 
00241   NS_IMETHOD  GetXHeight(nscoord& aResult);
00242   NS_IMETHOD  GetSuperscriptOffset(nscoord& aResult);
00243   NS_IMETHOD  GetSubscriptOffset(nscoord& aResult);
00244   NS_IMETHOD  GetStrikeout(nscoord& aOffset, nscoord& aSize);
00245   NS_IMETHOD  GetUnderline(nscoord& aOffset, nscoord& aSize);
00246 
00247   NS_IMETHOD  GetHeight(nscoord &aHeight);
00248   NS_IMETHOD  GetNormalLineHeight(nscoord &aHeight);
00249   NS_IMETHOD  GetLeading(nscoord &aLeading);
00250   NS_IMETHOD  GetEmHeight(nscoord &aHeight);
00251   NS_IMETHOD  GetEmAscent(nscoord &aAscent);
00252   NS_IMETHOD  GetEmDescent(nscoord &aDescent);
00253   NS_IMETHOD  GetMaxHeight(nscoord &aHeight);
00254   NS_IMETHOD  GetMaxAscent(nscoord &aAscent);
00255   NS_IMETHOD  GetMaxDescent(nscoord &aDescent);
00256   NS_IMETHOD  GetMaxAdvance(nscoord &aAdvance);
00257   NS_IMETHOD  GetAveCharWidth(nscoord &aAveCharWidth);
00258   virtual PRInt32 GetMaxStringLength();
00259   NS_IMETHOD  GetLangGroup(nsIAtom** aLangGroup);
00260   NS_IMETHOD  GetFontHandle(nsFontHandle &aHandle);
00261   
00262   NS_IMETHOD  GetSpaceWidth(nscoord &aSpaceWidth);
00263   NS_IMETHOD  ResolveForwards(const PRUnichar* aString, PRUint32 aLength,
00264                               nsFontSwitchCallbackGTK aFunc, void* aData);
00265 
00266   nsFontGTK*  FindFont(PRUint32 aChar);
00267   nsFontGTK*  FindUserDefinedFont(PRUint32 aChar);
00268   nsFontGTK*  FindStyleSheetSpecificFont(PRUint32 aChar);
00269   nsFontGTK*  FindStyleSheetGenericFont(PRUint32 aChar);
00270   nsFontGTK*  FindLangGroupPrefFont(nsIAtom* aLangGroup, PRUint32 aChar);
00271   nsFontGTK*  FindLangGroupFont(nsIAtom* aLangGroup, PRUint32 aChar, nsCString* aName);
00272   nsFontGTK*  FindAnyFont(PRUint32 aChar);
00273   nsFontGTK*  FindSubstituteFont(PRUint32 aChar);
00274 
00275   nsFontGTK*  SearchNode(nsFontNode* aNode, PRUint32 aChar);
00276   nsFontGTK*  TryAliases(nsCString* aName, PRUint32 aChar);
00277   nsFontGTK*  TryFamily(nsCString* aName, PRUint32 aChar);
00278   nsFontGTK*  TryNode(nsCString* aName, PRUint32 aChar);
00279   nsFontGTK*  TryNodes(nsACString &aFFREName, PRUint32 aChar);
00280   nsFontGTK*  TryLangGroup(nsIAtom* aLangGroup, nsCString* aName, PRUint32 aChar);
00281 
00282   nsFontGTK*  AddToLoadedFontsList(nsFontGTK* aFont);
00283   nsFontGTK*  FindNearestSize(nsFontStretch* aStretch, PRUint16 aSize);
00284   nsFontGTK*  GetAASBBaseFont(nsFontStretch* aStretch, 
00285                               nsFontCharSetInfo* aCharSet);
00286   nsFontGTK*  PickASizeAndLoad(nsFontStretch* aStretch,
00287                                nsFontCharSetInfo* aCharSet, 
00288                                PRUint32 aChar,
00289                                const char *aName);
00290 
00291   // nsIFontMetricsGTK (calls from the font rendering layer)
00292   virtual nsresult GetWidth(const char* aString, PRUint32 aLength,
00293                             nscoord& aWidth,
00294                             nsRenderingContextGTK *aContext);
00295   virtual nsresult GetWidth(const PRUnichar* aString, PRUint32 aLength,
00296                             nscoord& aWidth, PRInt32 *aFontID,
00297                             nsRenderingContextGTK *aContext);
00298   
00299   virtual nsresult GetTextDimensions(const PRUnichar* aString,
00300                                      PRUint32 aLength,
00301                                      nsTextDimensions& aDimensions, 
00302                                      PRInt32* aFontID,
00303                                      nsRenderingContextGTK *aContext);
00304   virtual nsresult GetTextDimensions(const char*         aString,
00305                                      PRInt32             aLength,
00306                                      PRInt32             aAvailWidth,
00307                                      PRInt32*            aBreaks,
00308                                      PRInt32             aNumBreaks,
00309                                      nsTextDimensions&   aDimensions,
00310                                      PRInt32&            aNumCharsFit,
00311                                      nsTextDimensions&   aLastWordDimensions,
00312                                      PRInt32*            aFontID,
00313                                      nsRenderingContextGTK *aContext);
00314   virtual nsresult GetTextDimensions(const PRUnichar*    aString,
00315                                      PRInt32             aLength,
00316                                      PRInt32             aAvailWidth,
00317                                      PRInt32*            aBreaks,
00318                                      PRInt32             aNumBreaks,
00319                                      nsTextDimensions&   aDimensions,
00320                                      PRInt32&            aNumCharsFit,
00321                                      nsTextDimensions&   aLastWordDimensions,
00322                                      PRInt32*            aFontID,
00323                                      nsRenderingContextGTK *aContext);
00324 
00325   virtual nsresult DrawString(const char *aString, PRUint32 aLength,
00326                               nscoord aX, nscoord aY,
00327                               const nscoord* aSpacing,
00328                               nsRenderingContextGTK *aContext,
00329                               nsDrawingSurfaceGTK *aSurface);
00330   virtual nsresult DrawString(const PRUnichar* aString, PRUint32 aLength,
00331                               nscoord aX, nscoord aY,
00332                               PRInt32 aFontID,
00333                               const nscoord* aSpacing,
00334                               nsRenderingContextGTK *aContext,
00335                               nsDrawingSurfaceGTK *aSurface);
00336 
00337 #ifdef MOZ_MATHML
00338   virtual nsresult GetBoundingMetrics(const char *aString, PRUint32 aLength,
00339                                       nsBoundingMetrics &aBoundingMetrics,
00340                                       nsRenderingContextGTK *aContext);
00341   virtual nsresult GetBoundingMetrics(const PRUnichar *aString,
00342                                       PRUint32 aLength,
00343                                       nsBoundingMetrics &aBoundingMetrics,
00344                                       PRInt32 *aFontID,
00345                                       nsRenderingContextGTK *aContext);
00346 #endif /* MOZ_MATHML */
00347 
00348   virtual GdkFont* GetCurrentGDKFont(void);
00349 
00350   virtual nsresult SetRightToLeftText(PRBool aIsRTL);
00351   virtual PRBool GetRightToLeftText();
00352 
00353   virtual nsresult GetClusterInfo(const PRUnichar *aText,
00354                                   PRUint32 aLength,
00355                                   PRUint8 *aClusterStarts);
00356 
00357   virtual PRInt32 GetPosition(const PRUnichar *aText,
00358                               PRUint32 aLength,
00359                               nsPoint aPt);
00360 
00361   virtual nsresult GetRangeWidth(const PRUnichar *aText,
00362                                  PRUint32 aLength,
00363                                  PRUint32 aStart,
00364                                  PRUint32 aEnd,
00365                                  PRUint32 &aWidth);
00366 
00367   virtual nsresult GetRangeWidth(const char *aText,
00368                                  PRUint32 aLength,
00369                                  PRUint32 aStart,
00370                                  PRUint32 aEnd,
00371                                  PRUint32 &aWidth);
00372 
00373   static nsresult FamilyExists(nsIDeviceContext *aDevice, const nsString& aName);
00374   static PRUint32 GetHints(void);
00375 
00376   //friend struct nsFontGTK;
00377 
00378   nsFontGTK   **mLoadedFonts;
00379   PRUint16    mLoadedFontsAlloc;
00380   PRUint16    mLoadedFontsCount;
00381 
00382   nsFontGTK   *mSubstituteFont;
00383 
00384   nsCStringArray mFonts;
00385   PRInt32        mFontsIndex;
00386   nsAutoVoidArray   mFontIsGeneric;
00387 
00388   nsCAutoString     mDefaultFont;
00389   nsCString         *mGeneric;
00390   nsCOMPtr<nsIAtom> mLangGroup;
00391   nsCAutoString     mUserDefined;
00392 
00393   PRUint8 mTriedAllGenerics;
00394   PRUint8 mIsUserDefined;
00395 
00396 protected:
00397   void RealizeFont();
00398   nsFontGTK* LocateFont(PRUint32 aChar, PRInt32 & aCount);
00399 
00400   nsIDeviceContext    *mDeviceContext;
00401   nsFontGTK           *mWesternFont;
00402   nsFontGTK           *mCurrentFont;
00403 
00404   nscoord             mLeading;
00405   nscoord             mEmHeight;
00406   nscoord             mEmAscent;
00407   nscoord             mEmDescent;
00408   nscoord             mMaxHeight;
00409   nscoord             mMaxAscent;
00410   nscoord             mMaxDescent;
00411   nscoord             mMaxAdvance;
00412   nscoord             mXHeight;
00413   nscoord             mSuperscriptOffset;
00414   nscoord             mSubscriptOffset;
00415   nscoord             mStrikeoutSize;
00416   nscoord             mStrikeoutOffset;
00417   nscoord             mUnderlineSize;
00418   nscoord             mUnderlineOffset;
00419   nscoord             mSpaceWidth;
00420   nscoord             mAveCharWidth;
00421   PRInt32             mMaxStringLength;
00422 
00423   PRUint16            mPixelSize;
00424   PRUint8             mStretchIndex;
00425   PRUint8             mStyleIndex;
00426   nsFontCharSetConverter mDocConverterType;
00427 };
00428 
00429 class nsFontEnumeratorGTK : public nsIFontEnumerator
00430 {
00431 public:
00432   nsFontEnumeratorGTK();
00433   NS_DECL_ISUPPORTS
00434   NS_DECL_NSIFONTENUMERATOR
00435 };
00436 
00437 class nsHashKey;
00438 PRBool FreeNode(nsHashKey* aKey, void* aData, void* aClosure);
00439 nsFontCharSetInfo *GetCharSetInfo(const char *aCharSetName);
00440 #ifdef MOZ_ENABLE_FREETYPE2
00441 void CharSetNameToCodeRangeBits(const char*, PRUint32*, PRUint32*);
00442 #endif
00443 nsFontCharSetMap *GetCharSetMap(const char *aCharSetName);
00444 
00445 
00446 
00447 
00448 #endif