| 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 <algorithm> |
| 35 |
#include <iomanip> |
| 36 |
#include <iostream> |
| 37 |
#include <limits> |
| 38 |
#include <set> |
| 39 |
#include <sstream> |
| 40 |
|
| 41 |
#include <librevenge/librevenge.h> |
| 42 |
|
| 43 |
#include "MWAWTextListener.hxx" |
| 44 |
#include "MWAWFont.hxx" |
| 45 |
#include "MWAWFontConverter.hxx" |
| 46 |
#include "MWAWHeader.hxx" |
| 47 |
#include "MWAWParagraph.hxx" |
| 48 |
#include "MWAWPictData.hxx" |
| 49 |
#include "MWAWPosition.hxx" |
| 50 |
#include "MWAWPrinter.hxx" |
| 51 |
#include "MWAWRSRCParser.hxx" |
| 52 |
#include "MWAWSubDocument.hxx" |
| 53 |
|
| 54 |
#include "libmwaw_internal.hxx" |
| 55 |
|
| 56 |
#include "MaxWrtParser.hxx" |
| 57 |
|
| 58 |
/** Internal: the structures of a MaxWrtParser */ |
| 59 |
namespace MaxWrtParserInternal |
| 60 |
{ |
| 61 |
//////////////////////////////////////// |
| 62 |
//! Internal: the state of a MaxWrtParser |
| 63 |
struct State { |
| 64 |
//! constructor |
| 65 |
State() |
| 66 |
: m_fontList() |
| 67 |
, m_posToPLCMap() |
| 68 |
{ |
| 69 |
} |
| 70 |
//! the list of font |
| 71 |
std::vector<MWAWFont> m_fontList; |
| 72 |
//! a map character pos to font id |
| 73 |
std::map<int,int> m_posToPLCMap; |
| 74 |
}; |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
//////////////////////////////////////////////////////////// |
| 79 |
// constructor/destructor, ... |
| 80 |
//////////////////////////////////////////////////////////// |
| 81 |
MaxWrtParser::MaxWrtParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 82 |
: MWAWTextParser(input, rsrcParser, header) |
| 83 |
, m_state(new MaxWrtParserInternal::State) |
| 84 |
{ |
| 85 |
setAsciiName("main-1"); |
| 86 |
// no margins ( ie. the document is a set of picture corresponding to each page ) |
| 87 |
getPageSpan().setMargins(0.01); |
| 88 |
} |
| 89 |
|
| 90 |
MaxWrtParser::~MaxWrtParser() |
| 91 |
{ |
| 92 |
} |
| 93 |
|
| 94 |
MWAWInputStreamPtr MaxWrtParser::rsrcInput() |
| 95 |
{ |
| 96 |
return getRSRCParser()->getInput(); |
| 97 |
} |
| 98 |
|
| 99 |
libmwaw::DebugFile &MaxWrtParser::rsrcAscii() |
| 100 |
{ |
| 101 |
return getRSRCParser()->ascii(); |
| 102 |
} |
| 103 |
|
| 104 |
//////////////////////////////////////////////////////////// |
| 105 |
// the parser |
| 106 |
//////////////////////////////////////////////////////////// |
| 107 |
void MaxWrtParser::parse(librevenge::RVNGTextInterface *docInterface) |
| 108 |
{ |
| 109 |
if (!getInput().get() || !getRSRCParser() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 110 |
bool ok = false; |
| 111 |
try { |
| 112 |
// create the asciiFile |
| 113 |
ascii().setStream(getInput()); |
| 114 |
ascii().open(asciiName()); |
| 115 |
checkHeader(nullptr); |
| 116 |
ok = createZones(); |
| 117 |
if (ok) { |
| 118 |
createDocument(docInterface); |
| 119 |
sendText(); |
| 120 |
} |
| 121 |
ascii().reset(); |
| 122 |
} |
| 123 |
catch (...) { |
| 124 |
MWAW_DEBUG_MSG(("MaxWrtParser::parse: exception catched when parsing\n")); |
| 125 |
ok = false; |
| 126 |
} |
| 127 |
|
| 128 |
resetTextListener(); |
| 129 |
if (!ok) throw(libmwaw::ParseException()); |
| 130 |
} |
| 131 |
|
| 132 |
//////////////////////////////////////////////////////////// |
| 133 |
// create the document |
| 134 |
//////////////////////////////////////////////////////////// |
| 135 |
void MaxWrtParser::createDocument(librevenge::RVNGTextInterface *documentInterface) |
| 136 |
{ |
| 137 |
if (!documentInterface) return; |
| 138 |
if (getTextListener()) { |
| 139 |
MWAW_DEBUG_MSG(("MaxWrtParser::createDocument: listener already exist\n")); |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
MWAWPageSpan ps(getPageSpan()); |
| 144 |
ps.setPageSpan(1); |
| 145 |
std::vector<MWAWPageSpan> pageList(1,ps); |
| 146 |
// |
| 147 |
MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface)); |
| 148 |
setTextListener(listen); |
| 149 |
listen->startDocument(); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
//////////////////////////////////////////////////////////// |
| 154 |
// |
| 155 |
// Intermediate level |
| 156 |
// |
| 157 |
//////////////////////////////////////////////////////////// |
| 158 |
bool MaxWrtParser::createZones() |
| 159 |
{ |
| 160 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 161 |
auto const &entryMap = rsrcParser->getEntriesMap(); |
| 162 |
|
| 163 |
// the 1 zone |
| 164 |
auto it = entryMap.lower_bound("LnHe"); |
| 165 |
while (it != entryMap.end()) { |
| 166 |
if (it->first != "LnHe") |
| 167 |
break; |
| 168 |
readLineHeight(it++->second); |
| 169 |
} |
| 170 |
it = entryMap.lower_bound("StTB"); |
| 171 |
while (it != entryMap.end()) { |
| 172 |
if (it->first != "StTB") |
| 173 |
break; |
| 174 |
readStyles(it++->second); |
| 175 |
} |
| 176 |
it = entryMap.lower_bound("Styl"); |
| 177 |
while (it != entryMap.end()) { |
| 178 |
if (it->first != "Styl") |
| 179 |
break; |
| 180 |
readStylePLC(it++->second); |
| 181 |
} |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
bool MaxWrtParser::readLineHeight(MWAWEntry const &entry) |
| 186 |
{ |
| 187 |
if (!entry.valid() || (entry.length()%4)) { |
| 188 |
MWAW_DEBUG_MSG(("MaxWrtParser::readLineHeight: the entry is bad\n")); |
| 189 |
return false; |
| 190 |
} |
| 191 |
entry.setParsed(true); |
| 192 |
long pos = entry.begin(); |
| 193 |
MWAWInputStreamPtr input = rsrcInput(); |
| 194 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 195 |
libmwaw::DebugStream f; |
| 196 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 197 |
f << "Entries(LineHeight)[" << entry.id() << "]:"; |
| 198 |
ascFile.addPos(pos-4); |
| 199 |
ascFile.addNote(f.str().c_str()); |
| 200 |
auto N=int(entry.length()/4); |
| 201 |
for (int i=0; i<N; ++i) { |
| 202 |
pos=input->tell(); |
| 203 |
f.str(""); |
| 204 |
f << "LineHeight-" << i << ":"; |
| 205 |
f << "height=" << input->readLong(2) << "x" << input->readLong(2) << ","; |
| 206 |
ascFile.addPos(pos); |
| 207 |
ascFile.addNote(f.str().c_str()); |
| 208 |
} |
| 209 |
return true; |
| 210 |
} |
| 211 |
|
| 212 |
bool MaxWrtParser::readStylePLC(MWAWEntry const &entry) |
| 213 |
{ |
| 214 |
if (!entry.valid() || (entry.length()%4) || entry.length()<20) { |
| 215 |
MWAW_DEBUG_MSG(("MaxWrtParser::readStylePLC: the entry is bad\n")); |
| 216 |
return false; |
| 217 |
} |
| 218 |
entry.setParsed(true); |
| 219 |
long pos = entry.begin(); |
| 220 |
MWAWInputStreamPtr input = rsrcInput(); |
| 221 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 222 |
libmwaw::DebugStream f; |
| 223 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 224 |
f << "Entries(StylPLC)[" << entry.id() << "]:"; |
| 225 |
f << "Num?=" << input->readULong(2) << "x" << input->readULong(2) << ","; |
| 226 |
f << "ids?=[" << std::hex << input->readULong(4) << "," << input->readULong(4) << std::dec << "],"; |
| 227 |
f << "unkn=" << std::hex << input->readULong(4) << std::dec << ","; // c82e0000|aa55aa55|74 |
| 228 |
f << "id2=" << std::hex << input->readULong(4) << std::dec << ","; |
| 229 |
auto N=int((entry.length()-20)/4); |
| 230 |
f << "plcs=["; |
| 231 |
for (int i=0; i<N; ++i) { |
| 232 |
auto cPos=static_cast<int>(input->readULong(2)); |
| 233 |
auto zone=static_cast<int>(input->readLong(2)); |
| 234 |
if (zone==-1) |
| 235 |
f << cPos << ":*,"; |
| 236 |
else { |
| 237 |
m_state->m_posToPLCMap[cPos]=zone; |
| 238 |
f << cPos << ":PLC" << zone << ","; |
| 239 |
} |
| 240 |
} |
| 241 |
f << "],"; |
| 242 |
ascFile.addPos(pos-4); |
| 243 |
ascFile.addNote(f.str().c_str()); |
| 244 |
return true; |
| 245 |
} |
| 246 |
|
| 247 |
bool MaxWrtParser::readStyles(MWAWEntry const &entry) |
| 248 |
{ |
| 249 |
if (!entry.valid() || (entry.length()%18)) { |
| 250 |
MWAW_DEBUG_MSG(("MaxWrtParser::readStyles: the entry is bad\n")); |
| 251 |
return false; |
| 252 |
} |
| 253 |
entry.setParsed(true); |
| 254 |
long pos = entry.begin(); |
| 255 |
MWAWInputStreamPtr input = rsrcInput(); |
| 256 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 257 |
libmwaw::DebugStream f; |
| 258 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 259 |
f << "Entries(StylDef)[" << entry.id() << "]:"; |
| 260 |
ascFile.addPos(pos-4); |
| 261 |
ascFile.addNote(f.str().c_str()); |
| 262 |
auto N=int(entry.length()/18); |
| 263 |
for (int i=0; i<N; ++i) { |
| 264 |
pos=input->tell(); |
| 265 |
f.str(""); |
| 266 |
f << "StylDef-PLC" << i << ":"; |
| 267 |
auto val=static_cast<int>(input->readLong(2)); |
| 268 |
if (val!=1) f << "used?=" << val << ","; |
| 269 |
val=static_cast<int>(input->readLong(2)); |
| 270 |
if (val!=16) f << "f0=" << val << ","; |
| 271 |
MWAWFont font; |
| 272 |
font.setSize(float(input->readULong(2))); |
| 273 |
font.setId(static_cast<int>(input->readULong(2))); |
| 274 |
auto flag = static_cast<int>(input->readULong(1)); |
| 275 |
uint32_t flags=0; |
| 276 |
if (flag&0x1) flags |= MWAWFont::boldBit; |
| 277 |
if (flag&0x2) flags |= MWAWFont::italicBit; |
| 278 |
if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple); |
| 279 |
if (flag&0x8) flags |= MWAWFont::embossBit; |
| 280 |
if (flag&0x10) flags |= MWAWFont::shadowBit; |
| 281 |
if (flag&0x20) font.setDeltaLetterSpacing(-1); |
| 282 |
if (flag&0x40) font.setDeltaLetterSpacing(1); |
| 283 |
if (flag&0x80) f << "#flag0[0x80],"; |
| 284 |
font.setFlags(flags); |
| 285 |
val=static_cast<int>(input->readULong(1)); |
| 286 |
if (val) f << "#flag1=" << std::hex << val << std::dec << ","; |
| 287 |
auto finalSz=static_cast<int>(input->readULong(2)); |
| 288 |
if (finalSz) font.setSize(float(finalSz)); |
| 289 |
unsigned char col[3]; |
| 290 |
for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8); |
| 291 |
font.setColor(MWAWColor(col[0],col[1],col[2])); |
| 292 |
f << font.getDebugString(getParserState()->m_fontConverter); |
| 293 |
if (static_cast<int>(m_state->m_fontList.size()) <= i) |
| 294 |
m_state->m_fontList.resize(size_t(i+1)); |
| 295 |
if (font.id()==0) font.setId(3); |
| 296 |
m_state->m_fontList[size_t(i)]=font; |
| 297 |
ascFile.addPos(pos); |
| 298 |
ascFile.addNote(f.str().c_str()); |
| 299 |
input->seek(pos+18, librevenge::RVNG_SEEK_SET); |
| 300 |
} |
| 301 |
return true; |
| 302 |
} |
| 303 |
|
| 304 |
//////////////////////////////////////////////////////////// |
| 305 |
// send data |
| 306 |
//////////////////////////////////////////////////////////// |
| 307 |
bool MaxWrtParser::sendText() |
| 308 |
{ |
| 309 |
MWAWListenerPtr listener=getTextListener(); |
| 310 |
if (!listener) { |
| 311 |
MWAW_DEBUG_MSG(("MaxWrtParser::sendText: can not find the listener\n")); |
| 312 |
return false; |
| 313 |
} |
| 314 |
MWAWInputStreamPtr input = getInput(); |
| 315 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 316 |
long debPos=0; |
| 317 |
|
| 318 |
libmwaw::DebugStream f; |
| 319 |
f << "Entries(TEXT):"; |
| 320 |
listener->setFont(MWAWFont(3,12)); |
| 321 |
|
| 322 |
long endPos = input->size(); |
| 323 |
for (int i=0; i < static_cast<int>(endPos); i++) { |
| 324 |
bool isEnd = input->isEnd(); |
| 325 |
unsigned char c=isEnd ? static_cast<unsigned char>(0) : static_cast<unsigned char>(input->readULong(1)); |
| 326 |
if (isEnd || c==0xd) { |
| 327 |
ascii().addPos(debPos); |
| 328 |
ascii().addNote(f.str().c_str()); |
| 329 |
debPos = input->tell(); |
| 330 |
if (isEnd) break; |
| 331 |
f.str(""); |
| 332 |
f << "TEXT:"; |
| 333 |
} |
| 334 |
auto plcIt=m_state->m_posToPLCMap.find(i); |
| 335 |
if (plcIt != m_state->m_posToPLCMap.end()) { |
| 336 |
int fontId=m_state->m_posToPLCMap.find(i)->second; |
| 337 |
f << "[PLC" << fontId << "]"; |
| 338 |
if (fontId>=0 && fontId<static_cast<int>(m_state->m_fontList.size())) |
| 339 |
listener->setFont(m_state->m_fontList[size_t(fontId)]); |
| 340 |
else { |
| 341 |
MWAW_DEBUG_MSG(("MaxWrtParser::sendText: can not find a font\n")); |
| 342 |
f << "##"; |
| 343 |
} |
| 344 |
} |
| 345 |
if (c) |
| 346 |
f << c; |
| 347 |
switch (c) { |
| 348 |
case 0x9: |
| 349 |
listener->insertTab(); |
| 350 |
break; |
| 351 |
case 0xd: |
| 352 |
listener->insertEOL(); |
| 353 |
break; |
| 354 |
default: |
| 355 |
if (c < 0x20) f << "##[" << std::hex << int(c) << std::dec << "]"; |
| 356 |
i += listener->insertCharacter(c, input, endPos); |
| 357 |
break; |
| 358 |
} |
| 359 |
} |
| 360 |
return true; |
| 361 |
} |
| 362 |
|
| 363 |
//////////////////////////////////////////////////////////// |
| 364 |
// |
| 365 |
// Low level |
| 366 |
// |
| 367 |
//////////////////////////////////////////////////////////// |
| 368 |
|
| 369 |
//////////////////////////////////////////////////////////// |
| 370 |
// read the header |
| 371 |
//////////////////////////////////////////////////////////// |
| 372 |
bool MaxWrtParser::checkHeader(MWAWHeader *header, bool /*strict*/) |
| 373 |
{ |
| 374 |
*m_state = MaxWrtParserInternal::State(); |
| 375 |
/** no data fork, may be ok, but this means |
| 376 |
that the file contains no text, so... */ |
| 377 |
MWAWInputStreamPtr input = getInput(); |
| 378 |
if (!input || !getRSRCParser() || !input->hasDataFork()) |
| 379 |
return false; |
| 380 |
// check that the style zone exists |
| 381 |
auto const &entryMap = getRSRCParser()->getEntriesMap(); |
| 382 |
if (entryMap.find("Styl") == entryMap.end()) |
| 383 |
return false; |
| 384 |
if (header) |
| 385 |
header->reset(MWAWDocument::MWAW_T_MAXWRITE, version()); |
| 386 |
|
| 387 |
return true; |
| 388 |
} |
| 389 |
|
| 390 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |