| 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 <cstring> |
| 35 |
#include <iomanip> |
| 36 |
#include <iostream> |
| 37 |
#include <limits> |
| 38 |
#include <map> |
| 39 |
#include <sstream> |
| 40 |
|
| 41 |
#include <librevenge/librevenge.h> |
| 42 |
|
| 43 |
#include "MWAWTextListener.hxx" |
| 44 |
#include "MWAWDebug.hxx" |
| 45 |
#include "MWAWFont.hxx" |
| 46 |
#include "MWAWFontConverter.hxx" |
| 47 |
#include "MWAWPageSpan.hxx" |
| 48 |
#include "MWAWParagraph.hxx" |
| 49 |
#include "MWAWPosition.hxx" |
| 50 |
#include "MWAWRSRCParser.hxx" |
| 51 |
#include "MWAWSubDocument.hxx" |
| 52 |
|
| 53 |
#include "ZWrtParser.hxx" |
| 54 |
|
| 55 |
#include "ZWrtText.hxx" |
| 56 |
|
| 57 |
/** Internal: the structures of a ZWrtText */ |
| 58 |
namespace ZWrtTextInternal |
| 59 |
{ |
| 60 |
//////////////////////////////////////// |
| 61 |
//! Internal: struct used to store the font of a ZWrtText |
| 62 |
struct Font { |
| 63 |
//! constructor |
| 64 |
Font() |
| 65 |
: m_font() |
| 66 |
, m_height(0) |
| 67 |
, m_extra("") |
| 68 |
{ |
| 69 |
} |
| 70 |
//! operator<< |
| 71 |
friend std::ostream &operator<<(std::ostream &o, Font const &font); |
| 72 |
|
| 73 |
//! the font |
| 74 |
MWAWFont m_font; |
| 75 |
//! the line height |
| 76 |
int m_height; |
| 77 |
//! extra data |
| 78 |
std::string m_extra; |
| 79 |
}; |
| 80 |
|
| 81 |
std::ostream &operator<<(std::ostream &o, Font const &font) |
| 82 |
{ |
| 83 |
if (font.m_height) |
| 84 |
o << "h=" << font.m_height << ","; |
| 85 |
o << font.m_extra; |
| 86 |
return o; |
| 87 |
} |
| 88 |
|
| 89 |
//////////////////////////////////////// |
| 90 |
//! Internal: struct used to store a header/footer of a ZWrtText |
| 91 |
struct HFZone { |
| 92 |
//! constructor |
| 93 |
HFZone() |
| 94 |
: m_pos() |
| 95 |
, m_font() |
| 96 |
, m_extra("") |
| 97 |
{ |
| 98 |
m_font.m_font = MWAWFont(3,12); |
| 99 |
} |
| 100 |
//! returns true if the zone is not empty |
| 101 |
bool ok() const |
| 102 |
{ |
| 103 |
return m_pos.valid(); |
| 104 |
} |
| 105 |
//! operator<< |
| 106 |
std::string getDebugString(MWAWFontConverterPtr &convert) const |
| 107 |
{ |
| 108 |
std::stringstream s; |
| 109 |
if (!m_pos.valid()) return s.str(); |
| 110 |
if (convert) |
| 111 |
s << m_font.m_font.getDebugString(convert) << m_font << ","; |
| 112 |
else |
| 113 |
s << m_font << ","; |
| 114 |
s << m_extra; |
| 115 |
return s.str(); |
| 116 |
} |
| 117 |
//! the text position |
| 118 |
MWAWEntry m_pos; |
| 119 |
//! the font |
| 120 |
Font m_font; |
| 121 |
//! extra data |
| 122 |
std::string m_extra; |
| 123 |
}; |
| 124 |
|
| 125 |
//////////////////////////////////////// |
| 126 |
//! Internal: struct used to store a section of a ZWrtText |
| 127 |
struct Section { |
| 128 |
//! constructor |
| 129 |
Section() |
| 130 |
: m_id() |
| 131 |
, m_pos() |
| 132 |
, m_name("") |
| 133 |
, m_idFontMap() |
| 134 |
, m_parsed(false) |
| 135 |
{ |
| 136 |
} |
| 137 |
//! the section id |
| 138 |
int m_id; |
| 139 |
//! the text position |
| 140 |
MWAWEntry m_pos; |
| 141 |
//! the section name |
| 142 |
std::string m_name; |
| 143 |
//! a map pos -> font |
| 144 |
std::map<long,Font> m_idFontMap; |
| 145 |
//! true if the section is parsed |
| 146 |
mutable bool m_parsed; |
| 147 |
}; |
| 148 |
|
| 149 |
//////////////////////////////////////// |
| 150 |
//! Internal: the state of a ZWrtText |
| 151 |
struct State { |
| 152 |
//! constructor |
| 153 |
State() |
| 154 |
: m_version(-1) |
| 155 |
, m_numPages(-1) |
| 156 |
, m_actualPage(1) |
| 157 |
, m_idSectionMap() |
| 158 |
, m_header() |
| 159 |
, m_footer() |
| 160 |
{ |
| 161 |
} |
| 162 |
//! return a section for an id ( if it does not exists, create id ) |
| 163 |
Section &getSection(int id) |
| 164 |
{ |
| 165 |
auto it = m_idSectionMap.find(id); |
| 166 |
if (it != m_idSectionMap.end()) |
| 167 |
return it->second; |
| 168 |
it = m_idSectionMap.insert |
| 169 |
(std::map<int,Section>::value_type(id,Section())).first; |
| 170 |
it->second.m_id = id; |
| 171 |
return it->second; |
| 172 |
} |
| 173 |
|
| 174 |
//! the file version |
| 175 |
mutable int m_version; |
| 176 |
int m_numPages /* the number of pages */, m_actualPage /* the actual page */; |
| 177 |
|
| 178 |
//! a map id -> section |
| 179 |
std::map<int, Section> m_idSectionMap; |
| 180 |
//! the header zone |
| 181 |
HFZone m_header; |
| 182 |
//! the footer zone |
| 183 |
HFZone m_footer; |
| 184 |
}; |
| 185 |
|
| 186 |
//////////////////////////////////////// |
| 187 |
//! Internal: the subdocument of a ZWrtText |
| 188 |
class SubDocument final : public MWAWSubDocument |
| 189 |
{ |
| 190 |
public: |
| 191 |
SubDocument(ZWrtText &pars, MWAWInputStreamPtr const &input, int id, MWAWEntry const &entry, ZWrtText::TextCode type) |
| 192 |
: MWAWSubDocument(pars.m_mainParser, input, MWAWEntry()) |
| 193 |
, m_textParser(&pars) |
| 194 |
, m_id(id) |
| 195 |
, m_type(type) |
| 196 |
, m_pos(entry) |
| 197 |
{ |
| 198 |
} |
| 199 |
|
| 200 |
//! destructor |
| 201 |
~SubDocument() final {} |
| 202 |
|
| 203 |
//! operator!= |
| 204 |
bool operator!=(MWAWSubDocument const &doc) const final; |
| 205 |
|
| 206 |
//! the parser function |
| 207 |
void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final; |
| 208 |
|
| 209 |
protected: |
| 210 |
/** the text parser */ |
| 211 |
ZWrtText *m_textParser; |
| 212 |
//! the section id |
| 213 |
int m_id; |
| 214 |
//! the type of document |
| 215 |
ZWrtText::TextCode m_type; |
| 216 |
//! the file pos |
| 217 |
MWAWEntry m_pos; |
| 218 |
private: |
| 219 |
SubDocument(SubDocument const &orig) = delete; |
| 220 |
SubDocument &operator=(SubDocument const &orig) = delete; |
| 221 |
}; |
| 222 |
|
| 223 |
bool SubDocument::operator!=(MWAWSubDocument const &doc) const |
| 224 |
{ |
| 225 |
if (MWAWSubDocument::operator!=(doc)) return true; |
| 226 |
auto const *sDoc = dynamic_cast<SubDocument const *>(&doc); |
| 227 |
if (!sDoc) return true; |
| 228 |
if (m_textParser != sDoc->m_textParser) return true; |
| 229 |
if (m_id != sDoc->m_id) return true; |
| 230 |
if (m_type != sDoc->m_type) return true; |
| 231 |
if (m_pos != sDoc->m_pos) return true; |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/) |
| 236 |
{ |
| 237 |
if (!listener.get()) { |
| 238 |
MWAW_DEBUG_MSG(("ZWrtTextInternal::SubDocument::parse: no listener\n")); |
| 239 |
return; |
| 240 |
} |
| 241 |
if (!m_textParser) { |
| 242 |
MWAW_DEBUG_MSG(("ZWrtTextInternal::SubDocument::parse: no parser\n")); |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
long pos = m_input->tell(); |
| 247 |
if (m_type==ZWrtText::Link) |
| 248 |
listener->insertUnicodeString(librevenge::RVNGString("link to ")); |
| 249 |
else if (m_type==ZWrtText::Tag) |
| 250 |
listener->insertUnicodeString(librevenge::RVNGString("ref: ")); |
| 251 |
m_textParser->sendText(m_id, m_pos); |
| 252 |
m_input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
//////////////////////////////////////////////////////////// |
| 257 |
// constructor/destructor, ... |
| 258 |
//////////////////////////////////////////////////////////// |
| 259 |
ZWrtText::ZWrtText(ZWrtParser &parser) |
| 260 |
: m_parserState(parser.getParserState()) |
| 261 |
, m_state(new ZWrtTextInternal::State) |
| 262 |
, m_mainParser(&parser) |
| 263 |
{ |
| 264 |
} |
| 265 |
|
| 266 |
ZWrtText::~ZWrtText() |
| 267 |
{ |
| 268 |
} |
| 269 |
|
| 270 |
int ZWrtText::version() const |
| 271 |
{ |
| 272 |
if (m_state->m_version < 0) |
| 273 |
m_state->m_version = m_parserState->m_version; |
| 274 |
return m_state->m_version; |
| 275 |
} |
| 276 |
|
| 277 |
int ZWrtText::numPages() const |
| 278 |
{ |
| 279 |
if (m_state->m_numPages >= 0) |
| 280 |
return m_state->m_numPages; |
| 281 |
const_cast<ZWrtText *>(this)->computePositions(); |
| 282 |
return m_state->m_numPages; |
| 283 |
} |
| 284 |
|
| 285 |
bool ZWrtText::hasHeaderFooter(bool header) const |
| 286 |
{ |
| 287 |
if (header) return m_state->m_header.ok(); |
| 288 |
return m_state->m_footer.ok(); |
| 289 |
} |
| 290 |
|
| 291 |
void ZWrtText::computePositions() |
| 292 |
{ |
| 293 |
m_state->m_actualPage = 1; |
| 294 |
|
| 295 |
MWAWInputStreamPtr input = m_mainParser->rsrcInput(); |
| 296 |
int nPages = 0; |
| 297 |
for (auto const &it : m_state->m_idSectionMap) { |
| 298 |
nPages++; |
| 299 |
auto const §ion=it.second; |
| 300 |
if (!section.m_pos.valid()) |
| 301 |
continue; |
| 302 |
long endPos = section.m_pos.end(); |
| 303 |
input->seek(section.m_pos.begin(), librevenge::RVNG_SEEK_SET); |
| 304 |
while (!input->isEnd()) { |
| 305 |
if (input->tell()+3 >= endPos) |
| 306 |
break; |
| 307 |
if (char(input->readLong(1))!='<') |
| 308 |
continue; |
| 309 |
if (char(input->readLong(1))!='N') |
| 310 |
continue; |
| 311 |
if (char(input->readLong(1))!='>') |
| 312 |
continue; |
| 313 |
nPages++; |
| 314 |
} |
| 315 |
} |
| 316 |
m_state->m_numPages = nPages; |
| 317 |
} |
| 318 |
|
| 319 |
//////////////////////////////////////////////////////////// |
| 320 |
// Intermediate level |
| 321 |
//////////////////////////////////////////////////////////// |
| 322 |
|
| 323 |
// find the different zones |
| 324 |
bool ZWrtText::createZones() |
| 325 |
{ |
| 326 |
MWAWRSRCParserPtr rsrcParser = m_mainParser->getRSRCParser(); |
| 327 |
if (!rsrcParser) { |
| 328 |
MWAW_DEBUG_MSG(("ZWrtText::createZones: can not find the entry map\n")); |
| 329 |
return false; |
| 330 |
} |
| 331 |
auto const &entryMap = rsrcParser->getEntriesMap(); |
| 332 |
// the 128 zones |
| 333 |
char const *zNames[] = {"HEAD", "FOOT", "STLS"}; |
| 334 |
for (int z = 0; z < 3; z++) { |
| 335 |
auto it = entryMap.lower_bound(zNames[z]); |
| 336 |
while (it != entryMap.end()) { |
| 337 |
if (it->first != zNames[z]) |
| 338 |
break; |
| 339 |
MWAWEntry const &entry = it++->second; |
| 340 |
switch (z) { |
| 341 |
case 0: |
| 342 |
case 1: |
| 343 |
readHFZone(entry); |
| 344 |
break; |
| 345 |
case 2: // 128 and following |
| 346 |
readStyles(entry); |
| 347 |
break; |
| 348 |
default: |
| 349 |
break; |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
// 1001 and following |
| 355 |
char const *sNames[] = {"styl","TEXT"}; |
| 356 |
for (int z = 0; z < 2; z++) { |
| 357 |
auto it = entryMap.lower_bound(sNames[z]); |
| 358 |
while (it != entryMap.end()) { |
| 359 |
if (it->first != sNames[z]) |
| 360 |
break; |
| 361 |
MWAWEntry const &entry = it++->second; |
| 362 |
switch (z) { |
| 363 |
case 0: |
| 364 |
readSectionFonts(entry); |
| 365 |
break; |
| 366 |
case 1: { |
| 367 |
auto &sec=m_state->getSection(entry.id()); |
| 368 |
sec.m_pos = entry; |
| 369 |
break; |
| 370 |
} |
| 371 |
default: |
| 372 |
break; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
// now update the different data |
| 378 |
computePositions(); |
| 379 |
return true; |
| 380 |
} |
| 381 |
|
| 382 |
ZWrtText::TextCode ZWrtText::isTextCode |
| 383 |
(MWAWInputStreamPtr &input, long endPos, MWAWEntry &dPos) const |
| 384 |
{ |
| 385 |
dPos=MWAWEntry(); |
| 386 |
long pos = input->tell(); |
| 387 |
if (pos+2 > endPos) |
| 388 |
return None; |
| 389 |
auto c=char(input->readLong(1)); |
| 390 |
if (c=='C' || c=='N') { |
| 391 |
if (char(input->readLong(1))!='>') { |
| 392 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 393 |
return None; |
| 394 |
} |
| 395 |
return c=='C' ? Center : NewPage; |
| 396 |
} |
| 397 |
std::string expectedString(""); |
| 398 |
auto res = None; |
| 399 |
switch (c) { |
| 400 |
case 'b': |
| 401 |
expectedString="bookmark"; |
| 402 |
res = BookMark; |
| 403 |
break; |
| 404 |
case 'i': |
| 405 |
expectedString="insert"; |
| 406 |
res = Tag; |
| 407 |
break; |
| 408 |
case 'l': |
| 409 |
expectedString="link"; |
| 410 |
res = Link; |
| 411 |
break; |
| 412 |
default: |
| 413 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 414 |
return None; |
| 415 |
} |
| 416 |
expectedString += ' '; |
| 417 |
for (size_t s=1; s < expectedString.size(); s++) { |
| 418 |
if (input->isEnd() || input->tell() >= endPos || |
| 419 |
char(input->readLong(1)) != expectedString[s]) { |
| 420 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 421 |
return None; |
| 422 |
} |
| 423 |
} |
| 424 |
dPos.setBegin(input->tell()); |
| 425 |
while (1) { |
| 426 |
if (input->isEnd() || input->tell() >= endPos) { |
| 427 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 428 |
break; |
| 429 |
} |
| 430 |
c=char(input->readLong(1)); |
| 431 |
if (c==0 || c==0xa || c==0xd) { |
| 432 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 433 |
return None; |
| 434 |
} |
| 435 |
if (c=='>') { |
| 436 |
dPos.setEnd(input->tell()-1); |
| 437 |
return res; |
| 438 |
} |
| 439 |
} |
| 440 |
return None; |
| 441 |
} |
| 442 |
|
| 443 |
bool ZWrtText::sendText(ZWrtTextInternal::Section const &zone, MWAWEntry const &entry) |
| 444 |
{ |
| 445 |
MWAWTextListenerPtr listener=m_parserState->m_textListener; |
| 446 |
if (!listener) { |
| 447 |
MWAW_DEBUG_MSG(("ZWrtText::sendText: can not find a listener\n")); |
| 448 |
return false; |
| 449 |
} |
| 450 |
bool main = entry.begin()==zone.m_pos.begin(); |
| 451 |
if (main) |
| 452 |
m_mainParser->newPage(m_state->m_actualPage++); |
| 453 |
if (!entry.valid()) |
| 454 |
return true; |
| 455 |
MWAWInputStreamPtr input = m_mainParser->rsrcInput(); |
| 456 |
libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii(); |
| 457 |
libmwaw::DebugStream f; |
| 458 |
f << "Entries(TextContent)[" << zone.m_name << "]:"; |
| 459 |
zone.m_parsed=true; |
| 460 |
long pos = entry.begin(), endPos = entry.end(); |
| 461 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 462 |
|
| 463 |
ZWrtTextInternal::Font actFont; |
| 464 |
actFont.m_font=MWAWFont(3,12); |
| 465 |
long cPos = pos-zone.m_pos.begin(); |
| 466 |
auto fIt= zone.m_idFontMap.begin(); |
| 467 |
while (fIt != zone.m_idFontMap.end() && fIt->first<cPos) |
| 468 |
actFont = fIt++->second; |
| 469 |
listener->setFont(actFont.m_font); |
| 470 |
int fId=0; |
| 471 |
bool isCenter = false; |
| 472 |
MWAWParagraph para; |
| 473 |
while (1) { |
| 474 |
long actPos = input->tell(); |
| 475 |
bool done = input->isEnd() || actPos==endPos; |
| 476 |
|
| 477 |
char c = done ? char(0) : char(input->readULong(1)); |
| 478 |
if (c==0xd || done) { |
| 479 |
ascFile.addPos(pos); |
| 480 |
ascFile.addNote(f.str().c_str()); |
| 481 |
f.str(""); |
| 482 |
f << "TextContent:"; |
| 483 |
pos = actPos+1; |
| 484 |
} |
| 485 |
if (done) break; |
| 486 |
while (fIt != zone.m_idFontMap.end() && fIt->first<=cPos) { |
| 487 |
actFont = fIt++->second; |
| 488 |
listener->setFont(actFont.m_font); |
| 489 |
f << "[F" << fId++ << "]"; |
| 490 |
} |
| 491 |
|
| 492 |
cPos++; |
| 493 |
TextCode textCode; |
| 494 |
MWAWEntry textData; |
| 495 |
if (c=='<' && (textCode=isTextCode(input, endPos, textData))!=None) { |
| 496 |
long newPos = input->tell(); |
| 497 |
switch (textCode) { |
| 498 |
case Center: |
| 499 |
isCenter=true; |
| 500 |
para.m_justify=MWAWParagraph::JustificationCenter; |
| 501 |
listener->setParagraph(para); |
| 502 |
break; |
| 503 |
case NewPage: |
| 504 |
if (main) |
| 505 |
m_mainParser->newPage(m_state->m_actualPage++); |
| 506 |
break; |
| 507 |
case Link: |
| 508 |
case Tag: |
| 509 |
case BookMark: { |
| 510 |
if (textCode==Link) { |
| 511 |
MWAW_DEBUG_MSG(("ZWrtText::sendText: find a link, uses bookmark\n")); |
| 512 |
} |
| 513 |
else if (textCode==Tag) { |
| 514 |
MWAW_DEBUG_MSG(("ZWrtText::sendText: find a tag, uses bookmark\n")); |
| 515 |
} |
| 516 |
MWAWSubDocumentPtr subdoc(new ZWrtTextInternal::SubDocument(*this, input, zone.m_id, textData, textCode)); |
| 517 |
listener->insertComment(subdoc); |
| 518 |
break; |
| 519 |
} |
| 520 |
// coverity[dead_error_line : FALSE]: intentional ( needed to remove warning in other compiler ) |
| 521 |
case None: |
| 522 |
#if !defined(__clang__) |
| 523 |
default: |
| 524 |
#endif |
| 525 |
break; |
| 526 |
} |
| 527 |
input->seek(newPos, librevenge::RVNG_SEEK_SET); |
| 528 |
cPos=newPos-zone.m_pos.begin(); |
| 529 |
continue; |
| 530 |
} |
| 531 |
switch (c) { |
| 532 |
case 0x9: |
| 533 |
listener->insertTab(); |
| 534 |
break; |
| 535 |
case 0xd: |
| 536 |
listener->insertEOL(); |
| 537 |
if (isCenter) { |
| 538 |
isCenter=false; |
| 539 |
para.m_justify=MWAWParagraph::JustificationLeft; |
| 540 |
listener->setParagraph(para); |
| 541 |
} |
| 542 |
break; |
| 543 |
default: |
| 544 |
listener->insertCharacter(static_cast<unsigned char>(c), input, endPos); |
| 545 |
break; |
| 546 |
} |
| 547 |
f << c; |
| 548 |
} |
| 549 |
return true; |
| 550 |
} |
| 551 |
|
| 552 |
bool ZWrtText::sendText(int sectionId, MWAWEntry const &entry) |
| 553 |
{ |
| 554 |
if (!m_parserState->m_textListener) { |
| 555 |
MWAW_DEBUG_MSG(("ZWrtText::sendText: can not find a listener\n")); |
| 556 |
return false; |
| 557 |
} |
| 558 |
auto it = m_state->m_idSectionMap.find(sectionId); |
| 559 |
if (it==m_state->m_idSectionMap.end()) { |
| 560 |
MWAW_DEBUG_MSG(("ZWrtText::sendText: can not find the section\n")); |
| 561 |
return false; |
| 562 |
} |
| 563 |
sendText(it->second, entry); |
| 564 |
return true; |
| 565 |
} |
| 566 |
|
| 567 |
bool ZWrtText::sendMainText() |
| 568 |
{ |
| 569 |
if (!m_parserState->m_textListener) { |
| 570 |
MWAW_DEBUG_MSG(("ZWrtText::sendMainText: can not find a listener\n")); |
| 571 |
return false; |
| 572 |
} |
| 573 |
for (auto const &it : m_state->m_idSectionMap) { |
| 574 |
auto const §ion=it.second; |
| 575 |
sendText(section, section.m_pos); |
| 576 |
} |
| 577 |
return true; |
| 578 |
} |
| 579 |
|
| 580 |
////////////////////////////////////////////// |
| 581 |
// Fonts |
| 582 |
////////////////////////////////////////////// |
| 583 |
bool ZWrtText::readSectionFonts(MWAWEntry const &entry) |
| 584 |
{ |
| 585 |
if (!entry.valid() || entry.length() < 2) { |
| 586 |
MWAW_DEBUG_MSG(("ZWrtText::readSectionFonts: the entry is bad\n")); |
| 587 |
return false; |
| 588 |
} |
| 589 |
|
| 590 |
MWAWInputStreamPtr input = m_mainParser->rsrcInput(); |
| 591 |
libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii(); |
| 592 |
libmwaw::DebugStream f; |
| 593 |
f << "Entries(" << entry.type() << ")[" << entry << "]:"; |
| 594 |
entry.setParsed(true); |
| 595 |
auto §ion = m_state->getSection(entry.id()); |
| 596 |
section.m_name = entry.name(); |
| 597 |
|
| 598 |
long pos = entry.begin(); |
| 599 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 600 |
auto N=static_cast<int>(input->readLong(2)); |
| 601 |
f << "N=" << N << ","; |
| 602 |
if (2+N*20 != int(entry.length())) { |
| 603 |
MWAW_DEBUG_MSG(("ZWrtText::readSectionFonts: the number N seems bad\n")); |
| 604 |
f << "###"; |
| 605 |
ascFile.addPos(pos-4); |
| 606 |
ascFile.addNote(f.str().c_str()); |
| 607 |
return false; |
| 608 |
} |
| 609 |
ascFile.addPos(pos-4); |
| 610 |
ascFile.addNote(f.str().c_str()); |
| 611 |
|
| 612 |
for (int i = 0; i < N; i++) { |
| 613 |
pos = input->tell(); |
| 614 |
ZWrtTextInternal::Font font; |
| 615 |
f.str(""); |
| 616 |
auto cPos=long(input->readULong(4)); |
| 617 |
font.m_height = static_cast<int>(input->readLong(2)); |
| 618 |
auto sz = float(input->readLong(2)); |
| 619 |
font.m_font.setId(static_cast<int>(input->readULong(2))); |
| 620 |
auto flag = static_cast<int>(input->readULong(1)); |
| 621 |
uint32_t flags = 0; |
| 622 |
if (flag&0x1) flags |= MWAWFont::boldBit; |
| 623 |
if (flag&0x2) flags |= MWAWFont::italicBit; |
| 624 |
if (flag&0x4) font.m_font.setUnderlineStyle(MWAWFont::Line::Simple); |
| 625 |
if (flag&0xF8) f << "fl0=" << std::hex << (flag&0xF8) << std::dec << ","; |
| 626 |
|
| 627 |
flag = static_cast<int>(input->readULong(1)); // alway 0 |
| 628 |
if (flag) f << "#fl1=" << std::hex << flag << std::dec << ","; |
| 629 |
font.m_font.setSize(float(input->readLong(2))); |
| 630 |
if (sz < font.m_font.size() || sz > font.m_font.size()) |
| 631 |
f << "#sz=" << sz << ","; |
| 632 |
unsigned char col[3]; |
| 633 |
for (auto &c : col) c = static_cast<unsigned char>(input->readULong(2)>>8); |
| 634 |
if (col[0] || col[1] || col[2]) |
| 635 |
font.m_font.setColor(MWAWColor(col[0],col[1],col[2])); |
| 636 |
font.m_font.setFlags(flags); |
| 637 |
font.m_extra = f.str(); |
| 638 |
section.m_idFontMap.insert(std::map<long, ZWrtTextInternal::Font>::value_type(cPos, font)); |
| 639 |
|
| 640 |
f.str(""); |
| 641 |
f << entry.type() << "-F" << i << ":cPos=" << std::hex << cPos << std::dec << "," |
| 642 |
<< font.m_font.getDebugString(m_parserState->m_fontConverter) << font; |
| 643 |
ascFile.addPos(pos); |
| 644 |
ascFile.addNote(f.str().c_str()); |
| 645 |
input->seek(pos+20, librevenge::RVNG_SEEK_SET); |
| 646 |
} |
| 647 |
return true; |
| 648 |
} |
| 649 |
|
| 650 |
////////////////////////////////////////////// |
| 651 |
// Styles |
| 652 |
////////////////////////////////////////////// |
| 653 |
bool ZWrtText::readStyles(MWAWEntry const &entry) |
| 654 |
{ |
| 655 |
if (!entry.valid()) { |
| 656 |
MWAW_DEBUG_MSG(("ZWrtText::readStyles: the entry is bad\n")); |
| 657 |
return false; |
| 658 |
} |
| 659 |
|
| 660 |
long pos = entry.begin(); |
| 661 |
MWAWInputStreamPtr input = m_mainParser->rsrcInput(); |
| 662 |
libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii(); |
| 663 |
libmwaw::DebugStream f; |
| 664 |
f << "Entries(" << entry.type() << ")[" << entry << "]:"; |
| 665 |
entry.setParsed(true); |
| 666 |
|
| 667 |
std::vector<ZWField> fields; |
| 668 |
if (!m_mainParser->getFieldList(entry, fields)) { |
| 669 |
MWAW_DEBUG_MSG(("ZWrtText::readStyles: can not get fields list\n")); |
| 670 |
f << "###"; |
| 671 |
ascFile.addPos(pos-4); |
| 672 |
ascFile.addNote(f.str().c_str()); |
| 673 |
return false; |
| 674 |
} |
| 675 |
|
| 676 |
size_t numFields = fields.size(); |
| 677 |
if (numFields < 9) { |
| 678 |
MWAW_DEBUG_MSG(("ZWrtText::readStyles: the fields list seems very short\n")); |
| 679 |
} |
| 680 |
std::string strVal; |
| 681 |
int intVal; |
| 682 |
bool boolVal; |
| 683 |
for (size_t ff = 0; ff < numFields; ff++) { |
| 684 |
ZWField const &field = fields[ff]; |
| 685 |
bool done = false; |
| 686 |
unsigned char color[3]; |
| 687 |
switch (ff) { |
| 688 |
case 0: |
| 689 |
done = field.getString(input, strVal); |
| 690 |
if (!done||!strVal.length()) |
| 691 |
break; |
| 692 |
f << "font=" << strVal << ","; |
| 693 |
break; |
| 694 |
case 1: |
| 695 |
done = field.getInt(input, intVal); |
| 696 |
if (!done||!intVal) |
| 697 |
break; |
| 698 |
f << "fSz=" << intVal << ","; |
| 699 |
break; |
| 700 |
case 2: |
| 701 |
case 3: |
| 702 |
case 4: { |
| 703 |
color[0]=color[1]=color[2]=0; |
| 704 |
done = field.getInt(input, intVal); |
| 705 |
if (!done) |
| 706 |
break; |
| 707 |
color[ff-2]=static_cast<unsigned char>(intVal); |
| 708 |
while (ff < 4 && ff + 1 < numFields) { |
| 709 |
if (fields[++ff].getInt(input, intVal)) |
| 710 |
color[ff-2]=static_cast<unsigned char>(intVal); |
| 711 |
} |
| 712 |
if (color[0]||color[1]||color[2]) |
| 713 |
f << "col=" << MWAWColor(color[0],color[1],color[2]) << ","; |
| 714 |
break; |
| 715 |
} |
| 716 |
case 5: |
| 717 |
case 6: // italic? |
| 718 |
case 7: // always false ? |
| 719 |
done = field.getBool(input, boolVal); |
| 720 |
if (!done) |
| 721 |
break; |
| 722 |
if (boolVal) |
| 723 |
f << "f" << ff << "Set,"; |
| 724 |
break; |
| 725 |
case 8: // 0|1|2 |
| 726 |
done = field.getInt(input, intVal); |
| 727 |
if (!done||!intVal) |
| 728 |
break; |
| 729 |
f << "id?=" << intVal << ","; |
| 730 |
break; |
| 731 |
default: |
| 732 |
break; |
| 733 |
} |
| 734 |
if (done) |
| 735 |
continue; |
| 736 |
if (fields[ff].getDebugString(input, strVal)) |
| 737 |
f << "#f" << ff << "=\"" << strVal << "\","; |
| 738 |
else |
| 739 |
f << "#f" << ff << ","; |
| 740 |
} |
| 741 |
|
| 742 |
ascFile.addPos(pos-4); |
| 743 |
ascFile.addNote(f.str().c_str()); |
| 744 |
return true; |
| 745 |
} |
| 746 |
|
| 747 |
//////////////////////////////////////////////////////////// |
| 748 |
// read the header/footer zone |
| 749 |
//////////////////////////////////////////////////////////// |
| 750 |
bool ZWrtText::sendHeaderFooter(bool header) |
| 751 |
{ |
| 752 |
MWAWTextListenerPtr listener=m_parserState->m_textListener; |
| 753 |
if (!listener) { |
| 754 |
MWAW_DEBUG_MSG(("ZWrtText::sendHeaderFooter: can not find a listener\n")); |
| 755 |
return false; |
| 756 |
} |
| 757 |
auto const &zone = header ? m_state->m_header : m_state->m_footer; |
| 758 |
if (!zone.ok()) { |
| 759 |
MWAW_DEBUG_MSG(("ZWrtText::sendHeaderFooter: zone is not valid\n")); |
| 760 |
return false; |
| 761 |
} |
| 762 |
MWAWInputStreamPtr input = m_mainParser->rsrcInput(); |
| 763 |
input->seek(zone.m_pos.begin(), librevenge::RVNG_SEEK_SET); |
| 764 |
listener->setFont(zone.m_font.m_font); |
| 765 |
long endPos = zone.m_pos.end(); |
| 766 |
while (!input->isEnd()) { |
| 767 |
long actPos = input->tell(); |
| 768 |
if (actPos >= endPos) |
| 769 |
break; |
| 770 |
auto c = char(input->readULong(1)); |
| 771 |
switch (c) { |
| 772 |
case 0xa: |
| 773 |
listener->insertTab(); |
| 774 |
break; |
| 775 |
case 0xd: |
| 776 |
listener->insertEOL(); |
| 777 |
break; |
| 778 |
case '#': |
| 779 |
if (actPos+1 < endPos) { |
| 780 |
auto nextC = char(input->readULong(1)); |
| 781 |
bool done = true; |
| 782 |
switch (nextC) { |
| 783 |
case 'd': |
| 784 |
listener->insertField(MWAWField(MWAWField::Date)); |
| 785 |
break; |
| 786 |
case 'p': |
| 787 |
listener->insertField(MWAWField(MWAWField::PageNumber)); |
| 788 |
break; |
| 789 |
case 's': |
| 790 |
listener->insertUnicodeString(librevenge::RVNGString("#section#")); |
| 791 |
break; |
| 792 |
case 't': |
| 793 |
listener->insertField(MWAWField(MWAWField::Time)); |
| 794 |
break; |
| 795 |
case '#': |
| 796 |
listener->insertField(MWAWField(MWAWField::PageCount)); |
| 797 |
break; |
| 798 |
default: |
| 799 |
done=false; |
| 800 |
break; |
| 801 |
} |
| 802 |
if (done) |
| 803 |
break; |
| 804 |
} |
| 805 |
input->seek(actPos+1, librevenge::RVNG_SEEK_SET); |
| 806 |
MWAW_FALLTHROUGH; |
| 807 |
default: |
| 808 |
listener->insertCharacter(static_cast<unsigned char>(c), input, endPos); |
| 809 |
break; |
| 810 |
} |
| 811 |
} |
| 812 |
return true; |
| 813 |
} |
| 814 |
|
| 815 |
bool ZWrtText::readHFZone(MWAWEntry const &entry) |
| 816 |
{ |
| 817 |
if (!entry.valid()) { |
| 818 |
MWAW_DEBUG_MSG(("ZWrtText::readHFZone: the entry is bad\n")); |
| 819 |
return false; |
| 820 |
} |
| 821 |
|
| 822 |
if (entry.id()!=128) { |
| 823 |
MWAW_DEBUG_MSG(("ZWrtText::readHFZone: the entry id is odd\n")); |
| 824 |
} |
| 825 |
long pos = entry.begin(); |
| 826 |
MWAWInputStreamPtr input = m_mainParser->rsrcInput(); |
| 827 |
libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii(); |
| 828 |
libmwaw::DebugStream f; |
| 829 |
entry.setParsed(true); |
| 830 |
|
| 831 |
std::vector<ZWField> fields; |
| 832 |
if (!m_mainParser->getFieldList(entry, fields)) { |
| 833 |
MWAW_DEBUG_MSG(("ZWrtText::readHFZone: can not get fields list\n")); |
| 834 |
f << "Entries(" << entry.type() << ")[" << entry << "]:"; |
| 835 |
f << "###"; |
| 836 |
ascFile.addPos(pos-4); |
| 837 |
ascFile.addNote(f.str().c_str()); |
| 838 |
return false; |
| 839 |
} |
| 840 |
|
| 841 |
size_t numFields = fields.size(); |
| 842 |
if (numFields < 9) { |
| 843 |
MWAW_DEBUG_MSG(("ZWrtText::readHFZone: the fields list seems very short\n")); |
| 844 |
} |
| 845 |
std::string strVal; |
| 846 |
int intVal; |
| 847 |
bool boolVal; |
| 848 |
std::vector<int> intList; |
| 849 |
auto &zone = entry.type()=="HEAD" ? m_state->m_header : m_state->m_footer; |
| 850 |
auto &font = zone.m_font; |
| 851 |
uint32_t flags=0; |
| 852 |
for (size_t ff = 0; ff < numFields; ff++) { |
| 853 |
ZWField const &field = fields[ff]; |
| 854 |
bool done = false; |
| 855 |
switch (ff) { |
| 856 |
case 0: |
| 857 |
case 2: |
| 858 |
case 5: |
| 859 |
case 7: |
| 860 |
done = field.getBool(input, boolVal); |
| 861 |
if (!done) |
| 862 |
break; |
| 863 |
if (!boolVal) |
| 864 |
continue; |
| 865 |
switch (ff) { |
| 866 |
case 0: |
| 867 |
flags |= MWAWFont::boldBit; |
| 868 |
break; |
| 869 |
case 2: |
| 870 |
flags |= MWAWFont::italicBit; |
| 871 |
break; |
| 872 |
case 5: |
| 873 |
font.m_font.setUnderlineStyle(MWAWFont::Line::Simple); |
| 874 |
break; |
| 875 |
case 7: |
| 876 |
f << "addDelimiter,"; |
| 877 |
break; |
| 878 |
default: |
| 879 |
f << "f" << ff << "Set,"; |
| 880 |
break; |
| 881 |
} |
| 882 |
break; |
| 883 |
case 4: |
| 884 |
case 8: |
| 885 |
done = field.getInt(input, intVal); |
| 886 |
if (!done||!intVal) |
| 887 |
break; |
| 888 |
if (ff==4) |
| 889 |
font.m_font.setSize(float(intVal)); |
| 890 |
else |
| 891 |
f << "delimiterSize=" << intVal << ","; |
| 892 |
break; |
| 893 |
case 3: |
| 894 |
done = field.getString(input, strVal); |
| 895 |
if (!done||!strVal.length()) |
| 896 |
break; |
| 897 |
font.m_font.setId(m_parserState->m_fontConverter->getId(strVal)); |
| 898 |
break; |
| 899 |
case 6: |
| 900 |
done = field.getDebugString(input, strVal); |
| 901 |
if (!done||!strVal.length()) |
| 902 |
break; |
| 903 |
zone.m_pos = field.m_pos; |
| 904 |
f << "text=\"" << strVal << "\","; |
| 905 |
break; |
| 906 |
case 1: { |
| 907 |
done = field.getIntList(input, intList); |
| 908 |
if (!done|| intList.size() != 3) |
| 909 |
break; |
| 910 |
auto col = uint32_t((intList[0]<<16)|(intList[1]<<8)|intList[2]); |
| 911 |
if (col) |
| 912 |
font.m_font.setColor(MWAWColor(col)); |
| 913 |
break; |
| 914 |
} |
| 915 |
default: |
| 916 |
break; |
| 917 |
} |
| 918 |
if (done) |
| 919 |
continue; |
| 920 |
if (fields[ff].getDebugString(input, strVal)) |
| 921 |
f << "#f" << ff << "=\"" << strVal << "\","; |
| 922 |
else |
| 923 |
f << "#f" << ff << ","; |
| 924 |
} |
| 925 |
font.m_font.setFlags(flags); |
| 926 |
|
| 927 |
zone.m_extra=f.str(); |
| 928 |
f.str(""); |
| 929 |
f << "Entries(" << entry.type() << ")[" << entry << "]:" |
| 930 |
<< zone.getDebugString(m_parserState->m_fontConverter); |
| 931 |
|
| 932 |
ascFile.addPos(pos-4); |
| 933 |
ascFile.addNote(f.str().c_str()); |
| 934 |
return true; |
| 935 |
} |
| 936 |
|
| 937 |
//////////////////////////////////////////////////////////// |
| 938 |
// |
| 939 |
// Low level |
| 940 |
// |
| 941 |
//////////////////////////////////////////////////////////// |
| 942 |
|
| 943 |
void ZWrtText::flushExtra() |
| 944 |
{ |
| 945 |
} |
| 946 |
|
| 947 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |