FontTexture.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Purpose:
  9. // - Create and update a texture with the most recently used glyphs
  10. #if !defined(USE_NULLFONT_ALWAYS)
  11. #include <AtomLyIntegration/AtomFont/FontTexture.h>
  12. #include <AzCore/IO/FileIO.h>
  13. #include <AzCore/std/string/conversions.h>
  14. //-------------------------------------------------------------------------------------------------
  15. AZ::FontTexture::FontTexture()
  16. : m_slotUsage(1)
  17. , m_width(0)
  18. , m_height(0)
  19. , m_invWidth(0.0f)
  20. , m_invHeight(0.0f)
  21. , m_cellWidth(0)
  22. , m_cellHeight(0)
  23. , m_textureCellWidth(0)
  24. , m_textureCellHeight(0)
  25. , m_widthCellCount(0)
  26. , m_heightCellCount(0)
  27. , m_textureSlotCount(0)
  28. , m_buffer(0)
  29. , m_smoothMethod(AZ::FontSmoothMethod::None)
  30. , m_smoothAmount(AZ::FontSmoothAmount::None)
  31. {
  32. }
  33. //-------------------------------------------------------------------------------------------------
  34. AZ::FontTexture::~FontTexture()
  35. {
  36. Release();
  37. }
  38. //-------------------------------------------------------------------------------------------------
  39. int AZ::FontTexture::CreateFromFile(const AZStd::string& fileName, int width, int height, AZ::FontSmoothMethod smoothMethod, AZ::FontSmoothAmount smoothAmount, int widthCellCount, int heightCellCount)
  40. {
  41. if (!m_glyphCache.LoadFontFromFile(fileName))
  42. {
  43. Release();
  44. return 0;
  45. }
  46. if (!Create(width, height, smoothMethod, smoothAmount, widthCellCount, heightCellCount))
  47. {
  48. return 0;
  49. }
  50. return 1;
  51. }
  52. //-------------------------------------------------------------------------------------------------
  53. int AZ::FontTexture::CreateFromMemory(unsigned char* fileData, int dataSize, int width, int height, AZ::FontSmoothMethod smoothMethod, AZ::FontSmoothAmount smoothAmount, int widthCellCount, int heightCellCount, float sizeRatio)
  54. {
  55. if (!m_glyphCache.LoadFontFromMemory(fileData, dataSize))
  56. {
  57. Release();
  58. return 0;
  59. }
  60. if (!Create(width, height, smoothMethod, smoothAmount, widthCellCount, heightCellCount, sizeRatio))
  61. {
  62. return 0;
  63. }
  64. return 1;
  65. }
  66. //-------------------------------------------------------------------------------------------------
  67. int AZ::FontTexture::Create(int width, int height, AZ::FontSmoothMethod smoothMethod, AZ::FontSmoothAmount smoothAmount, int widthCellCount, int heightCellCount, float sizeRatio)
  68. {
  69. m_buffer = new FONT_TEXTURE_TYPE[width * height];
  70. if (!m_buffer)
  71. {
  72. return 0;
  73. }
  74. memset(m_buffer, 0, width * height * sizeof(FONT_TEXTURE_TYPE));
  75. if (!(widthCellCount * heightCellCount))
  76. {
  77. return 0;
  78. }
  79. m_width = width;
  80. m_height = height;
  81. m_invWidth = 1.0f / (float)width;
  82. m_invHeight = 1.0f / (float)height;
  83. m_widthCellCount = widthCellCount;
  84. m_heightCellCount = heightCellCount;
  85. m_textureSlotCount = m_widthCellCount * m_heightCellCount;
  86. m_smoothMethod = smoothMethod;
  87. m_smoothAmount = smoothAmount;
  88. m_cellWidth = m_width / m_widthCellCount;
  89. m_cellHeight = m_height / m_heightCellCount;
  90. m_textureCellWidth = m_cellWidth * m_invWidth;
  91. m_textureCellHeight = m_cellHeight * m_invHeight;
  92. if (!m_glyphCache.Create(AZ_FONT_GLYPH_CACHE_SIZE, m_cellWidth, m_cellHeight, smoothMethod, smoothAmount, sizeRatio))
  93. {
  94. Release();
  95. return 0;
  96. }
  97. if (!CreateSlotList(m_textureSlotCount))
  98. {
  99. Release();
  100. return 0;
  101. }
  102. return 1;
  103. }
  104. //-------------------------------------------------------------------------------------------------
  105. int AZ::FontTexture::Release()
  106. {
  107. delete[] m_buffer;
  108. m_buffer = 0;
  109. ReleaseSlotList();
  110. m_slotIndexMap.clear();
  111. m_glyphCache.Release();
  112. m_widthCellCount = 0;
  113. m_heightCellCount = 0;
  114. m_textureSlotCount = 0;
  115. m_width = 0;
  116. m_height = 0;
  117. m_invWidth = 0.0f;
  118. m_invHeight = 0.0f;
  119. m_cellWidth = 0;
  120. m_cellHeight = 0;
  121. m_smoothMethod = AZ::FontSmoothMethod::None;
  122. m_smoothAmount = AZ::FontSmoothAmount::None;
  123. m_textureCellWidth = 0.0f;
  124. m_textureCellHeight = 0.0f;
  125. m_slotUsage = 1;
  126. return 1;
  127. }
  128. //-------------------------------------------------------------------------------------------------
  129. uint32_t AZ::FontTexture::GetSlotChar(int slotIndex) const
  130. {
  131. return m_slotList[slotIndex]->m_currentCharacter;
  132. }
  133. //-------------------------------------------------------------------------------------------------
  134. AZ::TextureSlot* AZ::FontTexture::GetCharSlot(uint32_t character, const AZ::AtomFont::GlyphSize& glyphSize)
  135. {
  136. TextureSlotKey slotKey = GetTextureSlotKey(character, glyphSize);
  137. TextureSlotTableItor pItor = m_slotIndexMap.find(slotKey);
  138. if (pItor != m_slotIndexMap.end())
  139. {
  140. return pItor->second;
  141. }
  142. return 0;
  143. }
  144. //-------------------------------------------------------------------------------------------------
  145. AZ::TextureSlot* AZ::FontTexture::GetLRUSlot()
  146. {
  147. uint16_t wMaxSlotAge = 0;
  148. TextureSlot* pLRUSlot = 0;
  149. TextureSlot* slot;
  150. TextureSlotListItor pItor = m_slotList.begin();
  151. while (pItor != m_slotList.end())
  152. {
  153. slot = *pItor;
  154. if (slot->m_slotUsage == 0)
  155. {
  156. return slot;
  157. }
  158. else
  159. {
  160. uint16_t slotAge = m_slotUsage - slot->m_slotUsage;
  161. if (slotAge > wMaxSlotAge)
  162. {
  163. pLRUSlot = slot;
  164. wMaxSlotAge = slotAge;
  165. }
  166. }
  167. ++pItor;
  168. }
  169. return pLRUSlot;
  170. }
  171. //-------------------------------------------------------------------------------------------------
  172. AZ::TextureSlot* AZ::FontTexture::GetMRUSlot()
  173. {
  174. uint16_t wMinSlotAge = 0xFFFF;
  175. TextureSlot* pMRUSlot = 0;
  176. TextureSlot* slot;
  177. TextureSlotListItor pItor = m_slotList.begin();
  178. while (pItor != m_slotList.end())
  179. {
  180. slot = *pItor;
  181. if (slot->m_slotUsage != 0)
  182. {
  183. uint16_t slotAge = m_slotUsage - slot->m_slotUsage;
  184. if (slotAge > wMinSlotAge)
  185. {
  186. pMRUSlot = slot;
  187. wMinSlotAge = slotAge;
  188. }
  189. }
  190. ++pItor;
  191. }
  192. return pMRUSlot;
  193. }
  194. //-------------------------------------------------------------------------------------------------
  195. int AZ::FontTexture::PreCacheString(const char* string, int* updated, float sizeRatio, const AZ::AtomFont::GlyphSize& glyphSize, const FFont::FontHintParams& fontHintParams)
  196. {
  197. AZ::AtomFont::GlyphSize clampedGlyphSize = ClampGlyphSize(glyphSize, m_cellWidth, m_cellHeight);
  198. uint16_t slotUsage = m_slotUsage++;
  199. int updateCount = 0;
  200. AZStd::wstring stringW;
  201. AZStd::to_wstring(stringW, string);
  202. for (wchar_t character : stringW)
  203. {
  204. TextureSlot* slot = GetCharSlot(character, clampedGlyphSize);
  205. if (!slot)
  206. {
  207. slot = GetLRUSlot();
  208. if (!slot)
  209. {
  210. return 0;
  211. }
  212. if (!UpdateSlot(slot->m_textureSlot, slotUsage, character, sizeRatio, clampedGlyphSize, fontHintParams))
  213. {
  214. return 0;
  215. }
  216. ++updateCount;
  217. }
  218. else
  219. {
  220. slot->m_slotUsage = slotUsage;
  221. }
  222. }
  223. if (updated)
  224. {
  225. *updated = updateCount;
  226. }
  227. if (updateCount)
  228. {
  229. return 1;
  230. }
  231. return 2;
  232. }
  233. //-------------------------------------------------------------------------------------------------
  234. void AZ::FontTexture::GetTextureCoord(AZ::TextureSlot* slot, float texCoords[4],
  235. int& characterSizeX, int& characterSizeY, int& m_characterOffsetX, int& m_characterOffsetY,
  236. const AZ::AtomFont::GlyphSize& glyphSize) const
  237. {
  238. if (!slot)
  239. {
  240. return; // expected behavior
  241. }
  242. // Re-rendered glyphs are stored at smaller sizes than glyphs rendered at
  243. // the (maximum) font texture slot resolution. We scale the returned width
  244. // and height of the (actual) rendered glyph sizes so its transparent to
  245. // callers that the glyph is actually smaller (from being re-rendered).
  246. const float requestSizeWidthScale = AZ::GetMin<float>(1.0f, GetRequestSizeWidthScale(glyphSize));
  247. const float requestSizeHeightScale = AZ::GetMin<float>(1.0f, GetRequestSizeHeightScale(glyphSize));
  248. const float invRequestSizeWidthScale = 1.0f / requestSizeWidthScale;
  249. const float invRequestSizeHeightScale = 1.0f / requestSizeHeightScale;
  250. // The inverse scale grows as the glyph size decreases. Once the glyph size
  251. // reaches the font texture's max slot dimensions, we cap width/height scale
  252. // since the text draw context will apply normal (as opposed to re-rendered)
  253. // scaling.
  254. int iChWidth = static_cast<int>(slot->m_characterWidth * invRequestSizeWidthScale);
  255. int iChHeight = static_cast<int>(slot->m_characterHeight * invRequestSizeHeightScale);
  256. float slotCoord0 = slot->m_texCoords[0];
  257. float slotCoord1 = slot->m_texCoords[1];
  258. texCoords[0] = slotCoord0 - m_invWidth; // extra pixel for nicer bilinear filter
  259. texCoords[1] = slotCoord1 - m_invHeight; // extra pixel for nicer bilinear filter
  260. // UV coordinates also must be scaled relative to the re-rendered glyph size
  261. // as well. Width scale must be capped at 1.0f since glyph can't grow
  262. // beyond the slot's resolution.
  263. texCoords[2] = slotCoord0 + (((float)iChWidth * m_invWidth) * requestSizeWidthScale);
  264. texCoords[3] = slotCoord1 + (((float)iChHeight * m_invHeight) * requestSizeHeightScale);
  265. characterSizeX = iChWidth + 1; // extra pixel for nicer bilinear filter
  266. characterSizeY = iChHeight + 1; // extra pixel for nicer bilinear filter
  267. // Offsets are scaled accordingly when the rendered glyph size is smaller
  268. // than the glyph/slot dimensions, but otherwise we expect the text draw
  269. // context to apply scaling beyond that.
  270. m_characterOffsetX = static_cast<int>(slot->m_characterOffsetX * invRequestSizeWidthScale);
  271. m_characterOffsetY = static_cast<int>(slot->m_characterOffsetY * invRequestSizeHeightScale);
  272. }
  273. //-------------------------------------------------------------------------------------------------
  274. int AZ::FontTexture::GetCharacterWidth(uint32_t character) const
  275. {
  276. TextureSlotTableItorConst pItor = m_slotIndexMap.find(GetTextureSlotKey(character));
  277. if (pItor == m_slotIndexMap.end())
  278. {
  279. return 0;
  280. }
  281. const TextureSlot& rSlot = *pItor->second;
  282. // For proportional fonts, add one pixel of spacing for aesthetic reasons
  283. int proportionalOffset = GetMonospaced() ? 0 : 1;
  284. return rSlot.m_characterWidth + proportionalOffset;
  285. }
  286. //-------------------------------------------------------------------------------------------------
  287. int AZ::FontTexture::GetHorizontalAdvance(uint32_t character, const AZ::AtomFont::GlyphSize& glyphSize) const
  288. {
  289. TextureSlotTableItorConst pItor = m_slotIndexMap.find(GetTextureSlotKey(character, glyphSize));
  290. if (pItor == m_slotIndexMap.end())
  291. {
  292. return 0;
  293. }
  294. const TextureSlot& rSlot = *pItor->second;
  295. // Re-rendered glyphs are stored at smaller sizes than glyphs rendered at
  296. // the (maximum) font texture slot resolution. We scale the returned width
  297. // and height of the (actual) rendered glyph sizes so its transparent to
  298. // callers that the glyph is actually smaller (from being re-rendered).
  299. const float requestSizeWidthScale = GetRequestSizeWidthScale(glyphSize);
  300. const float invRequestSizeWidthScale = 1.0f / requestSizeWidthScale;
  301. // Only multiply by 1.0f when glyphsize is greater than cell width because we assume that callers
  302. // will use the font draw text context to scale the value appropriately.
  303. return static_cast<int>(rSlot.m_horizontalAdvance * AZ::GetMax<float>(1.0f, invRequestSizeWidthScale));
  304. }
  305. //-------------------------------------------------------------------------------------------------
  306. /*
  307. int AZ::FontTexture::GetCharHeightByChar(wchar_t character)
  308. {
  309. TextureSlotTableItor pItor = m_slotIndexMap.find(character);
  310. if (pItor != m_slotIndexMap.end())
  311. {
  312. return pItor->second->m_characterHeight;
  313. }
  314. return 0;
  315. }
  316. */
  317. //-------------------------------------------------------------------------------------------------
  318. Vec2 AZ::FontTexture::GetKerning(uint32_t leftGlyph, uint32_t rightGlyph)
  319. {
  320. return m_glyphCache.GetKerning(leftGlyph, rightGlyph);
  321. }
  322. //-------------------------------------------------------------------------------------------------
  323. float AZ::FontTexture::GetAscenderToHeightRatio()
  324. {
  325. return m_glyphCache.GetAscenderToHeightRatio();
  326. }
  327. //-------------------------------------------------------------------------------------------------
  328. int AZ::FontTexture::CreateSlotList(int listSize)
  329. {
  330. int y, x;
  331. for (int i = 0; i < listSize; i++)
  332. {
  333. TextureSlot* pTextureSlot = new TextureSlot;
  334. if (!pTextureSlot)
  335. {
  336. return 0;
  337. }
  338. pTextureSlot->m_textureSlot = i;
  339. pTextureSlot->Reset();
  340. y = i / m_widthCellCount;
  341. x = i % m_widthCellCount;
  342. pTextureSlot->m_texCoords[0] = (float)(x * m_textureCellWidth) + (0.5f / (float)m_width);
  343. pTextureSlot->m_texCoords[1] = (float)(y * m_textureCellHeight) + (0.5f / (float)m_height);
  344. m_slotList.push_back(pTextureSlot);
  345. }
  346. return 1;
  347. }
  348. //-------------------------------------------------------------------------------------------------
  349. int AZ::FontTexture::ReleaseSlotList()
  350. {
  351. TextureSlotListItor pItor = m_slotList.begin();
  352. while (pItor != m_slotList.end())
  353. {
  354. delete (*pItor);
  355. pItor = m_slotList.erase(pItor);
  356. }
  357. return 1;
  358. }
  359. //-------------------------------------------------------------------------------------------------
  360. int AZ::FontTexture::UpdateSlot(int slotIndex, uint16_t slotUsage, uint32_t character, float sizeRatio, const AZ::AtomFont::GlyphSize& glyphSize, const FFont::FontHintParams& fontHintParams)
  361. {
  362. TextureSlot* slot = m_slotList[slotIndex];
  363. if (!slot)
  364. {
  365. return 0;
  366. }
  367. TextureSlotTableItor pItor = m_slotIndexMap.find(GetTextureSlotKey(slot->m_currentCharacter, slot->m_glyphSize));
  368. if (pItor != m_slotIndexMap.end())
  369. {
  370. m_slotIndexMap.erase(pItor);
  371. }
  372. m_slotIndexMap.insert(TextureSlotTableEntry(GetTextureSlotKey(character, glyphSize), slot));
  373. slot->m_glyphSize = glyphSize;
  374. slot->m_slotUsage = slotUsage;
  375. slot->m_currentCharacter = character;
  376. int width = 0;
  377. int height = 0;
  378. // blit the char glyph into the texture
  379. int x = slot->m_textureSlot % m_widthCellCount;
  380. int y = slot->m_textureSlot / m_widthCellCount;
  381. GlyphBitmap* glyphBitmap;
  382. if (glyphSize.x > 0 && glyphSize.y > 0)
  383. {
  384. m_glyphCache.SetGlyphBitmapSize(glyphSize.x, glyphSize.y, sizeRatio);
  385. }
  386. if (!m_glyphCache.GetGlyph(&glyphBitmap, &slot->m_horizontalAdvance, &width, &height, slot->m_characterOffsetX, slot->m_characterOffsetY, character, glyphSize, fontHintParams))
  387. {
  388. return 0;
  389. }
  390. slot->m_characterWidth = static_cast<uint8_t>(width);
  391. slot->m_characterHeight = static_cast<uint8_t>(height);
  392. // Add a pixel along width and height to avoid artifacts being rendered
  393. // from a previous glyph in this slot due to bilinear filtering. The source
  394. // glyph bitmap buffer is presumed to be cleared prior to FreeType rendering
  395. // to the bitmap.
  396. const int blitWidth = AZ::GetMin<int>(width + 1, m_cellWidth);
  397. const int blitHeight = AZ::GetMin<int>(height + 1, m_cellHeight);
  398. glyphBitmap->BlitTo8(m_buffer, 0, 0,
  399. blitWidth, blitHeight, x * m_cellWidth, y * m_cellHeight, m_width);
  400. return 1;
  401. }
  402. //-------------------------------------------------------------------------------------------------
  403. void AZ::FontTexture::CreateGradientSlot()
  404. {
  405. TextureSlot* slot = GetGradientSlot();
  406. assert(slot->m_currentCharacter == (uint32_t)~0); // 0 needs to be unused spot
  407. slot->Reset();
  408. slot->m_characterWidth = static_cast<uint8_t>(m_cellWidth - 2);
  409. slot->m_characterHeight = static_cast<uint8_t>(m_cellHeight - 2);
  410. slot->SetNotReusable();
  411. int x = slot->m_textureSlot % m_widthCellCount;
  412. int y = slot->m_textureSlot / m_widthCellCount;
  413. assert(sizeof(*m_buffer) == sizeof(uint8_t));
  414. uint8_t* buffer = &m_buffer[x * m_cellWidth + y * m_cellHeight * m_width];
  415. for (uint32_t dwY = 0; dwY < slot->m_characterHeight; ++dwY)
  416. {
  417. for (uint32_t dwX = 0; dwX < slot->m_characterWidth; ++dwX)
  418. {
  419. buffer[dwX + dwY * m_width] = static_cast<uint8_t>(dwY * 255 / (slot->m_characterHeight - 1));
  420. }
  421. }
  422. }
  423. //-------------------------------------------------------------------------------------------------
  424. AZ::TextureSlot* AZ::FontTexture::GetGradientSlot()
  425. {
  426. return m_slotList[0];
  427. }
  428. //-------------------------------------------------------------------------------------------------
  429. AZ::FontTexture::TextureSlotKey AZ::FontTexture::GetTextureSlotKey(uint32_t character, const AZ::AtomFont::GlyphSize& glyphSize) const
  430. {
  431. const AZ::AtomFont::GlyphSize clampedGlyphSize(ClampGlyphSize(glyphSize, m_cellWidth, m_cellHeight));
  432. return AZ::FontTexture::TextureSlotKey(clampedGlyphSize, character);
  433. }
  434. AZ::AtomFont::GlyphSize AZ::FontTexture::ClampGlyphSize(const AZ::AtomFont::GlyphSize& glyphSize, int cellWidth, int cellHeight)
  435. {
  436. const AZ::AtomFont::GlyphSize maxCellDimensions(cellWidth, cellHeight);
  437. AZ::AtomFont::GlyphSize clampedGlyphSize(glyphSize);
  438. const bool hasZeroDimension = glyphSize.x == 0 || glyphSize.y == 0;
  439. const bool isDefaultSize = glyphSize == AZ::AtomFont::defaultGlyphSize;
  440. const bool exceedsDimensions = glyphSize.x > cellWidth || glyphSize.y > cellHeight;
  441. const bool useMaxCellDimension = hasZeroDimension || isDefaultSize || exceedsDimensions;
  442. if (useMaxCellDimension)
  443. {
  444. clampedGlyphSize = maxCellDimensions;
  445. }
  446. return clampedGlyphSize;
  447. }
  448. #endif // #if !defined(USE_NULLFONT_ALWAYS)