adjust line spacing and/or leading
-
Hi,
i'm working on an Image to ASCII converter and so far the conversion and displaying on a lable works fine. But now i want to print it to an pdf file and now i have a problem with the line spacing/leading. The font's leading is 0, line space 4, char width 2, char high 4.
void MainWindow::calculateASCII() { ui.label_ascii->setText(""); QImage scaledDown; if (m_image->height() > m_image->width()) scaledDown = m_image->scaledToHeight(ui.spinBox_scale->value()); else scaledDown = m_image->scaledToWidth(ui.spinBox_scale->value()); ui.label->setText(QString::number(scaledDown.width()) + " x " + QString::number(scaledDown.height())); m_ascii = ""; for (int y = 0; y < scaledDown.height(); y++) { for (int x = 0; x < scaledDown.width(); x++) { QRgb pixel = scaledDown.pixel(x, y); int ascii_index = floor(qGray(pixel) / (256.0 / 10.0)); m_ascii = m_ascii + QString(ASCII[ascii_index]) + " "; } m_ascii = m_ascii + "\n"; } ui.label_ascii->setText(m_ascii); } void MainWindow::on_print_clicked() { QPdfWriter pdfwriter("qtwritten.pdf"); pdfwriter.setPageSize(QPagedPaintDevice::A4); QPainter painter(&pdfwriter); painter.setFont(*m_font); painter.drawText(painter.viewport(), Qt::AlignLeft, m_ascii); }
rendering the lable works, but has a bad resolution, and i think the text printing is the better solution. Specially because i can later print the image to the center.
Now when i compare the both results it looks like this:
The source image is 50x50 pxl. There are two different cases: 1) the linespace of the pdf is less then the one from the label, 2) there is a leading between the characters in the pdf.
Both use the same font as you can see and the metric said that the line space is 4, char width 2, char high 4 (so i needed to add a space between all chars).
Best solution for me would be to adjust the line space, so i can remove the space between all chars. That'd improve the quality dramaticaly. Is there a way to do that? -
Hi,
What font are you using ?
-
I setup the font in the constructor. Later on only the font size changes with the spinbox value:
m_font = new QFont(); m_font->setFamily("monospace"); m_font->setStyleHint(QFont::Monospace); m_font->setStyleHint(QFont::TypeWriter); m_font->setPointSize(ui.spinBox_pointSize->value());
-
From the looks of it, you are leaking QFont objects. There's no need to allocate it on the heap.
-
But that isn't the problem, is it? I allocated a qfont because i use it for different things, too. So i don't need to do all the setup all the time again.
-
Are you sure you are getting the font you are asking for ?
-
I checked the metric from that font and it says char width 2, heigh 4 for each char, linespace 4 and leading 0.
So i guess it's my font. And for the label it works. it's just different for the pdf