xref: /trunk/main/sdext/source/pdfimport/tree/pdfiprocessor.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_PROCESSOR_HXX
29*cdf0e10cSrcweir #define INCLUDED_PDFI_PROCESSOR_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <com/sun/star/util/XStringMapping.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/task/XStatusIndicator.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/rendering/XVolatileBitmap.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/geometry/RealSize2D.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/geometry/RealPoint2D.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/geometry/RealRectangle2D.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/geometry/Matrix2D.hpp>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygon.hxx>
41*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
42*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
43*cdf0e10cSrcweir #include <basegfx/range/b2drange.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <rtl/ustring.hxx>
46*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
49*cdf0e10cSrcweir #include <list>
50*cdf0e10cSrcweir #include <hash_map>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include "imagecontainer.hxx"
53*cdf0e10cSrcweir #include "pdfihelper.hxx"
54*cdf0e10cSrcweir #include "contentsink.hxx"
55*cdf0e10cSrcweir #include "treevisitorfactory.hxx"
56*cdf0e10cSrcweir #include "genericelements.hxx"
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir namespace pdfi
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     class  PDFIProcessor;
62*cdf0e10cSrcweir     struct Element;
63*cdf0e10cSrcweir     struct DocumentElement;
64*cdf0e10cSrcweir     struct PageElement;
65*cdf0e10cSrcweir     class  ElementFactory;
66*cdf0e10cSrcweir     class  XmlEmitter;
67*cdf0e10cSrcweir     class  CharGlyph;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     /** Main entry from the parser
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir         Creates the internal DOM tree from the render calls
72*cdf0e10cSrcweir      */
73*cdf0e10cSrcweir     class PDFIProcessor : public ContentSink
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir     public:
76*cdf0e10cSrcweir         com::sun::star::uno::Reference<
77*cdf0e10cSrcweir             com::sun::star::uno::XComponentContext >  m_xContext;
78*cdf0e10cSrcweir         double fYPrevTextPosition;
79*cdf0e10cSrcweir         double fPrevTextHeight;
80*cdf0e10cSrcweir         double fXPrevTextPosition;
81*cdf0e10cSrcweir         double fPrevTextWidth;
82*cdf0e10cSrcweir         enum DocumentTextDirecion { LrTb, RlTb, TbLr };
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir         explicit PDFIProcessor( const com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator >& xStat,
85*cdf0e10cSrcweir             com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext) ;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir         /// TEMP - enable writer-like text:p on doc level
88*cdf0e10cSrcweir         void enableToplevelText();
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir         void emit( XmlEmitter&               rEmitter,
91*cdf0e10cSrcweir                    const TreeVisitorFactory& rVisitorFactory );
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir         sal_Int32 getGCId( const GraphicsContext& rGC );
94*cdf0e10cSrcweir         const GraphicsContext& getGraphicsContext( sal_Int32 nGCId ) const;
95*cdf0e10cSrcweir         GraphicsContext& getCurrentContext() { return m_aGCStack.back(); }
96*cdf0e10cSrcweir         const GraphicsContext& getCurrentContext() const { return m_aGCStack.back(); }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir         ImageContainer& getImages() { return m_aImages; }
99*cdf0e10cSrcweir         boost::shared_ptr<ElementFactory> getElementFactory() const { return m_pElFactory; }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir         const com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator >& getStatusIndicator() const
102*cdf0e10cSrcweir         { return m_xStatusIndicator; }
103*cdf0e10cSrcweir         void setStatusIndicator( const com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator >& xStatus )
104*cdf0e10cSrcweir         { m_xStatusIndicator = xStatus; }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir         const FontAttributes& getFont( sal_Int32 nFontId ) const;
107*cdf0e10cSrcweir         sal_Int32 getFontId( const FontAttributes& rAttr ) const;
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         void sortElements( Element* pElement, bool bDeep = false );
110*cdf0e10cSrcweir         void sortDocument( bool bDeep = false );
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         rtl::OUString mirrorString( const rtl::OUString& i_rInString );
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     private:
115*cdf0e10cSrcweir         void prepareMirrorMap();
116*cdf0e10cSrcweir         void processGlyphLine();
117*cdf0e10cSrcweir         void processGlyph(   double       fPreAvarageSpaceValue,
118*cdf0e10cSrcweir                              CharGlyph&   rGlyph,
119*cdf0e10cSrcweir                              ParagraphElement* pPara,
120*cdf0e10cSrcweir                              FrameElement* pFrame,
121*cdf0e10cSrcweir                              bool         bIsWhiteSpaceInLine );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         void drawGlyphLine( const rtl::OUString&                               rGlyphs,
124*cdf0e10cSrcweir                             const ::com::sun::star::geometry::RealRectangle2D& rRect,
125*cdf0e10cSrcweir                             const ::com::sun::star::geometry::Matrix2D&        rFontMatrix  );
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir         void drawCharGlyphs( rtl::OUString&             rGlyphs,
128*cdf0e10cSrcweir                              ::com::sun::star::geometry::RealRectangle2D&  rRect,
129*cdf0e10cSrcweir                              ::com::sun::star::geometry::Matrix2D&         rFontMatrix,
130*cdf0e10cSrcweir                              GraphicsContext aGC,
131*cdf0e10cSrcweir                              Element* pCurElement,
132*cdf0e10cSrcweir                              ParagraphElement* pPara,
133*cdf0e10cSrcweir                              FrameElement* pFrame,
134*cdf0e10cSrcweir                              bool bSpaceFlag );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         GraphicsContext& getTransformGlyphContext( CharGlyph& rGlyph );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         // ContentSink interface implementation
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         virtual void setPageNum( sal_Int32 nNumPages );
141*cdf0e10cSrcweir         virtual void startPage( const ::com::sun::star::geometry::RealSize2D& rSize );
142*cdf0e10cSrcweir         virtual void endPage();
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir         virtual void hyperLink( const ::com::sun::star::geometry::RealRectangle2D& rBounds,
145*cdf0e10cSrcweir                                 const ::rtl::OUString&                             rURI );
146*cdf0e10cSrcweir         virtual void pushState();
147*cdf0e10cSrcweir         virtual void popState();
148*cdf0e10cSrcweir         virtual void setFlatness( double );
149*cdf0e10cSrcweir         virtual void setTransformation( const ::com::sun::star::geometry::AffineMatrix2D& rMatrix );
150*cdf0e10cSrcweir         virtual void setLineDash( const ::com::sun::star::uno::Sequence<double>& dashes,
151*cdf0e10cSrcweir                                   double                                         start );
152*cdf0e10cSrcweir         virtual void setLineJoin(sal_Int8);
153*cdf0e10cSrcweir         virtual void setLineCap(sal_Int8);
154*cdf0e10cSrcweir         virtual void setMiterLimit(double);
155*cdf0e10cSrcweir         virtual void setLineWidth(double);
156*cdf0e10cSrcweir         virtual void setFillColor( const ::com::sun::star::rendering::ARGBColor& rColor );
157*cdf0e10cSrcweir         virtual void setStrokeColor( const ::com::sun::star::rendering::ARGBColor& rColor );
158*cdf0e10cSrcweir         virtual void setBlendMode(sal_Int8);
159*cdf0e10cSrcweir         virtual void setFont( const FontAttributes& rFont );
160*cdf0e10cSrcweir         virtual void setTextRenderMode( sal_Int32 );
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir         virtual void strokePath( const ::com::sun::star::uno::Reference<
163*cdf0e10cSrcweir                                        ::com::sun::star::rendering::XPolyPolygon2D >& rPath );
164*cdf0e10cSrcweir         virtual void fillPath( const ::com::sun::star::uno::Reference<
165*cdf0e10cSrcweir                                      ::com::sun::star::rendering::XPolyPolygon2D >& rPath );
166*cdf0e10cSrcweir         virtual void eoFillPath( const ::com::sun::star::uno::Reference<
167*cdf0e10cSrcweir                                        ::com::sun::star::rendering::XPolyPolygon2D >& rPath );
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         virtual void intersectClip(const ::com::sun::star::uno::Reference<
170*cdf0e10cSrcweir                                          ::com::sun::star::rendering::XPolyPolygon2D >& rPath);
171*cdf0e10cSrcweir         virtual void intersectEoClip(const ::com::sun::star::uno::Reference<
172*cdf0e10cSrcweir                                            ::com::sun::star::rendering::XPolyPolygon2D >& rPath);
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         virtual void drawGlyphs( const rtl::OUString&                               rGlyphs,
175*cdf0e10cSrcweir                                  const ::com::sun::star::geometry::RealRectangle2D& rRect,
176*cdf0e10cSrcweir                                  const ::com::sun::star::geometry::Matrix2D&        rFontMatrix );
177*cdf0e10cSrcweir         virtual void endText();
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir         virtual void drawMask(const ::com::sun::star::uno::Sequence<
180*cdf0e10cSrcweir                                     ::com::sun::star::beans::PropertyValue>& xBitmap,
181*cdf0e10cSrcweir                               bool                                           bInvert );
182*cdf0e10cSrcweir         /// Given image must already be color-mapped and normalized to sRGB.
183*cdf0e10cSrcweir         virtual void drawImage(const ::com::sun::star::uno::Sequence<
184*cdf0e10cSrcweir                                      ::com::sun::star::beans::PropertyValue>& xBitmap );
185*cdf0e10cSrcweir         /** Given image must already be color-mapped and normalized to sRGB.
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir             maskColors must contain two sequences of color components
188*cdf0e10cSrcweir          */
189*cdf0e10cSrcweir         virtual void drawColorMaskedImage(const ::com::sun::star::uno::Sequence<
190*cdf0e10cSrcweir                                                 ::com::sun::star::beans::PropertyValue>& xBitmap,
191*cdf0e10cSrcweir                                           const ::com::sun::star::uno::Sequence<
192*cdf0e10cSrcweir                                                 ::com::sun::star::uno::Any>&             xMaskColors );
193*cdf0e10cSrcweir         virtual void drawMaskedImage(const ::com::sun::star::uno::Sequence<
194*cdf0e10cSrcweir                                            ::com::sun::star::beans::PropertyValue>& xBitmap,
195*cdf0e10cSrcweir                                      const ::com::sun::star::uno::Sequence<
196*cdf0e10cSrcweir                                            ::com::sun::star::beans::PropertyValue>& xMask,
197*cdf0e10cSrcweir                                      bool                                             bInvertMask);
198*cdf0e10cSrcweir         virtual void drawAlphaMaskedImage(const ::com::sun::star::uno::Sequence<
199*cdf0e10cSrcweir                                                 ::com::sun::star::beans::PropertyValue>& xImage,
200*cdf0e10cSrcweir                                           const ::com::sun::star::uno::Sequence<
201*cdf0e10cSrcweir                                                 ::com::sun::star::beans::PropertyValue>& xMask);
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir         /// nElements == -1 means fill in number of pages
204*cdf0e10cSrcweir         void startIndicator( const rtl::OUString& rText, sal_Int32 nElements = -1 );
205*cdf0e10cSrcweir         void endIndicator();
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir         void setupImage(ImageId nImage);
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir         typedef std::hash_map<sal_Int32,FontAttributes> IdToFontMap;
210*cdf0e10cSrcweir         typedef std::hash_map<FontAttributes,sal_Int32,FontAttrHash> FontToIdMap;
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir         typedef std::hash_map<sal_Int32,GraphicsContext> IdToGCMap;
213*cdf0e10cSrcweir         typedef std::hash_map<GraphicsContext,sal_Int32,GraphicsContextHash> GCToIdMap;
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir         typedef std::vector<GraphicsContext> GraphicsContextStack;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         ::basegfx::B2DRange& calcTransformedRectBounds( ::basegfx::B2DRange&            outRect,
218*cdf0e10cSrcweir                                                         const ::basegfx::B2DRange&      inRect,
219*cdf0e10cSrcweir                                                         const ::basegfx::B2DHomMatrix&  transformation );
220*cdf0e10cSrcweir         std::vector<CharGlyph>             m_GlyphsList;
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir         boost::shared_ptr<ElementFactory>  m_pElFactory;
223*cdf0e10cSrcweir         boost::shared_ptr<DocumentElement> m_pDocument;
224*cdf0e10cSrcweir         PageElement*                       m_pCurPage;
225*cdf0e10cSrcweir         Element*                           m_pCurElement;
226*cdf0e10cSrcweir         sal_Int32                          m_nNextFontId;
227*cdf0e10cSrcweir         IdToFontMap                        m_aIdToFont;
228*cdf0e10cSrcweir         FontToIdMap                        m_aFontToId;
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir         GraphicsContextStack               m_aGCStack;
231*cdf0e10cSrcweir         GraphicsContext                    m_prev_aGC;
232*cdf0e10cSrcweir         sal_Int32                          m_nNextGCId;
233*cdf0e10cSrcweir         IdToGCMap                          m_aIdToGC;
234*cdf0e10cSrcweir         GCToIdMap                          m_aGCToId;
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir         ImageContainer                     m_aImages;
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir         DocumentTextDirecion               m_eTextDirection;
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir         sal_Int32                          m_nPages;
241*cdf0e10cSrcweir         sal_Int32                          m_nNextZOrder;
242*cdf0e10cSrcweir         double                             m_fWordSpace;
243*cdf0e10cSrcweir         bool                               m_bIsWhiteSpaceInLine;
244*cdf0e10cSrcweir         com::sun::star::uno::Reference<
245*cdf0e10cSrcweir             com::sun::star::task::XStatusIndicator >
246*cdf0e10cSrcweir                                            m_xStatusIndicator;
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir         bool                               m_bHaveTextOnDocLevel;
249*cdf0e10cSrcweir         std::vector< sal_Unicode >         m_aMirrorMap;
250*cdf0e10cSrcweir         com::sun::star::uno::Reference<
251*cdf0e10cSrcweir             com::sun::star::util::XStringMapping >
252*cdf0e10cSrcweir                                            m_xMirrorMapper;
253*cdf0e10cSrcweir         bool                               m_bMirrorMapperTried;
254*cdf0e10cSrcweir     };
255*cdf0e10cSrcweir     class CharGlyph
256*cdf0e10cSrcweir     {
257*cdf0e10cSrcweir         public:
258*cdf0e10cSrcweir             CharGlyph(){};
259*cdf0e10cSrcweir             virtual ~CharGlyph(){};
260*cdf0e10cSrcweir             rtl::OUString& getGlyph(){ return m_rGlyphs; }
261*cdf0e10cSrcweir             com::sun::star::geometry::RealRectangle2D& getRect(){ return m_rRect; }
262*cdf0e10cSrcweir             com::sun::star::geometry::Matrix2D&  getFontMatrix(){ return m_rFontMatrix; }
263*cdf0e10cSrcweir             GraphicsContext&  getGC(){ return m_rCurrentContext; }
264*cdf0e10cSrcweir             Element*  getCurElement(){ return m_pCurElement; }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir             void  setGlyph (const rtl::OUString& rGlyphs ){ m_rGlyphs=rGlyphs; }
267*cdf0e10cSrcweir             void  setRect  (const ::com::sun::star::geometry::RealRectangle2D& rRect ){ m_rRect=rRect; }
268*cdf0e10cSrcweir             void  setFontMatrix (const ::com::sun::star::geometry::Matrix2D& rFontMatrix ){ m_rFontMatrix= rFontMatrix; }
269*cdf0e10cSrcweir             void  setGraphicsContext (GraphicsContext&  rCurrentContext ){ m_rCurrentContext= rCurrentContext; }
270*cdf0e10cSrcweir             void  setCurElement( Element* pCurElement ){ m_pCurElement= pCurElement; }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir             double getYPrevGlyphPosition(){ return m_fYPrevGlyphPosition; }
273*cdf0e10cSrcweir             double getXPrevGlyphPosition(){ return m_fXPrevGlyphPosition; }
274*cdf0e10cSrcweir             double getPrevGlyphHeight(){ return m_fPrevGlyphHeight; }
275*cdf0e10cSrcweir             double getPrevGlyphWidth (){ return m_fPrevGlyphWidth; }
276*cdf0e10cSrcweir             double getPrevGlyphsSpace() { if( (m_rRect.X1-m_fXPrevGlyphPosition)<0 )
277*cdf0e10cSrcweir                                                 return 0;
278*cdf0e10cSrcweir                                            else
279*cdf0e10cSrcweir                                             return m_rRect.X1-m_fXPrevGlyphPosition;
280*cdf0e10cSrcweir                                          }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir             void setYPrevGlyphPosition( double fYPrevTextPosition ){ m_fYPrevGlyphPosition= fYPrevTextPosition; }
283*cdf0e10cSrcweir             void setXPrevGlyphPosition( double fXPrevTextPosition ){ m_fXPrevGlyphPosition= fXPrevTextPosition; }
284*cdf0e10cSrcweir             void setPrevGlyphHeight   ( double fPrevTextHeight ){ m_fPrevGlyphHeight= fPrevTextHeight; }
285*cdf0e10cSrcweir             void setPrevGlyphWidth    ( double fPrevTextWidth ){ m_fPrevGlyphWidth= fPrevTextWidth; }
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir         private:
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir             double                      m_fYPrevGlyphPosition ;
290*cdf0e10cSrcweir             double                      m_fXPrevGlyphPosition ;
291*cdf0e10cSrcweir             double                      m_fPrevGlyphHeight ;
292*cdf0e10cSrcweir             double                      m_fPrevGlyphWidth ;
293*cdf0e10cSrcweir             Element*                    m_pCurElement ;
294*cdf0e10cSrcweir             GraphicsContext             m_rCurrentContext ;
295*cdf0e10cSrcweir             com::sun::star::geometry::Matrix2D          m_rFontMatrix ;
296*cdf0e10cSrcweir             com::sun::star::geometry::RealRectangle2D   m_rRect ;
297*cdf0e10cSrcweir             rtl::OUString               m_rGlyphs ;
298*cdf0e10cSrcweir     };
299*cdf0e10cSrcweir }
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir #define USTR(x) rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir #endif
304