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