| 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 "MWAWList.hxx" |
| 46 |
#include "MWAWParagraph.hxx" |
| 47 |
#include "MWAWPosition.hxx" |
| 48 |
#include "MWAWPrinter.hxx" |
| 49 |
#include "MWAWRSRCParser.hxx" |
| 50 |
#include "MWAWSubDocument.hxx" |
| 51 |
|
| 52 |
#include "ActaText.hxx" |
| 53 |
|
| 54 |
#include "ActaParser.hxx" |
| 55 |
|
| 56 |
/** Internal: the structures of a ActaParser */ |
| 57 |
namespace ActaParserInternal |
| 58 |
{ |
| 59 |
//////////////////////////////////////// |
| 60 |
//! Internal: class used to store a list type in ActaParser |
| 61 |
struct Label { |
| 62 |
//! constructor |
| 63 |
explicit Label(int type=-1) |
| 64 |
: m_type(type) { } |
| 65 |
//! operator<< |
| 66 |
friend std::ostream &operator<<(std::ostream &o, Label const &lbl) |
| 67 |
{ |
| 68 |
switch (lbl.m_type) { |
| 69 |
case 0: |
| 70 |
o << "noLabel,"; |
| 71 |
break; |
| 72 |
case 2: |
| 73 |
o << "checkbox,"; |
| 74 |
break; |
| 75 |
case 0xb: |
| 76 |
o << "decimal,"; // 1.0, |
| 77 |
break; |
| 78 |
case 0xc: |
| 79 |
o << "I A...,"; |
| 80 |
break; |
| 81 |
case 0xe: |
| 82 |
o << "custom,"; |
| 83 |
break; |
| 84 |
default: |
| 85 |
o << "#labelType=" << lbl.m_type << ","; |
| 86 |
break; |
| 87 |
} |
| 88 |
return o; |
| 89 |
} |
| 90 |
//! operator== |
| 91 |
bool operator==(Label const &lbl) const |
| 92 |
{ |
| 93 |
return m_type==lbl.m_type; |
| 94 |
} |
| 95 |
//! operator=! |
| 96 |
bool operator!=(Label const &lbl) const |
| 97 |
{ |
| 98 |
return !operator==(lbl); |
| 99 |
} |
| 100 |
//! the label type |
| 101 |
int m_type; |
| 102 |
}; |
| 103 |
|
| 104 |
//////////////////////////////////////// |
| 105 |
//! Internal: class used to store the printing preferences in ActaParser |
| 106 |
struct Printing { |
| 107 |
//! constructor |
| 108 |
Printing() |
| 109 |
: m_font() |
| 110 |
{ |
| 111 |
for (int &flag : m_flags) |
| 112 |
flag=0; |
| 113 |
} |
| 114 |
//! returns true if the header is empty |
| 115 |
bool isEmpty() const |
| 116 |
{ |
| 117 |
return (m_flags[1]&7)==0; |
| 118 |
} |
| 119 |
//! operator== |
| 120 |
bool operator==(Printing const &print) const |
| 121 |
{ |
| 122 |
if (m_font != print.m_font) |
| 123 |
return false; |
| 124 |
for (int i=0; i<2; i++) { |
| 125 |
if (m_flags[i]!=print.m_flags[i]) |
| 126 |
return false; |
| 127 |
} |
| 128 |
return true; |
| 129 |
} |
| 130 |
//! operator=! |
| 131 |
bool operator!=(Printing const &print) const |
| 132 |
{ |
| 133 |
return !operator==(print); |
| 134 |
} |
| 135 |
//! operator<< |
| 136 |
friend std::ostream &operator<<(std::ostream &o, Printing const &print) |
| 137 |
{ |
| 138 |
if (print.m_flags[0]==1) |
| 139 |
o << "useFooter,"; |
| 140 |
else if (print.m_flags[0]) |
| 141 |
o << "#fl0=" << print.m_flags[0] << ","; |
| 142 |
int flag = print.m_flags[1]; |
| 143 |
if (flag&1) |
| 144 |
o << "title,"; |
| 145 |
if (flag&2) |
| 146 |
o << "date,"; |
| 147 |
if (flag&4) |
| 148 |
o << "pagenumber,"; |
| 149 |
flag &= 0xFFF8; |
| 150 |
if (flag) |
| 151 |
o << "#flags=" << std::hex << flag << std::dec << ","; |
| 152 |
return o; |
| 153 |
} |
| 154 |
//! the font |
| 155 |
MWAWFont m_font; |
| 156 |
//! the flags |
| 157 |
int m_flags[2]; |
| 158 |
}; |
| 159 |
|
| 160 |
//////////////////////////////////////// |
| 161 |
//! Internal: class used to store the optional preferences in ActaParser |
| 162 |
struct Option { |
| 163 |
//! constructor |
| 164 |
explicit Option(int flags=0) |
| 165 |
: m_flags(flags) { } |
| 166 |
//! operator<< |
| 167 |
friend std::ostream &operator<<(std::ostream &o, Option const &opt) |
| 168 |
{ |
| 169 |
int flag = opt.m_flags; |
| 170 |
if (flag&0x1000) |
| 171 |
o << "speaker[dial],"; |
| 172 |
if (flag&0x4) |
| 173 |
o << "smart['\"],"; |
| 174 |
if (flag&0x8) |
| 175 |
o << "open[startup],"; |
| 176 |
// flag&0x10: always ? |
| 177 |
if (flag&0x20) |
| 178 |
o << "nolabel[picture],"; |
| 179 |
if (flag&0x40) |
| 180 |
o << "noframe[current],"; |
| 181 |
if (flag&0x80) |
| 182 |
o << "nolabel[clipboard],"; |
| 183 |
|
| 184 |
flag &= 0xEF13; |
| 185 |
if (flag) // find also flag&(10|400|4000|8000) |
| 186 |
o << "option[flags]=" << std::hex << flag << std::dec << ","; |
| 187 |
return o; |
| 188 |
} |
| 189 |
//! the flags |
| 190 |
int m_flags; |
| 191 |
}; |
| 192 |
|
| 193 |
//////////////////////////////////////// |
| 194 |
//! Internal: the state of a ActaParser |
| 195 |
struct State { |
| 196 |
//! constructor |
| 197 |
State() |
| 198 |
: m_printerPreferences() |
| 199 |
, m_title("") |
| 200 |
, m_label() |
| 201 |
, m_stringLabel("") |
| 202 |
, m_actPage(0) |
| 203 |
, m_numPages(0) |
| 204 |
, m_headerHeight(0) |
| 205 |
, m_footerHeight(0) |
| 206 |
{ |
| 207 |
} |
| 208 |
|
| 209 |
//! the printer preferences |
| 210 |
Printing m_printerPreferences; |
| 211 |
//! the title (if defined) |
| 212 |
std::string m_title; |
| 213 |
//! the list type |
| 214 |
Label m_label; |
| 215 |
//! the custom label (if defined) |
| 216 |
std::string m_stringLabel; |
| 217 |
int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */; |
| 218 |
|
| 219 |
int m_headerHeight /** the header height if known */, |
| 220 |
m_footerHeight /** the footer height if known */; |
| 221 |
}; |
| 222 |
|
| 223 |
//////////////////////////////////////// |
| 224 |
//! Internal: the subdocument of a ActaParser |
| 225 |
class SubDocument final : public MWAWSubDocument |
| 226 |
{ |
| 227 |
public: |
| 228 |
SubDocument(ActaParser &pars, MWAWInputStreamPtr const &input) : |
| 229 |
MWAWSubDocument(&pars, input, MWAWEntry()) {} |
| 230 |
|
| 231 |
//! destructor |
| 232 |
~SubDocument() final {} |
| 233 |
|
| 234 |
//! operator!= |
| 235 |
bool operator!=(MWAWSubDocument const &doc) const final |
| 236 |
{ |
| 237 |
if (MWAWSubDocument::operator!=(doc)) return true; |
| 238 |
auto const *sDoc = dynamic_cast<SubDocument const *>(&doc); |
| 239 |
if (!sDoc) return true; |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
//! the parser function |
| 244 |
void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final; |
| 245 |
|
| 246 |
protected: |
| 247 |
}; |
| 248 |
|
| 249 |
void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/) |
| 250 |
{ |
| 251 |
if (!listener.get()) { |
| 252 |
MWAW_DEBUG_MSG(("ActaParserInternal::SubDocument::parse: no listener\n")); |
| 253 |
return; |
| 254 |
} |
| 255 |
auto *parser=dynamic_cast<ActaParser *>(m_parser); |
| 256 |
if (!parser) { |
| 257 |
MWAW_DEBUG_MSG(("ActaParserInternal::SubDocument::parse: can not find main parser\n")); |
| 258 |
return; |
| 259 |
} |
| 260 |
parser->sendHeaderFooter(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
//////////////////////////////////////////////////////////// |
| 266 |
// constructor/destructor, ... |
| 267 |
//////////////////////////////////////////////////////////// |
| 268 |
ActaParser::ActaParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 269 |
: MWAWTextParser(input, rsrcParser, header) |
| 270 |
, m_state() |
| 271 |
, m_textParser() |
| 272 |
{ |
| 273 |
init(); |
| 274 |
} |
| 275 |
|
| 276 |
ActaParser::~ActaParser() |
| 277 |
{ |
| 278 |
} |
| 279 |
|
| 280 |
void ActaParser::init() |
| 281 |
{ |
| 282 |
resetTextListener(); |
| 283 |
setAsciiName("main-1"); |
| 284 |
|
| 285 |
m_state.reset(new ActaParserInternal::State); |
| 286 |
|
| 287 |
// reduce the margin (in case, the page is not defined) |
| 288 |
getPageSpan().setMargins(0.1); |
| 289 |
|
| 290 |
m_textParser.reset(new ActaText(*this)); |
| 291 |
} |
| 292 |
|
| 293 |
MWAWInputStreamPtr ActaParser::rsrcInput() |
| 294 |
{ |
| 295 |
return getRSRCParser()->getInput(); |
| 296 |
} |
| 297 |
|
| 298 |
libmwaw::DebugFile &ActaParser::rsrcAscii() |
| 299 |
{ |
| 300 |
return getRSRCParser()->ascii(); |
| 301 |
} |
| 302 |
|
| 303 |
//////////////////////////////////////////////////////////// |
| 304 |
// position and height |
| 305 |
//////////////////////////////////////////////////////////// |
| 306 |
MWAWVec2f ActaParser::getPageLeftTop() const |
| 307 |
{ |
| 308 |
return MWAWVec2f(float(getPageSpan().getMarginLeft()), |
| 309 |
float(getPageSpan().getMarginTop()+m_state->m_headerHeight/72.0)); |
| 310 |
} |
| 311 |
|
| 312 |
//////////////////////////////////////////////////////////// |
| 313 |
// interface with the text parser |
| 314 |
//////////////////////////////////////////////////////////// |
| 315 |
|
| 316 |
std::shared_ptr<MWAWList> ActaParser::getMainList() |
| 317 |
{ |
| 318 |
MWAWListLevel level; |
| 319 |
level.m_labelAfterSpace=0.05; |
| 320 |
std::vector<MWAWListLevel> levels; |
| 321 |
switch (m_state->m_label.m_type) { |
| 322 |
case 0: // none |
| 323 |
level.m_type=MWAWListLevel::NONE; |
| 324 |
levels.resize(10, level); |
| 325 |
break; |
| 326 |
case 2: // checkbox |
| 327 |
level.m_type=MWAWListLevel::BULLET; |
| 328 |
libmwaw::appendUnicode(0x2610, level.m_bullet); |
| 329 |
levels.resize(10, level); |
| 330 |
break; |
| 331 |
case 0xb: // 1.0 1.1 1.1.1 1.1.1.1 1.1.1.1.1 ... |
| 332 |
level.m_suffix = "."; |
| 333 |
level.m_type=MWAWListLevel::DECIMAL; |
| 334 |
for (int i=0; i < 10; i++) { |
| 335 |
level.m_numBeforeLabels=i; |
| 336 |
levels.push_back(level); |
| 337 |
} |
| 338 |
break; |
| 339 |
case 0xc: // I. A. 1. a. i. [(1). (a). ]* |
| 340 |
level.m_suffix = "."; |
| 341 |
level.m_type=MWAWListLevel::UPPER_ROMAN; |
| 342 |
levels.push_back(level); |
| 343 |
level.m_type=MWAWListLevel::UPPER_ALPHA; |
| 344 |
levels.push_back(level); |
| 345 |
level.m_type=MWAWListLevel::DECIMAL; |
| 346 |
levels.push_back(level); |
| 347 |
level.m_type=MWAWListLevel::LOWER_ALPHA; |
| 348 |
levels.push_back(level); |
| 349 |
level.m_type=MWAWListLevel::LOWER_ROMAN; |
| 350 |
levels.push_back(level); |
| 351 |
level.m_prefix = "("; |
| 352 |
level.m_suffix = ")."; |
| 353 |
for (int i=0; i < 4; i++) { |
| 354 |
level.m_type=MWAWListLevel::DECIMAL; |
| 355 |
levels.push_back(level); |
| 356 |
level.m_type=MWAWListLevel::LOWER_ALPHA; |
| 357 |
levels.push_back(level); |
| 358 |
} |
| 359 |
break; |
| 360 |
default: // ok, switch to custom or by default bullet |
| 361 |
case 0xe: { //custom |
| 362 |
level.m_type=MWAWListLevel::BULLET; |
| 363 |
libmwaw::appendUnicode(0x2022, level.m_bullet); |
| 364 |
auto fontConvert=getFontConverter(); |
| 365 |
if (!fontConvert) { |
| 366 |
MWAW_DEBUG_MSG(("ActaParser::getMainList: can not find the listener\n")); |
| 367 |
} |
| 368 |
else { |
| 369 |
for (size_t i=0; i<m_state->m_stringLabel.size(); ++i) { // checkme |
| 370 |
int unicode=fontConvert->unicode(3, static_cast<unsigned char>(m_state->m_stringLabel[1])); |
| 371 |
level.m_bullet=""; |
| 372 |
libmwaw::appendUnicode((unicode > 0) ? uint32_t(unicode):0x2022, level.m_bullet); |
| 373 |
levels.push_back(level); |
| 374 |
} |
| 375 |
} |
| 376 |
while (levels.size() < 10) |
| 377 |
levels.push_back(level); |
| 378 |
break; |
| 379 |
} |
| 380 |
} |
| 381 |
std::shared_ptr<MWAWList> list; |
| 382 |
MWAWListManagerPtr listManager=getParserState()->m_listManager; |
| 383 |
if (!listManager) { |
| 384 |
MWAW_DEBUG_MSG(("ActaParser::getMainList: can not find the list manager\n")); |
| 385 |
return list; |
| 386 |
} |
| 387 |
|
| 388 |
for (size_t s=0; s < levels.size(); s++) { |
| 389 |
list = listManager->getNewList(list, int(s+1), levels[s]); |
| 390 |
if (!list) break; |
| 391 |
} |
| 392 |
return list; |
| 393 |
} |
| 394 |
//////////////////////////////////////////////////////////// |
| 395 |
// new page |
| 396 |
//////////////////////////////////////////////////////////// |
| 397 |
void ActaParser::newPage(int number) |
| 398 |
{ |
| 399 |
if (number <= m_state->m_actPage || number > m_state->m_numPages) |
| 400 |
return; |
| 401 |
|
| 402 |
while (m_state->m_actPage < number) { |
| 403 |
m_state->m_actPage++; |
| 404 |
if (!getTextListener() || m_state->m_actPage == 1) |
| 405 |
continue; |
| 406 |
getTextListener()->insertBreak(MWAWTextListener::PageBreak); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
//////////////////////////////////////////////////////////// |
| 411 |
// the parser |
| 412 |
//////////////////////////////////////////////////////////// |
| 413 |
void ActaParser::parse(librevenge::RVNGTextInterface *docInterface) |
| 414 |
{ |
| 415 |
if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 416 |
bool ok = false; |
| 417 |
try { |
| 418 |
// create the asciiFile |
| 419 |
ascii().setStream(getInput()); |
| 420 |
ascii().open(asciiName()); |
| 421 |
|
| 422 |
checkHeader(nullptr); |
| 423 |
ok = createZones(); |
| 424 |
if (ok) { |
| 425 |
createDocument(docInterface); |
| 426 |
m_textParser->sendMainText(); |
| 427 |
} |
| 428 |
ascii().reset(); |
| 429 |
} |
| 430 |
catch (...) { |
| 431 |
MWAW_DEBUG_MSG(("ActaParser::parse: exception catched when parsing\n")); |
| 432 |
ok = false; |
| 433 |
} |
| 434 |
|
| 435 |
resetTextListener(); |
| 436 |
if (!ok) throw(libmwaw::ParseException()); |
| 437 |
} |
| 438 |
|
| 439 |
//////////////////////////////////////////////////////////// |
| 440 |
// create the document |
| 441 |
//////////////////////////////////////////////////////////// |
| 442 |
void ActaParser::createDocument(librevenge::RVNGTextInterface *documentInterface) |
| 443 |
{ |
| 444 |
if (!documentInterface) return; |
| 445 |
if (getTextListener()) { |
| 446 |
MWAW_DEBUG_MSG(("ActaParser::createDocument: listener already exist\n")); |
| 447 |
return; |
| 448 |
} |
| 449 |
|
| 450 |
// update the page |
| 451 |
m_state->m_actPage = 0; |
| 452 |
|
| 453 |
// create the page list |
| 454 |
int numPages = 1; |
| 455 |
if (m_textParser->numPages() > numPages) |
| 456 |
numPages = m_textParser->numPages(); |
| 457 |
m_state->m_numPages = numPages; |
| 458 |
|
| 459 |
MWAWPageSpan ps(getPageSpan()); |
| 460 |
ps.setPageSpan(m_state->m_numPages+1); |
| 461 |
if (!m_state->m_printerPreferences.isEmpty()) { |
| 462 |
MWAWHeaderFooter hF(m_state->m_printerPreferences.m_flags[0]!=1 ? |
| 463 |
MWAWHeaderFooter::HEADER : MWAWHeaderFooter::FOOTER, |
| 464 |
MWAWHeaderFooter::ALL); |
| 465 |
hF.m_subDocument.reset(new ActaParserInternal::SubDocument(*this, getInput())); |
| 466 |
ps.setHeaderFooter(hF); |
| 467 |
} |
| 468 |
std::vector<MWAWPageSpan> pageList(1,ps); |
| 469 |
MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface)); |
| 470 |
setTextListener(listen); |
| 471 |
listen->startDocument(); |
| 472 |
} |
| 473 |
|
| 474 |
void ActaParser::sendHeaderFooter() |
| 475 |
{ |
| 476 |
MWAWTextListenerPtr listener=getTextListener(); |
| 477 |
if (!listener) { |
| 478 |
MWAW_DEBUG_MSG(("ActaParser::sendHeaderFooter: can not find the listener\n")); |
| 479 |
return; |
| 480 |
} |
| 481 |
auto const &print=m_state->m_printerPreferences; |
| 482 |
MWAWParagraph para; |
| 483 |
para.m_justify=MWAWParagraph::JustificationCenter; |
| 484 |
listener->setParagraph(para); |
| 485 |
listener->setFont(print.m_font); |
| 486 |
bool printDone=false; |
| 487 |
for (int i=0, wh=1; i < 3; i++, wh*=2) { |
| 488 |
if ((print.m_flags[1]&wh)==0) |
| 489 |
continue; |
| 490 |
if (printDone) |
| 491 |
listener->insertChar(' '); |
| 492 |
switch (i) { |
| 493 |
case 0: |
| 494 |
if (!m_state->m_title.length()) { |
| 495 |
listener->insertField(MWAWField(MWAWField::Title)); |
| 496 |
break; |
| 497 |
} |
| 498 |
for (auto c : m_state->m_title) |
| 499 |
listener->insertCharacter(static_cast<unsigned char>(c)); |
| 500 |
break; |
| 501 |
case 1: { |
| 502 |
MWAWField field(MWAWField::Date); |
| 503 |
field.m_DTFormat="%b %d, %Y"; |
| 504 |
listener->insertField(field); |
| 505 |
break; |
| 506 |
} |
| 507 |
case 2: |
| 508 |
listener->insertField(MWAWField(MWAWField::PageNumber)); |
| 509 |
break; |
| 510 |
default: |
| 511 |
MWAW_DEBUG_MSG(("ActaParser::sendHeaderFooter: unexpected step\n")); |
| 512 |
break; |
| 513 |
} |
| 514 |
printDone=true; |
| 515 |
} |
| 516 |
if (!printDone) |
| 517 |
listener->insertChar(' '); |
| 518 |
} |
| 519 |
|
| 520 |
//////////////////////////////////////////////////////////// |
| 521 |
// |
| 522 |
// Intermediate level |
| 523 |
// |
| 524 |
//////////////////////////////////////////////////////////// |
| 525 |
bool ActaParser::createZones() |
| 526 |
{ |
| 527 |
MWAWInputStreamPtr input = getInput(); |
| 528 |
readRSRCZones(); |
| 529 |
if (version()>=3) { |
| 530 |
input->setReadInverted(true); |
| 531 |
if (!readEndDataV3()) { |
| 532 |
ascii().addPos(input->tell()); |
| 533 |
ascii().addNote("Entries(Loose)"); |
| 534 |
} |
| 535 |
input->setReadInverted(false); |
| 536 |
} |
| 537 |
return m_textParser->createZones(); |
| 538 |
} |
| 539 |
|
| 540 |
bool ActaParser::readRSRCZones() |
| 541 |
{ |
| 542 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 543 |
if (!rsrcParser) |
| 544 |
return true; |
| 545 |
if (version() < 3) { // never seens so, better ignore |
| 546 |
MWAW_DEBUG_MSG(("ActaParser::readRSRCZones: find a resource fork in v1-v2!!!\n")); |
| 547 |
return false; |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
auto const &entryMap = rsrcParser->getEntriesMap(); |
| 552 |
|
| 553 |
// STR:0 -> title name, STR:1 custom label |
| 554 |
auto it = entryMap.lower_bound("STR "); |
| 555 |
while (it != entryMap.end()) { |
| 556 |
if (it->first != "STR ") |
| 557 |
break; |
| 558 |
MWAWEntry const &entry = it++->second; |
| 559 |
entry.setParsed(true); |
| 560 |
std::string str(""); |
| 561 |
if (!rsrcParser->parseSTR(entry,str) || str.length()==0) |
| 562 |
continue; |
| 563 |
switch (entry.id()) { |
| 564 |
case 0: |
| 565 |
m_state->m_title=str; |
| 566 |
break; |
| 567 |
case 1: |
| 568 |
m_state->m_stringLabel=str; |
| 569 |
break; |
| 570 |
default: |
| 571 |
MWAW_DEBUG_MSG(("ActaParser::readRSRCZones: find unexpected STR:%d\n", entry.id())); |
| 572 |
break; |
| 573 |
} |
| 574 |
} |
| 575 |
// the 0 zone |
| 576 |
char const *zNames[] = {"PSET", "WSIZ", "LABL", "QOPT", "QHDR"}; |
| 577 |
for (int z = 0; z < 5; z++) { |
| 578 |
it = entryMap.lower_bound(zNames[z]); |
| 579 |
while (it != entryMap.end()) { |
| 580 |
if (it->first != zNames[z]) |
| 581 |
break; |
| 582 |
MWAWEntry const &entry = it++->second; |
| 583 |
switch (z) { |
| 584 |
case 0: |
| 585 |
readPrintInfo(entry); |
| 586 |
break; |
| 587 |
case 1: |
| 588 |
readWindowPos(entry); |
| 589 |
break; |
| 590 |
case 2: |
| 591 |
readLabel(entry); |
| 592 |
break; |
| 593 |
case 3: |
| 594 |
readOption(entry); |
| 595 |
break; |
| 596 |
case 4: |
| 597 |
readHFProperties(entry); |
| 598 |
break; |
| 599 |
default: |
| 600 |
break; |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
return true; |
| 605 |
} |
| 606 |
|
| 607 |
bool ActaParser::readEndDataV3() |
| 608 |
{ |
| 609 |
if (version()<3) |
| 610 |
return true; |
| 611 |
MWAWInputStreamPtr input = getInput(); |
| 612 |
libmwaw::DebugStream f; |
| 613 |
input->seek(-8, librevenge::RVNG_SEEK_END); |
| 614 |
ascii().addPos(input->tell()); |
| 615 |
auto pos=static_cast<long>(input->readULong(4)); |
| 616 |
if (pos < 18 || !input->checkPosition(pos)) { |
| 617 |
MWAW_DEBUG_MSG(("ActaParser::readEndDataV3: oops begin of ressource is bad\n")); |
| 618 |
ascii().addNote("###"); |
| 619 |
return false; |
| 620 |
} |
| 621 |
ascii().addNote("_"); |
| 622 |
|
| 623 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 624 |
|
| 625 |
f << "Entries(QOpt):"; |
| 626 |
ActaParserInternal::Label lbl(static_cast<int>(input->readLong(1))); |
| 627 |
f << lbl << ","; |
| 628 |
if (m_state->m_label!=lbl) { |
| 629 |
if (m_state->m_label.m_type) { |
| 630 |
MWAW_DEBUG_MSG(("ActaParser::readEndDataV3: oops the label seems set and different\n")); |
| 631 |
} |
| 632 |
else |
| 633 |
m_state->m_label = lbl; |
| 634 |
} |
| 635 |
auto val=static_cast<int>(input->readLong(1)); |
| 636 |
if (val != 1) // always 1 |
| 637 |
f << "f0=" << val << ","; |
| 638 |
ActaParserInternal::Option opt(static_cast<int>(input->readULong(2))); |
| 639 |
f << opt; |
| 640 |
// string: name, followed by abbreviation |
| 641 |
for (int i = 0; i < 2; i++) { |
| 642 |
long fPos=input->tell(); |
| 643 |
auto fSz = static_cast<int>(input->readULong(1)); |
| 644 |
if (!input->checkPosition(fPos+fSz+1)) { |
| 645 |
MWAW_DEBUG_MSG(("ActaText::readEndDataV3: can not read following string\n")); |
| 646 |
f << "###"; |
| 647 |
ascii().addPos(pos); |
| 648 |
ascii().addNote(f.str().c_str()); |
| 649 |
return true; |
| 650 |
} |
| 651 |
if (!fSz) continue; |
| 652 |
std::string str(""); |
| 653 |
for (int s=0; s < fSz; s++) |
| 654 |
str+=static_cast<char>(input->readULong(1)); |
| 655 |
if (!str.length()) |
| 656 |
continue; |
| 657 |
f << "str" << i << "=" << str << ","; |
| 658 |
std::string &which=(i==0) ? m_state->m_title : m_state->m_stringLabel; |
| 659 |
if (which.length()) { |
| 660 |
if (which != str) { |
| 661 |
MWAW_DEBUG_MSG(("ActaText::readEndDataV3: find a different string\n")); |
| 662 |
f << "###"; |
| 663 |
} |
| 664 |
continue; |
| 665 |
} |
| 666 |
which = str; |
| 667 |
} |
| 668 |
val=static_cast<int>(input->readLong(1)); |
| 669 |
if (val) // always 0 or a another string |
| 670 |
f << "f1=" << val << ","; |
| 671 |
|
| 672 |
// from here unknown: maybe related to the printer definition... |
| 673 |
float dim[2]; |
| 674 |
for (float &i : dim) // very unsure... |
| 675 |
i = float(input->readULong(2))/256.f; |
| 676 |
f << "dim?=" << dim[1] << "x" << dim[0] << ","; |
| 677 |
ascii().addPos(pos); |
| 678 |
ascii().addNote(f.str().c_str()); |
| 679 |
|
| 680 |
pos = input->tell(); |
| 681 |
f.str(""); |
| 682 |
f << "Entries(Loose):"; |
| 683 |
auto type = static_cast<int>(input->readULong(1)); |
| 684 |
f << "type?=" << std::hex << type << ","; |
| 685 |
ascii().addPos(pos); |
| 686 |
ascii().addNote("Entries(Loose)"); |
| 687 |
|
| 688 |
if (type!=255 || !input->checkPosition(pos+200)) |
| 689 |
return true; |
| 690 |
|
| 691 |
input->seek(pos+198, librevenge::RVNG_SEEK_SET); |
| 692 |
pos = input->tell(); |
| 693 |
auto N=static_cast<int>(input->readLong(2)); |
| 694 |
if (N<0 || !input->checkPosition(pos+2+34*N)) |
| 695 |
return true; |
| 696 |
f.str(""); |
| 697 |
f << "Entries(Font):N=" << N << ","; |
| 698 |
ascii().addPos(pos); |
| 699 |
ascii().addNote(f.str().c_str()); |
| 700 |
for (int i= 0; i < N; i++) { |
| 701 |
pos=input->tell(); |
| 702 |
f.str(""); |
| 703 |
f << "Font-" << i << ":"; |
| 704 |
std::string str(""); |
| 705 |
while (input->tell()<pos+32) { |
| 706 |
auto c=static_cast<char>(input->readULong(1)); |
| 707 |
if (!c) break; |
| 708 |
str+=c; |
| 709 |
} |
| 710 |
if (str.length()) |
| 711 |
f << str << ","; |
| 712 |
input->seek(pos+32, librevenge::RVNG_SEEK_SET); |
| 713 |
for (int j = 0; j < 2; j++) { |
| 714 |
val = static_cast<int>(input->readLong(1)); |
| 715 |
if (val) f << "f" << j << "=" << std::hex << val << std::dec << ","; |
| 716 |
} |
| 717 |
ascii().addPos(pos); |
| 718 |
ascii().addNote(f.str().c_str()); |
| 719 |
} |
| 720 |
|
| 721 |
pos = input->tell(); |
| 722 |
ascii().addPos(pos); |
| 723 |
ascii().addNote("Entries(Loose)[II]"); |
| 724 |
return true; |
| 725 |
} |
| 726 |
|
| 727 |
//////////////////////////////////////////////////////////// |
| 728 |
// read the print info |
| 729 |
//////////////////////////////////////////////////////////// |
| 730 |
bool ActaParser::readPrintInfo(MWAWEntry const &entry) |
| 731 |
{ |
| 732 |
if (!entry.valid() || entry.length() != 120) { |
| 733 |
MWAW_DEBUG_MSG(("ActaParser::readPrintInfo: the entry is bad\n")); |
| 734 |
return false; |
| 735 |
} |
| 736 |
|
| 737 |
long pos = entry.begin(); |
| 738 |
MWAWInputStreamPtr input = rsrcInput(); |
| 739 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 740 |
libmwaw::DebugStream f; |
| 741 |
|
| 742 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 743 |
libmwaw::PrinterInfo info; |
| 744 |
if (!info.read(input)) return false; |
| 745 |
f << "Entries(PrintInfo):"<< info; |
| 746 |
entry.setParsed(true); |
| 747 |
|
| 748 |
MWAWVec2i paperSize = info.paper().size(); |
| 749 |
MWAWVec2i pageSize = info.page().size(); |
| 750 |
if (pageSize.x() <= 0 || pageSize.y() <= 0 || |
| 751 |
paperSize.x() <= 0 || paperSize.y() <= 0) return false; |
| 752 |
|
| 753 |
// define margin from print info |
| 754 |
MWAWVec2i lTopMargin= -1 * info.paper().pos(0); |
| 755 |
MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1); |
| 756 |
|
| 757 |
// move margin left | top |
| 758 |
int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0; |
| 759 |
int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0; |
| 760 |
lTopMargin -= MWAWVec2i(decalX, decalY); |
| 761 |
rBotMargin += MWAWVec2i(decalX, decalY); |
| 762 |
|
| 763 |
// decrease right | bottom |
| 764 |
int rightMarg = rBotMargin.x() -10; |
| 765 |
if (rightMarg < 0) rightMarg=0; |
| 766 |
int botMarg = rBotMargin.y() -50; |
| 767 |
if (botMarg < 0) botMarg=0; |
| 768 |
|
| 769 |
getPageSpan().setMarginTop(lTopMargin.y()/72.0); |
| 770 |
getPageSpan().setMarginBottom(botMarg/72.0); |
| 771 |
getPageSpan().setMarginLeft(lTopMargin.x()/72.0); |
| 772 |
getPageSpan().setMarginRight(rightMarg/72.0); |
| 773 |
getPageSpan().setFormLength(paperSize.y()/72.); |
| 774 |
getPageSpan().setFormWidth(paperSize.x()/72.); |
| 775 |
|
| 776 |
ascFile.addPos(pos-4); |
| 777 |
ascFile.addNote(f.str().c_str()); |
| 778 |
return true; |
| 779 |
} |
| 780 |
|
| 781 |
//////////////////////////////////////////////////////////// |
| 782 |
// read the windows positon info |
| 783 |
//////////////////////////////////////////////////////////// |
| 784 |
bool ActaParser::readWindowPos(MWAWEntry const &entry) |
| 785 |
{ |
| 786 |
if (!entry.valid() || entry.length() != 8) { |
| 787 |
MWAW_DEBUG_MSG(("ActaParser::readWindowPos: the entry is bad\n")); |
| 788 |
return false; |
| 789 |
} |
| 790 |
|
| 791 |
long pos = entry.begin(); |
| 792 |
MWAWInputStreamPtr input = rsrcInput(); |
| 793 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 794 |
libmwaw::DebugStream f; |
| 795 |
|
| 796 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 797 |
f << "Entries(WindowPos):"; |
| 798 |
entry.setParsed(true); |
| 799 |
int dim[4]; |
| 800 |
for (int &i : dim) |
| 801 |
i = static_cast<int>(input->readLong(2)); |
| 802 |
f << "pos=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ","; |
| 803 |
ascFile.addPos(pos-4); |
| 804 |
ascFile.addNote(f.str().c_str()); |
| 805 |
return true; |
| 806 |
} |
| 807 |
|
| 808 |
//////////////////////////////////////////////////////////// |
| 809 |
// small resource fork |
| 810 |
//////////////////////////////////////////////////////////// |
| 811 |
|
| 812 |
// label kind |
| 813 |
bool ActaParser::readLabel(MWAWEntry const &entry) |
| 814 |
{ |
| 815 |
if (!entry.valid() || entry.length() != 2) { |
| 816 |
MWAW_DEBUG_MSG(("ActaParser::readLabel: the entry is bad\n")); |
| 817 |
return false; |
| 818 |
} |
| 819 |
|
| 820 |
long pos = entry.begin(); |
| 821 |
MWAWInputStreamPtr input = rsrcInput(); |
| 822 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 823 |
libmwaw::DebugStream f; |
| 824 |
|
| 825 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 826 |
f << "Entries(Label):"; |
| 827 |
entry.setParsed(true); |
| 828 |
m_state->m_label.m_type=static_cast<int>(input->readLong(2)); |
| 829 |
f << m_state->m_label; |
| 830 |
ascFile.addPos(pos-4); |
| 831 |
ascFile.addNote(f.str().c_str()); |
| 832 |
return true; |
| 833 |
} |
| 834 |
|
| 835 |
// header/footer properties |
| 836 |
bool ActaParser::readHFProperties(MWAWEntry const &entry) |
| 837 |
{ |
| 838 |
if (!entry.valid() || entry.length() != 20) { |
| 839 |
MWAW_DEBUG_MSG(("ActaParser::readHFProperties: the entry is bad\n")); |
| 840 |
return false; |
| 841 |
} |
| 842 |
|
| 843 |
long pos = entry.begin(); |
| 844 |
MWAWInputStreamPtr input = rsrcInput(); |
| 845 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 846 |
libmwaw::DebugStream f; |
| 847 |
|
| 848 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 849 |
f << "Entries(QHDR):"; |
| 850 |
entry.setParsed(true); |
| 851 |
for (int st = 0; st < 2; st++) { |
| 852 |
if (st==0) |
| 853 |
f << "headerFooter=["; |
| 854 |
else |
| 855 |
f << "unknown=["; |
| 856 |
ActaParserInternal::Printing print; |
| 857 |
print.m_font.setId(static_cast<int>(input->readLong(2))); |
| 858 |
print.m_font.setSize(static_cast<float>(input->readLong(2))); |
| 859 |
auto flag=static_cast<int>(input->readLong(2)); |
| 860 |
uint32_t flags = 0; |
| 861 |
if (flag&0x1) flags |= MWAWFont::boldBit; |
| 862 |
if (flag&0x2) flags |= MWAWFont::italicBit; |
| 863 |
if (flag&0x4) print.m_font.setUnderlineStyle(MWAWFont::Line::Simple); |
| 864 |
if (flag&0x8) flags |= MWAWFont::embossBit; |
| 865 |
if (flag&0x10) flags |= MWAWFont::shadowBit; |
| 866 |
print.m_font.setFlags(flags); |
| 867 |
#ifdef DEBUG |
| 868 |
f << "font=[" << print.m_font.getDebugString(getFontConverter()) << "],"; |
| 869 |
#endif |
| 870 |
flag &= 0xE0; |
| 871 |
if (flag) |
| 872 |
f << "#font[flags]=" << std::hex << flags << std::dec << ","; |
| 873 |
for (int &i : print.m_flags) |
| 874 |
i = static_cast<int>(input->readULong(2)); |
| 875 |
f << print << "],"; |
| 876 |
if (st==0) |
| 877 |
m_state->m_printerPreferences = print; |
| 878 |
} |
| 879 |
|
| 880 |
ascFile.addPos(pos-4); |
| 881 |
ascFile.addNote(f.str().c_str()); |
| 882 |
return true; |
| 883 |
} |
| 884 |
|
| 885 |
// Option : small modificator change |
| 886 |
bool ActaParser::readOption(MWAWEntry const &entry) |
| 887 |
{ |
| 888 |
if (!entry.valid() || entry.length() != 2) { |
| 889 |
MWAW_DEBUG_MSG(("ActaParser::readOption: the entry is bad\n")); |
| 890 |
return false; |
| 891 |
} |
| 892 |
|
| 893 |
long pos = entry.begin(); |
| 894 |
MWAWInputStreamPtr input = rsrcInput(); |
| 895 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 896 |
libmwaw::DebugStream f; |
| 897 |
|
| 898 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 899 |
f << "Entries(Option):"; |
| 900 |
entry.setParsed(true); |
| 901 |
ActaParserInternal::Option opt(static_cast<int>(input->readULong(2))); |
| 902 |
f << opt; |
| 903 |
ascFile.addPos(pos-4); |
| 904 |
ascFile.addNote(f.str().c_str()); |
| 905 |
return true; |
| 906 |
} |
| 907 |
//////////////////////////////////////////////////////////// |
| 908 |
// |
| 909 |
// Low level |
| 910 |
// |
| 911 |
//////////////////////////////////////////////////////////// |
| 912 |
|
| 913 |
//////////////////////////////////////////////////////////// |
| 914 |
// read the header |
| 915 |
//////////////////////////////////////////////////////////// |
| 916 |
bool ActaParser::checkHeader(MWAWHeader *header, bool strict) |
| 917 |
{ |
| 918 |
*m_state = ActaParserInternal::State(); |
| 919 |
MWAWInputStreamPtr input = getInput(); |
| 920 |
if (!input || !input->hasDataFork() || !input->checkPosition(22)) |
| 921 |
return false; |
| 922 |
|
| 923 |
libmwaw::DebugStream f; |
| 924 |
f << "FileHeader:"; |
| 925 |
|
| 926 |
// first check end of file |
| 927 |
input->seek(-4,librevenge::RVNG_SEEK_END); |
| 928 |
int last[2]; |
| 929 |
for (int &i : last) |
| 930 |
i=static_cast<int>(input->readLong(2)); |
| 931 |
int vers=-1; |
| 932 |
if (last[0]==0x4E4C && last[1]==0x544F) |
| 933 |
vers=3; |
| 934 |
else if (last[1]==0) |
| 935 |
vers=1; |
| 936 |
if (vers<=0) |
| 937 |
return false; |
| 938 |
setVersion(vers); |
| 939 |
|
| 940 |
// ok, now check the beginning of the file |
| 941 |
int val; |
| 942 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 943 |
if (vers==3) { |
| 944 |
val=static_cast<int>(input->readULong(2)); |
| 945 |
if (val!=3) { |
| 946 |
if (strict) return false; |
| 947 |
if (val < 1 || val > 4) |
| 948 |
return false; |
| 949 |
f << "#vers=" << val << ","; |
| 950 |
MWAW_DEBUG_MSG(("ActaParser::checkHeader: find unexpected version: %d\n", val)); |
| 951 |
} |
| 952 |
} |
| 953 |
val = static_cast<int>(input->readULong(2)); // depth ( first topic must have depth=1) |
| 954 |
if (val != 1) |
| 955 |
return false; |
| 956 |
val = static_cast<int>(input->readULong(2)); // type |
| 957 |
if (val != 1 && val !=2) |
| 958 |
return false; |
| 959 |
|
| 960 |
// check that the first text size is valid |
| 961 |
input->seek(vers==1 ? 18 : 20, librevenge::RVNG_SEEK_SET); |
| 962 |
auto sz=static_cast<long>(input->readULong(4)); |
| 963 |
if (!input->checkPosition(input->tell()+sz)) |
| 964 |
return false; |
| 965 |
|
| 966 |
if (header) |
| 967 |
header->reset(MWAWDocument::MWAW_T_ACTA, vers); |
| 968 |
if (vers >= 3) { |
| 969 |
ascii().addPos(0); |
| 970 |
ascii().addNote(f.str().c_str()); |
| 971 |
} |
| 972 |
return true; |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |