Debian Codegraph @

src/lib/ScriptWriterParser.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 <array>
35 #include <cstring>
36 #include <iomanip>
37 #include <iostream>
38 #include <limits>
39 #include <set>
40 #include <sstream>
41 #include <stack>
42
43 #include <librevenge/librevenge.h>
44
45 #include "MWAWCell.hxx"
46 #include "MWAWTextListener.hxx"
47 #include "MWAWFont.hxx"
48 #include "MWAWFontConverter.hxx"
49 #include "MWAWHeader.hxx"
50 #include "MWAWParagraph.hxx"
51 #include "MWAWPosition.hxx"
52 #include "MWAWPictMac.hxx"
53 #include "MWAWPrinter.hxx"
54 #include "MWAWSection.hxx"
55 #include "MWAWStringStream.hxx"
56 #include "MWAWSubDocument.hxx"
57 #include "MWAWTable.hxx"
58
59 #include "ScriptWriterParser.hxx"
60
61 /** Internal: the structures of a ScriptWriterParser */
62 namespace ScriptWriterParserInternal
63 {
64 ////////////////////////////////////////
65 //! Internal: the paragraph of a ScriptWriterParser
66 struct Paragraph {
67 //! constructor
68 Paragraph()
69 : m_numChar(0)
70 , m_height(1)
71 , m_position(0,0)
72 , m_align(-3)
73 {
74 }
75 //! return true if the paragraph is empty
76 bool empty() const
77 {
78 return m_numChar<=0;
79 }
80 //! an operator<<
81 friend std::ostream &operator<<(std::ostream &o, Paragraph const &para)
82 {
83 if (para.empty())
84 return o;
85 o << "n=" << para.m_numChar << ",";
86 if (para.m_height!=1)
87 o << "h=" << para.m_height << "li,";
88 o << "pos=" << para.m_position << ",";
89 switch (para.m_align) {
90 case 0:
91 break;
92 case 1:
93 o << "center,";
94 break;
95 case -1:
96 o << "right,";
97 break;
98 case 5:
99 o << "justify,";
100 break;
101 default:
102 o << "###align=" << para.m_align << ",";
103 MWAW_DEBUG_MSG(("ScriptWriterParserInternal::Paragraph: unknown alignment\n"));
104 break;
105 }
106 return o;
107 }
108 //! the number of caracters
109 int m_numChar;
110 //! the height
111 int m_height;
112 //! the position in the text zone
113 MWAWVec2i m_position;
114 //! the alignment
115 int m_align;
116 };
117
118 ////////////////////////////////////////
119 //! Internal: the frame of a ScriptWriterParser
120 struct Frame {
121 //! constructor
122 Frame()
123 : m_position()
124 , m_numChar(0)
125 {
126 }
127 //! the frame bounding box
128 MWAWBox2i m_position;
129 //! the number of caracters
130 int m_numChar;
131 //! the text and the style entries
132 MWAWEntry m_entries[2];
133 };
134
135 ////////////////////////////////////////
136 //! Internal: the page of a ScriptWriterParser
137 struct Page {
138 //! constructor
139 Page()
140 : m_lastPage(true)
141 , m_zoneToParagraphsMap()
142 {
143 }
144
145 //! a flag to know if this page is the last one
146 bool m_lastPage;
147 //! a map id to left/right paragraph
148 std::map<int, std::array<Paragraph,2> > m_zoneToParagraphsMap;
149 //! two map zone, position to a font, one for each potential columns
150 std::map<std::pair<int,int>, MWAWFont> m_zonePosToFontMap[2];
151 };
152
153 ////////////////////////////////////////
154 //! Internal: the data of a header footer of a ScriptWriterParser
155 struct HFData {
156 //! constructor
157 HFData()
158 : m_numFrames(0)
159 , m_frames()
160
161 , m_pageNumberOrigin(-1,-1)
162 , m_dateOrigin(-1,-1)
163 , m_hasPicture(false)
164 , m_picturePosition()
165 , m_picture()
166 {
167 }
168 //! the number of frames: text zone
169 int m_numFrames;
170 //! the list of frames
171 std::vector<Frame> m_frames;
172
173 //! returns true if the zone is empty
174 bool empty() const
175 {
176 return m_frames.empty() && !m_hasPicture && !hasDate() && !hasPageNumber();
177 }
178 //! returns true if the zone has a date
179 bool hasDate() const
180 {
181 return m_dateOrigin[0]>=0 && m_dateOrigin[1]<1000 && m_dateOrigin[1]>=0;
182 }
183 //! returns true if the zone has a page number
184 bool hasPageNumber() const
185 {
186 return m_pageNumberOrigin[0]>=0 && m_pageNumberOrigin[1]<1000 && m_pageNumberOrigin[1]>=0;
187 }
188 //! the page number origin, only valid if 0<dim[0]<1000
189 MWAWVec2i m_pageNumberOrigin;
190 //! the date field origin, only valid if 0<dim[0]<1000
191 MWAWVec2i m_dateOrigin;
192 //! a flag to know if there is a picture
193 bool m_hasPicture;
194 //! the picture position
195 MWAWBox2i m_picturePosition;
196 //! the picture entry
197 MWAWEntry m_picture;
198 };
199
200 ////////////////////////////////////////
201 //! Internal: the state of a ScriptWriterParser
202 struct State {
203 //! constructor
204 State()
205 : m_actPage(0)
206 , m_numPages(1)
207
208 , m_documentType(-1)
209 , m_defaultFont(22,12)
210 , m_lineSpacing(12)
211 , m_columnSepPos(-1)
212 , m_columnOriginPos(-1)
213
214 , m_hasTitlePage(false)
215 , m_pages()
216 , m_mainZoneEntry()
217 {
218 m_lineSpacingPercent[0]=m_lineSpacingPercent[1]=1;
219 for (auto &d : m_leftMargins) d=0;
220 }
221
222 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
223
224 /** the document type: 0: advanced, 1: normal, 2: dual */
225 int m_documentType;
226 /** the default font */
227 MWAWFont m_defaultFont;
228 /** the default line spacing */
229 int m_lineSpacing;
230 /** the default line spacing percent */
231 int m_lineSpacingPercent[2];
232 /** the left margin positions */
233 int m_leftMargins[6];
234 /** the column separator position */
235 int m_columnSepPos;
236 /** the second column left margin */
237 int m_columnOriginPos;
238
239 /** a flag to know if the first page is a title page */
240 bool m_hasTitlePage;
241 /** the pages list */
242 std::vector<ScriptWriterParserInternal::Page> m_pages;
243 /// the main zone entry
244 MWAWEntry m_mainZoneEntry;
245
246 /** the header/footer data*/
247 HFData m_hfData[2];
248 /** the left/right columns tabulations */
249 std::vector<MWAWTabStop> m_tabs[2];
250 };
251
252 ////////////////////////////////////////
253 //! Internal: the subdocument of a ScriptWriterParser
254 class SubDocument final : public MWAWSubDocument
255 {
256 public:
257 SubDocument(ScriptWriterParser &pars, MWAWInputStreamPtr const &input, HFData const &hf)
258 : MWAWSubDocument(&pars, input, MWAWEntry())
259 , m_hfData(&hf)
260 {
261 }
262
263 //! destructor
264 ~SubDocument() final {}
265
266 //! operator!=
267 bool operator!=(MWAWSubDocument const &doc) const final
268 {
269 if (MWAWSubDocument::operator!=(doc)) return true;
270 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
271 if (!sDoc) return true;
272 if (&m_hfData != &sDoc->m_hfData) return true;
273 return false;
274 }
275
276 //! the parser function
277 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
278
279 protected:
280 //! the header/footer zone
281 HFData const *m_hfData;
282
283 SubDocument(SubDocument const &)=delete;
284 SubDocument &operator=(SubDocument const &)=delete;
285 };
286
287 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
288 {
289 if (!listener.get()) {
290 MWAW_DEBUG_MSG(("ScriptWriterParserInternal::SubDocument::parse: no listener\n"));
291 return;
292 }
293 auto *parser=dynamic_cast<ScriptWriterParser *>(m_parser);
294 if (!parser) {
295 MWAW_DEBUG_MSG(("ScriptWriterParserInternal::SubDocument::parse: no parser\n"));
296 return;
297 }
298
299 long pos = m_input->tell();
300 if (m_hfData)
301 parser->send(*m_hfData);
302 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
303 }
304 }
305
306 ////////////////////////////////////////////////////////////
307 // constructor/destructor, ...
308 ////////////////////////////////////////////////////////////
309 ScriptWriterParser::ScriptWriterParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
310 : MWAWTextParser(input, rsrcParser, header)
311 , m_state()
312 {
313 setAsciiName("main-1");
314
315 m_state.reset(new ScriptWriterParserInternal::State);
316
317 // reduce the margin (in case, the page is not defined)
318 getPageSpan().setMargins(0.1);
319 }
320
321 ScriptWriterParser::~ScriptWriterParser()
322 {
323 }
324
325 ////////////////////////////////////////////////////////////
326 // the parser
327 ////////////////////////////////////////////////////////////
328 void ScriptWriterParser::parse(librevenge::RVNGTextInterface *docInterface)
329 {
330 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
331 bool ok = true;
332 try {
333 // create the asciiFile
334 ascii().setStream(getInput());
335 ascii().open(asciiName());
336
337 checkHeader(nullptr);
338 ok = createZones();
339 if (ok) {
340 createDocument(docInterface);
341 sendMainZone();
342 }
343
344 ascii().reset();
345 }
346 catch (...) {
347 MWAW_DEBUG_MSG(("ScriptWriterParser::parse: exception catched when parsing\n"));
348 ok = false;
349 }
350
351 resetTextListener();
352 if (!ok) throw(libmwaw::ParseException());
353 }
354
355 ////////////////////////////////////////////////////////////
356 // create the document
357 ////////////////////////////////////////////////////////////
358 void ScriptWriterParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
359 {
360 if (!documentInterface) throw(libmwaw::ParseException());
361 if (getTextListener()) {
362 MWAW_DEBUG_MSG(("ScriptWriterParser::createDocument: listener already exist\n"));
363 return;
364 }
365
366 m_state->m_numPages=int(m_state->m_pages.size());
367 bool const hasHF[]= {!m_state->m_hfData[0].empty(), !m_state->m_hfData[1].empty()};
368 std::vector<MWAWPageSpan> pageList;
369 int numPageDone=0;
370 if (m_state->m_hasTitlePage && (hasHF[0] || hasHF[1])) {
371 MWAWPageSpan ps(getPageSpan());
372 ps.setPageSpan(1);
373 pageList.push_back(ps);
374 numPageDone=1;
375 }
376
377 if (m_state->m_numPages>numPageDone) {
378 MWAWPageSpan ps(getPageSpan());
379 ps.setPageSpan(m_state->m_numPages-numPageDone);
380 for (int hf=0; hf<2; ++hf) {
381 if (!hasHF[hf])
382 continue;
383 MWAWHeaderFooter hfDoc(hf==1 ? MWAWHeaderFooter::FOOTER : MWAWHeaderFooter::HEADER, MWAWHeaderFooter::ALL);
384 hfDoc.m_subDocument.reset(new ScriptWriterParserInternal::SubDocument(*this, getInput(),m_state->m_hfData[hf]));
385 ps.setHeaderFooter(hfDoc);
386 }
387 pageList.push_back(ps);
388 }
389
390 //
391 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
392 setTextListener(listen);
393 listen->startDocument();
394 }
395
396
397 ////////////////////////////////////////////////////////////
398 //
399 // Intermediate level
400 //
401 ////////////////////////////////////////////////////////////
402 bool ScriptWriterParser::createZones()
403 {
404 MWAWInputStreamPtr input = getInput();
405 if (!input || !readDocument())
406 return false;
407 for (auto &hf : m_state->m_hfData) {
408 if (hf.m_hasPicture && !readPicture(hf.m_picture))
409 return false;;
410 for (int col=0; col<hf.m_numFrames; ++col) {
411 ScriptWriterParserInternal::Frame frame;
412 if (!readFrame(frame))
413 return false;
414 hf.m_frames.push_back(frame);
415 }
416 }
417
418 if (!readPrintInfo())
419 return false;
420
421 while (true) {
422 ScriptWriterParserInternal::Page page;
423 if (!readPage(page))
424 return false;
425 m_state->m_pages.push_back(page);
426 if (!page.m_lastPage) continue;
427
428 if (!readTextZone())
429 return false;
430
431 if (!input->isEnd()) {
432 ascii().addPos(input->tell());
433 ascii().addNote("Entries(Extra):###");
434 MWAW_DEBUG_MSG(("ScriptWriterParser::createZones: find extra data\n"));
435 return false;
436 }
437 if (int(m_state->m_pages.size())!=m_state->m_numPages) {
438 // rare but can happens
439 MWAW_DEBUG_MSG(("ScriptWriterParser::createZones: the number of pages seems bad %d!=%d\n", int(m_state->m_pages.size()), m_state->m_numPages));
440 }
441 return true;
442 }
443 return false;
444 }
445
446 bool ScriptWriterParser::readDocument()
447 {
448 MWAWInputStreamPtr input = getInput();
449 if (!input->checkPosition(0x1ea)) {
450 MWAW_DEBUG_MSG(("ScriptWriterParser::readDocument: the entry seems too short\n"));
451 return false;
452 }
453
454 input->seek(4, librevenge::RVNG_SEEK_SET);
455 libmwaw::DebugStream f;
456 int val;
457
458 long pos = input->tell();
459 f << "Entries(Document):";
460 input->seek(pos+10, librevenge::RVNG_SEEK_SET);
461 ascii().addDelimiter(input->tell(),'|');
462 val=int(input->readLong(2));
463 if (val!=1)
464 f << "f0=" << val << ",";
465 f << "IDS=["; // para zone id, ???
466 for (int i=0; i<2; ++i) f << std::hex << input->readULong(4) << std::dec << ",";
467 f << "],";
468 m_state->m_lineSpacing=int(input->readLong(2));
469 if (m_state->m_lineSpacing!=12) f << "line[spacing]=" << m_state->m_lineSpacing << ",";
470 val=int(input->readLong(2));
471 if (val!=9) f << "f1=" << val << ",";
472 val=int(input->readLong(2)); // 1|4
473 if (val!=1) f << "f2=" << val << ",";
474 m_state->m_columnSepPos=int(input->readLong(2));
475 f << "column[pos]=" << m_state->m_columnSepPos << ",";
476 int lastVal=-1;
477 f << "margin[left]=[";
478 for (int i=0; i<6; ++i) { // v0 and v1: margin left of the first column
479 m_state->m_leftMargins[i]=val=int(input->readLong(2));
480 if (val!=lastVal)
481 f << val << ",";
482 else
483 f << "_,";
484 lastVal=val;
485 }
486 f << "],";
487 m_state->m_columnOriginPos=int(input->readLong(2)); // or ~1000 if only one column
488 f << "beg[col2]=" << m_state->m_columnOriginPos << ",";
489 lastVal=-1;
490 f << "margin[right]=[";
491 for (int i=0; i<6; ++i) { // v0 and v1: margin right of the first column
492 val=int(input->readLong(2));
493 if (val!=lastVal)
494 f << val << ",";
495 else
496 f << "_,";
497 lastVal=val;
498 }
499 f << "],";
500 f << "f3=" << input->readLong(2) << ",";
501 if (!readFont(m_state->m_defaultFont))
502 f << "###";
503 else
504 f << "font=[" << m_state->m_defaultFont.getDebugString(getFontConverter()) << "]],";
505 ascii().addPos(pos);
506 ascii().addNote(f.str().c_str());
507 input->seek(0x42, librevenge::RVNG_SEEK_SET);
508
509 pos=input->tell();
510 f.str("");
511 f << "Document-1:";
512 for (int i=0; i<2; ++i) {
513 val=int(input->readLong(2));
514 if (val!=1) f << "f" << i << "=" << val << ",";
515 }
516 for (int st=0; st<2; ++st) {
517 // unsure, maybe related to the current selection
518 int unkn[3], unkn2[3];
519 f << "unkn" << st << "=[";
520 for (auto &v : unkn) {
521 v=int(input->readLong(2));
522 if (v==1)
523 f << "_,";
524 else
525 f << v << ",";
526 }
527 f << "],";
528 for (auto &v : unkn2)
529 v=int(input->readLong(2));
530 if (unkn[0]!=unkn2[0] || unkn[1]!=unkn2[1] || unkn[2]!=unkn2[2])
531 f << "unkn" << st << "[col2]=[" << unkn2[0] << "," << unkn2[1] << "," << unkn2[2] << "],";
532 }
533 f << "left=" << input->readLong(2) << ","; // unsure
534 f << "hf[S#]=" << input->readLong(2) << ","; // unsure two header/footer dimension, use for?
535 ascii().addDelimiter(input->tell(),'|');
536 input->seek(2, librevenge::RVNG_SEEK_CUR); // 200
537 ascii().addDelimiter(input->tell(),'|');
538 for (int col=0; col<2; ++col) {
539 val=int(input->readLong(2));
540 if (val!=1) {
541 if (val>1 && val<=3)
542 m_state->m_lineSpacingPercent[col]=val;
543 else
544 f << "###";
545 f << "line[spacing," << col << "]=" << val << "%,";
546 }
547 }
548 ascii().addPos(pos);
549 ascii().addNote(f.str().c_str());
550
551 input->seek(0x68, librevenge::RVNG_SEEK_SET);
552 pos=input->tell();
553 f.str("");
554 f << "Document-tabs:";
555 int N[2];
556 for (int col=0; col<2; ++col) {
557 N[col]=int(input->readLong(2));
558 f << "N" << col << "=" << N[col] << ",";
559 if (N[col]<0 || N[col]>20) {
560 f << "###";
561 MWAW_DEBUG_MSG(("ScriptWriterParser::readDocument: the numbers of tabs seems bad\n"));
562 N[col]=0;
563 }
564 }
565 for (int col=0; col<2; ++col) {
566 long actPos=input->tell();
567 f << "pos" << col << "=[";
568 MWAWTabStop tab;
569 for (int i=0; i<N[col]; ++i) {
570 val=int(input->readLong(2));
571 f << val << ",";
572 tab.m_position=double(val)/72;
573 m_state->m_tabs[col].push_back(tab);
574 }
575 f << "],";
576 input->seek(actPos+40, librevenge::RVNG_SEEK_SET);
577 }
578 val=int(input->readLong(2));
579 if (val) f << "f0=" << val << ",";
580 ascii().addPos(pos);
581 ascii().addNote(f.str().c_str());
582
583 input->seek(0xbe, librevenge::RVNG_SEEK_SET);
584 for (int h=0; h<2; ++h) {
585 ScriptWriterParserInternal::HFData &hf=m_state->m_hfData[h];
586 char const *wh[]= {"header", "footer"};
587 pos=input->tell();
588 f.str("");
589 f << "Document-" << wh[h] << ":";
590 int dim[4];
591 for (auto &d : dim) d=int(input->readLong(2));
592 for (int type=0; type<2; ++type) {
593 if (dim[type]<0 || dim[type]>=4000 || dim[type+2]<0) {
594 input->seek(6, librevenge::RVNG_SEEK_CUR);
595 continue;
596 }
597 if (type==0)
598 hf.m_pageNumberOrigin=MWAWVec2i(dim[type],dim[type+2]);
599 else
600 hf.m_dateOrigin=MWAWVec2i(dim[type],dim[type+2]);
601 f << (type==0 ? "pagenumber" : "date") << "=[pos=" << MWAWVec2i(dim[type],dim[type+2]) << ",";
602 MWAWFont font;
603 readFont(font);
604 f << "font=[" << font.getDebugString(getFontConverter()) << "]],";
605 }
606 val=int(input->readLong(4));
607 if (val) {
608 hf.m_hasPicture=true;
609 f << "pict[id]=" << std::hex << val << std::dec << ",";
610 }
611
612 for (int i=0; i<3; ++i) {
613 val=int(input->readLong(2));
614 if (val)
615 f << "f" << i << "=" << val << ",";
616 }
617 for (int i=0; i<2; ++i) dim[i]=int(input->readLong(2));
618 if (dim[0]>0 && dim[1]>0)
619 f << "pict[sz]=" << MWAWVec2i(dim[1],dim[0]) << ",";
620 for (auto &d : dim) d=int(input->readLong(2));
621 if (dim[2]>dim[0] && dim[3]>dim[1]) {
622 hf.m_picturePosition=MWAWBox2i(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2]));
623 f << "pict[dim]=" << hf.m_picturePosition << ",";
624 }
625 ascii().addDelimiter(input->tell(),'|');
626 ascii().addPos(pos);
627 ascii().addNote(f.str().c_str());
628
629 input->seek(pos+46, librevenge::RVNG_SEEK_SET);
630 pos=input->tell();
631 f.str("");
632 f << "Document-" << wh[h] << "A:";
633 bool lastOk=true;
634 for (int i=0; i<10; ++i) { // unsure what is exactly the limit, at least 3
635 val=int(input->readLong(4));
636 if (!val) {
637 lastOk=false;
638 continue;
639 }
640 if (lastOk)
641 ++hf.m_numFrames;
642 f << "id" << i << "=" << std::hex << val << std::dec << ",";
643 }
644 for (int i=0; i<8; ++i) {
645 val=int(input->readLong(2));
646 if (val)
647 f << "g" << i << "=" << val << ",";
648 }
649 for (int i=0; i<2; ++i) dim[i]=int(input->readLong(2));
650 // fixme: step=0, dim[0]>0 means show header, dim[1]>0 mean show footer
651 if (h==0) {
652 if (dim[0]>0)
653 f << "h[max,header]=" << dim[0] << ",";
654 if (dim[1]>0)
655 f << "h[min,footer]=" << dim[1] << ",";
656 }
657 else
658 f << "unkn=" << MWAWVec2i(dim[0],dim[1]);
659 ascii().addPos(pos);
660 ascii().addNote(f.str().c_str());
661
662 input->seek(pos+60, librevenge::RVNG_SEEK_SET);
663 }
664
665 input->seek(0x192, librevenge::RVNG_SEEK_SET);
666 pos=input->tell();
667 f.str("");
668 f<<"Document-2:";
669 val=int(input->readLong(2));
670 if (val!=1)
671 f << "ruler[unit]=" << val << ","; // 1-4
672 val=int(input->readULong(2));
673 if (val)
674 f << "fl0=" << std::hex << val << std::dec << ",";
675 f << "flags=["; // empty on fl0 and fl8 set
676 for (int i=0; i<8; ++i) { // fl1=titlepage?, fl4=fl7=1 header?
677 val=int(input->readLong(1));
678 if (val==1) {
679 if (i==1) {
680 m_state->m_hasTitlePage=true;
681 f << "title[page],";
682 }
683 else
684 f << "*,";
685 }
686 else if (val)
687 f << val << ",";
688 else
689 f << "_,";
690 }
691 f << "],";
692 val=int(input->readLong(2));
693 if (val) f << "first[page]=" << val << ",";
694 for (int i=0; i<7; ++i) { // f9: num in parastyle group
695 val=int(input->readLong(2));
696 int const expected[]= {-1, 0, 0, 0xbb8, 1, 1, -1};
697 if (val==expected[i])
698 continue;
699 if (i==4) {
700 m_state->m_numPages=val;
701 f << "num[pages]=" << val << ",";
702 }
703 else
704 f << "f" << i+4 << "=" << val << ",";
705 }
706 val=int(input->readULong(2));
707 f << "fl=" << std::hex << val << std::dec << ",";
708 val=int(input->readLong(2));
709 if (val!=0x2e8)
710 f << "f4=" << val << ",";
711 f << "ID=" << std::hex << input->readULong(4) << std::dec << ",";
712 for (int i=0; i<2; ++i) { // g0=1|2, g1=1|2
713 val=int(input->readLong(2));
714 if (val!=1)
715 f << "g" << i << "=" << val << ",";
716 }
717 ascii().addDelimiter(input->tell(),'|');
718 ascii().addPos(pos);
719 ascii().addNote(f.str().c_str());
720 input->seek(0x1ea, librevenge::RVNG_SEEK_SET);
721 return true;
722 }
723
724 bool ScriptWriterParser::readFrame(ScriptWriterParserInternal::Frame &frame)
725 {
726 MWAWInputStreamPtr input = getInput();
727 long pos = input->tell();
728 long len = long(input->readULong(4));
729 long endPos=pos+4+len;
730
731 libmwaw::DebugStream f;
732 f << "Entries(Frame):";
733 if (len<0x6a || endPos<pos+4+0x6a || !input->checkPosition(endPos)) {
734 MWAW_DEBUG_MSG(("ScriptWriterParser::readFrame: the zone seems too short\n"));
735 f << "###";
736 ascii().addPos(pos);
737 ascii().addNote(f.str().c_str());
738 return false;
739 }
740 input->seek(pos+4+94, librevenge::RVNG_SEEK_SET);
741 int N=int(input->readULong(2));
742 if (len!=0x68+2*N) {
743 MWAW_DEBUG_MSG(("ScriptWriterParser::readFrame: can not find the number of lines\n"));
744 f << "###";
745 ascii().addPos(pos);
746 ascii().addNote(f.str().c_str());
747 return false;
748 }
749 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
750 for (int i=0; i<3; ++i) { // dim0: global position, dim0=dim1, dim2: local dimension
751 int dim[4];
752 for (auto &d : dim) d=int(input->readLong(2));
753 MWAWBox2i box(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2]));
754 if (i==0)
755 frame.m_position=box;
756 else if (box==frame.m_position)
757 continue;
758 f << "dim" << i << "=" << box << ",";
759 }
760 ascii().addDelimiter(input->tell(),'|');
761 input->seek(pos+4+58, librevenge::RVNG_SEEK_SET);
762 ascii().addDelimiter(input->tell(),'|');
763 f << "cPos=[";
764 for (int i=0; i<2; ++i) {
765 int val=int(input->readULong(2));
766 if (i==1)
767 frame.m_numChar=val;
768 f << val << ",";
769 }
770 f << "],";
771 f << "IDS=[";
772 for (int i=0; i<2; ++i) f << std::hex << input->readULong(4) << std::dec << ",";
773 f << "],";
774 for (int i=0; i<2; ++i) { // f0=0|ff00
775 int val=int(input->readLong(2));
776 if (val)
777 f << "f" << i << "=" << val << ",";
778 }
779 f << "IDS1=" << std::hex << input->readULong(4) << std::dec << ",";
780 for (int i=0; i<2; ++i) {
781 int val=int(input->readLong(2));
782 if (val!=(i==0 ? 1 : -1))
783 f << "f" << i+2 << "=" << val << ",";
784 }
785 f << "IDS2=" << std::hex << input->readULong(4) << std::dec << ",";
786 for (int i=0; i<4; ++i) {
787 int val=int(input->readLong(2));
788 if (val)
789 f << "f" << i+4 << "=" << val << ",";
790 }
791 input->seek(2, librevenge::RVNG_SEEK_CUR); // num line
792 f << "cPos[line]=[";
793 for (int i=0; i<=N; ++i) f << input->readULong(2) << ",";
794 f << "],";
795 for (int i=0; i<3; ++i) {
796 int val=int(input->readLong(2));
797 if (val)
798 f << "g" << i << "=" << val << ",";
799 }
800 ascii().addPos(pos);
801 ascii().addNote(f.str().c_str());
802 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
803
804 // now the text zone
805 pos=input->tell();
806 len=long(input->readULong(4));
807 if ((long)((unsigned long)pos+4+(unsigned long)len)<pos+4 || !input->checkPosition(pos+4+len)) {
808 MWAW_DEBUG_MSG(("ScriptWriterParser::readFrame: can not find a header/footer's text zone\n"));
809 ascii().addPos(pos);
810 ascii().addNote("Entries(TextData):###");
811 return false;
812 }
813 frame.m_entries[0].setBegin(pos+4);
814 frame.m_entries[0].setLength(len);
815 f.str("");
816 f << "Entries(TextData):";
817 std::string text;
818 for (int c=0; c<len; ++c) text+=char(input->readLong(1));
819 f << text;
820 ascii().addPos(pos);
821 ascii().addNote(f.str().c_str());
822 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
823
824 // and the style
825 pos=input->tell();
826 len=long(input->readULong(4));
827 if (pos+4+len<pos+4+22 || !input->checkPosition(pos+4+len)) {
828 MWAW_DEBUG_MSG(("ScriptWriterParser::readFrame: can not find a the style zone\n"));
829 ascii().addPos(pos);
830 ascii().addNote("Entries(HFStyle):###");
831 return false;
832 }
833 frame.m_entries[1].setBegin(pos+4);
834 frame.m_entries[1].setLength(len);
835 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
836
837 return true;
838 }
839
840 bool ScriptWriterParser::readFont(MWAWFont &font)
841 {
842 font=MWAWFont();
843 MWAWInputStreamPtr input = getInput();
844 long pos=input->tell();
845 if (!input->checkPosition(pos+6)) {
846 MWAW_DEBUG_MSG(("ScriptWriterParser::readFont: the entry seems too short\n"));
847 return false;
848 }
849 font.setId(int(input->readULong(2)));
850 font.setSize(float(input->readULong(2)));
851 int flag=int(input->readULong(1));
852 uint32_t flags = 0;
853 if (flag&0x1) flags |= MWAWFont::boldBit;
854 if (flag&0x2) flags |= MWAWFont::italicBit;
855 if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
856 if (flag&0x8) flags |= MWAWFont::embossBit;
857 if (flag&0x10) flags |= MWAWFont::shadowBit;
858 if (flag&0x20) font.setDeltaLetterSpacing(-1.);
859 if (flag&0x40) font.setDeltaLetterSpacing(1.);
860 libmwaw::DebugStream f;
861 if (flag&0x80) f << "#flags=" << std::hex << (flag&0x80) << std::dec << ",";
862 font.setFlags(flags);
863 font.m_extra=f.str();
864 input->seek(1, librevenge::RVNG_SEEK_CUR);
865 return true;
866 }
867
868 bool ScriptWriterParser::readCharStyle(ScriptWriterParserInternal::Page &page, int column)
869 {
870 if (column<0 || column>=2) {
871 MWAW_DEBUG_MSG(("ScriptWriterParser::readCharStyle: called with unexpected column\n"));
872 return false;
873 }
874 MWAWInputStreamPtr input = getInput();
875 long pos = input->tell();
876 long len = long(input->readULong(4));
877 long endPos=pos+4+len;
878 if (len<2 || endPos<pos+6 || !input->checkPosition(endPos)) {
879 MWAW_DEBUG_MSG(("ScriptWriterParser::readCharStyle: the entry seems too short\n"));
880 return false;
881 }
882 std::map<std::pair<int,int>, MWAWFont> &zPosToFontMap=page.m_zonePosToFontMap[column];
883 libmwaw::DebugStream f;
884 f << "Entries(CharStyle):";
885 int N=int(input->readULong(2));
886 if ((len-2)/10+1<N) {
887 MWAW_DEBUG_MSG(("ScriptWriterParser::readCharStyle: the number of entry seems bad\n"));
888 f << "###";
889 ascii().addPos(pos);
890 ascii().addNote(f.str().c_str());
891
892 input->seek(pos, librevenge::RVNG_SEEK_SET);
893 return false;
894 }
895 ascii().addPos(pos);
896 ascii().addNote(f.str().c_str());
897
898 for (int i=0; i<=N; ++i) {
899 pos=input->tell();
900 f.str("");
901 f << "CharStyle-" << i << ":";
902 int zone=int(input->readULong(2));
903 f << "Z" << zone << ",";
904 int cPos=int(input->readULong(2));
905 f << "cPos=" << cPos << ",";
906 MWAWFont font;
907 readFont(font);
908 f << ",font=[" << font.getDebugString(getFontConverter()) << "]";
909 if (zPosToFontMap.find(std::make_pair(zone,cPos))!=zPosToFontMap.end()) {
910 MWAW_DEBUG_MSG(("ScriptWriterParser::readCharStyle: find dupplicated position\n"));
911 f << "###";
912 }
913 else
914 zPosToFontMap[std::make_pair(zone,cPos)]=font;
915 ascii().addPos(pos);
916 ascii().addNote(f.str().c_str());
917 input->seek(pos+10, librevenge::RVNG_SEEK_SET);
918 }
919
920 if (input->tell()!=endPos) {
921 ascii().addPos(input->tell());
922 ascii().addNote("_");
923 input->seek(endPos, librevenge::RVNG_SEEK_SET);
924 }
925 return true;
926 }
927
928 bool ScriptWriterParser::readHFStyle(std::map<int,MWAWFont> &posToFontMap)
929 {
930 MWAWInputStreamPtr input = getInput();
931 long pos = input->tell();
932 long len = long(input->readULong(4));
933 long endPos=pos+4+len;
934 if (len<22 || endPos<pos+22+4 || !input->checkPosition(endPos)) {
935 MWAW_DEBUG_MSG(("ScriptWriterParser::readHFStyle: the entry seems too short\n"));
936 return false;
937 }
938 libmwaw::DebugStream f;
939 f << "Entries(HFStyle):";
940 int N=int(input->readULong(2));
941 if ((len-2)/20<N) {
942 MWAW_DEBUG_MSG(("ScriptWriterParser::readHFStyle: the number of entry seems bad\n"));
943 f << "###";
944 ascii().addPos(pos);
945 ascii().addNote(f.str().c_str());
946
947 input->seek(pos, librevenge::RVNG_SEEK_SET);
948 return false;
949 }
950 ascii().addPos(pos);
951 ascii().addNote(f.str().c_str());
952
953 for (int i=0; i<N; ++i) {
954 pos=input->tell();
955 f.str("");
956 f << "HFStyle-" << i+1 << ":";
957 int val=int(input->readLong(2));
958 if (val)
959 f << "f0=" << val << ",";
960 int cPos=int(input->readLong(2));
961 f << "cPos=" << cPos << ",";
962 f << "height=["; // max, min?
963 for (int j=0; j<2; ++j) f << input->readLong(2) << ",";
964 f << "],";
965 MWAWFont font;
966 font.setId(int(input->readULong(2)));
967 int flag=int(input->readULong(1));
968 uint32_t flags = 0;
969 if (flag&0x1) flags |= MWAWFont::boldBit;
970 if (flag&0x2) flags |= MWAWFont::italicBit;
971 if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
972 if (flag&0x8) flags |= MWAWFont::embossBit;
973 if (flag&0x10) flags |= MWAWFont::shadowBit;
974 if (flag&0x20) font.setDeltaLetterSpacing(-1.);
975 if (flag&0x40) font.setDeltaLetterSpacing(1.);
976 if (flag&0x80) f << "#flags=" << std::hex << (flag&0x80) << std::dec << ",";
977 font.setFlags(flags);
978 input->seek(1, librevenge::RVNG_SEEK_CUR);
979 font.setSize(float(input->readULong(2)));
980 f << "font=[" << font.getDebugString(getFontConverter()) << "]],";
981 for (int j=0; j<3; ++j) {
982 val=int(input->readLong(2));
983 if (val) f << "f" << j << "=" << val << ",";
984 }
985 if (posToFontMap.find(cPos)!=posToFontMap.end()) {
986 MWAW_DEBUG_MSG(("ScriptWriterParser::readHFStyle: the position %d is duplicated\n", cPos));
987 f << "###";
988 }
989 else
990 posToFontMap[cPos]=font;
991
992 ascii().addPos(pos);
993 ascii().addNote(f.str().c_str());
994 input->seek(pos+20, librevenge::RVNG_SEEK_SET);
995 }
996
997 if (input->tell()!=endPos) {
998 ascii().addPos(input->tell());
999 ascii().addNote("_");
1000 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1001 }
1002 return true;
1003 }
1004
1005 bool ScriptWriterParser::readPage(ScriptWriterParserInternal::Page &page)
1006 {
1007 MWAWInputStreamPtr input = getInput();
1008 long pos = input->tell();
1009 long len = long(input->readULong(4));
1010 long endPos=pos+4+len;
1011 libmwaw::DebugStream f;
1012 f << "Entries(Page):";
1013 if (len<30 || endPos<pos+34 || !input->checkPosition(endPos)) {
1014 f << "###";
1015 ascii().addPos(pos);
1016 ascii().addNote(f.str().c_str());
1017 input->seek(pos, librevenge::RVNG_SEEK_SET);
1018 MWAW_DEBUG_MSG(("ScriptWriterParser::readPage: the entry seems too short\n"));
1019 return false;
1020 }
1021 page.m_lastPage=true;
1022 int val=int(input->readLong(2));
1023 if (val!=1)
1024 f << "page=" << val << ",";
1025 for (int i=0; i<2; ++i) {
1026 val=int(input->readLong(1));
1027 if (val!=0x20)
1028 f << "f" << i << "=" << val << ",";
1029 }
1030 for (int i=0; i<4; ++i) {
1031 val=int(input->readLong(2));
1032 if (!val) continue;
1033 if (i==0) {
1034 if (val&0x100)
1035 f << "striked,";
1036 val&=0xfeff;
1037 if (val) f << "fl=" << std::hex << val << std::dec << ",";
1038 }
1039 else
1040 f << "f" << i+2 << "=" << val << ",";
1041 }
1042 f << "ID=" << std::hex << input->readLong(4) << std::dec << ",";
1043 val=int(input->readLong(4));
1044 if (val) {
1045 f << "next[ID]=" << std::hex << val << std::dec << ",";
1046 page.m_lastPage=false;
1047 }
1048 f << "IDS=[";
1049 for (int i=0; i<2; ++i)
1050 f << std::hex << input->readLong(4) << std::dec << ",";
1051 f << "],";
1052 int N=int(input->readULong(2));
1053 if ((len-30)/40<N) {
1054 MWAW_DEBUG_MSG(("ScriptWriterParser::readPage: the number of entry seems bad\n"));
1055 f << "###";
1056 ascii().addPos(pos);
1057 ascii().addNote(f.str().c_str());
1058
1059 input->seek(pos, librevenge::RVNG_SEEK_SET);
1060 return false;
1061 }
1062 ascii().addPos(pos);
1063 ascii().addNote(f.str().c_str());
1064
1065 for (int i=0; i<N; ++i) {
1066 pos=input->tell();
1067 f.str("");
1068 f << "Page-Z" << i+1 << ":";
1069
1070 std::array<ScriptWriterParserInternal::Paragraph,2> paras;
1071 for (int j=0; j<3; ++j) { // f2 related to spacing line 1,2,3
1072 val=int(input->readLong(2));
1073 int const expected[]= {0,6,1};
1074 if (val==expected[j])
1075 continue;
1076 if (j==2)
1077 f << "height=" << val << "li,";
1078 else
1079 f << "f" << j << "=" << val << ",";
1080 }
1081 for (size_t col=0; col<2; ++col)
1082 paras[col].m_align=int(input->readLong(2));
1083 for (size_t col=0; col<2; ++col)
1084 paras[col].m_height=int(input->readLong(2));
1085 for (size_t col=0; col<2; ++col)
1086 paras[col].m_numChar=int(input->readLong(2));
1087 if (paras[0].empty() && paras[1].empty()) {
1088 ascii().addPos(pos);
1089 ascii().addNote("_");
1090 input->seek(pos+40, librevenge::RVNG_SEEK_SET);
1091 continue;
1092 }
1093 for (size_t col=0; col<2; ++col)
1094 paras[col].m_position[0]=int(input->readLong(4));
1095 for (size_t col=0; col<2; ++col)
1096 paras[col].m_position[1]=int(input->readLong(4));
1097 std::string wh;
1098 for (int j=0; j<4; ++j) wh+=char(input->readLong(1));
1099 if (!wh.empty() && wh!=" ")
1100 f << "marker=" << wh << ",";
1101 for (size_t col=0; col<2; ++col) {
1102 if (paras[col].empty()) continue;
1103 f << "para" << col << "=[" << paras[col] << "],";
1104 }
1105 val=int(input->readULong(2));
1106 if (val&0x100) f << "striked,";
1107 val &= 0xfeff;
1108 if (val&0xff00) f << "fl=" << std::hex << val << std::dec << ",";
1109 page.m_zoneToParagraphsMap[i]=paras;
1110 ascii().addPos(pos);
1111 ascii().addNote(f.str().c_str());
1112 input->seek(pos+40, librevenge::RVNG_SEEK_SET);
1113 }
1114
1115 if (input->tell()!=endPos) {
1116 ascii().addPos(input->tell());
1117 ascii().addNote("_");
1118 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1119 }
1120
1121 pos=input->tell();
1122 for (int col=0; col<2; ++col) {
1123 if (!readCharStyle(page,col)) {
1124 input->seek(pos, librevenge::RVNG_SEEK_SET);
1125 return false;
1126 }
1127 }
1128
1129 return true;
1130 }
1131
1132 bool ScriptWriterParser::readPicture(MWAWEntry &entry)
1133 {
1134 MWAWInputStreamPtr input = getInput();
1135 long pos = input->tell();
1136 long len = long(input->readULong(4));
1137 long endPos=pos+4+len;
1138 if (len<18 || endPos<pos+22 || !input->checkPosition(endPos)) {
1139 MWAW_DEBUG_MSG(("ScriptWriterParser::readPicture: the entry seems too short\n"));
1140 return false;
1141 }
1142
1143 entry.setBegin(pos+4);
1144 entry.setLength(len);
1145 ascii().addPos(pos);
1146 ascii().addNote("Entries(Picture):");
1147 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1148 return true;
1149 }
1150
1151 bool ScriptWriterParser::readTextZone()
1152 {
1153 MWAWInputStreamPtr input = getInput();
1154 long pos = input->tell();
1155 long len = long(input->readULong(4));
1156 long endPos=pos+4+len;
1157 libmwaw::DebugStream f;
1158 f << "Entries(TextZone):";
1159 if (len<8 || endPos<pos+20 || !input->checkPosition(endPos)) {
1160 f << "###";
1161 MWAW_DEBUG_MSG(("ScriptWriterParser::readTextZone: the entry seems too short\n"));
1162 ascii().addPos(pos);
1163 ascii().addNote(f.str().c_str());
1164 return false;
1165 }
1166 for (int step=0; step<2; ++step) {
1167 // the first length corresponds to a free list, the last length to the last used position
1168 long len2=long(input->readULong(4));
1169 if (len2+8>len) {
1170 f << "###";
1171 MWAW_DEBUG_MSG(("ScriptWriterParser::readPage: a limit seems bad\n"));
1172 ascii().addPos(pos);
1173 ascii().addNote(f.str().c_str());
1174 return false;
1175 }
1176 if (len2+8!=len) {
1177 ascii().addPos(pos+4+8+len2);
1178 ascii().addNote("_");
1179 }
1180 }
1181
1182 m_state->m_mainZoneEntry.setBegin(pos+4);
1183 m_state->m_mainZoneEntry.setLength(len);
1184
1185 ascii().addPos(pos);
1186 ascii().addNote(f.str().c_str());
1187
1188 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1189 return true;
1190 }
1191
1192 ////////////////////////////////////////////////////////////
1193 // read the print info
1194 ////////////////////////////////////////////////////////////
1195 bool ScriptWriterParser::readPrintInfo()
1196 {
1197 MWAWInputStreamPtr input = getInput();
1198 long pos = input->tell();
1199 long len = long(input->readULong(4));
1200 long endPos=pos+4+len;
1201 if (len<0x78 || endPos<pos+0x7c || !input->checkPosition(endPos)) {
1202 MWAW_DEBUG_MSG(("ScriptWriterParser::readPrintInfo: the entry seems too short\n"));
1203 return false;
1204 }
1205 libmwaw::DebugStream f;
1206 // print info
1207 libmwaw::PrinterInfo info;
1208 if (!info.read(input)) return false;
1209 f << "Entries(PrintInfo):"<< info;
1210
1211 MWAWVec2i paperSize = info.paper().size();
1212 MWAWVec2i pageSize = info.page().size();
1213 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
1214 paperSize.x() <= 0 || paperSize.y() <= 0) {
1215 f << "###";
1216 ascii().addPos(pos);
1217 ascii().addNote(f.str().c_str());
1218 return true;
1219 }
1220
1221 // define margin from print info
1222 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
1223 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
1224
1225 // move margin left | top
1226 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
1227 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
1228 lTopMargin -= MWAWVec2i(decalX, decalY);
1229 rBotMargin += MWAWVec2i(decalX, decalY);
1230
1231 // decrease right | bottom
1232 int rightMarg = rBotMargin.x() -50;
1233 if (rightMarg < 0) rightMarg=0;
1234 int botMarg = rBotMargin.y() -50;
1235 if (botMarg < 0) botMarg=0;
1236
1237 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
1238 getPageSpan().setMarginBottom(botMarg/72.0);
1239 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
1240 getPageSpan().setMarginRight(rightMarg/72.0);
1241 getPageSpan().setFormLength(paperSize.y()/72.);
1242 getPageSpan().setFormWidth(paperSize.x()/72.);
1243
1244 if (input->tell()!=endPos)
1245 ascii().addDelimiter(input->tell(),'|');
1246 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1247 ascii().addPos(pos);
1248 ascii().addNote(f.str().c_str());
1249 return true;
1250 }
1251
1252 ////////////////////////////////////////////////////////////
1253 //
1254 // Low level
1255 //
1256 ////////////////////////////////////////////////////////////
1257
1258 ////////////////////////////////////////////////////////////
1259 // read the header
1260 ////////////////////////////////////////////////////////////
1261 bool ScriptWriterParser::checkHeader(MWAWHeader *header, bool strict)
1262 {
1263 *m_state = ScriptWriterParserInternal::State();
1264
1265 MWAWInputStreamPtr input = getInput();
1266 if (!input || !input->hasDataFork())
1267 return false;
1268
1269 if (!input->checkPosition(0x1ea+200)) {
1270 MWAW_DEBUG_MSG(("ScriptWriterParser::checkHeader: file is too short\n"));
1271 return false;
1272 }
1273 input->seek(0,librevenge::RVNG_SEEK_SET);
1274 libmwaw::DebugStream f;
1275 f << "FileHeader:";
1276 int val=int(input->readLong(2));
1277 if (val<130 || val>133)
1278 return false;
1279 f << "vers=" << val << ",";
1280 m_state->m_documentType=int(input->readULong(2));
1281 if (m_state->m_documentType>=3)
1282 return false;
1283 char const *wh[]= {"advanced", "normal", "dual"};
1284 f << wh[m_state->m_documentType] << ",";
1285
1286 if (strict) {
1287 input->seek(0x1ea, librevenge::RVNG_SEEK_SET);
1288 bool lastIsShort=false;
1289 while (!input->isEnd()) {
1290 long pos=input->tell();
1291 long len = long(input->readULong(4));
1292 long endPos=pos+4+len;
1293
1294 if (len<0 || endPos<pos+4 || !input->checkPosition(endPos))
1295 return false;
1296 if (len<22 && lastIsShort)
1297 return false;
1298 lastIsShort=len<22;
1299 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1300 }
1301 }
1302
1303 ascii().addPos(0);
1304 ascii().addNote(f.str().c_str());
1305
1306 if (header)
1307 header->reset(MWAWDocument::MWAW_T_SCRIPTWRITER, 1);
1308
1309 return true;
1310 }
1311
1312 ////////////////////////////////////////////////////////////
1313 // send data
1314 ////////////////////////////////////////////////////////////
1315 bool ScriptWriterParser::sendMainZone()
1316 {
1317 auto listener=getTextListener();
1318 if (!listener) {
1319 MWAW_DEBUG_MSG(("ScriptWriterParser::sendMainZone: can not find the main listener\n"));
1320 return false;
1321 }
1322
1323 if (m_state->m_documentType==2) {
1324 MWAWSection section;
1325 if (m_state->m_columnSepPos>0 && m_state->m_columnSepPos<72*getPageSpan().getPageWidth()) {
1326 section.m_columns.resize(2);
1327 section.m_columns[0].m_width=double(m_state->m_columnSepPos)/72;
1328 section.m_columns[1].m_width=getPageSpan().getPageWidth()-double(m_state->m_columnSepPos)/72;
1329 }
1330 else
1331 section.setColumns(2, getPageSpan().getPageWidth()/double(2), librevenge::RVNG_INCH);
1332 listener->openSection(section);
1333 }
1334 bool firstPage=true;
1335 for (auto const &page : m_state->m_pages) {
1336 if (!firstPage)
1337 listener->insertBreak(MWAWListener::PageBreak);
1338 sendText(page);
1339 firstPage=false;
1340 }
1341 if (m_state->m_documentType==2)
1342 listener->closeSection();
1343 return true;
1344 }
1345
1346 bool ScriptWriterParser::send(ScriptWriterParserInternal::HFData const &hf)
1347 {
1348 auto input=getInput();
1349 if (!input)
1350 return false;
1351 auto listener=getTextListener();
1352 if (!listener) {
1353 MWAW_DEBUG_MSG(("ScriptWriterParser::sendText[hfData]: can not find the main listener\n"));
1354 return false;
1355 }
1356
1357 /* as it seems difficult to add many frame in an header/footer, sort
1358 all the shape by y pos, then send them one row by row (using a
1359 table, if many frames share the same beginning position) */
1360 std::map<int,std::vector<int> > posToType;
1361 if (hf.hasDate())
1362 posToType[hf.m_dateOrigin[1]]= {-3};
1363 if (hf.hasPageNumber()) {
1364 if (posToType.find(hf.m_pageNumberOrigin[1])==posToType.end())
1365 posToType[hf.m_dateOrigin[1]]= {-2};
1366 else
1367 posToType[hf.m_dateOrigin[1]].push_back(-2);
1368 }
1369 if (hf.m_hasPicture) {
1370 if (posToType.find(hf.m_picturePosition[0][1])==posToType.end())
1371 posToType[hf.m_picturePosition[0][1]]= {-1};
1372 else
1373 posToType[hf.m_picturePosition[0][1]].push_back(-1);
1374 }
1375 for (size_t f=0; f<hf.m_frames.size(); ++f) {
1376 auto const &frame = hf.m_frames[f];
1377 if (posToType.find(frame.m_position[0][1])==posToType.end())
1378 posToType[frame.m_position[0][1]]= {int(f)};
1379 else
1380 posToType[frame.m_position[0][1]].push_back(int(f));
1381
1382 posToType.insert(std::make_pair(frame.m_position[0][1],int(f)));
1383 }
1384 for (auto const &it : posToType) {
1385 auto const &list=it.second;
1386 for (size_t i=0; i<list.size();) {
1387 int actVal=list[i];
1388 size_t nextI=i+1;
1389 while (nextI<list.size() && ((actVal<=-2 && list[nextI]<=-2) || (actVal>=0 && list[nextI]>=0)))
1390 ++nextI;
1391 bool useTable=false;
1392 if (nextI!=i+1) {
1393 useTable=true;
1394 MWAWTable table(MWAWTable::TableDimBit);
1395 std::vector<float> dim(nextI-i, 72*float(getPageSpan().getPageWidth())/float(nextI-i));
1396 table.setColsSize(dim);
1397 listener->openTable(table);
1398 listener->openTableRow(-float(m_state->m_lineSpacing), librevenge::RVNG_POINT);
1399 }
1400
1401 MWAWParagraph para;
1402 if (m_state->m_lineSpacing>4 && m_state->m_lineSpacing<40)
1403 para.setInterline(m_state->m_lineSpacing, librevenge::RVNG_POINT, MWAWParagraph::AtLeast);
1404 para.m_justify=MWAWParagraph::JustificationCenter;
1405
1406 int col=0;
1407 while (i<nextI) {
1408 if (useTable) {
1409 MWAWCell cell;
1410 cell.setPosition(MWAWVec2i(0,col++));
1411 listener->openTableCell(cell);
1412 }
1413 actVal=list[i++];
1414 listener->setParagraph(para);
1415 switch (actVal) {
1416 case -3: {
1417 MWAWField date(MWAWField::Date);
1418 date.m_DTFormat = "%a, %b %d, %Y";
1419 listener->insertField(date);
1420 break;
1421 }
1422 case -2:
1423 listener->insertField(MWAWField(MWAWField::PageNumber));
1424 break;
1425 case -1: {
1426 if (!hf.m_picture.valid() || hf.m_picture.length()<22) {
1427 MWAW_DEBUG_MSG(("ScriptWriterParser::send[hf]: the picture entry seems bd\n"));
1428 break;
1429 }
1430 input->seek(hf.m_picture.begin(), librevenge::RVNG_SEEK_SET);
1431 std::shared_ptr<MWAWPict> pict(MWAWPictData::get(input, int(hf.m_picture.length())));
1432 MWAWEmbeddedObject object;
1433 if (pict && pict->getBinary(object) && !object.m_dataList.empty()) {
1434 MWAWPosition pictPos(MWAWVec2f(0,0),MWAWVec2f(hf.m_picturePosition.size()), librevenge::RVNG_POINT);
1435 pictPos.setRelativePosition(MWAWPosition::Char);
1436 listener->insertPicture(pictPos, object);
1437 #ifdef DEBUG_WITH_FILES
1438 static int volatile pictName = 0;
1439 libmwaw::DebugStream f2;
1440 f2 << "PICT-" << ++pictName << ".pct";
1441 libmwaw::Debug::dumpFile(object.m_dataList[0], f2.str().c_str());
1442 ascii().skipZone(hf.m_picture.begin(), hf.m_picture.end()-1);
1443 #endif
1444 }
1445 else {
1446 MWAW_DEBUG_MSG(("ScriptWriterParser::send[hf]: can not find the picture\n"));
1447 }
1448 break;
1449 }
1450 default:
1451 sendText(hf.m_frames[size_t(actVal)]);
1452 break;
1453 }
1454 if (useTable)
1455 listener->closeTableCell();
1456 }
1457 listener->insertEOL();
1458 if (useTable) {
1459 listener->closeTableRow();
1460 listener->closeTable();
1461 }
1462 }
1463 }
1464 return true;
1465 }
1466
1467 bool ScriptWriterParser::sendText(ScriptWriterParserInternal::Frame const &frame)
1468 {
1469 auto input=getInput();
1470 if (!input)
1471 return false;
1472 auto listener=getTextListener();
1473 if (!listener) {
1474 MWAW_DEBUG_MSG(("ScriptWriterParser::sendText[frame]: can not find the main listener\n"));
1475 return false;
1476 }
1477
1478 std::map<int,MWAWFont> posToFontMap;
1479 if (frame.m_entries[1].valid()) {
1480 input->seek(frame.m_entries[1].begin()-4, librevenge::RVNG_SEEK_SET);
1481 readHFStyle(posToFontMap);
1482 }
1483
1484 if (!frame.m_entries[0].valid() || frame.m_entries[0].length()<frame.m_numChar) {
1485 MWAW_DEBUG_MSG(("ScriptWriterParser::sendText[frame]: can not find the text zone\n"));
1486 return false;
1487 }
1488 listener->setFont(m_state->m_defaultFont);
1489 input->seek(frame.m_entries[0].begin(), librevenge::RVNG_SEEK_SET);
1490 for (int c=0; c<frame.m_numChar; ++c) {
1491 auto fIt=posToFontMap.find(c);
1492 if (fIt!=posToFontMap.end())
1493 listener->setFont(fIt->second);
1494 unsigned char ch=(unsigned char)(input->readLong(1));
1495 switch (ch) {
1496 case 0:
1497 listener->insertEOL(true);
1498 break;
1499 case 0x9:
1500 listener->insertTab();
1501 break;
1502 case 0xd:
1503 listener->insertEOL();
1504 break;
1505 default:
1506 if (ch<0x1f)
1507 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText[frame]: find odd char c=%d\n", int(ch)));
1508 else
1509 listener->insertCharacter(ch);
1510 break;
1511 }
1512 }
1513 return true;
1514 }
1515
1516 bool ScriptWriterParser::sendText(ScriptWriterParserInternal::Page const &page)
1517 {
1518 auto input=getInput();
1519 if (!input)
1520 return false;
1521 auto listener=getTextListener();
1522 if (!listener) {
1523 MWAW_DEBUG_MSG(("ScriptWriterParser::sendText[page]: can not find the main listener\n"));
1524 return false;
1525 }
1526 auto const &textEntry=m_state->m_mainZoneEntry;
1527 if (!textEntry.valid()) {
1528 MWAW_DEBUG_MSG(("ScriptWriterParser::sendText[page]: can not find the main text entry\n"));
1529 return false;
1530 }
1531 bool const dual=m_state->m_documentType==2;
1532 libmwaw::DebugStream f;
1533 for (size_t col=0; col<2; ++col) {
1534 MWAWParagraph paragraph;
1535 if (m_state->m_lineSpacing>4 && m_state->m_lineSpacing<40)
1536 paragraph.setInterline(m_state->m_lineSpacing, librevenge::RVNG_POINT, MWAWParagraph::AtLeast);
1537 if (m_state->m_lineSpacingPercent[col]>1 && m_state->m_lineSpacingPercent[col]<5)
1538 paragraph.setInterline(m_state->m_lineSpacingPercent[col], librevenge::RVNG_PERCENT);
1539 paragraph.m_tabs=m_state->m_tabs[col];
1540 paragraph.m_marginsUnit=librevenge::RVNG_POINT;
1541 if (col==0)
1542 paragraph.m_margins[1]=m_state->m_leftMargins[0];
1543 else
1544 paragraph.m_margins[1]=m_state->m_columnOriginPos-m_state->m_columnSepPos;
1545 for (auto const &pIdPara : page.m_zoneToParagraphsMap) {
1546 auto const &para=pIdPara.second[col];
1547 switch (para.m_align) {
1548 case 0:// left
1549 default:
1550 paragraph.m_justify=MWAWParagraph::JustificationLeft;
1551 break;
1552 case 1:
1553 paragraph.m_justify=MWAWParagraph::JustificationCenter;
1554 break;
1555 case -1:
1556 paragraph.m_justify=MWAWParagraph::JustificationRight;
1557 break;
1558 case -5:
1559 paragraph.m_justify=MWAWParagraph::JustificationFull;
1560 break;
1561 }
1562 listener->setParagraph(paragraph);
1563 int totalHeight=std::max<int>(pIdPara.second[0].m_height, pIdPara.second[1].m_height);
1564 if (para.empty()) {
1565 if (dual) {
1566 for (int l=para.m_height; l<totalHeight; ++l)
1567 listener->insertEOL();
1568 }
1569 continue;
1570 }
1571 if (para.m_numChar<0 || para.m_position[0]<0 || para.m_position[1]<para.m_position[0]+4+para.m_numChar || para.m_position[1]+8>textEntry.length()) {
1572 MWAW_DEBUG_MSG(("ScriptWriterParser::sendText[page]: can not find a paragraph data\n"));
1573 continue;
1574 }
1575 input->seek(textEntry.begin()+8+para.m_position[0], librevenge::RVNG_SEEK_SET);
1576
1577 f.str("");
1578 f << "TextZone:";
1579 int pg=int(input->readULong(2));
1580 f << "pg=" << pg << ",";
1581 int id=int(input->readLong(2));
1582 if (id>0)
1583 f << "id=" << id << ",";
1584 else { // col2
1585 id *=-1;
1586 f << "id2=" << id << ",";
1587 }
1588 std::string text;
1589 bool lastIsEOL=false;
1590 listener->setFont(m_state->m_defaultFont);
1591 for (int c=0; c<para.m_numChar; ++c) {
1592 auto fIt=page.m_zonePosToFontMap[col].find(std::make_pair(id,c));
1593 if (fIt!=page.m_zonePosToFontMap[col].end())
1594 listener->setFont(fIt->second);
1595 unsigned char ch=(unsigned char)(input->readLong(1));
1596 if (ch)
1597 text+=char(ch);
1598 else
1599 text+="[#0]";
1600 lastIsEOL=false;
1601 switch (ch) {
1602 case 0:
1603 lastIsEOL=true;
1604 listener->insertEOL(true);
1605 break;
1606 case 0x9:
1607 listener->insertTab();
1608 break;
1609 case 0xd:
1610 lastIsEOL=true;
1611 listener->insertEOL();
1612 break;
1613 default:
1614 if (ch<0x1f)
1615 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: find odd char c=%d\n", int(ch)));
1616 else
1617 listener->insertCharacter(ch);
1618 break;
1619 }
1620 }
1621 if (!lastIsEOL)
1622 listener->insertEOL();
1623 f << text << ",";
1624 ascii().addPos(textEntry.begin()+8+para.m_position[0]);
1625 ascii().addNote(f.str().c_str());
1626 ascii().addPos(textEntry.begin()+8+para.m_position[1]);
1627 ascii().addNote("_");
1628 if (dual) {
1629 for (int l=para.m_height; l<totalHeight; ++l)
1630 listener->insertEOL();
1631 }
1632 }
1633 if (!dual)
1634 break;
1635 if (col==0)
1636 listener->insertBreak(MWAWListener::ColumnBreak);
1637 }
1638
1639 return true;
1640 }
1641
1642 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: