fontsdf.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include <bgfx/bgfx.h>
  8. #include <bx/timer.h>
  9. #include <bx/math.h>
  10. #include "font/font_manager.h"
  11. #include "font/text_metrics.h"
  12. #include "font/text_buffer_manager.h"
  13. #include "imgui/imgui.h"
  14. namespace
  15. {
  16. TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
  17. {
  18. uint32_t size;
  19. void* data = load(_filePath, &size);
  20. if (NULL != data)
  21. {
  22. TrueTypeHandle handle = _fm->createTtf( (uint8_t*)data, size);
  23. BX_FREE(entry::getAllocator(), data);
  24. return handle;
  25. }
  26. TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
  27. return invalid;
  28. }
  29. class ExampleFontSDF : public entry::AppI
  30. {
  31. public:
  32. ExampleFontSDF(const char* _name, const char* _description, const char* _url)
  33. : entry::AppI(_name, _description, _url)
  34. {
  35. }
  36. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  37. {
  38. Args args(_argc, _argv);
  39. m_width = _width;
  40. m_height = _height;
  41. m_debug = BGFX_DEBUG_NONE;
  42. m_reset = BGFX_RESET_VSYNC;
  43. bgfx::Init init;
  44. init.type = args.m_type;
  45. init.vendorId = args.m_pciId;
  46. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  47. init.platformData.ndt = entry::getNativeDisplayHandle();
  48. init.resolution.width = m_width;
  49. init.resolution.height = m_height;
  50. init.resolution.reset = m_reset;
  51. bgfx::init(init);
  52. // Enable debug text.
  53. bgfx::setDebug(m_debug);
  54. // Set view 0 clear state.
  55. bgfx::setViewClear(0
  56. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  57. , 0x303030ff
  58. , 1.0f
  59. , 0
  60. );
  61. // Initialize Imgui
  62. // This initializes the same allocator used by stb_truetype, so must do that before creating the font manager
  63. imguiCreate();
  64. uint32_t size;
  65. m_txtFile = load("text/sherlock_holmes_a_scandal_in_bohemia_arthur_conan_doyle.txt", &size);
  66. m_bigText = bx::StringView( (const char*)m_txtFile, size);
  67. // Set up some defaults.
  68. m_outlineColor = ImVec4(1.0f, 0.0f, 0.0f, 1.0f);
  69. m_outlineWidth = 1.0f;
  70. m_dropShadowColor = ImVec4(0.0f, 0.0f, 0.0f, 0.35f);
  71. m_dropShadowOffsetX = 1.0f;
  72. m_dropShadowOffsetY = 1.0f;
  73. m_dropShadowSoftener = 2.0f;
  74. m_textScroll = 0.0f;
  75. m_textRotation = 0.0f;
  76. m_textScale = 1.0f;
  77. m_textSize = 14.0f;
  78. // Init the text rendering system.
  79. m_fontManager = NULL;
  80. m_textBufferManager = NULL;
  81. m_font = BGFX_INVALID_HANDLE;
  82. m_fontSdf = BGFX_INVALID_HANDLE;
  83. m_fontScaled = BGFX_INVALID_HANDLE;
  84. m_scrollableBuffer = BGFX_INVALID_HANDLE;
  85. initializeFont(FONT_TYPE_DISTANCE);
  86. m_lineCount = m_metrics.getLineCount(m_bigText);
  87. m_visibleLineCount = 20.0f;
  88. m_textBegin = 0;
  89. m_textEnd = 0;
  90. m_metrics.getSubText(m_bigText, 0, (uint32_t)m_visibleLineCount, m_textBegin, m_textEnd);
  91. initializeTextBuffer(FONT_TYPE_DISTANCE);
  92. applyTextBufferAttributes();
  93. m_textBufferManager->appendText(m_scrollableBuffer, m_fontScaled, m_textBegin, m_textEnd);
  94. }
  95. virtual int shutdown() override
  96. {
  97. imguiDestroy();
  98. unload(m_txtFile);
  99. m_fontManager->destroyTtf(m_font);
  100. // Destroy the fonts.
  101. m_fontManager->destroyFont(m_fontSdf);
  102. m_fontManager->destroyFont(m_fontScaled);
  103. m_textBufferManager->destroyTextBuffer(m_scrollableBuffer);
  104. delete m_textBufferManager;
  105. delete m_fontManager;
  106. // Shutdown bgfx.
  107. bgfx::shutdown();
  108. return 0;
  109. }
  110. bool update() override
  111. {
  112. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  113. {
  114. imguiBeginFrame(m_mouseState.m_mx
  115. , m_mouseState.m_my
  116. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  117. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  118. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  119. , m_mouseState.m_mz
  120. , uint16_t(m_width)
  121. , uint16_t(m_height)
  122. );
  123. showExampleDialog(this);
  124. ImGui::SetNextWindowPos(
  125. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  126. , ImGuiCond_FirstUseEver
  127. );
  128. ImGui::SetNextWindowSize(
  129. ImVec2(m_width / 5.0f, m_height / 2.0f)
  130. , ImGuiCond_FirstUseEver
  131. );
  132. ImGui::Begin("Settings"
  133. , NULL
  134. , 0
  135. );
  136. bool recomputeVisibleText = false;
  137. static int fontTypeIndex = 0;
  138. if (ImGui::Combo("SDF Font Type", &fontTypeIndex, "Standard\0Outline\0Outline_Image\0DropShadow\0DropShadow_Image\0Outline_DropShadow_Image\0\0"))
  139. {
  140. uint32_t fontTypeList[] =
  141. {
  142. FONT_TYPE_DISTANCE,
  143. FONT_TYPE_DISTANCE_OUTLINE,
  144. FONT_TYPE_DISTANCE_OUTLINE_IMAGE,
  145. FONT_TYPE_DISTANCE_DROP_SHADOW,
  146. FONT_TYPE_DISTANCE_DROP_SHADOW_IMAGE,
  147. FONT_TYPE_DISTANCE_OUTLINE_DROP_SHADOW_IMAGE
  148. };
  149. uint32_t fontType = fontTypeList[fontTypeIndex];
  150. initializeFont(fontType);
  151. initializeTextBuffer(fontType);
  152. recomputeVisibleText = true;
  153. }
  154. if (ImGui::SliderFloat("Font size", &m_textSize, 6.0f, 64.0f) )
  155. {
  156. m_fontManager->destroyFont(m_fontScaled);
  157. m_fontScaled = m_fontManager->createScaledFontToPixelSize(m_fontSdf, (uint32_t) m_textSize);
  158. m_metrics = TextLineMetrics(m_fontManager->getFontInfo(m_fontScaled) );
  159. recomputeVisibleText = true;
  160. }
  161. if (ImGui::ColorEdit3("Outline Color", (float*)&m_outlineColor))
  162. {
  163. recomputeVisibleText = true;
  164. }
  165. if (ImGui::SliderFloat("Outline Width", &m_outlineWidth, 0.5f, 5.0f))
  166. {
  167. recomputeVisibleText = true;
  168. }
  169. if (ImGui::ColorEdit4("Drop Shadow Color", (float*)&m_dropShadowColor))
  170. {
  171. recomputeVisibleText = true;
  172. }
  173. if (ImGui::SliderFloat("Drop Shadow Offset X", &m_dropShadowOffsetX, -5.0f, 5.0f))
  174. {
  175. recomputeVisibleText = true;
  176. }
  177. if (ImGui::SliderFloat("Drop Shadow Offset Y", &m_dropShadowOffsetY, -5.0f, 5.0f))
  178. {
  179. recomputeVisibleText = true;
  180. }
  181. if (ImGui::SliderFloat("Drop Shadow Softener", &m_dropShadowSoftener, 0.0f, 5.0f))
  182. {
  183. recomputeVisibleText = true;
  184. }
  185. recomputeVisibleText |= ImGui::SliderFloat("# of lines", &m_visibleLineCount, 1.0f, 177.0f);
  186. recomputeVisibleText |= ImGui::SliderFloat("Scroll", &m_textScroll, 0.0f, (m_lineCount-m_visibleLineCount));
  187. ImGui::SliderFloat("Rotate", &m_textRotation, 0.0f, bx::kPi*2.0f);
  188. recomputeVisibleText |= ImGui::SliderFloat("Scale", &m_textScale, 0.1f, 10.0f);
  189. if (recomputeVisibleText)
  190. {
  191. m_textBufferManager->clearTextBuffer(m_scrollableBuffer);
  192. applyTextBufferAttributes();
  193. m_metrics.getSubText(m_bigText,(uint32_t)m_textScroll, (uint32_t)(m_textScroll+m_visibleLineCount), m_textBegin, m_textEnd);
  194. m_textBufferManager->appendText(m_scrollableBuffer, m_fontScaled, m_textBegin, m_textEnd);
  195. }
  196. ImGui::End();
  197. imguiEndFrame();
  198. // Set view 0 default viewport.
  199. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  200. // This dummy draw call is here to make sure that view 0 is cleared
  201. // if no other draw calls are submitted to view 0.
  202. bgfx::touch(0);
  203. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  204. const bx::Vec3 eye = { 0.0f, 0.0f, -1.0f };
  205. float view[16];
  206. bx::mtxLookAt(view, eye, at);
  207. float centering = 0.0f;
  208. if (bgfx::getRendererType() == bgfx::RendererType::Direct3D9) {
  209. centering = -0.5f;
  210. }
  211. // Setup a top-left ortho matrix for screen space drawing.
  212. const bgfx::Caps* caps = bgfx::getCaps();
  213. {
  214. float ortho[16];
  215. bx::mtxOrtho(ortho, centering, m_width + centering, m_height + centering, centering, -1.0f, 1.0f, 0.0f, caps->homogeneousDepth);
  216. bgfx::setViewTransform(0, view, ortho);
  217. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  218. }
  219. //very crude approximation :(
  220. float textAreaWidth = 0.5f * 66.0f * m_fontManager->getFontInfo(m_fontScaled).maxAdvanceWidth;
  221. float textRotMat[16];
  222. float textCenterMat[16];
  223. float textScaleMat[16];
  224. float screenCenterMat[16];
  225. bx::mtxRotateZ(textRotMat, m_textRotation);
  226. bx::mtxTranslate(textCenterMat, -(textAreaWidth * 0.5f), (-m_visibleLineCount)*m_metrics.getLineHeight()*0.5f, 0);
  227. bx::mtxScale(textScaleMat, m_textScale, m_textScale, 1.0f);
  228. bx::mtxTranslate(screenCenterMat, ( (m_width) * 0.5f), ( (m_height) * 0.5f), 0);
  229. //first translate to text center, then scale, then rotate
  230. float tmpMat[16];
  231. bx::mtxMul(tmpMat, textCenterMat, textRotMat);
  232. float tmpMat2[16];
  233. bx::mtxMul(tmpMat2, tmpMat, textScaleMat);
  234. float tmpMat3[16];
  235. bx::mtxMul(tmpMat3, tmpMat2, screenCenterMat);
  236. // Set model matrix for rendering.
  237. bgfx::setTransform(tmpMat3);
  238. // Draw your text.
  239. m_textBufferManager->submitTextBuffer(m_scrollableBuffer, 0);
  240. // Advance to next frame. Rendering thread will be kicked to
  241. // process submitted rendering primitives.
  242. bgfx::frame();
  243. return true;
  244. }
  245. return false;
  246. }
  247. void initializeFont(uint32_t fontType)
  248. {
  249. if (m_font.idx != bgfx::kInvalidHandle)
  250. {
  251. m_fontManager->destroyTtf(m_font);
  252. }
  253. if (m_scrollableBuffer.idx != bgfx::kInvalidHandle)
  254. {
  255. m_textBufferManager->destroyTextBuffer(m_scrollableBuffer);
  256. }
  257. if (m_fontScaled.idx != bgfx::kInvalidHandle)
  258. {
  259. m_fontManager->destroyFont(m_fontScaled);
  260. }
  261. if (m_fontSdf.idx != bgfx::kInvalidHandle)
  262. {
  263. m_fontManager->destroyFont(m_fontSdf);
  264. }
  265. delete m_textBufferManager;
  266. delete m_fontManager;
  267. m_fontManager = new FontManager(512);
  268. m_textBufferManager = new TextBufferManager(m_fontManager);
  269. m_font = loadTtf(m_fontManager, "font/special_elite.ttf");
  270. m_fontSdf = m_fontManager->createFontByPixelSize(m_font, 0, 48, fontType, 6 + 2, 6 + 2);
  271. m_fontScaled = m_fontManager->createScaledFontToPixelSize(m_fontSdf, (uint32_t) m_textSize);
  272. if (fontType & FONT_TYPE_MASK_DISTANCE_IMAGE)
  273. {
  274. float extraScale = 2.0f;
  275. bimg::ImageContainer* glyphImage = imageLoad("font/glyph_space.png", bgfx::TextureFormat::Enum::BGRA8);
  276. m_fontManager->addGlyphBitmap(m_fontSdf, 32, (uint16_t)glyphImage->m_width, (uint16_t)glyphImage->m_height, (uint16_t)(glyphImage->m_width * 4), extraScale, (const uint8_t*)glyphImage->m_data, 0.0f, -3.0f);
  277. glyphImage = imageLoad("font/glyph_long.png", bgfx::TextureFormat::Enum::BGRA8);
  278. m_fontManager->addGlyphBitmap(m_fontSdf, 65, (uint16_t)glyphImage->m_width, (uint16_t)glyphImage->m_height, (uint16_t)(glyphImage->m_width * 4), extraScale, (const uint8_t*)glyphImage->m_data, 0.0f, -3.0f);
  279. }
  280. m_metrics = TextLineMetrics(m_fontManager->getFontInfo(m_fontScaled) );
  281. }
  282. void initializeTextBuffer(uint32_t fontType)
  283. {
  284. m_scrollableBuffer = m_textBufferManager->createTextBuffer(fontType, BufferType::Transient);
  285. }
  286. void applyTextBufferAttributes()
  287. {
  288. m_textBufferManager->setTextColor(m_scrollableBuffer, 0xFFFFFFFF);
  289. m_textBufferManager->setOutlineColor(m_scrollableBuffer, ((uint32_t)(m_outlineColor.x * 255) << 24) | ((uint32_t)(m_outlineColor.y * 255) << 16) | ((uint32_t)(m_outlineColor.z * 255) << 8) | ((uint32_t)(m_outlineColor.w * 255)));
  290. m_textBufferManager->setOutlineWidth(m_scrollableBuffer, m_outlineWidth);
  291. m_textBufferManager->setDropShadowColor(m_scrollableBuffer, ((uint32_t)(m_dropShadowColor.x * 255) << 24) | ((uint32_t)(m_dropShadowColor.y * 255) << 16) | ((uint32_t)(m_dropShadowColor.z * 255) << 8) | ((uint32_t)(m_dropShadowColor.w * 255)));
  292. m_textBufferManager->setDropShadowOffset(m_scrollableBuffer, m_dropShadowOffsetX, m_dropShadowOffsetY);
  293. m_textBufferManager->setDropShadowSoftener(m_scrollableBuffer, m_dropShadowSoftener);
  294. }
  295. entry::MouseState m_mouseState;
  296. uint32_t m_width;
  297. uint32_t m_height;
  298. uint32_t m_debug;
  299. uint32_t m_reset;
  300. void* m_txtFile;
  301. bx::StringView m_bigText;
  302. // Init the text rendering system.
  303. FontManager* m_fontManager;
  304. TextBufferManager* m_textBufferManager;
  305. TrueTypeHandle m_font;
  306. FontHandle m_fontSdf;
  307. FontHandle m_fontScaled;
  308. TextBufferHandle m_scrollableBuffer;
  309. TextLineMetrics m_metrics = TextLineMetrics(FontInfo());
  310. uint32_t m_lineCount;
  311. float m_visibleLineCount;
  312. const char* m_textBegin;
  313. const char* m_textEnd;
  314. ImVec4 m_outlineColor;
  315. float m_outlineWidth;
  316. ImVec4 m_dropShadowColor;
  317. float m_dropShadowOffsetX;
  318. float m_dropShadowOffsetY;
  319. float m_dropShadowSoftener;
  320. float m_textScroll;
  321. float m_textRotation;
  322. float m_textScale;
  323. float m_textSize;
  324. };
  325. } // namespace
  326. ENTRY_IMPLEMENT_MAIN(
  327. ExampleFontSDF
  328. , "11-fontsdf"
  329. , "Use a single distance field font to render text of various size."
  330. , "https://bkaradzic.github.io/bgfx/examples.html#fontsdf"
  331. );