| 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 "BeagleWksStructManager.hxx" |
| 53 |
#include "BeagleWksText.hxx" |
| 54 |
|
| 55 |
#include "BeagleWksParser.hxx" |
| 56 |
|
| 57 |
/** Internal: the structures of a BeagleWksParser */ |
| 58 |
namespace BeagleWksParserInternal |
| 59 |
{ |
| 60 |
//////////////////////////////////////// |
| 61 |
//! Internal: the state of a BeagleWksParser |
| 62 |
struct State { |
| 63 |
//! constructor |
| 64 |
State() |
| 65 |
: m_textBegin(0) |
| 66 |
, m_typeEntryMap() |
| 67 |
, m_actPage(0) |
| 68 |
, m_numPages(0) |
| 69 |
, m_headerHeight(0) |
| 70 |
, m_footerHeight(0) |
| 71 |
{ |
| 72 |
} |
| 73 |
|
| 74 |
/** the text begin position */ |
| 75 |
long m_textBegin; |
| 76 |
/** the type entry map */ |
| 77 |
std::multimap<std::string, MWAWEntry> m_typeEntryMap; |
| 78 |
int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */; |
| 79 |
|
| 80 |
int m_headerHeight /** the header height if known */, |
| 81 |
m_footerHeight /** the footer height if known */; |
| 82 |
}; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
//////////////////////////////////////////////////////////// |
| 87 |
// constructor/destructor, ... |
| 88 |
//////////////////////////////////////////////////////////// |
| 89 |
BeagleWksParser::BeagleWksParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 90 |
: MWAWTextParser(input, rsrcParser, header) |
| 91 |
, m_state() |
| 92 |
, m_structureManager() |
| 93 |
, m_textParser() |
| 94 |
{ |
| 95 |
init(); |
| 96 |
} |
| 97 |
|
| 98 |
BeagleWksParser::~BeagleWksParser() |
| 99 |
{ |
| 100 |
} |
| 101 |
|
| 102 |
void BeagleWksParser::init() |
| 103 |
{ |
| 104 |
resetTextListener(); |
| 105 |
setAsciiName("main-1"); |
| 106 |
|
| 107 |
m_state.reset(new BeagleWksParserInternal::State); |
| 108 |
|
| 109 |
// reduce the margin (in case, the page is not defined) |
| 110 |
getPageSpan().setMargins(0.1); |
| 111 |
|
| 112 |
m_structureManager.reset(new BeagleWksStructManager(getParserState())); |
| 113 |
m_textParser.reset(new BeagleWksText(*this)); |
| 114 |
} |
| 115 |
|
| 116 |
MWAWInputStreamPtr BeagleWksParser::rsrcInput() |
| 117 |
{ |
| 118 |
return getRSRCParser()->getInput(); |
| 119 |
} |
| 120 |
|
| 121 |
libmwaw::DebugFile &BeagleWksParser::rsrcAscii() |
| 122 |
{ |
| 123 |
return getRSRCParser()->ascii(); |
| 124 |
} |
| 125 |
|
| 126 |
//////////////////////////////////////////////////////////// |
| 127 |
// position and height |
| 128 |
//////////////////////////////////////////////////////////// |
| 129 |
MWAWVec2f BeagleWksParser::getPageLeftTop() const |
| 130 |
{ |
| 131 |
return MWAWVec2f(float(getPageSpan().getMarginLeft()), |
| 132 |
float(getPageSpan().getMarginTop()+m_state->m_headerHeight/72.0)); |
| 133 |
} |
| 134 |
|
| 135 |
//////////////////////////////////////////////////////////// |
| 136 |
// interface with the text parser |
| 137 |
//////////////////////////////////////////////////////////// |
| 138 |
bool BeagleWksParser::sendFrame(int pId) |
| 139 |
{ |
| 140 |
BeagleWksStructManager::Frame frame; |
| 141 |
if (!m_structureManager->getFrame(pId, frame)) return false; |
| 142 |
if (!frame.m_charAnchor) { |
| 143 |
MWAW_DEBUG_MSG(("BeagleWksParser::sendFrame: the frame is bad for id=%d\n",pId)); |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
return sendFrame(frame); |
| 148 |
} |
| 149 |
|
| 150 |
//////////////////////////////////////////////////////////// |
| 151 |
// new page |
| 152 |
//////////////////////////////////////////////////////////// |
| 153 |
void BeagleWksParser::newPage(int number) |
| 154 |
{ |
| 155 |
if (number <= m_state->m_actPage || number > m_state->m_numPages) |
| 156 |
return; |
| 157 |
|
| 158 |
while (m_state->m_actPage < number) { |
| 159 |
m_state->m_actPage++; |
| 160 |
if (!getTextListener() || m_state->m_actPage == 1) |
| 161 |
continue; |
| 162 |
getTextListener()->insertBreak(MWAWTextListener::PageBreak); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
//////////////////////////////////////////////////////////// |
| 167 |
// the parser |
| 168 |
//////////////////////////////////////////////////////////// |
| 169 |
void BeagleWksParser::parse(librevenge::RVNGTextInterface *docInterface) |
| 170 |
{ |
| 171 |
if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 172 |
bool ok = false; |
| 173 |
try { |
| 174 |
// create the asciiFile |
| 175 |
ascii().setStream(getInput()); |
| 176 |
ascii().open(asciiName()); |
| 177 |
|
| 178 |
checkHeader(nullptr); |
| 179 |
ok = createZones(); |
| 180 |
if (ok) { |
| 181 |
createDocument(docInterface); |
| 182 |
sendPageFrames(); |
| 183 |
m_textParser->sendMainText(); |
| 184 |
#ifdef DEBUG |
| 185 |
m_textParser->flushExtra(); |
| 186 |
#endif |
| 187 |
} |
| 188 |
ascii().reset(); |
| 189 |
} |
| 190 |
catch (...) { |
| 191 |
MWAW_DEBUG_MSG(("BeagleWksParser::parse: exception catched when parsing\n")); |
| 192 |
ok = false; |
| 193 |
} |
| 194 |
|
| 195 |
resetTextListener(); |
| 196 |
if (!ok) throw(libmwaw::ParseException()); |
| 197 |
} |
| 198 |
|
| 199 |
//////////////////////////////////////////////////////////// |
| 200 |
// create the document |
| 201 |
//////////////////////////////////////////////////////////// |
| 202 |
void BeagleWksParser::createDocument(librevenge::RVNGTextInterface *documentInterface) |
| 203 |
{ |
| 204 |
if (!documentInterface) return; |
| 205 |
if (getTextListener()) { |
| 206 |
MWAW_DEBUG_MSG(("BeagleWksParser::createDocument: listener already exist\n")); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
// update the page |
| 211 |
m_state->m_actPage = 0; |
| 212 |
|
| 213 |
// create the page list |
| 214 |
int numPages = 1; |
| 215 |
if (m_textParser->numPages() > numPages) |
| 216 |
numPages = m_textParser->numPages(); |
| 217 |
m_state->m_numPages = numPages; |
| 218 |
|
| 219 |
std::vector<MWAWPageSpan> pageList; |
| 220 |
std::shared_ptr<MWAWSubDocument> subDoc; |
| 221 |
for (int i = 0; i <= numPages;) { |
| 222 |
MWAWPageSpan ps(getPageSpan()); |
| 223 |
int numSim[2]= {1,1}; |
| 224 |
subDoc = m_textParser->getHeader(i, numSim[0]); |
| 225 |
if (subDoc) { |
| 226 |
MWAWHeaderFooter header(MWAWHeaderFooter::HEADER, MWAWHeaderFooter::ALL); |
| 227 |
header.m_subDocument=subDoc; |
| 228 |
ps.setHeaderFooter(header); |
| 229 |
} |
| 230 |
subDoc = m_textParser->getFooter(i, numSim[1]); |
| 231 |
if (subDoc) { |
| 232 |
MWAWHeaderFooter footer(MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL); |
| 233 |
footer.m_subDocument=subDoc; |
| 234 |
ps.setHeaderFooter(footer); |
| 235 |
} |
| 236 |
if (numSim[1] < numSim[0]) numSim[0]=numSim[1]; |
| 237 |
if (numSim[0] < 1) numSim[0]=1; |
| 238 |
ps.setPageSpan(numSim[0]); |
| 239 |
i+=numSim[0]; |
| 240 |
pageList.push_back(ps); |
| 241 |
} |
| 242 |
|
| 243 |
MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface)); |
| 244 |
setTextListener(listen); |
| 245 |
listen->startDocument(); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
//////////////////////////////////////////////////////////// |
| 250 |
// |
| 251 |
// Intermediate level |
| 252 |
// |
| 253 |
//////////////////////////////////////////////////////////// |
| 254 |
bool BeagleWksParser::createZones() |
| 255 |
{ |
| 256 |
readRSRCZones(); |
| 257 |
MWAWInputStreamPtr input = getInput(); |
| 258 |
if (input->seek(66, librevenge::RVNG_SEEK_SET) || !readPrintInfo()) |
| 259 |
return false; |
| 260 |
long pos = input->tell(); |
| 261 |
if (!input->checkPosition(pos+70)) { |
| 262 |
MWAW_DEBUG_MSG(("BeagleWksParser::createZones: the file can not contains Zones\n")); |
| 263 |
return false; |
| 264 |
} |
| 265 |
// first check the main entry |
| 266 |
MWAWEntry mainEntry; |
| 267 |
mainEntry.setBegin(m_state->m_textBegin); |
| 268 |
input->seek(mainEntry.begin(), librevenge::RVNG_SEEK_SET); |
| 269 |
mainEntry.setLength(input->readLong(4)); |
| 270 |
if (!mainEntry.valid()||!input->checkPosition(mainEntry.end())) { |
| 271 |
MWAW_DEBUG_MSG(("BeagleWksParser::createZones: can not determine main zone size\n")); |
| 272 |
ascii().addPos(mainEntry.begin()); |
| 273 |
ascii().addNote("Entries(Text):###"); |
| 274 |
return false; |
| 275 |
} |
| 276 |
mainEntry.setType("Text"); |
| 277 |
mainEntry.setId(0); |
| 278 |
|
| 279 |
// now read the list of zones |
| 280 |
libmwaw::DebugStream f; |
| 281 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 282 |
f << "Entries(Zones):"; |
| 283 |
for (int i=0; i<7; ++i) { // checkme: at least 2 zones, maybe 7 |
| 284 |
MWAWEntry entry; |
| 285 |
entry.setBegin(input->readLong(4)); |
| 286 |
entry.setLength(input->readLong(4)); |
| 287 |
entry.setId(static_cast<int>(input->readLong(2))); |
| 288 |
if (entry.length()==0) continue; |
| 289 |
entry.setType(i==1?"Frame":"Unknown"); |
| 290 |
f << entry.type() << "[" << entry.id() << "]=" |
| 291 |
<< std::hex << entry.begin() << "<->" << entry.end() << ","; |
| 292 |
if (!entry.valid() || !input->checkPosition(entry.end())) { |
| 293 |
f << "###"; |
| 294 |
if (i<2) { |
| 295 |
MWAW_DEBUG_MSG(("BeagleWksParser::createZones: can not read the header/footer zone, stop\n")); |
| 296 |
ascii().addPos(pos); |
| 297 |
ascii().addNote(f.str().c_str()); |
| 298 |
return false; |
| 299 |
} |
| 300 |
MWAW_DEBUG_MSG(("BeagleWksParser::createZones: can not zones entry %d\n",i)); |
| 301 |
continue; |
| 302 |
} |
| 303 |
m_state->m_typeEntryMap.insert |
| 304 |
(std::multimap<std::string, MWAWEntry>::value_type(entry.type(),entry)); |
| 305 |
} |
| 306 |
ascii().addPos(pos); |
| 307 |
ascii().addNote(f.str().c_str()); |
| 308 |
|
| 309 |
// now parse the different zones |
| 310 |
auto it=m_state->m_typeEntryMap.find("FontNames"); |
| 311 |
if (it!=m_state->m_typeEntryMap.end()) |
| 312 |
m_structureManager->readFontNames(it->second); |
| 313 |
|
| 314 |
it=m_state->m_typeEntryMap.find("Frame"); |
| 315 |
if (it!=m_state->m_typeEntryMap.end()) |
| 316 |
m_structureManager->readFrame(it->second); |
| 317 |
|
| 318 |
for (auto const &eIt : m_state->m_typeEntryMap) { |
| 319 |
MWAWEntry const &entry=eIt.second; |
| 320 |
if (entry.isParsed()) |
| 321 |
continue; |
| 322 |
f.str(""); |
| 323 |
f << "Entries(" << entry.type() << ")[" << entry.id() << "]:"; |
| 324 |
ascii().addPos(entry.begin()); |
| 325 |
ascii().addNote(f.str().c_str()); |
| 326 |
ascii().addPos(entry.end()); |
| 327 |
ascii().addNote("_"); |
| 328 |
} |
| 329 |
|
| 330 |
if (!m_textParser->createZones(mainEntry)) |
| 331 |
return false; |
| 332 |
readLastZone(); |
| 333 |
return true; |
| 334 |
} |
| 335 |
|
| 336 |
bool BeagleWksParser::readRSRCZones() |
| 337 |
{ |
| 338 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 339 |
if (!rsrcParser) |
| 340 |
return true; |
| 341 |
|
| 342 |
auto const &entryMap = rsrcParser->getEntriesMap(); |
| 343 |
// the 1 zone |
| 344 |
char const *zNames[] = {"wPos", "DMPF" }; |
| 345 |
for (int z = 0; z < 2; ++z) { |
| 346 |
auto it = entryMap.lower_bound(zNames[z]); |
| 347 |
while (it != entryMap.end()) { |
| 348 |
if (it->first != zNames[z]) |
| 349 |
break; |
| 350 |
MWAWEntry const &entry = it++->second; |
| 351 |
switch (z) { |
| 352 |
case 0: // 1001 |
| 353 |
m_structureManager->readwPos(entry); |
| 354 |
break; |
| 355 |
case 1: // find in one file with id=4661 6a1f 4057 |
| 356 |
m_structureManager->readFontStyle(entry); |
| 357 |
break; |
| 358 |
/* find also |
| 359 |
- edpt: see sendPicture |
| 360 |
- DMPP: the paragraph style |
| 361 |
- sect and alis: position?, alis=filesystem alias(dir, filename, path...) |
| 362 |
*/ |
| 363 |
default: |
| 364 |
break; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
return true; |
| 369 |
} |
| 370 |
|
| 371 |
//////////////////////////////////////////////////////////// |
| 372 |
// read/send the list of frame |
| 373 |
//////////////////////////////////////////////////////////// |
| 374 |
bool BeagleWksParser::sendPageFrames() |
| 375 |
{ |
| 376 |
auto const &frameMap = m_structureManager->getIdFrameMap(); |
| 377 |
for (auto const &it : frameMap) { |
| 378 |
auto const &frame=it.second; |
| 379 |
if (!frame.m_charAnchor) |
| 380 |
sendFrame(frame); |
| 381 |
} |
| 382 |
return true; |
| 383 |
} |
| 384 |
|
| 385 |
bool BeagleWksParser::sendFrame(BeagleWksStructManager::Frame const &frame) |
| 386 |
{ |
| 387 |
MWAWPosition fPos(MWAWVec2f(0,0), frame.m_dim, librevenge::RVNG_POINT); |
| 388 |
MWAWGraphicStyle style=MWAWGraphicStyle::emptyStyle(); |
| 389 |
if (frame.m_charAnchor) |
| 390 |
fPos.setRelativePosition(MWAWPosition::Char); |
| 391 |
else { |
| 392 |
fPos.setPagePos(frame.m_page > 0 ? frame.m_page : 1, frame.m_origin); |
| 393 |
fPos.setRelativePosition(MWAWPosition::Page); |
| 394 |
|
| 395 |
fPos.m_wrapping = frame.m_wrap==0 ? MWAWPosition::WNone : MWAWPosition::WDynamic; |
| 396 |
style.setBorders(frame.m_bordersSet, frame.m_border); |
| 397 |
} |
| 398 |
return sendPicture(frame.m_pictId, fPos, style); |
| 399 |
} |
| 400 |
|
| 401 |
//////////////////////////////////////////////////////////// |
| 402 |
// read the print info |
| 403 |
//////////////////////////////////////////////////////////// |
| 404 |
bool BeagleWksParser::readPrintInfo() |
| 405 |
{ |
| 406 |
MWAWInputStreamPtr input = getInput(); |
| 407 |
long pos = input->tell(); |
| 408 |
if (!input->checkPosition(pos+0x70)) |
| 409 |
return false; |
| 410 |
|
| 411 |
libmwaw::DebugStream f; |
| 412 |
// print info |
| 413 |
libmwaw::PrinterInfo info; |
| 414 |
if (!info.read(input)) return false; |
| 415 |
f << "Entries(PrintInfo):"<< info; |
| 416 |
|
| 417 |
MWAWVec2i paperSize = info.paper().size(); |
| 418 |
MWAWVec2i pageSize = info.page().size(); |
| 419 |
if (pageSize.x() <= 0 || pageSize.y() <= 0 || |
| 420 |
paperSize.x() <= 0 || paperSize.y() <= 0) return false; |
| 421 |
|
| 422 |
// define margin from print info |
| 423 |
MWAWVec2i lTopMargin= -1 * info.paper().pos(0); |
| 424 |
MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1); |
| 425 |
|
| 426 |
// move margin left | top |
| 427 |
int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0; |
| 428 |
int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0; |
| 429 |
lTopMargin -= MWAWVec2i(decalX, decalY); |
| 430 |
rBotMargin += MWAWVec2i(decalX, decalY); |
| 431 |
|
| 432 |
// decrease right | bottom |
| 433 |
int rightMarg = rBotMargin.x() -10; |
| 434 |
if (rightMarg < 0) rightMarg=0; |
| 435 |
int botMarg = rBotMargin.y() -50; |
| 436 |
if (botMarg < 0) botMarg=0; |
| 437 |
|
| 438 |
getPageSpan().setMarginTop(lTopMargin.y()/72.0); |
| 439 |
getPageSpan().setMarginBottom(botMarg/72.0); |
| 440 |
getPageSpan().setMarginLeft(lTopMargin.x()/72.0); |
| 441 |
getPageSpan().setMarginRight(rightMarg/72.0); |
| 442 |
getPageSpan().setFormLength(paperSize.y()/72.); |
| 443 |
getPageSpan().setFormWidth(paperSize.x()/72.); |
| 444 |
|
| 445 |
ascii().addPos(pos); |
| 446 |
ascii().addNote(f.str().c_str()); |
| 447 |
input->seek(pos+0x78, librevenge::RVNG_SEEK_SET); |
| 448 |
if (long(input->tell()) != pos+0x78) { |
| 449 |
MWAW_DEBUG_MSG(("BeagleWksParser::readPrintInfo: file is too short\n")); |
| 450 |
return false; |
| 451 |
} |
| 452 |
ascii().addPos(input->tell()); |
| 453 |
|
| 454 |
return true; |
| 455 |
} |
| 456 |
|
| 457 |
//////////////////////////////////////////////////////////// |
| 458 |
// read the last zone ( mainly unknown ) |
| 459 |
//////////////////////////////////////////////////////////// |
| 460 |
bool BeagleWksParser::readLastZone() |
| 461 |
{ |
| 462 |
MWAWInputStreamPtr input = getInput(); |
| 463 |
long beginPos = input->tell(); |
| 464 |
if (input->seek(beginPos+568,librevenge::RVNG_SEEK_SET)||!input->isEnd()) { |
| 465 |
MWAW_DEBUG_MSG(("BeagleWksParser::readLastZone: the last zone seems odd\n")); |
| 466 |
ascii().addPos(beginPos); |
| 467 |
ascii().addNote("Entries(LastZone):###"); |
| 468 |
return false; |
| 469 |
} |
| 470 |
|
| 471 |
libmwaw::DebugStream f; |
| 472 |
ascii().addPos(beginPos); |
| 473 |
ascii().addNote("Entries(LastZone)"); |
| 474 |
|
| 475 |
input->seek(beginPos+4, librevenge::RVNG_SEEK_SET); |
| 476 |
long pos; |
| 477 |
for (int st=0; st<3; ++st) { |
| 478 |
pos=input->tell(); |
| 479 |
f.str(""); |
| 480 |
f << "LastZone-A" << st << ":"; |
| 481 |
ascii().addPos(pos); |
| 482 |
ascii().addNote(f.str().c_str()); |
| 483 |
input->seek(pos+34, librevenge::RVNG_SEEK_SET); |
| 484 |
} |
| 485 |
|
| 486 |
pos=input->tell(); |
| 487 |
ascii().addPos(pos); |
| 488 |
ascii().addNote("LastZone-B:"); |
| 489 |
input->seek(pos+100, librevenge::RVNG_SEEK_SET); |
| 490 |
pos=input->tell(); |
| 491 |
f.str(""); |
| 492 |
f << "LastZone-DocInfo:"; |
| 493 |
double margins[4]; // TBRL |
| 494 |
for (int i=0; i<4; ++i) { |
| 495 |
margins[i]=double(input->readLong(4))/65536./72.; |
| 496 |
if (i<2) input->seek(2, librevenge::RVNG_SEEK_CUR); // skip margins in point |
| 497 |
} |
| 498 |
f << "margins=[" << margins[0] << "," << margins[1] << "," << margins[2] << "," << margins[3] << "],"; |
| 499 |
if (margins[0]>=0&&margins[1]>=0&&margins[2]>=0&&margins[3]>=0&& |
| 500 |
margins[0]+margins[1]<0.5*getFormLength() && |
| 501 |
margins[2]+margins[3]<0.5*getFormWidth()) { |
| 502 |
getPageSpan().setMarginTop(margins[0]); |
| 503 |
getPageSpan().setMarginBottom(margins[1]); |
| 504 |
getPageSpan().setMarginLeft(margins[3]); |
| 505 |
getPageSpan().setMarginRight(margins[2]); |
| 506 |
} |
| 507 |
else { |
| 508 |
MWAW_DEBUG_MSG(("BeagleWksParser::readLastZone: the page margins seem bad\n")); |
| 509 |
f << "###"; |
| 510 |
} |
| 511 |
auto val=static_cast<int>(input->readLong(2)); |
| 512 |
if (val!=1) f << "firstPage=" << val << ","; |
| 513 |
ascii().addDelimiter(input->tell(),'|'); |
| 514 |
ascii().addPos(pos); |
| 515 |
ascii().addNote(f.str().c_str()); |
| 516 |
|
| 517 |
|
| 518 |
input->seek(pos+76, librevenge::RVNG_SEEK_SET); |
| 519 |
for (int i=0; i<20; ++i) { |
| 520 |
pos=input->tell(); |
| 521 |
f.str(""); |
| 522 |
f << "LastZone-C" << i << ":"; |
| 523 |
ascii().addPos(pos); |
| 524 |
ascii().addNote(f.str().c_str()); |
| 525 |
input->seek(pos+6, librevenge::RVNG_SEEK_SET); |
| 526 |
} |
| 527 |
|
| 528 |
pos=input->tell(); |
| 529 |
f.str(""); |
| 530 |
f << "LastZone-D:"; |
| 531 |
for (int i=0; i < 2; ++i) { // always 0 excepted one time f0=0x263,f1=-7 |
| 532 |
val=static_cast<int>(input->readLong(2)); |
| 533 |
if (val) f << "f" << i << "=" << val << ","; |
| 534 |
} |
| 535 |
for (int i=0; i < 140; ++i) { // a list of flags ? all 1 expected f3=-1 |
| 536 |
val=static_cast<int>(input->readLong(1)); |
| 537 |
if (val!=1) f << "g" << i << "=" << val << ","; |
| 538 |
} |
| 539 |
ascii().addPos(pos); |
| 540 |
ascii().addNote(f.str().c_str()); |
| 541 |
|
| 542 |
pos=input->tell(); |
| 543 |
f.str(""); |
| 544 |
f << "LastZone-E:"; |
| 545 |
val=static_cast<int>(input->readLong(4)); // a big number, a dim? |
| 546 |
if (val) f << "dim?=" << double(val)/65536. << ","; |
| 547 |
val=static_cast<int>(input->readLong(2)); // 1|2|3|4 |
| 548 |
if (val) f << "f0=" << val << ","; |
| 549 |
for (int i=0; i<4; ++i) { |
| 550 |
static int const expectedVal[]= {0x6c, 0xc, 0, 0x21}; |
| 551 |
val=static_cast<int>(input->readLong(2)); |
| 552 |
if (val != expectedVal[i]) |
| 553 |
f << "f" << i+1 << "=" << val << ","; |
| 554 |
} |
| 555 |
// then some flags ? |
| 556 |
ascii().addDelimiter(input->tell(),'|'); |
| 557 |
ascii().addPos(pos); |
| 558 |
ascii().addNote(f.str().c_str()); |
| 559 |
return true; |
| 560 |
} |
| 561 |
|
| 562 |
//////////////////////////////////////////////////////////// |
| 563 |
// resource fork data |
| 564 |
//////////////////////////////////////////////////////////// |
| 565 |
|
| 566 |
// read/send picture (edtp resource) |
| 567 |
bool BeagleWksParser::sendPicture |
| 568 |
(int pId, MWAWPosition const &pictPos, MWAWGraphicStyle const &style) |
| 569 |
{ |
| 570 |
MWAWTextListenerPtr listener=getTextListener(); |
| 571 |
if (!listener) { |
| 572 |
MWAW_DEBUG_MSG(("BeagleWksParser::sendPicture: can not find the listener\n")); |
| 573 |
return false; |
| 574 |
} |
| 575 |
auto rsrcParser = getRSRCParser(); |
| 576 |
if (!rsrcParser) { |
| 577 |
static bool first=true; |
| 578 |
if (first) { |
| 579 |
MWAW_DEBUG_MSG(("BeagleWksParser::sendPicture: need access to resource fork to retrieve picture content\n")); |
| 580 |
first=false; |
| 581 |
} |
| 582 |
return true; |
| 583 |
} |
| 584 |
|
| 585 |
librevenge::RVNGBinaryData data; |
| 586 |
if (!m_structureManager->readPicture(pId, data)) |
| 587 |
return false; |
| 588 |
|
| 589 |
listener->insertPicture(pictPos, MWAWEmbeddedObject(data, "image/pict"), style); |
| 590 |
return true; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
//////////////////////////////////////////////////////////// |
| 595 |
// |
| 596 |
// Low level |
| 597 |
// |
| 598 |
//////////////////////////////////////////////////////////// |
| 599 |
|
| 600 |
//////////////////////////////////////////////////////////// |
| 601 |
// read the header |
| 602 |
//////////////////////////////////////////////////////////// |
| 603 |
bool BeagleWksParser::checkHeader(MWAWHeader *header, bool strict) |
| 604 |
{ |
| 605 |
*m_state = BeagleWksParserInternal::State(); |
| 606 |
MWAWInputStreamPtr input = getInput(); |
| 607 |
if (!input || !input->hasDataFork() || !input->checkPosition(66)) |
| 608 |
return false; |
| 609 |
|
| 610 |
libmwaw::DebugStream f; |
| 611 |
f << "FileHeader:"; |
| 612 |
|
| 613 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 614 |
if (input->readLong(2)!=0x4257 || input->readLong(2)!=0x6b73 || |
| 615 |
input->readLong(2)!=0x4257 || input->readLong(2)!=0x7770 || |
| 616 |
input->readLong(2)!=0x4257 || input->readLong(2)!=0x7770) { |
| 617 |
return false; |
| 618 |
} |
| 619 |
for (int i=0; i<9; ++i) { // f2=f6=1 other 0 |
| 620 |
auto val=static_cast<int>(input->readLong(2)); |
| 621 |
if (val) f << "f" << i << "=" << val << ","; |
| 622 |
} |
| 623 |
setVersion(1); |
| 624 |
|
| 625 |
if (header) |
| 626 |
header->reset(MWAWDocument::MWAW_T_BEAGLEWORKS, 1); |
| 627 |
|
| 628 |
ascii().addPos(0); |
| 629 |
ascii().addNote(f.str().c_str()); |
| 630 |
|
| 631 |
long pos=input->tell(); |
| 632 |
f.str(""); |
| 633 |
f << "FileHeader-II:"; |
| 634 |
m_state->m_textBegin=input->readLong(4); |
| 635 |
if (!input->checkPosition(m_state->m_textBegin)) { |
| 636 |
MWAW_DEBUG_MSG(("BeagleWksParser::checkHeader: can not read the text position\n")); |
| 637 |
return false; |
| 638 |
} |
| 639 |
f << "text[ptr]=" << std::hex << m_state->m_textBegin << std::dec << ","; |
| 640 |
for (int i=0; i < 11; ++i) { // f2=0x50c|58c|5ac f3=f5=9 |
| 641 |
long val=input->readLong(2); |
| 642 |
if (val) f << "f" << i << "=" << std::hex << val << std::dec << ","; |
| 643 |
} |
| 644 |
MWAWEntry entry; |
| 645 |
entry.setBegin(input->readLong(4)); |
| 646 |
entry.setLength(input->readLong(4)); |
| 647 |
entry.setId(static_cast<int>(input->readLong(2))); // in fact nFonts |
| 648 |
entry.setType("FontNames"); |
| 649 |
f << "fontNames[ptr]=" << std::hex << entry.begin() << "<->" << entry.end() |
| 650 |
<< std::dec << ",nFonts=" << entry.id() << ","; |
| 651 |
if (entry.length() && (!entry.valid() || !input->checkPosition(entry.end()))) { |
| 652 |
MWAW_DEBUG_MSG(("BeagleWksParser::checkHeader: can not read the font names position\n")); |
| 653 |
f << "###"; |
| 654 |
ascii().addPos(pos); |
| 655 |
ascii().addNote(f.str().c_str()); |
| 656 |
return false; |
| 657 |
} |
| 658 |
|
| 659 |
m_state->m_typeEntryMap.insert |
| 660 |
(std::multimap<std::string, MWAWEntry>::value_type(entry.type(),entry)); |
| 661 |
ascii().addPos(pos); |
| 662 |
ascii().addNote(f.str().c_str()); |
| 663 |
if (strict && !readPrintInfo()) |
| 664 |
return false; |
| 665 |
ascii().addPos(66); |
| 666 |
ascii().addNote("_"); |
| 667 |
|
| 668 |
return true; |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |