Debian Codegraph @

src/lib/GreatWksGraph.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 <sstream>
41
42 #include <librevenge/librevenge.h>
43
44 #include "MWAWFont.hxx"
45 #include "MWAWGraphicEncoder.hxx"
46 #include "MWAWGraphicListener.hxx"
47 #include "MWAWGraphicShape.hxx"
48 #include "MWAWGraphicStyle.hxx"
49 #include "MWAWListener.hxx"
50 #include "MWAWParser.hxx"
51 #include "MWAWPictMac.hxx"
52 #include "MWAWPosition.hxx"
53 #include "MWAWRSRCParser.hxx"
54 #include "MWAWSubDocument.hxx"
55
56 #include "GreatWksDocument.hxx"
57 #include "GreatWksGraph.hxx"
58
59 /** Internal: the structures of a GreatWksGraph */
60 namespace GreatWksGraphInternal
61 {
62 ////////////////////////////////////////
63 //! Internal: the graphic zone of a GreatWksGraph
64 struct Frame {
65 //! the frame type
66 enum Type { T_BAD, T_BASIC, T_GROUP, T_PICTURE, T_TEXT, T_DBFIELD, T_UNSET };
67 //! constructor
68 Frame()
69 : m_type(-1)
70 , m_styleId(-1)
71 , m_parent(0)
72 , m_order(-1)
73 , m_dataSize(0)
74 , m_box()
75 , m_page(-1)
76 , m_extra("")
77 , m_parsed(false)
78 {
79 }
80 Frame(Frame const &)=default;
81 //! destructor
82 virtual ~Frame();
83 //! return the frame type
84 virtual Type getType() const
85 {
86 return T_UNSET;
87 }
88 //! operator<<
89 friend std::ostream &operator<<(std::ostream &o, Frame const &zone)
90 {
91 zone.print(o);
92 return o;
93 }
94 //! a virtual print function
95 virtual void print(std::ostream &o) const
96 {
97 switch (m_type) {
98 case 1:
99 o << "text,";
100 break;
101 case 2:
102 o << "line,";
103 break;
104 case 3:
105 o << "rect,";
106 break;
107 case 4:
108 o << "roundrect,";
109 break;
110 case 5:
111 o << "oval,";
112 break;
113 case 6:
114 o << "arc,";
115 break;
116 case 7:
117 o << "poly[regular],";
118 break;
119 case 8:
120 o << "poly,";
121 break;
122 case 10:
123 o << "database[field],";
124 break;
125 case 11:
126 o << "picture,";
127 break;
128 case 12:
129 o << "spline,";
130 break;
131 case 15:
132 o << "group,";
133 break;
134 default:
135 o << "type=" << m_type << ",";
136 break;
137 }
138 if (m_styleId >= 0)
139 o << "S" << m_styleId << ",";
140 if (m_order >= 0)
141 o << "order=" << m_order << ",";
142 if (m_parent > 0)
143 o << "F" << m_parent << "[parent],";
144 if (m_dataSize > 0)
145 o << "dataSize=" << m_dataSize << ",";
146 o << "box=" << m_box << ",";
147 if (m_page>0)
148 o << "page=" << m_page << ",";
149 o << m_extra;
150 }
151 //! the zone type
152 int m_type;
153 //! the style identifier
154 int m_styleId;
155 //! the parent identifier
156 int m_parent;
157 //! the z order
158 int m_order;
159 //! the data size ( if know)
160 long m_dataSize;
161 //! the zone bdbox
162 MWAWBox2f m_box;
163 //! the page
164 int m_page;
165 //! extra data
166 std::string m_extra;
167 //! true if the frame is send
168 mutable bool m_parsed;
169 };
170
171 Frame::~Frame()
172 {
173 }
174
175 ////////////////////////////////////////
176 //! Internal: a unknown zone of a GreatWksGraph
177 struct FrameBad final : public Frame {
178 //! constructor
179 FrameBad() : Frame()
180 {
181 }
182 //! destructor
183 ~FrameBad() final;
184 //! return the frame type
185 Type getType() const final
186 {
187 return T_BAD;
188 }
189 };
190
191 FrameBad::~FrameBad()
192 {
193 }
194
195 ////////////////////////////////////////
196 //! Internal: the basic shape of a GreatWksGraph
197 struct FrameShape final : public Frame {
198 //! constructor
199 explicit FrameShape(Frame const &frame)
200 : Frame(frame)
201 , m_shape()
202 , m_lineArrow(0)
203 , m_lineFormat(0)
204 {
205 }
206 //! destructor
207 ~FrameShape() final;
208 //! return the frame type
209 Type getType() const final
210 {
211 return T_BASIC;
212 }
213 //! print function
214 void print(std::ostream &o) const final
215 {
216 Frame::print(o);
217 switch (m_lineArrow) {
218 case 0: // unset
219 case 1: // none
220 break;
221 case 2:
222 o << "arrow=\'>\',";
223 break;
224 case 3:
225 o << "arrow=\'<\',";
226 break;
227 case 4:
228 o << "arrow=\'<>\',";
229 break;
230 default:
231 o<< "#arrow=" << m_lineArrow << ",";
232 }
233 if (m_lineFormat)
234 o << "L" << m_lineFormat << ",";
235 }
236 //! update the style
237 void updateStyle(MWAWGraphicStyle &style) const
238 {
239 if (m_shape.m_type!=MWAWGraphicShape::Line) {
240 style.m_arrows[0]=style.m_arrows[1]=MWAWGraphicStyle::Arrow();
241 style.m_lineDashWidth.resize(0);
242 }
243 else if (m_lineArrow > 1) {
244 switch (m_lineArrow) {
245 case 2:
246 style.m_arrows[1]=MWAWGraphicStyle::Arrow::plain();
247 break;
248 case 3:
249 style.m_arrows[0]=MWAWGraphicStyle::Arrow::plain();
250 break;
251 case 4:
252 style.m_arrows[0]=style.m_arrows[1]=MWAWGraphicStyle::Arrow::plain();
253 break;
254 default:
255 break;
256 }
257 }
258 }
259 //! the shape
260 MWAWGraphicShape m_shape;
261 //! the line arrow style (in v1)
262 int m_lineArrow;
263 //! the line format?
264 int m_lineFormat;
265 private:
266 FrameShape(FrameShape const &) = delete;
267 FrameShape &operator=(FrameShape const &) = delete;
268 };
269
270 FrameShape::~FrameShape()
271 {
272 }
273
274 ////////////////////////////////////////
275 //! Internal: the group zone of a GreatWksGraph
276 struct FrameGroup final : public Frame {
277 //! constructor
278 explicit FrameGroup(Frame const &frame)
279 : Frame(frame)
280 , m_numChild(0)
281 , m_childList()
282 {
283 }
284 //! destructor
285 ~FrameGroup() final;
286 //! return the frame type
287 Type getType() const final
288 {
289 return T_GROUP;
290 }
291 //! print funtion
292 void print(std::ostream &o) const final
293 {
294 Frame::print(o);
295 if (m_numChild)
296 o << "nChild=" << m_numChild << ",";
297 }
298 //! the number of child
299 int m_numChild;
300 //! the list of child
301 std::vector<int> m_childList;
302 };
303
304 FrameGroup::~FrameGroup()
305 {
306 }
307
308 ////////////////////////////////////////
309 //! Internal: the picture zone of a GreatWksGraph
310 struct FramePicture final : public Frame {
311 //! constructor
312 explicit FramePicture(Frame const &frame)
313 : Frame(frame)
314 , m_entry()
315 {
316 }
317 //! destructor
318 ~FramePicture() final;
319 //! return the frame type
320 Type getType() const final
321 {
322 return T_PICTURE;
323 }
324 //! print funtion
325 void print(std::ostream &o) const final
326 {
327 Frame::print(o);
328 if (m_entry.valid())
329 o << "pos=" << std::hex << m_entry.begin() << "->" << m_entry.end() << std::dec << ",";
330 }
331 //! the picture entry
332 MWAWEntry m_entry;
333 };
334
335 FramePicture::~FramePicture()
336 {
337 }
338
339 ////////////////////////////////////////
340 //! Internal: the text zone of a GreatWksGraph
341 struct FrameText final : public Frame {
342 //! constructor
343 explicit FrameText(Frame const &frame)
344 : Frame(frame)
345 , m_entry()
346 , m_rotate(0)
347 {
348 for (auto &flip : m_flip) flip=false;
349 }
350 //! destructor
351 ~FrameText() final;
352 //! return the frame type
353 Type getType() const final
354 {
355 return T_TEXT;
356 }
357 //! print funtion
358 void print(std::ostream &o) const final
359 {
360 Frame::print(o);
361 if (m_entry.valid())
362 o << "pos=" << std::hex << m_entry.begin() << "->" << m_entry.end() << std::dec << ",";
363 if (m_rotate) o << "rot=" << m_rotate << ",";
364 if (m_flip[0]) o << "flipX=" << m_flip[0] << ",";
365 if (m_flip[1]) o << "flipY=" << m_flip[1] << ",";
366 }
367 //! return the text style
368 MWAWGraphicStyle getStyle(MWAWGraphicStyle const &zoneStyle) const
369 {
370 MWAWGraphicStyle res(zoneStyle);
371 res.m_lineWidth=0; // no border
372 res.m_flip[0]=m_flip[0];
373 res.m_flip[1]=m_flip[1];
374 res.m_rotate = float(m_rotate);
375 return res;
376 }
377 /** return true if the has some transforms.
378
379 \note as we have no way to retrieve mirror, we consider only rotation here*/
380 bool hasTransform() const
381 {
382 return (m_flip[0]&m_flip[1]) || m_rotate;
383 }
384 //! the text entry
385 MWAWEntry m_entry;
386 //! two bool to know if we must flip x or y
387 bool m_flip[2];
388 //! the rotate angle
389 int m_rotate;
390 };
391
392 FrameText::~FrameText()
393 {
394 }
395
396 ////////////////////////////////////////
397 //! Internal: the DBField zone of a GreatWksGraph
398 struct FrameDBField final : public Frame {
399 //! constructor
400 explicit FrameDBField(Frame const &frame)
401 : Frame(frame)
402 {
403 }
404 //! destructor
405 ~FrameDBField() final;
406 //! return the frame type
407 Type getType() const final
408 {
409 return T_DBFIELD;
410 }
411 //! print funtion
412 void print(std::ostream &o) const final
413 {
414 Frame::print(o);
415 }
416 };
417
418 FrameDBField::~FrameDBField()
419 {
420 }
421
422 ////////////////////////////////////////
423 //! Internal: a list of graphic corresponding to a page
424 struct Zone {
425 //! constructor
426 Zone()
427 : m_page(-1)
428 , m_frameList()
429 , m_rootList()
430 , m_styleList()
431 , m_parsed(false)
432 {
433 }
434 //! the page number (if known)
435 int m_page;
436 //! the list of frame
437 std::vector<std::shared_ptr<Frame> > m_frameList;
438 //! the list of root id
439 std::vector<int> m_rootList;
440 //! the list of style
441 std::vector<MWAWGraphicStyle> m_styleList;
442 //! true if we have send the data
443 mutable bool m_parsed;
444 };
445
446 ////////////////////////////////////////
447 //! Internal: the state of a GreatWksGraph
448 struct State {
449 //! constructor
450 State()
451 : m_zoneList()
452 , m_numPages(0) { }
453 //! the list of zone ( one by page)
454 std::vector<Zone> m_zoneList;
455 int m_numPages /* the number of pages */;
456 };
457
458
459 ////////////////////////////////////////
460 //! Internal: the subdocument of a GreatWksGraph
461 class SubDocument final : public MWAWSubDocument
462 {
463 public:
464 //! constructor
465 SubDocument(GreatWksGraph &pars, MWAWInputStreamPtr const &input, MWAWEntry const &entry)
466 : MWAWSubDocument(pars.m_mainParser, input, MWAWEntry(entry))
467 , m_graphParser(&pars) {}
468
469
470 //! destructor
471 ~SubDocument() final {}
472
473 //! operator!=
474 bool operator!=(MWAWSubDocument const &doc) const final;
475
476 //! the parser function
477 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType) final;
478
479 protected:
480 /** the graph parser */
481 GreatWksGraph *m_graphParser;
482
483 private:
484 SubDocument(SubDocument const &orig) = delete;
485 SubDocument &operator=(SubDocument const &orig) = delete;
486 };
487
488 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
489 {
490 if (!listener || !listener->canWriteText()) {
491 MWAW_DEBUG_MSG(("GreatWksGraphInternal::SubDocument::parse: no listener\n"));
492 return;
493 }
494 if (!m_graphParser) {
495 MWAW_DEBUG_MSG(("GreatWksGraphInternal::SubDocument::parse: no parser\n"));
496 return;
497 }
498 long pos = m_input->tell();
499 m_graphParser->sendTextbox(m_zone,listener);
500 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
501 }
502
503 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
504 {
505 if (MWAWSubDocument::operator!=(doc)) return true;
506 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
507 if (!sDoc) return true;
508 if (m_graphParser != sDoc->m_graphParser) return true;
509 return false;
510 }
511 }
512
513 ////////////////////////////////////////////////////////////
514 // constructor/destructor, ...
515 ////////////////////////////////////////////////////////////
516 GreatWksGraph::GreatWksGraph(GreatWksDocument &document)
517 : m_document(document)
518 , m_parserState(document.m_parserState)
519 , m_state(new GreatWksGraphInternal::State)
520 , m_mainParser(&document.getMainParser())
521 {
522 }
523
524 GreatWksGraph::~GreatWksGraph()
525 { }
526
527 int GreatWksGraph::version() const
528 {
529 return m_parserState->m_version;
530 }
531
532
533 int GreatWksGraph::numPages() const
534 {
535 if (m_state->m_numPages)
536 return m_state->m_numPages;
537 int nPages = 0;
538 for (auto const &zone : m_state->m_zoneList) {
539 if (zone.m_page>nPages)
540 nPages=zone.m_page>nPages;
541 }
542
543 m_state->m_numPages = nPages;
544 return nPages;
545 }
546
547 bool GreatWksGraph::sendTextbox(MWAWEntry const &entry, MWAWListenerPtr listener)
548 {
549 return m_document.sendTextbox(entry, listener);
550 }
551
552 ////////////////////////////////////////////////////////////
553 //
554 // Intermediate level
555 //
556 ////////////////////////////////////////////////////////////
557
558 ////////////////////////////////////////////////////////////
559 // read the patterns list
560 ////////////////////////////////////////////////////////////
561 bool GreatWksGraph::readPatterns(MWAWEntry const &entry)
562 {
563 if (!entry.valid() || (entry.length()%8) != 2) {
564 MWAW_DEBUG_MSG(("GreatWksGraph::readPatterns: the entry is bad\n"));
565 return false;
566 }
567
568 long pos = entry.begin();
569 MWAWInputStreamPtr input = m_parserState->m_rsrcParser->getInput();
570 libmwaw::DebugFile &ascFile = m_parserState->m_rsrcParser->ascii();
571 libmwaw::DebugStream f;
572 entry.setParsed(true);
573
574 input->seek(pos, librevenge::RVNG_SEEK_SET);
575 f << "Entries(Pattern):";
576 auto N=static_cast<int>(input->readLong(2));
577 f << "N=" << N << ",";
578 if (2+8*N!=int(entry.length())) {
579 f << "###";
580 MWAW_DEBUG_MSG(("GreatWksGraph::readPatterns: the number of entries seems bad\n"));
581 ascFile.addPos(pos-4);
582 ascFile.addNote(f.str().c_str());
583 return true;
584 }
585 ascFile.addPos(pos-4);
586 ascFile.addNote(f.str().c_str());
587 for (int i=0; i < N; ++i) {
588 pos = input->tell();
589 f.str("");
590 f << "Pattern-" << i << ":";
591 MWAWGraphicStyle::Pattern pat;
592 pat.m_dim=MWAWVec2i(8,8);
593 pat.m_data.resize(8);
594 for (size_t j=0; j < 8; ++j)
595 pat.m_data[j]=static_cast<unsigned char>(input->readLong(1));
596 f << pat;
597 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
598 ascFile.addPos(pos);
599 ascFile.addNote(f.str().c_str());
600 }
601 return true;
602 }
603
604 bool GreatWksGraph::readPalettes(MWAWEntry const &entry)
605 {
606 if (!entry.valid() || entry.length() != 0x664) {
607 MWAW_DEBUG_MSG(("GreatWksGraph::readPalettes: the entry is bad\n"));
608 return false;
609 }
610
611 long pos = entry.begin();
612 MWAWInputStreamPtr input = m_parserState->m_rsrcParser->getInput();
613 libmwaw::DebugFile &ascFile = m_parserState->m_rsrcParser->ascii();
614 libmwaw::DebugStream f;
615 entry.setParsed(true);
616
617 input->seek(pos, librevenge::RVNG_SEEK_SET);
618 f << "Entries(Palette):";
619 auto val=static_cast<int>(input->readLong(2));
620 if (val!=2)
621 f << "#f0=" << val << ",";
622 val=static_cast<int>(input->readLong(2));
623 if (val!=8)
624 f << "#f1=" << val << ",";
625 ascFile.addPos(pos-4);
626 ascFile.addNote(f.str().c_str());
627
628 // 16 sets like: ffff, 6464, 0202 : maybe some color
629 for (int i=0; i < 16; ++i) {
630 pos = input->tell();
631 f.str("");
632 f << "Palette-" << i << ":";
633 for (int j=0; j < 3; ++j)
634 f << std::hex << input->readULong(2) << std::dec << ",";
635 input->seek(pos+6, librevenge::RVNG_SEEK_SET);
636 ascFile.addPos(pos);
637 ascFile.addNote(f.str().c_str());
638 }
639
640 for (int i=0; i < 256; ++i) {
641 pos = input->tell();
642 f.str("");
643 if (i==0) f << "Entries(Colors)-0:";
644 else f << "Colors-" << i << ":";
645 unsigned char col[3];
646 for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8);
647 f << MWAWColor(col[0], col[1], col[2]) << ",";
648 input->seek(pos+6, librevenge::RVNG_SEEK_SET);
649 ascFile.addPos(pos);
650 ascFile.addNote(f.str().c_str());
651 }
652
653 return true;
654 }
655
656 ////////////////////////////////////////////////////////////
657 // graphic zone ( main header )
658 ////////////////////////////////////////////////////////////
659 bool GreatWksGraph::isGraphicZone()
660 {
661 int const vers=version();
662 bool isDraw=m_parserState->m_kind==MWAWDocument::MWAW_K_DRAW;
663 if (vers == 1 && !isDraw)
664 return false;
665 int headerSize;
666 if (vers==1)
667 headerSize= 0x1c+0x38+0x1e +0x1a;
668 else
669 headerSize= 0x1c+0xaa+0x30;
670 MWAWInputStreamPtr input = m_parserState->m_input;
671 long pos = input->tell();
672 if (!input->checkPosition(pos+headerSize))
673 return false;
674
675 int dim[4];
676 for (int st=0; st<2; ++st) {
677 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
678 if (dim[0]>=dim[2] || dim[1]>=dim[3] || dim[2]<=0 || dim[3]<=0) {
679 input->seek(pos, librevenge::RVNG_SEEK_SET);
680 return false;
681 }
682 }
683
684 input->seek(pos+headerSize, librevenge::RVNG_SEEK_SET);
685 int pageHeaderSize=vers==1 ? 16 : isDraw ? 12 : 22;
686 if (!input->checkPosition(pos+headerSize+pageHeaderSize)) {
687 bool ok=input->isEnd();
688 input->seek(pos, librevenge::RVNG_SEEK_SET);
689 return ok;
690 }
691 bool ok=isPageFrames();
692 input->seek(pos, librevenge::RVNG_SEEK_SET);
693 return ok;
694 }
695
696 bool GreatWksGraph::readGraphicZone()
697 {
698 int const vers=version();
699 bool isDraw=m_parserState->m_kind==MWAWDocument::MWAW_K_DRAW;
700 if (vers == 1 && !isDraw)
701 return false;
702
703 MWAWInputStreamPtr input = m_parserState->m_input;
704 long beginPos = input->tell();
705 if (!isGraphicZone() && !findGraphicZone()) {
706 input->seek(beginPos, librevenge::RVNG_SEEK_SET);
707 return false;
708 }
709 long pos = input->tell();
710 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
711 if (pos!=beginPos) {
712 ascFile.addPos(beginPos);
713 ascFile.addNote("Entries(Unknown):");
714 }
715 libmwaw::DebugStream f;
716 f << "Entries(GZoneHeader):";
717 for (int st=0; st<2; ++st) {
718 int dim[4];
719 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
720 f << "dim" << st << "=" << dim[1] << "x" << dim[0]
721 << "<->"<< dim[3] << "x" << dim[2] << ",";
722 }
723 ascFile.addDelimiter(input->tell(),'|');
724 ascFile.addPos(pos);
725 ascFile.addNote(f.str().c_str());
726 pos += 0x1c;
727 if (vers==1) {
728 ascFile.addPos(pos);
729 ascFile.addNote("GZoneHeader-II");
730 pos += 0x38;
731
732 input->seek(pos, librevenge::RVNG_SEEK_SET);
733 f.str("");
734 f << "Entries(GLineFormat):";
735 std::string extra;
736 if (!readLineFormat(extra))
737 f << "###";
738 else
739 f << extra;
740 ascFile.addPos(pos);
741 ascFile.addNote(f.str().c_str());
742 pos += 0x1e;
743 }
744 else {
745 input->seek(pos, librevenge::RVNG_SEEK_SET);
746 MWAWGraphicStyle style;
747 f.str("");
748 f << "Entries(GStyle):";
749 if (!readStyle(style))
750 f << "###";
751 else
752 f << style;
753 ascFile.addPos(pos);
754 ascFile.addNote(f.str().c_str());
755
756 pos += 0xaa;
757
758 ascFile.addPos(pos);
759 ascFile.addNote("Entries(GDatC)[_]:");
760 pos += 0x16;
761 }
762 ascFile.addPos(pos);
763 ascFile.addNote("Entries(GDatD)[_]:");
764 pos += 0x1a;
765
766 input->seek(pos, librevenge::RVNG_SEEK_SET);
767 while (!input->isEnd() && readPageFrames())
768 pos=input->tell();
769 input->seek(pos, librevenge::RVNG_SEEK_SET);
770 return true;
771 }
772
773 bool GreatWksGraph::findGraphicZone()
774 {
775 int const vers=version();
776 bool isDraw=m_parserState->m_kind==MWAWDocument::MWAW_K_DRAW;
777 if (vers == 1 && !isDraw)
778 return false;
779 int headerSize;
780 if (vers==1)
781 headerSize= 0x1c+0x38+0x1e +0x1a;
782 else
783 headerSize= 0x1c+0xaa+0x30;
784 int pageHeaderSize=vers==1 ? 16 : isDraw ? 12 : 22;
785
786 MWAWInputStreamPtr input = m_parserState->m_input;
787 long pos = input->tell();
788 input->seek(pos+headerSize+pageHeaderSize, librevenge::RVNG_SEEK_SET);
789 while (!input->isEnd()) {
790 long actPos = input->tell();
791 unsigned long value= input->readULong(4);
792 int decal=-1;
793 // if we find some tabs, we have a problem
794 if (value==0x20FFFF)
795 decal = 0;
796 else if (value==0x20FFFFFF)
797 decal = 1;
798 else if (value==0xFFFFFFFF)
799 decal = 2;
800 else if (value==0xFFFFFF2E)
801 decal = 3;
802 if (decal>=0) {
803 input->seek(actPos-decal, librevenge::RVNG_SEEK_SET);
804 if (input->readULong(4)==0x20FFFF && input->readULong(4)==0xFFFF2E00)
805 break;
806 input->seek(actPos+4, librevenge::RVNG_SEEK_SET);
807 continue;
808 }
809
810 // graphic size
811 if ((value>>24)==0x36)
812 decal = 3;
813 else if ((value>>16)==0x36)
814 decal = 2;
815 else if (((value>>8)&0xFFFF)==0x36)
816 decal = 1;
817 else if ((value&0xFFFF)==0x36)
818 decal = 0;
819 if (decal==-1)
820 continue;
821
822 input->seek(actPos-decal, librevenge::RVNG_SEEK_SET);
823 auto N=static_cast<int>(input->readULong(2));
824 if (input->readLong(2)!=0x36 || !input->checkPosition(actPos-decal+4+0x36*N)) {
825 input->seek(actPos+4, librevenge::RVNG_SEEK_SET);
826 continue;
827 }
828 input->seek(actPos-decal-pageHeaderSize-headerSize, librevenge::RVNG_SEEK_SET);
829 if (!isGraphicZone()) {
830 input->seek(actPos+4, librevenge::RVNG_SEEK_SET);
831 continue;
832 }
833 input->seek(actPos-decal-pageHeaderSize-headerSize, librevenge::RVNG_SEEK_SET);
834 return true;
835 }
836 input->seek(pos, librevenge::RVNG_SEEK_SET);
837 return false;
838 }
839
840 ////////////////////////////////////////////////////////////
841 // graphic zone ( page frames)
842 ////////////////////////////////////////////////////////////
843 bool GreatWksGraph::isPageFrames()
844 {
845 int const vers=version();
846 bool isDatabase=m_parserState->m_kind==MWAWDocument::MWAW_K_DATABASE;
847 bool hasPageUnknown=vers==2 && m_parserState->m_kind!=MWAWDocument::MWAW_K_DRAW &&
848 !isDatabase;
849 int const headerSize=hasPageUnknown ? 22 : (vers==2&&!isDatabase) ? 12 : 16;
850 int const nZones= (vers==2||isDatabase) ? 3 : 4;
851 MWAWInputStreamPtr &input= m_parserState->m_input;
852 long pos=input->tell();
853 long endPos=pos+headerSize+4*nZones;
854 if (!input->checkPosition(endPos))
855 return false;
856 long sz=-1;
857 input->seek(pos, librevenge::RVNG_SEEK_SET);
858 if (hasPageUnknown) {
859 input->seek(2, librevenge::RVNG_SEEK_CUR); // page or 1c for spreadsheet
860 sz=long(input->readULong(4));
861 endPos=input->tell()+sz;
862 }
863 long zoneSz[4]= {0,0,0,0};
864 for (int i=0; i<nZones; ++i)
865 zoneSz[i]=long(input->readULong(4));
866 if (hasPageUnknown &&
867 (6+sz<headerSize+4*nZones || zoneSz[0]+zoneSz[1]+zoneSz[2]>sz ||
868 !input->checkPosition(endPos))) {
869 input->seek(pos, librevenge::RVNG_SEEK_SET);
870 return false;
871 }
872 pos+=headerSize;
873 input->seek(pos, librevenge::RVNG_SEEK_SET);
874 int expectedSz[]= {0x36, 0xaa, 0x2, 0};
875 if (vers==1) {
876 expectedSz[1]=0x34;
877 expectedSz[2]=0x1e;
878 expectedSz[3]=2;
879 }
880 for (int i=0; i < nZones; ++i) {
881 pos=input->tell();
882 if (pos==endPos)
883 return true;
884 auto nData=static_cast<int>(input->readLong(2));
885 auto fSz=static_cast<int>(input->readLong(2));
886 if (nData<0 || (nData!=0 && fSz!=expectedSz[i]) || nData*fSz+4 > zoneSz[i]) {
887 input->seek(pos, librevenge::RVNG_SEEK_SET);
888 return false;
889 }
890 if (i!=nZones-1 && nData *fSz+4!=zoneSz[i]) {
891 MWAW_DEBUG_MSG(("GreatWksGraph::isPageFrames: find a diff of %ld for data %d\n", zoneSz[i]-nData*fSz-4, i));
892 if ((2*nData+4)*fSz+4 < zoneSz[i]) {
893 input->seek(pos, librevenge::RVNG_SEEK_SET);
894 return false;
895 }
896 }
897 input->seek(expectedSz[i]*nData, librevenge::RVNG_SEEK_CUR);
898 }
899 input->seek(pos, librevenge::RVNG_SEEK_SET);
900 return true;
901 }
902
903 bool GreatWksGraph::readStyle(MWAWGraphicStyle &style)
904 {
905 style=MWAWGraphicStyle();
906 MWAWInputStreamPtr &input= m_parserState->m_input;
907 libmwaw::DebugStream f;
908 int const vers=version();
909 int const gDataSize=vers==1 ? 0x34 : 0xaa;
910
911 long pos=input->tell();
912 long endPos=pos+gDataSize;
913 if (!input->checkPosition(endPos))
914 return false;
915
916 auto val=static_cast<int>(input->readLong(2));
917 if (val) f << "used=" << val << ",";
918 float dim[2];
919 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
920 if (dim[0]<dim[1] || dim[0]>dim[1]) f << "lineWidth[real]=" << MWAWVec2f(dim[1],dim[0]) << ",";
921 style.m_lineWidth=(dim[1]+dim[0])/2.f;
922 if (vers==1) {
923 for (int i=0; i < 2; ++i) { // two flags 0|1
924 val=static_cast<int>(input->readULong(1));
925 if (val==0 || val==1) {
926 if (i==0)
927 style.m_lineOpacity=float(val);
928 else
929 style.m_surfaceOpacity=float(val);
930 }
931 else
932 f << "#hasPat" << i << "=" << val << ",";
933 }
934 MWAWGraphicStyle::Pattern patterns[2];
935 for (auto &pattern : patterns) {
936 pattern.m_dim=MWAWVec2i(8,8);
937 pattern.m_data.resize(8);
938 for (auto &data : pattern.m_data) data=static_cast<unsigned char>(input->readULong(1));
939 }
940 for (int i=0; i < 4; ++i) {
941 unsigned char col[3];
942 for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8);
943 patterns[i/2].m_colors[1-i%2]=MWAWColor(col[0], col[1], col[2]);
944 }
945 if (!patterns[0].getUniqueColor(style.m_lineColor)) {
946 f << "linePattern=[" << patterns[0] << "],";
947 patterns[0].getAverageColor(style.m_lineColor);
948 }
949 if (!patterns[1].getUniqueColor(style.m_surfaceColor)) {
950 if (style.m_surfaceOpacity<=0)
951 f << "surfPattern=[" << patterns[1] << "],";
952 else
953 style.setPattern(patterns[1]);
954 }
955
956 style.m_extra=f.str();
957 input->seek(endPos, librevenge::RVNG_SEEK_SET);
958 return true;
959 }
960 MWAWGraphicStyle::Pattern patterns[2];
961 for (int i=0; i < 4; ++i) {
962 val=static_cast<int>(input->readULong(2));
963 if (val) f << "col" << i << "=" << std::hex << val << std::dec << ",";
964 unsigned char col[3];
965 for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8);
966 patterns[i/2].m_colors[1-i%2]=MWAWColor(col[0], col[1], col[2]);
967 }
968 val=static_cast<int>(input->readULong(2));
969 if (val) f << "col4=" << std::hex << val << std::dec << ",";
970 for (int i=0; i < 2; ++i) {
971 val=static_cast<int>(input->readULong(2)); // small number
972 if (i==0)
973 style.m_lineOpacity=val ? 1.0 : 0.0;
974 else
975 style.m_surfaceOpacity=val ? 1.0 : 0.0;
976 if (val>1)
977 f << "pat" << i << "=" << val << ",";
978 patterns[i].m_dim=MWAWVec2i(8,8);
979 patterns[i].m_data.resize(8);
980 for (auto &data : patterns[i].m_data) data=static_cast<unsigned char>(input->readULong(1));
981 }
982 if (!patterns[0].getUniqueColor(style.m_lineColor)) {
983 f << "linePattern=[" << patterns[0] << "],";
984 patterns[0].getAverageColor(style.m_lineColor);
985 }
986 if (!patterns[1].getUniqueColor(style.m_surfaceColor)) {
987 if (style.m_surfaceOpacity<=0)
988 f << "surfPattern=[" << patterns[1] << "],";
989 else
990 style.setPattern(patterns[1]);
991 }
992
993 val=static_cast<int>(input->readULong(2));
994 if (val!=1) f << "patId=" << val << ",";
995
996 int nDash=val==1 ? 1 : static_cast<int>(input->readULong(2));
997 if (nDash<0||nDash>6) {
998 MWAW_DEBUG_MSG(("GreatWksGraph::readStyle: can not read number of line dash\n"));
999 f << "#nDash=" << nDash << ",";
1000 }
1001 else {
1002 for (int i=0; i < nDash; ++i) {
1003 float w=float(input->readLong(4))/65536.f;
1004 if (w<=0) {
1005 if (i==0 && nDash==1)
1006 break;
1007 MWAW_DEBUG_MSG(("GreatWksGraph::readStyle: the line dash seems bad\n"));
1008 f << "###dash" << i << ":w=" << w << ",";
1009 style.m_lineDashWidth.resize(0);
1010 break;
1011 }
1012 style.m_lineDashWidth.push_back(w);
1013 }
1014 }
1015 input->seek(pos+116, librevenge::RVNG_SEEK_SET);
1016 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1017 ascFile.addDelimiter(pos+92,'|'); // some data?
1018 ascFile.addDelimiter(input->tell(),'|');
1019 auto gradId=static_cast<int>(input->readLong(2));
1020 if (gradId) f << "gradientId=" << gradId << ",";
1021 auto gradType=static_cast<int>(input->readLong(2));
1022 if (gradId>=1 && gradId<=16 && (gradType>=1&&gradType<=3)) {
1023 auto &finalGrad=style.m_gradient;
1024 finalGrad.m_stopList.resize(2);
1025 finalGrad.m_stopList[0]=MWAWGraphicStyle::Gradient::Stop(0.0, MWAWColor::white());
1026 finalGrad.m_stopList[1]=MWAWGraphicStyle::Gradient::Stop(1.0, MWAWColor::black());
1027 finalGrad.m_type = MWAWGraphicStyle::Gradient::G_Linear;
1028 switch (gradId) {
1029 case 1:
1030 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1031 finalGrad.m_angle = -90;
1032 break;
1033 case 2:
1034 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1035 finalGrad.m_angle = -45;
1036 break;
1037 case 3:
1038 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1039 break;
1040 case 4:
1041 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1042 finalGrad.m_stopList[0]=MWAWGraphicStyle::Gradient::Stop(0.0, MWAWColor::black());
1043 finalGrad.m_stopList[1]=MWAWGraphicStyle::Gradient::Stop(1.0, MWAWColor::white());
1044 finalGrad.m_type=MWAWGraphicStyle::Gradient::G_Axial;
1045 finalGrad.m_angle = -45;
1046 break;
1047 case 5:
1048 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1049 finalGrad.m_angle = 90;
1050 break;
1051 case 6:
1052 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1053 finalGrad.m_stopList[0]=MWAWGraphicStyle::Gradient::Stop(0.0, MWAWColor::black());
1054 finalGrad.m_stopList[1]=MWAWGraphicStyle::Gradient::Stop(1.0, MWAWColor::white());
1055 finalGrad.m_type=MWAWGraphicStyle::Gradient::G_Axial;
1056 finalGrad.m_angle = 45;
1057 break;
1058 case 7:
1059 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1060 finalGrad.m_angle = 180;
1061 break;
1062 case 8:
1063 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1064 finalGrad.m_angle = -135;
1065 break;
1066 case 9:
1067 if (gradType!=3) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1068 finalGrad.m_percentCenter=MWAWVec2f(.5f,.5f);
1069 finalGrad.m_stopList[0]=MWAWGraphicStyle::Gradient::Stop(0.0, MWAWColor::black());
1070 finalGrad.m_stopList[1]=MWAWGraphicStyle::Gradient::Stop(1.0, MWAWColor::white());
1071 finalGrad.m_type=MWAWGraphicStyle::Gradient::G_Square;
1072 break;
1073 case 10:
1074 if (gradType!=3) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1075 finalGrad.m_percentCenter=MWAWVec2f(.5f,.5f);
1076 finalGrad.m_type=MWAWGraphicStyle::Gradient::G_Square;
1077 break;
1078 case 11:
1079 case 12:
1080 if (gradType!=2) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1081 finalGrad.m_stopList[0]=MWAWGraphicStyle::Gradient::Stop(0.0, MWAWColor::black());
1082 finalGrad.m_stopList[1]=MWAWGraphicStyle::Gradient::Stop(1.0, MWAWColor::white());
1083 finalGrad.m_type=MWAWGraphicStyle::Gradient::G_Radial;
1084 break;
1085 case 13:
1086 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1087 finalGrad.m_stopList[0]=MWAWGraphicStyle::Gradient::Stop(0.0, MWAWColor(0x88,0,0));
1088 finalGrad.m_stopList[1]=MWAWGraphicStyle::Gradient::Stop(1.0, MWAWColor(0xee,0,0));
1089 finalGrad.m_angle = 90;
1090 break;
1091 case 14:
1092 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1093 finalGrad.m_stopList[0]=MWAWGraphicStyle::Gradient::Stop(0.0, MWAWColor(0,0x55,0));
1094 finalGrad.m_stopList[1]=MWAWGraphicStyle::Gradient::Stop(1.0, MWAWColor(0,0xee,0));
1095 finalGrad.m_angle = 90;
1096 break;
1097 case 15:
1098 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1099 finalGrad.m_stopList[0]=MWAWGraphicStyle::Gradient::Stop(0.0, MWAWColor(0,0,0x88));
1100 finalGrad.m_stopList[1]=MWAWGraphicStyle::Gradient::Stop(1.0, MWAWColor(0,0,0xff));
1101 finalGrad.m_angle = 90;
1102 break;
1103 case 16:
1104 if (gradType!=1) finalGrad.m_type = MWAWGraphicStyle::Gradient::G_None;
1105 finalGrad.m_stopList[0]=MWAWGraphicStyle::Gradient::Stop(0.0, MWAWColor(0xff,0xff,0));
1106 finalGrad.m_stopList[1]=MWAWGraphicStyle::Gradient::Stop(1.0, MWAWColor(0xff,0xff,0xcc));
1107 finalGrad.m_angle = 90;
1108 break;
1109 default:
1110 break;
1111 }
1112 if (finalGrad.m_type==MWAWGraphicStyle::Gradient::G_None) {
1113 MWAW_DEBUG_MSG(("GreatWksGraph::readStyle: find odd gradient\n"));
1114 f << "grad[##type]=" << gradType << ",";
1115 }
1116 }
1117 for (size_t i=0; i < 2; ++i) {
1118 unsigned char col[3];
1119 for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8);
1120 f << "grad[col" << i << "]=" << MWAWColor(col[0], col[1], col[2]) << ",";
1121 }
1122 input->seek(2, librevenge::RVNG_SEEK_CUR); // junk ?
1123 val=static_cast<int>(input->readLong(2));
1124 switch (val) {
1125 case 1: // none
1126 break;
1127 case 2:
1128 style.m_arrows[1]=MWAWGraphicStyle::Arrow::plain();
1129 break;
1130 case 3:
1131 style.m_arrows[0]=MWAWGraphicStyle::Arrow::plain();
1132 break;
1133 case 4:
1134 style.m_arrows[0]=style.m_arrows[1]=MWAWGraphicStyle::Arrow::plain();
1135 break;
1136 default:
1137 f << "#lineArrows=" << val << ",";
1138 }
1139 val=static_cast<int>(input->readLong(2));
1140 if (val!=1)
1141 f << "#lineArrow[unset],";
1142 style.m_extra=f.str();
1143
1144 pos = input->tell();
1145 std::string extra("");
1146 f.str("");
1147 f << "Entries(GLineFormat):";
1148 if (readLineFormat(extra))
1149 f << extra;
1150 else
1151 f << "###";
1152 ascFile.addPos(pos);
1153 ascFile.addNote(f.str().c_str());
1154 ascFile.addDelimiter(input->tell(),'|');
1155 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1156 return true;
1157 }
1158
1159 bool GreatWksGraph::readLineFormat(std::string &extra)
1160 {
1161 MWAWInputStreamPtr &input= m_parserState->m_input;
1162 libmwaw::DebugStream f;
1163 int const gDataSize=0x1e;
1164 long pos=input->tell();
1165 if (!input->checkPosition(pos+gDataSize))
1166 return false;
1167
1168 // find: f0=1, f1=0, f2=9|c f3=3|5, f5=c|f, f0+a dim?
1169 for (int i=0; i<5; ++i) {
1170 auto val=static_cast<int>(input->readLong(2));
1171 if (val) f << "f" << i << "=" << val << ",";
1172 }
1173
1174 extra=f.str();
1175 // then 3ffda4bc7d1934f709244002fcfb724c879139b4
1176 m_parserState->m_asciiFile.addDelimiter(input->tell(),'|');
1177 input->seek(pos+gDataSize, librevenge::RVNG_SEEK_SET);
1178 return true;
1179 }
1180
1181 bool GreatWksGraph::readPageFrames()
1182 {
1183 MWAWInputStreamPtr &input= m_parserState->m_input;
1184 int const vers=version();
1185 bool isDraw = m_parserState->m_kind==MWAWDocument::MWAW_K_DRAW;
1186 bool isSpreadsheet = m_parserState->m_kind==MWAWDocument::MWAW_K_SPREADSHEET;
1187 bool isDatabase = m_parserState->m_kind==MWAWDocument::MWAW_K_DATABASE;
1188 bool hasPageUnknown=vers==2 && !isDraw && !isDatabase;
1189 int const nZones=hasPageUnknown ? 4 : (vers==2&&!isDatabase) ? 3 : 4;
1190 long pos=input->tell();
1191 if (!isPageFrames()) {
1192 input->seek(pos, librevenge::RVNG_SEEK_SET);
1193 return false;
1194 }
1195 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1196 libmwaw::DebugStream f;
1197 f << "Entries(GFrame):";
1198 input->seek(pos, librevenge::RVNG_SEEK_SET);
1199 long endPos=-1;
1200 GreatWksGraphInternal::Zone pageZone;
1201 if (isSpreadsheet) {
1202 auto val=static_cast<int>(input->readLong(2));
1203 if (val != 0x1c) {
1204 MWAW_DEBUG_MSG(("GreatWksGraph::readPageFrames: oops unexpected key word\n"));
1205 input->seek(pos, librevenge::RVNG_SEEK_SET);
1206 return false;
1207 }
1208 endPos=pos+6+long(input->readULong(4));
1209 }
1210 else if (hasPageUnknown) {
1211 pageZone.m_page = static_cast<int>(input->readLong(2));
1212 f << "page=" << pageZone.m_page << ",";
1213 endPos=pos+6+long(input->readULong(4));
1214 }
1215 static char const *wh[]= {"head", "gstyle", "root", "unknown"};
1216 long zoneSz[4]= {0,0,0,0};
1217 for (int i=0; i < nZones; ++i) {
1218 zoneSz[i] = long(input->readULong(4));
1219 if (zoneSz[i])
1220 f << wh[i] << "[sz]=" << std::hex << zoneSz[i] << std::dec << ",";
1221 }
1222
1223 int z=0;
1224 long zoneEnd=input->tell()+zoneSz[z++];
1225 auto nFrames=static_cast<int>(input->readLong(2));
1226 input->seek(2, librevenge::RVNG_SEEK_CUR);
1227 f << "nFrames=" << nFrames << ",";
1228 ascFile.addPos(pos);
1229 ascFile.addNote(f.str().c_str());
1230 auto &frames=pageZone.m_frameList;
1231 for (int i=0; i < nFrames; ++i) {
1232 pos = input->tell();
1233 f.str("");
1234 f << "GFrame[head]-F" << i+1 << ":";
1235 std::shared_ptr<GreatWksGraphInternal::Frame> zone;
1236 if (pos+0x36<=zoneEnd) zone=readFrameHeader();
1237 if (!zone) {
1238 MWAW_DEBUG_MSG(("GreatWksGraph::readPageFrames: oops graphic detection is probably bad\n"));
1239 f << "###";
1240 input->seek(pos+0x36, librevenge::RVNG_SEEK_SET);
1241 zone.reset(new GreatWksGraphInternal::FrameBad());
1242 }
1243 else
1244 f << *zone;
1245 zone->m_page=pageZone.m_page;
1246 frames.push_back(zone);
1247 ascFile.addPos(pos);
1248 ascFile.addNote(f.str().c_str());
1249 }
1250 pos=input->tell();
1251 if (pos!=zoneEnd) {
1252 ascFile.addPos(pos);
1253 ascFile.addNote("GFrame[head]-end:###");
1254 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
1255 }
1256
1257 pos=input->tell();
1258 zoneEnd=pos+zoneSz[z++];
1259 auto nData=static_cast<int>(input->readLong(2));
1260 input->seek(2, librevenge::RVNG_SEEK_CUR);
1261 f.str("");
1262 f << "Entries(GStyle): N=" << nData << ",";
1263 ascFile.addPos(pos);
1264 ascFile.addNote(f.str().c_str());
1265 int const gDataSize=vers==1 ? 0x34 : 0xaa;
1266 for (int i=0; i < nData; ++i) {
1267 pos = input->tell();
1268 f.str("");
1269 f << "GStyle-S" << i+1 << ":";
1270 MWAWGraphicStyle style;
1271 if (pos+gDataSize>zoneEnd || !readStyle(style))
1272 f << "###";
1273 else
1274 f << style;
1275 pageZone.m_styleList.push_back(style);
1276 ascFile.addPos(pos);
1277 ascFile.addNote(f.str().c_str());
1278 input->seek(pos+gDataSize, librevenge::RVNG_SEEK_SET);
1279 }
1280 pos=input->tell();
1281 if (pos!=zoneEnd) {
1282 ascFile.addPos(pos);
1283 ascFile.addNote("GStyle-end:###");
1284 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
1285 }
1286
1287 if (vers==1) {
1288 pos=input->tell();
1289 zoneEnd=pos+zoneSz[z++];
1290 nData=static_cast<int>(input->readLong(2));
1291 input->seek(2, librevenge::RVNG_SEEK_CUR);
1292 f.str("");
1293 f << "Entries(GLineFormat): N=" << nData << ",";
1294 ascFile.addPos(pos);
1295 ascFile.addNote(f.str().c_str());
1296 int const gLineFSize= 0x1e;
1297 for (int i=0; i < nData; ++i) {
1298 pos = input->tell();
1299 f.str("");
1300 f << "GLineFormat-L" << i << ",";
1301 std::string extra("");
1302 if (!readLineFormat(extra))
1303 f << "###";
1304 else
1305 f << extra;
1306 ascFile.addPos(pos);
1307 ascFile.addNote(f.str().c_str());
1308 input->seek(pos+gLineFSize, librevenge::RVNG_SEEK_SET);
1309 }
1310 pos=input->tell();
1311 if (pos!=zoneEnd) {
1312 ascFile.addPos(pos);
1313 ascFile.addNote("GLineFormat-end:###");
1314 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
1315 }
1316 }
1317
1318 pos = input->tell();
1319 zoneEnd=pos+zoneSz[z++];
1320 auto nRoots=static_cast<int>(input->readLong(2));
1321 input->seek(2, librevenge::RVNG_SEEK_CUR);
1322 f.str("");
1323 f << "GFrame[roots]: N=" << nRoots << ",roots=[";
1324 for (int i=0; i < nRoots; ++i) {
1325 if (input->tell()+2>zoneEnd) {
1326 MWAW_DEBUG_MSG(("GreatWksGraph::readPageFrames: can not read some roots\n"));
1327 f << "###";
1328 break;
1329 }
1330 auto val = static_cast<int>(input->readLong(2));
1331 if (val==0) {
1332 f << "_,";
1333 continue;
1334 }
1335 pageZone.m_rootList.push_back(val);
1336 f << "F" << val << ",";
1337 }
1338 f << "],";
1339 if (input->tell()!=zoneEnd) { // ok
1340 ascFile.addDelimiter(input->tell(),'|');
1341 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
1342 }
1343 ascFile.addPos(pos);
1344 ascFile.addNote(f.str().c_str());
1345
1346 if (isDraw) {
1347 // in draw document, we must sequence the frames recursively ...
1348 // sort rootList using m_order
1349 std::map<int,int> orderMap;
1350 for (auto id : pageZone.m_rootList) {
1351 if (id<=0 || id>nFrames) {
1352 MWAW_DEBUG_MSG(("GreatWksGraph::readPageFrames: can not find order for frame %d\n",id));
1353 continue;
1354 }
1355 int ord=frames[size_t(id-1)]->m_order;
1356 if (orderMap.find(ord)!=orderMap.end()) {
1357 MWAW_DEBUG_MSG(("GreatWksGraph::readPageFrames: oops order %d already exist\n",ord));
1358 continue;
1359 }
1360 orderMap[ord]=id;
1361 }
1362 pageZone.m_rootList.resize(0);
1363 for (auto const &it : orderMap)
1364 pageZone.m_rootList.push_back(it.second);
1365
1366 std::set<int> seens;
1367 bool ok=true;
1368 for (size_t c=pageZone.m_rootList.size(); c > 0;) {
1369 if (!readFrameExtraDataRec(pageZone, pageZone.m_rootList[--c]-1, seens, endPos)) {
1370 ok = false;
1371 break;
1372 }
1373 }
1374 m_state->m_zoneList.push_back(pageZone);
1375 if (endPos>0)
1376 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1377 return ok;
1378 }
1379
1380 // in text document, we must go through the last frame to the first frame to retrieve data
1381 bool ok=true;
1382 for (int id=nFrames-1; id >= 0; --id) {
1383 if (id>=nFrames) {
1384 MWAW_DEBUG_MSG(("GreatWksGraph::readPageFrames: can not find frame with id=%d\n",id));
1385 continue;
1386 }
1387 auto zone=frames[size_t(id)];
1388 if (!zone) continue;
1389 pos=input->tell();
1390 ok = readFrameExtraData(*zone, id, endPos);
1391 f.str("");
1392 f << "GFrame-data:F" << id+1 << "[" << *zone << "]:";
1393 if (zone->m_dataSize>0 && input->tell()!=pos+zone->m_dataSize) {
1394 if (input->tell()>pos+zone->m_dataSize || !input->checkPosition(pos+zone->m_dataSize)) {
1395 MWAW_DEBUG_MSG(("GreatWksGraph::readPageFrames: must stop, file position seems bad\n"));
1396 f << "###";
1397 ascFile.addPos(pos);
1398 ascFile.addNote(f.str().c_str());
1399 ok = false;
1400 break;
1401 }
1402 if (!ok) {
1403 f << "[##unparsed]";
1404 ascFile.addPos(pos);
1405 ascFile.addNote(f.str().c_str());
1406 }
1407 input->seek(pos+zone->m_dataSize, librevenge::RVNG_SEEK_SET);
1408 ok = true;
1409 }
1410 if (ok)
1411 continue;
1412 MWAW_DEBUG_MSG(("GreatWksGraph::readPageFrames: must stop parsing graphic data\n"));
1413 f << "###";
1414 ascFile.addPos(pos);
1415 ascFile.addNote(f.str().c_str());
1416 break;
1417 }
1418 // check the roots
1419 std::set<int> seens;
1420 for (auto &id : pageZone.m_rootList) {
1421 if (!checkGraph(pageZone, id-1, seens))
1422 id=0; // set the root has invalid
1423 }
1424 pos=input->tell();
1425 if (endPos>0 && pos!=endPos) {
1426 MWAW_DEBUG_MSG(("GreatWksGraph::readPageFrames: find some end data\n"));
1427 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1428 ascFile.addPos(pos);
1429 ascFile.addNote("GFrame-end:###");
1430 ok=true;
1431 }
1432 m_state->m_zoneList.push_back(pageZone);
1433 return ok;
1434 }
1435
1436 bool GreatWksGraph::checkGraph(GreatWksGraphInternal::Zone &zone, int id, std::set<int> &seens)
1437 {
1438 if (seens.find(id)!=seens.end()) {
1439 MWAW_DEBUG_MSG(("GreatWksGraph::checkGraph: index %d is already read\n", id));
1440 return false;
1441 }
1442 if (id < 0 || id >= int(zone.m_frameList.size())) {
1443 MWAW_DEBUG_MSG(("GreatWksGraph::readPageFrames: can not find zone %d\n", id));
1444 return false;
1445 }
1446 seens.insert(id);
1447 auto frame=zone.m_frameList[size_t(id)];
1448 if (!frame) return true;
1449 if (frame->getType()!=GreatWksGraphInternal::Frame::T_GROUP)
1450 return true;
1451 auto &group=static_cast<GreatWksGraphInternal::FrameGroup &>(*frame);
1452 for (size_t c=0; c < group.m_childList.size(); ++c) {
1453 if (!checkGraph(zone, group.m_childList[c]-1, seens)) {
1454 group.m_childList.resize(c);
1455 break;
1456 }
1457 }
1458 return true;
1459 }
1460
1461 bool GreatWksGraph::readFrameExtraDataRec(GreatWksGraphInternal::Zone &zone, int id, std::set<int> &seens, long endPos)
1462 {
1463 if (seens.find(id)!=seens.end()) {
1464 MWAW_DEBUG_MSG(("GreatWksGraph::readPageFrames: index %d is already read\n", id));
1465 return false;
1466 }
1467 if (id < 0 || id >= int(zone.m_frameList.size())) {
1468 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraDataRec: can not find zone %d\n", id));
1469 return false;
1470 }
1471 seens.insert(id);
1472 auto frame=zone.m_frameList[size_t(id)];
1473 if (!frame) return true;
1474 MWAWInputStreamPtr &input= m_parserState->m_input;
1475 long pos=input->tell();
1476 if (!readFrameExtraData(*frame, id, endPos)) return false;
1477 if (frame->m_dataSize>0 && input->tell()!=pos+frame->m_dataSize) {
1478 if (input->tell()>pos+frame->m_dataSize || !input->checkPosition(pos+frame->m_dataSize)) {
1479 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraDataRec: must stop, file position seems bad\n"));
1480 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1481 ascFile.addPos(pos);
1482 ascFile.addNote("GFrame-Data###");
1483 if (endPos>0)
1484 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1485 return false;
1486 }
1487 input->seek(pos+frame->m_dataSize, librevenge::RVNG_SEEK_SET);
1488 }
1489 if (frame->getType()!=GreatWksGraphInternal::Frame::T_GROUP)
1490 return true;
1491 auto &group=static_cast<GreatWksGraphInternal::FrameGroup &>(*frame);
1492 for (size_t c=0; c < group.m_childList.size(); ++c) {
1493 if (!readFrameExtraDataRec(zone, group.m_childList[c]-1, seens, endPos)) {
1494 group.m_childList.resize(c);
1495 return false;
1496 }
1497 }
1498 return true;
1499 }
1500
1501 std::shared_ptr<GreatWksGraphInternal::Frame> GreatWksGraph::readFrameHeader()
1502 {
1503 int const vers=version();
1504 GreatWksGraphInternal::Frame zone;
1505 std::shared_ptr<GreatWksGraphInternal::Frame> res;
1506 MWAWInputStreamPtr &input= m_parserState->m_input;
1507 long pos=input->tell();
1508 long endPos=pos+54;
1509 if (!input->checkPosition(endPos))
1510 return res;
1511
1512 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1513 libmwaw::DebugStream f;
1514 zone.m_type=static_cast<int>(input->readLong(1));
1515 auto locked=static_cast<int>(input->readLong(1));
1516 if (zone.m_type<0||zone.m_type>16||locked<0||locked>1) {
1517 input->seek(pos, librevenge::RVNG_SEEK_SET);
1518 return res;
1519 }
1520 if (locked) f << "lock,";
1521 float dim[4];
1522 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
1523 if (dim[2]<dim[0] || dim[3]<dim[1]) {
1524 input->seek(pos, librevenge::RVNG_SEEK_SET);
1525 return res;
1526 }
1527 zone.m_box=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
1528 zone.m_styleId=static_cast<int>(input->readULong(2));
1529 zone.m_parent=static_cast<int>(input->readULong(2));
1530 zone.m_order=static_cast<int>(input->readULong(2));
1531 switch (zone.m_type) {
1532 case 1: {
1533 auto textBox = std::make_shared<GreatWksGraphInternal::FrameText>(zone);
1534 res = textBox;
1535 res->m_dataSize=long(input->readULong(4));
1536 auto val=long(input->readULong(2));
1537 if (val&1) textBox->m_flip[0]=true;
1538 if (val&2) textBox->m_flip[1]=true;
1539 val &= 0xFFFC;
1540 if (val) f << "#flip=" << val << ",";
1541 textBox->m_rotate=static_cast<int>(input->readLong(2));
1542 if (vers==2) // checkme odd
1543 textBox->m_rotate = -textBox->m_rotate;
1544 val=input->readLong(2); // 0 or 100
1545 if (val) f << "f1=" << std::hex << val << std::dec << ",";
1546 break;
1547 }
1548 case 11:
1549 res.reset(new GreatWksGraphInternal::FramePicture(zone));
1550 res->m_dataSize=long(input->readULong(4));
1551 break;
1552 case 15: {
1553 auto grp = std::make_shared<GreatWksGraphInternal::FrameGroup>(zone);
1554 res = grp;
1555 grp->m_numChild=static_cast<int>(input->readULong(2));
1556 break;
1557 }
1558 case 2: {
1559 auto graph = std::make_shared<GreatWksGraphInternal::FrameShape>(zone);
1560 res = graph;
1561 if (vers==1) {
1562 graph->m_lineArrow=static_cast<int>(input->readLong(2));
1563 graph->m_lineFormat=static_cast<int>(input->readLong(2));
1564 }
1565 float points[4];
1566 for (auto &pt : points) pt=float(input->readLong(4))/65536;
1567 graph->m_shape=MWAWGraphicShape::line(MWAWVec2f(points[1],points[0]), MWAWVec2f(points[3],points[2]));
1568 break;
1569 }
1570 case 4: {
1571 auto graph = std::make_shared<GreatWksGraphInternal::FrameShape>(zone);
1572 res = graph;
1573 auto roundType = static_cast<int>(input->readLong(2));
1574 auto cornerDim = float(input->readLong(2));
1575 graph->m_shape = MWAWGraphicShape::rectangle(zone.m_box);
1576 switch (roundType) {
1577 case 1: // normal
1578 graph->m_shape.m_cornerWidth[0]=cornerDim > zone.m_box.size()[0] ? zone.m_box.size()[0]/2.f : cornerDim/2.f;
1579 graph->m_shape.m_cornerWidth[1]=cornerDim > zone.m_box.size()[1] ? zone.m_box.size()[1]/2.f : cornerDim/2.f;
1580 break;
1581 case 2:
1582 f << "cornerDim[minWidth/2],";
1583 cornerDim = zone.m_box.size()[0] < zone.m_box.size()[1] ? zone.m_box.size()[0] : zone.m_box.size()[1];
1584 graph->m_shape.m_cornerWidth[0]=graph->m_shape.m_cornerWidth[1]=0.5f*cornerDim;
1585 break;
1586 default:
1587 f << "#type[corner]=" << roundType << "[" << cornerDim << "],";
1588 break;
1589 }
1590 break;
1591 }
1592 case 6: {
1593 auto graph = std::make_shared<GreatWksGraphInternal::FrameShape>(zone);
1594 res = graph;
1595 int fileAngle[2];
1596 for (auto &angle : fileAngle) angle=static_cast<int>(input->readLong(2));
1597 auto type=static_cast<int>(input->readLong(1)); // 0:open, 1: close
1598 if (type==1) f << "closed,";
1599 else if (type) f << "#type=" << type << ",";
1600
1601 int angle[2] = { int(90-fileAngle[0]-fileAngle[1]), int(90-fileAngle[0]) };
1602 if (fileAngle[1]<0) {
1603 angle[0]=int(90-fileAngle[0]);
1604 angle[1]=int(90-fileAngle[0]-fileAngle[1]);
1605 }
1606 else if (fileAngle[1]==360)
1607 angle[0]=int(90-fileAngle[0]-359);
1608 if (angle[1]>360) {
1609 int numLoop=int(angle[1]/360)-1;
1610 angle[0]-=numLoop*360;
1611 angle[1]-=numLoop*360;
1612 while (angle[1] > 360) {
1613 angle[0]-=360;
1614 angle[1]-=360;
1615 }
1616 }
1617 if (angle[0] < -360) {
1618 int numLoop=int(angle[0]/360)+1;
1619 angle[0]-=numLoop*360;
1620 angle[1]-=numLoop*360;
1621 while (angle[0] < -360) {
1622 angle[0]+=360;
1623 angle[1]+=360;
1624 }
1625 }
1626 // we must compute the real bd box
1627 float minVal[2] = { 0, 0 }, maxVal[2] = { 0, 0 };
1628 int limitAngle[2];
1629 for (int i = 0; i < 2; ++i)
1630 limitAngle[i] = (angle[i] < 0) ? int(angle[i]/90)-1 : int(angle[i]/90);
1631 for (int bord = limitAngle[0]; bord <= limitAngle[1]+1; ++bord) {
1632 float ang = (bord == limitAngle[0]) ? float(angle[0]) :
1633 (bord == limitAngle[1]+1) ? float(angle[1]) : float(90 * bord);
1634 ang *= float(M_PI/180.);
1635 float const actVal[2] = { std::cos(ang), -std::sin(ang)};
1636 if (actVal[0] < minVal[0]) minVal[0] = actVal[0];
1637 else if (actVal[0] > maxVal[0]) maxVal[0] = actVal[0];
1638 if (actVal[1] < minVal[1]) minVal[1] = actVal[1];
1639 else if (actVal[1] > maxVal[1]) maxVal[1] = actVal[1];
1640 }
1641 MWAWBox2f circleBox=zone.m_box;
1642 // we have the shape box, we need to reconstruct the circle box
1643 if (maxVal[0]>minVal[0] && maxVal[1]>minVal[1]) {
1644 float const scaling[2]= { (zone.m_box[1][0]-zone.m_box[0][0])/(maxVal[0]-minVal[0]),
1645 (zone.m_box[1][1]-zone.m_box[0][1])/(maxVal[1]-minVal[1])
1646 };
1647 float const constant[2]= { zone.m_box[0][0]-minVal[0] *scaling[0], zone.m_box[0][1]-minVal[1] *scaling[1]};
1648 circleBox=MWAWBox2f(MWAWVec2f(constant[0]-scaling[0], constant[1]-scaling[1]),
1649 MWAWVec2f(constant[0]+scaling[0], constant[1]+scaling[1]));
1650 }
1651 if (type==1)
1652 graph->m_shape = MWAWGraphicShape::pie(zone.m_box, circleBox, MWAWVec2f(float(angle[0]), float(angle[1])));
1653 else
1654 graph->m_shape = MWAWGraphicShape::arc(zone.m_box, circleBox, MWAWVec2f(float(angle[0]), float(angle[1])));
1655 break;
1656 }
1657 case 3: // rect: no data
1658 case 5: { // oval no data
1659 auto graph = std::make_shared<GreatWksGraphInternal::FrameShape>(zone);
1660 MWAWGraphicShape &shape = graph->m_shape;
1661 res = graph;
1662 shape.m_bdBox = shape.m_formBox = zone.m_box;
1663 shape.m_type = zone.m_type==3 ? MWAWGraphicShape::Rectangle : MWAWGraphicShape::Circle;
1664 break;
1665 }
1666 case 10: {
1667 auto field = std::make_shared<GreatWksGraphInternal::FrameDBField>(zone);
1668 res = field;
1669 field->m_dataSize=long(input->readULong(4));
1670 break;
1671 }
1672 case 7:
1673 case 8:
1674 case 12: {
1675 auto graph = std::make_shared<GreatWksGraphInternal::FrameShape>(zone);
1676 res = graph;
1677 graph->m_shape = zone.m_type==12 ? MWAWGraphicShape::path(zone.m_box) : MWAWGraphicShape::polygon(zone.m_box);
1678 graph->m_dataSize=long(input->readULong(4));
1679 break;
1680 }
1681 default:
1682 break;
1683 }
1684 if (!res)
1685 res.reset(new GreatWksGraphInternal::Frame(zone));
1686 res->m_extra=f.str();
1687 ascFile.addDelimiter(input->tell(),'|');
1688 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1689 return res;
1690 }
1691
1692 bool GreatWksGraph::readFrameExtraData(GreatWksGraphInternal::Frame &frame, int id, long endPos)
1693 {
1694 MWAWInputStreamPtr &input= m_parserState->m_input;
1695 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1696 libmwaw::DebugStream f;
1697 f << "GFrame[data]-F" << id+1 << ":";
1698 long pos=input->tell();
1699 switch (frame.m_type) {
1700 case 0:
1701 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: find group with type=0\n"));
1702 return false;
1703 case 1: {
1704 if (frame.getType()!=GreatWksGraphInternal::Frame::T_TEXT) {
1705 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: unexpected type for text\n"));
1706 return false;
1707 }
1708 auto &text=static_cast<GreatWksGraphInternal::FrameText &>(frame);
1709 if (!input->checkPosition(pos+text.m_dataSize)) {
1710 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: text size seems bad\n"));
1711 return false;
1712 }
1713 text.m_entry.setBegin(pos);
1714 text.m_entry.setLength(text.m_dataSize);
1715 input->seek(pos+text.m_dataSize, librevenge::RVNG_SEEK_SET);
1716 ascFile.addPos(pos);
1717 ascFile.addNote(f.str().c_str());
1718 return true;
1719 }
1720 case 10:
1721 input->seek(pos+frame.m_dataSize, librevenge::RVNG_SEEK_SET);
1722 ascFile.addPos(pos);
1723 ascFile.addNote(f.str().c_str());
1724 return true;
1725 case 2:
1726 case 3:
1727 case 4:
1728 case 5:
1729 case 6:
1730 return true;
1731 case 8: // poly normal
1732 case 7: // regular poly
1733 case 12: { // spline
1734 if (frame.getType()!=GreatWksGraphInternal::Frame::T_BASIC) {
1735 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: unexpected type for basic graph\n"));
1736 return false;
1737 }
1738 auto nPt=static_cast<int>(input->readLong(2));
1739 long endData=pos+10+8*nPt;
1740 if (pos+frame.m_dataSize > endData)
1741 endData=pos+frame.m_dataSize;
1742 if (nPt<0 || (endPos>0 && endData>endPos) ||
1743 (endPos<0 && !input->checkPosition(endData)))
1744 return false;
1745 float dim[4];
1746 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
1747 f << "dim=" << dim[1] << "x" << dim[0] << "<->"
1748 << dim[3] << "x" << dim[2] << ",";
1749 f << "pt=[";
1750 auto &graph=static_cast<GreatWksGraphInternal::FrameShape &>(frame);
1751 float pt[2];
1752 std::vector<MWAWVec2f> vertices;
1753 for (int p=0; p<nPt; ++p) {
1754 for (auto &i: pt) i=float(input->readLong(4))/65536.f;
1755 vertices.push_back(MWAWVec2f(pt[1],pt[0]));
1756 f << pt[1] << "x" << pt[0] << ",";
1757 }
1758 f << "],";
1759 if (graph.m_shape.m_type == MWAWGraphicShape::Polygon)
1760 graph.m_shape.m_vertices = vertices;
1761 else if (graph.m_shape.m_type == MWAWGraphicShape::Path) {
1762 if (nPt<4 || (nPt%3)!=1) {
1763 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: the spline number of points seems bad\n"));
1764 f << "###";
1765 }
1766 else {
1767 std::vector<MWAWGraphicShape::PathData> &path=graph.m_shape.m_path;
1768 path.push_back(MWAWGraphicShape::PathData('M', vertices[0]));
1769
1770 for (size_t p=1; p < size_t(nPt); p+=3) {
1771 bool hasFirstC=vertices[p-1]!=vertices[p];
1772 bool hasSecondC=vertices[p+1]!=vertices[p+2];
1773 if (!hasFirstC && !hasSecondC)
1774 path.push_back(MWAWGraphicShape::PathData('L', vertices[p+2]));
1775 else if (hasFirstC)
1776 path.push_back(MWAWGraphicShape::PathData('C', vertices[p+2], vertices[p+1], vertices[p+1]));
1777 else
1778 path.push_back(MWAWGraphicShape::PathData('S', vertices[p+2], vertices[p+1]));
1779 }
1780 path.push_back(MWAWGraphicShape::PathData('Z'));
1781 }
1782 }
1783 else {
1784 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: find unexpected vertices\n"));
1785 f << "###";
1786 }
1787 if (input->tell()!=endData) {
1788 ascFile.addDelimiter(input->tell(),'|');
1789 input->seek(endData,librevenge::RVNG_SEEK_SET);
1790 }
1791 ascFile.addPos(pos);
1792 ascFile.addNote(f.str().c_str());
1793 return true;
1794 }
1795 case 11: {
1796 if (frame.getType()!=GreatWksGraphInternal::Frame::T_PICTURE) {
1797 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: unexpected type for picture\n"));
1798 return false;
1799 }
1800 auto &pict=static_cast<GreatWksGraphInternal::FramePicture &>(frame);
1801 if (!input->checkPosition(pos+pict.m_dataSize)) {
1802 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: picture size seems bad\n"));
1803 return false;
1804 }
1805 pict.m_entry.setBegin(pos);
1806 pict.m_entry.setLength(pict.m_dataSize);
1807 input->seek(pos+pict.m_dataSize, librevenge::RVNG_SEEK_SET);
1808 return true;
1809 }
1810 case 15: {
1811 auto nGrp=static_cast<int>(input->readLong(2));
1812 if (nGrp<0 || (endPos>0 && pos+4+2*nGrp>endPos) ||
1813 (endPos<0 && !input->checkPosition(pos+4+2*nGrp))) {
1814 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: unexpected number of group\n"));
1815 return false;
1816 }
1817
1818 if (frame.getType()!=GreatWksGraphInternal::Frame::T_GROUP) {
1819 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: unexpected type for group\n"));
1820 input->seek(pos+4+2*nGrp, librevenge::RVNG_SEEK_SET);
1821 f << "###[internal]";
1822 return false;
1823 }
1824 auto &group=static_cast<GreatWksGraphInternal::FrameGroup &>(frame);
1825 if (nGrp != group.m_numChild) {
1826 f << "###[N=" << group.m_numChild << "]";
1827 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: unexpected number of group child\n"));
1828 }
1829 auto val=static_cast<int>(input->readLong(2)); // always 2
1830 if (val!=2) f << "f0=" << val << ",";
1831 f << "grpId=[";
1832 for (int j=0; j < nGrp; ++j) {
1833 val = static_cast<int>(input->readLong(2));
1834 group.m_childList.push_back(val);
1835 f << val << ",";
1836 }
1837 f << "],";
1838 ascFile.addPos(pos);
1839 ascFile.addNote(f.str().c_str());
1840 return true;
1841 }
1842 default:
1843 break;
1844 }
1845 MWAW_DEBUG_MSG(("GreatWksGraph::readFrameExtraData: unexpected type\n"));
1846 return false;
1847 }
1848
1849 ////////////////////////////////////////////////////////////
1850 // textbox
1851 ////////////////////////////////////////////////////////////
1852 bool GreatWksGraph::sendTextbox(GreatWksGraphInternal::FrameText const &text, GreatWksGraphInternal::Zone const &zone, MWAWPosition const &pos)
1853 {
1854 MWAWListenerPtr listener=m_parserState->getMainListener();
1855 if (!listener) {
1856 MWAW_DEBUG_MSG(("GreatWksGraph::sendTextbox: can not find the listener\n"));
1857 return true;
1858 }
1859 MWAWGraphicStyle style;
1860 if (text.m_styleId>=1 && text.m_styleId <= int(zone.m_styleList.size()))
1861 style = zone.m_styleList[size_t(text.m_styleId-1)];
1862 MWAWVec2f fSz=pos.size();
1863 // increase slightly x and set y to atleast
1864 MWAWVec2f newSz(fSz[0]+3,fSz[1]);
1865 if (listener->getType()==MWAWListener::Graphic)
1866 return sendTextboxAsGraphic(MWAWBox2f(pos.origin(),pos.origin()+newSz), text, style, listener);
1867
1868 MWAWPosition finalPos(pos);
1869 finalPos.setSize(MWAWVec2f(newSz[0],-newSz[1]));
1870 if ((text.hasTransform() || style.hasPattern() || style.hasGradient()) &&
1871 m_document.canSendTextboxAsGraphic(text.m_entry)) {
1872 MWAWBox2f box(MWAWVec2f(0,0),newSz);
1873 MWAWGraphicEncoder graphicEncoder;
1874 MWAWGraphicListenerPtr graphicListener(new MWAWGraphicListener(*m_parserState, box, &graphicEncoder));
1875 graphicListener->startDocument();
1876 bool ok=sendTextboxAsGraphic(box, text, style, graphicListener);
1877 graphicListener->endDocument();
1878 MWAWEmbeddedObject picture;
1879 if (!graphicEncoder.getBinaryResult(picture) || !ok)
1880 return false;
1881 listener->insertPicture(finalPos, picture);
1882 return true;
1883 }
1884
1885 std::shared_ptr<MWAWSubDocument> doc(new GreatWksGraphInternal::SubDocument(*this, m_parserState->m_input, text.m_entry));
1886 MWAWGraphicStyle frameStyle;
1887 if (style.hasSurfaceColor())
1888 frameStyle.setBackgroundColor(style.m_surfaceColor);
1889 listener->insertTextBox(finalPos, doc, frameStyle);
1890 return true;
1891 }
1892
1893 bool GreatWksGraph::sendTextboxAsGraphic(MWAWBox2f const &box, GreatWksGraphInternal::FrameText const &text,
1894 MWAWGraphicStyle const &style, MWAWListenerPtr listener)
1895 {
1896 libmwaw::SubDocumentType subdocType;
1897 if (!listener || !listener->isDocumentStarted() || listener->isSubDocumentOpened(subdocType)) {
1898 MWAW_DEBUG_MSG(("GreatWksGraph::sendTextboxAsGraphic: unexpected graphic state\n"));
1899 return false;
1900 }
1901 std::shared_ptr<MWAWSubDocument> doc(new GreatWksGraphInternal::SubDocument(*this, m_parserState->m_input, text.m_entry));
1902
1903 MWAWVec2f fSz=box.size();
1904 MWAWBox2f textBox=MWAWBox2f(box[0],box[0]+MWAWVec2f(fSz[0],-fSz[1]));
1905 /* rotation are multiple of 90, so we can use the inverse rotation to find
1906 the original box */
1907 if (text.m_rotate)
1908 textBox=libmwaw::rotateBoxFromCenter(box, -float(text.m_rotate));
1909 MWAWPosition textPos(textBox[0], textBox.size(), librevenge::RVNG_POINT);
1910 textPos.m_anchorTo=MWAWPosition::Page;
1911 textPos.m_wrapping = MWAWPosition::WBackground;
1912 listener->insertTextBox(textPos, doc, text.getStyle(style));
1913 return true;
1914 }
1915
1916 ////////////////////////////////////////////////////////////
1917 // picture
1918 ////////////////////////////////////////////////////////////
1919 bool GreatWksGraph::sendPicture(MWAWEntry const &entry, MWAWPosition const &pos)
1920 {
1921 MWAWListenerPtr listener=m_parserState->getMainListener();
1922 if (!listener) {
1923 MWAW_DEBUG_MSG(("GreatWksGraph::sendPicture: can not find the listener\n"));
1924 return true;
1925 }
1926 if (!entry.valid()) {
1927 MWAW_DEBUG_MSG(("GreatWksGraph::sendPicture: can not find the entry\n"));
1928 return false;
1929 }
1930 entry.setParsed(true);
1931 MWAWInputStreamPtr &input= m_parserState->m_input;
1932 long actPos = input->tell();
1933
1934 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1935 std::shared_ptr<MWAWPict> thePict(MWAWPictData::get(input, static_cast<int>(entry.length())));
1936 MWAWEmbeddedObject picture;
1937 if (thePict && thePict->getBinary(picture))
1938 listener->insertPicture(pos, picture);
1939
1940 #ifdef DEBUG_WITH_FILES
1941 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1942 ascFile.skipZone(entry.begin(), entry.end()-1);
1943 librevenge::RVNGBinaryData file;
1944 input->seek(entry.begin(),librevenge::RVNG_SEEK_SET);
1945 input->readDataBlock(entry.length(), file);
1946
1947 static int volatile pictName = 0;
1948 libmwaw::DebugStream f;
1949 f << "DATA-" << ++pictName << ".pct";
1950 libmwaw::Debug::dumpFile(file, f.str().c_str());
1951 #endif
1952
1953 input->seek(actPos, librevenge::RVNG_SEEK_SET);
1954 return true;
1955 }
1956
1957 ////////////////////////////////////////////////////////////
1958 // group
1959 ////////////////////////////////////////////////////////////
1960 bool GreatWksGraph::sendGroup(GreatWksGraphInternal::FrameGroup const &group, GreatWksGraphInternal::Zone const &zone, MWAWPosition const &pos)
1961 {
1962 sendGroupChild(group,zone,pos);
1963 return true;
1964 }
1965
1966 bool GreatWksGraph::canCreateGraphic(GreatWksGraphInternal::FrameGroup const &group, GreatWksGraphInternal::Zone const &zone)
1967 {
1968 if (group.m_childList.empty())
1969 return true;
1970 auto numFrames=static_cast<int>(zone.m_frameList.size());
1971 int page=group.m_page;
1972 for (auto childId : group.m_childList) {
1973 if (childId<=0 || childId>int(numFrames)) continue;
1974 std::shared_ptr<GreatWksGraphInternal::Frame> frame=zone.m_frameList[size_t(childId-1)];
1975 if (!frame) continue;
1976 if (frame->m_page!=page) return false;
1977 switch (frame->getType()) {
1978 case GreatWksGraphInternal::Frame::T_BASIC:
1979 break;
1980 case GreatWksGraphInternal::Frame::T_DBFIELD:
1981 case GreatWksGraphInternal::Frame::T_PICTURE:
1982 return false;
1983 case GreatWksGraphInternal::Frame::T_GROUP:
1984 if (!canCreateGraphic(static_cast<GreatWksGraphInternal::FrameGroup const &>(*frame), zone))
1985 return false;
1986 break;
1987 case GreatWksGraphInternal::Frame::T_TEXT: {
1988 auto const &text=static_cast<GreatWksGraphInternal::FrameText const &>(*frame);
1989 if (!m_document.canSendTextboxAsGraphic(text.m_entry))
1990 return false;
1991 break;
1992 }
1993 case GreatWksGraphInternal::Frame::T_BAD:
1994 case GreatWksGraphInternal::Frame::T_UNSET:
1995 #if !defined(__clang__)
1996 default:
1997 #endif
1998 break;
1999 }
2000 }
2001 return true;
2002 }
2003
2004 void GreatWksGraph::sendGroup(GreatWksGraphInternal::FrameGroup const &group, GreatWksGraphInternal::Zone const &zone, MWAWGraphicListenerPtr &listener)
2005 {
2006 if (!listener || group.m_childList.empty()) return;
2007 auto numFrames=static_cast<int>(zone.m_frameList.size());
2008 for (auto childId : group.m_childList) {
2009 if (childId<=0 || childId>int(numFrames)) continue;
2010 auto frame=zone.m_frameList[size_t(childId-1)];
2011 if (!frame) continue;
2012
2013 MWAWBox2f const &box=frame->m_box;
2014 MWAWGraphicStyle style;
2015 if (frame->m_styleId>=1 && frame->m_styleId <= int(zone.m_styleList.size()))
2016 style = zone.m_styleList[size_t(frame->m_styleId-1)];
2017 switch (frame->getType()) {
2018 case GreatWksGraphInternal::Frame::T_BASIC: {
2019 auto const &shape=static_cast<GreatWksGraphInternal::FrameShape const &>(*frame);
2020 shape.updateStyle(style);
2021 MWAWPosition shapePos(box[0], box.size(), librevenge::RVNG_POINT);
2022 shapePos.m_anchorTo=MWAWPosition::Page;
2023 listener->insertShape(shapePos, shape.m_shape, style);
2024 break;
2025 }
2026 case GreatWksGraphInternal::Frame::T_GROUP:
2027 sendGroup(static_cast<GreatWksGraphInternal::FrameGroup const &>(*frame), zone,listener);
2028 break;
2029 case GreatWksGraphInternal::Frame::T_TEXT:
2030 sendTextboxAsGraphic(MWAWBox2f(box[0],box[1]+MWAWVec2f(3,0)),
2031 static_cast<GreatWksGraphInternal::FrameText const &>(*frame), style, listener);
2032 break;
2033 case GreatWksGraphInternal::Frame::T_DBFIELD:
2034 case GreatWksGraphInternal::Frame::T_PICTURE:
2035 case GreatWksGraphInternal::Frame::T_BAD:
2036 case GreatWksGraphInternal::Frame::T_UNSET:
2037 #if !defined(__clang__)
2038 default:
2039 #endif
2040 break;
2041 }
2042 }
2043 }
2044
2045 void GreatWksGraph::sendGroupChild(GreatWksGraphInternal::FrameGroup const &group, GreatWksGraphInternal::Zone const &zone, MWAWPosition const &pos)
2046 {
2047 MWAWListenerPtr listener=m_parserState->getMainListener();
2048 if (!listener) {
2049 MWAW_DEBUG_MSG(("GreatWksGraph::sendGroupChild: can not find the listeners\n"));
2050 return;
2051 }
2052 size_t numChilds=group.m_childList.size(), childNotSent=0;
2053 if (!numChilds) return;
2054
2055 bool isDraw=listener->getType()==MWAWListener::Graphic;
2056 int numDataToMerge=0;
2057 auto numFrames=static_cast<int>(zone.m_frameList.size());
2058 MWAWBox2f partialBdBox;
2059 MWAWPosition partialPos(pos);
2060
2061 for (size_t c=0; c<numChilds; ++c) {
2062 int childId=group.m_childList[c];
2063 if (childId<=0 || childId>int(numFrames)) continue;
2064 std::shared_ptr<GreatWksGraphInternal::Frame> frame=zone.m_frameList[size_t(childId-1)];
2065 if (!frame) continue;
2066
2067 bool canMerge=false;
2068 if (!isDraw && frame->m_page==group.m_page) {
2069 switch (frame->getType()) {
2070 case GreatWksGraphInternal::Frame::T_BASIC:
2071 canMerge=true;
2072 break;
2073 case GreatWksGraphInternal::Frame::T_GROUP:
2074 canMerge=canCreateGraphic(static_cast<GreatWksGraphInternal::FrameGroup const &>(*frame), zone);
2075 break;
2076 case GreatWksGraphInternal::Frame::T_TEXT: {
2077 auto const &text=static_cast<GreatWksGraphInternal::FrameText const &>(*frame);
2078 canMerge=m_document.canSendTextboxAsGraphic(text.m_entry);
2079 break;
2080 }
2081 case GreatWksGraphInternal::Frame::T_DBFIELD:
2082 case GreatWksGraphInternal::Frame::T_PICTURE:
2083 case GreatWksGraphInternal::Frame::T_BAD:
2084 case GreatWksGraphInternal::Frame::T_UNSET:
2085 #if !defined(__clang__)
2086 default:
2087 #endif
2088 break;
2089 }
2090 }
2091 MWAWBox2f box=frame->m_box;
2092 bool isLast=false;
2093 if (canMerge) {
2094 if (numDataToMerge == 0)
2095 partialBdBox=box;
2096 else
2097 partialBdBox=partialBdBox.getUnion(box);
2098 ++numDataToMerge;
2099 if (c+1 < numChilds)
2100 continue;
2101 isLast=true;
2102 }
2103
2104 if (numDataToMerge>1) {
2105 MWAWGraphicEncoder graphicEncoder;
2106 MWAWGraphicListenerPtr graphicListener(new MWAWGraphicListener(*m_parserState, partialBdBox, &graphicEncoder));
2107 graphicListener->startDocument();
2108 size_t lastChild = isLast ? c : c-1;
2109 for (size_t ch=childNotSent; ch <= lastChild; ++ch) {
2110 int localCId = group.m_childList[ch];
2111 if (localCId<=0 || localCId>int(numFrames)) continue;
2112 auto child=zone.m_frameList[size_t(localCId-1)];
2113 if (!child) continue;
2114
2115 box=child->m_box;
2116 MWAWGraphicStyle style;
2117 if (child->m_styleId>=1 && child->m_styleId <= int(zone.m_styleList.size()))
2118 style = zone.m_styleList[size_t(child->m_styleId-1)];
2119 switch (child->getType()) {
2120 case GreatWksGraphInternal::Frame::T_BASIC: {
2121 auto const &shape=static_cast<GreatWksGraphInternal::FrameShape const &>(*child);
2122 shape.updateStyle(style);
2123 MWAWPosition shapePos(box[0], box.size(), librevenge::RVNG_POINT);
2124 shapePos.m_anchorTo=MWAWPosition::Page;
2125 graphicListener->insertShape(shapePos, shape.m_shape, style);
2126 break;
2127 }
2128 case GreatWksGraphInternal::Frame::T_GROUP:
2129 sendGroup(static_cast<GreatWksGraphInternal::FrameGroup const &>(*child), zone,graphicListener);
2130 break;
2131 case GreatWksGraphInternal::Frame::T_TEXT:
2132 sendTextboxAsGraphic(MWAWBox2f(box[0],box[1]+MWAWVec2f(3,0)),
2133 static_cast<GreatWksGraphInternal::FrameText const &>(*child), style, graphicListener);
2134 break;
2135 case GreatWksGraphInternal::Frame::T_DBFIELD:
2136 case GreatWksGraphInternal::Frame::T_PICTURE:
2137 case GreatWksGraphInternal::Frame::T_BAD:
2138 case GreatWksGraphInternal::Frame::T_UNSET:
2139 #if !defined(__clang__)
2140 default:
2141 #endif
2142 break;
2143 }
2144 }
2145 graphicListener->endDocument();
2146 MWAWEmbeddedObject picture;
2147 if (graphicEncoder.getBinaryResult(picture)) {
2148 partialPos.setOrigin(pos.origin()+partialBdBox[0]-group.m_box[0]);
2149 partialPos.setSize(partialBdBox.size());
2150 listener->insertPicture(partialPos, picture);
2151 if (isLast)
2152 break;
2153 childNotSent=c;
2154 }
2155 }
2156
2157 // time to send back the data
2158 for (; childNotSent <= c; ++childNotSent) {
2159 int localCId=group.m_childList[childNotSent];
2160 if (localCId<=0 || localCId>int(numFrames)) continue;
2161 auto child=zone.m_frameList[size_t(localCId-1)];
2162 if (!child) continue;
2163 sendFrame(child, zone);
2164 }
2165 numDataToMerge=0;
2166 }
2167 }
2168
2169
2170 ////////////////////////////////////////////////////////////
2171 // send data to a listener
2172 ////////////////////////////////////////////////////////////
2173
2174 ////////////////////////////////////////////////////////////
2175 // send data
2176 ////////////////////////////////////////////////////////////
2177 bool GreatWksGraph::sendShape(GreatWksGraphInternal::FrameShape const &graph, GreatWksGraphInternal::Zone const &zone, MWAWPosition const &pos)
2178 {
2179 MWAWListenerPtr listener=m_parserState->getMainListener();
2180 if (!listener) {
2181 MWAW_DEBUG_MSG(("GreatWksGraph::sendShape: can not find a listener\n"));
2182 return false;
2183 }
2184 MWAWGraphicStyle style;
2185 if (graph.m_styleId>=1 && graph.m_styleId <= int(zone.m_styleList.size()))
2186 style = zone.m_styleList[size_t(graph.m_styleId-1)];
2187 graph.updateStyle(style);
2188 MWAWPosition finalPos(pos);
2189 finalPos.setOrigin(pos.origin()-MWAWVec2f(2,2));
2190 finalPos.setSize(pos.size()+MWAWVec2f(4,4));
2191 listener->insertShape(finalPos,graph.m_shape, style);
2192 return true;
2193 }
2194
2195 bool GreatWksGraph::sendFrame(std::shared_ptr<GreatWksGraphInternal::Frame> frame, GreatWksGraphInternal::Zone const &zone)
2196 {
2197 MWAWListenerPtr listener=m_parserState->getMainListener();
2198 if (!listener || !frame) {
2199 MWAW_DEBUG_MSG(("GreatWksGraph::sendFrame: can not find a listener\n"));
2200 return false;
2201 }
2202 frame->m_parsed=true;
2203 MWAWInputStreamPtr &input= m_parserState->m_input;
2204 long pos=input->tell();
2205 MWAWVec2f LTPos(0,0);
2206 if (m_parserState->m_kind==MWAWDocument::MWAW_K_DRAW)
2207 LTPos=72.0f*MWAWVec2f(float(m_mainParser->getPageSpan().getMarginLeft()), float(m_mainParser->getPageSpan().getMarginTop()));
2208 MWAWPosition fPos(frame->m_box[0]+LTPos,frame->m_box.size(),librevenge::RVNG_POINT);
2209 fPos.setRelativePosition(MWAWPosition::Page);
2210 fPos.setPage(frame->m_page<0 ? 1: frame->m_page);
2211 fPos.m_wrapping = MWAWPosition::WBackground;
2212 bool ok=true;
2213 switch (frame->getType()) {
2214 case GreatWksGraphInternal::Frame::T_BASIC:
2215 ok = sendShape(static_cast<GreatWksGraphInternal::FrameShape const &>(*frame), zone, fPos);
2216 break;
2217 case GreatWksGraphInternal::Frame::T_PICTURE:
2218 ok = sendPicture(static_cast<GreatWksGraphInternal::FramePicture const &>(*frame).m_entry, fPos);
2219 break;
2220 case GreatWksGraphInternal::Frame::T_GROUP:
2221 ok = sendGroup(static_cast<GreatWksGraphInternal::FrameGroup const &>(*frame), zone, fPos);
2222 break;
2223 case GreatWksGraphInternal::Frame::T_TEXT:
2224 ok = sendTextbox(static_cast<GreatWksGraphInternal::FrameText const &>(*frame), zone, fPos);
2225 break;
2226 case GreatWksGraphInternal::Frame::T_DBFIELD:
2227 // do me
2228 case GreatWksGraphInternal::Frame::T_BAD:
2229 case GreatWksGraphInternal::Frame::T_UNSET:
2230 #if !defined(__clang__)
2231 default:
2232 #endif
2233 ok=false;
2234 }
2235 input->seek(pos, librevenge::RVNG_SEEK_SET);
2236 return ok;
2237 }
2238
2239 bool GreatWksGraph::sendPageFrames(GreatWksGraphInternal::Zone const &zone)
2240 {
2241 MWAWListenerPtr listener=m_parserState->getMainListener();
2242 if (!listener) {
2243 MWAW_DEBUG_MSG(("GreatWksGraph::sendPageFrames: can not find a listener\n"));
2244 return false;
2245 }
2246 zone.m_parsed=true;
2247 for (auto id : zone.m_rootList) {
2248 --id;
2249 if (id<0 || !zone.m_frameList[size_t(id)])
2250 continue;
2251 auto frame=zone.m_frameList[size_t(id)];
2252 if (frame->m_parsed)
2253 continue;
2254 sendFrame(frame,zone);
2255 }
2256 return true;
2257 }
2258
2259 bool GreatWksGraph::sendPageGraphics()
2260 {
2261 MWAWListenerPtr listener=m_parserState->getMainListener();
2262 if (!listener) {
2263 MWAW_DEBUG_MSG(("GreatWksGraph::sendPageGraphics: can not find a listener\n"));
2264 return false;
2265 }
2266 for (auto const &zone : m_state->m_zoneList) {
2267 if (zone.m_parsed)
2268 continue;
2269 sendPageFrames(zone);
2270 }
2271 return true;
2272 }
2273
2274 void GreatWksGraph::flushExtra()
2275 {
2276 sendPageGraphics();
2277 }
2278 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: