Debian Codegraph @

src/lib/MacDrawProParser.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 "MWAWFontConverter.hxx"
45 #include "MWAWGraphicListener.hxx"
46 #include "MWAWGraphicShape.hxx"
47 #include "MWAWGraphicStyle.hxx"
48 #include "MWAWHeader.hxx"
49 #include "MWAWParagraph.hxx"
50 #include "MWAWPictBitmap.hxx"
51 #include "MWAWPictData.hxx"
52 #include "MWAWPrinter.hxx"
53 #include "MWAWPosition.hxx"
54 #include "MWAWRSRCParser.hxx"
55 #include "MWAWSubDocument.hxx"
56
57 #include "MacDrawProStyleManager.hxx"
58
59 #include "MacDrawProParser.hxx"
60
61 /** Internal: the structures of a MacDrawProParser */
62 namespace MacDrawProParserInternal
63 {
64 // generic class used to defined a layer
65 struct Layer {
66 //! constructor
67 Layer()
68 : m_numShapes(0)
69 , m_firstShape(-1)
70 , m_isHidden(false)
71 , m_box()
72 , m_libraryToObjectMap()
73 , m_name("")
74 {
75 for (auto &n : m_N) n=0;
76 }
77 //! the number of shape
78 int m_numShapes;
79 //! the first shape
80 int m_firstShape;
81 //! true if the layer is hidden
82 bool m_isHidden;
83 //! the layer bounding box (if computed)
84 MWAWBox2f m_box;
85 //! map library to pos
86 std::map<int, int> m_libraryToObjectMap;
87 //! some unknown number find in the beginning of the header
88 long m_N[3];
89 //! the layer name
90 librevenge::RVNGString m_name;
91 };
92
93 // generic class used to defined a library
94 struct Library {
95 //! constructor
96 explicit Library(int id=-1)
97 : m_id(id)
98 , m_layerList()
99 , m_box()
100 , m_name("")
101 {
102 }
103 //! the library id
104 int m_id;
105 //! the list of layer id
106 std::vector<int> m_layerList;
107 //! the bounding box (if computed)
108 MWAWBox2f m_box;
109 //! the library name
110 librevenge::RVNGString m_name;
111 };
112
113 // generic class used to store shape in MWAWDrawParser
114 struct Shape {
115 //! the different shape
116 enum Type { Basic, Bitmap, Group, GroupEnd, Note, Text, Unknown };
117
118 //! constructor
119 Shape()
120 : m_type(Unknown)
121 , m_fileType(0)
122 , m_box()
123 , m_style()
124 , m_shape()
125 , m_id(-1)
126 , m_nextId(-1)
127 , m_flags(0)
128 , m_textZoneId(-1)
129 , m_numChars(0)
130 , m_fontMap()
131 , m_lineBreakSet()
132 , m_paragraphMap()
133 , m_paragraph()
134 , m_childList()
135 , m_measureBox()
136 , m_measureEntry()
137 , m_numBytesByRow(0)
138 , m_bitmapIsColor(false)
139 , m_bitmapDim()
140 , m_bitmapFileDim()
141 , m_bitmapEntry()
142 , m_bitmapClutId(0)
143 , m_isSent(false)
144 {
145 }
146
147 //! basic operator<<
148 friend std::ostream &operator<<(std::ostream &o, Shape const &shape);
149
150 //! return the shape bdbox
151 MWAWBox2f getBdBox() const
152 {
153 if (m_type==Basic)
154 return m_shape.getBdBox();
155 if (m_style.m_rotate>=0 && m_style.m_rotate<=0) return m_box;
156 return libmwaw::rotateBoxFromCenter(m_box,m_style.m_rotate);
157 }
158 //! returns true if the object is a line
159 bool isLine() const
160 {
161 return m_type==Basic && m_shape.m_type==MWAWGraphicShape::Line;
162 }
163 //! the graphic type
164 Type m_type;
165 //! the file type
166 int m_fileType;
167 //! the shape bdbox
168 MWAWBox2f m_box;
169 //! the graphic style
170 MWAWGraphicStyle m_style;
171 //! the graphic shape ( for basic geometric form )
172 MWAWGraphicShape m_shape;
173 //! the shape id
174 int m_id;
175 //! the following id (if set)
176 int m_nextId;
177 //! the main shape flag
178 int m_flags;
179
180 // text box or note
181
182 //! the text zone ( for a text box or a note)
183 int m_textZoneId;
184 //! the number of caracters ( for a text box or a note)
185 int m_numChars;
186 //! a map position to font id ( for a text box or a note)
187 std::map<int,int> m_fontMap;
188 //! the list of line break position ( for a text box or a note)
189 std::set<int> m_lineBreakSet;
190 //! the paragraphMap ( for a text box or a note) and a Pro file
191 std::map<int,int> m_paragraphMap;
192 //! the paragraph ( for a text box or a note) and a II file
193 MWAWParagraph m_paragraph;
194
195 // group
196
197 //! the child list ( for a group )
198 std::vector<size_t> m_childList;
199
200 // line
201 // the measure box ( for a line)
202 MWAWBox2f m_measureBox;
203 // the measure message ( for a line )
204 MWAWEntry m_measureEntry;
205
206 // bitmap
207
208 //! the number of bytes by row (for a bitmap)
209 int m_numBytesByRow;
210 //! true if the bitmap is a color bitmap
211 bool m_bitmapIsColor;
212 //! the bitmap dimension (in page)
213 MWAWBox2i m_bitmapDim;
214 //! the bitmap dimension (in the file)
215 MWAWBox2i m_bitmapFileDim;
216 //! the bitmap entry (data)
217 MWAWEntry m_bitmapEntry;
218 //! the bitmap clut rsrc id
219 int m_bitmapClutId;
220 //! a flag used to know if the object is sent to the listener or not
221 mutable bool m_isSent;
222 };
223
224
225 std::ostream &operator<<(std::ostream &o, Shape const &shape)
226 {
227 o << "O" << shape.m_id << "[";
228 switch (shape.m_type) {
229 case Shape::Basic:
230 switch (shape.m_shape.m_type) {
231 case MWAWGraphicShape::Line:
232 o << "line,";
233 break;
234 case MWAWGraphicShape::Measure:
235 o << "measure,";
236 break;
237 case MWAWGraphicShape::Rectangle:
238 o << "rect,";
239 break;
240 case MWAWGraphicShape::Circle:
241 o << "circle,";
242 break;
243 case MWAWGraphicShape::Arc:
244 o << "arc,";
245 break;
246 case MWAWGraphicShape::Pie:
247 o << "pie,";
248 break;
249 case MWAWGraphicShape::Polygon:
250 o << "polygon,";
251 break;
252 case MWAWGraphicShape::Polyline:
253 o << "polyline,";
254 break;
255 case MWAWGraphicShape::Path:
256 o << "spline,";
257 break;
258 case MWAWGraphicShape::ShapeUnknown:
259 #if !defined(__clang__)
260 default:
261 #endif
262 o << "###unknown[shape],";
263 break;
264 }
265 break;
266 case Shape::Bitmap:
267 o << "bitmap,";
268 break;
269 case Shape::Group:
270 o << "group,";
271 break;
272 case Shape::GroupEnd:
273 o << "group[end],";
274 break;
275 case Shape::Note:
276 o << "note,";
277 break;
278 case Shape::Text:
279 o << "text,";
280 break;
281 case Shape::Unknown:
282 #if !defined(__clang__)
283 default:
284 #endif
285 o << "unknown,";
286 break;
287 }
288 o << shape.m_box << ",";
289 if (shape.m_flags & 0x80) o << "rotation,";
290
291 if (shape.m_flags & 0x3f)
292 o << "fl=" << std::hex << (shape.m_flags&0x3f) << std::dec << ",";
293 o << "],";
294 return o;
295 }
296
297 ////////////////////////////////////////
298 //! Internal: the state of a MacDrawProParser
299 struct State {
300 //! constructor
301 State()
302 : m_version(0)
303 , m_isStationery(false)
304 , m_numPages(1)
305 , m_actualLayer(1)
306 , m_numLayers(1)
307 , m_numHiddenLayers(0)
308 , m_numVisibleLayers(0)
309 , m_createMasterPage(false)
310 , m_sendAsLibraries(false)
311 , m_numLibraries(0)
312 , m_numShapes(0)
313 , m_libraryList()
314 , m_layerList()
315 , m_objectDataList()
316 , m_objectTextList()
317 , m_shapeList()
318 {
319 for (auto &size : m_sizeStyleZones) size=0;
320 for (auto &size : m_sizeLayerZones) size=0;
321 for (auto &size : m_sizeLibraryZones) size=0;
322 for (auto &size : m_sizeFZones) size=0;
323 }
324 //! the file version
325 int m_version;
326 //! flag to know if the file is a stationery document
327 bool m_isStationery;
328 //! the final number of pages
329 int m_numPages;
330 //! the actual layer
331 int m_actualLayer;
332 //! the number of layer
333 int m_numLayers;
334 //! the number of hidden layer
335 int m_numHiddenLayers;
336 //! the number of visible layer
337 int m_numVisibleLayers;
338 //! flag to know if we need or not to create a master
339 bool m_createMasterPage;
340 //! flag to know if we create a page by library or not
341 bool m_sendAsLibraries;
342 //! the number of library
343 int m_numLibraries;
344 //! the total number of shapes
345 int m_numShapes;
346 //! the size of the header zones
347 long m_sizeStyleZones[6];
348 //! the size of the layer zones
349 long m_sizeLayerZones[2];
350 //! the size of the library zones(checkme)
351 long m_sizeLibraryZones[2];
352 //! the size of the zoneF
353 long m_sizeFZones[4];
354 //! the library list
355 std::vector<Library> m_libraryList;
356 //! the layer list
357 std::vector<Layer> m_layerList;
358 //! the list of entries which stores the object's data
359 std::vector<MWAWEntry> m_objectDataList;
360 //! the list of entries which stores the object's text
361 std::vector<MWAWEntry> m_objectTextList;
362 //! the shape list
363 std::vector<Shape> m_shapeList;
364 };
365
366 ////////////////////////////////////////
367 //! Internal: the subdocument of a MacDrawProParser
368 class SubDocument final : public MWAWSubDocument
369 {
370 public:
371 //! constructor given an zone id
372 SubDocument(MacDrawProParser &pars, MWAWInputStreamPtr const &input, int zoneId)
373 : MWAWSubDocument(&pars, input, MWAWEntry())
374 , m_id(zoneId)
375 , m_measureEntry()
376 {
377 }
378 //! constructor given a measure entry
379 SubDocument(MacDrawProParser &pars, MWAWInputStreamPtr const &input, MWAWEntry const &measureEntry)
380 : MWAWSubDocument(&pars, input, MWAWEntry())
381 , m_id(-1)
382 , m_measureEntry(measureEntry)
383 {
384 }
385
386 //! destructor
387 ~SubDocument() final {}
388
389 //! operator!=
390 bool operator!=(MWAWSubDocument const &doc) const final
391 {
392 if (MWAWSubDocument::operator!=(doc)) return true;
393 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
394 if (!sDoc) return true;
395 if (m_id != sDoc->m_id) return true;
396 if (m_measureEntry != sDoc->m_measureEntry) return true;
397 return false;
398 }
399
400 //! the parser function
401 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
402
403 protected:
404 //! the subdocument id
405 int m_id;
406 //! the measure entry
407 MWAWEntry m_measureEntry;
408 private:
409 SubDocument(SubDocument const &orig) = delete;
410 SubDocument &operator=(SubDocument const &orig) = delete;
411 };
412
413 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
414 {
415 if (!listener || !listener->canWriteText()) {
416 MWAW_DEBUG_MSG(("MacDrawProParserInternal::SubDocument::parse: no listener\n"));
417 return;
418 }
419 auto *parser=dynamic_cast<MacDrawProParser *>(m_parser);
420 if (!parser) {
421 MWAW_DEBUG_MSG(("MacDrawProParserInternal::SubDocument::parse: no parser\n"));
422 return;
423 }
424 long pos = m_input->tell();
425 if (m_id >= 0)
426 parser->sendText(m_id);
427 else
428 parser->sendMeasure(m_measureEntry);
429 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
430 }
431
432 }
433
434 ////////////////////////////////////////////////////////////
435 // constructor/destructor, ...
436 ////////////////////////////////////////////////////////////
437 MacDrawProParser::MacDrawProParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
438 : MWAWGraphicParser(input, rsrcParser, header)
439 , m_state()
440 , m_styleManager()
441 {
442 init();
443 }
444
445 MacDrawProParser::~MacDrawProParser()
446 {
447 }
448
449 void MacDrawProParser::init()
450 {
451 resetGraphicListener();
452 setAsciiName("main-1");
453
454 m_state.reset(new MacDrawProParserInternal::State);
455 m_styleManager.reset(new MacDrawProStyleManager(*this));
456
457 getPageSpan().setMargins(0.1);
458 }
459
460 ////////////////////////////////////////////////////////////
461 // the parser
462 ////////////////////////////////////////////////////////////
463 void MacDrawProParser::parse(librevenge::RVNGDrawingInterface *docInterface)
464 {
465 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
466 bool ok = false;
467 try {
468 // create the asciiFile
469 ascii().setStream(getInput());
470 ascii().open(asciiName());
471 checkHeader(nullptr);
472 ok = createZones();
473 if (ok) {
474 createDocument(docInterface);
475 sendMasterPage();
476 for (int i=0; i<m_state->m_numPages; ++i)
477 sendPage(i);
478 #ifdef DEBUG
479 flushExtra();
480 #endif
481 }
482 ascii().reset();
483 }
484 catch (...) {
485 MWAW_DEBUG_MSG(("MacDrawProParser::parse: exception catched when parsing\n"));
486 ok = false;
487 }
488
489 resetGraphicListener();
490 if (!ok) throw(libmwaw::ParseException());
491 }
492
493 ////////////////////////////////////////////////////////////
494 // create the document
495 ////////////////////////////////////////////////////////////
496 void MacDrawProParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
497 {
498 if (!documentInterface) return;
499 if (getGraphicListener()) {
500 MWAW_DEBUG_MSG(("MacDrawProParser::createDocument: listener already exist\n"));
501 return;
502 }
503
504 // we need one page for the master page: one page by hidden layers
505 int numPages=m_state->m_sendAsLibraries ? static_cast<int>(m_state->m_libraryList.size()) :
506 m_state->m_numHiddenLayers;
507 if (numPages<=0) numPages=1;
508 m_state->m_numPages = numPages;
509
510 // create the list of page names
511 std::vector<librevenge::RVNGString> namesList;
512 if (m_state->m_sendAsLibraries) {
513 for (auto const &library : m_state->m_libraryList)
514 namesList.push_back(library.m_name);
515 }
516 else {
517 for (auto const &layer : m_state->m_layerList) {
518 if (!layer.m_isHidden) continue;
519 namesList.push_back(layer.m_name);
520 }
521 }
522 if (static_cast<int>(namesList.size()) < numPages)
523 namesList.resize(size_t(numPages));
524
525 // create the page list
526 MWAWPageSpan ps(getPageSpan());
527 m_state->m_createMasterPage=!m_state->m_sendAsLibraries &&
528 m_state->m_numHiddenLayers>1 && m_state->m_numVisibleLayers>0;
529 if (m_state->m_createMasterPage)
530 ps.setMasterPageName(librevenge::RVNGString("Master"));
531
532 std::vector<MWAWPageSpan> pageList;
533 int actUnamedPage=0;
534 for (auto const &name : namesList) {
535 if (name.empty()) {
536 ++actUnamedPage;
537 continue;
538 }
539 if (actUnamedPage) {
540 ps.setPageSpan(actUnamedPage);
541 pageList.push_back(ps);
542 actUnamedPage=0;
543 }
544 MWAWPageSpan psNamed(ps);
545 psNamed.setPageName(name);
546 psNamed.setPageSpan(1);
547 pageList.push_back(psNamed);
548 }
549 if (actUnamedPage) {
550 ps.setPageSpan(actUnamedPage);
551 pageList.push_back(ps);
552 }
553
554 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
555 setGraphicListener(listen);
556 listen->startDocument();
557 }
558
559 ////////////////////////////////////////////////////////////
560 //
561 // Intermediate level
562 //
563 ////////////////////////////////////////////////////////////
564 bool MacDrawProParser::createZones()
565 {
566 MWAWInputStreamPtr input = getInput();
567 int const vers=version();
568 if (getRSRCParser()) m_styleManager->readRSRCZones();
569 readHeaderInfo();
570
571 input->seek(vers==0 ? 0x1f4 : 0x1d4,librevenge::RVNG_SEEK_SET);
572 if (!m_styleManager->readStyles(m_state->m_sizeStyleZones) ||
573 !readLayersInfo() || !readLayerLibraryCorrespondance() || !readLibrariesInfo() ||
574 !findObjectPositions(true) || !findObjectPositions(false)) {
575 MWAW_DEBUG_MSG(("MacDrawProParser::createZones: something is bad, stop.\n"));
576 return false;
577 }
578 long pos;
579 libmwaw::DebugStream f;
580 if (m_state->m_sizeFZones[3]) {
581 /* checkme: I am not sure if this zone contains data or if it is
582 only an intermediary zone used to make possible to grow the
583 previous zones */
584 pos=input->tell();
585 long endPos=pos+m_state->m_sizeFZones[3];
586 f.str("");
587 f << "Entries(Free0):";
588 if (m_state->m_sizeFZones[3]<0 || !input->checkPosition(endPos)) {
589 MWAW_DEBUG_MSG(("MacDrawProParser::createZones: can not read Free0 size\n"));
590 ascii().addPos(pos);
591 ascii().addNote(f.str().c_str());
592 return false;
593 }
594 ascii().addPos(pos);
595 ascii().addNote(f.str().c_str());
596 input->seek(endPos, librevenge::RVNG_SEEK_SET);
597 }
598 pos=input->tell();
599 if (m_state->m_numShapes<0 || !input->checkPosition(pos+32L*m_state->m_numShapes)) {
600 MWAW_DEBUG_MSG(("MacDrawProParser::createZones: can not read the object information\n"));
601 ascii().addPos(pos);
602 ascii().addNote("Entries(Object):###");
603 return false;
604 }
605 for (int i=0; i<m_state->m_numShapes; ++i) {
606 if (readObject()<0) break;
607 }
608
609 // probably a free zone (used to reserve space to make the file grow)
610 if (!input->isEnd()) {
611 pos=input->tell();
612 ascii().addPos(pos);
613 ascii().addNote("Entries(Free1):");
614 }
615
616 int n=0;
617 for (auto const &entry : m_state->m_objectDataList) {
618 ++n;
619 if (!entry.valid() || entry.isParsed())
620 continue;
621 MWAW_DEBUG_MSG(("MacDrawProParser::createZones: find some unparsed data's object zones\n"));
622 f.str("");
623 f << "Entries(ObjData)[" << n-1 << "]:###unparsed";
624 ascii().addPos(entry.begin());
625 ascii().addNote(f.str().c_str());
626 }
627
628 return computeLayersAndLibrariesBoundingBox();
629 }
630
631 ////////////////////////////////////////////////////////////
632 // layer/library functions
633 ////////////////////////////////////////////////////////////
634 bool MacDrawProParser::readLayersInfo()
635 {
636 int const vers=version();
637 MWAWInputStreamPtr input = getInput();
638 long pos=input->tell();
639 long begNamePos=pos+m_state->m_sizeLayerZones[0];
640 long endPos=pos+m_state->m_sizeLayerZones[0]+m_state->m_sizeLayerZones[1];
641 if (input->isEnd() || m_state->m_numLayers*0x80>m_state->m_sizeLayerZones[0] || !input->checkPosition(endPos)) {
642 MWAW_DEBUG_MSG(("MacDrawProParser::readLayersInfo: problem with the layer dimensions\n"));
643 ascii().addPos(pos);
644 ascii().addNote("Entries(Layer):###");
645 return false;
646 }
647
648 libmwaw::DebugStream f;
649 int numShapes=0;
650 std::map<int,long> idToNamePosMap;
651 m_state->m_layerList.clear();
652 for (int i=0; i<m_state->m_numLayers; ++i) {
653 MacDrawProParserInternal::Layer layer;
654 layer.m_firstShape=numShapes;
655 if (i && m_state->m_actualLayer==i+1)
656 layer.m_isHidden=true;
657 pos=input->tell();
658 f.str("");
659 f << "Entries(Layer)[L" << i+1 << "]:";
660 auto val=long(input->readULong(2));
661 if (val&0x8000) {
662 layer.m_isHidden=true;
663 f << "hidden,";
664 }
665 val &=0x7FFF;
666 if (val) f << "fl=" << std::hex << val << std::dec << ",";
667 f << "id?=" << input->readULong(2) << ","; // a small number
668 auto delta=long(input->readULong(4));
669 if (delta) f << "name[pos]=" << std::hex << delta << std::dec << ",";
670 if (delta < 0 || delta >=m_state->m_sizeLayerZones[1]) {
671 MWAW_DEBUG_MSG(("MacDrawProParser::readLayersInfo: problem with the layer position dim\n"));
672 f << "###";
673 delta=-1;
674 }
675 idToNamePosMap[i]=delta;
676 layer.m_numShapes=static_cast<int>(input->readULong(4));
677 if (layer.m_numShapes < 0 || layer.m_numShapes > std::numeric_limits<int>::max() - numShapes) {
678 MWAW_DEBUG_MSG(("MacDrawProParser::readLayersInfo: found an absurd number of shapes\n"));
679 layer.m_numShapes = 0;
680 }
681 numShapes+=layer.m_numShapes;
682 if (layer.m_numShapes) f << "N[shapes]=" << layer.m_numShapes << ",";
683 m_state->m_layerList.push_back(layer);
684 val=long(input->readLong(4)); // always 0?
685 if (val) f << "f0=" << val << ",";
686 f << "N[unkn]=[";
687 for (auto &n : layer.m_N) { // small numbers always less than numShapes. Note: f2 can be equal to -1
688 n=long(input->readLong(4));
689 if (!n) {
690 f << "_,";
691 continue;
692 }
693 if (n>layer.m_numShapes) {
694 MWAW_DEBUG_MSG(("MacDrawProParser::readLayersInfo: find an old number in header\n"));
695 f << "#";
696 }
697 if (n==-1)
698 f << "*,";
699 else
700 f << n << "/" << layer.m_numShapes << ",";
701 }
702 f << "],";
703 ascii().addPos(pos);
704 ascii().addNote(f.str().c_str());
705
706 for (int j=0; j<2; ++j) {
707 /* very often Layer-A0=Layer-A1.
708 When they differ, Layer-A1 seems to contain more data than Layer-A0
709 */
710 pos=input->tell();
711 f.str("");
712 f << "Layer-A" << j << "[L" << i+1 << "]:";
713 f << "N[unkn]=[";
714 for (int k=0; k<3; ++k) { // small numbers always less than numShapes, but they can be greater than m_N[k]
715 val=long(input->readLong(4));
716 if (!val) {
717 f << "_,";
718 continue;
719 }
720 if (val > layer.m_numShapes) f << "#";
721 f << val << "/" << layer.m_numShapes << ",";
722 }
723 f << "],";
724 for (int k=0; k<2; ++k) {
725 float dim[4];
726 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
727 if (dim[0]>0||dim[1]>0||dim[2]>0||dim[3]>0)
728 f << "dim" << k << "=" << MWAWBox2f(MWAWVec2f(dim[0],dim[1]),MWAWVec2f(dim[2],dim[3])) << ",";
729 }
730 ascii().addPos(pos);
731 ascii().addNote(f.str().c_str());
732 }
733
734 pos=input->tell();
735 f.str("");
736 f << "Layer-B[L" << i+1 << "]:";
737 ascii().addPos(pos);
738 ascii().addNote(f.str().c_str());
739
740 input->seek(pos+(vers==0 ? 12 : 14), librevenge::RVNG_SEEK_SET);
741 }
742 m_state->m_numShapes=numShapes;
743
744 for (auto const &it : idToNamePosMap) {
745 if (it.second < 0) continue;
746 pos=begNamePos+it.second;
747 input->seek(pos, librevenge::RVNG_SEEK_SET);
748 f.str("");
749 f << "Layer[L" << it.first+1 << ",name]:";
750 auto fSz=static_cast<int>(input->readULong(1));
751 if (input->tell()+fSz>endPos) {
752 MWAW_DEBUG_MSG(("MacDrawProParser::readLayersInfo: oops the layer name size seems bad\n"));
753 f << "###";
754 ascii().addPos(pos);
755 ascii().addNote(f.str().c_str());
756 continue;
757 }
758 librevenge::RVNGString name("");
759 for (int c=0; c<fSz; ++c) {
760 auto ch=char(input->readULong(1));
761 if (!ch) continue;
762 f << ch;
763 int unicode= getParserState()->m_fontConverter->unicode(3, static_cast<unsigned char>(ch));
764 if (unicode==-1)
765 name.append(ch);
766 else
767 libmwaw::appendUnicode(static_cast<uint32_t>(unicode), name);
768 }
769 f << ",";
770 m_state->m_layerList[size_t(it.first)].m_name=name;
771 ascii().addPos(pos);
772 ascii().addNote(f.str().c_str());
773 }
774 input->seek(endPos, librevenge::RVNG_SEEK_SET);
775 return true;
776 }
777
778 bool MacDrawProParser::readLayerLibraryCorrespondance()
779 {
780 if (!m_state->m_sizeFZones[0]) return true;
781 MWAWInputStreamPtr input = getInput();
782 long pos=input->tell();
783
784 MWAWEntry entry;
785 entry.setBegin(pos);
786 entry.setLength(m_state->m_sizeFZones[0]);
787 entry.setName("LayToLib");
788
789 if (entry.length()<0 || !input->checkPosition(entry.end())) {
790 MWAW_DEBUG_MSG(("MacDrawProParser::readLayerLibraryCorrespondance: the zone size seems bad\n"));
791 ascii().addPos(pos);
792 ascii().addNote("Entries(LayToLib):###");
793 return false;
794 }
795 std::map<int, long> idToDecal;
796 if (!readStructuredHeaderZone(entry, idToDecal)) {
797 MWAW_DEBUG_MSG(("MacDrawProParser::readLayerLibraryCorrespondance: can not read the header\n"));
798 ascii().addPos(pos);
799 ascii().addNote("Entries(LayToLib):###");
800 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
801 return true;
802 }
803
804 pos=input->tell();
805 libmwaw::DebugStream f;
806 f << "LayToLib[data]:";
807 auto sz=long(input->readULong(4));
808 if (sz<4 || pos+sz>entry.end()) {
809 MWAW_DEBUG_MSG(("MacDrawProParser::readLayerLibraryCorrespondance: can not read the data size\n"));
810 f << "###";
811 ascii().addPos(pos);
812 ascii().addNote(f.str().c_str());
813 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
814 return true;
815 }
816 ascii().addPos(pos);
817 ascii().addNote(f.str().c_str());
818 MacDrawProParserInternal::Layer falseLayer;
819 int actId=0;
820 for (auto const &it : idToDecal) {
821 long decal=it.second;
822 if (decal<0 || decal+8>sz) {
823 MWAW_DEBUG_MSG(("MacDrawProParser::readLayerLibraryCorrespondance: the zone %d's decal seems bad\n", it.first));
824 continue;
825 }
826 int id=actId++;
827 f.str("");
828 f << "LayToLib-L" << id+1 << ":";
829 if (id<0 || id>=static_cast<int>(m_state->m_layerList.size())) {
830 MWAW_DEBUG_MSG(("MacDrawProParser::readLayerLibraryCorrespondance: the layer's number:%d seems bad\n", id));
831 }
832 MacDrawProParserInternal::Layer &layer=(id>=0 && id<static_cast<int>(m_state->m_layerList.size())) ?
833 m_state->m_layerList[size_t(id)] : falseLayer;
834 long begPos=pos+decal;
835 input->seek(begPos, librevenge::RVNG_SEEK_SET);
836 auto val=static_cast<int>(input->readLong(2)); // always 0?
837 if (val) f << "f0=" << val << ",";
838 val=static_cast<int>(input->readLong(2)); // 1|3
839 if (val!=1) f << "type=" << val << ",";
840 auto dataSz=long(input->readULong(4));
841 if (dataSz<8 || begPos+dataSz>entry.end() || (dataSz%8)!=0) {
842 MWAW_DEBUG_MSG(("MacDrawProParser::readLayerLibraryCorrespondance: the zone %d data size seems bad\n", id));
843 f << "###";
844 ascii().addPos(begPos);
845 ascii().addNote(f.str().c_str());
846 continue;
847 }
848 auto N=int((dataSz-8)/8);
849 f << "libs=[";
850 for (int i=0; i<N; ++i) {
851 auto obj=static_cast<int>(input->readLong(4)); // find 1 or 2
852 auto val1=static_cast<int>(input->readLong(2));
853 auto library=static_cast<int>(input->readLong(2));
854 if (library<1 || library>m_state->m_numLibraries) {
855 MWAW_DEBUG_MSG(("MacDrawProParser::readLayerLibraryCorrespondance: a library number seems bad\n"));
856 f << "###";
857 }
858 else {
859 if (library>int(m_state->m_libraryList.size()))
860 m_state->m_libraryList.resize(size_t(library));
861 m_state->m_libraryList[size_t(library-1)].m_id=library;
862 m_state->m_libraryList[size_t(library-1)].m_layerList.push_back(id);
863 layer.m_libraryToObjectMap[library]=obj;
864 }
865 f << "Li" << library;
866 if (obj>1) f << ":O" << obj-1;
867 if (val1) f << ":" << val1;
868 f << ",";
869 }
870 f << "],";
871 ascii().addPos(begPos+dataSz);
872 ascii().addNote("_");
873 ascii().addPos(begPos);
874 ascii().addNote(f.str().c_str());
875 }
876 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
877 return true;
878 }
879
880 bool MacDrawProParser::readLibrariesInfo()
881 {
882 if (!m_state->m_sizeLibraryZones[0] && !m_state->m_sizeLibraryZones[1])
883 return true;
884
885 int const vers=version();
886 MWAWInputStreamPtr input = getInput();
887 long pos=input->tell();
888 long begNamePos=pos+m_state->m_sizeLibraryZones[0];
889 long endPos=pos+m_state->m_sizeLibraryZones[0]+m_state->m_sizeLibraryZones[1];
890 if (!input->checkPosition(endPos)) {
891 MWAW_DEBUG_MSG(("MacDrawProParser::readLibrariesInfo: problem with the library dimensions\n"));
892 ascii().addPos(pos);
893 ascii().addNote("Entries(Library):###");
894 return false;
895 }
896 int const fieldSize=vers==0 ? 8 : 10;
897 if ((m_state->m_sizeLibraryZones[0]%fieldSize) || !m_state->m_sizeLibraryZones[0]||!m_state->m_sizeLibraryZones[1]) {
898 MWAW_DEBUG_MSG(("MacDrawProParser::readLibrariesInfo: problem with the size zone(II)\n"));
899 ascii().addPos(pos);
900 ascii().addNote("Entries(Library):###");
901 input->seek(endPos, librevenge::RVNG_SEEK_SET);
902 return true;
903 }
904
905 libmwaw::DebugStream f;
906 f << "Entries(Library):";
907 std::vector<long> posList;
908 auto N=int(m_state->m_sizeLibraryZones[0]/fieldSize);
909 for (int i=0; i<N; ++i) {
910 auto cPos=long(input->readULong(4));
911 if (cPos<0 || cPos>m_state->m_sizeLibraryZones[1]) {
912 MWAW_DEBUG_MSG(("MacDrawProParser::readLayersInfo: oops the library name positions seems bad\n"));
913 f << "###";
914 posList.push_back(-1);
915 }
916 else
917 posList.push_back(cPos);
918 f << std::hex << cPos << std::dec;
919 auto val=static_cast<int>(input->readLong(4));
920 if (val) f << ":" << val;
921 f << ",";
922 if (vers>0) {
923 val=static_cast<int>(input->readLong(2));
924 if (val!=i+1) f << "id=" << val << ",";
925 }
926 }
927 ascii().addPos(pos);
928 ascii().addNote(f.str().c_str());
929 for (size_t i=0; i<posList.size(); ++i) {
930 if (posList[i]<0) continue;
931
932 pos=begNamePos+posList[i];
933 f.str("");
934 f << "Library-name[" << i << "]:";
935 input->seek(pos, librevenge::RVNG_SEEK_SET);
936 auto fSz=static_cast<int>(input->readULong(1));
937 if (input->tell()+fSz>endPos) {
938 MWAW_DEBUG_MSG(("MacDrawProParser::readLayersInfo: oops the library name size seems bad\n"));
939 f << "###";
940 ascii().addPos(pos);
941 ascii().addNote(f.str().c_str());
942 continue;
943 }
944 librevenge::RVNGString name("");
945 for (int c=0; c<fSz; ++c) {
946 auto ch=char(input->readULong(1));
947 if (!ch) continue;
948 f << ch;
949 int unicode= getParserState()->m_fontConverter->unicode(3, static_cast<unsigned char>(ch));
950 if (unicode==-1)
951 name.append(ch);
952 else
953 libmwaw::appendUnicode(static_cast<uint32_t>(unicode), name);
954 }
955 f << ",";
956 if (int(i) < m_state->m_numLibraries) {
957 if (i>=m_state->m_libraryList.size()) {
958 m_state->m_libraryList.resize(i+1);
959 m_state->m_libraryList[i].m_id=int(i)+1;
960 }
961 m_state->m_libraryList[i].m_name=name;
962 }
963
964 ascii().addPos(pos);
965 ascii().addNote(f.str().c_str());
966 }
967 input->seek(endPos, librevenge::RVNG_SEEK_SET);
968 return true;
969 }
970
971 ////////////////////////////////////////////////////////////
972 // generic function
973 ////////////////////////////////////////////////////////////
974 bool MacDrawProParser::readStructuredHeaderZone(MWAWEntry const &entry, std::map<int, long> &idToDeltaPosMap)
975 {
976 idToDeltaPosMap.clear();
977 if (!entry.length())
978 return true;
979
980 MWAWInputStreamPtr input = getInput();
981 long pos=entry.begin();
982 libmwaw::DebugStream f;
983 f << "Entries(" << entry.name() << "):";
984
985 if (entry.length()<4+4 || !input->checkPosition(entry.end())) {
986 MWAW_DEBUG_MSG(("MacDrawProParser::readStructuredHeaderZone: can not read %s size\n", entry.name().c_str()));
987 f << "###";
988 ascii().addPos(pos);
989 ascii().addNote(f.str().c_str());
990 return false;
991 }
992
993 input->seek(pos,librevenge::RVNG_SEEK_SET);
994 auto sz=long(input->readULong(4));
995 long endPos=pos+sz;
996 if (sz<8 || endPos>entry.end()) {
997 MWAW_DEBUG_MSG(("MacDrawProParser::readStructuredHeaderZone: can not read %s-ptr size\n", entry.name().c_str()));
998 f << "ptr###";
999 ascii().addPos(pos);
1000 ascii().addNote(f.str().c_str());
1001 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1002 return true;
1003 }
1004 long fFree=static_cast<int>(input->readULong(4));
1005 f << "fFree=" << fFree << ",";
1006 if (fFree!=sz)
1007 ascii().addDelimiter(pos+fFree,'|');
1008 auto numDatas=int((sz-8)/4);
1009 f << "ptrs=[";
1010 for (int i=0; i<numDatas; ++i) {
1011 auto ptr=long(input->readULong(4));
1012 if (!ptr) continue;
1013 idToDeltaPosMap[i]=ptr;
1014 f << std::hex << ptr << std::dec << ":" << i << ",";
1015 }
1016 f << "],";
1017 ascii().addPos(pos);
1018 ascii().addNote(f.str().c_str());
1019 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1020
1021 return true;
1022 }
1023
1024 ////////////////////////////////////////////////////////////
1025 // object functions
1026 ////////////////////////////////////////////////////////////
1027 bool MacDrawProParser::findObjectPositions(bool dataZone)
1028 {
1029 if (!m_state->m_sizeFZones[dataZone ? 1 : 2]) return true;
1030 MWAWInputStreamPtr input = getInput();
1031 long pos=input->tell();
1032
1033 MWAWEntry entry;
1034 entry.setBegin(pos);
1035 entry.setLength(m_state->m_sizeFZones[dataZone ? 1 : 2]);
1036 entry.setName(dataZone ? "ObjData" : "ObjText");
1037
1038 libmwaw::DebugStream f;
1039 f << entry.name() << "[data]:";
1040 if (entry.length()<0 || !input->checkPosition(entry.end())) {
1041 MWAW_DEBUG_MSG(("MacDrawProParser::findObjectPositions: the zone size seems bad\n"));
1042 f << "###";
1043 ascii().addPos(pos);
1044 ascii().addNote(f.str().c_str());
1045 return false;
1046 }
1047 std::map<int, long> idToDecal;
1048 if (!readStructuredHeaderZone(entry, idToDecal)) {
1049 MWAW_DEBUG_MSG(("MacDrawProParser::findObjectPositions: can not read the header\n"));
1050 f << "###";
1051 ascii().addPos(pos);
1052 ascii().addNote(f.str().c_str());
1053 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1054 return true;
1055 }
1056
1057 pos=input->tell();
1058 auto sz=long(input->readULong(4));
1059 if (sz<4 || pos+sz>entry.end()) {
1060 MWAW_DEBUG_MSG(("MacDrawProParser::findObjectPositions: can not read the data size\n"));
1061 f << "###";
1062 ascii().addPos(pos);
1063 ascii().addNote(f.str().c_str());
1064 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1065 return true;
1066 }
1067 ascii().addPos(pos);
1068 ascii().addNote(f.str().c_str());
1069 std::vector<MWAWEntry> &positionList=dataZone ? m_state->m_objectDataList : m_state->m_objectTextList;
1070 positionList.clear();
1071 for (auto const &it : idToDecal) {
1072 int id=it.first;
1073 long decal=it.second;
1074 if (decal<4 || decal+8>sz) {
1075 MWAW_DEBUG_MSG(("MacDrawProParser::findObjectPositions: the zone %d's decal seems bad\n", id));
1076 continue;
1077 }
1078
1079 f.str("");
1080 f << entry.name() << "[" << id << "]:";
1081 long begPos=pos+decal;
1082 input->seek(begPos+4, librevenge::RVNG_SEEK_SET);
1083 auto dataSz=long(input->readULong(4));
1084 if (dataSz<8 || begPos+dataSz>entry.end()) {
1085 MWAW_DEBUG_MSG(("MacDrawProParser::findObjectPositions: the zone %d data size seems bad\n", id));
1086 f << "###";
1087 ascii().addPos(begPos);
1088 ascii().addNote(f.str().c_str());
1089 continue;
1090 }
1091 MWAWEntry dEntry;
1092 dEntry.setBegin(begPos);
1093 dEntry.setLength(dataSz);
1094 dEntry.setId(id);
1095 if (id>=int(positionList.size()))
1096 positionList.resize(size_t(id+1));
1097 if (id>=0 && id < int(positionList.size()))
1098 positionList[size_t(id)]=dEntry;
1099 else {
1100 MWAW_DEBUG_MSG(("MacDrawProParser::findObjectPositions: can not store entry %d\n", id));
1101 f << "###";
1102 ascii().addPos(begPos);
1103 ascii().addNote(f.str().c_str());
1104 }
1105 ascii().addPos(dEntry.end());
1106 ascii().addNote("_");
1107 }
1108 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1109 return true;
1110 }
1111
1112 bool MacDrawProParser::computeLayersAndLibrariesBoundingBox()
1113 {
1114 if (m_state->m_layerList.empty()) {
1115 MWAW_DEBUG_MSG(("MacDrawProParser::computeLayersAndLibrariesBoundingBox: can not find any layer\n"));
1116 return false;
1117 }
1118 int n=0;
1119 for (auto &layer : m_state->m_layerList) {
1120 ++n;
1121 if (layer.m_firstShape < 0 || layer.m_numShapes < 0 ||
1122 (layer.m_numShapes && layer.m_firstShape >= int(m_state->m_shapeList.size()))) {
1123 MWAW_DEBUG_MSG(("MacDrawProParser::computeLayersAndLibrariesBoundingBox: the layer %d seems bad\n", int(n-1)));
1124 layer.m_numShapes=0;
1125 layer.m_isHidden=false;
1126 continue;
1127 }
1128 if (layer.m_isHidden)
1129 ++m_state->m_numHiddenLayers;
1130 else
1131 ++m_state->m_numVisibleLayers;
1132 MWAWBox2f box;
1133 bool boxSet=false;
1134 for (int j=layer.m_firstShape; j<layer.m_firstShape+layer.m_numShapes; ++j) {
1135 if (j>=int(m_state->m_shapeList.size())) {
1136 MWAW_DEBUG_MSG(("MacDrawProParser::computeLayersAndLibrariesBoundingBox: the layer %d seems to contain to much shapes\n", int(n-1)));
1137 layer.m_numShapes=j-layer.m_firstShape;
1138 break;
1139 }
1140 auto const &shape=m_state->m_shapeList[size_t(j)];
1141 if (shape.m_type==MacDrawProParserInternal::Shape::Group ||
1142 shape.m_type==MacDrawProParserInternal::Shape::GroupEnd ||
1143 shape.m_type==MacDrawProParserInternal::Shape::Unknown)
1144 continue;
1145 if (!boxSet) {
1146 box=shape.getBdBox();
1147 boxSet=true;
1148 }
1149 else
1150 box=box.getUnion(shape.getBdBox());
1151 }
1152 if (boxSet)
1153 layer.m_box=box;
1154 else if (layer.m_numShapes) {
1155 MWAW_DEBUG_MSG(("MacDrawProParser::computeLayersAndLibrariesBoundingBox: can not compute the bdbox of the layer %d\n", int(n-1)));
1156 }
1157 }
1158
1159 size_t numLayers=m_state->m_layerList.size();
1160 if (m_state->m_libraryList.empty()) {
1161 // create a false library which contains all layer
1162 MacDrawProParserInternal::Library library(1);
1163 for (int i=0; i<int(numLayers); ++i)
1164 library.m_layerList.push_back(i);
1165 m_state->m_libraryList.push_back(library);
1166 }
1167
1168 MWAWVec2f pageSize(float(72*getPageSpan().getFormWidth()), float(72*getPageSpan().getFormLength()));
1169 MWAWVec2f leftTop(72.f*float(getPageSpan().getMarginLeft()),72.f*float(getPageSpan().getMarginTop()));
1170 MWAWBox2f docBox;
1171 bool docBoxSet=false;
1172 int numLibraryWithGroup=0;
1173 for (size_t i=0; i<m_state->m_libraryList.size(); ++i) {
1174 auto &library=m_state->m_libraryList[i];
1175 if (library.m_layerList.empty()) continue;
1176 MWAWBox2f box;
1177 bool boxSet=false;
1178 for (auto &id : library.m_layerList) {
1179 if (id<0 || id>=int(numLayers)) {
1180 MWAW_DEBUG_MSG(("MacDrawProParser::computeLayersAndLibrariesBoundingBox: library %d contains bad layer\n", int(i)));
1181 id=-1;
1182 continue;
1183 }
1184 auto const &layer= m_state->m_layerList[size_t(id)];
1185 if (layer.m_box.size()[0]<0 || layer.m_box.size()[1]<0)
1186 continue;
1187 MWAWBox2f newBox(layer.m_box);
1188 if (layer.m_libraryToObjectMap.find(int(i+1))!=layer.m_libraryToObjectMap.end()) {
1189 int objId=layer.m_firstShape+layer.m_libraryToObjectMap.find(int(i+1))->second;
1190 if (objId<=0 || objId>int(m_state->m_shapeList.size())) {
1191 MWAW_DEBUG_MSG(("MacDrawProParser::computeLayersAndLibrariesBoundingBox: can not find begin library group %d\n", int(objId)));
1192 }
1193 else {
1194 auto const &shape=m_state->m_shapeList[size_t(objId-1)];
1195 if (shape.m_type==MacDrawProParserInternal::Shape::Group) {
1196 newBox=shape.m_box;
1197 ++numLibraryWithGroup;
1198 }
1199 // else seems to happen sometimes, maybe, this may be ok...
1200 }
1201 }
1202
1203 if (!boxSet) {
1204 box=MWAWBox2f(newBox[0]+leftTop, newBox[1]+leftTop);
1205 boxSet=true;
1206 }
1207 else
1208 box=box.getUnion(MWAWBox2f(newBox[0]+leftTop, newBox[1]+leftTop));
1209 }
1210 if (boxSet)
1211 library.m_box=box;
1212 else if (m_state->m_numShapes>0) {
1213 MWAW_DEBUG_MSG(("MacDrawProParser::computeLayersAndLibrariesBoundingBox: can not compute the bdbox of the library %d\n", int(i)));
1214 }
1215 // convert it to a multiple of page size
1216 MWAWVec2f libRB=library.m_box[1];
1217 for (int c=0; c<2; ++c) {
1218 if (pageSize[c]<=0) continue;
1219 if (libRB[c]<=0)
1220 libRB[c]=pageSize[c];
1221 else {
1222 auto numPage=int(libRB[c]/pageSize[c]-0.01f);
1223 if (numPage<0) numPage=0;
1224 libRB[c]=float(numPage+1)*pageSize[c];
1225 }
1226 }
1227 library.m_box.setMax(libRB);
1228 if (!docBoxSet) {
1229 docBox=library.m_box;
1230 docBoxSet=true;
1231 }
1232 else
1233 docBox=docBox.getUnion(library.m_box);
1234 }
1235 if (docBoxSet) {
1236 getPageSpan().setFormWidth(double(docBox[1][0])/72);
1237 getPageSpan().setFormLength(double(docBox[1][1])/72);
1238 }
1239 if (m_state->m_isStationery && numLayers==1 && numLibraryWithGroup>1 && version()>0 &&
1240 numLibraryWithGroup==static_cast<int>(m_state->m_libraryList.size()))
1241 m_state->m_sendAsLibraries=true;
1242 return true;
1243 }
1244
1245 int MacDrawProParser::readObject()
1246 {
1247 int const vers=version();
1248 MWAWInputStreamPtr input = getInput();
1249 if (input->isEnd()) return -1;
1250 if (m_state->m_numShapes <= int(m_state->m_shapeList.size()))
1251 return false;
1252
1253 long pos=input->tell();
1254 libmwaw::DebugStream f;
1255 size_t shapeId= m_state->m_shapeList.size();
1256 f << "Entries(Object)[O" << shapeId << "]:";
1257 int const expectedSize=vers==0 ? 32 : 34;
1258 if (!input->checkPosition(pos+expectedSize)) {
1259 MWAW_DEBUG_MSG(("MacDrawProParser::readObject: the zone seems to small\n"));
1260 f << "###";
1261 ascii().addPos(pos);
1262 ascii().addNote(f.str().c_str());
1263 return -1;
1264 }
1265 m_state->m_shapeList.push_back(MacDrawProParserInternal::Shape());
1266 auto &shape=m_state->m_shapeList.back();
1267 shape.m_id=int(shapeId);
1268 shape.m_nextId=shape.m_id+1; // default value
1269 float dim[4];
1270 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
1271 shape.m_box=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
1272 f << shape.m_box << ",";
1273 auto flags=static_cast<int>(input->readULong(1));
1274 shape.m_flags=flags;
1275 if (flags & 0x8) f << "select,";
1276 if (flags & 0x20) f << "locked,";
1277 bool hasData=(flags & 0x40);
1278 if (flags & 0x80) f << "rotation,";
1279 flags &=0x17;
1280 if (flags) f << "fl0=" << std::hex << flags << std::dec << ",";
1281 auto val=static_cast<int>(input->readULong(1));
1282 // checkme
1283 if (val&0x20) {
1284 shape.m_style.m_flip[0]=true;
1285 f << "flipX,";
1286 }
1287 if (val&0x40) {
1288 shape.m_style.m_flip[1]=true;
1289 f << "flipY,";
1290 }
1291 if (val&0x90) f << "fl1=" << std::hex << ((val>>4)&9) << std::dec << ",";
1292 shape.m_fileType = (val & 0xF);
1293 switch (shape.m_fileType) {
1294 case 1:
1295 shape.m_type=MacDrawProParserInternal::Shape::Text;
1296 f << "text,";
1297 break;
1298 case 2:
1299 shape.m_type=MacDrawProParserInternal::Shape::Basic;
1300 shape.m_shape.m_type=MWAWGraphicShape::Line;
1301 f << "line,";
1302 break;
1303 case 3:
1304 shape.m_type=MacDrawProParserInternal::Shape::Basic;
1305 shape.m_shape.m_type=MWAWGraphicShape::Rectangle;
1306 f << "rect,";
1307 break;
1308 case 4:
1309 shape.m_type=MacDrawProParserInternal::Shape::Basic;
1310 shape.m_shape.m_type=MWAWGraphicShape::Rectangle;
1311 f << "rectOval,";
1312 break;
1313 case 5:
1314 shape.m_type=MacDrawProParserInternal::Shape::Basic;
1315 shape.m_shape.m_type=MWAWGraphicShape::Circle;
1316 f << "circle,";
1317 break;
1318 case 6:
1319 shape.m_type=MacDrawProParserInternal::Shape::Basic;
1320 shape.m_shape.m_type=MWAWGraphicShape::Arc;
1321 f << "arc,";
1322 break;
1323 case 7:
1324 shape.m_type=MacDrawProParserInternal::Shape::Basic;
1325 shape.m_shape.m_type=MWAWGraphicShape::Polygon;
1326 f << "poly[smooth],";
1327 break;
1328 case 8:
1329 shape.m_type=MacDrawProParserInternal::Shape::Basic;
1330 shape.m_shape.m_type=MWAWGraphicShape::Polygon;
1331 f << "poly,";
1332 break;
1333 case 9:
1334 if (vers==0) {
1335 shape.m_type=MacDrawProParserInternal::Shape::Bitmap;
1336 f << "bitmap,";
1337 }
1338 else {
1339 shape.m_type=MacDrawProParserInternal::Shape::Basic;
1340 shape.m_shape.m_type=MWAWGraphicShape::Polygon;
1341 f << "poly[9],";
1342 }
1343 break;
1344 case 0xa:
1345 if (hasData) {
1346 shape.m_type=MacDrawProParserInternal::Shape::Note;
1347 f << "note,";
1348 }
1349 else {
1350 shape.m_type=MacDrawProParserInternal::Shape::Group;
1351 f << "group,";
1352 }
1353 break;
1354 case 0xb:
1355 shape.m_type=MacDrawProParserInternal::Shape::GroupEnd;
1356 f << "group[end],";
1357 break;
1358 case 0xc:
1359 if (vers) {
1360 shape.m_type=MacDrawProParserInternal::Shape::Bitmap;
1361 f << "bitmap,";
1362 break;
1363 }
1364 MWAW_FALLTHROUGH;
1365 default:
1366 MWAW_DEBUG_MSG(("MacDrawProParser::readObject: find unknown type %d\n", val));
1367 f << "###type=" << std::hex << val << std::dec << ",";
1368 }
1369 val=static_cast<int>(input->readLong(1)); // 0|1
1370 if (val==0) f << "f0[no],";
1371 else if (val!=1) f << "f0=" << val << ",";
1372
1373 // read the linewidth
1374 val=static_cast<int>(input->readULong(1));
1375 if (val) {
1376 float penSize=1;
1377 if (!m_styleManager->getPenSize(val, penSize))
1378 f << "###P" << val << ",";
1379 else {
1380 shape.m_style.m_lineWidth=penSize;
1381 if (penSize<1 || penSize>1)
1382 f << "lineWidth=" << penSize << ",";
1383 }
1384 }
1385 if (vers==1) {
1386 val=static_cast<int>(input->readULong(2));
1387 if (val) f << "f1=" << std::hex << val << std::dec << ",";
1388 }
1389 // reads the pattern
1390 int patId[2]= {0,0};
1391 static char const *wh[]= {"line", "surf"};
1392 for (int i=0; i<2; ++i) {
1393 val=static_cast<int>(input->readULong(2));
1394 if (vers>0) {
1395 patId[i]=val;
1396 continue;
1397 }
1398 if (!val) { // no color
1399 if (i==0) {
1400 shape.m_style.m_lineWidth=0;
1401 f << "line[no],";
1402 }
1403 continue;
1404 }
1405 if (val&0x4000) { // pattern created from colorMap
1406 int cId=val&0x3FFF;
1407 MWAWColor color;
1408 if (!m_styleManager->getColor(cId, color)) {
1409 MWAW_DEBUG_MSG(("MacDrawProParser::readObject: find unknown basic color pattern: %d\n", cId));
1410 f << wh[i] << "[color]=###" << cId << ",";
1411 }
1412 else {
1413 if (i==0) shape.m_style.m_lineColor=color;
1414 else shape.m_style.setSurfaceColor(color);
1415 if ((i==0&&!color.isBlack()) || (i==1 && !color.isWhite()))
1416 f << wh[i] << "[color]=" << color << ",";
1417 }
1418 continue;
1419 }
1420 MWAWGraphicStyle::Pattern pattern;
1421 if (!m_styleManager->getPattern(val, pattern)) {
1422 f << wh[i] << "[pat]=###" << std::hex << val << std::dec << ",";
1423 continue;
1424 }
1425 MWAWColor color;
1426 if (pattern.getUniqueColor(color)) {
1427 if (i==0) shape.m_style.m_lineColor=color;
1428 else shape.m_style.setSurfaceColor(color);
1429 if ((i==0&&!color.isBlack()) || (i==1 && !color.isWhite()))
1430 f << wh[i] << "[color]=" << color << ",";
1431 continue;
1432 }
1433 f << wh[i] << "[pat]=[" << pattern << "],";
1434 if (i==0 && pattern.getAverageColor(color))
1435 shape.m_style.m_lineColor=color;
1436 else if (i==1)
1437 shape.m_style.setPattern(pattern);
1438 }
1439 // read the dash
1440 val=static_cast<int>(input->readULong(1));
1441 if (val) {
1442 if (!m_styleManager->getDash(val, shape.m_style.m_lineDashWidth))
1443 f << "###";
1444 f << "dash=D" << val << ",";
1445 }
1446
1447 float cornerWidth=-1;
1448 if (shape.m_fileType==4 && !hasData) {
1449 val=static_cast<int>(input->readLong(4));
1450 if (val) {
1451 cornerWidth = float(val)/65536.f/36.f; // check the unit here
1452 f << "round[dim]=" << cornerWidth << ",";
1453 }
1454 else
1455 f << "round[def],";
1456 }
1457 else {
1458 // unknown
1459 val=static_cast<int>(input->readLong(1)); // a small number negative or positif between -4 and 3
1460 if (val)
1461 f << "f2=" << val << ",";
1462 }
1463
1464 if (shape.m_type==MacDrawProParserInternal::Shape::Basic)
1465 updateGeometryShape(shape, cornerWidth);
1466 if (hasData) {
1467 val=static_cast<int>(input->readULong(2));
1468 if (val<8) {
1469 MWAW_DEBUG_MSG(("MacDrawProParser::readObject: can not find the data position\n"));
1470 f << "###objData[pos],";
1471 }
1472 else {
1473 long actPos=input->tell();
1474 if (!readObjectData(shape, (val-8)/4))
1475 f << "###";
1476 input->seek(actPos, librevenge::RVNG_SEEK_SET);
1477 f << "objData[id]=" << (val-8)/4 << ",";
1478 }
1479 if (val&3)
1480 f << "objData[low]=" << (val&3) << ",";
1481 }
1482 else if (shape.m_type==MacDrawProParserInternal::Shape::Group) {
1483 val=static_cast<int>(input->readULong(2));
1484 int N=(val/0x20)-2;
1485 size_t lastShapeId=shapeId+size_t(N);
1486 // in MacDraw Pro, the number of elements seems frequently a majorant, so ...
1487 if (vers>0 && val>0x40 && int(lastShapeId)>m_state->m_numShapes) {
1488 f << "#number[object]=" << N << ",";
1489 lastShapeId=size_t(m_state->m_numShapes);
1490 }
1491 if (val<0x40 || int(lastShapeId)>m_state->m_numShapes) {
1492 MWAW_DEBUG_MSG(("MacDrawProParser::readObject: can not find the group number\n"));
1493 f << "###number[object]=" << N << ",";
1494 }
1495 else {
1496 f << "N=" << N << ",";
1497 if (val&0x1F)
1498 f << "N[low]="<< (val&0x1F) << ",";
1499 input->seek(pos+expectedSize, librevenge::RVNG_SEEK_SET);
1500
1501 bool ok=true, findEnd=false;
1502 for (int i=0; i<N; ++i) {
1503 if (m_state->m_shapeList.size()>lastShapeId)
1504 break;
1505 int cId=readObject();
1506 if (cId<0) {
1507 MWAW_DEBUG_MSG(("MacDrawProParser::readObject: can not find a child\n"));
1508 f << "###";
1509 ok=false;
1510 break;
1511 }
1512 // in MacDraw Pro, the number of elements seems frequently a majorant, so we must check
1513 if (vers>0 && m_state->m_shapeList[size_t(cId)].m_type==MacDrawProParserInternal::Shape::GroupEnd) {
1514 f << "#[" << lastShapeId-m_state->m_shapeList.size() << "],";
1515 findEnd=true;
1516 break;
1517 }
1518 // do not use shape's childList as the vector can have grown
1519 m_state->m_shapeList[shapeId].m_childList.push_back(size_t(cId));
1520 }
1521
1522 auto nextId=int(m_state->m_shapeList.size());
1523 if (ok && !findEnd) {
1524 int cId=readObject(); // read end group
1525 if (cId<0 || m_state->m_shapeList[size_t(cId)].m_type!=MacDrawProParserInternal::Shape::GroupEnd) {
1526 MWAW_DEBUG_MSG(("MacDrawProParser::readObject: oops, can not find the end group data\n"));
1527 f << "###";
1528 }
1529 else // ok skip end group
1530 ++nextId;
1531 }
1532 m_state->m_shapeList[shapeId].m_nextId=nextId;
1533
1534 ascii().addPos(pos);
1535 ascii().addNote(f.str().c_str());
1536 return int(shapeId);
1537 }
1538 }
1539
1540 if (vers==0) {
1541 if (input->tell() != pos+expectedSize)
1542 ascii().addDelimiter(input->tell(),'|');
1543 input->seek(pos+expectedSize, librevenge::RVNG_SEEK_SET);
1544 ascii().addPos(pos);
1545 ascii().addNote(f.str().c_str());
1546 return int(shapeId);
1547 }
1548 input->seek(pos+expectedSize-4, librevenge::RVNG_SEEK_SET);
1549 int colId[2]= {0,0};
1550 for (auto &id : colId) id=static_cast<int>(input->readULong(2));
1551 // time to set the color
1552 for (int i=0; i<2; ++i) {
1553 if (!patId[i]) { // no color
1554 if (i==0) {
1555 shape.m_style.m_lineWidth=0;
1556 f << "line[no],";
1557 }
1558 continue;
1559 }
1560 MWAWColor color(MWAWColor::black());
1561 if ((colId[i]>>14)==3) {
1562 if (!i || !m_styleManager->updateGradient(colId[i]&0x3FFF, shape.m_style)) {
1563 MWAW_DEBUG_MSG(("MacDrawProParser::readObject: find unknown gradient: %d\n", colId[i]));
1564 f << wh[i] << "[grad]=###" << std::hex << colId[i] << std::dec << ",";
1565 }
1566 continue;
1567 }
1568 else if ((colId[i]>>14)==1) {
1569 static bool first=true;
1570 if (first) {
1571 MWAW_DEBUG_MSG(("MacDrawProParser::readObject: find some 4000 color\n"));
1572 first=false;
1573 }
1574 f << wh[i] << "[color]=##" << std::hex << colId[i] << std::dec << ",";
1575 color=MWAWColor::white();
1576 }
1577 else if (colId[i] && !m_styleManager->getColor(colId[i], color)) {
1578 MWAW_DEBUG_MSG(("MacDrawProParser::readObject: find unknown basic color: %d\n", colId[i]));
1579 f << wh[i] << "[color]=###" << std::hex << colId[i] << std::dec << ",";
1580 continue;
1581 }
1582 MWAWGraphicStyle::Pattern pattern;
1583 bool useBasicColor=false;
1584 // normally, we only have BWpattern
1585 MWAWColor patColor;
1586 if (patId[i]==0xC006) // used for black pattern
1587 useBasicColor=true;
1588 else if (!m_styleManager->getPattern(patId[i], pattern)) {
1589 f << wh[i] << "[pat]=###" << std::hex << patId[i] << std::dec << ",";
1590 useBasicColor=true;
1591 }
1592 else if (pattern.getUniqueColor(patColor))
1593 useBasicColor=true;
1594 if (useBasicColor) { // only color
1595 if (i==0) shape.m_style.m_lineColor=color;
1596 else shape.m_style.setSurfaceColor(color);
1597 if ((i==0&&!color.isBlack()) || (i==1 && !color.isWhite()))
1598 f << wh[i] << "[color]=" << color << ",";
1599 continue;
1600 }
1601 f << wh[i] << "[pat]=[" << pattern << "],";
1602 if (i==0 && pattern.getAverageColor(patColor)) {
1603 float alpha=1.0f-float(patColor.getBlue())/255.f;
1604 shape.m_style.m_lineColor=MWAWColor::barycenter(alpha, color, 1.f-alpha, MWAWColor::white());
1605 }
1606 else if (i==1) {
1607 pattern.m_colors[1]=color;
1608 shape.m_style.setPattern(pattern);
1609 }
1610 }
1611
1612 if (input->tell() != pos+expectedSize)
1613 ascii().addDelimiter(input->tell(),'|');
1614 input->seek(pos+expectedSize, librevenge::RVNG_SEEK_SET);
1615 ascii().addPos(pos);
1616 ascii().addNote(f.str().c_str());
1617 return int(shapeId);
1618 }
1619
1620 bool MacDrawProParser::readObjectData(MacDrawProParserInternal::Shape &shape, int zId)
1621 {
1622 if (zId<0 || zId>=static_cast<int>(m_state->m_objectDataList.size()) || !m_state->m_objectDataList[size_t(zId)].valid()) {
1623 MWAW_DEBUG_MSG(("MacDrawProParser::readObjectData: can not find the data for zone %d\n", zId));
1624 return false;
1625 }
1626 MWAWEntry const &entry=m_state->m_objectDataList[size_t(zId)];
1627 entry.setParsed(true);
1628 ascii().addPos(entry.end());
1629 ascii().addNote("_");
1630
1631 MWAWInputStreamPtr input = getInput();
1632 long savePos=input->tell();
1633 if (shape.m_type==MacDrawProParserInternal::Shape::Basic) {
1634 bool res=readGeometryShapeData(shape, entry);
1635 input->seek(savePos, librevenge::RVNG_SEEK_SET);
1636 return res;
1637 }
1638 else if (shape.m_type==MacDrawProParserInternal::Shape::Bitmap) {
1639 bool res=readBitmap(shape, entry);
1640 input->seek(savePos, librevenge::RVNG_SEEK_SET);
1641 return res;
1642 }
1643 else if (shape.m_type==MacDrawProParserInternal::Shape::Text ||
1644 shape.m_type==MacDrawProParserInternal::Shape::Note) {
1645 bool res=version()==0 ? readTextII(shape, entry) : readTextPro(shape, entry);
1646 input->seek(savePos, librevenge::RVNG_SEEK_SET);
1647 return res;
1648 }
1649 libmwaw::DebugStream f;
1650 f << "ObjData[" << shape << "]:";
1651 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1652 auto val=static_cast<int>(input->readLong(2)); // always 0?
1653 if (val) f << "f0=" << val << ",";
1654 val=static_cast<int>(input->readLong(2));
1655 if (val!=1) f << "numUsed=" << val << ",";
1656 input->seek(4, librevenge::RVNG_SEEK_CUR); // skip the size field
1657
1658 val=static_cast<int>(input->readLong(2)); // always 0?
1659 if (val) f << "f1=" << val << ",";
1660 val=static_cast<int>(input->readLong(2));
1661 if (val) {
1662 MWAW_DEBUG_MSG(("MacDrawProParser::readObjectData: find unexpected f2 position\n"));
1663 f << "#f2=" << val << ",";
1664 }
1665 std::string extra("");
1666 if (!readRotationInObjectData(shape, entry.end(), extra)) {
1667 f << "###rot,";
1668 ascii().addPos(entry.begin());
1669 ascii().addNote(f.str().c_str());
1670 return false;
1671 }
1672 f << extra;
1673
1674 if (input->tell()!=entry.end()) {
1675 ascii().addDelimiter(input->tell(),'|');
1676 MWAW_DEBUG_MSG(("MacDrawProParser::readObjectData: find unexpected data\n"));
1677 f << "###";
1678 }
1679 ascii().addPos(entry.begin());
1680 ascii().addNote(f.str().c_str());
1681
1682 input->seek(savePos, librevenge::RVNG_SEEK_SET);
1683 return true;
1684 }
1685
1686 bool MacDrawProParser::readRotationInObjectData(MacDrawProParserInternal::Shape &shape, long endPos, std::string &extra)
1687 {
1688 if ((shape.m_flags & 0x80)==0)
1689 return true;
1690
1691 MWAWInputStreamPtr input = getInput();
1692 if (input->tell()+28>endPos) {
1693 MWAW_DEBUG_MSG(("MacDrawProParser::readRotationInObjectData: can not find the rotation data\n"));
1694 extra="###rot,";
1695 return false;
1696 }
1697
1698 libmwaw::DebugStream f;
1699 float angle= float(input->readLong(4))/65536.f; // in radians
1700 shape.m_style.m_rotate = float(180./M_PI*double(angle));
1701 f << "angl[rot]=" << shape.m_style.m_rotate << ",";
1702 float dim[4];
1703 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
1704 MWAWBox2f rect(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2]));
1705 f << "prevDim[rot]=" << rect << ",";
1706 f << "unkn[rot]=[";
1707 for (int i=0; i<2; ++i) // another points ?
1708 f << float(input->readLong(4))/65536.f << ",";
1709 f << "],";
1710 shape.m_box=MWAWBox2f(rect[0]+shape.m_box[0], rect[1]+shape.m_box[0]);
1711 if (shape.m_type==MacDrawProParserInternal::Shape::Basic)
1712 shape.m_shape.m_bdBox=shape.m_shape.m_formBox=shape.m_box;
1713 extra=f.str();
1714 return true;
1715 }
1716
1717 bool MacDrawProParser::updateGeometryShape(MacDrawProParserInternal::Shape &shape, float cornerWidth)
1718 {
1719 if (shape.m_type!=MacDrawProParserInternal::Shape::Basic) {
1720 MWAW_DEBUG_MSG(("MacDrawProParser::updateGeometryShape: called with unexpected shape\n"));
1721 return false;
1722 }
1723 switch (shape.m_fileType) {
1724 case 2: {
1725 int flipX=shape.m_style.m_flip[0] ? 1 : 0;
1726 int flipY=shape.m_style.m_flip[1] ? 1 : 0;
1727 shape.m_shape=MWAWGraphicShape::line
1728 (MWAWVec2f(shape.m_box[1-flipX][0],shape.m_box[1-flipY][1]),
1729 MWAWVec2f(shape.m_box[flipX][0],shape.m_box[flipY][1]));
1730 break;
1731 }
1732 case 3: // rect
1733 case 4: { // rectOval
1734 float cWidth=0;
1735 if (shape.m_fileType==4)
1736 cWidth=cornerWidth>0 ? cornerWidth : 25;
1737 shape.m_shape=MWAWGraphicShape::rectangle(shape.m_box, MWAWVec2f(cWidth,cWidth));
1738 break;
1739 }
1740 case 5: // circle
1741 shape.m_shape=MWAWGraphicShape::circle(shape.m_box);
1742 break;
1743 case 6:
1744 // we need the arc angle, shape will be created when reading data
1745 break;
1746 case 7: // polygon
1747 case 8: // spline
1748 case 9: // polygon smooth
1749 shape.m_shape.m_bdBox=shape.m_box;
1750 break;
1751 default:
1752 MWAW_DEBUG_MSG(("MacDrawProParser::updateGeometryShape: called with unexpected file type\n"));
1753 break;
1754 }
1755 return true;
1756 }
1757
1758 bool MacDrawProParser::readTextII(MacDrawProParserInternal::Shape &shape, MWAWEntry const &entry)
1759 {
1760 if ((shape.m_type!=MacDrawProParserInternal::Shape::Text &&
1761 shape.m_type!=MacDrawProParserInternal::Shape::Note) || version()>0 || entry.length()<28) {
1762 MWAW_DEBUG_MSG(("MacDrawProParser::readTextII: the entry seems bad\n"));
1763 return false;
1764 }
1765 entry.setParsed(true);
1766
1767 MWAWInputStreamPtr input = getInput();
1768 libmwaw::DebugStream f;
1769 f << "ObjData[" << shape << "]:";
1770 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1771 int val;
1772 val=static_cast<int>(input->readLong(2));
1773 if (val) f << "f0=" << val << ",";
1774 val=static_cast<int>(input->readLong(2));
1775 if (val!=1) f << "numUsed=" << val << ",";
1776 input->seek(4, librevenge::RVNG_SEEK_CUR); // skip the size field
1777
1778 val=static_cast<int>(input->readLong(2)); // always 0?
1779 if (val) f << "f1=" << val << ",";
1780 val=static_cast<int>(input->readLong(2));
1781 if (val>=8) {
1782 shape.m_textZoneId=(val-8)/4;
1783 f << "objText[id]=" << (val-8)/4 << ",";
1784 if (val&3)
1785 f << "objText[low]=" << (val&3) << ",";
1786 }
1787 else {
1788 MWAW_DEBUG_MSG(("MacDrawProParser::readTextII: find unexpected text position\n"));
1789 f << "###objText[pos]=" << val << ",";
1790 }
1791
1792 std::string extra("");
1793 if (!readRotationInObjectData(shape, entry.end(), extra)) {
1794 f << "###rot,";
1795 ascii().addPos(entry.begin());
1796 ascii().addNote(f.str().c_str());
1797 return false;
1798 }
1799 f << extra;
1800 auto remain=int(entry.end()-input->tell());
1801 bool isNote=shape.m_type==MacDrawProParserInternal::Shape::Note;
1802 int headerSize=20;
1803 if (isNote) headerSize+=68;
1804 if (remain<headerSize) {
1805 MWAW_DEBUG_MSG(("MacDrawProParser::readTextII: the text zone seems too short\n"));
1806 return false;
1807 }
1808 val=static_cast<int>(input->readLong(1));
1809 if (val) f << "f0=" << val << ","; // -1|0|1
1810 val=static_cast<int>(input->readLong(1));
1811 MWAWParagraph paragraph;
1812 switch (val) {
1813 case -1:
1814 paragraph.setInterline(2, librevenge::RVNG_PERCENT);
1815 f << "interline=200%,";
1816 break;
1817 case 0: // normal
1818 break;
1819 default:
1820 if (val>0) {
1821 paragraph.setInterline(val, librevenge::RVNG_POINT, MWAWParagraph::AtLeast);
1822 f << "interline=" << val << "pt,";
1823 }
1824 else // maybe percent
1825 f << "#interline=" << val << ",";
1826 }
1827 val=static_cast<int>(input->readLong(1));
1828 if (val) f << "f1=" << val << ","; // 0|-1
1829 val=static_cast<int>(input->readLong(1));
1830 switch (val) {
1831 case 0: // left
1832 break;
1833 case -1:
1834 paragraph.m_justify = MWAWParagraph::JustificationRight;
1835 f << "right,";
1836 break;
1837 case 1:
1838 paragraph.m_justify = MWAWParagraph::JustificationCenter;
1839 f << "center,";
1840 break;
1841 case 2:
1842 paragraph.m_justify = MWAWParagraph::JustificationFull;
1843 f << "justified,";
1844 break;
1845 default:
1846 f << "#align=" << val << ",";
1847 break;
1848 }
1849 shape.m_paragraph=paragraph;
1850 shape.m_numChars=static_cast<int>(input->readULong(2));
1851 if (shape.m_numChars) f << "nChar=" << shape.m_numChars << ",";
1852 int N[2];
1853 for (auto &n : N) n=static_cast<int>(input->readULong(2));
1854 f << "N=[" << N[0] << "," << N[1] << "],";
1855 if (remain<headerSize+6*N[0]+4*N[1]) {
1856 MWAW_DEBUG_MSG(("MacDrawProParser::readTextII: can not read the number of data\n"));
1857 f << "###,";
1858 return false;
1859 }
1860 if (isNote) {
1861 long debPos=input->tell();
1862 f << "note=[";
1863 for (int i=0; i<2; ++i) { // always 0
1864 val=static_cast<int>(input->readLong(2));
1865 if (val) f << "f" << i << "=" << val << ",";
1866 }
1867 f << "flgs?=[";
1868 for (int i=0; i<2; ++i) // two time the same big number ?
1869 f << std::hex << input->readULong(4) << std::dec << ",";
1870 f << "],";
1871 for (int i=0; i<8; ++i) { // always 0
1872 val=static_cast<int>(input->readLong(2));
1873 if (val) f << "f" << i+2 << "=" << val << ",";
1874 }
1875 auto sSz=static_cast<int>(input->readULong(1));
1876 if (sSz>31) {
1877 MWAW_DEBUG_MSG(("MacDrawProParser::readTextII: the note size seems to big\n"));
1878 f << "#sSz=" << sSz << ",";
1879 }
1880 else {
1881 std::string name("");
1882 for (int i=0; i<sSz; ++i) name+=char(input->readULong(1));
1883 f << name << ",";
1884 }
1885 input->seek(debPos+60, librevenge::RVNG_SEEK_SET);
1886 for (int i=0; i<4; ++i) { // always 0
1887 val=static_cast<int>(input->readLong(2));
1888 if (val) f << "g" << i << "=" << val << ",";
1889 }
1890 f << "],";
1891 }
1892 f << "lineBreak=[";
1893 for (int i=0; i<=N[0]; ++i) {
1894 val=static_cast<int>(input->readULong(2));
1895 // do not store the first line: pos 0 and the last line numChars
1896 if (val && val< shape.m_numChars)
1897 shape.m_lineBreakSet.insert(val);
1898 f << val << ",";
1899 }
1900 f << "],";
1901 f << "font=[";
1902 for (int i=0; i<=N[1]; ++i) {
1903 auto cPos=static_cast<int>(input->readULong(2));
1904 val=static_cast<int>(input->readLong(2));
1905 shape.m_fontMap[cPos]=val;
1906 f << cPos << ":F" << val+1 << ",";
1907 }
1908 f << "],";
1909 f << "line[h,?]=[";
1910 for (int i=0; i<=N[0]; ++i)
1911 f << input->readULong(2) << ":" << input->readULong(2) << ",";
1912 f << "],";
1913
1914 if (input->tell()+4<=entry.end()) {
1915 MWAW_DEBUG_MSG(("MacDrawProParser::readTextII: find unexpected data\n"));
1916 f << "#remain,";
1917 }
1918 if (input->tell()!=entry.end()) {
1919 ascii().addDelimiter(input->tell(),'|');
1920 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1921 }
1922 ascii().addPos(entry.begin());
1923 ascii().addNote(f.str().c_str());
1924 return true;
1925 }
1926
1927 bool MacDrawProParser::readTextPro(MacDrawProParserInternal::Shape &shape, MWAWEntry const &entry)
1928 {
1929 if ((shape.m_type!=MacDrawProParserInternal::Shape::Text &&
1930 shape.m_type!=MacDrawProParserInternal::Shape::Note) || entry.length()<28 || version()==0) {
1931 MWAW_DEBUG_MSG(("MacDrawProParser::readTextPro: the entry seems bad\n"));
1932 return false;
1933 }
1934 entry.setParsed(true);
1935
1936 MWAWInputStreamPtr input = getInput();
1937 libmwaw::DebugStream f;
1938 f << "ObjData[" << shape << "]:";
1939 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1940 int val;
1941 val=static_cast<int>(input->readLong(2));
1942 if (val) f << "f0=" << val << ",";
1943 val=static_cast<int>(input->readLong(2));
1944 if (val!=1) f << "numUsed=" << val << ",";
1945 input->seek(4, librevenge::RVNG_SEEK_CUR); // skip the size field
1946
1947 val=static_cast<int>(input->readLong(2)); // always 0?
1948 if (val) f << "f1=" << val << ",";
1949 val=static_cast<int>(input->readLong(2));
1950 if (val>=8) {
1951 shape.m_textZoneId=(val-8)/4;
1952 f << "objText[id]=" << (val-8)/4 << ",";
1953 if (val&3)
1954 f << "objText[low]=" << (val&3) << ",";
1955 }
1956 else {
1957 MWAW_DEBUG_MSG(("MacDrawProParser::readTextPro: find unexpected text position\n"));
1958 f << "###objText[pos]=" << val << ",";
1959 }
1960
1961 std::string extra("");
1962 if (!readRotationInObjectData(shape, entry.end(), extra)) {
1963 f << "###rot,";
1964 ascii().addPos(entry.begin());
1965 ascii().addNote(f.str().c_str());
1966 return false;
1967 }
1968 f << extra;
1969 auto remain=int(entry.end()-input->tell());
1970 bool isNote=shape.m_type==MacDrawProParserInternal::Shape::Note;
1971 int headerSize= 16;
1972 if (isNote) headerSize+=20;
1973 if (remain<headerSize) {
1974 MWAW_DEBUG_MSG(("MacDrawProParser::readTextPro: the text zone seems too short\n"));
1975 return false;
1976 }
1977 shape.m_numChars=static_cast<int>(input->readULong(4));
1978 if (shape.m_numChars) f << "nChar=" << shape.m_numChars << ",";
1979 long N[2];
1980 for (auto &n : N) n=long(input->readULong(4));
1981 f << "N=[" << N[0] << "," << N[1] << "],";
1982 if (remain<headerSize+26*(N[0]+1)+4*(N[1]+1) && N[0]==1)
1983 N[0]=-1;
1984 if (remain<headerSize+26*(N[0]+1)+4*(N[1]+1)) {
1985 MWAW_DEBUG_MSG(("MacDrawProParser::readTextPro: can not read the number of data\n"));
1986 f << "###remain,";
1987 return false;
1988 }
1989 int numVal=isNote ? 12 : 2;
1990 for (int i=0; i<numVal; ++i) {
1991 val=static_cast<int>(input->readLong(2));
1992 if (val) f << "f" << i << "=" << val << ",";
1993 }
1994 f << "mod=[";
1995 for (long i=0; i<=N[1]; ++i) {
1996 auto cPos=static_cast<int>(input->readULong(2));
1997 f << cPos;
1998 val=static_cast<int>(input->readULong(2));
1999 if (val==0xFFFF) // end
2000 f << ":_";
2001 else if (val&0x8000) {
2002 shape.m_paragraphMap[cPos]=(val&0x7FFF);
2003 f << ":P" << (val&0x7FFF) << ",";
2004 }
2005 else {
2006 shape.m_fontMap[cPos]=val;
2007 f << ":F" << val+1 << ",";
2008 }
2009 }
2010 f << "],";
2011 ascii().addPos(entry.begin());
2012 ascii().addNote(f.str().c_str());
2013 for (long i=0; i<=N[0]; ++i) {
2014 long pos=input->tell();
2015 auto cPos=static_cast<int>(input->readULong(2));
2016 // do not store the first line: pos 0 and the last line numChars
2017 if (cPos && cPos< shape.m_numChars)
2018 shape.m_lineBreakSet.insert(cPos);
2019
2020 f.str("");
2021 f << "ObjData[text-P" << i << "]:pos=" << cPos << ":";
2022 for (int j=0; j<5; ++j) {
2023 val=static_cast<int>(input->readULong(2));
2024 if (val)
2025 f << "f" << j << "=" << val << ",";
2026 }
2027 float dim[2];
2028 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
2029 if (dim[0]>0 || dim[0]<0 || dim[1]>0 || dim[1]<0)
2030 f << "dim?=" << MWAWVec2f(dim[0],dim[1]) << ",";
2031 val=static_cast<int>(input->readULong(2));
2032 if (val) f << "f5=" << val << ",";
2033 f << "y=" << float(input->readLong(4))/65536.f << ",";
2034 ascii().addPos(pos);
2035 ascii().addNote(f.str().c_str());
2036 }
2037 if (input->tell()+4<=entry.end()) {
2038 MWAW_DEBUG_MSG(("MacDrawProParser::readTextPro: find unexpected data\n"));
2039 ascii().addPos(input->tell());
2040 ascii().addNote("ObjData[text]:#remain");
2041 }
2042 else if (input->tell()!=entry.end()) {
2043 ascii().addPos(input->tell());
2044 ascii().addNote("ObjData[text]:_");
2045 }
2046 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
2047 return true;
2048 }
2049
2050 bool MacDrawProParser::readGeometryShapeData(MacDrawProParserInternal::Shape &shape, MWAWEntry const &entry)
2051 {
2052 int const vers=version();
2053 if (shape.m_type!=MacDrawProParserInternal::Shape::Basic || entry.length()<8) {
2054 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: the entry seems bad\n"));
2055 return false;
2056 }
2057 entry.setParsed(true);
2058
2059 MWAWInputStreamPtr input = getInput();
2060 libmwaw::DebugStream f;
2061 f << "ObjData[" << shape << "]:";
2062 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
2063 int val;
2064 val=static_cast<int>(input->readLong(2));
2065 if (val) f << "f0=" << val << ",";
2066 val=static_cast<int>(input->readLong(2));
2067 if (val!=1) f << "numUsed=" << val << ",";
2068 input->seek(4, librevenge::RVNG_SEEK_CUR); // skip the size field
2069
2070 val=static_cast<int>(input->readLong(2)); // find 0|1|2 for line, other 0
2071 if (val) f << "f1=" << val << ",";
2072 auto fl=static_cast<int>(input->readLong(2));
2073
2074 std::string extra("");
2075 if (!readRotationInObjectData(shape, entry.end(), extra)) {
2076 f << "###rot,";
2077 ascii().addPos(entry.begin());
2078 ascii().addNote(f.str().c_str());
2079 return false;
2080 }
2081 f << extra;
2082
2083 long remain=entry.end()-input->tell();
2084 switch (shape.m_fileType) {
2085 case 2: { // line
2086 if (fl & 0xF8) {
2087 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: find unknown line flag\n"));
2088 f << "#line[flag]=" << (fl&0xF8) << ",";
2089 }
2090 if (input->tell()+20>entry.end()) {
2091 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: can not read the line header\n"));
2092 f << "###";
2093 break;
2094 }
2095 f << "angl=" << float(input->readLong(4))/65536.f << ","; // in radians
2096 f << "dim=[";
2097 for (int i=0; i<4; ++i)
2098 f << float(input->readLong(4))/65536.f << ",";
2099 f << "],";
2100 bool ok=true;
2101 for (int i=0; i<2; ++i) { // the arrow data
2102 if ((fl&(i+1))==0) continue;
2103 shape.m_style.m_arrows[1-i]=MWAWGraphicStyle::Arrow::plain();
2104 if (input->tell()+24>entry.end()) {
2105 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: can not read the line arrow %d\n", i));
2106 f << "###";
2107 ok=false;
2108 break;
2109 }
2110 f << "arrow[" << (i==1 ? "beg" : "end") << "]=[";
2111 for (int j=0; j<6; ++j) { // arrow points
2112 f << float(input->readLong(4))/65536.f;
2113 if (j%2) f << ",";
2114 else f << "x";
2115 }
2116 f << "],";
2117 }
2118 if (!ok) break;
2119 if ((fl&4)==0) {
2120 fl &= 0xFC;
2121 break;
2122 }
2123
2124 fl &= 0xF8;
2125 long endMeasurePos=input->tell()+44;
2126 f << "measure=[";
2127 if (endMeasurePos>entry.end()) {
2128 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: can not read the line measure\n"));
2129 f << "###";
2130 break;
2131 }
2132 float dim[4];
2133 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
2134 shape.m_measureBox=MWAWBox2f(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2]));
2135 f << "dim=" << shape.m_measureBox << ",";
2136 val=static_cast<int>(input->readLong(2)); // 0 or 4
2137 if (val) f<< "f0=" << val << ",";
2138 auto sSz=static_cast<int>(input->readULong(1));
2139 if (sSz>27) {
2140 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: can not read the line measure size\n"));
2141 f << "###";
2142 break;
2143 }
2144 shape.m_measureEntry.setBegin(input->tell());
2145 shape.m_measureEntry.setLength(sSz);
2146 std::string measure("");
2147 for (int i=0; i<sSz; ++i) measure+=char(input->readULong(1));
2148 f << measure;
2149 input->seek(endMeasurePos, librevenge::RVNG_SEEK_SET);
2150 break;
2151 }
2152 case 3: // rect
2153 case 4: { // rectOval
2154 if (!remain)
2155 break;
2156 if (shape.m_fileType!=4 || remain!=128) {
2157 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: can not read the round rect representation\n"));
2158 f << "###";
2159 break;
2160 }
2161 f << "pts=[";
2162 for (int i=0; i<16; ++i) {
2163 float pt[2];
2164 for (auto &p : pt) p=float(input->readLong(4))/65536.f;
2165 f << MWAWVec2f(pt[1],pt[0]) << ",";
2166 }
2167 f << "],";
2168 break;
2169 }
2170 case 5: { // circle
2171 if (!remain)
2172 break;
2173 if (remain!=64) {
2174 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: can not read the circle representation\n"));
2175 f << "###";
2176 break;
2177 }
2178 f << "pts=[";
2179 for (int i=0; i<8; ++i) {
2180 float pt[2];
2181 for (auto &p : pt) p=float(input->readLong(4))/65536.f;
2182 f << MWAWVec2f(pt[1],pt[0]) << ",";
2183 }
2184 f << "],";
2185 break;
2186 }
2187 case 6: { // arc
2188 if (remain < 8 || (remain%8))
2189 break;
2190 float fileAngle[2];
2191 for (auto &angle : fileAngle) angle=float(input->readLong(4))/65536.f;
2192 f << "angle=" << fileAngle[0] << "->" << fileAngle[0]+fileAngle[1] << ",";
2193 float angle[2] = { 90.f-fileAngle[0]-fileAngle[1], 90.f-fileAngle[0] };
2194 if (fileAngle[1]<0) {
2195 angle[0]=90-fileAngle[0];
2196 angle[1]=90-fileAngle[0]-fileAngle[1];
2197 }
2198 while (angle[1] > 360) {
2199 angle[0]-=360;
2200 angle[1]-=360;
2201 }
2202 while (angle[0] < -360) {
2203 angle[0]+=360;
2204 angle[1]+=360;
2205 }
2206
2207 MWAWBox2f box=shape.m_box;
2208 // we must compute the real bd box
2209 float minVal[2] = { 0, 0 }, maxVal[2] = { 0, 0 };
2210 int limitAngle[2];
2211 for (int i = 0; i < 2; i++)
2212 limitAngle[i] = (angle[i] < 0) ? int(angle[i]/90)-1 : int(angle[i]/90);
2213 for (int bord = limitAngle[0]; bord <= limitAngle[1]+1; bord++) {
2214 float ang = (bord == limitAngle[0]) ? float(angle[0]) :
2215 (bord == limitAngle[1]+1) ? float(angle[1]) : float(90 * bord);
2216 ang *= float(M_PI/180.);
2217 float const actVal[2] = { std::cos(ang), -std::sin(ang)};
2218 if (actVal[0] < minVal[0]) minVal[0] = actVal[0];
2219 else if (actVal[0] > maxVal[0]) maxVal[0] = actVal[0];
2220 if (actVal[1] < minVal[1]) minVal[1] = actVal[1];
2221 else if (actVal[1] > maxVal[1]) maxVal[1] = actVal[1];
2222 }
2223 MWAWBox2f circleBox=box;
2224 // we have the shape box, we need to reconstruct the circle box
2225 if (maxVal[0]>minVal[0] && maxVal[1]>minVal[1]) {
2226 float const scaling[2]= { (box[1][0]-box[0][0])/(maxVal[0]-minVal[0]),
2227 (box[1][1]-box[0][1])/(maxVal[1]-minVal[1])
2228 };
2229 float const constant[2]= { box[0][0]-minVal[0] *scaling[0], box[0][1]-minVal[1] *scaling[1]};
2230 circleBox=MWAWBox2f(MWAWVec2f(constant[0]-scaling[0], constant[1]-scaling[1]),
2231 MWAWVec2f(constant[0]+scaling[0], constant[1]+scaling[1]));
2232 }
2233 if (shape.m_style.hasSurface())
2234 shape.m_shape = MWAWGraphicShape::pie(box, circleBox, MWAWVec2f(float(angle[0]),float(angle[1])));
2235 else
2236 shape.m_shape = MWAWGraphicShape::arc(box, circleBox, MWAWVec2f(float(angle[0]),float(angle[1])));
2237
2238 int N=int(remain/8)-1;
2239 f << "N=" << N << ",pts?=["; // a smooth polygon
2240 for (int i=0; i<N; ++i) {
2241 float pt[2];
2242 for (auto &p : pt) p=float(input->readLong(4))/65536.f;
2243 f << MWAWVec2f(pt[1],pt[0]) << ",";
2244 }
2245 f << "],";
2246 break;
2247 }
2248 case 7: // polygon[smooth]
2249 case 8: // polygon
2250 if (vers==0) {
2251 if (remain<=0 || (remain%8)) {
2252 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: can not read compute the number of point in polygon\n"));
2253 f << "###";
2254 break;
2255 }
2256 shape.m_shape.m_bdBox=shape.m_box;
2257 auto N=int(remain/8);
2258 f << "N=" << N << ",pts=[";
2259 std::vector<MWAWVec2f> listVertices;
2260 MWAWVec2f origin=shape.m_box[0];
2261 for (int i=0; i<N; ++i) {
2262 float pt[2];
2263 for (auto &p : pt) p=float(input->readLong(4))/65536.f;
2264 MWAWVec2f point(pt[1],pt[0]);
2265 listVertices.push_back(point+origin);
2266 f << point << ",";
2267 }
2268 f << "],";
2269 if (shape.m_fileType==8) {
2270 shape.m_shape.m_vertices=listVertices;
2271 break;
2272 }
2273 // try to smooth the curve
2274 shape.m_shape.m_type=MWAWGraphicShape::Path;
2275 shape.m_shape.m_path.push_back(MWAWGraphicShape::PathData('M', listVertices[0]));
2276 for (size_t i=1; i+1<listVertices.size(); ++i) {
2277 MWAWVec2f dir=listVertices[i+1]-listVertices[i-1];
2278 shape.m_shape.m_path.push_back(MWAWGraphicShape::PathData('S', listVertices[i], listVertices[i]-0.1f*dir));
2279 }
2280 if (listVertices.size()>1)
2281 shape.m_shape.m_path.push_back(MWAWGraphicShape::PathData('L', listVertices.back()));
2282 break;
2283 }
2284 MWAW_FALLTHROUGH;
2285 case 9: { // spline
2286 if (remain<=2) {
2287 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: can not read compute the number of point in spline\n"));
2288 f << "###";
2289 break;
2290 }
2291 auto N=static_cast<int>(input->readULong(2));
2292 f << "N=" << N << ",";
2293
2294 f << "pts=[";
2295 MWAWVec2f origin=shape.m_box[0], prevPoint;
2296 bool hasPrevPoint=false, isSpline=false;
2297 std::vector<MWAWVec2f> listVertices;
2298 std::vector<MWAWGraphicShape::PathData> path;
2299 for (int i=0; i<N; ++i) {
2300 long pos=input->tell();
2301 auto type=static_cast<int>(input->readULong(2));
2302 int nCoord=0;
2303 if (type>=0 && type<=9) {
2304 static int const numCoord[]= {3, 3, 3, 3, 0/*4*/, 0/*5*/, 3, 3, 1, 1};
2305 nCoord=numCoord[type];
2306 }
2307 if (nCoord<=0 || pos+2+8*nCoord>entry.end()) {
2308 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: can not determine the number of coordinate\n"));
2309 f << "###type=" << type << ",";
2310 break;
2311 }
2312 MWAWVec2f points[3];
2313 for (int j=0; j<nCoord; ++j) {
2314 float pPos[2];
2315 for (auto &p : pPos) p=float(input->readLong(4))/65536;
2316 points[j]=MWAWVec2f(pPos[1],pPos[0]);
2317 f << points[j] << (j==nCoord-1 ? "," : ":");
2318 points[j]+=origin;
2319 }
2320 if (nCoord==1)
2321 listVertices.push_back(points[0]);
2322 else
2323 isSpline=true;
2324 MWAWVec2f pt=nCoord==1 ? points[0] : points[1];
2325 char pType = hasPrevPoint ? 'C' : i==0 ? 'M' : nCoord==1 ? 'L' : 'S';
2326 path.push_back(MWAWGraphicShape::PathData(pType, pt, hasPrevPoint ? prevPoint : points[0], points[0]));
2327 hasPrevPoint=nCoord==3;
2328 if (hasPrevPoint) prevPoint=points[2];
2329 }
2330 f << "],";
2331 if (isSpline) {
2332 shape.m_shape.m_type=MWAWGraphicShape::Path;
2333 shape.m_shape.m_path=path;
2334 }
2335 else
2336 shape.m_shape.m_vertices=listVertices;
2337 break;
2338 }
2339 default:
2340 if (!remain) break;
2341 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: called with unexpected file type\n"));
2342 break;
2343 }
2344 if (fl) f << "fl=" << std::hex << fl << std::dec << ",";
2345 if (input->tell()!=entry.end()) {
2346 MWAW_DEBUG_MSG(("MacDrawProParser::readGeometryShapeData: find unexpected data\n"));
2347 f << "###";
2348 ascii().addDelimiter(input->tell(),'|');
2349 }
2350 // if there is a rotation, we need to recompute the rect, ..., arc shape
2351 if (shape.m_fileType>=3 && shape.m_fileType<=6 && (shape.m_flags & 0x80))
2352 shape.m_shape = shape.m_shape.rotate(shape.m_style.m_rotate, shape.m_box.center());
2353 ascii().addPos(entry.begin());
2354 ascii().addNote(f.str().c_str());
2355 return true;
2356 }
2357
2358 bool MacDrawProParser::readBitmap(MacDrawProParserInternal::Shape &shape, MWAWEntry const &entry)
2359 {
2360 if (shape.m_type!=MacDrawProParserInternal::Shape::Bitmap || entry.length()<10) {
2361 MWAW_DEBUG_MSG(("MacDrawProParser::readBitmap: the entry seems bad\n"));
2362 return false;
2363 }
2364 int const vers=version();
2365 entry.setParsed(true);
2366
2367 MWAWInputStreamPtr input = getInput();
2368 libmwaw::DebugStream f;
2369 f << "Entries(Bitmap)[" << shape << "]:";
2370 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
2371 int val;
2372 val=static_cast<int>(input->readLong(2));
2373 if (val) f << "f0=" << val << ",";
2374 val=static_cast<int>(input->readLong(2));
2375 if (val!=1) f << "numUsed=" << val << ",";
2376 input->seek(4, librevenge::RVNG_SEEK_CUR); // skip the size field
2377
2378 for (int i=0; i<2; ++i) {
2379 val=static_cast<int>(input->readLong(2));
2380 if (val) f << "f" << i+1 << "=" << val << ",";
2381 }
2382
2383 std::string extra("");
2384 if (!readRotationInObjectData(shape, entry.end(), extra)) {
2385 f << "###rot,";
2386 ascii().addPos(entry.begin());
2387 ascii().addNote(f.str().c_str());
2388 return false;
2389 }
2390 f << extra;
2391 int dim[4];
2392 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
2393 MWAWBox2i &bitmapBox=shape.m_bitmapDim;
2394 bitmapBox=MWAWBox2i(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2]));
2395 f << "dim=" << bitmapBox << ",";
2396 f << "id?=" << std::hex << input->readULong(4) << std::dec << ",";
2397 shape.m_numBytesByRow=static_cast<int>(input->readULong(2));
2398 if (vers>0 && shape.m_numBytesByRow&0x8000) {
2399 f << "color,";
2400 shape.m_bitmapIsColor=true;
2401 shape.m_numBytesByRow&=0x7FFF;
2402 }
2403 f << "rowSize=" << shape.m_numBytesByRow << ",";
2404 MWAWBox2i &fileBox=shape.m_bitmapFileDim;
2405 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
2406 fileBox=MWAWBox2i(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2]));
2407 if (bitmapBox!=fileBox)
2408 f << "bitmap[dimInFile]="<< fileBox << ",";
2409 ascii().addPos(entry.begin());
2410 ascii().addNote(f.str().c_str());
2411 if (vers) {
2412 long pos=input->tell();
2413 f.str("");
2414 f << "Bitmap-A:";
2415 for (int i=0; i<14; ++i) { // BW: all 0, color f4=f6=48, f9=f11=8, f10=1
2416 val=static_cast<int>(input->readLong(2));
2417 if (val) f << "f" << i << "=" << val << ",";
2418 }
2419 val=static_cast<int>(input->readULong(4)); // big number in color : an unit ?
2420 if (val) f << "f15=" << std::hex << val << std::dec << ",";
2421 for (int i=0; i<2; ++i) { // always 0
2422 val=static_cast<int>(input->readLong(2));
2423 if (val) f << "g" << i << "=" << val << ",";
2424 }
2425 shape.m_bitmapClutId=static_cast<int>(input->readLong(2));
2426 if (shape.m_bitmapClutId) f << "clut[id]=" << shape.m_bitmapClutId << ",";
2427 for (int i=0; i<2; ++i) { // always 0
2428 val=static_cast<int>(input->readLong(2));
2429 if (val) f << "g" << i+2 << "=" << val << ",";
2430 }
2431 ascii().addPos(pos);
2432 ascii().addNote(f.str().c_str());
2433 }
2434 shape.m_bitmapEntry.setBegin(input->tell());
2435 shape.m_bitmapEntry.setLength(fileBox.size()[1]*shape.m_numBytesByRow);
2436
2437 if (fileBox.size()[1]<0 || shape.m_numBytesByRow<0 || !input->checkPosition(shape.m_bitmapEntry.end())) {
2438 MWAW_DEBUG_MSG(("MacDrawProParser::readBitmap: can not compute the bitmap endPos\n"));
2439 ascii().addPos(shape.m_bitmapEntry.begin());
2440 ascii().addNote("Bitmap[data]:###");
2441 return false;
2442 }
2443 int const numBytesByPixel=shape.m_bitmapIsColor ? 1 : 8;
2444 if (shape.m_numBytesByRow*numBytesByPixel < fileBox.size()[0] ||
2445 fileBox[0][0]>bitmapBox[0][0] || fileBox[0][1]>bitmapBox[0][1] ||
2446 fileBox[1][0]<bitmapBox[1][0] || fileBox[1][1]<bitmapBox[1][1]) {
2447 ascii().addPos(shape.m_bitmapEntry.begin());
2448 ascii().addNote("Bitmap[data]:###");
2449 MWAW_DEBUG_MSG(("MacDrawProParser::readBitmap: something look bad when reading a bitmap header\n"));
2450 shape.m_bitmapEntry=MWAWEntry();
2451 }
2452 else
2453 ascii().skipZone(shape.m_bitmapEntry.begin(), shape.m_bitmapEntry.end()-1);
2454 if (shape.m_bitmapEntry.end()+4<entry.end()) {
2455 ascii().addPos(shape.m_bitmapEntry.end());
2456 ascii().addNote("Bitmap[end]:###");
2457 MWAW_DEBUG_MSG(("MacDrawProParser::readBitmap: the bitmap data zone seems too big\n"));
2458 }
2459 return true;
2460 }
2461
2462 ////////////////////////////////////////////////////////////
2463 // read the header
2464 ////////////////////////////////////////////////////////////
2465 bool MacDrawProParser::checkHeader(MWAWHeader *header, bool strict)
2466 {
2467 *m_state = MacDrawProParserInternal::State();
2468 MWAWInputStreamPtr input = getInput();
2469 if (!input || !input->hasDataFork() || !input->checkPosition(512))
2470 return false;
2471
2472 libmwaw::DebugStream f;
2473 f << "FileHeader:";
2474 input->seek(0, librevenge::RVNG_SEEK_SET);
2475 auto val=static_cast<int>(input->readULong(2));
2476 int vers=0;
2477 if (val==0x4452) { // MacDraw II
2478 if (input->readULong(2)!=0x5747) return false;
2479 }
2480 else if (val==0x5354) { // MacDraw II template
2481 m_state->m_isStationery=true;
2482 f << "stationery,";
2483 if (input->readULong(2)!=0x4154) return false;
2484 }
2485 else if (val==0x6444) { // MacDraw Pro
2486 if (input->readULong(2)!=0x6f63) return false;
2487 f << "pro,";
2488 vers=1;
2489 }
2490 else if (val==0x644c) { // MacDraw Pro template
2491 m_state->m_isStationery=true;
2492 if (input->readULong(2)!=0x6962) return false;
2493 f << "pro,";
2494 vers=1;
2495 }
2496 else return false;
2497
2498 #ifndef DEBUG
2499 if (vers==1 && !input->hasResourceFork()) {
2500 // as in MacDraw pro, the color, patterns, gradients are stored in the resource fork, ...
2501 MWAW_DEBUG_MSG(("MacDrawProParser::checkHeader: impossible to read a MacDraw Pro files without resource fork\n"));
2502 return false;
2503 }
2504 #endif
2505 val=static_cast<int>(input->readULong(2));
2506 if (val==0) {
2507 f << "D2[not],";
2508 vers=0;
2509 }
2510 else if (val!=0x4432) {
2511 MWAW_DEBUG_MSG(("MacDrawProParser::checkHeader: find unexpected header\n"));
2512 return false;
2513 }
2514 f << "version=" << vers << ",";
2515 f << "subVersion=" << input->readLong(2) << ",";
2516 ascii().addPos(0);
2517 ascii().addNote(f.str().c_str());
2518
2519 if (strict) {
2520 // try to begin the parsing
2521 if (!readHeaderInfo() || !m_styleManager->readStyles(m_state->m_sizeStyleZones)) return false;
2522 m_styleManager.reset(new MacDrawProStyleManager(*this));
2523 // we must check that this is not a basic pict file
2524 input->seek(512+2, librevenge::RVNG_SEEK_SET);
2525 int dim[4];
2526 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
2527 val=static_cast<int>(input->readLong(2));
2528 if (dim[0]<dim[2] && dim[1]<dim[3] && (val==0x1101 || (val==0x11 && input->readLong(2)==0x2ff))) {
2529 // posible
2530 input->seek(512, librevenge::RVNG_SEEK_SET);
2531 MWAWBox2f box;
2532 if (MWAWPictData::check(input, static_cast<int>(input->size()-512), box) != MWAWPict::MWAW_R_BAD)
2533 return false;
2534 }
2535 }
2536
2537 setVersion(vers);
2538 m_state->m_version=vers;
2539 if (header)
2540 header->reset(MWAWDocument::MWAW_T_MACDRAWPRO, vers, MWAWDocument::MWAW_K_DRAW);
2541 input->seek(512,librevenge::RVNG_SEEK_SET);
2542
2543 return true;
2544 }
2545
2546 ////////////////////////////////////////////////////////////
2547 // try to read the prefs zone
2548 ////////////////////////////////////////////////////////////
2549 bool MacDrawProParser::readHeaderInfo()
2550 {
2551 int const vers=version();
2552 MWAWInputStreamPtr input = getInput();
2553 if (!input->checkPosition(512)) {
2554 MWAW_DEBUG_MSG(("MacDrawProParser::readHeaderInfo: the prefs zone seems too short\n"));
2555 ascii().addPos(14);
2556 ascii().addNote("Entries(HeaderInfo):#");
2557 return false;
2558 }
2559 input->seek(8,librevenge::RVNG_SEEK_SET);
2560 if (!readPrintInfo()) {
2561 ascii().addPos(8);
2562 ascii().addNote("Entries(PrintInfo):#");
2563 }
2564 input->seek(8+120, librevenge::RVNG_SEEK_SET);
2565 // v2: cut in 128, 40, 3*40, remain
2566 long pos=input->tell();
2567 libmwaw::DebugStream f;
2568 f << "Entries(HeaderInfo):";
2569 for (int i=0; i<9; ++i) { // f0=1|2|7, f1=0|75|78|7c, f2=0|48, f4=0|48, f5=0|48, f6=0|48, f7=0|48
2570 auto val=static_cast<int>(input->readLong(2));
2571 if (val) f << "f" << i << "=" << val << ",";
2572 }
2573 ascii().addDelimiter(input->tell(),'|');
2574 ascii().addPos(pos);
2575 ascii().addNote(f.str().c_str());
2576
2577 input->seek(0x100, librevenge::RVNG_SEEK_SET);
2578 pos=input->tell();
2579 f.str("");
2580 f << "HeaderInfo-A:";
2581 auto val=static_cast<int>(input->readULong(2)); // 0 or 8
2582 if (val) f << "f0=" << val << ",";
2583 m_state->m_numLayers=static_cast<int>(input->readULong(2)); // 1|2|9
2584 if (m_state->m_numLayers!=1) f << "num[layers]=" << m_state->m_numLayers << ",";
2585 std::string extra("");
2586 int const headerStyleSize=vers==0 ? 24 : 12;
2587 if (!m_styleManager->readHeaderInfoStylePart(extra)) {
2588 f << "###Style[ignored],";
2589 input->seek(pos+4+headerStyleSize, librevenge::RVNG_SEEK_SET);
2590 }
2591 else
2592 f << extra;
2593
2594 m_state->m_actualLayer=static_cast<int>(input->readULong(2));
2595 if (m_state->m_actualLayer!=1) f << "layer[cur]=" << m_state->m_actualLayer << ",";
2596 val=static_cast<int>(input->readULong(2)); // h1=1|2|40
2597 if (val!=1) f << "h1=" << val << ",";
2598 val=static_cast<int>(input->readULong(2)); // 0|45
2599 if (val) f << "h2=" << val << ",";
2600 val=static_cast<int>(input->readULong(2)); // actual shape ?
2601 if (val!=1) f << "shape[actual]?=" << val << ",";
2602 for (int i=0; i<2; ++i) { // h3=0, h4=2|3|7|a
2603 val=static_cast<int>(input->readULong(2));
2604 if (val) f << "h" << i+3 << "=" << val << ",";
2605 }
2606 m_state->m_numLibraries=static_cast<int>(input->readULong(2));
2607 if (m_state->m_numLibraries) f << "num[libraries]=" << m_state->m_numLibraries << ",";
2608
2609 input->seek(pos+4+headerStyleSize+12, librevenge::RVNG_SEEK_SET);
2610 ascii().addPos(pos);
2611 ascii().addNote(f.str().c_str());
2612 for (int i=0; i<3; ++i) {
2613 pos=input->tell();
2614 f.str("");
2615 f << "HeaderInfo-B" << i << ":";
2616 val=static_cast<int>(input->readULong(2)); // always 0 ?
2617 if (val) f << "f0=" << val << ",";
2618
2619 val=static_cast<int>(input->readULong(2));
2620 if (val && i==0) {
2621 m_state->m_numLibraries=val;
2622 f << "num[libraries]=" << m_state->m_numLibraries << ",";
2623 }
2624 else if (val)
2625 f << "num[shape?]=" << val << ",";
2626
2627 ascii().addDelimiter(input->tell(),'|');
2628 input->seek(pos+40, librevenge::RVNG_SEEK_SET);
2629 ascii().addPos(pos);
2630 ascii().addNote(f.str().c_str());
2631 }
2632
2633 pos=input->tell();
2634 f.str("");
2635 f << "HeaderInfo-C:";
2636 int const numSizeZones=vers==0 ? 5 : 6;
2637 for (int i=0; i< numSizeZones; ++i) {
2638 m_state->m_sizeStyleZones[i]=long(input->readULong(4));
2639 if (!m_state->m_sizeStyleZones[i]) continue;
2640 f << "sz[style" << i << "]=" << std::hex << m_state->m_sizeStyleZones[i] << std::dec << ",";
2641 }
2642 if (vers==0) {
2643 for (int i=0; i< 5; ++i) {
2644 auto lVal=long(input->readULong(4));
2645 if (!lVal) continue;
2646 MWAW_DEBUG_MSG(("MacDrawProParser::readHeaderInfo: find some unexpected value in C zone, we may have a problem\n"));
2647 f << "##f" << i << "=" << std::hex << lVal << std::dec << ",";
2648 }
2649 }
2650 ascii().addPos(pos);
2651 ascii().addNote(f.str().c_str());
2652
2653 pos=input->tell();
2654 f.str("");
2655 f << "HeaderInfo-C(II):";
2656 for (auto &size : m_state->m_sizeLayerZones) size=long(input->readULong(4));
2657 f << "sz[layers]=" << std::hex << m_state->m_sizeLayerZones[0] << "+" << m_state->m_sizeLayerZones[1] << std::dec << ",";
2658 m_state->m_sizeFZones[0]=long(input->readULong(4));
2659 if (m_state->m_sizeFZones[0])
2660 f << "sz[ZoneF0]=" << std::hex << m_state->m_sizeFZones[0] << std::dec << ",";
2661 for (auto &size : m_state->m_sizeLibraryZones) size=long(input->readULong(4));
2662 if (m_state->m_sizeLibraryZones[0] || m_state->m_sizeLibraryZones[1])
2663 f << "sz[libraries]=" << std::hex << m_state->m_sizeLibraryZones[0] << "+" << m_state->m_sizeLibraryZones[1] << std::dec << ",";
2664 for (int i=1; i<4; ++i) {
2665 m_state->m_sizeFZones[i]=long(input->readULong(4));
2666 if (!m_state->m_sizeFZones[i]) continue;
2667 f << "sz[ZoneF" << i << "]=" << std::hex << m_state->m_sizeFZones[i] << std::dec << ",";
2668 }
2669 if (vers==0) {
2670 for (int i=0; i<3; ++i) { // always 0?
2671 auto lVal=long(input->readULong(4));
2672 if (!lVal) continue;
2673 MWAW_DEBUG_MSG(("MacDrawProParser::readHeaderInfo: find some unexpected value in C(II) zone, we may have a problem\n"));
2674 f << "##f" << i << "=" << lVal << ",";
2675 }
2676 }
2677 else {
2678 f << "id?=" << std::hex << input->readULong(4) << std::dec << ",";
2679 for (int i=0; i<2; ++i) { // always 0, 1?
2680 val=static_cast<int>(input->readLong(2));
2681 if (val!=i)
2682 f << "f" << i << "=" << val << ",";
2683 }
2684 }
2685
2686 input->seek(vers==1 ? 0x1d4 : 0x1f4, librevenge::RVNG_SEEK_SET);
2687 ascii().addPos(pos);
2688 ascii().addNote(f.str().c_str());
2689
2690 return true;
2691 }
2692
2693 ////////////////////////////////////////////////////////////
2694 // try to read the print info zone
2695 ////////////////////////////////////////////////////////////
2696 bool MacDrawProParser::readPrintInfo()
2697 {
2698 MWAWInputStreamPtr input = getInput();
2699 long pos=input->tell();
2700 long endPos=pos+120;
2701 if (!input->checkPosition(endPos)) {
2702 MWAW_DEBUG_MSG(("MacDrawProParser::readPrintInfo: file seems too short\n"));
2703 return false;
2704 }
2705 libmwaw::DebugStream f;
2706 f << "Entries(PrintInfo):";
2707 libmwaw::PrinterInfo info;
2708 if (!info.read(input)) {
2709 MWAW_DEBUG_MSG(("MacDrawProParser::readPrintInfo: can not read print info\n"));
2710 return false;
2711 }
2712 f << info;
2713 MWAWVec2i paperSize = info.paper().size();
2714 MWAWVec2i pageSize = info.page().size();
2715 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
2716 paperSize.x() <= 0 || paperSize.y() <= 0) {
2717 ascii().addPos(pos);
2718 ascii().addNote(f.str().c_str());
2719 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2720 return true;
2721 }
2722
2723 // define margin from print info
2724 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
2725 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
2726
2727 // move margin left | top
2728 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
2729 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
2730 lTopMargin -= MWAWVec2i(decalX, decalY);
2731 rBotMargin += MWAWVec2i(decalX, decalY);
2732
2733 // decrease right | bottom
2734 int rightMarg = rBotMargin.x() -50;
2735 if (rightMarg < 0) rightMarg=0;
2736 int botMarg = rBotMargin.y() -50;
2737 if (botMarg < 0) botMarg=0;
2738
2739 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
2740 getPageSpan().setMarginBottom(botMarg/72.0);
2741 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
2742 getPageSpan().setMarginRight(rightMarg/72.0);
2743 getPageSpan().setFormLength(paperSize.y()/72.);
2744 getPageSpan().setFormWidth(paperSize.x()/72.);
2745
2746 ascii().addPos(pos);
2747 ascii().addNote(f.str().c_str());
2748 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2749 return true;
2750 }
2751
2752 ////////////////////////////////////////////////////////////
2753 //
2754 // send data
2755 //
2756 ////////////////////////////////////////////////////////////
2757
2758 bool MacDrawProParser::sendMasterPage()
2759 {
2760 if (!m_state->m_createMasterPage)
2761 return true;
2762 MWAWGraphicListenerPtr listener=getGraphicListener();
2763 if (!listener) {
2764 MWAW_DEBUG_MSG(("MacDrawProParser::sendMasterPage: can not find the listener\n"));
2765 m_state->m_createMasterPage=false;
2766 return false;
2767 }
2768 MWAWPageSpan ps(getPageSpan());
2769 ps.setMasterPageName(librevenge::RVNGString("Master"));
2770 if (!listener->openMasterPage(ps)) {
2771 MWAW_DEBUG_MSG(("MacDrawProParser::sendMasterPage: can not create the master page\n"));
2772 m_state->m_createMasterPage=false;
2773 return false;
2774 }
2775 for (auto const &layer : m_state->m_layerList) {
2776 if (layer.m_isHidden)
2777 continue;
2778 send(layer);
2779 }
2780 listener->closeMasterPage();
2781 return true;
2782 }
2783
2784 bool MacDrawProParser::sendPage(int page)
2785 {
2786 MWAWGraphicListenerPtr listener=getGraphicListener();
2787 if (!listener) {
2788 MWAW_DEBUG_MSG(("MacDrawProParser::sendPage: can not find the listener\n"));
2789 return false;
2790 }
2791 if (page>0)
2792 listener->insertBreak(MWAWListener::PageBreak);
2793 if (m_state->m_sendAsLibraries) {
2794 if (page<0 || page>=static_cast<int>(m_state->m_libraryList.size())) {
2795 MWAW_DEBUG_MSG(("MacDrawProParser::sendPage: can not find a library\n"));
2796 return false;
2797 }
2798 send(m_state->m_libraryList[size_t(page)]);
2799 return true;
2800 }
2801 int actHidden=0;
2802 for (auto const &layer : m_state->m_layerList) {
2803 if (layer.m_isHidden && actHidden++!=page)
2804 continue;
2805 if (!layer.m_isHidden && m_state->m_createMasterPage)
2806 continue;
2807 send(layer);
2808 }
2809 return true;
2810 }
2811
2812 bool MacDrawProParser::send(MacDrawProParserInternal::Library const &library)
2813 {
2814 auto numLayers=static_cast<int>(m_state->m_layerList.size());
2815 MWAWVec2f leftTop(72.f*float(getPageSpan().getMarginLeft()),72.f*float(getPageSpan().getMarginTop()));
2816 for (auto id : library.m_layerList) {
2817 if (id<0 || id>=numLayers) continue;
2818 auto const &layer=m_state->m_layerList[size_t(id)];
2819 if (layer.m_libraryToObjectMap.find(library.m_id)!=layer.m_libraryToObjectMap.end()) {
2820 int objId=layer.m_firstShape+layer.m_libraryToObjectMap.find(library.m_id)->second;
2821 if (objId>0 && objId<int(m_state->m_shapeList.size())) {
2822 MacDrawProParserInternal::Shape const &shape=m_state->m_shapeList[size_t(objId-1)];
2823 if (shape.m_type==MacDrawProParserInternal::Shape::Group) {
2824 send(shape, leftTop);
2825 continue;
2826 }
2827 }
2828 }
2829 send(layer);
2830 }
2831 return true;
2832 }
2833
2834 bool MacDrawProParser::send(MacDrawProParserInternal::Layer const &layer)
2835 {
2836 MWAWGraphicListenerPtr listener=getGraphicListener();
2837 if (!listener) {
2838 MWAW_DEBUG_MSG(("MacDrawProParser::send[layer]: can not find the listener\n"));
2839 return false;
2840 }
2841 if (layer.m_firstShape<0)
2842 return true;
2843 auto maxShape=static_cast<int>(m_state->m_shapeList.size());
2844 if (layer.m_firstShape+layer.m_numShapes<maxShape)
2845 maxShape=layer.m_firstShape+layer.m_numShapes;
2846 if (maxShape<=layer.m_firstShape)
2847 return false;
2848 bool openLayer=false;
2849 if (!layer.m_name.empty())
2850 openLayer=listener->openLayer(layer.m_name);
2851 MWAWVec2f leftTop(72.f*float(getPageSpan().getMarginLeft()),72.f*float(getPageSpan().getMarginTop()));
2852 for (int i=layer.m_firstShape; i<maxShape; ++i) {
2853 MacDrawProParserInternal::Shape const &shape=m_state->m_shapeList[size_t(i)];
2854 send(shape, leftTop);
2855 if (shape.m_nextId>i+1)
2856 i+=shape.m_nextId-(i+1);
2857 }
2858 if (openLayer)
2859 listener->closeLayer();
2860 return true;
2861 }
2862
2863 bool MacDrawProParser::send(MacDrawProParserInternal::Shape const &shape, MWAWVec2f const &orig)
2864 {
2865 MWAWGraphicListenerPtr listener=getGraphicListener();
2866 if (!listener) {
2867 MWAW_DEBUG_MSG(("MacDrawProParser::send[shape]: can not find the listener\n"));
2868 return false;
2869 }
2870 shape.m_isSent=true;
2871 // for not basic shape, we need the box before the rotation, so compute the box by hand
2872 MWAWBox2f box=(shape.m_type==MacDrawProParserInternal::Shape::Basic) ? shape.m_shape.getBdBox() : shape.m_box;
2873 box=MWAWBox2f(box[0]+orig, box[1]+orig);
2874 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
2875 pos.m_anchorTo = MWAWPosition::Page;
2876 switch (shape.m_type) {
2877 case MacDrawProParserInternal::Shape::Basic:
2878 listener->insertShape(pos, shape.m_shape, shape.m_style);
2879 if (shape.m_shape.m_type==MWAWGraphicShape::Line && shape.m_measureEntry.valid()) {
2880 MWAWGraphicStyle style;
2881 style.m_lineWidth=0;
2882 style.setSurfaceColor(MWAWColor::white());
2883 std::shared_ptr<MWAWSubDocument> doc(new MacDrawProParserInternal::SubDocument(*this, getInput(), shape.m_measureEntry));
2884 MWAWPosition measurePos(box[0]+shape.m_measureBox[0], shape.m_measureBox.size(), librevenge::RVNG_POINT);
2885 measurePos.m_anchorTo = MWAWPosition::Page;
2886 listener->insertTextBox(measurePos, doc, style);
2887 }
2888 break;
2889 case MacDrawProParserInternal::Shape::Bitmap:
2890 return sendBitmap(shape, pos);
2891 case MacDrawProParserInternal::Shape::Group: {
2892 size_t numShapes=m_state->m_shapeList.size();
2893 if (!numShapes) break;
2894 listener->openGroup(pos);
2895 for (auto childId : shape.m_childList) {
2896 if (childId>=numShapes) {
2897 MWAW_DEBUG_MSG(("MacDrawProParser::send: can not find a child\n"));
2898 continue;
2899 }
2900 auto const &child=m_state->m_shapeList[childId];
2901 if (child.m_isSent) {
2902 MWAW_DEBUG_MSG(("MacDrawProParser::send: the child is already sent\n"));
2903 continue;
2904 }
2905 send(child, orig);
2906 }
2907 listener->closeGroup();
2908 break;
2909 }
2910 case MacDrawProParserInternal::Shape::GroupEnd:
2911 break;
2912 case MacDrawProParserInternal::Shape::Note: {
2913 MWAWGraphicStyle style=shape.m_style;
2914 style.m_lineWidth=1;
2915
2916 MWAWBorder border;
2917 border.m_color=MWAWColor::black();
2918 border.m_width=1;
2919 style.setBorders(libmwaw::LeftBit|libmwaw::BottomBit|libmwaw::RightBit, border);
2920 border.m_color=MWAWColor(0x60,0x60,0); // normally pattern of yellow and black
2921 border.m_width=20;
2922 style.setBorders(libmwaw::TopBit, border);
2923
2924 style.setSurfaceColor(MWAWColor(0xff,0xff,0));
2925 style.m_shadowOffset=MWAWVec2f(3,3);
2926 style.setShadowColor(MWAWColor(0x80,0x80,0x80));
2927
2928 std::shared_ptr<MWAWSubDocument> doc(new MacDrawProParserInternal::SubDocument(*this, getInput(), shape.m_id));
2929 listener->insertTextBox(pos, doc, style);
2930 return true;
2931 }
2932 case MacDrawProParserInternal::Shape::Text: {
2933 std::shared_ptr<MWAWSubDocument> doc(new MacDrawProParserInternal::SubDocument(*this, getInput(), shape.m_id));
2934 listener->insertTextBox(pos, doc, shape.m_style);
2935 return true;
2936 }
2937 case MacDrawProParserInternal::Shape::Unknown:
2938 #if !defined(__clang__)
2939 default:
2940 #endif
2941 return false;
2942 }
2943 return true;
2944 }
2945
2946 bool MacDrawProParser::sendBitmap(MacDrawProParserInternal::Shape const &shape, MWAWPosition const &position)
2947 {
2948 MWAWGraphicListenerPtr listener=getGraphicListener();
2949 if (!listener) {
2950 MWAW_DEBUG_MSG(("MacDrawProParser::sendBitmap: can not find the listener\n"));
2951 return false;
2952 }
2953 if (!shape.m_bitmapEntry.valid()) return false;
2954 int const numBytesByRow=shape.m_numBytesByRow;
2955 int const numPixelByBytes=shape.m_bitmapIsColor ? 1 : 8;
2956 MWAWVec2i pictDim=shape.m_bitmapDim.size();
2957 if (shape.m_type!=MacDrawProParserInternal::Shape::Bitmap || numBytesByRow<=0 ||
2958 pictDim[0]<0 || pictDim[1]<0 ||
2959 numBytesByRow*shape.m_bitmapFileDim.size()[1]<shape.m_bitmapEntry.length() ||
2960 shape.m_bitmapDim[0][0]<0 || shape.m_bitmapDim[0][1]<0 ||
2961 shape.m_bitmapDim[0][0]<shape.m_bitmapFileDim[0][0] ||
2962 shape.m_bitmapFileDim.size()[0]<=0 || shape.m_bitmapFileDim.size()[1]<=0 ||
2963 numPixelByBytes*numBytesByRow<shape.m_bitmapFileDim.size()[0]) {
2964 MWAW_DEBUG_MSG(("MacDrawProParser::sendBitmap: the bitmap seems bad\n"));
2965 return false;
2966 }
2967 std::shared_ptr<MWAWPictBitmap> pict;
2968 if (shape.m_bitmapIsColor) {
2969 MWAWRSRCParserPtr rsrcParser=getRSRCParser();
2970 if (!rsrcParser) {
2971 MWAW_DEBUG_MSG(("MacDrawProParser::sendBitmap: need access to resource fork to read colormap\n"));
2972 return false;
2973 }
2974
2975 auto &entryMap = rsrcParser->getEntriesMap();
2976 auto it=entryMap.find("clut");
2977 MWAWEntry entry;
2978 while (it!=entryMap.end() && it->first=="clut") {
2979 if (it->second.id()==shape.m_bitmapClutId) {
2980 entry=it->second;
2981 break;
2982 }
2983 ++it;
2984 }
2985
2986 std::vector<MWAWColor> colList;
2987 if (!entry.valid() || !rsrcParser->parseClut(entry, colList) || colList.empty()) {
2988 MWAW_DEBUG_MSG(("MacDrawProParser::sendBitmap: can not read the color list\n"));
2989 return false;
2990 }
2991
2992 auto pictIndexed = std::make_shared<MWAWPictBitmapIndexed>(pictDim);
2993 pict = pictIndexed;
2994 pictIndexed->setColors(colList);
2995 std::vector<int> data;
2996 data.resize(size_t(pictDim[0]), 0);
2997
2998 MWAWInputStreamPtr input=getInput();
2999 input->seek(shape.m_bitmapEntry.begin(), librevenge::RVNG_SEEK_SET);
3000 auto numCol=int(colList.size());
3001 for (int r=shape.m_bitmapFileDim[0][1]; r<shape.m_bitmapFileDim[1][1]; ++r) {
3002 long pos=input->tell();
3003 if (r<shape.m_bitmapDim[0][1]||r>=shape.m_bitmapDim[1][1]) { // must not appear, but...
3004 input->seek(pos+numBytesByRow, librevenge::RVNG_SEEK_SET);
3005 continue;
3006 }
3007 int wPos=shape.m_bitmapDim[0][0]-shape.m_bitmapFileDim[0][0];
3008 for (int col=shape.m_bitmapFileDim[0][0]; col<shape.m_bitmapFileDim[1][0]; ++col) {
3009 auto c=static_cast<int>(input->readULong(1));
3010 if (wPos>=pictDim[0]) break;
3011 if (c>=numCol) {
3012 MWAW_DEBUG_MSG(("MacDrawProParser::sendBitmap: find some eroneous index, reset to 0\n"));
3013 c=0;
3014 }
3015 data[size_t(wPos++)]=c;
3016 }
3017 pictIndexed->setRow(r-shape.m_bitmapDim[0][1], &data[0]);
3018 input->seek(pos+numBytesByRow, librevenge::RVNG_SEEK_SET);
3019 }
3020 }
3021 else {
3022 // change: implement indexed transparent color, replaced this code
3023 auto pictColor = std::make_shared<MWAWPictBitmapColor>(pictDim, true);
3024 pict = pictColor;
3025 MWAWColor transparent(255,255,255,0);
3026 MWAWColor black(MWAWColor::black());
3027 std::vector<MWAWColor> data;
3028 data.resize(size_t(pictDim[0]), transparent);
3029 // first set unseen row to zero (even if this must not appear)
3030 for (int r=shape.m_bitmapDim[0][1]; r<shape.m_bitmapFileDim[0][1]; ++r) pictColor->setRow(r-shape.m_bitmapDim[0][1], &data[0]);
3031 for (int r=shape.m_bitmapFileDim[1][1]; r<shape.m_bitmapDim[1][1]; ++r) pictColor->setRow(r-shape.m_bitmapDim[0][1], &data[0]);
3032
3033 MWAWInputStreamPtr input=getInput();
3034 input->seek(shape.m_bitmapEntry.begin(), librevenge::RVNG_SEEK_SET);
3035 for (int r=shape.m_bitmapFileDim[0][1]; r<shape.m_bitmapFileDim[1][1]; ++r) {
3036 long pos=input->tell();
3037 if (r<shape.m_bitmapDim[0][1]||r>=shape.m_bitmapDim[1][1]) { // must not appear, but...
3038 input->seek(pos+numBytesByRow, librevenge::RVNG_SEEK_SET);
3039 continue;
3040 }
3041 int wPos=shape.m_bitmapDim[0][0]-shape.m_bitmapFileDim[0][0];
3042 for (int col=shape.m_bitmapFileDim[0][0]; col<shape.m_bitmapFileDim[1][0]; ++col) {
3043 auto c=static_cast<unsigned char>(input->readULong(1));
3044 for (int j=0, bit=0x80; j<8 ; ++j, bit>>=1) {
3045 if (wPos>=pictDim[0]) break;
3046 data[size_t(wPos++)]=(c&bit) ? black : transparent;
3047 }
3048 }
3049 pictColor->setRow(r-shape.m_bitmapDim[0][1], &data[0]);
3050 input->seek(pos+numBytesByRow, librevenge::RVNG_SEEK_SET);
3051 }
3052 }
3053
3054 MWAWEmbeddedObject picture;
3055 if (!pict || !pict->getBinary(picture)) return false;
3056 #ifdef DEBUG_WITH_FILES
3057 if (!picture.m_dataList.empty()) {
3058 static int volatile pictName = 0;
3059 libmwaw::DebugStream f;
3060 f << "PICT-" << ++pictName << ".png";
3061 libmwaw::Debug::dumpFile(picture.m_dataList[0], f.str().c_str());
3062 }
3063 #endif
3064
3065 // bitmap have no border
3066 MWAWGraphicStyle style=shape.m_style;
3067 style.m_lineWidth=0;
3068 listener->insertPicture(position, picture, style);
3069
3070 return true;
3071 }
3072
3073 bool MacDrawProParser::sendText(int zId)
3074 {
3075 int const vers=version();
3076 MWAWGraphicListenerPtr listener=getGraphicListener();
3077 if (!listener) {
3078 MWAW_DEBUG_MSG(("MacDrawProParser::sendText: can not find the listener\n"));
3079 return false;
3080 }
3081 if (zId<0||zId>=static_cast<int>(m_state->m_shapeList.size()) ||
3082 (m_state->m_shapeList[size_t(zId)].m_type != MacDrawProParserInternal::Shape::Text &&
3083 m_state->m_shapeList[size_t(zId)].m_type != MacDrawProParserInternal::Shape::Note)) {
3084 MWAW_DEBUG_MSG(("MacDrawProParser::sendText: can not find the text shape %d\n", zId));
3085 return false;
3086 }
3087 auto const &shape=m_state->m_shapeList[size_t(zId)];
3088 shape.m_isSent = true;
3089 if (shape.m_textZoneId<0 || shape.m_textZoneId>=int(m_state->m_objectTextList.size())) {
3090 MWAW_DEBUG_MSG(("MacDrawProParser::sendText: can not find the text zone %d\n", shape.m_textZoneId));
3091 return false;
3092 }
3093 MWAWEntry const &entry=m_state->m_objectTextList[size_t(shape.m_textZoneId)];
3094 entry.setParsed(true);
3095
3096 MWAWInputStreamPtr input=getInput();
3097 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
3098 libmwaw::DebugStream f;
3099 f << "Object[text]:shape=[" << shape << "],";
3100 auto numUsed=static_cast<int>(input->readULong(4));
3101 if (numUsed!=1) f << "numUsed=" << numUsed << ",";
3102 // skip size
3103 input->seek(4, librevenge::RVNG_SEEK_CUR);
3104 int N=shape.m_numChars;
3105 if (int(entry.length()-8)<N) {
3106 MWAW_DEBUG_MSG(("MacDrawProParser::sendText: the zone size seems short\n"));
3107 N=int(entry.length()-8);
3108 }
3109 long endPos=input->tell()+N;
3110 if (vers==0)
3111 listener->setParagraph(shape.m_paragraph);
3112 for (int i=0; i<N; ++i) {
3113 if (input->isEnd())
3114 break;
3115 if (vers>=0 && shape.m_paragraphMap.find(i)!=shape.m_paragraphMap.end()) {
3116 int id=shape.m_paragraphMap.find(i)->second;
3117 f << "[P" << id+1 << "]";
3118 MWAWParagraph para;
3119 if (id>=0 && !m_styleManager->getParagraph(id, para))
3120 f << "###";
3121 listener->setParagraph(para);
3122 }
3123 /* note: it is also possible to add a soft linebreak here when the
3124 previous character is not a EOL. Sometimes this is better but
3125 other times not, ie. if the line slightly overlaps the right
3126 border, we will end with some "empty" lines */
3127 if (shape.m_lineBreakSet.find(i)!=shape.m_lineBreakSet.end())
3128 f << "[L]";
3129 if (shape.m_fontMap.find(i)!=shape.m_fontMap.end()) {
3130 int id=shape.m_fontMap.find(i)->second;
3131 f << "[F" << id+1 << "]";
3132 MWAWFont font;
3133 if (id>=0 && !m_styleManager->getFont(id+1, font))
3134 f << "###";
3135 listener->setFont(font);
3136 }
3137 auto c = char(input->readULong(1));
3138 if (c==0) {
3139 MWAW_DEBUG_MSG(("MacDrawProParser::sendText: find char 0\n"));
3140 f << "#[0]";
3141 continue;
3142 }
3143 f << c;
3144 switch (c) {
3145 case 9:
3146 listener->insertTab();
3147 break;
3148 case 0xd:
3149 if (i+1!=N)
3150 listener->insertEOL();
3151 break;
3152 default:
3153 listener->insertCharacter(static_cast<unsigned char>(c), input, endPos);
3154 break;
3155 }
3156 }
3157 ascii().addPos(entry.begin());
3158 ascii().addNote(f.str().c_str());
3159 return true;
3160 }
3161
3162 bool MacDrawProParser::sendMeasure(MWAWEntry const &entry)
3163 {
3164 MWAWGraphicListenerPtr listener=getGraphicListener();
3165 if (!listener) {
3166 MWAW_DEBUG_MSG(("MacDrawProParser::sendMeasure: can not find the listener\n"));
3167 return false;
3168 }
3169 if (!entry.valid()) {
3170 MWAW_DEBUG_MSG(("MacDrawProParser::sendMeasure: can not find the measure entry\n"));
3171 return false;
3172 }
3173 MWAWInputStreamPtr input=getInput();
3174 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
3175 long endPos=entry.end();
3176 for (long i=0; i<entry.length(); ++i) {
3177 if (input->isEnd()) {
3178 MWAW_DEBUG_MSG(("MacDrawProParser::sendMeasure: the zone seems too short\n"));
3179 break;
3180 }
3181 auto c=char(input->readULong(1));
3182 if (c==0) {
3183 MWAW_DEBUG_MSG(("MacDrawProParser::sendText: find char 0\n"));
3184 continue;
3185 }
3186 switch (c) {
3187 case 9:
3188 listener->insertTab();
3189 break;
3190 case 0xd:
3191 listener->insertEOL();
3192 break;
3193 default:
3194 listener->insertCharacter(static_cast<unsigned char>(c), input, endPos);
3195 break;
3196 }
3197 }
3198 return true;
3199 }
3200
3201 void MacDrawProParser::flushExtra()
3202 {
3203 MWAWVec2f leftTop(72.f*float(getPageSpan().getMarginLeft()),72.f*float(getPageSpan().getMarginRight()));
3204 for (auto const &shape : m_state->m_shapeList) {
3205 if (shape.m_isSent || shape.m_type==MacDrawProParserInternal::Shape::GroupEnd)
3206 continue;
3207 static bool first=true;
3208 if (first) {
3209 MWAW_DEBUG_MSG(("MacDrawProParser::flushExtra: find some unsent zone\n"));
3210 first=false;
3211 }
3212 send(shape, leftTop);
3213 }
3214 libmwaw::DebugStream f;
3215 int n=0;
3216 for (auto const &entry : m_state->m_objectTextList) {
3217 ++n;
3218 if (!entry.valid() || entry.isParsed())
3219 continue;
3220 static bool first=false;
3221 if (first) {
3222 MWAW_DEBUG_MSG(("MacDrawProParser::flushExtra: find some unparsed text's object zones\n"));
3223 first=false;
3224 }
3225 f.str("");
3226 f << "Entries(ObjText)[" << n-1 << "]:###unparsed";
3227 ascii().addPos(entry.begin());
3228 ascii().addNote(f.str().c_str());
3229 }
3230 }
3231 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: