Christoph
Christoph

Reputation: 1080

Object lookup with optional help of a lookup table which must be optimized away if unused

I want to speed up glyph lookup in my Cortex-M4 application for those fonts that are heavily used, by adding an optional lookup table. What I have now is - simplified and pseudo-ish - this:

in fontname-font.cpp:

static const uint8_t someFont = {
  headerbytes[knownHeaderSize],
  glyph[knownGlyphCount]
};

The size of each glyph depends on the glyph. Some are basically empty (space, for example), some are larger (like 'M'). The size of each glyph is stored in the glyph's header, so I can loop through the font and find out at which offset (someFont[offset]) a glyph with code code starts.

in fontname-map.cpp:

static const uint16_t someFont_lut[knownGlyphCount];

so I can use someFont[someFont_lut[code]] if the LUT is included.

How can I combine the font and an optional LUT in a class that provides a convenient interface? It seems to me that splitting the information into two files is already a bad idea (I can change that, though), but even if they were in the same file, how can I design a class that allows optional usage of a LUT, and allows the LUT to be optimized away if it is not used?

I'd like reduce the application interface to something like

Glyph Font::operator[](const char& c);

where Glyph can be just a pointer to the glyph stored in flash or a proxy object that can be used to retrieve glyph data from flash. Existence and usage of the LUT should be hidden behind this operator.

I'm using gcc, and suggestions may be gcc-specific. Which fonts should contain a LUT is known at compile-time.


Added: I'd like the solution to prevent me from mixing one font's glyphs with another font's LUT. I want to specify if the LUT is to used, not which that is. The font reading class must find it on its own.

Upvotes: 0

Views: 171

Answers (1)

Michael Karcher
Michael Karcher

Reputation: 4031

With your specification, you just need to write two font classes that share the same interface. One using a slow "operator[]" and a second one building a providing a fast "operator[]", given the LUT. Functions that use a font need to be templated on the font type.

If you are using just a very small subsets of fonts, you could even make a template class that uses the address of the characters and the lookup table as template parameters. In that case, font-using functions would be compiled separately for every font they are used with, but the addresses of the font data structure can be inlined into the function consuming the fonts.

As the template solution even allows you to mix "fixed" fonts as described in the previous section with "dynamic" fonts where the called function gets a pointer to a data structure (the "dynamic font" class) pointing to the character data or character data and optionally the LUT.

If your main question is about not linking the LUT if your code does not use it: Just compile all the fontname-xxx.cpp files into separate object files and put them in a static library. When you use that library, the compiler only picks the objects you refer to in your application.

Upvotes: 1

Related Questions