title: FreeType Glyph Metrics Caching of multiple Font sizes
body: <h2>Situation:</h2>
  
  <p>I have a project that renders product information onto a given template (custom XML format), then renders and converts it in a custom binary LCD format (steps simplified)</p>
  
  <p>Our customers now want <strong>auto-fitting text</strong> container. (customer gives a <strong>box of specific size</strong> and all kinds of <strong>strings have to get auto-resized</strong> to <strong>fit into that container</strong></p>
  
  <p>For that I have to calculate the width of the string (freetype: each char/glyph) for multiple font-sizes (e.g. 100pt doesnt fit, 99pt doesnt fit, 98pt doesnt..., ..., 65pt fits!)</p>
  
  <h2>Problem:</h2>
  
  <p>The Problem is that freetype takes a lot of time (~20-30 ms) for each auto-fit element and I have only ~100ms for my whole application to use. (so when customer adds 5 more autofit elements it's already guaranteed to exceed ~100 ms)</p>
  
  <h2>Attempts:</h2>
  
  <p>A selfmade font-cache-generator which takes a font-file and calculates the widths of each unicode-character for font-sizes from 1pt to 100pt. Then it generates C source code out of the data like this:</p>
  
  <pre><code>//
  #define  COUNT_SIZES  100    // Font-Size 1-100
  #define  COUNT_CHARS  65536  // Full Unicode Table
  int char_sizes[COUNT_SIZES][COUNT_CHARS] = 
  {
     {1,1,2,2,3,1,1,2,2,3,1,2,2,1,2,2,3,1,2,.......// 65536
     {2,2,3,3,4,2,1,3,3,4,2,3,3,2,3,3,4,2,3,.......// 65536
     {2,3,4,3,5,2,2,4,4,5,2,4,4,2,4,3,5,3,3,.......// 65536
     // ...
     // 100 font sizes
  };
  </code></pre>
  
  <p>That compiled in a dynamic lib (.so) is 25 MB in size and takes ~50ms to "dlload" and ~10ms to "dlsym" (WAAAAAAY too much!)</p>
  
  <p>The same way but only ASCII table (so only 128 of 65536) compiles into a 58 KB .so file and takes ~500µs for "dlload" and ~100µs for "dlsym" (very nice!)</p>
  
  <p>My next attempt would be to integrate the font-cache-generator into my project and cache only the glyphs I need for the specific customer (customer in europe needs ~500 glyphs, one in asia (e.g. traditional chinese) needs ~2500 (only examples, not exactly sure, maybe even more needed)</p>
  
  <p>But before I take on that hard-work journey (:() I wanted to ask you if you know a better way of doing it? A library/project that does just that?</p>
  
  <p>I cannot believe that it's not possible, how should a browser show lorem ipsum without loading seconds otherwise? :D</p>
  
  <p>Any idea on how to solve this performance issue?</p>
  
  <p>Any informative link on data caching with extremly fast access to cache (somewhat &lt;1ms)?</p>
  
  <h2>System Info:</h2>
  
  <ul>
  <li><strong>Unix</strong> (Ubuntu 16.04) <strong>64bit</strong></li>
  <li><strong>x86</strong> <em>AND</em> <strong>arm</strong> architectures exist!</li>
  </ul>