| 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 |
|
| 35 |
#include <iomanip> |
| 36 |
#include <iostream> |
| 37 |
#include <limits> |
| 38 |
#include <map> |
| 39 |
#include <set> |
| 40 |
#include <sstream> |
| 41 |
|
| 42 |
#include <librevenge/librevenge.h> |
| 43 |
|
| 44 |
#include "MWAWCell.hxx" |
| 45 |
#include "MWAWFont.hxx" |
| 46 |
#include "MWAWFontConverter.hxx" |
| 47 |
#include "MWAWListener.hxx" |
| 48 |
#include "MWAWParser.hxx" |
| 49 |
#include "MWAWPosition.hxx" |
| 50 |
#include "MWAWTable.hxx" |
| 51 |
|
| 52 |
#include "ClarisWksDocument.hxx" |
| 53 |
#include "ClarisWksStruct.hxx" |
| 54 |
#include "ClarisWksStyleManager.hxx" |
| 55 |
|
| 56 |
#include "ClarisWksTable.hxx" |
| 57 |
|
| 58 |
/** Internal: the structures of a ClarisWksTable */ |
| 59 |
namespace ClarisWksTableInternal |
| 60 |
{ |
| 61 |
/** Internal: the border of a ClarisWksTable */ |
| 62 |
struct Border { |
| 63 |
//! the constructor |
| 64 |
Border() |
| 65 |
: m_styleId(-1) |
| 66 |
, m_isSent(false) {} |
| 67 |
//! operator<< |
| 68 |
friend std::ostream &operator<<(std::ostream &o, Border const &bord) |
| 69 |
{ |
| 70 |
for (int i = 0; i < 2; i++) { |
| 71 |
MWAWVec2f pos(bord.m_position[i]); |
| 72 |
pos=(1.f/256.f)*pos; |
| 73 |
o << pos; |
| 74 |
if (i == 0) o << "<->"; |
| 75 |
else o << ","; |
| 76 |
} |
| 77 |
if (bord.m_styleId>0) |
| 78 |
o << "m_styleId?=" << bord.m_styleId << ","; |
| 79 |
return o; |
| 80 |
} |
| 81 |
|
| 82 |
/** the origin and the end of edge position : unit librevenge::RVNG_POINT/256 */ |
| 83 |
MWAWVec2i m_position[2]; |
| 84 |
/// the style id |
| 85 |
int m_styleId; |
| 86 |
/// a flag to know if the border is already sent |
| 87 |
mutable bool m_isSent; |
| 88 |
}; |
| 89 |
|
| 90 |
struct Table; |
| 91 |
/** Internal: a cell inside a ClarisWksTable */ |
| 92 |
struct TableCell final : public MWAWCell { |
| 93 |
//! constructor |
| 94 |
TableCell() |
| 95 |
: MWAWCell() |
| 96 |
, m_zoneId(0) |
| 97 |
, m_styleId(-1) |
| 98 |
{ |
| 99 |
} |
| 100 |
//! use table to finish updating cell |
| 101 |
void update(Table const &table); |
| 102 |
|
| 103 |
//! send the cell content to a listener |
| 104 |
bool sendContent(MWAWListenerPtr listener, MWAWTable &table) final; |
| 105 |
|
| 106 |
//! operator<< |
| 107 |
friend std::ostream &operator<<(std::ostream &o, TableCell const &cell) |
| 108 |
{ |
| 109 |
o << static_cast<MWAWCell const &>(cell); |
| 110 |
if (cell.m_zoneId) o << "zone=" << cell.m_zoneId << ","; |
| 111 |
if (cell.m_styleId >= 0)o << "style=" << cell.m_styleId << ","; |
| 112 |
return o; |
| 113 |
} |
| 114 |
|
| 115 |
/** the cell zone ( 0 is no content ) */ |
| 116 |
int m_zoneId; |
| 117 |
/** the list of border id : Left, Top, Right, Bottom |
| 118 |
|
| 119 |
Normally, one id but merge cells can have mutiple border |
| 120 |
*/ |
| 121 |
std::vector<int> m_bordersId[4]; |
| 122 |
/// the style id |
| 123 |
int m_styleId; |
| 124 |
|
| 125 |
private: |
| 126 |
TableCell(TableCell const &orig) = delete; |
| 127 |
TableCell &operator=(TableCell const &orig) = delete; |
| 128 |
}; |
| 129 |
|
| 130 |
//////////////////////////////////////// |
| 131 |
//////////////////////////////////////// |
| 132 |
/** the struct which stores the Table */ |
| 133 |
struct Table final : public ClarisWksStruct::DSET, public MWAWTable { |
| 134 |
friend struct TableCell; |
| 135 |
//! constructor |
| 136 |
Table(ClarisWksStruct::DSET const &dset, ClarisWksTable &parser, ClarisWksStyleManager &styleManager) |
| 137 |
: ClarisWksStruct::DSET(dset) |
| 138 |
, MWAWTable() |
| 139 |
, m_parser(&parser) |
| 140 |
, m_styleManager(&styleManager) |
| 141 |
, m_bordersList() |
| 142 |
, m_mainPtr(-1) |
| 143 |
, m_auxiliaryDetached(false) |
| 144 |
{ |
| 145 |
} |
| 146 |
//! destructor |
| 147 |
~Table() final; |
| 148 |
//! return a cell corresponding to id |
| 149 |
TableCell *get(int id) |
| 150 |
{ |
| 151 |
if (id < 0 || id >= numCells()) { |
| 152 |
MWAW_DEBUG_MSG(("ClarisWksTableInteral::Table::get: cell %d does not exists\n",id)); |
| 153 |
return nullptr; |
| 154 |
} |
| 155 |
return static_cast<TableCell *>(MWAWTable::get(id).get()); |
| 156 |
} |
| 157 |
//! operator<< |
| 158 |
friend std::ostream &operator<<(std::ostream &o, Table const &doc) |
| 159 |
{ |
| 160 |
o << static_cast<ClarisWksStruct::DSET const &>(doc); |
| 161 |
return o; |
| 162 |
} |
| 163 |
/** remove a child from a list. |
| 164 |
|
| 165 |
Normally, this function is not called, so optimizing it is not usefull |
| 166 |
*/ |
| 167 |
void removeChild(int cId, bool normalChild) final |
| 168 |
{ |
| 169 |
DSET::removeChild(cId, normalChild); |
| 170 |
if (cId==m_id+1) { |
| 171 |
m_auxiliaryDetached=true; |
| 172 |
return; |
| 173 |
} |
| 174 |
for (auto &i : m_cellsList) { |
| 175 |
auto *cell = static_cast<TableCell *>(i.get()); |
| 176 |
if (!cell || cell->m_zoneId!=cId) continue; |
| 177 |
cell->m_zoneId=0; |
| 178 |
return; |
| 179 |
} |
| 180 |
MWAW_DEBUG_MSG(("ClarisWksTableInternal::Zone can not detach %d\n", cId)); |
| 181 |
} |
| 182 |
|
| 183 |
//! finish updating all cells |
| 184 |
void updateCells() |
| 185 |
{ |
| 186 |
for (int c=0; c<numCells(); ++c) { |
| 187 |
if (!get(c)) continue; |
| 188 |
get(c)->update(*this); |
| 189 |
} |
| 190 |
} |
| 191 |
//! ask the main parser to send a zone |
| 192 |
bool askMainToSendZone(int number) |
| 193 |
{ |
| 194 |
return m_parser->askMainToSendZone(number); |
| 195 |
} |
| 196 |
|
| 197 |
/** the main parser */ |
| 198 |
ClarisWksTable *m_parser; |
| 199 |
/** the style manager */ |
| 200 |
ClarisWksStyleManager *m_styleManager; |
| 201 |
/** the list of border */ |
| 202 |
std::vector<Border> m_bordersList; |
| 203 |
/** the relative main pointer */ |
| 204 |
long m_mainPtr; |
| 205 |
//! true if the auxiliary zone is detached |
| 206 |
bool m_auxiliaryDetached; |
| 207 |
|
| 208 |
private: |
| 209 |
Table(Table const &orig) = delete; |
| 210 |
Table &operator=(Table const &orig) = delete; |
| 211 |
}; |
| 212 |
|
| 213 |
Table::~Table() |
| 214 |
{ |
| 215 |
} |
| 216 |
|
| 217 |
void TableCell::update(Table const &table) |
| 218 |
{ |
| 219 |
auto *styleManager = table.m_styleManager; |
| 220 |
if (!styleManager) { |
| 221 |
MWAW_DEBUG_MSG(("ClarisWksTableInternal::TableCell::update: style manager is not defined\n")); |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
auto numTableBorders = static_cast<int>(table.m_bordersList.size()); |
| 226 |
// now look for border id: L,T,R,B |
| 227 |
for (int w = 0; w < 4; w++) { |
| 228 |
size_t numBorders = m_bordersId[w].size(); |
| 229 |
if (!numBorders) continue; |
| 230 |
|
| 231 |
int bId = m_bordersId[w][0]; |
| 232 |
bool sameBorders = true; |
| 233 |
for (size_t b = 1; b < numBorders; b++) { |
| 234 |
if (m_bordersId[w][b]!=bId) { |
| 235 |
sameBorders = false; |
| 236 |
break; |
| 237 |
} |
| 238 |
} |
| 239 |
/** fixme: check that the opposite has a border, if not print the first border */ |
| 240 |
if (!sameBorders) continue; |
| 241 |
if (bId < 0 || bId >= numTableBorders) { |
| 242 |
MWAW_DEBUG_MSG(("ClarisWksTableInternal::TableCell::get: can not find the border definition\n")); |
| 243 |
continue; |
| 244 |
} |
| 245 |
auto const &border = table.m_bordersList[size_t(bId)]; |
| 246 |
ClarisWksStyleManager::Style bStyle; |
| 247 |
if (border.m_isSent || border.m_styleId < 0 || !styleManager->get(border.m_styleId, bStyle)) |
| 248 |
continue; |
| 249 |
border.m_isSent = true; |
| 250 |
MWAWGraphicStyle graph; |
| 251 |
bool haveGraph = false; |
| 252 |
if (bStyle.m_graphicId >= 0) |
| 253 |
haveGraph = styleManager->get(bStyle.m_graphicId, graph); |
| 254 |
ClarisWksStyleManager::KSEN ksen; |
| 255 |
bool haveKSEN = false; |
| 256 |
if (bStyle.m_ksenId >= 0) |
| 257 |
haveKSEN = styleManager->get(bStyle.m_ksenId, ksen); |
| 258 |
|
| 259 |
MWAWBorder bord; |
| 260 |
if (haveGraph && !graph.hasLine()) |
| 261 |
bord.m_style = MWAWBorder::None; |
| 262 |
else if (haveKSEN) { |
| 263 |
bord.m_style = ksen.m_lineType; |
| 264 |
bord.m_type = ksen.m_lineRepeat; |
| 265 |
if (bord.m_type == MWAWBorder::Double) |
| 266 |
bord.m_width = 0.5*double(graph.m_lineWidth); |
| 267 |
else |
| 268 |
bord.m_width = double(graph.m_lineWidth); |
| 269 |
bord.m_color = graph.m_lineColor; |
| 270 |
} |
| 271 |
static int const wh[] = { libmwaw::LeftBit, libmwaw::TopBit, libmwaw::RightBit, libmwaw::BottomBit}; |
| 272 |
setBorders(wh[w], bord); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
bool TableCell::sendContent(MWAWListenerPtr listener, MWAWTable &table) |
| 277 |
{ |
| 278 |
if (!listener) return true; |
| 279 |
if (m_zoneId <= 0) |
| 280 |
listener->insertChar(' '); |
| 281 |
else |
| 282 |
static_cast<Table &>(table).askMainToSendZone(m_zoneId); |
| 283 |
return true; |
| 284 |
} |
| 285 |
|
| 286 |
//////////////////////////////////////// |
| 287 |
//! Internal: the state of a ClarisWksTable |
| 288 |
struct State { |
| 289 |
//! constructor |
| 290 |
State() |
| 291 |
: m_tableMap() |
| 292 |
{ |
| 293 |
} |
| 294 |
//! map id -> table |
| 295 |
std::map<int, std::shared_ptr<Table> > m_tableMap; |
| 296 |
}; |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
//////////////////////////////////////////////////////////// |
| 301 |
// constructor/destructor, ... |
| 302 |
//////////////////////////////////////////////////////////// |
| 303 |
ClarisWksTable::ClarisWksTable(ClarisWksDocument &document) |
| 304 |
: m_document(document) |
| 305 |
, m_parserState(document.m_parserState) |
| 306 |
, m_state(new ClarisWksTableInternal::State) |
| 307 |
, m_mainParser(&document.getMainParser()) |
| 308 |
{ |
| 309 |
} |
| 310 |
|
| 311 |
ClarisWksTable::~ClarisWksTable() |
| 312 |
{ |
| 313 |
} |
| 314 |
|
| 315 |
int ClarisWksTable::version() const |
| 316 |
{ |
| 317 |
return m_parserState->m_version; |
| 318 |
} |
| 319 |
|
| 320 |
bool ClarisWksTable::askMainToSendZone(int number) |
| 321 |
{ |
| 322 |
return m_document.sendZone(number); |
| 323 |
} |
| 324 |
|
| 325 |
// fixme |
| 326 |
int ClarisWksTable::numPages() const |
| 327 |
{ |
| 328 |
return 1; |
| 329 |
} |
| 330 |
//////////////////////////////////////////////////////////// |
| 331 |
// Intermediate level |
| 332 |
//////////////////////////////////////////////////////////// |
| 333 |
|
| 334 |
//////////////////////////////////////////////////////////// |
| 335 |
// a document part |
| 336 |
//////////////////////////////////////////////////////////// |
| 337 |
std::shared_ptr<ClarisWksStruct::DSET> ClarisWksTable::readTableZone |
| 338 |
(ClarisWksStruct::DSET const &zone, MWAWEntry const &entry, bool &complete) |
| 339 |
{ |
| 340 |
complete = false; |
| 341 |
if (!entry.valid() || zone.m_fileType != 6 || entry.length() < 32) |
| 342 |
return std::shared_ptr<ClarisWksStruct::DSET>(); |
| 343 |
long pos = entry.begin(); |
| 344 |
MWAWInputStreamPtr &input= m_parserState->m_input; |
| 345 |
input->seek(pos+8+16, librevenge::RVNG_SEEK_SET); // avoid header+8 generic number |
| 346 |
libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile; |
| 347 |
libmwaw::DebugStream f; |
| 348 |
std::shared_ptr<ClarisWksTableInternal::Table> tableZone(new ClarisWksTableInternal::Table(zone, *this, *m_document.getStyleManager())); |
| 349 |
|
| 350 |
f << "Entries(TableDef):" << *tableZone << ","; |
| 351 |
float dim[2]; |
| 352 |
for (auto &d : dim) d = float(input->readLong(4))/256.f; |
| 353 |
f << "dim=" << dim[0] << "x" << dim[1] << ","; |
| 354 |
long val; |
| 355 |
for (int i = 0; i < 3; i++) { |
| 356 |
// f1=parentZoneId ? |
| 357 |
val = input->readLong(2); |
| 358 |
if (val) f << "f" << i << "=" << val << ","; |
| 359 |
} |
| 360 |
tableZone->m_mainPtr = long(input->readULong(4)); |
| 361 |
f << "PTR=X" << std::hex << tableZone->m_mainPtr << std::dec << ","; |
| 362 |
for (int i = 0; i < 2; i++) { // find 0,0 or -1,-1 here |
| 363 |
val = input->readLong(2); |
| 364 |
if (val) f << "f" << i+3 << "=" << val << ","; |
| 365 |
} |
| 366 |
f << "relPtr=PTR+[" << std::hex; |
| 367 |
for (int i = 0; i < 3; i++) { |
| 368 |
/** find 3 ptr here in general >= PTR, very often PTR+4,PTR+8,PTR+c, |
| 369 |
but can be more complex for instance PTR+354,PTR-6924,PTR+7fc, */ |
| 370 |
val = long(input->readULong(4)); |
| 371 |
if (val > tableZone->m_mainPtr) |
| 372 |
f << val-tableZone->m_mainPtr << ","; |
| 373 |
else |
| 374 |
f << "-" << tableZone->m_mainPtr-val << ","; |
| 375 |
} |
| 376 |
f << std::dec << "],"; |
| 377 |
ascFile.addDelimiter(input->tell(), '|'); |
| 378 |
ascFile.addPos(pos); |
| 379 |
ascFile.addNote(f.str().c_str()); |
| 380 |
|
| 381 |
// read the last part |
| 382 |
long data0Length = zone.m_dataSz; |
| 383 |
long N = zone.m_numData; |
| 384 |
if (entry.length() -8-12 != data0Length*N + zone.m_headerSz) { |
| 385 |
if (data0Length == 0 && N) { |
| 386 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTableZone: can not find definition size\n")); |
| 387 |
input->seek(entry.end(), librevenge::RVNG_SEEK_SET); |
| 388 |
return std::shared_ptr<ClarisWksStruct::DSET>(); |
| 389 |
} |
| 390 |
|
| 391 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTableZone: unexpected size for zone definition, try to continue\n")); |
| 392 |
} |
| 393 |
|
| 394 |
if (long(input->tell())+N*data0Length > entry.end()) { |
| 395 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTableZone: file is too short\n")); |
| 396 |
return std::shared_ptr<ClarisWksStruct::DSET>(); |
| 397 |
} |
| 398 |
if (N) { |
| 399 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTableZone: find some tabledef !!!\n")); |
| 400 |
input->seek(entry.end()-N*data0Length, librevenge::RVNG_SEEK_SET); |
| 401 |
|
| 402 |
for (long i = 0; i < N; i++) { |
| 403 |
pos = input->tell(); |
| 404 |
|
| 405 |
f.str(""); |
| 406 |
f << "TableDef#"; |
| 407 |
ascFile.addPos(pos); |
| 408 |
ascFile.addNote(f.str().c_str()); |
| 409 |
input->seek(pos+data0Length, librevenge::RVNG_SEEK_SET); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
input->seek(entry.end(), librevenge::RVNG_SEEK_SET); |
| 414 |
|
| 415 |
pos = input->tell(); |
| 416 |
bool ok = readTableBorders(*tableZone); |
| 417 |
if (ok) { |
| 418 |
pos = input->tell(); |
| 419 |
ok = readTableCells(*tableZone); |
| 420 |
} |
| 421 |
/** three fields which seems to follows the list of cells |
| 422 |
zone 0 : looks like a list of integer : related to last selected border ? |
| 423 |
zone 1 : looks like a list of integer : unknown meaning |
| 424 |
*/ |
| 425 |
for (int i = 0; ok && i < 2; i++) { |
| 426 |
std::stringstream s; |
| 427 |
s << "TableUnknown-" << i; |
| 428 |
std::vector<int> res; |
| 429 |
pos = input->tell(); |
| 430 |
ok = ClarisWksStruct::readIntZone(*m_parserState, s.str().c_str(), false, 2, res); |
| 431 |
} |
| 432 |
if (ok) { |
| 433 |
pos = input->tell(); |
| 434 |
ok = readTablePointers(*tableZone); |
| 435 |
if (!ok) { |
| 436 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 437 |
ok = ClarisWksStruct::readStructZone(*m_parserState, "TablePointers", false); |
| 438 |
} |
| 439 |
} |
| 440 |
if (ok) { |
| 441 |
pos = input->tell(); |
| 442 |
ok = readTableBordersId(*tableZone); |
| 443 |
} |
| 444 |
|
| 445 |
if (!ok) |
| 446 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 447 |
|
| 448 |
tableZone->updateCells(); |
| 449 |
if (m_state->m_tableMap.find(tableZone->m_id) != m_state->m_tableMap.end()) { |
| 450 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTableZone: zone %d already exists!!!\n", tableZone->m_id)); |
| 451 |
} |
| 452 |
else |
| 453 |
m_state->m_tableMap[tableZone->m_id] = tableZone; |
| 454 |
|
| 455 |
tableZone->m_otherChilds.push_back(tableZone->m_id+1); |
| 456 |
return tableZone; |
| 457 |
} |
| 458 |
|
| 459 |
bool ClarisWksTable::sendZone(int number) |
| 460 |
{ |
| 461 |
auto iter = m_state->m_tableMap.find(number); |
| 462 |
if (iter == m_state->m_tableMap.end()) |
| 463 |
return false; |
| 464 |
auto table = iter->second; |
| 465 |
table->m_parsed = true; |
| 466 |
if (!table->m_auxiliaryDetached) |
| 467 |
m_document.forceParsed(number+1); |
| 468 |
|
| 469 |
MWAWListenerPtr listener=m_parserState->getMainListener(); |
| 470 |
if (!listener) |
| 471 |
return true; |
| 472 |
|
| 473 |
if (table->sendTable(listener)) |
| 474 |
return true; |
| 475 |
return table->sendAsText(listener); |
| 476 |
} |
| 477 |
|
| 478 |
void ClarisWksTable::flushExtra() |
| 479 |
{ |
| 480 |
for (auto const &iter : m_state->m_tableMap) { |
| 481 |
std::shared_ptr<ClarisWksTableInternal::Table> table = iter.second; |
| 482 |
if (table->m_parsed) |
| 483 |
continue; |
| 484 |
if (m_parserState->getMainListener()) m_parserState->getMainListener()->insertEOL(); |
| 485 |
sendZone(iter.first); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
//////////////////////////////////////////////////////////// |
| 490 |
// |
| 491 |
// Intermediate level |
| 492 |
// |
| 493 |
//////////////////////////////////////////////////////////// |
| 494 |
bool ClarisWksTable::readTableBorders(ClarisWksTableInternal::Table &table) |
| 495 |
{ |
| 496 |
MWAWInputStreamPtr &input= m_parserState->m_input; |
| 497 |
long pos = input->tell(); |
| 498 |
ClarisWksStruct::Struct header; |
| 499 |
if (!header.readHeader(input,true) || header.m_dataSize<18) { |
| 500 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTableBorders: can not read the header\n")); |
| 501 |
return false; |
| 502 |
} |
| 503 |
libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile; |
| 504 |
libmwaw::DebugStream f; |
| 505 |
if (header.m_size==0) { |
| 506 |
ascFile.addPos(pos); |
| 507 |
ascFile.addNote("Nop"); |
| 508 |
return true; |
| 509 |
} |
| 510 |
long endPos=pos+4+header.m_size; |
| 511 |
f << "Entries(TableBorders):" << header; |
| 512 |
if (header.m_headerSize) { |
| 513 |
ascFile.addDelimiter(input->tell(),'|'); |
| 514 |
input->seek(header.m_headerSize, librevenge::RVNG_SEEK_CUR); |
| 515 |
} |
| 516 |
ascFile.addPos(pos); |
| 517 |
ascFile.addNote(f.str().c_str()); |
| 518 |
|
| 519 |
for (long i = 0; i < header.m_numData; i++) { |
| 520 |
pos = input->tell(); |
| 521 |
ClarisWksTableInternal::Border border; |
| 522 |
f.str(""); |
| 523 |
f << "TableBorders-" << i << ":"; |
| 524 |
int posi[4]; |
| 525 |
for (auto &p : posi) p = static_cast<int>(input->readLong(4)); |
| 526 |
border.m_position[0] = MWAWVec2i(posi[1], posi[0]); |
| 527 |
border.m_position[1] = MWAWVec2i(posi[3], posi[2]); |
| 528 |
border.m_styleId = static_cast<int>(input->readULong(2)); |
| 529 |
table.m_bordersList.push_back(border); |
| 530 |
f << border; |
| 531 |
|
| 532 |
ClarisWksStyleManager::Style style; |
| 533 |
if (border.m_styleId < 0) ; |
| 534 |
else if (!m_document.getStyleManager()->get(border.m_styleId, style)) { |
| 535 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTableBorders: can not find cell style\n")); |
| 536 |
f << "###style"; |
| 537 |
} |
| 538 |
else { |
| 539 |
ClarisWksStyleManager::KSEN ksen; |
| 540 |
if (style.m_ksenId >= 0 && m_document.getStyleManager()->get(style.m_ksenId, ksen)) { |
| 541 |
f << "ksen=[" << ksen << "],"; |
| 542 |
} |
| 543 |
MWAWGraphicStyle graph; |
| 544 |
if (style.m_graphicId >= 0 && m_document.getStyleManager()->get(style.m_graphicId, graph)) { |
| 545 |
f << "graph=[" << graph << "],"; |
| 546 |
} |
| 547 |
//f << "[" << style << "],"; |
| 548 |
} |
| 549 |
|
| 550 |
if (long(input->tell()) != pos+header.m_dataSize) |
| 551 |
ascFile.addDelimiter(input->tell(), '|'); |
| 552 |
ascFile.addPos(pos); |
| 553 |
ascFile.addNote(f.str().c_str()); |
| 554 |
input->seek(pos+header.m_dataSize, librevenge::RVNG_SEEK_SET); |
| 555 |
} |
| 556 |
|
| 557 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 558 |
return true; |
| 559 |
} |
| 560 |
|
| 561 |
bool ClarisWksTable::readTableCells(ClarisWksTableInternal::Table &table) |
| 562 |
{ |
| 563 |
MWAWInputStreamPtr &input= m_parserState->m_input; |
| 564 |
long pos = input->tell(); |
| 565 |
ClarisWksStruct::Struct header; |
| 566 |
if (!header.readHeader(input,true) || header.m_dataSize<32) { |
| 567 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTableCells: can not read the header\n")); |
| 568 |
return false; |
| 569 |
} |
| 570 |
libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile; |
| 571 |
libmwaw::DebugStream f; |
| 572 |
if (header.m_size==0) { |
| 573 |
ascFile.addPos(pos); |
| 574 |
ascFile.addNote("Nop"); |
| 575 |
return true; |
| 576 |
} |
| 577 |
long endPos = pos+4+header.m_size; |
| 578 |
f << "Entries(TableCell):" << header; |
| 579 |
if (header.m_headerSize) { |
| 580 |
ascFile.addDelimiter(input->tell(),'|'); |
| 581 |
input->seek(header.m_headerSize, librevenge::RVNG_SEEK_CUR); |
| 582 |
} |
| 583 |
ascFile.addPos(pos); |
| 584 |
ascFile.addNote(f.str().c_str()); |
| 585 |
|
| 586 |
for (long i = 0; i < header.m_numData; i++) { |
| 587 |
pos = input->tell(); |
| 588 |
std::shared_ptr<ClarisWksTableInternal::TableCell> cell(new ClarisWksTableInternal::TableCell()); |
| 589 |
float posi[6]; |
| 590 |
for (auto &p : posi) p = float(input->readLong(4))/256.f; |
| 591 |
cell->setBdBox(MWAWBox2f(MWAWVec2f(posi[1], posi[0]), MWAWVec2f(posi[3], posi[2]))); |
| 592 |
cell->setBdSize(MWAWVec2f(float(posi[5]), float(posi[4]))); |
| 593 |
cell->m_zoneId = static_cast<int>(input->readULong(4)); |
| 594 |
auto val = static_cast<int>(input->readLong(2)); |
| 595 |
if (val) // find one time a number here, another id?... |
| 596 |
f << "#unkn=" << val << ","; |
| 597 |
cell->m_styleId = static_cast<int>(input->readULong(2)); |
| 598 |
table.add(cell); |
| 599 |
if (cell->m_zoneId) |
| 600 |
table.m_otherChilds.push_back(cell->m_zoneId); |
| 601 |
f.str(""); |
| 602 |
f << "TableCell-" << i << ":"; |
| 603 |
ClarisWksStyleManager::Style style; |
| 604 |
if (cell->m_styleId < 0) ; |
| 605 |
else if (!m_document.getStyleManager()->get(cell->m_styleId, style)) { |
| 606 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTableCells: can not find cell style\n")); |
| 607 |
f << *cell << "###style"; |
| 608 |
} |
| 609 |
else { |
| 610 |
ClarisWksStyleManager::KSEN ksen; |
| 611 |
bool hasExtraLines=false; |
| 612 |
if (style.m_ksenId >= 0 && m_document.getStyleManager()->get(style.m_ksenId, ksen)) { |
| 613 |
switch (ksen.m_valign) { |
| 614 |
case 1: |
| 615 |
cell->setVAlignment(MWAWCell::VALIGN_CENTER); |
| 616 |
break; |
| 617 |
case 2: |
| 618 |
cell->setVAlignment(MWAWCell::VALIGN_BOTTOM); |
| 619 |
break; |
| 620 |
default: |
| 621 |
break; |
| 622 |
} |
| 623 |
hasExtraLines=(ksen.m_lines & 3); |
| 624 |
switch (ksen.m_lines & 3) { |
| 625 |
case 1: // TL->BR |
| 626 |
cell->setExtraLine(MWAWCell::E_Line1); |
| 627 |
break; |
| 628 |
case 2: // BL->TR |
| 629 |
cell->setExtraLine(MWAWCell::E_Line2); |
| 630 |
break; |
| 631 |
case 3: |
| 632 |
cell->setExtraLine(MWAWCell::E_Cross); |
| 633 |
break; |
| 634 |
default: |
| 635 |
case 0: // None |
| 636 |
break; |
| 637 |
} |
| 638 |
f << "ksen=[" << ksen << "],"; |
| 639 |
} |
| 640 |
MWAWGraphicStyle graph; |
| 641 |
if (style.m_graphicId >= 0 && m_document.getStyleManager()->get(style.m_graphicId, graph)) { |
| 642 |
// checkme: no always there |
| 643 |
if (graph.hasSurfaceColor()) |
| 644 |
cell->setBackgroundColor(graph.m_surfaceColor); |
| 645 |
if (hasExtraLines) { |
| 646 |
MWAWBorder border; |
| 647 |
border.m_width=double(graph.m_lineWidth); |
| 648 |
border.m_color=graph.m_lineColor; |
| 649 |
cell->setExtraLine(cell->extraLine(), border); |
| 650 |
} |
| 651 |
f << "graph=[" << graph << "],"; |
| 652 |
} |
| 653 |
f << *cell; |
| 654 |
//f << "[" << style << "],"; |
| 655 |
} |
| 656 |
|
| 657 |
if (long(input->tell()) != pos+header.m_dataSize) |
| 658 |
ascFile.addDelimiter(input->tell(), '|'); |
| 659 |
ascFile.addPos(pos); |
| 660 |
ascFile.addNote(f.str().c_str()); |
| 661 |
input->seek(pos+header.m_dataSize, librevenge::RVNG_SEEK_SET); |
| 662 |
} |
| 663 |
|
| 664 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 665 |
return true; |
| 666 |
} |
| 667 |
|
| 668 |
bool ClarisWksTable::readTableBordersId(ClarisWksTableInternal::Table &table) |
| 669 |
{ |
| 670 |
MWAWInputStreamPtr &input= m_parserState->m_input; |
| 671 |
int numCells = table.numCells(); |
| 672 |
auto numBorders = int(table.m_bordersList.size()); |
| 673 |
libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile; |
| 674 |
for (int i = 0; i < 4*numCells; i++) { |
| 675 |
auto *cell = table.get(i/4); |
| 676 |
long pos = input->tell(); |
| 677 |
ClarisWksStruct::Struct header; |
| 678 |
if (!header.readHeader(input,true) || header.m_dataSize != 2 || header.m_numData==0) { |
| 679 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTableBordersId: can not read the header\n")); |
| 680 |
return false; |
| 681 |
} |
| 682 |
long endPos = pos+4+header.m_size; |
| 683 |
libmwaw::DebugStream f; |
| 684 |
f << "Entries(TableBordersId)[" << i/4 << "," << i%4 << "]," << header; |
| 685 |
if (header.m_headerSize) { |
| 686 |
ascFile.addDelimiter(input->tell(),'|'); |
| 687 |
input->seek(header.m_headerSize, librevenge::RVNG_SEEK_CUR); |
| 688 |
} |
| 689 |
|
| 690 |
std::vector<int> idsList; |
| 691 |
for (long j = 0; j < header.m_numData; j++) { |
| 692 |
auto id = static_cast<int>(input->readLong(2)); |
| 693 |
if (id < 0 || id >= numBorders) { |
| 694 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 695 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTableBordersId: unexpected id\n")); |
| 696 |
return false; |
| 697 |
} |
| 698 |
idsList.push_back(id); |
| 699 |
if (j) |
| 700 |
f << "bordId" << j << "=" << id << ","; |
| 701 |
else |
| 702 |
f << "bordId=" << id << ","; |
| 703 |
} |
| 704 |
if (cell) |
| 705 |
cell->m_bordersId[i%4] = idsList; |
| 706 |
ascFile.addPos(pos); |
| 707 |
ascFile.addNote(f.str().c_str()); |
| 708 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 709 |
} |
| 710 |
return true; |
| 711 |
} |
| 712 |
|
| 713 |
bool ClarisWksTable::readTablePointers(ClarisWksTableInternal::Table &table) |
| 714 |
{ |
| 715 |
MWAWInputStreamPtr &input= m_parserState->m_input; |
| 716 |
long pos = input->tell(); |
| 717 |
ClarisWksStruct::Struct header; |
| 718 |
if (!header.readHeader(input,true) || (header.m_size && header.m_dataSize<16)) { |
| 719 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTablePointers: can not read the header\n")); |
| 720 |
return false; |
| 721 |
} |
| 722 |
libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile; |
| 723 |
libmwaw::DebugStream f; |
| 724 |
if (header.m_size==0) { |
| 725 |
// find this one time as the last entry which ends the table |
| 726 |
ascFile.addPos(pos); |
| 727 |
ascFile.addNote("Nop"); |
| 728 |
return true; |
| 729 |
} |
| 730 |
long endPos=pos+4+header.m_size; |
| 731 |
f << "Entries(TablePointers):" << header; |
| 732 |
if (header.m_numData != table.numCells()) { |
| 733 |
MWAW_DEBUG_MSG(("ClarisWksTable::readTablePointers: the number of pointers seems odd\n")); |
| 734 |
f << "###N,"; |
| 735 |
} |
| 736 |
if (header.m_headerSize) { |
| 737 |
ascFile.addDelimiter(input->tell(),'|'); |
| 738 |
input->seek(header.m_headerSize, librevenge::RVNG_SEEK_CUR); |
| 739 |
} |
| 740 |
ascFile.addPos(pos); |
| 741 |
ascFile.addNote(f.str().c_str()); |
| 742 |
|
| 743 |
for (long i = 0; i < header.m_numData; i++) { |
| 744 |
pos = input->tell(); |
| 745 |
f.str(""); |
| 746 |
f << "TablePointers-" << i << ":PTR+[" << std::hex; |
| 747 |
for (int j = 0; j < 4; j++) { |
| 748 |
auto val = long(input->readULong(4)); |
| 749 |
if (val > table.m_mainPtr) f << val-table.m_mainPtr << ","; |
| 750 |
else f << "-" << table.m_mainPtr-val << ","; |
| 751 |
} |
| 752 |
f << "]" << std::dec; |
| 753 |
if (long(input->tell()) != pos+header.m_dataSize) |
| 754 |
ascFile.addDelimiter(input->tell(), '|'); |
| 755 |
ascFile.addPos(pos); |
| 756 |
ascFile.addNote(f.str().c_str()); |
| 757 |
input->seek(pos+header.m_dataSize, librevenge::RVNG_SEEK_SET); |
| 758 |
} |
| 759 |
|
| 760 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 761 |
return true; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |