QXPContentCollector.h Source File documentation

QXPContentCollector.h
Go to the documentation of this file.
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 #ifndef QXPCONTENTCOLLECTOR_H_INCLUDED
11 #define QXPCONTENTCOLLECTOR_H_INCLUDED
12
13 #include "QXPCollector.h"
14 #include <vector>
15 #include <unordered_map>
16 #include <map>
17 #include <memory>
18 #include <functional>
19 #include <type_traits>
20
21 #include "QXPTypes.h"
22
23 namespace libqxp
24 {
25
26 class QXPContentCollector : public QXPCollector
27 {
28 // disable copying
29 QXPContentCollector(const QXPContentCollector &other) = delete;
30 QXPContentCollector &operator=(const QXPContentCollector &other) = delete;
31
32 public:
33 QXPContentCollector(librevenge::RVNGDrawingInterface *painter);
34 ~QXPContentCollector();
35
36 void startDocument() override;
37 void endDocument() override;
38
39 void startPage(const Page &page) override;
40 void endPage() override;
41
42 void collectDocumentProperties(const QXPDocumentProperties &props) override;
43
44 void collectLine(const std::shared_ptr<Line> &line) override;
45 void collectBox(const std::shared_ptr<Box> &box) override;
46 void collectTextBox(const std::shared_ptr<TextBox> &box) override;
47 void collectTextPath(const std::shared_ptr<TextPath> &textPath) override;
48 void collectGroup(const std::shared_ptr<Group> &group) override;
49
50 void collectText(const std::shared_ptr<Text> &text, const unsigned linkId) override;
51
52 private:
53 struct CollectedPage;
54
55 template<typename T>
56 using ObjectHandler = std::function<void(const std::shared_ptr<T> &obj, const CollectedPage &)>;
57
58 template<typename T, typename TThis>
59 using ObjectHandlerMember = std::function<void(TThis *, const std::shared_ptr<T> &obj, const CollectedPage &)>;
60
61 class CollectedObjectInterface
62 {
63 public:
64 virtual ~CollectedObjectInterface() = default;
65
66 virtual void draw(const CollectedPage &page) = 0;
67
68 virtual unsigned zIndex() const = 0;
69 virtual void setZIndex(unsigned value) = 0;
70 };
71
72 template<typename T>
73 class CollectedObject : public CollectedObjectInterface
74 {
75 static_assert(std::is_base_of<Object, T>::value, "T is not Object");
76 public:
77 const std::shared_ptr<T> object;
78
79 CollectedObject(const std::shared_ptr<T> &obj, const ObjectHandler<T> &handler)
80 : object(obj), m_handler(handler), m_isProcessed(false)
81 { }
82
83 void draw(const CollectedPage &page) override
84 {
85 if (!m_isProcessed)
86 {
87 m_isProcessed = true;
88 m_handler(object, page);
89 }
90 }
91
92 unsigned zIndex() const override
93 {
94 return object->zIndex;
95 }
96
97 void setZIndex(unsigned value) override
98 {
99 object->zIndex = value;
100 }
101
102 private:
103 const ObjectHandler<T> m_handler;
104 bool m_isProcessed;
105 };
106
107 struct CollectedPage
108 {
109 const PageSettings settings;
110 std::vector<std::shared_ptr<CollectedObject<Group>>> groups;
111 std::vector<std::shared_ptr<TextObject>> linkedTextObjects;
112 std::map<unsigned, std::shared_ptr<CollectedObjectInterface>> objects;
113
114 CollectedPage(const PageSettings &pageSettings)
115 : settings(pageSettings), groups(), linkedTextObjects(), objects()
116 { }
117
118 double getX(const double x) const;
119 double getX(const std::shared_ptr<Object> &obj) const;
120 double getY(const double y) const;
121 double getY(const std::shared_ptr<Object> &obj) const;
122 Point getPoint(const Point &p) const;
123 };
124
125 librevenge::RVNGDrawingInterface *m_painter;
126
127 bool m_isDocumentStarted;
128 bool m_isCollectingFacingPage;
129 unsigned m_currentObjectIndex;
130
131 std::vector<CollectedPage> m_unprocessedPages;
132
133 std::unordered_map<unsigned, std::shared_ptr<Text>> m_linkTextMap;
134 std::unordered_map<unsigned, std::unordered_map<unsigned, std::shared_ptr<TextObject>>> m_linkIndexedTextObjectsMap;
135
136 QXPDocumentProperties m_docProps;
137
138 CollectedPage &getInsertionPage(const std::shared_ptr<Object> &obj);
139
140 template<typename T>
141 std::shared_ptr<CollectedObject<T>> addObject(const std::shared_ptr<T> &obj, const ObjectHandlerMember<T, QXPContentCollector> &handler)
142 {
143 auto collectedObj = std::make_shared<CollectedObject<T>>(obj, std::bind(handler, this, std::placeholders::_1, std::placeholders::_2));
144 getInsertionPage(obj).objects[m_currentObjectIndex] = collectedObj;
145 ++m_currentObjectIndex;
146 return collectedObj;
147 }
148
149 void draw(bool force = false);
150
151 void collectTextObject(const std::shared_ptr<TextObject> &textObj, CollectedPage &page);
152 void updateLinkedTexts();
153 bool hasUnfinishedLinkedTexts();
154
155 void drawLine(const std::shared_ptr<Line> &line, const CollectedPage &page);
156 void drawBox(const std::shared_ptr<Box> &box, const CollectedPage &page);
157 void drawRectangle(const std::shared_ptr<Box> &box, const CollectedPage &page);
158 void drawOval(const std::shared_ptr<Box> &oval, const CollectedPage &page);
159 void drawPolygon(const std::shared_ptr<Box> &polygon, const CollectedPage &page);
160 void drawBezierBox(const std::shared_ptr<Box> &box, const CollectedPage &page);
161 void drawTextBox(const std::shared_ptr<TextBox> &textbox, const CollectedPage &page);
162 void drawTextPath(const std::shared_ptr<TextPath> &textPath, const CollectedPage &page);
163 void drawText(const std::shared_ptr<Text> &text, const LinkedTextSettings &linkSettings);
164 void drawGroup(const std::shared_ptr<Group> &group, const CollectedPage &page);
165
166 void writeFill(librevenge::RVNGPropertyList &propList, const boost::optional<Fill> &fill);
167 void writeFrame(librevenge::RVNGPropertyList &propList, const Frame &frame, const bool runaround, const bool allowHairline = false);
168 };
169
170 }
171
172 #endif // QXPCONTENTCOLLECTOR_H_INCLUDED
173
174 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
libqxp::TextPathAlignment::BASELINE
libqxp::LineStyle::isStripe
bool isStripe
Definition: QXPTypes.h:129
libqxp::QXPContentCollector::drawTextPath
void drawTextPath(const std::shared_ptr< TextPath > &textPath, const CollectedPage &page)
Definition: QXPContentCollector.cpp:762
libqxp::Page
Definition: QXPTypes.h:545
libqxp::LineStyle::joinType
LineJoinType joinType
Definition: QXPTypes.h:133
libqxp::QXPContentCollector::m_docProps
QXPDocumentProperties m_docProps
Definition: QXPContentCollector.h:136
libqxp::QXPContentCollector
Definition: QXPContentCollector.h:26
libqxp::Frame::width
double width
Definition: QXPTypes.h:346
libqxp::QXPContentCollector::~QXPContentCollector
~QXPContentCollector()
Definition: QXPContentCollector.cpp:302
libqxp::QXPContentCollector::CollectedObject::zIndex
unsigned zIndex() const override
Definition: QXPContentCollector.h:92
libqxp::QXPDocumentProperties::superscriptHScale
double superscriptHScale
Definition: QXPTypes.h:576
libqxp::QXPContentCollector::collectGroup
void collectGroup(const std::shared_ptr< Group > &group) override
Definition: QXPContentCollector.cpp:392
libqxp::BoxType::POLYGON
QXP_ALMOST_ZERO
#define QXP_ALMOST_ZERO(m)
Definition: libqxp_utils.h:27
libqxp::QXPDocumentProperties::subscriptHScale
double subscriptHScale
Definition: QXPTypes.h:579
libqxp::QXPContentCollector::m_isDocumentStarted
bool m_isDocumentStarted
Definition: QXPContentCollector.h:127
libqxp::LineCapType::RECT
libqxp::QXPContentCollector::updateLinkedTexts
void updateLinkedTexts()
Definition: QXPContentCollector.cpp:496
libqxp::GradientType::CIRCULAR
libqxp::QXPContentCollector::drawRectangle
void drawRectangle(const std::shared_ptr< Box > &box, const CollectedPage &page)
Definition: QXPContentCollector.cpp:597
libqxp::QXPDocumentProperties::subscriptVScale
double subscriptVScale
Definition: QXPTypes.h:580
libqxp::QXPContentCollector::endDocument
void endDocument() override
Definition: QXPContentCollector.cpp:320
libqxp::QXPContentCollector::drawTextBox
void drawTextBox(const std::shared_ptr< TextBox > &textbox, const CollectedPage &page)
Definition: QXPContentCollector.cpp:712
libqxp
Definition: libqxp_utils.cpp:24
libqxp::BoxType::BEZIER
libqxp::QXPContentCollector::CollectedPage::linkedTextObjects
std::vector< std::shared_ptr< TextObject > > linkedTextObjects
Definition: QXPContentCollector.h:111
libqxp::HorizontalAlignment::RIGHT
libqxp::QXPContentCollector::m_unprocessedPages
std::vector< CollectedPage > m_unprocessedPages
Definition: QXPContentCollector.h:131
libqxp::QXPContentCollector::m_linkIndexedTextObjectsMap
std::unordered_map< unsigned, std::unordered_map< unsigned, std::shared_ptr< TextObject > > > m_linkIndexedTextObjectsMap
Definition: QXPContentCollector.h:134
libqxp::QXPContentCollector::collectTextBox
void collectTextBox(const std::shared_ptr< TextBox > &box) override
Definition: QXPContentCollector.cpp:368
libqxp::TextPathAlignment::DESCENT
libqxp::PageSettings::offset
Rect offset
Definition: QXPTypes.h:538
libqxp::QXPContentCollector::CollectedPage::getY
double getY(const double y) const
Definition: QXPContentCollector.cpp:1173
libqxp::QXPContentCollector::CollectedObject::draw
void draw(const CollectedPage &page) override
Definition: QXPContentCollector.h:83
libqxp::QXPContentCollector::collectLine
void collectLine(const std::shared_ptr< Line > &line) override
Definition: QXPContentCollector.cpp:358
libqxp::QXPContentCollector::CollectedPage
Definition: QXPContentCollector.h:107
libqxp::QXPContentCollector::drawOval
void drawOval(const std::shared_ptr< Box > &oval, const CollectedPage &page)
Definition: QXPContentCollector.cpp:632
libqxp::QXPDocumentProperties::superiorVScale
double superiorVScale
Definition: QXPTypes.h:582
libqxp::LineJoinType::MITER
libqxp::QXPContentCollector::CollectedPage::settings
const PageSettings settings
Definition: QXPContentCollector.h:109
libqxp::Frame
Definition: QXPTypes.h:344
libqxp::HorizontalAlignment::JUSTIFIED
libqxp::BoxType::RECTANGLE
QXPCollector.h
libqxp::QXPContentCollector::hasUnfinishedLinkedTexts
bool hasUnfinishedLinkedTexts()
Definition: QXPContentCollector.cpp:519
libqxp::QXPContentCollector::operator=
QXPContentCollector & operator=(const QXPContentCollector &other)=delete
libqxp::LineStyle::segmentLengths
std::vector< double > segmentLengths
Definition: QXPTypes.h:128
libqxp::Page::isFacing
bool isFacing() const
Definition: QXPTypes.h:554
libqxp::QXPContentCollector::collectTextPath
void collectTextPath(const std::shared_ptr< TextPath > &textPath) override
Definition: QXPContentCollector.cpp:380
libqxp::QXPContentCollector::m_linkTextMap
std::unordered_map< unsigned, std::shared_ptr< Text > > m_linkTextMap
Definition: QXPContentCollector.h:133
libqxp::BoxType::OVAL
libqxp::QXPContentCollector::CollectedPage::getX
double getX(const double x) const
Definition: QXPContentCollector.cpp:1163
libqxp::QXPDocumentProperties::superscriptVScale
double superscriptVScale
Definition: QXPTypes.h:577
libqxp::HorizontalAlignment::LEFT
libqxp::QXPDocumentProperties::autoLeading
double autoLeading() const
Definition: QXPTypes.cpp:171
libqxp::QXPCollector
Definition: QXPCollector.h:27
libqxp::Frame::endArrow
const Arrow * endArrow
Definition: QXPTypes.h:351
libqxp::QXPContentCollector::draw
void draw(bool force=false)
Definition: QXPContentCollector.cpp:425
libqxp::QXPContentCollector::ObjectHandlerMember
std::function< void(TThis *, const std::shared_ptr< T > &obj, const CollectedPage &)> ObjectHandlerMember
Definition: QXPContentCollector.h:59
libqxp::LinkedTextSettings::textLength
boost::optional< unsigned > textLength
Definition: QXPTypes.h:367
libqxp::TextPathAlignment::ASCENT
libqxp::LineCapType::BUTT
libqxp::QXPDocumentProperties::superscriptOffset
double superscriptOffset
Definition: QXPTypes.h:575
libqxp::QXPContentCollector::collectDocumentProperties
void collectDocumentProperties(const QXPDocumentProperties &props) override
Definition: QXPContentCollector.cpp:353
libqxp::QXPDocumentProperties::subscriptOffset
double subscriptOffset
Definition: QXPTypes.h:578
libqxp::HorizontalAlignment::FORCED
libqxp::QXPContentCollector::collectText
void collectText(const std::shared_ptr< Text > &text, const unsigned linkId) override
Definition: QXPContentCollector.cpp:399
libqxp::LineCapType::ROUND
libqxp::QXPContentCollector::drawBox
void drawBox(const std::shared_ptr< Box > &box, const CollectedPage &page)
Definition: QXPContentCollector.cpp:577
libqxp::Frame::startArrow
const Arrow * startArrow
Definition: QXPTypes.h:350
libqxp::normalizeDegAngle
double normalizeDegAngle(double degAngle)
Definition: libqxp_utils.cpp:390
libqxp::QXPDocumentProperties::isIncrementalAutoLeading
bool isIncrementalAutoLeading() const
Definition: QXPTypes.h:588
QXP_DEBUG_MSG
#define QXP_DEBUG_MSG(M)
Definition: libqxp_utils.h:53
libqxp::QXPDocumentProperties
Definition: QXPTypes.h:560
libqxp::PageSettings
Definition: QXPTypes.h:536
libqxp::GradientType::LINEAR
libqxp::QXPContentCollector::addObject
std::shared_ptr< CollectedObject< T > > addObject(const std::shared_ptr< T > &obj, const ObjectHandlerMember< T, QXPContentCollector > &handler)
Definition: QXPContentCollector.h:141
libqxp::QXPContentCollector::CollectedObjectInterface::~CollectedObjectInterface
virtual ~CollectedObjectInterface()=default
libqxp::QXPContentCollector::drawGroup
void drawGroup(const std::shared_ptr< Group > &group, const CollectedPage &page)
Definition: QXPContentCollector.cpp:1047
libqxp::LineStyle::endcapType
LineCapType endcapType
Definition: QXPTypes.h:132
libqxp::Page::pageSettings
std::vector< PageSettings > pageSettings
Definition: QXPTypes.h:547
libqxp::QXPContentCollector::CollectedPage::groups
std::vector< std::shared_ptr< CollectedObject< Group > > > groups
Definition: QXPContentCollector.h:110
libqxp::LineJoinType::BEVEL
libqxp::Frame::color
boost::optional< Color > color
Definition: QXPTypes.h:347
libqxp::QXPContentCollector::CollectedObjectInterface::draw
virtual void draw(const CollectedPage &page)=0
libqxp::Point::x
double x
Definition: QXPTypes.h:26
libqxp::TabStopType::ALIGN
libqxp::HorizontalAlignment::CENTER
libqxp::Point
Definition: QXPTypes.h:24
QXPTypes.h
libqxp::QXPContentCollector::getInsertionPage
CollectedPage & getInsertionPage(const std::shared_ptr< Object > &obj)
Definition: QXPContentCollector.cpp:416
libqxp::QXPContentCollector::drawText
void drawText(const std::shared_ptr< Text > &text, const LinkedTextSettings &linkSettings)
Definition: QXPContentCollector.cpp:831
libqxp::GradientType::RECTANGULAR
libqxp::QXPContentCollector::m_painter
librevenge::RVNGDrawingInterface * m_painter
Definition: QXPContentCollector.h:125
libqxp::QXPContentCollector::endPage
void endPage() override
Definition: QXPContentCollector.cpp:347
libqxp::QXPContentCollector::drawLine
void drawLine(const std::shared_ptr< Line > &line, const CollectedPage &page)
Definition: QXPContentCollector.cpp:535
libqxp::QXPContentCollector::startPage
void startPage(const Page &page) override
Definition: QXPContentCollector.cpp:336
libqxp::GradientType::FULLCIRCULAR
libqxp::LinkedTextSettings::offsetIntoText
unsigned offsetIntoText
Definition: QXPTypes.h:364
libqxp::QXPDocumentProperties::superiorHScale
double superiorHScale
Definition: QXPTypes.h:581
QXPContentCollector.h
libqxp::QXPContentCollector::startDocument
void startDocument() override
Definition: QXPContentCollector.cpp:310
libqxp::appendCharacters
void appendCharacters(librevenge::RVNGString &text, const char *characters, const size_t size, const char *encoding)
Definition: libqxp_utils.cpp:398
libqxp::QXPContentCollector::writeFrame
void writeFrame(librevenge::RVNGPropertyList &propList, const Frame &frame, const bool runaround, const bool allowHairline=false)
Definition: QXPContentCollector.cpp:1090
libqxp::QXPContentCollector::drawBezierBox
void drawBezierBox(const std::shared_ptr< Box > &box, const CollectedPage &page)
Definition: QXPContentCollector.cpp:682
libqxp::QXPContentCollector::CollectedObject::setZIndex
void setZIndex(unsigned value) override
Definition: QXPContentCollector.h:97
libqxp::QXPContentCollector::CollectedObject::m_handler
const ObjectHandler< T > m_handler
Definition: QXPContentCollector.h:103
libqxp::QXPContentCollector::collectBox
void collectBox(const std::shared_ptr< Box > &box) override
Definition: QXPContentCollector.cpp:363
libqxp::Rect::left
double left
Definition: QXPTypes.h:51
libqxp::GradientType::DIAMOND
libqxp::QXPContentCollector::collectTextObject
void collectTextObject(const std::shared_ptr< TextObject > &textObj, CollectedPage &page)
Definition: QXPContentCollector.cpp:474
libqxp::QXPContentCollector::CollectedObjectInterface::zIndex
virtual unsigned zIndex() const =0
libqxp::QXPContentCollector::CollectedPage::objects
std::map< unsigned, std::shared_ptr< CollectedObjectInterface > > objects
Definition: QXPContentCollector.h:112
libqxp::QXPContentCollector::CollectedObjectInterface::setZIndex
virtual void setZIndex(unsigned value)=0
libqxp::QXPContentCollector::m_isCollectingFacingPage
bool m_isCollectingFacingPage
Definition: QXPContentCollector.h:128
libqxp::Point::y
double y
Definition: QXPTypes.h:27
libqxp::QXPContentCollector::CollectedObject::CollectedObject
CollectedObject(const std::shared_ptr< T > &obj, const ObjectHandler< T > &handler)
Definition: QXPContentCollector.h:79
libqxp::QXPContentCollector::CollectedObjectInterface
Definition: QXPContentCollector.h:61
libqxp::QXPContentCollector::m_currentObjectIndex
unsigned m_currentObjectIndex
Definition: QXPContentCollector.h:129
libqxp::QXPContentCollector::QXPContentCollector
QXPContentCollector(const QXPContentCollector &other)=delete
libqxp::QXPContentCollector::drawPolygon
void drawPolygon(const std::shared_ptr< Box > &polygon, const CollectedPage &page)
Definition: QXPContentCollector.cpp:656
libqxp::QXPContentCollector::writeFill
void writeFill(librevenge::RVNGPropertyList &propList, const boost::optional< Fill > &fill)
Definition: QXPContentCollector.cpp:1079
libqxp::QXPContentCollector::CollectedObject::m_isProcessed
bool m_isProcessed
Definition: QXPContentCollector.h:104
libqxp::VerticalAlignment::BOTTOM
libqxp::QXPContentCollector::ObjectHandler
std::function< void(const std::shared_ptr< T > &obj, const CollectedPage &)> ObjectHandler
Definition: QXPContentCollector.h:56
libqxp::LineStyle::patternLength
double patternLength
Definition: QXPTypes.h:131
libqxp::LinkedTextSettings
Definition: QXPTypes.h:361
libqxp::QXPContentCollector::CollectedPage::getPoint
Point getPoint(const Point &p) const
Definition: QXPContentCollector.cpp:1183
libqxp::Point::rotateDeg
Point rotateDeg(double rotationDeg, const Point &center) const
Definition: QXPTypes.cpp:33
libqxp::LineStyle::isProportional
bool isProportional
Definition: QXPTypes.h:130
m_propList
librevenge::RVNGPropertyList & m_propList
Definition: QXPContentCollector.cpp:278
libqxp::QXPContentCollector::CollectedPage::CollectedPage
CollectedPage(const PageSettings &pageSettings)
Definition: QXPContentCollector.h:114
libqxp::QXPContentCollector::CollectedObject
Definition: QXPContentCollector.h:73
libqxp::Frame::lineStyle
const LineStyle * lineStyle
Definition: QXPTypes.h:349
libqxp::VerticalAlignment::TOP
libqxp::QXPContentCollector::CollectedObject::object
const std::shared_ptr< T > object
Definition: QXPContentCollector.h:75