Debian Codegraph @

src/lib/ClarisWksDocument.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 <string.h>
35
36 #include <algorithm>
37 #include <iomanip>
38 #include <iostream>
39 #include <limits>
40 #include <map>
41 #include <set>
42 #include <sstream>
43
44 #include <librevenge/librevenge.h>
45
46 #include "MWAWDebug.hxx"
47 #include "MWAWFontConverter.hxx"
48 #include "MWAWHeader.hxx"
49 #include "MWAWInputStream.hxx"
50 #include "MWAWListener.hxx"
51 #include "MWAWParser.hxx"
52 #include "MWAWPrinter.hxx"
53 #include "MWAWSection.hxx"
54
55 #include "ClarisWksDatabase.hxx"
56 #include "ClarisWksGraph.hxx"
57 #include "ClarisWksPresentation.hxx"
58 #include "ClarisWksSpreadsheet.hxx"
59 #include "ClarisWksStyleManager.hxx"
60 #include "ClarisWksStruct.hxx"
61 #include "ClarisWksTable.hxx"
62 #include "ClarisWksText.hxx"
63
64 #include "ClarisWksDocument.hxx"
65
66 /** Internal: the structures of a ClarisWksDocument */
67 namespace ClarisWksDocumentInternal
68 {
69 ////////////////////////////////////////
70 //! Internal: the state of a ClarisWksDocument
71 struct State {
72 //! constructor
73 State()
74 : m_pageSpanSet(false)
75 , m_numPages(0)
76 , m_pages(0,0)
77 , m_pagesVerified(false)
78 , m_metaData()
79 , m_headerId(0)
80 , m_footerId(0)
81 , m_headerHeight(0)
82 , m_footerHeight(0)
83 , m_columns(1)
84 , m_columnsWidth()
85 , m_columnsSep()
86 , m_zonesMap()
87 , m_zonesTypeMap()
88 , m_rootZonesList()
89 , m_hFZonesList()
90 , m_zonesSentSet()
91 , m_linkIdToDabasePictIdMap()
92 {
93 }
94 //! returns a zone's type
95 ClarisWksStruct::DSET::Position getZoneType(int id) const
96 {
97 if (m_zonesTypeMap.find(id)==m_zonesTypeMap.end())
98 return ClarisWksStruct::DSET::P_Unknown;
99 return m_zonesTypeMap.find(id)->second;
100 }
101 //! a flag to know if pageSpan is filled
102 bool m_pageSpanSet;
103
104 //! the number of pages (computed)
105 int m_numPages;
106 //! the number of pages find in the header ( if known )
107 MWAWVec2i m_pages;
108 //! true if the number pages of pages has been verified
109 bool m_pagesVerified;
110 //! the metadata
111 librevenge::RVNGPropertyList m_metaData;
112 int m_headerId /** the header zone if known */,
113 m_footerId /** the footer zone if known */;
114 int m_headerHeight /** the header height if known */,
115 m_footerHeight /** the footer height if known */;
116
117 /** the number of columns */
118 int m_columns;
119 /** the columns witdh in Points*/
120 std::vector<int> m_columnsWidth;
121 /** the columns separator in Points*/
122 std::vector<int> m_columnsSep;
123
124 /** the map of zone*/
125 std::map<int, std::shared_ptr<ClarisWksStruct::DSET> > m_zonesMap;
126 /** the type of each zone*/
127 std::map<int, ClarisWksStruct::DSET::Position> m_zonesTypeMap;
128 /** the list of root zones */
129 std::vector<int> m_rootZonesList;
130 /** the list of header/footer group */
131 std::vector<int> m_hFZonesList;
132 /** a stack of send zone (id,anchor) used to avoid unsolved potential loop */
133 std::set<MWAWVec2i> m_zonesSentSet;
134 /** a map pict id to database pict id */
135 std::map<int,int> m_linkIdToDabasePictIdMap;
136 };
137
138 ////////////////////////////////////////
139 //! Internal: the subdocument of a ClarisWksDocument
140 class SubDocument final : public MWAWSubDocument
141 {
142 public:
143 SubDocument(ClarisWksDocument &doc, MWAWInputStreamPtr const &input, int zoneId)
144 : MWAWSubDocument(nullptr, input, MWAWEntry())
145 , m_document(doc)
146 , m_id(zoneId) {}
147
148 //! destructor
149 ~SubDocument() final {}
150
151 //! operator!=
152 bool operator!=(MWAWSubDocument const &doc) const final
153 {
154 if (MWAWSubDocument::operator!=(doc)) return true;
155 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
156 if (!sDoc) return true;
157 if (&m_document != &sDoc->m_document) return true;
158 if (m_id != sDoc->m_id) return true;
159 return false;
160 }
161
162 //! the parser function
163 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
164
165 protected:
166 //! the document manager
167 ClarisWksDocument &m_document;
168 //! the subdocument id
169 int m_id;
170 };
171
172 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
173 {
174 if (!listener.get()) {
175 MWAW_DEBUG_MSG(("ClarisWksDocumentInternal::SubDocument::parse: no listener\n"));
176 return;
177 }
178 if (m_id == -1) { // a number used to send linked frame
179 listener->insertChar(' ');
180 return;
181 }
182 if (m_id == 0) {
183 MWAW_DEBUG_MSG(("ClarisWksDocumentInternal::SubDocument::parse: unknown zone\n"));
184 return;
185 }
186
187 m_document.sendZone(m_id, listener);
188 }
189 }
190
191 ClarisWksDocument::ClarisWksDocument(MWAWParser &parser)
192 : m_state(new ClarisWksDocumentInternal::State)
193 , m_parserState(parser.getParserState())
194 , m_parser(&parser)
195 , m_styleManager()
196 , m_databaseParser()
197 , m_graphParser()
198 , m_presentationParser()
199 , m_spreadsheetParser()
200 , m_tableParser()
201 , m_textParser()
202 , m_newPage(nullptr)
203 , m_sendFootnote(nullptr)
204 {
205 m_styleManager.reset(new ClarisWksStyleManager(m_parserState, this));
206
207 m_databaseParser.reset(new ClarisWksDatabase(*this));
208 m_graphParser.reset(new ClarisWksGraph(*this));
209 m_presentationParser.reset(new ClarisWksPresentation(*this));
210 m_spreadsheetParser.reset(new ClarisWksSpreadsheet(*this));
211 m_tableParser.reset(new ClarisWksTable(*this));
212 m_textParser.reset(new ClarisWksText(*this));
213 }
214
215 ClarisWksDocument::~ClarisWksDocument()
216 {
217 }
218
219 ////////////////////////////////////////////////////////////
220 // position and height
221 ////////////////////////////////////////////////////////////
222 MWAWVec2i ClarisWksDocument::getDocumentPages()
223 {
224 if (!m_state->m_pagesVerified && m_parserState->m_kind==MWAWDocument::MWAW_K_DRAW) {
225 int numHPages=m_state->m_pages[0];
226 float textWidth=72.0f*float(m_parser->getPageWidth());
227 for (auto const &iter : m_state->m_zonesMap) {
228 std::shared_ptr<ClarisWksStruct::DSET> group = iter.second;
229 if (!group || group->m_position != ClarisWksStruct::DSET::P_Main)
230 continue;
231 int page=textWidth>0.2f ? int(float(group->getUnionChildBox()[1][0])/textWidth-0.2f)+1 : 1;
232 if (page > numHPages && page < numHPages+10) {
233 MWAW_DEBUG_MSG(("ClarisWksGraph::computePositions: increase num page accross to %d\n", page));
234 numHPages = page;
235 }
236 }
237 m_state->m_pages[0]=numHPages;
238 }
239 else if (m_state->m_pages[0]<=0)
240 m_state->m_pages[0]=1;
241 m_state->m_pagesVerified=true;
242 return m_state->m_pages;
243 }
244
245 int ClarisWksDocument::numPages() const
246 {
247 if (m_state->m_numPages>0) return m_state->m_numPages;
248 int numPage = m_textParser->numPages();
249 if (m_databaseParser->numPages() > numPage)
250 numPage = m_databaseParser->numPages();
251 if (m_presentationParser->numPages() > numPage)
252 numPage = m_presentationParser->numPages();
253 if (m_graphParser->numPages() > numPage)
254 numPage = m_graphParser->numPages();
255 if (m_spreadsheetParser->numPages() > numPage)
256 numPage = m_spreadsheetParser->numPages();
257 if (m_tableParser->numPages() > numPage)
258 numPage = m_tableParser->numPages();
259 m_state->m_numPages=numPage;
260 return numPage;
261 }
262
263 librevenge::RVNGPropertyList const &ClarisWksDocument::getDocumentMetaData() const
264 {
265 return m_state->m_metaData;
266 }
267
268 void ClarisWksDocument::updatePageSpanList(std::vector<MWAWPageSpan> &pageList)
269 {
270 MWAWPageSpan ps=getFinalPageSpan();
271 if (m_textParser->updatePageSpanList(ps, pageList))
272 return;
273 pageList.resize(0);
274 for (int i = 0; i < 2; i++) {
275 int zoneId = i==0 ? m_state->m_headerId : m_state->m_footerId;
276 if (zoneId == 0)
277 continue;
278 MWAWHeaderFooter hF((i==0) ? MWAWHeaderFooter::HEADER : MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
279 hF.m_subDocument.reset(new ClarisWksDocumentInternal::SubDocument(*this, m_parserState->m_input, zoneId));
280 ps.setHeaderFooter(hF);
281 }
282 ps.setPageSpan(numPages());
283 pageList=std::vector<MWAWPageSpan>(1,ps);
284 }
285
286 void ClarisWksDocument::updatePageSpanList(std::vector<MWAWPageSpan> &pageList, MWAWPageSpan &master)
287 {
288 MWAWPageSpan ps=getFinalPageSpan();
289 ps.setMasterPageName(librevenge::RVNGString("Master"));
290 master=ps;
291 if (m_presentationParser->updatePageSpanList(ps, pageList))
292 return;
293 ps.setPageSpan(numPages());
294 pageList=std::vector<MWAWPageSpan>(1,ps);
295 }
296
297 MWAWPageSpan ClarisWksDocument::getFinalPageSpan() const
298 {
299 MWAWPageSpan ps(m_parserState->m_pageSpan);
300 // decrease right | bottom
301 if (ps.getMarginRight()>50./72.)
302 ps.setMarginRight(ps.getMarginRight()-50./72.);
303 else
304 ps.setMarginRight(0);
305 if (ps.getMarginBottom()>50./72.)
306 ps.setMarginBottom(ps.getMarginBottom()-50./72.);
307 else
308 ps.setMarginBottom(0);
309 return ps;
310 }
311
312 double ClarisWksDocument::getTextHeight() const
313 {
314 return m_parserState->m_pageSpan.getPageLength()-m_state->m_headerHeight/72.0-m_state->m_footerHeight/72.0;
315 }
316
317 MWAWVec2f ClarisWksDocument::getPageLeftTop() const
318 {
319 return MWAWVec2f(float(m_parserState->m_pageSpan.getMarginLeft()),
320 float(m_parserState->m_pageSpan.getMarginTop()+m_state->m_headerHeight/72.0));
321 }
322
323 ////////////////////////////////////////////////////////////
324 // interface via callback
325 ////////////////////////////////////////////////////////////
326 void ClarisWksDocument::newPage(int page, bool softBreak)
327 {
328 if (!m_newPage) {
329 MWAW_DEBUG_MSG(("ClarisWksDocument::newPage: can not find newPage callback\n"));
330 return;
331 }
332 (m_parser->*m_newPage)(page, softBreak);
333 }
334
335 MWAWSection ClarisWksDocument::getMainSection() const
336 {
337 MWAWSection sec;
338 if (m_state->m_columns <= 1)
339 return sec;
340 auto numCols = size_t(m_state->m_columns);
341 bool hasSep = m_state->m_columnsSep.size()+1==numCols;
342 bool hasWidth = m_state->m_columnsWidth.size()==numCols;
343 double width=0.0;
344 if (!hasWidth) {
345 double totalWidth = 72.0*m_parserState->m_pageSpan.getPageWidth();
346 for (size_t c=0; c+1 < numCols; c++)
347 totalWidth -= double(m_state->m_columnsSep[c]);
348 width = totalWidth/double(numCols);
349 }
350 sec.m_columns.resize(numCols);
351 for (size_t c=0; c < numCols; c++) {
352 sec.m_columns[c].m_width =
353 hasWidth ? double(m_state->m_columnsWidth[c]) : width;
354 sec.m_columns[c].m_widthUnit = librevenge::RVNG_POINT;
355 if (!hasSep)
356 continue;
357 if (c)
358 sec.m_columns[c].m_margins[libmwaw::Left]=
359 double(m_state->m_columnsSep[c-1])/72./2.;
360 if (c+1!=numCols)
361 sec.m_columns[c].m_margins[libmwaw::Right]=
362 double(m_state->m_columnsSep[c])/72./2.;
363 }
364 return sec;
365 }
366
367 void ClarisWksDocument::sendFootnote(int zoneId)
368 {
369 if (!m_sendFootnote) {
370 MWAW_DEBUG_MSG(("ClarisWksDocument::sendFootnote: can not find sendFootnote callback\n"));
371 return;
372 }
373 (m_parser->*m_sendFootnote)(zoneId);
374 }
375
376 void ClarisWksDocument::checkOrdering(std::vector<int16_t> &vec16, std::vector<int32_t> &vec32) const
377 {
378 if (!m_parserState || m_parserState->m_version < 4) return;
379 int numSmallEndian = 0, numBigEndian = 0;
380 unsigned long val;
381 for (auto const &i : vec16) {
382 val = static_cast<unsigned long>(static_cast<uint16_t>(i));
383 if ((val & 0xFF00) && !(val & 0xFF))
384 numSmallEndian++;
385 else if ((val&0xFF) && !(val&0xFF00))
386 numBigEndian++;
387 }
388 for (auto const &i : vec32) {
389 val = static_cast<unsigned long>(static_cast<uint32_t>(i));
390 if ((val & 0xFFFF0000) && !(val & 0xFFFF))
391 numSmallEndian++;
392 else if ((val&0xFFFF) && !(val&0xFFFF0000))
393 numBigEndian++;
394 }
395 if (numBigEndian >= numSmallEndian)
396 return;
397 for (auto &i : vec16) {
398 val = static_cast<unsigned long>(static_cast<uint16_t>(i));
399 i = static_cast<int16_t>((val>>8) & ((val&0xFF)<<8));
400 }
401 for (auto &i : vec32) {
402 val = static_cast<unsigned long>(static_cast<uint32_t>(i));
403 i = static_cast<int32_t>((val>>16) & ((val&0xFFFF)<<16));
404 }
405 }
406
407 ////////////////////////////////////////////////////////////
408 // zone function
409 ////////////////////////////////////////////////////////////
410 std::vector<int> const &ClarisWksDocument::getMainZonesList() const
411 {
412 return m_state->m_rootZonesList;
413 }
414
415 std::shared_ptr<ClarisWksStruct::DSET> ClarisWksDocument::getZone(int zId) const
416 {
417 auto iter = m_state->m_zonesMap.find(zId);
418 if (iter != m_state->m_zonesMap.end())
419 return iter->second;
420 return std::shared_ptr<ClarisWksStruct::DSET>();
421 }
422
423 bool ClarisWksDocument::canSendZoneAsGraphic(int zoneId) const
424 {
425 auto zMap = getZone(zoneId);
426 MWAWVec2i zoneAnchor(zoneId, -1);
427 if (!zMap || m_state->m_zonesSentSet.find(zoneAnchor)!=m_state->m_zonesSentSet.end()) return false;
428 bool res=false;
429 m_state->m_zonesSentSet.insert(zoneAnchor);
430 switch (zMap->m_fileType) {
431 case 0:
432 res=m_graphParser->canSendGroupAsGraphic(zoneId);
433 break;
434 case 1:
435 res=m_textParser->canSendTextAsGraphic(zoneId);
436 break;
437 case 2:
438 res=m_spreadsheetParser->canSendSpreadsheetAsGraphic(zoneId);
439 break;
440 case 3:
441 res=m_databaseParser->canSendDatabaseAsGraphic(zoneId);
442 break;
443 case 4:
444 res=m_graphParser->canSendBitmapAsGraphic(zoneId);
445 break;
446 default:
447 break;
448 }
449 m_state->m_zonesSentSet.erase(zoneAnchor);
450 return res;
451 }
452
453 bool ClarisWksDocument::sendZone(int zoneId, MWAWListenerPtr listener, MWAWPosition position)
454 {
455 auto zMap = getZone(zoneId);
456 if (!zMap) return false;
457 MWAWVec2i zoneAnchor(zoneId, position.m_anchorTo);
458 if (m_state->m_zonesSentSet.find(zoneAnchor)!=m_state->m_zonesSentSet.end()) {
459 MWAW_DEBUG_MSG(("ClarisWksDocument::sendZone: argh find a loop for %d zone\n", zoneId));
460 return false;
461 }
462 m_state->m_zonesSentSet.insert(zoneAnchor);
463 MWAWInputStreamPtr input = m_parserState->m_input;
464 long pos = input->tell();
465 bool res = false;
466 switch (zMap->m_fileType) {
467 case 0:
468 res = getGraphParser()->sendGroup(zoneId, listener, position);
469 break;
470 case 1:
471 res = getTextParser()->sendZone(zoneId, listener);
472 break;
473 case 4:
474 res = getGraphParser()->sendBitmap(zoneId, listener, position);
475 break;
476 case 5: {
477 if (!listener) listener=m_parserState->getMainListener();
478 if (listener && listener->getType()==MWAWListener::Presentation)
479 res = getPresentationParser()->sendZone(zoneId);
480 else {
481 MWAW_DEBUG_MSG(("ClarisWksDocument::sendZone: sorry, not possible to send a presentation zone %d in a not presentation document\n", zoneId));
482 }
483 break;
484 }
485 case 6:
486 res = getTableParser()->sendZone(zoneId);
487 break;
488 case 2:
489 res = getSpreadsheetParser()->sendSpreadsheet(zoneId, listener);
490 break;
491 case 3:
492 res = getDatabaseParser()->sendDatabase(zoneId, listener);
493 break;
494 default:
495 MWAW_DEBUG_MSG(("ClarisWksDocument::sendZone: can not send zone: %d\n", zoneId));
496 break;
497 }
498 input->seek(pos, librevenge::RVNG_SEEK_SET);
499 zMap->m_parsed = true;
500 m_state->m_zonesSentSet.erase(zoneAnchor);
501 return res;
502 }
503
504 bool ClarisWksDocument::sendDatabasePictZone(int pictId, MWAWListenerPtr listener, MWAWPosition const &pos)
505 {
506 auto it=m_state->m_linkIdToDabasePictIdMap.find(pictId);
507 if (it==m_state->m_linkIdToDabasePictIdMap.end()) {
508 MWAW_DEBUG_MSG(("ClarisWksDocument::sendDatabasePictZone: can not find picture id: %d\n", pictId));
509 return false;
510 }
511 return getGraphParser()->sendDatabasePictZone(it->second, listener, pos);
512 }
513
514 ////////////////////////////////////////////////////////////
515 //
516 // Intermediate level
517 //
518 ////////////////////////////////////////////////////////////
519 bool ClarisWksDocument::createZones()
520 {
521 MWAWInputStreamPtr input = m_parserState->m_input;
522 int const vers=m_parserState->m_version;
523 libmwaw::DebugStream f;
524 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
525
526 long eof=-1;
527 if (readDocHeader() && readDocInfo()) {
528 long pos = input->tell();
529 // time to read the styles
530 if (vers > 1) {
531 readEndTable(eof);
532 if (eof > 0)
533 input->pushLimit(eof);
534 input->seek(pos, librevenge::RVNG_SEEK_SET);
535 }
536
537 pos = input->tell();
538 while (!input->isEnd()) {
539 if (!readZone()) {
540 input->seek(pos, librevenge::RVNG_SEEK_SET);
541 break;
542 }
543 pos = input->tell();
544 }
545 }
546 else if (vers > 1) {
547 // try at least to read the styles table
548 long pos = input->tell();
549 readEndTable(eof);
550 if (eof > 0)
551 input->pushLimit(eof);
552 input->seek(pos, librevenge::RVNG_SEEK_SET);
553 }
554
555 if (!input->isEnd()) {
556 ascFile.addPos(input->tell());
557 f.str("");
558 f << "Entries(Loose): vers=" << vers;
559 ascFile.addNote(f.str().c_str());
560 }
561 // look for graphic
562 while (!input->isEnd()) {
563 long pos = input->tell();
564 auto val = static_cast<int>(input->readULong(2));
565 if (input->isEnd()) break;
566 bool ok = false;
567 if (val == 0x4453) {
568 if (input->readULong(2) == 0x4554) {
569 ok = true;
570 input->seek(-4, librevenge::RVNG_SEEK_CUR);
571 }
572 }
573 if (!ok && (val == 0x1101 || val == 0x1102)) {
574 long debPos = (val == 0x1102) ? pos-15 : pos-14;
575 input->seek(debPos, librevenge::RVNG_SEEK_SET);
576 if (input->readULong(2) == 0) {
577 auto sz = static_cast<int>(input->readULong(2));
578 auto fileSz = static_cast<int>(input->readULong(2));
579 if (sz >= 0x10 && (val == 0x1102 || sz == fileSz)) {
580 ok = true;
581 input->seek(-6, librevenge::RVNG_SEEK_CUR);
582 }
583 }
584 }
585 if (!ok) {
586 input->seek(pos+1, librevenge::RVNG_SEEK_SET);
587 continue;
588 }
589
590 if (input->isEnd()) break;
591
592 long prevPos = pos;
593 ok = false;
594 while (!input->isEnd()) {
595 if (!readZone()) {
596 input->seek(pos+1, librevenge::RVNG_SEEK_SET);
597 break;
598 }
599 pos = input->tell();
600 if (pos <= prevPos)
601 break;
602 ok = true;
603 }
604 if (!ok || pos <= prevPos) {
605 input->seek(prevPos+1, librevenge::RVNG_SEEK_SET);
606 continue;
607 }
608 if (input->isEnd()) break;
609
610 ascFile.addPos(pos);
611 ascFile.addNote("Entries(End)");
612 }
613 if (eof > 0)
614 input->popLimit();
615 exploreZonesGraph();
616 typeMainZones();
617 cleanZonesGraph();
618 updateChildPositions();
619
620 #ifdef DEBUG
621 // let check if all is ok
622 size_t numMain=m_state->m_rootZonesList.size();
623 if (1 && numMain == 1)
624 return !getMainZonesList().empty();
625 MWAW_DEBUG_MSG(("ClarisWksDocument::createZones: find %d main zones\n", int(numMain)));
626 // we have do not have find the root note : probably a database...
627 std::cerr << "--------------------------------------------------------\n";
628 std::cerr << "List of potential main zones : ";
629 for (auto const &zone : m_state->m_rootZonesList)
630 std::cerr << zone << ",";
631 std::cerr << "\n";
632 for (auto const &iter : m_state->m_zonesMap)
633 std::cerr << *iter.second << "\n";
634 std::cerr << "--------------------------------------------------------\n";
635 #endif
636 return !getMainZonesList().empty();
637 }
638
639 ////////////////////////////////////////////////////////////
640 // read the header
641 ////////////////////////////////////////////////////////////
642 bool ClarisWksDocument::checkHeader(MWAWHeader *header, bool strict)
643 {
644 *m_state = ClarisWksDocumentInternal::State();
645
646 MWAWInputStreamPtr input = m_parserState->m_input;
647 if (!input || !input->hasDataFork())
648 return false;
649 libmwaw::DebugStream f;
650 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
651
652 int const headerSize=8;
653 if (!input->checkPosition(headerSize)) {
654 MWAW_DEBUG_MSG(("ClarisWksDocument::checkHeader: file is too short\n"));
655 return false;
656 }
657 input->seek(0,librevenge::RVNG_SEEK_SET);
658 f << "FileHeader:";
659 auto vers = static_cast<int>(input->readLong(1));
660 m_parserState->m_version = vers;
661 if (vers <=0 || vers > 6) {
662 MWAW_DEBUG_MSG(("ClarisWksDocument::checkHeader: unknown version: %d\n", vers));
663 return false;
664 }
665 f << "vers=" << vers << ",";
666 f << "unk=" << std::hex << input->readULong(2) << ",";
667 auto val = static_cast<int>(input->readLong(1));
668 if (val)
669 f << "unkn1=" << val << ",";
670 if (input->readULong(2) != 0x424f && input->readULong(2) != 0x424f)
671 return false;
672
673 ascFile.addPos(0);
674 ascFile.addNote(f.str().c_str());
675
676 int typePos = 0;
677 switch (vers) {
678 case 1:
679 typePos = 242;
680 break;
681 case 2:
682 case 3:
683 typePos = 248;
684 break;
685 case 4:
686 typePos = 256;
687 break;
688 case 5:
689 typePos = 268;
690 break;
691 case 6:
692 typePos = 278;
693 break;
694 default:
695 break;
696 }
697 input->seek(typePos, librevenge::RVNG_SEEK_SET);
698 if (long(input->tell()) != typePos)
699 return false;
700 auto type = static_cast<int>(input->readULong(1));
701
702 MWAWDocument::Kind kind=MWAWDocument::MWAW_K_UNKNOWN;
703 switch (type) {
704 case 0:
705 kind=MWAWDocument::MWAW_K_DRAW;
706 break;
707 case 1:
708 kind=MWAWDocument::MWAW_K_TEXT;
709 break;
710 case 2:
711 kind=MWAWDocument::MWAW_K_SPREADSHEET;
712 break;
713 case 3:
714 kind=MWAWDocument::MWAW_K_DATABASE;
715 break;
716 case 4:
717 kind=MWAWDocument::MWAW_K_PAINT;
718 break;
719 case 5:
720 kind=MWAWDocument::MWAW_K_PRESENTATION;
721 break;
722 default:
723 MWAW_DEBUG_MSG(("ClarisWksDocument::checkHeader: unknown type=%d\n", type));
724 break;
725 }
726 m_parserState->m_kind=kind;
727 if (header) {
728 header->reset(MWAWDocument::MWAW_T_CLARISWORKS, vers, kind);
729 // changeme: draw files are actually converted in text document
730 if (type == 0)
731 header->setKind(MWAWDocument::MWAW_K_TEXT);
732 }
733
734 if (strict && type > 5) return false;
735 #ifndef DEBUG
736 if (type > 8) return false;
737 #endif
738 input->seek(headerSize,librevenge::RVNG_SEEK_SET);
739
740 return true;
741 }
742
743 ////////////////////////////////////////////////////////////
744 // read the zone
745 ////////////////////////////////////////////////////////////
746 bool ClarisWksDocument::readZone()
747 {
748 MWAWInputStreamPtr input = m_parserState->m_input;
749 long pos = input->tell();
750 libmwaw::DebugStream f;
751 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
752
753 std::string name("");
754 auto c = char(input->readULong(1));
755 if (!c)
756 input->seek(-1, librevenge::RVNG_SEEK_CUR);
757 else {
758 if (c >= ' ' && c <= 'z')
759 name += c;
760 else
761 return false;
762 for (int i = 0; i < 3; i++) {
763 c= char(input->readULong(1));
764 if (c >= ' ' && c <= 'z')
765 name += c;
766 else
767 return false;
768 }
769 }
770 long sz = 0;
771 if (name == "QTIM")
772 sz = 4;
773 else {
774 long debPos = input->tell();
775 sz = long(input->readULong(4));
776 if (long(input->tell()) != debPos+4) return false;
777 }
778
779 if (sz == 0) {
780 f << "Entries(Nop):" << name;
781 ascFile.addPos(pos);
782 ascFile.addNote(f.str().c_str());
783 return true;
784 }
785 MWAWEntry entry;
786 entry.setBegin(pos);
787 entry.setLength(4+sz);
788
789 long actPos = input->tell();
790 if (!input->checkPosition(entry.end())) return false;
791 bool parsed = false;
792 if (name.length()) {
793 if (name == "DSET") {
794 input->seek(pos, librevenge::RVNG_SEEK_SET);
795 bool complete;
796 if (readDSET(complete))
797 return true;
798 }
799 if (name == "FNTM") {
800 input->seek(pos, librevenge::RVNG_SEEK_SET);
801 if (m_styleManager->readFontNames())
802 return true;
803 }
804 if (name == "HDNI" && m_parserState->m_version <= 4)
805 sz = 2;
806 f << "Entries(" << name << ")";
807 }
808 else {
809 auto firstOffset = static_cast<int>(input->readULong(2));
810 if (sz >= 16) { // check for a picture .pct
811 input->seek(8, librevenge::RVNG_SEEK_CUR);
812 auto val = static_cast<int>(input->readULong(2));
813 if (val == 0x1101 && firstOffset == sz)
814 parsed = true;
815 else if (val == 0x11 && input->readULong(1)==0x2)
816 parsed = true;
817
818 if (parsed) {
819 #ifdef DEBUG_WITH_FILES
820 librevenge::RVNGBinaryData file;
821 input->seek(actPos, librevenge::RVNG_SEEK_SET);
822 input->readDataBlock(sz, file);
823
824 libmwaw::DebugStream f2;
825 static int volatile pictName = 0;
826 f2 << "Parser" << ++pictName << ".pct";
827 libmwaw::Debug::dumpFile(file, f2.str().c_str());
828 ascFile.skipZone(actPos, actPos+sz-1);
829 #endif
830 f << "Entries(PICT)";
831 }
832 }
833 if (!parsed)
834 f << "Entries(UnknownA" << sz << "A)";
835 }
836
837 if (!parsed)
838 ascFile.addDelimiter(actPos, '|');
839
840 input->seek(actPos+sz, librevenge::RVNG_SEEK_SET);
841
842 ascFile.addPos(pos);
843 ascFile.addNote(f.str().c_str());
844
845 ascFile.addPos(input->tell());
846 ascFile.addNote("_");
847
848 return true;
849 }
850
851 ////////////////////////////////////////////////////////////
852 // read the document main part
853 ////////////////////////////////////////////////////////////
854 std::shared_ptr<ClarisWksStruct::DSET> ClarisWksDocument::readDSET(bool &complete)
855 {
856 complete = false;
857 MWAWInputStreamPtr input = m_parserState->m_input;
858 long pos = input->tell();
859 libmwaw::DebugStream f;
860 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
861 if (input->readULong(4) != 0x44534554L)
862 return std::shared_ptr<ClarisWksStruct::DSET>();
863 auto sz = long(input->readULong(4));
864 MWAWEntry entry;
865 entry.setBegin(pos);
866 entry.setLength(sz+8);
867
868 if (sz < 16) return std::shared_ptr<ClarisWksStruct::DSET>();
869 long endPos = entry.end();
870 if (!input->checkPosition(endPos)) {
871 MWAW_DEBUG_MSG(("ClarisWksDocument::readDSET: file is too short\n"));
872 return std::shared_ptr<ClarisWksStruct::DSET>();
873 }
874
875 ClarisWksStruct::DSET dset;
876 dset.m_size = sz;
877 dset.m_numData = static_cast<int>(input->readULong(2));
878
879 input->seek(10, librevenge::RVNG_SEEK_CUR);
880 dset.m_fileType = static_cast<int>(input->readULong(1));
881 input->seek(-11, librevenge::RVNG_SEEK_CUR);
882 int nFlags = 0;
883 switch (dset.m_fileType) {
884 case 1: // text
885 dset.m_beginSelection = static_cast<int>(input->readLong(4));
886 dset.m_endSelection = static_cast<int>(input->readLong(4));
887 dset.m_textType = static_cast<int>(input->readULong(1));
888 dset.m_flags[nFlags++] = static_cast<int>(input->readLong(1));
889 break;
890 default: {
891 // ( see libmwaw/discussion/help/thread/9c5816d751/ )
892 // I find some windows files (filetype==5) where this subzone is written in little endian
893 //
894 // more precisely, I found dataSz,headerSz,m_flags[2] stored as in little endian
895 // while m_size, m_flags[3], m_id are still stored as big endian
896 // m_numData=0, m_flags[0|1]={ffff,0} so unsure about these fields
897 //
898 // normally, when filetype==5, we have m_size=64, m_headerSz=52,
899 // so first check if m_headerSz>m_size to decide if we must swap to little endian
900 input->seek(4, librevenge::RVNG_SEEK_CUR);
901 bool invertRead=static_cast<int>(input->readULong(2))>dset.m_size;
902 if (invertRead)
903 input->setReadInverted(true);
904 input->seek(-6, librevenge::RVNG_SEEK_CUR);
905
906 // now read the data
907 dset.m_flags[nFlags++] = static_cast<int>(input->readLong(2)); // normally -1
908 dset.m_flags[nFlags++] = static_cast<int>(input->readLong(2)); // the 0
909 dset.m_dataSz = static_cast<int>(input->readULong(2));
910 dset.m_headerSz = static_cast<int>(input->readULong(2));
911 dset.m_flags[nFlags++] = static_cast<int>(input->readLong(2));
912 if (dset.m_headerSz > dset.m_size || (dset.m_dataSz != 0 && (dset.m_size - dset.m_headerSz) / dset.m_dataSz < dset.m_numData)) {
913 MWAW_DEBUG_MSG(("ClarisWksDocument::readDSET: invalid DSET\n"));
914 return std::shared_ptr<ClarisWksStruct::DSET>();
915 }
916 if (invertRead)
917 input->setReadInverted(false);
918 break;
919 }
920 }
921 dset.m_flags[nFlags++] = static_cast<int>(input->readLong(2));
922 dset.m_id = static_cast<int>(input->readULong(2));
923 bool parsed = true;
924 std::shared_ptr<ClarisWksStruct::DSET> res;
925 switch (dset.m_fileType) {
926 case 0:
927 res = getGraphParser()->readGroupZone(dset, entry, complete);
928 break;
929 case 1:
930 res = getTextParser()->readDSETZone(dset, entry, complete);
931 break;
932 case 2:
933 res = getSpreadsheetParser()->readSpreadsheetZone(dset, entry, complete);
934 break;
935 case 3:
936 res = getDatabaseParser()->readDatabaseZone(dset, entry, complete);
937 break;
938 case 4:
939 res = getGraphParser()->readBitmapZone(dset, entry, complete);
940 break;
941 case 5:
942 res = getPresentationParser()->readPresentationZone(dset, entry, complete);
943 break;
944 case 6:
945 res = getTableParser()->readTableZone(dset, entry, complete);
946 break;
947 default:
948 parsed = false;
949 break;
950 }
951
952 if (parsed) {
953 if (!res)
954 return std::shared_ptr<ClarisWksStruct::DSET>();
955 if (m_state->m_zonesMap.find(res->m_id) != m_state->m_zonesMap.end()) {
956 MWAW_DEBUG_MSG(("ClarisWksDocument::readDSET: zone %d already exists!!!!\n",
957 res->m_id));
958 }
959 else
960 m_state->m_zonesMap[res->m_id] = res;
961 return res;
962 }
963
964 std::shared_ptr<ClarisWksStruct::DSET> zone(new ClarisWksStruct::DSET(dset));
965 f << "Entries(DSETU): " << *zone;
966
967 long data0Length = zone->m_dataSz;
968 long N = zone->m_numData;
969
970 ascFile.addDelimiter(input->tell(), '|');
971 ascFile.addPos(pos);
972 ascFile.addNote(f.str().c_str());
973
974 if (sz-12 != data0Length*N + zone->m_headerSz) {
975 MWAW_DEBUG_MSG(("ClarisWksDocument::readDSET: unexpected size for zone definition, try to continue\n"));
976 input->seek(endPos, librevenge::RVNG_SEEK_SET);
977 return zone;
978 }
979
980 long debPos = endPos-N*data0Length;
981 for (long i = 0; i < zone->m_numData; i++) {
982 input->seek(debPos, librevenge::RVNG_SEEK_SET);
983 f.str("");
984 f << "DSETU-" << i << ":";
985
986 long actPos = input->tell();
987 if (actPos != debPos && actPos != debPos+data0Length)
988 ascFile.addDelimiter(input->tell(),'|');
989 ascFile.addPos(debPos);
990 ascFile.addNote(f.str().c_str());
991 debPos += data0Length;
992 }
993
994 // in general, such a zone is followed by a small zone ( a container)
995 zone->m_otherChilds.push_back(zone->m_id+1);
996 if (m_state->m_zonesMap.find(zone->m_id) != m_state->m_zonesMap.end()) {
997 MWAW_DEBUG_MSG(("ClarisWksDocument::readDSET: zone %d already exists!!!!\n",
998 zone->m_id));
999 }
1000 else
1001 m_state->m_zonesMap[zone->m_id] = zone;
1002
1003 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1004 return zone;
1005 }
1006
1007 ////////////////////////////////////////////////////////////
1008 // read the document information
1009 ////////////////////////////////////////////////////////////
1010 bool ClarisWksDocument::readDocInfo()
1011 {
1012 MWAWInputStreamPtr input = m_parserState->m_input;
1013 int const vers=m_parserState->m_version;
1014 libmwaw::DebugStream f;
1015 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1016 f << "Entries(DocInfo):";
1017 long expectedSize=vers==1 ? 352 : vers < 6 ? 372 : 374;
1018 long pos = input->tell();
1019 long endPos=pos+expectedSize;
1020 if (!input->checkPosition(endPos)) return false;
1021 f << "ptr=" << std::hex << input->readULong(4) << std::dec << ",";
1022 int val;
1023 for (int i = 0; i < 6; i++) {
1024 val = static_cast<int>(input->readULong(2));
1025 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
1026 }
1027 m_state->m_headerId = static_cast<int>(input->readLong(2));
1028 if (m_state->m_headerId) f << "headerId=" << m_state->m_headerId << ",";
1029 val = static_cast<int>(input->readLong(2));
1030 if (val) f << "unkn=" << val << ",";
1031 m_state->m_footerId = static_cast<int>(input->readLong(2));
1032 if (m_state->m_footerId) f << "footerId=" << m_state->m_footerId << ",";
1033 for (int i=0; i < 4; ++i) {
1034 val = static_cast<int>(input->readLong(2));
1035 if (val) f << "g" << i << "=" << val << ",";
1036 }
1037 int pages[2];
1038 for (auto &page : pages) page=static_cast<int>(input->readLong(2));
1039 if (pages[1]>=1 && pages[1] < 1000 &&
1040 (pages[0]==1 || (pages[0]>1 && pages[0]<100 && m_parserState->m_kind == MWAWDocument::MWAW_K_DRAW)))
1041 m_state->m_pages=MWAWVec2i(pages[0],pages[1]);
1042 // in database field, pages[1] can be very big, this number seems related to the number of record ?
1043 else if (m_parserState->m_kind != MWAWDocument::MWAW_K_DATABASE || pages[0]!=1) {
1044 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocInfo: the number of pages seems bad\n"));
1045 f << "###";
1046 }
1047 if (pages[0]!=1 || pages[1]!=1)
1048 f << "pages[num]=" << pages[0] << "x" << pages[1] << ",";
1049 if (vers==1) {
1050 ascFile.addDelimiter(input->tell(), '|');
1051 input->seek(8, librevenge::RVNG_SEEK_CUR);
1052 ascFile.addDelimiter(input->tell(), '|');
1053
1054 auto numCols = static_cast<int>(input->readLong(2));
1055 if (numCols < 1 || numCols > 9) {
1056 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocInfo: pb reading number of columns\n"));
1057 f << "###numCols=" << numCols;
1058 numCols = 1;
1059 }
1060 if (numCols != 1)
1061 f << "numCols=" << numCols << ",";
1062 m_state->m_columns = numCols;
1063 if (numCols > 1) {
1064 auto colSep = static_cast<int>(input->readLong(2));
1065 m_state->m_columnsSep.resize(size_t(numCols-1), colSep);
1066 f << "colSep=" << colSep << ",";
1067 }
1068 else
1069 input->seek(2, librevenge::RVNG_SEEK_CUR);
1070 }
1071 ascFile.addDelimiter(input->tell(), '|');
1072 ascFile.addPos(pos);
1073 ascFile.addNote(f.str().c_str());
1074 ascFile.addPos(pos+100);
1075 ascFile.addNote("DocInfo-2");
1076 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1077 return true;
1078 }
1079
1080 ////////////////////////////////////////////////////////////
1081 // read the document header
1082 ////////////////////////////////////////////////////////////
1083 bool ClarisWksDocument::readDocHeader()
1084 {
1085 MWAWInputStreamPtr input = m_parserState->m_input;
1086 int const vers=m_parserState->m_version;
1087 long debPos = input->tell();
1088 libmwaw::DebugStream f;
1089 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1090 f << "Entries(DocHeader):";
1091
1092 int val;
1093 if (vers >= 6) {
1094 f << "unkn=[";
1095 for (int i = 0; i < 4; i++) {
1096 val = static_cast<int>(input->readLong(1));
1097 if (val) f << val << ", ";
1098 else f << "_, ";
1099 }
1100 f << "],";
1101 for (int i = 0; i < 4; i++) {
1102 val = static_cast<int>(input->readLong(2));
1103 if (val) f << "e" << i << "=" << val << ",";
1104 }
1105 }
1106 long pos = input->tell();
1107 int zone0Length = 52, zone1Length=0;
1108 switch (vers) {
1109 case 1:
1110 zone0Length = 114;
1111 zone1Length=50;
1112 break;
1113 case 2:
1114 case 3: // checkme: never see a v3 file
1115 zone0Length = 116;
1116 zone1Length=112;
1117 break;
1118 case 4:
1119 zone0Length = 120;
1120 zone1Length=92;
1121 break;
1122 case 5:
1123 zone0Length = 132;
1124 zone1Length = 92;
1125 break;
1126 case 6:
1127 zone0Length = 124;
1128 zone1Length = 1126;
1129 break;
1130 default:
1131 break;
1132 }
1133 int totalLength = zone0Length+zone1Length;
1134
1135 input->seek(totalLength, librevenge::RVNG_SEEK_CUR);
1136 if (input->tell() != pos+totalLength) {
1137 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocHeader: file is too short\n"));
1138 return false;
1139 }
1140 input->seek(pos, librevenge::RVNG_SEEK_SET);
1141 val = static_cast<int>(input->readLong(2)); // always find 1
1142 if (val != 1)
1143 f << "#unkn=" << std::hex << val << std::dec << ",";
1144 for (int i = 0; i < 4; i++) {
1145 val = static_cast<int>(input->readULong(2));
1146 if (val)
1147 f << std::hex << "f" << i << "=" << std::hex << val << std::dec << ",";
1148 }
1149 int dim[2];
1150 for (auto &d : dim) d = static_cast<int>(input->readLong(2));
1151 f << "dim?=" << dim[1] << "x" << dim[0] << ",";
1152 int margin[6];
1153 f << "margin?=[";
1154 for (auto &marg : margin) {
1155 marg = static_cast<int>(input->readLong(2));
1156 f << marg << ",";
1157 }
1158 f << "],";
1159 if (dim[0] > 0 && dim[1] > 0 &&
1160 margin[0] >= 0 && margin[1] >= 0 && margin[2] >= 0 && margin[3] >= 0 &&
1161 dim[0] > margin[0]+margin[2] && dim[1] > margin[1]+margin[3]) {
1162
1163 MWAWVec2i paperSize(dim[1],dim[0]);
1164 MWAWVec2i lTopMargin(margin[1], margin[0]);
1165 MWAWVec2i rBotMargin(margin[3], margin[2]);
1166
1167 m_parser->getPageSpan().setMarginTop(lTopMargin.y()/72.0);
1168 m_parser->getPageSpan().setMarginBottom(rBotMargin.y()/72.0);
1169 m_parser->getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
1170 m_parser->getPageSpan().setMarginRight(rBotMargin.x()/72.0);
1171 m_parser->getPageSpan().setFormLength(paperSize.y()/72.);
1172 m_parser->getPageSpan().setFormWidth(paperSize.x()/72.);
1173 m_state->m_pageSpanSet = true;
1174 }
1175 int dim2[2];
1176 for (auto &d : dim2) d= static_cast<int>(input->readLong(2));
1177 f << "dim2?=" << dim2[1] << "x" << dim2[0] << ",";
1178 int fl[4];
1179 f << "fl?=[";
1180 for (int &i : fl) {
1181 i = static_cast<int>(input->readULong(1));
1182 if (i)
1183 f << i << ",";
1184 else
1185 f << "_,";
1186 }
1187 f << "],";
1188 for (int i = 0; i < 9; i++) {
1189 val = static_cast<int>(input->readLong(2));
1190 if (val)
1191 f << "g" << i << "=" << val << ",";
1192 }
1193
1194 if (long(input->tell()) != pos+zone0Length)
1195 ascFile.addDelimiter(input->tell(), '|');
1196 input->seek(pos+zone0Length, librevenge::RVNG_SEEK_SET);
1197 ascFile.addPos(debPos);
1198 ascFile.addNote(f.str().c_str());
1199
1200 /* zone 1 actual font, actul pos, .. */
1201 if (!getTextParser()->readParagraph())
1202 return false;
1203 pos = input->tell();
1204 f.str("");
1205 f << "DocHeader:zone?=" << input->readULong(2) << ",";
1206 if (vers >= 4) f << "unkn=" << input->readULong(2) << ",";
1207 ascFile.addPos(pos);
1208 ascFile.addNote(f.str().c_str());
1209 MWAWFont font;
1210 int posChar;
1211 if (!getStyleManager()->readFontAndPos(-1, posChar, font))
1212 return false;
1213
1214 /* zone 2, type, unknown */
1215 pos = input->tell();
1216 f.str("");
1217 f << "DocHeader-1:";
1218 for (int i = 0; i < 6; i++) {
1219 val = static_cast<int>(input->readULong(2));
1220 if (val) f << "f" << i << "=" << val << ",";
1221 }
1222 input->seek(4, librevenge::RVNG_SEEK_CUR);
1223 auto type = static_cast<int>(input->readULong(1));
1224 f << "type=" << type << ",";
1225 val = static_cast<int>(input->readULong(1));
1226 if (type != val) {
1227 if (val >= 0 && val < 7) {
1228 static char const *wh[]= { "draw", "text", "spreadsheet", "database", "paint", "presentation", "table" };
1229 f << "type[display]=" << wh[val] << ",";
1230 }
1231 else {
1232 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocHeader: find unknown display type\n"));
1233 f << "##unkn=" << val << ",";
1234 }
1235 }
1236 ascFile.addPos(pos);
1237 ascFile.addNote(f.str().c_str());
1238 if (vers <= 2) {
1239 // the document font ?
1240 if (!getStyleManager()->readFontAndPos(-1, posChar, font))
1241 return false;
1242 ascFile.addPos(input->tell());
1243 ascFile.addNote("DocHeader-2");
1244 if (vers==2) {
1245 input->seek(46, librevenge::RVNG_SEEK_CUR);
1246 long actPos = input->tell();
1247 f.str("");
1248 f << "DocHeader(Col):";
1249 auto numCols = static_cast<int>(input->readLong(2));
1250 if (numCols < 1 || numCols > 9) {
1251 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocHeader: pb reading number of columns\n"));
1252 f << "###numCols=" << numCols;
1253 numCols = 1;
1254 }
1255 if (numCols != 1)
1256 f << "numCols=" << numCols << ",";
1257 m_state->m_columns = numCols;
1258 f << "colsW=[";
1259 for (int i = 0; i < numCols; i++) {
1260 val = static_cast<int>(input->readULong(2));
1261 m_state->m_columnsWidth.push_back(val);
1262 f << val << ",";
1263 }
1264 f << "],";
1265 input->seek(actPos+20, librevenge::RVNG_SEEK_SET);
1266 if (numCols > 1) {
1267 f << "colsS=[";
1268 for (int i = 0; i < numCols-1; i++) {
1269 val = static_cast<int>(input->readULong(2));
1270 m_state->m_columnsSep.push_back(val);
1271 f << input->readULong(2) << ",";
1272 }
1273 f << "],";
1274 }
1275 input->seek(actPos+36, librevenge::RVNG_SEEK_SET);
1276 val = static_cast<int>(input->readLong(2));
1277 if (val) f << "unkn=" << val << ",";
1278 ascFile.addPos(actPos);
1279 ascFile.addNote(f.str().c_str());
1280 }
1281 }
1282 else if (long(input->tell()) != pos+zone1Length)
1283 ascFile.addDelimiter(input->tell(), '|');
1284 input->seek(pos+zone1Length, librevenge::RVNG_SEEK_SET);
1285 if (input->isEnd()) {
1286 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocHeader: file is too short\n"));
1287 return false;
1288 }
1289 switch (vers) {
1290 case 1:
1291 case 2: {
1292 pos = input->tell();
1293 if (!getTextParser()->readParagraphs()) {
1294 input->seek(pos, librevenge::RVNG_SEEK_SET);
1295 return false;
1296 }
1297 pos = input->tell();
1298 if (!readPrintInfo()) {
1299 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocHeader: can not find print info\n"));
1300 input->seek(pos, librevenge::RVNG_SEEK_SET);
1301 return false;
1302 }
1303 if (vers==1)
1304 break;
1305 pos = input->tell();
1306 if (!m_styleManager->readPatternList() ||
1307 !m_styleManager->readGradientList()) {
1308 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
1309 return false;
1310 }
1311 pos=input->tell();
1312 f.str("");
1313 f << "Entries(DocUnkn0):";
1314 auto sz=long(input->readULong(4));
1315 if (!sz) {
1316 ascFile.addPos(pos);
1317 ascFile.addNote("_");
1318 }
1319 else if (input->checkPosition(pos+sz+4)) {
1320 // find one time with size 0x400 but does not look like a classic struct
1321 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
1322 ascFile.addPos(pos);
1323 ascFile.addNote(f.str().c_str());
1324 }
1325 else {
1326 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocHeader: oops find bad size for DocUnkn0, we may have a problem\n"));
1327 f << sz << "###";
1328 ascFile.addPos(pos);
1329 ascFile.addNote(f.str().c_str());
1330 return false;
1331 }
1332 pos=input->tell();
1333 if (!ClarisWksStruct::readStructZone(*m_parserState, "DocUnkn1", false)) { // related to link/filename?
1334 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1335 return false;
1336 }
1337 pos=input->tell();
1338 ascFile.addPos(pos);
1339 ascFile.addNote("Entries(DocUnkn2)"); // another struct ?
1340 input->seek(4, librevenge::RVNG_SEEK_CUR);
1341 if (!ClarisWksStruct::readStructZone(*m_parserState, "DocH0", false)) {
1342 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1343 return false;
1344 }
1345 pos=input->tell();
1346 if (!readZoneA()) { // related to struct ?
1347 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1348 return false;
1349 }
1350 break;
1351 }
1352 case 4:
1353 case 5:
1354 case 6: {
1355 pos = input->tell();
1356 MWAWEntry entry;
1357 entry.setBegin(pos);
1358 entry.setLength(6*260);
1359 if (!readDSUM(entry, true))
1360 return false;
1361 pos = input->tell();
1362 auto sz = long(input->readULong(4));
1363 if (!sz) {
1364 ascFile.addPos(pos);
1365 ascFile.addNote("Nop");
1366 }
1367 else {
1368 long endPos = pos+4+sz;
1369 if (!input->checkPosition(endPos)) {
1370 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocHeader: unexpected LinkInfo size\n"));
1371 return false;
1372 }
1373 ascFile.addPos(pos);
1374 ascFile.addNote("Entries(LinkInfo)");
1375 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1376 }
1377
1378 if (vers > 4) {
1379 if (!getGraphParser()->readDatabasePictZone() || !readDataLinkList())
1380 return false;
1381 }
1382
1383 pos = input->tell(); // series of data with size 42 or 46
1384 int expectedSize = 0;
1385 switch (vers) {
1386 case 5:
1387 expectedSize=34;
1388 break;
1389 case 6:
1390 expectedSize=32;
1391 break;
1392 default:
1393 break;
1394 }
1395 if (expectedSize) {
1396 ascFile.addPos(pos);
1397 ascFile.addNote("DocHeader-3");
1398 input->seek(pos+expectedSize, librevenge::RVNG_SEEK_SET);
1399 }
1400
1401 if (!readPrintInfo()) {
1402 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocHeader: can not find print info\n"));
1403 input->seek(pos, librevenge::RVNG_SEEK_SET);
1404 return false;
1405 }
1406
1407 for (int z = 0; z < 4; z++) { // zone0, zone1 : color palette, zone2 (val:2, id:2)
1408 if (z==3 && vers!=4) break;
1409 pos = input->tell();
1410 sz = long(input->readULong(4));
1411 if (!sz) {
1412 ascFile.addPos(pos);
1413 ascFile.addNote("Nop");
1414 continue;
1415 }
1416 entry.setBegin(pos);
1417 entry.setLength(4+sz);
1418 if (!input->checkPosition(entry.end())) {
1419 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocHeader: can not read final zones\n"));
1420 return false;
1421 }
1422 input->seek(pos, librevenge::RVNG_SEEK_SET);
1423 switch (z) {
1424 case 0:
1425 ascFile.addPos(pos);
1426 ascFile.addNote("DocUnkn2");
1427 break;
1428 case 1:
1429 if (!m_styleManager->readColorList(entry)) {
1430 input->seek(pos, librevenge::RVNG_SEEK_SET);
1431 return false;
1432 }
1433 break;
1434 case 2: // a serie of id? num
1435 if (!ClarisWksStruct::readStructZone(*m_parserState, "DocH0", false)) {
1436 input->seek(pos, librevenge::RVNG_SEEK_SET);
1437 return false;
1438 }
1439 break;
1440 case 3: // checkme
1441 ascFile.addPos(pos);
1442 ascFile.addNote("DocUnkn3");
1443 break;
1444 default:
1445 break;
1446 }
1447 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1448 }
1449 break;
1450 }
1451 default:
1452 break;
1453 }
1454 return true;
1455 }
1456
1457 ////////////////////////////////////////////////////////////
1458 // the end zone (in some v2 file and after )
1459 ////////////////////////////////////////////////////////////
1460 bool ClarisWksDocument::readEndTable(long &eof)
1461 {
1462 MWAWInputStreamPtr input = m_parserState->m_input;
1463 input->seek(0, librevenge::RVNG_SEEK_END);
1464 eof = input->tell();
1465 if (m_parserState->m_version<=1) return false;
1466 if (eof < 20) // this is too short
1467 return false;
1468 input->seek(-20, librevenge::RVNG_SEEK_CUR);
1469
1470 auto entryPos= long(input->readULong(4));
1471 if (entryPos >= eof-20)
1472 return false;
1473
1474 input->seek(entryPos, librevenge::RVNG_SEEK_SET);
1475 if (input->readULong(4) != 0x4554424c)
1476 return false;
1477
1478 auto sz = long(input->readULong(4));
1479 if (sz <= 16 || (sz%8) != 0 || sz+entryPos+8 != eof) {
1480 MWAW_DEBUG_MSG(("ClarisWksDocument::readEndTable: bad size\n"));
1481 return false;
1482 }
1483
1484 auto numEntries = int((sz-16)/8);
1485 libmwaw::DebugStream f;
1486 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1487 f << "Entries(ETBL):";
1488 long prevPos = 0;
1489 std::vector<MWAWEntry> listEntries;
1490 MWAWEntry lastEntry;
1491 for (int i = 0; i < numEntries; i++) {
1492 std::string name("");
1493 for (int j = 0; j < 4; j++)
1494 name+=char(input->readULong(1));
1495 auto pos = long(input->readULong(4));
1496 if (pos < prevPos+4 || (i!=numEntries-1 && pos+4 > entryPos)) {
1497 MWAW_DEBUG_MSG(("ClarisWksDocument::readEndTable: bad pos\n"));
1498 return false;
1499 }
1500
1501 lastEntry.setEnd(pos);
1502 if (i)
1503 listEntries.push_back(lastEntry);
1504 lastEntry.setType(name);
1505 lastEntry.setBegin(pos);
1506
1507 f << "[" << name << ":" << std::hex << pos << std::dec << "],";
1508 prevPos = pos;
1509 }
1510 lastEntry.setEnd(eof);
1511 listEntries.push_back(lastEntry);
1512
1513 ascFile.addPos(entryPos);
1514 ascFile.addNote(f.str().c_str());
1515
1516 for (int i = 0; i < numEntries-1; i++) {
1517 MWAWEntry const &entry = listEntries[size_t(i)];
1518 bool parsed = false;
1519 if (entry.type() == "CPRT") {
1520 readCPRT(entry);
1521 parsed = true;
1522 }
1523 else if (entry.type() == "SNAP") {
1524 readSNAP(entry);
1525 parsed = true;
1526 }
1527 else if (entry.type() == "STYL") {
1528 m_styleManager->readStyles(entry);
1529 parsed = true;
1530 }
1531 else if (entry.type() == "DSUM") {
1532 readDSUM(entry, false);
1533 parsed = true;
1534 }
1535 else if (entry.type() == "TNAM") {
1536 readTNAM(entry);
1537 parsed = true;
1538 }
1539 else if (entry.type() == "MARK") {
1540 readMARKList(entry);
1541 parsed = true;
1542 }
1543 else if (entry.type() == "FNTM") {
1544 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1545 m_styleManager->readFontNames();
1546 parsed = true;
1547 }
1548
1549 // WMBT: crypt password ? 0|fieldSz + PString ?
1550 if (parsed && input->tell() == entry.end()) continue;
1551 f.str("");
1552 f << "Entries(" << entry.type() << ")";
1553 if (parsed) f << "*";
1554 ascFile.addPos(entry.begin());
1555 ascFile.addNote(f.str().c_str());
1556 }
1557
1558 if (numEntries)
1559 eof = listEntries[0].begin();
1560 return true;
1561 }
1562
1563 ////////////////////////////////////////////////////////////
1564 // a list of print info plist
1565 ////////////////////////////////////////////////////////////
1566 bool ClarisWksDocument::readCPRT(MWAWEntry const &entry)
1567 {
1568 if (!entry.valid() || entry.type() != "CPRT")
1569 return false;
1570 MWAWInputStreamPtr input = m_parserState->m_input;
1571 long pos = entry.begin();
1572 input->seek(pos+4, librevenge::RVNG_SEEK_SET); // skip header
1573 auto sz = long(input->readULong(4));
1574 if (sz > entry.length()) {
1575 MWAW_DEBUG_MSG(("ClarisWksDocument::readCPRT: pb with entry length"));
1576 input->seek(pos, librevenge::RVNG_SEEK_SET);
1577 return false;
1578 }
1579
1580 libmwaw::DebugStream f;
1581 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1582 f << "Entries(CPRT)";
1583 ascFile.addPos(pos);
1584 ascFile.addNote(f.str().c_str());
1585 int id = 0;
1586 while (long(input->tell()) < entry.end()) {
1587 pos = input->tell();
1588 sz = long(input->readULong(4));
1589 if (pos+sz > entry.end()) {
1590 MWAW_DEBUG_MSG(("ClarisWksDocument::readCPRT: pb with sub zone: %d", id));
1591 input->seek(pos, librevenge::RVNG_SEEK_SET);
1592 return false;
1593 }
1594 f.str("");
1595 f << "CPRT-" << id++ << ":";
1596 ascFile.addPos(pos);
1597 ascFile.addNote(f.str().c_str());
1598 if (!sz) continue;
1599 #ifdef DEBUG_WITH_FILES
1600 librevenge::RVNGBinaryData file;
1601 input->readDataBlock(sz, file);
1602
1603 static int volatile cprtName = 0;
1604 f.str("");
1605 f << "CPRT" << ++cprtName << ".plist";
1606 libmwaw::Debug::dumpFile(file, f.str().c_str());
1607
1608 ascFile.skipZone(pos+4,pos+4+sz-1);
1609 #endif
1610 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
1611 }
1612
1613 return true;
1614 }
1615
1616 ////////////////////////////////////////////////////////////
1617 // read the mark
1618 ////////////////////////////////////////////////////////////
1619 bool ClarisWksDocument::readMARKList(MWAWEntry const &entry)
1620 {
1621 if (!entry.valid() || entry.type() != "MARK")
1622 return false;
1623 int const vers=m_parserState->m_version;
1624 MWAWInputStreamPtr input = m_parserState->m_input;
1625 long pos = entry.begin();
1626 long sz = entry.length()-8;
1627 input->seek(pos, librevenge::RVNG_SEEK_SET);
1628 libmwaw::DebugStream f;
1629 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1630 f << "Entries(MARK)[header]:";
1631
1632 if (input->readULong(4) !=0x4d41524b || input->readLong(4) != sz || sz < 30) {
1633 f << "###";
1634 MWAW_DEBUG_MSG(("ClarisWksDocument::readMARKList: find unexpected header\n"));
1635 ascFile.addPos(entry.begin());
1636 ascFile.addNote(f.str().c_str());
1637
1638 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1639 return false;
1640 }
1641 ascFile.addPos(pos);
1642 ascFile.addNote(f.str().c_str());
1643
1644 pos=input->tell();
1645 f.str("");
1646 f << "MARK[MRKS]:";
1647 if (input->readULong(4)!=0x4d524b53) { // MRKS
1648 f << "###";
1649
1650 MWAW_DEBUG_MSG(("ClarisWksDocument::readMARKList: find unexpected MRKS header\n"));
1651 ascFile.addPos(pos);
1652 ascFile.addNote(f.str().c_str());
1653
1654 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1655 return false;
1656 }
1657 auto val=static_cast<int>(input->readLong(2));
1658 if (val != 3)
1659 f << "f0=" << val << ",";
1660 auto N=static_cast<int>(input->readLong(2));
1661 if (N) f << "N=" << N << ",";
1662 for (int i=0; i<2; ++i) {
1663 val=static_cast<int>(input->readLong(2));
1664 if (val)
1665 f << "f" << i << "=" << val << ",";
1666 }
1667 ascFile.addDelimiter(input->tell(),'|');
1668 ascFile.addPos(pos);
1669 ascFile.addNote(f.str().c_str());
1670 input->seek(pos+30, librevenge::RVNG_SEEK_SET);
1671
1672 for (int m=0; m < N; ++m) {
1673 pos = input->tell();
1674 if (pos+14>entry.end() || input->readULong(4)!=0x4d41524b) { // MARK
1675 input->seek(pos, librevenge::RVNG_SEEK_SET);
1676 break;
1677 }
1678 f.str("");
1679 f << "MARK-" << m << ":";
1680 val = static_cast<int>(input->readLong(2));
1681 if (val != 3)
1682 f << "f0=" << val << ",";
1683 auto N1=static_cast<int>(input->readLong(2));
1684 f << "N1=" << N1 << ",";
1685
1686 std::string name(""); // can be: Book (anchor), LDOC (link in doc), LURL
1687
1688 for (int i=0; i<4; i++) {
1689 auto c=char(input->readLong(1));
1690 if ((c>='a' && c<='z') || (c>='A' && c<='Z'))
1691 name += c;
1692 }
1693 if (name.size()!=4) {
1694 input->seek(pos, librevenge::RVNG_SEEK_SET);
1695 break;
1696 }
1697 int what = name=="Book"? 0 : name=="LDOC" ? 1 : name=="LURL" ? 2 : -1;
1698 if (what==-1) {
1699 input->seek(pos, librevenge::RVNG_SEEK_SET);
1700 break;
1701 }
1702 f << name << ",";
1703 if (vers < 6) {
1704 // I think mark in v5, but the code seem to differ from here
1705 MWAW_DEBUG_MSG(("ClarisWksDocument::readMARKList: OOOPS reading mark data is not implemented\n"));
1706 ascFile.addPos(pos);
1707 ascFile.addNote(f.str().c_str());
1708 ascFile.addPos(input->tell());
1709 ascFile.addNote("MARK[End]:###");
1710 return false;
1711 }
1712 f << "f1=" << std::hex << input->readULong(2) << std::dec << ",";
1713 ascFile.addPos(pos);
1714 ascFile.addNote(f.str().c_str());
1715
1716 bool ok=true;
1717 for (int n=0; n < N1; ++n) {
1718 pos=input->tell();
1719 if (pos+54+8>entry.end()) {
1720 ok=false;
1721 break;
1722 }
1723 f.str("");
1724 f << "MARK-" << m << "." << n << ":";
1725 if (input->readLong(2)!=-1 || input->readLong(2)) {
1726 ok=false;
1727 break;
1728 }
1729 for (int i=0; i < 9; ++i) { // f6:an id?,
1730 val=static_cast<int>(input->readULong(2));
1731 if (val)
1732 f << "f" << i << "=" << std::hex << val << std::dec << ",";
1733 }
1734 auto tSz=static_cast<int>(input->readULong(1));
1735 if (tSz <= 0 || tSz >=32) {
1736 ok=false;
1737 break;
1738 }
1739 std::string text("");
1740 for (int s=0; s < tSz; ++s)
1741 text+=char(input->readLong(1));
1742 f << text << ",";
1743 ascFile.addPos(pos);
1744 ascFile.addNote(f.str().c_str());
1745
1746 input->seek(pos+54, librevenge::RVNG_SEEK_SET);
1747 pos=input->tell();
1748 switch (what) {
1749 case 0:
1750 ok=readBookmark(entry.end());
1751 break;
1752 case 1:
1753 ok=readDocumentMark(entry.end());
1754 break;
1755 case 2:
1756 ok=readURL(entry.end());
1757 break;
1758 default:
1759 break;
1760 }
1761 if (!ok)
1762 break;
1763 }
1764 if (!ok) {
1765 input->seek(pos, librevenge::RVNG_SEEK_SET);
1766 break;
1767 }
1768 }
1769
1770 pos = input->tell();
1771 if (pos==entry.end())
1772 return true;
1773 f.str("");
1774 f << "###MARK-end:";
1775
1776 ascFile.addPos(pos);
1777 ascFile.addNote(f.str().c_str());
1778 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1779 return true;
1780 }
1781
1782 bool ClarisWksDocument::readURL(long endPos)
1783 {
1784 MWAWInputStreamPtr input = m_parserState->m_input;
1785 long pos=input->tell();
1786 if (pos+8>endPos) {
1787 return false;
1788 }
1789 libmwaw::DebugStream f;
1790 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1791 f << "MARK-URL:";
1792 auto type=long(input->readULong(4));
1793 if (type==0) {
1794 }
1795 else if (type!=0x554c6b64) {
1796 MWAW_DEBUG_MSG(("ClarisWksDocument::readURL: find unexpected header\n"));
1797 f << "###";
1798 ascFile.addPos(pos);
1799 ascFile.addNote(f.str().c_str());
1800 input->seek(pos, librevenge::RVNG_SEEK_SET);
1801 return false;
1802 }
1803 else { // ULkd
1804 if (input->tell()+32+256+8>endPos) {
1805 MWAW_DEBUG_MSG(("ClarisWksDocument::readURL: date seems too short\n"));
1806 f << "###";
1807 ascFile.addPos(pos);
1808 ascFile.addNote(f.str().c_str());
1809 input->seek(pos, librevenge::RVNG_SEEK_SET);
1810 return false;
1811 }
1812 for (int s=0; s < 2; ++s) {
1813 int const maxSize = s==0 ? 32: 256;
1814 long actPos=input->tell();
1815 auto tSz=static_cast<int>(input->readULong(1));
1816 if (tSz >= maxSize) {
1817 MWAW_DEBUG_MSG(("ClarisWksDocument::readURL: find unexpected text size\n"));
1818 f << "###";
1819 input->seek(pos, librevenge::RVNG_SEEK_SET);
1820 ascFile.addPos(pos);
1821 ascFile.addNote(f.str().c_str());
1822 return false;
1823 }
1824 std::string text("");
1825 for (int c=0; c < tSz; ++c)
1826 text+=char(input->readLong(1));
1827 f << text << ",";
1828 input->seek(actPos+maxSize, librevenge::RVNG_SEEK_SET);
1829 }
1830 }
1831 ascFile.addPos(pos);
1832 ascFile.addNote(f.str().c_str());
1833
1834 return readEndMark(endPos);
1835 }
1836
1837 bool ClarisWksDocument::readDocumentMark(long endPos)
1838 {
1839 // Checkme...
1840 MWAWInputStreamPtr input = m_parserState->m_input;
1841 long pos=input->tell();
1842 if (pos+8>endPos) {
1843 return false;
1844 }
1845 libmwaw::DebugStream f;
1846 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1847 f << "MARK-Document:";
1848 auto type=long(input->readULong(4));
1849 if (type==0) {
1850 }
1851 else if (type!=0x444c6b64) {
1852 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocumentMark: find unexpected header\n"));
1853 f << "###";
1854 ascFile.addPos(pos);
1855 ascFile.addNote(f.str().c_str());
1856 input->seek(pos, librevenge::RVNG_SEEK_SET);
1857 return false;
1858 }
1859 else { // DLkd
1860 if (input->tell()+32+64+20+8>endPos) {
1861 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocumentMark: date seems too short\n"));
1862 f << "###";
1863 ascFile.addPos(pos);
1864 ascFile.addNote(f.str().c_str());
1865 input->seek(pos, librevenge::RVNG_SEEK_SET);
1866 return false;
1867 }
1868 for (int s=0; s < 2; ++s) {
1869 int const maxSize = s==0 ? 32: 64;
1870 long actPos=input->tell();
1871 auto tSz=static_cast<int>(input->readULong(1));
1872 if (tSz >= maxSize) {
1873 MWAW_DEBUG_MSG(("ClarisWksDocument::readDocumentMark: find unexpected text size\n"));
1874 f << "###";
1875 input->seek(pos, librevenge::RVNG_SEEK_SET);
1876 ascFile.addPos(pos);
1877 ascFile.addNote(f.str().c_str());
1878 return false;
1879 }
1880 std::string text("");
1881 for (int c=0; c < tSz; ++c)
1882 text+=char(input->readLong(1));
1883 f << text << ",";
1884 input->seek(actPos+maxSize, librevenge::RVNG_SEEK_SET);
1885 }
1886 }
1887 for (int i=0; i < 10; ++i) { // f7=f9=id ?, other 0
1888 auto val=static_cast<int>(input->readULong(2));
1889 if (val)
1890 f << "f" << i << "=" << std::hex << val << std::dec << ",";
1891 }
1892 ascFile.addPos(pos);
1893 ascFile.addNote(f.str().c_str());
1894
1895 return readEndMark(endPos);
1896 }
1897
1898 bool ClarisWksDocument::readBookmark(long endPos)
1899 {
1900 MWAWInputStreamPtr input = m_parserState->m_input;
1901 long pos=input->tell();
1902 if (pos+8>endPos) {
1903 return false;
1904 }
1905 libmwaw::DebugStream f;
1906 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1907 f << "MARK-URL:";
1908 auto type=long(input->readULong(4));
1909 if (type==0) {
1910 }
1911 else if (type!=0x424d6b64) {
1912 MWAW_DEBUG_MSG(("ClarisWksDocument::readBookmark: find unexpected header\n"));
1913 f << "###";
1914 ascFile.addPos(pos);
1915 ascFile.addNote(f.str().c_str());
1916 input->seek(pos, librevenge::RVNG_SEEK_SET);
1917 return false;
1918 }
1919 else { // BMkd
1920 if (input->tell()+32+8>endPos) {
1921 MWAW_DEBUG_MSG(("ClarisWksDocument::readBookmark: date seems too short\n"));
1922 f << "###";
1923 ascFile.addPos(pos);
1924 ascFile.addNote(f.str().c_str());
1925 input->seek(pos, librevenge::RVNG_SEEK_SET);
1926 return false;
1927 }
1928 int const maxSize = 32;
1929 long actPos=input->tell();
1930 auto tSz=static_cast<int>(input->readULong(1));
1931 if (tSz >= maxSize) {
1932 MWAW_DEBUG_MSG(("ClarisWksDocument::readBookmark: find unexpected text size\n"));
1933 f << "###";
1934 input->seek(pos, librevenge::RVNG_SEEK_SET);
1935 ascFile.addPos(pos);
1936 ascFile.addNote(f.str().c_str());
1937 return false;
1938 }
1939 std::string text("");
1940 for (int c=0; c < tSz; ++c)
1941 text+=char(input->readLong(1));
1942 f << text << ",";
1943 input->seek(actPos+maxSize, librevenge::RVNG_SEEK_SET);
1944 }
1945 ascFile.addPos(pos);
1946 ascFile.addNote(f.str().c_str());
1947
1948 return readEndMark(endPos);
1949 }
1950
1951 bool ClarisWksDocument::readEndMark(long endPos)
1952 {
1953 MWAWInputStreamPtr input = m_parserState->m_input;
1954 long pos = input->tell();
1955 libmwaw::DebugStream f;
1956 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1957 f << "MARK[Last]:";
1958 long val=input->readLong(4);
1959 if (!val) {
1960 ascFile.addPos(pos);
1961 ascFile.addNote(f.str().c_str());
1962 return true;
1963 }
1964 f << "f0=" << std::hex << val << std::dec << ",";
1965 f << "f1=" << std::hex << input->readULong(4) << std::dec << ",";
1966 for (int i=0; i < 2; ++i) { // g0=1|2|3, g1=0
1967 val=static_cast<int>(input->readLong(2));
1968 if (val) f << "g" << i << "=" << val << ",";
1969 }
1970 val=static_cast<int>(input->readLong(2));
1971 f << "type=" << val << ",";
1972 int numExpected=val==1 ? 4: 1;
1973 if (input->tell()+2*numExpected >endPos) {
1974 MWAW_DEBUG_MSG(("ClarisWksDocument::readEndMark: find unexpected number of element\n"));
1975 f << "###";
1976 input->seek(pos, librevenge::RVNG_SEEK_SET);
1977 ascFile.addPos(pos);
1978 ascFile.addNote(f.str().c_str());
1979 return false;
1980 }
1981 f << "unkn=[";
1982 for (int i=0; i<numExpected; ++i)
1983 f << input->readLong(2) << ",";
1984 f << "],";
1985 ascFile.addPos(pos);
1986 ascFile.addNote(f.str().c_str());
1987 return true;
1988 }
1989
1990 ////////////////////////////////////////////////////////////
1991 // a string: temporary file name ?
1992 ////////////////////////////////////////////////////////////
1993 bool ClarisWksDocument::readTNAM(MWAWEntry const &entry)
1994 {
1995 MWAWInputStreamPtr input = m_parserState->m_input;
1996 if (!entry.valid() || entry.type() != "TNAM" || !input->checkPosition(entry.end()))
1997 return false;
1998 long pos = entry.begin();
1999 long sz = entry.length()-8;
2000 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
2001
2002 libmwaw::DebugStream f;
2003 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
2004 f << "Entries(TNAM):";
2005
2006 auto strSize = static_cast<int>(input->readULong(1));
2007 if (strSize != sz-1 || pos+8+sz > entry.end()) {
2008 MWAW_DEBUG_MSG(("ClarisWksDocument::readTNAM: unexpected string size\n"));
2009 input->seek(pos, librevenge::RVNG_SEEK_SET);
2010 return false;
2011 }
2012 std::string name("");
2013 for (int i = 0; i < strSize; i++) {
2014 auto c = char(input->readULong(1));
2015 if (c) {
2016 name += c;
2017 continue;
2018 }
2019 MWAW_DEBUG_MSG(("ClarisWksDocument::readTNAM: unexpected string char\n"));
2020 input->seek(pos, librevenge::RVNG_SEEK_SET);
2021 return false;
2022 }
2023 if (name.length())
2024 f << name << ",";
2025 if (long(input->tell()) != entry.end()) {
2026 ascFile.addDelimiter(input->tell(),'|');
2027 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
2028 }
2029
2030 ascFile.addPos(entry.begin());
2031 ascFile.addNote(f.str().c_str());
2032 return true;
2033 }
2034
2035 ////////////////////////////////////////////////////////////
2036 // a list the document property
2037 ////////////////////////////////////////////////////////////
2038 bool ClarisWksDocument::readDSUM(MWAWEntry const &entry, bool inHeader)
2039 {
2040 if (!entry.valid() || (!inHeader && entry.type() != "DSUM"))
2041 return false;
2042 MWAWInputStreamPtr input = m_parserState->m_input;
2043 long pos = entry.begin();
2044 long debStrings = inHeader ? pos : pos+8;
2045 input->seek(debStrings, librevenge::RVNG_SEEK_SET);
2046
2047 libmwaw::DebugStream f;
2048 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
2049 f << "Entries(DSUM):";
2050 for (int entete = 0; entete < 6; entete++) {
2051 pos = input->tell();
2052 long sz = static_cast<int>(input->readULong(4));
2053 if (!sz) continue;
2054 auto strSize = static_cast<int>(input->readULong(1));
2055 if (strSize != sz-1 || pos+4+sz > entry.end()) {
2056 MWAW_DEBUG_MSG(("ClarisWksDocument::readDSUM: unexpected string size\n"));
2057 if (pos+4+sz > entry.end() || strSize > sz-1) {
2058 input->seek(pos, librevenge::RVNG_SEEK_SET);
2059 return false;
2060 }
2061 f << "###";
2062 }
2063 librevenge::RVNGString text("");
2064 for (int i = 0; i < strSize; i++) {
2065 auto ch = static_cast<unsigned char>(input->readULong(1));
2066 if (ch) {
2067 int unicode= m_parserState->m_fontConverter->unicode(3, static_cast<unsigned char>(ch));
2068 if (unicode!=-1)
2069 libmwaw::appendUnicode(static_cast<uint32_t>(unicode), text);
2070 else if (ch>0x1f || ch==0x9)
2071 text.append(char(ch));
2072 continue;
2073 }
2074 MWAW_DEBUG_MSG(("ClarisWksDocument::readDSUM: unexpected string char\n"));
2075 input->seek(pos, librevenge::RVNG_SEEK_SET);
2076 return false;
2077 }
2078 if (!text.empty()) {
2079 char const *entryNames[] = { "Title", "Category", "Description", "Author", "Version", "Keywords"};
2080 char const *metaNames[] = { "dc:title", "librevenge:category", "dc:description", "dc:creator", "librevenge:version", "meta:keywords"};
2081 f << entryNames[entete] << "=" << text.cstr() << ",";
2082 m_state->m_metaData.insert(metaNames[entete], text);
2083 }
2084 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
2085 }
2086
2087 ascFile.addPos(entry.begin());
2088 ascFile.addNote(f.str().c_str());
2089 return true;
2090 }
2091
2092 ////////////////////////////////////////////////////////////
2093 // a generic list of strings
2094 ////////////////////////////////////////////////////////////
2095 bool ClarisWksDocument::readStringList(char const *zoneName, bool hasEntete, std::vector<std::string> &res)
2096 {
2097 MWAWInputStreamPtr input = m_parserState->m_input;
2098 long pos = input->tell();
2099 auto sz=long(input->readULong(4));
2100 long endPos=pos+4+sz;
2101 if (!input->checkPosition(endPos)) {
2102 MWAW_DEBUG_MSG(("ClarisWksDocument::readStringList: the zone size seems bad\n"));
2103 input->seek(pos, librevenge::RVNG_SEEK_SET);
2104 return false;
2105 }
2106 libmwaw::DebugStream f;
2107 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
2108 if (sz==0) {
2109 ascFile.addPos(pos-(hasEntete?4:0));
2110 ascFile.addNote("_");
2111 return true;
2112 }
2113 f << "Entries(" << zoneName << "):strings=[";
2114 while (!input->isEnd() && input->tell()<endPos) {
2115 long actPos = input->tell();
2116 auto strSize = static_cast<int>(input->readULong(1));
2117 std::string name("");
2118 if (!strSize) {
2119 res.push_back(name);
2120 f << "\"\",";
2121 continue;
2122 }
2123 if (actPos+1+strSize>endPos) {
2124 MWAW_DEBUG_MSG(("ClarisWksDocument::readStringList: unexpected string size\n"));
2125 input->seek(pos, librevenge::RVNG_SEEK_SET);
2126 return false;
2127 }
2128 for (int i = 0; i < strSize; i++) {
2129 auto c = char(input->readULong(1));
2130 if (c) {
2131 name += c;
2132 continue;
2133 }
2134 MWAW_DEBUG_MSG(("ClarisWksDocument::readStringList: unexpected string char\n"));
2135 f << "#[0]";
2136 }
2137 res.push_back(name);
2138 f << "\"" << name << "\",";
2139 }
2140 f << "],";
2141 ascFile.addPos(pos-(hasEntete?4:0));
2142 ascFile.addNote(f.str().c_str());
2143 return true;
2144 }
2145
2146 bool ClarisWksDocument::readDataLinkList()
2147 {
2148 MWAWInputStreamPtr input = m_parserState->m_input;
2149 long pos = input->tell();
2150 ClarisWksStruct::Struct zone;
2151 if (!zone.readHeader(input,false) || (zone.m_size && zone.m_dataSize<=0)) {
2152 input->seek(pos, librevenge::RVNG_SEEK_SET);
2153 MWAW_DEBUG_MSG(("ClarisWksDocument::readDataLinkList: can not read header\n"));
2154 return false;
2155 }
2156 libmwaw::DebugFile &ascFile= m_parserState->m_asciiFile;
2157 libmwaw::DebugStream f;
2158 f << "Entries(DataLink):";
2159
2160 if (zone.m_size == 0) {
2161 ascFile.addPos(pos);
2162 ascFile.addNote("NOP");
2163 return true;
2164 }
2165 long endPos=pos+4+zone.m_size;
2166 f << zone;
2167 if (zone.m_headerSize) {
2168 ascFile.addDelimiter(input->tell(), '|');
2169 input->seek(zone.m_headerSize, librevenge::RVNG_SEEK_CUR);
2170 }
2171 ascFile.addPos(pos);
2172 ascFile.addNote(f.str().c_str());
2173
2174 pos=input->tell();
2175 for (long i = 0; i < zone.m_numData; i++) {
2176 input->seek(pos, librevenge::RVNG_SEEK_SET);
2177 f.str("");
2178 f << "DataLink-" << i << ":";
2179 if (zone.m_dataSize==66 || zone.m_dataSize==70) { // find also 150 (probably 66+free space)
2180 int dim[2];
2181 for (auto &d: dim) d=int(input->readLong(2));
2182 if (dim[0]==-1) {
2183 ascFile.addPos(pos);
2184 ascFile.addNote(f.str().c_str());
2185 pos += zone.m_dataSize;
2186 continue;
2187 }
2188 f << "dim?=" << MWAWVec2i(dim[0],dim[1]) << ",";
2189 auto type=int(input->readLong(2));
2190 if (type==1)
2191 f << "pict,";
2192 else if (type)
2193 f << "type=" << type << ",";
2194 auto id=int(input->readLong(2));
2195 f << "id=" << id << ",";
2196 f << "Id=" << std::hex << input->readULong(4) << std::dec << ",";
2197 auto val=int(input->readULong(4));
2198 if (val) f << "data[size]=" << val << ",";
2199 val=int(input->readULong(1)); // 0|8|12
2200 if (val) f << "fl=" << std::hex << val << std::dec << ",";
2201 val=int(input->readLong(2)); // 0-1
2202 if (val) f << "f0=" << val << ",";
2203 val=int(input->readLong(2)); // 0,1200,1600
2204 if (val) f << "f1=" << double(val)/256. << ",";
2205 float fDim[4];
2206 for (auto &d : fDim) d=float(input->readLong(4))/65536.f;
2207 MWAWBox2f box(MWAWVec2f(fDim[0],fDim[1]),MWAWVec2f(fDim[2],fDim[3]));
2208 if (box!=MWAWBox2f()) f << "box=" << box << ",";
2209 val=int(input->readLong(2)); // 0,1 picture?
2210 if (val) f << "f2=" << val << ",";
2211 val=int(input->readLong(1)); // 0
2212 if (val) f << "f3=" << val << ",";
2213 for (int j=0; j<3; ++j) { // f4=[0,2:picture], f5=[0,1:pict,2:quicktime]
2214 val=int(input->readLong(2));
2215 if (val) f << "f" << 4+j << "=" << val << ",";
2216 }
2217 for (int j=0; j<2; ++j) {
2218 val=int(input->readULong(4));
2219 if (val) f << "Id" << 1+j << "=" << std::hex << val << std::dec << ",";
2220 }
2221 for (int j=0; j<(zone.m_dataSize==66 ? 6 : 7); ++j) { // g0=0|5, g1=0|-1
2222 val=int(input->readLong(2));
2223 if (val) f << "g" << j << "=" << val << ",";
2224 }
2225 if (zone.m_dataSize==70) {
2226 val=int(input->readLong(2));
2227 if (val!=id)
2228 f << "##id2=" << id << ",";
2229 }
2230 if (type==1)
2231 m_state->m_linkIdToDabasePictIdMap[int(i)]=id;
2232 }
2233 else if (i==0) {
2234 MWAW_DEBUG_MSG(("ClarisWksStruct::readDataLinkList: find unexpected data size\n"));
2235 f << "####";
2236 }
2237 ascFile.addPos(pos);
2238 ascFile.addNote(f.str().c_str());
2239 pos += zone.m_dataSize;
2240 }
2241 if (pos!=endPos) {
2242 MWAW_DEBUG_MSG(("ClarisWksStruct::readDataLinkList: find extra data\n"));
2243 ascFile.addPos(pos);
2244 ascFile.addNote("DataLink:###");
2245 }
2246 input->seek(endPos,librevenge::RVNG_SEEK_SET);
2247 return true;
2248 }
2249
2250 ///////////////////////////////////////////////////////////
2251 // a list of snapshot
2252 ////////////////////////////////////////////////////////////
2253 bool ClarisWksDocument::readSNAP(MWAWEntry const &entry)
2254 {
2255 if (!entry.valid() || entry.type() != "SNAP")
2256 return false;
2257 MWAWInputStreamPtr input = m_parserState->m_input;
2258 long pos = entry.begin();
2259 input->seek(pos+4, librevenge::RVNG_SEEK_SET); // skip header
2260 auto sz = long(input->readULong(4));
2261 if (sz > entry.length()) {
2262 MWAW_DEBUG_MSG(("ClarisWksDocument::readSNAP: pb with entry length"));
2263 input->seek(pos, librevenge::RVNG_SEEK_SET);
2264 return false;
2265 }
2266
2267 libmwaw::DebugStream f;
2268 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
2269 f << "Entries(SNAP)";
2270 ascFile.addPos(pos);
2271 ascFile.addNote(f.str().c_str());
2272 int id = 0;
2273 while (long(input->tell()) < entry.end()) {
2274 pos = input->tell();
2275 auto type=static_cast<int>(input->readLong(1));
2276 sz = long(input->readULong(4));
2277 if (pos+sz > entry.end()) {
2278 MWAW_DEBUG_MSG(("ClarisWksDocument::readSNAP: pb with sub zone: %d", id));
2279 input->seek(pos, librevenge::RVNG_SEEK_SET);
2280 return false;
2281 }
2282 f.str("");
2283 f << "SNAP-" << id++ << ":";
2284 if (type) f << "type=" << type;
2285 ascFile.addPos(pos);
2286 ascFile.addNote(f.str().c_str());
2287
2288 #ifdef DEBUG_WITH_FILES
2289 librevenge::RVNGBinaryData file;
2290 input->readDataBlock(sz, file);
2291
2292 static int volatile snapName = 0;
2293 f.str("");
2294 f << "SNAP" << ++snapName << ".pct";
2295 libmwaw::Debug::dumpFile(file, f.str().c_str());
2296
2297 if (type == 0)
2298 ascFile.skipZone(pos+5,pos+5+sz-1);
2299 #endif
2300 input->seek(pos+5+sz, librevenge::RVNG_SEEK_SET);
2301 }
2302
2303 return true;
2304 }
2305
2306 ////////////////////////////////////////////////////////////
2307 // read the print info
2308 ////////////////////////////////////////////////////////////
2309 bool ClarisWksDocument::readPrintInfo()
2310 {
2311 MWAWInputStreamPtr input = m_parserState->m_input;
2312 long pos = input->tell();
2313 if (input->readULong(2) != 0) return false;
2314 auto sz = long(input->readULong(2));
2315 if (sz < 0x78)
2316 return false;
2317 long endPos = pos+4+sz;
2318 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2319 if (long(input->tell()) != endPos) {
2320 MWAW_DEBUG_MSG(("ClarisWksDocument::readPrintInfo: file is too short\n"));
2321 return false;
2322 }
2323 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
2324
2325 libmwaw::DebugStream f;
2326 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
2327 // print info
2328 libmwaw::PrinterInfo info;
2329 if (!info.read(input)) {
2330 if (sz == 0x78) {
2331 // the size is ok, so let try to continue
2332 ascFile.addPos(pos);
2333 ascFile.addNote("Entries(PrintInfo):##");
2334 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2335 MWAW_DEBUG_MSG(("ClarisWksDocument::readPrintInfo: can not read print info, continue\n"));
2336 return true;
2337 }
2338 return false;
2339 }
2340 f << "Entries(PrintInfo):"<< info;
2341
2342 MWAWVec2i paperSize = info.paper().size();
2343 MWAWVec2i pageSize = info.page().size();
2344 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
2345 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
2346
2347 if (!m_state->m_pageSpanSet) {
2348 // define margin from print info
2349 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
2350 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
2351
2352 // move margin left | top
2353 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
2354 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
2355 lTopMargin -= MWAWVec2i(decalX, decalY);
2356 rBotMargin += MWAWVec2i(decalX, decalY);
2357
2358 m_parser->getPageSpan().setMarginTop(lTopMargin.y()/72.0);
2359 m_parser->getPageSpan().setMarginBottom(rBotMargin.y()/72.0);
2360 m_parser->getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
2361 m_parser->getPageSpan().setMarginRight(rBotMargin.x()/72.0);
2362 m_parser->getPageSpan().setFormLength(paperSize.y()/72.);
2363 m_parser->getPageSpan().setFormWidth(paperSize.x()/72.);
2364 }
2365
2366 if (long(input->tell()) !=endPos) {
2367 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2368 f << ", #endPos";
2369 ascFile.addDelimiter(input->tell(), '|');
2370 }
2371
2372 ascFile.addPos(pos);
2373 ascFile.addNote(f.str().c_str());
2374
2375 return true;
2376 }
2377
2378 // try to read a list of cell's zone
2379 bool ClarisWksDocument::readStructCellZone(char const *zoneName, bool hasEntete, std::vector<MWAWVec2i> &res)
2380 {
2381 if (!m_parserState) {
2382 MWAW_DEBUG_MSG(("ClarisWksDocument::readStructCellZone: can not find the parser state\n"));
2383 return false;
2384 }
2385 res.resize(0);
2386
2387 MWAWInputStreamPtr input = m_parserState->m_input;
2388 long pos=input->tell();
2389 ClarisWksStruct::Struct header;
2390 if (!header.readHeader(input,true) || (header.m_size && header.m_dataSize!=4)) {
2391 MWAW_DEBUG_MSG(("ClarisWksDocument::readStructCellZone: can not read the header\n"));
2392 return false;
2393 }
2394 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
2395 libmwaw::DebugStream f;
2396 if (zoneName && strlen(zoneName))
2397 f << "Entries(" << zoneName << "):";
2398 if (header.m_size==0) {
2399 if (hasEntete) {
2400 ascFile.addPos(pos-4);
2401 ascFile.addNote(f.str().c_str());
2402 }
2403 else {
2404 ascFile.addPos(pos);
2405 ascFile.addNote("_");
2406 }
2407 return true;
2408 }
2409 long endPos = pos+4+header.m_size;
2410 if (header.m_headerSize) {
2411 ascFile.addDelimiter(input->tell(),'|');
2412 input->seek(header.m_headerSize, librevenge::RVNG_SEEK_CUR);
2413 }
2414 f << "[";
2415 for (long i = 0; i < header.m_numData; i++) {
2416 int dim[2];
2417 for (int &d: dim) d = static_cast<int>(input->readLong(2));
2418 // checkme: do we need to invert the coordinate
2419 MWAWVec2i cell(dim[0],dim[1]);
2420 res.push_back(cell);
2421 f << cell << ",";
2422 }
2423 f << "]";
2424
2425 ascFile.addPos(hasEntete ? pos-4 : pos);
2426 ascFile.addNote(f.str().c_str());
2427
2428 input->seek(endPos,librevenge::RVNG_SEEK_SET);
2429 return true;
2430 }
2431
2432 bool ClarisWksDocument::readZoneA()
2433 {
2434 if (!m_parserState) {
2435 MWAW_DEBUG_MSG(("ClarisWksDocument::readZoneA: can not find the parser state\n"));
2436 return false;
2437 }
2438 MWAWInputStreamPtr input = m_parserState->m_input;
2439 long pos = input->tell();
2440 ClarisWksStruct::Struct header;
2441 if (!header.readHeader(input,true) || (header.m_size && header.m_dataSize==0)) {
2442 MWAW_DEBUG_MSG(("ClarisWksDocument::readZoneA: can not read the header\n"));
2443 return false;
2444 }
2445 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
2446 libmwaw::DebugStream f;
2447 if (header.m_size==0) {
2448 ascFile.addPos(pos);
2449 ascFile.addNote("Nop");
2450 return true;
2451 }
2452 if (header.m_headerSize) {
2453 ascFile.addDelimiter(input->tell(),'|');
2454 input->seek(header.m_headerSize, librevenge::RVNG_SEEK_CUR);
2455 }
2456 if (header.m_dataSize==4) {
2457 f << "ptrs=[";
2458 for (long i = 0; i < header.m_numData; i++) f << std::hex << input->readULong(4) << std::dec << ",";
2459 f << "],";
2460 }
2461 else {
2462 ascFile.addPos(pos);
2463 ascFile.addNote(f.str().c_str());
2464 for (long i = 0; i < header.m_numData; i++) {
2465 pos=input->tell();
2466 f.str("");
2467 f << "ZoneA-" << i << ":";
2468
2469 ascFile.addPos(pos);
2470 ascFile.addNote(f.str().c_str());
2471 input->seek(pos+header.m_dataSize, librevenge::RVNG_SEEK_SET);
2472 }
2473 MWAW_DEBUG_MSG(("ClarisWksDocument::readZoneA: unexpected field size\n"));
2474 return true;
2475 }
2476 ascFile.addPos(pos);
2477 ascFile.addNote(f.str().c_str());
2478
2479 pos=input->tell();
2480 if (!ClarisWksStruct::readStructZone(*m_parserState, "ZoneA",false)) { // find one time a list of id, 0x10
2481 MWAW_DEBUG_MSG(("ClarisWksDocument::readZoneA: can not read ZoneA-A\n"));
2482 input->seek(pos, librevenge::RVNG_SEEK_SET);
2483 return true;
2484 }
2485 ascFile.addPos(pos);
2486 ascFile.addNote("ZoneA-A:");
2487
2488 for (long i=0; i<2*header.m_numData; ++i) {
2489 pos = input->tell();
2490 if ((i%2)==0) {
2491 // the header contains a string: some chart/figure name?
2492 if (!ClarisWksStruct::readStructZone(*m_parserState, "ZoneA",false)) {
2493 MWAW_DEBUG_MSG(("ClarisWksDocument::readZoneA: can not read ZoneA-B\n"));
2494 input->seek(pos, librevenge::RVNG_SEEK_SET);
2495 return true;
2496 }
2497 f.str("");
2498 f << "ZoneA-B" << i/2 << ":";
2499 ascFile.addPos(pos);
2500 ascFile.addNote(f.str().c_str());
2501 continue;
2502 }
2503 f.str("");
2504 f << "ZoneA-C" << i/2 << "]:";
2505 // normally a block a 128 bytes: 8 block of 16 bytes?
2506 auto sz=long(input->readULong(4));
2507 if (!input->checkPosition(pos+sz+4)) {
2508 MWAW_DEBUG_MSG(("ClarisWksDocument::readZoneA: can not find a child field\n"));
2509 f << "###";
2510 ascFile.addPos(pos);
2511 ascFile.addNote(f.str().c_str());
2512 return false;
2513 }
2514 ascFile.addPos(pos);
2515 ascFile.addNote(f.str().c_str());
2516 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
2517 }
2518 return true;
2519 }
2520
2521 ////////////////////////////////////////////////////////////
2522 // try to order the zones
2523 ////////////////////////////////////////////////////////////
2524 void ClarisWksDocument::updateChildPositions()
2525 {
2526 // try to fix the page position corresponding to the main zone
2527 bool const isDraw=m_parserState->m_kind==MWAWDocument::MWAW_K_DRAW;
2528 int numHorizontalPages= isDraw ? getDocumentPages()[0] : 1;
2529 if (numHorizontalPages <= 0) {
2530 MWAW_DEBUG_MSG(("ClarisWksDocument::updateChildPositions: the number of accross pages is not set\n"));
2531 numHorizontalPages=1;
2532 }
2533 MWAWVec2f pageDim(72.0f*float(m_parser->getPageWidth()), 72.0f*float(getTextHeight())), graphPageDim;
2534 if (isDraw && m_graphParser->getPageDimension(graphPageDim)) {
2535 if (graphPageDim[0]>pageDim[0] || graphPageDim[1]>pageDim[1]) {
2536 MWAW_DEBUG_MSG(("ClarisWksDocument::updateChildPositions: the dimension given by the graph parser seems bad\n"));
2537 }
2538 else
2539 pageDim=graphPageDim;
2540 }
2541 float formLength = 72.0f*float(m_parser->getFormLength());
2542 float formWidth = 72.0f*float(m_parser->getFormWidth());
2543 if (isDraw) {
2544 for (int i=0; i<2; ++i) {
2545 if (i==0 && numHorizontalPages==1)
2546 continue;
2547 MWAWVariable<int> lastPage;
2548 for (auto const &iter : m_state->m_zonesMap) {
2549 auto zone = iter.second;
2550 if (!zone) continue;
2551 zone->findForbiddenPagesBreaking(pageDim[i], i==0 ? formWidth : formLength, i, lastPage);
2552 }
2553 if (!lastPage.isSet())
2554 continue;
2555 int last = *lastPage;
2556 if (last<=0 || last>100) {
2557 MWAW_DEBUG_MSG(("ClarisWksDocument::updateChildPositions: the last page seems bad for coord %d\n", i));
2558 continue;
2559 }
2560 MWAW_DEBUG_MSG(("ClarisWksDocument::updateChildPositions: increase page %d dimension by a factor %d\n", i, last+1));
2561 if (i==0) {
2562 formWidth*=float(last+1);
2563 m_parser->getPageSpan().setFormWidth(double(last+1)*m_parser->getFormWidth());
2564 numHorizontalPages=(numHorizontalPages+last)/(last+1);
2565 }
2566 else {
2567 formLength*=float(last+1);
2568 m_parser->getPageSpan().setFormLength(double(last+1)*m_parser->getFormLength());
2569 }
2570 pageDim[i] *= float(last+1);
2571 }
2572 }
2573 for (auto const &iter : m_state->m_zonesMap) {
2574 auto zone = iter.second;
2575 if (!zone) continue;
2576 zone->updateChildPositions(pageDim, formLength, numHorizontalPages);
2577 }
2578 }
2579
2580 void ClarisWksDocument::forceParsed(int zoneId)
2581 {
2582 std::shared_ptr<ClarisWksStruct::DSET> zMap = getZone(zoneId);
2583 if (zMap) zMap->m_parsed = true;
2584 }
2585
2586 bool ClarisWksDocument::exploreZonesGraph()
2587 {
2588 // first create the list of fathers
2589 for (auto const &iter : m_state->m_zonesMap) {
2590 std::shared_ptr<ClarisWksStruct::DSET> zone = iter.second;
2591 if (!zone) continue;
2592
2593 int id = zone->m_id;
2594 for (int step = 0; step < 2; step++) {
2595 size_t numChilds = step==0 ? zone->m_childs.size() : zone->m_otherChilds.size();
2596 for (size_t c = 0; c < numChilds; c++) {
2597 int cId = step == 0 ? zone->m_childs[c].m_id : zone->m_otherChilds[c];
2598 if (cId < 0) continue;
2599 if (cId == 0) {
2600 MWAW_DEBUG_MSG(("ClarisWksDocument::exploreZonesGraph: find a zone with id=0\n"));
2601 continue;
2602 }
2603
2604 auto iter2 = m_state->m_zonesMap.find(cId);
2605 if (iter2 == m_state->m_zonesMap.end()) {
2606 MWAW_DEBUG_MSG(("ClarisWksDocument::exploreZonesGraph: can not find zone %d\n", cId));
2607 continue;
2608 }
2609 iter2->second->m_fathersList.insert(id);
2610 }
2611
2612 if (step == 1) break;
2613 }
2614 }
2615
2616 // find the list of potential root
2617 std::vector<int> rootList;
2618 std::set<int> notDoneList;
2619 for (auto const &iter : m_state->m_zonesMap) {
2620 auto zone = iter.second;
2621 if (!zone) continue;
2622 zone->m_internal = 0;
2623 notDoneList.insert(zone->m_id);
2624 if (zone->m_fathersList.size()) continue;
2625 rootList.push_back(zone->m_id);
2626 }
2627
2628 std::set<int> toDoList(rootList.begin(), rootList.end());
2629 while (!notDoneList.empty()) {
2630 int id;
2631 if (!toDoList.empty()) {
2632 id = *toDoList.begin();
2633 toDoList.erase(id);
2634 }
2635 else {
2636 id = *notDoneList.begin();
2637 MWAW_DEBUG_MSG(("ClarisWksDocument::exploreZonesGraph: find a cycle, choose new root %d\n", id));
2638 rootList.push_back(id);
2639 }
2640 exploreZonesGraphRec(id, notDoneList);
2641 }
2642
2643 m_state->m_rootZonesList = rootList;
2644 if (rootList.empty()) {
2645 // we have a big problem here, no way to continue
2646 MWAW_DEBUG_MSG(("ClarisWksDocument::exploreZonesGraph: the graph contains no tree...\n"));
2647 return false;
2648 }
2649
2650 return true;
2651 }
2652
2653 bool ClarisWksDocument::exploreZonesGraphRec(int zId, std::set<int> &notDoneList)
2654 {
2655 notDoneList.erase(zId);
2656 auto iter = m_state->m_zonesMap.find(zId);
2657 if (iter == m_state->m_zonesMap.end()) {
2658 MWAW_DEBUG_MSG(("ClarisWksDocument::exploreZonesGraphRec: internal problem (can not find zone %d)\n", zId));
2659 return false;
2660 }
2661 auto zone = iter->second;
2662 if (!zone) return true;
2663 zone->m_internal = 1;
2664 for (int step = 0; step < 2; step++) {
2665 size_t numChilds = step==0 ? zone->m_childs.size() : zone->m_otherChilds.size();
2666 std::vector<int> toRemove; // list of bad child
2667 for (size_t c = 0; c < numChilds; c++) {
2668 int cId = step == 0 ? zone->m_childs[c].m_id : zone->m_otherChilds[c];
2669 if (cId <= 0) continue;
2670 bool isValid=true;
2671 if (notDoneList.find(cId) == notDoneList.end()) {
2672 auto iter2 = m_state->m_zonesMap.find(cId);
2673 if (iter2 == m_state->m_zonesMap.end()) {
2674 MWAW_DEBUG_MSG(("ClarisWksDocument::exploreZonesGraph: can not find zone %d\n", cId));
2675 isValid=false;
2676 }
2677 else if (iter2->second->m_internal==1) {
2678 MWAW_DEBUG_MSG(("ClarisWksDocument::exploreZonesGraph: find a cycle: for child : %d(<-%d)\n", cId, zId));
2679 isValid=false;
2680 }
2681 }
2682 else
2683 exploreZonesGraphRec(cId, notDoneList);
2684 if (!isValid) {
2685 toRemove.push_back(cId);
2686 auto child=getZone(cId);
2687 if (child) child->m_fathersList.erase(zone->m_id);
2688 }
2689 // else a valid child (already verified)
2690 }
2691 for (auto rem : toRemove)
2692 zone->removeChild(rem, step==0);
2693 }
2694 zone->m_internal = 2;
2695 return true;
2696 }
2697
2698 ////////////////////////////////////////////////////////////
2699 // try to mark the zones
2700 ////////////////////////////////////////////////////////////
2701 void ClarisWksDocument::typeMainZones()
2702 {
2703 // first type the main zone and its father
2704 typeMainZonesRec(1, ClarisWksStruct::DSET::P_Main, 100);
2705
2706 // now check the header/footer
2707 if (m_state->m_headerId) {
2708 auto iter = m_state->m_zonesMap.find(m_state->m_headerId);
2709 if (iter != m_state->m_zonesMap.end() && iter->second)
2710 iter->second->m_position = ClarisWksStruct::DSET::P_Header;
2711 }
2712 if (m_state->m_footerId) {
2713 auto iter = m_state->m_zonesMap.find(m_state->m_footerId);
2714 if (iter != m_state->m_zonesMap.end() && iter->second)
2715 iter->second->m_position = ClarisWksStruct::DSET::P_Footer;
2716 }
2717
2718 // then type the slides
2719 getPresentationParser()->updateSlideTypes();
2720
2721 std::vector<int> listZonesId[ClarisWksStruct::DSET::P_Unknown];
2722 for (auto const &iter : m_state->m_zonesMap) {
2723 int id = iter.first;
2724 auto node = iter.second;
2725 auto pos = node ? node->m_position : ClarisWksStruct::DSET::P_Unknown;
2726 if (pos == ClarisWksStruct::DSET::P_Unknown || pos == ClarisWksStruct::DSET::P_Main)
2727 continue;
2728 if (node->m_fileType != 1) // only propage data from a text node
2729 continue;
2730 if (pos > ClarisWksStruct::DSET::P_Unknown) {
2731 MWAW_DEBUG_MSG(("ClarisWksDocument::typeMainZones: OOPS, internal problem with type\n"));
2732 continue;
2733 }
2734 listZonesId[pos].push_back(id);
2735 }
2736 // try to type parents of typed zone
2737 for (int pos=ClarisWksStruct::DSET::P_Header; pos < ClarisWksStruct::DSET::P_Slide; pos++) {
2738 for (auto zId : listZonesId[pos])
2739 typeMainZonesRec(zId, ClarisWksStruct::DSET::Position(pos), 1);
2740 }
2741 // update m_zonesTypeMap
2742 for (auto const &iter : m_state->m_zonesMap) {
2743 int id = iter.first;
2744 auto node = iter.second;
2745 if (node)
2746 m_state->m_zonesTypeMap[id]=node->m_position;
2747 }
2748 // time to do to remove header/footer's zones in the main zones list
2749 auto rootList=m_state->m_rootZonesList;
2750 m_state->m_rootZonesList.resize(0);
2751 for (auto id : rootList) {
2752 auto zone=getZone(id);
2753 if (!zone) continue;
2754 if (zone->isHeaderFooter())
2755 m_state->m_hFZonesList.push_back(id);
2756 else
2757 m_state->m_rootZonesList.push_back(id);
2758 }
2759 // remove me
2760 bool isPresentation=m_parserState->m_kind==MWAWDocument::MWAW_K_PRESENTATION;
2761 bool isDraw=m_parserState->m_kind==MWAWDocument::MWAW_K_DRAW;
2762 for (int step=0; !isPresentation && step<2; ++step) {
2763 int id=step==0 ? m_state->m_headerId : m_state->m_footerId;
2764 if (!id) continue;
2765 // try to retrieve the father is also a header
2766 auto zone=getZone(id);
2767 if (zone && zone->m_fathersList.size()==1) {
2768 if (!isDraw) {
2769 auto father=getZone(*zone->m_fathersList.begin());
2770 if (father && father->isHeaderFooter()) {
2771 id=*zone->m_fathersList.begin();
2772 if (step==0) m_state->m_headerId=id;
2773 else m_state->m_footerId=id;
2774 }
2775 }
2776 if (std::find(m_state->m_hFZonesList.begin(),m_state->m_hFZonesList.end(), id)==m_state->m_hFZonesList.end())
2777 m_state->m_hFZonesList.push_back(id);
2778 }
2779 }
2780 }
2781
2782 int ClarisWksDocument::typeMainZonesRec(int zId, ClarisWksStruct::DSET::Position pos, int maxHeight)
2783 {
2784 if (maxHeight < 0) return 0;
2785
2786 auto node = getZone(zId);
2787 if (!node) return 0;
2788 if (node->m_position == ClarisWksStruct::DSET::P_Unknown)
2789 node->m_position = pos;
2790 else if (node->m_position != pos)
2791 return 0;
2792 if (maxHeight==0)
2793 return zId;
2794
2795 int res = zId;
2796 for (auto id : node->m_fathersList) {
2797 int fId = typeMainZonesRec(id, pos, maxHeight-1);
2798 if (fId) res = fId;
2799 }
2800 return res;
2801 }
2802
2803 ////////////////////////////////////////////////////////////
2804 // remove uneeded edge (mainly header/footer edge in main graph)
2805 ////////////////////////////////////////////////////////////
2806 void ClarisWksDocument::cleanZonesGraph()
2807 {
2808 bool isPresentation=m_parserState->m_kind==MWAWDocument::MWAW_K_PRESENTATION;
2809 if (isPresentation)
2810 m_presentationParser->disconnectMasterFromContents();
2811 else if (m_parserState->m_kind==MWAWDocument::MWAW_K_DRAW)
2812 m_graphParser->findMasterPage();
2813
2814 for (auto id : m_state->m_hFZonesList) {
2815 auto zone=getZone(id);
2816 if (!zone) continue;
2817 std::set<int> newFathers;
2818 for (auto fId : zone->m_fathersList) {
2819 std::shared_ptr<ClarisWksStruct::DSET> father = getZone(fId);
2820 if (!father || father->m_position!=ClarisWksStruct::DSET::P_Main || father->m_fileType!=0) {
2821 newFathers.insert(id);
2822 continue;
2823 }
2824 father->removeChild(id, true);
2825 }
2826 zone->m_fathersList=newFathers;
2827 }
2828 for (auto const &iter : m_state->m_zonesMap) {
2829 int id=iter.first;
2830 auto zone = iter.second;
2831 if (!zone || zone->m_fathersList.size()<2) continue;
2832 auto it=zone->m_fathersList.begin();
2833 int fId=*(it++);
2834 int fId1=*it;
2835 if (zone->m_fathersList.size()==2) {
2836 if (fId1==fId+1 && m_state->getZoneType(fId)==ClarisWksStruct::DSET::P_Table) {
2837 // a table and it auxiliary representation
2838 auto group=getZone(fId+1);
2839 if (group && group->m_fileType==0) {
2840 zone->m_fathersList.erase(fId1);
2841 group->removeChild(id, true);
2842 continue;
2843 }
2844 }
2845 else if (zone->isHeaderFooter() && !isPresentation) {
2846 // try to detach an header/footer text zone to the main zone
2847 auto fType=m_state->getZoneType(fId);
2848 auto fType1=m_state->getZoneType(fId1);
2849 int which=0;
2850 if (fType==ClarisWksStruct::DSET::P_Main && (fType1==ClarisWksStruct::DSET::P_Header || fType1==ClarisWksStruct::DSET::P_Footer))
2851 which=fId;
2852 else if (fType1==ClarisWksStruct::DSET::P_Main && (fType==ClarisWksStruct::DSET::P_Header || fType==ClarisWksStruct::DSET::P_Footer))
2853 which=fId1;
2854 if (which) {
2855 auto father=getZone(which);
2856 if (father && father->m_fileType==1) {
2857 /* ok, pointer to a text section, we only erase connection.
2858 fixme: replace the connection in the text section to the group corresponding to the header */
2859 father->m_fathersList.erase(which);
2860 continue;
2861 }
2862 }
2863 }
2864 }
2865 #ifdef DEBUG
2866 std::cerr << "Node " << id << " has [";
2867 for (auto i : zone->m_fathersList)
2868 std::cerr << i << ",";
2869 std::cerr << "] parents\n";
2870 #endif
2871 }
2872 if (m_parserState->m_kind == MWAWDocument::MWAW_K_TEXT) {
2873 // time to try detach the main text block
2874 auto main=getZone(1);
2875 if (main && main->m_fileType==1 && main->m_fathersList.size()==1) {
2876 auto mainFather=getZone(*main->m_fathersList.begin());
2877 if (mainFather && mainFather->m_fileType==0) {
2878 // ok, let remove the connection between the text group and the text and consider that the text group is parsed
2879 mainFather->m_parsed=true;
2880 mainFather->removeChild(1, true);
2881 main->m_fathersList.clear();
2882 // now try to detach the text group from the document group
2883 if (mainFather->m_fathersList.size()==1 && !m_state->m_rootZonesList.empty() &&
2884 *mainFather->m_fathersList.begin()==m_state->m_rootZonesList[0]) {
2885 auto root=getZone(m_state->m_rootZonesList[0]);
2886 if (root) {
2887 mainFather->m_fathersList.clear();
2888 root->removeChild(mainFather->m_id, true);
2889 }
2890 }
2891 else {
2892 MWAW_DEBUG_MSG(("ClarisWksDocument::cleanZonesGraph: oops, can not detach the main text zone from the document zone\n"));
2893 }
2894 }
2895 else {
2896 MWAW_DEBUG_MSG(("ClarisWksDocument::cleanZonesGraph: oops, can not find the main zone block\n"));
2897 }
2898 }
2899 else {
2900 MWAW_DEBUG_MSG(("ClarisWksDocument::cleanZonesGraph: oops, can not find the main zone block id\n"));
2901 }
2902 }
2903
2904 /* try to remove orphelan group. There often exists one in word
2905 processing document and if we do not remove it, it will create an
2906 empty textbox, so let remove all of them...
2907
2908 Checkme: removing empty textboxes must be also done as there
2909 appears frequently (a debutant's error), but as this requires
2910 interaction with the graph(to check the border/background's
2911 style) and the text(to check if the textbox are empty) parsers,
2912 :-~ */
2913 std::set<int> orphelanSet;
2914 for (auto const &iter : m_state->m_zonesMap) {
2915 int id=iter.first;
2916 std::shared_ptr<ClarisWksStruct::DSET> zone = iter.second;
2917 if (!zone || zone->m_fileType!=0 || !zone->m_childs.empty() || !zone->m_otherChilds.empty() || zone->m_parsed)
2918 continue;
2919 orphelanSet.insert(id);
2920 }
2921 while (!orphelanSet.empty()) {
2922 int id=*orphelanSet.begin();
2923 orphelanSet.erase(id);
2924 auto zone = getZone(id);
2925 if (!zone || zone->m_parsed || zone->m_fathersList.size()!=1)
2926 continue;
2927 zone->m_parsed=true;
2928
2929 int fId=*zone->m_fathersList.begin();
2930 auto father = getZone(fId);
2931 if (!father)
2932 continue;
2933 zone->m_fathersList.clear();
2934 father->removeChild(id);
2935
2936 if (father->m_fileType!=0 || !father->m_childs.empty() || !father->m_otherChilds.empty() || father->m_parsed)
2937 continue;
2938 orphelanSet.insert(fId);
2939 }
2940 }
2941
2942 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: