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>
28#include <com/sun/star/i18n/BreakIterator.hpp>
30#include <rtl/ustring.hxx>
31#include <rtl/ustrbuf.hxx>
32
33#include <list>
34
35namespace pdfi
36{
37 class XmlEmitter;
38 class StyleContainer;
39 class ImageContainer;
40 class PDFIProcessor;
41 class ElementFactory;
42
43
45 {
47 XmlEmitter& _rEmitter,
48 StyleContainer& _rStyles,
49 ImageContainer& _rImages,
50 PDFIProcessor& _rProcessor,
51 const css::uno::Reference<
52 css::task::XStatusIndicator>& _xStatusIndicator,
53 css::uno::Reference< css::uno::XComponentContext > const & xContext)
54 :
55 rEmitter(_rEmitter),
56 rStyles(_rStyles),
57 rImages(_rImages),
58 rProcessor(_rProcessor),
59 xStatusIndicator(_xStatusIndicator),
60 m_xContext(xContext)
61 {}
62
67 css::uno::Reference<
68 css::task::XStatusIndicator> xStatusIndicator;
69 css::uno::Reference<
70 css::uno::XComponentContext > m_xContext;
71 };
72
73 struct Element
74 {
75 protected:
76 explicit Element( Element* pParent )
77 : x( 0 ), y( 0 ), w( 0 ), h( 0 ), StyleId( -1 ), Parent( pParent )
78 {
79 if( pParent )
80 pParent->Children.emplace_back( this );
81 }
82
83 public:
84 virtual ~Element();
85
90 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) = 0;
94 void updateGeometryWith( const Element* pMergeFrom );
95
97 virtual const TextElement* dynCastAsTextElement() const { return nullptr; }
98 virtual TextElement* dynCastAsTextElement() { return nullptr; }
99
100#if OSL_DEBUG_LEVEL > 0
101 // xxx refact TODO: move code to visitor
102 virtual void emitStructure( int nLevel );
103#endif
107 static void setParent( std::list<std::unique_ptr<Element>>::iterator const & el, Element* pNewParent );
108
109 double x, y, w, h;
110 sal_Int32 StyleId;
112 std::list<std::unique_ptr<Element>> Children;
113 };
114
115 struct ListElement final : public Element
116 {
117 ListElement() : Element( nullptr ) {}
118 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
119 };
120
121 struct HyperlinkElement final : public Element
122 {
123 friend class ElementFactory;
124 HyperlinkElement( Element* pParent, const OUString& rURI )
125 : Element( pParent ), URI( rURI ) {}
126 public:
127 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
128
129 OUString URI;
130 };
131
132 struct GraphicalElement : public Element
133 {
134 protected:
135 GraphicalElement(Element* pParent, sal_Int32 nGCId)
136 : Element(pParent)
137 , GCId(nGCId)
138 , MirrorVertical(false)
139 , IsForText(false)
140 , FontSize(0.0)
141 , TextStyleId(0)
142 {
143 }
144
145 public:
146 sal_Int32 GCId;
149 double FontSize;
150 sal_Int32 TextStyleId;
151 };
152
154 {
155 protected:
156 DrawElement( Element* pParent, sal_Int32 nGCId )
157 : GraphicalElement( pParent, nGCId ), isCharacter(false), ZOrder(0) {}
158
159 public:
161 sal_Int32 ZOrder;
162 };
163
164 struct FrameElement final : public DrawElement
165 {
166 friend class ElementFactory;
167 FrameElement( Element* pParent, sal_Int32 nGCId )
168 : DrawElement( pParent, nGCId ) {}
169
170 public:
171 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
172 };
173
174 struct TextElement final : public GraphicalElement
175 {
176 friend class ElementFactory;
177 TextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
178 : GraphicalElement( pParent, nGCId ), FontId( nFontId ) {}
179
180 public:
181 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
182
183 virtual const TextElement* dynCastAsTextElement() const override { return this; }
184 virtual TextElement* dynCastAsTextElement() override { return this; }
185
186 OUStringBuffer Text;
187 sal_Int32 FontId;
188 };
189
190 struct ParagraphElement final : public Element
191 {
192 friend class ElementFactory;
193 explicit ParagraphElement( Element* pParent ) : Element( pParent ), Type( Normal ), bRtl( false ) {}
194
195 public:
196 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
197
198 // returns true only if only a single line is contained
199 bool isSingleLined( PDFIProcessor const & rProc ) const;
200 // returns the highest line height of the contained textelements
201 // line height is font height if the text element is itself multilined
202 double getLineHeight( PDFIProcessor& rProc ) const;
203 // returns the first text element child; does not recurse through subparagraphs
205
208 bool bRtl;
209 };
210
211 struct PolyPolyElement final : public DrawElement
212 {
213 friend class ElementFactory;
214 PolyPolyElement( Element* pParent, sal_Int32 nGCId,
215 const basegfx::B2DPolyPolygon& rPolyPoly,
216 sal_Int8 nAction );
217 public:
218 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
219
220 void updateGeometry();
221
222#if OSL_DEBUG_LEVEL > 0
223 virtual void emitStructure( int nLevel ) override;
224#endif
225
228 };
229
230 struct ImageElement final : public DrawElement
231 {
232 friend class ElementFactory;
233 ImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
234 : DrawElement( pParent, nGCId ), Image( nImage ) {}
235
236 public:
237 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
238
240 };
241
242 struct PageElement final : public Element
243 {
244 friend class ElementFactory;
245 PageElement( Element* pParent, sal_Int32 nPageNr )
246 : Element( pParent ), PageNumber( nPageNr ), Hyperlinks(),
247 TopMargin( 0.0 ), BottomMargin( 0.0 ), LeftMargin( 0.0 ), RightMargin( 0.0 )
248 {}
249 private:
250 // helper method for resolveHyperlinks
251 bool resolveHyperlink( const std::list<std::unique_ptr<Element>>::iterator& link_it, std::list<std::unique_ptr<Element>>& rElements );
252 public:
253 virtual ~PageElement() override;
254
255 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
256
257 void resolveHyperlinks();
258 void resolveFontStyles( PDFIProcessor const & rProc );
259 void resolveUnderlines( PDFIProcessor const & rProc );
260
261 sal_Int32 PageNumber;
262 ListElement Hyperlinks; // contains not yet realized links on this page
263 double TopMargin;
267 std::unique_ptr<Element> HeaderElement;
268 std::unique_ptr<Element> FooterElement;
269 };
270
271 struct DocumentElement final : public Element
272 {
273 friend class ElementFactory;
274 public:
275 DocumentElement() : Element( nullptr ) {}
276 virtual ~DocumentElement() override;
277
278 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
279 };
280
281 // this class is the differentiator of document types: it will create
282 // Element objects with an optimize() method suitable for the document type
284 {
285 public:
286 ElementFactory() = delete;
287
288 static HyperlinkElement* createHyperlinkElement( Element* pParent, const OUString& rURI )
289 { return new HyperlinkElement( pParent, rURI ); }
290
291 static TextElement* createTextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
292 { return new TextElement( pParent, nGCId, nFontId ); }
294 { return new ParagraphElement( pParent ); }
295
296 static FrameElement* createFrameElement( Element* pParent, sal_Int32 nGCId )
297 { return new FrameElement( pParent, nGCId ); }
298 static PolyPolyElement*
300 sal_Int32 nGCId,
301 const basegfx::B2DPolyPolygon& rPolyPoly,
302 sal_Int8 nAction)
303 { return new PolyPolyElement( pParent, nGCId, rPolyPoly, nAction ); }
304 static ImageElement* createImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
305 { return new ImageElement( pParent, nGCId, nImage ); }
306
308 sal_Int32 nPageNr )
309 { return new PageElement( pParent, nPageNr ); }
310 static std::shared_ptr<DocumentElement> createDocumentElement()
311 { return std::make_shared<DocumentElement>(); }
312 };
313
314 bool isComplex(const css::uno::Reference<css::i18n::XBreakIterator>& rBreakIterator, TextElement* const pTextElem);
315}
316
317#endif
318
319/* 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
bool isComplex(const css::uno::Reference< css::i18n::XBreakIterator > &rBreakIterator, TextElement *const pTextElem)
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