[Interest] Modifying the font metrics ("leading" and "tracking") everywhere

william.crocker at analog.com william.crocker at analog.com
Tue Jul 16 17:37:50 CEST 2013


On 07/16/2013 11:08 AM, Atlant Schmidt wrote:
> Bill:
>
>> Years ago I noticed (Linux/Fontconfig) that the line spacing of fonts
>> as rendered on the screen by Qt were obviously wrong, they were too large.
>> (The letter spacing looked fine.) Either the fonts were lying about
>> their ascent/descent or Qt was interpreting them incorrectly.
>
>    In my case, I think Qt is acting correctly on the information
>    provided by the font, but for our particular application, the
>    leading called-for by the font is far too generous. It's a
>    hybrid Asian/Roman font so I suppose that's why the interline
>    spacing defaults to such a large value; this keeps the descenders
>    on (e.g.) the Roman J/j from colliding with the tall CJK characters,
>    but we could control this in other, more-attractive ways.
>
>    My principal complaint is that Qt doesn't seem to have any
>    effective tools to deal with this. There ought to be a simple
>    C++ interface to set leading and tracking, just as one would
>    find in any desktop publishing app.
>
>    (I'm still investigating whether Qt CSS can affect this but
>    I don't see how yet.)
>
>    I may very well end up going your way; would you care to share
>    your patch(es) to the font engine?
>

Here are the diffs of my version with the original 4.7.1

Note that this hack assumes access to 'x', 'y' and 'h' glyphs.

I don't remember changing the few lines at the bottom,
outside of the WHC_METRIC_HACK wrapper or why.          :-)

Use at your own risk.

Bill

> #define WHC_METRIC_HACK
> #ifdef  WHC_METRIC_HACK
>
> #   define HACK1 1                      /* Increase height */
> #   define HACK2 2                      /* Shift position. */
>
>     // Bitmap based character height.
>     //
>     int
>     MyCharHeight( FT_Face face, FT_ULong char_code ) {
>         int height = 0;
>         int load_error = FT_Load_Char(face,char_code,FT_LOAD_RENDER);
>         if( load_error == 0 )
>             height = face->glyph->bitmap.rows;
>         return height;
>     }
>
>     void
>     MyComputeMetrics( FT_Face face, int *ascent, int *dscent ) {
>         // cerr << "MyComputeMetrics: " << endl;
>
>         FT_Size_Metrics metrics = face->size->metrics;
>         *ascent = metrics.ascender;
>         *dscent = metrics.descender;
>         // cerr << "    Face ascent: " << *ascent << endl;
>         // cerr << "    Face dscent: " << *dscent << endl;
>
>         int h_height = MyCharHeight(face,'h');
>         // cerr << "    h_height: " << h_height << ' ' << (h_height+HACK1+HACK2)*64 << endl;
>         if( h_height > 0 && (h_height+HACK1+HACK2)*64 < *ascent ) {
>             // cerr << "        Accept" << endl;
>             *ascent = (h_height+HACK1+HACK2)*64;                // Looks like a better number.
>         }
>
>         int y_height = MyCharHeight(face,'y');
>         // cerr << "    y_height: " << y_height << endl;
>         int x_height = MyCharHeight(face,'x');
>         // cerr << "    x_height: " << x_height << endl;
>         int y_desc = y_height - x_height;
>         // cerr << "    y_desc: " << y_desc << ' ' << (-y_desc-HACK1+HACK2)*64 << endl;
>         if( y_desc > 0 && (-y_desc-HACK1+HACK2)*64 > *dscent ) {
>             // cerr << "        Accept" << endl;
>             *dscent = (-y_desc-HACK1+HACK2)*64;         // Looks like a better number.
>         }
>
>         // cerr << "    Final ascent: " << *ascent << endl;
>         // cerr << "    Final dscent: " << *dscent << endl;
>     }
> #endif
>
751a802,817
> #   ifdef WHC_METRIC_HACK
>         // WHC: Fixes font-config bug where the general ascent/descent numbers for some faces
>         //      are grossly over estimated to the point where the text looks double-spaced.
>         // cerr << __FILE__ << ' ' << __LINE__ << " orig ascender  = " << metrics.ascender <<  endl;
>         // cerr << __FILE__ << ' ' << __LINE__ << " orig descender = " << metrics.descender << endl;
>         // cerr << __FILE__ << ' ' << __LINE__ << "    orig height = " << metrics.height << endl;
>         int ascent, dscent;
>         MyComputeMetrics( face, &ascent, &dscent );
>         metrics.ascender  = ascent;
>         metrics.descender = dscent;
>         metrics.height = qRound( 1.2 * (metrics.ascender - metrics.descender) );
>         // cerr << __FILE__ << ' ' << __LINE__ << " my ascender  = " << metrics.ascender << endl;
>         // cerr << __FILE__ << ' ' << __LINE__ << " my descender = " << metrics.descender << endl;
>         // cerr << __FILE__ << ' ' << __LINE__ << " my height = " << metrics.height << endl;
> #   endif
>
804c870
<     if (embolden) Q_FT_GLYPHSLOT_EMBOLDEN(slot);
---
>     if (embolden) { Q_FT_GLYPHSLOT_EMBOLDEN(slot); }
950c1016
<     if (embolden) Q_FT_GLYPHSLOT_EMBOLDEN(slot);
---
>     if (embolden) { Q_FT_GLYPHSLOT_EMBOLDEN(slot); }
1241c1307,1308
<     return QFixed::fromFixed(-metrics.descender - 64);
---
>     // return QFixed::fromFixed(-metrics.descender - 64);
>     return QFixed::fromFixed(-metrics.descender);
juno:qt:














>                                  Atlant
>
> -----Original Message-----
> From: interest-bounces+aschmidt=dekaresearch.com at qt-project.org [mailto:interest-bounces+aschmidt=dekaresearch.com at qt-project.org] On Behalf Of william.crocker at analog.com
> Sent: Tuesday, July 16, 2013 8:18 AM
> To: interest at qt-project.org
> Subject: Re: [Interest] Modifying the font metrics ("leading" and "tracking") everywhere
>
> ...
>
> Years ago I noticed (Linux/Fontconfig) that the line spacing of fonts
> as rendered on the screen by Qt were obviously wrong, they were too large.
> (The letter spacing looked fine.) Either the fonts were lying about
> their ascent/descent or Qt was interpreting them incorrectly.
>
> The eventual solution was to alter the Qt source code which reads the fonts to
> actually measure the glyph height of indicative chars like 'x', 'y' and 'h' to
> determine the proper ascent/descent numbers to be used.
> ( This works for all fonts except one where the lower case 'x' actually
>     intrudes below the base-line. :-) )
>
> My screens have never looked better.
>
> I am using Qt 4.7.1   and probably will be for life.
>
> Bill
>
>
> This e-mail and the information, including any attachments, it contains are intended to be a confidential communication only to the person or entity to whom it is addressed and may contain information that is privileged. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the sender and destroy the original message.
>
> Thank you.
>
> Please consider the environment before printing this email.
>
>




More information about the Interest mailing list