/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* libwps * Version: MPL 2.0 / LGPLv2.1+ * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Major Contributor(s): * Copyright (C) 2006, 2007 Andrew Ziem * Copyright (C) 2004 Marc Maurer (uwog@uwog.net) * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch) * * For minor contributions see the git repository. * * Alternatively, the contents of this file may be used under the terms * of the GNU Lesser General Public License Version 2.1 or later * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are * applicable instead of those above. */ #include #include #include #include #include #include #include #include #include #include #include "libwps_internal.h" #include "libwps_tools_win.h" #include "WPSCell.h" #include "WKSContentListener.h" #include "WPSEntry.h" #include "WPSFont.h" #include "WPSStream.h" #include "WPSTable.h" #include "QuattroFormula.h" #include "Quattro9.h" #include "Quattro9Spreadsheet.h" namespace Quattro9SpreadsheetInternal { //! a class used to store a style of a cell in Quattro9Spreadsheet struct Style final : public WPSCellFormat { //! construtor explicit Style() : WPSCellFormat() , m_fileFormat(-1) , m_alignAcrossColumn(false) , m_extra("") { } Style(Style const &) = default; //! destructor ~Style() final; //! operator<< friend std::ostream &operator<<(std::ostream &o, Style const &style); //! operator== bool operator==(Style const &st) const; //! operator!= bool operator!=(Style const &st) const { return !(*this==st); } //! the file format int m_fileFormat; //! flag to know if we must align across column bool m_alignAcrossColumn; /** extra data */ std::string m_extra; }; Style::~Style() { } //! operator<< std::ostream &operator<<(std::ostream &o, Style const &style) { o << static_cast(style) << ","; if (style.m_fileFormat!=0xFF) o << "format=" << std::hex << style.m_fileFormat << std::dec << ","; if (style.m_extra.length()) o << "extra=[" << style.m_extra << "],"; return o; } bool Style::operator==(Style const &st) const { if (m_fileFormat!=st.m_fileFormat) return false; int diff = WPSCellFormat::compare(st); if (diff) return false; return m_alignAcrossColumn==st.m_alignAcrossColumn && m_extra==st.m_extra; } //! a cellule of a Quattro spreadsheet class Cell final : public WPSCell { public: /// constructor explicit Cell() { } //! update the cell format using file format void updateFormat(int fileFormat) { if (fileFormat<0) return; switch (fileFormat>>5) { case 0: switch (fileFormat&0x1f) { case 1: // checkme: +/- : kind of bool setFormat(F_BOOLEAN); break; case 2: // generic break; case 3: setFormat(F_TEXT); break; case 4: // hidden { auto font=getFont(); font.m_attributes |= WPS_HIDDEN_BIT; setFont(font); break; } case 5: setDTFormat(F_DATE, "%d-%b-%y"); break; case 6: setDTFormat(F_DATE, "%d %b"); break; case 7: setDTFormat(F_DATE, "%b-%y"); break; case 8: setDTFormat(F_DATE, "%A %d %B %Y"); break; case 9: setDTFormat(F_DATE, "%m/%d/%Y"); break; case 0xa: setDTFormat(F_TIME, "%I:%M:%S%p"); break; case 0xb: setDTFormat(F_TIME, "%I:%M%p"); break; case 0xc: setDTFormat(F_TIME, "%H:%M:%S"); break; case 0xd: setDTFormat(F_TIME, "%H:%M"); break; case 0xe: setDTFormat(F_DATE, "%d-%b-%Y"); break; case 0xf: setDTFormat(F_DATE, "%b-%Y"); break; default: WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Cell::send: unknown format %x\n", fileFormat)); break; } break; case 1: // basic, neg red case 2: // neg (), neg red () case 3: // basic+thousand, neg red case 4: setFormat(F_NUMBER, 1); setDigits(fileFormat&0xF); break; case 5: // currency (see previous) case 6: case 7: case 8: setFormat(F_NUMBER, 4); setDigits(fileFormat&0xF); break; case 9: // percent, scientific setFormat(F_NUMBER, (fileFormat&0x10) ? 2 : 3); setDigits(fileFormat&0xF); break; case 0xa: // fraction (note fileFormat&0x1f seems to define the format of the denominator) setFormat(F_NUMBER, 7); break; case 0xb: { // id=(fileFormat>>4)-0x16 static bool first=true; if (first) { first=false; WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Cell::updateFormat: user defined format is not supported\n")); } break; } default: WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Cell::send: unknown format %x\n", fileFormat)); break; } } //! call when a cell must be send bool send(WPSListenerPtr &/*listener*/) final; //! call when the content of a cell must be send bool sendContent(WPSListenerPtr &/*listener*/) final { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Cell::sendContent: must not be called\n")); return false; } }; bool Cell::send(WPSListenerPtr &/*listener*/) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Cell::send: must not be called\n")); return false; } //! a class used to store the cell(s) content of a Quattro Spreadsheet struct CellData { //! constructor CellData() : m_type(0) , m_rows() , m_span(1,1) , m_style(-1) , m_intList() , m_doubleList() , m_flagList() { } //! returns true if the cell contains no data (and is not a merged cell) bool empty() const { return (m_type&0x1f)==1 && m_span==Vec2i(1,1); } //! returns the double value corresponding to a row double getDouble(int row) const { if (m_doubleList.empty()) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getDouble: no int value\n")); return 0; } if (rowm_rows[1]) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getDouble: called with bad row=%d\n", row)); return 0; } switch ((m_type>>5)&3) { case 0: return m_doubleList[0]; case 2: if (row-m_rows[0]>=int(m_doubleList.size())) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getDouble: oops, can not find row=%d\n", row)); return 0; } return m_doubleList[size_t(row-m_rows[0])]; case 3: if (m_doubleList.size()!=2) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getDouble: oops, unexpected data size\n")); return 0; } return m_doubleList[0]+(row-m_rows[0])*m_doubleList[1]; default: break; } WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getDouble: oops, unexpected type\n")); return 0; } //! returns the flag value corresponding to a row int getFlag(int row) const { if (m_flagList.empty()) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getFlag: no flag value\n")); return 0; } if (rowm_rows[1]) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getFlag: called with bad row=%d\n", row)); return 0; } switch ((m_type>>5)&3) { case 0: return m_flagList[0]; case 2: if (row-m_rows[0]>=int(m_flagList.size())) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getFlag: oops, can not find row=%d\n", row)); return 0; } return m_flagList[size_t(row-m_rows[0])]; case 3: if (m_flagList.size()!=2) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getFlag: oops, unexpected data size\n")); return 0; } return m_flagList[0]+(row-m_rows[0])*m_flagList[1]; default: break; } WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getFlag: oops, unexpected type\n")); return 0; } //! returns the int value corresponding to a row int getInt(int row) const { if (m_intList.empty()) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getInt: no int value\n")); return 0; } if (rowm_rows[1]) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getInt: called with bad row=%d\n", row)); return 0; } switch ((m_type>>5)&3) { case 0: return m_intList[0]; case 2: if (row-m_rows[0]>=int(m_intList.size())) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getInt: oops, can not find row=%d\n", row)); return 0; } return m_intList[size_t(row-m_rows[0])]; case 3: if (m_intList.size()!=2) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getInt: oops, unexpected data size\n")); return 0; } return m_intList[0]+(row-m_rows[0])*m_intList[1]; default: break; } WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::CellData::getInt: oops, unexpected type\n")); return 0; } //! the cell type int m_type; //! the min/max row Vec2i m_rows; //! the column row/span Vec2i m_span; //! the style id int m_style; //! the list of int values std::vector m_intList; //! the list of double values std::vector m_doubleList; //! a list of flag (for formula) std::vector m_flagList; }; //! Internal: a list of cell and result of Quattro9SpreadsheetInternal struct Column { //! constructor Column() : m_rowsToCellMap() , m_rowToCellResMap() { } //! add a cell/list of cells data void add(Vec2i limits, CellData const &cell) { auto rIt=m_rowsToCellMap.lower_bound(Vec2i(-1,limits[0])); while (rIt!=m_rowsToCellMap.end()) { auto const &cells=rIt->first; if (cells[0]>limits[1]) break; if (cells[1]>=limits[0]) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::column::addCells: oops, some rows are already set in %dx%d\n", limits[0], limits[1])); return; } ++rIt; } m_rowsToCellMap[limits]=cell; } //! add a cell result void add(int row, std::shared_ptr const &stream, Quattro9ParserInternal::TextEntry const &entry) { if (m_rowToCellResMap.find(row)!=m_rowToCellResMap.end()) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::column::addCells: oops, a result exists for row=%d\n", row)); return; } m_rowToCellResMap[row]=std::make_pair(stream,entry); } //! update a list of row (needed to know what row must be sent) void updateListOfRows(std::set &rows) const { for (auto const &it : m_rowsToCellMap) { rows.insert(it.first[0]); if (it.second.m_span[1]>1) { rows.insert(it.first[0]+1); rows.insert(it.first[0]+it.second.m_span[1]); continue; } rows.insert(it.first[1]+1); if ((it.second.m_type&0x1f)==1) continue; for (int r=it.first[0]+1; r<=it.first[1]; ++r) rows.insert(r); } } //! returns the cell corresponding to a row (if it exist) CellData const *getCell(int row) const { auto rIt=m_rowsToCellMap.lower_bound(Vec2i(-1,row)); if (rIt!=m_rowsToCellMap.end() && rIt->first[1]first[0]<=row && rIt->first[1]>=row) return &rIt->second; return nullptr; } //! a map rows to cell data std::map m_rowsToCellMap; //! a map row to cell result std::map, Quattro9ParserInternal::TextEntry> > m_rowToCellResMap; }; //! the spreadsheet of a Quattro Spreadsheet class Spreadsheet { public: //! a constructor explicit Spreadsheet(int id) : m_id(id) , m_defaultSizes(1080,260) , m_mergedCellList() , m_colToColumnMap() , m_invalidColumn() { } //! update the spreadsheet: check for merged cell, ... void update() { for (auto const &box : m_mergedCellList) { int const minRow=box[0][1]; int const maxRow=box[1][1]; for (int col=box[0][0]; col<=box[1][0]; ++col) { auto cIt=m_colToColumnMap.find(col); bool const firstCol=col==box[0][0]; if (cIt==m_colToColumnMap.end()) { if (firstCol) // we must create it { auto &column=getColumn(col); CellData cell; cell.m_type=1; // no data cell.m_rows=Vec2i(minRow, minRow); cell.m_span=Vec2i(box.size())+Vec2i(1,1); column.add(cell.m_rows, cell); } continue; } std::vector listCellToAdd; auto &column=getColumn(col); auto rIt=column.m_rowsToCellMap.lower_bound(Vec2i(-1,minRow)); if (rIt!=column.m_rowsToCellMap.end() && rIt->first[1]first[0]first[1]>=minRow) { // we need to split the first data auto rows=rIt->first; auto cell=rIt->second; ++rIt; column.m_rowsToCellMap.erase(rows); cell.m_rows[1]=minRow-1; listCellToAdd.push_back(cell); if (firstCol) { cell.m_rows=Vec2i(minRow, minRow); cell.m_span=Vec2i(box.size())+Vec2i(1,1); listCellToAdd.push_back(cell); } } else if (firstCol) { if (rIt!=column.m_rowsToCellMap.end() && rIt->first[0]==minRow) { // the first cell exist, update span auto cell=rIt->second; cell.m_rows=Vec2i(minRow, minRow); cell.m_span=Vec2i(box.size())+Vec2i(1,1); listCellToAdd.push_back(cell); } else { // create a new cell CellData cell; cell.m_type=1; // no data cell.m_rows=Vec2i(minRow, minRow); cell.m_span=Vec2i(box.size())+Vec2i(1,1); column.add(cell.m_rows, cell); } } while (rIt!=column.m_rowsToCellMap.end() && rIt->first[1]<=maxRow) { // remove cells between minRow and maxRow auto rows=rIt->first; ++rIt; column.m_rowsToCellMap.erase(rows); } if (rIt!=column.m_rowsToCellMap.end() && rIt->first[0]<=maxRow) { // we need to split the last data auto rows=rIt->first; auto cell=rIt->second; column.m_rowsToCellMap.erase(rows); cell.m_rows[0]=maxRow+1; listCellToAdd.push_back(cell); } for (auto const &cell: listCellToAdd) column.add(cell.m_rows, cell); } } } //! returns the row size in point WPSRowFormat getRowHeight(int row) const { auto rIt=m_sizesMap[1].lower_bound(Vec2i(-1,row)); if (rIt!=m_sizesMap[1].end() && rIt->first[0]<=row && rIt->first[1]>=row) return WPSRowFormat(float(rIt->second)/20.f); return WPSRowFormat(float(m_defaultSizes[1])/20.f); } //! return the columns format std::vector getWidths() const { std::vector widths; // g++-mp-7 does not like float(m_defaultSizes[0])/20.f), but this->m_default is ok... WPSColumnFormat defWidth(float(this->m_defaultSizes[0])/20.f); defWidth.m_useOptimalWidth=true; int prevRow=-1; for (auto const &it : m_sizesMap[0]) { if (it.first[0]prevRow+1) { defWidth.m_numRepeat=it.first[0]-(prevRow+1); widths.push_back(defWidth); } WPSColumnFormat width(float(it.second)/20.f); width.m_numRepeat=(it.first[1]+1-it.first[0]); widths.push_back(width); prevRow=it.first[1]; } if (widths.empty()) { defWidth.m_numRepeat=256; widths.push_back(defWidth); } return widths; } //! set the col/row size in TWIP void setColRowSize(int which, int pos, int w) { if (which!=0 && which!=1) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Spreadsheet::setColRowSize: oops, which=%d is bad\n", which)); return; } auto rIt=m_sizesMap[which].lower_bound(Vec2i(-1,pos)); if (rIt!=m_sizesMap[which].end() && rIt->first[0]<=pos && rIt->first[1]>=pos) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Spreadsheet::setColRowSize: oops, pos %d is already set\n", pos)); return; } if (w>=0) m_sizesMap[which][Vec2i(pos,pos)]=w; } //! set the col/row size in TWIP void setColRowSizes(int which, int minPos, int maxPos, int w) { if (which!=0 && which!=1) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Spreadsheet::setColRowSizes: oops, which=%d is bad\n", which)); return; } auto rIt=m_sizesMap[which].lower_bound(Vec2i(-1,minPos)); while (rIt!=m_sizesMap[which].end()) { auto const &cells=rIt->first; if (cells[0]>maxPos) break; if (cells[1]>=minPos) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Spreadsheet::setColRowSizes: oops, some rows are already set in %dx%d\n", minPos, maxPos)); return; } ++rIt; } if (w>=0) m_sizesMap[which][Vec2i(minPos,maxPos)]=w; } //! returns the cell position Vec2f getPosition(Vec2i const &cell) const { Vec2f res(0,0); for (int which=0; which<2; ++which) { int prevRow=-1; int width=0; for (auto const &it : m_sizesMap[0]) { if (it.first[0]prevRow+1) { if (it.first[0]>cell[which]) { width+=(cell[which]-(prevRow+1))*m_defaultSizes[which]; prevRow=cell[which]; break; } width+=(it.first[0]-(prevRow+1))*m_defaultSizes[which]; } if (it.first[1]>cell[which]) { width+=(cell[which]-it.first[0])*it.second; prevRow=cell[which]; break; } width+=(it.first[1]+1-it.first[0])*it.second; prevRow=it.first[1]; } if (cell[which]>prevRow+1) width+=(cell[which]-(prevRow+1))*m_defaultSizes[which]; res[which]=float(width)/20.f; } return res; } //! returns a ref to column data (or to invalidColumn if called with bad column) Column &getColumn(int col) { auto it=m_colToColumnMap.find(col); if (it!=m_colToColumnMap.end()) return it->second; if (col<0 || col>m_defaultSizes[0]) { WPS_DEBUG_MSG(("Quattro9SpreadsheetInternal::Spreadsheet::getColumn: called with invalid col=%d\n", col)); return m_invalidColumn; } m_colToColumnMap[col]=Column(); return m_colToColumnMap.find(col)->second; } //! returns the list of rows which need to be opened, ... std::set getListSendRow() const { std::set rows; rows.insert(0); for (auto const &it : m_colToColumnMap) it.second.updateListOfRows(rows); for (auto const &it : m_sizesMap[1]) { rows.insert(it.first[0]); rows.insert(it.first[1]+1); } for (auto const &box : m_mergedCellList) { rows.insert(box[0][1]); rows.insert(box[1][1]+1); } return rows; } //! returns the list of columns in which a row cell exists std::vector getListCellsInARow(int row) const { std::vector cols; CellData const *prevCell=nullptr; int prevCol=-1, numRepeated=0; for (auto const &it : m_colToColumnMap) { auto newCell=it.second.getCell(row); if (it.first==prevCol+numRepeated && prevCell && newCell && prevCell->empty() && newCell->empty() && prevCell->m_style==newCell->m_style) { numRepeated++; continue; } if (prevCell) cols.push_back(Vec2i(prevCol,prevCol+numRepeated-1)); prevCol=it.first; prevCell=newCell; numRepeated=1; } if (prevCell) cols.push_back(Vec2i(prevCol,prevCol+numRepeated-1)); return cols; } //! returns the cell corresponding to a position (if it exist) CellData const *getCell(Vec2i const &pos) const { auto rIt=m_colToColumnMap.find(pos[0]); if (rIt==m_colToColumnMap.end()) return nullptr; return rIt->second.getCell(pos[1]); } //! the spreadsheet id int m_id; /** the default col/row size in TWIP */ Vec2i m_defaultSizes; /** the map Vec2i(min, max) to col/row size in TWIP */ std::map m_sizesMap[2]; /** the merge cells */ std::vector m_mergedCellList; /** a map col to column data */ std::map m_colToColumnMap; private: /** a extra column used to return an invalid column */ Column m_invalidColumn; }; //! the state of Quattro9Spreadsheet struct State { //! constructor explicit State(QuattroFormulaManager::CellReferenceFunction const &readCellReference) : m_version(-1) , m_documentStrings() , m_documentFormulas() , m_formulaManager(readCellReference, 2) , m_actualSpreadsheet() , m_actualColumn(-1) , m_stylesList() , m_spreadsheetMap() , m_idToSheetNameMap() , m_idToUserFormatMap() { } //! returns the ith spreadsheet librevenge::RVNGString getSheetName(int id) const { auto it = m_idToSheetNameMap.find(id); if (it!=m_idToSheetNameMap.end() && !it->second.empty()) return it->second; librevenge::RVNGString name; name.sprintf("Sheet%d", id+1); return name; } //! the file version int m_version; //! the document strings std::pair, std::vector > m_documentStrings; //! the document formulas std::pair, std::vector > m_documentFormulas; //! the formula manager QuattroFormulaManager m_formulaManager; //! the actual sheet std::shared_ptr m_actualSpreadsheet; //! the actual column int m_actualColumn; //! the list of styles std::vector