[Development] Replacement for QFont::ForceIntegerMetrics in Qt 6?

Eskil Abrahamsen Blomfeldt Eskil.Abrahamsen-Blomfeldt at qt.io
Mon Nov 6 12:04:34 CET 2023


Hi!

ForceIntegerMetrics was originally added to get CoreText to look at little bit better with WebKit, since WebKit did not support subpixel positioning at the time and CoreText did not support font hinting. I removed it in Qt 6 because it's honestly not a typographically sensible thing to do, and the original use case had become irrelevant 🙂

For your case, where you want to make sure your font aligns to the pixel grid, you could try enabling hinting  on it with font.setHintingPreference(QFont::PreferFullHinting). Font hinting will slightly alter the glyphs so that they actually align to the pixel grid instead of introducing kerning errors (which is what ForceIntegerMetrics did). I'm not sure if this is what you mean by "disabling design metrics"? If so, what exactly was the issue with it? I principle, I think this is the most correct solution to the problem, so maybe there's some way of getting it to work.

If that does not work, then another option is to manually get the QGlyphRuns from the QTextLayout and align the glyphs yourself before drawing them. That should get you the same layout as with ForceIntegerMetrics, both good and bad, without much extra work.

(Of course, ideally the rendering code should not assume integer advances for the characters, since this is not really a reasonable expectation for unhinted, scalable fonts, but I can see how it's not tempting to rewrite it just for this port.)

Eskil Abrahamsen Blomfeldt
Senior Manager, Graphics

The Qt Company
Sandakerveien 116
0484 Oslo, Norway
eskil.abrahamsen-blomfeldt at qt.io
http://qt.io
________________________________
From: Development <development-bounces at qt-project.org> on behalf of Kai Uwe Broulik <kde at privat.broulik.de>
Sent: Saturday, November 4, 2023 10:50 AM
To: development at qt-project.org <development at qt-project.org>
Subject: [Development] Replacement for QFont::ForceIntegerMetrics in Qt 6?

Hi everyone,

in Qt 5.15 the QFont::ForceIntegerMetrics was deprecated and
subsequently removed in Qt 6.

The rendering engine in Konsole, KDE’s terminal emulator, relies on the
fact that every glyph in the monospace font is rendered at the same
width, since sections of different style (e.g. search highlight, text
selection, various escape sequence formattings, etc) are painted in
different pases, basically positioned at charWidth * letterXPos.

With Qt 6, without this flag, the individual letters might be placed at
fractional positions, so a long text run can end up short of what the
renderer expects, leading to gaps in the rendering, see the attached
screenshot where I selected some text which now makes a gap.

I have read into QFontEngine a bit and found that the general
replacement is disabling use of “design metrics”. I changed the
rendering to use QPainter::paint with a QTextOption disabling design
metrics [1], which fixed the issue on my machine but apparently causes
issues with other fonts again.

Many of us have tried to find a solution for this but haven’t, really,
and it is a major showstopper for the Qt 6 adoption here. Any idea how
to address this properly, short of rewriting the renderer that I assume
has been like this for decades or perhaps some insights on what prompted
the removal?

Cheers
Kai Uwe

[1] https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Finvent.kde.org%2Futilities%2Fkonsole%2F-%2Fmerge_requests%2F911&data=05%7C01%7Ceskil.abrahamsen-blomfeldt%40qt.io%7C402caffbb31d49df5cef08dbdd1bc085%7C20d0b167794d448a9d01aaeccc1124ac%7C0%7C0%7C638346883796105023%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=c5whMadCyHEavgFDTdDha8dTpO%2FRUBAnK%2Bp453ynvmk%3D&reserved=0<https://invent.kde.org/utilities/konsole/-/merge_requests/911>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20231106/085ba629/attachment.htm>


More information about the Development mailing list