Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT4 – Font rendering problem when using QBitmap



  • Hello,

    I am developing a application for my embedded device using QT4 with Freetype font library.
    I have to create an image to display it on Monochrome display. Therefore, I created instances of QBitmap and QPainter.
    When I draw the text, the text rendering (look) is different. Some part in the characters are quite dark.
    Below is my test code:

    class QBitmapTest
    {
    public:
    QBitmapTest();
    bool Text(wchar_t* f_pszFontName,int f_nXPos, int f_nYPos, int f_nWidth, int f_nHeight,int f_nFontHeight);
    void saveAsImage();
    private:
    QBitmap* m_hBitmap;
    QPen m_hPen;
    QBrush m_hBrush;
    QPainter* m_hdcMem;
    };
    BitmapTest::QBitmapTest()
    {
    m_hBitmap = new QBitmap(500,500);
    m_hdcMem = new QPainter();
    m_hBitmap->fill(Qt::color1);
    if(m_hBitmap->isNull()!= true){
    m_hdcMem->begin(m_hBitmap);
    }
    printf("Painter status: Dc %d ", m_hdcMem->isActive());
    m_hPen.setWidth(1);
    m_hPen.setColor(Qt::color0);
    m_hPen.setStyle(Qt::SolidLine);
    m_hBrush.setColor(Qt::color0);
    m_hBrush.setStyle(Qt::SolidPattern);
    //m_hdcMem->setBackgroundMode(Qt::OpaqueMode);
    m_hdcMem->setPen(m_hPen);
    m_hdcMem->setBrush(m_hBrush);
    }

    bool QBitmapTest::Text(wchar_t* f_pwFontName,int f_nXPos, int f_nYPos, int f_nWidth, int f_nHeight,int f_nFontHeight)
    {
    QFont font;
    // font.setStyleStrategy(QFont::NoFontMerging);
    // font.setStyleStrategy(QFont::PreferQuality);
    // font.setStretch(QFont::SemiExpanded);
    // font.setHintingPreference(QFont::PreferNoHinting);

    if(f_pwFontName){
        QString fontName = QString::fromWCharArray(f_pwFontName);
        font.setFamily(fontName);
        font.setWeight(QFont::Normal);
        font.setPixelSize(f_nFontHeight);
    
        //  font.setPointSize(f_nFontHeight);
        //  font.setFixedPitch(true);
        //  font.setRawMode(true);
        //  m_hDC->setFont(font);
        qDebug() << "Font match" << font.exactMatch();
        QFontInfo chk(font);
        qDebug() << "Font match fi" << chk.exactMatch();
        qDebug() << "Font famly fi" << chk.family();
        qDebug() << "Font fixed pitch fi" << chk.fixedPitch();
        qDebug() << "Font point size fi" << chk.pointSize();
    
        m_hdcMem->setFont(font);
    }
    
    QString str("ABCDEF GHIJK LMNOP QRST WXYZ");
    QRect rect;
    rect.setLeft(f_nXPos);
    rect.setTop(f_nYPos);
    rect.setWidth(f_nWidth);
    rect.setHeight(f_nHeight);
    qDebug() << "Drawing";
    m_hdcMem->drawText(rect,str);
    return true;}
    

    void QBitmapTest::saveAsImage()
    {
    QImage image = m_hBitmap->toImage();
    image.save("test.bmp");
    }

    But when I change QBitmap to QPixmap, I don’t see such rendering issue. If I use QPixmap, I have to convert the pixels data (16 bpp) to monochrome image to display it.

    Font name: Roboto Mono
    Line1 – Font size: 10
    Line2 – Font size: 12
    Line3 – Font size: 13
    Line4 – Font size: 15
    Line5 – Font size: 17

    Image with QBitmap:
    See Line4 (E, F, L, M, NP, Q, R) 5 (B,D,E,J,K,L,P,R,T) and 6(M,N): Some part of characters are quite dark.
    0_1564481062636_1b53f2a5-c48b-4a6d-9082-9322889701cf-image.png
    Image with QPixmap:
    0_1564481092590_9f608328-4761-4327-a1e2-147d997d9863-image.png

    How can I resolve the rendering problem with QBitmap?

    Thanks,
    Hari



  • Of course your QPixmap looks good - it is a grayscale image which has many bits per pixel allowing nice antialiasing of the letters. That is not possible with a 1 bit bitmap. Convert the QPixmap to a bitmap and you'll see similar result.

    The thing to do is to find a font that works well (or at least better) on 1 bit screens.


Log in to reply