Results 1 to 6 of 6

Thread: QPrinter & QPainter Font Differences Between LInux and WIndows

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 13:58. Reason: fixed word wrapping

Similar Threads

  1. Replies: 1
    Last Post: 25th December 2007, 10:35
  2. QPainter & QPrinter on linux (fedora 7)
    By wbt_ph in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2007, 15:37
  3. Font Problem Porting from Windows to Linux
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 13th July 2007, 10: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.