QXPContentCollector.h
Go to the documentation of this file.
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* This file is part of the libqxp project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef QXPCONTENTCOLLECTOR_H_INCLUDED
#define QXPCONTENTCOLLECTOR_H_INCLUDED
#include "QXPCollector.h"
#include <vector>
#include <unordered_map>
#include <map>
#include <memory>
#include <functional>
#include <type_traits>
#include "QXPTypes.h"
namespace libqxp
{
class QXPContentCollector : public QXPCollector
{
// disable copying
QXPContentCollector(const QXPContentCollector &other) = delete;
QXPContentCollector &operator=(const QXPContentCollector &other) = delete;
public:
QXPContentCollector(librevenge::RVNGDrawingInterface *painter);
~QXPContentCollector();
void startDocument() override;
void endDocument() override;
void startPage(const Page &page) override;
void endPage() override;
void collectDocumentProperties(const QXPDocumentProperties &props) override;
void collectLine(const std::shared_ptr<Line> &line) override;
void collectBox(const std::shared_ptr<Box> &box) override;
void collectTextBox(const std::shared_ptr<TextBox> &box) override;
void collectTextPath(const std::shared_ptr<TextPath> &textPath) override;
void collectGroup(const std::shared_ptr<Group> &group) override;
void collectText(const std::shared_ptr<Text> &text, const unsigned linkId) override;
private:
struct CollectedPage;
template<typename T>
using ObjectHandler = std::function<void(const std::shared_ptr<T> &obj, const CollectedPage &)>;
template<typename T, typename TThis>
using ObjectHandlerMember = std::function<void(TThis *, const std::shared_ptr<T> &obj, const CollectedPage &)>;
class CollectedObjectInterface
{
public:
virtual ~CollectedObjectInterface() = default;
virtual void draw(const CollectedPage &page) = 0;
virtual unsigned zIndex() const = 0;
virtual void setZIndex(unsigned value) = 0;
};
template<typename T>
class CollectedObject : public CollectedObjectInterface
{
static_assert(std::is_base_of<Object, T>::value, "T is not Object");
public:
const std::shared_ptr<T> object;
CollectedObject(const std::shared_ptr<T> &obj, const ObjectHandler<T> &handler)
: object(obj), m_handler(handler), m_isProcessed(false)
{ }
void draw(const CollectedPage &page) override
{
if (!m_isProcessed)
{
m_isProcessed = true;
m_handler(object, page);
}
}
unsigned zIndex() const override
{
return object->zIndex;
}
void setZIndex(unsigned value) override
{
object->zIndex = value;
}
private:
const ObjectHandler<T> m_handler;
bool m_isProcessed;
};
struct CollectedPage
{
const PageSettings settings;
std::vector<std::shared_ptr<CollectedObject<Group>>> groups;
std::vector<std::shared_ptr<TextObject>> linkedTextObjects;
std::map<unsigned, std::shared_ptr<CollectedObjectInterface>> objects;
CollectedPage(const PageSettings &pageSettings)
: settings(pageSettings), groups(), linkedTextObjects(), objects()
{ }
double getX(const double x) const;
double getX(const std::shared_ptr<Object> &obj) const;
double getY(const double y) const;
double getY(const std::shared_ptr<Object> &obj) const;
Point getPoint(const Point &p) const;
};
librevenge::RVNGDrawingInterface *m_painter;
bool m_isDocumentStarted;
bool m_isCollectingFacingPage;
unsigned m_currentObjectIndex;
std::vector<CollectedPage> m_unprocessedPages;
std::unordered_map<unsigned, std::shared_ptr<Text>> m_linkTextMap;
std::unordered_map<unsigned, std::unordered_map<unsigned, std::shared_ptr<TextObject>>> m_linkIndexedTextObjectsMap;
QXPDocumentProperties m_docProps;
CollectedPage &getInsertionPage(const std::shared_ptr<Object> &obj);
template<typename T>
std::shared_ptr<CollectedObject<T>> addObject(const std::shared_ptr<T> &obj, const ObjectHandlerMember<T, QXPContentCollector> &handler)
{
auto collectedObj = std::make_shared<CollectedObject<T>>(obj, std::bind(handler, this, std::placeholders::_1, std::placeholders::_2));
getInsertionPage(obj).objects[m_currentObjectIndex] = collectedObj;
++m_currentObjectIndex;
return collectedObj;
}
void draw(bool force = false);
void collectTextObject(const std::shared_ptr<TextObject> &textObj, CollectedPage &page);
void updateLinkedTexts();
bool hasUnfinishedLinkedTexts();
void drawLine(const std::shared_ptr<Line> &line, const CollectedPage &page);
void drawBox(const std::shared_ptr<Box> &box, const CollectedPage &page);
void drawRectangle(const std::shared_ptr<Box> &box, const CollectedPage &page);
void drawOval(const std::shared_ptr<Box> &oval, const CollectedPage &page);
void drawPolygon(const std::shared_ptr<Box> &polygon, const CollectedPage &page);
void drawBezierBox(const std::shared_ptr<Box> &box, const CollectedPage &page);
void drawTextBox(const std::shared_ptr<TextBox> &textbox, const CollectedPage &page);
void drawTextPath(const std::shared_ptr<TextPath> &textPath, const CollectedPage &page);
void drawText(const std::shared_ptr<Text> &text, const LinkedTextSettings &linkSettings);
void drawGroup(const std::shared_ptr<Group> &group, const CollectedPage &page);
void writeFill(librevenge::RVNGPropertyList &propList, const boost::optional<Fill> &fill);
void writeFrame(librevenge::RVNGPropertyList &propList, const Frame &frame, const bool runaround, const bool allowHairline = false);
};
}
#endif // QXPCONTENTCOLLECTOR_H_INCLUDED
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
QXPCollector.h
QXPTypes.h
libqxp::QXPCollector::QXPCollector
QXPCollector(const QXPCollector &other)=delete
libqxp::QXPContentCollector::CollectedObjectInterface
Definition QXPContentCollector.h:62
libqxp::QXPContentCollector::CollectedObjectInterface::zIndex
virtual unsigned zIndex() const =0
libqxp::QXPContentCollector::CollectedObjectInterface::setZIndex
virtual void setZIndex(unsigned value)=0
libqxp::QXPContentCollector::CollectedObjectInterface::~CollectedObjectInterface
virtual ~CollectedObjectInterface()=default
libqxp::QXPContentCollector::CollectedObjectInterface::draw
virtual void draw(const CollectedPage &page)=0
libqxp::QXPContentCollector::CollectedObject::m_isProcessed
bool m_isProcessed
Definition QXPContentCollector.h:104
libqxp::QXPContentCollector::CollectedObject::draw
void draw(const CollectedPage &page) override
Definition QXPContentCollector.h:83
libqxp::QXPContentCollector::CollectedObject::setZIndex
void setZIndex(unsigned value) override
Definition QXPContentCollector.h:97
libqxp::QXPContentCollector::CollectedObject::zIndex
unsigned zIndex() const override
Definition QXPContentCollector.h:92
libqxp::QXPContentCollector::CollectedObject::CollectedObject
CollectedObject(const std::shared_ptr< T > &obj, const ObjectHandler< T > &handler)
Definition QXPContentCollector.h:79
libqxp::QXPContentCollector::CollectedObject::object
const std::shared_ptr< T > object
Definition QXPContentCollector.h:77
libqxp::QXPContentCollector::CollectedObject::m_handler
const ObjectHandler< T > m_handler
Definition QXPContentCollector.h:103
libqxp::QXPContentCollector::endDocument
void endDocument() override
Definition QXPContentCollector.cpp:320
libqxp::QXPContentCollector::m_isCollectingFacingPage
bool m_isCollectingFacingPage
Definition QXPContentCollector.h:128
libqxp::QXPContentCollector::m_unprocessedPages
std::vector< CollectedPage > m_unprocessedPages
Definition QXPContentCollector.h:131
libqxp::QXPContentCollector::m_painter
librevenge::RVNGDrawingInterface * m_painter
Definition QXPContentCollector.h:125
libqxp::QXPContentCollector::drawOval
void drawOval(const std::shared_ptr< Box > &oval, const CollectedPage &page)
Definition QXPContentCollector.cpp:632
libqxp::QXPContentCollector::collectTextObject
void collectTextObject(const std::shared_ptr< TextObject > &textObj, CollectedPage &page)
Definition QXPContentCollector.cpp:474
libqxp::QXPContentCollector::drawBezierBox
void drawBezierBox(const std::shared_ptr< Box > &box, const CollectedPage &page)
Definition QXPContentCollector.cpp:682
libqxp::QXPContentCollector::drawText
void drawText(const std::shared_ptr< Text > &text, const LinkedTextSettings &linkSettings)
Definition QXPContentCollector.cpp:831
libqxp::QXPContentCollector::startPage
void startPage(const Page &page) override
Definition QXPContentCollector.cpp:336
libqxp::QXPContentCollector::ObjectHandler
std::function< void(const std::shared_ptr< T > &obj, const CollectedPage &)> ObjectHandler
Definition QXPContentCollector.h:56
libqxp::QXPContentCollector::~QXPContentCollector
~QXPContentCollector()
Definition QXPContentCollector.cpp:302
libqxp::QXPContentCollector::m_docProps
QXPDocumentProperties m_docProps
Definition QXPContentCollector.h:136
libqxp::QXPContentCollector::drawRectangle
void drawRectangle(const std::shared_ptr< Box > &box, const CollectedPage &page)
Definition QXPContentCollector.cpp:597
libqxp::QXPContentCollector::getInsertionPage
CollectedPage & getInsertionPage(const std::shared_ptr< Object > &obj)
Definition QXPContentCollector.cpp:416
libqxp::QXPContentCollector::ObjectHandlerMember
std::function< void(TThis *, const std::shared_ptr< T > &obj, const CollectedPage &)> ObjectHandlerMember
Definition QXPContentCollector.h:59
libqxp::QXPContentCollector::drawGroup
void drawGroup(const std::shared_ptr< Group > &group, const CollectedPage &page)
Definition QXPContentCollector.cpp:1047
libqxp::QXPContentCollector::updateLinkedTexts
void updateLinkedTexts()
Definition QXPContentCollector.cpp:496
libqxp::QXPContentCollector::drawLine
void drawLine(const std::shared_ptr< Line > &line, const CollectedPage &page)
Definition QXPContentCollector.cpp:535
libqxp::QXPContentCollector::writeFill
void writeFill(librevenge::RVNGPropertyList &propList, const boost::optional< Fill > &fill)
Definition QXPContentCollector.cpp:1079
libqxp::QXPContentCollector::writeFrame
void writeFrame(librevenge::RVNGPropertyList &propList, const Frame &frame, const bool runaround, const bool allowHairline=false)
Definition QXPContentCollector.cpp:1090
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::drawTextPath
void drawTextPath(const std::shared_ptr< TextPath > &textPath, const CollectedPage &page)
Definition QXPContentCollector.cpp:762
libqxp::QXPContentCollector::collectText
void collectText(const std::shared_ptr< Text > &text, const unsigned linkId) override
Definition QXPContentCollector.cpp:399
libqxp::QXPContentCollector::operator=
QXPContentCollector & operator=(const QXPContentCollector &other)=delete
libqxp::QXPContentCollector::drawBox
void drawBox(const std::shared_ptr< Box > &box, const CollectedPage &page)
Definition QXPContentCollector.cpp:577
libqxp::QXPContentCollector::collectLine
void collectLine(const std::shared_ptr< Line > &line) override
Definition QXPContentCollector.cpp:358
libqxp::QXPContentCollector::draw
void draw(bool force=false)
Definition QXPContentCollector.cpp:425
libqxp::QXPContentCollector::m_currentObjectIndex
unsigned m_currentObjectIndex
Definition QXPContentCollector.h:129
libqxp::QXPContentCollector::drawTextBox
void drawTextBox(const std::shared_ptr< TextBox > &textbox, const CollectedPage &page)
Definition QXPContentCollector.cpp:712
libqxp::QXPContentCollector::m_linkTextMap
std::unordered_map< unsigned, std::shared_ptr< Text > > m_linkTextMap
Definition QXPContentCollector.h:133
libqxp::QXPContentCollector::hasUnfinishedLinkedTexts
bool hasUnfinishedLinkedTexts()
Definition QXPContentCollector.cpp:519
libqxp::QXPContentCollector::collectGroup
void collectGroup(const std::shared_ptr< Group > &group) override
Definition QXPContentCollector.cpp:392
libqxp::QXPContentCollector::drawPolygon
void drawPolygon(const std::shared_ptr< Box > &polygon, const CollectedPage &page)
Definition QXPContentCollector.cpp:656
libqxp::QXPContentCollector::m_isDocumentStarted
bool m_isDocumentStarted
Definition QXPContentCollector.h:127
libqxp::QXPContentCollector::collectTextPath
void collectTextPath(const std::shared_ptr< TextPath > &textPath) override
Definition QXPContentCollector.cpp:380
libqxp::QXPContentCollector::collectDocumentProperties
void collectDocumentProperties(const QXPDocumentProperties &props) override
Definition QXPContentCollector.cpp:353
libqxp::QXPContentCollector::m_linkIndexedTextObjectsMap
std::unordered_map< unsigned, std::unordered_map< unsigned, std::shared_ptr< TextObject > > > m_linkIndexedTextObjectsMap
Definition QXPContentCollector.h:134
libqxp::QXPContentCollector::endPage
void endPage() override
Definition QXPContentCollector.cpp:347
libqxp::QXPContentCollector::QXPContentCollector
QXPContentCollector(const QXPContentCollector &other)=delete
libqxp::QXPContentCollector::startDocument
void startDocument() override
Definition QXPContentCollector.cpp:310
libqxp::QXPContentCollector::collectTextBox
void collectTextBox(const std::shared_ptr< TextBox > &box) override
Definition QXPContentCollector.cpp:368
libqxp::QXPContentCollector::collectBox
void collectBox(const std::shared_ptr< Box > &box) override
Definition QXPContentCollector.cpp:363
libqxp
Definition libqxp_utils.cpp:25
libqxp::Frame
Definition QXPTypes.h:345
libqxp::LinkedTextSettings
Definition QXPTypes.h:362
libqxp::PageSettings
Definition QXPTypes.h:537
libqxp::Page
Definition QXPTypes.h:546
libqxp::Point
Definition QXPTypes.h:25
libqxp::QXPContentCollector::CollectedPage
Definition QXPContentCollector.h:108
libqxp::QXPContentCollector::CollectedPage::getX
double getX(const double x) const
Definition QXPContentCollector.cpp:1163
libqxp::QXPContentCollector::CollectedPage::objects
std::map< unsigned, std::shared_ptr< CollectedObjectInterface > > objects
Definition QXPContentCollector.h:112
libqxp::QXPContentCollector::CollectedPage::settings
const PageSettings settings
Definition QXPContentCollector.h:109
libqxp::QXPContentCollector::CollectedPage::getY
double getY(const double y) const
Definition QXPContentCollector.cpp:1173
libqxp::QXPContentCollector::CollectedPage::groups
std::vector< std::shared_ptr< CollectedObject< Group > > > groups
Definition QXPContentCollector.h:110
libqxp::QXPContentCollector::CollectedPage::getPoint
Point getPoint(const Point &p) const
Definition QXPContentCollector.cpp:1183
libqxp::QXPContentCollector::CollectedPage::CollectedPage
CollectedPage(const PageSettings &pageSettings)
Definition QXPContentCollector.h:114
libqxp::QXPContentCollector::CollectedPage::linkedTextObjects
std::vector< std::shared_ptr< TextObject > > linkedTextObjects
Definition QXPContentCollector.h:111
libqxp::QXPDocumentProperties
Definition QXPTypes.h:561