Debian Codegraph @

src/lib/QXPContentCollector.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 "QXPContentCollector.h"
11
12 #include <algorithm>
13 #include <utility>
14 #include <iterator>
15
16 #include <boost/range/adaptor/reversed.hpp>
17 #include <boost/variant.hpp>
18
19 namespace libqxp
20 {
21
22 using librevenge::RVNG_POINT;
23 using librevenge::RVNG_PERCENT;
24 using librevenge::RVNGPropertyList;
25 using librevenge::RVNGPropertyListVector;
26 using librevenge::RVNGString;
27 using std::vector;
28 using std::string;
29
30 namespace
31 {
32
33 void writeBorder(RVNGPropertyList &propList, const char *const name, const double width, const Color &color, const LineStyle *lineStyle)
34 {
35 RVNGString border;
36
37 border.sprintf("%fpt", width);
38
39 border.append(" ");
40 if (lineStyle)
41 {
42 if (lineStyle->isStripe)
43 {
44 border.append("double");
45 }
46 if (lineStyle->segmentLengths.size() == 2)
47 {
48 border.append("dotted");
49 }
50 if (lineStyle->segmentLengths.size() > 2)
51 {
52 border.append("dashed");
53 }
54 else
55 {
56 border.append("solid");
57 }
58 }
59 else
60 {
61 border.append("solid");
62 }
63
64 border.append(" ");
65 border.append(color.toString());
66
67 propList.insert(name, border);
68 }
69
70 void writeBorder(RVNGPropertyList &propList, const char *const name, const std::shared_ptr<ParagraphRule> &rule)
71 {
72 writeBorder(propList, name, rule->width, rule->color, rule->lineStyle);
73 }
74
75 RVNGPropertyListVector createLinePath(const vector<Point> &points, bool closed)
76 {
77 RVNGPropertyListVector path;
78
79 for (size_t i = 0; i < points.size(); i++)
80 {
81 librevenge::RVNGPropertyList pathPart;
82 pathPart.insert("librevenge:path-action", i == 0 ? "M" : "L");
83 pathPart.insert("svg:x", points[i].x, RVNG_POINT);
84 pathPart.insert("svg:y", points[i].y, RVNG_POINT);
85
86 path.append(pathPart);
87 }
88
89 if (closed)
90 {
91 librevenge::RVNGPropertyList pathPart;
92 pathPart.insert("librevenge:path-action", "Z");
93
94 path.append(pathPart);
95 }
96
97 return path;
98 }
99
100 void addBezierPath(RVNGPropertyListVector &path, const vector<Point> &points, bool canBeClosed)
101 {
102 if (points.size() < 6)
103 {
104 QXP_DEBUG_MSG(("Not enough bezier points, %lu\n", points.size()));
105 return;
106 }
107
108 {
109 RVNGPropertyList pathPart;
110 pathPart.insert("librevenge:path-action", "M");
111 pathPart.insert("svg:x", points[1].x, RVNG_POINT);
112 pathPart.insert("svg:y", points[1].y, RVNG_POINT);
113
114 path.append(pathPart);
115 }
116
117 {
118 RVNGPropertyList pathPart;
119 pathPart.insert("librevenge:path-action", "C");
120 pathPart.insert("svg:x1", points[2].x, RVNG_POINT);
121 pathPart.insert("svg:y1", points[2].y, RVNG_POINT);
122 pathPart.insert("svg:x2", points[3].x, RVNG_POINT);
123 pathPart.insert("svg:y2", points[3].y, RVNG_POINT);
124 pathPart.insert("svg:x", points[4].x, RVNG_POINT);
125 pathPart.insert("svg:y", points[4].y, RVNG_POINT);
126
127 path.append(pathPart);
128 }
129
130 for (unsigned i = 6; i < points.size(); i += 3)
131 {
132 if (i + 1 >= points.size())
133 {
134 QXP_DEBUG_MSG(("Unexpected end of bezier points, %u / %lu\n", i, points.size()));
135 break;
136 }
137 RVNGPropertyList pathPart;
138 pathPart.insert("librevenge:path-action", "S");
139 pathPart.insert("svg:x1", points[i].x, RVNG_POINT);
140 pathPart.insert("svg:y1", points[i].y, RVNG_POINT);
141 pathPart.insert("svg:x2", points[3].x, RVNG_POINT);
142 pathPart.insert("svg:y2", points[3].y, RVNG_POINT);
143 pathPart.insert("svg:x", points[i + 1].x, RVNG_POINT);
144 pathPart.insert("svg:y", points[i + 1].y, RVNG_POINT);
145
146 path.append(pathPart);
147 }
148
149 if (canBeClosed && points[1] == points[points.size() - 2])
150 {
151 RVNGPropertyList pathPart;
152 pathPart.insert("librevenge:path-action", "Z");
153
154 path.append(pathPart);
155 }
156 }
157
158 void writeZIndex(librevenge::RVNGPropertyList &propList, unsigned value)
159 {
160 propList.insert("draw:z-index", static_cast<int>(value));
161 }
162
163 void flushText(librevenge::RVNGDrawingInterface *painter, string &text)
164 {
165 if (!text.empty())
166 {
167 painter->insertText(RVNGString(text.c_str()));
168 text.clear();
169 }
170 }
171
172 void insertText(librevenge::RVNGDrawingInterface *painter, const RVNGString &text)
173 {
174 // TODO: need to remember wasSpace on span change?
175 bool wasSpace = false;
176 std::string curText;
177
178 RVNGString::Iter iter(text);
179 iter.rewind();
180 while (iter.next())
181 {
182 const char *const utf8Char = iter();
183 switch (utf8Char[0])
184 {
185 case '\r':
186 wasSpace = false;
187 break;
188 case '\n':
189 wasSpace = false;
190 flushText(painter, curText);
191 painter->insertLineBreak();
192 break;
193 case '\t':
194 wasSpace = false;
195 flushText(painter, curText);
196 painter->insertTab();
197 break;
198 case ' ':
199 if (wasSpace)
200 {
201 flushText(painter, curText);
202 painter->insertSpace();
203 }
204 else
205 {
206 wasSpace = true;
207 curText.push_back(' ');
208 }
209 break;
210 default:
211 wasSpace = false;
212 curText.append(utf8Char);
213 break;
214 }
215 }
216
217 flushText(painter, curText);
218 }
219
220 void writeArrow(RVNGPropertyList &propList, const char *const name, const Arrow &arrow, double width)
221 {
222 librevenge::RVNGString propName;
223
224 propName.sprintf("draw:marker-%s-viewbox", name);
225 propList.insert(propName.cstr(), arrow.viewbox.c_str());
226 propName.sprintf("draw:marker-%s-path", name);
227 propList.insert(propName.cstr(), arrow.path.c_str());
228 propName.sprintf("draw:marker-%s-width", name);
229 propList.insert(propName.cstr(), width * arrow.scale, RVNG_POINT);
230 }
231
232 class FillWriter : public boost::static_visitor<void>
233 {
234 public:
235 explicit FillWriter(RVNGPropertyList &propList)
236 : m_propList(propList)
237 { }
238
239 void operator()(const Color &color)
240 {
241 m_propList.insert("draw:fill", "solid");
242 m_propList.insert("draw:fill-color", color.toString());
243 }
244
245 void operator()(const Gradient &gradient)
246 {
247 m_propList.insert("draw:fill", "gradient");
248 m_propList.insert("draw:start-color", gradient.color1.toString());
249 m_propList.insert("draw:end-color", gradient.color2.toString());
250
251 switch (gradient.type)
252 {
253 default:
254 case GradientType::LINEAR:
255 m_propList.insert("draw:style", "linear");
256 m_propList.insert("draw:angle", int(normalizeDegAngle(gradient.angle + 90)));
257 break;
258 case GradientType::CIRCULAR:
259 case GradientType::FULLCIRCULAR:
260 m_propList.insert("draw:style", "radial");
261 m_propList.insert("draw:cx", 0.5, librevenge::RVNG_PERCENT);
262 m_propList.insert("draw:cy", 0.5, librevenge::RVNG_PERCENT);
263 m_propList.insert("draw:border", GradientType::CIRCULAR == gradient.type ? 0.25 : 0.0, librevenge::RVNG_PERCENT);
264 m_propList.insert("draw:angle", int(normalizeDegAngle(gradient.angle)));
265 break;
266 case GradientType::RECTANGULAR:
267 case GradientType::DIAMOND:
268 m_propList.insert("draw:style", "square");
269 m_propList.insert("draw:cx", 0.5, librevenge::RVNG_PERCENT);
270 m_propList.insert("draw:cy", 0.5, librevenge::RVNG_PERCENT);
271 m_propList.insert("draw:border", 0.0, librevenge::RVNG_PERCENT);
272 m_propList.insert("draw:angle", int(normalizeDegAngle(gradient.angle)));
273 break;
274 }
275 }
276
277 private:
278 librevenge::RVNGPropertyList &m_propList;
279 };
280
281 void writeTextPosition(RVNGPropertyList &propList, const double offset, const double scale = 1.0)
282 {
283 RVNGString pos;
284 pos.sprintf("%f%% %f%%", 100 * offset, 100 * scale);
285 propList.insert("style:text-position", pos);
286 }
287
288 }
289
290 QXPContentCollector::QXPContentCollector(librevenge::RVNGDrawingInterface *painter)
291 : m_painter(painter)
292 , m_isDocumentStarted(false)
293 , m_isCollectingFacingPage(false)
294 , m_currentObjectIndex(0)
295 , m_unprocessedPages()
296 , m_linkTextMap()
297 , m_linkIndexedTextObjectsMap()
298 , m_docProps()
299 {
300 }
301
302 QXPContentCollector::~QXPContentCollector()
303 {
304 if (m_isDocumentStarted)
305 {
306 endDocument();
307 }
308 }
309
310 void QXPContentCollector::startDocument()
311 {
312 if (m_isDocumentStarted)
313 return;
314
315 m_painter->startDocument(RVNGPropertyList());
316
317 m_isDocumentStarted = true;
318 }
319
320 void QXPContentCollector::endDocument()
321 {
322 if (!m_isDocumentStarted)
323 return;
324
325 if (!m_unprocessedPages.empty())
326 endPage();
327
328 if (!m_unprocessedPages.empty())
329 draw(true);
330
331 m_painter->endDocument();
332
333 m_isDocumentStarted = false;
334 }
335
336 void QXPContentCollector::startPage(const Page &page)
337 {
338 m_unprocessedPages.push_back(CollectedPage(page.pageSettings[0]));
339 if (page.isFacing())
340 {
341 m_unprocessedPages.push_back(CollectedPage(page.pageSettings[1]));
342 }
343 m_isCollectingFacingPage = page.isFacing();
344 m_currentObjectIndex = 0;
345 }
346
347 void QXPContentCollector::endPage()
348 {
349 if (!m_unprocessedPages.empty())
350 draw();
351 }
352
353 void QXPContentCollector::collectDocumentProperties(const QXPDocumentProperties &props)
354 {
355 m_docProps = props;
356 }
357
358 void QXPContentCollector::collectLine(const std::shared_ptr<Line> &line)
359 {
360 addObject<Line>(line, &QXPContentCollector::drawLine);
361 }
362
363 void QXPContentCollector::collectBox(const std::shared_ptr<Box> &box)
364 {
365 addObject<Box>(box, &QXPContentCollector::drawBox);
366 }
367
368 void QXPContentCollector::collectTextBox(const std::shared_ptr<TextBox> &textbox)
369 {
370 addObject<TextBox>(textbox, &QXPContentCollector::drawTextBox);
371
372 if (0 == textbox->linkSettings.linkId)
373 {
374 QXP_DEBUG_MSG(("Collected textbox with link ID 0"));
375 }
376
377 collectTextObject(textbox, getInsertionPage(textbox));
378 }
379
380 void QXPContentCollector::collectTextPath(const std::shared_ptr<TextPath> &textPath)
381 {
382 addObject<TextPath>(textPath, &QXPContentCollector::drawTextPath);
383
384 if (0 == textPath->linkSettings.linkId)
385 {
386 QXP_DEBUG_MSG(("Collected text path with link ID 0"));
387 }
388
389 collectTextObject(textPath, getInsertionPage(textPath));
390 }
391
392 void QXPContentCollector::collectGroup(const std::shared_ptr<Group> &group)
393 {
394 auto collectedObj = addObject<Group>(group, &QXPContentCollector::drawGroup);
395
396 getInsertionPage(group).groups.push_back(collectedObj);
397 }
398
399 void QXPContentCollector::collectText(const std::shared_ptr<Text> &text, const unsigned linkId)
400 {
401 m_linkTextMap[linkId] = text;
402
403 auto it = m_linkIndexedTextObjectsMap.find(linkId);
404 if (it != m_linkIndexedTextObjectsMap.end())
405 {
406 for (const auto &indexTextbox : it->second)
407 {
408 if (!indexTextbox.second->text)
409 {
410 indexTextbox.second->text = text;
411 }
412 }
413 }
414 }
415
416 QXPContentCollector::CollectedPage &QXPContentCollector::getInsertionPage(const std::shared_ptr<Object> &obj)
417 {
418 if (m_isCollectingFacingPage && obj->boundingBox.left < m_unprocessedPages.back().settings.offset.left)
419 {
420 return m_unprocessedPages[m_unprocessedPages.size() - 2];
421 }
422 return m_unprocessedPages.back();
423 }
424
425 void QXPContentCollector::draw(bool force)
426 {
427 updateLinkedTexts();
428
429 if (hasUnfinishedLinkedTexts())
430 {
431 if (!force)
432 {
433 return;
434 }
435 QXP_DEBUG_MSG(("Drawing with unfinished linked texts\n"));
436 }
437
438 for (auto &page : m_unprocessedPages)
439 {
440 RVNGPropertyList propList;
441 propList.insert("svg:width", page.settings.offset.width(), RVNG_POINT);
442 propList.insert("svg:height", page.settings.offset.height(), RVNG_POINT);
443 m_painter->startPage(propList);
444
445 {
446 unsigned i = 0;
447 for (auto &obj : boost::adaptors::reverse(page.objects))
448 {
449 obj.second->setZIndex(i);
450 // we can't just increment by 1 because some objects may need to create several elements (such as box + text)
451 // also we can't just have a counter instead of this field because groups may not be consecutive
452 i += 100;
453 }
454 }
455
456 // handle groups first
457 // afaik groups that are part of other group never go before that group
458 for (const auto &group : page.groups)
459 {
460 group->draw(page);
461 }
462
463 for (auto &obj : page.objects)
464 {
465 obj.second->draw(page);
466 }
467
468 m_painter->endPage();
469 }
470
471 m_unprocessedPages.clear();
472 }
473
474 void QXPContentCollector::collectTextObject(const std::shared_ptr<TextObject> &textObj, CollectedPage &page)
475 {
476 if (textObj->linkSettings.linkedIndex > 0)
477 {
478 m_linkIndexedTextObjectsMap[textObj->linkSettings.linkId][textObj->linkSettings.linkedIndex] = textObj;
479 }
480
481 if (textObj->isLinked())
482 {
483 page.linkedTextObjects.push_back(textObj);
484 }
485
486 if (!textObj->text)
487 {
488 auto textIt = m_linkTextMap.find(textObj->linkSettings.linkId);
489 if (textIt != m_linkTextMap.end())
490 {
491 textObj->text = textIt->second;
492 }
493 }
494 }
495
496 void QXPContentCollector::updateLinkedTexts()
497 {
498 for (const auto &page : m_unprocessedPages)
499 {
500 for (const auto &textObj : page.linkedTextObjects)
501 {
502 if (textObj->linkSettings.nextLinkedIndex > 0 && !textObj->linkSettings.textLength)
503 {
504 const auto textObjectsIt = m_linkIndexedTextObjectsMap.find(textObj->linkSettings.linkId);
505 if (textObjectsIt != m_linkIndexedTextObjectsMap.end())
506 {
507 const auto &textObjects = textObjectsIt->second;
508 const auto nextTextObjectIt = textObjects.find(textObj->linkSettings.nextLinkedIndex);
509 if (nextTextObjectIt != textObjects.end())
510 {
511 textObj->linkSettings.textLength = nextTextObjectIt->second->linkSettings.offsetIntoText - textObj->linkSettings.offsetIntoText;
512 }
513 }
514 }
515 }
516 }
517 }
518
519 bool QXPContentCollector::hasUnfinishedLinkedTexts()
520 {
521 for (const auto &page : m_unprocessedPages)
522 {
523 for (const auto &textObj : page.linkedTextObjects)
524 {
525 if (!textObj->text || (textObj->linkSettings.nextLinkedIndex > 0 && !textObj->linkSettings.textLength))
526 {
527 return true;
528 }
529 }
530 }
531
532 return false;
533 }
534
535 void QXPContentCollector::drawLine(const std::shared_ptr<Line> &line, const QXPContentCollector::CollectedPage &page)
536 {
537 RVNGPropertyListVector path;
538 if (line->curveComponents.empty())
539 {
540 vector<Point> points =
541 {
542 page.getPoint(line->boundingBox.topLeft().rotateDeg(-line->rotation, line->boundingBox.center())),
543 page.getPoint(line->boundingBox.bottomRight().rotateDeg(-line->rotation, line->boundingBox.center()))
544 };
545
546 path = createLinePath(points, false);
547 }
548 else
549 {
550 for (const auto &curve : line->curveComponents)
551 {
552 vector<Point> points;
553 points.reserve(curve.points.size());
554 std::transform(curve.points.begin(), curve.points.end(), std::back_inserter(points),
555 [page, &line](const Point &p)
556 {
557 return page.getPoint(p.rotateDeg(-line->rotation, line->boundingBox.center()));
558 });
559
560 addBezierPath(path, points, false);
561 }
562 }
563
564 RVNGPropertyList propList;
565
566 writeFrame(propList, line->style, line->runaround, true);
567
568 m_painter->setStyle(propList);
569 propList.clear();
570
571 propList.insert("svg:d", path);
572 writeZIndex(propList, line->zIndex);
573
574 m_painter->drawPath(propList);
575 }
576
577 void QXPContentCollector::drawBox(const std::shared_ptr<Box> &box, const QXPContentCollector::CollectedPage &page)
578 {
579 switch (box->boxType)
580 {
581 default:
582 case BoxType::RECTANGLE:
583 drawRectangle(box, page);
584 break;
585 case BoxType::OVAL:
586 drawOval(box, page);
587 break;
588 case BoxType::POLYGON:
589 drawPolygon(box, page);
590 break;
591 case BoxType::BEZIER:
592 drawBezierBox(box, page);
593 break;
594 }
595 }
596
597 void QXPContentCollector::drawRectangle(const std::shared_ptr<Box> &box, const QXPContentCollector::CollectedPage &page)
598 {
599 const auto bbox = box->boundingBox.shrink(box->frame.width / 2);
600 vector<Point> points =
601 {
602 page.getPoint(bbox.topLeft()),
603 page.getPoint(bbox.topRight()),
604 page.getPoint(bbox.bottomRight()),
605 page.getPoint(bbox.bottomLeft())
606 };
607
608 if (!QXP_ALMOST_ZERO(box->rotation))
609 {
610 for (auto &p : points)
611 {
612 p = p.rotateDeg(-box->rotation, page.getPoint(box->boundingBox.center()));
613 }
614 }
615
616 auto path = createLinePath(points, true);
617
618 RVNGPropertyList propList;
619
620 writeFrame(propList, box->frame, box->runaround);
621 writeFill(propList, box->fill);
622
623 m_painter->setStyle(propList);
624 propList.clear();
625
626 propList.insert("svg:d", path);
627 writeZIndex(propList, box->zIndex);
628
629 m_painter->drawPath(propList);
630 }
631
632 void QXPContentCollector::drawOval(const std::shared_ptr<Box> &oval, const QXPContentCollector::CollectedPage &page)
633 {
634 librevenge::RVNGPropertyList propList;
635
636 writeFrame(propList, oval->frame, oval->runaround);
637 writeFill(propList, oval->fill);
638
639 m_painter->setStyle(propList);
640 propList.clear();
641
642 propList.insert("svg:cx", page.getX(oval->boundingBox.center().x), RVNG_POINT);
643 propList.insert("svg:cy", page.getY(oval->boundingBox.center().y), RVNG_POINT);
644 propList.insert("svg:rx", oval->boundingBox.width() / 2 - oval->frame.width / 2, RVNG_POINT);
645 propList.insert("svg:ry", oval->boundingBox.height() / 2 - oval->frame.width / 2, RVNG_POINT);
646 if (!QXP_ALMOST_ZERO(oval->rotation))
647 {
648 propList.insert("librevenge:rotate", oval->rotation);
649 }
650
651 writeZIndex(propList, oval->zIndex);
652
653 m_painter->drawEllipse(propList);
654 }
655
656 void QXPContentCollector::drawPolygon(const std::shared_ptr<Box> &polygon, const QXPContentCollector::CollectedPage &page)
657 {
658 vector<Point> points;
659 points.reserve(polygon->customPoints.size());
660 std::transform(polygon->customPoints.begin(), polygon->customPoints.end(), std::back_inserter(points),
661 [page, &polygon](const Point &p)
662 {
663 return page.getPoint(p.rotateDeg(-polygon->rotation, polygon->boundingBox.center()));
664 });
665
666 auto path = createLinePath(points, true);
667
668 RVNGPropertyList propList;
669
670 writeFrame(propList, polygon->frame, polygon->runaround);
671 writeFill(propList, polygon->fill);
672
673 m_painter->setStyle(propList);
674 propList.clear();
675
676 propList.insert("svg:d", path);
677 writeZIndex(propList, polygon->zIndex);
678
679 m_painter->drawPath(propList);
680 }
681
682 void QXPContentCollector::drawBezierBox(const std::shared_ptr<Box> &box, const QXPContentCollector::CollectedPage &page)
683 {
684 RVNGPropertyListVector path;
685 for (const auto &curve : box->curveComponents)
686 {
687 vector<Point> points;
688 points.reserve(curve.points.size());
689 std::transform(curve.points.begin(), curve.points.end(), std::back_inserter(points),
690 [page, &box](const Point &p)
691 {
692 return page.getPoint(p.rotateDeg(-box->rotation, box->boundingBox.center()));
693 });
694
695 addBezierPath(path, points, true);
696 }
697
698 RVNGPropertyList propList;
699
700 writeFrame(propList, box->frame, box->runaround);
701 writeFill(propList, box->fill);
702
703 m_painter->setStyle(propList);
704 propList.clear();
705
706 propList.insert("svg:d", path);
707 writeZIndex(propList, box->zIndex);
708
709 m_painter->drawPath(propList);
710 }
711
712 void QXPContentCollector::drawTextBox(const std::shared_ptr<TextBox> &textbox, const QXPContentCollector::CollectedPage &page)
713 {
714 drawBox(textbox, page);
715
716 const auto bbox = textbox->boundingBox.shrink(textbox->frame.width);
717
718 librevenge::RVNGPropertyList textObjPropList;
719
720 textObjPropList.insert("svg:x", page.getX(bbox.left), RVNG_POINT);
721 textObjPropList.insert("svg:y", page.getY(bbox.top), RVNG_POINT);
722 textObjPropList.insert("svg:width", bbox.width(), RVNG_POINT);
723 textObjPropList.insert("svg:height", bbox.height(), RVNG_POINT);
724
725 textObjPropList.insert("fo:padding-top", 0, RVNG_POINT);
726 textObjPropList.insert("fo:padding-right", 0, RVNG_POINT);
727 textObjPropList.insert("fo:padding-bottom", 0, RVNG_POINT);
728 textObjPropList.insert("fo:padding-left", 3, RVNG_POINT);
729
730 switch (textbox->settings.verticalAlignment)
731 {
732 case VerticalAlignment::TOP:
733 textObjPropList.insert("draw:textarea-vertical-align", "top");
734 break;
735 case VerticalAlignment::CENTER:
736 textObjPropList.insert("draw:textarea-vertical-align", "middle");
737 break;
738 case VerticalAlignment::BOTTOM:
739 textObjPropList.insert("draw:textarea-vertical-align", "bottom");
740 break;
741 case VerticalAlignment::JUSTIFIED:
742 textObjPropList.insert("draw:textarea-vertical-align", "justify");
743 break;
744 }
745 if (!QXP_ALMOST_ZERO(textbox->rotation))
746 {
747 textObjPropList.insert("librevenge:rotate", -textbox->rotation);
748 }
749
750 writeZIndex(textObjPropList, textbox->zIndex + 1);
751
752 m_painter->startTextObject(textObjPropList);
753
754 if (textbox->text)
755 {
756 drawText(textbox->text.get(), textbox->linkSettings);
757 }
758
759 m_painter->endTextObject();
760 }
761
762 void QXPContentCollector::drawTextPath(const std::shared_ptr<TextPath> &textPath, const QXPContentCollector::CollectedPage &page)
763 {
764 drawLine(textPath, page);
765
766 if (!textPath->text)
767 {
768 return;
769 }
770
771 double lineY;
772 switch (textPath->settings.lineAlignment)
773 {
774 default:
775 case TextPathLineAlignment::TOP:
776 lineY = textPath->boundingBox.top - textPath->style.width / 2;
777 break;
778 case TextPathLineAlignment::CENTER:
779 lineY = textPath->boundingBox.top;
780 break;
781 case TextPathLineAlignment::BOTTOM:
782 lineY = textPath->boundingBox.top + textPath->style.width / 2;
783 break;
784 }
785
786 const double height = textPath->text.get()->maxFontSize();
787
788 double textY;
789 switch (textPath->settings.alignment)
790 {
791 default:
792 case TextPathAlignment::DESCENT:
793 case TextPathAlignment::BASELINE:
794 textY = lineY - height;
795 break;
796 case TextPathAlignment::CENTER:
797 textY = lineY - height / 2;
798 break;
799 case TextPathAlignment::ASCENT:
800 textY = lineY;
801 break;
802 }
803
804 librevenge::RVNGPropertyList textObjPropList;
805
806 textObjPropList.insert("svg:x", page.getX(textPath->boundingBox.left), RVNG_POINT);
807 textObjPropList.insert("svg:y", page.getY(textY), RVNG_POINT);
808 // shouldn't grow vertically
809 textObjPropList.insert("svg:width", textPath->boundingBox.width() + height, RVNG_POINT);
810 textObjPropList.insert("svg:height", height, RVNG_POINT);
811
812 textObjPropList.insert("fo:padding-top", 0, RVNG_POINT);
813 textObjPropList.insert("fo:padding-right", 0, RVNG_POINT);
814 textObjPropList.insert("fo:padding-bottom", 0, RVNG_POINT);
815 textObjPropList.insert("fo:padding-left", 0, RVNG_POINT);
816
817 if (!QXP_ALMOST_ZERO(textPath->rotation))
818 {
819 textObjPropList.insert("librevenge:rotate", -textPath->rotation);
820 }
821
822 writeZIndex(textObjPropList, textPath->zIndex + 1);
823
824 m_painter->startTextObject(textObjPropList);
825
826 drawText(textPath->text.get(), textPath->linkSettings);
827
828 m_painter->endTextObject();
829 }
830
831 void QXPContentCollector::drawText(const std::shared_ptr<Text> &text, const LinkedTextSettings &linkSettings)
832 {
833 unsigned long spanTextStart = linkSettings.offsetIntoText;
834 const unsigned long textEnd = linkSettings.textLength ? (spanTextStart + linkSettings.textLength.get()) : text->text.length();
835
836 unsigned paragraphInd = 0;
837
838 for (auto &paragraph : text->paragraphs)
839 {
840 if (paragraph.startIndex >= textEnd)
841 {
842 break;
843 }
844 if (spanTextStart > paragraph.endIndex())
845 {
846 continue;
847 }
848
849 librevenge::RVNGPropertyList paragraphPropList;
850
851 paragraphPropList.insert("fo:margin-top", paragraph.format->margin.top, RVNG_POINT);
852 paragraphPropList.insert("fo:margin-right", paragraph.format->margin.right, RVNG_POINT);
853 paragraphPropList.insert("fo:margin-bottom", paragraph.format->margin.bottom, RVNG_POINT);
854 paragraphPropList.insert("fo:margin-left", paragraph.format->margin.left, RVNG_POINT);
855
856 paragraphPropList.insert("fo:text-indent", paragraph.format->firstLineIndent, RVNG_POINT);
857
858 if (!QXP_ALMOST_ZERO(paragraph.format->leading) && !paragraph.format->incrementalLeading)
859 {
860 paragraphPropList.insert("fo:line-height", paragraph.format->leading, RVNG_POINT);
861 }
862 else
863 {
864 const double fontSize = text->maxFontSize(paragraph);
865 const double initialLeading = fontSize + (m_docProps.isIncrementalAutoLeading() ? m_docProps.autoLeading() : (fontSize * m_docProps.autoLeading()));
866 const double lineHeight = initialLeading + paragraph.format->leading;
867 paragraphPropList.insert("fo:line-height", lineHeight, RVNG_POINT);
868 }
869
870 switch (paragraph.format->alignment)
871 {
872 case HorizontalAlignment::LEFT:
873 paragraphPropList.insert("fo:text-align", "left");
874 break;
875 case HorizontalAlignment::RIGHT:
876 paragraphPropList.insert("fo:text-align", "end");
877 break;
878 case HorizontalAlignment::CENTER:
879 paragraphPropList.insert("fo:text-align", "center");
880 break;
881 case HorizontalAlignment::JUSTIFIED:
882 case HorizontalAlignment::FORCED:
883 paragraphPropList.insert("fo:text-align", "justify");
884 break;
885 }
886 paragraphPropList.insert("fo:text-align-last", "start");
887
888 if (paragraph.format->hj)
889 {
890 paragraphPropList.insert("fo:hyphenate", paragraph.format->hj->hyphenate);
891 if (paragraph.format->hj->maxInRow == 0)
892 paragraphPropList.insert("fo:hyphenation-ladder-count", "no-limit");
893 else
894 paragraphPropList.insert("fo:hyphenation-ladder-count", int(paragraph.format->hj->maxInRow));
895 paragraphPropList.insert("style:justify-single-word", paragraph.format->hj->singleWordJustify);
896 }
897
898 if (!paragraph.format->tabStops.empty())
899 {
900 RVNGPropertyListVector tabs;
901 for (const auto &tab : paragraph.format->tabStops)
902 {
903 RVNGPropertyList tabProps;
904 tabProps.insert("style:position", tab.position, RVNG_POINT);
905 if (!tab.fillChar.empty())
906 {
907 tabProps.insert("style:leader-text", tab.fillChar);
908 }
909 switch (tab.type)
910 {
911 case TabStopType::LEFT:
912 tabProps.insert("style:type", "left");
913 break;
914 case TabStopType::RIGHT:
915 tabProps.insert("style:type", "right");
916 break;
917 case TabStopType::CENTER:
918 tabProps.insert("style:type", "center");
919 break;
920 case TabStopType::ALIGN:
921 tabProps.insert("style:type", "char");
922 tabProps.insert("style:char", tab.alignChar);
923 break;
924 }
925 tabs.append(tabProps);
926 }
927 paragraphPropList.insert("librevenge:tab-stops", tabs);
928 }
929
930 if (paragraph.format->ruleAbove && paragraphInd > 0)
931 {
932 writeBorder(paragraphPropList, "fo:border-top", paragraph.format->ruleAbove);
933 }
934 if (paragraph.format->ruleBelow && paragraphInd < text->paragraphs.size() - 1)
935 {
936 writeBorder(paragraphPropList, "fo:border-bottom", paragraph.format->ruleBelow);
937 }
938
939 m_painter->openParagraph(paragraphPropList);
940
941 for (auto &charFormat : text->charFormats)
942 {
943 if (spanTextStart > paragraph.endIndex() || spanTextStart >= textEnd || charFormat.startIndex > paragraph.endIndex() || charFormat.startIndex >= textEnd)
944 {
945 break;
946 }
947
948 if (spanTextStart > charFormat.endIndex())
949 {
950 continue;
951 }
952
953 if (spanTextStart >= text->text.length())
954 {
955 QXP_DEBUG_MSG(("Span start %lu out of range\n", spanTextStart));
956 break;
957 }
958
959 const auto spanTextEnd = static_cast<unsigned long>(
960 std::min<uint64_t>({ charFormat.afterEndIndex(), paragraph.afterEndIndex(), text->text.length(), textEnd })
961 );
962
963 if (charFormat.format->isControlChars)
964 {
965 spanTextStart = spanTextEnd;
966 continue;
967 }
968
969 librevenge::RVNGPropertyList spanPropList;
970
971 const double fontSize = std::max(charFormat.format->fontSize, 1.);
972
973 spanPropList.insert("style:font-name", charFormat.format->fontName);
974 spanPropList.insert("fo:font-size", fontSize, librevenge::RVNG_POINT);
975 spanPropList.insert("fo:font-weight", charFormat.format->bold ? "bold" : "normal");
976 spanPropList.insert("fo:font-style", charFormat.format->italic ? "italic" : "normal");
977 if (charFormat.format->underline || charFormat.format->wordUnderline)
978 {
979 spanPropList.insert("style:text-underline-color", "font-color");
980 spanPropList.insert("style:text-underline-type", "single");
981 spanPropList.insert("style:text-underline-style", "solid");
982 spanPropList.insert("style:text-underline-mode", charFormat.format->wordUnderline ? "skip-white-space" : "continuous");
983 }
984 if (charFormat.format->strike)
985 {
986 spanPropList.insert("style:text-line-through-color", "font-color");
987 spanPropList.insert("style:text-line-through-mode", "continuous");
988 spanPropList.insert("style:text-line-through-type", "single");
989 spanPropList.insert("style:text-line-through-style", "solid");
990 spanPropList.insert("style:text-line-through-width", "1pt");
991 }
992 spanPropList.insert("fo:font-variant", charFormat.format->smallCaps ? "small-caps" : "normal");
993 if (charFormat.format->allCaps)
994 spanPropList.insert("fo:text-transform", "capitalize");
995 spanPropList.insert("style:text-outline", charFormat.format->outline);
996 if (charFormat.format->shadow)
997 spanPropList.insert("fo:text-shadow", "1pt 1pt");
998 spanPropList.insert("fo:color", charFormat.format->color.toString());
999
1000 if (charFormat.format->subscript)
1001 {
1002 writeTextPosition(spanPropList, m_docProps.subscriptOffset + charFormat.format->baselineShift, m_docProps.subscriptVScale);
1003 spanPropList.insert("style:text-scale", m_docProps.subscriptHScale, librevenge::RVNG_PERCENT);
1004 }
1005 else if (charFormat.format->superscript)
1006 {
1007 writeTextPosition(spanPropList, m_docProps.superscriptOffset + charFormat.format->baselineShift, m_docProps.superscriptVScale);
1008 spanPropList.insert("style:text-scale", m_docProps.superscriptHScale, librevenge::RVNG_PERCENT);
1009 }
1010 else if (charFormat.format->superior)
1011 {
1012 // approximate "superior" positioning (char ascents are aligned with the cap height of the current font)
1013 const double offset = (fontSize * (1.0 - m_docProps.superiorVScale)) / fontSize;
1014 writeTextPosition(spanPropList, offset + charFormat.format->baselineShift, m_docProps.superiorVScale);
1015 spanPropList.insert("style:text-scale", m_docProps.superiorHScale, librevenge::RVNG_PERCENT);
1016 }
1017 else if (charFormat.format->baselineShift != 0.0)
1018 {
1019 writeTextPosition(spanPropList, charFormat.format->baselineShift);
1020 }
1021
1022 if (paragraph.format->hj)
1023 {
1024 spanPropList.insert("fo:hyphenation-remain-char-count", std::max(int(paragraph.format->hj->minBefore), 1));
1025 spanPropList.insert("fo:hyphenation-push-char-count", std::max(int(paragraph.format->hj->minAfter), 1));
1026 }
1027
1028 m_painter->openSpan(spanPropList);
1029
1030 auto sourceStr = text->text.substr(spanTextStart, spanTextEnd - spanTextStart);
1031 RVNGString str;
1032 appendCharacters(str, sourceStr.c_str(), sourceStr.length(), text->encoding);
1033
1034 insertText(m_painter, str);
1035
1036 m_painter->closeSpan();
1037
1038 spanTextStart = spanTextEnd;
1039 }
1040
1041 m_painter->closeParagraph();
1042
1043 paragraphInd++;
1044 }
1045 }
1046
1047 void QXPContentCollector::drawGroup(const std::shared_ptr<Group> &group, const QXPContentCollector::CollectedPage &page)
1048 {
1049 bool groupOpened = false;
1050
1051 for (const unsigned &ind : group->objectsIndexes)
1052 {
1053 const auto it = page.objects.find(ind);
1054 if (it == page.objects.end())
1055 {
1056 QXP_DEBUG_MSG(("Group element %u not found\n", ind));
1057 continue;
1058 }
1059 const auto &obj = it->second;
1060
1061 if (!groupOpened)
1062 {
1063 RVNGPropertyList propList;
1064 writeZIndex(propList, obj->zIndex() - 1);
1065 m_painter->openGroup(propList);
1066
1067 groupOpened = true;
1068 }
1069
1070 obj->draw(page);
1071 }
1072
1073 if (groupOpened)
1074 {
1075 m_painter->closeGroup();
1076 }
1077 }
1078
1079 void QXPContentCollector::writeFill(librevenge::RVNGPropertyList &propList, const boost::optional<Fill> &fill)
1080 {
1081 propList.insert("draw:fill", "none");
1082
1083 if (fill)
1084 {
1085 FillWriter fillWriter(propList);
1086 boost::apply_visitor(fillWriter, fill.get());
1087 }
1088 }
1089
1090 void QXPContentCollector::writeFrame(librevenge::RVNGPropertyList &propList, const Frame &frame, const bool runaround, const bool allowHairline)
1091 {
1092 propList.insert("draw:stroke", "none");
1093
1094 if (frame.color && (allowHairline || !QXP_ALMOST_ZERO(frame.width)))
1095 {
1096 propList.insert("draw:stroke", "solid");
1097 propList.insert("svg:stroke-color", frame.color->toString());
1098 propList.insert("svg:stroke-width", frame.width, RVNG_POINT);
1099
1100 if (frame.lineStyle)
1101 {
1102 if (frame.lineStyle->segmentLengths.size() > 1 && !frame.lineStyle->isStripe)
1103 {
1104 const double dots1 = frame.lineStyle->segmentLengths[0];
1105 const double dist = frame.lineStyle->segmentLengths[1];
1106 const double dots2 = frame.lineStyle->segmentLengths[frame.lineStyle->segmentLengths.size() >= 3 ? 2 : 0];
1107 const double scale = frame.lineStyle->isProportional ? frame.lineStyle->patternLength : 1.0;
1108 const auto unit = frame.lineStyle->isProportional ? RVNG_PERCENT : RVNG_POINT;
1109
1110 propList.insert("draw:stroke", "dash");
1111 propList.insert("draw:dots1", 1);
1112 propList.insert("draw:dots1-length", dots1 * scale, unit);
1113 propList.insert("draw:dots2", 1);
1114 propList.insert("draw:dots2-length", dots2 * scale, unit);
1115 propList.insert("draw:distance", dist * scale, unit);
1116 }
1117
1118 switch (frame.lineStyle->endcapType)
1119 {
1120 default:
1121 case LineCapType::BUTT:
1122 propList.insert("svg:stroke-linecap", "butt");
1123 break;
1124 case LineCapType::ROUND:
1125 propList.insert("svg:stroke-linecap", "round");
1126 break;
1127 case LineCapType::RECT:
1128 propList.insert("svg:stroke-linecap", "square");
1129 break;
1130 }
1131
1132 switch (frame.lineStyle->joinType)
1133 {
1134 default:
1135 case LineJoinType::BEVEL:
1136 propList.insert("svg:stroke-linejoin", "bevel");
1137 break;
1138 case LineJoinType::MITER:
1139 propList.insert("svg:stroke-linejoin", "miter");
1140 break;
1141 case LineJoinType::ROUND:
1142 propList.insert("svg:stroke-linejoin", "round");
1143 break;
1144 }
1145 }
1146
1147 if (frame.startArrow)
1148 {
1149 writeArrow(propList, "start", *frame.startArrow, frame.width);
1150 }
1151 if (frame.endArrow)
1152 {
1153 writeArrow(propList, "end", *frame.endArrow, frame.width);
1154 }
1155 }
1156
1157 if (runaround)
1158 {
1159 propList.insert("style:wrap", "biggest");
1160 }
1161 }
1162
1163 double QXPContentCollector::CollectedPage::getX(const double x) const
1164 {
1165 return x - settings.offset.left;
1166 }
1167
1168 double QXPContentCollector::CollectedPage::getX(const std::shared_ptr<Object> &obj) const
1169 {
1170 return getX(obj->boundingBox.left);
1171 }
1172
1173 double QXPContentCollector::CollectedPage::getY(const double y) const
1174 {
1175 return y - settings.offset.top;
1176 }
1177
1178 double QXPContentCollector::CollectedPage::getY(const std::shared_ptr<Object> &obj) const
1179 {
1180 return getY(obj->boundingBox.top);
1181 }
1182
1183 Point QXPContentCollector::CollectedPage::getPoint(const Point &p) const
1184 {
1185 return Point(getX(p.x), getY(p.y));
1186 }
1187
1188
1189 }
1190
1191 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */