Debian Codegraph @

src/lib/FreeHandParser.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <algorithm>
35 #include <cmath>
36 #include <iomanip>
37 #include <iostream>
38 #include <limits>
39 #include <map>
40 #include <set>
41 #include <sstream>
42 #include <utility>
43
44 #include <librevenge/librevenge.h>
45
46 #include "MWAWFont.hxx"
47 #include "MWAWFontConverter.hxx"
48 #include "MWAWGraphicListener.hxx"
49 #include "MWAWGraphicShape.hxx"
50 #include "MWAWGraphicStyle.hxx"
51 #include "MWAWHeader.hxx"
52 #include "MWAWParagraph.hxx"
53 #include "MWAWPictBitmap.hxx"
54 #include "MWAWPictData.hxx"
55 #include "MWAWPrinter.hxx"
56 #include "MWAWPosition.hxx"
57 #include "MWAWSubDocument.hxx"
58
59 #include "FreeHandParser.hxx"
60
61 /** Internal: the structures of a FreeHandParser */
62 namespace FreeHandParserInternal
63 {
64 //! the different zone type
65 enum ZoneType { Z_Unknown, Z_Color, Z_ColorGroup, Z_Dash, Z_DashGroup, Z_Data, Z_Fill, Z_FillGroup, Z_Group,
66 Z_LineStyle, Z_LineStyleGroup, Z_Note, Z_Picture, Z_PictureName, Z_String, Z_Shape, Z_StyleGroup
67 };
68
69 /** struct which defines the screen parameters in FreeHandParserInternal */
70 struct ScreenMode {
71 //! constructor
72 ScreenMode()
73 : m_function(0)
74 , m_angle(0)
75 , m_lineByInch(0)
76 , m_value(0)
77 {
78 }
79 //! operator<<
80 friend std::ostream &operator<<(std::ostream &o, ScreenMode const &screen)
81 {
82 switch (screen.m_function) {
83 case 0: // unset
84 case -1: // default
85 break;
86 case 1:
87 o << "function=round,";
88 break;
89 case 2:
90 o << "function=line,";
91 break;
92 default:
93 MWAW_DEBUG_MSG(("FreeHandParserInternal::operator<<(ScreenMode): find unexpected screen function\n"));
94 o << "function=###" << screen.m_function << ",";
95 break;
96 }
97 if (screen.m_angle < 0 || screen.m_angle>0)
98 o << "angle=" << screen.m_angle << ",";
99 if (screen.m_lineByInch==0xFFFF)
100 o << "lineByInch*,";
101 else
102 o << "lineByInch=" << screen.m_lineByInch << ",";
103 if (screen.m_value)
104 o << "unkn0=" << screen.m_value << ",";
105 return o;
106 }
107
108 //! the function
109 int m_function;
110 //! the angle
111 float m_angle;
112 //! the line/inch
113 int m_lineByInch;
114 //! unknow value
115 int m_value;
116 };
117
118 /** small structure of FreeHandParserInternal used to stored
119 a shape header
120 */
121 struct ShapeHeader {
122 //! constructor
123 ShapeHeader()
124 : m_size(0)
125 , m_type(0)
126 , m_note("")
127 , m_dataId(0)
128 , m_layerId(-1)
129 , m_screen()
130 , m_extra("")
131 {
132 for (auto &value : m_values) value=0;
133 }
134 //! operator<<
135 friend std::ostream &operator<<(std::ostream &o, ShapeHeader const &shape)
136 {
137 if (shape.m_layerId>=0) o << "layer=" << shape.m_layerId << ",";
138 if (shape.m_dataId) o << "data=Z" << shape.m_dataId << ",";
139 if (!shape.m_note.empty()) o << "note=\"" << shape.m_note << "\",";
140 if (shape.m_screen.isSet()) o << "screen=[" << *shape.m_screen << "],";
141 for (int i=0; i<3; ++i) {
142 if (!shape.m_values[i])
143 continue;
144 int val=shape.m_values[i];
145 if (i==1 && (val&1)) {
146 o << "locked,";
147 val&=0xFFFE;
148 }
149 if (val)
150 o << "unkn" << i << "=" << val << ",";
151 }
152 if (shape.m_values[3]) // always a 1005 zone ?
153 o << "unknZone=Z" << shape.m_values[3] << ",";
154 o << shape.m_extra;
155 return o;
156 }
157 //! a field related to the zone size
158 long m_size;
159 //! the zone type
160 int m_type;
161 //! the note
162 std::string m_note;
163 //! the data id (used to store a note, ...)
164 int m_dataId;
165 //! the layer id
166 int m_layerId;
167 //! the screen mode
168 MWAWVariable<ScreenMode> m_screen;
169 //! the unknown values
170 int m_values[4];
171 //! extra data
172 std::string m_extra;
173 };
174
175 /** small structure of FreeHandParserInternal used to stored
176 a fill style
177 */
178 struct FillStyle {
179 //! constructor
180 FillStyle()
181 : m_type(MWAWGraphicStyle::Gradient::G_None)
182 , m_pattern()
183 , m_angle(0)
184 , m_logarithm(false)
185 {
186 for (auto &colorId : m_colorId) colorId=0;
187 }
188 //! the gradient type
189 MWAWGraphicStyle::Gradient::Type m_type;
190 //! the color id
191 int m_colorId[2];
192 //! the pattern
193 MWAWGraphicStyle::Pattern m_pattern;
194 //! the angle
195 float m_angle;
196 //! flag to know if a flag has logarithmic scale
197 bool m_logarithm;
198 };
199
200 /** small structure of FreeHandParserInternal used to stored
201 a line style
202 */
203 struct LineStyle {
204 //! constructor
205 LineStyle()
206 : m_width(1)
207 , m_colorId(0)
208 , m_dashId(0)
209 , m_pattern()
210 , m_miterLimit(0)
211 , m_cap(MWAWGraphicStyle::C_Butt)
212 , m_join(MWAWGraphicStyle::J_Miter)
213 {
214 }
215 //! the line width
216 float m_width;
217 //! the color id
218 int m_colorId;
219 //! the dash id
220 int m_dashId;
221 //! the pattern
222 MWAWGraphicStyle::Pattern m_pattern;
223 //! the miter limit
224 float m_miterLimit;
225 //! the line cap
226 MWAWGraphicStyle::LineCap m_cap;
227 //! the line join
228 MWAWGraphicStyle::LineJoin m_join;
229 };
230 /** small structure of FreeHandParserInternal used to stored
231 a style header
232 */
233 struct StyleHeader {
234 //! constructor
235 StyleHeader()
236 : m_size(0)
237 , m_type(0)
238 , m_labelId(0)
239 , m_screen()
240 , m_unknownValue(0)
241 , m_extra("")
242 {
243 }
244 //! operator<<
245 friend std::ostream &operator<<(std::ostream &o, StyleHeader const &style)
246 {
247 if (style.m_labelId) o << "label=Z" << style.m_labelId << ",";
248 if (style.m_screen.isSet()) o << "screen=[" << *style.m_screen << "],";
249 if (style.m_unknownValue)
250 o << "unkn0=" << style.m_unknownValue << ",";
251 o << style.m_extra;
252 return o;
253 }
254 //! a field related to the zone size
255 long m_size;
256 //! the zone type
257 int m_type;
258 //! the label id
259 int m_labelId;
260 //! the screen mode
261 MWAWVariable<ScreenMode> m_screen;
262 //! the first unknown value
263 int m_unknownValue;
264 //! extra data
265 std::string m_extra;
266 };
267
268 /** small structure of FreeHandParserInternal used to stored a shape
269 */
270 struct Shape {
271 //! the different type
272 enum Type { Line, Rectangle, Ellipse, Path, BackgroundPicture, Picture, Group, JoinGroup, Unknown };
273
274 //! constructor
275 Shape()
276 : m_id(0)
277 , m_type(Unknown)
278 , m_layerId(-1)
279 , m_lineId(0)
280 , m_fillId(0)
281 , m_transformation()
282 , m_box()
283 , m_corner()
284 , m_vertices()
285 , m_closed(false)
286 , m_evenOdd(false)
287 , m_joinDistance(0)
288 , m_childs()
289 , m_picture()
290 , m_dataId(0)
291 , m_isSent(false)
292 {
293 }
294
295 //! try to returns a shape and position
296 bool updateShape(MWAWGraphicShape &shape) const
297 {
298 if (m_type==Line || m_type==Rectangle || m_type==Ellipse) {
299 MWAWBox2f box=m_box;
300 if (m_type==Line)
301 shape=MWAWGraphicShape::line(box[0],box[1]);
302 else if (m_type==Rectangle)
303 shape=MWAWGraphicShape::rectangle(box, m_corner);
304 else
305 shape=MWAWGraphicShape::circle(box);
306 return true;
307 }
308 if (m_type!=Path || m_vertices.empty()) return false;
309 if (m_vertices.size()<6) {
310 // probably an aborted spline, transform in a point
311 MWAWVec2f pt=m_vertices[0];
312 shape=MWAWGraphicShape::line(pt,pt);
313 return true;
314 }
315 shape.m_type=MWAWGraphicShape::Polygon;
316 MWAWBox2f box;
317 bool needSpline=false;
318 for (size_t i=0; i+2<m_vertices.size(); i+=3) {
319 MWAWVec2f pt=m_vertices[i];
320 if (i==0)
321 box=MWAWBox2f(pt,pt);
322 else
323 box=box.getUnion(MWAWBox2f(pt,pt));
324 if (!needSpline && (m_vertices[i]!=m_vertices[i+1] || m_vertices[i]!=m_vertices[i+2]))
325 needSpline=true;
326 shape.m_vertices.push_back(pt);
327 }
328 shape.m_bdBox=box;
329 if (m_closed)
330 shape.m_vertices.push_back(shape.m_vertices.front());
331 if (!needSpline)
332 return true;
333
334 MWAWVec2f prevPoint, pt1;
335 bool hasPrevPoint = false;
336 shape.m_type=MWAWGraphicShape::Path;
337 shape.m_vertices.clear();
338 for (size_t i=0; i+2<m_vertices.size()+3; i+=3) {
339 bool end=i+2>=m_vertices.size();
340 if (end) {
341 if (!m_closed)
342 break;
343 if (end && !hasPrevPoint && m_vertices[0]==m_vertices[1]) {
344 shape.m_path.push_back(MWAWGraphicShape::PathData('Z'));
345 break;
346 }
347 i=0;
348 }
349 MWAWVec2f pt=m_vertices[i];
350 pt1 = m_vertices[i+1];
351 char type = hasPrevPoint ? 'C' : i==0 ? 'M' : (m_vertices[i]!=m_vertices[i+1]) ? 'S' : 'L';
352 shape.m_path.push_back(MWAWGraphicShape::PathData(type, pt, hasPrevPoint ? prevPoint : pt1, pt1));
353 hasPrevPoint = m_vertices[i]!=m_vertices[i+2];
354 if (hasPrevPoint)
355 prevPoint=m_vertices[i+2];
356 if (end)
357 break;
358 }
359 return true;
360 }
361 //! the zone id
362 int m_id;
363 //! the type
364 Type m_type;
365 //! the layer
366 int m_layerId;
367 //! the line id
368 int m_lineId;
369 //! the fill id
370 int m_fillId;
371
372 //! the transformation
373 MWAWTransformation m_transformation;
374 //! the main box (for line, rectangle, ellipse)
375 MWAWBox2f m_box;
376 //! the corner size
377 MWAWVec2f m_corner;
378 //! the list of point for path: 3 Vec2f defining each point
379 std::vector<MWAWVec2f> m_vertices;
380 //! a flag to know if a path is closed
381 bool m_closed;
382 //! a flag to know how path intersection are defined
383 bool m_evenOdd;
384 //! the join distance
385 float m_joinDistance;
386 //! the list of child (for group and join group )
387 std::vector<int> m_childs;
388 //! the picture entry
389 MWAWEntry m_picture;
390 //! the id of a the picture date
391 int m_dataId;
392 //! flag to known if a shape is sent
393 mutable bool m_isSent;
394 };
395
396 /** structure of FreeHandParserInternal used to stored a font */
397 struct Font {
398 //! constructor
399 Font()
400 : m_font()
401 , m_nameId(0)
402 , m_colorId(0)
403 {
404 }
405 //! the font
406 MWAWFont m_font;
407 //! the font name id
408 int m_nameId;
409 //! the font color id
410 int m_colorId;
411 };
412
413 /** structure of FreeHandParserInternal used to stored a textbox */
414 struct Textbox {
415 //! constructor
416 explicit Textbox(int id)
417 : m_id(id)
418 , m_layerId(-1)
419 , m_box()
420 , m_transformation()
421 , m_spacings(0,0)
422 , m_scalings(1,1)
423 , m_baseline(0)
424 , m_justify(MWAWParagraph::JustificationLeft)
425 , m_text()
426 , m_posToFontMap()
427 , m_isSent(false)
428 {
429 }
430 //! the textbox id
431 int m_id;
432 //! the layer id
433 int m_layerId;
434 //! the main box
435 MWAWBox2f m_box;
436 //! the transformation
437 MWAWTransformation m_transformation;
438 //! the letter/word spacing
439 MWAWVec2f m_spacings;
440 //! the horizontal/vertical scalings
441 MWAWVec2f m_scalings;
442 //! the baseline
443 float m_baseline;
444 //! the paragraph justification
445 MWAWParagraph::Justification m_justify;
446 //! the text data
447 MWAWEntry m_text;
448 //! map char pos to font
449 std::map<int,Font> m_posToFontMap;
450 //! flag to known if a shape is sent
451 mutable bool m_isSent;
452 };
453
454 ////////////////////////////////////////
455 //! Internal: the state of a FreeHandParser
456 struct State {
457 //! constructor
458 State()
459 : m_mainGroupId(0)
460 , m_transform()
461 , m_zIdToTypeMap()
462 , m_zIdToColorMap()
463 , m_zIdToDashMap()
464 , m_zIdToDataMap()
465 , m_zIdToFillStyleMap()
466 , m_zIdToLineStyleMap()
467 , m_zIdToStringMap()
468 , m_zIdToPostscriptMap()
469 , m_zIdToShapeMap()
470 , m_zIdToTextboxMap()
471 , m_actualLayer(-1)
472 , m_sendIdSet()
473 , m_sendLayerSet()
474 {
475 }
476 //! try to return a zone type
477 ZoneType getZoneType(int id) const
478 {
479 if (m_zIdToTypeMap.find(id)==m_zIdToTypeMap.end())
480 return Z_Unknown;
481 return m_zIdToTypeMap.find(id)->second;
482 }
483 //! try to add a id
484 bool addZoneId(int id, ZoneType zoneType)
485 {
486 if (m_zIdToTypeMap.find(id)!=m_zIdToTypeMap.end())
487 return m_zIdToTypeMap.find(id)->second==zoneType;
488 m_zIdToTypeMap[id]=zoneType;
489 return true;
490 }
491 //! try to update the fill style
492 bool updateFillStyle(int zId, MWAWGraphicStyle &style) const;
493 //! try to update the line style
494 bool updateLineStyle(int zId, MWAWGraphicStyle &style) const;
495 //! try to update the group layer id, return 0 or the new layer id
496 int updateGroupLayerId(int zId, std::set<int> &seen);
497 //! the main group id
498 int m_mainGroupId;
499 //! the main transformation
500 MWAWTransformation m_transform;
501 //! the list of id seen
502 std::map<int, ZoneType> m_zIdToTypeMap;
503 //! the list zoneId to color
504 std::map<int, MWAWColor> m_zIdToColorMap;
505 //! the list zoneId to dash
506 std::map<int, std::vector<float> > m_zIdToDashMap;
507 //! the list zoneId to data map
508 std::map<int, MWAWEntry> m_zIdToDataMap;
509 //! the list zoneId to fillStyle
510 std::map<int, FillStyle> m_zIdToFillStyleMap;
511 //! the list zoneId to lineStyle
512 std::map<int, LineStyle> m_zIdToLineStyleMap;
513 //! the list zoneId to string
514 std::map<int, std::string> m_zIdToStringMap;
515 //! the list zoneId to postscrip code
516 std::map<int, std::string> m_zIdToPostscriptMap;
517 //! the list zoneId to shape
518 std::map<int, Shape> m_zIdToShapeMap;
519 //! the list zoneId to textbox
520 std::map<int, Textbox> m_zIdToTextboxMap;
521 //! the actual layer
522 int m_actualLayer;
523 //! a set of send id used to avoid potential loop
524 std::set<int> m_sendIdSet;
525 //! a set of create layer to avoid dupplicating layer
526 std::set<int> m_sendLayerSet;
527 };
528
529 bool State::updateFillStyle(int zId, MWAWGraphicStyle &style) const
530 {
531 if (!zId) return true;
532 // can be a simple color
533 if (m_zIdToColorMap.find(zId)!=m_zIdToColorMap.end()) {
534 style.setSurfaceColor(m_zIdToColorMap.find(zId)->second);
535 return true;
536 }
537 if (m_zIdToFillStyleMap.find(zId)==m_zIdToFillStyleMap.end()) {
538 static bool first=true;
539 if (first) {
540 first=false;
541 MWAW_DEBUG_MSG(("FreeHandParserInternal::State::updateFillStyle: can not find style %d\n", zId));
542 }
543 return false;
544 }
545 FillStyle const &fill=m_zIdToFillStyleMap.find(zId)->second;
546 int numColors=fill.m_type==MWAWGraphicStyle::Gradient::G_None ? 1 : 2;
547 MWAWColor colors[2];
548 for (int i=0; i<numColors; ++i) {
549 if (fill.m_colorId[i]==0) {
550 colors[i]=MWAWColor::white();
551 continue;
552 }
553 if (m_zIdToColorMap.find(fill.m_colorId[i])==m_zIdToColorMap.end()) {
554 MWAW_DEBUG_MSG(("FreeHandParserInternal::State::updateFillStyle: can not find some color %d\n", fill.m_colorId[i]));
555 return false;
556 }
557 colors[i]=m_zIdToColorMap.find(fill.m_colorId[i])->second;
558 }
559 if (!fill.m_pattern.empty()) {
560 auto pat=fill.m_pattern;
561 pat.m_colors[0]=MWAWColor::white();
562 pat.m_colors[1]=colors[0];
563 style.setPattern(pat);
564 return true;
565 }
566 if (fill.m_type==MWAWGraphicStyle::Gradient::G_None) {
567 style.setSurfaceColor(colors[0]);
568 return true;
569 }
570 auto &finalGrad=style.m_gradient;
571 finalGrad.m_type = fill.m_type;
572 finalGrad.m_angle = 270.f-fill.m_angle;
573 finalGrad.m_stopList.resize(2);
574 for (size_t i=0; i<2; ++i)
575 finalGrad.m_stopList[i]=MWAWGraphicStyle::Gradient::Stop(float(i), colors[i]);
576 return true;
577 }
578
579 bool State::updateLineStyle(int zId, MWAWGraphicStyle &style) const
580 {
581 if (!zId) {
582 style.m_lineWidth=0;
583 return true;
584 }
585 if (m_zIdToLineStyleMap.find(zId)==m_zIdToLineStyleMap.end()) {
586 MWAW_DEBUG_MSG(("FreeHandParserInternal::State::updateLineStyle: can not find style %d\n", zId));
587 style.m_lineWidth=1;
588 return false;
589 }
590 LineStyle const &line=m_zIdToLineStyleMap.find(zId)->second;
591 style.m_lineWidth=line.m_width;
592 MWAWColor color=MWAWColor::white();
593 if (line.m_colorId) {
594 if (m_zIdToColorMap.find(line.m_colorId)==m_zIdToColorMap.end()) {
595 static bool first=true;
596 if (first) {
597 first=false;
598 MWAW_DEBUG_MSG(("FreeHandParserInternal::State::updateLineStyle: can not find some color %d\n", line.m_colorId));
599 }
600 }
601 else
602 color=m_zIdToColorMap.find(line.m_colorId)->second;
603 }
604 if (!line.m_pattern.empty()) {
605 auto pat=line.m_pattern;
606 // CHECKME
607 pat.m_colors[0]=MWAWColor::white();
608 pat.m_colors[1]=color;
609 pat.getAverageColor(style.m_lineColor);
610 }
611 else
612 style.m_lineColor=color;
613 if (line.m_dashId) {
614 if (m_zIdToDashMap.find(line.m_dashId)==m_zIdToDashMap.end()) {
615 MWAW_DEBUG_MSG(("FreeHandParserInternal::State::updateLineStyle: can not find dash %d\n", line.m_dashId));
616 }
617 else if (m_zIdToDashMap.find(line.m_dashId)->second.size()>1)
618 style.m_lineDashWidth=m_zIdToDashMap.find(line.m_dashId)->second;
619 }
620 style.m_lineCap=line.m_cap;
621 style.m_lineJoin=line.m_join;
622 return true;
623 }
624
625 int State::updateGroupLayerId(int zId, std::set<int> &seen)
626 {
627 if (m_zIdToTextboxMap.find(zId)!=m_zIdToTextboxMap.end())
628 return m_zIdToTextboxMap.find(zId)->second.m_layerId;
629 if (m_zIdToShapeMap.find(zId)==m_zIdToShapeMap.end())
630 return -1;
631 Shape &shape=m_zIdToShapeMap.find(zId)->second;
632 if (seen.find(zId)!=seen.end() || (shape.m_type!=Shape::Group && shape.m_type!=Shape::JoinGroup))
633 return shape.m_layerId;
634 int layerId=-1;
635 seen.insert(zId);
636 bool first=true;
637 for (auto const &child : shape.m_childs) {
638 int newLayerId=updateGroupLayerId(child, seen);
639 if (newLayerId==-1 || (!first && layerId!=newLayerId))
640 layerId=-1;
641 else
642 layerId=newLayerId;
643 first=false;
644 }
645 shape.m_layerId=layerId;
646 seen.erase(zId);
647 return layerId;
648 }
649
650 ////////////////////////////////////////
651 //! Internal: the subdocument of a FreeHandParser
652 class SubDocument final : public MWAWSubDocument
653 {
654 public:
655 SubDocument(FreeHandParser &pars, MWAWInputStreamPtr const &input, int zoneId)
656 : MWAWSubDocument(&pars, input, MWAWEntry())
657 , m_id(zoneId) {}
658
659 //! destructor
660 ~SubDocument() final {}
661
662 //! operator!=
663 bool operator!=(MWAWSubDocument const &doc) const final
664 {
665 if (MWAWSubDocument::operator!=(doc)) return true;
666 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
667 if (!sDoc) return true;
668 if (m_id != sDoc->m_id) return true;
669 return false;
670 }
671
672 //! the parser function
673 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
674
675 protected:
676 //! the subdocument id
677 int m_id;
678 private:
679 SubDocument(SubDocument const &orig);
680 SubDocument &operator=(SubDocument const &orig);
681 };
682
683 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
684 {
685 if (!listener || !listener->canWriteText()) {
686 MWAW_DEBUG_MSG(("FreeHandParserInternal::SubDocument::parse: no listener\n"));
687 return;
688 }
689 auto *parser=dynamic_cast<FreeHandParser *>(m_parser);
690 if (!parser) {
691 MWAW_DEBUG_MSG(("FreeHandParserInternal::SubDocument::parse: no parser\n"));
692 return;
693 }
694 long pos = m_input->tell();
695 parser->sendText(m_id);
696 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
697 }
698
699 }
700
701 ////////////////////////////////////////////////////////////
702 // constructor/destructor, ...
703 ////////////////////////////////////////////////////////////
704 FreeHandParser::FreeHandParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
705 : MWAWGraphicParser(input, rsrcParser, header)
706 , m_state()
707 {
708 init();
709 }
710
711 FreeHandParser::~FreeHandParser()
712 {
713 }
714
715 void FreeHandParser::init()
716 {
717 resetGraphicListener();
718 setAsciiName("main-1");
719
720 m_state.reset(new FreeHandParserInternal::State);
721
722 getPageSpan().setMargins(0.1);
723 }
724
725 ////////////////////////////////////////////////////////////
726 // the parser
727 ////////////////////////////////////////////////////////////
728 void FreeHandParser::parse(librevenge::RVNGDrawingInterface *docInterface)
729 {
730 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
731 bool ok = false;
732 try {
733 // create the asciiFile
734 ascii().setStream(getInput());
735 ascii().open(asciiName());
736 checkHeader(nullptr);
737 ok = createZones();
738 if (ok) {
739 createDocument(docInterface);
740 sendZone(m_state->m_mainGroupId, m_state->m_transform);
741 #ifdef DEBUG
742 flushExtra();
743 #endif
744 }
745 ascii().reset();
746 }
747 catch (...) {
748 MWAW_DEBUG_MSG(("FreeHandParser::parse: exception catched when parsing\n"));
749 ok = false;
750 }
751
752 resetGraphicListener();
753 if (!ok) throw(libmwaw::ParseException());
754 }
755
756 ////////////////////////////////////////////////////////////
757 // create the document
758 ////////////////////////////////////////////////////////////
759 void FreeHandParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
760 {
761 if (!documentInterface) return;
762 if (getGraphicListener()) {
763 MWAW_DEBUG_MSG(("FreeHandParser::createDocument: listener already exist\n"));
764 return;
765 }
766
767 // create the page list
768 MWAWPageSpan ps(getPageSpan());
769 ps.setPageSpan(1);
770 std::vector<MWAWPageSpan> pageList(1,ps);
771 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
772 setGraphicListener(listen);
773 listen->startDocument();
774 }
775
776 ////////////////////////////////////////////////////////////
777 //
778 // Intermediate level
779 //
780 ////////////////////////////////////////////////////////////
781 bool FreeHandParser::createZones()
782 {
783 MWAWInputStreamPtr input = getInput();
784 long pos;
785 libmwaw::DebugStream f;
786 bool readSome=false;
787 int zId=1;
788 int const vers=version();
789 if (vers==2) {
790 pos=input->tell();
791 libmwaw::PrinterInfo info;
792 if (!info.read(input)) {
793 input->seek(pos, librevenge::RVNG_SEEK_SET);
794 if (input->readULong(4)==0) { // null print info is ok
795 ascii().addPos(pos);
796 ascii().addNote("_");
797 input->seek(pos+0x78, librevenge::RVNG_SEEK_SET);
798 }
799 else
800 input->seek(pos, librevenge::RVNG_SEEK_SET);
801 }
802 else {
803 f.str("");
804 f << "Entries(PrintInfo):" << info;
805 ascii().addPos(pos);
806 ascii().addNote(f.str().c_str());
807 }
808 }
809 while (!input->isEnd()) {
810 pos=input->tell();
811 while ((vers==1 && readZoneV1(zId)) || (vers==2 && readZoneV2(zId))) {
812 readSome=true;
813 pos=input->tell();
814 if (zId) ++zId;
815 }
816 input->seek(pos, librevenge::RVNG_SEEK_SET);
817 if (!input->checkPosition(pos+5))
818 break;
819 // ok try to continue
820 bool ok=true;
821 zId=0;
822 while (!input->isEnd()) {
823 auto val=static_cast<unsigned long>(input->readULong(4));
824 if (input->isEnd()) {
825 input->seek(pos, librevenge::RVNG_SEEK_SET);
826 ok=false;
827 break;
828 }
829 if (!val || (val&0xFF00)) {
830 input->seek(-1, librevenge::RVNG_SEEK_CUR);
831 continue;
832 }
833 if (val&0xFF0000) {
834 input->seek(-2, librevenge::RVNG_SEEK_CUR);
835 continue;
836 }
837 if (val&0xFF000000) {
838 input->seek(-3, librevenge::RVNG_SEEK_CUR);
839 continue;
840 }
841 input->seek(-4, librevenge::RVNG_SEEK_CUR);
842 long actPos=input->tell();
843 if ((vers==1 && readZoneV1(zId)) || (vers==2 && readZoneV2(zId))) {
844 if (pos!=actPos) {
845 MWAW_DEBUG_MSG(("FreeHandParser::createZones: find some unexpected data\n"));
846 ascii().addPos(pos);
847 ascii().addNote("Entries(Unknown):###");
848 }
849 break;
850 }
851 input->seek(actPos+4, librevenge::RVNG_SEEK_SET);
852 }
853 if (!ok) break;
854 }
855 pos=input->tell();
856 f.str("");
857 f << "Entries(End):";
858 if (input->readLong(4)!=-1) {
859 MWAW_DEBUG_MSG(("FreeHandParser::createZones: find unexpected end data\n"));
860 f << "###";
861 }
862 if (readSome && m_state->m_mainGroupId) {
863 std::set<int> seen;
864 m_state->updateGroupLayerId(m_state->m_mainGroupId, seen);
865 }
866 ascii().addPos(pos);
867 ascii().addNote(f.str().c_str());
868 return readSome;
869 }
870
871 bool FreeHandParser::readZoneV1(int zId)
872 {
873 MWAWInputStreamPtr input = getInput();
874 long pos=input->tell();
875 if (!input->checkPosition(pos+5))
876 return false;
877 auto val=static_cast<int>(input->readULong(4));
878 if (val < 0 || (static_cast<unsigned long>(val)&0xFF000000)) {
879 input->seek(pos, librevenge::RVNG_SEEK_SET);
880 return false;
881 }
882 auto type=static_cast<int>(input->readULong(2));
883 input->seek(pos, librevenge::RVNG_SEEK_SET);
884 switch (type) {
885 // does type=1 exist ?
886 case 2:
887 return readStyleGroup(zId);
888 case 3:
889 return readStringZone(zId);
890
891 // 4001-4002
892 case 0xfa1:
893 return readRootGroup(zId);
894 case 0xfa2:
895 return readGroupV1(zId);
896
897 // 4101-4104
898 case 0x1005:
899 return readTransformGroup(zId);
900 case 0x1006:
901 return readTextboxV1(zId);
902 case 0x1007:
903 return readBackgroundPicture(zId);
904 case 0x1008:
905 return readJoinGroup(zId);
906
907 // 4202-4204
908 // does type=0x1069 exist ?
909 case 0x106a:
910 case 0x106b:
911 case 0x106c:
912 return readColor(zId);
913
914 // 4301-4305
915 case 0x10cd:
916 return readFillStyle(zId);
917 case 0x10ce:
918 return readLineStyle(zId);
919 case 0x10cf:
920 return readPostscriptStyle(zId);
921 case 0x10d0:
922 case 0x10d1:
923 return readFillStyle(zId);
924
925 // 4401-4405
926 case 0x1131: // rectangle
927 case 0x1132: // ellipse
928 // does type=0x1133 exist ?
929 case 0x1134: // spline
930 case 0x1135: // line
931 return readShape(zId);
932
933 // 4501
934 case 0x1195:
935 return readDash(zId);
936 default:
937 break;
938 }
939 input->seek(pos, librevenge::RVNG_SEEK_SET);
940 return false;
941 }
942
943 bool FreeHandParser::readZoneV2(int zId)
944 {
945 MWAWInputStreamPtr input = getInput();
946 long pos=input->tell();
947 if (!input->checkPosition(pos+5))
948 return false;
949 auto val=static_cast<int>(input->readULong(4));
950 if (val < 0 || (static_cast<unsigned long>(val)&0xFF000000)) {
951 input->seek(pos, librevenge::RVNG_SEEK_SET);
952 return false;
953 }
954 auto type=static_cast<int>(input->readULong(2));
955 input->seek(pos, librevenge::RVNG_SEEK_SET);
956 switch (type) {
957 case 5: // style group
958 return readStyleGroup(zId);
959 case 6:
960 return readStringZone(zId);
961 case 0x1389:
962 return readRootGroup(zId);
963 case 0x138a:
964 return readGroupV2(zId);
965 case 0x138b:
966 return readDataZone(zId);
967 case 0x13ed:
968 return readTransformGroup(zId);
969 case 0x13ee:
970 return readTextboxV2(zId);
971 case 0x13f0:
972 return readJoinGroup(zId);
973 case 0x13f8:
974 return readPictureZone(zId);
975 case 0x1452: // basic
976 case 0x1453: // tint
977 case 0x1454: // cmyk
978 case 0x1455: // pantome ?
979 return readColor(zId);
980 case 0x14b5: // basic
981 return readFillStyle(zId);
982 case 0x14b6: // line style
983 return readLineStyle(zId);
984 case 0x14b7: // gradient linear
985 case 0x14b8: // radial
986 return readFillStyle(zId);
987 case 0x14c9: // line, always follow 14d3
988 case 0x14ca: // surf, always follow 14d4
989 return readPostscriptStyle(zId);
990 case 0x14d3: // pattern
991 return readFillStyle(zId);
992 case 0x14d4: // pattern line style
993 return readLineStyle(zId);
994 case 0x14dd: // tile style
995 return readFillStyle(zId);
996 case 0x1519: // rectangle
997 case 0x151a: // ellipse
998 return readShape(zId);
999 case 0x151c:
1000 return readShape(zId);
1001 case 0x151d: // line
1002 return readShape(zId);
1003 case 0x157d:
1004 return readDash(zId);
1005 default:
1006 break;
1007 }
1008 input->seek(pos, librevenge::RVNG_SEEK_SET);
1009 return false;
1010 }
1011
1012 ////////////////////////////////////////////////////////////
1013 // read the header
1014 ////////////////////////////////////////////////////////////
1015 bool FreeHandParser::checkHeader(MWAWHeader *header, bool strict)
1016 {
1017 *m_state = FreeHandParserInternal::State();
1018 MWAWInputStreamPtr input = getInput();
1019 if (!input || !input->hasDataFork() || !input->checkPosition(128))
1020 return false;
1021
1022 libmwaw::DebugStream f;
1023 f << "FileHeader:";
1024 input->seek(0, librevenge::RVNG_SEEK_SET);
1025 auto signature=long(input->readULong(4));
1026 int vers=1, val;
1027 if (signature==0x61636633) {
1028 val=static_cast<int>(input->readULong(2)); // the subversion?
1029 if (strict && val>=9) return false;
1030 if (val!=5)
1031 f << "f0=" << val << ",";
1032 }
1033 else if (signature==0x46484432) {
1034 if (!input->checkPosition(256))
1035 return false;
1036 vers=2;
1037 val=static_cast<int>(input->readULong(2)); // the subversion?
1038 if (strict && val>20) return false;
1039 if (val!=9)
1040 f << "f0=" << val << ",";
1041 }
1042 else
1043 return false;
1044 val=static_cast<int>(input->readULong(2));
1045 if (val!=100) f << "f1=" << val << ",";
1046 float dim[8];
1047 for (auto &d : dim) d=float(input->readLong(2))/10.f;
1048 f << "page[sz]=" << MWAWVec2f(dim[0],dim[1]) << ",";
1049 f << "paper[sz]=" << MWAWVec2f(dim[2],dim[3]) << ",";
1050 if (dim[4]>0 || dim[5]>0)
1051 f << "unkn[sz]=" << MWAWVec2f(dim[4],dim[5]) << ",";
1052 f << "margins=" << MWAWVec2f(dim[6],dim[7]) << ",";
1053 if (vers>1) {
1054 ascii().addDelimiter(input->tell(),'|');
1055 input->seek(30, librevenge::RVNG_SEEK_CUR);
1056 for (int i=0; i<3; ++i) {
1057 val=static_cast<int>(input->readULong(2));
1058 if (!val) continue;
1059 // checkme: odd
1060 if (i==0 && (val&0x20) && dim[0]>dim[1]) {
1061 f << "landscape,";
1062 getPageSpan().setFormOrientation(MWAWPageSpan::LANDSCAPE);
1063 for (int j=0; j<4; ++j) {
1064 if (j==1) continue;
1065 std::swap(dim[2*j],dim[2*j+1]);
1066 }
1067 val &= 0xFFDF;
1068 }
1069 if (val)
1070 f << i+2 << "=" << std::hex << val << std::dec << ",";
1071 }
1072 }
1073 else {
1074 for (int i=0; i<2; ++i) { // f2=1|2|a
1075 val=static_cast<int>(input->readULong(2));
1076 if (!val) continue;
1077 if (i==1) {
1078 if (val&1) {
1079 f << "landscape,";
1080 getPageSpan().setFormOrientation(MWAWPageSpan::LANDSCAPE);
1081 for (int j=0; j<4; ++j)
1082 std::swap(dim[2*j],dim[2*j+1]);
1083 }
1084 if (val&2) f << "crop[mark],";
1085 if (val&4) f << "center[mark],";
1086 if (val&8) f << "separation[name],";
1087 if (val&0x10) f << "file[name&date],";
1088 if (val&0x40) f << "include[processColor],";
1089 if (val&0x80) f << "display[quality]=better,";
1090 if (val&0x100) f << "print[quality]=better,";
1091 val &= 0xFE20;
1092 }
1093 if (val)
1094 f << "f" << i+2 << "=" << std::hex << val << std::dec << ",";
1095 }
1096 }
1097 if (dim[2]>0 && dim[3]>0) {
1098 getPageSpan().setFormLength(double(dim[2])/72.);
1099 getPageSpan().setFormWidth(double(dim[3])/72.);
1100 if (dim[0]+dim[6]<=dim[2]) {
1101 getPageSpan().setMarginBottom(double(dim[6])/72.0);
1102 getPageSpan().setMarginTop(double(dim[2]-dim[0]-dim[6])/72.0);
1103 }
1104 else {
1105 MWAW_DEBUG_MSG(("FreeHandParser::checkHeader: the vertical margins seems bad\n"));
1106 if (dim[0]<=dim[2]) {
1107 getPageSpan().setMarginBottom(double(dim[2]-dim[0])/2.0/72.0);
1108 getPageSpan().setMarginTop(double(dim[2]-dim[0])/2.0/72.0);
1109 }
1110 }
1111 if (dim[1]+dim[7]<=dim[3]) {
1112 getPageSpan().setMarginRight(double(dim[7])/72.0);
1113 getPageSpan().setMarginLeft(double(dim[3]-dim[1]-dim[7])/72.0);
1114 }
1115 else {
1116 MWAW_DEBUG_MSG(("FreeHandParser::checkHeader: the horizontal margins seems bad\n"));
1117 if (dim[1]<=dim[3]) {
1118 getPageSpan().setMarginLeft(double(dim[3]-dim[1])/2.0/72.0);
1119 getPageSpan().setMarginRight(double(dim[3]-dim[1])/2.0/72.0);
1120 }
1121 }
1122 }
1123 else {
1124 if (strict)
1125 return false;
1126 MWAW_DEBUG_MSG(("FreeHandParser::checkHeader: the paper size seems bad\n"));
1127 }
1128 // transform orig from page content LeftBot -> origin form page LeftTop
1129 m_state->m_transform=
1130 MWAWTransformation::translation(72.f*MWAWVec2f(float(getPageSpan().getMarginLeft()),float(getPageSpan().getPageLength()+getPageSpan().getMarginTop()))) *
1131 MWAWTransformation::scale(MWAWVec2f(1,-1));
1132 if (vers==1) {
1133 val=static_cast<int>(input->readULong(4));
1134 switch ((val>>29)) {
1135 case 0: // point
1136 break;
1137 case 1:
1138 f << "unit=picas,";
1139 break;
1140 case 2:
1141 f << "unit=inches,";
1142 break;
1143 case 3:
1144 f << "unit=decimal[inches],";
1145 break;
1146 case 4:
1147 f << "unit=millimeters,";
1148 break;
1149 default:
1150 MWAW_DEBUG_MSG(("FreeHandParser::checkHeader: find unknown unit\n"));
1151 f << "##units=" << ((val>>29)&7) << ",";
1152 }
1153 val &= 0x1FFF;
1154 if (val) f << "grid[size]=" << float(val)/65536.f/10.f << ",";
1155 for (int i=0; i<4; ++i) { // f4=0|200
1156 val=static_cast<int>(input->readULong(2));
1157 if (val)
1158 f << "f" << i+4 << "=" << std::hex << val << std::dec << ",";
1159 }
1160 }
1161
1162 for (int i=0; i<2; ++i) {
1163 // checkme no sure what are limit
1164 long actPos=input->tell();
1165 auto sSz=static_cast<int>(input->readULong(1));
1166 if (sSz>31) {
1167 if (strict) return false;
1168 MWAW_DEBUG_MSG(("FreeHandParser::checkHeader: string size %d seems bad\n", i));
1169 f << "##sSz,";
1170 sSz=0;
1171 }
1172 std::string name;
1173 for (int s=0; s<sSz; ++s) name+=char(input->readULong(1));
1174 if (!name.empty()) {
1175 char const *wh[]= {"printer", "paper"};
1176 f << wh[i] << "=" << name << ",";
1177 }
1178 input->seek(actPos+32, librevenge::RVNG_SEEK_SET);
1179 }
1180 if (vers==1) {
1181 for (int i=0; i<5; ++i) { // g0=0|41, g1=0|3-7, g3=0|20, g4=0|b4
1182 val=static_cast<int>(input->readULong(2));
1183 if (val)
1184 f << "g" << i << "=" << std::hex << val << std::dec << ",";
1185 }
1186 // big number
1187 f << "unkn=" << std::hex << input->readULong(4) << std::dec << ",";
1188 for (int i=0; i<5; ++i) { // always 0
1189 val=static_cast<int>(input->readULong(2));
1190 if (val)
1191 f << "h" << i << "=" << val << ",";
1192 }
1193 }
1194 else {
1195 ascii().addDelimiter(input->tell(),'|');
1196 f << "unkn=" << std::hex << input->readULong(4) << std::dec << ",";
1197 // always 0
1198 for (int i=0; i<64; ++i) {
1199 val=static_cast<int>(input->readULong(2));
1200 if (val)
1201 f << "h" << i << "=" << val << ",";
1202 }
1203 // check for printer info or null printer info
1204 if (strict) {
1205 libmwaw::PrinterInfo info;
1206 if (!info.read(input)) {
1207 input->seek(256, librevenge::RVNG_SEEK_SET);
1208 if (input->readULong(4))
1209 return false;
1210 }
1211 }
1212 }
1213 ascii().addPos(0);
1214 ascii().addNote(f.str().c_str());
1215 setVersion(vers);
1216 if (header)
1217 header->reset(MWAWDocument::MWAW_T_FREEHAND, vers, MWAWDocument::MWAW_K_DRAW);
1218 input->seek(vers==1 ? 128 : 256, librevenge::RVNG_SEEK_SET);
1219
1220 return true;
1221 }
1222
1223 ////////////////////////////////////////////////////////////
1224 // try to read the zone
1225 ////////////////////////////////////////////////////////////
1226 bool FreeHandParser::readRootGroup(int zId)
1227 {
1228 if ((zId && zId!=1) || !m_state->m_zIdToTypeMap.empty())
1229 return false;
1230 MWAWInputStreamPtr input = getInput();
1231 libmwaw::DebugStream f;
1232
1233 long pos=input->tell();
1234 int const vers=version();
1235 if (!input->checkPosition(pos+(vers==1 ? 24 : 34)))
1236 return false;
1237 f.str("");
1238 f << "Entries(Root):";
1239 auto dSz=static_cast<int>(input->readULong(4)); // probably related to size
1240 auto opCode=static_cast<int>(input->readULong(2));
1241 if ((vers==1 && opCode !=0xfa1) || (vers>1 && opCode != 0x1389))
1242 return false;
1243 if (vers>1) dSz-=4;
1244 if (dSz!=0x34) {
1245 MWAW_DEBUG_MSG(("FreeHandParser::readRootGroup: find unexpected zone size\n"));
1246 f << "#sz?=" << dSz << ",";
1247 }
1248 if (vers==1) {
1249 for (int i=0; i<2; ++i) { // always 0 ?
1250 auto val=static_cast<int>(input->readLong(2));
1251 if (!val) continue;
1252 MWAW_DEBUG_MSG(("FreeHandParser::readRootGroup: find unknown zone %d\n", i));
1253 f << "#f" << i << "=" << val << ",";
1254 }
1255 }
1256 auto id=static_cast<int>(input->readLong(2));
1257 if (id) {
1258 m_state->m_mainGroupId=id;
1259 m_state->addZoneId(id, FreeHandParserInternal::Z_Group);
1260 f << "main=Z" << id << ",";
1261 }
1262 if (vers==1) {
1263 for (int i=0; i<6; ++i) {
1264 id=static_cast<int>(input->readLong(2));
1265 if (!id) continue;
1266 // the first group is a style group, but I never find any child, so...
1267 FreeHandParserInternal::ZoneType type[6]= {
1268 FreeHandParserInternal::Z_StyleGroup, FreeHandParserInternal::Z_FillGroup, FreeHandParserInternal::Z_LineStyleGroup,
1269 FreeHandParserInternal::Z_ColorGroup, FreeHandParserInternal::Z_DashGroup, FreeHandParserInternal::Z_ColorGroup
1270 };
1271 if (!m_state->addZoneId(id, type[i])) {
1272 MWAW_DEBUG_MSG(("FreeHandParser::readRootGroup: find dupplicated id\n"));
1273 f << "###";
1274 }
1275 char const *wh[6]= { "groupStyle0", "fillStyle", "lineStyle", "colStyle", "dashStyle", "colStyle2" };
1276 f << wh[i] << "=Z" << id << ",";
1277 }
1278 }
1279 else {
1280 // at least 8, maybe more
1281 for (int i=0; i<8; ++i) {
1282 id=static_cast<int>(input->readLong(2));
1283 if (!id) continue;
1284 FreeHandParserInternal::ZoneType type[8]= {
1285 FreeHandParserInternal::Z_ColorGroup, FreeHandParserInternal::Z_FillGroup, FreeHandParserInternal::Z_LineStyleGroup,
1286 FreeHandParserInternal::Z_StyleGroup, FreeHandParserInternal::Z_FillGroup, FreeHandParserInternal::Z_LineStyleGroup,
1287 FreeHandParserInternal::Z_DashGroup, FreeHandParserInternal::Z_ColorGroup,
1288 };
1289 if (!m_state->addZoneId(id, type[i])) {
1290 MWAW_DEBUG_MSG(("FreeHandParser::readRootGroup: find dupplicated id\n"));
1291 f << "###";
1292 }
1293 char const *wh[8]= { "colStyle", "fillStyle", "lineStyle", "groupStyle3", "fillStyle[unamed]", "lineStyle[unamed]", "dashStyle", "colStyle2"};
1294 f << wh[i] << "=Z" << id << ",";
1295 }
1296 for (int i=0; i<5; ++i) {
1297 auto val=static_cast<int>(input->readULong(2));
1298 if (!val) continue;
1299 MWAW_DEBUG_MSG(("FreeHandParser::readRootGroup: find unknown group id\n"));
1300 f << "###Z" << val << ",";
1301 }
1302 }
1303 ascii().addPos(pos);
1304 ascii().addNote(f.str().c_str());
1305
1306 return true;
1307 }
1308
1309 bool FreeHandParser::readGroupV1(int zId)
1310 {
1311 MWAWInputStreamPtr input = getInput();
1312 libmwaw::DebugStream f;
1313
1314 long pos=input->tell();
1315 if (!input->checkPosition(pos+20))
1316 return false;
1317 f.str("");
1318 if (zId)
1319 f << "Entries(Group)[Z" << zId << "]:";
1320 else
1321 f << "Entries(Group):";
1322 if (zId) {
1323 auto type=m_state->getZoneType(zId);
1324 if (type!=FreeHandParserInternal::Z_Group && type!=FreeHandParserInternal::Z_Shape) {
1325 MWAW_DEBUG_MSG(("FreeHandParser::readGroupV1: find unexpected zone type for zone %d\n", zId));
1326 }
1327 }
1328
1329 auto dSz=static_cast<int>(input->readULong(4)); // probably related to size
1330 f << "sz=" << dSz << ",";
1331 if (input->readULong(2)!=0xfa2) {
1332 input->seek(pos, librevenge::RVNG_SEEK_SET);
1333 return false;
1334 }
1335 FreeHandParserInternal::Shape res;
1336 res.m_id=zId;
1337 res.m_type=FreeHandParserInternal::Shape::Group;
1338 ascii().addDelimiter(input->tell(),'|');
1339 input->seek(pos+18, librevenge::RVNG_SEEK_SET);
1340 ascii().addDelimiter(input->tell(),'|');
1341 auto N=static_cast<int>(input->readULong(2));
1342 if (!input->checkPosition(pos+20+2*N)) {
1343 input->seek(pos, librevenge::RVNG_SEEK_SET);
1344 return false;
1345 }
1346 f << "childs=[";
1347 for (int i=0; i<N; ++i) {
1348 auto id=static_cast<int>(input->readULong(2));
1349 if (!m_state->addZoneId(id, FreeHandParserInternal::Z_Shape)) {
1350 input->seek(pos, librevenge::RVNG_SEEK_SET);
1351 return false;
1352 }
1353 res.m_childs.push_back(id);
1354 f << "Z" << id << ",";
1355 }
1356 f <<"],";
1357 ascii().addPos(pos);
1358 ascii().addNote(f.str().c_str());
1359 if (zId && m_state->m_zIdToShapeMap.find(zId)==m_state->m_zIdToShapeMap.end())
1360 m_state->m_zIdToShapeMap.insert(std::map<int,FreeHandParserInternal::Shape>::value_type(zId,res));
1361 return true;
1362 }
1363
1364 bool FreeHandParser::readGroupV2(int zId)
1365 {
1366 MWAWInputStreamPtr input = getInput();
1367 libmwaw::DebugStream f;
1368
1369 long pos=input->tell();
1370 if (!input->checkPosition(pos+20))
1371 return false;
1372 f.str("");
1373 if (zId)
1374 f << "Entries(Group)[Z" << zId << "]:";
1375 else
1376 f << "Entries(Group):";
1377 if (zId) {
1378 auto type=m_state->getZoneType(zId);
1379 if (type!=FreeHandParserInternal::Z_Group && type!=FreeHandParserInternal::Z_Shape) {
1380 MWAW_DEBUG_MSG(("FreeHandParser::readGroupV2: find unexpected zone type for zone %d\n", zId));
1381 }
1382 }
1383
1384 auto dSz=static_cast<int>(input->readULong(4)); // probably related to size
1385 f << "sz=" << dSz << ",";
1386 if (input->readULong(2)!=0x138a) {
1387 input->seek(pos, librevenge::RVNG_SEEK_SET);
1388 return false;
1389 }
1390 FreeHandParserInternal::Shape res;
1391 res.m_id=zId;
1392 res.m_type=FreeHandParserInternal::Shape::Group;
1393 for (int i=0; i<2; ++i) { // always 0
1394 auto val=static_cast<int>(input->readLong(2));
1395 if (val) f << "f" << i << "=" << val << ",";
1396 }
1397 auto sSz=static_cast<int>(input->readULong(2));
1398 if (!input->checkPosition(input->tell()+sSz+8)) {
1399 input->seek(pos, librevenge::RVNG_SEEK_SET);
1400 return false;
1401 }
1402 ascii().addDelimiter(input->tell(),'|');
1403 /* find
1404 00000000000000000000000000000000
1405 000f000a000000000000000000000000a89d800180017fff7fff7fff7fff7fff7fff7fff7fff7fff7fff4ead7fff80017fff52807fff7fff7fff60144ead8fff670a
1406 */
1407 input->seek(sSz, librevenge::RVNG_SEEK_CUR);
1408 ascii().addDelimiter(input->tell(),'|');
1409 float dim[2];
1410 for (auto &d : dim) d=float(input->readLong(2))/10.f;
1411 if (MWAWVec2f(dim[0],dim[1])!=MWAWVec2f(0,0))
1412 f << "dim?=" << MWAWVec2f(dim[0],dim[1]) << ",";
1413 auto val=static_cast<int>(input->readLong(2));
1414 if (val) f << "f2=" << val << ",";
1415
1416 auto N=static_cast<int>(input->readULong(2));
1417 if (!input->checkPosition(input->tell()+2*N)) {
1418 input->seek(pos, librevenge::RVNG_SEEK_SET);
1419 return false;
1420 }
1421 f << "childs=[";
1422 for (int i=0; i<N; ++i) {
1423 auto id=static_cast<int>(input->readULong(2));
1424 if (!m_state->addZoneId(id, FreeHandParserInternal::Z_Shape)) {
1425 input->seek(pos, librevenge::RVNG_SEEK_SET);
1426 return false;
1427 }
1428 res.m_childs.push_back(id);
1429 f << "Z" << id << ",";
1430 }
1431 f <<"],";
1432 ascii().addPos(pos);
1433 ascii().addNote(f.str().c_str());
1434 if (zId && m_state->m_zIdToShapeMap.find(zId)==m_state->m_zIdToShapeMap.end())
1435 m_state->m_zIdToShapeMap.insert(std::map<int,FreeHandParserInternal::Shape>::value_type(zId,res));
1436 return true;
1437 }
1438
1439 bool FreeHandParser::readJoinGroup(int zId)
1440 {
1441 MWAWInputStreamPtr input = getInput();
1442 libmwaw::DebugStream f;
1443
1444 long pos=input->tell();
1445 FreeHandParserInternal::ShapeHeader shape;
1446 int const vers=version();
1447 if (!readShapeHeader(shape)) {
1448 input->seek(pos, librevenge::RVNG_SEEK_SET);
1449 return false;
1450 }
1451 if ((vers==1 && shape.m_type!=0x1008) || (vers>1 && shape.m_type!=0x13f0)
1452 || !input->checkPosition(input->tell()+8)) {
1453 input->seek(pos, librevenge::RVNG_SEEK_SET);
1454 return false;
1455 }
1456 f.str("");
1457 if (zId)
1458 f << "Entries(JoinGrp)[Z" << zId << "]:" << shape;
1459 else
1460 f << "Entries(JoinGrp):" << shape;
1461 if (zId) {
1462 auto type=m_state->getZoneType(zId);
1463 if (type!=FreeHandParserInternal::Z_Shape) {
1464 MWAW_DEBUG_MSG(("FreeHandParser::readJoinGroup: find unexpected zone type for zone %d\n", zId));
1465 }
1466 }
1467 FreeHandParserInternal::Shape res;
1468 res.m_id=zId;
1469 res.m_layerId=shape.m_layerId;
1470 res.m_type=FreeHandParserInternal::Shape::JoinGroup;
1471 if (shape.m_size!=0x24) f << "sz?=" << shape.m_size << ",";
1472 res.m_joinDistance=float(input->readLong(4))/65536.f;
1473 if (res.m_joinDistance<0 || res.m_joinDistance>0)
1474 f << "dist=" << res.m_joinDistance << ",";
1475 f << "childs=[";
1476 for (int i=0; i<2; ++i) {
1477 auto id=static_cast<int>(input->readULong(2));
1478 if (!m_state->addZoneId(id, FreeHandParserInternal::Z_Shape)) {
1479 MWAW_DEBUG_MSG(("FreeHandParser::readJoinGroup: find unexpected child id\n"));
1480 f << "###";
1481 }
1482 res.m_childs.push_back(id);
1483 f << "Z" << id << ",";
1484 }
1485 f <<"],";
1486 ascii().addPos(pos);
1487 ascii().addNote(f.str().c_str());
1488 if (zId && m_state->m_zIdToShapeMap.find(zId)==m_state->m_zIdToShapeMap.end())
1489 m_state->m_zIdToShapeMap.insert(std::map<int,FreeHandParserInternal::Shape>::value_type(zId,res));
1490 static bool first=true;
1491 if (first) {
1492 MWAW_DEBUG_MSG(("FreeHandParser::readJoinGroup: Ooops, sending text on path is unimplemented\n"));
1493 first=false;
1494 }
1495 return true;
1496 }
1497
1498 bool FreeHandParser::readTransformGroup(int zId)
1499 {
1500 MWAWInputStreamPtr input = getInput();
1501 libmwaw::DebugStream f;
1502
1503 long pos=input->tell();
1504 FreeHandParserInternal::ShapeHeader shape;
1505 if (!readShapeHeader(shape)) {
1506 input->seek(pos, librevenge::RVNG_SEEK_SET);
1507 return false;
1508 }
1509 int const vers=version();
1510 if ((vers==1 && shape.m_type!=0x1005) || (vers>1 && shape.m_type!=0x13ed) || !input->checkPosition(input->tell()+30)) {
1511 input->seek(pos, librevenge::RVNG_SEEK_SET);
1512 return false;
1513 }
1514 f.str("");
1515 if (zId)
1516 f << "Entries(TransformGrp)[Z" << zId << "]:" << shape;
1517 else
1518 f << "Entries(TransformGrp):" << shape;
1519 if (shape.m_size!=0x38) f << "sz?=" << shape.m_size << ",";
1520 if (zId) {
1521 auto zType=m_state->getZoneType(zId);
1522 if (zType!=FreeHandParserInternal::Z_Group && zType!=FreeHandParserInternal::Z_Shape) {
1523 MWAW_DEBUG_MSG(("FreeHandParser::readTransformGroup: find unexpected zone type for zone %d\n", zId));
1524 }
1525 }
1526 FreeHandParserInternal::Shape res;
1527 res.m_id=zId;
1528 res.m_layerId=shape.m_layerId;
1529 res.m_type=FreeHandParserInternal::Shape::Group;
1530 auto id=static_cast<int>(input->readULong(2));
1531 if (!m_state->addZoneId(id, FreeHandParserInternal::Z_Group)) {
1532 MWAW_DEBUG_MSG(("FreeHandParser::readTransformGroup: find unexpected child id\n"));
1533 f << "###";
1534 }
1535 f << "child=Z" << id << ",";
1536 res.m_childs.push_back(id);
1537 auto val=static_cast<int>(input->readULong(2)); // always 0
1538 if (val) f << "f0=" << val << ",";
1539 f << "flags=" << std::hex << input->readULong(2) << std::dec << ",";
1540 f << "rot=[";
1541 float dim[6];
1542 for (int i=0; i<4; ++i) {
1543 dim[i]=float(input->readLong(4))/65536.f;
1544 f << dim[i] << ",";
1545 }
1546 f << "],";
1547 f << "trans=[";
1548 for (int i=0; i<2; ++i) {
1549 dim[i+4]=float(input->readLong(4))/65536.f/10.f;
1550 f << dim[i+4] << ",";
1551 }
1552 f << "],";
1553 res.m_transformation=MWAWTransformation(MWAWVec3f(dim[0],dim[2],dim[4]),MWAWVec3f(dim[1],dim[3],dim[5]));
1554
1555 ascii().addPos(pos);
1556 ascii().addNote(f.str().c_str());
1557 if (zId && m_state->m_zIdToShapeMap.find(zId)==m_state->m_zIdToShapeMap.end())
1558 m_state->m_zIdToShapeMap.insert(std::map<int,FreeHandParserInternal::Shape>::value_type(zId,res));
1559 return true;
1560 }
1561
1562 bool FreeHandParser::readStyleGroup(int zId)
1563 {
1564 int const vers=version();
1565 MWAWInputStreamPtr input = getInput();
1566 libmwaw::DebugStream f;
1567
1568 long pos=input->tell();
1569 if (!input->checkPosition(pos+(vers>1 ? 12 : 16)))
1570 return false;
1571 f.str("");
1572 if (zId)
1573 f << "Entries(StyleGrp)[Z" << zId << "]:";
1574 else
1575 f << "Entries(StyleGrp):";
1576 auto cType=FreeHandParserInternal::Z_Unknown;
1577 bool checkDSize=true;
1578 if (zId) {
1579 auto zType=m_state->getZoneType(zId);
1580 checkDSize=false;
1581 if (zType==FreeHandParserInternal::Z_ColorGroup)
1582 cType=FreeHandParserInternal::Z_Color;
1583 else if (zType==FreeHandParserInternal::Z_DashGroup)
1584 cType=FreeHandParserInternal::Z_Dash;
1585 else if (zType==FreeHandParserInternal::Z_FillGroup)
1586 cType=FreeHandParserInternal::Z_Fill;
1587 else if (zType==FreeHandParserInternal::Z_LineStyleGroup)
1588 cType=FreeHandParserInternal::Z_LineStyle;
1589 else if (zType!=FreeHandParserInternal::Z_StyleGroup) {
1590 checkDSize=true;
1591 MWAW_DEBUG_MSG(("FreeHandParser::readStyleGroup: find unexpected zone type for zone %d\n", zId));
1592 }
1593 }
1594 auto dSz=static_cast<int>(input->readULong(4)); // probably related to size
1595 f << "sz?=" << dSz << ",";
1596 auto opCode=static_cast<int>(input->readULong(2));
1597 if ((vers==1 && opCode!=2) || (vers>1 && opCode!=5))
1598 return false;
1599 if (vers==1) {
1600 for (int i=0; i<2; ++i) { // always f0=0,f1=16 ?
1601 auto val=static_cast<int>(input->readLong(2));
1602 if (val)
1603 f << "f" << i << "=" << val << ",";
1604 }
1605 }
1606 auto N=static_cast<int>(input->readULong(2));
1607 if (!input->checkPosition(input->tell()+4+2*N) ||
1608 (vers==1 && checkDSize && N!=(dSz-16)/2) ||
1609 (vers>1 && checkDSize && N!=(dSz-12)/2))
1610 return false;
1611 for (int i=0; i<2; ++i) { // always 0?
1612 auto val=static_cast<int>(input->readLong(2));
1613 if (val)
1614 f << "f" << i+2 << "=" << val << ",";
1615 }
1616 f << "childs=[";
1617 for (int i=0; i<N; ++i) {
1618 auto id=static_cast<int>(input->readULong(2));
1619 if (!m_state->addZoneId(id, cType)) {
1620 if (checkDSize) return false;
1621 MWAW_DEBUG_MSG(("FreeHandParser::readStyleGroup: find unexpected child zone %d\n", id));
1622 f << "###";
1623 }
1624 f << "Z" << id << ",";
1625 }
1626 f << "],";
1627 ascii().addPos(pos);
1628 ascii().addNote(f.str().c_str());
1629
1630 return true;
1631 }
1632
1633 bool FreeHandParser::readStringZone(int zId)
1634 {
1635 MWAWInputStreamPtr input = getInput();
1636 libmwaw::DebugStream f;
1637
1638 long pos=input->tell();
1639 auto dSz=long(input->readULong(4));
1640 auto opCode=static_cast<int>(input->readLong(2));
1641 int const vers=version();
1642 // v1: opcode=3, v2: opcode=6
1643 if (vers==2) {
1644 dSz-=4;
1645 opCode-=3;
1646 }
1647 if (opCode!=3 || dSz<3 || !input->checkPosition(pos+dSz+2)) {
1648 input->seek(pos, librevenge::RVNG_SEEK_SET);
1649 return false;
1650 }
1651 f.str("");
1652 if (zId)
1653 f << "Entries(String)[Z" << zId << "]:";
1654 else
1655 f << "Entries(String):";
1656 if (zId && m_state->getZoneType(zId)!=FreeHandParserInternal::Z_String) {
1657 MWAW_DEBUG_MSG(("FreeHandParser::readStringZone: find unexpected zone type for zone %d\n", zId));
1658 }
1659 auto sSz=int(input->readULong(1));
1660 if (sSz+5>dSz || (!zId && sSz+6<dSz)) {
1661 input->seek(pos, librevenge::RVNG_SEEK_SET);
1662 return false;
1663 }
1664 std::string name;
1665 for (int i=0; i<sSz; ++i) name += char(input->readULong(1));
1666 f << name << ",";
1667 if (zId && m_state->m_zIdToStringMap.find(zId)==m_state->m_zIdToStringMap.end())
1668 m_state->m_zIdToStringMap[zId]=name;
1669 ascii().addPos(pos);
1670 ascii().addNote(f.str().c_str());
1671 input->seek(pos+dSz+2, librevenge::RVNG_SEEK_SET);
1672 return true;
1673 }
1674
1675 bool FreeHandParser::readShapeHeader(FreeHandParserInternal::ShapeHeader &shape)
1676 {
1677 MWAWInputStreamPtr input = getInput();
1678 libmwaw::DebugStream f;
1679 long pos=input->tell();
1680 int const vers=version();
1681 if (!input->checkPosition(pos+(vers==1 ? 20 : 18))) return false;
1682 shape.m_size=long(input->readULong(4));
1683 shape.m_type=static_cast<int>(input->readULong(2));
1684 if (vers>1) {
1685 shape.m_dataId=static_cast<int>(input->readULong(2));
1686 if (shape.m_dataId && !m_state->addZoneId(shape.m_dataId, FreeHandParserInternal::Z_Note)) {
1687 MWAW_DEBUG_MSG(("FreeHandParser::readShapeHeader: find unexpected data id\n"));
1688 f << "###dataId";
1689 }
1690 shape.m_values[0]=static_cast<int>(input->readLong(2)); // always 0
1691 shape.m_layerId=static_cast<int>(input->readULong(2));
1692 shape.m_values[1]=static_cast<int>(input->readLong(2)); // always 0
1693 // now to multiple of 256 ???
1694 f << "unkn=[" << float(input->readLong(2))/256.f << "," << float(input->readLong(2))/256.f << "],";
1695 shape.m_extra=f.str();
1696 return true;
1697 }
1698 // always 0, if not we may have a problem...
1699 shape.m_values[0]=static_cast<int>(input->readLong(2));
1700 auto dataSz=static_cast<int>(input->readULong(2));
1701 if (!input->checkPosition(pos+14+dataSz)) {
1702 input->seek(pos, librevenge::RVNG_SEEK_SET);
1703 return false;
1704 }
1705 if (dataSz) {
1706 auto sSz=static_cast<int>(input->readULong(1));
1707 if (sSz==dataSz-1) {
1708 for (int i=0; i<sSz; ++i)
1709 shape.m_note+=char(input->readULong(1));
1710 }
1711 else {
1712 MWAW_DEBUG_MSG(("FreeHandParser::readShapeHeader: find unexpected special size\n"));
1713 f << "##specialSize=" << dataSz << ",";
1714 input->seek(dataSz-1, librevenge::RVNG_SEEK_CUR);
1715 }
1716 }
1717 shape.m_layerId=static_cast<int>(input->readULong(2));
1718 /* val1,val2: always 0, if not we may have a problem...
1719 val3: sometimes a 1005 zone
1720 */
1721 for (int i=0; i<3; ++i)
1722 shape.m_values[i+1]=static_cast<int>(input->readLong(2));
1723 if (shape.m_values[3]) {
1724 if (!m_state->addZoneId(shape.m_values[3], FreeHandParserInternal::Z_Shape)) {
1725 MWAW_DEBUG_MSG(("FreeHandParser::readShapeHeader: find unexpected shape id\n"));
1726 f << "###shapeId";
1727 }
1728 }
1729 // the special zone
1730 dataSz=static_cast<int>(input->readULong(2));
1731 if (!input->checkPosition(input->tell()+dataSz)) {
1732 input->seek(pos, librevenge::RVNG_SEEK_SET);
1733 return false;
1734 }
1735 if (dataSz==8) {
1736 long actPos=input->tell();
1737 if (!readScreenMode(*shape.m_screen)) {
1738 MWAW_DEBUG_MSG(("FreeHandParser::readShapeHeader: can not read screen mode\n"));
1739 f << "##screenMode,";
1740 input->seek(actPos+8, librevenge::RVNG_SEEK_SET);
1741 }
1742 }
1743 else if (dataSz) {
1744 MWAW_DEBUG_MSG(("FreeHandParser::readShapeHeader: find unexpected special size\n"));
1745 f << "##specialSize=" << dataSz << ",";
1746 input->seek(dataSz, librevenge::RVNG_SEEK_CUR);
1747 }
1748 shape.m_extra=f.str();
1749 return true;
1750 }
1751
1752 bool FreeHandParser::readScreenMode(FreeHandParserInternal::ScreenMode &screen)
1753 {
1754 MWAWInputStreamPtr input = getInput();
1755 long pos=input->tell();
1756 if (!input->checkPosition(pos+8)) return false;
1757 screen.m_function=static_cast<int>(input->readLong(2));
1758 screen.m_angle=float(input->readLong(2))/10.f;
1759 screen.m_lineByInch=static_cast<int>(input->readULong(2));
1760 screen.m_value=static_cast<int>(input->readLong(2)); // always 0?
1761 return true;
1762 }
1763
1764 bool FreeHandParser::readStyleHeader(FreeHandParserInternal::StyleHeader &style)
1765 {
1766 MWAWInputStreamPtr input = getInput();
1767 libmwaw::DebugStream f;
1768 long pos=input->tell();
1769 if (!input->checkPosition(pos+12)) return false;
1770 style.m_size=long(input->readULong(4));
1771 style.m_type=static_cast<int>(input->readULong(2));
1772 if (version()==1) {
1773 // always 0, if not we may have a problem...
1774 style.m_unknownValue=static_cast<int>(input->readLong(2));
1775 auto dataSz=static_cast<int>(input->readULong(2));
1776 if (!input->checkPosition(pos+12+dataSz)) {
1777 input->seek(pos, librevenge::RVNG_SEEK_SET);
1778 return false;
1779 }
1780 if (dataSz==8) {
1781 long actPos=input->tell();
1782 if (!readScreenMode(*style.m_screen)) {
1783 MWAW_DEBUG_MSG(("FreeHandParser::readStyleHeader: can not read screen mode\n"));
1784 f << "##screenMode,";
1785 input->seek(actPos+8, librevenge::RVNG_SEEK_SET);
1786 }
1787 }
1788 else if (dataSz) {
1789 MWAW_DEBUG_MSG(("FreeHandParser::readStyleHeader: find unexpected special size\n"));
1790 f << "##specialSize=" << dataSz << ",";
1791 input->seek(dataSz, librevenge::RVNG_SEEK_CUR);
1792 }
1793 }
1794 auto id=static_cast<int>(input->readULong(2));
1795 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_String)) {
1796 input->seek(pos, librevenge::RVNG_SEEK_SET);
1797 return false;
1798 }
1799 else
1800 style.m_labelId=id;
1801 return true;
1802 }
1803
1804 bool FreeHandParser::readColor(int zId)
1805 {
1806 MWAWInputStreamPtr input = getInput();
1807 libmwaw::DebugStream f;
1808
1809 long pos=input->tell();
1810 f.str("");
1811 if (zId)
1812 f << "Entries(Color)[Z" << zId << "]:";
1813 else
1814 f << "Entries(Color):";
1815 if (zId && m_state->getZoneType(zId)!=FreeHandParserInternal::Z_Color) {
1816 MWAW_DEBUG_MSG(("FreeHandParser::readColor: find unexpected zone type for zone %d\n", zId));
1817 }
1818 FreeHandParserInternal::StyleHeader zone;
1819 if (!readStyleHeader(zone)) {
1820 input->seek(pos, librevenge::RVNG_SEEK_SET);
1821 return false;
1822 }
1823 int expectedSize=0;
1824 int const vers=version();
1825 switch (zone.m_type) {
1826 case 0x106a:
1827 case 0x1452:
1828 f << "color,";
1829 expectedSize=12;
1830 if ((vers==1 && zone.m_size!=0x1c) || (vers>1 && zone.m_size!=0x1e))
1831 f << "#sz?=" << zone.m_size << ",";
1832 break;
1833 case 0x106b:
1834 f << "tint,";
1835 expectedSize=4;
1836 if (zone.m_size!=0x16)
1837 f << "#sz?=" << zone.m_size << ",";
1838 break;
1839 case 0x1453:
1840 f << "tint,";
1841 expectedSize=10;
1842 if (zone.m_size!=0x1e)
1843 f << "#sz?=" << zone.m_size << ",";
1844 break;
1845 case 0x106c:
1846 f << "cmyk,";
1847 expectedSize=8;
1848 if (zone.m_size!=0x18)
1849 f << "#sz?=" << zone.m_size << ",";
1850 break;
1851 case 0x1454:
1852 f << "cmyk,";
1853 expectedSize=14;
1854 if (zone.m_size!=0x20)
1855 f << "#sz?=" << zone.m_size << ",";
1856 break;
1857 case 0x1455:
1858 f << "pantome?,";
1859 expectedSize=22;
1860 if (zone.m_size!=0x28)
1861 f << "#sz?=" << zone.m_size << ",";
1862 break;
1863 default:
1864 break;
1865 }
1866 long endPos=input->tell()+expectedSize;
1867 if (expectedSize==0 || !input->checkPosition(endPos)) {
1868 input->seek(pos, librevenge::RVNG_SEEK_SET);
1869 return false;
1870 }
1871 f << zone;
1872 MWAWColor color;
1873 if (zone.m_type==0x106a || zone.m_type==0x1452) {
1874 unsigned char col[3];
1875 for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8);
1876 color=MWAWColor(col[0],col[1],col[2]);
1877 f << color << ",";
1878 auto val=static_cast<int>(input->readLong(2));
1879 if (val) f << "id=" << val << ",";
1880 val=static_cast<int>(input->readLong(2)); // flag or big number
1881 if (val) f << "f0=" << val << ",";
1882 val=static_cast<int>(input->readLong(2)); // always 1
1883 if (val!=1) f << "f1=" << val << ",";
1884 }
1885 else if (zone.m_type==0x106b || zone.m_type==0x1453) {
1886 if (zone.m_type==0x1453) {
1887 unsigned char col[3];
1888 for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8);
1889 color=MWAWColor(col[0],col[1],col[2]);
1890 f << color << ",";
1891 }
1892 auto cId=static_cast<int>(input->readULong(2));
1893 if (cId && !m_state->addZoneId(cId, FreeHandParserInternal::Z_Color)) {
1894 input->seek(pos, librevenge::RVNG_SEEK_SET);
1895 return false;
1896 }
1897 f << "main[color]=Z" << cId << ",";
1898 MWAWColor mainColor(MWAWColor::white());
1899 if (cId && m_state->m_zIdToColorMap.find(cId)!=m_state->m_zIdToColorMap.end())
1900 mainColor=m_state->m_zIdToColorMap.find(cId)->second;
1901 else if (cId) {
1902 static bool first=true;
1903 if (first) {
1904 MWAW_DEBUG_MSG(("FreeHandParser::readColor: can not find some main color\n"));
1905 first=false;
1906 }
1907 }
1908 float tint=float(input->readULong(2))/65535.f;
1909 if (zone.m_type==0x106b)
1910 color=MWAWColor::barycenter(tint, mainColor, 1-tint, MWAWColor::white());
1911 f << "percent=" << tint << ",";
1912 }
1913 else if (zone.m_type==0x1455) {
1914 unsigned char col[3];
1915 for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8);
1916 color=MWAWColor(col[0],col[1],col[2]);
1917 f << color << ",";
1918 // what is that ?
1919 for (int i=0; i<8; ++i) { // f0=0|1a5|1f7,f2=0|451e,f4=0|f5c|1c28,f5=147a|2e14|828f,f6=1c2, f7=1
1920 auto val=static_cast<int>(input->readULong(2));
1921 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
1922 }
1923 }
1924 else {
1925 if (zone.m_type==0x1454) {
1926 unsigned char col[3];
1927 for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8);
1928 color=MWAWColor(col[0],col[1],col[2]);
1929 f << color << ",";
1930 }
1931 unsigned char col[4];
1932 for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8);
1933 if (zone.m_type==0x106c) {
1934 color=MWAWColor::colorFromCMYK(col[1],col[2],col[3],col[0]);
1935 f << color << ",";
1936 }
1937 }
1938 if (zId && m_state->m_zIdToColorMap.find(zId)==m_state->m_zIdToColorMap.end())
1939 m_state->m_zIdToColorMap[zId]=color;
1940 ascii().addPos(pos);
1941 ascii().addNote(f.str().c_str());
1942 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1943 return true;
1944 }
1945
1946 bool FreeHandParser::readDash(int zId)
1947 {
1948 MWAWInputStreamPtr input = getInput();
1949 libmwaw::DebugStream f;
1950
1951 long pos=input->tell();
1952 f.str("");
1953 if (zId)
1954 f << "Entries(Dash)[Z" << zId << "]:";
1955 else
1956 f << "Entries(Dash):";
1957 if (zId && m_state->getZoneType(zId)!=FreeHandParserInternal::Z_Dash) {
1958 MWAW_DEBUG_MSG(("FreeHandParser::readDash: find unexpected zone type for zone %d\n", zId));
1959 }
1960 FreeHandParserInternal::StyleHeader zone;
1961 if (!readStyleHeader(zone)) {
1962 input->seek(pos, librevenge::RVNG_SEEK_SET);
1963 return false;
1964 }
1965 int const vers=version();
1966 if (zone.m_size < 12 || (vers==1 && zone.m_type!=0x1195) || (vers>1 && zone.m_type!=0x157d)) {
1967 input->seek(pos, librevenge::RVNG_SEEK_SET);
1968 return false;
1969 }
1970 long endPos;
1971 if (vers==1)
1972 endPos=pos+2+zone.m_size;
1973 else {
1974 endPos=pos-2+zone.m_size;
1975 for (int i=0; i<2; ++i) { // 0
1976 auto val=static_cast<int>(input->readULong(2));
1977 if (val)
1978 f << "f" << i << "=" << val << ",";
1979 }
1980 }
1981 f << zone;
1982 auto N=static_cast<int>(input->readLong(2));
1983 if (endPos!=input->tell()+2*N || !input->checkPosition(endPos)) {
1984 input->seek(pos, librevenge::RVNG_SEEK_SET);
1985 return false;
1986 }
1987 f << "dash=[";
1988 std::vector<float> dashes;
1989 for (int j=0; j<N; ++j) {
1990 dashes.push_back(float(input->readLong(2))/10.f);
1991 f << dashes.back() << ",";
1992 }
1993 f << "],";
1994 if (zId && m_state->m_zIdToDashMap.find(zId)==m_state->m_zIdToDashMap.end())
1995 m_state->m_zIdToDashMap[zId]=dashes;
1996 ascii().addPos(pos);
1997 ascii().addNote(f.str().c_str());
1998 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1999
2000 return true;
2001 }
2002
2003 bool FreeHandParser::readFillStyle(int zId)
2004 {
2005 MWAWInputStreamPtr input = getInput();
2006 libmwaw::DebugStream f;
2007
2008 long pos=input->tell();
2009 if (!input->checkPosition(pos+13))
2010 return false;
2011 f.str("");
2012 if (zId)
2013 f << "Entries(FillStyle)[Z" << zId << "]:";
2014 else
2015 f << "Entries(FillStyle):";
2016 if (zId && m_state->getZoneType(zId)!=FreeHandParserInternal::Z_Fill) {
2017 MWAW_DEBUG_MSG(("FreeHandParser::readFillStyle: find unexpected zone type for zone %d\n", zId));
2018 }
2019 FreeHandParserInternal::StyleHeader zone;
2020 if (!readStyleHeader(zone)) {
2021 input->seek(pos, librevenge::RVNG_SEEK_SET);
2022 return false;
2023 }
2024 FreeHandParserInternal::FillStyle style;
2025 int expectedSize=0;
2026 int const vers=version();
2027 switch (zone.m_type) {
2028 case 0x10cd:
2029 f << "basic,";
2030 if (zone.m_size!=0x12) f << "sz?=" << zone.m_size << ",";
2031 expectedSize=3;
2032 break;
2033 case 0x10d0:
2034 f << "gradient,";
2035 style.m_type=MWAWGraphicStyle::Gradient::G_Linear;
2036 if (zone.m_size!=0x18) f << "sz?=" << zone.m_size << ",";
2037 expectedSize=8;
2038 break;
2039 case 0x10d1:
2040 f << "radial,";
2041 style.m_type=MWAWGraphicStyle::Gradient::G_Radial;
2042 if (zone.m_size!=0x14) f << "sz?=" << zone.m_size << ",";
2043 expectedSize=4;
2044 break;
2045 case 0x14b5:
2046 f << "basic,";
2047 if (zone.m_size!=0x16) f << "sz?=" << zone.m_size << ",";
2048 expectedSize=8;
2049 break;
2050 case 0x14b7:
2051 f << "gradient,";
2052 style.m_type=MWAWGraphicStyle::Gradient::G_Linear;
2053 if (zone.m_size!=0x1c) f << "sz?=" << zone.m_size << ",";
2054 expectedSize=12;
2055 break;
2056 case 0x14b8:
2057 f << "radial,";
2058 style.m_type=MWAWGraphicStyle::Gradient::G_Radial;
2059 if (zone.m_size!=0x1e) f << "sz?=" << zone.m_size << ",";
2060 expectedSize=14;
2061 break;
2062 case 0x14d3:
2063 f << "pattern,";
2064 if (zone.m_size!=0x1c) f << "sz?=" << zone.m_size << ",";
2065 expectedSize=14;
2066 break;
2067 case 0x14dd:
2068 f << "tiled,";
2069 if (zone.m_size!=0x44) f << "sz?=" << zone.m_size << ",";
2070 expectedSize=54;
2071 break;
2072 default:
2073 break;
2074 }
2075 long endPos=input->tell()+expectedSize;
2076 if (expectedSize==0 || !input->checkPosition(endPos)) {
2077 input->seek(pos, librevenge::RVNG_SEEK_SET);
2078 return false;
2079 }
2080 f << zone;
2081 if (vers>1) {
2082 for (int i=0; i<2; ++i) { // always 0
2083 auto val=static_cast<int>(input->readLong(2));
2084 if (val) f << "f" << i << "=" << val << ",";
2085 }
2086 }
2087 auto id=static_cast<int>(input->readULong(2));
2088 if (zone.m_type==0x14dd) {
2089 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_Group)) {
2090 input->seek(pos, librevenge::RVNG_SEEK_SET);
2091 return false;
2092 }
2093 else if (id) {
2094 static bool first=true;
2095 if (first) {
2096 MWAW_DEBUG_MSG(("FreeHandParser::readFillStyle: retrieving tiled style is not implemented\n"));
2097 first=false;
2098 }
2099 f << "group=Z" << id << ",";
2100 }
2101 for (int i=0; i<4; ++i) { // always 0
2102 auto val=static_cast<int>(input->readLong(2));
2103 if (val) f << "f" << i+2 << "=" << val << ",";
2104 }
2105 f << "scale=" << float(input->readLong(4))/65536.f << "x" << float(input->readLong(4))/65536.f << ",";
2106 f << "decal=" << float(input->readLong(2))/10.f << "x" << float(input->readLong(2))/10.f << ",";
2107 f << "angle=" << float(input->readLong(2))/10.f << ",";
2108 f << "fl=" << std::hex << input->readULong(2) << std::dec << ","; // 39|49
2109 f << "rot=[";
2110 for (int i=0; i<4; ++i)
2111 f << float(input->readLong(4))/65536.f << ",";
2112 f << "],";
2113 f << "trans=[";
2114 for (int i=0; i<2; ++i)
2115 f << float(input->readLong(4))/65536.f/10.f << ",";
2116 f << "],";
2117 }
2118 else if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_Color)) {
2119 input->seek(pos, librevenge::RVNG_SEEK_SET);
2120 return false;
2121 }
2122 else if (id) {
2123 style.m_colorId[0]=id;
2124 f << "color=Z" << id << ",";
2125 }
2126 if (zone.m_type==0x10d0 || zone.m_type==0x10d1 || zone.m_type==0x14b7 || zone.m_type==0x14b8) {
2127 id=static_cast<int>(input->readULong(2));
2128 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_Color)) {
2129 input->seek(pos, librevenge::RVNG_SEEK_SET);
2130 return false;
2131 }
2132 else if (id) {
2133 style.m_colorId[1]=id;
2134 f << "color2=Z" << id << ",";
2135 }
2136 }
2137 if (zone.m_type==0x10d0 || zone.m_type==0x14b7) {
2138 style.m_angle = float(input->readULong(2))/10.f;
2139 f << "angle=" << style.m_angle << ",";
2140 auto val=static_cast<int>(input->readULong(vers==1 ? 1 : 2));
2141 switch (val) {
2142 case 1:
2143 f << "linear,";
2144 break;
2145 case 2:
2146 style.m_logarithm=true;
2147 f << "logarithm,";
2148 break;
2149 default:
2150 MWAW_DEBUG_MSG(("FreeHandParser::readFillStyle: find unexpected gradient type\n"));
2151 f << "#gradient[type]=" << val << ",";
2152 }
2153 }
2154 else if (zone.m_type==0x14b8) {
2155 for (int i=0; i<3; ++i) { // always 0
2156 auto val=static_cast<int>(input->readLong(2));
2157 if (val) f << "g" << i << "=" << val << ",";
2158 }
2159 }
2160 else if (zone.m_type==0x14d3) {
2161 MWAWGraphicStyle::Pattern pattern;
2162 pattern.m_colors[0]=MWAWColor::white();
2163 pattern.m_colors[1]=MWAWColor::black();
2164 pattern.m_dim=MWAWVec2i(8,8);
2165 pattern.m_data.resize(8);
2166 for (auto &data : pattern.m_data)
2167 data=static_cast<uint8_t>(input->readULong(1));
2168 style.m_pattern=pattern;
2169 f << pattern;
2170 }
2171 if ((vers==1 && zone.m_type!=0x10d1) || (vers>1 && zone.m_type==0x14b5)) {
2172 auto val=static_cast<int>(input->readULong(vers==1 ? 1 : 2)); // always 0
2173 if (val==1)
2174 f << "overprint,";
2175 else
2176 f << "g0=" << val << ",";
2177 }
2178 if (zId && m_state->m_zIdToFillStyleMap.find(zId)==m_state->m_zIdToFillStyleMap.end())
2179 m_state->m_zIdToFillStyleMap[zId]=style;
2180 ascii().addPos(pos);
2181 ascii().addNote(f.str().c_str());
2182 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2183
2184 return true;
2185 }
2186
2187 bool FreeHandParser::readLineStyle(int zId)
2188 {
2189 MWAWInputStreamPtr input = getInput();
2190 libmwaw::DebugStream f;
2191
2192 long pos=input->tell();
2193 if (!input->checkPosition(pos+13))
2194 return false;
2195 f.str("");
2196 if (zId)
2197 f << "Entries(LinStyle)[Z" << zId << "]:";
2198 else
2199 f << "Entries(LinStyle):";
2200 if (zId && m_state->getZoneType(zId)!=FreeHandParserInternal::Z_LineStyle) {
2201 MWAW_DEBUG_MSG(("FreeHandParser::readLineStyle: find unexpected zone type for zone %d\n", zId));
2202 }
2203 FreeHandParserInternal::StyleHeader zone;
2204 int const vers=version();
2205 if (!readStyleHeader(zone)) {
2206 input->seek(pos, librevenge::RVNG_SEEK_SET);
2207 return false;
2208 }
2209 f << zone;
2210 long endPos=0;
2211 bool ok=false;
2212 switch (zone.m_type) {
2213 case 0x10ce:
2214 ok=(vers==1);
2215 endPos=input->tell()+12;
2216 if (zone.m_size!=0x1c) f << "sz?=" << zone.m_size << ",";
2217 break;
2218 case 0x14b6:
2219 ok=(vers>1);
2220 endPos=input->tell()+18;
2221 if (zone.m_size!=0x22) f << "sz?=" << zone.m_size << ",";
2222 break;
2223 case 0x14d4:
2224 f << "pattern,";
2225 ok=(vers>1);
2226 endPos=input->tell()+22;
2227 if (zone.m_size!=0x24) f << "sz?=" << zone.m_size << ",";
2228 break;
2229 default:
2230 ok=false;
2231 break;
2232 }
2233 if (!ok || !input->checkPosition(endPos)) {
2234 input->seek(pos, librevenge::RVNG_SEEK_SET);
2235 return false;
2236 }
2237 if (vers>1) {
2238 for (int i=0; i<2; ++i) { // 0
2239 auto val=static_cast<int>(input->readLong(2));
2240 if (val)
2241 f << "f" << i << "=" << val << ",";
2242 }
2243 }
2244 FreeHandParserInternal::LineStyle style;
2245 auto id=static_cast<int>(input->readULong(2));
2246 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_Color)) {
2247 input->seek(pos, librevenge::RVNG_SEEK_SET);
2248 return false;
2249 }
2250 else if (id) {
2251 style.m_colorId=id;
2252 f << "color=Z" << id << ",";
2253 }
2254 if (zone.m_type==0x14d4) {
2255 MWAWGraphicStyle::Pattern pattern;
2256 pattern.m_colors[0]=MWAWColor::white();
2257 pattern.m_colors[1]=MWAWColor::black();
2258 pattern.m_dim=MWAWVec2i(8,8);
2259 pattern.m_data.resize(8);
2260 for (auto &data : pattern.m_data)
2261 data=static_cast<uint8_t>(input->readULong(1));
2262 style.m_pattern=pattern;
2263 f << pattern;
2264 }
2265 else {
2266 id=static_cast<int>(input->readULong(2));
2267 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_Dash)) {
2268 input->seek(pos, librevenge::RVNG_SEEK_SET);
2269 return false;
2270 }
2271 else if (id) {
2272 style.m_dashId=id;
2273 f << "dash=Z" << id << ",";
2274 }
2275 }
2276 // probably cosecant(miter/2)
2277 float value=float(input->readLong(4))/65536.f;
2278 if (value<=-1||value>=1) {
2279 style.m_miterLimit=float(360./M_PI)*float(std::asin(1/value));
2280 f << "miter[limit]=" << style.m_miterLimit << ",";
2281 }
2282 else if (value<0||value>0) {
2283 f << "##miter[limit]=2*asin(" << 1/value << "),";
2284 }
2285 else
2286 f << "miter[limit]*,";
2287 style.m_width=vers==1 ? float(input->readLong(2))/10.0f : float(input->readLong(4))/65536.f/10.0f;
2288 f << "width=" << style.m_width << ",";
2289 if (zone.m_type!=0x14d4) {
2290 auto val=static_cast<int>(input->readULong(1));
2291 switch (val) {
2292 case 0: // default
2293 break;
2294 case 1:
2295 style.m_join=MWAWGraphicStyle::J_Bevel;
2296 f << "join=bevel,";
2297 break;
2298 case 2:
2299 style.m_join=MWAWGraphicStyle::J_Round;
2300 f << "join=round,";
2301 break;
2302 default:
2303 MWAW_DEBUG_MSG(("FreeHandParser::readLineStyle: find unknown join\n"));
2304 f << "#join=" << val << ",";
2305 }
2306 val=static_cast<int>(input->readULong(1));
2307 switch (val) {
2308 case 0: // default
2309 break;
2310 case 1:
2311 style.m_cap=MWAWGraphicStyle::C_Round;
2312 f << "cap=round,";
2313 break;
2314 case 2:
2315 style.m_cap=MWAWGraphicStyle::C_Square;
2316 f << "cap=square,";
2317 break;
2318 default:
2319 MWAW_DEBUG_MSG(("FreeHandParser::readLineStyle: find unknown cap\n"));
2320 f << "#cap=" << val << ",";
2321 }
2322 }
2323 if (zId && m_state->m_zIdToLineStyleMap.find(zId)==m_state->m_zIdToLineStyleMap.end())
2324 m_state->m_zIdToLineStyleMap[zId]=style;
2325 ascii().addPos(pos);
2326 ascii().addNote(f.str().c_str());
2327 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2328
2329 return true;
2330 }
2331
2332 bool FreeHandParser::readPostscriptStyle(int zId)
2333 {
2334 MWAWInputStreamPtr input = getInput();
2335 libmwaw::DebugStream f;
2336
2337 long pos=input->tell();
2338 if (!input->checkPosition(pos+12))
2339 return false;
2340 f.str("");
2341 if (zId)
2342 f << "Entries(Postscript)[Z" << zId << "]:";
2343 else
2344 f << "Entries(Postscript):";
2345 if (zId) {
2346 auto type=m_state->getZoneType(zId);
2347 if (type!=FreeHandParserInternal::Z_Fill && type!=FreeHandParserInternal::Z_LineStyle) {
2348 MWAW_DEBUG_MSG(("FreeHandParser::readPostscriptStyle: find unexpected zone type for zone %d\n", zId));
2349 }
2350 }
2351 FreeHandParserInternal::StyleHeader zone;
2352 if (!readStyleHeader(zone)) {
2353 input->seek(pos, librevenge::RVNG_SEEK_SET);
2354 return false;
2355 }
2356 f << zone;
2357 long endPos;
2358 int sSz;
2359 if (version()==1) {
2360 if (zone.m_type!=0x10cf) {
2361 input->seek(pos, librevenge::RVNG_SEEK_SET);
2362 return false;
2363 }
2364 if (zone.m_size!=0x12) f << "sz?=" << zone.m_size << ",";
2365 sSz=static_cast<int>(input->readULong(1));
2366 endPos=input->tell()+sSz;
2367 }
2368 else {
2369 bool ok=true;
2370 if (zone.m_type==0x14c9)
2371 f << "surf,";
2372 else if (zone.m_type==0x14ca)
2373 f << "line,";
2374 else
2375 ok=false;
2376 if (!ok || zone.m_size<16) {
2377 input->seek(pos, librevenge::RVNG_SEEK_SET);
2378 return false;
2379 }
2380 for (int i=0; i<2; ++i) { // always 0?
2381 auto val=static_cast<int>(input->readLong(2));
2382 if (val) f << "f" << i << "=" << val << ",";
2383 }
2384 endPos=pos+zone.m_size-4;
2385 sSz=int(zone.m_size-16);
2386 }
2387 if (!input->checkPosition(endPos)) {
2388 input->seek(pos, librevenge::RVNG_SEEK_SET);
2389 return false;
2390 }
2391 std::string text;
2392 for (int i=0; i<sSz; ++i) text+=char(input->readULong(1));
2393 if (!text.empty())
2394 f << "ps=\"" << text << "\",";
2395 if (zId && m_state->m_zIdToPostscriptMap.find(zId)==m_state->m_zIdToPostscriptMap.end())
2396 m_state->m_zIdToPostscriptMap[zId]=text;
2397 ascii().addPos(pos);
2398 ascii().addNote(f.str().c_str());
2399 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2400
2401 return true;
2402 }
2403
2404 bool FreeHandParser::readBackgroundPicture(int zId)
2405 {
2406 MWAWInputStreamPtr input = getInput();
2407 libmwaw::DebugStream f;
2408
2409 long pos=input->tell();
2410 FreeHandParserInternal::ShapeHeader shape;
2411 if (!readShapeHeader(shape) || shape.m_type!=0x1007 || !input->checkPosition(input->tell()+32)) {
2412 input->seek(pos, librevenge::RVNG_SEEK_SET);
2413 return false;
2414 }
2415 FreeHandParserInternal::Shape res;
2416 res.m_type=FreeHandParserInternal::Shape::BackgroundPicture;
2417 res.m_layerId=shape.m_layerId;
2418 if (zId)
2419 f << "Entries(BackgroundPicture)[Z" << zId << "]:" << shape;
2420 else
2421 f << "Entries(BackgroundPicture):" << shape;
2422 for (int i=0; i<14; ++i) { // f1=29|39, f2=1, f8=1, f10=0|-5, f12=109|113|118
2423 auto val=static_cast<int>(input->readLong(2));
2424 if (val) f << "f" << i << "=" << val << ",";
2425 }
2426 auto picSize=long(input->readLong(4));
2427 res.m_picture.setBegin(input->tell());
2428 res.m_picture.setLength(picSize);
2429 if (picSize<0 || !input->checkPosition(res.m_picture.end())) {
2430 input->seek(pos, librevenge::RVNG_SEEK_SET);
2431 return false;
2432 }
2433 ascii().skipZone(res.m_picture.begin(), res.m_picture.end()-1);
2434 input->seek(picSize, librevenge::RVNG_SEEK_CUR);
2435 ascii().addPos(pos);
2436 ascii().addNote(f.str().c_str());
2437
2438 if (zId && m_state->m_zIdToShapeMap.find(zId)==m_state->m_zIdToShapeMap.end())
2439 m_state->m_zIdToShapeMap.insert(std::map<int,FreeHandParserInternal::Shape>::value_type(zId,res));
2440 return true;
2441 }
2442
2443 bool FreeHandParser::readPictureZone(int zId)
2444 {
2445 MWAWInputStreamPtr input = getInput();
2446 libmwaw::DebugStream f;
2447
2448 long pos=input->tell();
2449 FreeHandParserInternal::ShapeHeader shape;
2450 if (!readShapeHeader(shape) || shape.m_type!=0x13f8 || !input->checkPosition(input->tell()+58)) {
2451 input->seek(pos, librevenge::RVNG_SEEK_SET);
2452 return false;
2453 }
2454 long endPos=input->tell()+58;
2455 FreeHandParserInternal::Shape res;
2456 res.m_type=FreeHandParserInternal::Shape::Picture;
2457 res.m_layerId=shape.m_layerId;
2458 if (zId)
2459 f << "Entries(Picture)[Z" << zId << "]:" << shape;
2460 else
2461 f << "Entries(Picture):" << shape;
2462 if (zId && m_state->getZoneType(zId)!=FreeHandParserInternal::Z_Shape) {
2463 MWAW_DEBUG_MSG(("FreeHandParser::readPictureZone: find unexpected zone type for zone %d\n", zId));
2464 }
2465 for (int i=0; i<2; ++i) {
2466 auto id=static_cast<int>(input->readULong(2));
2467 if (!id) continue;
2468 if (!m_state->addZoneId(id, i==0 ? FreeHandParserInternal::Z_Picture : FreeHandParserInternal::Z_PictureName)) {
2469 input->seek(pos, librevenge::RVNG_SEEK_SET);
2470 return false;
2471 }
2472 if (i==0) {
2473 res.m_dataId=id;
2474 f << "data=Z" << id << ",";
2475 }
2476 else
2477 f << "name=Z" << id << ",";
2478 }
2479 auto val=static_cast<int>(input->readLong(2)); // 0
2480 if (val) f << "f0=" << val << ",";
2481 float dim[6];
2482 for (int i=0; i<2; ++i) dim[i]=float(input->readLong(2))/10.f;
2483 f << "dim=" << MWAWVec2f(dim[1],dim[0]) << ",";
2484 // checkme: why are the coord inverted ?
2485 res.m_box=MWAWBox2f(MWAWVec2f(0,0), MWAWVec2f(dim[1],dim[0]));
2486 for (int i=0; i<2; ++i) { // 0?
2487 val=static_cast<int>(input->readLong(2)); // 0
2488 if (val) f << "f" << i+1 << "=" << val << ",";
2489 }
2490 f << "flags=" << std::hex << input->readULong(2) << std::dec << ",";
2491 f << "rot=[";
2492 for (int i=0; i<4; ++i) {
2493 dim[i]=float(input->readLong(4))/65536.f;
2494 f << dim[i] << ",";
2495 }
2496 f << "],";
2497 f << "trans=[";
2498 for (int i=0; i<2; ++i) {
2499 dim[i+4]=float(input->readLong(4))/65536.f/10.f;
2500 f << dim[i+4] << ",";
2501 }
2502 f << "],";
2503 res.m_transformation=MWAWTransformation(MWAWVec3f(dim[0],dim[2],dim[4]),MWAWVec3f(dim[1],dim[3],dim[5]));
2504 auto id=static_cast<int>(input->readULong(2));
2505 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_Color)) {
2506 MWAW_DEBUG_MSG(("FreeHandParser::readPictureZone: find unexpected colorId\n"));
2507 f << "###colorId,";
2508 }
2509 else if (id)
2510 f << "color=Z" << id << ",";
2511 for (int i=0; i<2; ++i) {
2512 int iDim[4];
2513 for (auto &d : iDim) d=static_cast<int>(input->readLong(2));
2514 f << "box" << i << "=" << MWAWBox2i(MWAWVec2i(iDim[0],iDim[1]),MWAWVec2i(iDim[2],iDim[3])) << ",";
2515 }
2516 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2517 ascii().addPos(pos);
2518 ascii().addNote(f.str().c_str());
2519
2520 if (zId && m_state->m_zIdToShapeMap.find(zId)==m_state->m_zIdToShapeMap.end())
2521 m_state->m_zIdToShapeMap.insert(std::map<int,FreeHandParserInternal::Shape>::value_type(zId,res));
2522 return true;
2523 }
2524
2525 bool FreeHandParser::readShape(int zId)
2526 {
2527 MWAWInputStreamPtr input = getInput();
2528 libmwaw::DebugStream f;
2529
2530 long pos=input->tell();
2531 FreeHandParserInternal::ShapeHeader shape;
2532 if (!readShapeHeader(shape)) {
2533 input->seek(pos, librevenge::RVNG_SEEK_SET);
2534 return false;
2535 }
2536 bool canHaveMatrix=true, hasDimension=true;
2537 int dataSize=-1;
2538 FreeHandParserInternal::Shape res;
2539 res.m_layerId=shape.m_layerId;
2540 const int vers=version();
2541 switch (shape.m_type) {
2542 case 0x1131:
2543 case 0x1519:
2544 if (zId)
2545 f << "Entries(Rectangle)[Z" << zId << "]:" << shape;
2546 else
2547 f << "Entries(Rectangle):" << shape;
2548 dataSize=4;
2549 res.m_type=FreeHandParserInternal::Shape::Rectangle;
2550 break;
2551 case 0x1132:
2552 case 0x151a:
2553 if (zId)
2554 f << "Entries(Circle)[Z" << zId << "]:" << shape;
2555 else
2556 f << "Entries(Circle):" << shape;
2557 dataSize=0;
2558 res.m_type=FreeHandParserInternal::Shape::Ellipse;
2559 break;
2560 case 0x1134:
2561 case 0x151c:
2562 if (zId)
2563 f << "Entries(Spline)[Z" << zId << "]:" << shape;
2564 else
2565 f << "Entries(Spline):" << shape;
2566 dataSize=4;
2567 hasDimension=canHaveMatrix=false;
2568 res.m_type=FreeHandParserInternal::Shape::Path;
2569 break;
2570 case 0x1135:
2571 case 0x151d:
2572 if (zId)
2573 f << "Entries(Line)[Z" << zId << "]:" << shape;
2574 else
2575 f << "Entries(Line):" << shape;
2576 dataSize=0;
2577 canHaveMatrix=false;
2578 res.m_type=FreeHandParserInternal::Shape::Line;
2579 break;
2580 default:
2581 break;
2582 }
2583 if (dataSize<0 || !input->checkPosition(input->tell()+4+(vers>1 ? 6 : 0)+(hasDimension ? 8 : 0)+(canHaveMatrix ? 4 : 0)+dataSize)) {
2584 input->seek(pos, librevenge::RVNG_SEEK_SET);
2585 return false;
2586 }
2587 f << "sz?=" << shape.m_size << ",";
2588 if (zId && m_state->getZoneType(zId)!=FreeHandParserInternal::Z_Shape) {
2589 MWAW_DEBUG_MSG(("FreeHandParser::readShape: find unexpected zone type for zone %d\n", zId));
2590 }
2591 if (vers>1) {
2592 auto id=static_cast<int>(input->readULong(2));
2593 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_Group)) {
2594 MWAW_DEBUG_MSG(("FreeHandParser::readShapeHeader: find unexpected group id\n"));
2595 f << "###groupId,";
2596 }
2597 else if (id) {
2598 f << "group=Z" << id << ",";
2599 res.m_childs.push_back(id);
2600 }
2601 }
2602 auto id=static_cast<int>(input->readLong(2)); // always 0?
2603 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_Fill)) {
2604 MWAW_DEBUG_MSG(("FreeHandParser::readShape: find a bad color\n"));
2605 f << "###";
2606 }
2607 if (id) f << "fill=Z" << id << ",";
2608 res.m_fillId=id;
2609 id=static_cast<int>(input->readULong(2));
2610 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_LineStyle)) {
2611 MWAW_DEBUG_MSG(("FreeHandParser::readShape: find a bad style\n"));
2612 f << "###";
2613 }
2614 if (id) f << "line[style]=Z" << id << ",";
2615 res.m_lineId=id;
2616 if (vers>1) {
2617 for (int i=0; i<2; ++i) { // always 0?
2618 auto val=static_cast<int>(input->readULong(2));
2619 if (val) f << "f" << i << "=" << val << ",";
2620 }
2621 }
2622 if (hasDimension) {
2623 float dim[4];
2624 for (auto &d : dim) d=float(input->readLong(2))/10.f;
2625 res.m_box=MWAWBox2f(MWAWVec2f(dim[0],dim[1]),MWAWVec2f(dim[2],dim[3]));
2626 f << "rect=" << res.m_box << ",";
2627 }
2628 int dSz=canHaveMatrix ? static_cast<int>(input->readULong(4)) : 0;
2629 if (dSz<0 || !input->checkPosition(input->tell()+dSz+dataSize)) {
2630 input->seek(pos, librevenge::RVNG_SEEK_SET);
2631 return false;
2632 }
2633 if (dSz==0x1a) {
2634 f << "flags=" << std::hex << input->readULong(2) << std::dec << ",";
2635 float dim[6];
2636 f << "rot=[";
2637 for (int i=0; i<4; ++i) {
2638 dim[i]=float(input->readLong(4))/65536.f;
2639 f << dim[i] << ",";
2640 }
2641 f << "],";
2642 f << "trans=[";
2643 for (int i=0; i<2; ++i) {
2644 dim[i+4]=float(input->readLong(4))/65536.f/10.f;
2645 f << dim[i+4] << ",";
2646 }
2647 f << "],";
2648 res.m_transformation=MWAWTransformation(MWAWVec3f(dim[0],dim[2],dim[4]),MWAWVec3f(dim[1],dim[3],dim[5]));
2649 }
2650 else if (dSz) {
2651 MWAW_DEBUG_MSG(("FreeHandParser::readShape: find unknown matrix size\n"));
2652 f << "###matrix,";
2653 input->seek(dSz, librevenge::RVNG_SEEK_CUR);
2654 }
2655 if (shape.m_type==0x1131 || shape.m_type==0x1519) {
2656 float dim[2];
2657 for (auto &d : dim) d=float(input->readLong(2))/10.f;
2658 res.m_corner=MWAWVec2f(dim[0],dim[1]);
2659 if (res.m_corner!=MWAWVec2f(0,0))
2660 f << "corner=" << res.m_corner << ",";
2661 }
2662 if (shape.m_type==0x1134 || shape.m_type==0x151c) {
2663 auto val=static_cast<int>(input->readULong(2));
2664 if (val&1) {
2665 res.m_closed=true;
2666 f << "closed,";
2667 }
2668 if (val&2) {
2669 res.m_evenOdd=true;
2670 f << "even/odd,";
2671 }
2672 val &= 0xFFFC;
2673 // find also 4
2674 if (val) f << "fl=" << std::hex << val << std::dec << ",";
2675 auto nPt=static_cast<int>(input->readULong(2));
2676 f << "N=" << nPt << ",";
2677 if (!input->checkPosition(input->tell()+16*nPt)) {
2678 input->seek(pos, librevenge::RVNG_SEEK_SET);
2679 return false;
2680 }
2681 ascii().addPos(pos);
2682 ascii().addNote(f.str().c_str());
2683 for (int i=0; i<nPt; ++i) {
2684 pos=input->tell();
2685 f.str("");
2686 f << "Spline-" << i << ":";
2687 val=static_cast<int>(input->readULong(2));
2688 switch (val) {
2689 case 0: // corner
2690 break;
2691 case 1:
2692 f << "connector,";
2693 break;
2694 case 2:
2695 f << "curve,";
2696 break;
2697 default:
2698 // find also 0xf0
2699 MWAW_DEBUG_MSG(("FreeHandParser::readShape: find unknown point type\n"));
2700 f << "#type=" << val << ",";
2701 }
2702 val=static_cast<int>(input->readULong(2));
2703 if (val&0x100) f << "no[autoCurvature],";
2704 val &= 0xFEFF;
2705 // find unknow [01][4|9|b]
2706 if (val) f << "fl=" << std::hex << val << std::dec << ",";
2707 MWAWVec2f coord[3];
2708 for (auto &pt : coord) {
2709 float dim[2];
2710 for (auto &d : dim) d=float(input->readLong(2))/10.f;
2711 pt=MWAWVec2f(dim[0], dim[1]);
2712 res.m_vertices.push_back(pt);
2713 }
2714 if (coord[0]==coord[1] && coord[0]==coord[2])
2715 f << coord[0] << ",";
2716 else
2717 f << "pts=[" << coord[0] << "," << coord[1] << "," << coord[2] << "],";
2718 ascii().addPos(pos);
2719 ascii().addNote(f.str().c_str());
2720 }
2721 }
2722 else {
2723 ascii().addPos(pos);
2724 ascii().addNote(f.str().c_str());
2725 }
2726 if (zId && m_state->m_zIdToShapeMap.find(zId)==m_state->m_zIdToShapeMap.end())
2727 m_state->m_zIdToShapeMap.insert(std::map<int,FreeHandParserInternal::Shape>::value_type(zId,res));
2728 return true;
2729 }
2730
2731 bool FreeHandParser::readTextboxV1(int zId)
2732 {
2733 MWAWInputStreamPtr input = getInput();
2734 libmwaw::DebugStream f;
2735
2736 long pos=input->tell();
2737 FreeHandParserInternal::ShapeHeader shape;
2738 if (!readShapeHeader(shape) || shape.m_type!=0x1006 || !input->checkPosition(input->tell()+4+8+54)) {
2739 input->seek(pos, librevenge::RVNG_SEEK_SET);
2740 return false;
2741 }
2742 if (zId)
2743 f << "Entries(Textbox)[Z" << zId << "]:" << shape;
2744 else
2745 f << "Entries(Textbox):" << shape;
2746 FreeHandParserInternal::Textbox textbox(zId);
2747 textbox.m_layerId=shape.m_layerId;
2748 f << "sz?=" << shape.m_size << ",";
2749 if (zId && m_state->getZoneType(zId)!=FreeHandParserInternal::Z_Shape) {
2750 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV1: find unexpected zone type for zone %d\n", zId));
2751 }
2752 auto val=static_cast<int>(input->readLong(2)); // always 0?
2753 if (val) f << "f0=" << val << ",";
2754 auto nbPt=static_cast<int>(input->readULong(2)); // nbPt=4*textSz
2755 f << "N=" << nbPt << ",";
2756 long actPos=input->tell();
2757 if ((nbPt%2) || !input->checkPosition(actPos+3*(nbPt/2)+8+54)) {
2758 input->seek(pos, librevenge::RVNG_SEEK_SET);
2759 return false;
2760 }
2761 // probably nbPt/2 float (size of each char)+ nbPt/2 bytes (flag?)
2762 ascii().skipZone(actPos, actPos+3*(nbPt/2)-1);
2763
2764 actPos+=3*(nbPt/2);
2765 ascii().addPos(pos);
2766 ascii().addNote(f.str().c_str());
2767
2768 input->seek(actPos, librevenge::RVNG_SEEK_SET);
2769 f.str("");
2770 f << "Textbox-A:";
2771 for (int i=0; i<3; ++i) { // f0=0|2|-82|-123|-225, f1=0|-41|-82|-123|-151, f2=0
2772 val=static_cast<int>(input->readLong(2));
2773 if (val) f << "f" << i << "=" << val << ",";
2774 }
2775 auto sSz=static_cast<int>(input->readULong(2));
2776 if (!input->checkPosition(input->tell()+sSz+54)) {
2777 input->seek(pos, librevenge::RVNG_SEEK_SET);
2778 return false;
2779 }
2780 f << "text[sz]=" << sSz << ",";
2781 ascii().addPos(actPos);
2782 ascii().addNote(f.str().c_str());
2783
2784 textbox.m_text.setBegin(input->tell());
2785 textbox.m_text.setLength(sSz);
2786 input->seek(textbox.m_text.end(), librevenge::RVNG_SEEK_SET);
2787
2788 actPos=input->tell();
2789 f.str("");
2790 f << "Textbox-B:";
2791 float dim[6];
2792 for (int i=0; i<4; ++i) dim[i]=float(input->readLong(2))/10.f;
2793 textbox.m_box=MWAWBox2f(MWAWVec2f(dim[0],dim[1]),MWAWVec2f(dim[2],dim[3]));
2794 f << "dim=" << textbox.m_box << ",";
2795 f << "flags=" << std::hex << input->readULong(2) << std::dec << ",";
2796 f << "rot=[";
2797 for (int i=0; i<4; ++i) {
2798 dim[i]=float(input->readLong(4))/65536.f;
2799 f << dim[i] << ",";
2800 }
2801 f << "],";
2802 f << "trans=[";
2803 for (int i=0; i<2; ++i) {
2804 dim[i+4]=float(input->readLong(4))/65536.f/10.f;
2805 f << dim[i+4] << ",";
2806 }
2807 f << "],";
2808 textbox.m_transformation=MWAWTransformation(MWAWVec3f(dim[0],dim[2],dim[4]),MWAWVec3f(dim[1],dim[3],dim[5]));
2809 f << "spacing=["; // letter and word
2810 for (int i=0; i<2; ++i) {
2811 dim[i]=float(input->readLong(4))/65536.f/10.f;
2812 f << dim[i] << ",";
2813 }
2814 f << "],";
2815 textbox.m_spacings=MWAWVec2f(dim[0],dim[1]);
2816 f << "scaling=[";
2817 for (int i=0; i<2; ++i) {
2818 dim[i]=float(input->readLong(4))/65536.f;
2819 f << dim[i] << ",";
2820 }
2821 f << "],";
2822 textbox.m_scalings=MWAWVec2f(dim[0],dim[1]);
2823 val=static_cast<int>(input->readLong(1)); // 0|1|2
2824 if (val) f << "f0=" << val << ",";
2825 val=static_cast<int>(input->readLong(1));
2826 switch (val) {
2827 case 0: // left
2828 break;
2829 case 1:
2830 f << "right,";
2831 textbox.m_justify=MWAWParagraph::JustificationRight;
2832 break;
2833 case 2:
2834 f << "center,";
2835 textbox.m_justify=MWAWParagraph::JustificationCenter;
2836 break;
2837 case 3:
2838 f << "justify=all,";
2839 textbox.m_justify=MWAWParagraph::JustificationFull;
2840 break;
2841 default:
2842 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV1: find unexpected align\n"));
2843 f << "###align=" << val << ",";
2844 break;
2845 }
2846 auto nPLC=static_cast<int>(input->readULong(2));
2847 f << "NPLC=" << nPLC << ",";
2848 if (!input->checkPosition(input->tell()+18*nPLC)) {
2849 input->seek(pos, librevenge::RVNG_SEEK_SET);
2850 return false;
2851 }
2852 ascii().addPos(actPos);
2853 ascii().addNote(f.str().c_str());
2854 for (int plc=0; plc<nPLC; ++plc) {
2855 actPos=input->tell();
2856 f.str("");
2857 f << "Textbox-PLC" << plc << ":";
2858 FreeHandParserInternal::Font font;
2859 if (plc+1!=nPLC) {
2860 auto id=static_cast<int>(input->readULong(2));
2861 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_String)) {
2862 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV1: find bad font name\n"));
2863 f << "###";
2864 }
2865 font.m_nameId=id;
2866 if (id)
2867 f << "font[name]=Z" << id << ",";
2868 id=static_cast<int>(input->readULong(2));
2869 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_Color)) {
2870 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV1: find bad color\n"));
2871 f << "###";
2872 }
2873 if (id)
2874 f << "color=Z" << id << ",";
2875 font.m_colorId=id;
2876 float sz=float(input->readLong(4))/65536.f;
2877 font.m_font.setSize(sz);
2878 f << "font[sz]=" << sz << ",";
2879 val=static_cast<int>(input->readLong(4));
2880 switch (val) { // useme
2881 case -2: // solid
2882 break;
2883 case -1:
2884 f << "leading=auto,";
2885 break;
2886 default:
2887 f << "leading=" << float(val)/65536.f << ",";
2888 }
2889 }
2890 else
2891 input->seek(12, librevenge::RVNG_SEEK_CUR);
2892 auto cPos=static_cast<int>(input->readULong(2));
2893 f << "pos=" << cPos << ",";
2894 uint32_t flags=0;
2895 val=static_cast<int>(input->readULong(2));
2896 if (val & 1) {
2897 flags |= MWAWFont::boldBit;
2898 f << "bold,";
2899 }
2900 if (val & 2) {
2901 flags |= MWAWFont::italicBit;
2902 f << "italic,";
2903 }
2904 val &= 0xFFFC;
2905 if (val && plc+1!=nPLC) {
2906 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV1: find unknown font flag1\n"));
2907 f << "##flag1=" << std::hex << val << std::dec << ",";
2908 }
2909 val=static_cast<int>(input->readULong(2));
2910 switch (val) {
2911 case 1: // solid
2912 break;
2913 case 2:
2914 flags |= MWAWFont::boldBit;
2915 f << "heavy,";
2916 break;
2917 case 3:
2918 flags |= MWAWFont::italicBit;
2919 f << "oblique,";
2920 break;
2921 case 4:
2922 flags |= MWAWFont::outlineBit;
2923 f << "outline,";
2924 break;
2925 case 5:
2926 flags |= MWAWFont::shadowBit;
2927 f << "shadow,";
2928 break;
2929 default:
2930 if (plc+1==nPLC) break;
2931 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV1: find unknown font flag2\n"));
2932 f << "##flag2=" << val << ",";
2933 break;
2934 }
2935 font.m_font.setFlags(flags);
2936 textbox.m_posToFontMap[cPos]=font;
2937 ascii().addPos(actPos);
2938 ascii().addNote(f.str().c_str());
2939 }
2940 if (zId && m_state->m_zIdToTextboxMap.find(zId)==m_state->m_zIdToTextboxMap.end())
2941 m_state->m_zIdToTextboxMap.insert(std::map<int,FreeHandParserInternal::Textbox>::value_type(zId,textbox));
2942
2943 return true;
2944 }
2945
2946 bool FreeHandParser::readTextboxV2(int zId)
2947 {
2948 MWAWInputStreamPtr input = getInput();
2949 libmwaw::DebugStream f;
2950
2951 long pos=input->tell();
2952 FreeHandParserInternal::ShapeHeader shape;
2953 if (!readShapeHeader(shape) || shape.m_type!=0x13ee || !input->checkPosition(input->tell()+66)) {
2954 input->seek(pos, librevenge::RVNG_SEEK_SET);
2955 return false;
2956 }
2957 if (zId)
2958 f << "Entries(Textbox)[Z" << zId << "]:" << shape;
2959 else
2960 f << "Entries(Textbox):" << shape;
2961 FreeHandParserInternal::Textbox textbox(zId);
2962 textbox.m_layerId=shape.m_layerId;
2963 f << "sz?=" << shape.m_size << ",";
2964 if (zId && m_state->getZoneType(zId)!=FreeHandParserInternal::Z_Shape) {
2965 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV2: find unexpected zone type for zone %d\n", zId));
2966 }
2967 for (int i=0; i<6; ++i) { // f3=0|2, f4=0|1
2968 auto val=static_cast<int>(input->readLong(2));
2969 if (val) f << "f" << i << "=" << val << ",";
2970 }
2971 auto val=static_cast<int>(input->readULong(2)); // 1a|7b|c3|eb|f4|fb|114|1af|7d00|7d02
2972 if (val) f << "f6=" << val << ",";
2973 float dim[6];
2974 for (int i=0; i<4; ++i) dim[i]=float(input->readLong(2))/10.f;
2975 textbox.m_box=MWAWBox2f(MWAWVec2f(dim[0],dim[1]),MWAWVec2f(dim[2],dim[3]));
2976 f << "dim=" << textbox.m_box << ",";
2977 val=static_cast<int>(input->readULong(2)); // 0
2978 if (val) f << "f7=" << val << ",";
2979 f << "flags=" << std::hex << input->readULong(2) << std::dec << ",";
2980 f << "rot=[";
2981 for (int i=0; i<4; ++i) {
2982 dim[i]=float(input->readLong(4))/65536.f;
2983 f << dim[i] << ",";
2984 }
2985 f << "],";
2986 f << "trans=[";
2987 for (int i=0; i<2; ++i) {
2988 dim[i+4]=float(input->readLong(4))/65536.f/10.f;
2989 f << dim[i+4] << ",";
2990 }
2991 f << "],";
2992 textbox.m_transformation=MWAWTransformation(MWAWVec3f(dim[0],dim[2],dim[4]),MWAWVec3f(dim[1],dim[3],dim[5]));
2993 val=static_cast<int>(input->readLong(1));
2994 switch (val) {
2995 case 0: // left
2996 break;
2997 case 1:
2998 f << "center,";
2999 textbox.m_justify=MWAWParagraph::JustificationCenter;
3000 break;
3001 case 2:
3002 f << "right,";
3003 textbox.m_justify=MWAWParagraph::JustificationRight;
3004 break;
3005 case 3:
3006 f << "justify=all,";
3007 textbox.m_justify=MWAWParagraph::JustificationFull;
3008 break;
3009 default:
3010 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV2: find unexpected align\n"));
3011 f << "###align=" << val << ",";
3012 break;
3013 }
3014 ascii().addDelimiter(input->tell(), '|');
3015 ascii().addPos(pos);
3016 ascii().addNote(f.str().c_str());
3017 input->seek(11, librevenge::RVNG_SEEK_CUR);
3018
3019 pos=input->tell();
3020 f.str("");
3021 f << "Textbox-A:";
3022 auto dSz=static_cast<int>(input->readULong(2));
3023 auto sSz=static_cast<int>(input->readULong(2));
3024 long endPos=pos+dSz-18-80;
3025 if (dSz-18-80<58 || !input->checkPosition(endPos)) {
3026 input->seek(pos, librevenge::RVNG_SEEK_SET);
3027 return false;
3028 }
3029 f << "text[sz]=" << sSz << ",";
3030 val=static_cast<int>(input->readULong(2));
3031 if (val!=sSz) f << "text[pos]=" << val << ",";
3032 val=static_cast<int>(input->readULong(2)); // always 7ffd ?
3033 if (val!=0x7ffd) f << "f0=" << val << ",";
3034 for (int i=0; i<2; ++i) {
3035 val=static_cast<int>(input->readLong(2));
3036 if (val) f << "f" << i+1 << "=" << val << ",";
3037 }
3038 FreeHandParserInternal::Font font;
3039 auto id=static_cast<int>(input->readULong(2));
3040 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_String)) {
3041 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV2: find bad font name\n"));
3042 f << "###";
3043 }
3044 font.m_nameId=id;
3045 if (id)
3046 f << "font[name]=Z" << id << ",";
3047 float sz=float(input->readLong(4))/65536.f;
3048 font.m_font.setSize(sz);
3049 f << "font[sz]=" << sz << ",";
3050 val=static_cast<int>(input->readULong(4));
3051 // use me
3052 if (val==static_cast<int>(0xFFFE0000))
3053 f << "leading=auto,";
3054 else if (val!=static_cast<int>(0xFFFF0000)) // no solid
3055 f << "leading=" << float(val)/65536.f << ",";
3056 val=static_cast<int>(input->readLong(2));
3057 if (val) f << "f4=" << val << ",";
3058 uint32_t flags=0;
3059 val=static_cast<int>(input->readULong(2));
3060 if (val & 1) {
3061 flags |= MWAWFont::boldBit;
3062 f << "bold,";
3063 }
3064 if (val & 2) {
3065 flags |= MWAWFont::italicBit;
3066 f << "italic,";
3067 }
3068 val &= 0xFFFC;
3069 if (val) {
3070 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV2: find unknown font flag1\n"));
3071 f << "##flag1=" << std::hex << val << std::dec << ",";
3072 }
3073 id=static_cast<int>(input->readULong(2));
3074 if (id && !m_state->addZoneId(id, FreeHandParserInternal::Z_Color)) {
3075 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV2: find bad color\n"));
3076 f << "###";
3077 }
3078 if (id)
3079 f << "color=Z" << id << ",";
3080 font.m_colorId=id;
3081 val=static_cast<int>(input->readLong(2)); // 0
3082 if (val) f << "f6=" << val << ",";
3083 auto special=static_cast<int>(input->readLong(2));
3084 int specialData[6];
3085 for (int i=0; i<6; ++i) specialData[i]=static_cast<int>(input->readULong(i>=4 ? 1 : 2));
3086 if (!m_state->addZoneId(specialData[0], FreeHandParserInternal::Z_Color)) {
3087 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV2: find bad text color\n"));
3088 f << "###";
3089 }
3090 if (specialData[0])
3091 f << "col2=Z" << specialData[0] << ",";
3092 switch (special) {
3093 case 1: // solid
3094 break;
3095 case 2:
3096 flags |= MWAWFont::boldBit;
3097 f << "heavy,";
3098 break;
3099 case 3:
3100 flags |= MWAWFont::italicBit;
3101 f << "oblique,";
3102 break;
3103 case 4:
3104 flags |= MWAWFont::outlineBit;
3105 f << "outline,";
3106 break;
3107 case 5:
3108 flags |= MWAWFont::shadowBit;
3109 f << "shadow,";
3110 break;
3111 case 6:
3112 f << "fillAndStroke,";
3113 if (specialData[1]||specialData[2]) {
3114 f << "stroke[w]=" << float((specialData[1]<<16)+specialData[2])/65536.f << ",";
3115 specialData[1]=specialData[2]=0;
3116 }
3117 if (specialData[3]&0x100) f << "fill[set],";
3118 if (specialData[3]&0x1) f << "fill[overprint],";
3119 specialData[3]&=0xfefe;
3120 for (int i=0; i<2; ++i) {
3121 if (!specialData[4+i]) continue;
3122
3123 char const *wh[]= {"stroke[set]", "stroke[overprint]"};
3124 f << wh[i] << "=" << specialData[4+i] << ",";
3125 specialData[4+i]=0;
3126 }
3127 break;
3128 case 0x79:
3129 f << "char,";
3130 for (int i=1; i<5; ++i) {
3131 if (!specialData[i]) continue;
3132 char const *wh[]= {"", "fill[sz]", "line[spacing]", "stroke[width]", "has[stroke]"};
3133 if (i==3)
3134 f << wh[i] << "=" << float(specialData[i])/10.f << ",";
3135 else
3136 f << wh[i] << "=" << specialData[i] << ",";
3137 specialData[i]=0;
3138 }
3139 break;
3140 case 0x7a:
3141 f << "zoom,";
3142 for (int i=1; i<4; ++i) {
3143 if (!specialData[i]) continue;
3144 char const *wh[]= {"", "zoom[horOffset]", "zoom[verOffset]", "zoom[%]"};
3145 f << wh[i] << "=" << specialData[i] << ",";
3146 specialData[i]=0;
3147 }
3148 break;
3149 default:
3150 MWAW_DEBUG_MSG(("FreeHandParser::readTextboxV2: find unknown font flag2\n"));
3151 f << "##flag2=" << special << ",";
3152 break;
3153 }
3154 for (int i=1; i<6; ++i) {
3155 if (specialData[i])
3156 f << "#special" << i << "=" << specialData[i] << ",";
3157 }
3158 font.m_font.setFlags(flags);
3159 textbox.m_posToFontMap[0]=font;
3160 f << "spacing=["; // letter and word
3161 for (int i=0; i<2; ++i) {
3162 dim[i]=float(input->readLong(4))/65536.f;
3163 f << dim[i] << ",";
3164 }
3165 f << "],";
3166 textbox.m_spacings=MWAWVec2f(dim[0],dim[1]);
3167 textbox.m_scalings[0]=float(input->readLong(4))/65536.f;
3168 f << "scalings[hor]=" << textbox.m_scalings[0] << ",";
3169 textbox.m_baseline=float(input->readLong(4))/65536.f;
3170 if (textbox.m_baseline<0||textbox.m_baseline>0)
3171 f << "baseline=" << textbox.m_baseline << ",";
3172 ascii().addPos(pos);
3173 ascii().addNote(f.str().c_str());
3174 /* CHECKME: find some blocks here : [12bytes]* followed by [unkn]* and [22bytes]*
3175 for instance
3176 00290003007a9ce0000ffed1
3177 002a00190069046000100000
3178 0012000...
3179 001a0000000000c9f280000ac3de00033c22000f0000
3180 00290000000000690460000ac3de00033c22000f0000
3181 004d0000000000e2f160000ac3de00033c2200120000
3182 006000000000007a6860000ac3de00033c22000f0000
3183 */
3184 if (endPos!=input->tell()) {
3185 pos=input->tell();
3186 f.str("");
3187 f << "Textbox-B:";
3188 ascii().addPos(pos);
3189 ascii().addNote(f.str().c_str());
3190 }
3191 input->seek(endPos, librevenge::RVNG_SEEK_SET);
3192
3193 textbox.m_text.setBegin(input->tell());
3194 textbox.m_text.setLength(sSz);
3195 input->seek(textbox.m_text.end(), librevenge::RVNG_SEEK_SET);
3196
3197 if (zId && m_state->m_zIdToTextboxMap.find(zId)==m_state->m_zIdToTextboxMap.end())
3198 m_state->m_zIdToTextboxMap.insert(std::map<int,FreeHandParserInternal::Textbox>::value_type(zId,textbox));
3199
3200 return true;
3201 }
3202
3203 bool FreeHandParser::readDataZone(int zId)
3204 {
3205 MWAWInputStreamPtr input = getInput();
3206 libmwaw::DebugStream f;
3207 long pos=input->tell();
3208 if (!input->checkPosition(pos+10)) return false;
3209 auto dSz=long(input->readULong(4));
3210 auto opCode=static_cast<int>(input->readULong(2));
3211 auto dataSize=long(input->readULong(4));
3212 long endPos=pos+10+dataSize;
3213 if (opCode!=0x138b || dataSize<0 || !input->checkPosition(endPos)) {
3214 input->seek(pos, librevenge::RVNG_SEEK_SET);
3215 return false;
3216 }
3217 auto type=zId ? m_state->getZoneType(zId) : FreeHandParserInternal::Z_Unknown;
3218 if (type==FreeHandParserInternal::Z_Note) {
3219 f << "Entries(Note)[Z" << zId << "]:";
3220 if (dataSize) {
3221 auto sSz=static_cast<int>(input->readULong(1));
3222 if (sSz+1>dataSize) {
3223 MWAW_DEBUG_MSG(("FreeHandParser::readDataZone: can not read the note size\n"));
3224 f << "##sSz";
3225 }
3226 else {
3227 std::string note;
3228 for (int i=0; i<sSz; ++i) note+=char(input->readULong(1));
3229 f << note;
3230 }
3231 }
3232 }
3233 else if (type==FreeHandParserInternal::Z_PictureName) {
3234 f << "Picture[name][Z" << zId << "]:";
3235 if (dataSize<6) {
3236 MWAW_DEBUG_MSG(("FreeHandParser::readDataZone: can not read the picture name zone\n"));
3237 f << "##sSz";
3238 }
3239 else {
3240 auto val=static_cast<int>(input->readLong(4)); // disk id?
3241 if (val) f << "f0=" << std::hex << val << std::dec << ",";
3242 for (int i=0; i<2; ++i) { // disk name ?, file name
3243 auto sSz=static_cast<int>(input->readULong(1));
3244 if (input->tell()+sSz>endPos) {
3245 MWAW_DEBUG_MSG(("FreeHandParser::readDataZone: can not read some string\n"));
3246 f << "##sSz";
3247 break;
3248 }
3249 std::string name;
3250 for (int c=0; c<sSz; ++c) name+=char(input->readULong(1));
3251 f << name << ",";
3252 }
3253 }
3254 }
3255 else if (type==FreeHandParserInternal::Z_Picture) {
3256 f << "Picture[data][Z" << zId << "]:";
3257 if (dataSize) {
3258 MWAWEntry entry;
3259 entry.setBegin(input->tell());
3260 entry.setLength(dataSize);
3261 if (zId && m_state->m_zIdToDataMap.find(zId)==m_state->m_zIdToDataMap.end())
3262 m_state->m_zIdToDataMap[zId]=entry;
3263 ascii().skipZone(entry.begin(), entry.end()-1);
3264 }
3265 }
3266 else {
3267 MWAW_DEBUG_MSG(("FreeHandParser::readDataZone: find unknown zone\n"));
3268 if (zId)
3269 f << "Entries(DataZone)[Z" << zId << "]:";
3270 else
3271 f << "Entries(DataZone):";
3272 if (dSz!=dataSize+5)
3273 f << "sz?=" << dSz << ",";
3274 }
3275 input->seek(endPos, librevenge::RVNG_SEEK_SET);
3276 ascii().addPos(pos);
3277 ascii().addNote(f.str().c_str());
3278 return true;
3279 }
3280
3281 ////////////////////////////////////////////////////////////
3282 //
3283 // send data
3284 //
3285 ////////////////////////////////////////////////////////////
3286 bool FreeHandParser::openLayer(int zId)
3287 {
3288 if (zId<0 || m_state->m_actualLayer>=0 || m_state->m_sendLayerSet.find(zId)!=m_state->m_sendLayerSet.end())
3289 return false;
3290 if (!getGraphicListener()) {
3291 MWAW_DEBUG_MSG(("FreeHandParser::openLayer: can not find the listener\n"));
3292 return false;
3293 }
3294 m_state->m_sendLayerSet.insert(zId);
3295 librevenge::RVNGString layer;
3296 layer.sprintf("%d", zId);
3297 if (!getGraphicListener()->openLayer(layer))
3298 return false;
3299 m_state->m_actualLayer=zId;
3300 return true;
3301 }
3302
3303 void FreeHandParser::closeLayer()
3304 {
3305 if (m_state->m_actualLayer<0)
3306 return;
3307 getGraphicListener()->closeLayer();
3308 m_state->m_actualLayer=-1;
3309 }
3310
3311 bool FreeHandParser::sendZone(int zId, MWAWTransformation const &transform)
3312 {
3313 if (!getGraphicListener()) {
3314 MWAW_DEBUG_MSG(("FreeHandParser::sendZone: can not find the listener\n"));
3315 return false;
3316 }
3317 if (m_state->m_zIdToTextboxMap.find(zId)!=m_state->m_zIdToTextboxMap.end())
3318 return sendTextbox(m_state->m_zIdToTextboxMap.find(zId)->second, transform);
3319 if (m_state->m_zIdToShapeMap.find(zId)==m_state->m_zIdToShapeMap.end()) {
3320 MWAW_DEBUG_MSG(("FreeHandParser::sendZone: can not find the zone %d\n", zId));
3321 return false;
3322 }
3323 auto const &shape=m_state->m_zIdToShapeMap.find(zId)->second;
3324 shape.m_isSent=true;
3325 if (shape.m_type==FreeHandParserInternal::Shape::Group ||
3326 shape.m_type==FreeHandParserInternal::Shape::JoinGroup)
3327 return sendGroup(shape, transform);
3328 if (shape.m_type==FreeHandParserInternal::Shape::Picture)
3329 return sendPicture(shape, transform);
3330 if (shape.m_type==FreeHandParserInternal::Shape::BackgroundPicture)
3331 return sendBackgroundPicture(shape, transform);
3332 if (shape.m_type!=FreeHandParserInternal::Shape::Unknown)
3333 return sendShape(shape, transform);
3334 return false;
3335 }
3336
3337 bool FreeHandParser::sendGroup(FreeHandParserInternal::Shape const &group, MWAWTransformation const &transform)
3338 {
3339 MWAWGraphicListenerPtr listener=getGraphicListener();
3340 if (!listener) {
3341 MWAW_DEBUG_MSG(("FreeHandParser::sendGroup: can not find the listener\n"));
3342 return false;
3343 }
3344 if (group.m_childs.empty()) return true;
3345 if (m_state->m_sendIdSet.find(group.m_id)!=m_state->m_sendIdSet.end()) {
3346 MWAW_DEBUG_MSG(("FreeHandParser::sendGroup: sorry the zone %d is already sent\n", group.m_id));
3347 return false;
3348 }
3349 m_state->m_sendIdSet.insert(group.m_id);
3350 MWAWTransformation transf=transform*group.m_transformation;
3351 bool createGroup=group.m_childs.size()>1 && group.m_id!=m_state->m_mainGroupId;
3352 // TODO check for join group
3353 bool newLayer=openLayer(group.m_layerId);
3354 if (createGroup) {
3355 MWAWPosition pos(MWAWVec2f(0,0), MWAWVec2f(0,0), librevenge::RVNG_POINT);
3356 pos.m_anchorTo = MWAWPosition::Page;
3357 listener->openGroup(pos);
3358 }
3359 bool checkLayer=m_state->m_actualLayer==-1;
3360 int actualLayerId=-1;
3361 for (int cId : group.m_childs) {
3362 if (checkLayer) {
3363 int newLayerId=-1;
3364 if (m_state->m_zIdToTextboxMap.find(cId)!=m_state->m_zIdToTextboxMap.end())
3365 newLayerId=m_state->m_zIdToTextboxMap.find(cId)->second.m_layerId;
3366 else if (m_state->m_zIdToShapeMap.find(cId)!=m_state->m_zIdToShapeMap.end())
3367 newLayerId=m_state->m_zIdToShapeMap.find(cId)->second.m_layerId;
3368 if (newLayerId!=actualLayerId) {
3369 if (actualLayerId>=0)
3370 closeLayer();
3371 if (openLayer(newLayerId))
3372 actualLayerId=newLayerId;
3373 else
3374 actualLayerId=-1;
3375 }
3376 }
3377 sendZone(cId, transf);
3378 }
3379 if (actualLayerId>=0) closeLayer();
3380 if (createGroup)
3381 listener->closeGroup();
3382 m_state->m_sendIdSet.erase(group.m_id);
3383 if (newLayer) closeLayer();
3384 return true;
3385 }
3386
3387 bool FreeHandParser::sendBackgroundPicture(FreeHandParserInternal::Shape const &picture, MWAWTransformation const &)
3388 {
3389 MWAWGraphicListenerPtr listener=getGraphicListener();
3390 if (!listener) {
3391 MWAW_DEBUG_MSG(("FreeHandParser::sendBackgroundPicture: can not find the listener\n"));
3392 return false;
3393 }
3394 if (!picture.m_picture.valid()) {
3395 MWAW_DEBUG_MSG(("FreeHandParser::sendBackgroundPicture: can not find the background picture\n"));
3396 return false;
3397 }
3398 MWAWInputStreamPtr input=getInput();
3399 input->seek(picture.m_picture.begin(), librevenge::RVNG_SEEK_SET);
3400 librevenge::RVNGBinaryData data;
3401 if (!input->readDataBlock(picture.m_picture.length(), data) || data.empty()) {
3402 MWAW_DEBUG_MSG(("FreeHandParser::sendBackgroundPicture: oops the picture is empty\n"));
3403 return false;
3404 }
3405 #ifdef DEBUG_WITH_FILES
3406 static int volatile pictName = 0;
3407 libmwaw::DebugStream f;
3408 f << "PICT-" << ++pictName << ".pct";
3409 libmwaw::Debug::dumpFile(data, f.str().c_str());
3410 #endif
3411 MWAWPosition pos(MWAWVec2f(float(getPageSpan().getMarginLeft()), float(getPageSpan().getMarginTop())),
3412 MWAWVec2f(float(getPageSpan().getPageWidth()), float(getPageSpan().getPageLength())), librevenge::RVNG_INCH);
3413 pos.m_anchorTo = MWAWPosition::Page;
3414 pos.setOrder(-1);
3415 MWAWEmbeddedObject pict(data);
3416 listener->insertPicture(pos, pict);
3417 return true;
3418 }
3419
3420 bool FreeHandParser::sendPicture(FreeHandParserInternal::Shape const &picture, MWAWTransformation const &transform)
3421 {
3422 MWAWGraphicListenerPtr listener=getGraphicListener();
3423 if (!listener) {
3424 MWAW_DEBUG_MSG(("FreeHandParser::sendPicture: can not find the listener\n"));
3425 return false;
3426 }
3427 if (m_state->m_zIdToDataMap.find(picture.m_dataId)==m_state->m_zIdToDataMap.end() ||
3428 !m_state->m_zIdToDataMap.find(picture.m_dataId)->second.valid()) {
3429 MWAW_DEBUG_MSG(("FreeHandParser::sendPicture: can not find the picture\n"));
3430 return false;
3431 }
3432 MWAWInputStreamPtr input=getInput();
3433 MWAWEntry entry=m_state->m_zIdToDataMap.find(picture.m_dataId)->second;
3434 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
3435 librevenge::RVNGBinaryData data;
3436 if (!input->readDataBlock(entry.length(), data) || data.empty()) {
3437 MWAW_DEBUG_MSG(("FreeHandParser::sendPicture: oops the picture is empty\n"));
3438 return false;
3439 }
3440 #ifdef DEBUG_WITH_FILES
3441 static int volatile pictName = 0;
3442 libmwaw::DebugStream f;
3443 f << "PICT-" << ++pictName << ".pct";
3444 libmwaw::Debug::dumpFile(data, f.str().c_str());
3445 #endif
3446 MWAWGraphicStyle style=MWAWGraphicStyle::emptyStyle();
3447 MWAWTransformation finalTransformation=transform*picture.m_transformation;
3448 MWAWTransformation transf;
3449 float rotation=0;
3450 MWAWBox2f box;
3451 if (decomposeMatrix(finalTransformation,rotation,transf,picture.m_box.center())) {
3452 box=transf*picture.m_box;
3453 style.m_rotate=rotation;
3454 }
3455 else
3456 box=finalTransformation*picture.m_box;
3457 for (int c=0; c<2; ++c) {
3458 if (box.min()[c]>box.max()[c])
3459 std::swap(box.min()[c],box.max()[c]);
3460 }
3461 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
3462 pos.m_anchorTo = MWAWPosition::Page;
3463 MWAWEmbeddedObject pict(data);
3464 listener->insertPicture(pos, pict, style);
3465 return true;
3466 }
3467
3468 bool FreeHandParser::sendShape(FreeHandParserInternal::Shape const &shape, MWAWTransformation const &transform)
3469 {
3470 MWAWGraphicListenerPtr listener=getGraphicListener();
3471 if (!listener) {
3472 MWAW_DEBUG_MSG(("FreeHandParser::sendShape: can not find the listener\n"));
3473 return false;
3474 }
3475 MWAWGraphicStyle style;
3476 m_state->updateLineStyle(shape.m_lineId, style);
3477 if (shape.m_type!=FreeHandParserInternal::Shape::Line &&
3478 (shape.m_type!=FreeHandParserInternal::Shape::Path || shape.m_closed))
3479 m_state->updateFillStyle(shape.m_fillId, style);
3480 MWAWTransformation finalTransformation=transform*shape.m_transformation;
3481 MWAWGraphicShape res;
3482 if (shape.updateShape(res)) {
3483 res=res.transform(finalTransformation);
3484 MWAWPosition pos(res.m_bdBox[0], res.m_bdBox.size(), librevenge::RVNG_POINT);
3485 pos.m_anchorTo = MWAWPosition::Page;
3486 listener->insertShape(pos, res, style);
3487 return true;
3488 }
3489 MWAW_DEBUG_MSG(("FreeHandParser::sendShape: found some unexpected shape\n"));
3490 return false;
3491 }
3492
3493 bool FreeHandParser::sendTextbox(FreeHandParserInternal::Textbox const &textbox, MWAWTransformation const &transform)
3494 {
3495 textbox.m_isSent=true;
3496 MWAWGraphicListenerPtr listener=getGraphicListener();
3497 if (!listener) {
3498 MWAW_DEBUG_MSG(("FreeHandParser::sendTextbox: can not find the listener\n"));
3499 return false;
3500 }
3501 MWAWGraphicStyle style(MWAWGraphicStyle::emptyStyle());
3502 MWAWTransformation finalTransformation=transform*textbox.m_transformation;
3503 MWAWTransformation transf;
3504 float rotation=0;
3505 MWAWBox2f box;
3506 if (decomposeMatrix(finalTransformation,rotation,transf,textbox.m_box.center())) {
3507 box=transf*textbox.m_box;
3508 style.m_rotate=rotation;
3509 }
3510 else
3511 box=finalTransformation*textbox.m_box;
3512 for (int c=0; c<2; ++c) {
3513 if (box.min()[c]>box.max()[c])
3514 std::swap(box.min()[c],box.max()[c]);
3515 }
3516 MWAWPosition pos(box[0], box.size(), librevenge::RVNG_POINT);
3517 pos.m_anchorTo = MWAWPosition::Page;
3518 std::shared_ptr<MWAWSubDocument> doc(new FreeHandParserInternal::SubDocument(*this, getInput(), textbox.m_id));
3519 listener->insertTextBox(pos, doc, style);
3520 return false;
3521 }
3522
3523 bool FreeHandParser::sendText(int zId)
3524 {
3525 MWAWGraphicListenerPtr listener=getGraphicListener();
3526 if (!listener) {
3527 MWAW_DEBUG_MSG(("FreeHandParser::sendText: can not find the listener\n"));
3528 return false;
3529 }
3530 if (m_state->m_zIdToTextboxMap.find(zId)==m_state->m_zIdToTextboxMap.end()) {
3531 MWAW_DEBUG_MSG(("FreeHandParser::sendText: can not find the text shape\n"));
3532 return false;
3533 }
3534 auto &textbox=m_state->m_zIdToTextboxMap.find(zId)->second;
3535 MWAWParagraph para;
3536 para.m_justify = textbox.m_justify;
3537 listener->setParagraph(para);
3538 if (!textbox.m_text.valid())
3539 return true;
3540 MWAWInputStreamPtr input=getInput();
3541 input->seek(textbox.m_text.begin(), librevenge::RVNG_SEEK_SET);
3542
3543 libmwaw::DebugStream f;
3544 f << "Textbox[text]:";
3545 long endPos=textbox.m_text.end();
3546 int cPos=0;
3547 float deltaSpacing=textbox.m_spacings[0];
3548 while (!input->isEnd()) {
3549 if (input->tell()>=endPos)
3550 break;
3551 if (textbox.m_posToFontMap.find(cPos)!=textbox.m_posToFontMap.end()) {
3552 auto &font=textbox.m_posToFontMap.find(cPos)->second;
3553 if (font.m_nameId && m_state->m_zIdToStringMap.find(font.m_nameId)!= m_state->m_zIdToStringMap.end())
3554 font.m_font.setId(getParserState()->m_fontConverter->getId(m_state->m_zIdToStringMap.find(font.m_nameId)->second));
3555 // color
3556 if (font.m_colorId && m_state->m_zIdToColorMap.find(font.m_colorId) != m_state->m_zIdToColorMap.end())
3557 font.m_font.setColor(m_state->m_zIdToColorMap.find(font.m_colorId)->second);
3558 // spacing
3559 font.m_font.setDeltaLetterSpacing(deltaSpacing, librevenge::RVNG_POINT);
3560 // streching
3561 bool needStreching=false;
3562 if (textbox.m_scalings[1]<1||textbox.m_scalings[1]>1) {
3563 font.m_font.setSize(font.m_font.size()*textbox.m_scalings[1]);
3564 needStreching=true;
3565 }
3566 if ((needStreching || textbox.m_scalings[0]<1 || textbox.m_scalings[0]>1) && textbox.m_scalings[1]>0)
3567 font.m_font.setWidthStreching(textbox.m_scalings[0]/textbox.m_scalings[1]);
3568 listener->setFont(font.m_font);
3569 f << "[F]";
3570 }
3571 ++cPos;
3572 auto c = char(input->readULong(1));
3573 if (c==0) {
3574 MWAW_DEBUG_MSG(("FreeHandParser::sendText: find char 0\n"));
3575 f << "#[0]";
3576 continue;
3577 }
3578 f << c;
3579 switch (c) {
3580 case 9:
3581 listener->insertTab();
3582 break;
3583 case 0xd:
3584 listener->insertEOL();
3585 break;
3586 default:
3587 listener->insertCharacter(static_cast<unsigned char>(c), input, endPos);
3588 break;
3589 }
3590 }
3591 ascii().addPos(textbox.m_text.begin());
3592 ascii().addNote(f.str().c_str());
3593
3594 return true;
3595 }
3596
3597 void FreeHandParser::flushExtra()
3598 {
3599 bool first=true;
3600 for (auto const &it : m_state->m_zIdToShapeMap) {
3601 if (it.second.m_isSent) continue;
3602 if (first) {
3603 MWAW_DEBUG_MSG(("FreeHandParser::flushExtra: find some unused shape: %d\n", it.first));
3604 first=false;
3605 }
3606 sendZone(it.first, m_state->m_transform);
3607 }
3608 first=true;
3609 for (auto const &it : m_state->m_zIdToTextboxMap) {
3610 if (it.second.m_isSent) continue;
3611 if (first) {
3612 MWAW_DEBUG_MSG(("FreeHandParser::flushExtra: find some unsed textbox %d\n", it.first));
3613 first=false;
3614 }
3615 sendZone(it.first, m_state->m_transform);
3616 }
3617 }
3618
3619 bool FreeHandParser::decomposeMatrix(MWAWTransformation const &matrix, float &rot, MWAWTransformation &transform, MWAWVec2f const &origCenter)
3620 {
3621 // FIXME: this assumes that there is no skewing, ...
3622 MWAWVec3f const &yRow=matrix[1];
3623 if (matrix.isIdentity() || (yRow[0]>=0 && yRow[0]<=0)) return false;
3624 rot=std::atan2(yRow[0],-yRow[1]);
3625 rot *= float(180/M_PI);
3626 transform=MWAWTransformation::rotation(-rot, matrix * origCenter) * matrix;
3627 return true;
3628 }
3629
3630 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: