Debian Codegraph @

src/lib/MacDraftParser.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 <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWGraphicListener.hxx"
43 #include "MWAWGraphicShape.hxx"
44 #include "MWAWGraphicStyle.hxx"
45 #include "MWAWHeader.hxx"
46 #include "MWAWParagraph.hxx"
47 #include "MWAWPictBitmap.hxx"
48 #include "MWAWPictData.hxx"
49 #include "MWAWPrinter.hxx"
50 #include "MWAWPosition.hxx"
51 #include "MWAWSubDocument.hxx"
52
53 #include "MacDraftParser.hxx"
54
55 /** Internal: the structures of a MacDraftParser */
56 namespace MacDraftParserInternal
57 {
58 //! low level: bitmap file position in a MacDraft file
59 struct BitmapFileData {
60 //! constructor
61 BitmapFileData()
62 : m_id(0)
63 , m_rowSize(0)
64 , m_dimension()
65 , m_entry()
66 {
67 }
68 //! check is the entry is coherent
69 bool ok() const
70 {
71 return m_dimension.size()[0]>=0 && m_dimension.size()[1]>=0 &&
72 m_rowSize>=0 && long(m_rowSize)*8>=m_dimension.size()[1] &&
73 long(m_rowSize)*m_dimension.size()[1]<=m_entry.length();
74 }
75 //! the bitmap id
76 unsigned long m_id;
77 //! the row size
78 int m_rowSize;
79 //! the bitmap dimension
80 MWAWBox2i m_dimension;
81 //! the data position
82 MWAWEntry m_entry;
83 };
84
85 //! generic class used to store shape in MWAWDraftParser
86 struct Shape {
87 //! the different shape
88 enum Type { Basic, Bitmap, Group, Label, Text, Unknown };
89
90 //! constructor
91 Shape()
92 : m_type(Unknown)
93 , m_box()
94 , m_origin()
95 , m_style()
96 , m_patternId(-1)
97 , m_shape()
98 , m_isLine(false)
99 , m_id(-1)
100 , m_nextId(-1)
101 , m_font()
102 , m_paragraph()
103 , m_textEntry()
104 , m_labelWidth(0)
105 , m_childList()
106 , m_bitmapIdList()
107 , m_bitmapDimensionList()
108 , m_isSent(false)
109 {
110 }
111
112 //! return the shape bdbox
113 MWAWBox2f getBdBox() const
114 {
115 return m_type==Basic ? m_shape.getBdBox() : m_box;
116 }
117 //! translate a shape
118 void translate(MWAWVec2f const &dir)
119 {
120 if (m_type==Basic)
121 m_shape.translate(dir);
122 m_box=MWAWBox2f(m_box[0]+dir, m_box[1]+dir);
123 m_origin=m_origin+dir;
124 }
125 //! transform a shape
126 void transform(float rotate, bool flipX, MWAWVec2f const &center)
127 {
128 if (rotate<0 || rotate>0) {
129 if (m_type==Basic) {
130 m_shape=m_shape.rotate(rotate, center);
131 m_box=m_shape.getBdBox();
132 }
133 else {
134 if (m_type==Text) // the textbox is the rotated textbox, so first compute the normal box
135 m_box=rotateBox(m_box, -m_style.m_rotate*float(M_PI/180.), m_box.center());
136 if (m_type!=Label) // the label seems always horizontal
137 m_style.m_rotate+=rotate;
138 float angle=rotate*float(M_PI/180.);
139 m_box=rotateBox(m_box, angle, center);
140 MWAWVec2f decal=center-MWAWVec2f(std::cos(angle)*center[0]-std::sin(angle)*center[1],
141 std::sin(angle)*center[0]+std::cos(angle)*center[1]);
142 m_origin=MWAWVec2f(std::cos(angle)*m_origin[0]-std::sin(angle)*m_origin[1],
143 std::sin(angle)*m_origin[0]+std::cos(angle)*m_origin[1])+decal;
144 if (m_type==Text) // now, we can compute the final rotated textbox
145 m_box=rotateBox(m_box, m_style.m_rotate*float(M_PI/180.), m_box.center());
146 }
147 }
148 if (flipX) {
149 if (m_type==Basic) {
150 m_shape.scale(MWAWVec2f(-1,1));
151 m_shape.translate(MWAWVec2f(2*center[0],0));
152 }
153 m_style.m_flip[0]=!m_style.m_flip[0];
154 m_box=MWAWBox2f(MWAWVec2f(2*center[0]-m_box[1][0],m_box[0][1]), MWAWVec2f(2*center[0]-m_box[0][0],m_box[1][1]));
155 m_origin=MWAWVec2f(2*center[0]-m_origin[0],m_origin[1]);
156 }
157 }
158 //! returns the rotation of a box
159 static MWAWBox2f rotateBox(MWAWBox2f const &box, float angle, MWAWVec2f const &center)
160 {
161 MWAWVec2f decal=center-MWAWVec2f(std::cos(angle)*center[0]-std::sin(angle)*center[1],
162 std::sin(angle)*center[0]+std::cos(angle)*center[1]);
163 MWAWBox2f fBox;
164 for (int i=0; i < 4; ++i) {
165 MWAWVec2f pt=MWAWVec2f(box[i%2][0],box[i/2][1]);
166 pt = MWAWVec2f(std::cos(angle)*pt[0]-std::sin(angle)*pt[1],
167 std::sin(angle)*pt[0]+std::cos(angle)*pt[1])+decal;
168 if (i==0) fBox=MWAWBox2f(pt,pt);
169 else fBox=fBox.getUnion(MWAWBox2f(pt,pt));
170 }
171
172 return fBox;
173 }
174 //! the graphic type
175 Type m_type;
176 //! the shape bdbox
177 MWAWBox2f m_box;
178 //! the shape origin
179 MWAWVec2f m_origin;
180 //! the graphic style
181 MWAWGraphicStyle m_style;
182 //! the pattern id
183 int m_patternId;
184 //! the graphic shape ( for basic geometric form )
185 MWAWGraphicShape m_shape;
186 //! flag to know if the shape is a line
187 bool m_isLine;
188 //! the shape id
189 int m_id;
190 //! the following id (if set)
191 int m_nextId;
192 //! the font ( for a text box)
193 MWAWFont m_font;
194 //! the paragraph ( for a text box)
195 MWAWParagraph m_paragraph;
196 //! the textbox entry (main text)
197 MWAWEntry m_textEntry;
198 //! the 1D label width in point
199 float m_labelWidth;
200 //! the child list ( for a group )
201 std::vector<size_t> m_childList;
202 //! the list of bitmap id ( for a bitmap)
203 std::vector<unsigned long> m_bitmapIdList;
204 //! the list of bitmap dimension ( for a bitmap)
205 std::vector<MWAWBox2i> m_bitmapDimensionList;
206 //! a flag used to know if the object is sent to the listener or not
207 mutable bool m_isSent;
208 };
209
210 ////////////////////////////////////////
211 //! Internal: the state of a MacDraftParser
212 struct State {
213 //! constructor
214 State()
215 : m_version(0)
216 , m_patternList()
217 , m_shapeList()
218 , m_idToBitmapMap()
219 {
220 }
221 //! returns a pattern if posible
222 bool getPattern(int id, MWAWGraphicStyle::Pattern &pat)
223 {
224 if (m_patternList.empty()) initPatterns();
225 if (id<=0 || id>=int(m_patternList.size())) {
226 MWAW_DEBUG_MSG(("MacDraftParserInternal::getPattern: can not find pattern %d\n", id));
227 return false;
228 }
229 pat=m_patternList[size_t(id)];
230 return true;
231 }
232
233 //! init the patterns list
234 void initPatterns();
235 //! the file version
236 int m_version;
237 //! the patterns list
238 std::vector<MWAWGraphicStyle::Pattern> m_patternList;
239 //! the shapes list
240 std::vector<Shape> m_shapeList;
241 //! map id to bitmap file data
242 std::map<unsigned long, BitmapFileData> m_idToBitmapMap;
243 };
244
245 void State::initPatterns()
246 {
247 if (!m_patternList.empty()) return;
248 for (int i=0; i<64; ++i) {
249 static uint16_t const patterns[] = {
250 0x0, 0x0, 0x0, 0x0, 0x40, 0x400, 0x10, 0x100, 0x8040, 0x2010, 0x804, 0x201, 0x102, 0x408, 0x1020, 0x4080,
251 0x0, 0x0, 0x0, 0x0, 0x842, 0x90, 0x440, 0x1001, 0xe070, 0x381c, 0xe07, 0x83c1, 0x8307, 0xe1c, 0x3870, 0xe0c1,
252 0x8000, 0x0, 0x800, 0x0, 0x42a, 0x4025, 0x251, 0x2442, 0x4422, 0x88, 0x4422, 0x88, 0x1122, 0x4400, 0x1122, 0x4400,
253 0x8000, 0x800, 0x8000, 0x800, 0x4aa4, 0x8852, 0x843a, 0x4411, 0x8844, 0x2211, 0x8844, 0x2211, 0x1122, 0x4488, 0x1122, 0x4488,
254 0x8800, 0x2200, 0x8800, 0x2200, 0x4cd2, 0x532d, 0x9659, 0x46b3, 0x99cc, 0x6633, 0x99cc, 0x6633, 0x3366, 0xcc99, 0x3366, 0xcc99,
255 0x8822, 0x8822, 0x8822, 0x8822, 0xdbbe, 0xedbb, 0xfeab, 0xbeeb, 0xcc00, 0x0, 0x3300, 0x0, 0x101, 0x1010, 0x101, 0x1010,
256 0xaa55, 0xaa55, 0xaa55, 0xaa55, 0xf7bd, 0xff6f, 0xfbbf, 0xeffe, 0x2040, 0x8000, 0x804, 0x200, 0x40a0, 0x0, 0x40a, 0x0,
257 0x77dd, 0x77dd, 0x77dd, 0x77dd, 0x8244, 0x3944, 0x8201, 0x101, 0xff00, 0x0, 0xff00, 0x0, 0x8888, 0x8888, 0x8888, 0x8888,
258 0xffff, 0xffff, 0xffff, 0xffff, 0x8142, 0x3c18, 0x183c, 0x4281, 0xb130, 0x31b, 0xb8c0, 0xc8d, 0x6c92, 0x8282, 0x4428, 0x1000,
259 0xff80, 0x8080, 0xff80, 0x8080, 0x8142, 0x2418, 0x1020, 0x4080, 0xff80, 0x8080, 0xff08, 0x808, 0x8080, 0x413e, 0x808, 0x14e3,
260 0xff88, 0x8888, 0xff88, 0x8888, 0xff80, 0x8080, 0x8080, 0x8080, 0xbf00, 0xbfbf, 0xb0b0, 0xb0b0, 0xaa00, 0x8000, 0x8800, 0x8000,
261 0xaa44, 0xaa11, 0xaa44, 0xaa11, 0x8244, 0x2810, 0x2844, 0x8201, 0x8, 0x142a, 0x552a, 0x1408, 0x1038, 0x7cfe, 0x7c38, 0x1000,
262 0x1020, 0x54aa, 0xff02, 0x408, 0x8080, 0x8080, 0x8094, 0xaa55, 0x804, 0x2a55, 0xff40, 0x2010, 0x7789, 0x8f8f, 0x7798, 0xf8f8,
263 0x8814, 0x2241, 0x8800, 0xaa00, 0x77eb, 0xddbe, 0x77ff, 0x55ff, 0x1022, 0x408a, 0x4022, 0x108a, 0xefdd, 0xbf75, 0xbfdd, 0xef75,
264 0x9f90, 0x909f, 0xf909, 0x9f9, 0xf078, 0x2442, 0x870f, 0x1221, 0xfe82, 0xfeee, 0xef28, 0xefee, 0xf9fc, 0x664f, 0x9f3f, 0x66f3,
265 0xaf5f, 0xaf5f, 0xd0b, 0xd0b, 0xa011, 0xa1c, 0x2844, 0x82c1, 0xf0f0, 0xf0f0, 0xf0f, 0xf0f, 0xc864, 0x3219, 0x9923, 0x468c
266 };
267
268 MWAWGraphicStyle::Pattern pat;
269 pat.m_dim=MWAWVec2i(8,8);
270 pat.m_data.resize(8);
271 pat.m_colors[0]=MWAWColor::white();
272 pat.m_colors[1]=MWAWColor::black();
273 uint16_t const *patPtr=&patterns[4*i];
274 for (size_t j=0; j<8; j+=2, ++patPtr) {
275 pat.m_data[j]=uint8_t((*patPtr)>>8);
276 pat.m_data[j+1]=uint8_t((*patPtr)&0xFF);
277 }
278 m_patternList.push_back(pat);
279 }
280 }
281 ////////////////////////////////////////
282 //! Internal: the subdocument of a MacDraftParser
283 class SubDocument final : public MWAWSubDocument
284 {
285 public:
286 SubDocument(MacDraftParser &pars, MWAWInputStreamPtr const &input, int zoneId)
287 : MWAWSubDocument(&pars, input, MWAWEntry())
288 , m_id(zoneId)
289 {
290 }
291
292 //! destructor
293 ~SubDocument() final {}
294
295 //! operator!=
296 bool operator!=(MWAWSubDocument const &doc) const final
297 {
298 if (MWAWSubDocument::operator!=(doc)) return true;
299 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
300 if (!sDoc) return true;
301 if (m_id != sDoc->m_id) return true;
302 return false;
303 }
304
305 //! the parser function
306 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
307
308 protected:
309 //! the subdocument id
310 int m_id;
311 private:
312 SubDocument(SubDocument const &orig) = delete;
313 SubDocument &operator=(SubDocument const &orig) = delete;
314 };
315
316 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
317 {
318 if (!listener || !listener->canWriteText()) {
319 MWAW_DEBUG_MSG(("MacDraftParserInternal::SubDocument::parse: no listener\n"));
320 return;
321 }
322 auto *parser=dynamic_cast<MacDraftParser *>(m_parser);
323 if (!parser) {
324 MWAW_DEBUG_MSG(("MacDraftParserInternal::SubDocument::parse: no parser\n"));
325 return;
326 }
327 long pos = m_input->tell();
328 parser->sendText(m_id);
329 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
330 }
331
332 }
333
334 ////////////////////////////////////////////////////////////
335 // constructor/destructor, ...
336 ////////////////////////////////////////////////////////////
337 MacDraftParser::MacDraftParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
338 : MWAWGraphicParser(input, rsrcParser, header)
339 , m_state()
340 {
341 init();
342 }
343
344 MacDraftParser::~MacDraftParser()
345 {
346 }
347
348 void MacDraftParser::init()
349 {
350 resetGraphicListener();
351 setAsciiName("main-1");
352
353 m_state.reset(new MacDraftParserInternal::State);
354
355 getPageSpan().setMargins(0.1);
356 }
357
358 ////////////////////////////////////////////////////////////
359 // the parser
360 ////////////////////////////////////////////////////////////
361 void MacDraftParser::parse(librevenge::RVNGDrawingInterface *docInterface)
362 {
363 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
364 bool ok = false;
365 try {
366 // create the asciiFile
367 ascii().setStream(getInput());
368 ascii().open(asciiName());
369 checkHeader(nullptr);
370 ok = createZones();
371 if (ok) {
372 createDocument(docInterface);
373 // reimplement me, ie. we may loose some shape if the chain is breaked
374 for (size_t i=0; i<m_state->m_shapeList.size(); ++i) {
375 MacDraftParserInternal::Shape const &shape=m_state->m_shapeList[i];
376 if (shape.m_isSent) continue;
377 send(shape);
378 if (shape.m_nextId>0 && shape.m_nextId>int(i))
379 i=size_t(shape.m_nextId-1);
380 }
381 }
382 ascii().reset();
383 }
384 catch (...) {
385 MWAW_DEBUG_MSG(("MacDraftParser::parse: exception catched when parsing\n"));
386 ok = false;
387 }
388
389 resetGraphicListener();
390 if (!ok) throw(libmwaw::ParseException());
391 }
392
393 ////////////////////////////////////////////////////////////
394 // create the document
395 ////////////////////////////////////////////////////////////
396 void MacDraftParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
397 {
398 if (!documentInterface) return;
399 if (getGraphicListener()) {
400 MWAW_DEBUG_MSG(("MacDraftParser::createDocument: listener already exist\n"));
401 return;
402 }
403
404 // create the page list
405 MWAWPageSpan ps(getPageSpan());
406 ps.setPageSpan(1);
407 std::vector<MWAWPageSpan> pageList(1,ps);
408 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
409 setGraphicListener(listen);
410 listen->startDocument();
411 }
412
413 ////////////////////////////////////////////////////////////
414 //
415 // Intermediate level
416 //
417 ////////////////////////////////////////////////////////////
418 bool MacDraftParser::createZones()
419 {
420 if (!readDocHeader())
421 return false;
422 MWAWInputStreamPtr input = getInput();
423
424 // first the list of patterns (often empty)
425 while (readPattern()) {
426 }
427 long pos=input->tell();
428 auto fSz=long(input->readULong(2));
429 if (fSz==0) {
430 ascii().addPos(pos);
431 ascii().addNote("Patterns[end]:");
432 }
433 else {
434 MWAW_DEBUG_MSG(("MacDraftParser::createZones: can not find end patterns zone\n"));
435 ascii().addPos(pos);
436 ascii().addNote("###");
437 input->seek(pos,librevenge::RVNG_SEEK_SET);
438 }
439 // bitmap data
440 if (m_state->m_version==1) {
441 while (readBitmapData()) {
442 }
443 pos=input->tell();
444 fSz=long(input->readULong(2));
445 if (fSz==0) {
446 ascii().addPos(pos);
447 ascii().addNote("BitmapData[end]:");
448 }
449 else {
450 MWAW_DEBUG_MSG(("MacDraftParser::createZones: can not find end bitmap data zone\n"));
451 ascii().addPos(pos);
452 ascii().addNote("###");
453 input->seek(pos,librevenge::RVNG_SEEK_SET);
454 }
455 }
456
457 // second the list of objects (can be empty)
458 while (readObject()) {
459 }
460
461 pos=input->tell();
462 fSz=long(input->readULong(2));
463 if (fSz==0) {
464 ascii().addPos(pos);
465 ascii().addNote("Object[end]:");
466 }
467 else {
468 MWAW_DEBUG_MSG(("MacDraftParser::createZones: can not find end object zone\n"));
469 ascii().addPos(pos);
470 ascii().addNote("###");
471 input->seek(pos,librevenge::RVNG_SEEK_SET);
472 }
473
474 // never seems
475 for (int i=0; i<2; ++i) {
476 while (!input->isEnd()) {
477 pos=input->tell();
478 fSz=long(input->readULong(2));
479 if (!input->checkPosition(pos+2+fSz)) {
480 input->seek(pos,librevenge::RVNG_SEEK_SET);
481 break;
482 }
483 if (fSz==0) {
484 ascii().addPos(pos);
485 ascii().addNote("_");
486 break;
487 }
488 else {
489 MWAW_DEBUG_MSG(("MacDraftParser::createZones: find some unknown zone\n"));
490 ascii().addPos(pos);
491 ascii().addNote(i==0 ? "Entries(ZoneA):" : "Entries(ZoneB):");
492 input->seek(pos+2+fSz,librevenge::RVNG_SEEK_SET);
493 }
494 }
495 }
496 if (m_state->m_version==1) {
497 if (input->isEnd())
498 return true;
499 MWAW_DEBUG_MSG(("MacDraftParser::createZones: find some extra zone\n"));
500 ascii().addPos(pos);
501 ascii().addNote("Entries(Extra):###");
502 return !m_state->m_shapeList.empty();
503 }
504 // bitmap data
505 while (readBitmapData()) {
506 }
507 pos=input->tell();
508 fSz=long(input->readULong(2));
509 if (fSz==0) {
510 ascii().addPos(pos);
511 ascii().addNote("BitmapData[end]:");
512 }
513 else {
514 MWAW_DEBUG_MSG(("MacDraftParser::createZones: can not find end bitmap data zone\n"));
515 ascii().addPos(pos);
516 ascii().addNote("###");
517 input->seek(pos,librevenge::RVNG_SEEK_SET);
518 }
519
520 // modified patterns list
521 while (readPattern()) {
522 }
523 pos=input->tell();
524 fSz=long(input->readULong(2));
525 if (fSz==0) {
526 ascii().addPos(pos);
527 ascii().addNote("Patterns[end]:");
528 }
529 else {
530 MWAW_DEBUG_MSG(("MacDraftParser::createZones: can not find end of pattern zone\n"));
531 ascii().addPos(pos);
532 ascii().addNote("Entries(Extra):###");
533 return !m_state->m_shapeList.empty();
534 }
535
536 if (input->isEnd())
537 return !m_state->m_shapeList.empty();
538 pos=input->tell();
539 fSz=long(input->readULong(2));
540 input->seek(pos,librevenge::RVNG_SEEK_SET);
541 if (fSz==0) {
542 ascii().addPos(pos);
543 ascii().addNote("_");
544 return true;
545 }
546 else if (fSz==0x78) {
547 if (!readPrintInfo()) {
548 ascii().addPos(pos);
549 ascii().addNote("Entries(PrintInfo):#");
550 }
551 input->seek(pos+2+fSz,librevenge::RVNG_SEEK_SET);
552 }
553
554 if (!input->isEnd()) {
555 MWAW_DEBUG_MSG(("MacDraftParser::createZones: find some extra zone\n"));
556 ascii().addPos(input->tell());
557 ascii().addNote("Entries(Extra):###");
558 }
559 return true;
560 }
561
562 bool MacDraftParser::readZone()
563 {
564 MWAWInputStreamPtr input = getInput();
565 if (input->isEnd())
566 return false;
567 long pos=input->tell();
568 auto fSz=long(input->readULong(2));
569 if (fSz==0) {
570 ascii().addPos(pos);
571 ascii().addNote("_");
572 return true;
573 }
574 input->seek(pos,librevenge::RVNG_SEEK_SET);
575 if (fSz==0x1e)
576 return readPattern();
577 if (fSz==0x78 && readPrintInfo())
578 return true;
579 if (!input->checkPosition(pos+2+fSz)) {
580 input->seek(pos,librevenge::RVNG_SEEK_SET);
581 return false;
582 }
583 ascii().addPos(pos);
584 ascii().addNote("Entries(UnknZone):");
585 input->seek(pos+2+fSz,librevenge::RVNG_SEEK_SET);
586
587 return true;
588 }
589
590 ////////////////////////////////////////////////////////////
591 // read an object
592 ////////////////////////////////////////////////////////////
593 bool MacDraftParser::readObject()
594 {
595 MWAWInputStreamPtr input = getInput();
596 if (input->isEnd())
597 return false;
598 long pos=input->tell();
599 auto fSz=long(input->readULong(2));
600 if (fSz==0x1a) {
601 input->seek(pos,librevenge::RVNG_SEEK_SET);
602 return readLabel();
603 }
604 long endPos=pos+2+fSz;
605 if (fSz<0x20 || !input->checkPosition(endPos)) {
606 input->seek(pos,librevenge::RVNG_SEEK_SET);
607 return false;
608 }
609 libmwaw::DebugStream f;
610 f << "Entries(Object):";
611 int val;
612 for (int i=0; i<2; ++i) {
613 val=static_cast<int>(input->readULong(4));
614 if (val) f << "id" << i << "=" << std::hex << val << std::dec << ",";
615 }
616 size_t shapeId= m_state->m_shapeList.size();
617 MacDraftParserInternal::Shape shape;
618 shape.m_id=static_cast<int>(shapeId);
619 shape.m_nextId=shape.m_id+1; // default value
620 unsigned long flag=input->readULong(4);
621 switch ((flag>>3)&7) {
622 case 0:
623 break;
624 case 1:
625 shape.m_style.m_arrows[1]=MWAWGraphicStyle::Arrow::plain();
626 f << "->,";
627 break;
628 case 2:
629 shape.m_style.m_arrows[0]=MWAWGraphicStyle::Arrow::plain();
630 f << "<-,";
631 break;
632 case 3:
633 shape.m_style.m_arrows[0]=shape.m_style.m_arrows[1]=MWAWGraphicStyle::Arrow::plain();
634 f << "<->,";
635 break;
636 case 4:
637 shape.m_style.m_arrows[0]=shape.m_style.m_arrows[1]=MWAWGraphicStyle::Arrow::plain();
638 f << "<->+length,";
639 break;
640 default:
641 f << "##arrow=" << ((flag>>3)&7) << ",";
642 }
643 switch ((flag>>9)&0x3) {
644 case 0:
645 f << "line[inside],";
646 break;
647 case 1: // centered
648 break;
649 case 2:
650 f << "line[outside],";
651 break;
652 default:
653 f << "line[pos]=##3,";
654 }
655 shape.m_patternId=(flag>>11)&0x3f;
656 if (shape.m_patternId)
657 f << "pat=" << shape.m_patternId << ",";
658 else
659 shape.m_style.m_surfaceOpacity=0;
660 if (flag&0x40) {
661 shape.m_style.m_lineWidth=0.; // or maybe dotted
662 f << "hairline,";
663 }
664 else {
665 shape.m_style.m_lineWidth=float((flag>>23)&7)+1;
666 f << "width=" << ((flag>>23)&7)+1 << ",";
667 }
668 if (flag&0x80000000)
669 f << "select,";
670 flag &= 0x7c7e0187;
671
672 if (flag>>16) f << "fl0[h]=" << std::hex << (flag>>16) << std::dec << ",";
673 if (flag&0xFFFF) f << "fl0[l]=" << std::hex << (flag&0xFFFF) << std::dec << ",";
674
675 val=static_cast<int>(input->readULong(2));
676 if (val&0x8000) f << "lock,";
677 val &= 0x7fff;
678 if (val) f << "fl1=" << std::hex << val << std::dec << ",";
679
680 val=static_cast<int>(input->readULong(2));
681 bool flipX=false;
682 float rotate=0;
683 if (val&0xfc00)
684 f << "rot[h]=" << std::hex << (val>>10) << std::dec << ",";
685 if (val&0x200) {
686 flipX=true;
687 f << "flipX,";
688 }
689 val &=0x1ff;
690 if (val) {
691 rotate=float(val);
692 f << "rot=" << val << ",";
693 }
694 float dim[4];
695 for (auto &d : dim) d=float(input->readLong(2))/8.f;
696 shape.m_box=MWAWBox2f(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2]));
697 f << "dim=" << shape.m_box << ",";
698 for (int i=0; i<2; ++i) dim[i]=float(input->readLong(2))/8.f;
699 MWAWVec2f origin=MWAWVec2f(dim[1],dim[0]);
700 f << "orig=" << origin << ",";
701 switch (fSz) {
702 case 0x28:
703 case 0x2c:
704 case 0x30:
705 case 0x38: {
706 shape.m_type=MacDraftParserInternal::Shape::Basic;
707 val=static_cast<int>(input->readULong(4));
708 if (val) f << "id?=" << std::hex << val << std::dec << ",";
709 for (auto &d : dim) d=float(input->readLong(2))/8.f;
710 MWAWBox2f box(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2]));
711 f << "dim1=" << box << ",";
712 if (fSz==0x28) {
713 if (flag&0x8000000) {
714 shape.m_shape=MWAWGraphicShape::circle(box);
715 f << "ellipse,";
716 }
717 else {
718 shape.m_shape=MWAWGraphicShape::rectangle(box);
719 f << "rect,";
720 }
721 break;
722 }
723 else if (fSz==0x2c) {
724 shape.m_shape=MWAWGraphicShape::rectangle(box);
725 f << "rectOval,";
726 }
727 else if (fSz==0x30) {
728 if ((flag&0x28000000)==0x28000000) {
729 shape.m_shape=MWAWGraphicShape::circle(box);
730 f << "circle,";
731 }
732 else if ((flag&0x8000000)==0x8000000) {
733 shape.m_type=MacDraftParserInternal::Shape::Bitmap;
734 f << "bitmap,";
735 }
736 else {
737 shape.m_type=MacDraftParserInternal::Shape::Group;
738 f << "group,";
739 }
740 }
741 else if (fSz==0x38)
742 f << "pie,";
743 else {
744 // must not appear
745 static bool first=true;
746 if (first) {
747 MWAW_DEBUG_MSG(("MacDraftParser::readObject: find unexpected data\n"));
748 first=false;
749 }
750 f << "Bitmap###,";
751 }
752 if (fSz==0x30) {
753 val=static_cast<int>(input->readULong(4));
754 if (val)
755 f << "id2=" << std::hex << val << std::dec << ",";
756 }
757 else if (fSz==0x38) {
758 int fileAngle[2];
759 for (auto &angle : fileAngle) angle=static_cast<int>(input->readLong(2));
760 f << "angle=" << fileAngle[0] << "x" << fileAngle[0]+fileAngle[1] << ",";
761 if (fileAngle[1]<0) {
762 fileAngle[0]+=fileAngle[1];
763 fileAngle[1]*=-1;
764 }
765 int angle[2] = { 90-fileAngle[0]-fileAngle[1], 90-fileAngle[0] };
766 if (angle[1]>360) {
767 int numLoop=int(angle[1]/360)-1;
768 angle[0]-=numLoop*360;
769 angle[1]-=numLoop*360;
770 while (angle[1] > 360) {
771 angle[0]-=360;
772 angle[1]-=360;
773 }
774 }
775 if (angle[0] < -360) {
776 int numLoop=int(angle[0]/360)+1;
777 angle[0]-=numLoop*360;
778 angle[1]-=numLoop*360;
779 while (angle[0] < -360) {
780 angle[0]+=360;
781 angle[1]+=360;
782 }
783 }
784 MWAWVec2f axis = 0.5f*box.size();
785 // we must compute the real bd box
786 float minVal[2] = { 0, 0 }, maxVal[2] = { 0, 0 };
787 int limitAngle[2];
788 for (int i = 0; i < 2; i++)
789 limitAngle[i] = (angle[i] < 0) ? int(angle[i]/90)-1 : int(angle[i]/90);
790 for (int bord = limitAngle[0]; bord <= limitAngle[1]+1; bord++) {
791 float ang = (bord == limitAngle[0]) ? float(angle[0]) :
792 (bord == limitAngle[1]+1) ? float(angle[1]) : float(90 * bord);
793 ang *= float(M_PI/180.);
794 float const actVal[2] = { axis[0] *std::cos(ang), -axis[1] *std::sin(ang)};
795 if (actVal[0] < minVal[0]) minVal[0] = actVal[0];
796 else if (actVal[0] > maxVal[0]) maxVal[0] = actVal[0];
797 if (actVal[1] < minVal[1]) minVal[1] = actVal[1];
798 else if (actVal[1] > maxVal[1]) maxVal[1] = actVal[1];
799 }
800 MWAWVec2f center = box.center();
801 MWAWBox2f realBox(MWAWVec2f(center[0]+minVal[0],center[1]+minVal[1]),
802 MWAWVec2f(center[0]+maxVal[0],center[1]+maxVal[1]));
803 shape.m_box=MWAWBox2f(MWAWVec2f(shape.m_box[0])+realBox[0],MWAWVec2f(shape.m_box[0])+realBox[1]);
804 if (shape.m_patternId!=0)
805 shape.m_shape = MWAWGraphicShape::pie(realBox, box, MWAWVec2f(float(angle[0]),float(angle[1])));
806 else
807 shape.m_shape = MWAWGraphicShape::arc(realBox, box, MWAWVec2f(float(angle[0]),float(angle[1])));
808
809 for (int i=0; i<2; ++i) { // always 0
810 val=static_cast<int>(input->readULong(2));
811 if (val) f << "f" << i+2 << "=" << std::hex << val << std::dec << ",";
812 }
813 for (int i=0; i<2; ++i) dim[i]=float(input->readLong(2))/8.f;
814 f << "sz=" << MWAWVec2f(dim[1],dim[0]) << ",";
815 }
816 for (int i=0; i<2; ++i) dim[i]=float(input->readLong(2))/8.f;
817 if (fSz==0x2c)
818 shape.m_shape.m_cornerWidth=MWAWVec2f(dim[1]/2.f,dim[0]/2.f);
819 f << "corner=" << MWAWVec2f(dim[1],dim[0]) << ",";
820 if (shape.m_type!=MacDraftParserInternal::Shape::Group && shape.m_type!=MacDraftParserInternal::Shape::Bitmap)
821 break;
822 if (input->tell()!=endPos)
823 ascii().addDelimiter(input->tell(),'|');
824 ascii().addPos(pos);
825 ascii().addNote(f.str().c_str());
826 input->seek(endPos,librevenge::RVNG_SEEK_SET);
827
828 if (shape.m_type==MacDraftParserInternal::Shape::Bitmap) {
829 while (!input->isEnd()) {
830 pos=input->tell();
831 if (input->readULong(2)==0) {
832 ascii().addPos(pos);
833 ascii().addNote("Bitmap[end]");
834 break;
835 }
836 input->seek(pos, librevenge::RVNG_SEEK_SET);
837 if (!readBitmapDefinition(shape)) {
838 MWAW_DEBUG_MSG(("MacDraftParser::readObject: can not find end of bitmap\n"));
839 input->seek(pos, librevenge::RVNG_SEEK_SET);
840 ascii().addPos(pos);
841 ascii().addNote("Bitmap###");
842 break;
843 }
844 }
845 m_state->m_shapeList.push_back(shape);
846 return true;
847 }
848
849 m_state->m_shapeList.push_back(shape);
850 while (!input->isEnd()) {
851 pos=input->tell();
852 if (input->readULong(2)==0) {
853 ascii().addPos(pos);
854 ascii().addNote("Object[endGroup]");
855 break;
856 }
857 input->seek(pos, librevenge::RVNG_SEEK_SET);
858 size_t cId=m_state->m_shapeList.size();
859 if (!readObject()) {
860 MWAW_DEBUG_MSG(("MacDraftParser::readObject: can not find end of group\n"));
861 input->seek(pos, librevenge::RVNG_SEEK_SET);
862 break;
863 }
864 m_state->m_shapeList[shapeId].m_childList.push_back(size_t(cId));
865 }
866 m_state->m_shapeList[shapeId].m_nextId=static_cast<int>(m_state->m_shapeList.size());
867 // times to apply transformation on child
868 if (origin!=MWAWVec2f(0,0)) {
869 for (int i=m_state->m_shapeList[shapeId].m_id+1; i<m_state->m_shapeList[shapeId].m_nextId; ++i)
870 m_state->m_shapeList[size_t(i)].translate(origin);
871 }
872 if (rotate<0 || rotate>0 || flipX) {
873 MWAWVec2f center=origin+box.center();
874 for (int i=m_state->m_shapeList[shapeId].m_id+1; i<m_state->m_shapeList[shapeId].m_nextId; ++i)
875 m_state->m_shapeList[size_t(i)].transform(rotate,flipX,center);
876 }
877 return true;
878 }
879 case 0x32: {
880 f << "line,";
881 val=static_cast<int>(input->readULong(4));
882 if (val) f << "pat[id]=" << std::hex << val << ",";
883 for (int i=0; i<2; ++i) { // 0
884 val=static_cast<int>(input->readULong(2));
885 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
886 }
887 for (int i=0; i<2; ++i) dim[i]=float(input->readLong(2))/8.f;
888 if (dim[0]<0 || dim[0]>0 || dim[1]<0 || dim[1]>0)
889 f << "center=" << MWAWVec2f(dim[1],dim[0]) << ",";
890 val=static_cast<int>(input->readLong(2)); // always 2
891 if (val != 1)
892 f << "f2=" << val << ",";
893 for (auto &d : dim) d=float(input->readLong(2))/8.f;
894 MWAWBox2f box(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2]));
895 f << "line=" << box << ",";
896 shape.m_type=MacDraftParserInternal::Shape::Basic;
897 shape.m_shape=MWAWGraphicShape::line(box[0], box[1]);
898 shape.m_isLine=true;
899 break;
900 }
901 default: {
902 int type=(flag>>26)&0x1f;
903 if (type==3) {
904 f << "textbox,";
905 shape.m_type=MacDraftParserInternal::Shape::Text;
906 shape.m_style.m_lineWidth=0;
907 for (int i=0; i<2; ++i) {
908 val=static_cast<int>(input->readLong(2));
909 if (val)
910 f << "f" << i << "=" << val << ",";
911 }
912 for (auto &d : dim) d=float(input->readLong(2))/8.f;
913 MWAWBox2f box(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2]));
914 shape.m_box=box;
915 f << "box=" << box << ",";
916 f << "unkn=[";
917 for (int i=0; i<2; ++i) {
918 val=static_cast<int>(input->readULong(2));
919 if (val)
920 f << std::hex << val << std::dec << ",";
921 else
922 f << "_,";
923 }
924 f << "],";
925 MWAWFont &font=shape.m_font;
926 val=static_cast<int>(input->readULong(1));
927 uint32_t flags=0;
928 if (val&0x1) flags |= MWAWFont::boldBit;
929 if (val&0x2) flags |= MWAWFont::italicBit;
930 if (val&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
931 if (val&0x8) flags |= MWAWFont::embossBit;
932 if (val&0x10) flags |= MWAWFont::shadowBit;
933 if (val>>5) f << "#flag[font]=" << (val>>5) << ",";
934 val=static_cast<int>(input->readULong(1));
935 if (val) f << "flag1[font]=" << val << ",";
936 font.setId(static_cast<int>(input->readULong(1)));
937 val=static_cast<int>(input->readULong(1));
938 if (val)
939 font.setSize(float(val));
940 font.setFlags(flags);
941 f << "font=[" << font.getDebugString(getParserState()->m_fontConverter) << "],";
942 val=static_cast<int>(input->readLong(1));
943 switch (val) {
944 case -1:
945 shape.m_paragraph.m_justify = MWAWParagraph::JustificationRight;
946 f << "right,";
947 break;
948 case 0:
949 break;
950 case 1:
951 shape.m_paragraph.m_justify = MWAWParagraph::JustificationCenter;
952 f << "center,";
953 break;
954 default:
955 MWAW_DEBUG_MSG(("MacDraftParser::readObject: find unknown align\n"));
956 f << "##align=" << val << ",";
957 break;
958 }
959 val=static_cast<int>(input->readLong(1)); // find 2
960 if (val) f << "f4=" << val << ",";
961 val=static_cast<int>(input->readLong(1));
962 switch (val) {
963 case 0:
964 break;
965 case 1:
966 shape.m_paragraph.setInterline(1.5, librevenge::RVNG_PERCENT);
967 f << "interline=150%,";
968 break;
969 case 2:
970 shape.m_paragraph.setInterline(2, librevenge::RVNG_PERCENT);
971 f << "interline=200%,";
972 break;
973 default:
974 MWAW_DEBUG_MSG(("MacDraftParser::readObject: find unknown interline\n"));
975 f << "##interline=" << val << ",";
976 break;
977 }
978 val=static_cast<int>(input->readLong(1));
979 switch (val) { // the string must already be in lowercase, ..., so
980 case 0:
981 break;
982 case 1:
983 f << "upper,";
984 break;
985 case 2:
986 f << "lower,";
987 break;
988 case 3:
989 f << "title,";
990 break;
991 default:
992 MWAW_DEBUG_MSG(("MacDraftParser::readObject: find unknown position\n"));
993 f << "##position=" << val << ",";
994 break;
995 }
996 val=static_cast<int>(input->readLong(2));
997 if (val!=1) // find 1 or 3
998 f << "f5=" << val << ",";
999 auto N=static_cast<int>(input->readULong(2));
1000 if (input->tell()+4+N>endPos) {
1001 MWAW_DEBUG_MSG(("MacDraftParser::readObject: can not read the number of char \n"));
1002 f << "##N=" << N << ",";
1003 break;
1004 }
1005 val=static_cast<int>(input->readLong(2));
1006 if (val!=1) // find always 1
1007 f << "f6=" << val << ",";
1008 std::string text("");
1009 shape.m_textEntry.setBegin(input->tell());
1010 shape.m_textEntry.setLength(N);
1011 for (int i=0; i<N; ++i) text+=char(input->readULong(1));
1012 f << text << ",";
1013 val=static_cast<int>(input->readULong(2));
1014 if (val)
1015 f << "fl3=" << std::hex << val << std::dec << ",";
1016 break;
1017 }
1018 val=static_cast<int>(input->readULong(4));
1019 if (val) f << "pat[id]=" << std::hex << val << ",";
1020 for (auto &d: dim) d=float(input->readLong(2))/8.f;
1021 auto N=static_cast<int>(input->readULong(2));
1022 if (N*4+46==fSz) {
1023 f << "poly, box=" << MWAWBox2f(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2])) << ",";
1024 shape.m_type=MacDraftParserInternal::Shape::Basic;
1025 shape.m_shape.m_type = MWAWGraphicShape::Polygon;
1026 std::vector<MWAWVec2f> &vertices=shape.m_shape.m_vertices;
1027 MWAWBox2f box;
1028 f << "pts=[";
1029 for (int i=0; i<N; ++i) {
1030 float coord[2];
1031 for (auto &c : coord) c=float(input->readLong(2))/8.f;
1032 MWAWVec2f point=MWAWVec2f(coord[1],coord[0]);
1033 vertices.push_back(point);
1034 if (i==0)
1035 box=MWAWBox2f(point, point);
1036 else
1037 box=box.getUnion(MWAWBox2f(point, point));
1038 f << point << ",";
1039 }
1040 shape.m_shape.m_bdBox=shape.m_shape.m_formBox=box;
1041 f << "],";
1042 break;
1043 }
1044 input->seek(-10, librevenge::RVNG_SEEK_CUR);
1045 // happens in a converted file: a quite horrible file
1046 MWAW_DEBUG_MSG(("MacDraftParser::readObject: find unknown data\n"));
1047 f << "UNKN###,";
1048 break;
1049 }
1050 }
1051 shape.transform(rotate, flipX, shape.getBdBox().center());
1052 shape.translate(origin);
1053 m_state->m_shapeList.push_back(shape);
1054 if (input->tell()!=endPos)
1055 ascii().addDelimiter(input->tell(),'|');
1056 ascii().addPos(pos);
1057 ascii().addNote(f.str().c_str());
1058 input->seek(endPos,librevenge::RVNG_SEEK_SET);
1059 return true;
1060 }
1061
1062 ////////////////////////////////////////////////////////////
1063 // read a bitmap
1064 ////////////////////////////////////////////////////////////
1065
1066 bool MacDraftParser::readBitmapData()
1067 {
1068 MWAWInputStreamPtr input = getInput();
1069 if (input->isEnd())
1070 return false;
1071 long pos=input->tell();
1072 auto fSz=long(input->readULong(2));
1073 long endPos=pos+2+fSz;
1074 if (fSz<28 || !input->checkPosition(endPos)) {
1075 input->seek(pos,librevenge::RVNG_SEEK_SET);
1076 return false;
1077 }
1078 MacDraftParserInternal::BitmapFileData data;
1079 libmwaw::DebugStream f;
1080 f << "Entries(BitmapData):";
1081 auto val=long(input->readULong(4));
1082 if (val)
1083 f << "id[next]=" << std::hex << val << std::dec << ",";
1084 val=long(input->readULong(2));
1085 if (val) f << "fl=" << std::hex << val << std::dec << ",";
1086 data.m_id=input->readULong(4);
1087 f << "id=" << std::hex << data.m_id << std::dec << ",";
1088 val=input->readLong(2); // 1 or 2
1089 if (val) f << "f0=" << val << ",";
1090 val=input->readLong(2); // 0
1091 if (val) f << "f1=" << val << ",";
1092 f << "id2=" << std::hex << input->readULong(4) << std::dec << ","; // 80000000+id
1093 data.m_rowSize=static_cast<int>(input->readULong(2)); // 2|4|72 : number of bytes by row
1094 if (data.m_rowSize) f << "row[size]=" << data.m_rowSize << ",";
1095 int dim[4];
1096 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
1097 data.m_dimension=MWAWBox2i(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2]));
1098 f << "dim=" << data.m_dimension << ",";
1099 data.m_entry.setBegin(input->tell());
1100 data.m_entry.setEnd(endPos);
1101 if (!data.ok()) {
1102 input->seek(pos,librevenge::RVNG_SEEK_SET);
1103 return false;
1104 }
1105 if (data.m_rowSize*data.m_dimension.size()[1]+28!=fSz) {
1106 MWAW_DEBUG_MSG(("MacDraftParser::readBitmapData: the bitmap size seems odd\n"));
1107 f << "###sz,";
1108 }
1109 if (m_state->m_idToBitmapMap.find(data.m_id)!=m_state->m_idToBitmapMap.end()) {
1110 MWAW_DEBUG_MSG(("MacDraftParser::readBitmapData: the id is already defined\n"));
1111 f << "###id,";
1112 }
1113 else
1114 m_state->m_idToBitmapMap[data.m_id]=data;
1115 if (data.m_entry.length())
1116 ascii().skipZone(data.m_entry.begin(), data.m_entry.end()-1);
1117 ascii().addPos(pos);
1118 ascii().addNote(f.str().c_str());
1119 input->seek(endPos,librevenge::RVNG_SEEK_SET);
1120 return true;
1121 }
1122
1123 bool MacDraftParser::readBitmapDefinition(MacDraftParserInternal::Shape &bitmap)
1124 {
1125 MWAWInputStreamPtr input = getInput();
1126 if (input->isEnd())
1127 return false;
1128 long pos=input->tell();
1129 auto fSz=long(input->readULong(2));
1130 long endPos=pos+2+fSz;
1131 if (fSz!=0x3e || !input->checkPosition(endPos)) {
1132 input->seek(pos,librevenge::RVNG_SEEK_SET);
1133 return false;
1134 }
1135 libmwaw::DebugStream f;
1136 f << "Entries(BitmapDef):";
1137 int val;
1138 for (int i=0; i<2; ++i) {
1139 val=static_cast<int>(input->readULong(4));
1140 if (val) f << "id" << i << "=" << std::hex << val << std::dec << ",";
1141 }
1142 unsigned long flag=input->readULong(4);
1143 if (flag>>16) f << "fl0[h]=" << std::hex << (flag>>16) << std::dec << ",";
1144 if (flag&0xFFFF) f << "fl0[l]=" << std::hex << (flag&0xFFFF) << std::dec << ",";
1145
1146 val=static_cast<int>(input->readULong(2));
1147 if (val&0x8000) f << "lock,";
1148 val &= 0x7fff;
1149 if (val) f << "fl1=" << std::hex << val << std::dec << ",";
1150
1151 val=static_cast<int>(input->readULong(2));
1152 if (val&0xfc00)
1153 f << "rot[h]=" << std::hex << (val>>10) << std::dec << ",";
1154 if (val&0x200) {
1155 f << "flipX,";
1156 }
1157 val &=0x1ff;
1158 if (val)
1159 f << "rot=" << float(val) << ",";
1160 float dim[4];
1161 for (auto &d : dim) d=float(input->readLong(2))/8.f;
1162 f << "dim=" << MWAWBox2f(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2])) << ",";
1163 float coord[2];
1164 for (auto &c : coord) c=float(input->readLong(2))/8.f;
1165 f << "orig=" << MWAWVec2f(coord[1],coord[0]) << ",";
1166
1167 val=static_cast<int>(input->readULong(4));
1168 if (val) f << "id?=" << std::hex << val << std::dec << ",";
1169 for (auto &d : dim) d=float(input->readLong(2))/8.f;
1170 f << "dim1=" << MWAWBox2f(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2])) << ",";
1171 unsigned long id=input->readULong(4);
1172 bitmap.m_bitmapIdList.push_back(id);
1173 f << "id[data]=" << std::hex << id << std::dec << ",";
1174 val=static_cast<int>(input->readLong(2)); // 0|1
1175 if (val) f << "g0=" << val << ",";
1176 int iDim[4];
1177 for (auto &d : iDim) d=int(input->readLong(2));
1178 MWAWBox2i box(MWAWVec2i(iDim[1],iDim[0]), MWAWVec2i(iDim[3],iDim[2]));
1179 bitmap.m_bitmapDimensionList.push_back(box);
1180 f << "dim2=" << box << ",";
1181 for (auto &d : dim) d=float(input->readLong(2));
1182 f << "dim3=" << MWAWBox2f(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2])) << ",";
1183
1184 if (input->tell()!=endPos)
1185 ascii().addDelimiter(input->tell(),'|');
1186 ascii().addPos(pos);
1187 ascii().addNote(f.str().c_str());
1188 input->seek(endPos,librevenge::RVNG_SEEK_SET);
1189 return true;
1190 }
1191
1192 ////////////////////////////////////////////////////////////
1193 // read a pattern
1194 ////////////////////////////////////////////////////////////
1195 bool MacDraftParser::readPattern()
1196 {
1197 MWAWInputStreamPtr input = getInput();
1198 if (input->isEnd())
1199 return false;
1200 long pos=input->tell();
1201 auto fSz=long(input->readULong(2));
1202 long endPos=pos+2+fSz;
1203 if (fSz!=0x1e || !input->checkPosition(endPos)) {
1204 input->seek(pos,librevenge::RVNG_SEEK_SET);
1205 return false;
1206 }
1207 libmwaw::DebugStream f;
1208 f << "Entries(Pattern):";
1209 auto val=long(input->readULong(4));
1210 if (val) f << "id=" << std::hex << val << std::dec << ",";
1211 auto id=static_cast<int>(input->readULong(2));
1212 if (id==0xf0)
1213 f << "default,";
1214 else if (id<64)
1215 f << "id=" << id << ",";
1216 else {
1217 MWAW_DEBUG_MSG(("MacDraftParser::readPattern: find odd id\n"));
1218 f << "##id=" << id << ",";
1219 }
1220 val=long(input->readULong(4));
1221 if (val) f << "id[prev?]=" << std::hex << val << std::dec << ",";
1222 for (int i=0; i<2; ++i) { //3,3 | 4,4 | 5,5
1223 val=input->readLong(2);
1224 if (val) f << "f" << i << "=" << val << ",";
1225 }
1226 MWAWGraphicStyle::Pattern pat;
1227 pat.m_dim=MWAWVec2i(8,8);
1228 pat.m_data.resize(8);
1229 pat.m_colors[0]=MWAWColor::white();
1230 pat.m_colors[1]=MWAWColor::black();
1231
1232 for (auto &data : pat.m_data) data=uint8_t(input->readULong(1));
1233 f << "orig=" << pat << ",";
1234 for (auto &data : pat.m_data) data=uint8_t(input->readULong(1));
1235 f << "final=" << pat << ",";
1236 if (id>=0 && id<64) {
1237 m_state->initPatterns();
1238 if (id<static_cast<int>(m_state->m_patternList.size()))
1239 m_state->m_patternList[size_t(id)]=pat;
1240 }
1241 ascii().addPos(pos);
1242 ascii().addNote(f.str().c_str());
1243 input->seek(endPos,librevenge::RVNG_SEEK_SET);
1244 return true;
1245 }
1246 ////////////////////////////////////////////////////////////
1247 // read an label
1248 ////////////////////////////////////////////////////////////
1249 bool MacDraftParser::readLabel()
1250 {
1251 MWAWInputStreamPtr input = getInput();
1252 if (input->isEnd())
1253 return false;
1254 long pos=input->tell();
1255 auto fSz=long(input->readULong(2));
1256 long endPos=pos+2+fSz;
1257 if (fSz!=0x1a || !input->checkPosition(endPos)) {
1258 input->seek(pos,librevenge::RVNG_SEEK_SET);
1259 return false;
1260 }
1261 size_t shapeId= m_state->m_shapeList.size();
1262 MacDraftParserInternal::Shape shape;
1263 shape.m_id=static_cast<int>(shapeId);
1264 shape.m_nextId=shape.m_id+1;
1265 shape.m_type=MacDraftParserInternal::Shape::Label;
1266 shape.m_style.m_lineWidth=0;
1267 shape.m_style.setSurfaceColor(MWAWColor::white());
1268 libmwaw::DebugStream f;
1269 f << "Entries(Label):";
1270 int val;
1271 for (int i=0; i<2; ++i) {
1272 val=static_cast<int>(input->readULong(4));
1273 if (val) f << "id" << i << "=" << std::hex << val << std::dec << ",";
1274 }
1275 unsigned long flag=input->readULong(4);
1276 MWAWFont &font=shape.m_font;
1277 if ((flag & 0x7F))
1278 font.setSize(float(flag & 0x7F));
1279 font.setId((flag>>8) & 0xFF);
1280 uint32_t flags=0;
1281 if (flag&0x1000000) flags |= MWAWFont::boldBit;
1282 if (flag&0x2000000) flags |= MWAWFont::italicBit;
1283 if (flag&0x4000000) font.setUnderlineStyle(MWAWFont::Line::Simple);
1284 if (flag&0x8000000) flags |= MWAWFont::embossBit;
1285 if (flag&0x10000000) flags |= MWAWFont::shadowBit;
1286 if (flag&static_cast<unsigned long>(0x80000000)) f << "select,";
1287 flag &= static_cast<unsigned long>(0x60FF0080);
1288 if (flag>>16) f << "fl0[h]=" << std::hex << (flag>>16) << std::dec << ",";
1289 if (flag&0xFFFF) f << "fl0[l]=" << std::hex << (flag&0xFFFF) << std::dec << ",";
1290
1291 flag=input->readULong(4);
1292 switch (flag&3) {
1293 case 1:
1294 flags |= MWAWFont::uppercaseBit;
1295 break;
1296 case 2:
1297 flags |= MWAWFont::lowercaseBit;
1298 break;
1299 case 3:
1300 f << "title,";
1301 flags |= MWAWFont::smallCapsBit;
1302 break;
1303 case 0:
1304 default:
1305 break;
1306 }
1307 font.setFlags(flags);
1308 f << "font=[" << font.getDebugString(getParserState()->m_fontConverter) << "],";
1309 switch ((flag>>8)&3) {
1310 case 0:
1311 break;
1312 case 1:
1313 shape.m_paragraph.setInterline(1.5, librevenge::RVNG_PERCENT);
1314 f << "interline=150%,";
1315 break;
1316 case 2:
1317 shape.m_paragraph.setInterline(200, librevenge::RVNG_PERCENT);
1318 f << "interline=200%,";
1319 break;
1320 default:
1321 MWAW_DEBUG_MSG(("MacDraftParser::readLabel: find unknown interline\n"));
1322 f << "##interline=3,";
1323 break;
1324 }
1325 switch ((flag>>24)) {
1326 case 0:
1327 break;
1328 case 0xff:
1329 shape.m_paragraph.m_justify = MWAWParagraph::JustificationRight;
1330 f << "right,";
1331 break;
1332 case 1:
1333 shape.m_paragraph.m_justify = MWAWParagraph::JustificationCenter;
1334 f << "center,";
1335 break;
1336 default:
1337 MWAW_DEBUG_MSG(("MacDraftParser::readLabel: find unknown align\n"));
1338 f << "##align=" << (flag>>24) << ",";
1339 break;
1340 }
1341
1342 flag &= 0xFFFCFC;
1343 if (flag>>16) f << "fl1[h]=" << std::hex << (flag>>16) << std::dec << ",";
1344 if (flag&0xFFFF) f << "fl1[l]=" << std::hex << (flag&0xFFFF) << std::dec << ",";
1345
1346 float dim[4];
1347 for (auto &d : dim) d=float(input->readLong(2))/8.f;
1348 shape.m_box=MWAWBox2f(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2]));
1349 f << shape.m_box << ",";
1350 val=static_cast<int>(input->readULong(2));
1351 if (val!=0x204)
1352 f << "fl2=" << std::hex << val << std::dec << ",";
1353 m_state->m_shapeList.push_back(shape);
1354 ascii().addPos(pos);
1355 ascii().addNote(f.str().c_str());
1356 input->seek(endPos,librevenge::RVNG_SEEK_SET);
1357 return true;
1358 }
1359
1360 ////////////////////////////////////////////////////////////
1361 // read the header
1362 ////////////////////////////////////////////////////////////
1363 bool MacDraftParser::checkHeader(MWAWHeader *header, bool strict)
1364 {
1365 *m_state = MacDraftParserInternal::State();
1366 MWAWInputStreamPtr input = getInput();
1367 if (!input || !input->hasDataFork() || !input->checkPosition(0x270))
1368 return false;
1369
1370 libmwaw::DebugStream f;
1371 f << "FileHeader:";
1372 input->seek(0, librevenge::RVNG_SEEK_SET);
1373 if (input->readULong(2)!=2 || input->readULong(2)!=0 || input->readULong(2)!=2 ||
1374 input->readULong(2)!=0x262 || input->readULong(2)!=0x262)
1375 return false;
1376 int const vers=1;
1377 auto val=static_cast<int>(input->readULong(2));
1378 if (val)
1379 f << "f0=" << val << ",";
1380 f << "id=" << std::hex << input->readULong(4) << std::dec << ",";
1381 for (int i=0; i<2; ++i) {// f0=fc[4c]0, f1=[09]200
1382 val=static_cast<int>(input->readULong(2));
1383 if (val) f << "fl" << i << "=" << std::hex << val << std::dec << ",";
1384 }
1385 f << "id1=" << std::hex << input->readULong(4) << std::dec << ",";
1386 val=static_cast<int>(input->readLong(2)); // always 0
1387 if (val)
1388 f << "f1=" << val << ",";
1389 f << "ids=[";
1390 for (int i=0; i<5; ++i) { // maybe junk
1391 val=static_cast<int>(input->readULong(4));
1392 if (val)
1393 f << std::hex << val << std::dec << ",";
1394 else
1395 f << "_,";
1396 }
1397 f << "],";
1398 val=static_cast<int>(input->readLong(2)); // 0|1
1399 if (val)
1400 f << "f2=" << val << ",";
1401 auto sSz=static_cast<int>(input->readULong(1));
1402 if (sSz>31) {
1403 if (strict)
1404 return false;
1405 MWAW_DEBUG_MSG(("MacDraftParser::checkHeader: string size seems bad\n"));
1406 f << "##sSz=" << sSz << ",";
1407 }
1408 else {
1409 std::string name("");
1410 for (int i=0; i<sSz; ++i) name+=char(input->readULong(1));
1411 f << name << ",";
1412 }
1413 input->seek(80, librevenge::RVNG_SEEK_SET);
1414 val=static_cast<int>(input->readULong(4));
1415 if (val)
1416 f << "id2=" << std::hex << val << std::dec << ",";
1417
1418 ascii().addPos(0);
1419 ascii().addNote(f.str().c_str());
1420 if (strict) {
1421 input->seek(0x270,librevenge::RVNG_SEEK_SET);
1422 for (int i=0; i<10; ++i) {
1423 if (input->isEnd())
1424 break;
1425 long pos=input->tell();
1426 long fSz=static_cast<int>(input->readULong(2));
1427 if (!input->checkPosition(pos+2+fSz))
1428 return false;
1429 input->seek(pos+2+fSz, librevenge::RVNG_SEEK_SET);
1430 }
1431 input->seek(84, librevenge::RVNG_SEEK_SET);
1432 }
1433 setVersion(vers);
1434 m_state->m_version=vers;
1435 if (header)
1436 header->reset(MWAWDocument::MWAW_T_MACDRAFT, vers, MWAWDocument::MWAW_K_DRAW);
1437
1438 return true;
1439 }
1440
1441 ////////////////////////////////////////////////////////////
1442 // read the document header
1443 ////////////////////////////////////////////////////////////
1444 bool MacDraftParser::readDocHeader()
1445 {
1446 MWAWInputStreamPtr input = getInput();
1447 if (!input->checkPosition(0x262)) {
1448 MWAW_DEBUG_MSG(("MacDraftParser::readDocHeader: the file seems too short\n"));
1449 return false;
1450 }
1451 input->seek(84,librevenge::RVNG_SEEK_SET);
1452 libmwaw::DebugStream f;
1453 f << "Entries(DocHeader):";
1454 int val;
1455 for (int i=0; i<16; ++i) {
1456 val=static_cast<int>(input->readULong(2));
1457 static int const expected[]= {0,1,0,0,0xdfff,0xffff,0,0, 0xeed7, 0xffff, 0, 0,
1458 0x9dd5, 0xbfff,0x4088,0x8000
1459 };
1460 if (val!=expected[i])
1461 f << "f" << i << "=" << std::hex << val << std::dec << ",";
1462 }
1463 for (int i=0; i<7; ++i) {
1464 f << "unkn" << i << "=[";
1465 for (int j=0; j<4; ++j) {
1466 val=static_cast<int>(input->readULong(2));
1467 if (val)
1468 f << std::hex << val << std::dec << ",";
1469 else
1470 f << "_,";
1471 }
1472 f << "],";
1473 }
1474 for (int i=0; i<14; ++i) { // always 0
1475 val=static_cast<int>(input->readLong(2));
1476 if (val)
1477 f << "g" << i << "=" << val << ",";
1478 }
1479 ascii().addPos(84);
1480 ascii().addNote(f.str().c_str());
1481
1482 long pos=input->tell();
1483 f.str("");
1484 f << "DocHeader-1:";
1485 auto N=static_cast<int>(input->readULong(2));
1486 if (N>=20) {
1487 MWAW_DEBUG_MSG(("MacDraftParser::readDocHeader: N seems bad\n"));
1488 f << "###";
1489 N=0;
1490 }
1491 // FIXME: find a better way to determine v1.0 and/or v1.2 file
1492 if (N==4)
1493 m_state->m_version=1;
1494 else
1495 m_state->m_version=2;
1496 f << "N=" << N << ",unk=[";
1497 for (int i=0; i<=N; ++i) {
1498 val=static_cast<int>(input->readULong(2));
1499 f << input->readULong(2) << ":" << std::hex << val << std::dec << ",";
1500 }
1501 f << "],";
1502 input->seek(pos+42, librevenge::RVNG_SEEK_SET);
1503 for (int i=0; i<6; ++i) {
1504 val=static_cast<int>(input->readULong(2));
1505 static int const expected[]= {0x144, 0xa, 0x152, 0xc, 0, 0 };
1506 if (val!=expected[i])
1507 f << "f" << i << "=" << std::hex << val << std::dec << ",";
1508 }
1509 f << "unkn1=[";
1510 for (int i=0; i<2; ++i) { // 0,0 or 99cc6633,99cc6633
1511 auto lVal=long(input->readULong(4));
1512 if (lVal)
1513 f << std::hex << lVal << std::dec << ",";
1514 else
1515 f << "_,";
1516 }
1517 f << "],";
1518 ascii().addPos(pos);
1519 ascii().addNote(f.str().c_str());
1520
1521 pos=input->tell();
1522 f.str("");
1523 f << "DocHeader-2:";
1524 input->seek(pos+132,librevenge::RVNG_SEEK_SET);
1525 ascii().addPos(pos);
1526 ascii().addNote(f.str().c_str());
1527
1528 pos=input->tell();
1529 f.str("");
1530 f << "DocHeader-3:";
1531 input->seek(pos+66,librevenge::RVNG_SEEK_SET);
1532 ascii().addPos(pos);
1533 ascii().addNote(f.str().c_str());
1534
1535 for (int st=0; st<2; ++st) {
1536 pos=input->tell();
1537 f.str("");
1538 f << "DocHeader-4" << (st==0 ? "a" : "b") << ":";
1539 input->seek(pos+40,librevenge::RVNG_SEEK_SET);
1540 ascii().addPos(pos);
1541 ascii().addNote(f.str().c_str());
1542 }
1543
1544 pos=input->tell();
1545 f.str("");
1546 f << "DocHeader-5:";
1547 for (int i=0; i<2; ++i) { // fl0=0|1, fl1=1
1548 val=static_cast<int>(input->readLong(1));
1549 if (val==1)
1550 f << "fl" << i << ",";
1551 else if (val)
1552 f << "fl" << i << "=" << val << ",";
1553 }
1554 for (int i=0; i<2; ++i) { // f0=3, f1=32|100
1555 val=static_cast<int>(input->readLong(2));
1556 if (val)
1557 f << "f" << i << "=" << val << ",";
1558 }
1559 for (int i=0; i<4; ++i) { // fl2=1, fl4=0|1
1560 val=static_cast<int>(input->readLong(1));
1561 if (val==1)
1562 f << "fl" << i+2 << ",";
1563 else if (val)
1564 f << "fl" << i+2 << "=" << val << ",";
1565 }
1566 f << "unkn=[";
1567 for (int i=0; i<6; ++i) { // small number
1568 val=static_cast<int>(input->readLong(2));
1569 if (val)
1570 f << val << ",";
1571 else
1572 f << "_,";
1573 }
1574 f << "],";
1575 float coord[2];
1576 for (auto &c : coord) c=float(input->readLong(2))/8.f;
1577 f << "dim?=" << MWAWVec2f(coord[1],coord[0]) << ",";
1578 for (int i=0; i<7; ++i) { // f2=0|256, f5=256
1579 val=static_cast<int>(input->readLong(2));
1580 if (val)
1581 f << "f" << i+2 << "=" << val << ",";
1582 }
1583 int numPages[2];
1584 for (auto &p : numPages) p=static_cast<int>(input->readLong(2));
1585 if (numPages[0]!=1 || numPages[1]!=1)
1586 f << "numPages=" << MWAWVec2i(numPages[0], numPages[1]) << ",";
1587 if (numPages[0]<=0 || numPages[0]>50) {
1588 MWAW_DEBUG_MSG(("MacDraftParser::readDocHeader: num horizontal pages seems bad, reset it to 1\n"));
1589 numPages[0]=1;
1590 f << "###hori,";
1591 }
1592 if (numPages[1]<=0 || numPages[1]>100) {
1593 MWAW_DEBUG_MSG(("MacDraftParser::readDocHeader: num vertical pages seems bad, reset it to 1\n"));
1594 numPages[1]=1;
1595 f << "###verti,";
1596 }
1597 int pageDim[2];
1598 for (auto &d : pageDim) d=static_cast<int>(input->readLong(2));
1599 f << "page[dim]=" << MWAWVec2i(pageDim[1],pageDim[0]) << ",";
1600 if (pageDim[0]>=50 && pageDim[0]<=2000)
1601 getPageSpan().setFormLength(double(pageDim[0]*numPages[1])/72.);
1602 else {
1603 MWAW_DEBUG_MSG(("MacDraftParser::readDocHeader: page height seems bad, use default\n"));
1604 }
1605 if (pageDim[1]>=50 && pageDim[1]<=2000)
1606 getPageSpan().setFormWidth(double(pageDim[1]*numPages[0])/72.);
1607 else {
1608 MWAW_DEBUG_MSG(("MacDraftParser::readDocHeader: page width seems bad, use default\n"));
1609 }
1610
1611 ascii().addDelimiter(input->tell(),'|');
1612 ascii().addPos(pos);
1613 ascii().addNote(f.str().c_str());
1614
1615 input->seek(0x270,librevenge::RVNG_SEEK_SET);
1616 return true;
1617 }
1618
1619 ////////////////////////////////////////////////////////////
1620 // try to read the print info zone
1621 ////////////////////////////////////////////////////////////
1622 bool MacDraftParser::readPrintInfo()
1623 {
1624 MWAWInputStreamPtr input = getInput();
1625 long pos=input->tell();
1626 auto fSz=long(input->readULong(2));
1627 long endPos=pos+2+120;
1628 if (fSz!=120 || !input->checkPosition(endPos)) {
1629 MWAW_DEBUG_MSG(("MacDraftParser::readPrintInfo: file seems too short\n"));
1630 return false;
1631 }
1632 libmwaw::DebugStream f;
1633 f << "Entries(PrintInfo):";
1634 libmwaw::PrinterInfo info;
1635 if (!info.read(input)) {
1636 MWAW_DEBUG_MSG(("MacDraftParser::readPrintInfo: can not read print info\n"));
1637 return false;
1638 }
1639 f << info;
1640 MWAWVec2i paperSize = info.paper().size();
1641 MWAWVec2i pageSize = info.page().size();
1642 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
1643 paperSize.x() <= 0 || paperSize.y() <= 0) {
1644 ascii().addPos(pos);
1645 ascii().addNote(f.str().c_str());
1646 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1647 return true;
1648 }
1649
1650 // define margin from print info
1651 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
1652 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
1653
1654 // move margin left | top
1655 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
1656 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
1657 lTopMargin -= MWAWVec2i(decalX, decalY);
1658 rBotMargin += MWAWVec2i(decalX, decalY);
1659
1660 // decrease right | bottom
1661 int rightMarg = rBotMargin.x() -50;
1662 if (rightMarg < 0) rightMarg=0;
1663 int botMarg = rBotMargin.y() -50;
1664 if (botMarg < 0) botMarg=0;
1665
1666 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
1667 getPageSpan().setMarginBottom(botMarg/72.0);
1668 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
1669 getPageSpan().setMarginRight(rightMarg/72.0);
1670 // do not set page height/width as this is done by readDocHeader
1671
1672 ascii().addPos(pos);
1673 ascii().addNote(f.str().c_str());
1674 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1675 return true;
1676 }
1677
1678 ////////////////////////////////////////////////////////////
1679 //
1680 // send data
1681 //
1682 ////////////////////////////////////////////////////////////
1683
1684 bool MacDraftParser::send(MacDraftParserInternal::Shape const &shape)
1685 {
1686 MWAWGraphicListenerPtr listener=getGraphicListener();
1687 if (!listener) {
1688 MWAW_DEBUG_MSG(("MacDraftParser::send: can not find the listener\n"));
1689 return false;
1690 }
1691 shape.m_isSent=true;
1692 MWAWBox2f box=shape.getBdBox();
1693 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
1694 pos.m_anchorTo = MWAWPosition::Page;
1695
1696 MWAWGraphicStyle style=shape.m_style;
1697 if (shape.m_patternId>0) {
1698 MWAWGraphicStyle::Pattern pat;
1699 if (m_state->getPattern(shape.m_patternId, pat)) {
1700 MWAWColor col;
1701 if (pat.getUniqueColor(col))
1702 style.setSurfaceColor(col);
1703 else
1704 style.setPattern(pat);
1705 }
1706 else {
1707 MWAW_DEBUG_MSG(("MacDraftParser::send: can not find pattern %d\n", shape.m_patternId));
1708 }
1709 }
1710 switch (shape.m_type) {
1711 case MacDraftParserInternal::Shape::Basic:
1712 listener->insertShape(pos, shape.m_shape, style);
1713 break;
1714 case MacDraftParserInternal::Shape::Group: {
1715 size_t numShapes=m_state->m_shapeList.size();
1716 if (!numShapes) break;
1717 listener->openGroup(pos);
1718 for (auto id : shape.m_childList) {
1719 if (id>=numShapes) {
1720 MWAW_DEBUG_MSG(("MacDraftParser::send: can not find a child\n"));
1721 continue;
1722 }
1723 MacDraftParserInternal::Shape const &child=m_state->m_shapeList[id];
1724 if (child.m_isSent) {
1725 MWAW_DEBUG_MSG(("MacDraftParser::send: the child is already sent\n"));
1726 continue;
1727 }
1728 send(child);
1729 }
1730 listener->closeGroup();
1731 break;
1732 }
1733 case MacDraftParserInternal::Shape::Label: {
1734 /* either a 1d label or a 2d label, so check if we can print it,
1735 ie. if the previous object is a line
1736 */
1737 if (shape.m_id<=0 || shape.m_id>=static_cast<int>(m_state->m_shapeList.size())) {
1738 MWAW_DEBUG_MSG(("MacDraftParser::send: problem finding the shape\n"));
1739 break;
1740 }
1741 auto const &obj=m_state->m_shapeList[size_t(shape.m_id-1)];
1742 if (obj.m_type!=MacDraftParserInternal::Shape::Basic || !obj.m_isLine) {
1743 MWAW_DEBUG_MSG(("MacDraftParser::send: can only retrieve line label\n"));
1744 break;
1745 }
1746 // update the label width and recenter the label box
1747 MWAWVec2f lineSz=obj.m_shape.getBdBox().size();
1748 auto &label=m_state->m_shapeList[size_t(shape.m_id)];
1749 label.m_labelWidth=std::sqrt(lineSz[0]*lineSz[0]+lineSz[1]*lineSz[1]);
1750 MWAWVec2f decal=obj.m_shape.getBdBox().center()-box.center();
1751 pos.setOrigin(box[0]+decal);
1752 std::shared_ptr<MWAWSubDocument> doc(new MacDraftParserInternal::SubDocument(*this, getInput(), shape.m_id));
1753 listener->insertTextBox(pos, doc, style);
1754 return true;
1755 }
1756 case MacDraftParserInternal::Shape::Text: {
1757 std::shared_ptr<MWAWSubDocument> doc(new MacDraftParserInternal::SubDocument(*this, getInput(), shape.m_id));
1758 listener->insertTextBox(pos, doc, style);
1759 return true;
1760 }
1761 case MacDraftParserInternal::Shape::Bitmap:
1762 sendBitmap(shape, pos);
1763 break;
1764 case MacDraftParserInternal::Shape::Unknown:
1765 #if !defined(__clang__)
1766 default:
1767 #endif
1768 return false;
1769 }
1770 return true;
1771 }
1772
1773 bool MacDraftParser::sendBitmap(MacDraftParserInternal::Shape const &bitmap, MWAWPosition const &position)
1774 {
1775 MWAWGraphicListenerPtr listener=getGraphicListener();
1776 if (!listener) {
1777 MWAW_DEBUG_MSG(("MacDraftParser::sendBitmap: can not find the listener\n"));
1778 return false;
1779 }
1780 bitmap.m_isSent=true;
1781
1782 if (bitmap.m_bitmapIdList.empty() || bitmap.m_bitmapDimensionList.size()!=bitmap.m_bitmapIdList.size()) {
1783 MWAW_DEBUG_MSG(("MacDraftParser::sendBitmap: the bitmap seems bad\n"));
1784 return false;
1785 }
1786 MWAWBox2i bitmapBox=bitmap.m_bitmapDimensionList[0];
1787 for (size_t i=1; i<bitmap.m_bitmapDimensionList.size(); ++i)
1788 bitmapBox=bitmapBox.getUnion(bitmap.m_bitmapDimensionList[i]);
1789 MWAWVec2i bitmapDim(bitmapBox.size());
1790 if (bitmapDim[0]<=0||bitmapDim[1]<=0||bitmapDim[0]*bitmapDim[1]>10000000) {
1791 MWAW_DEBUG_MSG(("MacDraftParser::sendBitmap: can not compute the bitmap bdbox\n"));
1792 return false;
1793 }
1794
1795 MWAWInputStreamPtr input = getInput();
1796 MWAWPictBitmapColor pict(bitmapDim, true);
1797 MWAWColor transparent(255,255,255,0);
1798 MWAWColor black(MWAWColor::black());
1799 std::vector<MWAWColor> colors;
1800 colors.resize(size_t(bitmapDim[0]), transparent);
1801 for (int r=0; r<bitmapDim[1]; ++r) pict.setRow(r, &colors[0]);
1802
1803 for (size_t i=0; i<bitmap.m_bitmapIdList.size(); ++i) {
1804 auto id=bitmap.m_bitmapIdList[i];
1805 if (m_state->m_idToBitmapMap.find(id)==m_state->m_idToBitmapMap.end()) {
1806 MWAW_DEBUG_MSG(("MacDraftParser::sendBitmap: can not find bitmap data %lx\n", id));
1807 continue;
1808 }
1809 MWAWBox2i dataBox(bitmap.m_bitmapDimensionList[i]);
1810 MacDraftParserInternal::BitmapFileData const &data=m_state->m_idToBitmapMap.find(id)->second;
1811 if (!data.ok() || data.m_dimension[0]!=dataBox[0] || data.m_dimension.size()[0]<dataBox.size()[0] ||
1812 data.m_dimension.size()[1]<dataBox.size()[1]) {
1813 MWAW_DEBUG_MSG(("MacDraftParser::sendBitmap: bitmap data %lx seems bad\n", id));
1814 continue;
1815 }
1816
1817 long pos=input->tell();
1818 input->seek(data.m_entry.begin(), librevenge::RVNG_SEEK_SET);
1819 for (int r=dataBox[0][1]; r<dataBox[1][1]; ++r) {
1820 long debRow=input->tell();
1821 if (r>=bitmapBox[1][1])
1822 break;
1823 int row=r-bitmapBox[0][1];
1824 int wPos=dataBox[0][0]-bitmapBox[0][0];
1825 for (int col=dataBox[0][0]; col<dataBox[1][0];) {
1826 auto c=static_cast<unsigned char>(input->readULong(1));
1827 for (int j=0, bit=0x80; j<8 ; ++j, bit>>=1) {
1828 if (wPos>=bitmapDim[0] || ++col>dataBox[1][0]) break;
1829 pict.set(wPos++, row, (c&bit) ? black : transparent);
1830 }
1831 }
1832 input->seek(debRow+data.m_rowSize, librevenge::RVNG_SEEK_SET);
1833 }
1834 input->seek(pos, librevenge::RVNG_SEEK_SET);
1835 }
1836
1837 MWAWEmbeddedObject picture;
1838 if (!pict.getBinary(picture)) return false;
1839 #ifdef DEBUG_WITH_FILES
1840 if (!picture.m_dataList.empty()) {
1841 static int volatile pictName = 0;
1842 libmwaw::DebugStream f;
1843 f << "PICT-" << ++pictName << ".bmp";
1844 libmwaw::Debug::dumpFile(picture.m_dataList[0], f.str().c_str());
1845 }
1846 #endif
1847
1848 listener->insertPicture(position, picture);
1849
1850 return true;
1851 }
1852
1853 bool MacDraftParser::sendText(int zId)
1854 {
1855 MWAWGraphicListenerPtr listener=getGraphicListener();
1856 if (!listener) {
1857 MWAW_DEBUG_MSG(("MacDraftParser::sendText: can not find the listener\n"));
1858 return false;
1859 }
1860 if (zId<0||zId>=static_cast<int>(m_state->m_shapeList.size())) {
1861 MWAW_DEBUG_MSG(("MacDraftParser::sendText: can not find the text shape\n"));
1862 return false;
1863 }
1864 auto const &shape=m_state->m_shapeList[size_t(zId)];
1865 shape.m_isSent = true;
1866
1867 listener->setParagraph(shape.m_paragraph);
1868 listener->setFont(shape.m_font);
1869 if (shape.m_type == MacDraftParserInternal::Shape::Label) {
1870 std::stringstream s;
1871 s << std::setprecision(0) << std::fixed << shape.m_labelWidth << " pt";
1872 listener->insertUnicodeString(librevenge::RVNGString(s.str().c_str()));
1873 return true;
1874 }
1875 if (shape.m_type != MacDraftParserInternal::Shape::Text) {
1876 MWAW_DEBUG_MSG(("MacDraftParser::sendText: can not find the text shape\n"));
1877 return false;
1878 }
1879 if (!shape.m_textEntry.valid())
1880 return true;
1881 MWAWInputStreamPtr input=getInput();
1882 input->seek(shape.m_textEntry.begin(), librevenge::RVNG_SEEK_SET);
1883 libmwaw::DebugStream f;
1884 f << "Object[text]:";
1885 long endPos=shape.m_textEntry.end();
1886 while (!input->isEnd()) {
1887 if (input->tell()>=shape.m_textEntry.end())
1888 break;
1889 auto c = char(input->readULong(1));
1890 if (c==0) {
1891 MWAW_DEBUG_MSG(("MacDraftParser::sendText: find char 0\n"));
1892 f << "#[0]";
1893 continue;
1894 }
1895 f << c;
1896 switch (c) {
1897 case 9:
1898 listener->insertTab();
1899 break;
1900 case 0xd:
1901 listener->insertEOL();
1902 break;
1903 default:
1904 listener->insertCharacter(static_cast<unsigned char>(c), input, endPos);
1905 break;
1906 }
1907 }
1908 ascii().addPos(shape.m_textEntry.begin());
1909 ascii().addNote(f.str().c_str());
1910 return true;
1911 }
1912
1913 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: