fontsdf.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include <bgfx.h>
  7. #include <bx/timer.h>
  8. #include "fpumath.h"
  9. #include "font/font_manager.h"
  10. #include "font/text_metrics.h"
  11. #include "font/text_buffer_manager.h"
  12. #include "imgui/imgui.h"
  13. #include <stdio.h>
  14. #include <string.h>
  15. long int fsize(FILE* _file)
  16. {
  17. long int pos = ftell(_file);
  18. fseek(_file, 0L, SEEK_END);
  19. long int size = ftell(_file);
  20. fseek(_file, pos, SEEK_SET);
  21. return size;
  22. }
  23. static char* loadText(const char* _filePath)
  24. {
  25. FILE* file = fopen(_filePath, "rb");
  26. if (NULL != file)
  27. {
  28. uint32_t size = (uint32_t)fsize(file);
  29. char* mem = (char*)malloc(size+1);
  30. size_t ignore = fread(mem, 1, size, file);
  31. BX_UNUSED(ignore);
  32. fclose(file);
  33. mem[size-1] = '\0';
  34. return mem;
  35. }
  36. return NULL;
  37. }
  38. TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
  39. {
  40. FILE* file = fopen(_filePath, "rb");
  41. if (NULL != file)
  42. {
  43. uint32_t size = (uint32_t)fsize(file);
  44. uint8_t* mem = (uint8_t*)malloc(size+1);
  45. size_t ignore = fread(mem, 1, size, file);
  46. BX_UNUSED(ignore);
  47. fclose(file);
  48. mem[size-1] = '\0';
  49. TrueTypeHandle handle = _fm->createTtf(mem, size);
  50. free(mem);
  51. return handle;
  52. }
  53. TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
  54. return invalid;
  55. }
  56. int _main_(int /*_argc*/, char** /*_argv*/)
  57. {
  58. uint32_t width = 1280;
  59. uint32_t height = 720;
  60. uint32_t debug = BGFX_DEBUG_TEXT;
  61. uint32_t reset = BGFX_RESET_VSYNC;
  62. bgfx::init();
  63. bgfx::reset(width, height, reset);
  64. // Enable debug text.
  65. bgfx::setDebug(debug);
  66. // Set view 0 clear state.
  67. bgfx::setViewClear(0
  68. , BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
  69. , 0x303030ff
  70. , 1.0f
  71. , 0
  72. );
  73. FILE* file = fopen("font/droidsans.ttf", "rb");
  74. uint32_t size = (uint32_t)fsize(file);
  75. void* data = malloc(size);
  76. size_t ignore = fread(data, 1, size, file);
  77. BX_UNUSED(ignore);
  78. fclose(file);
  79. imguiCreate(data, size);
  80. free(data);
  81. char* bigText = loadText( "text/sherlock_holmes_a_scandal_in_bohemia_arthur_conan_doyle.txt");
  82. // Init the text rendering system.
  83. FontManager* fontManager = new FontManager(512);
  84. TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
  85. TrueTypeHandle font = loadTtf(fontManager, "font/special_elite.ttf");
  86. // Create a distance field font.
  87. FontHandle fontSdf = fontManager->createFontByPixelSize(font, 0, 48, FONT_TYPE_DISTANCE);
  88. // Create a scaled down version of the same font (without adding anything to the atlas).
  89. FontHandle fontScaled = fontManager->createScaledFontToPixelSize(fontSdf, 14);
  90. TextLineMetrics metrics(fontManager->getFontInfo(fontScaled) );
  91. uint32_t lineCount = metrics.getLineCount(bigText);
  92. float visibleLineCount = 20.0f;
  93. const char* textBegin = 0;
  94. const char* textEnd = 0;
  95. metrics.getSubText(bigText, 0, (uint32_t)visibleLineCount, textBegin, textEnd);
  96. TextBufferHandle scrollableBuffer = textBufferManager->createTextBuffer(FONT_TYPE_DISTANCE, BufferType::Transient);
  97. textBufferManager->setTextColor(scrollableBuffer, 0xFFFFFFFF);
  98. textBufferManager->appendText(scrollableBuffer, fontScaled, textBegin, textEnd);
  99. entry::MouseState mouseState;
  100. int32_t scrollArea = 0;
  101. const int32_t guiPanelWidth = 250;
  102. const int32_t guiPanelHeight = 200;
  103. float textScroll = 0.0f;
  104. float textRotation = 0.0f;
  105. float textScale = 1.0f;
  106. float textSize = 14.0f;
  107. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  108. {
  109. imguiBeginFrame(mouseState.m_mx
  110. , mouseState.m_my
  111. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  112. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  113. , 0
  114. , width
  115. , height
  116. );
  117. imguiBeginScrollArea("Text Area", width - guiPanelWidth - 10, 10, guiPanelWidth, guiPanelHeight, &scrollArea);
  118. imguiSeparatorLine();
  119. bool recomputeVisibleText = false;
  120. recomputeVisibleText |= imguiSlider("Number of lines", &visibleLineCount, 1.0f, 177.0f , 1.0f);
  121. if (imguiSlider("Font size", &textSize, 6.0f, 64.0f , 1.0f) )
  122. {
  123. fontManager->destroyFont(fontScaled);
  124. fontScaled = fontManager->createScaledFontToPixelSize(fontSdf, (uint32_t) textSize);
  125. metrics = TextLineMetrics(fontManager->getFontInfo(fontScaled) );
  126. recomputeVisibleText = true;
  127. }
  128. recomputeVisibleText |= imguiSlider("Scroll", &textScroll, 0.0f, (lineCount-visibleLineCount) , 1.0f);
  129. imguiSlider("Rotate", &textRotation, 0.0f, (float) M_PI *2.0f , 0.1f);
  130. recomputeVisibleText |= imguiSlider("Scale", &textScale, 0.1f, 10.0f , 0.1f);
  131. if (recomputeVisibleText)
  132. {
  133. textBufferManager->clearTextBuffer(scrollableBuffer);
  134. metrics.getSubText(bigText,(uint32_t)textScroll, (uint32_t)(textScroll+visibleLineCount), textBegin, textEnd);
  135. textBufferManager->appendText(scrollableBuffer, fontScaled, textBegin, textEnd);
  136. }
  137. imguiEndScrollArea();
  138. imguiEndFrame();
  139. // Set view 0 default viewport.
  140. bgfx::setViewRect(0, 0, 0, width, height);
  141. // This dummy draw call is here to make sure that view 0 is cleared
  142. // if no other draw calls are submitted to view 0.
  143. bgfx::submit(0);
  144. int64_t now = bx::getHPCounter();
  145. static int64_t last = now;
  146. const int64_t frameTime = now - last;
  147. last = now;
  148. const double freq = double(bx::getHPFrequency() );
  149. const double toMs = 1000.0 / freq;
  150. // Use debug font to print32_t information about this example.
  151. bgfx::dbgTextClear();
  152. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/11-fontsdf");
  153. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use a single distance field font to render text of various size.");
  154. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime) * toMs);
  155. float at[3] = { 0, 0, 0.0f };
  156. float eye[3] = {0, 0, -1.0f };
  157. float view[16];
  158. float proj[16];
  159. mtxLookAt(view, eye, at);
  160. float centering = 0.5f;
  161. // Setup a top-left ortho matrix for screen space drawing.
  162. mtxOrtho(proj, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
  163. // Set view and projection matrix for view 0.
  164. bgfx::setViewTransform(0, view, proj);
  165. //very crude approximation :(
  166. float textAreaWidth = 0.5f * 66.0f * fontManager->getFontInfo(fontScaled).maxAdvanceWidth;
  167. float textRotMat[16];
  168. float textCenterMat[16];
  169. float textScaleMat[16];
  170. float screenCenterMat[16];
  171. mtxRotateZ(textRotMat, textRotation);
  172. mtxTranslate(textCenterMat, -(textAreaWidth * 0.5f), (-visibleLineCount)*metrics.getLineHeight()*0.5f, 0);
  173. mtxScale(textScaleMat, textScale, textScale, 1.0f);
  174. mtxTranslate(screenCenterMat, ( (width) * 0.5f), ( (height) * 0.5f), 0);
  175. //first translate to text center, then scale, then rotate
  176. float tmpMat[16];
  177. mtxMul(tmpMat, textCenterMat, textRotMat);
  178. float tmpMat2[16];
  179. mtxMul(tmpMat2, tmpMat, textScaleMat);
  180. float tmpMat3[16];
  181. mtxMul(tmpMat3, tmpMat2, screenCenterMat);
  182. // Set model matrix for rendering.
  183. bgfx::setTransform(tmpMat3);
  184. // Draw your text.
  185. textBufferManager->submitTextBuffer(scrollableBuffer, 0);
  186. // Advance to next frame. Rendering thread will be kicked to
  187. // process submitted rendering primitives.
  188. bgfx::frame();
  189. }
  190. imguiDestroy();
  191. free(bigText);
  192. fontManager->destroyTtf(font);
  193. // Destroy the fonts.
  194. fontManager->destroyFont(fontSdf);
  195. fontManager->destroyFont(fontScaled);
  196. textBufferManager->destroyTextBuffer(scrollableBuffer);
  197. delete textBufferManager;
  198. delete fontManager;
  199. // Shutdown bgfx.
  200. bgfx::shutdown();
  201. return 0;
  202. }