1 /*
2     <one line to give the program's name and a brief idea of what it does.>
3     Copyright (C) 2012  <copyright holder> <email>
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 "adjustedlink.h"
22 
23 #include <stdexcept>
24 #include <boost/numeric/conversion/cast.hpp>
25 using boost::numeric_cast;
26 
link() const27 QSharedPointer< Poppler::Link > AdjustedLink::link() const
28 {
29   return m_link;
30 }
31 
linkArea() const32 QRectF AdjustedLink::linkArea() const
33 {
34   QRectF const& orig( link()->linkArea() );
35   QRectF retval( link()->linkArea() );
36   switch(ri.pagePart()) {
37     case PagePart::FullPage:
38       break;
39     case PagePart::LeftHalf:
40       if ( retval.left() > 0.5 ) {
41 	return QRectF();
42       }
43       retval.setLeft( orig.left() * 2.0 );
44       retval.setWidth( orig.width() * 2.0 );
45       break;
46     case PagePart::RightHalf:
47       if ( retval.right() < 0.5 ) {
48 	// no part of the rectangle is in our page
49 	return QRectF();
50       }
51       retval.setLeft( (orig.left() - 0.5) * 2.0 );
52       retval.setWidth( orig.width() * 2.0 );
53       break;
54   }
55 
56   if ( retval.height() < 0 ) {
57     retval.setHeight( -retval.height() );
58     retval.moveTop( retval.top() - retval.height());
59   }
60 
61   return retval;
62 
63 }
64 
linkType() const65 Poppler::Link::LinkType AdjustedLink::linkType() const
66 {
67   return link()->linkType();
68 }
69 
70 
AdjustedLink(const RenderingIdentifier & renderIdent,QSharedPointer<Poppler::Link> link)71 AdjustedLink::AdjustedLink(const RenderingIdentifier& renderIdent, QSharedPointer< Poppler::Link > link)
72 :  m_link(link), ri(renderIdent)
73 {
74   if ( linkArea().isNull() )
75     throw OutsidePage();
76 }
77 
78 
OutsidePage()79 AdjustedLink::OutsidePage::OutsidePage(): runtime_error("This link is not inside the current page part")
80 {
81 }
82 
83 
targetPageNumber() const84 uint AdjustedLink::targetPageNumber() const
85 {
86 	/* Although page numbers in a PDF are non-negative, poppler's pageNumber
87 	 * is a signed integer.
88 	 *
89 	 * numeric_cast checks that the number actually *is* non-negative.
90 	 */
91   return numeric_cast<unsigned>(lgt().destination().pageNumber()) -1u;
92 }
93 
94 
lgt() const95 Poppler::LinkGoto const& AdjustedLink::lgt() const
96 {
97   return dynamic_cast<Poppler::LinkGoto const&>( *m_link );
98 }
99