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