Back to index

lightning-sunbird  0.9+nobinonly
nsFontMetricsPS.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 nsFontMetricsPS_h__
00039 #define nsFontMetricsPS_h__
00040 
00041 #include "gfx-config.h"
00042 #include "nsIFontMetrics.h"
00043 #include "nsAFMObject.h"
00044 #include "nsFont.h"
00045 #include "nsString.h"
00046 #include "nsIAtom.h"
00047 #include "nsUnitConversion.h"
00048 #include "nsIDeviceContext.h"
00049 #include "nsCOMPtr.h"
00050 #include "nsCRT.h"
00051 #include "nsCompressedCharMap.h"
00052 #include "nsPostScriptObj.h"
00053 #ifdef MOZ_ENABLE_XFT
00054 #include <ft2build.h>
00055 #include FT_FREETYPE_H
00056 #include FT_GLYPH_H
00057 #include FT_CACHE_H
00058 #include FT_CACHE_IMAGE_H
00059 #include FT_OUTLINE_H
00060 #include FT_OUTLINE_H
00061 #include FT_TRUETYPE_TABLES_H
00062 #else
00063 #ifdef MOZ_ENABLE_FREETYPE2
00064 #include "nsIFontCatalogService.h"
00065 #endif
00066 #endif
00067 #include "nsVoidArray.h"
00068 #include "nsHashtable.h"
00069 
00070 class nsPSFontGenerator;
00071 class nsDeviceContextPS;
00072 class nsRenderingContextPS;
00073 class nsFontPS;
00074 
00075 class nsFontMetricsPS : public nsIFontMetrics
00076 {
00077 public:
00078   nsFontMetricsPS();
00079   virtual ~nsFontMetricsPS();
00080 
00081   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00082 
00083   NS_DECL_ISUPPORTS
00084 
00085   NS_IMETHOD  Init(const nsFont& aFont, nsIAtom* aLangGroup,
00086                    nsIDeviceContext* aContext);
00087   NS_IMETHOD  Destroy();
00088 
00089   NS_IMETHOD  GetXHeight(nscoord& aResult);
00090   NS_IMETHOD  GetSuperscriptOffset(nscoord& aResult);
00091   NS_IMETHOD  GetSubscriptOffset(nscoord& aResult);
00092   NS_IMETHOD  GetStrikeout(nscoord& aOffset, nscoord& aSize);
00093   NS_IMETHOD  GetUnderline(nscoord& aOffset, nscoord& aSize);
00094 
00095   NS_IMETHOD  GetHeight(nscoord &aHeight);
00096   NS_IMETHOD  GetNormalLineHeight(nscoord &aHeight);
00097   NS_IMETHOD  GetLeading(nscoord &aLeading);
00098   NS_IMETHOD  GetEmHeight(nscoord &aHeight);
00099   NS_IMETHOD  GetEmAscent(nscoord &aAscent);
00100   NS_IMETHOD  GetEmDescent(nscoord &aDescent);
00101   NS_IMETHOD  GetMaxHeight(nscoord &aHeight);
00102   NS_IMETHOD  GetMaxAscent(nscoord &aAscent);
00103   NS_IMETHOD  GetMaxDescent(nscoord &aDescent);
00104   NS_IMETHOD  GetMaxAdvance(nscoord &aAdvance);
00105   NS_IMETHOD  GetAveCharWidth(nscoord &aAveCharWidth);
00106   NS_IMETHOD  GetSpaceWidth(nscoord& aAveCharWidth);
00107   NS_IMETHOD  GetLangGroup(nsIAtom** aLangGroup);
00108   NS_IMETHOD  GetFontHandle(nsFontHandle &aHandle);
00109   NS_IMETHOD  GetStringWidth(const char *String,nscoord &aWidth,nscoord aLength);
00110   NS_IMETHOD  GetStringWidth(const PRUnichar *aString,nscoord &aWidth,nscoord aLength);
00111   
00112   inline void SetXHeight(nscoord aXHeight) { mXHeight = aXHeight; };
00113   inline void SetSuperscriptOffset(nscoord aSuperscriptOffset) { mSuperscriptOffset = aSuperscriptOffset; };
00114   inline void SetSubscriptOffset(nscoord aSubscriptOffset) { mSubscriptOffset = aSubscriptOffset; };
00115   inline void SetStrikeout(nscoord aOffset, nscoord aSize) { mStrikeoutOffset = aOffset; mStrikeoutSize = aSize; };
00116   inline void SetUnderline(nscoord aOffset, nscoord aSize) { mUnderlineOffset = aOffset; mUnderlineSize = aSize; };
00117   inline void SetHeight(nscoord aHeight) { mHeight = aHeight; };
00118   inline void SetLeading(nscoord aLeading) { mLeading = aLeading; };
00119   inline void SetAscent(nscoord aAscent) { mAscent = aAscent; };
00120   inline void SetDescent(nscoord aDescent) { mDescent = aDescent; };
00121   inline void SetEmHeight(nscoord aEmHeight) { mEmHeight = aEmHeight; };
00122   inline void SetEmAscent(nscoord aEmAscent) { mEmAscent = aEmAscent; };
00123   inline void SetEmDescent(nscoord aEmDescent) { mEmDescent = aEmDescent; };
00124   inline void SetMaxHeight(nscoord aMaxHeight) { mMaxHeight = aMaxHeight; };
00125   inline void SetMaxAscent(nscoord aMaxAscent) { mMaxAscent = aMaxAscent; };
00126   inline void SetMaxDescent(nscoord aMaxDescent) { mMaxDescent = aMaxDescent; };
00127   inline void SetMaxAdvance(nscoord aMaxAdvance) { mMaxAdvance = aMaxAdvance; };
00128   inline void SetAveCharWidth(nscoord aAveCharWidth) { mAveCharWidth = aAveCharWidth; };
00129   inline void SetSpaceWidth(nscoord aSpaceWidth) { mSpaceWidth = aSpaceWidth; };
00130 
00131   inline nsDeviceContextPS* GetDeviceContext() { return mDeviceContext; }
00132   inline nsVoidArray* GetFontsPS() { return mFontsPS; };
00133   inline nsHashtable *GetFontsAlreadyLoadedList() {return mFontsAlreadyLoaded;};
00134   inline int GetFontPSState() { return mFontPSState; };
00135   inline void IncrementFontPSState() { mFontPSState++; };
00136 
00137 #if defined(XP_WIN)
00138 // this routine is defined here so the PostScript module can be debugged
00139 // on the windows platform
00140 
00144   NS_IMETHOD  GetAveCharWidth(nscoord& aAveCharWidth){return NS_OK;}
00145 #endif
00146 
00147 
00148 private:
00149   nsCOMPtr<nsIAtom> mLangGroup;
00150 
00151 protected:
00152   void RealizeFont();
00153 
00154   nsDeviceContextPS   *mDeviceContext;
00155   nscoord             mHeight;
00156   nscoord             mAscent;
00157   nscoord             mDescent;
00158   nscoord             mLeading;
00159   nscoord             mEmHeight;
00160   nscoord             mEmAscent;
00161   nscoord             mEmDescent;
00162   nscoord             mMaxHeight;
00163   nscoord             mMaxAscent;
00164   nscoord             mMaxDescent;
00165   nscoord             mMaxAdvance;
00166   nscoord             mXHeight;
00167   nscoord             mSuperscriptOffset;
00168   nscoord             mSubscriptOffset;
00169   nscoord             mStrikeoutSize;
00170   nscoord             mStrikeoutOffset;
00171   nscoord             mUnderlineSize;
00172   nscoord             mUnderlineOffset;
00173   nscoord             mSpaceWidth;
00174   nscoord             mAveCharWidth;
00175 
00176   nsVoidArray         *mFontsPS;
00177   nsHashtable         *mFontsAlreadyLoaded;
00178   int                 mFontPSState;
00179 };
00180 
00181 class nsFontPS
00182 {
00183 public:
00184   nsFontPS();
00185   nsFontPS(const nsFont& aFont, nsFontMetricsPS* aFontMetrics);
00186   virtual ~nsFontPS();
00187   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00188 
00189   static nsFontPS* FindFont(char aChar, const nsFont& aFont, 
00190                             nsFontMetricsPS* aFontMetrics);
00191   static nsFontPS* FindFont(PRUnichar aChar, const nsFont& aFont, 
00192                             nsFontMetricsPS* aFontMetrics);
00193   inline PRInt32 SupportsChar(PRUnichar aChar)
00194     { return mCCMap && CCMAP_HAS_CHAR(mCCMap, aChar); };
00195 
00196   virtual nscoord GetWidth(const char* aString, PRUint32 aLength) = 0;
00197   virtual nscoord GetWidth(const PRUnichar* aString, PRUint32 aLength) = 0;
00198   virtual nscoord DrawString(nsRenderingContextPS* aContext,
00199                              nscoord aX, nscoord aY,
00200                              const char* aString, PRUint32 aLength) = 0;
00201   virtual nscoord DrawString(nsRenderingContextPS* aContext,
00202                              nscoord aX, nscoord aY,
00203                              const PRUnichar* aString, PRUint32 aLength) = 0;
00204   virtual nsresult RealizeFont(nsFontMetricsPS* aFontMetrics, float dev2app) = 0;
00205   virtual nsresult SetupFont(nsRenderingContextPS* aContext) = 0;
00206 
00207 #ifdef MOZ_MATHML
00208   virtual nsresult
00209   GetBoundingMetrics(const char*        aString,
00210                      PRUint32           aLength,
00211                      nsBoundingMetrics& aBoundingMetrics) = 0;
00212   virtual nsresult
00213   GetBoundingMetrics(const PRUnichar*   aString,
00214                      PRUint32           aLength,
00215                      nsBoundingMetrics& aBoundingMetrics) = 0;
00216 #endif
00217 
00218 protected:
00219   nsFont*                  mFont;
00220   PRUint16*                mCCMap;
00221   nsFontMetricsPS*         mFontMetrics;
00222 };
00223 
00224 class nsFontPSAFM : public nsFontPS
00225 {
00226 public:
00227   static nsFontPS* FindFont(const nsFont& aFont, nsFontMetricsPS* aFontMetrics);
00228 
00229   nsFontPSAFM(const nsFont& aFont, nsAFMObject* aAFMInfo,
00230               PRInt16 aFontIndex, nsFontMetricsPS* aFontMetrics);
00231   virtual ~nsFontPSAFM();
00232   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00233 
00234   nscoord GetWidth(const char* aString, PRUint32 aLength);
00235   nscoord GetWidth(const PRUnichar* aString, PRUint32 aLength);
00236   nscoord DrawString(nsRenderingContextPS* aContext,
00237                      nscoord aX, nscoord aY,
00238                      const char* aString, PRUint32 aLength);
00239   nscoord DrawString(nsRenderingContextPS* aContext,
00240                      nscoord aX, nscoord aY,
00241                      const PRUnichar* aString, PRUint32 aLength);
00242   nsresult RealizeFont(nsFontMetricsPS* aFontMetrics, float dev2app);
00243   nsresult SetupFont(nsRenderingContextPS* aContext);
00244 
00245 #ifdef MOZ_MATHML
00246   nsresult
00247   GetBoundingMetrics(const char*        aString,
00248                      PRUint32           aLength,
00249                      nsBoundingMetrics& aBoundingMetrics);
00250   nsresult
00251   GetBoundingMetrics(const PRUnichar*   aString,
00252                      PRUint32           aLength,
00253                      nsBoundingMetrics& aBoundingMetrics);
00254 #endif
00255 
00256   nsAFMObject* mAFMInfo;
00257   PRInt16      mFontIndex;
00258   nsString     mFamilyName;
00259 };
00260 
00261 
00262 #ifdef MOZ_ENABLE_XFT
00263 
00264 #include <X11/Xlib.h>
00265 #include <X11/Xft/Xft.h>
00266 
00267 class nsXftEntry
00268 {
00269 public:
00270   nsXftEntry(FcPattern *aFontPattern);
00271   ~nsXftEntry() {}; 
00272 
00273   FT_Face       mFace;
00274   int           mFaceIndex;
00275   nsCString     mFontFileName;
00276   nsCString     mFamilyName;
00277   nsCString     mStyleName;
00278 
00279 protected:
00280   nsXftEntry() {};
00281 };
00282 
00283 struct fontps {
00284   nsXftEntry *entry;
00285   nsFontPS   *fontps;
00286   FcCharSet  *charset;
00287 };
00288 
00289 struct fontPSInfo {
00290   nsVoidArray     *fontps;
00291   const nsFont*    nsfont;
00292   nsCAutoString    lang;
00293   nsHashtable     *alreadyLoaded;
00294   nsCStringArray   mFontList;
00295   nsAutoVoidArray  mFontIsGeneric;
00296   nsCString       *mGenericFont;
00297 };
00298 
00299 class nsFontPSXft : public nsFontPS
00300 {
00301 public:
00302   static nsFontPS* FindFont(PRUnichar aChar, const nsFont& aFont,
00303                             nsFontMetricsPS* aFontMetrics);
00304   nsresult         Init(nsXftEntry* aEntry,
00305                         nsPSFontGenerator* aPSFontGen);
00306   static PRBool CSSFontEnumCallback(const nsString& aFamily, PRBool aGeneric,
00307                                     void* aFpi);
00308 
00309   nsFontPSXft(const nsFont& aFont, nsFontMetricsPS* aFontMetrics);
00310   virtual ~nsFontPSXft();
00311   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00312 
00313   nscoord GetWidth(const char* aString, PRUint32 aLength);
00314   nscoord GetWidth(const PRUnichar* aString, PRUint32 aLength);
00315   nscoord DrawString(nsRenderingContextPS* aContext,
00316                      nscoord aX, nscoord aY,
00317                      const char* aString, PRUint32 aLength);
00318   nscoord DrawString(nsRenderingContextPS* aContext,
00319                      nscoord aX, nscoord aY,
00320                      const PRUnichar* aString, PRUint32 aLength);
00321   nsresult RealizeFont(nsFontMetricsPS* aFontMetrics, float dev2app);
00322   nsresult SetupFont(nsRenderingContextPS* aContext);
00323 
00324 #ifdef MOZ_MATHML
00325   nsresult
00326   GetBoundingMetrics(const char*        aString,
00327                      PRUint32           aLength,
00328                      nsBoundingMetrics& aBoundingMetrics);
00329   nsresult
00330   GetBoundingMetrics(const PRUnichar*   aString,
00331                      PRUint32           aLength,
00332                      nsBoundingMetrics& aBoundingMetrics);
00333 #endif
00334 
00335   nsXftEntry *mEntry;
00336   FT_Face getFTFace();
00337 
00338 protected:
00339   PRUint16        mPixelSize;
00340   FT_Library      mFreeTypeLibrary;
00341   nsCString       mFontNameBase;   // the base name of type 1 (sub) fonts
00342   nscoord         mHeight; 
00343 
00344   int     ascent();
00345   int     descent();
00346   PRBool  getXHeight(unsigned long &aVal);
00347   int     max_ascent();
00348   int     max_descent();
00349   int     max_width();
00350   PRBool  superscript_y(long &aVal);
00351   PRBool  subscript_y(long &aVal);
00352   PRBool  underlinePosition(long &aVal);
00353   PRBool  underline_thickness(unsigned long &aVal);
00354   nsPSFontGenerator*  mPSFontGenerator;
00355 };
00356 
00357 #else
00358 
00359 #ifdef MOZ_ENABLE_FREETYPE2
00360 #include "nsIFreeType2.h"
00361 
00362 typedef struct {
00363   nsITrueTypeFontCatalogEntry *entry;
00364   nsFontPS *fontps;
00365   unsigned short *ccmap;
00366 } fontps;
00367 
00368 typedef struct {
00369   nsVoidArray *fontps;
00370   const nsFont* nsfont;
00371   nsCAutoString lang;
00372   nsHashtable *alreadyLoaded;
00373   PRUint16 slant;
00374   PRUint16 weight;
00375 } fontPSInfo;
00376 
00377 class nsFontPSFreeType : public nsFontPS
00378 {
00379 public:
00380   static nsFontPS* FindFont(PRUnichar aChar, const nsFont& aFont,
00381                             nsFontMetricsPS* aFontMetrics);
00382   static nsresult  FindFontEntry(const nsFont& aFont, nsIAtom* aLanguage,
00383                                  nsITrueTypeFontCatalogEntry** aEntry);
00384   static nsresult  AddFontEntries(nsACString& aFamilyName,
00385                                   nsACString& aLanguage,
00386                                   PRUint16 aWeight, PRUint16 aWidth,
00387                                   PRUint16 aSlant, PRUint16 aSpacing,
00388                                   fontPSInfo* aFpi);
00389   static PRBool CSSFontEnumCallback(const nsString& aFamily, PRBool aGeneric,
00390                                     void* aFpi);
00391   nsresult         Init(nsITrueTypeFontCatalogEntry* aEntry,
00392                         nsPSFontGenerator* aPSFontGen);
00393 
00394   nsFontPSFreeType(const nsFont& aFont, nsFontMetricsPS* aFontMetrics);
00395   virtual ~nsFontPSFreeType();
00396   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00397 
00398   nscoord GetWidth(const char* aString, PRUint32 aLength);
00399   nscoord GetWidth(const PRUnichar* aString, PRUint32 aLength);
00400   nscoord DrawString(nsRenderingContextPS* aContext,
00401                      nscoord aX, nscoord aY,
00402                      const char* aString, PRUint32 aLength);
00403   nscoord DrawString(nsRenderingContextPS* aContext,
00404                      nscoord aX, nscoord aY,
00405                      const PRUnichar* aString, PRUint32 aLength);
00406   nsresult RealizeFont(nsFontMetricsPS* aFontMetrics, float dev2app);
00407   nsresult SetupFont(nsRenderingContextPS* aContext);
00408 
00409 #ifdef MOZ_MATHML
00410   nsresult
00411   GetBoundingMetrics(const char*        aString,
00412                      PRUint32           aLength,
00413                      nsBoundingMetrics& aBoundingMetrics);
00414   nsresult
00415   GetBoundingMetrics(const PRUnichar*   aString,
00416                      PRUint32           aLength,
00417                      nsBoundingMetrics& aBoundingMetrics);
00418 #endif
00419 
00420   nsCOMPtr<nsITrueTypeFontCatalogEntry> mEntry;
00421   FT_Face getFTFace();
00422 
00423 protected:
00424   nsCOMPtr<nsITrueTypeFontCatalogEntry> mFaceID;
00425   nsCOMPtr<nsIFreeType2> mFt2;
00426   PRUint16        mPixelSize;
00427   FTC_Image_Desc  mImageDesc;
00428   nsCString       mFontNameBase;   // the base name of type 1 (sub) fonts
00429   nscoord         mHeight; 
00430 
00431   static PRBool AddUserPref(nsIAtom *aLang, const nsFont& aFont,
00432                             fontPSInfo *aFpi);
00433   int     ascent();
00434   int     descent();
00435   PRBool  getXHeight(unsigned long &aVal);
00436   int     max_ascent();
00437   int     max_descent();
00438   int     max_width();
00439   PRBool  superscript_y(long &aVal);
00440   PRBool  subscript_y(long &aVal);
00441   PRBool  underlinePosition(long &aVal);
00442   PRBool  underline_thickness(unsigned long &aVal);
00443   nsPSFontGenerator*  mPSFontGenerator;
00444 };
00445 
00446 #else // !FREETYPE2 && !XFT
00447 typedef struct {
00448   nsFontPS   *fontps;
00449 } fontps;
00450 #endif // MOZ_ENABLE_FREETYPE2
00451 #endif   // MOZ_ENABLE_XFT
00452 
00453 class nsPSFontGenerator {
00454 public:
00455   nsPSFontGenerator();
00456   virtual ~nsPSFontGenerator();
00457   virtual void  GeneratePSFont(FILE* aFile);
00458   PRInt32  AddToSubset(PRUnichar aChar);
00459   nsString *GetSubset();
00460 
00461   // 256 (PS type 1 encoding vector size) - 1 (1 is for mandatory /.notdef)
00462   const static PRUint16 kSubFontSize; 
00463 
00464 protected:
00465   // XXX To support non-BMP characters, we may have to use 
00466   // nsValueArray with PRUint32
00467   nsString mSubset;
00468 };
00469 
00470 
00471 #ifdef MOZ_ENABLE_XFT
00472 
00473 class nsXftType1Generator : public nsPSFontGenerator {
00474 public:
00475   nsXftType1Generator();
00476   ~nsXftType1Generator();
00477   nsresult Init(nsXftEntry* aFce);
00478   void  GeneratePSFont(FILE* aFile);
00479 
00480 protected:
00481   nsXftEntry *mEntry;
00482   FT_Library      mFreeTypeLibrary;
00483 };
00484 #else
00485 #ifdef MOZ_ENABLE_FREETYPE2
00486 class nsFT2Type1Generator : public nsPSFontGenerator {
00487 public:
00488   nsFT2Type1Generator();
00489   ~nsFT2Type1Generator();
00490   nsresult Init(nsITrueTypeFontCatalogEntry* aFce);
00491   void  GeneratePSFont(FILE* aFile);
00492 
00493 protected:
00494   nsCOMPtr<nsITrueTypeFontCatalogEntry> mEntry;
00495   nsCOMPtr<nsIFreeType2> mFt2;
00496   FTC_Image_Desc  mImageDesc;
00497 };
00498 #endif   // MOZ_ENABLE_FREETYPE2
00499 #endif   // MOZ_ENABLE_XFT
00500 
00501 #endif
00502