[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [ft] How to get nominal width of monospaced fonts
From: |
Werner LEMBERG |
Subject: |
Re: [ft] How to get nominal width of monospaced fonts |
Date: |
Wed, 22 May 2019 07:39:21 +0200 (CEST) |
> I'm looking for a metric that corresponds to what I get after an
> FT_Load_Glyph(), and looking at face->glyph->advance.[xy], for
> monospaced Truetype fonts like Liberation Mono.
If you call `FT_Load_Glyph' you always get a proper advance value.
> I'm trying to answer the following question: here's a monospaced
> font where, hopefully, all glyphs have the same advance.x for
> rendering. Can I obtain this value without actually rendering each
> glyph and seeing what I get.
Just checking the terminology: There are three steps that are
necessary to transfer the font file data to the screen.
(1) Reading global font data → FT_New_Face
(2) Loading and (optionally) hinting a single glyph → FT_Load_Glyph
(without using FT_LOAD_RENDER)
(3) Converting the vector outline to a bitmap (a.k.a. `rendering')
→ FT_Render_Glyph
Steps (1) and (2) are necessary to get the information you need; step
(3) is optional and doesn't change any metrics data. So the answer to
your question is: Yes, you don't need to render a glyph to get the
advance width. However, it must be loaded, and optionally hinted.
Note, by the way, that real monospaced fonts are quite rare since many
of them contain non-spacing glyphs with a zero advance width.
Additionally, you have to rely on the font creator that the font
behaves correctly. Having an advance width that is smaller than the
actual bounding box of the rendered bitmap is madness, making the font
essentially unusable...
>> > I thought, at first, to look at face->size->metrics.max_advance,
>> > but if I do that I find that it's always several pixels more than
>> > what I eventually get after FT_LOAD_RENDERing each glyph, and
>> > computing face->glyph->advance.x >> 6.
I forgot to mention that it is up to the font creator to set the
`max_advance' value, which can be arbitrarily large... Additionally,
hinting can adjust a glyph and its metrics almost arbitrarily, too.
> I was seeing differences that were more than two pixels. I would
> think that hinting-related differences would not translate to more
> than a pixel's worth of a difference.
Please prepare exact numbers. Which font, which glyph, which ppem,
etc., etc. You might compile FreeType statically with debugging
output (doing `make devel; make' without calling the `configure'
script), then setting the `FT2_DEBUG' variable as described in
`docs/DEBUG'. This should give you plenty of information that
probably helps us locate and explain the issue.
> That pretty much answers my question. In the interim, I am using a
> heuristic approach of loading a sample set of glyphs from each face,
> and averaging out their advance.
For a monospaced font?
Werner