winFont.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. dMemset(&mTextMetric, 0, sizeof(mTextMetric));
  114. }
  115. WinFont::~WinFont()
  116. {
  117. if(mFont)
  118. {
  119. DeleteObject(mFont);
  120. }
  121. }
  122. bool WinFont::create(const char *name, dsize_t size, U32 charset /* = TGE_ANSI_CHARSET */)
  123. {
  124. if(name == NULL || size < 1)
  125. return false;
  126. if(charset > NUMCHARSETMAP)
  127. charset = TGE_ANSI_CHARSET;
  128. U32 weight = 0;
  129. U32 doItalic = 0;
  130. String nameStr = name;
  131. nameStr = nameStr.trim();
  132. bool haveModifier;
  133. do
  134. {
  135. haveModifier = false;
  136. if( nameStr.compare( "Bold", 4, String::NoCase | String::Right ) == 0 )
  137. {
  138. weight = 700;
  139. nameStr = nameStr.substr( 0, nameStr.length() - 4 ).trim();
  140. haveModifier = true;
  141. }
  142. if( nameStr.compare( "Italic", 6, String::NoCase | String::Right ) == 0 )
  143. {
  144. doItalic = 1;
  145. nameStr = nameStr.substr( 0, nameStr.length() - 6 ).trim();
  146. haveModifier = true;
  147. }
  148. }
  149. while( haveModifier );
  150. #ifdef UNICODE
  151. const UTF16* n = nameStr.utf16();
  152. mFont = CreateFont(size,0,0,0,weight,doItalic,0,0,DEFAULT_CHARSET,OUT_TT_PRECIS,0,PROOF_QUALITY,0,n);
  153. #else
  154. mFont = CreateFont(size,0,0,0,weight,doItalic,0,0,charsetMap[charset],OUT_TT_PRECIS,0,PROOF_QUALITY,0,name);
  155. #endif
  156. if(mFont == NULL)
  157. return false;
  158. SelectObject(fontHDC, fontBMP);
  159. SelectObject(fontHDC, mFont);
  160. GetTextMetrics(fontHDC, &mTextMetric);
  161. return true;
  162. }
  163. bool WinFont::isValidChar(const UTF16 ch) const
  164. {
  165. return ch != 0 /* && (ch >= mTextMetric.tmFirstChar && ch <= mTextMetric.tmLastChar)*/;
  166. }
  167. bool WinFont::isValidChar(const UTF8 *str) const
  168. {
  169. return isValidChar(oneUTF8toUTF32(str));
  170. }
  171. PlatformFont::CharInfo &WinFont::getCharInfo(const UTF16 ch) const
  172. {
  173. static PlatformFont::CharInfo c;
  174. dMemset(&c, 0, sizeof(c));
  175. c.bitmapIndex = -1;
  176. static U8 scratchPad[65536];
  177. COLORREF backgroundColorRef = RGB( 0, 0, 0);
  178. COLORREF foregroundColorRef = RGB(255, 255, 255);
  179. SelectObject(fontHDC, fontBMP);
  180. SelectObject(fontHDC, mFont);
  181. SetBkColor(fontHDC, backgroundColorRef);
  182. SetTextColor(fontHDC, foregroundColorRef);
  183. MAT2 matrix;
  184. GLYPHMETRICS metrics;
  185. RectI clip;
  186. FIXED zero;
  187. zero.fract = 0;
  188. zero.value = 0;
  189. FIXED one;
  190. one.fract = 0;
  191. one.value = 1;
  192. matrix.eM11 = one;
  193. matrix.eM12 = zero;
  194. matrix.eM21 = zero;
  195. matrix.eM22 = one;
  196. if(GetGlyphOutline(
  197. fontHDC, // handle of device context
  198. ch, // character to query
  199. GGO_GRAY8_BITMAP, // format of data to return
  200. &metrics, // address of structure for metrics
  201. sizeof(scratchPad), // size of buffer for data
  202. scratchPad, // address of buffer for data
  203. &matrix // address of transformation matrix structure
  204. ) != GDI_ERROR)
  205. {
  206. U32 rowStride = (metrics.gmBlackBoxX + 3) & ~3; // DWORD aligned
  207. U32 size = rowStride * metrics.gmBlackBoxY;
  208. // [neo, 5/7/2007 - #3055]
  209. // If we get large font sizes rowStride * metrics.gmBlackBoxY will
  210. // be larger than scratch pad size and so overwrite mem, boom!
  211. // Added range check < scratchPad for now but we need to review what
  212. // to do here - do we want to call GetGlyphOutline() first with null
  213. // values and get the real size to alloc buffer?
  214. //if( size > sizeof( scratchPad ) )
  215. // DebugBreak();
  216. for(U32 j = 0; j < size && j < sizeof(scratchPad); j++)
  217. {
  218. U32 pad = U32(scratchPad[j]) << 2;
  219. if(pad > 255)
  220. pad = 255;
  221. scratchPad[j] = pad;
  222. }
  223. S32 inc = metrics.gmCellIncX;
  224. if(inc < 0)
  225. inc = -inc;
  226. c.xOffset = 0;
  227. c.yOffset = 0;
  228. c.width = metrics.gmBlackBoxX;
  229. c.height = metrics.gmBlackBoxY;
  230. c.xOrigin = metrics.gmptGlyphOrigin.x;
  231. c.yOrigin = metrics.gmptGlyphOrigin.y;
  232. c.xIncrement = metrics.gmCellIncX;
  233. c.bitmapData = new U8[c.width * c.height];
  234. AssertFatal( c.bitmapData != NULL, "Could not allocate memory for font bitmap data!");
  235. for(U32 y = 0; S32(y) < c.height; y++)
  236. {
  237. U32 x;
  238. for(x = 0; x < c.width; x++)
  239. {
  240. // [neo, 5/7/2007 - #3055]
  241. // See comments above about scratchPad overrun
  242. S32 spi = y * rowStride + x;
  243. if( spi >= sizeof(scratchPad) )
  244. return c;
  245. c.bitmapData[y * c.width + x] = scratchPad[spi];
  246. }
  247. }
  248. }
  249. else
  250. {
  251. SIZE size;
  252. GetTextExtentPoint32W(fontHDC, &ch, 1, &size);
  253. if(size.cx)
  254. {
  255. c.xIncrement = size.cx;
  256. c.bitmapIndex = 0;
  257. }
  258. }
  259. return c;
  260. }
  261. PlatformFont::CharInfo &WinFont::getCharInfo(const UTF8 *str) const
  262. {
  263. return getCharInfo(oneUTF8toUTF32(str));
  264. }