Debian Codegraph @

src/lib/RagTime5Spreadsheet.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 "MWAWCell.hxx"
46 #include "MWAWFont.hxx"
47 #include "MWAWGraphicEncoder.hxx"
48 #include "MWAWGraphicListener.hxx"
49 #include "MWAWListener.hxx"
50 #include "MWAWParagraph.hxx"
51 #include "MWAWPosition.hxx"
52 #include "MWAWSection.hxx"
53 #include "MWAWSpreadsheetEncoder.hxx"
54 #include "MWAWSpreadsheetListener.hxx"
55 #include "MWAWSubDocument.hxx"
56
57 #include "RagTime5ClusterManager.hxx"
58 #include "RagTime5Document.hxx"
59 #include "RagTime5Formula.hxx"
60 #include "RagTime5StructManager.hxx"
61 #include "RagTime5StyleManager.hxx"
62
63 #include "RagTime5Spreadsheet.hxx"
64
65 #include "libmwaw_internal.hxx"
66
67 /** Internal: the structures of a RagTime5Spreadsheet */
68 namespace RagTime5SpreadsheetInternal
69 {
70
71 //! internal: a structure used to store a value in a cell in RagTime5SpreadsheetInternal
72 struct CellValue {
73 //! constructor
74 CellValue()
75 : m_type(0)
76 , m_id(0)
77 , m_long(0)
78 , m_double(0)
79 , m_text("")
80 , m_formulaId(0)
81 , m_extra("")
82 {
83 }
84 //! update the cell's content
85 void update(MWAWCell &cell, MWAWCellContent &content) const
86 {
87 MWAWCell::Format format=cell.getFormat();
88 switch (m_type) {
89 case 4:
90 format.m_format=MWAWCell::F_NUMBER;
91 content.m_contentType=MWAWCellContent::C_NUMBER;
92 content.setValue(m_double);
93 break;
94 case 5:
95 format.m_format=MWAWCell::F_DATE;
96 content.m_contentType=MWAWCellContent::C_NUMBER;
97 content.setValue(m_double+1460);
98 break;
99 case 6:
100 format.m_format=MWAWCell::F_TIME;
101 content.m_contentType=MWAWCellContent::C_NUMBER;
102 content.setValue(m_double);
103 break;
104 case 7:
105 format.m_format=MWAWCell::F_TEXT;
106 content.m_contentType=MWAWCellContent::C_TEXT;
107 break;
108 default:
109 break;
110 }
111 cell.setFormat(format);
112 }
113 //! operator<<
114 friend std::ostream &operator<<(std::ostream &o, CellValue const &cell)
115 {
116 switch (cell.m_type) {
117 case 0: // empty
118 break;
119 case 1: // cell is masqued because previous cell text is too width, ...
120 o << "masked=" << cell.m_id << ",";
121 break;
122 case 2: // checkme
123 o << "nan=" << std::hex << cell.m_long << std::dec << ",";
124 break;
125 case 4:
126 o << "number=" << cell.m_double << ",";
127 break;
128 case 5:
129 o << "date=" << cell.m_double << ",";
130 break;
131 case 6:
132 o << "time=" << cell.m_double << ",";
133 break;
134 case 7:
135 o << "text=\"" << cell.m_text.cstr() << "\",";
136 break;
137 case 8: // simple text zone
138 o << "textZone=" << (cell.m_id&0xFFFFFF) << "[" << (cell.m_id>>24) << "],";
139 break;
140 case 9:
141 o << "zone[id]=" << (cell.m_id&0xFFFFFF) << ":" << (cell.m_id>>24) << ",";
142 break;
143 case 0xa:
144 o << "pict[id]=" << cell.m_id << ",";
145 break;
146 default:
147 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::CellValue::operator<<: unknown type\n"));
148 o << "##type=" << cell.m_type << ",";
149 }
150 if (cell.m_formulaId)
151 o << "formulaDefFD-" << cell.m_formulaId << ",";
152 o << cell.m_extra;
153 return o;
154 }
155 //! the cell type
156 int m_type;
157 //! a id value
158 unsigned long m_id;
159 //! a long value
160 long m_long;
161 //! a double value
162 double m_double;
163 //! the text
164 librevenge::RVNGString m_text;
165 //! the formula id
166 int m_formulaId;
167 //! extra data
168 std::string m_extra;
169 };
170
171 //! a struct to store what a cell contains
172 struct CellContent {
173 //! enum to define the id position
174 enum IdPosition { Value=0, Union, GraphicStyle, TextStyle,
175 BorderPrevVStyle, BorderNextVStyle, BorderPrevHStyle, BorderNextHStyle
176 };
177
178 //! constructor
179 CellContent(MWAWVec2i const &pos, int plane)
180 : m_position(pos)
181 , m_plane(plane)
182 , m_isMerged(false)
183 {
184 for (auto &id : m_id) id=-1;
185 }
186 //! returns true if the cell is merged
187 bool isMergedCell() const
188 {
189 return m_isMerged;
190 }
191 //! sets the cell content
192 void setContent(int id, int contentId)
193 {
194 if (id<0 || id>=8) {
195 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::CellContent: called with bad id=%d\n", id));
196 }
197 else
198 m_id[id]=contentId;
199 }
200 //! returns true if the cell has no id or is merged
201 bool hasContent() const
202 {
203 if (m_isMerged) return false;
204 for (auto id : m_id) {
205 if (id>=0)
206 return true;
207 }
208 return false;
209 }
210 //! small operator<<
211 friend std::ostream &operator<<(std::ostream &o, CellContent const &cell)
212 {
213 if (cell.m_isMerged) o << "_[" << cell.m_position << "]";
214 else if (!cell.hasContent()) o << "*";
215 else {
216 o << "[";
217 for (int i=0; i<8; ++i) {
218 if (cell.m_id[i]<0) continue;
219 static char const *wh[]= {"V", "U", "G", "T", "bv", "BV", "bh", "BH"};
220 o << wh[i] << cell.m_id[i] << ",";
221 }
222 o << "]";
223 }
224 return o;
225 }
226 /** the cell position
227
228 \note if the cell is an merged cell, this corresponds to the first cell*/
229 MWAWVec2i m_position;
230 //! the cell plane
231 int m_plane;
232 //! a flag to know if the cell is merged
233 bool m_isMerged;
234 //! the list of id
235 int m_id[8];
236 };
237
238 //! a border style PLC
239 struct BorderPLC {
240 //! constructor
241 BorderPLC()
242 : m_values(6,0)
243 {
244 }
245 //! constructor
246 explicit BorderPLC(std::vector<int> const &values) : m_values(values)
247 {
248 if (values.size()==6)
249 return;
250 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::BorderPLC::BorderPLC: bad value size\n"));
251 m_values.resize(6,0);
252 }
253 //! returns true if the cell is a merged cell
254 bool isMergedBorder() const
255 {
256 return m_values.size()==6 && (m_values[5]&0x300)==0x300;
257 }
258 //! returns the graphic style border id corresponding to this cell
259 int getBorderGraphicStyleId(bool prevCell) const
260 {
261 return m_values[prevCell ? 0 : 2];
262 }
263 //! operator<<
264 friend std::ostream &operator<<(std::ostream &o, BorderPLC const &plc)
265 {
266 for (size_t i=0; i<6; ++i) {
267 int val=plc.m_values[i];
268 if (!val) continue;
269 switch (i) {
270 case 0:
271 o << "GS" << val << "[prevCell],";
272 break;
273 case 2:
274 o << "GS" << val << "[nextCell],";
275 break;
276 case 5:
277 if ((val&0x300)==0x300) {
278 o << "none[merged],";
279 val&=0xFCFF;
280 }
281 // find also 2
282 if (val)
283 o << "fl=" << std::hex << val << std::dec << ",";
284 break;
285 default: // always 0?
286 o << "f" << i << "=" << val << ",";
287 break;
288 }
289 }
290 return o;
291 }
292 //! operator==
293 bool operator==(BorderPLC const &plc) const
294 {
295 for (size_t i=0; i<6; ++i) {
296 if (m_values[i]!=plc.m_values[i])
297 return false;
298 }
299 return true;
300 }
301 //! operator!=
302 bool operator!=(BorderPLC const &plc) const
303 {
304 return !(*this==plc);
305 }
306 //! the values
307 std::vector<int> m_values;
308 };
309
310 //! a graphic style PLC
311 struct GraphicPLC {
312 //! constructor
313 GraphicPLC()
314 : m_graphStyleId(0)
315 , m_unknownId(0)
316 {
317 }
318 //! constructor
319 explicit GraphicPLC(std::vector<int> const &values)
320 : m_graphStyleId(0)
321 , m_unknownId(0)
322 {
323 if (values.size()!=2) {
324 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::GraphicPLC::GraphicPLC: bad value size\n"));
325 return;
326 }
327 m_unknownId=values[0];
328 m_graphStyleId=values[1];
329 }
330 //! returns the graphic id
331 int getGraphicStyleId() const
332 {
333 return m_graphStyleId;
334 }
335 //! operator<<
336 friend std::ostream &operator<<(std::ostream &o, GraphicPLC const &plc)
337 {
338 if (plc.m_graphStyleId) o << "GS" << plc.m_graphStyleId << ",";
339 if (plc.m_unknownId) o << "#unkn=" << plc.m_unknownId << ",";
340 return o;
341 }
342 //! operator==
343 bool operator==(GraphicPLC const &plc) const
344 {
345 return m_graphStyleId==plc.m_graphStyleId && m_unknownId==plc.m_unknownId;
346 }
347 //! operator!=
348 bool operator!=(GraphicPLC const &plc) const
349 {
350 return !(*this==plc);
351 }
352 //! the graph style
353 int m_graphStyleId;
354 //! unknown id (always 0)
355 int m_unknownId;
356 };
357
358 //! a text style PLC
359 struct TextPLC {
360 //! constructor
361 TextPLC()
362 : m_textStyleId(0)
363 , m_formatId(0)
364 , m_flags(0)
365 {
366 }
367 //! constructor
368 explicit TextPLC(std::vector<int> const &values)
369 : m_textStyleId(0)
370 , m_formatId(0)
371 , m_flags(0)
372 {
373 if (values.size()!=3) {
374 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::TextPLC::TextPLC: bad value size\n"));
375 return;
376 }
377 m_textStyleId=values[0];
378 m_formatId=values[1];
379 m_flags=values[2];
380 }
381 //! returns the alignement
382 MWAWCell::HorizontalAlignment getHorizontalAlignment() const
383 {
384 switch (m_flags&3) {
385 case 1:
386 return MWAWCell::HALIGN_LEFT;
387 case 2:
388 return MWAWCell::HALIGN_CENTER;
389 case 3:
390 return MWAWCell::HALIGN_RIGHT;
391 default:
392 case 0: // none
393 break;
394 }
395 return MWAWCell::HALIGN_DEFAULT;
396 }
397 //! operator<<
398 friend std::ostream &operator<<(std::ostream &o, TextPLC const &plc)
399 {
400 if (plc.m_textStyleId) o << "TS" << plc.m_textStyleId << ",";
401 if (plc.m_formatId) o << "Fo" << plc.m_formatId << ",";
402 auto orientationFlags=plc.m_flags>>16;
403 if (orientationFlags&3) o << "orientation=" << (orientationFlags&3) << ","; // 0:LR, BT, RL, TB USEME
404 if (orientationFlags&0x1c) o << "vert[just]=" << ((orientationFlags&0x1c)>>2) << ","; // 0: standart, 1:T, 2:C, 3:bottom, 4:baseline
405 if (orientationFlags&0x20) o << "tategaki,";
406 if (orientationFlags&0xffc0) o << "##orient=" << (orientationFlags&0xffc0) << ",";
407 auto fl=plc.m_flags&0xffff;
408 if (fl) {
409 switch (fl&3) {
410 case 0: // none
411 break;
412 case 1:
413 o << "align=left,";
414 break;
415 case 2:
416 o << "align=center,";
417 break;
418 case 3:
419 o << "align=right,";
420 break;
421 default:
422 break;
423 }
424 if (fl&0x180)
425 o << "protection=" << ((fl&0x180)>>7) << ",";
426 if (fl&0x400)
427 o << "no[print],";
428 if (fl&0x800)
429 o << "no[screen],";
430 if (fl&0x1000)
431 o << "zero[hide],";
432 if (fl&0x2000)
433 o << "precision[use,format],";
434 if (fl&0x4000)
435 o << "formula[preserved],";
436 // fl&0x10 related to multitext?
437 fl &= 0x82bc;
438 if (fl) // find [04][03][0348][08]
439 o << "fl=" << std::hex << fl << std::dec << ",";
440 }
441 return o;
442 }
443 //! operator==
444 bool operator==(TextPLC const &plc) const
445 {
446 return m_textStyleId==plc.m_textStyleId && m_formatId==plc.m_formatId && m_flags==plc.m_flags;
447 }
448 //! operator!=
449 bool operator!=(TextPLC const &plc) const
450 {
451 return !(*this==plc);
452 }
453 //! the text style
454 int m_textStyleId;
455 //! the format style
456 int m_formatId;
457 //! low: text flag, high: orientation flags, tategaki, ...
458 int m_flags;
459 };
460
461 //! internal: a structure used to store a sheet in RagTime5SpreadsheetInternal
462 struct Sheet {
463 /** a row: a list of cell map */
464 struct Row {
465 //! constructor
466 Row(MWAWVec2i const &row, int plane)
467 : m_rows(row)
468 , m_columnsToDataMap()
469 {
470 // create the spreadsheet zone
471 m_columnsToDataMap.insert(std::map<MWAWVec2i, CellContent>::value_type(MWAWVec2i(0,15999),CellContent(MWAWVec2i(0,m_rows[0]), plane)));
472 }
473 //! returns the rows
474 MWAWVec2i const &getRows() const
475 {
476 return m_rows;
477 }
478 //! returns true if the row is empty
479 bool isEmpty() const
480 {
481 for (auto const &it : m_columnsToDataMap) {
482 if (it.second.hasContent())
483 return false;
484 }
485 return true;
486 }
487 //! split columns if needed, so that we can insert cells correspond to the cols interval
488 void splitColumns(MWAWVec2i const &cols)
489 {
490 // first find the first columns block
491 auto it=m_columnsToDataMap.lower_bound(MWAWVec2i(-1,cols[0]));
492 if (it==m_columnsToDataMap.end()) {
493 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::Row::splitColumns: argh can not find any column for %d-%d\n", cols[0], cols[1]));
494 return;
495 }
496 while (it!=m_columnsToDataMap.end()) {
497 MWAWVec2i contentCols=it->first;
498 if (cols[1]<contentCols[0])
499 return;
500 if (cols[0]<=contentCols[0] && contentCols[1]<=cols[1]) { // no need to split the cell
501 ++it;
502 continue;
503 }
504 CellContent content=it->second;
505 int breakPos=(cols[0]>contentCols[0] && cols[0]<=contentCols[1]) ? 0 :
506 (/*cols[1]>=contentCols[0] &&*/ cols[1]<contentCols[1]) ? 1 : -1;
507 if (breakPos==-1) {
508 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::Row::splitColumns: argh can not find break pos %d-%d\n", cols[0], cols[1]));
509 return;
510 }
511 m_columnsToDataMap.erase(contentCols);
512 int newMinCol=cols[breakPos]+(breakPos==0?0:1);
513 m_columnsToDataMap.insert
514 (std::map<MWAWVec2i, CellContent>::value_type(MWAWVec2i(contentCols[0], newMinCol-1),content));
515 // check if we need to update the cell position
516 if (!content.m_isMerged && content.m_id[CellContent::Union]==-1)
517 content.m_position[0]=newMinCol;
518 else
519 content.m_isMerged=true;
520 it=m_columnsToDataMap.insert
521 (std::map<MWAWVec2i, CellContent>::value_type(MWAWVec2i(newMinCol, contentCols[1]),content)).first;
522 }
523 }
524 //! update the cells content type
525 void update(MWAWVec2i const &cols, int id, int contentId, MWAWVec2i const &beginCellPos, std::set<MWAWVec2i> &unsetCell)
526 {
527 splitColumns(cols);
528 auto it=m_columnsToDataMap.lower_bound(MWAWVec2i(-1,cols[0]));
529 if (it==m_columnsToDataMap.end()) {
530 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::Row::update: argh can not find any column for %d-%d\n", cols[0], cols[1]));
531 return;
532 }
533 while (it!=m_columnsToDataMap.end()) {
534 MWAWVec2i const cPos=it->first;
535 if (cPos[0]>cols[1]) break;
536 if (cPos[0]<cols[0]||cPos[1]>cols[1]) {
537 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::Row::update: argh can insert some columns for %d-%d\n", cols[0], cols[1]));
538 break;
539 }
540 CellContent &content=(it++)->second;
541 if (content.m_isMerged) {
542 unsetCell.insert(content.m_position);
543 continue;
544 }
545 content.setContent(id, contentId);
546 if (id==CellContent::Union && content.m_position!=beginCellPos) {
547 content.m_position=beginCellPos;
548 content.m_isMerged=true;
549 }
550 }
551 }
552 //! reset each row's cell position to new row position
553 void resetMinRow(int row)
554 {
555 for (auto &it : m_columnsToDataMap) {
556 if (it.second.m_id[CellContent::Union]==-1)
557 it.second.m_position[1]=row;
558 else
559 it.second.m_isMerged=true;
560 }
561 }
562 friend std::ostream &operator<<(std::ostream &o, Row const &row)
563 {
564 for (auto const &it : row.m_columnsToDataMap)
565 o << it.first << ":" << it.second << ",";
566 return o;
567 }
568 //! the rows (min-max)
569 MWAWVec2i m_rows;
570 //! the map columns to data
571 std::map<MWAWVec2i, CellContent> m_columnsToDataMap;
572 };
573
574 /** a plane: a list of rows map */
575 struct Plane {
576 //! constructor
577 explicit Plane(int plane)
578 : m_plane(plane)
579 , m_rowsToDataMap()
580 , m_unitedCellMap()
581 {
582 // create the spreadsheet zone
583 m_rowsToDataMap.insert(std::map<MWAWVec2i, Row>::value_type(MWAWVec2i(0,15999),Row(MWAWVec2i(0,15999), plane)));
584 }
585 //! returns the plane
586 int getPlane() const
587 {
588 return m_plane;
589 }
590 //! returns true if the row is empty
591 bool isEmpty() const
592 {
593 for (auto const &it : m_rowsToDataMap) {
594 if (!it.second.isEmpty())
595 return false;
596 }
597 return true;
598 }
599
600 //! returns the span value corresponding to an id
601 MWAWVec2i getSpan(MWAWVec2i const &position) const
602 {
603 auto it=m_unitedCellMap.find(position);
604 if (it!=m_unitedCellMap.end())
605 return MWAWVec2i(it->second[0]-it->first[0]+1, it->second[1]-it->first[1]+1);
606 return MWAWVec2i(1,1);
607 }
608
609 //! split rows if needed, so that we can insert cells correspond to the rows interval
610 void splitRows(MWAWVec2i const &rows)
611 {
612 // first find the first rows block
613 auto it=m_rowsToDataMap.lower_bound(MWAWVec2i(-1,rows[0]));
614 if (it==m_rowsToDataMap.end()) {
615 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::Plane::splitRows: argh can not find any row for %d-%d\n", rows[0], rows[1]));
616 return;
617 }
618 while (it!=m_rowsToDataMap.end()) {
619 MWAWVec2i contentRows=it->first;
620 if (rows[1]<contentRows[0])
621 return;
622 if (rows[0]<=contentRows[0] && contentRows[1]<=rows[1]) { // no need to split the cell
623 ++it;
624 continue;
625 }
626 Row rContent=it->second;
627 int breakPos=(rows[0]>contentRows[0] && rows[0]<=contentRows[1]) ? 0 :
628 (/*rows[1]>=contentRows[0] &&*/ rows[1]<contentRows[1]) ? 1 : -1;
629 if (breakPos==-1) {
630 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::Plane::splitRows: argh can not find break pos %d-%d\n", rows[0], rows[1]));
631 return;
632 }
633 int newMinRow=rows[breakPos]+(breakPos==0?0:1);
634 m_rowsToDataMap.erase(contentRows);
635 rContent.m_rows=MWAWVec2i(contentRows[0], newMinRow-1);
636 m_rowsToDataMap.insert(std::map<MWAWVec2i, Row>::value_type(rContent.m_rows,rContent));
637
638 rContent.resetMinRow(newMinRow);
639 rContent.m_rows=MWAWVec2i(newMinRow, contentRows[1]);
640 it=m_rowsToDataMap.insert(std::map<MWAWVec2i, Row>::value_type(rContent.m_rows,rContent)).first;
641 }
642 }
643 //! update the cells content type
644 void update(Sheet const &sheet, MWAWBox2i const &box, int id, int contentId)
645 {
646 MWAWVec2i rows(box[0][1], box[1][1]), cols(box[0][0], box[1][0]);
647 splitRows(rows);
648 auto it=m_rowsToDataMap.lower_bound(MWAWVec2i(-1,rows[0]));
649 if (it==m_rowsToDataMap.end()) {
650 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::Plane::update: argh can not find any rows for %d-%d\n", rows[0], rows[1]));
651 return;
652 }
653 std::set<MWAWVec2i> unsetCell;
654 while (it!=m_rowsToDataMap.end()) {
655 MWAWVec2i const rPos=it->first;
656 if (rPos[0]>rows[1]) break;
657 if (rPos[0]<rows[0]||rPos[1]>rows[1]) {
658 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::Plane::update: argh can insert some rows for %d-%d\n", rows[0], rows[1]));
659 break;
660 }
661 it->second.update(cols, id, contentId, box[0], unsetCell);
662 ++it;
663 }
664 if (unsetCell.empty() || id==CellContent::GraphicStyle || id==CellContent::TextStyle ||
665 id==CellContent::BorderPrevHStyle || id==CellContent::BorderPrevVStyle)
666 return;
667 // sometimes merged cell have value with no content
668 if (id==CellContent::Value && contentId>0 && contentId<=static_cast<int>(sheet.m_valuesList.size()) &&
669 sheet.m_valuesList[size_t(contentId-1)].m_type==0)
670 return;
671 // if the cells have been merged, we need to affect the next border to the original cell
672 if (id==CellContent::BorderNextHStyle || id==CellContent::BorderNextVStyle) {
673 for (auto const &cellPos : unsetCell) {
674 it=m_rowsToDataMap.lower_bound(MWAWVec2i(-1,cellPos[1]));
675 if (it==m_rowsToDataMap.end() || it->first[0]!=cellPos[1]) {
676 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::Plane::update: argh can not find a cell to set border: %dx%d\n", cellPos[0], cellPos[1]));
677 continue;
678 }
679 Row &row=it->second;
680 auto rIt=row.m_columnsToDataMap.lower_bound(MWAWVec2i(-1,cellPos[0]));
681 if (rIt==row.m_columnsToDataMap.end() || rIt->first[0]!=cellPos[0] || rIt->second.m_id[CellContent::Union]<0) {
682 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::Plane::update: argh can not find a cell to set border: %dx%d(II)\n", cellPos[0], cellPos[1]));
683 continue;
684 }
685 rIt->second.setContent(id,contentId);
686 }
687 return;
688 }
689 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::Plane::update: argh can not set some cell for id=%d\n", id));
690 }
691
692 friend std::ostream &operator<<(std::ostream &o, Plane const &plane)
693 {
694 if (plane.m_rowsToDataMap.empty()) return o;
695 for (auto const &it : plane.m_rowsToDataMap)
696 o << "\t" << it.first << "[" << plane.m_plane << "]:" << it.second << "\n";
697 return o;
698 }
699
700 //! the plane
701 int m_plane;
702 //! the map rows to data
703 std::map<MWAWVec2i, Row> m_rowsToDataMap;
704 //! the list of united cell: map from TL cell to RB cell
705 std::map<MWAWVec2i, MWAWVec2i> m_unitedCellMap;
706 };
707
708 //! constructor
709 Sheet()
710 : m_name("")
711 , m_textboxZoneId(0)
712 , m_colWidthDef(56)
713 , m_colWidthsMap()
714 , m_rowHeightDef(13)
715 , m_rowHeightsMap()
716 , m_blockToCellRefMap()
717 , m_valueToCellRefMap()
718 , m_refToCellRefMap()
719 , m_formulaLink()
720 , m_idToFormula()
721 , m_valuesList()
722 , m_planesList()
723 , m_graphicPLCList()
724 , m_defGraphicPLC()
725 , m_textPLCList()
726 , m_defTextPLC()
727 , m_defaultFont(16, 12)
728 , m_defaultParagraph()
729 , m_childList()
730 , m_isSent(false)
731 {
732 }
733 //! returns a name corresponding to a plane
734 librevenge::RVNGString getName(int plane) const
735 {
736 if (plane==1)
737 return m_name;
738 librevenge::RVNGString name=m_name, suffix;
739 suffix.sprintf("_%d", plane);
740 name.append(suffix);
741 return name;
742 }
743 //! increase the number of planes if need
744 void increasePlaneSizeIfNeeded(int newPlane)
745 {
746 if (newPlane<0) {
747 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::increasePlaneSizeIfNeeded: called with bad id=%d\n", newPlane));
748 return;
749 }
750 for (auto plane=static_cast<int>(m_planesList.size()); plane<newPlane; ++plane) {
751 if (plane>=100) {
752 static bool first=true;
753 if (first) {
754 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::increasePlaneSizeIfNeeded: sorry, 100 planes is the arbitrary maximum\n"));
755 first=true;
756 }
757 return;
758 }
759 m_planesList.push_back(Plane(plane+1));
760 }
761 }
762 //! returns the number of planes
763 int getNumPlanes() const
764 {
765 int numPlanes=0;
766 for (size_t plane=m_planesList.size(); plane>=1; --plane) {
767 if (!m_planesList[plane-1].isEmpty())
768 return static_cast<int>(plane);
769 }
770 return numPlanes;
771 }
772
773 //! stores a plc
774 void setPLCValues(MWAWVec3i const &minPos, MWAWVec3i const &maxPos, int plcType, int plcId)
775 {
776 if (plcType==RagTime5SpreadsheetInternal::CellContent::Value && plcId) {
777 if (minPos[2]==maxPos[2]) {
778 MWAWCellContent::FormulaInstruction cells;
779 cells.m_type=minPos==maxPos ? MWAWCellContent::FormulaInstruction::F_Cell :
780 MWAWCellContent::FormulaInstruction::F_CellList;
781 cells.m_position[0]=MWAWVec2i(minPos[0],minPos[1]);
782 cells.m_position[1]=MWAWVec2i(maxPos[0],maxPos[1]);
783 cells.m_sheet[0]=getName(minPos[2]);
784 cells.m_sheet[1]=getName(maxPos[2]);
785 m_valueToCellRefMap[plcId]=cells;
786 }
787 else {
788 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::setPLCValues: storing value on multipleplane is not implemented\n"));
789 }
790 }
791 increasePlaneSizeIfNeeded(maxPos[2]);
792 MWAWBox2i box(MWAWVec2i(minPos[0],minPos[1]),MWAWVec2i(maxPos[0],maxPos[1]));
793 for (int plane=minPos[2]-1; plane<=maxPos[2]-1; ++plane) {
794 if (plane<0 || plane>=static_cast<int>(m_planesList.size())) {
795 static bool first=true;
796 if (first) {
797 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::setPLCValues: plane %d seems bad\n", plane));
798 first=false;
799 }
800 continue;
801 }
802 Plane &planeData=m_planesList[size_t(plane)];
803 planeData.update(*this, box, plcType, plcId);
804 }
805 }
806
807 //! stores an union of cells
808 void setMergedCells(MWAWVec3i const &minPos, MWAWVec3i const &maxPos)
809 {
810 increasePlaneSizeIfNeeded(maxPos[2]);
811 MWAWBox2i box(MWAWVec2i(minPos[0],minPos[1]),MWAWVec2i(maxPos[0],maxPos[1]));
812 for (int plane=minPos[2]-1; plane<=maxPos[2]-1; ++plane) {
813 if (plane<0 || plane>=static_cast<int>(m_planesList.size())) {
814 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::setMergedCells: plane %d seems bad\n", plane));
815 continue;
816 }
817 Plane &planeData=m_planesList[size_t(plane)];
818 planeData.m_unitedCellMap[box[0]]=box[1];
819 planeData.update(*this, box, CellContent::Union, 1);
820 if (minPos[1]!=maxPos[1]) // split the row to avoid sending merged cells
821 planeData.splitRows(MWAWVec2i(minPos[1],minPos[1]));
822 }
823 }
824
825 //! returns the row height in point
826 float getRowHeight(int row) const
827 {
828 auto rIt=m_rowHeightsMap.lower_bound(MWAWVec2i(-1,row));
829 if (rIt==m_rowHeightsMap.end() || rIt->first[0]>row || rIt->first[1]<row)
830 return -m_rowHeightDef;
831 return rIt->second;
832 }
833 //! sets the row heights
834 void setRowsHeight(MWAWVec2i const &rows, float height)
835 {
836 if (rows[0]<0 || rows[1]<rows[0]) {
837 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::setRowsHeight: the rows %dx%d seems bad\n", rows[0], rows[1]));
838 return;
839 }
840 m_rowHeightDef=m_rowHeightsMap[rows]=height;
841 }
842 //! returns the col width in point
843 float getColWidth(int col) const
844 {
845 auto rIt=m_colWidthsMap.lower_bound(MWAWVec2i(-1,col));
846 if (rIt==m_colWidthsMap.end() || rIt->first[0]>col || rIt->first[1]<col)
847 return m_colWidthDef;
848 return rIt->second;
849 }
850 //! returns the col width dimension in point
851 std::vector<float> getColumnWidths(std::vector<int> &repeated) const
852 {
853 std::vector<float> widths;
854 repeated.clear();
855 int actPos=0;
856 for (auto const &it : m_colWidthsMap) {
857 int lastPos=it.first[1];
858 if (lastPos<actPos) {
859 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::getColumnWidths: the position %d seems bad\n", lastPos));
860 continue;
861 }
862 widths.push_back(it.second);
863 repeated.push_back(lastPos+1-actPos);
864 actPos=lastPos;
865 }
866 return widths;
867 }
868 //! sets the row widths
869 void setColsWidth(MWAWVec2i const &cols, float width)
870 {
871 if (cols[0]<0 || cols[1]<cols[0]) {
872 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::setColsWidth: the cols %dx%d seems bad\n", cols[0], cols[1]));
873 return;
874 }
875 m_colWidthDef=m_colWidthsMap[cols]=width;
876 }
877 //! returns the cell dimension
878 MWAWVec2f getCellDimensions(MWAWVec2i const &position, int plane) const
879 {
880 MWAWVec2i maxPos=getBottomRightCell(position, plane);
881 float width=0;
882 for (int c=position[0]; c<maxPos[0]; ++c)
883 width+=getColWidth(c);
884 float height=0;
885 for (int r=position[1]; r<maxPos[1]; ++r) {
886 float h=getRowHeight(r);
887 height+=h<0 ? -h : h;
888 }
889 return MWAWVec2f(width, height);
890 }
891 //! returns the bottom right cell
892 MWAWVec2i getBottomRightCell(MWAWVec2i const &position, int plane) const
893 {
894 return position+getSpan(position, plane);
895 }
896 //! returns the span value corresponding to an id
897 MWAWVec2i getSpan(MWAWVec2i const &position, int plane) const
898 {
899 if (plane<=0 || plane>static_cast<int>(m_planesList.size()))
900 return MWAWVec2i(1,1);
901 return m_planesList[size_t(plane-1)].getSpan(position);
902 }
903
904 //! returns the graphic id
905 int getGraphicStyleId(int id) const
906 {
907 if (id<0 || id>=static_cast<int>(m_graphicPLCList.size())) {
908 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::getGraphicStyleId: unknown id=%d\n", id));
909 return -1;
910 }
911 return m_graphicPLCList[size_t(id)].getGraphicStyleId();
912 }
913 //! retrieves the text plc
914 bool getTextPLC(int id, TextPLC &plc) const
915 {
916 if (id<0 || id>=static_cast<int>(m_textPLCList.size())) {
917 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::Sheet::getTextPLC: unknown id=%d\n", id));
918 return false;
919 }
920 plc=m_textPLCList[size_t(id)];
921 return true;
922 }
923 //! the sheet name
924 librevenge::RVNGString m_name;
925 //! the text zone id
926 int m_textboxZoneId;
927 //! the default col width in point
928 float m_colWidthDef;
929 //! the col widths: map for cols positions to width in points
930 std::map<MWAWVec2i, float> m_colWidthsMap;
931 //! the default row height in point
932 float m_rowHeightDef;
933 //! the row heights: map from rows positions to height in points
934 std::map<MWAWVec2i, float> m_rowHeightsMap;
935 //! the list of block id to ref position
936 std::map<int, MWAWCellContent::FormulaInstruction> m_blockToCellRefMap;
937 //! the list of value id to ref position
938 std::map<int, MWAWCellContent::FormulaInstruction> m_valueToCellRefMap;
939 //! the list of ref id to ref position
940 std::map<int, MWAWCellContent::FormulaInstruction> m_refToCellRefMap;
941 //! the formula link
942 RagTime5ClusterManager::Link m_formulaLink;
943 //! all the formula
944 std::map<int, std::vector<MWAWCellContent::FormulaInstruction> > m_idToFormula;
945 //! the list of values
946 std::vector<CellValue> m_valuesList;
947 //! the list of planes
948 std::vector<Plane> m_planesList;
949 //! the graph plc
950 std::vector<GraphicPLC> m_graphicPLCList;
951 //! the default graphic plc
952 GraphicPLC m_defGraphicPLC;
953 //! the text plc
954 std::vector<TextPLC> m_textPLCList;
955 //! the default text plc
956 TextPLC m_defTextPLC;
957 //! the default border plc (vertical and horizontal)
958 BorderPLC m_defBordersPLC[2];
959 //! the default font (Palatino, 12)
960 MWAWFont m_defaultFont;
961 //! the default paragraph
962 MWAWParagraph m_defaultParagraph;
963 //! the list of child zone: picture, button, ...
964 std::vector<RagTime5StructManager::ZoneLink> m_childList;
965 //! a flag to know if the sheet has been sent
966 bool m_isSent;
967 };
968
969 //
970 // parser
971 //
972
973 //! Internal: the helper to read a clustList
974 struct ClustListParser final : public RagTime5StructManager::DataParser {
975 //! constructor
976 ClustListParser(RagTime5ClusterManager &clusterManager, int fieldSize, std::string const &zoneName)
977 : RagTime5StructManager::DataParser(zoneName)
978 , m_clusterList()
979 , m_fieldSize(fieldSize)
980 , m_clusterManager(clusterManager)
981 {
982 if (fieldSize!=24 && fieldSize!=60) {
983 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ClustListParser::ClustListParser: bad data size\n"));
984 m_fieldSize=0;
985 }
986 }
987 //! destructor
988 ~ClustListParser() final;
989 //! return a name which can be used for debugging
990 std::string getClusterDebugName(int id) const
991 {
992 return m_clusterManager.getClusterDebugName(id);
993 }
994
995 //! try to parse a data
996 bool parseData(MWAWInputStreamPtr &input, long endPos, RagTime5Zone &/*zone*/, int /*n*/, libmwaw::DebugStream &f) final
997 {
998 long pos=input->tell();
999 if (!m_fieldSize || endPos-pos!=m_fieldSize) {
1000 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ClustListParser::parse: bad data size\n"));
1001 return false;
1002 }
1003
1004 std::vector<int> listIds;
1005 if (!RagTime5StructManager::readDataIdList(input, 1, listIds)) {
1006 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ClustListParser::parse: can not read an cluster id\n"));
1007 f << "##clusterIds,";
1008 return false;
1009 }
1010 if (listIds[0]) {
1011 m_clusterList.push_back(listIds[0]);
1012 // a e,2003,200b, ... cluster
1013 f << getClusterDebugName(listIds[0]) << ",";
1014 }
1015 unsigned long lVal=input->readULong(4); // c00..small number
1016 if ((lVal&0xc0000000)==0xc0000000)
1017 f << "f0=" << (lVal&0x3fffffff) << ",";
1018 else
1019 f << "f0*" << lVal << ",";
1020 if (m_fieldSize==24) {
1021 for (int i=0; i<8; ++i) { // f1=0|1, f2=f3=f4=0, f5=0|c, f6=0|d|e
1022 auto val=static_cast<int>(input->readLong(2));
1023 if (val) f << "f" << i << "=" << val << ",";
1024 }
1025 return true;
1026 }
1027 auto val=static_cast<int>(input->readLong(4)); // small int
1028 if (val) f << "f0=" << val << ",";
1029 for (int i=0; i<3; ++i) {
1030 float dim[4];
1031 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
1032 MWAWBox2f box(MWAWVec2f(dim[0],dim[1]), MWAWVec2f(dim[2],dim[3]));
1033 if (box!=MWAWBox2f(MWAWVec2f(0,0),MWAWVec2f(0,0)))
1034 f << "dim" << i << "=" << box << ",";
1035 }
1036 return true;
1037 }
1038
1039 //! the list of read cluster
1040 std::vector<int> m_clusterList;
1041 private:
1042 //! the field size
1043 int m_fieldSize;
1044 //! the main zone manager
1045 RagTime5ClusterManager &m_clusterManager;
1046 //! copy constructor, not implemented
1047 ClustListParser(ClustListParser &orig) = delete;
1048 //! copy operator, not implemented
1049 ClustListParser &operator=(ClustListParser &orig) = delete;
1050 };
1051
1052 ClustListParser::~ClustListParser()
1053 {
1054 }
1055
1056 //! Internal: the helper to read a cell values
1057 struct ValuesParser final : public RagTime5StructManager::DataParser {
1058 //! constructor
1059 explicit ValuesParser(Sheet &sheet)
1060 : RagTime5StructManager::DataParser("SheetValue")
1061 , m_sheet(sheet)
1062 {
1063 }
1064 //! destructor
1065 ~ValuesParser() final;
1066 //! try to parse a data
1067 bool parseData(MWAWInputStreamPtr &input, long endPos, RagTime5Zone &/*zone*/, int n, libmwaw::DebugStream &debStream) final
1068 {
1069 long pos=input->tell();
1070 long fSz=endPos-pos;
1071 if (fSz<2) {
1072 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ValuesParser::parse: bad data size\n"));
1073 return false;
1074 }
1075 libmwaw::DebugStream f;
1076 RagTime5SpreadsheetInternal::CellValue cell;
1077 auto type=static_cast<int>(input->readULong(2));
1078 bool hasIndex[3]= {(type&0x40)!=0, (type&0x80)!=0, (type&0x2000)!=0};
1079 if (type&0x4E30)
1080 f << "fl" << std::hex << (type&0x4E30) << std::dec << ",";
1081 cell.m_type=(type&0x910F);
1082 bool ok=true;
1083 switch (cell.m_type) {
1084 case 0: // empty
1085 break;
1086 case 1:
1087 case 0xa: // componentId(pict)
1088 if (fSz<4) {
1089 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ValuesParser::parse: find bad size for long\n"));
1090 f << "###fSz[long],";
1091 ok=false;
1092 break;
1093 }
1094 cell.m_id=input->readULong(4);
1095 break;
1096 case 2: // find 800b88[47]8, so ?
1097 if (fSz<4) {
1098 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ValuesParser::parse: find bad size for long\n"));
1099 f << "###fSz[long],";
1100 ok=false;
1101 break;
1102 }
1103 cell.m_long=long(input->readULong(4));
1104 break;
1105 case 4: // number
1106 case 5: // date
1107 case 6: { // time
1108 if (fSz<10) {
1109 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ValuesParser::parse: find bad size for double\n"));
1110 f << cell << "###fSz[double],";
1111 ok=false;
1112 break;
1113 }
1114 bool isNan;
1115 if (!input->readDouble8(cell.m_double, isNan)) {
1116 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ValuesParser::parse: can not read a double\n"));
1117 f << "###double,";
1118 ok=false;
1119 }
1120 break;
1121 }
1122 case 7: {
1123 for (int i=0; i<3; ++i) {
1124 if (!hasIndex[i]) continue;
1125 if (input->tell()+4>endPos) {
1126 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ValuesParser::parse: can not read index\n"));
1127 f << "###index[string],";
1128 ok=false;
1129 break;
1130 }
1131 hasIndex[i]=false;
1132 auto val=static_cast<int>(input->readLong(4));
1133 if (!val) continue;
1134 if (i==0) // checkme
1135 cell.m_formulaId=val;
1136 else
1137 f << "f" << i << "=" << val << ",";
1138 }
1139 if (!ok)
1140 break;
1141 if (!RagTime5StructManager::readUnicodeString(input, endPos, cell.m_text)) {
1142 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ValuesParser::parse: can not read a string\n"));
1143 f << "###string,";
1144 ok=false;
1145 }
1146 break;
1147 }
1148 case 8:
1149 case 9: { // multiligne text zone
1150 if (fSz<4) {
1151 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ValuesParser::parse: find bad size for long2\n"));
1152 f << "###fSz[long],";
1153 ok=false;
1154 break;
1155 }
1156 cell.m_id=input->readULong(4);
1157 break;
1158 }
1159 default:
1160 break;
1161 }
1162 for (int i=0; ok && i<3; ++i) {
1163 if (!hasIndex[i]) continue;
1164 if (input->tell()+4>endPos) {
1165 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ValuesParser::parse: can not read an index\n"));
1166 f << "###index,";
1167 break;
1168 }
1169 auto val=static_cast<int>(input->readLong(4));
1170 if (!val) continue;
1171 if (i==0)
1172 cell.m_formulaId=val;
1173 else
1174 f << "f" << i << "=" << val << ",";
1175 }
1176 if (ok && input->tell()!=endPos) {
1177 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ValuesParser::parse: find extra data\n"));
1178 f << "###extra,";
1179 }
1180 cell.m_extra=f.str();
1181 if (n<=0) {
1182 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::ValuesParser::parse: n value:%d seems bad\n", n));
1183 }
1184 else {
1185 if (n>static_cast<int>(m_sheet.m_valuesList.size()))
1186 m_sheet.m_valuesList.resize(size_t(n));
1187 m_sheet.m_valuesList[size_t(n-1)]=cell;
1188 }
1189 debStream << "V" << n << "," << cell;
1190 return true;
1191 }
1192 //! the actual sheet
1193 Sheet &m_sheet;
1194 };
1195
1196 ValuesParser::~ValuesParser()
1197 {
1198 }
1199
1200 //! Internal: the helper to read a list of cell to paragraph/char/... data
1201 struct CellPLCParser final : public RagTime5StructManager::DataParser {
1202 //! constructor
1203 CellPLCParser(Sheet &sheet, int which, int fieldSize, std::map<MWAWVec2i,int> const &numRowByPlanes)
1204 : RagTime5StructManager::DataParser(fieldSize==6 ? "SheetGrphPLC" : fieldSize==10 ? "SheetTxtPLC" : "SheetBordPLC")
1205 , m_which(which)
1206 , m_fieldSize(fieldSize)
1207 , m_row(0)
1208 , m_planes(1,1)
1209 , m_sheet(sheet)
1210 , m_numRowByPlanes(numRowByPlanes)
1211 , m_numRemainingRows(-1)
1212 {
1213 static int const expectedSize[]= {6,10,14,14};
1214 if (m_which<0 || m_which>=4 || m_fieldSize!=expectedSize[m_which]) {
1215 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::CellPLCParser::CellPLCParser: bad field size\n"));
1216 m_which=-1;
1217 }
1218 if (!m_numRowByPlanes.empty()) {
1219 m_planes=m_numRowByPlanes.begin()->first;
1220 m_numRemainingRows=m_numRowByPlanes.begin()->second;
1221 }
1222 }
1223 //! destructor
1224 ~CellPLCParser() final;
1225 //! try to parse a data
1226 bool parseData(MWAWInputStreamPtr &input, long endPos, RagTime5Zone &zone, int n, libmwaw::DebugStream &f) final
1227 {
1228 long pos=input->tell();
1229 long fSz=endPos-pos;
1230 if (fSz<2 || (fSz%m_fieldSize)!=4) {
1231 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::CellPLCParser::parse: bad data size\n"));
1232 return false;
1233 }
1234 int const maxRow = m_fieldSize==14 ? 16001 : 16000;
1235 auto numRow=static_cast<int>(input->readULong(2));
1236 MWAWVec2i planes=m_planes;
1237 MWAWVec2i rows(m_row, m_row+numRow-1);
1238
1239 f << "R" << m_row+1;
1240 if (numRow!=1)
1241 f << "-" << m_row+numRow;
1242 f << ",";
1243 f << "planes=" << planes << ",";
1244 m_row += numRow;
1245 if (--m_numRemainingRows==0) {
1246 auto it=m_numRowByPlanes.find(m_planes);
1247 if (it!=m_numRowByPlanes.end() && (++it)!=m_numRowByPlanes.end()) {
1248 m_planes=it->first;
1249 m_numRemainingRows=it->second;
1250 m_row=0;
1251 }
1252 }
1253 if (m_numRemainingRows<0 && m_row>=maxRow) {
1254 // by default, suppose that we add one planes
1255 m_planes=MWAWVec2i(m_planes[1]+1,m_planes[1]+1);
1256 m_row=0;
1257 }
1258 auto N=static_cast<int>(input->readLong(2));
1259 f << "N=" << N << ",";
1260 if (fSz!=4+m_fieldSize*N || (m_fieldSize && (fSz-4)/m_fieldSize<N)) {
1261 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::CellPLCParser::parse: N seems bad\n"));
1262 f << "###";
1263 return true;
1264 }
1265 libmwaw::DebugStream f1;
1266 libmwaw::DebugFile &ascii=zone.ascii();
1267 int col=0;
1268 std::vector<int> values;
1269 size_t const numValues=m_which==1 ? 3 : size_t(m_fieldSize-2)/2;
1270 values.resize(numValues);
1271
1272 /* we limit the number of planes to the current number of planes
1273 or 20 to avoid creating 16000 sheets, just because the user has
1274 reset some style in all planes */
1275 int const maxPlanes=m_sheet.m_planesList.size()>20 ? int(m_sheet.m_planesList.size()+1) : 20;
1276 for (int i=0; i<N; ++i) {
1277 pos=input->tell();
1278 f1.str("");
1279 f1 << m_name << "-" << n << "-A" << i << ":";
1280 auto numCol=static_cast<int>(input->readLong(2));
1281 f1 << "C" << col+1;
1282 if (numCol!=1)
1283 f1 << "-" << col+numCol;
1284 f1 << ",";
1285 MWAWVec2i cols(col, col+numCol-1);
1286 col+=numCol;
1287 if (m_which==1) {
1288 for (size_t j=0; j<3; ++j) values[j]=int(input->readULong(j==2 ? 4 : 2));
1289 }
1290 else {
1291 for (auto &val : values) val=static_cast<int>(input->readLong(2));
1292 }
1293 switch (m_which) {
1294 case 0: {
1295 GraphicPLC plc(values);
1296 if (plc!=m_sheet.m_defGraphicPLC) {
1297 f1 << plc;
1298 for (int plane=planes[0]; plane<=planes[1] && plane<=maxPlanes; ++plane)
1299 m_sheet.setPLCValues(MWAWVec3i(cols[0],rows[0],plane), MWAWVec3i(cols[1],rows[1],plane),
1300 RagTime5SpreadsheetInternal::CellContent::GraphicStyle, static_cast<int>(m_sheet.m_graphicPLCList.size()));
1301 m_sheet.m_graphicPLCList.push_back(plc);
1302 }
1303 else
1304 f1 << "def,";
1305 break;
1306 }
1307 case 1: {
1308 TextPLC plc(values);
1309 if (plc!=m_sheet.m_defTextPLC) {
1310 f1 << plc;
1311 for (int plane=planes[0]; plane<=planes[1] && plane<=maxPlanes; ++plane)
1312 m_sheet.setPLCValues(MWAWVec3i(cols[0],rows[0],plane), MWAWVec3i(cols[1],rows[1],plane),
1313 RagTime5SpreadsheetInternal::CellContent::TextStyle, static_cast<int>(m_sheet.m_textPLCList.size()));
1314 m_sheet.m_textPLCList.push_back(plc);
1315 }
1316 else
1317 f1 << "def,";
1318 break;
1319 }
1320 case 2: // vertical border
1321 case 3: { // horizontal border
1322 BorderPLC plc(values);
1323 if (plc!=m_sheet.m_defBordersPLC[m_which-2]) {
1324 f1 << plc;
1325 if (plc.isMergedBorder())
1326 break;
1327 int bordersId[2]= {plc.getBorderGraphicStyleId(true), plc.getBorderGraphicStyleId(false) };
1328 for (int wh=0; wh<2; ++wh) {
1329 if (bordersId[wh]<=0) continue;
1330 MWAWVec2i finalRows=rows;
1331 if (wh==0) {
1332 if (finalRows[0]>0)
1333 --finalRows[0];
1334 --finalRows[1];
1335 }
1336 else {
1337 if (finalRows[1]==160000)
1338 --finalRows[1];
1339 }
1340 if (finalRows[0]>finalRows[1])
1341 continue;
1342 for (int plane=planes[0]; plane<=planes[1] && plane<=maxPlanes; ++plane) {
1343 if (m_which==2) // time to invert rows and columns
1344 m_sheet.setPLCValues(MWAWVec3i(finalRows[0],cols[0],plane), MWAWVec3i(finalRows[1],cols[1],plane),
1345 RagTime5SpreadsheetInternal::CellContent::BorderPrevVStyle+(1-wh), bordersId[wh]);
1346 else
1347 m_sheet.setPLCValues(MWAWVec3i(cols[0],finalRows[0],plane), MWAWVec3i(cols[1],finalRows[1],plane),
1348 RagTime5SpreadsheetInternal::CellContent::BorderPrevHStyle+(1-wh), bordersId[wh]);
1349 }
1350 }
1351 }
1352 else
1353 f1 << "def,";
1354 break;
1355 }
1356 default:
1357 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::CellPLCParser::parse: find unknown PLC\n"));
1358 for (size_t j=0; j<values.size(); ++j) {
1359 if (values[j])
1360 f1 << "f" << j << "=" << values[j] << ",";
1361 }
1362 break;
1363 }
1364 input->seek(pos+m_fieldSize, librevenge::RVNG_SEEK_SET);
1365 ascii.addPos(pos);
1366 ascii.addNote(f1.str().c_str());
1367 }
1368 return true;
1369 }
1370 //! the type: 0=graphicStyles, 1=textStyles, 2=vertical border, 3=horizontal border
1371 int m_which;
1372 //! the field size
1373 int m_fieldSize;
1374 //! the actual row
1375 int m_row;
1376 //! the actual plane set
1377 MWAWVec2i m_planes;
1378 //! the actual sheet
1379 Sheet &m_sheet;
1380 //! the number of row by planes
1381 std::map<MWAWVec2i,int> const m_numRowByPlanes;
1382 //! the remaining row in the planes
1383 int m_numRemainingRows;
1384 private:
1385 //! copy constructor, not implemented
1386 CellPLCParser(CellPLCParser &orig) = delete;
1387 //! copy operator, not implemented
1388 CellPLCParser &operator=(CellPLCParser &orig) = delete;
1389 };
1390
1391 CellPLCParser::~CellPLCParser()
1392 {
1393 }
1394
1395 ////////////////////////////////////////
1396 //! Internal: the state of a RagTime5Spreadsheet
1397 struct State {
1398 //! constructor
1399 State()
1400 : m_idSheetMap()
1401 , m_namesSet()
1402 , m_newSheetId(0)
1403 {
1404 }
1405 //! returns a new sheet name
1406 librevenge::RVNGString getNewSheetName(librevenge::RVNGString const &fileName) const
1407 {
1408 auto it=m_namesSet.find(fileName);
1409 if (!fileName.empty() && it==m_namesSet.end()) {
1410 m_namesSet.insert(fileName);
1411 return fileName;
1412 }
1413 auto name=fileName;
1414 if (name.empty())
1415 name="Sheet";
1416 while (true) {
1417 librevenge::RVNGString suffix, finalName(name);
1418 suffix.sprintf(" %d", ++m_newSheetId);
1419 finalName.append(suffix);
1420 it=m_namesSet.find(finalName);
1421 if (it==m_namesSet.end()) {
1422 m_namesSet.insert(finalName);
1423 return finalName;
1424 }
1425 }
1426 }
1427 //! map data id to sheet zone
1428 std::map<int, std::shared_ptr<Sheet> > m_idSheetMap;
1429 protected:
1430 //! the set of name already used
1431 mutable std::set<librevenge::RVNGString> m_namesSet;
1432 //! a int uses to generate unique sheet id
1433 mutable int m_newSheetId;
1434 };
1435
1436 }
1437
1438 ////////////////////////////////////////////////////////////
1439 // constructor/destructor, ...
1440 ////////////////////////////////////////////////////////////
1441 RagTime5Spreadsheet::RagTime5Spreadsheet(RagTime5Document &doc)
1442 : m_document(doc)
1443 , m_structManager(m_document.getStructManager())
1444 , m_styleManager(m_document.getStyleManager())
1445 , m_parserState(doc.getParserState())
1446 , m_state(new RagTime5SpreadsheetInternal::State)
1447 {
1448 }
1449
1450 RagTime5Spreadsheet::~RagTime5Spreadsheet()
1451 {
1452 }
1453
1454 int RagTime5Spreadsheet::version() const
1455 {
1456 return m_parserState->m_version;
1457 }
1458
1459 int RagTime5Spreadsheet::numPages() const
1460 {
1461 return m_state->m_idSheetMap.empty() ? 0 : 1;
1462 }
1463
1464 std::vector<int> RagTime5Spreadsheet::getSheetIdList() const
1465 {
1466 std::vector<int> res;
1467 for (auto const &it : m_state->m_idSheetMap)
1468 res.push_back(it.first);
1469 return res;
1470 }
1471
1472 bool RagTime5Spreadsheet::getFormulaRef(int sheetId, int refId, MWAWCellContent::FormulaInstruction &instruction) const
1473 {
1474 auto it=m_state->m_idSheetMap.find(sheetId);
1475 if (it==m_state->m_idSheetMap.end() || !it->second) {
1476 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::getFormulaRef: can not find sheet=%d\n", sheetId));
1477 return false;
1478 }
1479 auto const &sheet=*it->second;
1480 auto const &rIt=sheet.m_refToCellRefMap.find(refId);
1481 if (rIt==sheet.m_refToCellRefMap.end()) {
1482 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::getFormulaRef: can not find ref %d in sheet=%d\n", refId, sheetId));
1483 return false;
1484 }
1485 instruction=rIt->second;
1486 return true;
1487 }
1488
1489 void RagTime5Spreadsheet::parseSpreadsheetFormulas()
1490 {
1491 for (auto const &it : m_state->m_idSheetMap) {
1492 if (!it.second) continue;
1493 auto const &sheet=*it.second;
1494 if (!sheet.m_formulaLink.empty())
1495 m_document.getFormulaParser()->readFormulaClusters(sheet.m_formulaLink, it.first);
1496 }
1497 }
1498
1499 void RagTime5Spreadsheet::storeFormula(int sheetId, std::map<int, std::vector<MWAWCellContent::FormulaInstruction> > const &idToFormula)
1500 {
1501 auto it=m_state->m_idSheetMap.find(sheetId);
1502 if (it==m_state->m_idSheetMap.end() || !it->second) {
1503 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::storeFormula: can not find sheet=%d\n", sheetId));
1504 return;
1505 }
1506 it->second->m_idToFormula=idToFormula;
1507 }
1508
1509 ////////////////////////////////////////////////////////////
1510 //
1511 // Intermediate level
1512 //
1513 ////////////////////////////////////////////////////////////
1514 bool RagTime5Spreadsheet::readSheetDimensions(RagTime5SpreadsheetInternal::Sheet &sheet, RagTime5Zone &zone, RagTime5ClusterManager::Link const &link)
1515 {
1516 MWAWEntry const &entry=zone.m_entry;
1517 if (!entry.valid() || link.m_fieldSize!=24 || link.m_fieldSize*link.m_N>entry.length()) {
1518 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readSheetDimensions: the zone seems bad\n"));
1519 if (entry.valid()) {
1520 libmwaw::DebugFile &ascFile=zone.ascii();
1521 libmwaw::DebugStream f;
1522 f << "Entries(SheetDims)[" << zone << "]:###";
1523 zone.m_isParsed=true;
1524 ascFile.addPos(entry.begin());
1525 ascFile.addNote(f.str().c_str());
1526 }
1527 return false;
1528 }
1529 libmwaw::DebugFile &ascFile=zone.ascii();
1530 libmwaw::DebugStream f;
1531 f << "Entries(SheetDims)[" << zone << "]:";
1532 zone.m_isParsed=true;
1533 ascFile.addPos(entry.begin());
1534 ascFile.addNote(f.str().c_str());
1535 MWAWInputStreamPtr input=zone.getInput();
1536 input->setReadInverted(!zone.m_hiLoEndian);
1537 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1538 long endPos=entry.end();
1539
1540 int actCPos=0;
1541 for (int i=0; i<link.m_N; ++i) {
1542 long pos=input->tell();
1543 f.str("");
1544 f << "SheetDims-" << i+1 << ":";
1545 long newCPos=input->readLong(4);
1546 float value=float(input->readLong(4))/65536.f;
1547 if (newCPos<0 || newCPos>32000) {
1548 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readSheetDimensions: find unexpected newCPos\n"));
1549 f << "###newCPos=" << newCPos << ",";
1550 }
1551 else if (newCPos==0)
1552 f << "empty,";
1553 else if (newCPos<=16000) { // between 1 and 16000
1554 MWAWVec2i cols(actCPos, int(newCPos-1));
1555 sheet.setColsWidth(cols, value);
1556 f << "C" << cols << ",";
1557 }
1558 else { // between 16001 and 32000
1559 MWAWVec2i rows(actCPos-16000, int(newCPos-16000-1));
1560 sheet.setRowsHeight(rows, value);
1561 f << "R" << rows << ",";
1562 }
1563 f << "dim=" << value << ",";
1564 float dim[2]; // padding, useme
1565 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
1566 if (dim[0]>0||dim[1]>0) f << "padding[beg,end]=" << dim[0] << "x" << dim[1] << ",";
1567 auto val=long(input->readULong(4));
1568 if (val) f << "content[width]=" << double(val)/double(0x10000) << ",";
1569 auto fl=input->readULong(2); // 0|1
1570 if (fl&1)
1571 f << "automatic,";
1572 if (fl&2)
1573 f << "ignore[orientation],";
1574 if (fl&0x20)
1575 f << "hidden,";
1576 fl&=0xffdc;
1577 if (fl)
1578 f << "fl=" << std::hex << fl << std::dec << ",";
1579 val=input->readLong(2);
1580 if (val)
1581 f << "f1=" << val << ",";
1582 if (newCPos>0 && newCPos<=32000)
1583 actCPos=int(newCPos);
1584 ascFile.addPos(pos);
1585 ascFile.addNote(f.str().c_str());
1586 }
1587 long pos=input->tell();
1588 if (pos!=endPos) {
1589 // no rare
1590 ascFile.addPos(pos);
1591 ascFile.addNote("SheetDims:end");
1592 }
1593 input->setReadInverted(false);
1594 return true;
1595 }
1596
1597 bool RagTime5Spreadsheet::readCellBlocks(RagTime5SpreadsheetInternal::Sheet &sheet, RagTime5Zone &zone, RagTime5ClusterManager::Link const &link, bool isUnion)
1598 {
1599 MWAWEntry const &entry=zone.m_entry;
1600 std::string const wh(isUnion ? "SheetUnion" : "SheetRefBlock");
1601 if (!entry.valid() || (link.m_fieldSize!=22&&link.m_fieldSize!=24) || link.m_fieldSize*link.m_N>entry.length()) {
1602 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readCellBlocks: the zone seems bad\n"));
1603 if (entry.valid()) {
1604 libmwaw::DebugFile &ascFile=zone.ascii();
1605 libmwaw::DebugStream f;
1606 f << "Entries(" << wh << ")[" << zone << "]:###";
1607 zone.m_isParsed=true;
1608 ascFile.addPos(entry.begin());
1609 ascFile.addNote(f.str().c_str());
1610 }
1611 return false;
1612 }
1613 libmwaw::DebugFile &ascFile=zone.ascii();
1614 libmwaw::DebugStream f;
1615 f << "Entries(" << wh << ")[" << zone << "]:";
1616 zone.m_isParsed=true;
1617 ascFile.addPos(entry.begin());
1618 ascFile.addNote(f.str().c_str());
1619 MWAWInputStreamPtr input=zone.getInput();
1620 input->setReadInverted(!zone.m_hiLoEndian);
1621 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1622 long endPos=entry.end();
1623 int const numEnd=link.m_fieldSize==22 ? 1 : 2;
1624
1625 for (int i=0; i<link.m_N; ++i) {
1626 // checkme: it is possible that the list of valid block is maintained in the SheetRefPos
1627 long pos=input->tell();
1628 f.str("");
1629 if (isUnion)
1630 f << wh << "-" << i+1 << ":";
1631 else
1632 f << wh << "-RB" << i+1 << ":";
1633 auto val=static_cast<int>(input->readLong(2)); // always 0?
1634 auto type=static_cast<int>(input->readLong(2));
1635 if (val==0 && type==0) { // no data,
1636 f << "_";
1637 input->seek(pos+link.m_fieldSize, librevenge::RVNG_SEEK_SET);
1638 ascFile.addPos(pos);
1639 ascFile.addNote(f.str().c_str());
1640 continue;
1641 }
1642 if (val) f << "f0=" << val << ",";
1643 if (type!=1) f << "type=" << type << ",";
1644 int dim[4], plane[2];
1645 for (auto &d : dim) d=int(input->readULong(2));
1646 for (auto &p : plane) p=int(input->readULong(2));
1647 if (dim[0]==0 && dim[1]==0 && dim[2]==0 && dim[3]==0) {
1648 f << "_";
1649 input->seek(pos+link.m_fieldSize, librevenge::RVNG_SEEK_SET);
1650 ascFile.addPos(pos);
1651 ascFile.addNote(f.str().c_str());
1652 continue;
1653 }
1654 if (plane[1]<=plane[0]) {
1655 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readCellBlocks: the block planes seem bad\n"));
1656 f << "###plane,";
1657 }
1658 else if (isUnion && (dim[2]>=dim[0] || dim[3]>=dim[1])) {
1659 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readCellBlocks: the block seems bad\n"));
1660 f << "###";
1661 }
1662 else if (isUnion && (dim[0]!=dim[2]+1 || dim[1]!=dim[3]+1))
1663 sheet.setMergedCells(MWAWVec3i(dim[2]-1,dim[3]-1,plane[0]),MWAWVec3i(dim[0]-2,dim[1]-2,plane[1]-1));
1664 else if (!isUnion) {
1665 MWAWCellContent::FormulaInstruction cells;
1666 cells.m_position[0]=MWAWVec2i(dim[2]-1,dim[3]-1);
1667 cells.m_position[1]=MWAWVec2i(dim[0]-2,dim[1]-2);
1668 if (dim[2]==32700 && dim[0]==0) { // complete column
1669 cells.m_position[0][0]=cells.m_position[1][0]=-1;
1670 cells.m_type=MWAWCellContent::FormulaInstruction::F_CellList;
1671 }
1672 else if (dim[3]==32700 && dim[1]==0) { // complete row
1673 cells.m_position[0][1]=cells.m_position[1][1]=-1;
1674 cells.m_type=MWAWCellContent::FormulaInstruction::F_CellList;
1675 }
1676 else
1677 cells.m_type=cells.m_position[0]==cells.m_position[1] ? MWAWCellContent::FormulaInstruction::F_Cell :
1678 MWAWCellContent::FormulaInstruction::F_CellList;
1679 cells.m_sheet[0]=sheet.getName(plane[0]);
1680 cells.m_sheet[1]=sheet.getName(plane[1]-1);
1681 sheet.m_blockToCellRefMap[i+1]=cells;
1682 }
1683
1684 // 1 means first line, 16001 means last line
1685 f << "cells?=" << MWAWBox2i(MWAWVec2i(dim[2],dim[3]),MWAWVec2i(dim[0],dim[1])) << ",";
1686 if (plane[1]!=plane[0]+1)
1687 f << "planes=" << plane[0] << "<->" << plane[1]-1 << ",";
1688 else if (plane[0]!=1)
1689 f << "plane=" << plane[0] << ",";
1690 unsigned long zoneId=input->readULong(4);
1691 if (zoneId==0x2000000) // normal no zone
1692 ;
1693 else if ((zoneId>>24)==2)
1694 f << "zone[id]=" << (zoneId&0xFFFFFF) << ",";
1695 else { // find also (zoneId>>24)==42
1696 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readCellBlocks: the zone id seems bad\n"));
1697 f << "###zoneId=" << std::hex << zoneId << std::dec << ",";
1698 }
1699 for (int j=0; j<numEnd; ++j) { // always 0
1700 val=static_cast<int>(input->readLong(2));
1701 if (val)
1702 f << "f" << j+3 << "=" << val << ",";
1703 }
1704 ascFile.addPos(pos);
1705 ascFile.addNote(f.str().c_str());
1706 }
1707 long pos=input->tell();
1708 if (pos!=endPos) {
1709 // extra data seems rare, but possible...
1710 f.str("");
1711 f << wh << ":end";
1712 ascFile.addPos(pos);
1713 ascFile.addNote(f.str().c_str());
1714 }
1715 input->setReadInverted(false);
1716 return true;
1717 }
1718
1719 bool RagTime5Spreadsheet::readValuesTree(RagTime5SpreadsheetInternal::Sheet &sheet, RagTime5Zone &zone,
1720 RagTime5ClusterManager::Link const &link, int rootId, MWAWVec3i const &maxPos)
1721 {
1722 MWAWEntry const &entry=zone.m_entry;
1723 if (!entry.valid() || link.m_fieldSize!=8 || link.m_fieldSize*link.m_N>entry.length()) {
1724 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readValuesTree: the zone seems bad\n"));
1725 if (entry.valid()) {
1726 libmwaw::DebugFile &ascFile=zone.ascii();
1727 libmwaw::DebugStream f;
1728 f << "Entries(SheetVTree)[" << zone << "]:###";
1729 zone.m_isParsed=true;
1730 ascFile.addPos(entry.begin());
1731 ascFile.addNote(f.str().c_str());
1732 }
1733 return false;
1734 }
1735 libmwaw::DebugFile &ascFile=zone.ascii();
1736 libmwaw::DebugStream f;
1737 f << "Entries(SheetVTree)[" << zone << "]:";
1738 zone.m_isParsed=true;
1739 ascFile.addPos(entry.begin());
1740 ascFile.addNote(f.str().c_str());
1741 MWAWInputStreamPtr input=zone.getInput();
1742 input->setReadInverted(!zone.m_hiLoEndian);
1743 /* a binary tree, all leaf have the same level. Each node shares
1744 format coord-leftChild-type-rightChild where:
1745 - coord=0..2,
1746 - type=0(internal node) or type=11(leaf),
1747 - left(resp. right) child corresponds to 0(resp. 1) value,
1748 - bits defining each coord are stored in reversed order(to make
1749 the tree grows easily if needed)
1750 */
1751 std::stack<int> idStack;
1752 std::stack<MWAWVec3i> coordStack;
1753 std::set<int> idSeenSet;
1754 idStack.push(rootId);
1755 coordStack.push(MWAWVec3i(0,0,0));
1756 while (!idStack.empty()) {
1757 int id=idStack.top();
1758 MWAWVec3i coord=coordStack.top();
1759 idStack.pop();
1760 coordStack.pop();
1761 if (idSeenSet.find(id)!=idSeenSet.end() || id<=0 || id>link.m_N) {
1762 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readValuesTree: id %d is bad\n", id));
1763 continue;
1764 }
1765 idSeenSet.insert(id);
1766 long pos=entry.begin()+8*(id-1);
1767 input->seek(pos, librevenge::RVNG_SEEK_SET);
1768
1769 f.str("");
1770 f << "SheetVTree-VT" << id << ":";
1771 if (id==rootId) f << "root,";
1772
1773 int child[2];
1774 unsigned long value=input->readULong(4);
1775 auto actCoord=int(value>>24);
1776 child[0]=int(value&0xFFFFFF);
1777 value=input->readULong(4);
1778 auto type=int(value>>24);
1779 child[1]=int(value&0xFFFFFF);
1780
1781 if (actCoord<0 || actCoord>2) {
1782 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readValuesTree: coord %d seems bad\n", actCoord));
1783 f << "###coord=" << actCoord << ",";
1784 }
1785 else if (type!=0 && type!=0x11) {
1786 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readValuesTree: unknown type %d\n", type));
1787 f << "###type=" << type << ",";
1788 }
1789 else {
1790 coord[actCoord]*=2;
1791 for (int i=0; i<2; ++i) {
1792 int cId=child[i];
1793 if (!cId) {
1794 f << "_,";
1795 continue;
1796 }
1797 if (i==1) ++coord[actCoord];
1798 if (type==0x11) {
1799 f << "V" << cId << "[C" << coord << "],";
1800 if (coord[0]<=0 || coord[0]>maxPos[0] || coord[1]<=0 || coord[1]>maxPos[1] || coord[2]<=0 || coord[2]>maxPos[2]) {
1801 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readValuesTree: the final pos seems bad\n"));
1802 f << "###pos,";
1803 }
1804 else
1805 sheet.setPLCValues(coord-MWAWVec3i(1,1,0),coord-MWAWVec3i(1,1,0), RagTime5SpreadsheetInternal::CellContent::Value, cId);
1806 continue;
1807 }
1808 if (cId>=1 && cId<=link.m_N && idSeenSet.find(cId)==idSeenSet.end()) {
1809 idStack.push(cId);
1810 coordStack.push(coord);
1811 }
1812 else {
1813 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readValuesTree: find bad child\n"));
1814 f << "###";
1815 }
1816 f << "VT" << cId << ",";
1817 }
1818 }
1819 ascFile.addPos(pos);
1820 ascFile.addNote(f.str().c_str());
1821 }
1822
1823 #ifdef DEBUG_WITH_FILES
1824 auto it=idSeenSet.begin();
1825 for (int i=1; i<=link.m_N; ++i) {
1826 if (it!=idSeenSet.end() && *it==i) {
1827 ++it;
1828 continue;
1829 }
1830 ascFile.addPos(entry.begin()+(i-1)*8);
1831 ascFile.addNote("SheetVTree:_");
1832 }
1833 #endif
1834
1835 if (link.m_N*8!=entry.length()) { // no frequent, but can happens
1836 ascFile.addPos(entry.begin()+link.m_N*8);
1837 ascFile.addNote("SheetVTree:end");
1838 }
1839 input->setReadInverted(false);
1840 return true;
1841 }
1842
1843 ////////////////////////////////////////////////////////////
1844 //
1845 // Low level
1846 //
1847 ////////////////////////////////////////////////////////////
1848 bool RagTime5Spreadsheet::readCellRefPos(RagTime5SpreadsheetInternal::Sheet &sheet, RagTime5Zone &zone, RagTime5ClusterManager::Link const &link)
1849 {
1850 MWAWEntry const &entry=zone.m_entry;
1851 if (!entry.valid() || link.m_fieldSize!=10 || link.m_fieldSize*link.m_N>entry.length()) {
1852 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readCellRefPos: the zone seems bad\n"));
1853 if (entry.valid()) {
1854 libmwaw::DebugFile &ascFile=zone.ascii();
1855 libmwaw::DebugStream f;
1856 f << "Entries(SheetRefPos)[" << zone << "]:###";
1857 zone.m_isParsed=true;
1858 ascFile.addPos(entry.begin());
1859 ascFile.addNote(f.str().c_str());
1860 }
1861 return false;
1862 }
1863 libmwaw::DebugFile &ascFile=zone.ascii();
1864 libmwaw::DebugStream f;
1865 f << "Entries(SheetRefPos)[" << zone << "]:";
1866 zone.m_isParsed=true;
1867 ascFile.addPos(entry.begin());
1868 ascFile.addNote(f.str().c_str());
1869 MWAWInputStreamPtr input=zone.getInput();
1870 input->setReadInverted(!zone.m_hiLoEndian);
1871 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1872 long endPos=entry.end();
1873
1874 for (int i=0; i<link.m_N; ++i) {
1875 long pos=input->tell();
1876 f.str("");
1877 f << "SheetRefPos-RP" << i+1 << ":";
1878 auto used=input->readULong(4);
1879 auto fl=input->readULong(2); // [0145][08][0-f][0-f]
1880 auto id2=input->readULong(4);
1881 if (used) {
1882 if (used!=1) f << "used=" << used << ",";
1883 // fl&0x40: 1 coord
1884 // fl&0x80: 2 coord
1885 if (fl & 0xff00) f << "fl=" << std::hex << (fl&0xff00) << std::dec << "],";
1886 auto const &refMap=(fl&0x80)==0 ? sheet.m_valueToCellRefMap : sheet.m_blockToCellRefMap;
1887 auto it=refMap.find(int(id2));
1888 if (it!=refMap.end()) {
1889 auto instr=it->second;
1890 instr.m_positionRelative[0][0]=(fl&1)==0;
1891 instr.m_positionRelative[0][1]=(fl&2)==0;
1892 // fl&4: plane[0]
1893 instr.m_positionRelative[1][0]=(fl&8)==0;
1894 instr.m_positionRelative[1][1]=(fl&0x10)==0;
1895 // fl&0x20: plane[1]
1896 sheet.m_refToCellRefMap[i+1]=instr;
1897 f << instr;
1898 }
1899 else {
1900 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readCellRefPos: can not find a ref\n"));
1901 f << "##" << ((fl&0x80)==0 ? "V" : "RB") << id2 << ",";
1902 }
1903 }
1904 ascFile.addPos(pos);
1905 ascFile.addNote(f.str().c_str());
1906 }
1907 long pos=input->tell();
1908 if (pos!=endPos) {
1909 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readCellRefPos: find extra data\n"));
1910 ascFile.addPos(pos);
1911 ascFile.addNote("SheetRefPos:end###");
1912 }
1913 input->setReadInverted(false);
1914 return true;
1915 }
1916
1917 ////////////////////////////////////////////////////////////
1918 // interface send function
1919 ////////////////////////////////////////////////////////////
1920 bool RagTime5Spreadsheet::send(int zoneId, MWAWListenerPtr listener, MWAWPosition const &pos, int partId)
1921 {
1922 auto it=m_state->m_idSheetMap.find(zoneId);
1923 if (it==m_state->m_idSheetMap.end() || !it->second) {
1924 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::send: can not find sheet for zone %d\n", zoneId));
1925 return false;
1926 }
1927 return send(*it->second, listener, pos, partId);
1928 }
1929
1930 bool RagTime5Spreadsheet::send(RagTime5SpreadsheetInternal::Sheet &sheet, MWAWListenerPtr listener, MWAWPosition const &position, int partId)
1931 {
1932 sheet.m_isSent=true;
1933
1934 if (!listener)
1935 listener=m_parserState->getMainListener();
1936 if (!listener) {
1937 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::send: can not find the listener\n"));
1938 return false;
1939 }
1940
1941 if (partId>1) {
1942 static bool first=true;
1943 if (first) {
1944 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::send: send partial sheet is not implemented, send all sheets\n"));
1945 first=false;
1946 }
1947 }
1948 // time to update default value
1949 MWAWSection section;
1950 if (sheet.m_defTextPLC.m_textStyleId)
1951 m_styleManager->updateTextStyles(sheet.m_defTextPLC.m_textStyleId,sheet.m_defaultFont,sheet.m_defaultParagraph, section);
1952
1953 MWAWSpreadsheetListenerPtr sheetListener=std::dynamic_pointer_cast<MWAWSpreadsheetListener>(listener);
1954 MWAWSpreadsheetEncoder spreadsheetEncoder;
1955 bool localListener=!sheetListener;
1956 if (localListener) {
1957 MWAWBox2f box=MWAWBox2f(MWAWVec2f(0,0), position.size());
1958 sheetListener=std::make_shared<MWAWSpreadsheetListener>(*m_parserState, box, &spreadsheetEncoder);
1959 sheetListener->startDocument();
1960 }
1961 for (int plane=1; plane<=sheet.getNumPlanes(); ++plane) {
1962 if (plane>static_cast<int>(sheet.m_planesList.size()))
1963 break;
1964 auto const &data=sheet.m_planesList[size_t(plane-1)];
1965 if (data.isEmpty()) continue;
1966 std::vector<int> repeatedWidths;
1967 std::vector<float> colWidths=sheet.getColumnWidths(repeatedWidths);
1968 sheetListener->openSheet(colWidths, librevenge::RVNG_POINT, repeatedWidths, sheet.getName(plane).cstr());
1969 int actRow=-1;
1970 for (auto const &rIt : data.m_rowsToDataMap) {
1971 if (rIt.second.isEmpty()) continue;
1972
1973 MWAWVec2i rowPos=rIt.first;
1974 auto const row=rIt.second;
1975 if (rowPos[0]>actRow+1) {
1976 // must not happen, so suppose all row have same size
1977 sheetListener->openSheetRow(sheet.getRowHeight(actRow+1), librevenge::RVNG_POINT, rowPos[0]-actRow-1);
1978 sheetListener->closeSheetRow();
1979 }
1980
1981 while (rowPos[0]<=rowPos[1]) {
1982 // we must check that all row have the same height, if not, we must send them separatly
1983 MWAWVec2i blockRow=rowPos;
1984 auto hIt=sheet.m_rowHeightsMap.lower_bound(MWAWVec2i(-1,blockRow[0]));
1985 if (hIt!=sheet.m_rowHeightsMap.end() && hIt->first[0]>=blockRow[0] && hIt->first[1]<blockRow[1])
1986 blockRow[1]=hIt->first[1];
1987 // ok let send the current block
1988 sheetListener->openSheetRow(sheet.getRowHeight(blockRow[0]), librevenge::RVNG_POINT, blockRow[1]-blockRow[0]+1);
1989 actRow=blockRow[1];
1990 for (auto const &cIt : row.m_columnsToDataMap) {
1991 RagTime5SpreadsheetInternal::CellContent const &cContent=cIt.second;
1992 if (cContent.isMergedCell()) continue;
1993
1994 bool isUnion=cContent.m_id[RagTime5SpreadsheetInternal::CellContent::Union]>=0;
1995 send(sheet, plane, cContent, isUnion ? 1 : cIt.first[1]-cIt.first[0]+1, sheetListener);
1996 }
1997 sheetListener->closeSheetRow();
1998 rowPos[0]=blockRow[1]+1;
1999 }
2000 }
2001 sheetListener->closeSheet();
2002 }
2003 if (localListener) {
2004 sheetListener->endDocument();
2005
2006 MWAWEmbeddedObject object;
2007 if (spreadsheetEncoder.getBinaryResult(object))
2008 listener->insertPicture(position, object);
2009 }
2010 return true;
2011 }
2012
2013 bool RagTime5Spreadsheet::send
2014 (RagTime5SpreadsheetInternal::Sheet &sheet, int plane, RagTime5SpreadsheetInternal::CellContent const &cContent, int numRepeated,
2015 MWAWSpreadsheetListenerPtr &listener)
2016 {
2017 if (!listener) {
2018 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::send: can not find the listener\n"));
2019 return false;
2020 }
2021
2022 MWAWCell cell;
2023 MWAWCellContent content;
2024 cell.setPosition(cContent.m_position);
2025 cell.setVAlignment(MWAWCell::VALIGN_CENTER);
2026 // the span
2027 if (cContent.m_id[RagTime5SpreadsheetInternal::CellContent::Union]>=0)
2028 cell.setNumSpannedCells(sheet.getSpan(cContent.m_position, cContent.m_plane));
2029 // graphic style: background color
2030 int graphicId=cContent.m_id[RagTime5SpreadsheetInternal::CellContent::GraphicStyle];
2031 if (graphicId>=0)
2032 graphicId=sheet.getGraphicStyleId(graphicId);
2033 else
2034 graphicId=sheet.m_defGraphicPLC.getGraphicStyleId();
2035 MWAWColor color;
2036 if (graphicId>0 && m_styleManager->getCellBackgroundColor(graphicId, color))
2037 cell.setBackgroundColor(color);
2038 // the border
2039 for (int i=0; i<4; ++i) {
2040 int const static wh[]= {RagTime5SpreadsheetInternal::CellContent::BorderPrevVStyle,
2041 RagTime5SpreadsheetInternal::CellContent::BorderNextVStyle,
2042 RagTime5SpreadsheetInternal::CellContent::BorderPrevHStyle,
2043 RagTime5SpreadsheetInternal::CellContent::BorderNextHStyle
2044 };
2045 int bId=cContent.m_id[wh[i]];
2046 if (bId<0) bId=sheet.m_defBordersPLC[i<2 ? 0 : 1].getBorderGraphicStyleId((i%2)==1);
2047 if (bId<=0) continue;
2048 MWAWBorder border;
2049 if (!m_styleManager->getCellBorder(bId, border))
2050 continue;
2051 int const final[]= {libmwaw::LeftBit, libmwaw::RightBit, libmwaw::TopBit, libmwaw::BottomBit};
2052 cell.setBorders(final[i], border);
2053 }
2054 // the value
2055 RagTime5SpreadsheetInternal::CellValue value;
2056 if (cContent.m_id[RagTime5SpreadsheetInternal::CellContent::Value]>0 &&
2057 cContent.m_id[RagTime5SpreadsheetInternal::CellContent::Value]<=static_cast<int>(sheet.m_valuesList.size()))
2058 value=sheet.m_valuesList[size_t(cContent.m_id[RagTime5SpreadsheetInternal::CellContent::Value]-1)];
2059 value.update(cell, content);
2060 if (value.m_formulaId) {
2061 auto fIt=sheet.m_idToFormula.find(value.m_formulaId);
2062 if (fIt==sheet.m_idToFormula.end() || fIt->second.empty()) {
2063 static bool first=true;
2064 if (first) {
2065 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::send: can not retrieve some formula\n"));
2066 first=false;
2067 }
2068 }
2069 else {
2070 content.m_contentType=MWAWCellContent::C_FORMULA;
2071 content.m_formula=fIt->second;
2072 // try to remove uneeded sheet name
2073 auto sheetName=sheet.getName(plane);
2074 for (auto &instr : content.m_formula) {
2075 if (instr.m_type==instr.F_Cell && instr.m_sheet[0]==sheetName)
2076 instr.m_sheet[0].clear();
2077 else if (instr.m_type==instr.F_CellList && instr.m_sheet[0]==sheetName && instr.m_sheet[1]==sheetName) {
2078 instr.m_sheet[0].clear();
2079 instr.m_sheet[1].clear();
2080 }
2081 }
2082 }
2083 }
2084 // the number/text/format plc
2085 RagTime5SpreadsheetInternal::TextPLC plc;
2086 MWAWFont font(sheet.m_defaultFont);
2087 MWAWParagraph para(sheet.m_defaultParagraph);
2088 MWAWSection section;
2089 if (cContent.m_id[RagTime5SpreadsheetInternal::CellContent::TextStyle]>=0)
2090 sheet.getTextPLC(cContent.m_id[RagTime5SpreadsheetInternal::CellContent::TextStyle], plc);
2091 if (plc.m_formatId)
2092 m_styleManager->updateCellFormat(plc.m_formatId, cell);
2093 if (plc.m_textStyleId)
2094 m_styleManager->updateTextStyles(plc.m_textStyleId,font,para,section);
2095 auto align=plc.getHorizontalAlignment();
2096 if (align!=MWAWCell::HALIGN_DEFAULT)
2097 cell.setHAlignment(align);
2098 auto rot=(plc.m_flags>>16)&3;
2099 if (rot) // fixme, if rot=1|3, we must also swap H/V alignement
2100 cell.setRotation(rot*90);
2101 cell.setFont(font);
2102 listener->openSheetCell(cell, content, numRepeated);
2103 if (value.m_type==7) { // small text zone
2104 listener->setFont(font);
2105 listener->setParagraph(para);
2106 listener->insertUnicodeString(value.m_text);
2107 }
2108 else if (value.m_type==8 || value.m_type==9) // big text zone
2109 m_document.send(sheet.m_textboxZoneId, listener, MWAWPosition(), 0, static_cast<int>(value.m_id&0xFFFFFF));
2110 else if (value.m_type==0xa) { // a zone
2111 if (value.m_id>=static_cast<unsigned long>(sheet.m_childList.size()) ||
2112 sheet.m_childList[size_t(value.m_id)].m_dataId==0) {
2113 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::send: the child list seems bad\n"));
2114 }
2115 else {
2116 auto cellName=sheet.getName(cContent.m_plane);
2117 cellName.append('.');
2118 cellName.append(MWAWCell::getBasicCellName(sheet.getBottomRightCell(cContent.m_position,cContent.m_plane)).c_str());
2119 MWAWPosition position(MWAWVec2f(0,0), sheet.getCellDimensions(cContent.m_position, cContent.m_plane), librevenge::RVNG_POINT);
2120 position.setAnchorToCell(cellName);
2121 auto const &link=sheet.m_childList[size_t(value.m_id)];
2122
2123 auto type=m_document.getClusterType(link.m_dataId);
2124 if (type==RagTime5ClusterManager::Cluster::C_PictureZone)
2125 m_document.send(link.m_dataId, listener, position, int(link.getSubZoneId(0)&0xFFFFFF));
2126 else if (type==RagTime5ClusterManager::Cluster::C_ButtonZone)
2127 m_document.sendButtonZoneAsText(listener, link.m_dataId);
2128 else {
2129 // let try to create a graphic object to represent the content
2130 MWAWBox2f box(MWAWVec2f(0,0), position.size());
2131 MWAWGraphicEncoder graphicEncoder;
2132 MWAWGraphicListenerPtr graphicListener(new MWAWGraphicListener(*m_parserState, box, &graphicEncoder));
2133 graphicListener->startDocument();
2134 MWAWPosition graphicPos;
2135 graphicPos.m_anchorTo = MWAWPosition::Page;
2136 m_document.send(link.m_dataId, graphicListener, graphicPos, int(link.getSubZoneId(0)&0xFFFFFF));
2137 graphicListener->endDocument();
2138
2139 MWAWEmbeddedObject picture;
2140 if (graphicEncoder.getBinaryResult(picture))
2141 listener->insertPicture(position, picture);
2142 }
2143 }
2144 }
2145 listener->closeSheetCell();
2146 return true;
2147 }
2148
2149 void RagTime5Spreadsheet::flushExtra(bool onlyCheck)
2150 {
2151 MWAWListenerPtr listener=m_parserState->getMainListener();
2152 if (!listener) {
2153 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::flushExtra: can not find a listener\n"));
2154 return;
2155 }
2156 MWAWPosition position(MWAWVec2f(0,0), MWAWVec2f(100,100), librevenge::RVNG_POINT);
2157 position.m_anchorTo=MWAWPosition::Char;
2158 for (auto const &it : m_state->m_idSheetMap) {
2159 if (!it.second || it.second->m_isSent) continue;
2160 static bool first=true;
2161 if (first) {
2162 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::flushExtra: find some unsent spreadsheets %d, ...\n", it.first));
2163 first=false;
2164 }
2165 if (!onlyCheck)
2166 send(*it.second, listener, position);
2167 }
2168 }
2169
2170 ////////////////////////////////////////////////////////////
2171 // cluster parser
2172 ////////////////////////////////////////////////////////////
2173
2174 namespace RagTime5SpreadsheetInternal
2175 {
2176
2177 //! low level: the spreadsheet cluster data
2178 struct ClusterSpreadsheet final : public RagTime5ClusterManager::Cluster {
2179 //! constructor
2180 ClusterSpreadsheet()
2181 : RagTime5ClusterManager::Cluster(C_SpreadsheetZone)
2182 , m_dimensionLink()
2183 , m_valuesLink()
2184 , m_valuesTreeLink()
2185 , m_valuesTreeRoot(0)
2186 , m_valuesMaxPos(0,0,0)
2187 , m_graphPLCLink()
2188 , m_graphPLCNumRowByPlanesMap()
2189 , m_textPLCLink()
2190 , m_textPLCNumRowByPlanesMap()
2191 {
2192 }
2193 //! destructor
2194 ~ClusterSpreadsheet() final;
2195 //! the dimension link
2196 RagTime5ClusterManager::Link m_dimensionLink;
2197 //! the list of values link
2198 RagTime5ClusterManager::Link m_valuesLink;
2199 //! the value tree link
2200 RagTime5ClusterManager::Link m_valuesTreeLink;
2201 //! the value tree root
2202 int m_valuesTreeRoot;
2203 //! the maximum values position
2204 MWAWVec3i m_valuesMaxPos;
2205 //! the graph PLC link
2206 RagTime5ClusterManager::Link m_graphPLCLink;
2207 //! the list of row graph plc by planes
2208 std::map<MWAWVec2i,int> m_graphPLCNumRowByPlanesMap;
2209 //! the text PLC link
2210 RagTime5ClusterManager::Link m_textPLCLink;
2211 //! the list of row text plc by planes
2212 std::map<MWAWVec2i,int> m_textPLCNumRowByPlanesMap;
2213 //! the border PLC link(vertical then horizontal)
2214 RagTime5ClusterManager::Link m_borderPLCLink[2];
2215 //! the list of row border plc by planes
2216 std::map<MWAWVec2i,int> m_borderPLCNumRowByPlanesMap[2];
2217 //! the reference block/cell union/reference pos link
2218 RagTime5ClusterManager::Link m_blockLinks[3];
2219 };
2220
2221 ClusterSpreadsheet::~ClusterSpreadsheet()
2222 {
2223 }
2224
2225 //
2226 //! low level: parser of main spreadsheet cluster
2227 //
2228 struct SpreadsheetCParser final : public RagTime5ClusterManager::ClusterParser {
2229 enum { F_borderRoot, F_borderH, F_borderV, F_cellsTree, F_cellsTreeValue, F_cellsUnion, F_dims, F_graphPLC, F_name, F_nameRoot=F_name+3, F_sheetList, F_nextId=F_sheetList+3, F_parentList, F_childList, F_refBlock, F_refPos, F_textPLC, F_unknA, F_unknARoot };
2230 //! constructor
2231 SpreadsheetCParser(RagTime5ClusterManager &parser, int type)
2232 : ClusterParser(parser, type, "ClustSheet")
2233 , m_cluster(new ClusterSpreadsheet)
2234 , m_sheet(new Sheet)
2235 , m_fieldName("")
2236 , m_defaultPLCValues()
2237 , m_PLCNumRowByPlanesMap()
2238
2239 , m_expectedIdToType()
2240 , m_idStack()
2241 {
2242 }
2243 //! destructor
2244 ~SpreadsheetCParser() final;
2245 //! return the spreadsheet cluster
2246 std::shared_ptr<ClusterSpreadsheet> getSpreadsheetCluster()
2247 {
2248 return m_cluster;
2249 }
2250 //! return the current cluster
2251 std::shared_ptr<RagTime5ClusterManager::Cluster> getCluster() final
2252 {
2253 return m_cluster;
2254 }
2255 //! return the spreadsheet
2256 std::shared_ptr<Sheet> getSpreadsheet()
2257 {
2258 return m_sheet;
2259 }
2260 //! set a data id type
2261 void setExpectedType(int id, int type)
2262 {
2263 m_expectedIdToType[id]=type;
2264 m_idStack.push(id);
2265 }
2266 /** returns to new zone to parse. */
2267 int getNewZoneToParse() final
2268 {
2269 if (m_idStack.empty())
2270 return -1;
2271 int id=m_idStack.top();
2272 m_idStack.pop();
2273 return id;
2274 }
2275 //! end of a start zone call
2276 void endZone() final
2277 {
2278 if (m_link.empty())
2279 return;
2280 auto const &it=m_expectedIdToType.find(m_dataId);
2281 int expected=(it!=m_expectedIdToType.end())?it->second : -1;
2282 switch (expected) {
2283 case F_cellsTree:
2284 if (m_cluster->m_valuesTreeLink.empty())
2285 m_cluster->m_valuesTreeLink=m_link;
2286 else {
2287 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::endZone: oops the values tree link is already set\n"));
2288 m_cluster->m_linksList.push_back(m_link);
2289 }
2290 break;
2291 case F_cellsTreeValue:
2292 if (m_cluster->m_valuesLink.empty())
2293 m_cluster->m_valuesLink=m_link;
2294 else {
2295 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::endZone: oops the values link is already set\n"));
2296 m_cluster->m_linksList.push_back(m_link);
2297 }
2298 break;
2299 case F_dims:
2300 if (m_cluster->m_dimensionLink.empty())
2301 m_cluster->m_dimensionLink=m_link;
2302 else {
2303 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::endZone: oops the dimension link is already set\n"));
2304 m_cluster->m_linksList.push_back(m_link);
2305 }
2306 break;
2307 case F_name:
2308 if (m_cluster->m_nameLink.empty())
2309 m_cluster->m_nameLink=RagTime5ClusterManager::NameLink(m_link);
2310 else {
2311 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::endZone: oops the name link is already set\n"));
2312 m_cluster->m_linksList.push_back(m_link);
2313 }
2314 break;
2315 case F_name+1:
2316 case F_name+2:
2317 case F_sheetList:
2318 case F_sheetList+1:
2319 case F_sheetList+2:
2320 m_cluster->m_linksList.push_back(m_link);
2321 break;
2322 case F_childList:
2323 if (m_cluster->m_childLink.empty())
2324 m_cluster->m_childLink=m_link;
2325 else {
2326 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::endZone: oops the picture cluster link is already set\n"));
2327 m_cluster->m_linksList.push_back(m_link);
2328 }
2329 break;
2330 case F_parentList:// parent link (graphic or pipeline)
2331 if (m_cluster->m_parentLink.empty())
2332 m_cluster->m_parentLink=m_link;
2333 else {
2334 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::endZone: oops the picture cluster link is already set\n"));
2335 m_cluster->m_linksList.push_back(m_link);
2336 }
2337 break;
2338 case F_cellsUnion:
2339 case F_refBlock:
2340 if (expected==F_refBlock && m_cluster->m_blockLinks[0].empty())
2341 m_cluster->m_blockLinks[0]=m_link;
2342 else if (expected==F_cellsUnion && m_cluster->m_blockLinks[1].empty())
2343 m_cluster->m_blockLinks[1]=m_link;
2344 else {
2345 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::endZone: oops the two block links are already set\n"));
2346 m_cluster->m_linksList.push_back(m_link);
2347 }
2348 break;
2349 case F_refPos:
2350 if (m_cluster->m_blockLinks[2].empty())
2351 m_cluster->m_blockLinks[2]=m_link;
2352 else {
2353 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::endZone: oops the last block links is already set\n"));
2354 m_cluster->m_linksList.push_back(m_link);
2355 }
2356 break;
2357 case F_borderH:
2358 case F_borderV:
2359 case F_graphPLC:
2360 case F_textPLC:
2361 if (m_link.m_fieldSize==6 && expected==F_graphPLC && m_cluster->m_graphPLCLink.empty()) {
2362 m_cluster->m_graphPLCLink=m_link;
2363 m_cluster->m_graphPLCNumRowByPlanesMap=m_PLCNumRowByPlanesMap;
2364 m_sheet->m_defGraphicPLC=GraphicPLC(m_defaultPLCValues);
2365 }
2366 else if (m_link.m_fieldSize==10 && expected==F_textPLC && m_cluster->m_textPLCLink.empty()) {
2367 m_cluster->m_textPLCLink=m_link;
2368 m_cluster->m_textPLCNumRowByPlanesMap=m_PLCNumRowByPlanesMap;
2369 m_sheet->m_defTextPLC=TextPLC(m_defaultPLCValues);
2370 }
2371 else if (m_link.m_fieldSize==14 && expected==F_borderH && m_cluster->m_borderPLCLink[1].empty()) { // horizontal
2372 m_cluster->m_borderPLCLink[1]=m_link;
2373 m_cluster->m_borderPLCNumRowByPlanesMap[1]=m_PLCNumRowByPlanesMap;
2374 m_sheet->m_defBordersPLC[1]=BorderPLC(m_defaultPLCValues);
2375 }
2376 else if (m_link.m_fieldSize==14 && expected==F_borderV && m_cluster->m_borderPLCLink[0].empty()) { // vertical
2377 m_cluster->m_borderPLCLink[0]=m_link;
2378 m_cluster->m_borderPLCNumRowByPlanesMap[0]=m_PLCNumRowByPlanesMap;
2379 m_sheet->m_defBordersPLC[0]=BorderPLC(m_defaultPLCValues);
2380 }
2381 else {
2382 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::endZone: oops find unexpected PLC link with size %d\n",
2383 m_link.m_fieldSize));
2384 m_cluster->m_linksList.push_back(m_link);
2385 }
2386 break;
2387 default:
2388 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::endZone: oops find unexpected link for field %d\n",
2389 m_dataId));
2390 m_cluster->m_linksList.push_back(m_link);
2391 break;
2392 }
2393 }
2394 //! parse a zone
2395 bool parseZone(MWAWInputStreamPtr &input, long fSz, int N, int flag, libmwaw::DebugStream &f) final
2396 {
2397 m_fieldName="";
2398 if (N==-5)
2399 return parseHeaderZone(input,fSz,N,flag,f);
2400 if (N<0) {
2401 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseZone: expected N value\n"));
2402 f << "###N=" << N << ",";
2403 return true;
2404 }
2405 return parseDataZone(input, fSz, N, flag, f);
2406 }
2407 //! parse a field
2408 bool parseField(RagTime5StructManager::Field const &field, int /*m*/, libmwaw::DebugStream &f) final
2409 {
2410 if (!m_fieldName.empty())
2411 f << m_fieldName << ",";
2412 auto const &it=m_expectedIdToType.find(m_dataId);
2413 int expected=(it!=m_expectedIdToType.end())?it->second : -1;
2414 switch (expected) {
2415 case F_borderH:
2416 case F_borderV:
2417 case F_graphPLC:
2418 case F_textPLC:
2419 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0xce842) {
2420 f << "pos=[";
2421 for (auto val : field.m_longList)
2422 f << val << ",";
2423 f << "],";
2424 m_link.m_longList=field.m_longList;
2425 return true;
2426 }
2427 if (field.m_type==RagTime5StructManager::Field::T_FieldList && field.m_fileType==0x1671845) {
2428 f << "nData[byPlane]=["; // find only 0|1000X,[3e7f|3e80]0001: probably a selection
2429 for (auto const &child : field.m_fieldList) {
2430 if (child.m_type==RagTime5StructManager::Field::T_LongList && child.m_fileType==0xce842) {
2431 int actPlanes=1;
2432 for (auto val : child.m_longList) {
2433 auto nPlanes=static_cast<int>(val>>16);
2434 int nData=(val&0xFFFF);
2435 m_PLCNumRowByPlanesMap[MWAWVec2i(actPlanes,actPlanes+nPlanes-1)]=nData;
2436 actPlanes+=nPlanes;
2437 f << nData;
2438 if (nPlanes!=1)
2439 f << "[" << nPlanes << "]";
2440 f << ",";
2441 }
2442 if (actPlanes!=16001) {
2443 MWAW_DEBUG_MSG(("RagTime5GraphInternal::SpreadsheetCParser::parseField: the number of planes seems bad\n"));
2444 f << "###";
2445 m_PLCNumRowByPlanesMap.clear();
2446 }
2447 continue;
2448 }
2449 MWAW_DEBUG_MSG(("RagTime5GraphInternal::SpreadsheetCParser::parseField: find unexpected child[fSz=91]\n"));
2450 f << "##[" << child << "],";
2451 }
2452 f << "],";
2453 break;
2454 }
2455 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0x1671817) {
2456 f << "default=[";
2457 for (auto val : field.m_longList) {
2458 m_defaultPLCValues.push_back(int(val));
2459 f << val << ",";
2460 }
2461 if (expected==F_textPLC && m_defaultPLCValues.size()==4) { // FIXME this assume hilo
2462 m_defaultPLCValues[2]=(m_defaultPLCValues[2]<<16)+m_defaultPLCValues[3];
2463 m_defaultPLCValues.resize(3);
2464 }
2465 f << "],";
2466 break;
2467 }
2468 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseField: find unexpected list link field\n"));
2469 f << "###" << field;
2470 break;
2471 case F_name:
2472 case F_name+1:
2473 case F_name+2:
2474 case F_sheetList:
2475 case F_sheetList+1:
2476 case F_sheetList+2:
2477 case F_parentList:
2478 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0xce842) {
2479 f << "pos=[";
2480 for (auto val : field.m_longList)
2481 f << val << ",";
2482 f << "],";
2483 m_link.m_longList=field.m_longList;
2484 return true;
2485 }
2486 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseField: find unexpected list link field\n"));
2487 f << "###" << field;
2488 break;
2489 case F_cellsTreeValue:
2490 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0xce842) {
2491 f << "pos=[";
2492 for (auto val : field.m_longList)
2493 f << val << ",";
2494 f << "],";
2495 m_link.m_longList=field.m_longList;
2496 return true;
2497 }
2498 if (field.m_type==RagTime5StructManager::Field::T_Unstructured && field.m_fileType==0xce017) {
2499 // a small value 2|4|a|1c|40
2500 f << "unkn="<<field.m_extra << ",";
2501 break;
2502 }
2503 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseField: find unexpected list link field\n"));
2504 f << "###" << field;
2505 break;
2506 case F_cellsUnion:
2507 case F_refBlock:
2508 case F_refPos:
2509 if (field.m_type==RagTime5StructManager::Field::T_Unstructured && field.m_fileType==0xce017) {
2510 // a small value 2|4|a|1c|40
2511 f << "unkn="<<field.m_extra << ",";
2512 break;
2513 }
2514 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseField: find unexpected list link field\n"));
2515 f << "###" << field;
2516 break;
2517 case F_unknARoot:
2518 if (field.m_type==RagTime5StructManager::Field::T_LongList && field.m_fileType==0xcf042) {
2519 f << "ids=[";
2520 for (auto val : field.m_longList) {
2521 if (val==0) {
2522 f << "_,";
2523 continue;
2524 }
2525 setExpectedType(int(val-1), F_unknA);
2526 f << "F" << val-1 << ",";
2527 }
2528 break;
2529 }
2530 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseField: find unexpected list link field\n"));
2531 f << "###" << field;
2532 break;
2533 default:
2534 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseField: find unexpected list link field\n"));
2535 f << "###" << field;
2536 break;
2537 }
2538 return true;
2539 }
2540 protected:
2541 //! parse a data block
2542 bool parseDataZone(MWAWInputStreamPtr &input, long fSz, int N, int flag, libmwaw::DebugStream &f)
2543 {
2544 auto const &it=m_expectedIdToType.find(m_dataId);
2545 int expected=(it!=m_expectedIdToType.end())?it->second : -1;
2546 if (expected!=-1)
2547 f << "[F" << m_dataId << "]";
2548 if (flag!=0x10)
2549 f << "fl=" << std::hex << flag << std::dec << ",";
2550
2551 long pos=input->tell();
2552 int val;
2553 long linkValues[4];
2554 std::string mess;
2555 m_link.m_N=N;
2556 switch (expected) {
2557 case F_cellsTreeValue:
2558 case F_cellsUnion:
2559 case F_dims:
2560 case F_name:
2561 case F_name+1:
2562 case F_name+2:
2563 case F_sheetList:
2564 case F_sheetList+1:
2565 case F_sheetList+2:
2566 case F_parentList:
2567 case F_childList:
2568 case F_refBlock:
2569 case F_refPos: {
2570 if (fSz<28 || !readLinkHeader(input, fSz, m_link, linkValues, mess)) {
2571 f << "###fType=" << RagTime5ClusterManager::printType(m_link.m_fileType[0]) << ",";
2572 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: the expected field[%d] seems bad\n", expected));
2573 return true;
2574 }
2575 f << m_link << "," << mess;
2576 long expectedFileType1=-1, expectedFieldSize=0;
2577 if (expected==F_name && fSz==32) {
2578 if (m_link.m_fileType[0]) {
2579 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: the expected field[%d] fileType0 seems odd\n", expected));
2580 f << "###fileType0=" << RagTime5ClusterManager::printType(m_link.m_fileType[0]) << ",";
2581 }
2582 expectedFileType1=0x200;
2583 m_link.m_type=RagTime5ClusterManager::Link::L_UnicodeList;
2584 m_link.m_name="unicode";
2585 }
2586 else if ((expected==F_name+1 || expected==F_name+2 || expected==F_sheetList+1) && m_link.m_fileType[0]==0x35800)
2587 m_link.m_name=expected==F_name+1 ? "unicodeList1" : expected==F_name+2 ? "unicodeList2" : "sheetList1";
2588 else if (expected==F_sheetList && m_link.m_fileType[0]==0x3e800)
2589 m_link.m_name="sheetList0";
2590 else if (expected==F_sheetList+2 && m_link.m_fileType[0]==0x45080) {
2591 expectedFieldSize=2;
2592 m_link.m_name="sheetListInt";
2593 }
2594 else if (expected==F_dims && fSz==34) {
2595 expectedFileType1=0x40;
2596 expectedFieldSize=24;
2597 m_link.m_name="dims";
2598 val=static_cast<int>(input->readULong(4));
2599 if (val==32000) // always 0|32000
2600 f << "num[data32000],";
2601 else if (val)
2602 f << "num[data]=" << val << ",";
2603 }
2604 else if (expected==F_cellsTreeValue && fSz==34) {
2605 if (m_link.m_fileType[0]) {
2606 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: the expected field[%d] fileType0 seems odd\n", expected));
2607 f << "###fileType0=" << RagTime5ClusterManager::printType(m_link.m_fileType[0]) << ",";
2608 }
2609 expectedFileType1=0x10;
2610 m_link.m_name="cells[tree,values]";
2611 val=static_cast<int>(input->readULong(2));
2612 if (val) // some dataA ?
2613 f << "f0=" << val << ",";
2614 }
2615 else if ((expected==F_cellsUnion || expected==F_refBlock) && fSz==34) {
2616 if (m_link.m_fieldSize!=0x16 && m_link.m_fieldSize!=0x18) { // 5: 0x16, 6:0x18
2617 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: the expected field[%d] fieldSize seems odd\n", expected));
2618 f << "###fieldSize=" << m_link.m_fieldSize << ",";
2619 }
2620 expectedFileType1=0x50;
2621 m_link.m_name=expected==F_cellsUnion ? "cells[union]" : "ref[block]";
2622 val=static_cast<int>(input->readULong(4)); // always 1
2623 if (val!=1)
2624 f << "g0=" << val << ",";
2625 }
2626 else if (expected==F_parentList && fSz==36) { // parent link: graphic or pipeline link
2627 if (m_link.m_fileType[0]) {
2628 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: the expected field[%d] fileType0 seems odd\n", expected));
2629 f << "###fileType0=" << RagTime5ClusterManager::printType(m_link.m_fileType[0]) << ",";
2630 }
2631 expectedFileType1=0x10;
2632 m_link.m_name="parentList";
2633 f << "interval=";
2634 for (int i=0; i<2; ++i)
2635 f << input->readULong(2) << (i==0 ? "->" : ",");
2636 }
2637 else if (expected==F_childList && fSz==30) {
2638 expectedFieldSize=12;
2639 expectedFileType1=0xd0;
2640 m_link.m_name="sheetChildLst";
2641 m_link.m_type=RagTime5ClusterManager::Link::L_ClusterLink;
2642 }
2643 else if (expected==F_refPos && fSz==34) {
2644 expectedFileType1=0x50;
2645 expectedFieldSize=10;
2646 m_link.m_name="ref[pos]";
2647 val=static_cast<int>(input->readULong(4)); // always 1
2648 if (val!=1)
2649 f << "g0=" << val << ",";
2650 }
2651 else {
2652 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: the expected field[%d] seems bad\n", expected));
2653 f << "###";
2654 }
2655 if (!m_link.m_name.empty()) {
2656 f << m_link.m_name << ",";
2657 m_fieldName=m_link.m_name;
2658 }
2659 if (expectedFileType1>=0 && long(m_link.m_fileType[1]&0xFFD7)!=expectedFileType1) {
2660 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: the expected field[%d] fileType1 seems odd\n", expected));
2661 f << "###fileType1=" << std::hex << m_link.m_fileType[1] << std::dec << ",";
2662 }
2663 if (expectedFieldSize>0 && m_link.m_fieldSize!=expectedFieldSize) {
2664 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: fieldSize seems odd[%d]\n", expected));
2665 f << "###fieldSize,";
2666 }
2667 return true;
2668 }
2669 case F_borderRoot:
2670 if (fSz<16) {
2671 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseField: find unexpected size\n"));
2672 f << "###fSz";
2673 return true;
2674 }
2675 m_fieldName="border[root]";
2676 f << m_fieldName << ",";
2677 for (int i=0; i<2; ++i) { // either 0,0 or g1=g0+1
2678 val=static_cast<int>(input->readLong(4));
2679 if (!val) continue;
2680 setExpectedType(val-1, i==0 ? F_borderV : F_borderH);
2681 f << "border" << (i==0 ? "V": "H") << "=F" << val-1 << ",";
2682 }
2683 val=static_cast<int>(input->readLong(2)); // 4 or 8
2684 if (val!=4) f << "g2=" << val << ",";
2685 return true;
2686 case F_borderH:
2687 case F_borderV:
2688 case F_graphPLC: // fSz=71
2689 case F_textPLC: {
2690 if (fSz<69) {
2691 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseField: find unexpected size\n"));
2692 f << "###fSz";
2693 return true;
2694 }
2695 if (!readLinkHeader(input, fSz, m_link, linkValues, mess)) {
2696 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: can not read link for fSz69...\n"));
2697 input->seek(pos+26, librevenge::RVNG_SEEK_SET);
2698 f << "###link,";
2699 }
2700 else {
2701 if ((m_link.m_fileType[1]&0xFFD7)!=0x8000) {
2702 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: fileType1 seems odd[fSz69...]\n"));
2703 f << "###fileType1=" << std::hex << m_link.m_fileType[1] << std::dec << ",";
2704 }
2705 f << m_link << ",";
2706 }
2707 val=static_cast<int>(input->readLong(4)); // always 1
2708 if (val!=1)
2709 f << "g0=" << val << ",";
2710 m_link.m_fieldSize=static_cast<int>(input->readLong(2));
2711 val=static_cast<int>(input->readULong(2));
2712 m_defaultPLCValues.clear();
2713 m_PLCNumRowByPlanesMap.clear();
2714 if (val==0x3e81 && (expected==F_borderH || expected==F_borderV) && m_link.m_fieldSize==14) {
2715 m_fieldName= "border[PLC]";
2716 m_fieldName+=expected==F_borderV ? "[vert]" : "[hori]";
2717 m_link.m_name=m_fieldName;
2718 }
2719 else if (val==0x3e80 && ((expected==F_graphPLC && m_link.m_fieldSize==6) || (expected==F_textPLC && m_link.m_fieldSize==10)))
2720 m_link.m_name=m_fieldName=expected==F_graphPLC ? "graph[PLC]" : "text[PLC]";
2721 else {
2722 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: fileType2 seems odd[fSz69...]\n"));
2723 f << "###fileType2=" << std::hex << val << std::dec << ",";
2724 }
2725 f << m_fieldName << ",";
2726 val=static_cast<int>(input->readLong(2)); // always 1
2727 if (val!=1)
2728 f << "g2=" << val << ",";
2729 val=static_cast<int>(input->readLong(4)); // 1-2
2730 if (val!=2)
2731 f << "g3=" << val << ",";
2732 auto type=input->readULong(4);
2733 if (type!=0x34800) {
2734 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: fileType3 seems odd[fSz69...]\n"));
2735 f << "###fileType3=" << RagTime5Spreadsheet::printType(type) << ",";
2736 }
2737 for (int i=0; i<9; ++i) { // h6=32
2738 val=static_cast<int>(input->readLong(2));
2739 if (val)
2740 f << "h" << i << "=" << val << ",";
2741 }
2742 val=static_cast<int>(input->readLong(1)); // always 1
2743 if (val!=1)
2744 f << "h9=" << val << ",";
2745 if (fSz==71) { // graph[PLC]
2746 val=static_cast<int>(input->readLong(2)); // always 0
2747 if (val)
2748 f << "h10=" << val << ",";
2749 }
2750 return true;
2751 }
2752 case F_cellsTree: {
2753 if (fSz<58) {
2754 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseField: find unexpected size\n"));
2755 f << "###fSz";
2756 return true;
2757 }
2758 m_fieldName="cells[tree]";
2759 f << m_fieldName << ",";
2760 f << "root=VT" << N << ",";
2761 if (m_cluster->m_valuesTreeRoot) {
2762 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: the value tree root is already set\n"));
2763 f << "###";
2764 }
2765 else
2766 m_cluster->m_valuesTreeRoot=N;
2767 val=static_cast<int>(input->readLong(2)); // always 1
2768 if (val!=1) f << "g0=" << val << ",";
2769 m_link.m_N=static_cast<int>(input->readLong(4));
2770 long actPos=input->tell();
2771 m_fieldName=m_link.m_name="VTree";
2772 if (!readLinkHeader(input, fSz, m_link, linkValues, mess)) {
2773 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: can not read link for fSz58\n"));
2774 input->seek(actPos+30, librevenge::RVNG_SEEK_SET);
2775 f << "###link,";
2776 }
2777 else {
2778 f << m_link << "," << mess;
2779 m_link.m_fileType[0]=0;
2780 if ((m_link.m_fileType[1]&0xFFD7)!=0x40) {
2781 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: fileType1 seems odd[fSz58]\n"));
2782 f << "###fileType1=" << std::hex << m_link.m_fileType[1] << std::dec << ",";
2783 }
2784 }
2785 val=static_cast<int>(input->readLong(4));
2786 if (val) {
2787 setExpectedType(val-1, F_cellsTreeValue);
2788 f << "cells[tree,val]=F" << val-1 << ",";
2789 }
2790 f << "num=[";
2791 for (int i=0; i<4; ++i) {
2792 val=static_cast<int>(input->readULong(2));
2793 if (!val)
2794 f << "_,";
2795 else
2796 f << val << ",";
2797 }
2798 f << "],";
2799
2800 int dim[3];
2801 for (auto &d : dim) d=static_cast<int>(input->readULong(2));
2802 MWAWVec3i maxCell(dim[0],dim[1],dim[2]);
2803 if (m_cluster->m_valuesTreeLink.empty())
2804 m_cluster->m_valuesMaxPos=MWAWVec3i(dim[0],dim[1],dim[2]);
2805 f << "cell[max]=" << maxCell << ",";
2806 val=static_cast<int>(input->readLong(4)); // always 1?
2807 if (val!=1)
2808 f << "g2=" << val << ",";
2809 return true;
2810 }
2811 case F_unknA: {
2812 m_fieldName="unknA";
2813 f << m_fieldName << ",";
2814 if (fSz<68) {
2815 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseField: find unexpected size\n"));
2816 f << "###fSz";
2817 return true;
2818 }
2819 for (int i=0; i<2; ++i) { // always 0
2820 val=static_cast<int>(input->readLong(2));
2821 if (val)
2822 f << "f" << i << "=" << val << ",";
2823 }
2824 for (int i=0; i<2; ++i) { // f2=1, f3=0|a big number
2825 val=static_cast<int>(input->readLong(4));
2826 if (val)
2827 f << "f" << i+2 << "=" << val << ",";
2828 }
2829 auto type=input->readULong(4);
2830 if (type!=0x1646042) {
2831 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: fileType0 seems odd[fSz68]\n"));
2832 f << "###fileType0=" << RagTime5Spreadsheet::printType(type) << ",";
2833 }
2834 for (int i=0; i<4; ++i) { // always 0
2835 val=static_cast<int>(input->readLong(2));
2836 if (val)
2837 f << "f" << i+4 << "=" << val << ",";
2838 }
2839 f << "num0=[";
2840 for (int i=0; i<3; ++i) { // small number
2841 val=static_cast<int>(input->readLong(2));
2842 if (val)
2843 f << val << ",";
2844 else
2845 f << "_,";
2846 }
2847 f << "],";
2848 val=static_cast<int>(input->readULong(4)); // always 1
2849 if (val!=1) f << "f8=" << val << ",";
2850 for (int i=0; i<2; ++i) { // always 0
2851 val=static_cast<int>(input->readLong(2));
2852 if (val)
2853 f << "f" << i+9 << "=" << val << ",";
2854 }
2855 f << "num1=[";
2856 for (int i=0; i<10; ++i) { // find X,_,_,X,X,_,_,X,X,_ where X are some small numbers
2857 val=static_cast<int>(input->readLong(1));
2858 if (val)
2859 f << val << ",";
2860 else
2861 f << "_,";
2862 }
2863 f << "],";
2864 f << "num2=[";
2865 for (int i=0; i<7; ++i) { // first always 0, other some ints
2866 val=static_cast<int>(input->readLong(2));
2867 if (val)
2868 f << val << ",";
2869 else
2870 f << "_,";
2871 }
2872 f << "],";
2873 return true;
2874 }
2875 default:
2876 break;
2877 }
2878 if (expected==-1) {
2879 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: find unexpected field[%d]\n", m_dataId));
2880 f << "###";
2881 }
2882 switch (fSz) {
2883 case 29: { // unknARoot
2884 if (!readLinkHeader(input, fSz, m_link, linkValues, mess)) {
2885 f << "###fType=" << RagTime5Spreadsheet::printType(m_link.m_fileType[0]) << ",";
2886 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: the field type seems bad\n"));
2887 return true;
2888 }
2889 long expectedFileType1=0;
2890 if (m_link.m_fileType[0]==0x3c052) {
2891 m_link.m_fileType[0]=0;
2892 if (linkValues[0]!=0x1454877) {
2893 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: find unexpected linkValue[0]\n"));
2894 f << "#lValues0,";
2895 }
2896 expectedFileType1=0x50;
2897 m_fieldName="unknA[root]";
2898 m_expectedIdToType[m_dataId]=F_unknARoot;
2899 if (linkValues[2]) {
2900 setExpectedType(int(linkValues[2]-1), F_nextId);
2901 f << "next[id]=F" << linkValues[2]-1 << ",";
2902 }
2903 }
2904 else {
2905 f << "###fType=" << m_link << ",";
2906 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: the field fSz28 type seems bad\n"));
2907 return true;
2908 }
2909 if (expectedFileType1>0 && long(m_link.m_fileType[1]&0xFFD7)!=expectedFileType1) {
2910 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: fileType1 seems odd[fSz=28...]\n"));
2911 f << "###fileType1=" << std::hex << m_link.m_fileType[1] << std::dec << ",";
2912 }
2913 f << m_link << "," << mess;
2914 val=static_cast<int>(input->readLong(1));
2915 if (val!=1) // always 1
2916 f << "g0=" << val << ",";
2917 break;
2918 }
2919 case 36: {
2920 // probably related to named cell
2921 m_expectedIdToType[m_dataId]=F_nameRoot;
2922 auto type=input->readULong(4);
2923 auto type1=input->readULong(4);
2924 if ((type==0x35800 && type1==0x1454857) || (type==0 && type1==0x17db042)) {
2925 m_fieldName=type1==0x1454857? "name[root]" : "sheetList[root]";
2926 f << m_fieldName << ",";
2927 for (int i=0; i<2; ++i) { // g1=1669817
2928 val=static_cast<int>(input->readLong(4));
2929 if (val) f << "g" << i << "=" << std::hex << val << std::dec << ",";
2930 }
2931 val=static_cast<int>(input->readULong(2));
2932 if (val)
2933 f << "fileType1=" << std::hex << val << std::dec << ",";
2934 // increasing sequence
2935 f << "ids=[";
2936 for (int i=0; i<3; ++i) {
2937 val=static_cast<int>(input->readLong(4));
2938 if (!val) {
2939 f << "_,";
2940 continue;
2941 }
2942 setExpectedType(val-1, type1==0x1454857 ? F_name+i : F_sheetList+i);
2943 f << "F" << val-1 << ",";
2944 }
2945 f << "],";
2946 }
2947 else {
2948 f << "###fType=" << RagTime5Spreadsheet::printType(m_link.m_fileType[0]) << ",";
2949 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: the field type seems bad\n"));
2950 }
2951 return true;
2952 }
2953 default:
2954 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseDataZone: find unexpected file size\n"));
2955 f << "###fSz=" << fSz << ",";
2956 break;
2957 }
2958 if (!m_fieldName.empty())
2959 f << m_fieldName << ",";
2960 return true;
2961 }
2962 //! parse the header zone
2963 bool parseHeaderZone(MWAWInputStreamPtr &input, long fSz, int N, int flag, libmwaw::DebugStream &f)
2964 {
2965 f << "header, fl=" << std::hex << flag << std::dec << ",";
2966 m_fieldName="header";
2967 if (N!=-5 || m_dataId!=0 || fSz != 134) {
2968 f << "###N=" << N << ",fSz=" << fSz << ",";
2969 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseHeaderZone: find unexpected main field\n"));
2970 return true;
2971 }
2972 int val;
2973 for (int i=0; i<2; ++i) { // always 0?
2974 val=static_cast<int>(input->readLong(2));
2975 if (val) f << "f" << i+1 << "=" << val << ",";
2976 }
2977 val=static_cast<int>(input->readLong(2));
2978 f << "id=" << val << ",";
2979 val=static_cast<int>(input->readULong(2));
2980 if (m_type>0 && val!=m_type) {
2981 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseHeaderZone: unexpected zone type\n"));
2982 f << "##zoneType=" << std::hex << val << std::dec << ",";
2983 }
2984 val=int(input->readULong(4));
2985 if (val) {
2986 setExpectedType(val-1, F_parentList);
2987 f << "parent[list]=F" << val-1 << ",";
2988 }
2989 val=int(input->readULong(4));
2990 if (val) {
2991 setExpectedType(val-1, F_nextId);
2992 f << "next[id]=F" << val-1 << ",";
2993 }
2994 for (int i=0; i<5; ++i) {
2995 val=static_cast<int>(input->readULong(2));
2996 if (val)
2997 f << "f" << i+2 << "=" << val << ",";
2998 }
2999 for (int i=0; i<2; ++i) { // fl0=0|1, fl1=0|1
3000 val=static_cast<int>(input->readULong(1));
3001 if (val)
3002 f << "fl" << i << "=" << val << ",";
3003 }
3004 val=static_cast<int>(input->readULong(2)); // [02][08]0[12c]
3005 if (val&2)
3006 f << "cell[border,draw,hori],";
3007 if (val&4)
3008 f << "nogrid[hori],";
3009 if (val&8)
3010 f << "nogrid[vert],";
3011 if (val&0x40)
3012 f << "grid[print,hori],";
3013 if (val&0x20)
3014 f << "recalculate[demand],";
3015 if (val&0x80)
3016 f << "grid[print,vert],";
3017 if (val&0x400)
3018 f << "fixed[widths,heights]";
3019 if (val&0x8000)
3020 f << "space[between,para,sum],";
3021 val &= 0x7b11;
3022 if (val) f << "fl2=" << std::hex << val << std::dec << ",";
3023
3024 // first read dims, tree cells, ...
3025 int ids[6];
3026 for (int i=0; i<6; ++i) {
3027 val=static_cast<int>(input->readULong(4));
3028 ids[i]=val;
3029 if (!val) continue;
3030 char const *what[]= { "dims", "tree[cells]", "text[PLC]", "graph[PLC]", "root[unkn]", "refBlock"};
3031 f << what[i] << "=F" << val-1 << ",";
3032 }
3033
3034 std::vector<int> listIds;
3035 if (!RagTime5StructManager::readDataIdList(input, 2, listIds)) {
3036 f << "##field,";
3037 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseHeaderZone: can not read the field definitions\n"));
3038 return true;
3039 }
3040 else if (listIds[0] || listIds[1]) { // formuladef and formulapos
3041 m_sheet->m_formulaLink.m_type=RagTime5ClusterManager::Link::L_ClusterLink;
3042 m_sheet->m_formulaLink.m_ids=listIds;
3043 f << "formula[" << m_sheet->m_formulaLink << "],";
3044 }
3045 val=static_cast<int>(input->readULong(4));
3046 if (val) {
3047 setExpectedType(val-1, F_cellsUnion);
3048 f << "union[cells]=F" << val-1 << ",";
3049 }
3050 val=static_cast<int>(input->readULong(2)); // 10|110
3051 if (val) f << "fl3=" << std::hex << val << std::dec << ",";
3052 for (int i=0; i<2; ++i) { // 0
3053 val=static_cast<int>(input->readULong(2)); // 0
3054 if (val) f << "h" << i+2 << "=" << std::hex << val << std::dec << ",";
3055 }
3056 auto type=input->readULong(4);
3057 if (type!=0x34800)
3058 f << "#type1=" << RagTime5Spreadsheet::printType(type) << ",";
3059 for (int i=0; i<9; ++i) { // h10=32
3060 val=static_cast<int>(input->readLong(2));
3061 if (val) f << "h" << i+4 << "=" << val << ",";
3062 }
3063 val=static_cast<int>(input->readULong(2)); // always 1
3064 if (val!=1)
3065 f << "num[planes]=" << val << ",";
3066 val=static_cast<int>(input->readULong(4)); // always 1
3067 if (val!=1)
3068 f << "l1=" << val << ",";
3069 if (!RagTime5StructManager::readDataIdList(input, 1, listIds)) {
3070 f << "##text,";
3071 MWAW_DEBUG_MSG(("RagTime5SpreadsheetInternal::SpreadsheetCParser::parseHeaderZone: can not read the text zone\n"));
3072 return true;
3073 }
3074 else if (listIds[0]) { // text zone
3075 m_sheet->m_textboxZoneId=listIds[0];
3076 m_cluster->m_clusterIdsList.push_back(listIds[0]);
3077 f << "clusterId[text]=" << getClusterDebugName(listIds[0]) << ",";
3078 }
3079 for (int i=0; i<3; ++i) { // l1=0|7-9, l3=0-8-9
3080 val=static_cast<int>(input->readLong(4));
3081 if (!val) continue;
3082 if (i==0) {
3083 setExpectedType(val-1, F_childList);
3084 f << "childList=F" << val-1 << ",";
3085 }
3086 else if (i==2) {
3087 setExpectedType(val-1, F_refPos);
3088 f << "refPos=F" << val-1 << ",";
3089 }
3090 else
3091 f << "l" << i+2 << "=" << val << ",";
3092 }
3093 for (int i=0; i<6; ++i) { // l4=3|5, l5=0|1
3094 val=static_cast<int>(input->readLong(2));
3095 if (!val) continue;
3096 if (i==1)
3097 f << "num[title,vert]=" << val << ",";
3098 else if (i==2)
3099 f << "num[title,hori]=" << val << ",";
3100 else
3101 f << "l" << i+5 << "=" << val << ",";
3102 }
3103
3104 // time to mark the main ids type
3105 for (int i=5; i>=0; --i) {
3106 int id=ids[i];
3107 if (!id) continue;
3108 int const wh[]= { F_dims, F_cellsTree, F_textPLC, F_graphPLC, F_borderRoot, F_refBlock };
3109 setExpectedType(id-1, wh[i]);
3110 }
3111 return true;
3112 }
3113
3114 //! the current cluster
3115 std::shared_ptr<ClusterSpreadsheet> m_cluster;
3116 //! the sheet
3117 std::shared_ptr<Sheet> m_sheet;
3118 //! the actual field name
3119 std::string m_fieldName;
3120 //! the default plc values
3121 std::vector<int> m_defaultPLCValues;
3122 //! the list of row plc by planes
3123 std::map<MWAWVec2i,int> m_PLCNumRowByPlanesMap;
3124
3125 //! the expected id
3126 std::map<int,int> m_expectedIdToType;
3127 //! the id stack
3128 std::stack<int> m_idStack;
3129 private:
3130 //! copy constructor (not implemented)
3131 SpreadsheetCParser(SpreadsheetCParser const &orig) = delete;
3132 //! copy operator (not implemented)
3133 SpreadsheetCParser &operator=(SpreadsheetCParser const &orig) = delete;
3134 };
3135
3136 SpreadsheetCParser::~SpreadsheetCParser()
3137 {
3138 }
3139
3140 }
3141
3142 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Spreadsheet::readSpreadsheetCluster(RagTime5Zone &zone, int zoneType)
3143 {
3144 auto clusterManager=m_document.getClusterManager();
3145 if (!clusterManager) {
3146 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readSpreadsheetCluster: oops can not find the cluster manager\n"));
3147 return std::shared_ptr<RagTime5ClusterManager::Cluster>();
3148 }
3149 RagTime5SpreadsheetInternal::SpreadsheetCParser parser(*clusterManager, zoneType);
3150 if (!clusterManager->readCluster(zone, parser) || !parser.getSpreadsheetCluster() || !parser.getSpreadsheet()) {
3151 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readSpreadsheetCluster: oops can not find the cluster\n"));
3152 return std::shared_ptr<RagTime5ClusterManager::Cluster>();
3153 }
3154 auto cluster=parser.getSpreadsheetCluster();
3155 m_document.checkClusterList(cluster->m_clusterIdsList);
3156
3157 if (!cluster->m_dataLink.empty()) {
3158 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readSpreadsheetCluster: oops do not how to parse the main data\n"));
3159 }
3160 auto sheet=parser.getSpreadsheet();
3161 sheet->m_name=m_state->getNewSheetName(cluster->m_name);
3162
3163 // values
3164 if (!cluster->m_valuesLink.empty()) {
3165 RagTime5SpreadsheetInternal::ValuesParser valuesParser(*sheet);
3166 m_document.readListZone(cluster->m_valuesLink, valuesParser);
3167 }
3168 /* dimensions, cell blocks and finally the value tree
3169
3170 note: it is important to read the merged cell before the value list and other PLC*/
3171 for (int w=0; w<5; ++w) {
3172 RagTime5ClusterManager::Link link=w==0 ? cluster->m_dimensionLink :
3173 w==1 ? cluster->m_valuesTreeLink : cluster->m_blockLinks[w-2];
3174 if (link.m_ids.empty())
3175 continue;
3176 int cId=link.m_ids[0];
3177 std::shared_ptr<RagTime5Zone> dataZone=m_document.getDataZone(cId);
3178 if (!dataZone || !dataZone->m_entry.valid() ||
3179 dataZone->getKindLastPart(dataZone->m_kinds[1].empty())!="ItemData") {
3180 if (dataZone && dataZone->getKindLastPart()=="ItemData" && link.m_N==0)
3181 continue;
3182 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readSpreadsheetCluster: the %dth data zone %d seems bad\n", w, cId));
3183 continue;
3184 }
3185 if (w==0)
3186 readSheetDimensions(*sheet, *dataZone, link);
3187 else if (w==1)
3188 readValuesTree(*sheet, *dataZone, link, cluster->m_valuesTreeRoot, cluster->m_valuesMaxPos);
3189 else if (w==2 || w==3) // 2: formula block, 3: union block
3190 readCellBlocks(*sheet, *dataZone, link, w==3);
3191 else // 4: ref positions to value pos
3192 readCellRefPos(*sheet, *dataZone, link);
3193 }
3194
3195 // PLC
3196 // note: formula links can only be read after all spreadsheet are read
3197 for (int i=0; i<4; ++i) {
3198 auto link = i==0 ? cluster->m_graphPLCLink : i==1 ? cluster->m_textPLCLink : cluster->m_borderPLCLink[i-2];
3199 if (link.empty())
3200 continue;
3201 auto const &numRowByPlanes=i==0 ? cluster->m_graphPLCNumRowByPlanesMap : i==1 ? cluster->m_textPLCNumRowByPlanesMap :
3202 cluster->m_borderPLCNumRowByPlanesMap[i-2];
3203 RagTime5SpreadsheetInternal::CellPLCParser plcParser(*sheet, i, link.m_fieldSize, numRowByPlanes);
3204 m_document.readListZone(link, plcParser);
3205 }
3206
3207 // pictures list
3208 m_document.readChildList(cluster->m_childLink, sheet->m_childList);
3209 // parent zones: graphic or pipeline
3210 if (!cluster->m_parentLink.empty()) {
3211 RagTime5SpreadsheetInternal::ClustListParser linkParser(*clusterManager, 24, "SheetParentLst");
3212 m_document.readListZone(cluster->m_parentLink, linkParser);
3213 m_document.checkClusterList(linkParser.m_clusterList);
3214 }
3215
3216 if (!cluster->m_nameLink.empty()) {
3217 std::map<int, librevenge::RVNGString> idToStringMap;
3218 m_document.readUnicodeStringList(cluster->m_nameLink, idToStringMap);
3219 }
3220
3221 for (auto const &lnk : cluster->m_linksList) {
3222 if (lnk.m_type==RagTime5ClusterManager::Link::L_List) {
3223 m_document.readListZone(lnk);
3224 continue;
3225 }
3226 std::stringstream s;
3227 s << "Sheet_data" << lnk.m_fieldSize;
3228 RagTime5StructManager::DataParser defaultParser(lnk.m_name.empty() ? s.str() : lnk.m_name);
3229 m_document.readFixedSizeZone(lnk, defaultParser);
3230 }
3231
3232 if (m_state->m_idSheetMap.find(zone.m_ids[0])!=m_state->m_idSheetMap.end()) {
3233 MWAW_DEBUG_MSG(("RagTime5Spreadsheet::readSpreadsheetCluster: the sheet %d already exists\n",
3234 zone.m_ids[0]));
3235 }
3236 else
3237 m_state->m_idSheetMap[zone.m_ids[0]]=sheet;
3238 return cluster;
3239 }
3240
3241 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: