Debian Codegraph @

src/lib/Canvas5StyleManager.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <cmath>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <map>
39 #include <set>
40 #include <sstream>
41 #include <stack>
42
43 #include <librevenge/librevenge.h>
44
45 #include "MWAWFont.hxx"
46 #include "MWAWFontConverter.hxx"
47 #include "MWAWGraphicListener.hxx"
48 #include "MWAWGraphicStyle.hxx"
49 #include "MWAWParagraph.hxx"
50 #include "MWAWParser.hxx"
51
52 #include "Canvas5Image.hxx"
53 #include "Canvas5Parser.hxx"
54 #include "Canvas5Structure.hxx"
55
56 #include "Canvas5StyleManager.hxx"
57
58 #include "libmwaw_internal.hxx"
59
60 /** Internal: the structures of a Canvas5StyleManager */
61 namespace Canvas5StyleManagerInternal
62 {
63
64 ////////////////////////////////////////
65 //! Internal: the color style of a Canvas5StyleManager
66 struct ColorStyle {
67 //! constructor
68 ColorStyle()
69 : m_type(1)
70 , m_nameId(0)
71 , m_color()
72 , m_gradient()
73 , m_hatch()
74 , m_textureDim(0,0)
75 , m_texture()
76 {
77 }
78 friend std::ostream &operator<<(std::ostream &o, ColorStyle const &color)
79 {
80 if (color.m_type==0)
81 o << "transparent,";
82 else if (color.m_type==1 && color.m_color.isSet())
83 o << *color.m_color << ",";
84 else
85 o << "type=" << Canvas5Structure::getString(color.m_type) << ",";
86 if (color.m_nameId)
87 o << "N" << color.m_nameId << ",";
88 return o;
89 }
90 //! the type
91 unsigned m_type;
92 //! the name id
93 int m_nameId;
94 //! the basic color
95 MWAWVariable<MWAWColor> m_color;
96 //! the gradient
97 MWAWGraphicStyle::Gradient m_gradient;
98 //! the hatch
99 MWAWGraphicStyle::Hatch m_hatch;
100 //! the texture dimension
101 MWAWVec2i m_textureDim;
102 //! the embedded objet (texture)
103 MWAWEmbeddedObject m_texture;
104 };
105
106 ////////////////////////////////////////
107 //! Internal: the pen style of a Canvas5StyleManager
108 struct PenStyle {
109 /// a line of a Canvas5StyleManager pen style
110 struct Line {
111 //! constructor
112 Line()
113 : m_size(1,1)
114 , m_offset(0)
115 , m_color(MWAWColor::black())
116 {
117 }
118 //! the line width
119 MWAWVec2f m_size;
120 //! the offset
121 float m_offset;
122 //! the line color
123 MWAWVariable<MWAWColor> m_color;
124 };
125 //! constructor
126 PenStyle()
127 : m_type(1)
128 , m_size(1,1)
129 , m_usePenColor(true)
130 , m_lines()
131 {
132 for (auto &c : m_colors) c=MWAWColor::black();
133 }
134 friend std::ostream &operator<<(std::ostream &o, PenStyle const &pen)
135 {
136 if (pen.m_type!=1)
137 o << "type=" << Canvas5Structure::getString(pen.m_type) << ",";
138 if (pen.m_size!=MWAWVec2f(1,1))
139 o << "size=" << pen.m_size << ",";
140 return o;
141 }
142 //! the type
143 unsigned m_type;
144 //! the pen size
145 MWAWVec2f m_size;
146 //! the neo color
147 MWAWVariable<MWAWColor> m_colors[2];
148 //! use pen ink
149 bool m_usePenColor;
150 //! the plin lines
151 std::vector<Line> m_lines;
152 };
153
154 ////////////////////////////////////////
155 //! Internal: the stroke style of a Canvas5StyleManager
156 struct Stroke {
157 //! constructor
158 Stroke()
159 : m_type(1)
160 , m_penId(0)
161 , m_dashId(0)
162 {
163 for (auto &id : m_arrowId) id=0;
164 }
165 friend std::ostream &operator<<(std::ostream &o, Stroke const &stroke)
166 {
167 if (stroke.m_type!=1)
168 o << "type=" << Canvas5Structure::getString(stroke.m_type) << ",";
169 if (stroke.m_penId)
170 o << "Pe" << stroke.m_penId << ",";
171 if (stroke.m_dashId)
172 o << "Da" << stroke.m_dashId << ",";
173 for (int i=0; i<2; ++i) {
174 if (!stroke.m_arrowId[i]) continue;
175 o << (i==0 ? "beg" : "end") << "=Ar" << stroke.m_arrowId[i] << ",";
176 }
177 return o;
178 }
179 //! the type
180 unsigned m_type;
181 //! the pen id
182 int m_penId;
183 //! the dash id
184 int m_dashId;
185 //! the arrow id (beg/end)
186 int m_arrowId[2];
187 };
188
189 ////////////////////////////////////////
190 //! Internal: the state of a Canvas5StyleManager
191 struct State {
192 //! constructor
193 State()
194 : m_idToArrow()
195 , m_idToColor()
196 , m_idToPen()
197 , m_idToDash()
198 , m_idToStroke()
199 {
200 }
201
202 //! the id to arrow map
203 std::map<int, MWAWGraphicStyle::Arrow> m_idToArrow;
204 //! the id to color style map
205 std::map<int, std::shared_ptr<ColorStyle> > m_idToColor;
206 //! the id to pen style map
207 std::map<int, std::shared_ptr<PenStyle> > m_idToPen;
208 //! the id to dash map
209 std::map<int, std::vector<float> > m_idToDash;
210 //! the id to stroke style map
211 std::map<int, Stroke> m_idToStroke;
212 };
213
214 }
215
216 ////////////////////////////////////////////////////////////
217 // constructor/destructor, ...
218 ////////////////////////////////////////////////////////////
219 Canvas5StyleManager::Canvas5StyleManager(Canvas5Parser &parser)
220 : m_parserState(parser.getParserState())
221 , m_state(new Canvas5StyleManagerInternal::State)
222 , m_mainParser(&parser)
223 {
224 }
225
226 Canvas5StyleManager::~Canvas5StyleManager()
227 { }
228
229 int Canvas5StyleManager::version() const
230 {
231 return m_parserState->m_version;
232 }
233
234 std::shared_ptr<Canvas5Image> Canvas5StyleManager::getImageParser() const
235 {
236 return m_mainParser->m_imageParser;
237 }
238
239 ////////////////////////////////////////////////////////////
240 //
241 // Intermediate level
242 //
243 ////////////////////////////////////////////////////////////
244
245 bool Canvas5StyleManager::readColor(Canvas5Structure::Stream &stream, MWAWVariable<MWAWColor> &color, std::string &extra)
246 {
247 color.setSet(false);
248
249 auto input=stream.input();
250 long pos=input->tell();
251 extra="";
252 if (!input->checkPosition(pos+24)) {
253 MWAW_DEBUG_MSG(("Canvas5StyleManager::readColor: file is to short\n"));
254 return false;
255 }
256 libmwaw::DebugStream f;
257 for (int i=0; i<6; ++i) {
258 int val=(i>0 && i<5) ? int(input->readULong(2)) : int(input->readLong(2));
259 if (val==(i==5 ? -1 : 0)) continue;
260 if (i>0 && i<5)
261 f << "f" << i << "=" << std::hex << val << std::dec << ",";
262 else
263 f << "f" << i << "=" << val << ",";
264 }
265 unsigned char cols[4];
266 for (auto &c : cols) c=(unsigned char)(input->readULong(2)>>8);
267 // cmyk, gray, rgb , sepp, pton, trum, toyo
268 unsigned name=unsigned(input->readULong(4));
269 f << Canvas5Structure::getString(name) << ",";
270 if (name==0x67726179) // gray
271 color=MWAWColor(cols[0],cols[0],cols[0]);
272 else if (name==0x72676220) // rgb
273 color=MWAWColor(cols[0],cols[1],cols[2],(unsigned char)(255-cols[3]));
274 else {
275 if (name==0x70746f6e) { // pton
276 f << "##";
277 static bool first=true;
278 if (first) {
279 MWAW_DEBUG_MSG(("Canvas5StyleManager::readColor: this file contains pantom color, there will ne be converted correctly\n"));
280 first=false;
281 }
282 }
283 color=MWAWColor::colorFromCMYK(cols[0],cols[1],cols[2],cols[3]);
284 }
285 f << *color << ",";
286 extra=f.str();
287 return true;
288 }
289
290 bool Canvas5StyleManager::readGradient(std::shared_ptr<Canvas5Structure::Stream> stream, long len,
291 MWAWGraphicStyle::Gradient &gradient)
292 {
293 if (!stream || !stream->input())
294 return false;
295 auto input=stream->input();
296 long pos=input->tell();
297 libmwaw::DebugFile &ascFile = stream->ascii();
298 libmwaw::DebugStream f;
299
300 f << "Entries(ObFl):";
301 int vers=version();
302 long headerLength=vers==5 ? 56 : vers<9 ? 80+0x300 : 912;
303 unsigned dataSize=vers==5 ? 28 : 60;
304 if (len<headerLength || !input->checkPosition(pos+len)) {
305 if (vers>5 && input->checkPosition(pos+len) && len>=56) {
306 // find some v5 gradient in v6 files converted from v5, so let's us try to revert to v5
307 MWAW_DEBUG_MSG(("Canvas5StyleManager::readGradient: this does not look as a v6 gradient, try to read a v5 gradient\n"));
308 f << "#v5,";
309 vers=5;
310 headerLength=56;
311 dataSize=28;
312 }
313 else {
314 MWAW_DEBUG_MSG(("Canvas5StyleManager::readGradient: unexpected length\n"));
315 f << "###";
316 ascFile.addPos(pos);
317 ascFile.addNote(f.str().c_str());
318 return false;
319 }
320 }
321
322 int type;
323 size_t N;
324 if (vers==5) {
325 N=size_t(input->readULong(4));
326 f << "N=" << N << ",";
327 type=int(input->readLong(4));
328 f << "pts=[";
329 MWAWVec2f pts[3];
330 for (auto &pt : pts) {
331 float fDim[2];
332 for (auto &d : fDim) d=float(input->readLong(4))/65536;
333 pt=MWAWVec2f(fDim[1],fDim[0]);
334 f << pt << ",";
335 }
336 f << "],";
337 if (pts[0]!=pts[1]) {
338 MWAWVec2f dir=pts[1]-pts[0];
339 gradient.m_angle=90-180*std::atan2(dir[1],dir[0])/float(M_PI);
340 }
341 float fDim[4];
342 for (auto &d : fDim) d=float(input->readLong(4))/65536;
343 MWAWBox2f box=MWAWBox2f(MWAWVec2f(fDim[1],fDim[0]),MWAWVec2f(fDim[3],fDim[2]));
344 gradient.m_percentCenter=box.center();
345 f << "box=" << box << ",";
346 int val=int(input->readULong(4));
347 if (val==1)
348 f << "rainbow,";
349 else if (val) {
350 MWAW_DEBUG_MSG(("Canvas5StyleManager::readGradient: find unknown rainbow type\n"));
351 f << "##rainbow=" << val << ",";
352 }
353 val=int(input->readULong(2));
354 if (val==1)
355 f << "rainbow[inverted],";
356 else if (val)
357 f << "#rainbow[inverted]=" << val << ",";
358 val=int(input->readULong(1));
359 if (val)
360 f << "h0=" << val << ",";
361 val=int(input->readULong(1));
362 if (val!=0x13)
363 f << "h1=" << val << ",";
364 }
365 else {
366 for (int i=0; i<2; ++i) {
367 int val=int(input->readLong(4));
368 int const expected[]= {vers<9 ? 0xdf : 0xfa, vers<9 ? 0x600 : 0x900};
369 if (val!=expected[i])
370 f << "f" << i << "=" << val << ",";
371 }
372 type=int(input->readLong(4));
373 auto fl=input->readULong(4);
374 if (fl&1)
375 f << "rainbow,";
376 if ((fl&0x100)==0)
377 f << "rainbow[inverted],";
378 fl&=0xFFFFFEFE;
379 if (fl!=0x1000)
380 f << "fl=" << std::hex << fl << std::dec << ",";
381 MWAWVec2f pts[2];
382 for (auto &pt : pts) {
383 float coords[2];
384 for (auto &c : coords) c=float(m_mainParser->readDouble(*stream,vers<9 ? 4 : 8));
385 pt=MWAWVec2f(coords[1],coords[0]);
386 }
387 if (type<=2) {
388 MWAWVec2f dir=pts[1]-pts[0];
389 gradient.m_angle=90-180*std::atan2(dir[1],dir[0])/float(M_PI);
390 if (std::isnan(gradient.m_angle)) {
391 MWAW_DEBUG_MSG(("Canvas5StyleManager::readGradient: can not compute the gradient angle\n"));
392 f << "###angle,";
393 gradient.m_angle=0;
394 }
395 else if (gradient.m_angle<0 || gradient.m_angle>0)
396 f << "angle=" << gradient.m_angle << ",";
397 }
398 else {
399 MWAWBox2f box=MWAWBox2f(pts[0], pts[1]);
400 gradient.m_percentCenter=box.center();
401 f << "box=" << box << ",";
402 }
403 N=unsigned(input->readULong(4));
404 f << "N=" << N << ",";
405 ascFile.addDelimiter(input->tell(),'|');
406 ascFile.addPos(input->tell()+44);
407 ascFile.addNote("ObFl[unkn]:");
408 }
409 if (long(N)<0 || (len-headerLength)/long(dataSize)<long(N) || headerLength+long(N)*long(dataSize)<headerLength || len<headerLength+long(N*dataSize)) {
410 MWAW_DEBUG_MSG(("Canvas5StyleManager::readGradient: can not read N\n"));
411 f << "###";
412 ascFile.addPos(pos);
413 ascFile.addNote(f.str().c_str());
414 return false;
415 }
416 if (type>=1 && type<=5) {
417 char const *wh[]= {"radial", "directional", "shape", "rectangular",
418 "elliptical"
419 };
420 MWAWGraphicStyle::Gradient::Type const types[]= {
421 MWAWGraphicStyle::Gradient::G_Radial, MWAWGraphicStyle::Gradient::G_Linear,
422 MWAWGraphicStyle::Gradient::G_Rectangular, MWAWGraphicStyle::Gradient::G_Rectangular,
423 MWAWGraphicStyle::Gradient::G_Ellipsoid
424 };
425 gradient.m_type=types[type-1];
426 f << wh[type-1] << ",";
427 }
428 else {
429 MWAW_DEBUG_MSG(("Canvas5StyleManager::readGradient: find unknown type\n"));
430 f << "###type=" << type << ",";
431 }
432 ascFile.addPos(pos);
433 ascFile.addNote(f.str().c_str());
434
435 input->seek(pos+headerLength, librevenge::RVNG_SEEK_SET);
436 gradient.m_stopList.resize(size_t(N));
437 for (size_t i=0; i<size_t(N); ++i) {
438 long actPos=input->tell();
439 f.str("");
440 f << "ObFl[stop" << i << "]:";
441 MWAWGraphicStyle::Gradient::Stop &stop=gradient.m_stopList[size_t(N)-1-i];
442 stop.m_offset=1-float(input->readLong(4))/100;
443 f << "pos=" << stop.m_offset << ",";
444 std::string extra;
445 MWAWVariable<MWAWColor> stopColor;
446 if (!readColor(*stream, stopColor, extra))
447 input->seek(actPos+4+24, librevenge::RVNG_SEEK_SET);
448 else if (stopColor.isSet())
449 stop.m_color=*stopColor;
450 f << extra;
451 if (vers>5) {
452 std::string name;
453 for (int j=0; j<32; ++j) {
454 char c=char(input->readULong(1));
455 if (!c)
456 break;
457 name+=c;
458 }
459 f << name << ",";
460 input->seek(actPos+4+24+32, librevenge::RVNG_SEEK_SET);
461 }
462 ascFile.addPos(actPos);
463 ascFile.addNote(f.str().c_str());
464 }
465 return true;
466 }
467
468 bool Canvas5StyleManager::readHatch(std::shared_ptr<Canvas5Structure::Stream> stream, long len,
469 MWAWGraphicStyle::Hatch &hatch, MWAWVariable<MWAWColor> &backColor)
470 {
471 hatch=MWAWGraphicStyle::Hatch();
472 if (!stream || !stream->input())
473 return false;
474 auto input=stream->input();
475 long pos=input->tell();
476 libmwaw::DebugFile &ascFile = stream->ascii();
477 libmwaw::DebugStream f;
478 int const vers=version();
479 int headerSz=vers<9 ? 8 : 12;
480 int const dataSz=vers<9 ? 104 : 192;
481 f << "Entries(Hatch):";
482 if (len<headerSz+dataSz) {
483 MWAW_DEBUG_MSG(("Canvas5StyleManager::readHatch: unexpected length\n"));
484 f << "###";
485 ascFile.addPos(pos);
486 ascFile.addNote(f.str().c_str());
487 return false;
488 }
489 unsigned sz=unsigned(input->readULong(4));
490 f << "first[sz]=" << sz << ",";
491 int N=int(input->readULong(2));
492 f << "N=" << N << ",";
493 if (int(sz)<0 || headerSz+long(sz)>len || (len-long(sz)-headerSz)/dataSz<N ||
494 headerSz+int(sz+unsigned(dataSz)*unsigned(N))<headerSz+dataSz ||
495 len<headerSz+long(sz+unsigned(dataSz)*unsigned(N))) {
496 f << "###";
497 MWAW_DEBUG_MSG(("Canvas5StyleManager::readHatch: the number of line seems bad\n"));
498 ascFile.addPos(pos);
499 ascFile.addNote(f.str().c_str());
500 return false;
501 }
502 int val;
503 if (vers<9) {
504 val=int(input->readULong(2));
505 if (val!=0xf6f6) f << "fl=" << std::hex << val << std::dec << ",";
506 }
507 else {
508 for (int i=0; i<3; ++i) { // f2=0|1
509 val=int(input->readLong(2));
510 if (!val) continue;
511 f << "f" << i << "=" << val << ",";
512 }
513 }
514 ascFile.addPos(pos);
515 ascFile.addNote(f.str().c_str());
516
517 if (sz) {
518 pos=input->tell();
519 f.str("");
520 f << "Hatch[color,back]:";
521 unsigned type=unsigned(input->readULong(4));
522 if (sz>4) {
523 auto bgColor=readColorStyle(stream, type, sz-4);
524 if (!bgColor)
525 f << "###";
526 else {
527 backColor=bgColor->m_color;
528 f << *bgColor;
529 ascFile.addPos(pos+4);
530 ascFile.addNote(f.str().c_str());
531 }
532 }
533 else if (type!=0)
534 f << "###";
535 ascFile.addPos(pos);
536 ascFile.addNote(f.str().c_str());
537 input->seek(pos+sz, librevenge::RVNG_SEEK_SET);
538 }
539 if (N<=0)
540 return true;
541
542 hatch.m_type= N==1 ? MWAWGraphicStyle::Hatch::H_Single :
543 N==2 ? MWAWGraphicStyle::Hatch::H_Double : MWAWGraphicStyle::Hatch::H_Triple;
544 float offset=0;
545 for (int i=0; i<N; ++i) {
546 pos=input->tell();
547 f.str("");
548 f << "Hatch-L" << i << ":";
549 double w=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
550 if (w<0 || w>0)
551 f << "w=" << w << ",";
552 double angle=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
553 if (angle<0 || angle>0)
554 f << "angle=" << angle << ",";
555 if (i==0)
556 hatch.m_rotation=90-float(angle);
557 double offs=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
558 offset+=float(offs);
559 f << "offset=" << offs << ",";
560 double orig=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
561 if (orig<0 || orig>0)
562 f << "orig=" << orig << ",";
563 ascFile.addDelimiter(input->tell(),'|');
564 input->seek(pos+dataSz-24, librevenge::RVNG_SEEK_SET);
565 ascFile.addPos(pos);
566 ascFile.addNote(f.str().c_str());
567
568 pos=input->tell();
569 f.str("");
570 f << "Hatch-C" << i << ":";
571 std::string extra;
572 MWAWVariable<MWAWColor> col;
573 if (!readColor(*stream, col, extra))
574 input->seek(pos+24, librevenge::RVNG_SEEK_SET);
575 else if (col.isSet())
576 hatch.m_color=*col;
577 f << extra;
578 ascFile.addPos(pos);
579 ascFile.addNote(f.str().c_str());
580 }
581 hatch.m_distance=offset/float(N)/72;
582 return true;
583 }
584
585 bool Canvas5StyleManager::readArrows(std::shared_ptr<Canvas5Structure::Stream> stream)
586 {
587 if (!stream) {
588 MWAW_DEBUG_MSG(("Canvas5StyleManager::readArrows: no stream\n"));
589 return false;
590 }
591 auto input=stream->input();
592 long pos=input->tell();
593
594 libmwaw::DebugFile &ascFile = stream->ascii();
595 libmwaw::DebugStream f;
596 f << "Entries(Arrow):";
597 ascFile.addPos(pos);
598 ascFile.addNote(f.str().c_str());
599 if (!m_mainParser->readUsed(*stream, "Arrow")) // checkme: probably a type
600 return false;
601
602 if (!m_mainParser->readIndexMap(stream, "Arrow",
603 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
604 auto lInput=lStream->input();
605 libmwaw::DebugStream lF;
606 auto &asciiFile=lStream->ascii();
607 lF << "Arrow" << item.m_id << ",";
608 MWAWGraphicStyle::Arrow arrow;
609 if (!readArrow(lStream, arrow, 1, item.m_length))
610 lF << "###";
611 else
612 m_state->m_idToArrow[item.m_id]=arrow;
613 asciiFile.addPos(item.m_pos);
614 asciiFile.addNote(lF.str().c_str());
615 }))
616 return false;
617 return true;
618 }
619
620 bool Canvas5StyleManager::readArrow(std::shared_ptr<Canvas5Structure::Stream> stream, MWAWGraphicStyle::Arrow &arrow, unsigned, long len)
621 {
622 if (!stream) {
623 MWAW_DEBUG_MSG(("Canvas5StyleManager::readArrow: no stream\n"));
624 return false;
625 }
626 auto input=stream->input();
627 long pos=input->tell();
628 int const vers=version();
629 int const headerLen=vers<9 ? 24 : 88;
630 if (len<headerLen || !input->checkPosition(pos+headerLen)) {
631 MWAW_DEBUG_MSG(("Canvas5StyleManager::readArrow: file is to short\n"));
632 return false;
633 }
634 auto &ascFile=stream->ascii();
635 libmwaw::DebugStream f;
636 long dataLen=long(input->readULong(4));
637 if (dataLen==len) dataLen=len-headerLen; // can happen sometimes
638 if (pos+headerLen+dataLen<pos+headerLen || headerLen+dataLen>len) {
639 MWAW_DEBUG_MSG(("Canvas5StyleManager::readArrow: can not read the arrow's data size\n"));
640 return false;
641 }
642 ascFile.addDelimiter(input->tell(),'|');
643 ascFile.addPos(pos);
644 ascFile.addNote(f.str().c_str());
645 input->seek(pos+headerLen, librevenge::RVNG_SEEK_SET);
646
647 if (headerLen+dataLen!=len) {
648 MWAW_DEBUG_MSG(("Canvas5StyleManager::readArrow: find unknown length data\n"));
649 ascFile.addPos(pos+headerLen+dataLen);
650 ascFile.addNote("Arrow-End:###");
651 }
652
653 if (dataLen==0) {
654 arrow=MWAWGraphicStyle::Arrow();
655 return true;
656 }
657 std::shared_ptr<Canvas5ImageInternal::VKFLImage> image;
658 if (!getImageParser()->readVKFL(stream, dataLen, image) || !image
659 || !getImageParser()->getArrow(image, arrow))
660 arrow=MWAWGraphicStyle::Arrow::plain();
661
662 return true;
663 }
664
665 bool Canvas5StyleManager::readInks(std::shared_ptr<Canvas5Structure::Stream> stream)
666 {
667 if (!stream || !stream->input())
668 return false;
669 auto input=stream->input();
670 long pos=input->tell();
671
672 libmwaw::DebugFile &ascFile = stream->ascii();
673 libmwaw::DebugStream f;
674 f << "Entries(Color):type,";
675 ascFile.addPos(pos);
676 ascFile.addNote(f.str().c_str());
677
678 std::map<int, std::pair<unsigned, int> > idToTypeNameMap;
679 if (!m_mainParser->readExtendedHeader(stream, 8, "Color",
680 [&idToTypeNameMap](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
681 auto lInput=lStream->input();
682 libmwaw::DebugStream lF;
683 auto &asciiFile=lStream->ascii();
684 lF << "Co" << item.m_id << "[type]:";
685 Canvas5StyleManagerInternal::ColorStyle color;
686 unsigned type=unsigned(lInput->readULong(4)); // 1, TXUR: texture, ObFl, htch: hatch, vkfl: symbol
687 if (type!=1)
688 lF << "type=" << Canvas5Structure::getString(type) << ",";
689 int nameId=int(lInput->readLong(4));
690 if (nameId)
691 lF << "id[name]=" << nameId << ",";
692 idToTypeNameMap[item.m_id]=std::make_pair(type,nameId);
693 asciiFile.addPos(item.m_pos);
694 asciiFile.addNote(lF.str().c_str());
695 }))
696 return false;
697
698 if (!m_mainParser->readIndexMap(stream, "Color",
699 [this, &idToTypeNameMap](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
700 auto lInput=lStream->input();
701 libmwaw::DebugStream lF;
702 auto &asciiFile=lStream->ascii();
703 lF << "Co" << item.m_id << ",";
704 unsigned type=1;
705 int nameId=0;
706 auto const &it=idToTypeNameMap.find(item.m_id);
707 if (it!=idToTypeNameMap.end())
708 std::tie(type,nameId)=it->second;
709 auto color=readColorStyle(lStream, type, item.m_length);
710 if (color) {
711 color->m_nameId=nameId;
712 m_state->m_idToColor[item.m_id]=color;
713 }
714 else
715 lF << "###";
716 asciiFile.addPos(item.m_pos);
717 asciiFile.addNote(lF.str().c_str());
718 }))
719 return false;
720
721 if (!m_mainParser->readUsed(*stream, "Color"))
722 return false;
723
724 std::multimap<int,int> nameIdToColor;
725 for (auto &it : m_state->m_idToColor) {
726 if (it.second && it.second->m_nameId)
727 nameIdToColor.insert(std::make_pair(it.second->m_nameId, it.first));
728 }
729
730 pos=input->tell();
731 f.str("");
732 f << "Color:names";
733 int N;
734 if (!m_mainParser->readDataHeader(*stream, 4, N)) {
735 MWAW_DEBUG_MSG(("Canvas5StyleManager::readInks: can not read the last zone N\n"));
736 f << "###";
737 ascFile.addPos(pos);
738 ascFile.addNote(f.str().c_str());
739 return false;
740 }
741 f << "N=" << N << ",";
742 f << "id=[";
743 for (int i=0; i<N; ++i) f << input->readLong(4) << ",";
744 f << "],";
745 ascFile.addPos(pos);
746 ascFile.addNote(f.str().c_str());
747
748 return m_mainParser->readIndexMap(stream, "Color",
749 [&nameIdToColor](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
750 auto lInput=lStream->input();
751 libmwaw::DebugStream lF;
752 auto &asciiFile=lStream->ascii();
753 std::string name;
754 for (int i=0; i<int(item.m_length); ++i) {
755 char c=char(lInput->readULong(1));
756 if (c==0)
757 break;
758 name+=c;
759 }
760 lF << name << ",";
761 auto it = nameIdToColor.find(item.m_id);
762 lF << "[";
763 while (it!=nameIdToColor.end() && it->first==item.m_id) {
764 lF << "Co" << it->second << ",";
765 ++it;
766 }
767 lF << "],";
768 asciiFile.addPos(item.m_pos);
769 asciiFile.addNote(lF.str().c_str());
770 });
771 }
772
773
774 bool Canvas5StyleManager::readInks9(std::shared_ptr<Canvas5Structure::Stream> stream)
775 {
776 if (!stream || !stream->input())
777 return false;
778 auto input=stream->input();
779 auto &ascFile=stream->ascii();
780 long pos=input->tell();
781 libmwaw::DebugStream f;
782 f << "Entries(Color)[list]:";
783 ascFile.addPos(pos);
784 ascFile.addNote(f.str().c_str());
785
786 if (!m_mainParser->readArray9(stream, "Color",
787 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
788 auto lInput=lStream->input();
789 libmwaw::DebugStream lF;
790 auto &asciiFile=lStream->ascii();
791 if (item.m_length<8) {
792 MWAW_DEBUG_MSG(("Canvas5StyleManager::readInks9: can not find the color type\n"));
793 lF << "###";
794 asciiFile.addPos(item.m_pos);
795 asciiFile.addNote(lF.str().c_str());
796 return;
797 }
798 lF << "Co" << item.m_id << ",";
799 int lVal=int(lInput->readLong(4));
800 if (lVal!=item.m_id)
801 lF << "#g0=" << lVal << ",";
802 long len2=long(lInput->readULong(4));
803 auto color=readColorStyle(lStream, item.m_type, std::min(item.m_length-8,len2));
804 if (color)
805 m_state->m_idToColor[item.m_id]=color;
806 else
807 lF << "###";
808 asciiFile.addPos(item.m_pos);
809 asciiFile.addNote(lF.str().c_str());
810 }))
811 return false;
812 if (!m_mainParser->readArray9(stream, "Color[name]", &Canvas5Parser::stringDataFunction))
813 return false;
814 pos=input->tell();
815 if (!input->checkPosition(pos+4)) {
816 MWAW_DEBUG_MSG(("Canvas5StyleManager::readInks9: can not find the array block\n"));
817 ascFile.addPos(pos);
818 ascFile.addNote("Color-End###");
819 return false;
820 }
821 pos=input->tell();
822 f.str("");
823 f << "Color-End:";
824 int val=int(input->readLong(4));
825 if (val!=-1) // checkme: maybe another length
826 f << "f0=" << val << ",";
827 ascFile.addPos(pos);
828 ascFile.addNote(f.str().c_str());
829 return true;
830 }
831
832 std::shared_ptr<Canvas5StyleManagerInternal::ColorStyle> Canvas5StyleManager::readColorStyle(std::shared_ptr<Canvas5Structure::Stream> stream, unsigned type, long len)
833 {
834 if (!stream || !stream->input()) {
835 MWAW_DEBUG_MSG(("Canvas5StyleManager::readColorStyle: can not find the input\n"));
836 return nullptr;
837 }
838 auto input=stream->input();
839 long pos=input->tell();
840 if (len<0 || !input->checkPosition(pos+len)) {
841 MWAW_DEBUG_MSG(("Canvas5StyleManager::readColorStyle: the zone seems too short\n"));
842 return nullptr;
843 }
844 libmwaw::DebugFile &ascFile = stream->ascii();
845 libmwaw::DebugStream f;
846 if (version()>=9)
847 f << "Color:";
848 auto color=std::make_shared<Canvas5StyleManagerInternal::ColorStyle>();
849 color->m_type=type;
850 switch (type) {
851 case 0:
852 if (len==24) { // gray?
853 std::string extra;
854 if (!readColor(*stream, color->m_color, extra)) {
855 color->m_color.setSet(false);
856 f << "##";
857 }
858 f << extra << ",";
859 break;
860 }
861 if (len!=4) {
862 MWAW_DEBUG_MSG(("Canvas5StyleManager::readColorStyle: unexpected length\n"));
863 f << "###";
864 break;
865 }
866 color->m_color=MWAWColor(0,0,0,0);
867 for (int i=0; i<2; ++i) {
868 int val=int(input->readLong(2));
869 if (val)
870 f << "f" << i << "=" << val << ",";
871 }
872 break;
873 case 1: {
874 if (len<24) {
875 MWAW_DEBUG_MSG(("Canvas5StyleManager::readColorStyle: unexpected length\n"));
876 f << "###";
877 break;
878 }
879 std::string extra;
880 if (!readColor(*stream, color->m_color, extra)) {
881 color->m_color.setSet(false);
882 f << "##";
883 }
884 f << extra << ",";
885 if (len==24 || version()<=5)
886 break;
887 // normally only in v6
888 std::string name;
889 for (int i=0; i<len-24; ++i) {
890 char c=char(input->readULong(1));
891 if (!c)
892 break;
893 name+=c;
894 }
895 f << name << ",";
896 break;
897 }
898 case 0x68746368: { // htch
899 color->m_color=MWAWColor(0,0,0,0);
900 color->m_color.setSet(false);
901 if (!readHatch(stream, len, color->m_hatch, color->m_color))
902 f << "###";
903 break;
904 }
905 case 0x4f62466c: // ObFl
906 color->m_color.setSet(false);
907 f << "ObFl,";
908 if (!readGradient(stream, len, color->m_gradient))
909 f << "###";
910 break;
911 case 0x50415453: // PATS: v9
912 case 0x54585552: { // TXUR
913 MWAWVariable<MWAWColor> bgColor;
914 auto image=readSymbol(stream, len, bgColor);
915 MWAWColor avgColor;
916 color->m_color.setSet(false);
917 if (!image || !getImageParser()->getTexture(image, color->m_texture, color->m_textureDim, avgColor))
918 f << "###";
919 else
920 color->m_color=avgColor;
921 break;
922 }
923 case 0x766b666c: { // vkfl
924 color->m_color=MWAWColor(0,0,0,0);
925 color->m_color.setSet(false);
926 if (!readSymbol(stream, len, color->m_color))
927 f << "###";
928 break;
929 }
930 default: {
931 static bool first=true;
932 if (first) {
933 MWAW_DEBUG_MSG(("Canvas5StyleManager::readColorStyle: can not read some complex ink color\n"));
934 first=false;
935 }
936 f << "type=" << Canvas5Structure::getString(color->m_type) << "##";
937 color->m_color.setSet(false);
938 break;
939 }
940 }
941 ascFile.addPos(pos);
942 ascFile.addNote(f.str().c_str());
943 return color;
944 }
945
946 std::shared_ptr<Canvas5ImageInternal::VKFLImage> Canvas5StyleManager::readSymbol
947 (std::shared_ptr<Canvas5Structure::Stream> stream, long len, MWAWVariable<MWAWColor> &backColor)
948 {
949 if (!stream || !stream->input()) {
950 MWAW_DEBUG_MSG(("Canvas5StyleManager::readSymbol: bad input\n"));
951 return nullptr;
952 }
953 auto input=stream->input();
954 long pos=input->tell();
955 int const vers=version();
956 int const headerLen=vers<9 ? 36 : 56;
957 if (len<headerLen || !input->checkPosition(pos+len)) {
958 MWAW_DEBUG_MSG(("Canvas5StyleManager::readSymbol: the zone seems too short\n"));
959 return nullptr;
960 }
961 libmwaw::DebugFile &ascFile = stream->ascii();
962 libmwaw::DebugStream f;
963 f << "Entries(Symbol):";
964 for (int i=0; i<5; ++i) {
965 double value=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
966 if (value<=0 && value>=0)
967 continue;
968 char const *wh[]= { "deplX", "deplY", "stagger", "rotation", "scale" };
969 f << wh[i] << "=" << value << ",";
970 }
971 long sz=input->readLong(4);
972 f << "sz=" << sz << ",";
973 long endSize=long(input->readULong(4));
974 if (endSize)
975 f << "sz[end]=" << endSize << ",";
976 long endPos=pos+headerLen+sz;
977 if (sz<0 || endSize<0 || headerLen+sz+endSize<0 || headerLen+sz+endSize>len) {
978 f << "###";
979 MWAW_DEBUG_MSG(("Canvas5StyleManager::readSymbol: can not read the symbox sz\n"));
980 ascFile.addPos(pos);
981 ascFile.addNote(f.str().c_str());
982 return nullptr;
983 }
984 int val;
985 for (int i=0; i<2; ++i) { // g0=0|2e2e, g1=0|c3|e6e6
986 val=int(input->readLong(2));
987 if (val)
988 f << "g" << i << "=" << std::hex << val << std::dec << ",";
989 }
990 val=int(input->readLong(1));
991 if (val!=1) f << "type?=" << val << ",";
992 input->seek(3, librevenge::RVNG_SEEK_CUR);
993 ascFile.addPos(pos);
994 ascFile.addNote(f.str().c_str());
995
996 std::shared_ptr<Canvas5ImageInternal::VKFLImage> image;
997 if (sz>0)
998 getImageParser()->readVKFL(stream, sz, image);
999
1000 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1001 if (!endSize)
1002 return image;
1003
1004 f.str("");
1005 f << "Symbol-End:";
1006 if (endSize>4) {
1007 unsigned type=unsigned(input->readULong(4));
1008 f << Canvas5Structure::getString(type) << ",";
1009 auto endColor=readColorStyle(stream, type, endSize-4);
1010 if (!endColor)
1011 f << "###";
1012 else {
1013 backColor=endColor->m_color;
1014 ascFile.addPos(endPos+4);
1015 ascFile.addNote(f.str().c_str());
1016 }
1017 }
1018 ascFile.addPos(endPos);
1019 ascFile.addNote(f.str().c_str());
1020
1021 return image;
1022 }
1023
1024 bool Canvas5StyleManager::readDashes(std::shared_ptr<Canvas5Structure::Stream> stream)
1025 {
1026 if (!stream || !stream->input())
1027 return false;
1028 auto input=stream->input();
1029 long pos=input->tell();
1030 libmwaw::DebugFile &ascFile = stream->ascii();
1031 libmwaw::DebugStream f;
1032 f << "Entries(Dash):";
1033 ascFile.addPos(pos);
1034 ascFile.addNote(f.str().c_str());
1035
1036 if (!m_mainParser->readUsed(*stream, "Dash")) // checkme: probably a type list
1037 return false;
1038
1039 return m_mainParser->readExtendedHeader(stream, 64, "Dash",
1040 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
1041 auto lInput=lStream->input();
1042 libmwaw::DebugFile &asciiFile = lStream->ascii();
1043 libmwaw::DebugStream lF;
1044 lF << "Da" << item.m_id << ",";
1045 std::vector<float> dashes;
1046 if (!readDash(*lStream, dashes, 1, item.m_length))
1047 lF << "###";
1048 else
1049 m_state->m_idToDash[item.m_id]=dashes;
1050 asciiFile.addPos(item.m_pos);
1051 asciiFile.addNote(lF.str().c_str());
1052 });
1053 }
1054
1055 bool Canvas5StyleManager::readDash(Canvas5Structure::Stream &stream, std::vector<float> &dashes, unsigned /*type*/, long len)
1056 {
1057 dashes.clear();
1058 MWAWInputStreamPtr input = stream.input();
1059 long pos=input->tell();
1060 int const vers=version();
1061 int expectedSize=vers<9 ? 64 : 136;
1062 if (len<expectedSize || !input->checkPosition(pos+expectedSize)) {
1063 MWAW_DEBUG_MSG(("Canvas5StyleManager::readDash: the zone seems too short\n"));
1064 return false;
1065 }
1066
1067 libmwaw::DebugFile &ascFile = stream.ascii();
1068 libmwaw::DebugStream f;
1069 f << "Entries(Dash):";
1070 int val;
1071 for (int i=0; i<2; ++i) {
1072 val=int(input->readLong(2));
1073 if (val)
1074 f << "f" << i << "=" << val << ",";
1075 }
1076 int N;
1077 bool inverted=input->readInverted();
1078 if (vers<9) {
1079 N=int(input->readLong(2));
1080 val=int(input->readLong(2));
1081 if (val!=1) f << "f2=" << val << ",";
1082 }
1083 else {
1084 input->seek(pos+124, librevenge::RVNG_SEEK_SET);
1085 N=int(input->readLong(2)); // checkme: maybe 4
1086 for (int i=0; i<5; ++i) {
1087 val=int(input->readLong(2));
1088 if (val!=(i==0 ? 1 : 0)) f << "f" << i+1 << "=" << val << ",";
1089 }
1090 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1091 }
1092 f << "N=" << N << ",";
1093 if (N>14) {
1094 if (N>0 && (N%512)==0 && (N>>8)<14) { // look for potential inversion and N even
1095 MWAW_DEBUG_MSG(("Canvas5StyleManager::readDash: endian seems inverted\n"));
1096 input->setReadInverted(!inverted);
1097 N=(N>>8);
1098 f << "#N=" << N << ",";
1099 }
1100 else {
1101 MWAW_DEBUG_MSG(("Canvas5StyleManager::readDash: N seems bad\n"));
1102 f << "###";
1103 N=0;
1104 }
1105 }
1106 f << "dash=[";
1107 for (int i=0; i<N; ++i) {
1108 double value=m_mainParser->readDouble(stream, vers<9 ? 4 : 8);
1109 dashes.push_back(float(value));
1110 f << value << ",";
1111 }
1112 f << "],";
1113 input->setReadInverted(inverted);
1114 ascFile.addPos(pos);
1115 ascFile.addNote(f.str().c_str());
1116 return true;
1117 }
1118
1119 bool Canvas5StyleManager::readFonts(std::shared_ptr<Canvas5Structure::Stream> stream, int numFonts)
1120 {
1121 if (!stream || !stream->input())
1122 return false;
1123 MWAWInputStreamPtr input = stream->input();
1124 long pos=input->tell();
1125 if (numFonts<=0 || !input->checkPosition(pos+136*numFonts)) {
1126 MWAW_DEBUG_MSG(("Canvas5StyleManager::readFonts: zone seems too short\n"));
1127 return false;
1128 }
1129
1130 libmwaw::DebugFile &ascFile = stream->ascii();
1131 libmwaw::DebugStream f;
1132 f << "Entries(Font):N=" << numFonts << ",";
1133 ascFile.addPos(pos-4);
1134 ascFile.addNote(f.str().c_str());
1135
1136 auto fontConverter=m_parserState->m_fontConverter;
1137 std::string const family=m_mainParser->isWindowsFile() ? "CP1252" : "";
1138 for (int fo=0; fo<numFonts; ++fo) {
1139 pos=input->tell();
1140 f.str("");
1141 f << "Font-F" << fo << ":";
1142 int id=int(input->readULong(2));
1143 f << "id=" << id << ",";
1144 for (int i=0; i<3; ++i) {
1145 int val=int(input->readLong(2));
1146 if (val!=(i==0 ? 4 : 0)) f << "f" << i << "=" << val << ",";
1147 }
1148 int dSz=int(input->readULong(1));
1149 if (dSz>=127) {
1150 MWAW_DEBUG_MSG(("Canvas5StyleManager::readFonts: can not read a name\n"));
1151 f << "###name";
1152 }
1153 else {
1154 std::string name;
1155 for (int s=0; s<dSz; ++s) name+=char(input->readULong(1));
1156 if (!name.empty())
1157 fontConverter->setCorrespondance(fo+1, name, family);
1158 f << name << ",";
1159 }
1160 ascFile.addPos(pos);
1161 ascFile.addNote(f.str().c_str());
1162 input->seek(pos+136, librevenge::RVNG_SEEK_SET);
1163 }
1164 return true;
1165 }
1166
1167 bool Canvas5StyleManager::readFormats(std::shared_ptr<Canvas5Structure::Stream> stream)
1168 {
1169 if (!stream || !stream->input()) {
1170 MWAW_DEBUG_MSG(("Canvas5StyleManager::readFormats: can not find the input\n"));
1171 return false;
1172 }
1173 auto input=stream->input();
1174 long pos=input ? input->tell() : 0;
1175 if (!input || !input->checkPosition(pos+7*44+4)) {
1176 MWAW_DEBUG_MSG(("Canvas5StyleManager::readFormats: file is too short\n"));
1177 return false;
1178 }
1179
1180 libmwaw::DebugFile &ascFile = stream->ascii();
1181 libmwaw::DebugStream f;
1182 f << "Entries(Format):";
1183 ascFile.addPos(pos);
1184 ascFile.addNote(f.str().c_str());
1185
1186 for (int fo=0; fo<7; ++fo) {
1187 pos=input->tell();
1188 f.str("");
1189 f << "Format-" << fo << ":";
1190 int val=int(input->readLong(2));
1191 if (val) // small integer
1192 f << "f0=" << val << ",";
1193 val=int(input->readLong(2)); // 1|a|c|10
1194 f << "f1=" << val << ",";
1195 for (int i=0; i<2; ++i) { // f2=0|1, f3=0|3|10
1196 val=int(input->readLong(2));
1197 if (val)
1198 f << "f" << i+2 << "=" << val << ",";
1199 }
1200 for (int i=0; i<4; ++i) {
1201 val=int(input->readULong(4));
1202 if (val!=0x10000)
1203 f << "dim" << i << "=" << double(val)/double(0x10000) << ",";
1204 }
1205 int len=int(input->readULong(1));
1206 if (len<=19) {
1207 std::string text;
1208 for (int i=0; i<len; ++i) text+=char(input->readULong(1));
1209 f << "name=" << text << ",";
1210 }
1211 else {
1212 MWAW_DEBUG_MSG(("Canvas5StyleManager::readFormats: can not read the format name\n"));
1213 f << "###name,";
1214 }
1215 ascFile.addPos(pos);
1216 ascFile.addNote(f.str().c_str());
1217 input->seek(pos+44, librevenge::RVNG_SEEK_SET);
1218 if (fo!=0) continue;
1219
1220 pos=input->tell();
1221 f.str("");
1222 f << "Format-unk:";
1223 for (int i=0; i<2; ++i) { // f0: small number
1224 val=int(input->readLong(2));
1225 if (val)
1226 f << "f" << i << "=" << val << ",";
1227 }
1228 ascFile.addPos(pos);
1229 ascFile.addNote(f.str().c_str());
1230 }
1231 return true;
1232 }
1233
1234 bool Canvas5StyleManager::readPenSize(std::shared_ptr<Canvas5Structure::Stream> stream)
1235 {
1236 if (!stream || !stream->input()) {
1237 MWAW_DEBUG_MSG(("Canvas5StyleManager::readPenSize: can not find the input\n"));
1238 return false;
1239 }
1240 auto input=stream->input();
1241 long pos=input->tell();
1242 if (!input->checkPosition(pos+20)) {
1243 MWAW_DEBUG_MSG(("Canvas5StyleManager::readPenSize: file is too short\n"));
1244 return false;
1245 }
1246
1247 libmwaw::DebugFile &ascFile = stream->ascii();
1248 libmwaw::DebugStream f;
1249 f << "Entries(PenSize):sz=[";
1250 for (int i=0; i<10; ++i)
1251 f << double(input->readULong(2))/256. << ",";
1252 f << "],";
1253 ascFile.addPos(pos);
1254 ascFile.addNote(f.str().c_str());
1255 return true;
1256 }
1257
1258 bool Canvas5StyleManager::readPenStyles(std::shared_ptr<Canvas5Structure::Stream> stream)
1259 {
1260 if (!stream || !stream->input())
1261 return false;
1262 auto input=stream->input();
1263 long pos=input->tell();
1264
1265 libmwaw::DebugStream f;
1266 libmwaw::DebugFile &ascFile = stream->ascii();
1267 f << "Entries(PenStyl):";
1268 ascFile.addPos(pos);
1269 ascFile.addNote(f.str().c_str());
1270
1271 std::map<int, unsigned> idToTypeMap;
1272 if (!m_mainParser->readExtendedHeader(stream, 4, "PenStyl",
1273 [&idToTypeMap](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
1274 auto lInput=lStream->input();
1275 libmwaw::DebugFile &asciiFile = lStream->ascii();
1276 libmwaw::DebugStream lF;
1277 lF << "Pe" << item.m_id << ",";
1278 unsigned type=unsigned(lInput->readULong(4));
1279 lF << "type=" << Canvas5Structure::getString(type) << ",";
1280 idToTypeMap[item.m_id]=type;
1281 asciiFile.addPos(item.m_pos);
1282 asciiFile.addNote(lF.str().c_str());
1283 }))
1284 return false;
1285
1286 if (!m_mainParser->readIndexMap(stream, "PenStyl",
1287 [this, &idToTypeMap](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
1288 auto lInput=lStream->input();
1289 auto &asciiFile=lStream->ascii();
1290 libmwaw::DebugStream lF;
1291 lF << "Pe" << item.m_id << ",";
1292 unsigned type=1;
1293 auto it=idToTypeMap.find(item.m_id);
1294 if (it!=idToTypeMap.end()) type=it->second;
1295 auto style=readPenStyle(*lStream, type, item.m_length);
1296 if (!style)
1297 lF << "###";
1298 else
1299 m_state->m_idToPen[item.m_id]=style;
1300 asciiFile.addPos(item.m_pos);
1301 asciiFile.addNote(lF.str().c_str());
1302 }))
1303 return false;
1304 return m_mainParser->readUsed(*stream, "PenStyl");
1305 }
1306
1307 std::shared_ptr<Canvas5StyleManagerInternal::PenStyle> Canvas5StyleManager::readPenStyle(Canvas5Structure::Stream &stream, unsigned type, long len)
1308 {
1309 auto input=stream.input();
1310 long pos=input->tell();
1311
1312 libmwaw::DebugStream f;
1313 libmwaw::DebugFile &ascFile = stream.ascii();
1314 f << "Entries(PenStyl):";
1315
1316 auto style=std::make_shared<Canvas5StyleManagerInternal::PenStyle>();
1317 style->m_type=type;
1318 int const vers=version();
1319 switch (type) {
1320 // case 0: find one time with size=0x70 and inverted endian, so unsure
1321 case 1: {
1322 int expectedLength=vers<9 ? 32 : 0x70;
1323 if (len<expectedLength) {
1324 MWAW_DEBUG_MSG(("Canvas5StyleManager::readPenStyle: find unexpected size for type 1\n"));
1325 return nullptr;
1326 }
1327 float widths[2];
1328 for (auto &w : widths) {
1329 if (vers<9) {
1330 w=float(input->readLong(4))/65536.f;
1331 continue;
1332 }
1333 double dVal;
1334 bool isNan;
1335 if (m_mainParser->readDouble(stream, dVal, isNan))
1336 w=float(dVal);
1337 else {
1338 MWAW_DEBUG_MSG(("Canvas5StyleManager::readPenStyle: can not read a width\n"));
1339 w=0;
1340 f << "###bad value,";
1341 }
1342 }
1343 style->m_size=MWAWVec2f(widths[0],widths[1]);
1344 ascFile.addDelimiter(input->tell(),'|');
1345 break;
1346 }
1347 case 0x706c696e: { // plin
1348 int headerLen=vers==5 ? 16 : vers<9 ? 60 : 64;
1349 int dataLen=vers==5 ? 128 : vers<9 ? 164 : 328;
1350 if (len<headerLen) {
1351 MWAW_DEBUG_MSG(("Canvas5StyleManager::readPenStyle: find unexpected size for plin\n"));
1352 return nullptr;
1353 }
1354 if (vers>5) {
1355 // checkme: look like ObFl structure
1356 for (int i=0; i<2; ++i) {
1357 int lVal=int(input->readLong(4));
1358 int const expected[]= {vers<9 ? 0xfa : 0xdf, vers<9 ? 0x600 : 0x700};
1359 if (lVal!=expected[i])
1360 f << "f" << i << "=" << lVal << ",";
1361 }
1362 }
1363 int N=m_mainParser->readInteger(stream, vers==5 ? 4 : vers<9 ? 2 : 8);
1364
1365 f << "plin,N=" << N << ",";
1366 if (N<0 || (len-headerLen)/dataLen<N) {
1367 MWAW_DEBUG_MSG(("Canvas5StyleManager::readPenStyle: find unexpected value of N for plin\n"));
1368 f << "###";
1369 ascFile.addPos(pos);
1370 ascFile.addNote(f.str().c_str());
1371 return nullptr;
1372 }
1373 style->m_usePenColor=false;
1374 for (int i=0; i<2; ++i) {
1375 int val=int(input->readLong(1));
1376 if (!val) continue;
1377 char const *wh[]= {"equidistant", "usePenLine" };
1378 if (val==1) {
1379 f << wh[i] << ",";
1380 if (i==1)
1381 style->m_usePenColor=true;
1382 }
1383 else
1384 f << wh[i] << "=" << val << ",";
1385 }
1386 ascFile.addDelimiter(input->tell(),'|');
1387 input->seek(pos+headerLen, librevenge::RVNG_SEEK_SET);
1388 libmwaw::DebugStream f2;
1389 std::vector<float> offsets;
1390 float sumOffsets=0;
1391 for (int i=0; i<N; ++i) {
1392 long actPos=input->tell();
1393 f2.str("");
1394 f2 << "PenStyl,Pe[plin" << i << "]:";
1395 Canvas5StyleManagerInternal::PenStyle::Line line;
1396 std::string extra;
1397 if (!readColor(stream, line.m_color, extra)) {
1398 f2 << "##";
1399 input->seek(actPos+24, librevenge::RVNG_SEEK_SET);
1400 }
1401 f2 << extra;
1402 if (vers>5) {
1403 std::string name;
1404 for (int j=0; j<32; ++j) {
1405 char c=char(input->readULong(1));
1406 if (!c)
1407 break;
1408 name+=c;
1409 }
1410 f2 << name << ",";
1411 input->seek(actPos+24+32, librevenge::RVNG_SEEK_SET);
1412 }
1413 float width[2];
1414 for (auto &w : width) w=float(m_mainParser->readDouble(stream, vers<9 ? 4 : 8));
1415 line.m_size=MWAWVec2f(width[0],width[1]);
1416 f2 << "w=" << line.m_size << ",";
1417 for (int j=0; j<46; ++j) { // 0
1418 int val=int(input->readLong(2));
1419 if (val) f2 << "g" << j << "=" << val << ",";
1420 }
1421 if (vers==5) {
1422 offsets.push_back(float(input->readULong(4))/65536);
1423 sumOffsets+=offsets.back();
1424 f2 << "decal=" << offsets.back() << ",";
1425 }
1426 else {
1427 input->seek(actPos+dataLen-(vers<9 ? 8 : 16), librevenge::RVNG_SEEK_SET);
1428 offsets.push_back(float(m_mainParser->readDouble(stream, vers<9 ? 4 : 8)));
1429 sumOffsets+=offsets.back();
1430 f2 << "decal=" << offsets.back() << ",";
1431 for (int j=0; j<(vers<9 ? 1 : 2); ++j) {
1432 int val=int(input->readLong(4));
1433 if (val) f2 << "h" << j << "=" << val << ",";
1434 }
1435 }
1436 style->m_lines.push_back(line);
1437 ascFile.addPos(actPos);
1438 ascFile.addNote(f2.str().c_str());
1439 }
1440 float actualOffset=sumOffsets/2;
1441 for (size_t i=0; i<std::min(style->m_lines.size(),offsets.size()); ++i) {
1442 style->m_lines[i].m_offset=actualOffset;
1443 actualOffset-=offsets[i];
1444 }
1445 ascFile.addDelimiter(input->tell(),'|');
1446 break;
1447 }
1448 case 0x766e656f: { // vneo
1449 int headerLen=vers==5 ? 68 : vers<9 ? 184 : 236;
1450 if (len<headerLen) {
1451 MWAW_DEBUG_MSG(("Canvas5StyleManager::readPenStyle: the vneo zone seems too short\n"));
1452 return nullptr;
1453 }
1454 if (vers!=5) { // checkme: look like ObFl structure
1455 for (int i=0; i<3; ++i) {
1456 int lVal=int(input->readLong(4));
1457 int const expected[]= {vers<9 ? 0xdf : 0xadf, vers<9 ? 0x600 : 0xa00, 0};
1458 if (lVal!=expected[i])
1459 f << "f" << i << "=" << lVal << ",";
1460 }
1461 if (vers>=9) {
1462 int lVal=int(input->readULong(4));
1463 if (lVal) f << "type=" << Canvas5Structure::getString(unsigned(lVal)) << ",";
1464 }
1465 }
1466 float w=float(m_mainParser->readDouble(stream, vers<9 ? 4 : 8));
1467 f << "w=" << w << ",";
1468 style->m_size=MWAWVec2f(w,w);
1469 if (vers>=9) {
1470 ascFile.addDelimiter(input->tell(), '|');
1471 input->seek(16, librevenge::RVNG_SEEK_CUR); // 01040004000000000000000100000000
1472 ascFile.addDelimiter(input->tell(), '|');
1473 f << "values=[";
1474 for (int i=0; i<3; ++i) // unkn, width, angle
1475 f << m_mainParser->readDouble(stream, 8) << ",";
1476 f << "],";
1477 ascFile.addDelimiter(input->tell(), '|');
1478 input->seek(52, librevenge::RVNG_SEEK_CUR);
1479 ascFile.addDelimiter(input->tell(), '|');
1480 }
1481 for (int i=0; i<2; ++i) {
1482 std::string extra;
1483 long actPos=input->tell();
1484 if (!readColor(stream, style->m_colors[i], extra)) {
1485 f << "##";
1486 input->seek(actPos+24, librevenge::RVNG_SEEK_SET);
1487 }
1488 f << "col" << i << "=[" << extra << "],";
1489 if (vers>5) {
1490 std::string name;
1491 for (int j=0; j<32; ++j) {
1492 char c=char(input->readULong(1));
1493 if (!c)
1494 break;
1495 name+=c;
1496 }
1497 f << name << ",";
1498 input->seek(actPos+24+32, librevenge::RVNG_SEEK_SET);
1499 }
1500 }
1501 if (vers>=9)
1502 break;
1503 int val=int(input->readLong(2));
1504 if (val)
1505 f << "f0=" << val << ",";
1506 val=int(input->readULong(2));
1507 if (val&0x100)
1508 f << "axial,";
1509 val &= 0xfeff;
1510 if (val!=0xdd)
1511 f << "fl=" << std::hex << val << std::dec << ",";
1512 for (int i=0; i<2; ++i) {
1513 val=int(input->readULong(4));
1514 if (!val) continue;
1515 if (i==0)
1516 f << "corner=" << val << ","; // 0: none, 1: round, 2: square
1517 else
1518 f << "join=" << val << ","; // 0: mitter, 1: round, 2:bevel
1519 }
1520 f << "angle=" << float(input->readLong(4))/65536 << "rad,";
1521 if (vers!=5)
1522 ascFile.addDelimiter(input->tell(),'|');
1523 break;
1524 }
1525 default:
1526 MWAW_DEBUG_MSG(("Canvas5StyleManager::readPenStyle: find unexpected type=%s\n", Canvas5Structure::getString(type).c_str()));
1527 return nullptr;
1528 }
1529 f << *style;
1530 ascFile.addPos(pos);
1531 ascFile.addNote(f.str().c_str());
1532 return style;
1533 }
1534
1535 bool Canvas5StyleManager::readStrokes(std::shared_ptr<Canvas5Structure::Stream> stream)
1536 {
1537 if (!stream || !stream->input())
1538 return false;
1539 auto input=stream->input();
1540 long pos=input->tell();
1541
1542 libmwaw::DebugStream f;
1543 libmwaw::DebugFile &ascFile = stream->ascii();
1544 f << "Entries(Stroke):";
1545 ascFile.addPos(pos);
1546 ascFile.addNote(f.str().c_str());
1547 if (!m_mainParser->readUsed(*stream, "Stroke"))
1548 return false;
1549 if (!m_mainParser->readExtendedHeader(stream, 20, "Stroke",
1550 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
1551 auto lInput=lStream->input();
1552 libmwaw::DebugFile &asciiFile = lStream->ascii();
1553 libmwaw::DebugStream lF;
1554 lF << "St" << item.m_id << ",";
1555 Canvas5StyleManagerInternal::Stroke style;
1556 style.m_type=unsigned(lInput->readULong(4));
1557 style.m_penId=int(lInput->readLong(4));
1558 style.m_dashId=int(lInput->readLong(4));
1559 for (int i=0; i<2; ++i)
1560 style.m_arrowId[i]=int(lInput->readLong(4));
1561 lF << style;
1562 m_state->m_idToStroke[item.m_id]=style;
1563 asciiFile.addPos(item.m_pos);
1564 asciiFile.addNote(lF.str().c_str());
1565 }))
1566 return false;
1567
1568 return true;
1569 }
1570
1571 ////////////////////////////////////////////////////////////
1572 // styles
1573 ////////////////////////////////////////////////////////////
1574 bool Canvas5StyleManager::readCharStyle(Canvas5Structure::Stream &stream, int id, CharStyle &font,
1575 bool useFileColors)
1576 {
1577 auto input=stream.input();
1578 libmwaw::DebugFile &ascFile = stream.ascii();
1579 int const vers=version();
1580 long pos=input->tell();
1581 libmwaw::DebugStream f;
1582 if (id<0)
1583 f << "Entries(CharStyl):";
1584 else
1585 f << "CharStyl-" << id << ":";
1586 ascFile.addPos(pos);
1587 ascFile.addNote(f.str().c_str());
1588 font.m_paragraphId=font.m_linkId=0;
1589 int const sz=vers<9 ? 60 : 96;
1590 if (!input->checkPosition(pos+sz)) {
1591 MWAW_DEBUG_MSG(("Canvas5StyleManager::readCharStyle: the zone is too short\n"));
1592 f << "###";
1593 ascFile.addPos(pos);
1594 ascFile.addNote(f.str().c_str());
1595 return false;
1596 }
1597 f.str("");
1598 int val=int(input->readLong(2));
1599 if (val!=1) f << "used=" << val << ",";
1600 f << "h=[";
1601 for (int i=0; i<2; ++i)
1602 f << input->readLong(2) << ",";
1603 f << "],";
1604 int fId=int(input->readULong(2));
1605 font.m_font.setId(fId);
1606 val=int(input->readULong(1));
1607 uint32_t flags = 0;
1608 if (val&0x1) flags |= MWAWFont::boldBit;
1609 if (val&0x2) flags |= MWAWFont::italicBit;
1610 if (val&0x4) font.m_font.setUnderlineStyle(MWAWFont::Line::Simple);
1611 if (val&0x8) flags |= MWAWFont::embossBit;
1612 if (val&0x10) flags |= MWAWFont::shadowBit;
1613 if (val&0x80) font.m_font.setStrikeOutStyle(MWAWFont::Line::Simple);
1614 if (val&0x60) f << "fl=" << std::hex << (val&0x60) << std::dec << ",";
1615 val=int(input->readULong(1));
1616 if (val) f << "fl1=" << std::hex << val << std::dec;
1617 if (vers<9)
1618 font.m_font.setSize(float(input->readULong(2)));
1619 else {
1620 for (int i=0; i<3; ++i) {
1621 val=int(input->readLong(2));
1622 if (val)
1623 f << "f" << i << "=" << val << ",";
1624 }
1625 font.m_font.setSize(float(m_mainParser->readDouble(stream, 8)));
1626 }
1627 val=int(input->readLong(4));
1628 if (val) {
1629 // CHECKME: inside a Vkfl image, colorId and linkId are a negative number,
1630 // I do not know how to retrieve the color/link using this number
1631 if (!useFileColors)
1632 f << "##COLOR=" << val << ",";
1633 else {
1634 auto it=m_state->m_idToColor.find(val);
1635 if (it!=m_state->m_idToColor.end() && it->second && it->second->m_color.isSet())
1636 font.m_font.setColor(*it->second->m_color);
1637 else
1638 f << "#Co1" << val << ",";
1639 }
1640 }
1641 for (int i=0; i<2; ++i) { //
1642 val=int(input->readLong(2));
1643 if (!val) continue;
1644 if (i==0) {
1645 f << "E" << val << ",";
1646 font.m_paragraphId=val;
1647 }
1648 else
1649 f << "f" << i << "=" << val << ",";
1650 }
1651 float stretchs[]= {1,1};
1652 if (vers<9) {
1653 val=int(input->readLong(4));
1654 if ((val>=-60*65536 && val<0) || (val>0 && val<60*65536)) // is 30 pt is big enough ?
1655 font.m_font.setDeltaLetterSpacing(float(val)/2/65536, librevenge::RVNG_POINT);
1656 else if (val) {
1657 MWAW_DEBUG_MSG(("Canvas5Style::readCharStyle: unknown delta spacing\n"));
1658 f << "##delta[spacing]=" << val/65536 << ",";
1659 }
1660
1661 int lVals[4];
1662 for (auto &l : lVals) l=int(input->readLong(2));
1663 for (int i=0; i<2; ++i) {
1664 if (lVals[i]==lVals[i+2])
1665 continue;
1666 f << "scaling[" << (i==0 ? "hori" : "verti") << "]=" << lVals[i] << "/" << lVals[i+2] << ",";
1667 if (lVals[i]<=0 || lVals[i+2]<=0) {
1668 MWAW_DEBUG_MSG(("Canvas5Style::readCharStyle: invalid scaling\n"));
1669 f << "###";
1670 }
1671 else
1672 stretchs[i]=float(lVals[i])/float(lVals[i+2]);
1673 }
1674 val=int(input->readLong(4));
1675 if (val)
1676 font.m_font.set(MWAWFont::Script(float(val)/65536,librevenge::RVNG_POINT));
1677 }
1678 else {
1679 for (int i=0; i<4; ++i) {
1680 double dVal=m_mainParser->readDouble(stream, 8);
1681 double const expected=i==0 || i==3 ? 0 : 1;
1682 if (dVal<=expected && dVal>=expected)
1683 continue;
1684 if (i==0)
1685 font.m_font.setDeltaLetterSpacing(float(dVal), librevenge::RVNG_POINT);
1686 else if (i==3)
1687 font.m_font.set(MWAWFont::Script(float(dVal), librevenge::RVNG_POINT));
1688 else {
1689 stretchs[i-1]=float(dVal);
1690 f << "scaling[" << (i==1 ? "hori" : "verti") << "]=" << dVal << ",";
1691 }
1692 }
1693 }
1694 if (stretchs[1]>1-1e-4f && stretchs[1]<1+1e-4f) {
1695 if (stretchs[0]<1-1e-4f || stretchs[0]>1+1e-4f)
1696 font.m_font.setWidthStreching(stretchs[0]);
1697 }
1698 else {
1699 font.m_font.setSize(font.m_font.size()*stretchs[1]);
1700 font.m_font.setWidthStreching(stretchs[0]/stretchs[1]);
1701 }
1702 val=int(input->readLong(2));
1703 if (val) f << "h0=" << std::hex << val << std::dec << ","; // 4 or 8, 4 a link?
1704 val=int(input->readULong(2));
1705 if (val&1)
1706 flags |= MWAWFont::smallCapsBit;
1707 if (val&2)
1708 flags |= MWAWFont::uppercaseBit;
1709 if (val&4)
1710 flags |= MWAWFont::lowercaseBit;
1711 if (val&8)
1712 flags |= MWAWFont::initialcaseBit;
1713 if (val&0x200)
1714 f << "spread,";
1715 if (val&0x800)
1716 f << "overprint,";
1717 val &= 0xF5F0;
1718 if (val) {
1719 MWAW_DEBUG_MSG(("Canvas5Style::readCharStyle: unknown small caps bits\n"));
1720 f << "##smallCaps=" << std::hex << val << std::dec << ",";
1721 }
1722 for (int i=0; i<4; ++i) { // h2=0|1 a link id?
1723 val=int(input->readLong(4));
1724 if (!val) continue;
1725 if (i==0) {
1726 if (!useFileColors)
1727 f << "###LINK=" << val << ",";
1728 else {
1729 font.m_linkId=val;
1730 f << "link[id]=Tl" << val << ",";
1731 }
1732 }
1733 else if (i==3 && font.m_paragraphId==0) { // checkme: there is two color: the para color and the font color
1734 if (!useFileColors)
1735 f << "###COLOR=" << val << ",";
1736 else {
1737 auto it=m_state->m_idToColor.find(val);
1738 if (it!=m_state->m_idToColor.end() && it->second && it->second->m_color.isSet())
1739 font.m_font.setColor(*it->second->m_color);
1740 else
1741 f << "#Co2" << val << ",";
1742 }
1743 }
1744 else
1745 f << "h" << i+1 << "=" << val << ",";
1746 }
1747 for (int i=0; i<(vers<9 ? 2 : 6); ++i) {
1748 val=int(input->readLong(2));
1749 if (!val) continue;
1750 f << "h" << i+5 << "=" << val << ",";
1751 }
1752
1753 font.m_font.setFlags(flags);
1754 std::string const extra=f.str();
1755 f.str("");
1756 f << font.m_font.getDebugString(m_parserState->m_fontConverter) << "," << extra;
1757 ascFile.addPos(pos);
1758 ascFile.addNote(f.str().c_str());
1759 return true;
1760 }
1761
1762 bool Canvas5StyleManager::readCharStyles(std::shared_ptr<Canvas5Structure::Stream> stream)
1763 {
1764 if (!stream || !stream->input())
1765 return false;
1766 auto input=stream->input();
1767 libmwaw::DebugFile &ascFile = stream->ascii();
1768 int const vers=version();
1769 ascFile.addPos(input->tell());
1770 ascFile.addNote("Entries(CharStyl):");
1771 if (vers>=9) {
1772 return m_mainParser->readArray9(stream, "CharStyl",
1773 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &what) {
1774 auto lInput=lStream->input();
1775 long endPos=lInput->tell()+item.m_length;
1776 lInput->seek(-4, librevenge::RVNG_SEEK_CUR);
1777 CharStyle font;
1778 if (!readCharStyle(*lStream, item.m_id, font))
1779 return;
1780
1781 long pos=lInput->tell();
1782 libmwaw::DebugStream f;
1783 libmwaw::DebugFile &asciiFile = lStream->ascii();
1784 f << what << "-" << item.m_id << "[A]:";
1785 if (pos+44>endPos) {
1786 MWAW_DEBUG_MSG(("Canvas5StyleManager::readCharStyles: the zone seems too short\n"));
1787 f << "###";
1788 asciiFile.addPos(pos);
1789 asciiFile.addNote(f.str().c_str());
1790 return;
1791 }
1792 for (int i=0; i<4; ++i) {
1793 int val=int(lInput->readLong(2));
1794 if (val)
1795 f << "f" << i << "=" << val << ",";
1796 }
1797 asciiFile.addPos(pos);
1798 asciiFile.addNote(f.str().c_str());
1799
1800 pos=lInput->tell();
1801 f.str("");
1802 f << what << "-" << item.m_id << "[name]:";
1803 std::string name;
1804 for (int i=0; i<32; ++i) {
1805 char c=char(lInput->readLong(1));
1806 if (c==0) break;
1807 name+=c;
1808 }
1809 f << name << ",";
1810 lInput->seek(pos+32, librevenge::RVNG_SEEK_SET);
1811 int val=int(lInput->readLong(4)); // unsure can be an extended size
1812 if (val)
1813 f << "f0=" << val << ",";
1814 asciiFile.addPos(pos);
1815 asciiFile.addNote(f.str().c_str());
1816 });
1817 }
1818 if (!m_mainParser->readExtendedHeader(stream, 0x64, "CharStyl",
1819 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &what) {
1820 CharStyle font;
1821 if (!readCharStyle(*lStream, item.m_id, font))
1822 return;
1823 auto lInput=lStream->input();
1824 long pos=lInput->tell();
1825 libmwaw::DebugStream f;
1826 libmwaw::DebugFile &asciiFile = lStream->ascii();
1827
1828 f << what << "-" << item.m_id << "[A]:";
1829 for (int i=0; i<4; ++i) {
1830 int val=int(lInput->readLong(2));
1831 if (val)
1832 f << "f" << i << "=" << val << ",";
1833 }
1834 asciiFile.addPos(pos);
1835 asciiFile.addNote(f.str().c_str());
1836
1837 pos=lInput->tell();
1838 f.str("");
1839 f << what << "-" << item.m_id << "[name]:";
1840 std::string name;
1841 for (int i=0; i<32; ++i) {
1842 char c=char(lInput->readLong(1));
1843 if (c==0) break;
1844 name+=c;
1845 }
1846 f << name << ",";
1847 lInput->seek(pos+32, librevenge::RVNG_SEEK_SET);
1848 asciiFile.addPos(pos);
1849 asciiFile.addNote(f.str().c_str());
1850 }))
1851 return false;
1852 if (!m_mainParser->readIndexMap(stream, "CharStyl"))
1853 return false;
1854 std::vector<bool> defined;
1855 if (!m_mainParser->readDefined(*stream, defined, "CharStyl"))
1856 return false;
1857 if (!m_mainParser->readUsed(*stream, "CharStyl"))
1858 return false;
1859 return m_mainParser->readExtendedHeader(stream, 8, "CharStyl[data2]", &Canvas5Parser::defDataFunction);
1860 }
1861
1862 bool Canvas5StyleManager::readParaStyle(std::shared_ptr<Canvas5Structure::Stream> stream, int id, Canvas5StyleManager::StyleList *styles)
1863 {
1864 if (!stream || !stream->input())
1865 return false;
1866 auto input=stream->input();
1867 libmwaw::DebugFile &ascFile = stream->ascii();
1868 int const vers=version();
1869
1870 long pos=input->tell();
1871 libmwaw::DebugStream f;
1872 if (id<0)
1873 f << "Entries(ParaStyl):";
1874
1875 if (!input->checkPosition(pos+(vers<9 ? 128 : 224))) {
1876 MWAW_DEBUG_MSG(("Canvas5StyleManager::readParaStyle: the zone is too short\n"));
1877 f << "###";
1878 ascFile.addPos(pos);
1879 ascFile.addNote(f.str().c_str());
1880 return false;
1881 }
1882 std::pair<MWAWParagraph,int> *paraId=nullptr;
1883 if (styles && id>=0) {
1884 if (styles->m_paragraphs.size()<=size_t(id))
1885 styles->m_paragraphs.resize(size_t(id+1));
1886 paraId=&styles->m_paragraphs[size_t(id)];
1887 paraId->second=0;
1888 }
1889 MWAWParagraph *para=paraId ? &paraId->first : nullptr;
1890 int val=int(input->readLong(4));
1891 if (val!=1)
1892 f << "used=" << val << ",";
1893 val=int(input->readLong(4));
1894 if (val) {
1895 if (paraId) paraId->second=val;
1896 f << "Tab" << val << ",";
1897 }
1898 for (int i=0; i<2; ++i) { // 0
1899 val=int(input->readLong(2));
1900 if (!val) continue;
1901 if (i==0) {
1902 switch (val) {
1903 case -1:
1904 if (para) para->m_justify=MWAWParagraph::JustificationRight;
1905 f << "align=right,";
1906 break;
1907 // 0: left
1908 case 1:
1909 if (para) para->m_justify=MWAWParagraph::JustificationCenter;
1910 f << "align=center,";
1911 break;
1912 // 2: ?
1913 case 4:
1914 if (para) para->m_justify=MWAWParagraph::JustificationFull;
1915 f << "align=justify,";
1916 break;
1917 default:
1918 MWAW_DEBUG_MSG(("Canvas5StyleManager::readParaStyle: find unexpected align\n"));
1919 f << "##align=" << val << ",";
1920 break;
1921 }
1922 }
1923 else
1924 f << "f" << i << "=" << val << ",";
1925 }
1926 if (vers>=9) // align?
1927 input->seek(4, librevenge::RVNG_SEEK_CUR);
1928 double dVal=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
1929 if (dVal>0) {
1930 f << "interline=" << dVal << "pt,";
1931 if (para)
1932 para->setInterline(dVal, librevenge::RVNG_POINT);
1933 }
1934 dVal=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
1935 if (dVal>0 && (dVal<1 || dVal>1)) {
1936 f << "interline=" << dVal << ",";
1937 if (para)
1938 para->setInterline(dVal, librevenge::RVNG_PERCENT);
1939 }
1940 for (int i=0; i<4; ++i) {
1941 val=int(input->readULong(2));
1942 if (val)
1943 f << "g" << i << "=" << val << ",";
1944 }
1945 if (para) para->m_marginsUnit=librevenge::RVNG_POINT;
1946 for (int i=0; i<3; ++i) {
1947 dVal=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
1948 if (dVal<=0 && dVal>=0) continue;
1949 char const *wh[]= {"first", "left", "right"};
1950 f << wh[i] << "[marg]=" << dVal << ",";
1951 if (para)
1952 para->m_margins[i]=dVal;
1953 }
1954 if (para)
1955 para->m_margins[0]=*(para->m_margins[0])-*(para->m_margins[1]);
1956 for (int i=0; i<2; ++i) {
1957 dVal=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
1958 if (dVal<=0 && dVal>=0) continue;
1959 f << "space[" << (i==0 ? "before" : "after") << "]=" << dVal << ",";
1960 if (para)
1961 para->m_spacings[i+1]=dVal/72;
1962 }
1963 val=int(input->readULong(4));
1964 if (val) f << "g8=" << val << ",";
1965 dVal=m_mainParser->readDouble(*stream, 4);
1966 if (dVal<0 || dVal>0) f << "wrap[object]=" << dVal << ","; // useme: unit in point
1967 for (int i=0; i<(vers<9 ? 2 : 8); ++i) { // g9=0|2
1968 val=int(input->readULong(2));
1969 if (!val) continue;
1970 f << "g" << i+9 << "=" << val << ",";
1971 }
1972 if (vers>=9) {
1973 dVal=m_mainParser->readDouble(*stream, 8);
1974 if (dVal<0 || dVal>0)
1975 f << "unkn=" << dVal << ",";
1976 }
1977 int dropChar=int(input->readULong(2));
1978 int dropLine=int(input->readULong(2));
1979 if (dropChar>0 && dropLine>1) {
1980 if (para) {
1981 para->m_dropNumCharacters=dropChar;
1982 para->m_dropNumLines=dropLine;
1983 }
1984 f << "drop=" << dropChar << "[l=" << dropLine << "],";
1985 }
1986 if (vers>=9)
1987 input->seek(4, librevenge::RVNG_SEEK_CUR);
1988 ascFile.addPos(pos);
1989 ascFile.addNote(f.str().c_str());
1990
1991 MWAWFont *font=nullptr;
1992 if (styles) {
1993 for (auto &fId : styles->m_fonts) {
1994 if (fId.m_paragraphId==id)
1995 font=&fId.m_font;
1996 }
1997 }
1998
1999 return readStyleEnd(stream, font, para);
2000 }
2001
2002 bool Canvas5StyleManager::readStyleEnd(std::shared_ptr<Canvas5Structure::Stream> stream, MWAWFont *font, MWAWParagraph *para)
2003 {
2004 if (!stream || !stream->input())
2005 return false;
2006 auto input=stream->input();
2007 long pos=input->tell();
2008 libmwaw::DebugFile &ascFile = stream->ascii();
2009 libmwaw::DebugStream f;
2010 f << "ParaStyl[A]:";
2011
2012 int const vers=version();
2013 if (!input->checkPosition(pos+(vers<9 ? 64 : 104))) {
2014 MWAW_DEBUG_MSG(("Canvas5StyleManager::readStyleEnd: the zone seems too short\n"));
2015 f << "###";
2016 ascFile.addPos(pos);
2017 ascFile.addNote(f.str().c_str());
2018 return false;
2019 }
2020 double dVal=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
2021 if (dVal>0)
2022 f << "justify[last,width]=" << 100-dVal << "%,";
2023 for (int i=0; i<6; ++i) {
2024 dVal=m_mainParser->readDouble(*stream, vers<9 ? 4 : 8);
2025 if (dVal<=0 && dVal>=0) continue;
2026 char const *wh[]= {"spacing[word,min]", "spacing[word]", "spacing[word,max]", "spacing[min]", "spacing", "spacing[max]"};
2027 if (i==4 && font)
2028 font->setDeltaLetterSpacing(1+float(dVal), librevenge::RVNG_PERCENT);
2029 f << wh[i] << "=" << 100+100*dVal << "%,";
2030 }
2031 int val;
2032 f << "hyphen=[";
2033 for (int i=0; i<4; ++i) {
2034 val=int(input->readLong(2));
2035 int const expected[]= {3,2,6,3}; // after word, before word, smallest word, consecutive limite
2036 if (val!=expected[i])
2037 f << val << ",";
2038 else
2039 f << "_,";
2040 }
2041 f << "],";
2042 for (int i=0; i<4; ++i) { // g2=0|-1, g3=0|-1|0x140
2043 val=i==2 ? int(input->readULong(2)) : int(input->readLong(2));
2044 if (i==2) {
2045 int flags=0;
2046 if ((val&2)==0)
2047 f << "no[hyphen],";
2048 if ((val&4)==0)
2049 f << "skip[cap],";
2050 if ((val&0x200)==0)
2051 f << "orphan,";
2052 if ((val&0x400)==0) {
2053 flags|=MWAWParagraph::NoBreakBit;
2054 f << "keep[alllines],";
2055 }
2056 if ((val&0x800)==0) {
2057 flags|=MWAWParagraph::NoBreakWithNextBit;
2058 f << "keep[with,next],";
2059 }
2060 if (flags&&para)
2061 para->m_breakStatus=flags;
2062 val&=0xf1f9;
2063 if (val)
2064 f << "g2=" << std::hex << val << std::dec << ",";
2065 continue;
2066 }
2067 if (!val) continue;
2068 f << "g" << i << "=" << val << ",";
2069 }
2070 if (version()<9) {
2071 for (int i=0; i<10; ++i) { // 0
2072 val=int(input->readLong(2));
2073 if (!val)
2074 continue;
2075 if (i==2) { // checkme, something is bad here
2076 if (val!=100)
2077 f << "min[line,width]=" << 100-val << ",";
2078 }
2079 else if (i==3)
2080 f << "para[orphan]=" << val << ",";
2081 else if (i==4)
2082 f << "para[window]=" << val << ",";
2083 else
2084 f << "h" << i << "=" << val << ",";
2085 }
2086 }
2087 else {
2088 ascFile.addDelimiter(input->tell(),'|');
2089 input->seek(pos+104, librevenge::RVNG_SEEK_SET);
2090 }
2091 ascFile.addPos(pos);
2092 ascFile.addNote(f.str().c_str());
2093 return true;
2094 }
2095
2096 bool Canvas5StyleManager::readParaStyles(std::shared_ptr<Canvas5Structure::Stream> stream)
2097 {
2098 if (!stream || !stream->input())
2099 return false;
2100 auto input=stream->input();
2101 libmwaw::DebugFile &ascFile = stream->ascii();
2102 ascFile.addPos(input->tell());
2103 ascFile.addNote("Entries(ParaStyl):");
2104
2105 if (version()>=9) {
2106 return m_mainParser->readArray9(stream, "ParaStyl",
2107 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &what) {
2108 libmwaw::DebugStream f;
2109 auto &asciiFile=lStream->ascii();
2110 auto lInput=lStream->input();
2111 long endPos=lInput->tell()+item.m_length;
2112 lInput->seek(-4, librevenge::RVNG_SEEK_CUR);
2113 long pos=lInput->tell();
2114 f << what << "-" << item.m_id << ":";
2115
2116 asciiFile.addPos(pos);
2117 asciiFile.addNote(f.str().c_str());
2118 if (!readParaStyle(lStream, item.m_id))
2119 return;
2120
2121 pos=lInput->tell();
2122 f.str("");
2123 f << what << "-" << item.m_id << "[B]:";
2124 if (pos+44>endPos) {
2125 MWAW_DEBUG_MSG(("Canvas5StyleManager::readParaStyles: the zone seems too short\n"));
2126 f << "###";
2127 asciiFile.addPos(pos);
2128 asciiFile.addNote(f.str().c_str());
2129 return;
2130 }
2131 for (int i=0; i<4; ++i) { // f1=0|1, f3=0|1
2132 int val=int(lInput->readLong(2));
2133 if (val)
2134 f << "f" << i << "=" << val << ",";
2135 }
2136 asciiFile.addPos(pos);
2137 asciiFile.addNote(f.str().c_str());
2138
2139 pos=lInput->tell();
2140 f.str("");
2141 f << what << "-" << item.m_id << "[name]:";
2142 std::string name;
2143 for (int i=0; i<32; ++i) {
2144 char c=char(lInput->readLong(1));
2145 if (c==0) break;
2146 name+=c;
2147 }
2148 f << name << ",";
2149 lInput->seek(pos+32, librevenge::RVNG_SEEK_SET);
2150 int val=int(lInput->readLong(4)); // unsure can be an extended size
2151 if (val)
2152 f << "f0=" << val << ",";
2153 asciiFile.addPos(pos);
2154 asciiFile.addNote(f.str().c_str());
2155 });
2156 }
2157
2158 if (!m_mainParser->readUsed(*stream, "ParaStyl"))
2159 return false;
2160
2161 if (!m_mainParser->readExtendedHeader(stream, 0x114, "ParaStyl", &Canvas5Parser::stringDataFunction)) // string:256 + 5xlong?
2162 return false;
2163
2164 if (!m_mainParser->readExtendedHeader(stream, 0xa8, "ParaStyl",
2165 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &what) {
2166 if (!readParaStyle(lStream, item.m_id))
2167 return;
2168 auto lInput=lStream->input();
2169 long pos=lInput->tell();
2170 libmwaw::DebugStream f;
2171 auto &asciiFile=lStream->ascii();
2172 f << what << "-" << item.m_id << "[B]:";
2173 for (int i=0; i<4; ++i) { // f1=0|1, f3=0|1
2174 int val=int(lInput->readLong(2));
2175 if (val)
2176 f << "f" << i << "=" << val << ",";
2177 }
2178 asciiFile.addPos(pos);
2179 asciiFile.addNote(f.str().c_str());
2180
2181 pos=lInput->tell();
2182 f.str("");
2183 f << what << "-" << item.m_id << "[name]:";
2184 std::string name;
2185 for (int i=0; i<32; ++i) {
2186 char c=char(lInput->readLong(1));
2187 if (c==0) break;
2188 name+=c;
2189 }
2190 f << name << ",";
2191 lInput->seek(pos+32, librevenge::RVNG_SEEK_SET);
2192 asciiFile.addPos(pos);
2193 asciiFile.addNote(f.str().c_str());
2194 }))
2195 return false;
2196 if (!m_mainParser->readIndexMap(stream, "ParaStyl"))
2197 return false;
2198 std::vector<bool> defined;
2199 return m_mainParser->readDefined(*stream, defined, "ParaStyl");
2200 }
2201
2202 bool Canvas5StyleManager::readFrameStyles9(std::shared_ptr<Canvas5Structure::Stream> stream)
2203 {
2204 if (!stream || !stream->input()) return false;
2205 auto input=stream->input();
2206 auto &ascFile=stream->ascii();
2207 long pos=input->tell();
2208 libmwaw::DebugStream f;
2209 f << "Entries(FrameStyl):";
2210 ascFile.addPos(pos);
2211 ascFile.addNote(f.str().c_str());
2212
2213 if (!m_mainParser->readArray9(stream, "FrameStyl[stroke]",
2214 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
2215 auto lInput=lStream->input();
2216 libmwaw::DebugFile &asciiFile = lStream->ascii();
2217 libmwaw::DebugStream lF;
2218 lF << "St" << item.m_id << ",";
2219 if (item.m_length!=20) {
2220 MWAW_DEBUG_MSG(("Canvas5StyleManager::readFrameStyles9[stroke]: unexpected lengths\n"));
2221 lF << "###";
2222 asciiFile.addPos(item.m_pos);
2223 asciiFile.addNote(lF.str().c_str());
2224 return;
2225 }
2226 Canvas5StyleManagerInternal::Stroke style;
2227 style.m_type=item.m_type;
2228 style.m_penId=int(lInput->readLong(4));
2229 style.m_dashId=int(lInput->readLong(4));
2230 for (int i=0; i<2; ++i)
2231 style.m_arrowId[i]=int(lInput->readLong(4));
2232 int val=int(lInput->readLong(4));
2233 if (val) {
2234 MWAW_DEBUG_MSG(("Canvas5StyleManager::readFrameStyles9[stroke]: find extra value\n"));
2235 lF << "###f0=" << val << ",";
2236 }
2237 lF << style;
2238 m_state->m_idToStroke[item.m_id]=style;
2239 asciiFile.addPos(item.m_pos);
2240 asciiFile.addNote(lF.str().c_str());
2241 }))
2242 return false;
2243
2244 if (!m_mainParser->readArray9(stream, "FrameStyl[pen]",
2245 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
2246 auto lInput=lStream->input();
2247 auto &asciiFile=lStream->ascii();
2248 libmwaw::DebugStream lF;
2249 lF << "Pe" << item.m_id << ",";
2250 if (item.m_decal!=4 || item.m_length<4) {
2251 MWAW_DEBUG_MSG(("Canvas5StyleManager::readFrameStyles9[pen]: the zone seems bad\n"));
2252 lF << "###";
2253 asciiFile.addPos(item.m_pos);
2254 asciiFile.addNote(lF.str().c_str());
2255 return;
2256 }
2257 lInput->seek(4, librevenge::RVNG_SEEK_CUR);
2258 auto style=readPenStyle(*lStream, item.m_type, item.m_length-4);
2259 if (!style)
2260 lF << "###";
2261 else
2262 m_state->m_idToPen[item.m_id]=style;
2263 asciiFile.addPos(item.m_pos);
2264 asciiFile.addNote(lF.str().c_str());
2265 }))
2266 return false;
2267
2268 if (!m_mainParser->readArray9(stream, "FrameStyl[arrow]",
2269 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
2270 auto lInput=lStream->input();
2271 libmwaw::DebugStream lF;
2272 auto &asciiFile=lStream->ascii();
2273 lF << "Arrow" << item.m_id << ",";
2274 MWAWGraphicStyle::Arrow arrow;
2275 if (!readArrow(lStream, arrow, 1, item.m_length))
2276 lF << "###";
2277 else
2278 m_state->m_idToArrow[item.m_id]=arrow;
2279 asciiFile.addPos(item.m_pos);
2280 asciiFile.addNote(lF.str().c_str());
2281 }))
2282 return false;
2283
2284 return m_mainParser->readArray9(stream, "FrameStyl[dash]",
2285 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
2286 auto lInput=lStream->input();
2287 libmwaw::DebugFile &asciiFile = lStream->ascii();
2288 libmwaw::DebugStream lF;
2289 lF << "Da" << item.m_id << ",";
2290 std::vector<float> dashes;
2291 if (!readDash(*lStream, dashes, 1, item.m_length))
2292 lF << "###";
2293 else
2294 m_state->m_idToDash[item.m_id]=dashes;
2295 asciiFile.addPos(item.m_pos);
2296 asciiFile.addNote(lF.str().c_str());
2297 });
2298 }
2299
2300 ////////////////////////////////////////////////////////////
2301 //
2302 // Windows resource
2303 //
2304 ////////////////////////////////////////////////////////////
2305
2306 ////////////////////////////////////////////////////////////
2307 //
2308 // send data
2309 //
2310 ////////////////////////////////////////////////////////////
2311 bool Canvas5StyleManager::updateLineColor(int cId, MWAWGraphicStyle &style)
2312 {
2313 auto it=m_state->m_idToColor.find(cId);
2314 if (it==m_state->m_idToColor.end() || !it->second) {
2315 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateLineColor: can not find color %d\n", cId));
2316 return false;
2317 }
2318 return updateLineColor(*it->second, style);
2319 }
2320
2321 bool Canvas5StyleManager::updateLineColor(Canvas5StyleManagerInternal::ColorStyle const &color, MWAWGraphicStyle &style)
2322 {
2323 switch (color.m_type) {
2324 case 0: // checkme
2325 style.m_lineOpacity=0;
2326 break;
2327 case 1:
2328 if (!color.m_color.isSet()) {
2329 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateLineColor: can not find color\n"));
2330 break;
2331 }
2332 style.m_lineColor=*color.m_color;
2333 if (color.m_color->getAlpha()!=255)
2334 style.m_lineOpacity=float(color.m_color->getAlpha())/255;
2335 break;
2336 case 0x50415453: // pats
2337 case 0x54585552: // texture
2338 if (color.m_color.isSet())
2339 style.m_lineColor=*color.m_color;
2340 break;
2341 case 0x68746368: // hatch
2342 case 0x766b666c: // vkfl
2343 if (color.m_color.isSet())
2344 style.m_lineColor=*color.m_color;
2345 break;
2346 case 0x4f62466c: // ObFl
2347 if (color.m_gradient.hasGradient())
2348 color.m_gradient.getAverageColor(style.m_lineColor);
2349 break;
2350 default:
2351 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateLineColor: can not send type=%s\n", Canvas5Structure::getString(color.m_type).c_str()));
2352 break;
2353 }
2354 return true;
2355 }
2356
2357 bool Canvas5StyleManager::updateSurfaceColor(int cId, MWAWGraphicStyle &style)
2358 {
2359 auto it=m_state->m_idToColor.find(cId);
2360 if (it==m_state->m_idToColor.end() || !it->second) {
2361 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateSurfaceColor: can not find color %d\n", cId));
2362 return false;
2363 }
2364 return updateSurfaceColor(*it->second, style);
2365 }
2366
2367 bool Canvas5StyleManager::updateSurfaceColor(Canvas5StyleManagerInternal::ColorStyle const &color, MWAWGraphicStyle &style)
2368 {
2369 switch (color.m_type) {
2370 case 0:
2371 style.m_surfaceOpacity=0;
2372 break;
2373 case 1:
2374 if (color.m_color.isSet())
2375 style.setSurfaceColor(*color.m_color, float(color.m_color->getAlpha())/255);
2376 else {
2377 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateSurfaceColor: can not find the color\n"));
2378 return false;
2379 }
2380 break;
2381 case 0x50415453: // pats
2382 case 0x54585552: // txur
2383 if (color.m_texture.isEmpty()) {
2384 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateSurfaceColor: can not find the texture\n"));
2385 return false;
2386 }
2387 style.setPattern(MWAWGraphicStyle::Pattern(color.m_textureDim, color.m_texture, *color.m_color));
2388 break;
2389 case 0x4f62466c:
2390 if (!color.m_gradient.hasGradient()) {
2391 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateSurfaceColor: can not find the gradient\n"));
2392 return false;
2393 }
2394 style.m_gradient=color.m_gradient;
2395 break;
2396 case 0x68746368:
2397 if (!color.m_hatch.hasHatch()) {
2398 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateSurfaceColor: can not find the hatch\n"));
2399 return false;
2400 }
2401 style.m_hatch=color.m_hatch;
2402 if (color.m_color.isSet())
2403 style.setSurfaceColor(*color.m_color, float(color.m_color->getAlpha())/255);
2404 break;
2405 case 0x766b666c: { // vkfl
2406 if (color.m_color.isSet()) {
2407 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateSurfaceColor: can not find the symbol color\n"));
2408 return false;
2409 }
2410 style.setSurfaceColor(*color.m_color, float(color.m_color->getAlpha())/255);
2411 static bool first=true;
2412 if (first) {
2413 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateSurfaceColor: symbol color are replaced by background color\n"));
2414 first=false;
2415 }
2416 break;
2417 }
2418 default:
2419 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateSurfaceColor: can not send type=%s\n", Canvas5Structure::getString(color.m_type).c_str()));
2420 break;
2421 }
2422 return true;
2423 }
2424
2425 bool Canvas5StyleManager::updateLine(Canvas5StyleManagerInternal::PenStyle const &pen, MWAWGraphicStyle &style, int &numLines, int lineId, float *offset)
2426 {
2427 numLines=1;
2428 if (offset) *offset=0;
2429 style.m_lineWidth=0;
2430
2431 switch (pen.m_type) {
2432 case 1:
2433 style.m_lineWidth=(pen.m_size[0]+pen.m_size[1])/2;
2434 break;
2435 case 0x766e656f: { // vneo
2436 style.m_lineWidth=(pen.m_size[0]+pen.m_size[1])/2;
2437 // fixme: normally a gradient, let's replace it by it barycenters color...
2438 style.m_lineColor=MWAWColor::barycenter(0.5, *pen.m_colors[0], 0.5, *pen.m_colors[1]);
2439 static bool first=true;
2440 if (first) {
2441 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateLine: replace line gradient with their average color\n"));
2442 first=false;
2443 }
2444 break;
2445 }
2446 case 0x706c696e: // plin
2447 numLines=int(pen.m_lines.size());
2448 if ((lineId>=0 && lineId<numLines) || (numLines==1 && lineId<0)) {
2449 auto const &line = pen.m_lines[size_t(lineId)];
2450 style.m_lineWidth=(line.m_size[0]+line.m_size[1])/2;
2451 style.m_lineColor=*line.m_color;
2452 if (offset) *offset=line.m_offset;
2453 }
2454 else if (lineId>=0) {
2455 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateLine: can not find the line with: %d\n", lineId));
2456 return false;
2457 }
2458 break;
2459 default:
2460 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateLineStyle: can not send pen with type %s\n", Canvas5Structure::getString(pen.m_type).c_str()));
2461 return false;
2462 }
2463 return true;
2464 }
2465
2466 bool Canvas5StyleManager::updateLineStyle(int sId, MWAWGraphicStyle &style, int &numLines, int lineId, float *offset)
2467 {
2468 numLines=1;
2469 if (offset) *offset=0;
2470 auto it=m_state->m_idToStroke.find(sId);
2471 if (it==m_state->m_idToStroke.end()) {
2472 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateLineStyle: can not find stroke %d\n", sId));
2473 return false;
2474 }
2475 auto const &stroke=it->second;
2476 style.m_lineWidth=0;
2477 if (stroke.m_penId) {
2478 auto pIt=m_state->m_idToPen.find(stroke.m_penId);
2479 if (pIt==m_state->m_idToPen.end() || !pIt->second) {
2480 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateLineStyle: can not find pen %d\n", stroke.m_penId));
2481 }
2482 else
2483 updateLine(*pIt->second, style, numLines, lineId, offset);
2484 }
2485 if (stroke.m_dashId) {
2486 auto dIt=m_state->m_idToDash.find(stroke.m_dashId);
2487 if (dIt==m_state->m_idToDash.end()) {
2488 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateLineStyle: can not find dash %d\n", stroke.m_dashId));
2489 }
2490 else
2491 style.m_lineDashWidth=dIt->second;
2492 }
2493 for (int i=0; i<2; ++i) {
2494 if (!stroke.m_arrowId[i])
2495 continue;
2496 auto dIt=m_state->m_idToArrow.find(stroke.m_arrowId[i]);
2497 if (dIt==m_state->m_idToArrow.end()) {
2498 MWAW_DEBUG_MSG(("Canvas5StyleManager::updateLineStyle: can not find arrow %d\n", stroke.m_arrowId[i]));
2499 }
2500 else
2501 style.m_arrows[i]=dIt->second;
2502 }
2503 return true;
2504 }
2505
2506 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: