| 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 "MWAWFontConverter.hxx" |
| 44 |
#include "MWAWHeader.hxx" |
| 45 |
#include "MWAWParagraph.hxx" |
| 46 |
#include "MWAWPictData.hxx" |
| 47 |
#include "MWAWPosition.hxx" |
| 48 |
#include "MWAWPrinter.hxx" |
| 49 |
#include "MWAWRSRCParser.hxx" |
| 50 |
#include "MWAWSubDocument.hxx" |
| 51 |
|
| 52 |
#include "JazzWriterParser.hxx" |
| 53 |
|
| 54 |
/** Internal: the structures of a JazzWriterParser */ |
| 55 |
namespace JazzWriterParserInternal |
| 56 |
{ |
| 57 |
|
| 58 |
//////////////////////////////////////// |
| 59 |
//! Internal: the structure used to store a paragraph/section |
| 60 |
struct Paragraph { |
| 61 |
//! constructor |
| 62 |
Paragraph() |
| 63 |
: m_paragraph() |
| 64 |
, m_dimension(0,0) |
| 65 |
, m_nextParagraphId(0) |
| 66 |
, m_plcId(0) |
| 67 |
{ |
| 68 |
} |
| 69 |
//! the paragraph |
| 70 |
MWAWParagraph m_paragraph; |
| 71 |
//! the dimension |
| 72 |
MWAWVec2i m_dimension; |
| 73 |
//! the next paragraph id |
| 74 |
unsigned m_nextParagraphId; |
| 75 |
//! the plc id |
| 76 |
unsigned m_plcId; |
| 77 |
}; |
| 78 |
|
| 79 |
//////////////////////////////////////// |
| 80 |
//! Internal: the structure used to store a zone |
| 81 |
struct Zone { |
| 82 |
//! constructor |
| 83 |
Zone() |
| 84 |
: m_paragraphId(0) |
| 85 |
, m_entry() |
| 86 |
{ |
| 87 |
for (auto &id : m_hfIds) id=0; |
| 88 |
} |
| 89 |
//! the header/footer id |
| 90 |
unsigned m_hfIds[2]; |
| 91 |
//! the paragraph id |
| 92 |
unsigned m_paragraphId; |
| 93 |
//! the text position in the data fork |
| 94 |
MWAWEntry m_entry; |
| 95 |
}; |
| 96 |
|
| 97 |
//////////////////////////////////////// |
| 98 |
//! Internal: the state of a JazzWriterParser |
| 99 |
struct State { |
| 100 |
//! constructor |
| 101 |
State() |
| 102 |
: m_idToZones() |
| 103 |
, m_idToParagraphs() |
| 104 |
{ |
| 105 |
} |
| 106 |
|
| 107 |
/// map WDOC id to zones |
| 108 |
std::map<unsigned, Zone> m_idToZones; |
| 109 |
/// map WPPD id to zones |
| 110 |
std::map<unsigned, Paragraph> m_idToParagraphs; |
| 111 |
}; |
| 112 |
|
| 113 |
//////////////////////////////////////// |
| 114 |
//! Internal: the subdocument of a JazzWriterParser |
| 115 |
class SubDocument final : public MWAWSubDocument |
| 116 |
{ |
| 117 |
public: |
| 118 |
SubDocument(JazzWriterParser &pars, MWAWInputStreamPtr const &input, MWAWInputStreamPtr const &rsrcInput, unsigned id) |
| 119 |
: MWAWSubDocument(&pars, input, MWAWEntry()) |
| 120 |
, m_rsrcInput(rsrcInput) |
| 121 |
, m_zId(id) |
| 122 |
{ |
| 123 |
} |
| 124 |
|
| 125 |
//! destructor |
| 126 |
~SubDocument() final {} |
| 127 |
|
| 128 |
//! operator!= |
| 129 |
bool operator!=(MWAWSubDocument const &doc) const final; |
| 130 |
|
| 131 |
//! the parser function |
| 132 |
void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final; |
| 133 |
|
| 134 |
protected: |
| 135 |
//! the resource fork input |
| 136 |
MWAWInputStreamPtr m_rsrcInput; |
| 137 |
//! the zone id |
| 138 |
unsigned m_zId; |
| 139 |
}; |
| 140 |
|
| 141 |
void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/) |
| 142 |
{ |
| 143 |
if (!listener.get()) { |
| 144 |
MWAW_DEBUG_MSG(("JazzWriterParserInternal::SubDocument::parse: no listener\n")); |
| 145 |
return; |
| 146 |
} |
| 147 |
auto *parser=dynamic_cast<JazzWriterParser *>(m_parser); |
| 148 |
if (!parser) { |
| 149 |
MWAW_DEBUG_MSG(("JazzWriterParserInternal::SubDocument::parse: no parser\n")); |
| 150 |
return; |
| 151 |
} |
| 152 |
if (!m_input || !m_rsrcInput) { |
| 153 |
MWAW_DEBUG_MSG(("JazzWriterParserInternal::SubDocument::parse: no input\n")); |
| 154 |
return; |
| 155 |
} |
| 156 |
long pos = m_input->tell(); |
| 157 |
long rPos = m_rsrcInput->tell(); |
| 158 |
parser->sendZone(m_zId); |
| 159 |
m_input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 160 |
m_rsrcInput->seek(rPos, librevenge::RVNG_SEEK_SET); |
| 161 |
} |
| 162 |
|
| 163 |
bool SubDocument::operator!=(MWAWSubDocument const &doc) const |
| 164 |
{ |
| 165 |
if (MWAWSubDocument::operator!=(doc)) return true; |
| 166 |
auto const *sDoc = dynamic_cast<SubDocument const *>(&doc); |
| 167 |
if (!sDoc) return true; |
| 168 |
if (m_zId != sDoc->m_zId) return true; |
| 169 |
if (m_rsrcInput != sDoc->m_rsrcInput) return true; |
| 170 |
return false; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
//////////////////////////////////////////////////////////// |
| 176 |
// constructor/destructor, ... |
| 177 |
//////////////////////////////////////////////////////////// |
| 178 |
JazzWriterParser::JazzWriterParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 179 |
: MWAWTextParser(input, rsrcParser, header) |
| 180 |
, m_state(new JazzWriterParserInternal::State) |
| 181 |
{ |
| 182 |
setAsciiName("main-1"); |
| 183 |
} |
| 184 |
|
| 185 |
JazzWriterParser::~JazzWriterParser() |
| 186 |
{ |
| 187 |
} |
| 188 |
|
| 189 |
MWAWInputStreamPtr JazzWriterParser::rsrcInput() |
| 190 |
{ |
| 191 |
return getRSRCParser()->getInput(); |
| 192 |
} |
| 193 |
|
| 194 |
libmwaw::DebugFile &JazzWriterParser::rsrcAscii() |
| 195 |
{ |
| 196 |
return getRSRCParser()->ascii(); |
| 197 |
} |
| 198 |
|
| 199 |
//////////////////////////////////////////////////////////// |
| 200 |
// the parser |
| 201 |
//////////////////////////////////////////////////////////// |
| 202 |
void JazzWriterParser::parse(librevenge::RVNGTextInterface *docInterface) |
| 203 |
{ |
| 204 |
if (!getInput().get() || !getRSRCParser() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 205 |
bool ok = false; |
| 206 |
try { |
| 207 |
// create the asciiFile |
| 208 |
ascii().setStream(getInput()); |
| 209 |
ascii().open(asciiName()); |
| 210 |
checkHeader(nullptr); |
| 211 |
ok = createZones(); |
| 212 |
if (ok) { |
| 213 |
createDocument(docInterface); |
| 214 |
sendZone(257); |
| 215 |
if (!getInput()->isEnd()) { |
| 216 |
MWAW_DEBUG_MSG(("JazzWriterParser::parse: find some unsent characters\n")); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
catch (...) { |
| 221 |
MWAW_DEBUG_MSG(("JazzWriterParser::parse: exception catched when parsing\n")); |
| 222 |
ok = false; |
| 223 |
} |
| 224 |
|
| 225 |
resetTextListener(); |
| 226 |
if (!ok) throw(libmwaw::ParseException()); |
| 227 |
} |
| 228 |
|
| 229 |
//////////////////////////////////////////////////////////// |
| 230 |
// create the document |
| 231 |
//////////////////////////////////////////////////////////// |
| 232 |
void JazzWriterParser::createDocument(librevenge::RVNGTextInterface *documentInterface) |
| 233 |
{ |
| 234 |
if (!documentInterface) return; |
| 235 |
if (getTextListener()) { |
| 236 |
MWAW_DEBUG_MSG(("JazzWriterParser::createDocument: listener already exist\n")); |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
// found the numbers of page and update the page |
| 241 |
int numPages = 1; |
| 242 |
auto input=getInput(); |
| 243 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 244 |
while (!input->isEnd()) { |
| 245 |
if (input->readULong(1)==0xc) |
| 246 |
++numPages; |
| 247 |
} |
| 248 |
|
| 249 |
// create the page list |
| 250 |
MWAWPageSpan ps(getPageSpan()); |
| 251 |
ps.setPageSpan(numPages+1); |
| 252 |
auto it=m_state->m_idToZones.find(257); |
| 253 |
if (it==m_state->m_idToZones.end()) { |
| 254 |
MWAW_DEBUG_MSG(("JazzWriterParser::createDocument: can not find the main zone\n")); |
| 255 |
throw (libmwaw::ParseException()); |
| 256 |
} |
| 257 |
|
| 258 |
for (int wh=0; wh<2; ++wh) { |
| 259 |
if (!it->second.m_hfIds[wh]) |
| 260 |
continue; |
| 261 |
MWAWHeaderFooter header(wh==0 ? MWAWHeaderFooter::HEADER : MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL); |
| 262 |
header.m_subDocument.reset(new JazzWriterParserInternal::SubDocument(*this, getInput(), rsrcInput(), it->second.m_hfIds[wh])); |
| 263 |
ps.setHeaderFooter(header); |
| 264 |
} |
| 265 |
|
| 266 |
std::vector<MWAWPageSpan> pageList(1,ps); |
| 267 |
MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface)); |
| 268 |
setTextListener(listen); |
| 269 |
listen->startDocument(); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
//////////////////////////////////////////////////////////// |
| 274 |
// |
| 275 |
// Intermediate level |
| 276 |
// |
| 277 |
//////////////////////////////////////////////////////////// |
| 278 |
bool JazzWriterParser::createZones() |
| 279 |
{ |
| 280 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 281 |
if (!rsrcParser) { |
| 282 |
MWAW_DEBUG_MSG(("JazzWriterParser::createZones: can not find the entry map\n")); |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
auto const &entryMap = rsrcParser->getEntriesMap(); |
| 287 |
MWAWInputStreamPtr input = rsrcInput(); |
| 288 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 289 |
|
| 290 |
char const *zNames[] = {"LFRF", "LLNK", "WDOC", "WPPD"}; |
| 291 |
char const *what[]= {"FileRef", "Link", "Zone", "Paragraph"}; |
| 292 |
libmwaw::DebugStream f; |
| 293 |
for (int wh=0; wh<4; ++wh) { |
| 294 |
auto it = entryMap.lower_bound(zNames[wh]); |
| 295 |
while (it != entryMap.end()) { |
| 296 |
if (it->first != zNames[wh] || !it->second.valid()) |
| 297 |
break; |
| 298 |
MWAWEntry const &entry = it++->second; |
| 299 |
if (!input->checkPosition(entry.end())) { |
| 300 |
MWAW_DEBUG_MSG(("JazzWriterParser::createZones: find bad entry\n")); |
| 301 |
continue; |
| 302 |
} |
| 303 |
entry.setParsed(true); |
| 304 |
bool ok=false, done=false; |
| 305 |
int val; |
| 306 |
f.str(""); |
| 307 |
f << "Entries(" << what[wh] << ")[" << entry.id() << "]:"; |
| 308 |
input->seek(entry.begin(), librevenge::RVNG_SEEK_SET); |
| 309 |
switch (wh) { |
| 310 |
case 0: // link name |
| 311 |
f << entry.name() << ","; // the file name |
| 312 |
ok=true; |
| 313 |
if (entry.length()!=2) { |
| 314 |
MWAW_DEBUG_MSG(("JazzWriterParser::createZones[lref]: the entry length seems bad\n")); |
| 315 |
f << "###"; |
| 316 |
break; |
| 317 |
} |
| 318 |
val=int(input->readLong(2)); |
| 319 |
if (val!=1) f << "f0=" << val << ","; |
| 320 |
break; |
| 321 |
case 1: { |
| 322 |
librevenge::RVNGString text; |
| 323 |
ok=true; |
| 324 |
if (!readString(input, text, entry.end())) { |
| 325 |
MWAW_DEBUG_MSG(("JazzWriterParser::createZones[lnk]: can not find the text\n")); |
| 326 |
f << "###"; |
| 327 |
break; |
| 328 |
} |
| 329 |
if (!text.empty()) |
| 330 |
f << text.cstr() << ","; |
| 331 |
long cPos=input->tell(); |
| 332 |
if (cPos+2>entry.end()) { |
| 333 |
MWAW_DEBUG_MSG(("JazzWriterParser::createZones[lnk]: can not find the file ref\n")); |
| 334 |
f << "###"; |
| 335 |
break; |
| 336 |
} |
| 337 |
f << "file[ref]=" << input->readULong(2) << ","; |
| 338 |
break; |
| 339 |
} |
| 340 |
case 2: |
| 341 |
done=readZone(entry); |
| 342 |
break; |
| 343 |
case 3: |
| 344 |
done=readParagraph(entry); |
| 345 |
break; |
| 346 |
default: |
| 347 |
break; |
| 348 |
} |
| 349 |
if (done) |
| 350 |
continue; |
| 351 |
if (!ok) |
| 352 |
f << "###"; |
| 353 |
ascFile.addPos(entry.begin()-4); |
| 354 |
ascFile.addNote(f.str().c_str()); |
| 355 |
} |
| 356 |
} |
| 357 |
return checkZones(); |
| 358 |
} |
| 359 |
|
| 360 |
bool JazzWriterParser::checkZones() |
| 361 |
{ |
| 362 |
auto it=m_state->m_idToZones.find(257); |
| 363 |
if (it==m_state->m_idToZones.end()) { |
| 364 |
MWAW_DEBUG_MSG(("JazzWriterParser::checkZones: can not find the main zone\n")); |
| 365 |
return false; |
| 366 |
} |
| 367 |
unsigned const zones[3]= {it->second.m_hfIds[0],it->second.m_hfIds[1],257}; |
| 368 |
std::set<unsigned> seens; |
| 369 |
long pos=0; |
| 370 |
for (auto const &id : zones) { |
| 371 |
it=m_state->m_idToZones.find(id); |
| 372 |
if (it==m_state->m_idToZones.end()) { |
| 373 |
MWAW_DEBUG_MSG(("JazzWriterParser::checkZones: can not find the %x zone\n", id)); |
| 374 |
return false; |
| 375 |
} |
| 376 |
long length=0; |
| 377 |
if (!checkParagraphs(it->second.m_paragraphId, length, seens)) |
| 378 |
return false; |
| 379 |
MWAWEntry entry; |
| 380 |
entry.setBegin(pos); |
| 381 |
entry.setLength(length); |
| 382 |
it->second.m_entry=entry; |
| 383 |
pos+=length; |
| 384 |
} |
| 385 |
if (!getInput() || getInput()->size()<pos) { |
| 386 |
MWAW_DEBUG_MSG(("JazzWriterParser::createZones: the data fork seems too short\n")); |
| 387 |
return false; |
| 388 |
} |
| 389 |
getInput()->seek(0, librevenge::RVNG_SEEK_SET); |
| 390 |
|
| 391 |
return true; |
| 392 |
} |
| 393 |
|
| 394 |
bool JazzWriterParser::checkParagraphs(unsigned id, long &num, std::set<unsigned> &seens) const |
| 395 |
{ |
| 396 |
if (id==0) |
| 397 |
return true; |
| 398 |
if (seens.find(id) != seens.end()) { |
| 399 |
MWAW_DEBUG_MSG(("JazzWriterParser::checkParagraphs: paragraph %x is already seen\n", id)); |
| 400 |
return false; |
| 401 |
} |
| 402 |
seens.insert(id); |
| 403 |
auto it=m_state->m_idToParagraphs.find(id); |
| 404 |
if (it==m_state->m_idToParagraphs.end()) { |
| 405 |
MWAW_DEBUG_MSG(("JazzWriterParser::checkParagraphs: can not find paragraph %x\n", id)); |
| 406 |
return false; |
| 407 |
} |
| 408 |
long n=0; |
| 409 |
if (!const_cast<JazzWriterParser *>(this)->countCharactersInPLC(it->second.m_plcId, n)) |
| 410 |
return false; |
| 411 |
num+=n; |
| 412 |
checkParagraphs(it->second.m_nextParagraphId, num, seens); |
| 413 |
return true; |
| 414 |
} |
| 415 |
|
| 416 |
bool JazzWriterParser::countCharactersInPLC(unsigned plcId, long &n) |
| 417 |
{ |
| 418 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 419 |
if (!rsrcParser) { |
| 420 |
MWAW_DEBUG_MSG(("JazzWriterParser::countCharactersInPLC: can not find the rsrc parser\n")); |
| 421 |
return false; |
| 422 |
} |
| 423 |
MWAWInputStreamPtr input = rsrcInput(); |
| 424 |
MWAWEntry entry=rsrcParser->getEntry("WSCR", int(plcId)); |
| 425 |
if (!entry.valid() || !input || !input->checkPosition(entry.end())) { |
| 426 |
MWAW_DEBUG_MSG(("JazzWriterParser::countCharactersInPLC: can not find the %x WSCR\n", plcId)); |
| 427 |
return false; |
| 428 |
} |
| 429 |
n=0; |
| 430 |
input->seek(entry.begin(), librevenge::RVNG_SEEK_SET); |
| 431 |
long endPos=entry.end(); |
| 432 |
while (input->tell()+6<=endPos) { |
| 433 |
long pos=input->tell(); |
| 434 |
int type=int(input->readULong(2)); |
| 435 |
switch (type) { |
| 436 |
case 1: |
| 437 |
n+=int(input->readULong(4)); |
| 438 |
break; |
| 439 |
case 3: |
| 440 |
input->seek(3, librevenge::RVNG_SEEK_CUR); |
| 441 |
n+=int(input->readULong(1)); |
| 442 |
break; |
| 443 |
case 5: |
| 444 |
pos+=6; |
| 445 |
break; |
| 446 |
default: |
| 447 |
break; |
| 448 |
} |
| 449 |
input->seek(pos+6, librevenge::RVNG_SEEK_SET); |
| 450 |
} |
| 451 |
return true; |
| 452 |
} |
| 453 |
|
| 454 |
bool JazzWriterParser::readParagraph(MWAWEntry const &entry) |
| 455 |
{ |
| 456 |
if (entry.length()!=122) { |
| 457 |
MWAW_DEBUG_MSG(("JazzWriterParser::readParagraph: unexpected size\n")); |
| 458 |
return false; |
| 459 |
} |
| 460 |
MWAWInputStreamPtr input = rsrcInput(); |
| 461 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 462 |
libmwaw::DebugStream f; |
| 463 |
|
| 464 |
JazzWriterParserInternal::Paragraph para; |
| 465 |
f << "Entries(Paragraph)[" << entry.id() << "]:"; |
| 466 |
int val; |
| 467 |
para.m_nextParagraphId=unsigned(input->readLong(2)); |
| 468 |
if (para.m_nextParagraphId) f << "next[para]=" << para.m_nextParagraphId << ","; |
| 469 |
val=int(input->readLong(2)); |
| 470 |
if (val) f << "f0=" << val << ","; |
| 471 |
val=int(input->readLong(2)); |
| 472 |
if (val!=2) |
| 473 |
f << "ID1=" << input->readULong(2) << ","; |
| 474 |
else |
| 475 |
f << "ID1=" << input->readULong(2) << "[" << val << "],"; |
| 476 |
f << "dim0=" << input->readLong(2) << ","; |
| 477 |
int dims[2]; |
| 478 |
for (auto &d : dims) d=int(input->readLong(2)); |
| 479 |
para.m_dimension=MWAWVec2i(dims[1],dims[0]); |
| 480 |
f << "dim=" << para.m_dimension << ","; |
| 481 |
val=int(input->readLong(2)); |
| 482 |
if (val==2) |
| 483 |
f << "ID2=" << input->readULong(2) << ","; |
| 484 |
else |
| 485 |
f << "ID2=" << input->readULong(2) << "[" << val << "],"; |
| 486 |
for (int i=0; i<11; ++i) { // f5,f7 some heigths?, f8=0|600 |
| 487 |
val=int(input->readLong(2)); |
| 488 |
if (val) f << "f" << i+1 << "=" << val << ","; |
| 489 |
} |
| 490 |
f << "unk=" << input->readLong(2) << ","; // 1000|5000 |
| 491 |
for (int i=0; i<2; ++i) { |
| 492 |
val=int(input->readLong(2)); |
| 493 |
if (val!=(i==1 ? 4 : 0)) |
| 494 |
f << "g" << i << "=" << val << ","; |
| 495 |
} |
| 496 |
f << "fl=" << std::hex << input->readULong(2) << std::dec << ","; // 1002|1003 |
| 497 |
val=int(input->readLong(2)); |
| 498 |
if (val!=0xc00) f << "g2=" << val << ","; |
| 499 |
for (int i=0; i<4; ++i) { // g3=0|8 |
| 500 |
val=int(input->readULong(2)); |
| 501 |
if (i==2 && (val&0x300)) { |
| 502 |
f << "align=" << ((val>>8)&3) << ","; |
| 503 |
switch ((val>>8)&3) { |
| 504 |
case 1: |
| 505 |
para.m_paragraph.m_justify=MWAWParagraph::JustificationRight; |
| 506 |
break; |
| 507 |
case 2: |
| 508 |
para.m_paragraph.m_justify=MWAWParagraph::JustificationCenter; |
| 509 |
break; |
| 510 |
case 3: |
| 511 |
para.m_paragraph.m_justify=MWAWParagraph::JustificationFull; |
| 512 |
break; |
| 513 |
default: |
| 514 |
break; |
| 515 |
} |
| 516 |
val &= 0xfcff; |
| 517 |
} |
| 518 |
if (i==3 && (val&3)) { |
| 519 |
f << "line spacing=" << 1+float(val&3)/2 << ","; |
| 520 |
para.m_paragraph.setInterline(1+float(val&3)/2, librevenge::RVNG_PERCENT); |
| 521 |
val &= 0xfffc; |
| 522 |
} |
| 523 |
if (!val) continue; |
| 524 |
if (i==0) |
| 525 |
f << "g3=" << std::hex << val << std::dec << ","; |
| 526 |
else |
| 527 |
f << "g" << i+3 << "=" << val << ","; |
| 528 |
} |
| 529 |
ascFile.addPos(entry.begin()-4); |
| 530 |
ascFile.addNote(f.str().c_str()); |
| 531 |
|
| 532 |
input->seek(entry.begin()+58, librevenge::RVNG_SEEK_SET); |
| 533 |
long pos=input->tell(); |
| 534 |
f.str(""); |
| 535 |
f << "Paragraph-A:"; |
| 536 |
int N=int(input->readLong(2)); |
| 537 |
f << "num[tabs]=" << N << ","; |
| 538 |
if (N<0 || N>12) { |
| 539 |
MWAW_DEBUG_MSG(("JazzWriterParser::readParagraph: the number of tabs seems bads\n")); |
| 540 |
f << "###"; |
| 541 |
N=0; |
| 542 |
} |
| 543 |
para.m_paragraph.m_marginsUnit=librevenge::RVNG_POINT; |
| 544 |
for (int i=0; i<3; ++i) { |
| 545 |
val=int(input->readLong(2)); |
| 546 |
int const expected[]= {72, 0x21c, 72}; |
| 547 |
para.m_paragraph.m_margins[i==2 ? 0 : i+1]=val; |
| 548 |
if (val==expected[i]) continue; |
| 549 |
f << (i==0 ? "marg[left]" : i==1 ? "marg[right]" : "first[ident]") << "=" << val << ","; |
| 550 |
} |
| 551 |
*para.m_paragraph.m_margins[0]-=*para.m_paragraph.m_margins[1]; |
| 552 |
para.m_paragraph.m_margins[2]=0; // margin from left, so ignore |
| 553 |
f << "tabs["; |
| 554 |
for (int i=0; i<N; ++i) { |
| 555 |
MWAWTabStop tab; |
| 556 |
val=int(input->readLong(2)); |
| 557 |
if (val>=0) { |
| 558 |
tab.m_position=double(val)/72.; |
| 559 |
f << val << ","; |
| 560 |
} |
| 561 |
else { |
| 562 |
tab.m_position=double(-val)/72.; |
| 563 |
tab.m_alignment = MWAWTabStop::CENTER; |
| 564 |
f << -val << "[C],"; |
| 565 |
} |
| 566 |
para.m_paragraph.m_tabs->push_back(tab); |
| 567 |
} |
| 568 |
f << "],"; |
| 569 |
input->seek(pos+8+24, librevenge::RVNG_SEEK_SET); |
| 570 |
for (int i=0; i<6; ++i) { |
| 571 |
val=int(input->readLong(2)); |
| 572 |
if (val) f << "g" << i << "=" << val << ","; |
| 573 |
} |
| 574 |
val=int(input->readLong(2)); |
| 575 |
if (val!=2) |
| 576 |
f << "h0=" << val << ","; |
| 577 |
f << "ID1=" << input->readULong(2) << ","; |
| 578 |
for (int i=0; i<8; ++i) { |
| 579 |
val=int(input->readULong(2)); |
| 580 |
if (!val) continue; |
| 581 |
if (i==4) { |
| 582 |
para.m_plcId=unsigned(val); |
| 583 |
f << "plc[id]=" << val << ","; |
| 584 |
} |
| 585 |
else |
| 586 |
f << "h" << i+1 << "=" << val << ","; |
| 587 |
} |
| 588 |
if (m_state->m_idToParagraphs.find(unsigned(entry.id()))==m_state->m_idToParagraphs.end()) |
| 589 |
m_state->m_idToParagraphs[unsigned(entry.id())]=para; |
| 590 |
else { |
| 591 |
MWAW_DEBUG_MSG(("JazzWriterParser::readParagraph: paragraph %d already exists\n", entry.id())); |
| 592 |
f << "###id,"; |
| 593 |
} |
| 594 |
ascFile.addPos(pos); |
| 595 |
ascFile.addNote(f.str().c_str()); |
| 596 |
return true; |
| 597 |
} |
| 598 |
|
| 599 |
bool JazzWriterParser::readZone(MWAWEntry const &entry) |
| 600 |
{ |
| 601 |
if (entry.length()!=44) { |
| 602 |
MWAW_DEBUG_MSG(("JazzWriterParser::readZone: unexpected size\n")); |
| 603 |
return false; |
| 604 |
} |
| 605 |
MWAWInputStreamPtr input = rsrcInput(); |
| 606 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 607 |
libmwaw::DebugStream f; |
| 608 |
|
| 609 |
JazzWriterParserInternal::Zone zone; |
| 610 |
f << "Entries(Zone)[" << entry.id() << "]:"; |
| 611 |
zone.m_paragraphId=unsigned(input->readULong(2)); |
| 612 |
f << "para[id]=" << zone.m_paragraphId << ","; |
| 613 |
int id1=int(input->readULong(2)); |
| 614 |
f << "ID1=" << id1 << ","; |
| 615 |
int val=int(input->readULong(2)); |
| 616 |
if (unsigned(val)!=zone.m_paragraphId) |
| 617 |
f << "para[id1]=" << val << ","; |
| 618 |
val=int(input->readULong(2)); |
| 619 |
if (val!=id1) |
| 620 |
f << "ID2=" << val << ","; |
| 621 |
for (int i=0; i<3; ++i) { |
| 622 |
val=int(input->readLong(2)); |
| 623 |
if (val) |
| 624 |
f << "f" << i << "=" << val << ","; |
| 625 |
} |
| 626 |
int dims[2]; |
| 627 |
for (auto &d : dims) d=int(input->readLong(2)); |
| 628 |
f << "dim=" << MWAWVec2i(dims[1],dims[0]) << ","; |
| 629 |
val=int(input->readLong(2)); |
| 630 |
if (val) |
| 631 |
f << "f0=" << val << ","; |
| 632 |
zone.m_hfIds[0]=unsigned(input->readULong(2)); |
| 633 |
if (zone.m_hfIds[0]) f << "zone[header]=" << zone.m_hfIds[0] << ","; |
| 634 |
val=int(input->readULong(2)); |
| 635 |
if (val) |
| 636 |
f << "ID[header]=" << val << ","; |
| 637 |
zone.m_hfIds[1]=unsigned(input->readULong(2)); |
| 638 |
if (zone.m_hfIds[1]) f << "zone[footer]=" << zone.m_hfIds[1] << ","; |
| 639 |
val=int(input->readULong(2)); |
| 640 |
if (val) |
| 641 |
f << "ID[footer]=" << val << ","; |
| 642 |
for (int i=0; i<8; ++i) { // f3=1 header/footer, c:main? |
| 643 |
val=int(input->readLong(2)); |
| 644 |
if (val) f << "f" << i+1 << "=" << val << ","; |
| 645 |
} |
| 646 |
if (m_state->m_idToZones.find(unsigned(entry.id()))==m_state->m_idToZones.end()) |
| 647 |
m_state->m_idToZones[unsigned(entry.id())]=zone; |
| 648 |
else { |
| 649 |
MWAW_DEBUG_MSG(("JazzWriterParser::readZone: zone %d already exists\n", entry.id())); |
| 650 |
f << "###id,"; |
| 651 |
} |
| 652 |
ascFile.addPos(entry.begin()-4); |
| 653 |
ascFile.addNote(f.str().c_str()); |
| 654 |
return true; |
| 655 |
} |
| 656 |
|
| 657 |
//////////////////////////////////////////////////////////// |
| 658 |
// |
| 659 |
// Low level |
| 660 |
// |
| 661 |
//////////////////////////////////////////////////////////// |
| 662 |
bool JazzWriterParser::readString(MWAWInputStreamPtr input, librevenge::RVNGString &string, long endPos) |
| 663 |
{ |
| 664 |
string.clear(); |
| 665 |
if (!input) { |
| 666 |
MWAW_DEBUG_MSG(("JazzWriterParser::readString: can not find the input\n")); |
| 667 |
return false; |
| 668 |
} |
| 669 |
auto fontConverter=getFontConverter(); |
| 670 |
int defaultFont=3; |
| 671 |
long pos=input->tell(); |
| 672 |
int n=int(input->readULong(1)); |
| 673 |
if (!input->checkPosition(pos+1+n) || pos+1+n>endPos) { |
| 674 |
MWAW_DEBUG_MSG(("JazzWriterParser::readString: can not read the string length\n")); |
| 675 |
return false; |
| 676 |
} |
| 677 |
for (int i=0; i<n; ++i) { |
| 678 |
char c=char(input->readULong(1)); |
| 679 |
int unicode = fontConverter->unicode(defaultFont, static_cast<unsigned char>(c)); |
| 680 |
if (unicode>0) |
| 681 |
libmwaw::appendUnicode(uint32_t(unicode), string); |
| 682 |
else { |
| 683 |
MWAW_DEBUG_MSG(("JazzWriterParser::readString: find unknown unicode for char=%d\n", int(c))); |
| 684 |
} |
| 685 |
} |
| 686 |
return true; |
| 687 |
} |
| 688 |
|
| 689 |
//////////////////////////////////////////////////////////// |
| 690 |
// read the header |
| 691 |
//////////////////////////////////////////////////////////// |
| 692 |
bool JazzWriterParser::checkHeader(MWAWHeader *header, bool /*strict*/) |
| 693 |
{ |
| 694 |
*m_state = JazzWriterParserInternal::State(); |
| 695 |
if (!getRSRCParser()) |
| 696 |
return false; |
| 697 |
// check if the WDOC entry exists |
| 698 |
MWAWEntry entry = getRSRCParser()->getEntry("WDOC", 257); |
| 699 |
if (entry.begin()<=0) { // length can be not 0, so ... |
| 700 |
MWAW_DEBUG_MSG(("JazzWriterParser::checkHeader: can not find the WDOC[257] resource\n")); |
| 701 |
return false; |
| 702 |
} |
| 703 |
if (!getInput()->hasDataFork() || getInput()->size()<=0) { |
| 704 |
// checkme: is this possible when the document contains only a picture |
| 705 |
MWAW_DEBUG_MSG(("JazzWriterParser::checkHeader: can not find any data fork\n")); |
| 706 |
return false; |
| 707 |
} |
| 708 |
if (header) |
| 709 |
header->reset(MWAWDocument::MWAW_T_JAZZLOTUS, 1); |
| 710 |
|
| 711 |
return true; |
| 712 |
} |
| 713 |
|
| 714 |
//////////////////////////////////////////////////////////// |
| 715 |
// send the data |
| 716 |
//////////////////////////////////////////////////////////// |
| 717 |
bool JazzWriterParser::sendZone(unsigned zId) |
| 718 |
{ |
| 719 |
if (!getTextListener()) { |
| 720 |
MWAW_DEBUG_MSG(("JazzWriterParser::checkHeader: can not find the main listener\n")); |
| 721 |
return false; |
| 722 |
} |
| 723 |
auto it=m_state->m_idToZones.find(zId); |
| 724 |
if (it==m_state->m_idToZones.end()) { |
| 725 |
MWAW_DEBUG_MSG(("JazzWriterParser::checkZones: can not find the %x zone\n", zId)); |
| 726 |
return false; |
| 727 |
} |
| 728 |
|
| 729 |
auto input=getInput(); |
| 730 |
input->seek(it->second.m_entry.begin(), librevenge::RVNG_SEEK_SET); |
| 731 |
sendParagraph(it->second.m_paragraphId); |
| 732 |
return true; |
| 733 |
} |
| 734 |
|
| 735 |
bool JazzWriterParser::sendParagraph(unsigned pId) |
| 736 |
{ |
| 737 |
auto it=m_state->m_idToParagraphs.find(pId); |
| 738 |
if (it==m_state->m_idToParagraphs.end()) { |
| 739 |
MWAW_DEBUG_MSG(("JazzWriterParser::checkZones: can not find the %x paragraph\n", pId)); |
| 740 |
return false; |
| 741 |
} |
| 742 |
auto const ¶=it->second; |
| 743 |
getTextListener()->setParagraph(para.m_paragraph); |
| 744 |
sendPLC(para.m_plcId); |
| 745 |
if (para.m_nextParagraphId) |
| 746 |
sendParagraph(para.m_nextParagraphId); |
| 747 |
return true; |
| 748 |
} |
| 749 |
|
| 750 |
bool JazzWriterParser::sendPLC(unsigned pId) |
| 751 |
{ |
| 752 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 753 |
if (!rsrcParser) { |
| 754 |
MWAW_DEBUG_MSG(("JazzWriterParser::sendPLC: can not find the rsrc parser\n")); |
| 755 |
return false; |
| 756 |
} |
| 757 |
auto listener=getTextListener(); |
| 758 |
if (!listener) { |
| 759 |
MWAW_DEBUG_MSG(("JazzWriterParser::sendPLC: can not find the text listener\n")); |
| 760 |
return false; |
| 761 |
} |
| 762 |
MWAWInputStreamPtr input = getInput(); |
| 763 |
MWAWInputStreamPtr rInput = rsrcInput(); |
| 764 |
libmwaw::DebugFile &rAscFile = rsrcAscii(); |
| 765 |
libmwaw::DebugStream f; |
| 766 |
|
| 767 |
MWAWEntry entry=rsrcParser->getEntry("WSCR", int(pId)); |
| 768 |
if (!entry.valid()) { |
| 769 |
MWAW_DEBUG_MSG(("JazzWriterParser::sendPLC: can not find the %x plc\n", pId)); |
| 770 |
return false; |
| 771 |
} |
| 772 |
|
| 773 |
f << "Entries(PLC)[" << entry.id() << "]:"; |
| 774 |
rAscFile.addPos(entry.begin()-4); |
| 775 |
rAscFile.addNote(f.str().c_str()); |
| 776 |
|
| 777 |
rInput->seek(entry.begin(), librevenge::RVNG_SEEK_SET); |
| 778 |
int N=int(entry.length()/6); |
| 779 |
int val; |
| 780 |
auto fontConverter=getFontConverter(); |
| 781 |
for (int i=0; i<N; ++i) { |
| 782 |
long rPos=rInput->tell(); |
| 783 |
f.str(""); |
| 784 |
f << "PLC-" << i << ":"; |
| 785 |
int wh=int(rInput->readLong(2)); |
| 786 |
int numChar=0; |
| 787 |
switch (wh) { |
| 788 |
case 1: |
| 789 |
numChar=int(rInput->readULong(4)); |
| 790 |
f << "num=" << numChar << ","; |
| 791 |
break; |
| 792 |
case 2: { |
| 793 |
MWAWFont font; |
| 794 |
f << "font,"; |
| 795 |
f << "h=" << rInput->readULong(1) << ","; |
| 796 |
font.setId(int(rInput->readULong(1))); |
| 797 |
font.setSize(float(rInput->readULong(1))); |
| 798 |
val=int(rInput->readULong(1)); |
| 799 |
uint32_t flags = 0; |
| 800 |
if (val&0x1) flags |= MWAWFont::boldBit; |
| 801 |
if (val&0x2) flags |= MWAWFont::italicBit; |
| 802 |
if (val&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple); |
| 803 |
if (val&0x8) flags |= MWAWFont::embossBit; |
| 804 |
if (val&0x10) flags |= MWAWFont::shadowBit; |
| 805 |
font.setFlags(flags); |
| 806 |
val &= 0xe0; |
| 807 |
f << "font=[" << font.getDebugString(fontConverter) << "]"; |
| 808 |
if (val) |
| 809 |
f << "fl=" << std::hex << val << std::dec << ","; |
| 810 |
listener->setFont(font); |
| 811 |
break; |
| 812 |
} |
| 813 |
case 3: { |
| 814 |
val=int(rInput->readULong(2)); |
| 815 |
bool sent=false; |
| 816 |
if (val>=0xFFF0) { |
| 817 |
f << "field" << (0xFFFF-val) << ","; |
| 818 |
sent=true; |
| 819 |
switch (0xFFFF-val) { |
| 820 |
case 0: { |
| 821 |
MWAWField field(MWAWField::Date); |
| 822 |
field.m_DTFormat="%B %d, %Y"; |
| 823 |
listener->insertField(field); |
| 824 |
break; |
| 825 |
} |
| 826 |
case 1: { |
| 827 |
MWAWField field(MWAWField::Time); |
| 828 |
field.m_DTFormat="%I:%M %p"; |
| 829 |
listener->insertField(field); |
| 830 |
break; |
| 831 |
} |
| 832 |
case 2: |
| 833 |
listener->insertField(MWAWField(MWAWField::PageNumber)); |
| 834 |
break; |
| 835 |
default: |
| 836 |
MWAW_DEBUG_MSG(("JazzWriterParser::readPLC: find unknown field\n")); |
| 837 |
f << "###"; |
| 838 |
sent=false; |
| 839 |
break; |
| 840 |
} |
| 841 |
} |
| 842 |
else |
| 843 |
f << "link[id]=" << val << ","; |
| 844 |
f << "unk=" << int(rInput->readULong(1)) << ","; // 8-1e |
| 845 |
numChar=int(rInput->readULong(1)); |
| 846 |
f << "len=" << numChar << ","; |
| 847 |
if (sent) { |
| 848 |
input->seek(numChar, librevenge::RVNG_SEEK_CUR); |
| 849 |
numChar=0; |
| 850 |
} |
| 851 |
break; |
| 852 |
} |
| 853 |
case 5: { |
| 854 |
f << "pict[link],"; |
| 855 |
if (i+1>=N) { |
| 856 |
MWAW_DEBUG_MSG(("JazzWriterParser::readPLC: the zone seems too short\n")); |
| 857 |
f << "###"; |
| 858 |
break; |
| 859 |
} |
| 860 |
f << "link[id]=" << int(rInput->readULong(2)) << ","; |
| 861 |
unsigned pictId=unsigned(rInput->readULong(2)); |
| 862 |
f << "pict[id]=" << pictId << ","; |
| 863 |
int dim[2]; |
| 864 |
for (auto &d : dim) d=int(rInput->readULong(2)); |
| 865 |
f << "sz=" << MWAWVec2i(dim[1], dim[0]) << ","; |
| 866 |
int xPos=int(rInput->readLong(2)); |
| 867 |
f << "xPos=" << xPos << ","; // related to baseline? |
| 868 |
++i; |
| 869 |
rPos+=6; |
| 870 |
MWAWEmbeddedObject obj; |
| 871 |
if (!getPicture(pictId, obj) || obj.isEmpty()) { |
| 872 |
f << "##pictId"; |
| 873 |
break; |
| 874 |
} |
| 875 |
// TODO: use xPos |
| 876 |
MWAWPosition position(MWAWVec2f(0,0), MWAWVec2f(float(dim[1]), float(dim[0])), librevenge::RVNG_POINT); |
| 877 |
position.setRelativePosition(MWAWPosition::Char); |
| 878 |
listener->insertPicture(position, obj); |
| 879 |
listener->insertEOL(); // one picture by line |
| 880 |
break; |
| 881 |
} |
| 882 |
default: |
| 883 |
MWAW_DEBUG_MSG(("JazzWriterParser::readPLC: find unknown type\n")); |
| 884 |
f << "###type" << wh << ","; |
| 885 |
break; |
| 886 |
} |
| 887 |
rAscFile.addPos(rPos); |
| 888 |
rAscFile.addNote(f.str().c_str()); |
| 889 |
if (rInput->tell()!=rPos+6) |
| 890 |
rAscFile.addDelimiter(rInput->tell(),'|'); |
| 891 |
rInput->seek(rPos+6, librevenge::RVNG_SEEK_SET); |
| 892 |
|
| 893 |
// now read/send the character |
| 894 |
if (numChar<=0) continue; |
| 895 |
long pos=input->tell(); |
| 896 |
if (!input->checkPosition(pos+numChar)) { |
| 897 |
MWAW_DEBUG_MSG(("JazzWriterParser::readPLC: can not find some character\n")); |
| 898 |
break; |
| 899 |
} |
| 900 |
for (int c=0; c<numChar; ++c) { |
| 901 |
auto ch=(unsigned char)input->readULong(1); |
| 902 |
switch (ch) { |
| 903 |
case 0x9: |
| 904 |
listener->insertChar(ch); |
| 905 |
break; |
| 906 |
case 0xc: |
| 907 |
listener->insertBreak(MWAWTextListener::PageBreak); |
| 908 |
break; |
| 909 |
case 0xd: |
| 910 |
listener->insertEOL(); |
| 911 |
break; |
| 912 |
default: |
| 913 |
if (ch<=0x1f) { |
| 914 |
MWAW_DEBUG_MSG(("JazzWriterParser::readPLC: find bad character %d at pos=0x%lx\n", int(c), input->tell())); |
| 915 |
break; |
| 916 |
} |
| 917 |
listener->insertCharacter(ch); |
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
rAscFile.addPos(rInput->tell()); |
| 922 |
rAscFile.addNote("PLC-end:"); |
| 923 |
val=int(rInput->readLong(2)); |
| 924 |
if (val) |
| 925 |
f << "##f0=" << val << ","; |
| 926 |
return true; |
| 927 |
} |
| 928 |
|
| 929 |
bool JazzWriterParser::getPicture(unsigned pId, MWAWEmbeddedObject &obj) |
| 930 |
{ |
| 931 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 932 |
if (!rsrcParser) { |
| 933 |
MWAW_DEBUG_MSG(("JazzWriterParser::getPicture: can not find the rsrc parser\n")); |
| 934 |
return false; |
| 935 |
} |
| 936 |
MWAWInputStreamPtr input = rsrcInput(); |
| 937 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 938 |
|
| 939 |
MWAWEntry entry=rsrcParser->getEntry("PICT", int(pId)); |
| 940 |
if (!entry.valid()) { |
| 941 |
MWAW_DEBUG_MSG(("JazzWriterParser::getPicture: can not find the %x picture\n", pId)); |
| 942 |
return false; |
| 943 |
} |
| 944 |
input->seek(entry.begin(), librevenge::RVNG_SEEK_SET); |
| 945 |
std::shared_ptr<MWAWPict> pict(MWAWPictData::get(input, int(entry.length()))); |
| 946 |
if (!pict || !pict->getBinary(obj)) { |
| 947 |
MWAW_DEBUG_MSG(("JazzWriterParser::getPicture: can not read the %x picture\n", pId)); |
| 948 |
return false; |
| 949 |
} |
| 950 |
|
| 951 |
ascFile.skipZone(entry.begin(), entry.end()); |
| 952 |
return true; |
| 953 |
} |
| 954 |
|
| 955 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |