1 /*
2 dspdfviewer - Dual Screen PDF Viewer for LaTeX-Beamer
3 Copyright (C) 2012 Danny Edel <mail@danny-edel.de>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20
21 #include "renderutils.h"
22
23 #include "debug.h"
24 #include <QApplication>
25 #include <stdexcept>
26
27 #include <boost/math/special_functions/round.hpp>
28
29 using boost::math::iround;
30
31 namespace {
32 bool shuttingDown = false;
33 }
34
renderPagePart(QSharedPointer<const Poppler::Page> page,QSize targetSize,PagePart whichPart)35 QImage RenderUtils::renderPagePart(QSharedPointer< const Poppler::Page > page, QSize targetSize, PagePart whichPart)
36 {
37 if ( ! page )
38 {
39 throw std::runtime_error( std::string( qPrintable( QApplication::translate("RenderUtils", "RenderUtils::renderPagePart called with null page. Target size was %1x%2").
40 arg(targetSize.width()).arg(targetSize.height()) ) ) );
41 }
42
43 if ( shuttingDown )
44 return QImage();
45
46 /* pagesize in points, (72 points is an inch) */
47 QSizeF pagesize = page->pageSizeF();
48 QSizeF fullsize = pagesize;
49
50 if ( whichPart != PagePart::FullPage )
51 {
52 /* Only render half the page */
53 pagesize.setWidth(pagesize.width()/2);
54 }
55
56 /* Calculate DPI for displaying on size */
57 /* the 72 comes from converting from points to inches */
58 double dpiWidth = 72.0 * targetSize.width() / pagesize.width();
59 double dpiHeight = 72.0 * targetSize.height() / pagesize.height();
60
61 /* Take the smaller one, so that the image surely fits on target area */
62 double dpi = std::min(dpiWidth, dpiHeight);
63
64 /* Calculate Page Size in pixels */
65
66 QSize fullSizePixels( iround(dpi * fullsize.width() / 72.0),
67 iround(dpi * fullsize.height() / 72.0) );
68
69 /* Calculate rendered image size */
70 QSize imageSizePixels( iround(dpi * pagesize.width() / 72.0),
71 iround( dpi * pagesize.height() / 72.0 ) );
72
73 /* Calculate x-offset */
74 int x = 0;
75 if ( whichPart == PagePart::RightHalf ) {
76 /* start at an offset of width() pixels to the right, rounding up */
77 x = (fullSizePixels.width()+1)/2;
78 }
79
80 /* render it */
81 QImage renderedImage = page->renderToImage(
82 dpi, dpi,
83 x, /* x-offset */
84 0, /* y-offset */
85 imageSizePixels.width(),
86 imageSizePixels.height()
87 );
88
89 return renderedImage;
90 }
91
notifyShutdown(void)92 void RenderUtils::notifyShutdown(void) {
93 shuttingDown = true;
94 }
95