Debian Codegraph @

src/lib/Quattro9Spreadsheet.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2006, 2007 Andrew Ziem
11 * Copyright (C) 2004 Marc Maurer ([email protected])
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 *
14 * For minor contributions see the git repository.
15 *
16 * Alternatively, the contents of this file may be used under the terms
17 * of the GNU Lesser General Public License Version 2.1 or later
18 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
19 * applicable instead of those above.
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <algorithm>
26 #include <cctype>
27 #include <cmath>
28 #include <limits>
29 #include <set>
30 #include <sstream>
31 #include <stack>
32
33 #include <librevenge-stream/librevenge-stream.h>
34
35 #include "libwps_internal.h"
36 #include "libwps_tools_win.h"
37
38 #include "WPSCell.h"
39 #include "WKSContentListener.h"
40 #include "WPSEntry.h"
41 #include "WPSFont.h"
42 #include "WPSStream.h"
43 #include "WPSTable.h"
44
45 #include "QuattroFormula.h"
46 #include "Quattro9.h"
47
48 #include "Quattro9Spreadsheet.h"
49
50 namespace Quattro9SpreadsheetInternal
51 {
52 //! a class used to store a style of a cell in Quattro9Spreadsheet
53 struct Style final : public WPSCellFormat
54 {
55 //! construtor
56 explicit Style()
57 : WPSCellFormat()
58 , m_fileFormat(-1)
59 , m_alignAcrossColumn(false)
60 , m_extra("")
61 {
62 }
63 Style(Style const &) = default;
64 //! destructor
65 ~Style() final;
66 //! operator<<
67 friend std::ostream &operator<<(std::ostream &o, Style const &style);
68 //! operator==
69 bool operator==(Style const &st) const;
70 //! operator!=
71 bool operator!=(Style const &st) const
72 {
73 return !(*this==st);
74 }
75 //! the file format
76 int m_fileFormat;
77 //! flag to know if we must align across column
78 bool m_alignAcrossColumn;
79 /** extra data */
80 std::string m_extra;
81 };
82
83 Style::~Style()
84 {
85 }
86
87 //! operator<<
88 std::ostream &operator<<(std::ostream &o, Style const &style)
89 {
90 o << static_cast<WPSCellFormat const &>(style) << ",";
91 if (style.m_fileFormat!=0xFF)
92 o << "format=" << std::hex << style.m_fileFormat << std::dec << ",";
93 if (style.m_extra.length())
94 o << "extra=[" << style.m_extra << "],";
95
96 return o;
97 }
98
99 bool Style::operator==(Style const &st) const
100 {
101 if (m_fileFormat!=st.m_fileFormat) return false;
102 int diff = WPSCellFormat::compare(st);
103 if (diff) return false;
104 return m_alignAcrossColumn==st.m_alignAcrossColumn && m_extra==st.m_extra;
105 }
106
107 //! a cellule of a Quattro spreadsheet
108 class Cell final : public WPSCell
109 {
110 public:
111 /// constructor
112 explicit Cell()
113 { }
114
115 //! update the cell format using file format
116 void updateFormat(int fileFormat)
117 {
118 if (fileFormat<0)
119 return;
120 switch (fileFormat>>5)
121 {
122 case 0:
123 switch (fileFormat&0x1f)
124 {
125 case 1: // checkme: +/- : kind of bool
126 setFormat(F_BOOLEAN);
127 break;
128 case 2: // generic
129 break;
130 case 3:
131 setFormat(F_TEXT);
132 break;
133 case 4: // hidden
134 {
135 auto font=getFont();
136 font.m_attributes |= WPS_HIDDEN_BIT;
137 setFont(font);
138 break;
139 }
140 case 5:
141 setDTFormat(F_DATE, "%d-%b-%y");
142 break;
143 case 6:
144 setDTFormat(F_DATE, "%d %b");
145 break;
146 case 7:
147 setDTFormat(F_DATE, "%b-%y");
148 break;
149 case 8:
150 setDTFormat(F_DATE, "%A %d %B %Y");
151 break;
152 case 9:
153 setDTFormat(F_DATE, "%m/%d/%Y");
154 break;
155 case 0xa:
156 setDTFormat(F_TIME, "%I:%M:%S%p");
157 break;
158 case 0xb:
159 setDTFormat(F_TIME, "%I:%M%p");
160 break;
161 case 0xc:
162 setDTFormat(F_TIME, "%H:%M:%S");
163 break;
164 case 0xd:
165 setDTFormat(F_TIME, "%H:%M");
166 break;
167 case 0xe:
168 setDTFormat(F_DATE, "%d-%b-%Y");
169 break;
170 case 0xf:
171 setDTFormat(F_DATE, "%b-%Y");
172 break;
173 default:
174 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Cell::send: unknown format %x\n", fileFormat));
175 break;
176 }
177 break;
178 case 1: // basic, neg red
179 case 2: // neg (), neg red ()
180 case 3: // basic+thousand, neg red
181 case 4:
182 setFormat(F_NUMBER, 1);
183 setDigits(fileFormat&0xF);
184 break;
185 case 5: // currency (see previous)
186 case 6:
187 case 7:
188 case 8:
189 setFormat(F_NUMBER, 4);
190 setDigits(fileFormat&0xF);
191 break;
192 case 9: // percent, scientific
193 setFormat(F_NUMBER, (fileFormat&0x10) ? 2 : 3);
194 setDigits(fileFormat&0xF);
195 break;
196 case 0xa: // fraction (note fileFormat&0x1f seems to define the format of the denominator)
197 setFormat(F_NUMBER, 7);
198 break;
199 case 0xb:
200 {
201 // id=(fileFormat>>4)-0x16
202 static bool first=true;
203 if (first)
204 {
205 first=false;
206 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Cell::updateFormat: user defined format is not supported\n"));
207 }
208 break;
209 }
210 default:
211 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Cell::send: unknown format %x\n", fileFormat));
212 break;
213 }
214 }
215 //! call when a cell must be send
216 bool send(WPSListenerPtr &/*listener*/) final;
217
218 //! call when the content of a cell must be send
219 bool sendContent(WPSListenerPtr &/*listener*/) final
220 {
221 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Cell::sendContent: must not be called\n"));
222 return false;
223 }
224 };
225 bool Cell::send(WPSListenerPtr &/*listener*/)
226 {
227 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Cell::send: must not be called\n"));
228 return false;
229 }
230
231
232 //! a class used to store the cell(s) content of a Quattro Spreadsheet
233 struct CellData
234 {
235 //! constructor
236 CellData()
237 : m_type(0)
238 , m_rows()
239 , m_span(1,1)
240 , m_style(-1)
241 , m_intList()
242 , m_doubleList()
243 , m_flagList()
244 {
245 }
246 //! returns true if the cell contains no data (and is not a merged cell)
247 bool empty() const
248 {
249 return (m_type&0x1f)==1 && m_span==Vec2i(1,1);
250 }
251 //! returns the double value corresponding to a row
252 double getDouble(int row) const
253 {
254 if (m_doubleList.empty())
255 {
256 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getDouble: no int value\n"));
257 return 0;
258 }
259 if (row<m_rows[0]||row>m_rows[1])
260 {
261 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getDouble: called with bad row=%d\n", row));
262 return 0;
263 }
264 switch ((m_type>>5)&3)
265 {
266 case 0:
267 return m_doubleList[0];
268 case 2:
269 if (row-m_rows[0]>=int(m_doubleList.size()))
270 {
271 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getDouble: oops, can not find row=%d\n", row));
272 return 0;
273 }
274 return m_doubleList[size_t(row-m_rows[0])];
275 case 3:
276 if (m_doubleList.size()!=2)
277 {
278 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getDouble: oops, unexpected data size\n"));
279 return 0;
280 }
281 return m_doubleList[0]+(row-m_rows[0])*m_doubleList[1];
282 default:
283 break;
284 }
285 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getDouble: oops, unexpected type\n"));
286 return 0;
287 }
288 //! returns the flag value corresponding to a row
289 int getFlag(int row) const
290 {
291 if (m_flagList.empty())
292 {
293 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getFlag: no flag value\n"));
294 return 0;
295 }
296 if (row<m_rows[0]||row>m_rows[1])
297 {
298 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getFlag: called with bad row=%d\n", row));
299 return 0;
300 }
301 switch ((m_type>>5)&3)
302 {
303 case 0:
304 return m_flagList[0];
305 case 2:
306 if (row-m_rows[0]>=int(m_flagList.size()))
307 {
308 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getFlag: oops, can not find row=%d\n", row));
309 return 0;
310 }
311 return m_flagList[size_t(row-m_rows[0])];
312 case 3:
313 if (m_flagList.size()!=2)
314 {
315 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getFlag: oops, unexpected data size\n"));
316 return 0;
317 }
318 return m_flagList[0]+(row-m_rows[0])*m_flagList[1];
319 default:
320 break;
321 }
322 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getFlag: oops, unexpected type\n"));
323 return 0;
324 }
325 //! returns the int value corresponding to a row
326 int getInt(int row) const
327 {
328 if (m_intList.empty())
329 {
330 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getInt: no int value\n"));
331 return 0;
332 }
333 if (row<m_rows[0]||row>m_rows[1])
334 {
335 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getInt: called with bad row=%d\n", row));
336 return 0;
337 }
338 switch ((m_type>>5)&3)
339 {
340 case 0:
341 return m_intList[0];
342 case 2:
343 if (row-m_rows[0]>=int(m_intList.size()))
344 {
345 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getInt: oops, can not find row=%d\n", row));
346 return 0;
347 }
348 return m_intList[size_t(row-m_rows[0])];
349 case 3:
350 if (m_intList.size()!=2)
351 {
352 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getInt: oops, unexpected data size\n"));
353 return 0;
354 }
355 return m_intList[0]+(row-m_rows[0])*m_intList[1];
356 default:
357 break;
358 }
359 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getInt: oops, unexpected type\n"));
360 return 0;
361 }
362 //! the cell type
363 int m_type;
364 //! the min/max row
365 Vec2i m_rows;
366 //! the column row/span
367 Vec2i m_span;
368 //! the style id
369 int m_style;
370 //! the list of int values
371 std::vector<int> m_intList;
372 //! the list of double values
373 std::vector<double> m_doubleList;
374 //! a list of flag (for formula)
375 std::vector<int> m_flagList;
376 };
377
378 //! Internal: a list of cell and result of Quattro9SpreadsheetInternal
379 struct Column
380 {
381 //! constructor
382 Column()
383 : m_rowsToCellMap()
384 , m_rowToCellResMap()
385 {
386 }
387 //! add a cell/list of cells data
388 void add(Vec2i limits, CellData const &cell)
389 {
390 auto rIt=m_rowsToCellMap.lower_bound(Vec2i(-1,limits[0]));
391 while (rIt!=m_rowsToCellMap.end())
392 {
393 auto const &cells=rIt->first;
394 if (cells[0]>limits[1]) break;
395 if (cells[1]>=limits[0])
396 {
397 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::column::addCells: oops, some rows are already set in %dx%d\n", limits[0], limits[1]));
398 return;
399 }
400 ++rIt;
401 }
402 m_rowsToCellMap[limits]=cell;
403 }
404 //! add a cell result
405 void add(int row, std::shared_ptr<WPSStream> const &stream, Quattro9ParserInternal::TextEntry const &entry)
406 {
407 if (m_rowToCellResMap.find(row)!=m_rowToCellResMap.end())
408 {
409 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::column::addCells: oops, a result exists for row=%d\n", row));
410 return;
411 }
412 m_rowToCellResMap[row]=std::make_pair(stream,entry);
413 }
414 //! update a list of row (needed to know what row must be sent)
415 void updateListOfRows(std::set<int> &rows) const
416 {
417 for (auto const &it : m_rowsToCellMap)
418 {
419 rows.insert(it.first[0]);
420 if (it.second.m_span[1]>1)
421 {
422 rows.insert(it.first[0]+1);
423 rows.insert(it.first[0]+it.second.m_span[1]);
424 continue;
425 }
426 rows.insert(it.first[1]+1);
427 if ((it.second.m_type&0x1f)==1) continue;
428 for (int r=it.first[0]+1; r<=it.first[1]; ++r) rows.insert(r);
429 }
430 }
431 //! returns the cell corresponding to a row (if it exist)
432 CellData const *getCell(int row) const
433 {
434 auto rIt=m_rowsToCellMap.lower_bound(Vec2i(-1,row));
435 if (rIt!=m_rowsToCellMap.end() && rIt->first[1]<row) ++rIt;
436 if (rIt!=m_rowsToCellMap.end() && rIt->first[0]<=row && rIt->first[1]>=row)
437 return &rIt->second;
438 return nullptr;
439 }
440 //! a map rows to cell data
441 std::map<Vec2i, CellData> m_rowsToCellMap;
442 //! a map row to cell result
443 std::map<int, std::pair<std::shared_ptr<WPSStream>, Quattro9ParserInternal::TextEntry> > m_rowToCellResMap;
444 };
445
446 //! the spreadsheet of a Quattro Spreadsheet
447 class Spreadsheet
448 {
449 public:
450 //! a constructor
451 explicit Spreadsheet(int id)
452 : m_id(id)
453 , m_defaultSizes(1080,260)
454 , m_mergedCellList()
455 , m_colToColumnMap()
456 , m_invalidColumn()
457 {
458 }
459 //! update the spreadsheet: check for merged cell, ...
460 void update()
461 {
462 for (auto const &box : m_mergedCellList)
463 {
464 int const minRow=box[0][1];
465 int const maxRow=box[1][1];
466 for (int col=box[0][0]; col<=box[1][0]; ++col)
467 {
468 auto cIt=m_colToColumnMap.find(col);
469 bool const firstCol=col==box[0][0];
470 if (cIt==m_colToColumnMap.end())
471 {
472 if (firstCol) // we must create it
473 {
474 auto &column=getColumn(col);
475 CellData cell;
476 cell.m_type=1; // no data
477 cell.m_rows=Vec2i(minRow, minRow);
478 cell.m_span=Vec2i(box.size())+Vec2i(1,1);
479 column.add(cell.m_rows, cell);
480 }
481 continue;
482 }
483 std::vector<CellData> listCellToAdd;
484 auto &column=getColumn(col);
485 auto rIt=column.m_rowsToCellMap.lower_bound(Vec2i(-1,minRow));
486 if (rIt!=column.m_rowsToCellMap.end() && rIt->first[1]<minRow) ++rIt;
487 if (rIt!=column.m_rowsToCellMap.end() && rIt->first[0]<minRow && rIt->first[1]>=minRow)
488 {
489 // we need to split the first data
490 auto rows=rIt->first;
491 auto cell=rIt->second;
492 ++rIt;
493 column.m_rowsToCellMap.erase(rows);
494 cell.m_rows[1]=minRow-1;
495 listCellToAdd.push_back(cell);
496 if (firstCol)
497 {
498 cell.m_rows=Vec2i(minRow, minRow);
499 cell.m_span=Vec2i(box.size())+Vec2i(1,1);
500 listCellToAdd.push_back(cell);
501 }
502 }
503 else if (firstCol)
504 {
505 if (rIt!=column.m_rowsToCellMap.end() && rIt->first[0]==minRow)
506 {
507 // the first cell exist, update span
508 auto cell=rIt->second;
509 cell.m_rows=Vec2i(minRow, minRow);
510 cell.m_span=Vec2i(box.size())+Vec2i(1,1);
511 listCellToAdd.push_back(cell);
512 }
513 else
514 {
515 // create a new cell
516 CellData cell;
517 cell.m_type=1; // no data
518 cell.m_rows=Vec2i(minRow, minRow);
519 cell.m_span=Vec2i(box.size())+Vec2i(1,1);
520 column.add(cell.m_rows, cell);
521 }
522 }
523 while (rIt!=column.m_rowsToCellMap.end() && rIt->first[1]<=maxRow)
524 {
525 // remove cells between minRow and maxRow
526 auto rows=rIt->first;
527 ++rIt;
528 column.m_rowsToCellMap.erase(rows);
529 }
530 if (rIt!=column.m_rowsToCellMap.end() && rIt->first[0]<=maxRow)
531 {
532 // we need to split the last data
533 auto rows=rIt->first;
534 auto cell=rIt->second;
535 column.m_rowsToCellMap.erase(rows);
536 cell.m_rows[0]=maxRow+1;
537 listCellToAdd.push_back(cell);
538 }
539 for (auto const &cell: listCellToAdd) column.add(cell.m_rows, cell);
540 }
541 }
542 }
543 //! returns the row size in point
544 WPSRowFormat getRowHeight(int row) const
545 {
546 auto rIt=m_sizesMap[1].lower_bound(Vec2i(-1,row));
547 if (rIt!=m_sizesMap[1].end() && rIt->first[0]<=row && rIt->first[1]>=row)
548 return WPSRowFormat(float(rIt->second)/20.f);
549 return WPSRowFormat(float(m_defaultSizes[1])/20.f);
550 }
551 //! return the columns format
552 std::vector<WPSColumnFormat> getWidths() const
553 {
554 std::vector<WPSColumnFormat> widths;
555 // g++-mp-7 does not like float(m_defaultSizes[0])/20.f), but this->m_default is ok...
556 WPSColumnFormat defWidth(float(this->m_defaultSizes[0])/20.f);
557 defWidth.m_useOptimalWidth=true;
558 int prevRow=-1;
559 for (auto const &it : m_sizesMap[0])
560 {
561 if (it.first[0]<prevRow+1)
562 {
563 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Spreadsheet::getWidths: oops, some limits are bad\n"));
564 continue;
565 }
566 if (it.first[0]>prevRow+1)
567 {
568 defWidth.m_numRepeat=it.first[0]-(prevRow+1);
569 widths.push_back(defWidth);
570 }
571 WPSColumnFormat width(float(it.second)/20.f);
572 width.m_numRepeat=(it.first[1]+1-it.first[0]);
573 widths.push_back(width);
574 prevRow=it.first[1];
575 }
576 if (widths.empty())
577 {
578 defWidth.m_numRepeat=256;
579 widths.push_back(defWidth);
580 }
581 return widths;
582 }
583 //! set the col/row size in TWIP
584 void setColRowSize(int which, int pos, int w)
585 {
586 if (which!=0 && which!=1)
587 {
588 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Spreadsheet::setColRowSize: oops, which=%d is bad\n", which));
589 return;
590 }
591 auto rIt=m_sizesMap[which].lower_bound(Vec2i(-1,pos));
592 if (rIt!=m_sizesMap[which].end() && rIt->first[0]<=pos && rIt->first[1]>=pos)
593 {
594 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Spreadsheet::setColRowSize: oops, pos %d is already set\n", pos));
595 return;
596 }
597 if (w>=0)
598 m_sizesMap[which][Vec2i(pos,pos)]=w;
599 }
600 //! set the col/row size in TWIP
601 void setColRowSizes(int which, int minPos, int maxPos, int w)
602 {
603 if (which!=0 && which!=1)
604 {
605 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Spreadsheet::setColRowSizes: oops, which=%d is bad\n", which));
606 return;
607 }
608 auto rIt=m_sizesMap[which].lower_bound(Vec2i(-1,minPos));
609 while (rIt!=m_sizesMap[which].end())
610 {
611 auto const &cells=rIt->first;
612 if (cells[0]>maxPos) break;
613 if (cells[1]>=minPos)
614 {
615 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Spreadsheet::setColRowSizes: oops, some rows are already set in %dx%d\n", minPos, maxPos));
616 return;
617 }
618 ++rIt;
619 }
620 if (w>=0)
621 m_sizesMap[which][Vec2i(minPos,maxPos)]=w;
622 }
623 //! returns the cell position
624 Vec2f getPosition(Vec2i const &cell) const
625 {
626 Vec2f res(0,0);
627 for (int which=0; which<2; ++which)
628 {
629 int prevRow=-1;
630 int width=0;
631 for (auto const &it : m_sizesMap[0])
632 {
633 if (it.first[0]<prevRow+1) continue;
634 if (it.first[0]>prevRow+1)
635 {
636 if (it.first[0]>cell[which])
637 {
638 width+=(cell[which]-(prevRow+1))*m_defaultSizes[which];
639 prevRow=cell[which];
640 break;
641 }
642 width+=(it.first[0]-(prevRow+1))*m_defaultSizes[which];
643 }
644 if (it.first[1]>cell[which])
645 {
646 width+=(cell[which]-it.first[0])*it.second;
647 prevRow=cell[which];
648 break;
649 }
650 width+=(it.first[1]+1-it.first[0])*it.second;
651 prevRow=it.first[1];
652 }
653 if (cell[which]>prevRow+1)
654 width+=(cell[which]-(prevRow+1))*m_defaultSizes[which];
655 res[which]=float(width)/20.f;
656 }
657 return res;
658 }
659 //! returns a ref to column data (or to invalidColumn if called with bad column)
660 Column &getColumn(int col)
661 {
662 auto it=m_colToColumnMap.find(col);
663 if (it!=m_colToColumnMap.end()) return it->second;
664 if (col<0 || col>m_defaultSizes[0])
665 {
666 WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Spreadsheet::getColumn: called with invalid col=%d\n", col));
667 return m_invalidColumn;
668 }
669 m_colToColumnMap[col]=Column();
670 return m_colToColumnMap.find(col)->second;
671 }
672 //! returns the list of rows which need to be opened, ...
673 std::set<int> getListSendRow() const
674 {
675 std::set<int> rows;
676 rows.insert(0);
677 for (auto const &it : m_colToColumnMap) it.second.updateListOfRows(rows);
678 for (auto const &it : m_sizesMap[1])
679 {
680 rows.insert(it.first[0]);
681 rows.insert(it.first[1]+1);
682 }
683 for (auto const &box : m_mergedCellList)
684 {
685 rows.insert(box[0][1]);
686 rows.insert(box[1][1]+1);
687 }
688 return rows;
689 }
690 //! returns the list of columns in which a row cell exists
691 std::vector<Vec2i> getListCellsInARow(int row) const
692 {
693 std::vector<Vec2i> cols;
694 CellData const *prevCell=nullptr;
695 int prevCol=-1, numRepeated=0;
696 for (auto const &it : m_colToColumnMap)
697 {
698 auto newCell=it.second.getCell(row);
699 if (it.first==prevCol+numRepeated && prevCell && newCell &&
700 prevCell->empty() && newCell->empty() && prevCell->m_style==newCell->m_style)
701 {
702 numRepeated++;
703 continue;
704 }
705 if (prevCell) cols.push_back(Vec2i(prevCol,prevCol+numRepeated-1));
706 prevCol=it.first;
707 prevCell=newCell;
708 numRepeated=1;
709 }
710 if (prevCell) cols.push_back(Vec2i(prevCol,prevCol+numRepeated-1));
711 return cols;
712 }
713 //! returns the cell corresponding to a position (if it exist)
714 CellData const *getCell(Vec2i const &pos) const
715 {
716 auto rIt=m_colToColumnMap.find(pos[0]);
717 if (rIt==m_colToColumnMap.end()) return nullptr;
718 return rIt->second.getCell(pos[1]);
719 }
720 //! the spreadsheet id
721 int m_id;
722
723 /** the default col/row size in TWIP */
724 Vec2i m_defaultSizes;
725 /** the map Vec2i(min, max) to col/row size in TWIP */
726 std::map<Vec2i,int> m_sizesMap[2];
727 /** the merge cells */
728 std::vector<WPSBox2i> m_mergedCellList;
729 /** a map col to column data */
730 std::map<int, Column> m_colToColumnMap;
731 private:
732 /** a extra column used to return an invalid column */
733 Column m_invalidColumn;
734 };
735
736 //! the state of Quattro9Spreadsheet
737 struct State
738 {
739 //! constructor
740 explicit State(QuattroFormulaManager::CellReferenceFunction const &readCellReference)
741 : m_version(-1)
742 , m_documentStrings()
743 , m_documentFormulas()
744 , m_formulaManager(readCellReference, 2)
745 , m_actualSpreadsheet()
746 , m_actualColumn(-1)
747 , m_stylesList()
748 , m_spreadsheetMap()
749 , m_idToSheetNameMap()
750 , m_idToUserFormatMap()
751 {
752 }
753 //! returns the ith spreadsheet
754 librevenge::RVNGString getSheetName(int id) const
755 {
756 auto it = m_idToSheetNameMap.find(id);
757 if (it!=m_idToSheetNameMap.end() && !it->second.empty())
758 return it->second;
759 librevenge::RVNGString name;
760 name.sprintf("Sheet%d", id+1);
761 return name;
762 }
763 //! the file version
764 int m_version;
765 //! the document strings
766 std::pair<std::shared_ptr<WPSStream>, std::vector<Quattro9ParserInternal::TextEntry> > m_documentStrings;
767 //! the document formulas
768 std::pair<std::shared_ptr<WPSStream>, std::vector<WPSEntry> > m_documentFormulas;
769 //! the formula manager
770 QuattroFormulaManager m_formulaManager;
771 //! the actual sheet
772 std::shared_ptr<Spreadsheet> m_actualSpreadsheet;
773 //! the actual column
774 int m_actualColumn;
775 //! the list of styles
776 std::vector<Style> m_stylesList;
777 //! the map of spreadsheet
778 std::map<int, std::shared_ptr<Spreadsheet> > m_spreadsheetMap;
779 //! the map id to sheet's name
780 std::map<int, librevenge::RVNGString> m_idToSheetNameMap;
781 //! map id to user format string
782 std::map<int, librevenge::RVNGString> m_idToUserFormatMap;
783 };
784
785 }
786
787 // constructor, destructor
788 Quattro9Spreadsheet::Quattro9Spreadsheet(Quattro9Parser &parser)
789 : m_listener()
790 , m_mainParser(parser)
791 , m_state()
792 {
793 m_state.reset(new Quattro9SpreadsheetInternal::State(getReadCellReferenceFunction()));
794 }
795
796 Quattro9Spreadsheet::~Quattro9Spreadsheet()
797 {
798 }
799
800 void Quattro9Spreadsheet::cleanState()
801 {
802 m_state.reset(new Quattro9SpreadsheetInternal::State(getReadCellReferenceFunction()));
803 }
804
805 void Quattro9Spreadsheet::updateState()
806 {
807 }
808
809 int Quattro9Spreadsheet::version() const
810 {
811 if (m_state->m_version<0)
812 m_state->m_version=m_mainParser.version();
813 return m_state->m_version;
814 }
815
816 QuattroFormulaManager::CellReferenceFunction Quattro9Spreadsheet::getReadCellReferenceFunction()
817 {
818 return [this](std::shared_ptr<WPSStream> const &stream, long endPos,
819 QuattroFormulaInternal::CellReference &ref,
820 Vec2i const &pos, int sheetId)
821 {
822 return this->readCellReference(stream, endPos, ref, pos, sheetId);
823 };
824 }
825
826 int Quattro9Spreadsheet::getNumSpreadsheets() const
827 {
828 if (m_state->m_spreadsheetMap.empty())
829 return 0;
830 auto it=m_state->m_spreadsheetMap.end();
831 --it;
832 return it->first+1;
833 }
834
835 Vec2f Quattro9Spreadsheet::getPosition(int sheetId, Vec2i const &cell) const
836 {
837 auto it=m_state->m_spreadsheetMap.find(sheetId);
838 if (it==m_state->m_spreadsheetMap.end() || !it->second)
839 {
840 WPS_DEBUG_MSG(("Quattro9Spreadsheet::getPosition: can not find the sheet %d\n", sheetId));
841 return Vec2f(float(cell[0]*50), float(cell[1]*13));
842 }
843 return it->second->getPosition(cell);
844 }
845
846 librevenge::RVNGString Quattro9Spreadsheet::getSheetName(int id) const
847 {
848 return m_state->getSheetName(id);
849 }
850
851 void Quattro9Spreadsheet::addDLLIdName(int id, librevenge::RVNGString const &name, bool func1)
852 {
853 m_state->m_formulaManager.addDLLIdName(id, name, func1);
854 }
855
856 void Quattro9Spreadsheet::addUserFormat(int id, librevenge::RVNGString const &name)
857 {
858 if (name.empty())
859 {
860 WPS_DEBUG_MSG(("Quattro9Spreadsheet::addUserFormat: called with empty name for id=%d\n", id));
861 return;
862 }
863 if (m_state->m_idToUserFormatMap.find(id)!=m_state->m_idToUserFormatMap.end())
864 {
865 WPS_DEBUG_MSG(("Quattro9Spreadsheet::addUserFormat: called with dupplicated id=%d\n", id));
866 }
867 else
868 m_state->m_idToUserFormatMap[id]=name;
869 }
870
871 void Quattro9Spreadsheet::addDocumentStrings(std::shared_ptr<WPSStream> const &stream,
872 std::vector<Quattro9ParserInternal::TextEntry> const &entries)
873 {
874 if (!m_state->m_documentStrings.second.empty())
875 {
876 WPS_DEBUG_MSG(("Quattro9Spreadsheet::addDocumentStrings: the entries list is not empty\n"));
877 }
878 else
879 {
880 m_state->m_documentStrings.first=stream;
881 m_state->m_documentStrings.second=entries;
882 }
883 }
884
885 ////////////////////////////////////////////////////////////
886 // low level
887
888 ////////////////////////////////////////////////////////////
889 // parse sheet data: file zones
890 ////////////////////////////////////////////////////////////
891 bool Quattro9Spreadsheet::readCellStyles(std::shared_ptr<WPSStream> const &stream)
892 {
893 int const vers=version();
894 RVNGInputStreamPtr input = stream->m_input;
895 libwps::DebugFile &ascFile=stream->m_ascii;
896 libwps::DebugStream f;
897
898 long pos = input->tell();
899 auto type = long(libwps::readU16(input));
900 bool const bigBlock=type&0x8000;
901 if ((type&0x7fff) !=0xa)
902 {
903 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellStyles: not a cell property\n"));
904 return false;
905 }
906 long sz = bigBlock ? long(libwps::readU32(input)) : long(libwps::readU16(input));
907 long N= long(libwps::readU32(input));
908 f << "N=" << N << ",";
909 // qpw9[v=2001]: sz=30, qpwX2[v=2013] and qpwX9[v=2020]: sz=36, we can assume that sz=36 for X3..X8
910 int dataSz= vers<2012 ? 30 : 36;
911 if (N>=0 && sz>=4 && (sz-4)/dataSz!=N)
912 {
913 if (vers==2001 || (vers>=2013 && vers<=2020)) // we are "sure" of the data size, try to modify N
914 {
915 // this situation can happen when QP9 writes data in consecutive blocks(when needed), in this case, N is bad
916 if (sz%dataSz==4)
917 {
918 N=(sz-4)/dataSz;
919 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellStyles: reset N to %ld\n", N));
920 }
921 }
922 else // the data size is unknown, try to modify the data size
923 {
924 dataSz=int((sz-4)/N);
925 if (dataSz>=30)
926 {
927 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellStyles: reset data size to %d\n", dataSz));
928 }
929 }
930 }
931 if (N<0 || sz<4 || dataSz<30 || (sz-4)/dataSz!=N)
932 {
933 f << "###";
934 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellStyles: not a cell property\n"));
935 ascFile.addPos(pos);
936 ascFile.addNote(f.str().c_str());
937 return false;
938 }
939 ascFile.addPos(pos);
940 ascFile.addNote(f.str().c_str());
941 long actSize=long(m_state->m_stylesList.size());
942 m_state->m_stylesList.resize(size_t(actSize+N));
943 for (long i=actSize; i<actSize+N; ++i)
944 {
945 pos=input->tell();
946 f.str("");
947 f << "Cell[style-Ce" << i << "]:";
948 auto &style=m_state->m_stylesList[size_t(i)];
949 auto fId=int(libwps::readU16(input));
950 WPSFont font;
951 if (fId)
952 {
953 if (!m_mainParser.getFont(fId-1, font))
954 f << "###";
955 f << "F" << fId-1 << ",";
956 }
957 style.m_fileFormat=int(libwps::readU16(input));
958 f << "form=" << std::hex << style.m_fileFormat << std::dec << ",";
959 int flag=int(libwps::readU16(input));
960 switch (flag&7)
961 {
962 case 1:
963 style.setHAlignment(WPSCellFormat::HALIGN_LEFT);
964 f << "left,";
965 break;
966 case 2:
967 style.setHAlignment(WPSCellFormat::HALIGN_CENTER);
968 f << "center,";
969 break;
970 case 3:
971 style.setHAlignment(WPSCellFormat::HALIGN_RIGHT);
972 f << "right,";
973 break;
974 case 4:
975 style.setHAlignment(WPSCellFormat::HALIGN_FULL);
976 f << "block,";
977 break;
978 case 6: // useme
979 {
980 style.setHAlignment(WPSCellFormat::HALIGN_CENTER);
981 style.m_alignAcrossColumn=true;
982 f << "center[across],";
983 break;
984 }
985 case 7: // useme
986 f << "ident,";
987 break;
988 default:
989 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellStyles: find unexpected alignment\n"));
990 f << "###align=" << (flag&7) << ",";
991 break;
992 case 0: // standart
993 break;
994 }
995 switch ((flag>>4)&0x3)
996 {
997 case 0: // default
998 style.setVAlignment(WPSCellFormat::VALIGN_BOTTOM);
999 break;
1000 case 1:
1001 style.setVAlignment(WPSCellFormat::VALIGN_CENTER);
1002 f << "vAlign=center,";
1003 break;
1004 case 2:
1005 style.setVAlignment(WPSCellFormat::VALIGN_TOP);
1006 f << "vAlign=top,";
1007 break;
1008 default:
1009 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellStyle: find unexpected alignment\n"));
1010 f << "###valign=3,";
1011 break;
1012 }
1013 if (flag&0x80)
1014 {
1015 style.setTextRotation(270);
1016 f << "top[down],";
1017 }
1018 // flag&0x100 // maybe related to rotation
1019 if (flag&0x400)
1020 {
1021 style.setWrapping(WPSCellFormat::WRAP_WRAP);
1022 f << "wrap,";
1023 }
1024 flag &= 0xfb08;
1025 if (flag) f << "fl=" << std::hex << flag << std::dec << ",";
1026 int val=int(libwps::readU16(input));
1027 if (val&1) f << "not[protected],";
1028 val&=0xfffe;
1029 if (val) f << "f1=" << std::hex << val << std::dec << ",";
1030 val=int(libwps::readU16(input)); // useme
1031 if (val) f << "style[id]=" << val << ",";
1032 val=int(libwps::readU16(input));
1033 WPSColor color;
1034 if (!m_mainParser.getColor(val, color))
1035 f << "##color[font]=" << val << ",";
1036 else if (!color.isBlack())
1037 {
1038 font.m_color=color;
1039 f << "color[font]=" << color << ",";
1040 }
1041 style.setFont(font);
1042 val=int(libwps::readU8(input));
1043 if (val)
1044 {
1045 f << "rot=" << val << ",";
1046 style.setTextRotation(val);
1047 }
1048 val=int(libwps::readU16(input));
1049 if (val!=0x50) // 80=US, 82=Pt
1050 {
1051 f << "lang=" << val << ",";
1052 }
1053 int borderTypes[5];
1054 for (auto &t : borderTypes) t=int(libwps::readU8(input));
1055 int borderColors[5];
1056 for (auto &c : borderColors) c=int(libwps::readU8(input));
1057 WPSColor surfColors[2]= {WPSColor::white(), WPSColor::black()};
1058 for (int s=0; s<2; s++)
1059 {
1060 int c=int(libwps::readU8(input));
1061 if (!m_mainParser.getColor(c, color))
1062 {
1063 f << "###col" << s << "=" << c << ",";
1064 continue;
1065 }
1066 surfColors[s]=color;
1067 if ((s==0 && !color.isWhite()) || (s==1 && !color.isBlack()))
1068 f << "col" << s << "=" << color << ",";
1069 }
1070 int patId=int(libwps::readU8(input));
1071
1072 auto flags=libwps::readU8(input);
1073 // unsure, these flags seem set when a file is modified but
1074 // not when a file is imported => probably safe to ignore
1075
1076 //if (flags&1) f << "set[align],";
1077 //if (flags&2) f << "set[format],";
1078 //if (flags&4) f << "set[align2],";
1079 //if (flags&8) f << "set[style],";
1080 //if (flags&0x10) f << "set[font],";
1081 //if (flags&0x20) f << "set[border],";
1082 //if (flags&0x40) f << "set[protection],";
1083 if (flags&0x80) f << "fl2[80],";
1084 val=int(libwps::readU8(input)); // 0,1,ff
1085 if (val) f << "g0=" << val << ",";
1086
1087 // time to write the border and the background
1088 for (int b=0; b<5; ++b)
1089 {
1090 if (!borderTypes[b]) continue;
1091 WPSBorder border;
1092 char const *wh[]= {"bordL","bordT","bordR","bordB","bordall"/* or last, checkme*/};
1093 switch (borderTypes[b])
1094 {
1095 case 1: // normal
1096 f << wh[b] << ",";
1097 break;
1098 case 2:
1099 border.m_type=WPSBorder::Double;
1100 f << wh[b] << "=double,";
1101 break;
1102 case 3:
1103 border.m_width=2;
1104 f << wh[b] << "=w2,";
1105 break;
1106 case 4:
1107 border.m_style=WPSBorder::Dot;
1108 f << wh[b] << "=dot,";
1109 break;
1110 case 5:
1111 border.m_style=WPSBorder::LargeDot;
1112 f << wh[b] << "=dot,large,";
1113 break;
1114 case 6: // 3-1-1-1-1-1
1115 border.m_style=WPSBorder::Dash;
1116 f << wh[b] << "=dash311";
1117 break;
1118 case 7: // 3-1-1-1
1119 border.m_style=WPSBorder::Dash;
1120 f << wh[b] << "=dash31";
1121 break;
1122 case 8:
1123 border.m_style=WPSBorder::Dash;
1124 f << wh[b] << "=dash";
1125 break;
1126 case 9: // 2-2
1127 border.m_width=2;
1128 border.m_style=WPSBorder::LargeDot;
1129 f << wh[b] << "=dot,w2,";
1130 break;
1131 case 10: // 3-1-1-1-1-1
1132 border.m_width=2;
1133 border.m_style=WPSBorder::Dash;
1134 f << wh[b] << "=dash311,w2";
1135 break;
1136 case 11: // 3-1-1-1
1137 border.m_width=2;
1138 border.m_style=WPSBorder::Dash;
1139 f << wh[b] << "=dash31,w2";
1140 break;
1141 case 12: // 3-1
1142 border.m_width=2;
1143 border.m_style=WPSBorder::Dash;
1144 f << wh[b] << "=dash,w2";
1145 break;
1146 default:
1147 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellStyle: unknown border type\n"));
1148 break;
1149 }
1150 if (!m_mainParser.getColor(borderColors[b], border.m_color))
1151 f << ",##col=" << borderColors[b];
1152 else if (!border.m_color.isBlack())
1153 f << border.m_color;
1154 f << ",";
1155 if (b==4) continue;
1156 int const which[]= {WPSBorder::LeftBit, WPSBorder::TopBit, WPSBorder::RightBit, WPSBorder::BottomBit};
1157 style.setBorders(which[b], border);
1158 }
1159 if (patId==1)
1160 style.setBackgroundColor(surfColors[0]);
1161 else if (patId)
1162 {
1163 WPSGraphicStyle::Pattern pattern;
1164 if (!m_mainParser.getPattern(patId, pattern))
1165 f << "###";
1166 else
1167 {
1168 pattern.m_colors[0]=surfColors[1];
1169 pattern.m_colors[1]=surfColors[0];
1170 if (pattern.getAverageColor(color))
1171 style.setBackgroundColor(color);
1172 }
1173 f << "pat=" << patId << ",";
1174 }
1175 else
1176 f << "pat=none,";
1177 if (input->tell()!=pos+dataSz)
1178 ascFile.addDelimiter(input->tell(),'|');
1179 ascFile.addPos(pos);
1180 ascFile.addNote(f.str().c_str());
1181 input->seek(pos+dataSz, librevenge::RVNG_SEEK_SET);
1182 }
1183 return true;
1184 }
1185
1186 ////////////////////////////////////////////////////////////
1187 // parse sheet data: document zones
1188 ////////////////////////////////////////////////////////////
1189 bool Quattro9Spreadsheet::readDocumentFormulas(std::shared_ptr<WPSStream> const &stream)
1190 {
1191 RVNGInputStreamPtr input = stream->m_input;
1192 libwps::DebugFile &ascFile=stream->m_ascii;
1193 libwps::DebugStream f;
1194
1195 long pos = input->tell();
1196 auto type=int(libwps::readU16(input));
1197 if ((type&0x7fff) !=0x408)
1198 {
1199 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readDocumentFormulas: not a spreadsheet zone\n"));
1200 return false;
1201 }
1202 long sz = (type&0x8000) ? long(libwps::readU32(input)) : long(libwps::readU16(input));
1203 int const headerSize = (type&0x8000) ? 6 : 4;
1204 long endPos=pos+headerSize+sz;
1205 long N=long(libwps::readU16(input));
1206 if (sz<12 || (sz-headerSize-8)/4<N || !stream->checkFilePosition(endPos))
1207 {
1208 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readDocumentFormulas: the size seems bad\n"));
1209 return false;
1210 }
1211 f << "N=" << N << ",";
1212 int val=int(libwps::readU16(input));
1213 if (val) f << "f0=" << val << ",";
1214 f << "f1=" << libwps::readU32(input) << ",";
1215 f << "f2=" << libwps::readU32(input) << ",";
1216 ascFile.addPos(pos);
1217 ascFile.addNote(f.str().c_str());
1218
1219 m_state->m_documentFormulas.first = stream;
1220 auto &entriesList=m_state->m_documentFormulas.second;
1221 if (!entriesList.empty())
1222 {
1223 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readDocumentFormulas: oops, we have already some formula\n"));
1224 entriesList.clear();
1225 }
1226 entriesList.reserve(size_t(N));
1227 for (long i=0; i<N; ++i)
1228 {
1229 pos=input->tell();
1230 f.str("");
1231 f << "Document[formula-Fo" << i+1 << "]:";
1232 int dSz=int(libwps::readU16(input));
1233 if (pos+4+dSz>endPos)
1234 {
1235 input->seek(pos, librevenge::RVNG_SEEK_SET);
1236 break;
1237 }
1238 WPSEntry entry;
1239 entry.setBegin(pos);
1240 entry.setLength(4+dSz);
1241 entriesList.push_back(entry);
1242 ascFile.addPos(pos);
1243 ascFile.addNote(f.str().c_str());
1244 input->seek(pos+4+dSz, librevenge::RVNG_SEEK_SET);
1245 }
1246 if (input->tell()!=endPos)
1247 {
1248 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readDocumentFormulas: can not read some formulas\n"));
1249 ascFile.addPos(input->tell());
1250 ascFile.addNote("Document[formula]:###extra");
1251 }
1252 return true;
1253 }
1254
1255 ////////////////////////////////////////////////////////////
1256 // parse sheet data: spreadsheet zones
1257 ////////////////////////////////////////////////////////////
1258 bool Quattro9Spreadsheet::readBeginSheet(std::shared_ptr<WPSStream> const &stream, int &sheetId)
1259 {
1260 if (m_state->m_actualSpreadsheet)
1261 {
1262 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readBeginSheet: the last spreadsheet is not closed\n"));
1263 }
1264 RVNGInputStreamPtr input = stream->m_input;
1265 libwps::DebugFile &ascFile=stream->m_ascii;
1266 libwps::DebugStream f;
1267
1268 long pos = input->tell();
1269 auto type = long(libwps::readU16(input)&0x7fff);
1270 if (type !=0x601)
1271 {
1272 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readBeginSheet: not a spreadsheet zone\n"));
1273 return false;
1274 }
1275 long sz = libwps::readU16(input);
1276 long endPos=pos+4+sz;
1277 if (sz<22 || !stream->checkFilePosition(endPos))
1278 {
1279 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readBeginSheet: the size seems bad\n"));
1280 return false;
1281 }
1282 sheetId=int(libwps::readU16(input));
1283 f << "id=" << sheetId << ",";
1284 auto &sheet=m_state->m_actualSpreadsheet;
1285 sheet.reset(new Quattro9SpreadsheetInternal::Spreadsheet(sheetId));
1286 int dim[2];
1287 for (auto &d : dim) d=int(libwps::readU16(input));
1288 f << "cols[window?]=" << Vec2i(dim[0],dim[1]) << ","; // or 255-0 empty?
1289 int val=int(libwps::read16(input)); // 0,1,-1
1290 if (val) f << "f0=" << val << ",";
1291 for (auto &d : dim) d=int(libwps::readU16(input));
1292 f << "rows[window?]=" << Vec2i(dim[0],dim[1]) << ",";
1293 for (int i=0; i<3; ++i)
1294 {
1295 val=int(libwps::read16(input));
1296 int const expected[]= {0,-1,0};
1297 if (val!=expected[i]) f << "f" << i+1 << "=" << val << ",";
1298 }
1299 Quattro9ParserInternal::TextEntry entry;
1300 if (m_mainParser.readPString(stream, endPos, entry))
1301 {
1302 f << entry.getDebugString(stream) << ",";
1303 auto name=entry.getString(stream);
1304 if (!name.empty())
1305 {
1306 if (m_state->m_idToSheetNameMap.find(sheetId)==m_state->m_idToSheetNameMap.end())
1307 m_state->m_idToSheetNameMap[sheetId]=name;
1308 }
1309 }
1310 else
1311 {
1312 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readBeginSheet: can not read the spreadsheet name\n"));
1313 f << "###";
1314 }
1315 if (sheetId>=1024)
1316 {
1317 // checkme: what is the maximum of the number of sheet...
1318 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readBeginSheet: id=%d seems to big\n", sheetId));
1319 }
1320 else if (m_state->m_spreadsheetMap.find(sheetId)!=m_state->m_spreadsheetMap.end())
1321 {
1322 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readBeginSheet: id=%d sheet already exists\n", sheetId));
1323 f << "###id,";
1324 }
1325 else
1326 m_state->m_spreadsheetMap[sheetId]=sheet;
1327 if (input->tell()!=endPos)
1328 {
1329 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readBeginSheet: find extra data\n"));
1330 f << "###";
1331 ascFile.addDelimiter(input->tell(),'|');
1332 }
1333 ascFile.addPos(pos);
1334 ascFile.addNote(f.str().c_str());
1335 return true;
1336 }
1337
1338 bool Quattro9Spreadsheet::readEndSheet(std::shared_ptr<WPSStream> const &stream)
1339 {
1340 if (!m_state->m_actualSpreadsheet)
1341 {
1342 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readEndSheet: no spreadsheet are opened\n"));
1343 }
1344 else if (m_state->m_actualColumn>=0)
1345 {
1346 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readEndSheet: the last column is not closed\n"));
1347 m_state->m_actualColumn=-1;
1348 }
1349 m_state->m_actualSpreadsheet.reset();
1350 long filePos[2]; // f0=pointer to a previous zone 601|602
1351 Quattro9Parser::readFilePositions(stream, filePos);
1352 return true;
1353 }
1354
1355 bool Quattro9Spreadsheet::readColRowDefault(std::shared_ptr<WPSStream> const &stream)
1356 {
1357 RVNGInputStreamPtr input = stream->m_input;
1358 libwps::DebugFile &ascFile=stream->m_ascii;
1359 libwps::DebugStream f;
1360
1361 long pos = input->tell();
1362 auto type = long(libwps::readU16(input)&0x7fff);
1363 if (type !=0x631 && type != 0x632)
1364 {
1365 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readColRowDefault: not a dimension zone\n"));
1366 return false;
1367 }
1368 long sz = libwps::readU16(input);
1369 long endPos=pos+4+sz;
1370 if (sz!=2 || !stream->checkFilePosition(endPos))
1371 {
1372 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readColRowDefault: unexpected size\n"));
1373 return false;
1374 }
1375 int val=int(libwps::readU16(input)); // height/width in dpi?
1376 if (val&0x8000)
1377 f << "size=" << (val&0x7FFF) << ","; // exact
1378 else
1379 f << "size=" << val << "*,"; // automatic update
1380 if (!m_state->m_actualSpreadsheet)
1381 {
1382 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readColRowDimension: can not find the spreadsheet\n"));
1383 }
1384 else
1385 m_state->m_actualSpreadsheet->m_defaultSizes[type==0x631 ? 1 : 0]=(val&0x7fff);
1386 ascFile.addPos(pos);
1387 ascFile.addNote(f.str().c_str());
1388 return true;
1389 }
1390
1391 bool Quattro9Spreadsheet::readColRowDimension(std::shared_ptr<WPSStream> const &stream)
1392 {
1393 RVNGInputStreamPtr input = stream->m_input;
1394 libwps::DebugFile &ascFile=stream->m_ascii;
1395 libwps::DebugStream f;
1396
1397 long pos = input->tell();
1398 auto type = long(libwps::readU16(input)&0x7fff);
1399 if (type !=0x633 && type != 0x634)
1400 {
1401 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readColRowDimension: not a dimension zone\n"));
1402 return false;
1403 }
1404 long sz = libwps::readU16(input);
1405 long endPos=pos+4+sz;
1406 if (sz!=6 || !stream->checkFilePosition(endPos))
1407 {
1408 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readColRowDimension: unexpected size\n"));
1409 return false;
1410 }
1411 int posi=int(libwps::readU32(input));
1412 f << "id=" << posi << ",";
1413 int val=int(libwps::readU16(input)); // height/width in dpi?
1414 if (val&0x8000)
1415 f << "size=" << (val&0x7FFF) << ","; // exact
1416 else
1417 f << "size=" << val << "*,"; // automatic update
1418 if (!m_state->m_actualSpreadsheet)
1419 {
1420 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readColRowDimension: can not find the spreadsheet\n"));
1421 }
1422 else
1423 m_state->m_actualSpreadsheet->setColRowSize(type==0x633 ? 1 : 0, posi, (val&0x7fff));
1424 ascFile.addPos(pos);
1425 ascFile.addNote(f.str().c_str());
1426 return true;
1427 }
1428
1429 bool Quattro9Spreadsheet::readColRowDimensions(std::shared_ptr<WPSStream> const &stream)
1430 {
1431 RVNGInputStreamPtr input = stream->m_input;
1432 libwps::DebugFile &ascFile=stream->m_ascii;
1433 libwps::DebugStream f;
1434
1435 long pos = input->tell();
1436 auto type = long(libwps::readU16(input)&0x7fff);
1437 if (type !=0x635 && type != 0x636)
1438 {
1439 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readColRowDimensions: not a dimension zone\n"));
1440 return false;
1441 }
1442 long sz = libwps::readU16(input);
1443 long endPos=pos+4+sz;
1444 if (sz!=10 || !stream->checkFilePosition(endPos))
1445 {
1446 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readColRowDimension: unexpected size\n"));
1447 return false;
1448 }
1449 int dim[2];
1450 for (auto &d : dim) d=int(libwps::readU32(input));
1451 f << "limits=" << Vec2i(dim[0],dim[1]) << ",";
1452 int val=int(libwps::readU16(input)); // height/width in dpi?
1453 if (val&0x8000)
1454 f << "size=" << (val&0x7FFF) << ","; // exact
1455 else
1456 f << "size=" << val << "*,"; // automatic update
1457 if (!m_state->m_actualSpreadsheet)
1458 {
1459 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readColRowDimension: can not find the spreadsheet\n"));
1460 }
1461 else if (dim[0]<=dim[1])
1462 m_state->m_actualSpreadsheet->setColRowSizes(type==0x635 ? 1 : 0, dim[0], dim[1], (val&0x7fff));
1463 ascFile.addPos(pos);
1464 ascFile.addNote(f.str().c_str());
1465 return true;
1466 }
1467
1468 bool Quattro9Spreadsheet::readMergedCells(std::shared_ptr<WPSStream> const &stream)
1469 {
1470 RVNGInputStreamPtr input = stream->m_input;
1471 libwps::DebugFile &ascFile=stream->m_ascii;
1472 libwps::DebugStream f;
1473
1474 long pos = input->tell();
1475 auto type = long(libwps::readU16(input)&0x7fff);
1476 if (type !=0x61d)
1477 {
1478 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readMergedCells: not a merged cells zone\n"));
1479 return false;
1480 }
1481 long sz = libwps::readU16(input);
1482 if (sz!=16)
1483 {
1484 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readMergedCells: unexpected size\n"));
1485 return false;
1486 }
1487 int dim[4];
1488 for (auto &d : dim) d=int(libwps::readU32(input));
1489 WPSBox2i box(Vec2i(dim[0],dim[2]),Vec2i(dim[1],dim[3]));
1490 if (dim[0]<0 || dim[0]>dim[1] || dim[2]<0 || dim[2]>dim[3])
1491 {
1492 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readMergedCells: the selection seems bad\n"));
1493 f << "###";
1494 }
1495 else if (m_state->m_actualSpreadsheet)
1496 m_state->m_actualSpreadsheet->m_mergedCellList.push_back(box);
1497 else
1498 {
1499 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readMergedCells: can not find the main cell\n"));
1500 }
1501 f << box << ",";
1502 ascFile.addPos(pos);
1503 ascFile.addNote(f.str().c_str());
1504 return true;
1505 }
1506
1507 bool Quattro9Spreadsheet::readPageBreak(std::shared_ptr<WPSStream> const &stream)
1508 {
1509 RVNGInputStreamPtr input = stream->m_input;
1510 libwps::DebugFile &ascFile=stream->m_ascii;
1511 libwps::DebugStream f;
1512
1513 long pos = input->tell();
1514 auto type = long(libwps::readU16(input)&0x7fff);
1515 if (type !=0x617)
1516 {
1517 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readPageBreak: not a pagebreak zone\n"));
1518 return false;
1519 }
1520 long sz = libwps::readU16(input);
1521 if (sz<2 || (sz%2)!=0)
1522 {
1523 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readPageBreak: unexpected size\n"));
1524 return false;
1525 }
1526 int val=int(libwps::read16(input)); // 800=col?, a00=row?
1527 if (val) f << "fl=" << std::hex << val << std::dec << ",";
1528 int N=int((sz-2)/2);
1529 f << "break=[";
1530 for (int i=0; i<N; ++i) f << libwps::readU16(input) << ",";
1531 f << "],";
1532
1533 ascFile.addPos(pos);
1534 ascFile.addNote(f.str().c_str());
1535 return true;
1536 }
1537
1538 ////////////////////////////////////////////////////////////
1539 // parse sheet data: column zones
1540 ////////////////////////////////////////////////////////////
1541 bool Quattro9Spreadsheet::readBeginColumn(std::shared_ptr<WPSStream> const &stream)
1542 {
1543 if (!m_state->m_actualSpreadsheet)
1544 {
1545 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readBeginColumn: called outside a spreadsheet\n"));
1546 }
1547 else if (m_state->m_actualColumn>=0)
1548 {
1549 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readBeginColumn: the last column is not closed\n"));
1550 }
1551 RVNGInputStreamPtr input = stream->m_input;
1552 libwps::DebugFile &ascFile=stream->m_ascii;
1553 libwps::DebugStream f;
1554
1555 long pos = input->tell();
1556 auto type = long(libwps::readU16(input)&0x7fff);
1557 if (type !=0xa01)
1558 {
1559 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readBeginColumn: not a col[begin] zone\n"));
1560 return false;
1561 }
1562 long sz = libwps::readU16(input);
1563 long endPos=pos+4+sz;
1564 if (sz!=10 || !stream->checkFilePosition(endPos))
1565 {
1566 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readBeginColumn: unexpected size\n"));
1567 return false;
1568 }
1569 int col=int(libwps::readU16(input));
1570 f << "col=" << col << ",";
1571 if (m_state->m_actualSpreadsheet) m_state->m_actualColumn=col;
1572 int dim[2];
1573 for (auto &d : dim) d=int(libwps::readU32(input));
1574 f << "limits=" << Vec2i(dim[0],dim[1]) << ",";
1575 ascFile.addPos(pos);
1576 ascFile.addNote(f.str().c_str());
1577 return true;
1578 }
1579
1580 bool Quattro9Spreadsheet::readEndColumn(std::shared_ptr<WPSStream> const &stream)
1581 {
1582 if (m_state->m_actualColumn<0)
1583 {
1584 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readEndColumn: the last column is not opened\n"));
1585 }
1586 m_state->m_actualColumn=-1;
1587 long filePos[2]; // f0=pointer to a previous zone a01|a02
1588 Quattro9Parser::readFilePositions(stream, filePos);
1589 return true;
1590 }
1591
1592 ////////////////////////////////////////////////////////////
1593 // parse sheet data: cell zones
1594 ////////////////////////////////////////////////////////////
1595 bool Quattro9Spreadsheet::readCellList(std::shared_ptr<WPSStream> const &stream)
1596 {
1597 RVNGInputStreamPtr input = stream->m_input;
1598 libwps::DebugFile &ascFile=stream->m_ascii;
1599 libwps::DebugStream f;
1600
1601 long pos = input->tell();
1602 auto type = long(libwps::readU16(input)&0x7fff);
1603 if (type !=0xc01)
1604 {
1605 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellList: not a cell zone\n"));
1606 return false;
1607 }
1608 long sz = libwps::readU16(input);
1609 long endPos=pos+4+sz;
1610 if (sz<8 || !stream->checkFilePosition(endPos))
1611 {
1612 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellList: the size seems bad\n"));
1613 return false;
1614 }
1615 int row=int(libwps::readU32(input));
1616 if (row) f << "first[row]=" << row << ",";
1617 int nCells=int(libwps::readU32(input));
1618 if (nCells) f << "num[cells]=" << nCells << ",";
1619 int lastRow=row+nCells;
1620 ascFile.addPos(pos);
1621 ascFile.addNote(f.str().c_str());
1622
1623 int const col= m_state->m_actualSpreadsheet ? m_state->m_actualColumn : -1;
1624 if (col < 0)
1625 {
1626 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellList: call outside a col,begin zone\n"));
1627 }
1628 Quattro9SpreadsheetInternal::Column invalidColumn;
1629 Quattro9SpreadsheetInternal::Column &column = col>=0 ? m_state->m_actualSpreadsheet->getColumn(col) : invalidColumn;
1630 while (input->tell() < endPos)
1631 {
1632 pos=input->tell();
1633 f.str("");
1634 f << "Spreadsheet[cell]:";
1635 Quattro9SpreadsheetInternal::CellData cell;
1636 int cType=cell.m_type=int(libwps::readU8(input));
1637 bool ok=true;
1638 if (cType&0x80)
1639 {
1640 ok=(input->tell()+2<=endPos);
1641 if (ok)
1642 {
1643 cell.m_style=int(libwps::readU16(input));
1644 f << "Ce" << cell.m_style-1 << ",";
1645 }
1646 cType &= 0x7f;
1647 }
1648 int numData=1, N=1;
1649 if (ok)
1650 {
1651 if ((cType&0x60)==0x40) // list
1652 {
1653 ok=(input->tell()+2<=endPos);
1654 if (ok)
1655 {
1656 N=numData=int(libwps::readU16(input));
1657 f << "N=" << numData << ",";
1658 }
1659 cType &= 0x1f;
1660 }
1661 else if ((cType&0x60)==0x60) // first value followed by increment value
1662 {
1663 ok=(input->tell()+2<=endPos);
1664 if (ok)
1665 {
1666 N=int(libwps::readU16(input));
1667 f << "serie, N=" << N << ",";
1668 numData=2;
1669 }
1670 cType &= 0x1f;
1671 }
1672 else if ((cType&0x60)==0x20)
1673 {
1674 /* CHECKME what is it ?
1675 I tried to generate a list with 2|(n>2) similar values
1676 but it generates a list :-~
1677 */
1678 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellList: argh, list[cType]=0x20, some cells will be lost\n"));
1679 ok=false;
1680 }
1681 }
1682 if (col>=0)
1683 {
1684 if (N>1)
1685 f << "C" << col << "R" << row << "-" << row+N-1 << ",";
1686 else
1687 f << "C" << col << "R" << row << ",";
1688 }
1689 cell.m_rows=Vec2i(row, row+N-1);
1690 row += N;
1691 f << "type=" << std::hex << cType << std::dec << ",";
1692 if (ok)
1693 {
1694 long actPos=input->tell();
1695 ok=false;
1696 switch (cType)
1697 {
1698 case 1: // no data?
1699 ok=true;
1700 break;
1701 case 0x2: // unsigned int value
1702 if (actPos+numData*2>endPos) break;
1703 f << "values=[";
1704 for (int i=0; i<numData; ++i)
1705 {
1706 cell.m_intList.push_back(int(libwps::readU16(input)));
1707 f << cell.m_intList.back() << ",";
1708 }
1709 f << "],";
1710 ok=true;
1711 break;
1712 case 0x3: // signed int
1713 if (actPos+numData*2>endPos) break;
1714 f << "values=[";
1715 for (int i=0; i<numData; ++i)
1716 {
1717 cell.m_intList.push_back(int(libwps::read16(input)));
1718 f << cell.m_intList.back() << ",";
1719 }
1720 f << "],";
1721 ok=true;
1722 break;
1723 case 4: // double4 value
1724 if (actPos+4*numData>endPos) break;
1725 f << "values=[";
1726 for (int i=0; i<numData; ++i)
1727 {
1728 double value;
1729 bool isNaN;
1730 if (libwps::readDouble4(input, value, isNaN))
1731 f << value << ",";
1732 else
1733 {
1734 value=0;
1735 f << "###,";
1736 input->seek(actPos+(i+1)*8, librevenge::RVNG_SEEK_SET);
1737 }
1738 cell.m_doubleList.push_back(value);
1739 }
1740 f << "],";
1741 ok=true;
1742 break;
1743 case 5: // double
1744 {
1745 if (actPos+numData*8>endPos) break;
1746 f << "values=[";
1747 for (int i=0; i<numData; ++i)
1748 {
1749 double value;
1750 bool isNaN;
1751 if (libwps::readDouble8(input, value, isNaN))
1752 f << value << ",";
1753 else
1754 {
1755 value=0;
1756 f << "###,";
1757 input->seek(actPos+(i+1)*8, librevenge::RVNG_SEEK_SET);
1758 }
1759 cell.m_doubleList.push_back(value);
1760 }
1761 f << "],";
1762 ok=true;
1763 break;
1764 }
1765 case 7: // string index
1766 if (actPos+4*numData>endPos) break;
1767 f << "values=[";
1768 for (int i=0; i<numData; ++i) // the ith string
1769 {
1770 cell.m_intList.push_back(int(libwps::readU32(input)));
1771 f << "Str" << cell.m_intList.back() << ",";
1772 }
1773 f << "],";
1774 ok=true;
1775 break;
1776 case 8: // formula
1777 if (actPos+14*numData>endPos) break;
1778 f << "values=[";
1779 for (int i=0; i<numData; ++i)
1780 {
1781 double value;
1782 bool isNaN;
1783 if (libwps::readDouble8(input, value, isNaN))
1784 f << value << ",";
1785 else
1786 {
1787 f << "###,";
1788 input->seek(actPos+i*14+8, librevenge::RVNG_SEEK_SET);
1789 value=0;
1790 }
1791 cell.m_doubleList.push_back(value);
1792 cell.m_flagList.push_back(int(libwps::readU16(input))); // high byte=0-3, lower byte=0|8|10
1793 if (cell.m_flagList.back()) f << "fl=" << std::hex << cell.m_flagList.back() << std::dec << ",";
1794 cell.m_intList.push_back(int(libwps::readU32(input)));
1795 f << "Fo" << cell.m_intList.back() << ",";
1796 }
1797 f << "],";
1798 ok=true;
1799 break;
1800 default:
1801 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellList: argh, find unknown type %d, some cells will be lost\n", int(cType)));
1802 break;
1803 }
1804 }
1805 if (!ok)
1806 {
1807 input->seek(pos, librevenge::RVNG_SEEK_SET);
1808 break;
1809 }
1810 if ((cell.m_type&0x9f)!=1) // no need to add cell with no style and no content
1811 column.add(cell.m_rows, cell);
1812 ascFile.addPos(pos);
1813 ascFile.addNote(f.str().c_str());
1814 }
1815 if (input->tell() < endPos)
1816 {
1817 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellList: find extra data\n"));
1818 ascFile.addPos(input->tell());
1819 ascFile.addNote("Spreadsheet[cell]:###");
1820 }
1821 else if (lastRow!=row)
1822 {
1823 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellList: read an unexpected number of cells %d != %d\n", row, lastRow));
1824 }
1825 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1826 return true;
1827 }
1828
1829 bool Quattro9Spreadsheet::readCellResult(std::shared_ptr<WPSStream> const &stream)
1830 {
1831 RVNGInputStreamPtr input = stream->m_input;
1832 libwps::DebugFile &ascFile=stream->m_ascii;
1833 libwps::DebugStream f;
1834
1835 long pos = input->tell();
1836 auto type = long(libwps::readU16(input)&0x7fff);
1837 if (type !=0xc02)
1838 {
1839 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellResult: not a result zone\n"));
1840 return false;
1841 }
1842 long sz = libwps::readU16(input);
1843 long endPos=pos+4+sz;
1844 if (sz<10 || !stream->checkFilePosition(endPos))
1845 {
1846 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellResult: the size seems bad\n"));
1847 return false;
1848 }
1849
1850 int col=int(libwps::readU16(input));
1851 int row=int(libwps::readU32(input));
1852 if (col!=m_state->m_actualColumn)
1853 {
1854 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellResult: unexpected called\n"));
1855 f << "###";
1856 }
1857 f << "C" << col << "R" << row << ",";
1858 Quattro9ParserInternal::TextEntry entry;
1859 if (m_mainParser.readPString(stream, endPos, entry))
1860 {
1861 f << entry.getDebugString(stream) << ",";
1862 if (m_state->m_actualSpreadsheet && col>=0)
1863 m_state->m_actualSpreadsheet->getColumn(col).add(row,stream,entry);
1864 }
1865 else
1866 {
1867 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellResult: can not read a string\n"));
1868 f << "###";
1869 }
1870
1871 ascFile.addPos(pos);
1872 ascFile.addNote(f.str().c_str());
1873 return true;
1874 }
1875 ////////////////////////////////////////////////////////////
1876 // formula
1877 ////////////////////////////////////////////////////////////
1878 bool Quattro9Spreadsheet::readCell
1879 (std::shared_ptr<WPSStream> const &stream, Vec2i actPos, WKSContentListener::FormulaInstruction &instr, int sheetId, librevenge::RVNGString const &fName)
1880 {
1881 RVNGInputStreamPtr input = stream->m_input;
1882 instr=WKSContentListener::FormulaInstruction();
1883 instr.m_type=WKSContentListener::FormulaInstruction::F_Cell;
1884 instr.m_fileName=fName;
1885 int pos[3]; // col, sheet, fl|row
1886 bool relative[3] = { false, false, false};
1887 for (auto &p : pos) p=int(libwps::readU16(input));
1888 int fl=int(libwps::readU16(input));
1889 if (fl&0x8000)
1890 {
1891 pos[1] = int(uint16_t(pos[1]+sheetId)); // unsure what is the maximum
1892 relative[1] = true;
1893 }
1894 if (fl&0x4000)
1895 {
1896 pos[0] = int(uint16_t(pos[0]+actPos[0]));
1897 relative[0] = true;
1898 }
1899 if (fl&0x2000)
1900 {
1901 pos[2] = int(uint16_t(pos[2]+actPos[1]));
1902 relative[2] = true;
1903 }
1904 //fl&0x1fff=0 | 1fff depending on the sign of pos[2] probably junk
1905 instr.m_position[0]=Vec2i(pos[0],pos[2]);
1906 instr.m_positionRelative[0]=Vec2b(relative[0],relative[2]);
1907 if (!fName.empty()) // external file, assume default name
1908 {
1909 librevenge::RVNGString name;
1910 name.sprintf("Sheet%d", pos[1]+1);
1911 instr.m_sheetName[0]=name;
1912 }
1913 else
1914 instr.m_sheetId[0]=pos[1];
1915 return true;
1916 }
1917
1918 bool Quattro9Spreadsheet::readCellReference(std::shared_ptr<WPSStream> const &stream, long endPos,
1919 QuattroFormulaInternal::CellReference &ref,
1920 Vec2i const &cPos, int sheetId) const
1921 {
1922 ref.m_cells.clear();
1923 RVNGInputStreamPtr input = stream->m_input;
1924 long pos = input->tell();
1925 if (pos+4>endPos) return false;
1926 auto type=int(libwps::readU16(input));
1927 int cellType=type>>12;
1928 if (cellType>4) return false;
1929 if (cellType==4)
1930 {
1931 // type==4:
1932 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellReference: find a cell collection 4\n"));
1933 return false;
1934 }
1935 WKSContentListener::FormulaInstruction instr;
1936 if (cellType==3)
1937 {
1938 // checkme
1939 int dataSize=(type&0x3ff);
1940 if (pos+2+dataSize>endPos)
1941 {
1942 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellReference: can not read the cell collection data size\n"));
1943 return false;
1944 }
1945 if (type&0xc00) // check for deletion
1946 {
1947 input->seek(dataSize, librevenge::RVNG_SEEK_CUR);
1948 return true;
1949 }
1950 endPos=pos+2+dataSize;
1951 while (input->tell()<endPos)
1952 {
1953 QuattroFormulaInternal::CellReference cells;
1954 if (!readCellReference(stream, endPos, cells, cPos, sheetId))
1955 {
1956 WPS_DEBUG_MSG(("Quattro9Spreadsheet::readCellReference: can not read a cell\n"));
1957 return false;
1958 }
1959 for (auto const &c : cells.m_cells) ref.addInstruction(c);
1960 }
1961 return true;
1962 }
1963 int const expectedSize[]= {8,16,2};
1964 if (pos+2+expectedSize[cellType]>endPos) return false;
1965 // checkme: what means type&0x800?
1966 if (type&0x400)
1967 {
1968 input->seek(expectedSize[cellType], librevenge::RVNG_SEEK_CUR);
1969 return true;
1970 }
1971 librevenge::RVNGString fileName;
1972 if ((type&0x3ff))
1973 {
1974 if (!m_mainParser.getExternalFileName((type&0x3ff), fileName))
1975 return false;
1976 }
1977 if (cellType==0 && pos+10<=endPos)
1978 {
1979 if (!readCell(stream, cPos, instr, sheetId, fileName))
1980 return false;
1981 ref.addInstruction(instr);
1982 return true;
1983 }
1984 else if (cellType==2)
1985 {
1986 auto fId=int(libwps::readU16(input));
1987 librevenge::RVNGString text;
1988 return m_mainParser.getField(fId, text, ref, fileName);
1989 }
1990 else if (cellType==1 && pos+18<=endPos)
1991 {
1992 WKSContentListener::FormulaInstruction cell2;
1993 if (!readCell(stream, cPos, instr, sheetId, fileName) ||
1994 !readCell(stream, cPos, cell2, sheetId, fileName))
1995 return false;
1996 instr.m_type=WKSContentListener::FormulaInstruction::F_CellList;
1997 instr.m_position[1]=cell2.m_position[0];
1998 instr.m_positionRelative[1]=cell2.m_positionRelative[0];
1999 instr.m_sheetId[1]=cell2.m_sheetId[0];
2000 instr.m_sheetName[1]=cell2.m_sheetName[0];
2001 ref.addInstruction(instr);
2002 return true;
2003 }
2004 return false;
2005 }
2006
2007 ////////////////////////////////////////////////////////////
2008 // send data
2009 ////////////////////////////////////////////////////////////
2010 void Quattro9Spreadsheet::sendSpreadsheet(int sId)
2011 {
2012 if (!m_listener)
2013 {
2014 WPS_DEBUG_MSG(("Quattro9Spreadsheet::sendSpreadsheet: I can not find the listener\n"));
2015 return;
2016 }
2017 std::shared_ptr<Quattro9SpreadsheetInternal::Spreadsheet> sheet;
2018 auto sheetIt=m_state->m_spreadsheetMap.find(sId);
2019 if (sheetIt==m_state->m_spreadsheetMap.end())
2020 sheet.reset(new Quattro9SpreadsheetInternal::Spreadsheet(sId));
2021 else
2022 sheet=sheetIt->second;
2023 sheet->update();
2024 m_listener->openSheet(sheet->getWidths(), m_state->getSheetName(sId));
2025 m_mainParser.sendPageGraphics(sId);
2026 std::set<int> listRows=sheet->getListSendRow();
2027 for (auto rIt=listRows.begin(); rIt!=listRows.end(); ++rIt)
2028 {
2029 int row=*rIt;
2030 auto nextRIt=rIt;
2031 int numRow=(++nextRIt==listRows.end()) ? 1 : *nextRIt-row;
2032 m_listener->openSheetRow(sheet->getRowHeight(row), numRow);
2033 std::vector<Vec2i> cols=sheet->getListCellsInARow(row);
2034 for (auto const &c : cols)
2035 {
2036 Vec2i pos(c[0],row);
2037 sendCellContent(sheet->getCell(pos), pos, sId, 1+c[1]-c[0]);
2038 }
2039 m_listener->closeSheetRow();
2040 }
2041 m_listener->closeSheet();
2042 }
2043
2044 void Quattro9Spreadsheet::sendCellContent(Quattro9SpreadsheetInternal::CellData const *cell, Vec2i pos, int sheetId, int numRepeated)
2045 {
2046 if (!cell) return;
2047 if (m_listener.get() == nullptr)
2048 {
2049 WPS_DEBUG_MSG(("Quattro9Spreadsheet::sendCellContent: I can not find the listener\n"));
2050 return;
2051 }
2052 Quattro9SpreadsheetInternal::Cell finalCell;
2053 WKSContentListener::CellContent content;
2054 finalCell.setPosition(pos);
2055 finalCell.setNumSpannedCells(cell->m_span);
2056 WPSFont font;
2057 libwps_tools_win::Font::Type fontType=m_mainParser.getDefaultFontType();
2058 if (cell->m_style>0 && cell->m_style<=int(m_state->m_stylesList.size()))
2059 {
2060 auto const &style=m_state->m_stylesList[size_t(cell->m_style-1)];
2061 static_cast<WPSCellFormat &>(finalCell)=style;
2062 if (style.m_fileFormat>0) finalCell.updateFormat(style.m_fileFormat);
2063 font=style.getFont();
2064 if (!font.m_name.empty())
2065 {
2066 fontType=libwps_tools_win::Font::getFontType(font.m_name);
2067 if (fontType==libwps_tools_win::Font::UNKNOWN)
2068 fontType=m_mainParser.getDefaultFontType();
2069 else
2070 finalCell.setFont(font);
2071 }
2072 }
2073 else if (cell->m_style>0)
2074 {
2075 WPS_DEBUG_MSG(("Quattro9Spreadsheet::sendCellContent: unknown style %d\n", cell->m_style-1));
2076 }
2077 m_listener->setFont(font);
2078 int stringId=-1;
2079 switch ((cell->m_type&0x1f))
2080 {
2081 case 1: // none
2082 break;
2083 case 2:
2084 case 3:
2085 content.m_contentType=WKSContentListener::CellContent::C_NUMBER;
2086 content.setValue(cell->getInt(pos[1]));
2087 break;
2088 case 4:
2089 case 5:
2090 content.m_contentType=WKSContentListener::CellContent::C_NUMBER;
2091 content.setValue(cell->getDouble(pos[1]));
2092 break;
2093 case 7:
2094 content.m_contentType=WKSContentListener::CellContent::C_TEXT;
2095 stringId=cell->getInt(pos[1]);
2096 break;
2097 case 8: // changeme
2098 {
2099 content.m_contentType=WKSContentListener::CellContent::C_NUMBER;
2100 content.setValue(cell->getDouble(pos[1]));
2101 int fId=cell->getInt(pos[1]);
2102 if (fId>0 && fId<=int(m_state->m_documentFormulas.second.size()) &&
2103 m_state->m_documentFormulas.first)
2104 {
2105 auto const &entry=m_state->m_documentFormulas.second[size_t(fId-1)];
2106 auto &stream=m_state->m_documentFormulas.first;
2107 auto &input=stream->m_input;
2108 libwps::DebugFile &ascFile=stream->m_ascii;
2109 libwps::DebugStream f;
2110 long actPos=input->tell();
2111 std::string error;
2112 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
2113 bool formulaOk=m_state->m_formulaManager.readFormula(stream, entry.end(), pos, sheetId, content.m_formula, error);
2114 for (auto const &fo : content.m_formula)
2115 f << fo;
2116 f << "," << error;
2117 if (formulaOk)
2118 {
2119 content.m_contentType=WKSContentListener::CellContent::C_FORMULA;
2120 for (auto &fo : content.m_formula)
2121 {
2122 if (fo.m_type==WKSContentListener::FormulaInstruction::F_Cell ||
2123 fo.m_type==WKSContentListener::FormulaInstruction::F_CellList)
2124 {
2125 int dim=fo.m_type==WKSContentListener::FormulaInstruction::F_Cell ? 1 : 2;
2126 for (int i=0; i<dim; ++i)
2127 {
2128 if (fo.m_sheetId[i]>=0 && fo.m_sheetName[i].empty() && (fo.m_sheetId[i]!=sheetId || !fo.m_fileName.empty()))
2129 fo.m_sheetName[i]=getSheetName(fo.m_sheetId[i]);
2130 }
2131 continue;
2132 }
2133 if (fo.m_type!=WKSContentListener::FormulaInstruction::F_Text)
2134 continue;
2135 std::string &text=fo.m_content;
2136 librevenge::RVNGString finalString=libwps_tools_win::Font::unicodeString(text, fontType);
2137 if (finalString.empty())
2138 text.clear();
2139 else
2140 text=finalString.cstr();
2141 }
2142 }
2143 else
2144 content.m_formula.clear();
2145 ascFile.addPos(entry.begin());
2146 ascFile.addNote(f.str().c_str());
2147 input->seek(actPos, librevenge::RVNG_SEEK_SET);
2148 }
2149 break;
2150 }
2151 default:
2152 break;
2153 }
2154 m_listener->openSheetCell(finalCell, content, numRepeated);
2155 if (stringId>0 && stringId<=int(m_state->m_documentStrings.second.size()) &&
2156 m_state->m_documentStrings.first)
2157 m_state->m_documentStrings.second[size_t(stringId-1)].send(m_state->m_documentStrings.first, font, fontType, m_listener);
2158 else if (stringId>0)
2159 {
2160 WPS_DEBUG_MSG(("Quattro9Spreadsheet::sendCellContent: can not find the string %d\n", stringId));
2161 }
2162 m_listener->closeSheetCell();
2163 }
2164 ////////////////////////////////////////////////////////////
2165 // cell reference
2166 ////////////////////////////////////////////////////////////
2167
2168 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */