Font.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=11"/>
  6. <meta name="generator" content="Doxygen 1.9.8"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <title>Nuklear: Font</title>
  9. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  10. <script type="text/javascript" src="jquery.js"></script>
  11. <script type="text/javascript" src="dynsections.js"></script>
  12. <link href="navtree.css" rel="stylesheet" type="text/css"/>
  13. <script type="text/javascript" src="resize.js"></script>
  14. <script type="text/javascript" src="navtreedata.js"></script>
  15. <script type="text/javascript" src="navtree.js"></script>
  16. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  17. <script type="text/javascript" src="search/searchdata.js"></script>
  18. <script type="text/javascript" src="search/search.js"></script>
  19. <script type="text/javascript">
  20. /* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
  21. $(document).ready(function() { init_search(); });
  22. /* @license-end */
  23. </script>
  24. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  25. <link href="doxygen-awesome.css" rel="stylesheet" type="text/css"/>
  26. </head>
  27. <body>
  28. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  29. <div id="titlearea">
  30. <table cellspacing="0" cellpadding="0">
  31. <tbody>
  32. <tr id="projectrow">
  33. <td id="projectalign">
  34. <div id="projectname">Nuklear
  35. </div>
  36. <div id="projectbrief">This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.</div>
  37. </td>
  38. <td> <div id="MSearchBox" class="MSearchBoxInactive">
  39. <span class="left">
  40. <span id="MSearchSelect" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()">&#160;</span>
  41. <input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
  42. onfocus="searchBox.OnSearchFieldFocus(true)"
  43. onblur="searchBox.OnSearchFieldFocus(false)"
  44. onkeyup="searchBox.OnSearchFieldChange(event)"/>
  45. </span><span class="right">
  46. <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
  47. </span>
  48. </div>
  49. </td>
  50. </tr>
  51. </tbody>
  52. </table>
  53. </div>
  54. <!-- end header part -->
  55. <!-- Generated by Doxygen 1.9.8 -->
  56. <script type="text/javascript">
  57. /* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
  58. var searchBox = new SearchBox("searchBox", "search/",'.html');
  59. /* @license-end */
  60. </script>
  61. </div><!-- top -->
  62. <div id="side-nav" class="ui-resizable side-nav-resizable">
  63. <div id="nav-tree">
  64. <div id="nav-tree-contents">
  65. <div id="nav-sync" class="sync"></div>
  66. </div>
  67. </div>
  68. <div id="splitbar" style="-moz-user-select:none;"
  69. class="ui-resizable-handle">
  70. </div>
  71. </div>
  72. <script type="text/javascript">
  73. /* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
  74. $(document).ready(function(){initNavTree('Font.html',''); initResizable(); });
  75. /* @license-end */
  76. </script>
  77. <div id="doc-content">
  78. <!-- window showing the filter options -->
  79. <div id="MSearchSelectWindow"
  80. onmouseover="return searchBox.OnSearchSelectShow()"
  81. onmouseout="return searchBox.OnSearchSelectHide()"
  82. onkeydown="return searchBox.OnSearchSelectKey(event)">
  83. </div>
  84. <!-- iframe showing the search results (closed by default) -->
  85. <div id="MSearchResultsWindow">
  86. <div id="MSearchResults">
  87. <div class="SRPage">
  88. <div id="SRIndex">
  89. <div id="SRResults"></div>
  90. <div class="SRStatus" id="Loading">Loading...</div>
  91. <div class="SRStatus" id="Searching">Searching...</div>
  92. <div class="SRStatus" id="NoMatches">No Matches</div>
  93. </div>
  94. </div>
  95. </div>
  96. </div>
  97. <div><div class="header">
  98. <div class="headertitle"><div class="title">Font</div></div>
  99. </div><!--header-->
  100. <div class="contents">
  101. <div class="textblock"><p>Font handling in this library was designed to be quite customizable and lets you decide what you want to use and what you want to provide.</p>
  102. <p>There are three different ways to use the font atlas. The first two will use your font handling scheme and only requires essential data to run nuklear. The next slightly more advanced features is font handling with vertex buffer output. Finally the most complex API wise is using nuklear's font baking API.</p>
  103. <h1><a class="anchor" id="autotoc_md102"></a>
  104. Using your own implementation without vertex buffer output</h1>
  105. <p>So first up the easiest way to do font handling is by just providing a <code><a class="el" href="structnk__user__font.html">nk_user_font</a></code> struct which only requires the height in pixel of the used font and a callback to calculate the width of a string. This way of handling fonts is best fitted for using the normal draw shape command API where you do all the text drawing yourself and the library does not require any kind of deeper knowledge about which font handling mechanism you use. IMPORTANT: the <code><a class="el" href="structnk__user__font.html">nk_user_font</a></code> pointer provided to nuklear has to persist over the complete life time! I know this sucks but it is currently the only way to switch between fonts.</p>
  106. <div class="fragment"><div class="line"><span class="keywordtype">float</span> your_text_width_calculation(<a class="code hl_union" href="unionnk__handle.html">nk_handle</a> handle, <span class="keywordtype">float</span> height, <span class="keyword">const</span> <span class="keywordtype">char</span> *text, <span class="keywordtype">int</span> len)</div>
  107. <div class="line">{</div>
  108. <div class="line"> your_font_type *type = handle.ptr;</div>
  109. <div class="line"> <span class="keywordtype">float</span> text_width = ...;</div>
  110. <div class="line"> <span class="keywordflow">return</span> text_width;</div>
  111. <div class="line">}</div>
  112. <div class="line"> </div>
  113. <div class="line"><span class="keyword">struct </span><a class="code hl_struct" href="structnk__user__font.html">nk_user_font</a> font;</div>
  114. <div class="line">font.userdata.ptr = &amp;your_font_class_or_struct;</div>
  115. <div class="line">font.height = your_font_height;</div>
  116. <div class="line">font.width = your_text_width_calculation;</div>
  117. <div class="line"> </div>
  118. <div class="line"><span class="keyword">struct </span><a class="code hl_struct" href="structnk__context.html">nk_context</a> ctx;</div>
  119. <div class="line">nk_init_default(&amp;ctx, &amp;font);</div>
  120. <div class="ttc" id="astructnk__context_html"><div class="ttname"><a href="structnk__context.html">nk_context</a></div><div class="ttdef"><b>Definition</b> <a href="nuklear_8h_source.html#l05705">nuklear.h:5705</a></div></div>
  121. <div class="ttc" id="astructnk__user__font_html"><div class="ttname"><a href="structnk__user__font.html">nk_user_font</a></div><div class="ttdef"><b>Definition</b> <a href="nuklear_8h_source.html#l04006">nuklear.h:4006</a></div></div>
  122. <div class="ttc" id="aunionnk__handle_html"><div class="ttname"><a href="unionnk__handle.html">nk_handle</a></div><div class="ttdef"><b>Definition</b> <a href="nuklear_8h_source.html#l00268">nuklear.h:268</a></div></div>
  123. </div><!-- fragment --> <h1><a class="anchor" id="autotoc_md103"></a>
  124. Using your own implementation with vertex buffer output</h1>
  125. <p>While the first approach works fine if you don't want to use the optional vertex buffer output it is not enough if you do. To get font handling working for these cases you have to provide two additional parameters inside the <code><a class="el" href="structnk__user__font.html">nk_user_font</a></code>. First a texture atlas handle used to draw text as subimages of a bigger font atlas texture and a callback to query a character's glyph information (offset, size, ...). So it is still possible to provide your own font and use the vertex buffer output.</p>
  126. <div class="fragment"><div class="line"><span class="keywordtype">float</span> your_text_width_calculation(<a class="code hl_union" href="unionnk__handle.html">nk_handle</a> handle, <span class="keywordtype">float</span> height, <span class="keyword">const</span> <span class="keywordtype">char</span> *text, <span class="keywordtype">int</span> len)</div>
  127. <div class="line">{</div>
  128. <div class="line"> your_font_type *type = handle.ptr;</div>
  129. <div class="line"> <span class="keywordtype">float</span> text_width = ...;</div>
  130. <div class="line"> <span class="keywordflow">return</span> text_width;</div>
  131. <div class="line">}</div>
  132. <div class="line"><span class="keywordtype">void</span> query_your_font_glyph(<a class="code hl_union" href="unionnk__handle.html">nk_handle</a> handle, <span class="keywordtype">float</span> font_height, <span class="keyword">struct</span> nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)</div>
  133. <div class="line">{</div>
  134. <div class="line"> your_font_type *type = handle.ptr;</div>
  135. <div class="line"> glyph.width = ...;</div>
  136. <div class="line"> glyph.height = ...;</div>
  137. <div class="line"> glyph.xadvance = ...;</div>
  138. <div class="line"> glyph.uv[0].x = ...;</div>
  139. <div class="line"> glyph.uv[0].y = ...;</div>
  140. <div class="line"> glyph.uv[1].x = ...;</div>
  141. <div class="line"> glyph.uv[1].y = ...;</div>
  142. <div class="line"> glyph.offset.x = ...;</div>
  143. <div class="line"> glyph.offset.y = ...;</div>
  144. <div class="line">}</div>
  145. <div class="line"> </div>
  146. <div class="line"><span class="keyword">struct </span><a class="code hl_struct" href="structnk__user__font.html">nk_user_font</a> font;</div>
  147. <div class="line">font.userdata.ptr = &amp;your_font_class_or_struct;</div>
  148. <div class="line">font.height = your_font_height;</div>
  149. <div class="line">font.width = your_text_width_calculation;</div>
  150. <div class="line">font.query = query_your_font_glyph;</div>
  151. <div class="line">font.texture.id = your_font_texture;</div>
  152. <div class="line"> </div>
  153. <div class="line"><span class="keyword">struct </span><a class="code hl_struct" href="structnk__context.html">nk_context</a> ctx;</div>
  154. <div class="line">nk_init_default(&amp;ctx, &amp;font);</div>
  155. </div><!-- fragment --><h1><a class="anchor" id="autotoc_md104"></a>
  156. Nuklear font baker</h1>
  157. <p>The final approach if you do not have a font handling functionality or don't want to use it in this library is by using the optional font baker. The font baker APIs can be used to create a font plus font atlas texture and can be used with or without the vertex buffer output.</p>
  158. <p>It still uses the <code><a class="el" href="structnk__user__font.html">nk_user_font</a></code> struct and the two different approaches previously stated still work. The font baker is not located inside <code><a class="el" href="structnk__context.html">nk_context</a></code> like all other systems since it can be understood as more of an extension to nuklear and does not really depend on any <code><a class="el" href="structnk__context.html">nk_context</a></code> state.</p>
  159. <p>Font baker need to be initialized first by one of the nk_font_atlas_init_xxx functions. If you don't care about memory just call the default version <code>nk_font_atlas_init_default</code> which will allocate all memory from the standard library. If you want to control memory allocation but you don't care if the allocated memory is temporary and therefore can be freed directly after the baking process is over or permanent you can call <code>nk_font_atlas_init</code>.</p>
  160. <p>After successfully initializing the font baker you can add Truetype(.ttf) fonts from different sources like memory or from file by calling one of the <code>nk_font_atlas_add_xxx</code>. functions. Adding font will permanently store each font, font config and ttf memory block(!) inside the font atlas and allows to reuse the font atlas. If you don't want to reuse the font baker by for example adding additional fonts you can call <code>nk_font_atlas_cleanup</code> after the baking process is over (after calling nk_font_atlas_end).</p>
  161. <p>As soon as you added all fonts you wanted you can now start the baking process for every selected glyph to image by calling <code>nk_font_atlas_bake</code>. The baking process returns image memory, width and height which can be used to either create your own image object or upload it to any graphics library. No matter which case you finally have to call <code>nk_font_atlas_end</code> which will free all temporary memory including the font atlas image so make sure you created our texture beforehand. <code>nk_font_atlas_end</code> requires a handle to your font texture or object and optionally fills a <code>struct <a class="el" href="structnk__draw__null__texture.html">nk_draw_null_texture</a></code> which can be used for the optional vertex output. If you don't want it just set the argument to <code>NULL</code>.</p>
  162. <p>At this point you are done and if you don't want to reuse the font atlas you can call <code>nk_font_atlas_cleanup</code> to free all truetype blobs and configuration memory. Finally if you don't use the font atlas and any of it's fonts anymore you need to call <code>nk_font_atlas_clear</code> to free all memory still being used.</p>
  163. <div class="fragment"><div class="line"><span class="keyword">struct </span>nk_font_atlas atlas;</div>
  164. <div class="line">nk_font_atlas_init_default(&amp;atlas);</div>
  165. <div class="line">nk_font_atlas_begin(&amp;atlas);</div>
  166. <div class="line">nk_font *font = nk_font_atlas_add_from_file(&amp;atlas, <span class="stringliteral">&quot;Path/To/Your/TTF_Font.ttf&quot;</span>, 13, 0);</div>
  167. <div class="line">nk_font *font2 = nk_font_atlas_add_from_file(&amp;atlas, <span class="stringliteral">&quot;Path/To/Your/TTF_Font2.ttf&quot;</span>, 16, 0);</div>
  168. <div class="line"><span class="keyword">const</span> <span class="keywordtype">void</span>* img = nk_font_atlas_bake(&amp;atlas, &amp;img_width, &amp;img_height, NK_FONT_ATLAS_RGBA32);</div>
  169. <div class="line">nk_font_atlas_end(&amp;atlas, nk_handle_id(texture), 0);</div>
  170. <div class="line"> </div>
  171. <div class="line"><span class="keyword">struct </span><a class="code hl_struct" href="structnk__context.html">nk_context</a> ctx;</div>
  172. <div class="line">nk_init_default(&amp;ctx, &amp;font-&gt;handle);</div>
  173. <div class="line"><span class="keywordflow">while</span> (1) {</div>
  174. <div class="line"> </div>
  175. <div class="line">}</div>
  176. <div class="line">nk_font_atlas_clear(&amp;atlas);</div>
  177. </div><!-- fragment --><p> The font baker API is probably the most complex API inside this library and I would suggest reading some of my examples <code>example/</code> to get a grip on how to use the font atlas. There are a number of details I left out. For example how to merge fonts, configure a font with <code>nk_font_config</code> to use other languages, use another texture coordinate format and a lot more:</p>
  178. <div class="fragment"><div class="line"><span class="keyword">struct </span>nk_font_config cfg = nk_font_config(font_pixel_height);</div>
  179. <div class="line">cfg.merge_mode = nk_false or nk_true;</div>
  180. <div class="line">cfg.range = nk_font_korean_glyph_ranges();</div>
  181. <div class="line">cfg.coord_type = NK_COORD_PIXEL;</div>
  182. <div class="line">nk_font *font = nk_font_atlas_add_from_file(&amp;atlas, <span class="stringliteral">&quot;Path/To/Your/TTF_Font.ttf&quot;</span>, 13, &amp;cfg);</div>
  183. </div><!-- fragment --> </div></div><!-- contents -->
  184. </div><!-- PageDoc -->
  185. </div><!-- doc-content -->
  186. <!-- start footer part -->
  187. <div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  188. <ul>
  189. <li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.8 </li>
  190. </ul>
  191. </div>
  192. </body>
  193. </html>