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. adjust line spacing and/or leading

adjust line spacing and/or leading

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 4.2k Views 1 Watching
  • 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.
  • QT-static-prgmQ Offline
    QT-static-prgmQ Offline
    QT-static-prgm
    wrote on last edited by
    #1

    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:
    0_1541352608419_Unbenannt.JPG

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What font are you using ?

      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
      • QT-static-prgmQ Offline
        QT-static-prgmQ Offline
        QT-static-prgm
        wrote on last edited by
        #3

        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());
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          From the looks of it, you are leaking QFont objects. There's no need to allocate it on the heap.

          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
          1
          • QT-static-prgmQ Offline
            QT-static-prgmQ Offline
            QT-static-prgm
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Are you sure you are getting the font you are asking for ?

              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
              • QT-static-prgmQ Offline
                QT-static-prgmQ Offline
                QT-static-prgm
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0

                • Login

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