Debian Codegraph @

src/lib/RagTime5Graph.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 <cmath>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <map>
39 #include <set>
40 #include <stack>
41 #include <sstream>
42
43 #include <librevenge/librevenge.h>
44
45 #include "MWAWFont.hxx"
46 #include "MWAWGraphicShape.hxx"
47 #include "MWAWGraphicStyle.hxx"
48 #include "MWAWListener.hxx"
49 #include "MWAWPosition.hxx"
50 #include "MWAWSubDocument.hxx"
51
52 #include "RagTime5Document.hxx"
53 #include "RagTime5StyleManager.hxx"
54 #include "RagTime5StructManager.hxx"
55 #include "RagTime5ClusterManager.hxx"
56
57 #include "RagTime5Graph.hxx"
58
59 #include "libmwaw_internal.hxx"
60
61 /** Internal: the structures of a RagTime5Graph */
62 namespace RagTime5GraphInternal
63 {
64 //! Internal: the helper to read a clustList
65 struct ClustListParser final : public RagTime5StructManager::DataParser {
66 //! constructor
67 ClustListParser(RagTime5ClusterManager &clusterManager, std::string const &zoneName)
68 : RagTime5StructManager::DataParser(zoneName)
69 , m_clusterList()
70 , m_clusterManager(clusterManager)
71 {
72 }
73 //! destructor
74 ~ClustListParser() final;
75 //! returns a name with can be used for debugging
76 std::string getClusterDebugName(int id) const
77 {
78 return m_clusterManager.getClusterDebugName(id);
79 }
80
81 //! try to parse a data
82 bool parseData(MWAWInputStreamPtr &input, long endPos, RagTime5Zone &/*zone*/, int /*n*/, libmwaw::DebugStream &f) final
83 {
84 long pos=input->tell();
85 long fSz=endPos-pos;
86 if (fSz!=8 && fSz!=14 && fSz!=28) {
87 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ClustListParser::parse: bad data size\n"));
88 return false;
89 }
90 std::vector<int> listIds;
91 if (!RagTime5StructManager::readDataIdList(input, 1, listIds)) {
92 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ClustListParser::parse: can not read an cluster id\n"));
93 f << "##clusterIds,";
94 return false;
95 }
96 if (listIds[0]) {
97 m_clusterList.push_back(listIds[0]);
98 // a e,2003,200b, ... cluster
99 f << getClusterDebugName(listIds[0]) << ",";
100 }
101 if (fSz==8) { // f0=1, f1=1|2
102 for (int i=0; i<2; ++i) {
103 auto val=static_cast<int>(input->readLong(2));
104 if (val) f << "f" << i << "=" << val << ",";
105 }
106 return true;
107 }
108 unsigned long lVal=input->readULong(4); // c00..small number
109 if ((lVal&0xc0000000)==0xc0000000)
110 f << "f0=" << (lVal&0x3fffffff) << "*,";
111 else if ((lVal&0xc0000000))
112 f << "f0=" << (lVal&0x3fffffff) << "[" << (lVal>>30) << "],";
113 else
114 f << "f0" << lVal << ",";
115 if (fSz==14) {
116 for (int i=0; i<3; ++i) { // always 0
117 auto val=static_cast<int>(input->readLong(2));
118 if (val) f << "f" << i+1 << "=" << val << ",";
119 }
120 return true;
121 }
122 auto val=static_cast<int>(input->readLong(2)); // 0|2
123 if (val) f << "f1=" << val << ",";
124 float dim[4];
125 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
126 // very often (0x0<->1x1), if not, we often have dim[0]+dim[2]~1 and dim[1]+dim[3]~1, some margins?
127 f << "dim=" << MWAWBox2f(MWAWVec2f(dim[0],dim[1]),MWAWVec2f(dim[2],dim[3])) << ",";
128 val=static_cast<int>(input->readLong(2)); // 0|1
129 if (val) f << "f2=" << val << ",";
130 return true;
131 }
132
133 //! the list of read cluster
134 std::vector<int> m_clusterList;
135 private:
136 //! the main zone manager
137 RagTime5ClusterManager &m_clusterManager;
138 //! copy constructor, not implemented
139 ClustListParser(ClustListParser &orig);
140 //! copy operator, not implemented
141 ClustListParser &operator=(ClustListParser &orig);
142 };
143
144 ClustListParser::~ClustListParser()
145 {
146 }
147
148 //! Internal: the helper to read an integer list
149 struct IntListParser final : public RagTime5StructManager::DataParser {
150 //! constructor
151 IntListParser(int fieldSz, std::string const &zoneName)
152 : RagTime5StructManager::DataParser(zoneName)
153 , m_fieldSize(fieldSz)
154 , m_dataList()
155 {
156 if (m_fieldSize!=1 && m_fieldSize!=2 && m_fieldSize!=4) {
157 MWAW_DEBUG_MSG(("RagTime5GraphInternal::IntListParser: bad field size\n"));
158 m_fieldSize=0;
159 }
160 }
161 //! destructor
162 ~IntListParser() final;
163 //! try to parse a data
164 bool parseData(MWAWInputStreamPtr &input, long endPos, RagTime5Zone &/*zone*/, int, libmwaw::DebugStream &f) final
165 {
166 long pos=input->tell();
167 if (m_fieldSize<=0 || (endPos-pos)%m_fieldSize) {
168 MWAW_DEBUG_MSG(("RagTime5GraphInternal::IntListParser::parseData: bad data size\n"));
169 return false;
170 }
171 auto N=int((endPos-pos)/m_fieldSize);
172 f << "data=[";
173 for (int i=0; i<N; ++i) {
174 auto val=static_cast<int>(input->readLong(m_fieldSize));
175 f << val << ",";
176 m_dataList.push_back(val);
177 }
178 f << "],";
179 return true;
180 }
181
182 //! the field size
183 int m_fieldSize;
184 //! the list of read int
185 std::vector<int> m_dataList;
186
187 //! copy constructor, not implemented
188 IntListParser(IntListParser &orig) = delete;
189 //! copy operator, not implemented
190 IntListParser &operator=(IntListParser &orig) = delete;
191 };
192
193 IntListParser::~IntListParser()
194 {
195 }
196
197 //! Internal: the helper to read a int16 float
198 struct FloatParser final : public RagTime5StructManager::DataParser {
199 //! constructor
200 explicit FloatParser(std::string const &zoneName)
201 : RagTime5StructManager::DataParser(zoneName)
202 {
203 }
204 //! destructor
205 ~FloatParser() final;
206 //! try to parse a data
207 bool parseData(MWAWInputStreamPtr &input, long endPos, RagTime5Zone &/*zone*/, int, libmwaw::DebugStream &f) final
208 {
209 long pos=input->tell();
210 if (endPos-pos!=4) {
211 MWAW_DEBUG_MSG(("RagTime5GraphInternal::FloatParser: bad field size\n"));
212 return false;
213 }
214 f << float(input->readLong(4))/65536.f;
215 return true;
216 }
217 };
218
219 FloatParser::~FloatParser()
220 {
221 }
222
223 //! Internal: the shape of a RagTime5Graph
224 struct Shape {
225 //! the different shape
226 enum Type { S_Line, S_Rect, S_RectOval, S_Circle, S_Pie, S_Arc, S_Polygon, S_Spline, S_RegularPoly, S_TextBox, S_Group, S_Unknown };
227 //! constructor
228 Shape()
229 : m_id(0)
230 , m_parentId(0)
231 , m_linkId(0)
232 , m_partId(0)
233 , m_type(S_Unknown)
234 , m_dimension()
235 , m_shape()
236 , m_childIdList()
237 , m_flags(0)
238 , m_borderId(0)
239 , m_graphicId(0)
240 , m_transformId(0)
241 , m_extra("")
242 {
243 }
244 //! return the shape bdbox
245 MWAWBox2f getBdBox() const
246 {
247 return (m_type==S_TextBox || m_type==S_Group || m_type==S_Unknown) ? m_dimension : m_shape.getBdBox();
248 }
249
250 //! operator<<
251 friend std::ostream &operator<<(std::ostream &o, Shape const &shape);
252 //! the shape id
253 int m_id;
254 //! the shape parent id
255 int m_parentId;
256 //! the link to a zone id
257 int m_linkId;
258 //! the link part id
259 int m_partId;
260 //! the shape type
261 Type m_type;
262 //! the dimension
263 MWAWBox2f m_dimension;
264 //! the graphic shape
265 MWAWGraphicShape m_shape;
266 //! the child list (for group)
267 std::vector<int> m_childIdList;
268 //! the shape flag
269 uint32_t m_flags;
270 //! the border id
271 int m_borderId;
272 //! the graphic id
273 int m_graphicId;
274 //! the transformation id
275 int m_transformId;
276 //! extra data
277 std::string m_extra;
278 };
279
280 std::ostream &operator<<(std::ostream &o, Shape const &shape)
281 {
282 if (shape.m_id) o << "id=" << shape.m_id << ",";
283 if (shape.m_parentId) o << "id[parent]=" << shape.m_parentId << ",";
284 if (shape.m_linkId) {
285 o << "id[link]=" << shape.m_linkId;
286 if (shape.m_partId) o << "[" << shape.m_partId << "]";
287 o << ",";
288 }
289 if (!shape.m_childIdList.empty()) {
290 o << "child[id]=[";
291 for (auto const &id : shape.m_childIdList)
292 o << id << ",";
293 o << "],";
294 }
295 switch (shape.m_type) {
296 case Shape::S_Rect:
297 o << "rect,";
298 break;
299 case Shape::S_RectOval:
300 o << "rectoval,";
301 break;
302 case Shape::S_Circle:
303 o << "circle,";
304 break;
305 case Shape::S_Pie:
306 o << "pie,";
307 break;
308 case Shape::S_Arc:
309 o << "arc,";
310 break;
311 case Shape::S_Group:
312 o << "group,";
313 break;
314 case Shape::S_Line:
315 o << "line,";
316 break;
317 case Shape::S_Polygon:
318 o << "poly,";
319 break;
320 case Shape::S_Spline:
321 o << "spline,";
322 break;
323 case Shape::S_TextBox:
324 o << "textbox,";
325 break;
326 case Shape::S_RegularPoly:
327 o << "poly[regular],";
328 break;
329 // also b and c
330 case Shape::S_Unknown:
331 #if !defined(__clang__)
332 default:
333 #endif
334 break;
335 }
336 o << "dim=" << shape.m_dimension << ",";
337 uint32_t fl=shape.m_flags;
338 if (fl&1) o << "arrow[beg],";
339 if (fl&2) o << "arrow[end],";
340 if (fl&0x8) o << "hasTransf,";
341 if (fl&0x40) o << "text[flowArround],";
342 if (fl&0x200) o << "fixed,";
343 if (fl&0x400) o << "hasName,";
344 if (fl&0x800) o << "hasDist[bordTB],";
345 if (fl&0x1000) o << "hasDist[flowTB],";
346 if ((fl&0x4000)==0) o << "noPrint,";
347 if (fl&0x8000) o << "hasDist[bordLR],";
348 if (fl&0x10000) o << "hasDist[flowLR],";
349 if (fl&0x40000) o << "protected,";
350 if (fl&0x100000) o << "hasBorder,"; // checkme, maybe related to link data
351 fl &= 0xFFEA21B4;
352 if (fl) o << "flags=" << std::hex << fl << std::dec << ",";
353 if (shape.m_borderId) o << "border[id]=GS" << shape.m_borderId << ",";
354 if (shape.m_graphicId) o << "surface[id]=GS" << shape.m_graphicId << ",";
355 if (shape.m_transformId) o << "GT" << shape.m_transformId << ",";
356 o << shape.m_extra;
357 return o;
358 }
359
360 //! the button cluster
361 struct ClusterButton final : public RagTime5ClusterManager::Cluster {
362 //! constructor
363 ClusterButton()
364 : RagTime5ClusterManager::Cluster(C_ButtonZone)
365 , m_item(0)
366 , m_buttonType(0)
367 , m_idToItemStringMap()
368 , m_itemNamesLink()
369 , m_scriptComment()
370 , m_buttonName("")
371 {
372 }
373 //! destructor
374 ~ClusterButton() final;
375 //! the chosen item: 0 means none
376 int m_item;
377 //! the button type: 1:push, 2:radio, 3:checkbox, 4:popup, 5:push(invisible)
378 int m_buttonType;
379 //! the list of item strings
380 std::map<int, librevenge::RVNGString> m_idToItemStringMap;
381 //! the item name link
382 RagTime5ClusterManager::Link m_itemNamesLink;
383 //! the script comment zone
384 RagTime5ClusterManager::Link m_scriptComment;
385 //! the button name if known
386 librevenge::RVNGString m_buttonName;
387 };
388
389 ClusterButton::~ClusterButton()
390 {
391 }
392
393 //! the shape cluster
394 struct ClusterGraphic final : public RagTime5ClusterManager::Cluster {
395 //! constructor
396 ClusterGraphic()
397 : RagTime5ClusterManager::Cluster(C_GraphicZone)
398 , m_textboxZoneId(0)
399 , m_usedZoneId(0)
400 , m_transformationLinks()
401 , m_dimensionLinks()
402 , m_idToShapeMap()
403 , m_rootIdList()
404 , m_linkList()
405 {
406 for (auto &n : m_N) n=0;
407 }
408 //! destructor
409 ~ClusterGraphic() final;
410 //! number of graph shape(+1) and number of graph used
411 int m_N[2];
412 //! the main textbox zone id(if defined)
413 int m_textboxZoneId;
414 //! the graphic used zone id
415 int m_usedZoneId;
416 //! the list of transformation's link
417 std::vector<RagTime5ClusterManager::Link> m_transformationLinks;
418 //! the list of dimension's link
419 std::vector<RagTime5ClusterManager::Link> m_dimensionLinks;
420 //! two cluster links: list of pipeline: fixedSize=12, , fixedSize=8
421 RagTime5ClusterManager::Link m_clusterLinks[2];
422
423 //! the shape list
424 std::map<int, std::shared_ptr<Shape> > m_idToShapeMap;
425 //! the root id list
426 std::vector<int> m_rootIdList;
427 //! list of link to other zone
428 std::vector<RagTime5StructManager::ZoneLink> m_linkList;
429 };
430
431 ClusterGraphic::~ClusterGraphic()
432 {
433 }
434
435 //! the picture cluster
436 struct ClusterPicture final : public RagTime5ClusterManager::Cluster {
437 //! constructor
438 ClusterPicture()
439 : RagTime5ClusterManager::Cluster(C_PictureZone)
440 , m_auxilliarLink()
441 , m_containerId(0)
442 , m_dimension(0,0)
443 {
444 }
445 //! destructor
446 ~ClusterPicture() final;
447 //! the first auxilliar data
448 RagTime5ClusterManager::Link m_auxilliarLink;
449 //! the picture container id
450 int m_containerId;
451 //! the picture dimension
452 MWAWVec2f m_dimension;
453 };
454
455 ClusterPicture::~ClusterPicture()
456 {
457 }
458
459 ////////////////////////////////////////
460 //! Internal: the state of a RagTime5Graph
461 struct State {
462 //! enum used to defined list of classical pict
463 enum PictureType { P_Pict, P_Tiff, P_Epsf, P_Jpeg, P_PNG, P_ScreenRep, P_WMF, P_Unknown };
464
465 //! constructor
466 State()
467 : m_numPages(0)
468 , m_shapeTypeIdVector()
469 , m_idButtonMap()
470 , m_idGraphicMap()
471 , m_idPictClusterMap()
472 , m_idPictureMap()
473 {
474 }
475 //! the number of pages
476 int m_numPages;
477 //! try to return a set type
478 Shape::Type getShapeType(int id) const;
479 //! returns the picture type corresponding to a name
480 static PictureType getPictureType(std::string const &type)
481 {
482 if (type=="TIFF") return P_Tiff;
483 if (type=="PICT") return P_Pict;
484 if (type=="PNG") return P_PNG;
485 if (type=="JPEG") return P_Jpeg;
486 if (type=="WMF") return P_WMF;
487 if (type=="EPSF") return P_Epsf;
488 if (type=="ScreenRep" || type=="Thumbnail") return P_ScreenRep;
489 return P_Unknown;
490 }
491
492 //! the vector of shape type id
493 std::vector<unsigned long> m_shapeTypeIdVector;
494 //! map data id to button zone
495 std::map<int, std::shared_ptr<ClusterButton> > m_idButtonMap;
496 //! map data id to graphic zone
497 std::map<int, std::shared_ptr<ClusterGraphic> > m_idGraphicMap;
498 //! map data id to picture zone
499 std::map<int, std::shared_ptr<ClusterPicture> > m_idPictClusterMap;
500 //! map data id to picture
501 std::map<int, std::shared_ptr<MWAWEmbeddedObject> > m_idPictureMap;
502 };
503
504 Shape::Type State::getShapeType(int id) const
505 {
506 if (id<=0 || id>static_cast<int>(m_shapeTypeIdVector.size())) {
507 MWAW_DEBUG_MSG(("RagTime5GraphInternal::State::getShapeType: find some unknown id %d\n", id));
508 return Shape::S_Unknown;
509 }
510 unsigned long type=m_shapeTypeIdVector[size_t(id-1)];
511 switch (type) {
512 case 0x14e8842:
513 return Shape::S_Rect;
514 case 0x14e9042:
515 return Shape::S_Circle;
516 case 0x14e9842:
517 return Shape::S_RectOval;
518 case 0x14ea042:
519 return Shape::S_Arc;
520 case 0x14ea842:
521 return Shape::S_TextBox;
522 case 0x14eb842:
523 return Shape::S_Polygon;
524 case 0x14ec842:
525 return Shape::S_Line;
526 case 0x14ed842:
527 return Shape::S_Spline;
528 case 0x14f0042:
529 return Shape::S_Group;
530 case 0x14f8842:
531 return Shape::S_Pie;
532 case 0x1bbc042:
533 return Shape::S_RegularPoly;
534 default:
535 break;
536 }
537 MWAW_DEBUG_MSG(("RagTime5GraphInternal::State::getShapeType: find some unknown type %lx\n", type));
538 return Shape::S_Unknown;
539 }
540
541 //! Internal: the subdocument of a RagTime5Graph
542 class SubDocument final : public MWAWSubDocument
543 {
544 public:
545 // constructor
546 SubDocument(RagTime5Graph &parser, MWAWInputStreamPtr const &input, int zoneId, int partId, bool inButton=false, double width=-1)
547 : MWAWSubDocument(&parser.m_document.getMainParser(), input, MWAWEntry())
548 , m_ragtimeParser(parser)
549 , m_id(zoneId)
550 , m_subId(partId)
551 , m_inButton(inButton)
552 , m_width(width)
553 {
554 }
555
556 //! destructor
557 ~SubDocument() final {}
558
559 //! operator!=
560 bool operator!=(MWAWSubDocument const &doc) const final;
561
562 //! the parser function
563 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
564
565 protected:
566 //! the main parser
567 RagTime5Graph &m_ragtimeParser;
568 //! the zone id
569 int m_id;
570 //! the zone sub id
571 int m_subId;
572 //! a flag to know if we need to send the button content
573 bool m_inButton;
574 //! the zone width if known
575 double m_width;
576 };
577
578 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
579 {
580 if (!listener.get()) {
581 MWAW_DEBUG_MSG(("RagTime5GraphInternal::SubDocument::parse: no listener\n"));
582 return;
583 }
584
585 long pos = m_input->tell();
586 if (m_inButton)
587 m_ragtimeParser.sendButtonZoneAsText(listener, m_id);
588 else
589 m_ragtimeParser.sendTextZone(listener, m_id, m_subId, m_width);
590 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
591 }
592
593 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
594 {
595 if (MWAWSubDocument::operator!=(doc)) return true;
596 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
597 if (!sDoc) return true;
598 if (m_id != sDoc->m_id) return true;
599 if (m_subId != sDoc->m_subId) return true;
600 if (m_inButton != sDoc->m_inButton) return true;
601 if (m_width < sDoc->m_width || m_width > sDoc->m_width) return true;
602 if (&m_ragtimeParser != &sDoc->m_ragtimeParser) return true;
603 return false;
604 }
605
606 }
607
608 ////////////////////////////////////////////////////////////
609 // constructor/destructor, ...
610 ////////////////////////////////////////////////////////////
611 RagTime5Graph::RagTime5Graph(RagTime5Document &doc)
612 : m_document(doc)
613 , m_structManager(m_document.getStructManager())
614 , m_styleManager(m_document.getStyleManager())
615 , m_parserState(doc.getParserState())
616 , m_state(new RagTime5GraphInternal::State)
617 {
618 }
619
620 RagTime5Graph::~RagTime5Graph()
621 {
622 }
623
624 int RagTime5Graph::version() const
625 {
626 return m_parserState->m_version;
627 }
628
629 int RagTime5Graph::numPages() const
630 {
631 // TODO IMPLEMENT ME
632 MWAW_DEBUG_MSG(("RagTime5Graph::numPages: is not implemented\n"));
633 return 0;
634 }
635
636 bool RagTime5Graph::send(int zoneId, MWAWListenerPtr listener, MWAWPosition const &pos)
637 {
638 if (m_state->m_idGraphicMap.find(zoneId)!=m_state->m_idGraphicMap.end() &&
639 m_state->m_idGraphicMap.find(zoneId)->second)
640 return send(*m_state->m_idGraphicMap.find(zoneId)->second, listener, pos);
641 if (m_state->m_idPictClusterMap.find(zoneId)!=m_state->m_idPictClusterMap.end() &&
642 m_state->m_idPictClusterMap.find(zoneId)->second)
643 return send(*m_state->m_idPictClusterMap.find(zoneId)->second, listener, pos);
644 if (m_state->m_idButtonMap.find(zoneId)!=m_state->m_idButtonMap.end() &&
645 m_state->m_idButtonMap.find(zoneId)->second)
646 return send(*m_state->m_idButtonMap.find(zoneId)->second, listener, pos, MWAWGraphicStyle::emptyStyle());
647 MWAW_DEBUG_MSG(("RagTime5Graph::send: can not find zone %d\n", zoneId));
648 return false;
649 }
650
651 bool RagTime5Graph::sendTextZone(MWAWListenerPtr listener, int zId, int pId, double totalWidth)
652 {
653 return m_document.send(zId, listener, MWAWPosition(), pId, 0, totalWidth);
654 }
655
656 ////////////////////////////////////////////////////////////
657 //
658 // Intermediate level
659 //
660 ////////////////////////////////////////////////////////////
661
662 ////////////////////////////////////////////////////////////
663 // main graphic
664 ////////////////////////////////////////////////////////////
665 bool RagTime5Graph::readGraphicTypes(RagTime5ClusterManager::Link const &link)
666 {
667 if (link.empty() || link.m_ids.size()<2) {
668 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicTypes: call with no zone\n"));
669 return false;
670 }
671 auto dataZone=m_document.getDataZone(link.m_ids[1]);
672 // not frequent, but can happen...
673 if (dataZone && !dataZone->m_entry.valid())
674 return true;
675 if (!dataZone || dataZone->getKindLastPart(dataZone->m_kinds[1].empty())!="ItemData") {
676 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicTypes: the first zone seems bad\n"));
677 return false;
678 }
679 long length=dataZone->m_entry.length();
680 std::vector<long> decal;
681 if (link.m_ids[0])
682 m_document.readPositions(link.m_ids[0], decal);
683 if (decal.empty())
684 decal=link.m_longList;
685 if (!length) {
686 if (decal.empty()) return true;
687 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicTypes: can not find the type positions for zone %d\n", link.m_ids[0]));
688 return false;
689 }
690
691 MWAWInputStreamPtr input=dataZone->getInput();
692 input->setReadInverted(!dataZone->m_hiLoEndian);
693 dataZone->m_isParsed=true;
694 libmwaw::DebugFile &ascFile=dataZone->ascii();
695 libmwaw::DebugStream f;
696 f << "Entries(GraphType)[" << *dataZone << "]:";
697 input->seek(dataZone->m_entry.begin(), librevenge::RVNG_SEEK_SET);
698 ascFile.addPos(dataZone->m_entry.end());
699 ascFile.addNote("_");
700 if (decal.size()<=1) {
701 f << "###";
702 ascFile.addPos(dataZone->m_entry.begin());
703 ascFile.addNote(f.str().c_str());
704 input->setReadInverted(false);
705 return false;
706 }
707 ascFile.addPos(dataZone->m_entry.begin());
708 ascFile.addNote(f.str().c_str());
709 m_state->m_shapeTypeIdVector.resize(size_t(static_cast<int>(decal.size())-1),0);
710 for (size_t i=0; i+1<decal.size(); ++i) {
711 auto dLength=int(decal[i+1]-decal[i]);
712 if (!dLength) continue;
713 long pos=dataZone->m_entry.begin()+decal[i];
714 f.str("");
715 f << "GraphType-" << i << ":";
716 if (decal[i+1]>length || dLength<16) {
717 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicTypes: something look bad for decal %d\n", static_cast<int>(i)));
718 f << "###";
719 if (decal[i]<length) {
720 ascFile.addPos(pos);
721 ascFile.addNote(f.str().c_str());
722 }
723 continue;
724 }
725 input->seek(pos, librevenge::RVNG_SEEK_SET);
726 auto type=input->readULong(4);
727 m_state->m_shapeTypeIdVector[i]=type;
728 f << "type=" << RagTime5Graph::printType(type) << ",";
729 for (int j=0; j<4; ++j) { // always 0
730 auto val=static_cast<int>(input->readLong(2));
731 if (val) f << "f" << j << "=" << val << ",";
732 }
733 auto N=static_cast<int>(input->readULong(4));
734 if (N!=(dLength-16)/4) {
735 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicTypes: the number of data seems bad\n"));
736 f << "##N=" << N << ",";
737 N=0;
738 }
739 if (N) {
740 f << "unkn=[" << std::hex;
741 for (int j=0; j<N; ++j)
742 f << input->readULong(4) << ",";
743 f << std::dec << "],";
744 }
745 ascFile.addPos(pos);
746 ascFile.addNote(f.str().c_str());
747 }
748 input->setReadInverted(false);
749 return true;
750 }
751
752 ////////////////////////////////////////////////////////////
753 // colors
754 ////////////////////////////////////////////////////////////
755 bool RagTime5Graph::readColorPatternZone(RagTime5ClusterManager::Cluster &cluster)
756 {
757 for (const auto &lnk : cluster.m_linksList) {
758 auto data=m_document.getDataZone(lnk.m_ids[0]);
759 if (!data || !data->m_entry.valid()) {
760 if (lnk.m_N && lnk.m_fieldSize) {
761 MWAW_DEBUG_MSG(("RagTime5Graph::readColorPatternZone: can not find data zone %d\n", lnk.m_ids[0]));
762 }
763 continue;
764 }
765 long pos=data->m_entry.begin();
766 data->m_isParsed=true;
767 libmwaw::DebugFile &dAscFile=data->ascii();
768 libmwaw::DebugStream f;
769 std::string what("unkn");
770 switch (lnk.m_fileType[1]) {
771 case 0x40:
772 what="col2";
773 break;
774 case 0x84040:
775 what="color";
776 break;
777 case 0x16de842:
778 what="pattern";
779 break;
780 default:
781 MWAW_DEBUG_MSG(("RagTime5Graph::readColorPatternZone: find unexpected field\n"));
782 break;
783 }
784
785 if (lnk.m_fieldSize<=0 || lnk.m_N*lnk.m_fieldSize!=data->m_entry.length()) {
786 MWAW_DEBUG_MSG(("RagTime5Graph::readColorPatternZone: bad fieldSize/N for zone %d\n", lnk.m_ids[0]));
787 f << "Entries(GraphCPData)[" << *data << "]:N=" << lnk.m_N << ",fSz=" << lnk.m_fieldSize << ",###" << what;
788 dAscFile.addPos(pos);
789 dAscFile.addNote(f.str().c_str());
790 continue;
791 }
792 MWAWInputStreamPtr input=data->getInput();
793 input->setReadInverted(!data->m_hiLoEndian);
794 if (lnk.m_fieldSize!=8 && lnk.m_fieldSize!=10) {
795 MWAW_DEBUG_MSG(("RagTime5Graph::readColorPatternZone: find some unknown field size for zone %d\n", lnk.m_ids[0]));
796 }
797 for (int j=0; j<lnk.m_N; ++j) {
798 f.str("");
799 if (j==0)
800 f << "Entries(GraphCPData)[" << *data << "]:";
801 else
802 f << "GraphCPData-" << j+1 << ":";
803 f << what << ",";
804 if (lnk.m_fieldSize==10) {
805 auto val=static_cast<int>(input->readLong(2));
806 if (val!=1)
807 f << "numUsed?=" << val << ",";
808 unsigned char col[4];
809 for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8); // unsure if rgba, or ?
810 f << MWAWColor(col[0],col[1],col[2],col[3]);
811 }
812 else if (lnk.m_fieldSize==8) {
813 MWAWGraphicStyle::Pattern pat;
814 pat.m_colors[0]=MWAWColor::white();
815 pat.m_colors[1]=MWAWColor::black();
816 pat.m_dim=MWAWVec2i(8,8);
817 pat.m_data.resize(8);
818 for (auto &dta : pat.m_data) dta=static_cast<unsigned char>(input->readULong(1));
819 f << pat;
820 }
821 else
822 f << "###";
823 dAscFile.addPos(pos);
824 dAscFile.addNote(f.str().c_str());
825 pos+=lnk.m_fieldSize;
826 }
827 input->setReadInverted(false);
828 }
829
830 return true;
831 }
832
833 ////////////////////////////////////////////////////////////
834 // graphic zone
835 ////////////////////////////////////////////////////////////
836 bool RagTime5Graph::readGraphicShapes(RagTime5GraphInternal::ClusterGraphic &cluster)
837 {
838 RagTime5ClusterManager::Link const &link= cluster.m_dataLink;
839 if (link.m_ids.size()<2 || !link.m_ids[1]) {
840 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShapes: can not find main data\n"));
841 return true;
842 }
843
844 std::map<int, librevenge::RVNGString> idToNameMap;
845 if (!cluster.m_nameLink.empty()) {
846 m_document.readUnicodeStringList(cluster.m_nameLink, idToNameMap);
847 cluster.m_nameLink=RagTime5ClusterManager::NameLink();
848 }
849 std::vector<long> decal;
850 if (link.m_ids[0])
851 m_document.readPositions(link.m_ids[0], decal);
852 if (decal.empty())
853 decal=link.m_longList;
854 if (decal.size()<size_t(cluster.m_N[0])) {
855 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShapes: the decal array seems bad\n"));
856 }
857 else if (decal.size()>size_t(cluster.m_N[0])) // rare but can happens
858 decal.resize(size_t(cluster.m_N[0]));
859 int const dataId=link.m_ids[1];
860 auto dataZone=m_document.getDataZone(dataId);
861 if (!dataZone || !dataZone->m_entry.valid() ||
862 dataZone->getKindLastPart(dataZone->m_kinds[1].empty())!="ItemData") {
863 if (dataZone && decal.size()==1) {
864 // a graphic zone with 0 zone is ok...
865 dataZone->m_isParsed=true;
866 return true;
867 }
868 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShapes: the data zone %d seems bad\n", dataId));
869 return false;
870 }
871 dataZone->m_isParsed=true;
872 MWAWEntry entry=dataZone->m_entry;
873 libmwaw::DebugFile &ascFile=dataZone->ascii();
874 libmwaw::DebugStream f;
875 f << "Entries(GraphShape)[" << *dataZone << "]:";
876 ascFile.addPos(entry.end());
877 ascFile.addNote("_");
878 ascFile.addPos(entry.begin());
879 ascFile.addNote(f.str().c_str());
880
881 auto N=int(decal.size());
882 MWAWInputStreamPtr input=dataZone->getInput();
883 input->setReadInverted(!cluster.m_hiLoEndian); // checkme: can be !zone.m_hiLoEndian
884 long debPos=entry.begin();
885 long endPos=entry.end();
886 if (N==0) {
887 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShapes: can not find decal list for zone %d, let try to continue\n", dataId));
888 input->seek(debPos, librevenge::RVNG_SEEK_SET);
889 int n=0;
890 while (input->tell()+8 < endPos) {
891 long pos=input->tell();
892 int id=++n;
893 librevenge::RVNGString name("");
894 if (idToNameMap.find(id)!=idToNameMap.end())
895 name=idToNameMap.find(id)->second;
896 if (!readGraphicShape(cluster, *dataZone, endPos, id, name)) {
897 input->seek(pos, librevenge::RVNG_SEEK_SET);
898 break;
899 }
900 }
901 if (input->tell()!=endPos) {
902 static bool first=true;
903 if (first) {
904 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShapes: can not read some block\n"));
905 first=false;
906 }
907 ascFile.addPos(debPos);
908 ascFile.addNote("###");
909 }
910 }
911 else {
912 for (int i=0; i<N-1; ++i) {
913 long pos=decal[size_t(i)];
914 long nextPos=decal[size_t(i+1)];
915 if (pos<0 || debPos+pos>endPos) {
916 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShapes: can not read the data zone %d-%d seems bad\n", dataId, i));
917 continue;
918 }
919 input->seek(debPos+pos, librevenge::RVNG_SEEK_SET);
920 librevenge::RVNGString name("");
921 if (idToNameMap.find(i+1)!=idToNameMap.end())
922 name=idToNameMap.find(i+1)->second;
923 readGraphicShape(cluster, *dataZone, debPos+nextPos, i+1, name);
924 if (input->tell()!=debPos+nextPos) {
925 static bool first=true;
926 if (first) {
927 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShapes: can not read some block\n"));
928 first=false;
929 }
930 ascFile.addPos(debPos+pos);
931 ascFile.addNote("###");
932 }
933 }
934 }
935 return true;
936 }
937
938 bool RagTime5Graph::readGraphicShape(RagTime5GraphInternal::ClusterGraphic &cluster, RagTime5Zone &zone,
939 long endPos, int n, librevenge::RVNGString const &dataName)
940 {
941 MWAWInputStreamPtr input=zone.getInput();
942 long pos=input->tell();
943 libmwaw::DebugFile &ascFile=zone.ascii();
944 libmwaw::DebugStream f;
945 f << "GraphShape-" << n << ":";
946 if (!dataName.empty())
947 f << "\"" << dataName.cstr() << "\",";
948 if (pos+42>endPos) {
949 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: a graphic seems bad\n"));
950 f<<"###";
951 ascFile.addPos(pos);
952 ascFile.addNote(f.str().c_str());
953 return false;
954 }
955 std::shared_ptr<RagTime5GraphInternal::Shape> shape(new RagTime5GraphInternal::Shape);
956 shape->m_flags=static_cast<uint32_t>(input->readULong(4));
957 f.str("");
958 int val;
959 for (int i=0; i<7; ++i) { // f1=0..16
960 val=static_cast<int>(input->readLong(2));
961 if (!val) continue;
962 if (i==0)
963 shape->m_id=val;
964 else if (i==1 && shape->m_id) {
965 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: main id is already set\n"));
966 f << "#shape[id]=" << shape->m_id << ",";
967 shape->m_id=val;
968 }
969 else if (i==1)
970 shape->m_id=val;
971 else if (i==5)
972 shape->m_parentId=val;
973 else if (i==6)
974 shape->m_linkId=val;
975 else
976 f << "f" << i << "=" << val << ",";
977 }
978 val=static_cast<int>(input->readLong(2));
979 shape->m_type=m_state->getShapeType(val);
980 int typeFieldSize=8;
981 switch (shape->m_type) {
982 case RagTime5GraphInternal::Shape::S_Line:
983 case RagTime5GraphInternal::Shape::S_Rect:
984 case RagTime5GraphInternal::Shape::S_Circle:
985 break;
986 case RagTime5GraphInternal::Shape::S_RectOval:
987 typeFieldSize+=8;
988 break;
989 case RagTime5GraphInternal::Shape::S_Pie:
990 typeFieldSize+=10;
991 break;
992 case RagTime5GraphInternal::Shape::S_Arc:
993 typeFieldSize+=10;
994 break;
995 case RagTime5GraphInternal::Shape::S_Group:
996 typeFieldSize=6;
997 break;
998 case RagTime5GraphInternal::Shape::S_Polygon:
999 typeFieldSize=10;
1000 break;
1001 case RagTime5GraphInternal::Shape::S_Spline:
1002 typeFieldSize=18;
1003 break;
1004 case RagTime5GraphInternal::Shape::S_TextBox:
1005 typeFieldSize+=4;
1006 break;
1007 case RagTime5GraphInternal::Shape::S_RegularPoly:
1008 typeFieldSize=16;
1009 break;
1010 // also b and c
1011 case RagTime5GraphInternal::Shape::S_Unknown:
1012 #if !defined(__clang__)
1013 default:
1014 #endif
1015 if (val<=0 || val>static_cast<int>(m_state->m_shapeTypeIdVector.size())) {
1016 f << "###type[id]=" << val << ",";
1017 }
1018 else
1019 f << "type=" << RagTime5Graph::printType(m_state->m_shapeTypeIdVector[size_t(val-1)]) << ",";
1020 }
1021 shape->m_transformId=static_cast<int>(input->readLong(2));
1022 shape->m_graphicId=static_cast<int>(input->readLong(2));
1023 if (!dataName.empty())
1024 f << "\"" << dataName.cstr() << "\",";
1025 float dim[4];
1026 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
1027 shape->m_dimension=MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2],dim[3]));
1028 long dataPos=input->tell();
1029 if (shape->m_flags&0xB4)
1030 shape->m_borderId=static_cast<int>(input->readLong(2));
1031
1032 if (input->tell()+typeFieldSize>endPos) {
1033 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: the data size seems too short\n"));
1034 f << "###sz,";
1035
1036 shape->m_extra=f.str();
1037 f.str("");
1038 f << "GraphShape-" << n << ":";
1039 f << *shape;
1040 input->seek(dataPos, librevenge::RVNG_SEEK_SET);
1041 ascFile.addDelimiter(input->tell(),'|');
1042 ascFile.addPos(pos);
1043 ascFile.addNote(f.str().c_str());
1044 return false;
1045 }
1046
1047 bool ok=true;
1048 MWAWBox2f box2;
1049 if (shape->m_type!=RagTime5GraphInternal::Shape::S_Polygon && shape->m_type!=RagTime5GraphInternal::Shape::S_RegularPoly
1050 && shape->m_type!=RagTime5GraphInternal::Shape::S_Spline && shape->m_type!=RagTime5GraphInternal::Shape::S_Group) {
1051 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
1052 if ((dim[2]<=dim[0] || dim[3]<=dim[1]) && shape->m_type != RagTime5GraphInternal::Shape::S_Line) {
1053 f << "###";
1054 ok=false;
1055 }
1056 box2=MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2],dim[3]));
1057 f << "box2=" << box2 << ",";
1058 }
1059 switch (shape->m_type) {
1060 case RagTime5GraphInternal::Shape::S_Rect:
1061 shape->m_shape=MWAWGraphicShape::rectangle(box2);
1062 break;
1063 case RagTime5GraphInternal::Shape::S_Circle:
1064 shape->m_shape=MWAWGraphicShape::circle(box2);
1065 break;
1066 case RagTime5GraphInternal::Shape::S_Line:
1067 shape->m_shape=MWAWGraphicShape::line(box2[0], box2[1]);
1068 break;
1069 case RagTime5GraphInternal::Shape::S_RectOval: {
1070 for (int i=0; i<2; ++i) dim[i]=float(input->readLong(4))/65536.f;
1071 MWAWVec2f corner=MWAWVec2f(dim[1],dim[0]);
1072 f << "round=" << corner << ",";
1073 shape->m_shape=MWAWGraphicShape::rectangle(box2, corner);
1074 break;
1075 }
1076 case RagTime5GraphInternal::Shape::S_Arc:
1077 case RagTime5GraphInternal::Shape::S_Pie: {
1078 float fileAngle[2];
1079 for (auto &angle : fileAngle) angle=360.f *float(input->readLong(4))/65536.f;
1080 f << "angle=" << fileAngle[0] << "x" << fileAngle[0]+fileAngle[1] << ",";
1081 if (fileAngle[1]<0) {
1082 fileAngle[0]+=fileAngle[1];
1083 fileAngle[1]*=-1;
1084 }
1085 float angle[2] = { 90-fileAngle[0]-fileAngle[1], 90-fileAngle[0] };
1086 if (angle[1]>360) {
1087 int numLoop=int(angle[1]/360)-1;
1088 angle[0]-=float(numLoop*360);
1089 angle[1]-=float(numLoop*360);
1090 while (angle[1] > 360) {
1091 angle[0]-=360;
1092 angle[1]-=360;
1093 }
1094 }
1095 if (angle[0] < -360) {
1096 int numLoop=int(angle[0]/360)+1;
1097 angle[0]-=float(numLoop*360);
1098 angle[1]-=float(numLoop*360);
1099 while (angle[0] < -360) {
1100 angle[0]+=360;
1101 angle[1]+=360;
1102 }
1103 }
1104 if (shape->m_type==RagTime5GraphInternal::Shape::S_Arc)
1105 shape->m_shape=MWAWGraphicShape::arc(shape->m_dimension, box2, MWAWVec2f(angle[0],angle[1]));
1106 else
1107 shape->m_shape=MWAWGraphicShape::pie(shape->m_dimension, box2, MWAWVec2f(angle[0],angle[1]));
1108
1109 val=static_cast<int>(input->readLong(2));
1110 if (val) f << "h1=" << val << ",";
1111 break;
1112 }
1113 case RagTime5GraphInternal::Shape::S_TextBox: {
1114 unsigned long id=input->readULong(4);
1115 if ((id&0xfc000000) != 0x4000000) {
1116 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: textbox subId[high] seems odd\n"));
1117 f << "#partId[h]=" << (id>>26) << ",";
1118 }
1119 id &= 0x3ffffff;
1120 if (!id || !cluster.m_textboxZoneId) {
1121 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: find a textbox but can not find textbox zone\n"));
1122 f << "###partId=" << id << ",";
1123 shape->m_linkId=0;
1124 break;
1125 }
1126 if (shape->m_linkId) {
1127 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: link id is already defined\n"));
1128 f << "###linkId,";
1129 }
1130 f << "data" << cluster.m_textboxZoneId << "[" << id << "],";
1131 shape->m_linkId=cluster.m_textboxZoneId;
1132 shape->m_partId=static_cast<int>(-id);
1133 break;
1134 }
1135 case RagTime5GraphInternal::Shape::S_Polygon:
1136 case RagTime5GraphInternal::Shape::S_RegularPoly:
1137 case RagTime5GraphInternal::Shape::S_Spline: {
1138 long actPos=input->tell();
1139 bool isSpline=shape->m_type==RagTime5GraphInternal::Shape::S_Spline;
1140 if (actPos+10+(isSpline ? 8 : 0)>endPos) {
1141 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: can not read the polygon data\n"));
1142 break;
1143 }
1144 val=static_cast<int>(input->readLong(2));
1145 if (val) f << "h1=" << val << ",";
1146 if (isSpline) {
1147 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
1148 if ((dim[2]<=dim[0] || dim[3]<=dim[1]) && shape->m_type != RagTime5GraphInternal::Shape::S_Line) {
1149 f << "###";
1150 ok=false;
1151 }
1152 f << "dim2=" << MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2],dim[3])) << ",";
1153 shape->m_shape.m_bdBox=ok ? MWAWBox2f(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2],dim[3])) : shape->m_dimension;
1154 }
1155 for (int i=0; i<2; ++i) { // h2=0|1
1156 val=static_cast<int>(input->readLong(2));
1157 if (val) f << "h" << i+2 << "=" << val << ",";
1158 }
1159 auto N=static_cast<int>(input->readULong(4));
1160 actPos=input->tell();
1161 if (N<0 || N>(endPos-actPos)/8 || endPos-actPos<N*8+(shape->m_type==RagTime5GraphInternal::Shape::S_RegularPoly ? 6 : 0) ||
1162 N*8+(shape->m_type==RagTime5GraphInternal::Shape::S_RegularPoly ? 6 : 0) < 0) {
1163 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: can not read the polygon number of points\n"));
1164 f << "#N=" << N << ",";
1165 ok=false;
1166 break;
1167 }
1168
1169 if (shape->m_type!=RagTime5GraphInternal::Shape::S_Spline) {
1170 shape->m_shape.m_type = MWAWGraphicShape::Polygon;
1171 std::vector<MWAWVec2f> &vertices=shape->m_shape.m_vertices;
1172 f << "pts=[";
1173 MWAWBox2f box;
1174 for (int i=0; i<N; ++i) {
1175 float coord[2];
1176 for (auto &c : coord) c= float(input->readLong(4))/65536.f;
1177 MWAWVec2f pt(coord[0],coord[1]);
1178 if (i==0)
1179 box=MWAWBox2f(pt, pt);
1180 else
1181 box=box.getUnion(MWAWBox2f(pt,pt));
1182 vertices.push_back(pt);
1183 f << pt << ",";
1184 }
1185 shape->m_shape.m_bdBox=box;
1186 f << "],";
1187 }
1188 else {
1189 f << "pts=[";
1190 std::vector<MWAWVec2f> points;
1191 for (int i=0; i<N; ++i) {
1192 float coord[2];
1193 for (auto &c : coord) c= float(input->readLong(4))/65536.f;
1194 MWAWVec2f pt(coord[0],coord[1]);
1195 points.push_back(pt);
1196 f << pt << ",";
1197 }
1198 f << "],";
1199 if (N%3!=1) {
1200 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: the number of points seems odd\n"));
1201 f << "#N=" << N << ",";
1202 break;
1203 }
1204 shape->m_shape.m_type=MWAWGraphicShape::Path;
1205 auto &path=shape->m_shape.m_path;
1206 path.push_back(MWAWGraphicShape::PathData('M', points[0], points[0], points[0]));
1207 for (size_t i=0; i<size_t(N/3); ++i)
1208 path.push_back(MWAWGraphicShape::PathData('C', points[3*i+3], points[3*i+1], points[3*i+2]));
1209 }
1210 if (shape->m_type!=RagTime5GraphInternal::Shape::S_RegularPoly)
1211 break;
1212 // the number of points with define a regular polygon
1213 f << "N=" << input->readLong(2) << ",";
1214 val=static_cast<int>(input->readLong(4));
1215 if (val) f << "rot=" << 360.*double(val)/65536. << ",";
1216 break;
1217 }
1218 case RagTime5GraphInternal::Shape::S_Group: {
1219 val=static_cast<int>(input->readLong(2)); // always 0?
1220 if (val) f << "h1=" << val << ",";
1221 auto N=static_cast<int>(input->readULong(4));
1222 long actPos=input->tell();
1223 if (actPos+N*4>endPos) {
1224 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: can not read the group number of points\n"));
1225 f << "#N=" << N << ",";
1226 ok=false;
1227 break;
1228 }
1229 for (int i=0; i<N; ++i)
1230 shape->m_childIdList.push_back(static_cast<int>(input->readLong(4)));
1231 break;
1232 }
1233 case RagTime5GraphInternal::Shape::S_Unknown:
1234 #if !defined(__clang__)
1235 default:
1236 #endif
1237 ok=false;
1238 break;
1239 }
1240
1241 shape->m_extra=f.str();
1242 f.str("");
1243 f << "GraphShape-" << n << ":";
1244 f << *shape;
1245
1246 if (shape->m_id==0) {
1247 static bool first=true;
1248 if (first) {
1249 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: checkme: find some shape with no id\n"));
1250 first=false;
1251 }
1252 f << "#noId,";
1253 }
1254 else if (cluster.m_idToShapeMap.find(shape->m_id)!=cluster.m_idToShapeMap.end()) {
1255 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicShape: shape %d already exist\n", shape->m_id));
1256 f << "###duplicatedId,";
1257 }
1258 else
1259 cluster.m_idToShapeMap[shape->m_id]=shape;
1260 ascFile.addPos(pos);
1261 ascFile.addNote(f.str().c_str());
1262 return ok;
1263 }
1264
1265 bool RagTime5Graph::readGraphicUsed(int typeId)
1266 {
1267 if (!typeId)
1268 return false;
1269
1270 auto zone=m_document.getDataZone(typeId);
1271 if (!zone || !zone->m_entry.valid() || (zone->m_entry.length()%10) ||
1272 zone->getKindLastPart(zone->m_kinds[1].empty())!="ItemData") {
1273 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicUsed: the entry of zone %d seems bad\n", typeId));
1274 return false;
1275 }
1276 MWAWEntry entry=zone->m_entry;
1277 MWAWInputStreamPtr input=zone->getInput();
1278 input->setReadInverted(!zone->m_hiLoEndian);
1279 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1280
1281 libmwaw::DebugFile &ascFile=zone->ascii();
1282 libmwaw::DebugStream f;
1283 zone->m_isParsed=true;
1284 ascFile.addPos(entry.end());
1285 ascFile.addNote("_");
1286
1287 f << "Entries(GraphUsed)[" << *zone << "]:";
1288 ascFile.addPos(entry.begin());
1289 ascFile.addNote(f.str().c_str());
1290
1291 auto N=int(entry.length()/10);
1292 for (int i=1; i<=N; ++i) {
1293 long pos=input->tell();
1294 f.str("");
1295 f << "GraphUsed-" << i << ":";
1296
1297 auto used=static_cast<int>(input->readLong(4));
1298 auto id=static_cast<int>(input->readLong(4));
1299 if (id==0) {
1300 ascFile.addPos(pos);
1301 ascFile.addNote("_");
1302 input->seek(pos+10, librevenge::RVNG_SEEK_SET);
1303 continue;
1304 }
1305
1306 auto subId=static_cast<int>(input->readLong(2));
1307 if (subId)
1308 f << "id=" << id << "-" << subId << ",";
1309 else
1310 f << "id=" << id << ",";
1311 if (used!=1)
1312 f << "used=" << used << ",";
1313 ascFile.addPos(pos);
1314 ascFile.addNote(f.str().c_str());
1315 }
1316
1317 input->setReadInverted(false);
1318 return true;
1319 }
1320
1321 ////////////////////////////////////////////////////////////
1322 // transformation
1323 ////////////////////////////////////////////////////////////
1324 bool RagTime5Graph::readGraphicTransformations(RagTime5ClusterManager::Link const &link)
1325 {
1326 if (link.empty() || link.m_ids[0]==0 || link.m_fieldSize<34) {
1327 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicTransformations: can not find the transformation id\n"));
1328 return false;
1329 }
1330
1331 auto dataZone=m_document.getDataZone(link.m_ids[0]);
1332 if (!dataZone || !dataZone->m_entry.valid() || dataZone->m_entry.length()!=link.m_N*link.m_fieldSize ||
1333 dataZone->getKindLastPart(dataZone->m_kinds[1].empty())!="ItemData") {
1334 if (link.m_N==0 && dataZone && !dataZone->m_entry.valid()) {
1335 // an empty transformation zone is ok...
1336 dataZone->m_isParsed=true;
1337 return true;
1338 }
1339
1340 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicTransformations: the transformation zone %d seems bad\n", link.m_ids[0]));
1341 return false;
1342 }
1343 MWAWEntry entry=dataZone->m_entry;
1344 MWAWInputStreamPtr input=dataZone->getInput();
1345 input->setReadInverted(!dataZone->m_hiLoEndian);
1346
1347 dataZone->m_isParsed=true;
1348 libmwaw::DebugFile &ascFile=dataZone->ascii();
1349 libmwaw::DebugStream f;
1350 f << "Entries(GraphTransform)[" << *dataZone << "]:";
1351 ascFile.addPos(entry.begin());
1352 ascFile.addNote(f.str().c_str());
1353 ascFile.addPos(entry.end());
1354 ascFile.addNote("_");
1355
1356 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1357 for (int i=0; i<link.m_N; ++i) {
1358 long pos=input->tell();
1359 f.str("");
1360 f << "GraphTransform-GT" << i+1 << ":mat=[";
1361 for (int j=0; j<9; ++j) {
1362 if ((j%3)==0) f << "[";
1363 bool isShort=(j==8) && (link.m_fieldSize==34);
1364 long val=input->readLong(isShort ? 2 : 4);
1365 if (!val) f << "_";
1366 else if (isShort) f << val;
1367 else f << float(val)/65536.f;
1368 if ((j%3)==2) f << "]";
1369 f << ",";
1370 }
1371 f << "],";
1372 if (input->tell()!=pos+link.m_fieldSize)
1373 ascFile.addDelimiter(input->tell(),'|');
1374 ascFile.addPos(pos);
1375 ascFile.addNote(f.str().c_str());
1376 input->seek(pos+link.m_fieldSize, librevenge::RVNG_SEEK_SET);
1377 }
1378 input->setReadInverted(false);
1379 return true;
1380 }
1381
1382 ////////////////////////////////////////////////////////////
1383 // picture
1384 ////////////////////////////////////////////////////////////
1385 bool RagTime5Graph::readPictureList(RagTime5Zone &zone)
1386 {
1387 libmwaw::DebugFile &ascFile=zone.ascii();
1388 libmwaw::DebugStream f;
1389 if (zone.m_name.empty())
1390 f << "Entries(PictureList)[" << zone << "]:";
1391 else
1392 f << "Entries(" << zone.m_name << ")[pictureList," << zone << "]:";
1393 MWAWEntry &entry=zone.m_entry;
1394 zone.m_isParsed=true;
1395 std::vector<int> listIds;
1396 if (entry.valid()) {
1397 ascFile.addPos(entry.end());
1398 ascFile.addNote("_");
1399 m_document.ascii().addPos(zone.m_defPosition);
1400 m_document.ascii().addNote("picture[list]");
1401
1402 if (entry.length()%4) {
1403 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureList: the entry size seems bad\n"));
1404 f << "###";
1405 ascFile.addPos(entry.begin());
1406 ascFile.addNote(f.str().c_str());
1407 return false;
1408 }
1409
1410 MWAWInputStreamPtr input = zone.getInput();
1411 input->setReadInverted(!zone.m_hiLoEndian); // checkme never seens
1412 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1413
1414 auto N=int(entry.length()/4);
1415 for (int i=0; i<N; ++i) {
1416 auto val=static_cast<int>(input->readLong(2)); // always 1
1417 auto id=static_cast<int>(input->readLong(2));
1418 if (val==1) {
1419 f << "Data" << id << ",";
1420 listIds.push_back(id);
1421 }
1422 else if (val)
1423 f << "#" << i << ":" << val << ",";
1424 }
1425 ascFile.addPos(entry.begin());
1426 ascFile.addNote(f.str().c_str());
1427 input->setReadInverted(false);
1428 }
1429 else if (zone.m_variableD[0]==1)
1430 listIds.push_back(zone.m_variableD[1]);
1431 else {
1432 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureList: can not find the list of pictures\n"));
1433 return false;
1434 }
1435 for (int listId : listIds) {
1436 auto dataZone=m_document.getDataZone(listId);
1437 if (!dataZone) continue;
1438 readPictureRep(*dataZone);
1439 }
1440 return true;
1441 }
1442
1443 bool RagTime5Graph::readPictureRep(RagTime5Zone &zone)
1444 {
1445 if (!zone.m_entry.valid() ||
1446 m_state->getPictureType(zone.getKindLastPart())==RagTime5GraphInternal::State::P_Unknown) {
1447 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureRep: can not find the picture %d\n", zone.m_ids[0]));
1448 return false;
1449 }
1450 readPictureData(zone);
1451
1452 for (auto const &it : zone.m_childIdToZoneMap) {
1453 std::shared_ptr<RagTime5Zone> child=it.second;
1454 if (!child || child->m_isParsed) continue;
1455 child->m_isParsed=true;
1456 std::string kind=child->getKindLastPart();
1457 if (kind=="ScreenRepMatchData" || kind=="ScreenRepMatchDataColor") {
1458 readPictureMatch(*child, kind=="ScreenRepMatchDataColor");
1459 continue;
1460 }
1461
1462 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureRep: find unknown child for picture list zone %d\n", child->m_ids[0]));
1463 child->addErrorInDebugFile("PictureList");
1464 }
1465 return true;
1466 }
1467
1468 bool RagTime5Graph::readPictureContainer(RagTime5Zone &zone)
1469 {
1470 zone.m_isParsed=true;
1471 libmwaw::DebugFile &mainAscii=m_document.ascii();
1472 mainAscii.addPos(zone.m_defPosition);
1473 mainAscii.addNote("pict[container]");
1474 if (zone.m_entry.valid()) {
1475 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureContainer: find unexpected data in a picture container\n"));
1476 zone.ascii().addPos(zone.m_entry.begin());
1477 zone.ascii().addNote("Entries(PictureContainer):###");
1478 }
1479 if (zone.m_childIdToZoneMap.empty()) {
1480 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureContainer: find a picture container %d without any child\n", zone.m_ids[0]));
1481 return false;
1482 }
1483 std::shared_ptr<MWAWEmbeddedObject> picture(new MWAWEmbeddedObject);
1484 for (auto const &cIt : zone.m_childIdToZoneMap) {
1485 std::shared_ptr<RagTime5Zone> child=cIt.second;
1486 if (!child) continue;
1487 child->m_isParsed=true;
1488 if (cIt.first==8) {
1489 if (child->m_variableD[0] || child->m_variableD[1]<=0 || child->m_entry.valid()) {
1490 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureContainer: refCount seems odd\n"));
1491 mainAscii.addPos(child->m_defPosition);
1492 mainAscii.addNote("###badRef[pictContainer]");
1493 }
1494 continue;
1495 }
1496 // the screen representation
1497 if (child->getKindLastPart(child->m_kinds[1].empty())=="ScreenRepList") {
1498 if (child->m_entry.valid() || (child->m_variableD[0]==1 && child->m_variableD[1])) {
1499 readPictureList(*child);
1500 continue;
1501 }
1502 // screenrep can be also undefined
1503 mainAscii.addPos(child->m_defPosition);
1504 mainAscii.addNote("[empty]");
1505 continue;
1506 }
1507 if (child->getKindLastPart()=="TCubics" && child->m_entry.valid()) {
1508 libmwaw::DebugFile &ascFile=child->ascii();
1509 libmwaw::DebugStream f;
1510 f << "Entries(TCubics):" << *child;
1511 ascFile.addPos(child->m_entry.begin());
1512 ascFile.addNote(f.str().c_str());
1513 ascFile.addPos(child->m_entry.end());
1514 ascFile.addNote("_");
1515 continue;
1516 }
1517 librevenge::RVNGBinaryData data;
1518 std::string type;
1519 if (child->m_entry.valid() && readPictureData(*child, data, type)) {
1520 if (data.empty())
1521 continue;
1522 picture->add(data,type);
1523 continue;
1524 }
1525
1526 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureContainer: find unknown child zone\n"));
1527 mainAscii.addPos(child->m_defPosition);
1528 mainAscii.addNote("###unknChild[pictContainer]");
1529 }
1530 if (picture->m_dataList.empty()) {
1531 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureContainer: oops can not find any picture for zone %d\n", zone.m_ids[0]));
1532 }
1533 else if (m_state->m_idPictureMap.find(zone.m_ids[0])!=m_state->m_idPictureMap.end()) {
1534 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureContainer: a picture already exists for id %d\n", zone.m_ids[0]));
1535 }
1536 else
1537 m_state->m_idPictureMap[zone.m_ids[0]]=picture;
1538 return true;
1539 }
1540
1541 bool RagTime5Graph::readPictureData(RagTime5Zone &zone)
1542 {
1543 librevenge::RVNGBinaryData data;
1544 std::string type;
1545 return readPictureData(zone, data, type);
1546 }
1547 bool RagTime5Graph::readPictureData(RagTime5Zone &zone, librevenge::RVNGBinaryData &pictData, std::string &pictType)
1548 {
1549 pictData.clear();
1550 MWAWEntry const &entry=zone.m_entry;
1551 if (entry.length()<=40)
1552 return false;
1553 auto type=m_state->getPictureType(zone.getKindLastPart());
1554 bool testForScreenRep=false;
1555 if (type==RagTime5GraphInternal::State::P_ScreenRep && !zone.m_kinds[1].empty()) {
1556 type=m_state->getPictureType(zone.getKindLastPart(false));
1557 if (type==RagTime5GraphInternal::State::P_Unknown)
1558 type=RagTime5GraphInternal::State::P_ScreenRep;
1559 else
1560 testForScreenRep=true;
1561 }
1562 if (type==RagTime5GraphInternal::State::P_Unknown)
1563 return false;
1564 MWAWInputStreamPtr input = zone.getInput();
1565 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1566 long val;
1567 std::string extension("");
1568 bool ok=true;
1569 switch (type) {
1570 case RagTime5GraphInternal::State::P_Epsf:
1571 val=long(input->readULong(4));
1572 if (val!=long(0xc5d0d3c6) && val != long(0x25215053)) {
1573 ok=false;
1574 break;
1575 }
1576 extension="eps";
1577 pictType="application/postscript";
1578 #if 0
1579 // when header==0xc5d0d3c6, we may want to decompose the data
1580 input->setReadInverted(true);
1581 MWAWEntry fEntry[3];
1582 for (int i=0; i<3; ++i) {
1583 fEntry[i].setBegin(long(input->readULong(4)));
1584 fEntry[i].setLength(long(input->readULong(4)));
1585 if (!fEntry[i].length()) continue;
1586 f << "decal" << i << "=" << std::hex << fEntry[i].begin() << "<->" << fEntry[i].end() << std::dec << ",";
1587 if (fEntry[i].begin()<0x1c ||fEntry[i].end()>zone.m_entry.length()) {
1588 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureData: the address %d seems too big\n", i));
1589 fEntry[i]=MWAWEntry();
1590 f << "###";
1591 continue;
1592 }
1593 }
1594 for (int i=0; i<2; ++i) { // always -1,0
1595 if (input->tell()>=pos+fDataPos)
1596 break;
1597 val=static_cast<int>(input->readLong(2));
1598 if (val!=i-1)
1599 f << "f" << i << "=" << val << ",";
1600 }
1601 // now first fEntry=eps file, second WMF?, third=tiff file
1602 #endif
1603 break;
1604 case RagTime5GraphInternal::State::P_Jpeg:
1605 val=long(input->readULong(2));
1606 // jpeg format begin by 0xffd8 and jpeg-2000 format begin by 0000 000c 6a50...
1607 if (val!=0xffd8 && (val!=0 || input->readULong(4)!=0xc6a50 || input->readULong(4)!=0x20200d0a)) {
1608 ok=false;
1609 break;
1610 }
1611 extension="jpg";
1612 pictType="image/jpeg";
1613 break;
1614 case RagTime5GraphInternal::State::P_Pict:
1615 input->seek(10, librevenge::RVNG_SEEK_CUR);
1616 val=long(input->readULong(2));
1617 if (val!=0x1101 && val !=0x11) {
1618 ok=false;
1619 break;
1620 }
1621 extension="pct";
1622 pictType="image/x-pict";
1623 break;
1624 case RagTime5GraphInternal::State::P_PNG:
1625 if (input->readULong(4) != 0x89504e47) {
1626 ok=false;
1627 break;
1628 }
1629 extension="png";
1630 pictType="image/png";
1631 break;
1632 case RagTime5GraphInternal::State::P_ScreenRep:
1633 val=long(input->readULong(1));
1634 if (val!=0x49 && val!=0x4d) {
1635 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureData: find unknown picture format for zone %d\n", zone.m_ids[0]));
1636 ok=false;
1637 break;
1638 }
1639 extension="sRep";
1640 pictType="image/tiff"; // chekme
1641 break;
1642 case RagTime5GraphInternal::State::P_Tiff:
1643 val=long(input->readULong(2));
1644 if (val!=0x4949 && val != 0x4d4d) {
1645 ok=false;
1646 break;
1647 }
1648 pictType="image/tiff";
1649 val=long(input->readULong(2));
1650 /* find also frequently 4d4d 00dd b300 d61e here ?
1651 and one time 4d 00 b3 2a d6 */
1652 if (val!=0x2a00 && val!=0x002a) {
1653 static bool first=true;
1654 if (first) {
1655 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureData: some tiffs seem bad, zone %d, ...\n", zone.m_ids[0]));
1656 first=false;
1657 }
1658 extension="check.tiff";
1659 }
1660 else
1661 extension="tiff";
1662 break;
1663 case RagTime5GraphInternal::State::P_WMF:
1664 if (input->readULong(4)!=0x01000900) {
1665 ok=false;
1666 break;
1667 }
1668 extension="wmf";
1669 pictType="image/wmf";
1670 break;
1671 // coverity[dead_error_line : FALSE]: intended, needed for avoiding compiler warning
1672 case RagTime5GraphInternal::State::P_Unknown:
1673 #if !defined(__clang__)
1674 default:
1675 #endif
1676 ok=false;
1677 break;
1678 }
1679 if (!ok && testForScreenRep) {
1680 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1681 val=long(input->readULong(1));
1682 if (val==0x49 || val==0x4d) {
1683 ok=true;
1684 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureData: find unknown picture format for zone %d\n", zone.m_ids[0]));
1685 extension="sRep";
1686 #ifdef DEBUG_WITH_FILES
1687 type=RagTime5GraphInternal::State::P_ScreenRep;
1688 #endif
1689 }
1690 }
1691 zone.m_isParsed=true;
1692 libmwaw::DebugStream f;
1693 f << "picture[" << extension << "],";
1694 m_document.ascii().addPos(zone.m_defPosition);
1695 m_document.ascii().addNote(f.str().c_str());
1696 if (!ok) {
1697 f.str("");
1698 f << "Entries(BADPICT)[" << zone << "]:###";
1699 libmwaw::DebugFile &ascFile=zone.ascii();
1700 ascFile.addPos(zone.m_entry.begin());
1701 ascFile.addNote(f.str().c_str());
1702 return true;
1703 }
1704 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1705 input->readDataBlock(entry.length(), pictData);
1706 #ifdef DEBUG_WITH_FILES
1707 if (type==RagTime5GraphInternal::State::P_ScreenRep) {
1708 libmwaw::DebugFile &ascFile=zone.ascii();
1709 f.str("");
1710 f << "Entries(ScrRep)[" << zone << "]:";
1711 ascFile.addPos(zone.m_entry.begin());
1712 ascFile.addNote(f.str().c_str());
1713 return true;
1714 }
1715 if (zone.isMainInput())
1716 m_document.ascii().skipZone(entry.begin(), entry.end()-1);
1717 static int volatile pictName = 0;
1718 f.str("");
1719 f << "Pict-" << ++pictName << "." << extension;
1720 libmwaw::Debug::dumpFile(pictData, f.str().c_str());
1721 #endif
1722 return true;
1723 }
1724
1725 bool RagTime5Graph::readPictureMatch(RagTime5Zone &zone, bool color)
1726 {
1727 libmwaw::DebugFile &ascFile=zone.ascii();
1728 libmwaw::DebugStream f;
1729 if (zone.m_name.empty())
1730 f << "Entries(" << (color ? "PictureColMatch" : "PictureMatch") << ")[" << zone << "]:";
1731 else
1732 f << "Entries(" << zone.m_name << "[" << (color ? "pictureColMatch" : "pictureMatch") << ")[" << zone << "]:";
1733 MWAWEntry &entry=zone.m_entry;
1734 zone.m_isParsed=true;
1735 ascFile.addPos(entry.end());
1736 ascFile.addNote("_");
1737 m_document.ascii().addPos(zone.m_defPosition);
1738 m_document.ascii().addNote(color ? "picture[matchCol]" : "picture[match]");
1739
1740 int const expectedSz=color ? 42 : 32;
1741 if (entry.length() != expectedSz) {
1742 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureMatch: the entry size seems bad\n"));
1743 f << "###";
1744 ascFile.addPos(entry.begin());
1745 ascFile.addNote(f.str().c_str());
1746 return false;
1747 }
1748
1749 MWAWInputStreamPtr input = zone.getInput();
1750 input->setReadInverted(!zone.m_hiLoEndian); // checkme never seens
1751 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1752
1753 int val;
1754 for (int i=0; i<4; ++i) {
1755 static int const expected[]= {0,0,0x7fffffff,0x7fffffff};
1756 val=static_cast<int>(input->readLong(4));
1757 if (val!=expected[i])
1758 f << "f" << i << "=" << val << ",";
1759 }
1760 int dim[2];
1761 for (int &i : dim)
1762 i=static_cast<int>(input->readLong(2));
1763 f << "dim=" << dim[0] << "x" << dim[1] << ",";
1764 for (int i=0; i<2; ++i) { // f2=0-3, f4=0-1
1765 val=static_cast<int>(input->readLong(2));
1766 if (val)
1767 f << "f" << i+2 << "=" << val << ",";
1768 }
1769 // a very big number
1770 f << "ID?=" << std::hex << input->readULong(4) << std::dec << ",";
1771 for (int i=0; i<2; ++i) { // f5=f6=0
1772 val=static_cast<int>(input->readLong(2));
1773 if (val)
1774 f << "f" << i+4 << "=" << val << ",";
1775 }
1776 if (color) {
1777 for (int i=0; i<5; ++i) { // g0=a|32, g1=0|1, other 0, color and pattern ?
1778 val=static_cast<int>(input->readLong(2));
1779 if (val)
1780 f << "g" << i << "=" << val << ",";
1781 }
1782 }
1783 input->setReadInverted(false);
1784 ascFile.addPos(entry.begin());
1785 ascFile.addNote(f.str().c_str());
1786
1787 return true;
1788 }
1789
1790 ////////////////////////////////////////////////////////////
1791 //
1792 // Low level
1793 //
1794 ////////////////////////////////////////////////////////////
1795
1796 ////////////////////////////////////////////////////////////
1797 // interface send function
1798 ////////////////////////////////////////////////////////////
1799 void RagTime5Graph::flushExtra(bool onlyCheck)
1800 {
1801 MWAWListenerPtr listener=m_parserState->getMainListener();
1802 if (!listener) {
1803 MWAW_DEBUG_MSG(("RagTime5Graph::flushExtra: can not find the listener\n"));
1804 return;
1805 }
1806
1807 MWAWPosition position(MWAWVec2f(0,0), MWAWVec2f(100,100), librevenge::RVNG_POINT);
1808 position.m_anchorTo=MWAWPosition::Char;
1809 for (auto const &gIt : m_state->m_idGraphicMap) {
1810 if (!gIt.second || gIt.second->m_isSent)
1811 continue;
1812 static bool first=true;
1813 if (first) {
1814 MWAW_DEBUG_MSG(("RagTime5Graph::flushExtra: find some unsent graphic zones\n"));
1815 first=false;
1816 }
1817 if (!onlyCheck)
1818 send(*gIt.second, listener, position);
1819 }
1820 for (auto const &pIt : m_state->m_idPictClusterMap) {
1821 if (!pIt.second || pIt.second->m_isSent)
1822 continue;
1823 static bool first=true;
1824 if (first) {
1825 MWAW_DEBUG_MSG(("RagTime5Graph::flushExtra: find some unsent picture zones\n"));
1826 first=false;
1827 }
1828 if (!onlyCheck)
1829 send(*pIt.second, listener, position);
1830 }
1831 }
1832
1833 bool RagTime5Graph::sendButtonZoneAsText(MWAWListenerPtr listener, int zId)
1834 {
1835 if (!listener)
1836 listener=m_parserState->getMainListener();
1837 if (!listener || !listener->canWriteText()) {
1838 MWAW_DEBUG_MSG(("RagTime5Graph::sendButtonZoneAsText: can not find the listener\n"));
1839 return false;
1840 }
1841 auto it=m_state->m_idButtonMap.find(zId);
1842 if (it==m_state->m_idButtonMap.end() || !it->second) {
1843 MWAW_DEBUG_MSG(("RagTime5Graph::sendButtonZoneAsText: can not find the button for zone %d\n", zId));
1844 return false;
1845 }
1846 auto const &button=*it->second;
1847 auto itemIt=button.m_idToItemStringMap.find((button.m_buttonType==1 || button.m_buttonType==5) ? 1 : button.m_item); // for push button, retrieve item 1
1848 if (itemIt!=button.m_idToItemStringMap.end())
1849 listener->insertUnicodeString(itemIt->second);
1850 else if (button.m_item) {
1851 MWAW_DEBUG_MSG(("RagTime5Graph::sendButtonZoneAsText: can not find the button item %d for zone %d\n", button.m_item, zId));
1852 }
1853 return true;
1854 }
1855
1856 bool RagTime5Graph::send(RagTime5GraphInternal::ClusterButton &cluster,
1857 MWAWListenerPtr listener, MWAWPosition const &position, MWAWGraphicStyle const &style)
1858 {
1859 cluster.m_isSent=true;
1860 if (!listener)
1861 listener=m_parserState->getMainListener();
1862 if (!listener) {
1863 MWAW_DEBUG_MSG(("RagTime5Graph::send: can not find the listener\n"));
1864 return false;
1865 }
1866
1867 std::shared_ptr<MWAWSubDocument> doc(new RagTime5GraphInternal::SubDocument(*this, m_parserState->m_input, cluster.m_zoneId, 0, true, position.size()[0]));
1868 MWAWPosition pos(position);
1869 pos.m_wrapping=MWAWPosition::WDynamic;
1870 listener->insertTextBox(pos, doc, style);
1871 return true;
1872 }
1873
1874 bool RagTime5Graph::send(RagTime5GraphInternal::Shape const &shape, RagTime5GraphInternal::ClusterGraphic const &cluster,
1875 MWAWListenerPtr listener, MWAWPosition const &position)
1876 {
1877 if (!listener)
1878 listener=m_parserState->getMainListener();
1879 if (!listener) {
1880 MWAW_DEBUG_MSG(("RagTime5Graph::send: can not find the listener\n"));
1881 return false;
1882 }
1883 auto type=shape.m_linkId ? m_document.getClusterType(shape.m_linkId) : RagTime5ClusterManager::Cluster::C_Unknown;
1884 bool sendAsTextBox=type==RagTime5ClusterManager::Cluster::C_ButtonZone ||
1885 type==RagTime5ClusterManager::Cluster::C_TextZone;
1886 MWAWGraphicStyle style=MWAWGraphicStyle::emptyStyle();
1887 if (shape.m_type==RagTime5GraphInternal::Shape::S_Line ||
1888 shape.m_type==RagTime5GraphInternal::Shape::S_Arc) {
1889 if (shape.m_graphicId)
1890 m_styleManager->updateBorderStyle(shape.m_graphicId, style, true);
1891 }
1892 else {
1893 if (shape.m_borderId)
1894 m_styleManager->updateBorderStyle(shape.m_borderId, style, false);
1895 if (shape.m_graphicId) {
1896 if (sendAsTextBox && listener->getType()!=MWAWListener::Graphic && listener->getType()!=MWAWListener::Presentation)
1897 m_styleManager->updateFrameStyle(shape.m_graphicId, style);
1898 else
1899 m_styleManager->updateSurfaceStyle(shape.m_graphicId, style);
1900 }
1901 }
1902 if ((shape.m_flags&3) && style.hasLine() &&
1903 (shape.m_type==RagTime5GraphInternal::Shape::S_Line ||
1904 shape.m_type==RagTime5GraphInternal::Shape::S_Arc ||
1905 shape.m_type==RagTime5GraphInternal::Shape::S_Spline)) {
1906 // by construction arc wise is inverted
1907 int wh=shape.m_type==RagTime5GraphInternal::Shape::S_Arc ? 1 : 0;
1908 auto arrow=MWAWGraphicStyle::Arrow::plain();
1909 arrow.m_width=2.f*style.m_lineWidth+2.f;
1910 if (shape.m_flags&1)
1911 style.m_arrows[wh]=arrow;
1912 if (shape.m_flags&2)
1913 style.m_arrows[1-wh]=arrow;
1914 }
1915 MWAWBox2f bdbox=shape.getBdBox();
1916 MWAWPosition pos(position);
1917 pos.setOrigin(bdbox[0]);
1918 pos.setSize(bdbox.size());
1919 pos.setUnit(librevenge::RVNG_POINT);
1920 /*if ((shape.m_flags&0x40)==0)
1921 pos.m_wrapping=MWAWPosition::WForeground;
1922 else */
1923 pos.m_wrapping=MWAWPosition::WParallel;
1924 if (type==RagTime5ClusterManager::Cluster::C_TextZone) {
1925 std::shared_ptr<MWAWSubDocument> doc(new RagTime5GraphInternal::SubDocument(*this, m_parserState->m_input, shape.m_linkId, shape.m_partId, false, bdbox.size()[0]));
1926 pos.m_wrapping=MWAWPosition::WDynamic;
1927 if (shape.m_type==RagTime5GraphInternal::Shape::S_Rect || shape.m_type==RagTime5GraphInternal::Shape::S_TextBox)
1928 listener->insertTextBox(pos, doc, style);
1929 else
1930 listener->insertTextBoxInShape(pos, doc, shape.m_shape, style);
1931 return true;
1932 }
1933 else if (type==RagTime5ClusterManager::Cluster::C_PictureZone &&
1934 m_state->m_idPictClusterMap.find(shape.m_linkId)!=m_state->m_idPictClusterMap.end() && m_state->m_idPictClusterMap.find(shape.m_linkId)->second) {
1935 return send(*m_state->m_idPictClusterMap.find(shape.m_linkId)->second, listener, pos);
1936 }
1937 else if (type==RagTime5ClusterManager::Cluster::C_ButtonZone &&
1938 m_state->m_idButtonMap.find(shape.m_linkId)!=m_state->m_idButtonMap.end() && m_state->m_idButtonMap.find(shape.m_linkId)->second) {
1939 return send(*m_state->m_idButtonMap.find(shape.m_linkId)->second, listener, pos, style);
1940 }
1941 else if (type==RagTime5ClusterManager::Cluster::C_Pipeline) {
1942 type=m_document.getPipelineContainerType(shape.m_linkId);
1943 if (type==RagTime5ClusterManager::Cluster::C_TextZone) {
1944 pos.m_wrapping=MWAWPosition::WDynamic;
1945 std::shared_ptr<MWAWSubDocument> doc(new RagTime5GraphInternal::SubDocument(*this, m_parserState->m_input, shape.m_linkId, shape.m_partId, false, bdbox.size()[0]));
1946 if (shape.m_type==RagTime5GraphInternal::Shape::S_Rect || shape.m_type==RagTime5GraphInternal::Shape::S_TextBox)
1947 listener->insertTextBox(pos, doc, style);
1948 else
1949 listener->insertTextBoxInShape(pos, doc, shape.m_shape, style);
1950 return true;
1951 }
1952 else if (type==RagTime5ClusterManager::Cluster::C_SpreadsheetZone) {
1953 pos.m_wrapping=MWAWPosition::WDynamic;
1954 m_document.send(shape.m_linkId, listener, pos, shape.m_partId);
1955 }
1956 return true;
1957 }
1958 else if (type==RagTime5ClusterManager::Cluster::C_SpreadsheetZone) {
1959 pos.m_wrapping=MWAWPosition::WDynamic;
1960 m_document.send(shape.m_linkId, listener, pos, shape.m_partId);
1961 return true;
1962 }
1963
1964 switch (shape.m_type) {
1965 case RagTime5GraphInternal::Shape::S_Arc:
1966 case RagTime5GraphInternal::Shape::S_Circle:
1967 case RagTime5GraphInternal::Shape::S_Line:
1968 case RagTime5GraphInternal::Shape::S_Pie:
1969 case RagTime5GraphInternal::Shape::S_Polygon:
1970 case RagTime5GraphInternal::Shape::S_Rect:
1971 case RagTime5GraphInternal::Shape::S_RectOval:
1972 case RagTime5GraphInternal::Shape::S_RegularPoly:
1973 case RagTime5GraphInternal::Shape::S_Spline:
1974 listener->insertShape(pos, shape.m_shape, style);
1975 break;
1976 case RagTime5GraphInternal::Shape::S_Group: {
1977 bool openGroup=listener->openGroup(pos);
1978 for (auto childId : shape.m_childIdList) {
1979 if (cluster.m_idToShapeMap.find(childId)!=cluster.m_idToShapeMap.end() &&
1980 cluster.m_idToShapeMap.find(childId)->second)
1981 send(*cluster.m_idToShapeMap.find(childId)->second, cluster, listener, pos);
1982 }
1983 if (openGroup)
1984 listener->closeGroup();
1985 break;
1986 }
1987 case RagTime5GraphInternal::Shape::S_TextBox:
1988 case RagTime5GraphInternal::Shape::S_Unknown:
1989 #if !defined(__clang__)
1990 default:
1991 #endif
1992 {
1993 static bool first=true;
1994 if (first) {
1995 MWAW_DEBUG_MSG(("RagTime5Graph::send: sorry sending some shape is not implemented\n"));
1996 first=false;
1997 }
1998 break;
1999 }
2000 }
2001 return true;
2002 }
2003
2004 bool RagTime5Graph::send(RagTime5GraphInternal::ClusterGraphic &cluster, MWAWListenerPtr listener, MWAWPosition const &pos)
2005 {
2006 cluster.m_isSent=true;
2007 if (!listener)
2008 listener=m_parserState->getMainListener();
2009 if (!listener) {
2010 MWAW_DEBUG_MSG(("RagTime5Graph::send: can not find the listener\n"));
2011 return false;
2012 }
2013 size_t numShapes=cluster.m_rootIdList.size();
2014 for (size_t i=0; i<numShapes; ++i) {
2015 int shapeId=cluster.m_rootIdList[i];
2016 if (cluster.m_idToShapeMap.find(shapeId)==cluster.m_idToShapeMap.end() ||
2017 !cluster.m_idToShapeMap.find(shapeId)->second)
2018 continue;
2019 MWAWPosition position(pos);
2020 position.setOrder(static_cast<int>(i+1));
2021 //position.setOrder(static_cast<int>(numShapes-i+2));
2022 send(*cluster.m_idToShapeMap.find(shapeId)->second, cluster, listener, position);
2023 }
2024 return true;
2025 }
2026
2027 bool RagTime5Graph::send(RagTime5GraphInternal::ClusterPicture &cluster, MWAWListenerPtr listener, MWAWPosition const &position)
2028 {
2029 cluster.m_isSent=true;
2030 if (!listener)
2031 listener=m_parserState->getMainListener();
2032 if (!listener) {
2033 MWAW_DEBUG_MSG(("RagTime5Graph::send: can not find the listener\n"));
2034 return false;
2035 }
2036 if (!cluster.m_containerId ||
2037 m_state->m_idPictureMap.find(cluster.m_containerId)==m_state->m_idPictureMap.end() ||
2038 !m_state->m_idPictureMap.find(cluster.m_containerId)->second) {
2039 MWAW_DEBUG_MSG(("RagTime5Graph::send: can not find picture for zone %d\n", cluster.m_zoneId));
2040 return false;
2041 }
2042 listener->insertPicture(position, *m_state->m_idPictureMap.find(cluster.m_containerId)->second);
2043 return true;
2044 }
2045
2046 ////////////////////////////////////////////////////////////
2047 //
2048 // read cluster data
2049 //
2050 ////////////////////////////////////////////////////////////
2051
2052 namespace RagTime5GraphInternal
2053 {
2054 //
2055 //! low level: parser of script cluster : zone 2,a,4002,400a
2056 //
2057 struct ButtonCParser final : public RagTime5ClusterManager::ClusterParser {
2058 enum { F_nextId, F_formula, F_formulaRoot, F_name, F_parentList, F_itemNames, F_buttonList };
2059 //! constructor
2060 ButtonCParser(RagTime5ClusterManager &parser, int type)
2061 : ClusterParser(parser, type, "ClustButton")
2062 , m_cluster(new ClusterButton)
2063 , m_fieldName("")
2064
2065 , m_expectedIdToType()
2066 , m_idStack()
2067 {
2068 m_cluster->m_type=RagTime5ClusterManager::Cluster::C_ButtonZone;
2069 }
2070 //! destructor
2071 ~ButtonCParser() final;
2072 //! return the current cluster
2073 std::shared_ptr<RagTime5ClusterManager::Cluster> getCluster() final
2074 {
2075 return m_cluster;
2076 }
2077 //! return the button cluster
2078 std::shared_ptr<RagTime5GraphInternal::ClusterButton> getButtonCluster()
2079 {
2080 return m_cluster;
2081 }
2082
2083 //! set a data id type
2084 void setExpectedType(int id, int type)
2085 {
2086 m_expectedIdToType[id]=type;
2087 m_idStack.push(id);
2088 }
2089 /** returns to new zone to parse. */
2090 int getNewZoneToParse() final
2091 {
2092 if (m_idStack.empty())
2093 return -1;
2094 int id=m_idStack.top();
2095 m_idStack.pop();
2096 return id;
2097 }
2098 //! parse a zone
2099 bool parseZone(MWAWInputStreamPtr &input, long fSz, int N, int flag, libmwaw::DebugStream &f) final
2100 {
2101 if (N==-5)
2102 return parseHeaderZone(input, fSz, N, flag, f);
2103
2104 auto const &it=m_expectedIdToType.find(m_dataId);
2105 int expected=(it!=m_expectedIdToType.end())?it->second : -1;
2106 if (expected!=-1)
2107 f << "[F" << m_dataId << "]";
2108 if (flag!=0x10)
2109 f << "fl=" << std::hex << flag << std::dec << ",";
2110 m_fieldName="";
2111 if (N<0 && expected!=F_name) {
2112 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: find unexpected data block\n"));
2113 f << "###N=" << N << ",";
2114 return true;
2115 }
2116 m_link.m_N=N;
2117 int val;
2118 long linkValues[4];
2119 std::string mess;
2120 switch (expected) {
2121 case F_name:
2122 m_fieldName="script:name";
2123 f << m_fieldName <<",";
2124 if (!isANameHeader(N)) {
2125 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: not a name file\n"));
2126 f << "###";
2127 return true;
2128 }
2129 return true;
2130 case F_formula:
2131 m_fieldName="formula";
2132 f << m_fieldName <<",";
2133 if (fSz<30) {
2134 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: the expected field[%d] seems bad\n", expected));
2135 f << "##fSz=" << fSz << ",";
2136 return true;
2137 }
2138 for (int i=0; i<4; ++i) { // f3=1
2139 val=int(input->readLong(2));
2140 if (val) f << "f" << i << "=" << val << ",";
2141 }
2142 val=int(input->readULong(4));
2143 if (val) // c800/de80
2144 f << "fl=" << std::hex << val << std::dec << ",";
2145 val=int(input->readULong(4));
2146 if (val!=0x1d4e042)
2147 f << "type1=" << std::hex << val << std::dec << ",";
2148 for (int i=0; i<4; ++i) { // f4=1
2149 val=int(input->readLong(2));
2150 if (val) f << "f" << i+4 << "=" << val << ",";
2151 }
2152 return true;
2153 case F_formulaRoot:
2154 case F_itemNames:
2155 case F_buttonList:
2156 case F_buttonList+1:
2157 case F_buttonList+2: {
2158 if (expected==F_formulaRoot && fSz==36) {
2159 val=static_cast<int>(input->readLong(4));
2160 if (val) f << "#f0=" << val << ",";
2161 val=static_cast<int>(input->readLong(4));
2162 if (val!=0x17db042) {
2163 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: find unexpected type0\n"));
2164 f << "#fileType0=" << std::hex << val << std::dec << ",";
2165 }
2166 for (int i=0; i<2; ++i) {
2167 val=static_cast<int>(input->readLong(4));
2168 if (val) f << "f" << i+1 << "=" << val << ",";
2169 }
2170 val=static_cast<int>(input->readULong(2));
2171 if ((val&0xFFD7)!=0x10) {
2172 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: find unexpected type1[fSz36]\n"));
2173 f << "#fileType1=" << std::hex << val << std::dec << ",";
2174 }
2175 f << "ids=[";
2176 for (int i=0; i<3; ++i) {
2177 val=static_cast<int>(input->readLong(4));
2178 if (!val) {
2179 f << "_,";
2180 continue;
2181 }
2182 setExpectedType(val-1,F_buttonList+i);
2183 f << "F" << val-1 << ",";
2184 }
2185 f << "],";
2186 return true;
2187 }
2188 if (fSz<28 || !readLinkHeader(input, fSz, m_link, linkValues, mess)) {
2189 f << "###fType=" << RagTime5ClusterManager::printType(m_link.m_fileType[0]) << ",";
2190 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: the expected field[%d] seems bad\n", expected));
2191 return true;
2192 }
2193 f << m_link << "," << mess;
2194 long expectedFileType1=-1, expectedFieldSize=0;
2195 if (expected==F_itemNames && fSz==32) {
2196 expectedFileType1=0x600;
2197 m_link.m_name="itemName";
2198 m_link.m_type=RagTime5ClusterManager::Link::L_UnicodeList;
2199 }
2200 else if (expected==F_formulaRoot && fSz==29) {
2201 if (m_link.m_fileType[0]!=0x3c052) {
2202 f << "###fType=" << RagTime5ClusterManager::printType(m_link.m_fileType[0]) << ",";
2203 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: the expected field[%d] seems bad\n", expected));
2204 }
2205 if (linkValues[0]!=0x1454877) {
2206 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: find unexpected linkValue[0]\n"));
2207 f << "#lValues0,";
2208 }
2209 expectedFileType1=0x50;
2210 m_link.m_name="formula[root]";
2211 val=int(input->readULong(1));
2212 if (val) f << "g0=" << val << ",";
2213 }
2214 else if (expected==F_buttonList && m_link.m_fileType[0]==0x3e800)
2215 m_link.m_name="buttonList0";
2216 else if (expected==F_buttonList+1 && m_link.m_fileType[0]==0x35800)
2217 m_link.m_name="buttonList1";
2218 else if (expected==F_buttonList+2 && m_link.m_fileType[0]==0x45080) {
2219 m_link.m_name="buttonListInt";
2220 expectedFieldSize=2;
2221 }
2222 else {
2223 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: the expected field[%d] seems bad\n", expected));
2224 f << "###";
2225 }
2226 if (!m_link.m_name.empty()) {
2227 f << m_link.m_name << ",";
2228 m_fieldName=m_link.m_name;
2229 }
2230 if (expectedFileType1>=0 && long(m_link.m_fileType[1]&0xFFD7)!=expectedFileType1) {
2231 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: the expected field[%d] fileType1 seems odd\n", expected));
2232 f << "###fileType1=" << std::hex << m_link.m_fileType[1] << std::dec << ",";
2233 }
2234 if (expectedFieldSize>0 && m_link.m_fieldSize!=expectedFieldSize) {
2235 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: fieldSize seems odd[%d]\n", expected));
2236 f << "###fieldSize,";
2237 }
2238 return true;
2239 }
2240 case F_nextId:
2241 case F_parentList:
2242 default:
2243 break;
2244 }
2245 if (expected==-1) {
2246 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: find unexpected field[%d]\n", m_dataId));
2247 f << "###";
2248 }
2249
2250 switch (fSz) {
2251 case 36:
2252 f << "parentList,";
2253 if (!readLinkHeader(input,fSz,m_link,linkValues,mess)) {
2254 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: can not read the link\n"));
2255 f << "###link,";
2256 return true;
2257 }
2258 if ((m_link.m_fileType[1]&0xFFD7)!= 0x10) {
2259 MWAW_DEBUG_MSG(("RagTime5GraphInternal::RootCParser::parseZone: fileType1 seems odd[fSz=28...]\n"));
2260 f << "###fileType1,";
2261 }
2262 setExpectedType(m_dataId, F_parentList);
2263 m_fieldName=m_link.m_name="parentList";
2264 f << m_link << "," << mess;
2265 for (int i=0; i<2; ++i) { // g0: small number between 38 and 64, g1: 0|-1
2266 val=static_cast<int>(input->readLong(2));
2267 if (val) f << "g" << i << "=" << val << ",";
2268 }
2269 break;
2270 default:
2271 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseZone: find unknown size[%ld]\n", fSz));
2272 f << "###fSz=" << fSz << ",";
2273 break;
2274 }
2275 return true;
2276 }
2277 //! parse a field
2278 bool parseField(RagTime5StructManager::Field const &field, int /*m*/, libmwaw::DebugStream &f) final
2279 {
2280 if (!m_fieldName.empty())
2281 f << m_fieldName << ",";
2282 auto const &it=m_expectedIdToType.find(m_dataId);
2283 int expected=(it!=m_expectedIdToType.end())?it->second : -1;
2284 switch (expected) {
2285 case F_name:
2286 if (field.m_type==RagTime5StructManager::Field::T_Unicode && field.m_fileType==0xc8042) {
2287 m_cluster->m_buttonName=field.m_string.cstr();
2288 f << field.m_string.cstr();
2289 break;
2290 }
2291 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseField: find unexpected script field\n"));
2292 f << "###" << field;
2293 break;
2294 case F_formulaRoot:
2295 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0xcf042) {
2296 f << "ids=[";
2297 for (auto val : field.m_longList) {
2298 if (val==0) {
2299 f << "_,";
2300 continue;
2301 }
2302 setExpectedType(int(val-1), F_formula);
2303 f << "F" << val-1 << ",";
2304 }
2305 break;
2306 }
2307 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseField: find unexpected list link field\n"));
2308 f << "###" << field;
2309 break;
2310 case F_itemNames:
2311 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0xce842) {
2312 f << "pos=[";
2313 for (auto val : field.m_longList)
2314 f << val << ",";
2315 f << "],";
2316 m_link.m_longList=field.m_longList;
2317 break;
2318 }
2319 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseField: find unexpected list link field\n"));
2320 f << "###" << field;
2321 break;
2322 case F_buttonList:
2323 case F_buttonList+1:
2324 case F_buttonList+2:
2325 case F_parentList:
2326 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0xce842) {
2327 f << "pos=[";
2328 for (auto val : field.m_longList)
2329 f << val << ",";
2330 f << "],";
2331 m_link.m_longList=field.m_longList;
2332 break;
2333 }
2334 if (field.m_type==RagTime5StructManager::Field::T_Unstructured && field.m_fileType==0xce017) {
2335 // a small value 2 (can be first data)
2336 f << "unkn="<<field.m_extra << ",";
2337 break;
2338 }
2339 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseField: find unexpected list link field\n"));
2340 f << "###" << field;
2341 break;
2342 case F_formula:
2343 case F_nextId:
2344 default:
2345 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseField: find unexpected field\n"));
2346 f << "###" << field;
2347 break;
2348 }
2349 return true;
2350 }
2351 //! parse the header zone
2352 bool parseHeaderZone(MWAWInputStreamPtr &input, long fSz, int N, int flag, libmwaw::DebugStream &f)
2353 {
2354 f << "header,fl=" << std::hex << flag << std::dec << ",";
2355 if (N!=-5 || m_dataId!=0 || fSz!=74) {
2356 f << "###N=" << N << ",fSz=" << fSz << ",";
2357 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseHeaderZone: find unexpected main field\n"));
2358 return true;
2359 }
2360 int val;
2361 m_fieldName="main";
2362 for (int i=0; i<2; ++i) { // always 0?
2363 val=static_cast<int>(input->readLong(2));
2364 if (val) f << "f" << i+1 << "=" << val << ",";
2365 }
2366 val=static_cast<int>(input->readLong(2));
2367 f << "id=" << val << ",";
2368 val=static_cast<int>(input->readULong(2));
2369 if (val!=m_type) {
2370 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseHeaderZone: unexpected zone type[graph]\n"));
2371 f << "##zoneType=" << std::hex << val << std::dec << ",";
2372 }
2373 f << "button,";
2374 val=static_cast<int>(input->readLong(4));
2375 if (val) {
2376 setExpectedType(int(val-1), F_nextId); // either next[id] or parentList
2377 f << "next[id]=F" << val-1 << ",";
2378 }
2379 val=static_cast<int>(input->readLong(4));
2380 if (val) {
2381 setExpectedType(int(val-1), F_formulaRoot); // or button list v6
2382 f << "formula[root]=F" << val-1 << ",";
2383 }
2384 for (int i=0; i<7; ++i) { // g4=1-5, g7=1-5
2385 val=static_cast<int>(input->readULong(i==0 ? 4 : 2));
2386 if (!val) continue;
2387 if (i==0) {
2388 if (val&0x2)
2389 f << "return[title],"; // else index
2390 if (val&0x20)
2391 f << "recalculate[demand],";
2392 val &= 0xffffffdd;
2393 if (val)
2394 f << "fl1=" << std::hex << val << std::dec << ",";
2395 }
2396 else if (i==2) {
2397 m_cluster->m_item=val;
2398 f << "item=" << val << ","; // 0: no selected, 1: first, 2:second,
2399 }
2400 else if (i==3) {
2401 m_cluster->m_buttonType=val;
2402 f << "type=" << val << ","; // 1:push, 2:radio, 3:checkbox, 4:popup, 5:push(invisible)
2403 }
2404 else if (i==4)
2405 f << "appearence=" << val << ","; // 1: mac, 2: windows, 3: current platform
2406 else
2407 f << "g" << i << "=" << val << ",";
2408 }
2409 auto type=input->readULong(4); // find 0|93037 maybe a type
2410 if (type) f << "fileType=" << RagTime5ClusterManager::printType(type) << ",";
2411 val=static_cast<int>(input->readLong(4));
2412 if (val) {
2413 setExpectedType(int(val-1), F_name);
2414 f << "name=F" << val-1 << ",";
2415 }
2416 long actPos=input->tell();
2417 std::vector<int> listIds;
2418 if (!RagTime5StructManager::readDataIdList(input, 1, listIds)) {
2419 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::parseHeaderZone: can not find the unicode string data\n"));
2420 f << "##noData,";
2421 input->seek(actPos+2, librevenge::RVNG_SEEK_SET);
2422 }
2423 else if (listIds[0]) {
2424 // find a script comment
2425 RagTime5ClusterManager::Link scriptLink;
2426 scriptLink.m_type=RagTime5ClusterManager::Link::L_List;
2427 scriptLink.m_name="buttonComment";
2428 scriptLink.m_ids.push_back(listIds[0]);
2429 m_cluster->m_scriptComment=scriptLink;
2430 f << scriptLink << ",";
2431 }
2432 for (int i=0; i<9; ++i) { // h0=1-8d, h2=1f40, h4=0-26, h8=0|30
2433 val=static_cast<int>(input->readLong(i==1 ? 4 : 2));
2434 if (i==2) {
2435 switch (val&3) {
2436 case 1:
2437 f << "arrange[height],";
2438 break;
2439 case 2:
2440 f << "arrange[oneCol],";
2441 break;
2442 case 3:
2443 f << "arrange[oneRow],";
2444 break;
2445 case 0:
2446 default:
2447 break;
2448 }
2449 val &= 0xfffc;
2450 if (val==0x1f40) continue;
2451 f << "#fileType1=" << std::hex << val << std::dec << ",";
2452 }
2453 else {
2454 if (!val) continue;
2455 if (i==1) {
2456 setExpectedType(int(val-1), F_itemNames);
2457 f << "itemName=F" << val-1 << ",";
2458 }
2459 else if (i==6) {
2460 f << "avalaible[form]=FD" << val << ",";
2461 }
2462 else
2463 f << "h" << i << "=" << val << ",";
2464 }
2465 }
2466 std::string code(""); // find cent, left
2467 auto cod=input->readULong(4);
2468 for (int i=0; i<4; ++i) code+=char(cod>>(24-8*i));
2469 if (!code.empty()) f << "align=" << code << ",";
2470 return true;
2471 }
2472 //! end of a start zone call
2473 void endZone() final
2474 {
2475 if (m_link.empty())
2476 return;
2477 auto const &it=m_expectedIdToType.find(m_dataId);
2478 int expected=(it!=m_expectedIdToType.end())?it->second : -1;
2479 if (expected==F_itemNames) {
2480 if (m_cluster->m_itemNamesLink.empty())
2481 m_cluster->m_itemNamesLink=m_link;
2482 else {
2483 MWAW_DEBUG_MSG(("RagTime5GraphInternal::ButtonCParser::endZone: oops the item name link is already set\n"));
2484 m_cluster->m_linksList.push_back(m_link);
2485 }
2486 }
2487 else if (expected==F_parentList)
2488 m_cluster->m_parentLink=m_link;
2489 else
2490 m_cluster->m_linksList.push_back(m_link);
2491 }
2492
2493 protected:
2494 //! the current cluster
2495 std::shared_ptr<RagTime5GraphInternal::ClusterButton> m_cluster;
2496 //! the actual field name
2497 std::string m_fieldName;
2498
2499 //! the expected id
2500 std::map<int,int> m_expectedIdToType;
2501 //! the id stack
2502 std::stack<int> m_idStack;
2503 };
2504
2505 ButtonCParser::~ButtonCParser()
2506 {
2507 }
2508
2509 //
2510 //! low level: parser of picture cluster
2511 //
2512 struct PictCParser final : public RagTime5ClusterManager::ClusterParser {
2513 enum { F_nextId, F_pictList, F_pictRoot=F_pictList+3 };
2514 //! constructor
2515 PictCParser(RagTime5ClusterManager &parser, int type)
2516 : ClusterParser(parser, type, "ClustPict")
2517 , m_cluster(new ClusterPicture)
2518 , m_what(-1)
2519 , m_linkId(-1)
2520 , m_fieldName("")
2521
2522 , m_expectedIdToType()
2523 , m_idStack()
2524 {
2525 }
2526 //! destructor
2527 ~PictCParser() final;
2528 //! return the current cluster
2529 std::shared_ptr<RagTime5ClusterManager::Cluster> getCluster() final
2530 {
2531 return m_cluster;
2532 }
2533 //! return the current cluster
2534 std::shared_ptr<ClusterPicture> getPictureCluster()
2535 {
2536 return m_cluster;
2537 }
2538 //! set a data id type
2539 void setExpectedType(int id, int type)
2540 {
2541 m_expectedIdToType[id]=type;
2542 m_idStack.push(id);
2543 }
2544 /** returns to new zone to parse. -1: means no preference, 0: means first zone, ... */
2545 int getNewZoneToParse() final
2546 {
2547 if (m_idStack.empty())
2548 return -1;
2549 int id=m_idStack.top();
2550 m_idStack.pop();
2551 return id;
2552 }
2553 //! end of a start zone call
2554 void endZone() final
2555 {
2556 if (m_link.empty())
2557 return;
2558 switch (m_linkId) {
2559 case 0:
2560 m_cluster->m_auxilliarLink=m_link;
2561 break;
2562 case 1:
2563 m_cluster->m_parentLink=m_link;
2564 break;
2565 default:
2566 if (m_what==0) {
2567 if (m_cluster->m_dataLink.empty())
2568 m_cluster->m_dataLink=m_link;
2569 else {
2570 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::endZone: oops the main link is already set\n"));
2571 m_cluster->m_linksList.push_back(m_link);
2572 }
2573 }
2574 else
2575 m_cluster->m_linksList.push_back(m_link);
2576 break;
2577 }
2578 }
2579 //! parse a zone
2580 bool parseZone(MWAWInputStreamPtr &input, long fSz, int N, int flag, libmwaw::DebugStream &f) final
2581 {
2582 m_what=m_linkId=-1;
2583 m_fieldName="";
2584 if (N==-5)
2585 return parseHeaderZone(input,fSz,N,flag,f);
2586 if (N<0) {
2587 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseZone: expected N value\n"));
2588 f << "###N=" << N << ",";
2589 return true;
2590 }
2591 return parseDataZone(input, fSz, N, flag, f);
2592 }
2593 //! parse a field
2594 bool parseField(RagTime5StructManager::Field const &field, int /*m*/, libmwaw::DebugStream &f) final
2595 {
2596 if (!m_fieldName.empty())
2597 f << m_fieldName << ",";
2598 switch (m_what) {
2599 case 0:
2600 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0x170c8e5) {
2601 f << "pos=[";
2602 for (auto val : field.m_longList)
2603 f << val << ",";
2604 f << "],";
2605 m_link.m_longList=field.m_longList;
2606 break;
2607 }
2608 else if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0x3c057) {
2609 // rare, find one time with 4
2610 for (auto const &id : field.m_longList)
2611 f << "unkn0=" << id << ",";
2612 break;
2613 }
2614 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseField: find unexpected header field\n"));
2615 f << "###" << field << ",";
2616 break;
2617 case 1: // list link
2618 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0xce842) {
2619 f << "pos=[";
2620 for (auto val : field.m_longList)
2621 f << val << ",";
2622 f << "],";
2623 m_link.m_longList=field.m_longList;
2624 break;
2625 }
2626 if (field.m_type==RagTime5StructManager::Field::T_Unstructured && field.m_fileType==0xce017) {
2627 // a small value 2|4|a|1c|40
2628 f << "unkn="<<field.m_extra << ",";
2629 break;
2630 }
2631 // only with long2 list and with unk=[4]
2632 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0xcf042) {
2633 f << "unkn=[";
2634 for (auto val : field.m_longList) {
2635 if (val==0)
2636 f << "_,";
2637 else
2638 f << val << ",";
2639 }
2640 break;
2641 }
2642 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseField: find unexpected list link field\n"));
2643 f << "###" << field << ",";
2644 break;
2645 default:
2646 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseField: find unexpected field\n"));
2647 f << "###" << field << ",";
2648 break;
2649 }
2650 return true;
2651 }
2652 protected:
2653 //! parse a data block, find fSz=36, 36|36|28|28|32
2654 bool parseDataZone(MWAWInputStreamPtr &input, long fSz, int N, int flag, libmwaw::DebugStream &f)
2655 {
2656 auto const &it=m_expectedIdToType.find(m_dataId);
2657 int expected=(it!=m_expectedIdToType.end())?it->second : -1;
2658 if (expected!=-1)
2659 f << "[F" << m_dataId << "]";
2660 f << "fl=" << std::hex << flag << std::dec << ",";
2661 m_link.m_N=N;
2662 std::string mess;
2663 switch (expected) {
2664 case F_pictList:
2665 case F_pictList+1:
2666 case F_pictList+2: {
2667 long linkValues[4];
2668 if (fSz<28 || !readLinkHeader(input, fSz, m_link, linkValues, mess)) {
2669 f << "###fType=" << RagTime5ClusterManager::printType(m_link.m_fileType[0]) << ",";
2670 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseDataZone: the expected field[%d] seems bad\n", expected));
2671 return true;
2672 }
2673 f << m_link << "," << mess;
2674 long const expectedFileType1=0;
2675 long expectedFieldSize=0;
2676 m_what=1;
2677 if (expected==F_pictList && m_link.m_fileType[0]==0x3e800)
2678 m_link.m_name="pictList0";
2679 else if (expected==F_pictList+1 && m_link.m_fileType[0]==0x35800)
2680 m_link.m_name="pictList1";
2681 else if (expected==F_pictList+2 && ((unsigned long)(m_link.m_fileType[0])&0x7fffffff)==0x45080) { // v6.6 0x45080 other 0x80045080
2682 m_link.m_name="pictListInt";
2683 m_linkId=0;
2684 if (m_link.m_fileType[0]==0x45080) expectedFieldSize=2;
2685 }
2686 else {
2687 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseDataZone: the expected field[%d] seems bad\n", expected));
2688 f << "###";
2689 }
2690 if (!m_link.m_name.empty()) {
2691 f << m_link.m_name << ",";
2692 m_fieldName=m_link.m_name;
2693 }
2694 if (long(m_link.m_fileType[1]&0xFFD7)!=expectedFileType1) {
2695 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseDataZone: the expected field[%d] fileType1 seems odd\n", expected));
2696 f << "###fileType1=" << std::hex << m_link.m_fileType[1] << std::dec << ",";
2697 }
2698 if (expectedFieldSize>0 && m_link.m_fieldSize!=expectedFieldSize) {
2699 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseDataZone: fieldSize seems odd[%d]\n", expected));
2700 f << "###fieldSize,";
2701 }
2702 return true;
2703 }
2704 case F_pictRoot: {
2705 if (fSz<36) {
2706 f << "###fSz,";
2707 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseDataZone: the expected field[%d] seems bad\n", expected));
2708 return true;
2709 }
2710 m_fieldName="pictList[root]";
2711 f << m_fieldName << ",";
2712 auto val=static_cast<int>(input->readLong(4));
2713 if (val) f << "#f0=" << val << ",";
2714 val=static_cast<int>(input->readLong(4));
2715 if (val!=0x17d4842 && val!=0x17db042) { // 0x17db042: v66
2716 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseZone: find unexpected type0\n"));
2717 f << "#fileType0=" << std::hex << val << std::dec << ",";
2718 }
2719 for (int i=0; i<2; ++i) {
2720 val=static_cast<int>(input->readLong(4));
2721 if (val) f << "f" << i+1 << "=" << val << ",";
2722 }
2723 val=static_cast<int>(input->readULong(2));
2724 if ((val&0xFFD7)!=0x10) {
2725 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseZone: find unexpected type1[fSz36]\n"));
2726 f << "#fileType1=" << std::hex << val << std::dec << ",";
2727 }
2728 f << "ids=[";
2729 for (int i=0; i<3; ++i) {
2730 val=static_cast<int>(input->readLong(4));
2731 if (!val) {
2732 f << "_,";
2733 continue;
2734 }
2735 setExpectedType(val-1,F_pictList+i);
2736 f << "F" << val-1 << ",";
2737 }
2738 f << "],";
2739 return true;
2740 }
2741 case F_nextId:
2742 break;
2743 default:
2744 break;
2745 }
2746 if (expected==-1) {
2747 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseDataZone: find unexpected field\n"));
2748 f << "###field,";
2749 }
2750 switch (fSz) {
2751 case 36: {
2752 long linkValues[4];
2753 if (!readLinkHeader(input, fSz, m_link, linkValues, mess)) {
2754 f << "###fType=" << RagTime5Graph::printType(m_link.m_fileType[0]) << ",";
2755 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseZone: the field fSz28... type seems bad\n"));
2756 return true;
2757 }
2758 m_what=1;
2759 long expectedFileType1=0;
2760 if (m_link.m_fileType[0]==0) {
2761 expectedFileType1=0x10;
2762 // field of sz=28, dataId + ?
2763 m_linkId=1;
2764 m_link.m_name="pictParentList";
2765 m_fieldName="parentList";
2766 }
2767 else {
2768 f << "###fType=" << RagTime5Graph::printType(m_link.m_fileType[0]) << ",";
2769 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseZone: the field fSz28... type seems bad\n"));
2770 return true;
2771 }
2772 if (expectedFileType1>=0 && long(m_link.m_fileType[1]&0xFFD7)!=expectedFileType1) {
2773 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseZone: fileType1 seems odd[fSz=28...]\n"));
2774 f << "###fileType1=" << std::hex << m_link.m_fileType[1] << std::dec << ",";
2775 }
2776 f << m_link << "," << mess;
2777 f << "unkn=";
2778 for (int i=0; i<2; ++i) { // unsure some seq
2779 int val=int(input->readLong(2));
2780 f << val << (i==0 ? "-" : ",");
2781 }
2782 break;
2783 }
2784 default:
2785 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseZone: find unexpected fieldSize\n"));
2786 f << "##fSz=" << fSz << ",";
2787 break;
2788 }
2789 if (!m_fieldName.empty())
2790 f << m_fieldName << ",";
2791 return true;
2792 }
2793 //! parse the header zone
2794 bool parseHeaderZone(MWAWInputStreamPtr &input, long fSz, int N, int flag, libmwaw::DebugStream &f)
2795 {
2796 f << "header,fl=" << std::hex << flag << std::dec << ",";
2797 m_fieldName="header";
2798 m_what=0;
2799 if (N!=-5 || m_dataId!=0 || (fSz!=64 && fSz!=104 && fSz!=109)) {
2800 f << "###N=" << N << ",fSz=" << fSz << ",";
2801 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseHeaderZone: find unexpected main field\n"));
2802 return true;
2803 }
2804 int val;
2805 for (int i=0; i<2; ++i) { // always 0?
2806 val=static_cast<int>(input->readLong(2));
2807 if (val) f << "f" << i+1 << "=" << val << ",";
2808 }
2809 val=static_cast<int>(input->readLong(2));
2810 f << "id=" << val << ",";
2811 val=static_cast<int>(input->readULong(2));
2812 if (m_type>0 && val!=m_type) {
2813 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseHeaderZone: unexpected zone type\n"));
2814 f << "##zoneType=" << std::hex << val << std::dec << ",";
2815 }
2816 for (int i=0; i<2; ++i) {// f0=0|2|3, f1=0|3
2817 val=static_cast<int>(input->readLong(4));
2818 if (!val) continue;
2819 if (i==0) {
2820 setExpectedType(val-1,F_nextId);
2821 f << "next[id]=F" << val-1 << ",";
2822 }
2823 else {
2824 setExpectedType(val-1,F_pictRoot);
2825 f << "pict[root]=F" << val-1 << ",";
2826 }
2827 }
2828 if (fSz==64) { // movie
2829 for (int i=0; i<2; ++i) { // 0
2830 val=static_cast<int>(input->readLong(2));
2831 if (val) f << "f" << i+2 << "=" << val << ",";
2832 }
2833 f << "movie,";
2834 float dim[2];
2835 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
2836 f << "dim=" << MWAWVec2f(dim[0],dim[1]) << ",";
2837 for (int i=0; i<15; ++i) { // always 0
2838 val=static_cast<int>(input->readLong(2));
2839 if (val) f << "g" << i << "=" << val << ",";
2840 }
2841 return true;
2842 }
2843 for (int i=0; i<5; ++i) {
2844 val=static_cast<int>(input->readLong(2));
2845 static int const expected[]= {2, 0, 0x2000, 0, 0x2710};
2846 if (val!=expected[i]) f << "f" << i+2 << "=" << val << ",";
2847 }
2848 auto type=input->readULong(4);
2849 if (type!=0x3f7ff5) {
2850 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseHeaderZone: unexpected type [104|109]\n"));
2851 f << "#fieldType=" << RagTime5Graph::printType(type) << ",";
2852 }
2853 for (int i=0; i<2; ++i) { // always 1,1 ?
2854 val=static_cast<int>(input->readLong(1));
2855 if (val!=1) f << "fl" << i << "=" << val << ",";
2856 }
2857 float dim[4];
2858 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
2859 m_cluster->m_dimension=MWAWVec2f(dim[0],dim[1]);
2860 f << "dim=" << m_cluster->m_dimension << ",sz=" << MWAWVec2f(dim[2],dim[3]) << ",";
2861 for (int i=0; i<5; ++i) { // fl2=708|718|f18|...|7d4b, fl3=0|4, f5=800|900|8000,fl6=0|1|a
2862 val=static_cast<int>(input->readULong(2));
2863 if (val) f << "fl" << i+2 << "=" << std::hex << val << std::dec << ",";
2864 }
2865 for (int i=0; i<4; ++i) { // some selection ?
2866 val= static_cast<int>(input->readLong(4));
2867 if ((i<2&&val)||(i>=2&&val!=0x7FFFFFFF))
2868 f << "g" << i << "=" << val << ",";
2869 }
2870 for (int i=0; i<6; ++i) { // h2=0|1|3|8
2871 val= static_cast<int>(input->readLong(2));
2872 if (val) f << "h" << i << "=" << val << ",";
2873 }
2874 // find 5b84|171704|171804|172d84, so unsure
2875 m_link.m_fileType[0]=input->readULong(4);
2876 if (m_link.m_fileType[0])
2877 f << "fieldType1=" << RagTime5Graph::printType(m_link.m_fileType[0]) << ",";
2878 std::vector<int> listIds;
2879 long actPos=input->tell();
2880 if (!RagTime5StructManager::readDataIdList(input, 1, listIds)) {
2881 MWAW_DEBUG_MSG(("RagTime5GraphInternal::PictCParser::parseHeaderZone: can not find the data[104|109]\n"));
2882 f << "##noData,";
2883 m_link.m_ids.clear();
2884 input->seek(actPos+2, librevenge::RVNG_SEEK_SET);
2885 }
2886 else if (listIds[0]) {
2887 m_cluster->m_containerId=listIds[0];
2888 f << "container=data" << listIds[0] << "A,";
2889 }
2890 for (int i=0; i<2; ++i) { // always 0
2891 val= static_cast<int>(input->readLong(2));
2892 if (val) f << "h" << i+6 << "=" << val << ",";
2893 }
2894 if (fSz==109) {
2895 int dim2[2];
2896 for (auto &d : dim2) d=static_cast<int>(input->readLong(2));
2897 f << "dim2=" << MWAWVec2i(dim2[0], dim2[1]) << ",";
2898 val= static_cast<int>(input->readLong(1)); // 0 or 1
2899 if (val) f << "h8=" << val << ",";
2900 }
2901 return true;
2902 }
2903
2904 //! the current cluster
2905 std::shared_ptr<ClusterPicture> m_cluster;
2906 //! a index to know which field is parsed : 0: main, 1: list
2907 int m_what;
2908 //! the link id: 0: fieldSz=8 ?, data2: dataId+?
2909 int m_linkId;
2910 //! the actual field name
2911 std::string m_fieldName;
2912
2913 //! the expected id
2914 std::map<int,int> m_expectedIdToType;
2915 //! the id stack
2916 std::stack<int> m_idStack;
2917
2918 private:
2919 //! copy constructor (not implemented)
2920 PictCParser(PictCParser const &orig) = delete;
2921 //! copy operator (not implemented)
2922 PictCParser &operator=(PictCParser const &orig) = delete;
2923 };
2924
2925 PictCParser::~PictCParser()
2926 {
2927 }
2928
2929 //
2930 //! low level: parser of graph cluster
2931 //
2932 struct GraphicCParser final : public RagTime5ClusterManager::ClusterParser {
2933 enum { F_clustLink2, F_dim, F_graphLink=F_dim+3, F_graphList, F_name=F_graphList+3, F_name2=F_name+3, F_nextId=F_name2+3, F_unknA, F_unknClustLinkA };
2934 //! constructor
2935 GraphicCParser(RagTime5ClusterManager &parser, int type)
2936 : ClusterParser(parser, type, "ClustGraph")
2937 , m_cluster(new ClusterGraphic)
2938 , m_what(-1)
2939 , m_linkId(-1)
2940 , m_fieldName("")
2941 , m_conditionFormulaLinks()
2942
2943 , m_expectedIdToType()
2944 , m_idStack()
2945 {
2946 }
2947 //! destructor
2948 ~GraphicCParser() final;
2949 //! return the current cluster
2950 std::shared_ptr<RagTime5ClusterManager::Cluster> getCluster() final
2951 {
2952 return m_cluster;
2953 }
2954 //! return the current graphic cluster
2955 std::shared_ptr<ClusterGraphic> getGraphicCluster()
2956 {
2957 return m_cluster;
2958 }
2959 //! return the condition formula link (unsure)
2960 std::vector<RagTime5ClusterManager::Link> const &getConditionFormulaLinks() const
2961 {
2962 return m_conditionFormulaLinks;
2963 }
2964 //! set a data id type
2965 void setExpectedType(int id, int type)
2966 {
2967 m_expectedIdToType[id]=type;
2968 m_idStack.push(id);
2969 }
2970 /** returns to new zone to parse. -1: means no preference, 0: means first zone, ... */
2971 int getNewZoneToParse() final
2972 {
2973 if (m_idStack.empty())
2974 return -1;
2975 int id=m_idStack.top();
2976 m_idStack.pop();
2977 return id;
2978 }
2979 //! end of a start zone call
2980 void endZone() final
2981 {
2982 if (m_link.empty())
2983 return;
2984 switch (m_linkId) {
2985 case 0:
2986 if (m_cluster->m_nameLink.empty())
2987 m_cluster->m_nameLink=RagTime5ClusterManager::NameLink(m_link);
2988 else {
2989 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::endZone: oops the name link is already set\n"));
2990 m_cluster->m_linksList.push_back(m_link);
2991 }
2992 break;
2993 case 1:
2994 m_conditionFormulaLinks.push_back(m_link);
2995 break;
2996 case 2:
2997 case 4:
2998 m_cluster->m_clusterLinks[m_linkId==2 ? 0 : 1]=m_link;
2999 break;
3000 case 3:
3001 m_cluster->m_parentLink=m_link;
3002 break;
3003 case 5:
3004 m_cluster->m_transformationLinks.push_back(m_link);
3005 break;
3006 case 6:
3007 m_cluster->m_dimensionLinks.push_back(m_link);
3008 break;
3009 default:
3010 if (m_what==0) {
3011 if (m_cluster->m_dataLink.empty())
3012 m_cluster->m_dataLink=m_link;
3013 else {
3014 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::endZone: oops the main link is already set\n"));
3015 m_cluster->m_linksList.push_back(m_link);
3016 }
3017 }
3018 else
3019 m_cluster->m_linksList.push_back(m_link);
3020 break;
3021 }
3022 }
3023 //! parse a zone
3024 bool parseZone(MWAWInputStreamPtr &input, long fSz, int N, int flag, libmwaw::DebugStream &f) final
3025 {
3026 m_what=m_linkId=-1;
3027 m_fieldName="";
3028 if (N==-5)
3029 return parseHeaderZone(input,fSz,N,flag,f);
3030 if (N<0) {
3031 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseZone: expected N value\n"));
3032 f << "###N=" << N << ",";
3033 return true;
3034 }
3035 return parseDataZone(input, fSz, N, flag, f);
3036 }
3037 //! parse a field
3038 bool parseField(RagTime5StructManager::Field const &field, int /*m*/, libmwaw::DebugStream &f) final
3039 {
3040 if (!m_fieldName.empty())
3041 f << m_fieldName << ",";
3042 /* TODO: use the field type
3043 auto const &it=m_expectedIdToType.find(m_dataId);
3044 int expected=(it!=m_expectedIdToType.end())?it->second : -1;
3045 */
3046 switch (m_what) {
3047 case 0: // graph data
3048 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0x3c057) {
3049 // a small value 3|4
3050 for (auto const &id : field.m_longList)
3051 f << "unkn0=" << id << ",";
3052 break;
3053 }
3054 if (field.m_type==RagTime5StructManager::Field::T_FieldList && field.m_fileType==0x14e6825) {
3055 f << "decal=[";
3056 for (auto const &child : field.m_fieldList) {
3057 if (child.m_type==RagTime5StructManager::Field::T_LongList && child.m_fileType==0xce842) {
3058 for (auto val : child.m_longList)
3059 f << val << ",";
3060 m_link.m_longList=child.m_longList;
3061 continue;
3062 }
3063 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseField: find unexpected decal child[graph]\n"));
3064 f << "##[" << child << "],";
3065 }
3066 f << "],";
3067 break;
3068 }
3069 if (field.m_type==RagTime5StructManager::Field::T_FieldList && field.m_fileType==0x14e6875) {
3070 f << "listFlag?=[";
3071 for (auto const &child : field.m_fieldList) {
3072 if (child.m_type==RagTime5StructManager::Field::T_Unstructured && child.m_fileType==0xce017)
3073 f << child.m_extra << ","; // find data with different length there
3074 else {
3075 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseField: find unexpected unstructured child[graphZones]\n"));
3076 f << "##" << child << ",";
3077 }
3078 }
3079 f << "],";
3080 break;
3081 }
3082 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseField: find unexpected child[graphZones]\n"));
3083 f << "##" << field << ",";
3084 break;
3085 case 1: // list link
3086 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0xce842) {
3087 f << "pos=[";
3088 for (auto val : field.m_longList)
3089 f << val << ",";
3090 f << "],";
3091 m_link.m_longList=field.m_longList;
3092 break;
3093 }
3094 if (field.m_type==RagTime5StructManager::Field::T_Unstructured && field.m_fileType==0xce017) {
3095 // a small value 2|4|a|1c|40
3096 f << "unkn="<<field.m_extra << ",";
3097 break;
3098 }
3099 // only with long2 list and with unk=[4]
3100 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0xcf042) {
3101 f << "unkn=[";
3102 for (auto val : field.m_longList) {
3103 if (val==0)
3104 f << "_,";
3105 else {
3106 setExpectedType(int(val-1), F_unknA);
3107 f << "rootA=F" << val-1 << ",";
3108 }
3109 }
3110 break;
3111 }
3112 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseField: find unexpected list link field\n"));
3113 f << "###" << field;
3114 break;
3115 case 2: // cluster link, graph transform
3116 if (field.m_type==RagTime5StructManager::Field::T_Long && field.m_fileType==0xcf817) {
3117 // only in graph transform, small value between 3b|51|52|78
3118 f << "f0="<<field.m_longValue[0] << ",";
3119 break;
3120 }
3121 if (field.m_type==RagTime5StructManager::Field::T_Unstructured && field.m_fileType==0xce017) {
3122 // a small value 2|4|a|1c|40
3123 f << "unkn="<<field.m_extra << ",";
3124 break;
3125 }
3126 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseField: find unexpected cluster link field\n"));
3127 f << "###" << field;
3128 break;
3129 case 3: // fSz=91
3130 if (field.m_type==RagTime5StructManager::Field::T_FieldList && field.m_fileType==0x14f1825) {
3131 f << "list=["; // find only list with one element: 1,4,8,11 number of data?
3132 for (auto const &child : field.m_fieldList) {
3133 if (child.m_type==RagTime5StructManager::Field::T_LongList && child.m_fileType==0xce842) {
3134 for (auto val : child.m_longList)
3135 f << val << ",";
3136 m_link.m_longList=child.m_longList;
3137 continue;
3138 }
3139 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseField: find unexpected child[fSz=91]\n"));
3140 f << "##[" << child << "],";
3141 }
3142 f << "],";
3143 break;
3144 }
3145 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseField: find unexpected cluster field[fSz=91]\n"));
3146 f << "###" << field;
3147 break;
3148 default:
3149 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseField: find unexpected field\n"));
3150 f << "###" << field;
3151 break;
3152 }
3153 return true;
3154 }
3155 protected:
3156 //! parse a data block
3157 bool parseDataZone(MWAWInputStreamPtr &input, long fSz, int N, int flag, libmwaw::DebugStream &f)
3158 {
3159 auto const &it=m_expectedIdToType.find(m_dataId);
3160 int expected=(it!=m_expectedIdToType.end())?it->second : -1;
3161 if (expected!=-1)
3162 f << "[F" << m_dataId << "]";
3163 f << "fl=" << std::hex << flag << std::dec << ",";
3164 std::string mess("");
3165 m_link.m_N=N;
3166 int val;
3167 switch (expected) {
3168 case F_clustLink2:
3169 case F_dim:
3170 case F_dim+1:
3171 case F_dim+2:
3172 case F_name:
3173 case F_name+1:
3174 case F_name+2:
3175 case F_name2:
3176 case F_name2+1:
3177 case F_graphLink:
3178 case F_graphList:
3179 case F_graphList+1:
3180 case F_graphList+2:
3181 case F_unknClustLinkA: {
3182 long linkValues[4];
3183 if (fSz<28 || !readLinkHeader(input, fSz, m_link, linkValues, mess)) {
3184 f << "###fType=" << RagTime5ClusterManager::printType(m_link.m_fileType[0]) << ",";
3185 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: the expected field[%d] seems bad\n", expected));
3186 return true;
3187 }
3188 f << m_link << "," << mess;
3189 long expectedFileType1=0, expectedFieldSize=0;
3190 if (expected==F_clustLink2 && fSz==36) {
3191 if (m_link.m_fileType[0]) {
3192 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find file type in the field[%d]\n", expected));
3193 f << "###fileType0,";
3194 }
3195 m_what=1;
3196 m_linkId=3;
3197 m_link.m_name="parentLink";
3198 expectedFileType1=0x10;
3199 for (int i=0; i<2; ++i) { // g0=3a-4f, g1=0|-1
3200 val=static_cast<int>(input->readLong(2));
3201 if (val)
3202 f << "g" << i << "=" << val << ",";
3203 }
3204 }
3205 else if (expected==F_graphLink && fSz==30) {
3206 m_what=2;
3207 m_linkId=2;
3208 m_link.m_name="graphLinkLst";
3209 expectedFieldSize=12;
3210 expectedFileType1=0xd0;
3211 m_link.m_type=RagTime5ClusterManager::Link::L_ClusterLink;
3212 }
3213 else if ((expected==F_name||expected==F_name2||expected==F_dim) && m_link.m_fileType[0]==0x3e800) {
3214 m_what=1;
3215 m_link.m_name=(expected==F_name ? "unicodeList0" : expected==F_name2 ? "name2List0" : "dimList0");
3216 }
3217 else if ((expected==F_name+1||expected==F_name2+1||expected==F_dim+1) && m_link.m_fileType[0]==0x35800) {
3218 m_what=1;
3219 m_link.m_name=(expected==F_name+1 ? "unicodeList1" : expected==F_name2+1 ? "name2List1" : "dimList1");
3220 }
3221 else if (expected==F_dim+2 && m_link.m_fileType[0]==0x33000) { // 30
3222 expectedFieldSize=4;
3223 m_linkId=6;
3224 m_link.m_name="dims";
3225 }
3226 else if (expected==F_name+2 && m_link.m_fileType[0]==0) { // fSz==32
3227 expectedFileType1=0x200;
3228 m_what=1;
3229 m_linkId=0;
3230 m_link.m_type=RagTime5ClusterManager::Link::L_UnicodeList;
3231 m_link.m_name="unicodeNames";
3232 }
3233 else if (expected==F_unknClustLinkA && fSz==30) {
3234 if (m_link.m_fileType[0]) {
3235 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find file type in the field[%d]\n", expected));
3236 f << "###fileType0,";
3237 }
3238 expectedFieldSize=8;
3239 m_linkId=4;
3240 m_link.m_name="clustLink3";
3241 }
3242 else if (expected==F_graphList && m_link.m_fileType[0]==0x3e800) {
3243 m_what=1;
3244 m_link.m_name="graphList0";
3245 }
3246 else if (expected==F_graphList+1 && m_link.m_fileType[0]==0x35800) {
3247 m_what=1;
3248 m_link.m_name="graphList1";
3249 }
3250 else if (expected==F_graphList+2 && m_link.m_fileType[0]==0x45080) {
3251 m_link.m_name="graphListInt";
3252 //m_linkId=0;
3253 expectedFieldSize=2;
3254 }
3255 else {
3256 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: the expected field[%d] seems bad\n", expected));
3257 f << "###";
3258 }
3259 if (!m_link.m_name.empty()) {
3260 f << m_link.m_name << ",";
3261 m_fieldName=m_link.m_name;
3262 }
3263 if (expectedFileType1>0 && long(m_link.m_fileType[1]&0xFFD7)!=expectedFileType1) {
3264 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: the expected field[%d] fileType1 seems odd\n", expected));
3265 f << "###fileType1=" << std::hex << m_link.m_fileType[1] << std::dec << ",";
3266 }
3267 if (expectedFieldSize>0 && m_link.m_fieldSize!=expectedFieldSize) {
3268 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: fieldSize seems odd[%d]\n", expected));
3269 f << "###fieldSize,";
3270 }
3271 return true;
3272 }
3273 case F_name2+2:
3274 if (fSz<28) {
3275 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: unexpected data of size for name2\n"));
3276 f << "##fSz,";
3277 return true;
3278 }
3279 m_fieldName="name2Unkn";
3280 f << m_fieldName << ",";
3281 val=static_cast<int>(input->readLong(4));
3282 if (val!=0x46000) {
3283 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find odd file type in the field[%d]\n", expected));
3284 f << "###fileType0=" << RagTime5Graph::printType((unsigned long)val) << ",";
3285 }
3286 for (int i=0; i<9; ++i) {
3287 val=static_cast<int>(input->readLong(2));
3288 if (val)
3289 f << "f" << i << "=" << val << ",";
3290 }
3291 m_what=1;
3292 return true;
3293 case F_unknA: {
3294 if (fSz<91) {
3295 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: unexpected data of size for setting root\n"));
3296 f << "##fSz,";
3297 return true;
3298 }
3299 m_fieldName="unknA";
3300 f << m_fieldName << ",";
3301 m_what=3;
3302 if (N) // find always 0
3303 f << "#N=" << N << ",";
3304 for (int i=0; i<2; ++i) { // always 0
3305 val=static_cast<int>(input->readLong(2));
3306 if (val)
3307 f << "f" << i << "=" << val << ",";
3308 }
3309 val=static_cast<int>(input->readLong(4)); // always 1
3310 if (val!=1)
3311 f << "f2=" << val << ",";
3312 val=static_cast<int>(input->readLong(2)); // 0|4
3313 if (val) f << "f3=" << val << ",";
3314 val=static_cast<int>(input->readULong(2)); // ?
3315 if (val) f << "fl=" << std::hex << val << std::dec << ",";
3316 auto type=input->readULong(4);
3317 if (type!=0x14e7842) {
3318 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find unexpected file type\n"));
3319 f << "##filetype0=" << RagTime5Graph::printType(type) << ",";
3320 }
3321 for (int i=0; i<2; ++i) {
3322 val=static_cast<int>(input->readLong(2));
3323 if (val)
3324 f << "f" << i+4 << "=" << val << ",";
3325 }
3326 val=static_cast<int>(input->readLong(4));
3327 if (val) {
3328 setExpectedType(val-1, F_unknClustLinkA);
3329 f << "clustLinkA=F" << val-1 << ",";
3330 }
3331 for (int wh=0; wh<2; ++wh) { // checkme unsure about field separations
3332 f << "unkn" << wh << "=[";
3333 val=static_cast<int>(input->readLong(1)); // 0
3334 if (val) f << "g0=" << val << ",";
3335 for (int i=0; i<3; ++i) { // g0=8|16 g3=0|1
3336 static int const expectedVal[]= {16, 0, 0};
3337 val=static_cast<int>(input->readLong(2)); // 16
3338 if (val!=expectedVal[i])
3339 f << "g" << i+1 << "=" << val << ",";
3340 }
3341 val=static_cast<int>(input->readLong(1)); // 0
3342 if (val) f << "g4=" << val << ",";
3343 for (int i=0; i<7; ++i) { // g5=348,
3344 val=static_cast<int>(input->readLong(2));
3345 if (val) f << "g" << i+5 << "=" << val << ",";
3346 }
3347 val=static_cast<int>(input->readLong(1)); // 0
3348 if (val) f << "h0=" << val << ",";
3349 for (int i=0; i<2; ++i) { // h1=21|29
3350 val=static_cast<int>(input->readLong(2));
3351 if (val) f << "h" << i+1 << "=" << val << ",";
3352 }
3353 val=static_cast<int>(input->readLong(1)); // 0
3354 if (val) f << "h3=" << val << ",";
3355 f << "],";
3356 }
3357 for (int i=0; i<5; ++i) { // always 0
3358 val=static_cast<int>(input->readLong(1));
3359 if (val) f << "g" << i << "=" << val << ",";
3360 }
3361 return true;
3362 }
3363 default:
3364 break;
3365 }
3366 if (expected==-1) {
3367 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find unexpected field\n"));
3368 f << "###field,";
3369 }
3370 long endPos=input->tell()+fSz-6;
3371 switch (fSz) {
3372 case 28:
3373 case 29:
3374 case 30:
3375 case 32:
3376 case 34: {
3377 long linkValues[4];
3378 if (!readLinkHeader(input, fSz, m_link, linkValues, mess)) {
3379 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find unexpected link\n"));
3380 f << "###link";
3381 return true;
3382 }
3383 long expectedFileType1=0;
3384 if (m_link.m_fileType[0]==0x34800) {
3385 m_what=1;
3386 if (linkValues[0]!=0x14ff840) {
3387 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find unexpected linkValue[0]\n"));
3388 f << "#lValues0,";
3389 }
3390 m_fieldName="zone:longs1";
3391 if (linkValues[1]) {
3392 setExpectedType(int(linkValues[1]-1), F_nextId);
3393 f << "next[id]=F" << linkValues[1]-1 << ",";
3394 }
3395 }
3396 else if (m_link.m_fileType[0]==0x3c052) {
3397 m_what=1;
3398 if (linkValues[0]!=0x1454877) {
3399 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find unexpected linkValue[0]\n"));
3400 f << "#lValues0,";
3401 }
3402 m_fieldName="zone:longs2";
3403 expectedFileType1=0x50;
3404 }
3405 else if (m_link.m_fileType[0]==0x9f840) {
3406 if (m_link.m_fieldSize!=34 && m_link.m_fieldSize!=36) {
3407 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find unexpected fieldSize[fSz28...]\n"));
3408 f << "###fielSize,";
3409 }
3410 expectedFileType1=0x10;
3411 if (linkValues[0]!=0x1500040) {
3412 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find unexpected linkValues[fSz28...]\n"));
3413 f << "#linkValue0,";
3414 }
3415 m_linkId=5;
3416 m_what=2;
3417 m_fieldName="graphTransform";
3418 if (linkValues[1]) {
3419 setExpectedType(int(linkValues[1]-1), F_nextId);
3420 f << "next[id]=F" << linkValues[1]-1 << ",";
3421 }
3422 }
3423 else if (m_link.m_fileType[0]==0x14ff040) {
3424 if (linkValues[0]!=0x14ff040) {
3425 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find unexpected linkValues[fSz28...]\n"));
3426 f << "#linkValue0,";
3427 }
3428 m_what=1;
3429 m_linkId=1;
3430 m_link.m_name=m_fieldName="condFormula";
3431 expectedFileType1=0x10;
3432 }
3433 else {
3434 f << "###fType=" << RagTime5Graph::printType(m_link.m_fileType[0]) << ",";
3435 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: the field fSz28 type seems bad\n"));
3436 return true;
3437 }
3438 if (linkValues[2]) {
3439 setExpectedType(int(linkValues[2]-1), F_nextId);
3440 f << "next[id]=F" << linkValues[2]-1 << ",";
3441 }
3442 if (expectedFileType1>=0 && long(m_link.m_fileType[1]&0xFFD7)!=expectedFileType1) {
3443 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: fileType1 seems odd[fSz=28...]\n"));
3444 f << "###fileType1=" << std::hex << m_link.m_fileType[1] << std::dec << ",";
3445 }
3446 f << m_link << "," << mess;
3447 m_link.m_fileType[0]=0;
3448 auto remain=int(endPos-input->tell());
3449 if (remain==0) break;
3450 if (remain==4) {
3451 for (int i=0; i<2; ++i) { // g0=3a-4f, g1=0|-1
3452 val=static_cast<int>(input->readLong(2));
3453 if (val)
3454 f << "g" << i << "=" << val << ",";
3455 }
3456 break;
3457 }
3458 val=static_cast<int>(input->readLong(1));
3459 if (val!=1) // always 1
3460 f << "g0=" << val << ",";
3461 if (remain<6) break;
3462 val=static_cast<int>(input->readLong(1));
3463 if (val) // always 0
3464 f << "g1=" << val << ",";
3465 for (int i=0; i<2; ++i) { // g3=0|3c042
3466 val=static_cast<int>(input->readLong(2));
3467 if (val)
3468 f << "g" << i+2 << "=" << val << ",";
3469 }
3470 break;
3471 }
3472 case 36: {
3473 val=static_cast<int>(input->readLong(4));
3474 unsigned long type= input->readULong(4);
3475 if (type==0x7d01a || type==0x7d42a || (type&0xFFFFF8F)==0x14e818a||type==0x17db042) {
3476 m_what=2;
3477 m_fieldName=type==0x7d01a ? "name[root]" : type==0x7d42a ? "name2[root]" : type==0x17db042 ? "graphList" : "dim[root]";
3478 f << m_fieldName << ",";
3479 f << "type=" << RagTime5Graph::printType(type) << ",";
3480 if (val) f << "#f0=" << val << ",";
3481 for (int i=0; i<2; ++i) { // f1=0|7-11
3482 val=static_cast<int>(input->readLong(4));
3483 if (!val) continue;
3484 if (i==0) {
3485 setExpectedType(val-1, F_nextId);
3486 f << "next[id]=F" << val-1 << ",";
3487 }
3488 else
3489 f << "f" << i+1 << "=" << val << ",";
3490 }
3491 val=static_cast<int>(input->readULong(2));
3492 if ((val&0xFFD7)!=0x10) {
3493 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find unexpected type1[fSz36]\n"));
3494 f << "#fileType1=" << std::hex << val << std::dec << ",";
3495 }
3496 f << "ids=[";
3497 for (int i=0; i<3; ++i) { // g0=3-a, g1=g0+1, g2=g1+1
3498 val=static_cast<int>(input->readLong(4));
3499 if (!val) {
3500 f << "_,";
3501 continue;
3502 }
3503 if (type==0x7d01a)
3504 setExpectedType(val-1,F_name+i);
3505 else if (type==0x7d42a)
3506 setExpectedType(val-1,F_name2+i);
3507 else if (type==0x17db042)
3508 setExpectedType(val-1,F_graphList+i);
3509 else
3510 setExpectedType(val-1,F_dim+i);
3511 f << "F" << val-1 << ",";
3512 }
3513 f << "],";
3514 break;
3515 }
3516 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find unexpected link\n"));
3517 f << "###link=" << std::hex << type << std::dec;
3518 return true;
3519 }
3520 default:
3521 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseDataZone: find unexpected field size\n"));
3522 f << "##fSz=" << fSz << ",";
3523 break;
3524 }
3525 if (!m_fieldName.empty())
3526 f << m_fieldName << ",";
3527
3528 return true;
3529 }
3530 //! parse the header zone
3531 bool parseHeaderZone(MWAWInputStreamPtr &input, long fSz, int N, int flag, libmwaw::DebugStream &f)
3532 {
3533 // flag&0x100: selected? or unlock?
3534 f << "header,fl=" << std::hex << flag << std::dec << ",";
3535 m_fieldName="header";
3536 if (N!=-5 || m_dataId!=0 || fSz!=118) {
3537 f << "###N=" << N << ",fSz=" << fSz << ",";
3538 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseHeaderZone: find unexpected main field\n"));
3539 return true;
3540 }
3541 m_what=0;
3542
3543 int val;
3544 for (int i=0; i<2; ++i) { // always 0?
3545 val=static_cast<int>(input->readLong(2));
3546 if (val) f << "f" << i+1 << "=" << val << ",";
3547 }
3548 val=static_cast<int>(input->readLong(2));
3549 f << "id=" << val << ",";
3550 val=static_cast<int>(input->readULong(2));
3551 if (m_type>0 && val!=m_type) {
3552 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseHeaderZone: unexpected zone type\n"));
3553 f << "##zoneType=" << std::hex << val << std::dec << ",";
3554 }
3555 m_fieldName="graphZone";
3556 val=static_cast<int>(input->readLong(4));
3557 if (val) {
3558 setExpectedType(val-1, F_clustLink2);
3559 f << "clusterLink2[id]=F" << val-1 << ",";
3560 }
3561 val=static_cast<int>(input->readLong(4)); // 0|2|3|4
3562 if (val) {
3563 setExpectedType(val-1, F_nextId);
3564 f << "next[id]=F" << val-1 << ",";
3565 }
3566 m_link.m_fileType[0]=input->readULong(4); // find 0|80|81|880|8000|8080
3567
3568 if ((m_link.m_fileType[0]&0x777E)!=0) {
3569 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseHeaderZone: the file type0 seems bad[graph]\n"));
3570 f << "##fileType0=" << RagTime5Graph::printType(m_link.m_fileType[0]) << ",";
3571 }
3572 else if (m_link.m_fileType[0])
3573 f << "fileType0=" << RagTime5Graph::printType(m_link.m_fileType[0]) << ",";
3574 for (int wh=0; wh<2; ++wh) {
3575 f << "block" << wh << "[";
3576 val=static_cast<int>(input->readLong(2)); // 1 or 10
3577 if (val!=1) f << "g0=" << val << ",";
3578 m_cluster->m_N[wh]=static_cast<int>(input->readLong(4));
3579 if (m_cluster->m_N[wh]) f << "N=" << m_cluster->m_N[wh] << ","; // 0:graphShape(size)+1, 1:graphUsed(size)
3580 for (int i=0; i<4; ++i) { // g1=numData+1? and g2 small number, other 0 ?
3581 val=static_cast<int>(input->readLong(4));
3582 if (val) f << "g" << i+1 << "=" << val << ",";
3583 }
3584 if (wh==0) {
3585 m_link.m_fileType[1]=input->readULong(2);
3586 if (m_link.m_fileType[1]!=0x8000 && m_link.m_fileType[1]!=0x8020) {
3587 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseHeaderZone: the file type1 seems bad[graph]\n"));
3588 f << "##fileType1=" << std::hex << m_link.m_fileType[1] << std::dec << ",";
3589 }
3590 long actPos=input->tell();
3591 if (!RagTime5StructManager::readDataIdList(input, 2, m_link.m_ids) || m_link.m_ids[1]==0) {
3592 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseHeaderZone: can not find the graph data\n"));
3593 f << "##noData,";
3594 m_link.m_ids.clear();
3595 m_link.m_ids.resize(2,0);
3596 input->seek(actPos+8, librevenge::RVNG_SEEK_SET);
3597 }
3598 val=static_cast<int>(input->readLong(2)); // always 0
3599 if (val) f << "g6=" << val << ",";
3600 val=static_cast<int>(input->readLong(4)); // 0|2
3601 if (val) {
3602 setExpectedType(val-1, F_graphLink);
3603 f << "graphLink=F" << val-1 << ",";
3604 }
3605 float dim[2];
3606 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
3607 f << "dim=" << MWAWVec2f(dim[0], dim[1]) << ",";
3608 for (int i=0; i<4; ++i) { // always 0
3609 val=static_cast<int>(input->readLong(2));
3610 if (val) f << "h" << i << "=" << val << ",";
3611 }
3612 }
3613 else {
3614 RagTime5ClusterManager::Link unknLink;
3615 unknLink.m_fileType[1]=input->readULong(2);
3616 unknLink.m_fieldSize=static_cast<int>(input->readULong(2));
3617 if ((unknLink.m_fileType[1]!=0x50 && unknLink.m_fileType[1]!=0x58) || unknLink.m_fieldSize!=10) {
3618 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseHeaderZone: the file type2 seems bad[graph]\n"));
3619 f << "##fileType2=" << std::hex << unknLink.m_fileType[1] << std::dec << "[" << unknLink.m_fieldSize << "],";
3620 }
3621 // fixme store unknLink instead of updating the main link
3622 std::vector<int> listIds;
3623 if (RagTime5StructManager::readDataIdList(input, 3, listIds)) {
3624 if (listIds[0]) {
3625 m_cluster->m_usedZoneId=listIds[0];
3626 f << "graphUsed=data" << listIds[0] << "A,";
3627 }
3628 if (listIds[1]) {
3629 m_cluster->m_textboxZoneId=listIds[1];
3630 m_cluster->m_clusterIdsList.push_back(listIds[1]);
3631 f << "textboxId=data" << listIds[1] << "A,";
3632 }
3633 if (listIds[2]) {
3634 m_cluster->m_clusterIdsList.push_back(listIds[2]);
3635 f << "clusterId=" << getClusterDebugName(listIds[2]) << ",";
3636 }
3637 }
3638 else {
3639 MWAW_DEBUG_MSG(("RagTime5GraphInternal::GraphicCParser::parseHeaderZone: can not read unkn link list[graph]\n"));
3640 f << "##graph[unknown],";
3641 }
3642 }
3643 f << "],";
3644 }
3645 f << m_link << ",";
3646 if (!m_fieldName.empty())
3647 f << m_fieldName << ",";
3648 return true;
3649 }
3650
3651 //! the current cluster
3652 std::shared_ptr<ClusterGraphic> m_cluster;
3653 //! a index to know which field is parsed : 0: graphdata, 1: list, 2: clustLink, graph transform, 3:fSz=91
3654 int m_what;
3655 //! the link id: 0: unicode, 1: condition, 2: clustLink, 3: clustLink[list], 4: clustLink[8], 5: transformation, 6: some dimension?
3656 int m_linkId;
3657 //! the actual field name
3658 std::string m_fieldName;
3659 //! the conditions formula links (unsure)
3660 std::vector<RagTime5ClusterManager::Link> m_conditionFormulaLinks;
3661
3662 //! the expected id
3663 std::map<int,int> m_expectedIdToType;
3664 //! the id stack
3665 std::stack<int> m_idStack;
3666 private:
3667 //! copy constructor (not implemented)
3668 GraphicCParser(GraphicCParser const &orig) = delete;
3669 //! copy operator (not implemented)
3670 GraphicCParser &operator=(GraphicCParser const &orig) = delete;
3671 };
3672
3673 GraphicCParser::~GraphicCParser()
3674 {
3675 }
3676
3677 }
3678
3679 ////////////////////////////////////////////////////////////
3680 // button
3681 ////////////////////////////////////////////////////////////
3682 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Graph::readButtonCluster(RagTime5Zone &zone, int zoneType)
3683 {
3684 auto clusterManager=m_document.getClusterManager();
3685 if (!clusterManager) {
3686 MWAW_DEBUG_MSG(("RagTime5Graph::readButtonCluster: oops can not find the cluster manager\n"));
3687 return std::shared_ptr<RagTime5ClusterManager::Cluster>();
3688 }
3689 RagTime5GraphInternal::ButtonCParser parser(*clusterManager, zoneType);
3690 if (!clusterManager->readCluster(zone, parser) || !parser.getButtonCluster()) {
3691 MWAW_DEBUG_MSG(("RagTime5Graph::readButtonCluster: oops can not find the cluster\n"));
3692 return std::shared_ptr<RagTime5ClusterManager::Cluster>();
3693 }
3694 auto button=parser.getButtonCluster();
3695 if (m_state->m_idButtonMap.find(zone.m_ids[0])!=m_state->m_idButtonMap.end()) {
3696 MWAW_DEBUG_MSG(("RagTime5Graph::readButtonCluster: oops button zone %d is already stored\n", zone.m_ids[0]));
3697 }
3698 else
3699 m_state->m_idButtonMap[zone.m_ids[0]]=button;
3700 if (!button->m_itemNamesLink.empty()) {
3701 RagTime5ClusterManager::NameLink nameLink(button->m_itemNamesLink);
3702 m_document.readUnicodeStringList(nameLink, button->m_idToItemStringMap);
3703 }
3704 std::shared_ptr<RagTime5Zone> dataZone;
3705 int id=button->m_scriptComment.m_ids.empty() ? 0 : button->m_scriptComment.m_ids[0];
3706 if (id) dataZone=m_document.getDataZone(button->m_scriptComment.m_ids[0]);
3707 if (!dataZone && id) {
3708 MWAW_DEBUG_MSG(("RagTime5Document::readButtonCluster: the script comment zone %d seems bad\n", id));
3709 }
3710 else if (dataZone) {
3711 dataZone->m_hiLoEndian=button->m_hiLoEndian;
3712 m_document.readScriptComment(*dataZone);
3713 }
3714 std::vector<RagTime5StructManager::ZoneLink> listCluster;
3715 m_document.readClusterLinkList(button->m_parentLink, listCluster, "ButtonParentLst");
3716
3717 for (auto const &lnk : button->m_linksList) {
3718 if (lnk.m_type==RagTime5ClusterManager::Link::L_List) {
3719 m_document.readListZone(lnk);
3720 continue;
3721 }
3722 std::stringstream s;
3723 s << "DataScript_" << lnk.m_fieldSize;
3724 RagTime5StructManager::DataParser defaultParser(s.str());
3725 m_document.readFixedSizeZone(lnk, defaultParser);
3726 }
3727 return button;
3728 }
3729
3730 ////////////////////////////////////////////////////////////
3731 // picture
3732 ////////////////////////////////////////////////////////////
3733 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Graph::readPictureCluster(RagTime5Zone &zone, int zoneType)
3734 {
3735 auto clusterManager=m_document.getClusterManager();
3736 if (!clusterManager) {
3737 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureCluster: oops can not find the cluster manager\n"));
3738 return std::shared_ptr<RagTime5ClusterManager::Cluster>();
3739 }
3740 RagTime5GraphInternal::PictCParser parser(*clusterManager, zoneType);
3741 if (!clusterManager->readCluster(zone, parser) || !parser.getPictureCluster()) {
3742 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureCluster: oops can not find the cluster\n"));
3743 return std::shared_ptr<RagTime5ClusterManager::Cluster>();
3744 }
3745
3746 auto cluster=parser.getPictureCluster();
3747 if (m_state->m_idPictClusterMap.find(zone.m_ids[0])!=m_state->m_idPictClusterMap.end()) {
3748 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureCluster: oops picture zone %d is already stored\n", zone.m_ids[0]));
3749 }
3750 else
3751 m_state->m_idPictClusterMap[zone.m_ids[0]]=cluster;
3752 m_document.checkClusterList(cluster->m_clusterIdsList);
3753 if (cluster->m_containerId>0) {
3754 auto data=m_document.getDataZone(cluster->m_containerId);
3755 if (!data) {
3756 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureCluster: can not find container zone %d\n", cluster->m_containerId));
3757 }
3758 else if (!data->m_isParsed) {
3759 data->m_hiLoEndian=cluster->m_hiLoEndian;
3760 readPictureContainer(*data);
3761 }
3762 }
3763 if (!cluster->m_auxilliarLink.empty()) { // list of increasing int sequence....
3764 if (cluster->m_auxilliarLink.m_fileType[0]==(unsigned long)(long(0x80045080))) { // v5-6.5
3765 RagTime5GraphInternal::IntListParser intParser(2, "PictListInt");
3766 m_document.readListZone(cluster->m_auxilliarLink, intParser);
3767 }
3768 else if (cluster->m_auxilliarLink.m_ids.size()==1) { // v6.6
3769 std::vector<long> intList;
3770 cluster->m_auxilliarLink.m_name="PictListInt";
3771 m_document.readLongList(cluster->m_auxilliarLink, intList);
3772 }
3773 else {
3774 MWAW_DEBUG_MSG(("RagTime5Graph::readPictureCluster: unexpected auxilliar link for zone %d\n", cluster->m_containerId));
3775 }
3776 }
3777 if (!cluster->m_parentLink.empty()) {
3778 RagTime5GraphInternal::ClustListParser clustParser(*clusterManager, "PictParentLst");
3779 m_document.readListZone(cluster->m_parentLink, clustParser);
3780 m_document.checkClusterList(clustParser.m_clusterList);
3781 }
3782 for (auto const &lnk : cluster->m_linksList) {
3783 if (lnk.m_type==RagTime5ClusterManager::Link::L_List) {
3784 m_document.readListZone(lnk);
3785 continue;
3786 }
3787 std::stringstream s;
3788 s << "PictData" << lnk.m_fieldSize;
3789 RagTime5StructManager::DataParser defaultParser(s.str());
3790 m_document.readFixedSizeZone(lnk, defaultParser);
3791 }
3792
3793 return cluster;
3794 }
3795
3796 ////////////////////////////////////////////////////////////
3797 // shape
3798 ////////////////////////////////////////////////////////////
3799 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Graph::readGraphicCluster(RagTime5Zone &zone, int zoneType)
3800 {
3801 std::shared_ptr<RagTime5ClusterManager> clusterManager=m_document.getClusterManager();
3802 if (!clusterManager) {
3803 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicCluster: oops can not find the cluster manager\n"));
3804 return std::shared_ptr<RagTime5ClusterManager::Cluster>();
3805 }
3806 RagTime5GraphInternal::GraphicCParser parser(*clusterManager, zoneType);
3807 if (!clusterManager->readCluster(zone, parser) || !parser.getGraphicCluster()) {
3808 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicCluster: oops can not find the cluster\n"));
3809 return std::shared_ptr<RagTime5ClusterManager::Cluster>();
3810 }
3811
3812 auto cluster=parser.getGraphicCluster();
3813 if (m_state->m_idGraphicMap.find(zone.m_ids[0])!=m_state->m_idGraphicMap.end()) {
3814 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicCluster: oops graphic zone %d is already stored\n", zone.m_ids[0]));
3815 }
3816 else
3817 m_state->m_idGraphicMap[zone.m_ids[0]]=cluster;
3818 m_document.checkClusterList(cluster->m_clusterIdsList);
3819
3820 if (cluster->m_usedZoneId && !readGraphicUsed(cluster->m_usedZoneId)) {
3821 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicCluster: the zone id=%d seems bad\n", cluster->m_usedZoneId));
3822 }
3823
3824 for (auto const &link : cluster->m_transformationLinks)
3825 readGraphicTransformations(link);
3826 for (auto &link : cluster->m_dimensionLinks) {
3827 RagTime5GraphInternal::FloatParser floatParser("GraphDim");
3828 m_document.readFixedSizeZone(link, floatParser);
3829 }
3830 if (!cluster->m_clusterLinks[0].empty()) {
3831 // change me
3832 auto data=m_document.getDataZone(cluster->m_clusterLinks[0].m_ids[0]);
3833 if (!data || data->m_isParsed) {
3834 MWAW_DEBUG_MSG(("RagTime5Graph::readGraphicCluster: can not find data zone %d\n", cluster->m_clusterLinks[0].m_ids[0]));
3835 }
3836 else {
3837 data->m_hiLoEndian=cluster->m_hiLoEndian;
3838 m_document.readClusterLinkList(*data, cluster->m_clusterLinks[0], cluster->m_linkList);
3839 }
3840 }
3841 if (!cluster->m_parentLink.empty()) {
3842 std::vector<RagTime5StructManager::ZoneLink> list;
3843 m_document.readClusterLinkList(cluster->m_parentLink, list, "GraphParentLst");
3844 }
3845 if (!cluster->m_clusterLinks[1].empty()) {
3846 RagTime5GraphInternal::ClustListParser clustParser(*clusterManager, "GraphClustLst3");
3847 m_document.readFixedSizeZone(cluster->m_clusterLinks[1], clustParser);
3848 }
3849 readGraphicShapes(*cluster);
3850
3851 // can have some condition formula ?
3852 for (int wh=0; wh<2; ++wh) {
3853 auto const &list=wh==0 ? parser.getConditionFormulaLinks() : cluster->m_settingLinks;
3854 for (auto const &link : list) {
3855 if (link.empty()) continue;
3856 RagTime5StructManager::FieldParser defaultParser(wh==0 ? "CondFormula" : "Settings");
3857 m_document.readStructZone(link, defaultParser, 0);
3858 }
3859 }
3860
3861 for (auto const &lnk : cluster->m_linksList) {
3862 if (lnk.m_type==RagTime5ClusterManager::Link::L_List) {
3863 m_document.readListZone(lnk);
3864 }
3865 else if (lnk.m_type==RagTime5ClusterManager::Link::L_LongList) {
3866 std::vector<long> list;
3867 m_document.readLongList(lnk, list);
3868 }
3869 else {
3870 std::stringstream s;
3871 s << "Graph_Data" << lnk.m_fieldSize;
3872 m_document.readFixedSizeZone(lnk, s.str());
3873 }
3874 }
3875
3876 checkGraphicCluster(*cluster);
3877 return cluster;
3878 }
3879
3880 void RagTime5Graph::checkGraphicCluster(RagTime5GraphInternal::ClusterGraphic &cluster)
3881 {
3882 // time to check that all is valid and update root list
3883 std::vector<int> &rootList=cluster.m_rootIdList;
3884 std::stack<int> toCheck;
3885 std::multimap<int, int> idToChildIpMap;
3886 for (auto const &sIt : cluster.m_idToShapeMap) {
3887 if (!sIt.second)
3888 continue;
3889 auto &shape=*sIt.second;
3890 if (shape.m_parentId>0 && cluster.m_idToShapeMap.find(shape.m_parentId)==cluster.m_idToShapeMap.end()) {
3891 MWAW_DEBUG_MSG(("RagTime5Graph::checkGraphicCluster: find unexpected parent %d for shape %d\n",
3892 shape.m_parentId, sIt.first));
3893 shape.m_parentId=0;
3894 continue;
3895 }
3896 else if (shape.m_parentId>0) {
3897 idToChildIpMap.insert(std::multimap<int, int>::value_type(shape.m_parentId,sIt.first));
3898 continue;
3899 }
3900 rootList.push_back(sIt.first);
3901 toCheck.push(sIt.first);
3902 }
3903
3904 std::set<int> seens;
3905 while (true) {
3906 int posToCheck=0; // to make clang happy
3907 if (!toCheck.empty()) {
3908 posToCheck=toCheck.top();
3909 toCheck.pop();
3910 }
3911 else if (seens.size()==cluster.m_idToShapeMap.size())
3912 break;
3913 else {
3914 bool ok=false;
3915 for (auto const &sIt : cluster.m_idToShapeMap) {
3916 if (!sIt.second || seens.find(sIt.first)!=seens.end())
3917 continue;
3918 MWAW_DEBUG_MSG(("RagTime5Graph::checkGraphicCluster: find unexpected root %d\n", sIt.first));
3919 posToCheck=sIt.first;
3920 rootList.push_back(sIt.first);
3921
3922 RagTime5GraphInternal::Shape &shape=*sIt.second;
3923 shape.m_parentId=0;
3924 ok=true;
3925 break;
3926 }
3927 if (!ok)
3928 break;
3929 }
3930 if (seens.find(posToCheck)!=seens.end()) {
3931 MWAW_DEBUG_MSG(("RagTime5Graph::checkGraphicCluster: oops, %d is already seens\n", posToCheck));
3932 continue;
3933 }
3934
3935 seens.insert(posToCheck);
3936 auto childIt=idToChildIpMap.lower_bound(posToCheck);
3937 std::vector<int> badChildList, goodChildList;
3938
3939 RagTime5GraphInternal::Shape *group=nullptr;
3940 if (childIt!=idToChildIpMap.end() && childIt->first==posToCheck) {
3941 if (cluster.m_idToShapeMap.find(posToCheck)!=cluster.m_idToShapeMap.end() &&
3942 cluster.m_idToShapeMap.find(posToCheck)->second)
3943 group=cluster.m_idToShapeMap.find(posToCheck)->second.get();
3944 if (group && group->m_type!=RagTime5GraphInternal::Shape::S_Group)
3945 group=nullptr;
3946 if (!group) {
3947 MWAW_DEBUG_MSG(("RagTime5Graph::checkGraphicCluster: oops, %d is not a group\n", posToCheck));
3948 }
3949 }
3950 while (childIt!=idToChildIpMap.end() && childIt->first==posToCheck) {
3951 int childId=childIt++->second;
3952 bool ok=group!=nullptr;
3953 if (ok && seens.find(childId)!=seens.end()) {
3954 MWAW_DEBUG_MSG(("RagTime5Graph::checkGraphicCluster: find loop for child %d\n", childId));
3955 ok=false;
3956 }
3957 if (ok) {
3958 ok=false;
3959 for (auto child : group->m_childIdList) {
3960 if (child!=childId)
3961 continue;
3962 ok=true;
3963 break;
3964 }
3965 if (!ok) {
3966 MWAW_DEBUG_MSG(("RagTime5Graph::checkGraphicCluster: can not find child %d in group %d\n", childId, posToCheck));
3967 }
3968 }
3969 if (!ok) {
3970 if (cluster.m_idToShapeMap.find(childId)!=cluster.m_idToShapeMap.end() &&
3971 cluster.m_idToShapeMap.find(childId)->second)
3972 cluster.m_idToShapeMap.find(childId)->second->m_parentId=0;
3973 badChildList.push_back(childId);
3974 continue;
3975 }
3976 goodChildList.push_back(childId);
3977 toCheck.push(childId);
3978 }
3979 if (group && group->m_childIdList.size()!=goodChildList.size()) {
3980 MWAW_DEBUG_MSG(("RagTime5Graph::checkGraphicCluster: need to update the child list of group %d: %d child->%d new child\n",
3981 posToCheck, int(group->m_childIdList.size()), static_cast<int>(goodChildList.size())));
3982 group->m_childIdList=goodChildList;
3983 }
3984 for (auto badId : badChildList) {
3985 childIt=idToChildIpMap.lower_bound(posToCheck);
3986 while (childIt!=idToChildIpMap.end() && childIt->first==posToCheck) {
3987 if (childIt->second==badId) {
3988 idToChildIpMap.erase(childIt);
3989 break;
3990 }
3991 ++childIt;
3992 }
3993 }
3994 }
3995 // check that all linkId are valid
3996 for (auto const &sIt : cluster.m_idToShapeMap) {
3997 if (!sIt.second)
3998 continue;
3999 auto &shape=*sIt.second;
4000 if (!shape.m_linkId || shape.m_type==RagTime5GraphInternal::Shape::S_TextBox)
4001 continue;
4002 if (shape.m_linkId<1 || shape.m_linkId>=static_cast<int>(cluster.m_linkList.size())) {
4003 MWAW_DEBUG_MSG(("RagTime5Graph::checkGraphicCluster: can not find link %d\n", shape.m_linkId));
4004 shape.m_linkId=0;
4005 continue;
4006 }
4007 auto const &link=cluster.m_linkList[size_t(shape.m_linkId)];
4008 if (!link.m_dataId || link.m_subZoneId[1]!=shape.m_id) {
4009 MWAW_DEBUG_MSG(("RagTime5Graph::checkGraphicCluster: link %d seems bad\n", shape.m_linkId));
4010 shape.m_linkId=0;
4011 continue;
4012 }
4013 shape.m_linkId=link.m_dataId;
4014 shape.m_partId=link.getSubZoneId(0);
4015 }
4016 }
4017
4018 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: