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. Correct use of QFontMetrics.leftBearing for drawing Hindi characters using QRawFont
Forum Update on Monday, May 27th 2025

Correct use of QFontMetrics.leftBearing for drawing Hindi characters using QRawFont

Scheduled Pinned Locked Moved Solved General and Desktop
fontskerningfontmetricsdevnagarihindi
14 Posts 3 Posters 1.0k 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.
  • S SGaist
    30 Jun 2022, 17:59

    Hi,

    Can you provide a minimal compilable example that reproduces that ?

    Can you check whether it's still the case with Qt 6 ?

    T Offline
    T Offline
    TheCrowKaka
    wrote on 6 Jul 2022, 15:01 last edited by
    #4

    @SGaist Hello
    I have now tested the same application with Qt6.2.1 and there is no difference in the results.

    It is very confusing for me to understand the right solution to this issue.

    A Qt Enthusiastic...

    1 Reply Last reply
    0
    • S SGaist
      30 Jun 2022, 17:59

      Hi,

      Can you provide a minimal compilable example that reproduces that ?

      Can you check whether it's still the case with Qt 6 ?

      T Offline
      T Offline
      TheCrowKaka
      wrote on 6 Jul 2022, 18:35 last edited by
      #5

      @SGaist Hello

      I tried something else.

      In the same example, instead of trying to paint individual characters, i tried painting the complete text and lo.., i got perfect result.
      Which means the Qt font engine has all the text shaper rules to draw the text properly.

          basepath.addText(x,y,font(),text);
          painter.drawPath(basepath);
      
      

      OR

      painter.drawText(x,y,text);
      

      Both give perfect result as seen here.
      Screen Shot 2022-07-07 at 12.01.57 AM.png

      This will work for all straight line text applications.. However, my application is simillar to the wiggly example where i need align the text along a curve. In that case, i am unable to understand how to get the glyphs of individual characters so that i can align them along a path.

      A Qt Enthusiastic...

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 6 Jul 2022, 20:07 last edited by
        #6

        Then that might be a font specific issue.
        Can you try with a different one that provides the char set you need ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        T 1 Reply Last reply 7 Jul 2022, 16:45
        0
        • S SGaist
          6 Jul 2022, 20:07

          Then that might be a font specific issue.
          Can you try with a different one that provides the char set you need ?

          T Offline
          T Offline
          TheCrowKaka
          wrote on 7 Jul 2022, 16:45 last edited by
          #7

          @SGaist
          No. It does not seem to be a font specific issue. I have tried this will all the indic fonts i have and still have same results.

          I feel, this is a lot to do with the kerning of fonts. Since the results are proper with Path.addText and painter.drawText, i think Qt has the necessary tools to make this happen.

          Now, because I am interested in aligning text on a path just the way it is seen in Inkscape.. as seen here,
          Screenshot_8.png

          In order to do this, I have 2 options.
          Option 1: use the QFontMetrics and other classes available in Qt that give the right distances to combine 2 characters. Along with that get the combined character glyph paths which can be worked upon to align characters on the path.

          Option 2: I can get the combined paths using painterpath.addtext. I need to find out the right way to get the individual glyphs to align them on the path.

          Option 3: any other method that i dont know....

          Is this possible in Qt?

          A Qt Enthusiastic...

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 7 Jul 2022, 18:18 last edited by
            #8

            Good question. I recommend that you bring it to the interest mailing where you will find Qt's developers/maintainers.

            You might also want to check the bug report system. There are likely related tickets.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            T 1 Reply Last reply 8 Jul 2022, 02:42
            0
            • S SGaist
              7 Jul 2022, 18:18

              Good question. I recommend that you bring it to the interest mailing where you will find Qt's developers/maintainers.

              You might also want to check the bug report system. There are likely related tickets.

              T Offline
              T Offline
              TheCrowKaka
              wrote on 8 Jul 2022, 02:42 last edited by
              #9

              @SGaist OK. I will do both these things. Thanks for all the help.

              A Qt Enthusiastic...

              T 1 Reply Last reply 10 Jul 2022, 13:39
              0
              • A Offline
                A Offline
                Allan Jensen
                wrote on 8 Jul 2022, 05:58 last edited by
                #10

                QRawFont only does basic kerning, it doesn't do HarfBuzz shaping, so you can't really use QRawFont for any complex scripts (It assumes a 1 letter to 1 glyph mapping).

                T 2 Replies Last reply 8 Jul 2022, 19:35
                0
                • A Allan Jensen
                  8 Jul 2022, 05:58

                  QRawFont only does basic kerning, it doesn't do HarfBuzz shaping, so you can't really use QRawFont for any complex scripts (It assumes a 1 letter to 1 glyph mapping).

                  T Offline
                  T Offline
                  TheCrowKaka
                  wrote on 8 Jul 2022, 19:35 last edited by
                  #11

                  @Allan-Jensen Thanks for this response.

                  Yes, I am getting that. However, unable to understand what function will give the resultant metrics and even more important the combined glyph indexes after the shaping result.

                  A Qt Enthusiastic...

                  1 Reply Last reply
                  0
                  • A Allan Jensen
                    8 Jul 2022, 05:58

                    QRawFont only does basic kerning, it doesn't do HarfBuzz shaping, so you can't really use QRawFont for any complex scripts (It assumes a 1 letter to 1 glyph mapping).

                    T Offline
                    T Offline
                    TheCrowKaka
                    wrote on 9 Jul 2022, 10:40 last edited by
                    #12

                    @Allan-Jensen @SGaist I have managed to get a few things done... now just a small thing remains.

                    As mentioned in my earlier post, I got a solution to the Option2: Get individual glyph paths for the combined paths in the Hindi fonts. SO now that i have the individual glyph paths, i can align them as I want.
                    This is how I could achieve this result. Here is the changed paintEvent.

                    void WigglyWidget::paintEvent(QPaintEvent * /* event */)
                    //! [1] //! [2]
                    {
                        QRawFont *myFont = new QRawFont;
                        *myFont = QRawFont::fromFont(font());
                    
                        QFontMetrics metrics(font());
                        int x = (width() - metrics.horizontalAdvance(text)) / 2;
                        int y = (height() + metrics.ascent() - metrics.descent()) / 2;
                        QColor color;
                    //! [2]
                    
                    //! [3]
                        QPainter painter(this);
                        QPainterPath basepath;
                        QTextLayout curlay(text,font());
                        curlay.beginLayout();
                        curlay.createLine();
                        curlay.endLayout();
                        QList<QGlyphRun> curglyrunlist = curlay.glyphRuns();
                        QGlyphRun curglyrun = curglyrunlist[0];
                        QList<quint32> chindices = curglyrun.glyphIndexes();
                        *myFont =curglyrun.rawFont();
                        QList<QPointF> curglyadvlist = myFont->advancesForGlyphIndexes(chindices,QRawFont::KernedAdvances);
                    
                    //    qreal prevx;
                        for (int i = 0; i < chindices.count(); ++i) {
                            color.setRgb(255,0,0);
                            painter.setPen(color);
                            QPainterPath charpath = myFont->pathForGlyph(chindices[i]);    // get the glyph path for character.
                            qreal widt = curglyadvlist[i].x();
                    //        if(widt==0)
                    //            charpath = charpath.translated(prevx,y);
                    //        else
                                charpath = charpath.translated(x,y);
                            basepath.addPath(charpath);
                    //        basepath.addEllipse(x,y,5,5);
                    //        qDebug()<<widt<<x<<y;
                    //        prevx = x;
                            x += (widt);
                        }
                        painter.drawPath(basepath);
                        painter.drawText((width() - metrics.horizontalAdvance(text)) / 2,y-50,text);
                    }
                    

                    And this is the result.Screenshot_1.png

                    Here you can see that the one drawn below is as per the painter.drawPath which is the result of individual glyphs drawn as painterpaths.
                    While the top one is drawn with painter.drawText.
                    Now the problem remains that the bottom addition to the main character is not drawn at the right location. The advancesForGlyphIndexes function is not returning the right advance for this character. Whereas when the painter.drawText is drawing it perfectly well.

                    Now, am I missing something... some leftBearing or RightBearing that needs to be added or subtracted from the characters... ?

                    A Qt Enthusiastic...

                    1 Reply Last reply
                    1
                    • T TheCrowKaka
                      8 Jul 2022, 02:42

                      @SGaist OK. I will do both these things. Thanks for all the help.

                      T Offline
                      T Offline
                      TheCrowKaka
                      wrote on 10 Jul 2022, 13:39 last edited by
                      #13

                      @Allan-Jensen @SGaist I have finally solved it.

                      I substituted myFont->advancesForGlyphIndexes(chindices,QRawFont::KernedAdvances); with curglyrun.positions(); and that gave me the right positions for all glyphs.

                      A Qt Enthusiastic...

                      1 Reply Last reply
                      1
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 10 Jul 2022, 17:19 last edited by
                        #14

                        Great !

                        Thanks for sharing your solution :-)

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        13/14

                        10 Jul 2022, 13:39

                        • Login

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