1*06bcd5d2SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*06bcd5d2SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*06bcd5d2SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*06bcd5d2SAndrew Rist  * distributed with this work for additional information
6*06bcd5d2SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*06bcd5d2SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*06bcd5d2SAndrew Rist  * "License"); you may not use this file except in compliance
9*06bcd5d2SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*06bcd5d2SAndrew Rist  *
11*06bcd5d2SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*06bcd5d2SAndrew Rist  *
13*06bcd5d2SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*06bcd5d2SAndrew Rist  * software distributed under the License is distributed on an
15*06bcd5d2SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*06bcd5d2SAndrew Rist  * KIND, either express or implied.  See the License for the
17*06bcd5d2SAndrew Rist  * specific language governing permissions and limitations
18*06bcd5d2SAndrew Rist  * under the License.
19*06bcd5d2SAndrew Rist  *
20*06bcd5d2SAndrew Rist  *************************************************************/
21*06bcd5d2SAndrew Rist 
22*06bcd5d2SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef INCLUDED_PDFI_GENERICELEMENTS_HXX
25cdf0e10cSrcweir #define INCLUDED_PDFI_GENERICELEMENTS_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "pdfihelper.hxx"
28cdf0e10cSrcweir #include "treevisiting.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <com/sun/star/task/XStatusIndicator.hpp>
31cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
32cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygon.hxx>
33cdf0e10cSrcweir #include <basegfx/range/b2drange.hxx>
34cdf0e10cSrcweir #include <rtl/ustring.hxx>
35cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <list>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace pdfi
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     class XmlEmitter;
42cdf0e10cSrcweir     class StyleContainer;
43cdf0e10cSrcweir     class ImageContainer;
44cdf0e10cSrcweir     class PDFIProcessor;
45cdf0e10cSrcweir     class ElementFactory;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     struct EmitContext
49cdf0e10cSrcweir     {
EmitContextpdfi::EmitContext50cdf0e10cSrcweir         EmitContext(
51cdf0e10cSrcweir             XmlEmitter&                              _rEmitter,
52cdf0e10cSrcweir             StyleContainer&                          _rStyles,
53cdf0e10cSrcweir             ImageContainer&                          _rImages,
54cdf0e10cSrcweir             PDFIProcessor&                           _rProcessor,
55cdf0e10cSrcweir             const com::sun::star::uno::Reference<
56cdf0e10cSrcweir             com::sun::star::task::XStatusIndicator>& _xStatusIndicator,
57cdf0e10cSrcweir             com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >  xContext)
58cdf0e10cSrcweir 		:
59cdf0e10cSrcweir             rEmitter(_rEmitter),
60cdf0e10cSrcweir             rStyles(_rStyles),
61cdf0e10cSrcweir             rImages(_rImages),
62cdf0e10cSrcweir             rProcessor(_rProcessor),
63cdf0e10cSrcweir             xStatusIndicator(_xStatusIndicator),
64cdf0e10cSrcweir 	    m_xContext(xContext)
65cdf0e10cSrcweir         {}
66cdf0e10cSrcweir 
67cdf0e10cSrcweir         XmlEmitter&     rEmitter;
68cdf0e10cSrcweir         StyleContainer& rStyles;
69cdf0e10cSrcweir         ImageContainer& rImages;
70cdf0e10cSrcweir         PDFIProcessor&  rProcessor;
71cdf0e10cSrcweir         com::sun::star::uno::Reference<
72cdf0e10cSrcweir             com::sun::star::task::XStatusIndicator> xStatusIndicator;
73cdf0e10cSrcweir         com::sun::star::uno::Reference<
74cdf0e10cSrcweir             com::sun::star::uno::XComponentContext >  m_xContext;
75cdf0e10cSrcweir     };
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     struct Element : public ElementTreeVisitable
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir     protected:
Elementpdfi::Element80cdf0e10cSrcweir         Element( Element* pParent )
81cdf0e10cSrcweir             : x( 0 ), y( 0 ), w( 0 ), h( 0 ), StyleId( -1 ), Parent( pParent )
82cdf0e10cSrcweir         {
83cdf0e10cSrcweir             if( pParent )
84cdf0e10cSrcweir                 pParent->Children.push_back( this );
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     public:
88cdf0e10cSrcweir         virtual ~Element();
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         /// Apply visitor to all children
91cdf0e10cSrcweir         void applyToChildren( ElementTreeVisitor& );
92cdf0e10cSrcweir         /// Union element geometry with given element
93cdf0e10cSrcweir         void updateGeometryWith( const Element* pMergeFrom );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
96cdf0e10cSrcweir         // xxx refac TODO: move code to visitor
97cdf0e10cSrcweir         virtual void emitStructure( int nLevel );
98cdf0e10cSrcweir #endif
99cdf0e10cSrcweir         /** el must be a valid dereferencable iterator of el->Parent->Children
100cdf0e10cSrcweir             pNewParent must not be NULL
101cdf0e10cSrcweir         */
102cdf0e10cSrcweir         static void setParent( std::list<Element*>::iterator& el, Element* pNewParent );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         double              x, y, w, h;
105cdf0e10cSrcweir         sal_Int32           StyleId;
106cdf0e10cSrcweir         Element*            Parent;
107cdf0e10cSrcweir         std::list<Element*> Children;
108cdf0e10cSrcweir     };
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     struct ListElement : public Element
111cdf0e10cSrcweir     {
ListElementpdfi::ListElement112cdf0e10cSrcweir         ListElement() : Element( NULL ) {}
113cdf0e10cSrcweir         // ElementTreeVisitable
114cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
115cdf0e10cSrcweir     };
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     struct HyperlinkElement : public Element
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         friend class ElementFactory;
120cdf0e10cSrcweir     protected:
HyperlinkElementpdfi::HyperlinkElement121cdf0e10cSrcweir         HyperlinkElement( Element* pParent, const rtl::OUString& rURI )
122cdf0e10cSrcweir         : Element( pParent ), URI( rURI ) {}
123cdf0e10cSrcweir     public:
124cdf0e10cSrcweir         // ElementTreeVisitable
125cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         rtl::OUString URI;
128cdf0e10cSrcweir     };
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     struct GraphicalElement : public Element
131cdf0e10cSrcweir     {
132cdf0e10cSrcweir     protected:
GraphicalElementpdfi::GraphicalElement133cdf0e10cSrcweir         GraphicalElement( Element* pParent, sal_Int32 nGCId )
134cdf0e10cSrcweir         : Element( pParent ), GCId( nGCId ), MirrorVertical( false ) {}
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     public:
137cdf0e10cSrcweir         sal_Int32 GCId;
138cdf0e10cSrcweir         bool      MirrorVertical;
139cdf0e10cSrcweir     };
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     struct DrawElement : public GraphicalElement
142cdf0e10cSrcweir     {
143cdf0e10cSrcweir     protected:
DrawElementpdfi::DrawElement144cdf0e10cSrcweir         DrawElement( Element* pParent, sal_Int32 nGCId )
145cdf0e10cSrcweir         : GraphicalElement( pParent, nGCId ), isCharacter(false), ZOrder(0) {}
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     public:
148cdf0e10cSrcweir         bool      isCharacter;
149cdf0e10cSrcweir         sal_Int32 ZOrder;
150cdf0e10cSrcweir     };
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     struct FrameElement : public DrawElement
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         friend class ElementFactory;
155cdf0e10cSrcweir     protected:
FrameElementpdfi::FrameElement156cdf0e10cSrcweir         FrameElement( Element* pParent, sal_Int32 nGCId )
157cdf0e10cSrcweir         : DrawElement( pParent, nGCId ) {}
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     public:
160cdf0e10cSrcweir         // ElementTreeVisitable
161cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
162cdf0e10cSrcweir     };
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     struct TextElement : public GraphicalElement
165cdf0e10cSrcweir     {
166cdf0e10cSrcweir         friend class ElementFactory;
167cdf0e10cSrcweir     protected:
TextElementpdfi::TextElement168cdf0e10cSrcweir         TextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
169cdf0e10cSrcweir         : GraphicalElement( pParent, nGCId ), FontId( nFontId ) {}
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     public:
172cdf0e10cSrcweir         // ElementTreeVisitable
173cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir         rtl::OUStringBuffer Text;
176cdf0e10cSrcweir         sal_Int32           FontId;
177cdf0e10cSrcweir     };
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     struct ParagraphElement : public Element
180cdf0e10cSrcweir     {
181cdf0e10cSrcweir         friend class ElementFactory;
182cdf0e10cSrcweir     protected:
ParagraphElementpdfi::ParagraphElement183cdf0e10cSrcweir         ParagraphElement( Element* pParent ) : Element( pParent ), Type( Normal ), bRtl( false ) {}
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     public:
186cdf0e10cSrcweir         // ElementTreeVisitable
187cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& rParentIt );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         // returns true only if only a single line is contained
190cdf0e10cSrcweir         bool isSingleLined( PDFIProcessor& rProc ) const;
191cdf0e10cSrcweir         // returns the highest line height of the contained textelements
192cdf0e10cSrcweir         // line height is font height if the text element is itself multilined
193cdf0e10cSrcweir         double getLineHeight( PDFIProcessor& rProc ) const;
194cdf0e10cSrcweir         // returns the first text element child; does not recurse through subparagraphs
195cdf0e10cSrcweir         TextElement* getFirstTextChild() const;
196cdf0e10cSrcweir 
197cdf0e10cSrcweir         enum ParagraphType { Normal, Headline };
198cdf0e10cSrcweir         ParagraphType       Type;
199cdf0e10cSrcweir 	bool bRtl;
200cdf0e10cSrcweir     };
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     struct PolyPolyElement : public DrawElement
203cdf0e10cSrcweir     {
204cdf0e10cSrcweir         friend class ElementFactory;
205cdf0e10cSrcweir     protected:
206cdf0e10cSrcweir         PolyPolyElement( Element* pParent, sal_Int32 nGCId,
207cdf0e10cSrcweir                          const basegfx::B2DPolyPolygon& rPolyPoly,
208cdf0e10cSrcweir                          sal_Int8 nAction );
209cdf0e10cSrcweir     public:
210cdf0e10cSrcweir         // ElementTreeVisitable
211cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& rParentIt );
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         void updateGeometry();
214cdf0e10cSrcweir 
215cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
216cdf0e10cSrcweir         virtual void emitStructure( int nLevel );
217cdf0e10cSrcweir #endif
218cdf0e10cSrcweir 
219cdf0e10cSrcweir         basegfx::B2DPolyPolygon PolyPoly;
220cdf0e10cSrcweir         sal_Int8                Action;
221cdf0e10cSrcweir     };
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     struct ImageElement : public DrawElement
224cdf0e10cSrcweir     {
225cdf0e10cSrcweir         friend class ElementFactory;
226cdf0e10cSrcweir     protected:
ImageElementpdfi::ImageElement227cdf0e10cSrcweir         ImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
228cdf0e10cSrcweir         : DrawElement( pParent, nGCId ), Image( nImage ) {}
229cdf0e10cSrcweir 
230cdf0e10cSrcweir     public:
231cdf0e10cSrcweir         // ElementTreeVisitable
232cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         ImageId Image;
235cdf0e10cSrcweir     };
236cdf0e10cSrcweir 
237cdf0e10cSrcweir     struct PageElement : public Element
238cdf0e10cSrcweir     {
239cdf0e10cSrcweir         friend class ElementFactory;
240cdf0e10cSrcweir     protected:
PageElementpdfi::PageElement241cdf0e10cSrcweir         PageElement( Element* pParent, sal_Int32 nPageNr )
242cdf0e10cSrcweir         : Element( pParent ), PageNumber( nPageNr ), Hyperlinks(),
243cdf0e10cSrcweir         TopMargin( 0.0 ), BottomMargin( 0.0 ), LeftMargin( 0.0 ), RightMargin( 0.0 ),
244cdf0e10cSrcweir         HeaderElement( NULL ), FooterElement( NULL )
245cdf0e10cSrcweir         {}
246cdf0e10cSrcweir     private:
247cdf0e10cSrcweir         // helper method for resolveHyperlinks
248cdf0e10cSrcweir         bool resolveHyperlink( std::list<Element*>::iterator link_it, std::list<Element*>& rElements );
249cdf0e10cSrcweir         public:
250cdf0e10cSrcweir         virtual ~PageElement();
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         // ElementTreeVisitable
253cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& rParentIt );
254cdf0e10cSrcweir 
255cdf0e10cSrcweir         void emitPageAnchoredElements( EmitContext& rEmitContext );
256cdf0e10cSrcweir         static void updateParagraphGeometry( Element* pEle );
257cdf0e10cSrcweir         void resolveHyperlinks();
258cdf0e10cSrcweir         void resolveFontStyles( PDFIProcessor& rProc );
259cdf0e10cSrcweir         void resolveUnderlines( PDFIProcessor& rProc );
260cdf0e10cSrcweir 
261cdf0e10cSrcweir         sal_Int32      PageNumber;
262cdf0e10cSrcweir         ListElement    Hyperlinks; // contains not yet realized links on this page
263cdf0e10cSrcweir         double         TopMargin;
264cdf0e10cSrcweir         double         BottomMargin;
265cdf0e10cSrcweir         double         LeftMargin;
266cdf0e10cSrcweir         double         RightMargin;
267cdf0e10cSrcweir         Element*       HeaderElement;
268cdf0e10cSrcweir         Element*       FooterElement;
269cdf0e10cSrcweir     };
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     struct DocumentElement : public Element
272cdf0e10cSrcweir     {
273cdf0e10cSrcweir         friend class ElementFactory;
274cdf0e10cSrcweir     protected:
DocumentElementpdfi::DocumentElement275cdf0e10cSrcweir         DocumentElement() : Element( NULL ) {}
276cdf0e10cSrcweir     public:
277cdf0e10cSrcweir         virtual ~DocumentElement();
278cdf0e10cSrcweir 
279cdf0e10cSrcweir         // ElementTreeVisitable
280cdf0e10cSrcweir         virtual void visitedBy( ElementTreeVisitor&, const std::list< Element* >::const_iterator& );
281cdf0e10cSrcweir 
282cdf0e10cSrcweir     };
283cdf0e10cSrcweir 
284cdf0e10cSrcweir     // this class is the differentiator of document types: it will create
285cdf0e10cSrcweir     // Element objects with an optimize() method suitable for the document type
286cdf0e10cSrcweir     class ElementFactory
287cdf0e10cSrcweir     {
288cdf0e10cSrcweir     public:
ElementFactory()289cdf0e10cSrcweir         ElementFactory() {}
290cdf0e10cSrcweir         virtual ~ElementFactory();
291cdf0e10cSrcweir 
createHyperlinkElement(Element * pParent,const rtl::OUString & rURI)292cdf0e10cSrcweir         virtual HyperlinkElement* createHyperlinkElement( Element* pParent, const rtl::OUString& rURI )
293cdf0e10cSrcweir         { return new HyperlinkElement( pParent, rURI ); }
294cdf0e10cSrcweir 
createTextElement(Element * pParent,sal_Int32 nGCId,sal_Int32 nFontId)295cdf0e10cSrcweir         virtual TextElement* createTextElement( Element* pParent, sal_Int32 nGCId, sal_Int32 nFontId )
296cdf0e10cSrcweir         { return new TextElement( pParent, nGCId, nFontId ); }
createParagraphElement(Element * pParent)297cdf0e10cSrcweir         virtual ParagraphElement* createParagraphElement( Element* pParent )
298cdf0e10cSrcweir         { return new ParagraphElement( pParent ); }
299cdf0e10cSrcweir 
createFrameElement(Element * pParent,sal_Int32 nGCId)300cdf0e10cSrcweir         virtual FrameElement* createFrameElement( Element* pParent, sal_Int32 nGCId )
301cdf0e10cSrcweir         { return new FrameElement( pParent, nGCId ); }
302cdf0e10cSrcweir         virtual PolyPolyElement*
createPolyPolyElement(Element * pParent,sal_Int32 nGCId,const basegfx::B2DPolyPolygon & rPolyPoly,sal_Int8 nAction)303cdf0e10cSrcweir             createPolyPolyElement( Element* pParent,
304cdf0e10cSrcweir                                    sal_Int32 nGCId,
305cdf0e10cSrcweir                                    const basegfx::B2DPolyPolygon& rPolyPoly,
306cdf0e10cSrcweir                                    sal_Int8 nAction)
307cdf0e10cSrcweir         { return new PolyPolyElement( pParent, nGCId, rPolyPoly, nAction ); }
createImageElement(Element * pParent,sal_Int32 nGCId,ImageId nImage)308cdf0e10cSrcweir         virtual ImageElement* createImageElement( Element* pParent, sal_Int32 nGCId, ImageId nImage )
309cdf0e10cSrcweir         { return new ImageElement( pParent, nGCId, nImage ); }
310cdf0e10cSrcweir 
createPageElement(Element * pParent,sal_Int32 nPageNr)311cdf0e10cSrcweir         virtual PageElement* createPageElement( Element* pParent,
312cdf0e10cSrcweir                                                 sal_Int32 nPageNr )
313cdf0e10cSrcweir         { return new PageElement( pParent, nPageNr ); }
createDocumentElement()314cdf0e10cSrcweir         virtual DocumentElement* createDocumentElement()
315cdf0e10cSrcweir         { return new DocumentElement(); }
316cdf0e10cSrcweir     };
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir #endif
320