QImage from alphaMapForGlyph



  • Hi

    I've succeeded in getting images from glyphs as follows...

    QFont font( "Sans Serif" );
    font.setPointSize(100);
    
    QTextLayout *pTextLayout = new QTextLayout("John's String");
    pTextLayout->setFont(font);
    pTextLayout->beginLayout();
    QTextLine line = pTextLayout->createLine();
    pTextLayout->endLayout();
    
    QList<QGlyphRun> glyphData = pTextLayout->glyphRuns();
    
    QVector<quint32> glyphIndexes = glyphData[0].glyphIndexes();
    
    QRawFont rawFont = QRawFont::fromFont(pTextLayout->font());
    QImage image = rawFont.alphaMapForGlyph(glyphIndexes[0]);
    //image in this case is a capital 'J' in the chosen font
    

    I'm using the images to draw text in an OpenGL scene which is to be used in Windows, Linux and Android.

    The problem is that the images contain only the image of the character and I can find no means of finding out where the character sits relative to the baseline of the text being drawn. So in the text above, the bottom of the apostrophe and the 'g' appear on the baseline rather than descending below it in the case of the 'g' or above it the case of the apostrophe. Is their some means of getting this offset info from the font data?

    Thanks

    J


  • Qt Champions 2016

    Hi
    qfontmetrics has ascent() and descent().
    http://doc.qt.io/qt-5.5/qfontmetrics.html
    So I assumes its such info you are after?



  • Not really, this seems to a per-font limit rather than per-character. Ideally I would like the image offset to be available as say a bounding rectangle for each glyphIndex where the rectangle described the amount above and below the baseline so that I knew where to draw the image.


  • Qt Champions 2016

    and
    QRect QFontMetrics::boundingRect(QChar ch) const
    is not that info if combined with ascent() and descent() ?



  • mrjj

    Don't know how I missed that, thanks. The various bounding rectangles types returned are confusing so I'll need to fiddle around with a simple program to sort this out. When I have I'll post the code and mark it as solved.

    Ta

    J



  • MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QFontDatabase *pFontDatabase = new QFontDatabase();
    QStringList fontList = pFontDatabase->families();
    
    
    //Sans serif has a descender on the capital 'J' which threw me a bit;
    //FreeMono doesn't but it does have a mad apostrophe
    //QFont font( "Sans Serif" );
    QFont font( "FreeMono" );
    font.setPointSize(100);
    
    QFontMetrics *pFontMetrics = new QFontMetrics(font);
    
    aString = QString("John's String");
    
    pImage = new QImage[aString.size()];
    
    QTextLayout *pTextLayout = new QTextLayout(aString);
    pTextLayout->setFont(font);
    pTextLayout->beginLayout();
    QTextLine line = pTextLayout->createLine();
    pTextLayout->endLayout();
    
    QList<QGlyphRun> glyphData = pTextLayout->glyphRuns();
    
    QVector<quint32> glyphIndexes = glyphData[0].glyphIndexes();
    
    QRawFont rawFont = QRawFont::fromFont(pTextLayout->font());
    
    pX = new int[aString.size()];
    pY = new int[aString.size()];
    int x=10;
    int y=200;
    
    for (unsigned int i=0;i<aString.size();i++)
    {
        pX[i]=x;
        pImage[i] = rawFont.alphaMapForGlyph(glyphIndexes[i]);
        if(pImage[i].format()==QImage::Format_Invalid)
        {
            //In this case the space returns invalid
            x=x+rawFont.averageCharWidth();
        }
        else
        {
            QRect charRect = pFontMetrics->boundingRect(aString[i]);
            x=x+pImage[i].width();
            pY[i]=y+charRect.y();
        }
    }
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::paintEvent(QPaintEvent *)
    {
    QPainter painter(this);
    for(unsigned int i=0;i<aString.size();i++)
    {
    painter.drawImage(pX[i],pY[i],pImage[i]);
    }
    }


  • Qt Champions 2016

    so it worked?



  • It did, but I'm not familiar with this forum and couldn't find the solved button. I have now.


  • Qt Champions 2016

    @johnthesoftware
    well, you are not the first and not the last to wonder where the button is :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.