Debian Codegraph @

src/lib/LotusStyleManager.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2006, 2007 Andrew Ziem
11 * Copyright (C) 2004 Marc Maurer ([email protected])
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 *
14 * For minor contributions see the git repository.
15 *
16 * Alternatively, the contents of this file may be used under the terms
17 * of the GNU Lesser General Public License Version 2.1 or later
18 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
19 * applicable instead of those above.
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <cmath>
26 #include <sstream>
27 #include <limits>
28 #include <stack>
29
30 #include <librevenge-stream/librevenge-stream.h>
31
32 #include "libwps_internal.h"
33 #include "libwps_tools_win.h"
34
35 #include "WKSContentListener.h"
36 #include "WPSCell.h"
37 #include "WPSEntry.h"
38 #include "WPSFont.h"
39 #include "WPSGraphicShape.h"
40 #include "WPSGraphicStyle.h"
41 #include "WPSStream.h"
42
43 #include "Lotus.h"
44
45 #include "LotusStyleManager.h"
46
47 namespace LotusStyleManagerInternal
48 {
49 //! small struct used to defined a font name
50 struct FontName
51 {
52 //! constructor
53 FontName()
54 : m_name()
55 , m_id(-2)
56 {
57 for (int &i : m_size) i=0;
58 }
59 //! the font name
60 std::string m_name;
61 //! the font id
62 int m_id;
63 //! the font height, font size
64 int m_size[2];
65 };
66 //! small struct used to defined color style
67 struct ColorStyle
68 {
69 //! constructor
70 ColorStyle()
71 : m_patternId(0)
72 , m_pattern()
73 , m_extra("")
74 {
75 m_colors[0]=m_colors[1]=m_colors[3]=WPSColor::white();
76 m_colors[2]=WPSColor::black();
77 }
78 //! operator<<
79 friend std::ostream &operator<<(std::ostream &o, ColorStyle const &color)
80 {
81 for (int i=0; i<4; ++i)
82 {
83 if ((i==2 && color.m_colors[i].isBlack()) || (i!=2 && color.m_colors[i].isWhite()))
84 continue;
85 static char const *wh[]= {"unkn0", "unkn1", "line", "surf" };
86 o << "color[" << wh[i] << "]=" << color.m_colors[i] << ",";
87 }
88 if (color.m_patternId) // 0: none, 2:full
89 o << "id[pattern]=" << color.m_patternId;
90 if (!color.m_pattern.empty())
91 o << "[" << color.m_pattern << "%],";
92 o << color.m_extra;
93 return o;
94 }
95 //! the color id : unknown0, unknown1, line, surface
96 WPSColor m_colors[4];
97 //! the pattern id
98 int m_patternId;
99 //! the pattern
100 WPSGraphicStyle::Pattern m_pattern;
101 //! extra data
102 std::string m_extra;
103 };
104
105 //! small struct used to defined font style
106 struct FontStyle
107 {
108 //! constructor
109 explicit FontStyle(libwps_tools_win::Font::Type fontType)
110 : m_font()
111 , m_fontType(fontType)
112 , m_fontId(0)
113 , m_extra("")
114 {
115 }
116 //! operator<<
117 friend std::ostream &operator<<(std::ostream &o, FontStyle const &font)
118 {
119 o << font.m_font;
120 if (font.m_fontId) o << "FN" << font.m_fontId << ",";
121 o << font.m_extra;
122 return o;
123 }
124 //! the font
125 WPSFont m_font;
126 //! the font type
127 libwps_tools_win::Font::Type m_fontType;
128 //! the font id
129 int m_fontId;
130 //! extra data
131 std::string m_extra;
132 };
133
134 //! small struct used to defined format style
135 struct FormatStyle
136 {
137 //! constructor
138 FormatStyle()
139 : m_prefix("")
140 , m_suffix("")
141 , m_extra("")
142 {
143 }
144 //! operator<<
145 friend std::ostream &operator<<(std::ostream &o, FormatStyle const &format)
146 {
147 if (!format.m_prefix.empty())
148 o << "prefix=" << format.m_prefix << ",";
149 if (!format.m_suffix.empty())
150 o << "suffix=" << format.m_suffix << ",";
151 o << format.m_extra;
152 return o;
153 }
154 //! the prefix
155 std::string m_prefix;
156 //! the suffix
157 std::string m_suffix;
158 //! extra data
159 std::string m_extra;
160 };
161
162 //! small struct used to defined line style
163 struct LineStyle
164 {
165 //! constructor
166 LineStyle()
167 : m_width(1)
168 , m_color(WPSColor::black())
169 , m_dashId(0)
170 , m_extra("")
171 {
172 }
173 //! operator<<
174 friend std::ostream &operator<<(std::ostream &o, LineStyle const &line)
175 {
176 if (line.m_width<1 || line.m_width>1)
177 o << "w=" << line.m_width << ",";
178 if (!line.m_color.isBlack())
179 o << "color=" << line.m_color << ",";
180 if (line.m_dashId)
181 o << "dashId=" << line.m_dashId << ",";
182 o << line.m_extra;
183 return o;
184 }
185 //! the line width
186 float m_width;
187 //! the line color
188 WPSColor m_color;
189 //! the dash id
190 int m_dashId;
191 //! extra data
192 std::string m_extra;
193 };
194
195 //! small struct used to defined cell style
196 struct CellStyle
197 {
198 //! constructor
199 explicit CellStyle(libwps_tools_win::Font::Type fontType)
200 : m_borders(0)
201 , m_fontId(0)
202 , m_formatId(0)
203 , m_colorStyle()
204 , m_fontStyle(fontType)
205 , m_hAlign(WPSCellFormat::HALIGN_DEFAULT)
206 , m_vAlign(WPSCellFormat::VALIGN_DEFAULT)
207 , m_wrapping(WPSCellFormat::WRAP_DEFAULT)
208 , m_rotation(0)
209 , m_extra("")
210 {
211 for (int i=0; i<4; ++i)
212 {
213 m_bordersId[i]=0;
214 m_bordersStyle[i].m_style=WPSBorder::None;
215 }
216 for (int &i : m_colorsId) i=0;
217 }
218 //! operator<<
219 friend std::ostream &operator<<(std::ostream &o, CellStyle const &cell)
220 {
221 if (cell.m_fontId)
222 o << "id[font]=FS" << cell.m_fontId << ",";
223 for (int i=0; i<4; ++i)
224 {
225 if (cell.m_bordersId[i])
226 o << "bord" << i << "=Li" << cell.m_bordersId[i] << ",";
227 }
228 for (int i=0; i<2; ++i)
229 {
230 if (cell.m_colorsId[i])
231 o << (i==0 ? "color" : "color[shadow]") << "=Co" << cell.m_colorsId[i] << ",";
232 }
233 if (cell.m_rotation)
234 o << "rotation=" << cell.m_rotation << ",";
235 switch (cell.m_hAlign)
236 {
237 case WPSCellFormat::HALIGN_LEFT:
238 o << "left,";
239 break;
240 case WPSCellFormat::HALIGN_CENTER:
241 o << "centered,";
242 break;
243 case WPSCellFormat::HALIGN_RIGHT:
244 o << "right,";
245 break;
246 case WPSCellFormat::HALIGN_FULL:
247 o << "full,";
248 break;
249 case WPSCellFormat::HALIGN_DEFAULT:
250 default:
251 break; // default
252 }
253 switch (cell.m_vAlign)
254 {
255 case WPSCellFormat::VALIGN_TOP:
256 o << "top,";
257 break;
258 case WPSCellFormat::VALIGN_CENTER:
259 o << "yCentered,";
260 break;
261 case WPSCellFormat::VALIGN_BOTTOM:
262 o << "bottom,";
263 break;
264 case WPSCellFormat::VALIGN_DEFAULT:
265 default:
266 break; // default
267 }
268 switch (cell.m_wrapping)
269 {
270 case WPSCellFormat::WRAP_WRAP:
271 o << "wrap,";
272 break;
273 case WPSCellFormat::WRAP_NO_WRAP:
274 o << "wrap[no],";
275 break;
276 case WPSCellFormat::WRAP_DEFAULT:
277 default:
278 break;
279 }
280 if (cell.m_borders)
281 {
282 o << "bord=";
283 for (int i=0,depl=1; i<4; ++i, depl*=2)
284 {
285 if (cell.m_borders&depl)
286 {
287 static char const *wh[]= {"T","L","B","R"};
288 o << wh[i];
289 }
290 }
291 o << ",";
292 }
293 if (cell.m_formatId)
294 o << "id[format]=Fo" << cell.m_formatId << ",";
295 o << cell.m_extra;
296 return o;
297 }
298 //! the borders
299 int m_borders;
300 //! the border line id
301 int m_bordersId[4];
302 //! the color id : surface, shadow ?
303 int m_colorsId[2];
304 //! the font id
305 int m_fontId;
306 //! the format id
307 int m_formatId;
308 // wk5
309 //! the color style
310 ColorStyle m_colorStyle;
311 //! the font style
312 FontStyle m_fontStyle;
313 //! the horizontal align
314 WPSCellFormat::HorizontalAlignment m_hAlign;
315 //! the vertical align
316 WPSCellFormat::VerticalAlignment m_vAlign;
317 //! the wrapping
318 WPSCellFormat::Wrapping m_wrapping;
319 //! the rotation
320 int m_rotation;
321 //! the cell border
322 WPSBorder m_bordersStyle[4];
323 //! extra data
324 std::string m_extra;
325 };
326
327 //! small struct used to defined cell style file entry
328 struct CellStyleEntry
329 {
330 //! constructor
331 explicit CellStyleEntry(std::shared_ptr<WPSStream> const &stream)
332 : m_stream(stream)
333 , m_parentId(-1)
334 , m_entry()
335 {
336 }
337 //! the main strem
338 std::shared_ptr<WPSStream> m_stream;
339 //! the parent style id
340 int m_parentId;
341 //! the zone position
342 WPSEntry m_entry;
343 };
344
345 //! small struct used to defined graphic style
346 struct GraphicStyle
347 {
348 //! constructor
349 GraphicStyle()
350 : m_lineId(0)
351 , m_lineStyle()
352 , m_patternId(0)
353 , m_extra("")
354 {
355 for (int &i : m_colorsId) i=-1;
356 }
357 //! operator<<
358 friend std::ostream &operator<<(std::ostream &o, GraphicStyle const &graphic)
359 {
360 if (graphic.m_lineId)
361 o << "L" << graphic.m_lineId << ",";
362 if (graphic.m_colorsId[0]>=0)
363 o << "Co" << graphic.m_colorsId[0] << ",";
364 if (graphic.m_colorsId[1]>=0)
365 o << "shadow[color]=Co" << graphic.m_colorsId[1] << ",";
366 o << graphic.m_extra;
367 return o;
368 }
369 //! the line, shadow, surface, back pattern color id
370 int m_colorsId[4];
371 //! the border line id
372 int m_lineId;
373 //! the line style: 123 pc
374 LineStyle m_lineStyle;
375 //! the patten id: 123 pc
376 int m_patternId;
377 //! extra data
378 std::string m_extra;
379 };
380
381 //! the state of LotusStyleManager
382 struct State
383 {
384 //! constructor
385 State()
386 : m_version(-1)
387 , m_isUpdated(false)
388 , m_idCellStyleMap()
389 , m_idColorStyleMap()
390 , m_idFontStyleMap()
391 , m_idFormatStyleMap()
392 , m_idGraphicStyleMap()
393 , m_idLineStyleMap()
394 , m_idFontNameMap()
395 , m_idToParentStyleMap()
396 , m_idToCellStyleEntryMap()
397 {
398 }
399 //! returns a color corresponding to an id between 0 and 7
400 static bool getColor8(int id, WPSColor &color);
401 //! returns a color corresponding to an id between 0 and 15
402 static bool getColor16(int id, WPSColor &color);
403 //! returns a color corresponding to an id
404 static bool getColor256(int id, WPSColor &color);
405 //! returns the pattern corresponding to a pattern id between 1 and 48
406 static bool getPattern48(int id, WPSGraphicStyle::Pattern &pattern);
407 //! returns the pattern corresponding to a pattern id between 1 and 64
408 static bool getPattern64(int id, WPSGraphicStyle::Pattern &pattern);
409
410 //! the file version
411 int m_version;
412 //! a flag to know if updateState was launched
413 bool m_isUpdated;
414 //! a map id to cell style
415 std::map<int, CellStyle> m_idCellStyleMap;
416 //! a map id to color style
417 std::map<int, ColorStyle> m_idColorStyleMap;
418 //! a map id to font style
419 std::map<int, FontStyle> m_idFontStyleMap;
420 //! a map id to format style
421 std::map<int, FormatStyle> m_idFormatStyleMap;
422 //! a map id to graphic style
423 std::map<int, GraphicStyle> m_idGraphicStyleMap;
424 //! a map id to line style
425 std::map<int, LineStyle> m_idLineStyleMap;
426
427 //! a map id to font name style
428 std::map<int, FontName> m_idFontNameMap;
429 //! a map style id to the cell style id
430 std::map<int, int> m_idToParentStyleMap;
431 //! a map cell style id to the cell style entry
432 std::map<int, CellStyleEntry> m_idToCellStyleEntryMap;
433 };
434
435 bool State::getColor8(int id, WPSColor &color)
436 {
437 if (id<0||id>=8)
438 {
439 WPS_DEBUG_MSG(("LotusStyleManagerInteranl::State::getColor8(): unknown color id: %d\n",id));
440 return false;
441 }
442 static uint32_t colorMap[]=
443 {
444 // black, dark blue, green, cyan/gray, red, magenta, yellow, white?
445 0, 0xFF, 0xFF00, 0x7F7F7F, 0xFF0000, 0xFF00FF, 0xFFFF00, 0xFFFFFF
446 };
447 color=WPSColor(colorMap[id]);
448 return true;
449 }
450
451 bool State::getColor16(int id, WPSColor &color)
452 {
453 if (id<0||id>=16)
454 {
455 WPS_DEBUG_MSG(("LotusStyleManagerInteranl::State::getColor16(): unknown color id: %d\n",id));
456 return false;
457 }
458 static uint32_t colorMap[]=
459 {
460 0, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF,
461 0x7F0000, 0x007F00, 0x00007F, 0x7F7F00, 0x7F007F, 0x007F7F, 0x7F7F7F, 0x3F3F3F
462 };
463 color=WPSColor(colorMap[id]);
464 return true;
465 }
466
467 bool State::getColor256(int id, WPSColor &color)
468 {
469 if (id<0||id>=256)
470 {
471 WPS_DEBUG_MSG(("LotusStyleManagerInteranl::State::getColor256(): unknown color id: %d\n",id));
472 return false;
473 }
474 // in one file, find 0xEF as current...
475 static uint32_t colorMap[]=
476 {
477 0xffffff, 0xffcc99, 0xffffcc, 0xccff99, 0x99ff33, 0x99ff66, 0x99ff99, 0xccffcc, // 0
478 0xccffff, 0x99ccff, 0x6699ff, 0xccccff, 0xcc99ff, 0xffccff, 0xff99cc, 0xffffff,
479 0xffcccc, 0xffcc66, 0xffff99, 0xccff66, 0x99ff00, 0x66ff33, 0x66ff99, 0x99ffcc, // 10
480 0x99ffff, 0x3399ff, 0x6666ff, 0x9999ff, 0xcc66ff, 0xff99ff, 0xff66cc, 0xeeeeee,
481 0xff9999, 0xff9966, 0xffff66, 0xccff33, 0x66ff00, 0x66ff66, 0x33ff99, 0x66ffcc, // 20
482 0x66ffff, 0x0099ff, 0x3366ff, 0x9966ff, 0xcc66cc, 0xff66ff, 0xff6699, 0xdddddd,
483 0xff6666, 0xff9933, 0xffff33, 0xccff00, 0x33ff00, 0x33ff66, 0x00ff99, 0x33ffcc, // 30
484 0x33ffff, 0x0066ff, 0x0066cc, 0x9966cc, 0xcc33ff, 0xff33ff, 0xff3399, 0xcccccc,
485 0xff3333, 0xff6633, 0xffff00, 0xcccc33, 0x00ff00, 0x00ff66, 0x66cc99, 0x00ffcc, // 40
486 0x00ffff, 0x0033ff, 0x3366cc, 0x9933ff, 0xcc00ff, 0xff33cc, 0xff3366, 0xbbbbbb,
487 0xff0000, 0xff6600, 0xffcc33, 0xcccc00, 0x00ee00, 0x33ff33, 0x33cc99, 0x66cccc, // 50
488 0x66ccff, 0x0000ee, 0x3333ff, 0x9900ff, 0xcc00cc, 0xff00cc, 0xff0066, 0xaaaaaa,
489 0xcc0000, 0xff3300, 0xffcc00, 0x99cc33, 0x00dd00, 0x00ff33, 0x00cc99, 0x33cccc, // 60
490 0x33ccff, 0x0000dd, 0x3300ff, 0x6666cc, 0x9933cc, 0xcc33cc, 0xff0033, 0x999999,
491 0xbb0000, 0xee0000, 0xff9900, 0x99cc00, 0x00bb00, 0x33cc00, 0x33cc66, 0x00cccc, // 70
492 0x00ccff, 0x0000bb, 0x0000ff, 0x6633ff, 0x993399, 0xcc3399, 0xcc0033, 0x888888,
493 0xaa0000, 0xdd0000, 0xcc9933, 0x999933, 0x00aa00, 0x33cc33, 0x00cc66, 0x009999, // 80
494 0x0099cc, 0x0000aa, 0x0033cc, 0x6633cc, 0x9900cc, 0xcc0099, 0xcc0066, 0x777777,
495 0x990000, 0xcc3333, 0xcc9900, 0x999900, 0x008800, 0x00cc00, 0x339966, 0x339999, // 90
496 0x3399cc, 0x000088, 0x0000cc, 0x6600ff, 0x663399, 0x993366, 0xcc3366, 0x666666,
497 0x660000, 0xcc3300, 0xcc6633, 0x669900, 0x007700, 0x339933, 0x009966, 0x336666, // a0
498 0x336699, 0x000077, 0x3300cc, 0x3333cc, 0x663366, 0x990066, 0x990033, 0x555555,
499 0x550000, 0x993300, 0xcc6600, 0x669933, 0x005500, 0x339900, 0x336633, 0x006666, // b0
500 0x006699, 0x000055, 0x000099, 0x333399, 0x6600cc, 0x990099, 0x880000, 0x444444,
501 0x330000, 0x663300, 0x996633, 0x336600, 0x004400, 0x009900, 0x006633, 0x333333, // c0
502 0x003399, 0x000044, 0x000066, 0x330099, 0x660099, 0x660066, 0x770000, 0x333333,
503 0x220000, 0x440000, 0x996600, 0x333300, 0x002200, 0x006600, 0x003300, 0x003333, // d0
504 0x003366, 0x000022, 0x000033, 0x330066, 0x330033, 0x660033, 0x440000, 0x222222,
505 0xcc9966, 0xcc6666, 0xcccc99, 0xcccc66, 0x99cc66, 0x66cc66, 0x99cc99, 0x99ffcc, // e0
506 0x99cccc, 0x999999, 0x6699cc, 0x9999cc, 0xcc99cc, 0xcc9999, 0xcc6699, 0x111111,
507 0x996666, 0x993333, 0x999966, 0x666633, 0x66cc33, 0x009933, 0x669966, 0x66cc99, // f0
508 0x669999, 0x666666, 0x666699, 0x333366, 0x996699, 0x663333, 0x663366, 0x000000
509 };
510 color=WPSColor(colorMap[id]);
511 return true;
512 }
513
514 bool State::getPattern48(int id, WPSGraphicStyle::Pattern &pat)
515 {
516 if (id<=0 || id>=49)
517 {
518 WPS_DEBUG_MSG(("LotusStyleManagerInternal::State::getPattern48(): unknown pattern id: %d\n",id));
519 return false;
520 }
521 if (id==47 || id==48)
522 {
523 // the gradient
524 static const uint16_t patterns[]=
525 {
526 0x8814, 0x2241, 0x8800, 0xaa00, 0x2050, 0x8888, 0x8888, 0x502
527 };
528 pat.m_dim=Vec2i(8,8);
529 uint16_t const *ptr=&patterns[4*(id-47)];
530 pat.m_data.resize(8);
531 for (size_t i=0; i < 8; i+=2)
532 {
533 uint16_t val=*(ptr++);
534 pat.m_data[i]=static_cast<unsigned char>((val>>8) & 0xFF);
535 pat.m_data[i+1]=static_cast<unsigned char>(val & 0xFF);
536 }
537 }
538 return getPattern64(id, pat);
539 }
540
541 bool State::getPattern64(int id, WPSGraphicStyle::Pattern &pat)
542 {
543 if (id<=0 || id>=64)
544 {
545 WPS_DEBUG_MSG(("LotusStyleManagerInternal::State::getPattern64(): unknown pattern id: %d\n",id));
546 return false;
547 }
548 static const uint16_t patterns[]=
549 {
550 0xffff, 0xffff, 0xffff, 0xffff, 0x0, 0x0, 0x0, 0x0, 0x50a, 0x1428, 0x50a0, 0x4182, 0xa851, 0xa245, 0x8a15, 0x2a54, // 1-4
551 0x2142, 0x8409, 0x1224, 0x4890, 0x102, 0x408, 0x1020, 0x4080, 0x1122, 0x4488, 0x1122, 0x4488, 0xeedd, 0xbb77, 0xeedd, 0xbb77,
552 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa, 0x8888, 0x8888, 0x8888, 0x8888, 0xa050, 0x2814, 0xa05, 0x8241, 0x158a, 0x45a2, 0x51a8, 0x542a, // 9-12
553 0x9048, 0x2412, 0x984, 0x4221, 0x8040, 0x2010, 0x804, 0x201, 0xff00, 0xff00, 0xff00, 0xff00, 0xff00, 0x0, 0xff00, 0x0,
554 0x44aa, 0x11aa, 0x44aa, 0x11aa, 0x182, 0x4428, 0x1028, 0x4482, 0xf874, 0x2247, 0x8f17, 0x2271, 0xaa00, 0xaa00, 0xaa00, 0xaa00, // 17-20
555 0xff88, 0x8888, 0xff88, 0x8888, 0xff80, 0x8080, 0x8080, 0x8080, 0xaa00, 0x8000, 0x8800, 0x8000, 0xbf00, 0xbfbf, 0xb0b0, 0xb0b0,
556 0xddff, 0x77ff, 0xddff, 0x77ff, 0xdd77, 0xdd77, 0xdd77, 0xdd77, 0xaa55, 0xaa55, 0xaa55, 0xaa55, 0x8822, 0x8822, 0x8822, 0x8822, // 25-28
557 0x8010, 0x220, 0x108, 0x4004, 0x8800, 0x2200, 0x8800, 0x2200, 0x8000, 0x800, 0x8000, 0x800, 0x40a0, 0x0, 0x40a, 0x0,
558 0x8040, 0x2000, 0x204, 0x800, 0x8000, 0x0, 0x0, 0x0, 0xb130, 0x31b, 0xd8c0, 0xc8d, 0xff80, 0x8080, 0xff08, 0x808, // 33-36
559 0x81c, 0x22c1, 0x8001, 0x204, 0x8244, 0x3944, 0x8201, 0x101, 0x55a0, 0x4040, 0x550a, 0x404, 0x384, 0x4830, 0xc02, 0x101,
560 0x8080, 0x413e, 0x808, 0x14e3, 0x1020, 0x54aa, 0xff02, 0x408, 0x7789, 0x8f8f, 0x7798, 0xf8f8, 0x8, 0x142a, 0x552a, 0x1408, // 41-44
561 0xf0f0, 0xf0f0, 0xf0f, 0xf0f, 0x9966, 0x6699, 0x9966, 0x6699, 0x4188, 0x00cc, 0x8008, 0x1422, 0x8888, 0x8805, 0x220, 0x4184,
562 0xff, 0xff00, 0xff, 0xff00, 0x55aa, 0x55aa, 0x55aa, 0x55aa, 0xff55, 0xff55, 0xff55, 0xff55, 0x8142, 0x2418, 0x1824, 0x4281, // 49-52
563 0xc0c0, 0xc0c0, 0xc0c0, 0xc0c0, 0x3399, 0xcc66, 0x3399, 0xcc66, 0x3366, 0xcc99, 0x3366, 0xcc99, 0x1188, 0x4422, 0x1188, 0x4422,
564 0xffcc, 0xff33, 0xffcc, 0xff33, 0xf0f0, 0x0f0f, 0xf0f0, 0x0f0f, 0xcc33, 0x3333, 0x33cc, 0xcccc, 0xf0f, 0xf0f, 0xf0f, 0xf0f,// 57-60
565 0xf0f0, 0xf0f0, 0xf0f0, 0xf0f0, 0, 0, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0
566 };
567 pat.m_dim=Vec2i(8,8);
568 uint16_t const *ptr=&patterns[4*(id-1)];
569 pat.m_data.resize(8);
570 for (size_t i=0; i < 8; i+=2)
571 {
572 uint16_t val=*(ptr++);
573 pat.m_data[i]=static_cast<unsigned char>((val>>8) & 0xFF);
574 pat.m_data[i+1]=static_cast<unsigned char>(val & 0xFF);
575 }
576 return true;
577 }
578
579 }
580
581 // constructor, destructor
582 LotusStyleManager::LotusStyleManager(LotusParser &parser)
583 : m_mainParser(parser)
584 , m_state(new LotusStyleManagerInternal::State)
585 {
586 }
587
588 LotusStyleManager::~LotusStyleManager()
589 {
590 }
591
592 void LotusStyleManager::cleanState()
593 {
594 m_state.reset(new LotusStyleManagerInternal::State);
595 }
596
597 void LotusStyleManager::updateState()
598 {
599 if (m_state->m_isUpdated)
600 return;
601 m_state->m_isUpdated=true;
602 // try to update the font styles
603 for (auto &fIt : m_state->m_idFontStyleMap)
604 {
605 LotusStyleManagerInternal::FontStyle &font=fIt.second;
606 if (!font.m_fontId) continue;
607 WPSFont defFont;
608 if (!m_mainParser.getFont(font.m_fontId, defFont, font.m_fontType))
609 continue;
610 font.m_font.m_name = defFont.m_name;
611 }
612 }
613
614 int LotusStyleManager::version() const
615 {
616 if (m_state->m_version<0)
617 m_state->m_version=m_mainParser.version();
618 return m_state->m_version;
619 }
620
621 bool LotusStyleManager::getColor8(int cId, WPSColor &color) const
622 {
623 return m_state->getColor8(cId, color);
624 }
625
626 bool LotusStyleManager::getColor16(int cId, WPSColor &color) const
627 {
628 return m_state->getColor16(cId, color);
629 }
630
631 bool LotusStyleManager::getColor256(int cId, WPSColor &color) const
632 {
633 return m_state->getColor256(cId, color);
634 }
635
636 bool LotusStyleManager::getPattern64(int id, WPSGraphicStyle::Pattern &pattern) const
637 {
638 return m_state->getPattern64(id, pattern);
639 }
640
641 ////////////////////////////////////////////////////////////
642 // styles
643 ////////////////////////////////////////////////////////////
644 bool LotusStyleManager::readLineStyle(std::shared_ptr<WPSStream> stream, long endPos, int vers)
645 {
646 if (!stream) return false;
647 RVNGInputStreamPtr &input = stream->m_input;
648 libwps::DebugFile &ascFile=stream->m_ascii;
649 libwps::DebugStream f;
650
651 long pos = input->tell();
652 const int expectedSize=vers==0 ? 8 : vers==1 ? 14 : 0;
653 if (endPos-pos!=expectedSize) // only find in a WK3 mac file
654 {
655 WPS_DEBUG_MSG(("LotusStyleManager::readLineStyle: the zone size seems bad\n"));
656 ascFile.addPos(pos-6);
657 ascFile.addNote("Entries(LineStyle):###");
658 return true;
659 }
660 LotusStyleManagerInternal::LineStyle line;
661 auto id=int(libwps::readU8(input));
662 auto val=int(libwps::readU8(input)); // always 10?
663 if (val!=0x10)
664 f << "fl=" << std::hex << val << std::dec << ",";
665 val=int(libwps::readU16(input)); // 0 or small number
666 if (val) f << "f0=" << val << ",";
667 WPSColor color[2]= {WPSColor::black(), WPSColor::white()};
668 for (int i=0; i<2; ++i)
669 {
670 int col=vers==1 ? int(libwps::readU16(input)) : int(libwps::readU8(input));
671 if (col!=0xEF && !m_state->getColor256(col, color[i]))
672 {
673 f << "###col" << i << "=" << col << ",";
674 continue;
675 }
676 if ((i==0 && color[0].isBlack()) || (i==1 && color[1].isWhite()))
677 continue;
678 f << "col" << i << "=" << color[i] << ",";
679 }
680 WPSColor finalColor=color[0];
681 int patId;
682 if (vers==0)
683 {
684 val=int(libwps::readU16(input));
685 patId=(val&0x3f);
686 line.m_width=float((val>>6)&0xF);
687 line.m_dashId=(val>>11);
688 }
689 else // checkme
690 {
691 patId=int(libwps::readU16(input));
692 line.m_width=float(libwps::readU16(input))/256.f;
693 line.m_dashId=int(libwps::readU16(input));
694 }
695 if (patId!=1)
696 {
697 f << "pattern=" << patId << ",";
698 WPSGraphicStyle::Pattern pattern;
699 if (patId==0) // no pattern
700 line.m_width=0;
701 else if (patId==2)
702 finalColor=color[1];
703 else if (m_state->getPattern48(patId, pattern))
704 {
705 pattern.m_colors[0]=color[1];
706 pattern.m_colors[1]=color[0];
707 pattern.getAverageColor(finalColor);
708 }
709 }
710 if (line.m_dashId) // no plain, so ...
711 finalColor=WPSColor::barycenter(0.5f, finalColor, 0.5f, WPSColor::white());
712 line.m_color=finalColor;
713 line.m_extra=f.str();
714
715 f.str("");
716 f << "Entries(LineStyle):L" << id << "," << line;
717 if (m_state->m_idLineStyleMap.find(id)!=m_state->m_idLineStyleMap.end())
718 {
719 WPS_DEBUG_MSG(("LotusStyleManager::readLineStyle: the line style %d already exists\n", id));
720 f << "###";
721 }
722 else
723 m_state->m_idLineStyleMap[id]=line;
724 ascFile.addPos(pos-6);
725 ascFile.addNote(f.str().c_str());
726 return true;
727 }
728
729 bool LotusStyleManager::updateLineStyle(int lineId, WPSGraphicStyle &style) const
730 {
731 if (lineId==0)
732 return true;
733 if (m_state->m_idLineStyleMap.find(lineId)==m_state->m_idLineStyleMap.end())
734 {
735 WPS_DEBUG_MSG(("LotusStyleManager::updateLineStyle: the line style %d does not exist\n", lineId));
736 return false;
737 }
738 auto const &line=m_state->m_idLineStyleMap.find(lineId)->second;
739 style.m_lineWidth=line.m_width;
740 style.m_lineColor=line.m_color;
741 return true;
742 }
743
744 bool LotusStyleManager::readColorStyle(std::shared_ptr<WPSStream> stream, long endPos)
745 {
746 if (!stream) return false;
747 RVNGInputStreamPtr &input = stream->m_input;
748 libwps::DebugFile &ascFile=stream->m_ascii;
749 libwps::DebugStream f;
750
751 long pos = input->tell();
752 int colorSz=1;
753 if (endPos-pos==7)
754 colorSz=1;
755 else if (endPos-pos==11)
756 colorSz=2;
757 else
758 {
759 WPS_DEBUG_MSG(("LotusStyleManager::readColorStyle: the zone size seems bad\n"));
760 ascFile.addPos(pos-6);
761 ascFile.addNote("Entries(ColorStyle):###");
762 return true;
763 }
764 auto id=int(libwps::readU8(input));
765 auto val=int(libwps::readU8(input)); // always 20?
766 if (val!=0x20)
767 f << "fl=" << std::hex << val << std::dec << ",";
768 LotusStyleManagerInternal::ColorStyle color;
769 for (auto &i : color.m_colors)
770 {
771 val=(colorSz==1) ? int(libwps::readU8(input)) : int(libwps::readU16(input));
772 if (val!=0xEF && !m_state->getColor256(val,i))
773 {
774 WPS_DEBUG_MSG(("LotusStyleManager::readColorStyle: can not read a color\n"));
775 f << "##colId=" << val << ",";
776 }
777 }
778 color.m_patternId=int(libwps::readU8(input));
779 if (color.m_patternId && !m_state->getPattern48(color.m_patternId, color.m_pattern))
780 {
781 WPS_DEBUG_MSG(("LotusStyleManager::readColorStyle: can not read a pattern\n"));
782 f << "##patId=" << color.m_patternId << ",";
783 }
784
785 color.m_extra=f.str();
786
787 f.str("");
788 f << "Entries(ColorStyle):Co" << id << "," << color;
789 if (m_state->m_idColorStyleMap.find(id)!=m_state->m_idColorStyleMap.end())
790 {
791 WPS_DEBUG_MSG(("LotusStyleManager::readColorStyle: the color style %d already exists\n", id));
792 f << "###";
793 }
794 else
795 m_state->m_idColorStyleMap[id]=color;
796
797 ascFile.addPos(pos-6);
798 ascFile.addNote(f.str().c_str());
799 return true;
800 }
801
802 bool LotusStyleManager::updateSurfaceStyle(int colorId, WPSGraphicStyle &style) const
803 {
804 if (colorId==0)
805 return true;
806 if (m_state->m_idColorStyleMap.find(colorId)==m_state->m_idColorStyleMap.end())
807 {
808 WPS_DEBUG_MSG(("LotusStyleManager::updateSurfaceStyle: the color style %d does not exist\n", colorId));
809 return false;
810 }
811 LotusStyleManagerInternal::ColorStyle const &color=m_state->m_idColorStyleMap.find(colorId)->second;
812 if (color.m_patternId==0) // empty
813 return true;
814 WPSColor finalColor=color.m_colors[2];
815 WPSGraphicStyle::Pattern pattern;
816 if (color.m_patternId==2)
817 finalColor=color.m_colors[3];
818 else if (color.m_patternId>=47 && color.m_patternId<=48)
819 {
820 style.m_gradientType=WPSGraphicStyle::G_Linear;
821 style.m_gradientStopList.clear();
822 style.m_gradientStopList.push_back(WPSGraphicStyle::GradientStop(0.0, color.m_patternId==47 ? color.m_colors[2] : WPSColor::black()));
823 style.m_gradientStopList.push_back(WPSGraphicStyle::GradientStop(1.0, color.m_patternId==47 ? WPSColor::black() : color.m_colors[2]));
824 }
825 else if (color.m_patternId!=1 && m_state->getPattern48(color.m_patternId, pattern))
826 {
827 pattern.m_colors[0]=color.m_colors[3];
828 pattern.m_colors[1]=color.m_colors[2];
829 if (!pattern.getUniqueColor(finalColor))
830 style.setPattern(pattern);
831 }
832
833 if (!style.hasPattern() && !style.hasGradient())
834 style.setSurfaceColor(finalColor);
835 return true;
836 }
837
838 bool LotusStyleManager::updateSurfaceStyle(int fColorId, int bColorId, int patternId, WPSGraphicStyle &style) const
839 {
840 if (patternId==0)
841 return true;
842 WPSGraphicStyle::Pattern pattern;
843 if (!getColor256(fColorId, pattern.m_colors[0])||!getColor256(bColorId, pattern.m_colors[1]))
844 {
845 WPS_DEBUG_MSG(("LotusStyleManager::updateSurfaceStyle: can not find some colors\n"));
846 return false;
847 }
848 int const vers=version();
849 int const gradBegin=vers<4 ? 60 : 64;
850 if (patternId>=gradBegin && patternId<=gradBegin+3)
851 {
852 style.m_gradientType=WPSGraphicStyle::G_Linear;
853 style.m_gradientStopList.clear();
854 style.m_gradientStopList.push_back(WPSGraphicStyle::GradientStop(0.0, pattern.m_colors[1]));
855 style.m_gradientStopList.push_back(WPSGraphicStyle::GradientStop(1.0, pattern.m_colors[0]));
856 if (vers<4)
857 {
858 float const angles[]= {270, 90, 180, 0};
859 style.m_gradientAngle=angles[patternId-gradBegin];
860 }
861 else
862 {
863 float const angles[]= {0, 180, 90, 270};
864 style.m_gradientAngle=angles[patternId-gradBegin];
865 }
866 return true;
867 }
868 if (!getPattern64(patternId, pattern))
869 {
870 WPS_DEBUG_MSG(("LotusStyleManager::updateSurfaceStyle: can not find the pattern\n"));
871 return false;
872 }
873 WPSColor color;
874 if (pattern.getUniqueColor(color))
875 style.setSurfaceColor(color);
876 else
877 style.m_pattern=pattern;
878 return true;
879 }
880
881 bool LotusStyleManager::updateShadowStyle(int colorId, WPSGraphicStyle &style) const
882 {
883 if (colorId==0)
884 return true;
885 if (m_state->m_idColorStyleMap.find(colorId)==m_state->m_idColorStyleMap.end())
886 {
887 WPS_DEBUG_MSG(("LotusStyleManager::updateShadowStyle: the color style %d does not exist\n", colorId));
888 return false;
889 }
890 auto const &color=m_state->m_idColorStyleMap.find(colorId)->second;
891 if (color.m_patternId==0) // empty
892 return true;
893 WPSColor finalColor=color.m_colors[2];
894 WPSGraphicStyle::Pattern pattern;
895 if (color.m_patternId==2)
896 finalColor=color.m_colors[3];
897 else if (color.m_patternId!=1 && m_state->getPattern48(color.m_patternId, pattern))
898 {
899 pattern.m_colors[0]=color.m_colors[3];
900 pattern.m_colors[1]=color.m_colors[2];
901 pattern.getAverageColor(finalColor);
902 }
903 style.setShadowColor(finalColor);
904 style.m_shadowOffset=Vec2f(3,3);
905 return true;
906 }
907
908 bool LotusStyleManager::readGraphicStyle(std::shared_ptr<WPSStream> stream, long endPos)
909 {
910 if (!stream) return false;
911 RVNGInputStreamPtr &input = stream->m_input;
912 libwps::DebugFile &ascFile=stream->m_ascii;
913 libwps::DebugStream f;
914
915 long pos = input->tell();
916 if (endPos-pos!=13) // only find in a WK3 mac file
917 {
918 WPS_DEBUG_MSG(("LotusStyleManager::readGraphicStyle: the zone size seems bad\n"));
919 ascFile.addPos(pos-6);
920 ascFile.addNote("Entries(GraphicStyle):###");
921 return true;
922 }
923 auto id=int(libwps::readU8(input));
924 LotusStyleManagerInternal::GraphicStyle style;
925 auto val=int(libwps::readU8(input)); // always 40?
926 if (val!=0x40)
927 f << "fl=" << std::hex << val << std::dec << ",";
928 for (int i=0; i<4; ++i)
929 {
930 val=int(libwps::readU8(input));
931 auto fl=int(libwps::readU8(input));
932 if (!val) continue;
933 if (i==0) f << "unknId=" << val << "[" << std::hex << fl << std::dec << ",";
934 else if (i==1)
935 {
936 if (fl!=0x10) f << "#fl[line]=" << std::hex << fl << std::dec << ",";
937 if (m_state->m_idLineStyleMap.find(val)==m_state->m_idLineStyleMap.end())
938 {
939 WPS_DEBUG_MSG(("LotusStyleManager::readGraphicStyle: the line style %d does not exists\n", val));
940 f << "###lineId=" << val << ",";
941 }
942 else
943 style.m_lineId=val;
944 }
945 else
946 {
947 if (fl!=0x20) f << "#fl[color" << i-2 << "]=" << std::hex << fl << std::dec << ",";
948 if (m_state->m_idColorStyleMap.find(val)==m_state->m_idColorStyleMap.end())
949 {
950 WPS_DEBUG_MSG(("LotusStyleManager::readGraphicStyle: the color style %d does not exists\n", val));
951 f << "###colorId[" << i-2 << "]=" << val << ",";
952 }
953 else
954 style.m_colorsId[i-2]=val;
955 }
956 }
957 for (int i=0; i<3; ++i) //f0=f1=0|1|3|4 : a size?, f2=2|3|22
958 {
959 val=int(libwps::readU8(input));
960 if (val)
961 f << "f" << i << "=" << std::hex << val << std::dec << ",";
962 }
963 style.m_extra=f.str();
964 f.str("");
965 f << "Entries(GraphicStyle):G" << id << "," << style;
966
967 if (m_state->m_idGraphicStyleMap.find(id)!=m_state->m_idGraphicStyleMap.end())
968 {
969 WPS_DEBUG_MSG(("LotusStyleManager::readGraphicStyle: the graphic style %d already exists\n", id));
970 f << "###";
971 }
972 else
973 m_state->m_idGraphicStyleMap[id]=style;
974
975 ascFile.addPos(pos-6);
976 ascFile.addNote(f.str().c_str());
977 return true;
978 }
979
980 bool LotusStyleManager::readGraphicStyleC9(std::shared_ptr<WPSStream> stream, long endPos)
981 {
982 if (!stream) return false;
983 RVNGInputStreamPtr &input = stream->m_input;
984 libwps::DebugFile &ascFile=stream->m_ascii;
985 libwps::DebugStream f;
986
987 long pos = input->tell();
988 if (endPos-pos!=31) // 123 file
989 {
990 WPS_DEBUG_MSG(("LotusStyleManager::readGraphicStyleC9: the zone size seems bad\n"));
991 ascFile.addPos(pos-6);
992 ascFile.addNote("Entries(GraphicStyle):###");
993 return true;
994 }
995 f << "Entries(GraphicStyle):";
996 auto id=int(libwps::readU8(input));
997 LotusStyleManagerInternal::GraphicStyle style;
998 f << "GS" << id << ",";
999 auto val=int(libwps::readU8(input)); // always 40?
1000 if (val!=0x40)
1001 f << "fl=" << std::hex << val << std::dec << ",";
1002 val=int(libwps::readU32(input));
1003 if (val!=1) f << "used=" << val << ",";
1004 for (int i=0; i<2; ++i) // line, shadow color?
1005 {
1006 style.m_colorsId[i]=int(libwps::readU8(input));
1007 val=int(libwps::readU8(input));
1008 WPSColor color;
1009 if (!getColor256(style.m_colorsId[i],color))
1010 f << "##col=" << style.m_colorsId[i];
1011 else
1012 {
1013 if (i==0)
1014 style.m_lineStyle.m_color=color;
1015 f << color;
1016 }
1017 if (val) f << ":" << val << std::hex << std::dec;
1018 f << ",";
1019 }
1020 val=int(libwps::readU8(input)); // always 0
1021 if (val) f << "f0=" << val << ",";
1022 val=int(libwps::readU8(input));
1023 if (val)
1024 {
1025 style.m_lineStyle.m_width=float(val+1);
1026 f << "w=" << val << ",";
1027 }
1028 val=int(libwps::readU16(input)); // 0-32
1029 if (val==0x20) // checkme, seems bad
1030 {
1031 f << "no[line],";
1032 style.m_lineStyle.m_width=0;
1033 }
1034 else if (val) f << "dash[id]=" << std::hex << val << std::dec << ",";
1035 for (int i=0; i<2; ++i) // surface, pattern color
1036 {
1037 style.m_colorsId[3-i]=int(libwps::readU8(input));
1038 val=int(libwps::readU8(input));
1039 WPSColor color;
1040 if (!getColor256(style.m_colorsId[3-i],color))
1041 f << "##col=" << style.m_colorsId[3-i] << ",";
1042 else
1043 f << color;
1044 if (val) f << std::hex << ":" << val << std::dec;
1045 f << ",";
1046 }
1047 style.m_patternId=int(libwps::readU8(input)); // 0 none, .. gradient 65..
1048 if (style.m_patternId) f << "pattern=" << style.m_patternId << ",";
1049 for (int i=0; i<2; ++i)
1050 {
1051 auto col=int(libwps::readU8(input));
1052 val=int(libwps::readU8(input));
1053 WPSColor color;
1054 if (!getColor256(col,color))
1055 f << "##col=" << col;
1056 else
1057 f << color;
1058 if (val) f << std::hex << ":" << val;
1059 f << ",";
1060 }
1061 for (int i=0; i<4; ++i) // f0=2, f2=200
1062 {
1063 val=int(libwps::readU16(input));
1064 if (val) f << "f" << i+1 << "=" << std::hex << val << std::dec << ",";
1065 }
1066 if (m_state->m_idGraphicStyleMap.find(id)!=m_state->m_idGraphicStyleMap.end())
1067 {
1068 WPS_DEBUG_MSG(("LotusStyleManager::readGraphicStyleC9: the graphic style %d already exists\n", id));
1069 f << "###";
1070 }
1071 else
1072 m_state->m_idGraphicStyleMap[id]=style;
1073 ascFile.addPos(pos-6);
1074 ascFile.addNote(f.str().c_str());
1075 return true;
1076 }
1077
1078 bool LotusStyleManager::updateGraphicStyle(int graphicId, WPSGraphicStyle &style) const
1079 {
1080 if (graphicId==0)
1081 return true;
1082 if (m_state->m_idGraphicStyleMap.find(graphicId)==m_state->m_idGraphicStyleMap.end())
1083 {
1084 WPS_DEBUG_MSG(("LotusStyleManager::updateGraphicStyle: the graphic style %d does not exist\n", graphicId));
1085 return false;
1086 }
1087 auto const &graphic=m_state->m_idGraphicStyleMap.find(graphicId)->second;
1088 int const vers=version();
1089 if (vers>=4)
1090 {
1091 style.m_lineWidth=graphic.m_lineStyle.m_width;
1092 style.m_lineColor=graphic.m_lineStyle.m_color;
1093 updateSurfaceStyle(graphic.m_colorsId[2],graphic.m_colorsId[3],graphic.m_patternId, style);
1094 return true;
1095 }
1096 if (graphic.m_lineId)
1097 updateLineStyle(graphic.m_lineId, style);
1098 if (graphic.m_colorsId[0]>0)
1099 updateSurfaceStyle(graphic.m_colorsId[0], style);
1100 if (graphic.m_colorsId[1]>0)
1101 updateShadowStyle(graphic.m_colorsId[1], style);
1102 return true;
1103 }
1104
1105 bool LotusStyleManager::readFontStyleA0(std::shared_ptr<WPSStream> stream, long endPos)
1106 {
1107 if (!stream) return false;
1108 RVNGInputStreamPtr &input = stream->m_input;
1109 libwps::DebugFile &ascFile=stream->m_ascii;
1110 libwps::DebugStream f;
1111
1112 long pos = input->tell();
1113 if (endPos-pos!=12)
1114 {
1115 WPS_DEBUG_MSG(("LotusStyleManager::readFontStyleA0: the zone size seems bad\n"));
1116 ascFile.addPos(pos-6);
1117 ascFile.addNote("Entries(FontStyle):###");
1118 return true;
1119 }
1120 auto id=int(libwps::readU8(input));
1121 LotusStyleManagerInternal::FontStyle font(m_mainParser.getDefaultFontType());
1122 auto val=int(libwps::readU8(input)); // always 0?
1123 if (val)
1124 f << "fl=" << std::hex << val << std::dec << ",";
1125 for (int i=0; i<2; ++i) // always 0?
1126 {
1127 val=int(libwps::readU8(input));
1128 if (val)
1129 f << "f" << i << "=" << val << ",";
1130 }
1131 val=int(libwps::readU8(input));
1132 if (val!=0xFF)
1133 f << "g0=" << std::hex << val << std::dec << ",";
1134 // we can not read the font name here, because the font is defined after...
1135 font.m_fontId=int(libwps::readU8(input));
1136 val=int(libwps::readU16(input));
1137 if (val)
1138 font.m_font.m_size=val/32.;
1139 for (int i=0; i<2; ++i)
1140 {
1141 val=int(libwps::readU8(input));
1142 if (val==0xEF) continue;
1143 WPSColor color;
1144 if (!getColor256(val, color))
1145 f << "#col" << i << "=" << std::hex << val << std::dec << ",";
1146 else if (i==0)
1147 font.m_font.m_color=color;
1148 else if (color!=font.m_font.m_color) //unsured
1149 f << "col[def]=" << color << ",";
1150 }
1151 val=int(libwps::readU8(input));
1152 if (val)
1153 {
1154 if (val&1) font.m_font.m_attributes |= WPS_BOLD_BIT;
1155 if (val&2) font.m_font.m_attributes |= WPS_ITALICS_BIT;
1156 if (val&4) font.m_font.m_attributes |= WPS_UNDERLINE_BIT;
1157 if (val&8) font.m_font.m_attributes |= WPS_OUTLINE_BIT;
1158 if (val&0x10) font.m_font.m_attributes |= WPS_SHADOW_BIT;
1159 if (val&0x20) font.m_font.m_spacing=-2;
1160 if (val&0x40) font.m_font.m_spacing=2;
1161 if (val&0x80) f << "flags[#80],";
1162 }
1163 val=int(libwps::readU8(input));
1164 if (val) // 0|18|20|24
1165 f << "h0=" << std::hex << val << std::dec << ",";
1166 font.m_extra=f.str();
1167 if (m_state->m_idFontStyleMap.find(id)!=m_state->m_idFontStyleMap.end())
1168 {
1169 WPS_DEBUG_MSG(("LotusStyleManager::readFontStyleA0: the font style %d already exists\n", id));
1170 f << "###";
1171 }
1172 else
1173 m_state->m_idFontStyleMap.insert
1174 (std::map<int,LotusStyleManagerInternal::FontStyle>::value_type(id,font));
1175
1176 f.str("");
1177 f << "Entries(FontStyle):FS" << id << "," << font;
1178 ascFile.addPos(pos-6);
1179 ascFile.addNote(f.str().c_str());
1180 return true;
1181 }
1182
1183 bool LotusStyleManager::readFontStyleF0(std::shared_ptr<WPSStream> stream, long endPos)
1184 {
1185 if (!stream) return false;
1186 RVNGInputStreamPtr &input = stream->m_input;
1187 libwps::DebugFile &ascFile=stream->m_ascii;
1188 libwps::DebugStream f;
1189
1190 long pos = input->tell();
1191 long sz=endPos-pos;
1192 if (sz<20)
1193 {
1194 WPS_DEBUG_MSG(("LotusStyleManager::readFontStyleF0: the zone size seems bad\n"));
1195 ascFile.addPos(pos-6);
1196 ascFile.addNote("Entries(FontStyle):###");
1197 return true;
1198 }
1199 auto id=int(libwps::readU8(input));
1200 LotusStyleManagerInternal::FontStyle font(m_mainParser.getDefaultFontType());
1201 if (id&8)
1202 {
1203 font.m_font.m_attributes |= WPS_BOLD_BIT;
1204 f << "bold,";
1205 }
1206 if (id&0x10)
1207 {
1208 font.m_font.m_attributes |= WPS_ITALICS_BIT;
1209 f << "italic,";
1210 }
1211 auto fSz=int(libwps::readU16(input));
1212 font.m_font.m_size=double(fSz)/256;
1213 auto val=int(libwps::readU16(input));
1214 if (val!=fSz)
1215 f << "sz2=" << double(val)/256 << ",";
1216 for (int i=0; i<5; ++i) // always 0?
1217 {
1218 val=int(libwps::readU8(input));
1219 if (val)
1220 f << "f" << i << "=" << val << ",";
1221 }
1222 for (int i=0; i<8; ++i) // fl2=background?, fl3=pattern?
1223 {
1224 // fl0=0|c0, fl1=0|1, fl2=0|80, fl3=[0-5]0, fl4=0-4, fl5=0|50, fl6=0
1225 val=int(libwps::readU8(input));
1226 if (!val)
1227 continue;
1228 if (i==7)
1229 f << "font[id]=" << val << ",";
1230 else
1231 f << "fl" << i << "=" << std::hex << val << std::dec << ",";
1232 }
1233 val=int(libwps::readU8(input));
1234 WPSColor color;
1235 if (!getColor256(val, color)) f << "#colorId=" << val << ",";
1236 else
1237 {
1238 font.m_font.m_color=color;
1239 if (!color.isBlack()) f << "color=" << color << ",";
1240 }
1241
1242 std::string name("");
1243 for (long i=19; i<sz; ++i)
1244 {
1245 auto c=char(libwps::readU8(input));
1246 if (!c) break;
1247 name.push_back(c);
1248 }
1249 if (!name.empty())
1250 {
1251 font.m_font.m_name=libwps_tools_win::Font::unicodeString(name, font.m_fontType);
1252 auto fType=libwps_tools_win::Font::getFontType(font.m_font.m_name);
1253 if (fType!=libwps_tools_win::Font::UNKNOWN) font.m_fontType=fType;
1254 f << name << ",";
1255 }
1256 if (input->tell()!=endPos) ascFile.addDelimiter(input->tell(),'|');
1257 font.m_extra=f.str();
1258
1259 if (m_state->m_idFontStyleMap.find(id)!=m_state->m_idFontStyleMap.end())
1260 {
1261 WPS_DEBUG_MSG(("LotusStyleManager::readFontStyleA0: the font style %d already exists\n", id));
1262 f << "###";
1263 }
1264 else
1265 m_state->m_idFontStyleMap.insert
1266 (std::map<int,LotusStyleManagerInternal::FontStyle>::value_type(id,font));
1267 f.str("");
1268 f << "Entries(FontStyle):FS" << id << "," << font;
1269 ascFile.addPos(pos-6);
1270 ascFile.addNote(f.str().c_str());
1271 return true;
1272 }
1273
1274 bool LotusStyleManager::updateFontStyle(int fontId, WPSFont &font, libwps_tools_win::Font::Type &fontType)
1275 {
1276 if (fontId==0)
1277 return true;
1278 if (m_state->m_idFontStyleMap.find(fontId)==m_state->m_idFontStyleMap.end())
1279 {
1280 static bool first=true;
1281 if (first)
1282 {
1283 WPS_DEBUG_MSG(("LotusStyleManager::updateFontStyle: the font style %d does not exist\n", fontId));
1284 first=false;
1285 }
1286 return false;
1287 }
1288 auto const &fontStyle=m_state->m_idFontStyleMap.find(fontId)->second;
1289 font=fontStyle.m_font;
1290 fontType=fontStyle.m_fontType;
1291 return true;
1292 }
1293
1294 bool LotusStyleManager::readFormatStyle(std::shared_ptr<WPSStream> stream, long endPos)
1295 {
1296 if (!stream) return false;
1297 RVNGInputStreamPtr &input = stream->m_input;
1298 libwps::DebugFile &ascFile=stream->m_ascii;
1299 libwps::DebugStream f;
1300
1301 long pos = input->tell();
1302 if (endPos-pos<23)
1303 {
1304 WPS_DEBUG_MSG(("LotusStyleManager::readFormatStyle: the zone size seems bad\n"));
1305 ascFile.addPos(pos-6);
1306 ascFile.addNote("Entries(FormatStyle):###");
1307 return true;
1308 }
1309 auto id=int(libwps::readU8(input));
1310 LotusStyleManagerInternal::FormatStyle format;
1311 auto val=int(libwps::readU8(input)); // always 30?
1312 if (val!=0x30)
1313 f << "fl=" << std::hex << val << std::dec << ",";
1314
1315 for (int i=0; i<10; ++i) // always f1=100, other 0?
1316 {
1317 val=int(libwps::readU16(input));
1318 if (val) f << "f" << i << "=" << val << ",";
1319 }
1320 bool ok=true;
1321 for (int i=0; i<2; ++i)
1322 {
1323 val=int(libwps::readU8(input));
1324 if (val==0xf) continue;
1325 if (val!=0x3c)
1326 {
1327 WPS_DEBUG_MSG(("LotusStyleManager::readFormatStyle: find unknown type\n"));
1328 f << "###type=" << std::hex << val << std::dec << ",";
1329 ok=false;
1330 break;
1331 }
1332 auto dSz=int(libwps::readU8(input));
1333 if (input->tell()+dSz+1>endPos)
1334 {
1335 WPS_DEBUG_MSG(("LotusStyleManager::readFormatStyle: bad string size\n"));
1336 f << "###size=" << std::hex << dSz << std::dec << ",";
1337 ok=false;
1338 break;
1339 }
1340 std::string name("");
1341 for (int j=0; j<dSz; ++j)
1342 name += char(libwps::readU8(input));
1343 if (i==0)
1344 format.m_prefix=name;
1345 else
1346 format.m_suffix=name;
1347 }
1348 if (ok && input->tell()+1<=endPos)
1349 {
1350 val=int(libwps::readU8(input));
1351 if (val!=0xc)
1352 f << "g0=" << val << ",";
1353 }
1354 format.m_extra=f.str();
1355
1356 if (m_state->m_idFormatStyleMap.find(id)!=m_state->m_idFormatStyleMap.end())
1357 {
1358 WPS_DEBUG_MSG(("LotusStyleManager::readFormatStyle: the format style %d already exists\n", id));
1359 f << "###";
1360 }
1361 else
1362 m_state->m_idFormatStyleMap[id]=format;
1363
1364 f.str("");
1365 f << "Entries(FormatStyle):Fo" << id << "," << format;
1366 ascFile.addPos(pos-6);
1367 ascFile.addNote(f.str().c_str());
1368 return true;
1369 }
1370
1371 bool LotusStyleManager::readCellStyleD2Data(LotusStyleManagerInternal::CellStyleEntry const &entry, std::set<int> &seen)
1372 {
1373 if (!entry.m_stream || !entry.m_entry.valid() || entry.m_entry.length()!=33)
1374 {
1375 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2Data: can not find the stream\n"));
1376 return false;
1377 }
1378 LotusStyleManagerInternal::CellStyle cell(m_mainParser.getDefaultFontType());
1379 // first retrieve the cell parent, if it exists
1380 bool hasParent=false;
1381 if (entry.m_parentId>=0)
1382 {
1383 int pId=-1;
1384 if (m_state->m_idToParentStyleMap.find(entry.m_parentId)!=m_state->m_idToParentStyleMap.end())
1385 pId=m_state->m_idToParentStyleMap.find(entry.m_parentId)->second;
1386 else
1387 {
1388 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2Data: can not find main style %d\n", entry.m_parentId));
1389 }
1390 if (pId>=0 && m_state->m_idCellStyleMap.find(pId)==m_state->m_idCellStyleMap.end())
1391 {
1392 if (seen.find(pId)!=seen.end())
1393 {
1394 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2Data: oops find a loop\n"));
1395 }
1396 else if (m_state->m_idToCellStyleEntryMap.find(pId)!=m_state->m_idToCellStyleEntryMap.end())
1397 {
1398 seen.insert(pId);
1399 readCellStyleD2Data(m_state->m_idToCellStyleEntryMap.find(pId)->second, seen);
1400 }
1401 else
1402 {
1403 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2Data: can not find the style %d\n", pId));
1404 }
1405 }
1406 if (pId>=0 && m_state->m_idCellStyleMap.find(pId)!=m_state->m_idCellStyleMap.end())
1407 {
1408 cell=m_state->m_idCellStyleMap.find(pId)->second;
1409 hasParent=true;
1410 }
1411 }
1412
1413 RVNGInputStreamPtr &input = entry.m_stream->m_input;
1414 libwps::DebugFile &ascFile = entry.m_stream->m_ascii;
1415 libwps::DebugStream f;
1416
1417 input->seek(entry.m_entry.begin(), librevenge::RVNG_SEEK_SET);
1418 auto id=int(libwps::readU8(input));
1419 auto val=int(libwps::readU8(input)); // always 50?
1420 if (val!=0x50)
1421 f << "fl=" << std::hex << val << std::dec << ",";
1422 auto &font=cell.m_fontStyle;
1423 val=int(libwps::readU32(input));
1424 if (val!=1) f << "num[used]=" << val << ",";
1425 val=int(libwps::readU8(input));
1426 if (val!=255) f << "f0=" << val << ";";
1427 val=int(libwps::readU8(input));
1428 if (val!=255)
1429 {
1430 font.m_fontId=val;
1431 WPSFont defFont;
1432 if (m_mainParser.getFont(font.m_fontId, defFont, font.m_fontType))
1433 font.m_font.m_name = defFont.m_name;
1434 }
1435 val=int(libwps::readU16(input)); // checkme what is this unit? 1de:18, 173: 14, 140: 12?, 778:72
1436 if (val!=0xFFFF)
1437 font.m_font.m_size=double(int(double(val)*3./80.+0.5));
1438 else if (!hasParent)
1439 font.m_font.m_size=12;
1440 val=int(libwps::readU16(input));
1441 if (val!=0xFFFF)
1442 {
1443 WPSColor color;
1444 if (!m_state->getColor256(val,font.m_font.m_color))
1445 {
1446 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2Data sorry, can not read the font color\n"));
1447 f << "###col=" << std::hex << val << std::dec << ",";
1448 }
1449 }
1450 for (int i=0; i<2; ++i)
1451 {
1452 val=int(libwps::readU8(input));
1453 if (val!=255) f << "f" << i+1 << "=" << val << ",";
1454 }
1455 val=int(libwps::readU8(input));
1456 if (val!=255)
1457 {
1458 font.m_font.m_attributes=0;
1459 if (val&1) font.m_font.m_attributes |= WPS_BOLD_BIT;
1460 if (val&2) font.m_font.m_attributes |= WPS_ITALICS_BIT;
1461 if (val&4) font.m_font.m_attributes |= WPS_UNDERLINE_BIT;
1462 if (val&0x40) font.m_font.m_attributes |= WPS_STRIKEOUT_BIT;
1463 val &= 0xB8;
1464 if (val) f << "font[fl]=" << std::hex << val << std::dec << ",";
1465 }
1466 val=int(libwps::readU8(input));
1467 if (val!=255)
1468 {
1469 switch (val&3)
1470 {
1471 case 0:
1472 break;
1473 case 1:
1474 font.m_font.m_attributes |= WPS_DOUBLE_UNDERLINE_BIT;
1475 break;
1476 case 2:
1477 font.m_font.m_attributes |= WPS_UNDERLINE_BIT;
1478 f << "underline[w=2],";
1479 break;
1480 default:
1481 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2Data sorry, find underline=3\n"));
1482 f << "##underline=3,";
1483 break;
1484 }
1485 val&=0xFC;
1486 if (val!=0x80) f << "f3=" << std::hex << val << std::dec << ",";
1487 }
1488 bool angleTypeNormal=true;
1489 for (int i=0; i<5; ++i)
1490 {
1491 val=i==4 ? int(libwps::readU16(input)) : int(libwps::readU8(input));
1492 if (val==0xFF) continue; // default
1493 switch (i)
1494 {
1495 case 0: // look like the inverse of font flags
1496 f << "font[fl2]=" << std::hex << ((~val)&0xFF) << std::dec << ",";
1497 break;
1498 case 2:
1499 switch (val&7)
1500 {
1501 case 0: // default
1502 break;
1503 case 1:
1504 cell.m_hAlign=WPSCellFormat::HALIGN_LEFT;
1505 break;
1506 case 2:
1507 cell.m_hAlign=WPSCellFormat::HALIGN_RIGHT;
1508 break;
1509 case 3:
1510 cell.m_hAlign=WPSCellFormat::HALIGN_CENTER;
1511 break;
1512 case 4: // text:left, number:right
1513 f << "hAlign=def,";
1514 break;
1515 case 6:
1516 cell.m_hAlign=WPSCellFormat::HALIGN_FULL;
1517 break;
1518 default:
1519 f << "##hAlign=" << (val&7) << ",";
1520 }
1521 switch ((val>>4)&7)
1522 {
1523 case 0: // default
1524 break;
1525 case 1: // find it in basic style
1526 f << "#angle1,";
1527 break;
1528 case 3:
1529 cell.m_rotation=90;
1530 break;
1531 case 4:
1532 cell.m_rotation=-90;
1533 f << "angle[2spacings],";
1534 break;
1535 case 5:
1536 cell.m_rotation=-90;
1537 break;
1538 case 6:
1539 angleTypeNormal=false;
1540 f << "angle[free],";
1541 break;
1542 default:
1543 f << "##angle=" << ((val>>4)&7) << ",";
1544 break;
1545 }
1546 val&=0x88;
1547 if (val) f << "#hAlign=" << std::hex << val << std::dec << ",";
1548 break;
1549 case 3:
1550 switch (val&3)
1551 {
1552 case 0: // default
1553 default:
1554 break;
1555 case 1:
1556 cell.m_vAlign=WPSCellFormat::VALIGN_TOP;
1557 break;
1558 case 2:
1559 cell.m_vAlign=WPSCellFormat::VALIGN_CENTER;
1560 break;
1561 case 3:
1562 cell.m_vAlign=WPSCellFormat::VALIGN_BOTTOM;
1563 break;
1564 }
1565 if (!(val&0x80))
1566 {
1567 f << "wrap[text],";
1568 cell.m_wrapping=WPSCellFormat::WRAP_WRAP;
1569 }
1570 val&=0x7C;
1571 if (val) f << "#vAlign=" << std::hex << val << std::dec << ",";
1572 break;
1573 case 4:
1574 if (!angleTypeNormal)
1575 cell.m_rotation=(val!=3600) ? val : 45;
1576 else if (val!=3600)
1577 f << "angle=" << val << ",";
1578 break;
1579 default:
1580 {
1581 int const expected[]= {0xFF, 0xFF, 0, 0xc0, 0};
1582 if (val!=expected[i]) f << "f" << i+4 << "=" << val << ",";
1583 }
1584 }
1585 }
1586 for (int i=0; i<2; ++i)
1587 {
1588 val=int(libwps::readU16(input));
1589 if (val==0xFFFF)
1590 continue;
1591 WPSColor color;
1592 std::string wh(i==0 ? "pattern" : "back");
1593 if (m_state->getColor256(val,color))
1594 {
1595 cell.m_colorStyle.m_colors[2+i]=color;
1596 if (!color.isWhite())
1597 f << "color[" << wh << "]=" << color << ",";
1598 }
1599 else
1600 {
1601 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2Data: sorry, can not read the back color\n"));
1602 f << "###col[" << wh << "]=" << std::hex << val << std::dec << ",";
1603 }
1604 }
1605 val=int(libwps::readU8(input));
1606 if (val!=0xFF)
1607 cell.m_colorStyle.m_patternId=val;
1608 else if (!hasParent)
1609 cell.m_colorStyle.m_patternId=2;
1610 if (val!=0xFF)
1611 f << "pattern=" << val << ",";
1612 val=int(libwps::readU16(input));
1613 if ((val>>8)==0x60)
1614 f << "ST" << (val&0xFF) << ",";
1615 else if (val)
1616 {
1617 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2Data: sorry, find unexpected main style\n"));
1618 f << "##ST" << std::hex << val << std::dec << "\n";
1619 }
1620 cell.m_extra=f.str();
1621 f.str("");
1622 f << cell << "font=[" << font << "],";
1623 if (m_state->m_idCellStyleMap.find(id)!=m_state->m_idCellStyleMap.end())
1624 {
1625 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2Data: the cell style %d already exists\n", id));
1626 f << "###";
1627 }
1628 else
1629 m_state->m_idCellStyleMap.insert(std::map<int, LotusStyleManagerInternal::CellStyle>::value_type(id,cell));
1630
1631 ascFile.addDelimiter(input->tell(),'|');
1632 ascFile.addPos(entry.m_entry.begin()-6);
1633 ascFile.addNote(f.str().c_str());
1634 return true;
1635 }
1636
1637 bool LotusStyleManager::readCellStyleD2(std::shared_ptr<WPSStream> stream, long endPos)
1638 {
1639 if (!stream) return false;
1640 RVNGInputStreamPtr &input = stream->m_input;
1641 libwps::DebugFile &ascFile=stream->m_ascii;
1642 libwps::DebugStream f;
1643
1644 long pos = input->tell();
1645 int const vers=version();
1646 int expectedSize=vers<4 ? 21 : 33;
1647 if (endPos-pos!=expectedSize)
1648 {
1649 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2 the zone size seems bad\n"));
1650 ascFile.addPos(pos-6);
1651 ascFile.addNote("Entries(CellStyle):###");
1652 return true;
1653 }
1654 auto id=int(libwps::readU8(input));
1655 if (endPos-pos==33) // 123
1656 {
1657 f << "Entries(CellStyle):Ce" << id << ",";
1658 ascFile.addPos(pos-6);
1659 ascFile.addNote(f.str().c_str());
1660
1661 LotusStyleManagerInternal::CellStyleEntry cellEntry(stream);
1662 cellEntry.m_entry.setBegin(pos);
1663 cellEntry.m_entry.setEnd(endPos);
1664 input->seek(pos+27, librevenge::RVNG_SEEK_SET);
1665 auto val=int(libwps::readU16(input));
1666 if (!val)
1667 {
1668 std::set<int> seen;
1669 seen.insert(id);
1670 return readCellStyleD2Data(cellEntry, seen);
1671 }
1672 // we need to read the style first, so let defer the parsing of this field
1673 cellEntry.m_parentId=(val&0xFF);
1674 if (m_state->m_idToCellStyleEntryMap.find(id)!=m_state->m_idToCellStyleEntryMap.end())
1675 {
1676 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2Data: the cell style entry %d already exists\n", id));
1677 f << "###";
1678 }
1679 else
1680 m_state->m_idToCellStyleEntryMap.insert(std::map<int, LotusStyleManagerInternal::CellStyleEntry>::value_type(id,cellEntry));
1681 return true;
1682 }
1683 auto val=int(libwps::readU8(input)); // always 50?
1684
1685 LotusStyleManagerInternal::CellStyle cell(m_mainParser.getDefaultFontType());
1686 if (val!=0x50)
1687 f << "fl=" << std::hex << val << std::dec << ",";
1688 // wk4
1689 for (int i=0; i<2; ++i) // always 0?
1690 {
1691 val=int(libwps::readU8(input));
1692 if (val)
1693 f << "f" << i << "=" << val << ",";
1694 }
1695 for (int i=0; i<8; ++i)
1696 {
1697 val=int(libwps::readU8(input));
1698 auto fl=int(libwps::readU8(input));
1699 if (!val) continue;
1700 if (i<4)
1701 {
1702 if (fl!=0x10) f << "#fl[border" << i << "]=" << std::hex << fl << std::dec << ",";
1703 if (m_state->m_idLineStyleMap.find(val)==m_state->m_idLineStyleMap.end())
1704 {
1705 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2: the line style %d does not exists\n", val));
1706 f << "###borderId" << i << "=" << val << ",";
1707 }
1708 else
1709 cell.m_bordersId[i]=val;
1710 }
1711 else if (i==4 || i==7)
1712 {
1713 int wh=i==4 ? 0 : 1;
1714 if (fl!=0x20) f << "#fl[color" << wh << "]=" << std::hex << fl << std::dec << ",";
1715 if (m_state->m_idColorStyleMap.find(val)==m_state->m_idColorStyleMap.end())
1716 {
1717 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2: the color style %d does not exists\n", val));
1718 f << "###colorId[" << wh << "]=" << val << ",";
1719 }
1720 else
1721 cell.m_colorsId[wh]=val;
1722 }
1723 else if (i==5)
1724 {
1725 if (fl) f << "#fl[font]=" << std::hex << fl << std::dec << ",";
1726 if (m_state->m_idFontStyleMap.find(val)==m_state->m_idFontStyleMap.end())
1727 {
1728 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2: the font style %d does not exists\n", val));
1729 f << "###fontId=" << val << ",";
1730 }
1731 else
1732 cell.m_fontId=val;
1733 }
1734 else
1735 {
1736 if (fl!=0x30) f << "#fl[format]=" << std::hex << fl << std::dec << ",";
1737 if (m_state->m_idFormatStyleMap.find(val)==m_state->m_idFormatStyleMap.end())
1738 {
1739 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2: the format style %d does not exists\n", val));
1740 f << "###formatId=" << val << ",";
1741 }
1742 else
1743 cell.m_formatId=val;
1744 }
1745 }
1746 val=int(libwps::readU8(input));
1747 cell.m_borders=(val&0xF);
1748 val >>=4;
1749 // small number 0|2
1750 if (val) f << "f2=" << val << ",";
1751 cell.m_extra=f.str();
1752 f.str("");
1753 f << "Entries(CellStyle):Ce" << id << "," << cell;
1754
1755 if (m_state->m_idCellStyleMap.find(id)!=m_state->m_idCellStyleMap.end())
1756 {
1757 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleD2: the cell style %d already exists\n", id));
1758 f << "###";
1759 }
1760 else
1761 m_state->m_idCellStyleMap.insert(std::map<int, LotusStyleManagerInternal::CellStyle>::value_type(id,cell));
1762 ascFile.addPos(pos-6);
1763 ascFile.addNote(f.str().c_str());
1764 return true;
1765 }
1766
1767 bool LotusStyleManager::readCellStyleE6(std::shared_ptr<WPSStream> stream, long endPos)
1768 {
1769 if (!stream) return false;
1770 RVNGInputStreamPtr &input = stream->m_input;
1771 libwps::DebugFile &ascFile=stream->m_ascii;
1772 libwps::DebugStream f;
1773
1774 long pos = input->tell();
1775 long sz=endPos-pos;
1776 if ((sz%15)!=10)
1777 {
1778 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleE6: the zone size seems bad\n"));
1779 ascFile.addPos(pos-6);
1780 ascFile.addNote("Entries(CellStyle):###");
1781 return true;
1782 }
1783 f << "Entries(CellStyle):";
1784 for (int i=0; i<4; ++i)
1785 {
1786 auto val=int(libwps::readU16(input));
1787 int const expected[]= {0x10, 0x100, 0x10, 0xe};
1788 if (val!=expected[i]) f << "f" << i << "=" << val << ",";
1789 }
1790 auto decalId=int(libwps::readU16(input));
1791 if (decalId)
1792 {
1793 decalId *= 256;
1794 f << "decal[id]=" << decalId;
1795 }
1796 ascFile.addPos(pos-6);
1797 ascFile.addNote(f.str().c_str());
1798 auto N=int(sz/15);
1799 auto fontType=m_mainParser.getDefaultFontType();
1800 for (int i=0; i<N; ++i)
1801 {
1802 pos=input->tell();
1803 f.str("");
1804 f << "CellStyle-" << i << ":";
1805 int id=decalId+int(libwps::readU8(input));
1806 f << "Ce" << id << ",";
1807 LotusStyleManagerInternal::CellStyle cell(fontType);
1808 auto fId=int(libwps::readU8(input));
1809 auto &font=cell.m_fontStyle;
1810 if (fId)
1811 {
1812 if (!updateFontStyle(fId, font.m_font, font.m_fontType))
1813 f << "#";
1814 f << "FS" << fId << ",";
1815 }
1816 for (int j=0; j<5; ++j) // fl0=0|20|40, fl1=0|f|32|36|76|a8, fl2=XX, fl3=0-b|7c, fl4=0|2
1817 {
1818 auto val=int(libwps::readU8(input));
1819 if (j==0)
1820 {
1821 if (val&0x60)
1822 {
1823 int wh=((val>>5)&3);
1824 if (wh==1)
1825 {
1826 font.m_font.m_attributes |= WPS_UNDERLINE_BIT;
1827 f << "underline,";
1828 }
1829 else if (wh==2)
1830 {
1831 f << "underline[double],";
1832 font.m_font.m_attributes |= WPS_DOUBLE_UNDERLINE_BIT;
1833 }
1834 else
1835 {
1836 font.m_font.m_attributes |= WPS_UNDERLINE_BIT;
1837 f << "underline[w=2],"; // FIXME
1838 }
1839 }
1840 val&=0x9F;
1841 }
1842 if (!val) continue;
1843 if (j==1 || j==2)
1844 {
1845 WPSColor color;
1846 if (!getColor256(val, color)) f << "#colorId=" << val << ",";
1847 else
1848 {
1849 cell.m_colorStyle.m_colors[j==2 ? 3 : 2]=color;
1850 if (!color.isWhite()) f << "color" << j << "=" << color << ",";
1851 }
1852 }
1853 else if (j==3)
1854 {
1855 // checkme: something is not right here...
1856 if (val==1 || val==3)
1857 {
1858 cell.m_colorStyle.m_patternId=2;
1859 f << "pat[low]=" << val << ",";
1860 }
1861 else
1862 {
1863 cell.m_colorStyle.m_patternId=(val>>2);
1864 f << "pat=" << (val>>2) << ",";
1865 if (val&3) f << "pat[low]=" << (val&3) << ",";
1866 }
1867 }
1868 else
1869 f << "fl" << j << "=" << std::hex << val << std::dec << ",";
1870 }
1871 int colors[4]; // TBLR
1872 int borders[4];
1873 unsigned long lVal=libwps::readU16(input);
1874 colors[0]=(lVal>>10)&0x1f;
1875 colors[1]=(lVal>>5)&0x1f;
1876 lVal&=0xC21F;
1877 if (lVal) f << "col[h0]=" << std::hex << lVal << std::dec << ",";
1878 lVal=libwps::readU16(input);
1879 colors[2]=(lVal>>5)&0x1f;
1880 colors[3]=(lVal>>0)&0x1f;
1881 borders[1]=(lVal>>10)&0xf;
1882 lVal&=0xC210;
1883 if (lVal) f << "col[h1]=" << std::hex << lVal << std::dec << ",";
1884 lVal=libwps::readU16(input);
1885 borders[0]=(lVal&0xf);
1886 borders[3]=((lVal>>4)&0xf);
1887 borders[2]=((lVal>>8)&0xf);
1888 lVal&=0xF000;
1889 if (lVal) f << "col[h2]=" << std::hex << lVal << std::dec << ",";
1890 for (int j=0; j<4; ++j)
1891 {
1892 if (!borders[j]) continue;
1893 WPSBorder border;
1894 switch (borders[j])
1895 {
1896 case 1:
1897 border.m_style=WPSBorder::Simple;
1898 break;
1899 case 2:
1900 border.m_style=WPSBorder::Simple;
1901 border.m_type=WPSBorder::Double;
1902 break;
1903 case 3:
1904 border.m_style=WPSBorder::Simple;
1905 border.m_width=2;
1906 break;
1907 case 4:
1908 border.m_style=WPSBorder::Dot; // 1x1
1909 break;
1910 case 5:
1911 border.m_style=WPSBorder::LargeDot; // 2x2
1912 break;
1913 case 6:
1914 border.m_style=WPSBorder::Dash; // 3x1
1915 break;
1916 case 7:
1917 border.m_style=WPSBorder::Dash; // 1x1 2x1
1918 break;
1919 case 8:
1920 border.m_style=WPSBorder::Dash; // 1x1 1x1 2x1
1921 break;
1922 default:
1923 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleE6: can not read some border format\n"));
1924 f << "##border" << j << "=" << borders[j] << ",";
1925 border.m_style=WPSBorder::Simple;
1926 break;
1927 }
1928 if (!getColor16(colors[j], border.m_color))
1929 {
1930 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleE6: can not read some color\n"));
1931 f << "##col" << j << "=" << colors[j] << ",";
1932 }
1933 char const *wh[]= {"T", "B", "L", "R"};
1934 f << "bord" << wh[j] << "=" << border << ",";
1935 cell.m_bordersStyle[j]=border;
1936 }
1937 f << "],";
1938 ascFile.addDelimiter(input->tell(),'|');
1939 if (m_state->m_idCellStyleMap.find(id)!=m_state->m_idCellStyleMap.end())
1940 {
1941 WPS_DEBUG_MSG(("LotusStyleManager::readCellStyleE6: the cell style %d already exists\n", id));
1942 f << "###id";
1943 }
1944 else
1945 m_state->m_idCellStyleMap.insert(std::map<int, LotusStyleManagerInternal::CellStyle>::value_type(id,cell));
1946
1947 input->seek(pos+15,librevenge::RVNG_SEEK_SET);
1948 ascFile.addPos(pos);
1949 ascFile.addNote(f.str().c_str());
1950 }
1951 return true;
1952 }
1953
1954 bool LotusStyleManager::readStyleE6(std::shared_ptr<WPSStream> stream, long endPos)
1955 {
1956 if (!stream) return false;
1957 RVNGInputStreamPtr &input = stream->m_input;
1958 libwps::DebugFile &ascFile=stream->m_ascii;
1959 libwps::DebugStream f;
1960
1961 long pos = input->tell();
1962 long sz=endPos-pos;
1963 if (sz!=8)
1964 {
1965 WPS_DEBUG_MSG(("LotusStyleManager::readStyleE6: the zone size seems bad\n"));
1966 ascFile.addPos(pos-6);
1967 ascFile.addNote("Entries(MainStyle):###");
1968 return true;
1969 }
1970 f << "Entries(MainStyle):";
1971 auto id=int(libwps::readU8(input));
1972 auto val=int(libwps::readU8(input)); // always 50?
1973 if (val!=0x60)
1974 f << "##type=" << std::hex << val << std::dec << ",";
1975 f << "ST" << id << ",";
1976 val=int(libwps::readU32(input));
1977 if (val) f << "used=" << val << ",";
1978 val=int(libwps::readU16(input));
1979 int cellId=-1;
1980 if ((val>>8)==0x50)
1981 {
1982 cellId=(val&0xFF);
1983 f << "Ce" << (val&0xFF) << ",";
1984 }
1985 else if (val)
1986 {
1987 WPS_DEBUG_MSG(("LotusStyleManager::readStyleE6: find unexpected value\n"));
1988 f << "##Ce" << std::hex << val << std::dec << ",";
1989 }
1990 if (cellId>=0)
1991 {
1992 if (m_state->m_idToParentStyleMap.find(id)!=m_state->m_idToParentStyleMap.end())
1993 {
1994 WPS_DEBUG_MSG(("LotusStyleManager::readStyleE6: the cell style %d already exists\n", id));
1995 f << "###id";
1996 }
1997 else
1998 m_state->m_idToParentStyleMap.insert(std::map<int, int>::value_type(id,cellId));
1999 }
2000 ascFile.addPos(pos-6);
2001 ascFile.addNote(f.str().c_str());
2002 return true;
2003 }
2004
2005 bool LotusStyleManager::updateCellStyle(int cellId, WPSCellFormat &format,
2006 WPSFont &font, libwps_tools_win::Font::Type &fontType)
2007 {
2008 if (cellId==0)
2009 return true;
2010 if (m_state->m_idCellStyleMap.find(cellId)==m_state->m_idCellStyleMap.end() &&
2011 m_state->m_idToCellStyleEntryMap.find(cellId)!=m_state->m_idToCellStyleEntryMap.end())
2012 {
2013 std::set<int> seen;
2014 seen.insert(cellId);
2015 readCellStyleD2Data(m_state->m_idToCellStyleEntryMap.find(cellId)->second, seen);
2016 }
2017 if (m_state->m_idCellStyleMap.find(cellId)==m_state->m_idCellStyleMap.end())
2018 {
2019 static bool first=true;
2020 if (first)
2021 {
2022 WPS_DEBUG_MSG(("LotusStyleManager::updateCellStyle: the cell style %d does not exist\n", cellId));
2023 first=false;
2024 }
2025 return false;
2026 }
2027 int const vers=version();
2028 auto const &cellStyle=m_state->m_idCellStyleMap.find(cellId)->second;
2029 if (vers>=3)
2030 {
2031 font=cellStyle.m_fontStyle.m_font;
2032 fontType=cellStyle.m_fontStyle.m_fontType;
2033 format.setFont(font);
2034 format.setTextRotation(cellStyle.m_rotation);
2035 for (int i=0; i<4; ++i)
2036 {
2037 static int const wh[]= {WPSBorder::TopBit,WPSBorder::BottomBit,WPSBorder::LeftBit,WPSBorder::RightBit};
2038 format.setBorders(wh[i],cellStyle.m_bordersStyle[i]);
2039 }
2040 }
2041 // wk1 or wk3
2042 if ((vers!=3 && cellStyle.m_colorsId[0]) || vers>=3)
2043 {
2044 if (vers!=3 && m_state->m_idColorStyleMap.find(cellStyle.m_colorsId[0])==m_state->m_idColorStyleMap.end())
2045 {
2046 WPS_DEBUG_MSG(("LotusStyleManager::updateCellStyle: the color style %d does not exist\n", cellStyle.m_colorsId[0]));
2047 }
2048 else
2049 {
2050 auto const &color= vers>=3 ? cellStyle.m_colorStyle
2051 : m_state->m_idColorStyleMap.find(cellStyle.m_colorsId[0])->second;
2052 if (color.m_patternId) // not empty
2053 {
2054 WPSColor finalColor=color.m_colors[2];
2055 WPSGraphicStyle::Pattern pattern;
2056 if (color.m_patternId==2)
2057 finalColor=color.m_colors[3];
2058 else if (color.m_patternId!=1 &&
2059 ((vers<3 && m_state->getPattern48(color.m_patternId, pattern)) ||
2060 (vers>=3 && m_state->getPattern64(color.m_patternId, pattern))))
2061 {
2062 pattern.m_colors[0]=color.m_colors[3];
2063 pattern.m_colors[1]=color.m_colors[2];
2064 pattern.getAverageColor(finalColor);
2065 }
2066 format.setBackgroundColor(finalColor);
2067 }
2068 }
2069 }
2070 if (vers==3) return true;
2071 if (cellStyle.m_hAlign!=WPSCellFormat::HALIGN_DEFAULT)
2072 format.setHAlignment(cellStyle.m_hAlign);
2073 if (cellStyle.m_vAlign!=WPSCellFormat::VALIGN_DEFAULT)
2074 format.setVAlignment(cellStyle.m_vAlign);
2075 if (cellStyle.m_wrapping!=WPSCellFormat::WRAP_DEFAULT)
2076 format.setWrapping(cellStyle.m_wrapping);
2077 // wk3
2078 if (cellStyle.m_fontId>=0)
2079 {
2080 if (updateFontStyle(cellStyle.m_fontId, font, fontType))
2081 format.setFont(font);
2082 }
2083 if (!cellStyle.m_borders)
2084 return true;
2085 for (int i=0,depl=1; i<4; ++i, depl*=2)
2086 {
2087 if ((cellStyle.m_borders&depl)==0) continue;
2088 static int const wh[]= {WPSBorder::TopBit,WPSBorder::LeftBit,WPSBorder::BottomBit,WPSBorder::RightBit};
2089 WPSBorder border;
2090 format.setBorders(wh[i],border);
2091 }
2092 return true;
2093 }
2094
2095 bool LotusStyleManager::readFMTFontName(std::shared_ptr<WPSStream> stream)
2096 {
2097 if (!stream) return false;
2098 RVNGInputStreamPtr &input=stream->m_input;
2099 libwps::DebugFile &ascFile=stream->m_ascii;
2100 libwps::DebugStream f;
2101
2102 long pos = input->tell();
2103 auto type = int(libwps::read16(input));
2104 if (type!=0xae)
2105 {
2106 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontName: not a font name definition\n"));
2107 return false;
2108 }
2109 auto sz = long(libwps::readU16(input));
2110 long endPos=pos+4+sz;
2111 f << "Entries(FMTFont)[name]:";
2112 if (sz < 2)
2113 {
2114 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontName: the zone is too short\n"));
2115 f << "###";
2116 ascFile.addPos(pos);
2117 ascFile.addNote(f.str().c_str());
2118 return true;
2119 }
2120 auto id=int(libwps::readU8(input));
2121 f << "id=" << id << ",";
2122 bool nameOk=true;
2123 std::string name("");
2124 for (long i=1; i<sz; ++i)
2125 {
2126 auto c=char(libwps::readU8(input));
2127 if (!c) break;
2128 if (nameOk && !(c==' ' || (c>='0'&&c<='9') || (c>='a'&&c<='z') || (c>='A'&&c<='Z')))
2129 {
2130 nameOk=false;
2131 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontName: find odd character in name\n"));
2132 f << "#";
2133 }
2134 name += c;
2135 }
2136 f << name << ",";
2137 if (m_state->m_idFontNameMap.find(id)!=m_state->m_idFontNameMap.end())
2138 {
2139 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontName: can not update font map for id=%d\n", id));
2140 }
2141 else
2142 {
2143 LotusStyleManagerInternal::FontName font;
2144 font.m_name=name;
2145 m_state->m_idFontNameMap[id]=font;
2146 }
2147 if (input->tell()!=endPos)
2148 {
2149 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontName: find extra data\n"));
2150 f << "###extra";
2151 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2152 }
2153 ascFile.addPos(pos);
2154 ascFile.addNote(f.str().c_str());
2155 return true;
2156 }
2157
2158 bool LotusStyleManager::readFMTFontId(std::shared_ptr<WPSStream> stream)
2159 {
2160 if (!stream) return false;
2161 RVNGInputStreamPtr &input=stream->m_input;
2162 libwps::DebugFile &ascFile=stream->m_ascii;
2163 libwps::DebugStream f;
2164
2165 long pos = input->tell();
2166 auto type = int(libwps::read16(input));
2167 if (type!=0xb0)
2168 {
2169 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontId: not a font id definition\n"));
2170 return false;
2171 }
2172 auto sz = long(libwps::readU16(input));
2173 long endPos=pos+4+sz;
2174 f << "Entries(FMTFont)[ids]:";
2175 if ((sz%2)!=0)
2176 {
2177 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontId: the zone size is odd\n"));
2178 f << "###";
2179 ascFile.addPos(pos);
2180 ascFile.addNote(f.str().c_str());
2181 return true;
2182 }
2183 f << "ids=[";
2184 bool isFirstError=true;
2185 for (int i=0; i<int(sz)/2; ++i)
2186 {
2187 auto id =int(libwps::readU16(input));
2188 f << id << ",";
2189 if (m_state->m_idFontNameMap.find(i)!=m_state->m_idFontNameMap.end())
2190 m_state->m_idFontNameMap.find(i)->second.m_id=id;
2191 else if (isFirstError)
2192 {
2193 isFirstError=false;
2194 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontId: can not update some font map for id=%d\n", id));
2195 }
2196 }
2197 f << "],";
2198 if (input->tell()!=endPos)
2199 {
2200 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontId: find extra data\n"));
2201 f << "###extra";
2202 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2203 }
2204 ascFile.addPos(pos);
2205 ascFile.addNote(f.str().c_str());
2206 return true;
2207 }
2208
2209 bool LotusStyleManager::readFMTFontSize(std::shared_ptr<WPSStream> stream)
2210 {
2211 if (!stream) return false;
2212 RVNGInputStreamPtr &input=stream->m_input;
2213 libwps::DebugFile &ascFile=stream->m_ascii;
2214 libwps::DebugStream f;
2215
2216 long pos = input->tell();
2217 auto type = int(libwps::read16(input));
2218 if (type!=0xaf && type!=0xb1)
2219 {
2220 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontSize: not a font size definition\n"));
2221 return false;
2222 }
2223 auto sz = long(libwps::readU16(input));
2224 long endPos=pos+4+sz;
2225 int const wh=type==0xaf ? 0 : 1;
2226 f << "Entries(FMTFont)[size" << wh << "]:";
2227 if ((sz%2)!=0)
2228 {
2229 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontSize: the zone size is odd\n"));
2230 f << "###";
2231 ascFile.addPos(pos);
2232 ascFile.addNote(f.str().c_str());
2233 return true;
2234 }
2235 f << "size=[";
2236 bool isFirstError=true;
2237 for (int i=0; i<int(sz)/2; ++i)
2238 {
2239 auto size =int(libwps::readU16(input));
2240 f << size << ",";
2241 if (m_state->m_idFontNameMap.find(i)!=m_state->m_idFontNameMap.end())
2242 m_state->m_idFontNameMap.find(i)->second.m_size[wh]=size;
2243 else if (isFirstError)
2244 {
2245 isFirstError=false;
2246 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontSize: can not update some font map for size=%d\n", size));
2247 }
2248 }
2249 f << "],";
2250 if (input->tell()!=endPos)
2251 {
2252 WPS_DEBUG_MSG(("LotusStyleManager::readFMTFontSize: find extra data\n"));
2253 f << "###extra";
2254 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2255 }
2256 ascFile.addPos(pos);
2257 ascFile.addNote(f.str().c_str());
2258 return true;
2259 }
2260
2261 bool LotusStyleManager::readMenuStyleE7(std::shared_ptr<WPSStream> stream, long endPos)
2262 {
2263 if (!stream) return false;
2264 RVNGInputStreamPtr &input = stream->m_input;
2265 libwps::DebugFile &ascFile=stream->m_ascii;
2266 libwps::DebugStream f;
2267
2268 long pos = input->tell();
2269 if (endPos-pos<23)
2270 {
2271 WPS_DEBUG_MSG(("LotusStyleManager::readMenuStyleE7 the zone size seems bad\n"));
2272 ascFile.addPos(pos-6);
2273 ascFile.addNote("Entries(MenuStyle):###");
2274 return true;
2275 }
2276 f << "Entries(MenuStyle):";
2277 f << "id=" << libwps::readU16(input) << ",";
2278 for (int i=0; i<2; ++i) // fl0=0|[048c]0FF, fl1=[04]00[0-3]
2279 {
2280 auto val=int(libwps::readU16(input));
2281 if (val) f << "fl" << i << "=" << std::hex << val << std::dec << ",";
2282 }
2283 std::string name;
2284 for (int i=0; i<16; ++i)
2285 {
2286 auto c=char(libwps::readU8(input));
2287 if (!c) break;
2288 name += c;
2289 }
2290 f << name << ",";
2291 input->seek(pos+22, librevenge::RVNG_SEEK_SET);
2292 name="";
2293 auto maxN=int(endPos-input->tell());
2294 for (int i=0; i<maxN; ++i)
2295 {
2296 auto c=char(libwps::readU8(input));
2297 if (!c) break;
2298 name += c;
2299 }
2300 f << name;
2301 if (input->tell() != endPos)
2302 ascFile.addDelimiter(input->tell(),'|');
2303 ascFile.addPos(pos-6);
2304 ascFile.addNote(f.str().c_str());
2305 return true;
2306 }
2307
2308 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */