Debian Codegraph @

src/lib/CricketDrawParser.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 #include <stack>
40 #include <utility>
41
42 #include <librevenge/librevenge.h>
43
44 #include "MWAWGraphicListener.hxx"
45 #include "MWAWGraphicShape.hxx"
46 #include "MWAWGraphicStyle.hxx"
47 #include "MWAWHeader.hxx"
48 #include "MWAWParagraph.hxx"
49 #include "MWAWPictBitmap.hxx"
50 #include "MWAWPictData.hxx"
51 #include "MWAWPrinter.hxx"
52 #include "MWAWPosition.hxx"
53 #include "MWAWSubDocument.hxx"
54
55 #include "CricketDrawParser.hxx"
56
57 /** Internal: the structures of a CricketDrawParser */
58 namespace CricketDrawParserInternal
59 {
60 /** small structure of CricketDrawParserInternal used to stored
61 a shape
62 */
63 struct Shape {
64 //! the different shape type
65 enum Type {
66 Basic, Grate, Group, GroupEnd, Picture, StarBurst, Textbox, Unknown
67 };
68 //! constructor
69 Shape()
70 : m_id(-1)
71 , m_type(Unknown)
72 , m_shape()
73 , m_translation()
74 , m_rotation(0)
75 , m_shear(0)
76 , m_style(MWAWGraphicStyle::emptyStyle())
77 , m_vertices()
78 , m_closed(false)
79 , m_locked(false)
80 , m_smoothed(false)
81 , m_corners(0,0)
82 , m_angles(0,0)
83 , m_grateN(0)
84 , m_grateType(0)
85 , m_text()
86 , m_paragraph()
87 , m_textColor(MWAWColor::black())
88 , m_bitmap()
89 , m_bitmapDimension()
90 , m_bitmapRowSize()
91 , m_bitmapScale(100)
92 , m_isSent(false)
93 {
94 for (auto &fl : m_flip) fl=false;
95 for (auto &id : m_ids) id=0;
96 for (auto &group : m_groupIds) group=0;
97 for (auto &angle : m_starBustAngles) angle=0;
98 }
99 //! returns the current transformation
100 MWAWTransformation getTransformation(MWAWTransformation const &transform) const
101 {
102 auto transformation=transform*MWAWTransformation::translation(m_translation);
103 MWAWVec2f const &center=m_box[0].center();
104 if (m_shear<0 || m_shear>0) {
105 auto angl=float(double(m_shear)*M_PI/180);
106 auto cosA=float(std::cos(angl));
107 auto sinA=float(std::sin(angl));
108 transformation *=
109 MWAWTransformation(MWAWVec3f(1,sinA,-sinA*center[1]),MWAWVec3f(0,cosA,center[1]-cosA*center[1]));
110 }
111 if (m_rotation<0||m_rotation>0)
112 transformation *= MWAWTransformation::rotation(m_rotation,center);
113 static bool first=true;
114 if (first && (m_flip[0]||m_flip[1])) {
115 first=false;
116 MWAW_DEBUG_MSG(("CricketDrawParserInternal::Shape::getTransformation: oops flipping is not implemented\n"));
117 }
118 return transformation;
119 }
120 //! the shape id
121 int m_id;
122 //! the shape type
123 Type m_type;
124 //! the ids
125 long m_ids[2];
126 //! the shape
127 MWAWGraphicShape m_shape;
128 //! the main box (before translation and after translation)
129 MWAWBox2f m_box[2];
130 //! the translation
131 MWAWVec2f m_translation;
132 //! the rotation angle
133 float m_rotation;
134 //! the shear angle
135 float m_shear;
136 //! two bool to indicated we need to flip the shape or not
137 bool m_flip[2];
138 //! the style
139 MWAWGraphicStyle m_style;
140 //! the list of point
141 std::vector<MWAWVec2f> m_vertices;
142 //! flag to know if the shape is closed
143 bool m_closed;
144 //! flag to know if the shape is locked
145 bool m_locked;
146 //! flag to know if the shape is smoothed
147 bool m_smoothed;
148 //! the rectOval corner size
149 MWAWVec2f m_corners;
150 //! the arc limits
151 MWAWVec2i m_angles;
152 //! the grate number
153 int m_grateN;
154 //! the grate type
155 int m_grateType;
156 //! the starbust angle: min, max, delta
157 int m_starBustAngles[3];
158 //! some unknown group id
159 long m_groupIds[2];
160 //! the text entry
161 MWAWEntry m_text;
162 //! the paragraph style
163 MWAWParagraph m_paragraph;
164 //! the text color
165 MWAWColor m_textColor;
166 //! the bitmap entry
167 MWAWEntry m_bitmap;
168 //! the bitmap dimension
169 MWAWBox2i m_bitmapDimension;
170 //! the bitmap row size
171 int m_bitmapRowSize;
172 //! the bitmap scaling
173 int m_bitmapScale;
174 //! flag to know if a shape is already sent
175 mutable bool m_isSent;
176 };
177
178 ////////////////////////////////////////
179 //! Internal: the state of a CricketDrawParser
180 struct State {
181 //! constructor
182 State()
183 : m_dashList()
184 , m_shapeList()
185 {
186 }
187 //! returns the quickdraw color corresponding to some id
188 static bool getColor(int id, int intensity, MWAWColor &col);
189 //! returns a dash corresponding to some id
190 bool getDash(int id, std::vector<float> &dash)
191 {
192 if (m_dashList.empty())
193 initDashs();
194 if (id<1 || size_t(id)>m_dashList.size()) {
195 MWAW_DEBUG_MSG(("CricketDrawParserInternal::State::getDash: unknown dahs %d\n", id));
196 return false;
197 }
198 dash=m_dashList[size_t(id-1)];
199 return true;
200 }
201
202 protected:
203 //! init the dashs list
204 void initDashs();
205 //! the list of dash
206 std::vector< std::vector<float> > m_dashList;
207 public:
208 //! the list of shape
209 std::vector<Shape> m_shapeList;
210 };
211
212 bool State::getColor(int id, int intensity, MWAWColor &col)
213 {
214 switch (id) {
215 case 30:
216 col = MWAWColor::white();
217 break; // white
218 case 33:
219 col = MWAWColor::black();
220 break; // black
221 case 69:
222 col = MWAWColor(255,255,0);
223 break; // yellow
224 case 137:
225 col = MWAWColor(255,0,255);
226 break; // magenta
227 case 205:
228 col = MWAWColor(255,0,0);
229 break; // red
230 case 273:
231 col = MWAWColor(0,255,255);
232 break; // cyan
233 case 341:
234 col = MWAWColor(0,255,0);
235 break; // green
236 case 409:
237 col = MWAWColor(0,0,255);
238 break; // blue
239 default:
240 MWAW_DEBUG_MSG(("CricketDrawParserInternal::State::getColor: unknown color %d\n", id));
241 return false;
242 }
243 col=MWAWColor::barycenter(float(intensity)/100.f, col, float(100-intensity)/100.f, MWAWColor::white());
244 return true;
245 }
246
247 void State::initDashs()
248 {
249 if (!m_dashList.empty()) return;
250 std::vector<float> dash;
251 // 1 solid
252 dash.push_back(270);
253 m_dashList.push_back(dash);
254 // 2: 36x9 9x9 9x9
255 dash[0]=36;
256 for (int i=0; i<5; ++i) dash.push_back(9);
257 m_dashList.push_back(dash);
258 // 3:36x9 9x9
259 dash.resize(4);
260 m_dashList.push_back(dash);
261 // 4: 36x18
262 dash.resize(2);
263 dash[1]=18;
264 m_dashList.push_back(dash);
265 // 5:27x9
266 dash[0]=27;
267 dash[1]=9;
268 m_dashList.push_back(dash);
269 // 6:18x18
270 dash[0]=18;
271 dash[1]=18;
272 m_dashList.push_back(dash);
273 // 7:9x27
274 dash[0]=9;
275 dash[1]=27;
276 m_dashList.push_back(dash);
277 // 8:5x32
278 dash[0]=5;
279 dash[1]=32;
280 m_dashList.push_back(dash);
281 // 9:3x18
282 dash[0]=3;
283 dash[1]=18;
284 m_dashList.push_back(dash);
285 // 10:3x3
286 dash[0]=3;
287 dash[1]=3;
288 m_dashList.push_back(dash);
289 }
290
291 ////////////////////////////////////////
292 //! Internal: the subdocument of a CricketDrawParser
293 class SubDocument final : public MWAWSubDocument
294 {
295 public:
296 SubDocument(CricketDrawParser &pars, MWAWInputStreamPtr const &input, int zoneId)
297 : MWAWSubDocument(&pars, input, MWAWEntry())
298 , m_id(zoneId) {}
299
300 //! destructor
301 ~SubDocument() final {}
302
303 //! operator!=
304 bool operator!=(MWAWSubDocument const &doc) const final
305 {
306 if (MWAWSubDocument::operator!=(doc)) return true;
307 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
308 if (!sDoc) return true;
309 if (m_id != sDoc->m_id) return true;
310 return false;
311 }
312
313 //! the parser function
314 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
315
316 protected:
317 //! the subdocument id
318 int m_id;
319 private:
320 SubDocument(SubDocument const &orig) = delete;
321 SubDocument &operator=(SubDocument const &orig) = delete;
322 };
323
324 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
325 {
326 if (!listener || !listener->canWriteText()) {
327 MWAW_DEBUG_MSG(("CricketDrawParserInternal::SubDocument::parse: no listener\n"));
328 return;
329 }
330 auto *parser=dynamic_cast<CricketDrawParser *>(m_parser);
331 if (!parser) {
332 MWAW_DEBUG_MSG(("CricketDrawParserInternal::SubDocument::parse: no parser\n"));
333 return;
334 }
335 long pos = m_input->tell();
336 parser->sendText(m_id);
337 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
338 }
339
340 }
341
342 ////////////////////////////////////////////////////////////
343 // constructor/destructor, ...
344 ////////////////////////////////////////////////////////////
345 CricketDrawParser::CricketDrawParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
346 : MWAWGraphicParser(input, rsrcParser, header)
347 , m_state()
348 {
349 resetGraphicListener();
350 setAsciiName("main-1");
351
352 m_state.reset(new CricketDrawParserInternal::State);
353
354 getPageSpan().setMargins(0.1);
355 }
356
357 CricketDrawParser::~CricketDrawParser()
358 {
359 }
360
361 ////////////////////////////////////////////////////////////
362 // the parser
363 ////////////////////////////////////////////////////////////
364 void CricketDrawParser::parse(librevenge::RVNGDrawingInterface *docInterface)
365 {
366 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
367 bool ok = false;
368 try {
369 // create the asciiFile
370 ascii().setStream(getInput());
371 ascii().open(asciiName());
372 checkHeader(nullptr);
373 ok = createZones();
374 if (ok) {
375 createDocument(docInterface);
376 sendAll();
377 }
378 ascii().reset();
379 }
380 catch (...) {
381 MWAW_DEBUG_MSG(("CricketDrawParser::parse: exception catched when parsing\n"));
382 ok = false;
383 }
384
385 resetGraphicListener();
386 if (!ok) throw(libmwaw::ParseException());
387 }
388
389 ////////////////////////////////////////////////////////////
390 // create the document
391 ////////////////////////////////////////////////////////////
392 void CricketDrawParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
393 {
394 if (!documentInterface) return;
395 if (getGraphicListener()) {
396 MWAW_DEBUG_MSG(("CricketDrawParser::createDocument: listener already exist\n"));
397 return;
398 }
399
400 // create the page list
401 MWAWPageSpan ps(getPageSpan());
402 ps.setPageSpan(1);
403 std::vector<MWAWPageSpan> pageList(1,ps);
404 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
405 setGraphicListener(listen);
406 listen->startDocument();
407 }
408
409 ////////////////////////////////////////////////////////////
410 //
411 // Intermediate level
412 //
413 ////////////////////////////////////////////////////////////
414 bool CricketDrawParser::createZones()
415 {
416 MWAWInputStreamPtr input = getInput();
417 long pos=input->tell();
418 if (!readPrintInfo())
419 input->seek(pos,librevenge::RVNG_SEEK_SET);
420 pos=input->tell();
421 const int vers=version();
422 libmwaw::DebugStream f;
423 f << "Entries(Unknown):";
424 ascii().addPos(pos);
425 ascii().addNote(f.str().c_str());
426 // vers<=2 first block in 0xa0?
427 input->seek(0x200, librevenge::RVNG_SEEK_SET);
428 int const decal=vers<=2 ? 39 : 63;
429 while (!input->isEnd()) {
430 if (readShape())
431 continue;
432 pos=input->tell();
433 if (input->tell()==pos)
434 input->seek(pos+decal+1,librevenge::RVNG_SEEK_SET);
435 while (!input->isEnd()) {
436 long actPos=input->tell();
437 auto val=static_cast<int>(input->readULong(4));
438 if ((val&0xFFFFFF)==0x640021) {
439 input->seek(actPos+1-decal, librevenge::RVNG_SEEK_SET);
440 break;
441 }
442 if ((val>>8)==0x640021) {
443 input->seek(actPos-decal, librevenge::RVNG_SEEK_SET);
444 break;
445 }
446 if ((val&0xFFFF)==0x6400)
447 input->seek(-2, librevenge::RVNG_SEEK_CUR);
448 else if ((val&0xFF)==0x64)
449 input->seek(-3, librevenge::RVNG_SEEK_CUR);
450 }
451 if (input->tell()!=pos) {
452 ascii().addPos(pos);
453 ascii().addNote("Entries(Unknown):");
454 }
455 }
456 return true;
457 }
458
459 bool CricketDrawParser::readShape()
460 {
461 MWAWInputStreamPtr input = getInput();
462 long pos=input->tell();
463 const int vers=version();
464 int expectedSize=vers>2 ? 192 : 180;
465 if (!input->checkPosition(pos+expectedSize))
466 return false;
467 libmwaw::DebugStream f;
468 f << "Entries(Shape):";
469 CricketDrawParserInternal::Shape shape;
470 f << "IDS=[";
471 for (auto &id : shape.m_ids) {
472 id=long(input->readULong(4));
473 if (id)
474 f << std::hex << id << std::dec << ",";
475 else
476 f << "_,";
477 }
478 f << "],";
479 int val,type=0;
480 if (vers>2) {
481 for (int st=0; st<2; ++st) {
482 float dim[4];
483 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
484 shape.m_box[st]=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
485 f << "box" << st << "=" << shape.m_box[st] << ",";
486 }
487 for (int i=0; i<6; ++i) { // always 0
488 val=static_cast<int>(input->readLong(2));
489 if (val) f << "f" << i << "=" << val << ",";
490 }
491 }
492 else {
493 val=static_cast<int>(input->readULong(2));
494 if (val&0x100) f << "selected,";
495 val &=0xFEFF;
496 if (val) f << "f0=" << std::hex << val << std::dec << ",";
497 for (int i=0; i<2; ++i) { // f2=0|1
498 val=static_cast<int>(input->readLong(2));
499 if (i==0) {
500 if (val&0x100) f << "locked,";
501 shape.m_locked=true;
502 val&=0xFEFF;
503 }
504 if (val) f << "f" << i+2 << "=" << val << ",";
505 }
506 }
507 type=static_cast<int>(input->readLong(2));
508 if (type<0 || type>0x10) {
509 input->seek(pos, librevenge::RVNG_SEEK_SET);
510 return false;
511 }
512 switch (type) {
513 case 0:
514 shape.m_type=CricketDrawParserInternal::Shape::Group;
515 f << "group,";
516 break;
517 case 1:
518 shape.m_type=CricketDrawParserInternal::Shape::Textbox;
519 f << "textbox,";
520 break;
521 case 2:
522 shape.m_type=CricketDrawParserInternal::Shape::Basic;
523 f << "line,";
524 break;
525 case 3:
526 shape.m_type=CricketDrawParserInternal::Shape::Basic;
527 f << "rect,";
528 break;
529 case 4:
530 shape.m_type=CricketDrawParserInternal::Shape::Basic;
531 f << "rectOval,";
532 break;
533 case 5:
534 shape.m_type=CricketDrawParserInternal::Shape::Basic;
535 f << "circle,";
536 break;
537 case 6:
538 shape.m_type=CricketDrawParserInternal::Shape::Basic;
539 f << "arc,";
540 break;
541 case 7:
542 shape.m_type=CricketDrawParserInternal::Shape::Basic;
543 shape.m_closed=true;
544 f << "diamond,";
545 break;
546 case 8:
547 shape.m_type=CricketDrawParserInternal::Shape::Basic;
548 f << "poly,";
549 break;
550 case 9:
551 shape.m_type=CricketDrawParserInternal::Shape::Basic;
552 f << "spline,";
553 break;
554 case 10: // list of line
555 shape.m_type=CricketDrawParserInternal::Shape::Grate;
556 f << "grate,";
557 break;
558 case 11:
559 shape.m_type=CricketDrawParserInternal::Shape::StarBurst;
560 f << "starburst,";
561 break;
562 case 12:
563 shape.m_type=CricketDrawParserInternal::Shape::Basic;
564 f << "bezier,";
565 break;
566 case 13:
567 shape.m_type=CricketDrawParserInternal::Shape::Picture;
568 f << "picture,";
569 break;
570 case 14:
571 shape.m_type=CricketDrawParserInternal::Shape::GroupEnd;
572 f << "endgroup,";
573 break;
574 default:
575 f << "type=" << type << ",";
576 break;
577 }
578 if (vers<=2) {
579 val=static_cast<int>(input->readLong(2)); // always 0
580 if (val&1) {
581 shape.m_smoothed=true;
582 f << "smooth,";
583 }
584 val &=0xFFFE;
585 if (val) f << "f4=" << val << ",";
586 float dim[4];
587 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
588 shape.m_box[0]=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
589 f << "dim=" << shape.m_box[0] << ",";
590 }
591 else {
592 val=static_cast<int>(input->readLong(2));
593 if (val) f << "f6=" << val << ",";
594 val=static_cast<int>(input->readULong(2));
595 if (val&2) f << "selected,";
596 if (val&8) f << "locked,";
597 val &= 0xFFF5;
598 if (val) f << "f7=" << std::hex << val << std::dec << ",";
599 }
600 MWAWGraphicStyle &style=shape.m_style;
601 f << "line=[";
602 style.m_lineWidth=float(input->readLong(4))/65536.f;
603 f << "width=" << style.m_lineWidth << ",";
604 val=static_cast<int>(input->readULong(1));
605 if (val==0) { // checkme
606 style.m_lineWidth=0;
607 f << "none,";
608 }
609 else if (val!=1) {
610 m_state->getDash(val, style.m_lineDashWidth);
611 f << "dash=" << val << ",";
612 }
613 auto intensity=static_cast<int>(input->readULong(1));
614 if (intensity!=100)
615 f << "intensity=" << intensity << ",";
616 val=static_cast<int>(input->readULong(2));
617 if (!m_state->getColor(val, intensity, style.m_lineColor))
618 f << "##color=" << val << ",";
619 else if (!style.m_lineColor.isBlack())
620 f << "color=" << style.m_lineColor << ",";
621 f << "],";
622 for (int i=0; i<2; ++i) { // 0
623 val=static_cast<int>(input->readLong(2));
624 if (val) f << "g" << i+4 << "=" << val << ",";
625 }
626 val=static_cast<int>(input->readULong(2)); // 0|f02a
627 if (val) f << "g2=" << std::hex << val << std::dec << ",";
628 f << "surf=[";
629 intensity=static_cast<int>(input->readULong(1));
630 if (intensity) f << "intensity=" << intensity << ",";
631 val=static_cast<int>(input->readULong(1));
632 if (val!=14) f << "f0=" << val << ",";
633 val=static_cast<int>(input->readULong(2));
634 MWAWColor col;
635 if (!m_state->getColor(val, intensity,col))
636 f << "##color=" << val << ",";
637 else {
638 style.setSurfaceColor(col);
639 shape.m_textColor=col;
640 if (!col.isWhite())
641 f << "color=" << col << ",";
642 }
643 f << "],";
644 val=static_cast<int>(input->readULong(2));
645 if (val!=0x1bb9)
646 f << "g3=" << std::hex << val << std::dec << ",";
647 for (int i=0; i<2; ++i) { // 0
648 val=static_cast<int>(input->readLong(2));
649 if (val) f << "g" << i+3 << "=" << val << ",";
650 }
651
652 val=static_cast<int>(input->readULong(1));
653 if (val==1) {
654 f << "shadow=[";
655 int totalIntensity=0;
656 for (int i=0; i<3; ++i) {
657 val=static_cast<int>(input->readULong(1));
658 totalIntensity+=val;
659 int const expected[] = {0x64,0x19,0x4b};
660 if (val==expected[i]) continue;
661 char const *wh[]= {"begin","end","borders"};
662 f << wh[i] << "[intensities]=" << val << ",";
663 }
664 float decal[2];
665 for (auto &d : decal) d=float(input->readLong(4))/65536.f;
666 style.m_shadowOffset=MWAWVec2f(decal[1],decal[0]);
667 f << "decal=" << style.m_shadowOffset << ",";
668 val=static_cast<int>(input->readLong(2));
669 if (!m_state->getColor(val, totalIntensity/3, col))
670 f << "###color=" << val << ",";
671 else {
672 f << "color=" << col << ",";
673 style.setShadowColor(col);
674 }
675 f << "],";
676 }
677 else {
678 if (val) {
679 MWAW_DEBUG_MSG(("CricketDrawParser::readShape: unknown shadow type\n"));
680 f << "##shadow[type]=" << val << ",";
681 }
682 input->seek(13, librevenge::RVNG_SEEK_CUR);
683 }
684 for (int i=0; i<3; ++i) { // fl
685 val=static_cast<int>(input->readULong(2));
686 if (val) f << "fl" << i << "=" << std::hex << val << std::dec << ",";
687 }
688 ascii().addPos(pos);
689 ascii().addNote(f.str().c_str());
690 f.str("");
691 f << "Shape-A[" << type << "]:";
692 pos=input->tell();
693 expectedSize-=(vers<=2 ? 78 : 102);
694 auto fountainType=static_cast<int>(input->readLong(1));
695 switch (fountainType) {
696 case 0:
697 break;
698 case 1:
699 f << "fountain[type]=linear,";
700 break;
701 case 2:
702 f << "fountain[type]=log,";
703 break;
704 case 3:
705 f << "fountain[type]=radial,";
706 break;
707 default:
708 MWAW_DEBUG_MSG(("CricketDrawParser::readShape: unknown fountain type\n"));
709 f << "##fountain[type]=" << fountainType << ",";
710 fountainType=0;
711 break;
712 }
713 if (fountainType) {
714 f << "fountain=[";
715 int intensityVal[2];
716 for (int i=0; i<2; ++i) intensityVal[1-i]=static_cast<int>(input->readULong(1));
717 if (intensityVal[0]) f << "beg[intensity]=" << intensityVal[0] << ",";
718 if (intensityVal[1]!=100) f << "end[intensity]=" << intensityVal[1] << ",";
719 val=static_cast<int>(input->readLong(1)); // always 0?
720 if (val) f << "f0=" << val << ",";
721 auto angle=static_cast<int>(input->readLong(2));
722 if (angle) f << "angle=" << angle << ",";
723 float dim[2];
724 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
725 MWAWVec2f center(dim[1],dim[0]);
726 if (center!=MWAWVec2f(0,0)) f << "center=" << center << ",";
727 val=static_cast<int>(input->readLong(2));
728 if (!m_state->getColor(val, 100, col))
729 f << "###color=" << val << ",";
730 else {
731 if (!col.isBlack())
732 f << "color=" << col << ",";
733 // set the gradient
734 auto &finalGrad=style.m_gradient;
735 if (fountainType==3) {
736 finalGrad.m_type=MWAWGraphicStyle::Gradient::G_Radial;
737 finalGrad.m_percentCenter=MWAWVec2f(0.5f,0.5f); // fixme: use center and bdbox here
738 }
739 else {
740 finalGrad.m_type= MWAWGraphicStyle::Gradient::G_Linear;
741 finalGrad.m_angle=float(angle+90);
742 }
743 finalGrad.m_stopList.resize(2);
744 for (size_t i=0; i<2; ++i)
745 finalGrad.m_stopList[i]
746 =MWAWGraphicStyle::Gradient::Stop(float(i),MWAWColor::barycenter(float(intensityVal[i])/100.f, col, float(100-intensityVal[i])/100.f, MWAWColor::white()));
747 }
748 f << "],";
749 }
750 else
751 input->seek(15, librevenge::RVNG_SEEK_CUR);
752 for (int i=0; i<3; ++i) { // fl
753 val=static_cast<int>(input->readULong(2));
754 if (val) f << "fl" << i+3 << "=" << std::hex << val << std::dec << ",";
755 }
756 for (int i=0; i<2; ++i) { // 0
757 val=static_cast<int>(input->readLong(2));
758 if (i==0 && (val&0x300)) {
759 if (val&0x100) {
760 style.m_arrows[1]=MWAWGraphicStyle::Arrow::plain();
761 f << "arrow[beg],";
762 }
763 if (val&0x200) {
764 style.m_arrows[0]=MWAWGraphicStyle::Arrow::plain();
765 f << "arrow[end],";
766 }
767 val &= 0xFCFF;
768 }
769 if (val) f << "f" << i+7 << "=" << val << ",";
770 }
771 float dim[2];
772 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
773 shape.m_translation=MWAWVec2f(dim[1],dim[0]);
774 f << "orig=" << shape.m_translation << ",";
775 val=static_cast<int>(input->readLong(4));
776 if (val) {
777 shape.m_rotation=float(val)/65536.f;
778 f << "rotate=" << shape.m_rotation << ",";
779 }
780 val=static_cast<int>(input->readLong(4));
781 if (val) {
782 shape.m_shear=float(val)/65536.f;
783 f << "shear[angle]=" << shape.m_shear << ",";
784 }
785 val=static_cast<int>(input->readULong(1));
786 if (val&1) {
787 shape.m_flip[1]=true;
788 f << "flipY,";
789 }
790 if (val&2) {
791 shape.m_flip[0]=true;
792 f << "flipX,";
793 }
794 val &=0xFC;
795 if (val) f << "g0=" << val << ",";
796 val=static_cast<int>(input->readULong(1)); // small number
797 if (val) f << "g1=" << val << ",";
798 int const numData=vers<=2 ? 9 : 5;
799 for (int i=0; i<numData; ++i) {
800 val=static_cast<int>(input->readLong(2));
801 int const expected[]= {0,0x2d,0x48,0,0,0,0,0,0};
802 if (val!=expected[i])
803 f << "g" << i+2 << "=" << val << ",";
804 }
805 auto dataSize=long(input->readULong(4));
806 if (dataSize<0 || !input->checkPosition(pos+expectedSize+dataSize)) {
807 input->seek(pos, librevenge::RVNG_SEEK_SET);
808 return false;
809 }
810 if (dataSize)
811 f << "dataSize=" << dataSize << ",";
812 long bitmapSize=0;
813 switch (type) {
814 case 4: { // rectOval
815 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
816 shape.m_corners=MWAWVec2f(dim[1],dim[0]);
817 f << "corners=" << shape.m_corners << ",";
818 break;
819 }
820 case 6: { // arc
821 val=static_cast<int>(input->readULong(2));
822 if (val) // f | 2f0c
823 f << "h0=" << std::hex << val << std::dec << ",";
824 int angles[2];
825 for (auto &angle : angles) angle=static_cast<int>(input->readLong(2));
826 shape.m_angles=MWAWVec2i(angles[0], angles[1]);
827 f << "angles=" << shape.m_angles << ",";
828 // then 4 small int
829 break;
830 }
831 case 10: // grate
832 shape.m_grateN=static_cast<int>(input->readLong(2));
833 f << "grate[number]=" << shape.m_grateN << ",";
834 shape.m_grateType=static_cast<int>(input->readLong(2));
835 switch (shape.m_grateType) {
836 case 0: // uniform
837 break;
838 case 1:
839 f << "grad[type]=log,";
840 break;
841 case 2:
842 f << "grad[type]=radial,";
843 break;
844 default:
845 f << "#grad[type]=" << shape.m_grateType << ",";
846 }
847 break;
848 case 11: //starbust
849 for (auto &angle : shape.m_starBustAngles) angle=static_cast<int>(input->readLong(2));
850 if (shape.m_starBustAngles[0]) f << "starbust[beg]=" << shape.m_starBustAngles[0] << ",";
851 if (shape.m_starBustAngles[1]!=160) f << "starbust[end]=" << shape.m_starBustAngles[1] << ",";
852 if (shape.m_starBustAngles[2]!=10) f << "starbust[delta]=" << shape.m_starBustAngles[2] << ",";
853 break;
854 case 13: {
855 for (int i=0; i<2; ++i)
856 f << "flA" << i << "=" << std::hex << input->readULong(2) << std::dec << ",";
857 shape.m_bitmapRowSize=static_cast<int>(input->readULong(2));
858 f << "bitmap[rowSize]=" << shape.m_bitmapRowSize << ","; // 4|8|e
859 int dimInt[4];
860 for (auto &d : dimInt) d=static_cast<int>(input->readULong(2));
861 shape.m_bitmapDimension=MWAWBox2i(MWAWVec2i(dimInt[1],dimInt[0]),MWAWVec2i(dimInt[3],dimInt[2]));
862 f << "dim=" << shape.m_bitmapDimension << ",";
863 bitmapSize=long(input->readULong(4));
864 if (bitmapSize<0 || !input->checkPosition(pos+expectedSize+dataSize+bitmapSize)) {
865 input->seek(pos, librevenge::RVNG_SEEK_SET);
866 MWAW_DEBUG_MSG(("CricketDrawParser::readShape: can not read bitmap size\n"));
867 return false;
868 }
869 shape.m_bitmapScale=static_cast<int>(input->readLong(2));
870 if (shape.m_bitmapScale!=100) f << "scaling=" << shape.m_bitmapScale << "%";
871 break;
872 }
873 case 14: // endgroup
874 val=static_cast<int>(input->readLong(2)); // always 0
875 if (val) f << "h0=" << val << ",";
876 val=static_cast<int>(input->readLong(2));
877 if (val) f << "N=" << val << ",";
878 ascii().addDelimiter(input->tell(),'|');
879 input->seek(16, librevenge::RVNG_SEEK_CUR);
880 ascii().addDelimiter(input->tell(),'|');
881 f << "IDS=[";
882 for (auto &id : shape.m_groupIds) {
883 id=long(input->readULong(4));
884 f << std::hex << id << std::dec << ",";
885 }
886 f << "],";
887 break;
888 default:
889 break;
890 }
891 ascii().addDelimiter(input->tell(),'|');
892 ascii().addPos(pos);
893 ascii().addNote(f.str().c_str());
894 input->seek(pos+expectedSize, librevenge::RVNG_SEEK_SET);
895 if (dataSize) {
896 pos=input->tell();
897 f.str("");
898 f << "Shape-data[" << type << "]:";
899 bool ok=true;
900 switch (type) {
901 case 2:
902 if (dataSize!=16) {
903 ok=false;
904 break;
905 }
906 f << "pts=[";
907 for (int pt=0; pt<2; ++pt) {
908 float coord[2];
909 for (auto &c : coord) c=float(input->readLong(4))/65536.f;
910 shape.m_vertices.push_back(MWAWVec2f(coord[1],coord[0]));
911 f << shape.m_vertices.back() << ",";
912 }
913 f << "],";
914 shape.m_shape=MWAWGraphicShape::line(shape.m_vertices[0], shape.m_vertices[1]);
915 break;
916 case 1:
917 case 3:
918 case 4:
919 case 5:
920 case 6:
921 case 7:
922 case 10:
923 case 11:
924 case 12:
925 case 13:
926 if (dataSize!=32) {
927 ok=false;
928 break;
929 }
930 f << "pts=[";
931 for (int pt=0; pt<4; ++pt) {
932 float coord[2];
933 for (auto &c : coord) c=float(input->readLong(4))/65536.f;
934 shape.m_vertices.push_back(MWAWVec2f(coord[1],coord[0]));
935 f << shape.m_vertices.back() << ",";
936 }
937 f << "],";
938 switch (type) {
939 case 3:
940 case 4:
941 shape.m_shape=MWAWGraphicShape::rectangle(MWAWBox2f(shape.m_vertices[0], shape.m_vertices[2]), shape.m_corners);
942 break;
943 case 5:
944 shape.m_shape=MWAWGraphicShape::circle(MWAWBox2f(shape.m_vertices[0], shape.m_vertices[2]));
945 break;
946 case 6: {
947 MWAWBox2f box(shape.m_vertices[0], shape.m_vertices[2]);
948 int angle[2] = { shape.m_angles[0], shape.m_angles[1] };
949 if (box.min()[1]>box.max()[1]) {
950 std::swap(box.min()[1],box.max()[1]);
951 angle[0]=180-shape.m_angles[1];
952 angle[1]=180-shape.m_angles[0];
953 }
954 if (angle[1]<=angle[0])
955 std::swap(angle[0],angle[1]);
956 if (angle[1]>360) {
957 int numLoop=int(angle[1]/360)-1;
958 angle[0]-=numLoop*360;
959 angle[1]-=numLoop*360;
960 while (angle[1] > 360) {
961 angle[0]-=360;
962 angle[1]-=360;
963 }
964 }
965 if (angle[0] < -360) {
966 int numLoop=int(angle[0]/360)+1;
967 angle[0]-=numLoop*360;
968 angle[1]-=numLoop*360;
969 while (angle[0] < -360) {
970 angle[0]+=360;
971 angle[1]+=360;
972 }
973 }
974 // we must compute the real bd box
975 float minVal[2] = { 0, 0 }, maxVal[2] = { 0, 0 };
976 int limitAngle[2];
977 for (int i = 0; i < 2; ++i)
978 limitAngle[i] = (angle[i] < 0) ? int(angle[i]/90)-1 : int(angle[i]/90);
979 for (int bord = limitAngle[0]; bord <= limitAngle[1]+1; ++bord) {
980 float ang = (bord == limitAngle[0]) ? float(angle[0]) :
981 (bord == limitAngle[1]+1) ? float(angle[1]) : float(90 * bord);
982 ang *= float(M_PI/180.);
983 float const actVal[2] = { std::cos(ang), -std::sin(ang)};
984 if (actVal[0] < minVal[0]) minVal[0] = actVal[0];
985 else if (actVal[0] > maxVal[0]) maxVal[0] = actVal[0];
986 if (actVal[1] < minVal[1]) minVal[1] = actVal[1];
987 else if (actVal[1] > maxVal[1]) maxVal[1] = actVal[1];
988 }
989 MWAWBox2f circleBox(box);
990 // we have the shape box, we need to reconstruct the circle box
991 if (maxVal[0]>minVal[0] && maxVal[1]>minVal[1]) {
992 float const scaling[2]= { (box[1][0]-box[0][0])/(maxVal[0]-minVal[0]),
993 (box[1][1]-box[0][1])/(maxVal[1]-minVal[1])
994 };
995 float const constant[2]= { box[0][0]-minVal[0] *scaling[0], box[0][1]-minVal[1] *scaling[1]};
996 circleBox=MWAWBox2f(MWAWVec2f(constant[0]-scaling[0], constant[1]-scaling[1]),
997 MWAWVec2f(constant[0]+scaling[0], constant[1]+scaling[1]));
998 }
999 shape.m_shape = MWAWGraphicShape::arc(box, circleBox, MWAWVec2f(float(angle[0]),float(angle[1])));
1000 break;
1001 }
1002 case 7: // diamond
1003 shape.m_shape.m_type=MWAWGraphicShape::Polygon;
1004 shape.m_shape.m_bdBox=MWAWBox2f(shape.m_vertices[0], shape.m_vertices[2]);
1005 shape.m_shape.m_vertices=shape.m_vertices;
1006 break;
1007 case 12: // small bezier curve: 4 points
1008 shape.m_shape.m_type=MWAWGraphicShape::Path;
1009 shape.m_shape.m_bdBox=MWAWBox2f(shape.m_box[0][0], shape.m_box[0][1]);
1010 shape.m_shape.m_path.push_back(MWAWGraphicShape::PathData('M', shape.m_vertices[0]));
1011 shape.m_shape.m_path.push_back(MWAWGraphicShape::PathData('C', shape.m_vertices[3],shape.m_vertices[1],shape.m_vertices[2]));
1012 break;
1013 default:
1014 break;
1015 }
1016 break;
1017 case 8:
1018 case 9:
1019 if (dataSize%8) {
1020 ok=false;
1021 break;
1022 }
1023 f << "N=" << (dataSize/8) << ",";
1024 f << "pts=[";
1025 for (long pt=0; pt<(dataSize/8); ++pt) {
1026 float coord[2];
1027 for (auto &c : coord) c=float(input->readLong(4))/65536.f;
1028 shape.m_vertices.push_back(MWAWVec2f(coord[1],coord[0]));
1029 f << shape.m_vertices.back() << ",";
1030 }
1031 f << "],";
1032 if (!dataSize) break;
1033 shape.m_shape.m_bdBox=MWAWBox2f(shape.m_box[0][0], shape.m_box[0][1]);
1034 if (type==8 && !shape.m_smoothed) {
1035 shape.m_shape.m_type=MWAWGraphicShape::Polygon;
1036 shape.m_shape.m_vertices=shape.m_vertices;
1037 }
1038 else if (type==8) {
1039 shape.m_shape.m_type=MWAWGraphicShape::Path;
1040 shape.m_shape.m_path.push_back(MWAWGraphicShape::PathData('M', shape.m_vertices[0]));
1041 for (size_t i=0; i+1<shape.m_vertices.size(); ++i)
1042 shape.m_shape.m_path.push_back(MWAWGraphicShape::PathData('Q', 0.5f*(shape.m_vertices[i]+shape.m_vertices[i+1]),
1043 shape.m_vertices[i]));
1044 shape.m_shape.m_path.push_back(MWAWGraphicShape::PathData('T', shape.m_vertices.back()));
1045 }
1046 else if (type==9) {
1047 if ((shape.m_vertices.size()%2)==0) {
1048 MWAW_DEBUG_MSG(("CricketDrawParser::readShape: find uneven number of point, ignore last one\n"));
1049 f << "###odd";
1050 }
1051 shape.m_shape.m_type=MWAWGraphicShape::Path;
1052 shape.m_shape.m_path.push_back(MWAWGraphicShape::PathData('M', shape.m_vertices[0]));
1053 for (size_t i=1; i+1<shape.m_vertices.size(); i+=2)
1054 shape.m_shape.m_path.push_back(MWAWGraphicShape::PathData('Q', shape.m_vertices[i+1], shape.m_vertices[i]));
1055 }
1056 break;
1057 default:
1058 ok=false;
1059 break;
1060 }
1061 if (!ok) {
1062 MWAW_DEBUG_MSG(("CricketDrawParser::readShape: find unexpected data size for type %d\n", type));
1063 f << "###";
1064 }
1065 ascii().addPos(pos);
1066 ascii().addNote(f.str().c_str());
1067 input->seek(pos+dataSize, librevenge::RVNG_SEEK_SET);
1068 }
1069 if (type==1) {
1070 pos=input->tell();
1071 auto sSz=static_cast<int>(input->readULong(2));
1072 if (!input->checkPosition(pos+2+sSz+(sSz%2)+38)) {
1073 input->seek(pos, librevenge::RVNG_SEEK_SET);
1074 return false;
1075 }
1076 f.str("");
1077 f << "Shape-text:";
1078 shape.m_text.setBegin(pos+2);
1079 shape.m_text.setLength(sSz);
1080 if (sSz%2) ++sSz;
1081 input->seek(pos+2+sSz, librevenge::RVNG_SEEK_SET);
1082 ascii().addPos(pos);
1083 ascii().addNote(f.str().c_str());
1084 pos=input->tell();
1085 f.str("");
1086 f << "Shape-textPLC:";
1087 for (int i=0; i<6; ++i) { // f5 probably related to line height
1088 val=static_cast<int>(input->readLong(2));
1089 int const expected[]= {6,0,0,0x15,4,0x2e};
1090 if (val!=expected[i]) f << "f" << i << "=" << val << ",";
1091 }
1092 val=static_cast<int>(input->readLong(1));
1093 switch (val) {
1094 case 0: // left
1095 break;
1096 case 1:
1097 f << "right,";
1098 shape.m_paragraph.m_justify=MWAWParagraph::JustificationRight;
1099 break;
1100 case 2:
1101 f << "center,";
1102 shape.m_paragraph.m_justify=MWAWParagraph::JustificationCenter;
1103 break;
1104 case 3:
1105 f << "justify=all,";
1106 shape.m_paragraph.m_justify=MWAWParagraph::JustificationFull;
1107 break;
1108 default:
1109 MWAW_DEBUG_MSG(("CricketDrawParser::readShape: find unexpected align\n"));
1110 f << "###align=" << val << ",";
1111 break;
1112 }
1113 val=static_cast<int>(input->readLong(2));
1114 if (val) f << "f6=" << val << ",";
1115 val=static_cast<int>(input->readLong(1));
1116 switch (val) {
1117 case 0: // normal
1118 break;
1119 case 1:
1120 f << "interline=150%,";
1121 shape.m_paragraph.setInterline(1.5, librevenge::RVNG_PERCENT);
1122 break;
1123 case 2:
1124 f << "interline=200%,";
1125 shape.m_paragraph.setInterline(2., librevenge::RVNG_PERCENT);
1126 break;
1127 default:
1128 MWAW_DEBUG_MSG(("CricketDrawParser::readShape: find unexpected align\n"));
1129 f << "###interline=" << val << ",";
1130 break;
1131 }
1132
1133 ascii().addDelimiter(input->tell(),'|');
1134 ascii().addPos(pos);
1135 ascii().addNote(f.str().c_str());
1136 input->seek(pos+38, librevenge::RVNG_SEEK_SET);
1137 }
1138 if (bitmapSize) {
1139 pos=input->tell();
1140 shape.m_bitmap.setBegin(pos);
1141 shape.m_bitmap.setLength(bitmapSize);
1142 f.str("");
1143 f << "Entries(Bitmap):";
1144 ascii().addPos(pos);
1145 ascii().addNote(f.str().c_str());
1146 input->seek(pos+bitmapSize, librevenge::RVNG_SEEK_SET);
1147 }
1148 shape.m_id=static_cast<int>(m_state->m_shapeList.size());
1149 m_state->m_shapeList.push_back(shape);
1150 return true;
1151 }
1152
1153 ////////////////////////////////////////////////////////////
1154 // read the header
1155 ////////////////////////////////////////////////////////////
1156 bool CricketDrawParser::checkHeader(MWAWHeader *header, bool strict)
1157 {
1158 *m_state = CricketDrawParserInternal::State();
1159 MWAWInputStreamPtr input = getInput();
1160 if (!input || !input->hasDataFork() || !input->checkPosition(0x200))
1161 return false;
1162
1163 libmwaw::DebugStream f;
1164 f << "FileHeader:";
1165 input->seek(0, librevenge::RVNG_SEEK_SET);
1166 auto vers=static_cast<int>(input->readULong(2));
1167 if (vers<=0 || vers>4)
1168 return false;
1169 f << "vers=" << vers << ",";
1170 auto sSz=static_cast<int>(input->readULong(1));
1171 if (sSz<6||sSz>8) return false;
1172 std::string date;
1173 int numSlash=0;
1174 for (int i=0; i<sSz; ++i) {
1175 auto c=char(input->readULong(1));
1176 date+=c;
1177 if (c=='/')
1178 ++numSlash;
1179 else if (c<'0' || c>'9')
1180 return false;
1181 }
1182 if (numSlash!=2) return false;
1183 f << "vers[date]=" << date << ",";
1184 input->seek(12, librevenge::RVNG_SEEK_SET);
1185 auto val=static_cast<int>(input->readULong(2)); // always 0?
1186 if (val)
1187 f << "f0=" << val << ",";
1188 int dim[4];
1189 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
1190 f << "dim=" << MWAWBox2i(MWAWVec2i(dim[0],dim[1]), MWAWVec2i(dim[2],dim[3])) << ",";
1191 if (strict && !readPrintInfo()) { // check if empty
1192 input->seek(22,librevenge::RVNG_SEEK_SET);
1193 for (int i=0; i<4; ++i) {
1194 if (input->readLong(4)) return false;
1195 }
1196 return false;
1197 }
1198 ascii().addPos(0);
1199 ascii().addNote(f.str().c_str());
1200 setVersion(vers);
1201 if (header)
1202 header->reset(MWAWDocument::MWAW_T_CRICKETDRAW, vers, MWAWDocument::MWAW_K_DRAW);
1203 input->seek(22,librevenge::RVNG_SEEK_SET);
1204
1205 return true;
1206 }
1207
1208 ////////////////////////////////////////////////////////////
1209 // try to read the print info zone
1210 ////////////////////////////////////////////////////////////
1211 bool CricketDrawParser::readPrintInfo()
1212 {
1213 MWAWInputStreamPtr input = getInput();
1214 long pos=input->tell();
1215 long endPos=pos+120;
1216 if (!input->checkPosition(endPos)) {
1217 MWAW_DEBUG_MSG(("CricketDrawParser::readPrintInfo: file seems too short\n"));
1218 return false;
1219 }
1220 libmwaw::DebugStream f;
1221 f << "Entries(PrintInfo):";
1222 libmwaw::PrinterInfo info;
1223 if (!info.read(input)) {
1224 MWAW_DEBUG_MSG(("CricketDrawParser::readPrintInfo: can not read print info\n"));
1225 return false;
1226 }
1227 f << info;
1228 MWAWVec2i paperSize = info.paper().size();
1229 MWAWVec2i pageSize = info.page().size();
1230 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
1231 paperSize.x() <= 0 || paperSize.y() <= 0) {
1232 ascii().addPos(pos);
1233 ascii().addNote(f.str().c_str());
1234 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1235 return true;
1236 }
1237
1238 // define margin from print info
1239 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
1240 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
1241
1242 // move margin left | top
1243 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
1244 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
1245 lTopMargin -= MWAWVec2i(decalX, decalY);
1246 rBotMargin += MWAWVec2i(decalX, decalY);
1247
1248 // decrease right | bottom
1249 int rightMarg = rBotMargin.x() -50;
1250 if (rightMarg < 0) rightMarg=0;
1251 int botMarg = rBotMargin.y() -50;
1252 if (botMarg < 0) botMarg=0;
1253
1254 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
1255 getPageSpan().setMarginBottom(botMarg/72.0);
1256 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
1257 getPageSpan().setMarginRight(rightMarg/72.0);
1258 getPageSpan().setFormLength(paperSize.y()/72.);
1259 getPageSpan().setFormWidth(paperSize.x()/72.);
1260
1261 ascii().addPos(pos);
1262 ascii().addNote(f.str().c_str());
1263 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1264 return true;
1265 }
1266
1267 ////////////////////////////////////////////////////////////
1268 //
1269 // send data
1270 //
1271 ////////////////////////////////////////////////////////////
1272 bool CricketDrawParser::sendAll()
1273 {
1274 MWAWGraphicListenerPtr listener=getGraphicListener();
1275 if (!listener) {
1276 MWAW_DEBUG_MSG(("CricketDrawParser::sendAll: can not find the listener\n"));
1277 return false;
1278 }
1279 std::stack<MWAWTransformation> transformationStack;
1280 MWAWVec2f LT(float(getPageSpan().getMarginLeft())*72.f, float(getPageSpan().getMarginTop())*72.f);
1281 transformationStack.push(MWAWTransformation::translation(LT));
1282 for (size_t i=0; i<m_state->m_shapeList.size(); ++i) {
1283 auto const &shape=m_state->m_shapeList[i];
1284 if (shape.m_type==CricketDrawParserInternal::Shape::GroupEnd) {
1285 MWAWTransformation transformation=shape.getTransformation(transformationStack.top());
1286 transformationStack.push(transformation);
1287 MWAWBox2f box=transformation*shape.m_box[0];
1288 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
1289 pos.m_anchorTo = MWAWPosition::Page;
1290 listener->openGroup(pos);
1291 continue;
1292 }
1293 if (shape.m_type==CricketDrawParserInternal::Shape::Group) {
1294 if (transformationStack.size()>1) {
1295 transformationStack.pop();
1296 listener->closeGroup();
1297 }
1298 else if (i+1!=m_state->m_shapeList.size()) {
1299 MWAW_DEBUG_MSG(("CricketDrawParser::sendAll: can not find the group end shape\n"));
1300 }
1301 continue;
1302 }
1303 send(shape, transformationStack.top());
1304 }
1305 if (transformationStack.size()>1) {
1306 MWAW_DEBUG_MSG(("CricketDrawParser::sendAll: find some unclosed group\n"));
1307 for (size_t i=1; i<transformationStack.size(); ++i)
1308 listener->closeGroup();
1309 }
1310 return true;
1311 }
1312
1313 bool CricketDrawParser::send(CricketDrawParserInternal::Shape const &shape, MWAWTransformation const &transform)
1314 {
1315 MWAWGraphicListenerPtr listener=getGraphicListener();
1316 if (!listener) {
1317 MWAW_DEBUG_MSG(("CricketDrawParser::send: can not find the listener\n"));
1318 return false;
1319 }
1320 if (shape.m_isSent) {
1321 MWAW_DEBUG_MSG(("CricketDrawParser::send: the shape is already sent\n"));
1322 return false;
1323 }
1324 shape.m_isSent=true;
1325 MWAWTransformation transformation=shape.getTransformation(transform);
1326 switch (shape.m_type) {
1327 case CricketDrawParserInternal::Shape::Basic: {
1328 MWAWGraphicShape finalShape=shape.m_shape.transform(transformation);
1329 MWAWBox2f box=finalShape.getBdBox();
1330 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
1331 pos.m_anchorTo = MWAWPosition::Page;
1332 pos.setOrder(static_cast<int>(m_state->m_shapeList.size())-shape.m_id);
1333 listener->insertShape(pos, finalShape, shape.m_style);
1334 break;
1335 }
1336 case CricketDrawParserInternal::Shape::Textbox: {
1337 MWAWGraphicStyle style(MWAWGraphicStyle::emptyStyle());
1338 MWAWTransformation transf;
1339 float rotation=0;
1340 MWAWVec2f shearing;
1341 MWAWBox2f box=transformation*shape.m_box[0];
1342 if (transformation.decompose(rotation,shearing,transf,shape.m_box[0].center())) {
1343 box=transf*shape.m_box[0];
1344 style.m_rotate=-rotation;
1345 }
1346 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
1347 pos.m_anchorTo = MWAWPosition::Page;
1348 pos.setOrder(static_cast<int>(m_state->m_shapeList.size())-shape.m_id);
1349 for (int i=0; i<2; ++i) style.m_flip[i]=shape.m_flip[i];
1350 std::shared_ptr<MWAWSubDocument> doc(new CricketDrawParserInternal::SubDocument(*this, getInput(), shape.m_id));
1351
1352 listener->insertTextBox(pos, doc, style);
1353 break;
1354 }
1355 case CricketDrawParserInternal::Shape::Grate: {
1356 if (shape.m_grateN<=0) {
1357 MWAW_DEBUG_MSG(("CricketDrawParser::send: the number of line seems bad\n"));
1358 break;
1359 }
1360 if (shape.m_grateType<0||shape.m_grateType>2) {
1361 MWAW_DEBUG_MSG(("CricketDrawParser::send: sorry unexpected grate type\n"));
1362 break;
1363 }
1364 MWAWBox2f box=MWAWBox2f(shape.m_box[0][0], shape.m_box[0][1]);
1365 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
1366 pos.m_anchorTo = MWAWPosition::Page;
1367 listener->openGroup(pos);
1368 MWAWVec2f center=0.5f*(box[0]+box[1]);
1369 MWAWVec2f dir1=(shape.m_grateType==2) ? (box[1]-box[0]) : MWAWVec2f(box[1][0]-box[0][0],0);
1370 MWAWVec2f dir2(0, box[1][1]-box[0][1]);
1371 float decal;
1372 for (int i=0; i<shape.m_grateN; ++i) {
1373 if (i==0)
1374 decal=0;
1375 else if (i+1==shape.m_grateN)
1376 decal=1;
1377 else if (shape.m_grateType==1)
1378 decal=float(std::log(i+1)/std::log(shape.m_grateN));
1379 else
1380 decal=float(i)/float(shape.m_grateN-1);
1381 MWAWGraphicShape line;
1382 if (shape.m_grateType==2) // fixme: normally the lines are (portion of) circles and not ellipsis
1383 line=MWAWGraphicShape::circle(MWAWBox2f(center-0.5f*(1.f-decal)*dir1, center+0.5f*(1.f-decal)*dir1));
1384 else
1385 line=MWAWGraphicShape::line(box[0]+(1.f-decal)*dir2, box[0]+(1.f-decal)*dir2+dir1);
1386 line = line.transform(transformation);
1387 MWAWBox2f lineBox=line.getBdBox();
1388 pos=MWAWPosition(lineBox[0], lineBox.size(), librevenge::RVNG_POINT);
1389 pos.m_anchorTo = MWAWPosition::Page;
1390 pos.setOrder(static_cast<int>(m_state->m_shapeList.size())-shape.m_id);
1391 listener->insertShape(pos, line, shape.m_style);
1392 }
1393 listener->closeGroup();
1394 break;
1395 }
1396 case CricketDrawParserInternal::Shape::StarBurst: {
1397 if (shape.m_starBustAngles[0]>shape.m_starBustAngles[1] || shape.m_starBustAngles[2]<=0) {
1398 MWAW_DEBUG_MSG(("CricketDrawParser::send: the star burst angles seems bad\n"));
1399 break;
1400 }
1401 MWAWBox2f box=MWAWBox2f(shape.m_box[0][0], shape.m_box[0][1]);
1402 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
1403 pos.m_anchorTo = MWAWPosition::Page;
1404 listener->openGroup(pos);
1405 MWAWVec2f center=0.5f*(box[0]+box[1]);
1406 MWAWVec2f dir(0.5f*(box[1][0]-box[0][0]), 0.5f*(box[1][1]-box[0][1]));
1407 for (int angle=shape.m_starBustAngles[0]; angle<=shape.m_starBustAngles[1]; angle+=shape.m_starBustAngles[2]) {
1408 float angl=float(M_PI)/180.f*float(angle);
1409 MWAWGraphicShape line=MWAWGraphicShape::line(center,center+MWAWVec2f(std::cos(angl)*dir[0],-std::sin(angl)*dir[1]));
1410 line = line.transform(transformation);
1411 MWAWBox2f lineBox=line.getBdBox();
1412 pos=MWAWPosition(lineBox[0], lineBox.size(), librevenge::RVNG_POINT);
1413 pos.m_anchorTo = MWAWPosition::Page;
1414 pos.setOrder(static_cast<int>(m_state->m_shapeList.size())-shape.m_id);
1415 listener->insertShape(pos, line, shape.m_style);
1416 }
1417 listener->closeGroup();
1418 break;
1419 }
1420 case CricketDrawParserInternal::Shape::Picture:
1421 return sendBitmap(shape, transform);
1422 case CricketDrawParserInternal::Shape::Group:
1423 case CricketDrawParserInternal::Shape::GroupEnd:
1424 case CricketDrawParserInternal::Shape::Unknown:
1425 #if !defined(__clang__)
1426 default:
1427 #endif
1428 break;
1429 }
1430 return true;
1431 }
1432
1433 bool CricketDrawParser::sendText(int zId)
1434 {
1435 MWAWGraphicListenerPtr listener=getGraphicListener();
1436 if (!listener) {
1437 MWAW_DEBUG_MSG(("CricketDrawParser::sendText: can not find the listener\n"));
1438 return false;
1439 }
1440 if (zId<0||zId>=static_cast<int>(m_state->m_shapeList.size()) ||
1441 m_state->m_shapeList[size_t(zId)].m_type != CricketDrawParserInternal::Shape::Textbox) {
1442 MWAW_DEBUG_MSG(("CricketDrawParser::sendText: can not find the text shape\n"));
1443 return false;
1444 }
1445 auto const &shape=m_state->m_shapeList[size_t(zId)];
1446 if (!shape.m_text.valid())
1447 return true;
1448
1449 listener->setParagraph(shape.m_paragraph);
1450 MWAWFont font(3,12);
1451 font.setColor(shape.m_textColor);
1452 listener->setFont(font);
1453 MWAWInputStreamPtr input=getInput();
1454 input->seek(shape.m_text.begin(), librevenge::RVNG_SEEK_SET);
1455 libmwaw::DebugStream f;
1456 f << "Shape[text]:";
1457 long endPos=shape.m_text.end();
1458 while (!input->isEnd()) {
1459 if (input->tell()>=shape.m_text.end())
1460 break;
1461 auto c = char(input->readULong(1));
1462 if (c==0) {
1463 MWAW_DEBUG_MSG(("CricketDrawParser::sendText: find char 0\n"));
1464 f << "#[0]";
1465 continue;
1466 }
1467 f << c;
1468 switch (c) {
1469 case 9:
1470 listener->insertTab();
1471 break;
1472 case 0xd:
1473 listener->insertEOL();
1474 break;
1475 default:
1476 listener->insertCharacter(static_cast<unsigned char>(c), input, endPos);
1477 break;
1478 }
1479 }
1480 ascii().addPos(shape.m_text.begin());
1481 ascii().addNote(f.str().c_str());
1482 return true;
1483 }
1484
1485 bool CricketDrawParser::sendBitmap(CricketDrawParserInternal::Shape const &bitmap, MWAWTransformation const &transform)
1486 {
1487 MWAWGraphicListenerPtr listener=getGraphicListener();
1488 if (!listener) {
1489 MWAW_DEBUG_MSG(("CricketDrawParser::sendBitmap: can not find the listener\n"));
1490 return false;
1491 }
1492 MWAWVec2i dim=bitmap.m_bitmapDimension.size();
1493 if (!bitmap.m_bitmap.valid() || bitmap.m_bitmapRowSize<=0 ||
1494 8*bitmap.m_bitmapRowSize<dim[0] ||
1495 dim[0] <= 0 || dim[1] <= 0 ||
1496 dim[1]>bitmap.m_bitmap.length()/bitmap.m_bitmapRowSize) {
1497 MWAW_DEBUG_MSG(("CricketDrawParser::sendBitmap: oops, the bitmap dimension seems bad\n"));
1498 return false;
1499 }
1500 MWAWInputStreamPtr input = getInput();
1501 input->seek(bitmap.m_bitmap.begin(), librevenge::RVNG_SEEK_SET);
1502
1503 libmwaw::DebugStream f;
1504 // a bitmap is composed of 720 rows of (72x8bytes)
1505 std::shared_ptr<MWAWPictBitmapIndexed> pict;
1506 pict.reset(new MWAWPictBitmapIndexed(dim));
1507 std::vector<MWAWColor> colors(2);
1508 colors[0]=MWAWColor::white();
1509 colors[1]=bitmap.m_textColor;
1510 pict->setColors(colors);
1511
1512 for (int r=0; r<dim[1]; ++r) {
1513 long rowPos=input->tell();
1514 f.str("");
1515 f << "Entries(Bitmap)-" << r << ":";
1516 int col=0;
1517 while (col<dim[0]) {
1518 auto color=static_cast<int>(input->readULong(1));
1519 for (int b=7; b>=0; --b) {
1520 if (col>=dim[0])
1521 break;
1522 pict->set(col++, r, (color>>b)&1);
1523 }
1524 }
1525 input->seek(rowPos+bitmap.m_bitmapRowSize, librevenge::RVNG_SEEK_SET);
1526 ascii().addPos(rowPos);
1527 ascii().addNote(f.str().c_str());
1528 }
1529
1530 MWAWEmbeddedObject picture;
1531 if (!pict->getBinary(picture)) return false;
1532
1533 MWAWTransformation transformation=bitmap.getTransformation(transform);
1534 MWAWBox2f box=transformation*bitmap.m_box[0];
1535 MWAWTransformation transf;
1536 float rotation=0;
1537 MWAWVec2f shearing;
1538 MWAWGraphicStyle style(MWAWGraphicStyle::emptyStyle());
1539 if (transformation.decompose(rotation,shearing,transf,bitmap.m_box[0].center())) {
1540 box=transf*bitmap.m_box[0];
1541 style.m_rotate=-rotation;
1542 }
1543
1544 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
1545 pos.m_anchorTo = MWAWPosition::Page;
1546 pos.setOrder(static_cast<int>(m_state->m_shapeList.size())-bitmap.m_id);
1547 listener->insertPicture(pos, picture, style);
1548
1549 return true;
1550 }
1551
1552 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: