Debian Codegraph @

podofo/tools/podofoimpose/planreader_legacy.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 "planreader_legacy.h"
7
8 #include <fstream>
9 #include <stdexcept>
10 #include <algorithm>
11 #include <cmath>
12 #include <istream>
13 #include <iostream> //XXX#define MAX_SOURCE_PAGES 5000
14 #include <ostream>
15
16 #ifdef PODOFO_HAVE_LUA
17 #include "planreader_lua.h"
18 #endif // PODOFO_HAVE_LUA
19
20 using namespace std;
21 using namespace PoDoFo::Impose;
22
23 #define MAX_RECORD_SIZE 2048
24
25 int PlanReader_Legacy::sortLoop(vector<string>& memfile, int numline)
26 {
27 // cerr<<"===================================== "<<numline<<endl;
28 //Debug
29 // for(map<string, double>::iterator dit(localvars.begin());dit!=localvars.end(); dit++)
30 // {
31 // cerr<<"R "<<dit->first<<" = "<<dit->second<<endl;
32 // }
33 //
34 map<string, string> storedvars = I->vars;
35 int startAt(numline);
36 string buffer(memfile.at(numline));
37 unsigned len = (unsigned)buffer.length();
38 string iterN;
39 unsigned a = 1;
40 char ca = 0;
41 for (; a < len; a++)
42 {
43 ca = buffer.at(a);
44 if (ca == '[')
45 break;
46 else if (ca == 0x20 || ca == 0x9)
47 continue;
48 iterN += buffer.at(a);
49 }
50
51 map<string, double> increments;
52 string tvar;
53 string tinc;
54 a++;
55 bool varside(true);
56 for (; a < len; a++)
57 {
58 ca = buffer.at(a);
59 // if(ca == 0x20 || ca == 0x9 )
60 // continue;
61 if ((ca == ']') || (ca == ';')) // time to commit
62 {
63 if (I->vars.find(tvar) != I->vars.end())
64 {
65 // cerr<< "I " << tvar <<" = "<< tinc <<endl;
66 increments.insert(pair<string, double>(tvar, std::atof(tinc.c_str())));
67 }
68 tvar.clear();
69 tinc.clear();
70 if (ca == ';')
71 varside = true;
72 else
73 break;
74 }
75 else if (ca == '+')
76 {
77 varside = false;
78 continue;
79 }
80 else
81 {
82 if (varside)
83 tvar += ca;
84 else
85 tinc += ca;
86 }
87 }
88
89 int endOfloopBlock = numline + 1;
90 int openLoop = 0;
91 for (unsigned int bolb2 = (numline + 1); bolb2 < memfile.size(); bolb2++)
92 {
93 // cerr<<"| "<< memfile.at ( bolb2 ) <<" |"<<endl;
94 if (memfile.at(bolb2).at(0) == '<')
95 openLoop++;
96 else if (memfile.at(bolb2).at(0) == '>')
97 {
98 if (openLoop == 0)
99 break;
100 else
101 openLoop--;
102 }
103 else
104 endOfloopBlock = bolb2 + 1;
105 }
106
107 unsigned maxIter = (unsigned)PageRecord::calc(iterN, I->vars);
108 for (unsigned iter = 0; iter < maxIter; iter++)
109 {
110 if (iter != 0)
111 {
112 // we set the vars
113 map<string, double>::iterator vit;
114 for (vit = increments.begin(); vit != increments.end(); vit++)
115 {
116 I->vars[vit->first] = Util::dToStr(std::atof(I->vars[vit->first].c_str()) + vit->second);
117 }
118 }
119 for (int subi(numline + 1); subi < endOfloopBlock; subi++)
120 {
121 // cerr<< subi <<"/"<< endOfloopBlock <<" - "<<memfile.at(subi) <<endl;
122
123 if (memfile.at(subi).at(0) == '<')
124 {
125 subi += sortLoop(memfile, subi);
126 // cerr<< "|| " << memfile.at ( subi ) <<endl;
127 }
128 else
129 {
130 PageRecord p;
131 p.load(memfile.at(subi), I->vars);
132 if (!p.isValid() || p.sourcePage > I->sourceVars.PageCount)
133 {
134 // cerr<< "Error p("<<(p.isValid()?"valid":"invalid")<<") "<< p.sourcePage <<endl;
135 continue;
136 }
137 // maxPageDest = std::max ( maxPageDest, p.destPage );
138 // bool isDup(false);
139 // for(ImpositionPlan::const_iterator ipIt(planImposition.begin());ipIt != planImposition.end(); ipIt++)
140 // {
141 // if(ipIt->sourcePage == p.sourcePage)
142 // {
143 // isDup = true;
144 // break;
145 // }
146 // }
147 // if ( isDup )
148 // {
149 // p.duplicateOf = p.sourcePage;
150 // }
151 I->push_back(p);
152 }
153 }
154
155 }
156 // numline = endOfloopBlock;
157 // cerr<<"EOL"<<endl;
158 int retvalue(endOfloopBlock - startAt + 1);
159 I->vars = storedvars;
160 // cerr<<"------------------------------------- "<<retvalue<<endl;
161 return retvalue;
162 }
163
164 PlanReader_Legacy::PlanReader_Legacy(const string& plan, ImpositionPlan* Imp)
165 :I(Imp)
166 {
167 ifstream in(plan.c_str(), ifstream::in);
168 if (!in.good())
169 throw runtime_error("Failed to open plan file");
170
171 // duplicate = MAX_SOURCE_PAGES;
172 vector<string> memfile;
173 do
174 {
175 string buffer;
176 if (!std::getline(in, buffer) && (!in.eof() || in.bad()))
177 {
178 throw runtime_error("Failed to read line from plan");
179 }
180
181 #ifdef PODOFO_HAVE_LUA
182 // This was "supposed" to be a legacy file, but if it starts
183 // with two dashes, it must be a lua file, so process it accordingly:
184 if (buffer.substr(0, 2) == "--") {
185 in.close();
186 PlanReader_Lua(plan, Imp);
187 return;
188 }
189 #endif // PODOFO_HAVE_LUA
190
191 if (buffer.length() < 2) // Nothing
192 continue;
193
194 Util::trimmed_str(buffer);
195 if (buffer.length() < 2)
196 continue;
197 else if (buffer.at(0) == '#') // Comment
198 continue;
199 else
200 {
201 memfile.push_back(buffer);
202 // cerr<<buffer<<endl;
203 }
204 } while (!in.eof());
205 /// PROVIDED
206 I->vars[string("$PagesCount")] = Util::uToStr(I->sourceVars.PageCount);
207 I->vars[string("$SourceWidth")] = Util::dToStr(I->sourceVars.PageWidth);
208 I->vars[string("$SourceHeight")] = Util::dToStr(I->sourceVars.PageHeight);
209 /// END OF PROVIDED
210
211 for (unsigned int numline = 0; numline < memfile.size(); numline++)
212 {
213 string buffer(memfile.at(numline));
214 if (buffer.at(0) == '$') // Variable
215 {
216 unsigned sepPos = (unsigned)buffer.find_first_of('=');
217 string key(buffer.substr(0, sepPos));
218 string value(buffer.substr(sepPos + 1));
219
220 {
221 I->vars[key] = value;
222 }
223 }
224 else if (buffer.at(0) == '<') // Loop - experimental
225 {
226 numline += sortLoop(memfile, numline);
227 }
228 else // Record? We hope!
229 {
230 PageRecord p;
231 p.load(buffer, I->vars);
232 if (!p.isValid() || p.sourcePage > I->sourceVars.PageCount)
233 continue;
234 // maxPageDest = std::max ( maxPageDest, p.destPage );
235 // if ( pagesIndex.find ( p.sourcePage ) != pagesIndex.end() )
236 // {
237 // p.duplicateOf = p.sourcePage;
238 // }
239 I->push_back(p);
240 }
241
242 }
243
244 /// REQUIRED
245 if (I->vars.find("$PageWidth") == I->vars.end())
246 throw runtime_error("$PageWidth not set");
247 if (I->vars.find("$PageHeight") == I->vars.end())
248 throw runtime_error("$PageHeight not set");
249
250 I->setDestWidth(PageRecord::calc(I->vars["$PageWidth"], I->vars));
251 I->setDestHeight(PageRecord::calc(I->vars["$PageHeight"], I->vars));
252 /// END OF REQUIRED
253
254 /// SUPPORTED
255 if (I->vars.find("$ScaleFactor") != I->vars.end())
256 I->setScale(PageRecord::calc(I->vars["$ScaleFactor"], I->vars));
257 if (I->vars.find("$BoundingBox") != I->vars.end())
258 I->setBoundingBox(I->vars["$BoundingBox"]);
259 /// END OF SUPPORTED
260 }