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