Debian Codegraph @

src/lib/WPS8Graph.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2009, 2011 Alonso Laurent ([email protected])
11 * Copyright (C) 2006, 2007 Andrew Ziem
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 * Copyright (C) 2004 Marc Maurer ([email protected])
14 * Copyright (C) 2003-2005 William Lachance ([email protected])
15 *
16 * For minor contributions see the git repository.
17 *
18 * Alternatively, the contents of this file may be used under the terms
19 * of the GNU Lesser General Public License Version 2.1 or later
20 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
21 * applicable instead of those above.
22 *
23 * For further information visit http://libwps.sourceforge.net
24 */
25
26 #include <string.h>
27
28 #include <iomanip>
29 #include <iostream>
30
31 #include <librevenge/librevenge.h>
32
33 #include "WPSContentListener.h"
34 #include "WPSEntry.h"
35 #include "WPSFont.h"
36 #include "WPSOLEObject.h"
37 #include "WPSOLEParser.h"
38 #include "WPSParagraph.h"
39 #include "WPSPosition.h"
40 #include "WPSStream.h"
41
42 #include "WPS8.h"
43
44 #include "WPS8Graph.h"
45
46
47 /** Internal: the structures of a WPS8Graph */
48 namespace WPS8GraphInternal
49 {
50 /** Internal: a complex border picture
51 *
52 * It consists in series of at most 8 pictures which forms the border */
53 struct Border
54 {
55 //! constructor
56 Border()
57 : m_name("")
58 , m_pictList()
59 , m_parsed(false)
60 {
61 for (int &i : m_borderSize) i = -1;
62 for (int &i : m_borderId) i = -1;
63 }
64
65 //! Internal: the border name
66 std::string m_name;
67 /** three value to stored the border's sizes
68 *
69 * Checkme: corner size, following by horizontal, vertical size in points?
70 */
71 int m_borderSize[3];
72 //! the picture uses to draw TopLeft corner, Top border, TopRight corner, Right border...
73 int m_borderId[8];
74 //! the border's pictures: 1 to 8 pictures..
75 std::vector<WPSEmbeddedObject> m_pictList;
76 //! flag to know if the border was sent to the listener
77 bool m_parsed;
78 };
79
80 //! Internal: the state of a WPS8Graph
81 struct State
82 {
83 State()
84 : m_version(-1)
85 , m_numPages(0)
86 , m_borderMap()
87 , m_ibgfMap()
88 , m_pictMap()
89 , m_oleMap() {}
90 //! the version
91 int m_version;
92 //! the number page
93 int m_numPages;
94 //! a map id -> border
95 std::map<int, Border> m_borderMap;
96 //! a map id -> ibgf entry (background picture entry)
97 std::map<int, WPSEntry> m_ibgfMap;
98 //! a map id -> pictData
99 std::map<int, WPSEmbeddedObject> m_pictMap;
100 //! a map id -> OleData
101 std::map<int, WPSEmbeddedObject> m_oleMap;
102 };
103 }
104
105 ////////////////////////////////////////////////////////////
106 // constructor/destructor
107 ////////////////////////////////////////////////////////////
108 WPS8Graph::WPS8Graph(WPS8Parser &parser)
109 : m_listener()
110 , m_mainParser(parser)
111 , m_state()
112 , m_asciiFile(parser.ascii())
113 {
114 m_state.reset(new WPS8GraphInternal::State);
115 }
116
117 WPS8Graph::~WPS8Graph()
118 { }
119
120 ////////////////////////////////////////////////////////////
121 // update the positions and send data to the listener
122 ////////////////////////////////////////////////////////////
123 int WPS8Graph::version() const
124 {
125 if (m_state->m_version <= 0)
126 m_state->m_version = m_mainParser.version();
127 return m_state->m_version;
128 }
129
130 int WPS8Graph::numPages() const
131 {
132 return m_state->m_numPages;
133 }
134
135 ////////////////////////////////////////////////////////////
136 // update the positions and send data to the listener
137 ////////////////////////////////////////////////////////////
138 void WPS8Graph::computePositions() const
139 {
140 m_state->m_numPages = (m_state->m_pictMap.size() || m_state->m_oleMap.size()) ? 1 : 0;
141 }
142
143 void WPS8Graph::storeObjects(std::map<int,WPSEmbeddedObject> const &objectsMap)
144 {
145 m_state->m_oleMap=objectsMap;
146 }
147
148 ////////////////////////////////////////////////////////////
149 // find all structures which correspond to the picture
150 ////////////////////////////////////////////////////////////
151 bool WPS8Graph::readStructures(RVNGInputStreamPtr const &input)
152 {
153 auto const &nameTable = m_mainParser.getNameEntryMap();
154
155 // contains a text and 8 borders cells?
156 auto pos = nameTable.lower_bound("BDR ");
157 while (nameTable.end() != pos)
158 {
159 WPSEntry const &entry = pos++->second;
160 if (!entry.hasName("BDR ")) break;
161 if (!entry.hasType("WBDR")) continue;
162 readBDR(input, entry);
163 }
164
165 // read IBGF zone : picture type (image background f...? )
166 pos = nameTable.lower_bound("IBGF");
167 while (pos != nameTable.end())
168 {
169 WPSEntry const &entry = pos++->second;
170 if (!entry.hasName("IBGF")) break;
171 if (!entry.hasType("IBGF")) continue;
172
173 readIBGF(input, entry);
174 }
175
176 pos = nameTable.lower_bound("PICT");
177 while (pos != nameTable.end())
178 {
179 WPSEntry const &entry = pos++->second;
180 if (!entry.hasName("PICT")) break;
181
182 readPICT(input, entry);
183 }
184
185 return true;
186 }
187
188 ////////////////////////////////////////////////////////////
189 // different function to send data to a listener
190 ////////////////////////////////////////////////////////////
191 bool WPS8Graph::sendObject(WPSPosition const &posi, int id, bool ole)
192 {
193 if (m_listener.get() == nullptr)
194 {
195 WPS_DEBUG_MSG(("WPS8Graph::sendObject: listener is not set\n"));
196 return false;
197 }
198 auto &map = ole ? m_state->m_oleMap : m_state->m_pictMap;
199
200 auto pos = map.find(id);
201 if (pos == map.end())
202 {
203 WPS_DEBUG_MSG(("WPS8Graph::sendObject: can not find %dth object\n", id));
204 return false;
205 }
206 auto &pict = pos->second;
207 pict.m_sent = true;
208
209 Vec2f size(posi.size()), naturalSize;
210 if (size.x() <= 0 || size.y()<=0)
211 {
212 size=pict.m_size;
213 if (size.x() <= 0 || size.y()<=0)
214 {
215 WPS_DEBUG_MSG(("WPS8Graph::sendObject: can not find object size\n"));
216 size=Vec2f(0.5,0.5);
217 }
218 }
219 else if (pict.m_size.x() > 0 && pict.m_size.y() > 0)
220 naturalSize = pict.m_size;
221 WPSPosition finalPos(posi);
222 finalPos.setSize(size);
223 finalPos.setNaturalSize(naturalSize);
224 m_listener->insertObject(finalPos, pict);
225 return true;
226 }
227
228 bool WPS8Graph::sendIBGF(WPSPosition const &posi, int ibgfId)
229 {
230 auto pos = m_state->m_ibgfMap.find(ibgfId);
231 if (pos == m_state->m_ibgfMap.end())
232 {
233 WPS_DEBUG_MSG(("WPS8Graph::sendIBGF: can not find background\n"));
234 return false;
235 }
236
237 WPSEntry const &ent = pos->second;
238 if (!ent.hasName("PICT"))
239 {
240 WPS_DEBUG_MSG(("WPS8Graph::sendIBGF: unknown ibgf type\n"));
241 return false;
242 }
243 return sendObject(posi, ent.id(), false);
244 }
245
246 void WPS8Graph::sendObjects(int page, int)
247 {
248 if (page != -1) return;
249 if (m_listener.get() == nullptr)
250 {
251 WPS_DEBUG_MSG(("WPS8Graph::sendObjects: listener is not set\n"));
252 return;
253 }
254
255 #ifdef DEBUG
256 bool firstSend = false;
257 #endif
258 for (int st = 0; st < 2; st++)
259 {
260 auto &map = (st == 0) ? m_state->m_pictMap : m_state->m_oleMap;
261 auto pos = map.begin();
262
263 while (pos != map.end())
264 {
265 auto &pict = pos++->second;
266 if (pict.m_sent) continue;
267
268 #ifdef DEBUG
269 if (!firstSend)
270 {
271 firstSend = true;
272 WPS_DEBUG_MSG(("WPS8Graph::sendObjects: find some extra pictures\n"));
273 m_listener->setFont(WPSFont::getDefault());
274 m_listener->setParagraph(WPSParagraph());
275 m_listener->insertEOL();
276 librevenge::RVNGString message = "--------- The original document has some extra pictures: -------- ";
277 m_listener->insertUnicodeString(message);
278 m_listener->insertEOL();
279 }
280 #endif
281
282 pict.m_sent = true;
283 Vec2f size=pict.m_size;
284 // if we do not have the size of the data, we insert small picture
285 if (size.x() <= 0 || size.y() <= 0) size.set(1.0, 1.0);
286 WPSPosition position(Vec2f(),size);
287 position.setNaturalSize(pict.m_size);
288 position.setRelativePosition(WPSPosition::CharBaseLine);
289 position.m_wrapping = WPSPosition::WDynamic;
290 m_listener->insertObject(position, pict);
291 }
292 }
293
294 #ifdef DEBUG
295 // check the border
296 for (auto const &pos : m_state->m_borderMap)
297 {
298 int id = pos.first;
299 bool parsed = pos.second.m_parsed;
300 if (parsed) continue;
301 if (!firstSend)
302 {
303 firstSend = true;
304 m_listener->setFont(WPSFont::getDefault());
305 m_listener->setParagraph(WPSParagraph());
306 m_listener->insertEOL();
307 librevenge::RVNGString message;
308 message = "--------- The original document used some complex border: -------- ";
309 m_listener->insertUnicodeString(message);
310 m_listener->insertEOL();
311 }
312 sendBorder(id);
313 }
314 #endif
315 }
316
317 void WPS8Graph::sendBorder(int borderId)
318 {
319 if (!m_listener || m_state->m_borderMap.find(borderId) == m_state->m_borderMap.end()) return;
320
321 auto &border = m_state->m_borderMap[borderId];
322 if (border.m_parsed) return;
323
324 border.m_parsed = true;
325
326 m_listener->setFont(WPSFont::getDefault());
327 m_listener->setParagraph(WPSParagraph());
328 librevenge::RVNGString message("-------");
329 message.append(border.m_name.c_str());
330 message.append("---------");
331 m_listener->insertUnicodeString(message);
332
333 WPSPosition pos(Vec2f(),Vec2f(0.5,0.5));
334 pos.setRelativePosition(WPSPosition::CharBaseLine);
335 pos.m_wrapping = WPSPosition::WDynamic;
336 for (int i = 0; i < 8; i++)
337 {
338 if (i == 0 || i == 3 || i == 5) m_listener->insertEOL();
339 static int const wh[8] = {0, 1, 2, 7, 3, 6, 5, 4 };
340 auto id = size_t(border.m_borderId[wh[i]]);
341 if (border.m_pictList[id].m_size.x() > 0 &&
342 border.m_pictList[id].m_size.y() > 0)
343 pos.setSize(border.m_pictList[id].m_size);
344 m_listener->insertObject(pos, border.m_pictList[id]);
345 if (i == 3)
346 {
347 message = librevenge::RVNGString("-----");
348 m_listener->insertUnicodeString(message);
349 }
350 }
351 }
352
353 ////////////////////////////////////////////////////////////
354 //
355 // low level
356 //
357 ////////////////////////////////////////////////////////////
358
359 // Read a PICT/MEF4 entry : read uncompressed picture of sx*sy of rgb
360 bool WPS8Graph::readPICT(RVNGInputStreamPtr const &input, WPSEntry const &entry)
361 {
362 long page_offset = entry.begin();
363 long length = entry.length();
364 long endPos = entry.end();
365
366 WPSEmbeddedObject pict;
367
368 // too small, we return 0
369 if (length < 24) return false;
370
371 if (!entry.hasType("MEF4"))
372 {
373 WPS_DEBUG_MSG(("WPS8Graph::readPICT: warning: PICT name=%s, type=%s\n",
374 entry.name().c_str(), entry.type().c_str()));
375 return false;
376 }
377
378 input->seek(page_offset, librevenge::RVNG_SEEK_SET);
379 std::string name;
380 for (int i = 0; i < 4; i++) name += char(libwps::readU8(input));
381 if (strncmp("MEF4", name.c_str(), 4))
382 {
383 WPS_DEBUG_MSG(("WPS8Graph::readPICT: warning: PICT unknown header=%s\n",
384 name.c_str()));
385 return false;
386 }
387
388 libwps::DebugStream f;
389 f << "Header:";
390 // unkn0, unkn1 : always 0 ?
391 for (int i = 0; i < 2; i++)
392 {
393 auto val = long(libwps::readU32(input)); // is one the number of meta file ?
394 if (val) f << "unknA" << i << "=" << std::hex << val << std::dec << ",";
395 }
396 pict.m_size.setX(float(libwps::readU32(input))/914400.f);
397 pict.m_size.setY(float(libwps::readU32(input))/914400.f);
398 f << "pSz(inches)=" << pict.m_size << ",";
399
400 ascii().addPos(page_offset);
401 ascii().addNote(f.str().c_str());
402 entry.setParsed();
403
404 auto stream=std::make_shared<WPSStream>(input, ascii());
405 bool ok=WPSOLEObject::readWMF(stream, pict, endPos); // we may also want to check if this is a emf file
406 if (ok)
407 {
408 if (m_state->m_pictMap.find(entry.id()) != m_state->m_pictMap.end())
409 WPS_DEBUG_MSG(("WPS8Graph::readPICT: Pict entry %d already exists\n", entry.id()));
410 else
411 m_state->m_pictMap[entry.id()] = pict;
412 }
413 else
414 input->seek(page_offset+24, librevenge::RVNG_SEEK_SET);
415
416 if (input->tell() != endPos)
417 {
418 ascii().addPos(input->tell());
419 ascii().addNote("PICT###");
420 }
421
422
423 return ok;
424 }
425
426
427 // reads a IBGF zone
428 // Warning: only seems very simple IBGF, complex may differ
429 bool WPS8Graph::readIBGF(RVNGInputStreamPtr const &input, WPSEntry const &entry)
430 {
431 libwps::DebugStream f;
432 if (!entry.hasType(entry.name()))
433 {
434 WPS_DEBUG_MSG(("WPS8Graph::readIBGF: warning: IBGF name=%s, type=%s\n",
435 entry.name().c_str(), entry.type().c_str()));
436 return false;
437 }
438
439 long page_offset = entry.begin();
440 long length = entry.length();
441
442 if (length != 26)
443 {
444 WPS_DEBUG_MSG(("WPS8Graph::readIBGF: IBGF length=0x%lx\n", static_cast<unsigned long>(length)));
445 return false;
446 }
447
448 entry.setParsed();
449 input->seek(page_offset, librevenge::RVNG_SEEK_SET);
450 std::string name;
451 for (int i = 0; i < 4; i++)
452 {
453 auto val = char(libwps::readU8(input));
454 if (val >= '0' && val <= 'z')
455 {
456 name+=val;
457 continue;
458 }
459 WPS_DEBUG_MSG(("WPS8Graph::readIBGF: invalid name %s\n", name.c_str()));
460 return false;
461 }
462
463 auto id = int(libwps::read16(input));
464
465 WPSEntry res;
466 res.setName(name);
467 res.setId(id);
468
469 // name = PICT ?
470 f << "indexEntry='" << name << "':" << id;
471
472 for (int i = 0; i < 10; i++)
473 {
474 auto val = libwps::read16(input);
475 if (val) f << ", f"<<i <<"=" << val;
476 }
477
478 ascii().addPos(page_offset);
479 ascii().addNote(f.str().c_str());
480
481 if (m_state->m_ibgfMap.find(entry.id()) != m_state->m_ibgfMap.end())
482 WPS_DEBUG_MSG(("WPS8Graph::readIBGF: warning: IBGF entry %d already exists\n",
483 entry.id()));
484 else
485 m_state->m_ibgfMap[entry.id()] = res;
486
487 return true;
488 }
489
490 // BDR/WBDR Code : read a BDR Code
491 bool WPS8Graph::readBDR(RVNGInputStreamPtr const &input, WPSEntry const &entry)
492 {
493 long page_offset = entry.begin();
494 long length = entry.length();
495 long endPos = entry.end();
496
497 // too small, we return 0
498 if (length < 20)
499 {
500 WPS_DEBUG_MSG(("WPS8Graph::readBDR: length=%ld is too short\n", length));
501 return false;
502 }
503
504 if (!entry.hasType("WBDR"))
505 {
506 WPS_DEBUG_MSG(("WPS8Graph::readBDR: warning: BDR name=%s, type=%s\n",
507 entry.name().c_str(), entry.type().c_str()));
508 return false;
509 }
510
511 entry.setParsed();
512 input->seek(page_offset, librevenge::RVNG_SEEK_SET);
513
514 WPS8GraphInternal::Border border;
515 border.m_name = entry.extra();
516 libwps::DebugStream f;
517 if (!border.m_name.empty()) f << "Header:borderName='"<<border.m_name << "',";
518
519 // 1, unk? = ~ 1/20*following size ?
520 for (int i = 0; i < 2; i++) f << libwps::read16(input) << ",";
521 // 3*size (corner size, following by x/y steps) in points?
522 f << "sizes=(";
523 for (int &i : border.m_borderSize)
524 {
525 i = libwps::read16(input);
526 f << i << ",";
527 }
528 f << "),";
529
530 bool ok = true;
531 // ids of picture which corresponds to the frame borders:
532 // TL, T, TR, R, BR, B, BL, L ?
533 f << "id=(";
534 for (int &i : border.m_borderId)
535 {
536 auto id = int(libwps::read8(input));
537 if (id < 0 || id> 8)
538 {
539 ok = false;
540 break;
541 }
542 i = id;
543 f << id << ",";
544 }
545 f << "),";
546 auto N = int(libwps::read8(input));
547 f << "Nbdr="<<N << ",";
548 auto unkn = int(libwps::read8(input));
549 if (unkn) f << "###unkn=" << unkn << ",";
550
551 if (!ok || N < 0 || N > 8 || 20+N*4 > length)
552 {
553 WPS_DEBUG_MSG(("WPS8Graph::readBDR: can not read the pictures\n"));
554 f << "###";
555 ascii().addPos(page_offset);
556 ascii().addNote(f.str().c_str());
557 return false;
558 }
559
560 long debPos = page_offset+4*N+20;
561 f << "ptr(" << std::hex;
562
563 std::vector<long> listPtrs;
564 listPtrs.push_back(debPos);
565
566 for (int i = 0; i < N; i++)
567 {
568 f << debPos << ",";
569 auto sz = long(libwps::readU32(input));
570 debPos += sz;
571 if (sz < 0 || debPos > endPos)
572 {
573 ok = false;
574 break;
575 }
576 listPtrs.push_back(debPos);
577 }
578 f << debPos << ")," << std::dec;
579
580 ascii().addPos(page_offset);
581 ascii().addNote(f.str().c_str());
582 if (!ok) return false;
583
584 if (listPtrs[size_t(N)] != endPos)
585 {
586 ascii().addPos(listPtrs[size_t(N)]);
587 ascii().addNote("###BDR");
588 }
589
590 auto stream=std::make_shared<WPSStream>(input, ascii());
591 for (size_t bd = 0; bd < size_t(N); bd++)
592 {
593 long debP = listPtrs[bd];
594 long endP = listPtrs[bd+1];
595
596 input->seek(debP, librevenge::RVNG_SEEK_SET);
597 f.str("");
598 f << "BDR(" << bd << "):";
599
600 if (debP+12 > endP || libwps::readU32(input) != 0x4154454d) // META
601 {
602 WPS_DEBUG_MSG(("WPS8Graph::readBDR: unknown type can not read the picture %d\n", int(bd)));
603 f << "###";
604 ascii().addPos(debP);
605 ascii().addNote(f.str().c_str());
606 ok = false;
607 continue;
608 }
609
610 WPSEmbeddedObject pict;
611 int dim[4];
612 for (int &i : dim)
613 {
614 i = int(libwps::read16(input));
615 f << i << ",";
616 }
617 // final picture size
618 pict.m_size.set(float(dim[2]-dim[0])/1440.f, float(dim[3]-dim[1])/1440.f);
619 bool correct=WPSOLEObject::readWMF(stream, pict, endP);
620 if (!correct) f << "###";
621 ascii().addPos(debP);
622 ascii().addNote(f.str().c_str());
623
624 if (!correct)
625 {
626 ok = false;
627 continue;
628 }
629
630 if (ok) border.m_pictList.push_back(pict);
631 }
632
633 if (!ok) return false;
634 m_state->m_borderMap[entry.id()] = border;
635 return true;
636 }
637
638 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */