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.

310 lines
8.8 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-1998 Microsoft Corporation. All rights reserved.
  18. */
  19. #ifndef _FONT_H
  20. #define _FONT_H
  21. // Forwards
  22. class CFontCache;
  23. class CDevDesc;
  24. class CDisplay;
  25. // ============================= CCcs ========================
  26. // CCcs - caches font metrics and character size for one font
  27. #define DEFAULTCACHESIZE 0 // size - 1
  28. #define INITIALCACHESIZE 7 // size - 1 = 7; 2^n-1; size = 8
  29. #define PERFCHECKEPOCH 64 // If changed, you must recalc
  30. // and change COLLISION_SHIFT below.
  31. #define COLLISION_SHIFT 3 // log(PERFCHECKEPOCH) / log(2) - 3
  32. static const INT maxCacheSize = 511;
  33. SHORT GetFontNameIndex(const WCHAR *pFontName);
  34. BYTE GetFontLegitimateSize(LONG iFont, BOOL fUIFont, int cpg);
  35. BOOL SetFontLegitimateSize(LONG iFont, BOOL fUIFont, BYTE bSize, int cpg);
  36. const WCHAR *GetFontName(LONG iFont);
  37. UINT GetTextCharsetInfoPri(HDC hdc, FONTSIGNATURE* pcsi, DWORD dwFlags);
  38. DWORD GetFontSignatureFromFace(int ifont, DWORD* pdwFontSig = NULL);
  39. void FreeFontNames();
  40. enum FONTINDEX
  41. {
  42. IFONT_ARIAL = 0,
  43. IFONT_TMSNEWRMN = 1,
  44. IFONT_SYMBOL = 2,
  45. IFONT_SYSTEM = 3
  46. };
  47. typedef unsigned int CCSHASHKEY;
  48. extern const WCHAR *szArial;
  49. extern const WCHAR *szTimesNewRoman;
  50. extern const WCHAR *szSymbol;
  51. extern const WCHAR *szSystem;
  52. extern const WCHAR *szWingdings;
  53. //Not automatically added to font table
  54. extern const WCHAR *szMicrosSansSerif;
  55. extern const WCHAR *szMSSansSerif;
  56. extern const WCHAR *szMangal;
  57. extern const WCHAR *szLatha;
  58. extern const WCHAR *szCordiaNew;
  59. extern const WCHAR *szTahoma;
  60. extern const WCHAR *szArialUnicode;
  61. /*
  62. * CWidthCache
  63. *
  64. * @class Lightweight Unicode width cache.
  65. *
  66. * @devnote Initial size is 52 bytes, 1st step is 100, and exponentially
  67. * growing (currently) to 4660 bytes; NOTE, this can be reduced
  68. * to 28, 60 and 3100 bytes if shorts are used and appropriate
  69. * guarantees are placed on the range of width values.
  70. *
  71. * Owner: <nl>
  72. * Jon Matousek (jonmat) <nl>
  73. */
  74. class CWidthCache
  75. {
  76. //@access Private methods and data
  77. private:
  78. INT _cacheSize; //@cmember size is total cache slots - 1.
  79. INT _cacheUsed; //@cmember for statistics, num slots in use.
  80. INT _collisions; //@cmember for statistics, num fetches required.
  81. INT _accesses; //@cmember for statistics, total num accesses.
  82. BOOL _fMaxPerformance; //@cmember for statistics, TRUE if grown to max.
  83. struct CacheEntry
  84. {
  85. WCHAR ch;
  86. SHORT width;
  87. };
  88. SHORT _dxpHangul;
  89. SHORT _dxpHan;
  90. //@cmember default storage for widths.
  91. CacheEntry _defaultWidthCache[DEFAULTCACHESIZE+1];
  92. //@cmember pointers to storage for widths.
  93. CacheEntry *(_pWidthCache);
  94. inline BOOL FLookasideCharacter(WCHAR ch)
  95. {
  96. if (ch < 0x4E00)
  97. return FALSE;
  98. if (IN_RANGE(0x4E00, ch, 0x9FFF) || // CJK ideograph
  99. IN_RANGE(0xF900, ch, 0xFAFF) || // CJK compatibility ideograph
  100. IN_RANGE(0xAC00, ch, 0xD7FF)) // Hangul
  101. return TRUE;
  102. return FALSE;
  103. }
  104. void FetchLookasideWidth(WCHAR ch, LONG &dxp)
  105. {
  106. BOOL fHangul = IN_RANGE(0xAC00, ch, 0xD7FF);
  107. dxp = fHangul ? _dxpHangul : _dxpHan;
  108. }
  109. //@cmember Get location where width is stored.
  110. inline CacheEntry * GetEntry( const WCHAR ch )
  111. { // logical & is really a MOD, as all of the bits
  112. // of cacheSize are turned on; the value of cacheSize is
  113. // required to be of the form 2^n-1.
  114. return &_pWidthCache[ ch & _cacheSize ];
  115. }
  116. //@cmember See if cache is performing within spec.
  117. void CheckPerformance();
  118. //@cmember Increase width cache size.
  119. BOOL GrowCache( CacheEntry **widthCache, INT *cacheSize, INT *cacheUsed);
  120. //@access Public Methods
  121. public:
  122. //@cmember Called before GetWidth
  123. BOOL CheckWidth (const WCHAR ch, LONG &dxp);
  124. //@cmember Fetch width if CheckWidth ret FALSE.
  125. BOOL FillWidth (
  126. HDC hdc,
  127. const WCHAR ch,
  128. const SHORT xOverhang,
  129. LONG &dxp,
  130. UINT uiCodePage,
  131. INT iDefWidth);
  132. //@cmember Fetch char width.
  133. INT GetWidth(const WCHAR ch);
  134. void Free(); //@cmember Recycle width cache
  135. CWidthCache(); //@cmember Construct width cache
  136. ~CWidthCache(); //@cmember Free dynamic mem
  137. };
  138. class CCcs
  139. {
  140. friend class CFontCache;
  141. private:
  142. CCSHASHKEY _ccshashkey; // Hash key
  143. DWORD _dwAge; // for LRU algorithm
  144. SHORT _iFont; // Index into FONTNAME table
  145. SHORT _cRefs; // ref. count
  146. class CWidthCache _widths;
  147. public:
  148. DWORD _dwFontSig; // Flags from low 32 bits of FONTSIGNATURE fsCsb member
  149. HDC _hdc; // HDC font is selected into
  150. HFONT _hfont; // Windows font handle
  151. void* _sc; // A handle to the Uniscribe glyph width/font cmap information
  152. //REVIEW (keithcu) We should make these into at least 24 bit or possibly 32 bit values,
  153. //or at least use unsigned values so that we don't overflow as easily.
  154. SHORT _yHeightRequest;// Font height requested (logical units)
  155. SHORT _yHeight; // Total height of char cell (logical units)
  156. SHORT _yDescent; // Distance from baseline to char cell bottom (logical units)
  157. SHORT _xAveCharWidth; // Average character width in logical units
  158. SHORT _xOverhangAdjust;// Overhang for synthesized fonts in logical units
  159. SHORT _xOverhang; // Font overhang.
  160. SHORT _xUnderhang; // Font underhang.
  161. SHORT _dyULOffset; // Underline offset
  162. SHORT _dyULWidth; // Underline width
  163. SHORT _dySOOffset; // Strikeout offset
  164. SHORT _dySOWidth; // Strikeout width
  165. SHORT _yOffsetSuperscript; //Amount raised if superscipt (positive)
  166. SHORT _yOffsetSubscript;//Amount lowered if subscript (negative)
  167. USHORT _weight; // Font weight
  168. USHORT _wCodePage; // Font code page
  169. BYTE _bCharSetRequest; //Requested charset
  170. BYTE _bCharSet; // Font CharSet
  171. BYTE _bCMDefault; // Used in calculation of _bConvertMode
  172. BYTE _bConvertMode; // CONVERTMODE: CVT_NONE, CVT_WCTMB, CVT_LOWBYTE
  173. BYTE _bPitchAndFamily;// Font pitch and family
  174. BYTE _fValid:1; // CCcs is valid
  175. BYTE _fFixPitchFont:1;// Font has fixed character width
  176. BYTE _fItalic:1; // Font is italic
  177. BYTE _fFECharSet:1; // Font has FE charset
  178. BYTE _fForceTrueType:1;// Font has been forced to be truetype
  179. private:
  180. BOOL Compare (const CCharFormat * const pCF, HDC hdc);
  181. BOOL MakeFont(const CCharFormat * const pCF);
  182. void DestroyFont();
  183. BOOL GetMetrics(WCHAR *szNewFaceName = 0);
  184. HFONT GetFontWithMetrics(LOGFONT *plf, WCHAR* szNewFaceName);
  185. BOOL FillWidth(WCHAR ch, LONG &dxp);
  186. public:
  187. CCcs () {_fValid = FALSE;}
  188. ~CCcs () {if(_fValid) Free();}
  189. void GetOffset(const CCharFormat * const pCF, LONG dypInch,
  190. LONG *pyOffset, LONG *pyAdjust);
  191. BOOL Init(const CCharFormat * const pCF);
  192. void Free();
  193. void AddRef() {_cRefs++;}
  194. void Release() {if(_cRefs) _cRefs--;}
  195. BOOL Include(WCHAR ch, LONG &dxp)
  196. {
  197. if(!_widths.CheckWidth(ch, dxp))
  198. return FillWidth(ch, dxp);
  199. return TRUE;
  200. }
  201. BYTE BestCharSet(BYTE bCharSet, BYTE bCharSetDefault, int fFontMatching);
  202. SHORT AdjustFEHeight(BOOL fAjdust)
  203. {
  204. return ((fAjdust && _fFECharSet) ? MulDiv(_yHeight, 15, 100) : 0);
  205. }
  206. };
  207. // FONTINFO cache
  208. typedef union
  209. {
  210. WORD wFlags;
  211. struct
  212. {
  213. WORD fCached :1; // Font signature was already cached
  214. WORD fBadFaceName :1; // Face is junk or doesnt exist in the system
  215. WORD fTrueType :1; // Font is TrueType
  216. WORD fBitmap :1; // Font is Bitmap
  217. WORD fNonBiDiAscii :1; // Font is non-BiDi, single charset and support ASCII
  218. WORD fScaleByCpg :1; // Scale the font based on given codepage
  219. WORD fThaiDTP :1; // Thai DTP font
  220. };
  221. } FONTINFO_FLAGS;
  222. typedef struct
  223. {
  224. const WCHAR *szFontName;
  225. DWORD dwFontSig; // font signature
  226. BYTE bSizeUI; // UI font legitimate size (in point)
  227. BYTE bSizeNonUI; // Non-UI font legitimate size
  228. FONTINFO_FLAGS ff; // flags
  229. } FONTINFO;
  230. // ============================= CFontCache =====================================================
  231. // CFontCache - maintains up to FONTCACHESIZE font caches
  232. class CFontCache
  233. {
  234. friend class CCcs;
  235. private:
  236. CCcs _rgccs[FONTCACHESIZE];
  237. DWORD _dwAgeNext;
  238. struct {
  239. CCSHASHKEY ccshashkey;
  240. CCcs *pccs;
  241. } quickHashSearch[CCSHASHSEARCHSIZE+1];
  242. private:
  243. CCcs* GrabInitNewCcs(const CCharFormat * const pCF, HDC hdc);
  244. CCSHASHKEY MakeHashKey(const CCharFormat *pCF);
  245. public:
  246. void Init();
  247. CCcs* GetCcs(const CCharFormat * const pCF, const LONG dypInch, HDC hdc = 0, BOOL fForceTrueType = 0);
  248. FONTINFO_FLAGS GetInfoFlags(int ifont);
  249. };
  250. extern CFontCache & fc(); // font cache manager
  251. #endif