| 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 <map> |
| 38 |
#include <sstream> |
| 39 |
|
| 40 |
#include <librevenge/librevenge.h> |
| 41 |
|
| 42 |
#include "MWAWDebug.hxx" |
| 43 |
#include "MWAWFont.hxx" |
| 44 |
#include "MWAWFontConverter.hxx" |
| 45 |
#include "MWAWListener.hxx" |
| 46 |
#include "MWAWParagraph.hxx" |
| 47 |
#include "MWAWParser.hxx" |
| 48 |
#include "MWAWSubDocument.hxx" |
| 49 |
|
| 50 |
#include "MsWksDocument.hxx" |
| 51 |
|
| 52 |
#include "MsWks3Text.hxx" |
| 53 |
|
| 54 |
/** Internal: the structures of a MsWks3Text */ |
| 55 |
namespace MsWks3TextInternal |
| 56 |
{ |
| 57 |
//////////////////////////////////////// |
| 58 |
//! Internal: header zone |
| 59 |
struct LineZone { |
| 60 |
//! the constructor |
| 61 |
LineZone() |
| 62 |
: m_type(-1) |
| 63 |
, m_pos() |
| 64 |
, m_id(0) |
| 65 |
, m_flags() |
| 66 |
, m_height(0) |
| 67 |
{ |
| 68 |
} |
| 69 |
//! operator<< |
| 70 |
friend std::ostream &operator<<(std::ostream &o, LineZone const &z) |
| 71 |
{ |
| 72 |
switch (z.m_type>>5) { |
| 73 |
case 0: |
| 74 |
case 1: |
| 75 |
case 2: |
| 76 |
case 3: |
| 77 |
o << "Text,"; |
| 78 |
break; |
| 79 |
case 4: |
| 80 |
o << "Tabs,"; |
| 81 |
break; |
| 82 |
default: |
| 83 |
o << "##type=" << std::hex << (z.m_type >> 5) << std::dec << ","; |
| 84 |
} |
| 85 |
if (z.m_type&0x8) o << "note,"; |
| 86 |
if (z.m_type&0x7) o << "#type=" << std::hex << (z.m_type&0x7) << std::dec << ","; |
| 87 |
if (z.m_id) o << "id=" << z.m_id << ","; |
| 88 |
if (z.m_flags&1) o << "softBreak,"; |
| 89 |
if (z.m_flags&2) o << "hardBreak,"; |
| 90 |
if (!(z.m_flags&4)) o << "beginDoc,"; |
| 91 |
if (z.m_flags&8) o << "graphics,"; |
| 92 |
if (z.m_flags&0x20) o << "noteFl,"; |
| 93 |
if (z.m_flags&0x40) o << "fields,"; |
| 94 |
if (z.m_flags&0x90) o << "#flags=" << std::hex << (z.m_flags&0x90) << std::dec << ","; |
| 95 |
if (z.m_height) o << "h=" << z.m_height << ","; |
| 96 |
return o; |
| 97 |
} |
| 98 |
//! return true if this is a note |
| 99 |
bool isNote() const |
| 100 |
{ |
| 101 |
return (m_type&0x8)==0x8; |
| 102 |
} |
| 103 |
//! return true if this is a tabs |
| 104 |
bool isRuler() const |
| 105 |
{ |
| 106 |
return (m_type&0xE0)==0x80; |
| 107 |
} |
| 108 |
//! the type |
| 109 |
int m_type; |
| 110 |
//! the file position |
| 111 |
MWAWEntry m_pos; |
| 112 |
//! the id |
| 113 |
int m_id; |
| 114 |
//! the zone flags |
| 115 |
int m_flags; |
| 116 |
//! the height |
| 117 |
int m_height; |
| 118 |
}; |
| 119 |
|
| 120 |
//////////////////////////////////////// |
| 121 |
//! Internal: the fonts |
| 122 |
struct Font { |
| 123 |
//! the constructor |
| 124 |
Font(): m_font(), m_extra("") |
| 125 |
{ |
| 126 |
for (int &flag : m_flags) flag = 0; |
| 127 |
} |
| 128 |
|
| 129 |
//! operator<< |
| 130 |
friend std::ostream &operator<<(std::ostream &o, Font const &font) |
| 131 |
{ |
| 132 |
for (int i = 0; i < 3; i++) { |
| 133 |
if (!font.m_flags[i]) continue; |
| 134 |
o << "ft" << i << "="; |
| 135 |
if (i == 0) o << std::hex; |
| 136 |
o << font.m_flags[i] << std::dec << ","; |
| 137 |
} |
| 138 |
if (font.m_extra.length()) |
| 139 |
o << font.m_extra << ","; |
| 140 |
return o; |
| 141 |
} |
| 142 |
|
| 143 |
//! the font |
| 144 |
MWAWFont m_font; |
| 145 |
//! some unknown flag |
| 146 |
int m_flags[3]; |
| 147 |
//! extra data |
| 148 |
std::string m_extra; |
| 149 |
}; |
| 150 |
|
| 151 |
//////////////////////////////////////// |
| 152 |
//! Internal: the text zone |
| 153 |
struct TextZone { |
| 154 |
enum Type { Header, Footer, Main, Unknown }; |
| 155 |
//! constructor |
| 156 |
TextZone() |
| 157 |
: m_type(Unknown) |
| 158 |
, m_id(0) |
| 159 |
, m_zonesList() |
| 160 |
, m_linesHeight() |
| 161 |
, m_pagesHeight() |
| 162 |
, m_pagesPosition() |
| 163 |
, m_footnoteMap() |
| 164 |
, m_text("") |
| 165 |
, m_isSent(false) |
| 166 |
{ |
| 167 |
} |
| 168 |
|
| 169 |
//! return true if this is the main zone |
| 170 |
bool isMain() const |
| 171 |
{ |
| 172 |
return m_type == Main; |
| 173 |
} |
| 174 |
//! the zone type; |
| 175 |
int m_type; |
| 176 |
//! the zone id |
| 177 |
int m_id; |
| 178 |
//! the list of zones |
| 179 |
std::vector<LineZone> m_zonesList; |
| 180 |
//! the line height |
| 181 |
std::vector<int> m_linesHeight; |
| 182 |
//! the pages height |
| 183 |
std::vector<int> m_pagesHeight; |
| 184 |
//! the zone id -> hard break |
| 185 |
std::map<int, bool> m_pagesPosition; |
| 186 |
//! the note id -> zone limit |
| 187 |
std::map<int, MWAWVec2i> m_footnoteMap; |
| 188 |
//! a string used to store v1-2 files header/footer |
| 189 |
std::string m_text; |
| 190 |
//! flag to know if the zone is send or not |
| 191 |
bool m_isSent; |
| 192 |
}; |
| 193 |
|
| 194 |
//////////////////////////////////////// |
| 195 |
//! Internal: the state of a MsWks3Text |
| 196 |
struct State { |
| 197 |
//! constructor |
| 198 |
State() |
| 199 |
: m_version(-1) |
| 200 |
, m_zones() |
| 201 |
, m_numPages(1) |
| 202 |
, m_actualPage(1) |
| 203 |
{ |
| 204 |
} |
| 205 |
//! the file version |
| 206 |
mutable int m_version; |
| 207 |
//! the main zone |
| 208 |
std::vector<TextZone> m_zones; |
| 209 |
|
| 210 |
int m_numPages /* the number of pages */, m_actualPage /* the actual page */; |
| 211 |
}; |
| 212 |
|
| 213 |
//////////////////////////////////////// |
| 214 |
//! Internal: the subdocument of a MsWks3Text |
| 215 |
class SubDocument final : public MWAWSubDocument |
| 216 |
{ |
| 217 |
public: |
| 218 |
enum Type { Zone, Text }; |
| 219 |
SubDocument(MsWks3Text &pars, MWAWInputStreamPtr const &input, int zoneId, int noteId) |
| 220 |
: MWAWSubDocument(pars.m_mainParser, input, MWAWEntry()) |
| 221 |
, m_textParser(&pars) |
| 222 |
, m_id(zoneId) |
| 223 |
, m_noteId(noteId) |
| 224 |
{ |
| 225 |
} |
| 226 |
|
| 227 |
//! destructor |
| 228 |
~SubDocument() final {} |
| 229 |
|
| 230 |
//! operator!= |
| 231 |
bool operator!=(MWAWSubDocument const &doc) const final; |
| 232 |
|
| 233 |
//! the parser function |
| 234 |
void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final; |
| 235 |
|
| 236 |
protected: |
| 237 |
/** the text parser */ |
| 238 |
MsWks3Text *m_textParser; |
| 239 |
/** the subdocument id*/ |
| 240 |
int m_id; |
| 241 |
/** the note id */ |
| 242 |
int m_noteId; |
| 243 |
private: |
| 244 |
SubDocument(SubDocument const &orig) = delete; |
| 245 |
SubDocument operator=(SubDocument const &orig) = delete; |
| 246 |
}; |
| 247 |
|
| 248 |
void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/) |
| 249 |
{ |
| 250 |
if (!listener.get()) { |
| 251 |
MWAW_DEBUG_MSG(("MsWks3TextInternal::SubDocument::parse: no listener\n")); |
| 252 |
return; |
| 253 |
} |
| 254 |
if (!m_textParser) { |
| 255 |
MWAW_DEBUG_MSG(("MsWks3TextInternal::SubDocument::parse: no parser\n")); |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
long pos = m_input->tell(); |
| 260 |
m_textParser->sendNote(m_id, m_noteId); |
| 261 |
m_input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 262 |
} |
| 263 |
|
| 264 |
bool SubDocument::operator!=(MWAWSubDocument const &doc) const |
| 265 |
{ |
| 266 |
if (MWAWSubDocument::operator!=(doc)) return true; |
| 267 |
auto const *sDoc = dynamic_cast<SubDocument const *>(&doc); |
| 268 |
if (!sDoc) return true; |
| 269 |
if (m_textParser != sDoc->m_textParser) return true; |
| 270 |
if (m_id != sDoc->m_id) return true; |
| 271 |
if (m_noteId != sDoc->m_noteId) return true; |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
//////////////////////////////////////////////////////////// |
| 278 |
// constructor/destructor, ... |
| 279 |
//////////////////////////////////////////////////////////// |
| 280 |
MsWks3Text::MsWks3Text(MsWksDocument &document) |
| 281 |
: m_parserState() |
| 282 |
, m_state(new MsWks3TextInternal::State) |
| 283 |
, m_mainParser(&document.getMainParser()) |
| 284 |
, m_document(document) |
| 285 |
{ |
| 286 |
m_parserState=m_mainParser->getParserState(); |
| 287 |
} |
| 288 |
|
| 289 |
MsWks3Text::~MsWks3Text() |
| 290 |
{ |
| 291 |
} |
| 292 |
|
| 293 |
int MsWks3Text::version() const |
| 294 |
{ |
| 295 |
if (m_state->m_version < 0) |
| 296 |
m_state->m_version = m_parserState->m_version; |
| 297 |
return m_state->m_version; |
| 298 |
} |
| 299 |
|
| 300 |
int MsWks3Text::numPages(int zoneId) const |
| 301 |
{ |
| 302 |
if (zoneId < 0 || zoneId >= int(m_state->m_zones.size())) { |
| 303 |
MWAW_DEBUG_MSG(("MsWks3Text::numPages: unknown zone %d\n", zoneId)); |
| 304 |
return 0; |
| 305 |
} |
| 306 |
auto const &zone = m_state->m_zones[size_t(zoneId)]; |
| 307 |
int nPages = 1 + int(zone.m_pagesPosition.size()); |
| 308 |
if (zone.isMain()) { |
| 309 |
m_state->m_actualPage = 1; |
| 310 |
m_state->m_numPages = nPages; |
| 311 |
} |
| 312 |
else { |
| 313 |
MWAW_DEBUG_MSG(("MsWks3Text::numPages: called on no main zone: %d\n", zoneId)); |
| 314 |
} |
| 315 |
return nPages; |
| 316 |
} |
| 317 |
|
| 318 |
bool MsWks3Text::getLinesPagesHeight |
| 319 |
(int zoneId, std::vector<int> &lines, std::vector<int> &pages) |
| 320 |
{ |
| 321 |
lines.resize(0); |
| 322 |
pages.resize(0); |
| 323 |
if (zoneId < 0 || zoneId >= int(m_state->m_zones.size())) { |
| 324 |
MWAW_DEBUG_MSG(("MsWks3Text::getLinesPagesHeight: unknown zone %d\n", zoneId)); |
| 325 |
return false; |
| 326 |
} |
| 327 |
lines = m_state->m_zones[size_t(zoneId)].m_linesHeight; |
| 328 |
pages = m_state->m_zones[size_t(zoneId)].m_pagesHeight; |
| 329 |
return true; |
| 330 |
} |
| 331 |
|
| 332 |
//////////////////////////////////////////////////////////// |
| 333 |
// Intermediate level |
| 334 |
//////////////////////////////////////////////////////////// |
| 335 |
int MsWks3Text::getHeader() const |
| 336 |
{ |
| 337 |
for (size_t i = 0; i < m_state->m_zones.size(); i++) |
| 338 |
if (m_state->m_zones[i].m_type == MsWks3TextInternal::TextZone::Header) |
| 339 |
return int(i); |
| 340 |
return -1; |
| 341 |
} |
| 342 |
|
| 343 |
int MsWks3Text::getFooter() const |
| 344 |
{ |
| 345 |
for (size_t i = 0; i < m_state->m_zones.size(); i++) |
| 346 |
if (m_state->m_zones[i].m_type == MsWks3TextInternal::TextZone::Footer) |
| 347 |
return int(i); |
| 348 |
return -1; |
| 349 |
} |
| 350 |
|
| 351 |
//////////////////////////////////////////////////////////// |
| 352 |
// try to find the different zone |
| 353 |
//////////////////////////////////////////////////////////// |
| 354 |
int MsWks3Text::createZones(int numLines, bool mainZone) |
| 355 |
{ |
| 356 |
MsWks3TextInternal::LineZone zone; |
| 357 |
auto zoneId = int(m_state->m_zones.size()); |
| 358 |
m_state->m_zones.push_back(MsWks3TextInternal::TextZone()); |
| 359 |
auto &actualZone = m_state->m_zones.back(); |
| 360 |
actualZone.m_id = zoneId; |
| 361 |
if (mainZone) |
| 362 |
actualZone.m_type = MsWks3TextInternal::TextZone::Main; |
| 363 |
bool hasNote=false; |
| 364 |
int firstNote=0; |
| 365 |
MWAWInputStreamPtr input=m_document.getInput(); |
| 366 |
while (!input->isEnd()) { |
| 367 |
if (numLines==0) break; |
| 368 |
if (numLines>0) numLines--; |
| 369 |
long pos = input->tell(); |
| 370 |
if (!readZoneHeader(zone)) { |
| 371 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 372 |
break; |
| 373 |
} |
| 374 |
if (!hasNote && zone.isNote()) { |
| 375 |
firstNote = int(actualZone.m_zonesList.size()); |
| 376 |
hasNote = true; |
| 377 |
} |
| 378 |
actualZone.m_zonesList.push_back(zone); |
| 379 |
input->seek(zone.m_pos.end(), librevenge::RVNG_SEEK_SET); |
| 380 |
} |
| 381 |
auto numLineZones = int(actualZone.m_zonesList.size()); |
| 382 |
if (numLineZones == 0) { |
| 383 |
m_state->m_zones.pop_back(); |
| 384 |
return -1; |
| 385 |
} |
| 386 |
|
| 387 |
update(actualZone); |
| 388 |
if (hasNote) |
| 389 |
updateNotes(actualZone, firstNote); |
| 390 |
return zoneId; |
| 391 |
} |
| 392 |
|
| 393 |
void MsWks3Text::update(MsWks3TextInternal::TextZone &zone) |
| 394 |
{ |
| 395 |
size_t numLineZones = zone.m_zonesList.size(); |
| 396 |
if (numLineZones == 0) return; |
| 397 |
|
| 398 |
auto textHeight = int(72.0*m_mainParser->getPageSpan().getPageLength()); |
| 399 |
|
| 400 |
int actH = 0, actualPH = 0; |
| 401 |
zone.m_linesHeight.push_back(0); |
| 402 |
for (size_t i = 0; i < numLineZones; i++) { |
| 403 |
auto const &z = zone.m_zonesList[i]; |
| 404 |
if (z.isNote()) continue; // a note |
| 405 |
actH += z.m_height; |
| 406 |
zone.m_linesHeight.push_back(actH); |
| 407 |
bool newPage = ((z.m_flags&1) && actualPH) || (z.m_flags&2); |
| 408 |
actualPH += z.m_height; |
| 409 |
if (newPage || (actualPH > textHeight && textHeight > 0)) { |
| 410 |
zone.m_pagesPosition[int(i)]=(z.m_flags&2); |
| 411 |
zone.m_pagesHeight.push_back(actualPH-z.m_height); |
| 412 |
actualPH=z.m_height; |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
void MsWks3Text::updateNotes(MsWks3TextInternal::TextZone &zone, int firstNote) |
| 418 |
{ |
| 419 |
auto numLineZones = int(zone.m_zonesList.size()); |
| 420 |
if (firstNote < 0 || firstNote >= numLineZones) { |
| 421 |
MWAW_DEBUG_MSG(("MsWks3Text::updateNotes: can not find first note position\n")); |
| 422 |
return; |
| 423 |
} |
| 424 |
|
| 425 |
MWAWInputStreamPtr input=m_document.getInput(); |
| 426 |
MsWks3TextInternal::Font font; |
| 427 |
int noteId = -1; |
| 428 |
long lastIndentPos = -1; |
| 429 |
MWAWVec2i notePos; |
| 430 |
|
| 431 |
for (int n = firstNote; n < numLineZones; n++) { |
| 432 |
auto const &z = zone.m_zonesList[size_t(n)]; |
| 433 |
if (!z.isNote()) { |
| 434 |
noteId=-1; |
| 435 |
MWAW_DEBUG_MSG(("MsWks3Text::updateNotes: find extra data in notes, stop recording note\n")); |
| 436 |
break; |
| 437 |
} |
| 438 |
if (z.isRuler()) { |
| 439 |
lastIndentPos = n; |
| 440 |
continue; |
| 441 |
} |
| 442 |
if (z.m_pos.length() < 8) continue; |
| 443 |
long actPos = z.m_pos.begin(); |
| 444 |
input->seek(actPos+6,librevenge::RVNG_SEEK_SET); |
| 445 |
|
| 446 |
auto c = static_cast<int>(input->readULong(1)); |
| 447 |
if ((c == 1 || c == 2) && readFont(font, z.m_pos.end())) { |
| 448 |
if (long(input->tell())+2 > z.m_pos.end()) |
| 449 |
continue; |
| 450 |
c = static_cast<int>(input->readULong(1)); |
| 451 |
if (c <= 4) { |
| 452 |
if (long(input->tell())+2 > z.m_pos.end()) |
| 453 |
continue; |
| 454 |
c = static_cast<int>(input->readULong(1)); |
| 455 |
} |
| 456 |
} |
| 457 |
if (c != 0x14) continue; |
| 458 |
if (noteId >= 0) { |
| 459 |
notePos[1] = (lastIndentPos != -1) ? int(lastIndentPos) : n; |
| 460 |
if (zone.m_footnoteMap.find(noteId)==zone.m_footnoteMap.end()) |
| 461 |
zone.m_footnoteMap[noteId] = notePos; |
| 462 |
else { |
| 463 |
MWAW_DEBUG_MSG(("MsWks3Text::updateNotes: note %d is already defined, ignored\n", noteId)); |
| 464 |
} |
| 465 |
} |
| 466 |
noteId = static_cast<int>(input->readULong(2)); |
| 467 |
notePos[0] = (lastIndentPos != -1) ? int(lastIndentPos) : n; |
| 468 |
lastIndentPos = -1; |
| 469 |
} |
| 470 |
if (noteId >= 0) { |
| 471 |
notePos[1] = numLineZones; |
| 472 |
if (zone.m_footnoteMap.find(noteId)==zone.m_footnoteMap.end()) |
| 473 |
zone.m_footnoteMap[noteId] = notePos; |
| 474 |
else { |
| 475 |
MWAW_DEBUG_MSG(("MsWks3Text::updateNotes: note %d is already defined, ignored\n", noteId)); |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
bool MsWks3Text::readZoneHeader(MsWks3TextInternal::LineZone &zone) const |
| 481 |
{ |
| 482 |
zone = MsWks3TextInternal::LineZone(); |
| 483 |
MWAWInputStreamPtr input=m_document.getInput(); |
| 484 |
long pos = input->tell(); |
| 485 |
if (!input->checkPosition(pos+6)) return false; |
| 486 |
zone.m_pos.setBegin(pos); |
| 487 |
zone.m_type = static_cast<int>(input->readULong(1)); |
| 488 |
if (zone.m_type & 0x17) return false; |
| 489 |
zone.m_id = static_cast<int>(input->readULong(1)); |
| 490 |
zone.m_flags = static_cast<int>(input->readULong(1)); |
| 491 |
zone.m_height = static_cast<int>(input->readULong(1)); |
| 492 |
zone.m_pos.setLength(6+long(input->readULong(2))); |
| 493 |
if (!input->checkPosition(zone.m_pos.end())) return false; |
| 494 |
return true; |
| 495 |
} |
| 496 |
|
| 497 |
//////////////////////////////////////////////////////////// |
| 498 |
// the text: |
| 499 |
//////////////////////////////////////////////////////////// |
| 500 |
bool MsWks3Text::sendText(MsWks3TextInternal::LineZone &zone, int zoneId) |
| 501 |
{ |
| 502 |
MWAWListenerPtr listener=m_parserState->getMainListener(); |
| 503 |
if (!listener) { |
| 504 |
MWAW_DEBUG_MSG(("MsWks3Text::sendText: can not find the listener\n")); |
| 505 |
return true; |
| 506 |
} |
| 507 |
MWAWInputStreamPtr input=m_document.getInput(); |
| 508 |
input->seek(zone.m_pos.begin()+6, librevenge::RVNG_SEEK_SET); |
| 509 |
int vers = version(); |
| 510 |
libmwaw::DebugFile &ascFile = m_document.ascii(); |
| 511 |
libmwaw::DebugStream f; |
| 512 |
f << "Entries(TextZone):" << zone << ","; |
| 513 |
MsWks3TextInternal::Font font; |
| 514 |
if (zone.m_height > 0) { |
| 515 |
MWAWParagraph para=listener->getParagraph(); |
| 516 |
para.setInterline(zone.m_height, librevenge::RVNG_POINT); |
| 517 |
listener->setParagraph(para); |
| 518 |
} |
| 519 |
while (!input->isEnd()) { |
| 520 |
long pos = input->tell(); |
| 521 |
if (pos >= zone.m_pos.end()) break; |
| 522 |
auto c = static_cast<int>(input->readULong(1)); |
| 523 |
if ((c == 1 || c == 2) && readFont(font, zone.m_pos.end())) { |
| 524 |
listener->setFont(font.m_font); |
| 525 |
f << "[" << font.m_font.getDebugString(m_parserState->m_fontConverter) << font << "]"; |
| 526 |
continue; |
| 527 |
} |
| 528 |
if (c == 0) { |
| 529 |
f << "#"; |
| 530 |
continue; |
| 531 |
} |
| 532 |
f << char(c); |
| 533 |
switch (c) { |
| 534 |
case 0x9: |
| 535 |
listener->insertTab(); |
| 536 |
break; |
| 537 |
case 0x10: // cursor pos |
| 538 |
case 0x11: |
| 539 |
break; |
| 540 |
default: |
| 541 |
if (c >= 0x14 && c <= 0x19 && vers >= 3) { |
| 542 |
int sz = (c==0x19) ? 0 : (c == 0x18) ? 1 : 2; |
| 543 |
int id = (sz && pos+1+sz <= zone.m_pos.end()) ? int(input->readLong(sz)) : 0; |
| 544 |
if (id) f << "[" << id << "]"; |
| 545 |
switch (c) { |
| 546 |
case 0x19: |
| 547 |
listener->insertField(MWAWField(MWAWField::Title)); |
| 548 |
break; |
| 549 |
case 0x18: |
| 550 |
listener->insertField(MWAWField(MWAWField::PageNumber)); |
| 551 |
break; |
| 552 |
case 0x16: |
| 553 |
listener->insertField(MWAWField(MWAWField::Time)); |
| 554 |
break; |
| 555 |
case 0x17: // id = 0 : short date ; id=9 : long date |
| 556 |
listener->insertField(MWAWField(MWAWField::Date)); |
| 557 |
break; |
| 558 |
case 0x15: |
| 559 |
MWAW_DEBUG_MSG(("MsWks3Text::sendText: find unknown field type 0x15\n")); |
| 560 |
break; |
| 561 |
case 0x14: |
| 562 |
if (!zone.isNote()) { |
| 563 |
MWAWSubDocumentPtr subdoc |
| 564 |
(new MsWks3TextInternal::SubDocument(*this, m_document.getInput(), zoneId, id)); |
| 565 |
listener->insertNote(MWAWNote(MWAWNote::FootNote), subdoc); |
| 566 |
} |
| 567 |
break; |
| 568 |
default: |
| 569 |
break; |
| 570 |
} |
| 571 |
} |
| 572 |
else if (c <= 0x1f) { |
| 573 |
f << "#" << std::hex << c << std::dec << "]"; |
| 574 |
ascFile.addDelimiter(pos,'#'); |
| 575 |
MWAW_DEBUG_MSG(("MsWks3Text::sendText: find char=%x\n", static_cast<unsigned int>(c))); |
| 576 |
} |
| 577 |
else |
| 578 |
listener->insertCharacter(static_cast<unsigned char>(c), input, zone.m_pos.end()); |
| 579 |
break; |
| 580 |
} |
| 581 |
} |
| 582 |
if (listener) |
| 583 |
listener->insertEOL(); |
| 584 |
ascFile.addPos(zone.m_pos.begin()); |
| 585 |
ascFile.addNote(f.str().c_str()); |
| 586 |
return true; |
| 587 |
} |
| 588 |
|
| 589 |
bool MsWks3Text::sendString(std::string &str) |
| 590 |
{ |
| 591 |
MWAWListenerPtr listener=m_parserState->getMainListener(); |
| 592 |
if (!listener) |
| 593 |
return true; |
| 594 |
MsWks3TextInternal::Font defFont; |
| 595 |
defFont.m_font = MWAWFont(20,12); |
| 596 |
listener->setFont(defFont.m_font); |
| 597 |
|
| 598 |
for (auto c : str) { |
| 599 |
switch (c) { |
| 600 |
case 0x9: |
| 601 |
listener->insertTab(); |
| 602 |
break; |
| 603 |
case 0x10: // cursor pos |
| 604 |
case 0x11: |
| 605 |
break; |
| 606 |
case 0x19: |
| 607 |
listener->insertField(MWAWField(MWAWField::Title)); |
| 608 |
break; |
| 609 |
case 0x18: |
| 610 |
listener->insertField(MWAWField(MWAWField::PageNumber)); |
| 611 |
break; |
| 612 |
case 0x16: |
| 613 |
listener->insertField(MWAWField(MWAWField::Time)); |
| 614 |
break; |
| 615 |
case 0x17: // id = 0 : short date ; id=9 : long date |
| 616 |
listener->insertField(MWAWField(MWAWField::Date)); |
| 617 |
break; |
| 618 |
case 0x15: |
| 619 |
MWAW_DEBUG_MSG(("MsWks3Text::sendString: find unknown field type 0x15\n")); |
| 620 |
break; |
| 621 |
case 0x14: // fixme |
| 622 |
MWAW_DEBUG_MSG(("MsWks3Text::sendString: footnote are not implemented\n")); |
| 623 |
break; |
| 624 |
default: |
| 625 |
listener->insertCharacter(static_cast<unsigned char>(c)); |
| 626 |
break; |
| 627 |
} |
| 628 |
} |
| 629 |
return true; |
| 630 |
} |
| 631 |
|
| 632 |
//////////////////////////////////////////////////////////// |
| 633 |
// the font: |
| 634 |
//////////////////////////////////////////////////////////// |
| 635 |
bool MsWks3Text::readFont(MsWks3TextInternal::Font &font, long endPos) |
| 636 |
{ |
| 637 |
int vers = version(); |
| 638 |
font = MsWks3TextInternal::Font(); |
| 639 |
MWAWInputStreamPtr input=m_document.getInput(); |
| 640 |
long pos = input->tell(); |
| 641 |
input->seek(-1, librevenge::RVNG_SEEK_CUR); |
| 642 |
auto type = static_cast<int>(input->readLong(1)); |
| 643 |
if ((type != 1 && type != 2) || pos+type+3 > endPos) { |
| 644 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 645 |
return false; |
| 646 |
} |
| 647 |
libmwaw::DebugStream f; |
| 648 |
auto flag = static_cast<int>(input->readULong(1)); // check or font ? |
| 649 |
if (flag) f << "#f0=" << flag << ","; |
| 650 |
font.m_font.setId(static_cast<int>(input->readULong(1))); |
| 651 |
font.m_font.setSize(float(input->readULong(1))); |
| 652 |
flag = static_cast<int>(input->readULong(1)); |
| 653 |
uint32_t flags = 0; |
| 654 |
if (flag & 0x1) flags |= MWAWFont::boldBit; |
| 655 |
if (flag & 0x2) flags |= MWAWFont::italicBit; |
| 656 |
if (flag & 0x4) font.m_font.setUnderlineStyle(MWAWFont::Line::Simple); |
| 657 |
if (flag & 0x8) flags |= MWAWFont::embossBit; |
| 658 |
if (flag & 0x10) flags |= MWAWFont::shadowBit; |
| 659 |
if (flag & 0x20) { |
| 660 |
if (vers==1) |
| 661 |
font.m_font.set(MWAWFont::Script(20,librevenge::RVNG_PERCENT,80)); |
| 662 |
else |
| 663 |
font.m_font.set(MWAWFont::Script::super100()); |
| 664 |
} |
| 665 |
if (flag & 0x40) { |
| 666 |
if (vers==1) |
| 667 |
font.m_font.set(MWAWFont::Script(-20,librevenge::RVNG_PERCENT,80)); |
| 668 |
else |
| 669 |
font.m_font.set(MWAWFont::Script::sub100()); |
| 670 |
} |
| 671 |
if ((flag & 0x80) && !(flag & 0x60)) f << "fFl80#,"; |
| 672 |
font.m_font.setFlags(flags); |
| 673 |
int color = 1; |
| 674 |
if (type == 2) { |
| 675 |
color=static_cast<int>(input->readULong(1)); |
| 676 |
} |
| 677 |
else if (pos+type+5 <= endPos) { |
| 678 |
auto val = static_cast<int>(input->readULong(1)); |
| 679 |
if (val == 0) |
| 680 |
f << "end0#,"; |
| 681 |
else |
| 682 |
input->seek(-1, librevenge::RVNG_SEEK_CUR); |
| 683 |
} |
| 684 |
if (color != 1) { |
| 685 |
MWAWColor col; |
| 686 |
if (m_document.getColor(color,col,vers)) |
| 687 |
font.m_font.setColor(col); |
| 688 |
else |
| 689 |
f << "#fColor=" << color << ","; |
| 690 |
} |
| 691 |
font.m_extra = f.str(); |
| 692 |
return true; |
| 693 |
} |
| 694 |
|
| 695 |
//////////////////////////////////////////////////////////// |
| 696 |
// the tabulations: |
| 697 |
//////////////////////////////////////////////////////////// |
| 698 |
bool MsWks3Text::readParagraph(MsWks3TextInternal::LineZone &zone, MWAWParagraph ¶g) |
| 699 |
{ |
| 700 |
int dataSize = int(zone.m_pos.length())-6; |
| 701 |
if (dataSize < 15) return false; |
| 702 |
MWAWInputStreamPtr input=m_document.getInput(); |
| 703 |
input->seek(zone.m_pos.begin()+6, librevenge::RVNG_SEEK_SET); |
| 704 |
|
| 705 |
parag = MWAWParagraph(); |
| 706 |
libmwaw::DebugFile &ascFile = m_document.ascii(); |
| 707 |
libmwaw::DebugStream f; |
| 708 |
|
| 709 |
int fl[2]; |
| 710 |
bool firstFlag = (dataSize & 1) == 0; |
| 711 |
fl[0] = firstFlag ? static_cast<int>(input->readULong(1)) : 0x4c; |
| 712 |
switch (fl[0]) { |
| 713 |
case 0x4c: |
| 714 |
break; |
| 715 |
case 0x43: |
| 716 |
parag.m_justify = MWAWParagraph::JustificationCenter; |
| 717 |
break; |
| 718 |
case 0x52: |
| 719 |
parag.m_justify = MWAWParagraph::JustificationRight; |
| 720 |
break; |
| 721 |
case 0x46: |
| 722 |
parag.m_justify = MWAWParagraph::JustificationFull; |
| 723 |
break; |
| 724 |
default: |
| 725 |
f << "#align=" << std::hex << fl[0] << ","; |
| 726 |
MWAW_DEBUG_MSG(("MsWks3Text::readParagraph: unknown alignment %x\n", static_cast<unsigned int>(fl[0]))); |
| 727 |
break; |
| 728 |
} |
| 729 |
fl[1] = static_cast<int>(input->readULong(1)); |
| 730 |
if (fl[1]) |
| 731 |
f << "fl0=" <<fl[1] << std::dec << ","; |
| 732 |
int dim[3]; |
| 733 |
bool ok = true; |
| 734 |
for (int i = 0; i < 3; i++) { |
| 735 |
dim[i] = static_cast<int>(input->readULong(2)); |
| 736 |
if (i==0&&(dim[0]&0x8000)) { |
| 737 |
dim[0] &= 0x7FFF; |
| 738 |
f << "6linesByInches,"; |
| 739 |
} |
| 740 |
if (dim[i] > 3000) ok = false; |
| 741 |
} |
| 742 |
if (!ok) { |
| 743 |
MWAW_DEBUG_MSG(("MsWks3Text::readParagraph: size is very odd\n")); |
| 744 |
f << "##"; |
| 745 |
} |
| 746 |
if (dim[0] || dim[1] || dim[2]) { |
| 747 |
f << "size=" << dim[1] << "x" << dim[0]; |
| 748 |
if (dim[2]) f << "[" << dim[2] << "]"; |
| 749 |
f << ","; |
| 750 |
} |
| 751 |
|
| 752 |
int fl2[2]; |
| 753 |
for (auto &flag : fl2) // between -16 and 16 |
| 754 |
flag = static_cast<int>(input->readULong(1)); |
| 755 |
if (fl2[0] || fl2[1]) |
| 756 |
f << "fl2=(" << std::hex << fl2[0] << ", " <<fl2[1] << ")" << std::dec << ","; |
| 757 |
|
| 758 |
for (int i = 0; i < 3; i++) { |
| 759 |
auto val = static_cast<int>(input->readULong(2)); |
| 760 |
int flag = (val & 0xc000) >> 14; |
| 761 |
val = (val & 0x3fff); |
| 762 |
if (val > 8000 || flag) { |
| 763 |
MWAW_DEBUG_MSG(("MsWks3Text::readParagraph: find odd margin %d\n", i)); |
| 764 |
f << "#margin" << i << "=" << val << "(" << flag << "),"; |
| 765 |
} |
| 766 |
if (val > 8000) continue; |
| 767 |
// i = 0 (last), i = 1 (firstL), i=2 (nextL) |
| 768 |
parag.m_margins[2-i] = val/72.0; |
| 769 |
} |
| 770 |
*(parag.m_margins[0]) -= *(parag.m_margins[1]); |
| 771 |
if (parag.m_margins[2].get() > 0.0) |
| 772 |
parag.m_margins[2] = m_mainParser->getPageWidth()-*(parag.m_margins[2]); |
| 773 |
if (parag.m_margins[2].get() > 56./72.) *(parag.m_margins[2]) -= 28./72.; |
| 774 |
else if (parag.m_margins[2].get() >=0.0) *(parag.m_margins[2]) *= 0.5; |
| 775 |
else parag.m_margins[2] = 0.0; |
| 776 |
int numVal = (dataSize-9)/2-3; |
| 777 |
parag.m_tabs->resize(size_t(numVal)); |
| 778 |
size_t numTabs = 0; |
| 779 |
|
| 780 |
// checkme: in order to avoid x_tabs > textWidth (appears sometimes when i=0) |
| 781 |
auto maxWidth = long(m_mainParser->getPageWidth()*72-36); |
| 782 |
if (dim[1] > maxWidth) maxWidth = dim[1]; |
| 783 |
|
| 784 |
for (int i = 0; i < numVal; i++) { |
| 785 |
auto val = static_cast<int>(input->readULong(2)); |
| 786 |
MWAWTabStop::Alignment align = MWAWTabStop::LEFT; |
| 787 |
switch (val >> 14) { |
| 788 |
case 1: |
| 789 |
align = MWAWTabStop::DECIMAL; |
| 790 |
break; |
| 791 |
case 2: |
| 792 |
align = MWAWTabStop::RIGHT; |
| 793 |
break; |
| 794 |
case 3: |
| 795 |
align = MWAWTabStop::CENTER; |
| 796 |
break; |
| 797 |
default: |
| 798 |
break; |
| 799 |
} |
| 800 |
val = (val & 0x3fff); |
| 801 |
if (val > maxWidth) { |
| 802 |
static bool first = true; |
| 803 |
if (first) { |
| 804 |
MWAW_DEBUG_MSG(("MsWks3Text::readParagraph: finds some odd tabs (ignored)\n")); |
| 805 |
first = false; |
| 806 |
} |
| 807 |
f << "#tabs" << i << "=" << val << ","; |
| 808 |
continue; |
| 809 |
} |
| 810 |
(*parag.m_tabs)[numTabs].m_alignment = align; |
| 811 |
(*parag.m_tabs)[numTabs++].m_position = val/72.0; |
| 812 |
} |
| 813 |
if (int(numTabs)!=numVal) parag.m_tabs->resize(numTabs); |
| 814 |
parag.m_extra = f.str(); |
| 815 |
|
| 816 |
f.str(""); |
| 817 |
f << "Entries(Paragraph):" << zone << "," << parag << ","; |
| 818 |
ascFile.addPos(zone.m_pos.begin()); |
| 819 |
ascFile.addNote(f.str().c_str()); |
| 820 |
|
| 821 |
return true; |
| 822 |
} |
| 823 |
|
| 824 |
//////////////////////////////////////////////////////////// |
| 825 |
// read v1-v2 header/footer string |
| 826 |
//////////////////////////////////////////////////////////// |
| 827 |
std::string MsWks3Text::readHeaderFooterString(bool header) |
| 828 |
{ |
| 829 |
std::string res(""); |
| 830 |
MWAWInputStreamPtr input=m_document.getInput(); |
| 831 |
auto numChar = static_cast<int>(input->readULong(1)); |
| 832 |
if (!numChar) return res; |
| 833 |
if (!input->checkPosition(input->tell()+numChar)) { |
| 834 |
MWAW_DEBUG_MSG(("MsWks3Text::readHeaderFooterString: the number of char seems bad\n")); |
| 835 |
return res; |
| 836 |
} |
| 837 |
for (int i = 0; i < numChar; i++) { |
| 838 |
auto c = static_cast<unsigned char>(input->readULong(1)); |
| 839 |
if (c == 0) { |
| 840 |
input->seek(-1, librevenge::RVNG_SEEK_CUR); |
| 841 |
break; |
| 842 |
} |
| 843 |
if (c == '&') { |
| 844 |
auto nextC = static_cast<unsigned char>(input->readULong(1)); |
| 845 |
bool field = true; |
| 846 |
switch (nextC) { |
| 847 |
case 'F': |
| 848 |
res += char(0x19); |
| 849 |
break; // file name |
| 850 |
case 'D': |
| 851 |
res += char(0x17); |
| 852 |
break; // date |
| 853 |
case 'P': |
| 854 |
res += char(0x18); |
| 855 |
break; // page |
| 856 |
case 'T': |
| 857 |
res += char(0x16); |
| 858 |
break; // time |
| 859 |
default: |
| 860 |
field = false; |
| 861 |
} |
| 862 |
if (field) continue; |
| 863 |
input->seek(-1, librevenge::RVNG_SEEK_CUR); |
| 864 |
} |
| 865 |
res += char(c); |
| 866 |
} |
| 867 |
if (res.length()) { |
| 868 |
m_state->m_zones.push_back(MsWks3TextInternal::TextZone()); |
| 869 |
auto &zone = m_state->m_zones.back(); |
| 870 |
zone.m_id = int(m_state->m_zones.size())-1; |
| 871 |
zone.m_type = header ? MsWks3TextInternal::TextZone::Header : |
| 872 |
MsWks3TextInternal::TextZone::Footer; |
| 873 |
zone.m_text = res; |
| 874 |
} |
| 875 |
return res; |
| 876 |
} |
| 877 |
|
| 878 |
//////////////////////////////////////////////////////////// |
| 879 |
// |
| 880 |
// Low level |
| 881 |
// |
| 882 |
//////////////////////////////////////////////////////////// |
| 883 |
void MsWks3Text::send(MsWks3TextInternal::TextZone &zone, MWAWVec2i limit) |
| 884 |
{ |
| 885 |
auto numZones = int(zone.m_zonesList.size()); |
| 886 |
// set the default font |
| 887 |
if (m_parserState->getMainListener()) |
| 888 |
m_parserState->getMainListener()->setFont(MWAWFont(20,12)); |
| 889 |
if (numZones == 0 && zone.m_text.length()) { |
| 890 |
sendString(zone.m_text); |
| 891 |
zone.m_isSent = true; |
| 892 |
return; |
| 893 |
} |
| 894 |
bool isMain = false; |
| 895 |
MWAWVec2i notePos(-1,-1); |
| 896 |
if (limit[0] < 0) { |
| 897 |
limit = MWAWVec2i(0,numZones); |
| 898 |
isMain = zone.isMain(); |
| 899 |
// find the notes in the text zones |
| 900 |
for (auto const ¬eIt : zone.m_footnoteMap) { |
| 901 |
if (notePos[0]==-1) { |
| 902 |
notePos = noteIt.second; |
| 903 |
continue; |
| 904 |
} |
| 905 |
if (notePos[0] > noteIt.second[0]) |
| 906 |
notePos[0] = noteIt.second[0]; |
| 907 |
if (notePos[1] < noteIt.second[1]) |
| 908 |
notePos[1] = noteIt.second[1]; |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
for (int i = limit[0]; i < limit[1]; i++) { |
| 913 |
if (i == notePos[0]) { |
| 914 |
i = notePos[1]-1; |
| 915 |
continue; |
| 916 |
} |
| 917 |
if (isMain && zone.m_pagesPosition.find(i) != zone.m_pagesPosition.end()) { |
| 918 |
++m_state->m_actualPage; |
| 919 |
m_document.newPage(m_state->m_actualPage, zone.m_pagesPosition[i]); |
| 920 |
} |
| 921 |
auto &z = zone.m_zonesList[size_t(i)]; |
| 922 |
if (z.m_type & 0x80) { |
| 923 |
MWAWParagraph parag; |
| 924 |
if (readParagraph(z, parag) && m_parserState->getMainListener()) |
| 925 |
m_parserState->getMainListener()->setParagraph(parag); |
| 926 |
} |
| 927 |
else |
| 928 |
sendText(z, zone.m_id); |
| 929 |
} |
| 930 |
zone.m_isSent = true; |
| 931 |
} |
| 932 |
|
| 933 |
void MsWks3Text::sendNote(int zoneId, int noteId) |
| 934 |
{ |
| 935 |
MWAWListenerPtr listener=m_parserState->getMainListener(); |
| 936 |
if (zoneId < 0 || zoneId >= int(m_state->m_zones.size())) { |
| 937 |
if (listener) listener->insertChar(' '); |
| 938 |
MWAW_DEBUG_MSG(("MsWks3Text::sendNote: unknown zone %d\n", zoneId)); |
| 939 |
return; |
| 940 |
} |
| 941 |
auto &zone=m_state->m_zones[size_t(zoneId)]; |
| 942 |
auto noteIt = zone.m_footnoteMap.find(noteId); |
| 943 |
if (noteIt==zone.m_footnoteMap.end()) { |
| 944 |
MWAW_DEBUG_MSG(("MsWks3Text::sendNote: unknown note %d-%d\n", zoneId, noteId)); |
| 945 |
if (listener) listener->insertChar(' '); |
| 946 |
} |
| 947 |
else |
| 948 |
send(zone, noteIt->second); |
| 949 |
} |
| 950 |
|
| 951 |
void MsWks3Text::sendZone(int zoneId) |
| 952 |
{ |
| 953 |
if (zoneId < 0 || zoneId >= int(m_state->m_zones.size())) { |
| 954 |
MWAW_DEBUG_MSG(("MsWks3Text::sendZone: unknown zone %d\n", zoneId)); |
| 955 |
return; |
| 956 |
} |
| 957 |
send(m_state->m_zones[size_t(zoneId)]); |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
void MsWks3Text::flushExtra() |
| 962 |
{ |
| 963 |
for (auto &zone : m_state->m_zones) { |
| 964 |
if (zone.m_isSent) |
| 965 |
continue; |
| 966 |
send(zone); |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |