Debian Codegraph @

src/lib/QXP4Parser.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 "QXP4Parser.h"
11
12 #include <librevenge-stream/librevenge-stream.h>
13 #include <memory>
14
15 #include "QXP4Deobfuscator.h"
16 #include "QXP4Header.h"
17 #include "QXPCollector.h"
18 #include "QXPMemoryStream.h"
19
20 namespace libqxp
21 {
22
23 using std::make_shared;
24 using std::shared_ptr;
25
26 namespace
27 {
28
29 template<typename T>
30 shared_ptr<T> createBox(const QXP4Parser::ObjectHeader &header)
31 {
32 auto box = make_shared<T>();
33 box->cornerType = header.cornerType;
34 box->boxType = header.boxType;
35 box->rotation = header.rotation;
36 box->fill = header.fillColor;
37 return box;
38 }
39
40 template<typename T>
41 shared_ptr<T> createLine(const QXP4Parser::ObjectHeader &header)
42 {
43 auto line = make_shared<T>();
44 line->rotation = header.rotation;
45 return line;
46 }
47
48 }
49
50 QXP4Parser::QXP4Parser(const std::shared_ptr<librevenge::RVNGInputStream> &input, librevenge::RVNGDrawingInterface *painter, const std::shared_ptr<QXP4Header> &header)
51 : QXPParser(input, painter, header)
52 , m_header(header)
53 , m_paragraphTabStops()
54 {
55 }
56
57 bool QXP4Parser::parseDocument(const std::shared_ptr<librevenge::RVNGInputStream> &docStream, QXPCollector &collector)
58 {
59 collector.collectDocumentProperties(m_header->documentProperties());
60
61 for (int i = 0; i < 5; ++i)
62 {
63 skipRecord(docStream);
64 }
65
66 parseFonts(docStream);
67
68 skipRecord(docStream);
69
70 parseColors(docStream);
71
72 // don't need stylesheets, everything is included in the current style
73 skipParagraphStylesheets(docStream);
74 skipRecord(docStream);
75
76 parseHJs(docStream);
77
78 parseLineStyles(docStream);
79
80 skipRecord(docStream);
81
82 skipTemplates(docStream);
83
84 parseCharFormats(docStream);
85
86 parseTabStops(docStream);
87
88 parseParagraphFormats(docStream);
89
90 skipRecord(docStream);
91
92 return true;
93 }
94
95 bool QXP4Parser::parsePages(const std::shared_ptr<librevenge::RVNGInputStream> &stream, QXPCollector &collector)
96 {
97 QXP4Deobfuscator deobfuscate(m_header->seed(), m_header->increment());
98 QXPDummyCollector dummyCollector;
99
100 for (unsigned ind = 0; ind < m_header->pagesCount() + m_header->masterPagesCount(); ++ind)
101 {
102 // don't output master pages, everything is included in normal pages
103 QXPCollector &coll = ind < m_header->masterPagesCount() ? dummyCollector : collector;
104
105 auto page = parsePage(stream, deobfuscate);
106 coll.startPage(page);
107 deobfuscate.nextRev();
108
109 for (unsigned i = 0; i < page.objectsCount; ++i)
110 {
111 parseObject(stream, deobfuscate, coll, page, i);
112 }
113
114 m_groupObjects.clear();
115 coll.endPage();
116 }
117
118 return true;
119 }
120
121 void QXP4Parser::parseColors(const std::shared_ptr<librevenge::RVNGInputStream> &docStream)
122 {
123 unsigned length = readU32(docStream, be);
124 if (length > getRemainingLength(docStream))
125 {
126 QXP_DEBUG_MSG(("Invalid colors length %u\n", length));
127 throw ParseError();
128 }
129
130 auto stream = make_shared<QXPMemoryStream>(readNBytes(docStream, length), length);
131
132 try
133 {
134 skip(stream, 14);
135 const unsigned blocksCount = readU16(stream, be);
136 if (blocksCount == 0 || blocksCount * 4 > length)
137 {
138 QXP_DEBUG_MSG(("Invalid number of blocks %u\n", blocksCount));
139 return;
140 }
141 skip(stream, 20);
142 std::vector<ColorBlockSpec> blocks;
143 blocks.resize(blocksCount + 1); // first dummy block
144 for (unsigned i = 1; i <= blocksCount; ++i)
145 {
146 blocks[i] = parseColorBlockSpec(stream);
147 }
148
149 for (unsigned i = 2; i <= blocksCount; ++i)
150 {
151 seek(stream, blocks[i].offset);
152 if (readU16(stream, be) + readU16(stream, be) == 6) // 04 02
153 {
154 parseColor(stream, blocks);
155 }
156 }
157 }
158 catch (...)
159 {
160 QXP_DEBUG_MSG(("Failed to parse colors, offset %ld\n", stream->tell()));
161 }
162 }
163
164 QXP4Parser::ColorBlockSpec QXP4Parser::parseColorBlockSpec(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
165 {
166 const uint32_t info = readU32(stream, be);
167 ColorBlockSpec spec;
168 spec.offset = info & 0xFFFFFFF;
169 spec.padding = (info >> 28) & 0x7;
170 return spec;
171 }
172
173 void QXP4Parser::parseColor(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const std::vector<ColorBlockSpec> &blocks)
174 {
175 skip(stream, 30);
176 unsigned id = readU16(stream, be);
177
178 skip(stream, 70);
179 unsigned rgbBlockInd = readU16(stream, be);
180
181 if (rgbBlockInd != 0)
182 {
183 if (rgbBlockInd >= blocks.size())
184 {
185 QXP_DEBUG_MSG(("RGB block %u not found\n", rgbBlockInd));
186 return;
187 }
188 seek(stream, blocks[rgbBlockInd].offset + 16);
189 Color color;
190 color.red = readColorComp(stream);
191 color.green = readColorComp(stream);
192 color.blue = readColorComp(stream);
193 m_colors[id] = color;
194 }
195 }
196
197 void QXP4Parser::skipParagraphStylesheets(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
198 {
199 const unsigned length = readU32(stream, be);
200 if (length > getRemainingLength(stream))
201 {
202 QXP_DEBUG_MSG(("Invalid paragraph stylesheets length %u\n", length));
203 throw ParseError();
204 }
205 const long end = stream->tell() + length;
206
207 unsigned tabRecordsCount = 0;
208 while (stream->tell() < end)
209 {
210 skip(stream, 90);
211 const unsigned tabsCount = readU16(stream, be);
212 if (tabsCount > 0)
213 {
214 tabRecordsCount++;
215 }
216 skip(stream, 152);
217 }
218
219 seek(stream, end);
220
221 for (unsigned i = 0; i < tabRecordsCount; ++i)
222 {
223 skipRecord(stream);
224 }
225 }
226
227 CharFormat QXP4Parser::parseCharFormat(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
228 {
229 skip(stream, 8);
230
231 CharFormat result;
232 parseCommonCharFormatProps(stream, result);
233
234 skip(stream, 4);
235
236 const unsigned colorId = readU16(stream, be);
237
238 skip(stream, 2);
239
240 const double shade = readFraction(stream, be);
241 result.color = getColor(colorId).applyShade(shade);
242
243 skip(stream, 8);
244 result.baselineShift = readFraction(stream, be);
245
246 result.isControlChars = readU8(stream) != 0;
247
248 skip(stream, 23);
249
250 return result;
251 }
252
253 void QXP4Parser::parseLineStyles(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
254 {
255 parseCollection(stream, [=]()
256 {
257 const unsigned long start = stream->tell();
258 const unsigned long end = start + 252;
259
260 try
261 {
262 skip(stream, 168);
263
264 const unsigned id = readU16(stream, be);
265 auto &result = (m_lineStyles[id] = LineStyle());
266
267 result.isStripe = readU8(stream) == 1;
268 skip(stream, 1);
269 const unsigned segmentsCount = readU16(stream, be);
270 if (segmentsCount > 42)
271 {
272 QXP_DEBUG_MSG(("Invalid line style segments count %u\n", segmentsCount));
273 throw ParseError();
274 }
275 result.isProportional = readU8(stream) == 1;
276 skip(stream, 69);
277 result.patternLength = readFraction(stream, be);
278
279 const unsigned miter = readU16(stream, be);
280 switch (miter)
281 {
282 default:
283 QXP_DEBUG_MSG(("Unknown line join type %u\n", miter));
284 QXP_FALLTHROUGH; // pick a default
285 case 0:
286 result.joinType = LineJoinType::MITER;
287 break;
288 case 1:
289 result.joinType = LineJoinType::ROUND;
290 break;
291 case 2:
292 result.joinType = LineJoinType::BEVEL;
293 break;
294 }
295
296 const unsigned endcap = readU16(stream, be);
297 switch (endcap)
298 {
299 default:
300 QXP_DEBUG_MSG(("Unknown line cap type %u\n", endcap));
301 QXP_FALLTHROUGH; // pick a default
302 case 0:
303 result.endcapType = LineCapType::BUTT;
304 break;
305 case 1:
306 result.endcapType = LineCapType::ROUND;
307 break;
308 case 2:
309 result.endcapType = LineCapType::RECT;
310 break;
311 case 3:
312 result.endcapType = LineCapType::STRETCH;
313 break;
314 }
315
316 seek(stream, start);
317 result.segmentLengths.resize(segmentsCount);
318 for (double &segmentLength : result.segmentLengths)
319 {
320 segmentLength = readFraction(stream, be);
321 }
322 }
323 catch (...)
324 {
325 QXP_DEBUG_MSG(("Failed to parse line style, offset %ld\n", stream->tell()));
326 }
327
328 seek(stream, end);
329 });
330 }
331
332 void QXP4Parser::skipTemplates(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
333 {
334 const unsigned length = readU32(stream, be);
335 if (length > getRemainingLength(stream))
336 {
337 QXP_DEBUG_MSG(("Invalid templates index length %u\n", length));
338 throw ParseError();
339 }
340
341 const unsigned count = readU32(stream, be);
342
343 skip(stream, length - 4);
344
345 if (count > getRemainingLength(stream) / 4)
346 {
347 QXP_DEBUG_MSG(("Invalid template count %u\n", count));
348 throw ParseError();
349 }
350
351 for (unsigned i = 0; i < count; ++i)
352 {
353 skipRecord(stream);
354 }
355 }
356
357 void QXP4Parser::parseTabStops(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
358 {
359 const unsigned specLength = readU32(stream, be);
360 if (specLength > getRemainingLength(stream))
361 {
362 QXP_DEBUG_MSG(("Invalid tab stop spec length %u\n", specLength));
363 throw ParseError();
364 }
365
366 std::vector<unsigned> tabStopsCounts;
367 tabStopsCounts.resize(specLength / 8);
368 for (auto countIt = tabStopsCounts.rbegin(); countIt != tabStopsCounts.rend(); ++countIt)
369 {
370 skip(stream, 2);
371 const unsigned count = readU16(stream, be);
372 if (count > getRemainingLength(stream) / 8)
373 {
374 QXP_DEBUG_MSG(("Invalid tab stop count %u\n", count));
375 throw ParseError();
376 }
377 *countIt = count;
378 skip(stream, 4);
379 }
380
381 m_paragraphTabStops.resize(tabStopsCounts.size());
382 unsigned i = 0;
383 for (auto it = m_paragraphTabStops.rbegin(); it != m_paragraphTabStops.rend(); ++it)
384 {
385 skip(stream, 4);
386 const unsigned tabStopsCount = tabStopsCounts[i++];
387 it->resize(tabStopsCount);
388 for (auto &tabStop : *it)
389 {
390 tabStop = parseTabStop(stream);
391 }
392 }
393 }
394
395 ParagraphFormat QXP4Parser::parseParagraphFormat(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
396 {
397 ParagraphFormat result;
398
399 skip(stream, 8);
400
401 bool hasRuleAbove, hasRuleBelow;
402 readParagraphFlags(stream, result.incrementalLeading, hasRuleAbove, hasRuleBelow);
403
404 skip(stream, 2);
405 result.alignment = readHorAlign(stream);
406
407 skip(stream, 4);
408 const unsigned hj = readU16(stream, be);
409 if (hj < m_hjs.size())
410 result.hj = m_hjs[hj];
411 skip(stream, 2);
412
413 result.margin.left = readFraction(stream, be);
414 result.firstLineIndent = readFraction(stream, be);
415 result.margin.right = readFraction(stream, be);
416 result.leading = readFraction(stream, be);
417 result.margin.top = readFraction(stream, be);
418 result.margin.bottom = readFraction(stream, be);
419
420 skip(stream, 4);
421
422 auto ruleAbove = readParagraphRule(stream);
423 auto ruleBelow = readParagraphRule(stream);
424 if (hasRuleAbove)
425 result.ruleAbove = ruleAbove;
426 if (hasRuleBelow)
427 result.ruleBelow = ruleBelow;
428
429 const unsigned tabStopsIndex = readU16(stream, be);
430 if (tabStopsIndex != 0xffff)
431 {
432 if (tabStopsIndex >= m_paragraphTabStops.size())
433 {
434 QXP_DEBUG_MSG(("Tab stop %u not found\n", tabStopsIndex));
435 }
436 else
437 {
438 result.tabStops = m_paragraphTabStops[tabStopsIndex];
439 }
440 }
441
442 skip(stream, 2);
443
444 return result;
445 }
446
447 std::shared_ptr<HJ> QXP4Parser::parseHJ(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
448 {
449 auto hj = make_shared<HJ>();
450
451 skip(stream, 4);
452 parseHJProps(stream, *hj);
453 skip(stream, 64);
454
455 return hj;
456 }
457
458 Page QXP4Parser::parsePage(const std::shared_ptr<librevenge::RVNGInputStream> &stream, QXP4Deobfuscator &deobfuscate)
459 {
460 Page page;
461 page.pageSettings = parsePageSettings(stream);
462 page.objectsCount = deobfuscate(uint16_t(readU32(stream, be) & 0xffff));
463 return page;
464 }
465
466 void QXP4Parser::parseObject(const std::shared_ptr<librevenge::RVNGInputStream> &stream, QXP4Deobfuscator &deobfuscate, QXPCollector &collector, const Page &page, const unsigned index)
467 {
468 const auto header = parseObjectHeader(stream, deobfuscate);
469
470 switch (header.contentType)
471 {
472 case ContentType::NONE:
473 switch (header.shapeType)
474 {
475 case ShapeType::LINE:
476 case ShapeType::ORTHOGONAL_LINE:
477 parseLine(stream, header, collector);
478 break;
479 case ShapeType::BEZIER_LINE:
480 parseBezierLine(stream, header, collector);
481 break;
482 case ShapeType::BEZIER_BOX:
483 parseBezierEmptyBox(stream, header, collector);
484 break;
485 case ShapeType::RECTANGLE:
486 case ShapeType::ROUNDED_RECTANGLE:
487 case ShapeType::CONCAVE_RECTANGLE:
488 case ShapeType::BEVELED_RECTANGLE:
489 case ShapeType::OVAL:
490 parseEmptyBox(stream, header, collector);
491 break;
492 default:
493 QXP_DEBUG_MSG(("Unsupported shape\n"));
494 throw GenericException();
495 }
496 break;
497 case ContentType::PICTURE:
498 switch (header.shapeType)
499 {
500 case ShapeType::BEZIER_BOX:
501 parseBezierPictureBox(stream, header, collector);
502 break;
503 case ShapeType::RECTANGLE:
504 case ShapeType::ROUNDED_RECTANGLE:
505 case ShapeType::CONCAVE_RECTANGLE:
506 case ShapeType::BEVELED_RECTANGLE:
507 case ShapeType::OVAL:
508 parsePictureBox(stream, header, collector);
509 break;
510 default:
511 QXP_DEBUG_MSG(("Unsupported shape\n"));
512 throw GenericException();
513 }
514 break;
515 case ContentType::TEXT:
516 switch (header.shapeType)
517 {
518 case ShapeType::LINE:
519 case ShapeType::ORTHOGONAL_LINE:
520 parseLineText(stream, header, collector);
521 break;
522 case ShapeType::BEZIER_LINE:
523 parseBezierText(stream, header, collector);
524 break;
525 case ShapeType::BEZIER_BOX:
526 parseBezierTextBox(stream, header, collector);
527 break;
528 case ShapeType::RECTANGLE:
529 case ShapeType::ROUNDED_RECTANGLE:
530 case ShapeType::CONCAVE_RECTANGLE:
531 case ShapeType::BEVELED_RECTANGLE:
532 case ShapeType::OVAL:
533 parseTextBox(stream, header, collector);
534 break;
535 default:
536 QXP_DEBUG_MSG(("Unsupported shape\n"));
537 throw GenericException();
538 }
539 break;
540 case ContentType::OBJECTS:
541 parseGroup(stream, header, collector, page, index);
542 break;
543 default:
544 QXP_DEBUG_MSG(("Unsupported content\n"));
545 throw GenericException();
546 }
547
548 deobfuscate.next(uint16_t(header.contentIndex));
549 }
550
551 QXP4Parser::ObjectHeader QXP4Parser::parseObjectHeader(const std::shared_ptr<librevenge::RVNGInputStream> &stream, QXP4Deobfuscator &deobfuscate)
552 {
553 ObjectHeader result;
554
555 bool noColor;
556 readObjectFlags(stream, noColor);
557
558 skip(stream, 1);
559
560 const unsigned colorId = readU16(stream, be);
561 const double shade = readFraction(stream, be);
562 result.color = getColor(colorId).applyShade(shade);
563 if (!noColor)
564 {
565 result.fillColor = result.color;
566 }
567
568 skip(stream, 4);
569
570 const uint16_t contentIndexObf = uint16_t(readU32(stream, be) & 0xffff);
571
572 result.rotation = readFraction(stream, be);
573 result.skew = readFraction(stream, be);
574
575 result.linkId = readU32(stream, be);
576 result.oleId = readU32(stream, be);
577 result.gradientId = readU32(stream, be);
578
579 skip(stream, 4);
580
581 const uint8_t boxFlag1 = readU8(stream);
582 const uint8_t boxFlag2 = readU8(stream);
583 if (be)
584 {
585 result.hflip = boxFlag1 & 0x80;
586 result.vflip = boxFlag2 & 0x80;
587 }
588 else
589 {
590 result.hflip = boxFlag1 & 0x1;
591 result.vflip = boxFlag2 & 0x1;
592 }
593
594 const uint8_t contentType = deobfuscate(readU8(stream));
595 deobfuscate.nextShift(contentType);
596
597 result.contentIndex = deobfuscate(contentIndexObf);
598
599 const uint8_t shapeType = deobfuscate(readU8(stream));
600
601 switch (contentType)
602 {
603 case 0:
604 result.contentType = ContentType::NONE;
605 break;
606 case 2:
607 result.contentType = ContentType::OBJECTS;
608 break;
609 case 3:
610 result.contentType = ContentType::TEXT;
611 break;
612 case 4:
613 result.contentType = ContentType::PICTURE;
614 break;
615 default:
616 QXP_DEBUG_MSG(("Unknown content type %u\n", contentType));
617 throw ParseError();
618 }
619
620 switch (shapeType)
621 {
622 case 1:
623 result.shapeType = ShapeType::LINE;
624 break;
625 case 2:
626 result.shapeType = ShapeType::ORTHOGONAL_LINE;
627 break;
628 case 4:
629 result.shapeType = ShapeType::BEZIER_LINE;
630 break;
631 case 5:
632 result.shapeType = ShapeType::RECTANGLE;
633 result.boxType = BoxType::RECTANGLE;
634 break;
635 case 6:
636 result.shapeType = ShapeType::ROUNDED_RECTANGLE;
637 result.boxType = BoxType::RECTANGLE;
638 result.cornerType = CornerType::ROUNDED;
639 break;
640 case 7:
641 result.shapeType = ShapeType::CONCAVE_RECTANGLE;
642 result.boxType = BoxType::RECTANGLE;
643 result.cornerType = CornerType::CONCAVE;
644 break;
645 case 8:
646 result.shapeType = ShapeType::BEVELED_RECTANGLE;
647 result.boxType = BoxType::RECTANGLE;
648 result.cornerType = CornerType::BEVELED;
649 break;
650 case 9:
651 result.shapeType = ShapeType::OVAL;
652 result.boxType = BoxType::OVAL;
653 break;
654 case 11:
655 result.shapeType = ShapeType::BEZIER_BOX;
656 result.boxType = BoxType::BEZIER;
657 break;
658 default:
659 QXP_DEBUG_MSG(("Unknown shape type %u\n", shapeType));
660 throw ParseError();
661 }
662
663 return result;
664 }
665
666 void QXP4Parser::parseLine(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &header, QXPCollector &collector)
667 {
668 auto line = createLine<Line>(header);
669
670 line->style = readFrame(stream);
671 skip(stream, 4);
672 line->runaround = readRunaround(stream);
673 skip(stream, 4);
674
675 line->boundingBox = readObjectBBox(stream);
676
677 skip(stream, 24);
678
679 collector.collectLine(line);
680 }
681
682 void QXP4Parser::parseBezierLine(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &header, QXPCollector &collector)
683 {
684 auto line = createLine<Line>(header);
685
686 line->style = readFrame(stream);
687 skip(stream, 4);
688 line->runaround = readRunaround(stream);
689 skip(stream, 44);
690
691 readBezierData(stream, line->curveComponents, line->boundingBox);
692
693 collector.collectLine(line);
694 }
695
696 void QXP4Parser::parseBezierEmptyBox(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &header, QXPCollector &collector)
697 {
698 auto box = createBox<Box>(header);
699
700 box->frame = readFrame(stream);
701 skip(stream, 4);
702 box->runaround = readRunaround(stream);
703 skip(stream, 44);
704
705 if (header.gradientId != 0)
706 {
707 box->fill = readGradient(stream, header.color);
708 }
709
710 readBezierData(stream, box->curveComponents, box->boundingBox);
711
712 collector.collectBox(box);
713 }
714
715 void QXP4Parser::parseEmptyBox(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &header, QXPCollector &collector)
716 {
717 auto box = createBox<Box>(header);
718
719 box->frame = readFrame(stream);
720 skip(stream, 4);
721 box->runaround = readRunaround(stream);
722 skip(stream, 4);
723
724 box->boundingBox = readObjectBBox(stream);
725
726 box->cornerRadius = readFraction(stream, be);
727
728 skip(stream, 20);
729
730 if (header.gradientId != 0)
731 {
732 box->fill = readGradient(stream, header.color);
733 }
734
735 collector.collectBox(box);
736 }
737
738 void QXP4Parser::parseBezierPictureBox(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &header, QXPCollector &collector)
739 {
740 auto box = createBox<PictureBox>(header);
741
742 box->frame = readFrame(stream);
743 skip(stream, 4);
744 box->runaround = readRunaround(stream);
745 skip(stream, 40);
746
747 readOleObject(stream);
748
749 if (header.gradientId != 0)
750 {
751 box->fill = readGradient(stream, header.color);
752 }
753
754 readPictureSettings(stream, box);
755
756 skip(stream, 76);
757
758 if (header.contentIndex != 0 && header.oleId == 0)
759 {
760 readImageData(stream);
761 }
762
763 readBezierData(stream, box->curveComponents, box->boundingBox);
764
765 collector.collectBox(box);
766 }
767
768 void QXP4Parser::parsePictureBox(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &header, QXPCollector &collector)
769 {
770 auto picturebox = createBox<PictureBox>(header);
771
772 picturebox->frame = readFrame(stream);
773 skip(stream, 4);
774 picturebox->runaround = readRunaround(stream);
775 skip(stream, 4);
776
777 picturebox->boundingBox = readObjectBBox(stream);
778
779 picturebox->cornerRadius = readFraction(stream, be);
780
781 skip(stream, 16);
782
783 readOleObject(stream);
784
785 if (header.gradientId != 0)
786 {
787 picturebox->fill = readGradient(stream, header.color);
788 }
789
790 readPictureSettings(stream, picturebox);
791
792 skip(stream, 76);
793
794 if (header.contentIndex != 0 && header.oleId == 0)
795 {
796 readImageData(stream);
797 }
798
799 collector.collectBox(picturebox);
800 }
801
802 void QXP4Parser::parseLineText(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &header, QXPCollector &collector)
803 {
804 auto textpath = createLine<TextPath>(header);
805 textpath->linkSettings.linkId = header.linkId;
806
807 textpath->style = readFrame(stream);
808 skip(stream, 4);
809 textpath->runaround = readRunaround(stream);
810 skip(stream, 4);
811
812 textpath->boundingBox = readObjectBBox(stream);
813
814 skip(stream, 24);
815
816 textpath->linkSettings.offsetIntoText = readU32(stream, be);
817 skip(stream, 44);
818 readLinkedTextSettings(stream, textpath->linkSettings);
819 skip(stream, 4);
820 readTextPathSettings(stream, textpath->settings);
821 skip(stream, 4);
822
823 skipTextObjectEnd(stream, header, textpath->linkSettings);
824
825 if (header.contentIndex == 0)
826 {
827 collector.collectLine(textpath);
828 }
829 else
830 {
831 if (textpath->linkSettings.offsetIntoText > 0)
832 {
833 textpath->linkSettings.linkedIndex = header.contentIndex;
834 }
835 else
836 {
837 textpath->text = parseText(header.contentIndex, header.linkId, collector);
838 }
839
840 collector.collectTextPath(textpath);
841 }
842 }
843
844 void QXP4Parser::parseBezierText(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &header, QXPCollector &collector)
845 {
846 auto textpath = createLine<TextPath>(header);
847 textpath->linkSettings.linkId = header.linkId;
848
849 textpath->style = readFrame(stream);
850 skip(stream, 4);
851 textpath->runaround = readRunaround(stream);
852 skip(stream, 44);
853
854 textpath->linkSettings.offsetIntoText = readU32(stream, be);
855 skip(stream, 44);
856 readLinkedTextSettings(stream, textpath->linkSettings);
857 skip(stream, 4);
858 readTextPathSettings(stream, textpath->settings);
859 skip(stream, 4);
860
861 readBezierData(stream, textpath->curveComponents, textpath->boundingBox);
862
863 skipTextObjectEnd(stream, header, textpath->linkSettings);
864
865 if (header.contentIndex == 0)
866 {
867 collector.collectLine(textpath);
868 }
869 else
870 {
871 if (textpath->linkSettings.offsetIntoText > 0)
872 {
873 textpath->linkSettings.linkedIndex = header.contentIndex;
874 }
875 else
876 {
877 textpath->text = parseText(header.contentIndex, header.linkId, collector);
878 }
879
880 collector.collectTextPath(textpath);
881 }
882 }
883
884 void QXP4Parser::parseBezierTextBox(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &header, QXPCollector &collector)
885 {
886 auto textbox = createBox<TextBox>(header);
887 textbox->linkSettings.linkId = header.linkId;
888
889 textbox->frame = readFrame(stream);
890 skip(stream, 4);
891 textbox->runaround = readRunaround(stream);
892 skip(stream, 44);
893
894 if (header.gradientId != 0)
895 {
896 textbox->fill = readGradient(stream, header.color);
897 }
898
899 textbox->linkSettings.offsetIntoText = readU32(stream, be);
900 skip(stream, 2);
901 readTextSettings(stream, textbox->settings);
902 readLinkedTextSettings(stream, textbox->linkSettings);
903 skip(stream, 12);
904
905 readBezierData(stream, textbox->curveComponents, textbox->boundingBox);
906
907 skipTextObjectEnd(stream, header, textbox->linkSettings);
908
909 if (header.contentIndex == 0)
910 {
911 collector.collectBox(textbox);
912 }
913 else
914 {
915 if (textbox->linkSettings.offsetIntoText > 0)
916 {
917 textbox->linkSettings.linkedIndex = header.contentIndex;
918 }
919 else
920 {
921 textbox->text = parseText(header.contentIndex, header.linkId, collector);
922 }
923
924 collector.collectTextBox(textbox);
925 }
926 }
927
928 void QXP4Parser::parseTextBox(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &header, QXPCollector &collector)
929 {
930 auto textbox = createBox<TextBox>(header);
931 textbox->linkSettings.linkId = header.linkId;
932
933 textbox->frame = readFrame(stream);
934 skip(stream, 4);
935 textbox->runaround = readRunaround(stream);
936 skip(stream, 4);
937
938 textbox->boundingBox = readObjectBBox(stream);
939
940 textbox->cornerRadius = readFraction(stream, be);
941
942 skip(stream, 20);
943
944 if (header.gradientId != 0)
945 {
946 textbox->fill = readGradient(stream, header.color);
947 }
948
949 textbox->linkSettings.offsetIntoText = readU32(stream, be);
950 skip(stream, 2);
951 readTextSettings(stream, textbox->settings);
952 readLinkedTextSettings(stream, textbox->linkSettings);
953 skip(stream, 12);
954
955 skipTextObjectEnd(stream, header, textbox->linkSettings);
956
957 if (header.contentIndex == 0)
958 {
959 collector.collectBox(textbox);
960 }
961 else
962 {
963 if (textbox->linkSettings.offsetIntoText > 0)
964 {
965 textbox->linkSettings.linkedIndex = header.contentIndex;
966 }
967 else
968 {
969 textbox->text = parseText(header.contentIndex, header.linkId, collector);
970 }
971
972 collector.collectTextBox(textbox);
973 }
974 }
975
976 void QXP4Parser::parseGroup(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &, QXPCollector &collector, const Page &page, const unsigned index)
977 {
978 auto group = make_shared<Group>();
979
980 skip(stream, 68);
981
982 group->boundingBox = readObjectBBox(stream);
983
984 skip(stream, 24);
985
986 const unsigned count = readU16(stream, be);
987 if (count > page.objectsCount - 1)
988 {
989 QXP_DEBUG_MSG(("Invalid group elements count %u\n", count));
990 throw ParseError();
991 }
992 skip(stream, 10);
993
994 readGroupElements(stream, count, page.objectsCount, index, group->objectsIndexes);
995
996 collector.collectGroup(group);
997 }
998
999 Frame QXP4Parser::readFrame(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
1000 {
1001 Frame frame;
1002 frame.width = readFraction(stream, be);
1003 const double shade = readFraction(stream, be);
1004 const unsigned colorId = readU16(stream, be);
1005 frame.color = getColor(colorId).applyShade(shade);
1006 const unsigned gapColorId = readU16(stream, be);
1007 const double gapShade = readFraction(stream, be);
1008 frame.gapColor = getColor(gapColorId).applyShade(gapShade);
1009
1010 const uint8_t arrowType = readU8(stream);
1011 setArrow((arrowType >> 2) & 0xf, frame);
1012
1013 const bool isBitmapFrame = readU8(stream) == 1;
1014 const unsigned styleIndex = readU16(stream, be);
1015 if (!isBitmapFrame)
1016 {
1017 frame.lineStyle = getLineStyle(styleIndex);
1018 }
1019
1020 return frame;
1021 }
1022
1023 bool QXP4Parser::readRunaround(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
1024 {
1025 bool result = readU8(stream) == 1;
1026 skip(stream, 39);
1027 return result;
1028 }
1029
1030 void QXP4Parser::readLinkedTextSettings(const std::shared_ptr<librevenge::RVNGInputStream> &stream, LinkedTextSettings &settings)
1031 {
1032 settings.nextLinkedIndex = readU32(stream, be);
1033 skip(stream, 4);
1034 }
1035
1036 void QXP4Parser::readTextSettings(const std::shared_ptr<librevenge::RVNGInputStream> &stream, TextSettings &settings)
1037 {
1038 skip(stream, 2);
1039 settings.gutterWidth = readFraction(stream, be);
1040 settings.inset.top = readFraction(stream, be);
1041 settings.inset.left = readFraction(stream, be);
1042 settings.inset.right = readFraction(stream, be);
1043 settings.inset.bottom = readFraction(stream, be);
1044 settings.rotation = readFraction(stream, be);
1045 settings.skew = readFraction(stream, be);
1046 settings.columnsCount = readU8(stream);
1047 settings.verticalAlignment = readVertAlign(stream);
1048 skip(stream, 10);
1049 }
1050
1051 void QXP4Parser::readTextPathSettings(const std::shared_ptr<librevenge::RVNGInputStream> &stream, TextPathSettings &settings)
1052 {
1053 settings.skew = readU8(stream) == 1;
1054 settings.rotate = readU8(stream) == 1;
1055 const uint8_t align = readU8(stream);
1056 switch (align)
1057 {
1058 default:
1059 QXP_DEBUG_MSG(("Unknown text path align %u\n", align));
1060 QXP_FALLTHROUGH; // pick a default
1061 case 2:
1062 settings.alignment = TextPathAlignment::BASELINE;
1063 break;
1064 case 0:
1065 settings.alignment = TextPathAlignment::ASCENT;
1066 break;
1067 case 1:
1068 settings.alignment = TextPathAlignment::CENTER;
1069 break;
1070 case 3:
1071 settings.alignment = TextPathAlignment::DESCENT;
1072 break;
1073 }
1074 const uint8_t lineAlign = readU8(stream);
1075 switch (lineAlign)
1076 {
1077 default:
1078 QXP_DEBUG_MSG(("Unknown text path line align %u\n", lineAlign));
1079 QXP_FALLTHROUGH; // pick a default
1080 case 0:
1081 settings.lineAlignment = TextPathLineAlignment::TOP;
1082 break;
1083 case 1:
1084 settings.lineAlignment = TextPathLineAlignment::CENTER;
1085 break;
1086 case 2:
1087 settings.lineAlignment = TextPathLineAlignment::BOTTOM;
1088 break;
1089 }
1090 }
1091
1092 void QXP4Parser::readOleObject(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
1093 {
1094 const unsigned length = readU32(stream, be);
1095 skip(stream, length);
1096 }
1097
1098 void QXP4Parser::readPictureSettings(const std::shared_ptr<librevenge::RVNGInputStream> &stream, std::shared_ptr<PictureBox> &picturebox)
1099 {
1100 skip(stream, 24);
1101 picturebox->pictureRotation = readFraction(stream, be);
1102 picturebox->pictureSkew = readFraction(stream, be);
1103 picturebox->offsetLeft = readFraction(stream, be);
1104 picturebox->offsetTop = readFraction(stream, be);
1105 picturebox->scaleHor = readFraction(stream, be);
1106 picturebox->scaleVert = readFraction(stream, be);
1107 }
1108
1109 void QXP4Parser::readImageData(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
1110 {
1111 const unsigned length = readU32(stream, be);
1112 skip(stream, length);
1113 }
1114
1115 void QXP4Parser::readBezierData(const std::shared_ptr<librevenge::RVNGInputStream> &stream, std::vector<CurveComponent> &curveComponents, Rect &bbox)
1116 {
1117 const unsigned length = readU32(stream, be);
1118 if (length > getRemainingLength(stream))
1119 {
1120 QXP_DEBUG_MSG(("Invalid bezier data length %ul\n", length));
1121 throw ParseError();
1122 }
1123 const unsigned long start = stream->tell();
1124 const unsigned long end = start + length;
1125
1126 try
1127 {
1128 skip(stream, 2);
1129 const unsigned componentsCount = readU16(stream, be);
1130 if (componentsCount > length / 24)
1131 {
1132 QXP_DEBUG_MSG(("Invalid bezier components count %u\n", componentsCount));
1133 throw ParseError();
1134 }
1135
1136 bbox = readObjectBBox(stream);
1137
1138 std::vector<unsigned long> componentsOffsets;
1139 componentsOffsets.resize(componentsCount);
1140 for (auto &off : componentsOffsets)
1141 {
1142 off = start + readU32(stream, be);
1143 }
1144
1145 curveComponents.resize(componentsCount);
1146 unsigned i = 0;
1147 for (auto &comp : curveComponents)
1148 {
1149 seek(stream, componentsOffsets[i++]);
1150
1151 skip(stream, 2);
1152 const unsigned pointsCount = readU16(stream, be);
1153 if (pointsCount > length / 8)
1154 {
1155 QXP_DEBUG_MSG(("Invalid bezier points count %u\n", componentsCount));
1156 throw ParseError();
1157 }
1158
1159 comp.boundingBox = readObjectBBox(stream);
1160
1161 comp.points.resize(pointsCount);
1162 for (auto &p : comp.points)
1163 {
1164 p = readYX(stream);
1165 }
1166 }
1167 }
1168 catch (...)
1169 {
1170 QXP_DEBUG_MSG(("Failed to parse bezier data, offset %ld\n", stream->tell()));
1171 }
1172
1173 seek(stream, end);
1174 }
1175
1176 void QXP4Parser::skipTextObjectEnd(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const QXP4Parser::ObjectHeader &header, const LinkedTextSettings &linkedTextSettings)
1177 {
1178 if (header.contentIndex == 0 || linkedTextSettings.offsetIntoText == 0)
1179 {
1180 skip(stream, 4);
1181 const unsigned fileInfoId = readU32(stream, be);
1182 skip(stream, 4);
1183 if (fileInfoId != 0)
1184 {
1185 skipFileInfo(stream);
1186 }
1187 if (header.contentIndex == 0)
1188 {
1189 skip(stream, 16);
1190 }
1191 }
1192 }
1193
1194 }
1195
1196 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */