1 /***************************************************************************
2 * Copyright (C) 2006 by Dominik Seichter *
3 * domseichter@web.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (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 Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 * *
20 * In addition, as a special exception, the copyright holders give *
21 * permission to link the code of portions of this program with the *
22 * OpenSSL library under certain conditions as described in each *
23 * individual source file, and distribute linked combinations *
24 * including the two. *
25 * You must obey the GNU General Public License in all respects *
26 * for all of the code used other than OpenSSL. If you modify *
27 * file(s) with this exception, you may extend this exception to your *
28 * version of the file(s), but you are not obligated to do so. If you *
29 * do not wish to do so, delete this exception statement from your *
30 * version. If you delete this exception statement from all source *
31 * files in the program, then also delete it here. *
32 ***************************************************************************/
33
34 #include "PdfRect.h"
35
36 #include "PdfArray.h"
37 #include "PdfVariant.h"
38 #include "PdfDefinesPrivate.h"
39
40 #include <iostream>
41 #include <sstream>
42 #include <iomanip>
43
44 static void NormalizeCoordinates( double &coord1, double &coord2 );
45
46 namespace PoDoFo {
47
PdfRect()48 PdfRect::PdfRect()
49 {
50 m_dBottom = m_dLeft = m_dWidth = m_dHeight = 0;
51 }
52
PdfRect(double dLeft,double dBottom,double dWidth,double dHeight)53 PdfRect::PdfRect( double dLeft, double dBottom, double dWidth, double dHeight )
54 {
55 m_dBottom = dBottom;
56 m_dLeft = dLeft;
57 m_dWidth = dWidth;
58 m_dHeight = dHeight;
59 }
60
PdfRect(const PdfArray & inArray)61 PdfRect::PdfRect( const PdfArray& inArray )
62 {
63 m_dBottom = m_dLeft = m_dWidth = m_dHeight = 0;
64 FromArray( inArray );
65 }
66
PdfRect(const PdfRect & rhs)67 PdfRect::PdfRect( const PdfRect & rhs )
68 {
69 this->operator=( rhs );
70 }
71
ToVariant(PdfVariant & var) const72 void PdfRect::ToVariant( PdfVariant & var ) const
73 {
74 PdfArray array;
75
76 array.push_back( PdfVariant( m_dLeft ) );
77 array.push_back( PdfVariant( m_dBottom ) );
78 array.push_back( PdfVariant( (m_dWidth+m_dLeft) ) );
79 array.push_back( PdfVariant( (m_dHeight+m_dBottom) ) );
80
81 var = array;
82 }
83
ToString() const84 std::string PdfRect::ToString() const
85 {
86 PdfVariant var;
87 std::string str;
88 this->ToVariant( var );
89 var.ToString( str );
90
91 return str;
92
93 /*
94 std::ostringstream oStr;
95 oStr << "[ ";
96 oStr << std::setprecision( 3 ) << m_dLeft << " ";
97 oStr << std::setprecision( 3 ) << m_dBottom << " ";
98 oStr << std::setprecision( 3 ) << m_dWidth + m_dLeft << " ";
99 oStr << std::setprecision( 3 ) << m_dHeight - m_dBottom << " ]";
100
101 return oStr.str();
102 */
103 }
104
FromArray(const PdfArray & inArray)105 void PdfRect::FromArray( const PdfArray& inArray )
106 {
107 if ( inArray.size() == 4 )
108 {
109 double x1 = inArray[0].GetReal();
110 double y1 = inArray[1].GetReal();
111 double x2 = inArray[2].GetReal();
112 double y2 = inArray[3].GetReal();
113
114 // See Pdf Reference 1.7, 3.8.4 Rectangles
115 NormalizeCoordinates( x1, x2 );
116 NormalizeCoordinates( y1, y2 );
117
118 m_dLeft = x1;
119 m_dBottom = y1;
120 m_dWidth = x2 - x1;
121 m_dHeight = y2 - y1;
122 }
123 else
124 {
125 PODOFO_RAISE_ERROR( ePdfError_ValueOutOfRange );
126 }
127 }
128
Intersect(const PdfRect & rRect)129 void PdfRect::Intersect( const PdfRect & rRect )
130 {
131 if( rRect.GetBottom() != 0 || rRect.GetHeight() != 0 || rRect.GetLeft() != 0 || rRect.GetWidth() != 0 )
132 {
133 double diff;
134
135 diff = rRect.m_dLeft - m_dLeft;
136 if ( diff > 0.0 )
137 {
138 m_dLeft += diff;
139 m_dWidth -= diff;
140 }
141
142 diff = (m_dLeft + m_dWidth) - (rRect.m_dLeft + rRect.m_dWidth);
143 if ( diff > 0.0 )
144 {
145 m_dWidth -= diff;
146 }
147
148 diff = rRect.m_dBottom - m_dBottom;
149 if ( diff > 0.0 )
150 {
151 m_dBottom += diff;
152 m_dHeight -= diff;
153 }
154
155 diff = (m_dBottom + m_dHeight) - (rRect.m_dBottom + rRect.m_dHeight);
156 if ( diff > 0.0 )
157 {
158 m_dHeight -= diff;
159 }
160 }
161 }
162
operator =(const PdfRect & rhs)163 PdfRect & PdfRect::operator=( const PdfRect & rhs )
164 {
165 this->m_dBottom = rhs.m_dBottom;
166 this->m_dLeft = rhs.m_dLeft;
167 this->m_dWidth = rhs.m_dWidth;
168 this->m_dHeight = rhs.m_dHeight;
169
170 return *this;
171 }
172
173 };
174
NormalizeCoordinates(double & coord1,double & coord2)175 void NormalizeCoordinates( double & coord1, double & coord2 )
176 {
177 if ( coord1 > coord2 )
178 {
179 double temp = coord1;
180 coord1 = coord2;
181 coord2 = temp;
182 }
183 }
184