Debian Codegraph @

src/lib/MarinerWrtParser.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 <string.h>
35
36 #include <iomanip>
37 #include <iostream>
38 #include <limits>
39 #include <map>
40 #include <set>
41 #include <sstream>
42 #include <string>
43
44 #include <librevenge/librevenge.h>
45
46 #include "MWAWCell.hxx"
47 #include "MWAWTextListener.hxx"
48 #include "MWAWFont.hxx"
49 #include "MWAWFontConverter.hxx"
50 #include "MWAWHeader.hxx"
51 #include "MWAWList.hxx"
52 #include "MWAWParagraph.hxx"
53 #include "MWAWPosition.hxx"
54 #include "MWAWPictMac.hxx"
55 #include "MWAWPrinter.hxx"
56 #include "MWAWSection.hxx"
57 #include "MWAWSubDocument.hxx"
58
59 #include "MarinerWrtGraph.hxx"
60 #include "MarinerWrtText.hxx"
61
62 #include "MarinerWrtParser.hxx"
63
64 /** Internal: the structures of a MarinerWrtParser */
65 namespace MarinerWrtParserInternal
66 {
67 ////////////////////////////////////////
68 //! Internal: the struct used to store the zone of a MarinerWrtParser
69 struct Zone {
70 //! a enum to define the diffent zone type
71 enum Type { Z_Main, Z_Footnote, Z_Header, Z_Footer, Z_Unknown };
72 //! constructor
73 Zone()
74 : m_id(-1)
75 , m_fileId(0)
76 , m_type(Z_Unknown)
77 , m_endNote(false)
78 , m_height(0)
79 , m_RBpos(0,0)
80 , m_dim()
81 , m_pageDim()
82 , m_pageTextDim()
83 , m_section()
84 , m_backgroundColor(MWAWColor::white())
85 , m_extra("")
86 {
87 }
88 //! operator<<
89 friend std::ostream &operator<<(std::ostream &o, Zone const &zone);
90 //! the zone id
91 int m_id;
92 //! the file zone id
93 uint32_t m_fileId;
94 //! the zone type
95 Type m_type;
96 //! a flag to know if this an endnote
97 bool m_endNote;
98 //! height of the zone
99 long m_height;
100 //! right/bottom position
101 MWAWVec2l m_RBpos;
102 //! the zone total position
103 MWAWBox2l m_dim;
104 //! the page dimension (?)
105 MWAWBox2i m_pageDim;
106 //! the zone of text dimension ( ie page less margins)
107 MWAWBox2i m_pageTextDim;
108 //! the section
109 MWAWSection m_section;
110 //! the background color
111 MWAWColor m_backgroundColor;
112 //! extra data
113 std::string m_extra;
114 };
115
116 std::ostream &operator<<(std::ostream &o, Zone const &zone)
117 {
118 switch (zone.m_type) {
119 case Zone::Z_Main:
120 o << "main,";
121 break;
122 case Zone::Z_Footnote:
123 if (zone.m_endNote)
124 o << "endnote,";
125 else
126 o << "footnote,";
127 break;
128 case Zone::Z_Header:
129 o << "header,";
130 break;
131 case Zone::Z_Footer:
132 o << "footer,";
133 break;
134 case Zone::Z_Unknown:
135 #if !defined(__clang__)
136 default:
137 #endif
138 break;
139 }
140 if (zone.m_type==Zone::Z_Header || zone.m_type==Zone::Z_Footer) {
141 switch (zone.m_fileId) {
142 case 0:
143 break; // main
144 case 1:
145 o << "left,";
146 break;
147 case 2:
148 o << "right,";
149 break;
150 case 3:
151 o << "firstpage,";
152 break;
153 default:
154 o << "#fileId" << zone.m_fileId << ",";
155 break;
156 }
157 }
158 else if (zone.m_fileId & 0xFFFFFF)
159 o << "fileId=" << std::hex << (zone.m_fileId&0xFFFFFF) << std::dec << ",";
160 if (zone.m_RBpos[0] || zone.m_RBpos[1])
161 o << "RBpos=" << zone.m_RBpos << ",";
162 if (zone.m_height)
163 o << "height=" << zone.m_height << ",";
164 if (zone.m_dim.size()[0] || zone.m_dim.size()[1])
165 o << "dim=" << zone.m_dim << ",";
166 if (!zone.m_backgroundColor.isWhite())
167 o << "background=" << zone.m_backgroundColor << ",";
168 o << zone.m_extra;
169 return o;
170 }
171 ////////////////////////////////////////
172 //! Internal: the state of a MarinerWrtParser
173 struct State {
174 //! constructor
175 State()
176 : m_zonesList()
177 , m_fileToZoneMap()
178 , m_actPage(0)
179 , m_numPages(0)
180 , m_firstPageFooter(false)
181 , m_hasOddEvenHeaderFooter(false)
182 {
183 }
184
185 //! the list of zone
186 std::vector<Zone> m_zonesList;
187 //! a map fileZoneId -> localZoneId
188 std::map<uint32_t,int> m_fileToZoneMap;
189 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
190 /** a flag to know if we have a first page footer */
191 bool m_firstPageFooter;
192 /** a flag to know if we have odd/even header footer */
193 bool m_hasOddEvenHeaderFooter;
194 };
195
196 ////////////////////////////////////////
197 //! Internal: the subdocument of a MarinerWrtParser
198 class SubDocument final : public MWAWSubDocument
199 {
200 public:
201 SubDocument(MarinerWrtParser &pars, MWAWInputStreamPtr const &input, int zoneId)
202 : MWAWSubDocument(&pars, input, MWAWEntry())
203 , m_id(zoneId)
204 {
205 }
206
207 //! destructor
208 ~SubDocument() final {}
209
210 //! operator!=
211 bool operator!=(MWAWSubDocument const &doc) const final
212 {
213 if (MWAWSubDocument::operator!=(doc)) return true;
214 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
215 if (!sDoc) return true;
216 if (m_id != sDoc->m_id) return true;
217 return false;
218 }
219
220 //! the parser function
221 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
222
223 protected:
224 //! the subdocument id
225 int m_id;
226 };
227
228 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
229 {
230 if (!listener.get()) {
231 MWAW_DEBUG_MSG(("MarinerWrtParserInternal::SubDocument::parse: no listener\n"));
232 return;
233 }
234 auto *parser=dynamic_cast<MarinerWrtParser *>(m_parser);
235 if (!parser) {
236 MWAW_DEBUG_MSG(("MarinerWrtParserInternal::SubDocument::parse: no parser\n"));
237 return;
238 }
239 long pos = m_input->tell();
240 parser->sendText(m_id);
241 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
242 }
243 }
244
245 ////////////////////////////////////////////////////////////
246 // constructor/destructor + basic interface ...
247 ////////////////////////////////////////////////////////////
248 MarinerWrtParser::MarinerWrtParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
249 : MWAWTextParser(input, rsrcParser, header)
250 , m_state()
251 , m_pageMarginsSpanSet(false)
252 , m_graphParser()
253 , m_textParser()
254 {
255 init();
256 }
257
258 MarinerWrtParser::~MarinerWrtParser()
259 {
260 }
261
262 void MarinerWrtParser::init()
263 {
264 resetTextListener();
265 setAsciiName("main-1");
266
267 m_state.reset(new MarinerWrtParserInternal::State);
268
269 // reduce the margin (in case, the page is not defined)
270 getPageSpan().setMargins(0.1);
271
272 m_graphParser.reset(new MarinerWrtGraph(*this));
273 m_textParser.reset(new MarinerWrtText(*this));
274 }
275
276 ////////////////////////////////////////////////////////////
277 // position and height
278 ////////////////////////////////////////////////////////////
279 MWAWVec2f MarinerWrtParser::getPageLeftTop() const
280 {
281 return MWAWVec2f(float(getPageSpan().getMarginLeft()),
282 float(getPageSpan().getMarginTop()));
283 }
284
285 MWAWSection MarinerWrtParser::getSection(int zId) const
286 {
287 if (zId >= 0 && zId < int(m_state->m_zonesList.size()))
288 return m_state->m_zonesList[size_t(zId)].m_section;
289 return MWAWSection();
290 }
291
292 ////////////////////////////////////////////////////////////
293 // new page
294 ////////////////////////////////////////////////////////////
295 void MarinerWrtParser::newPage(int number)
296 {
297 if (number <= m_state->m_actPage || number > m_state->m_numPages)
298 return;
299
300 while (m_state->m_actPage < number) {
301 m_state->m_actPage++;
302 if (!getTextListener() || m_state->m_actPage == 1)
303 continue;
304 getTextListener()->insertBreak(MWAWTextListener::PageBreak);
305 }
306 }
307
308 ////////////////////////////////////////////////////////////
309 // interface
310 ////////////////////////////////////////////////////////////
311 int MarinerWrtParser::getZoneId(uint32_t fileId, bool &endNote)
312 {
313 if (m_state->m_fileToZoneMap.find(fileId)==m_state->m_fileToZoneMap.end()) {
314 MWAW_DEBUG_MSG(("MarinerWrtParser::getZoneId: can not find zone for %x\n", fileId));
315 return -1;
316 }
317 int id=m_state->m_fileToZoneMap.find(fileId)->second;
318 endNote=false;
319 if (id>=0 && id < int(m_state->m_zonesList.size()))
320 endNote = m_state->m_zonesList[size_t(id)].m_endNote;
321 return id;
322 }
323
324 void MarinerWrtParser::sendText(int zoneId)
325 {
326 MWAWInputStreamPtr input = getInput();
327 long actPos = input->tell();
328 m_textParser->send(zoneId);
329 input->seek(actPos, librevenge::RVNG_SEEK_SET);
330 }
331
332 float MarinerWrtParser::getPatternPercent(int id) const
333 {
334 return m_graphParser->getPatternPercent(id);
335 }
336
337 void MarinerWrtParser::sendToken(int zoneId, long tokenId)
338 {
339 MWAWInputStreamPtr input = getInput();
340 long actPos = input->tell();
341 m_graphParser->sendToken(zoneId, tokenId);
342 input->seek(actPos, librevenge::RVNG_SEEK_SET);
343 }
344
345 ////////////////////////////////////////////////////////////
346 // the parser
347 ////////////////////////////////////////////////////////////
348 void MarinerWrtParser::parse(librevenge::RVNGTextInterface *docInterface)
349 {
350 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
351 bool ok = true;
352 try {
353 // create the asciiFile
354 ascii().setStream(getInput());
355 ascii().open(asciiName());
356
357 checkHeader(nullptr);
358 ok = createZones();
359 if (ok) {
360 createDocument(docInterface);
361 m_graphParser->sendPageGraphics();
362 m_textParser->send(0);
363
364 #ifdef DEBUG
365 m_textParser->flushExtra();
366 m_graphParser->flushExtra();
367 #endif
368 }
369 ascii().reset();
370 }
371 catch (...) {
372 MWAW_DEBUG_MSG(("MarinerWrtParser::parse: exception catched when parsing\n"));
373 ok = false;
374 }
375
376 resetTextListener();
377 if (!ok) throw(libmwaw::ParseException());
378 }
379
380 ////////////////////////////////////////////////////////////
381 // create the document
382 ////////////////////////////////////////////////////////////
383 void MarinerWrtParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
384 {
385 if (!documentInterface) return;
386 if (getTextListener()) {
387 MWAW_DEBUG_MSG(("MarinerWrtParser::createDocument: listener already exist\n"));
388 return;
389 }
390
391 // update the page
392 m_state->m_actPage = 0;
393
394 int numPage = m_textParser->numPages();
395 if (m_graphParser->numPages() > numPage)
396 numPage = m_graphParser->numPages();
397 m_state->m_numPages = numPage;
398
399 // create the page list
400 MWAWPageSpan ps(getPageSpan());
401 if (m_state->m_zonesList.size())
402 ps.setBackgroundColor(m_state->m_zonesList[0].m_backgroundColor);
403
404 // look for an header/footer
405 int headerId[4] = { -1, -1, -1, -1}, footerId[4] = { -1, -1, -1, -1};
406 for (size_t z = 0; z < m_state->m_zonesList.size(); z++) {
407 auto const &zone=m_state->m_zonesList[z];
408 if (zone.m_type==MarinerWrtParserInternal::Zone::Z_Header) {
409 if (zone.m_fileId < 4)
410 headerId[zone.m_fileId]=int(z);
411 }
412 else if (zone.m_type==MarinerWrtParserInternal::Zone::Z_Footer) {
413 if (zone.m_fileId < 4)
414 footerId[zone.m_fileId]=int(z);
415 }
416 }
417 MWAWPageSpan firstPs(ps);
418 if (m_state->m_firstPageFooter) {
419 if (headerId[3]>0) {
420 MWAWHeaderFooter header(MWAWHeaderFooter::HEADER, MWAWHeaderFooter::ALL);
421 header.m_subDocument.reset(new MarinerWrtParserInternal::SubDocument(*this, getInput(), int(headerId[3])));
422 firstPs.setHeaderFooter(header);
423 }
424 if (footerId[3]>0) {
425 MWAWHeaderFooter footer(MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
426 footer.m_subDocument.reset(new MarinerWrtParserInternal::SubDocument(*this, getInput(), int(footerId[3])));
427 firstPs.setHeaderFooter(footer);
428 }
429 }
430 for (int st = 0; st < 2; st++) {
431 MWAWHeaderFooter::Occurrence what=
432 !m_state->m_hasOddEvenHeaderFooter ? MWAWHeaderFooter::ALL : st==0 ? MWAWHeaderFooter::ODD : MWAWHeaderFooter::EVEN;
433 int which= !m_state->m_hasOddEvenHeaderFooter ? 0 : 1+st;
434 if (headerId[which]>0) {
435 MWAWHeaderFooter header(MWAWHeaderFooter::HEADER, what);
436 header.m_subDocument.reset(new MarinerWrtParserInternal::SubDocument(*this, getInput(), int(headerId[which])));
437 ps.setHeaderFooter(header);
438 }
439 if (footerId[which]>0) {
440 MWAWHeaderFooter footer(MWAWHeaderFooter::FOOTER, what);
441 footer.m_subDocument.reset(new MarinerWrtParserInternal::SubDocument(*this, getInput(), int(footerId[which])));
442 ps.setHeaderFooter(footer);
443 }
444 if (!m_state->m_hasOddEvenHeaderFooter)
445 break;
446 }
447
448 std::vector<MWAWPageSpan> pageList;
449 if (m_state->m_firstPageFooter) {
450 pageList.push_back(firstPs);
451 ps.setPageSpan(m_state->m_numPages);
452 }
453 else
454 ps.setPageSpan(m_state->m_numPages+1);
455 if (ps.getPageSpan())
456 pageList.push_back(ps);
457 //
458 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
459 setTextListener(listen);
460 listen->startDocument();
461 }
462
463
464 ////////////////////////////////////////////////////////////
465 //
466 // Intermediate level
467 //
468 ////////////////////////////////////////////////////////////
469
470 // ------ read the different zones ---------
471 bool MarinerWrtParser::createZones()
472 {
473 MWAWInputStreamPtr input = getInput();
474 long pos = input->tell();
475 int actZone=-1;
476 while (readZone(actZone))
477 pos = input->tell();
478 ascii().addPos(pos);
479 ascii().addNote("Entries(Loose)");
480 return m_state->m_zonesList.size();
481 }
482
483 bool MarinerWrtParser::readZone(int &actZone, bool onlyTest)
484 {
485 MWAWInputStreamPtr input = getInput();
486 if (input->isEnd())
487 return false;
488 long pos = input->tell();
489 MarinerWrtEntry zone;
490 if (!readEntryHeader(zone)) {
491 input->seek(pos, librevenge::RVNG_SEEK_SET);
492 return false;
493 }
494
495 libmwaw::DebugStream f;
496 f << "Entries(" << zone.name() << "):" << zone;
497
498 bool done = false;
499 switch (zone.m_fileType) {
500 case -1: // separator
501 case -2: // last file
502 done = readSeparator(zone);
503 if (onlyTest)
504 break;
505 actZone++;
506 break;
507 case 0:
508 done = readZoneHeader(zone, actZone, onlyTest);
509 break;
510 case 1:
511 done = m_textParser->readTextStruct(zone, actZone);
512 break;
513 case 2:
514 done = m_textParser->readZone(zone, actZone);
515 break;
516 case 4:
517 case 5:
518 done = m_textParser->readPLCZone(zone, actZone);
519 break;
520 case 6:
521 done = m_textParser->readFonts(zone, actZone);
522 break;
523 case 7:
524 done = m_textParser->readRulers(zone, actZone);
525 break;
526 case 8:
527 done = m_textParser->readFontNames(zone, actZone);
528 break;
529 case 9: // zone size
530 done = readZoneDim(zone, actZone);
531 break;
532 case 0xa: // zone size with margins removal
533 done = readZoneDim(zone, actZone);
534 break;
535 case 0xb: // border dim?
536 done = readZoneb(zone, actZone);
537 break;
538 case 0xc:
539 done = readZonec(zone, actZone);
540 break;
541 case 0xf:
542 done = readDocInfo(zone, actZone);
543 break;
544 case 0x13:
545 done = readZone13(zone, actZone);
546 break;
547 case 0x14:
548 done = m_graphParser->readToken(zone, actZone);
549 break;
550 case 0x1a:
551 done = m_textParser->readStyleNames(zone, actZone);
552 break;
553 case 0x1f:
554 done = readPrintInfo(zone);
555 break;
556 case 0x24:
557 done = readCPRT(zone);
558 break;
559 /* 0x41a: docInfo */
560 case 0x420:
561 done = m_graphParser->readPostscript(zone, actZone);
562 break;
563 default:
564 break;
565 }
566 if (done) {
567 ascii().addPos(pos);
568 ascii().addNote(f.str().c_str());
569 input->seek(zone.end(), librevenge::RVNG_SEEK_SET);
570 return true;
571 }
572 if (onlyTest)
573 return false;
574 input->seek(zone.begin(), librevenge::RVNG_SEEK_SET);
575 input->pushLimit(zone.end());
576 std::vector<MarinerWrtStruct> dataList;
577 decodeZone(dataList);
578 input->popLimit();
579
580 size_t numData = dataList.size();
581 f << "numData=" << numData << ",";
582 ascii().addPos(pos);
583 ascii().addNote(f.str().c_str());
584
585 int numDataByField = zone.m_fileType==1 ? 22 : 10;
586 for (size_t d = 0; d < numData; d++) {
587 MarinerWrtStruct const &dt = dataList[d];
588 if ((int(d)%numDataByField)==0) {
589 if (d)
590 ascii().addNote(f.str().c_str());
591 f.str("");
592 f << zone.name() << "-" << d << ":";
593 ascii().addPos(dt.m_filePos);
594 }
595 f << dt << ",";
596 }
597 if (numData)
598 ascii().addNote(f.str().c_str());
599
600 if (input->tell() != zone.end()) {
601 f.str("");
602 if (input->tell() == zone.end()-1)
603 f << "_";
604 else
605 f << zone.name() << ":###";
606 ascii().addPos(input->tell());
607 ascii().addNote(f.str().c_str());
608 }
609 input->seek(zone.end(), librevenge::RVNG_SEEK_SET);
610 return true;
611 }
612
613 // --------- zone separator ----------
614
615 // read the zone separator
616 bool MarinerWrtParser::readSeparator(MarinerWrtEntry const &entry)
617 {
618 if (entry.length() < 0x3) {
619 MWAW_DEBUG_MSG(("MarinerWrtParser::readSeparator: data seems to short\n"));
620 return false;
621 }
622 MWAWInputStreamPtr input = getInput();
623 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
624 input->pushLimit(entry.end());
625 std::vector<MarinerWrtStruct> dataList;
626 decodeZone(dataList);
627 input->popLimit();
628
629 if (dataList.size() != 1) {
630 MWAW_DEBUG_MSG(("MarinerWrtParser::readSeparator: can find my data\n"));
631 return false;
632 }
633
634 MarinerWrtStruct const &data = dataList[0]; // always 0x77aa
635 libmwaw::DebugStream f;
636 f << entry.name() << "[data]:";
637 if (data.m_data.size() != 1 || data.m_data[0] != 0x77aa)
638 f << "#" << data;
639 ascii().addPos(entry.begin());
640 ascii().addNote(f.str().c_str());
641
642 return true;
643 }
644
645 bool MarinerWrtParser::readZoneHeader(MarinerWrtEntry const &entry, int actId, bool onlyTest)
646 {
647 if (entry.length() < 3) {
648 MWAW_DEBUG_MSG(("MarinerWrtParser::readZoneHeader: data seems to short\n"));
649 return false;
650 }
651 MWAWInputStreamPtr input = getInput();
652 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
653 input->pushLimit(entry.end());
654 std::vector<MarinerWrtStruct> dataList;
655 decodeZone(dataList);
656 input->popLimit();
657
658 size_t numData = dataList.size();
659 if (numData < 47) {
660 MWAW_DEBUG_MSG(("MarinerWrtParser::readZoneHeader: find unexpected number of data\n"));
661 return false;
662 }
663 if (onlyTest)
664 return true;
665 size_t d = 0;
666 long val;
667 libmwaw::DebugStream f;
668 MarinerWrtParserInternal::Zone zone;
669 for (int j = 0; j < 47; j++) {
670 MarinerWrtStruct const &data = dataList[d++];
671 if (!data.isBasic()) {
672 f << "###f" << j << "=" << data << ",";
673 continue;
674 }
675 long sel[4]= {0,0,0,0};
676 int dim[4]= {0,0,0,0};
677 unsigned char color[3];
678 switch (j) {
679 case 0: // version?
680 val = data.value(0);
681 if ((val>>16)==1) // checkme v1.6 -> 1[29] while v3.5->1[33]
682 setVersion((val&0xFFFF)<30 ? 1 : 2);
683 f << "vers?=" << (val>>16) << "[" << (val&0xFFFF) << "],";
684 break;
685 case 1:
686 val = data.value(0);
687 if (val>>16) // 0 or 1000
688 f << "f1[high]=" << std::hex << (val>>16) << std::dec << ",";
689 if ((val&0xFFFF)!=1)
690 f << "f1[low]=" << (val& 0xFFFF) << ",";
691 break;
692 case 2: { // find 8[01]0[0145]8120 or a0808120
693 uint32_t high = uint32_t(data.value(0))>>16;
694 // high&4: no odd/even header/footer ?
695 if (high) f << "fl2[high]=" << std::hex << high << std::dec << ",";
696 uint32_t low = uint32_t(data.value(0))&0xFFFF;
697 if (low) f << "fl2[low]=" << std::hex << low << std::dec << ",";
698 break;
699 }
700 case 3: { // 80000[01] or 800000[01]
701 auto value=uint32_t(data.value(0));
702 if (value&0x2000000) {
703 zone.m_endNote=true;
704 value &= ~uint32_t(0x2000000);
705 }
706 // value&1: firstPage[header/footer] ?
707 if (value)
708 f << "fl3=" << std::hex << value << std::dec << ",";
709 break;
710 }
711 case 4: {
712 auto v = uint32_t(data.value(0));
713 switch (v>>28) {
714 case 0: // main
715 zone.m_type = MarinerWrtParserInternal::Zone::Z_Main;
716 break;
717 case 0xc:
718 zone.m_type = MarinerWrtParserInternal::Zone::Z_Header;
719 break;
720 case 0xd:
721 zone.m_type = MarinerWrtParserInternal::Zone::Z_Footer;
722 break;
723 case 0xe:
724 zone.m_type = MarinerWrtParserInternal::Zone::Z_Footnote;
725 break;
726 default:
727 f << "#type=" << (v>>28) << ",";
728 break;
729 }
730 zone.m_fileId=(v&0xFFFFFFF);
731 m_state->m_fileToZoneMap[v]=actId;
732 break;
733 }
734 case 9: // real bottom margin
735 if (data.value(0))
736 f << "y[bottom+footer?]=" << data.value(0) << ",";
737 break;
738 case 10:
739 zone.m_height = data.value(0);
740 break;
741 case 13: // right margin
742 case 14: // bottom margin
743 zone.m_RBpos[j-13] = data.value(0);
744 break;
745 case 21:
746 if (data.value(0))
747 f << "h[act]=" << data.value(0) << ",";
748 break;
749 case 5: // 0|14|90
750 case 6: // 0|11-14
751 case 15: // -2: main?|-1
752 case 16: // -2: main?|-1
753 case 25: // 0|43
754 case 41: // 0|5|26
755 if (data.value(0))
756 f << "f" << j << "=" << data.value(0) << ",";
757 break;
758 case 7: // two units ?
759 if (data.value(0) != 0x480048)
760 f << "f" << j << "=" << std::hex << data.value(0) << std::dec << ",";
761 break;
762 case 8: // always -2?
763 if (data.value(0) != -2)
764 f << "#f" << j << "=" << data.value(0) << ",";
765 break;
766 case 12: // always 16
767 if (data.value(0) != 16)
768 f << "#f" << j << "=" << data.value(0) << ",";
769 break;
770 case 17:
771 case 18:
772 case 19:
773 case 20:
774 sel[j-17] = data.value(0);
775 while (j<20)
776 sel[++j-17] = dataList[d++].value(0);
777 if (sel[0]||sel[1])
778 f << "sel0=" << std::hex << sel[0] << "x" << sel[1] << std::dec << ",";
779 if (sel[2]||sel[3])
780 f << "sel1=" << std::hex << sel[2] << "x" << sel[3] << std::dec << ",";
781 break;
782 case 28: // left margin
783 case 29: // top margin
784 case 30:
785 case 31:
786 dim[j-28] = static_cast<int>(data.value(0));
787 while (j<31)
788 dim[++j-28] = static_cast<int>(dataList[d++].value(0));
789 zone.m_dim = MWAWBox2l(MWAWVec2l(dim[1],dim[0]), MWAWVec2l(dim[3],dim[2]));
790 break;
791 case 32:
792 case 33:
793 case 34: { // 35,36,37: front color?
794 color[0]=color[1]=color[2]=0xFF;
795 color[j-32]=static_cast<unsigned char>(data.value(0)>>8);
796 while (j < 34)
797 color[++j-32] = static_cast<unsigned char>(dataList[d++].value(0)>>8);
798 zone.m_backgroundColor = MWAWColor(color[0],color[1],color[2]);
799 break;
800 }
801 case 38:
802 if (data.value(0)!=1)
803 f << "#f" << j << "=" << data.value(0) << ",";
804 break;
805 case 39:
806 if (data.value(0) && data.value(0)!=0x4d4d4242) // MMBB: creator
807 f << "#creator=" << std::hex << uint32_t(data.value(0)) << std::dec << ",";
808 break;
809 case 40: // normally 0 or pretty small, but can also be very very big
810 if (data.value(0))
811 f << "f" << j << "=" << std::hex << data.value(0) << std::dec << ",";
812 break;
813 case 42:
814 if (data.value(0)!=43)
815 f << "#f" << j << "=" << data.value(0) << ",";
816 break;
817 case 44:
818 if (data.value(0))
819 f << "sel[pt]=" << std::hex << data.value(0) << std::dec << ",";
820 break;
821 case 45:
822 if (data.value(0)!=72)
823 f << "#f" << j << "=" << data.value(0) << ",";
824 break;
825 default:
826 if (data.value(0))
827 f << "#f" << j << "=" << data.value(0) << ",";
828 break;
829 }
830 }
831 while (d < numData) {
832 f << "#f" << d << "=" << dataList[d] << ",";
833 d++;
834 }
835 zone.m_extra = f.str();
836 if (actId < 0) {
837 MWAW_DEBUG_MSG(("MarinerWrtParser::readZoneHeader: called with negative id\n"));
838 }
839 else {
840 if (actId >= int(m_state->m_zonesList.size()))
841 m_state->m_zonesList.resize(size_t(actId)+1);
842 m_state->m_zonesList[size_t(actId)] = zone;
843 }
844 f.str("");
845 f << entry.name() << ":" << zone;
846
847 ascii().addPos(entry.begin());
848 ascii().addNote(f.str().c_str());
849 return true;
850 }
851
852 bool MarinerWrtParser::readZoneDim(MarinerWrtEntry const &entry, int zoneId)
853 {
854 if (entry.length() < entry.m_N) {
855 MWAW_DEBUG_MSG(("MarinerWrtParser::readZoneDim: data seems to short\n"));
856 return false;
857 }
858 MWAWInputStreamPtr input = getInput();
859 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
860 input->pushLimit(entry.end());
861 std::vector<MarinerWrtStruct> dataList;
862 decodeZone(dataList, 1+4*entry.m_N);
863 input->popLimit();
864
865 if (int(dataList.size()) != 4*entry.m_N) {
866 MWAW_DEBUG_MSG(("MarinerWrtParser::readZoneDim: find unexpected number of data\n"));
867 return false;
868 }
869
870 libmwaw::DebugStream f;
871 size_t d = 0;
872 std::vector<int> colPos;
873 for (int i = 0; i < entry.m_N; i++) {
874 f.str("");
875 f << entry.name() << "-" << i << ":";
876 ascii().addPos(dataList[d].m_filePos);
877
878 int dim[4] = { 0, 0, 0, 0 };
879 for (int j = 0; j < 4; j++) {
880 MarinerWrtStruct const &data = dataList[d++];
881 if (!data.isBasic()) {
882 MWAW_DEBUG_MSG(("MarinerWrtParser::readZoneDim: find unexpected dim data type\n"));
883 f << "###dim" << j << "=" << data << ",";
884 }
885 else
886 dim[j] = static_cast<int>(data.value(0));
887 }
888 // checkme
889 MWAWBox2i dimension(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2]));
890 f << "pos=" << dimension << ",";
891 bool dimOk=dim[0] >= 0 && dim[0] < dim[2] && dim[1] >= 0 && dim[1] < dim[3];
892 if (i==0 && dimOk) {
893 if (zoneId < 0 || zoneId >= int(m_state->m_zonesList.size())) {
894 MWAW_DEBUG_MSG(("MarinerWrtParser::readZoneDim: can not find the zone storage\n"));
895 }
896 else if (entry.m_fileType == 9)
897 m_state->m_zonesList[size_t(zoneId)].m_pageDim = dimension;
898 else if (entry.m_fileType == 0xa)
899 m_state->m_zonesList[size_t(zoneId)].m_pageTextDim = dimension;
900 else {
901 MWAW_DEBUG_MSG(("MarinerWrtParser::readZoneDim: unknown zone type\n"));
902 }
903 }
904 else if (i && dimOk) {
905 if (!colPos.size() || colPos.back() <= dim[1]) {
906 colPos.push_back(dim[1]);
907 colPos.push_back(dim[3]);
908 }
909 else
910 f << "###";
911 }
912 ascii().addNote(f.str().c_str());
913 }
914 if (entry.m_fileType == 0xa && zoneId >= 0 &&
915 zoneId < int(m_state->m_zonesList.size()) &&
916 colPos.size() > 2 && int(colPos.size())==2*(entry.m_N-1)) {
917 auto numCols=size_t(entry.m_N-1);
918 MWAWSection &sec=m_state->m_zonesList[size_t(zoneId)].m_section;
919 sec.m_columns.resize(numCols);
920 for (size_t c=0; c < numCols; c++) {
921 MWAWSection::Column &col = sec.m_columns[c];
922 int prevPos= c==0 ? colPos[0] : (colPos[2*c-1]+colPos[2*c])/2;
923 int nextPos= c+1==numCols ? colPos[2*c+1] :
924 (colPos[2*c+1]+colPos[2*c+2])/2;
925 col.m_width=double(nextPos-prevPos);
926 col.m_widthUnit=librevenge::RVNG_POINT;
927 col.m_margins[libmwaw::Left]=double(colPos[2*c]-prevPos)/72.;
928 col.m_margins[libmwaw::Right]=double(nextPos-colPos[2*c+1])/72.;
929 }
930 }
931 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
932
933 return true;
934 }
935
936 bool MarinerWrtParser::readDocInfo(MarinerWrtEntry const &entry, int zoneId)
937 {
938 if (entry.length() < 3) {
939 MWAW_DEBUG_MSG(("MarinerWrtParser::readDocInfo: data seems to short\n"));
940 return false;
941 }
942 MWAWInputStreamPtr input = getInput();
943 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
944 input->pushLimit(entry.end());
945 std::vector<MarinerWrtStruct> dataList;
946 decodeZone(dataList);
947 input->popLimit();
948
949 auto numDatas = int(dataList.size());
950 if (numDatas < 60) {
951 MWAW_DEBUG_MSG(("MarinerWrtParser::readDocInfo: find unexpected number of data\n"));
952 return false;
953 }
954
955 libmwaw::DebugStream f;
956 f << entry.name() << ":";
957
958 int dim[2], margins[4]= {0,0,0,0};
959 unsigned char color[3];
960 size_t d=0;
961 for (int j=0; j < numDatas; j++, d++) {
962 MarinerWrtStruct const &dt = dataList[d];
963 if (!dt.isBasic()) {
964 f << "#f" << d << "=" << dt << ",";
965 static bool first=true;
966 if (first) {
967 MWAW_DEBUG_MSG(("MarinerWrtParser::readDocInfo: find some struct block\n"));
968 first = false;
969 }
970 continue;
971 }
972 switch (j) {
973 case 0: { // 15|40
974 auto val=uint32_t(dt.value(0));
975 if (val&0x4000) {
976 if (zoneId==0)
977 m_state->m_hasOddEvenHeaderFooter=true;
978 f << "hasOddEven[header/footer],";
979 val &= ~uint32_t(0x4000);
980 }
981 if (val&0x20000) {
982 if (zoneId==0 && m_state->m_zonesList.size()) {
983 MWAWSection &sec=m_state->m_zonesList[0].m_section;
984 sec.m_columnSeparator = MWAWBorder();
985 }
986 f << "colSep,";
987 val &= ~uint32_t(0x20000);
988 }
989 if (val)
990 f << "f0=" << std::hex << val << std::dec << ",";
991 break;
992 }
993 case 2: // small number between 108 and 297
994 case 3: // f3=f2-24?
995 case 5: // small number between -18 and 57
996 case 6: // f6~=f5 ?
997 case 13: // a small number 1|2|3|4|58|61|283
998 case 35: // 1|2|31|60|283
999 case 44: // 0 or 1
1000 case 46: // 0 or 1
1001 case 47: // 0 or 1
1002 case 48: // 0 or 1
1003 case 51: // always 1
1004 if (dt.value(0))
1005 f << "f" << j << "=" << dt.value(0) << ",";
1006 break;
1007 case 1: // 1
1008 if (dt.value(0)!=1)
1009 f << "f" << j << "=" << dt.value(0) << ",";
1010 break;
1011 case 7:
1012 case 8: // a dim?
1013 dim[0] = dim[1] = 0;
1014 dim[j-7]= static_cast<int>(dt.value(0));
1015 if (j!=8)
1016 dim[++j-7] = static_cast<int>(dataList[++d].value(0));
1017 f << "dim?=" << dim[1] << "x" << dim[0] << ",";
1018 break;
1019 case 9:
1020 case 10:
1021 case 11:
1022 case 12:
1023 margins[j-9]=static_cast<int>(dt.value(0));
1024 while (j<12)
1025 margins[++j-9]= static_cast<int>(dataList[++d].value(0));
1026 f << "margins=" << margins[1] << "x" << margins[0]
1027 << "<->" << margins[3] << "x" << margins[2] << ",";
1028 break;
1029 case 14: // a very big number
1030 if (dt.value(0))
1031 f << "id?=" << std::hex << dt.value(0) << std::dec << ",";
1032 break;
1033 case 15: { // border type?
1034 long val = dt.value(0);
1035 if (!val) break;
1036 int depl = 24;
1037 f << "border?=[";
1038 for (int b = 0; b < 4; b++) {
1039 if ((val>>depl)&0xFF)
1040 f << ((val>>depl)&0xFF) << ",";
1041 else
1042 f << "_";
1043 depl -= 8;
1044 }
1045 f << "],";
1046 break;
1047 }
1048 case 16:
1049 case 17:
1050 case 18: {
1051 color[0]=color[1]=color[2]=0;
1052 color[j-16]=static_cast<unsigned char>(dt.value(0)>>8);
1053 while (j < 18)
1054 color[++j-16] = static_cast<unsigned char>(dataList[++d].value(0)>>8);
1055 MWAWColor col(color[0],color[1],color[2]);
1056 if (!col.isBlack()) f << "color=" << col << ",";
1057 break;
1058 }
1059 case 76: // a very big number
1060 case 79:
1061 if (dt.value(0))
1062 f << "f" << j << "=" << std::hex << uint32_t(dt.value(0)) << std::dec << ",";
1063 break;
1064 default:
1065 if (dt.value(0))
1066 f << "#f" << j << "=" << dt.value(0) << ",";
1067 break;
1068 }
1069 }
1070 if (zoneId==0 && margins[0] > 0 && margins[1] > 0 &&
1071 margins[2] > 0 && margins[3] > 0) {
1072 m_pageMarginsSpanSet= true;
1073 getPageSpan().setMarginTop(double(margins[0])/72.0);
1074 if (margins[2]>80)
1075 getPageSpan().setMarginBottom(double(margins[2]-40)/72.0);
1076 else
1077 getPageSpan().setMarginBottom(double(margins[2]/2)/72.0);
1078 getPageSpan().setMarginLeft(double(margins[1])/72.0);
1079 getPageSpan().setMarginRight(double(margins[3])/72.0);
1080
1081 }
1082 ascii().addPos(entry.begin());
1083 ascii().addNote(f.str().c_str());
1084 return true;
1085 }
1086 bool MarinerWrtParser::readZoneb(MarinerWrtEntry const &entry, int)
1087 {
1088 if (entry.length() < entry.m_N) {
1089 MWAW_DEBUG_MSG(("MarinerWrtParser::readZoneb: data seems to short\n"));
1090 return false;
1091 }
1092 MWAWInputStreamPtr input = getInput();
1093 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1094 input->pushLimit(entry.end());
1095 std::vector<MarinerWrtStruct> dataList;
1096 decodeZone(dataList, 1+4*entry.m_N);
1097 input->popLimit();
1098
1099 if (int(dataList.size()) != 4*entry.m_N) {
1100 MWAW_DEBUG_MSG(("MarinerWrtParser::readZoneb: find unexpected number of data\n"));
1101 return false;
1102 }
1103
1104 libmwaw::DebugStream f;
1105 size_t d = 0;
1106 for (int i = 0; i < entry.m_N; i++) {
1107 f.str("");
1108 f << entry.name() << "-" << i << ":";
1109 ascii().addPos(dataList[d].m_filePos);
1110
1111 for (int j = 0; j < 4; j++) { // always 0, 0, 0, 0?
1112 MarinerWrtStruct const &data = dataList[d++];
1113 if (!data.isBasic()) {
1114 MWAW_DEBUG_MSG(("MarinerWrtParser::readZoneb: find unexpected dim data type\n"));
1115 f << "###dim" << j << "=" << data << ",";
1116 }
1117 else if (data.value(0))
1118 f << "f" << j << "=" << data.value(0) << ",";
1119 }
1120 ascii().addNote(f.str().c_str());
1121 }
1122 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1123
1124 return true;
1125 }
1126
1127 bool MarinerWrtParser::readZonec(MarinerWrtEntry const &entry, int zoneId)
1128 {
1129 if (entry.length() < entry.m_N) {
1130 MWAW_DEBUG_MSG(("MarinerWrtParser::readZonec: data seems to short\n"));
1131 return false;
1132 }
1133 MWAWInputStreamPtr input = getInput();
1134 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1135 input->pushLimit(entry.end());
1136 std::vector<MarinerWrtStruct> dataList;
1137 decodeZone(dataList, 1+9*entry.m_N);
1138 input->popLimit();
1139
1140 if (int(dataList.size()) != 9*entry.m_N) {
1141 MWAW_DEBUG_MSG(("MarinerWrtParser::readZonec: find unexpected number of data\n"));
1142 return false;
1143 }
1144
1145 libmwaw::DebugStream f;
1146 size_t d = 0;
1147 for (int i = 0; i < entry.m_N; i++) {
1148 f.str("");
1149 f << entry.name() << "-" << i << ":";
1150 ascii().addPos(dataList[d].m_filePos);
1151
1152 for (int j = 0; j < 9; j++) {
1153 MarinerWrtStruct const &data = dataList[d++];
1154 if (!data.isBasic()) {
1155 MWAW_DEBUG_MSG(("MarinerWrtParser::readZonec: find unexpected dim data type\n"));
1156 f << "###dim" << j << "=" << data << ",";
1157 }
1158 else if (j==8) {
1159 if (!data.value(0)) {
1160 f << "firstPage[header/footer],";
1161 if (zoneId==0)
1162 m_state->m_firstPageFooter=true;
1163 }
1164 else if (data.value(0)!=1)
1165 f << "#f8=" << "=" << data.value(0) << ",";
1166 }
1167 else if (data.value(0))
1168 f << "f" << j << "=" << data.value(0) << ",";
1169 }
1170 ascii().addNote(f.str().c_str());
1171 }
1172 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1173
1174 return true;
1175 }
1176
1177 bool MarinerWrtParser::readZone13(MarinerWrtEntry const &entry, int)
1178 {
1179 if (entry.length() < 3) {
1180 MWAW_DEBUG_MSG(("MarinerWrtParser::readZone13: data seems to short\n"));
1181 return false;
1182 }
1183 MWAWInputStreamPtr input = getInput();
1184 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1185 input->pushLimit(entry.end());
1186 std::vector<MarinerWrtStruct> dataList;
1187 decodeZone(dataList, 1+23);
1188 input->popLimit();
1189
1190 if (int(dataList.size()) != 23) {
1191 MWAW_DEBUG_MSG(("MarinerWrtParser::readZone13: find unexpected number of data\n"));
1192 return false;
1193 }
1194
1195 libmwaw::DebugStream f;
1196 size_t d = 0;
1197 f << entry.name() << ":";
1198 ascii().addPos(dataList[d].m_filePos);
1199
1200 int val;
1201 for (int j = 0; j < 23; j++) {
1202 MarinerWrtStruct const &data = dataList[d++];
1203 if ((j!=14 && !data.isBasic()) || (j==14 && data.m_type)) {
1204 MWAW_DEBUG_MSG(("MarinerWrtParser::readZone13: find unexpected struct data type\n"));
1205 f << "#f" << j << "=" << data << ",";
1206 continue;
1207 }
1208 if (j < 14) {
1209 int const expectedValues[]= {13,10,9,31,8,12,14,28,29,30,31,0x7f,27,0};
1210 if (static_cast<int>(data.value(0)) != expectedValues[j])
1211 f << "f" << j << "=" << data.value(0) << ",";
1212 continue;
1213 }
1214 if (j == 14) {
1215 if (!data.m_pos.valid())
1216 f << "#f" << j << "=" << data << ",";
1217 else {
1218 /* find v_{2i}=0 and v_{2i+1}=
1219 {301,302,422,450,454,422,421,418,302,295,290,468,469,466,467,457,448}
1220 */
1221 f << "bl=[";
1222 input->seek(data.m_pos.begin(), librevenge::RVNG_SEEK_SET);
1223 auto N = int(data.m_pos.length()/2);
1224 for (int k = 0; k < N; k++) {
1225 val = static_cast<int>(input->readLong(2));
1226 if (val) f << val << ",";
1227 else f << "_,";
1228 }
1229 f << "],";
1230 }
1231 continue;
1232 }
1233 unsigned char color[3];
1234 MWAWColor col;
1235 switch (j) {
1236 case 15:
1237 case 16:
1238 case 17:
1239 color[0]=color[1]=color[2]=0xFF;
1240 color[j-15]=static_cast<unsigned char>(data.value(0)>>8);
1241 while (j < 17)
1242 color[++j-15] = static_cast<unsigned char>(dataList[d++].value(0)>>8);
1243 col = MWAWColor(color[0],color[1],color[2]);
1244 if (!col.isWhite())
1245 f << "col0=" << col << ",";
1246 break;
1247 case 19:
1248 case 20:
1249 case 21:
1250 color[0]=color[1]=color[2]=0xFF;
1251 color[j-19]=static_cast<unsigned char>(data.value(0)>>8);
1252 while (j < 21)
1253 color[++j-19] = static_cast<unsigned char>(dataList[d++].value(0)>>8);
1254 col = MWAWColor(color[0],color[1],color[2]);
1255 if (!col.isWhite())
1256 f << "col1=" << col << ",";
1257 break;
1258 default:
1259 if (data.value(0))
1260 f << "#f" << j << "=" << data.value(0) << ",";
1261 }
1262 }
1263 ascii().addNote(f.str().c_str());
1264 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1265
1266 return true;
1267 }
1268
1269 // --------- print info ----------
1270
1271 // read the print info xml data
1272 bool MarinerWrtParser::readCPRT(MarinerWrtEntry const &entry)
1273 {
1274 if (entry.length() < 0x10) {
1275 MWAW_DEBUG_MSG(("MarinerWrtParser::readCPRT: data seems to short\n"));
1276 return false;
1277 }
1278
1279 MWAWInputStreamPtr input = getInput();
1280 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1281 #ifdef DEBUG_WITH_FILES
1282 librevenge::RVNGBinaryData file;
1283 input->readDataBlock(entry.length(), file);
1284
1285 static int volatile cprtName = 0;
1286 libmwaw::DebugStream f;
1287 f << "CPRT" << ++cprtName << ".plist";
1288 libmwaw::Debug::dumpFile(file, f.str().c_str());
1289
1290 ascii().skipZone(entry.begin(),entry.end()-1);
1291 #endif
1292
1293 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1294 return true;
1295 }
1296
1297 // read the print info data
1298 bool MarinerWrtParser::readPrintInfo(MarinerWrtEntry const &entry)
1299 {
1300 if (entry.length() < 0x77) {
1301 MWAW_DEBUG_MSG(("MarinerWrtParser::readPrintInfo: data seems to short\n"));
1302 return false;
1303 }
1304
1305 MWAWInputStreamPtr input = getInput();
1306 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1307
1308 libmwaw::PrinterInfo info;
1309 if (!info.read(input))
1310 return false;
1311
1312 libmwaw::DebugStream f;
1313 f << "PrintInfo:"<< info;
1314 MWAWVec2i paperSize = info.paper().size();
1315 MWAWVec2i pageSize = info.page().size();
1316 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
1317 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
1318
1319 if (!m_pageMarginsSpanSet) {
1320 // define margin from print info
1321 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
1322 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
1323
1324 // move margin left | top
1325 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
1326 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
1327 lTopMargin -= MWAWVec2i(decalX, decalY);
1328 rBotMargin += MWAWVec2i(decalX, decalY);
1329
1330 // decrease right | bottom
1331 int rightMarg = rBotMargin.x() -50;
1332 if (rightMarg < 0) rightMarg=0;
1333 int botMarg = rBotMargin.y() -50;
1334 if (botMarg < 0) botMarg=0;
1335
1336 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
1337 getPageSpan().setMarginBottom(botMarg/72.0);
1338 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
1339 getPageSpan().setMarginRight(rightMarg/72.0);
1340 }
1341 getPageSpan().setFormLength(paperSize.y()/72.);
1342 getPageSpan().setFormWidth(paperSize.x()/72.);
1343 getPageSpan().checkMargins();
1344
1345 ascii().addPos(entry.begin());
1346 ascii().addNote(f.str().c_str());
1347
1348 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1349 return true;
1350 }
1351
1352 ////////////////////////////////////////////////////////////
1353 // low level
1354 ////////////////////////////////////////////////////////////
1355
1356 // ---- field decoder ---------
1357 bool MarinerWrtParser::readEntryHeader(MarinerWrtEntry &entry)
1358 {
1359 MWAWInputStreamPtr input = getInput();
1360 long pos = input->tell();
1361 std::vector<long> dataList;
1362 if (!readNumbersString(4,dataList)||dataList.size()<5) {
1363 MWAW_DEBUG_MSG(("MarinerWrtParser::readEntryHeader: oops can not find header entry\n"));
1364 input->seek(pos, librevenge::RVNG_SEEK_SET);
1365 return false;
1366 }
1367 long length = dataList[1] >= 0 ? (dataList[1]<<16)+dataList[2] : -1;
1368 if (length < 0 || !input->checkPosition(input->tell()+length)) {
1369 MWAW_DEBUG_MSG(("MarinerWrtParser::readEntryHeader: the header data seems to short\n"));
1370 input->seek(pos, librevenge::RVNG_SEEK_SET);
1371 return false;
1372 }
1373 entry.setBegin(input->tell());
1374 entry.setLength(length);
1375 entry.m_fileType = static_cast<int>(int16_t(dataList[0]));
1376 entry.m_N = static_cast<int>(dataList[4]);
1377 entry.m_value = static_cast<int>(dataList[3]);
1378
1379 return true;
1380 }
1381
1382 bool MarinerWrtParser::readNumbersString(int num, std::vector<long> &res)
1383 {
1384 res.resize(0);
1385 // first read the string
1386 MWAWInputStreamPtr input = getInput();
1387 std::string str("");
1388 while (!input->isEnd()) {
1389 auto ch = int(input->readULong(1));
1390 if (ch=='-' || (ch >= 'A' && ch <= 'F') || (ch >= '0' && ch <= '9')) {
1391 str += char(ch);
1392 continue;
1393 }
1394 input->seek(-1, librevenge::RVNG_SEEK_CUR);
1395 break;
1396 }
1397 if (!str.length()) return false;
1398
1399 // ok we have the string, let decodes it
1400 size_t sz = str.length(), i = sz;
1401 int nBytes = 0;
1402 long val=0;
1403 while (1) {
1404 if (i==0) {
1405 if (nBytes)
1406 res.insert(res.begin(),val);
1407 break;
1408 }
1409 char c = str[--i];
1410 if (c=='-') {
1411 if (!nBytes) {
1412 MWAW_DEBUG_MSG(("MarinerWrtParser::readNumbersString find '-' with no val\n"));
1413 break;
1414 }
1415 res.insert(res.begin(),-val);
1416 val = 0;
1417 nBytes = 0;
1418 continue;
1419 }
1420
1421 if (nBytes==num) {
1422 res.insert(res.begin(),val);
1423 val = 0;
1424 nBytes = 0;
1425 }
1426
1427 if (c >= '0' && c <= '9')
1428 val += (long(c-'0')<<(4*nBytes));
1429 else if (c >= 'A' && c <= 'F')
1430 val += (long(c+10-'A')<<(4*nBytes));
1431 else {
1432 MWAW_DEBUG_MSG(("MarinerWrtParser::readNumbersString find odd char %x\n", static_cast<unsigned int>(c)));
1433 break;
1434 }
1435 nBytes++;
1436 }
1437 return true;
1438 }
1439
1440 bool MarinerWrtParser::decodeZone(std::vector<MarinerWrtStruct> &dataList, long numData)
1441 {
1442 dataList.clear();
1443
1444 MWAWInputStreamPtr input = getInput();
1445 long pos = input->tell();
1446 while (!input->isEnd()) {
1447 size_t numVal = dataList.size();
1448 if (numVal >= size_t(numData))
1449 break;
1450 MarinerWrtStruct data;
1451 data.m_filePos = pos;
1452 auto type = int(input->readULong(1));
1453 data.m_type = (type&3);
1454 if (type == 3)
1455 return true;
1456 if ((type & 0x3c) || (type && !(type&0x3)))
1457 break;
1458 if ((type>>4)==0xc) {
1459 if (input->isEnd()) break;
1460 auto num = int(input->readULong(1));
1461 if (!num) break;
1462 if (numVal==0) {
1463 MWAW_DEBUG_MSG(("MarinerWrtParser::decodeZone: no previous data to copy\n"));
1464 }
1465 else // checkme
1466 data = dataList[numVal-1];
1467
1468 for (int j = 0; j < num; j++)
1469 dataList.push_back(data);
1470 pos = input->tell();
1471 continue;
1472 }
1473 if ((type>>4)==0x8) {
1474 if (numVal==0) {
1475 MWAW_DEBUG_MSG(("MarinerWrtParser::decodeZone: no previous data to copy(II)\n"));
1476 dataList.push_back(data);
1477 }
1478 else
1479 dataList.push_back(dataList[numVal-1]);
1480 pos = input->tell();
1481 continue;
1482 }
1483 std::vector<long> &numbers = data.m_data;
1484 if (!readNumbersString(data.m_type==1 ? 4: 8, numbers))
1485 break;
1486 if (type==0) {
1487 if (numbers.size() != 1 || numbers[0] < 0 || input->readULong(1) != 0x2c)
1488 break;
1489 data.m_pos.setBegin(input->tell());
1490 data.m_pos.setLength(numbers[0]);
1491 if (!input->checkPosition(data.m_pos.end()))
1492 break;
1493 input->seek(data.m_pos.end(), librevenge::RVNG_SEEK_SET);
1494 numbers.resize(0);
1495 }
1496
1497 dataList.push_back(data);
1498 pos = input->tell();
1499 }
1500 input->seek(pos, librevenge::RVNG_SEEK_SET);
1501 return dataList.size();
1502 }
1503
1504 ////////////////////////////////////////////////////////////
1505 // read the header
1506 ////////////////////////////////////////////////////////////
1507 bool MarinerWrtParser::checkHeader(MWAWHeader *header, bool strict)
1508 {
1509 *m_state = MarinerWrtParserInternal::State();
1510 MWAWInputStreamPtr input = getInput();
1511 if (!input || !input->hasDataFork())
1512 return false;
1513
1514 long const headerSize=0x2e;
1515 if (!input->checkPosition(headerSize)) {
1516 MWAW_DEBUG_MSG(("MarinerWrtParser::checkHeader: file is too short\n"));
1517 return false;
1518 }
1519 input->seek(0,librevenge::RVNG_SEEK_SET);
1520
1521 int actZone = -1;
1522 if (!readZone(actZone, true))
1523 return false;
1524 if (strict && !readZone(actZone, true))
1525 return false;
1526
1527 input->seek(0, librevenge::RVNG_SEEK_SET);
1528 if (header)
1529 header->reset(MWAWDocument::MWAW_T_MARINERWRITE, 1);
1530
1531 return true;
1532 }
1533
1534 ////////////////////////////////////////////////////////////
1535 // MarinerWrtEntry/MarinerWrtStruct function
1536 ////////////////////////////////////////////////////////////
1537 MarinerWrtEntry::~MarinerWrtEntry()
1538 {
1539 }
1540
1541 std::string MarinerWrtEntry::name() const
1542 {
1543 switch (m_fileType) {
1544 case -1:
1545 return "Separator";
1546 case -2:
1547 return "EndZone";
1548 case 0:
1549 return "ZoneHeader";
1550 case 1:
1551 return "TextStruct";
1552 case 2:
1553 return "TEXT";
1554 case 4:
1555 return "CharPLC";
1556 case 5:
1557 return "ParagPLC";
1558 case 6:
1559 return "Fonts";
1560 case 7:
1561 return "Paragraphs";
1562 case 8:
1563 return "FontNames";
1564 case 9:
1565 return "PaperSize";
1566 case 0xa:
1567 return "ColDim";
1568 case 0xf:
1569 return "DocInfo";
1570 case 0x14: // token, picture, ...
1571 return "Token";
1572 case 0x1a:
1573 return "StyleNames";
1574 case 0x1f:
1575 return "PrintInfo";
1576 case 0x24:
1577 return "CPRT";
1578 case 0x41a:
1579 return "DocInf2";
1580 case 0x420:
1581 return "PSFile";
1582 default:
1583 break;
1584 }
1585 std::stringstream s;
1586 if (m_fileType >= 0)
1587 s << "Zone" << std::hex << std::setfill('0') << std::setw(2) << m_fileType << std::dec;
1588 else
1589 s << "Zone-" << std::hex << std::setfill('0') << std::setw(2) << -m_fileType << std::dec;
1590
1591 return s.str();
1592 }
1593
1594 long MarinerWrtStruct::value(int i) const
1595 {
1596 if (i < 0 || i >= int(m_data.size())) {
1597 if (i) {
1598 MWAW_DEBUG_MSG(("MarinerWrtStruct::value: can not find value %d\n", i));
1599 }
1600 return 0;
1601 }
1602 return m_data[size_t(i)];
1603 }
1604 std::ostream &operator<<(std::ostream &o, MarinerWrtStruct const &dt)
1605 {
1606 switch (dt.m_type) {
1607 case 0: // data
1608 o << "sz=" << std::hex << dt.m_pos.length() << std::dec;
1609 return o;
1610 case 3: // end of data
1611 return o;
1612 case 1: // int?
1613 case 2: // long?
1614 break;
1615 default:
1616 if (dt.m_type) o << ":" << dt.m_type;
1617 break;
1618 }
1619 size_t numData = dt.m_data.size();
1620 if (!numData) {
1621 o << "_";
1622 return o;
1623 }
1624 if (numData > 1) o << "[";
1625 for (size_t d = 0; d < numData; d++) {
1626 long val = dt.m_data[d];
1627 if (val > -100 && val < 100)
1628 o << val;
1629 else if (val > 0)
1630 o << "0x" << std::hex << val << std::dec;
1631 else
1632 o << "-0x" << std::hex << -val << std::dec;
1633 if (d+1 != numData) o << ",";
1634 }
1635 if (numData > 1) o << "]";
1636 return o;
1637 }
1638
1639
1640 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: