QPrinter gives different font spacing for PDF and physical printer

Viewed 629

I'm using QPrinter to fill forms with text and print them on the printer.

For tests I generate pdf files to avoid paper and ink waste. I figured out that font spacings are different for PDF's and factical prints

Minimal example:

#include <QApplication>
#include <QPrinter>
#include <QPrinterInfo>
#include <QPainter>
#include <QFontDatabase>

void printForm(bool pdf);

QString g_fontFamily = "";

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    int id = QFontDatabase::addApplicationFont(":/IBMPlexMono-Medium.ttf");
    g_fontFamily = QFontDatabase::applicationFontFamilies(id).at(0);

    printForm(true); //pdf
    printForm(false); //physical printer

    return a.exec();
}


void printForm(bool pdf)
{
    //setup printer
    QPrinter *printer;
    if(pdf)
    {
        printer = new QPrinter(QPrinter::ScreenResolution);
        printer->setOutputFormat(QPrinter::PdfFormat);
        printer->setOutputFileName("./regular.pdf");
    }
    else
    {
        printer = new QPrinter(QPrinterInfo::defaultPrinter(), QPrinter::ScreenResolution);
        printer->setOutputFormat(QPrinter::NativeFormat);
    }
    printer->setOrientation(QPrinter::Orientation::Landscape);
    QPageSize customSize(QSizeF(105.0, 157.76), QPageSize::Millimeter, "", QPageSize::ExactMatch);
    printer->setPageSize(customSize);

    //draw background
    QImage img(":/regular_transfer_form");
    QPainter painter(printer);
    painter.drawImage(QRect(0,0, printer->width(), printer->height()), img, QRect(0,0, img.width(), img.height()));

    //draw text
    painter.setPen(Qt::black);
    QFont font(g_fontFamily, 11);
    font.setStyleHint(QFont::TypeWriter);
    font.setLetterSpacing(QFont::PercentageSpacing, 212);

    painter.setFont(font);

    QTextOption textOption(Qt::AlignVCenter | Qt::AlignLeft);
    textOption.setWrapMode(QTextOption::NoWrap);

    QString text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit";

    QRectF relativeArea(0.05838, 0.0783, 0.8971, 0.04583); //coords relative to background

    QRectF areaOnPaper(relativeArea.x() * printer->width(),
                                relativeArea.y() * printer->height(),
                                relativeArea.width() * printer->width(),
                                relativeArea.height() * printer->height()
                                );

    painter.drawText(areaOnPaper, text, textOption); //draw 1-st line wit percentage Spacing

    //set spacing to absolute
    font.setLetterSpacing(QFont::AbsoluteSpacing, 10);
    painter.setFont(font);

    areaOnPaper.setRect(areaOnPaper.x(), areaOnPaper.y() + 28, areaOnPaper.width(), areaOnPaper.height());
    painter.drawText(areaOnPaper, text, textOption); //draw 2-nd line wit absolute Spacing

}

And the results:

PDF: Its fine enter image description here

Print from the printer: enter image description here

As you can see - on paper the letters are not in boxes, but the code is identical for both.

I tried with QFont::PercentageSpacing (1-st line) and QFont::AbsoluteSpacing (2nd line) due to Note in QPrinter documentation. No success.

Am I doing something wrong or there is a bug in Qt?

Is there any way to achieve the same result for both cases?

Full example on github with background image and font

EDIT: https://bugreports.qt.io/browse/QTBUG-72939

0 Answers
Related