Debian Codegraph @

src/lib/NisusWrtGraph.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 "NisusWrtParser.hxx"
52 #include "NisusWrtStruct.hxx"
53
54 #include "NisusWrtGraph.hxx"
55
56 /** Internal: the structures of a NisusWrtGraph */
57 namespace NisusWrtGraphInternal
58 {
59 //! a RSSO entry in a pict file
60 struct RSSOEntry {
61 //! constructor
62 RSSOEntry()
63 : m_id(-1)
64 , m_position()
65 {
66 }
67 //! operator<<
68 friend std::ostream &operator<<(std::ostream &o, RSSOEntry const &entry)
69 {
70 o << "id=" << entry.m_id << ",";
71 o << "box=" << entry.m_position << ",";
72 return o;
73 }
74 //! the id
75 int m_id;
76 //! the bdbox
77 MWAWBox2f m_position;
78 };
79 ////////////////////////////////////////
80 //! Internal: the state of a NisusWrtGraph
81 struct State {
82 //! constructor
83 State()
84 : m_numPages(0)
85 , m_maxPageGraphic(0)
86 , m_idPictMap()
87 , m_idRssoMap()
88 {
89 }
90
91 int m_numPages /* the number of pages */;
92 //! the last page containing page graphic
93 int m_maxPageGraphic;
94 //! the map pictId -> pictEntry
95 std::map<int, MWAWEntry> m_idPictMap;
96 //! the map id -> rssoEntry
97 std::map<int, MWAWEntry> m_idRssoMap;
98 };
99
100 ////////////////////////////////////////
101 //! Internal: the subdocument of a NisusWrtGraph
102 class SubDocument final : public MWAWSubDocument
103 {
104 public:
105 SubDocument(NisusWrtGraph &pars, MWAWInputStreamPtr const &input, int id, MWAWPosition const &pos)
106 : MWAWSubDocument(pars.m_mainParser, input, MWAWEntry())
107 , m_graphParser(&pars)
108 , m_id(id)
109 , m_position(pos)
110 {
111 }
112
113 //! destructor
114 ~SubDocument() final {}
115
116 //! operator!=
117 bool operator!=(MWAWSubDocument const &doc) const final;
118
119 //! the parser function
120 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
121
122 protected:
123 /** the graph parser */
124 NisusWrtGraph *m_graphParser;
125 //! the pict id
126 int m_id;
127 //! the pict position
128 MWAWPosition m_position;
129 private:
130 SubDocument(SubDocument const &orig) = delete;
131 SubDocument &operator=(SubDocument const &orig) = delete;
132 };
133
134 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
135 {
136 if (!listener.get()) {
137 MWAW_DEBUG_MSG(("NisusWrtGraphInternal::SubDocument::parse: no listener\n"));
138 return;
139 }
140 if (!m_graphParser) {
141 MWAW_DEBUG_MSG(("NisusWrtGraphInternal::SubDocument::parse: no graph parser\n"));
142 return;
143 }
144 long pos = m_input->tell();
145 m_graphParser->sendPicture(m_id, true, m_position);
146 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
147 }
148
149 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
150 {
151 if (MWAWSubDocument::operator!=(doc)) return true;
152 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
153 if (!sDoc) return true;
154 if (m_graphParser != sDoc->m_graphParser) return true;
155 if (m_id != sDoc->m_id) return true;
156 if (m_position != sDoc->m_position) return true;
157 return false;
158 }
159 }
160
161 ////////////////////////////////////////////////////////////
162 // constructor/destructor, ...
163 ////////////////////////////////////////////////////////////
164 NisusWrtGraph::NisusWrtGraph(NisusWrtParser &parser)
165 : m_parserState(parser.getParserState())
166 , m_state(new NisusWrtGraphInternal::State)
167 , m_mainParser(&parser)
168 {
169 }
170
171 NisusWrtGraph::~NisusWrtGraph()
172 { }
173
174 int NisusWrtGraph::version() const
175 {
176 return m_parserState->m_version;
177 }
178
179 int NisusWrtGraph::numPages() const
180 {
181 return m_state->m_maxPageGraphic;
182 }
183
184 ////////////////////////////////////////////////////////////
185 //
186 // Intermediate level
187 //
188 ////////////////////////////////////////////////////////////
189 bool NisusWrtGraph::createZones()
190 {
191 MWAWRSRCParserPtr rsrcParser = m_mainParser->getRSRCParser();
192 if (!rsrcParser) {
193 MWAW_DEBUG_MSG(("NisusWrtGraph::createZones: can not find the entry map\n"));
194 return false;
195 }
196 auto &entryMap = rsrcParser->getEntriesMap();
197
198 // the different pict zones
199 auto it = entryMap.lower_bound("PICT");
200 while (it != entryMap.end()) {
201 if (it->first != "PICT")
202 break;
203 MWAWEntry const &entry = it++->second;
204 m_state->m_idPictMap[entry.id()]=entry;
205 }
206 it = entryMap.lower_bound("RSSO");
207 while (it != entryMap.end()) {
208 if (it->first != "RSSO")
209 break;
210 MWAWEntry const &entry = it++->second;
211 m_state->m_idRssoMap[entry.id()]=entry;
212 }
213
214 // number of page graphic
215 it = entryMap.lower_bound("PGRA");
216 while (it != entryMap.end()) {
217 if (it->first != "PGRA")
218 break;
219 MWAWEntry const &entry = it++->second;
220 readPGRA(entry);
221 }
222
223 // a picture position ?
224 it = entryMap.lower_bound("PLAC");
225 while (it != entryMap.end()) {
226 if (it->first != "PLAC")
227 break;
228 MWAWEntry const &entry = it++->second;
229 readPLAC(entry);
230 }
231 it = entryMap.lower_bound("PLDT");
232 while (it != entryMap.end()) {
233 if (it->first != "PLDT")
234 break;
235 MWAWEntry &entry = it++->second;
236 entry.setName("PLDT");
237 NisusWrtStruct::RecursifData data(NisusWrtStruct::Z_Main);
238 data.read(*m_mainParser, entry);
239 readPLDT(data);
240 }
241
242 return true;
243 }
244
245 // read the PLAC zone ( a list of picture placement ? )
246 bool NisusWrtGraph::readPLAC(MWAWEntry const &entry)
247 {
248 if ((!entry.valid()&&entry.length()) || (entry.length()%202)) {
249 MWAW_DEBUG_MSG(("NisusWrtGraph::readPLAC: the entry is bad\n"));
250 return false;
251 }
252 entry.setParsed(true);
253 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
254 libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii();
255 long pos = entry.begin();
256 input->seek(pos, librevenge::RVNG_SEEK_SET);
257
258 auto numElt = int(entry.length()/202);
259 libmwaw::DebugStream f;
260 f << "Entries(PLAC)[" << entry.id() << "]:N=" << numElt;
261 ascFile.addPos(pos-4);
262 ascFile.addNote(f.str().c_str());
263
264 for (int i = 0; i < numElt; i++) {
265 pos = input->tell();
266 f.str("");
267 f << "PLAC" << i << ":";
268 auto val = static_cast<int>(input->readULong(2));
269 f << "pictId=" << val;
270 ascFile.addDelimiter(input->tell(),'|');
271 ascFile.addPos(pos);
272 ascFile.addNote(f.str().c_str());
273 input->seek(pos+202, librevenge::RVNG_SEEK_SET);
274 }
275 return true;
276 }
277
278 // read PLDT zone: a unknown zone (a type, an id/anchor type? and a bdbox )
279 bool NisusWrtGraph::readPLDT(NisusWrtStruct::RecursifData const &data)
280 {
281 if (!data.m_info || data.m_info->m_zoneType >= 3) {
282 MWAW_DEBUG_MSG(("NisusWrtGraph::readPLDT: find unexpected zoneType\n"));
283 return false;
284 }
285
286 if (!data.m_childList.size())
287 return true;
288
289 if (data.m_childList.size() > 1) {
290 MWAW_DEBUG_MSG(("NisusWrtGraph::readPLDT: level 0 node contains more than 1 node\n"));
291 }
292 if (data.m_childList[0].isLeaf()) {
293 MWAW_DEBUG_MSG(("NisusWrtGraph::readPLDT: level 1 node is a leaf\n"));
294 return false;
295 }
296 auto const &mainData = *data.m_childList[0].m_data;
297 // NisusWrtGraphInternal::Zone &zone = m_state->m_zones[static_cast<int>(data.m_info->m_zoneType)];
298 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
299 libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii();
300 libmwaw::DebugStream f;
301
302 long val;
303 for (auto const &mData : mainData.m_childList) {
304 if (mData.isLeaf()) {
305 MWAW_DEBUG_MSG(("NisusWrtGraph::readPLDT: oops some level 2 node are leaf\n"));
306 continue;
307 }
308 NisusWrtStruct::RecursifData const &dt=*mData.m_data;
309 /* type == 7fffffff and wh = 2 */
310 if (dt.m_childList.size() != 1) {
311 MWAW_DEBUG_MSG(("NisusWrtGraph::readPLDT: find an odd number of 3 leavers\n"));
312 continue;
313 }
314 auto const &child= dt.m_childList[0];
315 if (!child.isLeaf() || child.m_entry.length() < 14) {
316 MWAW_DEBUG_MSG(("NisusWrtGraph::readPLDT: find an odd level 3 leaf\n"));
317 continue;
318 }
319
320 long pos = child.m_entry.begin();
321 input->seek(pos, librevenge::RVNG_SEEK_SET);
322 f.str("");
323 std::string type(""); // find different small string here
324 for (int i = 0; i < 4; i++)
325 type += char(input->readULong(1));
326 f << type << ",";
327 val = input->readLong(2); // a small number find 4,5,b,d
328 if (val) f << "f0=" << val << ",";
329 int dim[4];
330 for (auto &d : dim) d = static_cast<int>(input->readLong(2));
331 f << "bdbox=(" << dim[1] << "x" << dim[0] << "<->"
332 << dim[3] << "x" << dim[2] << "),";
333 ascFile.addPos(pos-12);
334 ascFile.addNote(f.str().c_str());
335 }
336 return true;
337 }
338
339 //! read the PGRA resource: the number of page graphic ? (id 20000)
340 bool NisusWrtGraph::readPGRA(MWAWEntry const &entry)
341 {
342 if (!entry.valid() || entry.length() < 2) {
343 MWAW_DEBUG_MSG(("NisusWrtGraph::readPGRA: the entry is bad\n"));
344 return false;
345 }
346 if (entry.id() != 20000) {
347 MWAW_DEBUG_MSG(("NisusWrtGraph::readPGRA: the entry id %d is odd\n", entry.id()));
348 }
349 entry.setParsed(true);
350 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
351 libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii();
352 long pos = entry.begin();
353 input->seek(pos, librevenge::RVNG_SEEK_SET);
354
355 libmwaw::DebugStream f;
356 if (entry.id() != 20000)
357 f << "Entries(PGRA)[#" << entry.id() << "]:";
358 else
359 f << "Entries(PGRA):";
360 // a number between 0 and 2: seems related to PICT20000 -> PICT20000+N-1
361 m_state->m_maxPageGraphic = static_cast<int>(input->readLong(2));
362 f << "lastPage[withGraphic]=" << m_state->m_maxPageGraphic << ",";
363
364 if (entry.length()!=2)
365 f << "###size=" << entry.length() << ",";
366
367 ascFile.addPos(pos-4);
368 ascFile.addNote(f.str().c_str());
369 return true;
370 }
371
372 ////////////////////////////////////////////////////////////
373 // low level
374 ////////////////////////////////////////////////////////////
375 std::vector<NisusWrtGraphInternal::RSSOEntry> NisusWrtGraph::findRSSOEntry(MWAWInputStreamPtr input) const
376 {
377 std::vector<NisusWrtGraphInternal::RSSOEntry> listRSSO;
378 if (!input) {
379 MWAW_DEBUG_MSG(("NisusWrtGraph::findRSSOEntry: can not find the input\n"));
380 return listRSSO;
381 }
382
383 input->seek(10, librevenge::RVNG_SEEK_SET);
384 auto header = static_cast<int>(input->readULong(2));
385 if (header==0x0011) {
386 if (input->readULong(2) != 0x2ff)
387 return listRSSO;
388 // ok look like a pict2
389 }
390 else if (header != 0x1101)
391 return listRSSO;
392
393 // look for: 00a1006400104e495349000900b3000901dc01f90002
394 while (!input->isEnd()) {
395 long pos = input->tell();
396 auto val=static_cast<int>(input->readULong(4));
397 int depl = 0;
398 if (val == 0x104e4953) ;
399 else if (val == 0x4e495349) depl=-1;
400 else if (val == 0x49534900) depl=-2;
401 else if (val == 0x53490009) depl=-3;
402 else continue;
403 input->seek(depl-8, librevenge::RVNG_SEEK_CUR);
404 bool ok = input->readULong(1) == 0xa1;
405 ok = ok && input->readULong(4) == 0x00640010;
406 ok = ok && input->readULong(4) == 0x4e495349;
407 ok = ok && input->readULong(2) == 0x0009;
408 if (!ok) {
409 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
410 continue;
411 }
412 float dim[4];
413 for (float &i : dim) i = float(input->readLong(2));
414 if (input->isEnd()) break;
415 NisusWrtGraphInternal::RSSOEntry rsso;
416 rsso.m_id = static_cast<int>(input->readLong(2));
417 if (input->isEnd()) break;
418 rsso.m_position=MWAWBox2f(MWAWVec2f(dim[1], dim[0]), MWAWVec2f(dim[3], dim[2]));
419 if (rsso.m_id > 0)
420 listRSSO.push_back(rsso);
421 else if (version() > 3) {
422 MWAW_DEBUG_MSG(("NisusWrtGraph::findRSSOEntry: find odd rsso id%d\n", rsso.m_id));
423 }
424 }
425
426 return listRSSO;
427 }
428
429 ////////////////////////////////////////////////////////////
430 // send data
431 ////////////////////////////////////////////////////////////
432 bool NisusWrtGraph::sendPicture(int pictId, bool inPictRsrc, MWAWPosition pictPos)
433 {
434 MWAWRSRCParserPtr rsrcParser = m_mainParser->getRSRCParser();
435 MWAWTextListenerPtr listener=m_parserState->m_textListener;
436 if (!listener) {
437 MWAW_DEBUG_MSG(("NisusWrtGraph::sendPicture: can not find the listener\n"));
438 return true;
439 }
440 std::map<int, MWAWEntry> &pictMap = inPictRsrc ?
441 m_state->m_idPictMap : m_state->m_idRssoMap;
442 if (pictMap.find(pictId) == pictMap.end()) {
443 if (version() <= 3 && !inPictRsrc)
444 return true;
445 MWAW_DEBUG_MSG(("NisusWrtGraph::sendPicture: can not find the picture\n"));
446 return false;
447 }
448 MWAWEntry &entry = pictMap.find(pictId)->second;
449 librevenge::RVNGBinaryData data;
450 bool ok = rsrcParser->parsePICT(entry, data) && data.size();
451 if (!ok) {
452 MWAW_DEBUG_MSG(("NisusWrtGraph::sendPicture: can not read the picture\n"));
453 }
454 entry.setParsed(true);
455 if (!ok) return true;
456
457 std::vector<NisusWrtGraphInternal::RSSOEntry> listRSSO;
458 if (inPictRsrc) {
459 // we must first look for RSSO entry
460 MWAWInputStreamPtr dataInput=MWAWInputStream::get(data,false);
461 if (dataInput)
462 listRSSO=findRSSOEntry(dataInput);
463 }
464
465 if (listRSSO.size() && (pictPos.m_anchorTo == MWAWPosition::Char ||
466 pictPos.m_anchorTo == MWAWPosition::CharBaseLine)) {
467 // we need to create a frame
468 MWAWPosition framePos(pictPos.origin(), pictPos.size(), librevenge::RVNG_POINT);;
469 framePos.setRelativePosition(MWAWPosition::Char,
470 MWAWPosition::XLeft, MWAWPosition::YTop);
471 framePos.m_wrapping = MWAWPosition::WBackground;
472 pictPos.setRelativePosition(MWAWPosition::Frame);
473 pictPos.setOrigin(MWAWVec2f(0,0));
474 MWAWSubDocumentPtr subdoc
475 (new NisusWrtGraphInternal::SubDocument(*this, m_mainParser->rsrcInput(), pictId, pictPos));
476 listener->insertTextBox(framePos, subdoc);
477 return true;
478 }
479 // first the picture
480 listener->insertPicture(pictPos, MWAWEmbeddedObject(data, "image/pict"));
481 // then the author possible picture
482 pictPos.setClippingPosition(MWAWVec2f(), MWAWVec2f());
483 for (auto const &rssoEntry : listRSSO) {
484 MWAWPosition rssoPos(pictPos);
485 rssoPos.setOrigin(pictPos.origin()+rssoEntry.m_position.min());
486 rssoPos.setSize(rssoEntry.m_position.size());
487 sendPicture(rssoEntry.m_id, false, rssoPos);
488 }
489 return true;
490 }
491
492 bool NisusWrtGraph::sendPageGraphics()
493 {
494 MWAWRSRCParserPtr rsrcParser = m_mainParser->getRSRCParser();
495 if (!m_parserState->m_textListener) {
496 MWAW_DEBUG_MSG(("NisusWrtGraph::sendPageGraphics: can not find the listener\n"));
497 return true;
498 }
499 MWAWVec2f LT = 72.f*m_mainParser->getPageLeftTop();
500 for (int i = 0; i < m_state->m_maxPageGraphic; i++) {
501 if (m_state->m_idPictMap.find(20000+i)==m_state->m_idPictMap.end())
502 continue;
503 MWAWEntry &entry = m_state->m_idPictMap.find(20000+i)->second;
504 librevenge::RVNGBinaryData data;
505 if (!rsrcParser->parsePICT(entry, data) || !data.size()) {
506 MWAW_DEBUG_MSG(("NisusWrtGraph::sendPageGraphics: can not read the file picture\n"));
507 continue;
508 }
509 MWAWInputStreamPtr dataInput=MWAWInputStream::get(data, false);
510 if (!dataInput) continue;
511 dataInput->seek(0, librevenge::RVNG_SEEK_SET);
512 MWAWBox2f box;
513 if (MWAWPictData::check(dataInput, static_cast<int>(data.size()), box) == MWAWPict::MWAW_R_BAD) {
514 MWAW_DEBUG_MSG(("NisusWrtGraph::sendPageGraphics: can not determine the picture type\n"));
515 continue;
516 }
517 MWAWPosition pictPos(box.min()+LT, box.size(), librevenge::RVNG_POINT);
518 pictPos.setRelativePosition(MWAWPosition::Page);
519 pictPos.m_wrapping = MWAWPosition::WBackground;
520 pictPos.setPage(i+1);
521 sendPicture(20000+i, true, pictPos);
522 }
523 return true;
524 }
525
526 void NisusWrtGraph::flushExtra()
527 {
528 for (auto const &it : m_state->m_idPictMap) {
529 MWAWEntry const &entry = it.second;
530 if (entry.isParsed()) continue;
531 MWAW_DEBUG_MSG(("NisusWrtGraph::sendPicture: picture unparsed: %d\n", entry.id()));
532 MWAWPosition pictPos(MWAWVec2f(0,0), MWAWVec2f(1.,1.));
533 pictPos.setRelativePosition(MWAWPosition::Char);
534 sendPicture(entry.id(), true, pictPos);
535 }
536 for (auto const &it : m_state->m_idRssoMap) {
537 MWAWEntry const &entry = it.second;
538 if (entry.isParsed()) continue;
539 MWAW_DEBUG_MSG(("NisusWrtGraph::sendPicture: rsso picture unparsed: %d\n", entry.id()));
540 MWAWPosition pictPos(MWAWVec2f(0,0), MWAWVec2f(1.,1.));
541 pictPos.setRelativePosition(MWAWPosition::Char);
542 sendPicture(entry.id(), false, pictPos);
543 }
544 }
545 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: