Debian Codegraph @

src/lib/GreatWksDRParser.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 "MWAWParagraph.hxx"
45 #include "MWAWPosition.hxx"
46 #include "MWAWPrinter.hxx"
47 #include "MWAWSection.hxx"
48 #include "MWAWRSRCParser.hxx"
49 #include "MWAWSubDocument.hxx"
50
51 #include "GreatWksDocument.hxx"
52 #include "GreatWksGraph.hxx"
53 #include "GreatWksText.hxx"
54
55 #include "GreatWksDRParser.hxx"
56
57 /** Internal: the structures of a GreatWksDRParser */
58 namespace GreatWksDRParserInternal
59 {
60
61 ////////////////////////////////////////
62 //! Internal: the state of a GreatWksDRParser
63 struct State {
64 //! constructor
65 State()
66 : m_columnsWidth()
67 , m_hasColSep(false)
68 , m_actPage(0)
69 , m_numPages(0)
70 , m_headerHeight(0)
71 , m_footerHeight(0)
72 {
73 for (auto &hf : m_hfFlags) hf=false;
74 }
75 //! returns the number of expected header/footer zones
76 int numHeaderFooters() const
77 {
78 int num=0;
79 if (m_hfFlags[2]) num++; // header
80 if (m_hfFlags[3]) num++; // footer
81 if (m_hfFlags[1]) num*=2; // lf page
82 return num;
83 }
84
85 //! returns a section
86 MWAWSection getSection() const
87 {
88 MWAWSection sec;
89 size_t numCols = m_columnsWidth.size()/2;
90 if (numCols <= 1)
91 return sec;
92 sec.m_columns.resize(size_t(numCols));
93 if (m_hasColSep)
94 sec.m_columnSeparator=MWAWBorder();
95 for (size_t c=0; c < numCols; c++) {
96 double wSep=0;
97 if (c)
98 wSep += sec.m_columns[c].m_margins[libmwaw::Left]=
99 double(m_columnsWidth[2*c]-m_columnsWidth[2*c-1])/72./2.;
100 if (c+1!=numCols)
101 wSep+=sec.m_columns[c].m_margins[libmwaw::Right]=
102 double(m_columnsWidth[2*c+2]-m_columnsWidth[2*c+1])/72./2.;
103 sec.m_columns[c].m_width =
104 double(m_columnsWidth[2*c+1]-m_columnsWidth[2*c])+72.*wSep;
105 sec.m_columns[c].m_widthUnit = librevenge::RVNG_POINT;
106 }
107 return sec;
108 }
109
110 //! the columns dimension
111 std::vector<double> m_columnsWidth;
112 //! flags to define header/footer (titlePage, l/rPage, header, footer)
113 bool m_hfFlags[4];
114 //! true if columns have columns separator
115 bool m_hasColSep;
116 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
117
118 int m_headerHeight /** the header height if known */,
119 m_footerHeight /** the footer height if known */;
120 };
121 }
122
123 ////////////////////////////////////////////////////////////
124 // constructor/destructor, ...
125 ////////////////////////////////////////////////////////////
126 GreatWksDRParser::GreatWksDRParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
127 : MWAWGraphicParser(input, rsrcParser, header)
128 , m_state()
129 , m_document()
130 {
131 init();
132 }
133
134 GreatWksDRParser::~GreatWksDRParser()
135 {
136 }
137
138 void GreatWksDRParser::init()
139 {
140 resetGraphicListener();
141 setAsciiName("main-1");
142
143 m_state.reset(new GreatWksDRParserInternal::State);
144
145 // reduce the margin (in case, the page is not defined)
146 getPageSpan().setMargins(0.1);
147
148 m_document.reset(new GreatWksDocument(*this));
149 }
150
151 ////////////////////////////////////////////////////////////
152 // the parser
153 ////////////////////////////////////////////////////////////
154 void GreatWksDRParser::parse(librevenge::RVNGDrawingInterface *docInterface)
155 {
156 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
157 bool ok = false;
158 try {
159 // create the asciiFile
160 ascii().setStream(getInput());
161 ascii().open(asciiName());
162
163 checkHeader(nullptr);
164 ok = createZones();
165 if (ok) {
166 createDocument(docInterface);
167 m_document->getGraphParser()->sendPageGraphics();
168 m_document->getTextParser()->sendMainText();
169 #ifdef DEBUG
170 m_document->getTextParser()->flushExtra();
171 #endif
172 }
173 ascii().reset();
174 }
175 catch (...) {
176 MWAW_DEBUG_MSG(("GreatWksDRParser::parse: exception catched when parsing\n"));
177 ok = false;
178 }
179
180 resetGraphicListener();
181 if (!ok) throw(libmwaw::ParseException());
182 }
183
184 ////////////////////////////////////////////////////////////
185 // create the document
186 ////////////////////////////////////////////////////////////
187 void GreatWksDRParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
188 {
189 if (!documentInterface) return;
190 if (getGraphicListener()) {
191 MWAW_DEBUG_MSG(("GreatWksDRParser::createDocument: listener already exist\n"));
192 return;
193 }
194
195 // update the page
196 m_state->m_actPage = 0;
197
198 // create the page list
199 int numPages = 1;
200 if (m_document->getGraphParser()->numPages() > numPages)
201 numPages = m_document->getGraphParser()->numPages();
202 if (m_document->getTextParser()->numPages() > numPages)
203 numPages = m_document->getTextParser()->numPages();
204 m_state->m_numPages = numPages;
205
206 std::vector<MWAWPageSpan> pageList;
207 MWAWPageSpan ps(getPageSpan());
208 ps.setPageSpan(numPages);
209 pageList.push_back(ps);
210 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
211 setGraphicListener(listen);
212 listen->startDocument();
213 }
214
215 ////////////////////////////////////////////////////////////
216 //
217 // Intermediate level
218 //
219 ////////////////////////////////////////////////////////////
220 bool GreatWksDRParser::createZones()
221 {
222 m_document->readRSRCZones();
223 MWAWInputStreamPtr input = getInput();
224 ascii().addPos(40);
225 ascii().addNote("Entries(GZoneHeader)");
226 ascii().addDelimiter(68,'|');
227 long pos = 74;
228 input->seek(pos, librevenge::RVNG_SEEK_SET);
229 if (!m_document->getTextParser()->readFontNames())
230 input->seek(pos, librevenge::RVNG_SEEK_SET);
231
232 bool ok=m_document->getGraphParser()->readGraphicZone();
233 if (!input->isEnd()) {
234 pos = input->tell();
235 MWAW_DEBUG_MSG(("GreatWksDRParser::createZones: find some extra data\n"));
236 ascii().addPos(pos);
237 ascii().addNote("Entries(Loose):");
238 ascii().addPos(pos+200);
239 ascii().addNote("_");
240 }
241 return ok;
242 }
243
244 ////////////////////////////////////////////////////////////
245 // read the header
246 ////////////////////////////////////////////////////////////
247 bool GreatWksDRParser::checkHeader(MWAWHeader *header, bool strict)
248 {
249 *m_state = GreatWksDRParserInternal::State();
250 if (!m_document->checkHeader(header,strict)) return false;
251 return getParserState()->m_kind==MWAWDocument::MWAW_K_DRAW;
252 }
253
254
255
256 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: