Debian Codegraph @

src/lib/ApplePictParser.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 <algorithm>
35 #include <cmath>
36 #include <iomanip>
37 #include <iostream>
38 #include <limits>
39 #include <sstream>
40 #include <utility>
41
42 #include <librevenge/librevenge.h>
43
44 #include "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWGraphicListener.hxx"
47 #include "MWAWGraphicShape.hxx"
48 #include "MWAWGraphicStyle.hxx"
49 #include "MWAWHeader.hxx"
50 #include "MWAWParagraph.hxx"
51 #include "MWAWPictBitmap.hxx"
52 #include "MWAWPictData.hxx"
53 #include "MWAWPrinter.hxx"
54 #include "MWAWPosition.hxx"
55 #include "MWAWSubDocument.hxx"
56
57 #include "ApplePictParser.hxx"
58
59 /** Internal: the structures of a ApplePictParser */
60 namespace ApplePictParserInternal
61 {
62
63 //! internal: low level class to store a region
64 struct Region {
65 //! constructor
66 Region()
67 : m_bdBox()
68 , m_points()
69 , m_extra("")
70 {
71 }
72 //! operator<<
73 friend std::ostream &operator<<(std::ostream &o, Region const &rgn)
74 {
75 o << rgn.m_bdBox << ",";
76 if (!rgn.m_points.empty()) {
77 o << "points=[";
78 for (auto const &pt : rgn.m_points)
79 o << pt << ",";
80 o << "],";
81 }
82 o << rgn.m_extra;
83 return o;
84 }
85 //! the bdbox
86 MWAWBox2i m_bdBox;
87 //! the set of points which defines the mask
88 std::vector<MWAWVec2i> m_points;
89 //! extra data
90 std::string m_extra;
91 };
92
93 //! Internal and low level: a class used to read pack/unpack black-white bitmap
94 struct Bitmap {
95 Bitmap()
96 : m_rowBytes()
97 , m_rect()
98 , m_src()
99 , m_dst()
100 , m_region()
101 , m_bitmap()
102 , m_mode(0) {}
103
104 //! operator<< for Bitmap
105 friend std::ostream &operator<< (std::ostream &o, Bitmap const &f)
106 {
107 o << "rDim=" << f.m_rowBytes << ", " << f.m_rect << ", " << f.m_src << ", " << f.m_dst;
108 if (f.m_region.get()) o << ", " << *f.m_region;
109 return o;
110 }
111
112 //! creates the bitmap from the packdata
113 bool unpackedData(unsigned char const *pData, int sz)
114 {
115 int rPos = 0;
116 size_t wPos = m_bitmap.size(), wNPos = wPos+size_t(m_rowBytes);
117 m_bitmap.resize(size_t(wNPos));
118
119 while (rPos < sz) {
120 if (rPos+2 > sz) return false;
121 auto n = static_cast<signed char>(pData[rPos++]);
122 if (n < 0) {
123 int nCount = 1-n;
124 if (wPos+size_t(nCount) > wNPos) return false;
125
126 unsigned char val = pData[rPos++];
127 for (int i = 0; i < nCount; i++)
128 m_bitmap[wPos++] = val;
129 continue;
130 }
131 int nCount = 1+n;
132 if (rPos+nCount > sz || wPos+size_t(nCount) > wNPos) return false;
133 for (int i = 0; i < nCount; i++)
134 m_bitmap[wPos++] = pData[rPos++];
135 }
136 return (wPos == wNPos);
137 }
138
139 //! parses the bitmap data zone
140 bool readBitmapData(MWAWInputStream &input, bool packed)
141 {
142 int numRows = m_rect.size().y(), szRowSize=1;
143
144 if (packed) {
145 // CHECKME: the limit(1/2 bytes) is probably 251: the value for a Pict2.0
146 // from collected data files, we have 246 < limit < 254
147 if (m_rowBytes > 250) szRowSize = 2;
148 }
149 else
150 m_bitmap.resize(size_t(numRows*m_rowBytes));
151
152 size_t pos=0;
153 for (int i = 0; i < numRows; i++) {
154 if (input.isEnd()) break;
155
156 if (!packed) {
157 unsigned long numR = 0;
158 unsigned char const *data = input.read(size_t(m_rowBytes), numR);
159 if (!data || int(numR) != m_rowBytes) {
160 MWAW_DEBUG_MSG(("ApplePictParserInternal::Bitmap::readBitmapData: can not read line %d/%d (%d chars)\n", i, numRows, m_rowBytes));
161 return false;
162 }
163 for (int j = 0; j < m_rowBytes; j++)
164 m_bitmap[pos++]=data[j];
165 }
166 else {
167 auto numB = static_cast<int>(input.readULong(szRowSize));
168 if (numB < 0 || numB > 2*m_rowBytes) {
169 MWAW_DEBUG_MSG(("ApplePictParserInternal::Bitmap::readBitmapData: odd numB:%d in row: %d/%d\n", numB, i, numRows));
170 return false;
171 }
172 unsigned long numR = 0;
173 unsigned char const *data = input.read(size_t(numB), numR);
174 if (!data || int(numR) != numB) {
175 MWAW_DEBUG_MSG(("ApplePictParserInternal::Bitmap::readBitmapData: can not read line %d/%d (%d chars)\n", i, numRows, numB));
176 return false;
177 }
178 if (!unpackedData(data,numB)) {
179 MWAW_DEBUG_MSG(("ApplePictParserInternal::Bitmap::readBitmapData: can not unpacked line:%d\n", i));
180 return false;
181 }
182 }
183 }
184 return true;
185 }
186 //! returns the bitmap and the type
187 bool get(MWAWEmbeddedObject &picture) const
188 {
189 if (m_rowBytes <= 0) return false;
190 int nRows = int(m_bitmap.size())/m_rowBytes;
191 MWAWPictBitmapBW bitmap(MWAWVec2i(m_rect.size().x(),nRows));
192 if (!bitmap.valid()) return false;
193
194 for (int i = 0; i < nRows; i++)
195 bitmap.setRowPacked(i, &m_bitmap[size_t(i*m_rowBytes)], &m_bitmap[0] + m_bitmap.size());
196
197 return bitmap.getBinary(picture);
198 }
199
200 //! the num of bytes used to store a row
201 int m_rowBytes;
202 MWAWBox2i m_rect /** the bitmap rectangle */, m_src/** the initial dimension */, /** the final dimension */ m_dst ;
203 //! the region
204 std::shared_ptr<Region> m_region;
205 //! the bitmap
206 std::vector<unsigned char> m_bitmap;
207 //! the encoding mode ?
208 int m_mode;
209 };
210
211 //! Internal and low level: a class used to read a color map in a Apple Pict
212 struct ColorTable {
213 //! constructor
214 ColorTable()
215 : m_flags(0)
216 , m_colors() {}
217
218 //! tries to read a colortable
219 bool read(MWAWInputStream &input)
220 {
221 long actPos = input.tell();
222 input.seek(4, librevenge::RVNG_SEEK_CUR); // ignore seed
223 m_flags = static_cast<int>(input.readULong(2));
224 int n = static_cast<int>(input.readLong(2))+1;
225 if (n < 0 || !input.checkPosition(actPos+8+8*n)) return false;
226 m_colors.resize(size_t(n));
227 for (size_t i = 0; i < size_t(n); i++) {
228 input.readULong(2); // indexId: ignored
229 unsigned char col[3];
230 for (unsigned char &c : col) {
231 c = static_cast<unsigned char>(input.readULong(1));
232 input.readULong(1);
233 }
234 m_colors[i] = MWAWColor(col[0], col[1], col[2]);
235 }
236 return long(input.tell()) == actPos + 8+ 8*n;
237 }
238
239 //! operator<< for ColorTable
240 friend std::ostream &operator<< (std::ostream &o, ColorTable const &f)
241 {
242 size_t numColor = f.m_colors.size();
243 o << "color";
244 if (f.m_flags) o << "(" << std::hex << f.m_flags << ")";
245 o << "={" << std::dec;
246 for (size_t i = 0; i < numColor; i++)
247 o << "col" << i << "=" << f.m_colors[i] << ",";
248 o << "}";
249 return o;
250 }
251
252 //! the color table flags
253 int m_flags;
254
255 //! the list of colors
256 std::vector<MWAWColor> m_colors;
257 };
258
259 //! Internal and low level: a class used to read pack/unpack color pixmap (version 2)
260 struct Pixmap {
261 Pixmap()
262 : m_rowBytes(0)
263 , m_rect()
264 , m_version(-1)
265 , m_packType(0)
266 , m_packSize(0)
267 , m_pixelType(0)
268 , m_pixelSize(0)
269 , m_compCount(0)
270 , m_compSize(0)
271 , m_planeBytes(0)
272 , m_colorTable()
273 , m_src()
274 , m_dst()
275 , m_region()
276 , m_indices()
277 , m_colors()
278 , m_mode(0)
279 {
280 m_resolution[0] = m_resolution[1] = 0;
281 }
282
283 //! operator<< for Pixmap
284 friend std::ostream &operator<< (std::ostream &o, Pixmap const &f)
285 {
286 o << "rDim=" << f.m_rowBytes << ", " << f.m_rect << ", " << f.m_src << ", " << f.m_dst;
287 o << ", resol=" << f.m_resolution[0] << "x" << f.m_resolution[1];
288 if (f.m_colorTable.get()) o << ", " << *f.m_colorTable;
289 if (f.m_region.get()) o << ", " << *f.m_region;
290 return o;
291 }
292
293 //! creates the pixmap from the packdata
294 bool unpackedData(unsigned char const *pData, int sz, int byteSz, int nSize, std::vector<unsigned char> &res) const
295 {
296 if (byteSz<1||byteSz>4) {
297 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::unpackedData: unknown byteSz\n"));
298 return false;
299 }
300 int rPos = 0, wPos = 0, maxW = m_rowBytes+24;
301 while (rPos < sz) {
302 if (rPos+2 > sz) return false;
303 auto n = static_cast<signed char>(pData[rPos++]);
304 if (n < 0) {
305 int nCount = 1-n;
306 if (rPos+byteSz > sz || wPos+byteSz *nCount >= maxW) return false;
307
308 unsigned char val[4];
309 for (int b = 0; b < byteSz; b++) val[b] = pData[rPos++];
310 for (int i = 0; i < nCount; i++) {
311 if (wPos+byteSz >= maxW) break;
312 for (int b = 0; b < byteSz; b++)res[size_t(wPos++)] = val[b];
313 }
314 continue;
315 }
316 int nCount = 1+n;
317 if (rPos+byteSz *nCount > sz || wPos+byteSz *nCount >= maxW) return false;
318 for (int i = 0; i < nCount; i++) {
319 if (wPos+byteSz >= maxW) break;
320 for (int b = 0; b < byteSz; b++) res[size_t(wPos++)] = pData[rPos++];
321 }
322 }
323 return wPos+8 >= nSize;
324 }
325
326 MWAWColor extractColor(const std::vector<unsigned char> &data, size_t rIdx, size_t gIdx, size_t bIdx)
327 {
328 unsigned char r = rIdx < data.size() ? data[rIdx] : 0;
329 unsigned char g = gIdx < data.size() ? data[gIdx] : 0;
330 unsigned char b = bIdx < data.size() ? data[bIdx] : 0;
331 return MWAWColor(r, g, b);
332 }
333 MWAWColor extractColor(const std::vector<unsigned char> &data, size_t aIdx, size_t rIdx, size_t gIdx, size_t bIdx)
334 {
335 unsigned char r = rIdx < data.size() ? data[rIdx] : 0;
336 unsigned char g = gIdx < data.size() ? data[gIdx] : 0;
337 unsigned char b = bIdx < data.size() ? data[bIdx] : 0;
338 unsigned char alpha = aIdx < data.size() ? data[aIdx] : 0;
339 return MWAWColor(r, g, b, (unsigned char)(255-alpha));
340 }
341
342 //! computes the height based on available data
343 int computeHeight(MWAWInputStream &input, const int height, const int width, const bool packed, const int szRowSize) const
344 {
345 if (packed) {
346 const long pos = input.tell();
347 int h = 0;
348 for (; h < height && !input.isEnd(); ++h) {
349 auto len = long(input.readULong(szRowSize));
350 input.seek(len, librevenge::RVNG_SEEK_CUR);
351 }
352 input.seek(pos, librevenge::RVNG_SEEK_SET);
353 return h;
354 }
355 else if (width<=0) {
356 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::computeHeight: width is bad\n"));
357 return 0;
358 }
359 else {
360 const long remaining = input.size() - input.tell();
361 const long maxHeight = remaining / width + int(remaining % width > 1);
362 return std::min(height, int(maxHeight));
363 }
364 }
365
366 //! parses the pixmap data zone
367 bool readPixmapData(MWAWInputStream &input)
368 {
369 int W = m_rect.size().x();
370
371 int szRowSize=1;
372 if (m_rowBytes > 250) szRowSize = 2;
373
374 bool packed = !(m_rowBytes < 8 || m_packType == 1);
375 int H = computeHeight(input, m_rect.size().y(), W, packed, szRowSize);
376
377 int nPlanes = 1, nBytes = 3, rowBytes = m_rowBytes;
378 int numValuesByInt = 1;
379 int numColors = m_colorTable.get() ? int(m_colorTable->m_colors.size()) : 0;
380 int maxColorsIndex = -1;
381
382 switch (m_pixelSize) {
383 case 1:
384 case 2:
385 case 4:
386 case 8: { // indices (associated to a color map)
387 nBytes = 1;
388 numValuesByInt = 8/m_pixelSize;
389 int numValues = (W+numValuesByInt-1)/numValuesByInt;
390 if (m_rowBytes < numValues || m_rowBytes > numValues+10) {
391 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::readPixmapData invalid number of rowsize : %d, pixelSize=%d, W=%d\n", m_rowBytes, m_pixelSize, W));
392 return false;
393 }
394 if (numColors == 0) {
395 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::readPixmapData: no color table \n"));
396 return false;
397 }
398 break;
399 }
400
401 case 16:
402 nBytes = 2;
403 break;
404 case 32:
405 if (!packed) {
406 nBytes=4;
407 break;
408 }
409 if (m_packType == 2) {
410 packed = false;
411 break;
412 }
413 if (m_compCount != 3 && m_compCount != 4) {
414 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::readPixmapData: do not known how to read cmpCount=%d\n", m_compCount));
415 return false;
416 }
417 nPlanes=m_compCount;
418 nBytes=1;
419 if (nPlanes == 3) rowBytes = (3*rowBytes)/4;
420 break;
421 default:
422 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::readPixmapData: do not known how to read pixelsize=%d \n", m_pixelSize));
423 return false;
424 }
425 const size_t dataSize = size_t(H) * size_t(W);
426 if (m_pixelSize <= 8)
427 m_indices.resize(dataSize);
428 else {
429 if (rowBytes != W * nBytes * nPlanes) {
430 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::readPixmapData: find W=%d pixelsize=%d, rowSize=%d\n", W, m_pixelSize, m_rowBytes));
431 }
432 m_colors.resize(dataSize);
433 }
434
435 std::vector<unsigned char> values;
436 values.resize(size_t(m_rowBytes+24), static_cast<unsigned char>(0));
437
438 for (int y = 0; y < H; y++) {
439 if (!packed) {
440 unsigned long numR = 0;
441 unsigned char const *data = input.read(size_t(m_rowBytes), numR);
442 if (!data || int(numR) != m_rowBytes) {
443 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::readPixmapData: readColors can not read line %d/%d (%d chars)\n", y, H, m_rowBytes));
444 return false;
445 }
446 for (size_t j = 0; j < size_t(m_rowBytes); j++)
447 values[j]=data[j];
448 }
449 else { // ok, packed
450 auto numB = static_cast<int>(input.readULong(szRowSize));
451 if (numB < 0 || numB > 2*m_rowBytes) {
452 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::readPixmapData: odd numB:%d in row: %d/%d\n", numB, y, H));
453 return false;
454 }
455 unsigned long numR = 0;
456 unsigned char const *data = input.read(size_t(numB), numR);
457 if (!data || int(numR) != numB) {
458 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::readPixmapData: can not read line %d/%d (%d chars)\n", y, H, numB));
459 return false;
460 }
461 if (!unpackedData(data,numB, nBytes, rowBytes, values)) {
462 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::readPixmapData: can not unpacked line:%d\n", y));
463 return false;
464 }
465 }
466
467 //
468 // ok, we can add it in the pictures
469 //
470 int wPos = y*W;
471 if (m_pixelSize <= 8) { // indexed
472 int maxValues = (1 << m_pixelSize)-1;
473 for (int x = 0, rPos = 0; x < W;) {
474 unsigned char val = values[size_t(rPos++)];
475 for (int v = numValuesByInt-1; v >=0; v--) {
476 int index = (val>>(v*m_pixelSize))&maxValues;
477 if (index > maxColorsIndex) maxColorsIndex = index;
478 m_indices[size_t(wPos++)] = index;
479 if (++x >= W) break;
480 }
481 }
482 }
483 else if (m_pixelSize == 16) {
484 for (int x = 0, rPos = 0; x < W; x++) {
485 unsigned int c1 = size_t(rPos) < values.size() ? values[size_t(rPos)] : 0;
486 unsigned int c2 = size_t(rPos+1) < values.size() ? values[size_t(rPos+1)] : 0;
487 unsigned int val = 256*c1 + c2;
488 rPos+=2;
489 m_colors[size_t(wPos++)]=MWAWColor((val>>7)& 0xF8, (val>>2) & 0xF8, static_cast<unsigned char>(val << 3));
490 }
491 }
492 else if (nPlanes==1) {
493 for (int x = 0, rPos = 0; x < W; x++) {
494 if (nBytes==4) rPos++;
495 m_colors[size_t(wPos++)]=extractColor(values, size_t(rPos), size_t(rPos+1), size_t(rPos+2));
496 rPos+=3;
497 }
498 }
499 else if (nPlanes==3) {
500 for (int x = 0, rPos = 0; x < W; x++) {
501 m_colors[size_t(wPos++)]=extractColor(values, size_t(rPos), size_t(rPos+W), size_t(rPos+2*W));
502 rPos+=1;
503 }
504 }
505 else {
506 for (int x = 0, rPos = 0; x < W; x++) {
507 m_colors[size_t(wPos++)]=extractColor(values, size_t(rPos), size_t(rPos+W), size_t(rPos+2*W), size_t(rPos+3*W));
508 rPos+=1;
509 }
510 }
511 }
512 if (maxColorsIndex >= numColors) {
513 if (!m_colorTable) m_colorTable.reset(new ColorTable);
514 std::vector<MWAWColor> &cols = m_colorTable->m_colors;
515
516 // can be ok for a pixpat ; in this case:
517 // maxColorsIndex -> foregroundColor, numColors -> backGroundColor
518 // and intermediate index fills with intermediate colors
519 int numUnset = maxColorsIndex-numColors+1;
520
521 int decGray = (numUnset==1) ? 0 : 255/(numUnset-1);
522 for (int i = 0; i < numUnset; i++)
523 cols.push_back(MWAWColor(static_cast<unsigned char>(255-i*decGray), static_cast<unsigned char>(255-i*decGray), static_cast<unsigned char>(255-i*decGray)));
524 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::readPixmapData: find index=%d >= numColors=%d\n", maxColorsIndex, numColors));
525
526 return true;
527 }
528 return true;
529 }
530 //! returns the pixmap
531 bool get(MWAWEmbeddedObject &picture) const
532 {
533 int W = m_rect.size().x();
534 if (W <= 0) return false;
535 if (m_colorTable.get() && m_indices.size()) {
536 int nRows = int(m_indices.size())/W;
537 MWAWPictBitmapIndexed pixmap(MWAWVec2i(W,nRows));
538 if (!pixmap.valid()) return false;
539
540 pixmap.setColors(m_colorTable->m_colors);
541
542 size_t rPos = 0;
543 for (int i = 0; i < nRows; i++) {
544 for (int x = 0; x < W; x++)
545 pixmap.set(x, i, m_indices[rPos++]);
546 }
547
548 return pixmap.getBinary(picture);
549 }
550
551 if (m_colors.size()) {
552 int nRows = int(m_colors.size())/W;
553 MWAWPictBitmapColor pixmap(MWAWVec2i(W,nRows));
554 if (!pixmap.valid()) return false;
555
556 size_t rPos = 0;
557 for (int i = 0; i < nRows; i++) {
558 for (int x = 0; x < W; x++)
559 pixmap.set(x, i, m_colors[rPos++]);
560 }
561
562 return pixmap.getBinary(picture);
563 }
564
565 MWAW_DEBUG_MSG(("ApplePictParserInternal::Pixmap::get: can not find any indices or colors \n"));
566 return false;
567 }
568
569 //! the num of bytes used to store a row
570 int m_rowBytes;
571 MWAWBox2i m_rect /** the pixmap rectangle */;
572 int m_version /** the pixmap version */;
573 int m_packType /** the packing format */;
574 long m_packSize /** size of data in the packed state */;
575 int m_resolution[2] /** horizontal/vertical definition */;
576 int m_pixelType /** format of pixel image */;
577 int m_pixelSize /** physical bit by image */;
578 int m_compCount /** logical components per pixels */;
579 int m_compSize /** logical bits by components */;
580 long m_planeBytes /** offset to the next plane */;
581 std::shared_ptr<ColorTable> m_colorTable /** the color table */;
582
583 MWAWBox2i m_src/** the initial dimension */, /** another final dimension */ m_dst ;
584 //! the region
585 std::shared_ptr<Region> m_region;
586 //! the pixmap indices
587 std::vector<int> m_indices;
588 //! the colors
589 std::vector<MWAWColor> m_colors;
590 //! the encoding mode ?
591 int m_mode;
592 };
593
594 ////////////////////////////////////////
595 //! Internal: the state of a ApplePictParser
596 struct State {
597 //! constructor
598 State()
599 : m_version(0)
600 , m_bdBox()
601 , m_origin(0,0)
602 , m_penPosition(0,0)
603 , m_textPosition(0,0)
604 , m_penSize(1,1)
605 , m_ovalSize(1,1)
606 , m_penMode(0)
607 , m_textMode(0)
608 , m_isHiliteMode(false)
609 , m_font(3,12)
610 , m_foreColor(MWAWColor::black())
611 , m_backColor(MWAWColor::white())
612 , m_hiliteColor(MWAWColor::black())
613 , m_opColor(MWAWColor::black())
614 , m_penPattern()
615 , m_backgroundPattern()
616 , m_fillPattern()
617 , m_rectangle()
618 , m_roundRectangle()
619 , m_circle()
620 , m_pie()
621 , m_points()
622 , m_afterQuicktime(false)
623 {
624 }
625 //! init the patterns list
626 void initPatterns();
627 //! returns true if the shape is invisible
628 bool isInvisible(ApplePictParser::DrawingMethod method) const
629 {
630 if (method==ApplePictParser::D_INVERT ||
631 (method==ApplePictParser::D_TEXT && m_textMode==23) ||
632 (method!=ApplePictParser::D_TEXT && m_penMode==23)) return true;
633 return (method==ApplePictParser::D_FRAME && (m_penSize[0] == 0 || m_penSize[1] == 0));
634 }
635 //! update the actual style
636 void updateStyle(ApplePictParser::DrawingMethod method, MWAWGraphicStyle &style)
637 {
638 style=MWAWGraphicStyle();
639 if (method!=ApplePictParser::D_FRAME)
640 style.m_lineWidth=0;
641 else
642 style.m_lineWidth=0.5f*float(m_penSize[0]+m_penSize[1]);
643 MWAWColor color;
644 switch (method) {
645 case ApplePictParser::D_FRAME:
646 case ApplePictParser::D_TEXT: // set foreColor, it is use for defining the font color
647 color=m_foreColor;
648 if (!m_penPattern.empty())
649 m_penPattern.getAverageColor(color);
650 style.m_lineColor=color;
651 break;
652 case ApplePictParser::D_PAINT:
653 if (m_penPattern.empty())
654 style.setSurfaceColor(m_foreColor);
655 else if (m_penPattern.getUniqueColor(color))
656 style.setSurfaceColor(color);
657 else
658 style.setPattern(m_penPattern);
659 break;
660 case ApplePictParser::D_FILL:
661 if (m_fillPattern.empty())
662 style.setSurfaceColor(m_foreColor);
663 else if (m_fillPattern.getUniqueColor(color))
664 style.setSurfaceColor(color);
665 else
666 style.setPattern(m_fillPattern);
667 break;
668 case ApplePictParser::D_ERASE:
669 if (m_backgroundPattern.empty())
670 style.setSurfaceColor(MWAWColor(255,255,255));
671 else if (m_backgroundPattern.getUniqueColor(color))
672 style.setSurfaceColor(color);
673 else
674 style.setPattern(m_backgroundPattern);
675 break;
676 case ApplePictParser::D_INVERT:
677 break;
678 case ApplePictParser::D_UNDEFINED:
679 #if !defined(__clang__)
680 default:
681 #endif
682 break;
683 }
684
685 }
686 //! update the position
687 void updatePosition(MWAWVec2f const &orig, MWAWPosition &pos)
688 {
689 pos=MWAWPosition(orig-m_bdBox[0]+m_origin, MWAWVec2f(-1,-1), librevenge::RVNG_POINT);
690 pos.m_anchorTo=MWAWPosition::Page;
691 }
692 //! update the position
693 void updatePosition(MWAWBox2f const &bdBox, MWAWPosition &pos)
694 {
695 pos=MWAWPosition(bdBox[0]-m_bdBox[0]+m_origin, bdBox.size(), librevenge::RVNG_POINT);
696 pos.m_anchorTo=MWAWPosition::Page;
697 }
698 //! the file version
699 int m_version;
700 //! the bounding rectangle
701 MWAWBox2f m_bdBox;
702 //! the origin
703 MWAWVec2f m_origin;
704 //! the actual pen position
705 MWAWVec2i m_penPosition;
706 //! the actual text position
707 MWAWVec2i m_textPosition;
708 //! the actual pensize
709 MWAWVec2i m_penSize;
710 //! the actual ovalsize
711 MWAWVec2i m_ovalSize;
712 //! the pen mode
713 int m_penMode;
714 //! the text mode
715 int m_textMode;
716 //! true if we must use the hilite mode
717 bool m_isHiliteMode;
718 //! the actual font
719 MWAWFont m_font;
720 //! the foreground color
721 MWAWColor m_foreColor;
722 //! the background color
723 MWAWColor m_backColor;
724 //! the hilite color
725 MWAWColor m_hiliteColor;
726 //! the op color
727 MWAWColor m_opColor;
728 //! the pen pattern
729 MWAWGraphicStyle::Pattern m_penPattern;
730 //! the background pattern
731 MWAWGraphicStyle::Pattern m_backgroundPattern;
732 //! the fill pattern
733 MWAWGraphicStyle::Pattern m_fillPattern;
734 //! the last rectangle
735 MWAWBox2i m_rectangle;
736 //! the last round rectangle
737 MWAWBox2i m_roundRectangle;
738 //! the last circle
739 MWAWBox2i m_circle;
740 //! the last pie
741 MWAWBox2i m_pie;
742 //! the last polygon points
743 std::vector<MWAWVec2i> m_points;
744 //! a flag to know if we have found a quicktime picture/movie
745 bool m_afterQuicktime;
746 };
747
748 ////////////////////////////////////////
749 //! Internal: the subdocument of a ApplePictParser
750 class SubDocument final : public MWAWSubDocument
751 {
752 public:
753 SubDocument(ApplePictParser &pars, MWAWInputStreamPtr const &input, MWAWEntry const &entry)
754 : MWAWSubDocument(&pars, input, entry) {}
755
756 //! destructor
757 ~SubDocument() final {}
758
759 //! operator!=
760 bool operator!=(MWAWSubDocument const &doc) const final
761 {
762 if (MWAWSubDocument::operator!=(doc)) return true;
763 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
764 if (!sDoc) return true;
765 return false;
766 }
767
768 //! the parser function
769 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
770
771 private:
772 SubDocument(SubDocument const &orig) = delete;
773 SubDocument &operator=(SubDocument const &orig) = delete;
774 };
775
776 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
777 {
778 if (!listener || !listener->canWriteText()) {
779 MWAW_DEBUG_MSG(("ApplePictParserInternal::SubDocument::parse: no listener\n"));
780 return;
781 }
782 auto *parser=dynamic_cast<ApplePictParser *>(m_parser);
783 if (!parser) {
784 MWAW_DEBUG_MSG(("ApplePictParserInternal::SubDocument::parse: no parser\n"));
785 return;
786 }
787 long pos = m_input->tell();
788 parser->drawText(m_zone);
789 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
790 }
791
792
793 }
794
795 ////////////////////////////////////////////////////////////
796 // constructor/destructor, ...
797 ////////////////////////////////////////////////////////////
798 ApplePictParser::ApplePictParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
799 : MWAWGraphicParser(input, rsrcParser, header)
800 , m_state()
801 {
802 init();
803 }
804
805 ApplePictParser::~ApplePictParser()
806 {
807 }
808
809 void ApplePictParser::init()
810 {
811 resetGraphicListener();
812 setAsciiName("main-1");
813
814 m_state.reset(new ApplePictParserInternal::State);
815
816 getPageSpan().setMargins(0.001);
817 }
818
819 ////////////////////////////////////////////////////////////
820 // the parser
821 ////////////////////////////////////////////////////////////
822 void ApplePictParser::parse(librevenge::RVNGDrawingInterface *docInterface)
823 {
824 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
825 bool ok = false;
826 try {
827 // create the asciiFile
828 ascii().setStream(getInput());
829 ascii().open(asciiName());
830 checkHeader(nullptr);
831 createDocument(docInterface);
832 ok = createZones();
833 ascii().reset();
834 }
835 catch (...) {
836 MWAW_DEBUG_MSG(("ApplePictParser::parse: exception catched when parsing\n"));
837 ok = false;
838 }
839
840 resetGraphicListener();
841 if (!ok) throw(libmwaw::ParseException());
842 }
843
844 ////////////////////////////////////////////////////////////
845 // create the document
846 ////////////////////////////////////////////////////////////
847 void ApplePictParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
848 {
849 if (!documentInterface) return;
850 if (getGraphicListener()) {
851 MWAW_DEBUG_MSG(("ApplePictParser::createDocument: listener already exist\n"));
852 return;
853 }
854
855 // create the page list
856 MWAWPageSpan ps(getPageSpan());
857 ps.setPageSpan(1);
858 ps.setFormWidth(double(m_state->m_bdBox.size()[0])/72.);
859 ps.setFormLength(double(m_state->m_bdBox.size()[1])/72.);
860 std::vector<MWAWPageSpan> pageList(1,ps);
861 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
862 setGraphicListener(listen);
863 listen->startDocument();
864 }
865
866 ////////////////////////////////////////////////////////////
867 //
868 // Intermediate level
869 //
870 ////////////////////////////////////////////////////////////
871 bool ApplePictParser::createZones()
872 {
873 //m_state->m_penPosition=m_state->m_bdBox[0];
874 MWAWInputStreamPtr input = getInput();
875 long pos, debPos=input->tell();
876 while (!input->isEnd()) {
877 pos=input->tell();
878 if (!readZone()) {
879 input->seek(pos, librevenge::RVNG_SEEK_SET);
880 break;
881 }
882 }
883
884 bool ok=true;
885 if (!input->isEnd()) {
886 pos=input->tell();
887 MWAW_DEBUG_MSG(("ApplePictParser::createZones: find extra data\n"));
888 ascii().addPos(input->tell());
889 ascii().addNote("Entries(Data):##");
890 ok=(input->size()-debPos)<=2*(pos-debPos);
891 }
892 return ok;
893 }
894
895 bool ApplePictParser::readZone()
896 {
897 MWAWInputStreamPtr input = getInput();
898 long pos=input->tell();
899 int const vers=version();
900 int const opLength=version()>=2 ? 2 : 1;
901 if (!input->checkPosition(pos+opLength))
902 return false;
903 auto opCode=static_cast<int>(input->readULong(opLength));
904 DrawingMethod drawingMethod = D_UNDEFINED;
905 /* in general a basic bitmap follow a Quicktime picture/movie,
906 so let store the state to ignore some uneeded bitmap/pixmap
907 */
908 bool afterQuicktime=m_state->m_afterQuicktime;
909 m_state->m_afterQuicktime=false;
910 switch (opCode & 7) {
911 case 0:
912 drawingMethod = D_FRAME;
913 break;
914 case 1:
915 drawingMethod = D_PAINT;
916 break;
917 case 2:
918 drawingMethod = D_ERASE;
919 break;
920 case 3:
921 drawingMethod = D_INVERT;
922 break;
923 case 4:
924 drawingMethod = D_FILL;
925 break;
926 default:
927 break;
928 }
929 long dSz;
930 libmwaw::DebugStream f;
931 int val;
932 long actPos=input->tell();
933 switch (opCode) {
934 case 0: // NOP
935 f << "_";
936 break;
937 case 1: {
938 ApplePictParserInternal::Region region;
939 if (!readRegion(region))
940 return false;
941 f << "Entries(Region)[clip]:" << region;
942 break;
943 }
944 case 2:
945 if (!readBWPattern(m_state->m_backgroundPattern))
946 return false;
947 f << "Entries(Pattern)[back]:" << m_state->m_backgroundPattern << ",";
948 break;
949 case 3:
950 if (!input->checkPosition(actPos+2))
951 return false;
952 m_state->m_font.setId(static_cast<int>(input->readULong(2)));
953 f << "Entries(FontId):" << m_state->m_font.id() << ",";
954 break;
955 case 4: {
956 if (!input->checkPosition(actPos+1))
957 return false;
958 f << "Entries(TextFace):";
959 auto flag=static_cast<int>(input->readULong(1));
960 uint32_t flags=0;
961 if (flag&0x1) {
962 flags |= MWAWFont::boldBit;
963 f << "b:";
964 }
965 if (flag&0x2) {
966 flags |= MWAWFont::italicBit;
967 f << "it:";
968 }
969 if (flag&0x4) {
970 m_state->m_font.setUnderlineStyle(MWAWFont::Line::Simple);
971 f << "under:";
972 }
973 else
974 m_state->m_font.setUnderlineStyle(MWAWFont::Line::None);
975 if (flag&0x8) {
976 flags |= MWAWFont::embossBit;
977 f << "emboss:";
978 }
979 if (flag&0x10) {
980 flags |= MWAWFont::shadowBit;
981 f << "shadow:";
982 }
983 m_state->m_font.setDeltaLetterSpacing(0);
984 if (flag&0x20) {
985 m_state->m_font.setDeltaLetterSpacing(-1);
986 f << "condensed:";
987 }
988 if (flag&0x40) {
989 m_state->m_font.setDeltaLetterSpacing(1);
990 f << "extend:";
991 }
992 if (flag&0x80) f << "#flag0[0x80],";
993 m_state->m_font.setFlags(flags);
994 break;
995 }
996 case 5:
997 case 8: {
998 if (!input->checkPosition(actPos+2))
999 return false;
1000 auto mode=static_cast<int>(input->readULong(2));
1001 if (opCode==5) {
1002 f << "Entries(TextMode):";
1003 m_state->m_textMode=mode;
1004 }
1005 else {
1006 f << "Entries(PenMode):";
1007 m_state->m_penMode=mode;
1008 }
1009 f << getModeName(mode);
1010 break;
1011 }
1012 case 6:
1013 if (!input->checkPosition(actPos+4))
1014 return false;
1015 f << "Entries(SpaceExtra):" << float(input->readLong(4))/65536.f;
1016 break;
1017 case 7:
1018 case 0xb:
1019 case 0xc: {
1020 if (!input->checkPosition(actPos+4))
1021 return false;
1022 MWAWVec2i size;
1023 for (int i=0; i<2; ++i)
1024 size[1-i]=static_cast<int>(input->readULong(2));
1025 if (opCode==7) {
1026 m_state->m_penSize=size;
1027 f << "Entries(PenSize):" << size << ",";
1028 }
1029 else if (opCode==0xb) {
1030 m_state->m_ovalSize=size;
1031 f << "Entries(OvalSize):" << size << ",";
1032 }
1033 else {
1034 m_state->m_origin+=MWAWVec2f(static_cast<float>(size[1]),static_cast<float>(size[0]));
1035 f << "Entries(Orign):delta=" << MWAWVec2i(size[1],size[0]) << ",";
1036 }
1037 break;
1038 }
1039 case 9:
1040 if (!readBWPattern(m_state->m_penPattern))
1041 return false;
1042 f << "Entries(Pattern)[pen]:" << m_state->m_penPattern << ",";
1043 break;
1044 case 0xa:
1045 if (!readBWPattern(m_state->m_fillPattern))
1046 return false;
1047 f << "Entries(Pattern)[fill]:" << m_state->m_fillPattern << ",";
1048 break;
1049 case 0xd:
1050 if (!input->checkPosition(actPos+2))
1051 return false;
1052 val=static_cast<int>(input->readULong(2));
1053 f << "Entries(FontSz):" << val;
1054 m_state->m_font.setSize(float(val));
1055 break;
1056 case 0xe:
1057 case 0xf: {
1058 if (!input->checkPosition(actPos+2))
1059 return false;
1060 val=static_cast<int>(input->readULong(4));
1061 MWAWColor color;
1062 if (opCode==0xe)
1063 f << "Entries(Color)[fore]:";
1064 else
1065 f << "Entries(Color)[back]:";
1066 switch (val) {
1067 case 30:
1068 color=MWAWColor::white();
1069 break;
1070 case 33:
1071 color=MWAWColor::black();
1072 break;
1073 case 69:
1074 color=MWAWColor(255,255,0);
1075 break;
1076 case 137:
1077 color=MWAWColor(255,0,255);
1078 break;
1079 case 205:
1080 color=MWAWColor(255,0,0);
1081 break;
1082 case 273:
1083 color=MWAWColor(0,255,255);
1084 break;
1085 case 341:
1086 color=MWAWColor(0,0,255);
1087 break;
1088 case 409:
1089 color=MWAWColor(0,255,0);
1090 break;
1091 default:
1092 MWAW_DEBUG_MSG(("ApplePictParser::readZone: find unknown color\n"));
1093 break;
1094 }
1095 f << color;
1096 if (opCode==0xe)
1097 m_state->m_foreColor=color;
1098 else
1099 m_state->m_backColor=color;
1100 break;
1101 }
1102 case 0x10:
1103 if (!input->checkPosition(actPos+8))
1104 return false;
1105 f << "Entries(TextRatio):";
1106 for (int i=0; i<2; ++i) {
1107 if (i==0)
1108 f << "num=";
1109 else
1110 f << "denom=";
1111 for (int j=0; j<2; ++j) {
1112 f << input->readULong(2);
1113 if (j==0)
1114 f << "x";
1115 else
1116 f << ",";
1117 }
1118 }
1119 break;
1120 case 0x11:
1121 if (!input->checkPosition(actPos+1))
1122 return false;
1123 f << "Entries(Version):" << input->readLong(1);
1124 break;
1125 case 0x12:
1126 if (!readColorPattern(m_state->m_backgroundPattern))
1127 return false;
1128 f << "Entries(CPat)[back]:" << m_state->m_backgroundPattern << ",";
1129 break;
1130 case 0x13:
1131 if (!readColorPattern(m_state->m_penPattern))
1132 return false;
1133 f << "Entries(CPat)[pen]:" << m_state->m_penPattern << ",";
1134 break;
1135 case 0x14:
1136 if (!readColorPattern(m_state->m_fillPattern))
1137 return false;
1138 f << "Entries(CPat)[fill]:" << m_state->m_fillPattern << ",";
1139 break;
1140 case 0x15:
1141 if (!input->checkPosition(actPos+2))
1142 return false;
1143 f << "Entries(PnLocHFrac):" << float(input->readLong(2))/256.f;
1144 break;
1145 case 0x16:
1146 if (!input->checkPosition(actPos+2))
1147 return false;
1148 val=static_cast<int>(input->readLong(2));
1149 f << "Entries(ChExtra):" << val;
1150 m_state->m_font.setDeltaLetterSpacing(static_cast<float>(val));
1151 break;
1152 case 0x1c:
1153 f << "Entries(HiliteMode):";
1154 m_state->m_isHiliteMode=true;
1155 break;
1156 case 0x1e:
1157 f << "Entries(HiliteDef):";
1158 m_state->m_hiliteColor=MWAWColor::black();
1159 break;
1160 case 0x1a:
1161 case 0x1b:
1162 case 0x1d:
1163 case 0x1f: {
1164 MWAWColor col;
1165 if (!readRGBColor(col))
1166 return false;
1167 f << "Entries(Color)";
1168 if (opCode==0x1a) {
1169 f << "[fore]";
1170 m_state->m_foreColor=col;
1171 }
1172 else if (opCode==0x1b) {
1173 f << "[back]";
1174 m_state->m_backColor=col;
1175 }
1176 else if (opCode==0x1d) {
1177 f << "[hilite]";
1178 m_state->m_hiliteColor=col;
1179 }
1180 else {
1181 f << "[op]";
1182 m_state->m_opColor=col;
1183 }
1184 f << ":" << col;
1185 break;
1186 }
1187 case 0x20: {
1188 if (!input->checkPosition(8+actPos))
1189 return false;
1190 f << "Entries(Line):";
1191 for (int i=0; i<2; ++i)
1192 m_state->m_penPosition[1-i]=static_cast<int>(input->readLong(2));
1193 MWAWVec2i point;
1194 for (int i=0; i<2; ++i)
1195 point[1-i]=static_cast<int>(input->readLong(2));
1196 f << m_state->m_penPosition << "->" << point << ",";
1197 drawLine(point);
1198 break;
1199 }
1200 case 0x21: {
1201 if (!input->checkPosition(4+actPos))
1202 return false;
1203 f << "Entries(Line):";
1204 MWAWVec2i point;
1205 for (int i=0; i<2; ++i)
1206 point[1-i]=static_cast<int>(input->readLong(2));
1207 f << m_state->m_penPosition << "->" << point << ",";
1208 drawLine(point);
1209 break;
1210 }
1211 case 0x22: {
1212 if (!input->checkPosition(6+actPos))
1213 return false;
1214 f << "Entries(Line):";
1215 for (int i=0; i<2; ++i)
1216 m_state->m_penPosition[1-i]=static_cast<int>(input->readLong(2));
1217 MWAWVec2i point;
1218 for (int i=0; i<2; ++i)
1219 point[i]=m_state->m_penPosition[i]+static_cast<int>(input->readLong(1));
1220 f << m_state->m_penPosition << "->" << point << ",";
1221 drawLine(point);
1222 break;
1223 }
1224 case 0x23: {
1225 if (!input->checkPosition(2+actPos))
1226 return false;
1227 f << "Entries(Line):";
1228 MWAWVec2i point;
1229 for (int i=0; i<2; ++i)
1230 point[i]=m_state->m_penPosition[i]+static_cast<int>(input->readLong(1));
1231 f << m_state->m_penPosition << "->" << point << ",";
1232 drawLine(point);
1233 break;
1234 }
1235 case 0x28: {
1236 if (!input->checkPosition(5+actPos))
1237 return false;
1238 f << "Entries(TextData):";
1239 for (int i=0; i<2; ++i)
1240 m_state->m_textPosition[1-i]=static_cast<int>(input->readLong(2));
1241 f << m_state->m_textPosition << ",";
1242 std::string text("");
1243 if (!readAndDrawText(text))
1244 return false;
1245 f << text;
1246 break;
1247 }
1248 case 0x29:
1249 case 0x2a: {
1250 if (!input->checkPosition(2+actPos))
1251 return false;
1252 f << "Entries(TextData):";
1253 m_state->m_textPosition[opCode-0x29]=
1254 m_state->m_textPosition[opCode-0x29]+static_cast<int>(input->readULong(1));
1255 f << m_state->m_textPosition << ",";
1256 std::string text("");
1257 if (!readAndDrawText(text))
1258 return false;
1259 f << text;
1260 break;
1261 }
1262 case 0x2b: {
1263 if (!input->checkPosition(3+actPos))
1264 return false;
1265 f << "Entries(TextData):";
1266 for (int i=0; i<2; ++i)
1267 m_state->m_textPosition[i]=m_state->m_textPosition[i]+
1268 static_cast<int>(input->readULong(1));
1269 f << m_state->m_textPosition << ",";
1270 std::string text("");
1271 if (!readAndDrawText(text))
1272 return false;
1273 f << text;
1274 break;
1275 }
1276 case 0x2c: {
1277 if (!input->checkPosition(5+actPos))
1278 return false;
1279 f << "Entries(FontName):";
1280 dSz=static_cast<long>(input->readULong(2));
1281 f << "dSz=" << dSz << ",";
1282 if (!input->checkPosition(2+dSz+actPos))
1283 return false;
1284 auto id=static_cast<int>(input->readULong(2));
1285 f << "id=" << id << ",";
1286 auto sSz=static_cast<int>(input->readULong(1));
1287 if (sSz>dSz+3) {
1288 MWAW_DEBUG_MSG(("ApplePictParser::readZone: font name size seems bad\n"));
1289 sSz=int(dSz-3);
1290 }
1291 if (!input->checkPosition(5+sSz+actPos))
1292 return false;
1293 std::string name("");
1294 for (int i=0; i < sSz; ++i) name+=static_cast<char>(input->readULong(1));
1295 f << name;
1296 if (!name.empty())
1297 getParserState()->m_fontConverter->setCorrespondance(id, name);
1298 m_state->m_font.setId(id);
1299 input->seek(2+dSz+actPos, librevenge::RVNG_SEEK_SET);
1300 break;
1301 }
1302 case 0x2d:
1303 if (!input->checkPosition(actPos+2))
1304 return false;
1305 dSz=static_cast<long>(input->readULong(2));
1306 if (!input->checkPosition(actPos+2+dSz))
1307 return false;
1308 f << "Entries(LineSpacing):";
1309 if (dSz!=8) {
1310 MWAW_DEBUG_MSG(("ApplePictParser::readZone: the data length seems bad\n"));
1311 f << "###";
1312 input->seek(actPos+2+dSz, librevenge::RVNG_SEEK_SET);
1313 break;
1314 }
1315 val=static_cast<int>(input->readLong(4));
1316 m_state->m_font.setDeltaLetterSpacing(float(val)/65536.f);
1317 f << "char[spacing]=" << float(val)/65536.f << ",";
1318 f << "space[spacing]=" << float(input->readLong(4))/65536.f << ",";
1319 break;
1320 case 0x2e:
1321 if (!input->checkPosition(actPos+2))
1322 return false;
1323 dSz=static_cast<long>(input->readULong(2));
1324 if (!input->checkPosition(actPos+2+dSz))
1325 return false;
1326 f << "Entries(Glyph):";
1327 if (dSz!=4) {
1328 MWAW_DEBUG_MSG(("ApplePictParser::readZone: the data length seems bad\n"));
1329 f << "###";
1330 input->seek(actPos+2+dSz, librevenge::RVNG_SEEK_SET);
1331 break;
1332 }
1333 for (int i=0; i<4; ++i) {
1334 val=static_cast<int>(input->readLong(1));
1335 if (!val) continue;
1336 f << "f" << i << "=" << val << ",";
1337 }
1338 break;
1339 case 0x30:
1340 case 0x31:
1341 case 0x32:
1342 case 0x33:
1343 case 0x34:
1344 if (!readAndDrawRectangle(drawingMethod))
1345 return false;
1346 break;
1347 case 0x38:
1348 case 0x39:
1349 case 0x3a:
1350 case 0x3b:
1351 case 0x3c:
1352 f << "Entries(Rectangle)[" << getDrawingName(drawingMethod) << "]:same";
1353 drawRectangle(drawingMethod);
1354 break;
1355 case 0x40:
1356 case 0x41:
1357 case 0x42:
1358 case 0x43:
1359 case 0x44:
1360 if (!readAndDrawRoundRectangle(drawingMethod))
1361 return false;
1362 break;
1363 case 0x48:
1364 case 0x49:
1365 case 0x4a:
1366 case 0x4b:
1367 case 0x4c:
1368 f << "Entries(RoundRect)[" << getDrawingName(drawingMethod) << "]:same";
1369 drawRoundRectangle(drawingMethod);
1370 break;
1371 case 0x50:
1372 case 0x51:
1373 case 0x52:
1374 case 0x53:
1375 case 0x54:
1376 if (!readAndDrawCircle(drawingMethod))
1377 return false;
1378 break;
1379 case 0x58:
1380 case 0x59:
1381 case 0x5a:
1382 case 0x5b:
1383 case 0x5c:
1384 f << "Entries(Circle)[" << getDrawingName(drawingMethod) << "]:same";
1385 drawCircle(drawingMethod);
1386 break;
1387 case 0x60:
1388 case 0x61:
1389 case 0x62:
1390 case 0x63:
1391 case 0x64:
1392 if (!readAndDrawPie(drawingMethod))
1393 return false;
1394 break;
1395 case 0x68:
1396 case 0x69:
1397 case 0x6a:
1398 case 0x6b:
1399 case 0x6c: {
1400 if (!input->checkPosition(actPos+4))
1401 return false;
1402 f << "Entries(Pie)[" << getDrawingName(drawingMethod) << "]:same";
1403 int angles[2];
1404 for (int &angle : angles) angle=static_cast<int>(input->readLong(2));
1405 drawPie(drawingMethod, angles[0], angles[1]);
1406 break;
1407 }
1408 case 0x70:
1409 case 0x71:
1410 case 0x72:
1411 case 0x73:
1412 case 0x74:
1413 if (!readAndDrawPolygon(drawingMethod))
1414 return false;
1415 break;
1416 case 0x78:
1417 case 0x79:
1418 case 0x7a:
1419 case 0x7b:
1420 case 0x7c:
1421 f << "Entries(Polygon)[" << getDrawingName(drawingMethod) << "]:same";
1422 drawPolygon(drawingMethod);
1423 break;
1424 case 0x80:
1425 case 0x81:
1426 case 0x82:
1427 case 0x83:
1428 case 0x84: {
1429 ApplePictParserInternal::Region region;
1430 if (!readRegion(region))
1431 return false;
1432 f << "Entries(Region)[" << getDrawingName(drawingMethod) << "]:" << region;
1433 break;
1434 }
1435 case 0x88:
1436 case 0x89:
1437 case 0x8a:
1438 case 0x8b:
1439 case 0x8c:
1440 f << "Entries(Region)[" << getDrawingName(drawingMethod) << "]:same";
1441 break;
1442 case 0x90:
1443 case 0x91:
1444 case 0x98:
1445 case 0x99: {
1446 // first check if it is a bitmap or a pixmap
1447 bool pixmap = input->readULong(2) & 0x8000;
1448 input->seek(-2, librevenge::RVNG_SEEK_CUR);
1449 bool packed = (opCode&8);
1450 bool hasRgn = (opCode&1);
1451 if (pixmap) {
1452 ApplePictParserInternal::Pixmap bitmap;
1453 if (!readPixmap(bitmap, packed, true, true, hasRgn))
1454 return false;
1455 if (!afterQuicktime)
1456 drawPixmap(bitmap);
1457 f << "Entries(Pixmap):";
1458 }
1459 else {
1460 ApplePictParserInternal::Bitmap bitmap;
1461 if (!readBitmap(bitmap, packed, hasRgn))
1462 return false;
1463 if (!afterQuicktime)
1464 drawBitmap(bitmap);
1465 f << "Entries(Bitmap):";
1466 }
1467 break;
1468 }
1469 case 0x9a:
1470 case 0x9b: {
1471 ApplePictParserInternal::Pixmap bitmap;
1472 if (!readPixmap(bitmap, false, false, true, (opCode&1) ? true : false))
1473 return false;
1474 drawPixmap(bitmap);
1475 f << "Entries(Pixmap):";
1476 break;
1477 }
1478 case 0xa0:
1479 if (!input->checkPosition(actPos+2))
1480 return false;
1481 f << "Entries(Comment)[short]:kind="<<input->readLong(2) << ",";
1482 break;
1483 case 0xa1:
1484 case 0xa5: // not really a Pict1 code, but it can appear in some pict
1485 if (!input->checkPosition(actPos+4))
1486 return false;
1487 f << "Entries(Comment)[long]:kind="<<input->readLong(2) << ",";
1488 if (opCode==0xa5) f << "#unusual,";
1489 dSz=static_cast<long>(input->readULong(2));
1490 if (!input->checkPosition(actPos+4+dSz))
1491 return false;
1492 input->seek(dSz, librevenge::RVNG_SEEK_CUR);
1493 break;
1494 case 0xff:
1495 f << "Entries(EOP):";
1496 if (vers>=2) input->seek(2, librevenge::RVNG_SEEK_CUR);
1497 break;
1498 case 0x8200: {
1499 MWAWEmbeddedObject picture;
1500 MWAWBox2f bdBox;
1501 if (!readQuicktime(picture,bdBox))
1502 return false;
1503 m_state->m_afterQuicktime=true;
1504 if (!picture.isEmpty() && getGraphicListener()) {
1505 MWAWGraphicStyle style;
1506 MWAWPosition position;
1507 m_state->updatePosition(bdBox, position);
1508 getGraphicListener()->insertPicture(position,picture);
1509 }
1510 break;
1511 }
1512 case 0x8201: {
1513 dSz=4+static_cast<long>(input->readULong(4));
1514 if (!input->checkPosition(actPos+dSz))
1515 return false;
1516 MWAW_DEBUG_MSG(("ApplePictParser::readZone: reading compressed Quicktime is not implemented\n"));
1517 f << "Entries(QuickTComp):";
1518 input->seek(actPos+dSz, librevenge::RVNG_SEEK_SET);
1519 break;
1520 }
1521
1522 //
1523 // Reserved
1524 //
1525 case 0x17: // reserved + 0 byes
1526 case 0x18:
1527 case 0x19:
1528 case 0x3d:
1529 case 0x3e:
1530 case 0x3f:
1531 case 0x4d:
1532 case 0x4e:
1533 case 0x4f:
1534 case 0x5d:
1535 case 0x5e:
1536 case 0x5f:
1537 case 0x6d:
1538 case 0x6e:
1539 case 0x6f:
1540 case 0x7d:
1541 case 0x7e:
1542 case 0x7f:
1543 case 0x8d:
1544 case 0x8e:
1545 case 0x8f:
1546 case 0xcf: // checkme
1547 f << "Entries(Reserved"<<std::hex << opCode << std::dec << "):";
1548 break;
1549
1550 case 0x35: // reserved + 8 bytes
1551 case 0x36:
1552 case 0x37:
1553 case 0x45:
1554 case 0x46:
1555 case 0x47:
1556 case 0x55:
1557 case 0x56:
1558 case 0x57:
1559 if (!input->checkPosition(actPos+8))
1560 return false;
1561 f << "Entries(Reserved"<<std::hex << opCode << std::dec << "):";
1562 input->seek(8, librevenge::RVNG_SEEK_CUR);
1563 break;
1564
1565 case 0x65: // reserved + 12 bytes
1566 case 0x66:
1567 case 0x67:
1568 if (!input->checkPosition(actPos+12))
1569 return false;
1570 f << "Entries(Reserved"<<std::hex << opCode << std::dec << "):";
1571 input->seek(8, librevenge::RVNG_SEEK_CUR);
1572 break;
1573
1574 case 0x24: // reserved + N bytes
1575 case 0x25:
1576 case 0x26:
1577 case 0x27:
1578 case 0x2f:
1579 case 0x75:
1580 case 0x76:
1581 case 0x77:
1582 case 0x85:
1583 case 0x86:
1584 case 0x87:
1585 case 0x92:
1586 case 0x93:
1587 case 0x94:
1588 case 0x95:
1589 case 0x96:
1590 case 0x97:
1591 case 0x9c:
1592 case 0x9d:
1593 case 0x9e:
1594 case 0x9f:
1595 case 0xa2: // checkme
1596 if (!input->checkPosition(actPos+2))
1597 return false;
1598 f << "Entries(Reserved"<<std::hex << opCode << std::dec << "):";
1599 dSz=static_cast<long>(input->readULong(2));
1600 if (!input->checkPosition(actPos+2+dSz))
1601 return false;
1602 input->seek(dSz, librevenge::RVNG_SEEK_CUR);
1603 break;
1604 default: {
1605 if (opCode<=0xaf) dSz=2+static_cast<int>(input->readULong(2));
1606 else if (opCode<=0xcf) dSz=0;
1607 else if (opCode<=0x100) dSz=4+static_cast<long>(input->readULong(4));
1608 else if (opCode<=0x01ff) dSz=2;
1609 else if (opCode<=0x0bfe) dSz=4;
1610 else if (opCode<=0x0bff) dSz=22;
1611 else if (opCode==0x0c00) dSz=24; // HeaderOp
1612 else if (opCode<=0x7eff) dSz=24;
1613 else if (opCode<=0x7fff) dSz=254;
1614 else if (opCode<=0x80ff) dSz=0;
1615 else dSz=4+static_cast<long>(input->readULong(4));
1616 if (!input->checkPosition(actPos+dSz))
1617 return false;
1618 f << "Entries(Reserved"<<std::hex << opCode << std::dec << "):";
1619 input->seek(actPos+dSz, librevenge::RVNG_SEEK_SET);
1620 break;
1621 }
1622 }
1623 if (vers>=2 && ((input->tell()-pos)%2)!=0)
1624 input->seek(1, librevenge::RVNG_SEEK_CUR);
1625 ascii().addPos(pos);
1626 ascii().addNote(f.str().c_str());
1627 return true;
1628 }
1629
1630 bool ApplePictParser::readAndDrawRectangle(ApplePictParser::DrawingMethod method)
1631 {
1632 MWAWInputStreamPtr input = getInput();
1633 long pos=input->tell();
1634 if (!input->checkPosition(pos+8))
1635 return false;
1636 libmwaw::DebugStream f;
1637 f << "Entries(Rectangle)[" << getDrawingName(method) << "]:";
1638 int dim[4];
1639 for (int &i : dim) i=static_cast<int>(input->readLong(2));
1640 m_state->m_rectangle=MWAWBox2i(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2]));
1641 f << m_state->m_rectangle;
1642 drawRectangle(method);
1643 ascii().addPos(pos-(version()==1 ? 1 : 2));
1644 ascii().addNote(f.str().c_str());
1645 return true;
1646 }
1647
1648 bool ApplePictParser::readAndDrawRoundRectangle(ApplePictParser::DrawingMethod method)
1649 {
1650 MWAWInputStreamPtr input = getInput();
1651 long pos=input->tell();
1652 if (!input->checkPosition(pos+8))
1653 return false;
1654 libmwaw::DebugStream f;
1655 f << "Entries(RoundRect)[" << getDrawingName(method) << "]:";
1656 int dim[4];
1657 for (int &i : dim) i=static_cast<int>(input->readLong(2));
1658 m_state->m_roundRectangle=MWAWBox2i(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2]));
1659 f << m_state->m_roundRectangle;
1660 drawRoundRectangle(method);
1661 ascii().addPos(pos-(version()==1 ? 1 : 2));
1662 ascii().addNote(f.str().c_str());
1663 return true;
1664 }
1665
1666 bool ApplePictParser::readAndDrawCircle(ApplePictParser::DrawingMethod method)
1667 {
1668 MWAWInputStreamPtr input = getInput();
1669 long pos=input->tell();
1670 if (!input->checkPosition(pos+8))
1671 return false;
1672 libmwaw::DebugStream f;
1673 f << "Entries(Circle)[" << getDrawingName(method) << "]:";
1674 int dim[4];
1675 for (int &i : dim) i=static_cast<int>(input->readLong(2));
1676 m_state->m_circle=MWAWBox2i(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2]));
1677 f << m_state->m_circle;
1678 drawCircle(method);
1679 ascii().addPos(pos-(version()==1 ? 1 : 2));
1680 ascii().addNote(f.str().c_str());
1681 return true;
1682 }
1683
1684 bool ApplePictParser::readAndDrawPie(ApplePictParser::DrawingMethod method)
1685 {
1686 MWAWInputStreamPtr input = getInput();
1687 long pos=input->tell();
1688 if (!input->checkPosition(pos+12))
1689 return false;
1690 libmwaw::DebugStream f;
1691 f << "Entries(Pie)[" << getDrawingName(method) << "]:";
1692 int dim[4];
1693 for (int &i : dim) i=static_cast<int>(input->readLong(2));
1694 m_state->m_pie=MWAWBox2i(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2]));
1695 f << m_state->m_pie << ",";
1696 int angles[2];
1697 for (int &angle : angles) angle=static_cast<int>(input->readLong(2));
1698 f << "angl=" << angles[0] << "x" << angles[0]+angles[1] << ",";
1699 drawPie(method, angles[0], angles[1]);
1700 ascii().addPos(pos-(version()==1 ? 1 : 2));
1701 ascii().addNote(f.str().c_str());
1702 return true;
1703 }
1704
1705 bool ApplePictParser::readAndDrawPolygon(ApplePictParser::DrawingMethod method)
1706 {
1707 MWAWInputStreamPtr input = getInput();
1708 long pos=input->tell();
1709 auto dSz=static_cast<int>(input->readULong(2));
1710 if (dSz<10 || (dSz%4)!=2 || !input->checkPosition(pos+dSz))
1711 return false;
1712 libmwaw::DebugStream f;
1713 f << "Entries(Polygon)[" << getDrawingName(method) << "]:";
1714 int dim[4];
1715 for (int &i : dim) i=static_cast<int>(input->readLong(2));
1716 f << MWAWBox2i(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2])) << ",";
1717 int N=(dSz-10)/4;
1718 f << "pts=[";
1719 m_state->m_points.clear();
1720 for (int i=0; i<N; ++i) {
1721 int coord[2];
1722 for (int &j : coord) j=int(input->readLong(2));
1723 m_state->m_points.push_back(MWAWVec2i(coord[1],coord[0]));
1724 f << MWAWVec2i(coord[1],coord[0]) << ",";
1725 }
1726 f << "],";
1727 drawPolygon(method);
1728 ascii().addPos(pos-(version()==1 ? 1 : 2));
1729 ascii().addNote(f.str().c_str());
1730 return true;
1731 }
1732
1733 bool ApplePictParser::readRGBColor(MWAWColor &color)
1734 {
1735 MWAWInputStreamPtr input = getInput();
1736 long pos=input->tell();
1737 if (!input->checkPosition(pos+6))
1738 return false;
1739 uint8_t col[3];
1740 for (uint8_t &i : col) i=uint8_t(input->readULong(2)>>8);
1741 color=MWAWColor(col[0],col[1],col[2]);
1742 return true;
1743 }
1744
1745 bool ApplePictParser::readBWPattern(MWAWGraphicStyle::Pattern &pat)
1746 {
1747 MWAWInputStreamPtr input = getInput();
1748 long pos=input->tell();
1749 if (!input->checkPosition(pos+8))
1750 return false;
1751 pat.m_dim=MWAWVec2i(8,8);
1752 pat.m_data.resize(8);
1753 pat.m_colors[0]=MWAWColor::white();
1754 pat.m_colors[1]=MWAWColor::black();
1755
1756 for (size_t i=0; i<8; ++i)
1757 pat.m_data[i]=uint8_t(input->readULong(1));
1758 return true;
1759 }
1760
1761 bool ApplePictParser::readColorPattern(MWAWGraphicStyle::Pattern &pat)
1762 {
1763 MWAWInputStreamPtr input = getInput();
1764 long pos=input->tell();
1765 if (!input->checkPosition(pos+10))
1766 return false;
1767 auto type = static_cast<int>(input->readULong(2));
1768 if (type !=1 && type != 2) {
1769 MWAW_DEBUG_MSG(("ApplePictParser::readColorPattern: unknown type=%d... \n", type));
1770 return false;
1771 }
1772
1773 pat.m_dim=MWAWVec2i(8,8);
1774 pat.m_data.resize(8);
1775 pat.m_colors[0]=MWAWColor::white();
1776 pat.m_colors[1]=MWAWColor::black();
1777
1778 for (size_t i=0; i<8; ++i)
1779 pat.m_data[i]=uint8_t(input->readULong(1));
1780 if (type==2) {
1781 // a color pattern -> create a uniform color pattern
1782 if (!readRGBColor(pat.m_colors[0]))
1783 return false;
1784 for (size_t i=0; i<8; ++i)
1785 pat.m_data[i]=0;
1786 return true;
1787 }
1788 ApplePictParserInternal::Pixmap pixmap;
1789 return readPixmap(pixmap, false, true, false, false);
1790 }
1791
1792 bool ApplePictParser::readAndDrawText(std::string &text)
1793 {
1794 MWAWInputStreamPtr input = getInput();
1795 long pos=input->tell();
1796 if (!input->checkPosition(pos+1))
1797 return false;
1798 auto dSz=static_cast<int>(input->readULong(1));
1799 if (!input->checkPosition(pos+1+dSz))
1800 return false;
1801 text="";
1802 MWAWEntry entry;
1803 entry.setBegin(input->tell());
1804 entry.setLength(dSz);
1805 for (int i=0; i<dSz; ++i) text+=static_cast<char>(input->readULong(1));
1806 if (m_state->isInvisible(D_TEXT)) return true;
1807
1808 MWAWListenerPtr listener=getGraphicListener();
1809 if (!listener || listener->canWriteText()) {
1810 MWAW_DEBUG_MSG(("ApplePictParser::readAndDrawText: can not find the listener\n"));
1811 return true;
1812 }
1813 std::shared_ptr<MWAWSubDocument> doc(new ApplePictParserInternal::SubDocument(*this, getInput(), entry));
1814 MWAWGraphicStyle style;
1815 m_state->updateStyle(D_TEXT, style);
1816 MWAWVec2f orig(m_state->m_textPosition);
1817 orig[1]=orig[1]-m_state->m_font.size();
1818 MWAWPosition position;
1819 m_state->updatePosition(orig, position);
1820 listener->insertTextBox(position, doc, style);
1821
1822 input->seek(pos+1+dSz, librevenge::RVNG_SEEK_SET);
1823 return true;
1824 }
1825
1826 bool ApplePictParser::readBitmap(ApplePictParserInternal::Bitmap &bitmap, bool packed, bool hasRegion)
1827 {
1828 MWAWInputStreamPtr input = getInput();
1829 long pos=input->tell();
1830 if (!input->checkPosition(pos+28))
1831 return false;
1832 libmwaw::DebugStream f;
1833 f << "Bitmap:";
1834 bitmap.m_rowBytes = static_cast<int>(input->readULong(2));
1835 bitmap.m_rowBytes &= 0x3FFF;
1836 if (bitmap.m_rowBytes < 0 || (!packed && bitmap.m_rowBytes > 8)) {
1837 MWAW_DEBUG_MSG(("ApplePictParser::readBitmap: find odd rowBytes %d... \n", bitmap.m_rowBytes));
1838 return false;
1839 }
1840 // read the rectangle: bound
1841 // ------ end of bitmap ----------
1842 // and the two general rectangle src, dst
1843 for (int c = 0; c < 3; c++) {
1844 int val[4];
1845 for (int &d : val) d = static_cast<int>(input->readLong(2));
1846 MWAWBox2i box(MWAWVec2i(val[1],val[0]), MWAWVec2i(val[3],val[2]));
1847 if (box.size().x() <= 0 || box.size().y() <= 0) {
1848 MWAW_DEBUG_MSG(("ApplePictParser::readBitmap: find odd rectangle %d... \n", c));
1849 return false;
1850 }
1851 if (c == 0) bitmap.m_rect=box;
1852 else if (c==1) bitmap.m_src = box;
1853 else bitmap.m_dst = box;
1854 }
1855
1856 if (!packed && bitmap.m_rowBytes*8 < bitmap.m_rect.size().x()) {
1857 MWAW_DEBUG_MSG(("ApplePictParser::readBitmap: row bytes seems to short: %d/%d... \n", bitmap.m_rowBytes*8, bitmap.m_rect.size().y()));
1858 return false;
1859 }
1860 bitmap.m_mode = static_cast<int>(input->readLong(2)); // mode: I find 0,1 and 3
1861 if (bitmap.m_mode < 0 || bitmap.m_mode > 64) {
1862 MWAW_DEBUG_MSG(("ApplePictParser::readBitmap: unknown mode: %d \n", bitmap.m_mode));
1863 return false;
1864 }
1865
1866 if (hasRegion) { // CHECKME...
1867 std::shared_ptr<ApplePictParserInternal::Region>rgn(new ApplePictParserInternal::Region);
1868 if (!readRegion(*rgn)) return false;
1869 bitmap.m_region = rgn;
1870 }
1871 long actPos=input->tell();
1872 if (!bitmap.readBitmapData(*input, packed)) return false;
1873 ascii().skipZone(actPos,input->tell()-1);
1874 f << bitmap;
1875 f << getModeName(bitmap.m_mode) << ",";
1876 ascii().addPos(pos);
1877 ascii().addNote(f.str().c_str());
1878 return true;
1879 }
1880
1881 bool ApplePictParser::readPixmap(ApplePictParserInternal::Pixmap &pixmap, bool packed, bool colorTable, bool hasRectsMode, bool hasRegion)
1882 {
1883 MWAWInputStreamPtr input = getInput();
1884 long pos=input->tell();
1885 if (!input->checkPosition(pos+46))
1886 return false;
1887 libmwaw::DebugStream f;
1888 f << "Pixmap:";
1889
1890 if (!colorTable) input->seek(4, librevenge::RVNG_SEEK_CUR); // skip the base address
1891
1892 pixmap.m_rowBytes = static_cast<int>(input->readULong(2));
1893 pixmap.m_rowBytes &= 0x3FFF;
1894
1895 // read the rectangle: bound
1896 int val[4];
1897 for (int &d : val) d = static_cast<int>(input->readLong(2));
1898 pixmap.m_rect = MWAWBox2i(MWAWVec2i(val[1],val[0]), MWAWVec2i(val[3],val[2]));
1899 if (pixmap.m_rect.size().x() <= 0 || pixmap.m_rect.size().y() <= 0) {
1900 MWAW_DEBUG_MSG(("ApplePictParser::readPixmap: find odd bound rectangle ... \n"));
1901 return false;
1902 }
1903 pixmap.m_version = static_cast<int>(input->readLong(2));
1904 pixmap.m_packType = static_cast<int>(input->readLong(2));
1905 pixmap.m_packSize = static_cast<int>(input->readLong(4));
1906 for (int &c : pixmap.m_resolution) {
1907 c = static_cast<int>(input->readLong(2));
1908 input->readLong(2);
1909 }
1910 pixmap.m_pixelType = static_cast<int>(input->readLong(2));
1911 pixmap.m_pixelSize = static_cast<int>(input->readLong(2));
1912 pixmap.m_compCount = static_cast<int>(input->readLong(2));
1913 pixmap.m_compSize = static_cast<int>(input->readLong(2));
1914 pixmap.m_planeBytes = static_cast<int>(input->readLong(4));
1915
1916 // ignored: colorHandle+reserved
1917 input->seek(8, librevenge::RVNG_SEEK_CUR);
1918
1919 // the color table
1920 if (colorTable) {
1921 pixmap.m_colorTable.reset(new ApplePictParserInternal::ColorTable);
1922 if (!pixmap.m_colorTable->read(*input)) return false;
1923 }
1924
1925 if (!packed && pixmap.m_rowBytes*8 < pixmap.m_rect.size().y()) {
1926 MWAW_DEBUG_MSG(("ApplePictParser::readPixmap: row bytes seems to short: %d/%d... \n", pixmap.m_rowBytes*8, pixmap.m_rect.size().y()));
1927 return false;
1928 }
1929
1930 // read the two general rectangle src, dst
1931 if (hasRectsMode) {
1932 for (int c = 0; c < 2; c++) {
1933 int dim[4];
1934 for (int &d : dim) d = static_cast<int>(input->readLong(2));
1935 MWAWBox2i box(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2]));
1936 if (box.size().x() <= 0 || box.size().y() <= 0) {
1937 MWAW_DEBUG_MSG(("ApplePictParser::readPixmap: find odd rectangle %d... \n", c));
1938 return false;
1939 }
1940 else if (c==0) pixmap.m_src = box;
1941 else pixmap.m_dst = box;
1942 }
1943 pixmap.m_mode = static_cast<int>(input->readLong(2)); // mode: I find 0,1 and 3
1944 f << "mode=" << getModeName(pixmap.m_mode) << ",";
1945 }
1946
1947 if (hasRegion) { // CHECKME...
1948 std::shared_ptr<ApplePictParserInternal::Region> rgn(new ApplePictParserInternal::Region);
1949 if (!readRegion(*rgn)) return false;
1950 pixmap.m_region = rgn;
1951 }
1952 long actPos=input->tell();
1953 if (!pixmap.readPixmapData(*input)) return false;
1954 ascii().skipZone(actPos,input->tell()-1);
1955 f << pixmap << ",";
1956 ascii().addPos(pos);
1957 ascii().addNote(f.str().c_str());
1958
1959 return true;
1960 }
1961
1962 bool ApplePictParser::readQuicktime(MWAWEmbeddedObject &object, MWAWBox2f &bdBox)
1963 {
1964 MWAWInputStreamPtr input = getInput();
1965 long pos=input->tell();
1966 if (!input->checkPosition(pos+4))
1967 return false;
1968 auto dSz=static_cast<long>(input->readULong(4));
1969 long endPos=pos+4+dSz;
1970 if (dSz<68 || !input->checkPosition(endPos))
1971 return false;
1972 libmwaw::DebugStream f;
1973 f << "Entries(Quicktime):";
1974 auto val=static_cast<int>(input->readULong(2));
1975 if (val) f << "vers=" << val << ",";
1976 float matrix[9];
1977 f << "mat=[";
1978 for (int i=0; i<9; ++i) {
1979 float value=float(input->readLong(4))/65536.f;
1980 // the last one is a fract number
1981 if (i==8) value/=16384.f;
1982 matrix[i]=value;
1983 if (value<0 || value>0)
1984 f << value << ",";
1985 else
1986 f << "_,";
1987 }
1988 f << "],";
1989 if (matrix[8]<=0) {
1990 MWAW_DEBUG_MSG(("ApplePictParser::readQuicktime: find odd w coefficient in matrix\n"));
1991 f << "###w,";
1992 matrix[8]=1;
1993 }
1994 auto matteSize=static_cast<long>(input->readULong(4));
1995 if (matteSize)
1996 f << "matteSize=" << std::hex << matteSize << std::dec << ",";
1997 int dim[4];
1998 for (int &i : dim) i=static_cast<int>(input->readLong(2));
1999 if (dim[2]!=dim[0] || dim[3]!=dim[2])
2000 f << "matteRect=" << MWAWBox2i(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2])) << ",";
2001
2002 auto mode=static_cast<int>(input->readULong(2));
2003 if (mode) f << "mode=" << mode << ",";
2004 for (int &i : dim) i=static_cast<int>(input->readLong(2));
2005 f << "srcRec=" << MWAWBox2i(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2])) << ",";
2006 // assume that the matrix contains only some scaling and/or translation
2007 if (matrix[1]<0 || matrix[1]>0 || matrix[2]<0 || matrix[2]>0 ||
2008 matrix[3]<0 || matrix[3]>0 || matrix[5]<0 || matrix[5]>0) {
2009 MWAW_DEBUG_MSG(("ApplePictParser::readQuicktime: oops the matrix is not a scaling matrix\n"));
2010 matrix[1]=matrix[3]=1;
2011 matrix[2]=matrix[5]=0;
2012 }
2013 float dimF[4];
2014 for (int i=0; i<2; ++i) {
2015 dimF[2*i]=(static_cast<float>(dim[2*i])*matrix[0]+matrix[7])/matrix[8];
2016 dimF[2*i+1]=(static_cast<float>(dim[2*i+1])*matrix[4]+matrix[6])/matrix[8];
2017 }
2018 if (dimF[0]>dimF[2]) std::swap(dimF[0],dimF[2]);
2019 if (dimF[1]>dimF[3]) std::swap(dimF[1],dimF[3]);
2020 bdBox=MWAWBox2f(MWAWVec2f(dimF[1],dimF[0]),MWAWVec2f(dimF[3],dimF[2]));
2021 val=static_cast<int>(input->readULong(4));
2022 if (val) f << "accuracy=" << val << ",";
2023 auto maskSize=static_cast<long>(input->readULong(4));
2024 if (maskSize)
2025 f << "maskSize=" << std::hex << maskSize << std::dec << ",";
2026 ascii().addPos(pos-2);
2027 ascii().addNote(f.str().c_str());
2028 if (matteSize) {
2029 pos=input->tell();
2030 f.str("");
2031 f << "Quicktime:matteDesc,";
2032 dSz=static_cast<long>(input->readULong(4));
2033 if (pos+4+dSz+matteSize>endPos) {
2034 MWAW_DEBUG_MSG(("ApplePictParser::readQuicktime: find odd mat size\n"));
2035 f << "###";
2036 ascii().addPos(pos);
2037 ascii().addNote(f.str().c_str());
2038 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2039 return true;
2040 }
2041 input->seek(pos+4+dSz, librevenge::RVNG_SEEK_SET);
2042 ascii().addPos(pos);
2043 ascii().addNote(f.str().c_str());
2044
2045 pos=input->tell();
2046 f.str("");
2047 f << "Quicktime:matteData,";
2048 input->seek(pos+matteSize, librevenge::RVNG_SEEK_SET);
2049 ascii().addPos(pos);
2050 ascii().addNote(f.str().c_str());
2051 }
2052 if (maskSize) {
2053 pos=input->tell();
2054 f.str("");
2055 f << "Quicktime:mask,";
2056 if (pos+maskSize>endPos) {
2057 MWAW_DEBUG_MSG(("ApplePictParser::readQuicktime: can not read the mask section\n"));
2058 f << "###";
2059 ascii().addPos(pos);
2060 ascii().addNote(f.str().c_str());
2061 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2062 return true;
2063 }
2064 // CHECKME: normally a region
2065 input->seek(pos+maskSize, librevenge::RVNG_SEEK_SET);
2066 ascii().addPos(pos);
2067 ascii().addNote(f.str().c_str());
2068 }
2069 pos=input->tell();
2070 f.str("");
2071 f << "Quicktime:imageDesc,";
2072 dSz=static_cast<long>(input->readULong(4));
2073 if (dSz<86 || pos+dSz>endPos) {
2074 MWAW_DEBUG_MSG(("ApplePictParser::readQuicktime: can not read the image description\n"));
2075 f << "###";
2076 ascii().addPos(pos);
2077 ascii().addNote(f.str().c_str());
2078 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2079 return true;
2080 }
2081 std::string creator;
2082 for (int i=0; i<4; ++i) creator+=char(input->readULong(1));
2083 f << "creator=" << creator << ",";
2084 input->seek(6, librevenge::RVNG_SEEK_CUR); // reserved1 and reserved2
2085 val=static_cast<int>(input->readLong(2));
2086 if (val) f << "dataRefId=" << val << ","; // must be 0
2087 val=static_cast<int>(input->readLong(2));
2088 if (val) f << "vers=" << val << ",";
2089 val=static_cast<int>(input->readLong(2));
2090 if (val) f << "revision[level]=" << val << ",";
2091 std::string vendor;
2092 for (int i=0; i<4; ++i) vendor+=char(input->readULong(1));
2093 f << "vendor=" << vendor << ",";
2094 val=static_cast<int>(input->readULong(4)); // CodecQ: min=0, max=3FF, loseless=400
2095 if (val) f << "quality[temporal]=" << std::hex << val << std::dec << ",";
2096 val=static_cast<int>(input->readULong(4)); // CodecQ: min=0, max=3FF, loseless=400
2097 if (val) f << "quality[spacial]=" << std::hex << val << std::dec << ",";
2098 for (int i=0; i<2; ++i) dim[i]=static_cast<int>(input->readLong(2));
2099 f << "src[sz]=" << MWAWVec2i(dim[0],dim[1]) << ",";
2100 f << "res=" << double(input->readLong(4))/65536. << "x" << double(input->readLong(4))/65536. << ",";
2101 auto dataSize=static_cast<long>(input->readULong(4));
2102 f << "dataSize=" << std::hex << dataSize << std::dec << ",";
2103 f << "frame[count]=" << input->readULong(2) << ",";
2104 auto sSz=static_cast<int>(input->readULong(1));
2105 if (sSz>31 || input->tell()+sSz>pos+dSz) {
2106 MWAW_DEBUG_MSG(("ApplePictParser::readQuicktime: can not read the compression name\n"));
2107 f << "###";
2108 ascii().addPos(pos);
2109 ascii().addNote(f.str().c_str());
2110 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2111 return true;
2112 }
2113 long lastSPos=input->tell()+31;
2114 std::string name;
2115 for (int i=0; i<sSz; ++i) name+=char(input->readULong(1));
2116 f << name << ",";
2117 input->seek(lastSPos, librevenge::RVNG_SEEK_SET);
2118 val=static_cast<int>(input->readLong(2));
2119 if (val) f << "depth=" << val << ",";
2120 val=static_cast<int>(input->readLong(2));
2121 if (val!=-1) f << "clutId=" << val << ",";
2122 if (input->tell()!=pos+dSz)
2123 ascii().addDelimiter(input->tell(),'|');
2124 ascii().addPos(pos);
2125 ascii().addNote(f.str().c_str());
2126 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
2127
2128 pos=input->tell();
2129 f.str("");
2130 f << "Quicktime:data,";
2131 librevenge::RVNGBinaryData data;
2132 if (dataSize<=0 || pos+dataSize>endPos || !input->readDataBlock(dataSize, data)) {
2133 MWAW_DEBUG_MSG(("ApplePictParser::readQuicktime: can not read the data zone\n"));
2134 f << "###";
2135 ascii().addPos(pos);
2136 ascii().addNote(f.str().c_str());
2137 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2138 return true;
2139 }
2140 object.add(data);
2141 ascii().skipZone(pos,pos+dataSize-1);
2142 input->seek(pos+dataSize, librevenge::RVNG_SEEK_SET);
2143 pos=input->tell();
2144 if (pos+3<endPos) {
2145 MWAW_DEBUG_MSG(("ApplePictParser::readQuicktime: find some extra data\n"));
2146 f << "#extra,";
2147 ascii().addPos(pos);
2148 ascii().addNote(f.str().c_str());
2149 }
2150 else if (pos!=endPos) {
2151 ascii().addPos(pos);
2152 ascii().addNote("_");
2153 }
2154 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2155 return true;
2156 }
2157
2158 bool ApplePictParser::readRegion(ApplePictParserInternal::Region &region)
2159 {
2160 MWAWInputStreamPtr input = getInput();
2161 long pos=input->tell();
2162 if (!input->checkPosition(pos+10))
2163 return false;
2164 auto dSz=static_cast<int>(input->readULong(2));
2165 if (dSz<10||!input->checkPosition(pos+dSz))
2166 return false;
2167 int dim[4];
2168 for (int &i : dim) i=static_cast<int>(input->readLong(2));
2169 region.m_bdBox=MWAWBox2i(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2]));
2170 int remain=(dSz-10)/2;
2171 // une liste de point dans la box: x1, y1, .. yn 0x7fff, x2, ... 0x7fff
2172 while (remain > 0) {
2173 auto y = static_cast<int>(input->readLong(2));
2174 --remain;
2175 if (y == 0x7fff) break;
2176 if (y < region.m_bdBox[0].y() || y > region.m_bdBox[1].y()) {
2177 MWAW_DEBUG_MSG(("ApplePictParser::readRegion: found eroneous y value: %d\n", y));
2178 break;
2179 }
2180 bool endF = false;
2181 while (remain > 0) {
2182 auto x = static_cast<int>(input->readLong(2));
2183 --remain;
2184 if (x == 0x7fff) {
2185 endF = true;
2186 break;
2187 }
2188 if (x < region.m_bdBox[0].x() || x > region.m_bdBox[1].x()) {
2189 MWAW_DEBUG_MSG(("ApplePictParser::readRegion: found eroneous x value\n"));
2190 break;
2191 }
2192 region.m_points.push_back(MWAWVec2i(x,y));
2193 }
2194 if (!endF) {
2195 MWAW_DEBUG_MSG(("ApplePictParser::readRegion: does not find end of file...\n"));
2196 break;
2197 }
2198 }
2199 if (remain) {
2200 MWAW_DEBUG_MSG(("ApplePictParser::readRegion: find some remaining data ...\n"));
2201 region.m_extra="###,";
2202 }
2203
2204 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
2205 return true;
2206 }
2207
2208 //
2209 // helper function
2210 //
2211 std::string ApplePictParser::getModeName(int mode)
2212 {
2213 switch (mode) {
2214 case 0:
2215 return "srcCopy";
2216 case 1:
2217 return "srcOr";
2218 case 2:
2219 return "srcXOr";
2220 case 3:
2221 return "srcBic";
2222 case 4:
2223 return "notSrcCopy";
2224 case 5:
2225 return "notSrcOr";
2226 case 6:
2227 return "notSrcXOr";
2228 case 7:
2229 return "notSrcBic";
2230 case 8:
2231 return "patCopy";
2232 case 9:
2233 return "patOr";
2234 case 10:
2235 return "patXOr";
2236 case 11:
2237 return "patBic";
2238 case 12:
2239 return "notPatCopy";
2240 case 13:
2241 return "notPatOr";
2242 case 14:
2243 return "notPatXOr";
2244 case 15:
2245 return "notPatBic";
2246 case 23:
2247 return "postscript";
2248 case 32:
2249 return "blend";
2250 case 33:
2251 return "addPin";
2252 case 34:
2253 return "addOver";
2254 case 35:
2255 return "subPin";
2256 case 36:
2257 return "transparent";
2258 case 37:
2259 return "addMax";
2260 case 38:
2261 return "subOver";
2262 case 39:
2263 return "addMin";
2264 case 49:
2265 return "grayishTextOr";
2266 case 50:
2267 return "hilite";
2268 case 64:
2269 return "mask";
2270 default:
2271 break;
2272 }
2273 MWAW_DEBUG_MSG(("ApplePictParser::getModeName: find unknown mode\n"));
2274 std::stringstream s;
2275 s << "##mode=" << mode;
2276 return s.str();
2277 }
2278
2279 ////////////////////////////////////////////////////////////
2280 // read the header
2281 ////////////////////////////////////////////////////////////
2282 bool ApplePictParser::checkHeader(MWAWHeader *header, bool strict)
2283 {
2284 *m_state = ApplePictParserInternal::State();
2285 MWAWInputStreamPtr input = getInput();
2286 if (!input || !input->hasDataFork() || !input->checkPosition(13))
2287 return false;
2288
2289 int vers=0;
2290 for (int st=0; st<2; ++st) {
2291 if (!input->checkPosition(512*st+13))
2292 return false;
2293 long pos=st*512;
2294 input->seek(pos, librevenge::RVNG_SEEK_SET);
2295 libmwaw::DebugStream f;
2296 f << "FileHeader:";
2297 auto dSz=static_cast<int>(input->readULong(2));
2298 if (dSz)
2299 f << "dSz=" << dSz << ",";
2300 int dim[4];
2301 for (int &i : dim) i=static_cast<int>(input->readLong(2));
2302 if (dim[0]>dim[2]||dim[1]>dim[3])
2303 continue;
2304 m_state->m_bdBox=MWAWBox2f(MWAWVec2f(float(dim[1]),float(dim[0])),MWAWVec2f(float(dim[3]),float(dim[2])));
2305 f << "dim=" << m_state->m_bdBox << ",";
2306 long lastFlag = input->readLong(2);
2307 switch (lastFlag) {
2308 case 0x1101:
2309 // some MacDraw Pro pict stores a bigger size 512(file header?)+10?
2310 if (!input->checkPosition(pos+dSz) && (st!=1 && input->size()!=dSz+512+2))
2311 break;
2312 f << "pict1,";
2313 vers = 1;
2314 break;
2315 case 0x11: {
2316 if (!input->checkPosition(pos+40))
2317 break;
2318 if (input->readULong(2) != 0x2ff || input->readULong(2) != 0xC00) break;
2319 int fileVersion = -int(input->readLong(2));
2320 int subvers = -int(input->readLong(2));
2321 float dim2[4];
2322 switch (fileVersion) {
2323 case 1:
2324 f << "pict2[1:" << subvers << "],";
2325 for (float &i : dim2) i=float(input->readLong(4))/65536.f;
2326 if (strict && (dim2[0]>dim2[2]||dim2[1]>dim2[3]))
2327 break;
2328 m_state->m_bdBox=MWAWBox2f(MWAWVec2f(static_cast<float>(dim2[0]),static_cast<float>(dim2[1])),MWAWVec2f(static_cast<float>(dim2[2]),static_cast<float>(dim2[3])));
2329 f << "dim[fixed]=" << m_state->m_bdBox << ",";
2330 vers=2;
2331 break;
2332 case 2: {
2333 f << "pict2[2:" << subvers << "],";
2334 for (int i=0; i<2; ++i) dim2[i]=float(input->readLong(4))/65536.f;
2335 if (strict && (dim2[0]<0 || dim2[1]<=0)) break;
2336 f << "res=" << MWAWVec2f(dim2[1],dim2[0]) << ",";
2337 for (int &i : dim) i=static_cast<int>(input->readLong(2));
2338 if (dim[0]>dim[2]||dim[1]>dim[3])
2339 break;
2340 MWAWBox2f bdbox(MWAWVec2f(static_cast<float>(dim[1]),static_cast<float>(dim[0])),MWAWVec2f(static_cast<float>(dim[3]),static_cast<float>(dim[2])));
2341 if (bdbox.size()[0]>0 && bdbox.size()[1]>0)
2342 m_state->m_bdBox=bdbox;
2343 else
2344 f << "##";
2345 f << "dim[optimal]=" << bdbox << ",";
2346 vers=2;
2347 break;
2348 }
2349 default:
2350 break;
2351 }
2352 if (vers==0 || !input->checkPosition(input->tell()+4)) {
2353 vers=0;
2354 break;
2355 }
2356 input->seek(4, librevenge::RVNG_SEEK_CUR); // reserved
2357 break;
2358 }
2359 default:
2360 break;
2361 }
2362 if (vers) {
2363 ascii().addPos(pos);
2364 ascii().addNote(f.str().c_str());
2365 break;
2366 }
2367 if (st==0) {
2368 ascii().addPos(0);
2369 ascii().addNote("Entries(Pref):");
2370 }
2371 }
2372 if (vers==0) return false;
2373 setVersion(vers);
2374 m_state->m_version=vers;
2375 if (header)
2376 header->reset(MWAWDocument::MWAW_T_APPLEPICT, vers, MWAWDocument::MWAW_K_DRAW);
2377
2378 return true;
2379 }
2380
2381 ////////////////////////////////////////////////////////////
2382 //
2383 // send data
2384 //
2385 ////////////////////////////////////////////////////////////
2386 void ApplePictParser::drawLine(MWAWVec2i const &pt)
2387 {
2388 MWAWVec2f orig(m_state->m_penPosition);
2389 m_state->m_penPosition=pt;
2390 MWAWListenerPtr listener=getGraphicListener();
2391 if (m_state->isInvisible(D_FRAME) || !listener)
2392 return;
2393 MWAWGraphicStyle style;
2394 m_state->updateStyle(D_FRAME, style);
2395 auto shape=MWAWGraphicShape::line(orig, MWAWVec2f(pt));
2396 MWAWPosition pos;
2397 m_state->updatePosition(shape.getBdBox(), pos);
2398 listener->insertShape(pos,shape, style);
2399 }
2400
2401 void ApplePictParser::drawRectangle(ApplePictParser::DrawingMethod method)
2402 {
2403 MWAWListenerPtr listener=getGraphicListener();
2404 if (m_state->isInvisible(method) || !listener)
2405 return;
2406 MWAWGraphicStyle style;
2407 m_state->updateStyle(method, style);
2408 MWAWBox2f rect(m_state->m_rectangle);
2409 if (method==D_ERASE) // the rectangle can be very large
2410 rect=rect.getIntersection(m_state->m_bdBox);
2411 auto shape=MWAWGraphicShape::rectangle(rect);
2412 MWAWPosition pos;
2413 m_state->updatePosition(shape.getBdBox(), pos);
2414 listener->insertShape(pos,shape, style);
2415 }
2416
2417 void ApplePictParser::drawRoundRectangle(ApplePictParser::DrawingMethod method)
2418 {
2419 MWAWListenerPtr listener=getGraphicListener();
2420 if (m_state->isInvisible(method) || !listener)
2421 return;
2422 MWAWGraphicStyle style;
2423 m_state->updateStyle(D_FRAME, style);
2424 auto shape=MWAWGraphicShape::rectangle(MWAWBox2f(m_state->m_roundRectangle), MWAWVec2f(m_state->m_ovalSize));
2425 MWAWPosition pos;
2426 m_state->updatePosition(shape.getBdBox(), pos);
2427 listener->insertShape(pos,shape, style);
2428 }
2429
2430 void ApplePictParser::drawCircle(ApplePictParser::DrawingMethod method)
2431 {
2432 MWAWListenerPtr listener=getGraphicListener();
2433 if (m_state->isInvisible(method) || !listener)
2434 return;
2435 MWAWGraphicStyle style;
2436 m_state->updateStyle(method, style);
2437 auto shape=MWAWGraphicShape::circle(MWAWBox2f(m_state->m_circle));
2438 MWAWPosition pos;
2439 m_state->updatePosition(shape.getBdBox(), pos);
2440 listener->insertShape(pos,shape, style);
2441 }
2442
2443 void ApplePictParser::drawPie(DrawingMethod method, int startAngle, int dAngle)
2444 {
2445 MWAWListenerPtr listener=getGraphicListener();
2446 if (m_state->isInvisible(method) || !listener)
2447 return;
2448 MWAWGraphicStyle style;
2449 m_state->updateStyle(method, style);
2450 //
2451 int angle[2] = { 90-startAngle-dAngle, 90-startAngle };
2452 if (dAngle<0) {
2453 angle[0]=90-startAngle;
2454 angle[1]=90-startAngle-dAngle;
2455 }
2456 if (angle[1]>360) {
2457 int numLoop=int(angle[1]/360)-1;
2458 angle[0]-=numLoop*360;
2459 angle[1]-=numLoop*360;
2460 while (angle[1] > 360) {
2461 angle[0]-=360;
2462 angle[1]-=360;
2463 }
2464 }
2465 if (angle[0] < -360) {
2466 int numLoop=int(angle[0]/360)+1;
2467 angle[0]-=numLoop*360;
2468 angle[1]-=numLoop*360;
2469 while (angle[0] < -360) {
2470 angle[0]+=360;
2471 angle[1]+=360;
2472 }
2473 }
2474
2475 MWAWVec2f axis = 0.5f*MWAWVec2f(m_state->m_pie.size());
2476 // we must compute the real bd box
2477 float minVal[2] = { 0, 0 }, maxVal[2] = { 0, 0 };
2478 int limitAngle[2];
2479 for (int i = 0; i < 2; i++)
2480 limitAngle[i] = (angle[i] < 0) ? int(angle[i]/90)-1 : int(angle[i]/90);
2481 for (int bord = limitAngle[0]; bord <= limitAngle[1]+1; bord++) {
2482 float ang = (bord == limitAngle[0]) ? float(angle[0]) :
2483 (bord == limitAngle[1]+1) ? float(angle[1]) : float(90 * bord);
2484 ang *= float(M_PI/180.);
2485 float const actVal[2] = { axis[0] *std::cos(ang), -axis[1] *std::sin(ang)};
2486 if (actVal[0] < minVal[0]) minVal[0] = actVal[0];
2487 else if (actVal[0] > maxVal[0]) maxVal[0] = actVal[0];
2488 if (actVal[1] < minVal[1]) minVal[1] = actVal[1];
2489 else if (actVal[1] > maxVal[1]) maxVal[1] = actVal[1];
2490 }
2491 MWAWVec2f center(m_state->m_pie.center());
2492 MWAWBox2f realBox(MWAWVec2f(center[0]+minVal[0],center[1]+minVal[1]),
2493 MWAWVec2f(center[0]+maxVal[0],center[1]+maxVal[1]));
2494 auto shape = method==D_FRAME ?
2495 MWAWGraphicShape::arc(realBox, MWAWBox2f(m_state->m_pie), MWAWVec2f(float(angle[0]),float(angle[1]))) :
2496 MWAWGraphicShape::pie(realBox, MWAWBox2f(m_state->m_pie), MWAWVec2f(float(angle[0]),float(angle[1])));
2497 MWAWPosition pos;
2498 m_state->updatePosition(shape.getBdBox(), pos);
2499 listener->insertShape(pos,shape, style);
2500 }
2501
2502 void ApplePictParser::drawPolygon(ApplePictParser::DrawingMethod method)
2503 {
2504 if (m_state->m_points.empty()) {
2505 MWAW_DEBUG_MSG(("ApplePictParser::drawPolygon: can not find the main polygon\n"));
2506 return;
2507 }
2508 MWAWListenerPtr listener=getGraphicListener();
2509 if (m_state->isInvisible(method) || !listener)
2510 return;
2511 // first compute the bdbox
2512 MWAWGraphicShape shape;
2513 shape.m_type=MWAWGraphicShape::Polygon;
2514 MWAWBox2f box(MWAWVec2f(m_state->m_points[0]),MWAWVec2f(m_state->m_points[0]));
2515 shape.m_vertices.push_back(MWAWVec2f(m_state->m_points[0]));
2516 for (size_t i=1; i<m_state->m_points.size(); ++i) {
2517 box=box.getUnion(MWAWBox2f(MWAWVec2f(m_state->m_points[i]),MWAWVec2f(m_state->m_points[i])));
2518 shape.m_vertices.push_back(MWAWVec2f(m_state->m_points[i]));
2519 }
2520 shape.m_bdBox=box;
2521 MWAWGraphicStyle style;
2522 m_state->updateStyle(method, style);
2523 MWAWPosition pos;
2524 m_state->updatePosition(shape.getBdBox(), pos);
2525 listener->insertShape(pos,shape, style);
2526 }
2527
2528 void ApplePictParser::drawText(MWAWEntry const &entry)
2529 {
2530 MWAWListenerPtr listener=getGraphicListener();
2531 if (!listener || !listener->canWriteText()) {
2532 MWAW_DEBUG_MSG(("ApplePictParser::drawText: can not find the listener\n"));
2533 return;
2534 }
2535 MWAWGraphicStyle style;
2536 m_state->updateStyle(D_TEXT, style);
2537 MWAWFont font(m_state->m_font);
2538 font.setColor(style.m_lineColor);
2539 listener->setFont(font);
2540 if (!entry.valid())
2541 return;
2542 MWAWInputStreamPtr input = getInput();
2543 long actPos=input->tell();
2544 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
2545 long endPos=entry.end();
2546 while (!input->isEnd() && input->tell()<endPos) {
2547 auto c = static_cast<char>(input->readULong(1));
2548 if (c==0) {
2549 MWAW_DEBUG_MSG(("ApplePictParser::drawText: find char 0\n"));
2550 continue;
2551 }
2552 switch (c) {
2553 case 9:
2554 listener->insertTab();
2555 break;
2556 case 0xd:
2557 listener->insertEOL();
2558 break;
2559 default:
2560 listener->insertCharacter(static_cast<unsigned char>(c), input, entry.end());
2561 break;
2562 }
2563 }
2564 input->seek(actPos, librevenge::RVNG_SEEK_SET);
2565 }
2566
2567 #if 0
2568 static int pictId=0;
2569 #endif
2570 void ApplePictParser::drawBitmap(ApplePictParserInternal::Bitmap const &bitmap)
2571 {
2572 MWAWListenerPtr listener=getGraphicListener();
2573 if (!listener)
2574 return;
2575 MWAWEmbeddedObject picture;
2576 if (!bitmap.get(picture))
2577 return;
2578 #if 0
2579 std::stringstream s;
2580 s << "PICT-" << pictId++ << ".png";
2581 MWAWEmbeddedObject obj;
2582 if (!picture.m_dataList.empty())
2583 libmwaw::Debug::dumpFile(picture.m_dataList[0], s.str().c_str());
2584 #endif
2585 MWAWGraphicStyle style;
2586 MWAWPosition pos;
2587 if (bitmap.m_dst.size()[0]>0 && bitmap.m_dst.size()[1]>0)
2588 m_state->updatePosition(MWAWBox2f(bitmap.m_dst), pos);
2589 else
2590 m_state->updatePosition(MWAWBox2f(bitmap.m_rect), pos);
2591 listener->insertPicture(pos,picture);
2592 }
2593
2594 void ApplePictParser::drawPixmap(ApplePictParserInternal::Pixmap const &pixmap)
2595 {
2596 MWAWListenerPtr listener=getGraphicListener();
2597 if (!listener)
2598 return;
2599 MWAWEmbeddedObject picture;
2600 if (!pixmap.get(picture))
2601 return;
2602 #if 0
2603 std::stringstream s;
2604 s << "PICT-" << pictId++ << ".png";
2605 MWAWEmbeddedObject obj;
2606 if (!picture.m_dataList.empty())
2607 libmwaw::Debug::dumpFile(picture.m_dataList[0], s.str().c_str());
2608 #endif
2609 MWAWGraphicStyle style;
2610 MWAWPosition pos;
2611 m_state->updatePosition(MWAWBox2f(pixmap.m_dst), pos);
2612 listener->insertPicture(pos,picture);
2613 }
2614
2615 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: