Debian Codegraph @

src/lib/LightWayTxtGraph.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 <cmath>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <map>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWTextListener.hxx"
44 #include "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWPictMac.hxx"
47 #include "MWAWPosition.hxx"
48 #include "MWAWRSRCParser.hxx"
49 #include "MWAWSubDocument.hxx"
50
51 #include "LightWayTxtParser.hxx"
52
53 #include "LightWayTxtGraph.hxx"
54
55 /** Internal: the structures of a LightWayTxtGraph */
56 namespace LightWayTxtGraphInternal
57 {
58 ////////////////////////////////////////
59 //! Internal: the state of a LightWayTxtGraph
60 struct State {
61 //! constructor
62 State()
63 : m_numPages(-1)
64 , m_idPictMap()
65 , m_idJPEGMap()
66 {
67 }
68
69 int m_numPages /* the number of pages */;
70 /** a map id -> PICT entry */
71 std::map<int, MWAWEntry> m_idPictMap;
72 /** a map id -> JPEG entry */
73 std::map<int, MWAWEntry> m_idJPEGMap;
74 };
75 }
76
77 ////////////////////////////////////////////////////////////
78 // constructor/destructor, ...
79 ////////////////////////////////////////////////////////////
80 LightWayTxtGraph::LightWayTxtGraph(LightWayTxtParser &parser)
81 : m_parserState(parser.getParserState())
82 , m_state(new LightWayTxtGraphInternal::State)
83 , m_mainParser(&parser)
84 {
85 }
86
87 LightWayTxtGraph::~LightWayTxtGraph()
88 {
89 }
90
91 int LightWayTxtGraph::version() const
92 {
93 return m_parserState->m_version;
94 }
95
96 int LightWayTxtGraph::numPages() const
97 {
98 if (m_state->m_numPages < 0)
99 m_state->m_numPages= (m_state->m_idPictMap.size() ||m_state->m_idJPEGMap.size()) ? 1 : 0;
100 return m_state->m_numPages;
101 }
102
103 ////////////////////////////////////////////////////////////
104 //
105 // Intermediate level
106 //
107 ////////////////////////////////////////////////////////////
108 bool LightWayTxtGraph::createZones()
109 {
110 MWAWRSRCParserPtr rsrcParser = m_mainParser->getRSRCParser();
111 if (!rsrcParser) {
112 MWAW_DEBUG_MSG(("LightWayTxtGraph::createZones: can not find the entry map\n"));
113 return false;
114 }
115 auto const &entryMap = rsrcParser->getEntriesMap();
116
117 // the different pict zones
118 auto it = entryMap.lower_bound("PICT");
119 while (it != entryMap.end()) {
120 if (it->first != "PICT")
121 break;
122
123 MWAWEntry const &entry = it++->second;
124 m_state->m_idPictMap.insert
125 (std::map<int, MWAWEntry>::value_type(entry.id(), entry));
126 // fixme stored the pict id here, ...
127 librevenge::RVNGBinaryData data;
128 rsrcParser->parsePICT(entry, data);
129 }
130 it = entryMap.lower_bound("JPEG");
131 while (it != entryMap.end()) {
132 if (it->first != "JPEG")
133 break;
134
135 MWAWEntry const &entry = it++->second;
136
137 m_state->m_idJPEGMap.insert
138 (std::map<int, MWAWEntry>::value_type(entry.id(), entry));
139 }
140 return true;
141 }
142
143 ////////////////////////////////////////////////////////////
144 // low level
145 ////////////////////////////////////////////////////////////
146 bool LightWayTxtGraph::sendPICT(MWAWEntry const &entry)
147 {
148 entry.setParsed(true);
149 MWAWRSRCParserPtr rsrcParser = m_mainParser->getRSRCParser();
150
151 if (!m_parserState->m_textListener || !rsrcParser) {
152 MWAW_DEBUG_MSG(("LightWayTxtGraph::sendPICT: can not find the listener\n"));
153 return false;
154 }
155 librevenge::RVNGBinaryData data;
156 rsrcParser->parsePICT(entry, data);
157
158 MWAWInputStreamPtr input=MWAWInputStream::get(data, false);
159 if (!input) {
160 MWAW_DEBUG_MSG(("LightWayTxtGraph::sendPICT: can not find the stream\n"));
161 return false;
162 }
163 std::shared_ptr<MWAWPict> pict(MWAWPictData::get(input, int(entry.length())));
164 if (!pict)
165 return false;
166
167 MWAWBox2f bdBox=pict->getBdBox();
168 MWAWPosition pictPos(MWAWVec2f(0,0), bdBox.size(), librevenge::RVNG_POINT);
169 pictPos.setRelativePosition(MWAWPosition::Char);
170
171 MWAWEmbeddedObject picture;
172 if (pict->getBinary(picture))
173 m_parserState->m_textListener->insertPicture(pictPos, picture);
174 return true;
175 }
176
177 bool LightWayTxtGraph::sendJPEG(MWAWEntry const &entry)
178 {
179 if (!m_parserState->m_textListener) {
180 MWAW_DEBUG_MSG(("LightWayTxtGraph::sendJPEG: can not find the listener\n"));
181 return false;
182 }
183 if (!entry.valid()) {
184 MWAW_DEBUG_MSG(("LightWayTxtGraph::sendJPEG: the entry is bad\n"));
185 return false;
186 }
187
188 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
189 libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii();
190 long pos = entry.begin();
191 input->seek(pos, librevenge::RVNG_SEEK_SET);
192
193 libmwaw::DebugStream f;
194 f << "Entries(JPEG):" << entry.id();
195 ascFile.addPos(pos-4);
196 ascFile.addNote(f.str().c_str());
197
198 librevenge::RVNGBinaryData data;
199 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
200 input->readDataBlock(entry.length(), data);
201 MWAWPosition pictPos;
202 pictPos.setRelativePosition(MWAWPosition::Char);
203 MWAWVec2i sz;
204 if (findJPEGSize(data,sz)) {
205 pictPos.setSize(MWAWVec2f(sz));
206 pictPos.setUnit(librevenge::RVNG_POINT);
207 }
208 m_parserState->m_textListener->insertPicture(pictPos, MWAWEmbeddedObject(data, "image/pict"));
209
210 #ifdef DEBUG_WITH_FILES
211 if (!entry.isParsed()) {
212 ascFile.skipZone(entry.begin(), entry.end()-1);
213 libmwaw::DebugStream f2;
214 f2 << "JPEG" << entry.id() << ".jpg";
215 libmwaw::Debug::dumpFile(data, f2.str().c_str());
216 }
217 #endif
218 entry.setParsed(true);
219
220 return true;
221 }
222
223 bool LightWayTxtGraph::findJPEGSize(librevenge::RVNGBinaryData const &data, MWAWVec2i &sz)
224 {
225 sz = MWAWVec2i(100,100);
226 MWAWInputStreamPtr input=MWAWInputStream::get(data, false);
227 if (!input) {
228 MWAW_DEBUG_MSG(("LightWayTxtGraph::findJPEGSize: can not find the stream\n"));
229 return false;
230 }
231
232 if (input->readULong(4)!=0xFFD8FFE0) {
233 MWAW_DEBUG_MSG(("LightWayTxtGraph::findJPEGSize: invalid header\n"));
234 return false;
235 }
236 long pos = input->tell();
237 auto len = static_cast<int>(input->readULong(2));
238 if (input->readULong(4)!=0x4a464946) {
239 MWAW_DEBUG_MSG(("LightWayTxtGraph::findJPEGSize: not a JFIF file\n"));
240 return false;
241 }
242 input->seek(pos+len, librevenge::RVNG_SEEK_SET);
243 while (!input->isEnd()) {
244 auto header = static_cast<int>(input->readULong(2));
245 pos = input->tell();
246 len = static_cast<int>(input->readULong(2));
247 if ((header&0xFF00) != 0xFF00) {
248 MWAW_DEBUG_MSG(("LightWayTxtGraph::findJPEGSize: oops bad data header\n"));
249 break;
250 }
251 if (header != 0xFFC0) {
252 input->seek(pos+len, librevenge::RVNG_SEEK_SET);
253 continue;
254 }
255 input->seek(1, librevenge::RVNG_SEEK_CUR);
256 int dim[2];
257 for (auto &d : dim) d = static_cast<int>(input->readULong(2));
258 sz = MWAWVec2i(dim[1],dim[0]);
259 return true;
260 }
261 return false;
262 }
263
264 ////////////////////////////////////////////////////////////
265 // send data
266 ////////////////////////////////////////////////////////////
267
268 void LightWayTxtGraph::send(int id)
269 {
270 if (m_state->m_idJPEGMap.find(999+id) != m_state->m_idJPEGMap.end()) {
271 sendJPEG(m_state->m_idJPEGMap.find(999+id)->second);
272 return;
273 }
274 if (m_state->m_idPictMap.find(999+id) != m_state->m_idPictMap.end()) {
275 sendPICT(m_state->m_idPictMap.find(999+id)->second);
276 return;
277 }
278 MWAW_DEBUG_MSG(("LightWayTxtGraph::send: can not find graphic %d\n", id));
279 }
280
281 bool LightWayTxtGraph::sendPageGraphics()
282 {
283 return true;
284 }
285
286 void LightWayTxtGraph::flushExtra()
287 {
288 #ifdef DEBUG
289 for (auto const &it : m_state->m_idPictMap) {
290 MWAWEntry const &entry = it.second;
291 if (entry.isParsed()) continue;
292 sendPICT(entry);
293 }
294 for (auto const &it : m_state->m_idJPEGMap) {
295 MWAWEntry const &entry = it.second;
296 if (entry.isParsed()) continue;
297 sendJPEG(entry);
298 }
299 #endif
300 }
301 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: