Debian Codegraph @

src/lib/MsWks4Zone.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 <vector>
37
38 #include <librevenge/librevenge.h>
39
40 #include "MWAWTextListener.hxx"
41 #include "MWAWFont.hxx"
42 #include "MWAWFontConverter.hxx"
43 #include "MWAWPosition.hxx"
44 #include "MWAWPrinter.hxx"
45 #include "MWAWSection.hxx"
46 #include "MWAWSubDocument.hxx"
47
48 #include "MsWksGraph.hxx"
49 #include "MsWksDocument.hxx"
50
51 #include "MsWks4Text.hxx"
52
53 #include "MsWks4Zone.hxx"
54
55
56 /** Internal: the structures of a MsWks4Zone */
57 namespace MsWks4ZoneInternal
58 {
59 /**Internal: some data are dupplicated, an enum to know which picture to ignored.
60 *
61 * For intance, the header text is stored in a separate ole, and we find
62 * in the main ole, a picture which represents the headers */
63 enum { IgnoreFrame = -4 };
64
65 //! Internal: a frame ( position, type, ...)
66 struct Frame {
67 //! the type of the frame which can represent header, footer, textbox, ...
68 enum Type { Unknown = 0, Header, Footer, Table, Object, Textbox };
69
70 //! constructor
71 Frame()
72 : m_type(Unknown)
73 , m_position()
74 , m_pictId()
75 , m_error("")
76 {
77 m_position.setPage(-3);
78 }
79 //! operator<<
80 friend std::ostream &operator<<(std::ostream &o, Frame const &ft);
81
82 //! the frame type
83 Type m_type;
84
85 //! the position of the frame in the document
86 MWAWPosition m_position;
87 //! some frames are associated with a picture stored in this entry ( name + id)
88 MWAWEntry m_pictId;
89 //! a string to store unparsed data
90 std::string m_error;
91 };
92 //! friend operator<< for frame
93 std::ostream &operator<<(std::ostream &o, Frame const &ft)
94 {
95 switch (ft.m_type) {
96 case Frame::Header:
97 o<< "header,";
98 break;
99 case Frame::Footer:
100 o<< "footer,";
101 break;
102 case Frame::Table:
103 o<< "table,";
104 break;
105 case Frame::Textbox:
106 o<< "textbox,";
107 break;
108 case Frame::Object:
109 o<< "object,";
110 break;
111 case Frame::Unknown:
112 #if !defined(__clang__)
113 default:
114 #endif
115 break;
116 }
117
118 int page = ft.m_position.page();
119 switch (page) {
120 case -1:
121 o << "allpages,";
122 break;
123 case -2:
124 o << "undef,";
125 break;
126 case -3:
127 o << "def,";
128 break;
129 default:
130 if (page <= 0) o << "###page=" << page << ",";
131 break;
132 }
133 if (ft.m_pictId.name().length())
134 o << "pict='" << ft.m_pictId.name() << "':" << ft.m_pictId.id() << ",";
135
136 o << ft.m_position;
137 if (!ft.m_error.empty()) o << "errors=(" << ft.m_error << ")";
138 return o;
139 }
140
141 //!Internal: the state of a MsWks4Zone
142 struct State {
143 //! constructor
144 State()
145 : m_mainOle(false)
146 , m_numColumns(1)
147 , m_hasColumnSep(false)
148 , m_parsed(false)
149 , m_actPage(0)
150 , m_numPages(0)
151 , m_defFont(20,12)
152 , m_framesList()
153 {
154 }
155
156 //! true if we parse the main MN0
157 bool m_mainOle;
158
159 //! the number of column
160 int m_numColumns;
161 //! true if a line is added to separated the column
162 bool m_hasColumnSep;
163 //! a flag to known if the ole is already parsed
164 bool m_parsed;
165
166 int m_actPage /** the actual page*/, m_numPages /* the number of pages */;
167
168 //! the default font
169 MWAWFont m_defFont;
170
171 //! the list of frames
172 std::vector<Frame> m_framesList;
173
174 };
175 }
176
177 ////////////////////////////////////////////////////////////
178 // constructor/destructor, ...
179 ////////////////////////////////////////////////////////////
180 MsWks4Zone::MsWks4Zone(MWAWInputStreamPtr const &input, MWAWParserStatePtr const &parserState,
181 MWAWParser &parser, std::string const &oleName)
182 : m_mainParser(&parser)
183 , m_parserState(parserState)
184 , m_state()
185 , m_document()
186 {
187 m_document.reset(new MsWksDocument(input, parser));
188 setAscii(oleName);
189 m_parserState->m_version=4;
190 init();
191 }
192
193 MsWks4Zone::~MsWks4Zone()
194 {
195 }
196
197 ////////////////////////////////////////////////////////////
198 // small helper to manage interaction between parent and child, ...
199 ////////////////////////////////////////////////////////////
200 void MsWks4Zone::init()
201 {
202 m_state.reset(new MsWks4ZoneInternal::State);
203 m_document->getTextParser4()->setDefault(m_state->m_defFont);
204 }
205
206 MWAWInputStreamPtr MsWks4Zone::getInput()
207 {
208 return m_document->m_input;
209 }
210
211 void MsWks4Zone::setAscii(std::string const &oleName)
212 {
213 std::string fName = libmwaw::Debug::flattenFileName(oleName);
214 m_document->initAsciiFile(fName);
215 }
216
217 libmwaw::DebugFile &MsWks4Zone::ascii()
218 {
219 return m_document->ascii();
220 }
221
222 void MsWks4Zone::readFootNote(int id)
223 {
224 m_document->getTextParser4()->readFootNote(m_document->getInput(), id);
225 }
226
227 ////////////////////////////////////////////////////////////
228 // position and height
229 ////////////////////////////////////////////////////////////
230 double MsWks4Zone::getTextHeight() const
231 {
232 return m_parserState->m_pageSpan.getPageLength();
233 }
234
235 ////////////////////////////////////////////////////////////
236 // text positions
237 ////////////////////////////////////////////////////////////
238 void MsWks4Zone::newPage(int number, bool /*soft*/)
239 {
240 if (number <= m_state->m_actPage || number > m_state->m_numPages)
241 return;
242
243 long pos = m_document->getInput()->tell();
244 while (m_state->m_actPage < number) {
245 m_state->m_actPage++;
246 if (!m_parserState->getMainListener() || m_state->m_actPage == 1)
247 continue;
248 // FIXME: find a way to force the page break to happen
249 // ie. graphParser must add a space to force it :-~
250 if (m_state->m_mainOle) m_parserState->getMainListener()->insertBreak(MWAWTextListener::PageBreak);
251
252 MsWksGraph::SendData sendData;
253 sendData.m_type = MsWksGraph::SendData::RBDR;
254 sendData.m_anchor = MWAWPosition::Page;
255 sendData.m_page = m_state->m_actPage;
256 m_document->getGraphParser()->sendObjects(sendData);
257 }
258 m_document->getInput()->seek(pos, librevenge::RVNG_SEEK_SET);
259 }
260
261 MWAWEntry MsWks4Zone::getTextPosition() const
262 {
263 return m_document->getTextParser4()->m_textPositions;
264 }
265
266 ////////////////////////////////////////////////////////////
267 // create the main listener ( given a header and a footer document)
268 ////////////////////////////////////////////////////////////
269 MWAWTextListenerPtr MsWks4Zone::createListener(librevenge::RVNGTextInterface *interface)
270 {
271 std::vector<MWAWPageSpan> pageList;
272 m_state->m_actPage = 0;
273 m_document->getPageSpanList(pageList, m_state->m_numPages);
274 MWAWTextListenerPtr res(new MWAWTextListener(*m_parserState, pageList, interface));
275
276 // ok, now we can update the page position
277 std::vector<int> linesH, pagesH;
278 pagesH.resize(size_t(m_state->m_numPages), int(72.*getTextHeight()));
279 m_document->getGraphParser()->computePositions(-1, linesH, pagesH);
280 m_document->getGraphParser()->setPageLeftTop
281 (MWAWVec2f(72.f*float(m_parserState->m_pageSpan.getMarginLeft()),
282 72.f*float(m_parserState->m_pageSpan.getMarginTop())));
283
284 return res;
285 }
286
287 ////////////////////////////////////////////////////////////
288 //
289 // entry management
290 //
291 ////////////////////////////////////////////////////////////
292 bool MsWks4Zone::parseHeaderIndexEntry(MWAWInputStreamPtr &input)
293 {
294 long pos = input->tell();
295 m_document->ascii().addPos(pos);
296
297 libmwaw::DebugStream f;
298
299 auto cch = uint16_t(input->readULong(2));
300
301 // check if the entry can be read
302 input->seek(pos + cch, librevenge::RVNG_SEEK_SET);
303 if (input->tell() != pos+cch) {
304 MWAW_DEBUG_MSG(("MsWks4Zone:parseHeaderIndexEntry error: incomplete entry\n"));
305 m_document->ascii().addNote("###IndexEntry incomplete (ignored)");
306 return false;
307 }
308 input->seek(pos + 2, librevenge::RVNG_SEEK_SET);
309
310 if (0x18 != cch) {
311 if (cch < 0x18) {
312 input->seek(pos + cch, librevenge::RVNG_SEEK_SET);
313 m_document->ascii().addNote("MsWks4Zone:parseHeaderIndexEntry: ###IndexEntry too short(ignored)");
314 if (cch < 10) throw libmwaw::ParseException();
315 return true;
316 }
317 }
318
319 std::string name;
320
321 // sanity check
322 for (size_t i =0; i < 4; i++) {
323 name.append(1, char(input->readULong(1)));
324
325 if (name[i] != 0 && name[i] != 0x20 &&
326 (41 > static_cast<uint8_t>(name[i]) || static_cast<uint8_t>(name[i]) > 90)) {
327 MWAW_DEBUG_MSG(("MsWks4Zone:parseHeaderIndexEntry: bad character=%u (0x%02x) in name in header index\n",
328 static_cast<unsigned int>(name[i]), static_cast<unsigned int>(name[i])));
329 m_document->ascii().addNote("###IndexEntry bad name(ignored)");
330
331 input->seek(pos + cch, librevenge::RVNG_SEEK_SET);
332 return true;
333 }
334 }
335
336 f << "Entries("<<name<<")";
337 if (cch != 24) f << ", ###size=" << int(cch);
338 auto id = static_cast<int>(input->readULong(2));
339 f << ", id=" << id << ", (";
340 for (int i = 0; i < 2; i ++) {
341 auto val = static_cast<int>(input->readLong(2));
342 f << val << ",";
343 }
344
345 std::string name2;
346 for (size_t i =0; i < 4; i++)
347 name2.append(1, char(input->readULong(1)));
348 f << "), " << name2;
349
350 MWAWEntry hie;
351 hie.setName(name);
352 hie.setType(name2);
353 hie.setId(id);
354 hie.setBegin(long(input->readULong(4)));
355 hie.setLength(long(input->readULong(4)));
356
357 f << ", offset=" << std::hex << hie.begin() << ", length=" << hie.length();
358
359 if (cch != 0x18) {
360 m_document->ascii().addDelimiter(pos+0x18, '|');
361 f << ",#extraData";
362 }
363
364 input->seek(hie.end(), librevenge::RVNG_SEEK_SET);
365 if (input->tell() != hie.end()) {
366 f << ", ###ignored";
367 m_document->ascii().addNote(f.str().c_str());
368 input->seek(pos + cch, librevenge::RVNG_SEEK_SET);
369 return true;
370 }
371
372
373 m_document->getEntryMap().insert(std::multimap<std::string, MWAWEntry>::value_type(name, hie));
374
375 m_document->ascii().addPos(pos);
376 m_document->ascii().addNote(f.str().c_str());
377
378 m_document->ascii().addPos(hie.begin());
379 f.str("");
380 f << name;
381 if (name != name2) f << "/" << name2;
382 f << ":" << std::dec << id;
383 m_document->ascii().addNote(f.str().c_str());
384
385 m_document->ascii().addPos(hie.end());
386 m_document->ascii().addNote("_");
387
388 input->seek(pos + cch, librevenge::RVNG_SEEK_SET);
389 return true;
390 }
391
392 bool MsWks4Zone::parseHeaderIndex(MWAWInputStreamPtr &input)
393 {
394 m_document->getEntryMap().clear();
395 input->seek(0x08, librevenge::RVNG_SEEK_SET);
396
397 long pos = input->tell();
398 auto i0 = static_cast<int>(input->readLong(2));
399 auto i1 = static_cast<int>(input->readLong(2));
400 auto n_entries = static_cast<uint16_t>(input->readULong(2));
401 // fixme: sanity check n_entries
402
403 libmwaw::DebugStream f;
404 f << "Header: N=" << n_entries << ", " << i0 << ", " << i1 << "(";
405
406 for (int i = 0; i < 4; i++)
407 f << std::hex << input->readLong(2) << ",";
408 f << "), ";
409 f << "end=" << std::hex << input->readLong(2);
410
411 m_document->ascii().addPos(pos);
412 m_document->ascii().addNote(f.str().c_str());
413
414 input->seek(0x18, librevenge::RVNG_SEEK_SET);
415 bool readSome = false;
416 do {
417 if (input->isEnd()) return readSome;
418
419 pos = input->tell();
420 f.str("");
421 auto unknown1 = static_cast<uint16_t>(input->readULong(2)); // function of the size of the entries ?
422
423 auto n_entries_local = static_cast<uint16_t>(input->readULong(2));
424 f << "Header("<<std::hex << unknown1<<"): N=" << std::dec << n_entries_local;
425
426 if (n_entries_local > 0x20) {
427 MWAW_DEBUG_MSG(("MsWks4Zone::parseHeaderIndex: error: n_entries_local=%i\n", n_entries_local));
428 return readSome;
429 }
430
431 auto next_index_table = static_cast<uint32_t>(input->readULong(4));
432 f << std::hex << ", nextHeader=" << next_index_table;
433 if (next_index_table != 0xFFFFFFFF && long(next_index_table) < pos) {
434 MWAW_DEBUG_MSG(("MsWks4Zone::parseHeaderIndex: error: next_index_table=%x decreasing !!!\n", next_index_table));
435 return readSome;
436 }
437
438 m_document->ascii().addPos(pos);
439 m_document->ascii().addNote(f.str().c_str());
440
441 do {
442 if (!parseHeaderIndexEntry(input)) return readSome;
443
444 readSome=true;
445 n_entries--;
446 n_entries_local--;
447 }
448 while (n_entries > 0 && n_entries_local);
449
450 if (0xFFFFFFFF == next_index_table && n_entries > 0) {
451 MWAW_DEBUG_MSG(("MsWks4Zone::parseHeaderIndex: error: expected more header index entries\n"));
452 return true;
453 }
454
455 if (0xFFFFFFFF == next_index_table) break;
456 if (long(next_index_table) < input->tell()) {
457 MWAW_DEBUG_MSG(("MsWks4Zone::parseHeaderIndex: error: next_index_table=%x decreasing !!!\n", next_index_table));
458 return readSome;
459 }
460
461 if (input->seek(long(next_index_table), librevenge::RVNG_SEEK_SET) != 0) return readSome;
462 }
463 while (n_entries > 0);
464
465 return true;
466 }
467
468 ////////////////////////////////////////////////////////////
469 // parse the ole part and create the main part
470 ////////////////////////////////////////////////////////////
471 bool MsWks4Zone::createZones(bool mainOle)
472 {
473 if (m_state->m_parsed) return true;
474
475 auto &entryMap=m_document->getEntryMap();
476 MWAWInputStreamPtr input = m_document->getInput();
477 m_state->m_parsed = true;
478
479 entryMap.clear();
480
481 m_document->ascii().addPos(0);
482 m_document->ascii().addNote("FileHeader");
483 /* header index */
484 if (!parseHeaderIndex(input)) return false;
485 // the text structure
486 if (!m_document->getTextParser4()->readStructures(input, mainOle)) return !mainOle;
487
488 // DOP, PRNT
489 auto pos = entryMap.find("PRNT");
490 if (entryMap.end() != pos) {
491 pos->second.setParsed(true);
492 MWAWPageSpan page;
493 if (readPRNT(input, pos->second, page) && mainOle) m_parserState->m_pageSpan = page;
494 }
495 pos = entryMap.find("DOP ");
496 if (entryMap.end() != pos) {
497 MWAWPageSpan page;
498 if (readDOP(input, pos->second, page) && mainOle) m_parserState->m_pageSpan = page;
499 }
500
501 // RLRB
502 pos = entryMap.lower_bound("RLRB");
503 while (pos != entryMap.end()) {
504 MWAWEntry const &entry = pos++->second;
505 if (!entry.hasName("RLRB")) break;
506 if (!entry.hasType("RLRB")) continue;
507
508 readRLRB(input, entry);
509 }
510
511 // SELN
512 pos = entryMap.lower_bound("SELN");
513 while (entryMap.end() != pos) {
514 MWAWEntry const &entry = pos++->second;
515 if (!entry.hasName("SELN")) break;
516 if (!entry.hasType("SELN")) continue;
517 readSELN(input, entry);
518 }
519
520 // FRAM
521 m_state->m_framesList.resize(0);
522 pos = entryMap.lower_bound("FRAM");
523 while (entryMap.end() != pos) {
524 MWAWEntry const &entry = pos++->second;
525 if (!entry.hasName("FRAM")) break;
526 if (!entry.hasType("FRAM")) continue;
527 readFRAM(input, entry);
528 }
529
530 /* Graph data */
531 pos = entryMap.lower_bound("RBDR");
532 while (pos != entryMap.end()) {
533 MWAWEntry const &entry = pos++->second;
534 if (!entry.hasName("RBDR")) break;
535 if (!entry.hasType("RBDR")) continue;
536
537 if (m_document->getGraphParser()->readRB(input, entry, 0))
538 entry.setParsed(true);
539 }
540 pos = entryMap.lower_bound("RBIL");
541 while (pos != entryMap.end()) {
542 MWAWEntry const &entry = pos++->second;
543 if (!entry.hasName("RBIL")) break;
544 if (!entry.hasType("RBIL")) continue;
545
546 if (m_document->getGraphParser()->readRB(input, entry, 0))
547 entry.setParsed(true);
548 }
549
550 /* read the pictures */
551 // in the main block, pict are used to representant the
552 // header/footer, so we skip it.
553 // In the others block, maybe there can be interesting, so, we read them
554 pos = entryMap.lower_bound("PICT");
555 while (pos != entryMap.end()) {
556 MWAWEntry const &entry = pos++->second;
557 if (!entry.hasName("PICT")) break;
558 m_document->getGraphParser()->readPictureV4(input, entry);
559 }
560 return true;
561 }
562
563 ////////////////////////////////////////////////////////////
564 // send all the data corresponding to a zone
565 ////////////////////////////////////////////////////////////
566 void MsWks4Zone::readContentZones(MWAWEntry const &entry, bool mainOle)
567 {
568 MWAWInputStreamPtr input = m_document->getInput();
569 bool oldMain = m_state->m_mainOle;
570 m_state->m_mainOle = mainOle;
571
572 MsWksGraph::SendData sendData;
573 sendData.m_type = MsWksGraph::SendData::RBDR;
574 sendData.m_anchor = mainOle ? MWAWPosition::Page : MWAWPosition::Paragraph;
575 sendData.m_page = 0;
576 m_document->getGraphParser()->sendObjects(sendData);
577
578 if (mainOle && m_parserState->getMainListener() && m_state->m_numColumns > 1) {
579 if (m_parserState->getMainListener()->isSectionOpened())
580 m_parserState->getMainListener()->closeSection();
581 MWAWSection sec;
582 sec.setColumns(m_state->m_numColumns, m_mainParser->getPageWidth()/double(m_state->m_numColumns), librevenge::RVNG_INCH);
583 if (m_state->m_hasColumnSep)
584 sec.m_columnSeparator=MWAWBorder();
585 m_parserState->getMainListener()->openSection(sec);
586 }
587
588 MWAWEntry ent(entry);
589 if (!ent.valid()) ent = m_document->getTextParser4()->m_textPositions;
590 m_document->getTextParser4()->readText(input, ent, mainOle);
591
592 if (!mainOle) {
593 m_state->m_mainOle = oldMain;
594 return;
595 }
596
597 // send the final data
598 #ifdef DEBUG
599 newPage(m_state->m_numPages);
600 m_document->getTextParser4()->flushExtra(input);
601
602 sendData.m_type = MsWksGraph::SendData::ALL;
603 sendData.m_anchor = MWAWPosition::Char;
604 m_document->getGraphParser()->sendObjects(sendData);
605 #endif
606 m_state->m_mainOle = oldMain;
607
608 #ifdef DEBUG
609 // check if we have parsed all zones
610 auto const &entryMap=m_document->getEntryMap();
611
612 for (auto const &it : entryMap) {
613 MWAWEntry const &zone = it.second;
614 if (zone.isParsed() ||
615 zone.hasName("TEXT") || // TEXT entries are managed directly by MsWks4Text
616 zone.hasName("INK ")) // INK Zone are ignored: always = 2*99
617 continue;
618 MWAW_DEBUG_MSG(("MsWks4Zone::readContentZones: WARNING zone %s ignored\n",
619 zone.name().c_str()));
620 }
621 #endif
622 }
623
624 ////////////////////////////////////////////////////////////
625 //
626 // low level
627 //
628 ////////////////////////////////////////////////////////////
629
630 ////////////////////////////////////////////////////////////
631 // the printer definition
632 ////////////////////////////////////////////////////////////
633 bool MsWks4Zone::readPRNT(MWAWInputStreamPtr input, MWAWEntry const &entry, MWAWPageSpan &page)
634 {
635 page = MWAWPageSpan();
636 if (!entry.hasType("PRR ")) {
637 MWAW_DEBUG_MSG(("Works: unknown PRNT type='%s'\n", entry.type().c_str()));
638 return false;
639 }
640
641 long debPos = entry.begin();
642 input->seek(debPos, librevenge::RVNG_SEEK_SET);
643 libmwaw::PrinterInfo info;
644 if (!info.read(input)) {
645 MWAW_DEBUG_MSG(("Works: error: can not read PRNT\n"));
646 return false;
647 }
648 else {
649 MWAWVec2i paperSize = info.paper().size();
650 MWAWVec2i pageSize = info.page().size();
651 MWAWVec2i margin = paperSize - pageSize;
652 margin.set(margin.x()/2, margin.y()/2);
653
654 page.setMarginTop(margin.y()/72.0);
655 page.setMarginBottom(margin.y()/72.0);
656 page.setMarginLeft(margin.x()/72.0);
657 page.setMarginRight(margin.x()/72.0);
658 page.setFormLength(pageSize.y()/72.);
659 page.setFormWidth(pageSize.x()/72.);
660
661 if (paperSize.y() > paperSize.x())
662 page.setFormOrientation(MWAWPageSpan::PORTRAIT);
663 else
664 page.setFormOrientation(MWAWPageSpan::LANDSCAPE);
665
666 libmwaw::DebugStream f;
667 f << info;
668
669 m_document->ascii().addPos(debPos);
670 m_document->ascii().addNote(f.str().c_str());
671 }
672 return true;
673 }
674
675 ////////////////////////////////////////////////////////////
676 // document
677 ////////////////////////////////////////////////////////////
678 bool MsWks4Zone::readDOP(MWAWInputStreamPtr input, MWAWEntry const &entry, MWAWPageSpan &page)
679 {
680 if (!entry.hasType("DOP ")) {
681 MWAW_DEBUG_MSG(("Works: unknown DOP type='%s'\n", entry.type().c_str()));
682 return false;
683 }
684 entry.setParsed(true);
685
686 // FIXME: update the page
687 page=MWAWPageSpan();
688
689 libmwaw::DebugStream f, f2;
690
691 long debPage = entry.begin();
692 long length = entry.length();
693 long endPage = entry.end();
694
695 input->seek(debPage, librevenge::RVNG_SEEK_SET);
696 auto sz = static_cast<int>(input->readULong(1));
697 if (sz != length-1) f << "###sz=" << sz << ",";
698
699 bool ok = true;
700 double dim[6] = {-1., -1., -1., -1., -1., -1. };
701 while (input->tell() < endPage) {
702 long debPos = input->tell();
703 auto val = static_cast<int>(input->readULong(1));
704
705 switch (val) {
706 case 0x41: // w (or 4000 when frame)
707 case 0x42: // h (or 4000 when frame)
708 case 0x43: // and margin (y,x)
709 case 0x44:
710 case 0x45:
711 case 0x46: {
712 if (debPos+2 > endPage) {
713 ok = false;
714 break;
715 }
716 long d = static_cast<int>(input->readULong(2));
717 if (d == 4000) break;
718 dim[val-0x41] = double(d)/72.;
719 break;
720 }
721
722 case 0x4d: { // cols-1 ?
723 if (debPos+2 > endPage) {
724 ok = false;
725 break;
726 }
727 auto v = static_cast<int>(input->readULong(2));
728 m_state->m_numColumns=v+1;
729 if (v) f2 << "cols=" << m_state->m_numColumns << ",";
730 break;
731 }
732 case 0x4f: {
733 if (debPos+1 > endPage) {
734 ok = false;
735 break;
736 }
737 auto v = static_cast<int>(input->readLong(1));
738 if (v==1) {
739 m_state->m_hasColumnSep=true;
740 f2 << "hasColSep,";
741 }
742 else if (v)
743 f2 << "#hasColSeps=" << v << ",";
744 break;
745
746 }
747 case 0x60: // always 1 ?
748 case 0x61: {
749 if (debPos+2 > endPage) {
750 ok = false;
751 break;
752 }
753 auto v = static_cast<int>(input->readULong(2));
754 f2 << "f" << val;
755 if (v != 1) f2 << "=" << v << "#";
756 f2 << ",";
757 break;
758 }
759
760 case 0x62: { // some flag : 1| 11| 31| 42| 44| 80 | 100 ?
761 if (debPos+2 > endPage) {
762 ok = false;
763 break;
764 }
765 auto v = static_cast<int>(input->readULong(2));
766 f2 << "f" << val << "=" << std::hex << v << std::dec << ",";
767 break;
768 }
769
770 case 0x6c: // always follows by three 0xFFFF ? white color ?
771 if (debPos+6 > endPage) {
772 ok = false;
773 break;
774 }
775 f2 << "bkcol?=(";
776 for (int i = 0; i < 3; i++) {
777 f2 << input->readULong(1) << ",";
778 input->readULong(1);
779 }
780 f2 << "),";
781 break;
782
783 case 0x48: // 12
784 case 0x4c: { // 36
785 if (debPos+2 > endPage) {
786 ok = false;
787 break;
788 }
789 auto v = static_cast<int>(input->readLong(2));
790 f2 << "f" << val << "=" << v << ",";
791 break;
792 }
793
794 case 0x6a: { // 1
795 if (debPos+1 > endPage) {
796 ok = false;
797 break;
798 }
799 auto v = static_cast<int>(input->readLong(1));
800 f2 << "f" << val << "=" << v << ",";
801 break;
802 }
803
804 case 0x5f: // -1 if header|footer ?
805 case 0x63: // -1 if header ?
806 case 0x64: // -1 if footer ?
807 case 0x66: // 1
808 case 0x67: { // 1
809 if (debPos+2 > endPage) {
810 ok = false;
811 break;
812 }
813 auto v = static_cast<int>(input->readLong(1));
814 f2 << "f" << val << "=" << v << ",";
815 break;
816 }
817
818 default:
819 ok = false;
820 break;
821 }
822
823 if (ok) continue;
824
825 m_document->ascii().addPos(debPos);
826 m_document->ascii().addNote("DOP ###");
827
828 break;
829 }
830 bool dimOk=dim[1]>2.*(dim[2]+dim[4]) && dim[0]>2.*(dim[3]+dim[5]);
831 for (int i = 2; i < 6; i++)
832 dimOk = dimOk && dim[i]>=0;
833 if (dimOk) {
834 if (dim[1] > dim[0])
835 page.setFormOrientation(MWAWPageSpan::PORTRAIT);
836 else
837 page.setFormOrientation(MWAWPageSpan::LANDSCAPE);
838 page.setFormLength(dim[1]);
839 page.setFormWidth(dim[0]);
840 page.setMarginTop(dim[2]);
841 page.setMarginBottom(dim[4]);
842 page.setMarginLeft(dim[3]);
843 page.setMarginRight(dim[5]);
844 }
845 if (dim[0] > 0. || dim[1] > 0.)
846 f << "sz=" << dim[0] << "x" << dim[1] << ",";
847 bool hasMargin = false;
848 for (int i = 2; i < 6; i++) {
849 if (dim[i] > 0.0) {
850 hasMargin = true;
851 break;
852 }
853 }
854 if (hasMargin) {
855 f << "margin=(";
856 for (int i = 2; i < 6; i++) {
857 if (dim[i] < 0) f << "_";
858 else f << dim[i];
859 if (i == 2 || i == 4) f << "x";
860 else f << " ";
861 }
862 f << "),";
863 }
864 f << f2.str();
865 m_document->ascii().addPos(debPage);
866 m_document->ascii().addNote(f.str().c_str());
867
868 return dimOk;
869 }
870
871 // the position in the page ? Constant size 0x28c ?
872 // link to the graphic entry RBDR ?
873 // does this means RL -> RB while RBDR means RB->DRawing
874 bool MsWks4Zone::readRLRB(MWAWInputStreamPtr input, MWAWEntry const &entry)
875 {
876 if (entry.length() < 13+32) return false;
877 entry.setParsed(true);
878
879 long debPos = entry.begin();
880 input->seek(debPos, librevenge::RVNG_SEEK_SET);
881 libmwaw::DebugStream f;
882 f << "BDB1=("; // some kind of bdbox: BDB1
883 for (int i = 0; i < 4; i++)
884 f << input->readLong(2) << ", ";
885 f << "), ";
886 f << input->readLong(1) << ", ";
887 f << input->readLong(2) << ", ";
888 for (int i = 0; i < 2; i++)
889 f << input->readLong(1) << ", ";
890
891 m_document->ascii().addPos(debPos);
892 m_document->ascii().addNote(f.str().c_str());
893 m_document->ascii().addPos(input->tell());
894 m_document->ascii().addNote("RLRB(2)");
895
896
897 debPos = entry.end()-32;
898 input->seek(debPos, librevenge::RVNG_SEEK_SET);
899 f.str("");
900 // v0=[29|72..79|189], v1=[-1|2|4|33], (v2xv3) = dim?=(~700x~1000)
901 // seems related to BDB1
902 f << "RLRB(3):BDB2(";
903 for (int i = 0; i< 4; i++)
904 f << input->readLong(2) << ",";
905 f << ")," << input->readLong(1) << ","; // always 1 ?
906 f << "unk1=(" << std::hex;
907 for (int i = 0; i < 9; i++)
908 f << std::setw(2) << input->readULong(1) << ",";
909 f << ")," << input->readLong(1); // always 1 ?
910 f << ",unk2=(" << std::hex;
911 for (int i = 0; i < 5; i++)
912 f << std::setw(2) << input->readULong(1) << ",";
913 // header? : [4f|50|5e|75], 0, 0, 0,[80|8b]
914 // footer? : 48, 0, 0, 0,8b
915 // qfrm, main : no specific form ?
916 f << "),dims=(" << std::dec;
917 for (int i = 0; i < 4; i++)
918 f << input->readLong(2) << ", ";
919 f << "), ";
920
921 m_document->ascii().addPos(debPos);
922 m_document->ascii().addNote(f.str().c_str());
923
924 return true;
925 }
926
927 // the frame position
928 bool MsWks4Zone::readFRAM(MWAWInputStreamPtr input, MWAWEntry const &entry)
929 {
930 libmwaw::DebugStream f;
931
932 long debPage = entry.begin();
933 long endPage = entry.end();
934
935 input->seek(debPage, librevenge::RVNG_SEEK_SET);
936 auto numFram = static_cast<int>(input->readULong(2));
937 if (numFram <= 0) return false;
938 entry.setParsed(true);
939
940 f << "N=" << numFram;
941
942 m_document->ascii().addPos(debPage);
943 m_document->ascii().addNote(f.str().c_str());
944
945 for (int i = 0; i < numFram; i++) {
946 long debPos = input->tell();
947 auto size = static_cast<int>(input->readULong(1));
948 if (size <= 0) break;
949
950 f.str("");
951
952 bool ok = true;
953 long endPos = debPos+1+size;
954 if (endPos > endPage) break;
955
956 MsWks4ZoneInternal::Frame frame;
957 MWAWVec2f fOrig, fSiz;
958 while (input->tell() < endPos) {
959 auto val = static_cast<int>(input->readULong(1));
960 long pos = input->tell();
961
962 int sz = 0;
963 bool done = true;
964 switch (val) {
965 case 0x2e: // x
966 case 0x2f: // y
967 if (pos+2 > endPos) {
968 ok = false;
969 break;
970 }
971 if (val == 0x2e) fOrig.setX(float(input->readLong(2))/72.f);
972 else fOrig.setY(float(input->readLong(2))/72.f);
973 break;
974 case 0x30: // width
975 case 0x31: // height
976 if (pos+2 > endPos) {
977 ok = false;
978 break;
979 }
980 if (val == 0x30) fSiz.setX(float(input->readLong(2))/72.f);
981 else fSiz.setY(float(input->readLong(2))/72.f);
982 break;
983 case 0x3c: { // type : checkme ?
984 if (pos+2 > endPos) {
985 ok = false;
986 break;
987 }
988 auto value = static_cast<int>(input->readLong(2));
989 switch (value) {
990 case 1:
991 frame.m_type = MsWks4ZoneInternal::Frame::Textbox;
992 break;
993 case 2:
994 frame.m_type = MsWks4ZoneInternal::Frame::Header;
995 break;
996 case 4:
997 frame.m_type = MsWks4ZoneInternal::Frame::Footer;
998 break;
999 default:
1000 f << "###type=" << value << ",";
1001 }
1002 break;
1003 }
1004 case 0x29: { // always -1 ?
1005 if (pos+2 > endPos) {
1006 ok = false;
1007 break;
1008 }
1009 auto value = static_cast<int>(input->readLong(2));
1010 if (value != -1) f << "###29=" << value << ",";
1011 }
1012 break;
1013 case 0x2a: // in main 2a=6, in frame 2a=4, 2b=4
1014 case 0x2b:
1015 done = false;
1016 sz = 1;
1017 break;
1018 case 0x3d: // only in textBox, related to the field id of Graphics15
1019 if (pos+4 > endPos) {
1020 ok = false;
1021 break;
1022 }
1023 f << "textBoxId=X" << std::hex << input->readLong(4) << "," << std::dec;
1024 break;
1025 case 0x3e: { // appear only in QFrm with value ~ 1/2, 2000
1026 if (pos+4 > endPos) {
1027 ok = false;
1028 break;
1029 }
1030 f << std::hex << val << "=(" << std::dec;
1031 for (int j = 0; j < 2; j++)
1032 f << input->readLong(2) << ",";
1033 f << "),";
1034 }
1035 break;
1036
1037 case 0x37: {
1038 if (pos+6 > endPos) {
1039 ok = false;
1040 break;
1041 }
1042 std::string fName("");
1043 for (int j = 0; j < 4; j++) {
1044 auto c = char(input->readULong(1));
1045 if (c == '\0') continue;
1046 fName += c;
1047 }
1048 frame.m_pictId.setName(fName);
1049 frame.m_pictId.setId(static_cast<int>(input->readULong(2)));
1050 done = true;
1051 }
1052 break;
1053 default:
1054 done = false;
1055 break;
1056 }
1057
1058 if (!ok) break;
1059 if (done) continue;
1060
1061 if (sz == 0 || pos + sz > endPos) {
1062 input->seek(-1, librevenge::RVNG_SEEK_CUR);
1063 ok = false;
1064 break;
1065 }
1066
1067 f << std::hex << val << "=" << std::dec;
1068 auto v2 = static_cast<int>(input->readLong(sz)); // sz=1
1069 f << v2 << ", ";
1070 }
1071
1072 frame.m_position=MWAWPosition(fOrig, fSiz);
1073 frame.m_position.setPage(-3);
1074 frame.m_error = f.str();
1075 f.str("");
1076 m_state->m_framesList.push_back(frame);
1077
1078 f << "FRAM(" << size << "):" << frame;
1079 m_document->ascii().addPos(debPos);
1080 m_document->ascii().addNote(f.str().c_str());
1081 if (!ok) {
1082 m_document->ascii().addPos(input->tell());
1083 m_document->ascii().addNote("FRAM###");
1084 }
1085
1086 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1087 }
1088
1089 return true;
1090 }
1091
1092 // the selection
1093 bool MsWks4Zone::readSELN(MWAWInputStreamPtr input, MWAWEntry const &entry)
1094 {
1095 libmwaw::DebugStream f;
1096
1097 long debPage = entry.begin();
1098 long endPage = entry.end();
1099
1100 input->seek(debPage, librevenge::RVNG_SEEK_SET);
1101 if (endPage-debPage <= 12) {
1102 MWAW_DEBUG_MSG(("MsWks4Zone::readSELN: SELN size=%ld too short\n", endPage-debPage));
1103 return false;
1104 }
1105 entry.setParsed(true);
1106
1107 auto type = static_cast<int>(input->readLong(1));
1108 // 2,3
1109 switch (type) {
1110 case 2:
1111 f << "textPoint, ";
1112 break;
1113 case 3:
1114 f << "textZone, ";
1115 break;
1116 default:
1117 f << "type=###" << type << ",";
1118 break;
1119 }
1120 // 0 0xFF 0
1121 for (int i = 0; i < 3; i++) {
1122 auto val = static_cast<int>(input->readLong(1));
1123 if ((i%2)+val)
1124 f << "unk" << i << "=" << val << ",";
1125 }
1126 // textBegin, textEnd
1127 f << "textPos?=(";
1128 for (int i = 0; i < 2; i++) {
1129 f << input->readLong(4);
1130 if (i == 0) f << "<->";
1131 }
1132 f << ")";
1133
1134 // I find f2=-1,f20=-1,f21=-1 and f22=0|1
1135 // maybe f22=0 -> mainTextZone, f22=1 -> header, ...
1136 for (long i = 0; i < (endPage-debPage-12)/2; i++) {
1137 auto val = static_cast<int>(input->readLong(2));
1138 if (val) f << ",f" << i << "=" << val;
1139 }
1140
1141 m_document->ascii().addPos(debPage);
1142 m_document->ascii().addNote(f.str().c_str());
1143
1144 if (long(input->tell()) != endPage) {
1145 m_document->ascii().addPos(input->tell());
1146 m_document->ascii().addNote("SELN###");
1147 }
1148 return true;
1149 }
1150 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: