/* -*- 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 #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 "Quattro.h" #include "QuattroFormula.h" #include "QuattroSpreadsheet.h" namespace QuattroSpreadsheetInternal { //! a class used to store a style of a cell in QuattroSpreadsheet struct Style final : public WPSCellFormat { //! construtor explicit Style(libwps_tools_win::Font::Type type) : WPSCellFormat() , m_fontType(type) , m_fileFormat(0xFF) , 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); } //! font encoding type libwps_tools_win::Font::Type m_fontType; //! 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_fontType!=st.m_fontType || 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(libwps_tools_win::Font::Type type) : m_fontType(type) , m_fileFormat(0xFF) , m_styleId(-1) , m_alignAcrossColumn(false) , m_content() , m_hasGraphic(false) , m_stream() { } //! operator<< friend std::ostream &operator<<(std::ostream &o, Cell const &cell); //! 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(("QuattroSpreadsheetInternal::Cell::sendContent: must not be called\n")); return false; } //! update the cell format using file format void updateFormat() { if (m_fileFormat==0xFF) return; switch ((m_fileFormat>>4)&7) { case 0: case 6: // checkme: date format set by system switch (m_fileFormat&0xF) { case 1: // +/- : kind of bool setFormat(F_BOOLEAN); break; case 2: // default break; case 3: setFormat(F_TEXT); break; case 4: setFormat(F_TEXT); m_font.m_attributes |= WPS_HIDDEN_BIT; break; case 5: setDTFormat(F_DATE, "%d %b %y"); break; case 6: setDTFormat(F_DATE, "%d %b"); break; case 7: setDTFormat(F_DATE, "%b-%d"); break; case 8: setDTFormat(F_DATE, "%m/%d/%y"); break; case 9: setDTFormat(F_DATE, "%m/%d"); 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_TIME, "%y"); break; case 0xf: setDTFormat(F_TIME, "%b"); break; default: WPS_DEBUG_MSG(("QuattroSpreadsheetInternal::Cell::updateFormat: unknown format %x\n", unsigned(m_fileFormat))); break; } break; case 1: // fixed setFormat(F_NUMBER, 1); setDigits(m_fileFormat&0xF); break; case 2: // scientific setFormat(F_NUMBER, 2); setDigits(m_fileFormat&0xF); break; case 3: // currency setFormat(F_NUMBER, 4); setDigits(m_fileFormat&0xF); break; case 4: // percent setFormat(F_NUMBER, 3); setDigits(m_fileFormat&0xF); break; case 5: // decimal setFormat(F_NUMBER, 1); setDigits(m_fileFormat&0xF); break; case 7: // fixme use UserFormat (m_fileFormat&0xF) in Quattro.cpp at least to decode date... { static bool first=true; if (first) { first=false; WPS_DEBUG_MSG(("QuattroSpreadsheetInternal::Cell::updateFormat: user defined format is not supported\n")); } break; } default: break; } } //! font encoding type libwps_tools_win::Font::Type m_fontType; //! the file format int m_fileFormat; //! the style id int m_styleId; //! flag to know if we must align across column bool m_alignAcrossColumn; //! the content WKSContentListener::CellContent m_content; //! a flag to know a cell has some graphic bool m_hasGraphic; //! the text stream(used to send text's zone) std::shared_ptr m_stream; }; bool Cell::send(WPSListenerPtr &/*listener*/) { WPS_DEBUG_MSG(("QuattroSpreadsheetInternal::Cell::send: must not be called\n")); return false; } //! operator<< std::ostream &operator<<(std::ostream &o, Cell const &cell) { o << reinterpret_cast(cell) << cell.m_content << ","; if (cell.m_fileFormat!=0xFF) o << "format=" << std::hex << cell.m_fileFormat << std::dec << ","; return o; } //! the spreadsheet of a Quattro Spreadsheet class Spreadsheet { public: //! a constructor Spreadsheet(int id, libwps_tools_win::Font::Type fontType) : m_id(id) , m_numCols(0) , m_rowHeightMap() , m_heightDefault(13) // fixme: use zone d2 , m_widthCols() , m_widthDefault(54) // fixme: use zone d4 , m_positionToCellMap() , m_dummyCell(fontType) { } //! return a cell corresponding to a spreadsheet, create one if needed Cell &getCell(Vec2i const &pos, libwps_tools_win::Font::Type type) { if (m_positionToCellMap.find(pos)==m_positionToCellMap.end()) { Cell cell(type); cell.setPosition(pos); if (pos[0]<0 || pos[0]>255) { WPS_DEBUG_MSG(("QuattroSpreadsheetInternal::Spreadsheet::getCell: find unexpected col=%d\n", pos[0])); return m_dummyCell; } m_positionToCellMap.insert(std::map::value_type(pos,cell)); } return m_positionToCellMap.find(pos)->second; } //! returns true if the spreedsheet is empty bool empty() const { return m_positionToCellMap.empty(); } //! set the columns size void setColumnWidth(int col, int w=-1) { if (col < 0) return; if (col >= int(m_widthCols.size())) m_widthCols.resize(size_t(col)+1, -1); m_widthCols[size_t(col)] = w; if (col >= m_numCols) m_numCols=col+1; } //! return the columns format std::vector getWidths() const { std::vector widths; WPSColumnFormat defWidth(m_widthDefault), actWidth; defWidth.m_useOptimalWidth=true; int repeat=0; for (auto const &w : m_widthCols) { WPSColumnFormat newWidth; if (w < 0) newWidth=defWidth; else newWidth=WPSColumnFormat(float(w)/20.f); if (repeat && newWidth!=actWidth) { actWidth.m_numRepeat=repeat; widths.push_back(actWidth); repeat=0; } if (repeat==0) actWidth=newWidth; ++repeat; } if (repeat) { actWidth.m_numRepeat=repeat; widths.push_back(actWidth); } return widths; } //! set the rows size in TWIP void setRowHeight(int row, int h) { auto rIt=m_rowHeightMap.lower_bound(Vec2i(-1,row)); if (rIt!=m_rowHeightMap.end() && rIt->first[0]<=row && rIt->first[1]>=row) { WPS_DEBUG_MSG(("QuattroSpreadsheetInternal::Spreadsheet::setRowHeight: oops, row %d is already set\n", row)); return; } if (h>=0) m_rowHeightMap[Vec2i(row,row)]=h; } //! set the rows size in TWIP void setRowHeights(int minRow, int maxRow, int h) { auto rIt=m_rowHeightMap.lower_bound(Vec2i(-1,minRow)); while (rIt!=m_rowHeightMap.end()) { auto const &cells=rIt->first; if (cells[0]>maxRow) break; if (cells[1]>=minRow) { WPS_DEBUG_MSG(("QuattroSpreadsheetInternal::Spreadsheet::setRowHeight: oops, some rows are already set in %dx%d\n", minRow, maxRow)); return; } ++rIt; } if (h>=0) m_rowHeightMap[Vec2i(minRow,maxRow)]=h; } //! returns the row size in point float getRowHeight(int row) const { auto rIt=m_rowHeightMap.lower_bound(Vec2i(-1,row)); if (rIt!=m_rowHeightMap.end() && rIt->first[0]<=row && rIt->first[1]>=row) return float(rIt->second)/20.f; return m_heightDefault; } //! returns the height of a row in point and updated repeated row float getRowHeight(int row, int &numRepeated) const { auto rIt=m_rowHeightMap.lower_bound(Vec2i(-1,row)); if (rIt!=m_rowHeightMap.end() && rIt->first[0]<=row && rIt->first[1]>=row) { numRepeated=rIt->first[1]-row+1; return float(rIt->second)/20.f; } numRepeated=10000; return m_heightDefault; } //! try to compress the list of row height void compressRowHeights() { auto oldMap=m_rowHeightMap; m_rowHeightMap.clear(); int actHeight=-1; Vec2i actPos(0,-1); for (auto const &rIt : oldMap) { // first check for not filled row if (rIt.first[0]!=actPos[1]+1) { if (actHeight==int(m_heightDefault)*20) actPos[1]=rIt.first[0]-1; else { if (actPos[1]>=actPos[0]) m_rowHeightMap[actPos]=actHeight; actHeight=int(m_heightDefault)*20; actPos=Vec2i(actPos[1]+1, rIt.first[0]-1); } } if (rIt.second!=actHeight) { if (actPos[1]>=actPos[0]) m_rowHeightMap[actPos]=actHeight; actPos[0]=rIt.first[0]; actHeight=rIt.second; } actPos[1]=rIt.first[1]; } if (actPos[1]>=actPos[0]) m_rowHeightMap[actPos]=actHeight; } //! returns the cell position Vec2f getPosition(Vec2i const &cell) const { float c=0; int numWidth=int(m_widthCols.size()); for (int i=0; i=numWidth) { c+=float(i+1-numWidth)*m_widthDefault; break; } int w=m_widthCols[size_t(i)]; if (w < 0) c+=m_widthDefault; else c+=float(w)/20.f; } int r=0, prevR=0; for (auto const &it : m_rowHeightMap) { int maxR=std::min(it.first[1],cell[1]-1); if (prevR m_rowHeightMap; /** the default row size in point */ float m_heightDefault; /** the column size in TWIP */ std::vector m_widthCols; /** the default width size in point */ float m_widthDefault; /** a map cell to not empty cells */ std::map m_positionToCellMap; /** a dummy cell */ mutable Cell m_dummyCell; }; //! the state of QuattroSpreadsheet struct State { //! constructor explicit State(QuattroFormulaManager::CellReferenceFunction const &readCellReference) : m_version(-1) , m_maxDimension(0,0,0) , m_actSheet(-1) , m_stylesList() , m_formulaManager(readCellReference, 1) , m_spreadsheetMap() , m_idToSheetNameMap() , m_sheetNameSet() , m_idToUserFormatMap() { } //! returns the ith real spreadsheet std::shared_ptr getSheet(int id, libwps_tools_win::Font::Type fontType) { auto it=m_spreadsheetMap.find(id); if (it!=m_spreadsheetMap.end()) return it->second; std::shared_ptr sheet(new Spreadsheet(id, fontType)); sheet->setColumnWidth(m_maxDimension[0]); if (id<0 || id>m_maxDimension[2]) { WPS_DEBUG_MSG(("QuattroSpreadsheetInternal::State::getSheet: find unexpected id=%d\n", id)); if (id<0 || id>255) // too small or too big, return dummy spreadsheet return sheet; } m_spreadsheetMap[id]=sheet; return sheet; } //! returns the ith spreadsheet librevenge::RVNGString getSheetName(int id) { 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); name=getUniqueSheetName(name); m_idToSheetNameMap[id]=name; return name; } //! returns a unique sheet name build using the current name librevenge::RVNGString getUniqueSheetName(librevenge::RVNGString const &name) { if (m_sheetNameSet.find(name)==m_sheetNameSet.end()) { m_sheetNameSet.insert(name); return name; } int i=0; while (i<1000) { librevenge::RVNGString uniqueName(name); uniqueName.append((std::string("_")+std::to_string(i++)).c_str()); if (m_sheetNameSet.find(uniqueName)==m_sheetNameSet.end()) { m_sheetNameSet.insert(uniqueName); return uniqueName; } } return name; } //! the file version int m_version; //! the maximum col, row, sheet WPSVec3i m_maxDimension; //! the actual sheet int m_actSheet; //! the list of styles std::vector