Debian Codegraph @

src/lib/QXPParser.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 "QXPParser.h"
11
12 #include "QXPContentCollector.h"
13 #include "QXPHeader.h"
14
15 #include <cmath>
16 #include <memory>
17
18 namespace libqxp
19 {
20
21 using std::make_shared;
22
23 QXPParser::QXPParser(const std::shared_ptr<librevenge::RVNGInputStream> &input, librevenge::RVNGDrawingInterface *painter, const std::shared_ptr<QXPHeader> &header)
24 : m_input(input)
25 , m_painter(painter)
26 , be(header->isBigEndian())
27 , m_blockParser(input, header)
28 , m_textParser(input, header)
29 , m_colors()
30 , m_fonts()
31 , m_charFormats()
32 , m_paragraphFormats()
33 , m_lineStyles()
34 , m_arrows()
35 , m_hjs()
36 , m_groupObjects()
37 , m_header(header)
38 {
39 // default colors, in case parsing fails
40 m_colors[0] = Color(255, 255, 255); // white
41 m_colors[1] = Color(0, 0, 0); // black
42 m_colors[2] = Color(255, 0, 0); // red
43 m_colors[3] = Color(0, 255, 0); // green
44 m_colors[4] = Color(0, 0, 255); // blue
45 m_colors[5] = Color(1, 160, 198); // cyan
46 m_colors[6] = Color(239, 4, 127); // magenta
47 m_colors[7] = Color(255, 255, 0); // yellow
48 m_colors[8] = Color(0, 0, 0); // registration
49
50 // customm dashes are available only from 4.0
51 m_lineStyles[0] = LineStyle({}, true, 1.0, LineCapType::BUTT, LineJoinType::MITER);
52 m_lineStyles[1] = LineStyle({0.6, 0.4}, true, 5.0, LineCapType::BUTT, LineJoinType::MITER);
53 m_lineStyles[2] = LineStyle({0.75, 0.25}, true, 4.0, LineCapType::BUTT, LineJoinType::MITER);
54 m_lineStyles[3] = LineStyle({0.5455, 0.1818, 0.0909, 0.1818}, true, 11.0, LineCapType::BUTT, LineJoinType::MITER);
55 m_lineStyles[4] = LineStyle({0.0, 1.0}, true, 2.0, LineCapType::ROUND, LineJoinType::MITER);
56
57 m_arrows = std::vector<Arrow>( // should be in ctor, but breaks astyle
58 {
59 // does viewbox has any effect?
60 Arrow("m9 0 l-9 25 l6 -1.5 l6 0 l6 1.5 z", "0 0 18 25", 3),
61 Arrow("m9 5 l-9 -5 l0 20 l6 10 l6 0 l6 -10 l0 -20 z", "0 0 18 35", 2.5)
62 });
63 }
64
65 bool QXPParser::parse()
66 {
67 QXPContentCollector collector(m_painter);
68
69 collector.startDocument();
70
71 auto docStream = m_blockParser.getChain(3);
72 if (!parseDocument(docStream, collector))
73 return false;
74
75 if (!parsePages(docStream, collector))
76 return false;
77
78 collector.endDocument();
79
80 return true;
81 }
82
83 Color QXPParser::getColor(unsigned id, Color defaultColor) const
84 {
85 auto it = m_colors.find(id);
86 if (it == m_colors.end())
87 {
88 QXP_DEBUG_MSG(("Color %u not found\n", id));
89 return defaultColor;
90 }
91 return (*it).second;
92 }
93
94 const LineStyle *QXPParser::getLineStyle(unsigned id) const
95 {
96 auto it = m_lineStyles.find(id);
97 if (it == m_lineStyles.end())
98 {
99 QXP_DEBUG_MSG(("Line style %u not found\n", id));
100 return nullptr;
101 }
102 return &it->second;
103 }
104
105 std::string QXPParser::getFont(int id, std::string defaultFont) const
106 {
107 auto it = m_fonts.find(id);
108 if (it == m_fonts.end())
109 {
110 QXP_DEBUG_MSG(("Font %d not found\n", id));
111 return defaultFont;
112 }
113 return (*it).second;
114 }
115
116 void QXPParser::skipRecord(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
117 {
118 unsigned length = readU32(stream, be);
119 if (length > 0)
120 {
121 skip(stream, length);
122 }
123 }
124
125 void QXPParser::parseFonts(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
126 {
127 unsigned length = readU32(stream, be);
128 if (length > getRemainingLength(stream))
129 {
130 QXP_DEBUG_MSG(("Invalid record length %u\n", length));
131 throw ParseError();
132 }
133 const long end = stream->tell() + length;
134
135 try
136 {
137 unsigned count = readU16(stream, be);
138 for (unsigned i = 0; i < count; ++i)
139 {
140 int index = readS16(stream, be);
141 if (m_header->version() >= QXP_4)
142 {
143 skip(stream, 2);
144 }
145 auto name = readPlatformString(stream, be);
146 readPlatformString(stream, be); // skip
147
148 m_fonts[index] = name;
149 }
150 }
151 catch (...)
152 {
153 QXP_DEBUG_MSG(("Failed to parse fonts, offset %ld\n", stream->tell()));
154 }
155
156 seek(stream, end);
157 }
158
159 void QXPParser::parseHJs(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
160 {
161 parseCollection(stream, [=]()
162 {
163 m_hjs.push_back(parseHJ(stream));
164 });
165 }
166
167 void QXPParser::parseCharFormats(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
168 {
169 m_charFormats.clear();
170 parseCollection(stream, [=]()
171 {
172 m_charFormats.push_back(make_shared<CharFormat>(parseCharFormat(stream)));
173 });
174 }
175
176 void QXPParser::parseHJProps(const std::shared_ptr<librevenge::RVNGInputStream> &stream, HJ &result)
177 {
178 skip(stream, 1);
179 result.minBefore = readU8(stream);
180 result.minAfter = readU8(stream);
181 result.maxInRow = readU8(stream);
182 skip(stream, 4);
183 result.singleWordJustify = readU8(stream) == 0;
184 skip(stream, 1);
185 result.hyphenate = readU8(stream) != 0;
186 skip(stream, 33);
187 }
188
189 void QXPParser::parseCommonCharFormatProps(const std::shared_ptr<librevenge::RVNGInputStream> &stream, CharFormat &result)
190 {
191 const int fontIndex = readS16(stream, be);
192 result.fontName = getFont(fontIndex).c_str();
193
194 const unsigned flags = readU16(stream, be);
195 convertCharFormatFlags(flags, result);
196
197 result.fontSize = readFraction(stream, be);
198 }
199
200 TabStop QXPParser::parseTabStop(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
201 {
202 TabStop tabStop;
203
204 const uint8_t type = readU8(stream);
205 tabStop.type = convertTabStopType(type);
206
207 const uint8_t alignChar = readU8(stream);
208 tabStop.alignChar.clear();
209 switch (alignChar)
210 {
211 case 1:
212 tabStop.alignChar.append('.');
213 break;
214 case 2:
215 tabStop.alignChar.append(',');
216 break;
217 default:
218 tabStop.alignChar.append(char(alignChar));
219 break;
220 }
221
222 tabStop.fillChar.clear();
223 tabStop.fillChar.append(char(readU16(stream, be)));
224
225 tabStop.position = readFraction(stream, be);
226
227 return tabStop;
228 }
229
230 void QXPParser::parseParagraphFormats(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
231 {
232 m_paragraphFormats.clear();
233 parseCollection(stream, [=]()
234 {
235 m_paragraphFormats.push_back(make_shared<ParagraphFormat>(parseParagraphFormat(stream)));
236 });
237 }
238
239 void QXPParser::parseCollection(const std::shared_ptr<librevenge::RVNGInputStream> stream, std::function<void()> itemHandler)
240 {
241 unsigned length = readU32(stream, be);
242 if (length > getRemainingLength(stream))
243 {
244 QXP_DEBUG_MSG(("Invalid record length %u\n", length));
245 throw ParseError();
246 }
247 const long end = stream->tell() + length;
248
249 try
250 {
251 while (stream->tell() < end)
252 {
253 itemHandler();
254 }
255 }
256 catch (...)
257 {
258 QXP_DEBUG_MSG(("Failed to parse collection, offset %ld\n", stream->tell()));
259 }
260
261 seek(stream, end);
262 }
263
264 std::vector<PageSettings> QXPParser::parsePageSettings(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
265 {
266 skip(stream, 6);
267 const unsigned count = readU16(stream, be);
268 if (count == 0 || count > 2)
269 {
270 QXP_DEBUG_MSG(("Invalid page settings blocks count %u\n", count));
271 throw ParseError();
272 }
273 skip(stream, 2);
274
275 std::vector<PageSettings> pages;
276 pages.resize(count);
277 for (auto &page : pages)
278 {
279 page.offset.top = readFraction(stream, be);
280 page.offset.left = readFraction(stream, be);
281 page.offset.bottom = readFraction(stream, be);
282 page.offset.right = readFraction(stream, be);
283 skip(stream, 36);
284 skip(stream, m_header->version() >= QXP_4 ? 12 : 8);
285 }
286
287 for (unsigned i = 0; i <= count; ++i)
288 {
289 const unsigned length = readU32(stream, be);
290 skip(stream, length + 4);
291 }
292
293 if (!be)
294 {
295 skip(stream, 4);
296 }
297 const unsigned nameLength = readU32(stream, be);
298 skip(stream, nameLength);
299
300 return pages;
301 }
302
303 std::shared_ptr<Text> QXPParser::parseText(unsigned index, unsigned linkId, QXPCollector &collector)
304 {
305 try
306 {
307 auto text = m_textParser.parseText(index, m_charFormats, m_paragraphFormats);
308 collector.collectText(text, linkId);
309 return text;
310 }
311 catch (...)
312 {
313 QXP_DEBUG_MSG(("Failed to parse text %u\n", index));
314 return make_shared<Text>();
315 }
316 }
317
318 uint32_t QXPParser::readRecordEndOffset(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
319 {
320 unsigned length = readU32(stream, be);
321 return stream->tell() + length;
322 }
323
324 uint8_t QXPParser::readColorComp(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
325 {
326 return uint8_t(std::round(255 * readFloat16(stream, be)));
327 }
328
329 Rect QXPParser::readObjectBBox(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
330 {
331 Rect bbox;
332 bbox.top = readFraction(stream, be);
333 bbox.left = readFraction(stream, be);
334 bbox.bottom = readFraction(stream, be);
335 bbox.right = readFraction(stream, be);
336 return bbox;
337 }
338
339 Gradient QXPParser::readGradient(const std::shared_ptr<librevenge::RVNGInputStream> &stream, const Color &color1)
340 {
341 Gradient gradient;
342 gradient.color1 = color1;
343
344 skip(stream, m_header->version() >= QXP_4 ? 20 : 14);
345
346 const uint8_t type = readU16(stream, be) & 0xff;
347 switch (type)
348 {
349 default:
350 QXP_DEBUG_MSG(("Unknown gradient type %u\n", type));
351 QXP_FALLTHROUGH; // pick a default
352 case 0x10:
353 gradient.type = GradientType::LINEAR;
354 break;
355 case 0x18:
356 gradient.type = GradientType::MIDLINEAR;
357 break;
358 case 0x19:
359 gradient.type = GradientType::RECTANGULAR;
360 break;
361 case 0x1a:
362 gradient.type = GradientType::DIAMOND;
363 break;
364 case 0x1b:
365 gradient.type = GradientType::CIRCULAR;
366 break;
367 case 0x1c:
368 gradient.type = GradientType::FULLCIRCULAR;
369 break;
370 }
371 skip(stream, 4);
372
373 unsigned colorId;
374 if (m_header->version() >= QXP_4)
375 {
376 colorId = readU16(stream, be);
377 }
378 else
379 {
380 colorId = readU8(stream);
381 skip(stream, 1);
382 }
383 const double shade = readFraction(stream, be);
384 gradient.color2 = getColor(colorId).applyShade(shade);
385
386 gradient.angle = readFraction(stream, be);
387 skip(stream, 4);
388
389 return gradient;
390 }
391
392 HorizontalAlignment QXPParser::readHorAlign(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
393 {
394 const uint8_t align = readU8(stream);
395 switch (align)
396 {
397 default:
398 QXP_DEBUG_MSG(("Unknown hor. align %u\n", align));
399 QXP_FALLTHROUGH; // pick a default
400 case 0:
401 return HorizontalAlignment::LEFT;
402 case 1:
403 return HorizontalAlignment::CENTER;
404 case 2:
405 return HorizontalAlignment::RIGHT;
406 case 3:
407 return HorizontalAlignment::JUSTIFIED;
408 case 4:
409 return HorizontalAlignment::FORCED;
410 }
411 }
412
413 VerticalAlignment QXPParser::readVertAlign(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
414 {
415 const uint8_t align = readU8(stream);
416 switch (align)
417 {
418 default:
419 QXP_DEBUG_MSG(("Unknown vert. align %u\n", align));
420 QXP_FALLTHROUGH; // pick a default
421 case 0:
422 return VerticalAlignment::TOP;
423 case 1:
424 return VerticalAlignment::CENTER;
425 case 2:
426 return VerticalAlignment::BOTTOM;
427 case 3:
428 return VerticalAlignment::JUSTIFIED;
429 }
430 }
431
432 Point QXPParser::readYX(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
433 {
434 Point p;
435 p.y = readFraction(stream, be);
436 p.x = readFraction(stream, be);
437 return p;
438 }
439
440 std::shared_ptr<ParagraphRule> QXPParser::readParagraphRule(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
441 {
442 auto rule = make_shared<ParagraphRule>();
443
444 rule->width = readFraction(stream, be);
445
446 const unsigned styleIndex = m_header->version() >= QXP_4 ? readU16(stream, be) : readU8(stream);
447 rule->lineStyle = getLineStyle(styleIndex);
448
449 const unsigned colorId = m_header->version() >= QXP_4 ? readU16(stream, be) : readU8(stream);
450 const double shade = readFraction(stream, be);
451 rule->color = getColor(colorId).applyShade(shade);
452
453 rule->leftMargin = readFraction(stream, be);
454 rule->rightMargin = readFraction(stream, be);
455 rule->offset = readFraction(stream, be);
456
457 return rule;
458 }
459
460 uint8_t QXPParser::readParagraphFlags(const std::shared_ptr<librevenge::RVNGInputStream> &stream, bool &incrementalLeading, bool &ruleAbove, bool &ruleBelow)
461 {
462 const uint8_t flags = readU8(stream);
463 if (be)
464 {
465 ruleBelow = flags & 0x2;
466 ruleAbove = flags & 0x4;
467 incrementalLeading = flags & 0x20;
468 }
469 else
470 {
471 incrementalLeading = flags & 0x4;
472 ruleAbove = flags & 0x20;
473 ruleBelow = flags & 0x40;
474 }
475 return flags;
476 }
477
478 uint8_t QXPParser::readObjectFlags(const std::shared_ptr<librevenge::RVNGInputStream> &stream, bool &noColor)
479 {
480 const uint8_t flags = readU8(stream);
481 if (be)
482 {
483 noColor = flags & 0x80;
484 }
485 else
486 {
487 noColor = flags & 0x1;
488 }
489 return flags;
490 }
491
492 void QXPParser::setArrow(const unsigned index, Frame &frame) const
493 {
494 switch (index)
495 {
496 case 1:
497 frame.endArrow = &m_arrows[0];
498 break;
499 case 2:
500 frame.startArrow = &m_arrows[0];
501 break;
502 case 3:
503 frame.startArrow = &m_arrows[1];
504 frame.endArrow = &m_arrows[0];
505 break;
506 case 4:
507 frame.startArrow = &m_arrows[0];
508 frame.endArrow = &m_arrows[1];
509 break;
510 case 5:
511 frame.startArrow = &m_arrows[0];
512 frame.endArrow = &m_arrows[0];
513 break;
514 }
515 }
516
517 void QXPParser::skipFileInfo(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
518 {
519 const unsigned length = readU32(stream, be);
520 if (length > 0)
521 {
522 skip(stream, length);
523 }
524 }
525
526 void QXPParser::convertCharFormatFlags(unsigned flags, CharFormat &format)
527 {
528 format.bold = flags & 0x1;
529 format.italic = flags & 0x2;
530 format.underline = flags & 0x4;
531 format.outline = flags & 0x8;
532 format.shadow = flags & 0x10;
533 format.superscript = flags & 0x20;
534 format.subscript = flags & 0x40;
535 format.superior = flags & 0x100;
536 format.strike = flags & 0x200;
537 format.allCaps = flags & 0x400;
538 format.smallCaps = flags & 0x800;
539 format.wordUnderline = flags & 0x1000;
540 }
541
542 TabStopType QXPParser::convertTabStopType(unsigned type)
543 {
544 switch (type)
545 {
546 default:
547 QXP_DEBUG_MSG(("Unknown tab stop type %u\n", type));
548 QXP_FALLTHROUGH; // pick a default
549 case 0:
550 return TabStopType::LEFT;
551 case 1:
552 return TabStopType::CENTER;
553 case 2:
554 return TabStopType::RIGHT;
555 case 3:
556 return TabStopType::ALIGN;
557 }
558 }
559
560 void QXPParser::readGroupElements(const std::shared_ptr<librevenge::RVNGInputStream> &stream, unsigned count, const unsigned objectsCount, const unsigned index, std::vector<unsigned> &elements)
561 {
562 elements.reserve(count);
563 for (unsigned i = 0; i < count; ++i)
564 {
565 const unsigned ind = readU32(stream, be);
566 if (ind >= objectsCount || ind == index)
567 {
568 QXP_DEBUG_MSG(("Invalid group element index %u\n", ind));
569 continue;
570 }
571 if (m_groupObjects.insert(ind).second)
572 elements.push_back(ind);
573 }
574 }
575
576 }
577
578 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */