Debian Codegraph @

src/lib/GreatWksParser.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <iomanip>
35 #include <iostream>
36 #include <limits>
37 #include <set>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWTextListener.hxx"
43 #include "MWAWHeader.hxx"
44 #include "MWAWParagraph.hxx"
45 #include "MWAWPosition.hxx"
46 #include "MWAWPrinter.hxx"
47 #include "MWAWSection.hxx"
48 #include "MWAWRSRCParser.hxx"
49 #include "MWAWSubDocument.hxx"
50
51 #include "GreatWksDocument.hxx"
52 #include "GreatWksGraph.hxx"
53 #include "GreatWksText.hxx"
54
55 #include "GreatWksParser.hxx"
56
57 /** Internal: the structures of a GreatWksParser */
58 namespace GreatWksParserInternal
59 {
60
61 ////////////////////////////////////////
62 //! Internal: the state of a GreatWksParser
63 struct State {
64 //! constructor
65 State()
66 : m_columnsWidth()
67 , m_hasColSep(false)
68 , m_actPage(0)
69 , m_numPages(0)
70 , m_headerHeight(0)
71 , m_footerHeight(0)
72 {
73 for (auto &fl : m_hfFlags) fl=false;
74 }
75 //! returns the number of expected header/footer zones
76 int numHeaderFooters() const
77 {
78 int num=0;
79 if (m_hfFlags[2]) num++; // header
80 if (m_hfFlags[3]) num++; // footer
81 if (m_hfFlags[1]) num*=2; // lf page
82 return num;
83 }
84
85 //! returns a section
86 MWAWSection getSection() const
87 {
88 MWAWSection sec;
89 size_t numCols = m_columnsWidth.size()/2;
90 if (numCols <= 1)
91 return sec;
92 sec.m_columns.resize(size_t(numCols));
93 if (m_hasColSep)
94 sec.m_columnSeparator=MWAWBorder();
95 for (size_t c=0; c < numCols; c++) {
96 double wSep=0;
97 if (c)
98 wSep += sec.m_columns[c].m_margins[libmwaw::Left]=
99 double(m_columnsWidth[2*c]-m_columnsWidth[2*c-1])/72./2.;
100 if (c+1!=numCols)
101 wSep+=sec.m_columns[c].m_margins[libmwaw::Right]=
102 double(m_columnsWidth[2*c+2]-m_columnsWidth[2*c+1])/72./2.;
103 sec.m_columns[c].m_width =
104 double(m_columnsWidth[2*c+1]-m_columnsWidth[2*c])+72.*wSep;
105 sec.m_columns[c].m_widthUnit = librevenge::RVNG_POINT;
106 }
107 return sec;
108 }
109
110 //! the columns dimension
111 std::vector<double> m_columnsWidth;
112 //! flags to define header/footer (titlePage, l/rPage, header, footer)
113 bool m_hfFlags[4];
114 //! true if columns have columns separator
115 bool m_hasColSep;
116 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
117
118 int m_headerHeight /** the header height if known */,
119 m_footerHeight /** the footer height if known */;
120 };
121
122 ////////////////////////////////////////
123 //! Internal: the subdocument of a GreatWksParser
124 class SubDocument final : public MWAWSubDocument
125 {
126 public:
127 SubDocument(GreatWksParser &pars, MWAWInputStreamPtr const &input, int zoneId)
128 : MWAWSubDocument(&pars, input, MWAWEntry())
129 , m_id(zoneId) {}
130
131 //! destructor
132 ~SubDocument() final {}
133
134 //! operator!=
135 bool operator!=(MWAWSubDocument const &doc) const final;
136
137 //! the parser function
138 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
139
140 protected:
141 //! the subdocument id
142 int m_id;
143 };
144
145 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type)
146 {
147 if (!listener.get()) {
148 MWAW_DEBUG_MSG(("GreatWksParserInternal::SubDocument::parse: no listener\n"));
149 return;
150 }
151 if (type!=libmwaw::DOC_HEADER_FOOTER) {
152 MWAW_DEBUG_MSG(("GreatWksParserInternal::SubDocument::parse: unknown type\n"));
153 return;
154 }
155 auto *parser=dynamic_cast<GreatWksParser *>(m_parser);
156 if (!parser) {
157 MWAW_DEBUG_MSG(("GreatWksParserInternal::SubDocument::parse: no parser\n"));
158 return;
159 }
160
161 long pos = m_input->tell();
162 parser->sendHF(m_id);
163 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
164 }
165
166 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
167 {
168 if (MWAWSubDocument::operator!=(doc)) return true;
169 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
170 if (!sDoc) return true;
171 if (m_id != sDoc->m_id) return true;
172 return false;
173 }
174 }
175
176 ////////////////////////////////////////////////////////////
177 // constructor/destructor, ...
178 ////////////////////////////////////////////////////////////
179 GreatWksParser::GreatWksParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
180 : MWAWTextParser(input, rsrcParser, header)
181 , m_state()
182 , m_document()
183 {
184 init();
185 }
186
187 GreatWksParser::~GreatWksParser()
188 {
189 }
190
191 void GreatWksParser::init()
192 {
193 resetTextListener();
194 setAsciiName("main-1");
195
196 m_state.reset(new GreatWksParserInternal::State);
197
198 // reduce the margin (in case, the page is not defined)
199 getPageSpan().setMargins(0.1);
200
201 m_document.reset(new GreatWksDocument(*this));
202 m_document->m_newPage=static_cast<GreatWksDocument::NewPage>(&GreatWksParser::newPage);
203 m_document->m_getMainSection=static_cast<GreatWksDocument::GetMainSection>(&GreatWksParser::getMainSection);
204 }
205
206 ////////////////////////////////////////////////////////////
207 // interface with the text parser
208 ////////////////////////////////////////////////////////////
209 MWAWSection GreatWksParser::getMainSection() const
210 {
211 return m_state->getSection();
212 }
213
214 bool GreatWksParser::sendHF(int id)
215 {
216 return m_document->getTextParser()->sendHF(id);
217 }
218
219 ////////////////////////////////////////////////////////////
220 // new page
221 ////////////////////////////////////////////////////////////
222 void GreatWksParser::newPage(int number)
223 {
224 if (number <= m_state->m_actPage || number > m_state->m_numPages)
225 return;
226
227 while (m_state->m_actPage < number) {
228 m_state->m_actPage++;
229 if (!getTextListener() || m_state->m_actPage == 1)
230 continue;
231 getTextListener()->insertBreak(MWAWTextListener::PageBreak);
232 }
233 }
234
235 ////////////////////////////////////////////////////////////
236 // the parser
237 ////////////////////////////////////////////////////////////
238 void GreatWksParser::parse(librevenge::RVNGTextInterface *docInterface)
239 {
240 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
241 bool ok = false;
242 try {
243 // create the asciiFile
244 ascii().setStream(getInput());
245 ascii().open(asciiName());
246
247 checkHeader(nullptr);
248 ok = createZones();
249 if (ok) {
250 createDocument(docInterface);
251 m_document->getGraphParser()->sendPageGraphics();
252 m_document->getTextParser()->sendMainText();
253 #ifdef DEBUG
254 m_document->getTextParser()->flushExtra();
255 #endif
256 }
257 ascii().reset();
258 }
259 catch (...) {
260 MWAW_DEBUG_MSG(("GreatWksParser::parse: exception catched when parsing\n"));
261 ok = false;
262 }
263
264 resetTextListener();
265 if (!ok) throw(libmwaw::ParseException());
266 }
267
268 ////////////////////////////////////////////////////////////
269 // create the document
270 ////////////////////////////////////////////////////////////
271 void GreatWksParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
272 {
273 if (!documentInterface) return;
274 if (getTextListener()) {
275 MWAW_DEBUG_MSG(("GreatWksParser::createDocument: listener already exist\n"));
276 return;
277 }
278
279 // update the page
280 m_state->m_actPage = 0;
281
282 // create the page list
283 int numPages = 1;
284 if (m_document->getGraphParser()->numPages() > numPages)
285 numPages = m_document->getGraphParser()->numPages();
286 if (m_document->getTextParser()->numPages() > numPages)
287 numPages = m_document->getTextParser()->numPages();
288 m_state->m_numPages = numPages;
289
290 MWAWPageSpan ps(getPageSpan());
291 int numHF=m_state->numHeaderFooters();
292 if (numHF!=m_document->getTextParser()->numHFZones()) {
293 MWAW_DEBUG_MSG(("GreatWksParser::createDocument: header/footer will be ignored\n"));
294 numHF=0;
295 }
296 std::vector<MWAWPageSpan> pageList;
297 if (numHF && m_state->m_hfFlags[0]) // title page have no header/footer
298 pageList.push_back(ps);
299 else
300 numPages++;
301 if (numHF) {
302 int id=0;
303 for (int w=0; w<2; ++w) {
304 if (!m_state->m_hfFlags[w+2])
305 continue;
306 auto type= w==0 ? MWAWHeaderFooter::HEADER : MWAWHeaderFooter::FOOTER;
307 MWAWHeaderFooter hF;
308 if (m_state->m_hfFlags[1]==false) {
309 hF=MWAWHeaderFooter(type, MWAWHeaderFooter::ALL);
310 hF.m_subDocument.reset(new GreatWksParserInternal::SubDocument(*this, getInput(), id++));
311 ps.setHeaderFooter(hF);
312 continue;
313 }
314 hF=MWAWHeaderFooter(type, MWAWHeaderFooter::ODD);
315 hF.m_subDocument.reset(new GreatWksParserInternal::SubDocument(*this, getInput(), id++));
316 ps.setHeaderFooter(hF);
317 hF=MWAWHeaderFooter(type, MWAWHeaderFooter::EVEN);
318 hF.m_subDocument.reset(new GreatWksParserInternal::SubDocument(*this, getInput(), id++));
319 ps.setHeaderFooter(hF);
320 }
321 }
322 ps.setPageSpan(numPages);
323 pageList.push_back(ps);
324 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
325 setTextListener(listen);
326 listen->startDocument();
327 }
328
329 ////////////////////////////////////////////////////////////
330 //
331 // Intermediate level
332 //
333 ////////////////////////////////////////////////////////////
334 bool GreatWksParser::createZones()
335 {
336 m_document->readRSRCZones();
337 MWAWInputStreamPtr input = getInput();
338 long pos=36;
339 input->seek(pos, librevenge::RVNG_SEEK_SET);
340 if (!readDocInfo()) {
341 ascii().addPos(pos);
342 ascii().addNote("Entries(DocInfo):###");
343 return false;
344 }
345
346 bool ok=m_document->getTextParser()->createZones(m_state->numHeaderFooters());
347 if (input->isEnd()) // v1 file end here
348 return ok;
349
350 pos = input->tell();
351 if (!m_document->getGraphParser()->readGraphicZone())
352 input->seek(pos, librevenge::RVNG_SEEK_SET);
353 if (!input->isEnd()) {
354 pos = input->tell();
355 MWAW_DEBUG_MSG(("GreatWksParser::createZones: find some extra data\n"));
356 ascii().addPos(pos);
357 ascii().addNote("Entries(Loose):");
358 ascii().addPos(pos+200);
359 ascii().addNote("_");
360 }
361
362 return ok;
363 }
364
365 ////////////////////////////////////////////////////////////
366 //
367 // Low level
368 //
369 ////////////////////////////////////////////////////////////
370
371 ////////////////////////////////////////////////////////////
372 // read some unknown zone in data fork
373 ////////////////////////////////////////////////////////////
374 bool GreatWksParser::readDocInfo()
375 {
376 MWAWInputStreamPtr input = getInput();
377 long pos = input->tell();
378 int const vers=version();
379 if (!input->checkPosition(pos+46+(vers==2?6:0)+12+38*16)) {
380 MWAW_DEBUG_MSG(("GreatWksParser::readDocInfo: the zone is too short\n"));
381 return false;
382 }
383 libmwaw::DebugStream f;
384 f << "Entries(DocInfo):";
385 int val;
386 for (int i=0; i < 4; ++i) {
387 static char const *wh[]= {"fl0", "fl1", "smartquote","hidepict"};
388 val =static_cast<int>(input->readLong(1));
389 if (!val) continue;
390 if (val==1) f << wh[i] << ",";
391 else f << "#" << wh[i] << "=" << val << ",";
392 }
393 val =static_cast<int>(input->readLong(2));
394 if (val!=1) f << "first[page]=" << val << ",";
395 for (int i=0; i < 19; ++i) { // always 0
396 val =static_cast<int>(input->readLong(2));
397 if (val)
398 f << "f" << i+1 << "=" << val << ",";
399 }
400 ascii().addDelimiter(input->tell(),'|');
401 ascii().addPos(pos);
402 ascii().addNote(f.str().c_str());
403
404 pos+=46+(vers==2?6:0);
405 input->seek(pos, librevenge::RVNG_SEEK_SET);
406 f.str("");
407 f << "DocInfo-II:";
408 for (int i=0; i < 4; ++i) {
409 val=static_cast<int>(input->readLong(1));
410 if (!val) continue;
411 static char const *wh[]= {"titlePage", "left/rightPage", "header","footer"};
412 if (val!=1) {
413 f << "#" << wh[i] << "=" << val << ",";
414 continue;
415 }
416 f << wh[i] << ",";
417 m_state->m_hfFlags[i]=true;
418 }
419
420 val=static_cast<int>(input->readLong(2)); // f1=1|2
421 if (val)
422 f << "f0=" << val << ",";
423 f << "colSep[w]=" << float(input->readLong(4))/65536.f << ",";
424 val=static_cast<int>(input->readLong(1));
425 if (val==1) f << "same[colW]?,";
426 else if (val) f << "#same[colW]=" << val << ",";
427 val=static_cast<int>(input->readLong(1));
428 if (val==1) {
429 f << "hasColSep,";
430 m_state->m_hasColSep = true;
431 }
432 else if (val) f << "#hasColSep=" << val << ",";
433 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
434 ascii().addPos(pos);
435 ascii().addNote(f.str().c_str());
436 for (int i=0; i < 14; ++i) {
437 pos = input->tell();
438 f.str("");
439 if (i<4) {
440 static char const *wh[]= {"margins", "header/footer", "1", "pageDim" };
441 f << "DocInfo[" << wh[i] << "]:";
442 }
443 else
444 f << "DocInfo[" << i << "]:";
445
446 double dim[4];
447 for (auto &d : dim) d=double(input->readLong(4))/65536.;
448 if (dim[0]>0 || dim[1]>0||dim[2]>0||dim[3]>0) {
449 f << "dim=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ",";
450 if (i==0) {
451 getPageSpan().setMarginTop(dim[0]/72.0);
452 getPageSpan().setMarginBottom(dim[2]/72.0);
453 getPageSpan().setMarginLeft(dim[1]/72.0);
454 getPageSpan().setMarginRight(dim[3]/72.0);
455 }
456 }
457 ascii().addPos(pos);
458 ascii().addNote(f.str().c_str());
459 input->seek(pos+16, librevenge::RVNG_SEEK_SET);
460 }
461 for (int st=0; st < 2; ++st) {
462 pos=input->tell();
463 f.str("");
464 if (st==0)
465 f << "DocInfo[leftPage]:";
466 else
467 f << "DocInfo[rightPage]:";
468 for (int i=0; i < 12; ++i) {
469 double dim[4];
470 for (auto &d : dim) d=double(input->readLong(4))/65536.;
471 if (dim[0]>0 || dim[1]>0||dim[2]>0||dim[3]>0) {
472 switch (i) {
473 case 0:
474 f << "header=";
475 break;
476 case 11:
477 f << "footer=";
478 break;
479 default:
480 f << "col" << i-1 << "=";
481 if (st==1)
482 continue;
483 m_state->m_columnsWidth.push_back(dim[1]);
484 m_state->m_columnsWidth.push_back(dim[3]);
485 break;
486 }
487 f << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ",";
488 }
489 }
490 ascii().addPos(pos);
491 ascii().addNote(f.str().c_str());
492 }
493
494 return true;
495 }
496
497 ////////////////////////////////////////////////////////////
498 // read the header
499 ////////////////////////////////////////////////////////////
500 bool GreatWksParser::checkHeader(MWAWHeader *header, bool strict)
501 {
502 *m_state = GreatWksParserInternal::State();
503 if (!m_document->checkHeader(header,strict)) return false;
504 return getParserState()->m_kind==MWAWDocument::MWAW_K_TEXT;
505 }
506
507
508
509 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: