| 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 <librevenge/librevenge.h> |
| 35 |
|
| 36 |
#include "libmwaw_internal.hxx" |
| 37 |
#include "MWAWListener.hxx" |
| 38 |
#include "MWAWParagraph.hxx" |
| 39 |
#include "MWAWSubDocument.hxx" |
| 40 |
|
| 41 |
#include "MWAWPageSpan.hxx" |
| 42 |
|
| 43 |
/** Internal: the structures of a MWAWPageSpan */ |
| 44 |
namespace MWAWPageSpanInternal |
| 45 |
{ |
| 46 |
//////////////////////////////////////// |
| 47 |
//! Internal: the subdocument of a MWAWParser |
| 48 |
class SubDocument final : public MWAWSubDocument |
| 49 |
{ |
| 50 |
public: |
| 51 |
//! constructor |
| 52 |
explicit SubDocument(MWAWHeaderFooter const &headerFooter) |
| 53 |
: MWAWSubDocument(nullptr, MWAWInputStreamPtr(), MWAWEntry()) |
| 54 |
, m_headerFooter(headerFooter) |
| 55 |
{ |
| 56 |
} |
| 57 |
|
| 58 |
//! destructor |
| 59 |
~SubDocument() final {} |
| 60 |
|
| 61 |
//! operator!= |
| 62 |
bool operator!=(MWAWSubDocument const &doc) const final |
| 63 |
{ |
| 64 |
if (MWAWSubDocument::operator!=(doc)) return true; |
| 65 |
auto const *sDoc = dynamic_cast<SubDocument const *>(&doc); |
| 66 |
if (!sDoc) return true; |
| 67 |
if (m_headerFooter != sDoc->m_headerFooter) return true; |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
//! the parser function |
| 72 |
void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final; |
| 73 |
|
| 74 |
protected: |
| 75 |
//! the header footer |
| 76 |
MWAWHeaderFooter const &m_headerFooter; |
| 77 |
}; |
| 78 |
|
| 79 |
void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) |
| 80 |
{ |
| 81 |
if (!listener.get()) { |
| 82 |
MWAW_DEBUG_MSG(("MWAWPageSpanInternal::SubDocument::parse: no listener\n")); |
| 83 |
return; |
| 84 |
} |
| 85 |
if (m_headerFooter.m_pageNumberPosition >= MWAWHeaderFooter::TopLeft && |
| 86 |
m_headerFooter.m_pageNumberPosition <= MWAWHeaderFooter::TopRight) |
| 87 |
m_headerFooter.insertPageNumberParagraph(listener.get()); |
| 88 |
if (m_headerFooter.m_subDocument) |
| 89 |
m_headerFooter.m_subDocument->parse(listener, type); |
| 90 |
if (m_headerFooter.m_pageNumberPosition >= MWAWHeaderFooter::BottomLeft && |
| 91 |
m_headerFooter.m_pageNumberPosition <= MWAWHeaderFooter::BottomRight) |
| 92 |
m_headerFooter.insertPageNumberParagraph(listener.get()); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// ----------------- MWAWHeaderFooter ------------------------ |
| 97 |
MWAWHeaderFooter::MWAWHeaderFooter(MWAWHeaderFooter::Type const type, MWAWHeaderFooter::Occurrence const occurrence) |
| 98 |
: m_type(type) |
| 99 |
, m_occurrence(occurrence) |
| 100 |
, m_height(0) |
| 101 |
, m_pageNumberPosition(MWAWHeaderFooter::None) |
| 102 |
, m_pageNumberType(libmwaw::ARABIC) |
| 103 |
, m_pageNumberFont(20,12) |
| 104 |
, m_subDocument() |
| 105 |
{ |
| 106 |
} |
| 107 |
|
| 108 |
MWAWHeaderFooter::~MWAWHeaderFooter() |
| 109 |
{ |
| 110 |
} |
| 111 |
|
| 112 |
bool MWAWHeaderFooter::operator==(MWAWHeaderFooter const &hf) const |
| 113 |
{ |
| 114 |
if (&hf==this) return true; |
| 115 |
if (m_type != hf.m_type) |
| 116 |
return false; |
| 117 |
if (m_type == UNDEF) |
| 118 |
return true; |
| 119 |
if (m_occurrence != hf.m_occurrence) |
| 120 |
return false; |
| 121 |
if (m_height < hf.m_height || m_height > hf.m_height) |
| 122 |
return false; |
| 123 |
if (m_pageNumberPosition != hf.m_pageNumberPosition || |
| 124 |
m_pageNumberType != hf.m_pageNumberType || |
| 125 |
m_pageNumberFont != hf.m_pageNumberFont) |
| 126 |
return false; |
| 127 |
if (!m_subDocument) |
| 128 |
return !hf.m_subDocument; |
| 129 |
if (*m_subDocument.get() != hf.m_subDocument) |
| 130 |
return false; |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
// send data to the listener |
| 135 |
void MWAWHeaderFooter::send(MWAWListener *listener) const |
| 136 |
{ |
| 137 |
if (m_type == UNDEF) |
| 138 |
return; |
| 139 |
if (!listener) { |
| 140 |
MWAW_DEBUG_MSG(("MWAWHeaderFooter::send: called without listener\n")); |
| 141 |
return; |
| 142 |
} |
| 143 |
librevenge::RVNGPropertyList propList; |
| 144 |
switch (m_occurrence) { |
| 145 |
case ODD: |
| 146 |
propList.insert("librevenge:occurrence", "odd"); |
| 147 |
break; |
| 148 |
case EVEN: |
| 149 |
propList.insert("librevenge:occurrence", "even"); |
| 150 |
break; |
| 151 |
case ALL: |
| 152 |
propList.insert("librevenge:occurrence", "all"); |
| 153 |
break; |
| 154 |
case NEVER: |
| 155 |
#if !defined(__clang__) |
| 156 |
default: |
| 157 |
#endif |
| 158 |
break; |
| 159 |
} |
| 160 |
if (m_pageNumberPosition!=None) { |
| 161 |
std::shared_ptr<MWAWPageSpanInternal::SubDocument> doc |
| 162 |
(new MWAWPageSpanInternal::SubDocument(*this)); |
| 163 |
if (m_type == HEADER) |
| 164 |
listener->insertHeader(doc,propList); |
| 165 |
else |
| 166 |
listener->insertFooter(doc,propList); |
| 167 |
return; |
| 168 |
} |
| 169 |
if (m_type == HEADER) |
| 170 |
listener->insertHeader(m_subDocument,propList); |
| 171 |
else |
| 172 |
listener->insertFooter(m_subDocument,propList); |
| 173 |
} |
| 174 |
|
| 175 |
void MWAWHeaderFooter::insertPageNumberParagraph(MWAWListener *listener) const |
| 176 |
{ |
| 177 |
MWAWParagraph para; |
| 178 |
para.m_justify = MWAWParagraph::JustificationCenter; |
| 179 |
switch (m_pageNumberPosition) { |
| 180 |
case TopLeft: |
| 181 |
case BottomLeft: |
| 182 |
para.m_justify = MWAWParagraph::JustificationLeft; |
| 183 |
break; |
| 184 |
case TopRight: |
| 185 |
case BottomRight: |
| 186 |
para.m_justify = MWAWParagraph::JustificationRight; |
| 187 |
break; |
| 188 |
case TopCenter: |
| 189 |
case BottomCenter: |
| 190 |
case None: |
| 191 |
break; |
| 192 |
#if !defined(__clang__) |
| 193 |
default: |
| 194 |
MWAW_DEBUG_MSG(("MWAWHeaderFooter::insertPageNumberParagraph: unexpected value\n")); |
| 195 |
break; |
| 196 |
#endif |
| 197 |
} |
| 198 |
listener->setParagraph(para); |
| 199 |
listener->setFont(m_pageNumberFont); |
| 200 |
if (listener->isParagraphOpened()) |
| 201 |
listener->insertEOL(); |
| 202 |
|
| 203 |
MWAWField field(MWAWField::PageNumber); |
| 204 |
field.m_numberingType=m_pageNumberType; |
| 205 |
listener->insertField(field); |
| 206 |
} |
| 207 |
|
| 208 |
// ----------------- MWAWPageSpan ------------------------ |
| 209 |
MWAWPageSpan::MWAWPageSpan() |
| 210 |
: m_formLength(11.0) |
| 211 |
, m_formWidth(8.5) |
| 212 |
, m_name("") |
| 213 |
, m_masterName("") |
| 214 |
, m_formOrientation(MWAWPageSpan::PORTRAIT) |
| 215 |
, m_backgroundColor(MWAWColor::white()) |
| 216 |
, m_headerFooterList() |
| 217 |
, m_pageNumber(-1) |
| 218 |
, m_pageSpan(1) |
| 219 |
{ |
| 220 |
for (auto &margin : m_margins) margin = 1.0; |
| 221 |
} |
| 222 |
|
| 223 |
MWAWPageSpan::~MWAWPageSpan() |
| 224 |
{ |
| 225 |
} |
| 226 |
|
| 227 |
void MWAWPageSpan::setHeaderFooter(MWAWHeaderFooter const &hF) |
| 228 |
{ |
| 229 |
MWAWHeaderFooter::Type const type=hF.m_type; |
| 230 |
switch (hF.m_occurrence) { |
| 231 |
case MWAWHeaderFooter::NEVER: |
| 232 |
removeHeaderFooter(type, MWAWHeaderFooter::ALL); |
| 233 |
MWAW_FALLTHROUGH; |
| 234 |
case MWAWHeaderFooter::ALL: |
| 235 |
removeHeaderFooter(type, MWAWHeaderFooter::ODD); |
| 236 |
removeHeaderFooter(type, MWAWHeaderFooter::EVEN); |
| 237 |
break; |
| 238 |
case MWAWHeaderFooter::ODD: |
| 239 |
removeHeaderFooter(type, MWAWHeaderFooter::ALL); |
| 240 |
break; |
| 241 |
case MWAWHeaderFooter::EVEN: |
| 242 |
removeHeaderFooter(type, MWAWHeaderFooter::ALL); |
| 243 |
break; |
| 244 |
#if !defined(__clang__) |
| 245 |
default: |
| 246 |
break; |
| 247 |
#endif |
| 248 |
} |
| 249 |
int pos = getHeaderFooterPosition(hF.m_type, hF.m_occurrence); |
| 250 |
if (pos != -1) |
| 251 |
m_headerFooterList[size_t(pos)]=hF; |
| 252 |
|
| 253 |
bool containsHFLeft = containsHeaderFooter(type, MWAWHeaderFooter::ODD); |
| 254 |
bool containsHFRight = containsHeaderFooter(type, MWAWHeaderFooter::EVEN); |
| 255 |
|
| 256 |
if (containsHFLeft && !containsHFRight) { |
| 257 |
MWAW_DEBUG_MSG(("Inserting dummy header right\n")); |
| 258 |
MWAWHeaderFooter dummy(type, MWAWHeaderFooter::EVEN); |
| 259 |
pos = getHeaderFooterPosition(type, MWAWHeaderFooter::EVEN); |
| 260 |
if (pos != -1) |
| 261 |
m_headerFooterList[size_t(pos)]=MWAWHeaderFooter(type, MWAWHeaderFooter::EVEN); |
| 262 |
} |
| 263 |
else if (!containsHFLeft && containsHFRight) { |
| 264 |
MWAW_DEBUG_MSG(("Inserting dummy header left\n")); |
| 265 |
pos = getHeaderFooterPosition(type, MWAWHeaderFooter::ODD); |
| 266 |
if (pos != -1) |
| 267 |
m_headerFooterList[size_t(pos)]=MWAWHeaderFooter(type, MWAWHeaderFooter::ODD); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
void MWAWPageSpan::checkMargins() |
| 272 |
{ |
| 273 |
if (m_margins[libmwaw::Left]+m_margins[libmwaw::Right] > 0.95*m_formWidth) { |
| 274 |
MWAW_DEBUG_MSG(("MWAWPageSpan::checkMargins: left/right margins seems bad\n")); |
| 275 |
m_margins[libmwaw::Left] = m_margins[libmwaw::Right] = 0.05*m_formWidth; |
| 276 |
} |
| 277 |
if (m_margins[libmwaw::Top]+m_margins[libmwaw::Bottom] > 0.95*m_formLength) { |
| 278 |
MWAW_DEBUG_MSG(("MWAWPageSpan::checkMargins: top/bottom margins seems bad\n")); |
| 279 |
m_margins[libmwaw::Top] = m_margins[libmwaw::Bottom] = 0.05*m_formLength; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
void MWAWPageSpan::sendHeaderFooters(MWAWListener *listener) const |
| 284 |
{ |
| 285 |
if (!listener) { |
| 286 |
MWAW_DEBUG_MSG(("MWAWPageSpan::sendHeaderFooters: no listener\n")); |
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
for (auto const &hf : m_headerFooterList) { |
| 291 |
if (!hf.isDefined()) continue; |
| 292 |
hf.send(listener); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
void MWAWPageSpan::sendHeaderFooters(MWAWListener *listener, MWAWHeaderFooter::Occurrence occurrence) const |
| 297 |
{ |
| 298 |
if (!listener) { |
| 299 |
MWAW_DEBUG_MSG(("MWAWPageSpan::sendHeaderFooters: no listener\n")); |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
for (auto const &hf : m_headerFooterList) { |
| 304 |
if (!hf.isDefined()) continue; |
| 305 |
if (hf.m_occurrence==occurrence || hf.m_occurrence==MWAWHeaderFooter::ALL) |
| 306 |
hf.send(listener); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
void MWAWPageSpan::getPageProperty(librevenge::RVNGPropertyList &propList, bool isPresentation) const |
| 311 |
{ |
| 312 |
propList.insert("librevenge:num-pages", getPageSpan()); |
| 313 |
|
| 314 |
if (hasPageName()) |
| 315 |
propList.insert("draw:name", getPageName()); |
| 316 |
if (hasMasterPageName()) |
| 317 |
propList.insert("librevenge:master-page-name", getMasterPageName()); |
| 318 |
propList.insert("fo:page-height", getFormLength(), librevenge::RVNG_INCH); |
| 319 |
propList.insert("fo:page-width", getFormWidth(), librevenge::RVNG_INCH); |
| 320 |
if (getFormOrientation() == LANDSCAPE) |
| 321 |
propList.insert("style:print-orientation", "landscape"); |
| 322 |
else |
| 323 |
propList.insert("style:print-orientation", "portrait"); |
| 324 |
propList.insert("fo:margin-left", getMarginLeft(), librevenge::RVNG_INCH); |
| 325 |
propList.insert("fo:margin-right", getMarginRight(), librevenge::RVNG_INCH); |
| 326 |
propList.insert("fo:margin-top", getMarginTop(), librevenge::RVNG_INCH); |
| 327 |
propList.insert("fo:margin-bottom", getMarginBottom(), librevenge::RVNG_INCH); |
| 328 |
if (!m_backgroundColor.isWhite()) { |
| 329 |
if (isPresentation) { |
| 330 |
propList.insert("draw:fill", "solid"); |
| 331 |
propList.insert("draw:fill-color", m_backgroundColor.str().c_str()); |
| 332 |
} |
| 333 |
else |
| 334 |
propList.insert("fo:background-color", m_backgroundColor.str().c_str()); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
bool MWAWPageSpan::operator==(std::shared_ptr<MWAWPageSpan> const &page2) const |
| 340 |
{ |
| 341 |
if (!page2) return false; |
| 342 |
if (page2.get() == this) return true; |
| 343 |
if (m_formLength < page2->m_formLength || m_formLength > page2->m_formLength || |
| 344 |
m_formWidth < page2->m_formWidth || m_formWidth > page2->m_formWidth || |
| 345 |
m_formOrientation != page2->m_formOrientation) |
| 346 |
return false; |
| 347 |
if (getMarginLeft() < page2->getMarginLeft() || getMarginLeft() > page2->getMarginLeft() || |
| 348 |
getMarginRight() < page2->getMarginRight() || getMarginRight() > page2->getMarginRight() || |
| 349 |
getMarginTop() < page2->getMarginTop() || getMarginTop() > page2->getMarginTop() || |
| 350 |
getMarginBottom() < page2->getMarginBottom() || getMarginBottom() > page2->getMarginBottom()) |
| 351 |
return false; |
| 352 |
if (getPageName() != page2->getPageName() || getMasterPageName() != page2->getMasterPageName() || |
| 353 |
backgroundColor() != page2->backgroundColor()) |
| 354 |
return false; |
| 355 |
|
| 356 |
if (getPageNumber() != page2->getPageNumber()) |
| 357 |
return false; |
| 358 |
|
| 359 |
size_t numHF = m_headerFooterList.size(); |
| 360 |
size_t numHF2 = page2->m_headerFooterList.size(); |
| 361 |
for (size_t i = numHF; i < numHF2; i++) { |
| 362 |
if (page2->m_headerFooterList[i].isDefined()) |
| 363 |
return false; |
| 364 |
} |
| 365 |
for (size_t i = numHF2; i < numHF; i++) { |
| 366 |
if (m_headerFooterList[i].isDefined()) |
| 367 |
return false; |
| 368 |
} |
| 369 |
if (numHF2 < numHF) numHF = numHF2; |
| 370 |
for (size_t i = 0; i < numHF; i++) { |
| 371 |
if (m_headerFooterList[i] != page2->m_headerFooterList[i]) |
| 372 |
return false; |
| 373 |
} |
| 374 |
MWAW_DEBUG_MSG(("WordPerfect: MWAWPageSpan == comparison finished, found no differences\n")); |
| 375 |
|
| 376 |
return true; |
| 377 |
} |
| 378 |
|
| 379 |
// -------------- manage header footer list ------------------ |
| 380 |
void MWAWPageSpan::removeHeaderFooter(MWAWHeaderFooter::Type type, MWAWHeaderFooter::Occurrence occurrence) |
| 381 |
{ |
| 382 |
int pos = getHeaderFooterPosition(type, occurrence); |
| 383 |
if (pos == -1) return; |
| 384 |
m_headerFooterList[size_t(pos)]=MWAWHeaderFooter(); |
| 385 |
} |
| 386 |
|
| 387 |
bool MWAWPageSpan::containsHeaderFooter(MWAWHeaderFooter::Type type, MWAWHeaderFooter::Occurrence occurrence) |
| 388 |
{ |
| 389 |
int pos = getHeaderFooterPosition(type, occurrence); |
| 390 |
if (pos == -1 || !m_headerFooterList[size_t(pos)].isDefined()) return false; |
| 391 |
return true; |
| 392 |
} |
| 393 |
|
| 394 |
int MWAWPageSpan::getHeaderFooterPosition(MWAWHeaderFooter::Type type, MWAWHeaderFooter::Occurrence occurrence) |
| 395 |
{ |
| 396 |
int typePos = 0, occurrencePos = 0; |
| 397 |
switch (type) { |
| 398 |
case MWAWHeaderFooter::HEADER: |
| 399 |
typePos = 0; |
| 400 |
break; |
| 401 |
case MWAWHeaderFooter::FOOTER: |
| 402 |
typePos = 1; |
| 403 |
break; |
| 404 |
case MWAWHeaderFooter::UNDEF: |
| 405 |
#if !defined(__clang__) |
| 406 |
default: |
| 407 |
#endif |
| 408 |
MWAW_DEBUG_MSG(("MWAWPageSpan::getVectorPosition: unknown type\n")); |
| 409 |
return -1; |
| 410 |
} |
| 411 |
switch (occurrence) { |
| 412 |
case MWAWHeaderFooter::ALL: |
| 413 |
occurrencePos = 0; |
| 414 |
break; |
| 415 |
case MWAWHeaderFooter::ODD: |
| 416 |
occurrencePos = 1; |
| 417 |
break; |
| 418 |
case MWAWHeaderFooter::EVEN: |
| 419 |
occurrencePos = 2; |
| 420 |
break; |
| 421 |
case MWAWHeaderFooter::NEVER: |
| 422 |
#if !defined(__clang__) |
| 423 |
default: |
| 424 |
#endif |
| 425 |
MWAW_DEBUG_MSG(("MWAWPageSpan::getVectorPosition: unknown occurrence\n")); |
| 426 |
return -1; |
| 427 |
} |
| 428 |
auto res = size_t(typePos*3+occurrencePos); |
| 429 |
if (res >= m_headerFooterList.size()) |
| 430 |
m_headerFooterList.resize(res+1); |
| 431 |
return int(res); |
| 432 |
} |
| 433 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |