QPainter adds and removes spaces
-
Hi Qt communety,
I have the problem that if I use the drawText function of QPainter it addes and removes random spaces.
I use the QPainter to create PDF's and to create the Preview, in both I have the same Problem. I tried a differen font but nothing helped.
The code to draw text in the PDF Generator is the folowing:qint64 TemplateGenPDF::drawText(Coordinate position, QString text, QString name, double textSize, TextHeightAnchor textHeightAnchor, TextWidthAnchor textWidthAnchor, double lineWidth, bool isEditable, QString font) { QFont qFont(font); QFont qFontA(font); qFont.setPointSizeF(((textSize * std::sqrt(2))/18897.6378) * 2.8346456692913 );//18897.6378 qFontA.setPointSizeF(100); double posX = position.X; double posY = position.Y; QFontMetrics fm(qFontA); if(textWidthAnchor == TextWidthAnchor::Left) { } else if(textWidthAnchor == TextWidthAnchor::Center) { posX -= (textSize * (fm.size(Qt::TextDontPrint, text).width()/double(100))) / 2; } else if(textWidthAnchor == TextWidthAnchor::Right) { posX -= (textSize * (fm.size(Qt::TextDontPrint, text).width()/double(100))); } if(textHeightAnchor == TextHeightAnchor::Top) { posY += textSize; } else if(textHeightAnchor == TextHeightAnchor::Middle) { posY += (textSize / 2); } else if(textHeightAnchor == TextHeightAnchor::Bottom) { } QPen pen(Qt::black); pen.setStyle(Qt::SolidLine); pen.setWidthF(lineWidth); PAINTER->setPen(pen); PAINTER->setBrush(Qt::NoBrush); PAINTER->setFont(qFont); PAINTER->drawText(QPointF(posX, posY), text); return text.length(); }
The whole Projekt can be foun on github: KiCAD FreeCAD TechDraw Template Generator
-
Could you wrap that in a simple test harness to create a self-contained demonstration of the problem? It's unclear what the units and coordinate system should be.
You are using the metrics of a fixed size font to horizontally place the text rendered with a differently size (tiny?) font. This seems odd.
-
This is the most simplifit version that I can make:
void MainWindow::on_pushButton_clicked() { std::shared_ptr<QPdfWriter> PDFWRITER; std::shared_ptr<QPainter> PAINTER; std::shared_ptr<QSvgRenderer> RENDERER; PDFWRITER = std::shared_ptr<QPdfWriter>(new QPdfWriter("Test.pdf")); PDFWRITER->setPageMargins(QMarginsF(0, 0, 0, 0)); PDFWRITER->setPageSize(QPageSize(QSizeF(210, 297), QPageSize::Millimeter)); PDFWRITER->setResolution(480000); PAINTER = std::shared_ptr<QPainter>(new QPainter(PDFWRITER.get())); PAINTER->setTransform(QTransform().scale(18897.6378, 18897.6378));// 18,897.6378 p/mm = 480,000 dpi QString text = "text test ABCDEF, Valid from to"; double textSize = 1.8; double lineWidth = .18; QString font = "osifont"; QFont qFont(font); QFont qFontA(font); qFont.setPointSizeF(((textSize * std::sqrt(2))/18897.6378) * 2.8346456692913 );//18897.6378 qFontA.setPointSizeF(100); double posX = 100; double posY = 100; QFontMetrics fm(qFontA); QPen pen(Qt::black); pen.setStyle(Qt::SolidLine); pen.setWidthF(lineWidth); PAINTER->setPen(pen); PAINTER->setBrush(Qt::NoBrush); PAINTER->setFont(qFont); PAINTER->drawText(QPointF(posX, posY), text); }
In makin this I did find out that it happens if I use a text higt of 2.45mm or lower and if I use somthing bigger like 5mm it dous not.
-
-
Yes I have tryed it with "arial" and there the problemis not showing:
Thx -
Yes I have tryed it with "arial" and there the problemis not showing:
Thx@TobiasFalk42 I modified your example to repeat the text 10 times with sizes 1.8 increasing by 0.2. This is what my result was with Linux, Qt 6.4.0 or 5.15.2, and Okular as the PDF viewer. The text is clear and regular but the font sizes are actually rendered in steps.
This may actually be an issue with the PDF viewer and not the actual PDF.
-
- I tried open it with diferent PDF viewers(Adobe, Okular on WIn and Edge) and still have the Problem
- A friend of miene run the progrma on Linux and did not have the Problem, we opened it with Adobe on Win and Okular on both win and Linux
- Just for cheking, I downloaded the newest version of osifontosifont and tryed it again with normal and italic, did not change a thing.
-
I dit let the Program print me the PointSize and the result is: 0.000381838 Pt, this is with 480 000 DPI.
After that I changed the DPI(Resolution) to 1200 and whit a 0.152735Pt font, but I still get the Problem even with a Resolution of 100DPI I have the problemvoid MainWindow::on_pushButton_clicked() { double PAGEDPI = 100; double PAGEPPMM = PAGEDPI * (double)5/127; // DPI to points/mm std::shared_ptr<QPdfWriter> PDFWRITER; std::shared_ptr<QPainter> PAINTER; std::shared_ptr<QSvgRenderer> RENDERER; PDFWRITER = std::shared_ptr<QPdfWriter>(new QPdfWriter("Test.pdf")); PDFWRITER->setPageMargins(QMarginsF(0, 0, 0, 0)); PDFWRITER->setPageSize(QPageSize(QSizeF(210, 297), QPageSize::Millimeter)); PDFWRITER->setResolution(PAGEDPI); PAINTER = std::shared_ptr<QPainter>(new QPainter(PDFWRITER.get())); PAINTER->setTransform(QTransform().scale(PAGEPPMM, PAGEPPMM)); QString text = "text test ABCDEF, Valid from to"; double textSize = 1.8; double lineWidth = .18; QString font = "osifont"; QFont qFont(font); QFont qFontA(font); qFont.setPointSizeF(((textSize * std::sqrt(2))/PAGEPPMM) * 2.8346456692913 ); qDebug() << qFont.pointSizeF(); qFontA.setPointSizeF(100); double posX = 100; double posY = 100; QFontMetrics fm(qFontA); //qFont.setItalic(true); QPen pen(Qt::black); pen.setStyle(Qt::SolidLine); pen.setWidthF(lineWidth); PAINTER->setPen(pen); PAINTER->setBrush(Qt::NoBrush); PAINTER->setFont(qFont); PAINTER->drawText(QPointF(posX, posY), text); }