Debian Codegraph @

src/lib/MacWrtParser.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 /* Inspired of TN-012-Disk-Based-MW-Format.txt */
35
36 #include <iomanip>
37 #include <iostream>
38 #include <limits>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWTextListener.hxx"
44 #include "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWHeader.hxx"
47 #include "MWAWParagraph.hxx"
48 #include "MWAWPictMac.hxx"
49 #include "MWAWPosition.hxx"
50 #include "MWAWPrinter.hxx"
51 #include "MWAWRSRCParser.hxx"
52 #include "MWAWSubDocument.hxx"
53
54 #include "MacWrtParser.hxx"
55
56 /** Internal: the structures of a MacWrtParser */
57 namespace MacWrtParserInternal
58 {
59
60 //! Document header
61 struct FileHeader {
62 FileHeader()
63 : m_hideFirstPageHeaderFooter(false)
64 , m_startNumberPage(1)
65 , m_freeListPos(0)
66 , m_freeListLength(0)
67 , m_freeListAllocated(0)
68 , m_dataPos(0)
69 {
70 for (auto &num : m_numParagraphs) num = 0;
71 }
72
73 friend std::ostream &operator<<(std::ostream &o, FileHeader const &header);
74
75 //! the number of lines : text, header footer
76 int m_numParagraphs[3];
77 //! true if the first page header/footer must be draw
78 bool m_hideFirstPageHeaderFooter;
79 //! the first number page
80 int m_startNumberPage;
81 //! free list start position
82 long m_freeListPos;
83 //! free list length
84 long m_freeListLength;
85 //! free list allocated
86 long m_freeListAllocated;
87 //! the begin of data ( if version == 3)
88 long m_dataPos;
89 };
90
91 std::ostream &operator<<(std::ostream &o, FileHeader const &header)
92 {
93 for (int i=0; i < 3; i++) {
94 if (!header.m_numParagraphs[i]) continue;
95 o << "numParagraph";
96 if (i==1) o << "[header]";
97 else if (i==2) o << "[footer]";
98 o << "=" << header.m_numParagraphs[i] << ",";
99 }
100 if (header.m_hideFirstPageHeaderFooter)
101 o << "noHeaderFooter[FirstPage],";
102 if (header.m_startNumberPage != 1)
103 o << "firstPageNumber=" << header.m_startNumberPage << ",";
104 if (header.m_freeListPos) {
105 o << "FreeList=" << std::hex
106 << header.m_freeListPos
107 << "[" << header.m_freeListLength << "+" << header.m_freeListAllocated << "],"
108 << std::dec << ",";
109 }
110 if (header.m_dataPos)
111 o << "DataPos=" << std::hex << header.m_dataPos << std::dec << ",";
112
113 return o;
114 }
115
116 ////////////////////////////////////////
117 //! the paragraph... information
118 struct Information {
119 /** the different type */
120 enum Type { TEXT, RULER, GRAPHIC, PAGEBREAK, UNKNOWN };
121
122 //! constructor
123 Information()
124 : m_type(UNKNOWN)
125 , m_compressed(false)
126 , m_pos()
127 , m_height(0)
128 , m_justify(MWAWParagraph::JustificationLeft)
129 , m_justifySet(false)
130 , m_data()
131 , m_font()
132 {
133 }
134
135 //! operator<<
136 friend std::ostream &operator<<(std::ostream &o, Information const &info);
137
138 //! the type
139 Type m_type;
140
141 //! a flag to know if the text data are compressed
142 bool m_compressed;
143
144 //! top left position
145 MWAWPosition m_pos;
146
147 //! the paragraph height
148 int m_height;
149
150 //! paragraph justification : MWAW_PARAGRAPH_JUSTIFICATION*
151 MWAWParagraph::Justification m_justify;
152
153 //! true if the justification must be used
154 bool m_justifySet;
155 //! the position in the file
156 MWAWEntry m_data;
157
158 //! the font
159 MWAWFont m_font;
160 };
161
162 std::ostream &operator<<(std::ostream &o, Information const &info)
163 {
164 switch (info.m_type) {
165 case Information::TEXT:
166 o << "text";
167 if (info.m_compressed) o << "[compressed]";
168 o << ",";
169 break;
170 case Information::RULER:
171 o << "indent,";
172 break;
173 case Information::GRAPHIC:
174 o << "graphics,";
175 break;
176 case Information::PAGEBREAK:
177 o << "pageBreak,";
178 break;
179 case Information::UNKNOWN:
180 #if !defined(__clang__)
181 default:
182 #endif
183 o << "###unknownType,";
184 break;
185 }
186 o << info.m_pos << ",";
187 if (info.m_height) o << "height=" << info.m_height << ",";
188
189 if (info.m_justifySet) {
190 switch (info.m_justify) {
191 case MWAWParagraph::JustificationLeft:
192 o << "left[justify],";
193 break;
194 case MWAWParagraph::JustificationCenter:
195 o << "center[justify],";
196 break;
197 case MWAWParagraph::JustificationRight:
198 o << "right[justify],";
199 break;
200 case MWAWParagraph::JustificationFull:
201 o << "full[justify],";
202 break;
203 case MWAWParagraph::JustificationFullAllLines:
204 o << "fullAllLines[justify],";
205 break;
206 #if !defined(__clang__)
207 default:
208 o << "###unknown[justify],";
209 break;
210 #endif
211 }
212 }
213 if (info.m_data.begin() > 0)
214 o << std::hex << "data=[" << info.m_data.begin() << "-" << info.m_data.end() << "]," << std::dec;
215 return o;
216 }
217
218 ////////////////////////////////////////
219 //! the windows structure
220 struct WindowsInfo {
221 WindowsInfo()
222 : m_startSel()
223 , m_endSel()
224 , m_posTopY(0)
225 , m_informations()
226 , m_firstParagLine()
227 , m_linesHeight()
228 , m_pageNumber()
229 , m_date()
230 , m_time()
231 {
232 }
233
234 /** small function used to recognized empty header or footer */
235 bool isEmpty() const
236 {
237 if (m_informations.size() == 0) return true;
238 if (m_pageNumber.x() >= 0 || m_date.x() >= 0 || m_time.x() >= 0)
239 return false;
240 if (m_informations.size() > 2) return false;
241 for (auto const &info : m_informations) {
242 switch (info.m_type) {
243 case Information::GRAPHIC:
244 return false;
245 case Information::TEXT:
246 if (info.m_data.length() != 10)
247 return false;
248 // empty line : ok
249 break;
250 case Information::RULER:
251 case Information::PAGEBREAK:
252 case Information::UNKNOWN:
253 #if !defined(__clang__)
254 default:
255 #endif
256 break;
257 }
258 }
259 return true;
260 }
261
262 friend std::ostream &operator<<(std::ostream &o, WindowsInfo const &w);
263
264 MWAWVec2i m_startSel, m_endSel; // start end selection (parag, char)
265 int m_posTopY;
266 std::vector<Information> m_informations;
267 std::vector<int> m_firstParagLine, m_linesHeight;
268 MWAWVec2i m_pageNumber, m_date, m_time;
269 };
270
271 std::ostream &operator<<(std::ostream &o, WindowsInfo const &w)
272 {
273 o << "sel=[" << w.m_startSel << "-" << w.m_endSel << "],";
274 if (w.m_posTopY) o << "windowsY=" << w.m_posTopY << ",";
275 o << "pageNumberPos=" << w.m_pageNumber << ",";
276 o << "datePos=" << w.m_date << ",";
277 o << "timePos=" << w.m_time << ",";
278 return o;
279 }
280
281 ////////////////////////////////////////
282 //! Internal: the state of a MacWrtParser
283 struct State {
284 //! constructor
285 State()
286 : m_compressCorr(" etnroaisdlhcfp")
287 , m_actPage(0)
288 , m_numPages(0)
289 , m_fileHeader()
290 , m_headerHeight(0)
291 , m_footerHeight(0)
292 {
293 }
294
295 //! the correspondance between int compressed and char : must be 15 character
296 std::string m_compressCorr;
297
298 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
299
300 //! the header
301 FileHeader m_fileHeader;
302
303 //! the information of main document, header, footer
304 WindowsInfo m_windows[3];
305
306 int m_headerHeight /** the header height if known */,
307 m_footerHeight /** the footer height if known */;
308 };
309
310 ////////////////////////////////////////
311 //! Internal: the subdocument of a MacWrtParser
312 class SubDocument final : public MWAWSubDocument
313 {
314 public:
315 SubDocument(MacWrtParser &pars, MWAWInputStreamPtr const &input, int zoneId)
316 : MWAWSubDocument(&pars, input, MWAWEntry())
317 , m_id(zoneId)
318 {
319 }
320
321 //! destructor
322 ~SubDocument() final {}
323
324 //! operator!=
325 bool operator!=(MWAWSubDocument const &doc) const final;
326
327 //! the parser function
328 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
329
330 protected:
331 //! the subdocument id
332 int m_id;
333 };
334
335 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
336 {
337 if (!listener.get()) {
338 MWAW_DEBUG_MSG(("MacWrtParserInternal::SubDocument::parse: no listener\n"));
339 return;
340 }
341 if (m_id != 1 && m_id != 2) {
342 MWAW_DEBUG_MSG(("MacWrtParserInternal::SubDocument::parse: unknown zone\n"));
343 return;
344 }
345 auto *parser=dynamic_cast<MacWrtParser *>(m_parser);
346 if (!parser) {
347 MWAW_DEBUG_MSG(("MacWrtParserInternal::SubDocument::parse: no parser\n"));
348 return;
349 }
350
351 long pos = m_input->tell();
352 parser->sendWindow(m_id);
353 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
354 }
355
356 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
357 {
358 if (MWAWSubDocument::operator!=(doc)) return true;
359 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
360 if (!sDoc) return true;
361 if (m_id != sDoc->m_id) return true;
362 return false;
363 }
364 }
365
366 ////////////////////////////////////////////////////////////
367 // constructor/destructor, ...
368 ////////////////////////////////////////////////////////////
369 MacWrtParser::MacWrtParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
370 : MWAWTextParser(input, rsrcParser, header)
371 , m_state()
372 {
373 init();
374 }
375
376 MacWrtParser::~MacWrtParser()
377 {
378 }
379
380 void MacWrtParser::init()
381 {
382 resetTextListener();
383 setAsciiName("main-1");
384
385 m_state.reset(new MacWrtParserInternal::State);
386
387 // reduce the margin (in case, the page is not defined)
388 getPageSpan().setMargins(0.1);
389 }
390
391 ////////////////////////////////////////////////////////////
392 // new page
393 ////////////////////////////////////////////////////////////
394 void MacWrtParser::newPage(int number)
395 {
396 if (number <= m_state->m_actPage || number > m_state->m_numPages)
397 return;
398
399 while (m_state->m_actPage < number) {
400 m_state->m_actPage++;
401 if (!getTextListener() || m_state->m_actPage == 1)
402 continue;
403 getTextListener()->insertBreak(MWAWTextListener::PageBreak);
404 }
405 }
406
407 ////////////////////////////////////////////////////////////
408 // the parser
409 ////////////////////////////////////////////////////////////
410 void MacWrtParser::parse(librevenge::RVNGTextInterface *docInterface)
411 {
412 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
413 bool ok = true;
414 try {
415 // create the asciiFile
416 ascii().setStream(getInput());
417 ascii().open(asciiName());
418 checkHeader(nullptr);
419 if (getRSRCParser()) {
420 MWAWEntry corrEntry = getRSRCParser()->getEntry("STR ", 700);
421 std::string corrString("");
422 if (corrEntry.valid() && getRSRCParser()->parseSTR(corrEntry, corrString)) {
423 if (corrString.length() != 15) {
424 MWAW_DEBUG_MSG(("MacWrtParser::parse: resource correspondance string seems bad\n"));
425 }
426 else
427 m_state->m_compressCorr = corrString;
428 }
429 }
430 ok = (version() <= 3) ? createZonesV3() : createZones();
431 if (ok) {
432 createDocument(docInterface);
433 sendWindow(0);
434 }
435
436 ascii().reset();
437 }
438 catch (...) {
439 MWAW_DEBUG_MSG(("MacWrtParser::parse: exception catched when parsing\n"));
440 ok = false;
441 }
442
443 resetTextListener();
444 if (!ok) throw(libmwaw::ParseException());
445 }
446
447 ////////////////////////////////////////////////////////////
448 // create the document
449 ////////////////////////////////////////////////////////////
450 void MacWrtParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
451 {
452 if (!documentInterface) return;
453 if (getTextListener()) {
454 MWAW_DEBUG_MSG(("MacWrtParser::createDocument: listener already exist\n"));
455 return;
456 }
457
458 // update the page
459 m_state->m_actPage = 0;
460
461 // create the page list
462 MWAWPageSpan ps(getPageSpan());
463 for (int i = 1; i < 3; i++) {
464 if (m_state->m_windows[i].isEmpty()) {
465 #ifdef DEBUG
466 sendWindow(i); // force the parsing
467 #endif
468 continue;
469 }
470 MWAWHeaderFooter hF((i==1) ? MWAWHeaderFooter::HEADER : MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
471 hF.m_subDocument.reset(new MacWrtParserInternal::SubDocument(*this, getInput(), i));
472 ps.setHeaderFooter(hF);
473 }
474
475 std::vector<MWAWPageSpan> pageList;
476 if (m_state->m_fileHeader.m_hideFirstPageHeaderFooter) {
477 pageList.push_back(getPageSpan());
478 ps.setPageSpan(m_state->m_numPages);
479 }
480 else
481 ps.setPageSpan(m_state->m_numPages+1);
482 if (ps.getPageSpan())
483 pageList.push_back(ps);
484 //
485 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
486 setTextListener(listen);
487 listen->startDocument();
488 }
489
490
491 ////////////////////////////////////////////////////////////
492 //
493 // Intermediate level
494 //
495 ////////////////////////////////////////////////////////////
496 bool MacWrtParser::createZones()
497 {
498 MWAWInputStreamPtr input = getInput();
499 long pos = input->tell();
500
501 if (!readPrintInfo()) {
502 // bad sign, but we can try to recover
503 ascii().addPos(pos);
504 ascii().addNote("###PrintInfo");
505 input->seek(pos+0x78, librevenge::RVNG_SEEK_SET);
506 }
507
508 pos = input->tell();
509 for (int i = 0; i < 3; i++) {
510 if (readWindowsInfo(i))
511 continue;
512 if (i == 2) return false; // problem on the main zone, better quit
513
514 // reset state
515 m_state->m_windows[2-i] = MacWrtParserInternal::WindowsInfo();
516 int const windowsSize = 46;
517
518 // and try to continue
519 input->seek(pos+(i+1)*windowsSize, librevenge::RVNG_SEEK_SET);
520 }
521
522 #ifdef DEBUG
523 checkFreeList();
524 #endif
525
526 // ok, we can find calculate the number of pages and the header and the footer height
527 for (int i = 1; i < 3; i++) {
528 auto const &info = m_state->m_windows[i];
529 if (info.isEmpty()) // avoid reserving space for empty header/footer
530 continue;
531 int height = 0;
532 for (auto const &inf : info.m_informations)
533 height+=inf.m_height;
534 if (i == 1) m_state->m_headerHeight = height;
535 else m_state->m_footerHeight = height;
536 }
537 int numPages = 0;
538 auto const &mainInfo = m_state->m_windows[0];
539 for (auto &info : mainInfo.m_informations) {
540 if (info.m_pos.page() > numPages)
541 numPages = info.m_pos.page();
542 }
543 m_state->m_numPages = numPages+1;
544
545 return true;
546 }
547
548 bool MacWrtParser::createZonesV3()
549 {
550 MWAWInputStreamPtr input = getInput();
551 long pos = input->tell();
552
553 if (!readPrintInfo()) {
554 // bad sign, but we can try to recover
555 ascii().addPos(pos);
556 ascii().addNote("###PrintInfo");
557 input->seek(pos+0x78, librevenge::RVNG_SEEK_SET);
558 }
559
560 pos = input->tell();
561 for (int i = 0; i < 3; i++) {
562 if (readWindowsInfo(i))
563 continue;
564 if (i == 2) return false; // problem on the main zone, better quit
565
566 // reset state
567 m_state->m_windows[2-i] = MacWrtParserInternal::WindowsInfo();
568 int const windowsSize = 34;
569
570 // and try to continue
571 input->seek(pos+(i+1)*windowsSize, librevenge::RVNG_SEEK_SET);
572 }
573
574 auto const &header = m_state->m_fileHeader;
575
576 for (int i = 0; i < 3; i++) {
577 if (!readInformationsV3
578 (header.m_numParagraphs[i], m_state->m_windows[i].m_informations))
579 return false;
580 }
581 if (int(input->tell()) != header.m_dataPos) {
582 MWAW_DEBUG_MSG(("MacWrtParser::createZonesV3: pb with dataPos\n"));
583 ascii().addPos(input->tell());
584 ascii().addNote("###FileHeader");
585
586 // posibility to do very bad thing from here, so we stop
587 if (int(input->tell()) > header.m_dataPos)
588 return false;
589
590 // and try to continue
591 input->seek(header.m_dataPos, librevenge::RVNG_SEEK_SET);
592 if (int(input->tell()) != header.m_dataPos)
593 return false;
594 }
595 for (int z = 0; z < 3; z++) {
596 int numParag = header.m_numParagraphs[z];
597 auto &wInfo = m_state->m_windows[z];
598 for (int p = 0; p < numParag; p++) {
599 pos = input->tell();
600 auto type = static_cast<int>(input->readLong(2));
601 auto sz = static_cast<int>(input->readLong(2));
602 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
603 if (sz < 0 || long(input->tell()) != pos+4+sz) {
604 MWAW_DEBUG_MSG(("MacWrtParser::createZonesV3: pb with dataZone\n"));
605 return (p != 0);
606 }
607 MWAWEntry entry;
608 entry.setBegin(pos+4);
609 entry.setLength(sz);
610 if (int(wInfo.m_informations.size()) <= p)
611 continue;
612 wInfo.m_informations[size_t(p)].m_data = entry;
613 auto newType = MacWrtParserInternal::Information::UNKNOWN;
614
615 switch ((type & 0x7)) {
616 case 0:
617 newType=MacWrtParserInternal::Information::RULER;
618 break;
619 case 1:
620 newType=MacWrtParserInternal::Information::TEXT;
621 break;
622 case 2:
623 newType=MacWrtParserInternal::Information::PAGEBREAK;
624 break;
625 default:
626 break;
627 }
628 if (newType != wInfo.m_informations[size_t(p)].m_type) {
629 MWAW_DEBUG_MSG(("MacWrtParser::createZonesV3: types are inconstant\n"));
630 if (newType != MacWrtParserInternal::Information::UNKNOWN)
631 wInfo.m_informations[size_t(p)].m_type = newType;
632 }
633 }
634 }
635 if (!input->isEnd()) {
636 ascii().addPos(input->tell());
637 ascii().addNote("Entries(END)");
638 }
639
640 int numPages = 0;
641 auto const &mainInfo = m_state->m_windows[0];
642 for (auto const &info : mainInfo.m_informations) {
643 if (info.m_pos.page() > numPages)
644 numPages = info.m_pos.page();
645 }
646 m_state->m_numPages = numPages+1;
647 return true;
648 }
649
650 bool MacWrtParser::sendWindow(int zone)
651 {
652 if (zone < 0 || zone >= 3) {
653 MWAW_DEBUG_MSG(("MacWrtParser::sendWindow: invalid zone %d\n", zone));
654 return false;
655 }
656
657 auto const &info = m_state->m_windows[zone];
658 size_t numInfo = info.m_informations.size();
659 auto numPara = int(info.m_firstParagLine.size());
660
661 if (version() <= 3 && zone == 0)
662 newPage(1);
663 for (size_t i=0; i < numInfo; i++) {
664 if (zone == 0)
665 newPage(info.m_informations[i].m_pos.page()+1);
666 switch (info.m_informations[i].m_type) {
667 case MacWrtParserInternal::Information::TEXT:
668 if (!zone || info.m_informations[i].m_data.length() != 10) {
669 std::vector<int> lineHeight;
670 if (int(i) < numPara) {
671 int firstLine = info.m_firstParagLine[i];
672 int lastLine = (int(i+1) < numPara) ? info.m_firstParagLine[i+1] : int(info.m_linesHeight.size());
673 for (int line = firstLine; line < lastLine; line++)
674 lineHeight.push_back(info.m_linesHeight[size_t(line)]);
675 }
676 readText(info.m_informations[i], lineHeight);
677 }
678 break;
679 case MacWrtParserInternal::Information::RULER:
680 readParagraph(info.m_informations[i]);
681 break;
682 case MacWrtParserInternal::Information::GRAPHIC:
683 readGraphic(info.m_informations[i]);
684 break;
685 case MacWrtParserInternal::Information::PAGEBREAK:
686 readPageBreak(info.m_informations[i]);
687 if (zone == 0 && version() <= 3)
688 newPage(info.m_informations[i].m_pos.page()+2);
689 break;
690 case MacWrtParserInternal::Information::UNKNOWN:
691 #if !defined(__clang__)
692 default:
693 #endif
694 break;
695 }
696 }
697 if (getTextListener() && zone) {
698 // FIXME: try to insert field in the good place
699 if (info.m_pageNumber.x() >= 0 && info.m_pageNumber.y() >= 0)
700 getTextListener()->insertField(MWAWField(MWAWField::PageNumber));
701 if (info.m_date.x() >= 0 && info.m_date.y() >= 0)
702 getTextListener()->insertField(MWAWField(MWAWField::Date));
703 if (info.m_time.x() >= 0 && info.m_time.y() >= 0)
704 getTextListener()->insertField(MWAWField(MWAWField::Time));
705 }
706 return true;
707 }
708
709 ////////////////////////////////////////////////////////////
710 //
711 // Low level
712 //
713 ////////////////////////////////////////////////////////////
714
715 ////////////////////////////////////////////////////////////
716 // read the header
717 ////////////////////////////////////////////////////////////
718 bool MacWrtParser::checkHeader(MWAWHeader *header, bool /*strict*/)
719 {
720 *m_state = MacWrtParserInternal::State();
721 MacWrtParserInternal::FileHeader fHeader = m_state->m_fileHeader;
722
723 MWAWInputStreamPtr input = getInput();
724 if (!input || !input->hasDataFork())
725 return false;
726
727 libmwaw::DebugStream f;
728 int headerSize=40;
729 if (!input->checkPosition(headerSize)) {
730 MWAW_DEBUG_MSG(("MacWrtParser::checkHeader: file is too short\n"));
731 return false;
732 }
733 input->seek(0,librevenge::RVNG_SEEK_SET);
734
735 auto vers = static_cast<int>(input->readULong(2));
736 setVersion(vers);
737
738 std::string vName("");
739
740 switch (vers) {
741 case 3:
742 vName="v1.0-2.2";
743 break;
744 case 6: // version 4.5 ( also version 5.01 of Claris MacWrite )
745 vName="v4.5-5.01";
746 break;
747 default:
748 MWAW_DEBUG_MSG(("MacWrtParser::checkHeader: unknown version\n"));
749 return false;
750 }
751 if (vName.empty()) {
752 MWAW_DEBUG_MSG(("Maybe a MacWrite file unknown version(%d)\n", vers));
753 }
754 else {
755 MWAW_DEBUG_MSG(("MacWrite file %s\n", vName.c_str()));
756 }
757
758 f << "FileHeader: vers=" << vers << ",";
759
760 if (vers <= 3) fHeader.m_dataPos = static_cast<int>(input->readULong(2));
761
762 for (int &numParagraph : fHeader.m_numParagraphs) {
763 auto numParag = static_cast<int>(input->readLong(2));
764 numParagraph = numParag;
765 if (numParag < 0) {
766 MWAW_DEBUG_MSG(("MacWrtParser::checkHeader: numParagraphs is negative : %d\n",
767 numParag));
768 return false;
769 }
770 }
771
772 if (vers <= 3) {
773 input->seek(6, librevenge::RVNG_SEEK_CUR); // unknown
774 if (input->readLong(1)) f << "hasFooter(?);";
775 if (input->readLong(1)) f << "hasHeader(?),";
776 fHeader.m_startNumberPage = static_cast<int>(input->readLong(2));
777 headerSize=20;
778 }
779 else {
780 fHeader.m_hideFirstPageHeaderFooter = (input->readULong(1)==0xFF);
781
782 input->seek(7, librevenge::RVNG_SEEK_CUR); // unused + 4 display flags + active doc
783 fHeader.m_startNumberPage = static_cast<int>(input->readLong(2));
784 fHeader.m_freeListPos = long(input->readULong(4));
785 fHeader.m_freeListLength = static_cast<int>(input->readULong(2));
786 fHeader.m_freeListAllocated = static_cast<int>(input->readULong(2));
787 // 14 unused
788 }
789 f << fHeader;
790
791 //
792 input->seek(headerSize, librevenge::RVNG_SEEK_SET);
793 if (!readPrintInfo()) {
794 input->seek(headerSize, librevenge::RVNG_SEEK_SET);
795 if (input->readLong(2)) return false; // allow iPrVersion to be zero
796 input->seek(headerSize+0x78, librevenge::RVNG_SEEK_SET);
797 for (int i=0; i<3; ++i)
798 if (!readWindowsInfo(i) && i==2) return false;
799 }
800 if (!input->checkPosition(vers <= 3 ? fHeader.m_dataPos : fHeader.m_freeListPos))
801 return false;
802
803 input->seek(headerSize, librevenge::RVNG_SEEK_SET);
804 m_state->m_fileHeader = fHeader;
805
806 // ok, we can finish initialization
807 if (header)
808 header->reset(MWAWDocument::MWAW_T_MACWRITE, version());
809
810 ascii().addPos(0);
811 ascii().addNote(f.str().c_str());
812 ascii().addPos(headerSize);
813
814 return true;
815 }
816
817 ////////////////////////////////////////////////////////////
818 // read the print info
819 ////////////////////////////////////////////////////////////
820 bool MacWrtParser::readPrintInfo()
821 {
822 MWAWInputStreamPtr input = getInput();
823 long pos = input->tell();
824 libmwaw::DebugStream f;
825 // print info
826 libmwaw::PrinterInfo info;
827 if (!info.read(input)) return false;
828 f << "Entries(PrintInfo):"<< info;
829
830 MWAWVec2i paperSize = info.paper().size();
831 MWAWVec2i pageSize = info.page().size();
832 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
833 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
834
835 // define margin from print info
836 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
837 MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1);
838
839 // move margin left | top
840 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
841 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
842 lTopMargin -= MWAWVec2i(decalX, decalY);
843 rBotMargin += MWAWVec2i(decalX, decalY);
844
845 // decrease right | bottom
846 int rightMarg = rBotMargin.x() -10;
847 if (rightMarg < 0) rightMarg=0;
848 int botMarg = rBotMargin.y() -50;
849 if (botMarg < 0) botMarg=0;
850
851 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
852 getPageSpan().setMarginBottom(botMarg/72.0);
853 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
854 getPageSpan().setMarginRight(rightMarg/72.0);
855 getPageSpan().setFormLength(paperSize.y()/72.);
856 getPageSpan().setFormWidth(paperSize.x()/72.);
857
858 ascii().addPos(pos);
859 ascii().addNote(f.str().c_str());
860 input->seek(pos+0x78, librevenge::RVNG_SEEK_SET);
861 if (long(input->tell()) != pos+0x78) {
862 MWAW_DEBUG_MSG(("MacWrtParser::readPrintInfo: file is too short\n"));
863 return false;
864 }
865 ascii().addPos(input->tell());
866
867 return true;
868 }
869
870 ////////////////////////////////////////////////////////////
871 // read the windows info
872 ////////////////////////////////////////////////////////////
873 bool MacWrtParser::readWindowsInfo(int wh)
874 {
875 MWAWInputStreamPtr input = getInput();
876 long pos = input->tell();
877 int windowsSize = version() <= 3 ? 34 : 46;
878
879 input->seek(pos+windowsSize, librevenge::RVNG_SEEK_SET);
880 if (long(input->tell()) !=pos+windowsSize) {
881 MWAW_DEBUG_MSG(("MacWrtParser::readWindowsInfo: file is too short\n"));
882 return false;
883 }
884
885 input->seek(pos, librevenge::RVNG_SEEK_SET);
886 libmwaw::DebugStream f;
887 f << "Entries(Windows)";
888 switch (wh) {
889 case 0:
890 f << "[Footer]";
891 break;
892 case 1:
893 f << "[Header]";
894 break;
895 case 2:
896 break;
897 default:
898 MWAW_DEBUG_MSG(("MacWrtParser::readWindowsInfo: called with bad which=%d\n",wh));
899 return false;
900 }
901
902 int which = 2-wh;
903 auto &info = m_state->m_windows[which];
904 f << ": ";
905
906 MWAWEntry informations;
907 MWAWEntry lineHeightEntry;
908
909 for (int i = 0; i < 2; i++) {
910 auto x = static_cast<int>(input->readLong(2));
911 auto y = static_cast<int>(input->readLong(2));
912 if (i == 0) info.m_startSel = MWAWVec2i(x,y);
913 else info.m_endSel = MWAWVec2i(x,y);
914 }
915
916 if (version() <= 3) {
917 for (int i = 0; i < 2; i++) {
918 auto val = static_cast<int>(input->readLong(2));
919 if (val) f << "unkn" << i << "=" << val << ",";
920 }
921 }
922 else {
923 info.m_posTopY = static_cast<int>(input->readLong(2));
924 input->seek(2,librevenge::RVNG_SEEK_CUR); // need to redraw
925 informations.setBegin(long(input->readULong(4)));
926 informations.setLength(long(input->readULong(2)));
927 informations.setId(which);
928
929 lineHeightEntry.setBegin(long(input->readULong(4)));
930 lineHeightEntry.setLength(long(input->readULong(2)));
931 lineHeightEntry.setId(which);
932
933 f << std::hex
934 << "lineHeight=[" << lineHeightEntry.begin() << "-" << lineHeightEntry.end() << "],"
935 << "informations=[" << informations.begin() << "-" << informations.end() << "],"
936 << std::dec;
937 }
938 for (int i = 0; i < 3; i++) {
939 auto x = static_cast<int>(input->readLong(2));
940 auto y = static_cast<int>(input->readLong(2));
941 if (i == 0) info.m_pageNumber = MWAWVec2i(x,y);
942 else if (i == 1) info.m_date = MWAWVec2i(x,y);
943 else info.m_time = MWAWVec2i(x,y);
944 }
945 f << info;
946 bool ok=true;
947 if (version() <= 3) {
948 input->seek(6,librevenge::RVNG_SEEK_CUR); // unknown flags: ff ff ff ff ff 00
949 f << "actFont=" << input->readLong(1) << ",";
950 for (int i= 0; i < 2; i++) {
951 auto val = static_cast<int>(input->readULong(1));
952 if (val==255) f << "f" << i << "=true,";
953 }
954 f << "flg=" << input->readLong(1);
955 }
956 else {
957 input->seek(4,librevenge::RVNG_SEEK_CUR); // unused
958 if (input->readULong(1) == 0xFF) f << "redrawOval,";
959 if (input->readULong(1) == 0xFF) f << "lastOvalUpdate,";
960 f << "actStyle=" << input->readLong(2) << ",";
961 f << "actFont=" << input->readLong(2);
962
963 if (!readLinesHeight(lineHeightEntry, info.m_firstParagLine, info.m_linesHeight)) {
964 // ok, try to continue without lineHeight
965 info.m_firstParagLine.resize(0);
966 info.m_linesHeight.resize(0);
967 }
968 ok = readInformations(informations, info.m_informations);
969 if (!ok) info.m_informations.resize(0);
970 }
971
972 input->seek(pos+windowsSize, librevenge::RVNG_SEEK_SET);
973 ascii().addPos(pos);
974 ascii().addNote(f.str().c_str());
975 ascii().addPos(input->tell());
976
977 return ok;
978 }
979
980 ////////////////////////////////////////////////////////////
981 // read the lines height
982 ////////////////////////////////////////////////////////////
983 bool MacWrtParser::readLinesHeight(MWAWEntry const &entry, std::vector<int> &firstParagLine, std::vector<int> &linesHeight)
984 {
985 firstParagLine.resize(0);
986 linesHeight.resize(0);
987
988 if (!entry.valid()) return false;
989
990 MWAWInputStreamPtr input = getInput();
991
992 input->seek(entry.end()-1, librevenge::RVNG_SEEK_SET);
993 if (long(input->tell()) != entry.end()-1) {
994 MWAW_DEBUG_MSG(("MacWrtParser::readLinesHeight: file is too short\n"));
995 return false;
996 }
997
998 long pos = entry.begin(), endPos = entry.end();
999 input->seek(pos, librevenge::RVNG_SEEK_SET);
1000
1001 libmwaw::DebugStream f;
1002 int numParag=0;
1003 while (input->tell() != endPos) {
1004 pos = input->tell();
1005 auto sz = static_cast<int>(input->readULong(2));
1006 if (pos+sz+2 > endPos) {
1007 MWAW_DEBUG_MSG(("MacWrtParser::readLinesHeight: find odd line\n"));
1008 ascii().addPos(pos);
1009 ascii().addNote("Entries(LineHeight):###");
1010 return false;
1011 }
1012
1013 firstParagLine.push_back(int(linesHeight.size()));
1014 int actHeight = 0;
1015 bool heightOk = false;
1016 f.str("");
1017 f << "Entries(LineHeight)[" << entry.id() << "-" << ++numParag << "]:";
1018 for (int c = 0; c < sz; c++) {
1019 auto val = static_cast<int>(input->readULong(1));
1020 if (val & 0x80) {
1021 val &= 0x7f;
1022 if (!heightOk || val==0) {
1023 MWAW_DEBUG_MSG(("MacWrtParser::readLinesHeight: find factor without height \n"));
1024 return false;
1025 }
1026
1027 for (int i = 0; i < val-1; i++)
1028 linesHeight.push_back(actHeight);
1029 if (val != 0x7f) heightOk = false;
1030 f << "x" << val;
1031 continue;
1032 }
1033 actHeight = val;
1034 linesHeight.push_back(actHeight);
1035 heightOk = true;
1036 if (c) f << ",";
1037 f << actHeight;
1038 }
1039
1040 ascii().addPos(pos);
1041 ascii().addNote(f.str().c_str());
1042
1043 if ((sz%2)==1) sz++;
1044 input->seek(pos+sz+2, librevenge::RVNG_SEEK_SET);
1045 }
1046 firstParagLine.push_back(int(linesHeight.size()));
1047
1048 ascii().addPos(endPos);
1049 ascii().addNote("_");
1050 return true;
1051 }
1052
1053 ////////////////////////////////////////////////////////////
1054 // read the entries
1055 ////////////////////////////////////////////////////////////
1056 bool MacWrtParser::readInformationsV3(int numEntries, std::vector<MacWrtParserInternal::Information> &informations)
1057 {
1058 informations.resize(0);
1059
1060 if (numEntries < 0) return false;
1061 if (numEntries == 0) return true;
1062
1063 MWAWInputStreamPtr input = getInput();
1064
1065 libmwaw::DebugStream f;
1066 for (int i = 0; i < numEntries; i++) {
1067 long pos = input->tell();
1068 MacWrtParserInternal::Information info;
1069 f.str("");
1070 f << "Entries(Information)[" << i+1 << "]:";
1071 auto height = static_cast<int>(input->readLong(2));
1072 info.m_height = height;
1073 if (info.m_height < 0) {
1074 info.m_height = 0;
1075 info.m_type = MacWrtParserInternal::Information::PAGEBREAK;
1076 }
1077 else if (info.m_height > 0)
1078 info.m_type = MacWrtParserInternal::Information::TEXT;
1079 else
1080 info.m_type = MacWrtParserInternal::Information::RULER;
1081
1082 auto y = static_cast<int>(input->readLong(2));
1083 info.m_pos=MWAWPosition(MWAWVec2f(0,float(y)), MWAWVec2f(0, float(height)), librevenge::RVNG_POINT);
1084 info.m_pos.setPage(static_cast<int>(input->readLong(1)));
1085 f << info;
1086 informations.push_back(info);
1087
1088 f << "unkn1=" << std::hex << input->readULong(2) << std::dec << ",";
1089 f << "unkn2=" << std::hex << input->readULong(1) << std::dec << ",";
1090 ascii().addPos(pos);
1091 ascii().addNote(f.str().c_str());
1092 }
1093 ascii().addPos(input->tell());
1094 ascii().addNote("_");
1095
1096 return true;
1097 }
1098
1099 ////////////////////////////////////////////////////////////
1100 // read the entries
1101 ////////////////////////////////////////////////////////////
1102 bool MacWrtParser::readInformations(MWAWEntry const &entry, std::vector<MacWrtParserInternal::Information> &informations)
1103 {
1104 informations.resize(0);
1105
1106 if (!entry.valid()) return false;
1107
1108 MWAWInputStreamPtr input = getInput();
1109
1110 input->seek(entry.end()-1, librevenge::RVNG_SEEK_SET);
1111 if (long(input->tell()) != entry.end()-1) {
1112 MWAW_DEBUG_MSG(("MacWrtParser::readInformations: file is too short\n"));
1113 return false;
1114 }
1115
1116 long pos = entry.begin(), endPos = entry.end();
1117 if ((endPos-pos)%16) {
1118 MWAW_DEBUG_MSG(("MacWrtParser::readInformations: entry size is odd\n"));
1119 return false;
1120 }
1121 auto numEntries = int((endPos-pos)/16);
1122 libmwaw::DebugStream f;
1123
1124 input->seek(pos, librevenge::RVNG_SEEK_SET);
1125 for (int i = 0; i < numEntries; i++) {
1126 pos = input->tell();
1127
1128 f.str("");
1129 f << "Entries(Information)[" << entry.id() << "-" << i+1 << "]:";
1130 MacWrtParserInternal::Information info;
1131 auto height = static_cast<int>(input->readLong(2));
1132 if (height < 0) {
1133 info.m_type = MacWrtParserInternal::Information::GRAPHIC;
1134 height *= -1;
1135 }
1136 else if (height == 0)
1137 info.m_type = MacWrtParserInternal::Information::RULER;
1138 else
1139 info.m_type = MacWrtParserInternal::Information::TEXT;
1140 info.m_height = height;
1141
1142 auto y = static_cast<int>(input->readLong(2));
1143 auto page = static_cast<int>(input->readULong(1));
1144 input->seek(3, librevenge::RVNG_SEEK_CUR); // unused
1145 info.m_pos = MWAWPosition(MWAWVec2f(0,float(y)), MWAWVec2f(0, float(height)), librevenge::RVNG_POINT);
1146 info.m_pos.setPage(page);
1147
1148 auto paragStatus = static_cast<int>(input->readULong(1));
1149 switch (paragStatus & 0x3) {
1150 case 0:
1151 info.m_justify = MWAWParagraph::JustificationLeft;
1152 break;
1153 case 1:
1154 info.m_justify = MWAWParagraph::JustificationCenter;
1155 break;
1156 case 2:
1157 info.m_justify = MWAWParagraph::JustificationRight;
1158 break;
1159 case 3:
1160 info.m_justify = MWAWParagraph::JustificationFull;
1161 break;
1162 default:
1163 break;
1164 }
1165 info.m_compressed = (paragStatus & 0x8);
1166 info.m_justifySet = (paragStatus & 0x20);
1167
1168 // other bits used internally
1169 auto highPos = static_cast<unsigned int>(input->readULong(1));
1170 info.m_data.setBegin(long(highPos<<16)+long(input->readULong(2)));
1171 info.m_data.setLength(long(input->readULong(2)));
1172
1173 auto paragFormat = static_cast<int>(input->readULong(2));
1174 uint32_t flags = 0;
1175 // bit 1 = plain
1176 if (paragFormat&0x2) flags |= MWAWFont::boldBit;
1177 if (paragFormat&0x4) flags |= MWAWFont::italicBit;
1178 if (paragFormat&0x8) info.m_font.setUnderlineStyle(MWAWFont::Line::Simple);
1179 if (paragFormat&0x10) flags |= MWAWFont::embossBit;
1180 if (paragFormat&0x20) flags |= MWAWFont::shadowBit;
1181 if (paragFormat&0x40)
1182 info.m_font.set(MWAWFont::Script::super100());
1183 if (paragFormat&0x80)
1184 info.m_font.set(MWAWFont::Script::sub100());
1185 info.m_font.setFlags(flags);
1186
1187 int fontSize = 0;
1188 switch ((paragFormat >> 8) & 7) {
1189 case 0:
1190 break;
1191 case 1:
1192 fontSize=9;
1193 break;
1194 case 2:
1195 fontSize=10;
1196 break;
1197 case 3:
1198 fontSize=12;
1199 break;
1200 case 4:
1201 fontSize=14;
1202 break;
1203 case 5:
1204 fontSize=18;
1205 break;
1206 case 6:
1207 fontSize=14;
1208 break;
1209 default: // rare, but can appears on some empty lines
1210 MWAW_DEBUG_MSG(("MacWrtParser::readInformations: unknown font size=7\n"));
1211 f << "##fSize=7,";
1212 }
1213 if (fontSize) info.m_font.setSize(float(fontSize));
1214 if ((paragFormat >> 11)&0x1F) info.m_font.setId((paragFormat >> 11)&0x1F);
1215
1216 informations.push_back(info);
1217 f << info;
1218 #ifdef DEBUG
1219 f << "font=[" << info.m_font.getDebugString(getFontConverter()) << "]";
1220 #endif
1221
1222 input->seek(pos+16, librevenge::RVNG_SEEK_SET);
1223 ascii().addPos(pos);
1224 ascii().addNote(f.str().c_str());
1225 }
1226
1227 ascii().addPos(endPos);
1228 ascii().addNote("_");
1229 return true;
1230 }
1231
1232 ////////////////////////////////////////////////////////////
1233 // read a text
1234 ////////////////////////////////////////////////////////////
1235 bool MacWrtParser::readText(MacWrtParserInternal::Information const &info,
1236 std::vector<int> const &lineHeight)
1237 {
1238 if (!getTextListener()) {
1239 MWAW_DEBUG_MSG(("MacWrtParser::readText: can not find the listener\n"));
1240 return false;
1241 }
1242 MWAWEntry const &entry = info.m_data;
1243 if (!entry.valid()) return false;
1244
1245 MWAWInputStreamPtr input = getInput();
1246 input->seek(entry.end()-1, librevenge::RVNG_SEEK_SET);
1247 if (long(input->tell()) != entry.end()-1) {
1248 MWAW_DEBUG_MSG(("MacWrtParser::readText: file is too short\n"));
1249 return false;
1250 }
1251
1252 long pos = entry.begin();
1253 input->seek(pos, librevenge::RVNG_SEEK_SET);
1254
1255 libmwaw::DebugStream f;
1256 f << "Entries(Text):";
1257
1258 auto numChar = static_cast<int>(input->readULong(2));
1259 std::string text("");
1260 if (!info.m_compressed) {
1261 if (numChar+2 >= entry.length()) {
1262 MWAW_DEBUG_MSG(("MacWrtParser::readText: text is too long\n"));
1263 return false;
1264 }
1265 for (int i = 0; i < numChar; i++)
1266 text += char(input->readULong(1));
1267 }
1268 else {
1269 std::string const &compressCorr = m_state->m_compressCorr;
1270
1271 int actualChar = 0;
1272 bool actualCharSet = false;
1273
1274 for (int i = 0; i < numChar; i++) {
1275 int highByte = 0;
1276 for (int st = 0; st < 3; st++) {
1277 int actVal;
1278 if (!actualCharSet) {
1279 if (long(input->tell()) >= entry.end()) {
1280 MWAW_DEBUG_MSG(("MacWrtParser::readText: text is too long\n"));
1281 return false;
1282 }
1283 actualChar = static_cast<int>(input->readULong(1));
1284 actVal = (actualChar >> 4);
1285 }
1286 else
1287 actVal = (actualChar & 0xf);
1288 actualCharSet = !actualCharSet;
1289 if (st == 0) {
1290 if (actVal == 0xf) continue;
1291 text += compressCorr[size_t(actVal)];
1292 break;
1293 }
1294 if (st == 1) { // high bytes
1295 highByte = (actVal<<4);
1296 continue;
1297 }
1298 text += char(highByte | actVal);
1299 }
1300 }
1301 }
1302 f << "'" << text << "'";
1303
1304 long actPos = input->tell();
1305 if ((actPos-pos)%2==1) {
1306 input->seek(1,librevenge::RVNG_SEEK_CUR);
1307 actPos++;
1308 }
1309
1310 auto formatSize = static_cast<int>(input->readULong(2));
1311 if ((formatSize%6)!=0 || actPos+2+formatSize > entry.end()) {
1312 MWAW_DEBUG_MSG(("MacWrtParser::readText: format is too long\n"));
1313 return false;
1314 }
1315 int numFormat = formatSize/6;
1316
1317 std::vector<int> listPos;
1318 std::vector<MWAWFont> listFonts;
1319
1320 for (int i = 0; i < numFormat; i++) {
1321 auto tPos = static_cast<int>(input->readULong(2));
1322
1323 MWAWFont font;
1324 font.setSize(float(input->readULong(1)));
1325 auto flag = static_cast<int>(input->readULong(1));
1326 uint32_t flags = 0;
1327 // bit 1 = plain
1328 if (flag&0x1) flags |= MWAWFont::boldBit;
1329 if (flag&0x2) flags |= MWAWFont::italicBit;
1330 if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
1331 if (flag&0x8) flags |= MWAWFont::embossBit;
1332 if (flag&0x10) flags |= MWAWFont::shadowBit;
1333 if (flag&0x20) font.set(MWAWFont::Script::super100());
1334 if (flag&0x40) font.set(MWAWFont::Script::sub100());
1335 font.setFlags(flags);
1336 font.setId(static_cast<int>(input->readULong(2)));
1337 listPos.push_back(tPos);
1338 listFonts.push_back(font);
1339 f << ",f" << i << "=[pos=" << tPos;
1340 #ifdef DEBUG
1341 f << ",font=[" << font.getDebugString(getFontConverter()) << "]";
1342 #endif
1343 f << "]";
1344 }
1345
1346 std::vector<int> const *lHeight = &lineHeight;
1347 int totalHeight = info.m_height;
1348 std::vector<int> textLineHeight;
1349 if (version() <= 3) {
1350 std::vector<int> fParagLines;
1351 pos = input->tell();
1352 MWAWEntry hEntry;
1353 hEntry.setBegin(pos);
1354 hEntry.setEnd(entry.end());
1355
1356 if (readLinesHeight(hEntry, fParagLines, textLineHeight)) {
1357 lHeight = &textLineHeight;
1358 totalHeight = 0;
1359 for (auto height : textLineHeight)
1360 totalHeight+=height;
1361 }
1362 else
1363 input->seek(pos, librevenge::RVNG_SEEK_SET);
1364 }
1365 if (long(input->tell()) != entry.end()) {
1366 f << "#badend";
1367 ascii().addDelimiter(input->tell(), '|');
1368 }
1369
1370 if (getTextListener()) {
1371 MWAWParagraph para=getTextListener()->getParagraph();
1372 if (totalHeight && lHeight->size()) // fixme find a way to associate the good size to each line
1373 para.setInterline(totalHeight/double(lHeight->size()), librevenge::RVNG_POINT);
1374 else
1375 para.setInterline(1.2, librevenge::RVNG_PERCENT);
1376 if (info.m_justifySet)
1377 para.m_justify=info.m_justify;
1378 getTextListener()->setParagraph(para);
1379
1380 if (!numFormat || listPos[0] != 0)
1381 getTextListener()->setFont(info.m_font);
1382
1383 int actFormat = 0;
1384 numChar = int(text.length());
1385 for (int i = 0; i < numChar; i++) {
1386 if (actFormat < numFormat && i == listPos[size_t(actFormat)]) {
1387 getTextListener()->setFont(listFonts[size_t(actFormat)]);
1388 actFormat++;
1389 }
1390 auto c = static_cast<unsigned char>(text[size_t(i)]);
1391 if (c == 0x9)
1392 getTextListener()->insertTab();
1393 else if (c == 0xd)
1394 getTextListener()->insertEOL();
1395 else if (c==0x11) // command key (found in some files)
1396 getTextListener()->insertUnicode(0x2318);
1397 else if (c==0x14) // apple logo: check me
1398 getTextListener()->insertUnicode(0xf8ff);
1399 else if (c<0x1f) {
1400 // MacWrite allows to add "invalid" characters in the text
1401 // (and do not display them), this does not imply that the
1402 // file is invalid...
1403 MWAW_DEBUG_MSG(("MacWrtParser::readText: find bad character %d at pos=0x%lx\n", int(c), version()<=3 ? entry.begin()-4 : entry.begin()));
1404 f << "###[" << int(c) << "]";
1405 }
1406 else
1407 getTextListener()->insertCharacter(c);
1408 }
1409 }
1410
1411 ascii().addPos(version()<=3 ? entry.begin()-4 : entry.begin());
1412 ascii().addNote(f.str().c_str());
1413
1414 return true;
1415 }
1416
1417 ////////////////////////////////////////////////////////////
1418 // read a paragraph
1419 ////////////////////////////////////////////////////////////
1420 bool MacWrtParser::readParagraph(MacWrtParserInternal::Information const &info)
1421 {
1422 MWAWEntry const &entry = info.m_data;
1423 if (!entry.valid()) return false;
1424 if (entry.length() != 34) {
1425 MWAW_DEBUG_MSG(("MacWrtParser::readParagraph: size is odd\n"));
1426 return false;
1427 }
1428
1429 MWAWParagraph parag;
1430 MWAWInputStreamPtr input = getInput();
1431
1432 input->seek(entry.end()-1, librevenge::RVNG_SEEK_SET);
1433 if (long(input->tell()) != entry.end()-1) {
1434 MWAW_DEBUG_MSG(("MacWrtParser::readParagraph: file is too short\n"));
1435 return false;
1436 }
1437
1438 long pos = entry.begin();
1439 input->seek(pos, librevenge::RVNG_SEEK_SET);
1440
1441 libmwaw::DebugStream f;
1442 f << "Entries(Paragraph):";
1443
1444 parag.m_margins[1] = double(input->readLong(2))/80.;
1445 parag.m_margins[2] = double(input->readLong(2))/80.;
1446 auto justify = static_cast<int>(input->readLong(1));
1447 switch (justify) {
1448 case 0:
1449 parag.m_justify = MWAWParagraph::JustificationLeft;
1450 break;
1451 case 1:
1452 parag.m_justify = MWAWParagraph::JustificationCenter;
1453 break;
1454 case 2:
1455 parag.m_justify = MWAWParagraph::JustificationRight;
1456 break;
1457 case 3:
1458 parag.m_justify = MWAWParagraph::JustificationFull;
1459 break;
1460 default:
1461 f << "##justify=" << justify << ",";
1462 break;
1463 }
1464 auto numTabs = static_cast<int>(input->readLong(1));
1465 if (numTabs < 0 || numTabs > 10) {
1466 f << "##numTabs=" << numTabs << ",";
1467 numTabs = 0;
1468 }
1469 auto highspacing = static_cast<int>(input->readULong(1));
1470 if (highspacing==0x80) // 6 by line
1471 parag.setInterline(12, librevenge::RVNG_POINT);
1472 else if (highspacing) {
1473 f << "##highSpacing=" << std::hex << highspacing << std::dec << ",";
1474 MWAW_DEBUG_MSG(("MacWrtParser::readParagraph: high spacing bit set=%d\n", highspacing));
1475 }
1476 auto spacing = static_cast<int>(input->readLong(1));
1477 if (spacing < 0)
1478 f << "#interline=" << 1.+spacing/2.0 << ",";
1479 else if (spacing)
1480 parag.setInterline(1.+spacing/2.0, librevenge::RVNG_PERCENT);
1481 parag.m_margins[0] = double(input->readLong(2))/80.;
1482
1483 parag.m_tabs->resize(size_t(numTabs));
1484 for (size_t i = 0; i < size_t(numTabs); i++) {
1485 auto numPixel = static_cast<int>(input->readLong(2));
1486 auto align = MWAWTabStop::LEFT;
1487 if (numPixel < 0) {
1488 align = MWAWTabStop::DECIMAL;
1489 numPixel *= -1;
1490 }
1491 (*parag.m_tabs)[i].m_alignment = align;
1492 (*parag.m_tabs)[i].m_position = numPixel/72.0;
1493 }
1494 *(parag.m_margins[0]) -= parag.m_margins[1].get();
1495 if (parag.m_margins[2].get() > 0.0)
1496 parag.m_margins[2]=getPageWidth()-parag.m_margins[2].get()-1.0;
1497 if (parag.m_margins[2].get() < 0) parag.m_margins[2] = 0;
1498 f << parag;
1499
1500 if (getTextListener())
1501 getTextListener()->setParagraph(parag);
1502 ascii().addPos(version()<=3 ? pos-4 : pos);
1503 ascii().addNote(f.str().c_str());
1504
1505 return true;
1506 }
1507
1508 ////////////////////////////////////////////////////////////
1509 // read the page break
1510 ////////////////////////////////////////////////////////////
1511 bool MacWrtParser::readPageBreak(MacWrtParserInternal::Information const &info)
1512 {
1513 MWAWEntry const &entry = info.m_data;
1514 if (!entry.valid()) return false;
1515 if (entry.length() != 21) {
1516 MWAW_DEBUG_MSG(("MacWrtParser::readPageBreak: size is odd\n"));
1517 return false;
1518 }
1519
1520 MWAWParagraph parag;
1521 MWAWInputStreamPtr input = getInput();
1522
1523 input->seek(entry.end()-1, librevenge::RVNG_SEEK_SET);
1524 if (long(input->tell()) != entry.end()-1) {
1525 MWAW_DEBUG_MSG(("MacWrtParser::readPageBreak: file is too short\n"));
1526 return false;
1527 }
1528
1529 long pos = entry.begin();
1530 input->seek(pos, librevenge::RVNG_SEEK_SET);
1531
1532 libmwaw::DebugStream f;
1533
1534 f << "Entries(PageBreak):";
1535 for (int i = 0; i < 2; i++) {
1536 auto val = static_cast<int>(input->readLong(2));
1537 if (val) f << "f" << i << "=" << val << ",";
1538 }
1539 int dim[2]= {0,0};
1540 for (auto &d : dim) d = static_cast<int>(input->readLong(2));
1541 f << "pageSize(?)=" << dim[0] << "x" << dim[1] << ",";
1542 f << "unk=" << input->readLong(2) << ","; // find 0xd
1543
1544 // find MAGICPIC
1545 std::string name("");
1546 for (int i = 0; i < 8; i++)
1547 name += char(input->readULong(1));
1548 f << name << ",";
1549 // then I find 1101ff: end of quickdraw pict1 ?
1550 ascii().addPos(version()<=3 ? pos-4 : pos);
1551 ascii().addNote(f.str().c_str());
1552
1553 return true;
1554 }
1555
1556 ////////////////////////////////////////////////////////////
1557 // read a graphic
1558 ////////////////////////////////////////////////////////////
1559 bool MacWrtParser::readGraphic(MacWrtParserInternal::Information const &info)
1560 {
1561 MWAWEntry const &entry = info.m_data;
1562 if (!entry.valid()) return false;
1563
1564 if (entry.length() < 12) {
1565 MWAW_DEBUG_MSG(("MacWrtParser::readGraphic: file is too short\n"));
1566 return false;
1567 }
1568
1569 MWAWInputStreamPtr input = getInput();
1570
1571 input->seek(entry.end()-1, librevenge::RVNG_SEEK_SET);
1572 if (long(input->tell()) != entry.end()-1) {
1573 MWAW_DEBUG_MSG(("MacWrtParser::readGraphic: file is too short\n"));
1574 return false;
1575 }
1576 long pos = entry.begin();
1577 input->seek(pos, librevenge::RVNG_SEEK_SET);
1578
1579 int dim[4];
1580 for (auto &d : dim) d = static_cast<int>(input->readLong(2));
1581 if (dim[2] < dim[0] || dim[3] < dim[1]) {
1582 MWAW_DEBUG_MSG(("MacWrtParser::readGraphic: bdbox is bad\n"));
1583 return false;
1584 }
1585 libmwaw::DebugStream f;
1586 f << "Entries(Graphic):";
1587
1588 MWAWBox2f box;
1589 auto res = MWAWPictData::check(input, int(entry.length()-8), box);
1590 if (res == MWAWPict::MWAW_R_BAD) {
1591 MWAW_DEBUG_MSG(("MacWrtParser::readGraphic: can not find the picture\n"));
1592 return false;
1593 }
1594
1595
1596 MWAWVec2f actualSize(float(dim[3]-dim[1]), float(dim[2]-dim[0])), naturalSize(actualSize);
1597 if (box.size().x() > 0 && box.size().y() > 0) naturalSize = box.size();
1598 MWAWPosition pictPos=MWAWPosition(MWAWVec2f(0,0),actualSize, librevenge::RVNG_POINT);
1599 pictPos.setRelativePosition(MWAWPosition::Char);
1600 pictPos.setNaturalSize(naturalSize);
1601 f << pictPos;
1602
1603 // get the picture
1604 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
1605
1606 std::shared_ptr<MWAWPict> pict(MWAWPictData::get(input, int(entry.length()-8)));
1607 if (pict) {
1608 if (getTextListener()) {
1609 MWAWParagraph para=getTextListener()->getParagraph();
1610 para.setInterline(1.0, librevenge::RVNG_PERCENT);
1611 getTextListener()->setParagraph(para);
1612
1613 MWAWEmbeddedObject picture;
1614 if (pict->getBinary(picture) && !picture.m_dataList.empty() && !isMagicPic(picture.m_dataList[0]))
1615 getTextListener()->insertPicture(pictPos, picture);
1616 getTextListener()->insertEOL();
1617 #ifdef DEBUG_WITH_FILES
1618 if (!picture.m_dataList.empty()) {
1619 static int volatile pictName = 0;
1620 libmwaw::DebugStream f2;
1621 f2 << "PICT-" << ++pictName;
1622 libmwaw::Debug::dumpFile(picture.m_dataList[0], f2.str().c_str());
1623 ascii().skipZone(pos+8, entry.end()-1);
1624 }
1625 #endif
1626 }
1627 }
1628
1629 ascii().addPos(pos);
1630 ascii().addNote(f.str().c_str());
1631
1632 return true;
1633 }
1634
1635 bool MacWrtParser::isMagicPic(librevenge::RVNGBinaryData const &dt)
1636 {
1637 if (dt.size() != 526)
1638 return false;
1639 static char const *header="MAGICPIC";
1640 unsigned char const *dtBuf = dt.getDataBuffer()+514;
1641 for (int i=0; i < 8; i++)
1642 if (*(dtBuf++)!=header[i])
1643 return false;
1644 return true;
1645 }
1646
1647 ////////////////////////////////////////////////////////////
1648 // read the free list
1649 ////////////////////////////////////////////////////////////
1650 bool MacWrtParser::checkFreeList()
1651 {
1652 if (version() <= 3)
1653 return true;
1654 MWAWInputStreamPtr input = getInput();
1655 long pos = m_state->m_fileHeader.m_freeListPos;
1656 if (!input->checkPosition(pos+m_state->m_fileHeader.m_freeListLength)) {
1657 MWAW_DEBUG_MSG(("MacWrtParser::checkFreeList: zone is too short\n"));
1658 return false;
1659 }
1660 input->seek(pos, librevenge::RVNG_SEEK_SET);
1661
1662 libmwaw::DebugStream f;
1663 int N=int(m_state->m_fileHeader.m_freeListLength/8);
1664 for (int n=0; n<N; ++n) {
1665 pos=input->tell();
1666 auto freePos = long(input->readULong(4));
1667 auto sz = long(input->readULong(4));
1668
1669 f.str("");
1670 f << "Entries(FreeList)[" << n << "]:" << std::hex << freePos << "-" << sz << std::dec;
1671 ascii().addPos(pos);
1672 ascii().addNote(f.str().c_str());
1673
1674 // if the file end by a free zone, pos+sz can be greater than the file size
1675 if (!input->checkPosition(freePos+1)) {
1676 if (!input->checkPosition(freePos)) {
1677 MWAW_DEBUG_MSG(("MacWrtParser::checkFreeList: bad free block: \n"));
1678 return false;
1679 }
1680 continue;
1681 }
1682 f.str("");
1683 f << "Entries(FreeBlock)[" << n << "]:";
1684 ascii().addPos(freePos);
1685 ascii().addNote(f.str().c_str());
1686 }
1687 if (m_state->m_fileHeader.m_freeListLength!=m_state->m_fileHeader.m_freeListAllocated) {
1688 ascii().addPos(input->tell());
1689 ascii().addNote("Entries(FreeList)[end]:");
1690 }
1691 return true;
1692 }
1693
1694 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: