LibreOffice Module sdext (master) 1
genericelements.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice 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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#ifndef INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_GENERICELEMENTS_HXX
21#define INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_GENERICELEMENTS_HXX
22
23#include "pdfihelper.hxx"
24#include "treevisiting.hxx"
25
26#include <com/sun/star/task/XStatusIndicator.hpp>
27#include <com/sun/star/uno/XComponentContext.hpp>
29#include <rtl/ustring.hxx>
30#include <rtl/ustrbuf.hxx>
31
32#include <list>
33
34namespace pdfi
35{
36 class XmlEmitter;
37 class StyleContainer;
38 class ImageContainer;
39 class PDFIProcessor;
40 class ElementFactory;
41
42
44 {
46 XmlEmitter& _rEmitter,
47 StyleContainer& _rStyles,
48 ImageContainer& _rImages,
49 PDFIProcessor& _rProcessor,
50 const css::uno::Reference<
51 css::task::XStatusIndicator>& _xStatusIndicator,
52 css::uno::Reference< css::uno::XComponentContext > const & xContext)
53 :
54 rEmitter(_rEmitter),
55 rStyles(_rStyles),
56 rImages(_rImages),
57 rProcessor(_rProcessor),
58 xStatusIndicator(_xStatusIndicator),
59 m_xContext(xContext)
60 {}
61
66 css::uno::Reference<
67 css::task::XStatusIndicator> xStatusIndicator;
68 css::uno::Reference<
69 css::uno::XComponentContext > m_xContext;
70 };
71
72 struct Element
73 {
74 protected:
75 explicit Element( Element* pParent )
76 : x( 0 ), y( 0 ), w( 0 ), h( 0 ), StyleId( -1 ), Parent( pParent )
77 {
78 if( pParent )
79 pParent->Children.emplace_back( this );
80 }
81
82 public:
83 virtual ~Element();
84
89 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) = 0;
93 void updateGeometryWith( const Element* pMergeFrom );
94
96 virtual const TextElement* dynCastAsTextElement() const { return nullptr; }
97 virtual TextElement* dynCastAsTextElement() { return nullptr; }
98
99#if OSL_DEBUG_LEVEL > 0
100 // xxx refact TODO: move code to visitor
101 virtual void emitStructure( int nLevel );
102#endif
106 static void setParent( std::list<std::unique_ptr<Element>>::iterator const & el, Element* pNewParent );
107
108 double x, y, w, h;
109 sal_Int32 StyleId;
111 std::list<std::unique_ptr<Element>> Children;
112 };
113
114 struct ListElement final : public Element
115 {
116 ListElement() : Element( nullptr ) {}
117 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
118 };
119
120 struct HyperlinkElement final : public Element
121 {
122 friend class ElementFactory;
123 HyperlinkElement( Element* pParent, const OUString& rURI )
124 : Element( pParent ), URI( rURI ) {}
125 public:
126 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
127
128 OUString URI;
129 };
130
131 struct GraphicalElement : public Element
132 {
133 protected:
134 GraphicalElement(Element* pParent, sal_Int32 nGCId)
135 : Element(pParent)
136 , GCId(nGCId)
137 , MirrorVertical(false)
138 , IsForText(false)
139 , FontSize(0.0)
140 , TextStyleId(0)
141 {
142 }
143
144 public:
145 sal_Int32 GCId;
148 double FontSize;
149 sal_Int32 TextStyleId;
150 };
151
153 {
154 protected:
155 DrawElement( Element* pParent, sal_Int32 nGCId )
156 : GraphicalElement( pParent, nGCId ), isCharacter(false), ZOrder(0) {}
157
158 public:
160 sal_Int32 ZOrder;
161 };
162
163 struct FrameElement final : public DrawElement
164 {
165 friend class ElementFactory;
166 FrameElement( Element* pParent, sal_Int32 nGCId )
167 : DrawElement( pParent, nGCId ) {}
168
169 public:
170 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
171 };
172
173 struct TextElement final : public GraphicalElement
174 {
175 friend class ElementFactory;
176 TextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
177 : GraphicalElement( pParent, nGCId ), FontId( nFontId ) {}
178
179 public:
180 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
181
182 virtual const TextElement* dynCastAsTextElement() const override { return this; }
183 virtual TextElement* dynCastAsTextElement() override { return this; }
184
185 OUStringBuffer Text;
186 sal_Int32 FontId;
187 };
188
189 struct ParagraphElement final : public Element
190 {
191 friend class ElementFactory;
192 explicit ParagraphElement( Element* pParent ) : Element( pParent ), Type( Normal ), bRtl( false ) {}
193
194 public:
195 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
196
197 // returns true only if only a single line is contained
198 bool isSingleLined( PDFIProcessor const & rProc ) const;
199 // returns the highest line height of the contained textelements
200 // line height is font height if the text element is itself multilined
201 double getLineHeight( PDFIProcessor& rProc ) const;
202 // returns the first text element child; does not recurse through subparagraphs
204
207 bool bRtl;
208 };
209
210 struct PolyPolyElement final : public DrawElement
211 {
212 friend class ElementFactory;
213 PolyPolyElement( Element* pParent, sal_Int32 nGCId,
214 const basegfx::B2DPolyPolygon& rPolyPoly,
215 sal_Int8 nAction );
216 public:
217 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
218
219 void updateGeometry();
220
221#if OSL_DEBUG_LEVEL > 0
222 virtual void emitStructure( int nLevel ) override;
223#endif
224
227 };
228
229 struct ImageElement final : public DrawElement
230 {
231 friend class ElementFactory;
232 ImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
233 : DrawElement( pParent, nGCId ), Image( nImage ) {}
234
235 public:
236 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
237
239 };
240
241 struct PageElement final : public Element
242 {
243 friend class ElementFactory;
244 PageElement( Element* pParent, sal_Int32 nPageNr )
245 : Element( pParent ), PageNumber( nPageNr ), Hyperlinks(),
246 TopMargin( 0.0 ), BottomMargin( 0.0 ), LeftMargin( 0.0 ), RightMargin( 0.0 )
247 {}
248 private:
249 // helper method for resolveHyperlinks
250 bool resolveHyperlink( const std::list<std::unique_ptr<Element>>::iterator& link_it, std::list<std::unique_ptr<Element>>& rElements );
251 public:
252 virtual ~PageElement() override;
253
254 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
255
256 void resolveHyperlinks();
257 void resolveFontStyles( PDFIProcessor const & rProc );
258 void resolveUnderlines( PDFIProcessor const & rProc );
259
260 sal_Int32 PageNumber;
261 ListElement Hyperlinks; // contains not yet realized links on this page
262 double TopMargin;
266 std::unique_ptr<Element> HeaderElement;
267 std::unique_ptr<Element> FooterElement;
268 };
269
270 struct DocumentElement final : public Element
271 {
272 friend class ElementFactory;
273 public:
274 DocumentElement() : Element( nullptr ) {}
275 virtual ~DocumentElement() override;
276
277 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
278 };
279
280 // this class is the differentiator of document types: it will create
281 // Element objects with an optimize() method suitable for the document type
283 {
284 public:
285 ElementFactory() = delete;
286
287 static HyperlinkElement* createHyperlinkElement( Element* pParent, const OUString& rURI )
288 { return new HyperlinkElement( pParent, rURI ); }
289
290 static TextElement* createTextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
291 { return new TextElement( pParent, nGCId, nFontId ); }
293 { return new ParagraphElement( pParent ); }
294
295 static FrameElement* createFrameElement( Element* pParent, sal_Int32 nGCId )
296 { return new FrameElement( pParent, nGCId ); }
297 static PolyPolyElement*
299 sal_Int32 nGCId,
300 const basegfx::B2DPolyPolygon& rPolyPoly,
301 sal_Int8 nAction)
302 { return new PolyPolyElement( pParent, nGCId, rPolyPoly, nAction ); }
303 static ImageElement* createImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
304 { return new ImageElement( pParent, nGCId, nImage ); }
305
307 sal_Int32 nPageNr )
308 { return new PageElement( pParent, nPageNr ); }
309 static std::shared_ptr<DocumentElement> createDocumentElement()
310 { return std::make_shared<DocumentElement>(); }
311 };
312}
313
314#endif
315
316/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static ParagraphElement * createParagraphElement(Element *pParent)
static FrameElement * createFrameElement(Element *pParent, sal_Int32 nGCId)
static PageElement * createPageElement(Element *pParent, sal_Int32 nPageNr)
static PolyPolyElement * createPolyPolyElement(Element *pParent, sal_Int32 nGCId, const basegfx::B2DPolyPolygon &rPolyPoly, sal_Int8 nAction)
static TextElement * createTextElement(Element *pParent, sal_Int32 nGCId, sal_Int32 nFontId)
static ImageElement * createImageElement(Element *pParent, sal_Int32 nGCId, ImageId nImage)
static HyperlinkElement * createHyperlinkElement(Element *pParent, const OUString &rURI)
static std::shared_ptr< DocumentElement > createDocumentElement()
Main entry from the parser.
Output interface to ODF.
Definition: xmlemitter.hxx:33
Type
sal_Int32 ImageId
Definition: pdfihelper.hxx:45
FontSize
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
virtual ~DocumentElement() override
DrawElement(Element *pParent, sal_Int32 nGCId)
To be visited by all tree element types.
Element(Element *pParent)
virtual const TextElement * dynCastAsTextElement() const
To avoid some dynamic_cast cost.
static void setParent(std::list< std::unique_ptr< Element > >::iterator const &el, Element *pNewParent)
el must be a valid dereferenceable iterator of el->Parent->Children pNewParent must not be NULL
std::list< std::unique_ptr< Element > > Children
virtual ~Element()
virtual void emitStructure(int nLevel)
virtual TextElement * dynCastAsTextElement()
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &rParentIt)=0
To be implemented by every tree node that needs to be visitable.
void applyToChildren(ElementTreeVisitor &)
Apply visitor to all children.
void updateGeometryWith(const Element *pMergeFrom)
Union element geometry with given element.
ImageContainer & rImages
css::uno::Reference< css::uno::XComponentContext > m_xContext
EmitContext(XmlEmitter &_rEmitter, StyleContainer &_rStyles, ImageContainer &_rImages, PDFIProcessor &_rProcessor, const css::uno::Reference< css::task::XStatusIndicator > &_xStatusIndicator, css::uno::Reference< css::uno::XComponentContext > const &xContext)
XmlEmitter & rEmitter
PDFIProcessor & rProcessor
css::uno::Reference< css::task::XStatusIndicator > xStatusIndicator
StyleContainer & rStyles
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
FrameElement(Element *pParent, sal_Int32 nGCId)
GraphicalElement(Element *pParent, sal_Int32 nGCId)
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
HyperlinkElement(Element *pParent, const OUString &rURI)
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
ImageElement(Element *pParent, sal_Int32 nGCId, ImageId nImage)
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
std::unique_ptr< Element > FooterElement
std::unique_ptr< Element > HeaderElement
void resolveUnderlines(PDFIProcessor const &rProc)
PageElement(Element *pParent, sal_Int32 nPageNr)
bool resolveHyperlink(const std::list< std::unique_ptr< Element > >::iterator &link_it, std::list< std::unique_ptr< Element > > &rElements)
virtual ~PageElement() override
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &rParentIt) override
To be implemented by every tree node that needs to be visitable.
void resolveFontStyles(PDFIProcessor const &rProc)
TextElement * getFirstTextChild() const
double getLineHeight(PDFIProcessor &rProc) const
ParagraphElement(Element *pParent)
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &rParentIt) override
To be implemented by every tree node that needs to be visitable.
bool isSingleLined(PDFIProcessor const &rProc) const
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &rParentIt) override
To be implemented by every tree node that needs to be visitable.
basegfx::B2DPolyPolygon PolyPoly
PolyPolyElement(Element *pParent, sal_Int32 nGCId, const basegfx::B2DPolyPolygon &rPolyPoly, sal_Int8 nAction)
virtual void emitStructure(int nLevel) override
TextElement(Element *pParent, sal_Int32 nGCId, sal_Int32 nFontId)
virtual TextElement * dynCastAsTextElement() override
virtual const TextElement * dynCastAsTextElement() const override
To avoid some dynamic_cast cost.
OUStringBuffer Text
virtual void visitedBy(ElementTreeVisitor &, const std::list< std::unique_ptr< Element > >::const_iterator &) override
To be implemented by every tree node that needs to be visitable.
signed char sal_Int8