Debian Codegraph @

src/lib/WordMakerParser.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 <cstring>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWCell.hxx"
43 #include "MWAWTextListener.hxx"
44 #include "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWHeader.hxx"
47 #include "MWAWParagraph.hxx"
48 #include "MWAWPosition.hxx"
49 #include "MWAWPictMac.hxx"
50 #include "MWAWPrinter.hxx"
51 #include "MWAWSection.hxx"
52 #include "MWAWSubDocument.hxx"
53 #include "MWAWTable.hxx"
54
55 #include "WordMakerParser.hxx"
56
57 /** Internal: the structures of a WordMakerParser */
58 namespace WordMakerParserInternal
59 {
60 //! Internal: small structure used to store a zone of a WordMakerParser
61 struct Zone {
62 //! the zone type
63 enum Type { Z_MAIN, Z_HEADER, Z_FOOTER };
64 //! constructor
65 explicit Zone(Type type)
66 : m_type(type)
67 , m_id(0)
68 , m_hasTitlePage(false)
69 , m_beginPos(0)
70
71 , m_numCharacter(0)
72 , m_numParagraph(0)
73 , m_numPicture(0)
74 {
75 }
76 //! small function to know if a zone is empty
77 bool empty() const
78 {
79 return m_numCharacter<=0 && m_numParagraph<=1 && m_numPicture<=0;
80 }
81 //! the zone type
82 Type m_type;
83 //! the zone id
84 int m_id;
85 //! true if the document has a title page
86 bool m_hasTitlePage;
87 //! the zone beginning in the file
88 long m_beginPos;
89 //! the number of character
90 long m_numCharacter;
91 //! the number of paragraph
92 int m_numParagraph;
93 //! the number of picture
94 int m_numPicture;
95 };
96
97 ////////////////////////////////////////
98 //! Internal: the state of a WordMakerParser
99 struct State {
100 //! constructor
101 State()
102 : m_actPage(0)
103 , m_numPages(0)
104
105 , m_endDataZone(0)
106 , m_currentZone(nullptr)
107
108 , m_typeToZoneMap()
109 , m_pictureList()
110 {
111 }
112 State(State const &) = delete;
113 State &operator=(State const &) = default;
114
115 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
116
117 //! the end data zone limit
118 long m_endDataZone;
119 //! the current zone
120 Zone *m_currentZone;
121 //! a map type to zone
122 std::multimap<Zone::Type, Zone> m_typeToZoneMap;
123 //! the list of document picture entries
124 std::vector<MWAWEntry> m_pictureList;
125 };
126
127 ////////////////////////////////////////
128 //! Internal: the subdocument of a WordMakerParser
129 class SubDocument final : public MWAWSubDocument
130 {
131 public:
132 SubDocument(WordMakerParser &pars, MWAWInputStreamPtr const &input, Zone const &zone)
133 : MWAWSubDocument(&pars, input, MWAWEntry()), m_wZone(zone)
134 {
135 }
136
137 //! destructor
138 ~SubDocument() final {}
139
140 //! operator!=
141 bool operator!=(MWAWSubDocument const &doc) const final
142 {
143 if (MWAWSubDocument::operator!=(doc)) return true;
144 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
145 if (!sDoc) return true;
146 if (m_wZone.m_beginPos != sDoc->m_wZone.m_beginPos) return true;
147 return false;
148 }
149
150 //! the parser function
151 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
152
153 protected:
154 //! the zone
155 Zone m_wZone;
156 };
157
158 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
159 {
160 if (!listener.get()) {
161 MWAW_DEBUG_MSG(("WordMakerParserInternal::SubDocument::parse: no listener\n"));
162 return;
163 }
164 auto *parser=dynamic_cast<WordMakerParser *>(m_parser);
165 if (!parser) {
166 MWAW_DEBUG_MSG(("WordMakerParserInternal::SubDocument::parse: no parser\n"));
167 return;
168 }
169
170 long pos = m_input->tell();
171 parser->sendZone(m_wZone);
172 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
173 }
174 }
175
176 ////////////////////////////////////////////////////////////
177 // constructor/destructor, ...
178 ////////////////////////////////////////////////////////////
179 WordMakerParser::WordMakerParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
180 : MWAWTextParser(input, rsrcParser, header)
181 , m_state()
182 {
183 setAsciiName("main-1");
184
185 m_state.reset(new WordMakerParserInternal::State);
186
187 // reduce the margin (in case, the page is not defined)
188 getPageSpan().setMargins(0.1);
189 }
190
191 WordMakerParser::~WordMakerParser()
192 {
193 }
194
195 ////////////////////////////////////////////////////////////
196 // the parser
197 ////////////////////////////////////////////////////////////
198 void WordMakerParser::parse(librevenge::RVNGTextInterface *docInterface)
199 {
200 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
201 bool ok = true;
202 try {
203 // create the asciiFile
204 ascii().setStream(getInput());
205 ascii().open(asciiName());
206
207 checkHeader(nullptr);
208 ok = createZones();
209 ascii().addPos(getInput()->tell());
210 ascii().addNote("_");
211 if (ok) {
212 createDocument(docInterface);
213 for (auto const &entry : m_state->m_pictureList)
214 sendPicture(entry);
215 for (auto const &it : m_state->m_typeToZoneMap) {
216 if (it.first==WordMakerParserInternal::Zone::Z_MAIN)
217 sendZone(it.second);
218 }
219 }
220
221 ascii().reset();
222 }
223 catch (...) {
224 MWAW_DEBUG_MSG(("WordMakerParser::parse: exception catched when parsing\n"));
225 ok = false;
226 }
227
228 resetTextListener();
229 if (!ok) throw(libmwaw::ParseException());
230 }
231
232 ////////////////////////////////////////////////////////////
233 // create the document
234 ////////////////////////////////////////////////////////////
235 void WordMakerParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
236 {
237 if (!documentInterface) return;
238 if (getTextListener()) {
239 MWAW_DEBUG_MSG(("WordMakerParser::createDocument: listener already exist\n"));
240 return;
241 }
242
243 // first parse the zone to look for header/footer
244 MWAWPageSpan ps(getPageSpan());
245 bool hasTitlePage=false, hasHF=false;
246 for (auto const &it : m_state->m_typeToZoneMap) {
247 if (it.first==WordMakerParserInternal::Zone::Z_MAIN || it.second.empty())
248 continue;
249 if (it.second.m_hasTitlePage)
250 hasTitlePage=true;
251 hasHF=true;
252 MWAWHeaderFooter hf(it.first==WordMakerParserInternal::Zone::Z_FOOTER ?
253 MWAWHeaderFooter::FOOTER : MWAWHeaderFooter::HEADER,
254 it.second.m_id==1 ? MWAWHeaderFooter::ODD :
255 it.second.m_id==2 ? MWAWHeaderFooter::EVEN : MWAWHeaderFooter::ALL);
256 hf.m_subDocument.reset(new WordMakerParserInternal::SubDocument(*this, getInput(),it.second));
257 ps.setHeaderFooter(hf);
258 }
259
260 // create the page list
261 std::vector<MWAWPageSpan> pageList;
262 if (hasTitlePage && hasHF) {
263 MWAWPageSpan title(getPageSpan());
264 title.setPageSpan(1);
265 pageList.push_back(title);
266 }
267 ps.setPageSpan(m_state->m_numPages+(hasTitlePage && hasHF ? 0 : 1));
268 pageList.push_back(ps);
269
270 //
271 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
272 setTextListener(listen);
273 listen->startDocument();
274 }
275
276
277 ////////////////////////////////////////////////////////////
278 //
279 // Intermediate level
280 //
281 ////////////////////////////////////////////////////////////
282 bool WordMakerParser::createZones()
283 {
284 MWAWInputStreamPtr input = getInput();
285 if (!input)
286 return false;
287 input->seek(4,librevenge::RVNG_SEEK_SET);
288 long len=long(input->readLong(4));
289 long endPos=8+len;
290 m_state->m_endDataZone=endPos;
291 if (len<20 || !input->checkPosition(endPos) || input->readULong(4) != 0x574f5231) {
292 MWAW_DEBUG_MSG(("WordMakerParser::createZone: can not read the data size\n"));
293 return false;
294 }
295
296 libmwaw::DebugStream f;
297 m_state->m_numPages=1;
298 while (input->tell()+8<endPos) {
299 long pos=input->tell();
300 f.str("");
301 std::string what;
302 for (int i=0; i<4; ++i)
303 what+=char(input->readULong(1));
304 f << "Entries(" << what << "):";
305 long dataLen=long(input->readLong(4));
306 if (dataLen<0 || (long)((unsigned long)pos+8+(unsigned long)dataLen)<pos+8 || pos+8+dataLen>endPos) {
307 MWAW_DEBUG_MSG(("WordMakerParser::createZone: can not retrieve a zone header\n"));
308 f << "###";
309 ascii().addPos(pos);
310 ascii().addNote(f.str().c_str());
311 return false;
312 }
313 if (what=="COLR") {
314 if (dataLen!=8) {
315 MWAW_DEBUG_MSG(("WordMakerParser::createZone: the color length seems bad\n"));
316 f << "###";
317 }
318 else {
319 for (int i=0; i<8; ++i) {
320 int val=int(input->readLong(1));
321 if (val!=i)
322 f << "f" << i << "=" << val << ",";
323 }
324 }
325 }
326 else if (what=="DOC ") {
327 if (dataLen!=8) {
328 MWAW_DEBUG_MSG(("WordMakerParser::createZone: the document length seems bad\n"));
329 f << "###";
330 }
331 else {
332 WordMakerParserInternal::Zone zone(WordMakerParserInternal::Zone::Z_MAIN);
333 for (int i=0; i<4; ++i) { // f2=0|256
334 int val=int(input->readLong(2));
335 int const expected[]= {1,2,0,0};
336 if (val!=expected[i])
337 f << "f" << i << "=" << val << ",";
338 }
339 zone.m_beginPos=input->tell();
340 if (m_state->m_typeToZoneMap.find(WordMakerParserInternal::Zone::Z_MAIN)!=
341 m_state->m_typeToZoneMap.end()) {
342 MWAW_DEBUG_MSG(("WordMakerParser::createZone: arghhs, find multiple main zone\n"));
343 f << "###";
344 ascii().addPos(pos);
345 ascii().addNote(f.str().c_str());
346 throw libmwaw::ParseException();
347 }
348 auto zIt=m_state->m_typeToZoneMap.insert(std::make_pair(zone.m_type, zone));
349 m_state->m_currentZone=&zIt->second;
350 }
351 }
352 else if (what=="DPIC") {
353 if (m_state->m_currentZone)
354 ++m_state->m_currentZone->m_numPicture;
355 // the picture will be read by sendPicture
356 MWAWEntry picture;
357 picture.setBegin(pos+8);
358 picture.setLength(dataLen);
359 m_state->m_pictureList.push_back(picture);
360 }
361 else if (what=="FONT") {
362 if (!readFontNames(dataLen))
363 f << "###";
364 }
365 else if (what=="FOOT" || what=="HEAD") {
366 if (dataLen!=6) {
367 MWAW_DEBUG_MSG(("WordMakerParser::createZone: the footer/header length seems too short\n"));
368 f << "###";
369 }
370 else {
371 WordMakerParserInternal::Zone zone(what=="FOOT" ? WordMakerParserInternal::Zone::Z_FOOTER :
372 WordMakerParserInternal::Zone::Z_HEADER);
373 for (int i=0; i<6; ++i) {
374 int val=int(input->readLong(1));
375 if (i==0)
376 zone.m_id=val;
377 int const expected[]= {0, 1, 0/*or 1*/, 0, 0, 0};
378 if (val==expected[i])
379 continue;
380 switch (i) {
381 case 0:
382 if (val==1)
383 f << "odd,";
384 else if (val==2)
385 f << "even,";
386 else
387 f << "type=" << val << ",";
388 break;
389 case 1:
390 if (val==0) {
391 zone.m_hasTitlePage=true;
392 f << "hasTitle[page],";
393 }
394 else
395 f << "hasTitle[page]=" << val << ",";
396 break;
397 default:
398 f << "f" << i << "=" << val << ",";
399 break;
400 }
401 }
402 zone.m_beginPos=input->tell();
403 auto zIt=m_state->m_typeToZoneMap.insert(std::make_pair(zone.m_type, zone));
404 m_state->m_currentZone=&zIt->second;
405 }
406 }
407 else if (what=="PAGE") {
408 ++m_state->m_numPages;
409 if (m_state->m_currentZone)
410 ++m_state->m_currentZone->m_numParagraph;
411 if (dataLen!=0) {
412 MWAW_DEBUG_MSG(("WordMakerParser::createZone: the page length seems bad\n"));
413 f << "###";
414 }
415 }
416 else if (what=="PARA") // will be parsed when we send the zone
417 ;
418 else if (what=="PREC") {
419 if (!readPrintInfo(dataLen))
420 f << "###";
421 }
422 else if (what=="STYL") // follow TEXT, will be parsed when we send the zone
423 ;
424 else if (what=="TABS") // will be parsed when we send the zone
425 ;
426 else if (what=="TEXT") {
427 std::string text;
428 for (int i=0; i<dataLen; ++i) {
429 char c=char(input->readLong(1));
430 if (c)
431 text+=c;
432 else if (i+1!=dataLen)
433 text+="##[0]";
434 }
435 f << text << ",";
436 if (m_state->m_currentZone) {
437 m_state->m_currentZone->m_numCharacter+=dataLen;
438 ++m_state->m_currentZone->m_numParagraph;
439 }
440 }
441 else if (what=="WIND") {
442 if (dataLen!=8) {
443 MWAW_DEBUG_MSG(("WordMakerParser::createZone: the windows length seems too short\n"));
444 f << "###";
445 }
446 else {
447 int dim[4];
448 for (auto &d: dim) d=int(input->readLong(2));
449 f << MWAWBox2i(MWAWVec2i(dim[0],dim[1]),MWAWVec2i(dim[2],dim[3])) << ",";
450 }
451 }
452 else {
453 if (!what.empty()) {
454 MWAW_DEBUG_MSG(("WordMakerParser::createZone: unexpected tags=%s\n", what.c_str()));
455 }
456 f << "###";
457 }
458 ascii().addPos(pos);
459 ascii().addNote(f.str().c_str());
460 input->seek(pos+8+dataLen+(dataLen%2), librevenge::RVNG_SEEK_SET);
461 }
462 input->seek(endPos,librevenge::RVNG_SEEK_SET);
463 if (!input->isEnd()) {
464 MWAW_DEBUG_MSG(("WordMakerParser::createZone: find extra data\n"));
465 ascii().addPos(input->tell());
466 ascii().addNote("Entries(Extra):###");
467 }
468 return m_state->m_typeToZoneMap.find(WordMakerParserInternal::Zone::Z_MAIN)!=
469 m_state->m_typeToZoneMap.end();
470 }
471
472 ////////////////////////////////////////////////////////////
473 //
474 // Low level
475 //
476 ////////////////////////////////////////////////////////////
477
478 ////////////////////////////////////////////////////////////
479 // read the header
480 ////////////////////////////////////////////////////////////
481 bool WordMakerParser::checkHeader(MWAWHeader *header, bool /*strict*/)
482 {
483 *m_state = WordMakerParserInternal::State();
484
485 MWAWInputStreamPtr input = getInput();
486 if (!input || !input->hasDataFork())
487 return false;
488
489 if (!input->checkPosition(12)) {
490 MWAW_DEBUG_MSG(("WordMakerParser::checkHeader: file is too short\n"));
491 return false;
492 }
493 input->seek(0,librevenge::RVNG_SEEK_SET);
494 if (input->readULong(4) != 0x464f524d)
495 return false;
496 long len=long(input->readLong(4));
497 if (len<20 || !input->checkPosition(8+len)) {
498 MWAW_DEBUG_MSG(("WordMakerParser::checkHeader: can not read the data size\n"));
499 return false;
500 }
501 if (input->readULong(4) != 0x574f5231) {
502 MWAW_DEBUG_MSG(("WordMakerParser::checkHeader: can not find the first type\n"));
503 return false;
504 }
505 ascii().addPos(0);
506 ascii().addNote("FileHeader");
507
508 if (header)
509 header->reset(MWAWDocument::MWAW_T_WORDMAKER, 1);
510
511 return true;
512 }
513
514 ////////////////////////////////////////////////////////////
515 // read the print info
516 ////////////////////////////////////////////////////////////
517 bool WordMakerParser::readPrintInfo(long len)
518 {
519 MWAWInputStreamPtr input = getInput();
520 long pos = input->tell();
521 if (len<0x78 || !input->checkPosition(pos+len)) {
522 MWAW_DEBUG_MSG(("WordMakerParser::readPrintInfo: the entry seems too short\n"));
523 return false;
524 }
525 libmwaw::DebugStream f;
526 // print info
527 libmwaw::PrinterInfo info;
528 if (!info.read(input)) return false;
529 f << "Entries(PrintInfo):"<< info;
530
531 MWAWVec2i paperSize = info.paper().size();
532 MWAWVec2i pageSize = info.page().size();
533 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
534 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
535
536 // define margin from print info
537 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
538 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
539
540 // move margin left | top
541 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
542 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
543 lTopMargin -= MWAWVec2i(decalX, decalY);
544 rBotMargin += MWAWVec2i(decalX, decalY);
545
546 // decrease right | bottom
547 int rightMarg = rBotMargin.x() -50;
548 if (rightMarg < 0) rightMarg=0;
549 int botMarg = rBotMargin.y() -50;
550 if (botMarg < 0) botMarg=0;
551
552 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
553 getPageSpan().setMarginBottom(botMarg/72.0);
554 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
555 getPageSpan().setMarginRight(rightMarg/72.0);
556 getPageSpan().setFormLength(paperSize.y()/72.);
557 getPageSpan().setFormWidth(paperSize.x()/72.);
558
559 if (input->tell()!=pos+len)
560 ascii().addDelimiter(input->tell(),'|');
561 ascii().addPos(pos);
562 ascii().addNote(f.str().c_str());
563
564 input->seek(pos+len, librevenge::RVNG_SEEK_SET);
565 return true;
566 }
567
568 ////////////////////////////////////////////////////////////
569 // read the style
570 ////////////////////////////////////////////////////////////
571 bool WordMakerParser::readFontNames(long len)
572 {
573 MWAWInputStreamPtr input = getInput();
574 long pos = input->tell();
575 if (len<3 || !input->checkPosition(pos+len)) {
576 MWAW_DEBUG_MSG(("WordMakerParser::readFontNames: the entry seems too short\n"));
577 return false;
578 }
579
580 libmwaw::DebugStream f;
581 f << "FONT:";
582 int id=int(input->readULong(2));
583 f << "id=" << id << ",";
584 std::string name;
585 for (int i=2; i<len; ++i) {
586 char c=char(input->readLong(1));
587 if (c==0)
588 break;
589 name+=c;
590 }
591 f << name << ",";
592 if (!name.empty())
593 getFontConverter()->setCorrespondance(id, name);
594 ascii().addPos(pos-8);
595 ascii().addNote(f.str().c_str());
596
597 return true;
598 }
599
600 bool WordMakerParser::readFont(long len, MWAWFont &font)
601 {
602 font=MWAWFont();
603 MWAWInputStreamPtr input = getInput();
604 long pos = input->tell();
605 if (len!=8 || !input->checkPosition(pos+8)) {
606 MWAW_DEBUG_MSG(("WordMakerParser::readFont: the entry seems too short\n"));
607 return false;
608 }
609
610 libmwaw::DebugStream f;
611 f << "STYL:";
612
613 font.setId(int(input->readULong(2)));
614 font.setSize(float(input->readLong(1)));
615 uint32_t flags=0;
616 int val=int(input->readULong(1));
617 if (val&0x1) flags |= MWAWFont::boldBit;
618 if (val&0x2) flags |= MWAWFont::italicBit;
619 if (val&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
620 if (val&0x8) flags |= MWAWFont::embossBit;
621 if (val&0x10) flags |= MWAWFont::shadowBit;
622 if (val&0xe0) f << "fl=" << std::hex << (val&0xe0) << std::dec << ",";
623 font.setFlags(flags);
624 val=int(input->readULong(1));
625 switch (val&3) {
626 case 0:
627 break;
628 case 1:
629 font.set(MWAWFont::Script::super100());
630 break;
631 case 2:
632 font.set(MWAWFont::Script::sub100());
633 break;
634 default:
635 MWAW_DEBUG_MSG(("WordMakerParser::readFont: unknown script\n"));
636 f << "##script3,";
637 break;
638 }
639 if (val&0xfc) f << "fl2=" << std::hex << (val&0xfc) << std::dec << ",";
640 val=int(input->readULong(1));
641 if (val>0 && val<7) {
642 uint32_t const colors[]= {0,0xff0000,0xffff00,0xff00,0xffff, 0xff, 0xff00ff};
643 font.setColor(MWAWColor(colors[val]));
644 }
645 else if (val) {
646 MWAW_DEBUG_MSG(("WordMakerParser::readFont: unknown color\n"));
647 f << "##color=" << val << ",";
648 }
649 f << "font=[" << font.getDebugString(getFontConverter()) << "],";
650 val=int(input->readLong(2));
651 if (val) f << "f0=" << val << ",";
652 ascii().addPos(pos);
653 ascii().addNote(f.str().c_str());
654
655 return true;
656 }
657
658 bool WordMakerParser::readParagraph(long len, MWAWParagraph &para, MWAWFont &font)
659 {
660 font=MWAWFont();
661 std::vector<MWAWTabStop> oldTabs; // do not modify the tabulations
662 std::swap(*para.m_tabs,oldTabs);
663 para=MWAWParagraph();
664 std::swap(*para.m_tabs,oldTabs);
665
666 MWAWInputStreamPtr input = getInput();
667 long pos = input->tell();
668 if (len!=18 || !input->checkPosition(pos+18)) {
669 MWAW_DEBUG_MSG(("WordMakerParser::readParagraph: the entry seems too short\n"));
670 return false;
671 }
672 libmwaw::DebugStream f;
673 f << "PARA:";
674 para.m_marginsUnit=librevenge::RVNG_POINT;
675 for (int i=0; i<3; ++i) // first, left, right
676 para.m_margins[i]=double(input->readLong(2))/20;
677 *(para.m_margins[0])-=para.m_margins[1].get();
678 int val=int(input->readLong(1));
679 switch (val) {
680 case 0:
681 break;
682 case 8:
683 case 0x10:
684 para.setInterline(1+double(val)/16, librevenge::RVNG_PERCENT);
685 break;
686 default:
687 MWAW_DEBUG_MSG(("WordMakerParser::readParagraph: unknow interline\n"));
688 f << "###interline=" << val << ",";
689 break;
690 }
691 val=int(input->readULong(1));
692 switch (val & 3) {
693 case 1:
694 para.m_justify = MWAWParagraph::JustificationCenter;
695 break;
696 case 2:
697 para.m_justify = MWAWParagraph::JustificationRight;
698 break;
699 case 3:
700 para.m_justify = MWAWParagraph::JustificationFull;
701 break;
702 case 0: // left
703 default:
704 break;
705 }
706 if (val&0xfc) f << "fl=" << std::hex << (val&0xfc) << std::dec << ",";
707 val=int(input->readULong(1));
708 if (val)
709 para.setInterline(val, librevenge::RVNG_POINT);
710 val=int(input->readULong(1));
711 if (val&1)
712 para.m_spacings[1]=12/72.;
713 if (val&0x10)
714 para.m_spacings[2]=12/72.;
715 if (val&0xee) f << "fl1=" << std::hex << (val&0xee) << std::dec << ",";
716
717 f << "para=[" << para << "],";
718
719 readFont(8, font);
720 ascii().addPos(pos-8);
721 ascii().addNote(f.str().c_str());
722
723 return true;
724 }
725
726 bool WordMakerParser::readTabulations(long len, MWAWParagraph &para)
727 {
728 para.m_tabs->resize(0);
729
730 MWAWInputStreamPtr input = getInput();
731 long pos = input->tell();
732 if ((len%4)!=0 || !input->checkPosition(pos+len)) {
733 MWAW_DEBUG_MSG(("WordMakerParser::readTabulations: the entry seems bad\n"));
734 return false;
735 }
736 libmwaw::DebugStream f;
737 f << "TABS:";
738
739 int N=int(len/4);
740 f << "tabs=[";
741 for (int i=0; i<N; ++i) {
742 MWAWTabStop newTab;
743 newTab.m_position=double(input->readLong(2))/20/72;
744 int val=int(input->readULong(1));
745 switch (val&3) {
746 case 0:
747 break;
748 case 1:
749 newTab.m_alignment = MWAWTabStop::CENTER;
750 break;
751 case 2:
752 newTab.m_alignment = MWAWTabStop::RIGHT;
753 break;
754 case 3:
755 newTab.m_alignment = MWAWTabStop::DECIMAL;
756 break;
757 default:
758 break;
759 }
760 f << "[" << newTab << ",";
761 if (val&0xfc) f << "fl=" << std::hex << (val&0xfc) << ",";
762 val=int(input->readLong(1)); // 0
763 if (val) f << "f0=" << val << ",";
764 f << "],";
765 para.m_tabs->push_back(newTab);
766 }
767 f << "],";
768
769 ascii().addPos(pos-8);
770 ascii().addNote(f.str().c_str());
771
772 return true;
773 }
774
775 bool WordMakerParser::readPicture(long len, MWAWEmbeddedObject &object, MWAWBox2f &bdbox, int &page)
776 {
777 object=MWAWEmbeddedObject();
778 MWAWInputStreamPtr input = getInput();
779 long pos = input->tell();
780 if (len<=10 || !input->checkPosition(pos+len)) {
781 MWAW_DEBUG_MSG(("WordMakerParser::readPicture: the zone seems too short\n"));
782 return false;
783 }
784 libmwaw::DebugStream f;
785 f << "DPIC:";
786
787 int dim[2];
788 for (auto &d: dim) d=int(input->readLong(2));
789 page=int(input->readLong(2));
790 if (page) f << "page=" << page << ",";
791 int pPos[2];
792 for (auto &d: pPos) d=int(input->readLong(2));
793 MWAWVec2f orig=MWAWVec2f(float(pPos[0])/10, float(pPos[1])/10);
794 bdbox=MWAWBox2f(orig, orig+MWAWVec2f(float(dim[0]), float(dim[1])));
795 f << "box=" << bdbox << ",";
796 std::shared_ptr<MWAWPict> pict(MWAWPictData::get(input, int(len-10)));
797 if (pict && pict->getBinary(object) && !object.m_dataList.empty()) {
798 #ifdef DEBUG_WITH_FILES
799 static int volatile pictName = 0;
800 libmwaw::DebugStream f2;
801 f2 << "PICT-" << ++pictName;
802 libmwaw::Debug::dumpFile(object.m_dataList[0], f2.str().c_str());
803 ascii().skipZone(pos+10, pos+len-1);
804 #endif
805 }
806 else {
807 MWAW_DEBUG_MSG(("WordMakerParser::readPicture: can not retrieve a object\n"));
808 f << "###";
809 }
810
811 ascii().addPos(pos-8);
812 ascii().addNote(f.str().c_str());
813
814 return true;
815 }
816
817 ////////////////////////////////////////////////////////////
818 // send data
819 ////////////////////////////////////////////////////////////
820 void WordMakerParser::newPage()
821 {
822 if (m_state->m_actPage >= m_state->m_numPages)
823 return;
824
825 m_state->m_actPage++;
826 if (!getTextListener())
827 return;
828 getTextListener()->insertBreak(MWAWTextListener::PageBreak);
829 }
830
831 bool WordMakerParser::sendPicture(MWAWEntry const &entry)
832 {
833 MWAWInputStreamPtr input = getInput();
834 if (!input)
835 return false;
836 auto listener=getTextListener();
837 if (!listener) {
838 MWAW_DEBUG_MSG(("WordMakerParser::sendPicture: can not find the text listener\n"));
839 return false;
840 }
841 if (!entry.valid() || entry.length()<=20 || !input->checkPosition(entry.end())) {
842 MWAW_DEBUG_MSG(("WordMakerParser::sendPicture: can not find the picture zone\n"));
843 return false;
844 }
845 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
846 MWAWEmbeddedObject object;
847 MWAWBox2f bdbox;
848 int objPage;
849 if (!readPicture(entry.length(),object,bdbox,objPage))
850 return false;
851 MWAWPosition pictPos(bdbox[0], bdbox.size(), librevenge::RVNG_POINT);
852 pictPos.setPage(objPage+1);
853 pictPos.setRelativePosition(MWAWPosition::Page);
854 pictPos.m_wrapping = MWAWPosition::WBackground;
855 listener->insertPicture(pictPos, object);
856 return true;
857 }
858
859 bool WordMakerParser::sendZone(WordMakerParserInternal::Zone const &zone)
860 {
861 MWAWInputStreamPtr input = getInput();
862 if (!input)
863 return false;
864 auto listener=getTextListener();
865 if (!listener) {
866 MWAW_DEBUG_MSG(("WordMakerParser::sendZone: can not find the text listener\n"));
867 return false;
868 }
869 long endPos=m_state->m_endDataZone;
870 if (zone.m_beginPos<=20 || !input->checkPosition(zone.m_beginPos+8) || zone.m_beginPos+8>endPos) {
871 MWAW_DEBUG_MSG(("WordMakerParser::sendZone: can not find the text zone\n"));
872 return false;
873 }
874 input->seek(zone.m_beginPos, librevenge::RVNG_SEEK_SET);
875
876 MWAWParagraph para;
877 MWAWFont paraFont;
878 listener->setParagraph(para);
879 while (input->tell()+8<endPos) {
880 long pos=input->tell();
881 std::string what;
882 for (int i=0; i<4; ++i)
883 what+=char(input->readULong(1));
884 long dataLen=long(input->readLong(4));
885 if (dataLen<0 || (long)((unsigned long)pos+8+(unsigned long)dataLen)<pos+8 || pos+8+dataLen>endPos)
886 return false;
887 if (what=="DOC " || what=="FOOT" || what=="HEAD") // begin of the next zone
888 return true;
889 else if (what=="PAGE")
890 newPage();
891 else if (what=="PARA") {
892 if (readParagraph(dataLen, para, paraFont))
893 listener->setParagraph(para);
894 }
895 else if (what=="TABS") {
896 if (readTabulations(dataLen, para))
897 listener->setParagraph(para);
898 }
899 else if (what=="TEXT") {
900 // first look for the following style zone
901 long nextPos=pos+8+dataLen+(dataLen%2);
902 std::map<int,MWAWFont> posToFont;
903 if (nextPos+8<endPos) {
904 input->seek(nextPos, librevenge::RVNG_SEEK_SET);
905 std::string what2;
906 for (int i=0; i<4; ++i)
907 what2+=char(input->readULong(1));
908 long dataLen2=long(input->readLong(4));
909 if (what2=="STYL" && nextPos+8+dataLen2<=endPos) {
910 if ((dataLen2%10)!=0) {
911 MWAW_DEBUG_MSG(("WordMakerParser::sendZone: the style length seems bad\n"));
912 ascii().addPos(nextPos);
913 ascii().addNote("###");
914 }
915 else {
916 int N=int(dataLen2/10);
917 libmwaw::DebugStream f;
918 for (int i=0; i<N; ++i) {
919 f << "STYL-" << i << ":";
920 long pos2=input->tell();
921 int cPos=int(input->readULong(2));
922 if (cPos) f << "pos=" << cPos << ",";
923 MWAWFont font;
924 readFont(8, font);
925 posToFont[cPos]=font;
926 input->seek(pos2+10, librevenge::RVNG_SEEK_SET);
927 ascii().addPos(pos2);
928 ascii().addNote(f.str().c_str());
929 }
930 }
931 }
932 }
933 // now read the text
934 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
935 listener->setFont(paraFont);
936 for (int i=0; i<dataLen; ++i) {
937 auto it=posToFont.find(i);
938 if (it!=posToFont.end())
939 listener->setFont(it->second);
940
941 unsigned char c=(unsigned char)(input->readULong(1));
942 switch (c) {
943 case 0x4:
944 listener->insertField(MWAWField(MWAWField::PageNumber));
945 break;
946 case 0x5: {
947 MWAWField date(MWAWField::Date);
948 date.m_DTFormat = "%a, %b %d, %Y";
949 listener->insertField(date);
950 break;
951 }
952 case 0x6: {
953 MWAWField time(MWAWField::Time);
954 time.m_DTFormat="%H:%M";
955 listener->insertField(time);
956 break;
957 }
958 case 0x9:
959 listener->insertTab();
960 break;
961 default:
962 if (c<0x1f) {
963 MWAW_DEBUG_MSG(("WordMakerParser::sendZone: find unknown char=%d\n", int(c)));
964 break;
965 }
966 listener->insertCharacter(c);
967 }
968 }
969 listener->insertEOL();
970 }
971 input->seek(pos+8+dataLen+(dataLen%2), librevenge::RVNG_SEEK_SET);
972 }
973 return true;
974 }
975
976 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: