Debian Codegraph @

src/lib/MacWrtProParser.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 <algorithm>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <map>
39 #include <memory>
40 #include <set>
41 #include <sstream>
42
43 #include <librevenge/librevenge.h>
44
45 #include "MWAWCell.hxx"
46 #include "MWAWFontConverter.hxx"
47 #include "MWAWHeader.hxx"
48 #include "MWAWPosition.hxx"
49 #include "MWAWPictMac.hxx"
50 #include "MWAWPrinter.hxx"
51 #include "MWAWStream.hxx"
52 #include "MWAWStringStream.hxx"
53 #include "MWAWSubDocument.hxx"
54 #include "MWAWTextListener.hxx"
55
56 #include "MacWrtProStructures.hxx"
57
58 #include "MacWrtProParser.hxx"
59
60 /** Internal: the structures of a MacWrtProParser */
61 namespace MacWrtProParserInternal
62 {
63 ////////////////////////////////////////
64 //! Internal: a struct used to store a zone
65 struct Zone {
66 Zone()
67 : m_type(-1)
68 , m_blockId(0)
69 , m_stream()
70 , m_parsed(false)
71 {
72 }
73 ~Zone()
74 {
75 }
76
77 //! the type : 0(text), 1(graphic)
78 int m_type;
79
80 //! the first block id
81 int m_blockId;
82
83 //! the storage
84 std::shared_ptr<MWAWStream> m_stream;
85
86 //! true if the zone is sended
87 bool m_parsed;
88 };
89
90 //! Internal: a struct used to store a text zone
91 struct TextZoneData {
92 TextZoneData()
93 : m_type(-1)
94 , m_length(0)
95 , m_id(0)
96 {
97 }
98 friend std::ostream &operator<<(std::ostream &o, TextZoneData const &tData)
99 {
100 switch (tData.m_type) {
101 case 0:
102 o << "C" << tData.m_id << ",";
103 break;
104 case 1:
105 o << "P" << tData.m_id << ",";
106 break;
107 default:
108 o << "type=" << tData.m_type << ",id=" << tData.m_id << ",";
109 break;
110 }
111 o << "nC=" << tData.m_length << ",";
112 return o;
113 }
114 //! the type
115 int m_type;
116 //! the text length
117 int m_length;
118 //! an id
119 int m_id;
120 };
121
122 //! Internal: a struct used to store a text zone
123 struct Token {
124 Token()
125 : m_type(-1)
126 , m_length(0)
127 , m_blockId(-1)
128 , m_box()
129 {
130 for (auto &fl : m_flags) fl = 0;
131 }
132 //! operator<<
133 friend std::ostream &operator<<(std::ostream &o, Token const &tkn)
134 {
135 o << "nC=" << tkn.m_length << ",";
136 switch (tkn.m_type) {
137 case 1:
138 o << "pagenumber,";
139 break;
140 case 2:
141 o << "footnote(pos),";
142 break;
143 case 3:
144 o << "footnote(content),";
145 break;
146 case 4:
147 o << "figure,";
148 break;
149 case 5:
150 o << "hyphen,";
151 break;
152 case 6:
153 o << "date,";
154 break;
155 case 7:
156 o << "time,";
157 break;
158 case 8:
159 o << "title,";
160 break;
161 case 9:
162 o << "revision,";
163 break;
164 case 10:
165 o << "sectionnumber,";
166 break;
167 default:
168 o << "#type=" << tkn.m_type << ",";
169 }
170 if (tkn.m_blockId >= 0) o << "blockId=" << tkn.m_blockId << ",";
171 for (int i = 0; i < 4; i++) {
172 if (tkn.m_flags[i]) o << "fl" << i << "=" << std::hex << tkn.m_flags[i] << ",";
173 }
174 return o;
175 }
176 //! the type
177 int m_type;
178 //! the text length
179 int m_length;
180 //! the block id
181 int m_blockId;
182 //! the bdbox ( filled in MWII for figure)
183 MWAWBox2f m_box;
184 //! some flags
185 unsigned int m_flags[4];
186 };
187
188 //! Internal: a struct used to store a text zone
189 struct TextZone {
190 TextZone()
191 : m_textLength(0)
192 , m_entries()
193 , m_tokens()
194 , m_parsed(false)
195 {
196 }
197
198 //! the text length
199 int m_textLength;
200
201 //! the list of entries
202 std::vector<MWAWEntry> m_entries;
203
204 //! two vector list of id ( charIds, paragraphIds)
205 std::vector<TextZoneData> m_ids[2];
206
207 //! the tokens list
208 std::vector<Token> m_tokens;
209
210 //! true if the zone is sended
211 bool m_parsed;
212 };
213
214
215 ////////////////////////////////////////
216 //! Internal: the state of a MacWrtProParser
217 struct State {
218 //! constructor
219 State()
220 : m_parsedBlocks()
221 , m_dataMap()
222 , m_textMap()
223 , m_graphicIdsCallByTokens()
224 , m_fileNumPages(0)
225 , m_col(1)
226 , m_colSeparator(0.16667)
227 , m_actPage(0)
228 , m_numPages(0)
229 , m_hasTitlePage(false)
230 {
231 }
232
233 //! the list of retrieved block : block
234 std::set<int> m_parsedBlocks;
235
236 //! the list of blockId->data zone
237 std::map<int, std::shared_ptr<Zone> > m_dataMap;
238
239 //! the list of blockId->text zone
240 std::map<int, std::shared_ptr<TextZone> > m_textMap;
241
242 //! the list of graphicId called by tokens
243 std::vector<int> m_graphicIdsCallByTokens;
244
245 int m_fileNumPages /** the number of page in MWII */;
246 int m_col /** the number of columns in MWII */;
247 double m_colSeparator /** the columns separator in inch MWII */;
248 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
249 bool m_hasTitlePage /** flag to know if we have a title page */;
250 };
251
252 ////////////////////////////////////////
253 //! Internal: the subdocument of a MacWrtProParser
254 class SubDocument final : public MWAWSubDocument
255 {
256 public:
257 SubDocument(MacWrtProParser &pars, MWAWInputStreamPtr const &input, int zoneId)
258 : MWAWSubDocument(&pars, input, MWAWEntry())
259 , m_id(zoneId)
260 {
261 }
262
263 //! destructor
264 ~SubDocument() final {}
265
266 //! operator!=
267 bool operator!=(MWAWSubDocument const &doc) const final;
268
269 //! the parser function
270 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
271
272 protected:
273 //! the subdocument id
274 int m_id;
275 };
276
277 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
278 {
279 if (m_id == -3) return; // empty block
280 if (!listener.get()) {
281 MWAW_DEBUG_MSG(("MacWrtProParserInternal::SubDocument::parse: no listener\n"));
282 return;
283 }
284 auto *parser = dynamic_cast<MacWrtProParser *>(m_parser);
285 if (!parser) {
286 MWAW_DEBUG_MSG(("MacWrtProParserInternal::SubDocument::parse: no parser\n"));
287 return;
288 }
289
290 long pos = m_input->tell();
291 if (parser->m_structures.get())
292 parser->m_structures->send(m_id);
293 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
294 }
295
296 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
297 {
298 if (MWAWSubDocument::operator!=(doc)) return true;
299 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
300 if (!sDoc) return true;
301 if (m_id != sDoc->m_id) return true;
302 return false;
303 }
304 }
305
306
307 ////////////////////////////////////////////////////////////
308 // constructor/destructor, ...
309 ////////////////////////////////////////////////////////////
310 MacWrtProParser::MacWrtProParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
311 : MWAWTextParser(input, rsrcParser, header)
312 , m_state()
313 , m_structures()
314 {
315 init();
316 }
317
318 MacWrtProParser::~MacWrtProParser()
319 {
320 }
321
322 void MacWrtProParser::init()
323 {
324 resetTextListener();
325 setAsciiName("main-1");
326
327 m_state.reset(new MacWrtProParserInternal::State);
328 m_structures.reset(new MacWrtProStructures(*this));
329
330 // reduce the margin (in case, the page is not defined)
331 getPageSpan().setMargins(0.1);
332 }
333
334 ////////////////////////////////////////////////////////////
335 // position and height
336 ////////////////////////////////////////////////////////////
337 int MacWrtProParser::numColumns(double &sep) const
338 {
339 sep=m_state->m_colSeparator;
340 if (m_state->m_col <= 1) return 1;
341 return m_state->m_col;
342 }
343
344 ////////////////////////////////////////////////////////////
345 // new page
346 ////////////////////////////////////////////////////////////
347 void MacWrtProParser::newPage(int number, bool softBreak)
348 {
349 if (number <= m_state->m_actPage) return;
350 if (number > m_state->m_numPages) {
351 MWAW_DEBUG_MSG(("MacWrtProParser::newPage: can not create new page\n"));
352 return;
353 }
354
355 while (m_state->m_actPage < number) {
356 m_state->m_actPage++;
357 if (!getTextListener() || m_state->m_actPage == 1)
358 continue;
359 if (softBreak)
360 getTextListener()->insertBreak(MWAWTextListener::SoftPageBreak);
361 else
362 getTextListener()->insertBreak(MWAWTextListener::PageBreak);
363 }
364 }
365
366 bool MacWrtProParser::hasTitlePage() const
367 {
368 return m_state->m_hasTitlePage;
369 }
370
371 std::vector<int> const &MacWrtProParser::getGraphicIdCalledByToken() const
372 {
373 return m_state->m_graphicIdsCallByTokens;
374 }
375
376 std::shared_ptr<MWAWSubDocument> MacWrtProParser::getSubDocument(int blockId)
377 {
378 return std::make_shared<MacWrtProParserInternal::SubDocument>(*this, getInput(), blockId);
379 }
380
381 ////////////////////////////////////////////////////////////
382 // the parser
383 ////////////////////////////////////////////////////////////
384 void MacWrtProParser::parse(librevenge::RVNGTextInterface *docInterface)
385 {
386 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
387 bool ok = true;
388 try {
389 m_state->m_parsedBlocks.clear();
390
391 // create the asciiFile
392 ascii().setStream(getInput());
393 ascii().open(asciiName());
394
395 checkHeader(nullptr);
396
397 ok = createZones();
398 if (ok) {
399 createDocument(docInterface);
400 if (m_structures) {
401 m_structures->sendMainZone();
402 m_structures->flushExtra();
403 }
404 }
405
406 #ifdef DEBUG
407 if (version()>0) {
408 std::vector<int> freeList;
409 getFreeZoneList(freeList);
410 for (auto bl : freeList) {
411 ascii().addPos((bl-1)*0x100);
412 ascii().addNote("Entries(Free)");
413 }
414 }
415 checkUnparsed();
416 #endif
417
418 ascii().reset();
419 }
420 catch (...) {
421 MWAW_DEBUG_MSG(("MacWrtProParser::parse: exception catched when parsing\n"));
422 ok = false;
423 }
424
425 resetTextListener();
426 if (!ok) throw(libmwaw::ParseException());
427 }
428
429 ////////////////////////////////////////////////////////////
430 // stream (internal)
431 ////////////////////////////////////////////////////////////
432 std::shared_ptr<MWAWStream> MacWrtProParser::getStreamForBlock(int block)
433 {
434 MWAWInputStreamPtr input=getInput();
435 std::shared_ptr<MWAWStream> res;
436 int const vers=version();
437 if (block<3 || !input->checkPosition(0x100*block)) {
438 MWAW_DEBUG_MSG(("MacWrtProParser::getStreamForBlock: unexpected block=%d\n", block));
439 return res;
440 }
441 int const linkSz = vers<= 0 ? 2 : 4;
442 input->seek(0x100*block-linkSz, librevenge::RVNG_SEEK_SET);
443 if (input->readULong(linkSz)==0) {
444 m_state->m_parsedBlocks.insert(block-1);
445 input->seek(0x100*(block-1), librevenge::RVNG_SEEK_SET);
446 res.reset(new MWAWStream(input, ascii()));
447 res->m_bof=0x100*(block-1);
448 res->m_eof=0x100*block-linkSz;
449 return res;
450 }
451 int const fBlock=block;
452 int numBlock=1, decal=0;
453 std::shared_ptr<MWAWStringStream> resInput;
454 while (true) {
455 --block; // block i : (i-1)00..i00
456 if (numBlock<=0 || block<2 || unsigned(block)+unsigned(numBlock)>=std::numeric_limits<int>::max() ||
457 !input->checkPosition(0x100*unsigned(block+numBlock))) {
458 MWAW_DEBUG_MSG(("MacWrtProParser::getStreamForBlock: can not read some block %dx%d\n", block, numBlock));
459 break;
460 }
461 bool ok=true;
462 for (int bl=block; bl<block+numBlock; ++bl) {
463 if (m_state->m_parsedBlocks.find(bl)!=m_state->m_parsedBlocks.end()) {
464 MWAW_DEBUG_MSG(("MacWrtProParser::getStreamForBlock: block %d is already m_state->m_parsedBlockss\n", bl));
465 ok=false;
466 break;
467 }
468 m_state->m_parsedBlocks.insert(bl);
469 }
470 if (!ok) break;
471 ascii().skipZone(0x100*block, 0x100*(block+numBlock)-1);
472 input->seek(0x100*block+decal, librevenge::RVNG_SEEK_SET);
473 unsigned long read;
474 unsigned long sz=static_cast<unsigned long>(0x100*numBlock-linkSz-decal);
475 const unsigned char *dt = input->read(sz, read);
476 if (!dt || read != sz) {
477 MWAW_DEBUG_MSG(("MacWrtProParser::getStreamForBlock: can not read some data\n"));
478 break;
479 }
480 if (!resInput)
481 resInput.reset(new MWAWStringStream(dt, unsigned(sz)));
482 else
483 resInput->append(dt, unsigned(sz));
484 decal=0;
485 numBlock=1;
486 block=int(input->readLong(linkSz));
487 if (block==0) break;
488 if (block<0) {
489 block*=-1;
490 if (block<3 || !input->checkPosition(0x100*(block-1)+linkSz)) {
491 MWAW_DEBUG_MSG(("MacWrtProParser::getStreamForBlock: bad block %d\n", block));
492 break;
493 }
494 input->seek(0x100*(block-1), librevenge::RVNG_SEEK_SET);
495 numBlock=int(input->readULong(linkSz));
496 decal=linkSz;
497 }
498 }
499 if (!resInput) return res;
500 res.reset(new MWAWStream(std::make_shared<MWAWInputStream>(resInput, false)));
501 std::stringstream s;
502 s << "DataZone" << std::hex << fBlock << std::dec;
503 res->m_ascii.open(s.str());
504 res->m_input->seek(0, librevenge::RVNG_SEEK_SET);
505 return res;
506 }
507
508 ////////////////////////////////////////////////////////////
509 // return the chain list of block ( used to get free blocks)
510 ////////////////////////////////////////////////////////////
511 bool MacWrtProParser::getFreeZoneList(std::vector<int> &blockLists)
512 {
513 blockLists.clear();
514 MWAWInputStreamPtr input = getInput();
515 if (!input->checkPosition(0x200) || version() <= 0)
516 return false;
517 input->seek(0x200-4, librevenge::RVNG_SEEK_SET);
518 int blockId=int(input->readULong(4));
519 if (!blockId) return true;
520
521 if (blockId<2 || !input->checkPosition(blockId*0x100)) {
522 MWAW_DEBUG_MSG(("MacWrtProParser::getFreeZoneList: find a bad free block=%x\n", unsigned(blockId)));
523 return false;
524 }
525 libmwaw::DebugStream f;
526 f << "Entries(Free):";
527 input->seek((blockId-1)*0x100, librevenge::RVNG_SEEK_SET);
528 // FIXME: use (blockId)*0x100-4 to get the complete list when there is more than 63/64 free blocks
529 // Note: the different blocks seems linked together N free block -> N-1 free block -> ...
530 for (int b=0; b<63; ++b) { // checkme limit=63 or 64
531 int bId=int(input->readULong(4));
532 if (bId==0) break;
533 if (bId<2 || !input->checkPosition(bId*0x100) || m_state->m_parsedBlocks.find(bId-1)!=m_state->m_parsedBlocks.end()) {
534 MWAW_DEBUG_MSG(("MacWrtProParser::getFreeZoneList: find a bad block %x\n", unsigned(bId)));
535 f << "###" << std::hex << bId << std::dec << ",";
536 break;
537 }
538 f << std::hex << bId << std::dec << ",";
539 blockLists.push_back(bId);
540 m_state->m_parsedBlocks.insert(bId-1);
541 }
542 ascii().addPos((blockId-1)*0x100);
543 ascii().addNote(f.str().c_str());
544 if (input->tell()!=blockId*0x100) ascii().addDelimiter(input->tell(),'|');
545 return blockLists.size() != 0;
546 }
547
548 ////////////////////////////////////////////////////////////
549 // create the document
550 ////////////////////////////////////////////////////////////
551 void MacWrtProParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
552 {
553 if (!documentInterface) return;
554 if (getTextListener()) {
555 MWAW_DEBUG_MSG(("MacWrtProParser::createDocument: listener already exist\n"));
556 return;
557 }
558
559 // update the page
560 m_state->m_actPage = 0;
561 int numPages = m_structures ? m_structures->numPages() : 0;
562 if (numPages <= 0) numPages = 1;
563 m_state->m_numPages = numPages;
564
565 // create the page list
566 std::vector<MWAWPageSpan> pageList;
567 for (int i = 0; i < m_state->m_numPages;) {
568 MWAWPageSpan ps(getPageSpan());
569 if (m_structures)
570 m_structures->updatePageSpan(i, m_state->m_hasTitlePage, ps);
571 pageList.push_back(ps);
572 i+=std::max<int>(1,ps.getPageSpan());
573 }
574
575 //
576 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
577 setTextListener(listen);
578 listen->startDocument();
579 }
580
581
582 ////////////////////////////////////////////////////////////
583 //
584 // Intermediate level
585 //
586 ////////////////////////////////////////////////////////////
587 bool MacWrtProParser::createZones()
588 {
589 MWAWInputStreamPtr input = getInput();
590 long pos = input->tell();
591
592 if (!readPrintInfo()) {
593 // can happens in some valid file
594 ascii().addPos(pos);
595 ascii().addNote("Entries(PrintInfo):###");
596 input->seek(pos+0x78, librevenge::RVNG_SEEK_SET);
597 }
598
599 pos = input->tell();
600 if (!readDocHeader()) {
601 ascii().addPos(pos);
602 ascii().addNote("##Entries(Data0)");
603 }
604
605 // ok now ask the structure manager to retrieve its data
606 auto stream=getStreamForBlock(3);
607 if (!stream)
608 return false;
609 return m_structures->createZones(stream, m_state->m_fileNumPages);
610 }
611
612 ////////////////////////////////////////////////////////////
613 //
614 // Low level
615 //
616 ////////////////////////////////////////////////////////////
617
618 ////////////////////////////////////////////////////////////
619 // read the header
620 ////////////////////////////////////////////////////////////
621 bool MacWrtProParser::checkHeader(MWAWHeader *header, bool strict)
622 {
623 *m_state = MacWrtProParserInternal::State();
624
625 MWAWInputStreamPtr input = getInput();
626 if (!input || !input->hasDataFork())
627 return false;
628
629 libmwaw::DebugStream f;
630 int const headerSize=4;
631 if (!input->checkPosition(0x300)) {
632 MWAW_DEBUG_MSG(("MacWrtProParser::checkHeader: file is too short\n"));
633 return false;
634 }
635 input->seek(0,librevenge::RVNG_SEEK_SET);
636
637 auto vers = static_cast<int>(input->readULong(2));
638 auto val = static_cast<int>(input->readULong(2));
639
640 f << "FileHeader:";
641 switch (vers) {
642 case 0x2e:
643 vers = 0;
644 if (val != 0x2e)
645 return false;
646 break;
647 case 4:
648 vers = 1;
649 if (val != 4) {
650 #ifdef DEBUG
651 if (strict || val < 3 || val > 5)
652 return false;
653 f << "#unk=" << val << ",";
654 #else
655 return false;
656 #endif
657 }
658 break;
659 default:
660 MWAW_DEBUG_MSG(("MacWrtProParser::checkHeader: unknown version\n"));
661 return false;
662 }
663 setVersion(vers);
664 f << "vers=" << vers << ",";
665 if (strict) {
666 if (vers) {
667 input->seek(0xdd, librevenge::RVNG_SEEK_SET);
668 // "MP" seems always in this position
669 if (input->readULong(2) != 0x4d50)
670 return false;
671 }
672 else if (!readPrintInfo()) { // last chance, check DocHeader
673 input->seek(4+0x78+2, librevenge::RVNG_SEEK_SET);
674 val=static_cast<int>(input->readULong(2));
675 if ((val&0x0280)!=0x0280) return false;
676 for (int i=0; i<4; ++i) {
677 val=static_cast<int>(input->readLong(1));
678 if (val<-1 || val>1) return false;
679 }
680 }
681 }
682
683
684 // ok, we can finish initialization
685 if (header)
686 header->reset(MWAWDocument::MWAW_T_MACWRITEPRO, version());
687
688 //
689 input->seek(headerSize, librevenge::RVNG_SEEK_SET);
690
691 ascii().addPos(0);
692 ascii().addNote(f.str().c_str());
693 ascii().addPos(headerSize);
694
695 return true;
696 }
697
698 ////////////////////////////////////////////////////////////
699 // read the print info
700 ////////////////////////////////////////////////////////////
701 bool MacWrtProParser::readPrintInfo()
702 {
703 MWAWInputStreamPtr input = getInput();
704 long pos = input->tell();
705 libmwaw::DebugStream f;
706 // print info
707 libmwaw::PrinterInfo info;
708 if (!info.read(input)) return false;
709 f << "Entries(PrintInfo):"<< info;
710
711 MWAWVec2i paperSize = info.paper().size();
712 MWAWVec2i pageSize = info.page().size();
713 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
714 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
715
716 // define margin from print info
717 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
718 MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1);
719
720 // move margin left | top
721 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
722 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
723 lTopMargin -= MWAWVec2i(decalX, decalY);
724 rBotMargin += MWAWVec2i(decalX, decalY);
725
726 // decrease right | bottom
727 int rightMarg = rBotMargin.x() -10;
728 if (rightMarg < 0) rightMarg=0;
729 int botMarg = rBotMargin.y() -10;
730 if (botMarg < 0) botMarg=0;
731
732 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
733 getPageSpan().setMarginBottom(botMarg/72.0);
734 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
735 getPageSpan().setMarginRight(rightMarg/72.0);
736 getPageSpan().setFormLength(paperSize.y()/72.);
737 getPageSpan().setFormWidth(paperSize.x()/72.);
738
739 ascii().addPos(pos);
740 ascii().addNote(f.str().c_str());
741 input->seek(pos+0x78, librevenge::RVNG_SEEK_SET);
742 if (long(input->tell()) != pos+0x78) {
743 MWAW_DEBUG_MSG(("MacWrtProParser::readPrintInfo: file is too short\n"));
744 return false;
745 }
746 ascii().addPos(input->tell());
747
748 return true;
749 }
750
751 ////////////////////////////////////////////////////////////
752 // read the document header
753 ////////////////////////////////////////////////////////////
754 bool MacWrtProParser::readDocHeader()
755 {
756 MWAWInputStreamPtr input = getInput();
757 long pos = input->tell();
758 libmwaw::DebugStream f;
759
760 f << "Entries(Data0):";
761 long val;
762 if (version()==0) {
763 val = input->readLong(2); // always 0 ?
764 if (val) f << "f0=" << val << ",";
765 /* fl0=[2|6|82|86], fl1=[80|a0|a4], other 0|1|-1 */
766 for (int i = 0; i < 9; i++) {
767 val = (i<2) ? int(input->readULong(1)) : input->readLong(1);
768 if (i==0 && (val&4)) {
769 f << "has[titlePage],";
770 m_state->m_hasTitlePage=true;
771 val &= 0xfb; // 2 or 82
772 }
773 if (!val) continue;
774 if (i < 2)
775 f << "fl" << i << "=" << std::hex << val << std::dec << ",";
776 else
777 f << "fl" << i << "=" << val << ",";
778 }
779 val = input->readLong(2); // always 612 ?
780 if (val != 0x612) f << "f1=" << val << ",";
781 val = input->readLong(1); // always 1 ?
782 if (val != 1) f << "f2=" << val << ",";
783 val = input->readLong(2); // always 2 ?
784 if (val != 2) f << "f3=" << val << ",";
785 val = input->readLong(2); // always 12c ?
786 if (val != 0x12c) f << "f4=" << val << ",";
787 for (int i = 0; i < 3; i++) { // 0, 0, 3c, a small number
788 val = input->readLong(2);
789 if (val) f << "g" << i << "=" << val << ",";
790 }
791 m_state->m_fileNumPages = int(input->readLong(2));
792 if (m_state->m_fileNumPages!=1)
793 f << "num[pages]=" << m_state->m_fileNumPages << ",";
794 /* then
795 0009000020000000fd803333000600000000000120 |
796 000c000020000000fd803333000600000000000180 |
797 000c000020000000fd8033330006000000000001a0 |
798 000c0000200e0000fd8033330006000000000001a0 |
799 00240000200e0000fd8033330006000000000001a0
800
801 and
802 000001000000016f66000000000000000800090001000000
803 */
804 }
805 else {
806 val = input->readLong(1); // always 0 ?
807 if (val) f << "unkn=" << val << ",";
808 auto N=static_cast<int>(input->readLong(2)); // find 2, a, 9e, 1a
809 f << "N?=" << N << ",";
810 N = static_cast<int>(input->readLong(1)); // almost always 0, find one time 6 ?
811 if (N) f << "N1?=" << N << ",";
812 val = static_cast<int>(input->readLong(2)); // almost always 0x622, find also 0 and 12
813 f << "f0=" << std::hex << val << std::dec << ",";
814 val = static_cast<int>(input->readLong(1)); // always 0 ?
815 if (val) f << "unkn1=" << val << ",";
816 N = static_cast<int>(input->readLong(2));
817 f << "N2?=" << N << ",";
818 val = input->readLong(1); // almost always 1 ( find one time 2)
819 f << "f1=" << val << ",";
820 int const defVal[] = { 0x64, 0/*small number between 1 and 8*/, 0x24 };
821 for (int i = 0; i < 3; i++) {
822 val = input->readLong(2);
823 if (i==1) {
824 m_state->m_fileNumPages = int(val);
825 if (m_state->m_fileNumPages!=1)
826 f << "num[pages]=" << val << ",";
827 continue;
828 }
829 if (val != defVal[i])
830 f << "f" << i+2 << "=" << val << ",";
831 }
832 for (int i = 5; i < 10; i++) { // always 0 ?
833 val = input->readLong(1);
834 if (val)
835 f << "f" << i << "=" << val << ",";
836 }
837 val = input->readLong(2); // always 480 ?
838 if (val != 0x480) f << "f10=" << val << ",";
839 val = long(input->readULong(1)); // always 0 ?
840 if (val) f << "f11=" << val << ",";
841 }
842 float dim[6];
843 bool ok = true;
844 for (auto &d : dim) {
845 d = float(input->readLong(4))/65356.f;
846 if (d<0) ok=false;
847 }
848 if (ok) ok = dim[0] > dim[2]+dim[3] && dim[1] > dim[4]+dim[5];
849
850 if (ok) {
851 getPageSpan().setMarginTop(double(dim[2])/72.0);
852 getPageSpan().setMarginLeft(double(dim[4])/72.0);
853 /* decrease a little the right/bottom margin to allow fonts discrepancy*/
854 getPageSpan().setMarginBottom((dim[3]<36) ? 0.0 : double(dim[3])/72.0-0.1);
855 getPageSpan().setMarginRight((dim[5]<18) ? 0.0 : double(dim[5])/72.0-0.1);
856 getPageSpan().setFormLength(double(dim[0])/72.);
857 getPageSpan().setFormWidth(double(dim[1])/72.);
858 }
859 else {
860 MWAW_DEBUG_MSG(("MacWrtProParser::readDocHeader: find odd page dimensions, ignored\n"));
861 f << "#";
862 }
863 f << "dim=" << dim[1] << "x" << dim[0] << ",";
864 f << "margins=["; // top, bottom, left, right
865 for (int i = 2; i < 6; i++) f << dim[i] << ",";
866 f << "],";
867 if (version()==0) {
868 m_state->m_col = static_cast<int>(input->readLong(2));
869 if (m_state->m_col != 1) f << "col=" << m_state->m_col << ",";
870 m_state->m_colSeparator=double(input->readLong(4))/65536./72.;
871 f << "col[sep]=" << m_state->m_colSeparator << "in,";
872 }
873
874 ascii().addDelimiter(input->tell(), '|');
875 if (version()>=1) {
876 /** then find
877 000000fd0000000000018200000100002f00
878 44[40|80] followed by something like a7c3ec07|a7c4c3c6 : 2 date
879 6f6600000000000000080009000105050506010401
880 */
881 input->seek(20, librevenge::RVNG_SEEK_CUR);
882 ascii().addDelimiter(input->tell(), '|');
883 for (int i=0; i<2; ++i)
884 f << "date" << i << "=" << convertDateToDebugString(unsigned(input->readULong(4)));
885 ascii().addDelimiter(input->tell(), '|');
886 }
887 ascii().addPos(pos);
888 ascii().addNote(f.str().c_str());
889
890 f.str("");
891 f << "Data0-A:";
892 if (version()==0) {
893 input->seek(pos+120, librevenge::RVNG_SEEK_SET);
894 pos = input->tell();
895 f << "selection=[" << input->readULong(4) << "x" << input->readULong(4) << ",";
896 for (int i=0; i<2; ++i) {
897 val = long(input->readULong(2));
898 if (!val) continue;
899 f << (i==0 ? "zone" : "pg") << "=" << val << ",";
900 }
901 f << "],";
902 }
903 else {
904 input->seek(pos+97, librevenge::RVNG_SEEK_SET);
905 pos = input->tell();
906 val = long(input->readULong(2));
907 if (val != 0x4d50) // MP
908 f << "#keyWord=" << std::hex << val <<std::dec;
909 //always 4, 4, 6 ?
910 for (int i = 0; i < 3; i++) {
911 val = input->readLong(1);
912 if ((i==2 && val!=6) || (i < 2 && val != 4))
913 f << "f" << i << "=" << val << ",";
914 }
915 for (int i = 3; i < 9; i++) { // always 0 ?
916 val = input->readLong(2);
917 if (val) f << "f" << i << "=" << val << ",";
918 }
919 }
920 // some dim ?
921 f << "dim=[";
922 for (int i = 0; i < 4; i++)
923 f << input->readLong(2) << ",";
924 f << "],";
925 // always 0x48 0x48
926 for (int i = 0; i < 2; i++) {
927 val = input->readLong(2);
928 if (val != 0x48) f << "g" << i << "=" << val << ",";
929 }
930 // always 0 ?
931 for (int i = 2; i < 42; i++) {
932 val = long(input->readULong(2));
933 if (val) f << "g" << i << "=" << std::hex << val << std::dec << ",";
934 }
935 ascii().addPos(pos);
936 ascii().addNote(f.str().c_str());
937
938 // then junk ? (ie. find a string portion, a list of 0...),
939 pos = input->tell();
940 f.str("");
941 f << "Data0-B:";
942 ascii().addPos(pos);
943 ascii().addNote(f.str().c_str());
944
945 // interesting data seems to begin again in 0x200...
946 input->seek(0x200, librevenge::RVNG_SEEK_SET);
947 ascii().addPos(input->tell());
948 ascii().addNote("_");
949 return true;
950 }
951
952 ////////////////////////////////////////////////////////////
953 // try to parse a data zone
954 ////////////////////////////////////////////////////////////
955 bool MacWrtProParser::parseDataZone(int blockId, int type)
956 {
957 if (m_state->m_dataMap.find(blockId) != m_state->m_dataMap.end())
958 return true;
959 if (blockId < 1) {
960 MWAW_DEBUG_MSG(("MacWrtProParser::parseDataZone: block %d seems bad\n", blockId));
961 return false;
962 }
963 if (m_state->m_parsedBlocks.find(blockId-1) != m_state->m_parsedBlocks.end()) {
964 MWAW_DEBUG_MSG(("MacWrtProParser::parseDataZone: block %d is already parsed\n", blockId));
965 return false;
966 }
967
968 auto input=getInput();
969 long pos=input->tell();
970 std::shared_ptr<MacWrtProParserInternal::Zone> zone(new MacWrtProParserInternal::Zone);
971 zone->m_blockId = blockId;
972 zone->m_type = type;
973 auto &stream = zone->m_stream = getStreamForBlock(blockId);
974 if (!stream)
975 return false;
976 m_state->m_dataMap[blockId] = zone;
977
978 // ok init is done
979 if (type == 0)
980 parseTextZone(zone);
981 else if (type == 1)
982 ;
983 else {
984 libmwaw::DebugStream f;
985 f << "Entries(DataZone):type" << type;
986 stream->m_ascii.addPos(stream->m_input->tell());
987 stream->m_ascii.addNote(f.str().c_str());
988 }
989 input->seek(pos, librevenge::RVNG_SEEK_SET);
990 return true;
991 }
992
993 bool MacWrtProParser::parseTextZone(std::shared_ptr<MacWrtProParserInternal::Zone> zone)
994 {
995 if (!zone || !zone->m_stream) return false;
996 if (zone->m_type != 0) {
997 MWAW_DEBUG_MSG(("MacWrtProParser::parseTextZone: not a text zone\n"));
998 return false;
999 }
1000
1001 auto &stream = zone->m_stream;
1002 MWAWInputStreamPtr input = stream->m_input;
1003 MWAWInputStreamPtr fileInput = getInput();
1004 libmwaw::DebugFile &asciiFile = stream->m_ascii;
1005 libmwaw::DebugStream f;
1006
1007 std::shared_ptr<MacWrtProParserInternal::TextZone> text(new MacWrtProParserInternal::TextZone);
1008
1009 long pos = input->tell();
1010 f << "Entries(TextZone):";
1011 text->m_textLength = static_cast<int>(input->readLong(4));
1012 f << "textLength=" << text->m_textLength << ",";
1013
1014 asciiFile.addPos(pos);
1015 asciiFile.addNote(f.str().c_str());
1016
1017 if (!readTextEntries(zone, text->m_entries, text->m_textLength))
1018 return false;
1019 m_state->m_textMap[zone->m_blockId] = text;
1020
1021 int n=0;
1022 for (auto &entry : text->m_entries) {
1023 n++;
1024 if (!fileInput->checkPosition(entry.begin())) {
1025 MWAW_DEBUG_MSG(("MacWrtProParser::parseTextZone: bad block id for block %ld\n", long(n-1)));
1026 entry.setBegin(-1);
1027 }
1028 }
1029 for (int i = 0; i < 2; i++) {
1030 if (!readTextIds(zone, text->m_ids[i], text->m_textLength, i))
1031 return true;
1032 }
1033
1034 if (!readTextTokens(zone, text->m_tokens, text->m_textLength))
1035 return true;
1036
1037 asciiFile.addPos(input->tell());
1038 asciiFile.addNote("TextZone(end)");
1039
1040 return true;
1041 }
1042
1043 bool MacWrtProParser::readTextEntries(std::shared_ptr<MacWrtProParserInternal::Zone> zone,
1044 std::vector<MWAWEntry> &res, int textLength)
1045 {
1046 res.resize(0);
1047 int vers = version();
1048 int expectedSize = vers == 0 ? 4 : 6;
1049 auto &stream=zone->m_stream;
1050 MWAWInputStreamPtr input = stream->m_input;
1051 libmwaw::DebugFile &asciiFile = stream->m_ascii;
1052 libmwaw::DebugStream f;
1053 long pos = input->tell();
1054
1055 auto sz = static_cast<int>(input->readULong(4));
1056 long endPos = pos+sz+4;
1057 if ((sz%expectedSize) != 0 || (long)((unsigned long)pos+(unsigned long)sz)<pos || !stream->checkPosition(endPos)) {
1058 MWAW_DEBUG_MSG(("MacWrtProParser::readTextEntries: find an odd size\n"));
1059 return false;
1060 }
1061
1062 int numElt = sz/expectedSize;
1063 f << "TextZone:entry(header),N=" << numElt << ",";
1064 asciiFile.addPos(pos);
1065 asciiFile.addNote(f.str().c_str());
1066
1067 int remainLength = textLength;
1068 for (int i = 0; i < numElt; i++) {
1069 pos = input->tell();
1070 f.str("");
1071 f << "TextZone-" << i << ":entry,";
1072 int unkn = 0;
1073 if (vers >= 1) {
1074 unkn = static_cast<int>(input->readLong(2));
1075 if (unkn) f << "unkn=" << unkn << ",";
1076 }
1077 auto bl = static_cast<int>(input->readLong(2));
1078 f << "block=" << std::hex << bl << std::dec << ",";
1079 auto nChar = static_cast<int>(input->readULong(2));
1080 f << "blockSz=" << nChar;
1081
1082 if (nChar > remainLength || nChar > 256) {
1083 MWAW_DEBUG_MSG(("MacWrtProParser::readTextEntries: bad size for block %d\n", i));
1084 input->seek(pos, librevenge::RVNG_SEEK_SET);
1085 break;
1086 }
1087 remainLength -= nChar;
1088 bool ok = bl >= 3 && m_state->m_parsedBlocks.find(bl-1) == m_state->m_parsedBlocks.end();
1089 if (!ok) {
1090 MWAW_DEBUG_MSG(("MacWrtProParser::readTextEntries: bad block id for block %d\n", i));
1091 input->seek(pos, librevenge::RVNG_SEEK_SET);
1092 break;
1093 }
1094
1095 m_state->m_parsedBlocks.insert(bl-1);
1096 asciiFile.addPos(pos);
1097 asciiFile.addNote(f.str().c_str());
1098 if (nChar==0) continue;
1099
1100 MWAWEntry entry;
1101 entry.setId(unkn);
1102 entry.setBegin((bl-1)*0x100);
1103 entry.setLength(nChar);
1104 res.push_back(entry);
1105 }
1106
1107 if (remainLength) {
1108 MWAW_DEBUG_MSG(("MacWrtProParser::readTextEntries: can not find %d characters\n", remainLength));
1109 asciiFile.addPos(input->tell());
1110 asciiFile.addNote("TextEntry-#");
1111 }
1112
1113 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1114 return true;
1115 }
1116
1117 bool MacWrtProParser::readTextIds(std::shared_ptr<MacWrtProParserInternal::Zone> zone,
1118 std::vector<MacWrtProParserInternal::TextZoneData> &res,
1119 int textLength, int type)
1120 {
1121 res.resize(0);
1122 auto &stream=zone->m_stream;
1123 MWAWInputStreamPtr input = stream->m_input;
1124 libmwaw::DebugFile &asciiFile = stream->m_ascii;
1125 libmwaw::DebugStream f;
1126 long pos = input->tell();
1127
1128 auto val = static_cast<int>(input->readULong(2));
1129 auto sz = static_cast<int>(input->readULong(2));
1130 if (sz == 0) {
1131 asciiFile.addPos(pos);
1132 asciiFile.addNote("_");
1133 return true;
1134 }
1135
1136 long endPos = pos+sz+4;
1137 if ((sz%6) != 0 || !stream->checkPosition(endPos)) {
1138 MWAW_DEBUG_MSG(("MacWrtProParser::readTextIds: find an odd size\n"));
1139 return false;
1140 }
1141
1142 int numElt = sz/6;
1143 f << "TextZone:type=" << type << "(header),N=" << numElt << ",";
1144 if (val) f << "unkn=" << val << ",";
1145 asciiFile.addPos(pos);
1146 asciiFile.addNote(f.str().c_str());
1147
1148 long remainLength = textLength;
1149 for (int i = 0; i < numElt; i++) {
1150 MacWrtProParserInternal::TextZoneData data;
1151 data.m_type = type;
1152 pos = input->tell();
1153 data.m_id = static_cast<int>(input->readLong(2));
1154 auto nChar = long(input->readULong(4));
1155 data.m_length = static_cast<int>(nChar);
1156 f.str("");
1157 f << "TextZone-" << i<< ":" << data;
1158
1159 if (nChar > remainLength) {
1160 MWAW_DEBUG_MSG(("MacWrtProParser::readTextIds: bad size for block %d\n", i));
1161 input->seek(pos, librevenge::RVNG_SEEK_SET);
1162 break;
1163 }
1164 remainLength -= nChar;
1165 asciiFile.addPos(pos);
1166 asciiFile.addNote(f.str().c_str());
1167 if (nChar==0) continue;
1168
1169 res.push_back(data);
1170 }
1171
1172 if (remainLength) {
1173 MWAW_DEBUG_MSG(("MacWrtProParser::readTextIds: can not find %ld characters\n", remainLength));
1174 asciiFile.addPos(input->tell());
1175 asciiFile.addNote("TextZone:id-#");
1176 }
1177
1178 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1179 return res.size() != 0;
1180 }
1181
1182 bool MacWrtProParser::readTextTokens(std::shared_ptr<MacWrtProParserInternal::Zone> zone,
1183 std::vector<MacWrtProParserInternal::Token> &res,
1184 int textLength)
1185 {
1186 res.resize(0);
1187 int vers = version();
1188 int expectedSz = vers==0 ? 8 : 10;
1189 auto &stream=zone->m_stream;
1190 MWAWInputStreamPtr input = stream->m_input;
1191 libmwaw::DebugFile &asciiFile = stream->m_ascii;
1192 libmwaw::DebugStream f;
1193 long pos = input->tell();
1194
1195 auto val = static_cast<int>(input->readULong(2));
1196 if (val && vers == 0) {
1197 input->seek(pos, librevenge::RVNG_SEEK_SET);
1198 asciiFile.addPos(pos);
1199 asciiFile.addNote("_");
1200 return true;
1201 }
1202 long sz = static_cast<int>(input->readULong(2));
1203 if (sz == 0) {
1204 asciiFile.addPos(pos);
1205 asciiFile.addNote("_");
1206 return true;
1207 }
1208
1209 long endPos = pos+sz+4;
1210 if ((sz%expectedSz) != 0 || !stream->checkPosition(endPos)) {
1211 MWAW_DEBUG_MSG(("MacWrtProParser::readTextTokens: find an odd size\n"));
1212 return false;
1213 }
1214
1215 auto numElt = int(sz/expectedSz);
1216 f << "TextZone:token(header),N=" << numElt << ",";
1217 if (val) f << "unkn=" << val << ",";
1218 asciiFile.addPos(pos);
1219 asciiFile.addNote(f.str().c_str());
1220
1221 long remainLength = textLength;
1222 int numFootnotes = 0;
1223 std::vector<int> pictPos;
1224 for (int i = 0; i < numElt; i++) {
1225 f.str("");
1226 pos = input->tell();
1227
1228 MacWrtProParserInternal::Token data;
1229 data.m_type = static_cast<int>(input->readULong(1));
1230 if (vers==0) { // check me
1231 switch (data.m_type) {
1232 case 2: // page number
1233 data.m_type=1;
1234 break;
1235 case 3: // footnote content
1236 break;
1237 case 4: // figure
1238 break;
1239 case 5: // footnote pos
1240 data.m_type=2;
1241 data.m_blockId = ++numFootnotes; // for MW2
1242 break;
1243 case 0x15: // Fixme: must find other date
1244 case 0x17: // date alpha
1245 data.m_type=6;
1246 break;
1247 case 0x1a: // time
1248 data.m_type=7;
1249 break;
1250 default:
1251 MWAW_DEBUG_MSG(("MacWrtProParser::readTextTokens: unknown block type %d\n", data.m_type));
1252 f << "#type=" << data.m_type << ",";
1253 data.m_type = -1;
1254 break;
1255 }
1256 }
1257 data.m_flags[0] = static_cast<unsigned int>(input->readULong(1));
1258 auto nChar = long(input->readULong(vers == 0 ? 2 : 4));
1259 data.m_length = static_cast<int>(nChar);
1260
1261 if (vers==0)
1262 data.m_flags[1]=static_cast<unsigned int>(input->readULong(4)); // some kind of ID
1263 else {
1264 for (int j = 1; j < 3; j++) data.m_flags[j] = static_cast<unsigned int>(input->readULong(1));
1265 data.m_blockId = static_cast<int>(input->readULong(2));
1266 }
1267 f << "TextZone-" << i<< ":token," << data;
1268 if (nChar > remainLength) {
1269 MWAW_DEBUG_MSG(("MacWrtProParser::readTextTokens: bad size for block %d\n", i));
1270 input->seek(pos, librevenge::RVNG_SEEK_SET);
1271 break;
1272 }
1273 remainLength -= nChar;
1274 asciiFile.addPos(pos);
1275 asciiFile.addNote(f.str().c_str());
1276 if (data.m_type == 4) pictPos.push_back(static_cast<int>(res.size()));
1277 res.push_back(data);
1278
1279 if (vers == 1 && data.m_blockId && (data.m_type == 2 || data.m_type == 4))
1280 m_state->m_graphicIdsCallByTokens.push_back(data.m_blockId);
1281 }
1282 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1283 if (vers == 0 && pictPos.size()) {
1284 size_t numPict = pictPos.size();
1285 // checkme always inverted ?
1286 for (size_t i = numPict; i > 0; i--) {
1287 MacWrtProParserInternal::Token &token = res[size_t(pictPos[i-1])];
1288 pos = input->tell();
1289 f.str("");
1290 f << "TextZone-pict" << i-1<< ":";
1291 val = static_cast<int>(input->readLong(2));
1292 if (val) f << "unkn=" << val << ",";
1293 auto blockId = static_cast<int>(input->readULong(2));
1294 if (blockId) {
1295 token.m_blockId = blockId;
1296 f << "block=" << blockId << ",";
1297 parseDataZone(blockId,1);
1298 }
1299 sz = long(input->readULong(4));
1300 f << "sz=" << std::hex << sz << std::dec << ",";
1301 int dim[4];
1302 for (auto &d : dim) d = static_cast<int>(input->readLong(2));
1303 token.m_box = MWAWBox2f(MWAWVec2f(float(dim[1]),float(dim[0])), MWAWVec2f(float(dim[3]),float(dim[2])));
1304 f << "dim=" << token.m_box << ",";
1305 for (auto &d : dim) d = static_cast<int>(input->readLong(2));
1306 f << "dim2=" << MWAWBox2i(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2])) << ",";
1307 // followed by junk ?
1308 ascii().addDelimiter(input->tell(),'|');
1309 input->seek(pos+62, librevenge::RVNG_SEEK_SET);
1310 asciiFile.addPos(pos);
1311 asciiFile.addNote(f.str().c_str());
1312 }
1313 }
1314
1315 return res.size() != 0;
1316 }
1317
1318 ////////////////////////////////////////////////////////////
1319 // try to send a empty zone
1320 ////////////////////////////////////////////////////////////
1321 bool MacWrtProParser::sendEmptyFrameZone(MWAWPosition const &pos, MWAWGraphicStyle const &style)
1322 {
1323 std::shared_ptr<MacWrtProParserInternal::SubDocument> subdoc
1324 (new MacWrtProParserInternal::SubDocument(*this, getInput(), -3));
1325 if (getTextListener())
1326 getTextListener()->insertTextBox(pos, subdoc, style);
1327 return true;
1328 }
1329
1330 ////////////////////////////////////////////////////////////
1331 // try to send a text
1332 ////////////////////////////////////////////////////////////
1333 int MacWrtProParser::findNumHardBreaks(int blockId)
1334 {
1335 auto it = m_state->m_textMap.find(blockId);
1336 if (it == m_state->m_textMap.end()) {
1337 MWAW_DEBUG_MSG(("MacWrtProParser::findNumHardBreaks: can not find text zone\n"));
1338 return 0;
1339 }
1340 return findNumHardBreaks(it->second);
1341 }
1342
1343 int MacWrtProParser::findNumHardBreaks(std::shared_ptr<MacWrtProParserInternal::TextZone> zone)
1344 {
1345 if (!zone->m_entries.size()) return 0;
1346 int num = 0;
1347 MWAWInputStreamPtr input = getInput();
1348 for (auto const &entry : zone->m_entries) {
1349 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1350 for (long j = 0; j < entry.length(); j++) {
1351 switch (input->readULong(1)) {
1352 case 0xc: // hard page
1353 case 0xb: // difficult to differentiate column/page break so...
1354 num++;
1355 break;
1356 default:
1357 break;
1358 }
1359 }
1360 }
1361 return num;
1362 }
1363
1364 ////////////////////////////////////////////////////////////
1365 // try to send a text
1366 ////////////////////////////////////////////////////////////
1367 bool MacWrtProParser::sendTextZone(int blockId, bool mainZone)
1368 {
1369 auto it = m_state->m_textMap.find(blockId);
1370 if (it == m_state->m_textMap.end()) {
1371 MWAW_DEBUG_MSG(("MacWrtProParser::sendTextZone: can not find text zone %x\n", unsigned(blockId)));
1372 return false;
1373 }
1374 sendText(it->second, mainZone);
1375 return true;
1376 }
1377
1378 bool MacWrtProParser::sendTextBoxZone(int blockId, MWAWPosition const &pos, MWAWGraphicStyle const &style)
1379 {
1380 std::shared_ptr<MacWrtProParserInternal::SubDocument> subdoc
1381 (new MacWrtProParserInternal::SubDocument(*this, getInput(), blockId));
1382 if (getTextListener())
1383 getTextListener()->insertTextBox(pos, subdoc, style);
1384 return true;
1385 }
1386
1387 namespace MacWrtProParserInternal
1388 {
1389 /** Internal and low level: structure used to sort the position of data */
1390 struct DataPosition {
1391 //! constructor
1392 explicit DataPosition(int type=-1, int id=-1, long pos=0)
1393 : m_type(type)
1394 , m_id(id)
1395 , m_pos(pos)
1396 {
1397 }
1398 //! the type
1399 int m_type;
1400 //! an id
1401 int m_id;
1402 //! the position
1403 long m_pos;
1404 //! the comparison structure
1405 struct Compare {
1406 //! comparaison function
1407 bool operator()(DataPosition const &p1, DataPosition const &p2) const
1408 {
1409 long diff = p1.m_pos - p2.m_pos;
1410 if (diff) return (diff < 0);
1411 diff = p1.m_type - p2.m_type;
1412 if (diff) return (diff < 0);
1413 diff = p1.m_id - p2.m_id;
1414 return (diff < 0);
1415 }
1416 };
1417 };
1418 }
1419
1420 bool MacWrtProParser::sendText(std::shared_ptr<MacWrtProParserInternal::TextZone> zone, bool mainZone)
1421 {
1422 if (!zone->m_entries.size()) // can happen in header/footer
1423 return false;
1424 int vers = version();
1425 MacWrtProStructuresListenerState listenerState(m_structures, mainZone, vers);
1426 MacWrtProParserInternal::DataPosition::Compare compareFunction;
1427 std::set<MacWrtProParserInternal::DataPosition, MacWrtProParserInternal::DataPosition::Compare>
1428 set(compareFunction);
1429 long cPos = 0;
1430 for (size_t i = 0; i < zone->m_entries.size(); i++) {
1431 set.insert(MacWrtProParserInternal::DataPosition(3, static_cast<int>(i), cPos));
1432 cPos += zone->m_entries[i].length();
1433 }
1434 set.insert(MacWrtProParserInternal::DataPosition(4, 0, cPos));
1435 cPos = 0;
1436 for (size_t i = 0; i < zone->m_tokens.size(); i++) {
1437 cPos += zone->m_tokens[i].m_length;
1438 set.insert(MacWrtProParserInternal::DataPosition(2, static_cast<int>(i), cPos));
1439 }
1440 for (int id = 0; id < 2; id++) {
1441 cPos = 0;
1442 for (size_t i = 0; i < zone->m_ids[id].size(); i++) {
1443 set.insert(MacWrtProParserInternal::DataPosition(1-id, static_cast<int>(i), cPos));
1444 cPos += zone->m_ids[id][i].m_length;
1445 }
1446 }
1447 std::vector<int> pageBreaks=listenerState.getPageBreaksPos();
1448 for (size_t i = 0; i < pageBreaks.size(); i++) {
1449 if (pageBreaks[i]<=0 || pageBreaks[i] >= zone->m_textLength) {
1450 if (pageBreaks[i] >= zone->m_textLength+1) {
1451 MWAW_DEBUG_MSG(("MacWrtProParser::sendText: page breaks seems bad\n"));
1452 }
1453 break;
1454 }
1455 set.insert(MacWrtProParserInternal::DataPosition(-1, static_cast<int>(i), pageBreaks[i]));
1456 }
1457
1458 MWAWInputStreamPtr input = getInput();
1459 long pos = zone->m_entries[0].begin();
1460 long asciiPos = pos;
1461 if (pos > 0)
1462 input->seek(pos, librevenge::RVNG_SEEK_SET);
1463
1464 libmwaw::DebugStream f, f2;
1465 cPos = 0;
1466 for (auto const &data : set) {
1467 long oldPos = pos;
1468 if (data.m_pos < cPos) {
1469 MWAW_DEBUG_MSG(("MacWrtProParser::sendText: position go backward, stop...\n"));
1470 break;
1471 }
1472 if (data.m_pos != cPos) {
1473 if (pos > 0) {
1474 std::string text("");
1475 for (long i = cPos; i < data.m_pos && !input->isEnd(); i++) {
1476 auto ch = char(input->readULong(1));
1477 if (!ch)
1478 text+= "#";
1479 else {
1480 listenerState.sendChar(ch);
1481 if (ch > 0 && ch < 20 && ch != 0xd && ch != 0x9) text+="#";
1482 text+=ch;
1483 }
1484 }
1485 f << "'" << text << "'";
1486 }
1487
1488 if (pos > 0 && f.str().length()) {
1489 f2.str("");
1490 f2 << "Entries(TextContent):" << f.str();
1491 f.str("");
1492 ascii().addPos(asciiPos);
1493 ascii().addNote(f2.str().c_str());
1494 pos += (data.m_pos-cPos);
1495 }
1496
1497 cPos = data.m_pos;
1498 }
1499 switch (data.m_type) {
1500 case -1:
1501 listenerState.insertSoftPageBreak();
1502 break;
1503 case 4:
1504 case 3:
1505 if (pos > 0 && (pos&0xFF))
1506 ascii().addDelimiter(pos,'|');
1507 if (data.m_type == 3) {
1508 pos = zone->m_entries[size_t(data.m_id)].begin();
1509 if (pos > 0)
1510 input->seek(pos, librevenge::RVNG_SEEK_SET);
1511 }
1512 break;
1513 case 2: {
1514 // save the position because we read some extra data ( footnote, table, textbox)
1515 long actPos = input->tell();
1516 switch (zone->m_tokens[size_t(data.m_id)].m_type) {
1517 case 1:
1518 if (getTextListener()) getTextListener()->insertField(MWAWField(MWAWField::PageNumber));
1519 break;
1520 case 2:
1521 if (vers == 1 && listenerState.isSent(zone->m_tokens[size_t(data.m_id)].m_blockId)) {
1522 MWAW_DEBUG_MSG(("MacWrtProParser::sendText: footnote is already sent...\n"));
1523 }
1524 else {
1525 int id = zone->m_tokens[size_t(data.m_id)].m_blockId;
1526 if (vers == 0) id = -id;
1527 MWAWSubDocumentPtr subdoc(new MacWrtProParserInternal::SubDocument(*this, getInput(), id));
1528 getTextListener()->insertNote(MWAWNote(MWAWNote::FootNote), subdoc);
1529 }
1530 break;
1531 case 3:
1532 break; // footnote content, ok
1533 case 4:
1534 if (vers==0) {
1535 MWAWPosition pictPos(MWAWVec2f(0,0), zone->m_tokens[size_t(data.m_id)].m_box.size(), librevenge::RVNG_POINT);
1536 pictPos.setRelativePosition(MWAWPosition::Char, MWAWPosition::XLeft, MWAWPosition::YBottom);
1537 sendPictureZone(zone->m_tokens[size_t(data.m_id)].m_blockId, pictPos);
1538 }
1539 else
1540 listenerState.send(zone->m_tokens[size_t(data.m_id)].m_blockId);
1541 break;
1542 case 5:
1543 break; // hyphen ok
1544 case 6:
1545 if (getTextListener()) getTextListener()->insertField(MWAWField(MWAWField::Date));
1546 break;
1547 case 7:
1548 if (getTextListener()) getTextListener()->insertField(MWAWField(MWAWField::Time));
1549 break;
1550 case 8:
1551 if (getTextListener()) getTextListener()->insertField(MWAWField(MWAWField::Title));
1552 break;
1553 case 9:
1554 if (getTextListener()) getTextListener()->insertUnicodeString(librevenge::RVNGString("#REVISION#"));
1555 break;
1556 case 10:
1557 if (getTextListener()) {
1558 int numSection = listenerState.numSection()+1;
1559 std::stringstream s;
1560 s << numSection;
1561 getTextListener()->insertUnicodeString(librevenge::RVNGString(s.str().c_str()));
1562 }
1563 break;
1564 default:
1565 break;
1566 }
1567 f << "token[" << zone->m_tokens[size_t(data.m_id)] << "],";
1568 input->seek(actPos, librevenge::RVNG_SEEK_SET);
1569 break;
1570 }
1571 case 1:
1572 if (m_structures)
1573 listenerState.sendFont(zone->m_ids[0][size_t(data.m_id)].m_id);
1574 f << "[C" << zone->m_ids[0][size_t(data.m_id)].m_id << "],";
1575 break;
1576 case 0:
1577 if (m_structures)
1578 listenerState.sendParagraph(zone->m_ids[1][size_t(data.m_id)].m_id);
1579 f << "[P" << zone->m_ids[1][size_t(data.m_id)].m_id << "],";
1580 break;
1581 default: {
1582 static bool firstError = true;
1583 if (firstError) {
1584 MWAW_DEBUG_MSG(("MacWrtProParser::sendText: find unexpected data type...\n"));
1585 firstError = false;
1586 }
1587 f << "#";
1588 break;
1589 }
1590
1591 }
1592 if (pos >= 0 && pos != oldPos)
1593 asciiPos = pos;
1594 }
1595
1596 return true;
1597 }
1598
1599
1600 ////////////////////////////////////////////////////////////
1601 // try to send a picture
1602 ////////////////////////////////////////////////////////////
1603 bool MacWrtProParser::sendPictureZone(int blockId, MWAWPosition const &pictPos,
1604 MWAWGraphicStyle const &style)
1605 {
1606 auto it = m_state->m_dataMap.find(blockId);
1607 if (it == m_state->m_dataMap.end()) {
1608 MWAW_DEBUG_MSG(("MacWrtProParser::sendPictureZone: can not find picture zone\n"));
1609 return false;
1610 }
1611 sendPicture(it->second, pictPos, style);
1612 return true;
1613 }
1614
1615 bool MacWrtProParser::sendPicture(std::shared_ptr<MacWrtProParserInternal::Zone> zone,
1616 MWAWPosition pictPos, MWAWGraphicStyle const &style)
1617 {
1618 if (!zone) return false;
1619 if (zone->m_type != 1) {
1620 MWAW_DEBUG_MSG(("MacWrtProParser::sendPicture: not a picture date\n"));
1621 return false;
1622 }
1623
1624 zone->m_parsed = true;
1625
1626 // ok init is done
1627 auto &stream=zone->m_stream;
1628 MWAWInputStreamPtr input = stream->m_input;
1629 libmwaw::DebugFile &asciiFile = stream->m_ascii;
1630 long pos=stream->m_bof;
1631 input->seek(pos, librevenge::RVNG_SEEK_SET);
1632 libmwaw::DebugStream f;
1633
1634 f << "Entries(PICT),";
1635 asciiFile.addPos(pos);
1636 asciiFile.addNote(f.str().c_str());
1637
1638 auto pictSize = long(input->readULong(4));
1639 if (pictSize < 10 || !stream->checkPosition(pos+4+pictSize)) {
1640 MWAW_DEBUG_MSG(("MacWrtProParser::sendPicture: oops a pb with pictSize\n"));
1641 asciiFile.addPos(4);
1642 asciiFile.addNote("#PICT");
1643 return false;
1644 }
1645 std::shared_ptr<MWAWPict> pict(MWAWPictData::get(input, static_cast<int>(pictSize)));
1646 if (!pict) {
1647 // sometimes this just fails because the pictSize is not correct
1648 input->seek(pos+14, librevenge::RVNG_SEEK_SET);
1649 if (input->readULong(2) == 0x1101) { // try to force the size to be ok
1650 librevenge::RVNGBinaryData data;
1651 input->seek(pos, librevenge::RVNG_SEEK_SET);
1652 input->readDataBlock(4+pictSize, data);
1653 auto *dataPtr=const_cast<unsigned char *>(data.getDataBuffer());
1654 if (!dataPtr) {
1655 MWAW_DEBUG_MSG(("MacWrtProParser::sendPicture: oops where is the picture...\n"));
1656 return false;
1657 }
1658
1659 dataPtr[4]=dataPtr[2];
1660 dataPtr[5]=dataPtr[3];
1661
1662 MWAWInputStreamPtr pictInput=MWAWInputStream::get(data, false);
1663 if (!pictInput) {
1664 MWAW_DEBUG_MSG(("MacWrtProParser::sendPicture: oops where is the picture input...\n"));
1665 return false;
1666 }
1667
1668 pictInput->seek(4, librevenge::RVNG_SEEK_SET);
1669 pict.reset(MWAWPictData::get(pictInput, static_cast<int>(pictSize)));
1670 }
1671 }
1672
1673 #ifdef DEBUG_WITH_FILES
1674 asciiFile.skipZone(pos+4, pos+4+pictSize-1);
1675 librevenge::RVNGBinaryData file;
1676 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1677 input->readDataBlock(pictSize, file);
1678 static int volatile pictName = 0;
1679 f.str("");
1680 f << "PICT-" << ++pictName;
1681 libmwaw::Debug::dumpFile(file, f.str().c_str());
1682 asciiFile.addPos(pos+4+pictSize);
1683 asciiFile.addNote("PICT(end)");
1684 #endif
1685
1686 if (!pict) { // ok, we can not do anything except sending the data...
1687 MWAW_DEBUG_MSG(("MacWrtProParser::sendPicture: no sure this is a picture\n"));
1688 if (pictPos.size().x() <= 0 || pictPos.size().y() <= 0)
1689 pictPos=MWAWPosition(MWAWVec2f(0,0),MWAWVec2f(100.,100.), librevenge::RVNG_POINT);
1690 if (getTextListener()) {
1691 librevenge::RVNGBinaryData data;
1692 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1693 input->readDataBlock(pictSize, data);
1694 getTextListener()->insertPicture(pictPos, MWAWEmbeddedObject(data, "image/pict"), style);
1695 }
1696 return true;
1697 }
1698
1699 if (pictPos.size().x() <= 0 || pictPos.size().y() <= 0) {
1700 pictPos.setOrigin(MWAWVec2f(0,0));
1701 pictPos.setSize(pict->getBdBox().size());
1702 pictPos.setUnit(librevenge::RVNG_POINT);
1703 }
1704 if (pict->getBdBox().size().x() > 0 && pict->getBdBox().size().y() > 0)
1705 pictPos.setNaturalSize(pict->getBdBox().size());
1706
1707 if (getTextListener()) {
1708 MWAWEmbeddedObject picture;
1709 if (pict->getBinary(picture))
1710 getTextListener()->insertPicture(pictPos, picture, style);
1711 }
1712 return true;
1713 }
1714
1715 ////////////////////////////////////////////////////////////
1716 // some debug functions
1717 ////////////////////////////////////////////////////////////
1718 void MacWrtProParser::checkUnparsed()
1719 {
1720 MWAWInputStreamPtr input = getInput();
1721 libmwaw::DebugStream f;
1722
1723 long pos;
1724 std::stringstream notParsed;
1725 for (int bl = 3; bl < 1000; bl++) {
1726 if (m_state->m_parsedBlocks.find(bl) != m_state->m_parsedBlocks.end())
1727 continue;
1728
1729 pos = bl*0x100;
1730 input->seek(pos, librevenge::RVNG_SEEK_SET);
1731 if (input->isEnd()) break;
1732 notParsed << std::hex << bl << std::dec << ",";
1733
1734 // normaly there must remains only text entry...
1735 f.str("");
1736 f << "Entries(Unparsed):";
1737
1738 std::string text("");
1739 bool findZero = false;
1740 for (int c = 0; c < 256; c++) {
1741 auto ch = char(input->readULong(1));
1742 if (!ch) {
1743 if (findZero) {
1744 input->seek(-1, librevenge::RVNG_SEEK_CUR);
1745 break;
1746 }
1747 findZero = true;
1748 continue;
1749 }
1750 if (findZero) {
1751 text += "#";
1752 findZero = false;
1753 }
1754 text+=ch;
1755 }
1756 f << text;
1757 if (long(input->tell()) != pos+256)
1758 ascii().addDelimiter(input->tell(),'|');
1759 ascii().addPos(pos);
1760 ascii().addNote(f.str().c_str());
1761 }
1762 if (!notParsed.str().empty()) {
1763 MWAW_DEBUG_MSG(("MacWrtProParser::checkUnparsed: not parsed %s\n", notParsed.str().c_str()));
1764 }
1765 }
1766
1767 std::string MacWrtProParser::convertDateToDebugString(unsigned dt)
1768 {
1769 int Y, M, D, HH, MM, SS;
1770 MWAWCellContent::double2Date(double(dt/3600/24)+1460., Y, M, D); // change the reference date from 1/1/1904 to 1/1/1900
1771 double time=double(dt%(3600*24))/3600/24;
1772 MWAWCellContent::double2Time(time, HH, MM, SS);
1773 std::stringstream s;
1774 s << D << "/" << M << "/" << Y << " " << HH << ":" << MM << ",";
1775 return s.str();
1776 }
1777
1778 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: