| 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 <set> |
| 38 |
#include <sstream> |
| 39 |
|
| 40 |
#include <librevenge/librevenge.h> |
| 41 |
|
| 42 |
#include "MWAWTextListener.hxx" |
| 43 |
#include "MWAWHeader.hxx" |
| 44 |
#include "MWAWParagraph.hxx" |
| 45 |
#include "MWAWParser.hxx" |
| 46 |
#include "MWAWPosition.hxx" |
| 47 |
#include "MWAWPrinter.hxx" |
| 48 |
#include "MWAWSection.hxx" |
| 49 |
#include "MWAWRSRCParser.hxx" |
| 50 |
|
| 51 |
#include "GreatWksGraph.hxx" |
| 52 |
#include "GreatWksText.hxx" |
| 53 |
|
| 54 |
#include "GreatWksDocument.hxx" |
| 55 |
|
| 56 |
/** Internal: the structures of a GreatWksDocument */ |
| 57 |
namespace GreatWksDocumentInternal |
| 58 |
{ |
| 59 |
|
| 60 |
//////////////////////////////////////// |
| 61 |
//! Internal: the state of a GreatWksDocument |
| 62 |
struct State { |
| 63 |
//! constructor |
| 64 |
State() |
| 65 |
: m_actPage(0) |
| 66 |
, m_numPages(0) |
| 67 |
, m_headerHeight(0) |
| 68 |
, m_footerHeight(0) |
| 69 |
{ |
| 70 |
} |
| 71 |
|
| 72 |
int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */; |
| 73 |
|
| 74 |
int m_headerHeight /** the header height if known */, |
| 75 |
m_footerHeight /** the footer height if known */; |
| 76 |
}; |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
//////////////////////////////////////////////////////////// |
| 81 |
// constructor/destructor, ... |
| 82 |
//////////////////////////////////////////////////////////// |
| 83 |
GreatWksDocument::GreatWksDocument(MWAWParser &parser) |
| 84 |
: m_state() |
| 85 |
, m_parserState(parser.getParserState()) |
| 86 |
, m_parser(&parser) |
| 87 |
, m_graphParser() |
| 88 |
, m_textParser() |
| 89 |
, m_getMainSection(nullptr) |
| 90 |
, m_newPage(nullptr) |
| 91 |
{ |
| 92 |
m_state.reset(new GreatWksDocumentInternal::State); |
| 93 |
|
| 94 |
m_graphParser.reset(new GreatWksGraph(*this)); |
| 95 |
m_textParser.reset(new GreatWksText(*this)); |
| 96 |
} |
| 97 |
|
| 98 |
GreatWksDocument::~GreatWksDocument() |
| 99 |
{ |
| 100 |
} |
| 101 |
|
| 102 |
MWAWInputStreamPtr GreatWksDocument::rsrcInput() |
| 103 |
{ |
| 104 |
return m_parser->getRSRCParser()->getInput(); |
| 105 |
} |
| 106 |
|
| 107 |
libmwaw::DebugFile &GreatWksDocument::rsrcAscii() |
| 108 |
{ |
| 109 |
return m_parser->getRSRCParser()->ascii(); |
| 110 |
} |
| 111 |
|
| 112 |
//////////////////////////////////////////////////////////// |
| 113 |
// interface via callback |
| 114 |
//////////////////////////////////////////////////////////// |
| 115 |
MWAWSection GreatWksDocument::getMainSection() const |
| 116 |
{ |
| 117 |
if (!m_getMainSection) { |
| 118 |
MWAW_DEBUG_MSG(("GreatWksDocument::getMainSection: can not find getMainSection callback\n")); |
| 119 |
return MWAWSection(); |
| 120 |
} |
| 121 |
return (m_parser->*m_getMainSection)(); |
| 122 |
} |
| 123 |
|
| 124 |
void GreatWksDocument::newPage(int page) |
| 125 |
{ |
| 126 |
if (!m_newPage) { |
| 127 |
MWAW_DEBUG_MSG(("GreatWksDocument::newPage: can not find newPage callback\n")); |
| 128 |
return; |
| 129 |
} |
| 130 |
(m_parser->*m_newPage)(page); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
//////////////////////////////////////////////////////////// |
| 135 |
// interface with the text document |
| 136 |
//////////////////////////////////////////////////////////// |
| 137 |
bool GreatWksDocument::sendTextbox(MWAWEntry const &entry, MWAWListenerPtr listener) |
| 138 |
{ |
| 139 |
MWAWInputStreamPtr input = m_parserState->m_input; |
| 140 |
long actPos = input->tell(); |
| 141 |
bool ok=getTextParser()->sendTextbox(entry, listener); |
| 142 |
input->seek(actPos, librevenge::RVNG_SEEK_SET); |
| 143 |
return ok; |
| 144 |
} |
| 145 |
|
| 146 |
bool GreatWksDocument::canSendTextboxAsGraphic(MWAWEntry const &entry) |
| 147 |
{ |
| 148 |
MWAWInputStreamPtr input = m_parserState->m_input; |
| 149 |
long actPos = input->tell(); |
| 150 |
bool ok=getTextParser()->canSendTextBoxAsGraphic(entry); |
| 151 |
input->seek(actPos, librevenge::RVNG_SEEK_SET); |
| 152 |
return ok; |
| 153 |
} |
| 154 |
|
| 155 |
//////////////////////////////////////////////////////////// |
| 156 |
// interface with the graph document |
| 157 |
//////////////////////////////////////////////////////////// |
| 158 |
bool GreatWksDocument::sendPicture(MWAWEntry const &entry, MWAWPosition const &pos) |
| 159 |
{ |
| 160 |
MWAWInputStreamPtr input = m_parserState->m_input; |
| 161 |
long actPos = input->tell(); |
| 162 |
bool ok=getGraphParser()->sendPicture(entry, pos); |
| 163 |
input->seek(actPos, librevenge::RVNG_SEEK_SET); |
| 164 |
return ok; |
| 165 |
} |
| 166 |
|
| 167 |
//////////////////////////////////////////////////////////// |
| 168 |
// |
| 169 |
// Intermediate level |
| 170 |
// |
| 171 |
//////////////////////////////////////////////////////////// |
| 172 |
bool GreatWksDocument::readRSRCZones() |
| 173 |
{ |
| 174 |
MWAWRSRCParserPtr rsrcParser = m_parser->getRSRCParser(); |
| 175 |
if (!rsrcParser) |
| 176 |
return true; |
| 177 |
|
| 178 |
auto const &entryMap = rsrcParser->getEntriesMap(); |
| 179 |
// the 1 zone |
| 180 |
char const *zNames[] = {"PRNT", "PAT#", "WPSN", "PlTT", "ARRs", "DaHS", "GrDS", "NxEd" }; |
| 181 |
for (int z = 0; z < 8; ++z) { |
| 182 |
auto it = entryMap.lower_bound(zNames[z]); |
| 183 |
while (it != entryMap.end()) { |
| 184 |
if (it->first != zNames[z]) |
| 185 |
break; |
| 186 |
MWAWEntry const &entry = it++->second; |
| 187 |
switch (z) { |
| 188 |
case 0: |
| 189 |
readPrintInfo(entry); |
| 190 |
break; |
| 191 |
case 1: |
| 192 |
m_graphParser->readPatterns(entry); |
| 193 |
break; |
| 194 |
case 2: |
| 195 |
readWPSN(entry); |
| 196 |
break; |
| 197 |
case 3: // only in v2 |
| 198 |
m_graphParser->readPalettes(entry); |
| 199 |
break; |
| 200 |
case 4: // only in v2? |
| 201 |
readARRs(entry); |
| 202 |
break; |
| 203 |
case 5: // only in v2? |
| 204 |
readDaHS(entry); |
| 205 |
break; |
| 206 |
case 6: // only in v2? |
| 207 |
readGrDS(entry); |
| 208 |
break; |
| 209 |
case 7: // only in v2? |
| 210 |
readNxEd(entry); |
| 211 |
break; |
| 212 |
default: |
| 213 |
break; |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
//////////////////////////////////////////////////////////// |
| 222 |
// |
| 223 |
// Low level |
| 224 |
// |
| 225 |
//////////////////////////////////////////////////////////// |
| 226 |
|
| 227 |
//////////////////////////////////////////////////////////// |
| 228 |
// read the windows position blocks |
| 229 |
//////////////////////////////////////////////////////////// |
| 230 |
bool GreatWksDocument::readWPSN(MWAWEntry const &entry) |
| 231 |
{ |
| 232 |
if (!entry.valid() || (entry.length()%24) != 2) { |
| 233 |
MWAW_DEBUG_MSG(("GreatWksDocument::readWPSN: the entry is bad\n")); |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
long pos = entry.begin(); |
| 238 |
MWAWInputStreamPtr input = rsrcInput(); |
| 239 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 240 |
libmwaw::DebugStream f; |
| 241 |
entry.setParsed(true); |
| 242 |
|
| 243 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 244 |
f << "Entries(Windows):"; |
| 245 |
auto N=static_cast<int>(input->readLong(2)); |
| 246 |
f << "N=" << N << ","; |
| 247 |
if (2+24*N!=int(entry.length())) { |
| 248 |
f << "###"; |
| 249 |
MWAW_DEBUG_MSG(("GreatWksDocument::readWPSN: the number of entries seems bad\n")); |
| 250 |
ascFile.addPos(pos-4); |
| 251 |
ascFile.addNote(f.str().c_str()); |
| 252 |
return true; |
| 253 |
} |
| 254 |
ascFile.addPos(pos-4); |
| 255 |
ascFile.addNote(f.str().c_str()); |
| 256 |
for (int i=0; i < N; ++i) { |
| 257 |
pos = input->tell(); |
| 258 |
f.str(""); |
| 259 |
f << "Windows-" << i << ":"; |
| 260 |
int width[2]; |
| 261 |
for (auto &w : width) w=static_cast<int>(input->readLong(2)); |
| 262 |
f << "w=" << width[1] << "x" << width[0] << ","; |
| 263 |
int LT[2]; |
| 264 |
for (auto &orig : LT) orig=static_cast<int>(input->readLong(2)); |
| 265 |
f << "LT=" << LT[1] << "x" << LT[0] << ","; |
| 266 |
for (int st=0; st < 2; ++st) { |
| 267 |
int dim[4]; |
| 268 |
for (auto &d : dim) d=static_cast<int>(input->readLong(2)); |
| 269 |
if (dim[0]!=LT[0] || dim[1]!=LT[1] || dim[2]!=LT[0]+width[0]) |
| 270 |
f << "dim" << st << "=" << dim[1] << "x" << dim[0] << "<->" |
| 271 |
<< dim[3] << "x" << dim[2] << ","; |
| 272 |
} |
| 273 |
input->seek(pos+24, librevenge::RVNG_SEEK_SET); |
| 274 |
ascFile.addPos(pos); |
| 275 |
ascFile.addNote(f.str().c_str()); |
| 276 |
} |
| 277 |
return true; |
| 278 |
} |
| 279 |
|
| 280 |
//////////////////////////////////////////////////////////// |
| 281 |
// read the print info |
| 282 |
//////////////////////////////////////////////////////////// |
| 283 |
bool GreatWksDocument::readPrintInfo(MWAWEntry const &entry) |
| 284 |
{ |
| 285 |
if (!entry.valid() || entry.length() != 120) { |
| 286 |
MWAW_DEBUG_MSG(("GreatWksDocument::readPrintInfo: the entry is bad\n")); |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
long pos = entry.begin(); |
| 291 |
MWAWInputStreamPtr input = rsrcInput(); |
| 292 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 293 |
libmwaw::DebugStream f; |
| 294 |
|
| 295 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 296 |
libmwaw::PrinterInfo info; |
| 297 |
if (!info.read(input)) return false; |
| 298 |
f << "Entries(PrintInfo):"<< info; |
| 299 |
entry.setParsed(true); |
| 300 |
|
| 301 |
MWAWVec2i paperSize = info.paper().size(); |
| 302 |
MWAWVec2i pageSize = info.page().size(); |
| 303 |
if (pageSize.x() <= 0 || pageSize.y() <= 0 || |
| 304 |
paperSize.x() <= 0 || paperSize.y() <= 0) return false; |
| 305 |
|
| 306 |
// define margin from print info |
| 307 |
MWAWVec2i lTopMargin= -1 * info.paper().pos(0); |
| 308 |
MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1); |
| 309 |
|
| 310 |
// move margin left | top |
| 311 |
int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0; |
| 312 |
int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0; |
| 313 |
lTopMargin -= MWAWVec2i(decalX, decalY); |
| 314 |
rBotMargin += MWAWVec2i(decalX, decalY); |
| 315 |
|
| 316 |
// decrease right | bottom |
| 317 |
int rightMarg = rBotMargin.x() -10; |
| 318 |
if (rightMarg < 0) rightMarg=0; |
| 319 |
int botMarg = rBotMargin.y() -50; |
| 320 |
if (botMarg < 0) botMarg=0; |
| 321 |
|
| 322 |
m_parser->getPageSpan().setMarginTop(lTopMargin.y()/72.0); |
| 323 |
m_parser->getPageSpan().setMarginBottom(botMarg/72.0); |
| 324 |
m_parser->getPageSpan().setMarginLeft(lTopMargin.x()/72.0); |
| 325 |
m_parser->getPageSpan().setMarginRight(rightMarg/72.0); |
| 326 |
m_parser->getPageSpan().setFormLength(paperSize.y()/72.); |
| 327 |
m_parser->getPageSpan().setFormWidth(paperSize.x()/72.); |
| 328 |
|
| 329 |
ascFile.addPos(pos-4); |
| 330 |
ascFile.addNote(f.str().c_str()); |
| 331 |
return true; |
| 332 |
} |
| 333 |
|
| 334 |
//////////////////////////////////////////////////////////// |
| 335 |
// read some unknown zone in rsrc fork |
| 336 |
//////////////////////////////////////////////////////////// |
| 337 |
bool GreatWksDocument::readARRs(MWAWEntry const &entry) |
| 338 |
{ |
| 339 |
if (!entry.valid() || (entry.length()%32)) { |
| 340 |
MWAW_DEBUG_MSG(("GreatWksDocument::readARRs: the entry is bad\n")); |
| 341 |
return false; |
| 342 |
} |
| 343 |
|
| 344 |
long pos = entry.begin(); |
| 345 |
MWAWInputStreamPtr input = rsrcInput(); |
| 346 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 347 |
libmwaw::DebugStream f; |
| 348 |
entry.setParsed(true); |
| 349 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 350 |
ascFile.addPos(pos-4); |
| 351 |
ascFile.addNote("Entries(ARRs)"); |
| 352 |
auto N=int(entry.length()/32); |
| 353 |
for (int i=0; i < N; ++i) { |
| 354 |
pos = input->tell(); |
| 355 |
f.str(""); |
| 356 |
f << "ARRs-" << i << ":"; |
| 357 |
input->seek(pos+32, librevenge::RVNG_SEEK_SET); |
| 358 |
ascFile.addPos(pos); |
| 359 |
ascFile.addNote(f.str().c_str()); |
| 360 |
} |
| 361 |
return true; |
| 362 |
} |
| 363 |
|
| 364 |
bool GreatWksDocument::readDaHS(MWAWEntry const &entry) |
| 365 |
{ |
| 366 |
if (!entry.valid() || entry.length() < 44 || (entry.length()%12) != 8) { |
| 367 |
MWAW_DEBUG_MSG(("GreatWksDocument::readDaHS: the entry is bad\n")); |
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
long pos = entry.begin(); |
| 372 |
MWAWInputStreamPtr input = rsrcInput(); |
| 373 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 374 |
libmwaw::DebugStream f; |
| 375 |
entry.setParsed(true); |
| 376 |
|
| 377 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 378 |
f << "Entries(DaHS):"; |
| 379 |
auto val=static_cast<int>(input->readLong(2)); |
| 380 |
if (val!=2) |
| 381 |
f << "#f0=" << val << ","; |
| 382 |
val=static_cast<int>(input->readLong(2)); |
| 383 |
if (val!=9) |
| 384 |
f << "#f1=" << val << ","; |
| 385 |
ascFile.addDelimiter(input->tell(), '|'); |
| 386 |
ascFile.addPos(pos-4); |
| 387 |
ascFile.addNote(f.str().c_str()); |
| 388 |
|
| 389 |
pos=entry.begin()+44; |
| 390 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 391 |
int N=int((entry.length()-44))/12; |
| 392 |
|
| 393 |
for (int i=0; i < N; ++i) { |
| 394 |
pos = input->tell(); |
| 395 |
f.str(""); |
| 396 |
f << "DaHS-" << i << ":"; |
| 397 |
input->seek(pos+12, librevenge::RVNG_SEEK_SET); |
| 398 |
ascFile.addPos(pos); |
| 399 |
ascFile.addNote(f.str().c_str()); |
| 400 |
} |
| 401 |
|
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
bool GreatWksDocument::readGrDS(MWAWEntry const &entry) |
| 406 |
{ |
| 407 |
if (!entry.valid() || (entry.length()%16)) { |
| 408 |
MWAW_DEBUG_MSG(("GreatWksDocument::readGrDS: the entry is bad\n")); |
| 409 |
return false; |
| 410 |
} |
| 411 |
|
| 412 |
long pos = entry.begin(); |
| 413 |
MWAWInputStreamPtr input = rsrcInput(); |
| 414 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 415 |
libmwaw::DebugStream f; |
| 416 |
entry.setParsed(true); |
| 417 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 418 |
ascFile.addPos(pos-4); |
| 419 |
ascFile.addNote("Entries(GrDS)"); |
| 420 |
auto N=int(entry.length()/16); |
| 421 |
for (int i=0; i < N; ++i) { |
| 422 |
pos = input->tell(); |
| 423 |
f.str(""); |
| 424 |
f << "GrDS-" << i << ":"; |
| 425 |
auto val=static_cast<int>(input->readLong(2)); // 1,2,3 |
| 426 |
f << "unkn=" << val << ","; |
| 427 |
for (int st=0; st < 2; ++st) { |
| 428 |
unsigned char col[3]; |
| 429 |
for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8); |
| 430 |
MWAWColor color(col[0], col[1], col[2]); |
| 431 |
if (st==0) { |
| 432 |
if (!color.isWhite()) f << "backColor=" << color << ","; |
| 433 |
} |
| 434 |
else if (!color.isBlack()) f << "frontColor=" << color << ","; |
| 435 |
} |
| 436 |
val = static_cast<int>(input->readULong(2)); |
| 437 |
if (val) f << "ptr?=" << std::hex << val << std::dec << ","; |
| 438 |
input->seek(pos+16, librevenge::RVNG_SEEK_SET); |
| 439 |
ascFile.addPos(i==0?pos-4:pos); |
| 440 |
ascFile.addNote(f.str().c_str()); |
| 441 |
} |
| 442 |
return true; |
| 443 |
} |
| 444 |
|
| 445 |
bool GreatWksDocument::readNxEd(MWAWEntry const &entry) |
| 446 |
{ |
| 447 |
if (!entry.valid() || entry.length()<4) { |
| 448 |
MWAW_DEBUG_MSG(("GreatWksDocument::readNxEd: the entry is bad\n")); |
| 449 |
return false; |
| 450 |
} |
| 451 |
|
| 452 |
if (entry.length()!=4) { |
| 453 |
MWAW_DEBUG_MSG(("GreatWksDocument::readNxEd: OHHHH the entry is filled\n")); |
| 454 |
} |
| 455 |
|
| 456 |
long pos = entry.begin(); |
| 457 |
MWAWInputStreamPtr input = rsrcInput(); |
| 458 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 459 |
libmwaw::DebugStream f; |
| 460 |
entry.setParsed(true); |
| 461 |
|
| 462 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 463 |
f << "Entries(NxED):"; |
| 464 |
for (int i = 0; i < 2; ++i) { // always 0 |
| 465 |
auto val=static_cast<int>(input->readLong(2)); |
| 466 |
if (val) |
| 467 |
f << "f" << i << "=" << val << ","; |
| 468 |
} |
| 469 |
ascFile.addPos(pos-4); |
| 470 |
ascFile.addNote("Entries(NxED):"); |
| 471 |
return true; |
| 472 |
} |
| 473 |
|
| 474 |
//////////////////////////////////////////////////////////// |
| 475 |
// read the header |
| 476 |
//////////////////////////////////////////////////////////// |
| 477 |
bool GreatWksDocument::checkHeader(MWAWHeader *header, bool strict) |
| 478 |
{ |
| 479 |
*m_state = GreatWksDocumentInternal::State(); |
| 480 |
MWAWInputStreamPtr input = m_parserState->m_input; |
| 481 |
if (!input || !input->hasDataFork() || !input->checkPosition(0x4c)) |
| 482 |
return false; |
| 483 |
|
| 484 |
libmwaw::DebugFile &ascFile=m_parserState->m_asciiFile; |
| 485 |
libmwaw::DebugStream f; |
| 486 |
f << "FileHeader:"; |
| 487 |
|
| 488 |
input->seek(0,librevenge::RVNG_SEEK_SET); |
| 489 |
auto vers=static_cast<int>(input->readLong(1)); |
| 490 |
if (vers < 1 || vers > 2) |
| 491 |
return false; |
| 492 |
if (input->readLong(1)) |
| 493 |
return false; |
| 494 |
m_parserState->m_version=vers; |
| 495 |
std::string type(""); |
| 496 |
for (int i=0; i < 4; ++i) |
| 497 |
type+=char(input->readLong(1)); |
| 498 |
if (type=="ZOBJ") { |
| 499 |
m_parserState->m_kind=MWAWDocument::MWAW_K_DRAW; |
| 500 |
MWAW_DEBUG_MSG(("GreatWksDocument::checkHeader: find a draw file\n")); |
| 501 |
} |
| 502 |
else if (type=="ZCAL") { |
| 503 |
m_parserState->m_kind=MWAWDocument::MWAW_K_SPREADSHEET; |
| 504 |
MWAW_DEBUG_MSG(("GreatWksDocument::checkHeader: find a spreadsheet file\n")); |
| 505 |
} |
| 506 |
else if (type=="ZDBS") { |
| 507 |
m_parserState->m_kind=MWAWDocument::MWAW_K_DATABASE; |
| 508 |
MWAW_DEBUG_MSG(("GreatWksDocument::checkHeader: find a database file\n")); |
| 509 |
} |
| 510 |
else if (type!="ZWRT") |
| 511 |
return false; |
| 512 |
|
| 513 |
// extra check for database must be done in GreatWksDBParser |
| 514 |
if (strict && m_parserState->m_kind!=MWAWDocument::MWAW_K_DATABASE) { |
| 515 |
// check that the fonts table is in expected position |
| 516 |
long fontPos=-1; |
| 517 |
if (m_parserState->m_kind==MWAWDocument::MWAW_K_DRAW) |
| 518 |
fontPos = 0x4a; |
| 519 |
else if (m_parserState->m_kind==MWAWDocument::MWAW_K_SPREADSHEET) |
| 520 |
fontPos=18; |
| 521 |
else if (m_parserState->m_kind==MWAWDocument::MWAW_K_TEXT) |
| 522 |
fontPos = vers==1 ? 0x302 : 0x308; |
| 523 |
if (fontPos>0 && (input->seek(fontPos, librevenge::RVNG_SEEK_SET) || !m_textParser->readFontNames())) { |
| 524 |
MWAW_DEBUG_MSG(("GreatWksDocument::checkHeader: can not find fonts table\n")); |
| 525 |
return false; |
| 526 |
} |
| 527 |
} |
| 528 |
ascFile.addPos(0); |
| 529 |
ascFile.addNote(f.str().c_str()); |
| 530 |
ascFile.addPos(6); |
| 531 |
ascFile.addNote("FileHeader-II:"); |
| 532 |
|
| 533 |
if (header) |
| 534 |
header->reset(MWAWDocument::MWAW_T_GREATWORKS, vers,m_parserState->m_kind); |
| 535 |
return true; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
//////////////////////////////////////////////////////////// |
| 540 |
// read a formula |
| 541 |
//////////////////////////////////////////////////////////// |
| 542 |
bool GreatWksDocument::readCellInFormula(MWAWVec2i const &pos, MWAWCellContent::FormulaInstruction &instr) |
| 543 |
{ |
| 544 |
MWAWInputStreamPtr input=m_parserState->m_input; |
| 545 |
instr=MWAWCellContent::FormulaInstruction(); |
| 546 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Cell; |
| 547 |
bool absolute[2] = { true, true}; |
| 548 |
int cPos[2]; |
| 549 |
for (int i=0; i<2; ++i) { |
| 550 |
auto val = static_cast<int>(input->readULong(2)); |
| 551 |
if (val & 0x8000) { |
| 552 |
absolute[i]=false; |
| 553 |
if (val&0x4000) |
| 554 |
cPos[i] = pos[i]+(val-0xFFFF); |
| 555 |
else |
| 556 |
cPos[i] = pos[i]+(val-0x7FFF); |
| 557 |
} |
| 558 |
else |
| 559 |
cPos[i]=val; |
| 560 |
} |
| 561 |
|
| 562 |
if (cPos[0] < 1 || cPos[1] < 1) { |
| 563 |
MWAW_DEBUG_MSG(("GreatWksDocument::readCellInFormula: can not read cell position\n")); |
| 564 |
return false; |
| 565 |
} |
| 566 |
instr.m_position[0]=MWAWVec2i(cPos[0]-1,cPos[1]-1); |
| 567 |
instr.m_positionRelative[0]=MWAWVec2b(!absolute[0],!absolute[1]); |
| 568 |
return true; |
| 569 |
} |
| 570 |
|
| 571 |
bool GreatWksDocument::readString(long endPos, std::string &res) |
| 572 |
{ |
| 573 |
res=""; |
| 574 |
MWAWInputStreamPtr input=m_parserState->m_input; |
| 575 |
long pos=input->tell(); |
| 576 |
auto fSz=static_cast<int>(input->readULong(1)); |
| 577 |
if (pos+1+fSz>endPos || !input->checkPosition(endPos)) { |
| 578 |
MWAW_DEBUG_MSG(("GreatWksDocument::readString: can not read string size\n")); |
| 579 |
return false; |
| 580 |
} |
| 581 |
for (int i=0; i<fSz; ++i) |
| 582 |
res += char(input->readULong(1)); |
| 583 |
return true; |
| 584 |
} |
| 585 |
|
| 586 |
bool GreatWksDocument::readNumber(long endPos, double &res, bool &isNan) |
| 587 |
{ |
| 588 |
MWAWInputStreamPtr input=m_parserState->m_input; |
| 589 |
long pos=input->tell(); |
| 590 |
if (pos+10>endPos) { |
| 591 |
MWAW_DEBUG_MSG(("GreatWksDocument::readNumber: can not read a number\n")); |
| 592 |
return false; |
| 593 |
} |
| 594 |
return input->readDouble10(res, isNan); |
| 595 |
} |
| 596 |
|
| 597 |
namespace GreatWksDocumentInternal |
| 598 |
{ |
| 599 |
struct Functions { |
| 600 |
char const *m_name; |
| 601 |
int m_arity; |
| 602 |
}; |
| 603 |
|
| 604 |
static Functions const s_listFunctions[] = { |
| 605 |
{ "=", 1}, {"", -1} /*UNKN*/, {"", 0}/*SPEC:long*/, {"", -1} /*UNKN*/, |
| 606 |
{ "", -2} /*UNKN*/,{ "", 0} /*SPEC:double*/,{ "", -2} /*UNKN*/,{ "", 0} /*SPEC:text*/, |
| 607 |
{ "", 0} /*SPEC:short*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 608 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 609 |
|
| 610 |
{ "", 0} /*SPEC:cell*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 611 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 612 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 613 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 614 |
|
| 615 |
{ ":", 2} /*SPEC:concatenate cell*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 616 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 617 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 618 |
{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/,{ "", -2} /*UNKN*/, |
| 619 |
|
| 620 |
{ "(", 1}, { "-", 1}, { "+", 1} /*checkme*/,{ "^", 2}, |
| 621 |
{ "*", 2}, { "/", 2}, { "+", 2}, { "-", 2}, |
| 622 |
{ "", -2} /*UNKN*/,{ "=", 2}, { "<", 2}, { "<=", 2}, |
| 623 |
{ ">", 2}, { ">=", 2}, { "<>", 2}, { "", -2} /*UNKN*/, |
| 624 |
|
| 625 |
}; |
| 626 |
} |
| 627 |
|
| 628 |
bool GreatWksDocument::readFormula(MWAWVec2i const &cPos, long endPos, std::vector<MWAWCellContent::FormulaInstruction> &formula, std::string &error) |
| 629 |
{ |
| 630 |
MWAWInputStreamPtr input=m_parserState->m_input; |
| 631 |
libmwaw::DebugStream f; |
| 632 |
std::vector<std::vector<MWAWCellContent::FormulaInstruction> > stack; |
| 633 |
bool ok=true; |
| 634 |
while (!input->isEnd()) { |
| 635 |
long pos=input->tell(); |
| 636 |
if (pos >= endPos) |
| 637 |
break; |
| 638 |
int arity=0, val, type=static_cast<int>(input->readULong(1)); |
| 639 |
MWAWCellContent::FormulaInstruction instr; |
| 640 |
switch (type) { |
| 641 |
case 2: |
| 642 |
if (pos+1+2 > endPos) { |
| 643 |
ok = false; |
| 644 |
break; |
| 645 |
} |
| 646 |
val = static_cast<int>(input->readLong(2)); |
| 647 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Long; |
| 648 |
instr.m_longValue=val; |
| 649 |
break; |
| 650 |
case 5: { |
| 651 |
double value; |
| 652 |
bool isNan; |
| 653 |
ok=readNumber(endPos, value, isNan); |
| 654 |
if (!ok) break; |
| 655 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Double; |
| 656 |
instr.m_doubleValue=value; |
| 657 |
break; |
| 658 |
} |
| 659 |
case 7: { |
| 660 |
std::string text; |
| 661 |
ok=readString(endPos, text); |
| 662 |
if (!ok) break; |
| 663 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Text; |
| 664 |
instr.m_content=text; |
| 665 |
break; |
| 666 |
} |
| 667 |
case 8: |
| 668 |
if (pos+1+1 > endPos) { |
| 669 |
ok = false; |
| 670 |
break; |
| 671 |
} |
| 672 |
val = static_cast<int>(input->readLong(1)); |
| 673 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Long; |
| 674 |
instr.m_longValue=val; |
| 675 |
break; |
| 676 |
case 0x10: { |
| 677 |
ok=readCellInFormula(cPos,instr); |
| 678 |
if (!ok) break; |
| 679 |
f << instr << ","; |
| 680 |
break; |
| 681 |
} |
| 682 |
case 0x41: { |
| 683 |
// field in a database formula |
| 684 |
if (pos+1+1 > endPos || m_parserState->m_kind!=MWAWDocument::MWAW_K_DATABASE) { |
| 685 |
ok = false; |
| 686 |
break; |
| 687 |
} |
| 688 |
std::string text; |
| 689 |
ok=readString(endPos, text); |
| 690 |
if (!ok) break; |
| 691 |
// we have not sufficient information to fill the position, let GreatWksDBParser fills it |
| 692 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Cell; |
| 693 |
instr.m_positionRelative[0]=MWAWVec2b(true,true); |
| 694 |
instr.m_content=text; |
| 695 |
break; |
| 696 |
} |
| 697 |
case 0x40: { |
| 698 |
if (pos+1+1 > endPos) { |
| 699 |
ok = false; |
| 700 |
break; |
| 701 |
} |
| 702 |
val = static_cast<int>(input->readULong(1)); |
| 703 |
arity= static_cast<int>(input->readULong(1)); |
| 704 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Function; |
| 705 |
|
| 706 |
static char const *s_functions[] = { |
| 707 |
"", "Abs", "Exp", "Fact", "Int", "Ln", "Log", "Log10", |
| 708 |
"Mod", "Pi", "Product", "Rand", "Round", "Sign", "Sqrt", "Trunc", |
| 709 |
|
| 710 |
"Average", "Count", "CountA", "Max", "Min", "StDev", "StDevP", "Sum", |
| 711 |
"Var", "VarP", "Acos", "Asin", "Atan", "Atan2", "Cos", "Sin", |
| 712 |
|
| 713 |
"Tan", "Degrees", "Radians", "And", "Choose", "False", "If", "IsBlank", |
| 714 |
"IsErr", "IsError", "IsLogical", "IsNa", "IsNonText", "IsNum", "IsRef", "IsText", |
| 715 |
|
| 716 |
"Not", "Or", "True", "Char", "Clean", "Code", "Dollar", "Exact", |
| 717 |
"Find", "Fixed", "Left", "Len", "Lower", "Mid", "Proper"/*checkme: first majuscule*/, "Replace", |
| 718 |
|
| 719 |
"Rept", "Right", "Search", "Substitute", "Trim", "Upper", "DDB", "FV", |
| 720 |
"IPMT", "IRR", "MIRR", "NPER", "NPV", "PMT", "PPMT", "PV", |
| 721 |
|
| 722 |
"Rate", "SLN", "SYD", "Annuity", "Compound", "Date", "Day", "Hour", |
| 723 |
"Minute", "Month", "Now", "Second", "Time", "Weekday", "Year", "HLookup", |
| 724 |
|
| 725 |
"Index", "Lookup", "Match", "N", "Na", "T", "Type", "VLookup", |
| 726 |
"", "", "", "", "", "", "", "", |
| 727 |
|
| 728 |
}; |
| 729 |
std::string functName(""); |
| 730 |
if (val < 0x70) functName=s_functions[val]; |
| 731 |
if (!functName.empty()) |
| 732 |
instr.m_content=functName; |
| 733 |
else { |
| 734 |
std::stringstream s; |
| 735 |
s << "Funct" << std::hex << val << std::dec << "#"; |
| 736 |
instr.m_content=s.str(); |
| 737 |
} |
| 738 |
break; |
| 739 |
} |
| 740 |
default: |
| 741 |
if (type >= 0x40 || GreatWksDocumentInternal::s_listFunctions[type].m_arity == -2) { |
| 742 |
f.str(""); |
| 743 |
f << "##Funct" << std::hex << type << std::dec; |
| 744 |
ok = false; |
| 745 |
break; |
| 746 |
} |
| 747 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Function; |
| 748 |
instr.m_content=GreatWksDocumentInternal::s_listFunctions[type].m_name; |
| 749 |
ok=!instr.m_content.empty(); |
| 750 |
arity = GreatWksDocumentInternal::s_listFunctions[type].m_arity; |
| 751 |
if (arity == -1) arity = static_cast<int>(input->readLong(1)); |
| 752 |
if (arity<0) ok=false; |
| 753 |
break; |
| 754 |
} |
| 755 |
if (!ok) break; |
| 756 |
std::vector<MWAWCellContent::FormulaInstruction> child; |
| 757 |
if (instr.m_type!=MWAWCellContent::FormulaInstruction::F_Function) { |
| 758 |
child.push_back(instr); |
| 759 |
stack.push_back(child); |
| 760 |
continue; |
| 761 |
} |
| 762 |
size_t numElt = stack.size(); |
| 763 |
if (static_cast<int>(numElt) < arity) { |
| 764 |
f.str(""); |
| 765 |
f << instr.m_content << "[##" << arity << "]"; |
| 766 |
ok = false; |
| 767 |
break; |
| 768 |
} |
| 769 |
if ((instr.m_content[0] >= 'A' && instr.m_content[0] <= 'Z') || instr.m_content[0] == '(') { |
| 770 |
if (instr.m_content[0] != '(') |
| 771 |
child.push_back(instr); |
| 772 |
|
| 773 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator; |
| 774 |
instr.m_content="("; |
| 775 |
child.push_back(instr); |
| 776 |
for (int i = 0; i < arity; i++) { |
| 777 |
if (i) { |
| 778 |
instr.m_content=";"; |
| 779 |
child.push_back(instr); |
| 780 |
} |
| 781 |
auto const &node= stack[size_t(static_cast<int>(numElt)-arity+i)]; |
| 782 |
child.insert(child.end(), node.begin(), node.end()); |
| 783 |
} |
| 784 |
instr.m_content=")"; |
| 785 |
child.push_back(instr); |
| 786 |
|
| 787 |
stack.resize(size_t(static_cast<int>(numElt)-arity+1)); |
| 788 |
stack[size_t(static_cast<int>(numElt)-arity)] = child; |
| 789 |
continue; |
| 790 |
} |
| 791 |
if (arity==1) { |
| 792 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator; |
| 793 |
stack[numElt-1].insert(stack[numElt-1].begin(), instr); |
| 794 |
if (type==0 && pos+1==endPos) |
| 795 |
break; |
| 796 |
continue; |
| 797 |
} |
| 798 |
if (arity==2 && instr.m_content==":") { |
| 799 |
if (stack[numElt-2].size()!=1 || stack[numElt-2][0].m_type!=MWAWCellContent::FormulaInstruction::F_Cell || |
| 800 |
stack[numElt-1].size()!=1 || stack[numElt-1][0].m_type!=MWAWCellContent::FormulaInstruction::F_Cell) { |
| 801 |
f << "### unexpected type of concatenate argument"; |
| 802 |
ok=false; |
| 803 |
break; |
| 804 |
} |
| 805 |
instr=stack[numElt-2][0]; |
| 806 |
auto instr2=stack[numElt-1][0]; |
| 807 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_CellList; |
| 808 |
instr.m_position[1]=instr2.m_position[0]; |
| 809 |
instr.m_positionRelative[1]=instr2.m_positionRelative[0]; |
| 810 |
stack[numElt-2][0]=instr; |
| 811 |
stack.resize(numElt-1); |
| 812 |
continue; |
| 813 |
} |
| 814 |
if (arity==2) { |
| 815 |
instr.m_type=MWAWCellContent::FormulaInstruction::F_Operator; |
| 816 |
stack[numElt-2].push_back(instr); |
| 817 |
stack[numElt-2].insert(stack[numElt-2].end(), stack[numElt-1].begin(), stack[numElt-1].end()); |
| 818 |
stack.resize(numElt-1); |
| 819 |
continue; |
| 820 |
} |
| 821 |
ok=false; |
| 822 |
f << "### unexpected arity"; |
| 823 |
break; |
| 824 |
} |
| 825 |
|
| 826 |
if (!ok) ; |
| 827 |
else if (stack.size()==1 && stack[0].size()>1 && stack[0][0].m_content=="=") { |
| 828 |
formula.insert(formula.begin(),stack[0].begin()+1,stack[0].end()); |
| 829 |
return true; |
| 830 |
} |
| 831 |
else |
| 832 |
f << "###stack problem"; |
| 833 |
|
| 834 |
m_parserState->m_asciiFile.addDelimiter(input->tell(),'#'); |
| 835 |
static bool first = true; |
| 836 |
if (first) { |
| 837 |
MWAW_DEBUG_MSG(("GreatWksDocument::readFormula: I can not read some formula\n")); |
| 838 |
first = false; |
| 839 |
} |
| 840 |
|
| 841 |
error = f.str(); |
| 842 |
f.str(""); |
| 843 |
for (auto const &i : stack) { |
| 844 |
for (auto const &j : i) |
| 845 |
f << j << ","; |
| 846 |
} |
| 847 |
f << error; |
| 848 |
error = f.str(); |
| 849 |
return false; |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |