Font.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Deserializer.h"
  25. #include "Font.h"
  26. #include "Profiler.h"
  27. #include "Renderer.h"
  28. #include "RendererImpl.h"
  29. #include "Texture2D.h"
  30. #include <stb_truetype.h>
  31. #include <windows.h>
  32. #include "DebugNew.h"
  33. Font::Font(Renderer* renderer, const std::string& name) :
  34. Resource(name),
  35. mRenderer(renderer),
  36. mFontDataSize(0)
  37. {
  38. }
  39. Font::~Font()
  40. {
  41. }
  42. void Font::load(Deserializer& source, ResourceCache* cache)
  43. {
  44. mFaces.clear();
  45. mFontDataSize = source.getSize();
  46. if (source.getSize())
  47. {
  48. mFontData = new unsigned char[mFontDataSize];
  49. source.read(&mFontData[0], source.getSize());
  50. }
  51. else
  52. mFontData.reset();
  53. setMemoryUse(mFontDataSize);
  54. }
  55. const FontFace* Font::getFace(int pointSize)
  56. {
  57. std::map<int, FontFace>::const_iterator i = mFaces.find(pointSize);
  58. if (i != mFaces.end())
  59. return &i->second;
  60. try
  61. {
  62. PROFILE(Font_GetFace);
  63. if (pointSize <= 0)
  64. EXCEPTION("Zero or negative font point size");
  65. if (!mFontData)
  66. EXCEPTION("Font not loaded");
  67. FontFace newFace;
  68. stbtt_fontinfo fontInfo;
  69. // Assume 1 font per file for now
  70. if (!stbtt_InitFont(&fontInfo, &mFontData[0], 0))
  71. EXCEPTION("Could not initialize font");
  72. std::vector<bool> glyphUsed;
  73. glyphUsed.resize(fontInfo.numGlyphs);
  74. for (int i = 0; i < fontInfo.numGlyphs; ++i)
  75. glyphUsed[i] = false;
  76. // Build glyph mapping and mark used glyphs
  77. for (int i = 0; i < MAX_FONT_CHARS; ++i)
  78. {
  79. newFace.mGlyphIndex[i] = stbtt_FindGlyphIndex(&fontInfo, i);
  80. glyphUsed[newFace.mGlyphIndex[i]] = true;
  81. }
  82. // Get row height
  83. int ascent, descent, lineGap;
  84. stbtt_GetFontVMetrics(&fontInfo, &ascent, &descent, &lineGap);
  85. // Calculate scale (use ascent only)
  86. float scale = (float)pointSize / ascent;
  87. // Go through glyphs to get their dimensions & offsets
  88. for (int i = 0; i < fontInfo.numGlyphs; ++i)
  89. {
  90. FontGlyph newGlyph;
  91. int ix0, iy0, ix1, iy1;
  92. int advanceWidth, leftSideBearing;
  93. if (glyphUsed[i])
  94. {
  95. stbtt_GetGlyphBitmapBox(&fontInfo, i, scale, scale, &ix0, &iy0, &ix1, &iy1);
  96. stbtt_GetGlyphHMetrics(&fontInfo, i, &advanceWidth, &leftSideBearing);
  97. newGlyph.mWidth = ix1 - ix0;
  98. newGlyph.mHeight = iy1 - iy0;
  99. newGlyph.mOffsetX = (int)(leftSideBearing * scale);
  100. newGlyph.mOffsetY = iy0;
  101. newGlyph.mAdvanceX = (int)(advanceWidth * scale);
  102. }
  103. else
  104. {
  105. newGlyph.mWidth = 0;
  106. newGlyph.mHeight = 0;
  107. newGlyph.mOffsetX = 0;
  108. newGlyph.mOffsetY = 0;
  109. newGlyph.mAdvanceX = 0;
  110. }
  111. newFace.mGlyphs.push_back(newGlyph);
  112. }
  113. // Adjust the Y-offset so that the fonts are top-aligned
  114. int scaledAscent = (int)(scale * ascent);
  115. for (int i = 0; i < fontInfo.numGlyphs; ++i)
  116. {
  117. if (glyphUsed[i])
  118. newFace.mGlyphs[i].mOffsetY += scaledAscent;
  119. }
  120. // Calculate row advance
  121. newFace.mRowHeight = (int)(scale * (ascent - descent + lineGap));
  122. // Now try to pack into the smallest possible texture
  123. int texWidth = FONT_TEXTURE_MIN_SIZE;
  124. int texHeight = FONT_TEXTURE_MIN_SIZE;
  125. bool doubleHorizontal = true;
  126. for (;;)
  127. {
  128. bool success = true;
  129. // Check first for theoretical possible fit. If it fails, there is no need to try to fit
  130. int totalArea = 0;
  131. for (int i = 0; i < fontInfo.numGlyphs; ++i)
  132. {
  133. if ((newFace.mGlyphs[i].mWidth) && (newFace.mGlyphs[i].mHeight))
  134. totalArea += (newFace.mGlyphs[i].mWidth + 1) * (newFace.mGlyphs[i].mHeight + 1);
  135. }
  136. if (totalArea > texWidth * texHeight)
  137. success = false;
  138. else
  139. {
  140. PROFILE(Font_FitToTexture);
  141. AreaAllocator allocator(texWidth, texHeight);
  142. for (int i = 0; i < fontInfo.numGlyphs; ++i)
  143. {
  144. if ((newFace.mGlyphs[i].mWidth) && (newFace.mGlyphs[i].mHeight))
  145. {
  146. int x, y;
  147. // Reserve an empty border between glyphs for filtering
  148. if (!allocator.reserve(newFace.mGlyphs[i].mWidth + 1, newFace.mGlyphs[i].mHeight + 1, x, y))
  149. {
  150. success = false;
  151. break;
  152. }
  153. else
  154. {
  155. newFace.mGlyphs[i].mX = x;
  156. newFace.mGlyphs[i].mY = y;
  157. }
  158. }
  159. else
  160. {
  161. newFace.mGlyphs[i].mX = 0;
  162. newFace.mGlyphs[i].mY = 0;
  163. }
  164. }
  165. }
  166. if (!success)
  167. {
  168. // Alternate between doubling the horizontal and the vertical dimension
  169. if (doubleHorizontal)
  170. texWidth <<= 1;
  171. else
  172. texHeight <<= 1;
  173. if ((texWidth > FONT_TEXTURE_MAX_SIZE) || (texHeight > FONT_TEXTURE_MAX_SIZE))
  174. EXCEPTION("Font face could not be fit into the largest possible texture");
  175. doubleHorizontal = !doubleHorizontal;
  176. }
  177. else
  178. break;
  179. }
  180. // Create the texture
  181. if (mRenderer)
  182. {
  183. SharedPtr<Texture2D> texture(new Texture2D(mRenderer, TEXTURE_STATIC));
  184. texture->setNumLevels(1); // No mipmaps
  185. texture->setAddressMode(COORD_U, ADDRESS_BORDER);
  186. texture->setAddressMode(COORD_V, ADDRESS_BORDER),
  187. texture->setBorderColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
  188. texture->setSize(texWidth, texHeight, D3DFMT_A8);
  189. D3DLOCKED_RECT hwRect;
  190. texture->lock(0, 0, &hwRect);
  191. // First clear the whole texture
  192. for (int y = 0; y < texHeight; ++y)
  193. {
  194. unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * y;
  195. memset(dest, 0, texWidth);
  196. }
  197. // Render glyphs into texture, and find out a scaling value in case font uses less than full opacity (thin outlines)
  198. int sumOpacity = 0;
  199. int nonEmptyGlyphs = 0;
  200. for (int i = 0; i < fontInfo.numGlyphs; ++i)
  201. {
  202. if ((newFace.mGlyphs[i].mWidth) && (newFace.mGlyphs[i].mHeight))
  203. {
  204. stbtt_MakeGlyphBitmap(&fontInfo, (unsigned char*)hwRect.pBits + hwRect.Pitch * newFace.mGlyphs[i].mY + newFace.mGlyphs[i].mX, newFace.mGlyphs[i].mWidth, newFace.mGlyphs[i].mHeight, hwRect.Pitch, scale, scale, i);
  205. int glyphMaxOpacity = 0;
  206. for (int y = 0; y < newFace.mGlyphs[i].mHeight; ++y)
  207. {
  208. unsigned char* pixels = (unsigned char*)hwRect.pBits + hwRect.Pitch * (y + newFace.mGlyphs[i].mY) + newFace.mGlyphs[i].mX;
  209. for (int x = 0; x < newFace.mGlyphs[i].mWidth; ++x)
  210. glyphMaxOpacity = max(glyphMaxOpacity, pixels[x]);
  211. }
  212. if (glyphMaxOpacity > 0)
  213. {
  214. sumOpacity += glyphMaxOpacity;
  215. ++nonEmptyGlyphs;
  216. }
  217. }
  218. }
  219. // Apply the scaling if necessary
  220. int avgOpacity = nonEmptyGlyphs ? sumOpacity / nonEmptyGlyphs : 255;
  221. if (avgOpacity < 255)
  222. {
  223. float scale = 255.0f / avgOpacity;
  224. for (int i = 0; i < fontInfo.numGlyphs; ++i)
  225. {
  226. for (int y = 0; y < newFace.mGlyphs[i].mHeight; ++y)
  227. {
  228. unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * (y + newFace.mGlyphs[i].mY) + newFace.mGlyphs[i].mX;
  229. for (int x = 0; x < newFace.mGlyphs[i].mWidth; ++x)
  230. {
  231. int pixel = dest[x];
  232. dest[x] = min((int)(pixel * scale), 255);
  233. }
  234. }
  235. }
  236. }
  237. texture->unlock();
  238. setMemoryUse(getMemoryUse() + texWidth * texHeight);
  239. newFace.mTexture = staticCast<Texture>(texture);
  240. }
  241. mFaces[pointSize] = newFace;
  242. return &mFaces[pointSize];
  243. }
  244. catch (...)
  245. {
  246. // An error occurred. Store and return an empty font face
  247. FontFace emptyFace;
  248. FontGlyph emptyGlyph;
  249. emptyGlyph.mX = 0;
  250. emptyGlyph.mY = 0;
  251. emptyGlyph.mWidth = 0;
  252. emptyGlyph.mHeight = 0;
  253. emptyGlyph.mOffsetX = 0;
  254. emptyGlyph.mOffsetY = 0;
  255. emptyGlyph.mAdvanceX = 0;
  256. emptyFace.mGlyphs.push_back(emptyGlyph);
  257. for (unsigned i = 0; i < MAX_FONT_CHARS; ++i)
  258. emptyFace.mGlyphIndex[i] = 0;
  259. mFaces[pointSize] = emptyFace;
  260. return &mFaces[pointSize];
  261. }
  262. }
  263. std::string getSystemFontDirectory()
  264. {
  265. char expandedSystemPath[256];
  266. if (!ExpandEnvironmentStrings("%WinDir%", expandedSystemPath, 256))
  267. return std::string();
  268. return std::string(expandedSystemPath) + "\\Fonts";
  269. }