]>
Commit | Line | Data |
---|---|---|
2bfcb2e4 JB |
1 | /* poppler-annotation-helper.h: qt interface to poppler |
2 | * Copyright (C) 2006, Albert Astals Cid <aacid@kde.org> | |
3 | * Adapting code from | |
4 | * Copyright (C) 2004 by Enrico Ros <eros.kde@email.it> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2, or (at your option) | |
9 | * any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
19 | */ | |
20 | ||
21 | #include <QtCore/QDebug> | |
22 | ||
23 | namespace Poppler { | |
24 | ||
25 | class XPDFReader | |
26 | { | |
27 | public: | |
28 | // find named symbol and parse it | |
29 | static void lookupName( Dict *, const char *, QString & dest ); | |
30 | static void lookupString( Dict *, const char *, QString & dest ); | |
31 | static void lookupBool( Dict *, const char *, bool & dest ); | |
32 | static void lookupInt( Dict *, const char *, int & dest ); | |
33 | static void lookupNum( Dict *, const char *, double & dest ); | |
34 | static int lookupNumArray( Dict *, const char *, double * dest, int len ); | |
35 | static void lookupColor( Dict *, const char *, QColor & color ); | |
36 | static void lookupIntRef( Dict *, const char *, int & dest ); | |
37 | static void lookupDate( Dict *, const char *, QDateTime & dest ); | |
38 | // transform from user coords to normalized ones using the matrix M | |
39 | static inline void transform( double * M, double x, double y, QPointF &res ); | |
40 | }; | |
41 | ||
42 | void XPDFReader::lookupName( Dict * dict, const char * type, QString & dest ) | |
43 | { | |
44 | Object nameObj; | |
45 | dict->lookup( type, &nameObj ); | |
46 | if ( nameObj.isNull() ) | |
47 | return; | |
48 | if ( nameObj.isName() ) | |
49 | dest = nameObj.getName(); | |
50 | else | |
51 | qDebug() << type << " is not Name." << endl; | |
52 | nameObj.free(); | |
53 | } | |
54 | ||
55 | void XPDFReader::lookupString( Dict * dict, const char * type, QString & dest ) | |
56 | { | |
57 | Object stringObj; | |
58 | dict->lookup( type, &stringObj ); | |
59 | if ( stringObj.isNull() ) | |
60 | return; | |
61 | if ( stringObj.isString() ) | |
62 | dest = stringObj.getString()->getCString(); | |
63 | else | |
64 | qDebug() << type << " is not String." << endl; | |
65 | stringObj.free(); | |
66 | } | |
67 | ||
68 | void XPDFReader::lookupBool( Dict * dict, const char * type, bool & dest ) | |
69 | { | |
70 | Object boolObj; | |
71 | dict->lookup( type, &boolObj ); | |
72 | if ( boolObj.isNull() ) | |
73 | return; | |
74 | if ( boolObj.isBool() ) | |
75 | dest = boolObj.getBool() == gTrue; | |
76 | else | |
77 | qDebug() << type << " is not Bool." << endl; | |
78 | boolObj.free(); | |
79 | } | |
80 | ||
81 | void XPDFReader::lookupInt( Dict * dict, const char * type, int & dest ) | |
82 | { | |
83 | Object intObj; | |
84 | dict->lookup( type, &intObj ); | |
85 | if ( intObj.isNull() ) | |
86 | return; | |
87 | if ( intObj.isInt() ) | |
88 | dest = intObj.getInt(); | |
89 | else | |
90 | qDebug() << type << " is not Int." << endl; | |
91 | intObj.free(); | |
92 | } | |
93 | ||
94 | void XPDFReader::lookupNum( Dict * dict, const char * type, double & dest ) | |
95 | { | |
96 | Object numObj; | |
97 | dict->lookup( type, &numObj ); | |
98 | if ( numObj.isNull() ) | |
99 | return; | |
100 | if ( numObj.isNum() ) | |
101 | dest = numObj.getNum(); | |
102 | else | |
103 | qDebug() << type << " is not Num." << endl; | |
104 | numObj.free(); | |
105 | } | |
106 | ||
107 | int XPDFReader::lookupNumArray( Dict * dict, const char * type, double * dest, int len ) | |
108 | { | |
109 | Object arrObj; | |
110 | dict->lookup( type, &arrObj ); | |
111 | if ( arrObj.isNull() ) | |
112 | return 0; | |
113 | Object numObj; | |
114 | if ( arrObj.isArray() ) | |
115 | { | |
116 | len = qMin( len, arrObj.arrayGetLength() ); | |
117 | for ( int i = 0; i < len; i++ ) | |
118 | { | |
119 | dest[i] = arrObj.arrayGet( i, &numObj )->getNum(); | |
120 | numObj.free(); | |
121 | } | |
122 | } | |
123 | else | |
124 | { | |
125 | len = 0; | |
126 | qDebug() << type << "is not Array." << endl; | |
127 | } | |
128 | arrObj.free(); | |
129 | return len; | |
130 | } | |
131 | ||
132 | void XPDFReader::lookupColor( Dict * dict, const char * type, QColor & dest ) | |
133 | { | |
134 | double c[3]; | |
135 | if ( XPDFReader::lookupNumArray( dict, type, c, 3 ) == 3 ) | |
136 | dest = QColor( (int)(c[0]*255.0), (int)(c[1]*255.0), (int)(c[2]*255.0)); | |
137 | } | |
138 | ||
139 | void XPDFReader::lookupIntRef( Dict * dict, const char * type, int & dest ) | |
140 | { | |
141 | Object refObj; | |
142 | dict->lookupNF( type, &refObj ); | |
143 | if ( refObj.isNull() ) | |
144 | return; | |
145 | if ( refObj.isRef() ) | |
146 | dest = refObj.getRefNum(); | |
147 | else | |
148 | qDebug() << type << " is not Ref." << endl; | |
149 | refObj.free(); | |
150 | } | |
151 | ||
152 | void XPDFReader::lookupDate( Dict * dict, const char * type, QDateTime & dest ) | |
153 | { | |
154 | Object dateObj; | |
155 | dict->lookup( type, &dateObj ); | |
156 | if ( dateObj.isNull() ) | |
157 | return; | |
158 | if ( dateObj.isString() ) | |
159 | { | |
160 | const char * s = dateObj.getString()->getCString(); | |
161 | if ( s[0] == 'D' && s[1] == ':' ) | |
162 | s += 2; | |
163 | int year, mon, day, hour, min, sec; | |
164 | if ( sscanf( s, "%4d%2d%2d%2d%2d%2d", &year, &mon, &day, &hour, &min, &sec ) == 6 ) | |
165 | { | |
166 | QDate d( year, mon, day ); | |
167 | QTime t( hour, min, sec ); | |
168 | if ( d.isValid() && t.isValid() ) | |
169 | dest = QDateTime(d, t); | |
170 | } | |
171 | else | |
172 | qDebug() << "Wrong Date format '" << s << "' for '" << type << "'." << endl; | |
173 | } | |
174 | else | |
175 | qDebug() << type << " is not Date" << endl; | |
176 | dateObj.free(); | |
177 | } | |
178 | ||
179 | void XPDFReader::transform( double * M, double x, double y, QPointF &res ) | |
180 | { | |
181 | res.setX( M[0] * x + M[2] * y + M[4] ); | |
182 | res.setY( M[1] * x + M[3] * y + M[5] ); | |
183 | } | |
184 | ||
185 | /** @short Helper classes for CROSSDEPS resolving and DS conversion. */ | |
186 | struct ResolveRevision | |
187 | { | |
188 | int prevAnnotationID; // ID of the annotation to be reparended | |
189 | int nextAnnotationID; // (only needed for speeding up resolving) | |
190 | Annotation * nextAnnotation; // annotation that will act as parent | |
191 | Annotation::RevScope nextScope; // scope of revision (Reply) | |
192 | Annotation::RevType nextType; // type of revision (None) | |
193 | }; | |
194 | ||
195 | struct ResolveWindow | |
196 | { | |
197 | int popupWindowID; // ID of the (maybe shared) window | |
198 | Annotation * annotation; // annotation having the popup window | |
199 | }; | |
200 | ||
201 | struct PostProcessText // this handles a special pdf case conversion | |
202 | { | |
203 | Annotation * textAnnotation; // a popup text annotation (not FreeText) | |
204 | bool opened; // pdf property to convert to window flags | |
205 | }; | |
206 | ||
207 | struct PopupWindow | |
208 | { | |
209 | Annotation * dummyAnnotation; // window properties (in pdf as Annotation) | |
210 | bool shown; // converted to Annotation::Hidden flag | |
211 | }; | |
212 | ||
213 | } |