Debian Codegraph @

src/lib/QuattroDosSpreadsheet.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2006, 2007 Andrew Ziem
11 * Copyright (C) 2004 Marc Maurer ([email protected])
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 *
14 * For minor contributions see the git repository.
15 *
16 * Alternatively, the contents of this file may be used under the terms
17 * of the GNU Lesser General Public License Version 2.1 or later
18 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
19 * applicable instead of those above.
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <cmath>
26 #include <limits>
27 #include <set>
28 #include <sstream>
29 #include <stack>
30 #include <string>
31
32 #include <librevenge-stream/librevenge-stream.h>
33
34 #include "libwps_internal.h"
35 #include "libwps_tools_win.h"
36
37 #include "WPSCell.h"
38 #include "WKSContentListener.h"
39 #include "WPSEntry.h"
40 #include "WPSFont.h"
41 #include "WPSTable.h"
42
43 #include "QuattroDos.h"
44
45 #include "QuattroDosSpreadsheet.h"
46
47 namespace QuattroDosSpreadsheetInternal
48 {
49
50 ///////////////////////////////////////////////////////////////////
51 //! a class used to store a style of a cell in QuattroDosSpreadsheet
52 struct Style final : public WPSCellFormat
53 {
54 //! construtor
55 explicit Style(libwps_tools_win::Font::Type type)
56 : WPSCellFormat()
57 , m_fontType(type)
58 , m_fileFormat(0xFF)
59 , m_extra("")
60 {
61 }
62 Style(Style const &)=default;
63 Style &operator=(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 //! font encoding type
76 libwps_tools_win::Font::Type m_fontType;
77 //! the file format
78 int m_fileFormat;
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_fontType!=st.m_fontType || m_fileFormat!=st.m_fileFormat) return false;
102 int diff = WPSCellFormat::compare(st);
103 if (diff) return false;
104 return m_extra==st.m_extra;
105 }
106
107 ///////////////////////////////////////////////////////////////////
108 //! the style manager
109 class StyleManager
110 {
111 public:
112 StyleManager()
113 : m_idStyleMap() {}
114 //! add a new style and returns its id
115 void add(int id, Style const &st)
116 {
117 if (m_idStyleMap.find(id)!=m_idStyleMap.end())
118 {
119 WPS_DEBUG_MSG(("QuattroDosParserInternal::StyleManager::add style %d already exists\n", id));
120 return;
121 }
122 m_idStyleMap.insert(std::map<int, Style>::value_type(id,st));
123 }
124 //! returns the style with id
125 bool get(int id, Style &style) const
126 {
127 if (m_idStyleMap.find(id)==m_idStyleMap.end())
128 {
129 WPS_DEBUG_MSG(("QuattroDosParserInternal::StyleManager::get can not find style %d\n", id));
130 return false;
131 }
132 style=m_idStyleMap.find(id)->second;
133 return true;
134 }
135 //! print a style
136 void print(int id, std::ostream &o) const
137 {
138 if (m_idStyleMap.find(id)!=m_idStyleMap.end())
139 o << ", style=" << m_idStyleMap.find(id)->second;
140 else
141 {
142 WPS_DEBUG_MSG(("QuattroDosParserInternal::StyleManager::print: can not find a style\n"));
143 o << ", ###style=" << id;
144 }
145 }
146
147 protected:
148 //! the styles
149 std::map<int, Style> m_idStyleMap;
150 };
151
152 //! a cellule of a Quattro spreadsheet
153 class Cell final : public WPSCell
154 {
155 public:
156 /// constructor
157 explicit Cell(libwps_tools_win::Font::Type type)
158 : m_fontType(type)
159 , m_fileFormat(0xFF)
160 , m_content() { }
161
162 //! operator<<
163 friend std::ostream &operator<<(std::ostream &o, Cell const &cell);
164
165 //! call when a cell must be send
166 bool send(WPSListenerPtr &/*listener*/) final;
167
168 //! call when the content of a cell must be send
169 bool sendContent(WPSListenerPtr &/*listener*/) final
170 {
171 WPS_DEBUG_MSG(("QuattroDosSpreadsheetInternal::Cell::sendContent: must not be called\n"));
172 return false;
173 }
174
175 //! update the cell format using file format
176 void updateFormat()
177 {
178 switch ((m_fileFormat>>4)&7)
179 {
180 case 0: // fixed
181 setFormat(F_NUMBER, 1);
182 setDigits(m_fileFormat&0xF);
183 break;
184 case 1: // scientific
185 setFormat(F_NUMBER, 2);
186 setDigits(m_fileFormat&0xF);
187 break;
188 case 2: // currency
189 setFormat(F_NUMBER, 4);
190 setDigits(m_fileFormat&0xF);
191 break;
192 case 3: // percent
193 setFormat(F_NUMBER, 3);
194 setDigits(m_fileFormat&0xF);
195 break;
196 case 4: // decimal
197 setFormat(F_NUMBER, 1);
198 setDigits(m_fileFormat&0xF);
199 break;
200 case 5:
201 switch (m_fileFormat&0xF)
202 {
203 case 4: // a date but no sure which format is good
204 setDTFormat(F_DATE, "%m/%d/%y");
205 break;
206 case 5:
207 setDTFormat(F_DATE, "%m/%d");
208 break;
209 default:
210 WPS_DEBUG_MSG(("QuattroDosSpreadsheetInternal::Cell::updateFormat: unknown format %x\n", unsigned(m_fileFormat)));
211 break;
212 }
213 break;
214 case 6:
215 WPS_DEBUG_MSG(("QuattroDosSpreadsheetInternal::Cell::updateFormat: unknown format %x\n", unsigned(m_fileFormat)));
216 break;
217 case 7:
218 switch (m_fileFormat&0xF)
219 {
220 case 0: // +/- : kind of bool
221 setFormat(F_BOOLEAN);
222 break;
223 case 1:
224 setFormat(F_NUMBER, 0);
225 break;
226 case 2:
227 setDTFormat(F_DATE, "%d %B %y");
228 break;
229 case 3:
230 setDTFormat(F_DATE, "%d %B");
231 break;
232 case 4:
233 setDTFormat(F_DATE, "%B %y");
234 break;
235 case 5:
236 setFormat(F_TEXT);
237 break;
238 case 6:
239 setFormat(F_TEXT);
240 m_font.m_attributes |= WPS_HIDDEN_BIT;
241 break;
242 case 7:
243 setDTFormat(F_TIME, "%I:%M:%S%p");
244 break;
245 case 8:
246 setDTFormat(F_TIME, "%I:%M%p");
247 break;
248 case 9:
249 setDTFormat(F_DATE, "%m/%d/%y");
250 break;
251 case 0xa:
252 setDTFormat(F_DATE, "%m/%d");
253 break;
254 case 0xb:
255 setDTFormat(F_TIME, "%H:%M:%S");
256 break;
257 case 0xc:
258 setDTFormat(F_TIME, "%H:%M");
259 break;
260 case 0xd:
261 setFormat(F_TEXT);
262 break;
263 case 0xf: // automatic
264 break;
265 default:
266 break;
267 }
268 break;
269 default:
270 break;
271 }
272 }
273 //! font encoding type
274 libwps_tools_win::Font::Type m_fontType;
275 //! the file format
276 int m_fileFormat;
277 //! the content
278 WKSContentListener::CellContent m_content;
279 };
280
281 bool Cell::send(WPSListenerPtr &/*listener*/)
282 {
283 WPS_DEBUG_MSG(("QuattroDosSpreadsheetInternal::Cell::send: must not be called\n"));
284 return false;
285 }
286 //! operator<<
287 std::ostream &operator<<(std::ostream &o, Cell const &cell)
288 {
289 o << reinterpret_cast<WPSCell const &>(cell) << cell.m_content << ",";
290 if (cell.m_fileFormat!=0xFF)
291 o << "format=" << std::hex << cell.m_fileFormat << std::dec << ",";
292 return o;
293 }
294
295 ///////////////////////////////////////////////////////////////////
296 //! the spreadsheet of a QuattroDos Spreadsheet
297 class Spreadsheet
298 {
299 public:
300 //! the spreadsheet type
301 enum Type { T_Spreadsheet, T_Filter, T_Report };
302
303 //! a constructor
304 explicit Spreadsheet(Type type=T_Spreadsheet, int id=0)
305 : m_type(type)
306 , m_id(id)
307 , m_numCols(0)
308 , m_widthCols()
309 , m_rowHeightMap()
310 , m_heightDefault(16)
311 , m_widthDefault(76)
312 , m_positionToCellMap()
313 , m_lastCellPos()
314 , m_rowPageBreaksList() {}
315 //! return a cell corresponding to a spreadsheet, create one if needed
316 Cell &getCell(Vec2i const &pos, libwps_tools_win::Font::Type type)
317 {
318 if (m_positionToCellMap.find(pos)==m_positionToCellMap.end())
319 {
320 Cell cell(type);
321 cell.setPosition(pos);
322 m_positionToCellMap.insert(std::map<Vec2i, Cell>::value_type(pos,cell));
323 }
324 m_lastCellPos=pos;
325 return m_positionToCellMap.find(pos)->second;
326 }
327 //! returns the last cell
328 Cell *getLastCell()
329 {
330 if (m_positionToCellMap.find(m_lastCellPos)==m_positionToCellMap.end())
331 return nullptr;
332 return &m_positionToCellMap.find(m_lastCellPos)->second;
333 }
334 void insertChartPositions(std::map<Vec2i,Vec2i> const &chartPosMap, libwps_tools_win::Font::Type type)
335 {
336 int maxCol=-1;
337 for (auto const &it : chartPosMap)
338 {
339 if (it.second[0]>maxCol) maxCol=it.second[0];
340 if (m_positionToCellMap.find(it.first)!=m_positionToCellMap.end())
341 continue;
342 Cell cell(type);
343 cell.setPosition(it.first);
344 m_positionToCellMap.insert(std::map<Vec2i, Cell>::value_type(it.first,cell));
345 }
346 if (maxCol>=int(m_widthCols.size()))
347 m_widthCols.resize(size_t(maxCol+1),-1);
348 }
349 //! set the columns size
350 void setColumnWidth(int col, int w=-1)
351 {
352 if (col < 0) return;
353 if (col >= int(m_widthCols.size())) m_widthCols.resize(size_t(col)+1, -1);
354 m_widthCols[size_t(col)] = w;
355 if (col >= m_numCols) m_numCols=col+1;
356 }
357
358 //! return the columns format
359 std::vector<WPSColumnFormat> getWidths() const
360 {
361 std::vector<WPSColumnFormat> widths;
362 WPSColumnFormat defWidth(static_cast<float>(m_widthDefault)), actWidth;
363 defWidth.m_useOptimalWidth=true;
364 int repeat=0;
365 for (auto const &w : m_widthCols)
366 {
367 WPSColumnFormat newWidth;
368 if (w < 0)
369 newWidth=defWidth;
370 else
371 newWidth=WPSColumnFormat(float(w)/20.f);
372 if (repeat && newWidth!=actWidth)
373 {
374 actWidth.m_numRepeat=repeat;
375 widths.push_back(actWidth);
376 repeat=0;
377 }
378 if (repeat==0)
379 actWidth=newWidth;
380 ++repeat;
381 }
382 if (repeat)
383 {
384 actWidth.m_numRepeat=repeat;
385 widths.push_back(actWidth);
386 }
387 return widths;
388 }
389 //! returns the row size in point
390 float getRowHeight(int row) const
391 {
392 auto rIt=m_rowHeightMap.lower_bound(Vec2i(-1,row));
393 if (rIt!=m_rowHeightMap.end() && rIt->first[0]<=row && rIt->first[1]>=row)
394 return float(rIt->second);
395 return float(m_heightDefault);
396 }
397 //! returns the height of a row in point and updated repeated row
398 float getRowHeight(int row, int &numRepeated) const
399 {
400 auto rIt=m_rowHeightMap.lower_bound(Vec2i(-1,row));
401 if (rIt!=m_rowHeightMap.end() && rIt->first[0]<=row && rIt->first[1]>=row)
402 {
403 numRepeated=rIt->first[1]-row+1;
404 return float(rIt->second);
405 }
406 numRepeated=10000;
407 return float(m_heightDefault);
408 }
409 //! set the rows size
410 void setRowHeight(int row, int h)
411 {
412 if (h>=0)
413 m_rowHeightMap[Vec2i(row,row)]=h;
414 }
415 //! return the size of the zone containning two cells
416 Vec2f getZoneSize(Vec2i const &cell0, Vec2i const &cell1) const
417 {
418 int w=0;
419 auto numCol=int(m_widthCols.size());
420 for (int i=cell0[0]; i<=cell1[0]; ++i)
421 {
422 if (i<0 || i>=numCol)
423 w+=m_widthDefault;
424 else
425 w+=m_widthCols[size_t(i)];
426 }
427 int h=0;
428 auto rIt=m_rowHeightMap.lower_bound(Vec2i(-1,cell0[1]));
429 for (int i=cell0[1]; i<=cell1[1]; ++i)
430 {
431 if (rIt==m_rowHeightMap.end())
432 h+=m_heightDefault;
433 else if (rIt->first[0]<=i && rIt->first[1]>=i)
434 h+=rIt->second;
435 else
436 {
437 if (rIt->first[1]<i) ++rIt;
438 if (rIt==m_rowHeightMap.end() || rIt->first[0]>i)
439 h+=m_heightDefault;
440 else
441 h+=rIt->second;
442 }
443 }
444 return Vec2f(float(w),float(h));
445 }
446 //! try to compress the list of row height
447 void compressRowHeights()
448 {
449 auto oldMap=m_rowHeightMap;
450 m_rowHeightMap.clear();
451 int actHeight=-1;
452 Vec2i actPos(0,-1);
453 for (auto const &rIt : oldMap)
454 {
455 // first check for not filled row
456 if (rIt.first[0]!=actPos[1]+1)
457 {
458 if (actHeight==m_heightDefault)
459 actPos[1]=rIt.first[0]-1;
460 else
461 {
462 if (actPos[1]>=actPos[0])
463 m_rowHeightMap[actPos]=actHeight;
464 actHeight=m_heightDefault;
465 actPos=Vec2i(actPos[1]+1, rIt.first[0]-1);
466 }
467 }
468 if (rIt.second!=actHeight)
469 {
470 if (actPos[1]>=actPos[0])
471 m_rowHeightMap[actPos]=actHeight;
472 actPos[0]=rIt.first[0];
473 actHeight=rIt.second;
474 }
475 actPos[1]=rIt.first[1];
476 }
477 if (actPos[1]>=actPos[0])
478 m_rowHeightMap[actPos]=actHeight;
479 }
480 //! returns true if the spreedsheet is empty
481 bool empty() const
482 {
483 return m_positionToCellMap.empty();
484 }
485 //! the spreadsheet type
486 Type m_type;
487 //! the spreadsheet id
488 int m_id;
489 /** the number of columns */
490 int m_numCols;
491
492 /** the column size in TWIP (?) */
493 std::vector<int> m_widthCols;
494 /** the map Vec2i(min row, max row) to size in points */
495 std::map<Vec2i,int> m_rowHeightMap;
496 /** the default row size in point */
497 int m_heightDefault;
498 /** the default width size in point */
499 int m_widthDefault;
500 /** a map cell to not empty cells */
501 std::map<Vec2i, Cell> m_positionToCellMap;
502 /** the last cell position */
503 Vec2i m_lastCellPos;
504 /** the list of row page break */
505 std::vector<int> m_rowPageBreaksList;
506
507 };
508
509 //! the state of QuattroDosSpreadsheet
510 struct State
511 {
512 //! constructor
513 State()
514 : m_eof(-1)
515 , m_version(-1)
516 , m_styleManager()
517 , m_spreadsheetList()
518 , m_spreadsheetStack()
519 , m_idToSheetNameMap()
520 , m_sheetNameSet()
521 {
522 pushNewSheet(std::shared_ptr<Spreadsheet>(new Spreadsheet(Spreadsheet::T_Spreadsheet, 0)));
523 }
524 //! returns the maximal spreadsheet
525 int getMaximalSheet(Spreadsheet::Type type=Spreadsheet::T_Spreadsheet) const
526 {
527 int max=-1;
528 for (auto const &sheet : m_spreadsheetList)
529 {
530 if (!sheet || sheet->m_type != type || sheet->m_id<=max || sheet->empty()) continue;
531 max=sheet->m_id;
532 }
533 return max;
534 }
535 //! returns the ith real spreadsheet
536 std::shared_ptr<Spreadsheet> getSheet(Spreadsheet::Type type, int id)
537 {
538 for (auto const &sheet : m_spreadsheetList)
539 {
540 if (!sheet || sheet->m_type!=type || sheet->m_id!=id)
541 continue;
542 return sheet;
543 }
544 return std::shared_ptr<Spreadsheet>();
545 }
546 //! returns the ith spreadsheet
547 librevenge::RVNGString getSheetName(int id)
548 {
549 auto it = m_idToSheetNameMap.find(id);
550 if (it!=m_idToSheetNameMap.end() && !it->second.empty())
551 return it->second;
552 librevenge::RVNGString name;
553 name.sprintf("Sheet%d", id+1);
554 name=getUniqueSheetName(name);
555 m_idToSheetNameMap[id]=name;
556 return name;
557 }
558 //! returns a unique sheet name build using the current name
559 librevenge::RVNGString getUniqueSheetName(librevenge::RVNGString const &name)
560 {
561 if (m_sheetNameSet.find(name)==m_sheetNameSet.end())
562 {
563 m_sheetNameSet.insert(name);
564 return name;
565 }
566 int i=0;
567 while (i<1000)
568 {
569 librevenge::RVNGString uniqueName(name);
570 uniqueName.append((std::string("_")+std::to_string(i++)).c_str());
571 if (m_sheetNameSet.find(uniqueName)==m_sheetNameSet.end())
572 {
573 m_sheetNameSet.insert(uniqueName);
574 return uniqueName;
575 }
576 }
577 return name;
578 }
579 //! returns the actual sheet
580 Spreadsheet &getActualSheet()
581 {
582 return *m_spreadsheetStack.top();
583 }
584 //! create a new sheet and stack id
585 void pushNewSheet(std::shared_ptr<Spreadsheet> sheet)
586 {
587 if (!sheet)
588 {
589 WPS_DEBUG_MSG(("QuattroDosSpreadsheetInternal::State::pushSheet: can not find the sheet\n"));
590 return;
591 }
592 m_spreadsheetStack.push(sheet);
593 m_spreadsheetList.push_back(sheet);
594 }
595 //! try to pop the actual sheet
596 bool popSheet()
597 {
598 if (m_spreadsheetStack.size()<=1)
599 {
600 WPS_DEBUG_MSG(("QuattroDosSpreadsheetInternal::State::popSheet: can pop the main sheet\n"));
601 return false;
602 }
603 m_spreadsheetStack.pop();
604 return true;
605 }
606
607 //! the last file position
608 long m_eof;
609 //! the file version
610 int m_version;
611 //! the style manager
612 StyleManager m_styleManager;
613
614 //! the list of spreadsheet ( first: main spreadsheet, other report spreadsheet )
615 std::vector<std::shared_ptr<Spreadsheet> > m_spreadsheetList;
616 //! the stack of spreadsheet id
617 std::stack<std::shared_ptr<Spreadsheet> > m_spreadsheetStack;
618 //! the map id to sheet's name
619 std::map<int, librevenge::RVNGString> m_idToSheetNameMap;
620 //! the set of used sheet name
621 std::set<librevenge::RVNGString> m_sheetNameSet;
622
623 };
624
625 }
626
627 // constructor, destructor
628 QuattroDosSpreadsheet::QuattroDosSpreadsheet(QuattroDosParser &parser)
629 : m_input(parser.getInput())
630 , m_listener()
631 , m_mainParser(parser)
632 , m_state(new QuattroDosSpreadsheetInternal::State)
633 , m_asciiFile(parser.ascii())
634 {
635 }
636
637 QuattroDosSpreadsheet::~QuattroDosSpreadsheet()
638 {
639 }
640
641 int QuattroDosSpreadsheet::version() const
642 {
643 if (m_state->m_version<0)
644 m_state->m_version=m_mainParser.version();
645 return m_state->m_version;
646 }
647
648 bool QuattroDosSpreadsheet::checkFilePosition(long pos)
649 {
650 if (m_state->m_eof < 0)
651 {
652 long actPos = m_input->tell();
653 m_input->seek(0, librevenge::RVNG_SEEK_END);
654 m_state->m_eof=m_input->tell();
655 m_input->seek(actPos, librevenge::RVNG_SEEK_SET);
656 }
657 return pos <= m_state->m_eof;
658 }
659
660 int QuattroDosSpreadsheet::getNumSpreadsheets() const
661 {
662 return m_state->getMaximalSheet(QuattroDosSpreadsheetInternal::Spreadsheet::T_Spreadsheet)+1;
663 }
664
665 librevenge::RVNGString QuattroDosSpreadsheet::getSheetName(int id) const
666 {
667 return m_state->getSheetName(id);
668 }
669
670 ////////////////////////////////////////////////////////////
671 // low level
672
673 ////////////////////////////////////////////////////////////
674 // parse sheet data
675 ////////////////////////////////////////////////////////////
676 bool QuattroDosSpreadsheet::readSheetSize()
677 {
678 libwps::DebugStream f;
679 long pos = m_input->tell();
680 long type = libwps::read16(m_input);
681 if (type != 0x6)
682 {
683 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSheetSize: not a sheet zone\n"));
684 return false;
685 }
686 long sz = libwps::readU16(m_input);
687 int const vers=version();
688 if (sz < (vers>=2 ? 12 : 8))
689 {
690 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSheetSize: block is too short\n"));
691 return false;
692 }
693 f << "Entries(SheetSize):";
694 for (int i = 0; i < (vers>=2 ? 3 : 2); i++) // always 2 zero ?
695 {
696 int val = libwps::read16(m_input);
697 if (!val) continue;
698 f << "f" << i << "=" << std::hex << val << std::dec << ",";
699 }
700 int nCol = libwps::read16(m_input)+1;
701 f << "nCols=" << nCol << ",";
702 int nRow = libwps::read16(m_input);
703 f << "nRow=" << nRow << ",";
704 int nSheet = vers<=1 ? 0 : libwps::read16(m_input);
705 if (nSheet>0)
706 f << "nSheet=" << nSheet << ",";
707 ascii().addPos(pos);
708 ascii().addNote(f.str().c_str());
709 // empty spreadsheet
710 if (nRow==-1 && nCol==0) return true;
711 if (nRow < 0 || nCol <= 0) return false;
712 m_state->getActualSheet().setColumnWidth(nCol-1);
713 return true;
714
715 }
716
717 bool QuattroDosSpreadsheet::readRowSize()
718 {
719 libwps::DebugStream f;
720 long pos = m_input->tell();
721 long type = libwps::read16(m_input);
722 if (type != 0xe0)
723 {
724 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readRowSize: not a row size zone\n"));
725 return false;
726 }
727 long sz = libwps::readU16(m_input);
728 if (sz < 3)
729 {
730 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readRowSize: block is too short\n"));
731 return false;
732 }
733
734 int row = libwps::read16(m_input);
735 int height = libwps::readU8(m_input);
736
737 bool ok = row >= 0;
738 f << "Entries(Row):Row" << row << "";
739 if (!ok) f << "###";
740 f << ":height=" << height << ",";
741
742 if (ok)
743 m_state->getActualSheet().setRowHeight(row, height); // point to TWIP
744 ascii().addPos(pos);
745 ascii().addNote(f.str().c_str());
746
747 return ok;
748 }
749
750 bool QuattroDosSpreadsheet::readColumnSize()
751 {
752 libwps::DebugStream f;
753 long pos = m_input->tell();
754 long type = libwps::read16(m_input);
755 if (type != 0x8 && type != 0xe2)
756 {
757 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readColumnSize: not a column size zone\n"));
758 return false;
759 }
760 long sz = libwps::readU16(m_input);
761 if (sz < 3)
762 {
763 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readColumnSize: block is too short\n"));
764 return false;
765 }
766
767 int col = libwps::read16(m_input);
768 int width = libwps::readU8(m_input);
769
770 bool ok = col >= 0 && col < m_state->getActualSheet().m_numCols+10;
771 f << "Entries(Column):Col" << col << "";
772 if (!ok) f << "###";
773 f << ":width=" << width << ",";
774
775 if (ok)
776 {
777 if (col >= m_state->getActualSheet().m_numCols)
778 {
779 static bool first = true;
780 if (first)
781 {
782 first = false;
783 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readColumnSize: I must increase the number of columns\n"));
784 }
785 f << "#col[inc],";
786 }
787 // checkme: unit in character(?) -> TWIP
788 m_state->getActualSheet().setColumnWidth(col, width*105);
789 }
790 ascii().addPos(pos);
791 ascii().addNote(f.str().c_str());
792
793 return ok;
794 }
795
796 bool QuattroDosSpreadsheet::readHiddenColumns()
797 {
798 libwps::DebugStream f;
799 long pos = m_input->tell();
800 long type = libwps::read16(m_input);
801 if (type != 0x64)
802 {
803 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readHiddenColumns: not a column hidden zone\n"));
804 return false;
805 }
806 long sz = libwps::readU16(m_input);
807 if (sz != 32)
808 {
809 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readHiddenColumns: block size seems bad\n"));
810 return false;
811 }
812
813 f << "Entries(HiddenCol):";
814 for (int i=0; i<32; ++i)
815 {
816 auto val=int(libwps::readU8(m_input));
817 if (!val) continue;
818 static bool first=true;
819 if (first)
820 {
821 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readHiddenColumns: find some hidden col, ignored\n"));
822 first=false;
823 }
824 // checkme
825 for (int j=0, depl=1; j<8; ++j, depl<<=1)
826 {
827 if (val&depl)
828 f << "col" << j+8*i << "[hidden],";
829 }
830 }
831 ascii().addPos(pos);
832 ascii().addNote(f.str().c_str());
833
834 return true;
835 }
836
837 bool QuattroDosSpreadsheet::readCellStyle()
838 {
839 libwps::DebugStream f;
840 long pos = m_input->tell();
841 long type = libwps::read16(m_input);
842 int const vers=version();
843 if (type != 0xd8)
844 {
845 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCellStyle: not a style zone\n"));
846 return false;
847 }
848 long sz = libwps::readU16(m_input);
849 f << "Entries(CellStyle):";
850 if ((vers==1&&(sz%12)!=0) || (vers>1 && sz!=0x16))
851 {
852 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCellStyle: size seems bad\n"));
853 f << "###";
854 ascii().addPos(pos);
855 ascii().addNote(f.str().c_str());
856 return true;
857 }
858 if (vers>1)
859 {
860 auto pId=int(libwps::readU16(m_input));
861 QuattroDosSpreadsheetInternal::Style style(m_mainParser.getDefaultFontType());
862 auto id=int(libwps::readU16(m_input));
863 f.str("");
864 if (!m_state->m_styleManager.get(pId, style))
865 f << "###";
866 if (pId!=0xFF0F)
867 {
868 f << "Ce" << (pId>>8);
869 if (pId&0xFF) f << "-" << (pId&0xFF);
870 f << "[parent],";
871 }
872 int val;
873 for (int i=0; i<4; ++i) // small int, unknown
874 {
875 val=int(libwps::readU8(m_input));
876 if (val) f << "f" << i << "=" << val << ",";
877 }
878 WPSFont font;
879 auto flags = int(libwps::readU16(m_input));
880 uint32_t attributes = 0;
881 if (flags & 1) attributes |= WPS_BOLD_BIT;
882 if (flags & 2) attributes |= WPS_ITALICS_BIT;
883 if (flags & 8) attributes |= WPS_UNDERLINE_BIT;
884
885 font.m_attributes=attributes;
886 if (flags & 0xFFF4)
887 f << "fl=" << std::hex << (flags & 0xFFF4) << std::dec << ",";
888 auto fId=int(libwps::readU16(m_input));
889 f << "fId=" << fId << ",";
890 auto fSize = int(libwps::readU16(m_input));
891 if (fSize >= 1 && fSize <= 50)
892 font.m_size=double(fSize);
893 else
894 f << "###fSize=" << fSize << ",";
895 auto color = int(libwps::readU16(m_input));
896 if (color && !m_mainParser.getColor(color, font.m_color))
897 {
898 WPS_DEBUG_MSG(("QuattroDosParser::readCellStyle: unknown color\n"));
899 f << "##color=" << color << ",";
900 }
901 style.setFont(font);
902
903 val=int(libwps::readU8(m_input));
904 if (val)
905 {
906 for (int i=0, depl=0; i<4; ++i, depl+=2)
907 {
908 int bd=(val>>depl)&3;
909 if (!bd) continue;
910 WPSBorder border;
911 switch (bd)
912 {
913 case 1: // normal
914 break;
915 case 2:
916 border.m_type = WPSBorder::Double;
917 break;
918 case 3:
919 border.m_width=2;
920 break;
921 default:
922 break;
923 }
924 int const wh[]= {WPSBorder::TopBit, WPSBorder::LeftBit, WPSBorder::BottomBit, WPSBorder::RightBit};
925 style.setBorders(wh[i], border);
926 }
927 }
928 val=int(libwps::readU8(m_input)); // 8|10|18
929 if (val) f << "g0=" << val << ",";
930 val=int(libwps::readU8(m_input));
931 switch (val)
932 {
933 case 0:
934 break;
935 case 1:
936 style.setBackgroundColor(WPSColor(0x80,0x80,0x80));
937 break;
938 case 2:
939 style.setBackgroundColor(WPSColor::black());
940 break;
941 default:
942 if ((val&3)==3)
943 {
944 WPSColor col;
945 if (!m_mainParser.getColor(val>>2, col))
946 {
947 WPS_DEBUG_MSG(("QuattroDosParser::readCellStyle: unknown background color\n"));
948 f << "##color=" << val << ",";
949 }
950 else
951 style.setBackgroundColor(col);
952 }
953 else
954 f << "##background=" << val << ",";
955 break;
956 }
957 style.m_fileFormat=int(libwps::readU8(m_input));
958 for (int i=0; i<2; ++i) // g1=0|f8
959 {
960 val=int(libwps::readU8(m_input));
961 if (val) f << "g" << i+1 << "=" << val << ",";
962 }
963
964 style.m_extra=f.str();
965 m_state->m_styleManager.add(id, style);
966 f.str("");
967 f << "Entries(CellStyle):Ce" << (id>>8);
968 if (id&0xFF)
969 f << "-" << (id&0xFF) << "#";
970 f << "," << style << ",";
971
972 ascii().addPos(pos);
973 ascii().addNote(f.str().c_str());
974 return true;
975 }
976 ascii().addPos(pos);
977 ascii().addNote(f.str().c_str());
978 auto N=int(sz/12);
979 for (int i=0; i<N; ++i)
980 {
981 pos=m_input->tell();
982 QuattroDosSpreadsheetInternal::Style style(m_mainParser.getDefaultFontType());
983 f.str("");
984 auto id=int(libwps::readU16(m_input));
985
986 WPSFont font;
987 auto flags = int(libwps::readU16(m_input));
988 uint32_t attributes = 0;
989 if (flags & 1) attributes |= WPS_BOLD_BIT;
990 if (flags & 2) attributes |= WPS_ITALICS_BIT;
991 if (flags & 8) attributes |= WPS_UNDERLINE_BIT;
992
993 font.m_attributes=attributes;
994 if (flags & 0xFFF4)
995 f << "fl=" << std::hex << (flags & 0xFFF4) << std::dec << ",";
996 auto fId=int(libwps::readU16(m_input));
997 f << "fId=" << fId << ",";
998 auto fSize = int(libwps::readU16(m_input));
999 if (fSize >= 1 && fSize <= 50)
1000 font.m_size=double(fSize);
1001 else
1002 f << "###fSize=" << fSize << ",";
1003 auto color = int(libwps::readU16(m_input));
1004 if (color && !m_mainParser.getColor(color, font.m_color))
1005 {
1006 WPS_DEBUG_MSG(("QuattroDosParser::readCellStyle: unknown color\n"));
1007 f << "##color=" << color << ",";
1008 }
1009 style.setFont(font);
1010 auto val=int(libwps::readU16(m_input));
1011 f << "f0=" << std::hex << val << std::dec << ",";
1012
1013 style.m_extra=f.str();
1014 m_state->m_styleManager.add(id, style);
1015 f.str("");
1016 f << "CellStyle-" << i << ":Ce" << id << "," << style << ",";
1017
1018 ascii().addPos(pos);
1019 ascii().addNote(f.str().c_str());
1020 m_input->seek(pos+12, librevenge::RVNG_SEEK_SET);
1021 }
1022 return true;
1023 }
1024
1025 bool QuattroDosSpreadsheet::readCellProperty()
1026 {
1027 libwps::DebugStream f;
1028 long pos = m_input->tell();
1029 long type = libwps::read16(m_input);
1030 auto defFontType=m_mainParser.getDefaultFontType();
1031
1032 if (type != 0x9d)
1033 {
1034 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCellProperty: not a cell property zone zone\n"));
1035 return false;
1036 }
1037 f << "Entries(CellProperty):";
1038 long sz = libwps::readU16(m_input);
1039 if (sz!=7)
1040 {
1041 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCellProperty: the size seems bad\n"));
1042 f << "###sz";
1043 ascii().addPos(pos);
1044 ascii().addNote(f.str().c_str());
1045 return true;
1046 }
1047 auto format=int(libwps::readU8(m_input));
1048 auto col=int(libwps::read16(m_input));
1049 auto row=int(libwps::read16(m_input));
1050 QuattroDosSpreadsheetInternal::Cell emptyCell(defFontType);
1051 auto *cell=&emptyCell;
1052 if (col<0 || row<0)
1053 {
1054 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCellProperty: the position seems bad\n"));
1055 f << "###";
1056 }
1057 else
1058 cell = &m_state->getActualSheet().getCell(Vec2i(col,row),defFontType);
1059 f << "C" << Vec2i(col,row) << ",";
1060 auto flag=int(libwps::readU8(m_input));
1061 auto id=int(libwps::readU8(m_input));
1062 if (id&0x80)
1063 {
1064 f << "Ce" << (id&0x7f) << ",";
1065 QuattroDosSpreadsheetInternal::Style style(cell->m_fontType);
1066 if (!m_state->m_styleManager.get((id&0x7f), style))
1067 {
1068 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCellProperty: can not find a style\n"));
1069 f << "###";
1070 }
1071 else
1072 {
1073 if (style.m_fileFormat==0xFF)
1074 cell->m_fileFormat=style.m_fileFormat;
1075 cell->m_fontType=style.m_fontType;
1076 cell->setFont(style.getFont());
1077 cell->setBackgroundColor(style.backgroundColor());
1078 if (style.hasBorders())
1079 cell->setBorders(style.borders());
1080 }
1081 }
1082 else if (id)
1083 {
1084 if (id&0x7c)
1085 f << "Fo" << (id>>2) << ",";
1086 WPSFont font;
1087 if (!m_mainParser.getFont(id>>2, font, cell->m_fontType))
1088 {
1089 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCellProperty: can not find a font\n"));
1090 f << "###";
1091 }
1092 else
1093 cell->setFont(font);
1094 if (id&0x3)
1095 f << "f0=" << (id&3) << ",";
1096 }
1097 if (format!=0xFF)
1098 {
1099 cell->m_fileFormat=format;
1100 f << "form=" << std::hex << format << std::dec << ",";
1101 }
1102 switch (flag>>6)
1103 {
1104 case 1:
1105 cell->setHAlignment(WPSCellFormat::HALIGN_LEFT);
1106 f << "left,";
1107 break;
1108 case 2:
1109 cell->setHAlignment(WPSCellFormat::HALIGN_RIGHT);
1110 f << "right,";
1111 break;
1112 case 3:
1113 cell->setHAlignment(WPSCellFormat::HALIGN_CENTER);
1114 f << "center,";
1115 break;
1116 default:
1117 case 0:
1118 break;
1119 }
1120 for (int i=0; i<2; ++i)
1121 {
1122 int bd=(flag>>(2*i))&0x3;
1123 if (!bd) continue;
1124 f << "bord" << (i==0 ? "T" : "L");
1125 WPSBorder border;
1126 switch (bd)
1127 {
1128 case 2:
1129 border.m_type = WPSBorder::Double;
1130 f << "[double]";
1131 break;
1132 case 3:
1133 border.m_width=2;
1134 f << "[w=2]";
1135 break;
1136 default:
1137 case 1: // normal
1138 break;
1139 }
1140 f << ",";
1141 cell->setBorders(i==0 ? WPSBorder::TopBit : WPSBorder::LeftBit, border);
1142 }
1143 switch ((flag>>4)&3)
1144 {
1145 case 1:
1146 cell->setBackgroundColor(WPSColor(0x80,0x80,0x80));
1147 f << "back[grey],";
1148 break;
1149 case 2:
1150 cell->setBackgroundColor(WPSColor::black());
1151 f << "back[black],";
1152 break;
1153 case 3:
1154 f << "#back[color]=3";
1155 break;
1156 case 0:
1157 default:
1158 break;
1159 }
1160 ascii().addPos(pos);
1161 ascii().addNote(f.str().c_str());
1162 return true;
1163 }
1164
1165 bool QuattroDosSpreadsheet::readUserStyle()
1166 {
1167 libwps::DebugStream f;
1168 long pos = m_input->tell();
1169 long type = libwps::read16(m_input);
1170 int const vers=version();
1171 if (type != 0xc9)
1172 {
1173 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readUserStyle: not a style zone\n"));
1174 return false;
1175 }
1176 long sz = libwps::readU16(m_input);
1177 f << "Entries(UserStyle):";
1178 if ((vers==1&&sz!=0x2a) || (vers>1 && sz!=0x24))
1179 {
1180 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readUserStyle: size seems bad\n"));
1181 f << "###";
1182 ascii().addPos(pos);
1183 ascii().addNote(f.str().c_str());
1184 return true;
1185 }
1186 if (vers>1)
1187 {
1188 f.str("");
1189 librevenge::RVNGString name("");
1190 if (!m_mainParser.readPString(name,15))
1191 f << "##sSz,";
1192 else if (!name.empty())
1193 f << name.cstr() << ",";
1194 m_input->seek(pos+20, librevenge::RVNG_SEEK_SET);
1195
1196 QuattroDosSpreadsheetInternal::Style style(m_mainParser.getDefaultFontType());
1197 auto id=int(libwps::readU16(m_input));
1198 int val;
1199 for (int i=0; i<4; ++i) // small int, unknown
1200 {
1201 val=int(libwps::readU8(m_input));
1202 if (val) f << "f" << i << "=" << val << ",";
1203 }
1204
1205 WPSFont font;
1206 auto flags = int(libwps::readU16(m_input));
1207 uint32_t attributes = 0;
1208 if (flags & 1) attributes |= WPS_BOLD_BIT;
1209 if (flags & 2) attributes |= WPS_ITALICS_BIT;
1210 if (flags & 8) attributes |= WPS_UNDERLINE_BIT;
1211
1212 font.m_attributes=attributes;
1213 if (flags & 0xFFF4)
1214 f << "fl=" << std::hex << (flags & 0xFFF4) << std::dec << ",";
1215 auto fId=int(libwps::readU16(m_input));
1216 f << "fId=" << fId << ",";
1217 auto fSize = int(libwps::readU16(m_input));
1218 if (fSize >= 1 && fSize <= 50)
1219 font.m_size=double(fSize);
1220 else
1221 f << "###fSize=" << fSize << ",";
1222 auto color = int(libwps::readU16(m_input));
1223 if (color && !m_mainParser.getColor(color, font.m_color))
1224 {
1225 WPS_DEBUG_MSG(("QuattroDosParser::readCellStyle: unknown color\n"));
1226 f << "##color=" << color << ",";
1227 }
1228 style.setFont(font);
1229 val=int(libwps::readU8(m_input));
1230 if (val)
1231 {
1232 for (int i=0, depl=0; i<4; ++i, depl+=2)
1233 {
1234 int bd=(val>>depl)&3;
1235 if (!bd) continue;
1236 WPSBorder border;
1237 switch (bd)
1238 {
1239 case 1: // normal
1240 break;
1241 case 2:
1242 border.m_type = WPSBorder::Double;
1243 break;
1244 case 3:
1245 border.m_width=2;
1246 break;
1247 default:
1248 break;
1249 }
1250 int const wh[]= {WPSBorder::TopBit, WPSBorder::LeftBit, WPSBorder::BottomBit, WPSBorder::RightBit};
1251 style.setBorders(wh[i], border);
1252 }
1253 }
1254 val=int(libwps::readU8(m_input)); // 8|10|18
1255 if (val) f << "g0=" << val << ",";
1256 val=int(libwps::readU8(m_input));
1257 switch (val)
1258 {
1259 case 0:
1260 break;
1261 case 1:
1262 style.setBackgroundColor(WPSColor(0x80,0x80,0x80));
1263 break;
1264 case 2:
1265 style.setBackgroundColor(WPSColor::black());
1266 break;
1267 default:
1268 f << "##background=" << val << ",";
1269 break;
1270 }
1271 style.m_fileFormat=int(libwps::readU8(m_input));
1272 for (int i=0; i<2; ++i) // g1=0|f8
1273 {
1274 val=int(libwps::readU8(m_input));
1275 if (val) f << "g" << i+1 << "=" << val << ",";
1276 }
1277
1278 style.m_extra=f.str();
1279 m_state->m_styleManager.add(id, style);
1280 f.str("");
1281 f << "Entries(UserStyle):";
1282 if (id!=0xFF0F)
1283 {
1284 f << "Ce" << (id>>8) << "-" << (id&0xFF);
1285 if ((id&0xFF)!=8)
1286 f << "#";
1287 }
1288 f << "," << style << ",";
1289
1290 ascii().addPos(pos);
1291 ascii().addNote(f.str().c_str());
1292 return true;
1293 }
1294
1295 QuattroDosSpreadsheetInternal::Style style(m_mainParser.getDefaultFontType());
1296 auto id=int(libwps::readU16(m_input));
1297 f.str("");
1298 auto flags = int(libwps::readU16(m_input));
1299
1300 WPSFont font;
1301 uint32_t attributes = 0;
1302 if (flags & 1) attributes |= WPS_BOLD_BIT;
1303 if (flags & 2) attributes |= WPS_ITALICS_BIT;
1304 if (flags & 8) attributes |= WPS_UNDERLINE_BIT;
1305
1306 font.m_attributes=attributes;
1307 if (flags & 0xFFF4)
1308 f << "font[fl]=" << std::hex << (flags & 0xFFF4) << std::dec << ",";
1309 auto fId=int(libwps::readU16(m_input));
1310 f << "fId=" << fId << ",";
1311 auto fSize = int(libwps::readU16(m_input));
1312 if (fSize >= 1 && fSize <= 50)
1313 font.m_size=double(fSize);
1314 else
1315 f << "###fSize=" << fSize << ",";
1316 auto color = int(libwps::readU16(m_input));
1317 if (color && !m_mainParser.getColor(color, font.m_color))
1318 {
1319 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readUserStyle: unknown color\n"));
1320 f << "##font[color]=" << color << ",";
1321 }
1322 style.setFont(font);
1323 auto val=int(libwps::read16(m_input));
1324 if (val!=-1) f << "f1=" << val << ",";
1325 librevenge::RVNGString name("");
1326 if (!m_mainParser.readPString(name,15))
1327 f << "##sSz,";
1328 else if (!name.empty())
1329 f << name.cstr() << ",";
1330 m_input->seek(pos+32, librevenge::RVNG_SEEK_SET);
1331 val=int(libwps::readU16(m_input)); // probably list of modified flags
1332 if (val)
1333 f << "wh=" << std::hex << val << std::dec << ",";
1334 for (int i=0; i<4; ++i)
1335 {
1336 val=int(libwps::readU8(m_input));
1337 if (!val) continue;
1338 WPSBorder border;
1339 switch (val)
1340 {
1341 case 1: // normal
1342 break;
1343 case 2:
1344 border.m_type = WPSBorder::Double;
1345 break;
1346 case 3:
1347 border.m_width=2;
1348 break;
1349 default:
1350 f << "#border" << i << "=" << val << ",";
1351 break;
1352 }
1353 int const wh[]= {WPSBorder::TopBit, WPSBorder::LeftBit, WPSBorder::BottomBit, WPSBorder::RightBit};
1354 style.setBorders(wh[i], border);
1355 }
1356 val=int(libwps::readU8(m_input));
1357 switch (val)
1358 {
1359 case 0:
1360 break;
1361 case 1:
1362 style.setBackgroundColor(WPSColor(0x80,0x80,0x80));
1363 break;
1364 case 2:
1365 style.setBackgroundColor(WPSColor::black());
1366 break;
1367 default:
1368 f << "#back[color]=" << val << ",";
1369 break;
1370 }
1371 val=int(libwps::readU8(m_input));
1372 switch (val)
1373 {
1374 case 0:
1375 break;
1376 case 1:
1377 style.setHAlignment(WPSCellFormat::HALIGN_LEFT);
1378 break;
1379 case 2:
1380 style.setHAlignment(WPSCellFormat::HALIGN_RIGHT);
1381 break;
1382 case 3:
1383 style.setHAlignment(WPSCellFormat::HALIGN_CENTER);
1384 break;
1385 default:
1386 f << "#align=" << val << ",";
1387 break;
1388 }
1389 val=int(libwps::readU8(m_input));
1390 switch (val)
1391 {
1392 case 0:
1393 break;
1394 case 1:
1395 f << "input=labels[only],";
1396 break;
1397 case 2:
1398 f << "input=dates[only],";
1399 break;
1400 default:
1401 f << "#input=" << val << ",";
1402 break;
1403 }
1404 style.m_fileFormat=int(libwps::readU8(m_input));
1405 for (int i=0; i<3; ++i)
1406 {
1407 val=int(libwps::read8(m_input));
1408 if (val) f << "f" << i+2 << "=" << val << ",";
1409 }
1410
1411 style.m_extra=f.str();
1412 m_state->m_styleManager.add(id, style);
1413 f.str("");
1414 f << "Entries(UserStyle):Ce" << id << "," << style << ",";
1415 ascii().addPos(pos);
1416 ascii().addNote(f.str().c_str());
1417 return true;
1418 }
1419
1420 ////////////////////////////////////////////////////////////
1421 // general
1422 ////////////////////////////////////////////////////////////
1423
1424 bool QuattroDosSpreadsheet::readCell()
1425 {
1426 libwps::DebugStream f;
1427
1428 long pos = m_input->tell();
1429 long type = libwps::read16(m_input);
1430 if (type < 0xc || type > 0x10)
1431 {
1432 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCell: not a cell property\n"));
1433 return false;
1434 }
1435 long sz = libwps::readU16(m_input);
1436 long endPos = pos+4+sz;
1437
1438 if (sz < 5)
1439 {
1440 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCell: cell def is too short\n"));
1441 return false;
1442 }
1443 int const vers=version();
1444 auto defFontType=m_mainParser.getDefaultFontType();
1445 bool dosFile = vers<=1;
1446 int format=0xFF;
1447 if (dosFile)
1448 format = int(libwps::readU8(m_input));
1449 int cellPos[2];
1450 cellPos[0]=int(libwps::readU8(m_input));
1451 auto sheetId=int(libwps::readU8(m_input));
1452 cellPos[1]=int(libwps::read16(m_input));
1453 if (cellPos[1] < 0)
1454 {
1455 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCell: cell pos is bad\n"));
1456 return false;
1457 }
1458 if (sheetId)
1459 {
1460 if (vers==1)
1461 {
1462 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCell: find unexpected sheet id\n"));
1463 f << "###";
1464 }
1465 f << "sheet[id]=" << sheetId << ",";
1466 }
1467
1468 auto &cell=m_state->getActualSheet().getCell(Vec2i(cellPos[0],cellPos[1]), defFontType);
1469 cell.m_fileFormat=format;
1470 if (!dosFile)
1471 {
1472 auto id=int(libwps::readU16(m_input));
1473 if (id!=0xFF0F)
1474 {
1475 f << "Ce" << (id>>8);
1476 if (id&0xFF) f << "-" << (id&0xFF);
1477 f << ",";
1478 }
1479 QuattroDosSpreadsheetInternal::Style style(defFontType);
1480 if (!m_state->m_styleManager.get(id, style))
1481 f << "###";
1482 else
1483 {
1484 cell.m_fileFormat=style.m_fileFormat;
1485 cell.m_fontType=style.m_fontType;
1486 cell.setFont(style.getFont());
1487 cell.setBackgroundColor(style.backgroundColor());
1488 if (style.hasBorders())
1489 cell.setBorders(style.borders());
1490 }
1491 }
1492
1493 long dataPos = m_input->tell();
1494 auto dataSz = int(endPos-dataPos);
1495
1496 bool ok = true;
1497 switch (type)
1498 {
1499 case 12:
1500 {
1501 if (dataSz == 0)
1502 {
1503 cell.m_content.m_contentType=WKSContentListener::CellContent::C_NONE;
1504 break;
1505 }
1506 ok = false;
1507 break;
1508 }
1509 case 13:
1510 {
1511 if (dataSz == 2)
1512 {
1513 cell.m_content.m_contentType=WKSContentListener::CellContent::C_NUMBER;
1514 cell.m_content.setValue(libwps::read16(m_input));
1515 break;
1516 }
1517 ok = false;
1518 break;
1519 }
1520 case 14:
1521 {
1522 double val;
1523 bool isNaN;
1524 if (dataSz == 8 && libwps::readDouble8(m_input, val, isNaN))
1525 {
1526 cell.m_content.m_contentType=WKSContentListener::CellContent::C_NUMBER;
1527 cell.m_content.setValue(val);
1528 break;
1529 }
1530 ok = false;
1531 break;
1532 }
1533 case 15:
1534 {
1535 cell.m_content.m_contentType=WKSContentListener::CellContent::C_TEXT;
1536 long begText=m_input->tell()+2, endText=begText+dataSz-2;
1537 // pascal string
1538 auto align=char(libwps::readU8(m_input));
1539 if (align=='\'') cell.setHAlignment(WPSCellFormat::HALIGN_DEFAULT);
1540 else if (align=='\\') cell.setHAlignment(WPSCellFormat::HALIGN_LEFT);
1541 else if (align=='^') cell.setHAlignment(WPSCellFormat::HALIGN_CENTER);
1542 else if (align=='\"') cell.setHAlignment(WPSCellFormat::HALIGN_RIGHT);
1543 else f << "#align=" << int(align) << ",";
1544
1545 librevenge::RVNGString text("");
1546 if (!m_mainParser.readPString(text,dataSz-2))
1547 f << "##sSz,";
1548 else
1549 {
1550 if (endText!=m_input->tell())
1551 {
1552 endText=m_input->tell();
1553 f << "#extra,";
1554 ascii().addDelimiter(m_input->tell(), '|');
1555 }
1556 if (!text.empty())
1557 f << text.cstr() << ",";
1558 }
1559 cell.m_content.m_textEntry.setBegin(begText);
1560 cell.m_content.m_textEntry.setEnd(endText);
1561 break;
1562 }
1563 case 16:
1564 {
1565 double val;
1566 bool isNaN;
1567 if (dataSz >= 8 && libwps::readDouble8(m_input, val, isNaN))
1568 {
1569 cell.m_content.m_contentType=WKSContentListener::CellContent::C_FORMULA;
1570 cell.m_content.setValue(val);
1571 std::string error;
1572 if (!readFormula(endPos, cell.position(), sheetId, cell.m_content.m_formula, error))
1573 {
1574 cell.m_content.m_contentType=WKSContentListener::CellContent::C_NUMBER;
1575 ascii().addDelimiter(m_input->tell()-1, '#');
1576 }
1577 if (error.length()) f << error;
1578 break;
1579 }
1580 ok = false;
1581 break;
1582 }
1583 default:
1584 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCell: unknown type=%ld\n", type));
1585 ok = false;
1586 break;
1587 }
1588 if (!ok) ascii().addDelimiter(dataPos, '#');
1589
1590 if (dosFile)
1591 {
1592 switch (cell.m_content.m_contentType)
1593 {
1594 case WKSContentListener::CellContent::C_NONE:
1595 break;
1596 case WKSContentListener::CellContent::C_TEXT:
1597 cell.setFormat(WPSCell::F_TEXT);
1598 break;
1599 case WKSContentListener::CellContent::C_NUMBER:
1600 case WKSContentListener::CellContent::C_FORMULA:
1601 case WKSContentListener::CellContent::C_UNKNOWN:
1602 default:
1603 cell.setFormat(WPSCell::F_NUMBER);
1604 break;
1605 }
1606
1607 }
1608 m_input->seek(pos+sz, librevenge::RVNG_SEEK_SET);
1609
1610 std::string extra=f.str();
1611 f.str("");
1612 f << "Entries(CellContent):" << cell << "," << extra;
1613
1614 ascii().addPos(pos);
1615 ascii().addNote(f.str().c_str());
1616
1617 return true;
1618 }
1619
1620 bool QuattroDosSpreadsheet::readCellFormulaResult()
1621 {
1622 libwps::DebugStream f;
1623
1624 long pos = m_input->tell();
1625 long type = libwps::read16(m_input);
1626 if (type != 0x33)
1627 {
1628 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCellFormulaResult: not a cell property\n"));
1629 return false;
1630 }
1631 long sz = libwps::readU16(m_input);
1632 if (sz<6)
1633 {
1634 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCellFormulaResult: the zone seems to short\n"));
1635 return false;
1636 }
1637 long endPos = pos+4+sz;
1638
1639 bool dosFile = version() <= 1;
1640 // skip format for dosFile
1641 m_input->seek(dosFile ? pos+5 : pos+4, librevenge::RVNG_SEEK_SET);
1642 f << "CellContent[res]:";
1643 int dim[2];
1644 for (int &i : dim) i=int(libwps::readU16(m_input));
1645 f << "C" << dim[0] << "x" << dim[1] << ",";
1646 // skip format for windows file
1647 if (!dosFile) m_input->seek(2, librevenge::RVNG_SEEK_CUR);
1648 librevenge::RVNGString text("");
1649 if (!m_mainParser.readPString(text,endPos-m_input->tell()-1))
1650 f << "##sSz,";
1651 else if (!text.empty())
1652 f << text.cstr() << ",";
1653 ascii().addPos(pos);
1654 ascii().addNote(f.str().c_str());
1655 return true;
1656 }
1657
1658 ////////////////////////////////////////////////////////////
1659 // Data
1660 ////////////////////////////////////////////////////////////
1661 bool QuattroDosSpreadsheet::readSpreadsheetName()
1662 {
1663 libwps::DebugStream f;
1664
1665 long pos = m_input->tell();
1666 auto type = long(libwps::readU16(m_input));
1667 if (type != 0xde)
1668 {
1669 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSpreadsheetName: not a spreadsheet header\n"));
1670 return false;
1671 }
1672 auto sz = int(libwps::readU16(m_input));
1673 f << "Entries(SheetName):";
1674 librevenge::RVNGString name("");
1675 if (!m_mainParser.readPString(name,sz-1))
1676 f << "##sSz,";
1677 else if (!name.empty())
1678 {
1679 f << name.cstr() << ",";
1680 if (m_state->m_spreadsheetStack.empty() || !m_state->m_spreadsheetStack.top())
1681 {
1682 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSpreadsheetName: can not find the current sheet\n"));
1683 f << "###current";
1684 }
1685 else if (m_state->m_idToSheetNameMap.find(m_state->getActualSheet().m_id)!=m_state->m_idToSheetNameMap.end())
1686 {
1687 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSpreadsheetName: id=%d dupplicated\n", m_state->getActualSheet().m_id));
1688 f << "###id";
1689 }
1690 else
1691 m_state->m_idToSheetNameMap[m_state->getActualSheet().m_id]=m_state->getUniqueSheetName(name);
1692 }
1693 ascii().addPos(pos);
1694 ascii().addNote(f.str().c_str());
1695 return true;
1696 }
1697
1698 bool QuattroDosSpreadsheet::readSpreadsheetOpen()
1699 {
1700 libwps::DebugStream f;
1701
1702 long pos = m_input->tell();
1703 auto type = long(libwps::readU16(m_input));
1704 if (type != 0xdc)
1705 {
1706 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSpreadsheetOpen: not a spreadsheet header\n"));
1707 return false;
1708 }
1709 auto sz = long(libwps::readU16(m_input));
1710 f << "Entries(Spreadsheet)[beg]:";
1711 if (sz!=2)
1712 {
1713 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSpreadsheetOpen: the field size seems odd\n"));
1714 f << "###";
1715 }
1716 auto id=int(libwps::readU16(m_input));
1717 if (id<0||id>255)
1718 {
1719 f << "###";
1720 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSpreadsheetOpen: the id seems odd\n"));
1721 }
1722 else if (id==0 && m_state->m_spreadsheetStack.size()!=1)
1723 {
1724 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSpreadsheetOpen: find 0 but the stack is not empty\n"));
1725 f << "###stack";
1726 m_state->m_spreadsheetStack.push(m_state->m_spreadsheetList[0]);
1727 }
1728 else if (id)
1729 m_state->pushNewSheet(std::shared_ptr<QuattroDosSpreadsheetInternal::Spreadsheet>
1730 (new QuattroDosSpreadsheetInternal::Spreadsheet
1731 (QuattroDosSpreadsheetInternal::Spreadsheet::T_Spreadsheet, id)));
1732 f << id << ",";
1733 ascii().addPos(pos);
1734 ascii().addNote(f.str().c_str());
1735 return true;
1736 }
1737
1738 bool QuattroDosSpreadsheet::readSpreadsheetClose()
1739 {
1740 libwps::DebugStream f;
1741
1742 long pos = m_input->tell();
1743 auto type = long(libwps::readU16(m_input));
1744 if (type != 0xdd)
1745 {
1746 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSpreadsheetClose: not a spreadsheet header\n"));
1747 return false;
1748 }
1749 auto sz = long(libwps::readU16(m_input));
1750 f << "Entries(Spreadsheet)[end]:";
1751 auto const &sheet=m_state->getActualSheet();
1752 if (sheet.m_type!=QuattroDosSpreadsheetInternal::Spreadsheet::T_Spreadsheet)
1753 {
1754 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSpreadsheetClose: can not find spreadsheet spreadsheet\n"));
1755 f << "###[noOpen],";
1756 }
1757 else if (m_state->m_spreadsheetStack.size() > 1)
1758 m_state->popSheet();
1759 if (sz!=0)
1760 {
1761 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readSpreadsheetClose: the field size seems odd\n"));
1762 f << "###";
1763 }
1764 ascii().addPos(pos);
1765 ascii().addNote(f.str().c_str());
1766 return true;
1767 }
1768
1769 bool QuattroDosSpreadsheet::readCell
1770 (Vec2i actPos, WKSContentListener::FormulaInstruction &instr, bool hasSheetId, int sheetId)
1771 {
1772 instr=WKSContentListener::FormulaInstruction();
1773 instr.m_type=WKSContentListener::FormulaInstruction::F_Cell;
1774 bool ok = true;
1775 int pos[3]= {0,0,0};
1776 bool absolute[3] = { true, true, true};
1777 for (int dim = 0; dim < (hasSheetId ? 3 : 2); dim++)
1778 {
1779 auto val = int(libwps::readU16(m_input));
1780 if (dim==0 && (val&0xF00) && (val&0xF000)!=0xF000)
1781 {
1782 // checkme: probably (val>>8)&0x1f
1783 instr.m_fileName=m_mainParser.getFileName(int((val>>8)&0xF));
1784 val &= 0xF0FF;
1785 }
1786 if ((val & 0xF000) == 0); // absolute value ?
1787 else if ((val & 0xc000) == 0x8000) // relative ?
1788 {
1789 if (version()==1 || dim==0)
1790 {
1791 val &= 0xFF;
1792 if ((val & 0x80) && val+actPos[dim] >= 0x100)
1793 // sometimes this value is odd, so do not generate errors here
1794 val = val - 0x100;
1795 }
1796 else
1797 {
1798 val &= 0x3FFF;
1799 if (val>0x1000) val = val - 0x2000;
1800 }
1801 if (dim==2)
1802 val += sheetId;
1803 else
1804 val += actPos[dim];
1805 absolute[dim] = false;
1806 }
1807 else if (val==0xFFFF)
1808 {
1809 static bool first=true;
1810 if (first) // in general associated with a nan value, so maybe be normal
1811 {
1812 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCell: find some ffff cell\n"));
1813 first=false;
1814 }
1815 ok = false;
1816 }
1817 else
1818 {
1819 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCell: can not read cell %x\n", unsigned(val)));
1820 ok = false;
1821 }
1822 pos[dim] = val;
1823 }
1824
1825 if (pos[0] < 0 || pos[1] < 0)
1826 {
1827 if (ok)
1828 {
1829 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readCell: can not read cell position\n"));
1830 }
1831 return false;
1832 }
1833 if (pos[0]>255) // FIXME: can happens in some list cell, so...
1834 pos[0]&=0xFF;
1835 if (hasSheetId && pos[2]>255) // CHECKME: this can also happen, unsure why
1836 pos[2]%=256;
1837
1838 instr.m_position[0]=Vec2i(pos[0],pos[1]);
1839 if (hasSheetId && sheetId!=pos[2]) instr.m_sheetId[0]=pos[2];
1840 else if (!instr.m_fileName.empty()) instr.m_sheetId[0]=sheetId;
1841 instr.m_positionRelative[0]=Vec2b(!absolute[0],!absolute[1]);
1842 return ok;
1843 }
1844
1845 namespace QuattroDosSpreadsheetInternal
1846 {
1847 struct Functions
1848 {
1849 char const *m_name;
1850 int m_arity;
1851 };
1852
1853 static Functions const s_listFunctions[] =
1854 {
1855 { "", 0} /*SPEC: number*/, {"", 0}/*SPEC: cell*/, {"", 0}/*SPEC: cells*/, {"=", 1} /*SPEC: end of formula*/,
1856 { "(", 1} /* SPEC: () */, {"", 0}/*SPEC: number*/, { "", -2} /*SPEC: text*/, {"", -2}/*unused*/,
1857 { "-", 1}, {"+", 2}, {"-", 2}, {"*", 2},
1858 { "/", 2}, { "^", 2}, {"=", 2}, {"<>", 2},
1859
1860 { "<=", 2},{ ">=", 2},{ "<", 2},{ ">", 2},
1861 { "And", 2},{ "Or", 2}, { "Not", 1}, { "+", 1},
1862 { "&", 2}, { "", -2} /*unused*/,{ "", -2} /*unused*/,{ "", -2} /*unused*/,
1863 { "", -2} /*unused*/,{ "", -2} /*unused*/,{ "", -2} /*unused*/,{ "NA", 0} /*checkme*/,
1864
1865 { "NA", 0} /* Error*/,{ "Abs", 1},{ "Int", 1},{ "Sqrt", 1},
1866 { "Log10", 1},{ "Ln", 1},{ "Pi", 0},{ "Sin", 1},
1867 { "Cos", 1},{ "Tan", 1},{ "Atan2", 2},{ "Atan", 1},
1868 { "Asin", 1},{ "Acos", 1},{ "Exp", 1},{ "Mod", 2},
1869
1870 { "Choose", -1},{ "IsNa", 1},{ "IsError", 1},{ "False", 0},
1871 { "True", 0},{ "Rand", 0},{ "Date", 3},{ "Now", 0},
1872 { "PMT", 3} /*BAD*/,{ "PV", 3} /*BAD*/,{ "FV", 3} /*BAD*/,{ "IF", 3},
1873 { "Day", 1},{ "Month", 1},{ "Year", 1},{ "Round", 2},
1874
1875 { "Time", 3},{ "Hour", 1},{ "Minute", 1},{ "Second", 1},
1876 { "IsNumber", 1},{ "IsText", 1},{ "Len", 1},{ "Value", 1},
1877 { "Fixed", 2}, { "Mid", 3}, { "Char", 1},{ "Ascii", 1},
1878 { "Find", 3},{ "DateValue", 1} /*checkme*/,{ "TimeValue", 1} /*checkme*/,{ "CellPointer", 1} /*checkme*/,
1879
1880 { "Sum", -1},{ "Average", -1},{ "COUNT", -1},{ "Min", -1},
1881 { "Max", -1},{ "VLookUp", 3},{ "NPV", 2}, { "Var", -1},
1882 { "StDev", -1},{ "IRR", 2} /*BAD*/, { "HLookup", 3},{ "DSum", 3},
1883 { "DAvg", 3},{ "DCount", 3},{ "DMin", 3},{ "DMax", 3},
1884
1885 { "DVar", 3},{ "DStd", 3},{ "Index", 3}, { "Columns", 1},
1886 { "Rows", 1},{ "Rept", 2},{ "Upper", 1},{ "Lower", 1},
1887 { "Left", 2},{ "Right", 2},{ "Replace", 4}, { "Proper", 1},
1888 { "Cell", 1} /*checkme*/,{ "Trim", 1},{ "Clean", 1} /*UNKN*/,{ "T", 1},
1889
1890 { "IsNonText", 1},{ "Exact", 2},{ "", -2} /*UNKN*/,{ "Indirect", 1},
1891 { "RRI", 3}, { "TERM", 3}, { "CTERM", 3}, { "SLN", 3},
1892 { "SYD", 4},{ "DDB", 4}, { "StDevP", -1}, { "VarP", -1},
1893 { "DBStdDevP", -1}, { "DBVarP", -1}, { "PV", 3}, { "PMT", 5},
1894
1895 { "FV", 3}, { "Nper", 5}, { "Rate", 5}, { "Ipmt", 4},
1896 { "Ppmt", 6}, { "SumProduct", 2}, { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1897 { "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,
1898 { "", -2} /*UNKN*/,{ "And", -1},{ "Or", -1},{ "Not", 1},
1899
1900 };
1901 }
1902
1903 bool QuattroDosSpreadsheet::readFormula(long endPos, Vec2i const &position, int sheetId,
1904 std::vector<WKSContentListener::FormulaInstruction> &formula, std::string &error)
1905 {
1906 int const vers=version();
1907 formula.resize(0);
1908 error = "";
1909 long pos = m_input->tell();
1910 if (endPos - pos < (vers==1 ? 6 : 4)) return false;
1911 auto sz = int(libwps::readU16(m_input));
1912 if (endPos-pos-2 != sz) return false;
1913
1914 std::vector<WKSContentListener::FormulaInstruction> listCellsPos;
1915 int fieldPos[2]= {0,sz};
1916 for (int &i : fieldPos)
1917 {
1918 i= int(libwps::readU16(m_input));
1919 if (vers>1) break;
1920 }
1921 if (fieldPos[0]<0||fieldPos[0]>fieldPos[1]||fieldPos[1]>sz)
1922 {
1923 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readFormula: can not find the field header\n"));
1924 error="###fieldPos";
1925 return false;
1926 }
1927 int NSingle=0;
1928 if (fieldPos[0]!=sz)
1929 {
1930 m_input->seek(pos+2+fieldPos[0], librevenge::RVNG_SEEK_SET);
1931 ascii().addDelimiter(pos+2+fieldPos[0],'|');
1932 if (vers==1)
1933 {
1934 int N=int(sz-fieldPos[0])/4;
1935 NSingle=int(fieldPos[1]-fieldPos[0])/4;
1936 for (int i=0; i<N; ++i)
1937 {
1938 WKSContentListener::FormulaInstruction cell;
1939 if (!readCell(position, cell))
1940 {
1941 ascii().addDelimiter(pos+2+fieldPos[0]+i*4,'@');
1942 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readFormula: can not read some cell\n"));
1943 error="###cell,";
1944 break;
1945 }
1946 listCellsPos.push_back(cell);
1947 }
1948 }
1949 else
1950 {
1951 while (!m_input->isEnd())
1952 {
1953 long actPos=m_input->tell();
1954 if (actPos+8>endPos) break;
1955 auto type=int(libwps::readU16(m_input));
1956 WKSContentListener::FormulaInstruction cell, cell2;
1957 if (type==0)
1958 {
1959 if (!readCell(position, cell, true, sheetId))
1960 {
1961 m_input->seek(actPos, librevenge::RVNG_SEEK_SET);
1962 break;
1963 }
1964 listCellsPos.push_back(cell);
1965 continue;
1966 }
1967 if (type!=0x1000 || actPos+14>endPos || !readCell(position, cell, true, sheetId) ||
1968 !readCell(position, cell2, true, sheetId))
1969 {
1970 m_input->seek(actPos, librevenge::RVNG_SEEK_SET);
1971 break;
1972 }
1973 cell.m_type=WKSContentListener::FormulaInstruction::F_CellList;
1974 cell.m_position[1]=cell2.m_position[0];
1975 cell.m_positionRelative[1]=cell2.m_positionRelative[0];
1976 cell.m_sheetId[1]=cell2.m_sheetId[0];
1977 listCellsPos.push_back(cell);
1978 }
1979 if (m_input->tell() !=endPos)
1980 {
1981 ascii().addDelimiter(m_input->tell(),'@');
1982 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readFormula: can not read some cell\n"));
1983 error="###cell,";
1984 }
1985 }
1986 m_input->seek(pos+4+(vers==1 ? 2 : 0), librevenge::RVNG_SEEK_SET);
1987 endPos=pos+2+fieldPos[0];
1988 }
1989 std::stringstream f;
1990 std::vector<std::vector<WKSContentListener::FormulaInstruction> > stack;
1991 bool ok = true;
1992 size_t actCellId=0, actDualCellId=size_t(NSingle);
1993 while (long(m_input->tell()) != endPos)
1994 {
1995 double val;
1996 bool isNaN;
1997 pos = m_input->tell();
1998 if (pos > endPos) return false;
1999 auto wh = int(libwps::readU8(m_input));
2000 int arity = 0;
2001 WKSContentListener::FormulaInstruction instr;
2002 switch (wh)
2003 {
2004 case 0x0:
2005 if (endPos-pos<9 || !libwps::readDouble8(m_input, val, isNaN))
2006 {
2007 f.str("");
2008 f << "###number";
2009 error=f.str();
2010 ok = false;
2011 break;
2012 }
2013 instr.m_type=WKSContentListener::FormulaInstruction::F_Double;
2014 instr.m_doubleValue=val;
2015 break;
2016 case 0x1:
2017 if (actCellId>=listCellsPos.size())
2018 {
2019 f.str("");
2020 f << "###unknCell" << actCellId;
2021 error=f.str();
2022 ok = false;
2023 break;
2024 }
2025 instr=listCellsPos[actCellId++];
2026 break;
2027 case 0x2:
2028 if (vers>=2)
2029 {
2030 if (actCellId>=listCellsPos.size())
2031 {
2032 f.str("");
2033 f << "###unknListCell" << actCellId;
2034 error=f.str();
2035 ok = false;
2036 break;
2037 }
2038 instr=listCellsPos[actCellId++];
2039 break;
2040 }
2041 if (actDualCellId+1>=listCellsPos.size())
2042 {
2043 f.str("");
2044 f << "###unknListCell" << actDualCellId;
2045 error=f.str();
2046 ok = false;
2047 break;
2048 }
2049 instr=listCellsPos[actDualCellId++];
2050 instr.m_type=WKSContentListener::FormulaInstruction::F_CellList;
2051 instr.m_position[1]=listCellsPos[actDualCellId].m_position[0];
2052 instr.m_positionRelative[1]=listCellsPos[actDualCellId].m_positionRelative[0];
2053 ++actDualCellId;
2054 break;
2055 case 0x5:
2056 instr.m_type=WKSContentListener::FormulaInstruction::F_Long;
2057 instr.m_longValue=long(libwps::read16(m_input));
2058 break;
2059 case 0x6:
2060 instr.m_type=WKSContentListener::FormulaInstruction::F_Text;
2061 while (!m_input->isEnd())
2062 {
2063 if (m_input->tell() >= endPos)
2064 {
2065 ok=false;
2066 break;
2067 }
2068 auto c = char(libwps::readU8(m_input));
2069 if (c==0) break;
2070 instr.m_content += c;
2071 }
2072 break;
2073 default:
2074 if (wh >= 0x90 || QuattroDosSpreadsheetInternal::s_listFunctions[wh].m_arity == -2)
2075 {
2076 f.str("");
2077 f << "##Funct" << std::hex << wh;
2078 error=f.str();
2079 ok = false;
2080 break;
2081 }
2082 instr.m_type=WKSContentListener::FormulaInstruction::F_Function;
2083 instr.m_content=QuattroDosSpreadsheetInternal::s_listFunctions[wh].m_name;
2084 ok=!instr.m_content.empty();
2085 arity = QuattroDosSpreadsheetInternal::s_listFunctions[wh].m_arity;
2086 if (arity == -1) arity = int(libwps::read8(m_input));
2087 break;
2088 }
2089
2090 if (!ok) break;
2091 std::vector<WKSContentListener::FormulaInstruction> child;
2092 if (instr.m_type!=WKSContentListener::FormulaInstruction::F_Function)
2093 {
2094 child.push_back(instr);
2095 stack.push_back(child);
2096 continue;
2097 }
2098 size_t numElt = stack.size();
2099 if (int(numElt) < arity)
2100 {
2101 f.str("");
2102 f << instr.m_content << "[##" << arity << "]";
2103 error=f.str();
2104 ok = false;
2105 break;
2106 }
2107 //
2108 // first treat the special cases
2109 //
2110 if (arity==3 && instr.m_type==WKSContentListener::FormulaInstruction::F_Function && instr.m_content=="TERM")
2111 {
2112 // @TERM(pmt,pint,fv) -> NPER(pint,-pmt,pv=0,fv)
2113 auto pmt=stack[size_t(int(numElt)-3)];
2114 auto pint=stack[size_t(int(numElt)-2)];
2115 auto fv=stack[size_t(int(numElt)-1)];
2116
2117 stack.resize(size_t(++numElt));
2118 // pint
2119 stack[size_t(int(numElt)-4)]=pint;
2120 //-pmt
2121 auto &node=stack[size_t(int(numElt)-3)];
2122 instr.m_type=WKSContentListener::FormulaInstruction::F_Operator;
2123 instr.m_content="-";
2124 node.resize(0);
2125 node.push_back(instr);
2126 instr.m_content="(";
2127 node.push_back(instr);
2128 node.insert(node.end(), pmt.begin(), pmt.end());
2129 instr.m_content=")";
2130 node.push_back(instr);
2131 //pv=zero
2132 instr.m_type=WKSContentListener::FormulaInstruction::F_Long;
2133 instr.m_longValue=0;
2134 stack[size_t(int(numElt)-2)].resize(0);
2135 stack[size_t(int(numElt)-2)].push_back(instr);
2136 //fv
2137 stack[size_t(int(numElt)-1)]=fv;
2138 arity=4;
2139 instr.m_type=WKSContentListener::FormulaInstruction::F_Function;
2140 instr.m_content="NPER";
2141 }
2142 else if (arity==3 && instr.m_type==WKSContentListener::FormulaInstruction::F_Function && instr.m_content=="CTERM")
2143 {
2144 // @CTERM(pint,fv,pv) -> NPER(pint,pmt=0,-pv,fv)
2145 auto pint=stack[size_t(int(numElt)-3)];
2146 auto fv=stack[size_t(int(numElt)-2)];
2147 auto pv=stack[size_t(int(numElt)-1)];
2148 stack.resize(size_t(++numElt));
2149 // pint
2150 stack[size_t(int(numElt)-4)]=pint;
2151 // pmt=0
2152 instr.m_type=WKSContentListener::FormulaInstruction::F_Long;
2153 instr.m_longValue=0;
2154 stack[size_t(int(numElt)-3)].resize(0);
2155 stack[size_t(int(numElt)-3)].push_back(instr);
2156 // -pv
2157 auto &node=stack[size_t(int(numElt)-2)];
2158 instr.m_type=WKSContentListener::FormulaInstruction::F_Operator;
2159 instr.m_content="-";
2160 node.resize(0);
2161 node.push_back(instr);
2162 instr.m_content="(";
2163 node.push_back(instr);
2164 node.insert(node.end(), pv.begin(), pv.end());
2165 instr.m_content=")";
2166 node.push_back(instr);
2167
2168 //fv
2169 stack[size_t(int(numElt)-1)]=fv;
2170 arity=4;
2171 instr.m_type=WKSContentListener::FormulaInstruction::F_Function;
2172 instr.m_content="NPER";
2173 }
2174
2175 if ((instr.m_content[0] >= 'A' && instr.m_content[0] <= 'Z') || instr.m_content[0] == '(')
2176 {
2177 if (instr.m_content[0] != '(')
2178 child.push_back(instr);
2179
2180 instr.m_type=WKSContentListener::FormulaInstruction::F_Operator;
2181 instr.m_content="(";
2182 child.push_back(instr);
2183 for (int i = 0; i < arity; i++)
2184 {
2185 if (i)
2186 {
2187 instr.m_content=";";
2188 child.push_back(instr);
2189 }
2190 auto const &node=stack[size_t(int(numElt)-arity+i)];
2191 child.insert(child.end(), node.begin(), node.end());
2192 }
2193 instr.m_content=")";
2194 child.push_back(instr);
2195
2196 stack.resize(size_t(int(numElt)-arity+1));
2197 stack[size_t(int(numElt)-arity)] = child;
2198 continue;
2199 }
2200 if (arity==1)
2201 {
2202 instr.m_type=WKSContentListener::FormulaInstruction::F_Operator;
2203 stack[numElt-1].insert(stack[numElt-1].begin(), instr);
2204 if (wh==3)
2205 break;
2206 continue;
2207 }
2208 if (arity==2)
2209 {
2210 instr.m_type=WKSContentListener::FormulaInstruction::F_Operator;
2211 stack[numElt-2].push_back(instr);
2212 stack[numElt-2].insert(stack[numElt-2].end(), stack[numElt-1].begin(), stack[numElt-1].end());
2213 stack.resize(numElt-1);
2214 continue;
2215 }
2216 ok=false;
2217 error = "### unexpected arity";
2218 break;
2219 }
2220
2221 if (!ok) ;
2222 else if (stack.size()==1 && stack[0].size()>1 && stack[0][0].m_content=="=")
2223 {
2224 formula.insert(formula.begin(),stack[0].begin()+1,stack[0].end());
2225 if (m_input->tell()!=endPos)
2226 {
2227 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readFormula: find some extra data\n"));
2228 error="##extra data";
2229 ascii().addDelimiter(m_input->tell(),'#');
2230 }
2231 return true;
2232 }
2233 else
2234 error = "###stack problem";
2235
2236 static bool first = true;
2237 if (first)
2238 {
2239 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::readFormula: I can not read some formula\n"));
2240 first = false;
2241 }
2242
2243 f.str("");
2244 for (auto const &i : stack)
2245 {
2246 for (auto const &j : i)
2247 f << j << ",";
2248 }
2249 f << error << "###";
2250 error = f.str();
2251 return false;
2252 }
2253
2254 ////////////////////////////////////////////////////////////
2255 // send data
2256 ////////////////////////////////////////////////////////////
2257 void QuattroDosSpreadsheet::sendSpreadsheet(int sId, std::map<Vec2i,Vec2i> const &chartPosMap)
2258 {
2259 if (!m_listener)
2260 {
2261 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::sendSpreadsheet: I can not find the listener\n"));
2262 return;
2263 }
2264 auto sheet = m_state->getSheet(QuattroDosSpreadsheetInternal::Spreadsheet::T_Spreadsheet, sId);
2265 if (!sheet)
2266 {
2267 if (sId==0)
2268 {
2269 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::sendSpreadsheet: oops can not find the actual sheet\n"));
2270 }
2271 sheet.reset(new QuattroDosSpreadsheetInternal::Spreadsheet);
2272 }
2273 sheet->insertChartPositions(chartPosMap, m_mainParser.getDefaultFontType());
2274 m_listener->openSheet(sheet->getWidths(), m_state->getSheetName(sId));
2275 sheet->compressRowHeights();
2276 auto it = sheet->m_positionToCellMap.begin();
2277 int prevRow = -1;
2278 while (it != sheet->m_positionToCellMap.end())
2279 {
2280 int row=it->first[1];
2281 auto const &cell=(it++)->second;
2282 if (row>prevRow+1)
2283 {
2284 while (row > prevRow+1)
2285 {
2286 if (prevRow != -1) m_listener->closeSheetRow();
2287 int numRepeat;
2288 float h=sheet->getRowHeight(prevRow+1, numRepeat);
2289 if (row<prevRow+1+numRepeat)
2290 numRepeat=row-1-prevRow;
2291 m_listener->openSheetRow(WPSRowFormat(h), numRepeat);
2292 prevRow+=numRepeat;
2293 }
2294 }
2295 if (row!=prevRow)
2296 {
2297 if (prevRow != -1) m_listener->closeSheetRow();
2298 m_listener->openSheetRow(WPSRowFormat(sheet->getRowHeight(++prevRow)));
2299 }
2300 auto cIt=chartPosMap.find(cell.position());
2301 if (cIt==chartPosMap.end())
2302 sendCellContent(cell, sId);
2303 else
2304 sendCellContent(cell, sId, sId, sheet->getZoneSize(cIt->first, cIt->second));
2305 }
2306 if (prevRow!=-1) m_listener->closeSheetRow();
2307 m_listener->closeSheet();
2308 }
2309
2310 void QuattroDosSpreadsheet::sendCellContent(QuattroDosSpreadsheetInternal::Cell const &cell, int sheetId, int cellChartSheetId, Vec2f const &chartSize)
2311 {
2312 if (m_listener.get() == nullptr)
2313 {
2314 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::sendCellContent: I can not find the listener\n"));
2315 return;
2316 }
2317
2318 libwps_tools_win::Font::Type fontType = cell.m_fontType;
2319 m_listener->setFont(cell.getFont());
2320
2321 QuattroDosSpreadsheetInternal::Cell finalCell(cell);
2322 auto &content=finalCell.m_content;
2323 for (auto &f : content.m_formula)
2324 {
2325 if (f.m_type==WKSContentListener::FormulaInstruction::F_Cell ||
2326 f.m_type==WKSContentListener::FormulaInstruction::F_CellList)
2327 {
2328 int dim=f.m_type==WKSContentListener::FormulaInstruction::F_Cell ? 1 : 2;
2329 for (int i=0; i<dim; ++i)
2330 {
2331 if (f.m_sheetId[i]>=0 && f.m_sheetName[i].empty() && (f.m_sheetId[i]!=sheetId || !f.m_fileName.empty()))
2332 f.m_sheetName[i]=getSheetName(f.m_sheetId[i]);
2333 }
2334 continue;
2335 }
2336 if (f.m_type!=WKSContentListener::FormulaInstruction::F_Text)
2337 continue;
2338 std::string &text=f.m_content;
2339 auto finalText=libwps_tools_win::Font::unicodeString(text, fontType);
2340 if (finalText.empty())
2341 text.clear();
2342 else
2343 text=finalText.cstr();
2344 }
2345 finalCell.updateFormat();
2346 m_listener->openSheetCell(finalCell, content);
2347
2348 if (cell.m_content.m_textEntry.valid())
2349 {
2350 m_input->seek(cell.m_content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET);
2351 bool prevEOL=false;
2352 std::string text;
2353 while (m_input->tell()<=cell.m_content.m_textEntry.end())
2354 {
2355 bool last=m_input->isEnd() || m_input->tell()>=cell.m_content.m_textEntry.end();
2356 auto c=last ? '\0' : char(libwps::readU8(m_input));
2357 if ((c==0 || c==0xa || c==0xd) && !text.empty())
2358 {
2359 m_listener->insertUnicodeString(libwps_tools_win::Font::unicodeString(text, fontType));
2360 text.clear();
2361 }
2362 if (last) break;
2363 if (c==0xd)
2364 {
2365 m_listener->insertEOL();
2366 prevEOL=true;
2367 }
2368 else if (c==0xa)
2369 {
2370 if (!prevEOL)
2371 {
2372 WPS_DEBUG_MSG(("QuattroDosSpreadsheet::sendCellContent: find 0xa without 0xd\n"));
2373 }
2374 prevEOL=false;
2375 }
2376 else
2377 {
2378 text.push_back(c);
2379 prevEOL=false;
2380 }
2381 }
2382 }
2383 if (cellChartSheetId>=0)
2384 m_mainParser.sendChart(cellChartSheetId, cell.position(), chartSize);
2385 m_listener->closeSheetCell();
2386 }
2387
2388 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */