Debian Codegraph @

src/lib/Canvas5Image.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 <array>
35 #include <cmath>
36 #include <iomanip>
37 #include <iostream>
38 #include <limits>
39 #include <map>
40 #include <set>
41 #include <sstream>
42 #include <stack>
43
44 #include <librevenge/librevenge.h>
45
46 #ifdef DEBUG_WITH_FILES
47 # define DEBUG_CANVAS_VKFL
48 #endif
49
50 #ifdef DEBUG_CANVAS_VKFL
51 # include "MWAWGraphicEncoder.hxx"
52 #endif
53 #include "MWAWGraphicListener.hxx"
54 #include "MWAWGraphicShape.hxx"
55 #include "MWAWGraphicStyle.hxx"
56 #include "MWAWListener.hxx"
57 #include "MWAWParser.hxx"
58
59 #include "Canvas5Parser.hxx"
60
61 #include "Canvas5Graph.hxx"
62 #include "Canvas5Image.hxx"
63 #include "Canvas5Structure.hxx"
64 #include "Canvas5StyleManager.hxx"
65
66 #include "libmwaw_internal.hxx"
67
68 /** Internal: the structures of a Canvas5Image */
69 namespace Canvas5ImageInternal
70 {
71 //! Internal: the stroke style of a Canvas5Image
72 struct Stroke {
73 //! constructor
74 Stroke()
75 : m_type(1)
76 , m_penPos(-1)
77 , m_dashPos(-1)
78 {
79 for (auto &id : m_arrowPos) id=-1;
80 }
81 //! the type
82 unsigned m_type;
83 //! the pen id
84 long m_penPos;
85 //! the dash id
86 long m_dashPos;
87 //! the arrow id (beg/end)
88 long m_arrowPos[2];
89 };
90
91 ////////////////////////////////////////
92 //! Internal: the internal shape of a Canvas5Image
93 struct VKFLShape {
94 //! constructor
95 VKFLShape()
96 : m_type(-1)
97 , m_subType(0)
98 , m_box()
99 , m_idToDataPos()
100 , m_style()
101
102 , m_vertices()
103 , m_bitmap()
104 , m_bitmapColor(MWAWColor::white())
105
106 , m_special()
107 , m_macoImage()
108 {
109 for (auto &v : m_locals) v=0;
110 }
111 static std::string getTypeName(int type)
112 {
113 switch (type) {
114 case -1:
115 return "";
116 case 1:
117 return "poly";
118 case 2:
119 return "spline";
120 case 6:
121 return "rect";
122 case 7:
123 return "circle";
124 case 8:
125 return "rectOval";
126 case 9:
127 return "line";
128 case 10:
129 return "arc";
130 case 11:
131 return "group";
132 case 12: // a group for ???
133 return "group1";
134 case 14:
135 return "special";
136 default:
137 return Canvas5Structure::getString(unsigned(type));
138 }
139 }
140 //! the type
141 int m_type;
142 //! the sub type
143 unsigned m_subType;
144 //! the dimension
145 MWAWBox2f m_box;
146 //! the map id(type) to data pos in the main zone
147 std::map<int, long> m_idToDataPos;
148 //! the graphic style
149 MWAWGraphicStyle m_style;
150
151 //! the vertices: spline, poly, ...
152 std::vector<MWAWVec2f> m_vertices;
153 //! the local values : arc=>angles, rectOval=>oval size
154 float m_locals[2];
155 //! the bitmap
156 MWAWEmbeddedObject m_bitmap;
157 //! the bitmap color
158 MWAWColor m_bitmapColor;
159 //! a graph pseudo box: special
160 std::shared_ptr<Canvas5GraphInternal::PseudoShape> m_special;
161 //! a macro image : special
162 std::shared_ptr<VKFLImage> m_macoImage;
163 };
164
165 //! Internal: the internal image of a Canvas5Image
166 struct VKFLImage {
167 //! constructor
168 VKFLImage()
169 : m_data()
170 , m_shapes()
171
172 , m_posToTypesMap()
173
174 , m_posToArrowMap()
175 , m_posToColorMap()
176 , m_posToDashMap()
177 , m_posToMatrixMap()
178 , m_posToPenMap()
179 , m_posToStrokeMap()
180 {
181 for (auto &box : m_boxes) box=MWAWBox2f();
182 for (auto &mat : m_matrices) mat=std::array<double,9>();
183 }
184 //! the data entry
185 MWAWEntry m_data[2];
186 //! the list of shape
187 std::vector<VKFLShape> m_shapes;
188 //! the dimensions
189 MWAWBox2f m_boxes[2];
190 //! the transformations
191 std::array<double,9> m_matrices[2];
192
193 //! the map pos to type and sub type
194 std::map<long, std::pair<unsigned, unsigned> > m_posToTypesMap;
195
196 //! the position to arrow map
197 std::map<long, MWAWGraphicStyle::Arrow> m_posToArrowMap;
198 //! the position to color map
199 std::map<long, std::shared_ptr<Canvas5StyleManagerInternal::ColorStyle> > m_posToColorMap;
200 //! the position to dash map
201 std::map<long, std::vector<float> > m_posToDashMap;
202 //! the position to matrix map
203 std::map<long, std::array<double,9> > m_posToMatrixMap;
204 //! the position to pen map
205 std::map<long, std::shared_ptr<Canvas5StyleManagerInternal::PenStyle> > m_posToPenMap;
206 //! the position to stroke map
207 std::map<long, Stroke> m_posToStrokeMap;
208 };
209
210 ////////////////////////////////////////
211 //! Internal: the state of a Canvas5Image
212 struct State {
213 //! constructor
214 State()
215 : m_idToObject()
216 , m_idToGIF()
217 , m_idToMACO()
218 , m_idToQkTm()
219 {
220 }
221
222 //! the map id to bitmap
223 std::map<int, MWAWEmbeddedObject> m_idToObject;
224 //! the map id to git
225 std::map<int, std::shared_ptr<VKFLImage> > m_idToGIF;
226 //! the map id to maco
227 std::map<std::vector<unsigned>, std::shared_ptr<VKFLImage> > m_idToMACO;
228 //! the map id to quicktime
229 std::map<int, librevenge::RVNGBinaryData> m_idToQkTm;
230 };
231
232 }
233
234 ////////////////////////////////////////////////////////////
235 // constructor/destructor, ...
236 ////////////////////////////////////////////////////////////
237 Canvas5Image::Canvas5Image(Canvas5Parser &parser)
238 : m_parserState(parser.getParserState())
239 , m_state(new Canvas5ImageInternal::State)
240 , m_mainParser(&parser)
241 {
242 }
243
244 Canvas5Image::~Canvas5Image()
245 {
246 }
247
248 int Canvas5Image::version() const
249 {
250 return m_parserState->m_version;
251 }
252
253 std::shared_ptr<Canvas5StyleManager> Canvas5Image::getStyleManager() const
254 {
255 return m_mainParser->m_styleManager;
256 }
257
258 bool Canvas5Image::getBitmap(int bitmapId, MWAWEmbeddedObject &object)
259 {
260 auto const &pIt=m_state->m_idToObject.find(bitmapId);
261 if (pIt==m_state->m_idToObject.end()) {
262 MWAW_DEBUG_MSG(("Canvas5Image::getBitmap: can not find bitmap %d\n", bitmapId));
263 return false;
264 }
265 object=pIt->second;
266 return true;
267 }
268
269 std::shared_ptr<Canvas5ImageInternal::VKFLImage> Canvas5Image::getGIF(int gifId)
270 {
271 auto const &pIt=m_state->m_idToGIF.find(gifId);
272 if (pIt==m_state->m_idToGIF.end()) {
273 MWAW_DEBUG_MSG(("Canvas5Image::getGIF: can not find GIF %d\n", gifId));
274 return nullptr;
275 }
276 return pIt->second;
277 }
278
279 std::shared_ptr<Canvas5ImageInternal::VKFLImage> Canvas5Image::getMACO(std::vector<unsigned> const &macoId)
280 {
281 auto const &pIt=m_state->m_idToMACO.find(macoId);
282 if (pIt==m_state->m_idToMACO.end()) {
283 MWAW_DEBUG_MSG(("Canvas5Image::getMACO: can not find a MACO picture\n"));
284 return nullptr;
285 }
286 return pIt->second;
287 }
288
289 bool Canvas5Image::getQuickTime(int quicktimeId, MWAWEmbeddedObject &object)
290 {
291 object=MWAWEmbeddedObject();
292 auto const &qIt=m_state->m_idToQkTm.find(quicktimeId);
293 if (qIt==m_state->m_idToQkTm.end()) {
294 MWAW_DEBUG_MSG(("Canvas5Image::getBitmap: can not find quicktime %d\n", quicktimeId));
295 return false;
296 }
297 object.add(qIt->second, "video/quicktime");
298 return true;
299 }
300
301 ////////////////////////////////////////////////////////////
302 //
303 // Intermediate level
304 //
305 ////////////////////////////////////////////////////////////
306
307 ////////////////////////////////////////////////////////////
308 // bitmap
309 ////////////////////////////////////////////////////////////
310
311 bool Canvas5Image::readImages(std::shared_ptr<Canvas5Structure::Stream> stream)
312 {
313 if (!stream || !stream->input()) return false;
314 auto input=stream->input();
315
316 long pos=input->tell();
317 if (!input->checkPosition(pos+4)) {
318 MWAW_DEBUG_MSG(("Canvas5Image::readImages: the zone is too short\n"));
319 return false;
320 }
321
322 libmwaw::DebugFile &ascFile = stream->ascii();
323 ascFile.addPos(pos);
324 ascFile.addNote("Entries(BitmDef):");
325 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
326
327 std::vector<bool> defined;
328 if (!m_mainParser->readDefined(*stream, defined, "BitmDef"))
329 return false;
330
331 // find list of 2bb73XXX, always multiple of 4 some auto ref ?
332 std::vector<unsigned long> unknowns;
333 if (!m_mainParser->readExtendedHeader(stream, 4, "BitmDef",
334 [&unknowns](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &, std::string const &) {
335 auto lInput=lStream->input();
336 unknowns.push_back(lInput->readULong(4));
337 }))
338 return false;
339
340 size_t w=0;
341 int const vers=version();
342 for (size_t i=0; i<defined.size(); ++i) {
343 if (!defined[i]) continue;
344 if (w>=unknowns.size())
345 break;
346 if (unknowns[w++]==0) continue;
347 MWAWEmbeddedObject object;
348 if (!Canvas5Structure::readBitmapDAD58Bim(*stream, vers, object))
349 return false;
350 m_state->m_idToObject[int(i+1)]=object;
351 }
352 return true;
353 }
354
355 bool Canvas5Image::readImages9(std::shared_ptr<Canvas5Structure::Stream> stream)
356 {
357 if (!stream || !stream->input()) return false;
358
359 auto input=stream->input();
360 auto &ascFile=stream->ascii();
361 int const vers=version();
362 long pos=input->tell();
363 if (!input->checkPosition(pos+8)) {
364 MWAW_DEBUG_MSG(("Canvas5Image::readImages9: the zone is too short\n"));
365 return false;
366 }
367 libmwaw::DebugStream f;
368 f << "Entries(Image):";
369 int val=int(input->readLong(4));
370 if (val!=3)
371 f << "f0=" << val << ",";
372 int N=int(input->readLong(4));
373 f << "N=" << N << ",";
374 ascFile.addPos(pos);
375 ascFile.addNote(f.str().c_str());
376
377 for (int i=0; i<N; ++i) {
378 pos=input->tell();
379 f.str("");
380 f << "Image-B" << i+1 << ":";
381 std::string name;
382 int type;
383 if (!m_mainParser->getTAG9(*stream, name, type) || type!=0) {
384 MWAW_DEBUG_MSG(("Canvas5Image::readImages9: can not find the image tag\n"));
385 f << "###";
386 ascFile.addPos(pos);
387 ascFile.addNote(f.str().c_str());
388 return false;
389 }
390 f << "name=" << name << ",";
391 if (!input->checkPosition(input->tell()+4)) {
392 MWAW_DEBUG_MSG(("Canvas5Image::readImages9: the zone seems too short\n"));
393 f << "###";
394 ascFile.addPos(pos);
395 ascFile.addNote(f.str().c_str());
396 return false;
397 }
398 int id=int(input->readLong(4));
399 f << "id=" << id << ",";
400 ascFile.addPos(pos);
401 ascFile.addNote(f.str().c_str());
402 MWAWEmbeddedObject object;
403 if (!Canvas5Structure::readBitmapDAD58Bim(*stream, vers, object))
404 return false;
405 if (m_state->m_idToObject.find(i+1) != m_state->m_idToObject.end()) {
406 MWAW_DEBUG_MSG(("Canvas5Image::readImages9: id=%d already exists\n", i+1));
407 }
408 else
409 m_state->m_idToObject[i+1]=object;
410 if (!m_mainParser->checkTAG9(*stream, name, 1)) {
411 MWAW_DEBUG_MSG(("Canvas5Image::readImages9: can not find the image tag\n"));
412 ascFile.addPos(input->tell());
413 ascFile.addNote("Image:###");
414 return false;
415 }
416 }
417 return true;
418 }
419
420 ////////////////////////////////////////////////////////////
421 // macros
422 ////////////////////////////////////////////////////////////
423 bool Canvas5Image::readMacroIndent(Canvas5Structure::Stream &stream, std::vector<unsigned> &id, std::string &extra)
424 {
425 id.clear();
426
427 auto input=stream.input();
428 long pos=input ? input->tell() : 0;
429
430 if (!input || !input->checkPosition(pos+20)) {
431 MWAW_DEBUG_MSG(("Canvas5Image::readMacroIndent: can not read first MACO value\n"));
432 extra="###";
433 return false;
434 }
435
436 std::stringstream s;
437 for (int k=0; k<8; ++k) { // f0,f1,f2: a date, f3,f4,f5: hour?
438 unsigned val=unsigned(input->readULong(2));
439 unsigned const expected[]= {1998,10,5,14,0,0,2,1100};
440
441 if (k>=0 && k<=6)
442 id.push_back(val);
443 if (val==expected[k]) continue;
444 if (k==7)
445 s << "fl=" << std::hex << val << std::dec << ",";
446 else
447 s << "f" << k << "=" << val << ",";
448 }
449 int val=int(input->readLong(4));
450 if (val)
451 s << "id=" << val << ",";
452 extra=s.str();
453 return true;
454 }
455
456 bool Canvas5Image::readMACORsrc(std::shared_ptr<Canvas5Structure::Stream> stream)
457 {
458 if (!stream) {
459 MWAW_DEBUG_MSG(("Canvas5Parser::readMACORsrc: no stream\n"));
460 return false;
461 }
462 auto input=stream->input();
463 long pos=input ? input->tell() : 0;
464 int const vers=version();
465 auto &ascFile=stream->ascii();
466 libmwaw::DebugStream f;
467 f << "Entries(Macros):";
468 if (!input || !input->checkPosition(pos+4)) {
469 MWAW_DEBUG_MSG(("Canvas5Parser::readMACORsrc: can not read first MACO value\n"));
470 f << "###";
471 ascFile.addPos(pos);
472 ascFile.addNote(f.str().c_str());
473 return false;
474 }
475 int val=int(input->readULong(4));
476 if (val!=0x77cc) f << "f0=" << std::hex << val << std::dec << ",";
477 ascFile.addPos(pos);
478 ascFile.addNote(f.str().c_str());
479 if (input->isEnd())
480 return true;
481
482 // 32: name, dim, last part of DataShap Maco, another dim
483 std::map<int, std::vector<unsigned> > idToUniqueIdMap;
484 std::set<std::vector<unsigned> > uniqueIdSet;
485 if (!m_mainParser->readExtendedHeader(stream, vers>=9 ? 0x80 : 0x58, "Macros",
486 [this, vers, &idToUniqueIdMap, &uniqueIdSet](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
487 auto lInput=lStream->input();
488 libmwaw::DebugFile &asciiFile = lStream->ascii();
489 long lPos=lInput->tell();
490 libmwaw::DebugStream lF;
491 std::string name;
492 for (int i=0; i<32; ++i) {
493 char c=char(lInput->readULong(1));
494 if (!c)
495 break;
496 name+=c;
497 }
498 lF << name << ",";
499 lInput->seek(lPos+32, librevenge::RVNG_SEEK_SET);
500 for (int i=0; i<(vers<9 ? 2 : 4); ++i) { // checkme is g0 related to endian ordering?
501 int lVal=int(lInput->readLong(2));
502 if (lVal)
503 lF << "g" << i << "=" << lVal << ",";
504 }
505 if (vers>=9) {
506 double dim[4];
507 bool isNan;
508 for (auto &d : dim) {
509 long actPos=lInput->tell();
510 if (m_mainParser->readDouble(*lStream, d, isNan)) continue;
511 d=0;
512 lF << "###";
513 lInput->seek(actPos+8, librevenge::RVNG_SEEK_SET);
514 }
515 lF << "box=" << MWAWBox2f(MWAWVec2f((float) dim[0], (float) dim[1]),
516 MWAWVec2f((float) dim[2], (float) dim[3])) << ",";
517 std::vector<unsigned> mId;
518 std::string extra;
519
520 if (!readMacroIndent(*lStream, mId, extra) || uniqueIdSet.find(mId)!=uniqueIdSet.end()) {
521 MWAW_DEBUG_MSG(("Canvas5Image::readMACORsrc: oops, find multiple unique id\n"));
522 lF << "###";
523 lInput->seek(lPos+32+8+32+20, librevenge::RVNG_SEEK_SET);
524 }
525 else {
526 idToUniqueIdMap[item.m_id]=mId;
527 uniqueIdSet.insert(mId);
528 }
529 int lVal=int(lInput->readLong(4));
530 if (lVal)
531 lF << "g4=" << lVal << ",";
532 for (auto &d : dim) {
533 long actPos=lInput->tell();
534 if (m_mainParser->readDouble(*lStream, d, isNan)) continue;
535 d=0;
536 lF << "###";
537 lInput->seek(actPos+8, librevenge::RVNG_SEEK_SET);
538 }
539 lF << "box2=" << MWAWBox2f(MWAWVec2f((float) dim[0], (float) dim[1]),
540 MWAWVec2f((float) dim[2], (float) dim[3])) << ",";
541 asciiFile.addPos(item.m_pos);
542 asciiFile.addNote(lF.str().c_str());
543 return ;
544 }
545 float fDim[4];
546 for (auto &d : fDim) d=float(lInput->readULong(4))/65536.f;
547 lF << "box=" << MWAWBox2f(MWAWVec2f(fDim[0], fDim[1]), MWAWVec2f(fDim[2], fDim[3])) << ",";
548
549 std::vector<unsigned> mId;
550 std::string extra;
551
552 if (!readMacroIndent(*lStream, mId, extra) || uniqueIdSet.find(mId)!=uniqueIdSet.end()) {
553 MWAW_DEBUG_MSG(("Canvas5Image::readMACORsrc: oops, find multiple unique id\n"));
554 lF << "###";
555 lInput->seek(lPos+32+20+20, librevenge::RVNG_SEEK_SET);
556 }
557 else {
558 idToUniqueIdMap[item.m_id]=mId;
559 uniqueIdSet.insert(mId);
560 }
561 for (auto &d : fDim) d=float(lInput->readULong(4))/65536.f;
562 lF << "box2=" << MWAWBox2f(MWAWVec2f(fDim[0], fDim[1]), MWAWVec2f(fDim[2], fDim[3])) << ",";
563 asciiFile.addPos(item.m_pos);
564 asciiFile.addNote(lF.str().c_str());
565 })) {
566 return false;
567 }
568 if (input->isEnd())
569 return true;;
570 if (!m_mainParser->readIndexMap(stream, "Macros",
571 [this, &idToUniqueIdMap](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
572 std::shared_ptr<Canvas5ImageInternal::VKFLImage> image;
573 if (!readVKFL(lStream, item.m_length, image)) return;
574 auto it=idToUniqueIdMap.find(item.m_id);
575 if (it==idToUniqueIdMap.end()) {
576 MWAW_DEBUG_MSG(("Canvas5Image::readMACORsrc: oops, can not find an unique id for %d\n", item.m_id));
577 }
578 else
579 m_state->m_idToMACO[it->second]=image;
580 })) { // vkfl
581 MWAW_DEBUG_MSG(("Canvas5Image::readMACORsrc: can not read the first data value\n"));
582 return false;
583 }
584 if (input->isEnd())
585 return true;
586 if (!m_mainParser->readUsed(*stream, "Macros")) {
587 MWAW_DEBUG_MSG(("Canvas5Image::readMACORsrc: can not read the used value\n"));
588 return false;
589 }
590 pos=input->tell();
591 f.str("");
592 f << "Macros-F:";
593 int N;
594 if (!m_mainParser->readDataHeader(*stream, 0x14,N)) {
595 MWAW_DEBUG_MSG(("Canvas5Image::readMACORsrc: can not read the last data value\n"));
596 f << "###";
597 ascFile.addPos(pos);
598 ascFile.addNote(f.str().c_str());
599 return false;
600 }
601 ascFile.addPos(pos);
602 ascFile.addNote(f.str().c_str());
603 for (int j=0; j<N; ++j) {
604 pos=input->tell();
605 f.str("");
606 f << "Macros-F" << j << ":";
607 std::vector<unsigned> mId;
608 std::string extra;
609 readMacroIndent(*stream, mId, extra);
610 f << extra << ",";
611 ascFile.addPos(pos);
612 ascFile.addNote(f.str().c_str());
613 input->seek(pos+0x14, librevenge::RVNG_SEEK_SET);
614 }
615
616 return true;
617 }
618
619
620 ////////////////////////////////////////////////////////////
621 //
622 // VKFL
623 //
624 ////////////////////////////////////////////////////////////
625
626 bool Canvas5Image::readAGIFRsrc(std::shared_ptr<Canvas5Structure::Stream> stream)
627 {
628 if (!stream || !stream->input())
629 return false;
630 auto input=stream->input();
631 long pos=input->tell();
632 libmwaw::DebugStream f;
633 auto &ascFile=stream->ascii();
634 f << "RsrcAGIF:";
635
636 if (!input->checkPosition(pos+56)) {
637 MWAW_DEBUG_MSG(("Canvas5Image::readAGIFRsrc: the zone seems too short\n"));
638 f << "###";
639 ascFile.addPos(pos);
640 ascFile.addNote(f.str().c_str());
641 return false;
642 }
643
644 int val=int(input->readLong(4));
645 if (val!=2)
646 f << "f0=" << val << ",";
647 int N=int(input->readULong(4));
648 f << "N=" << N << ",";
649 if (N<1) {
650 MWAW_DEBUG_MSG(("Canvas5Image::readAGIFRsrc: the N value seems bad\n"));
651 f << "###";
652 ascFile.addPos(pos);
653 ascFile.addNote(f.str().c_str());
654 return false;
655 }
656 ascFile.addPos(pos);
657 ascFile.addNote(f.str().c_str());
658
659 for (int z=0; z<N; ++z) {
660 pos=input->tell();
661 f.str("");
662 f << "RsrcAGIF" << z << ":";
663
664 if (!input->checkPosition(pos+4)) {
665 MWAW_DEBUG_MSG(("Canvas5Image::readAGIFRsrc: the zone %d seems too short\n", z));
666 f << "###";
667 ascFile.addPos(pos);
668 ascFile.addNote(f.str().c_str());
669 return false;
670 }
671 val=int(input->readLong(4));
672 if (val==0) {
673 ascFile.addPos(pos);
674 ascFile.addNote(f.str().c_str());
675 continue;
676 }
677 if (val!=1 || !input->checkPosition(pos+20)) {
678 MWAW_DEBUG_MSG(("Canvas5Image::readAGIFRsrc: find unknown identifier for the sub zone %d\n", z));
679 f << "###id=" << val << ",";
680 ascFile.addPos(pos);
681 ascFile.addNote(f.str().c_str());
682 return false;
683 }
684 int N0=1;
685 for (int i=0; i<4; ++i) {
686 val=int(input->readLong(4));
687 if (val==1) continue;
688 if (i==1) {
689 N0=val;
690 f << "N[subZ]=" << N0 << ",";
691 }
692 else
693 f << "f" << i << "=" << val << ",";
694 }
695
696 ascFile.addPos(pos);
697 ascFile.addNote(f.str().c_str());
698 for (int s=0; s<N0; ++s) {
699 pos=input->tell();
700 f.str("");
701 f << "RsrcAGIF" << z << "-" << s << ":";
702 if (!input->checkPosition(pos+24)) {
703 MWAW_DEBUG_MSG(("Canvas5Image::readAGIFRsrc: the sub zone %d-%d seems too short\n", z, s));
704 f << "###";
705 ascFile.addPos(pos);
706 ascFile.addNote(f.str().c_str());
707 return false;
708 }
709 val=int(input->readULong(4));
710 if (val!=0)
711 f << "unkn=" << std::hex << val << std::dec << ",";
712 val=int(input->readULong(4));
713 long len=long(input->readULong(4));
714 if (val!=0x3e23d70a || pos+24+len<pos+24 || !input->checkPosition(pos+24+len)) {
715 MWAW_DEBUG_MSG(("Canvas5Image::readAGIFRsrc: the sub zone %d-%d seems bad\n", z, s));
716 f << "###";
717 ascFile.addPos(pos);
718 ascFile.addNote(f.str().c_str());
719 return false;
720 }
721 if (len) {
722 std::shared_ptr<Canvas5ImageInternal::VKFLImage> image;
723 if (!readVKFL(stream, len, image)) {
724 f << "###";
725 input->seek(pos+12+len, librevenge::RVNG_SEEK_SET);
726 }
727 else
728 m_state->m_idToGIF[s]=image;
729 ascFile.addPos(pos);
730 ascFile.addNote(f.str().c_str());
731
732 pos=input->tell();
733 f.str("");
734 f << "RsrcAGIF" << z << "-" << s << "[B]:";
735 }
736 for (int i=0; i<3; ++i) { // g2=1 means continue
737 val=int(input->readLong(4));
738 int const expected[]= {0,1,0};
739 if (val!=expected[i])
740 f << "g" << i << "=" << val << ",";
741 }
742 ascFile.addPos(pos);
743 ascFile.addNote(f.str().c_str());
744 }
745 }
746 if (!input->isEnd()) {
747 MWAW_DEBUG_MSG(("Canvas5Image::readAGIFRsrc: find extra data\n"));
748 ascFile.addPos(input->tell());
749 ascFile.addNote("RsrcAGIF-end:###extra");
750 }
751 return true;
752 }
753
754 bool Canvas5Image::readQkTmRsrc(Canvas5Structure::Stream &stream)
755 {
756 auto input=stream.input();
757 long pos=input->tell();
758 libmwaw::DebugStream f;
759 auto &ascFile=stream.ascii();
760 f << "RsrcQkTm:";
761
762 if (!input->checkPosition(pos+4)) {
763 MWAW_DEBUG_MSG(("Canvas5Image::readQkTmRsrc: the zone seems too short\n"));
764 f << "###";
765 ascFile.addPos(pos);
766 ascFile.addNote(f.str().c_str());
767 return false;
768 }
769
770 int N=int(input->readULong(4));
771 f << "N=" << N << ",";
772 ascFile.addPos(pos);
773 ascFile.addNote(f.str().c_str());
774
775 for (int z=0; z<N; ++z) {
776 pos=input->tell();
777 f.str("");
778 f << "RsrcQkTm-QK" << z+1 << ":";
779 if (!input->checkPosition(pos+44)) {
780 MWAW_DEBUG_MSG(("Canvas5Image::readQkTmRsrc: the %d zone seems too short\n", z));
781 f << "###";
782 ascFile.addPos(pos);
783 ascFile.addNote(f.str().c_str());
784 return false;
785 }
786 input->seek(pos+40, librevenge::RVNG_SEEK_SET);
787 long len=input->readLong(4);
788 if (len<0 || pos+44+len<pos+44 || !input->checkPosition(pos+44+len)) {
789 MWAW_DEBUG_MSG(("Canvas5Image::readQkTmRsrc: the %d zone len seems bad\n", z));
790 f << "###len=" << len << ",";
791 ascFile.addPos(pos);
792 ascFile.addNote(f.str().c_str());
793 return false;
794 }
795 ascFile.addPos(pos);
796 ascFile.addNote(f.str().c_str());
797
798 if (len) {
799 librevenge::RVNGBinaryData movie;
800 if (!input->readDataBlock(len, movie)) {
801 MWAW_DEBUG_MSG(("Canvas5Image::readQkTmRsrc: oops can not retrieve the %d movie\n", z));
802 f << "###";
803 }
804 else {
805 m_state->m_idToQkTm[z+1]=movie;
806 #ifdef DEBUG_WITH_FILES
807 std::stringstream s;
808 static int index=0;
809 s << "movie" << ++index << ".mov";
810 libmwaw::Debug::dumpFile(movie, s.str().c_str());
811 #endif
812 }
813 ascFile.skipZone(pos+44, pos+44+len-1);
814 }
815 input->seek(pos+44+len, librevenge::RVNG_SEEK_SET);
816 }
817 return true;
818 }
819
820 bool Canvas5Image::getArrow(std::shared_ptr<Canvas5ImageInternal::VKFLImage> image, MWAWGraphicStyle::Arrow &arrow) const
821 {
822 arrow=MWAWGraphicStyle::Arrow::plain();
823 if (!image) {
824 MWAW_DEBUG_MSG(("Canvas5Image::getArrow: can not find the image\n"));
825 return false;
826 }
827 std::vector<int> typeList;
828 for (auto const &shape : image->m_shapes) {
829 if (shape.m_type>=0 && shape.m_type!=11)
830 typeList.push_back(shape.m_type);
831 }
832 if (typeList.size()==1) {
833 // TODO: get the real shape and extract the path
834 switch (typeList[0]) {
835 case 1:
836 case 2:
837 arrow=MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(20,30)), "M1013 1491l118 89-567-1580-564 1580 114-85 136-68 148-46 161-17 161 13 153 46z", false);
838 break;
839 case 7:
840 arrow=MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(1131,1131)), "M462 1118l-102-29-102-51-93-72-72-93-51-102-29-102-13-105 13-102 29-106 51-102 72-89 93-72 102-50 102-34 106-9 101 9 106 34 98 50 93 72 72 89 51 102 29 106 13 102-13 105-29 102-51 102-72 93-93 72-98 51-106 29-101 13z", false);
841 break;
842 case 10:
843 arrow=MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(20,30)), "m10 0l-10 30h20z", false);
844 break;
845 default:
846 MWAW_DEBUG_MSG(("Canvas5Image::readArrow: find unexpected arrow with type %d\n", typeList[0]));
847 break;
848 }
849 }
850 else if (typeList.size()==2 && typeList[0]==1 && typeList[1]==1)
851 arrow=MWAWGraphicStyle::Arrow(10, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(40,35)), "m20 0l-20 0 l0 4 l20 0 l-10 30 l20 0 l-10 -30 l20 0 l0 -4z", false);
852 else if (typeList.size()==2 && typeList[0]==2 && typeList[1]==2)
853 arrow=MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(20,30)), "m0 3 h-20 v-3 h40 v3 h-20 l-10 30 h20z", false);
854 else if (typeList.size()==3 && typeList[0]==10)
855 arrow=MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(20,33)), "m10 0l-10 30 l10 3 l10 -3z", false);
856 else {
857 MWAW_DEBUG_MSG(("Canvas5Image::readArrow: find unexpected arrow with size=%d\n", int(typeList.size())));
858 }
859 return true;
860 }
861
862 bool Canvas5Image::getTexture(std::shared_ptr<Canvas5ImageInternal::VKFLImage> image, MWAWEmbeddedObject &texture, MWAWVec2i &textureDim, MWAWColor &averageColor) const
863 {
864 if (!image) {
865 MWAW_DEBUG_MSG(("Canvas5Image::getTexture: can not find the image\n"));
866 return false;
867 }
868 bool bitmapFound=false;
869 for (auto const &shape : image->m_shapes) {
870 if (shape.m_type!=14 || shape.m_bitmap.isEmpty()) continue;
871 if (bitmapFound) {
872 MWAW_DEBUG_MSG(("Canvas5Image::getTexture: found multiple textures, return the first one\n"));
873 return true;
874 }
875 bitmapFound=true;
876 texture=shape.m_bitmap;
877 textureDim=MWAWVec2i(shape.m_box.size());
878 averageColor=shape.m_bitmapColor;
879 }
880 if (bitmapFound)
881 return true;
882 MWAW_DEBUG_MSG(("Canvas5Image::getTexture: can not find any texture\n"));
883 return false;
884 }
885
886 bool Canvas5Image::readVKFL(std::shared_ptr<Canvas5Structure::Stream> stream, long len, std::shared_ptr<Canvas5ImageInternal::VKFLImage> &image)
887 {
888 image.reset();
889 if (!stream)
890 return false;
891 if (len==0)
892 return true;
893 auto input=stream->input();
894 int const vers=version();
895 long pos=input->tell();
896 long endPos=pos+len;
897 int const headerLen=vers<9 ? 180 : 288;
898 if (len<headerLen || !input->checkPosition(endPos)) {
899 MWAW_DEBUG_MSG(("Canvas5Image::readVKFL: the zone seems too short\n"));
900 return false;
901 }
902
903 auto &ascFile=stream->ascii();
904 libmwaw::DebugStream f;
905
906 f << "Entries(Vkfl):";
907 int val=int(input->readLong(4));
908 if (val!=256)
909 f << "f0=" << val << ",";
910 auto tBegin=long(input->readULong(4));
911 auto tLen=long(input->readULong(4));
912 f << "pos=" << tBegin << "<->" << tBegin+tLen << ",";
913 if (tBegin+tLen<0 || tLen<36 || tBegin<headerLen || tBegin+tLen>len) {
914 f << "###";
915 MWAW_DEBUG_MSG(("Canvas5Image::readVKFL: can not read the data length length\n"));
916 ascFile.addPos(pos);
917 ascFile.addNote(f.str().c_str());
918 return false;
919 }
920 if (tBegin+tLen<len) {
921 ascFile.addPos(pos+tBegin+tLen);
922 ascFile.addNote("Vkfl-[end]:###");
923 MWAW_DEBUG_MSG(("Canvas5Image::readVKFL: find extra data\n"));
924 }
925
926 image=std::make_shared<Canvas5ImageInternal::VKFLImage>();
927 if (vers>=9)
928 input->seek(4, librevenge::RVNG_SEEK_CUR);
929 float dim[4];
930 for (auto &d : dim) d=float(m_mainParser->readDouble(*stream,vers<9 ? 4 : 8));
931 if (vers<9)
932 image->m_boxes[0]=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
933 else
934 image->m_boxes[0]=MWAWBox2f(MWAWVec2f(dim[0],dim[1]),MWAWVec2f(dim[2],dim[3]));
935 f << "dim=" << image->m_boxes[0] << ",";
936 for (int i=0; i<2; ++i) {
937 val=int(input->readLong(4));
938 if (val!=1-i)
939 f << "f" << i+2 << "=" << val << ",";
940 }
941 for (auto &d : dim) d=float(m_mainParser->readDouble(*stream,vers<9 ? 4 : 8));
942 if (vers<9)
943 image->m_boxes[1]=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
944 else
945 image->m_boxes[1]=MWAWBox2f(MWAWVec2f(dim[0],dim[1]),MWAWVec2f(dim[2],dim[3]));
946 if (image->m_boxes[0]!=image->m_boxes[1])
947 f << "dim2=" << image->m_boxes[1] << ",";
948 for (int st=0; st<2; ++st) {
949 f << "mat" << st << "=[";
950 for (auto &d : image->m_matrices[st]) {
951 d=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
952 f << d << ",";
953 }
954 f << "],";
955 }
956 for (int j=0; j<3; ++j) { // g1=54|6c
957 val=int(input->readLong(4));
958 if (val!=-1)
959 f << "g" << j << "=" << val << ",";
960 }
961 int dDim[2]; // g1+48, g1+24
962 for (auto &d : dDim) d=int(input->readLong(4));
963 if (vers<9)
964 f << "dim3=" << MWAWVec2i(dDim[1],dDim[0]) << ",";
965 else
966 f << "dim3=" << MWAWVec2i(dDim[0],dDim[1]) << ",";
967
968 long firstBlockDecal=0;
969 for (int j=0; j<5; ++j) {
970 val=int(input->readLong(4));
971 int const expected[]= {-1,-1,1,0,0};
972 if (val==expected[j]) continue;
973 if (j==3)
974 firstBlockDecal=val;
975 f << "g" << j+3 << "=" << val << ",";
976 }
977
978 f << "entries=[";
979 for (auto &entry : image->m_data) {
980 entry.setBegin(input->readLong(4)+pos);
981 entry.setLength(input->readLong(4));
982 f << std::hex << entry.begin() << ":" << entry.end() << std::dec << ",";
983 if (entry.begin()<pos || entry.end()>pos+tBegin+tLen) {
984 MWAW_DEBUG_MSG(("Canvas5Image::readVKFL: unexpected subs size for an effect\n"));
985 f << "###";
986 entry.setLength(0);
987 }
988 }
989 f << ",";
990
991 ascFile.addPos(pos);
992 ascFile.addNote(f.str().c_str());
993
994 std::set<long> seen;
995 long begPos=image->m_data[1].begin();
996 if (image->m_data[1].valid() && firstBlockDecal>=0 && firstBlockDecal<=image->m_data[1].length()) {
997 input->seek(begPos+firstBlockDecal, librevenge::RVNG_SEEK_SET);
998 while (input->tell()<image->m_data[1].end()) {
999 if (input->tell()<begPos || seen.find(input->tell())!=seen.end()) {
1000 MWAW_DEBUG_MSG(("Canvas5Image::readVKFL: oops find a loop\n"));
1001 break;
1002 }
1003 seen.insert(input->tell());
1004 if (!readVKFLShape(stream, *image))
1005 break;
1006 }
1007 }
1008 else if (firstBlockDecal>=0) {
1009 MWAW_DEBUG_MSG(("Canvas5Image::readVKFL: first block seems bad\n"));
1010 }
1011
1012 if (!image->m_data[0].valid()) {
1013 MWAW_DEBUG_MSG(("Canvas5Image::readVKFL: can not find any data0 zoone\n"));
1014 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1015 return true;
1016 }
1017
1018 std::stack<std::pair<long,int> > dataStack;
1019 for (auto const &shape : image->m_shapes) {
1020 for (auto const &it : shape.m_idToDataPos) {
1021 pos=begPos+it.second;
1022 if (seen.find(pos)!=seen.end()) // already parsed or pb
1023 continue;
1024 seen.insert(pos);
1025 dataStack.push(std::make_pair(pos, it.first));
1026 }
1027 }
1028
1029 while (!dataStack.empty()) {
1030 auto posId=dataStack.top();
1031 dataStack.pop();
1032
1033 pos=posId.first;
1034 if (pos<begPos || pos+24>image->m_data[1].end()) {
1035 MWAW_DEBUG_MSG(("Canvas5Image::readVKFL: can not find sub zone0[%lx]\n", posId.first));
1036 continue;
1037 }
1038 input->seek(pos, librevenge::RVNG_SEEK_SET);
1039 f.str("");
1040 if (posId.second>=0)
1041 f << "Vkfl-VK" << std::hex << pos-begPos << std::dec << "A[" << posId.second << "]:";
1042 else
1043 f << "Vkfl-VK" << std::hex << pos-begPos << std::dec << "A:";
1044 unsigned dataType=unsigned(input->readULong(4));
1045 auto unknPos=input->readULong(4);
1046 if (unknPos!=0xFFFFFFFF) {
1047 long childPos=begPos+long(unknPos);
1048 f << "unkn=VK" << std::hex << long(unknPos) << std::dec << ",";
1049 if (seen.find(childPos)==seen.end()) {
1050 seen.insert(childPos);
1051 dataStack.push(std::make_pair(childPos,-1));
1052 }
1053 }
1054 long dataLength[2];
1055 dataLength[0]=input->readLong(4);
1056 val=int(input->readULong(4));
1057 if (val) f << "f0=" << val << ",";
1058 unsigned dataSubType=unsigned(input->readULong(4));
1059 dataLength[1]=input->readLong(4);
1060 image->m_posToTypesMap[pos-begPos]=std::make_pair(dataType, dataSubType);
1061 if (dataLength[0]) {
1062 MWAWEntry data;
1063 data.setId(int(dataType));
1064 data.setBegin(image->m_data[0].begin()+dataLength[1]);
1065 data.setLength(dataLength[0]);
1066 f << "data=[" << Canvas5Structure::getString(dataType) << "-" << Canvas5Structure::getString(dataSubType) << ","
1067 << std::hex << data.begin() << "->" << data.end() << std::dec << "],";
1068 std::vector<long> childFieldPos;
1069 input->pushLimit(data.end());
1070 readVKFLShapeOtherData(stream, *image, std::make_tuple(data, dataSubType, pos-begPos),
1071 childFieldPos, posId.second);
1072 input->popLimit();
1073 for (auto cPos : childFieldPos) {
1074 if (cPos<0) continue;
1075 long childPos=begPos+cPos;
1076 if (seen.find(childPos)==seen.end()) {
1077 seen.insert(childPos);
1078 dataStack.push(std::make_pair(childPos,-1));
1079 }
1080 }
1081 }
1082 ascFile.addPos(pos);
1083 ascFile.addNote(f.str().c_str());
1084 input->seek(pos+24, librevenge::RVNG_SEEK_SET);
1085 ascFile.addPos(pos+24);
1086 ascFile.addNote("_");
1087 }
1088
1089 #ifdef DEBUG_CANVAS_VKFL
1090 MWAWGraphicEncoder graphicEncoder;
1091 MWAWBox2f pictBox=image->m_boxes[0]; // 0: image size, 1: image + translation
1092 pictBox.setMax(pictBox[1]+MWAWVec2f(100,100));
1093 auto graphicListener=std::make_shared<MWAWGraphicListener>(*m_parserState, pictBox, &graphicEncoder);
1094 graphicListener->startDocument();
1095 MWAWTransformation transf;
1096 auto const &mat=image->m_matrices[0];
1097 if (mat[2]<-1e-3 || mat[2]>1e-3 || mat[5]<-1e-3 || mat[5]>1e-3) {
1098 MWAW_DEBUG_MSG(("Canvas5Image::readVKFL: image matrix will be ignored\n"));
1099 }
1100 else
1101 transf=MWAWTransformation(MWAWVec3f((float)mat[0],(float)mat[3],(float)mat[6]), MWAWVec3f((float)mat[1],(float)mat[4],(float)mat[7]));
1102
1103 send(image, graphicListener, pictBox, transf);
1104 graphicListener->endDocument();
1105 MWAWEmbeddedObject picture;
1106 if (graphicEncoder.getBinaryResult(picture) && !picture.m_dataList.empty()) {
1107 static int vkflId=0;
1108 std::stringstream s;
1109 s << "Vkfl" << ++vkflId << ".odg";
1110 libmwaw::Debug::dumpFile(picture.m_dataList[0], s.str().c_str());
1111 }
1112 #endif
1113
1114 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1115 return true;
1116 }
1117
1118 bool Canvas5Image::readVKFLShape(std::shared_ptr<Canvas5Structure::Stream> stream, Canvas5ImageInternal::VKFLImage &image)
1119 {
1120 if (!stream || !image.m_data[1].valid()) {
1121 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShape: the image data1 is not valid\n"));
1122 return false;
1123 }
1124
1125 auto input=stream->input();
1126 int const vers=version();
1127 long pos=input->tell();
1128 long begPos=image.m_data[1].begin();
1129 long endPos=image.m_data[1].end();
1130 auto &ascFile=stream->ascii();
1131 libmwaw::DebugStream f;
1132
1133 int id=1+int(image.m_shapes.size());
1134 f << "Vkfl-s" << id << ":";
1135 int const headerLen=vers<9 ? 44 : 64;
1136 if (pos+headerLen>endPos) {
1137 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShape: the zone seems too short\n"));
1138 f << "###";
1139 ascFile.addPos(pos);
1140 ascFile.addNote(f.str().c_str());
1141 return false;
1142 }
1143 image.m_shapes.push_back(Canvas5ImageInternal::VKFLShape());
1144 auto &shape=image.m_shapes.back();
1145 shape.m_type=int(input->readLong(4)); // find 1-14
1146 f << "type=" << Canvas5ImageInternal::VKFLShape::getTypeName(shape.m_type) << ",";
1147 if (vers>=9)
1148 input->seek(4, librevenge::RVNG_SEEK_CUR);
1149 float dim[4];
1150 for (auto &d : dim) d=float(m_mainParser->readDouble(*stream, vers<9 ? 4 : 8));
1151 if (vers<9)
1152 shape.m_box=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
1153 else
1154 shape.m_box=MWAWBox2f(MWAWVec2f(dim[0],dim[1]),MWAWVec2f(dim[2],dim[3]));
1155 f << "dim=" << shape.m_box << ",";
1156 int val=int(input->readULong(4)); // 0|8000
1157 if (val) f << "fl0=" << std::hex << val << std::dec << ",";
1158 auto fl=input->readULong(4); // [347]|[01]ff
1159 if (fl) f << "fl1=" << std::hex << fl << std::dec << ",";
1160 auto decal=input->readULong(4);
1161 long nextPos=decal==0xFFFFFFFF ? endPos : begPos+long(decal);
1162 if (nextPos<=begPos || (nextPos>=pos && nextPos<pos+44) || nextPos>endPos) {
1163 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShape: the zone seems too short\n"));
1164 f << "###";
1165 ascFile.addPos(pos);
1166 ascFile.addNote(f.str().c_str());
1167 return false;
1168 }
1169 long dataLength[2];
1170 dataLength[0]=input->readLong(4);
1171 shape.m_subType=unsigned(input->readULong(4));
1172 dataLength[1]=input->readLong(4);
1173 if (dataLength[0])
1174 f << "data=[" << Canvas5Structure::getString(shape.m_subType) << "," << std::hex << image.m_data[0].begin()+dataLength[1] << "->" << image.m_data[0].begin()+dataLength[0]+dataLength[1] << std::dec << "],";
1175 else if (shape.m_subType && shape.m_type==11)
1176 f << "N=" << shape.m_subType << ",";
1177 else if (shape.m_subType)
1178 f << "unkn=" << Canvas5Structure::getString(shape.m_subType) << ",";
1179
1180 unsigned dec=1;
1181 for (int i=0; i<12; ++i, dec*=2) {
1182 if ((fl&dec)==0) continue;
1183 if (input->tell()+4>nextPos) {
1184 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShape: the zone seems too short\n"));
1185 f << "###";
1186 ascFile.addPos(pos);
1187 ascFile.addNote(f.str().c_str());
1188 break;
1189 }
1190 long decalData=long(input->readLong(4));
1191 if (decalData>=0) {
1192 shape.m_idToDataPos[i]=decalData;
1193 char const *wh[]= {"surfColor", "lineColor", "stroke", "matrix", "matrix1",
1194 "matrix2", nullptr, nullptr, nullptr, "name"
1195 };
1196 if (i<10 && wh[i])
1197 f << "beg[" << wh[i] << "]=VK" << std::hex << decalData << std::dec << ",";
1198 else
1199 f << "beg[t" << i << "]=VK" << std::hex << decalData << std::dec << ",";
1200 }
1201 }
1202 ascFile.addPos(pos);
1203 ascFile.addNote(f.str().c_str());
1204 ascFile.addPos(input->tell());
1205 ascFile.addNote("_");
1206
1207 if (dataLength[0]) {
1208 MWAWEntry data;
1209 data.setBegin(image.m_data[0].begin()+dataLength[1]);
1210 data.setLength(dataLength[0]);
1211 readVKFLShapeMainData(stream, image, shape, data);
1212 }
1213
1214 input->seek(nextPos, librevenge::RVNG_SEEK_SET);
1215 return true;
1216 }
1217
1218 bool Canvas5Image::readVKFLShapeMainData(std::shared_ptr<Canvas5Structure::Stream> stream, Canvas5ImageInternal::VKFLImage &image, Canvas5ImageInternal::VKFLShape &shape, MWAWEntry const &data)
1219 {
1220 if (!data.valid() || !stream)
1221 return true;
1222
1223 auto input=stream->input();
1224 int const vers=version();
1225 if (!input || !input->checkPosition(data.end()) || data.begin()<image.m_data[0].begin() || data.end()>image.m_data[0].end()) {
1226 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeMainData: the entry seems bad\n"));
1227 return false;
1228 }
1229
1230 libmwaw::DebugStream f;
1231 auto &ascFile=stream->ascii();
1232 f << "Vkfl-s" << image.m_shapes.size() << "M[" << Canvas5ImageInternal::VKFLShape::getTypeName(shape.m_type) << ":" << Canvas5Structure::getString(shape.m_subType) << "]:";
1233 input->seek(data.begin(), librevenge::RVNG_SEEK_SET);
1234
1235 int val;
1236 switch (shape.m_type) {
1237 case 1: // poly?
1238 case 2: { // unsure a list of 16/20 points, many are similar
1239 if (data.length()<8) {
1240 f << "###";
1241 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeMainData[poly]: the zone seems too short\n"));
1242 break;
1243 }
1244 int nPts;
1245 if (vers<9) {
1246 val=int(input->readULong(4)); // checkme: either N or a number less than N
1247 if (val)
1248 f << "f0=" << val << ",";
1249 nPts=int(input->readULong(4));
1250 }
1251 else
1252 nPts=m_mainParser->readInteger(*stream, 8);
1253 int const fieldSize=vers<9 ? 8 : 16;
1254 f << "N=" << nPts << ",";
1255 if (nPts<0 || 8+fieldSize*nPts<0 || nPts>(data.length()-8)/fieldSize || 8+fieldSize*nPts>data.length()) {
1256 f << "###";
1257 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeMainData[poly]: can not read the number of points\n"));
1258 break;
1259 }
1260 f << "pts=[";
1261 shape.m_vertices.resize(size_t(nPts));
1262 for (auto &pt : shape.m_vertices) {
1263 float coord[2];
1264 for (auto &d : coord) d=float(m_mainParser->readDouble(*stream, vers<9 ? 4 : 8));
1265 pt=MWAWVec2f(coord[1], coord[0]);
1266 f << pt << ",";
1267 }
1268 f << "],";
1269 break;
1270 }
1271 // 6,7: rect, circle: no data
1272 case 8:
1273 if (data.length()!=(vers<9 ? 8 : 16)) {
1274 f << "###";
1275 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeMainData[rectOval]: the zone seems too short\n"));
1276 break;
1277 }
1278 f << "oval[sz]=[";
1279 for (auto &v : shape.m_locals)
1280 v=float(m_mainParser->readDouble(*stream, vers<9 ? 4 : 8));
1281 f << MWAWVec2f(shape.m_locals[1],shape.m_locals[0]) << ",";
1282 break;
1283 case 9: // line
1284 if (data.length()<(vers<9 ? 16 : 32)) {
1285 f << "###";
1286 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeMainData[type9]: the zone seems too short\n"));
1287 break;
1288 }
1289 f << "pts=[";
1290 shape.m_vertices.resize(2);
1291 for (auto &pt : shape.m_vertices) {
1292 float coord[2];
1293 for (auto &d : coord) d=float(m_mainParser->readDouble(*stream, vers<9 ? 4 : 8));
1294 if (vers<9)
1295 pt=MWAWVec2f(coord[1], coord[0]);
1296 else
1297 pt=MWAWVec2f(coord[0], coord[1]);
1298 f << pt << ",";
1299 }
1300 f << "],";
1301 break;
1302 case 10:
1303 if (data.length()!=(vers<9 ? 8 : 16)) {
1304 f << "###";
1305 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeMainData[arc]: the zone seems too short\n"));
1306 break;
1307 }
1308 for (auto &v : shape.m_locals) {
1309 v=float(m_mainParser->readDouble(*stream, vers<9 ? 4 : 8));
1310 if (vers>=9) v*=float(180/M_PI);
1311 }
1312 f << "angles=[" << shape.m_locals[0] << "," << shape.m_locals[1] << "],";
1313 break;
1314 // 11, 12: no dat
1315 case 14: { // special
1316 switch (shape.m_subType) {
1317 case 0x706f626a: // special a pobj which contains a bitmap
1318 if (!Canvas5Structure::readBitmap(*stream, vers, shape.m_bitmap, &shape.m_bitmapColor)) {
1319 f << "###";
1320 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeMainData: can not retrieve the bitmap\n"));
1321 }
1322 if (input->tell()!=data.end()) {
1323 ascFile.addPos(input->tell());
1324 ascFile.addNote("Vkfl-end");
1325 }
1326 break;
1327 case 0x8F909d96: { // special a bitmap in a mac/windows files
1328 bool readInverted=input->readInverted();
1329 input->setReadInverted(!readInverted);
1330 if (!Canvas5Structure::readBitmap(*stream, vers, shape.m_bitmap, &shape.m_bitmapColor)) {
1331 f << "###";
1332 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeMainData: can not retrieve the bitmap\n"));
1333 }
1334 input->setReadInverted(readInverted);
1335 if (input->tell()!=data.end()) {
1336 ascFile.addPos(input->tell());
1337 ascFile.addNote("Vkfl-end");
1338 }
1339 break;
1340 }
1341 case 0x4d41434f: { // MACO
1342 if (data.length()<(vers<9 ? 96 : 116)) {
1343 f << "###";
1344 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeMainData: can not retrieve the MACO vkfl\n"));
1345 break;
1346 }
1347 // see also Canvas5Image::readMACORsrc
1348 f << "unkn=" << std::hex << input->readULong(4) << std::dec << ",";
1349 std::string name;
1350 for (int i=0; i<32; ++i) {
1351 char c=char(input->readULong(1));
1352 if (!c)
1353 break;
1354 name+=c;
1355 }
1356 f << name << ",";
1357 input->seek(data.begin()+4+32, librevenge::RVNG_SEEK_SET);
1358 for (int k=0; k<2; ++k) {
1359 val=int(input->readLong(2));
1360 if (val!=(k==0 ? 256 : 0))
1361 f << "g" << k << "=" << val << ",";
1362 }
1363 if (vers>=9)
1364 input->seek(4, librevenge::RVNG_SEEK_CUR);
1365 float fDim[4];
1366 for (auto &d : fDim) d=float(m_mainParser->readDouble(*stream, vers<9 ? 4 : 8));
1367 if (vers<9)
1368 f << "box=" << MWAWBox2f(MWAWVec2f(fDim[0], fDim[1]), MWAWVec2f(fDim[2], fDim[3])) << ",";
1369 else
1370 f << "box=" << MWAWBox2f(MWAWVec2f(fDim[1], fDim[0]), MWAWVec2f(fDim[3], fDim[2])) << ",";
1371
1372 long actPos=input->tell();
1373 std::vector<unsigned> mId;
1374 std::string extra;
1375 readMacroIndent(*stream, mId, extra);
1376 f << "id=[" << extra << "],";
1377 input->seek(actPos+20, librevenge::RVNG_SEEK_SET);
1378
1379 for (auto &d : fDim) d=float(input->readULong(4))/65536.f;
1380 f << "box2=" << MWAWBox2f(MWAWVec2f(fDim[0], fDim[1]), MWAWVec2f(fDim[2], fDim[3])) << ",";
1381
1382 long imageLen=input->readLong(4);
1383 if (96+imageLen<96 || 96+imageLen>data.length()) {
1384 f << "###";
1385 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeMainData: can not retrieve the vkfl length\n"));
1386 break;
1387 }
1388 if (!imageLen)
1389 break;
1390 if (!readVKFL(stream, imageLen, shape.m_macoImage))
1391 f << "###";
1392 break;
1393 }
1394 case 0x44494d4e: // MACO
1395 case 0x54656368: // Tech
1396 if (vers>=9) {
1397 if (data.length()<=4)
1398 break;
1399 val=int(input->readLong(4));
1400 if (val!=1) f << "f0=" << val << ",";
1401 std::string extra;
1402 shape.m_special=m_mainParser->m_graphParser->readSpecialData(stream, data.length()-4, shape.m_subType, shape.m_box, extra);
1403 if (!shape.m_special)
1404 f << "###";
1405 f << extra;
1406 break;
1407 }
1408 MWAW_FALLTHROUGH;
1409 case 0x54585420: {
1410 std::string extra;
1411 shape.m_special=m_mainParser->m_graphParser->readSpecialData(stream, data.length(), shape.m_subType, shape.m_box, extra);
1412 if (!shape.m_special)
1413 f << "###";
1414 f << extra;
1415 break;
1416 }
1417 default: {
1418 std::string extra;
1419 shape.m_special=m_mainParser->m_graphParser->readSpecialData(stream, data.length(), shape.m_subType, shape.m_box, extra);
1420 if (!shape.m_special)
1421 f << "###";
1422 f << extra;
1423 break;
1424 }
1425 }
1426 break;
1427 }
1428 default:
1429 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeMainData: find unexpected data for type=%d\n", shape.m_type));
1430 f << "##";
1431 break;
1432 }
1433
1434 ascFile.addPos(data.begin());
1435 ascFile.addNote(f.str().c_str());
1436 return true;
1437 }
1438
1439 bool Canvas5Image::readVKFLShapeOtherData
1440 (std::shared_ptr<Canvas5Structure::Stream> stream, Canvas5ImageInternal::VKFLImage &image,
1441 std::tuple<MWAWEntry, unsigned, long> const &dataTypePos,
1442 std::vector<long> &childFieldPos, int subId)
1443 {
1444 if (!stream || !stream->input())
1445 return false;
1446 MWAWEntry data;
1447 unsigned subType;
1448 long idPos;
1449 std::tie(data, subType, idPos)=dataTypePos;
1450 auto input=stream->input();
1451 if (!input->checkPosition(data.end()) || data.begin()<image.m_data[0].begin() || data.end()>image.m_data[0].end()) {
1452 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeOtherData: the entry seems bad\n"));
1453 return false;
1454 }
1455
1456 int const vers=version();
1457 libmwaw::DebugStream f;
1458 auto &ascFile=stream->ascii();
1459 f << "Vkfl-B" << Canvas5Structure::getString(unsigned(data.id())) << "-" << Canvas5Structure::getString(subType);
1460 if (subId>=0)
1461 f << "[" << subId << "]";
1462 f << ":";
1463 input->seek(data.begin(), librevenge::RVNG_SEEK_SET);
1464 switch (data.id()) {
1465 case 1: {
1466 f << "color,";
1467 auto color=getStyleManager()->readColorStyle(stream, subType, data.length());
1468 if (!color) {
1469 f << "###";
1470 break;
1471 }
1472
1473 image.m_posToColorMap[idPos]=color;
1474 break;
1475 }
1476 case 2: {
1477 f << "stroke,";
1478 if (data.length()!=20) {
1479 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeOtherData: can not read a style field\n"));
1480 f << "##";
1481 break;
1482 }
1483 Canvas5ImageInternal::Stroke stroke;
1484 stroke.m_type=unsigned(input->readULong(4));
1485 if (stroke.m_type!=1) f << "type=" << Canvas5Structure::getString(stroke.m_type) << ",";
1486 for (int i=0; i<4; ++i) {
1487 long cPos=input->readLong(4);
1488 if (cPos<0) continue;
1489 char const *wh[]= { "penId", "dashId", "arrow[beg]", "arrow[end]"};
1490 childFieldPos.push_back(cPos);
1491 switch (i) {
1492 case 0:
1493 stroke.m_penPos=cPos;
1494 break;
1495 case 1:
1496 stroke.m_dashPos=cPos;
1497 break;
1498 default:
1499 stroke.m_arrowPos[i-2]=cPos;
1500 break;
1501 }
1502 f << wh[i] << "=Vk" << std::hex << cPos << std::dec << ",";
1503 }
1504 image.m_posToStrokeMap[idPos]=stroke;
1505 break;
1506 }
1507 case 3: {
1508 f << "pen,";
1509 auto pen=getStyleManager()->readPenStyle(*stream, subType, data.length());
1510 if (!pen) {
1511 f << "###";
1512 break;
1513 }
1514
1515 image.m_posToPenMap[idPos]=pen;
1516 break;
1517 }
1518 case 4: {
1519 f << "matrix,";
1520 if (data.length()!=(vers<9 ? 72 : 144)) {
1521 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeOtherData: can not read a matrix field\n"));
1522 f << "##";
1523 break;
1524 }
1525 for (size_t st=0; st<2; ++st) {
1526 f << "mat" << st << "=[";
1527 std::array<double,9> matrix;
1528 for (auto &d : matrix) {
1529 d=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
1530 f << d << ",";
1531 }
1532 f << "],";
1533 if (st==0)
1534 image.m_posToMatrixMap[idPos]=matrix;
1535 }
1536 break;
1537 }
1538 case 5: {
1539 f << "arrow,";
1540 MWAWGraphicStyle::Arrow arrow;
1541 if (!getStyleManager()->readArrow(stream, arrow, subType, data.length())) {
1542 f << "###";
1543 break;
1544 }
1545 image.m_posToArrowMap[idPos]=arrow;
1546 break;
1547 }
1548 case 6: {
1549 f << "dashes,";
1550 std::vector<float> dashes;
1551 if (!getStyleManager()->readDash(*stream, dashes, subType, data.length())) {
1552 f << "###";
1553 break;
1554 }
1555 image.m_posToDashMap[idPos]=dashes;
1556 break;
1557 }
1558 case 8: {
1559 f << "styles,";
1560 switch (subType) {
1561 case 0x54585420: { // TXT
1562 Canvas5StyleManager::CharStyle font;
1563 if (!getStyleManager()->readCharStyle(*stream, 0, font, false)) {
1564 f << "###";
1565 break;
1566 }
1567
1568 bool ok=true;
1569 libmwaw::DebugStream f2;
1570 while (true) {
1571 long pos=input->tell();
1572 f2.str("");
1573 f2 << "Vkfl-B8-TXT [B]:";
1574 if (!input->checkPosition(pos+4)) {
1575 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeOtherData[8,TXT ]: zone seems too short\n"));
1576 f << "###";
1577 ascFile.addPos(pos);
1578 ascFile.addNote(f2.str().c_str());
1579 ok=false;
1580 break;
1581 }
1582
1583 int N=int(input->readLong(2));
1584 int type=int(input->readLong(2));
1585 if (N==0) {
1586 ascFile.addPos(pos);
1587 ascFile.addNote(f2.str().c_str());
1588 break;
1589 }
1590 int expectedLength=type==2 ? 64 : 0;
1591 if (N<0 || expectedLength==0 || (input->size()-pos-4)/expectedLength<N || pos+4+expectedLength*N>input->size()) {
1592 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeOtherData[8,TXT ]: can not read the number of data\n"));
1593 f << "###";
1594 ascFile.addPos(pos);
1595 ascFile.addNote(f2.str().c_str());
1596 ok=false;
1597 break;
1598 }
1599 ascFile.addPos(pos);
1600 ascFile.addNote(f2.str().c_str());
1601
1602 for (int k=0; k<N; ++k) {
1603 pos=input->tell();
1604 if (!getStyleManager()->readStyleEnd(stream)) {
1605 ascFile.addPos(pos);
1606 ascFile.addNote("Vkfl-B8-TXT [B]###:");
1607 }
1608 input->seek(pos+64, librevenge::RVNG_SEEK_SET);
1609 }
1610 }
1611 if (!ok) break;
1612 ascFile.addPos(input->tell());
1613 ascFile.addNote("Vkfl-B8-TXT [C]");
1614 // 000000004e6f726d616c0000000000000000000000000000000000000000000000000000 + (color)*
1615 break;
1616 }
1617 // also 0x70636567: pceg which contains a text link
1618 default:
1619 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeOtherData[8]: unknown subtype=%s\n", Canvas5Structure::getString(subType).c_str()));
1620 f << "###";
1621 break;
1622 }
1623 break;
1624 }
1625 case 10: {
1626 f << "name,";
1627 std::string name;
1628 for (int i=0; i<data.length(); ++i) {
1629 char c=char(input->readLong(1));
1630 if (!c) break;
1631 name+=c;
1632 }
1633 f << name << ",";
1634 break;
1635 }
1636 case 11: {
1637 long len=input->readLong(4);
1638 // v5-v8:36, v9:64
1639 if (len<36 || len>data.length()) {
1640 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeOtherData[11]: can not read the header length\n"));
1641 f << "###";
1642 break;
1643 }
1644 // 0, pobj, MACO
1645 f << "type=" << Canvas5Structure::getString(unsigned(input->readULong(4))) << ",";
1646 if (len!=data.length()) { // only if type=0
1647 // N+1 block of size 56: v<9 or 96: v==9
1648 // XOBJ, 3e8, endian, ...
1649 ascFile.addPos(data.begin()+len);
1650 ascFile.addNote("Vkfl-B11-0[data]:");
1651 }
1652 break;
1653 }
1654 case 12: // similar to some XObd data 2 block
1655 if (data.length()!=40) {
1656 MWAW_DEBUG_MSG(("Canvas5Image::readVKFLShapeOtherData: can not read a type12 field\n"));
1657 f << "##";
1658 break;
1659 }
1660 f << "unkn=["; // find [-62.4277,0,3,127.855,127.855,127.855,0,0,3,127.855]
1661 for (int i=0; i<10; ++i)
1662 f << float(input->readLong(4))/65536 << ",";
1663 f << "],";
1664 break;
1665 default:
1666 f << "##";
1667 break;
1668 }
1669
1670 ascFile.addPos(data.begin());
1671 ascFile.addNote(f.str().c_str());
1672 return true;
1673 }
1674
1675 ////////////////////////////////////////////////////////////
1676 //
1677 // Low level
1678 //
1679 ////////////////////////////////////////////////////////////
1680
1681 ////////////////////////////////////////////////////////////
1682 // send data to the listener
1683 ////////////////////////////////////////////////////////////
1684
1685 bool Canvas5Image::send(std::shared_ptr<Canvas5ImageInternal::VKFLImage> image, MWAWListenerPtr listener,
1686 MWAWBox2f const &box, MWAWTransformation const &transformation) const
1687 {
1688 if (!listener || !image) {
1689 MWAW_DEBUG_MSG(("Canvas5Image::send: can not find the image or the listener\n"));
1690 return false;
1691 }
1692
1693 size_t shapeId=0;
1694 MWAWGraphicStyle style=MWAWGraphicStyle::emptyStyle();
1695 float scale[2]= {1,1};
1696 for (int i=0; i<2; ++i) {
1697 if (image->m_boxes[1].size()[i]>0)
1698 scale[i]=box.size()[i]/image->m_boxes[1].size()[i];
1699 }
1700 MWAWTransformation lTransformation=transformation*
1701 MWAWTransformation(MWAWVec3f(scale[0],0,box[0][0]-scale[0]*image->m_boxes[1][0][0]), MWAWVec3f(0,scale[1],box[0][1]-scale[0]*image->m_boxes[1][0][1]));
1702 while (shapeId<image->m_shapes.size()) {
1703 if (!send(*image, shapeId, listener, style, lTransformation))
1704 return false;
1705 }
1706 return true;
1707 }
1708
1709 bool Canvas5Image::send(Canvas5ImageInternal::VKFLImage const &image, size_t &shapeId, MWAWListenerPtr listener,
1710 MWAWGraphicStyle const &/*style*/, MWAWTransformation const &transformation) const
1711 {
1712 if (shapeId>=image.m_shapes.size()) {
1713 MWAW_DEBUG_MSG(("Canvas5Image::send: can not find the shape %d\n", int(shapeId)));
1714 return false;
1715 }
1716 auto const &shape=image.m_shapes[shapeId++];
1717 MWAWGraphicStyle lStyle;
1718 auto sIt=shape.m_idToDataPos.find(0);
1719 if (sIt!=shape.m_idToDataPos.end()) {
1720 auto cIt=image.m_posToColorMap.find(sIt->second);
1721 if (cIt==image.m_posToColorMap.end() || !cIt->second) {
1722 MWAW_DEBUG_MSG(("Canvas5Image::send: can not find the surface color %x\n", unsigned(sIt->second)));
1723 }
1724 else
1725 getStyleManager()->updateSurfaceColor(*cIt->second, lStyle);
1726 }
1727 sIt=shape.m_idToDataPos.find(1);
1728 if (sIt!=shape.m_idToDataPos.end()) {
1729 auto cIt=image.m_posToColorMap.find(sIt->second);
1730 if (cIt==image.m_posToColorMap.end() || !cIt->second) {
1731 MWAW_DEBUG_MSG(("Canvas5Image::send: can not find the line color %x\n", unsigned(sIt->second)));
1732 }
1733 else
1734 getStyleManager()->updateLineColor(*cIt->second, lStyle);
1735 }
1736 lStyle.m_lineWidth=0;
1737 sIt=shape.m_idToDataPos.find(2);
1738 if (sIt!=shape.m_idToDataPos.end()) {
1739 auto cIt=image.m_posToStrokeMap.find(sIt->second);
1740 if (cIt==image.m_posToStrokeMap.end()) {
1741 MWAW_DEBUG_MSG(("Canvas5Image::send: can not find the surface stroke %x\n", unsigned(sIt->second)));
1742 }
1743 else {
1744 auto const &stroke=cIt->second;
1745 if (stroke.m_penPos>=0) {
1746 auto pIt=image.m_posToPenMap.find(stroke.m_penPos);
1747 if (pIt==image.m_posToPenMap.end() || !pIt->second) {
1748 MWAW_DEBUG_MSG(("Canvas5Image::send: can not find pen %ld\n", stroke.m_penPos));
1749 }
1750 else {
1751 int numLines;
1752 getStyleManager()->updateLine(*pIt->second, lStyle, numLines, 0, nullptr);
1753 }
1754 }
1755 if (stroke.m_dashPos>=0) {
1756 auto dIt=image.m_posToDashMap.find(stroke.m_dashPos);
1757 if (dIt==image.m_posToDashMap.end()) {
1758 MWAW_DEBUG_MSG(("Canvas5Image::send: can not find dash %ld\n", stroke.m_dashPos));
1759 }
1760 else
1761 lStyle.m_lineDashWidth=dIt->second;
1762 }
1763 for (int i=0; i<2; ++i) {
1764 if (stroke.m_arrowPos[i]<0)
1765 continue;
1766 auto dIt=image.m_posToArrowMap.find(stroke.m_arrowPos[i]);
1767 if (dIt==image.m_posToArrowMap.end()) {
1768 MWAW_DEBUG_MSG(("Canvas5Image::send: can not find arrow %ld\n", stroke.m_arrowPos[i]));
1769 }
1770 else
1771 lStyle.m_arrows[i]=dIt->second;
1772 }
1773 }
1774 }
1775 auto lTransformation(transformation);
1776 for (int m=3; m<6; ++m) {
1777 sIt=shape.m_idToDataPos.find(m);
1778 if (sIt==shape.m_idToDataPos.end())
1779 continue;
1780 auto mIt=image.m_posToMatrixMap.find(sIt->second);
1781 if (mIt==image.m_posToMatrixMap.end()) {
1782 MWAW_DEBUG_MSG(("Canvas5Image::send: can not find the surface matrix %x\n", unsigned(sIt->second)));
1783 continue;
1784 }
1785 if (m!=3)
1786 continue;
1787 auto const &mat=mIt->second;
1788 if (mat[2]<-1e-3 || mat[2]>1e-3 || mat[5]<-1e-3 || mat[5]>1e-3) {
1789 MWAW_DEBUG_MSG(("Canvas5Image::send: image matrix will be ignored\n"));
1790 }
1791 else
1792 lTransformation*=MWAWTransformation(MWAWVec3f((float)mat[0],(float)mat[3],(float)mat[6]), MWAWVec3f((float)mat[1],(float)mat[4],(float)mat[7]));
1793 }
1794 MWAWGraphicShape fShape;
1795 switch (shape.m_type) {
1796 case 1:
1797 fShape=MWAWGraphicShape::polygon(shape.m_box);
1798 fShape.m_vertices=shape.m_vertices;
1799 break;
1800 case 2: {
1801 if (shape.m_vertices.size()<2 || (shape.m_vertices.size()%4)) {
1802 MWAW_DEBUG_MSG(("Canvas5Image::send[spline]: find bad N\n"));
1803 return true;
1804 }
1805 fShape=MWAWGraphicShape::path(shape.m_box);
1806 auto &path=fShape.m_path;
1807 path.push_back(MWAWGraphicShape::PathData('M', shape.m_vertices[0]));
1808 for (size_t p=3; p < shape.m_vertices.size(); p+=4) {
1809 if (p>=4 && shape.m_vertices[p-4]!=shape.m_vertices[p-3])
1810 path.push_back(MWAWGraphicShape::PathData('M', shape.m_vertices[p-3]));
1811 bool hasFirstC=shape.m_vertices[p-3]!=shape.m_vertices[p-2];
1812 bool hasSecondC=shape.m_vertices[p-1]!=shape.m_vertices[p];
1813 if (!hasFirstC && !hasSecondC)
1814 path.push_back(MWAWGraphicShape::PathData('L', shape.m_vertices[p]));
1815 else
1816 path.push_back(MWAWGraphicShape::PathData('C', shape.m_vertices[p], shape.m_vertices[p-2], shape.m_vertices[p-1]));
1817 }
1818 if (lStyle.hasSurface())
1819 path.push_back(MWAWGraphicShape::PathData('Z'));
1820 break;
1821 }
1822 case 6:
1823 fShape=MWAWGraphicShape::rectangle(shape.m_box);
1824 break;
1825 case 7:
1826 fShape=MWAWGraphicShape::circle(shape.m_box);
1827 break;
1828 case 8:
1829 fShape=MWAWGraphicShape::rectangle(shape.m_box, MWAWVec2f(shape.m_locals[0], shape.m_locals[1]));
1830 break;
1831 case 9: // checkme: maybe better to use shape.m_vertices[0-1] if it exists...
1832 if (shape.m_vertices.size()==2)
1833 fShape=MWAWGraphicShape::line(shape.m_vertices[0], shape.m_vertices[1]);
1834 else
1835 fShape=MWAWGraphicShape::line(shape.m_box[0], shape.m_box[1]);
1836 break;
1837 case 10: {
1838 float angles[]= {shape.m_locals[0], shape.m_locals[1]};
1839 int angle[2] = { int(90-angles[0]), int(90-angles[0]-angles[1]) };
1840 if (angles[0]<0)
1841 std::swap(angle[0],angle[1]);
1842 else if (angles[0]>=360)
1843 angle[0]-=359;
1844 if (angle[1]>360) {
1845 int numLoop=int(angle[1]/360)-1;
1846 angle[0]-=numLoop*360;
1847 angle[1]-=numLoop*360;
1848 while (angle[1] > 360) {
1849 angle[0]-=360;
1850 angle[1]-=360;
1851 }
1852 }
1853 if (angle[0] < -360) {
1854 int numLoop=int(angle[0]/360)+1;
1855 angle[0]-=numLoop*360;
1856 angle[1]-=numLoop*360;
1857 while (angle[0] < -360) {
1858 angle[0]+=360;
1859 angle[1]+=360;
1860 }
1861 }
1862 // we must compute the real bd box
1863 float minVal[2] = { 0, 0 }, maxVal[2] = { 0, 0 };
1864 int limitAngle[2];
1865 for (int i = 0; i < 2; ++i)
1866 limitAngle[i] = (angle[i] < 0) ? int(angle[i]/90)-1 : int(angle[i]/90);
1867 for (int bord = limitAngle[0]; bord <= limitAngle[1]+1; ++bord) {
1868 float ang = (bord == limitAngle[0]) ? float(angle[0]) :
1869 (bord == limitAngle[1]+1) ? float(angle[1]) : float(90 * bord);
1870 ang *= float(M_PI/180.);
1871 float actVal[2] = { std::cos(ang), -std::sin(ang)};
1872 if (actVal[0] < minVal[0]) minVal[0] = actVal[0];
1873 else if (actVal[0] > maxVal[0]) maxVal[0] = actVal[0];
1874 if (actVal[1] < minVal[1]) minVal[1] = actVal[1];
1875 else if (actVal[1] > maxVal[1]) maxVal[1] = actVal[1];
1876 }
1877 MWAWBox2f circleBox=shape.m_box;
1878 // we have the shape box, we need to reconstruct the circle box
1879 if (maxVal[0]>minVal[0] && maxVal[1]>minVal[1]) {
1880 float scaling[2]= { (shape.m_box[1][0]-shape.m_box[0][0])/(maxVal[0]-minVal[0]),
1881 (shape.m_box[1][1]-shape.m_box[0][1])/(maxVal[1]-minVal[1])
1882 };
1883 for (auto &s : scaling) {
1884 if (s>1e7)
1885 s=100;
1886 else if (s<-1e7)
1887 s=-100;
1888 }
1889 float constant[2]= { shape.m_box[0][0]-minVal[0] *scaling[0], shape.m_box[0][1]-minVal[1] *scaling[1]};
1890 circleBox=MWAWBox2f(MWAWVec2f(constant[0]-scaling[0], constant[1]-scaling[1]),
1891 MWAWVec2f(constant[0]+scaling[0], constant[1]+scaling[1]));
1892 }
1893 fShape = MWAWGraphicShape::pie(shape.m_box, circleBox, MWAWVec2f(float(angle[0]), float(angle[1])));
1894 break;
1895 }
1896 case 11:
1897 case 12: {
1898 if (shapeId+size_t(shape.m_subType)<shapeId ||
1899 shapeId+size_t(shape.m_subType)>image.m_shapes.size()) {
1900 MWAW_DEBUG_MSG(("Canvas5Image::send[group]: find bad N=%d\n", int(shape.m_subType)));
1901 return true;
1902 }
1903 if (shape.m_subType<=1)
1904 return true;
1905 MWAWPosition pos(MWAWVec2f(0,0), MWAWVec2f(100,100), librevenge::RVNG_POINT); // checkme shape box is not valid
1906 pos.m_anchorTo = MWAWPosition::Page;
1907 listener->openGroup(pos);
1908 for (size_t i=0; i<size_t(shape.m_subType); ++i) {
1909 if (!send(image, shapeId, listener, lStyle, lTransformation))
1910 break;
1911 }
1912 listener->closeGroup();
1913 return true;
1914 }
1915 case 14:
1916 switch (shape.m_subType) {
1917 case 0x706f626a:
1918 case 0x8F909d96: {
1919 if (shape.m_bitmap.isEmpty()) {
1920 MWAW_DEBUG_MSG(("Canvas5Image::send[pobj]: can not find the bitmap\n"));
1921 return true;
1922 }
1923
1924 MWAWTransformation transf;
1925 float rotation=0;
1926 MWAWVec2f shearing;
1927 if (!lTransformation.isIdentity() && lTransformation.decompose(rotation,shearing,transf,shape.m_box.center())) {
1928 MWAWBox2f shapeBox=transf*shape.m_box;
1929 MWAWPosition pos(shapeBox[0], shapeBox.size(), librevenge::RVNG_POINT);
1930 pos.m_anchorTo = MWAWPosition::Page;
1931 lStyle.m_rotate=-rotation;
1932 listener->insertPicture(pos, shape.m_bitmap, lStyle);
1933 }
1934 else {
1935 MWAWPosition pos(shape.m_box[0], shape.m_box.size(), librevenge::RVNG_POINT);
1936 pos.m_anchorTo = MWAWPosition::Page;
1937 listener->insertPicture(pos, shape.m_bitmap, lStyle);
1938 }
1939 return true;
1940 }
1941 case 0x4d41434f: { // MACO
1942 if (!shape.m_macoImage) {
1943 MWAW_DEBUG_MSG(("Canvas5Image::send[pobj]: can not find the macro imag\n"));
1944 return true;
1945 }
1946 return send(shape.m_macoImage, listener, shape.m_box, lTransformation);
1947 }
1948 default:
1949 if (!shape.m_special) {
1950 MWAW_DEBUG_MSG(("Canvas5Image::send[special]: can not find the special data\n"));
1951 return true;
1952 }
1953 else {
1954 Canvas5Graph::LocalState lState;
1955 lState.m_position=MWAWPosition(shape.m_box[0], shape.m_box.size(), librevenge::RVNG_POINT);
1956 lState.m_position.m_anchorTo = MWAWPosition::Page;
1957 lState.m_style=lStyle;
1958 lState.m_transform=lTransformation;
1959 return m_mainParser->m_graphParser->sendSpecial(listener, *shape.m_special, lState);
1960 }
1961 break;
1962 }
1963 break;
1964 default:
1965 MWAW_DEBUG_MSG(("Canvas5Image::send: sending type=%d is not implemented\n", shape.m_type));
1966 return true;
1967 }
1968 if (!lTransformation.isIdentity())
1969 fShape=fShape.transform(lTransformation);
1970 MWAWBox2f shapeBox=fShape.getBdBox();
1971 MWAWPosition sPosition(shapeBox[0], shapeBox.size(), librevenge::RVNG_POINT);
1972 listener->insertShape(sPosition, fShape, lStyle);
1973 return true;
1974 }
1975
1976 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: