| 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 <cstring> |
| 36 |
#include <iomanip> |
| 37 |
#include <iostream> |
| 38 |
#include <limits> |
| 39 |
#include <map> |
| 40 |
#include <sstream> |
| 41 |
|
| 42 |
#include <librevenge/librevenge.h> |
| 43 |
|
| 44 |
#include "MWAWFont.hxx" |
| 45 |
#include "MWAWFontConverter.hxx" |
| 46 |
#include "MWAWHeader.hxx" |
| 47 |
#include "MWAWPosition.hxx" |
| 48 |
#include "MWAWPictMac.hxx" |
| 49 |
#include "MWAWPresentationListener.hxx" |
| 50 |
#include "MWAWPrinter.hxx" |
| 51 |
#include "MWAWSection.hxx" |
| 52 |
|
| 53 |
#include "ClarisWksDatabase.hxx" |
| 54 |
#include "ClarisWksDocument.hxx" |
| 55 |
#include "ClarisWksGraph.hxx" |
| 56 |
#include "ClarisWksPresentation.hxx" |
| 57 |
#include "ClarisWksSpreadsheet.hxx" |
| 58 |
#include "ClarisWksStruct.hxx" |
| 59 |
#include "ClarisWksStyleManager.hxx" |
| 60 |
#include "ClarisWksTable.hxx" |
| 61 |
#include "ClarisWksText.hxx" |
| 62 |
|
| 63 |
#include "ClarisWksPRParser.hxx" |
| 64 |
|
| 65 |
/** Internal: the structures of a ClarisWksPRParser */ |
| 66 |
namespace ClarisWksPRParserInternal |
| 67 |
{ |
| 68 |
|
| 69 |
//////////////////////////////////////// |
| 70 |
//! Internal: the state of a ClarisWksPRParser |
| 71 |
struct State { |
| 72 |
//! constructor |
| 73 |
State() |
| 74 |
: m_actPage(0) |
| 75 |
, m_numPages(0) |
| 76 |
{ |
| 77 |
} |
| 78 |
|
| 79 |
int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */; |
| 80 |
}; |
| 81 |
|
| 82 |
//////////////////////////////////////// |
| 83 |
//! Internal: the subdocument of a ClarisWksPRParser |
| 84 |
class SubDocument final : public MWAWSubDocument |
| 85 |
{ |
| 86 |
public: |
| 87 |
SubDocument(ClarisWksPRParser &pars, MWAWInputStreamPtr const &input, int zoneId, MWAWPosition const &pos=MWAWPosition()) |
| 88 |
: MWAWSubDocument(&pars, input, MWAWEntry()) |
| 89 |
, m_id(zoneId) |
| 90 |
, m_position(pos) {} |
| 91 |
|
| 92 |
//! destructor |
| 93 |
~SubDocument() final {} |
| 94 |
|
| 95 |
//! operator!= |
| 96 |
bool operator!=(MWAWSubDocument const &doc) const final |
| 97 |
{ |
| 98 |
if (MWAWSubDocument::operator!=(doc)) return true; |
| 99 |
auto const *sDoc = dynamic_cast<SubDocument const *>(&doc); |
| 100 |
if (!sDoc) return true; |
| 101 |
if (m_id != sDoc->m_id) return true; |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
//! the parser function |
| 106 |
void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final; |
| 107 |
|
| 108 |
protected: |
| 109 |
//! the subdocument id |
| 110 |
int m_id; |
| 111 |
//! the subdocument position if defined |
| 112 |
MWAWPosition m_position; |
| 113 |
}; |
| 114 |
|
| 115 |
void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType) |
| 116 |
{ |
| 117 |
if (!listener.get()) { |
| 118 |
MWAW_DEBUG_MSG(("ClarisWksPRParserInternal::SubDocument::parse: no listener\n")); |
| 119 |
return; |
| 120 |
} |
| 121 |
if (m_id == -1) { // a number used to send linked frame |
| 122 |
listener->insertChar(' '); |
| 123 |
return; |
| 124 |
} |
| 125 |
if (m_id == 0) { |
| 126 |
MWAW_DEBUG_MSG(("ClarisWksPRParserInternal::SubDocument::parse: unknown zone\n")); |
| 127 |
return; |
| 128 |
} |
| 129 |
auto *parser=dynamic_cast<ClarisWksPRParser *>(m_parser); |
| 130 |
if (!parser) { |
| 131 |
MWAW_DEBUG_MSG(("ClarisWksPRParserInternal::SubDocument::parse: can not find parser\n")); |
| 132 |
return; |
| 133 |
} |
| 134 |
parser->m_document->sendZone(m_id, listener, m_position); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
//////////////////////////////////////////////////////////// |
| 139 |
// constructor/destructor, ... |
| 140 |
//////////////////////////////////////////////////////////// |
| 141 |
ClarisWksPRParser::ClarisWksPRParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 142 |
: MWAWPresentationParser(input, rsrcParser, header) |
| 143 |
, m_state() |
| 144 |
, m_document() |
| 145 |
{ |
| 146 |
init(); |
| 147 |
} |
| 148 |
|
| 149 |
ClarisWksPRParser::~ClarisWksPRParser() |
| 150 |
{ |
| 151 |
} |
| 152 |
|
| 153 |
void ClarisWksPRParser::init() |
| 154 |
{ |
| 155 |
resetPresentationListener(); |
| 156 |
setAsciiName("main-1"); |
| 157 |
|
| 158 |
m_state.reset(new ClarisWksPRParserInternal::State); |
| 159 |
m_document.reset(new ClarisWksDocument(*this)); |
| 160 |
// reduce the margin (in case, the page is not defined) |
| 161 |
getPageSpan().setMargins(0.1); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
//////////////////////////////////////////////////////////// |
| 166 |
// interface with the different parser |
| 167 |
//////////////////////////////////////////////////////////// |
| 168 |
bool ClarisWksPRParser::checkHeader(MWAWHeader *header, bool strict) |
| 169 |
{ |
| 170 |
*m_state = ClarisWksPRParserInternal::State(); |
| 171 |
if (!m_document->checkHeader(header, strict)) |
| 172 |
return false; |
| 173 |
return getParserState()->m_kind==MWAWDocument::MWAW_K_PRESENTATION; |
| 174 |
} |
| 175 |
|
| 176 |
//////////////////////////////////////////////////////////// |
| 177 |
// the parser |
| 178 |
//////////////////////////////////////////////////////////// |
| 179 |
void ClarisWksPRParser::parse(librevenge::RVNGPresentationInterface *docInterface) |
| 180 |
{ |
| 181 |
if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 182 |
bool ok = true; |
| 183 |
try { |
| 184 |
// create the asciiFile |
| 185 |
ascii().setStream(getInput()); |
| 186 |
ascii().open(asciiName()); |
| 187 |
|
| 188 |
checkHeader(nullptr); |
| 189 |
ok = m_document->createZones(); |
| 190 |
// check that we have at least read the main zone |
| 191 |
if (ok) { |
| 192 |
auto zMap = m_document->getZone(1); |
| 193 |
ok = zMap && zMap->m_fileType==5; |
| 194 |
} |
| 195 |
if (ok) { |
| 196 |
createDocument(docInterface); |
| 197 |
|
| 198 |
m_document->sendZone(1, getPresentationListener(), MWAWPosition()); |
| 199 |
} |
| 200 |
ascii().reset(); |
| 201 |
} |
| 202 |
catch (...) { |
| 203 |
MWAW_DEBUG_MSG(("ClarisWksPRParser::parse: exception catched when parsing\n")); |
| 204 |
ok = false; |
| 205 |
} |
| 206 |
|
| 207 |
resetPresentationListener(); |
| 208 |
if (!ok) throw(libmwaw::ParseException()); |
| 209 |
} |
| 210 |
|
| 211 |
//////////////////////////////////////////////////////////// |
| 212 |
// create the document |
| 213 |
//////////////////////////////////////////////////////////// |
| 214 |
void ClarisWksPRParser::createDocument(librevenge::RVNGPresentationInterface *documentInterface) |
| 215 |
{ |
| 216 |
if (!documentInterface) return; |
| 217 |
if (getPresentationListener()) { |
| 218 |
MWAW_DEBUG_MSG(("ClarisWksPRParser::createDocument: listener already exist\n")); |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
// update the page |
| 223 |
m_state->m_actPage = 0; |
| 224 |
m_state->m_numPages = m_document->numPages(); |
| 225 |
|
| 226 |
// create the page list |
| 227 |
std::vector<MWAWPageSpan> pageList; |
| 228 |
MWAWPageSpan master; |
| 229 |
m_document->updatePageSpanList(pageList, master); |
| 230 |
|
| 231 |
// |
| 232 |
MWAWPresentationListenerPtr listen(new MWAWPresentationListener(*getParserState(), pageList, documentInterface)); |
| 233 |
setPresentationListener(listen); |
| 234 |
listen->setDocumentMetaData(m_document->getDocumentMetaData()); |
| 235 |
listen->startDocument(); |
| 236 |
|
| 237 |
// time to send the master |
| 238 |
listen->openMasterPage(master); |
| 239 |
m_document->getPresentationParser()->sendMaster(); |
| 240 |
listen->closeMasterPage(); |
| 241 |
} |
| 242 |
|
| 243 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |