winFont.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "gfx/gFont.h"
  25. #include "gfx/bitmap/gBitmap.h"
  26. #include "math/mRect.h"
  27. #include "console/console.h"
  28. #include "core/strings/unicode.h"
  29. #include "core/strings/stringFunctions.h"
  30. #include "core/stringTable.h"
  31. static HDC fontHDC = NULL;
  32. static HBITMAP fontBMP = NULL;
  33. static U32 charsetMap[]=
  34. {
  35. ANSI_CHARSET,
  36. SYMBOL_CHARSET,
  37. SHIFTJIS_CHARSET,
  38. HANGEUL_CHARSET,
  39. HANGUL_CHARSET,
  40. GB2312_CHARSET,
  41. CHINESEBIG5_CHARSET,
  42. OEM_CHARSET,
  43. JOHAB_CHARSET,
  44. HEBREW_CHARSET,
  45. ARABIC_CHARSET,
  46. GREEK_CHARSET,
  47. TURKISH_CHARSET,
  48. VIETNAMESE_CHARSET,
  49. THAI_CHARSET,
  50. EASTEUROPE_CHARSET,
  51. RUSSIAN_CHARSET,
  52. MAC_CHARSET,
  53. BALTIC_CHARSET,
  54. };
  55. #define NUMCHARSETMAP (sizeof(charsetMap) / sizeof(U32))
  56. void createFontInit(void);
  57. void createFontShutdown(void);
  58. void CopyCharToBitmap(GBitmap *pDstBMP, HDC hSrcHDC, const RectI &r);
  59. void createFontInit()
  60. {
  61. //shared library sets the appInstance here
  62. winState.appInstance = GetModuleHandle(NULL);
  63. fontHDC = CreateCompatibleDC(NULL);
  64. fontBMP = CreateCompatibleBitmap(fontHDC, 256, 256);
  65. }
  66. void createFontShutdown()
  67. {
  68. DeleteObject(fontBMP);
  69. DeleteObject(fontHDC);
  70. }
  71. void CopyCharToBitmap(GBitmap *pDstBMP, HDC hSrcHDC, const RectI &r)
  72. {
  73. for (S32 i = r.point.y; i < r.point.y + r.extent.y; i++)
  74. {
  75. for (S32 j = r.point.x; j < r.point.x + r.extent.x; j++)
  76. {
  77. COLORREF color = GetPixel(hSrcHDC, j, i);
  78. if (color)
  79. *pDstBMP->getAddress(j, i) = 255;
  80. else
  81. *pDstBMP->getAddress(j, i) = 0;
  82. }
  83. }
  84. }
  85. //-----------------------------------------------------------------------------
  86. // WinFont class
  87. //-----------------------------------------------------------------------------
  88. BOOL CALLBACK EnumFamCallBack(LPLOGFONT logFont, LPNEWTEXTMETRIC textMetric, DWORD fontType, LPARAM lParam)
  89. {
  90. if( !( fontType & TRUETYPE_FONTTYPE ) )
  91. return true;
  92. Vector<StringTableEntry>* fonts = (Vector< StringTableEntry>*)lParam;
  93. const U32 len = dStrlen( logFont->lfFaceName ) * 3 + 1;
  94. FrameTemp<UTF8> buffer( len );
  95. convertUTF16toUTF8N( logFont->lfFaceName, buffer, len );
  96. fonts->push_back( StringTable->insert( buffer ) );
  97. return true;
  98. }
  99. void PlatformFont::enumeratePlatformFonts( Vector<StringTableEntry>& fonts, UTF16* fontFamily )
  100. {
  101. EnumFontFamilies( fontHDC, fontFamily, (FONTENUMPROC)EnumFamCallBack, (LPARAM)&fonts );
  102. }
  103. PlatformFont *createPlatformFont(const char *name, dsize_t size, U32 charset /* = TGE_ANSI_CHARSET */)
  104. {
  105. PlatformFont *retFont = new WinFont;
  106. if(retFont->create(name, size, charset))
  107. return retFont;
  108. delete retFont;
  109. return NULL;
  110. }
  111. WinFont::WinFont() : mFont(NULL)
  112. {
  113. }
  114. WinFont::~WinFont()
  115. {
  116. if(mFont)
  117. {
  118. DeleteObject(mFont);
  119. }
  120. }
  121. bool WinFont::create(const char *name, dsize_t size, U32 charset /* = TGE_ANSI_CHARSET */)
  122. {
  123. if(name == NULL || size < 1)
  124. return false;
  125. if(charset > NUMCHARSETMAP)
  126. charset = TGE_ANSI_CHARSET;
  127. U32 weight = 0;
  128. U32 doItalic = 0;
  129. String nameStr = name;
  130. nameStr = nameStr.trim();
  131. bool haveModifier;
  132. do
  133. {
  134. haveModifier = false;
  135. if( nameStr.compare( "Bold", 4, String::NoCase | String::Right ) == 0 )
  136. {
  137. weight = 700;
  138. nameStr = nameStr.substr( 0, nameStr.length() - 4 ).trim();
  139. haveModifier = true;
  140. }
  141. if( nameStr.compare( "Italic", 6, String::NoCase | String::Right ) == 0 )
  142. {
  143. doItalic = 1;
  144. nameStr = nameStr.substr( 0, nameStr.length() - 6 ).trim();
  145. haveModifier = true;
  146. }
  147. }
  148. while( haveModifier );
  149. #ifdef UNICODE
  150. const UTF16* n = nameStr.utf16();
  151. mFont = CreateFont(size,0,0,0,weight,doItalic,0,0,DEFAULT_CHARSET,OUT_TT_PRECIS,0,PROOF_QUALITY,0,n);
  152. #else
  153. mFont = CreateFont(size,0,0,0,weight,doItalic,0,0,charsetMap[charset],OUT_TT_PRECIS,0,PROOF_QUALITY,0,name);
  154. #endif
  155. if(mFont == NULL)
  156. return false;
  157. SelectObject(fontHDC, fontBMP);
  158. SelectObject(fontHDC, mFont);
  159. GetTextMetrics(fontHDC, &mTextMetric);
  160. return true;
  161. }
  162. bool WinFont::isValidChar(const UTF16 ch) const
  163. {
  164. return ch != 0 /* && (ch >= mTextMetric.tmFirstChar && ch <= mTextMetric.tmLastChar)*/;
  165. }
  166. bool WinFont::isValidChar(const UTF8 *str) const
  167. {
  168. return isValidChar(oneUTF8toUTF32(str));
  169. }
  170. PlatformFont::CharInfo &WinFont::getCharInfo(const UTF16 ch) const
  171. {
  172. static PlatformFont::CharInfo c;
  173. dMemset(&c, 0, sizeof(c));
  174. c.bitmapIndex = -1;
  175. static U8 scratchPad[65536];
  176. COLORREF backgroundColorRef = RGB( 0, 0, 0);
  177. COLORREF foregroundColorRef = RGB(255, 255, 255);
  178. SelectObject(fontHDC, fontBMP);
  179. SelectObject(fontHDC, mFont);
  180. SetBkColor(fontHDC, backgroundColorRef);
  181. SetTextColor(fontHDC, foregroundColorRef);
  182. MAT2 matrix;
  183. GLYPHMETRICS metrics;
  184. RectI clip;
  185. FIXED zero;
  186. zero.fract = 0;
  187. zero.value = 0;
  188. FIXED one;
  189. one.fract = 0;
  190. one.value = 1;
  191. matrix.eM11 = one;
  192. matrix.eM12 = zero;
  193. matrix.eM21 = zero;
  194. matrix.eM22 = one;
  195. if(GetGlyphOutline(
  196. fontHDC, // handle of device context
  197. ch, // character to query
  198. GGO_GRAY8_BITMAP, // format of data to return
  199. &metrics, // address of structure for metrics
  200. sizeof(scratchPad), // size of buffer for data
  201. scratchPad, // address of buffer for data
  202. &matrix // address of transformation matrix structure
  203. ) != GDI_ERROR)
  204. {
  205. U32 rowStride = (metrics.gmBlackBoxX + 3) & ~3; // DWORD aligned
  206. U32 size = rowStride * metrics.gmBlackBoxY;
  207. // [neo, 5/7/2007 - #3055]
  208. // If we get large font sizes rowStride * metrics.gmBlackBoxY will
  209. // be larger than scratch pad size and so overwrite mem, boom!
  210. // Added range check < scratchPad for now but we need to review what
  211. // to do here - do we want to call GetGlyphOutline() first with null
  212. // values and get the real size to alloc buffer?
  213. //if( size > sizeof( scratchPad ) )
  214. // DebugBreak();
  215. for(U32 j = 0; j < size && j < sizeof(scratchPad); j++)
  216. {
  217. U32 pad = U32(scratchPad[j]) << 2;
  218. if(pad > 255)
  219. pad = 255;
  220. scratchPad[j] = pad;
  221. }
  222. S32 inc = metrics.gmCellIncX;
  223. if(inc < 0)
  224. inc = -inc;
  225. c.xOffset = 0;
  226. c.yOffset = 0;
  227. c.width = metrics.gmBlackBoxX;
  228. c.height = metrics.gmBlackBoxY;
  229. c.xOrigin = metrics.gmptGlyphOrigin.x;
  230. c.yOrigin = metrics.gmptGlyphOrigin.y;
  231. c.xIncrement = metrics.gmCellIncX;
  232. c.bitmapData = new U8[c.width * c.height];
  233. AssertFatal( c.bitmapData != NULL, "Could not allocate memory for font bitmap data!");
  234. for(U32 y = 0; S32(y) < c.height; y++)
  235. {
  236. U32 x;
  237. for(x = 0; x < c.width; x++)
  238. {
  239. // [neo, 5/7/2007 - #3055]
  240. // See comments above about scratchPad overrun
  241. S32 spi = y * rowStride + x;
  242. if( spi >= sizeof(scratchPad) )
  243. return c;
  244. c.bitmapData[y * c.width + x] = scratchPad[spi];
  245. }
  246. }
  247. }
  248. else
  249. {
  250. SIZE size;
  251. GetTextExtentPoint32W(fontHDC, &ch, 1, &size);
  252. if(size.cx)
  253. {
  254. c.xIncrement = size.cx;
  255. c.bitmapIndex = 0;
  256. }
  257. }
  258. return c;
  259. }
  260. PlatformFont::CharInfo &WinFont::getCharInfo(const UTF8 *str) const
  261. {
  262. return getCharInfo(oneUTF8toUTF32(str));
  263. }