Debian Codegraph @

src/lib/QXP33Parser.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libqxp project.
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
10 #include "QXP33Parser.h"
11
12 #include "QXP33Deobfuscator.h"
13 #include "QXP33Header.h"
14 #include "QXPCollector.h"
15 #include "QXPTypes.h"
16
17 namespace libqxp
18 {
19
20 using librevenge::RVNGInputStream;
21 using std::make_shared;
22 using std::vector;
23 using std::shared_ptr;
24
25 namespace
26 {
27
28 template<typename T>
29 shared_ptr<T> createBox(const QXP33Parser::ObjectHeader &header)
30 {
31 auto box = make_shared<T>();
32 box->boundingBox = header.boundingBox;
33 box->runaround = header.runaround;
34 box->boxType = header.boxType;
35 box->fill = header.fill;
36 box->cornerType = header.cornerType;
37 box->cornerRadius = header.cornerRadius;
38 box->rotation = header.rotation;
39 return box;
40 }
41
42 template<typename T>
43 shared_ptr<T> createLine(const QXP33Parser::ObjectHeader &header)
44 {
45 auto line = make_shared<T>();
46 line->boundingBox = header.boundingBox;
47 line->runaround = header.runaround;
48 line->rotation = header.rotation;
49 if (auto fill = header.fill.get_ptr())
50 {
51 if (auto color = boost::get<Color>(fill))
52 {
53 line->style.color = *color;
54 }
55 else
56 {
57 QXP_DEBUG_MSG(("Unsupported line fill type\n"));
58 }
59 }
60 return line;
61 }
62
63 }
64
65 QXP33Parser::QXP33Parser(const std::shared_ptr<librevenge::RVNGInputStream> &input, librevenge::RVNGDrawingInterface *painter, const std::shared_ptr<QXP33Header> &header)
66 : QXPParser(input, painter, header)
67 , m_header(header)
68 {
69 }
70
71 bool QXP33Parser::parseDocument(const std::shared_ptr<librevenge::RVNGInputStream> &docStream, QXPCollector &collector)
72 {
73 collector.collectDocumentProperties(m_header->documentProperties());
74
75 for (int i = 0; i < 4; ++i)
76 {
77 skipRecord(docStream);
78 }
79
80 parseFonts(docStream);
81
82 if (m_header->version() == QXP_33)
83 skipRecord(docStream);
84
85 parseColors(docStream);
86
87 skipRecord(docStream);
88 skipRecord(docStream); // don't need stylesheets, everything is included in the current style
89
90 parseHJs(docStream);
91
92 skipRecord(docStream);
93
94 parseCharFormats(docStream);
95
96 parseParagraphFormats(docStream);
97
98 skipRecord(docStream);
99
100 return true;
101 }
102
103 bool QXP33Parser::parsePages(const std::shared_ptr<librevenge::RVNGInputStream> &stream, QXPCollector &collector)
104 {
105 QXP33Deobfuscator deobfuscate(m_header->seed(), m_header->increment());
106 QXPDummyCollector dummyCollector;
107
108 for (unsigned ind = 0; ind < m_header->pagesCount() + m_header->masterPagesCount(); ++ind)
109 {
110 // don't output master pages, everything is included in normal pages
111 QXPCollector &coll = ind < m_header->masterPagesCount() ? dummyCollector : collector;
112
113 auto page = parsePage(stream);
114 coll.startPage(page);
115
116 for (unsigned i = 0; i < page.objectsCount; ++i)
117 {
118
119 parseObject(stream, deobfuscate, coll, page, i);
120 deobfuscate.next();
121 }
122
123 m_groupObjects.clear();
124 coll.endPage();
125 }
126
127 return true;
128 }
129
130 void QXP33Parser::parseColors(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
131 {
132 const unsigned end = readRecordEndOffset(stream);
133
134 try
135 {
136 skip(stream, 1);
137 unsigned count = readU8(stream);
138 skip(stream, 32);
139 for (unsigned i = 0; i < count; ++i)
140 {
141 unsigned id = readU8(stream);
142 skip(stream, 1);
143 Color color;
144 color.red = readColorComp(stream);
145 color.green = readColorComp(stream);
146 color.blue = readColorComp(stream);
147 m_colors[id] = color;
148 skip(stream, 42);
149 readName(stream); // skip
150 }
151 }
152 catch (...)
153 {
154 QXP_DEBUG_MSG(("Failed to parse colors, offset %ld\n", stream->tell()));
155 }
156
157 seek(stream, end);
158 }
159
160 CharFormat QXP33Parser::parseCharFormat(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
161 {
162 skip(stream, 2);
163
164 CharFormat result;
165 parseCommonCharFormatProps(stream, result);
166
167 skip(stream, 4);
168
169 const unsigned colorId = readU8(stream);
170
171 skip(stream, 1);
172
173 const double shade = readFraction(stream, be);
174 result.color = getColor(colorId).applyShade(shade);
175
176 skip(stream, 8);
177 result.baselineShift = readFraction(stream, be);
178
179 result.isControlChars = readU8(stream) != 0;
180
181 skip(stream, 13);
182
183 return result;
184 }
185
186 ParagraphFormat QXP33Parser::parseParagraphFormat(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
187 {
188 ParagraphFormat result;
189
190 skip(stream, 2);
191
192 bool hasRuleAbove, hasRuleBelow;
193 readParagraphFlags(stream, result.incrementalLeading, hasRuleAbove, hasRuleBelow);
194
195 skip(stream, 2);
196 result.alignment = readHorAlign(stream);
197
198 skip(stream, 4);
199 const unsigned hj = readU16(stream, be);
200 if (hj < m_hjs.size())
201 result.hj = m_hjs[hj];
202 skip(stream, 2);
203
204 result.margin.left = readFraction(stream, be);
205 result.firstLineIndent = readFraction(stream, be);
206 result.margin.right = readFraction(stream, be);
207 result.leading = readFraction(stream, be);
208 result.margin.top = readFraction(stream, be);
209 result.margin.bottom = readFraction(stream, be);
210
211 auto ruleAbove = readParagraphRule(stream);
212 auto ruleBelow = readParagraphRule(stream);
213 if (hasRuleAbove)
214 result.ruleAbove = ruleAbove;
215 if (hasRuleBelow)
216 result.ruleBelow = ruleBelow;
217
218 skip(stream, 8);
219
220 for (unsigned i = 0; i < 20; ++i)
221 {
222 const auto tabStop = parseTabStop(stream);
223 if (tabStop.isDefined())
224 {
225 result.tabStops.push_back(tabStop);
226 }
227 }
228
229 skip(stream, 6);
230
231 return result;
232 }
233
234 std::shared_ptr<HJ> QXP33Parser::parseHJ(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
235 {
236 auto hj = make_shared<HJ>();
237
238 skip(stream, 4);
239 parseHJProps(stream, *hj);
240 readName(stream);
241
242 return hj;
243 }
244
245 Page QXP33Parser::parsePage(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
246 {
247 Page page;
248 page.pageSettings = parsePageSettings(stream);
249 page.objectsCount = readU32(stream, be);
250 return page;
251 }
252
253 void QXP33Parser::parseObject(const std::shared_ptr<librevenge::RVNGInputStream> &stream, QXP33Deobfuscator &deobfuscate, QXPCollector &collector, const Page &page, const unsigned index)
254 {
255 const auto header = parseObjectHeader(stream, deobfuscate);
256
257 switch (header.contentType)
258 {
259 case ContentType::NONE:
260 switch (header.shapeType)
261 {
262 case ShapeType::LINE:
263 case ShapeType::ORTHOGONAL_LINE:
264 parseLine(stream, header, collector);
265 break;
266 case ShapeType::RECTANGLE:
267 case ShapeType::CORNERED_RECTANGLE:
268 case ShapeType::OVAL:
269 case ShapeType::POLYGON:
270 parseEmptyBox(stream, header, collector);
271 break;
272 default:
273 QXP_DEBUG_MSG(("Unsupported shape\n"));
274 throw GenericException();
275 }
276 break;
277 case ContentType::PICTURE:
278 parsePictureBox(stream, header, collector);
279 break;
280 case ContentType::TEXT:
281 parseTextBox(stream, header, collector);
282 break;
283 case ContentType::OBJECTS:
284 parseGroup(stream, header, collector, page, index);
285 break;
286 default:
287 QXP_DEBUG_MSG(("Unsupported content\n"));
288 throw GenericException();
289 }
290 }
291
292 QXP33Parser::ObjectHeader QXP33Parser::parseObjectHeader(const std::shared_ptr<librevenge::RVNGInputStream> &stream, QXP33Deobfuscator &deobfuscate)
293 {
294 ObjectHeader result;
295
296 const unsigned objectType = deobfuscate(readU8(stream, be));
297 if (m_header->version() < QXP_33)
298 {
299 switch (objectType)
300 {
301 case 0:
302 result.shapeType = ShapeType::LINE;
303 result.contentType = ContentType::NONE;
304 break;
305 case 1:
306 result.shapeType = ShapeType::ORTHOGONAL_LINE;
307 result.contentType = ContentType::NONE;
308 break;
309 case 3:
310 result.shapeType = ShapeType::RECTANGLE;
311 result.contentType = ContentType::TEXT;
312 break;
313 case 11:
314 result.shapeType = ShapeType::RECTANGLE;
315 result.contentType = ContentType::OBJECTS;
316 break;
317 case 12:
318 result.shapeType = ShapeType::RECTANGLE;
319 result.contentType = ContentType::PICTURE;
320 break;
321 case 13:
322 result.shapeType = ShapeType::CORNERED_RECTANGLE;
323 result.contentType = ContentType::PICTURE;
324 break;
325 case 14:
326 result.shapeType = ShapeType::OVAL;
327 result.contentType = ContentType::PICTURE;
328 break;
329 case 15:
330 result.shapeType = ShapeType::POLYGON;
331 result.contentType = ContentType::PICTURE;
332 break;
333 default:
334 QXP_DEBUG_MSG(("Unknown object type %u\n", objectType));
335 throw ParseError();
336 }
337 }
338
339 const unsigned colorId = readU8(stream);
340 const double shade = readFraction(stream, be);
341 const auto color = getColor(colorId).applyShade(shade);
342
343 result.contentIndex = deobfuscate(uint16_t(readU32(stream, be) & 0xffff));
344
345 bool noColor;
346 bool noRunaround;
347 readObjectFlags(stream, noColor, noRunaround);
348 if (!noColor)
349 {
350 result.fill = color;
351 }
352 result.runaround = !noRunaround;
353
354 skip(stream, 1);
355
356 result.rotation = readFraction(stream, be);
357 result.skew = readFraction(stream, be);
358
359 result.linkId = readU32(stream, be);
360 result.gradientId = readU32(stream, be);
361
362 skip(stream, 4);
363
364 const uint8_t boxFlag1 = readU8(stream);
365 const uint8_t boxFlag2 = readU8(stream);
366 bool beveled;
367 bool concave;
368 if (be)
369 {
370 result.hflip = boxFlag1 & 0x80;
371 result.vflip = boxFlag2 & 0x80;
372 beveled = boxFlag2 & 0x20;
373 concave = boxFlag2 & 0x40;
374 }
375 else
376 {
377 result.hflip = boxFlag1 & 0x1;
378 result.vflip = boxFlag2 & 0x1;
379 beveled = boxFlag2 & 0x2;
380 concave = boxFlag2 & 0x4;
381 }
382
383 if (m_header->version() == QXP_33)
384 {
385 const uint8_t contentType = readU8(stream);
386 switch (contentType)
387 {
388 case 1:
389 result.contentType = ContentType::OBJECTS;
390 break;
391 case 2:
392 case 4:
393 result.contentType = ContentType::NONE;
394 break;
395 case 3:
396 result.contentType = ContentType::TEXT;
397 break;
398 case 5:
399 result.contentType = ContentType::PICTURE;
400 break;
401 default:
402 QXP_DEBUG_MSG(("Unknown content type %u\n", contentType));
403 throw ParseError();
404 }
405
406 const uint8_t shapeType = readU8(stream);
407 switch (shapeType)
408 {
409 case 0:
410 result.shapeType = ShapeType::LINE;
411 break;
412 case 1:
413 result.shapeType = ShapeType::ORTHOGONAL_LINE;
414 break;
415 case 2:
416 result.shapeType = ShapeType::RECTANGLE;
417 break;
418 case 3:
419 result.shapeType = ShapeType::CORNERED_RECTANGLE;
420 break;
421 case 4:
422 result.shapeType = ShapeType::OVAL;
423 break;
424 case 5:
425 result.shapeType = ShapeType::POLYGON;
426 break;
427 default:
428 QXP_DEBUG_MSG(("Unknown shape type %u\n", shapeType));
429 throw ParseError();
430 }
431 }
432
433 switch (result.shapeType)
434 {
435 case ShapeType::RECTANGLE:
436 case ShapeType::CORNERED_RECTANGLE:
437 result.boxType = BoxType::RECTANGLE;
438 break;
439 case ShapeType::OVAL:
440 result.boxType = BoxType::OVAL;
441 break;
442 case ShapeType::POLYGON:
443 result.boxType = BoxType::POLYGON;
444 break;
445 default:
446 break;
447 }
448
449 if (result.shapeType == ShapeType::CORNERED_RECTANGLE)
450 {
451 if (concave)
452 result.cornerType = CornerType::CONCAVE;
453 else if (beveled)
454 result.cornerType = CornerType::BEVELED;
455 else
456 result.cornerType = CornerType::ROUNDED;
457 }
458
459 if (m_header->version() == QXP_33)
460 result.cornerRadius = readFraction(stream, be);
461
462 if (result.gradientId != 0)
463 {
464 result.fill = readGradient(stream, color);
465 }
466
467 result.boundingBox = readObjectBBox(stream);
468
469 return result;
470 }
471
472 void QXP33Parser::readObjectFlags(const std::shared_ptr<librevenge::RVNGInputStream> &stream, bool &noColor, bool &noRunaround)
473 {
474 const uint8_t flags = QXPParser::readObjectFlags(stream, noColor);
475 if (be)
476 {
477 noRunaround = flags & 0x02;
478 }
479 else
480 {
481 noRunaround = flags & 0x20;
482 }
483 }
484
485 void QXP33Parser::parseLine(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP33Parser::ObjectHeader &header, QXPCollector &collector)
486 {
487 auto line = createLine<Line>(header);
488
489 line->style.width = readFraction(stream, be);
490
491 const unsigned styleIndex = readU8(stream);
492 const bool isStripe = (styleIndex >> 7) == 1;
493 if (!isStripe)
494 {
495 line->style.lineStyle = getLineStyle(styleIndex);
496 }
497
498 const uint8_t arrowType = readU8(stream);
499 setArrow(arrowType, line->style);
500
501 collector.collectLine(line);
502 }
503
504 void QXP33Parser::parseTextBox(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP33Parser::ObjectHeader &header, QXPCollector &collector)
505 {
506 auto textbox = createBox<TextBox>(header);
507 textbox->linkSettings.linkId = header.linkId;
508
509 textbox->frame = readFrame(stream);
510 skip(stream, 4);
511 const unsigned runaroundId = readU32(stream, be);
512
513 textbox->linkSettings.offsetIntoText = readU32(stream, be);
514 skip(stream, 4);
515 textbox->settings.gutterWidth = readFraction(stream, be);
516 textbox->settings.inset.top = readFraction(stream, be);
517 textbox->settings.inset.left = readFraction(stream, be);
518 textbox->settings.inset.right = readFraction(stream, be);
519 textbox->settings.inset.bottom = readFraction(stream, be);
520 textbox->settings.rotation = readFraction(stream, be);
521 textbox->settings.skew = readFraction(stream, be);
522 textbox->settings.columnsCount = readU8(stream);
523 textbox->settings.verticalAlignment = readVertAlign(stream);
524 skip(stream, 8);
525 textbox->linkSettings.nextLinkedIndex = readU32(stream, be);
526 skip(stream, 8);
527
528 if (header.shapeType == ShapeType::POLYGON)
529 {
530 textbox->customPoints = readPolygonData(stream);
531 }
532
533 if (header.contentIndex == 0 || textbox->linkSettings.offsetIntoText == 0)
534 {
535 skip(stream, 4);
536 const unsigned fileInfoId = readU32(stream, be);
537 skip(stream, 4);
538 if (fileInfoId != 0)
539 {
540 skipFileInfo(stream);
541 }
542 if (header.contentIndex == 0)
543 {
544 skip(stream, 12);
545 }
546 }
547
548 if (runaroundId != 0)
549 {
550 const unsigned length = readU32(stream, be);
551 skip(stream, length);
552 }
553
554 if (header.contentIndex == 0)
555 {
556 collector.collectBox(textbox);
557 }
558 else
559 {
560 if (textbox->linkSettings.offsetIntoText > 0)
561 {
562 textbox->linkSettings.linkedIndex = header.contentIndex;
563 }
564 else
565 {
566 textbox->text = parseText(header.contentIndex, header.linkId, collector);
567 }
568
569 collector.collectTextBox(textbox);
570 }
571 }
572
573 void QXP33Parser::parsePictureBox(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP33Parser::ObjectHeader &objHeader, QXPCollector &collector)
574 {
575 const auto frame = readFrame(stream);
576
577 skip(stream, 4);
578
579 unsigned runaroundId = 0;
580 unsigned clipId = 0;
581
582 QXP33Parser::ObjectHeader header(objHeader);
583
584 if (m_header->version() == QXP_33)
585 {
586 runaroundId = readU32(stream, be);
587 skip(stream, 2);
588 clipId = readU32(stream, be);
589 skip(stream, 14);
590 }
591 else
592 {
593 skip(stream, 4);
594 if (header.shapeType == ShapeType::CORNERED_RECTANGLE)
595 {
596 header.cornerRadius = readFraction(stream, be);
597 const unsigned cornerType = readU8(stream, be);
598 switch (cornerType)
599 {
600 case 0:
601 header.cornerType = CornerType::BEVELED;
602 break;
603 default:
604 QXP_DEBUG_MSG(("Unknown corner type %u\n", cornerType));
605 QXP_FALLTHROUGH; // pick a default
606 case 1:
607 header.cornerType = CornerType::ROUNDED;
608 break;
609 case 2:
610 header.cornerType = CornerType::CONCAVE;
611 break;
612 }
613 }
614 else if (header.shapeType == ShapeType::POLYGON)
615 {
616 skip(stream, 5);
617 }
618 }
619
620 auto picturebox = createBox<PictureBox>(header);
621 picturebox->frame = frame;
622
623 picturebox->pictureRotation = readFraction(stream, be);
624 picturebox->pictureSkew = readFraction(stream, be);
625 picturebox->offsetLeft = readFraction(stream, be);
626 picturebox->offsetTop = readFraction(stream, be);
627 picturebox->scaleHor = readFraction(stream, be);
628 picturebox->scaleVert = readFraction(stream, be);
629 skip(stream, 30);
630
631 if (header.shapeType == ShapeType::POLYGON)
632 {
633 picturebox->customPoints = readPolygonData(stream);
634 }
635
636 if (runaroundId != 0)
637 {
638 const unsigned rlength = readU32(stream, be);
639 skip(stream, rlength);
640
641 if (clipId != 0)
642 {
643 const unsigned clength = readU32(stream, be);
644 skip(stream, clength);
645 }
646 }
647
648 collector.collectBox(picturebox);
649 }
650
651 void QXP33Parser::parseEmptyBox(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP33Parser::ObjectHeader &header, QXPCollector &collector)
652 {
653 auto box = createBox<Box>(header);
654
655 box->frame = readFrame(stream);
656 skip(stream, 4);
657 const unsigned runaroundId = readU32(stream, be);
658
659 skip(stream, 74);
660
661 if (header.shapeType == ShapeType::POLYGON)
662 {
663 box->customPoints = readPolygonData(stream);
664 }
665
666 if (runaroundId != 0)
667 {
668 const unsigned length = readU32(stream, be);
669 skip(stream, length);
670 }
671
672 collector.collectBox(box);
673 }
674
675 void QXP33Parser::parseGroup(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP33Parser::ObjectHeader &header, QXPCollector &collector, const Page &page, const unsigned index)
676 {
677 skip(stream, 10);
678 const unsigned count = readU16(stream, be);
679 if (count > page.objectsCount - 1)
680 {
681 QXP_DEBUG_MSG(("Invalid group elements count %u\n", count));
682 throw ParseError();
683 }
684 skip(stream, 6);
685
686 auto group = make_shared<Group>();
687 group->boundingBox = header.boundingBox;
688
689 readGroupElements(stream, count, page.objectsCount, index, group->objectsIndexes);
690
691 collector.collectGroup(group);
692 }
693
694 Frame QXP33Parser::readFrame(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
695 {
696 Frame frame;
697 frame.width = readFraction(stream, be);
698 const double shade = readFraction(stream, be);
699 const unsigned colorId = readU8(stream);
700 frame.color = getColor(colorId).applyShade(shade);
701 skip(stream, 1);
702 return frame;
703 }
704
705 std::vector<Point> QXP33Parser::readPolygonData(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
706 {
707 const unsigned length = readU32(stream, be);
708 if (length < 18 || length > getRemainingLength(stream))
709 {
710 QXP_DEBUG_MSG(("Invalid polygon data length %u\n", length));
711 throw ParseError();
712 }
713 skip(stream, 18);
714
715 const unsigned count = (length - 18) / 8;
716 std::vector<Point> points;
717 points.resize(count);
718 for (auto &p : points)
719 {
720 p = readYX(stream);
721 }
722
723 return points;
724 }
725
726 std::string QXP33Parser::readName(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
727 {
728 const long start = stream->tell();
729 auto name = readPlatformString(stream, be);
730 if ((stream->tell() - start) % 2 == 1)
731 {
732 skip(stream, 1);
733 }
734 return name;
735 }
736
737 }
738
739 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */