| 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 <cmath> |
| 35 |
#include <iomanip> |
| 36 |
#include <iostream> |
| 37 |
#include <limits> |
| 38 |
#include <set> |
| 39 |
#include <sstream> |
| 40 |
|
| 41 |
#include <librevenge/librevenge.h> |
| 42 |
|
| 43 |
#include "MWAWHeader.hxx" |
| 44 |
#include "MWAWPictBitmap.hxx" |
| 45 |
#include "MWAWPosition.hxx" |
| 46 |
#include "MWAWPrinter.hxx" |
| 47 |
#include "MWAWRSRCParser.hxx" |
| 48 |
#include "MWAWGraphicListener.hxx" |
| 49 |
#include "MWAWSubDocument.hxx" |
| 50 |
|
| 51 |
#include "BeagleWksStructManager.hxx" |
| 52 |
|
| 53 |
#include "BeagleWksBMParser.hxx" |
| 54 |
|
| 55 |
/** Internal: the structures of a BeagleWksBMParser */ |
| 56 |
namespace BeagleWksBMParserInternal |
| 57 |
{ |
| 58 |
//////////////////////////////////////// |
| 59 |
//! Internal: a bitmap of a BeagleWksBMParser |
| 60 |
struct Bitmap { |
| 61 |
//! the constructor |
| 62 |
Bitmap() |
| 63 |
: m_colorBytes(1) |
| 64 |
, m_dim(0,0) |
| 65 |
, m_colorList() |
| 66 |
{ |
| 67 |
} |
| 68 |
//! the number of color bytes (1: means black/white, 8: means color) |
| 69 |
int m_colorBytes; |
| 70 |
//! the bitmap dimension |
| 71 |
MWAWVec2i m_dim; |
| 72 |
//! the color list |
| 73 |
std::vector<MWAWColor> m_colorList; |
| 74 |
}; |
| 75 |
|
| 76 |
//////////////////////////////////////// |
| 77 |
//! Internal: the state of a BeagleWksBMParser |
| 78 |
struct State { |
| 79 |
//! constructor |
| 80 |
State() |
| 81 |
: m_graphicBegin(-1) |
| 82 |
, m_bitmap() |
| 83 |
, m_pict() |
| 84 |
, m_typeEntryMap() |
| 85 |
, m_actPage(0) |
| 86 |
, m_numPages(0) |
| 87 |
, m_headerHeight(0) |
| 88 |
, m_footerHeight(0) |
| 89 |
{ |
| 90 |
} |
| 91 |
|
| 92 |
/** the graphic begin position */ |
| 93 |
long m_graphicBegin; |
| 94 |
/** the bitmap data */ |
| 95 |
Bitmap m_bitmap; |
| 96 |
/// the bitmap |
| 97 |
std::shared_ptr<MWAWPict> m_pict; |
| 98 |
/** the type entry map */ |
| 99 |
std::multimap<std::string, MWAWEntry> m_typeEntryMap; |
| 100 |
|
| 101 |
int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */; |
| 102 |
|
| 103 |
int m_headerHeight /** the header height if known */, |
| 104 |
m_footerHeight /** the footer height if known */; |
| 105 |
}; |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
//////////////////////////////////////////////////////////// |
| 111 |
// constructor/destructor, ... |
| 112 |
//////////////////////////////////////////////////////////// |
| 113 |
BeagleWksBMParser::BeagleWksBMParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 114 |
: MWAWGraphicParser(input, rsrcParser, header) |
| 115 |
, m_state() |
| 116 |
, m_structureManager() |
| 117 |
{ |
| 118 |
init(); |
| 119 |
} |
| 120 |
|
| 121 |
BeagleWksBMParser::~BeagleWksBMParser() |
| 122 |
{ |
| 123 |
} |
| 124 |
|
| 125 |
void BeagleWksBMParser::init() |
| 126 |
{ |
| 127 |
resetGraphicListener(); |
| 128 |
setAsciiName("main-1"); |
| 129 |
|
| 130 |
m_state.reset(new BeagleWksBMParserInternal::State); |
| 131 |
m_structureManager.reset(new BeagleWksStructManager(getParserState())); |
| 132 |
|
| 133 |
// reduce the margin (in case, the page is not defined) |
| 134 |
getPageSpan().setMargins(0.1); |
| 135 |
} |
| 136 |
|
| 137 |
MWAWInputStreamPtr BeagleWksBMParser::rsrcInput() |
| 138 |
{ |
| 139 |
return getRSRCParser()->getInput(); |
| 140 |
} |
| 141 |
|
| 142 |
libmwaw::DebugFile &BeagleWksBMParser::rsrcAscii() |
| 143 |
{ |
| 144 |
return getRSRCParser()->ascii(); |
| 145 |
} |
| 146 |
|
| 147 |
//////////////////////////////////////////////////////////// |
| 148 |
// position and height |
| 149 |
//////////////////////////////////////////////////////////// |
| 150 |
MWAWVec2f BeagleWksBMParser::getPageLeftTop() const |
| 151 |
{ |
| 152 |
return MWAWVec2f(float(getPageSpan().getMarginLeft()), |
| 153 |
float(getPageSpan().getMarginTop()+m_state->m_headerHeight/72.0)); |
| 154 |
} |
| 155 |
|
| 156 |
//////////////////////////////////////////////////////////// |
| 157 |
// new page |
| 158 |
//////////////////////////////////////////////////////////// |
| 159 |
void BeagleWksBMParser::newPage(int number) |
| 160 |
{ |
| 161 |
if (number <= m_state->m_actPage || number > m_state->m_numPages) |
| 162 |
return; |
| 163 |
|
| 164 |
while (m_state->m_actPage < number) { |
| 165 |
m_state->m_actPage++; |
| 166 |
if (!getGraphicListener() || m_state->m_actPage == 1) |
| 167 |
continue; |
| 168 |
getGraphicListener()->insertBreak(MWAWGraphicListener::PageBreak); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
//////////////////////////////////////////////////////////// |
| 173 |
// the parser |
| 174 |
//////////////////////////////////////////////////////////// |
| 175 |
void BeagleWksBMParser::parse(librevenge::RVNGDrawingInterface *docInterface) |
| 176 |
{ |
| 177 |
if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 178 |
bool ok = false; |
| 179 |
try { |
| 180 |
// create the asciiFile |
| 181 |
ascii().setStream(getInput()); |
| 182 |
ascii().open(asciiName()); |
| 183 |
|
| 184 |
checkHeader(nullptr); |
| 185 |
ok = createZones(); |
| 186 |
if (ok) { |
| 187 |
createDocument(docInterface); |
| 188 |
sendPageFrames(); |
| 189 |
sendBitmap(); |
| 190 |
} |
| 191 |
ascii().reset(); |
| 192 |
} |
| 193 |
catch (...) { |
| 194 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::parse: exception catched when parsing\n")); |
| 195 |
ok = false; |
| 196 |
} |
| 197 |
|
| 198 |
resetGraphicListener(); |
| 199 |
if (!ok) throw(libmwaw::ParseException()); |
| 200 |
} |
| 201 |
|
| 202 |
//////////////////////////////////////////////////////////// |
| 203 |
// create the document |
| 204 |
//////////////////////////////////////////////////////////// |
| 205 |
void BeagleWksBMParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface) |
| 206 |
{ |
| 207 |
if (!documentInterface) return; |
| 208 |
if (getGraphicListener()) { |
| 209 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::createDocument: listener already exist\n")); |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
// update the page |
| 214 |
m_state->m_actPage = 0; |
| 215 |
|
| 216 |
// create the page list |
| 217 |
int numPages = 1; |
| 218 |
m_state->m_numPages = numPages; |
| 219 |
|
| 220 |
std::vector<MWAWPageSpan> pageList; |
| 221 |
MWAWPageSpan ps(getPageSpan()); |
| 222 |
ps.setPageSpan(numPages); |
| 223 |
pageList.push_back(ps); |
| 224 |
|
| 225 |
MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface)); |
| 226 |
setGraphicListener(listen); |
| 227 |
listen->startDocument(); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
//////////////////////////////////////////////////////////// |
| 232 |
// |
| 233 |
// Intermediate level |
| 234 |
// |
| 235 |
//////////////////////////////////////////////////////////// |
| 236 |
bool BeagleWksBMParser::createZones() |
| 237 |
{ |
| 238 |
readRSRCZones(); |
| 239 |
MWAWInputStreamPtr input = getInput(); |
| 240 |
if (input->seek(66, librevenge::RVNG_SEEK_SET) || !readPrintInfo()) |
| 241 |
return false; |
| 242 |
long pos = input->tell(); |
| 243 |
if (!input->checkPosition(pos+70)) { |
| 244 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::createZones: the file can not contains zones\n")); |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
// now read the list of zones |
| 249 |
libmwaw::DebugStream f; |
| 250 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 251 |
f << "Entries(Zones):"; |
| 252 |
for (int i=0; i<7; ++i) { // checkme: at least 2 zones, i=2 is not a zone, maybe 7 |
| 253 |
MWAWEntry entry; |
| 254 |
entry.setBegin(input->readLong(4)); |
| 255 |
entry.setLength(input->readLong(4)); |
| 256 |
entry.setId(static_cast<int>(input->readLong(2))); |
| 257 |
if (entry.length()==0) continue; |
| 258 |
entry.setType(i==1?"Frame":"Unknown"); |
| 259 |
f << entry.type() << "[" << entry.id() << "]=" |
| 260 |
<< std::hex << entry.begin() << "<->" << entry.end() << ","; |
| 261 |
if (!entry.valid() || !input->checkPosition(entry.end())) { |
| 262 |
f << "###"; |
| 263 |
if (i<2) { |
| 264 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::createZones: can not read the header zone, stop\n")); |
| 265 |
ascii().addPos(pos); |
| 266 |
ascii().addNote(f.str().c_str()); |
| 267 |
return false; |
| 268 |
} |
| 269 |
if (i!=2) { |
| 270 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::createZones: can not zones entry %d\n",i)); |
| 271 |
} |
| 272 |
continue; |
| 273 |
} |
| 274 |
m_state->m_typeEntryMap.insert |
| 275 |
(std::multimap<std::string, MWAWEntry>::value_type(entry.type(),entry)); |
| 276 |
} |
| 277 |
ascii().addPos(pos); |
| 278 |
ascii().addNote(f.str().c_str()); |
| 279 |
|
| 280 |
// now parse the different zones |
| 281 |
auto it=m_state->m_typeEntryMap.find("FontNames"); |
| 282 |
if (it!=m_state->m_typeEntryMap.end()) |
| 283 |
m_structureManager->readFontNames(it->second); |
| 284 |
it=m_state->m_typeEntryMap.find("Frame"); |
| 285 |
if (it!=m_state->m_typeEntryMap.end()) |
| 286 |
m_structureManager->readFrame(it->second); |
| 287 |
|
| 288 |
// now parse the different zones |
| 289 |
for (it=m_state->m_typeEntryMap.begin(); it!=m_state->m_typeEntryMap.end(); ++it) { |
| 290 |
MWAWEntry const &entry=it->second; |
| 291 |
if (entry.isParsed()) |
| 292 |
continue; |
| 293 |
f.str(""); |
| 294 |
f << "Entries(" << entry.type() << ")[" << entry.id() << "]:"; |
| 295 |
ascii().addPos(entry.begin()); |
| 296 |
ascii().addNote(f.str().c_str()); |
| 297 |
ascii().addPos(entry.end()); |
| 298 |
ascii().addNote("_"); |
| 299 |
} |
| 300 |
|
| 301 |
input->seek(m_state->m_graphicBegin, librevenge::RVNG_SEEK_SET); |
| 302 |
if (!readPrefColorMap()) { |
| 303 |
ascii().addPos(pos); |
| 304 |
ascii().addNote("Entries(ColorMap)[Pref]:###"); |
| 305 |
return false; |
| 306 |
} |
| 307 |
|
| 308 |
pos = input->tell(); |
| 309 |
if (!readBitmap(true)) |
| 310 |
return false; |
| 311 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 312 |
if (!readBitmap() || !m_state->m_pict) return false; |
| 313 |
if (!input->isEnd()) { |
| 314 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::createZones: find some extra data\n")); |
| 315 |
ascii().addPos(input->tell()); |
| 316 |
ascii().addNote("Entries(ZoneEnd)"); |
| 317 |
} |
| 318 |
return true; |
| 319 |
} |
| 320 |
|
| 321 |
bool BeagleWksBMParser::readRSRCZones() |
| 322 |
{ |
| 323 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 324 |
if (!rsrcParser) |
| 325 |
return true; |
| 326 |
|
| 327 |
auto const &entryMap = rsrcParser->getEntriesMap(); |
| 328 |
// the 1 zone |
| 329 |
char const *zNames[] = {"wPos", "DMPF" }; |
| 330 |
for (int z = 0; z < 2; ++z) { |
| 331 |
auto it = entryMap.lower_bound(zNames[z]); |
| 332 |
while (it != entryMap.end()) { |
| 333 |
if (it->first != zNames[z]) |
| 334 |
break; |
| 335 |
MWAWEntry const &entry = it++->second; |
| 336 |
switch (z) { |
| 337 |
case 0: // 1001 |
| 338 |
m_structureManager->readwPos(entry); |
| 339 |
break; |
| 340 |
case 1: // find in one file with id=4661 6a1f 4057 |
| 341 |
m_structureManager->readFontStyle(entry); |
| 342 |
break; |
| 343 |
/* find also |
| 344 |
- edpt: see sendPicture |
| 345 |
- DMPP: the paragraph style |
| 346 |
- sect and alis: position?, alis=filesystem alias(dir, filename, path...) |
| 347 |
*/ |
| 348 |
default: |
| 349 |
break; |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
return true; |
| 354 |
} |
| 355 |
|
| 356 |
//////////////////////////////////////////////////////////// |
| 357 |
// read the bitmap data |
| 358 |
//////////////////////////////////////////////////////////// |
| 359 |
bool BeagleWksBMParser::readBitmap(bool onlyCheck) |
| 360 |
{ |
| 361 |
MWAWInputStreamPtr input = getInput(); |
| 362 |
auto &bitmap=m_state->m_bitmap; |
| 363 |
bool isColor=bitmap.m_colorBytes==8; |
| 364 |
|
| 365 |
if (bitmap.m_dim[0] < 0 || bitmap.m_dim[1] < 0) { |
| 366 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::readBitmap: invalid bitmap dimensions\n")); |
| 367 |
return false; |
| 368 |
} |
| 369 |
|
| 370 |
long pos = input->tell(); |
| 371 |
auto dSz=long(input->readULong(2)); |
| 372 |
long endPos=pos+dSz; |
| 373 |
if (dSz<38 || !input->checkPosition(endPos)) { |
| 374 |
ascii().addPos(pos); |
| 375 |
ascii().addNote("Entries(Bitmap):###"); |
| 376 |
return false; |
| 377 |
} |
| 378 |
|
| 379 |
libmwaw::DebugStream f; |
| 380 |
f << "Entries(Bitmap):"; |
| 381 |
int dim[4]; |
| 382 |
for (int &i : dim) i=static_cast<int>(input->readULong(2)); |
| 383 |
if (dim[0]||dim[1]||dim[2]!=bitmap.m_dim[1]||dim[3]!=bitmap.m_dim[0]) |
| 384 |
f << "dim=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ","; |
| 385 |
int val; |
| 386 |
for (int i=0; i<8; ++i) { |
| 387 |
int const expected[] = {0x11,0x2ff, 0xc00, -1, -1, 0, 0, 0 }; |
| 388 |
val=static_cast<int>(input->readLong(2)); |
| 389 |
if (val!=expected[i]) |
| 390 |
f << "f" << i << "=" << val << ","; |
| 391 |
} |
| 392 |
for (int &i : dim) i=static_cast<int>(input->readULong(2)); |
| 393 |
if (dim[0]||dim[2]||dim[1]!=bitmap.m_dim[0]||dim[3]!=bitmap.m_dim[1]) |
| 394 |
f << "dim1=" << dim[0] << "x" << dim[2] << "<->" << dim[1] << "x" << dim[3] << ","; |
| 395 |
for (int i=0; i<5; ++i) { |
| 396 |
int const expected[] = {0, 0, 0, 0xa0, 0x82 }; |
| 397 |
val=static_cast<int>(input->readLong(2)); |
| 398 |
if (val!=expected[i]) |
| 399 |
f << "g" << i << "=" << val << ","; |
| 400 |
} |
| 401 |
val=static_cast<int>(input->readULong(2)); |
| 402 |
if (val==0x1e) { |
| 403 |
f << "f1e,"; |
| 404 |
val=static_cast<int>(input->readULong(2)); |
| 405 |
} |
| 406 |
if (val!=1) { |
| 407 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::readBitmap: we may have a problem\n")); |
| 408 |
f << "h0=" << val << ","; |
| 409 |
} |
| 410 |
val=static_cast<int>(input->readULong(2)); |
| 411 |
if (val != 0xa) f << "h1=" << val << ","; |
| 412 |
for (int &i : dim) i=static_cast<int>(input->readULong(2)); |
| 413 |
if (dim[0]||dim[1]||dim[2]!=bitmap.m_dim[1]||dim[3]!=bitmap.m_dim[0]) |
| 414 |
f << "dim2=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ","; |
| 415 |
val=static_cast<int>(input->readULong(2)); |
| 416 |
if (val != 0x98) f << "h2=" << val << ","; |
| 417 |
f << "fl?=" << std::hex << input->readULong(2) << std::dec << ","; // 48[BW]|8240[Color] |
| 418 |
for (int &i : dim) i=static_cast<int>(input->readULong(2)); |
| 419 |
if (dim[0]||dim[1]||dim[2]!=bitmap.m_dim[1]||dim[3]!=bitmap.m_dim[0]) |
| 420 |
f << "dim3=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ","; |
| 421 |
ascii().addPos(pos); |
| 422 |
ascii().addNote(f.str().c_str()); |
| 423 |
if (isColor) { |
| 424 |
pos=input->tell(); |
| 425 |
if (!readColorMap() || bitmap.m_colorList.empty()) { |
| 426 |
ascii().addPos(pos); |
| 427 |
ascii().addNote("ColorMap:###"); |
| 428 |
return false; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
pos=input->tell(); |
| 433 |
if (pos+18>endPos || !input->checkPosition(pos+17)) { |
| 434 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::readBitmap: the zone B seems too short\n")); |
| 435 |
return false; |
| 436 |
} |
| 437 |
f.str(""); |
| 438 |
f << "Bitmap-A:"; |
| 439 |
for (int j=0; j < 2; ++j) { |
| 440 |
for (int &i : dim) i=static_cast<int>(input->readULong(2)); |
| 441 |
if (dim[0]||dim[1]||dim[2]!=bitmap.m_dim[1]||dim[3]!=bitmap.m_dim[0]) |
| 442 |
f << "dim" << j << "=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ","; |
| 443 |
} |
| 444 |
val=static_cast<int>(input->readLong(2)); |
| 445 |
if (val) f << "f0=" << val << ","; |
| 446 |
ascii().addPos(pos); |
| 447 |
ascii().addNote(f.str().c_str()); |
| 448 |
|
| 449 |
int sSz=isColor ? 2 : 1; |
| 450 |
auto numColors=static_cast<int>(bitmap.m_colorList.size()); |
| 451 |
std::shared_ptr<MWAWPictBitmapIndexed> pict; |
| 452 |
if (!onlyCheck) { |
| 453 |
pict = std::make_shared<MWAWPictBitmapIndexed>(bitmap.m_dim); |
| 454 |
if (isColor) |
| 455 |
pict->setColors(bitmap.m_colorList); |
| 456 |
else { |
| 457 |
std::vector<MWAWColor> colors(2); |
| 458 |
colors[0]=MWAWColor::white(); |
| 459 |
colors[1]=MWAWColor::black(); |
| 460 |
pict->setColors(colors); |
| 461 |
} |
| 462 |
} |
| 463 |
for (int r=0; r<bitmap.m_dim[1]; ++r) { |
| 464 |
pos=input->tell(); |
| 465 |
auto sz=static_cast<int>(input->readULong(sSz)); |
| 466 |
f.str(""); |
| 467 |
f << "Bitmap-R" << r << ":"; |
| 468 |
long rEndPos=pos+sSz+sz; |
| 469 |
if (!sz || rEndPos>endPos) { |
| 470 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::readBitmap: can read row %d\n", r)); |
| 471 |
f << "###"; |
| 472 |
ascii().addPos(pos); |
| 473 |
ascii().addNote(f.str().c_str()); |
| 474 |
return false; |
| 475 |
} |
| 476 |
int col=0; |
| 477 |
while (input->tell()<rEndPos) { // UnpackBits |
| 478 |
auto wh=static_cast<int>(input->readULong(1)); |
| 479 |
if (wh>=0x81) { |
| 480 |
auto color=static_cast<int>(input->readULong(1)); |
| 481 |
for (int j=0; j < 0x101-wh; ++j) { |
| 482 |
if (col>=bitmap.m_dim[0]) break; |
| 483 |
if (isColor) { |
| 484 |
if (color>=numColors) break; |
| 485 |
if (onlyCheck) |
| 486 |
++col; |
| 487 |
else |
| 488 |
pict->set(col++, r, color); |
| 489 |
} |
| 490 |
else { |
| 491 |
for (int b=7; b>=0; --b) { |
| 492 |
if (col>=bitmap.m_dim[0]) break; |
| 493 |
if (onlyCheck) |
| 494 |
++col; |
| 495 |
else |
| 496 |
pict->set(col++, r, (color>>b)&1); |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
else { // checkme normally 0x80 is reserved and almost nobody used it (for ending the compression) |
| 502 |
for (int j=0; j < wh+1; ++j) { |
| 503 |
auto color=static_cast<int>(input->readULong(1)); |
| 504 |
if (col>=bitmap.m_dim[0]) continue; |
| 505 |
if (isColor) { |
| 506 |
if (color>=numColors) break; |
| 507 |
if (onlyCheck) |
| 508 |
++col; |
| 509 |
else |
| 510 |
pict->set(col++, r, color); |
| 511 |
} |
| 512 |
else { |
| 513 |
for (int b=7; b>=0; --b) { |
| 514 |
if (col>=bitmap.m_dim[0]) break; |
| 515 |
if (onlyCheck) |
| 516 |
++col; |
| 517 |
else |
| 518 |
pict->set(col++, r, (color>>b)&1); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
if (input->tell()>rEndPos) { |
| 524 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::readBitmap: can read row data %d\n", r)); |
| 525 |
f << "###"; |
| 526 |
ascii().addPos(pos); |
| 527 |
ascii().addNote(f.str().c_str()); |
| 528 |
return false; |
| 529 |
} |
| 530 |
} |
| 531 |
if (col!=bitmap.m_dim[0]) { |
| 532 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::readBitmap: row %d has a unexpected number of pixels\n", r)); |
| 533 |
f << "#" << col << "!=" << bitmap.m_dim[0] << ","; |
| 534 |
ascii().addPos(pos); |
| 535 |
ascii().addNote(f.str().c_str()); |
| 536 |
return false; |
| 537 |
} |
| 538 |
ascii().addPos(pos); |
| 539 |
ascii().addNote(f.str().c_str()); |
| 540 |
input->seek(rEndPos, librevenge::RVNG_SEEK_SET); |
| 541 |
} |
| 542 |
if (!onlyCheck) |
| 543 |
m_state->m_pict=pict; |
| 544 |
pos=input->tell(); |
| 545 |
f.str(""); |
| 546 |
f << "Bitmap-Data[end]:"; |
| 547 |
if (pos+7==endPos) { // no sure when it exists |
| 548 |
val=static_cast<int>(input->readLong(1)); |
| 549 |
if (val) f << "unkn=" << val << ","; |
| 550 |
} |
| 551 |
if (input->tell()+6!=endPos) { |
| 552 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::readBitmap: the end seems bad\n")); |
| 553 |
f << "###"; |
| 554 |
} |
| 555 |
else { |
| 556 |
for (int i=0; i<3; ++i) // f0=a0, f1=83, f2=ff |
| 557 |
f << "f" << i << "=" << std::hex << input->readLong(2) << std::dec << ","; |
| 558 |
} |
| 559 |
ascii().addPos(pos); |
| 560 |
ascii().addNote(f.str().c_str()); |
| 561 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 562 |
return true; |
| 563 |
} |
| 564 |
|
| 565 |
bool BeagleWksBMParser::readPrefColorMap() |
| 566 |
{ |
| 567 |
MWAWInputStreamPtr input = getInput(); |
| 568 |
long pos = input->tell(); |
| 569 |
if (!input->checkPosition(pos+38)) { |
| 570 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::readPrefColorMap: the bitmap header seems too short\n")); |
| 571 |
ascii().addPos(input->tell()); |
| 572 |
ascii().addNote("Entries(ColorMap)[pref]:###"); |
| 573 |
return false; |
| 574 |
} |
| 575 |
auto &bitmap=m_state->m_bitmap; |
| 576 |
libmwaw::DebugStream f; |
| 577 |
f << "Entries(ColorMap)[pref,header]:"; |
| 578 |
auto val=static_cast<int>(input->readLong(2)); |
| 579 |
if (val) f << "f0=" << val << ","; |
| 580 |
f << "sz[bitmap]=" << std::hex << input->readULong(2) << std::dec << ","; |
| 581 |
for (int i=0; i<4; ++i) { // 0,0,8,8 ? f2-f3=colorMap size? |
| 582 |
val=static_cast<int>(input->readLong(1)); |
| 583 |
if (val) f << "f" << i+1 << "=" << val << ","; |
| 584 |
} |
| 585 |
int dim[2]; |
| 586 |
for (int &i : dim) i=static_cast<int>(input->readLong(2)); |
| 587 |
bitmap.m_dim=MWAWVec2i(dim[1], dim[0]); |
| 588 |
f << "dim=" << bitmap.m_dim << ","; |
| 589 |
bitmap.m_colorBytes=static_cast<int>(input->readLong(2)); |
| 590 |
switch (bitmap.m_colorBytes) { |
| 591 |
case 1: |
| 592 |
f << "BW,"; |
| 593 |
break; |
| 594 |
case 8: |
| 595 |
f << "color,"; |
| 596 |
break; |
| 597 |
default: |
| 598 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::readPrefColorMap: the number of bytes seems bad\n")); |
| 599 |
f << "#bytes=" << bitmap.m_colorBytes << ","; |
| 600 |
break; |
| 601 |
} |
| 602 |
for (int i=0; i<11; ++i) { |
| 603 |
val=static_cast<int>(input->readLong(2)); |
| 604 |
if (val) f << "g" << i << "=" << val << ","; |
| 605 |
} |
| 606 |
auto maxColor=static_cast<int>(input->readULong(2)); |
| 607 |
f << "N="<< maxColor+1 << ","; |
| 608 |
ascii().addPos(pos); |
| 609 |
ascii().addNote(f.str().c_str()); |
| 610 |
|
| 611 |
pos=input->tell(); |
| 612 |
if (!input->checkPosition(pos+(1+maxColor)*8)) |
| 613 |
return false; |
| 614 |
|
| 615 |
for (int i=0; i<= maxColor; ++i) { |
| 616 |
f.str(""); |
| 617 |
f << "ColorMap-P" << i << ":"; |
| 618 |
pos = input->tell(); |
| 619 |
auto id=static_cast<int>(input->readLong(2)); |
| 620 |
if (id!=i) f << "#id=" << id << ","; |
| 621 |
unsigned char col[3]; |
| 622 |
for (unsigned char &c : col) c = static_cast<unsigned char>(input->readULong(2)>>8); |
| 623 |
MWAWColor color(col[0], col[1], col[2]); |
| 624 |
f << color << ","; |
| 625 |
ascii().addPos(pos); |
| 626 |
ascii().addNote(f.str().c_str()); |
| 627 |
} |
| 628 |
return true; |
| 629 |
} |
| 630 |
|
| 631 |
bool BeagleWksBMParser::readColorMap() |
| 632 |
{ |
| 633 |
MWAWInputStreamPtr input = getInput(); |
| 634 |
long pos = input->tell(); |
| 635 |
if (!input->checkPosition(pos+44)) { |
| 636 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::readColorMap: the bitmap header seems too short\n")); |
| 637 |
ascii().addPos(input->tell()); |
| 638 |
ascii().addNote("ColorMap:###"); |
| 639 |
return false; |
| 640 |
} |
| 641 |
libmwaw::DebugStream f; |
| 642 |
f << "ColorMap[header]:"; |
| 643 |
int val; |
| 644 |
for (int i=0; i<14; ++i) { // always f4=72,f6=72,f9=8,f10=1,f11=8 ? |
| 645 |
val=static_cast<int>(input->readLong(2)); |
| 646 |
if (val) f << "f" << i << "=" << val << ","; |
| 647 |
} |
| 648 |
f << "id=" << std::hex << input->readULong(4) << std::dec << ","; |
| 649 |
for (int i=0; i<5; ++i) { // 0,0,0,sz?, 0 |
| 650 |
val=static_cast<int>(input->readLong(2)); |
| 651 |
if (val) f << "g" << i << "=" << std::hex << val << std::dec << ","; |
| 652 |
} |
| 653 |
auto maxColor=static_cast<int>(input->readULong(2)); |
| 654 |
f << "N="<< maxColor+1 << ","; |
| 655 |
ascii().addPos(pos); |
| 656 |
ascii().addNote(f.str().c_str()); |
| 657 |
|
| 658 |
pos=input->tell(); |
| 659 |
if (!input->checkPosition(pos+(1+maxColor)*8)) |
| 660 |
return false; |
| 661 |
|
| 662 |
auto &bitmap=m_state->m_bitmap; |
| 663 |
bitmap.m_colorList.resize(size_t(maxColor+1)); |
| 664 |
for (int i=0; i<= maxColor; ++i) { |
| 665 |
f.str(""); |
| 666 |
f << "ColorMap-" << i << ":"; |
| 667 |
pos = input->tell(); |
| 668 |
auto id=static_cast<int>(input->readLong(2)); |
| 669 |
if (id!=i) f << "#id=" << id << ","; |
| 670 |
unsigned char col[3]; |
| 671 |
for (unsigned char &c : col) c = static_cast<unsigned char>(input->readULong(2)>>8); |
| 672 |
MWAWColor color(col[0], col[1], col[2]); |
| 673 |
bitmap.m_colorList[size_t(i)]=color; |
| 674 |
f << color << ","; |
| 675 |
ascii().addPos(pos); |
| 676 |
ascii().addNote(f.str().c_str()); |
| 677 |
} |
| 678 |
return true; |
| 679 |
} |
| 680 |
|
| 681 |
//////////////////////////////////////////////////////////// |
| 682 |
// read the print info |
| 683 |
//////////////////////////////////////////////////////////// |
| 684 |
bool BeagleWksBMParser::readPrintInfo() |
| 685 |
{ |
| 686 |
MWAWInputStreamPtr input = getInput(); |
| 687 |
long pos = input->tell(); |
| 688 |
if (!input->checkPosition(pos+0x70)) |
| 689 |
return false; |
| 690 |
|
| 691 |
libmwaw::DebugStream f; |
| 692 |
// print info |
| 693 |
libmwaw::PrinterInfo info; |
| 694 |
if (!info.read(input)) return false; |
| 695 |
f << "Entries(PrintInfo):"<< info; |
| 696 |
|
| 697 |
MWAWVec2i paperSize = info.paper().size(); |
| 698 |
MWAWVec2i pageSize = info.page().size(); |
| 699 |
if (pageSize.x() <= 0 || pageSize.y() <= 0 || |
| 700 |
paperSize.x() <= 0 || paperSize.y() <= 0) return false; |
| 701 |
|
| 702 |
// define margin from print info |
| 703 |
MWAWVec2i lTopMargin= -1 * info.paper().pos(0); |
| 704 |
MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1); |
| 705 |
|
| 706 |
// move margin left | top |
| 707 |
int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0; |
| 708 |
int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0; |
| 709 |
lTopMargin -= MWAWVec2i(decalX, decalY); |
| 710 |
rBotMargin += MWAWVec2i(decalX, decalY); |
| 711 |
|
| 712 |
// decrease right | bottom |
| 713 |
int rightMarg = rBotMargin.x() -10; |
| 714 |
if (rightMarg < 0) rightMarg=0; |
| 715 |
int botMarg = rBotMargin.y() -50; |
| 716 |
if (botMarg < 0) botMarg=0; |
| 717 |
|
| 718 |
getPageSpan().setMarginTop(lTopMargin.y()/72.0); |
| 719 |
getPageSpan().setMarginBottom(botMarg/72.0); |
| 720 |
getPageSpan().setMarginLeft(lTopMargin.x()/72.0); |
| 721 |
getPageSpan().setMarginRight(rightMarg/72.0); |
| 722 |
getPageSpan().setFormLength(paperSize.y()/72.); |
| 723 |
getPageSpan().setFormWidth(paperSize.x()/72.); |
| 724 |
|
| 725 |
ascii().addPos(pos); |
| 726 |
ascii().addNote(f.str().c_str()); |
| 727 |
input->seek(pos+0x78, librevenge::RVNG_SEEK_SET); |
| 728 |
if (long(input->tell()) != pos+0x78) { |
| 729 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::readPrintInfo: file is too short\n")); |
| 730 |
return false; |
| 731 |
} |
| 732 |
ascii().addPos(input->tell()); |
| 733 |
|
| 734 |
return true; |
| 735 |
} |
| 736 |
|
| 737 |
//////////////////////////////////////////////////////////// |
| 738 |
// send data |
| 739 |
//////////////////////////////////////////////////////////// |
| 740 |
bool BeagleWksBMParser::sendBitmap() |
| 741 |
{ |
| 742 |
MWAWGraphicListenerPtr listener=getGraphicListener(); |
| 743 |
if (!listener) { |
| 744 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::sendBitmap: can not find the listener\n")); |
| 745 |
return false; |
| 746 |
} |
| 747 |
|
| 748 |
MWAWEmbeddedObject picture; |
| 749 |
if (!m_state->m_pict || !m_state->m_pict->getBinary(picture)) return false; |
| 750 |
|
| 751 |
MWAWPageSpan const &page=getPageSpan(); |
| 752 |
MWAWPosition pos(MWAWVec2f(float(page.getMarginLeft()),float(page.getMarginRight())), |
| 753 |
MWAWVec2f(float(page.getPageWidth()),float(page.getPageLength())), librevenge::RVNG_INCH); |
| 754 |
pos.setRelativePosition(MWAWPosition::Page); |
| 755 |
pos.m_wrapping = MWAWPosition::WNone; |
| 756 |
listener->insertPicture(pos, picture); |
| 757 |
return true; |
| 758 |
} |
| 759 |
|
| 760 |
bool BeagleWksBMParser::sendPageFrames() |
| 761 |
{ |
| 762 |
std::map<int, BeagleWksStructManager::Frame> const &frameMap = m_structureManager->getIdFrameMap(); |
| 763 |
std::map<int, BeagleWksStructManager::Frame>::const_iterator it; |
| 764 |
for (it=frameMap.begin(); it!=frameMap.end(); ++it) |
| 765 |
sendFrame(it->second); |
| 766 |
return true; |
| 767 |
} |
| 768 |
|
| 769 |
bool BeagleWksBMParser::sendFrame(BeagleWksStructManager::Frame const &frame) |
| 770 |
{ |
| 771 |
MWAWPosition fPos(MWAWVec2f(0,0), frame.m_dim, librevenge::RVNG_POINT); |
| 772 |
|
| 773 |
fPos.setPagePos(frame.m_page > 0 ? frame.m_page : 1, frame.m_origin); |
| 774 |
fPos.setRelativePosition(MWAWPosition::Page); |
| 775 |
fPos.m_wrapping = frame.m_wrap==0 ? MWAWPosition::WNone : MWAWPosition::WDynamic; |
| 776 |
|
| 777 |
MWAWGraphicStyle style=MWAWGraphicStyle::emptyStyle(); |
| 778 |
style.setBorders(frame.m_bordersSet, frame.m_border); |
| 779 |
return sendPicture(frame.m_pictId, fPos, style); |
| 780 |
} |
| 781 |
|
| 782 |
// read/send picture (edtp resource) |
| 783 |
bool BeagleWksBMParser::sendPicture |
| 784 |
(int pId, MWAWPosition const &pictPos, MWAWGraphicStyle const &style) |
| 785 |
{ |
| 786 |
MWAWGraphicListenerPtr listener=getGraphicListener(); |
| 787 |
if (!listener) { |
| 788 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::sendPicture: can not find the listener\n")); |
| 789 |
return false; |
| 790 |
} |
| 791 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 792 |
if (!rsrcParser) { |
| 793 |
static bool first=true; |
| 794 |
if (first) { |
| 795 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::sendPicture: need access to resource fork to retrieve picture content\n")); |
| 796 |
first=false; |
| 797 |
} |
| 798 |
return true; |
| 799 |
} |
| 800 |
|
| 801 |
librevenge::RVNGBinaryData data; |
| 802 |
if (!m_structureManager->readPicture(pId, data)) |
| 803 |
return false; |
| 804 |
|
| 805 |
listener->insertPicture(pictPos, MWAWEmbeddedObject(data, "image/pict"), style); |
| 806 |
return true; |
| 807 |
} |
| 808 |
|
| 809 |
//////////////////////////////////////////////////////////// |
| 810 |
// |
| 811 |
// Low level |
| 812 |
// |
| 813 |
//////////////////////////////////////////////////////////// |
| 814 |
|
| 815 |
//////////////////////////////////////////////////////////// |
| 816 |
// read the header |
| 817 |
//////////////////////////////////////////////////////////// |
| 818 |
bool BeagleWksBMParser::checkHeader(MWAWHeader *header, bool strict) |
| 819 |
{ |
| 820 |
*m_state = BeagleWksBMParserInternal::State(); |
| 821 |
MWAWInputStreamPtr input = getInput(); |
| 822 |
if (!input || !input->hasDataFork() || !input->checkPosition(66)) |
| 823 |
return false; |
| 824 |
|
| 825 |
libmwaw::DebugStream f; |
| 826 |
f << "FileHeader:"; |
| 827 |
|
| 828 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 829 |
if (input->readLong(2)!=0x4257 || input->readLong(2)!=0x6b73 || |
| 830 |
input->readLong(2)!=0x4257 || input->readLong(2)!=0x7074 || |
| 831 |
input->readLong(2)!=0x4257 || input->readLong(2)!=0x7074) { |
| 832 |
return false; |
| 833 |
} |
| 834 |
for (int i=0; i < 9; ++i) { // f2=f6=1 other 0 |
| 835 |
auto val=static_cast<int>(input->readLong(2)); |
| 836 |
if (val) f << "f" << i << "=" << val << ","; |
| 837 |
} |
| 838 |
setVersion(1); |
| 839 |
|
| 840 |
if (header) |
| 841 |
header->reset(MWAWDocument::MWAW_T_BEAGLEWORKS, 1, MWAWDocument::MWAW_K_PAINT); |
| 842 |
|
| 843 |
ascii().addPos(0); |
| 844 |
ascii().addNote(f.str().c_str()); |
| 845 |
|
| 846 |
long pos=input->tell(); |
| 847 |
f.str(""); |
| 848 |
f << "FileHeader-II:"; |
| 849 |
m_state->m_graphicBegin=input->readLong(4); |
| 850 |
if (!input->checkPosition(m_state->m_graphicBegin)) { |
| 851 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::checkHeader: can not read the graphic position\n")); |
| 852 |
return false; |
| 853 |
} |
| 854 |
f << "graphic[ptr]=" << std::hex << m_state->m_graphicBegin << std::dec << ","; |
| 855 |
for (int i=0; i < 11; ++i) { // f2=0x50c|58c|5ac f3=f5=9 |
| 856 |
long val=input->readLong(2); |
| 857 |
if (val) f << "f" << i << "=" << std::hex << val << std::dec << ","; |
| 858 |
} |
| 859 |
MWAWEntry entry; |
| 860 |
entry.setBegin(input->readLong(4)); |
| 861 |
entry.setLength(input->readLong(4)); |
| 862 |
entry.setId(static_cast<int>(input->readLong(2))); // in fact nFonts |
| 863 |
entry.setType("FontNames"); |
| 864 |
f << "fontNames[ptr]=" << std::hex << entry.begin() << "<->" << entry.end() |
| 865 |
<< std::dec << ",nFonts=" << entry.id() << ","; |
| 866 |
if (entry.length() && (!entry.valid() || !input->checkPosition(entry.end()))) { |
| 867 |
MWAW_DEBUG_MSG(("BeagleWksBMParser::checkHeader: can not read the font names position\n")); |
| 868 |
f << "###"; |
| 869 |
ascii().addPos(pos); |
| 870 |
ascii().addNote(f.str().c_str()); |
| 871 |
return false; |
| 872 |
} |
| 873 |
|
| 874 |
m_state->m_typeEntryMap.insert |
| 875 |
(std::multimap<std::string, MWAWEntry>::value_type(entry.type(),entry)); |
| 876 |
ascii().addPos(pos); |
| 877 |
ascii().addNote(f.str().c_str()); |
| 878 |
if (strict && !readPrintInfo()) |
| 879 |
return false; |
| 880 |
ascii().addPos(66); |
| 881 |
ascii().addNote("_"); |
| 882 |
|
| 883 |
return true; |
| 884 |
} |
| 885 |
|
| 886 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |