Debian Codegraph @

src/lib/ScoopParser.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <cmath>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWGraphicListener.hxx"
43 #include "MWAWGraphicShape.hxx"
44 #include "MWAWGraphicStyle.hxx"
45 #include "MWAWHeader.hxx"
46 #include "MWAWParagraph.hxx"
47 #include "MWAWPictBitmap.hxx"
48 #include "MWAWPictData.hxx"
49 #include "MWAWPrinter.hxx"
50 #include "MWAWPosition.hxx"
51 #include "MWAWSubDocument.hxx"
52
53 #include "ScoopParser.hxx"
54
55 /** Internal: the structures of a ScoopParser */
56 namespace ScoopParserInternal
57 {
58 ////////////////////////////////////////
59 //! Internal: the shape of a ScoopParser
60 struct Shape {
61 //! constructor
62 Shape()
63 : m_type(-1)
64 , m_style()
65 , m_mode(0)
66
67 , m_page(0)
68 , m_rotation(0)
69 , m_verticalMode(2)
70
71 , m_vertices()
72
73 , m_textId(0)
74 , m_textLinkId(0)
75
76 , m_children()
77
78 {
79 for (auto &l : m_local) l=0;
80 for (auto &id : m_ids) id=0;
81 for (auto &f : m_flips) f=false;
82 }
83
84 //! the shape type
85 int m_type;
86 //! the graphic style
87 MWAWGraphicStyle m_style;
88 //! the mode: 0 copy, 1: or, ...
89 int m_mode;
90
91 //! the page
92 int m_page;
93 //! the bounding boxes: final and original
94 MWAWBox2f m_boxes[2];
95 //! the rotation
96 int m_rotation;
97 //! the vertical position: 0: center, 1: bottom, 2: top, 3: justify
98 int m_verticalMode;
99 //! the flip flags
100 bool m_flips[2];
101 //! the local values: circle: angles, rect: round dimension
102 int m_local[2];
103
104 //! the vertices: poly, spline, ...
105 std::vector<MWAWVec2i> m_vertices;
106
107 //! the text main id
108 long m_textId;
109 //! the text link id
110 int m_textLinkId;
111 //! the list of ids
112 long m_ids[4];
113 //! the list of entries
114 MWAWEntry m_entries[4];
115
116 //! the list of children: group
117 std::vector<Shape> m_children;
118 };
119
120 ////////////////////////////////////////
121 //! Internal: a special field of a ScoopParser
122 struct Special {
123 //! constructor
124 Special()
125 : m_type(0)
126 , m_value(0)
127 {
128 }
129 //! the special type: 1: numeric, 2: roman, 3: Alpha, 4: time, 5: date numeric, 6: date alpha
130 int m_type;
131 //! the special value
132 int m_value;
133 };
134
135 ////////////////////////////////////////
136 //! Internal: a paragraph of a ScoopParser
137 struct Paragraph {
138 //! constructor
139 Paragraph()
140 : m_numChar(0)
141 , m_text()
142 , m_cPosToFontMap()
143 , m_cPosToKernelMap()
144 , m_cPosToSpecialMap()
145 , m_paragraph()
146 {
147 }
148 //! the number of characters
149 int m_numChar;
150 //! the text entry
151 MWAWEntry m_text;
152 //! a map character position to font
153 std::map<int, MWAWFont> m_cPosToFontMap;
154 //! a map character position to kernel modifier
155 std::map<int, float> m_cPosToKernelMap;
156 //! a map character positions to special field
157 std::map<std::pair<int,int>, Special> m_cPosToSpecialMap;
158 //! the paragraph style
159 MWAWParagraph m_paragraph;
160 };
161
162 ////////////////////////////////////////
163 //! Internal: a text zone shape of a ScoopParser
164 struct TextZoneShape {
165 //! constructor
166 TextZoneShape()
167 : m_page(0)
168 , m_box()
169 , m_verticalMode(2)
170 {
171 for (auto &l : m_limits) l=0;
172 for (auto &s : m_slants) s=0;
173 for (auto &f : m_flips) f=false;
174 }
175 //! the page
176 int m_page;
177 //! the bounding box
178 MWAWBox2f m_box;
179 //! the paragraph id list corresponding to this shape
180 int m_limits[2];
181 //! the slant values: original, decal?
182 float m_slants[2];
183 //! the vertical position: 0: center, 1: bottom, 2: top, 3: justify
184 int m_verticalMode;
185 //! the flip flags
186 bool m_flips[2];
187 };
188
189 ////////////////////////////////////////
190 //! Internal: a text zone of a ScoopParser
191 struct TextZone {
192 //! constructor
193 TextZone()
194 : m_id(0)
195 , m_storyEntry()
196 , m_font()
197 , m_paragraphs()
198 , m_shapes()
199 {
200 }
201
202 //! the text zone id
203 long m_id;
204 //! the story name entry
205 MWAWEntry m_storyEntry;
206 //! the default font (or maybe the story font)
207 MWAWFont m_font;
208 //! the paragraph list
209 std::vector<Paragraph> m_paragraphs;
210 //! the list of shapes displaying this text
211 std::vector<TextZoneShape> m_shapes;
212 };
213
214 ////////////////////////////////////////
215 //! Internal: the state of a ScoopParser
216 struct State {
217 //! constructor
218 State()
219 : m_numPages(1)
220 , m_displayMode(1)
221 , m_leftPage(0)
222 , m_rightPage(-20)
223 , m_thumbnailSize(1,1)
224 , m_layoutDimension()
225 , m_hasScrapPage(false)
226 , m_patterns()
227 , m_shapes()
228 , m_idToParagraphMap()
229 , m_idToTextZoneMap()
230 {
231 initPatterns();
232 }
233
234 //! init the patterns
235 void initPatterns();
236
237 //! the number of pages
238 int m_numPages;
239 //! the display mode 0: thumbnail, 1: one page, 2: facings pages, 3: one page+scrap
240 int m_displayMode;
241 //! the left-top page
242 int m_leftPage;
243 //! the right-bottom page
244 int m_rightPage;
245 //! the number of page using in thumbnail display
246 MWAWVec2i m_thumbnailSize;
247 //! the layout dimension
248 MWAWVec2i m_layoutDimension;
249 //! a flag to know if some shape are on the scrap page
250 bool m_hasScrapPage;
251 //! the list of patterns
252 std::vector<MWAWGraphicStyle::Pattern> m_patterns;
253 //! the main list of shapes
254 std::vector<Shape> m_shapes;
255 //! the style map: id to paragraph
256 std::map<long,MWAWParagraph> m_idToParagraphMap;
257 //! the text zone map: id to text zone
258 std::map<long,TextZone> m_idToTextZoneMap;
259 };
260
261 void State::initPatterns()
262 {
263 if (!m_patterns.empty()) return;
264 uint16_t const values[]= {
265 0xffff, 0xffff, 0xffff, 0xffff, 0xddff, 0x77ff, 0xddff, 0x77ff, 0xdd77, 0xdd77, 0xdd77, 0xdd77, 0xaa55, 0xaa55, 0xaa55, 0xaa55,
266 0x55ff, 0x55ff, 0x55ff, 0x55ff, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa, 0xeedd, 0xbb77, 0xeedd, 0xbb77, 0x8888, 0x8888, 0x8888, 0x8888,
267 0xb130, 0x031b, 0xd8c0, 0x0c8d, 0x8010, 0x0220, 0x0108, 0x4004, 0xff88, 0x8888, 0xff88, 0x8888, 0xff80, 0x8080, 0xff08, 0x0808,
268 0x0000, 0x0002, 0x0000, 0x0002, 0x8040, 0x2000, 0x0204, 0x0800, 0x8244, 0x3944, 0x8201, 0x0101, 0xf874, 0x2247, 0x8f17, 0x2271,
269 0x55a0, 0x4040, 0x550a, 0x0404, 0x2050, 0x8888, 0x8888, 0x0502, 0xbf00, 0xbfbf, 0xb0b0, 0xb0b0, 0x0000, 0x0000, 0x0000, 0x0000,
270 0x8000, 0x0800, 0x8000, 0x0800, 0x8800, 0x2200, 0x8800, 0x2200, 0x8822, 0x8822, 0x8822, 0x8822, 0xaa00, 0xaa00, 0xaa00, 0xaa00,
271 0x00ff, 0x00ff, 0x00ff, 0x00ff, 0x1122, 0x4488, 0x1122, 0x4488, 0x8040, 0x2000, 0x0204, 0x0800, 0x0102, 0x0408, 0x1020, 0x4080,
272 0xaa00, 0x8000, 0x8800, 0x8000, 0xff80, 0x8080, 0x8080, 0x8080, 0x0814, 0x2241, 0x8001, 0x0204, 0x8814, 0x2241, 0x8800, 0xaa00,
273 0x40a0, 0x0000, 0x040a, 0x0000, 0x0384, 0x4830, 0x0c02, 0x0101, 0x8080, 0x413e, 0x0808, 0x14e3, 0x1020, 0x54aa, 0xff02, 0x0408,
274 0x7789, 0x8f8f, 0x7798, 0xf8f8, 0x0008, 0x142a, 0x552a, 0x1408 //, 0x0000, 0x0000, 0x0000, 0x0000,
275 };
276 size_t N=MWAW_N_ELEMENTS(values)/4;
277 m_patterns.resize(N);
278 uint16_t const *ptr=values;
279 for (size_t i=0; i<N; ++i) {
280 auto &pat=m_patterns[i];
281 pat.m_dim=MWAWVec2i(8,8);
282 pat.m_data.resize(8);
283 for (size_t j=0; j<8; j+=2) {
284 pat.m_data[j]=uint8_t(~(*ptr>>8));
285 pat.m_data[j+1]=uint8_t(~(*(ptr++)&0xff));
286 }
287 }
288
289 }
290
291 ////////////////////////////////////////
292 //! Internal: the subdocument of a ScoopParser
293 class SubDocument final : public MWAWSubDocument
294 {
295 public:
296 SubDocument(ScoopParser &pars, MWAWInputStreamPtr const &input, long zoneId, int subZoneId)
297 : MWAWSubDocument(&pars, input, MWAWEntry())
298 , m_id(zoneId)
299 , m_subId(subZoneId)
300 {
301 }
302
303 //! destructor
304 ~SubDocument() final {}
305
306 //! operator!=
307 bool operator!=(MWAWSubDocument const &doc) const final
308 {
309 if (MWAWSubDocument::operator!=(doc)) return true;
310 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
311 if (!sDoc) return true;
312 if (m_id != sDoc->m_id) return true;
313 if (m_subId != sDoc->m_subId) return true;
314 return false;
315 }
316
317 //! the parser function
318 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
319
320 protected:
321 //! the textzone id
322 long m_id;
323 //! the sub zone id
324 int m_subId;
325 private:
326 SubDocument(SubDocument const &orig) = delete;
327 SubDocument &operator=(SubDocument const &orig) = delete;
328 };
329
330 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
331 {
332 if (!listener || !listener->canWriteText()) {
333 MWAW_DEBUG_MSG(("ScoopParserInternal::SubDocument::parse: no listener\n"));
334 return;
335 }
336 if (!m_parser) {
337 MWAW_DEBUG_MSG(("ScoopParserInternal::SubDocument::parse: no parser\n"));
338 return;
339 }
340 long pos = m_input->tell();
341 static_cast<ScoopParser *>(m_parser)->sendText(m_id, m_subId);
342 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
343 }
344
345
346 }
347
348 ////////////////////////////////////////////////////////////
349 // constructor/destructor, ...
350 ////////////////////////////////////////////////////////////
351 ScoopParser::ScoopParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
352 : MWAWGraphicParser(input, rsrcParser, header)
353 , m_state(new ScoopParserInternal::State)
354 {
355 setAsciiName("main-1");
356
357 getPageSpan().setMargins(0.1);
358 }
359
360 ScoopParser::~ScoopParser()
361 {
362 }
363
364 ////////////////////////////////////////////////////////////
365 // the parser
366 ////////////////////////////////////////////////////////////
367 void ScoopParser::parse(librevenge::RVNGDrawingInterface *docInterface)
368 {
369 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
370 bool ok = false;
371 try {
372 // create the asciiFile
373 ascii().setStream(getInput());
374 ascii().open(asciiName());
375 checkHeader(nullptr);
376 ok = createZones();
377 if (ok) {
378 createDocument(docInterface);
379
380 auto listener=getGraphicListener();
381 int numPages=std::max<int>(1, m_state->m_numPages);
382 for (int p=0; p<numPages+(m_state->m_hasScrapPage ? 1 : 0);) {
383 if (p && listener)
384 listener->insertBreak(MWAWListener::PageBreak);
385 MWAWVec2i decal(0,0);
386 int pId=p>=numPages ? -3 : p;
387 switch (m_state->m_displayMode) {
388 case 2:
389 case 3:
390 if (pId==m_state->m_rightPage)
391 decal[0]=-m_state->m_layoutDimension[0];
392 break;
393 default:
394 break;
395 }
396 for (auto const &shape : m_state->m_shapes) {
397 if (shape.m_page==pId)
398 send(shape, decal);
399 }
400 if (p==0 && m_state->m_displayMode==0) {
401 p=m_state->m_thumbnailSize[0]*m_state->m_thumbnailSize[1];
402 if (p<=0) {
403 MWAW_DEBUG_MSG(("ScoopParser::parse: oops, can not use the thumbnail size\n"));
404 p=1;
405 }
406 }
407 else
408 ++p;
409 }
410 }
411 ascii().reset();
412 }
413 catch (...) {
414 MWAW_DEBUG_MSG(("ScoopParser::parse: exception catched when parsing\n"));
415 ok = false;
416 }
417
418 resetGraphicListener();
419 if (!ok) throw(libmwaw::ParseException());
420 }
421
422 ////////////////////////////////////////////////////////////
423 // create the document
424 ////////////////////////////////////////////////////////////
425 void ScoopParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
426 {
427 if (!documentInterface) return;
428 if (getGraphicListener()) {
429 MWAW_DEBUG_MSG(("ScoopParser::createDocument: listener already exist\n"));
430 return;
431 }
432
433 // create the page list
434 std::vector<MWAWPageSpan> pageList;
435 int numPages=std::max<int>(1,m_state->m_numPages);
436 if (m_state->m_displayMode==0 && m_state->m_thumbnailSize!=MWAWVec2i(1,1)) {
437 if (m_state->m_thumbnailSize[0]<1 || m_state->m_thumbnailSize[1]<1) {
438 MWAW_DEBUG_MSG(("ScoopParser::createDocument: can not use the thumbnail size, assume 1x1\n"));
439 m_state->m_thumbnailSize=MWAWVec2i(1,1);
440 }
441 else {
442 MWAWPageSpan ps(getPageSpan());
443 ps.setFormWidth(ps.getFormWidth()*m_state->m_thumbnailSize[0]);
444 ps.setFormLength(ps.getFormLength()*m_state->m_thumbnailSize[1]);
445 ps.setPageSpan(1);
446 pageList.push_back(ps);
447 numPages/=m_state->m_thumbnailSize[0]*m_state->m_thumbnailSize[1];
448 numPages=std::max<int>(0,numPages);
449 }
450 }
451 if (numPages || m_state->m_hasScrapPage) {
452 MWAWPageSpan ps(getPageSpan());
453 ps.setPageSpan(numPages+(m_state->m_hasScrapPage ? 1 : 0));
454 pageList.push_back(ps);
455 }
456 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
457 setGraphicListener(listen);
458 listen->startDocument();
459 }
460
461
462 ////////////////////////////////////////////////////////////
463 //
464 // Intermediate level
465 //
466 ////////////////////////////////////////////////////////////
467 bool ScoopParser::createZones()
468 {
469 /* the file contains also a resource fork which contains a resource
470 WWWW:19018, the windows' position, so it seems safe to ignore it */
471 MWAWInputStreamPtr input = getInput();
472 if (!input || !readHeader())
473 return false;
474 ScoopParserInternal::TextZone tZone;
475 while (readTextZone(tZone))
476 ;
477 long pos=input->tell();
478 if (!input->checkPosition(pos+4) || input->readLong(4)) {
479 MWAW_DEBUG_MSG(("ScoopParser::createZones: can not find the shape id=0\n"));
480 ascii().addPos(pos);
481 ascii().addNote("Entries(Shape):###id");
482 return false;
483 }
484 ascii().addPos(pos);
485 ascii().addNote("_");
486 if (!readShapesList(m_state->m_shapes))
487 return false;
488 if (!input->isEnd()) {
489 MWAW_DEBUG_MSG(("ScoopParser::createZones: find extra data\n"));
490 ascii().addPos(input->tell());
491 ascii().addNote("Entries(Extra):###");
492 return !m_state->m_shapes.empty();
493 }
494 return true;
495 }
496
497 ////////////////////////////////////////////////////////////
498 // send shapes
499 ////////////////////////////////////////////////////////////
500
501 bool ScoopParser::send(ScoopParserInternal::Shape const &shape, MWAWVec2i const &decal)
502 {
503 auto input=getInput();
504 auto listener=getGraphicListener();
505 if (!input || !listener) {
506 MWAW_DEBUG_MSG(("ScoopParser::send: can not find the listener\n"));
507 return false;
508 }
509 MWAWBox2f box=MWAWBox2f(shape.m_boxes[0][0]+MWAWVec2f(decal), shape.m_boxes[0][1]+MWAWVec2f(decal));
510 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
511 pos.setRelativePosition(MWAWPosition::Page);
512 MWAWGraphicShape gShape;
513
514 switch (shape.m_type) {
515 case 0: // group
516 listener->openGroup(pos);
517 for (auto const &child : shape.m_children)
518 send(child, decal);
519 listener->closeGroup();
520 return true;
521 case 3: { // line
522 gShape=MWAWGraphicShape::line(box[0], box[1]);
523 auto shapeBox=gShape.getBdBox();
524 pos=MWAWPosition(shapeBox[0], shapeBox.size(), librevenge::RVNG_POINT);
525 break;
526 }
527 case 5: // rect or rect oval
528 gShape=MWAWGraphicShape::rectangle(box,
529 MWAWVec2f(0.5f*float(shape.m_local[0]), 0.5f*float(shape.m_local[1])));
530 break;
531 case 7: { // circle or arc
532 if (shape.m_local[0]<=0 || shape.m_local[1]<=0) {
533 gShape=MWAWGraphicShape::circle(box);
534 break;
535 }
536 int angle[2] = { 90-shape.m_local[0]-shape.m_local[1], 90-shape.m_local[1] };
537 if (angle[1]>360) {
538 int numLoop=int(angle[1]/360)-1;
539 angle[0]-=numLoop*360;
540 angle[1]-=numLoop*360;
541 while (angle[1] > 360) {
542 angle[0]-=360;
543 angle[1]-=360;
544 }
545 }
546 if (angle[0] < -360) {
547 int numLoop=int(angle[0]/360)+1;
548 angle[0]-=numLoop*360;
549 angle[1]-=numLoop*360;
550 while (angle[0] < -360) {
551 angle[0]+=360;
552 angle[1]+=360;
553 }
554 }
555 MWAWVec2f center = box.center();
556 MWAWVec2f axis = 0.5f*MWAWVec2f(box.size());
557 // we must compute the real bd box
558 float minVal[2] = { 0, 0 }, maxVal[2] = { 0, 0 };
559 int limitAngle[2];
560 for (int i = 0; i < 2; i++)
561 limitAngle[i] = (angle[i] < 0) ? int(angle[i]/90)-1 : int(angle[i]/90);
562 for (int bord = limitAngle[0]; bord <= limitAngle[1]+1; bord++) {
563 float ang = (bord == limitAngle[0]) ? float(angle[0]) :
564 (bord == limitAngle[1]+1) ? float(angle[1]) : float(90 * bord);
565 ang *= float(M_PI/180.);
566 float const actVal[2] = { axis[0] *std::cos(ang), -axis[1] *std::sin(ang)};
567 if (actVal[0] < minVal[0]) minVal[0] = actVal[0];
568 else if (actVal[0] > maxVal[0]) maxVal[0] = actVal[0];
569 if (actVal[1] < minVal[1]) minVal[1] = actVal[1];
570 else if (actVal[1] > maxVal[1]) maxVal[1] = actVal[1];
571 }
572 MWAWBox2f realBox(MWAWVec2f(center[0]+minVal[0],center[1]+minVal[1]),
573 MWAWVec2f(center[0]+maxVal[0],center[1]+maxVal[1]));
574 if (shape.m_style.hasSurface())
575 gShape = MWAWGraphicShape::pie(realBox, box, MWAWVec2f(float(angle[0]),float(angle[1])));
576 else
577 gShape = MWAWGraphicShape::arc(realBox, box, MWAWVec2f(float(angle[0]),float(angle[1])));
578 break;
579 }
580 case 8: { // bitmap
581 MWAWEmbeddedObject object;
582 if (!readBitmap(shape.m_entries[0], object) || object.isEmpty()) {
583 MWAW_DEBUG_MSG(("ScoopParser::send[bitmap]: the bitmap entries does not seem valid\n"));
584 return false;
585 }
586 auto fStyle=shape.m_style;
587 if (shape.m_rotation)
588 fStyle.m_rotate=float(shape.m_rotation);
589 for (int i=0; i<2; ++i)
590 fStyle.m_flip[i]=shape.m_flips[i];
591 listener->insertPicture(pos, object, fStyle);
592 return true;
593 }
594 case 9: { // polygon
595 if (shape.m_vertices.size()<4) {
596 MWAW_DEBUG_MSG(("ScoopParser::send[poly]: the number of points seems too short\n"));
597 return false;
598 }
599 if (shape.m_style.hasSurface())
600 gShape=MWAWGraphicShape::polygon(box);
601 else
602 gShape=MWAWGraphicShape::polyline(box);
603 float scaling[]= {1,1};
604 for (int coord=0; coord<2; ++coord) {
605 float dirC=float(shape.m_vertices[1][coord]-shape.m_vertices[0][coord]);
606 if ((dirC<=0 && dirC>=0))
607 continue;
608 scaling[coord]=box.size()[coord]/dirC;
609 }
610 gShape.m_vertices.resize(shape.m_vertices.size()-2);
611 for (size_t p=2; p<shape.m_vertices.size(); ++p)
612 gShape.m_vertices[p-2]=box[0]+MWAWVec2f(scaling[0]*float(shape.m_vertices[p][0]), scaling[1]*float(shape.m_vertices[p][1]));
613 if (shape.m_style.hasSurface() && gShape.m_vertices[0]!=gShape.m_vertices.back())
614 gShape.m_vertices.push_back(gShape.m_vertices[0]);
615 break;
616 }
617 case 10: { // picture
618 MWAWEmbeddedObject object;
619 for (int i=0; i<3; ++i) {
620 auto const &entry=shape.m_entries[i];
621 if (!entry.valid())
622 continue;
623 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
624 librevenge::RVNGBinaryData picture;
625 if (!input->readDataBlock(entry.length(), picture)) {
626 MWAW_DEBUG_MSG(("ScoopParser::send[picture]: can not read a picture\n"));
627 continue;
628 }
629 object.add(picture);
630 #ifdef DEBUG_WITH_FILES
631 static int volatile pictName = 0;
632 libmwaw::DebugStream f2;
633 f2 << "PICT-" << ++pictName << ".pct";
634 libmwaw::Debug::dumpFile(picture, f2.str().c_str());
635 ascii().skipZone(entry.begin(), entry.end()-1);
636 #endif
637 }
638 if (object.isEmpty())
639 return false;
640 auto fStyle=shape.m_style;
641 if (shape.m_rotation)
642 fStyle.m_rotate=float(shape.m_rotation);
643 for (int i=0; i<2; ++i)
644 fStyle.m_flip[i]=shape.m_flips[i];
645 listener->insertPicture(pos, object, fStyle);
646 return true;
647 }
648 case 11: // layout
649 return true;
650 case 12: // diamond
651 gShape=MWAWGraphicShape::polygon(box);
652 gShape.m_vertices= {MWAWVec2f(0.5f*(box[0][0]+box[1][0]),box[0][1]),
653 MWAWVec2f(box[0][0],0.5f*(box[0][1]+box[1][1])),
654 MWAWVec2f(0.5f*(box[0][0]+box[1][0]),box[1][1]),
655 MWAWVec2f(box[1][0],0.5f*(box[0][1]+box[1][1])),
656 MWAWVec2f(0.5f*(box[0][0]+box[1][0]),box[0][1])
657 };
658 break;
659 case 13: { // cross-line
660 listener->openGroup(pos);
661 MWAWVec2f const center=box.center();
662 MWAWVec2f const dir=0.5f*box.size();
663 for (int i=0; i<12; ++i) {
664 MWAWVec2f newDir(float(std::cos(i*M_PI/12))*dir[0], float(std::sin(i*M_PI/12))*dir[1]);
665 gShape=MWAWGraphicShape::line(center-newDir, center+newDir);
666 auto shapeBox=gShape.getBdBox();
667 pos=MWAWPosition(shapeBox[0], shapeBox.size(), librevenge::RVNG_POINT);
668 listener->insertShape(pos, gShape, shape.m_style);
669 }
670 listener->closeGroup();
671 return true;
672 }
673 case 14: // multi line
674 listener->openGroup(pos);
675 for (int st=0; st<2; ++st) {
676 gShape=MWAWGraphicShape::line(MWAWVec2f(box[0][0],box[st][1]),
677 MWAWVec2f(box[1][0],box[st][1]));
678 auto shapeBox=gShape.getBdBox();
679 pos=MWAWPosition(shapeBox[0], shapeBox.size(), librevenge::RVNG_POINT);
680 listener->insertShape(pos, gShape, shape.m_style);
681 }
682 listener->closeGroup();
683 return true;
684 case 15: { // spline
685 if (shape.m_vertices.size()<4) {
686 MWAW_DEBUG_MSG(("ScoopParser::send[spline]: the number of points seems too short\n"));
687 return false;
688 }
689 if (shape.m_vertices.size()%3) {
690 MWAW_DEBUG_MSG(("ScoopParser::send[spline]: the number of points seems odd\n"));
691 }
692 float scaling[]= {1,1};
693 for (int coord=0; coord<2; ++coord) {
694 float dirC=float(shape.m_vertices[1][coord]-shape.m_vertices[0][coord]);
695 if ((dirC<=0 && dirC>=0))
696 continue;
697 scaling[coord]=box.size()[coord]/dirC;
698 }
699 std::vector<MWAWVec2f> points;
700 points.resize(shape.m_vertices.size()-2);
701 for (size_t p=2; p<shape.m_vertices.size(); ++p)
702 points[p-2]=box[0]+MWAWVec2f(scaling[0]*float(shape.m_vertices[p][0]), scaling[1]*float(shape.m_vertices[p][1]));
703
704 gShape=MWAWGraphicShape::path(box);
705 std::vector<MWAWGraphicShape::PathData> &path=gShape.m_path;
706 path.push_back(MWAWGraphicShape::PathData('M', points[0]));
707 for (size_t i=1; i+2<points.size(); i+=3)
708 path.push_back(MWAWGraphicShape::PathData('C', points[i+2], points[i], points[i+1]));
709 if (shape.m_style.hasSurface())
710 path.push_back(MWAWGraphicShape::PathData('Z'));
711 break;
712 }
713 case 17: { // text
714 std::shared_ptr<MWAWSubDocument> doc(new ScoopParserInternal::SubDocument(*this, input, shape.m_textId, shape.m_textLinkId));
715 auto textStyle=shape.m_style;
716 switch (shape.m_verticalMode) {
717 case 0:
718 textStyle.m_verticalAlignment=MWAWGraphicStyle::V_AlignCenter;
719 break;
720 case 1:
721 textStyle.m_verticalAlignment=MWAWGraphicStyle::V_AlignBottom;
722 break;
723 case 2: // top
724 break;
725 case 3:
726 textStyle.m_verticalAlignment=MWAWGraphicStyle::V_AlignJustify;
727 break;
728 default:
729 MWAW_DEBUG_MSG(("ScoopParse::send: unknown alignment %x\n", shape.m_verticalMode));
730 break;
731 }
732 if (shape.m_rotation)
733 textStyle.m_rotate=float(shape.m_rotation);
734 for (int i=0; i<2; ++i)
735 textStyle.m_flip[i]=shape.m_flips[i];
736 listener->insertTextBox(pos, doc, textStyle);
737 return true;
738 }
739 default:
740 gShape=MWAWGraphicShape::rectangle(box);
741 break;
742 }
743 listener->insertShape(pos, gShape, shape.m_style);
744 return true;
745 }
746
747 bool ScoopParser::sendText(long tZoneId, int subZone)
748 {
749 auto listener=getGraphicListener();
750 if (!listener) {
751 MWAW_DEBUG_MSG(("ScoopParser::sendText: can not find the listener\n"));
752 return false;
753 }
754 if (tZoneId==0) // ok, no text zone associated
755 return true;
756 auto const &it=m_state->m_idToTextZoneMap.find(tZoneId);
757 if (it==m_state->m_idToTextZoneMap.end()) {
758 MWAW_DEBUG_MSG(("ScoopParser::sendText: can not find zone with id=%lx\n", (unsigned long) tZoneId));
759 return false;
760 }
761 auto const &zone=it->second;
762 if (subZone<0 || size_t(subZone)>=zone.m_shapes.size()) {
763 MWAW_DEBUG_MSG(("ScoopParser::sendText: can not find the shape %d for zone with id=%lx\n", subZone, (unsigned long) tZoneId));
764 return false;
765 }
766 auto const &shape=zone.m_shapes[size_t(subZone)];
767 for (int p=shape.m_limits[0]; p<shape.m_limits[1]; ++p) {
768 if (p<0 || size_t(p)>=zone.m_paragraphs.size()) {
769 MWAW_DEBUG_MSG(("ScoopParser::sendText: find bad paragraph id for zone with id=%lx[%d]\n", (unsigned long) tZoneId, subZone));
770 break;
771 }
772 sendText(zone.m_paragraphs[size_t(p)]);
773 }
774 return true;
775 }
776
777 bool ScoopParser::sendText(ScoopParserInternal::Paragraph const &paragraph)
778 {
779 auto input=getInput();
780 auto listener=getGraphicListener();
781 if (!input || !listener) {
782 MWAW_DEBUG_MSG(("ScoopParser::sendText: can not find the listener\n"));
783 return false;
784 }
785 listener->setParagraph(paragraph.m_paragraph);
786 int numChar=paragraph.m_numChar;
787 if (numChar==0) {
788 listener->insertEOL();
789 return true;
790 }
791 if (numChar>paragraph.m_text.length()) {
792 MWAW_DEBUG_MSG(("ScoopParser::sendText: the number of characters seems too big\n"));
793 numChar=int(paragraph.m_text.length());
794 }
795 if (!input->checkPosition(paragraph.m_text.end())) {
796 MWAW_DEBUG_MSG(("ScoopParser::sendText: can not find the text zone\n"));
797 return true;
798 }
799 input->seek(paragraph.m_text.begin(), librevenge::RVNG_SEEK_SET);
800 bool lastIsKerning=false;
801 for (int cPos=0; cPos<numChar; ++cPos) {
802 auto const &fIt=paragraph.m_cPosToFontMap.find(cPos);
803 if (fIt!=paragraph.m_cPosToFontMap.end()) {
804 listener->setFont(fIt->second);
805 lastIsKerning=false;
806 }
807 auto const &kIt=paragraph.m_cPosToKernelMap.find(cPos+1);
808 if (kIt!=paragraph.m_cPosToKernelMap.end()) {
809 lastIsKerning=true;
810 auto font=listener->getFont();
811 font.setDeltaLetterSpacing(kIt->second);
812 listener->setFont(font);
813 }
814 else if (lastIsKerning) {
815 lastIsKerning=false;
816 auto font=listener->getFont();
817 font.setDeltaLetterSpacing(0);
818 listener->setFont(font);
819 }
820 unsigned char ch=(unsigned char)(input->readLong(1));
821 switch (ch) {
822 case 0x9:
823 listener->insertTab();
824 break;
825 case 0x1f: // hyphen character
826 break;
827 default:
828 if (ch<0x1f)
829 MWAW_DEBUG_MSG(("ScoopParser::sendText: find odd char c=%d\n", int(ch)));
830 else
831 listener->insertCharacter(ch);
832 break;
833 }
834 }
835 listener->insertEOL();
836 return true;
837 }
838
839 ////////////////////////////////////////////////////////////
840 // read shapes
841 ////////////////////////////////////////////////////////////
842 bool ScoopParser::readTextZone(ScoopParserInternal::TextZone &tZone)
843 {
844 tZone=ScoopParserInternal::TextZone();
845 auto input=getInput();
846 long pos=input->tell();
847 if (!input->checkPosition(pos+90))
848 return false;
849 libmwaw::DebugStream f;
850 static int id=0;
851 f << "Entries(TextZone)[" << id++ << "]:list,";
852 int val;
853 tZone.m_id=long(input->readULong(4));
854 if (!tZone.m_id || input->readULong(4)!=0x52) {
855 input->seek(pos, librevenge::RVNG_SEEK_SET);
856 return false;
857 }
858 f << "ID=" << std::hex << tZone.m_id << std::dec << ",";
859 ascii().addDelimiter(input->tell(),'|');
860 input->seek(pos+28, librevenge::RVNG_SEEK_SET);
861 ascii().addDelimiter(input->tell(),'|');
862 bool hasStoryName=false;
863 val=int(input->readULong(4));
864 if (val) {
865 hasStoryName=true;
866 f << "ID[name]=" << std::hex << val << std::dec << ",";
867 }
868 val=int(input->readULong(2)); // 40|100 : a dim?
869 f << "f0=" << val << ",";
870 val=int(input->readULong(2)); // 0
871 if (val)
872 f << "f1=" << val << ",";
873 f << "h=" << 2*72*float(input->readLong(4))/65536 << ",";
874
875 ascii().addDelimiter(input->tell(),'|');
876 input->seek(pos+46, librevenge::RVNG_SEEK_SET);
877 ascii().addDelimiter(input->tell(),'|');
878 readFont(tZone.m_font);
879 f << "font=[" << tZone.m_font.getDebugString(getFontConverter()) << "],";
880 for (int i=0; i<2; ++i) { // 0
881 val=int(input->readLong(2));
882 if (val)
883 f << "f" << i << "=" << val << ",";
884 }
885 int numShapes=int(input->readULong(2));
886 f << "num[shape]=" << numShapes << ",";
887 val=int(input->readULong(4));
888 if (val)
889 f << "shape[ID]=" << std::hex << val << std::dec << ",";
890 int numPara=int(input->readULong(2));
891 f << "num[para]=" << numPara << ",";
892 val=int(input->readULong(4));
893 if (val)
894 f << "para[ID]=" << std::hex << val << std::dec << ",";
895 for (int i=0; i<6; ++i) { // 0
896 val=int(input->readLong(2));
897 if (val)
898 f << "g" << i << "=" << val << ",";
899 }
900 ascii().addPos(pos);
901 ascii().addNote(f.str().c_str());
902
903 input->seek(pos+90, librevenge::RVNG_SEEK_SET);
904 if (hasStoryName && !readText(tZone.m_storyEntry, "name"))
905 return false;
906
907 // first look for name style
908 while (!input->isEnd()) {
909 pos=input->tell();
910 val=int(input->readULong(4));
911 if (!val) {
912 ascii().addPos(pos);
913 ascii().addNote("_");
914 break;
915 }
916 input->seek(-4, librevenge::RVNG_SEEK_CUR);
917 MWAWParagraph para;
918 if (!readParagraph(para, true))
919 return false;
920 }
921
922 for (int i=0; i<numPara; ++i) {
923 ScoopParserInternal::Paragraph para;
924 if (!readTextZoneParagraph(para, i))
925 return false;
926 tZone.m_paragraphs.push_back(para);
927 }
928
929 pos=input->tell();
930 if (!input->checkPosition(pos+numShapes*56)) {
931 input->seek(pos, librevenge::RVNG_SEEK_SET);
932 MWAW_DEBUG_MSG(("ScoopParser::readTextZone: can find some find the last sub zone\n"));
933 return false;
934 }
935 for (int n=0; n<numShapes; ++n) {
936 pos=input->tell();
937 f.str("");
938 f << "TextZone-shape" << n << ":";
939 ScoopParserInternal::TextZoneShape shape;
940 float dim[4];
941 for (auto &d : dim) d=float(input->readLong(4))/65536;
942 shape.m_box=MWAWBox2f(MWAWVec2f(dim[0],dim[1]),MWAWVec2f(dim[0]+dim[2],dim[1]+dim[3]));
943 f << "box=" << shape.m_box << ",";
944 for (int i=0; i<3; ++i) { // f1=0|1, f2=0|1
945 val=int(input->readLong(2));
946 if (!val) continue;
947 if (i==0) {
948 shape.m_page=val;
949 f << "page=" << val << ",";
950 if (val!=-3 && (val<0 || val>=m_state->m_numPages)) {
951 f << "###";
952 MWAW_DEBUG_MSG(("ScoopParser::readTextZone: find unexpected pages\n"));
953 }
954 }
955 else
956 f << "f" << i << "=" << val << ",";
957 }
958 for (auto &l: shape.m_limits) l=int(input->readULong(2));
959 shape.m_limits[1]+=shape.m_limits[0];
960 f << "pPos[para]=" << MWAWVec2i(shape.m_limits[0], shape.m_limits[1]) << ",";
961 for (int i=0; i<4; ++i) { // f3: small number
962 val=int(input->readULong(2));
963 if (val) f << "f" << i+3 << "=" << val << ",";
964 }
965 int numAround=int(input->readLong(2));
966 if (numAround) // data size 4*numAround if numAround>0 else 4
967 f << "num[around]=" << numAround << ",";
968 val=int(input->readULong(4));
969 if (val)
970 f << "ID[run,around]=" << std::hex << val << std::dec << ",";
971 int iDim[2];
972 for (auto &d : iDim) d=int(input->readLong(2));
973 if (iDim[0]||iDim[1]) {
974 shape.m_slants[0]=float(iDim[0])/256;
975 shape.m_slants[1]=float(iDim[1])/256;
976 MWAW_DEBUG_MSG(("ScoopParser::readShape: oops, retrieving slant is not implemented\n"));
977 f << "slant=[" << shape.m_slants[0] << "," << shape.m_slants[1] << "],"; // orig, decal ?
978 }
979 for (int i=0; i<2; ++i) {
980 val=int(input->readULong(1));
981 if (val==2-i) continue;
982 if (i==0) {
983 shape.m_verticalMode=val&3;
984 f << "vertical[mode]=" << shape.m_verticalMode << ","; // 0: center, 2: top
985 if (val&4) {
986 shape.m_flips[0]=true;
987 f << "flip[hori],";
988 }
989 if (val&8) {
990 shape.m_flips[1]=true;
991 f << "flip[vertical],";
992 }
993 val&=0xf0;
994 if (val)
995 f << "fl1=" << std::hex << val << ",";
996 }
997 else
998 f << "g0=" << val << ",";
999 }
1000 for (int i=0; i<5; ++i) {
1001 val=int(input->readLong(2));
1002 if (val)
1003 f << "g" << i+1 << "=" << val << ",";
1004 }
1005 tZone.m_shapes.push_back(shape);
1006 ascii().addPos(pos);
1007 ascii().addNote(f.str().c_str());
1008 input->seek(pos+56, librevenge::RVNG_SEEK_SET);
1009
1010 if (numAround<0) {
1011 pos=input->tell();
1012 f.str("");
1013 f << "TextZone-wrap" << n << ":";
1014 if (!input->checkPosition(pos+4)) {
1015 input->seek(pos, librevenge::RVNG_SEEK_SET);
1016 MWAW_DEBUG_MSG(("ScoopParser::readTextZone: can find some find the round around zone\n"));
1017 return false;
1018 }
1019 f << "f0=" << input->readLong(4) << ",";
1020 ascii().addPos(pos);
1021 ascii().addNote(f.str().c_str());
1022 }
1023 else if (numAround>0) {
1024 pos=input->tell();
1025 f.str("");
1026 f << "TextZone-wrap" << n << ":";
1027 long len=long(input->readULong(4));
1028 if (len<0 || (long)((unsigned long)pos+4+(unsigned long)len)<pos+4 || !input->checkPosition(pos+4+len)) {
1029 input->seek(pos, librevenge::RVNG_SEEK_SET);
1030 MWAW_DEBUG_MSG(("ScoopParser::readTextZone: can find some find the round around zone\n"));
1031 return false;
1032 }
1033 if (len<4*numAround) {
1034 f << "###";
1035 MWAW_DEBUG_MSG(("ScoopParser::readTextZone: the round around size seems too short\n"));
1036 numAround=0;
1037 }
1038 f << "sz=[";
1039 for (int i=0; i<numAround; ++i) {
1040 int wrap[2];
1041 for (auto &w : wrap) w=int(input->readLong(2));
1042 f << MWAWVec2i(wrap[0],wrap[1]) << ",";
1043 }
1044 f << "],";
1045 ascii().addPos(pos);
1046 ascii().addNote(f.str().c_str());
1047 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
1048 }
1049 }
1050 if (!tZone.m_shapes.empty() && tZone.m_shapes.back().m_limits[1]<numPara) // force the last shape to display the remaining text
1051 tZone.m_shapes.back().m_limits[1]=numPara;
1052 if (m_state->m_idToTextZoneMap.find(tZone.m_id)!=m_state->m_idToTextZoneMap.end()) {
1053 MWAW_DEBUG_MSG(("ScoopParser::readTextZone: find dupplicated text zone id=%lx\n", (unsigned long) tZone.m_id));
1054 }
1055 else
1056 m_state->m_idToTextZoneMap[tZone.m_id]=tZone;
1057 return true;
1058 }
1059
1060 bool ScoopParser::readTextZoneParagraph(ScoopParserInternal::Paragraph &para, int id)
1061 {
1062 auto input=getInput();
1063 long pos=input->tell();
1064 libmwaw::DebugStream f;
1065
1066 f.str("");
1067 f << "TextZone-para" << id << "[beg]:";
1068 if (!input->checkPosition(pos+56)) {
1069 f << "###";
1070 MWAW_DEBUG_MSG(("ScoopParser::readTextZoneParagraph: can find the second sub zone part\n"));
1071 ascii().addPos(pos);
1072 ascii().addNote(f.str().c_str());
1073 return false;
1074 }
1075 int val;
1076 float dim[2];
1077 for (auto &d : dim) d=float(input->readLong(4))/65536;
1078 f << "h=" << MWAWVec2f(dim[0],dim[0]+dim[1]) << ",";
1079
1080 bool hasSpecial=false;
1081 val=int(input->readULong(4));
1082 if (val) {
1083 hasSpecial=true;
1084 f << "special[ID]=" << std::hex << val << std::dec << ",";
1085 }
1086 for (int i=0; i<5; ++i) { // 0
1087 val=int(input->readLong(2));
1088 if (val)
1089 f << "f" << i << "=" << val << ",";
1090 }
1091 int numCharStyle=int(input->readULong(2));
1092 f << "num[char,style]=" << numCharStyle << ",";
1093 f << "ID[cStyle]=" << std::hex << input->readULong(4) << std::dec << ",";
1094 for (int i=0; i<3; ++i) {
1095 val=int(input->readLong(2));
1096 if (val)
1097 f << "f" << i+5 << "=" << val << ",";
1098 }
1099 int numKerns=int(input->readLong(2));
1100 if (numKerns) f << "num[kern]=" << numKerns << ",";
1101 val=int(input->readULong(4));
1102 if (val)
1103 f << "ID[kern]=" << std::hex << val << std::dec << ",";
1104 for (int i=0; i<3; ++i) {
1105 val=int(input->readLong(2));
1106 if (val)
1107 f << "f" << i+3 << "=" << val << ",";
1108 }
1109 para.m_numChar=int(input->readULong(2));
1110 if (para.m_numChar)
1111 f << "text[len]=" << para.m_numChar << ",";
1112 f << "IDs[text]=["; // text, style
1113 for (int i=0; i<2; ++i) {
1114 val=int(input->readULong(4));
1115 if (val)
1116 f << std::hex << val << std::dec << ",";
1117 else
1118 f << "_,";
1119 }
1120 f << "],";
1121 ascii().addPos(pos);
1122 ascii().addNote(f.str().c_str());
1123 input->seek(pos+56, librevenge::RVNG_SEEK_SET);
1124
1125 if (hasSpecial) {
1126 pos=input->tell();
1127 long len=long(input->readULong(4));
1128 long endPos=pos+4+len;
1129 if (len<0 || endPos<pos+4 || !input->checkPosition(endPos)) {
1130 input->seek(pos, librevenge::RVNG_SEEK_SET);
1131 MWAW_DEBUG_MSG(("ScoopParser::readTextZoneParagraph: can find the special zone\n"));
1132 return false;
1133 }
1134 static bool first=true;
1135 if (first) {
1136 // as each special field is replaced by some text in the text zone, we can ignore them
1137 first=false;
1138 MWAW_DEBUG_MSG(("ScoopParser::readTextZoneParagraph: this file contains some special fields, the conversion will ignore them\n"));
1139 }
1140 f.str("");
1141 f << "TextZone-special:";
1142 ascii().addPos(pos);
1143 ascii().addNote(f.str().c_str());
1144 int n=0;
1145 while (input->tell()<endPos) {
1146 pos=input->tell();
1147 f.str("");
1148 f << "TextZone-Sp" << n++ << ":";
1149 len=long(input->readULong(4));
1150 if (len<16 || pos+len<pos+4 || pos+len>endPos) {
1151 MWAW_DEBUG_MSG(("ScoopParser::readTextZoneParagraph: can not find a special\n"));
1152 f << "###";
1153 ascii().addPos(pos);
1154 ascii().addNote(f.str().c_str());
1155 break;
1156 }
1157 int cPos[2];
1158 for (auto &p : cPos) p=int(input->readULong(2));
1159 f << "cPos=" << MWAWVec2i(cPos[0],cPos[0]+cPos[1]) << ",";
1160 ScoopParserInternal::Special special;
1161 special.m_type=int(input->readLong(2));
1162 f << "type=" << special.m_type << ",";
1163 for (int i=0; i<3; ++i) { // f2: the value
1164 val=int(input->readLong(2));
1165 if (!val) continue;
1166 if (i==2) special.m_value=val;
1167 f << "f" << i << "=" << val << ",";
1168 }
1169 if (len!=16)
1170 ascii().addDelimiter(input->tell(),'|');
1171 para.m_cPosToSpecialMap[std::make_pair(cPos[0],cPos[0]+cPos[1])]=special;
1172 ascii().addPos(pos);
1173 ascii().addNote(f.str().c_str());
1174 input->seek(pos+len, librevenge::RVNG_SEEK_SET);
1175 }
1176 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1177 }
1178 if (numCharStyle) {
1179 pos=input->tell();
1180 f.str("");
1181 f << "Entries(CStyle):";
1182 long len=long(input->readULong(4));
1183 long endPos=pos+4+len;
1184 if (len<0 || endPos<pos+4 || !input->checkPosition(endPos)) {
1185 input->seek(pos, librevenge::RVNG_SEEK_SET);
1186 MWAW_DEBUG_MSG(("ScoopParser::readTextZoneParagraph: can find the char style zone\n"));
1187 return false;
1188 }
1189 if (len<numCharStyle*18) {
1190 MWAW_DEBUG_MSG(("ScoopParser::readTextZoneParagraph: the char style zone seems too short\n"));
1191 f << "###";
1192 numCharStyle=0;
1193 }
1194 ascii().addPos(pos);
1195 ascii().addNote(f.str().c_str());
1196
1197 auto fontConverter=getFontConverter();
1198 int cPos=0;
1199 for (int i=0; i<numCharStyle; ++i) {
1200 pos=input->tell();
1201 f.str("");
1202 f << "CStyle" << i << ":";
1203 int cLen=int(input->readULong(2));
1204 f << "pos=" << cPos << ",";
1205 MWAWFont font;
1206 readFont(font);
1207 f << font.getDebugString(fontConverter) << ",";
1208 para.m_cPosToFontMap[cPos]=font;
1209 cPos+=cLen;
1210 ascii().addPos(pos);
1211 ascii().addNote(f.str().c_str());
1212 input->seek(pos+18, librevenge::RVNG_SEEK_SET);
1213 }
1214 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1215 }
1216 if (numKerns) {
1217 pos=input->tell();
1218 long len=long(input->readULong(4));
1219 if (len<0 || (long)((unsigned long)pos+4+(unsigned long)len)<pos+4 || !input->checkPosition(pos+4+len)) {
1220 input->seek(pos, librevenge::RVNG_SEEK_SET);
1221 MWAW_DEBUG_MSG(("ScoopParser::readTextZoneParagraph: can find the kerns' zone\n"));
1222 return false;
1223 }
1224 f.str("");
1225 f << "TextZone-kern:";
1226 if (len<4*numKerns) {
1227 MWAW_DEBUG_MSG(("ScoopParser::readTextZoneParagraph: the number of kerns seems bad\n"));
1228 f << "###";
1229 numKerns=0;
1230 }
1231 f << "kerns=[";
1232 int cPos=0;
1233 for (int i=0; i<numKerns; ++i) {
1234 cPos+=int(input->readULong(2));
1235 float kernel=float(input->readLong(2))/256;
1236 para.m_cPosToKernelMap[cPos]=kernel;
1237 f << kernel << ":c=" << cPos << ",";
1238 }
1239 f << "],";
1240 ascii().addPos(pos);
1241 ascii().addNote(f.str().c_str());
1242 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
1243 }
1244 if ((para.m_numChar && !readText(para.m_text, "para")) || !readParagraph(para.m_paragraph))
1245 return false;
1246 return true;
1247 }
1248
1249 bool ScoopParser::readFont(MWAWFont &font)
1250 {
1251 font=MWAWFont();
1252 auto input=getInput();
1253 long pos=input->tell();
1254 libmwaw::DebugStream f;
1255
1256 if (!input->checkPosition(pos+16)) {
1257 MWAW_DEBUG_MSG(("ScoopParser::readFont[font]: the zone seems too short\n"));
1258 f << "###";
1259 font.m_extra=f.str();
1260 return false;
1261 }
1262
1263 font.setId(int(input->readULong(2)));
1264 font.setSize(float(input->readULong(1)));
1265 input->seek(1, librevenge::RVNG_SEEK_CUR);
1266 int val=int(input->readULong(2));
1267 uint32_t flags=0;
1268 if (val&0x1) flags |= MWAWFont::boldBit;
1269 if (val&0x2) flags |= MWAWFont::italicBit;
1270 if (val&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
1271 if (val&0x8) flags |= MWAWFont::embossBit;
1272 if (val&0x10) flags |= MWAWFont::shadowBit;
1273 if (val&0xffe0) f << "fl=#" << std::hex << (val&0xffe0) << std::dec << ",";
1274 font.setFlags(flags);
1275
1276 val=int(input->readULong(2));
1277 if (val!=0x700)
1278 f << "unk=" << float(val)/0x100 << ","; // related to streching, maybe related to delta spacing
1279 val=int(input->readLong(2));
1280 if (val)
1281 font.set(MWAWFont::Script(-float(val)/256, librevenge::RVNG_POINT));
1282 val=int(input->readULong(2));
1283 if (val!=0x100)
1284 font.setWidthStreching(float(val)/256);
1285 for (int i=0; i<2; ++i) { // 0
1286 val=int(input->readLong(2));
1287 if (val) f << "f" << i << "=" << val << ",";
1288 }
1289 input->seek(pos+16, librevenge::RVNG_SEEK_SET);
1290 font.m_extra=f.str();
1291 return true;
1292 }
1293
1294 bool ScoopParser::readParagraph(MWAWParagraph &para, bool define)
1295 {
1296 auto input=getInput();
1297 long pos=input->tell();
1298 libmwaw::DebugStream f;
1299
1300 para=MWAWParagraph();
1301 f << "Entries(PStyle):";
1302 if (!input->checkPosition(pos+4)) {
1303 f << "###";
1304 MWAW_DEBUG_MSG(("ScoopParser::readParagraph: the zone seems too short\n"));
1305 ascii().addPos(pos);
1306 ascii().addNote(f.str().c_str());
1307 return false;
1308 }
1309 long id=long(input->readULong(4));
1310 if (define && !id) {
1311 f << "###";
1312 MWAW_DEBUG_MSG(("ScoopParser::readParagraph: can not find the text id\n"));
1313 ascii().addPos(pos);
1314 ascii().addNote(f.str().c_str());
1315 return false;
1316 }
1317 if (id) {
1318 f << "ID=" << std::hex << id << std::dec << ",";
1319 if (!define) {
1320 auto sIt=m_state->m_idToParagraphMap.find(id);
1321 if (sIt!=m_state->m_idToParagraphMap.end())
1322 para=sIt->second;
1323 else {
1324 MWAW_DEBUG_MSG(("ScoopParser::readParagraph: unknown style id=%lx\n", (unsigned long) id));
1325 }
1326 ascii().addPos(pos);
1327 ascii().addNote(f.str().c_str());
1328 return true;
1329 }
1330 }
1331
1332 if (!input->checkPosition(pos+4+58) || input->readULong(4)!=0x36) {
1333 f << "###";
1334 MWAW_DEBUG_MSG(("ScoopParser::readParagraph: unexpected data length\n"));
1335 ascii().addPos(pos);
1336 ascii().addNote(f.str().c_str());
1337 return false;
1338 }
1339 bool hasName=false;
1340 int val=int(input->readULong(4));
1341 if (val) {
1342 hasName=true;
1343 f << "name[has,ID]=" << std::hex << val << std::dec << ",";
1344 }
1345 for (int i=0; i<4; ++i) { // f0=0|1
1346 val=int(input->readLong(2));
1347 if (val==(i==0 ? 1 : 0))
1348 continue;
1349 if (i==1) {
1350 para.m_spacings[2]=float(val)/256/72;
1351 f << "after[line]=" << float(val)/256 << "pt,";
1352 }
1353 else if (i==2) {
1354 para.setInterline(double(val)/256, librevenge::RVNG_POINT);
1355 f << "line[fixed]=" << float(val)/256 << "pt,";
1356 }
1357 else if (i==3) {
1358 para.m_spacings[1]=float(val)/256/72; // checkme
1359 f << "bef[line]=" << float(val)/256 << "pt,";
1360 }
1361 else // does f0=0 means default, ie. ignore margins?
1362 f << "f0=" << val << ",";
1363 }
1364 val=int(input->readLong(2));
1365 if (val!=0x100) {
1366 para.setInterline(double(val)/256, librevenge::RVNG_PERCENT, MWAWParagraph::AtLeast);
1367 f << "interline=" << float(val)/256 << "%,";
1368 }
1369 for (int i=0; i<5; ++i) {
1370 val=int(input->readLong(2));
1371 int const expected[]= {0xcd, 0x200, 0xcd, 0x180, 0x100};
1372 if (val==expected[i]) continue;
1373 char const *wh[]= {"min[word,spacing]", "max[word,spacing]", "min[letter,spacing]", "max[letter,spacing]", "raggedness"};
1374 f << wh[i] << "=" << float(val)/256 << "%,";
1375 }
1376 val=int(input->readLong(2));
1377 switch (val) {
1378 case 2: // left
1379 break;
1380 case 0:
1381 para.m_justify = MWAWParagraph::JustificationCenter;
1382 f << "center,";
1383 break;
1384 case 1:
1385 para.m_justify = MWAWParagraph::JustificationRight;
1386 f << "right,";
1387 break;
1388 case 3:
1389 para.m_justify = MWAWParagraph::JustificationFull;
1390 f << "justify,";
1391 break;
1392 default:
1393 MWAW_DEBUG_MSG(("ScoopParser::readParagraph: find unknown alignment\n"));
1394 f << "###align=" << val << ",";;
1395 break;
1396 }
1397 val=int(input->readLong(2));
1398 if (val)
1399 f << "f4=" << val << ",";
1400 para.m_marginsUnit=librevenge::RVNG_POINT;
1401 for (int i=0; i<3; ++i) {
1402 val=int(input->readLong(4));
1403 if (!val) continue;
1404 // check validity sometimes right margin is too big
1405 f << (i==0 ? "left" : i==1 ? "right" : "indent") << "[margin]=" << float(val)/65536 << ",";
1406 if (val<-200*65536 || val>200*65536) {
1407 f << "###";
1408 static bool first=true;
1409 if (first) {
1410 MWAW_DEBUG_MSG(("ScoopParser::readParagraph: some margins seem bad\n"));
1411 first=false;
1412 }
1413 }
1414 else
1415 para.m_margins[i<2 ? 1+i : 0]=double(val)/65536*(i==1 ? -1 : 1);
1416 }
1417 *para.m_margins[0]-=*para.m_margins[1];
1418 val=int(input->readLong(2));
1419 if (val!=0x2400)
1420 f << "inter[tab]=" << float(val)/256 << ",";
1421
1422 int numTabs=int(input->readULong(2));
1423 if (numTabs)
1424 f << "num[tabs]=" << numTabs << ",";
1425 val=int(input->readULong(4));
1426 if (val)
1427 f << "tab[ID]=" << std::hex << val << std::dec << ",";
1428 for (int i=0; i<3; ++i) {
1429 val=int(input->readLong(2));
1430 if (val!=-1)
1431 f << "g" << i+1 << "=" << val << ",";
1432 }
1433 ascii().addPos(pos);
1434 ascii().addNote(f.str().c_str());
1435 input->seek(pos+4+58, librevenge::RVNG_SEEK_SET);
1436
1437 MWAWEntry nameEntry;
1438 if (hasName && !readText(nameEntry, "stylename"))
1439 return false;
1440
1441 if (numTabs) {
1442 pos=input->tell();
1443 f.str("");
1444 f << "PStyle-tabs:";
1445 long len=long(input->readLong(4));
1446 if (len<0 || (long)((unsigned long)pos+4+(unsigned long)len)<pos+4 || !input->checkPosition(pos+4+len)) {
1447 f << "###";
1448 MWAW_DEBUG_MSG(("ScoopParser::readParagraph: unexpected tabs length\n"));
1449 ascii().addPos(pos);
1450 ascii().addNote(f.str().c_str());
1451 return false;
1452 }
1453
1454 if (len<6*numTabs) {
1455 f << "###";
1456 MWAW_DEBUG_MSG(("ScoopParser::readParagraph: the number of tabs seems bad\n"));
1457 numTabs=0;
1458 }
1459 f << "tabs=[";
1460 for (int i=0; i<numTabs; ++i) {
1461 MWAWTabStop tab;
1462 tab.m_position=float(input->readLong(4))/65536/72;
1463 val=int(input->readULong(1));
1464 switch (val&3) {
1465 case 0:
1466 tab.m_alignment = MWAWTabStop::CENTER;
1467 break;
1468 case 1:
1469 tab.m_alignment = MWAWTabStop::RIGHT;
1470 break;
1471 case 2:
1472 break;
1473 case 3:
1474 tab.m_alignment = MWAWTabStop::DECIMAL;
1475 break;
1476 default:
1477 break;
1478 }
1479 f << tab << ",";
1480 if (val&0xfc) f << "fl=" << std::hex << (val&0xfc) << std::dec << ",";
1481 input->seek(1, librevenge::RVNG_SEEK_CUR);
1482 para.m_tabs->push_back(tab);
1483 }
1484 f << "],";
1485 ascii().addPos(pos);
1486 ascii().addNote(f.str().c_str());
1487 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
1488 }
1489
1490 if (id)
1491 m_state->m_idToParagraphMap[id]=para;
1492 return true;
1493 }
1494
1495 bool ScoopParser::readText(MWAWEntry &entry, std::string const &what)
1496 {
1497 entry=MWAWEntry();
1498
1499 auto input=getInput();
1500 long pos=input->tell();
1501 libmwaw::DebugStream f;
1502
1503 f << "TextZone-" << what << ":";
1504 long len=long(input->readULong(4));
1505 if ((long)((unsigned long)pos+4+(unsigned long)len)<pos+4 || !input->checkPosition(pos+4+len)) {
1506 f << "###";
1507 MWAW_DEBUG_MSG(("ScoopParser::readText:can not find the text\n"));
1508 ascii().addPos(pos);
1509 ascii().addNote(f.str().c_str());
1510 return false;
1511 }
1512 std::string text;
1513 for (long i=0; i<len; ++i) text+=char(input->readLong(1));
1514 f << text << ",";
1515 entry.setBegin(pos+4);
1516 entry.setLength(len);
1517 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
1518 ascii().addPos(pos);
1519 ascii().addNote(f.str().c_str());
1520 return true;
1521 }
1522
1523 bool ScoopParser::readShapesList(std::vector<ScoopParserInternal::Shape> &shapes)
1524 {
1525 auto input=getInput();
1526 long pos=input->tell();
1527 libmwaw::DebugStream f;
1528 f << "Entries(Shape):";
1529 if (!input->checkPosition(pos+4)) {
1530 MWAW_DEBUG_MSG(("ScoopParser::readShapesList: the header seems bad\n"));
1531 f << "###";
1532 ascii().addPos(pos);
1533 ascii().addNote(f.str().c_str());
1534 return false;
1535 }
1536
1537 long len=long(input->readULong(4));
1538 long endPos=pos+4+len;
1539 if (len<0 || endPos<pos+4 || !input->checkPosition(endPos) || (len%80)!=0) {
1540 f << "###";
1541 MWAW_DEBUG_MSG(("ScoopParser::readShapesList:can not find the zone's length\n"));
1542 ascii().addPos(pos);
1543 ascii().addNote(f.str().c_str());
1544 return false;
1545 }
1546
1547 ascii().addPos(pos);
1548 ascii().addNote(f.str().c_str());
1549 int N=int(len/80);
1550 shapes.reserve(size_t(N));
1551 for (int i=0; i<N; ++i) {
1552 ScoopParserInternal::Shape shape;
1553 if (!readShape(shape, i))
1554 return false;
1555 shapes.push_back(shape);
1556 }
1557 return true;
1558 }
1559
1560 bool ScoopParser::readShape(ScoopParserInternal::Shape &shape, int id)
1561 {
1562 auto input=getInput();
1563 long pos=input->tell();
1564 libmwaw::DebugStream f;
1565 f << "Shape-" << id << ":";
1566 if (!input->checkPosition(pos+80)) {
1567 MWAW_DEBUG_MSG(("ScoopParser::readShape: the zone seems too short\n"));
1568 f << "###";
1569 ascii().addPos(pos);
1570 ascii().addNote(f.str().c_str());
1571 return false;
1572 }
1573 int val;
1574 shape.m_type=int(input->readULong(1));
1575 if (shape.m_type&0x80)
1576 f << "selected,";
1577 shape.m_type&=0x7f;
1578 char const *wh[]= { "group", nullptr, nullptr, "line", nullptr,
1579 "rect", nullptr, "circle", "bitmap", "poly",
1580 "picture", "layout", "diamond", "cross[line]", "multi[line]",
1581 "spline", nullptr, "text"
1582 };
1583 std::string what;
1584 if (shape.m_type<=17 && wh[shape.m_type])
1585 what=wh[shape.m_type];
1586 else {
1587 std::stringstream s;
1588 s << "typ" << shape.m_type;
1589 what=s.str();
1590 }
1591 f << what << ",";
1592 int patterns[]= {155, 0};
1593 for (int i=0; i<5; ++i) {
1594 val=int(input->readULong(1));
1595 int const expected[]= { 0 /*|-50*/, 0x11, 0, 155, 0};
1596 if (val==expected[i]) continue;
1597 if (i==1) {
1598 MWAWVec2i penSize=MWAWVec2i((val>>4),val&0xf);
1599 shape.m_style.m_lineWidth=float(penSize[0]+penSize[1])/2;
1600 f << "sz=" << penSize << ",";
1601 }
1602 else if (i==2) {
1603 shape.m_mode=val;
1604 f << "mode=" << val << ","; // 0: copy, 1: or, ...
1605 }
1606 else if (i==3) {
1607 patterns[0]=val;
1608 f << "pat[line]=" << val << ","; // 0-.. pat , 0x80+... gray
1609 }
1610 else if (i==4) {
1611 patterns[1]=val;
1612 f << "pat[surf]=" << val << ",";
1613 }
1614 else // 1: pos, 2: size, 4:existence, 8: pen setting, 10: attribute, 20: originality, 40:associated with layout???, 80:associated with layout???
1615 f << "lock=" << std::hex << val << std::dec << ",";
1616 }
1617
1618 // time to affect the color
1619 for (int i=0; i<2; ++i) {
1620 if (patterns[i]==0) { // none
1621 if (i==0)
1622 shape.m_style.m_lineWidth=0;
1623 }
1624 else if (patterns[i]>=128 && patterns[i]<=255) {
1625 uint8_t grey=uint8_t(1+2*(patterns[i]-128));
1626 MWAWColor color(grey, grey, grey);
1627 if (i==0)
1628 shape.m_style.m_lineColor=color;
1629 else
1630 shape.m_style.setSurfaceColor(color);
1631 }
1632 else if (patterns[i]>0 && size_t(patterns[i])<=m_state->m_patterns.size()) {
1633 auto const &pattern=m_state->m_patterns[size_t(patterns[i]-1)];
1634 if (i==0) {
1635 MWAWColor color;
1636 if (pattern.getAverageColor(color))
1637 shape.m_style.m_lineColor=color;
1638 }
1639 else
1640 shape.m_style.setPattern(pattern);
1641 }
1642 else {
1643 MWAW_DEBUG_MSG(("ScoopParser::readShape: find unknown pattern %d\n", patterns[i]));
1644 }
1645 }
1646 for (int i=0; i<2; ++i) { // f6=0|-3
1647 val=int(input->readLong(2));
1648 if (!val) continue;
1649 if (i==1) {
1650 shape.m_page=val;
1651 f << "page=" << val << ",";
1652 // can -2 or -1 appear? does this mean master page?
1653 if (val==-3)
1654 m_state->m_hasScrapPage=true;
1655 else if (val<0 || val>=m_state->m_numPages) {
1656 MWAW_DEBUG_MSG(("ScoopParser::readShape: find bad page=%d\n", val));
1657 f << "###";
1658 }
1659 }
1660 else
1661 f << "f0=" << val << ",";
1662 }
1663 val=int(input->readULong(1));
1664 if (val!=0) {
1665 shape.m_verticalMode=val&3;
1666 f << "vertical[mode]=" << shape.m_verticalMode << ","; // 0: center, 2: top
1667 if (val&4) {
1668 shape.m_flips[0]=true;
1669 f << "flip[hori],";
1670 }
1671 if (val&8) {
1672 shape.m_flips[1]=true;
1673 f << "flip[verti],";
1674 }
1675 // val&0x80 can also appear with bitmap, unsure what this means
1676 if (shape.m_type==17) {
1677 if (val&0x80) // ie. it is possible to write postscript commands and display them
1678 f << "as[graphic],";
1679 val&=0x7f;
1680 }
1681 val&=0xf8;
1682 if (val)
1683 f << "fl=" << std::hex << val << std::dec << ",";
1684 }
1685 val=int(input->readLong(1));
1686 if (val!=1) // main ff, sometime 1
1687 f << "fl1=" << val << ",";
1688 for (int st=0; st<2; ++st) {
1689 float dim[4];
1690 for (auto &d : dim) d=float(input->readLong(2));
1691 shape.m_boxes[st]=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
1692 if (st==0)
1693 f << "box=" << shape.m_boxes[st] << ",";
1694 else if (shape.m_boxes[st]!=MWAWBox2f() && shape.m_boxes[1]!=shape.m_boxes[0] &&
1695 shape.m_type!=5 && shape.m_type!=7 && shape.m_type!=8) // unsure, this box is often broken
1696 f << "box[orig]=" << shape.m_boxes[st] << ",";
1697 }
1698 switch (shape.m_type) {
1699 case 11:
1700 f << "id=" << input->readLong(1) << ",";
1701 input->seek(1, librevenge::RVNG_SEEK_CUR);
1702 val=int(input->readLong(2));
1703 if (val) f << "f1=" << val << ",";
1704 break;
1705 case 0:
1706 case 8:
1707 case 9:
1708 case 10:
1709 case 15:
1710 shape.m_ids[0]=long(input->readULong(4));
1711 if (shape.m_ids[0])
1712 f << "ID[" << what << "]=" << std::hex << shape.m_ids[0] << std::dec << ",";
1713 break;
1714 default:
1715 for (int i=0; i<2; ++i) shape.m_local[i]=int(input->readULong(2));
1716 if (!shape.m_local[0] && !shape.m_local[1]) break;
1717
1718 if (shape.m_type==5)
1719 f << "round=" << MWAWVec2i(shape.m_local[0],shape.m_local[1]) << ",";
1720 else if (shape.m_type==7)
1721 f << "arc,angles=" << MWAWVec2i(shape.m_local[1],shape.m_local[0]+shape.m_local[1]) << ",";
1722 else
1723 f << "unkn=" << MWAWVec2i(shape.m_local[0],shape.m_local[1]) << ",";
1724 break;
1725 }
1726 for (int i=0; i<2; ++i) {
1727 shape.m_ids[i+1]=int(input->readULong(4));
1728 if (!shape.m_ids[i+1]) continue;
1729 if (i==0 && shape.m_type==17) { // special this text data is stored in previous zone
1730 std::swap(shape.m_ids[i+1], shape.m_textId);
1731 f << "ID[text]=" << std::hex << shape.m_textId << std::dec << ",";
1732 }
1733 else
1734 f << "ID" << i+1 << "=" << std::hex << shape.m_ids[i+1] << std::dec << ",";
1735 }
1736 for (int i=0; i<10; ++i) {
1737 val=int(input->readLong(2));
1738 if (!val) continue;
1739 if (i==3) {
1740 if (shape.m_type==17) {
1741 shape.m_textLinkId=val;
1742 f << "link[id]=" << val << ",";
1743 }
1744 else if (shape.m_type==8)
1745 f << "res=" << val << ",";
1746 else
1747 f << "g" << i << "=" << val << ","; // find g3=0|1|2 in header zones
1748 }
1749 else if (i==5) {
1750 shape.m_rotation=val;
1751 f << "rot=" << val << ",";
1752 }
1753 else
1754 f << "g" << i << "=" << val << ",";
1755 }
1756 val=int(input->readULong(4)); // really big number
1757 if (val)
1758 f << "unkn1=" << std::hex << val << std::dec << ",";
1759 for (int i=0; i<6; ++i) { // 0
1760 val=int(input->readLong(2));
1761 if (val)
1762 f << "h" << i << "=" << val << ",";
1763 }
1764 shape.m_ids[3]=long(input->readULong(4)); // a bitmap to replace the content (flip mode)
1765 if (shape.m_ids[3])
1766 f << "ID[bitmap,final]=" << std::hex << shape.m_ids[3] << std::dec << ",";
1767 input->seek(pos+80, librevenge::RVNG_SEEK_SET);
1768 ascii().addPos(pos);
1769 ascii().addNote(f.str().c_str());
1770
1771 for (int c=0; c<4; ++c) {
1772 if (!shape.m_ids[c]) continue;
1773 if (c==0 && shape.m_type==0) {
1774 if (!readShapesList(shape.m_children))
1775 return false;
1776 continue;
1777 }
1778 if (shape.m_type==11 && c==2)
1779 continue;
1780 pos=input->tell();
1781 long len=long(input->readULong(4));
1782 if ((long)((unsigned long)pos+4+(unsigned long)len)<pos+4 || !input->checkPosition(pos+4+len)) {
1783 MWAW_DEBUG_MSG(("ScoopParser::readShape: can not find a child at position=%lx\n", (unsigned long) pos));
1784 input->seek(pos, librevenge::RVNG_SEEK_SET);
1785 return true;
1786 }
1787
1788 shape.m_entries[c].setBegin(pos+4);
1789 shape.m_entries[c].setLength(len);
1790
1791 f.str("");
1792 f << "Shape[data]:" << what << ",";
1793 if (c==0) {
1794 switch (shape.m_type) {
1795 case 9: // poly
1796 case 15: { // spline
1797 if (len<2) {
1798 MWAW_DEBUG_MSG(("ScoopParser::readShape: the vertices zone seems bad\n"));
1799 f << "###";
1800 break;
1801 }
1802 long len2=long(input->readULong(2));
1803 if (len2<len || (len2%4)!=2) {
1804 MWAW_DEBUG_MSG(("ScoopParser::readShape: can not determine the number of vertices\n"));
1805 f << "###";
1806 break;
1807 }
1808
1809 int N=int(len2/4);
1810 f << "pts=[";
1811 for (int i=0; i<N; ++i) {
1812 int coords[2];
1813 for (auto &co : coords) co=int(input->readLong(2));
1814 shape.m_vertices.push_back(MWAWVec2i(coords[1],coords[0]));
1815 f << shape.m_vertices.back() << ",";
1816 }
1817 f << "],";
1818 break;
1819 }
1820 // case 10: // picture
1821 default:
1822 break;
1823 }
1824 }
1825 else if (c==3) {
1826 MWAWEmbeddedObject bitmap;
1827 if (readBitmap(shape.m_entries[c], bitmap, false))
1828 shape.m_entries[c]=MWAWEntry();
1829 }
1830
1831 ascii().addPos(pos);
1832 ascii().addNote(f.str().c_str());
1833 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
1834 }
1835
1836 return true;
1837 }
1838
1839 bool ScoopParser::readBitmap(MWAWEntry const &entry, MWAWEmbeddedObject &object, bool compressed)
1840 {
1841 auto input=getInput();
1842 object=MWAWEmbeddedObject();
1843 if (!input) return false;
1844 if (!entry.valid() || !input->checkPosition(entry.end()) || entry.length()<14) {
1845 MWAW_DEBUG_MSG(("ScoopParser::readBitmap: the data seems too short\n"));
1846 return false;
1847 }
1848 libmwaw::DebugStream f;
1849 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1850 f << "ID=" << std::hex << input->readULong(4) << std::dec << ",";
1851 auto numColByRow=static_cast<int>(input->readLong(2));
1852 f << "numCol[byRow]=" << numColByRow << ",";
1853 int dim[4];
1854 for (auto &d : dim) d=static_cast<int>(input->readULong(2));
1855 f << "dim=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ",";
1856 if (dim[2]<dim[0] || numColByRow*8 < dim[3]-dim[1] || dim[1]<0) {
1857 MWAW_DEBUG_MSG(("ScoopParser::readBitmap: the dimension seems bad\n"));
1858 f << "###";
1859 ascii().addPos(entry.begin()-4);
1860 ascii().addNote(f.str().c_str());
1861 return false;
1862 }
1863 ascii().addPos(entry.begin()-4);
1864 ascii().addNote(f.str().c_str());
1865
1866 MWAWPictBitmapIndexed pict(MWAWVec2i(dim[3],dim[2]));
1867 std::vector<MWAWColor> colors= {MWAWColor::white(), MWAWColor::black()};
1868 pict.setColors(colors);
1869
1870 for (int r=dim[0]; r<dim[2]; ++r) {
1871 long pos=input->tell();
1872 f.str("");
1873 f << "bitmap-R" << r << ":";
1874 if (input->tell()+1>entry.end()) {
1875 MWAW_DEBUG_MSG(("ScoopParser::readBitmap: can not read row %d\n", r));
1876 f << "###";
1877 ascii().addPos(pos);
1878 ascii().addNote(f.str().c_str());
1879 return false;
1880 }
1881 int col=dim[1];
1882 while ((col-dim[1])<8*numColByRow && input->tell()<entry.end()) { // UnpackBits
1883 auto wh=compressed ? static_cast<int>(input->readULong(1)) : 256;
1884 if (wh>=0x81) {
1885 auto color=static_cast<int>(input->readULong(1));
1886 for (int j=0; j < 0x101-wh; ++j) {
1887 for (int b=7; b>=0; --b) {
1888 if (col<dim[3])
1889 pict.set(col, r, (color>>b)&1);
1890 ++col;
1891 }
1892 }
1893 }
1894 else { // checkme normally 0x80 is reserved and almost nobody used it (for ending the compression)
1895 if (input->tell()+wh+1>entry.end()) {
1896 MWAW_DEBUG_MSG(("ScoopParser::readBitmap: can not read row %d\n", r));
1897 f << "###";
1898 ascii().addPos(pos);
1899 ascii().addNote(f.str().c_str());
1900 return false;
1901 }
1902 for (int j=0; j < wh+1; ++j) {
1903 auto color=static_cast<int>(input->readULong(1));
1904 for (int b=7; b>=0; --b) {
1905 if (col<dim[3])
1906 pict.set(col, r, (color>>b)&1);
1907 ++col;
1908 }
1909 }
1910 }
1911 }
1912 }
1913 ascii().skipZone(entry.begin()+14, input->tell()-1);
1914 return pict.getBinary(object);
1915 }
1916
1917 ////////////////////////////////////////////////////////////
1918 // read the header
1919 ////////////////////////////////////////////////////////////
1920 bool ScoopParser::checkHeader(MWAWHeader *header, bool strict)
1921 {
1922 *m_state = ScoopParserInternal::State();
1923 MWAWInputStreamPtr input = getInput();
1924 if (!input || !input->hasDataFork() || !input->checkPosition(288))
1925 return false;
1926
1927 libmwaw::DebugStream f;
1928 f << "FileHeader:";
1929 input->seek(0, librevenge::RVNG_SEEK_SET);
1930 if (input->readULong(4)!=0 || input->readULong(4)!=0x70 || input->readULong(2)!=0x1100) return false;
1931
1932 if (strict) {
1933 // look for the printer information structure
1934 input->seek(0x7c, librevenge::RVNG_SEEK_SET);
1935 if (input->readULong(4)!=0x78)
1936 return false;
1937 // look if the first zone is a text zone or a list of shape
1938 input->seek(0x118, librevenge::RVNG_SEEK_SET);
1939 bool hasId=input->readULong(4)!=0;
1940 long len=long(input->readULong(4));
1941 if (len<0 || !input->checkPosition(0x118+len) || (hasId && len!=0x52) || (!hasId && (len%80)!=0))
1942 return false;
1943 }
1944
1945 ascii().addPos(0);
1946 ascii().addNote(f.str().c_str());
1947 if (header)
1948 header->reset(MWAWDocument::MWAW_T_SCOOP, 1, MWAWDocument::MWAW_K_DRAW);
1949
1950 return true;
1951 }
1952
1953 ////////////////////////////////////////////////////////////
1954 // try to read the header zone
1955 ////////////////////////////////////////////////////////////
1956 bool ScoopParser::readHeader()
1957 {
1958 MWAWInputStreamPtr input = getInput();
1959 if (!input->checkPosition(124)) {
1960 MWAW_DEBUG_MSG(("ScoopParser::readHeader: the header zone seems too short\n"));
1961 ascii().addPos(10);
1962 ascii().addNote("Entries(Header):#");
1963 return false;
1964 }
1965 input->seek(10,librevenge::RVNG_SEEK_SET);
1966 libmwaw::DebugStream f;
1967 f << "fl=" << std::hex << input->readULong(2) << std::dec << ","; // 0404, 9bff
1968 int val;
1969 for (int i=0; i<2; ++i) {
1970 val=int(input->readULong(2));
1971 int const expected[]= {0x600, 0x101 };
1972 if (val==expected[i]) continue;
1973 f << "f" << i << "=" << std::hex << val << std::dec << ",";
1974 }
1975 f << "fl2=" << std::hex << input->readULong(2) << std::dec << ","; // 30|1174|170|470
1976 val=int(input->readULong(1));
1977 if (val!=0x2)
1978 f << "f2=" << std::hex << val << std::dec << ",";
1979 m_state->m_displayMode=int(input->readULong(1));
1980 if (m_state->m_displayMode!=1)
1981 f << "display[mode]=" << m_state->m_displayMode << ",";
1982 int dim[2];
1983 for (auto &d : dim) d=int(input->readULong(2));
1984 f << "unkn=" << MWAWVec2i(dim[1],dim[0]) << ",";
1985 for (auto &d : dim) d=int(input->readULong(2));
1986 m_state->m_thumbnailSize=MWAWVec2i(dim[1],dim[0]);
1987 if (m_state->m_thumbnailSize!=MWAWVec2i(1,1))
1988 f << "num[pages]=" << m_state->m_thumbnailSize << ",";
1989 int dim4[4];
1990 for (auto &d : dim4) d=int(input->readULong(2));
1991 f << "box[layout?]=" << MWAWBox2i(MWAWVec2i(dim4[0],dim4[1]),MWAWVec2i(dim4[2],dim4[3])) << ",";
1992 for (int i=0; i<4; ++i) {
1993 val=int(input->readLong(2));
1994 if (!val) continue;
1995 f << "f" << i+2 << "=" << val << ",";
1996 }
1997 m_state->m_numPages=int(input->readULong(2));
1998 if (m_state->m_numPages!=1) {
1999 f << "num[pages]=" << m_state->m_numPages << ",";
2000 if (m_state->m_numPages>100) {
2001 MWAW_DEBUG_MSG(("ScoopParser::readHeader: the number of pages seems bad, limits it to 100 pages\n"));
2002 f << "###";
2003 m_state->m_numPages=100;
2004 }
2005 }
2006 for (int i=0; i<3; ++i) { // g0=small negatif number: -20, ..
2007 val=int(input->readLong(2));
2008 int const expected[]= {-20, 0, 0x1ff};
2009 if (val==expected[i])
2010 continue;
2011 if (i==0) {
2012 m_state->m_rightPage=val;
2013 f << "page[right]=" << val << ",";
2014 }
2015 else if (i==1) {
2016 m_state->m_leftPage=val;
2017 f << "page[left]=" << val << ",";
2018 }
2019 else
2020 f << "g" << i << "=" << val << ",";
2021 }
2022 f << "ID=" << std::hex << input->readULong(4) << std::dec << ",";
2023 val=int(input->readULong(4));
2024 if (val)
2025 f << "ID1=" << std::hex << val << std::dec << ",";
2026 ascii().addPos(0);
2027 ascii().addNote(f.str().c_str());
2028 input->seek(60, librevenge::RVNG_SEEK_SET);
2029
2030 long pos=input->tell();
2031 f.str("");
2032 f << "FileHeader-A:";
2033 for (auto &d : dim) d=int(input->readULong(2));
2034 m_state->m_layoutDimension=MWAWVec2i(dim[1],dim[0]);
2035 f << "dim=" << m_state->m_layoutDimension << ",";
2036 ascii().addDelimiter(input->tell(),'|');
2037 ascii().addPos(pos);
2038 ascii().addNote(f.str().c_str());
2039 input->seek(pos+64, librevenge::RVNG_SEEK_SET);
2040
2041 if (!readPrintInfo())
2042 return false;
2043
2044 pos=input->tell();
2045 f.str("");
2046 f << "FileHeader-B:";
2047 if (!input->checkPosition(pos+32)) {
2048 MWAW_DEBUG_MSG(("ScoopParser::readHeader: can not find the end of the header\n"));
2049 f << "###";
2050 ascii().addPos(pos);
2051 ascii().addNote(f.str().c_str());
2052 }
2053 for (int i=0; i<8; ++i) {
2054 val=int(input->readULong(4));
2055 // maybe some size: frame size?, frame dataB size?, ..., frame end size,
2056 int const expected[]= {82, 56, 56, 40, 18, 54, 32, 0};
2057 if (val!=expected[i])
2058 f << "f" << i << "=" << val << ",";
2059 }
2060
2061 input->seek(pos+32, librevenge::RVNG_SEEK_SET);
2062 ascii().addPos(pos);
2063 ascii().addNote(f.str().c_str());
2064 return true;
2065 }
2066
2067 ////////////////////////////////////////////////////////////
2068 // try to read the print info zone
2069 ////////////////////////////////////////////////////////////
2070 bool ScoopParser::readPrintInfo()
2071 {
2072 MWAWInputStreamPtr input = getInput();
2073 long pos=input->tell();
2074 long len=long(input->readULong(4));
2075 long endPos=pos+4+len;
2076 libmwaw::DebugStream f;
2077 f << "Entries(PrintInfo):";
2078 if (len<120 || endPos<pos+124 || !input->checkPosition(endPos)) {
2079 MWAW_DEBUG_MSG(("ScoopParser::readPrintInfo: file seems too short\n"));
2080 f << "###";
2081 ascii().addPos(pos);
2082 ascii().addNote(f.str().c_str());
2083 return false;
2084 }
2085 libmwaw::PrinterInfo info;
2086 if (!info.read(input)) {
2087 MWAW_DEBUG_MSG(("ScoopParser::readPrintInfo: can not read print info\n"));
2088 return false;
2089 }
2090 f << info;
2091 MWAWVec2i paperSize = info.paper().size();
2092 MWAWVec2i pageSize = info.page().size();
2093 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
2094 paperSize.x() <= 0 || paperSize.y() <= 0) {
2095 ascii().addPos(pos);
2096 ascii().addNote(f.str().c_str());
2097 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2098 return true;
2099 }
2100
2101 // define margin from print info
2102 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
2103 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
2104
2105 // move margin left | top
2106 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
2107 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
2108 lTopMargin -= MWAWVec2i(decalX, decalY);
2109 rBotMargin += MWAWVec2i(decalX, decalY);
2110
2111 // decrease right | bottom
2112 int rightMarg = rBotMargin.x() -50;
2113 if (rightMarg < 0) rightMarg=0;
2114 int botMarg = rBotMargin.y() -50;
2115 if (botMarg < 0) botMarg=0;
2116
2117 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
2118 getPageSpan().setMarginBottom(botMarg/72.0);
2119 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
2120 getPageSpan().setMarginRight(rightMarg/72.0);
2121 getPageSpan().setFormLength(paperSize.y()/72.);
2122 getPageSpan().setFormWidth(paperSize.x()/72.);
2123
2124 ascii().addPos(pos);
2125 ascii().addNote(f.str().c_str());
2126
2127 if (input->tell()!=endPos) {
2128 ascii().addPos(input->tell());
2129 ascii().addNote("PrintInfo-extra:###");
2130 }
2131 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2132 return true;
2133 }
2134
2135 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: