FreeType Glyph Metrics Caching of multiple Font sizes

1.9k views Asked by At

Situation:

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)

Our customers now want auto-fitting text container. (customer gives a box of specific size and all kinds of strings have to get auto-resized to fit into that container

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!)

Problem:

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)

Attempts:

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:

//
#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
};

That compiled in a dynamic lib (.so) is 25 MB in size and takes ~50ms to "dlload" and ~10ms to "dlsym" (WAAAAAAY too much!)

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!)

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)

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?

I cannot believe that it's not possible, how should a browser show lorem ipsum without loading seconds otherwise? :D

Any idea on how to solve this performance issue?

Any informative link on data caching with extremly fast access to cache (somewhat <1ms)?

System Info:

  • Unix (Ubuntu 16.04) 64bit
  • x86 AND arm architectures exist!
1

There are 1 answers

0
Iyashi On BEST ANSWER

I found one possible way using these libraries:

  • ICU (for unicode)
  • Freetype (for the Glyphs)
  • Harfbuzz (for layout)

Github Project: Harfbuzz-ICU-Freetype

Loose build instructions:

  • Search options in CMakeLists.txt option(WITH_XX "DESCRIPT." ON/OFF)
  • Enable CMake options with -D: cmake -DWITH_ZLIB=ON -DWITH_Harfbuzz=ON ..
  • mkdir build && cd build && cmake [option [option [...]]] ..
  • make -j $count_of_cpu_cores && sudo make install

Google for some Harfbuzz Layout tutorials / guides