1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pdfiprocessor.hxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #ifndef INCLUDED_PDFI_PROCESSOR_HXX
33 #define INCLUDED_PDFI_PROCESSOR_HXX
35 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
36 #include <com/sun/star/task/XStatusIndicator.hpp>
37 #include <com/sun/star/rendering/XVolatileBitmap.hpp>
38 #include <com/sun/star/geometry/RealSize2D.hpp>
39 #include <com/sun/star/geometry/RealPoint2D.hpp>
40 #include <com/sun/star/geometry/RealRectangle2D.hpp>
41 #include <com/sun/star/geometry/Matrix2D.hpp>
43 #include <basegfx/polygon/b2dpolypolygon.hxx>
44 #include <basegfx/polygon/b2dpolygon.hxx>
45 #include <basegfx/matrix/b2dhommatrix.hxx>
46 #include <basegfx/range/b2drange.hxx>
48 #include <rtl/ustring.hxx>
49 #include <rtl/ustrbuf.hxx>
51 #include <boost/shared_ptr.hpp>
55 #include "imagecontainer.hxx"
56 #include "pdfihelper.hxx"
57 #include "contentsink.hxx"
58 #include "treevisitorfactory.hxx"
59 #include "genericelements.hxx"
66 struct DocumentElement
;
72 /** Main entry from the parser
74 Creates the internal DOM tree from the render calls
76 class PDFIProcessor
: public ContentSink
79 double fYPrevTextPosition
;
80 double fPrevTextHeight
;
81 double fXPrevTextPosition
;
82 double fPrevTextWidth
;
83 enum DocumentTextDirecion
{ LrTb
, RlTb
, TbLr
};
85 explicit PDFIProcessor( const com::sun::star::uno::Reference
< com::sun::star::task::XStatusIndicator
>& );
87 /// TEMP - enable writer-like text:p on doc level
88 void enableToplevelText();
90 void emit( XmlEmitter
& rEmitter
,
91 const TreeVisitorFactory
& rVisitorFactory
);
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(); }
98 ImageContainer
& getImages() { return m_aImages
; }
99 boost::shared_ptr
<ElementFactory
> getElementFactory() const { return m_pElFactory
; }
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
; }
106 const FontAttributes
& getFont( sal_Int32 nFontId
) const;
107 sal_Int32
getFontId( const FontAttributes
& rAttr
) const;
109 void sortElements( Element
* pElement
, bool bDeep
= false );
110 void sortDocument( bool bDeep
= false );
113 void processGlyphLine();
114 void processGlyph( double fPreAvarageSpaceValue
,
116 ParagraphElement
* pPara
,
117 FrameElement
* pFrame
,
118 bool bIsWhiteSpaceInLine
);
120 void drawGlyphLine( const rtl::OUString
& rGlyphs
,
121 const ::com::sun::star::geometry::RealRectangle2D
& rRect
,
122 const ::com::sun::star::geometry::Matrix2D
& rFontMatrix
);
124 void drawCharGlyphs( rtl::OUString
& rGlyphs
,
125 ::com::sun::star::geometry::RealRectangle2D
& rRect
,
126 ::com::sun::star::geometry::Matrix2D
& rFontMatrix
,
128 Element
* pCurElement
,
129 ParagraphElement
* pPara
,
130 FrameElement
* pFrame
,
133 GraphicsContext
& getTransformGlyphContext( CharGlyph
& rGlyph
);
135 // ContentSink interface implementation
137 virtual void setPageNum( sal_Int32 nNumPages
);
138 virtual void startPage( const ::com::sun::star::geometry::RealSize2D
& rSize
);
139 virtual void endPage();
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
,
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
);
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
);
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
);
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();
176 virtual void drawMask(const ::com::sun::star::uno::Sequence
<
177 ::com::sun::star::beans::PropertyValue
>& xBitmap
,
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.
184 maskColors must contain two sequences of color components
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
,
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
);
200 /// nElements == -1 means fill in number of pages
201 void startIndicator( const rtl::OUString
& rText
, sal_Int32 nElements
= -1 );
204 void setupImage(ImageId nImage
);
206 typedef std::hash_map
<sal_Int32
,FontAttributes
> IdToFontMap
;
207 typedef std::hash_map
<FontAttributes
,sal_Int32
,FontAttrHash
> FontToIdMap
;
209 typedef std::hash_map
<sal_Int32
,GraphicsContext
> IdToGCMap
;
210 typedef std::hash_map
<GraphicsContext
,sal_Int32
,GraphicsContextHash
> GCToIdMap
;
212 typedef std::vector
<GraphicsContext
> GraphicsContextStack
;
214 ::basegfx::B2DRange
& calcTransformedRectBounds( ::basegfx::B2DRange
& outRect
,
215 const ::basegfx::B2DRange
& inRect
,
216 const ::basegfx::B2DHomMatrix
& transformation
);
217 std::vector
<CharGlyph
> m_GlyphsList
;
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
;
227 GraphicsContextStack m_aGCStack
;
228 GraphicsContext m_prev_aGC
;
229 sal_Int32 m_nNextGCId
;
233 ImageContainer m_aImages
;
235 DocumentTextDirecion m_eTextDirection
;
238 sal_Int32 m_nNextZOrder
;
240 bool m_bIsWhiteSpaceInLine
;
241 com::sun::star::uno::Reference
<
242 com::sun::star::task::XStatusIndicator
>
245 bool m_bHaveTextOnDocLevel
;
251 virtual ~CharGlyph(){};
252 rtl::OUString
& getGlyph(){ return m_rGlyphs
; }
253 com::sun::star::geometry::RealRectangle2D
& getRect(){ return m_rRect
; }
254 com::sun::star::geometry::Matrix2D
& getFontMatrix(){ return m_rFontMatrix
; }
255 GraphicsContext
& getGC(){ return m_rCurrentContext
; }
256 Element
* getCurElement(){ return m_pCurElement
; }
258 void setGlyph (const rtl::OUString
& rGlyphs
){ m_rGlyphs
=rGlyphs
; }
259 void setRect (const ::com::sun::star::geometry::RealRectangle2D
& rRect
){ m_rRect
=rRect
; }
260 void setFontMatrix (const ::com::sun::star::geometry::Matrix2D
& rFontMatrix
){ m_rFontMatrix
= rFontMatrix
; }
261 void setGraphicsContext (GraphicsContext
& rCurrentContext
){ m_rCurrentContext
= rCurrentContext
; }
262 void setCurElement( Element
* pCurElement
){ m_pCurElement
= pCurElement
; }
264 double getYPrevGlyphPosition(){ return m_fYPrevGlyphPosition
; }
265 double getXPrevGlyphPosition(){ return m_fXPrevGlyphPosition
; }
266 double getPrevGlyphHeight(){ return m_fPrevGlyphHeight
; }
267 double getPrevGlyphWidth (){ return m_fPrevGlyphWidth
; }
268 double getPrevGlyphsSpace() { if( (m_rRect
.X1
-m_fXPrevGlyphPosition
)<0 )
271 return m_rRect
.X1
-m_fXPrevGlyphPosition
;
274 void setYPrevGlyphPosition( double fYPrevTextPosition
){ m_fYPrevGlyphPosition
= fYPrevTextPosition
; }
275 void setXPrevGlyphPosition( double fXPrevTextPosition
){ m_fXPrevGlyphPosition
= fXPrevTextPosition
; }
276 void setPrevGlyphHeight ( double fPrevTextHeight
){ m_fPrevGlyphHeight
= fPrevTextHeight
; }
277 void setPrevGlyphWidth ( double fPrevTextWidth
){ m_fPrevGlyphWidth
= fPrevTextWidth
; }
281 double m_fYPrevGlyphPosition
;
282 double m_fXPrevGlyphPosition
;
283 double m_fPrevGlyphHeight
;
284 double m_fPrevGlyphWidth
;
285 Element
* m_pCurElement
;
286 GraphicsContext m_rCurrentContext
;
287 com::sun::star::geometry::Matrix2D m_rFontMatrix
;
288 com::sun::star::geometry::RealRectangle2D m_rRect
;
289 rtl::OUString m_rGlyphs
;
293 #define USTR(x) rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )