Debian Codegraph @

src/lib/GreatWksBMParser.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 <iomanip>
35 #include <iostream>
36 #include <limits>
37 #include <set>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWGraphicListener.hxx"
43 #include "MWAWHeader.hxx"
44 #include "MWAWPictData.hxx"
45 #include "MWAWPosition.hxx"
46
47 #include "GreatWksBMParser.hxx"
48
49 /** Internal: the structures of a GreatWksBMParser */
50 namespace GreatWksBMParserInternal
51 {
52 ////////////////////////////////////////
53 //! Internal: the state of a GreatWksBMParser
54 struct State {
55 //! constructor
56 State()
57 : m_picture()
58 {
59 }
60 //! the picture entry (v2)
61 MWAWEntry m_picture;
62 };
63
64 }
65
66 ////////////////////////////////////////////////////////////
67 // constructor/destructor, ...
68 ////////////////////////////////////////////////////////////
69 GreatWksBMParser::GreatWksBMParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
70 : MWAWGraphicParser(input, rsrcParser, header), m_state()
71 {
72 init();
73 }
74
75 GreatWksBMParser::~GreatWksBMParser()
76 {
77 }
78
79 void GreatWksBMParser::init()
80 {
81 resetGraphicListener();
82 setAsciiName("main-1");
83
84 m_state.reset(new GreatWksBMParserInternal::State);
85
86 getPageSpan().setMargins(0.1);
87 }
88
89 ////////////////////////////////////////////////////////////
90 // the parser
91 ////////////////////////////////////////////////////////////
92 void GreatWksBMParser::parse(librevenge::RVNGDrawingInterface *docInterface)
93 {
94 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
95 bool ok = false;
96 try {
97 // create the asciiFile
98 ascii().setStream(getInput());
99 ascii().open(asciiName());
100 checkHeader(nullptr);
101 ok = createZones();
102 if (ok) {
103 createDocument(docInterface);
104 sendPicture();
105 }
106 ascii().reset();
107 }
108 catch (...) {
109 MWAW_DEBUG_MSG(("GreatWksBMParser::parse: exception catched when parsing\n"));
110 ok = false;
111 }
112
113 resetGraphicListener();
114 if (!ok) throw(libmwaw::ParseException());
115 }
116
117 ////////////////////////////////////////////////////////////
118 // create the document
119 ////////////////////////////////////////////////////////////
120 void GreatWksBMParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
121 {
122 if (!documentInterface) return;
123 if (getGraphicListener()) {
124 MWAW_DEBUG_MSG(("GreatWksBMParser::createDocument: listener already exist\n"));
125 return;
126 }
127
128 // create the page list
129 MWAWPageSpan ps(getPageSpan());
130 ps.setPageSpan(1);
131 std::vector<MWAWPageSpan> pageList(1,ps);
132 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
133 setGraphicListener(listen);
134 listen->startDocument();
135 }
136
137
138 ////////////////////////////////////////////////////////////
139 //
140 // Intermediate level
141 //
142 ////////////////////////////////////////////////////////////
143 bool GreatWksBMParser::createZones()
144 {
145 MWAWInputStreamPtr input = getInput();
146 if (input->size()<512) return false;
147 #ifdef DEBUG
148 libmwaw::DebugStream f;
149 f << "FileHeader:";
150 input->seek(0, librevenge::RVNG_SEEK_SET);
151 for (int i=0; i<256; i++) { // normally 0
152 auto val=static_cast<int>(input->readLong(2));
153 if (val)
154 f << "f" << i << "=" << val << ",";
155 }
156 ascii().addPos(0);
157 ascii().addNote(f.str().c_str());
158 #endif
159 m_state->m_picture.setBegin(512);
160 m_state->m_picture.setEnd(input->size());
161 return true;
162 }
163
164 ////////////////////////////////////////////////////////////
165 // send data
166 ////////////////////////////////////////////////////////////
167 // the picture
168 bool GreatWksBMParser::sendPicture()
169 {
170 MWAWListenerPtr listener=getGraphicListener();
171 if (!listener) {
172 MWAW_DEBUG_MSG(("GreatWksBMParser::sendPicture: can not find the listener\n"));
173 return false;
174 }
175 MWAWEntry const &entry=m_state->m_picture;
176 if (!entry.valid()) {
177 MWAW_DEBUG_MSG(("GreatWksBMParser::sendPicture: can not find the picture entry\n"));
178 return false;
179 }
180
181 MWAWInputStreamPtr input = getInput();
182 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
183 std::shared_ptr<MWAWPict> thePict(MWAWPictData::get(input, static_cast<int>(entry.length())));
184 if (!thePict) {
185 MWAW_DEBUG_MSG(("GreatWksBMParser::sendPicture: can not retrieve the picture\n"));
186 return false;
187 }
188 MWAWEmbeddedObject picture;
189 if (!thePict->getBinary(picture)) {
190 MWAW_DEBUG_MSG(("GreatWksBMParser::sendPicture: can not retrieve the picture data\n"));
191 return false;
192 }
193 MWAWPageSpan const &page=getPageSpan();
194 MWAWPosition pos(MWAWVec2f(float(page.getMarginLeft()),float(page.getMarginRight())),
195 MWAWVec2f(float(page.getPageWidth()),float(page.getPageLength())), librevenge::RVNG_INCH);
196 pos.setRelativePosition(MWAWPosition::Page);
197 pos.m_wrapping = MWAWPosition::WNone;
198 listener->insertPicture(pos, picture);
199
200 #ifdef DEBUG_WITH_FILES
201 ascii().skipZone(entry.begin(), entry.end()-1);
202 librevenge::RVNGBinaryData file;
203 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
204 input->readDataBlock(entry.length(), file);
205 static int volatile pictName = 0;
206 libmwaw::DebugStream f;
207 f << "PICT-" << ++pictName;
208 libmwaw::Debug::dumpFile(file, f.str().c_str());
209 #endif
210 return true;
211 }
212
213 ////////////////////////////////////////////////////////////
214 // read the header
215 ////////////////////////////////////////////////////////////
216 bool GreatWksBMParser::checkHeader(MWAWHeader *header, bool strict)
217 {
218 *m_state = GreatWksBMParserInternal::State();
219 MWAWInputStreamPtr input = getInput();
220 if (!input || !input->hasDataFork() || !input->checkPosition(512+10))
221 return false;
222
223 std::string type, creator;
224 /** no finder info, may be ok, but this means
225 that we may have a basic pict file, so... */
226 if (!input->getFinderInfo(type, creator))
227 return false;
228 if (creator!="ZEBR")
229 return false;
230 int vers=2;
231 if (type!="ZPNT")
232 return false;
233 if (strict) {
234 input->seek(512, librevenge::RVNG_SEEK_SET);
235 MWAWBox2f box;
236 if (MWAWPictData::check(input, static_cast<int>(input->size()-512), box)==MWAWPict::MWAW_R_BAD)
237 return false;
238 }
239 setVersion(vers);
240 if (header)
241 header->reset(MWAWDocument::MWAW_T_GREATWORKS, vers, MWAWDocument::MWAW_K_PAINT);
242
243 return true;
244 }
245
246 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: