winFont.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformWin32/platformWin32.h"
  23. #include "platformWin32/winFont.h"
  24. #include "graphics/gFont.h"
  25. #include "graphics/gBitmap.h"
  26. #include "math/mRect.h"
  27. #include "console/console.h"
  28. #include "string/unicode.h"
  29. #include "memory/frameAllocator.h"
  30. static HDC fontHDC = NULL;
  31. static HBITMAP fontBMP = NULL;
  32. static U32 charsetMap[]=
  33. {
  34. ANSI_CHARSET,
  35. SYMBOL_CHARSET,
  36. SHIFTJIS_CHARSET,
  37. HANGEUL_CHARSET,
  38. HANGUL_CHARSET,
  39. GB2312_CHARSET,
  40. CHINESEBIG5_CHARSET,
  41. OEM_CHARSET,
  42. JOHAB_CHARSET,
  43. HEBREW_CHARSET,
  44. ARABIC_CHARSET,
  45. GREEK_CHARSET,
  46. TURKISH_CHARSET,
  47. VIETNAMESE_CHARSET,
  48. THAI_CHARSET,
  49. EASTEUROPE_CHARSET,
  50. RUSSIAN_CHARSET,
  51. MAC_CHARSET,
  52. BALTIC_CHARSET,
  53. };
  54. #define NUMCHARSETMAP (sizeof(charsetMap) / sizeof(U32))
  55. void createFontInit(void);
  56. void createFontShutdown(void);
  57. void CopyCharToBitmap(GBitmap *pDstBMP, HDC hSrcHDC, const RectI &r);
  58. void createFontInit()
  59. {
  60. fontHDC = CreateCompatibleDC(NULL);
  61. fontBMP = CreateCompatibleBitmap(fontHDC, 256, 256);
  62. }
  63. void createFontShutdown()
  64. {
  65. DeleteObject(fontBMP);
  66. DeleteObject(fontHDC);
  67. }
  68. void CopyCharToBitmap(GBitmap *pDstBMP, HDC hSrcHDC, const RectI &r)
  69. {
  70. for (S32 i = r.point.y; i < r.point.y + r.extent.y; i++)
  71. {
  72. for (S32 j = r.point.x; j < r.point.x + r.extent.x; j++)
  73. {
  74. COLORREF color = GetPixel(hSrcHDC, j, i);
  75. if (color)
  76. *pDstBMP->getAddress(j, i) = 255;
  77. else
  78. *pDstBMP->getAddress(j, i) = 0;
  79. }
  80. }
  81. }
  82. //////////////////////////////////////////////////////////////////////////
  83. // WinFont class
  84. //////////////////////////////////////////////////////////////////////////
  85. BOOL CALLBACK EnumFamCallBack(LPLOGFONT logFont, LPNEWTEXTMETRIC textMetric, DWORD fontType, LPARAM lParam)
  86. {
  87. if( !( fontType & TRUETYPE_FONTTYPE ) )
  88. return true;
  89. Vector<StringTableEntry>* fonts = (Vector< StringTableEntry>*)lParam;
  90. const U32 len = dStrlen( logFont->lfFaceName ) * 3 + 1;
  91. FrameTemp<UTF8> buffer( len );
  92. convertUTF16toUTF8( logFont->lfFaceName, buffer, len );
  93. fonts->push_back( StringTable->insert( buffer ) );
  94. return true;
  95. }
  96. void PlatformFont::enumeratePlatformFonts( Vector<StringTableEntry>& fonts )
  97. {
  98. EnumFontFamilies( fontHDC, NULL, (FONTENUMPROC)EnumFamCallBack, (LPARAM)&fonts );
  99. }
  100. PlatformFont *createPlatformFont(const char *name, U32 size, U32 charset /* = TGE_ANSI_CHARSET */)
  101. {
  102. PlatformFont *retFont = new WinFont;
  103. if(retFont->create(name, size, charset))
  104. return retFont;
  105. delete retFont;
  106. return NULL;
  107. }
  108. WinFont::WinFont() : mFont(NULL)
  109. {
  110. }
  111. WinFont::~WinFont()
  112. {
  113. if(mFont)
  114. {
  115. DeleteObject(mFont);
  116. }
  117. }
  118. bool WinFont::create(const char *name, U32 size, U32 charset /* = TGE_ANSI_CHARSET */)
  119. {
  120. if(name == NULL || size < 1)
  121. return false;
  122. //-Mat add font name
  123. dSprintf( mFontName, 256, "%s_%i", name, size );
  124. if(charset > NUMCHARSETMAP)
  125. charset = TGE_ANSI_CHARSET;
  126. #ifdef UNICODE
  127. UTF16 n[512];
  128. convertUTF8toUTF16((UTF8 *)name, n, sizeof(n));
  129. mFont = CreateFont(size,0,0,0,0,0,0,0,DEFAULT_CHARSET,OUT_TT_PRECIS,0,PROOF_QUALITY,0,n);
  130. #else
  131. mFont = CreateFont(size,0,0,0,0,0,0,0,charsetMap[charset],OUT_TT_PRECIS,0,PROOF_QUALITY,0,name);
  132. #endif
  133. if(mFont == NULL)
  134. return false;
  135. SelectObject(fontHDC, fontBMP);
  136. SelectObject(fontHDC, mFont);
  137. GetTextMetrics(fontHDC, &mTextMetric);
  138. return true;
  139. }
  140. bool WinFont::isValidChar(const UTF16 ch) const
  141. {
  142. return ch != 0 /* && (ch >= mTextMetric.tmFirstChar && ch <= mTextMetric.tmLastChar)*/;
  143. }
  144. bool WinFont::isValidChar(const UTF8 *str) const
  145. {
  146. return isValidChar(oneUTF8toUTF32(str));
  147. }
  148. PlatformFont::CharInfo &WinFont::getCharInfo(const UTF16 ch) const
  149. {
  150. static PlatformFont::CharInfo c;
  151. dMemset(&c, 0, sizeof(c));
  152. c.bitmapIndex = -1;
  153. static U8 scratchPad[65536];
  154. COLORREF backgroundColorRef = RGB( 0, 0, 0);
  155. COLORREF foregroundColorRef = RGB(255, 255, 255);
  156. SelectObject(fontHDC, fontBMP);
  157. SelectObject(fontHDC, mFont);
  158. SetBkColor(fontHDC, backgroundColorRef);
  159. SetTextColor(fontHDC, foregroundColorRef);
  160. MAT2 matrix;
  161. GLYPHMETRICS metrics;
  162. RectI clip;
  163. FIXED zero;
  164. zero.fract = 0;
  165. zero.value = 0;
  166. FIXED one;
  167. one.fract = 0;
  168. one.value = 1;
  169. matrix.eM11 = one;
  170. matrix.eM12 = zero;
  171. matrix.eM21 = zero;
  172. matrix.eM22 = one;
  173. if(GetGlyphOutline(
  174. fontHDC, // handle of device context
  175. ch, // character to query
  176. GGO_GRAY8_BITMAP, // format of data to return
  177. &metrics, // address of structure for metrics
  178. sizeof(scratchPad), // size of buffer for data
  179. scratchPad, // address of buffer for data
  180. &matrix // address of transformation matrix structure
  181. ) != GDI_ERROR)
  182. {
  183. U32 rowStride = (metrics.gmBlackBoxX + 3) & ~3; // DWORD aligned
  184. U32 size = rowStride * metrics.gmBlackBoxY;
  185. // [neo, 5/7/2007 - #3055]
  186. // If we get large font sizes rowStride * metrics.gmBlackBoxY will
  187. // be larger than scratch pad size and so overwrite mem, boom!
  188. // Added range check < scratchPad for now but we need to review what
  189. // to do here - do we want to call GetGlyphOutline() first with null
  190. // values and get the real size to alloc buffer?
  191. //if( size > sizeof( scratchPad ) )
  192. // DebugBreak();
  193. for(U32 j = 0; j < size && j < sizeof(scratchPad); j++)
  194. {
  195. U32 pad = U32(scratchPad[j]) << 2;
  196. if(pad > 255)
  197. pad = 255;
  198. scratchPad[j] = pad;
  199. }
  200. S32 inc = metrics.gmCellIncX;
  201. if(inc < 0)
  202. inc = -inc;
  203. c.xOffset = 0;
  204. c.yOffset = 0;
  205. c.width = metrics.gmBlackBoxX;
  206. c.height = metrics.gmBlackBoxY;
  207. c.xOrigin = metrics.gmptGlyphOrigin.x;
  208. c.yOrigin = metrics.gmptGlyphOrigin.y;
  209. c.xIncrement = metrics.gmCellIncX;
  210. c.bitmapData = new U8[c.width * c.height];
  211. for(U32 y = 0; S32(y) < c.height; y++)
  212. {
  213. U32 x;
  214. for(x = 0; x < c.width; x++)
  215. {
  216. // [neo, 5/7/2007 - #3055]
  217. // See comments above about scratchPad overrun
  218. S32 spi = y * rowStride + x;
  219. if( spi >= sizeof(scratchPad) )
  220. return c;
  221. c.bitmapData[y * c.width + x] = scratchPad[spi];
  222. }
  223. }
  224. }
  225. else
  226. {
  227. SIZE size;
  228. GetTextExtentPoint32W(fontHDC, &ch, 1, &size);
  229. if(size.cx)
  230. {
  231. //-Mat hack make spaces thin
  232. if( ch == ' ' ) {
  233. c.xIncrement = PUAP_SPACE_CHAR_X_INCREMENT;
  234. } else {
  235. c.xIncrement = size.cx;
  236. }
  237. c.bitmapIndex = 0;
  238. }
  239. }
  240. return c;
  241. }
  242. PlatformFont::CharInfo &WinFont::getCharInfo(const UTF8 *str) const
  243. {
  244. return getCharInfo(oneUTF8toUTF32(str));
  245. }