Debian Codegraph @

src/lib/MacDraft5StyleManager.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <cmath>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWFontConverter.hxx"
43 #include "MWAWGraphicListener.hxx"
44 #include "MWAWGraphicShape.hxx"
45 #include "MWAWGraphicStyle.hxx"
46 #include "MWAWHeader.hxx"
47 #include "MWAWParagraph.hxx"
48 #include "MWAWPictBitmap.hxx"
49 #include "MWAWPictData.hxx"
50 #include "MWAWPrinter.hxx"
51 #include "MWAWPosition.hxx"
52 #include "MWAWRSRCParser.hxx"
53 #include "MWAWSubDocument.hxx"
54
55 #include "MacDraft5Parser.hxx"
56
57 #include "MacDraft5StyleManager.hxx"
58
59 /** Internal: the structures of a MacDraft5StyleManager */
60 namespace MacDraft5StyleManagerInternal
61 {
62 /** Internal and low level: a class used to read pack/unpack color pixmap of a MacDraf5StyleManager
63
64 \note parse only unpacked pixmap, if packed pixmap can exist, the code of ApplePictParserInternal::Pixmap
65 must be used
66 */
67 struct Pixmap {
68 Pixmap()
69 : m_rowBytes(0)
70 , m_rect()
71 , m_version(-1)
72 , m_packType(0)
73 , m_packSize(0)
74 , m_pixelType(0)
75 , m_pixelSize(0)
76 , m_compCount(0)
77 , m_compSize(0)
78 , m_planeBytes(0)
79 , m_colorTable()
80 , m_indices()
81 , m_colors()
82 , m_mode(0)
83 {
84 for (auto &res : m_resolution) res=0;
85 }
86
87 //! operator<< for Pixmap
88 friend std::ostream &operator<< (std::ostream &o, Pixmap const &f)
89 {
90 o << "rDim=" << f.m_rowBytes << ", " << f.m_rect;
91 o << ", resol=" << f.m_resolution[0] << "x" << f.m_resolution[1];
92 return o;
93 }
94
95 //! parses the pixmap data zone
96 bool readPixmapData(MWAWInputStream &input)
97 {
98 int W = m_rect.size().x(), H = m_rect.size().y();
99
100 int const nPlanes = 1;
101 int nBytes = 3, rowBytes = m_rowBytes;
102 int numValuesByInt = 1;
103 auto numColors = static_cast<int>(m_colorTable.size());
104 int maxColorsIndex = -1;
105
106 switch (m_pixelSize) {
107 case 1:
108 case 2:
109 case 4:
110 case 8: { // indices (associated to a color map)
111 nBytes = 1;
112 numValuesByInt = 8/m_pixelSize;
113 int numValues = (W+numValuesByInt-1)/numValuesByInt;
114 if (m_rowBytes < numValues || m_rowBytes > numValues+10) {
115 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::Pixmap::readPixmapData invalid number of rowsize : %d, pixelSize=%d, W=%d\n", m_rowBytes, m_pixelSize, W));
116 return false;
117 }
118 if (numColors == 0) {
119 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::Pixmap::readPixmapData: readPixmapData no color table \n"));
120 return false;
121 }
122 break;
123 }
124 case 16:
125 nBytes = 2;
126 break;
127 case 32:
128 nBytes=4;
129 break;
130 default:
131 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::Pixmap::readPixmapData: do not known how to read pixelsize=%d \n", m_pixelSize));
132 return false;
133 }
134 if (m_pixelSize <= 8)
135 m_indices.resize(size_t(H*W));
136 else {
137 if (rowBytes != W * nBytes * nPlanes) {
138 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::Pixmap::readPixmapData: find W=%d pixelsize=%d, rowSize=%d\n", W, m_pixelSize, m_rowBytes));
139 if (rowBytes < W * nBytes * nPlanes)
140 return false;
141 }
142 m_colors.resize(size_t(H*W));
143 }
144 long maxValues = m_pixelSize<=8 ? long(1 << m_pixelSize)-1 : 0;
145 std::vector<unsigned char> values;
146 values.resize(size_t(m_rowBytes+24), static_cast<unsigned char>(0));
147
148 for (int y = 0; y < H; y++) {
149 unsigned long numR = 0;
150 unsigned char const *data = input.read(size_t(m_rowBytes), numR);
151 if (!data || int(numR) != m_rowBytes) {
152 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::Pixmap::readPixmapData: readColors can not read line %d/%d (%d chars)\n", y, H, m_rowBytes));
153 return false;
154 }
155 for (size_t j = 0; j < size_t(m_rowBytes); j++)
156 values[j]=data[j];
157
158 //
159 // ok, we can add it in the pictures
160 //
161 int wPos = y*W;
162 if (m_pixelSize <= 8) { // indexed
163 for (int x = 0, rPos = 0; x < W;) {
164 unsigned char val = values[size_t(rPos++)];
165 for (int v = numValuesByInt-1; v >=0; v--) {
166 int index = int((val>>(v*m_pixelSize))&maxValues);
167 if (index > maxColorsIndex) maxColorsIndex = index;
168 m_indices[size_t(wPos++)] = index;
169 if (++x >= W) break;
170 }
171 }
172 }
173 else if (m_pixelSize == 16) {
174 for (int x = 0, rPos = 0; x < W; x++) {
175 unsigned int val = 256*static_cast<unsigned int>(values[size_t(rPos)])+static_cast<unsigned int>(values[size_t(rPos+1)]);
176 rPos+=2;
177 m_colors[size_t(wPos++)]=MWAWColor((val>>7)& 0xF8, (val>>2) & 0xF8, static_cast<unsigned char>(val << 3));
178 }
179 }
180 else { // nPlanes=1
181 for (int x = 0, rPos = 0; x < W; x++) {
182 if (nBytes==4) rPos++;
183 m_colors[size_t(wPos++)]=MWAWColor(values[size_t(rPos)], values[size_t(rPos+1)], values[size_t(rPos+2)]);
184 rPos+=3;
185 }
186 }
187 }
188 if (maxColorsIndex >= numColors) {
189 // can be ok for a pixpat ; in this case:
190 // maxColorsIndex -> foregroundColor, numColors -> backGroundColor
191 // and intermediate index fills with intermediate colors
192 int numUnset = maxColorsIndex-numColors+1;
193
194 int decGray = (numUnset==1) ? 0 : 255/(numUnset-1);
195 for (int i = 0; i < numUnset; i++)
196 m_colorTable.push_back(MWAWColor(static_cast<unsigned char>(255-i*decGray), static_cast<unsigned char>(255-i*decGray), static_cast<unsigned char>(255-i*decGray)));
197 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::Pixmap::readPixmapData: find index=%d >= numColors=%d\n", maxColorsIndex, numColors));
198
199 return true;
200 }
201 return true;
202 }
203 //! returns the pixmap
204 bool get(MWAWEmbeddedObject &picture, MWAWVec2i &pictSize, MWAWColor &avColor) const
205 {
206 pictSize=m_rect.size();
207 int W = m_rect.size().x();
208 if (W <= 0) return false;
209 if (!m_colorTable.empty() && m_indices.size()) {
210 int nRows = int(m_indices.size())/W;
211 MWAWPictBitmapIndexed pixmap(MWAWVec2i(W,nRows));
212 if (!pixmap.valid()) return false;
213
214 pixmap.setColors(m_colorTable);
215
216 size_t numColor=m_colorTable.size();
217 std::vector<int> colorByIndices(numColor,0);
218 size_t rPos = 0;
219 for (int i = 0; i < nRows; i++) {
220 for (int x = 0; x < W; x++) {
221 int id=m_indices[rPos++];
222 if (id<0 || id>=static_cast<int>(numColor)) {
223 static bool first=true;
224 if (first) {
225 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::Pixmap::get: find some bad index\n"));
226 first=false;
227 }
228 pixmap.set(x, i, 0);
229 }
230 else {
231 ++colorByIndices[size_t(id)];
232 pixmap.set(x, i, id);
233 }
234 }
235 }
236 float totalCol[3]= {0,0,0};
237 long numCols=0;
238 for (size_t i=0; i<numColor; ++i) {
239 if (!colorByIndices[i]) continue;
240 numCols+=colorByIndices[i];
241 totalCol[0]+=float(colorByIndices[i])*float(m_colorTable[i].getRed());
242 totalCol[1]+=float(colorByIndices[i])*float(m_colorTable[i].getGreen());
243 totalCol[2]+=float(colorByIndices[i])*float(m_colorTable[i].getBlue());
244 }
245 if (numCols==0)
246 avColor=MWAWColor::black();
247 else
248 avColor=MWAWColor(static_cast<unsigned char>(totalCol[0]/float(numCols)),
249 static_cast<unsigned char>(totalCol[1]/float(numCols)),
250 static_cast<unsigned char>(totalCol[2]/float(numCols)));
251 return pixmap.getBinary(picture);
252 }
253
254 if (m_colors.size()) {
255 int nRows = int(m_colors.size())/W;
256 MWAWPictBitmapColor pixmap(MWAWVec2i(W,nRows));
257 if (!pixmap.valid()) return false;
258
259 size_t rPos = 0;
260 long numCols=0;
261 float totalCol[3]= {0,0,0};
262 for (int i = 0; i < nRows; i++) {
263 for (int x = 0; x < W; x++) {
264 MWAWColor col=m_colors[rPos++];
265 pixmap.set(x, i, col);
266 totalCol[0]+=float(col.getRed());
267 totalCol[1]+=float(col.getGreen());
268 totalCol[2]+=float(col.getBlue());
269 ++numCols;
270 }
271 }
272 if (numCols==0)
273 avColor=MWAWColor::black();
274 else
275 avColor=MWAWColor(static_cast<unsigned char>(totalCol[0]/float(numCols)),
276 static_cast<unsigned char>(totalCol[1]/float(numCols)),
277 static_cast<unsigned char>(totalCol[2]/float(numCols)));
278
279 return pixmap.getBinary(picture);
280 }
281
282 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::Pixmap::get: can not find any indices or colors \n"));
283 return false;
284 }
285
286 //! the num of bytes used to store a row
287 int m_rowBytes;
288 MWAWBox2i m_rect /** the pixmap rectangle */;
289 int m_version /** the pixmap version */;
290 int m_packType /** the packing format */;
291 long m_packSize /** size of data in the packed state */;
292 int m_resolution[2] /** horizontal/vertical definition */;
293 int m_pixelType /** format of pixel image */;
294 int m_pixelSize /** physical bit by image */;
295 int m_compCount /** logical components per pixels */;
296 int m_compSize /** logical bits by components */;
297 long m_planeBytes /** offset to the next plane */;
298
299 //! the color table
300 std::vector<MWAWColor> m_colorTable;
301 //! the pixmap indices
302 std::vector<int> m_indices;
303 //! the colors
304 std::vector<MWAWColor> m_colors;
305 //! the encoding mode ?
306 int m_mode;
307 };
308
309 ////////////////////////////////////////
310 //! Internal: the state of a MacDraft5StyleManager
311 struct State {
312 //! constructor
313 State()
314 : m_dataEnd(-1)
315 , m_rsrcBegin(-1)
316 , m_arrowList()
317 , m_colorList()
318 , m_patternList()
319 , m_dashList()
320 , m_beginToBitmapEntryMap()
321 , m_bitmapIdToPixmapMap()
322 , m_pixIdToPixmapMap()
323 , m_pixIdToPatternIdMap()
324 {
325 }
326 //! returns an arrow if possible
327 bool getArrow(int id, MWAWGraphicStyle::Arrow &arrow)
328 {
329 if (m_arrowList.empty()) initArrows();
330 if (id<=0 || id>int(m_arrowList.size())) {
331 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::getArrow: can not find arrow %d\n", id));
332 return false;
333 }
334 arrow=m_arrowList[size_t(id-1)];
335 return true;
336 }
337 //! returns a color if possible
338 bool getColor(int id, MWAWColor &col)
339 {
340 if (m_colorList.empty()) initColors();
341 if (id<=0 || id>int(m_colorList.size())) {
342 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::getColor: can not find color %d\n", id));
343 return false;
344 }
345 col=m_colorList[size_t(id-1)];
346 return true;
347 }
348
349 //! returns a pattern if possible
350 bool getPattern(int id, MWAWGraphicStyle::Pattern &pat)
351 {
352 if (m_patternList.empty()) initPatterns();
353 if (id<=0 || id>int(m_patternList.size())) {
354 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::getPattern: can not find pattern %d\n", id));
355 return false;
356 }
357 pat=m_patternList[size_t(id-1)];
358 return true;
359 }
360 //! returns the dash
361 bool getDash(int id, std::vector<float> &dash)
362 {
363 if (m_dashList.empty()) initDashs();
364 if (id<0 || id>=int(m_dashList.size())) {
365 MWAW_DEBUG_MSG(("MacDraft5StyleManagerInternal::getDash: can not find dash %d\n", id));
366 return false;
367 }
368 dash=m_dashList[size_t(id)];
369 return true;
370 }
371 //! init the arrow list
372 void initArrows();
373 //! init the color list
374 void initColors();
375 //! init the patterns list
376 void initPatterns();
377 //! init the dashs list
378 void initDashs();
379
380 //! the end of the main data zone
381 long m_dataEnd;
382 //! the begin of the rsrc data
383 long m_rsrcBegin;
384 //! the arrow list
385 std::vector<MWAWGraphicStyle::Arrow> m_arrowList;
386 //! the color list
387 std::vector<MWAWColor> m_colorList;
388 //! the patterns list
389 std::vector<MWAWGraphicStyle::Pattern> m_patternList;
390 //! the list of dash
391 std::vector< std::vector<float> > m_dashList;
392 //! a map file position to entry ( used to stored intermediar zones )
393 std::map<long, MWAWEntry> m_beginToBitmapEntryMap;
394 //! a map bitmapId to pixmap map
395 std::map<int, std::shared_ptr<Pixmap> > m_bitmapIdToPixmapMap;
396 //! a map pixmapId to pixmap map
397 std::map<int, std::shared_ptr<Pixmap> > m_pixIdToPixmapMap;
398 //! a map pixmapId to patternId map
399 std::map<int, size_t> m_pixIdToPatternIdMap;
400 };
401
402 void State::initArrows()
403 {
404 if (!m_arrowList.empty())
405 return;
406 // -- triangle
407 m_arrowList.push_back(MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(3000,3000)),
408 "M1500 0l1500 3000h-3000zM1500 447l-1176 2353h2353z", false));
409 m_arrowList.push_back(MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(20,30)), "m10 0l-10 30h20z", false));
410 m_arrowList.push_back(MWAWGraphicStyle::Arrow(7, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(21590,27940)),
411 "M 4568,5865 C 5566,5865 6421,6329 6936,7063 L 4568,2063 2200,7063 C 2716,6329 3570,5865 4568,5865 Z M 4529,6665 C 3041,6665 1768,7358 1000,8451 L 4529,1000 8057,8451 C 7289,7358 6016,6665 4529,6665 Z", false));
412 m_arrowList.push_back(MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(1131,1580)),
413 "M1013 1491l118 89-567-1580-564 1580 114-85 136-68 148-46 161-17 161 13 153 46z", false));
414 m_arrowList.push_back(MWAWGraphicStyle::Arrow(8, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(3000,3000)),
415 "M1500 0l1500 3000h-3000zM1500 447l-1176 2353h2353z", false));
416 m_arrowList.push_back(MWAWGraphicStyle::Arrow(8, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(20,30)), "m10 0l-10 30h20z", false));
417 // -- circle/disk
418 m_arrowList.push_back(MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(3000,3000)), "M1500 3000c-276 0-511-63-750-201s-411-310-549-549-201-474-201-750 63-511 201-750 310-411 549-549 474-201 750-201 511 63 750 201 411 310 549 549 201 474 201 750-63 511-201 750-310 411-549 549-474 201-750 201zM1500 2800c-239 0-443-55-650-174s-356-269-476-476-174-411-174-650 55-443 174-650 269-356 476-476c207-119 411-174 650-174s443 55 650 174c207 120 356 269 476 476s174 411 174 650-55 443-174 650-269 356-476 476c-207 119-411 174-650 174z", false));
419 m_arrowList.push_back(MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(1131,1131)), "M462 1118l-102-29-102-51-93-72-72-93-51-102-29-102-13-105 13-102 29-106 51-102 72-89 93-72 102-50 102-34 106-9 101 9 106 34 98 50 93 72 72 89 51 102 29 106 13 102-13 105-29 102-51 102-72 93-93 72-98 51-106 29-101 13z", false));
420 m_arrowList.push_back(MWAWGraphicStyle::Arrow(10, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(3000,3000)), "M1500 3000c-276 0-511-63-750-201s-411-310-549-549-201-474-201-750 63-511 201-750 310-411 549-549 474-201 750-201 511 63 750 201 411 310 549 549 201 474 201 750-63 511-201 750-310 411-549 549-474 201-750 201zM1500 2800c-239 0-443-55-650-174s-356-269-476-476-174-411-174-650 55-443 174-650 269-356 476-476c207-119 411-174 650-174s443 55 650 174c207 120 356 269 476 476s174 411 174 650-55 443-174 650-269 356-476 476c-207 119-411 174-650 174z", false));
421 m_arrowList.push_back(MWAWGraphicStyle::Arrow(10, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(1131,1131)), "M462 1118l-102-29-102-51-93-72-72-93-51-102-29-102-13-105 13-102 29-106 51-102 72-89 93-72 102-50 102-34 106-9 101 9 106 34 98 50 93 72 72 89 51 102 29 106 13 102-13 105-29 102-51 102-72 93-93 72-98 51-106 29-101 13z", false));
422
423 // -- line
424 m_arrowList.push_back(MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(1000,1000),MWAWVec2i(8801, 9501)),
425 "M 1000,1000 L 2000,1000 9800,10499 8588,10500 5900,6900 6000,10400 4900,10400 4800,5700 1000,1000 Z", false));
426 m_arrowList.push_back(MWAWGraphicStyle::Arrow(10, MWAWBox2i(MWAWVec2i(1000,1000),MWAWVec2i(8801, 9501)),
427 "M 1000,1000 L 2000,1000 9800,10499 8588,10500 5900,6900 6000,10400 4900,10400 4800,5700 1000,1000 Z", false));
428 // -- cross
429 m_arrowList.push_back(MWAWGraphicStyle::Arrow(10, MWAWBox2i(MWAWVec2i(1000, 900),MWAWVec2i(5001,5201)),
430 "M 6000,5500 L 5600,6100 3900,4500 3900,6000 3000,6000 3000,4500 1400,6100 1000,5500 2900,3551 1000,1500 1600,900 3500,2936 5300,1100 5900,1500 4025,3475 6000,5500 Z", false));
431 // -- triangle
432 m_arrowList.push_back(MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(3000,3000)),
433 "M1500 0l1500 3000h-3000zM1500 447l-1176 2353h2353z", false));
434 m_arrowList.push_back(MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(1122,2243)), "M0 2108v17 17l12 42 30 34 38 21 43 4 29-8 30-21 25-26 13-34 343-1532 339 1520 13 42 29 34 39 21 42 4 42-12 34-30 21-42v-39-12l-4 4-440-1998-9-42-25-39-38-25-43-8-42 8-38 25-26 39-8 42z", false));
435 }
436
437 void State::initColors()
438 {
439 if (!m_colorList.empty()) return;
440 for (int i=0; i<131; ++i) {
441 static uint32_t const colors[131] = {
442 0xffffff,0x000000,0x7f7f7f,0xdd0806,0x008011,0x0000d4,0x02abea,0xf20884,
443 0xfcf305,0xff1b00,0xff3700,0xff5300,0xff6f00,0xff8b00,0xffa700,0xffc300,
444 0xffdf00,0xfffb00,0xe8ff00,0xccff00,0xb0ff00,0x94ff00,0x79ff00,0x5dff00,
445 0x41ff00,0x25ff00,0x09ff00,0x00ff12,0x00ff2e,0x00ff4a,0x00ff66,0x00ff82,
446 0x00ff9e,0x00ffba,0x00ffd6,0x00fff2,0x00f2ff,0x00d6ff,0x00baff,0x009eff,
447 0x0082ff,0x0066ff,0x004aff,0x002eff,0x0012ff,0x0900ff,0x2500ff,0x4100ff,
448 0x5d00ff,0x7800ff,0x9400ff,0xb000ff,0xcc00ff,0xe800ff,0xff00fb,0xff00df,
449 0xff00c3,0xff00a7,0xff008b,0xff006f,0xff0053,0xff0037,0xff001b,0xff0000,
450 0xc31500,0xc32a00,0xc33f00,0xc35500,0xc36a00,0xc37f00,0xc39500,0x3a3a3a,
451 0x535353,0x6d6d6d,0x868686,0xa0a0a0,0xb9b9b9,0xd2d2d2,0xeaeaea,0xcccccc,
452 0x999999,0x878787,0x00ffff
453 };
454 m_colorList.push_back(MWAWColor(colors[i]));
455 }
456 }
457
458 void State::initPatterns()
459 {
460 if (!m_patternList.empty()) return;
461 for (int i=0; i<71; ++i) {
462 static uint16_t const patterns[71*4] = {
463 0x0,0x0,0x0,0x0,0xffff,0xffff,0xffff,0xffff,0x40,0x400,0x10,0x100,0x8040,0x2010,0x804,0x201,
464 0x102,0x408,0x1020,0x4080,0x842,0x90,0x440,0x1001,0xe070,0x381c,0xe07,0x83c1,0x8307,0xe1c,0x3870,0xe0c1,
465 0x8000,0x0,0x800,0x0,0x42a,0x4025,0x251,0x2442,0x4422,0x88,0x4422,0x88,0x1122,0x4400,0x1122,0x4400,
466 0x8000,0x800,0x8000,0x800,0x4aa4,0x8852,0x843a,0x4411,0x8844,0x2211,0x8844,0x2211,0x1122,0x4488,0x1122,0x4488,
467 0x8800,0x2200,0x8800,0x2200,0x4cd2,0x532d,0x9659,0x46b3,0x99cc,0x6633,0x99cc,0x6633,0x3366,0xcc99,0x3366,0xcc99,
468 0x8822,0x8822,0x8822,0x8822,0xdbbe,0xedbb,0xfeab,0xbeeb,0xcc00,0x0,0x3300,0x0,0x101,0x1010,0x101,0x1010,
469 0xaa55,0xaa55,0xaa55,0xaa55,0xf7bd,0xff6f,0xfbbf,0xeffe,0x2040,0x8000,0x804,0x200,0x40a0,0x0,0x40a,0x0,
470 0x77dd,0x77dd,0x77dd,0x77dd,0x8244,0x3944,0x8201,0x101,0xff00,0x0,0xff00,0x0,0x8888,0x8888,0x8888,0x8888,
471 0x8142,0x3c18,0x183c,0x4281,0xb130,0x31b,0xb8c0,0xc8d,0x6c92,0x8282,0x4428,0x1000,0xff80,0x8080,0xff80,0x8080,
472 0x8142,0x2418,0x1020,0x4080,0xff80,0x8080,0xff08,0x808,0x8080,0x413e,0x808,0x14e3,0xff88,0x8888,0xff88,0x8888,
473 0xff80,0x8080,0x8080,0x8080,0xbf00,0xbfbf,0xb0b0,0xb0b0,0xaa00,0x8000,0x8800,0x8000,0xaa44,0xaa11,0xaa44,0xaa11,
474 0x8244,0x2810,0x2844,0x8201,0x8,0x142a,0x552a,0x1408,0x1038,0x7cfe,0x7c38,0x1000,0x1020,0x54aa,0xff02,0x408,
475 0x8080,0x8080,0x8094,0xaa55,0x804,0x2a55,0xff40,0x2010,0x7789,0x8f8f,0x7798,0xf8f8,0x8814,0x2241,0x8800,0xaa00,
476 0x77eb,0xddbe,0x77ff,0x55ff,0x1022,0x408a,0x4022,0x108a,0xefdd,0xbf75,0xbfdd,0xef75,0x9f90,0x909f,0xf909,0x9f9,
477 0xf078,0x2442,0x870f,0x1221,0xfe82,0xfeee,0xef28,0xefee,0xf9fc,0x664f,0x9f3f,0x66f3,0xaf5f,0xaf5f,0xd0b,0xd0b,
478 0xa011,0xa1c,0x2844,0x82c1,0xf0f0,0xf0f0,0xf0f,0xf0f,0xc864,0x3219,0x9923,0x468c,0xc000,0x0,0xc,0x1221,
479 0x101,0x8040,0x2020,0x4080,0x8844,0x2211,0x1121,0x4284,0xf87c,0x3e1f,0x1121,0x4284,0x1c32,0x71f0,0xf8e4,0xc281,
480 0xd86c,0x3613,0xa141,0x8205,0x810,0x1038,0xcf07,0x204,0x8851,0x2254,0x8814,0x2241
481 };
482
483 MWAWGraphicStyle::Pattern pat;
484 pat.m_dim=MWAWVec2i(8,8);
485 pat.m_data.resize(8);
486 pat.m_colors[0]=MWAWColor::white();
487 pat.m_colors[1]=MWAWColor::black();
488 uint16_t const *patPtr=&patterns[4*i];
489 for (size_t j=0; j<8; j+=2, ++patPtr) {
490 pat.m_data[j]=uint8_t((*patPtr)>>8);
491 pat.m_data[j+1]=uint8_t((*patPtr)&0xFF);
492 }
493 m_patternList.push_back(pat);
494 }
495 }
496
497 void State::initDashs()
498 {
499 if (!m_dashList.empty()) return;
500 std::vector<float> dash;
501 // 0: full
502 m_dashList.push_back(dash);
503 // 1: 6x2
504 dash.push_back(6);
505 dash.push_back(2);
506 m_dashList.push_back(dash);
507 // 2: 12x2
508 dash[0]=12;
509 m_dashList.push_back(dash);
510 // 3: 24x3
511 dash[0]=24;
512 dash[1]=3;
513 m_dashList.push_back(dash);
514 // 4: 48x4
515 dash[0]=48;
516 dash[1]=4;
517 m_dashList.push_back(dash);
518 // 5: 6,2,1,2
519 dash.resize(4);
520 dash[0]=6;
521 dash[1]=dash[3]=2;
522 dash[2]=1;
523 m_dashList.push_back(dash);
524 // 6: 12,2,1,2
525 dash[0]=12;
526 m_dashList.push_back(dash);
527 // 7: 24,3,2,3
528 dash[0]=24;
529 dash[1]=dash[3]=3;
530 dash[2]=2;
531 m_dashList.push_back(dash);
532 // 8: 48,4,2,4
533 dash[0]=48;
534 dash[1]=dash[3]=4;
535 dash[2]=2;
536 m_dashList.push_back(dash);
537 // 9:6,2,1,2,1,2,
538 dash.resize(6);
539 dash[0]=6;
540 dash[1]=dash[3]=dash[5]=2;
541 dash[2]=dash[4]=1;
542 m_dashList.push_back(dash);
543 // 10:12,2,1,2,1,2,
544 dash[0]=12;
545 m_dashList.push_back(dash);
546 // 11:24,3,2,2,2,3
547 dash[0]=24;
548 dash[2]=dash[3]=dash[4]=2;
549 dash[1]=dash[5]=3;
550 m_dashList.push_back(dash);
551 // 12: 48,4,2,2,2,4
552 dash[0]=48;
553 dash[1]=dash[5]=4;
554 m_dashList.push_back(dash);
555 // 13: 12,2,1,2,1,2,1,2
556 dash.resize(8);
557 dash[0]=6;
558 dash[1]=dash[3]=dash[5]=dash[7]=2;
559 dash[2]=dash[4]=dash[6]=1;
560 m_dashList.push_back(dash);
561 // 14: 24,3,2,2,2,2,2,3
562 dash[0]=24;
563 dash[2]=dash[3]=dash[4]=dash[5]=dash[6]=2;
564 dash[1]=dash[7]=3;
565 m_dashList.push_back(dash);
566 // 15: 48,4,2,3,2,3,2,4
567 dash[0]=48;
568 dash[1]=dash[7]=4;
569 dash[2]=dash[4]=dash[6]=2;
570 dash[3]=dash[5]=3;
571 m_dashList.push_back(dash);
572 }
573
574 }
575
576 ////////////////////////////////////////////////////////////
577 // constructor/destructor, ...
578 ////////////////////////////////////////////////////////////
579 MacDraft5StyleManager::MacDraft5StyleManager(MacDraft5Parser &parser)
580 : m_parser(parser)
581 , m_parserState(parser.getParserState())
582 , m_state(new MacDraft5StyleManagerInternal::State)
583 {
584 }
585
586 MacDraft5StyleManager::~MacDraft5StyleManager()
587 {
588 }
589
590 long MacDraft5StyleManager::getEndDataPosition() const
591 {
592 return m_state->m_dataEnd;
593 }
594
595 bool MacDraft5StyleManager::getColor(int colId, MWAWColor &color) const
596 {
597 return m_state->getColor(colId, color);
598 }
599
600 std::string MacDraft5StyleManager::updateArrows(int startId, int endId, MWAWGraphicStyle &style)
601 {
602 if (style.m_lineWidth<=0) return "";
603 libmwaw::DebugStream f;
604 if (startId) {
605 if (m_state->getArrow(startId, style.m_arrows[0])) {
606 style.m_arrows[0].m_width *= std::sqrt(style.m_lineWidth);
607 f << "start[arrow]=[" << style.m_arrows[0] << "],";
608 }
609 else
610 f << "##arrow[startId]=" << startId << ",";
611 }
612 if (endId) {
613 if (m_state->getArrow(endId, style.m_arrows[1])) {
614 style.m_arrows[1].m_width *= std::sqrt(style.m_lineWidth);
615 f << "end[arrow]=[" << style.m_arrows[1] << "],";
616 }
617 else
618 f << "##arrow[endId]=" << endId << ",";
619 }
620 return f.str();
621 }
622
623 std::string MacDraft5StyleManager::updateLineStyle(int type, int id, int dashId, MWAWGraphicStyle &style)
624 {
625 libmwaw::DebugStream f;
626 switch (type) {
627 case 0:
628 style.m_lineWidth=0;
629 f << "no[line],";
630 break;
631 case 1: { // use color
632 MWAWColor color;
633 if (id==0) {
634 style.m_lineWidth=0;
635 f << "no[color],";
636 }
637 else if (m_state->getColor(id, color)) {
638 if (!color.isBlack())
639 f << "col[line]=" << color << ",";
640 style.m_lineColor=color;
641 }
642 else
643 f << "###colId=" << id << ",";
644 break;
645 }
646 case 2: {
647 f << "opacity[line]=" << float(id)/255 << "%,";
648 style.m_lineOpacity=float(id)/255.f;
649 break;
650 }
651 case 3: { // use pattern
652 MWAWGraphicStyle::Pattern pattern;
653 if (id==0) {
654 style.m_lineWidth=0;
655 f << "no[linePattern],";
656 }
657 else if (m_state->getPattern(id, pattern)) {
658 f << "usePattern[line]=" << id << ",";
659 pattern.getAverageColor(style.m_lineColor);
660 }
661 else
662 f << "###patId=" << id << ",";
663 break;
664 }
665 default:
666 MWAW_DEBUG_MSG(("MacDraft5StyleManager::updateLineStyle: find unknown line type\n"));
667 f << "###line[type]=" << type << ",";
668 break;
669 }
670 if (m_state->getDash(dashId, style.m_lineDashWidth)) {
671 if (style.m_lineDashWidth.size()) f << "dash[id]=" << dashId << ",";
672 }
673 else
674 f << "##dash[id]=" << dashId << ",";
675 return f.str();
676
677 }
678
679 std::string MacDraft5StyleManager::updateSurfaceStyle(int type, int id, MWAWGraphicStyle &style)
680 {
681 libmwaw::DebugStream f;
682 switch (type) {
683 case 0:
684 f << "no[surf],";
685 break;
686 case 1: { // use color
687 MWAWColor color;
688 if (id==0)
689 f << "no[color],";
690 else if (m_state->getColor(id, color)) {
691 if (!color.isWhite())
692 f << "col[surf]=" << color << ",";
693 style.setSurfaceColor(color);
694 }
695 else
696 f << "###colId=" << id << ",";
697 break;
698 }
699 case 2: { // use pattern
700 MWAWGraphicStyle::Pattern pattern;
701 if (id==0)
702 f << "no[pattern],";
703 else if (m_state->getPattern(id, pattern)) {
704 style.setPattern(pattern);
705 f << "usePattern[surf]=" << id << ",";
706 }
707 else
708 f << "###patId=" << id << ",";
709 break;
710 }
711 case 3: {
712 if (id>=0 && id<255)
713 style.m_surfaceOpacity=float(id)/255.f;
714 else {
715 MWAW_DEBUG_MSG(("MacDraft5StyleManager::updateSurfaceStyle:surface opacity seems bads\n"));
716 f << "###";
717 }
718 f << "opacity=" << int(id)/255 << "%,";
719 break;
720 }
721 default:
722 MWAW_DEBUG_MSG(("MacDraft5StyleManager::updateSurfaceStyle: find unknown surf type\n"));
723 f << "###surf[type]=" << type << ",";
724 break;
725 }
726 return f.str();
727 }
728
729 bool MacDraft5StyleManager::getBitmap(int bId, MWAWEmbeddedObject &picture) const
730 {
731 MWAWVec2i pictSize;
732 MWAWColor avColor;
733 if (m_state->m_bitmapIdToPixmapMap.find(bId)==m_state->m_bitmapIdToPixmapMap.end() ||
734 !m_state->m_bitmapIdToPixmapMap.find(bId)->second ||
735 !m_state->m_bitmapIdToPixmapMap.find(bId)->second->get(picture,pictSize,avColor)) {
736 MWAW_DEBUG_MSG(("MWAWMacDraft5StyleManager::getBitmap: can not find bitmap %d\n", bId));
737 return false;
738 }
739 #ifdef DEBUG_WITH_FILES
740 if (!picture.m_dataList.empty()) {
741 std::stringstream s;
742 s << "Bitmap" << bId << ".ppm";
743 libmwaw::Debug::dumpFile(picture.m_dataList[0], s.str().c_str());
744 }
745 #endif
746
747 return true;
748 }
749
750 bool MacDraft5StyleManager::getPixmap(int pId, MWAWEmbeddedObject &picture, MWAWVec2i &pictSize, MWAWColor &avColor) const
751 {
752 if (m_state->m_pixIdToPixmapMap.find(pId)==m_state->m_pixIdToPixmapMap.end() ||
753 !m_state->m_pixIdToPixmapMap.find(pId)->second ||
754 !m_state->m_pixIdToPixmapMap.find(pId)->second->get(picture,pictSize,avColor)) {
755 MWAW_DEBUG_MSG(("MWAWMacDraft5StyleManager::getPixmap: can not find pixmap %d\n", pId));
756 return false;
757 }
758 #ifdef DEBUG_WITH_FILES
759 if (!picture.m_dataList.empty()) {
760 std::stringstream s;
761 s << "PixPat" << pId << ".ppm";
762 libmwaw::Debug::dumpFile(picture.m_dataList[0], s.str().c_str());
763 }
764 #endif
765
766 return true;
767 }
768
769 void MacDraft5StyleManager::updatePatterns()
770 {
771 for (auto const &it : m_state->m_pixIdToPatternIdMap) {
772 MWAWEmbeddedObject picture;
773 MWAWVec2i bitmapSize;
774 MWAWColor averageColor;
775 if (!getPixmap(it.first, picture, bitmapSize, averageColor)) continue;
776 if (it.second>=m_state->m_patternList.size()) {
777 MWAW_DEBUG_MSG(("MWAWMacDraft5StyleManager::updatePatterns: oops patterns id seems bad for %d\n", it.first));
778 continue;
779 }
780 auto pixmap=m_state->m_pixIdToPixmapMap.find(it.first)->second;
781 m_state->m_patternList[it.second]=MWAWGraphicStyle::Pattern(bitmapSize, picture, averageColor);
782 }
783 // check for unused pixmap
784 for (auto const &it : m_state->m_pixIdToPixmapMap) {
785 if (m_state->m_pixIdToPatternIdMap.find(it.first)!=m_state->m_pixIdToPatternIdMap.end() || !it.second)
786 continue;
787 static bool first=true;
788 if (first) {
789 MWAW_DEBUG_MSG(("MWAWMacDraft5StyleManager::updatePatterns: find unread pixmap %d\n", it.first));
790 first=false;
791 }
792 MWAWEmbeddedObject picture;
793 MWAWVec2i bitmapSize;
794 MWAWColor averageColor;
795 getPixmap(it.first, picture, bitmapSize, averageColor);
796 }
797 }
798
799 ////////////////////////////////////////////////////////////
800 //
801 // Intermediate level
802 //
803 ////////////////////////////////////////////////////////////
804 bool MacDraft5StyleManager::readBitmapZones()
805 {
806 if (m_state->m_beginToBitmapEntryMap.empty()) {
807 m_state->m_dataEnd=m_state->m_rsrcBegin;
808 return true;
809 }
810 MWAWInputStreamPtr input = m_parserState->m_input;
811 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
812 if (m_state->m_rsrcBegin>0)
813 input->pushLimit(m_state->m_rsrcBegin);
814 auto it=m_state->m_beginToBitmapEntryMap.begin();
815 m_state->m_dataEnd=it->first;
816 long lastPos=it->first;
817 while (it!=m_state->m_beginToBitmapEntryMap.end()) {
818 if (it->first!=lastPos) {
819 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmapZones: find some unknown zone\n"));
820 ascFile.addPos(lastPos);
821 ascFile.addNote("Entries(UnknZone):");
822 }
823 MWAWEntry const &entry=it++->second;
824 lastPos=entry.end();
825 if (entry.type()=="bitmap" && readBitmap(entry))
826 continue;
827 ascFile.addPos(entry.begin());
828 ascFile.addNote("Entries(BITData):");
829 }
830 if (m_state->m_rsrcBegin>0)
831 input->popLimit();
832 return true;
833 }
834
835 ////////////////////////////////////////////////////////////
836 // resource fork
837 ////////////////////////////////////////////////////////////
838 bool MacDraft5StyleManager::readResources()
839 {
840 // first look the resource manager
841 MWAWRSRCParserPtr rsrcParser = m_parserState->m_rsrcParser;
842 if (rsrcParser) {
843 auto &entryMap = rsrcParser->getEntriesMap();
844 for (auto &it : entryMap)
845 readResource(it.second, true);
846 }
847
848 MWAWInputStreamPtr input = m_parserState->m_input;
849 long endPos=input->size();
850 if (endPos<=28) {
851 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readResources: the file seems too short\n"));
852 return false;
853 }
854 input->seek(-10, librevenge::RVNG_SEEK_END);
855 auto dSz=static_cast<int>(input->readULong(2));
856 if (dSz<28 || dSz>=endPos)
857 return false;
858 std::string name("");
859 for (int i=0; i<8; ++i) name+=char(input->readULong(1));
860 if (name!="RBALRPH ") return false;
861 input->seek(-dSz, librevenge::RVNG_SEEK_END);
862 long pos=input->tell();
863 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
864 libmwaw::DebugStream f;
865 f << "Entries(RSRCMap):";
866 auto depl=long(input->readULong(4));
867 long debRSRCPos=endPos-depl;
868 if (depl>endPos || debRSRCPos>pos) {
869 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readResources: the depl0 is bad\n"));
870 return false;
871 }
872 f << "debPos=" << std::hex << debRSRCPos << std::dec << ",";
873 depl=long(input->readULong(4));
874 if (pos-depl!=debRSRCPos) {
875 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readResources: the depl1 is bad\n"));
876 f << "###";
877 ascFile.addPos(pos);
878 ascFile.addNote(f.str().c_str());
879
880 return false;
881 }
882
883 name="";
884 for (int i=0; i<4; ++i) name +=char(input->readULong(1));
885 if (name!="RSRC") {
886 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readResources: can not find the resource name\n"));
887 f << "###";
888 ascFile.addPos(pos);
889 ascFile.addNote(f.str().c_str());
890
891 return false;
892 }
893 int N=(dSz-22)/2;
894 for (int i=0; i<N; ++i) { // f0=1
895 auto val=static_cast<int>(input->readLong(2));
896 if (val) f << "f" << i << "=" << val << ",";
897 }
898 ascFile.addPos(pos);
899 ascFile.addNote(f.str().c_str());
900
901 input->pushLimit(pos);
902 input->seek(debRSRCPos, librevenge::RVNG_SEEK_SET);
903 while (!input->isEnd()) {
904 pos=input->tell();
905 auto fSz=long(input->readULong(4));
906 if (fSz==0) {
907 ascFile.addPos(pos);
908 ascFile.addNote("_");
909 continue;
910 }
911 endPos=pos+fSz;
912 if (!input->checkPosition(endPos)) {
913 input->seek(pos,librevenge::RVNG_SEEK_SET);
914 ascFile.addPos(pos);
915 ascFile.addNote("Entries(rsrcBAD):");
916 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readResources: find some bad resource\n"));
917 break;
918 }
919 if (fSz<16) {
920 ascFile.addPos(pos);
921 ascFile.addNote("Entries(rsrcBAD):");
922 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readResources: find unknown resource\n"));
923 input->seek(endPos, librevenge::RVNG_SEEK_SET);
924 continue;
925 }
926 MWAWEntry entry;
927 entry.setBegin(pos+16);
928 entry.setLength(long(input->readULong(4)));
929 name="";
930 for (int i=0; i<4; ++i) name+=char(input->readULong(1));
931 entry.setType(name);
932 entry.setId(static_cast<int>(input->readLong(2)));
933 if (entry.end()<pos+16 || entry.end()>endPos || name.empty()) {
934 ascFile.addPos(pos);
935 ascFile.addNote("Entries(rsrcBAD):###");
936 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readResources: problem reading rsrc data\n"));
937 input->seek(endPos, librevenge::RVNG_SEEK_SET);
938 continue;
939 }
940 if (readResource(entry, false)) {
941 input->seek(endPos, librevenge::RVNG_SEEK_SET);
942 continue;
943 }
944 f.str("");
945 f << "Entries(rsrc" << name << ")[" << entry.id() << "]:###";
946 ascFile.addPos(pos);
947 ascFile.addNote(f.str().c_str());
948 if (fSz>120)
949 ascFile.skipZone(pos+100, endPos-1);
950 input->seek(endPos, librevenge::RVNG_SEEK_SET);
951 }
952 input->popLimit();
953 m_state->m_rsrcBegin=debRSRCPos;
954 updatePatterns();
955 return true;
956 }
957
958 bool MacDraft5StyleManager::readResource(MWAWEntry &entry, bool inRsrc)
959 {
960 if (inRsrc && !m_parserState->m_rsrcParser) {
961 MWAW_DEBUG_MSG(("MWAWMacDraft5StyleManager::readResource: can not find the resource parser\n"));
962 return false;
963 }
964 if (entry.type()=="PICT") {
965 librevenge::RVNGBinaryData data;
966 if (inRsrc)
967 return m_parserState->m_rsrcParser->parsePICT(entry,data);
968 else
969 return m_parser.readPICT(entry, data);
970 }
971 if (entry.type()=="ppat")
972 return readPixPat(entry, inRsrc);
973 if (entry.type()=="vers") {
974 if (inRsrc) {
975 MWAWRSRCParser::Version vers;
976 return m_parserState->m_rsrcParser->parseVers(entry,vers);
977 }
978 else
979 return readVersion(entry);
980 }
981 // 0 resources
982 if (entry.type()=="BITL")
983 return readBitmapList(entry, inRsrc);
984 if (entry.type()=="LAYI")
985 return m_parser.readLayoutDefinitions(entry, inRsrc);
986 if (entry.type()=="pnot")
987 return m_parser.readPICTList(entry, inRsrc);
988 // 1 resources
989 if (entry.type()=="VIEW")
990 return m_parser.readViews(entry, inRsrc);
991 if (entry.type()=="FNUS")
992 return readFonts(entry, inRsrc);
993 // 1-2-3 resources
994 if (entry.type()=="OPST") {
995 if (inRsrc && !m_parserState->m_rsrcParser) return false;
996 MWAWInputStreamPtr input = inRsrc ? m_parserState->m_rsrcParser->getInput() : m_parserState->m_input;
997 libmwaw::DebugStream f;
998 f << "Entries(OPST)[" << entry.id() << "]:";
999 entry.setParsed(true);
1000 if (!input || !entry.valid() || entry.length()!=2) {
1001 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readResource: unexpected OPST length\n"));
1002 f << "###";
1003 }
1004 else {
1005 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1006 auto val=static_cast<int>(input->readLong(2));
1007 if (val!=100) // always 100?
1008 f << "f0=" << val << ",";
1009 }
1010 if (input && entry.valid()) {
1011 libmwaw::DebugFile &ascFile = inRsrc ? m_parserState->m_rsrcParser->ascii() : m_parserState->m_asciiFile;
1012 ascFile.addPos(entry.begin()-(inRsrc ? 4 : 16));
1013 ascFile.addNote(f.str().c_str());
1014 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1015 }
1016 return true;
1017 }
1018
1019 // 128 resources
1020 if (entry.type()=="pltt")
1021 return readColors(entry, inRsrc);
1022 if (entry.type()=="DASH")
1023 return readDashes(entry, inRsrc);
1024 if (entry.type()=="PLDT")
1025 return readPatterns(entry, inRsrc);
1026 // PATL: link to PLDT(ie. pattern point), Opac: link to Opac
1027 if (entry.type()=="PATL" || entry.type()=="Opac")
1028 return readRSRCList(entry, inRsrc);
1029
1030 // 256+x
1031 if (entry.type()=="Link")
1032 return m_parser.readLinks(entry, inRsrc);
1033
1034 //
1035 if (entry.type()=="Opcd") // associated with "Opac"
1036 return readOpcd(entry, inRsrc);
1037
1038 if (entry.type()=="icns") { // file icone, safe to ignore
1039 MWAWInputStreamPtr input = inRsrc ? m_parserState->m_rsrcParser->getInput() : m_parserState->m_input;
1040 libmwaw::DebugFile &ascFile = inRsrc ? m_parserState->m_rsrcParser->ascii() : m_parserState->m_asciiFile;
1041 entry.setParsed(true);
1042 ascFile.addPos(entry.begin()-(inRsrc ? 4 : 16));
1043 ascFile.addNote("Entries(Icone):...");
1044 ascFile.skipZone(entry.begin(), entry.end()-1);
1045 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1046 return true;
1047 }
1048 if (inRsrc) return false;
1049
1050 if (entry.type()=="flPF") { // printer plist, safe to ignore
1051 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1052 ascFile.addPos(entry.begin()-16);
1053 ascFile.addNote("Entries(PrintPList):...");
1054 ascFile.skipZone(entry.begin(), entry.end()-1);
1055 m_parserState->m_input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1056 return true;
1057 }
1058 libmwaw::DebugStream f;
1059 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1060 f << "Entries(rsrc" << entry.type() << ")[" << entry.id() << "]:";
1061 ascFile.addPos(entry.begin()-16);
1062 ascFile.addNote(f.str().c_str());
1063 m_parserState->m_input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1064
1065 return true;
1066 }
1067
1068 bool MacDraft5StyleManager::readFonts(MWAWEntry const &entry, bool inRsrc)
1069 {
1070 if (inRsrc && !m_parserState->m_rsrcParser) return false;
1071 MWAWInputStreamPtr input = inRsrc ? m_parserState->m_rsrcParser->getInput() : m_parserState->m_input;
1072 if (!input || !entry.valid()) {
1073 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readFNUS: entry is invalid\n"));
1074 return false;
1075 }
1076 entry.setParsed(true);
1077 libmwaw::DebugFile &ascFile = inRsrc ? m_parserState->m_rsrcParser->ascii() : m_parserState->m_asciiFile;
1078 libmwaw::DebugStream f;
1079 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1080 f << "Entries(Fonts):";
1081 if (entry.id()!=1) {
1082 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readFNUS: id seems bad\n"));
1083 f << "##id=" << entry.id() << ",";
1084 }
1085 ascFile.addPos(entry.begin()-(inRsrc ? 4 : 16));
1086 ascFile.addNote(f.str().c_str());
1087 int n=0;
1088 while (!input->isEnd()) {
1089 long pos=input->tell();
1090 if (pos+3>entry.end())
1091 break;
1092 f.str("");
1093 f << "Fonts-" << n++ << ":";
1094 auto fId=static_cast<int>(input->readULong(2));
1095 f<<"fId=" << fId << ",";
1096 auto sSz=static_cast<int>(input->readULong(1));
1097 if (pos+3+sSz>entry.end()) {
1098 input->seek(pos,librevenge::RVNG_SEEK_SET);
1099 break;
1100 }
1101 std::string name("");
1102 for (int c=0; c<sSz; ++c) name += char(input->readULong(1));
1103 f << name << ",";
1104 if (!name.empty())
1105 m_parserState->m_fontConverter->setCorrespondance(fId, name);
1106 ascFile.addPos(pos);
1107 ascFile.addNote(f.str().c_str());
1108 }
1109 if (input->tell()!=entry.end()) {
1110 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readFNUS: find extra data\n"));
1111 ascFile.addPos(input->tell());
1112 ascFile.addNote("FNUS-extra:###");
1113 }
1114 input->seek(entry.end(),librevenge::RVNG_SEEK_SET);
1115 return true;
1116 }
1117
1118 bool MacDraft5StyleManager::readColors(MWAWEntry const &entry, bool inRsrc)
1119 {
1120 if (inRsrc && !m_parserState->m_rsrcParser) return false;
1121 MWAWInputStreamPtr input = inRsrc ? m_parserState->m_rsrcParser->getInput() : m_parserState->m_input;
1122 if (!input || !entry.valid() || entry.length()<16 || (entry.length()%16)!=0) {
1123 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readColors: entry is invalid\n"));
1124 return false;
1125 }
1126 entry.setParsed(true);
1127 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1128 libmwaw::DebugFile &ascFile = inRsrc ? m_parserState->m_rsrcParser->ascii() : m_parserState->m_asciiFile;
1129 libmwaw::DebugStream f;
1130 f << "Entries(Color):";
1131 if (entry.id()!=128) {
1132 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readColors: entry id seems odd\n"));
1133 f << "##id=" << entry.id() << ",";
1134 }
1135 auto N=static_cast<int>(input->readLong(2));
1136 f << "N=" << N << ",";
1137 if (N*16+16!=entry.length()) {
1138 f << "###";
1139 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readColors: the N values seems odd\n"));
1140 N=int(entry.length()/16)-1;
1141 }
1142 int val;
1143 for (int i=0; i<5; ++i) { // f2=[8c]0[01][0-c]
1144 val=static_cast<int>(input->readULong(2));
1145 if (val) f << "fl" << i << "=" << std::hex << val << std::dec << ",";
1146 }
1147 val=static_cast<int>(input->readULong(4));
1148 if (val) f << "unkn=" << std::hex << val << std::dec << ",";
1149 ascFile.addPos(entry.begin()-(inRsrc ? 4 : 16));
1150 ascFile.addNote(f.str().c_str());
1151
1152 m_state->m_colorList.clear();
1153 for (long i=0; i<N; ++i) {
1154 long pos=input->tell();
1155 f.str("");
1156 f << "Color-" << i << ":";
1157 uint8_t col[3];
1158 for (auto &c : col) c=static_cast<uint8_t>(input->readULong(2)>>8);
1159 MWAWColor color(col[0],col[1],col[2]);
1160 f << color << ",";
1161 m_state->m_colorList.push_back(color);
1162 for (int j=0; j<5; ++j) { // f0=0|2, f1=0|5(gray?), f3=id?
1163 val=static_cast<int>(input->readLong(2));
1164 if (val) f << "f" << j << "=" << val << ",";
1165 }
1166 input->seek(pos+16, librevenge::RVNG_SEEK_SET);
1167 ascFile.addPos(pos);
1168 ascFile.addNote(f.str().c_str());
1169 }
1170 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1171 return true;
1172 }
1173
1174 bool MacDraft5StyleManager::readDashes(MWAWEntry const &entry, bool inRsrc)
1175 {
1176 if (inRsrc && !m_parserState->m_rsrcParser) return false;
1177 MWAWInputStreamPtr input = inRsrc ? m_parserState->m_rsrcParser->getInput() : m_parserState->m_input;
1178 if (!input || !entry.valid() || entry.length()<16 || (entry.length()%16)!=0) {
1179 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readDashes: entry is invalid\n"));
1180 return false;
1181 }
1182 entry.setParsed(true);
1183 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1184 libmwaw::DebugFile &ascFile = inRsrc ? m_parserState->m_rsrcParser->ascii() : m_parserState->m_asciiFile;
1185 libmwaw::DebugStream f;
1186 f << "Entries(Dash):";
1187 if (entry.id()!=128) {
1188 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readDashes: entry id seems odd\n"));
1189 f << "##id=" << entry.id() << ",";
1190 }
1191 auto N=int(entry.length()/16);
1192 ascFile.addPos(entry.begin()-(inRsrc ? 4 : 16));
1193 ascFile.addNote(f.str().c_str());
1194
1195 for (long i=0; i<N; ++i) {
1196 long pos=input->tell();
1197 f.str("");
1198 f << "Dash-" << i << ":";
1199 auto n=static_cast<int>(input->readULong(1));
1200 if (n>15) {
1201 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readDashes: n is bad\n"));
1202 f << "##n=" << n << ",";
1203 n=0;
1204 }
1205 std::vector<float> dash;
1206 f << "[";
1207 for (int j=0; j<n; ++j) {
1208 dash.push_back(float(input->readULong(1)));
1209 f << dash.back() << ",";
1210 }
1211 f << "],";
1212 m_state->m_dashList.push_back(dash);
1213 input->seek(pos+16, librevenge::RVNG_SEEK_SET);
1214 ascFile.addPos(pos);
1215 ascFile.addNote(f.str().c_str());
1216 }
1217 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1218 return true;
1219 }
1220
1221 bool MacDraft5StyleManager::readPatterns(MWAWEntry const &entry, bool inRsrc)
1222 {
1223 if (inRsrc && !m_parserState->m_rsrcParser) return false;
1224 MWAWInputStreamPtr input = inRsrc ? m_parserState->m_rsrcParser->getInput() : m_parserState->m_input;
1225 if (!input || !entry.valid() || entry.length()<12 || (entry.length()%12)!=0) {
1226 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readPatterns: entry is invalid\n"));
1227 return false;
1228 }
1229 entry.setParsed(true);
1230 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1231 libmwaw::DebugFile &ascFile = inRsrc ? m_parserState->m_rsrcParser->ascii() : m_parserState->m_asciiFile;
1232 libmwaw::DebugStream f;
1233 f << "Entries(Pattern):";
1234 if (entry.id()!=128) {
1235 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readPatterns: entry id seems odd\n"));
1236 f << "##id=" << entry.id() << ",";
1237 }
1238 int val;
1239 for (int i=0; i<4; ++i) { // f0=0|1|-1, f1=0|-1
1240 val=static_cast<int>(input->readLong(2));
1241 if (val) f << "f" << i << "=" << val << ",";
1242 }
1243 val=static_cast<int>(input->readULong(4));
1244 if (val) f << "unkn=" << std::hex << val << std::dec << ",";
1245 ascFile.addPos(entry.begin()-(inRsrc ? 4 : 16));
1246 ascFile.addNote(f.str().c_str());
1247
1248 auto N=size_t(entry.length()/12-1);
1249 m_state->m_patternList.resize(N);
1250 for (size_t i=0; i<N; ++i) {
1251 long pos=input->tell();
1252 f.str("");
1253 f << "Pattern-" << i << ":";
1254 auto type=static_cast<int>(input->readLong(2));
1255 switch (type) {
1256 case 0: {
1257 val=static_cast<int>(input->readLong(2)); // always 0
1258 if (val) f << "f0=" << val << ",";
1259 MWAWGraphicStyle::Pattern pat;
1260 pat.m_dim=MWAWVec2i(8,8);
1261 pat.m_data.resize(8);
1262 pat.m_colors[0]=MWAWColor::white();
1263 pat.m_colors[1]=MWAWColor::black();
1264 for (auto &data : pat.m_data) data=static_cast<uint8_t>(input->readULong(1));
1265 f << pat << ",";
1266 m_state->m_patternList[i]=pat;
1267 break;
1268 }
1269 case 1: {
1270 auto pixId=static_cast<int>(input->readLong(2));
1271 f << "id[pixpat]=" << pixId << ",";
1272 if (m_state->m_pixIdToPatternIdMap.find(pixId)==m_state->m_pixIdToPatternIdMap.end())
1273 m_state->m_pixIdToPatternIdMap[pixId]=i;
1274 else if (m_state->m_pixIdToPatternIdMap.find(pixId)->second!=i) { // can be normal if def in rsrc and data fork
1275 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readPatterns: find a dupplicated pixId\n"));
1276 f << "###";
1277 }
1278 break;
1279 }
1280 default:
1281 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readPatterns: find unknown type\n"));
1282 f << "##type=" << type << ",";
1283 break;
1284 }
1285 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
1286 ascFile.addPos(pos);
1287 ascFile.addNote(f.str().c_str());
1288 }
1289 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1290 return true;
1291 }
1292
1293 bool MacDraft5StyleManager::readBitmap(MWAWEntry const &entry)
1294 {
1295 MWAWInputStreamPtr input = m_parserState->m_input;
1296 if (!input || !entry.valid() || entry.length()<54) {
1297 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmap: entry is invalid\n"));
1298 return false;
1299 }
1300 entry.setParsed(true);
1301 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1302 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1303 libmwaw::DebugStream f;
1304 f << "Entries(Bitmap)[" << entry.id() << "]:";
1305 auto fSz=long(input->readULong(4));
1306 if (fSz+4!=entry.length()) {
1307 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmap: zone size seems bad\n"));
1308 f << "#sz[entry]=" << fSz << ",";
1309 }
1310 fSz=long(input->readULong(4));
1311 if (fSz+8>entry.length()) {
1312 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmap: data size seems bad\n"));
1313 f << "#sz[data]=" << fSz << ",";
1314 }
1315 // now the pixmap
1316 std::shared_ptr<MacDraft5StyleManagerInternal::Pixmap> pixmap(new MacDraft5StyleManagerInternal::Pixmap);
1317 pixmap->m_rowBytes = static_cast<int>(input->readULong(2));
1318 pixmap->m_rowBytes &= 0x3FFF;
1319
1320 // read the rectangle: bound
1321 int dim[4];
1322 for (auto &d : dim) d = static_cast<int>(input->readLong(2));
1323 pixmap->m_rect = MWAWBox2i(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2]));
1324 if (pixmap->m_rect.size().x() <= 0 || pixmap->m_rect.size().y() <= 0) {
1325 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmap: find odd bound rectangle ... \n"));
1326 return false;
1327 }
1328 pixmap->m_version = static_cast<int>(input->readLong(2));
1329 pixmap->m_packType = static_cast<int>(input->readLong(2));
1330 pixmap->m_packSize = static_cast<int>(input->readLong(4));
1331 for (auto &res : pixmap->m_resolution) {
1332 res = static_cast<int>(input->readLong(2));
1333 input->readLong(2);
1334 }
1335 pixmap->m_pixelType = static_cast<int>(input->readLong(2));
1336 pixmap->m_pixelSize = static_cast<int>(input->readLong(2));
1337 pixmap->m_compCount = static_cast<int>(input->readLong(2));
1338 pixmap->m_compSize = static_cast<int>(input->readLong(2));
1339 pixmap->m_planeBytes = static_cast<int>(input->readLong(4));
1340 f << *pixmap;
1341 input->seek(8, librevenge::RVNG_SEEK_CUR); // color handle, reserved
1342 ascFile.addPos(entry.begin());
1343 ascFile.addNote(f.str().c_str());
1344
1345 long pos=input->tell();
1346 if (pixmap->m_rowBytes*8 < pixmap->m_rect.size().y()) {
1347 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmap: row bytes seems to short: %d/%d... \n", pixmap->m_rowBytes*8, pixmap->m_rect.size().y()));
1348 ascFile.addPos(pos);
1349 ascFile.addNote("Bitmap:###");
1350 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1351 return true;
1352 }
1353
1354 if (!pixmap->readPixmapData(*input)) {
1355 ascFile.addPos(pos);
1356 ascFile.addNote("Bitmap:###");
1357 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1358 return true;
1359 }
1360 ascFile.skipZone(pos,input->tell()-1);
1361 if (input->tell()!=entry.end()) {
1362 // find 00000018000000000000000000000000
1363 ascFile.addPos(input->tell());
1364 ascFile.addNote("Bitmap-A");
1365 }
1366 m_state->m_bitmapIdToPixmapMap[entry.id()]=pixmap;
1367 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1368 return true;
1369 }
1370
1371 bool MacDraft5StyleManager::readPixPat(MWAWEntry const &entry, bool inRsrc)
1372 {
1373 if (inRsrc && !m_parserState->m_rsrcParser) return false;
1374 MWAWInputStreamPtr input = inRsrc ? m_parserState->m_rsrcParser->getInput() : m_parserState->m_input;
1375 if (!input || !entry.valid() || entry.length()<74) {
1376 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readPixPat: entry is invalid\n"));
1377 return false;
1378 }
1379 entry.setParsed(true);
1380 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1381 libmwaw::DebugFile &ascFile = inRsrc ? m_parserState->m_rsrcParser->ascii() : m_parserState->m_asciiFile;
1382 libmwaw::DebugStream f;
1383 f << "Entries(PixPat)[" << entry.id() << "]:";
1384 for (int i=0; i<16; ++i) {
1385 auto val=static_cast<int>(input->readLong(2));
1386 static int const expected[]= {1,0,0x1c,0,0x4e,0,0,-1,0,0,
1387 -21931,-21931,-21931,-21931,0,0
1388 }; // pattern, 0, 0
1389 if (val!=expected[i])
1390 f << "f" << i << "=" << val << ",";
1391 }
1392 // now the pixmap
1393 std::shared_ptr<MacDraft5StyleManagerInternal::Pixmap> pixmap(new MacDraft5StyleManagerInternal::Pixmap);
1394 pixmap->m_rowBytes = static_cast<int>(input->readULong(2));
1395 pixmap->m_rowBytes &= 0x3FFF;
1396
1397 // read the rectangle: bound
1398 int dim[4];
1399 for (auto &d : dim) d = static_cast<int>(input->readLong(2));
1400 pixmap->m_rect = MWAWBox2i(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2]));
1401 if (pixmap->m_rect.size().x() <= 0 || pixmap->m_rect.size().y() <= 0) {
1402 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readPixPat: find odd bound rectangle ... \n"));
1403 return false;
1404 }
1405 pixmap->m_version = static_cast<int>(input->readLong(2));
1406 pixmap->m_packType = static_cast<int>(input->readLong(2));
1407 pixmap->m_packSize = static_cast<int>(input->readLong(4));
1408 for (auto &res : pixmap->m_resolution) {
1409 res = static_cast<int>(input->readLong(2));
1410 input->readLong(2);
1411 }
1412 pixmap->m_pixelType = static_cast<int>(input->readLong(2));
1413 pixmap->m_pixelSize = static_cast<int>(input->readLong(2));
1414 pixmap->m_compCount = static_cast<int>(input->readLong(2));
1415 pixmap->m_compSize = static_cast<int>(input->readLong(2));
1416 pixmap->m_planeBytes = static_cast<int>(input->readLong(4));
1417 f << *pixmap;
1418 auto colorDepl=long(input->readULong(4));
1419 input->seek(4, librevenge::RVNG_SEEK_CUR); // reserved
1420 ascFile.addPos(entry.begin()-(inRsrc ? 4 : 16));
1421 ascFile.addNote(f.str().c_str());
1422
1423 long pos=input->tell();
1424 if (pixmap->m_rowBytes*8 < pixmap->m_rect.size().y()) {
1425 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readPixPat: row bytes seems to short: %d/%d... \n", pixmap->m_rowBytes*8, pixmap->m_rect.size().y()));
1426 ascFile.addPos(pos);
1427 ascFile.addNote("PixPat:###");
1428 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1429 return true;
1430 }
1431
1432 if (colorDepl && (colorDepl<68 || !input->checkPosition(entry.begin()+colorDepl+6))) {
1433 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readPixPat: the color handle seems bad\n"));
1434 ascFile.addPos(pos);
1435 ascFile.addNote("PixPat-A:###");
1436 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1437 return true;
1438 }
1439 if (colorDepl) {
1440 input->seek(entry.begin()+colorDepl+6, librevenge::RVNG_SEEK_SET);
1441 long colorPos=input->tell();
1442 f.str("");
1443 f << "PixPat-colors:";
1444 auto N=static_cast<int>(input->readULong(2));
1445 if (N>2000) {
1446 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readPixPat: the number of color seems bad\n"));
1447 f << "###";
1448 N=2000;
1449 }
1450 f << "N=" << N << ",";
1451 pixmap->m_colorTable.resize(size_t(N+1));
1452
1453 int numColor=int(entry.end()-colorPos)/8;
1454 bool ok=true;
1455 for (int i=0; i<numColor; ++i) {
1456 auto id=static_cast<int>(input->readULong(2));
1457 uint8_t col[3];
1458 for (auto &c : col) c=static_cast<uint8_t>(input->readULong(2)>>8);
1459 MWAWColor color(col[0],col[1],col[2]);
1460 if (id!=i) f << "col" << id << "=" << color;
1461 else f << color;
1462 if (id>N) {
1463 if (ok) {
1464 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readPixPat: first field size seems bad\n"));
1465 ok=false;
1466 }
1467 f << "###" << ",";
1468 continue;
1469 }
1470 f << ",";
1471 pixmap->m_colorTable[size_t(id)]=color;
1472 }
1473 if (!ok) {
1474 ascFile.addPos(colorPos);
1475 ascFile.addNote(f.str().c_str());
1476 }
1477 else
1478 ascFile.skipZone(colorPos,entry.end()-1);
1479 input->seek(pos, librevenge::RVNG_SEEK_SET);
1480 }
1481 input->seek(pos, librevenge::RVNG_SEEK_SET);
1482 if (!pixmap->readPixmapData(*input)) {
1483 ascFile.addPos(pos);
1484 ascFile.addNote("PixPat:###");
1485 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1486 return true;
1487 }
1488 ascFile.skipZone(pos,input->tell()-1);
1489 if (input->tell()!=entry.end() && input->tell()!=entry.begin()+colorDepl+6) {
1490 // remain 6 empty byte: maybe some pointer
1491 ascFile.addPos(input->tell());
1492 ascFile.addNote("PixPat-A");
1493 }
1494
1495 m_state->m_pixIdToPixmapMap[entry.id()]=pixmap;
1496
1497 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1498 return true;
1499 }
1500
1501 bool MacDraft5StyleManager::readVersion(MWAWEntry &entry)
1502 {
1503 MWAWInputStreamPtr input = m_parserState->m_input;
1504 if (!input || !entry.valid() || entry.length()<8 || !input->checkPosition(entry.end())) {
1505 MWAW_DEBUG_MSG(("MWAWMacDraft5StyleManager::readVersion: entry is invalid\n"));
1506 return false;
1507 }
1508 MWAWRSRCParser::Version vers;
1509 entry.setParsed(true);
1510 libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile;
1511 libmwaw::DebugStream f;
1512 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1513 vers.m_majorVersion = static_cast<int>(input->readULong(1));
1514 vers.m_minorVersion = static_cast<int>(input->readULong(1));
1515 auto val = long(input->readULong(1));
1516 if (val) f << "devStage=" << val << ",";
1517 val = long(input->readULong(1));
1518 if (val) f << "preReleaseLevel=" << std::hex << val << std::dec << ",";
1519 vers.m_countryCode = static_cast<int>(input->readULong(2));
1520 for (int i = 0; i < 2; i++) {
1521 auto sz = static_cast<int>(input->readULong(1));
1522 long pos = input->tell();
1523 if (pos+sz > entry.end()) {
1524 MWAW_DEBUG_MSG(("MWAWMacDraft5StyleManager::readVersion: can not read strings %d\n",i));
1525 return false;
1526 }
1527 std::string str("");
1528 for (int c = 0; c < sz; c++)
1529 str+=char(input->readULong(1));
1530 if (i==0)
1531 vers.m_versionString = str;
1532 else
1533 vers.m_string = str;
1534 }
1535 vers.m_extra = f.str();
1536 f << "Entries(RSRCvers)[" << entry.id() << "]:" << vers;
1537 ascFile.addPos(entry.begin()-16);
1538 ascFile.addNote(f.str().c_str());
1539 return true;
1540 }
1541
1542 ////////////////////////////////////////////////////////////
1543 // PICT list
1544 ////////////////////////////////////////////////////////////
1545 bool MacDraft5StyleManager::readBitmapList(MWAWEntry const &entry, bool inRsrc)
1546 {
1547 if (inRsrc && !m_parserState->m_rsrcParser) return false;
1548 MWAWInputStreamPtr input = inRsrc ? m_parserState->m_rsrcParser->getInput() : m_parserState->m_input;
1549 MWAWInputStreamPtr fileInput=m_parserState->m_input;
1550 if (!input || !fileInput || !entry.valid() || entry.length()<30 || (entry.length()%12)<6 || (entry.length()%12)>7) {
1551 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmapList: entry is invalid\n"));
1552 return false;
1553 }
1554 entry.setParsed(true);
1555 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1556 libmwaw::DebugFile &ascFile = inRsrc ? m_parserState->m_rsrcParser->ascii() : m_parserState->m_asciiFile;
1557 libmwaw::DebugStream f;
1558 f << "Entries(BitmList):";
1559 if (entry.id()!=0) {
1560 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmapList: entry id seems odd\n"));
1561 f << "##id=" << entry.id() << ",";
1562 }
1563 int val;
1564 for (int i=0; i<3; ++i) { // always 0
1565 val=static_cast<int>(input->readLong(2));
1566 if (val) f << "f" << i << "=" << val << ",";
1567 }
1568 auto N=static_cast<int>(input->readULong(2));
1569 if (30+12*N!=entry.length() && 31+12*N!=entry.length()) {
1570 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmapList:N seems bad\n"));
1571 f << "##N=" << N << ",";
1572 if (30+12*N>entry.length())
1573 N=int((entry.length()-30)/12);
1574 }
1575 val=static_cast<int>(input->readLong(2)); // always 0 ?
1576 if (val) f << "f3=" << val << ",";
1577 val=static_cast<int>(input->readLong(2)); // always c
1578 if (val!=12) f << "#fSz=" << val << ",";
1579 long dataSz=input->readLong(4);
1580 if ((dataSz%12) || dataSz>12*N) {
1581 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmapList:dataSize seems bad\n"));
1582 f << "##dataSz=" << dataSz << ",";
1583 }
1584 for (int i=0; i<7; ++i) {
1585 val=static_cast<int>(input->readLong(2));
1586 static int const expected[]= {0,0xc,0,4,0,0,0};
1587 if (val!=expected[i]) f << "f" << i+4 << "=" << val << ",";
1588 }
1589
1590 ascFile.addPos(entry.begin()-(inRsrc ? 4 : 16));
1591 ascFile.addNote(f.str().c_str());
1592 for (long i=0; i<N; ++i) {
1593 long pos=input->tell();
1594 f.str("");
1595 f << "BitmList-" << i << ":";
1596 MWAWEntry dataEntry;
1597 dataEntry.setBegin(long(input->readULong(4)));
1598 dataEntry.setLength(long(input->readULong(4)));
1599
1600 if (dataEntry.begin()==0) // none
1601 ;
1602 else if (!dataEntry.valid() || !fileInput->checkPosition(dataEntry.end())) {
1603 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmapList:dataEntry seems bad\n"));
1604 f << "###data=" << std::hex << dataEntry.begin() << ":" << dataEntry.length() << ",";
1605 }
1606 else {
1607 f << "data=" << std::hex << dataEntry.begin() << ":" << dataEntry.length() << ",";
1608 dataEntry.setId(static_cast<int>(i));
1609 dataEntry.setType("bitmap");
1610 if (m_state->m_beginToBitmapEntryMap.find(dataEntry.begin())==m_state->m_beginToBitmapEntryMap.end())
1611 m_state->m_beginToBitmapEntryMap[dataEntry.begin()]=dataEntry;
1612 else if (m_state->m_beginToBitmapEntryMap.find(dataEntry.begin())->second!=dataEntry) {
1613 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readBitmapList:dataEntry already exist\n"));
1614 f << "###dupplicated,";
1615 }
1616 }
1617 for (int j=0; j<4; ++j) { // fl0=fl1=1
1618 val=static_cast<int>(input->readLong(1));
1619 if (val==1) f << "fl" << j << ",";
1620 else if (val) f << "#fl" << j << "=" << val << ",";
1621 }
1622 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
1623 ascFile.addPos(pos);
1624 ascFile.addNote(f.str().c_str());
1625 }
1626 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1627 return true;
1628 }
1629
1630
1631 ////////////////////////////////////////////////////////////
1632 // RSRC various
1633 ////////////////////////////////////////////////////////////
1634 bool MacDraft5StyleManager::readRSRCList(MWAWEntry const &entry, bool inRsrc)
1635 {
1636 if (inRsrc && !m_parserState->m_rsrcParser) return false;
1637 MWAWInputStreamPtr input = inRsrc ? m_parserState->m_rsrcParser->getInput() : m_parserState->m_input;
1638 if (!input || !entry.valid() || entry.length()!=0x1f) {
1639 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readRSRCList: entry is invalid\n"));
1640 return false;
1641 }
1642 entry.setParsed(true);
1643 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1644 libmwaw::DebugFile &ascFile = inRsrc ? m_parserState->m_rsrcParser->ascii() : m_parserState->m_asciiFile;
1645 libmwaw::DebugStream f;
1646 f << "Entries(RSRCList)[" << entry.type() << "-" << entry.id() << "]:";
1647 auto val=static_cast<int>(input->readLong(2));
1648 if (val!=entry.id()) f << "#id=" << val << ",";
1649 std::string name("");
1650 for (int i=0; i<4; ++i) name+=char(input->readULong(1));
1651 if (name!=entry.type()) f << "#type=" << name << ",";
1652 val=static_cast<int>(input->readULong(2)); // 40|48|..|28d6
1653 if (val)
1654 f << "fl=" << std::hex << val << ",";
1655 for (int i=0; i<8; ++i) { // f3=f5=c|78, f6=0|4, f7=1|6|8
1656 val=static_cast<int>(input->readLong(2));
1657 static int const expected[]= {0,0xc,0,0xc,0,0xc,0,0};
1658 if (val!=expected[i]) f << "f" << i << "=" << val << ",";
1659 }
1660 auto id=static_cast<int>(input->readULong(2));
1661 name="";
1662 for (int i=0; i<4; ++i) name+=char(input->readULong(1));
1663 f << name << ":" << id << ",";
1664 val=static_cast<int>(input->readLong(1)); // 0|-1|3f
1665 if (val)
1666 f << "fl2=" << std::hex << val << ",";
1667 ascFile.addPos(entry.begin()-(inRsrc ? 4 : 16));
1668 ascFile.addNote(f.str().c_str());
1669 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1670 return true;
1671 }
1672
1673 bool MacDraft5StyleManager::readOpcd(MWAWEntry const &entry, bool inRsrc)
1674 {
1675 if (inRsrc && !m_parserState->m_rsrcParser) return false;
1676 MWAWInputStreamPtr input = inRsrc ? m_parserState->m_rsrcParser->getInput() : m_parserState->m_input;
1677 if (!input || !entry.valid() || (entry.length()%4)) {
1678 MWAW_DEBUG_MSG(("MacDraft5StyleManager::readOpcd: entry is invalid\n"));
1679 return false;
1680 }
1681 entry.setParsed(true);
1682 libmwaw::DebugFile &ascFile = inRsrc ? m_parserState->m_rsrcParser->ascii() : m_parserState->m_asciiFile;
1683 libmwaw::DebugStream f;
1684 f << "Entries(RSRCOpcd)[" << entry.id() << "]:";
1685 long N=entry.length()/4;
1686 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1687 for (long i=0; i<N; ++i) { // find a serie of 125k 0x3f800000: double4?
1688 auto val=long(input->readULong(4));
1689 if (val!=0x3f800000)
1690 f << "f" << i << "=" << std::hex << val << std::dec << ",";
1691 }
1692 if (N>25)
1693 ascFile.skipZone(entry.begin()+100, entry.end()-1);
1694
1695 ascFile.addPos(entry.begin()-(inRsrc ? 4 : 16));
1696 ascFile.addNote(f.str().c_str());
1697 return true;
1698 }
1699
1700 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: