Debian Codegraph @

src/lib/RagTimeParser.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <iomanip>
35 #include <iostream>
36 #include <limits>
37 #include <set>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWTextListener.hxx"
43 #include "MWAWFont.hxx"
44 #include "MWAWGraphicStyle.hxx"
45 #include "MWAWGraphicShape.hxx"
46 #include "MWAWHeader.hxx"
47 #include "MWAWParagraph.hxx"
48 #include "MWAWPictBitmap.hxx"
49 #include "MWAWPictMac.hxx"
50 #include "MWAWPosition.hxx"
51 #include "MWAWPrinter.hxx"
52 #include "MWAWRSRCParser.hxx"
53 #include "MWAWSubDocument.hxx"
54
55 #include "RagTimeSpreadsheet.hxx"
56 #include "RagTimeStruct.hxx"
57 #include "RagTimeText.hxx"
58
59 #include "RagTimeParser.hxx"
60
61 /** Internal: the structures of a RagTimeParser */
62 namespace RagTimeParserInternal
63 {
64 ////////////////////////////////////////
65 //! Internal: the pattern of a RagTimeManager
66 struct Pattern final : public MWAWGraphicStyle::Pattern {
67 //! constructor ( 4 int by patterns )
68 explicit Pattern(uint16_t const *pat=nullptr)
69 : MWAWGraphicStyle::Pattern()
70 , m_percent(0)
71 {
72 if (!pat) return;
73 m_colors[0]=MWAWColor::white();
74 m_colors[1]=MWAWColor::black();
75 m_dim=MWAWVec2i(8,8);
76 m_data.resize(8);
77 for (size_t i=0; i < 4; ++i) {
78 uint16_t val=pat[i];
79 m_data[2*i]=static_cast<unsigned char>(val>>8);
80 m_data[2*i+1]=static_cast<unsigned char>(val&0xFF);
81 }
82 int numOnes=0;
83 for (size_t j=0; j < 8; ++j) {
84 auto val=static_cast<uint8_t>(m_data[j]);
85 for (int b=0; b < 8; b++) {
86 if (val&1) ++numOnes;
87 val = uint8_t(val>>1);
88 }
89 }
90 m_percent=float(numOnes)/64.f;
91 }
92 Pattern(Pattern const &)=default;
93 Pattern &operator=(Pattern const &)=default;
94 Pattern &operator=(Pattern &&)=default;
95 //! destructor
96 ~Pattern() final;
97 //! the percentage
98 float m_percent;
99 };
100
101 Pattern::~Pattern()
102 {
103 }
104 ////////////////////////////////////////
105 //! Internal: a picture of a RagTimeParser
106 struct Picture {
107 //! constructor
108 Picture()
109 : m_type(0)
110 , m_pos()
111 , m_dim()
112 , m_headerPos(0)
113 , m_isSent(false)
114 {
115 }
116 //! the picture type(unsure)
117 int m_type;
118 //! the data position
119 MWAWEntry m_pos;
120 //! the dimension
121 MWAWBox2f m_dim;
122 //! the beginning of the header(for debugging)
123 long m_headerPos;
124 //! a flag to know if the picture is sent
125 mutable bool m_isSent;
126 };
127
128 ////////////////////////////////////////
129 //! Internal: a zone of a RagTimeParser
130 struct Zone {
131 //! the zone type
132 enum Type { Text, Page, Picture, Line, Spreadsheet, Chart, Unknown };
133 //! constructor
134 Zone()
135 : m_type(Unknown)
136 , m_subType(0)
137 , m_read32Size(false)
138 , m_dimension()
139 , m_page(0)
140 , m_rotation(0)
141 , m_style(MWAWGraphicStyle::emptyStyle())
142 , m_fontColor(MWAWColor::black())
143 , m_arrowFlags(0)
144 , m_sharedWith(0)
145 , m_isSent(false)
146 , m_extra("")
147 {
148 for (auto &lZone : m_linkZones) lZone=0;
149 }
150 //! returns the bounding box
151 MWAWBox2f getBoundingBox() const
152 {
153 MWAWVec2f minPt=m_dimension[0], maxPt=m_dimension[1];
154 for (int i=0; i<2; ++i) {
155 if (m_dimension[0][i]<=m_dimension[1][i])
156 continue;
157 minPt[i]=m_dimension[1][i];
158 maxPt[i]=m_dimension[0][i];
159 }
160 return MWAWBox2f(minPt,maxPt);
161 }
162 //! returns a zone name
163 std::string getTypeString() const
164 {
165 switch (m_type) {
166 case Chart:
167 return "Chart";
168 case Spreadsheet:
169 return "SheetZone";
170 case Page:
171 return "Page";
172 case Picture:
173 return "PictZone";
174 case Text:
175 return "TextZone";
176 case Line:
177 return "Line";
178 case Unknown:
179 #if !defined(__clang__)
180 default:
181 #endif
182 break;
183 }
184 std::stringstream s;
185 if (m_subType&0xf)
186 s << "Undef" << m_subType << "A";
187 else
188 s << "Zone" << m_subType;
189 return s.str();
190 }
191 //! operator<<
192 friend std::ostream &operator<<(std::ostream &o, Zone const &z);
193 //! the zone type
194 Type m_type;
195 //! the zone sub type
196 int m_subType;
197 //! flag to know if the datasize in uint16 or uint32
198 bool m_read32Size;
199 //! the dimension
200 MWAWBox2f m_dimension;
201 //! the page
202 int m_page;
203 //! the rotation
204 int m_rotation;
205 //! the style
206 MWAWGraphicStyle m_style;
207 //! the font color (for text)
208 MWAWColor m_fontColor;
209 //! arrow flag 1:begin, 2:end
210 int m_arrowFlags;
211 //! the link zones ( parent, prev, next, child, linked)
212 int m_linkZones[5];
213 //! the zone which contains the content
214 int m_sharedWith;
215 //! a flag to know if the picture is sent
216 mutable bool m_isSent;
217 //! extra data
218 std::string m_extra;
219 };
220
221 std::ostream &operator<<(std::ostream &o, Zone const &z)
222 {
223 switch (z.m_type) {
224 case Zone::Chart:
225 o << "chart,";
226 break;
227 case Zone::Line:
228 o << "line,";
229 break;
230 case Zone::Page:
231 o << "page,";
232 break;
233 case Zone::Picture:
234 o << "pict,";
235 break;
236 case Zone::Text:
237 o << "text,";
238 break;
239 case Zone::Spreadsheet:
240 o << "spreadsheet,";
241 break;
242 case Zone::Unknown:
243 #if !defined(__clang__)
244 default:
245 #endif
246 o << "zone" << z.m_subType << ",";
247 break;
248 }
249 if (z.m_read32Size) o << "32[dataSize],";
250 o << "dim=" << z.m_dimension << ",";
251 if (z.m_page>0) o << "page=" << z.m_page << ",";
252 if (z.m_rotation) o << "rot=" << z.m_rotation << ",";
253 o << "style=[" << z.m_style << "],";
254 if (!z.m_fontColor.isBlack())
255 o << "color[font]=" << z.m_fontColor << ",";
256 if (z.m_arrowFlags&1)
257 o << "arrows[beg],";
258 if (z.m_arrowFlags&2)
259 o << "arrows[end],";
260 o << "ids=[";
261 for (int i=0; i<5; ++i) {
262 if (!z.m_linkZones[i]) continue;
263 static char const *wh[]= {"parent", "prev", "next", "child", "linked"};
264 o << wh[i] << "=Z" << z.m_linkZones[i] << ",";
265 }
266 o << "],";
267 if (z.m_sharedWith)
268 o << "#shared=Z" << z.m_sharedWith << ",";
269 o << z.m_extra << ",";
270 return o;
271 }
272
273 ////////////////////////////////////////
274 //! Internal: the state of a RagTimeParser
275 struct State {
276 //! constructor
277 State()
278 : m_numDataZone(0)
279 , m_dataZoneMap()
280 , m_RSRCZoneMap()
281 , m_idColorsMap()
282 , m_patternList()
283 , m_actualZoneId(-1)
284 , m_idZoneMap()
285 , m_pageZonesIdMap()
286 , m_idPictureMap()
287 , m_actPage(0)
288 , m_numPages(0)
289 {
290 }
291 //! init the pattern to default
292 void initDefaultPatterns(int vers);
293 //! the number of data zone
294 int m_numDataZone;
295 //! a map: type->entry (datafork)
296 std::multimap<std::string, MWAWEntry> m_dataZoneMap;
297 //! a map: type->entry (resource fork)
298 std::multimap<std::string, MWAWEntry> m_RSRCZoneMap;
299 //! the color map
300 std::map<int, std::vector<MWAWColor> > m_idColorsMap;
301 //! a list patternId -> pattern
302 std::vector<Pattern> m_patternList;
303 //! the actual zone id
304 int m_actualZoneId;
305 //! a map: zoneId->zone (datafork)
306 std::map<int, Zone> m_idZoneMap;
307 //! a map: page->main zone id
308 std::map<int, std::vector<int> > m_pageZonesIdMap;
309 //! a map: zoneId->picture (datafork)
310 std::map<int, Picture> m_idPictureMap;
311 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
312 };
313
314 void State::initDefaultPatterns(int vers)
315 {
316 if (!m_patternList.empty()) return;
317 if (vers <= 2) {
318 static uint16_t const s_pattern[4*40] = {
319 0x0, 0x0, 0x0, 0x0, 0x8000, 0x800, 0x8000, 0x800, 0x8800, 0x2200, 0x8800, 0x2200, 0x8822, 0x8822, 0x8822, 0x8822,
320 0xaa55, 0xaa55, 0xaa55, 0xaa55, 0xdd77, 0xdd77, 0xdd77, 0xdd77, 0xddff, 0x77ff, 0xddff, 0x77ff, 0xffff, 0xffff, 0xffff, 0xffff,
321 0x8888, 0x8888, 0x8888, 0x8888, 0xff00, 0x0, 0xff00, 0x0, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa, 0xff00, 0xff00, 0xff00, 0xff00,
322 0xeedd, 0xbb77, 0xeedd, 0xbb77, 0x1122, 0x4488, 0x1122, 0x4488, 0x102, 0x408, 0x1020, 0x4080, 0x102, 0x0, 0x0, 0x4080,
323 0x77bb, 0xddee, 0x77bb, 0xddee, 0x8844, 0x2211, 0x8844, 0x2211, 0x8040, 0x2010, 0x804, 0x201, 0x8040, 0x0, 0x0, 0x201,
324 0x55ff, 0x55ff, 0x55ff, 0x55ff, 0xaa00, 0xaa00, 0xaa00, 0xaa00, 0x8010, 0x220, 0x108, 0x4004, 0xb130, 0x31b, 0xd8c0, 0xc8d,
325 0xff88, 0x8888, 0xff88, 0x8888, 0xaa00, 0x8000, 0x8800, 0x8000, 0xff80, 0x8080, 0x8080, 0x8080, 0xf0f0, 0xf0f0, 0xf0f, 0xf0f,
326 0xc300, 0x0, 0x3c00, 0x0, 0x808, 0x8080, 0x8080, 0x808, 0x8040, 0x2000, 0x204, 0x800, 0x2, 0x588, 0x5020, 0x0,
327 0x8000, 0x0, 0x0, 0x0, 0x40a0, 0x0, 0x40a, 0x0, 0x8142, 0x2418, 0x1824, 0x4281, 0x2050, 0x8888, 0x8888, 0x502,
328 0xff80, 0x8080, 0xff08, 0x808, 0x44, 0x2810, 0x2844, 0x0, 0x103c, 0x5038, 0x1478, 0x1000, 0x8, 0x142a, 0x552a, 0x1408
329 };
330 m_patternList.resize(40);
331 for (size_t i = 0; i < 40; i++)
332 m_patternList[i]=Pattern(&s_pattern[i*4]);
333 }
334 }
335
336 ////////////////////////////////////////
337 //! Internal: the subdocument of a RagTimeParser
338 class SubDocument final : public MWAWSubDocument
339 {
340 public:
341 // constructor
342 SubDocument(RagTimeParser &pars, MWAWInputStreamPtr const &input, int zoneId)
343 : MWAWSubDocument(&pars, input, MWAWEntry())
344 , m_id(zoneId) {}
345
346 //! destructor
347 ~SubDocument() final {}
348
349 //! operator!=
350 bool operator!=(MWAWSubDocument const &doc) const final;
351
352 //! the parser function
353 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
354
355 protected:
356 //! the subdocument id
357 int m_id;
358 };
359
360 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
361 {
362 if (!listener.get()) {
363 MWAW_DEBUG_MSG(("RagTimeParserInternal::SubDocument::parse: no listener\n"));
364 return;
365 }
366 auto *parser=dynamic_cast<RagTimeParser *>(m_parser);
367 if (!parser) {
368 MWAW_DEBUG_MSG(("RagTimeParserInternal::SubDocument::parse: no listener\n"));
369 return;
370 }
371 long pos = m_input->tell();
372 parser->sendText(m_id, listener);
373 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
374 }
375
376 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
377 {
378 if (MWAWSubDocument::operator!=(doc)) return true;
379 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
380 if (!sDoc) return true;
381 if (m_id != sDoc->m_id) return true;
382 return false;
383 }
384 }
385
386
387 ////////////////////////////////////////////////////////////
388 // constructor/destructor, ...
389 ////////////////////////////////////////////////////////////
390 RagTimeParser::RagTimeParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
391 : MWAWTextParser(input, rsrcParser, header)
392 , m_state()
393 , m_spreadsheetParser()
394 , m_textParser()
395 {
396 init();
397 }
398
399 RagTimeParser::~RagTimeParser()
400 {
401 }
402
403 void RagTimeParser::init()
404 {
405 resetTextListener();
406 setAsciiName("main-1");
407
408 m_state.reset(new RagTimeParserInternal::State);
409
410 // reduce the margin (in case, the page is not defined)
411 getPageSpan().setMargins(0.1);
412
413 m_spreadsheetParser.reset(new RagTimeSpreadsheet(*this));
414 m_textParser.reset(new RagTimeText(*this));
415 }
416
417 int RagTimeParser::getFontId(int localId) const
418 {
419 return m_textParser->getFontId(localId);
420 }
421
422 bool RagTimeParser::getCharStyle(int charId, MWAWFont &font) const
423 {
424 return m_textParser->getCharStyle(charId, font);
425 }
426
427 bool RagTimeParser::getColor(int colId, MWAWColor &color, int listId) const
428 {
429 if (listId==-1) listId=version()>=2 ? 1 : 0;
430 if (m_state->m_idColorsMap.find(listId)==m_state->m_idColorsMap.end()) {
431 MWAW_DEBUG_MSG(("RagTimeParser::getColor: can not find the color list %d\n", listId));
432 return false;
433 }
434 auto const &colors=m_state->m_idColorsMap.find(listId)->second;
435 if (colId<0 || colId>=static_cast<int>(colors.size())) {
436 MWAW_DEBUG_MSG(("RagTimeParser::getColor: can not find color %d\n", colId));
437 return false;
438 }
439 color=colors[size_t(colId)];
440 return true;
441 }
442
443 int RagTimeParser::getZoneDataFieldSize(int zId) const
444 {
445 if (m_state->m_idZoneMap.find(zId)==m_state->m_idZoneMap.end()) {
446 MWAW_DEBUG_MSG(("RagTimeParser::getZoneDataSize: can not find the zone %d\n", zId));
447 return 2;
448 }
449 return m_state->m_idZoneMap.find(zId)->second.m_read32Size ? 4 : 2;
450 }
451
452 int RagTimeParser::getNewZoneId()
453 {
454 return ++m_state->m_actualZoneId;
455 }
456
457 bool RagTimeParser::readTextZone(MWAWEntry &entry, int width, MWAWColor const &fontColor)
458 {
459 return m_textParser->readTextZone(entry, width, fontColor);
460 }
461
462 bool RagTimeParser::getDateTimeFormat(int dtId, std::string &dtFormat) const
463 {
464 return m_spreadsheetParser->getDateTimeFormat(dtId,dtFormat);
465 }
466
467 bool RagTimeParser::sendText(int zId, MWAWListenerPtr listener)
468 {
469 return m_textParser->send(zId, listener);
470 }
471
472 ////////////////////////////////////////////////////////////
473 // new page
474 ////////////////////////////////////////////////////////////
475 void RagTimeParser::newPage(int number)
476 {
477 if (number <= m_state->m_actPage || number > m_state->m_numPages)
478 return;
479
480 while (m_state->m_actPage < number) {
481 m_state->m_actPage++;
482 if (!getTextListener() || m_state->m_actPage == 1)
483 continue;
484 getTextListener()->insertBreak(MWAWTextListener::PageBreak);
485 }
486 }
487
488 ////////////////////////////////////////////////////////////
489 // the parser
490 ////////////////////////////////////////////////////////////
491 void RagTimeParser::parse(librevenge::RVNGTextInterface *docInterface)
492 {
493 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
494 bool ok = true;
495 try {
496 // create the asciiFile
497 ascii().setStream(getInput());
498 ascii().open(asciiName());
499 checkHeader(nullptr);
500 ok=createZones();
501 if (ok) {
502 createDocument(docInterface);
503 sendZones();
504 #ifdef DEBUG
505 m_textParser->flushExtra();
506 m_spreadsheetParser->flushExtra();
507 flushExtra();
508 #endif
509 }
510
511 ascii().reset();
512 }
513 catch (...) {
514 MWAW_DEBUG_MSG(("RagTimeParser::parse: exception catched when parsing\n"));
515 ok = false;
516 }
517
518 resetTextListener();
519 if (!ok) throw(libmwaw::ParseException());
520 }
521
522 ////////////////////////////////////////////////////////////
523 // create the document
524 ////////////////////////////////////////////////////////////
525 void RagTimeParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
526 {
527 if (!documentInterface) return;
528 if (getTextListener()) {
529 MWAW_DEBUG_MSG(("RagTimeParser::createDocument: listener already exist\n"));
530 return;
531 }
532
533 // update the page
534 int numPages=1;
535 for (auto const &it : m_state->m_idZoneMap) {
536 auto const &z=it.second;
537 if (z.m_type!=RagTimeParserInternal::Zone::Page || numPages>z.m_page)
538 continue;
539 numPages=z.m_page;
540 }
541 m_state->m_actPage = 0;
542 m_state->m_numPages=numPages;
543
544 // create the page list
545 MWAWPageSpan ps(getPageSpan());
546 std::vector<MWAWPageSpan> pageList;
547 ps.setPageSpan(m_state->m_numPages);
548 pageList.push_back(ps);
549 //
550 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
551 setTextListener(listen);
552 listen->startDocument();
553 }
554
555
556 ////////////////////////////////////////////////////////////
557 //
558 // Intermediate level
559 //
560 ////////////////////////////////////////////////////////////
561
562 bool RagTimeParser::createZones()
563 {
564 int const vers=version();
565 if (!findDataZones())
566 return false;
567
568 libmwaw::DebugStream f;
569
570 if (vers<=1) {
571 MWAWRSRCParserPtr rsrcParser = getRSRCParser();
572 if (rsrcParser) {
573 auto &entryMap = rsrcParser->getEntriesMap();
574 auto it=entryMap.find("PREC");
575 if (it!=entryMap.end())
576 readPrintInfo(it->second, true);
577 }
578 }
579 else if (vers==2) {
580 // print info
581 auto it=m_state->m_RSRCZoneMap.lower_bound("rsrcPREC");
582 while (it!=m_state->m_RSRCZoneMap.end() && it->first=="rsrcPREC")
583 readPrintInfo(it++->second);
584 // the font
585 it=m_state->m_RSRCZoneMap.lower_bound("FontName");
586 while (it!=m_state->m_RSRCZoneMap.end() && it->first=="FontName")
587 m_textParser->readFontNames(it++->second);
588 // the character properties
589 it=m_state->m_RSRCZoneMap.lower_bound("CharProp");
590 while (it!=m_state->m_RSRCZoneMap.end() && it->first=="CharProp")
591 m_textParser->readCharProperties(it++->second);
592 // the numeric and spreadsheet formats
593 readFormatsMap();
594 // the file link
595 it=m_state->m_RSRCZoneMap.lower_bound("Link");
596 while (it!=m_state->m_RSRCZoneMap.end() && it->first=="Link")
597 readLinks(it++->second);
598 // puce, ...
599 it=m_state->m_RSRCZoneMap.lower_bound("Macro");
600 while (it!=m_state->m_RSRCZoneMap.end() && it->first=="Macro")
601 readMacroFormats(it++->second);
602 }
603 auto it=m_state->m_dataZoneMap.lower_bound("TextZone");
604 while (it!=m_state->m_dataZoneMap.end() && it->first=="TextZone") {
605 MWAWEntry entry=it++->second;
606 int width;
607 MWAWColor fontColor;
608 if (m_state->m_idZoneMap.find(entry.id())!=m_state->m_idZoneMap.end()) {
609 auto const &zone=m_state->m_idZoneMap.find(entry.id())->second;
610 if (zone.m_rotation==90 || zone.m_rotation==270)
611 width=int(zone.m_dimension.size()[1]);
612 else
613 width=int(zone.m_dimension.size()[0]);
614 fontColor=zone.m_fontColor;
615 }
616 else {
617 MWAW_DEBUG_MSG(("RagTimeParser::createZones: can not find text zone with id=%d\n", entry.id()));
618 width=int(72.*getPageWidth());
619 fontColor=MWAWColor::black();
620 }
621 m_textParser->readTextZone(entry, width, fontColor);
622 }
623 it=m_state->m_dataZoneMap.lower_bound("PictZone");
624 while (it!=m_state->m_dataZoneMap.end() && it->first=="PictZone") {
625 if (vers==1)
626 readPictZoneV2(it++->second);
627 else
628 readPictZone(it++->second);
629 }
630
631 // now unknown zone
632 it=m_state->m_dataZoneMap.lower_bound("PageZone");
633 while (it!=m_state->m_dataZoneMap.end() && it->first=="PageZone")
634 readPageZone(it++->second);
635
636 it=m_state->m_dataZoneMap.lower_bound("SheetZone");
637 while (it!=m_state->m_dataZoneMap.end() && it->first=="SheetZone") {
638 if (vers==1)
639 m_spreadsheetParser->readSpreadsheetV2(it++->second);
640 else
641 m_spreadsheetParser->readSpreadsheet(it++->second);
642 }
643 it=m_state->m_dataZoneMap.lower_bound("Zone6");
644 while (it!=m_state->m_dataZoneMap.end() && it->first=="Zone6")
645 readZone6(it++->second);
646
647 it=m_state->m_RSRCZoneMap.lower_bound("rsrcfppr");
648 while (it!=m_state->m_RSRCZoneMap.end() && it->first=="rsrcfppr")
649 readRsrcfppr(it++->second);
650 it=m_state->m_RSRCZoneMap.lower_bound("rsrcBeDc");
651 while (it!=m_state->m_RSRCZoneMap.end() && it->first=="rsrcBeDc")
652 readRsrcBeDc(it++->second);
653 it=m_state->m_RSRCZoneMap.lower_bound("rsrcBtch");
654 while (it!=m_state->m_RSRCZoneMap.end() && it->first=="rsrcBtch")
655 readRsrcBtch(it++->second);
656 it=m_state->m_RSRCZoneMap.lower_bound("rsrcCalc");
657 while (it!=m_state->m_RSRCZoneMap.end() && it->first=="rsrcCalc")
658 readRsrcCalc(it++->second);
659
660 it=m_state->m_RSRCZoneMap.lower_bound("rsrcFHwl");
661 while (it!=m_state->m_RSRCZoneMap.end() && it->first=="rsrcFHwl")
662 readRsrcFHwl(it++->second);
663 for (int i=0; i<6; ++i) {
664 /* gray [2bytes]:color,0,26
665 color [2bytes]*3:color
666 res_ [2bytes]*3: sometimes string?
667 BuSl: [small 2bytes int]*3
668 BuGr : structure with many data of size 0x1a
669 Unamed: sometimes structured
670 */
671 static char const *what[]= {
672 "rsrcgray", "rsrccolr", "rsrcres_",
673 "rsrcBuSl", "rsrcBuGr", "rsrcUnamed"
674 };
675 it=m_state->m_RSRCZoneMap.lower_bound(what[i]);
676 while (it!=m_state->m_RSRCZoneMap.end() && it->first==what[i])
677 readRsrcStructured(it++->second);
678 }
679
680 for (auto const &iter : m_state->m_dataZoneMap) {
681 MWAWEntry const &entry=iter.second;
682 if (entry.isParsed()) continue;
683 f.str("");
684 f << "Entries(" << entry.type() << "):";
685 ascii().addPos(entry.begin());
686 ascii().addNote(f.str().c_str());
687 }
688 for (auto const &iter : m_state->m_RSRCZoneMap) {
689 MWAWEntry const &entry=iter.second;
690 if (entry.isParsed()) continue;
691 f.str("");
692 f << "Entries(" << entry.type() << ")";
693 if (entry.id()) f << "[" << entry.id() << "]";
694 f << ":";
695 ascii().addPos(entry.begin());
696 ascii().addNote(f.str().c_str());
697 }
698
699 // time to sort the zones by page
700 findPagesZones();
701 return true;
702 }
703
704 ////////////////////////////////////////////////////////////
705 // find the different zones
706 ////////////////////////////////////////////////////////////
707
708 bool RagTimeParser::findDataZones()
709 {
710 MWAWInputStreamPtr input = getInput();
711 int const vers=version();
712 int const headerSize=vers>=2 ? 156 : 196;
713 int const zoneLength=vers>=2 ? 54 : 40;
714 m_state->initDefaultPatterns(vers);
715
716 libmwaw::DebugStream f;
717 f << "Entries(Zones)[header]:";
718 long pos = input->tell();
719 input->seek(pos+(vers>=2 ? 48 : 72), librevenge::RVNG_SEEK_SET);
720 ascii().addDelimiter(input->tell(),'|');
721 m_state->m_numDataZone=static_cast<int>(input->readULong(2));
722 long endPos=pos+headerSize+m_state->m_numDataZone*zoneLength;
723 f << "num[zones]=" << m_state->m_numDataZone << ",";
724 if (m_state->m_numDataZone==0 || !input->checkPosition(endPos)) {
725 MWAW_DEBUG_MSG(("RagTimeParser::findDataZones: can not find the number of zones\n"));
726 f << "###";
727 ascii().addPos(pos);
728 ascii().addNote(f.str().c_str());
729 return false;
730 }
731 ascii().addDelimiter(input->tell(),'|');
732 // we must first read the color map as the frame header can use them
733 if (vers==1) {
734 input->seek(pos+186, librevenge::RVNG_SEEK_SET);
735 MWAWEntry entry;
736 entry.setBegin(long(input->readULong(2)));
737 entry.setType("ColorMap");
738 readColorMapV2(entry);
739 }
740 else if (vers>=2) {
741 input->seek(endPos, librevenge::RVNG_SEEK_SET);
742 if (findRsrcZones())
743 readColorsMap();
744 }
745 ascii().addPos(pos);
746 ascii().addNote(f.str().c_str());
747 input->seek(pos+headerSize, librevenge::RVNG_SEEK_SET);
748 for (int n=0; n<m_state->m_numDataZone; ++n) {
749 pos=input->tell();
750 if (!readDataZoneHeader(n+1, endPos)) {
751 input->seek(pos, librevenge::RVNG_SEEK_SET);
752 break;
753 }
754 }
755 ascii().addPos(endPos);
756 ascii().addNote("_");
757
758 return true;
759 }
760
761 bool RagTimeParser::findPagesZones()
762 {
763 for (auto const &it : m_state->m_idZoneMap) {
764 auto const &main=it.second;
765 if (main.m_type!=RagTimeParserInternal::Zone::Page)
766 continue;
767 int zId=main.m_linkZones[3];
768 int page=main.m_page;
769 std::set<int> seens;
770 std::vector<int> lists;
771 while (zId) {
772 if (seens.find(zId)!=seens.end()) {
773 MWAW_DEBUG_MSG(("RagTimeParser::findPagesZones: already find zone %d\n", zId));
774 break;
775 }
776 seens.insert(zId);
777 if (m_state->m_idZoneMap.find(zId)==m_state->m_idZoneMap.end()) {
778 MWAW_DEBUG_MSG(("RagTimeParser::findPagesZones: %d does not correspond to a zone\n", zId));
779 break;
780 }
781 auto &zone=m_state->m_idZoneMap.find(zId)->second;
782 if (zone.m_type==RagTimeParserInternal::Zone::Page) {
783 MWAW_DEBUG_MSG(("RagTimeParser::findPagesZones: oops find a page zone %d\n", zId));
784 break;
785 }
786 zone.m_page=page;
787 lists.push_back(zId);
788 zId=zone.m_linkZones[2];
789 }
790 if (lists.empty()) continue;
791 if (m_state->m_pageZonesIdMap.find(page)!=m_state->m_pageZonesIdMap.end()) {
792 MWAW_DEBUG_MSG(("RagTimeParser::findPagesZones: oops already find main zone for %d\n", page));
793 }
794 else
795 m_state->m_pageZonesIdMap[page]=lists;
796 }
797 return true;
798 }
799
800 bool RagTimeParser::readDataZoneHeader(int id, long endPos)
801 {
802 MWAWInputStreamPtr input = getInput();
803 long pos=input->tell();
804 int const vers=version();
805 int const zoneLength=vers>=2 ? 54 : 40;
806 libmwaw::DebugStream f;
807 if (!input->checkPosition(pos+zoneLength)) {
808 MWAW_DEBUG_MSG(("RagTimeParser::readDataZoneHeader: the zone seems too short\n"));
809 return false;
810 }
811 RagTimeParserInternal::Zone zone;
812 zone.m_subType=static_cast<int>(input->readULong(1));
813 bool hasPointer=true;
814 switch (zone.m_subType) {
815 case 0: // checkme
816 zone.m_type=RagTimeParserInternal::Zone::Page;
817 break;
818 case 2:
819 zone.m_type=RagTimeParserInternal::Zone::Text;
820 break;
821 case 3:
822 zone.m_type=RagTimeParserInternal::Zone::Spreadsheet;
823 break;
824 case 4:
825 zone.m_type=RagTimeParserInternal::Zone::Picture;
826 break;
827 case 5:
828 zone.m_type=RagTimeParserInternal::Zone::Line;
829 hasPointer=false;
830 break;
831 case 6:
832 zone.m_type=RagTimeParserInternal::Zone::Chart;
833 break;
834 default:
835 if ((zone.m_subType&0xf)==0xF)
836 hasPointer=false;
837 break;
838 }
839 MWAWEntry entry;
840 entry.setType(zone.getTypeString());
841 entry.setId(id);
842 if (hasPointer) {
843 input->seek(pos+zoneLength-4, librevenge::RVNG_SEEK_SET);
844 auto filePos=long(input->readULong(4));
845 if (filePos==0) {
846 // checkme: is it normal or not ?
847 }
848 else if (filePos>0 && filePos<=long(m_state->m_numDataZone)) {
849 /* TODO: rare but we must manage that, this seems easy for
850 shared text zone, more difficult for other shared zones */
851 zone.m_sharedWith=int(filePos);
852 static bool first=true;
853 if (zone.m_sharedWith && first) {
854 first=false;
855 MWAW_DEBUG_MSG(("RagTimeParser::readDataZoneHeader: find potential shared zones, not implemented\n"));
856 }
857 }
858 else if (filePos<endPos || !input->checkPosition(filePos)) {
859 f << "###";
860 MWAW_DEBUG_MSG(("RagTimeParser::readDataZoneHeader: find an odd zone\n"));
861 }
862 else if (filePos) {
863 // checkme: does vers==1 has some flags which explains +4
864 entry.setBegin(filePos+(vers==1 ? 4 : 0));
865 m_state->m_dataZoneMap.insert
866 (std::multimap<std::string,MWAWEntry>::value_type(entry.type(),entry));
867 }
868 }
869 input->seek(pos+1, librevenge::RVNG_SEEK_SET);
870
871 MWAWGraphicStyle &style=zone.m_style;
872 style.m_lineWidth=float(input->readULong(1))/8.f;
873 int val;
874 if (vers>=2) {
875 val=static_cast<int>(input->readLong(2));
876 if (val) f << "d0=" << val << ",";
877 }
878 for (int i=0; i< 4; ++i) {
879 // fl0=0|80|81|83|a0|b0|c0,fl1=0|2|80,fl2=1|3|5|9|b|19|1d, fl3=0|1|28|2a|38|...|f8
880 val=static_cast<int>(input->readULong(1));
881 switch (i) {
882 case 0:
883 style.m_rotate=float(zone.m_rotation=((val>>2)&3)*90);
884 val&=0xF3;
885 break;
886 case 1:
887 if (val&4) f << "round,";
888 if (val&2) {
889 style.m_shadowOffset=MWAWVec2f(5,5);
890 style.setShadowColor(MWAWColor(128,128,128));
891 }
892 val&=0xF9;
893 break;
894 case 2:
895 if (val&2)
896 f << "selected,";
897 if (val&0x80)
898 zone.m_read32Size=true;
899 val &= 0x7D;
900 break;
901 default:
902 break;
903 }
904
905 if (val) f << "fl" << i << "=" << std::hex << val << std::dec << ",";
906 }
907 float dim[4];
908 for (auto &d : dim) {
909 if (vers >= 2)
910 d=float(input->readLong(4))/65536.f;
911 else
912 d=float(input->readLong(2));
913 }
914 zone.m_dimension=MWAWBox2f(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2]));
915
916 int numZones=m_state->m_numDataZone;
917 val=static_cast<int>(input->readLong(2)); // 0, find also 6 for a line ?
918 if (val) f << "f0=" << val << ",";
919 for (int i=0; i<2; ++i) {
920 val=static_cast<int>(input->readLong(2));
921 if (val < 0 || val > numZones) {
922 MWAW_DEBUG_MSG(("RagTimeParser::readDataZoneHeader: find unexpected link zone\n"));
923 f << "##id[" << i+1 << "]=" << val << ",";
924 continue;
925 }
926 zone.m_linkZones[i==0 ? 2 : 1]=val;
927 }
928 if (zone.m_type==RagTimeParserInternal::Zone::Page)
929 // todo: data probably differs from here
930 zone.m_linkZones[3]=static_cast<int>(input->readLong(2));
931 else {
932 val=static_cast<int>(input->readULong(2)); //f1=(-20)-14|8001
933 if (val==0x8001) // use to indicated the last main zone
934 f << "linked=last,";
935 else if (val >=0 && val <= numZones)
936 zone.m_linkZones[4]=val;
937 else
938 f << "#linked=" << val << ",";
939 }
940 for (int i=0; i<(vers>=2 ? 4 : 2); ++i) { // fl5=0|1|2|3|10, fl6=0|1
941 val=static_cast<int>(input->readULong(1));
942 if (val) f << "fl" << i+5 << "=" << std::hex << val << std::dec << ",";
943 }
944 if (zone.m_type==RagTimeParserInternal::Zone::Page) {
945 zone.m_page=static_cast<int>(input->readLong(2));
946 f << "pg=" << zone.m_page << ",";
947 // todo: data probably differs before
948 }
949 else {
950 val=static_cast<int>(input->readLong(2));
951 if (val) f << "f2=" << val << ",";
952 val=static_cast<int>(input->readLong(2));
953 if (val < 0 || val > numZones) {
954 MWAW_DEBUG_MSG(("RagTimeParser::readDataZoneHeader: find unexpected parent zone\n"));
955 f << "##id[parent]=" << val << ",";
956 }
957 else
958 zone.m_linkZones[0]=val;
959 val=static_cast<int>(input->readULong(1));
960 if (val) f << "fl7=" << std::hex << val << std::dec << ",";
961 if (vers>=2) {
962 val=static_cast<int>(input->readULong(2));
963 if (val) f << "f3=" << val << ",";
964 }
965 auto pattern=static_cast<int>(input->readLong(1));
966 if (pattern!=7) f << "pattern=" << pattern << ",";
967 int percentValues[2]= {100,100};
968 MWAWColor colors[2]= {MWAWColor::black(), MWAWColor::white()};
969 bool hasSurfaceColor=true;
970 bool const isLine=zone.m_type==RagTimeParserInternal::Zone::Line;
971 int const numColorsInFile=isLine ? 1 : 2;
972 for (int i=0; i<numColorsInFile; ++i) {
973 static char const *wh[]= {"line", "surf"};
974 int col;
975 if (vers<2) {
976 percentValues[i]=static_cast<int>(input->readLong(1));
977 col=static_cast<int>(input->readULong(1));
978 if (col==255) col=-1;
979 }
980 else
981 col=static_cast<int>(input->readLong(2))-1;
982 // 255 means default, ...
983 if (percentValues[i]<=0 || percentValues[i]>=100) percentValues[i]=100;
984 else if (i) percentValues[i]=100-percentValues[i];
985 if (percentValues[i]!=100) f << "gray[" << wh[i] << "]=" << percentValues[i] << "%,";
986
987 if (i==1 && col==-1) { // none ?
988 hasSurfaceColor=false;
989 f << "noColors,";
990 continue;
991 }
992 if (col!=-1 && !getColor(col, colors[i]))
993 f << "##color[" << wh[i] << "]=" << col << ",";
994 else
995 f << "color[" << wh[i] << "]=" << colors[i] << ",";
996
997 colors[i]=MWAWColor::barycenter(float(percentValues[i])/100.f,colors[i],
998 1.f-float(percentValues[i])/100.f, i==0 ? MWAWColor::white() : MWAWColor::black());
999 }
1000
1001 if (pattern >=0 && pattern<int(m_state->m_patternList.size())) {
1002 auto pat=m_state->m_patternList[size_t(pattern)];
1003 if (isLine)
1004 style.m_lineColor=MWAWColor::barycenter(1.f-pat.m_percent/100.f,colors[0],
1005 pat.m_percent/100.f, MWAWColor::white());
1006 else {
1007 style.m_lineColor=colors[0];
1008 pat.m_colors[0]=colors[1];
1009 pat.m_colors[1]=colors[0];
1010 MWAWColor col;
1011 if (pat.getUniqueColor(col)) {
1012 if (!col.isWhite())
1013 style.setBackgroundColor(col);
1014 }
1015 else {
1016 style.setPattern(pat);
1017 if (style.m_pattern.getAverageColor(col) && !col.isWhite())
1018 style.setBackgroundColor(col);
1019 }
1020 }
1021 }
1022 else {
1023 MWAW_DEBUG_MSG(("RagTimeParser::readDataZoneHeader: can not find pattern %d\n", pattern));
1024 style.m_lineColor=colors[0];
1025 if (!isLine && hasSurfaceColor)
1026 style.setBackgroundColor(colors[1]);
1027 }
1028 if (isLine) {
1029 if (vers<2) {
1030 for (int i=0; i<2; ++i) {
1031 val=static_cast<int>(input->readULong(1));
1032 if (val) f << "g" << i << "=" << std::hex << val << std::dec << ",";
1033 }
1034 }
1035 val=static_cast<int>(input->readULong(1));
1036 zone.m_arrowFlags=((val>>3)&3);
1037 if (val&4) {
1038 f << "mirrorX,";
1039 float tmp=zone.m_dimension[1][1];
1040 zone.m_dimension.setMax(MWAWVec2f(zone.m_dimension[1][0], zone.m_dimension[0][1]));
1041 zone.m_dimension.setMin(MWAWVec2f(zone.m_dimension[0][0], tmp));
1042 }
1043 val &=0xE3;
1044 if (val) f << "flA4=" << std::hex << val << std::dec << ",";
1045 // not sure are to read the end but as I find 00c0000000: vertical and 0000004000: horizontal, let try
1046 val=static_cast<int>(input->readULong(1));
1047 if (val) f << "g2=" << val << ",";
1048 int unkn[2];
1049 for (auto &u : unkn) u=static_cast<int>(input->readULong(2));
1050 if (unkn[0]==0xc000 && unkn[1]==0) {
1051 f << "vertical,";
1052 zone.m_dimension.setMax(MWAWVec2f(zone.m_dimension[0][0], zone.m_dimension[1][1]));
1053 }
1054 else if (unkn[0]==0 && unkn[1]==0x4000) {
1055 f << "horizontal,";
1056 zone.m_dimension.setMax(MWAWVec2f(zone.m_dimension[1][0], zone.m_dimension[0][1]));
1057 }
1058 }
1059 else {
1060 val=static_cast<int>(input->readULong(1));
1061 if (val!=100) f << "gray[text]=" << val << "%,";
1062 val=static_cast<int>(input->readULong(1));
1063 if (val!=255 && !getColor(val, zone.m_fontColor))
1064 f << "##color[text]=" << val << ",";
1065 }
1066 }
1067 zone.m_extra=f.str();
1068 m_state->m_idZoneMap[id]=zone;
1069 if (id>m_state->m_actualZoneId) m_state->m_actualZoneId=id;
1070 f.str("");
1071 f << "Zones-Z" << id << ":" << zone;
1072
1073 ascii().addDelimiter(input->tell(),'|');
1074 ascii().addPos(pos);
1075 ascii().addNote(f.str().c_str());
1076 input->seek(pos+zoneLength, librevenge::RVNG_SEEK_SET);
1077 return true;
1078 }
1079
1080 bool RagTimeParser::findRsrcZones()
1081 {
1082 MWAWInputStreamPtr input = getInput();
1083 libmwaw::DebugStream f;
1084 long pos=input->tell();
1085 auto dSz=static_cast<int>(input->readULong(2));
1086 f << "Entries(RsrcMap):";
1087 if ((dSz%10)!=2 || !input->checkPosition(pos+2+dSz)) {
1088 MWAW_DEBUG_MSG(("RagTimeParser::findRsrcZones: find odd size\n"));
1089 f << "###";
1090 ascii().addPos(pos);
1091 ascii().addNote(f.str().c_str());
1092 return false;
1093 }
1094 // the resource map
1095 int N=dSz/10;
1096 f << "N=" << N << ",";
1097 ascii().addPos(pos);
1098 ascii().addNote(f.str().c_str());
1099 std::vector<MWAWEntry> entriesList;
1100 std::set<std::string> seensSet;
1101 for (int i=0; i<N; ++i) {
1102 long fPos=input->tell();
1103 f.str("");
1104 f << "RsrcMap[" << i << "]:";
1105 MWAWEntry entry;
1106 auto depl=long(input->readULong(4));
1107 entry.setBegin(pos+depl+2);
1108 std::string what("rsrc");
1109 for (int c=0; c<4; ++c) {
1110 auto ch=char(input->readULong(1));
1111 if (ch==' ') what += '_';
1112 else what+=ch;
1113 }
1114 if (what=="rsrcCHTa") what="Color";
1115 else if (what=="rsrcFHfl") what="FontName";
1116 else if (what=="rsrcFHsl") what="CharProp";
1117 else if (what=="rsrcFLin") what="Link";
1118 else if (what=="rsrcFoTa") what="NumFormat";
1119 else if (what=="rsrcRTml") what="Macro";
1120 /* not really sure, find in v3.2 with value:
1121 id=2:0011000200000000000c00010647656e6576610000
1122 id=4:000e00042e0000060000000c000000000000
1123 id=5:000a00050000000d000100000000
1124 id=6:00060006000000000000
1125 id=7:00060007000000030000
1126 id=8:00060008000000020000
1127 */
1128 else if (what=="rsrcRtPr") what="Reserved";
1129 entry.setType(what);
1130 entry.setId(static_cast<int>(input->readLong(2)));
1131 f << what << "[" << entry.id() << "],";
1132 // no data rsrc seems to exists, so...
1133 if (depl) {
1134 f << std::hex << entry.begin() << std::dec << ",";
1135 if (!entry.begin() || !input->checkPosition(entry.begin())) {
1136 f << "###";
1137 MWAW_DEBUG_MSG(("RagTimeParser::findRsrcZones: the entry position seems bad\n"));
1138 entry=MWAWEntry();
1139 }
1140 }
1141 else
1142 entry=MWAWEntry();
1143 entriesList.push_back(entry);
1144 if (!entry.type().empty())
1145 seensSet.insert(entry.type());
1146 input->seek(fPos+10, librevenge::RVNG_SEEK_SET);
1147 ascii().addPos(fPos);
1148 ascii().addNote(f.str().c_str());
1149 }
1150 /* FIXME: some rsrc does not have a name, ie. have " " as name,
1151 let use an heuristic to name them...
1152 */
1153 #ifdef DEBUG
1154 int n=0;
1155 #endif
1156 for (auto &entry : entriesList) {
1157 #ifdef DEBUG
1158 ++n;
1159 #endif
1160 if (entry.begin()<=0) continue;
1161 if (entry.type() != "rsrc____") {
1162 m_state->m_RSRCZoneMap.insert(std::multimap<std::string,MWAWEntry>::value_type(entry.type(),entry));
1163 continue;
1164 }
1165 entry.setType("rsrcUnamed");
1166 RagTimeStruct::ResourceList zone;
1167 if (zone.read(input, entry) && zone.m_type!=RagTimeStruct::ResourceList::Undef) {
1168 std::string newType("rsrc");
1169 newType+=RagTimeStruct::ResourceList::getName(zone.m_type);
1170 if (seensSet.find(newType)==seensSet.end()) {
1171 MWAW_DEBUG_MSG(("RagTimeParser::findRsrcZones: set the rsrc zone %d to %s\n", int(n-1), newType.c_str()));
1172 entry.setType(newType);
1173 }
1174 }
1175 entry.setParsed(false);
1176 m_state->m_RSRCZoneMap.insert(std::multimap<std::string,MWAWEntry>::value_type(entry.type(),entry));
1177 }
1178 return true;
1179 }
1180 ////////////////////////////////////////////////////////////
1181 //
1182 // Low level
1183 //
1184 ////////////////////////////////////////////////////////////
1185
1186 ////////////////////////////////////////////////////////////
1187 // picture zone
1188 ////////////////////////////////////////////////////////////
1189 bool RagTimeParser::readPictZone(MWAWEntry &entry)
1190 {
1191 MWAWInputStreamPtr input = getInput();
1192 long pos=entry.begin();
1193 int dataFieldSize=getZoneDataFieldSize(entry.id());
1194 if (pos<=0 || !input->checkPosition(pos+0x48+dataFieldSize)) {
1195 MWAW_DEBUG_MSG(("RagTimeParser::readPictZone: the position seems bad\n"));
1196 return false;
1197 }
1198 if (version()<2) {
1199 MWAW_DEBUG_MSG(("RagTimeParser::readPictZone: must not be called for v1-2... file\n"));
1200 return false;
1201 }
1202 entry.setParsed(true);
1203 input->seek(pos, librevenge::RVNG_SEEK_SET);
1204 libmwaw::DebugStream f;
1205 f << "Entries(PictZone):";
1206 auto dSz=static_cast<int>(input->readULong(dataFieldSize));
1207 long endPos=pos+dataFieldSize+dSz;
1208 if (dSz<76 || !input->checkPosition(endPos)) {
1209 MWAW_DEBUG_MSG(("RagTimeParser::readPictZone: the header size seems bad\n"));
1210 f << "###dSz=" << dSz << ",";
1211 ascii().addPos(pos);
1212 ascii().addNote(f.str().c_str());
1213 return false;
1214 }
1215 auto headerSz=static_cast<int>(input->readULong(2));
1216 if ((headerSz&0x8000)) // default
1217 headerSz&=0x7FFF;
1218 else
1219 f << "noHeaderflags,";
1220 if (headerSz>dSz) {
1221 MWAW_DEBUG_MSG(("RagTimeParser::readPictZone: the header size seems bad\n"));
1222 f << "###";
1223 ascii().addPos(pos);
1224 ascii().addNote(f.str().c_str());
1225 return false;
1226 }
1227
1228 RagTimeParserInternal::Picture pict;
1229 pict.m_headerPos=entry.begin();
1230
1231 int val;
1232 for (int i=0; i<6; ++i) { // f1=0|1|9|10|20
1233 val=static_cast<int>(input->readULong(2));
1234 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
1235 }
1236 int bitmapDim[4];
1237 for (auto &d : bitmapDim) d=static_cast<int>(input->readLong(2));
1238 if (bitmapDim[0]||bitmapDim[1]||bitmapDim[2]||bitmapDim[3])
1239 f << "dim[bitmap?]=" << bitmapDim[1] << "x" << bitmapDim[0] << "<->"
1240 << bitmapDim[3] << "x" << bitmapDim[2] << ",";
1241
1242 float dim[4];
1243 for (auto &d: dim) d=float(input->readULong(4))/65536.f;
1244 pict.m_dim=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
1245 f << "dim=" << pict.m_dim << ",";
1246 for (int i=0; i<2; ++i) {
1247 float dim2[2];
1248 for (auto &d : dim2) d=float(input->readULong(4))/65536.f;
1249 if (dim2[0]<dim[2]-dim[0] || dim2[0]>dim[2]-dim[0] ||
1250 dim2[1]<dim[3]-dim[1] || dim2[1]>dim[3]-dim[1])
1251 f << "dim" << i+1 << "=" << dim2[1] << "x" << dim2[0] << ",";
1252 }
1253 /* checkme: no sure what the different type of pict can be
1254 - pict[bitmap]: f4=1-4, f5=1
1255 - pict: f5=1|3
1256 */
1257 for (int i=0; i<7; ++i) {
1258 val=static_cast<int>(input->readULong(2));
1259 if (val) f << "g" << i << "=" << val << ",";
1260 }
1261 pict.m_type=static_cast<int>(input->readULong(1));
1262 switch (pict.m_type) {
1263 case 2:
1264 f << "pict,";
1265 break;
1266 case 3:
1267 f << "pict+label,";
1268 break;
1269 case 6:
1270 f << "pict[bitmap],";
1271 break;
1272 default:
1273 MWAW_DEBUG_MSG(("RagTimeParser::readPictZone: find unexpected type\n"));
1274 f << "#type=" << pict.m_type << ",";
1275 break;
1276 }
1277
1278 ascii().addDelimiter(input->tell(),'|');
1279
1280 input->seek(pos+72+dataFieldSize, librevenge::RVNG_SEEK_SET);
1281 ascii().addDelimiter(input->tell(),'|');
1282 auto pictSz=long(input->readULong(2));
1283 if (headerSz+pictSz>dSz) {
1284 MWAW_DEBUG_MSG(("RagTimeParser::readPictZone: the pict size seems bad\n"));
1285 f << "###pictSz=" << std::hex << pictSz << std::dec << ",";
1286 ascii().addPos(pos);
1287 ascii().addNote(f.str().c_str());
1288 return false;
1289 }
1290 ascii().addPos(pos);
1291 ascii().addNote(f.str().c_str());
1292
1293 input->seek(pos+dataFieldSize+headerSz, librevenge::RVNG_SEEK_SET);
1294 pos=input->tell();
1295
1296 pict.m_pos.setBegin(pos);
1297 pict.m_pos.setLength(pictSz);
1298 m_state->m_idPictureMap[entry.id()]=pict;
1299
1300 input->seek(pos+pictSz, librevenge::RVNG_SEEK_SET);
1301 pos=input->tell();
1302 if (pos!=endPos) {
1303 MWAW_DEBUG_MSG(("RagTimeParser::readPictZone: find some extra data\n"));
1304 ascii().addPos(pos);
1305 ascii().addNote("PictZone[end]");
1306 }
1307 return true;
1308 }
1309
1310 bool RagTimeParser::readPictZoneV2(MWAWEntry &entry)
1311 {
1312 if (version()>=2) {
1313 MWAW_DEBUG_MSG(("RagTimeParser::readPictZoneV2: must not be called for v3... file\n"));
1314 return false;
1315 }
1316 MWAWInputStreamPtr input = getInput();
1317 long pos=entry.begin();
1318 if (pos<=0) {
1319 MWAW_DEBUG_MSG(("RagTimeParser::readPictZoneV2: the position seems bad\n"));
1320 return false;
1321 }
1322 entry.setParsed(true);
1323 input->seek(pos, librevenge::RVNG_SEEK_SET);
1324 libmwaw::DebugStream f;
1325 f << "Entries(PictZone):";
1326 auto dSz=static_cast<int>(input->readULong(2));
1327 long endPos=pos+2+dSz;
1328 if (dSz<0x24 || !input->checkPosition(endPos)) {
1329 MWAW_DEBUG_MSG(("RagTimeParser::readPictZoneV2: the size seems bad\n"));
1330 f << "###";
1331 ascii().addPos(pos);
1332 ascii().addNote(f.str().c_str());
1333 return false;
1334 }
1335 ascii().addPos(endPos);
1336 ascii().addNote("_");
1337 auto headerSz=static_cast<int>(input->readULong(2));
1338 long endHeader=pos+2+headerSz;
1339 if (headerSz<0x24 || !input->checkPosition(endHeader)) {
1340 MWAW_DEBUG_MSG(("RagTimeParser::readPictZoneV2: the header size seems bad\n"));
1341 f << "###";
1342 ascii().addPos(pos);
1343 ascii().addNote(f.str().c_str());
1344 return false;
1345 }
1346
1347 RagTimeParserInternal::Picture pict;
1348 pict.m_headerPos=entry.begin();
1349 int dim[4];
1350 for (auto &d : dim) d=static_cast<int>(input->readULong(2));
1351 pict.m_dim=MWAWBox2f(MWAWVec2f(float(dim[1]),float(dim[0])),MWAWVec2f(float(dim[3]),float(dim[2])));
1352 f << "dim=" << pict.m_dim << ",";
1353 for (int i=0; i<2; ++i) {
1354 int dim2[2];
1355 for (auto &d : dim2) d=static_cast<int>(input->readULong(2));
1356 if (dim2[0]!=dim[2]-dim[0] || dim2[1]!=dim[3]-dim[1])
1357 f << "dim" << i+1 << "=" << dim2[1] << "x" << dim2[0] << ",";
1358 }
1359 /* find for
1360 - bitmap: headerSz=24, f2=1, f7=1
1361 - pict simple: headerSz=24, f2=f7=2
1362 - pict+link: headerSz=78, f2=4, f4=80, f7=3
1363 */
1364 for (int i=0; i<6; ++i) {
1365 auto val=static_cast<int>(input->readULong(2));
1366 if (val) f << "f" << i+1 << "=" << val << ",";
1367 }
1368 pict.m_type=static_cast<int>(input->readLong(2));
1369 switch (pict.m_type) {
1370 case 1:
1371 f << "bitmap,";
1372 break;
1373 case 2:
1374 f << "pict,";
1375 break;
1376 case 3:
1377 f << "pict+eps,";
1378 break;
1379 default:
1380 MWAW_DEBUG_MSG(("RagTimeParser::readPictZoneV2: find unexpected type\n"));
1381 f << "#type=" << pict.m_type << ",";
1382 break;
1383 }
1384 f << "id?=" << std::hex << input->readULong(4) << std::dec << ",";
1385 ascii().addPos(pos);
1386 ascii().addNote(f.str().c_str());
1387
1388 if (headerSz>0x24) {
1389 f.str("");
1390 f << "PictZone[Link]:";
1391 if (headerSz>0x24+34) {
1392 f << "id2=" << std::hex << input->readULong(4) << std::dec << ",";
1393 int val;
1394 for (int i=0; i<14; ++i) {
1395 static int const expected[]= {0,0x1f22,0xffe8,0xff80,0x2c,0x3f,0x2d0,0x2d0,2,0x2d,0x3c,0,0x16,0x9dba};
1396 val=static_cast<int>(input->readULong(2));
1397 if (val!=expected[i])
1398 f << "f" << i << "=" << std::hex << val << std::dec << ",";
1399 }
1400 val=static_cast<int>(input->readULong(1));
1401 if (val!=0x5d) f << "f14=" << std::hex << val << std::dec << ",";
1402 auto sSz=static_cast<int>(input->readULong(1));
1403 if (input->tell()+sSz<=endHeader) {
1404 std::string text("");
1405 for (int i=0; i<sSz; ++i) text+=char(input->readULong(1));
1406 f << "file=" << text << ",";
1407 }
1408 else {
1409 MWAW_DEBUG_MSG(("RagTimeParser::readPictZoneV2: can not read the file name\n"));
1410 f << "#sz=" << sSz << ",";
1411 }
1412 }
1413 ascii().addPos(pos+2+0x24);
1414 ascii().addNote(f.str().c_str());
1415 }
1416 input->seek(endHeader, librevenge::RVNG_SEEK_SET);
1417
1418 pos=input->tell();
1419 pict.m_pos.setBegin(pos);
1420 pict.m_pos.setEnd(endPos);
1421 m_state->m_idPictureMap[entry.id()]=pict;
1422
1423 return true;
1424 }
1425
1426 ////////////////////////////////////////////////////////////
1427 // color map
1428 ////////////////////////////////////////////////////////////
1429 bool RagTimeParser::readColorsMap()
1430 {
1431 // we must read the colors map in order: first the 0th color map, ...
1432 for (int i=0; i<3; ++i) {
1433 auto it=m_state->m_RSRCZoneMap.lower_bound("Color");
1434 while (it!=m_state->m_RSRCZoneMap.end() && it->first=="Color") {
1435 MWAWEntry &entry=it++->second;
1436 if ((i<2 && i==entry.id()) || (i>=2 && !entry.isParsed()))
1437 readColorTable(entry);
1438 }
1439 }
1440 return true;
1441 }
1442
1443 bool RagTimeParser::readColorMapV2(MWAWEntry &entry)
1444 {
1445 MWAWInputStreamPtr input = getInput();
1446 long pos=entry.begin();
1447 if (pos<=0 || !input->checkPosition(pos+8)) {
1448 MWAW_DEBUG_MSG(("RagTimeParser::readColorMapV2: the position seems bad\n"));
1449 return false;
1450 }
1451 if (version()>=2) {
1452 MWAW_DEBUG_MSG(("RagTimeParser::readColorMapV2: must only be called for v1-2... file\n"));
1453 return false;
1454 }
1455 entry.setParsed(true);
1456 input->seek(pos, librevenge::RVNG_SEEK_SET);
1457 libmwaw::DebugStream f;
1458 f << "Entries(ColorMap):";
1459 auto val=static_cast<int>(input->readLong(2)); // always 0?
1460 if (val) f << "f0=" << val << ",";
1461 auto dSz=static_cast<int>(input->readULong(2));
1462 int N[3];
1463 for (auto &nVal : N) nVal=static_cast<int>(input->readLong(2));
1464 f << "numColors=" << N[1] << ",";
1465 if (N[0]+1!=N[1]) {
1466 MWAW_DEBUG_MSG(("RagTimeParser::readColorMapV2: arghhs N[0]+1!=N[1]\n"));
1467 f << "###num?=" << N[0] << ",";
1468 }
1469 if (N[2]>=0) f << "maxptr=" << N[2] << ",";
1470 long endPos=pos+4+dSz;
1471 if (dSz<6+10*N[1]+(N[2]+1)*4+N[1] || !input->checkPosition(endPos)) {
1472 MWAW_DEBUG_MSG(("RagTimeParser::readColorMapV2: the size seems bad\n"));
1473 f << "###";
1474 ascii().addPos(pos);
1475 ascii().addNote(f.str().c_str());
1476 return false;
1477 }
1478 ascii().addPos(pos);
1479 ascii().addNote(f.str().c_str());
1480 std::vector<MWAWColor> colorList;
1481 for (int i=0; i<=N[0]; ++i) {
1482 //-2: white, -1: black, ...
1483 pos=input->tell();
1484 f.str("");
1485 f << "ColorMap-" << i << ":";
1486 unsigned char col[3];
1487 for (auto &c : col) c = static_cast<unsigned char>(input->readULong(2)>>8);
1488 MWAWColor color(col[0],col[1],col[2]);
1489 colorList.push_back(color);
1490 f << "col=" << color << ",";
1491 val=static_cast<int>(input->readULong(2)); // 1|2|3|4|22
1492 if (val) f << "used=" << val << ",";
1493 f << "id?=" << input->readLong(2) << ",";
1494 ascii().addPos(pos);
1495 ascii().addNote(f.str().c_str());
1496 }
1497 m_state->m_idColorsMap[0]=colorList;
1498 for (int i=0; i<=N[2]; ++i) {
1499 pos=input->tell();
1500 f.str("");
1501 f << "ColorMap-A" << i << ":";
1502 val=static_cast<int>(input->readULong(2)); // the index in the text?
1503 f << "text[pos]?=" << std::hex << val << std::dec << ",";
1504 val=static_cast<int>(input->readULong(2)); // 1|2|4 color used?
1505 f << "id?=" << val << ",";
1506 ascii().addPos(pos);
1507 ascii().addNote(f.str().c_str());
1508 }
1509 for (int i=0; i< N[1]; ++i) { // the color name
1510 pos=input->tell();
1511 f.str("");
1512 f << "ColorMap-" << i << ":name=";
1513 auto fSz=static_cast<int>(input->readULong(1));
1514 if (pos+1+fSz>endPos) {
1515 MWAW_DEBUG_MSG(("RagTimeParser::readColorMapV2: can not read a string\n"));
1516 f << "###";
1517 ascii().addPos(pos);
1518 ascii().addNote(f.str().c_str());
1519 return false;
1520 }
1521 std::string text("");
1522 for (int c=0; c<fSz; ++c) text+=char(input->readULong(1));
1523 f << text;
1524 ascii().addPos(pos);
1525 ascii().addNote(f.str().c_str());
1526 }
1527 pos=input->tell();
1528 if (pos!=endPos) {
1529 MWAW_DEBUG_MSG(("RagTimeParser::readColorMapV2: find some extra data\n"));
1530 ascii().addPos(pos);
1531 ascii().addNote("ColorMap-end:###");
1532 }
1533 return true;
1534 }
1535
1536 ////////////////////////////////////////////////////////////
1537 // print info
1538 ////////////////////////////////////////////////////////////
1539 bool RagTimeParser::readPrintInfo(MWAWEntry &entry, bool inRSRCFork)
1540 {
1541 MWAWInputStreamPtr input = inRSRCFork ? getRSRCParser()->getInput() : getInput();
1542 long pos=entry.begin();
1543 if (pos<=0 || !input->checkPosition(pos+2+120)) {
1544 MWAW_DEBUG_MSG(("RagTimeParser::readPrintInfo: the position seems bad\n"));
1545 return false;
1546 }
1547 entry.setParsed(true);
1548 input->seek(pos, librevenge::RVNG_SEEK_SET);
1549 libmwaw::DebugFile &ascFile=inRSRCFork ? getRSRCParser()->ascii() : ascii();
1550 libmwaw::DebugStream f;
1551 f << "Entries(rsrcPREC):";
1552 long dSz=inRSRCFork ? entry.length() : static_cast<int>(input->readULong(2));
1553 long endPos=input->tell()+dSz;
1554 libmwaw::PrinterInfo info;
1555 if (dSz<120 || !input->checkPosition(endPos) || !info.read(input)) {
1556 MWAW_DEBUG_MSG(("RagTimeParser::readPrintInfo: the zone seems too short\n"));
1557 f << "###";
1558 ascFile.addPos(pos);
1559 ascFile.addNote(f.str().c_str());
1560 return false;
1561 }
1562 f << info;
1563
1564 MWAWVec2i paperSize = info.paper().size();
1565 MWAWVec2i pageSize = info.page().size();
1566 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
1567 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
1568
1569 // define margin from print info
1570 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
1571 MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1);
1572
1573 // move margin left | top
1574 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
1575 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
1576 lTopMargin -= MWAWVec2i(decalX, decalY);
1577 rBotMargin += MWAWVec2i(decalX, decalY);
1578
1579 // decrease right | bottom
1580 int rightMarg = rBotMargin.x() -10;
1581 if (rightMarg < 0) rightMarg=0;
1582 int botMarg = rBotMargin.y() -50;
1583 if (botMarg < 0) botMarg=0;
1584
1585 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
1586 getPageSpan().setMarginBottom(botMarg/72.0);
1587 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
1588 getPageSpan().setMarginRight(rightMarg/72.0);
1589 getPageSpan().setFormLength(paperSize.y()/72.);
1590 getPageSpan().setFormWidth(paperSize.x()/72.);
1591 ascFile.addPos(pos);
1592 ascFile.addNote(f.str().c_str());
1593
1594 pos=input->tell();
1595 if (pos==endPos) return true;
1596 // TODO read the last part 0x20(size?) + page dim + some flags
1597 f.str("");
1598 f << "rsrcPREC[data]:";
1599 ascFile.addPos(pos);
1600 ascFile.addNote(f.str().c_str());
1601
1602 return true;
1603 }
1604
1605 ////////////////////////////////////////////////////////////
1606 // unknown zone
1607 ////////////////////////////////////////////////////////////
1608 bool RagTimeParser::readZone6(MWAWEntry &entry)
1609 {
1610 MWAWInputStreamPtr input = getInput();
1611 long pos=entry.begin();
1612 if (pos<=0 || !input->checkPosition(pos+0x66)) {
1613 MWAW_DEBUG_MSG(("RagTimeParser::readZone6: the position seems bad\n"));
1614 return false;
1615 }
1616 if (version()<2) {
1617 MWAW_DEBUG_MSG(("RagTimeParser::readZone6: must not be called for v1-2... file\n"));
1618 return false;
1619 }
1620 entry.setParsed(true);
1621 input->seek(pos, librevenge::RVNG_SEEK_SET);
1622 libmwaw::DebugStream f;
1623 f << "Entries(Zone6):";
1624 auto dSz=static_cast<int>(input->readULong(2));
1625 long endPos=pos+2+dSz;
1626 if (dSz<0x62 || !input->checkPosition(endPos)) {
1627 MWAW_DEBUG_MSG(("RagTimeParser::readZone6: the size seems bad\n"));
1628 f << "###";
1629 ascii().addPos(pos);
1630 ascii().addNote(f.str().c_str());
1631 return false;
1632 }
1633 std::set<long> beginZones;
1634 long zonesList[4]= {0,0,0,0};
1635 int val;
1636 beginZones.insert(endPos);
1637 auto ptr=long(input->readULong(4));
1638 val=static_cast<int>(input->readULong(2));
1639 if (ptr) {
1640 f << "zone0=" << std::hex << pos+2+ptr << std::dec << "[" << val << "],";
1641 if (pos+2+ptr>endPos) {
1642 MWAW_DEBUG_MSG(("RagTimeParser::readZone6: the zone 0 seems bad\n"));
1643 f << "###";
1644 }
1645 else {
1646 beginZones.insert(pos+2+ptr);
1647 zonesList[0]=pos+2+ptr;
1648 }
1649 }
1650 for (int i=0; i<2; ++i) {
1651 val=static_cast<int>(input->readULong(2));
1652 if (val) f << "f" << i << "=" << val << ",";
1653 }
1654
1655 for (int i=0; i<3; ++i) {
1656 ptr=long(input->readULong(4));
1657 val=static_cast<int>(input->readULong(2));
1658 if (!ptr) continue;
1659 f << "zone" << i+1 << "=" << std::hex << pos+2+ptr << std::dec << "[" << val << "],";
1660 if (pos+2+ptr>endPos) {
1661 MWAW_DEBUG_MSG(("RagTimeParser::readZone6: the zone %d seems bad\n",i+1));
1662 f << "###";
1663 continue;
1664 }
1665 beginZones.insert(pos+2+ptr);
1666 zonesList[i+1]=pos+2+ptr;
1667 }
1668 ascii().addDelimiter(input->tell(),'|');
1669 ascii().addPos(pos);
1670 ascii().addNote(f.str().c_str());
1671
1672 for (int i=0; i<4; ++i) {
1673 pos=zonesList[i];
1674 if (!pos || pos==endPos) continue;
1675 f.str("");
1676 f << "Zone6-" << i << ":";
1677 auto it=beginZones.find(pos);
1678 if (it==beginZones.end() || ++it==beginZones.end()) {
1679 MWAW_DEBUG_MSG(("RagTimeParser::readZone6: can not find the end of zone\n"));
1680 f << "###";
1681 ascii().addPos(pos);
1682 ascii().addNote(f.str().c_str());
1683 continue;
1684 }
1685 long zEndPos=*it;
1686 input->seek(pos, librevenge::RVNG_SEEK_SET);
1687 while (!input->isEnd()) {
1688 pos=input->tell();
1689 if (pos+4>zEndPos) break;
1690 dSz=static_cast<int>(input->readULong(4));
1691 if (pos+4+dSz>zEndPos) {
1692 MWAW_DEBUG_MSG(("RagTimeParser::readZone6: can not determine the end of sub zone\n"));
1693 f << "###";
1694 ascii().addPos(pos);
1695 ascii().addNote(f.str().c_str());
1696 break;
1697 }
1698 input->seek(pos+4+dSz, librevenge::RVNG_SEEK_SET);
1699 ascii().addPos(pos);
1700 ascii().addNote(f.str().c_str());
1701 }
1702 }
1703 return true;
1704 }
1705
1706 bool RagTimeParser::readPageZone(MWAWEntry &entry)
1707 {
1708 MWAWInputStreamPtr input = getInput();
1709 long pos=entry.begin();
1710 if (pos<=0 || !input->checkPosition(pos+22)) {
1711 MWAW_DEBUG_MSG(("RagTimeParser::readPageZone: the position seems bad\n"));
1712 return false;
1713 }
1714 entry.setParsed(true);
1715 input->seek(pos, librevenge::RVNG_SEEK_SET);
1716 libmwaw::DebugStream f;
1717 f << "Entries(PageZone):";
1718 auto dSz=static_cast<int>(input->readULong(2));
1719 long endPos=pos+2+dSz;
1720 if (dSz<20 || !input->checkPosition(endPos)) {
1721 MWAW_DEBUG_MSG(("RagTimeParser::readPageZone: the size seems bad\n"));
1722 f << "###";
1723 ascii().addPos(pos);
1724 ascii().addNote(f.str().c_str());
1725 return false;
1726 }
1727 int val;
1728 for (int i=0; i<4; i++) { // always 0?
1729 val=static_cast<int>(input->readLong(2));
1730 if (val) f << "f" << i << "=" << val << ",";
1731 }
1732 auto fl=static_cast<int>(input->readULong(4)); // 71606|e2c60|e2c68|e6658
1733 f << "flag?=" << std::hex << fl << std::dec << ",";
1734 val=static_cast<int>(input->readULong(4)); // always equal to fl?
1735 if (val!=fl)
1736 f << "flag1?=" << std::hex << val << std::dec << ",";
1737 val=static_cast<int>(input->readLong(2)); // 4|5
1738 if (val!=4) f << "f4=" << val << ",";
1739 val=static_cast<int>(input->readLong(2)); // always 0
1740 if (val) f << "f5=" << val << ",";
1741 if (input->tell()!=endPos)
1742 ascii().addDelimiter(input->tell(),'|');
1743 ascii().addPos(pos);
1744 ascii().addNote(f.str().c_str());
1745 return true;
1746 }
1747
1748 ////////////////////////////////////////////////////////////
1749 // rsrc zone: link
1750 ////////////////////////////////////////////////////////////
1751 bool RagTimeParser::readLinks(MWAWEntry &entry)
1752 {
1753 MWAWInputStreamPtr input = getInput();
1754 long pos=entry.begin();
1755 if (pos<=0 || !input->checkPosition(pos+2+0x26)) {
1756 MWAW_DEBUG_MSG(("RagTimeParser::readLinks: the position seems bad\n"));
1757 return false;
1758 }
1759 entry.setParsed(true);
1760 input->seek(pos, librevenge::RVNG_SEEK_SET);
1761 libmwaw::DebugStream f;
1762 f << "Entries(" << entry.type() << ")[" << entry.id() << "]:";
1763 auto dSz=static_cast<int>(input->readULong(2));
1764 long endPos=pos+2+dSz;
1765 auto headerSz=static_cast<int>(input->readULong(2));
1766 auto fSz=static_cast<int>(input->readULong(2));
1767 auto N=static_cast<int>(input->readULong(2));
1768 f << "N=" << N << ",";
1769 if (headerSz<0x20 || fSz<0x10 || dSz<headerSz+long(N+1)*fSz || !input->checkPosition(endPos)) {
1770 MWAW_DEBUG_MSG(("RagTimeParser::readLinks: the size seems bad\n"));
1771 f << "###";
1772 ascii().addPos(pos);
1773 ascii().addNote(f.str().c_str());
1774 return false;
1775 }
1776 input->seek(pos+2+headerSz, librevenge::RVNG_SEEK_SET);
1777 ascii().addPos(pos);
1778 ascii().addNote(f.str().c_str());
1779
1780 std::set<long> posSet;
1781 posSet.insert(endPos);
1782 for (int i=0; i<=N; ++i) {
1783 pos=input->tell();
1784 f.str("");
1785 f << entry.type() << "-" << i << ":";
1786 auto val=static_cast<int>(input->readLong(2)); // small number
1787 if (val) f << "f0=" << val << ",";
1788 val=static_cast<int>(input->readLong(2)); // always 0?
1789 if (val) f << "f1=" << val << ",";
1790 auto fPos=static_cast<int>(input->readULong(2));
1791 f << "pos[name]=" << std::hex << entry.begin()+2+fPos << std::dec << ",";
1792 posSet.insert(entry.begin()+2+fPos);
1793 val=static_cast<int>(input->readLong(2));
1794 if (val) f << "fId=" << val << ",";
1795 ascii().addDelimiter(input->tell(),'|');
1796 input->seek(pos+fSz, librevenge::RVNG_SEEK_SET);
1797 ascii().addPos(pos);
1798 ascii().addNote(f.str().c_str());
1799 }
1800
1801 for (auto it=posSet.begin(); it!=posSet.end();) {
1802 pos=*(it++);
1803 if (pos>=endPos) break;
1804 long nextPos=it==posSet.end() ? endPos : *it;
1805 f.str("");
1806 f << entry.type() << "[name]:";
1807 input->seek(pos, librevenge::RVNG_SEEK_SET);
1808 std::string name("");
1809 while (!input->isEnd() && input->tell()<nextPos) {
1810 auto c=char(input->readULong(1));
1811 if (c=='\0') break;
1812 name+=c;
1813 }
1814 f << name;
1815 ascii().addPos(pos);
1816 ascii().addNote(f.str().c_str());
1817 }
1818 return true;
1819 }
1820
1821 ////////////////////////////////////////////////////////////
1822 // rsrc zone: numeric format
1823 ////////////////////////////////////////////////////////////
1824 bool RagTimeParser::readFormatsMap()
1825 {
1826 for (auto &it : m_state->m_RSRCZoneMap) {
1827 std::string const &type=it.first;
1828 if (type=="NumFormat")
1829 m_spreadsheetParser->readNumericFormat(it.second);
1830 else if (type.length()>=7 && type.compare(0,6,"rsrcSp")==0)
1831 m_spreadsheetParser->readResource(it.second);
1832 }
1833 return true;
1834 }
1835
1836 bool RagTimeParser::readColorTable(MWAWEntry &entry)
1837 {
1838 MWAWInputStreamPtr input = getInput();
1839 long pos=entry.begin();
1840 if (pos<=0 || !input->checkPosition(pos+2+0x26)) {
1841 MWAW_DEBUG_MSG(("RagTimeParser::readColorTable: the position seems bad\n"));
1842 return false;
1843 }
1844 entry.setParsed(true);
1845 input->seek(pos, librevenge::RVNG_SEEK_SET);
1846 libmwaw::DebugStream f;
1847 std::string what(entry.id()==0 ? "ColorMain" : "ColorList");
1848 if (entry.id()<0 || entry.id()>1) {
1849 MWAW_DEBUG_MSG(("RagTimeParser::readColorTable: find unknown color map\n"));
1850 std::stringstream s;
1851 s << "ColorTable" << entry.id();
1852 what=s.str();
1853 }
1854 f << "Entries(" << what << "):";
1855 auto dSz=static_cast<int>(input->readULong(2));
1856 long endPos=pos+2+dSz;
1857 auto headerSz=static_cast<int>(input->readULong(2));
1858 auto fSz=static_cast<int>(input->readULong(2));
1859 auto N=static_cast<int>(input->readULong(2));
1860 f << "N=" << N << ",";
1861 if (headerSz<0x20 || fSz<0x8 || dSz<headerSz+long(N+1)*fSz || !input->checkPosition(endPos)) {
1862 MWAW_DEBUG_MSG(("RagTimeParser::readColorTable: the size seems bad\n"));
1863 f << "###";
1864 ascii().addPos(pos);
1865 ascii().addNote(f.str().c_str());
1866 return false;
1867 }
1868 bool okSize=(entry.id()==0&&fSz==12)||(entry.id()==1&&fSz==8);
1869 if (!okSize) {
1870 MWAW_DEBUG_MSG(("RagTimeParser::readColorTable: unexpected size for table %d\n", entry.id()));
1871 f << "###";
1872 }
1873 input->seek(pos+2+headerSz, librevenge::RVNG_SEEK_SET);
1874 ascii().addPos(pos);
1875 ascii().addNote(f.str().c_str());
1876
1877 std::set<long> posSet;
1878 posSet.insert(endPos);
1879 std::vector<MWAWColor> colorList;
1880 for (int i=0; i<=N; ++i) {
1881 pos=input->tell();
1882 f.str("");
1883 f << what << "-" << i << ":";
1884 auto val=static_cast<int>(input->readLong(2)); // 0 (except last)
1885 if (val) f << "f0=" << val << ",";
1886 if (i==N) {
1887 }
1888 else if (!okSize)
1889 f << "###";
1890 else if (entry.id()==0) {
1891 auto fPos=static_cast<int>(input->readULong(2));
1892 if (fPos) {
1893 f << "pos[def]=" << std::hex << entry.begin()+2+fPos << std::dec << ",";
1894 posSet.insert(entry.begin()+2+fPos);
1895 }
1896 unsigned char col[3];
1897 for (auto &c : col) c = static_cast<unsigned char>(input->readULong(2)>>8);
1898 MWAWColor color(col[0],col[1],col[2]);
1899 colorList.push_back(color);
1900 f << "col=" << color << ",";
1901 f << "id?=" << input->readLong(2) << ",";
1902 }
1903 else if (entry.id()==1) {
1904 val=static_cast<int>(input->readLong(2));
1905 if (val) f << "used?=" << val << ",";
1906 int colId=static_cast<int>(input->readULong(2))-1;
1907 MWAWColor col=MWAWColor::white();
1908 if (!getColor(colId,col,0) && val) { // ie. if the color is not used, this can be normal
1909 MWAW_DEBUG_MSG(("RagTimeParser::readColorTable: unexpected color id=%d\n", colId));
1910 f << "###";
1911 }
1912 float percent=float(input->readLong(2))/1000.f;
1913 col=MWAWColor::barycenter(percent,col,1.f-percent, MWAWColor::white());
1914 colorList.push_back(col);
1915 f << col << ",";
1916 }
1917 input->seek(pos+fSz, librevenge::RVNG_SEEK_SET);
1918 ascii().addPos(pos);
1919 ascii().addNote(f.str().c_str());
1920 }
1921 m_state->m_idColorsMap[entry.id()]=colorList;
1922 for (auto it=posSet.begin(); it!=posSet.end();) {
1923 pos=*(it++);
1924 if (pos>=endPos) break;
1925 long nextPos=it==posSet.end() ? endPos : *it;
1926 f.str("");
1927 f << what << "[name]:";
1928 input->seek(pos, librevenge::RVNG_SEEK_SET);
1929 std::string name("");
1930 while (!input->isEnd() && input->tell()<nextPos) {
1931 auto c=char(input->readULong(1));
1932 if (c=='\0') break;
1933 name+=c;
1934 }
1935 f << name;
1936 ascii().addPos(pos);
1937 ascii().addNote(f.str().c_str());
1938 }
1939 return true;
1940 }
1941
1942 bool RagTimeParser::readMacroFormats(MWAWEntry &entry)
1943 {
1944 MWAWInputStreamPtr input = getInput();
1945 long pos=entry.begin();
1946 if (pos<=0 || !input->checkPosition(pos+2+0x26)) {
1947 MWAW_DEBUG_MSG(("RagTimeParser::readMacroFormats: the position seems bad\n"));
1948 return false;
1949 }
1950 entry.setParsed(true);
1951 input->seek(pos, librevenge::RVNG_SEEK_SET);
1952 libmwaw::DebugStream f;
1953 f << "Entries(" << entry.type() << ")[" << entry.id() << "]:";
1954 auto dSz=static_cast<int>(input->readULong(2));
1955 long endPos=pos+2+dSz;
1956 auto headerSz=static_cast<int>(input->readULong(2));
1957 auto fSz=static_cast<int>(input->readULong(2));
1958 auto N=static_cast<int>(input->readULong(2));
1959 f << "N=" << N << ",";
1960 if (headerSz<0x20 || fSz<0x10 || dSz<headerSz+long(N+1)*fSz || !input->checkPosition(endPos)) {
1961 MWAW_DEBUG_MSG(("RagTimeParser::readMacroFormats: the size seems bad\n"));
1962 f << "###";
1963 ascii().addPos(pos);
1964 ascii().addNote(f.str().c_str());
1965 return false;
1966 }
1967 input->seek(pos+2+headerSz, librevenge::RVNG_SEEK_SET);
1968 ascii().addPos(pos);
1969 ascii().addNote(f.str().c_str());
1970
1971 std::set<long> posSet;
1972 posSet.insert(endPos);
1973 for (int i=0; i<=N; ++i) {
1974 pos=input->tell();
1975 f.str("");
1976 f << entry.type() << "-" << i << ":";
1977 auto fPos=static_cast<int>(input->readULong(2));
1978 f << "pos[name]=" << std::hex << entry.begin()+2+fPos << std::dec << ",";
1979 posSet.insert(entry.begin()+2+fPos);
1980 ascii().addDelimiter(input->tell(),'|');
1981 input->seek(pos+fSz, librevenge::RVNG_SEEK_SET);
1982 ascii().addPos(pos);
1983 ascii().addNote(f.str().c_str());
1984 }
1985
1986 for (auto it=posSet.begin(); it!=posSet.end();) {
1987 pos=*(it++);
1988 if (pos>=endPos) break;
1989 long nextPos=it==posSet.end() ? endPos : *it;
1990 f.str("");
1991 f << entry.type() << "[name]:";
1992 input->seek(pos, librevenge::RVNG_SEEK_SET);
1993 std::string name("");
1994 while (!input->isEnd() && input->tell()<nextPos) {
1995 auto c=char(input->readULong(1));
1996 if (c=='\0') break;
1997 name+=c;
1998 }
1999 f << name;
2000 ascii().addPos(pos);
2001 ascii().addNote(f.str().c_str());
2002 }
2003 return true;
2004 }
2005
2006 ////////////////////////////////////////////////////////////
2007 // other unknown rsrc zone
2008 ////////////////////////////////////////////////////////////
2009 bool RagTimeParser::readRsrcCalc(MWAWEntry &entry)
2010 {
2011 MWAWInputStreamPtr input = getInput();
2012 long pos=entry.begin();
2013 if (pos<=0 || !input->checkPosition(pos+2+0x26)) {
2014 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcCalc: the position seems bad\n"));
2015 return false;
2016 }
2017 entry.setParsed(true);
2018 input->seek(pos, librevenge::RVNG_SEEK_SET);
2019 libmwaw::DebugStream f;
2020 f << "Entries(rsrcCalc)[" << entry.id() << "]:";
2021 auto dSz=static_cast<int>(input->readULong(2));
2022 auto val=static_cast<int>(input->readLong(2)); // always 0
2023 if (val) f << "f0=" << val << ",";
2024 val=static_cast<int>(input->readLong(2)); // a small number
2025 if (val) f << "f1=" << val << ",";
2026 auto N=static_cast<int>(input->readLong(2));
2027 f << "N=" << N << ",";
2028 long endPos=pos+2+dSz;
2029 if (dSz!=24+N*26 || !input->checkPosition(endPos)) {
2030 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcCalc: the size seems bad\n"));
2031 f << "###";
2032 ascii().addPos(pos);
2033 ascii().addNote(f.str().c_str());
2034 return false;
2035 }
2036 input->seek(pos+2+24, librevenge::RVNG_SEEK_SET);
2037 ascii().addPos(pos);
2038 ascii().addNote(f.str().c_str());
2039
2040 for (int i=0; i<N; ++i) {
2041 pos=input->tell();
2042 f.str("");
2043 f << "rsrcCalc-" << i << ":";
2044
2045 input->seek(pos+26, librevenge::RVNG_SEEK_SET);
2046 ascii().addPos(pos);
2047 ascii().addNote(f.str().c_str());
2048 }
2049 return true;
2050 }
2051
2052 bool RagTimeParser::readRsrcFHwl(MWAWEntry &entry)
2053 {
2054 MWAWInputStreamPtr input = getInput();
2055 long pos=entry.begin();
2056 if (pos<=0 || !input->checkPosition(pos+2+0x26)) {
2057 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcFHwl: the position seems bad\n"));
2058 return false;
2059 }
2060 entry.setParsed(true);
2061 input->seek(pos, librevenge::RVNG_SEEK_SET);
2062 libmwaw::DebugStream f;
2063 f << "Entries(rsrcFHwl)[" << entry.id() << "]:";
2064 auto dSz=static_cast<int>(input->readULong(2));
2065 long endPos=pos+2+dSz;
2066 auto headerSz=static_cast<int>(input->readULong(2));
2067 auto fSz=static_cast<int>(input->readULong(2));
2068 auto N=static_cast<int>(input->readULong(2));
2069 f << "N=" << N << ",";
2070 if (headerSz<0x26 || dSz!=headerSz+long(N+1)*fSz || !input->checkPosition(endPos)) {
2071 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcFHwl: the size seems bad\n"));
2072 f << "###";
2073 ascii().addPos(pos);
2074 ascii().addNote(f.str().c_str());
2075 return false;
2076 }
2077 for (int i=0; i<8; ++i) {
2078 auto val=static_cast<int>(input->readLong(2));
2079 static int const expected[]= {0x200, 2, 0, 0, 0, 0, 5, 0/* small number*/};
2080 if (val!=expected[i])
2081 f << "f" << i+2 << "=" << val << ",";
2082 }
2083 for (int i=0; i<8; ++i) { // g4=1, g6=17|21|2b|7b, g7=0x208
2084 auto val=static_cast<int>(input->readLong(2));
2085 f << "g" << i << "=" << val << ",";
2086 }
2087 input->seek(pos+2+headerSz, librevenge::RVNG_SEEK_SET);
2088 ascii().addPos(pos);
2089 ascii().addNote(f.str().c_str());
2090
2091 for (int i=0; i<=N; ++i) {
2092 pos=input->tell();
2093 f.str("");
2094 f << "rsrcFHwl-" << i << ":";
2095 /* a small number, except for the last value
2096 so, maybe the interesting data are at the end of the bloc, in pos+0x210-4 ?
2097 or the block are subdivided differently
2098 */
2099 f << "N=" << input->readLong(2) << ",";
2100 input->seek(pos+fSz, librevenge::RVNG_SEEK_SET);
2101 ascii().addPos(pos);
2102 ascii().addNote(f.str().c_str());
2103 }
2104 return true;
2105 }
2106
2107 bool RagTimeParser::readRsrcBeDc(MWAWEntry &entry)
2108 {
2109 MWAWInputStreamPtr input = getInput();
2110 long pos=entry.begin();
2111 if (pos<=0 || !input->checkPosition(pos+2+52)) {
2112 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcBeDc: the position seems bad\n"));
2113 return false;
2114 }
2115 entry.setParsed(true);
2116 input->seek(pos, librevenge::RVNG_SEEK_SET);
2117 libmwaw::DebugStream f;
2118 f << "Entries(rsrcBeDc)[" << entry.id() << "]:";
2119 auto dSz=static_cast<int>(input->readULong(2));
2120 long endPos=pos+2+dSz;
2121 if (dSz<52 || !input->checkPosition(endPos)) {
2122 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcBeDc: the size seems bad\n"));
2123 f << "###";
2124 ascii().addPos(pos);
2125 ascii().addNote(f.str().c_str());
2126 return false;
2127 }
2128 auto val=static_cast<int>(input->readLong(2)); // alway 1?
2129 if (val!=1) f << "f0=" << val << ",";
2130 val=static_cast<int>(input->readLong(2)); // alway 0?
2131 if (val) f << "f1=" << val << ",";
2132 for (int i=0; i<2; ++i) { // f2=0|1|11a0|349c|5c74|a250|e09c, f3=0|1|a|17|..2014
2133 val=static_cast<int>(input->readULong(2));
2134 if (val) f << "f" << i+2 << "=" << std::hex << val << std::dec << ",";
2135 }
2136 f << "id?=" << std::hex << input->readULong(4) << std::dec << ","; // a really big number
2137 for (int i=0; i<7; ++i) { // all 0, or g2=1,g3=157,g4=13,g5=1f5|275: (g2,g3,g4,g5) a dim?
2138 val=static_cast<int>(input->readULong(2));
2139 if (val) f << "g" << i << "=" << val << ",";
2140 }
2141 for (int i=0; i<13; ++i) { // begin by small int, h2,h5: maybe two bool,
2142 val=static_cast<int>(input->readLong(2));
2143 if (val) f << "h" << i << "=" << val << ",";
2144 }
2145 if (input->tell()!=endPos)
2146 ascii().addDelimiter(input->tell(),'|');
2147 ascii().addPos(pos);
2148 ascii().addNote(f.str().c_str());
2149 return true;
2150 }
2151
2152 bool RagTimeParser::readRsrcBtch(MWAWEntry &entry)
2153 {
2154 MWAWInputStreamPtr input = getInput();
2155 long pos=entry.begin();
2156 if (pos<=0 || !input->checkPosition(pos+2+6)) {
2157 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcBtch: the position seems bad\n"));
2158 return false;
2159 }
2160 entry.setParsed(true);
2161 input->seek(pos, librevenge::RVNG_SEEK_SET);
2162 libmwaw::DebugStream f;
2163 f << "Entries(rsrcBtch)[" << entry.id() << "]:";
2164 auto dSz=static_cast<int>(input->readULong(2));
2165 long endPos=pos+2+dSz;
2166 if (dSz<6 || !input->checkPosition(endPos)) {
2167 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcBtch: the size seems bad\n"));
2168 f << "###";
2169 ascii().addPos(pos);
2170 ascii().addNote(f.str().c_str());
2171 return false;
2172 }
2173 auto val=static_cast<int>(input->readLong(2)); // an id?
2174 f << "id=" << val << ",";
2175 for (int i=0; i<2; ++i) { // alway 0?
2176 val=static_cast<int>(input->readLong(2));
2177 if (val) f << "f" << i << "=" << val << ",";
2178 }
2179 ascii().addPos(pos);
2180 ascii().addNote(f.str().c_str());
2181 return true;
2182 }
2183
2184 bool RagTimeParser::readRsrcfppr(MWAWEntry &entry)
2185 {
2186 MWAWInputStreamPtr input = getInput();
2187 long pos=entry.begin();
2188 if (pos<=0 || !input->checkPosition(pos+2+0x4a)) {
2189 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcfppr: the position seems bad\n"));
2190 return false;
2191 }
2192 entry.setParsed(true);
2193 input->seek(pos, librevenge::RVNG_SEEK_SET);
2194 libmwaw::DebugStream f;
2195 f << "Entries(rsrcfppr)[" << entry.id() << "]:";
2196 auto dSz=static_cast<int>(input->readULong(2));
2197 long endPos=pos+2+dSz;
2198 if (dSz<0x1a || !input->checkPosition(endPos)) {
2199 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcfppr: the size seems bad\n"));
2200 f << "###";
2201 ascii().addPos(pos);
2202 ascii().addNote(f.str().c_str());
2203 return false;
2204 }
2205 for (int i=0; i<5; ++i) {
2206 int const expected[]= {2, 0x2d, 0x3c, 0, 0x64 };
2207 auto val=static_cast<int>(input->readLong(2));
2208 if (val!=expected[i]) f << "f" << i << "=" << std::hex << val << std::dec << ",";
2209 }
2210 for (int i=0; i<4; ++i) { // g0-g2: find small number of form 10*k+3 and often g1=g0+10, g2=g1+10 ; g3=0
2211 auto val = static_cast<int>(input->readLong(4));
2212 if (val)
2213 f << "g" << i << "=" << val << ",";
2214 }
2215 ascii().addPos(pos);
2216 ascii().addNote(f.str().c_str());
2217 return true;
2218 }
2219
2220 bool RagTimeParser::readRsrcSele(MWAWEntry &entry)
2221 {
2222 MWAWInputStreamPtr input = getInput();
2223 long pos=entry.begin();
2224 if (pos<=0 || !input->checkPosition(pos+2+4)) {
2225 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcSele: the position seems bad\n"));
2226 return false;
2227 }
2228 entry.setParsed(true);
2229 input->seek(pos, librevenge::RVNG_SEEK_SET);
2230 libmwaw::DebugStream f;
2231 f << "Entries(rsrcSele)[" << entry.id() << "]:";
2232 auto dSz=static_cast<int>(input->readULong(2));
2233 long endPos=pos+2+dSz;
2234 // checkme: what are the posible size value, I find either 4 or 18, 26, 74
2235 if ((dSz!=4 && (dSz%8)!=2) || !input->checkPosition(endPos)) {
2236 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcSele: the size seems bad\n"));
2237 f << "###";
2238 ascii().addPos(pos);
2239 ascii().addNote(f.str().c_str());
2240 return false;
2241 }
2242 auto val=static_cast<int>(input->readLong(2));
2243 f << "f0=" << val << ",";
2244 if (dSz==4) {
2245 val=static_cast<int>(input->readLong(2)); // maybe 2 bool
2246 f << "f1=" << val << ",";
2247 ascii().addPos(pos);
2248 ascii().addNote(f.str().c_str());
2249 return true;
2250 }
2251 ascii().addPos(pos);
2252 ascii().addNote(f.str().c_str());
2253 int N=(dSz/8);
2254 for (int i=0; i<N; ++i) {
2255 pos=input->tell();
2256 f.str("");
2257 f << "rsrcSele-" << i << ":";
2258 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
2259 ascii().addPos(pos);
2260 ascii().addNote(f.str().c_str());
2261 }
2262 return true;
2263 }
2264
2265 bool RagTimeParser::readRsrcStructured(MWAWEntry &entry)
2266 {
2267 MWAWInputStreamPtr input = getInput();
2268 long pos=entry.begin();
2269 if (pos<=0) {
2270 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcStructured: the position seems bad\n"));
2271 return false;
2272 }
2273 entry.setParsed(true);
2274 input->seek(pos, librevenge::RVNG_SEEK_SET);
2275 libmwaw::DebugStream f;
2276 f << "Entries(" << entry.type() << ")[" << entry.id() << "]:";
2277 RagTimeStruct::ResourceList zone;
2278 if (!zone.read(input, entry)) {
2279 static bool first=true;
2280 if (first) {
2281 first=false;
2282 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcStructured: find some no list field\n"));
2283 }
2284 f << "#";
2285 ascii().addPos(pos);
2286 ascii().addNote(f.str().c_str());
2287 return false;
2288 }
2289 f << zone;
2290 ascii().addPos(pos);
2291 ascii().addNote(f.str().c_str());
2292
2293 input->seek(zone.m_dataPos, librevenge::RVNG_SEEK_SET);
2294 for (int i=0; i<=zone.m_dataNumber; ++i) {
2295 pos=input->tell();
2296 f.str("");
2297 f << entry.type() << "-" << i << ":";
2298 input->seek(pos+zone.m_dataSize, librevenge::RVNG_SEEK_SET);
2299 ascii().addPos(pos);
2300 ascii().addNote(f.str().c_str());
2301 }
2302 if (input->tell()!=zone.m_endPos) {
2303 MWAW_DEBUG_MSG(("RagTimeParser::readRsrcStructured: find some extra data\n"));
2304 f.str("");
2305 f << entry.type() << "-end:";
2306 ascii().addPos(input->tell());
2307 ascii().addNote(f.str().c_str());
2308 }
2309 return true;
2310 }
2311
2312 ////////////////////////////////////////////////////////////
2313 // read the header
2314 ////////////////////////////////////////////////////////////
2315 bool RagTimeParser::checkHeader(MWAWHeader *header, bool /*strict*/)
2316 {
2317 *m_state = RagTimeParserInternal::State();
2318
2319 MWAWInputStreamPtr input = getInput();
2320 if (!input || !input->hasDataFork())
2321 return false;
2322
2323 libmwaw::DebugStream f;
2324 f << "FileHeader:";
2325 int headerSize=48;
2326 if (!input->checkPosition(headerSize)) {
2327 MWAW_DEBUG_MSG(("RagTimeParser::checkHeader: file is too short\n"));
2328 return false;
2329 }
2330 input->seek(0,librevenge::RVNG_SEEK_SET);
2331 /* the document dimension, but find also a spreadsheet size which was remained opened */
2332 int dim[2];
2333 dim[0]=static_cast<int>(input->readULong(4));
2334 dim[1]=static_cast<int>(input->readULong(2));
2335 f << "dim=" << dim[1] << "x" << dim[0] << ",";
2336 auto val=static_cast<int>(input->readLong(2)); // related to page?
2337 if (val!=1) f << "f0=" << val << ",";
2338 for (int i=0; i< 2; ++i) {
2339 f << "unkn" << i << "[";
2340 for (int j=0; j<7; ++j) {
2341 val=static_cast<int>(input->readLong(2));
2342 static int const expected[]= {0,0,0,0,0x688f,0x688f,0x688f};
2343 // unkn0: always as expected
2344 // unkn1: find f0=-13:602, f1=0|...|431, f2=0|228|296
2345 if (val!=expected[j])
2346 f << "f" << j << "=" << val << ",";
2347 }
2348 f << "],";
2349 }
2350 for (int i=0; i<3; ++i) { // f0=0|4|9|a|14|23|29, other 0
2351 val=static_cast<int>(input->readLong(2));
2352 if (val) f << "g" << i << "=" << val << ",";
2353 }
2354 // finally a very big number, which seems a multiple of 4 as in 0xb713cfc
2355 f << "unkn2=" << std::hex << input->readULong(4) << std::dec << ",";
2356 auto vers=static_cast<int>(input->readULong(1));
2357 val=static_cast<int>(input->readULong(1));
2358 switch (vers) {
2359 case 0:
2360 setVersion(1);
2361 if (val==0x64 || val==0x65)
2362 f << "v2.1[classic],";
2363 else // or v1?
2364 f << "v2[" << std::hex << val << std::dec << "],";
2365 break;
2366 case 1:
2367 setVersion(2);
2368 if (val==0x2c)
2369 f << "v3.0,";
2370 else if (val==0x90) f << "v3.[12],";
2371 else
2372 f << "v3[" << std::hex << val << std::dec << "],";
2373 break;
2374 default:
2375 MWAW_DEBUG_MSG(("RagTimeParser::checkHeader: unknown version=%d\n", vers));
2376 return false;
2377 }
2378 input->seek(headerSize, librevenge::RVNG_SEEK_SET);
2379
2380 // ok, we can finish initialization
2381 if (header)
2382 header->reset(MWAWDocument::MWAW_T_RAGTIME, version());
2383
2384 ascii().addPos(0);
2385 ascii().addNote(f.str().c_str());
2386 ascii().addPos(headerSize);
2387
2388 return true;
2389 }
2390
2391 ////////////////////////////////////////////////////////////
2392 // send data to the listener
2393 ////////////////////////////////////////////////////////////
2394 bool RagTimeParser::sendPicture(int zId, MWAWPosition const &position)
2395 {
2396 MWAWListenerPtr listener=getMainListener();
2397 if (!listener) {
2398 MWAW_DEBUG_MSG(("RagTimeParser::sendPicture: can not find the listener\n"));
2399 return false;
2400 }
2401 if (m_state->m_idPictureMap.find(zId)==m_state->m_idPictureMap.end()) {
2402 // can be an empty textbox
2403 if (m_state->m_idZoneMap.find(zId)==m_state->m_idZoneMap.end()) {
2404 MWAW_DEBUG_MSG(("RagTimeParser::sendPicture: can not find the picture and zone %d\n", zId));
2405 return false;
2406 }
2407 MWAWGraphicStyle style=m_state->m_idZoneMap.find(zId)->second.m_style;
2408 if (style.hasLine()) {
2409 MWAWBorder border;
2410 border.m_width=double(style.m_lineWidth);
2411 border.m_color=style.m_lineColor;
2412 style.setBorders(0xf, border);
2413 }
2414 listener->insertTextBox(position, MWAWSubDocumentPtr(), style);
2415 return true;
2416 }
2417 auto const &pict=m_state->m_idPictureMap.find(zId)->second;
2418 pict.m_isSent=true;
2419 if (!pict.m_pos.valid()) return false;
2420
2421 MWAWInputStreamPtr input = getInput();
2422 MWAWBox2f box;
2423 input->seek(pict.m_pos.begin(), librevenge::RVNG_SEEK_SET);
2424 if (pict.m_type==2 || pict.m_type==3 || pict.m_type==6) {
2425 // first read the picture
2426 auto pictSize=static_cast<int>(pict.m_pos.length());
2427 if (pict.m_type==3) {
2428 pictSize=static_cast<int>(input->readULong(2));
2429 input->seek(pict.m_pos.begin(), librevenge::RVNG_SEEK_SET);
2430 }
2431 if (!pictSize || pictSize>pict.m_pos.length() ||
2432 MWAWPictData::check(input, pictSize, box) == MWAWPict::MWAW_R_BAD) {
2433 MWAW_DEBUG_MSG(("RagTimeParser::sendPicture: can not find picture\n"));
2434 ascii().addPos(pict.m_pos.begin());
2435 ascii().addNote("PictZone-data:###");
2436 return false;
2437 }
2438 input->seek(pict.m_pos.begin(), librevenge::RVNG_SEEK_SET);
2439 std::shared_ptr<MWAWPict> thePict(MWAWPictData::get(input, pictSize));
2440 auto ok=bool(thePict);
2441 if (ok) {
2442 MWAWEmbeddedObject picture;
2443 if (thePict->getBinary(picture))
2444 listener->insertPicture(position, picture);
2445 }
2446 #ifdef DEBUG_WITH_FILES
2447 ascii().skipZone(pict.m_pos.begin(), pict.m_pos.begin()+pictSize-1);
2448 input->seek(pict.m_pos.begin(), librevenge::RVNG_SEEK_SET);
2449 librevenge::RVNGBinaryData file;
2450 input->seek(pict.m_pos.begin(), librevenge::RVNG_SEEK_SET);
2451 input->readDataBlock(pictSize, file);
2452 static int volatile pictName = 0;
2453 libmwaw::DebugStream f;
2454 f << "PICT-" << ++pictName;
2455 libmwaw::Debug::dumpFile(file, f.str().c_str());
2456 if (pict.m_type!=3 || pictSize>=static_cast<int>(pict.m_pos.length())) return ok;
2457
2458 long pos=pict.m_pos.begin()+pictSize;
2459 if (version()>=2) {
2460 // FIXME: read the label which is a text zone
2461 MWAW_DEBUG_MSG(("RagTimeParser::sendPicture: sending a label is not implemented\n"));
2462 ascii().addPos(pos);
2463 ascii().addNote("PictZone[label]");
2464 return ok;
2465 }
2466 // ok, now a eps file
2467 ascii().skipZone(pos, pict.m_pos.end()-1);
2468 file.clear();
2469 input->seek(pos, librevenge::RVNG_SEEK_SET);
2470 input->readDataBlock(pict.m_pos.end()-pos, file);
2471 f.str("");
2472 f << "PICT-" << pictName << ".eps";
2473 libmwaw::Debug::dumpFile(file, f.str().c_str());
2474 #endif
2475
2476 return ok;
2477 }
2478 else if (pict.m_type==1)
2479 return sendBitmap(pict, position);
2480
2481 MWAW_DEBUG_MSG(("RagTimeParser::sendPicture: do not known how to read the picture\n"));
2482 ascii().addPos(pict.m_pos.begin());
2483 ascii().addNote("PictZone-data:###");
2484 return false;
2485 }
2486
2487 bool RagTimeParser::sendBitmap(RagTimeParserInternal::Picture const &pict, MWAWPosition const &position)
2488 {
2489 MWAWListenerPtr listener=getMainListener();
2490 if (!listener) {
2491 MWAW_DEBUG_MSG(("RagTimeParser::sendBitmap: can not find the listener\n"));
2492 return false;
2493 }
2494 if (pict.m_pos.length()<14) {
2495 MWAW_DEBUG_MSG(("RagTimeParser::sendBitmap: the picture size seems bad\n"));
2496 return false;
2497 }
2498 int const vers=version();
2499 MWAWInputStreamPtr input = getInput();
2500 input->seek(pict.m_pos.begin(), librevenge::RVNG_SEEK_SET);
2501 libmwaw::DebugStream f;
2502 f << "PictZone-data:";
2503 // a big number: 804289ce
2504 f << "f0=" << std::hex << input->readULong(4) << std::dec << ",";
2505 auto rowSize=static_cast<int>(input->readULong(2));
2506 for (int i=0; i<2; ++i) { // always 0 or is it some min dimension ?
2507 auto val=static_cast<int>(input->readULong(2));
2508 if (val) f << "f" << i+1 << "=" << val << ",";
2509 }
2510 int dim[2];
2511 for (auto &d : dim) d=static_cast<int>(input->readULong(2));
2512 f << "dim=" << dim[1] << "x" << dim[0] << ",";
2513 if (dim[0]==0 || rowSize!=(pict.m_pos.length()-14)/dim[0] || rowSize<dim[1]/8) {
2514 MWAW_DEBUG_MSG(("RagTimeParser::sendBitmap: can not read a bitmap\n"));
2515 f << "###";
2516 ascii().addPos(pict.m_pos.begin());
2517 ascii().addNote(f.str().c_str());
2518 return false;
2519 }
2520 ascii().addPos(pict.m_pos.begin());
2521 ascii().addNote(f.str().c_str());
2522 if (vers>=2) {
2523 MWAW_DEBUG_MSG(("RagTimeParser::sendBitmap: send bitmap is not implemented\n"));
2524 return false;
2525 }
2526 ascii().skipZone(input->tell(), pict.m_pos.end()-1);
2527 MWAWPictBitmapBW bitmap(MWAWVec2i(dim[1],dim[0]));
2528 for (int r=0; r<dim[0]; ++r) {
2529 long pos=input->tell();
2530 unsigned long numReads;
2531 uint8_t const *values=input->read(size_t(rowSize), numReads);
2532 if (!values || numReads!=static_cast<unsigned long>(rowSize)) {
2533 MWAW_DEBUG_MSG(("RagTimeParser::sendBitmap: can not read row %d\n", r));
2534 return false;
2535 }
2536 auto const *rowValues = reinterpret_cast<unsigned char const *>(values);
2537 bitmap.setRowPacked(r, rowValues, rowValues + rowSize);
2538 input->seek(pos+rowSize, librevenge::RVNG_SEEK_SET);
2539 }
2540 MWAWEmbeddedObject picture;
2541 if (bitmap.getBinary(picture))
2542 listener->insertPicture(position, picture);
2543 return true;
2544 }
2545
2546 bool RagTimeParser::sendBasicPicture(int zId, MWAWPosition const &position)
2547 {
2548 MWAWListenerPtr listener=getMainListener();
2549 if (!listener) {
2550 MWAW_DEBUG_MSG(("RagTimeParser::sendBasicPicture: can not find the listener\n"));
2551 return false;
2552 }
2553 if (m_state->m_idZoneMap.find(zId)==m_state->m_idZoneMap.end()) {
2554 MWAW_DEBUG_MSG(("RagTimeParser::sendBasicPicture: can not find the zone %d\n", zId));
2555 return false;
2556 }
2557 RagTimeParserInternal::Zone const &zone=m_state->m_idZoneMap.find(zId)->second;
2558 zone.m_isSent=true;
2559 if (zone.m_type!=RagTimeParserInternal::Zone::Line) {
2560 MWAW_DEBUG_MSG(("RagTimeParser::sendBasicPicture: find unexpected type for zone %d\n", zId));
2561 return false;
2562 }
2563 MWAWGraphicShape shape= MWAWGraphicShape::line(MWAWVec2f(zone.m_dimension[0][0],zone.m_dimension[0][1])-position.origin(),
2564 MWAWVec2f(zone.m_dimension[1][0],zone.m_dimension[1][1])-position.origin());
2565 MWAWGraphicStyle style(zone.m_style);
2566 if (zone.m_arrowFlags&1) style.m_arrows[0]=MWAWGraphicStyle::Arrow::plain();
2567 if (zone.m_arrowFlags&2) style.m_arrows[1]=MWAWGraphicStyle::Arrow::plain();
2568 listener->insertShape(position, shape, style);
2569 return true;
2570 }
2571
2572 bool RagTimeParser::sendPageZone(int page)
2573 {
2574 if (m_state->m_pageZonesIdMap.find(page+1)==m_state->m_pageZonesIdMap.end())
2575 return true;
2576 std::vector<int> const &list=m_state->m_pageZonesIdMap.find(page+1)->second;
2577 for (int i : list)
2578 send(i);
2579 return true;
2580 }
2581
2582 bool RagTimeParser::sendZones()
2583 {
2584 for (int pg=0; pg<m_state->m_numPages; ++pg)
2585 sendPageZone(pg);
2586 return true;
2587 }
2588
2589 bool RagTimeParser::send(int zId)
2590 {
2591 MWAWListenerPtr listener=getMainListener();
2592 if (!listener) {
2593 MWAW_DEBUG_MSG(("RagTimeParser::send: can not find the listener\n"));
2594 return false;
2595 }
2596 if (m_state->m_idZoneMap.find(zId)==m_state->m_idZoneMap.end()) {
2597 MWAW_DEBUG_MSG(("RagTimeParser::send: can not find the zone %d\n", zId));
2598 return false;
2599 }
2600 RagTimeParserInternal::Zone const &zone=m_state->m_idZoneMap.find(zId)->second;
2601 auto box=zone.getBoundingBox();
2602 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
2603 pos.m_anchorTo=MWAWPosition::Page;
2604 if (zone.m_page>0)
2605 pos.setPage(zone.m_page);
2606 pos.m_wrapping=MWAWPosition::WRunThrough;
2607 switch (zone.m_type) {
2608 case RagTimeParserInternal::Zone::Chart: {
2609 static bool first=true;
2610 if (first) {
2611 MWAW_DEBUG_MSG(("RagTimeParser::send: this file contains some charts which will be ignored\n"));
2612 first=false;
2613 }
2614 break;
2615 }
2616 case RagTimeParserInternal::Zone::Line:
2617 return sendBasicPicture(zId, pos);
2618 case RagTimeParserInternal::Zone::Picture:
2619 return sendPicture(zId, pos);
2620 case RagTimeParserInternal::Zone::Spreadsheet:
2621 return m_spreadsheetParser->send(zId, pos);
2622 case RagTimeParserInternal::Zone::Text: {
2623 pos.m_wrapping=MWAWPosition::WBackground;
2624 MWAWSubDocumentPtr doc(new RagTimeParserInternal::SubDocument(*this, getInput(), zId));
2625 MWAWGraphicStyle style=zone.m_style;
2626 if (style.hasLine()) {
2627 MWAWBorder border;
2628 border.m_width=double(style.m_lineWidth);
2629 border.m_color=style.m_lineColor;
2630 style.setBorders(0xf, border);
2631 }
2632 pos.setSize(MWAWVec2f(box.size()[0],-box.size()[1]));
2633 if (zone.m_rotation) {
2634 static bool first=true;
2635 if (first) {
2636 MWAW_DEBUG_MSG(("RagTimeParser::send: some rotation will be ignored\n"));
2637 first=false;
2638 }
2639 }
2640 listener->insertTextBox(pos, doc, style);
2641 return true;
2642 }
2643 case RagTimeParserInternal::Zone::Unknown:
2644 case RagTimeParserInternal::Zone::Page:
2645 #if !defined(__clang__)
2646 default:
2647 #endif
2648 break;
2649 }
2650 return false;
2651 }
2652
2653 void RagTimeParser::flushExtra()
2654 {
2655 MWAWListenerPtr listener=getMainListener();
2656 if (!listener) {
2657 MWAW_DEBUG_MSG(("RagTimeParser::flushExtra: can not find the listener\n"));
2658 return;
2659 }
2660 for (auto const &pIt : m_state->m_idPictureMap) {
2661 auto const &pict=pIt.second;
2662 if (pict.m_isSent) continue;
2663 static bool first=true;
2664 if (first) {
2665 MWAW_DEBUG_MSG(("RagTimeParser::flushExtra: find some unsent picture zone\n"));
2666 first=false;
2667 }
2668 MWAWPosition pos(MWAWVec2f(0,0), MWAWVec2f(200,200), librevenge::RVNG_POINT);
2669 pos.m_anchorTo=MWAWPosition::Char;
2670 sendPicture(pIt.first, pos);
2671 listener->insertEOL();
2672 }
2673 for (auto const &zIt : m_state->m_idZoneMap) {
2674 RagTimeParserInternal::Zone const &zone=zIt.second;
2675 if (zone.m_isSent || zone.m_type != RagTimeParserInternal::Zone::Line) continue;
2676 static bool first=true;
2677 if (first) {
2678 MWAW_DEBUG_MSG(("RagTimeParser::flushExtra: find some unsent line zone\n"));
2679 first=false;
2680 }
2681 MWAWPosition pos(MWAWVec2f(0,0), MWAWVec2f(50,50), librevenge::RVNG_POINT);
2682 pos.m_anchorTo=MWAWPosition::Char;
2683 sendBasicPicture(zIt.first, pos);
2684 listener->insertEOL();
2685 }
2686 }
2687
2688 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: