Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter adds and removes spaces
QtWS25 Last Chance

QPainter adds and removes spaces

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 580 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TobiasFalk42
    wrote on 30 Nov 2022, 09:19 last edited by
    #1

    Hi Qt communety,
    I have the problem that if I use the drawText function of QPainter it addes and removes random spaces.
    spcae_problems.JPG
    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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 30 Nov 2022, 10:27 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TobiasFalk42
        wrote on 30 Nov 2022, 18:37 last edited by TobiasFalk42
        #3

        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.
        spcae_problems_2.JPG

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stryga42
          wrote on 30 Nov 2022, 20:13 last edited by
          #4

          My guess is that QFont picks a font with corrupt / strange kerning. Do you see similar behavior if you specify another font family? Double check which fonts are actually installed on your system and if they can be used reasonably from another SW.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TobiasFalk42
            wrote on 30 Nov 2022, 21:53 last edited by
            #5

            Yes I have tryed it with "arial" and there the problemis not showing:
            spcae_problems_3.JPG
            Thx

            C 1 Reply Last reply 30 Nov 2022, 23:13
            0
            • T TobiasFalk42
              30 Nov 2022, 21:53

              Yes I have tryed it with "arial" and there the problemis not showing:
              spcae_problems_3.JPG
              Thx

              C Offline
              C Offline
              ChrisW67
              wrote on 30 Nov 2022, 23:13 last edited by
              #6

              @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.
              29e5315e-98d0-489f-a29f-ea20b89c5fda-image.png

              This may actually be an issue with the PDF viewer and not the actual PDF.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                TobiasFalk42
                wrote on 1 Dec 2022, 07:27 last edited by
                #7
                1. I tried open it with diferent PDF viewers(Adobe, Okular on WIn and Edge) and still have the Problem
                2. 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
                3. Just for cheking, I downloaded the newest version of osifontosifont and tryed it again with normal and italic, did not change a thing.
                  spcae_problems_4.JPG
                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  TobiasFalk42
                  wrote on 1 Dec 2022, 07:49 last edited by
                  #8

                  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 problem

                  void 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);
                  }
                  
                  1 Reply Last reply
                  0

                  7/8

                  1 Dec 2022, 07:27

                  • Login

                  • Login or register to search.
                  7 out of 8
                  • First post
                    7/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved