Debian Codegraph @

src/lib/DrawTableParser.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <cmath>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <sstream>
39 #include <stack>
40 #include <utility>
41
42 #include <librevenge/librevenge.h>
43
44 #include "MWAWFontConverter.hxx"
45 #include "MWAWGraphicListener.hxx"
46 #include "MWAWGraphicShape.hxx"
47 #include "MWAWGraphicStyle.hxx"
48 #include "MWAWHeader.hxx"
49 #include "MWAWParagraph.hxx"
50 #include "MWAWPictBitmap.hxx"
51 #include "MWAWPictData.hxx"
52 #include "MWAWPrinter.hxx"
53 #include "MWAWPosition.hxx"
54 #include "MWAWRSRCParser.hxx"
55 #include "MWAWStringStream.hxx"
56 #include "MWAWSubDocument.hxx"
57
58 #include "DrawTableParser.hxx"
59
60 /** Internal: the structures of a DrawTableParser */
61 namespace DrawTableParserInternal
62 {
63
64 ////////////////////////////////////////
65 //! Internal: the state of a DrawTableParser
66 struct State {
67 //! constructor
68 State()
69 : m_openedGroup(0)
70 , m_patternList()
71 , m_maxDim(0,0)
72 {
73 }
74
75 //! try to return a color
76 MWAWColor getColor(int id) const;
77 //! try to init a patterns
78 bool getPattern(int id, MWAWGraphicStyle::Pattern &pat) const;
79 //! init the patterns list
80 void initPatterns();
81 //! the number of opened groups
82 int m_openedGroup;
83 //! the patterns list
84 std::vector<MWAWGraphicStyle::Pattern> m_patternList;
85 //! the max dimensions
86 MWAWVec2f m_maxDim;
87 };
88
89 MWAWColor State::getColor(int id) const
90 {
91 if (id<0 || id>=8) {
92 MWAW_DEBUG_MSG(("DrawTableParserInternal::State::getColor: unknown color %d\n", id));
93 return MWAWColor::white();
94 }
95 MWAWColor const colors[]= {
96 MWAWColor::white(), MWAWColor::black(), MWAWColor(255,0,0), MWAWColor(0,255,0),
97 MWAWColor(0,0,255), MWAWColor(0,255,255), MWAWColor(255,0,255), MWAWColor(255,255,0)
98 };
99 return colors[id];
100 }
101
102 bool State::getPattern(int id, MWAWGraphicStyle::Pattern &pat) const
103 {
104 if (m_patternList.empty())
105 const_cast<State *>(this)->initPatterns();
106 if (id<0 || id>=int(m_patternList.size())) {
107 MWAW_DEBUG_MSG(("DrawTableParserInternal::State::getPattern: unknown pattern %d\n", id));
108 return false;
109 }
110 pat=m_patternList[size_t(id)];
111 return true;
112 }
113
114 void State::initPatterns()
115 {
116 if (!m_patternList.empty()) return;
117 static uint16_t const patterns[]= {
118 0x0, 0x0, 0x0, 0x0, 0xffff, 0xffff, 0xffff, 0xffff, 0x77dd, 0x77dd, 0x77dd, 0x77dd, 0xaa55, 0xaa55, 0xaa55, 0xaa55,
119 0x8822, 0x8822, 0x8822, 0x8822, 0x8800, 0x2200, 0x8800, 0x2200, 0x8000, 0x800, 0x8000, 0x800, 0x8000, 0x0, 0x800, 0x0,
120 0x8080, 0x413e, 0x808, 0x14e3, 0xff80, 0x8080, 0xff08, 0x808, 0x8142, 0x2418, 0x8142, 0x2418, 0x8040, 0x2010, 0x804, 0x201,
121 0xe070, 0x381c, 0xe07, 0x83c1, 0x77bb, 0xddee, 0x77bb, 0xddee, 0x8844, 0x2211, 0x8844, 0x2211, 0x99cc, 0x6633, 0x99cc, 0x6633,
122 0x2040, 0x8000, 0x804, 0x200, 0xff00, 0xff00, 0xff00, 0xff00, 0xff00, 0x0, 0xff00, 0x0, 0xcc00, 0x0, 0x3300, 0x0,
123 0xf0f0, 0xf0f0, 0xf0f, 0xf0f, 0xff88, 0x8888, 0xff88, 0x8888, 0xaa44, 0xaa11, 0xaa44, 0xaa11, 0x102, 0x408, 0x1020, 0x4080,
124 0x8307, 0xe1c, 0x3870, 0xe0c1, 0xeedd, 0xbb77, 0xeedd, 0xbb77, 0x1122, 0x4488, 0x1122, 0x4488, 0x3366, 0xcc99, 0x3366, 0xcc99,
125 0x40a0, 0x0, 0x40a, 0x0, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa, 0x8888, 0x8888, 0x8888, 0x8888, 0x101, 0x1010, 0x101, 0x1010,
126 0x8, 0x142a, 0x552a, 0x1408, 0xff80, 0x8080, 0x8080, 0x8080, 0x8244, 0x2810, 0x2844, 0x8201, 0x0, 0x0, 0x0, 0x0,
127 0x8000, 0x0, 0x0, 0x0, 0x8000, 0x0, 0x800, 0x0, 0x8800, 0x0, 0x800, 0x0, 0x8800, 0x0, 0x8800, 0x0,
128 0x8800, 0x2000, 0x8800, 0x0, 0x8800, 0x2000, 0x8800, 0x200, 0x8800, 0x2200, 0x8800, 0x2200, 0xa800, 0x2200, 0x8a00, 0x2200,
129 0xaa00, 0x2200, 0xaa00, 0x2200, 0xaa00, 0xa200, 0xaa00, 0x2a00, 0xaa00, 0xaa00, 0xaa00, 0xaa00, 0xaa40, 0xaa00, 0xaa04, 0xaa00,
130 0xaa44, 0xaa00, 0xaa44, 0xaa00, 0xaa44, 0xaa10, 0xaa44, 0xaa01, 0xaa44, 0xaa11, 0xaa44, 0xaa11, 0xaa54, 0xaa11, 0xaa45, 0xaa11,
131 0xaa55, 0xaa11, 0xaa55, 0xaa11, 0xaa55, 0xaa51, 0xaa55, 0xaa15, 0xaa55, 0xaa55, 0xaa55, 0xaa55, 0xea55, 0xaa55, 0xae55, 0xaa55,
132 0xee55, 0xaa55, 0xee55, 0xaa55, 0xee55, 0xba55, 0xee55, 0xab55, 0xee55, 0xbb55, 0xee55, 0xbb55, 0xfe55, 0xbb55, 0xef55, 0xbb55,
133 0xff55, 0xbb55, 0xff55, 0xbb55, 0xff55, 0xfb55, 0xff55, 0xbf55, 0xff55, 0xff55, 0xff55, 0xff55, 0xffd5, 0xff55, 0xff5d, 0xff55,
134 0xffdd, 0xff55, 0xffdd, 0xff55, 0xffdd, 0xff75, 0xffdd, 0xff57, 0xffdd, 0xff77, 0xffdd, 0xff77, 0xfffd, 0xff77, 0xffdf, 0xff77,
135 0xffff, 0xff77, 0xffff, 0xff77, 0xffff, 0xfff7, 0xffff, 0xff7f, 0xffff, 0xffff, 0xffff, 0xffff, 0x81c3, 0x8100, 0x183c, 0x1800,
136 0xffff, 0x0, 0xffff, 0x0, 0x1122, 0x2211, 0x1188, 0x8811, 0xbb00, 0x0, 0xee00, 0x0, 0xa55a, 0xa545, 0x45ba, 0x45ba,
137 0x82c7, 0x10, 0x287c, 0x1, 0xe7db, 0x9966, 0x6699, 0xdbe7, 0x66, 0x6f0f, 0x3e78, 0x7b33, 0xefef, 0xffef, 0xefef, 0x28ef,
138 0x7fd, 0x1b0e, 0x6672, 0x5272, 0xdb66, 0xbddb, 0xbd66, 0xdbbd, 0x525, 0x7525, 0x525, 0x5525, 0xff00, 0x0, 0x4024, 0xa850,
139 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xbfb0, 0xb0b0, 0xb0bf, 0xbf, 0x6600, 0x99, 0x9900, 0x66, 0x1010, 0x1010, 0x1028, 0xc628,
140 0xe0a0, 0xe000, 0xe0a, 0xe00, 0xebeb, 0xebeb, 0xebeb, 0xebeb, 0xc366, 0x3c66, 0xc366, 0x3c66, 0x8004, 0x2211, 0x8004, 0x2211,
141 0xcf4d, 0xca4d, 0xca4d, 0xcf00, 0x83c6, 0x6c38, 0x180c, 0x603, 0xff, 0x80be, 0xa2aa, 0x2aeb, 0x6dab, 0xd729, 0xd7ab, 0x6dfe,
142 0xaabf, 0xa0bf, 0xaafb, 0xafb, 0x1010, 0x10, 0x1010, 0xd710, 0x18, 0x187e, 0x7e18, 0x1800, 0x82a, 0x1463, 0x142a, 0x880,
143 0x2418, 0x8142, 0x4281, 0x1824, 0xffff, 0xffff, 0xff, 0xff, 0xcc06, 0x3318, 0xcc60, 0x3381, 0x447c, 0x4483, 0x3844, 0xc744,
144 0x2808, 0x3000, 0x0, 0x6080, 0x7e3c, 0x99c3, 0xe7c3, 0x993c, 0x220e, 0x8838, 0x22e0, 0x8883, 0x80be, 0xa2aa, 0xaaba, 0x82fe,
145 0x40, 0x5c5c, 0x5c40, 0x7e00, 0xaa55, 0xaa55, 0x0, 0x0, 0x0, 0x0, 0x40e0, 0x4040, 0x81c, 0x3e7f, 0xf7e3, 0xc180,
146 0x3e7f, 0x7f7f, 0x7f7f, 0x3e80, 0xeaee, 0xeaee, 0xeaee, 0xeaee, 0x10, 0x1, 0x20, 0x4, 0xf68e, 0x7efd, 0xc3bf, 0x7ff8,
147 0x8888, 0x8888, 0x8877, 0x22dd, 0x3800, 0x3800, 0x3800, 0x3800, 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xc0c0, 0xc0ff, 0xffc0, 0xc0c0,
148 0xff80, 0x8183, 0x878f, 0x9fbf, 0xa050, 0xa050, 0xa050, 0xa050, 0xd0a0, 0xd0a0, 0xd0bc, 0xf2e1, 0x8310, 0x55, 0x10, 0x8393,
149 0xff00, 0x7755, 0xdd00, 0xff00, 0x182, 0x7c54, 0x7c54, 0x7c82, 0x10, 0x10fe, 0x7c38, 0x6c44, 0x2874, 0xeac5, 0x83c5, 0xea74,
150 0x288, 0x75d8, 0xa8d8, 0x7588, 0x0, 0xaaaa, 0xaa00, 0x0, 0xcccc, 0x3333, 0xcccc, 0x3333, 0x24e7, 0x7e, 0x427e, 0xe7,
151 0x7f1f, 0xdfc7, 0xf7f1, 0xfd7c, 0x4182, 0x50a, 0x1428, 0x50a0, 0x8894, 0x2249, 0x8800, 0xaa00, 0x300, 0x6066, 0x600, 0x3033,
152 0x7744, 0x5c50, 0x7705, 0x1d11, 0xe3dd, 0x3eba, 0x3edd, 0xa3eb, 0x1c1c, 0x14e3, 0xc1e3, 0x141c, 0x2449, 0x9224, 0x9249, 0x2492,
153 0xe724, 0xbd81, 0x7e42, 0xdb18, 0xe000, 0x3800, 0xe00, 0x8300, 0x60, 0x908c, 0x43e0, 0x0
154 };
155 MWAWGraphicStyle::Pattern pat;
156 pat.m_dim=MWAWVec2i(8,8);
157 pat.m_data.resize(8);
158 pat.m_colors[0]=MWAWColor::white();
159 pat.m_colors[1]=MWAWColor::black();
160 m_patternList.push_back(pat); // none pattern
161
162 int numPatterns=int(MWAW_N_ELEMENTS(patterns))/4;
163 uint16_t const *patPtr=patterns;
164 for (int i=0; i<numPatterns; ++i) {
165 for (size_t j=0; j<8; j+=2, ++patPtr) {
166 pat.m_data[j]=uint8_t((*patPtr)>>8);
167 pat.m_data[j+1]=uint8_t((*patPtr)&0xFF);
168 }
169 m_patternList.push_back(pat);
170 }
171 }
172
173 ////////////////////////////////////////
174 //! Internal: the subdocument of a DrawTableParser
175 class SubDocument final : public MWAWSubDocument
176 {
177 public:
178 //! constructor from a zoneId
179 SubDocument(DrawTableParser &parser, MWAWInputStreamPtr const &input, MWAWEntry const &entry, MWAWFont const &font, MWAWParagraph const &para)
180 : MWAWSubDocument(&parser, input, entry)
181 , m_font(font)
182 , m_para(para)
183 {
184 }
185
186 //! destructor
187 ~SubDocument() final {}
188
189 //! the parser function
190 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
191
192 protected:
193 //! the font style
194 MWAWFont m_font;
195 //! the paragraph style
196 MWAWParagraph m_para;
197 private:
198 SubDocument(SubDocument const &orig) = delete;
199 SubDocument &operator=(SubDocument const &orig) = delete;
200 };
201
202 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
203 {
204 if (!listener || !listener->canWriteText()) {
205 MWAW_DEBUG_MSG(("DrawTableParserInternal::SubDocument::parse: no listener\n"));
206 return;
207 }
208 listener->setFont(m_font);
209 listener->setParagraph(m_para);
210 if (!m_input || !m_zone.valid() || !m_input->checkPosition(m_zone.end()))
211 return;
212 long pos = m_input->tell();
213 m_input->seek(m_zone.begin(), librevenge::RVNG_SEEK_SET);
214 while (m_input->tell()<m_zone.end() && !m_input->isEnd()) {
215 unsigned char c=(unsigned char)(m_input->readULong(1));
216 switch (c) {
217 case 0x9:
218 listener->insertTab();
219 break;
220 case 0xd:
221 if (m_input->tell()<m_zone.end())
222 listener->insertEOL();
223 break;
224 default:
225 if (c<=0x1f) {
226 MWAW_DEBUG_MSG(("DrawTableParserInternal::SubDocument::parse: find unexpected char=%x\n", (unsigned int)(c)));
227 }
228 else
229 listener->insertCharacter(c);
230 }
231 }
232 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
233 }
234
235 }
236
237 ////////////////////////////////////////////////////////////
238 // constructor/destructor, ...
239 ////////////////////////////////////////////////////////////
240 DrawTableParser::DrawTableParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
241 : MWAWGraphicParser(input, rsrcParser, header)
242 , m_state()
243 {
244 resetGraphicListener();
245 setAsciiName("main-1");
246
247 m_state.reset(new DrawTableParserInternal::State);
248
249 getPageSpan().setMargins(0.1);
250 }
251
252 DrawTableParser::~DrawTableParser()
253 {
254 }
255
256 ////////////////////////////////////////////////////////////
257 // the parser
258 ////////////////////////////////////////////////////////////
259 void DrawTableParser::parse(librevenge::RVNGDrawingInterface *docInterface)
260 {
261 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
262 bool ok = false;
263 try {
264 // create the asciiFile
265 ascii().setStream(getInput());
266 ascii().open(asciiName());
267
268 checkHeader(nullptr);
269
270 ok = createZones();
271 if (ok) {
272 createDocument(docInterface);
273 sendShapes();
274 }
275 ascii().reset();
276 }
277 catch (...) {
278 MWAW_DEBUG_MSG(("DrawTableParser::parse: exception catched when parsing\n"));
279 ok = false;
280 }
281
282 resetGraphicListener();
283 if (!ok) throw(libmwaw::ParseException());
284 }
285
286 ////////////////////////////////////////////////////////////
287 // create the document
288 ////////////////////////////////////////////////////////////
289 void DrawTableParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
290 {
291 if (!documentInterface) return;
292 if (getGraphicListener()) {
293 MWAW_DEBUG_MSG(("DrawTableParser::createDocument: listener already exist\n"));
294 return;
295 }
296
297 // create the page list
298 MWAWPageSpan ps(getPageSpan());
299 ps.setPageSpan(1);
300 // check if the document has multiple pages, if yes increase the page
301 if (ps.getFormLength()>0 && 1.02*ps.getFormLength()*72<m_state->m_maxDim[0]) {
302 int numPageY=int(m_state->m_maxDim[0]/ps.getFormLength()/72)+1;
303 MWAW_DEBUG_MSG(("DrawTableParser::createDocument: increase Y pages to %d\n", numPageY));
304 ps.setFormLength(ps.getFormLength()*(numPageY>10 ? 10 : numPageY));
305 }
306 if (ps.getFormWidth()>0 && 1.02*ps.getFormWidth()*72<m_state->m_maxDim[1]) {
307 int numPageX=int(m_state->m_maxDim[1]/ps.getFormWidth()/72)+1;
308 MWAW_DEBUG_MSG(("DrawTableParser::createDocument: increase X pages to %d\n", numPageX));
309 ps.setFormWidth(ps.getFormWidth()*(numPageX>10 ? 10 : numPageX));
310 }
311 std::vector<MWAWPageSpan> pageList(1,ps);
312 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
313 setGraphicListener(listen);
314 listen->startDocument();
315 }
316
317 ////////////////////////////////////////////////////////////
318 //
319 // Intermediate level
320 //
321 ////////////////////////////////////////////////////////////
322 bool DrawTableParser::createZones()
323 {
324 auto input=getInput();
325 if (!input) return false;
326
327 if (!readPrefs() || !readPrintInfo() || !readFonts())
328 return false;
329
330 long pos=input->tell();
331 if (!computeMaxDimension())
332 return false;
333 input->seek(pos, librevenge::RVNG_SEEK_SET);
334 return true;
335 }
336
337 bool DrawTableParser::computeMaxDimension()
338 {
339 auto input=getInput();
340 int numShapes=0;
341 while (input->checkPosition(input->tell()+10)) {
342 long pos=input->tell();
343 int val=int(input->readULong(2));
344 if (val==0)
345 continue;
346 if (val!=6)
347 break;
348 int type=int(input->readULong(2));
349 if (type<=1 || type>=10)
350 break;
351 input->seek(2, librevenge::RVNG_SEEK_CUR); // flags
352 int headerSz=int(input->readULong(4));
353 long endPos=pos+10+headerSz;
354 if (!input->checkPosition(endPos))
355 break;
356 long dimPos=0;
357 switch (type) {
358 case 2:
359 case 3:
360 dimPos=endPos-8;
361 break;
362 case 4:
363 dimPos=pos+22;
364 break;
365 case 7:
366 case 9:
367 dimPos=endPos-10;
368 break;
369 case 8:
370 dimPos=endPos-16;
371 break;
372 default:
373 break;
374 }
375 if (dimPos>=pos+10 && dimPos+8<=endPos) {
376 input->seek(dimPos, librevenge::RVNG_SEEK_SET);
377 for (int pt=0; pt<2; ++pt) {
378 if (type==4 && pt==1)
379 break;
380 float dim[2];
381 for (auto &d : dim) d=float(input->readLong(2))/10.f;
382 for (int i=0; i<2; ++i) {
383 if (dim[i]>m_state->m_maxDim[i])
384 m_state->m_maxDim[i]=dim[i];
385 }
386 }
387 }
388 input->seek(endPos, librevenge::RVNG_SEEK_SET);
389 int const numData[]= {
390 0, 0, 0, 0, 0,
391 2, 0, 1, 2, 1
392 };
393 bool ok=true;
394 for (int i=0; i<numData[type]; ++i) {
395 pos=input->tell();
396 int dSz=int(input->readULong(2));
397 if (!input->checkPosition(pos+2+dSz)) {
398 ok=false;
399 break;
400 }
401 if (type==5 && i==0 && (dSz%4)==0) {
402 for (int pt=0; pt<(dSz/4); ++pt) {
403 float dim[2];
404 for (auto &d : dim) d=float(input->readLong(2))/10.f;
405 for (int j=0; j<2; ++j) {
406 if (dim[j]>m_state->m_maxDim[j])
407 m_state->m_maxDim[j]=dim[j];
408 }
409 }
410 }
411 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
412 }
413 if (!ok)
414 break;
415 ++numShapes;
416 }
417 return numShapes>0;
418 }
419
420 bool DrawTableParser::readFonts()
421 {
422 auto input=getInput();
423 libmwaw::DebugStream f;
424 auto fontConverter=getFontConverter();
425 while (input->checkPosition(input->tell()+6)) {
426 long pos=input->tell();
427 if (input->readULong(2)!=2) {
428 input->seek(pos, librevenge::RVNG_SEEK_SET);
429 return true;
430 }
431 f.str("");
432 f << "Entries(Font):";
433 int dataSz[2];
434 for (auto &d : dataSz) d=int(input->readULong(2));
435 if (dataSz[0]>dataSz[1])
436 std::swap(dataSz[0],dataSz[1]);
437 if (!input->checkPosition(pos+6+dataSz[1])) {
438 MWAW_DEBUG_MSG(("DrawTableParser::readFonts: zone seems too short\n"));
439 f << "###";
440 ascii().addPos(pos);
441 ascii().addNote(f.str().c_str());
442 input->seek(pos, librevenge::RVNG_SEEK_SET);
443 return false;
444 }
445 int type=int(input->readULong(2));
446 bool ok=false;
447 switch (type) {
448 case 0:
449 ok=true;
450 break;
451 case 1: {
452 if (dataSz[0]<3) {
453 MWAW_DEBUG_MSG(("DrawTableParser::readFonts: the data size seems to short\n"));
454 break;
455 }
456 int id=int(input->readULong(2));
457 f << "id=" << id << ",";
458 int dSz=int(input->readULong(1));
459 if (3+dSz>dataSz[0])
460 break;
461 ok=true;
462 std::string name;
463 for (int s=0; s<dSz; ++s) name+=char(input->readULong(1));
464 if (!name.empty())
465 fontConverter->setCorrespondance(id, name);
466 f << name << ",";
467 break;
468 }
469 default:
470 f << "type=" << type << ",";
471 MWAW_DEBUG_MSG(("DrawTableParser::readFonts: unknown type\n"));
472 break;
473 }
474 if (!ok)
475 f << "###";
476 ascii().addPos(pos);
477 ascii().addNote(f.str().c_str());
478 input->seek(pos+6+dataSz[1], librevenge::RVNG_SEEK_SET);
479 }
480 return true;
481 }
482
483 bool DrawTableParser::readPrintInfo()
484 {
485 auto input=getInput();
486 long pos=input->tell();
487 libmwaw::DebugStream f;
488 f << "Entries(PrintInfo):";
489 auto sz = long(input->readULong(2));
490 if (sz < 0x78 || !input->checkPosition(pos+2+sz)) {
491 MWAW_DEBUG_MSG(("DrawTableParser::readPrintInfo: can not find the print info zone\n"));
492 f << "###";
493 ascii().addPos(pos);
494 ascii().addNote(f.str().c_str());
495 return false;
496 }
497 // print info
498 libmwaw::PrinterInfo info;
499 if (!info.read(input))
500 f << "###";
501 else {
502 f << info;
503 MWAWVec2i paperSize = info.paper().size();
504 MWAWVec2i pageSize = info.page().size();
505 if (pageSize.x() > 0 && pageSize.y() > 0 && paperSize.x() > 0 && paperSize.y() > 0) {
506 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
507 MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1);
508
509 getPageSpan().setMarginTop(lTopMargin.y()<0 ? 0 : lTopMargin.y()/72.0);
510 getPageSpan().setMarginBottom(rBotMargin.y()<0 ? 0 : rBotMargin.y()/72.0);
511 getPageSpan().setMarginLeft(lTopMargin.x()<0 ? 0 : lTopMargin.x()/72.0);
512 getPageSpan().setMarginRight(rBotMargin.y()<0 ? 0 : rBotMargin.y()/72.0);
513 getPageSpan().setFormLength(paperSize.y()/72.);
514 getPageSpan().setFormWidth(paperSize.x()/72.);
515 }
516 }
517 ascii().addPos(pos);
518 ascii().addNote(f.str().c_str());
519 input->seek(pos+2+sz, librevenge::RVNG_SEEK_SET);
520 return true;
521 }
522
523 bool DrawTableParser::readPrefs()
524 {
525 auto input=getInput();
526 long pos=input->tell();
527 if (!input->checkPosition(pos+172)) {
528 MWAW_DEBUG_MSG(("DrawTableParser::readPrefs: the zone is too short\n"));
529 return false;
530 }
531 libmwaw::DebugStream f;
532 int val;
533 f << "Entries(Prefs):";
534 for (int i=0; i<4; ++i) {
535 val=int(input->readULong(2));
536 int const expected[]= { 0 /* or 1*/, 4 /* 2-4*/, 3, 0xc };
537 if (val==expected[i]) continue;
538 if (i==2)
539 f << "font[id]=" << val << ",";
540 else if (i==3)
541 f << "font[sz]=" << val << ",";
542 else
543 f << "f" << i << "=" << val << ",";
544 }
545 for (int i=0; i<3; ++i) { // f4=0|1
546 val=int(input->readULong(2));
547 if (!val) continue;
548 if (i==0)
549 f << "font[flags]=" << std::hex << val << std::dec << ",";
550 else if (i==1) {
551 if (val&0xff00) f << "align=" << (val>>8) << ",";
552 if (val&0xff) f << "interline=" << float(2+(val&0xff))/2.f << ",";
553 }
554 else
555 f << "f" << i+4 << "=" << val << ",";
556 }
557 for (int i=0; i<2; ++i) { // always 101: an int or two boolean ?
558 val=int(input->readULong(1));
559 if (val==1) continue;
560 if (i==1)
561 f << "font[color]=" << val << ",";
562 else
563 f << "fl" << i << "=" << val << ",";
564 }
565 for (int i=0; i<4; ++i) {
566 val=int(input->readULong(2));
567 int const expected[]= { 10, 1, 2, 1 };
568 if (val!=expected[i]) f << "f" << i+7 << "=" << val << ",";
569 }
570 int dim[2];
571 for (auto &d : dim) d=int(input->readULong(2));
572 if (dim[1]&3) {
573 f << "penSize=" << (dim[0]&0x7fff) << "/" << (dim[1]>>2);
574 if ((dim[1]&3)!=1) f << "[" << (dim[1]&3) << "]";
575 if (dim[0]&0x8000) f << "_dec"; // decimal or frac
576 f << ",";
577 }
578 for (int st=0; st<2; ++st) {
579 f << (st==0 ? "line" : "surf") << "[style]=[";
580 val=int(input->readULong(1));
581 if (val==0)
582 f << "none,";
583 else if (val!=2-st)
584 f << "pat=" << val << ",";
585 for (int i=0; i<2; ++i) { // 0: back, 1: front
586 val=int(input->readULong(1));
587 if (val!=i)
588 f << "col" << i << "=" << val << ",";
589 }
590 input->seek(1, librevenge::RVNG_SEEK_CUR);
591 f << "],";
592 }
593 for (int i=0; i<13; ++i) { // g0=6|15, g4=0|1, g6=g10=1
594 val=int(input->readULong(2));
595 if (val) f << "g" << i << "=" << val << ",";
596 }
597 float fDim[2];
598 for (auto &d : fDim) d=float(input->readLong(2))/10.f;
599 f << "dim=" << MWAWVec2f(fDim[1], fDim[0]) << ",";
600 ascii().addPos(pos);
601 ascii().addNote(f.str().c_str());
602 input->seek(pos+66, librevenge::RVNG_SEEK_SET);
603
604 pos=input->tell();
605 f.str("");
606 f << "Prefs-1:";
607 for (int i=0; i<9; ++i) {
608 val=int(input->readULong(2));
609 int const expected[]= { 0 /* or 1*/, 0, 1, 1, 0 /* or 1*/, 0, 1, 0, 2 };
610 if (val==expected[i]) continue;
611 if (i==5 && val==1)
612 f << "spline,";
613 else
614 f << "f" << i << "=" << val << ",";
615 }
616 for (int i=0; i<10; ++i) { // g1=0|1
617 val=int(input->readULong(2));
618 if (val) f << "g" << i << "=" << val << ",";
619 }
620 val=int(input->readULong(2)); // 0|6|c6e
621 if (val) f << "fl=" << std::hex << val << std::dec << ",";
622 val=int(input->readULong(2)); // 0-2
623 if (val) f << "h0=" << val << ",";
624 for (auto &d : fDim) d=float(input->readLong(2))/10.f;
625 f << "dim=" << MWAWVec2f(fDim[1], fDim[0]) << ","; // unsure two times the same number
626 ascii().addPos(pos);
627 ascii().addNote(f.str().c_str());
628
629 input->seek(pos+46, librevenge::RVNG_SEEK_SET);
630
631 pos=input->tell();
632 f.str("");
633 f << "Prefs-2:";
634 for (int i=0; i<30; ++i) { // f8=0|1 f10=1
635 val=int(input->readULong(2));
636 if (val) f << "f" << i << "=" << val << ",";
637 }
638 ascii().addPos(pos);
639 ascii().addNote(f.str().c_str());
640
641 return true;
642 }
643
644 ////////////////////////////////////////////////////////////
645 // read the header
646 ////////////////////////////////////////////////////////////
647 bool DrawTableParser::checkHeader(MWAWHeader *header, bool strict)
648 {
649 MWAWInputStreamPtr input = getInput();
650 if (!input || !input->hasDataFork() || !input->checkPosition(316))
651 return false;
652
653 libmwaw::DebugStream f;
654 input->seek(0, librevenge::RVNG_SEEK_SET);
655 if (input->readULong(2)!=0xc || input->readULong(2)!=0x1357)
656 return false;
657 f << "FileHeader:";
658 int const vers=1;
659 setVersion(vers);
660 if (header)
661 header->reset(MWAWDocument::MWAW_T_DRAWINGTABLE, vers, MWAWDocument::MWAW_K_DRAW);
662 for (int i=0; i<6; ++i) { // checkme: f0=f2 some file's version?
663 int const expected[]= {0x13 /* or 14*/, 0, 0x13 /* or 14*/, 2, 2, 0xac};
664 int val=int(input->readLong(2));
665 if (val!=expected[i])
666 f << "f" << i << "=" << val << ",";
667 }
668 if (strict) {
669 long pos=input->tell();
670 input->seek(0xbc, librevenge::RVNG_SEEK_SET);
671 if (!readPrintInfo()) return false;
672 input->seek(pos, librevenge::RVNG_SEEK_SET);
673 }
674 ascii().addPos(0);
675 ascii().addNote(f.str().c_str());
676 return true;
677 }
678
679 ////////////////////////////////////////////////////////////
680 //
681 // send data
682 //
683 ////////////////////////////////////////////////////////////
684 bool DrawTableParser::sendShapes()
685 {
686 auto input=getInput();
687 auto listener=getGraphicListener();
688 if (!input || !listener) {
689 MWAW_DEBUG_MSG(("DrawTableParser::sendShapes: can not find the listener\n"));
690 return false;
691 }
692
693 while (input->checkPosition(input->tell()+2)) {
694 long pos=input->tell();
695 if (sendShape())
696 continue;
697 input->seek(pos, librevenge::RVNG_SEEK_SET);
698 break;
699 }
700 if (m_state->m_openedGroup) {
701 MWAW_DEBUG_MSG(("DrawTableParser::sendShapes: find unclosed group\n"));
702 }
703 while (m_state->m_openedGroup-->0)
704 listener->closeGroup();
705 if (!input->isEnd()) {
706 MWAW_DEBUG_MSG(("DrawTableParser::sendShapes: find extra data\n"));
707 ascii().addPos(input->tell());
708 ascii().addNote("Entries(Extra):###");
709 }
710 return true;
711 }
712
713 bool DrawTableParser::sendShape()
714 {
715 auto input=getInput();
716 auto listener=getGraphicListener();
717 if (!input || !listener)
718 return false;
719 long pos=input->tell();
720 libmwaw::DebugStream f;
721 f << "Entries(Shape):";
722 if (!input->checkPosition(pos+2))
723 return false;
724 int type=int(input->readULong(2));
725 if (type==0) { // end of shapes' list or end of group
726 if (m_state->m_openedGroup>0) {
727 --m_state->m_openedGroup;
728 listener->closeGroup();
729 }
730 ascii().addPos(pos);
731 ascii().addNote("_");
732 return true;
733 }
734 if (type!=6 || !input->checkPosition(pos+10))
735 return false;
736
737 type=int(input->readULong(2));
738 char const *wh[]= {
739 nullptr, nullptr, "line", "rect", "arc",
740 "poly", "group", "bitmap", "epsf", "text"
741 };
742 int const numData[]= {
743 0, 0, 0, 0, 0,
744 2, 0, 1, 2, 1
745 };
746 std::string what;
747 int nbData=0;
748 if (type<10 && wh[type]) {
749 what=wh[type];
750 nbData=numData[type];
751 }
752 else {
753 MWAW_DEBUG_MSG(("DrawTableParser::sendShape: find unknown shape %d\n", type));
754 f << "###";
755
756 std::stringstream s;
757 s << "Type" << type;
758 what=s.str();
759 }
760 f << what << ",";
761
762 int val=int(input->readULong(2));
763 if (val&1) f << "selected,";
764 if (val&0x40) f << "group,";
765 if (val&0x80) f << "locked,";
766 val&=0xFF3E;
767 if (val) f << "fl=" << std::hex << val << std::dec << ",";
768 val=int(input->readULong(2)); // 0
769 if (val) f << "f0=" << val << ",";
770 int headerSz=int(input->readULong(2));
771 long endPos=pos+10+headerSz;
772 if (!input->checkPosition(endPos)) {
773 MWAW_DEBUG_MSG(("DrawTableParser::sendShape: the zone seems too short\n"));
774 f << "###hSz=" << headerSz << ",";
775 ascii().addPos(pos);
776 ascii().addNote(f.str().c_str());
777 return false;
778 }
779 MWAWGraphicStyle style=MWAWGraphicStyle::emptyStyle();
780 if (headerSz>=8 && type!=9) {
781 int dim[2];
782 for (auto &d : dim) d=int(input->readULong(2));
783 if (dim[1]&3) {
784 int fDim[]= {(dim[0]&0x7fff), (dim[1]>>2)};
785 if (fDim[1]) style.m_lineWidth=72*float(fDim[0])/float(fDim[1]);
786 f << "penSize=" << fDim[0] << "/" << fDim[1];
787 if ((dim[1]&3)!=1) f << "[" << (dim[1]&3) << "]"; // main pt LT, C, BT
788 if (dim[0]&0x8000) f << "_dec"; // decimal or frac
789 f << ",";
790 }
791 for (int st=0; st<2; ++st) {
792 if (st==1 && (type==2 || headerSz<12)) break;
793 f << (st==0 ? "line" : "surface") << "[style]=[";
794 val=int(input->readULong(1));
795 if (val==0)
796 f << "none,";
797 else if (val!=2)
798 f << "pat=" << val << ",";
799 bool ok=true;
800 MWAWGraphicStyle::Pattern pat;
801 ok=val!=0 && m_state->getPattern(val, pat);
802 for (int i=0; i<2; ++i) { // 0: back, 1: front
803 val=int(input->readULong(1));
804 pat.m_colors[i]=m_state->getColor(val);
805 if (val!=i)
806 f << "col" << i << "=" << val << ",";
807 }
808 if (st==0 && type==2) {
809 val=int(input->readULong(1));
810 if (val) f << "arrow=" << val << ",";
811 if (val&1) style.m_arrows[0]=MWAWGraphicStyle::Arrow::plain();
812 if (val&2) style.m_arrows[1]=MWAWGraphicStyle::Arrow::plain();
813 }
814 else
815 input->seek(1, librevenge::RVNG_SEEK_CUR);
816 f << "],";
817 if (st==0) {
818 if (!ok)
819 style.m_lineWidth=0;
820 else
821 pat.getAverageColor(style.m_lineColor);
822 }
823 else if (ok)
824 style.setPattern(pat);
825 }
826 }
827 MWAWGraphicShape shape;
828 MWAWBox2f shapeBox;
829 int numPolyPoints=0;
830 int polyType=0;
831 MWAWBox2i bitmapBox;
832 MWAWFont font;
833 MWAWParagraph para;
834
835 switch (type) {
836 case 2: {
837 if (headerSz!=18) break;
838 val=int(input->readULong(1));
839 if (val!=1) f << "f1=" << val << ",";
840 input->seek(1, librevenge::RVNG_SEEK_CUR);
841 float dim[4];
842 for (auto &d : dim) d=float(input->readLong(2))/10.f;
843 shape=MWAWGraphicShape::line(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
844 f << MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2])) << ",";
845 shapeBox=shape.getBdBox();
846 break;
847 }
848 case 3: {
849 if (headerSz!=28) break;
850 int fl=int(input->readULong(2));
851 if (fl==1)
852 f << "round/oval,";
853 else if (fl)
854 f << "##f1=" << fl << ",";
855 int round=int(input->readULong(2));
856 if (round)
857 f << "roundSz=" << round << ",";
858 for (int i=0; i<2; ++i) { // 0,0|-1
859 val=int(input->readLong(2));
860 if (val)
861 f << "f" << i+2 << "=" << val << ",";
862 }
863 float dim[4];
864 for (auto &d : dim) d=float(input->readLong(2))/10.f;
865 shapeBox=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
866 f << shapeBox << ",";
867 if (round || fl!=1)
868 shape=MWAWGraphicShape::rectangle(shapeBox, MWAWVec2f(float(round),float(round)));
869 else
870 shape=MWAWGraphicShape::circle(shapeBox);
871 break;
872 }
873 case 4: {
874 if (headerSz!=26) break;
875 MWAWVec2f pts[2];
876 for (auto &pt : pts) {
877 float dim[2];
878 for (auto &d : dim) d=float(input->readLong(2))/10.f;
879 pt=MWAWVec2f(dim[1],dim[0]);
880 }
881 shapeBox=MWAWBox2f(pts[0]-pts[1],pts[0]+pts[1]);
882 f << shapeBox << ",";
883 val=int(input->readLong(2));
884 if (val)
885 f << "f1=" << val << ",";
886 int fileAngles[2];
887 for (auto &angl : fileAngles) angl=int(input->readLong(2));
888 f << "angle=" << fileAngles[0] << "->" << fileAngles[0]+fileAngles[1] << ",";
889 int angle[2] = { int(90-fileAngles[0]-fileAngles[1]), int(90-fileAngles[0]) };
890 if (angle[1]<angle[0])
891 std::swap(angle[0],angle[1]);
892 if (angle[1]>360) {
893 int numLoop=int(angle[1]/360)-1;
894 angle[0]-=int(numLoop*360);
895 angle[1]-=int(numLoop*360);
896 while (angle[1] > 360) {
897 angle[0]-=360;
898 angle[1]-=360;
899 }
900 }
901 if (angle[0] < -360) {
902 int numLoop=int(angle[0]/360)+1;
903 angle[0]-=int(numLoop*360);
904 angle[1]-=int(numLoop*360);
905 while (angle[0] < -360) {
906 angle[0]+=360;
907 angle[1]+=360;
908 }
909 }
910 MWAWVec2f center = shapeBox.center();
911 MWAWVec2f axis = 0.5f*MWAWVec2f(shapeBox.size());
912 // we must compute the real bd box
913 float minVal[2] = { 0, 0 }, maxVal[2] = { 0, 0 };
914 int limitAngle[2];
915 for (int i = 0; i < 2; i++)
916 limitAngle[i] = (angle[i] < 0) ? int(angle[i]/90)-1 : int(angle[i]/90);
917 for (int bord = limitAngle[0]; bord <= limitAngle[1]+1; bord++) {
918 float ang = (bord == limitAngle[0]) ? float(angle[0]) :
919 (bord == limitAngle[1]+1) ? float(angle[1]) : 90 * float(bord);
920 ang *= float(M_PI/180.);
921 float const actVal[2] = { axis[0] *std::cos(ang), -axis[1] *std::sin(ang)};
922 if (actVal[0] < minVal[0]) minVal[0] = actVal[0];
923 else if (actVal[0] > maxVal[0]) maxVal[0] = actVal[0];
924 if (actVal[1] < minVal[1]) minVal[1] = actVal[1];
925 else if (actVal[1] > maxVal[1]) maxVal[1] = actVal[1];
926 }
927 MWAWBox2f realBox(MWAWVec2f(center[0]+minVal[0],center[1]+minVal[1]),
928 MWAWVec2f(center[0]+maxVal[0],center[1]+maxVal[1]));
929 if (style.hasSurface())
930 shape = MWAWGraphicShape::pie(realBox, shapeBox, MWAWVec2f(float(angle[0]),float(angle[1])));
931 else
932 shape = MWAWGraphicShape::arc(realBox, shapeBox, MWAWVec2f(float(angle[0]),float(angle[1])));
933 shapeBox=realBox;
934 break;
935 }
936 case 5:
937 if (headerSz!=20) break;
938 for (int i=0; i<2; ++i) { // 0,0
939 val=int(input->readLong(2));
940 if (val)
941 f << "f" << i+1 << "=" << val << ",";
942 }
943 numPolyPoints=int(input->readLong(2));
944 f << "num[pts]=" << numPolyPoints << ",";
945 polyType=int(input->readLong(2));
946 f << "type=" << polyType << ","; // (type&1) means closed
947 if (polyType==0) {
948 if (!style.hasSurface())
949 shape=MWAWGraphicShape::polyline(shapeBox);
950 else
951 shape=MWAWGraphicShape::polygon(shapeBox);
952 }
953 else if (polyType==1)
954 shape=MWAWGraphicShape::polygon(shapeBox);
955 else if (polyType==2 || polyType==3)
956 shape=MWAWGraphicShape::path(shapeBox);
957 else {
958 MWAW_DEBUG_MSG(("DrawTableParser::sendShape: unknown polygon type\n"));
959 f << "###";
960 shape=MWAWGraphicShape::polyline(shapeBox);
961 polyType=0;
962 }
963 break;
964 case 7: {
965 if (headerSz!=42) break;
966 val=int(input->readLong(2));
967 style.m_rotate=float(val);
968 if (val) f << "rot=" << val << ",";
969 val=int(input->readLong(2)); // 0
970 if (val)
971 f << "f1=" << val << ",";
972 for (int i=0; i<2; ++i) {
973 int dim[4];
974 for (auto &d : dim) d=int(input->readLong(2));
975 MWAWBox2i box(MWAWVec2i(dim[1],dim[0]),MWAWVec2i(dim[3],dim[2]));
976 if (i==0)
977 bitmapBox=box;
978 f << "dim" << i << "=" << box << ",";
979 }
980 float dim[4];
981 for (auto &d : dim) d=float(input->readLong(2))/10.f;
982 shapeBox=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
983 f << shapeBox << ",";
984 val=int(input->readLong(2)); // 0
985 if (val)
986 f << "f3=" << val << ",";
987 break;
988 }
989 case 8: {
990 if (headerSz!=40) break;
991 val=int(input->readLong(2));
992 style.m_rotate=float(val);
993 if (val) f << "rot=" << val << ",";
994 float dim[4];
995 for (auto &d : dim) d=float(input->readLong(2))/10.f;
996 shapeBox=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
997 f << shapeBox << ",";
998 val=int(input->readLong(2)); // 3
999 if (val)
1000 f << "f2=" << val << ",";
1001 for (auto &d : dim) d=float(input->readLong(2))/10.f;
1002 f << "orig=" << MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2])) << ",";
1003 for (int i=0; i<2; ++i)
1004 f << "sz" << i << "=" << std::hex << input->readULong(4) << std::dec << ",";
1005 break;
1006 }
1007 case 9: {
1008 if (headerSz!=24) break;
1009 // font
1010 font.setId(int(input->readULong(2)));
1011 font.setSize(float(input->readLong(2)));
1012 uint32_t flags=0;
1013 val=int(input->readULong(2));
1014 if (val&0x1) flags |= MWAWFont::boldBit;
1015 if (val&0x2) flags |= MWAWFont::italicBit;
1016 if (val&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
1017 if (val&0x8) flags |= MWAWFont::embossBit;
1018 if (val&0x10) flags |= MWAWFont::shadowBit;
1019 font.setFlags(flags);
1020 f << font.getDebugString(getFontConverter());
1021 val&=0xffe0;
1022 if (val) f << "font[fl]=" << std::hex << val << std::dec << ",";
1023 // paragraph
1024 val=int(input->readULong(1));
1025 switch (val&3) {
1026 case 0: // left
1027 break;
1028 case 1:
1029 para.m_justify = MWAWParagraph::JustificationCenter;
1030 f << "align=center,";
1031 break;
1032 case 2:
1033 para.m_justify = MWAWParagraph::JustificationRight;
1034 f << "align=right,";
1035 break;
1036 case 3:
1037 default:
1038 MWAW_DEBUG_MSG(("DrawTableParser::sendShape: find align=3\n"));
1039 f << "###align=3,";
1040 break;
1041 }
1042 if (val&0xfc) f << "#para[align]=" << (val>>2) << ",";
1043 val=int(input->readULong(1));
1044 switch (val&3) {
1045 case 0: // 1 line
1046 break;
1047 case 1:
1048 para.setInterline(1.5, librevenge::RVNG_PERCENT);
1049 f << "interline=150%,";
1050 break;
1051 case 2:
1052 para.setInterline(2, librevenge::RVNG_PERCENT);
1053 f << "interline=200%,";
1054 break;
1055 default:
1056 MWAW_DEBUG_MSG(("DrawTableParser::sendShape: find unknown interline\n"));
1057 f << "#interline3,";
1058 }
1059 if (val&0xfc) f << "#interline=" << (val>>2) << ",";
1060 val=int(input->readLong(2));
1061 if (val) f << "f3=" << val << ",";
1062 val=int(input->readULong(1));
1063 if (val!=1) f << "f4=" << val << ",";
1064 val=int(input->readULong(1));
1065 if (val!=1) {
1066 MWAWColor color=m_state->getColor(val);
1067 font.setColor(color);
1068 f << "text[color]=" << val << ",";
1069 }
1070 val=int(input->readLong(2));
1071 style.m_rotate=float(val);
1072 if (val) f << "rot=" << val << ",";
1073 float dim[4];
1074 for (auto &d : dim) d=float(input->readLong(2))/10.f;
1075 shapeBox=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
1076 f << shapeBox << ",";
1077 f << "N=" << input->readULong(2) << ",";
1078 break;
1079 }
1080 default:
1081 break;
1082 }
1083 if (input->tell()!=endPos)
1084 ascii().addDelimiter(input->tell(),'|');
1085 ascii().addPos(pos);
1086 ascii().addNote(f.str().c_str());
1087
1088 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1089 MWAWPosition position(shapeBox[0], shapeBox.size(), librevenge::RVNG_POINT);
1090 position.m_anchorTo = MWAWPosition::Page;
1091 if (type>=2 && type<=4)
1092 listener->insertShape(position, shape, style);
1093 else if (type==6 && m_state->m_openedGroup>=0) {
1094 ++m_state->m_openedGroup;
1095 listener->openGroup(position);
1096 }
1097 for (int i=0; i<nbData; ++i) {
1098 pos=input->tell();
1099 // checkme: find how blocks with size>=65536 are stored
1100 int dSz=int(input->readULong(2));
1101 f.str("");
1102 f << "Shape-" << i << "[" << what << "]:";
1103 if (!input->checkPosition(pos+2+dSz)) {
1104 MWAW_DEBUG_MSG(("DrawTableParser::sendShape: bad size for zone %d\n", i));
1105 f << "###dSz=" << dSz << ",";
1106 ascii().addPos(pos);
1107 ascii().addNote(f.str().c_str());
1108 return false;
1109 }
1110 if (type==5) {
1111 if (i==0 && (dSz%4)==0) {
1112 f << "pts=[";
1113 std::vector<MWAWVec2f> points;
1114 for (int j=0; j<(dSz>>2); ++j) {
1115 float dim[2];
1116 for (auto &d : dim) d=float(input->readLong(2))/10.f;
1117 points.push_back(MWAWVec2f(dim[1],dim[0]));
1118 f << points.back() << ",";
1119 }
1120 f << "],";
1121 if (polyType&1 && points.size()>1)
1122 points.push_back(points[0]);
1123 if (polyType==0 || polyType==1)
1124 shape.m_vertices=points;
1125 else if (!points.empty()) {
1126 shape.m_path.push_back(MWAWGraphicShape::PathData('M', points[0]));
1127 for (size_t j=1; j+1<points.size(); ++j) {
1128 MWAWVec2f dir=points[j+1]-points[j-1];
1129 shape.m_path.push_back(MWAWGraphicShape::PathData('S', points[j], points[j]-0.1f*dir));
1130 }
1131 if (polyType==3)
1132 shape.m_path.push_back(MWAWGraphicShape::PathData('Z'));
1133 }
1134 listener->insertShape(position, shape, style);
1135 }
1136 else if (i==1 && dSz==4) {
1137 for (int j=0; j<2; ++j) { // 0,0
1138 val=int(input->readLong(2));
1139 if (val)
1140 f << "f" << j << "=" << val << ",";
1141 }
1142 }
1143 else {
1144 MWAW_DEBUG_MSG(("DrawTableParser::sendShape: can not read poly's zone %d\n", i));
1145 f << "###";
1146 }
1147 }
1148 else if (type==7) {
1149 MWAWEmbeddedObject obj;
1150 if (getBitmap(bitmapBox, obj, pos+2+dSz)) {
1151 listener->insertPicture(position, obj);
1152 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
1153 continue;
1154 }
1155 f << "###";
1156 }
1157 else if (type==8) {
1158 if (i==0) { // normally must be a apple picture
1159 MWAWBox2f box;
1160 auto res = MWAWPictData::check(input, dSz, box);
1161 if (res == MWAWPict::MWAW_R_BAD) {
1162 MWAW_DEBUG_MSG(("DrawTableParser::sendShape:: can not find the picture\n"));
1163 }
1164 else {
1165 input->seek(pos+2, librevenge::RVNG_SEEK_SET);
1166 std::shared_ptr<MWAWPict> thePict(MWAWPictData::get(input, dSz));
1167 if (thePict) {
1168 MWAWEmbeddedObject picture;
1169 if (thePict->getBinary(picture))
1170 listener->insertPicture(position, picture);
1171 }
1172 }
1173 }
1174 #ifdef DEBUG_WITH_FILES
1175 librevenge::RVNGBinaryData file;
1176 input->seek(pos+2, librevenge::RVNG_SEEK_SET);
1177 input->readDataBlock(dSz, file);
1178 static int volatile pictName = 0;
1179 libmwaw::DebugStream s;
1180 s << "PICT-" << ++pictName << (i==0 ? ".pct" : ".eps");
1181 libmwaw::Debug::dumpFile(file, s.str().c_str());
1182 ascii().skipZone(pos, pos+1+dSz);
1183 #endif
1184 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
1185 continue;
1186 }
1187 else if (type==9) {
1188 std::string text;
1189 for (int j=0; j<dSz; ++j) text+=char(input->readULong(1));
1190 f << text << ",";
1191 MWAWEntry entry;
1192 entry.setBegin(pos+2);
1193 entry.setLength(dSz);
1194 std::shared_ptr<MWAWSubDocument> doc(new DrawTableParserInternal::SubDocument(*this, getInput(), entry, font, para));
1195 listener->insertTextBox(position, doc, style);
1196 }
1197 if (input->tell()!=pos+2 && input->tell()!=pos+2+dSz) {
1198 f << "###extra";
1199 MWAW_DEBUG_MSG(("DrawTableParser::sendShape: find extra data in zone %d\n", i));
1200 ascii().addDelimiter(input->tell(),'|');
1201 }
1202 input->seek(pos+2+dSz, librevenge::RVNG_SEEK_SET);
1203 ascii().addPos(pos);
1204 ascii().addNote(f.str().c_str());
1205 }
1206 return true;
1207 }
1208
1209 bool DrawTableParser::getBitmap(MWAWBox2i const &box, MWAWEmbeddedObject &obj, long endPos)
1210 {
1211 auto input=getInput();
1212 long pos=input->tell();
1213 long dSz=endPos-pos;
1214 int Y=box.size()[1];
1215 if (Y==0 || dSz<0 || (dSz%Y)!=0) {
1216 MWAW_DEBUG_MSG(("DrawTableParser::getBitmap: unexpected bitmap size\n"));
1217 return false;
1218 }
1219
1220 long width=dSz/Y;
1221 int X=box.size()[0];
1222 if (8*width<X) {
1223 MWAW_DEBUG_MSG(("DrawTableParser::getBitmap: unexpected bitmap size\n"));
1224 return false;
1225 }
1226
1227 obj=MWAWEmbeddedObject();
1228 ascii().addPos(pos-2);
1229 ascii().addNote("Entries(Bitmap)");
1230
1231 MWAWPictBitmapIndexed pict(box.size());
1232 std::vector<MWAWColor> colors;
1233 colors.push_back(MWAWColor::black());
1234 colors.push_back(MWAWColor::white());
1235 pict.setColors(colors);
1236
1237 libmwaw::DebugStream f;
1238 for (int y=0; y<Y; ++y) {
1239 pos=input->tell();
1240 f.str("");
1241 f << "Bitmap-" << y << ":";
1242
1243 int x=0;
1244 for (int w=0; w<width; ++w) {
1245 int val=int(input->readULong(1));
1246 for (int v=0, depl=0x80; v<8; ++v, depl>>=1) {
1247 if (x>=X)
1248 break;
1249 pict.set(x++,y, (val&depl) ? 0 : 1);
1250 }
1251 }
1252 ascii().addPos(pos);
1253 ascii().addNote(f.str().c_str());
1254 input->seek(pos+width, librevenge::RVNG_SEEK_SET);
1255 }
1256 return pict.getBinary(obj);
1257 }
1258
1259 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: