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. QRawFont::pathForGlyph returns low quality glyph

QRawFont::pathForGlyph returns low quality glyph

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 393 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.
  • G Offline
    G Offline
    Glax
    wrote on last edited by
    #1

    I'm trying to render some text manually using QRawFont, but it looks like the path data it returns contains jagged lines instead of smooth curves

    Here's an example:
    Example

    The filled area is based on the QPainterPath returned by the QRawFont, the outline is rendered by a QGraphicsTextItem. both are based on the same QFont instance.
    It seems the jaggedness is more pronounced for smaller point sizes.

    is there any way to get the smooth shape of the glyph as a QPainterPath?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Glax
      wrote on last edited by
      #2

      Maybe the issue is with some kind of numerical instability with QPainterPath?

      I managed to work around the issue by upscaling the QRawFont, getting the shape from that, simplifying it and scaling it back down:

      void upscaled_raw()
      {
          QFont font =  query;
          font.setPointSizeF(font.pointSizeF() * 1000);
          raw_scaled = QRawFont::fromFont(font);
      }
      
      QPainterPath path_for_glyph(quint32  glyph)
      {
          QPainterPath path = raw_scaled.pathForGlyph(glyph).simplified();
          if ( raw_scaled.pixelSize() == 0 )
              return path;
      
          QPainterPath dest;
          qreal mult = raw.pixelSize() / raw_scaled.pixelSize();
      
          std::array<QPointF, 3> data;
          int data_i = 0;
          for ( int i = 0; i < path.elementCount(); i++ )
          {
              auto element = path.elementAt(i);
              QPointF p = element * mult;
              switch ( element.type )
              {
                  case QPainterPath::MoveToElement:
                      dest.moveTo(p);
                      break;
                  case QPainterPath::LineToElement:
                      dest.lineTo(p);
                      break;
                  case QPainterPath::CurveToElement:
                      data_i = 0;
                      data[0] = p;
                      break;
                  case QPainterPath::CurveToDataElement:
                      ++data_i;
                      data[data_i] = p;
                      if ( data_i == 2 )
                      {
                          dest.cubicTo(data[0], data[1], data[2]);
                          data_i = -1;
                      }
                      break;
              }
          }
      
          return dest;
      }
      

      Note that both rescaling and simplifying are required to get a nice shape, neither works on its own.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mammut
        wrote on last edited by
        #3

        You can use antialias on the QGraphicsView and the glyph outline will be smooth:

        view->setRenderHint(QPainter::Antialiasing);
        
        1 Reply Last reply
        0

        • Login

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