PDA

View Full Version : Font size calculation when painting in a QImage



Ishark
6th July 2007, 20:29
Hello,
I'm trying to generate a modified image starting from a preexisting PNG file and drawing some text on it. I'm not getting what I think I should get :)
In particular, I have some issues with font sizes.
I have found that the PNG resolution plays a role in the calculation of the actual pixel size of the font, if I increase the resolution and keep the font size fixed, the actual rendered text is smaller. The sizes do not match what I see if I do the same in inkscape, but this is not a problem. My problem is that there seems to be a "minimum pixel size" which limits the size of the smallest font I can draw. Right now I have the PNG at 72x72 dpi and a font size of 2 looks identical to font size 10.

The code:


int main(int argc, char **argv)
{
QApplication a(argc, argv);
QImage* card = new QImage("blank72.png");
QPainter* painter = new QPainter(card);
QFont* FontText = new QFont("Serif", 2, QFont::Normal);
QFont* FontText2 = new QFont("Serif", 10, QFont::Normal);
painter->setRenderHint(QPainter::TextAntialiasing, true);
painter->setPen(Qt::black);
painter->setFont(*FontText);
painter->drawText(QRect(0,0,256,128), Qt::TextWordWrap, "Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt");
painter->setFont(*FontText2);
painter->drawText(QRect(0,128,256,128), Qt::TextWordWrap, "Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt");
QLabel* label = new QLabel(0);
label->setPixmap(QPixmap::fromImage(*card));
label->show();

return a.exec();
}


blank72.png is a 256x256 white image with 72dpi resolution.

Side question: is there any easy way to have the text justified?

wysota
9th July 2007, 16:40
Remember that you can set not only the point size for the font, but also the pixel size (which is dependent on the resolution).

Ishark
10th July 2007, 18:29
There still seem to be some kind of "lower limit", even when using setPixelSize.
I've resorted to rendering in a separate image and then drawing this image while scaling it down. Of course it's not very readable, but this is not a problem since the text is more "decoration" than other.

Is there a way to control the lineskip to draw the text lines closer or again it's easier to draw into intermediate images and build the text in blocks?

Thanks in advance.

wysota
15th July 2007, 23:22
I guess you'd have to use QTextLayout or implement your own QAbstractDocumentLayout implementation.