Debian Codegraph @

src/lib/CorelPainterParser.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 <iomanip>
35 #include <iostream>
36 #include <limits>
37 #include <set>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWFontConverter.hxx"
43 #include "MWAWGraphicListener.hxx"
44 #include "MWAWGraphicShape.hxx"
45 #include "MWAWHeader.hxx"
46 #include "MWAWParagraph.hxx"
47 #include "MWAWPictBitmap.hxx"
48 #include "MWAWPictData.hxx"
49 #include "MWAWPosition.hxx"
50 #include "MWAWSubDocument.hxx"
51
52 #include "CorelPainterParser.hxx"
53
54 /** Internal: the structures of a CorelPainterParser */
55 namespace CorelPainterParserInternal
56 {
57 ////////////////////////////////////////////////////////////
58 //! Internal: a node of a CorelPainterParser
59 struct Node {
60 //! constructor
61 Node()
62 {
63 for (auto &v: m_values) v=0;
64 }
65 //! the child
66 std::shared_ptr<Node> m_childs[2];
67 //! the values
68 int m_values[2];
69 };
70 ////////////////////////////////////////////////////////////
71 //! Internal: a zone header of a CorelPainterParser
72 struct ZoneHeader {
73 //! constructor
74 ZoneHeader()
75 : m_isMainZone(false)
76 , m_dimension()
77 , m_origin()
78 , m_pixelByInch(0)
79 , m_numTreeNodes(0)
80 , m_tree()
81 , m_bitmapPos(0)
82 , m_rsrcDataPos(0)
83 , m_nextPos(0)
84 , m_rsrcMap()
85 {
86 for (auto &f : m_flags) f=0;
87 }
88 //! check if it is a picture header
89 bool isBitmap() const
90 {
91 if (m_dimension[0]<=2 || m_dimension[1]<=2) return false;
92 long endPos=m_rsrcDataPos>0 ? m_rsrcDataPos : m_nextPos;
93 if (m_bitmapPos>=endPos) return false;
94 if ((m_flags[1]&1) && m_bitmapPos+4*long(m_dimension[0])*long(m_dimension[1])>endPos)
95 return false;
96 return true;
97 }
98 //! a flag to know if this is the main picture
99 bool m_isMainZone;
100 //! the bitmap dimension
101 MWAWVec2i m_dimension;
102 //! the bitmap origin
103 MWAWVec2i m_origin;
104 //! number of pixel by inch
105 int m_pixelByInch;
106 /// the number of Huffman node
107 int m_numTreeNodes;
108 /// the Huffman tree
109 std::shared_ptr<Node> m_tree;
110 /// the bitmap position
111 long m_bitmapPos;
112 //! the resource data position
113 long m_rsrcDataPos;
114 //! the next zone position
115 long m_nextPos;
116 //! the main zone flags
117 int m_flags[2];
118 //! the different rsrc zone (v7)
119 std::map<std::string, MWAWEntry> m_rsrcMap;
120 };
121
122 ////////////////////////////////////////
123 //! Internal: the state of a CorelPainterParser
124 struct State {
125 //! constructor
126 State()
127 : m_zoneList()
128 , m_pixelByInch(0)
129 {
130 }
131 //! the main zone header
132 std::vector<ZoneHeader> m_zoneList;
133 //! number of pixel by inch
134 int m_pixelByInch;
135 };
136
137 ////////////////////////////////////////
138 //! Internal: the subdocument of a CorelPainterParser
139 class SubDocument final : public MWAWSubDocument
140 {
141 public:
142 SubDocument(CorelPainterParser &pars, MWAWInputStreamPtr const &input, MWAWEntry const &entry, MWAWEntry const &unicodeEntry)
143 : MWAWSubDocument(&pars, input, entry)
144 , m_unicodeEntry(unicodeEntry)
145 {
146 }
147
148 //! destructor
149 ~SubDocument() final {}
150
151 //! operator!=
152 bool operator!=(MWAWSubDocument const &doc) const final
153 {
154 if (MWAWSubDocument::operator!=(doc)) return true;
155 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
156 if (!sDoc) return true;
157 return m_unicodeEntry!=sDoc->m_unicodeEntry;
158 }
159 //! the parser function
160 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
161
162 private:
163 //! the unicode entry
164 MWAWEntry m_unicodeEntry;
165
166 SubDocument(SubDocument const &orig) = delete;
167 SubDocument &operator=(SubDocument const &orig) = delete;
168 };
169
170 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
171 {
172 if (!listener || !listener->canWriteText()) {
173 MWAW_DEBUG_MSG(("CorelPainterParserInternal::SubDocument::parse: no listener\n"));
174 return;
175 }
176 auto *parser=dynamic_cast<CorelPainterParser *>(m_parser);
177 if (!parser) {
178 MWAW_DEBUG_MSG(("CorelPainterParserInternal::SubDocument::parse: no parser\n"));
179 return;
180 }
181 long pos = m_input->tell();
182 parser->sendText(m_zone, m_unicodeEntry);
183 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
184 }
185
186 }
187
188 ////////////////////////////////////////////////////////////
189 // constructor/destructor, ...
190 ////////////////////////////////////////////////////////////
191 CorelPainterParser::CorelPainterParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
192 : MWAWGraphicParser(input, rsrcParser, header)
193 , m_state()
194 {
195 init();
196 }
197
198 CorelPainterParser::~CorelPainterParser()
199 {
200 }
201
202 void CorelPainterParser::init()
203 {
204 resetGraphicListener();
205 setAsciiName("main-1");
206
207 m_state.reset(new CorelPainterParserInternal::State);
208
209 getPageSpan().setMargins(0.1);
210 }
211
212 ////////////////////////////////////////////////////////////
213 // the parser
214 ////////////////////////////////////////////////////////////
215 void CorelPainterParser::parse(librevenge::RVNGDrawingInterface *docInterface)
216 {
217 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
218 bool ok = false;
219 try {
220 // create the asciiFile
221 ascii().setStream(getInput());
222 ascii().open(asciiName());
223 checkHeader(nullptr);
224 ok = createZones();
225 if (ok) {
226 createDocument(docInterface);
227 for (auto const &z : m_state->m_zoneList) {
228 if (z.isBitmap())
229 sendBitmap(z);
230 else
231 sendZone(z);
232 }
233 }
234 ascii().reset();
235 }
236 catch (...) {
237 MWAW_DEBUG_MSG(("CorelPainterParser::parse: exception catched when parsing\n"));
238 ok = false;
239 }
240
241 resetGraphicListener();
242 if (!ok) throw(libmwaw::ParseException());
243 }
244
245 ////////////////////////////////////////////////////////////
246 // create the document
247 ////////////////////////////////////////////////////////////
248 void CorelPainterParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
249 {
250 if (!documentInterface) return;
251 if (getGraphicListener()) {
252 MWAW_DEBUG_MSG(("CorelPainterParser::createDocument: listener already exist\n"));
253 return;
254 }
255 if (m_state->m_zoneList.empty()) {
256 MWAW_DEBUG_MSG(("CorelPainterParser::createDocument: can not find any zone\n"));
257 return;
258 }
259 auto const &zone=m_state->m_zoneList[0];
260 int const &pixelByInch=m_state->m_pixelByInch;
261 if (pixelByInch>0 && pixelByInch<0xFFFF) { // time to update the page dimension
262 getPageSpan().setFormWidth(0.2+double(zone.m_dimension[0])/double(pixelByInch));
263 getPageSpan().setFormLength(0.2+double(zone.m_dimension[1])/double(pixelByInch));
264 }
265 // create the page list
266 MWAWPageSpan ps(getPageSpan());
267 ps.setPageSpan(1);
268 std::vector<MWAWPageSpan> pageList(1,ps);
269 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
270 setGraphicListener(listen);
271 listen->startDocument();
272 }
273
274
275 ////////////////////////////////////////////////////////////
276 //
277 // Intermediate level
278 //
279 ////////////////////////////////////////////////////////////
280 bool CorelPainterParser::createZones()
281 {
282 MWAWInputStreamPtr input = getInput();
283 input->seek(0, librevenge::RVNG_SEEK_SET);
284 while (!input->isEnd()) {
285 long pos=input->tell();
286 CorelPainterParserInternal::ZoneHeader zone;
287 if (!readZoneHeader(zone) || input->tell()>zone.m_nextPos) {
288 MWAW_DEBUG_MSG(("CorelPainterParser::createZones: find extra data\n"));
289 ascii().addPos(pos);
290 ascii().addNote("Entries(UnknownD):###extra");
291 break;
292 }
293 if (zone.m_rsrcDataPos>0)
294 readResourcesList(zone);
295 m_state->m_zoneList.push_back(zone);
296 input->seek(zone.m_nextPos, librevenge::RVNG_SEEK_SET);
297 }
298 if (m_state->m_zoneList.empty() || !m_state->m_zoneList[0].isBitmap()) {
299 MWAW_DEBUG_MSG(("CorelPainterParser::createZones: oops the first zone is not a picture\n"));
300 return false;
301 }
302
303 return true;
304 }
305
306 std::shared_ptr<MWAWPict> CorelPainterParser::readBitmap(CorelPainterParserInternal::ZoneHeader const &zone)
307 {
308 MWAWInputStreamPtr input = getInput();
309 auto const &dim=zone.m_dimension;
310 long endPos= zone.m_rsrcDataPos>0 ? zone.m_rsrcDataPos : zone.m_nextPos;
311 if (dim[0]<2 || dim[1]<2 || input->tell()>=endPos) return nullptr;
312 // in the main zone, the alpha channel is used to store the selected
313 // zone, so we must not retrieve it....
314 auto bitmap=std::make_shared<MWAWPictBitmapColor>(MWAWVec2i(dim[0],dim[1]), !zone.m_isMainZone);
315 std::vector<MWAWColor> listColor;
316 if (!zone.m_numTreeNodes) { // uncompressed
317 libmwaw::DebugStream f;
318 listColor.resize(size_t(dim[0]));
319 for (int i=0; i<dim[1]; ++i) {
320 long pos=input->tell();
321 f.str("");
322 f << "BitmapRow[unc]:";
323 if (pos+4*dim[0]>endPos) {
324 MWAW_DEBUG_MSG(("CorelPainterParser::readBitmap: can not read some row\n"));
325 f << "###";
326 ascii().addPos(pos);
327 ascii().addNote(f.str().c_str());
328 return nullptr;
329 }
330 for (size_t c=0; c<size_t(dim[0]); ++c) {
331 unsigned char data[4];
332 for (auto &d : data) d=static_cast<unsigned char>(input->readULong(1));
333 listColor[c]=MWAWColor(data[1],data[2],data[3],data[0]);
334 }
335 bitmap->setRow(i, listColor.data());
336 ascii().addPos(pos);
337 ascii().addNote(f.str().c_str());
338 }
339 }
340 else { // compressed
341 std::vector<unsigned char> previousValues;
342 previousValues.resize(size_t(4*dim[0]),0);
343 for (int i=0; i<dim[1]; ++i) {
344 long pos=input->tell();
345 if (!readBitmapRow(zone, listColor, previousValues) || input->tell()>endPos) {
346 MWAW_DEBUG_MSG(("CorelPainterParser::readBitmap: can not read some row\n"));
347 ascii().addPos(pos);
348 ascii().addNote("Entries(UnknownB):###extra");
349 return nullptr;
350 }
351 bitmap->setRow(i, listColor.data());
352 }
353 }
354 return bitmap;
355 }
356
357 bool CorelPainterParser::readBitmapRow(CorelPainterParserInternal::ZoneHeader const &zone,
358 std::vector<MWAWColor> &colorList, std::vector<unsigned char> &previousValues)
359 {
360 MWAWInputStreamPtr input = getInput();
361 long pos=input->tell();
362 int const &dim=zone.m_dimension[0];
363 if (!input->checkPosition(pos+4) || dim<=0) return false;
364 libmwaw::DebugStream f;
365 f << "Entries(BitmapRow):";
366 int type=int(input->readLong(1)); // 0,2
367 if (type==0)
368 f << "huffman,";
369 else if (type!=2) f << "##type=" << type << ",";
370 unsigned char firstData=static_cast<unsigned char>(input->readULong(1));
371 if (firstData) f << "d0=" << std::hex << int(firstData) << std::dec << ",";
372 int sz=int(input->readULong(2));
373 long endPos=pos+sz;
374 if (sz<4 || !input->checkPosition(endPos)) {
375 input->seek(pos, librevenge::RVNG_SEEK_SET);
376 return false;
377 }
378 bool ok=true;
379 std::vector<unsigned char> listColorData;
380 int expectedNumData=4*dim;
381 listColorData.reserve(size_t(expectedNumData));
382 listColorData.push_back(firstData);
383 switch (type) {
384 case 0: { // use Huffman tree
385 f << "*[";
386 int buffer, numBitsInBuffer=0;
387 for (int i=0; i<expectedNumData-1; ++i) {
388 int value;
389 if (!decompressData(zone, endPos, value, buffer, numBitsInBuffer)) {
390 MWAW_DEBUG_MSG(("CorelPainterParser::readBitmapRow: oops, problem when decompressing the data\n"));
391 ok=false;
392 f << "###";
393 break;
394 }
395 if (i<10)
396 f << std::hex << value << std::dec << ",";
397 listColorData.push_back(static_cast<unsigned char>(value));
398 }
399 f << "...],";
400 break;
401 }
402 // case 1: never seems in v1.2, maybe exists in v1.0 or v1.1 ?
403 case 2: { // basic compression: 0:(n+1) following values, 1:(n+1)*val1
404 while (input->tell()<endPos && listColorData.size() < size_t(expectedNumData)) {
405 long actPos=input->tell();
406 int subType=int(input->readULong(1));
407 if (subType==0) {
408 int dSz=int(input->readULong(1));
409 long lastPos=actPos+3+dSz;
410 if (lastPos>endPos) {
411 input->seek(actPos, librevenge::RVNG_SEEK_SET);
412 break;
413 }
414 f << "0[";
415 for (int i=0; i<dSz+1; ++i) {
416 listColorData.push_back(static_cast<unsigned char>(input->readULong(1)));
417 if (i<3)
418 f << std::hex << int(listColorData.back()) << std::dec << ",";
419 else if (i==3)
420 f << "...";
421 }
422 f << "],";
423 }
424 else if (subType==1) {
425 if (actPos+3>endPos) {
426 MWAW_DEBUG_MSG(("CorelPainterParser::readBitmapRow: can not read the color data\n"));
427 input->seek(actPos, librevenge::RVNG_SEEK_SET);
428 break;
429 }
430 int nData=int(input->readULong(1));
431 unsigned char value=static_cast<unsigned char>(input->readULong(1));
432 f << "1[" << std::hex << int(value) << std::dec << "x" << (nData+1) << "],";
433 for (int i=0; i<nData+1; ++i) listColorData.push_back(value);
434 }
435 else {
436 input->seek(actPos, librevenge::RVNG_SEEK_SET);
437 MWAW_DEBUG_MSG(("CorelPainterParser::readBitmapRow: unknown sub type %d\n", subType));
438 ok=false;
439 f << "###subType=" << subType;
440 break;
441 }
442 }
443 break;
444 }
445 default:
446 MWAW_DEBUG_MSG(("CorelPainterParser::readBitmapRow: unknown type %d\n", type));
447 ok=false;
448 break;
449 }
450 if (ok && listColorData.size() != size_t(expectedNumData)) {
451 MWAW_DEBUG_MSG(("CorelPainterParser::readBitmapRow: bad number of data\n"));
452 f << "###numData,";
453 ok=false;
454 }
455 if (input->tell()!=endPos && input->tell()+1!=endPos)
456 ascii().addDelimiter(input->tell(),'|');
457 if (ok && previousValues.size()!=listColorData.size()) {
458 MWAW_DEBUG_MSG(("CorelPainterParser::readBitmapRow: oops bad previous values\n"));
459 f << "###prevValues,";
460 ok=false;
461 }
462 ascii().addPos(pos);
463 ascii().addNote(f.str().c_str());
464 input->seek(endPos, librevenge::RVNG_SEEK_SET);
465 if (!ok) return false;
466 // before compressing a row, a difference to the previous row is done,
467 // then in this row, a difference "value less previous value" is done.
468 //
469 // so ...
470 unsigned char actCol=0;
471 for (size_t i=0; i<listColorData.size(); ++i) {
472 actCol = static_cast<unsigned char>(actCol+listColorData[i]);
473 previousValues[i] = static_cast<unsigned char>(previousValues[i]+actCol);
474 }
475
476 colorList.resize(size_t(dim));
477 for (size_t i=0; i<size_t(dim); ++i)
478 colorList[i]=MWAWColor(previousValues[i+size_t(dim)], previousValues[i+2*size_t(dim)],previousValues[i+3*size_t(dim)],static_cast<unsigned char>(255-previousValues[i]));
479 return true;
480 }
481
482 bool CorelPainterParser::decompressData(CorelPainterParserInternal::ZoneHeader const &zone,
483 long endPos, int &value, int &buffer, int &numBitsInBuffer)
484 {
485 if (!zone.m_tree) {
486 MWAW_DEBUG_MSG(("CorelPainterParser::decompressData: can not find the main tree node\n"));
487 return false;
488 }
489 MWAWInputStreamPtr input = getInput();
490 long pos=input->tell();
491 auto node=zone.m_tree;
492 while (!input->isEnd()) {
493 if (numBitsInBuffer<=0) {
494 if (input->tell()>=endPos) break;
495 buffer=int(input->readULong(1));
496 numBitsInBuffer=8;
497 }
498 int c=(buffer>>(--numBitsInBuffer))&1;
499 if (node->m_childs[c]) {
500 node=node->m_childs[c];
501 continue;
502 }
503 value=node->m_values[c];
504 return true;
505 }
506 input->seek(pos, librevenge::RVNG_SEEK_SET);
507 return false;
508 }
509
510 bool CorelPainterParser::readDouble(double &res)
511 {
512 MWAWInputStreamPtr input = getInput();
513 long pos=input->tell();
514 if (!input->checkPosition(pos+4)) {
515 MWAW_DEBUG_MSG(("CorelPainterParser::readDouble: can not read a double\n"));
516 return false;
517 }
518 auto exp=int(input->readULong(1));
519 int fVal=int(input->readULong(1));
520 exp=(exp<<1)+(fVal>>7);
521 double mantisse=(fVal&0x7f)/128., factor=1./128./256.;
522 for (int j=0; j<2; ++j, factor/=256)
523 mantisse+=double(input->readULong(1))*factor;
524 if (exp==0 && mantisse<=0) { // initialized ?
525 res=0;
526 return true;
527 }
528 int sign = 1;
529 if (exp & 0x100) {
530 exp &= 0xff;
531 sign = -1;
532 }
533 exp -= 0x7f;
534 res = std::ldexp(1+mantisse, exp);
535 if (sign == -1)
536 res *= -1.;
537 return true;
538 }
539
540 ////////////////////////////////////////////////////////////
541 // send data
542 ////////////////////////////////////////////////////////////
543 MWAWPosition CorelPainterParser::getZonePosition(CorelPainterParserInternal::ZoneHeader const &zone) const
544 {
545 MWAWPageSpan const &page=getPageSpan();
546 int pixelByInch=zone.m_pixelByInch;
547 if (pixelByInch<=0 || pixelByInch>=0xffff) pixelByInch=m_state->m_pixelByInch;
548 if (pixelByInch<=0 || pixelByInch>=0xffff) {
549 MWAW_DEBUG_MSG(("CorelPainterParser::updatePosition: can not find the number of pixel by inch\n"));
550 pixelByInch=1;
551 }
552 MWAWPosition pos(MWAWVec2f(float(page.getMarginLeft()),float(page.getMarginRight()))
553 +1.f/float(pixelByInch)*MWAWVec2f(zone.m_origin),
554 1.f/float(pixelByInch)*MWAWVec2f(zone.m_dimension), librevenge::RVNG_INCH);
555 pos.setRelativePosition(MWAWPosition::Page);
556 pos.m_wrapping = MWAWPosition::WNone;
557 return pos;
558 }
559
560 bool CorelPainterParser::sendBitmap(CorelPainterParserInternal::ZoneHeader const &zone)
561 {
562 if (!zone.isBitmap()) {
563 MWAW_DEBUG_MSG(("CorelPainterParser::sendBitmap: oops, the zone is not a bitmap\n"));
564 return false;
565 }
566 MWAWGraphicListenerPtr listener=getGraphicListener();
567 if (!listener) {
568 MWAW_DEBUG_MSG(("CorelPainterParser::sendBitmap: can not find the listener\n"));
569 return false;
570 }
571
572 auto input=getInput();
573 input->seek(zone.m_bitmapPos, librevenge::RVNG_SEEK_SET);
574 auto bitmap=readBitmap(zone);
575 if (!bitmap) return false;
576
577 // let finish to read the zone
578 if (zone.m_rsrcDataPos>0 &&input->tell()<zone.m_rsrcDataPos) {
579 ascii().addPos(input->tell());
580 ascii().addNote("Entries(UnknownB):");
581 input->seek(zone.m_rsrcDataPos, librevenge::RVNG_SEEK_SET);
582 }
583 // send the bitmap
584 MWAWEmbeddedObject picture;
585 if (!bitmap->getBinary(picture)) return false;
586
587 #ifdef DEBUG_WITH_FILES
588 if (!picture.m_dataList.empty() && !picture.m_dataList[0].empty()) {
589 static int volatile pictName = 0;
590 libmwaw::DebugStream f;
591 f << "Pict-" << ++pictName << ".png";
592 libmwaw::Debug::dumpFile(picture.m_dataList[0], f.str().c_str());
593 }
594 #endif
595
596 listener->insertPicture(getZonePosition(zone), picture);
597 return true;
598 }
599
600 bool CorelPainterParser::sendZone(CorelPainterParserInternal::ZoneHeader const &zone)
601 {
602 MWAWGraphicListenerPtr listener=getGraphicListener();
603 if (!listener) {
604 MWAW_DEBUG_MSG(("CorelPainterParser::sendZone: can not find the listener\n"));
605 return false;
606 }
607
608 auto it=zone.m_rsrcMap.find("TEXT");
609 auto unicodeIt=zone.m_rsrcMap.find("utxt");
610 if (it!=zone.m_rsrcMap.end()) {
611 it->second.setParsed(true);
612 auto position=getZonePosition(zone);
613 position.setSize(MWAWVec2f(-0.1f,-0.1f));
614 std::shared_ptr<MWAWSubDocument> doc
615 (new CorelPainterParserInternal::SubDocument(*this, getInput(), it->second, unicodeIt!=zone.m_rsrcMap.end() ? unicodeIt->second : MWAWEntry()));
616 listener->insertTextBox(position, doc, MWAWGraphicStyle::emptyStyle());
617 return true;
618 }
619
620 MWAWInputStreamPtr input = getInput();
621 if (zone.isBitmap() || zone.m_rsrcDataPos!=0 || zone.m_bitmapPos+2 >= zone.m_nextPos) {
622 MWAW_DEBUG_MSG(("CorelPainterParser::sendZone: oops, unexpected zone\n"));
623 return false;
624 }
625
626 input->seek(zone.m_bitmapPos,librevenge::RVNG_SEEK_SET);
627 long pos=input->tell();
628 // CHECKME: do we need to check the zone flags, ie find a poly with zone.m_flags[1]=4[13]10
629 MWAWGraphicShape shape;
630 MWAWGraphicStyle style;
631 if (readPolygon(zone.m_nextPos, shape, style)) {
632 // we must rescale the shape
633 int pixelByInch=zone.m_pixelByInch;
634 if (pixelByInch<=0 || pixelByInch>=0xffff) pixelByInch=m_state->m_pixelByInch;
635 if (pixelByInch>0 && pixelByInch<0xff00) { // must be enough to avoid special case
636 float const factor=72.f/float(pixelByInch);
637 shape.scale(MWAWVec2f(factor,factor));
638 }
639 listener->insertShape(getZonePosition(zone), shape, style);
640 }
641 else {
642 MWAW_DEBUG_MSG(("CorelPainterParser::sendZone: sending not spline zone is not implemented\n"));
643 input->seek(pos, librevenge::RVNG_SEEK_SET);
644 }
645 if (input->tell()!=zone.m_nextPos) {
646 MWAW_DEBUG_MSG(("CorelPainterParser::sendZone: find extra data\n"));
647 ascii().addPos(input->tell());
648 ascii().addNote("Entries(Shape):###extra");
649 }
650
651 return false;
652 }
653
654 bool CorelPainterParser::sendText(MWAWEntry const &entry, MWAWEntry const &unicodeEntry)
655 {
656 MWAWGraphicListenerPtr listener=getGraphicListener();
657 if (!listener) {
658 MWAW_DEBUG_MSG(("CorelPainterParser::sendText: can not find the listener\n"));
659 return false;
660 }
661
662 MWAWInputStreamPtr input = getInput();
663 if (!entry.valid() || !input->checkPosition(entry.end()) || entry.length()<140) {
664 MWAW_DEBUG_MSG(("CorelPainterParser::sendText: bad entry\n"));
665 return false;
666 }
667 libmwaw::DebugStream f;
668 f << "Entries(TextZone):";
669 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
670 int val=int(input->readLong(2));
671 if (val!=4) f << "f0=" << val << ",";
672 int dSz=int(input->readULong(2));
673 if (dSz<140 || dSz>entry.length()) {
674 MWAW_DEBUG_MSG(("CorelPainterParser::sendText: unexpected data size value\n"));
675 f << "###dSz=" << dSz << ",";
676 ascii().addPos(entry.begin());
677 ascii().addNote(f.str().c_str());
678 return false;
679 }
680 else if (dSz!=entry.length())
681 f << "header[size]=" << dSz << ",";
682 int sizes[2];
683 for (auto &s : sizes) s=int(input->readULong(2));
684 if (sizes[0]+sizes[1]+140>dSz) {
685 MWAW_DEBUG_MSG(("CorelPainterParser::sendText: unexpected size value\n"));
686 f << "###text[size]=" << sizes[0] << ",";
687 f << "###font[size]=" << sizes[1] << ",";
688 ascii().addPos(entry.begin());
689 ascii().addNote(f.str().c_str());
690 return false;
691 }
692 f << "text[size]=" << sizes[0] << ",";
693 f << "fontname[size]=" << sizes[1] << ",";
694 MWAWFont font;
695 MWAWParagraph para;
696 double value;
697 for (int i=0; i<2; ++i) { // value0=0.5?
698 if (!readDouble(value)) { // must not happen as we have check the input size
699 f << "###value" << i;
700 }
701 else {
702 if (i==0) font.setSize(float(value));
703 if (value<0||value>0) f << (i==0 ? "font[size]" : "value0") << "=" << value << ",";
704 }
705 }
706 for (int i=0; i<4; ++i) {
707 val=int(input->readULong(2));
708 int const expected[]= {0, 0, 0, 0};
709 if (val!=expected[i]) f << "f" << i+2 << "=" << std::hex << val << std::dec << ",";
710 }
711 double opacity=1;
712 for (int i=0; i<5; ++i) {
713 readDouble(value);
714 double const expectedValue[]= {0,1,1,0, 0};
715 if (value<expectedValue[i]||value>expectedValue[i]) {
716 char const *what[]= {"traking", "leading", "opacity", "blur", "direction[rad]"};
717 if (i==2) opacity=value;
718 f << what[i] << "=" << value << ",";
719 }
720 }
721 int dim[2]; // begin of first line position?
722 for (auto &d : dim) d=int(input->readLong(2));
723 f << "pos?=" << MWAWVec2i(dim[1],dim[0]) << ",";
724 readDouble(value);
725 if (value<1 || value>1) f << "value1=" << value << ",";
726 for (int i=0; i<11; ++i) { // g6=0|40, g9=0|100,
727 val=int(input->readULong(2));
728 int const expected[]= {0, 0, 0, 0, 4, 4,
729 0, 0, 0, 0, 1
730 };
731 if (i==9) {
732 uint32_t fontFlags=0;
733 switch ((val>>8)&3) {
734 default:
735 case 0:
736 break;
737 case 1:
738 f << "shadow,";
739 fontFlags |= MWAWFont::shadowBit;
740 break;
741 case 2:
742 f << "engraved,";
743 fontFlags |= MWAWFont::engraveBit;
744 break;
745 case 3:
746 f << "##shadow=3,";
747 break;
748 }
749 font.setFlags(fontFlags);
750 val &= 0xfcff;
751 }
752 if (val!=expected[i]) f << "g" << i << "=" << std::hex << val << std::dec << ",";
753 }
754 val=int(input->readULong(1));
755 switch (val) {
756 case 0: // left
757 break;
758 case 1:
759 para.m_justify = MWAWParagraph::JustificationCenter;
760 f << "align=center,";
761 break;
762 case 2:
763 para.m_justify = MWAWParagraph::JustificationRight;
764 f << "align=right,";
765 break;
766 default:
767 f << "###align=" << val << ",";
768 break;
769 }
770 input->seek(1, librevenge::RVNG_SEEK_CUR); // unused
771 ascii().addPos(entry.begin());
772 ascii().addNote(f.str().c_str());
773
774 long begTextPos=input->tell();
775 input->seek(sizes[0], librevenge::RVNG_SEEK_CUR);
776
777 long pos=input->tell();
778 f.str("");
779 f << "TextZone-II:";
780 std::string fontName;
781 for (int i=0; i<sizes[1]; ++i) fontName+=char(input->readULong(1));
782 f << fontName << ",";
783 if (!fontName.empty()) font.setId(getFontConverter()->getId(fontName));
784 val=int(input->readULong(2));
785 if (val) f << "f0=" << val << ",";
786 MWAWColor color(uint32_t(input->readULong(4))>>8);
787 if (!color.isBlack()) f << "col=" << color << ",";
788 if (opacity>=0 && opacity<1) {
789 uint32_t opValue=uint32_t(opacity*255);
790 font.setColor(MWAWColor((color.value()&0xffffff)|(opValue<<24)));
791 }
792 else
793 font.setColor(color);
794 MWAWColor shadowColor(uint32_t(input->readULong(4)));
795 if (!shadowColor.isBlack()) f << "col[shadow]=" << shadowColor << ",";
796 for (int i=0; i<5; ++i) { // f2=0|1, f3=1
797 val=int(input->readLong(2));
798 if (!val) continue;
799 if (i==0)
800 f << "curve[type]=" << val << ","; // useme
801 else
802 f << "f" << i+1 << "=" << val << ",";
803 }
804 for (int i=0; i<6; ++i) {
805 readDouble(value);
806 double const expected[]= {0, 70, -70, 130, -70, 200};
807 if (value<expected[i]||value>expected[i]) {
808 if (i==0)
809 f << "centering=" << value << ",";
810 else
811 f << "val" << i << "=" << value << ",";
812 }
813 }
814 for (int i=0; i<6; ++i) { // 0
815 val=int(input->readLong(2));
816 if (val==0) continue;
817 if (i==4)
818 f << "composite[method]=" << val << ",";
819 else
820 f << "g" << i << "=" << val << ",";
821 }
822 float cDim[2];
823 for (auto &d : cDim) {
824 readDouble(value);
825 d=float(value);
826 }
827 MWAWVec2f corner(cDim[0],cDim[1]); // checkme
828 if (corner!=MWAWVec2f(4,4)) f << "corner?=" << corner << ",";
829
830 if (input->tell()!=entry.end() && input->tell()+1!=entry.end()) {
831 MWAW_DEBUG_MSG(("CorelPainterParser::sendText: find extra data\n"));
832 f << "###extra,";
833 if (input->tell()!=pos)
834 ascii().addDelimiter(input->tell(),'|');
835 }
836 ascii().addPos(pos);
837 ascii().addNote(f.str().c_str());
838
839 // time to send the data
840 input->seek(begTextPos, librevenge::RVNG_SEEK_SET);
841 listener->setFont(font);
842 listener->setParagraph(para);
843 f.str("");
844 f << "TextZone[txt]:";
845 if (unicodeEntry.valid()) {
846 for (int i=0; i<sizes[0]; ++i) {
847 auto c=char(input->readULong(1));
848 if (c==0)
849 f << "#[0]";
850 else
851 f << c;
852 }
853 ascii().addPos(begTextPos);
854 ascii().addNote(f.str().c_str());
855
856 f.str("");
857 f << "Rsrc[utxt]:";
858 input->seek(unicodeEntry.begin(), librevenge::RVNG_SEEK_SET);
859 while (input->tell() < unicodeEntry.end()) {
860 auto c=static_cast<unsigned char>(input->readULong(1));
861 if (c&0x80) {
862 // send one unicode character (to be sure to create consistant utf8 string)
863 unsigned char outbuf[9];
864 int i=0;
865 while ((c&0x40) && i<7 && input->tell() < unicodeEntry.end()) {
866 outbuf[i++] = c;
867 f << "#[" << std::hex << int(c) << std::dec << "]";
868 c=static_cast<unsigned char>(input->readULong(1));
869 }
870 f << "#[" << std::hex << int(c) << std::dec << "]";
871 outbuf[i++] = c;
872 outbuf[i++] = 0;
873 librevenge::RVNGString unicodeString;
874 unicodeString.append(reinterpret_cast<char const *>(outbuf));
875 listener->insertUnicodeString(unicodeString);
876 continue;
877 }
878 if (c==0) break;
879 f << c;
880 switch (c) {
881 case 9:
882 listener->insertTab();
883 break;
884 case 0xd:
885 listener->insertEOL();
886 break;
887 default:
888 listener->insertCharacter(c);
889 break;
890 }
891 }
892 ascii().addPos(unicodeEntry.begin());
893 ascii().addNote(f.str().c_str());
894 return true;
895 }
896 else {
897 for (int i=0; i<sizes[0]; ++i) {
898 auto c=char(input->readULong(1));
899 if (c==0) {
900 MWAW_DEBUG_MSG(("CorelPainterParser::sendText: find char 0\n"));
901 f << "#[0]";
902 continue;
903 }
904 f << c;
905 switch (c) {
906 case 9:
907 listener->insertTab();
908 break;
909 case 0xd:
910 listener->insertEOL();
911 break;
912 default:
913 listener->insertCharacter(static_cast<unsigned char>(c));
914 break;
915 }
916 }
917 }
918 ascii().addPos(begTextPos);
919 ascii().addNote(f.str().c_str());
920
921 return true;
922 }
923
924 ////////////////////////////////////////////////////////////
925 // read the header
926 //
927 // there exists also the movie files
928 // 00000003003c0030000000020000001400001680 (then list of colors)
929 // maybe 0 numFrame dimY dimX 0 2[some format?] 14: begin of data 1680: end of data?
930 ////////////////////////////////////////////////////////////
931 bool CorelPainterParser::checkHeader(MWAWHeader *header, bool strict)
932 {
933 *m_state = CorelPainterParserInternal::State();
934 MWAWInputStreamPtr input = getInput();
935 if (!input || !input->hasDataFork())
936 return false;
937
938 input->seek(0,librevenge::RVNG_SEEK_SET);
939 MWAWDocument::Type type=MWAWDocument::MWAW_T_CORELPAINTER;
940 int const vers=1;
941
942 CorelPainterParserInternal::ZoneHeader zone;
943 if (!readZoneHeader(zone) || !zone.isBitmap())
944 return false;
945 if (strict) {
946 auto const &flags=zone.m_flags[1];
947 auto const &numTree = zone.m_numTreeNodes;
948 bool uncompressed=flags&1;
949 if ((uncompressed && numTree!=0) || (!uncompressed && numTree==0)) return false;
950 }
951 m_state->m_pixelByInch=zone.m_pixelByInch;
952 setVersion(vers);
953 if (header)
954 header->reset(type, vers, MWAWDocument::MWAW_K_PAINT);
955 return true;
956 }
957
958 std::shared_ptr<CorelPainterParserInternal::Node> CorelPainterParser::readCompressionTree(long endPos, int numNodes)
959 {
960 MWAWInputStreamPtr input = getInput();
961 long pos=input->tell();
962 if (numNodes==0) return nullptr;
963 if (numNodes<=0 || endPos<pos+4*numNodes) {
964 MWAW_DEBUG_MSG(("CorelPainterParser::readCompressionTree: the number of nodes seems bad\n"));
965 return nullptr;
966 }
967 libmwaw::DebugStream f;
968 f << "Entries(Compression):";
969 std::vector<std::shared_ptr<CorelPainterParserInternal::Node> > nodesList;
970 nodesList.resize(size_t(numNodes));
971 nodesList[0]=std::make_shared<CorelPainterParserInternal::Node>();
972 for (size_t i=0; i<size_t(numNodes); ++i) {
973 auto &node=nodesList[i];
974 if (!node) {
975 MWAW_DEBUG_MSG(("CorelPainterParser::readCompressionTree: can not find node %d\n", int(i)));
976 return nullptr;
977 }
978 for (int c=0; c<2; ++c) {
979 int val=int(input->readULong(2));
980 if (val&0x8000) {
981 node->m_values[c]=(val&0xff);
982 f << std::hex << node->m_values[c] << std::dec;
983 }
984 else {
985 int id=(val/4);
986 if (id>=numNodes || nodesList[size_t(id)]) {
987 MWAW_DEBUG_MSG(("CorelPainterParser::readCompressionTree: problem with id=%d\n", int(id)));
988 return nullptr;
989 }
990 nodesList[size_t(id)]=node->m_childs[c]=std::make_shared<CorelPainterParserInternal::Node>();
991 f << "N" << id;
992 }
993 if (c==0) f << "-";
994 }
995 f << ",";
996 nodesList.push_back(node);
997 }
998 ascii().addPos(pos);
999 ascii().addNote(f.str().c_str());
1000 return nodesList[0];
1001 }
1002
1003 bool CorelPainterParser::readResourcesList(CorelPainterParserInternal::ZoneHeader &zone)
1004 {
1005 MWAWInputStreamPtr input = getInput();
1006 if (zone.m_rsrcDataPos<=0 || !input->checkPosition(zone.m_rsrcDataPos) ||
1007 zone.m_rsrcDataPos>=zone.m_nextPos)
1008 return false;
1009
1010 libmwaw::DebugStream f;
1011 input->seek(zone.m_rsrcDataPos, librevenge::RVNG_SEEK_SET);
1012 long const &endPos = zone.m_nextPos;
1013 while (input->tell()+4<=endPos && !input->isEnd()) {
1014 long pos=input->tell();
1015 int sz=int(input->readULong(4));
1016 if (sz==0) {
1017 ascii().addPos(pos);
1018 ascii().addNote("_");
1019 return true;
1020 }
1021 long endRsrcPos=pos+sz;
1022 if (sz<16 || endRsrcPos>endPos) {
1023 input->seek(pos, librevenge::RVNG_SEEK_SET);
1024 break;
1025 }
1026 f.str("");
1027 f << "Entries(Rsrc):";
1028 std::string type;
1029 for (int i=0; i<4; ++i) type+=char(input->readULong(1));
1030 f << type << ",";
1031 long dSizes[2];
1032 for (auto &d : dSizes) d=long(input->readULong(4));
1033 if (dSizes[0]<18 || dSizes[1]<0 || dSizes[0]+dSizes[1]>sz) {
1034 MWAW_DEBUG_MSG(("CorelPainterParser::readResourcesList: the sizes seems bad\n"));
1035 input->seek(pos, librevenge::RVNG_SEEK_SET);
1036 break;
1037 }
1038 MWAWEntry entry;
1039 entry.setType(type);
1040 entry.setBegin(pos+dSizes[0]);
1041 entry.setEnd(endRsrcPos);
1042 if (dSizes[0]>18) f << "header[sz]=" << dSizes[0] << ",";
1043 if (dSizes[1]>0) {
1044 if (zone.m_rsrcMap.find(type)!=zone.m_rsrcMap.end()) {
1045 f << "##duplicated,";
1046 MWAW_DEBUG_MSG(("CorelPainterParser::readResourcesList: a entry with the same names already exist\n"));
1047 }
1048 else
1049 zone.m_rsrcMap[type]=entry;
1050 f << "data[sz]=" << dSizes[1] << ",";
1051 }
1052
1053 int nameSz=int(input->readULong(2));
1054 if (18+nameSz>dSizes[0]) {
1055 MWAW_DEBUG_MSG(("CorelPainterParser::readResourcesList: the name size seems bad\n"));
1056 f << "###name[sz]=" << nameSz << ",";
1057 }
1058 else if (nameSz) {
1059 // PCOL => "Paper Color", FSKT => "Friskets", ANNO => "Annotations", NOTE => "Note Text"
1060 int pSz=int(input->readULong(1));
1061 if (pSz+1<=nameSz) {
1062 std::string text;
1063 for (int c=0; c<pSz; ++c) text+=char(input->readULong(1));
1064 f << text << ",";
1065 }
1066 }
1067
1068 if (input->tell()!=pos+dSizes[0] && input->tell()+1!=pos+dSizes[0]) {
1069 f << "##extra,";
1070 MWAW_DEBUG_MSG(("CorelPainterParser::readResourcesList: find extra header data\n"));
1071 ascii().addDelimiter(input->tell(),'|');
1072 }
1073 input->seek(pos+dSizes[0], librevenge::RVNG_SEEK_SET);
1074 if (entry.valid())
1075 readResource(entry);
1076 ascii().addPos(pos);
1077 ascii().addNote(f.str().c_str());
1078 input->seek(endRsrcPos, librevenge::RVNG_SEEK_SET);
1079 }
1080 if (input->tell()<endPos) {
1081 MWAW_DEBUG_MSG(("CorelPainterParser::readResourcesList: find extra data\n"));
1082 ascii().addPos(input->tell());
1083 ascii().addNote("Rsrc:##extra");
1084 }
1085
1086 return true;
1087 }
1088
1089 bool CorelPainterParser::readResource(MWAWEntry const &entry)
1090 {
1091 MWAWInputStreamPtr input = getInput();
1092 if (!entry.valid() || !input->checkPosition(entry.end())) {
1093 MWAW_DEBUG_MSG(("CorelPainterParser::readResource: bad entry\n"));
1094 return false;
1095 }
1096 if (entry.isParsed() || entry.type()=="TEXT" || entry.type()=="utxt") return true;
1097 entry.setParsed(true);
1098 libmwaw::DebugStream f;
1099 f << "Rsrc[" << entry.type() << "]:";
1100 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1101 // find also ANNO,GUID with sz=0
1102 int val;
1103 if (entry.length()==2 && entry.type()=="TXGL") { // text extra? v8
1104 val=int(input->readLong(2));
1105 if (val!=1) f << "f0=" << val << ",";
1106 }
1107 else if (entry.length()==4 && entry.type()=="PCOL") {
1108 MWAWColor col(uint32_t(input->readULong(4)));
1109 if (!col.isWhite()) f << "bgColor=" << col << ",";
1110 }
1111 else if (entry.length()==8 && entry.type()=="MOSA") {
1112 for (int i=0; i<4; ++i) { // f0=-2
1113 val=int(input->readLong(2));
1114 if (val) f << "f" << i << "=" << val << ",";
1115 }
1116 }
1117 else if (entry.length()==12 && entry.type()=="WRAP") {
1118 for (int i=0; i<6; ++i) { // 0
1119 val=int(input->readLong(2));
1120 if (val) f << "f" << i << "=" << val << ",";
1121 }
1122 }
1123 else if (entry.length()==14 && entry.type()=="RULR") {
1124 for (int i=0; i<7; ++i) { // f0=0|100, f1=2, f6=f0=0|100
1125 val=int(input->readLong(2));
1126 if (val) f << "f" << i << "=" << val << ",";
1127 }
1128 }
1129 else if (entry.length()>=8 && entry.type()=="FSKT") {
1130 std::string type;
1131 for (int i=0; i<4; ++i) type+=char(input->readULong(1));
1132 if (type!="FSKT") f << "type2=" << type << ",";
1133 val=int(input->readULong(4));
1134 if (val) f << "f0=" << val << ",";
1135 if (val==1 && entry.length()>=24) {
1136 val=int(input->readULong(4));
1137 if (val!=0xc) f << "f1=" << val << ",";
1138 long N=long(input->readULong(4));
1139 long hSize=long(input->readULong(4));
1140 if (hSize<24 || long((unsigned long)hSize+8*(unsigned long)N)<0 || hSize+8*N>entry.length()) {
1141 MWAW_DEBUG_MSG(("CorelPainterParser::readResource: unsure how to read a frisket zone\n"));
1142 f << "###,";
1143 }
1144 else {
1145 for (int i=0; i<2; ++i) { // fl0=0|4a80
1146 val=int(input->readULong(2));
1147 if (val) f << "fl" << i << "=" << std::hex << val << std::dec << ",";
1148 }
1149 if (hSize>24) {
1150 // fl0=0: no data
1151 // fl0=4a80: 000000000000ffff00000000000000040652656374203900 + 15 points, related to polygon?
1152 ascii().addDelimiter(input->tell(),'|');
1153 }
1154 input->seek(entry.begin()+hSize, librevenge::RVNG_SEEK_SET);
1155 if (N) {
1156 long pos=input->tell();
1157 libmwaw::DebugStream f2;
1158 f2 << "Rsrc[FSKT-pt]:";
1159 for (long i=0; i<N; ++i) {
1160 float dim[2];
1161 for (auto &d : dim) d=float(input->readLong(4))/float(65536);
1162 f2 << MWAWVec2f(dim[0],dim[1]) << ",";
1163 }
1164 ascii().addPos(pos);
1165 ascii().addNote(f2.str().c_str());
1166 }
1167 }
1168 }
1169 }
1170 else if (entry.length()==256 && entry.type()=="NOTE") {
1171 int noteSz=int(input->readULong(1));
1172 std::string note;
1173 for (int i=0; i<noteSz; ++i) note+=char(input->readULong(1));
1174 f << note << ",";
1175 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1176 }
1177 else if (entry.length()==544 && entry.type()=="IPAR") {
1178 for (int i=0; i<16; ++i) { // f2=0|10,f5=0|100,f14=1,f15=-1
1179 val=int(input->readLong(2));
1180 if (val) f << "f" << i << "=" << val << ",";
1181 }
1182 int sSz=int(input->readULong(1));
1183 if (sSz && sSz<=31) {
1184 std::string name;
1185 for (int i=0; i<sSz; ++i) name+=char(input->readULong(1));
1186 f << name << ",";
1187 }
1188 else if (sSz) {
1189 MWAW_DEBUG_MSG(("CorelPainterParser::readResource: can not read a name\n"));
1190 f << "##sSz=" << sSz << ",";
1191 }
1192 input->seek(entry.begin()+64, librevenge::RVNG_SEEK_SET);
1193 for (int i=0; i<240; ++i) { // 0
1194 val=int(input->readLong(2));
1195 if (val) f << "g" << i << "=" << val << ",";
1196 }
1197 }
1198 // v18
1199 else if (entry.length()==6 && entry.type()=="CSPR") {
1200 for (int i=0; i<3; ++i) { // f1=3f00
1201 val=int(input->readULong(2));
1202 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
1203 }
1204 }
1205 else if (entry.length()==2 && entry.type()=="CSTI") { // 0
1206 val=int(input->readLong(2));
1207 if (val) f << "f0=" << val << ",";
1208 }
1209 #ifdef DEBUG_WITH_FILES
1210 // v10
1211 else if (entry.length()>8 && entry.type()=="RETP") { // Painter-X small preview bitmap (one by file)
1212 int dim[4];
1213 for (auto &d : dim) d=int(input->readULong(2));
1214 f << "dim=" << MWAWVec2i(dim[1],dim[0]) << "x" << MWAWVec2i(dim[3],dim[2]) << ",";
1215 if (entry.length()!=8+4*long(dim[2])*long(dim[3])) {
1216 MWAW_DEBUG_MSG(("CorelPainterParser::readResource: bitmap size seem bad\n"));
1217 f << "##";
1218 ascii().addPos(entry.begin());
1219 ascii().addNote(f.str().c_str());
1220 return true;
1221 }
1222
1223 ascii().addPos(entry.begin());
1224 ascii().addNote(f.str().c_str());
1225
1226 MWAWPictBitmapColor bitmap(MWAWVec2i(dim[3],dim[2]), false);
1227 std::vector<MWAWColor> listColor;
1228 listColor.resize(size_t(dim[3]));
1229 for (int i=0; i<dim[2]; ++i) {
1230 long pos=input->tell();
1231 f.str("");
1232 f << "Rsrc[RETP-" << i << ":";
1233 if (pos+4*dim[2]>entry.end()) {
1234 MWAW_DEBUG_MSG(("CorelPainterParser::readResource: can not read some row\n"));
1235 f << "###";
1236 ascii().addPos(pos);
1237 ascii().addNote(f.str().c_str());
1238 return false;
1239 }
1240 for (size_t c=0; c<size_t(dim[3]); ++c) {
1241 unsigned char data[4];
1242 for (auto &d : data) d=static_cast<unsigned char>(input->readULong(1));
1243 listColor[c]=MWAWColor(data[1],data[2],data[3],data[0]);
1244 }
1245 bitmap.setRow(i, listColor.data());
1246 ascii().addPos(pos);
1247 ascii().addNote(f.str().c_str());
1248 }
1249 MWAWEmbeddedObject picture;
1250 if (bitmap.getBinary(picture) && !picture.m_dataList.empty() && !picture.m_dataList[0].empty()) {
1251 static int volatile pictName = 0;
1252 libmwaw::DebugStream f2;
1253 f2 << "Preview" << ++pictName << ".ppm";
1254 libmwaw::Debug::dumpFile(picture.m_dataList[0], f2.str().c_str());
1255 }
1256 return true;
1257 }
1258 else if (entry.type()=="PRFL" && entry.length()>0) {
1259 // look like a standard .icc file
1260 MWAW_DEBUG_MSG(("CorelPainterParser::readResouce: this file contains a color profile, unimplemented\n"));
1261 librevenge::RVNGBinaryData file;
1262 input->readDataBlock(entry.length(), file);
1263 libmwaw::Debug::dumpFile(file, "profile.icc");
1264 ascii().skipZone(entry.begin(), entry.end()-1);
1265 return true;
1266 }
1267 // v18
1268 else if (entry.type()=="TJPG" && entry.length()>0) {
1269 librevenge::RVNGBinaryData file;
1270 input->readDataBlock(entry.length(), file);
1271 static int volatile pictName = 0;
1272 libmwaw::DebugStream f2;
1273 f2 << "Pict" << ++pictName << ".jpg";
1274 libmwaw::Debug::dumpFile(file, f2.str().c_str());
1275 ascii().skipZone(entry.begin(), entry.end()-1);
1276 return true;
1277 }
1278 else if (entry.type()=="FSPG" && entry.length()>22) { // paper texture bitamp
1279 val=int(input->readLong(2));
1280 if (val!=2) f << "f0=" << val << ",";
1281 int dim[2];
1282 for (auto &d : dim) d=int(input->readULong(2));
1283 f << "dim=" << MWAWVec2i(dim[0], dim[1]) << ",";
1284 for (int i=0; i<8; ++i) { // f2=1[number of plane?], f8=22?
1285 val=int(input->readLong(2));
1286 if (val) f << "f" << i+1 << "=" << val << ",";
1287 }
1288 if (dim[0]<=0 || dim[1]<=0 || (entry.length()-22)/dim[0]!=dim[1]) {
1289 MWAW_DEBUG_MSG(("CorelPainterParser::readResouce[FSPG]: can not read the color paper\n"));
1290 f << "###";
1291 }
1292 else {
1293 MWAWPictBitmapIndexed bitmap(MWAWVec2i(dim[0],dim[1]));
1294 std::vector<MWAWColor> indexes;
1295 indexes.reserve(256);
1296 for (int i=0; i<=255; ++i) indexes.push_back(MWAWColor(static_cast<unsigned char>(i),static_cast<unsigned char>(i),static_cast<unsigned char>(i)));
1297 bitmap.setColors(indexes);
1298 bool ok=true;
1299 for (int r=0; r<dim[1]; ++r) {
1300 unsigned long numReads;
1301 uint8_t const *values=input->read(size_t(dim[0]), numReads);
1302 if (!values || numReads!=static_cast<unsigned long>(dim[0])) {
1303 MWAW_DEBUG_MSG(("CorelPainterParser::readResouce[FSPG]: can not read row %d\n", r));
1304 f << "###";
1305 ok=false;
1306 break;
1307 }
1308 bitmap.setRow(r, reinterpret_cast<unsigned char const *>(values));
1309 }
1310 MWAWEmbeddedObject picture;
1311 if (ok && bitmap.getBinary(picture) && !picture.isEmpty() && !picture.m_dataList[0].empty()) {
1312 static int volatile pictName = 0;
1313 libmwaw::DebugStream f2;
1314 f2 << "PaperTexture" << ++pictName << ".png";
1315 libmwaw::Debug::dumpFile(picture.m_dataList[0], f2.str().c_str());
1316 ascii().skipZone(entry.begin(), entry.end()-1);
1317 return true;
1318 }
1319 }
1320 }
1321 // else if (entry.type()=="APSF" && entry.length()>=1096) some preferences' file? probably safe to ignore...
1322 #endif
1323 if (input->tell()!=entry.end()) {
1324 f << "#extra,";
1325 if (input->tell()!=entry.begin())
1326 ascii().addDelimiter(input->tell(),'|');
1327 }
1328 ascii().addPos(entry.begin());
1329 ascii().addNote(f.str().c_str());
1330 return true;
1331 }
1332
1333 bool CorelPainterParser::readZoneHeader(CorelPainterParserInternal::ZoneHeader &zone)
1334 {
1335 MWAWInputStreamPtr input = getInput();
1336 libmwaw::DebugStream f;
1337 long pos=input->tell();
1338 if (!input->checkPosition(pos+64) || input->readULong(2)!=2) return false;
1339 f << "Entries(ZoneHeader):";
1340
1341 int headerSize=64;
1342 zone.m_isMainZone=pos==0;
1343 zone.m_flags[0]=int(input->readULong(2)); // 2000: in painter v6 file
1344 if (zone.m_flags[0]&0x2000) f << "extra[pict],";
1345 if (zone.m_flags[0]&0xdfff) f << "fl0=" << std::hex << (zone.m_flags[0]&0xdfff) << std::dec << ",";
1346 int dim[2];
1347 for (auto &d : dim) d=int(input->readLong(2));
1348 zone.m_dimension=MWAWVec2i(dim[1],dim[0]);
1349 if (dim[0] || dim[1]) f << "dim=" << zone.m_dimension << ",";
1350 zone.m_flags[1]=int(input->readULong(2)); // main 2, subzone 510(bitmap?)|4110
1351 if (zone.m_flags[1]&1) f << "uncompressed,";
1352 if (zone.m_flags[1]&2) {
1353 f << "has[order],";
1354 headerSize+=256;
1355 if (!input->checkPosition(pos+headerSize)) return false;
1356 }
1357 // zone.m_flags[1]&0x10 local shape?
1358 // (zone.m_flags[1]>>16): 0 main picture, 5: floater with IPAR&FSKT, 7: floater with IPAR, 41: shape?
1359 if (zone.m_flags[1]&0xfffc)
1360 f << "fl1=" << std::hex << (zone.m_flags[1]&0xfffc) << std::dec << ",";
1361 int val;
1362 for (int i=0; i<2; ++i) { // checkme: f2=num of ordering?
1363 val=int(input->readULong(2));
1364 int const expected[] = {7,0x100};
1365 if (val!=expected[i]) f << "f" << i+1 << "=" << val << ",";
1366 }
1367 auto &bitmapPos=zone.m_bitmapPos;
1368 bitmapPos=long(input->readULong(4));
1369 if (bitmapPos<headerSize || !input->checkPosition(pos+bitmapPos)) return false;
1370 bitmapPos+=pos;
1371 for (int i=0; i<4; ++i) {
1372 val=int(input->readLong(2));
1373 if (val) f << "f" << i+3 << "=" << val << ",";
1374 }
1375 val=int(input->readULong(4));
1376 if (val && pos+val!=bitmapPos) f << "bitmap[pos2]=" << val << ","; // use me
1377 zone.m_pixelByInch=int(input->readULong(2));
1378 if (zone.m_pixelByInch == 0xFFFF)
1379 f << "pixel[inch]=inherited,";
1380 else if (zone.m_pixelByInch)
1381 f << "pixel[inch]=" << zone.m_pixelByInch << ",";
1382 val=int(input->readULong(2));
1383 if (val) f << "f7=" << std::hex << val << std::dec << ",";
1384 auto &numTree=zone.m_numTreeNodes;
1385 numTree=int(input->readULong(2));
1386 if (numTree>=256 || pos+headerSize+4*numTree > bitmapPos) return false;
1387 for (auto &d : dim) d=int(input->readLong(2));
1388 zone.m_origin=MWAWVec2i(dim[1],dim[0]);
1389 if (dim[0] || dim[1])
1390 f << "orig=" << zone.m_origin << ",";
1391 long lVal=int(input->readULong(4));
1392 if (lVal==0x3fe66666)
1393 f << "main,";
1394 else if (lVal)
1395 f << "zone[type]=" << std::hex << lVal << std::dec << ",";
1396 for (int i=0; i<4; ++i) { // 0
1397 val=int(input->readLong(2));
1398 if (val) f << "g" << i << "=" << val << ",";
1399 }
1400 long prevPos=bitmapPos;
1401 for (int z=0; z<2; ++z) {
1402 long newPos=long(input->readULong(4));
1403 if (newPos<=0) continue;
1404 newPos+=pos;
1405 if (input->checkPosition(newPos) && newPos>=prevPos) {
1406 if (z==0)
1407 zone.m_rsrcDataPos=newPos;
1408 else
1409 zone.m_nextPos=newPos;
1410 prevPos=newPos;
1411 }
1412 else {
1413 MWAW_DEBUG_MSG(("CorelPainterParser::readZoneHeader: zone pos%d seems bad\n", z));
1414 f << "###";
1415 }
1416 f << (z==0 ? "rsrc" : "next") << "[pos]=" << std::hex << newPos << std::dec << ",";
1417 }
1418 if (zone.m_nextPos==0) zone.m_nextPos=input->size();
1419 for (int i=0; i<2; ++i) { // 0
1420 val=int(input->readLong(2));
1421 if (val) f << "g" << i+2 << "=" << val << ",";
1422 }
1423 ascii().addPos(pos);
1424 ascii().addNote(f.str().c_str());
1425
1426 if (zone.m_flags[1]&2) { // read the ordering
1427 pos=input->tell();
1428 f.str("");
1429 f << "Entries(Ordering):[";
1430 for (int i=0; i<256; ++i) {
1431 val=int(input->readULong(1));
1432 if (val!=i)
1433 f << val << ",";
1434 else
1435 f << "_,";
1436 }
1437 f << "],";
1438 ascii().addPos(pos);
1439 ascii().addNote(f.str().c_str());
1440 }
1441
1442 if (numTree>0) {
1443 zone.m_tree=readCompressionTree(bitmapPos, numTree);
1444 if (!zone.m_tree) return false;
1445 }
1446 if (input->tell()<bitmapPos) {
1447 // before v10 flag&2000 => a zone of 40, v18 => a zone of 48
1448 long const extraSize=bitmapPos-input->tell();
1449 if ((zone.m_flags[0]&0x2000) && (extraSize==40 || extraSize==48)) {
1450 // 40 char, like 00070000000004640000000000000000000000000000000000000000000000000000000000000000
1451 // 48 char, like 00070000000004ec00000000000000000000000000000000000000000000000000000000000000000000000000000000
1452 ascii().addPos(input->tell());
1453 ascii().addNote("ZoneHeader[pict,extra]:");
1454 }
1455 else {
1456 MWAW_DEBUG_MSG(("CorelPainterParser::readZoneHeader: find extra data\n"));
1457 ascii().addPos(input->tell());
1458 ascii().addNote("ZoneHeader:###extra");
1459 }
1460 }
1461 return true;
1462 }
1463
1464 bool CorelPainterParser::readPolygon(long endPos, MWAWGraphicShape &shape, MWAWGraphicStyle &style)
1465 {
1466 auto input=getInput();
1467 MWAWGraphicStyle insideStyle;
1468 for (int st=0; st<2; ++st) {
1469 long pos=input->tell();
1470 if (pos+0x6c>endPos)
1471 return false;
1472 int dSz=int(input->readULong(2));
1473 if (dSz!=0x6c)
1474 return false;
1475 libmwaw::DebugStream f;
1476 f << "Entries(Polygon):";
1477 MWAWGraphicStyle &styl=st==0 ? style : insideStyle;
1478 int val;
1479 for (int i=0; i<2; ++i) {
1480 val=int(input->readLong(2));
1481 if (val!=2-2*i) f << "f" << i << "=" << val << ",";
1482 }
1483 int flags=int(input->readULong(2));
1484 if (flags&1) f << "has[insidePoly],";
1485 if (flags&0xfffe) f << "fl=" << std::hex << (flags&0xfffe) << std::dec << ",";
1486 for (int i=0; i<2; ++i) { // f2=2, f3=2
1487 val=int(input->readLong(2));
1488 if (val!=2-i) f << "f" << i+2 << "=" << val << ",";
1489 }
1490 for (int i=0; i<2; ++i) { // fl0=1, fl1=0|1
1491 val=int(input->readLong(1));
1492 if (val!=1-i) f << "fl" << i << "=" << val << ",";
1493 }
1494 int dim[4];
1495 for (auto &d : dim) d=int(input->readLong(2));
1496 f << "box=" << MWAWBox2i(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2])) << ",";
1497 val=int(input->readLong(2)); // 0
1498 if (val) f << "f4=" << val << ",";
1499 int flags2=int(input->readULong(2));
1500 if (flags2&1) f << "line,";
1501 if (flags2&2) f << "has[surface],";
1502 if (flags2&4) f << "has[stroke],";
1503 if ((flags2&8)==0) {
1504 styl.m_fillRuleEvenOdd=true;
1505 f << "fill[evenOdd],";
1506 }
1507 if (flags2&0xfff0) f << "flags=" << std::hex << (flags2&0xfff0) << std::dec << ",";
1508 std::string name;
1509 int pSz=int(input->readULong(1));
1510 if (pSz>32) {
1511 MWAW_DEBUG_MSG(("CorelPainterParser::readPolygon: can not read a name\n"));
1512 f << "##pSz=" << pSz << ",";
1513 pSz=0;
1514 }
1515 for (int i=0; i<pSz; ++i) {
1516 char c=char(input->readULong(1));
1517 if (!c) break;
1518 name+=c;
1519 }
1520 f << "name=" << name << ","; // shape name or corresponding letter
1521 input->seek(pos+64, librevenge::RVNG_SEEK_SET);
1522 int type=int(input->readLong(2));
1523 switch (type) {
1524 case 100:
1525 f << "spline,";
1526 break;
1527 case 101:
1528 f << "rect,";
1529 break;
1530 case 102:
1531 f << "oval,";
1532 break;
1533 default:
1534 MWAW_DEBUG_MSG(("CorelPainterParser::readPolygon: find unknown type=%d\n",type));
1535 f << "###type=" << type << ",";
1536 break;
1537 }
1538 val=int(input->readULong(2));
1539 switch ((val>>12)) {
1540 case 0: // butt
1541 break;
1542 case 1:
1543 styl.m_lineCap=MWAWGraphicStyle::C_Round;
1544 f << "line[cap]=round,";
1545 break;
1546 case 2:
1547 styl.m_lineCap=MWAWGraphicStyle::C_Square;
1548 f << "line[cap]=square,";
1549 break;
1550 default:
1551 f << "#line[cap]=" << (val>>12) << ",";
1552 break;
1553 }
1554 switch ((val>>8)&0xf) {
1555 case 0: // miter
1556 break;
1557 case 1:
1558 styl.m_lineJoin=MWAWGraphicStyle::J_Round;
1559 f << "line[join]=round,";
1560 break;
1561 case 2:
1562 styl.m_lineJoin=MWAWGraphicStyle::J_Bevel;
1563 f << "line[join]=bevel,";
1564 break;
1565 default:
1566 f << "#line[join]=" << ((val>>8)&0xf) << ",";
1567 break;
1568 }
1569 if (val&0xff)
1570 f << "f5=" << (val&0xff) << ",";
1571 MWAWColor colors[2];
1572 for (int i=0; i<2; ++i) {
1573 colors[i]=MWAWColor(uint32_t(input->readULong(4)));
1574 if ((i==0 && !colors[0].isWhite()) || (i==1 && !colors[1].isBlack()))
1575 f << "col[" << (i==0 ? "surface" : "stroke") << "]=" << colors[i] << ",";
1576 }
1577 val=int(input->readULong(4));
1578 styl.m_lineWidth=float(val)/float(65536);
1579 if (val!=0x30000) f << "stroke[w]=" << styl.m_lineWidth << ",";
1580 val=int(input->readULong(4));
1581 if (val!=0x70000) f << "mitter[limit]=" << float(val)/float(65536) << ",";
1582 val=int(input->readLong(2)); // 1 ou 40
1583 if (val!=40) f << "flatness=" << val << ",";
1584 int N=int(input->readULong(2));
1585 f << "N=" << N << ",";
1586 float fDim[4];
1587 for (auto &d : fDim) d=float(input->readLong(4))/65536.f;
1588 MWAWBox2f box(MWAWVec2f(fDim[0],fDim[1]),MWAWVec2f(fDim[2],fDim[3]));
1589 f << "box[float]=" << box << ",";
1590 float opacity[2];
1591 for (int i=0; i<2; ++i) {
1592 opacity[i]=float(input->readULong(2))/float(65535);
1593 if (opacity[i]<1.f)
1594 f << "opacity[" << (i==0 ? "surface" : "stroke") << "=" << opacity[i] << ",";
1595 }
1596 if (flags2&2)
1597 styl.setSurfaceColor(colors[0],opacity[0]);
1598 if (flags2&4) {
1599 styl.m_lineColor=colors[1];
1600 styl.m_lineOpacity=opacity[1];
1601 }
1602 else
1603 styl.m_lineWidth=0;
1604 if (pos+dSz+40*(N+1)+2<pos+dSz+2 || pos+dSz+40*(N+1)+2>endPos) {
1605 MWAW_DEBUG_MSG(("CorelPainterParser::readPolygon: the number of point seems bad\n"));
1606 f << "###N";
1607 ascii().addPos(pos);
1608 ascii().addNote(f.str().c_str());
1609 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
1610 return false;
1611 }
1612 ascii().addPos(pos);
1613 ascii().addNote(f.str().c_str());
1614 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
1615 if (st==0) shape=MWAWGraphicShape::path(box);
1616
1617 // checkme: this must work for simple shape, for most complex
1618 // shape, we need to understand what ptype codes
1619 std::vector<MWAWVec2f> vertices;
1620 vertices.reserve(3*size_t(N+1));
1621 for (int i=0; i<=N; ++i) {
1622 pos=input->tell();
1623 f.str("");
1624 f << "Polygon:";
1625 int pType=int(input->readULong(2)); // find 0|1|11|a0
1626 if (pType==0) // end of poly
1627 f << "_,";
1628 else if (pType==1 || pType==0x11) // 1 normal, 11: pt0=pt1
1629 f << "point,";
1630 else // a0: line
1631 f << "point" << std::hex << pType << std::dec << ",";
1632 if (pType || i!=N) {
1633 val=int(input->readLong(2)); // 0
1634 if (val) f << "f0=" << val << ",";
1635 f << "pts=[";
1636 for (int pt=0; pt<3; ++pt) {
1637 float fPos[2];
1638 for (auto &d : fPos) d=float(input->readLong(4))/65536.f;
1639 if (pType==0 && pt==0 && (fPos[0]<fDim[0] || fPos[0]>fDim[2] || fPos[1]<fDim[1] || fPos[1]>fDim[3]))
1640 break;
1641 vertices.push_back(MWAWVec2f(fPos[0],fPos[1]));
1642 f << vertices.back() << ",";
1643 }
1644 f << "],";
1645 // then junk?
1646 }
1647 if ((i==N || pType==0) && !vertices.empty()) {
1648 // TODO use point type to recreate the path
1649 shape.m_path.push_back(MWAWGraphicShape::PathData('M', vertices[0]));
1650 size_t numPts=vertices.size()/3;
1651 for (size_t pt=1; pt<numPts; ++pt) {
1652 if (vertices[3*pt-3]==vertices[3*pt-2] && vertices[3*pt-1]==vertices[3*pt])
1653 shape.m_path.push_back(MWAWGraphicShape::PathData('L', vertices[3*pt]));
1654 else
1655 shape.m_path.push_back(MWAWGraphicShape::PathData('C', vertices[3*pt], vertices[3*pt-2], vertices[3*pt-1]));
1656 }
1657 if (i<=1 && (numPts==1 || (numPts==2 && vertices[0]==vertices[3]))) // line special case
1658 shape.m_path.push_back(MWAWGraphicShape::PathData('L', vertices[2]));
1659 else if (numPts>2 && vertices[0]==vertices[3*numPts-3])
1660 shape.m_path.push_back(MWAWGraphicShape::PathData('Z'));
1661 vertices.clear();
1662 }
1663
1664 ascii().addDelimiter(input->tell(),'|');
1665 input->seek(pos+40, librevenge::RVNG_SEEK_SET);
1666 ascii().addPos(pos);
1667 ascii().addNote(f.str().c_str());
1668 }
1669 pos=input->tell();
1670 if (pos+2>endPos) {
1671 MWAW_DEBUG_MSG(("CorelPainterParser::readPolygon: can not find the end marker\n"));
1672 return false;
1673 }
1674 f.str("");
1675 f << "Polygon[end]:";
1676 val=int(input->readULong(2));
1677 if (val!=0) f << "f0=" << val << ",";
1678 ascii().addPos(pos);
1679 ascii().addNote(f.str().c_str());
1680 if (st==0 && (flags&1)==0)
1681 break;
1682 }
1683
1684 return true;
1685 }
1686 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: