Debian Codegraph @

src/lib/ClarisWksParser.cxx

raw
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 "MWAWTextListener.hxx"
45 #include "MWAWFont.hxx"
46 #include "MWAWFontConverter.hxx"
47 #include "MWAWHeader.hxx"
48 #include "MWAWPosition.hxx"
49 #include "MWAWPictMac.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 "ClarisWksParser.hxx"
64
65 /** Internal: the structures of a ClarisWksParser */
66 namespace ClarisWksParserInternal
67 {
68
69 ////////////////////////////////////////
70 //! Internal: the state of a ClarisWksParser
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 ClarisWksParser
84 class SubDocument final : public MWAWSubDocument
85 {
86 public:
87 SubDocument(ClarisWksParser &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(("ClarisWksParserInternal::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(("ClarisWksParserInternal::SubDocument::parse: unknown zone\n"));
127 return;
128 }
129
130 if (!m_parser) {
131 MWAW_DEBUG_MSG(("ClarisWksParserInternal::SubDocument::parse: can not find the parser\n"));
132 return;
133 }
134 static_cast<ClarisWksParser *>(m_parser)->m_document->sendZone(m_id, listener, m_position);
135 }
136 }
137
138 ////////////////////////////////////////////////////////////
139 // constructor/destructor, ...
140 ////////////////////////////////////////////////////////////
141 ClarisWksParser::ClarisWksParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
142 : MWAWTextParser(input, rsrcParser, header)
143 , m_state()
144 , m_document()
145 {
146 init();
147 }
148
149 ClarisWksParser::~ClarisWksParser()
150 {
151 }
152
153 void ClarisWksParser::init()
154 {
155 resetTextListener();
156 setAsciiName("main-1");
157
158 m_state.reset(new ClarisWksParserInternal::State);
159 m_document.reset(new ClarisWksDocument(*this));
160 m_document->m_newPage=static_cast<ClarisWksDocument::NewPage>(&ClarisWksParser::newPage);
161 m_document->m_sendFootnote=static_cast<ClarisWksDocument::SendFootnote>(&ClarisWksParser::sendFootnote);
162 // reduce the margin (in case, the page is not defined)
163 getPageSpan().setMargins(0.1);
164 }
165
166 ////////////////////////////////////////////////////////////
167 // new page
168 ////////////////////////////////////////////////////////////
169 void ClarisWksParser::newPage(int number, bool soft)
170 {
171 if (number <= m_state->m_actPage || number > m_state->m_numPages)
172 return;
173
174 while (m_state->m_actPage < number) {
175 m_state->m_actPage++;
176 if (!getTextListener() || m_state->m_actPage == 1)
177 continue;
178 if (soft)
179 getTextListener()->insertBreak(MWAWTextListener::SoftPageBreak);
180 else
181 getTextListener()->insertBreak(MWAWTextListener::PageBreak);
182 }
183 }
184
185 ////////////////////////////////////////////////////////////
186 // interface with the different parser
187 ////////////////////////////////////////////////////////////
188 void ClarisWksParser::sendFootnote(int zoneId)
189 {
190 if (!getTextListener()) return;
191
192 MWAWSubDocumentPtr subdoc(new ClarisWksParserInternal::SubDocument(*this, getInput(), zoneId));
193 getTextListener()->insertNote(MWAWNote(MWAWNote::FootNote), subdoc);
194 }
195
196 bool ClarisWksParser::checkHeader(MWAWHeader *header, bool strict)
197 {
198 *m_state = ClarisWksParserInternal::State();
199 if (!m_document->checkHeader(header, strict))
200 return false;
201 return getParserState()->m_kind==MWAWDocument::MWAW_K_TEXT ||
202 getParserState()->m_kind==MWAWDocument::MWAW_K_DRAW;
203 }
204
205 ////////////////////////////////////////////////////////////
206 // the parser
207 ////////////////////////////////////////////////////////////
208 void ClarisWksParser::parse(librevenge::RVNGTextInterface *docInterface)
209 {
210 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
211 bool ok = true;
212 try {
213 // create the asciiFile
214 ascii().setStream(getInput());
215 ascii().open(asciiName());
216
217 checkHeader(nullptr);
218 ok = m_document->createZones();
219 if (ok) {
220 createDocument(docInterface);
221 auto const &mainZonesList=m_document->getMainZonesList();
222 if (getParserState()->m_kind==MWAWDocument::MWAW_K_DRAW) {
223 for (int i=0; i<m_state->m_numPages; ++i)
224 m_document->getGraphParser()->sendMaster(i);
225 }
226 for (auto const zone : mainZonesList)
227 m_document->getGraphParser()->sendPageGraphics(zone);
228 if (getParserState()->m_kind==MWAWDocument::MWAW_K_TEXT) {
229 auto mainMap = m_document->getZone(1);
230 if (mainMap && mainMap->m_fileType==1 && !mainMap->m_parsed)
231 m_document->sendZone(1, MWAWListenerPtr(), MWAWPosition());
232 else {
233 MWAW_DEBUG_MSG(("ClarisWksParser::parse: find a problem with the main text zones\n"));
234 }
235 }
236 newPage(m_state->m_numPages, false);
237 #ifdef DEBUG
238 m_document->getGraphParser()->flushExtra();
239 #endif
240 m_document->getTableParser()->flushExtra();
241 m_document->getTextParser()->flushExtra();
242 }
243 ascii().reset();
244 }
245 catch (...) {
246 MWAW_DEBUG_MSG(("ClarisWksParser::parse: exception catched when parsing\n"));
247 ok = false;
248 }
249
250 resetTextListener();
251 if (!ok) throw(libmwaw::ParseException());
252 }
253
254 ////////////////////////////////////////////////////////////
255 // create the document
256 ////////////////////////////////////////////////////////////
257 void ClarisWksParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
258 {
259 if (!documentInterface) return;
260 if (getTextListener()) {
261 MWAW_DEBUG_MSG(("ClarisWksParser::createDocument: listener already exist\n"));
262 return;
263 }
264
265 // update the page
266 m_state->m_actPage = 0;
267 m_state->m_numPages = m_document->numPages();
268
269 // create the page list
270 std::vector<MWAWPageSpan> pageList;
271 m_document->updatePageSpanList(pageList);
272 //
273 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
274 setTextListener(listen);
275 listen->setDocumentMetaData(m_document->getDocumentMetaData());
276 listen->startDocument();
277 }
278
279 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: