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