Subject: Re: bug#33885: 27.0.50; PUA character makes emacs crash

From: Werner LEMBERG <[email-masked]>
Date: Fri, 28 Dec 2018 18:03:11 +0100 (CET)

>> (2) Improve the heuristics to get sensible global ascent and
>>     descent values of a font.  In particular, use the FreeType
>>     library to access a TrueType's or OpenType's `OS/2' table and
>>     compare its vertical metrics values with the `hhea' data to
>>     eliminate bugs in the font – sign problems with `descent' do
>>     happen especially in older fonts that were either designed for
>>     Apple or for Windows, and the fields for the `other' OS were
>>     filled out arbitrarily, more or less.
> 
> I'd appreciate more details on how this could be done.  I'm no
> expert on fonts nor on FreeType, and last I checked we have no one
> else.

A possible workflow.

(0) Check that we have an SFNT font (which means either TrueType or
    OpenType):

      if (FT_IS_SFNT(ft_face))
        ...

(1) Use

      TT_OS2* os2_table;

      os2_table = (TT_OS2)FT_Get_Sfnt_Table(ft_face, FT_SFNT_OS2);

    to extract the `OS/2' table data from an existing `FT_Face' object
    for a given font.  We are interested in the fields

      fsSelection

      sTypoAscender
      sTypoDescender
      sTypoLineGap

      usWinAscent
      usWinDescent

    of FreeType's `TT_OS2' structure and in

      ascent
      descent

    of xft's `XftFont' structure; the latter is based on the
    `ascender' and `descender' fields of an OpenType font's `hhea'
    table (with `descent' having the opposite sign of of `descender').

    [Since `XftFont' doesn't provide `hhea's `lineGap' value it
     probably makes sense to use `FT_Get_Sfnt_Table' also to access
     this value.]

(2) Check that

      sTypoAscender - sTypoDescender > 0
      ascent + descent > 0
      usWinAscent + usWinDescent > 0

    and filter out the value pairs that don't fit.  For example, I've
    seen fonts where `sTypo*' values were all zero.

(3) Check that

      -sign(descent) == sign(sTypoDescender) == -sign(usWinDescent)

    If this holds, use `usWin*' or `sTypo*' values, depending on bit 7
    of `fsSelection'.  Maybe use `ascent/descent' if on a Mac.

    If this doesn't hold, try to sanitize the not-fitting value.

     Werner