vcl: allow for overriding the default PDF rendering resolution
[LibreOffice.git] / sdext / source / pdfimport / inc / genericelements.hxx
blob177533501f3f1fcea29d3e7a0d742982702e4cec
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_GENERICELEMENTS_HXX
21 #define INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_GENERICELEMENTS_HXX
23 #include "pdfihelper.hxx"
24 #include "treevisiting.hxx"
26 #include <com/sun/star/task/XStatusIndicator.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
28 #include <basegfx/polygon/b2dpolypolygon.hxx>
29 #include <rtl/ustring.hxx>
30 #include <rtl/ustrbuf.hxx>
32 #include <list>
34 namespace pdfi
36 class XmlEmitter;
37 class StyleContainer;
38 class ImageContainer;
39 class PDFIProcessor;
40 class ElementFactory;
43 struct EmitContext
45 EmitContext(
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)
54 rEmitter(_rEmitter),
55 rStyles(_rStyles),
56 rImages(_rImages),
57 rProcessor(_rProcessor),
58 xStatusIndicator(_xStatusIndicator),
59 m_xContext(xContext)
62 XmlEmitter& rEmitter;
63 StyleContainer& rStyles;
64 ImageContainer& rImages;
65 PDFIProcessor& rProcessor;
66 css::uno::Reference<
67 css::task::XStatusIndicator> xStatusIndicator;
68 css::uno::Reference<
69 css::uno::XComponentContext > m_xContext;
72 struct Element
74 protected:
75 explicit Element( Element* pParent )
76 : x( 0 ), y( 0 ), w( 0 ), h( 0 ), StyleId( -1 ), Parent( pParent )
78 if( pParent )
79 pParent->Children.emplace_back( this );
82 public:
83 virtual ~Element();
85 /**
86 To be implemented by every tree node that needs to be
87 visitable.
89 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) = 0;
90 /// Apply visitor to all children
91 void applyToChildren( ElementTreeVisitor& );
92 /// Union element geometry with given element
93 void updateGeometryWith( const Element* pMergeFrom );
95 #if OSL_DEBUG_LEVEL > 0
96 // xxx refact TODO: move code to visitor
97 virtual void emitStructure( int nLevel );
98 #endif
99 /** el must be a valid dereferenceable iterator of el->Parent->Children
100 pNewParent must not be NULL
102 static void setParent( std::list<std::unique_ptr<Element>>::iterator const & el, Element* pNewParent );
104 double x, y, w, h;
105 sal_Int32 StyleId;
106 Element* Parent;
107 std::list<std::unique_ptr<Element>> Children;
110 struct ListElement : public Element
112 ListElement() : Element( nullptr ) {}
113 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
116 struct HyperlinkElement : public Element
118 friend class ElementFactory;
119 protected:
120 HyperlinkElement( Element* pParent, const OUString& rURI )
121 : Element( pParent ), URI( rURI ) {}
122 public:
123 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
125 OUString const URI;
128 struct GraphicalElement : public Element
130 protected:
131 GraphicalElement(Element* pParent, sal_Int32 nGCId)
132 : Element(pParent)
133 , GCId(nGCId)
134 , MirrorVertical(false)
135 , IsForText(false)
136 , FontSize(0.0)
137 , TextStyleId(0)
141 public:
142 sal_Int32 GCId;
143 bool MirrorVertical;
144 bool IsForText;
145 double FontSize;
146 sal_Int32 TextStyleId;
149 struct DrawElement : public GraphicalElement
151 protected:
152 DrawElement( Element* pParent, sal_Int32 nGCId )
153 : GraphicalElement( pParent, nGCId ), isCharacter(false), ZOrder(0) {}
155 public:
156 bool isCharacter;
157 sal_Int32 ZOrder;
160 struct FrameElement : public DrawElement
162 friend class ElementFactory;
163 protected:
164 FrameElement( Element* pParent, sal_Int32 nGCId )
165 : DrawElement( pParent, nGCId ) {}
167 public:
168 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
171 struct TextElement : public GraphicalElement
173 friend class ElementFactory;
174 protected:
175 TextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
176 : GraphicalElement( pParent, nGCId ), FontId( nFontId ) {}
178 public:
179 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
181 OUStringBuffer Text;
182 sal_Int32 FontId;
185 struct ParagraphElement : public Element
187 friend class ElementFactory;
188 protected:
189 explicit ParagraphElement( Element* pParent ) : Element( pParent ), Type( Normal ), bRtl( false ) {}
191 public:
192 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
194 // returns true only if only a single line is contained
195 bool isSingleLined( PDFIProcessor const & rProc ) const;
196 // returns the highest line height of the contained textelements
197 // line height is font height if the text element is itself multilined
198 double getLineHeight( PDFIProcessor& rProc ) const;
199 // returns the first text element child; does not recurse through subparagraphs
200 TextElement* getFirstTextChild() const;
202 enum ParagraphType { Normal, Headline };
203 ParagraphType Type;
204 bool bRtl;
207 struct PolyPolyElement : public DrawElement
209 friend class ElementFactory;
210 protected:
211 PolyPolyElement( Element* pParent, sal_Int32 nGCId,
212 const basegfx::B2DPolyPolygon& rPolyPoly,
213 sal_Int8 nAction );
214 public:
215 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
217 void updateGeometry();
219 #if OSL_DEBUG_LEVEL > 0
220 virtual void emitStructure( int nLevel ) override;
221 #endif
223 basegfx::B2DPolyPolygon PolyPoly;
224 sal_Int8 Action;
227 struct ImageElement : public DrawElement
229 friend class ElementFactory;
230 protected:
231 ImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
232 : DrawElement( pParent, nGCId ), Image( nImage ) {}
234 public:
235 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
237 ImageId const Image;
240 struct PageElement : public Element
242 friend class ElementFactory;
243 protected:
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 )
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;
254 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt ) override;
256 void resolveHyperlinks();
257 void resolveFontStyles( PDFIProcessor const & rProc );
258 void resolveUnderlines( PDFIProcessor const & rProc );
260 sal_Int32 const PageNumber;
261 ListElement Hyperlinks; // contains not yet realized links on this page
262 double TopMargin;
263 double BottomMargin;
264 double LeftMargin;
265 double RightMargin;
266 std::unique_ptr<Element> HeaderElement;
267 std::unique_ptr<Element> FooterElement;
270 struct DocumentElement : public Element
272 friend class ElementFactory;
273 protected:
274 DocumentElement() : Element( nullptr ) {}
275 public:
276 virtual ~DocumentElement() override;
278 virtual void visitedBy( ElementTreeVisitor&, const std::list< std::unique_ptr<Element> >::const_iterator& ) override;
281 // this class is the differentiator of document types: it will create
282 // Element objects with an optimize() method suitable for the document type
283 class ElementFactory
285 public:
286 ElementFactory() = delete;
288 static HyperlinkElement* createHyperlinkElement( Element* pParent, const OUString& rURI )
289 { return new HyperlinkElement( pParent, rURI ); }
291 static TextElement* createTextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
292 { return new TextElement( pParent, nGCId, nFontId ); }
293 static ParagraphElement* createParagraphElement( Element* pParent )
294 { return new ParagraphElement( pParent ); }
296 static FrameElement* createFrameElement( Element* pParent, sal_Int32 nGCId )
297 { return new FrameElement( pParent, nGCId ); }
298 static PolyPolyElement*
299 createPolyPolyElement( Element* pParent,
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 ); }
307 static PageElement* createPageElement( Element* pParent,
308 sal_Int32 nPageNr )
309 { return new PageElement( pParent, nPageNr ); }
310 static DocumentElement* createDocumentElement()
311 { return new DocumentElement(); }
315 #endif
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */