discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Re: [OpenSCAD] More on text alignment

CA
Carsten Arnholm
Wed, Dec 30, 2020 5:59 AM

On 30.12.2020 00:17, Jordan Brown wrote:

On 12/29/2020 1:29 AM, arnholm@arnholm.org wrote:

Btw., why is there a need for a "font cache" in OpenSCAD?

I believe (somewhat through black-box observation) that there are two.

The Fontconfig library keeps a list of all of the available fonts, for
each use when doing its fuzzy font matching.  (That's the "updating
cache" popup you see sometimes.)

So OpenSCAD uses fontconfig "fuzzy font matching" to choose the actual
font file, based on the user parameters in the text() command? That
seems kind of unnecessary, especially since updating the cache takes a
long time, caches are supposed to make things faster, not slower.

Regardless of the time spent, I can't see that "fuzzy font matching"
actually doing anything different from a trivial lookup (see below).

In comparison, AngelCAD's approach is to scan the system font
directories once per .scad file containing text() and record the
correspondence between font name/style contained with the actual font
file name. This is fast. Later, when processing text(), the lookup to
the relevant font file to obtain glyph geometries is direct and immediate.

It looks like OpenSCAD keeps an in-memory cache of fonts that have been
used, presumably to avoid rereading the font files on each "text" call.

In memory cache is not the same as file cache. Keeping things in memory
isn't always a good thing either.

Are you saying the file cache contains copies/interpretation of the font
geometries (glyphs)? If so, that can't be any faster than reading the
font files directly. I am guessing the file cache does not contain
copies of the font geometries though.

Reading TrueType font files is very fast using FreeType, and is in any
case orders of magnitude faster than the booleans in CGAL or Carve that
follow, so there should not be any reason to try to optimize on font
file reading, given the side effects that slow things down.

So I still wonder why the font cache exists in OpenSCAD. But ok, it exists.

Carsten Arnholm

On 30.12.2020 00:17, Jordan Brown wrote: > On 12/29/2020 1:29 AM, arnholm@arnholm.org wrote: >> Btw., why is there a need for a "font cache" in OpenSCAD? > > I believe (somewhat through black-box observation) that there are two. > > The Fontconfig library keeps a list of all of the available fonts, for > each use when doing its fuzzy font matching.  (That's the "updating > cache" popup you see sometimes.) So OpenSCAD uses fontconfig "fuzzy font matching" to choose the actual font file, based on the user parameters in the text() command? That seems kind of unnecessary, especially since updating the cache takes a long time, caches are supposed to make things faster, not slower. Regardless of the time spent, I can't see that "fuzzy font matching" actually doing anything different from a trivial lookup (see below). In comparison, AngelCAD's approach is to scan the system font directories once per .scad file containing text() and record the correspondence between font name/style contained with the actual font file name. This is fast. Later, when processing text(), the lookup to the relevant font file to obtain glyph geometries is direct and immediate. > It looks like OpenSCAD keeps an in-memory cache of fonts that have been > used, presumably to avoid rereading the font files on each "text" call. In memory cache is not the same as file cache. Keeping things in memory isn't always a good thing either. Are you saying the file cache contains copies/interpretation of the font geometries (glyphs)? If so, that can't be any faster than reading the font files directly. I am guessing the file cache does not contain copies of the font geometries though. Reading TrueType font files is very fast using FreeType, and is in any case orders of magnitude faster than the booleans in CGAL or Carve that follow, so there should not be any reason to try to optimize on font file reading, given the side effects that slow things down. So I still wonder why the font cache exists in OpenSCAD. But ok, it exists. Carsten Arnholm
JB
Jordan Brown
Thu, Dec 31, 2020 9:01 PM

On 12/29/2020 9:59 PM, Carsten Arnholm wrote:

So OpenSCAD uses fontconfig "fuzzy font matching" to choose the actual
font file, based on the user parameters in the text() command?

Something like that.
https://www.freedesktop.org/wiki/Software/fontconfig/ if you want more
information.

I'm not looking at that area and so I don't know much about it.

Fuzzy matching lets you say things like "give me a bold italic serif
font", and get a reasonable answer without having to know exactly what
fonts are installed on the system.  In OpenSCAD, say

text("Hello World", font="serif:italic:bold");

It looks like OpenSCAD keeps an in-memory cache of fonts that have
been used, presumably to avoid rereading the font files on each
"text" call.

In memory cache is not the same as file cache. Keeping things in
memory isn't always a good thing either.

Are you saying the file cache contains copies/interpretation of the
font geometries (glyphs)? If so, that can't be any faster than reading
the font files directly. I am guessing the file cache does not contain
copies of the font geometries though.

Perhaps I didn't make it clear enough that I believe that there are
two totally independent caches.  Fontconfig's cache is, I believe, a
cache of font metadata to make its fuzzy matching fast.  OpenSCAD also
has its own in-memory cache (in src/FontCache.cc) that caches the
definitions of fonts that you use, at some level.  (I can't tell whether
it caches all of the glyph data, but my guess is "yes".)

On 12/29/2020 9:59 PM, Carsten Arnholm wrote: > So OpenSCAD uses fontconfig "fuzzy font matching" to choose the actual > font file, based on the user parameters in the text() command? Something like that. https://www.freedesktop.org/wiki/Software/fontconfig/ if you want more information. I'm not looking at that area and so I don't know much about it. Fuzzy matching lets you say things like "give me a bold italic serif font", and get a reasonable answer without having to know exactly what fonts are installed on the system.  In OpenSCAD, say text("Hello World", font="serif:italic:bold"); >> It looks like OpenSCAD keeps an in-memory cache of fonts that have >> been used, presumably to avoid rereading the font files on each >> "text" call. > > In memory cache is not the same as file cache. Keeping things in > memory isn't always a good thing either. > > Are you saying the file cache contains copies/interpretation of the > font geometries (glyphs)? If so, that can't be any faster than reading > the font files directly. I am guessing the file cache does not contain > copies of the font geometries though. Perhaps I didn't make it clear enough that I believe that there are *two* totally independent caches.  Fontconfig's cache is, I believe, a cache of font metadata to make its fuzzy matching fast.  OpenSCAD also has its own in-memory cache (in src/FontCache.cc) that caches the definitions of fonts that you use, at some level.  (I can't tell whether it caches all of the glyph data, but my guess is "yes".)