Bypass Qt font metric calculations (#272)

- This originally showed up as fonts rendering differently on X11 than Wayland. (#230)
- Setting font size in points should be device and back end independent, however the same exact font face and size, on
  the same machine, sometimes results in different font metrics between the xcb and wayland Qt back ends.
- Setting font size in pixels, assuming a virtual DPI of 96 pixels/inch, results in consistent font metrics and rendering
  between these back ends. Furthermore, this virtual DPI works for either on-screen or hi-res printer QPainter contexts.
- This virtual DPI seems to work correctly with some limited testing with Windows and MacOS.
- Add rendering tests to build-tests CI script.
This commit is contained in:
Jaye Evins
2025-12-30 00:33:17 -05:00
committed by GitHub
parent 82b264a7a0
commit ce8bbad26c
14 changed files with 289 additions and 22 deletions
+25 -6
View File
@@ -24,11 +24,31 @@
#include "Size.h"
#include <QBrush>
#include <QDebug>
#include <QPen>
#include <QTextDocument>
#include <QTextBlock>
#include <QRegularExpression>
#include <QtDebug>
//
// Private
//
namespace
{
///
/// Calculate pixel size
///
/// Assume a virtual DPI of 96 pixels/inch for all QPainter contexts.
/// Ideally, we should use pointSizes for device independence, but as
/// Qt-6.4 on X11, Wayland, and MacOS this approach has better results.
///
int pixelSize( double pointSize )
{
const double virtual_dpi = 96;
return qMax( 1, qRound( pointSize * virtual_dpi/72.0 ) );
}
}
namespace glabels
@@ -467,7 +487,7 @@ namespace glabels
{
QFont font;
font.setFamily( mFontFamily );
font.setPointSizeF( mFontSize );
font.setPixelSize( pixelSize( mFontSize ) );
font.setWeight( mFontWeight );
font.setItalic( mFontItalicFlag );
font.setUnderline( mFontUnderlineFlag );
@@ -591,7 +611,7 @@ namespace glabels
{
QFont font;
font.setFamily( mFontFamily );
font.setPointSizeF( mFontSize );
font.setPixelSize( pixelSize( mFontSize ) );
font.setWeight( mFontWeight );
font.setItalic( mFontItalicFlag );
font.setUnderline( mFontUnderlineFlag );
@@ -712,7 +732,7 @@ namespace glabels
QFont font;
font.setFamily( mFontFamily );
font.setPointSizeF( mTextAutoShrink ? autoShrinkFontSize( record, variables ) : mFontSize );
font.setPixelSize( pixelSize( mTextAutoShrink ? autoShrinkFontSize( record, variables ) : mFontSize ) );
font.setWeight( mFontWeight );
font.setItalic( mFontItalicFlag );
font.setUnderline( mFontUnderlineFlag );
@@ -816,7 +836,7 @@ namespace glabels
double candidateSize = mFontSize;
while ( candidateSize > 1.0 )
{
font.setPointSizeF( candidateSize );
font.setPixelSize( pixelSize( candidateSize ) );
// Line spacing is affected by font size
QFontMetricsF fontMetrics( font );
@@ -860,6 +880,5 @@ namespace glabels
return candidateSize;
}
}
}