xref: /AOO41X/main/sdext/source/pdfimport/tree/genericelements.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef INCLUDED_PDFI_GENERICELEMENTS_HXX
29*cdf0e10cSrcweir #define INCLUDED_PDFI_GENERICELEMENTS_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "pdfihelper.hxx"
32*cdf0e10cSrcweir #include "treevisiting.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <com/sun/star/task/XStatusIndicator.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
36*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygon.hxx>
37*cdf0e10cSrcweir #include <basegfx/range/b2drange.hxx>
38*cdf0e10cSrcweir #include <rtl/ustring.hxx>
39*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <list>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir namespace pdfi
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir     class XmlEmitter;
46*cdf0e10cSrcweir     class StyleContainer;
47*cdf0e10cSrcweir     class ImageContainer;
48*cdf0e10cSrcweir     class PDFIProcessor;
49*cdf0e10cSrcweir     class ElementFactory;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     struct EmitContext
53*cdf0e10cSrcweir     {
54*cdf0e10cSrcweir         EmitContext(
55*cdf0e10cSrcweir             XmlEmitter&                              _rEmitter,
56*cdf0e10cSrcweir             StyleContainer&                          _rStyles,
57*cdf0e10cSrcweir             ImageContainer&                          _rImages,
58*cdf0e10cSrcweir             PDFIProcessor&                           _rProcessor,
59*cdf0e10cSrcweir             const com::sun::star::uno::Reference<
60*cdf0e10cSrcweir             com::sun::star::task::XStatusIndicator>& _xStatusIndicator,
61*cdf0e10cSrcweir             com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >  xContext)
62*cdf0e10cSrcweir 		:
63*cdf0e10cSrcweir             rEmitter(_rEmitter),
64*cdf0e10cSrcweir             rStyles(_rStyles),
65*cdf0e10cSrcweir             rImages(_rImages),
66*cdf0e10cSrcweir             rProcessor(_rProcessor),
67*cdf0e10cSrcweir             xStatusIndicator(_xStatusIndicator),
68*cdf0e10cSrcweir 	    m_xContext(xContext)
69*cdf0e10cSrcweir         {}
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir         XmlEmitter&     rEmitter;
72*cdf0e10cSrcweir         StyleContainer& rStyles;
73*cdf0e10cSrcweir         ImageContainer& rImages;
74*cdf0e10cSrcweir         PDFIProcessor&  rProcessor;
75*cdf0e10cSrcweir         com::sun::star::uno::Reference<
76*cdf0e10cSrcweir             com::sun::star::task::XStatusIndicator> xStatusIndicator;
77*cdf0e10cSrcweir         com::sun::star::uno::Reference<
78*cdf0e10cSrcweir             com::sun::star::uno::XComponentContext >  m_xContext;
79*cdf0e10cSrcweir     };
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     struct Element : public ElementTreeVisitable
82*cdf0e10cSrcweir     {
83*cdf0e10cSrcweir     protected:
84*cdf0e10cSrcweir         Element( Element* pParent )
85*cdf0e10cSrcweir             : x( 0 ), y( 0 ), w( 0 ), h( 0 ), StyleId( -1 ), Parent( pParent )
86*cdf0e10cSrcweir         {
87*cdf0e10cSrcweir             if( pParent )
88*cdf0e10cSrcweir                 pParent->Children.push_back( this );
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     public:
92*cdf0e10cSrcweir         virtual ~Element();
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         /// Apply visitor to all children
95*cdf0e10cSrcweir         void applyToChildren( ElementTreeVisitor& );
96*cdf0e10cSrcweir         /// Union element geometry with given element
97*cdf0e10cSrcweir         void updateGeometryWith( const Element* pMergeFrom );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
100*cdf0e10cSrcweir         // xxx refac TODO: move code to visitor
101*cdf0e10cSrcweir         virtual void emitStructure( int nLevel );
102*cdf0e10cSrcweir #endif
103*cdf0e10cSrcweir         /** el must be a valid dereferencable iterator of el->Parent->Children
104*cdf0e10cSrcweir             pNewParent must not be NULL
105*cdf0e10cSrcweir         */
106*cdf0e10cSrcweir         static void setParent( std::list<Element*>::iterator& el, Element* pNewParent );
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir         double              x, y, w, h;
109*cdf0e10cSrcweir         sal_Int32           StyleId;
110*cdf0e10cSrcweir         Element*            Parent;
111*cdf0e10cSrcweir         std::list<Element*> Children;
112*cdf0e10cSrcweir     };
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     struct ListElement : public Element
115*cdf0e10cSrcweir     {
116*cdf0e10cSrcweir         ListElement() : Element( NULL ) {}
117*cdf0e10cSrcweir         // ElementTreeVisitable
118*cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
119*cdf0e10cSrcweir     };
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     struct HyperlinkElement : public Element
122*cdf0e10cSrcweir     {
123*cdf0e10cSrcweir         friend class ElementFactory;
124*cdf0e10cSrcweir     protected:
125*cdf0e10cSrcweir         HyperlinkElement( Element* pParent, const rtl::OUString& rURI )
126*cdf0e10cSrcweir         : Element( pParent ), URI( rURI ) {}
127*cdf0e10cSrcweir     public:
128*cdf0e10cSrcweir         // ElementTreeVisitable
129*cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         rtl::OUString URI;
132*cdf0e10cSrcweir     };
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     struct GraphicalElement : public Element
135*cdf0e10cSrcweir     {
136*cdf0e10cSrcweir     protected:
137*cdf0e10cSrcweir         GraphicalElement( Element* pParent, sal_Int32 nGCId )
138*cdf0e10cSrcweir         : Element( pParent ), GCId( nGCId ), MirrorVertical( false ) {}
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     public:
141*cdf0e10cSrcweir         sal_Int32 GCId;
142*cdf0e10cSrcweir         bool      MirrorVertical;
143*cdf0e10cSrcweir     };
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     struct DrawElement : public GraphicalElement
146*cdf0e10cSrcweir     {
147*cdf0e10cSrcweir     protected:
148*cdf0e10cSrcweir         DrawElement( Element* pParent, sal_Int32 nGCId )
149*cdf0e10cSrcweir         : GraphicalElement( pParent, nGCId ), isCharacter(false), ZOrder(0) {}
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     public:
152*cdf0e10cSrcweir         bool      isCharacter;
153*cdf0e10cSrcweir         sal_Int32 ZOrder;
154*cdf0e10cSrcweir     };
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     struct FrameElement : public DrawElement
157*cdf0e10cSrcweir     {
158*cdf0e10cSrcweir         friend class ElementFactory;
159*cdf0e10cSrcweir     protected:
160*cdf0e10cSrcweir         FrameElement( Element* pParent, sal_Int32 nGCId )
161*cdf0e10cSrcweir         : DrawElement( pParent, nGCId ) {}
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     public:
164*cdf0e10cSrcweir         // ElementTreeVisitable
165*cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
166*cdf0e10cSrcweir     };
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     struct TextElement : public GraphicalElement
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir         friend class ElementFactory;
171*cdf0e10cSrcweir     protected:
172*cdf0e10cSrcweir         TextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
173*cdf0e10cSrcweir         : GraphicalElement( pParent, nGCId ), FontId( nFontId ) {}
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     public:
176*cdf0e10cSrcweir         // ElementTreeVisitable
177*cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir         rtl::OUStringBuffer Text;
180*cdf0e10cSrcweir         sal_Int32           FontId;
181*cdf0e10cSrcweir     };
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir     struct ParagraphElement : public Element
184*cdf0e10cSrcweir     {
185*cdf0e10cSrcweir         friend class ElementFactory;
186*cdf0e10cSrcweir     protected:
187*cdf0e10cSrcweir         ParagraphElement( Element* pParent ) : Element( pParent ), Type( Normal ), bRtl( false ) {}
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir     public:
190*cdf0e10cSrcweir         // ElementTreeVisitable
191*cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& rParentIt );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir         // returns true only if only a single line is contained
194*cdf0e10cSrcweir         bool isSingleLined( PDFIProcessor& rProc ) const;
195*cdf0e10cSrcweir         // returns the highest line height of the contained textelements
196*cdf0e10cSrcweir         // line height is font height if the text element is itself multilined
197*cdf0e10cSrcweir         double getLineHeight( PDFIProcessor& rProc ) const;
198*cdf0e10cSrcweir         // returns the first text element child; does not recurse through subparagraphs
199*cdf0e10cSrcweir         TextElement* getFirstTextChild() const;
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir         enum ParagraphType { Normal, Headline };
202*cdf0e10cSrcweir         ParagraphType       Type;
203*cdf0e10cSrcweir 	bool bRtl;
204*cdf0e10cSrcweir     };
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     struct PolyPolyElement : public DrawElement
207*cdf0e10cSrcweir     {
208*cdf0e10cSrcweir         friend class ElementFactory;
209*cdf0e10cSrcweir     protected:
210*cdf0e10cSrcweir         PolyPolyElement( Element* pParent, sal_Int32 nGCId,
211*cdf0e10cSrcweir                          const basegfx::B2DPolyPolygon& rPolyPoly,
212*cdf0e10cSrcweir                          sal_Int8 nAction );
213*cdf0e10cSrcweir     public:
214*cdf0e10cSrcweir         // ElementTreeVisitable
215*cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& rParentIt );
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         void updateGeometry();
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
220*cdf0e10cSrcweir         virtual void emitStructure( int nLevel );
221*cdf0e10cSrcweir #endif
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir         basegfx::B2DPolyPolygon PolyPoly;
224*cdf0e10cSrcweir         sal_Int8                Action;
225*cdf0e10cSrcweir     };
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir     struct ImageElement : public DrawElement
228*cdf0e10cSrcweir     {
229*cdf0e10cSrcweir         friend class ElementFactory;
230*cdf0e10cSrcweir     protected:
231*cdf0e10cSrcweir         ImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
232*cdf0e10cSrcweir         : DrawElement( pParent, nGCId ), Image( nImage ) {}
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir     public:
235*cdf0e10cSrcweir         // ElementTreeVisitable
236*cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir         ImageId Image;
239*cdf0e10cSrcweir     };
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir     struct PageElement : public Element
242*cdf0e10cSrcweir     {
243*cdf0e10cSrcweir         friend class ElementFactory;
244*cdf0e10cSrcweir     protected:
245*cdf0e10cSrcweir         PageElement( Element* pParent, sal_Int32 nPageNr )
246*cdf0e10cSrcweir         : Element( pParent ), PageNumber( nPageNr ), Hyperlinks(),
247*cdf0e10cSrcweir         TopMargin( 0.0 ), BottomMargin( 0.0 ), LeftMargin( 0.0 ), RightMargin( 0.0 ),
248*cdf0e10cSrcweir         HeaderElement( NULL ), FooterElement( NULL )
249*cdf0e10cSrcweir         {}
250*cdf0e10cSrcweir     private:
251*cdf0e10cSrcweir         // helper method for resolveHyperlinks
252*cdf0e10cSrcweir         bool resolveHyperlink( std::list<Element*>::iterator link_it, std::list<Element*>& rElements );
253*cdf0e10cSrcweir         public:
254*cdf0e10cSrcweir         virtual ~PageElement();
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir         // ElementTreeVisitable
257*cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& rParentIt );
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir         void emitPageAnchoredElements( EmitContext& rEmitContext );
260*cdf0e10cSrcweir         static void updateParagraphGeometry( Element* pEle );
261*cdf0e10cSrcweir         void resolveHyperlinks();
262*cdf0e10cSrcweir         void resolveFontStyles( PDFIProcessor& rProc );
263*cdf0e10cSrcweir         void resolveUnderlines( PDFIProcessor& rProc );
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         sal_Int32      PageNumber;
266*cdf0e10cSrcweir         ListElement    Hyperlinks; // contains not yet realized links on this page
267*cdf0e10cSrcweir         double         TopMargin;
268*cdf0e10cSrcweir         double         BottomMargin;
269*cdf0e10cSrcweir         double         LeftMargin;
270*cdf0e10cSrcweir         double         RightMargin;
271*cdf0e10cSrcweir         Element*       HeaderElement;
272*cdf0e10cSrcweir         Element*       FooterElement;
273*cdf0e10cSrcweir     };
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir     struct DocumentElement : public Element
276*cdf0e10cSrcweir     {
277*cdf0e10cSrcweir         friend class ElementFactory;
278*cdf0e10cSrcweir     protected:
279*cdf0e10cSrcweir         DocumentElement() : Element( NULL ) {}
280*cdf0e10cSrcweir     public:
281*cdf0e10cSrcweir         virtual ~DocumentElement();
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir         // ElementTreeVisitable
284*cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir     };
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir     // this class is the differentiator of document types: it will create
289*cdf0e10cSrcweir     // Element objects with an optimize() method suitable for the document type
290*cdf0e10cSrcweir     class ElementFactory
291*cdf0e10cSrcweir     {
292*cdf0e10cSrcweir     public:
293*cdf0e10cSrcweir         ElementFactory() {}
294*cdf0e10cSrcweir         virtual ~ElementFactory();
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir         virtual HyperlinkElement* createHyperlinkElement( Element* pParent, const rtl::OUString& rURI )
297*cdf0e10cSrcweir         { return new HyperlinkElement( pParent, rURI ); }
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir         virtual TextElement* createTextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
300*cdf0e10cSrcweir         { return new TextElement( pParent, nGCId, nFontId ); }
301*cdf0e10cSrcweir         virtual ParagraphElement* createParagraphElement( Element* pParent )
302*cdf0e10cSrcweir         { return new ParagraphElement( pParent ); }
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir         virtual FrameElement* createFrameElement( Element* pParent, sal_Int32 nGCId )
305*cdf0e10cSrcweir         { return new FrameElement( pParent, nGCId ); }
306*cdf0e10cSrcweir         virtual PolyPolyElement*
307*cdf0e10cSrcweir             createPolyPolyElement( Element* pParent,
308*cdf0e10cSrcweir                                    sal_Int32 nGCId,
309*cdf0e10cSrcweir                                    const basegfx::B2DPolyPolygon& rPolyPoly,
310*cdf0e10cSrcweir                                    sal_Int8 nAction)
311*cdf0e10cSrcweir         { return new PolyPolyElement( pParent, nGCId, rPolyPoly, nAction ); }
312*cdf0e10cSrcweir         virtual ImageElement* createImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
313*cdf0e10cSrcweir         { return new ImageElement( pParent, nGCId, nImage ); }
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir         virtual PageElement* createPageElement( Element* pParent,
316*cdf0e10cSrcweir                                                 sal_Int32 nPageNr )
317*cdf0e10cSrcweir         { return new PageElement( pParent, nPageNr ); }
318*cdf0e10cSrcweir         virtual DocumentElement* createDocumentElement()
319*cdf0e10cSrcweir         { return new DocumentElement(); }
320*cdf0e10cSrcweir     };
321*cdf0e10cSrcweir }
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir #endif
324