Debian Codegraph @

src/lib/ClarisWksBMParser.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 "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWGraphicListener.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 "ClarisWksBMParser.hxx"
64
65 /** Internal: the structures of a ClarisWksBMParser */
66 namespace ClarisWksBMParserInternal
67 {
68
69 ////////////////////////////////////////
70 //! Internal: the state of a ClarisWksBMParser
71 struct State {
72 //! constructor
73 State()
74 {
75 }
76 };
77 }
78
79 ////////////////////////////////////////////////////////////
80 // constructor/destructor, ...
81 ////////////////////////////////////////////////////////////
82 ClarisWksBMParser::ClarisWksBMParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
83 : MWAWGraphicParser(input, rsrcParser, header)
84 , m_state()
85 , m_document()
86 {
87 init();
88 }
89
90 ClarisWksBMParser::~ClarisWksBMParser()
91 {
92 }
93
94 void ClarisWksBMParser::init()
95 {
96 resetGraphicListener();
97 setAsciiName("main-1");
98
99 m_state.reset(new ClarisWksBMParserInternal::State);
100 m_document.reset(new ClarisWksDocument(*this));
101 // reduce the margin (in case, the page is not defined)
102 getPageSpan().setMargins(0.1);
103 }
104
105 ////////////////////////////////////////////////////////////
106 // interface with the different parser
107 ////////////////////////////////////////////////////////////
108 bool ClarisWksBMParser::checkHeader(MWAWHeader *header, bool strict)
109 {
110 *m_state = ClarisWksBMParserInternal::State();
111 if (!m_document->checkHeader(header, strict))
112 return false;
113 return getParserState()->m_kind==MWAWDocument::MWAW_K_PAINT;
114 }
115
116 ////////////////////////////////////////////////////////////
117 // the parser
118 ////////////////////////////////////////////////////////////
119 void ClarisWksBMParser::parse(librevenge::RVNGDrawingInterface *docInterface)
120 {
121 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
122 bool ok = true;
123 try {
124 // create the asciiFile
125 ascii().setStream(getInput());
126 ascii().open(asciiName());
127
128 checkHeader(nullptr);
129 ok = m_document->createZones();
130 // check that we have at least read the main zone
131 if (ok) {
132 auto zMap = m_document->getZone(1);
133 ok = zMap && zMap->m_fileType==4;
134 }
135 if (ok) {
136 createDocument(docInterface);
137
138 MWAWPageSpan const &page=getPageSpan();
139 MWAWPosition pos(MWAWVec2f(float(page.getMarginLeft()),float(page.getMarginRight())),
140 MWAWVec2f(float(page.getPageWidth()),float(page.getPageLength())), librevenge::RVNG_INCH);
141 pos.setRelativePosition(MWAWPosition::Page);
142 pos.m_wrapping = MWAWPosition::WNone;
143 m_document->sendZone(1, getGraphicListener(), pos);
144 }
145 ascii().reset();
146 }
147 catch (...) {
148 MWAW_DEBUG_MSG(("ClarisWksBMParser::parse: exception catched when parsing\n"));
149 ok = false;
150 }
151
152 resetGraphicListener();
153 if (!ok) throw(libmwaw::ParseException());
154 }
155
156 ////////////////////////////////////////////////////////////
157 // create the document
158 ////////////////////////////////////////////////////////////
159 void ClarisWksBMParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
160 {
161 if (!documentInterface) return;
162 if (getGraphicListener()) {
163 MWAW_DEBUG_MSG(("ClarisWksBMParser::createDocument: listener already exist\n"));
164 return;
165 }
166
167 m_document->m_graphParser->computePositions();
168
169 //
170 std::vector<MWAWPageSpan> pageList;
171 m_document->updatePageSpanList(pageList);
172 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
173 setGraphicListener(listen);
174 listen->setDocumentMetaData(m_document->getDocumentMetaData());
175 listen->startDocument();
176 }
177
178 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: