Results 1 to 6 of 6

Thread: QPrinter & QPainter Font Differences Between LInux and WIndows

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPrinter & QPainter Font Differences Between LInux and WIndows

    I have a Qt 4.4.3 app that uses QPainter to draw to a PDF QPrinter. It works perfectly in linux, and in windows, however the font sizes are enlarged when the code runs on the windows version. How can I get the Windows and Linux PDF's to render fonts exactly the same size?

    Heres the code that I used to create the PDF:

    Qt Code:
    1. void PdfGenerate::generatePdf() {
    2.  
    3. QString stufftowritetopdf;
    4.  
    5. QPrinter printer; //create a printer
    6. setupLetterPoints(printer, "filename.pdf"); //set up reference points for our coordinate system
    7. QPainter painter(&printer); //make a painter, which uses this printer,
    8.  
    9. ///////////////////////////////////////////
    10. /// in here draw some text
    11. /////////////////////////////////////////////
    12.  
    13. stufftowritetopdf = "here we put a lot of text so that its long enough
    14. to force a word wrap. When this text is rendered in linux and windows,
    15. the resulting PDFs will have a different visable font size, even though the
    16. code and text is the same. If You put enough text here to fill up a page
    17. when compiled in linux, it will run off the bottom of the page in the windows
    18. compiled version due to the windows version using a larger font size.";
    19. totaldepth = totaldepth + drawrichtextinrect(&painter, leftmargin, totaldepth, horizmarginwidth, 0, stufftowritetopdf, normalsize);
    20.  
    21. ///////////////////////////////////////////
    22. /// done drawing text
    23. /////////////////////////////////////////////
    24.  
    25. printlastpage(painter); //end the final page:
    26. }
    27.  
    28. //set up the point coordinates for a letter sheet of paper:
    29. void PdfGenerate::setupLetterPoints(QPrinter& printer, QString filename) {
    30.  
    31. printer.setOrientation(QPrinter::Portrait); //set theorientation of the paper
    32. printer.setOutputFormat(QPrinter::PdfFormat); //make that printer as a PDF
    33. printer.setOutputFileName(filename); //set the PDF file name
    34. printer.setPaperSize(QPrinter::Letter); //set paper size
    35. printer.setFullPage(false); //coordinates based on printable area
    36. printer.setPageMargins( 0.5, 0.5, 0.5, 0.5, QPrinter::Inch);
    37. printer.setResolution(72); //72 is standard
    38.  
    39. //(72 points / inch)
    40. pagewidth = 540; //8.5 inch
    41. pageheight = 720; //11 inch
    42.  
    43. margin = 0; // 0.5 inch margin
    44.  
    45. topmargin = margin;
    46. bottommargin = pageheight - margin;
    47. leftmargin = margin;
    48. rightmargin = pagewidth - margin;
    49.  
    50. vertmarginwidth = pageheight - (margin * 2);
    51. horizmarginwidth = pagewidth - (margin * 2);
    52.  
    53. noindent = 0;
    54. indent1 = noindent + 15;
    55. indent2 = indent1 + 15;
    56.  
    57. //font sizes:
    58. fontFamily = "Times New Roman";
    59. headersize = 15;
    60. titlesize = 12;
    61. stitlesize = 10;
    62. normalsize = 8;
    63. contractsize = 6;
    64. pagenumsize = 8;
    65.  
    66. totaldepth = topmargin;
    67. pagenum = 1;
    68. totalpages = 5;
    69. }
    70.  
    71. //draw rich text in a rect:
    72. qreal PdfGenerate::drawrichtextinrect(QPainter *painter, qreal ulx, qreal uly, qreal rwidth, int flags, QString &text, int fontsize) {
    73. QRectF rect(ulx, uly - 2, rwidth, 0);
    74. qreal txtHeight = rect.height();
    75.  
    76. QTextDocument textdocument;
    77. textdocument.setHtml(text);
    78. textdocument.setDefaultFont(QFont(fontFamily, fontsize));
    79.  
    80. textdocument.setPageSize(QSize(rect.width(), QWIDGETSIZE_MAX));
    81.  
    82. QAbstractTextDocumentLayout* layout = textdocument.documentLayout();
    83.  
    84. //if our text takes up more than the rectangle height, then return the actual space taken up:
    85. if (layout->documentSize().height() > txtHeight) {
    86. txtHeight = layout->documentSize().height() - 4;
    87. }
    88.  
    89. const int height = qRound(layout->documentSize().height());
    90.  
    91. int y = rect.y();
    92. if (flags & Qt::AlignBottom)
    93. y += (rect.height() - height);
    94. else if (flags & Qt::AlignVCenter)
    95. y += (rect.height() - height)/2;
    96.  
    97. QAbstractTextDocumentLayout::PaintContext context;
    98. context.palette.setColor(QPalette::Text, painter->pen().color());
    99.  
    100. painter->save();
    101.  
    102. painter->translate(rect.x(), rect.y());
    103. layout->draw(painter, context);
    104.  
    105. painter->restore();
    106.  
    107. return txtHeight;
    108. }
    109.  
    110. void PdfGenerate::printlastpage(QPainter& painter) {
    111. painter.end(); //done drawing, so save the PDF
    112. }
    To copy to clipboard, switch view to plain text mode 

    Also, the PDF that is generated on the linux machine looks the same on the windows machine as it does on the linux machine. Conversely the PDF generated on the windows machine will have larger fonts when viewed on the linux machine. This leads me to believe the problem lies either in my code, the font, or a difference in the PDF QPrinter from windows to linux.
    Last edited by tpf80; 13th January 2009 at 14:58. Reason: fixed word wrapping

  2. #2
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPrinter & QPainter Font Differences Between LInux and WIndows

    I ran into similar issue lately. It turned out that QTextDocument will allways assume (logical) screen resolution when painting, and that is probably different between windows and linux. On windows (where I'm working) the logical screen resolution is 96 DPI.

    In my case everything was fine when printing in default QPrinter::ScreenResolution mode, but everything was far too small when printing with QPrinter::HighResolution.

    My workaround was to scale the QPainter so QTextDocument could paint believing that the resolution is = screen resolution, and QPainter would "fix" it by its transformation.

    Here a snippet of code:

    Qt Code:
    1. int screenResolution = Bps::screenResolution();
    2. qreal oldWidth = prv_->mDocument.textWidth();
    3. if (prv_->mResolution>0 && prv_->mResolution != screenResolution) {
    4. qreal f = prv_->mResolution / screenResolution;
    5. aPainter->scale(f, f);
    6. prv_->mDocument.setTextWidth(oldWidth / f);
    7. } // if
    8. prv_->mDocument.drawContents(aPainter);
    9. prv_->mDocument.setTextWidth(oldWidth);
    To copy to clipboard, switch view to plain text mode 
    Last edited by seneca; 14th January 2009 at 15:05.

  3. #3
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter & QPainter Font Differences Between LInux and WIndows

    Thanks, your tip did reveal that the difference in screen resolutions was causing the PDFs to render differently. Since QTextDocument is only used to paint to the PDF and not shown to the user, I would have never figured this out.

  4. #4
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter & QPainter Font Differences Between LInux and WIndows

    Using the scaling workaround, I have gotten the PDF's to render very closely to the same on Windows and Linux. Although the characters render exactly the same size, Still the windows one renders blocks of text slightly bigger. What I notice is that there are still some slight differences in the way the font is rendered. For example:

    1) There seems to be a difference between the character spacing on windows vs linux. Some phrases rendered in the linux version are maybe a pixel or 2 wider than on windows or vice versa. On average however it seems that windows is rendering blocks of text slightly wider.

    2) Underlined text in linux renders different as well. For example if I have letters that hang down such as "y" "q" "g", etc. in linux the underline is below the whole letter, and on windows, the letter actually crosses the line.

    On a small form with not much text this isn't much of a problem, but on a document with many pages of dense text, it is much more noticeable, and causes some text to not fit.

    I am thinking that the font on windows is not exactly the same as the one on linux, but very close.

    Currently I use the "Times New Roman" family and use this function to set the font:
    Qt Code:
    1. textdocument.setDefaultFont(QFont(fontFamily, fontsize));
    To copy to clipboard, switch view to plain text mode 

    is there a way to specify more strictly specify the font to use, for example including a font file with my program and telling it to only use that font to generate PDF's?

  5. #5
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPrinter & QPainter Font Differences Between LInux and WIndows

    QFontDatabase will do that.

  6. #6
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter & QPainter Font Differences Between LInux and WIndows

    Thanks, looking further into QFontDatabase, I found blog entry at http://labs.trolltech.com/blogs/2006...un-with-fonts/ which had a good example of this.

Similar Threads

  1. Replies: 1
    Last Post: 25th December 2007, 11:35
  2. QPainter & QPrinter on linux (fedora 7)
    By wbt_ph in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2007, 16:37
  3. Font Problem Porting from Windows to Linux
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 13th July 2007, 11:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.