Debian Codegraph @

podofo/tools/podofoimpose/impositionplan.cpp

raw
1 /**
2 * SPDX-FileCopyrightText: (C) 2008 Pierre Marchand <[email protected]>
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6 #include "impositionplan.h"
7 #include <fstream>
8 #include <stdexcept>
9 #include <algorithm>
10 #include <cmath>
11 #include <iostream>
12 #include <ostream>
13 #include <cstdio>
14
15 using namespace std;
16 using namespace PoDoFo::Impose;
17
18 PageRecord::PageRecord(int s, int d, double r, double tx, double ty, int du, double sx, double sy)
19 : sourcePage(s),
20 destPage(d),
21 rotate(r),
22 transX(tx),
23 transY(ty),
24 scaleX(sx),
25 scaleY(sy),
26 duplicateOf(du)
27 {
28 };
29
30 PageRecord::PageRecord()
31 : sourcePage(0),
32 destPage(0),
33 rotate(0),
34 transX(0),
35 transY(0),
36 scaleX(1),
37 scaleY(1),
38 duplicateOf(0)
39 {};
40
41 void PageRecord::load(const string& buffer, const map<string, string>& vars)
42 {
43 unsigned len = (unsigned)buffer.length();
44 vector<string> tokens;
45 string ts;
46 for (unsigned i = 0; i < len; i++)
47 {
48 char ci(buffer.at(i));
49 if (ci == ' ')
50 continue;
51 else if (ci == ';')
52 {
53 tokens.push_back(ts);
54 ts.clear();
55 continue;
56 }
57 ts += ci;
58 }
59
60 if (tokens.size() != 5 && tokens.size() != 7)
61 {
62 sourcePage = destPage = 0; // will return false for isValid()
63 cerr << "INVALID_RECORD(" << tokens.size() << ") " << buffer << endl;
64 for (unsigned int i = 0; i < tokens.size(); ++i)
65 cerr << "\t+ " << tokens.at(i) << endl;
66 }
67
68 sourcePage = static_cast<int>(calc(tokens.at(0), vars));
69 destPage = static_cast<int>(calc(tokens.at(1), vars));
70 if ((sourcePage < 1) || (destPage < 1))
71 {
72 sourcePage = destPage = 0;
73 }
74
75 rotate = calc(tokens.at(2), vars);
76 transX = calc(tokens.at(3), vars);
77 transY = calc(tokens.at(4), vars);
78 if (tokens.size() == 7) {
79 scaleX = calc(tokens.at(5), vars);
80 scaleY = calc(tokens.at(6), vars);
81 }
82 else {
83 scaleX = scaleY = 1.0;
84 }
85
86 cerr << " " << sourcePage << " " << destPage << " " << rotate << " " << transX << " " << transY << " " << scaleX << " " << scaleY << endl;
87
88 }
89
90 double PageRecord::calc(const string& s, const map<string, string>& vars)
91 {
92 // cerr<< s;
93 vector<string> tokens;
94 unsigned tokenCount = (unsigned)s.length();
95 string ts;
96 for (unsigned i = 0; i < tokenCount; i++)
97 {
98 char ci(s.at(i));
99 // if ( ci == 0x20 || ci == 0x9 )// skip spaces and horizontal tabs
100 // continue;
101 if ((ci == '+')
102 || (ci == '-')
103 || (ci == '*')
104 || (ci == '/')
105 || (ci == '%')
106 || (ci == '|')
107 || (ci == '"')
108 || (ci == '(')
109 || (ci == ')'))
110 {
111 // commit current string
112 if (ts.length() > 0)
113 {
114 map<string, string>::const_iterator vit = vars.find(ts);
115 if (vit != vars.end())
116 {
117 // cerr<<"A Found "<<ts<<" "<< vit->second <<endl;
118 tokens.push_back(Util::dToStr(calc(vit->second, vars)));
119 }
120 else
121 {
122 // cerr<<"A Not Found "<<ts<<endl;
123 tokens.push_back(ts);
124 }
125 }
126 ts.clear();
127 // append operator
128 ts += ci;
129 tokens.push_back(ts);
130 ts.clear();
131 }
132 else if (ci > 32)
133 {
134 ts += ci;
135 }
136 // else
137 // cerr<<"Wrong char : "<< ci <<endl;
138 }
139 if (ts.length() > 0)
140 {
141 map<string, string>::const_iterator vit2 = vars.find(ts);
142 if (vit2 != vars.end())
143 {
144 // cerr<<endl<<"Found "<<ts<<endl;
145 tokens.push_back(Util::dToStr(calc(vit2->second, vars)));
146 }
147 else
148 {
149 // if((ts.length() > 0) && (ts[0] == '$'))
150 // {
151 // cerr<<endl<<"Not Found \"";
152 // for(unsigned int c(0);c < ts.length(); ++c)
153 // {
154 // cerr<<ts[c]<<"/";
155 // }
156 // cerr<<"\""<<endl;
157 // for(map<string,string>::iterator i(PoDoFoImpose::vars.begin());i != PoDoFoImpose::vars.end(); ++i)
158 // {
159 // // cerr<<"VA \""<< i->first << "\" => " <<(i->first == ts ? "True" : "False") <<endl;
160 // for(unsigned int c(0);c < i->first.length(); ++c)
161 // {
162 // cerr<< i->first[c]<<"/";
163 // }
164 // cerr<<endl;
165 // }
166 // }
167 tokens.push_back(ts);
168 }
169 }
170 double result(calc(tokens));
171 // cerr<<" = "<<result<<endl;
172 return result;
173
174 }
175
176 double PageRecord::calc(const vector<string>& t)
177 {
178 // cerr<<"C =";
179 // for(uint i(0);i<t.size();++i)
180 // cerr<<" "<< t.at(i) <<" ";
181 // cerr<<endl;
182
183
184 if (t.size() == 0)
185 return 0.0;
186
187 double ret(0.0);
188
189 vector<double> values;
190 vector<string> ops;
191 ops.push_back("+");
192
193 for (unsigned int vi = 0; vi < t.size(); ++vi)
194 {
195 if (t.at(vi) == "(")
196 {
197 vector<string> tokens;
198 int cdeep(0);
199 // cerr<<"(";
200 for (++vi; vi < t.size(); ++vi)
201 {
202 // cerr<<t.at ( ti );
203 if (t.at(vi) == ")")
204 {
205 if (cdeep == 0)
206 break;
207 else
208 {
209 --cdeep;
210 }
211 }
212 else if (t.at(vi) == "(")
213 {
214 ++cdeep;
215 }
216 // cerr<<endl<<"\t";
217 tokens.push_back(t.at(vi));
218 }
219 // cerr<<endl;
220 values.push_back(calc(tokens));
221 }
222 else if (t.at(vi) == "+")
223 ops.push_back("+");
224 else if (t.at(vi) == "-")
225 ops.push_back("-");
226 else if (t.at(vi) == "*")
227 ops.push_back("*");
228 else if (t.at(vi) == "/")
229 ops.push_back("/");
230 else if (t.at(vi) == "%")
231 ops.push_back("%");
232 else if (t.at(vi) == "|")
233 ops.push_back("|");
234 else if (t.at(vi) == "\"")
235 ops.push_back("\"");
236 else
237 values.push_back(std::atof(t.at(vi).c_str()));
238 }
239
240 if (values.size() == 1)
241 ret = values.at(0);
242 else
243 {
244 for (unsigned int vi = 0; vi < ops.size(); ++vi)
245 {
246 // cerr<<"OP>> \""<<ret<<"\" " << ops.at ( vi )<<" \""<<values.at( vi ) <<"\" = ";
247 if (ops.at(vi) == "+")
248 ret += values.at(vi);
249 else if (ops.at(vi) == "-")
250 ret -= values.at(vi);
251 else if (ops.at(vi) == "*")
252 ret *= values.at(vi);
253 /// I know it’s not good (tm)
254 /// + http://gcc.gnu.org/ml/gcc/2001-08/msg00853.html
255 else if (ops.at(vi) == "/")
256 {
257 if (values.at(vi) == 0.0)
258 ret = 0.0;
259 else
260 ret /= values.at(vi);
261 }
262 else if (ops.at(vi) == "%")
263 {
264 if (values.at(vi) == 0.0)
265 ret = 0.0;
266 else
267 ret = static_cast<int> (ret) % static_cast<int> (values.at(vi));
268 }
269 else if (ops.at(vi) == "|") // Stands for max(a,b), easier than true condition, allow to filter division by 0
270 ret = std::max(ret, values.at(vi));
271 else if (ops.at(vi) == "\"") // Stands for min(a,b)
272 ret = std::min(ret, values.at(vi));
273
274 // cerr<<ret<<endl;
275 }
276 }
277 // cerr<<" <"<< values.size() <<"> "<<ret<<endl;
278 return ret;
279 }
280
281 bool PageRecord::isValid() const
282 {
283 //TODO
284 if (!sourcePage || !destPage)
285 return false;
286 return true;
287 }
288
289 ImpositionPlan::ImpositionPlan(const SourceVars& sv) :
290 sourceVars(sv),
291 m_destWidth(0.0),
292 m_destHeight(0.0),
293 m_scale(1.0)
294 {
295 }
296
297 ImpositionPlan::~ImpositionPlan()
298 {
299 }
300
301 bool ImpositionPlan::valid() const
302 {
303 if (destWidth() <= 0.0)
304 return false;
305 else if (destHeight() <= 0.0)
306 return false;
307 // else if ( scale() <= 0.0 )
308 // return false;
309 else if (size() == 0)
310 return false;
311
312 return true;
313
314 }
315
316 void ImpositionPlan::setDestWidth(double theValue)
317 {
318 m_destWidth = theValue;
319 }
320
321
322 void ImpositionPlan::setDestHeight(double theValue)
323 {
324 m_destHeight = theValue;
325 }
326
327
328 void ImpositionPlan::setScale(double theValue)
329 {
330 m_scale = theValue;
331 }
332
333 void ImpositionPlan::setBoundingBox(const string& theString)
334 {
335 m_boundingBox = theString;
336 }