Debian Codegraph @

src/lib/SuperPaintParser.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <cmath>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWGraphicListener.hxx"
43 #include "MWAWGraphicShape.hxx"
44 #include "MWAWGraphicStyle.hxx"
45 #include "MWAWHeader.hxx"
46 #include "MWAWParagraph.hxx"
47 #include "MWAWPictBitmap.hxx"
48 #include "MWAWPictData.hxx"
49 #include "MWAWPrinter.hxx"
50 #include "MWAWPosition.hxx"
51 #include "MWAWSubDocument.hxx"
52
53 #include "SuperPaintParser.hxx"
54
55 /** Internal: the structures of a SuperPaintParser */
56 namespace SuperPaintParserInternal
57 {
58 ////////////////////////////////////////
59 //! Internal: the shape of a SuperPaintParser
60 struct Shape {
61 //! the type
62 enum Type { GraphicShape, Group, Picture, TextBox };
63 //! constructor
64 Shape(Type type, MWAWBox2f const &box)
65 : m_type(type)
66 , m_box(box)
67 , m_entry()
68 , m_shape()
69 , m_style()
70 , m_font()
71 , m_justify(MWAWParagraph::JustificationLeft)
72 , m_interline(1)
73 {
74 }
75 //! operator<<
76 friend std::ostream &operator<<(std::ostream &o, Shape const &shape);
77 //! the shape type
78 Type m_type;
79 //! the bdbox
80 MWAWBox2f m_box;
81 //! the picture/textbox entry
82 MWAWEntry m_entry;
83 //! the graphic shape
84 MWAWGraphicShape m_shape;
85 //! the graphic style
86 MWAWGraphicStyle m_style;
87 //! the textbox font
88 MWAWFont m_font;
89 //! the textbox justification
90 MWAWParagraph::Justification m_justify;
91 //! the interline in percent
92 double m_interline;
93 };
94
95 std::ostream &operator<<(std::ostream &o, Shape const &shape)
96 {
97 switch (shape.m_type) {
98 case Shape::GraphicShape:
99 o << "shape," << shape.m_shape << ",";
100 break;
101 case Shape::Group:
102 o << "group,box=" << shape.m_box << ",";
103 break;
104 case Shape::Picture:
105 o << "picture,box=" << shape.m_box << ",";
106 break;
107 case Shape::TextBox:
108 o << "textbox,box=" << shape.m_box << ",";
109 break;
110 #if !defined(__clang__)
111 default:
112 MWAW_DEBUG_MSG(("SuperPaintParserInternal::Shape::operator<<: find unknown type"));
113 break;
114 #endif
115 }
116 o << shape.m_style;
117 return o;
118 }
119
120 ////////////////////////////////////////
121 //! Internal: the state of a SuperPaintParser
122 struct State {
123 //! constructor
124 State()
125 : m_kind(MWAWDocument::MWAW_K_DRAW)
126 , m_bitmap()
127 , m_shapeList()
128 {
129 }
130 //! try to return the color corresponding to an id
131 static bool getColor(int id, MWAWColor &color);
132 //! the file type
133 MWAWDocument::Kind m_kind;
134 /// the bitmap (v1)
135 std::shared_ptr<MWAWPict> m_bitmap;
136 //! the list of shapes
137 std::vector<Shape> m_shapeList;
138 };
139
140 bool State::getColor(int id, MWAWColor &color)
141 {
142 switch (id) {
143 case 0:
144 color=MWAWColor(0,0,0);
145 break;
146 case 1:
147 color=MWAWColor(255,255,255);
148 break;
149 case 2:
150 color=MWAWColor(255,0,0);
151 break;
152 case 3:
153 color=MWAWColor(0,255,0);
154 break;
155 case 4:
156 color=MWAWColor(0,0,255);
157 break;
158 case 5: // orange
159 color=MWAWColor(255, 165, 0);
160 break;
161 case 6: // purple
162 color=MWAWColor(128,0,128);
163 break;
164 case 7: // yellow
165 color=MWAWColor(255,255,0);
166 break;
167 default:
168 MWAW_DEBUG_MSG(("SuperPaintParserInternal::State::getColor: can not determine color for id=%d\n", id));
169 return false;
170 }
171 return true;
172 }
173
174 ////////////////////////////////////////
175 //! Internal: the subdocument of a SuperPaintParser
176 class SubDocument final : public MWAWSubDocument
177 {
178 public:
179 SubDocument(SuperPaintParser &pars, MWAWInputStreamPtr const &input, int zoneId)
180 : MWAWSubDocument(&pars, input, MWAWEntry())
181 , m_id(zoneId)
182 {
183 }
184
185 //! destructor
186 ~SubDocument() final {}
187
188 //! operator!=
189 bool operator!=(MWAWSubDocument const &doc) const final
190 {
191 if (MWAWSubDocument::operator!=(doc)) return true;
192 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
193 if (!sDoc) return true;
194 if (m_id != sDoc->m_id) return true;
195 return false;
196 }
197
198 //! the parser function
199 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
200
201 protected:
202 //! the subdocument id
203 int m_id;
204 private:
205 SubDocument(SubDocument const &orig) = delete;
206 SubDocument &operator=(SubDocument const &orig) = delete;
207 };
208
209 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
210 {
211 if (!listener || !listener->canWriteText()) {
212 MWAW_DEBUG_MSG(("SuperPaintParserInternal::SubDocument::parse: no listener\n"));
213 return;
214 }
215 if (!m_parser) {
216 MWAW_DEBUG_MSG(("SuperPaintParserInternal::SubDocument::parse: no parser\n"));
217 return;
218 }
219 long pos = m_input->tell();
220 static_cast<SuperPaintParser *>(m_parser)->sendText(m_id);
221 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
222 }
223
224
225 }
226
227 ////////////////////////////////////////////////////////////
228 // constructor/destructor, ...
229 ////////////////////////////////////////////////////////////
230 SuperPaintParser::SuperPaintParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
231 : MWAWGraphicParser(input, rsrcParser, header)
232 , m_state()
233 {
234 init();
235 }
236
237 SuperPaintParser::~SuperPaintParser()
238 {
239 }
240
241 void SuperPaintParser::init()
242 {
243 resetGraphicListener();
244 setAsciiName("main-1");
245
246 m_state.reset(new SuperPaintParserInternal::State);
247
248 getPageSpan().setMargins(0.1);
249 }
250
251 ////////////////////////////////////////////////////////////
252 // the parser
253 ////////////////////////////////////////////////////////////
254 void SuperPaintParser::parse(librevenge::RVNGDrawingInterface *docInterface)
255 {
256 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
257 bool ok = false;
258 try {
259 // create the asciiFile
260 ascii().setStream(getInput());
261 ascii().open(asciiName());
262 checkHeader(nullptr);
263 ok = createZones();
264 if (ok) {
265 createDocument(docInterface);
266 if (m_state->m_kind==MWAWDocument::MWAW_K_PAINT)
267 sendBitmap();
268 else
269 sendPictures();
270 }
271 ascii().reset();
272 }
273 catch (...) {
274 MWAW_DEBUG_MSG(("SuperPaintParser::parse: exception catched when parsing\n"));
275 ok = false;
276 }
277
278 resetGraphicListener();
279 if (!ok) throw(libmwaw::ParseException());
280 }
281
282 ////////////////////////////////////////////////////////////
283 // create the document
284 ////////////////////////////////////////////////////////////
285 void SuperPaintParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
286 {
287 if (!documentInterface) return;
288 if (getGraphicListener()) {
289 MWAW_DEBUG_MSG(("SuperPaintParser::createDocument: listener already exist\n"));
290 return;
291 }
292
293 // create the page list
294 MWAWPageSpan ps(getPageSpan());
295 ps.setPageSpan(1);
296 std::vector<MWAWPageSpan> pageList(1,ps);
297 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
298 setGraphicListener(listen);
299 listen->startDocument();
300 }
301
302
303 ////////////////////////////////////////////////////////////
304 //
305 // Intermediate level
306 //
307 ////////////////////////////////////////////////////////////
308 bool SuperPaintParser::createZones()
309 {
310 MWAWInputStreamPtr input = getInput();
311 readHeader();
312 input->seek(512,librevenge::RVNG_SEEK_SET);
313 bool ok=true;
314 long pos=input->tell();
315 if (m_state->m_kind==MWAWDocument::MWAW_K_DRAW) {
316 ok = readPictures();
317 }
318 else if (readBitmap(true)) {
319 input->seek(pos, librevenge::RVNG_SEEK_SET);
320 ok = readBitmap();
321 }
322
323 pos=input->tell();
324 if (input->size()==pos+2 && input->readLong(2)==0) {
325 ascii().addPos(pos);
326 ascii().addNote("_");
327 }
328 else if (input->size()!=pos) {
329 MWAW_DEBUG_MSG(("SuperPaintParser::createZones: find some extra data\n"));
330 ascii().addPos(input->tell());
331 ascii().addNote("Entries(End):###");
332 }
333 return ok;
334 }
335
336 ////////////////////////////////////////////////////////////
337 // send shapes: vector graphic document
338 ////////////////////////////////////////////////////////////
339 bool SuperPaintParser::sendPictures()
340 {
341 MWAWGraphicListenerPtr listener=getGraphicListener();
342 if (!listener) {
343 MWAW_DEBUG_MSG(("SuperPaintParser::sendPictures: can not find the listener\n"));
344 return false;
345 }
346 MWAWInputStreamPtr input = getInput();
347
348 for (size_t i=m_state->m_shapeList.size(); i>0;) {
349 auto const &shape=m_state->m_shapeList[--i];
350 MWAWPosition pos(shape.m_box[0], shape.m_box.size(), librevenge::RVNG_POINT);
351 pos.setPage(1);
352 pos.m_anchorTo=MWAWPosition::Page;
353 switch (shape.m_type) {
354 case SuperPaintParserInternal::Shape::Group:
355 break;
356 case SuperPaintParserInternal::Shape::TextBox: {
357 std::shared_ptr<MWAWSubDocument> doc(new SuperPaintParserInternal::SubDocument(*this, input, static_cast<int>(i)));
358 listener->insertTextBox(pos, doc, MWAWGraphicStyle::emptyStyle());
359 break;
360 }
361 case SuperPaintParserInternal::Shape::Picture:
362 if (!shape.m_entry.valid()) {
363 MWAW_DEBUG_MSG(("SuperPaintParser::sendPictures: the picture entry seems bad\n"));
364 break;
365 }
366 else {
367 input->seek(shape.m_entry.begin(), librevenge::RVNG_SEEK_SET);
368 std::shared_ptr<MWAWPict> thePict(MWAWPictData::get(input, static_cast<int>(shape.m_entry.length())));
369 MWAWEmbeddedObject picture;
370 if (thePict && thePict->getBinary(picture))
371 listener->insertPicture(pos, picture);
372 else {
373 MWAW_DEBUG_MSG(("SuperPaintParser::sendPictures: can not check the picture data\n"));
374 break;
375 }
376 }
377 break;
378 case SuperPaintParserInternal::Shape::GraphicShape:
379 listener->insertShape(pos, shape.m_shape, shape.m_style);
380 break;
381 #if !defined(__clang__)
382 default:
383 MWAW_DEBUG_MSG(("SuperPaintParser::sendPictures: find unknown shape type\n"));
384 break;
385 #endif
386 }
387 }
388 return true;
389 }
390
391 bool SuperPaintParser::sendText(int id)
392 {
393 MWAWGraphicListenerPtr listener=getGraphicListener();
394 if (!listener || !listener->canWriteText()) {
395 MWAW_DEBUG_MSG(("SuperPaintParser::sendText: can not find the listener\n"));
396 return false;
397 }
398 if (id<0 || id>=static_cast<int>(m_state->m_shapeList.size())) {
399 MWAW_DEBUG_MSG(("SuperPaintParser::sendText: can not find the textbox\n"));
400 return false;
401 }
402 auto const &shape=m_state->m_shapeList[size_t(id)];
403 if (!shape.m_entry.valid() || shape.m_type != SuperPaintParserInternal::Shape::TextBox) {
404 MWAW_DEBUG_MSG(("SuperPaintParser::sendText: the textbox seems invalid\n"));
405 return false;
406 }
407 MWAWParagraph para;
408 para.setInterline(shape.m_interline, librevenge::RVNG_PERCENT);
409 para.m_justify=shape.m_justify;
410 listener->setParagraph(para);
411 MWAWFont font=shape.m_font;
412 listener->setFont(font);
413
414 MWAWInputStreamPtr input = getInput();
415 input->seek(shape.m_entry.begin(), librevenge::RVNG_SEEK_SET);
416 libmwaw::DebugStream f;
417 f << "Shape-Text:";
418 for (long i = 0; i < shape.m_entry.length(); i++) {
419 auto c = static_cast<unsigned char>(input->readULong(1));
420 f << c;
421 switch (c) {
422 case 0x9:
423 listener->insertTab();
424 break;
425 case 0xd:
426 // this is marks the end of a paragraph
427 listener->insertEOL();
428 break;
429 default:
430 listener->insertCharacter(c);
431 break;
432 }
433 }
434 ascii().addPos(shape.m_entry.begin());
435 ascii().addNote(f.str().c_str());
436
437 return true;
438 }
439
440 ////////////////////////////////////////////////////////////
441 // read shapes: vector graphic document
442 ////////////////////////////////////////////////////////////
443 bool SuperPaintParser::readPictures()
444 {
445 MWAWInputStreamPtr input = getInput();
446 input->seek(512, librevenge::RVNG_SEEK_SET);
447 libmwaw::DebugStream f;
448 while (!input->isEnd()) {
449 long pos=input->tell();
450 if (readShape()) continue;
451 input->seek(pos, librevenge::RVNG_SEEK_SET);
452 f.str("");
453 f << "Entries(Picture):";
454 auto dSz=static_cast<int>(input->readULong(2));
455 if (!input->checkPosition(pos+2+dSz)) {
456 input->seek(pos, librevenge::RVNG_SEEK_SET);
457 break;
458 }
459 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
460 ascii().addPos(pos);
461 ascii().addNote(f.str().c_str());
462 }
463 return !m_state->m_shapeList.empty();
464 }
465
466 bool SuperPaintParser::readShape()
467 {
468 MWAWInputStreamPtr input = getInput();
469 long pos=input->tell();
470
471 libmwaw::DebugStream f;
472 f << "Entries(Shape):";
473 auto dSz=static_cast<int>(input->readULong(2));
474 if (dSz==0xFFFF) {
475 f << "endGroup";
476 ascii().addPos(pos);
477 ascii().addNote(f.str().c_str());
478 return true;
479 }
480 if (dSz<50 || !input->checkPosition(pos+2+dSz)) {
481 input->seek(pos, librevenge::RVNG_SEEK_SET);
482 return false;
483 }
484 int dim[4];
485 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
486 MWAWBox2f box(MWAWVec2f(static_cast<float>(dim[1]),static_cast<float>(dim[0])),MWAWVec2f(static_cast<float>(dim[3]),static_cast<float>(dim[2])));
487 f << "box=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ",";
488 int val;
489 for (int i=0; i<4; ++i) { // always 0?
490 val=static_cast<int>(input->readLong(2));
491 if (val) f << "f" << i << "=" << val << ",";
492 }
493 val=static_cast<int>(input->readLong(1)); // always 0?
494 if (val) f << "f4=" << val << ",";
495 auto type=static_cast<int>(input->readLong(1));
496 MWAWGraphicStyle style;
497 MWAWColor fontColor(MWAWColor::black());
498 if (type) {
499 val=static_cast<int>(input->readULong(1));
500 bool hasSurfaceColor=false;
501 if (val==1)
502 hasSurfaceColor=true;
503 else if (val)
504 f << "#hasColor=" << val << ",";
505 // small number: pattern?
506 val=static_cast<int>(input->readULong(1));
507 if (val) f << "g0=" << val << ",";
508 val=static_cast<int>(input->readULong(1));
509 if (val==1) f << "select,";
510 else if (val) f << "#select=val,";
511 val=static_cast<int>(input->readULong(1)); // always 0
512 if (val) f << "g1=" << val << ",";
513 val=static_cast<int>(input->readULong(2)); // g3=a big number
514 if (val) f << "g2=" << std::hex << val << std::dec << ",";
515 int penSize[2];
516 for (auto &pSize : penSize) pSize=static_cast<int>(input->readULong(2));
517 if (penSize[0]!=1||penSize[1]!=1) f << "pen[size]=" << penSize[0] << "x" << penSize[1] << ",";
518 style.m_lineWidth=0.5f*float(penSize[0]+penSize[1]);
519 MWAWGraphicStyle::Pattern pat[2];
520 for (auto &p : pat) {
521 p.m_dim=MWAWVec2i(8,8);
522 p.m_data.resize(8);
523 for (auto &data : p.m_data) data=static_cast<uint8_t>(input->readULong(1));
524 }
525 f << "line[pattern]=";
526 MWAWColor color;
527 if (pat[0].getUniqueColor(color))
528 f << color << ",";
529 else
530 f << "[" << pat[0] << "],";
531 f << "surface[pattern]=";
532 if (pat[1].getUniqueColor(color))
533 f << color << ",";
534 else
535 f << "[" << pat[1] << "],";
536 val=static_cast<int>(input->readULong(2));
537 if (m_state->getColor((val>>13)&7, color)) {
538 pat[0].m_colors[1]=color;
539 if (!color.isBlack())
540 f << "line0[col]=" << color << ",";
541 }
542 if (m_state->getColor((val>>10)&7, color)) {
543 pat[0].m_colors[0]=color;
544 if (!color.isWhite())
545 f << "line1[col]=" << color << ",";
546 }
547 if (m_state->getColor((val>>7)&7, color)) {
548 pat[1].m_colors[1]=fontColor=color;
549 if (!color.isBlack())
550 f << "surf0[col]=" << color << ",";
551 }
552 if (m_state->getColor(val&7, color)) {
553 pat[1].m_colors[0]=color;
554 if (!color.isWhite())
555 f << "surf1[col]=" << color << ",";
556 }
557 if (pat[0].getAverageColor(color))
558 style.m_lineColor=color;
559 if (hasSurfaceColor) {
560 if (pat[1].getUniqueColor(color))
561 style.setSurfaceColor(color);
562 else
563 style.setPattern(pat[1]);
564 }
565 if (val&0x78) f << "g4=" << ((val&0x78)>>3) << ",";
566 }
567 int numDatas=0;
568 SuperPaintParserInternal::Shape::Type shapeType=SuperPaintParserInternal::Shape::GraphicShape;
569 switch (type) {
570 case 0:
571 shapeType=SuperPaintParserInternal::Shape::Group;
572 m_state->m_shapeList.push_back(SuperPaintParserInternal::Shape(shapeType, box));
573 f << "group,";
574 break;
575 case 0x2:
576 shapeType=SuperPaintParserInternal::Shape::Picture;
577 m_state->m_shapeList.push_back(SuperPaintParserInternal::Shape(shapeType, box));
578 f << "pict,";
579 numDatas=2;
580 break;
581 case 0x17: {
582 f << "textbox,";
583 val=static_cast<int>(input->readULong(4));
584 if (val) f << "id?=" << std::hex << val << ",";
585 shapeType=SuperPaintParserInternal::Shape::TextBox;
586 m_state->m_shapeList.push_back(SuperPaintParserInternal::Shape(shapeType, box));
587 numDatas=2;
588 if (dSz<0x4a) {
589 MWAW_DEBUG_MSG(("SuperPaintParser::readShape: textbox size seems to short\n"));
590 f << "###";
591 break;
592 }
593 MWAWFont font;
594 font.setId(static_cast<int>(input->readULong(2)));
595 font.setSize(float(input->readULong(2)));
596 font.setColor(fontColor);
597 val=static_cast<int>(input->readULong(2));
598 switch (val&3) {
599 case 0: // left
600 break;
601 case 1:
602 m_state->m_shapeList.back().m_justify=MWAWParagraph::JustificationCenter;
603 f << "center,";
604 break;
605 case 2:
606 m_state->m_shapeList.back().m_justify=MWAWParagraph::JustificationRight;
607 f << "right,";
608 break;
609 default:
610 f << "#align=3,";
611 break;
612 }
613 if ((val&0xFFFC)!=8) f << "#fl0=" << std::hex << (val&0xFFFC) << std::dec << ",";
614 f << "height[line]?=" << input->readLong(2) << ","; // checkme
615 val=static_cast<int>(input->readULong(2));
616 switch (val&3) {
617 case 0: // basic interline
618 break;
619 case 1:
620 m_state->m_shapeList.back().m_interline=1.5;
621 f << "interline=150%,";
622 break;
623 case 2:
624 m_state->m_shapeList.back().m_interline=2;
625 f << "interline=200%,";
626 break;
627 default:
628 break;
629 }
630 if ((val&0xFFFC)!=0xc) f << "#fl1=" << std::hex << (val&0xFFFC) << std::dec << ",";
631 val=static_cast<int>(input->readULong(2)); // no sure about this one, seems sometimes related to the next font flags...
632 if (val) f << "fl2="<< std::hex << val << std::dec << ",";
633 auto flag=static_cast<int>(input->readULong(1));
634 uint32_t flags=0;
635 if (flag&0x1) flags |= MWAWFont::boldBit;
636 if (flag&0x2) flags |= MWAWFont::italicBit;
637 if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
638 if (flag&0x8) flags |= MWAWFont::embossBit;
639 if (flag&0x10) flags |= MWAWFont::shadowBit;
640 if (flag&0xE0) f << "#fFlag=" << std::hex << (flag&0xE0) << std::dec << ",";
641 font.setFlags(flags);
642
643 m_state->m_shapeList.back().m_font=font;
644 f << "font=[" << font.getDebugString(getParserState()->m_fontConverter) << "],";
645 break;
646 }
647 case 0x6: // axis line
648 case 0x18: { // normal
649 if (type==6)
650 f << "line[axis],";
651 else
652 f << "line,";
653 m_state->m_shapeList.push_back(SuperPaintParserInternal::Shape(shapeType, box));
654 m_state->m_shapeList.back().m_shape=MWAWGraphicShape::line(box[0],box[1]);
655 // now check the box size
656 m_state->m_shapeList.back().m_box=m_state->m_shapeList.back().m_shape.getBdBox();
657 break;
658 }
659 case 0x19:
660 f << "rect,";
661 m_state->m_shapeList.push_back(SuperPaintParserInternal::Shape(shapeType, box));
662 m_state->m_shapeList.back().m_shape=MWAWGraphicShape::rectangle(box);
663 break;
664 case 0x1a:
665 f << "roundRect,";
666 m_state->m_shapeList.push_back(SuperPaintParserInternal::Shape(shapeType, box));
667 m_state->m_shapeList.back().m_shape=MWAWGraphicShape::rectangle(box, MWAWVec2f(5,5));
668 break;
669 case 0x1b:
670 case 0x20:
671 if (type==0x20)
672 f << "circle[ortho],";
673 else
674 f << "circle,";
675 m_state->m_shapeList.push_back(SuperPaintParserInternal::Shape(shapeType, box));
676 m_state->m_shapeList.back().m_shape=MWAWGraphicShape::circle(box);
677 break;
678 case 0x1c: {
679 f << "arc,";
680 int fileAngle[2];
681 for (auto &angl : fileAngle) angl = static_cast<int>(input->readLong(2));
682 int angle[2] = { 90-fileAngle[0]-fileAngle[1], 90-fileAngle[0] };
683 while (angle[1] > 360) {
684 angle[0]-=360;
685 angle[1]-=360;
686 }
687 while (angle[0] < -360) {
688 angle[0]+=360;
689 angle[1]+=360;
690 }
691 MWAWVec2f center = box.center();
692 MWAWVec2f axis = 0.5f*MWAWVec2f(box.size());
693 // we must compute the real bd box
694 float minVal[2] = { 0, 0 }, maxVal[2] = { 0, 0 };
695 int limitAngle[2];
696 for (int i = 0; i < 2; i++)
697 limitAngle[i] = (angle[i] < 0) ? int(angle[i]/90)-1 : int(angle[i]/90);
698 for (int bord = limitAngle[0]; bord <= limitAngle[1]+1; bord++) {
699 float ang = (bord == limitAngle[0]) ? float(angle[0]) :
700 (bord == limitAngle[1]+1) ? float(angle[1]) : float(90 * bord);
701 ang *= float(M_PI/180.);
702 float const actVal[2] = { axis[0] *std::cos(ang), -axis[1] *std::sin(ang)};
703 if (actVal[0] < minVal[0]) minVal[0] = actVal[0];
704 else if (actVal[0] > maxVal[0]) maxVal[0] = actVal[0];
705 if (actVal[1] < minVal[1]) minVal[1] = actVal[1];
706 else if (actVal[1] > maxVal[1]) maxVal[1] = actVal[1];
707 }
708 MWAWBox2f realBox(MWAWVec2f(center[0]+minVal[0],center[1]+minVal[1]),
709 MWAWVec2f(center[0]+maxVal[0],center[1]+maxVal[1]));
710 m_state->m_shapeList.push_back(SuperPaintParserInternal::Shape(shapeType, realBox));
711 m_state->m_shapeList.back().m_shape = MWAWGraphicShape::pie(realBox, box, MWAWVec2f(float(angle[0]),float(angle[1])));
712 break;
713 }
714 case 0x1d:
715 case 0x1e:
716 if (type==0x1d)
717 f << "poly,";
718 else
719 f << "poly[hand],";
720 m_state->m_shapeList.push_back(SuperPaintParserInternal::Shape(shapeType, box));
721 m_state->m_shapeList.back().m_shape.m_type=MWAWGraphicShape::Polygon;
722 numDatas=2;
723 break;
724 default:
725 input->seek(pos, librevenge::RVNG_SEEK_SET);
726 return false;
727 }
728 if (m_state->m_shapeList.empty() || m_state->m_shapeList.back().m_type!=shapeType) {
729 MWAW_DEBUG_MSG(("SuperPaintParser::readShape: the shape list seems bad\n"));
730 f.str("");
731 f << "Shape:type=" << type << ",###";
732 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
733 ascii().addPos(pos);
734 ascii().addNote(f.str().c_str());
735 return true;
736 }
737 if (input->tell()!=pos+2+dSz) {
738 ascii().addDelimiter(input->tell(),'|');
739 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
740 }
741 ascii().addPos(pos);
742 ascii().addNote(f.str().c_str());
743
744 auto &shape=m_state->m_shapeList.back();
745 shape.m_style=style;
746 if (!numDatas) return true;
747
748 // first zone always 0 (or it is a 4 bytes pointer ) ?
749 pos=input->tell();
750 dSz=static_cast<int>(input->readULong(2));
751 if (!input->checkPosition(pos+2+dSz)) {
752 input->seek(pos, librevenge::RVNG_SEEK_SET);
753 return false;
754 }
755 if (dSz==0) {
756 ascii().addPos(pos);
757 ascii().addNote("_");
758 }
759 else {
760 MWAW_DEBUG_MSG(("SuperPaintParser::readShape: find some data for type %d\n", type));
761 f.str("");
762 f << "Shape-Data0:type=" << type << ",###";
763 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
764 ascii().addPos(pos);
765 ascii().addNote(f.str().c_str());
766 }
767 if (numDatas<=1) return true;
768
769 // now the real data
770 pos=input->tell();
771 dSz=static_cast<int>(input->readULong(2));
772 if (!input->checkPosition(pos+2+dSz)) {
773 input->seek(pos, librevenge::RVNG_SEEK_SET);
774 return false;
775 }
776 switch (shapeType) {
777 case SuperPaintParserInternal::Shape::Picture:
778 shape.m_entry.setBegin(pos+2);
779 shape.m_entry.setLength(dSz);
780 ascii().skipZone(pos+2,pos+1+dSz);
781 break;
782 case SuperPaintParserInternal::Shape::TextBox:
783 shape.m_entry.setBegin(pos+2);
784 shape.m_entry.setLength(dSz);
785 break;
786 case SuperPaintParserInternal::Shape::GraphicShape: {
787 f.str("");
788 f << "Shape-Point:";
789 if (shape.m_shape.m_type==MWAWGraphicShape::Polygon)
790 f << "poly,";
791 else {
792 m_state->m_shapeList.pop_back();
793 MWAW_DEBUG_MSG(("SuperPaintParser::readShape: unexpected type\n"));
794 f << "###";
795 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
796 ascii().addPos(pos);
797 ascii().addNote(f.str().c_str());
798 return true;
799 }
800 if (dSz<6 || (dSz%4)!=2) {
801 m_state->m_shapeList.pop_back();
802 MWAW_DEBUG_MSG(("SuperPaintParser::readShape: can not compute the number of polygon points\n"));
803 f << "###";
804 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
805 ascii().addPos(pos);
806 ascii().addNote(f.str().c_str());
807 return true;
808 }
809 val=static_cast<int>(input->readLong(2));
810 if (val!=dSz) f << "#sz=" << val << ",";
811 f << "bdbox=[";
812 for (int i=0; i< 4; ++i) f << input->readLong(2) << ",";
813 f << "],";
814 int N=(dSz-6)/4;
815 f << "N=" << N << ",pts=[";
816 std::vector<MWAWVec2f> vertices(static_cast<size_t>(N));
817 for (int i=0; i<N; ++i) {
818 int coord[2];
819 for (auto &c : coord) c=static_cast<int>(input->readLong(2));
820 f << MWAWVec2i(coord[1],coord[0]) << ",";
821 vertices[size_t(i)]=MWAWVec2f(static_cast<float>(coord[1]),static_cast<float>(coord[0]))-box[0];
822 }
823 f << "],";
824 ascii().addPos(pos);
825 ascii().addNote(f.str().c_str());
826 // the last vertices is sometimes bad, so...
827 if (!vertices.empty())
828 vertices.pop_back();
829 shape.m_shape.m_vertices=vertices;
830 break;
831 }
832 case SuperPaintParserInternal::Shape::Group:
833 #if !defined(__clang__)
834 default:
835 #endif
836 MWAW_DEBUG_MSG(("SuperPaintParser::readShape: call with unexpeced type\n"));
837 f.str("");
838 f << "Shape-Data1:type=" << type << ",###";
839 ascii().addPos(pos);
840 ascii().addNote(f.str().c_str());
841 }
842 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
843
844 if (numDatas<=2) return true;
845 // must not appear
846 MWAW_DEBUG_MSG(("SuperPaintParser::readShape: find numData=%d\n", numDatas));
847 for (int i=2; i<numDatas; ++i) {
848 pos=input->tell();
849 f.str("");
850 f << "Shape-Data" << i << ":##";
851 dSz=static_cast<int>(input->readULong(2));
852 if (!input->checkPosition(pos+2+dSz)) {
853 input->seek(pos, librevenge::RVNG_SEEK_SET);
854 return false;
855 }
856 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
857 ascii().addPos(pos);
858 ascii().addNote(f.str().c_str());
859 }
860 return true;
861 }
862
863 ////////////////////////////////////////////////////////////
864 // read/send a bitmap ( paint document)
865 ////////////////////////////////////////////////////////////
866 bool SuperPaintParser::sendBitmap()
867 {
868 MWAWGraphicListenerPtr listener=getGraphicListener();
869 if (!listener) {
870 MWAW_DEBUG_MSG(("SuperPaintParser::sendBitmap: can not find the listener\n"));
871 return false;
872 }
873
874 MWAWEmbeddedObject picture;
875 if (!m_state->m_bitmap || !m_state->m_bitmap->getBinary(picture)) return false;
876
877 MWAWPageSpan const &page=getPageSpan();
878 MWAWPosition pos(MWAWVec2f(float(page.getMarginLeft()),float(page.getMarginRight())),
879 MWAWVec2f(float(page.getPageWidth()),float(page.getPageLength())), librevenge::RVNG_INCH);
880 pos.setRelativePosition(MWAWPosition::Page);
881 pos.m_wrapping = MWAWPosition::WNone;
882 listener->insertPicture(pos, picture);
883 return true;
884 }
885
886 bool SuperPaintParser::readBitmap(bool onlyCheck)
887 {
888 MWAWInputStreamPtr input = getInput();
889 input->seek(512, librevenge::RVNG_SEEK_SET);
890 long pos=input->tell();
891 libmwaw::DebugStream f;
892 f << "Entries(Bitmap):";
893 if (!input->checkPosition(pos+48)) {
894 MWAW_DEBUG_MSG(("SuperPaintParser::readBitmap: the bitmap header seems too short\n"));
895 f << "###";
896 ascii().addPos(pos);
897 ascii().addNote(f.str().c_str());
898 return false;
899 }
900 int val;
901 for (int i=0; i<11; ++i) {
902 static int const expected[]= {0x11,1,0xa0,0x62,0xc,0xa0,0,0x8e,1,0,0xa};
903 val=static_cast<int>(input->readULong(1));
904 if (val!=expected[i])
905 f << "f" << i << "=" << val << ",";
906 }
907 int pictDim[4];
908 for (auto &pDim : pictDim) pDim=static_cast<int>(input->readLong(2));
909 f << "dim=" << pictDim[1] << "x" << pictDim[0] << "<->" << pictDim[3] << "x" << pictDim[2] << ",";
910 if (pictDim[0]<0 || pictDim[1]<0 || pictDim[0]>pictDim[2] || pictDim[1]>pictDim[3]) {
911 MWAW_DEBUG_MSG(("SuperPaintParser::readBitmap: the picture dimension seems bad\n"));
912 f << "###";
913 ascii().addPos(pos);
914 ascii().addNote(f.str().c_str());
915 return false;
916 }
917 ascii().addPos(pos);
918 ascii().addNote(f.str().c_str());
919 input->seek(pos+19, librevenge::RVNG_SEEK_SET);
920
921 std::shared_ptr<MWAWPictBitmapIndexed> pict;
922 if (!onlyCheck) {
923 pict.reset(new MWAWPictBitmapIndexed(MWAWVec2i(pictDim[3],pictDim[2])));
924 std::vector<MWAWColor> colors(2);
925 colors[0]=MWAWColor::white();
926 colors[1]=MWAWColor::black();
927 pict->setColors(colors);
928 }
929 while (!input->isEnd()) {
930 pos=input->tell();
931 f.str("");
932 f << "Bitmap-Row:";
933 val=static_cast<int>(input->readULong(1));
934 if (val!=0x98||!input->checkPosition(pos+29)) {
935 input->seek(pos, librevenge::RVNG_SEEK_SET);
936 break;
937 }
938 auto numColByRow=static_cast<int>(input->readLong(2)); // 20|2c|2a
939 f << "numCol[byRow]=" << numColByRow << ",";
940 int dim[4];
941 for (auto &d : dim) d=static_cast<int>(input->readULong(2));
942 f << "dim?=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ",";
943 if (dim[2]<dim[0] || numColByRow*8 < dim[3]-dim[1] || dim[1]<0 || dim[3]>pictDim[3]+8) {
944 input->seek(pos, librevenge::RVNG_SEEK_SET);
945 break;
946 }
947 for (int j=0; j<2; ++j) {
948 // normally the real picture position (before alignment to 8)
949 int dim2[4];
950 for (auto &d : dim2) d=static_cast<int>(input->readULong(2));
951 if (dim[0]!=dim2[0]||dim[2]!=dim2[2]||(dim[1]>dim2[1]||dim[1]+8<dim2[1])||
952 (dim[3]<dim2[3]||dim[3]>dim2[3]+8))
953 f << "dim" << j << "?=" << dim2[1] << "x" << dim2[0] << "<->" << dim2[3] << "x" << dim2[2] << ",";
954 }
955 val=static_cast<int>(input->readLong(2));
956 if (val!=1) f << "f1=" << val << ",";
957 ascii().addPos(pos);
958 ascii().addNote(f.str().c_str());
959
960 for (int r=dim[0]; r<dim[2]; ++r) {
961 pos=input->tell();
962 auto len=static_cast<int>(input->readULong(1));
963 long rEndPos=pos+1+len;
964 if (!len || !input->checkPosition(rEndPos)) {
965 input->seek(pos, librevenge::RVNG_SEEK_SET);
966 break;
967 }
968 f.str("");
969 f << "Bitmap-R" << r << ":";
970 int col=dim[1];
971 while (input->tell()<rEndPos) { // UnpackBits
972 auto wh=static_cast<int>(input->readULong(1));
973 if (wh>=0x81) {
974 auto color=static_cast<int>(input->readULong(1));
975 if (onlyCheck) {
976 col+=8*(0x101-wh);
977 }
978 else {
979 for (int j=0; j < 0x101-wh; ++j) {
980 for (int b=7; b>=0; --b) {
981 if (col<pictDim[3])
982 pict->set(col, r, (color>>b)&1);
983 ++col;
984 }
985 }
986 }
987 }
988 else { // checkme normally 0x80 is reserved and almost nobody used it (for ending the compression)
989 if (input->tell()+wh+1>rEndPos) {
990 MWAW_DEBUG_MSG(("SuperPaintParser::readBitmap: can not read row %d\n", r));
991 f << "###";
992 ascii().addPos(pos);
993 ascii().addNote(f.str().c_str());
994 return false;
995 }
996 if (onlyCheck) {
997 col+=8*(wh+1);
998 input->seek(wh+1, librevenge::RVNG_SEEK_CUR);
999 }
1000 else {
1001 for (int j=0; j < wh+1; ++j) {
1002 auto color=static_cast<int>(input->readULong(1));
1003 for (int b=7; b>=0; --b) {
1004 if (col<pictDim[3])
1005 pict->set(col, r, (color>>b)&1);
1006 ++col;
1007 }
1008 }
1009 }
1010 }
1011 if (col>pictDim[3]+8) {
1012 MWAW_DEBUG_MSG(("SuperPaintParser::readBitmap: can not read row %d\n", r));
1013 f << "###";
1014 ascii().addPos(pos);
1015 ascii().addNote(f.str().c_str());
1016 return false;
1017 }
1018 }
1019 f << "col=" << col << ",";
1020 ascii().addPos(pos);
1021 ascii().addNote(f.str().c_str());
1022 input->seek(rEndPos, librevenge::RVNG_SEEK_SET);
1023 }
1024 }
1025 pos=input->tell();
1026 f.str("");
1027 f << "Bitmap-End:";
1028 if (!input->checkPosition(pos+6) || input->readULong(1)!=0xa0) {
1029 MWAW_DEBUG_MSG(("SuperPaintParser::readBitmap: problem when reading the bitmap\n"));
1030 f << "###";
1031 ascii().addPos(pos);
1032 ascii().addNote(f.str().c_str());
1033 return false;
1034 }
1035 if (!onlyCheck)
1036 m_state->m_bitmap=pict;
1037 for (int i=0; i<3; ++i) {
1038 int const expected[]= {0,0x8F,0xFF};
1039 val=static_cast<int>(input->readULong(1));
1040 if (val!=expected[i])
1041 f << "f" << i << "=" << val << ",";
1042 }
1043 ascii().addPos(pos);
1044 ascii().addNote(f.str().c_str());
1045 if (!onlyCheck)
1046 m_state->m_bitmap=pict;
1047 return true;
1048 }
1049
1050 ////////////////////////////////////////////////////////////
1051 // read the header
1052 ////////////////////////////////////////////////////////////
1053 bool SuperPaintParser::checkHeader(MWAWHeader *header, bool strict)
1054 {
1055 *m_state = SuperPaintParserInternal::State();
1056 MWAWInputStreamPtr input = getInput();
1057 if (!input || !input->hasDataFork() || !input->checkPosition(512))
1058 return false;
1059
1060 int const vers=1;
1061 libmwaw::DebugStream f;
1062 f << "FileHeader:";
1063 input->seek(0, librevenge::RVNG_SEEK_SET);
1064 if (input->readULong(2)!=0x1000) return false;
1065 int dim[4];
1066 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
1067 if (dim[0]||dim[1]||dim[2]||dim[3])
1068 f << "bitmap[content]=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ",";
1069 auto val=static_cast<int>(input->readLong(2));
1070 switch (val) {
1071 case 1:
1072 m_state->m_kind=MWAWDocument::MWAW_K_PAINT;
1073 f << "paint,";
1074 break;
1075 case 2: // not yet implemented
1076 f << "draw,";
1077 break;
1078 default:
1079 return false;
1080 }
1081 val=static_cast<int>(input->readLong(1));
1082 if (val==1) f << "hasPrintInfo?,";
1083 else if (val) f << "#f0=" << val << ",";
1084 val=static_cast<int>(input->readLong(1));
1085 if (val==8) f << "color?,";
1086 else if (val) f << "#f1=" << val << ",";
1087 ascii().addPos(0);
1088 ascii().addNote(f.str().c_str());
1089
1090 if (strict) {
1091 if (m_state->m_kind==MWAWDocument::MWAW_K_PAINT) {
1092 if (!readBitmap(true))
1093 return false;
1094 }
1095 else {
1096 input->seek(512,librevenge::RVNG_SEEK_SET);
1097 for (int i=0; i<4; ++i) {
1098 if (input->isEnd()) break;
1099 long pos=input->tell();
1100 auto dSz=long(input->readULong(2));
1101 if (!input->checkPosition(pos+2+dSz)) return false;
1102 input->seek(pos+2+dSz,librevenge::RVNG_SEEK_SET);
1103 }
1104 }
1105 }
1106
1107 setVersion(vers);
1108 if (header)
1109 header->reset(MWAWDocument::MWAW_T_SUPERPAINT, vers, m_state->m_kind);
1110 input->seek(512,librevenge::RVNG_SEEK_SET);
1111
1112 return true;
1113 }
1114
1115 ////////////////////////////////////////////////////////////
1116 // try to read the header zone
1117 ////////////////////////////////////////////////////////////
1118 bool SuperPaintParser::readHeader()
1119 {
1120 MWAWInputStreamPtr input = getInput();
1121 if (!input->checkPosition(512)) {
1122 MWAW_DEBUG_MSG(("SuperPaintParser::readHeader: the header zone seems too short\n"));
1123 ascii().addPos(14);
1124 ascii().addNote("Entries(Header):#");
1125 return false;
1126 }
1127 input->seek(14,librevenge::RVNG_SEEK_SET);
1128 if (!readPrintInfo()) {
1129 ascii().addPos(14);
1130 ascii().addNote("Entries(PrintInfo):#");
1131 input->seek(14+120, librevenge::RVNG_SEEK_SET);
1132 }
1133 long pos=input->tell();
1134 libmwaw::DebugStream f;
1135 f << "Entries(Header):";
1136 int val;
1137 for (int i=0; i<6; ++i) {
1138 int const expected[]= {0x24,1,0,0/*or3*/,0,0x24 /* or 32*/};
1139 val=static_cast<int>(input->readLong(1));
1140 if (val != expected[i]) f << "f" << i << "=" << val << ",";
1141 }
1142 for (int i=0; i<60; ++i) { // always 0?
1143 val=static_cast<int>(input->readLong(2));
1144 if (val) f << "g" << i << "=" << val << ",";
1145 }
1146 ascii().addPos(pos);
1147 ascii().addNote(f.str().c_str());
1148
1149 pos=input->tell();
1150 f.str("");
1151 f << "Header-II:";
1152 for (int i=0; i<126; ++i) { // always 0?
1153 val=static_cast<int>(input->readLong(2));
1154 if (val) f << "f" << i << "=" << val << ",";
1155 }
1156 ascii().addPos(pos);
1157 ascii().addNote(f.str().c_str());
1158 return true;
1159 }
1160
1161 ////////////////////////////////////////////////////////////
1162 // try to read the print info zone
1163 ////////////////////////////////////////////////////////////
1164 bool SuperPaintParser::readPrintInfo()
1165 {
1166 MWAWInputStreamPtr input = getInput();
1167 long pos=input->tell();
1168 long endPos=pos+120;
1169 if (!input->checkPosition(endPos)) {
1170 MWAW_DEBUG_MSG(("SuperPaintParser::readPrintInfo: file seems too short\n"));
1171 return false;
1172 }
1173 libmwaw::DebugStream f;
1174 f << "Entries(PrintInfo):";
1175 libmwaw::PrinterInfo info;
1176 if (!info.read(input)) {
1177 MWAW_DEBUG_MSG(("SuperPaintParser::readPrintInfo: can not read print info\n"));
1178 return false;
1179 }
1180 f << info;
1181 MWAWVec2i paperSize = info.paper().size();
1182 MWAWVec2i pageSize = info.page().size();
1183 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
1184 paperSize.x() <= 0 || paperSize.y() <= 0) {
1185 ascii().addPos(pos);
1186 ascii().addNote(f.str().c_str());
1187 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1188 return true;
1189 }
1190
1191 // define margin from print info
1192 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
1193 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
1194
1195 // move margin left | top
1196 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
1197 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
1198 lTopMargin -= MWAWVec2i(decalX, decalY);
1199 rBotMargin += MWAWVec2i(decalX, decalY);
1200
1201 // decrease right | bottom
1202 int rightMarg = rBotMargin.x() -50;
1203 if (rightMarg < 0) rightMarg=0;
1204 int botMarg = rBotMargin.y() -50;
1205 if (botMarg < 0) botMarg=0;
1206
1207 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
1208 getPageSpan().setMarginBottom(botMarg/72.0);
1209 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
1210 getPageSpan().setMarginRight(rightMarg/72.0);
1211 getPageSpan().setFormLength(paperSize.y()/72.);
1212 getPageSpan().setFormWidth(paperSize.x()/72.);
1213
1214 ascii().addPos(pos);
1215 ascii().addNote(f.str().c_str());
1216 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1217 return true;
1218 }
1219
1220 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: