Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-20900

QPainterPath::addText has incorrect font spacing on Windows only

    XMLWordPrintable

Details

    • 6a3f8cbc3ad47d8585aac25d3775ed2350afe20c (qt/qtbase/dev) 9e278ec1f570fb7150cf0053727fe45d187a6bab (qt/qtbase/6.0) 99d3a65cf373bd563710ff91d9922845befa47a2 (qt/tqtc-qtbase/tqtc/lts-5.15)

    Description

      On Windows only, the spacing between characters is often incorrect when using QPainterPath::addText. In the test application below, the drawn strings should be more or less identical.

      main.cpp
      #include <QtGui>
      #include <QFont>
      
      class MyWidget : public QWidget {
      public:
        MyWidget(QWidget *parent = NULL) : QWidget(parent) {
          setFixedSize(100, 100);
        }
      
      protected:
        void paintEvent(QPaintEvent *) {
          QFont f;
          f.setFamily("Arial");
          f.setPixelSize(11);
      
          QPainterPath path;
          path.addText(50, 50, f, "wiwi");
      
          QPainter painter(this);
          painter.setRenderHint(QPainter::Antialiasing, true);
          painter.setBrush(Qt::SolidPattern);
          painter.setPen(Qt::NoPen);
          painter.drawPath(path);
      
          painter.setFont(f);
          painter.setBrush(Qt::NoBrush);
          painter.setPen(Qt::SolidLine);
          painter.drawText(50, 70, "wiwi");
        }
      };
      
      int main(int argc, char **argv) {
        QApplication app(argc, argv);
        MyWidget w;
        w.show();
        return app.exec();
      }
      

      A screenshot of what this looks like on my machine is attached. Some things I have noticed:

      • It is fine on X11 (Ubuntu, at least) and Mac OS X.
      • It only affects certain fonts at certain sizes. If, for example, you change the font in the example to pixel size 12 or point size 11, it looks fine.

      In my application I have to use a QPainterPath, and the output looks horrible on Windows because of this.

      Some more information. I wrote out all the points that are being used for the outline of the "w". Note that the path is drawn such that the left-most part is at 50, and the width is a little under 8.

      moveTo QPointF(51.7778, 50) 
      lineTo QPointF(50.0322, 44.2959) 
      lineTo QPointF(51.0312, 44.2959) 
      lineTo QPointF(51.939, 47.5884) 
      lineTo QPointF(52.2773, 48.813) 
      quadTo QPointF(52.2988, 48.7217) QPointF(52.5728, 47.6367) 
      lineTo QPointF(53.4805, 44.2959) 
      lineTo QPointF(54.4741, 44.2959) 
      lineTo QPointF(55.3281, 47.6045) 
      lineTo QPointF(55.6128, 48.6948) 
      lineTo QPointF(55.9404, 47.5938) 
      lineTo QPointF(56.918, 44.2959) 
      lineTo QPointF(57.8579, 44.2959) 
      lineTo QPointF(56.0747, 50) 
      lineTo QPointF(55.0703, 50) 
      lineTo QPointF(54.1626, 46.584) 
      lineTo QPointF(53.9424, 45.6118) 
      lineTo QPointF(52.7876, 50) 
      

      The problem seems to be that the "w" as drawn by addText is putting the left of the "w" at 51. There is a tiny pixel bleed that goes at x=50, but this bleed is not reflected in the path information. Thus, the "w" is shifted one pixel too far to the left on the path (or 1 pixel too far to the right on addText, one could argue.) In the screenshot below, the red line is at x=49. Notice that the first "w" (addPath) starts at 50 and is roughly 8 pixels wide. The second "w" (addText) really starts at 51 (with a pixel bleed at 50) and has a width of 10.

      Attachments

        1. ss.png
          ss.png
          4 kB
        2. ss.png
          ss.png
          6 kB

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              esabraha Eskil Abrahamsen Blomfeldt
              dmateer Dave Mateer
              Votes:
              4 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes