Debian Codegraph @

src/lib/MouseWrtParser.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <iomanip>
35 #include <iostream>
36 #include <limits>
37 #include <map>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWTextListener.hxx"
43 #include "MWAWFont.hxx"
44 #include "MWAWFontConverter.hxx"
45 #include "MWAWHeader.hxx"
46 #include "MWAWParagraph.hxx"
47 #include "MWAWPictMac.hxx"
48 #include "MWAWPosition.hxx"
49 #include "MWAWPrinter.hxx"
50 #include "MWAWRSRCParser.hxx"
51 #include "MWAWSubDocument.hxx"
52
53 #include "MouseWrtParser.hxx"
54
55 /** Internal: the structures of a MouseWrtParser */
56 namespace MouseWrtParserInternal
57 {
58 ////////////////////////////////////////
59 //! Internal: class to store zone information of a MouseWrtParser
60 struct Zone {
61 //! constructor
62 Zone()
63 : m_font()
64 , m_writingHebrew(false)
65 , m_text()
66 {
67 }
68 //! the font
69 MWAWFont m_font;
70 //! flag to know if the writing is reverted
71 bool m_writingHebrew;
72 //! the text entry
73 MWAWEntry m_text;
74 };
75
76 ////////////////////////////////////////
77 //! Internal: class to store paragraph information of a MouseWrtParser
78 struct Paragraph {
79 //! constructor
80 explicit Paragraph(int id=0)
81 : m_id(id)
82 , m_paragraph()
83 , m_picture(false)
84 {
85 }
86 //! the paragraph id
87 int m_id;
88 //! the paragraph
89 MWAWParagraph m_paragraph;
90 //! flag to know if this is a picture
91 bool m_picture;
92 };
93 ////////////////////////////////////////
94 //! Internal: the state of a MouseWrtParser
95 struct State {
96 //! constructor
97 State()
98 : m_actPage(0)
99 , m_numPages(0)
100 , m_charPLCMap()
101 , m_paraPLCMap()
102 , m_text()
103 {
104 for (auto &size : m_blockSizes) size=0;
105 }
106
107 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
108 /** the first zone's size */
109 long m_blockSizes[5];
110 /** the map position to charPLC */
111 std::map<int, MWAWFont> m_charPLCMap;
112 /** the map position to paraPLC */
113 std::map<int, Paragraph> m_paraPLCMap;
114 /** the main text entry */
115 MWAWEntry m_text;
116 /** the header and the footer zone */
117 Zone m_zones[2];
118 };
119
120 ////////////////////////////////////////
121 //! Internal: the subdocument of a MouseWrtParser
122 class SubDocument final : public MWAWSubDocument
123 {
124 public:
125 SubDocument(MouseWrtParser &pars, MWAWInputStreamPtr const &input, int zoneId)
126 : MWAWSubDocument(&pars, input, MWAWEntry())
127 , m_id(zoneId)
128 {
129 }
130
131 //! destructor
132 ~SubDocument() final {}
133
134 //! operator!=
135 bool operator!=(MWAWSubDocument const &doc) const final;
136
137 //! the parser function
138 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
139
140 protected:
141 //! the subdocument id
142 int m_id;
143 };
144
145 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
146 {
147 if (!listener.get()) {
148 MWAW_DEBUG_MSG(("MouseWrtParserInternal::SubDocument::parse: no listener\n"));
149 return;
150 }
151 auto *parser=dynamic_cast<MouseWrtParser *>(m_parser);
152 if (!parser) {
153 MWAW_DEBUG_MSG(("MouseWrtParserInternal::SubDocument::parse: no parser\n"));
154 return;
155 }
156
157 long pos = m_input->tell();
158 parser->sendZone(m_id);
159 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
160 }
161
162 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
163 {
164 if (MWAWSubDocument::operator!=(doc)) return true;
165 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
166 if (!sDoc) return true;
167 if (m_id != sDoc->m_id) return true;
168 return false;
169 }
170 }
171
172 ////////////////////////////////////////////////////////////
173 // constructor/destructor, ...
174 ////////////////////////////////////////////////////////////
175 MouseWrtParser::MouseWrtParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
176 : MWAWTextParser(input, rsrcParser, header)
177 , m_state(new MouseWrtParserInternal::State)
178 {
179 setAsciiName("main-1");
180 }
181
182 MouseWrtParser::~MouseWrtParser()
183 {
184 }
185
186 ////////////////////////////////////////////////////////////
187 // new page
188 ////////////////////////////////////////////////////////////
189 void MouseWrtParser::newPage(int number)
190 {
191 if (number <= m_state->m_actPage || number > m_state->m_numPages)
192 return;
193
194 while (m_state->m_actPage < number) {
195 m_state->m_actPage++;
196 if (!getTextListener() || m_state->m_actPage == 1)
197 continue;
198 getTextListener()->insertBreak(MWAWTextListener::PageBreak);
199 }
200 }
201
202 ////////////////////////////////////////////////////////////
203 // the parser
204 ////////////////////////////////////////////////////////////
205 void MouseWrtParser::parse(librevenge::RVNGTextInterface *docInterface)
206 {
207 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
208 bool ok = true;
209 try {
210 // create the asciiFile
211 ascii().setStream(getInput());
212 ascii().open(asciiName());
213 checkHeader(nullptr);
214 ok=createZones();
215 if (ok) {
216 createDocument(docInterface);
217 sendMainZone();
218 }
219
220 ascii().reset();
221 }
222 catch (...) {
223 MWAW_DEBUG_MSG(("MouseWrtParser::parse: exception catched when parsing\n"));
224 ok = false;
225 }
226
227 resetTextListener();
228 if (!ok) throw(libmwaw::ParseException());
229 }
230
231 ////////////////////////////////////////////////////////////
232 // create the document
233 ////////////////////////////////////////////////////////////
234 void MouseWrtParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
235 {
236 if (!documentInterface) return;
237 if (getTextListener()) {
238 MWAW_DEBUG_MSG(("MouseWrtParser::createDocument: listener already exist\n"));
239 return;
240 }
241
242 // update the page
243 m_state->m_actPage = 0;
244
245 // create the page list
246 MWAWPageSpan ps(getPageSpan());
247 m_state->m_numPages=computeNumPages();
248 ps.setPageSpan(m_state->m_numPages);
249 for (int i=0; i<2; ++i) {
250 if (!m_state->m_zones[i].m_text.valid()) continue;
251 MWAWHeaderFooter hF(i==0 ? MWAWHeaderFooter::HEADER : MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
252 hF.m_subDocument.reset(new MouseWrtParserInternal::SubDocument(*this, getInput(), i));
253 ps.setHeaderFooter(hF);
254 }
255 std::vector<MWAWPageSpan> pageList;
256 pageList.push_back(ps);
257 //
258 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
259 setTextListener(listen);
260 listen->startDocument();
261 }
262
263
264 ////////////////////////////////////////////////////////////
265 //
266 // Intermediate level
267 //
268 ////////////////////////////////////////////////////////////
269 bool MouseWrtParser::createZones()
270 {
271 MWAWInputStreamPtr input = getInput();
272 libmwaw::DebugStream f;
273 for (int i=0; i<5; ++i) {
274 if (!m_state->m_blockSizes[i]) continue;
275 long pos = input->tell();
276 if (m_state->m_blockSizes[i]<0 || !input->checkPosition(pos+m_state->m_blockSizes[i])) {
277 MWAW_DEBUG_MSG(("MouseWrtParser::createZones: the block sizes are wrong\n"));
278 return false;
279 }
280 if (i==1) continue;
281 bool done=false;
282 switch (i) {
283 case 0:
284 done=readCharPLCs(m_state->m_blockSizes[i]);
285 break;
286 case 2:
287 done=readParagraphPLCs(m_state->m_blockSizes[i]);
288 break;
289 case 3:
290 done=m_state->m_blockSizes[i]>=120 && readPrintInfo();
291 break;
292 case 4:
293 done=readDocumentInfo(m_state->m_blockSizes[i]);
294 break;
295 default:
296 break;
297 }
298 if (done) {
299 if (input->tell()!=pos+m_state->m_blockSizes[i])
300 ascii().addDelimiter(input->tell(),'|');
301 }
302 else {
303 f.str("");
304 f << "Entries(Zone" << i << "):";
305 ascii().addPos(pos);
306 ascii().addNote(f.str().c_str());
307 }
308 input->seek(pos+m_state->m_blockSizes[i], librevenge::RVNG_SEEK_SET);
309 }
310 m_state->m_text.setBegin(input->tell());
311 m_state->m_text.setLength(m_state->m_blockSizes[1]);
312 if (m_state->m_blockSizes[1]<0 || !input->checkPosition(m_state->m_text.end())) {
313 MWAW_DEBUG_MSG(("MouseWrtParser::createZones: can not find the text zone\n"));
314 return false;
315 }
316 if (!input->isEnd()) {
317 ascii().addPos(input->tell());
318 ascii().addNote("Entries(Unknown):");
319 }
320 return true;
321 }
322
323 ////////////////////////////////////////////////////////////
324 //
325 // Low level
326 //
327 ////////////////////////////////////////////////////////////
328
329 ////////////////////////////////////////////////////////////
330 // read the header
331 ////////////////////////////////////////////////////////////
332 bool MouseWrtParser::checkHeader(MWAWHeader *header, bool strict)
333 {
334 *m_state = MouseWrtParserInternal::State();
335
336 MWAWInputStreamPtr input = getInput();
337 if (!input || !input->hasDataFork())
338 return false;
339
340 libmwaw::DebugStream f;
341 f << "FileHeader:";
342 int headerSize=30;
343 if (!input->checkPosition(headerSize)) {
344 MWAW_DEBUG_MSG(("MouseWrtParser::checkHeader: file is too short\n"));
345 return false;
346 }
347 input->seek(0,librevenge::RVNG_SEEK_SET);
348 if (input->readULong(4)!=0x4474d30 || input->readULong(2)!=0x3400)
349 return false;
350 long totalSize=m_state->m_blockSizes[0];
351 for (int i=0; i<5; ++i) {
352 m_state->m_blockSizes[i]=long(input->readLong(4));
353 if (m_state->m_blockSizes[i]<0) return false;
354 char const *wh[]= {"charPlc","text","paraPLC","printer","zone4"};
355 f << wh[i] << "[sz]=" << m_state->m_blockSizes[i] << ",";
356 totalSize+=m_state->m_blockSizes[i];
357 }
358 if (totalSize<0 || !input->checkPosition(30+totalSize)) return false;
359 if (strict && ((m_state->m_blockSizes[0]%8)!=0 || (m_state->m_blockSizes[2]%38)!=0 ||
360 (m_state->m_blockSizes[3] && m_state->m_blockSizes[3]<120) ||
361 (m_state->m_blockSizes[4] && m_state->m_blockSizes[4]<76)))
362 return false;
363 // probably a size, maybe pict size
364 auto dSz=long(input->readLong(4));
365 if (dSz) {
366 MWAW_DEBUG_MSG(("MouseWrtParser::checkHeader: find some extra size?\n"));
367 f << "##f0=" << dSz << ",";
368 }
369 // ok, we can finish initialization
370 if (header)
371 header->reset(MWAWDocument::MWAW_T_MOUSEWRITE, 1);
372 input->seek(headerSize,librevenge::RVNG_SEEK_SET);
373
374 ascii().addPos(0);
375 ascii().addNote(f.str().c_str());
376 ascii().addPos(headerSize);
377 return true;
378 }
379
380 ////////////////////////////////////////////////////////////
381 // read the basic structure
382 ////////////////////////////////////////////////////////////
383 bool MouseWrtParser::readCharPLCs(long sz)
384 {
385 MWAWInputStreamPtr input = getInput();
386 long pos = input->tell();
387 if (sz<0 || (sz%8)!=0 || !input->checkPosition(pos+sz)) {
388 MWAW_DEBUG_MSG(("MouseWrtParser::readCharPLCs: find unexpected size length\n"));
389 return false;
390 }
391 long N=sz/8;
392 libmwaw::DebugStream f;
393 f << "Entries(CharPLC):";
394 ascii().addPos(pos);
395 ascii().addNote(f.str().c_str());
396 for (long i=0; i<N; ++i) {
397 pos=input->tell();
398 f.str("");
399 f << "CharPLC-C" << i << ":";
400 int cPos;
401 MWAWFont font;
402 if (i+1!=N && readFont(font, cPos)) {
403 f << "cPos=" << cPos << "," << font.getDebugString(getParserState()->m_fontConverter);
404 m_state->m_charPLCMap[cPos]=font;
405 }
406 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
407 ascii().addPos(pos);
408 ascii().addNote(f.str().c_str());
409 }
410 return true;
411 }
412
413 bool MouseWrtParser::readFont(MWAWFont &font, int &cPos)
414 {
415 MWAWInputStreamPtr input = getInput();
416 long pos = input->tell();
417 if (!input->checkPosition(pos+8)) return false;
418 cPos=static_cast<int>(input->readULong(2));
419 libmwaw::DebugStream f;
420 font=MWAWFont();
421 auto val=int(input->readULong(1));
422 if (val) f << "f0=" << val << ",";
423 font.setSize(float(input->readULong(1)));
424 auto flag=static_cast<int>(input->readULong(1));
425 uint32_t flags=0;
426 if (flag&0x1) flags |= MWAWFont::boldBit;
427 if (flag&0x2) flags |= MWAWFont::italicBit;
428 if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
429 if (flag&0x8) flags |= MWAWFont::embossBit;
430 if (flag&0x10) flags |= MWAWFont::shadowBit;
431 if (flag&0x60) f << "#flag[hi]=" << std::hex << (flag&0x60) << std::dec << ",";
432 font.setFlags(flags);
433 val=static_cast<int>(input->readULong(1)); // 0|7d
434 if (val) f << "f1=" << val << ",";
435 font.setId(static_cast<int>(input->readULong(2)));
436 font.m_extra=f.str().c_str();
437 return true;
438 }
439
440 bool MouseWrtParser::readParagraphPLCs(long sz)
441 {
442 MWAWInputStreamPtr input = getInput();
443 long pos = input->tell();
444 if (sz<0 || (sz%38)!=0 || !input->checkPosition(pos+sz)) {
445 MWAW_DEBUG_MSG(("MouseWrtParser::readParagraphPLCs: find unexpected size length\n"));
446 return false;
447 }
448 auto N=int(sz/38);
449 libmwaw::DebugStream f;
450 f << "Entries(ParaPLC):";
451 ascii().addPos(pos);
452 ascii().addNote(f.str().c_str());
453 for (int i=0; i<N; ++i) {
454 pos=input->tell();
455 f.str("");
456 f << "ParaPLC-P" << i << ":";
457 if (i+1==N) { // last is sometimes random
458 input->seek(pos+36, librevenge::RVNG_SEEK_SET);
459 auto cPos=static_cast<int>(input->readULong(2));
460 f << "cPos=" << cPos << ",";
461 input->seek(pos+38, librevenge::RVNG_SEEK_SET);
462 ascii().addPos(pos);
463 ascii().addNote(f.str().c_str());
464 break;
465 }
466 MouseWrtParserInternal::Paragraph para(i);
467 para.m_paragraph.m_marginsUnit=librevenge::RVNG_POINT;
468 // note: right margins is defined from left, so must be corrected when we know the page length
469 para.m_paragraph.m_margins[1] = double(input->readLong(2));
470 para.m_paragraph.m_margins[2] = double(input->readLong(2));
471 auto val=static_cast<int>(input->readULong(1));
472 switch (val) {
473 case 0xf: // left
474 break;
475 case 0x10:
476 para.m_paragraph.m_justify = MWAWParagraph::JustificationFull;
477 break;
478 case 0x11:
479 para.m_paragraph.m_justify = MWAWParagraph::JustificationRight;
480 break;
481 case 0x12:
482 para.m_paragraph.m_justify = MWAWParagraph::JustificationCenter;
483 break;
484 case 0x13:
485 f << "justify=rowCol,";
486 break;
487 case 0x14:
488 f << "justify=col,";
489 break;
490 default:
491 if (!val) break;
492 MWAW_DEBUG_MSG(("MouseWrtParser::readParagraphPLCs: unknown justify\n"));
493 f << "#justify=" << std::hex << val << std::dec << ",";
494 }
495 for (int j=0; j<2; ++j) { // fl0=1|7, maybe related to tabs definition
496 val=static_cast<int>(input->readULong(1)); // 1|7
497 if (val) f << "fl" << j << "=" << std::hex << val << std::dec << ",";
498 }
499 val=static_cast<int>(input->readULong(1));
500 switch (val) {
501 case 0xb:
502 break;
503 case 0xc:
504 para.m_paragraph.setInterline(1.1, librevenge::RVNG_PERCENT);
505 break;
506 case 0xd:
507 para.m_paragraph.setInterline(1.5, librevenge::RVNG_PERCENT);
508 break;
509 case 0xe:
510 para.m_paragraph.setInterline(2, librevenge::RVNG_PERCENT);
511 break;
512 default:
513 MWAW_DEBUG_MSG(("MouseWrtParser::readParagraphPLCs: unknown interline\n"));
514 f << "#interline=" << val << ",";
515 break;
516 }
517 val=static_cast<int>(input->readULong(1)); // always 0
518 if (val) f << "fl2=" << val << ",";
519 val=static_cast<int>(input->readULong(1));
520 switch (val) {
521 case 0:
522 break;
523 case 1:
524 para.m_picture=true;
525 f << "picture,";
526 break;
527 default:
528 MWAW_DEBUG_MSG(("MouseWrtParser::readParagraphPLCs: unknown picture def\n"));
529 f << "#picture=" << val << ",";
530 break;
531 }
532 int lastTabPos=0; // check that the tabulation are in increasing order
533 for (int j=0; j<10; ++j) {
534 val=static_cast<int>(input->readLong(2));
535 if (!val || val<=lastTabPos) break;
536 MWAWTabStop tab;
537 tab.m_alignment=MWAWTabStop::CENTER;
538 tab.m_position=double(val)/72.;
539 para.m_paragraph.m_tabs->push_back(tab);
540 lastTabPos=val;
541 }
542 input->seek(pos+30, librevenge::RVNG_SEEK_SET);
543 val=static_cast<int>(input->readLong(2));
544 if (val) f << "act[tab]=" << val << ",";
545 val=static_cast<int>(input->readULong(1));
546 switch (val) {
547 case 0:
548 para.m_paragraph.m_writingMode=libmwaw::WritingRightTop;
549 break;
550 case 1: // normal writing
551 break;
552 default:
553 MWAW_DEBUG_MSG(("MouseWrtParser::readParagraphPLCs: unknown writing mode\n"));
554 f << "#writing[mode]=" << val << ",";
555 break;
556 }
557 for (int j=0; j<3; ++j) { // always 0?
558 val=static_cast<int>(input->readULong(1));
559 if (val) f << "flA" << j << "=" << val << ",";
560 }
561 f << para.m_paragraph;
562 auto cPos=static_cast<int>(input->readULong(2));
563 f << "cPos=" << cPos << ",";
564 m_state->m_paraPLCMap[cPos]=para;
565 input->seek(pos+38, librevenge::RVNG_SEEK_SET);
566 ascii().addPos(pos);
567 ascii().addNote(f.str().c_str());
568 }
569 return true;
570 }
571
572 bool MouseWrtParser::readDocumentInfo(long sz)
573 {
574 MWAWInputStreamPtr input = getInput();
575 long pos = input->tell();
576 if (sz<76 || !input->checkPosition(pos+sz)) {
577 MWAW_DEBUG_MSG(("MouseWrtParser::readDocumentInfo: find unexpected size length\n"));
578 return false;
579 }
580 libmwaw::DebugStream f;
581 f << "Entries(DocumentInfo):";
582 int val;
583 f << "unkns=[";
584 for (int i=0; i<2; ++i) { // footer,header
585 // find 0|18|25, does not seems related to heigth...
586 val=static_cast<int>(input->readULong(2));
587 if (val)
588 f << val << ",";
589 else
590 f << "_,";
591 }
592 f << "],";
593 for (auto &zone : m_state->m_zones) { // header,footer
594 val=static_cast<int>(input->readULong(1));
595 switch (val) {
596 case 0:
597 zone.m_writingHebrew=true;
598 f << "writing[mode]=rt-lb,";
599 break;
600 case 1: // normal writing
601 break;
602 default:
603 MWAW_DEBUG_MSG(("MouseWrtParser::readDocumentInfo: unknown writing mode\n"));
604 f << "#writing[mode]=" << val << ",";
605 break;
606 }
607 }
608 f << "ids=[";
609 for (int i=0; i<2; ++i) { // header,footer
610 val=static_cast<int>(input->readULong(4));
611 if (val)
612 f << std::hex << val << std::dec << ",";
613 else
614 f << "_,";
615 }
616 f << "],";
617 long zoneSize[2];
618 for (int i=0; i<2; ++i) { // header,footer
619 zoneSize[i]=long(input->readULong(4));
620 if (zoneSize[i]) f << "block" << i << "[sz]=" << zoneSize[i] << ",";
621 }
622 for (int i=0; i<2; ++i) { // header,footer, always 0|-1?
623 val=static_cast<int>(input->readLong(4));
624 if (val) f << "f" << i+2 << "=" << val << ",";
625 }
626 for (int i=0; i<2; ++i) { // header,footer
627 long actPos=input->tell();
628 int cPos;
629 if (zoneSize[i]==0 || !readFont(m_state->m_zones[i].m_font, cPos))
630 input->seek(actPos+8, librevenge::RVNG_SEEK_SET);
631 else
632 f << "font" << i << "=[" << m_state->m_zones[i].m_font.getDebugString(getParserState()->m_fontConverter) << "],";
633 }
634 ascii().addDelimiter(input->tell(),'|');
635 if (sz > 76+zoneSize[0]+zoneSize[1] || zoneSize[0]<0 || zoneSize[1]<0) {
636 MWAW_DEBUG_MSG(("MouseWrtParser::readDocumentInfo: problem with the zoneSize\n"));
637 f << "##zoneSize,";
638 input->seek(pos+sz, librevenge::RVNG_SEEK_SET);
639 }
640 else {
641 input->seek(pos+76, librevenge::RVNG_SEEK_SET);
642 for (int i=0; i<2; ++i) {
643 if (zoneSize[i]<=0) continue;
644 m_state->m_zones[i].m_text.setBegin(input->tell());
645 m_state->m_zones[i].m_text.setLength(zoneSize[i]);
646 input->seek(zoneSize[i], librevenge::RVNG_SEEK_CUR);
647 }
648 }
649
650 ascii().addPos(pos);
651 ascii().addNote(f.str().c_str());
652 return true;
653 }
654
655 int MouseWrtParser::computeNumPages()
656 {
657 if (!m_state->m_text.valid()) return 1;
658 MWAWInputStreamPtr input = getInput();
659 int numPages=1;
660 auto pIt=m_state->m_paraPLCMap.begin();
661 long const beginPos=m_state->m_text.begin();
662 while (pIt!=m_state->m_paraPLCMap.end()) {
663 if (pIt->second.m_picture) {
664 ++pIt;
665 continue;
666 }
667 long actPos=beginPos+(pIt++)->first;
668 long lastPos=(pIt!=m_state->m_paraPLCMap.end()) ? beginPos+pIt->first : m_state->m_text.end();
669 if (lastPos>m_state->m_text.end()) {
670 MWAW_DEBUG_MSG(("MouseWrtParser::computeNumPages: oops, problem with some plc pos\n"));
671 break;
672 }
673 input->seek(actPos, librevenge::RVNG_SEEK_SET);
674 for (long cPos=actPos; cPos<lastPos; ++cPos) {
675 if (input->readULong(1)==0xd7)
676 ++numPages;
677 }
678 }
679 return numPages;
680 }
681
682 ////////////////////////////////////////////////////////////
683 // read the print info
684 ////////////////////////////////////////////////////////////
685 bool MouseWrtParser::readPrintInfo()
686 {
687 MWAWInputStreamPtr input = getInput();
688 long pos = input->tell();
689 libmwaw::DebugStream f;
690 // print info
691 libmwaw::PrinterInfo info;
692 if (!info.read(input)) return false;
693 f << "Entries(PrintInfo):"<< info;
694
695 MWAWVec2i paperSize = info.paper().size();
696 MWAWVec2i pageSize = info.page().size();
697 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
698 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
699
700 // define margin from print info
701 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
702 MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1);
703
704 // move margin left | top
705 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
706 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
707 lTopMargin -= MWAWVec2i(decalX, decalY);
708 rBotMargin += MWAWVec2i(decalX, decalY);
709
710 // decrease right | bottom
711 int rightMarg = rBotMargin.x() -10;
712 if (rightMarg < 0) rightMarg=0;
713 int botMarg = rBotMargin.y() -50;
714 if (botMarg < 0) botMarg=0;
715
716 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
717 getPageSpan().setMarginBottom(botMarg/72.0);
718 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
719 getPageSpan().setMarginRight(rightMarg/72.0);
720 getPageSpan().setFormLength(paperSize.y()/72.);
721 getPageSpan().setFormWidth(paperSize.x()/72.);
722
723 ascii().addPos(pos);
724 ascii().addNote(f.str().c_str());
725 input->seek(pos+0x78, librevenge::RVNG_SEEK_SET);
726 if (long(input->tell()) != pos+0x78) {
727 MWAW_DEBUG_MSG(("MouseWrtParser::readPrintInfo: file is too short\n"));
728 return false;
729 }
730 ascii().addPos(input->tell());
731
732 return true;
733 }
734
735 ////////////////////////////////////////////////////////////
736 // send the data
737 ////////////////////////////////////////////////////////////
738 bool MouseWrtParser::sendMainZone()
739 {
740 MWAWListenerPtr listener=getMainListener();
741 if (!listener) {
742 MWAW_DEBUG_MSG(("MouseWrtParser::sendMainZone: can not find the listener\n"));
743 return false;
744 }
745 MWAWInputStreamPtr input = getInput();
746 if (!m_state->m_text.valid() || !input->checkPosition(m_state->m_text.end())) {
747 listener->insertChar(' ');
748 return true;
749 }
750 long begPos = m_state->m_text.begin(), pos=begPos;
751 input->seek(pos, librevenge::RVNG_SEEK_SET);
752 auto N=static_cast<int>(m_state->m_text.length());
753 libmwaw::DebugStream f;
754 f << "Entries(Text):";
755 double const pageWidth=72. * getPageSpan().getPageWidth();
756 int actPage=1;
757 newPage(actPage);
758 for (int i=0; i<N; ++i) {
759 auto pIt=m_state->m_paraPLCMap.find(i);
760 if (pIt!=m_state->m_paraPLCMap.end()) {
761 if (i!=0) {
762 ascii().addPos(pos);
763 ascii().addNote(f.str().c_str());
764 pos=input->tell();
765 f.str("");
766 f << "Text:";
767 }
768 auto const &para=pIt->second;
769 f << "[P" << para.m_id << "]";
770 // time to send the paragraph, so first update the right margins
771 MWAWParagraph paragraph=para.m_paragraph;
772 if (*(paragraph.m_margins[2])>pageWidth) {
773 f << "#";
774 paragraph.m_margins[2]=0;
775 }
776 else
777 paragraph.m_margins[2]=pageWidth-*paragraph.m_margins[2];
778 if (para.m_picture) paragraph.m_justify = MWAWParagraph::JustificationRight;
779 listener->setParagraph(paragraph);
780 if (para.m_picture) {
781 f << "[picture],";
782 ++pIt;
783 long endPos=pIt==m_state->m_paraPLCMap.end() ? m_state->m_text.end() : begPos+pIt->first;
784 if (endPos<=input->tell()) { // check that we do not go backward
785 f << "###";
786 MWAW_DEBUG_MSG(("MouseWrtParser::sendZone: can not compute the end of picture pos, stop!!!\n"));
787 break;
788 }
789 long actPos=input->tell();
790 bool ok=endPos-actPos>9;
791 if (ok) {
792 // look for pict
793 auto dSz=static_cast<int>(input->readULong(2));
794 if (dSz+9>endPos-actPos || dSz+12<endPos-actPos) {
795 f << "#pict?";
796 MWAW_DEBUG_MSG(("MouseWrtParser::sendZone: no sure that this is a picture\n"));
797 input->seek(endPos-9, librevenge::RVNG_SEEK_SET);
798 }
799 else
800 input->seek(actPos+dSz, librevenge::RVNG_SEEK_SET);
801 long pictSz=input->tell()-actPos;
802 int dim[4];
803 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
804 MWAWBox2i box(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2]));
805 f << "box=" << box << ",";
806 if (box.size()[0]<0 || box.size()[1]<0 || box.size()[0]>2000 || box.size()[1]>2000) {
807 MWAW_DEBUG_MSG(("MouseWrtParser::sendZone: the bdbox is bad\n"));
808 f << "###";
809 ok=false;
810 }
811 else {
812 librevenge::RVNGBinaryData data;
813 input->seek(actPos, librevenge::RVNG_SEEK_SET);
814 input->readDataBlock(pictSz, data);
815 MWAWEmbeddedObject object(data);
816 MWAWPosition position(MWAWVec2f(0,0), MWAWVec2f(box.size()), librevenge::RVNG_POINT);
817 position.m_anchorTo=MWAWPosition::Char;
818 listener->insertPicture(position, object);
819 #ifdef DEBUG_WITH_FILES
820 static int volatile pictName = 0;
821 libmwaw::DebugStream f2;
822 f2 << "Pict-" << ++pictName << ".pct";
823 libmwaw::Debug::dumpFile(data, f2.str().c_str());
824 #endif
825 ascii().skipZone(actPos, actPos+pictSz-1);
826 }
827 }
828
829 if (ok || endPos-actPos>20) {
830 listener->insertEOL();
831 ascii().addPos(pos);
832 ascii().addNote(f.str().c_str());
833 pos=input->tell();
834 f.str("");
835 f << "Text:";
836 input->seek(endPos, librevenge::RVNG_SEEK_SET);
837 i=int(endPos-begPos-1); // will be endPos-begPos after
838 continue;
839 }
840 }
841 }
842 auto fIt=m_state->m_charPLCMap.find(i);
843 if (fIt!=m_state->m_charPLCMap.end()) {
844 listener->setFont(fIt->second);
845 f << "[" << fIt->second.getDebugString(getParserState()->m_fontConverter) << "]";
846 }
847 auto c=static_cast<unsigned char>(input->readULong(1));
848 f << c;
849 switch (c) {
850 case 0x9:
851 listener->insertTab();
852 break;
853 case 0xd:
854 listener->insertEOL();
855 ascii().addPos(pos);
856 ascii().addNote(f.str().c_str());
857 pos=input->tell();
858 f.str("");
859 f << "Text:";
860 break;
861 case 0xd7: {
862 newPage(++actPage);
863 long actPos=input->tell();
864 if (i+1!=N && input->readULong(1)==0xd) {
865 ascii().addPos(pos);
866 ascii().addNote(f.str().c_str());
867 pos=input->tell();
868 f.str("");
869 f << "Text:";
870 ++i;
871 }
872 else
873 input->seek(actPos, librevenge::RVNG_SEEK_SET);
874 break;
875 }
876 default:
877 listener->insertCharacter(c);
878 break;
879 }
880 }
881 if (input->tell()!=pos) {
882 ascii().addPos(pos);
883 ascii().addNote(f.str().c_str());
884 }
885 return true;
886 }
887
888 bool MouseWrtParser::sendZone(int zoneId)
889 {
890 if (zoneId < 0 || zoneId >= 2) {
891 MWAW_DEBUG_MSG(("MouseWrtParser::sendZone: invalid zone %d\n", zoneId));
892 return false;
893 }
894 MWAWListenerPtr listener=getMainListener();
895 if (!listener) {
896 MWAW_DEBUG_MSG(("MouseWrtParser::sendZone: can not find the listener\n"));
897 return false;
898 }
899 auto const &zone=m_state->m_zones[zoneId];
900 if (!zone.m_text.valid()) return true;
901 if (zone.m_writingHebrew) {
902 MWAWParagraph para;
903 para.m_writingMode=libmwaw::WritingRightTop;
904 listener->setParagraph(para);
905 }
906 listener->setFont(zone.m_font);
907 MWAWInputStreamPtr input = getInput();
908 libmwaw::DebugStream f;
909 f << "Entries(Text):" << (zoneId==0 ? "header" : "footer") << ",";
910 input->seek(zone.m_text.begin(), librevenge::RVNG_SEEK_SET);
911 auto N=static_cast<int>(zone.m_text.length());
912 for (long i=0; i<N ; ++i) {
913 if (input->isEnd()) {
914 MWAW_DEBUG_MSG(("MouseWrtParser::sendZone: oops the text length seems too big\n"));
915 f << "###";
916 break;
917 }
918 auto c=static_cast<unsigned char>(input->readULong(1));
919 f << c;
920 switch (c) {
921 case 0x9:
922 MWAW_DEBUG_MSG(("MouseWrtParser::sendZone: oops unexpected tab\n"));
923 listener->insertChar(' ');
924 break;
925 case 0xd:
926 if (i+1==N)
927 break;
928 listener->insertEOL();
929 break;
930 default:
931 listener->insertCharacter(c);
932 break;
933 }
934 }
935 ascii().addPos(zone.m_text.begin());
936 ascii().addNote(f.str().c_str());
937 return true;
938 }
939
940 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: