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