| 1 |
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
| 2 |
|
| 3 |
/* libmwaw |
| 4 |
* Version: MPL 2.0 / LGPLv2+ |
| 5 |
* |
| 6 |
* The contents of this file are subject to the Mozilla Public License Version |
| 7 |
* 2.0 (the "License"); you may not use this file except in compliance with |
| 8 |
* the License or as specified alternatively below. You may obtain a copy of |
| 9 |
* the License at http://www.mozilla.org/MPL/ |
| 10 |
* |
| 11 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 12 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 |
* for the specific language governing rights and limitations under the |
| 14 |
* License. |
| 15 |
* |
| 16 |
* Major Contributor(s): |
| 17 |
* Copyright (C) 2002 William Lachance ([email protected]) |
| 18 |
* Copyright (C) 2002,2004 Marc Maurer ([email protected]) |
| 19 |
* Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 20 |
* Copyright (C) 2006, 2007 Andrew Ziem |
| 21 |
* Copyright (C) 2011, 2012 Alonso Laurent ([email protected]) |
| 22 |
* |
| 23 |
* |
| 24 |
* All Rights Reserved. |
| 25 |
* |
| 26 |
* For minor contributions see the git repository. |
| 27 |
* |
| 28 |
* Alternatively, the contents of this file may be used under the terms of |
| 29 |
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), |
| 30 |
* in which case the provisions of the LGPLv2+ are applicable |
| 31 |
* instead of those above. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <iomanip> |
| 35 |
#include <iostream> |
| 36 |
#include <limits> |
| 37 |
#include <map> |
| 38 |
#include <sstream> |
| 39 |
|
| 40 |
#include <librevenge/librevenge.h> |
| 41 |
|
| 42 |
#include "MWAWCell.hxx" |
| 43 |
#include "MWAWFont.hxx" |
| 44 |
#include "MWAWFontConverter.hxx" |
| 45 |
#include "MWAWListener.hxx" |
| 46 |
#include "MWAWParser.hxx" |
| 47 |
#include "MWAWSpreadsheetListener.hxx" |
| 48 |
#include "MWAWTable.hxx" |
| 49 |
|
| 50 |
#include "ClarisWksDbaseContent.hxx" |
| 51 |
#include "ClarisWksDocument.hxx" |
| 52 |
#include "ClarisWksStruct.hxx" |
| 53 |
#include "ClarisWksStyleManager.hxx" |
| 54 |
|
| 55 |
#include "ClarisWksSpreadsheet.hxx" |
| 56 |
|
| 57 |
/** Internal: the structures of a ClarisWksSpreadsheet */ |
| 58 |
namespace ClarisWksSpreadsheetInternal |
| 59 |
{ |
| 60 |
//! Internal the spreadsheet |
| 61 |
struct Spreadsheet final : public ClarisWksStruct::DSET { |
| 62 |
// constructor |
| 63 |
explicit Spreadsheet(ClarisWksStruct::DSET const &dset = ClarisWksStruct::DSET()) : |
| 64 |
ClarisWksStruct::DSET(dset), m_colWidths(), m_rowHeightMap(), m_content() |
| 65 |
{ |
| 66 |
} |
| 67 |
//! destructor |
| 68 |
~Spreadsheet() final; |
| 69 |
//! operator<< |
| 70 |
friend std::ostream &operator<<(std::ostream &o, Spreadsheet const &doc) |
| 71 |
{ |
| 72 |
o << static_cast<ClarisWksStruct::DSET const &>(doc); |
| 73 |
return o; |
| 74 |
} |
| 75 |
//! returns the row size in point |
| 76 |
float getRowHeight(int row) const |
| 77 |
{ |
| 78 |
if (m_rowHeightMap.find(row)!=m_rowHeightMap.end()) |
| 79 |
return float(m_rowHeightMap.find(row)->second); |
| 80 |
return 14; |
| 81 |
} |
| 82 |
//! returns the height of a row in point and updated repeated row |
| 83 |
float getRowHeight(int row, int &numRepeated) const |
| 84 |
{ |
| 85 |
int res=14; |
| 86 |
if (m_rowHeightMap.empty()) { |
| 87 |
numRepeated=1000; |
| 88 |
return float(res); |
| 89 |
} |
| 90 |
auto it=m_rowHeightMap.lower_bound(row); |
| 91 |
if (it==m_rowHeightMap.end()) { |
| 92 |
numRepeated=1000; |
| 93 |
return float(res); |
| 94 |
} |
| 95 |
numRepeated=1; |
| 96 |
if (it->first==row) |
| 97 |
res=it++->second; |
| 98 |
int lastRow=row; |
| 99 |
while (it!=m_rowHeightMap.end()) { |
| 100 |
int nRow=it->first; |
| 101 |
int nextH=it++->second; |
| 102 |
|
| 103 |
if (nRow!=lastRow+1) { |
| 104 |
if (res!=14) |
| 105 |
break; |
| 106 |
else |
| 107 |
numRepeated+=(nRow-(lastRow+1)); |
| 108 |
} |
| 109 |
if (nRow==row) |
| 110 |
continue; |
| 111 |
numRepeated=(nRow-row); |
| 112 |
if (nextH!=res) |
| 113 |
break; |
| 114 |
++numRepeated; |
| 115 |
lastRow=nRow; |
| 116 |
} |
| 117 |
return float(res); |
| 118 |
} |
| 119 |
|
| 120 |
//! the columns width |
| 121 |
std::vector<int> m_colWidths; |
| 122 |
//! a map row to height |
| 123 |
std::map<int, int> m_rowHeightMap; |
| 124 |
//! the data |
| 125 |
std::shared_ptr<ClarisWksDbaseContent> m_content; |
| 126 |
}; |
| 127 |
|
| 128 |
Spreadsheet::~Spreadsheet() |
| 129 |
{ |
| 130 |
} |
| 131 |
|
| 132 |
//! Internal: the state of a ClarisWksSpreadsheet |
| 133 |
struct State { |
| 134 |
//! constructor |
| 135 |
State() |
| 136 |
: m_spreadsheetMap() |
| 137 |
{ |
| 138 |
} |
| 139 |
//! a map zoneId to spreadsheet |
| 140 |
std::map<int, std::shared_ptr<Spreadsheet> > m_spreadsheetMap; |
| 141 |
}; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
//////////////////////////////////////////////////////////// |
| 146 |
// constructor/destructor, ... |
| 147 |
//////////////////////////////////////////////////////////// |
| 148 |
ClarisWksSpreadsheet::ClarisWksSpreadsheet(ClarisWksDocument &document) |
| 149 |
: m_document(document) |
| 150 |
, m_parserState(document.m_parserState) |
| 151 |
, m_state(new ClarisWksSpreadsheetInternal::State) |
| 152 |
, m_mainParser(&document.getMainParser()) |
| 153 |
{ |
| 154 |
} |
| 155 |
|
| 156 |
ClarisWksSpreadsheet::~ClarisWksSpreadsheet() |
| 157 |
{ } |
| 158 |
|
| 159 |
int ClarisWksSpreadsheet::version() const |
| 160 |
{ |
| 161 |
return m_parserState->m_version; |
| 162 |
} |
| 163 |
|
| 164 |
// fixme |
| 165 |
int ClarisWksSpreadsheet::numPages() const |
| 166 |
{ |
| 167 |
return 1; |
| 168 |
} |
| 169 |
//////////////////////////////////////////////////////////// |
| 170 |
// Intermediate level |
| 171 |
//////////////////////////////////////////////////////////// |
| 172 |
|
| 173 |
//////////////////////////////////////////////////////////// |
| 174 |
// a document part |
| 175 |
//////////////////////////////////////////////////////////// |
| 176 |
std::shared_ptr<ClarisWksStruct::DSET> ClarisWksSpreadsheet::readSpreadsheetZone |
| 177 |
(ClarisWksStruct::DSET const &zone, MWAWEntry const &entry, bool &complete) |
| 178 |
{ |
| 179 |
complete = false; |
| 180 |
if (!entry.valid() || zone.m_fileType != 2 || entry.length() < 256) |
| 181 |
return std::shared_ptr<ClarisWksStruct::DSET>(); |
| 182 |
long pos = entry.begin(); |
| 183 |
MWAWInputStreamPtr &input= m_parserState->m_input; |
| 184 |
input->seek(pos+8+16, librevenge::RVNG_SEEK_SET); // avoid header+8 generic number |
| 185 |
libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile; |
| 186 |
libmwaw::DebugStream f; |
| 187 |
std::shared_ptr<ClarisWksSpreadsheetInternal::Spreadsheet> |
| 188 |
sheet(new ClarisWksSpreadsheetInternal::Spreadsheet(zone)); |
| 189 |
|
| 190 |
f << "Entries(SpreadsheetDef):" << *sheet << ","; |
| 191 |
ascFile.addDelimiter(input->tell(), '|'); |
| 192 |
ascFile.addPos(pos); |
| 193 |
ascFile.addNote(f.str().c_str()); |
| 194 |
|
| 195 |
// read the last part |
| 196 |
long data0Length = zone.m_dataSz; |
| 197 |
long N = zone.m_numData; |
| 198 |
if (entry.length() -8-12 != data0Length*N + zone.m_headerSz) { |
| 199 |
if (data0Length == 0 && N) { |
| 200 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::readSpreadsheetZone: can not find definition size\n")); |
| 201 |
input->seek(entry.end(), librevenge::RVNG_SEEK_SET); |
| 202 |
return std::shared_ptr<ClarisWksStruct::DSET>(); |
| 203 |
} |
| 204 |
|
| 205 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::readSpreadsheetZone: unexpected size for zone definition, try to continue\n")); |
| 206 |
} |
| 207 |
int debColSize = 0; |
| 208 |
int vers = version(); |
| 209 |
switch (vers) { |
| 210 |
case 1: |
| 211 |
debColSize = 72; |
| 212 |
break; |
| 213 |
case 2: |
| 214 |
case 3: // checkme... |
| 215 |
case 4: |
| 216 |
case 5: |
| 217 |
debColSize = 76; |
| 218 |
break; |
| 219 |
case 6: |
| 220 |
debColSize = 72; |
| 221 |
break; |
| 222 |
default: |
| 223 |
break; |
| 224 |
} |
| 225 |
|
| 226 |
sheet->m_colWidths.resize(0); |
| 227 |
sheet->m_colWidths.resize(256,36); |
| 228 |
if (debColSize) { |
| 229 |
pos = entry.begin()+debColSize; |
| 230 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 231 |
f.str(""); |
| 232 |
f << "Entries(SpreadsheetCol):width,"; |
| 233 |
for (size_t i = 0; i < 256; ++i) { |
| 234 |
auto w=static_cast<int>(input->readULong(1)); |
| 235 |
sheet->m_colWidths[i]=w; |
| 236 |
if (w!=36) // default |
| 237 |
f << "w" << i << "=" << w << ","; |
| 238 |
} |
| 239 |
ascFile.addPos(pos); |
| 240 |
ascFile.addNote(f.str().c_str()); |
| 241 |
|
| 242 |
ascFile.addPos(input->tell()); |
| 243 |
ascFile.addNote("SpreadsheetDef-A"); |
| 244 |
} |
| 245 |
|
| 246 |
long dataEnd = entry.end()-N*data0Length; |
| 247 |
int numLast = version()==6 ? 4 : 0; |
| 248 |
if (long(input->tell()) + data0Length + numLast <= dataEnd) { |
| 249 |
ascFile.addPos(dataEnd-data0Length-numLast); |
| 250 |
ascFile.addNote("SpreadsheetDef-_"); |
| 251 |
if (numLast) { |
| 252 |
ascFile.addPos(dataEnd-numLast); |
| 253 |
ascFile.addNote("SpreadsheetDef-extra"); |
| 254 |
} |
| 255 |
} |
| 256 |
input->seek(dataEnd, librevenge::RVNG_SEEK_SET); |
| 257 |
|
| 258 |
for (long i = 0; i < N; i++) { |
| 259 |
pos = input->tell(); |
| 260 |
|
| 261 |
f.str(""); |
| 262 |
f << "SpreadsheetDef-" << i; |
| 263 |
ascFile.addPos(pos); |
| 264 |
ascFile.addNote(f.str().c_str()); |
| 265 |
input->seek(pos+data0Length, librevenge::RVNG_SEEK_SET); |
| 266 |
} |
| 267 |
|
| 268 |
input->seek(entry.end(), librevenge::RVNG_SEEK_SET); |
| 269 |
|
| 270 |
if (m_state->m_spreadsheetMap.find(sheet->m_id) != m_state->m_spreadsheetMap.end()) { |
| 271 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::readSpreadsheetZone: zone %d already exists!!!\n", sheet->m_id)); |
| 272 |
} |
| 273 |
else |
| 274 |
m_state->m_spreadsheetMap[sheet->m_id] = sheet; |
| 275 |
|
| 276 |
sheet->m_otherChilds.push_back(sheet->m_id+1); |
| 277 |
pos = input->tell(); |
| 278 |
|
| 279 |
bool ok = readZone1(*sheet); |
| 280 |
if (ok) { |
| 281 |
pos = input->tell(); |
| 282 |
ok = ClarisWksStruct::readStructZone(*m_parserState, "SpreadsheetZone2", false); |
| 283 |
} |
| 284 |
if (ok) { |
| 285 |
pos = input->tell(); |
| 286 |
std::shared_ptr<ClarisWksDbaseContent> content(new ClarisWksDbaseContent(m_document, true)); |
| 287 |
ok = content->readContent(); |
| 288 |
if (ok) sheet->m_content=content; |
| 289 |
} |
| 290 |
if (ok) { |
| 291 |
pos = input->tell(); |
| 292 |
if (!readRowHeightZone(*sheet)) { |
| 293 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 294 |
ok = ClarisWksStruct::readStructZone(*m_parserState, "SpreadsheetRowHeight", false); |
| 295 |
} |
| 296 |
} |
| 297 |
if (ok && vers <= 2) { // field with size 0xa in v2 |
| 298 |
pos = input->tell(); |
| 299 |
ok = ClarisWksStruct::readStructZone(*m_parserState, "SpreadsheetUnkn1", false); |
| 300 |
} |
| 301 |
/* checkme: now a sequence of 5/6 lists: when filed the first two zones are a list of cell, |
| 302 |
while the last 2 lists contains only 4 numbers */ |
| 303 |
while (ok) { |
| 304 |
pos=input->tell(); |
| 305 |
auto sz=long(input->readULong(4)); |
| 306 |
if (!input->checkPosition(pos+4+sz)) { |
| 307 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 308 |
break; |
| 309 |
} |
| 310 |
// empty or list of 2*uint16_t ? |
| 311 |
if (!sz) { |
| 312 |
ascFile.addPos(pos); |
| 313 |
ascFile.addNote("Entries(SpreadsheetListCell):_"); |
| 314 |
continue; |
| 315 |
} |
| 316 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 317 |
std::vector<MWAWVec2i> res; |
| 318 |
ok = m_document.readStructCellZone("SpreadsheetListCell", false, res); |
| 319 |
if (ok) continue; |
| 320 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 321 |
ok = ClarisWksStruct::readStructZone(*m_parserState, "SpreadsheetUnkn2", false); |
| 322 |
if (ok) { |
| 323 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::readSpreadsheetZone: find unexpected Unkn2 zone\n")); |
| 324 |
} |
| 325 |
} |
| 326 |
if (ok) { |
| 327 |
pos=input->tell(); |
| 328 |
auto sz=long(input->readULong(4)); |
| 329 |
if (input->checkPosition(pos+4+sz)) { |
| 330 |
input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET); |
| 331 |
ascFile.addPos(pos); |
| 332 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::readSpreadsheetZone: find some extra block\n")); |
| 333 |
ascFile.addNote("Entries(SpreadsheetEnd):###"); |
| 334 |
} |
| 335 |
else |
| 336 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 337 |
} |
| 338 |
|
| 339 |
if (!ok) { |
| 340 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::readSpreadsheetZone: find a bad block\n")); |
| 341 |
ascFile.addPos(pos); |
| 342 |
ascFile.addNote("Entries(SpreadsheetEnd):###"); |
| 343 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 344 |
} |
| 345 |
return sheet; |
| 346 |
} |
| 347 |
|
| 348 |
//////////////////////////////////////////////////////////// |
| 349 |
// |
| 350 |
// Intermediate level |
| 351 |
// |
| 352 |
//////////////////////////////////////////////////////////// |
| 353 |
bool ClarisWksSpreadsheet::readZone1(ClarisWksSpreadsheetInternal::Spreadsheet &/*sheet*/) |
| 354 |
{ |
| 355 |
MWAWInputStreamPtr &input= m_parserState->m_input; |
| 356 |
long pos = input->tell(); |
| 357 |
auto sz = long(input->readULong(4)); |
| 358 |
long endPos = pos+4+sz; |
| 359 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 360 |
if (long(input->tell()) != endPos) { |
| 361 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 362 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::readZone1: spreadsheet\n")); |
| 363 |
return false; |
| 364 |
} |
| 365 |
libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile; |
| 366 |
if (sz == 0) { |
| 367 |
ascFile.addPos(pos); |
| 368 |
ascFile.addNote("Nop"); |
| 369 |
return true; |
| 370 |
} |
| 371 |
int fSize = 0; |
| 372 |
switch (version()) { |
| 373 |
case 4: |
| 374 |
case 5: |
| 375 |
fSize = 4; |
| 376 |
break; |
| 377 |
case 6: |
| 378 |
fSize = 6; |
| 379 |
break; |
| 380 |
default: |
| 381 |
break; |
| 382 |
} |
| 383 |
if (!fSize) { |
| 384 |
ascFile.addPos(pos); |
| 385 |
ascFile.addNote("Entries(SpreadsheetZone1)"); |
| 386 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 387 |
return true; |
| 388 |
} |
| 389 |
long numElts = sz/fSize; |
| 390 |
if (numElts *fSize != sz) { |
| 391 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 392 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::readZone1: unexpected size\n")); |
| 393 |
return false; |
| 394 |
} |
| 395 |
|
| 396 |
ascFile.addPos(pos); |
| 397 |
ascFile.addNote("Entries(SpreadsheetZone1)"); |
| 398 |
|
| 399 |
libmwaw::DebugStream f; |
| 400 |
input->seek(pos+4, librevenge::RVNG_SEEK_SET); |
| 401 |
for (long i = 0; i < numElts; i++) { |
| 402 |
pos = input->tell(); |
| 403 |
|
| 404 |
f.str(""); |
| 405 |
f << "SpreadsheetZone1-" << i << ":"; |
| 406 |
f << "row?=" << input->readLong(2) << ","; |
| 407 |
f << "col?=" << input->readLong(2) << ","; |
| 408 |
if (fSize == 6) { |
| 409 |
auto val = static_cast<int>(input->readLong(2)); |
| 410 |
if (val != -1) |
| 411 |
f << "#unkn=" << val << ","; |
| 412 |
} |
| 413 |
ascFile.addPos(pos); |
| 414 |
ascFile.addNote(f.str().c_str()); |
| 415 |
input->seek(pos+fSize, librevenge::RVNG_SEEK_SET); |
| 416 |
} |
| 417 |
return true; |
| 418 |
} |
| 419 |
|
| 420 |
bool ClarisWksSpreadsheet::readRowHeightZone(ClarisWksSpreadsheetInternal::Spreadsheet &sheet) |
| 421 |
{ |
| 422 |
MWAWInputStreamPtr &input= m_parserState->m_input; |
| 423 |
long pos = input->tell(); |
| 424 |
ClarisWksStruct::Struct header; |
| 425 |
if (!header.readHeader(input,false)) { |
| 426 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::readRowHeightZone: can not read the header\n")); |
| 427 |
return false; |
| 428 |
} |
| 429 |
libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile; |
| 430 |
libmwaw::DebugStream f; |
| 431 |
if (header.m_size==0) { |
| 432 |
ascFile.addPos(pos); |
| 433 |
ascFile.addNote("Nop"); |
| 434 |
return true; |
| 435 |
} |
| 436 |
long endPos=pos+4+header.m_size; |
| 437 |
f << "Entries(SpreadsheetRowHeight):" << header; |
| 438 |
if (header.m_dataSize!=4) { |
| 439 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 440 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::readRowHeightZone: unexpected size for fieldSize\n")); |
| 441 |
f << "###"; |
| 442 |
ascFile.addPos(pos); |
| 443 |
ascFile.addNote(f.str().c_str()); |
| 444 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 445 |
return true; |
| 446 |
} |
| 447 |
if (header.m_headerSize) { |
| 448 |
ascFile.addDelimiter(input->tell(),'|'); |
| 449 |
input->seek(header.m_headerSize, librevenge::RVNG_SEEK_CUR); |
| 450 |
} |
| 451 |
ascFile.addPos(pos); |
| 452 |
ascFile.addNote(f.str().c_str()); |
| 453 |
|
| 454 |
for (long i = 0; i < header.m_numData; i++) { |
| 455 |
pos = input->tell(); |
| 456 |
|
| 457 |
f.str(""); |
| 458 |
f << "SpreadsheetRowHeightZone-" << i << ":"; |
| 459 |
auto row=static_cast<int>(input->readLong(2)); |
| 460 |
auto h=static_cast<int>(input->readLong(2)); |
| 461 |
sheet.m_rowHeightMap[row]=h; |
| 462 |
f << "row=" << row << ", height=" << h << ","; |
| 463 |
ascFile.addPos(pos); |
| 464 |
ascFile.addNote(f.str().c_str()); |
| 465 |
} |
| 466 |
return true; |
| 467 |
} |
| 468 |
|
| 469 |
//////////////////////////////////////////////////////////// |
| 470 |
// |
| 471 |
// send data |
| 472 |
// |
| 473 |
//////////////////////////////////////////////////////////// |
| 474 |
bool ClarisWksSpreadsheet::sendSpreadsheet(int zId, MWAWListenerPtr listener) |
| 475 |
{ |
| 476 |
if (!listener) |
| 477 |
listener=m_parserState->getMainListener(); |
| 478 |
if (!listener) { |
| 479 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::sendSpreadsheet: called without any listener\n")); |
| 480 |
return false; |
| 481 |
} |
| 482 |
if (listener->getType()!=MWAWListener::Spreadsheet || |
| 483 |
(m_parserState->m_kind==MWAWDocument::MWAW_K_SPREADSHEET && zId!=1)) |
| 484 |
return sendSpreadsheetAsTable(zId, listener); |
| 485 |
|
| 486 |
auto *sheetListener=static_cast<MWAWSpreadsheetListener *>(listener.get()); |
| 487 |
auto it=m_state->m_spreadsheetMap.find(zId); |
| 488 |
if (it == m_state->m_spreadsheetMap.end() || !it->second) { |
| 489 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::sendSpreadsheet: can not find zone %d!!!\n", zId)); |
| 490 |
return false; |
| 491 |
} |
| 492 |
auto &sheet=*it->second; |
| 493 |
MWAWVec2i minData, maxData; |
| 494 |
if (!sheet.m_content || !sheet.m_content->getExtrema(minData,maxData)) { |
| 495 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::sendSpreadsheet: can not find content\n")); |
| 496 |
return false; |
| 497 |
} |
| 498 |
if (m_parserState->m_kind==MWAWDocument::MWAW_K_SPREADSHEET && zId==1) |
| 499 |
minData=MWAWVec2i(0,0); |
| 500 |
std::vector<float> colSize(size_t(maxData[0]-minData[0]+1),72); |
| 501 |
for (int c=minData[0], fC=0; c <= maxData[0]; ++c, ++fC) { |
| 502 |
if (c>=0 && c < int(sheet.m_colWidths.size())) |
| 503 |
colSize[size_t(fC)]=2.0f*float(sheet.m_colWidths[size_t(c)]); |
| 504 |
} |
| 505 |
sheetListener->openSheet(colSize, librevenge::RVNG_POINT); |
| 506 |
MWAWInputStreamPtr &input= m_parserState->m_input; |
| 507 |
std::vector<int> rowsPos, colsPos; |
| 508 |
if (!sheet.m_content->getRecordList(rowsPos)) { |
| 509 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::sendSpreadsheet: can not find the record position\n")); |
| 510 |
sheetListener->closeSheet(); |
| 511 |
return true; |
| 512 |
} |
| 513 |
bool recomputeCellPosition=(minData!=MWAWVec2i(0,0)); |
| 514 |
int prevRow = minData[1]-1; |
| 515 |
for (int r : rowsPos) { |
| 516 |
int fR=r-minData[1]; |
| 517 |
if (r>prevRow+1) { |
| 518 |
while (r > prevRow+1) { |
| 519 |
int numRepeat; |
| 520 |
auto h=float(sheet.getRowHeight(prevRow+1, numRepeat)); |
| 521 |
if (r<prevRow+1+numRepeat) |
| 522 |
numRepeat=r-1-prevRow; |
| 523 |
sheetListener->openSheetRow(h, librevenge::RVNG_POINT, numRepeat); |
| 524 |
sheetListener->closeSheetRow(); |
| 525 |
prevRow+=numRepeat; |
| 526 |
} |
| 527 |
} |
| 528 |
sheetListener->openSheetRow(float(sheet.getRowHeight(r)), librevenge::RVNG_POINT); |
| 529 |
prevRow=r; |
| 530 |
colsPos.clear(); |
| 531 |
if (!sheet.m_content->getColumnList(r, colsPos)) { |
| 532 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::sendSpreadsheet: can not find the columns for row=%d\n", r)); |
| 533 |
sheetListener->closeSheetRow(); |
| 534 |
continue; |
| 535 |
} |
| 536 |
for (auto c : colsPos) { |
| 537 |
ClarisWksDbaseContent::Record rec; |
| 538 |
if (!sheet.m_content->get(MWAWVec2i(c,r),rec)) continue; |
| 539 |
MWAWCell cell; |
| 540 |
cell.setPosition(MWAWVec2i(c-minData[0],fR)); |
| 541 |
cell.setFormat(rec.m_format); |
| 542 |
cell.setHAlignment(rec.m_hAlign); |
| 543 |
cell.setFont(rec.m_font); |
| 544 |
if (recomputeCellPosition) |
| 545 |
rec.updateFormulaCells(minData); |
| 546 |
// change the reference date from 1/1/1904 to 1/1/1900 |
| 547 |
if (rec.m_format.m_format==MWAWCell::F_DATE && rec.m_content.isValueSet()) |
| 548 |
rec.m_content.setValue(rec.m_content.m_value+1460); |
| 549 |
if (rec.m_borders) { |
| 550 |
int wh=0; |
| 551 |
for (int b=0, bit=1; b < 4; ++b, bit*=2) { |
| 552 |
if ((rec.m_borders&bit)==0) continue; |
| 553 |
static int const what[] = {libmwaw::LeftBit, libmwaw::TopBit, libmwaw::RightBit, libmwaw::BottomBit}; |
| 554 |
wh |= what[b]; |
| 555 |
} |
| 556 |
cell.setBorders(wh, MWAWBorder()); |
| 557 |
} |
| 558 |
if (!rec.m_backgroundColor.isWhite()) |
| 559 |
cell.setBackgroundColor(rec.m_backgroundColor); |
| 560 |
sheetListener->openSheetCell(cell, rec.m_content); |
| 561 |
if (rec.m_content.m_textEntry.valid()) { |
| 562 |
long fPos = input->tell(); |
| 563 |
input->seek(rec.m_content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET); |
| 564 |
long endPos = rec.m_content.m_textEntry.end(); |
| 565 |
sheetListener->setFont(rec.m_font); |
| 566 |
while (!input->isEnd() && input->tell() < endPos) { |
| 567 |
auto ch=static_cast<unsigned char>(input->readULong(1)); |
| 568 |
if (ch==0xd || ch==0xa) |
| 569 |
sheetListener->insertEOL(); |
| 570 |
else |
| 571 |
sheetListener->insertCharacter(ch, input, endPos); |
| 572 |
} |
| 573 |
input->seek(fPos,librevenge::RVNG_SEEK_SET); |
| 574 |
} |
| 575 |
sheetListener->closeSheetCell(); |
| 576 |
} |
| 577 |
sheetListener->closeSheetRow(); |
| 578 |
} |
| 579 |
sheetListener->closeSheet(); |
| 580 |
return true; |
| 581 |
} |
| 582 |
|
| 583 |
bool ClarisWksSpreadsheet::sendSpreadsheetAsTable(int zId, MWAWListenerPtr listener) |
| 584 |
{ |
| 585 |
if (!listener) |
| 586 |
listener=m_parserState->getMainListener(); |
| 587 |
if (!listener) { |
| 588 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::sendSpreadsheetAsTable: called without any listener\n")); |
| 589 |
return false; |
| 590 |
} |
| 591 |
auto it=m_state->m_spreadsheetMap.find(zId); |
| 592 |
if (it == m_state->m_spreadsheetMap.end() || !it->second) { |
| 593 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::sendSpreadsheetAsTable: can not find zone %d!!!\n", zId)); |
| 594 |
return false; |
| 595 |
} |
| 596 |
auto &sheet=*it->second; |
| 597 |
MWAWVec2i minData, maxData; |
| 598 |
if (!sheet.m_content || !sheet.m_content->getExtrema(minData,maxData)) { |
| 599 |
MWAW_DEBUG_MSG(("ClarisWksSpreadsheet::sendSpreadsheetAsTable: can not find content\n")); |
| 600 |
return false; |
| 601 |
} |
| 602 |
std::vector<float> colSize(size_t(maxData[0]-minData[0]+1),72); |
| 603 |
for (int c=minData[0], fC=0; c <= maxData[0]; ++c, ++fC) { |
| 604 |
if (c>=0 && c < int(sheet.m_colWidths.size())) |
| 605 |
colSize[size_t(fC)]=2.0f*float(sheet.m_colWidths[size_t(c)]); |
| 606 |
} |
| 607 |
MWAWTable table(MWAWTable::TableDimBit); |
| 608 |
table.setColsSize(colSize); |
| 609 |
listener->openTable(table); |
| 610 |
for (int r=minData[1], fR=0; r <= maxData[1]; ++r, ++fR) { |
| 611 |
if (sheet.m_rowHeightMap.find(r)!=sheet.m_rowHeightMap.end()) |
| 612 |
listener->openTableRow(float(sheet.m_rowHeightMap.find(r)->second), librevenge::RVNG_POINT); |
| 613 |
else |
| 614 |
listener->openTableRow(14.f, librevenge::RVNG_POINT); |
| 615 |
for (int c=minData[0], fC=0; c <= maxData[0]; ++c, ++fC) { |
| 616 |
MWAWCell cell; |
| 617 |
cell.setPosition(MWAWVec2i(fC,fR)); |
| 618 |
cell.setVAlignment(MWAWCell::VALIGN_BOTTOM); // always ? |
| 619 |
listener->openTableCell(cell); |
| 620 |
sheet.m_content->send(MWAWVec2i(c, r)); |
| 621 |
listener->closeTableCell(); |
| 622 |
} |
| 623 |
listener->closeTableRow(); |
| 624 |
} |
| 625 |
listener->closeTable(); |
| 626 |
return true; |
| 627 |
} |
| 628 |
|
| 629 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |