Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
9.3 KiB

  1. /*
  2. * @doc INTERNAL
  3. *
  4. * @module _FONT.H -- Declaration of classes comprising font caching |
  5. *
  6. * Purpose:
  7. * Font cache
  8. *
  9. * Owner: <nl>
  10. * David R. Fulmer (original RE 1.0 code)<nl>
  11. * Christian Fortini (initial conversion to C++)<nl>
  12. * Jon Matousek <nl>
  13. *
  14. * History: <nl>
  15. * 8/6/95 jonmat Devised dynamic expanding cache for widths.
  16. *
  17. * Copyright (c) 1995-2000 Microsoft Corporation. All rights reserved.
  18. */
  19. #ifndef _FONT_H
  20. #define _FONT_H
  21. #include "_kern.h"
  22. // Forwards
  23. class CFontCache;
  24. class CDevDesc;
  25. class CDisplay;
  26. // ============================= CCcs ========================
  27. // CCcs - caches font metrics and character size for one font
  28. #define DEFAULTCACHESIZE 0 // size - 1
  29. #define INITIALCACHESIZE 7 // size - 1 = 7; 2^n-1; size = 8
  30. #define PERFCHECKEPOCH 64 // If changed, you must recalc
  31. // and change COLLISION_SHIFT below.
  32. static const INT maxCacheSize = 511;
  33. SHORT GetFontNameIndex(const WCHAR *pFontName);
  34. BYTE GetFontLegitimateSize(LONG iFont, BOOL fUIFont, int iCharRep);
  35. BOOL SetFontLegitimateSize(LONG iFont, BOOL fUIFont, BYTE bSize, BOOL fFEcpg);
  36. void SetFontSignature(LONG iFont, QWORD qwFontSig);
  37. const WCHAR *GetFontName(LONG iFont);
  38. UINT GetTextCharsetInfoPri(HDC hdc, FONTSIGNATURE* pcsi, DWORD dwFlags);
  39. QWORD GetFontSignatureFromFace(int ifont, QWORD* pqwFontSig = NULL);
  40. void FreeFontNames();
  41. enum FONTINDEX
  42. {
  43. IFONT_ARIAL = 0,
  44. IFONT_TMSNEWRMN = 1,
  45. IFONT_SYMBOL = 2,
  46. IFONT_SYSTEM = 3
  47. };
  48. typedef unsigned int CCSHASHKEY;
  49. extern const WCHAR *szArial;
  50. extern const WCHAR *szTimesNewRoman;
  51. extern const WCHAR *szSymbol;
  52. extern const WCHAR *szSystem;
  53. extern const WCHAR *szWingdings;
  54. //Not automatically added to font table
  55. extern const WCHAR *szMicrosSansSerif;
  56. extern const WCHAR *szMSSansSerif;
  57. extern const WCHAR *szMangal;
  58. extern const WCHAR *szLatha;
  59. extern const WCHAR *szRaavi;
  60. extern const WCHAR *szShruti;
  61. extern const WCHAR *szTunga;
  62. extern const WCHAR *szGautami;
  63. extern const WCHAR *szCordiaNew;
  64. extern const WCHAR *szTahoma;
  65. extern const WCHAR *szArialUnicode;
  66. extern const WCHAR *szSylfaen;
  67. extern const WCHAR *szSyriac;
  68. extern const WCHAR *szThaana;
  69. /*
  70. * CWidthCache
  71. *
  72. * @class Lightweight Unicode width cache.
  73. *
  74. *
  75. * Owner: <nl>
  76. * Jon Matousek (jonmat) <nl>
  77. */
  78. struct CacheEntry
  79. {
  80. WCHAR ch;
  81. SHORT width;
  82. };
  83. class CWidthCache
  84. {
  85. //@access Private methods and data
  86. friend class CCcs;
  87. private:
  88. INT _cacheSize; //@cmember size is total cache slots - 1.
  89. INT _cacheUsed; //@cmember for statistics, num slots in use.
  90. INT _collisions; //@cmember for statistics, num fetches required.
  91. INT _accesses; //@cmember for statistics, total num accesses.
  92. BOOL _fMaxPerformance; //@cmember for statistics, TRUE if grown to max.
  93. SHORT _dupCJK;
  94. //@cmember default storage for widths.
  95. CacheEntry _defaultWidthCache[DEFAULTCACHESIZE+1];
  96. //@cmember pointers to storage for widths.
  97. CacheEntry *(_pWidthCache);
  98. __forceinline BOOL FLookasideCharacter(WCHAR ch)
  99. {
  100. if (ch < 0x3400)
  101. return FALSE;
  102. if (IN_RANGE(0x3400, ch, 0x9FFF) || // CJK ideograph
  103. IN_RANGE(0xF900, ch, 0xFAFF) || // CJK compatibility ideograph
  104. IN_RANGE(0xAC00, ch, 0xD7FF)) // Hangul
  105. return TRUE;
  106. return FALSE;
  107. }
  108. //@cmember Get location where width is stored.
  109. inline CacheEntry * GetEntry( const WCHAR ch )
  110. { // logical & is really a MOD, as all of the bits
  111. // of cacheSize are turned on; the value of cacheSize is
  112. // required to be of the form 2^n-1.
  113. return &_pWidthCache[ ch & _cacheSize ];
  114. }
  115. //@cmember See if cache is performing within spec.
  116. void CheckPerformance();
  117. //@cmember Increase width cache size.
  118. BOOL GrowCache( CacheEntry **widthCache, INT *cacheSize, INT *cacheUsed);
  119. //@access Public Methods
  120. public:
  121. //@cmember Called before GetWidth
  122. BOOL CheckWidth (const WCHAR ch, LONG &dup);
  123. //@cmember Fetch width if CheckWidth ret FALSE.
  124. void Free(); //@cmember Recycle width cache
  125. CWidthCache(); //@cmember Construct width cache
  126. ~CWidthCache(); //@cmember Free dynamic mem
  127. };
  128. class CCcs
  129. {
  130. friend class CFontCache;
  131. private:
  132. CCSHASHKEY _ccshashkey; // Hash key
  133. DWORD _dwAge; // for LRU algorithm
  134. SHORT _iFont; // Index into FONTNAME table
  135. SHORT _cRefs; // ref. count
  136. class CWidthCache _widths;
  137. public:
  138. QWORD _qwFontSig; // Internal font signature flags
  139. HDC _hdc; // HDC font is selected into
  140. HFONT _hfont; // Windows font handle
  141. void* _sc; // A handle to the Uniscribe glyph width/font cmap information
  142. //REVIEW (keithcu) We should make these into at least 24 bit or possibly 32 bit values,
  143. //or at least use unsigned values so that we don't overflow as easily.
  144. SHORT _yHeightRequest;// Font height requested (logical units)
  145. SHORT _yHeight; // Total height of char cell (logical units)
  146. SHORT _yDescent; // Distance from baseline to char cell bottom (logical units)
  147. SHORT _xAveCharWidth; // Average character width in logical units
  148. SHORT _xOverhangAdjust;// Overhang for synthesized fonts in logical units
  149. SHORT _dyULOffset; // Underline offset
  150. SHORT _dyULWidth; // Underline width
  151. SHORT _dySOOffset; // Strikeout offset
  152. SHORT _dySOWidth; // Strikeout width
  153. USHORT _weight; // Font weight
  154. USHORT _wCodePage; // Font code page
  155. BYTE _bCharSetRequest; //Requested charset
  156. BYTE _bCharSet; // Font CharSet
  157. BYTE _bCMDefault; // Used in calculation of _bConvertMode
  158. BYTE _bConvertMode; // CONVERTMODE: CVT_NONE, CVT_WCTMB, CVT_LOWBYTE
  159. BYTE _bPitchAndFamily;// Font pitch and family
  160. TFLOW _tflow; // Tflow of _hfont
  161. BYTE _bQuality; // LOGFONT quality
  162. BYTE _fValid:1; // CCcs is valid
  163. BYTE _fFixPitchFont:1; // Font has fixed character width
  164. BYTE _fItalic:1; // Font is italic
  165. BYTE _fFECharSet:1; // Font has FE charset
  166. BYTE _fForceTrueType:1; // Font has been forced to be truetype
  167. BYTE _fCustomTextOut:1; // Should we use the ICustomTextOut handlers?
  168. BYTE _fUseAtFont:1; // Switch to @ font
  169. private:
  170. BOOL Compare (const CCharFormat * const pCF, HDC hdc, DWORD dwFlags);
  171. BOOL MakeFont(const CCharFormat * const pCF);
  172. void DestroyFont();
  173. BOOL GetMetrics(WCHAR *szNewFaceName = 0);
  174. HFONT GetFontWithMetrics(LOGFONT *plf, WCHAR* szNewFaceName);
  175. BOOL FillWidth(WCHAR ch, LONG &dup);
  176. public:
  177. CCcs () {_fValid = FALSE;}
  178. ~CCcs () {if(_fValid) Free();}
  179. void GetFontOverhang(LONG *pdupOverhang, LONG *pdupUnderhang);
  180. void GetOffset(const CCharFormat * const pCF, LONG dvpInch,
  181. LONG *pyOffset, LONG *pyAdjust);
  182. BOOL Init(const CCharFormat * const pCF);
  183. void Free();
  184. void AddRef() {_cRefs++;}
  185. void Release() {if(_cRefs) _cRefs--;}
  186. BOOL Include(WCHAR ch, LONG &dup)
  187. {
  188. if(!_widths.CheckWidth(ch, dup))
  189. return FillWidth(ch, dup);
  190. return TRUE;
  191. }
  192. BYTE BestCharRep(BYTE iCharRep, BYTE iCharRepDefault, int fFontMatching);
  193. SHORT AdjustFEHeight(BOOL fAjdust)
  194. {return ((fAjdust && _fFECharSet) ? W32MulDiv(_yHeight, 15, 100) : 0);}
  195. };
  196. // FONTINFO cache
  197. typedef union
  198. {
  199. WORD wFlags;
  200. struct
  201. {
  202. WORD fCached :1; // Font signature was already cached
  203. WORD fBadFaceName :1; // Face is junk or doesnt exist in the system
  204. WORD fTrueType :1; // Font is TrueType
  205. WORD fBitmap :1; // Font is Bitmap
  206. WORD fNonBiDiAscii :1; // Font is non-BiDi, single charset and support ASCII
  207. WORD fScaleByCpg :1; // Scale the font based on given codepage
  208. WORD fThaiDTP :1; // Thai DTP font
  209. };
  210. } FONTINFO_FLAGS;
  211. class CFontFamilyMember
  212. {
  213. public:
  214. CFontFamilyMember(LONG weight, BOOL fItalic)
  215. {
  216. _weight = weight; _fItalic = fItalic;
  217. }
  218. void Free() {_kc.Free();}
  219. CKernCache* GetKernCache() {return &_kc;}
  220. LONG _weight;
  221. BOOL _fItalic;
  222. CKernCache _kc;
  223. };
  224. class CFontFamilyMgr
  225. {
  226. public:
  227. CFontFamilyMgr::~CFontFamilyMgr();
  228. CFontFamilyMember *GetFontFamilyMember(LONG weight, BOOL fItalic);
  229. CArray <CFontFamilyMember> _rgf;
  230. };
  231. struct FONTINFO
  232. {
  233. const WCHAR *szFontName;
  234. QWORD qwFontSig; // Font signature
  235. BYTE bSizeUI; // UI font legitimate size (in point)
  236. BYTE bSizeNonUI; // Non-UI font legitimate size
  237. FONTINFO_FLAGS ff; // flags
  238. CFontFamilyMgr* _pffm; // Information which is different for
  239. }; // bold/italic variants of a font
  240. // ============================= CFontCache =====================================================
  241. // CFontCache - maintains up to FONTCACHESIZE font caches
  242. //The low 2 bits are reserved for passing down the TFLOW of the text
  243. const DWORD FGCCSUSETRUETYPE = 0x04;
  244. const DWORD FGCCSUSEATFONT = 0x08;
  245. class CFontCache
  246. {
  247. friend class CCcs;
  248. private:
  249. CCcs _rgccs[FONTCACHESIZE];
  250. DWORD _dwAgeNext;
  251. struct {
  252. CCSHASHKEY ccshashkey;
  253. CCcs *pccs;
  254. } quickHashSearch[CCSHASHSEARCHSIZE+1];
  255. private:
  256. CCcs* GrabInitNewCcs(const CCharFormat * const pCF, HDC hdc, DWORD dwFlags);
  257. CCSHASHKEY MakeHashKey(const CCharFormat *pCF);
  258. public:
  259. void Init();
  260. CFontFamilyMgr * GetFontFamilyMgr(LONG iFont);
  261. CKernCache * GetKernCache(LONG iFont, LONG weight, BOOL fItalic);
  262. CCcs* GetCcs(CCharFormat *pCF, const LONG dvpInch, DWORD dwFlags, HDC hdc = 0);
  263. FONTINFO_FLAGS GetInfoFlags(int ifont);
  264. };
  265. extern CFontCache & fc(); // font cache manager
  266. #endif