Debian Codegraph @

src/lib/WriteNowParser.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 <map>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWTextListener.hxx"
43 #include "MWAWHeader.hxx"
44 #include "MWAWFont.hxx"
45 #include "MWAWPosition.hxx"
46 #include "MWAWPictMac.hxx"
47 #include "MWAWPrinter.hxx"
48
49 #include "WriteNowEntry.hxx"
50 #include "WriteNowText.hxx"
51
52 #include "WriteNowParser.hxx"
53
54 /** Internal: the structures of a WriteNowParser */
55 namespace WriteNowParserInternal
56 {
57 ////////////////////////////////////////
58 //! Internal: the state of a WriteNowParser
59 struct State {
60 //! constructor
61 State()
62 : m_endPos(-1)
63 , m_colorMap()
64 , m_picturesList()
65 , m_actPage(0)
66 , m_numPages(0)
67 , m_headerHeight(0)
68 , m_footerHeight(0)
69 , m_numColumns(1)
70 , m_columnWidth(-1)
71 {
72 }
73
74 //! the last position
75 long m_endPos;
76 //! the color map
77 std::vector<MWAWColor> m_colorMap;
78 //! the list of picture entries
79 std::vector<WriteNowEntry> m_picturesList;
80
81 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
82
83 int m_headerHeight /** the header height if known */,
84 m_footerHeight /** the footer height if known */;
85
86 int m_numColumns /** the number of columns */, m_columnWidth /** the columns size */;
87 };
88
89 ////////////////////////////////////////
90 //! Internal: the subdocument of a WriteNowParser
91 class SubDocument final : public MWAWSubDocument
92 {
93 public:
94 SubDocument(WriteNowParser &pars, MWAWInputStreamPtr const &input, WriteNowEntry const &pos)
95 : MWAWSubDocument(&pars, input, MWAWEntry())
96 , m_pos(pos)
97 {
98 }
99
100 //! destructor
101 ~SubDocument() final {}
102
103 //! operator!=
104 bool operator!=(MWAWSubDocument const &doc) const final;
105
106 //! the parser function
107 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
108
109 protected:
110 //! the subdocument file position
111 WriteNowEntry m_pos;
112 };
113
114 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
115 {
116 if (!listener.get()) {
117 MWAW_DEBUG_MSG(("WriteNowParserInternal::SubDocument::parse: no listener\n"));
118 return;
119 }
120 auto *parser=dynamic_cast<WriteNowParser *>(m_parser);
121 if (!parser) {
122 MWAW_DEBUG_MSG(("WriteNowParserInternal::SubDocument::parse: no parser\n"));
123 return;
124 }
125
126 long pos = m_input->tell();
127 parser->send(m_pos);
128 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
129 }
130
131 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
132 {
133 if (MWAWSubDocument::operator!=(doc)) return true;
134 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
135 if (!sDoc) return true;
136 if (m_pos != sDoc->m_pos) return true;
137 return false;
138 }
139 }
140
141
142 ////////////////////////////////////////////////////////////
143 // constructor/destructor, ...
144 ////////////////////////////////////////////////////////////
145 WriteNowParser::WriteNowParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
146 : MWAWTextParser(input, rsrcParser, header)
147 , m_state()
148 , m_entryManager()
149 , m_textParser()
150 {
151 init();
152 }
153
154 WriteNowParser::~WriteNowParser()
155 {
156 }
157
158 void WriteNowParser::init()
159 {
160 resetTextListener();
161 setAsciiName("main-1");
162
163 m_state.reset(new WriteNowParserInternal::State);
164 m_entryManager.reset(new WriteNowEntryManager);
165
166 // reduce the margin (in case, the page is not defined)
167 getPageSpan().setMargins(0.1);
168
169 m_textParser.reset(new WriteNowText(*this));
170 }
171
172 ////////////////////////////////////////////////////////////
173 // position and height
174 ////////////////////////////////////////////////////////////
175 void WriteNowParser::getColumnInfo(int &numColumns, int &width) const
176 {
177 numColumns = m_state->m_numColumns;
178 width = m_state->m_columnWidth;
179 }
180
181 ////////////////////////////////////////////////////////////
182 // new page and color
183 ////////////////////////////////////////////////////////////
184 void WriteNowParser::newPage(int number)
185 {
186 if (number <= m_state->m_actPage || number > m_state->m_numPages)
187 return;
188
189 while (m_state->m_actPage < number) {
190 m_state->m_actPage++;
191 if (!getTextListener() || m_state->m_actPage == 1)
192 continue;
193 getTextListener()->insertBreak(MWAWTextListener::PageBreak);
194 }
195 }
196
197 bool WriteNowParser::getColor(int colId, MWAWColor &col) const
198 {
199 if (colId >= 0 && colId < int(m_state->m_colorMap.size())) {
200 col = m_state->m_colorMap[size_t(colId)];
201 return true;
202 }
203 return false;
204 }
205
206 void WriteNowParser::sendFootnote(WriteNowEntry const &entry)
207 {
208 if (!getTextListener()) return;
209
210 MWAWSubDocumentPtr subdoc(new WriteNowParserInternal::SubDocument(*this, getInput(), entry));
211 getTextListener()->insertNote(MWAWNote(MWAWNote::FootNote), subdoc);
212 }
213
214 void WriteNowParser::send(WriteNowEntry const &entry)
215 {
216 m_textParser->send(entry);
217 }
218
219 bool WriteNowParser::sendGraphic(int gId, MWAWBox2i const &bdbox)
220 {
221 if (gId < 8 || (gId-8) >= int(m_state->m_picturesList.size())) {
222 MWAW_DEBUG_MSG(("WriteNowParser::sendGraphic: called with bad id=%d\n", gId));
223 return false;
224 }
225 if (!m_state->m_picturesList[size_t(gId)-8].isZone()) {
226 MWAW_DEBUG_MSG(("WriteNowParser::sendGraphic: called with a no zone id=%d\n", gId));
227 return false;
228 }
229
230 sendPicture(m_state->m_picturesList[size_t(gId)-8], bdbox);
231 return true;
232 }
233
234
235 ////////////////////////////////////////////////////////////
236 // the parser
237 ////////////////////////////////////////////////////////////
238 void WriteNowParser::parse(librevenge::RVNGTextInterface *docInterface)
239 {
240 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
241 bool ok = true;
242 try {
243 // create the asciiFile
244 ascii().setStream(getInput());
245 ascii().open(asciiName());
246
247 m_entryManager->reset();
248 checkHeader(nullptr);
249 ascii().addPos(getInput()->tell());
250 ascii().addNote("_");
251
252 ok = createZones();
253 if (ok) {
254 createDocument(docInterface);
255 m_textParser->sendZone(0);
256
257 // ok, we can now send what is not send.
258 m_textParser->flushExtra();
259 MWAWBox2i emptyBdBox;
260 for (auto &picture : m_state->m_picturesList) {
261 if (picture.isParsed() || !picture.isZone()) continue;
262 sendPicture(picture, emptyBdBox);
263 }
264 }
265
266 ascii().reset();
267 }
268 catch (...) {
269 MWAW_DEBUG_MSG(("WriteNowParser::parse: exception catched when parsing\n"));
270 ok = false;
271 }
272
273 resetTextListener();
274 if (!ok) throw(libmwaw::ParseException());
275 }
276
277 ////////////////////////////////////////////////////////////
278 // create the document
279 ////////////////////////////////////////////////////////////
280 void WriteNowParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
281 {
282 if (!documentInterface) return;
283 if (getTextListener()) {
284 MWAW_DEBUG_MSG(("WriteNowParser::createDocument: listener already exist\n"));
285 return;
286 }
287
288 // update the page
289 m_state->m_actPage = 0;
290
291 // create the page list
292 MWAWPageSpan ps(getPageSpan());
293
294 WriteNowEntry entry = m_textParser->getHeader();
295 if (entry.valid()) {
296 MWAWHeaderFooter header(MWAWHeaderFooter::HEADER, MWAWHeaderFooter::ALL);
297 header.m_subDocument.reset(new WriteNowParserInternal::SubDocument(*this, getInput(), entry));
298 ps.setHeaderFooter(header);
299 }
300
301 entry = m_textParser->getFooter();
302 if (entry.valid()) {
303 MWAWHeaderFooter footer(MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
304 footer.m_subDocument.reset(new WriteNowParserInternal::SubDocument(*this, getInput(), entry));
305 ps.setHeaderFooter(footer);
306 }
307
308 int numPage = m_textParser->numPages();
309 m_state->m_numPages = numPage;
310 ps.setPageSpan(m_state->m_numPages+1);
311 std::vector<MWAWPageSpan> pageList(1,ps);
312 //
313 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
314 setTextListener(listen);
315 listen->startDocument();
316 }
317
318
319 ////////////////////////////////////////////////////////////
320 //
321 // Intermediate level
322 //
323 ////////////////////////////////////////////////////////////
324
325 ////////////////////////////////////////////////////////////
326 // try to find the different zone
327 ////////////////////////////////////////////////////////////
328 bool WriteNowParser::createZones()
329 {
330 if (version() < 3) {
331 if (!readDocEntriesV2())
332 return false;
333 }
334 else if (!readDocEntries())
335 return false;
336
337 // the Color map zone
338 auto iter = m_entryManager->m_typeMap.find("ColMap");
339 if (iter != m_entryManager->m_typeMap.end())
340 readColorMap(*iter->second);
341
342 // the graphic zone
343 iter = m_entryManager->m_typeMap.find("GraphZone");
344 if (iter != m_entryManager->m_typeMap.end())
345 parseGraphicZone(*iter->second);
346
347
348 iter = m_entryManager->m_typeMap.find("UnknZone1");
349 if (iter != m_entryManager->m_typeMap.end())
350 readGenericUnkn(*iter->second);
351
352 iter = m_entryManager->m_typeMap.find("PrintZone");
353 if (iter != m_entryManager->m_typeMap.end())
354 readPrintInfo(*iter->second);
355
356 // checkme: never seens, but probably also a list of zone...
357 iter = m_entryManager->m_typeMap.find("UnknZone2");
358 if (iter != m_entryManager->m_typeMap.end())
359 readGenericUnkn(*iter->second);
360
361 bool ok = m_textParser->createZones();
362
363 // fixme: we must continue....
364 libmwaw::DebugStream f;
365 for (auto const &it : m_entryManager->m_typeMap) {
366 WriteNowEntry const &ent = *it.second;
367 if (ent.isParsed()) continue;
368 ascii().addPos(ent.begin());
369 f.str("");
370 f << "Entries(" << it.first << ")";
371 if (ent.id() >= 0) f << "[" << ent.id() << "]";
372 ascii().addNote(f.str().c_str());
373 ascii().addPos(ent.end());
374 ascii().addNote("_");
375 }
376 return ok;
377 }
378
379 ////////////////////////////////////////////////////////////
380 //
381 // Low level
382 //
383 ////////////////////////////////////////////////////////////
384
385 ////////////////////////////////////////////////////////////
386 // try to read the document main zone
387 ////////////////////////////////////////////////////////////
388 bool WriteNowParser::readDocEntries()
389 {
390 MWAWInputStreamPtr input = getInput();
391
392 auto it = m_entryManager->m_typeMap.find("DocEntries");
393 if (it == m_entryManager->m_typeMap.end()) {
394 MWAW_DEBUG_MSG(("WriteNowParser::readDocEntries: can not find last zone\n"));
395 return false;
396 }
397 WriteNowEntry const &entry = *(it->second);
398 if (!entry.valid() || entry.length() < 148) {
399 MWAW_DEBUG_MSG(("WriteNowParser::readDocEntries: last entry is invalid\n"));
400 return false;
401 }
402
403 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
404 bool ok = input->readLong(4) == entry.length();
405 if (!ok || input->readLong(4) != entry.begin()) {
406 MWAW_DEBUG_MSG(("WriteNowParser::readDocEntries: bad begin of last zone\n"));
407 return false;
408 }
409 entry.setParsed(true);
410 libmwaw::DebugStream f;
411 f << "Entries(DocEntries):";
412
413 long val;
414 long const expectedVal[] = { 0, 0x80, 0x40000000L};
415 long freePos = 0;
416 for (int i = 0; i < 7; i++) { // 0, 80, 4000
417 val = long(input->readULong(4));
418 if (i == 3) {
419 freePos = val;
420 continue;
421 }
422 if ((i < 3 && val!= expectedVal[i]) || (i >=3 && val))
423 f << "f" << i << "=" << std::hex << val << std::dec << ",";
424 }
425 if (freePos) { // checkme
426 if (freePos > m_state->m_endPos) {
427 MWAW_DEBUG_MSG(("WriteNowParser::readDocEntries: find odd freePos\n"));
428 }
429 else {
430 f << "freeZone?=" << std::hex << freePos << std::dec << ",";
431 ascii().addPos(freePos);
432 ascii().addNote("Entries(Free)");
433 }
434 }
435 char const *entryName[] = { "TextZone", "TextZone", "TextZone", "UnknZone0",
436 "GraphZone", "ColMap", "StylZone", "FontZone",
437 "UnknZone1", "UnknZone2"
438 };
439 for (int i = 0; i < 10; i++) {
440 WriteNowEntry zone = readEntry();
441 zone.setType(entryName[i]);
442 if (i < 3) zone.setId(i);
443 if (zone.isZone())
444 m_entryManager->add(zone);
445 f << zone;
446 }
447 ascii().addDelimiter(input->tell(), '|');
448 ascii().addPos(entry.begin());
449 ascii().addNote(f.str().c_str());
450 if (entry.length() > 386) {
451 long pos = entry.begin()+376;
452 input->seek(pos, librevenge::RVNG_SEEK_SET);
453 f.str("");
454 f << "DocEntries-II:";
455 m_state->m_numColumns = static_cast<int>(input->readLong(1));
456 f << "nCol=" << m_state->m_numColumns << ",";
457 val = input->readLong(1);
458 if (val != 1) f << "unkn=" << val << ",";
459 m_state->m_columnWidth = static_cast<int>(input->readLong(2));
460 f << "colWidth=" << m_state->m_columnWidth << ",";
461
462 ascii().addPos(pos);
463 ascii().addNote(f.str().c_str());
464
465 if (long(input->tell()) != entry.end())
466 ascii().addDelimiter(input->tell(), '|');
467 }
468 ascii().addPos(entry.end());
469 ascii().addNote("_");
470
471 return true;
472 }
473
474 ////////////////////////////////////////////////////////////
475 // try to read the document main zone : v2
476 ////////////////////////////////////////////////////////////
477 bool WriteNowParser::readDocEntriesV2()
478 {
479 MWAWInputStreamPtr input = getInput();
480 long pos = input->tell(), debPos=pos;
481 libmwaw::DebugStream f;
482 std::stringstream s;
483 f << "Entries(DocEntries):";
484 for (int i = 0; i < 5; i++) {
485 auto val=static_cast<int>(input->readLong(1));
486 if (val != 4 && val != 0x44) {
487 MWAW_DEBUG_MSG(("WriteNowParser::readDocEntriesV2: can not find entries header:%d\n",i));
488 return false;
489 }
490 auto dataPos = long(input->readULong(1));
491 dataPos = (dataPos<<16)+long(input->readULong(2));
492 if (!checkIfPositionValid(dataPos)) {
493 MWAW_DEBUG_MSG(("WriteNowParser::readDocEntriesV2: find an invalid position for entry:%d\n",i));
494 continue;
495 }
496 WriteNowEntry entry;
497 entry.setBegin(dataPos);
498 switch (i) {
499 case 0:
500 case 1:
501 case 2:
502 entry.setType("TextZone");
503 entry.setId(i);
504 break;
505 case 4:
506 entry.setType("PrintZone");
507 break;
508 default: { // find 2 time 0006000800000000
509 std::stringstream name;
510 s << "Unknown" << i;
511 entry.setType(name.str());
512 }
513 }
514 long actPos = input->tell();
515 input->seek(dataPos, librevenge::RVNG_SEEK_SET);
516 entry.setLength(long(input->readULong(2)+2));
517 input->seek(actPos, librevenge::RVNG_SEEK_SET);
518 m_entryManager->add(entry);
519 }
520 f << "ptr=[";
521 for (int i = 0; i < 5; i++)
522 f << std::hex << input->readULong(4) << std::dec << ",";
523 f << "],";
524 ascii().addPos(pos);
525 ascii().addNote(f.str().c_str());
526 input->seek(debPos+0x6E,librevenge::RVNG_SEEK_SET);
527 pos=input->tell();
528 f.str("");
529 f << "DocEntries-II:";
530
531 if (version()==2) {
532 m_state->m_numColumns = static_cast<int>(input->readLong(1));
533 f << "nCol=" << m_state->m_numColumns << ",";
534 long val = input->readLong(1);
535 if (val != 1) f << "unkn=" << val << ",";
536 m_state->m_columnWidth = static_cast<int>(input->readLong(2));
537 f << "colWidth=" << m_state->m_columnWidth << ",";
538 ascii().addDelimiter(input->tell(),'|');
539 }
540 ascii().addPos(pos);
541 ascii().addNote(f.str().c_str());
542 // another copy (visibly the previous version)
543 ascii().addPos(debPos+0xFc);
544 ascii().addNote("DocEntries[Old]:");
545 ascii().addPos(debPos+0x16a);
546 ascii().addNote("DocEntries-II[Old]:");
547 ascii().addPos(debPos+0x1F8);
548 ascii().addNote("_");
549 return true;
550 }
551 ////////////////////////////////////////////////////////////
552 // try to read a graphic list zone
553 ////////////////////////////////////////////////////////////
554 bool WriteNowParser::parseGraphicZone(WriteNowEntry const &entry)
555 {
556 MWAWInputStreamPtr input = getInput();
557
558 if (!entry.valid() || entry.length() < 24) {
559 MWAW_DEBUG_MSG(("WriteNowParser::parseGraphicZone: zone size is invalid\n"));
560 return false;
561 }
562
563 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
564 if (input->readLong(4) != entry.length()) {
565 MWAW_DEBUG_MSG(("WriteNowParser::parseGraphicZone: bad begin of last zone\n"));
566 return false;
567 }
568 libmwaw::DebugStream f;
569 f << "Entries(GraphicZone):";
570 f << "ptr?=" << std::hex << input->readULong(4) << std::dec << ",";
571 f << "ptr2?=" << std::hex << input->readULong(4) << std::dec << ",";
572 long val;
573 for (int i = 0; i < 3; i++) { // always 3, 80, 0 ?
574 val = input->readLong(2);
575 if (val) f << "f" << i << "=" << val << ",";
576 }
577 auto N = static_cast<int>(input->readLong(2));
578 f << "N?=" << N << ",";
579 for (int i = 4; i < 6; i++) { // alway 0 ?
580 val = input->readLong(2);
581 if (val) f << "f" << i << "=" << val << ",";
582 }
583 ascii().addPos(entry.begin());
584 ascii().addNote(f.str().c_str());
585
586 if (entry.length() != 24+12*N) {
587 MWAW_DEBUG_MSG(("WriteNowParser::parseGraphicZone: zone size is invalid(II)\n"));
588 return false;
589 }
590 for (int i = 0; i < N; i++) {
591 long pos = input->tell();
592 WriteNowEntry zone = readEntry();
593 f.str("");
594 if (i < 8)
595 f << "GraphicZoneA-" << i << ":";
596 else
597 f << "GraphicZone-" << i-8 << ":";
598
599 zone.setId((i < 8) ? i : i-8);
600 if (zone.isZone()) {
601 if (i == 0)
602 zone.setType("PrintZone");
603 else if (i < 8) {
604 std::stringstream s;
605 s << "GraphicUnkn" << i;
606 zone.setType(s.str());
607 }
608 else
609 zone.setType("GraphicData");
610 if (i < 8)
611 m_entryManager->add(zone);
612 }
613 else if (zone.m_val[2]==-1 && zone.m_val[3]==0x76543210L) {
614 zone.m_val[2]= zone.m_val[3]=0;
615 f << "*";
616 }
617 if (i >= 8)
618 m_state->m_picturesList.push_back(zone);
619 f << zone;
620
621 ascii().addPos(pos);
622 ascii().addNote(f.str().c_str());
623 }
624
625 entry.setParsed(true);
626 ascii().addPos(entry.end());
627 ascii().addNote("_");
628 return true;
629 }
630
631 ////////////////////////////////////////////////////////////
632 // try to read a graphic zone
633 ////////////////////////////////////////////////////////////
634 bool WriteNowParser::sendPicture(WriteNowEntry const &entry, MWAWBox2i const &bdbox)
635 {
636 MWAWInputStreamPtr input = getInput();
637
638 if (!entry.valid() || entry.length() < 24) {
639 MWAW_DEBUG_MSG(("WriteNowParser::sendPicture: zone size is invalid\n"));
640 return false;
641 }
642
643 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
644 if (input->readLong(4) != entry.length()) {
645 MWAW_DEBUG_MSG(("WriteNowParser::sendPicture: bad begin of last zone\n"));
646 return false;
647 }
648 libmwaw::DebugStream f;
649 f << "Entries(GraphicData):";
650 f << "ptr?=" << std::hex << input->readULong(4) << std::dec << ",";
651 f << "ptr2?=" << std::hex << input->readULong(4) << std::dec << ",";
652 auto type = static_cast<int>(input->readLong(2));
653 if (type != 14) f << "#type=" << type << ",";
654 long val;
655 /* fl0 : 0[pict1] or 1[pict2] : graphic type ?, fl1 : always 0
656 */
657 for (int i = 0; i < 2; i++) {
658 val = input->readLong(1);
659 if (val) f << "fl" << i << "=" << val << ",";
660 }
661
662 f << "ptr3?=" << std::hex << input->readULong(4) << std::dec << ",";
663 for (int i = 0; i < 2; i++) { // alway 0 ?
664 val = input->readLong(2);
665 if (val) f << "f" << i << "=" << val << ",";
666 }
667
668 ascii().addPos(entry.begin());
669 ascii().addNote(f.str().c_str());
670
671 int sz = static_cast<int>(entry.length())-24;
672 if (sz) {
673 long pos = input->tell();
674 std::shared_ptr<MWAWPict> pict(MWAWPictData::get(input, sz));
675 if (!pict) {
676 MWAW_DEBUG_MSG(("WriteNowParser::sendPicture: can not read the picture\n"));
677 ascii().addDelimiter(pos, '|');
678 }
679 else {
680 if (getTextListener()) {
681 MWAWPosition pictPos;
682 if (bdbox.size().x() > 0 && bdbox.size().y() > 0) {
683 pictPos=MWAWPosition(MWAWVec2f(0,0),MWAWVec2f(bdbox.size()), librevenge::RVNG_POINT);
684 pictPos.setNaturalSize(pict->getBdBox().size());
685 }
686 else
687 pictPos=MWAWPosition(MWAWVec2f(0,0),pict->getBdBox().size(), librevenge::RVNG_POINT);
688 pictPos.setRelativePosition(MWAWPosition::Char);
689
690 MWAWEmbeddedObject picture;
691 if (pict->getBinary(picture))
692 getTextListener()->insertPicture(pictPos, picture);
693 }
694
695 #ifdef DEBUG_WITH_FILES
696 if (!entry.isParsed()) {
697 ascii().skipZone(pos, entry.end()-1);
698 librevenge::RVNGBinaryData file;
699 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
700 input->readDataBlock(entry.length(), file);
701 static int volatile pictName = 0;
702 libmwaw::DebugStream f2;
703 f2 << "PICT-" << ++pictName << ".pct";
704 libmwaw::Debug::dumpFile(file, f2.str().c_str());
705 }
706 #endif
707 }
708 }
709
710 entry.setParsed(true);
711 ascii().addPos(entry.end());
712 ascii().addNote("_");
713
714 return true;
715 }
716
717
718 ////////////////////////////////////////////////////////////
719 // try to read the color map zone
720 ////////////////////////////////////////////////////////////
721 bool WriteNowParser::readColorMap(WriteNowEntry const &entry)
722 {
723 m_state->m_colorMap.resize(0);
724
725 MWAWInputStreamPtr input = getInput();
726
727 if (!entry.valid() || entry.length() < 0x10) {
728 MWAW_DEBUG_MSG(("WriteNowParser::readColorMap: zone size is invalid\n"));
729 return false;
730 }
731
732 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
733 if (input->readLong(4) != entry.length()) {
734 MWAW_DEBUG_MSG(("WriteNowParser::readColorMap: bad begin of last zone\n"));
735 return false;
736 }
737 libmwaw::DebugStream f;
738 f << "Entries(ColorMap):";
739 f << "ptr?=" << std::hex << input->readULong(4) << std::dec << ",";
740 f << "ptr2?=" << std::hex << input->readULong(4) << std::dec << ",";
741 long pos, val;
742 for (int i = 0; i < 3; i++) { // always 4, 0, 0 ?
743 val = input->readLong(2);
744 if (val) f << "f" << i << "=" << val << ",";
745 }
746 auto N=static_cast<int>(input->readULong(2));
747 f << "N=" << N << ",";
748 for (int i = 0; i < 2; i++) { // 0
749 val = static_cast<int>(input->readLong(2));
750 if (val) f << "g" << i << "=" << val << ",";
751 }
752 if (long(input->tell())+N*8 > entry.end()) {
753 MWAW_DEBUG_MSG(("WriteNowParser::readColorMap: the zone is too short\n"));
754 return false;
755 }
756 ascii().addPos(entry.begin());
757 ascii().addNote(f.str().c_str());
758 std::vector<long> defPos;
759 for (int n = 0; n < N; n++) {
760 pos = input->tell();
761 f.str("");
762 f << "ColorMap[" << n << "]:";
763 auto type = static_cast<int>(input->readLong(1));
764 switch (type) {
765 case 1:
766 f << "named(RGB),";
767 break;
768 case 2:
769 f << "unamed,";
770 break; // check me : are the color in RGB ?
771 case 3:
772 f << "unamed(RGB),";
773 break;
774 default:
775 MWAW_DEBUG_MSG(("WriteNowParser::readColorMap: find unknown type %d\n", type));
776 f << "#type=" << type << ",";
777 break;
778 }
779 for (int i = 0; i < 3; i++) { // always 0
780 val = input->readLong(1);
781 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
782 }
783 val = long(input->readULong(4));
784 defPos.push_back(pos+val);
785 // fixme: used this to read the data...
786 f << "defPos=" << std::hex << pos+val << std::dec << ",";
787 ascii().addPos(pos);
788 ascii().addNote(f.str().c_str());
789 }
790
791 for (int n = 0; n < N; n++) {
792 pos = defPos[size_t(n)];
793 if (pos+12 > entry.end()) {
794 MWAW_DEBUG_MSG(("WriteNowParser::readColorMap: can not read entry : %d\n", n));
795 return false;
796 }
797
798 input->seek(pos, librevenge::RVNG_SEEK_SET);
799 f.str("");
800 f << "ColorMapData[" << n << "]:";
801 unsigned char col[4];
802 for (auto &c : col) c = static_cast<unsigned char>(input->readULong(2)/256);
803 f << "col=" << MWAWColor(col[0],col[1],col[2],col[3]) << ",";
804 m_state->m_colorMap.push_back(MWAWColor(static_cast<unsigned char>(col[0]),static_cast<unsigned char>(col[1]),static_cast<unsigned char>(col[2])));
805
806 auto sz = static_cast<int>(input->readULong(1));
807 if (pos+8+1+sz > entry.end()) {
808 MWAW_DEBUG_MSG(("WriteNowParser::readColorMap: can not read end of entry : %d\n", n));
809 return false;
810 }
811 std::string name("");
812 for (int i = 0; i < sz; i++) name += char(input->readULong(1));
813
814 if (name.length()) f << name;
815 ascii().addPos(pos);
816 ascii().addNote(f.str().c_str());
817 }
818
819 entry.setParsed(true);
820 ascii().addPos(entry.end());
821 ascii().addNote("_");
822 return true;
823 }
824
825 ////////////////////////////////////////////////////////////
826 // try to read the print info zone
827 ////////////////////////////////////////////////////////////
828 bool WriteNowParser::readPrintInfo(WriteNowEntry const &entry)
829 {
830 MWAWInputStreamPtr input = getInput();
831 int expectedLength = version() <= 2 ? 0x78+2 : 0x88;
832 if (!entry.valid() || entry.length() < expectedLength) {
833 MWAW_DEBUG_MSG(("WriteNowParser::readPrintInfo: zone size is invalid\n"));
834 return false;
835 }
836
837 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
838 long sz = version() <= 2 ? 2+long(input->readULong(2)) : long(input->readULong(4));
839 if (sz != entry.length()) {
840 MWAW_DEBUG_MSG(("WriteNowParser::readPrintInfo: bad begin of last zone\n"));
841 return false;
842 }
843 libmwaw::DebugStream f;
844 f << "Entries(PrintInfo):";
845 if (version()>=3) {
846 f << "ptr?=" << std::hex << input->readULong(4) << std::dec << ",";
847 f << "ptr2?=" << std::hex << input->readULong(4) << std::dec << ",";
848 long val;
849 for (int i = 0; i < 4; i++) { // 15, 0, ??, ???
850 val = input->readLong(2);
851 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
852 }
853 for (int i = 0; i < 2; i++) { // 0
854 val = input->readLong(2);
855 if (val) f << "g" << i << "=" << val << ",";
856 }
857 }
858 libmwaw::PrinterInfo info;
859 if (!info.read(input)) {
860 MWAW_DEBUG_MSG(("WriteNowParser::readPrintInfo: can not read print info\n"));
861 return false;
862 }
863 f << info;
864
865 MWAWVec2i paperSize = info.paper().size();
866 MWAWVec2i pageSize = info.page().size();
867 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
868 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
869
870 // define margin from print info
871 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
872 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
873
874 // move margin left | top
875 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
876 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
877 lTopMargin -= MWAWVec2i(decalX, decalY);
878 rBotMargin += MWAWVec2i(decalX, decalY);
879
880 // decrease right | bottom
881 int rightMarg = rBotMargin.x() -50;
882 if (rightMarg < 0) rightMarg=0;
883 int botMarg = rBotMargin.y() -50;
884 if (botMarg < 0) botMarg=0;
885
886 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
887 getPageSpan().setMarginBottom(botMarg/72.0);
888 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
889 getPageSpan().setMarginRight(rightMarg/72.0);
890 getPageSpan().setFormLength(paperSize.y()/72.);
891 getPageSpan().setFormWidth(paperSize.x()/72.);
892
893 entry.setParsed(true);
894 ascii().addPos(entry.begin());
895 ascii().addNote(f.str().c_str());
896 ascii().addPos(entry.end());
897 ascii().addNote("_");
898 return true;
899 }
900
901 ////////////////////////////////////////////////////////////
902 // try to read the last generic zone ( always find N=0 :-~)
903 ////////////////////////////////////////////////////////////
904 bool WriteNowParser::readGenericUnkn(WriteNowEntry const &entry)
905 {
906 MWAWInputStreamPtr input = getInput();
907
908 if (!entry.valid() || entry.length() < 0x10) {
909 MWAW_DEBUG_MSG(("WriteNowParser::readGenericUnkn: zone size is invalid\n"));
910 return false;
911 }
912
913 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
914 if (input->readLong(4) != entry.length()) {
915 MWAW_DEBUG_MSG(("WriteNowParser::readGenericUnkn: bad begin of last zone\n"));
916 return false;
917 }
918 libmwaw::DebugStream f;
919 f << "Entries(" << entry.type() << "):";
920 f << "ptr?=" << std::hex << input->readULong(4) << std::dec << ",";
921 f << "ptr2?=" << std::hex << input->readULong(4) << std::dec << ",";
922 long pos, val;
923 for (int i = 0; i < 3; i++) { // 7, 0, 0
924 val = input->readLong(2);
925 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
926 }
927 auto N=static_cast<int>(input->readULong(2));
928 f << "N=" << N << ",";
929 for (int i = 0; i < 2; i++) { // 0
930 val = input->readLong(2);
931 if (val) f << "g" << i << "=" << val << ",";
932 }
933 if (long(input->tell())+N*8 > entry.end()) {
934 MWAW_DEBUG_MSG(("WriteNowParser::readGenericUnkn: the zone is too short\n"));
935 return false;
936 }
937 ascii().addPos(entry.begin());
938 ascii().addNote(f.str().c_str());
939 std::vector<long> defPos;
940 for (int n = 0; n < N; n++) {
941 pos = input->tell();
942 f.str("");
943 f << entry.type() << "[" << n << "]:";
944 auto type = static_cast<int>(input->readULong(1));
945 switch (type) {
946 case 0:
947 f << "def,";
948 break;
949 default:
950 MWAW_DEBUG_MSG(("WriteNowParser::readGenericUnkn: find unknown type %d\n", type));
951 f << "#type=" << type << ",";
952 break;
953 }
954 for (int i = 0; i < 3; i++) { // always 0
955 val = input->readLong(1);
956 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
957 }
958 val = long(input->readULong(4));
959 defPos.push_back(pos+val);
960 // fixme: used this to read the data...
961 f << "defPos=" << std::hex << pos+val << std::dec << ",";
962 ascii().addPos(pos);
963 ascii().addNote(f.str().c_str());
964 }
965
966 for (int n = 0; n < N; n++) {
967 pos = defPos[size_t(n)];
968 if (pos == entry.end()) continue;
969 if (pos+12 > entry.end()) {
970 MWAW_DEBUG_MSG(("WriteNowParser::readGenericUnkn: can not read entry : %d\n", n));
971 return false;
972 }
973
974 input->seek(pos, librevenge::RVNG_SEEK_SET);
975 f.str("");
976 f << entry.type() << "Data[" << n << "]:";
977
978 ascii().addPos(pos);
979 ascii().addNote(f.str().c_str());
980 }
981
982 entry.setParsed(true);
983 ascii().addPos(entry.end());
984 ascii().addNote("_");
985 return true;
986 }
987
988 bool WriteNowParser::checkIfPositionValid(long pos)
989 {
990 if (pos <= m_state->m_endPos)
991 return true;
992 MWAWInputStreamPtr input = getInput();
993 long actPos = input->tell();
994 input->seek(pos, librevenge::RVNG_SEEK_SET);
995 bool ok = long(input->tell())==pos;
996 if (ok) m_state->m_endPos = pos;
997
998 input->seek(actPos, librevenge::RVNG_SEEK_SET);
999 return ok;
1000 }
1001
1002 WriteNowEntry WriteNowParser::readEntry()
1003 {
1004 WriteNowEntry res;
1005 MWAWInputStreamPtr input = getInput();
1006 auto val = static_cast<int>(input->readULong(2));
1007 res.m_fileType = (val >> 12);
1008 res.m_val[0]= val & 0x0FFF;
1009 res.m_val[1]= static_cast<int>(input->readLong(2));
1010 if (res.isZoneType()) {
1011 res.setBegin(long(input->readULong(4)));
1012 res.setLength(long(input->readULong(4)));
1013 if (!checkIfPositionValid(res.end())) {
1014 MWAW_DEBUG_MSG(("WriteNowParser::readEntry: find an invalid entry\n"));
1015 res.setLength(0);
1016 }
1017 }
1018 else {
1019 res.m_val[2]= static_cast<int>(input->readLong(4));
1020 res.m_val[3]= static_cast<int>(input->readLong(4));
1021 }
1022 return res;
1023 }
1024
1025 ////////////////////////////////////////////////////////////
1026 // read the header
1027 ////////////////////////////////////////////////////////////
1028 bool WriteNowParser::checkHeader(MWAWHeader *header, bool strict)
1029 {
1030 *m_state = WriteNowParserInternal::State();
1031
1032 MWAWInputStreamPtr input = getInput();
1033 if (!input || !input->hasDataFork())
1034 return false;
1035
1036 libmwaw::DebugStream f;
1037 int headerSize=28;
1038 input->seek(headerSize,librevenge::RVNG_SEEK_SET);
1039 if (int(input->tell()) != headerSize) {
1040 MWAW_DEBUG_MSG(("WriteNowParser::checkHeader: file is too short\n"));
1041 return false;
1042 }
1043 input->seek(0, librevenge::RVNG_SEEK_SET);
1044 auto val = long(input->readULong(4));
1045 int vers = 0;
1046 switch (val) {
1047 case 0:
1048 if (input->readULong(4) != 0)
1049 return false;
1050 vers = 2;
1051 break;
1052 case 0x57726974: // Writ
1053 if (input->readULong(4) != 0x654e6f77) // eNow
1054 return false;
1055 vers = 3;
1056 break;
1057 default:
1058 return false;
1059 }
1060 setVersion(vers);
1061 f << "FileHeader:";
1062
1063 if (vers < 3) {
1064 if (strict) {
1065 for (int i=0; i < 4; ++i) {
1066 val = long(input->readLong(1));
1067 if (val!=4 && val!=0x44) return false;
1068 input->seek(3, librevenge::RVNG_SEEK_CUR);
1069 }
1070 input->seek(8, librevenge::RVNG_SEEK_SET);
1071 }
1072
1073 ascii().addPos(0);
1074 ascii().addNote(f.str().c_str());
1075 ascii().addPos(input->tell());
1076
1077 return true;
1078 }
1079
1080 val = long(input->readULong(2));
1081 if (strict && val > 3)
1082 return false;
1083 #ifndef DEBUG
1084 if (val != 2) return false;
1085 #endif
1086 f << "f0=" << val << ",";
1087
1088 for (int i = 1; i < 4; i++) {
1089 // all zero, excepted f1=1 in one file...
1090 val = input->readLong(2);
1091 if (val) f << "f" << i << "=" << val << ",";
1092 }
1093
1094 // a flag ??
1095 val = long(input->readULong(2));
1096 if (val != 0x4000) f << "fl=" << std::hex << val << std::dec << ",";
1097 val = long(input->readLong(2));
1098 if (val) f << "f4=" << val << ",";
1099
1100 // last entry
1101 WriteNowEntry entry;
1102 entry.setBegin(long(input->readULong(4)));
1103 entry.setLength(long(input->readULong(4)));
1104 entry.m_fileType = 4;
1105
1106 if (!checkIfPositionValid(entry.end())) {
1107 MWAW_DEBUG_MSG(("WriteNowParser::checkHeader: can not find final zone\n"));
1108 return false;
1109 }
1110 entry.setType("DocEntries");
1111 m_entryManager->add(entry);
1112
1113 f << "entry=" << std::hex << entry.begin() << ":" << entry.end() << std::dec << ",";
1114
1115 // ok, we can finish initialization
1116 if (header)
1117 header->reset(MWAWDocument::MWAW_T_WRITENOW, version());
1118
1119 //
1120 input->seek(headerSize, librevenge::RVNG_SEEK_SET);
1121
1122 ascii().addPos(0);
1123 ascii().addNote(f.str().c_str());
1124 ascii().addPos(headerSize);
1125
1126 return true;
1127 }
1128
1129 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: