xref: /aoo42x/main/vcl/source/gdi/pdfwriter_impl2.cxx (revision f228387c)
19f62ea84SAndrew Rist /**************************************************************
29f62ea84SAndrew Rist  *
39f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59f62ea84SAndrew Rist  * distributed with this work for additional information
69f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
99f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
109f62ea84SAndrew Rist  *
119f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129f62ea84SAndrew Rist  *
139f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149f62ea84SAndrew Rist  * software distributed under the License is distributed on an
159f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
179f62ea84SAndrew Rist  * specific language governing permissions and limitations
189f62ea84SAndrew Rist  * under the License.
199f62ea84SAndrew Rist  *
209f62ea84SAndrew Rist  *************************************************************/
219f62ea84SAndrew Rist 
229f62ea84SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_vcl.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "pdfwriter_impl.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "vcl/pdfextoutdevdata.hxx"
29cdf0e10cSrcweir #include "vcl/virdev.hxx"
30cdf0e10cSrcweir #include "vcl/gdimtf.hxx"
31cdf0e10cSrcweir #include "vcl/metaact.hxx"
32cdf0e10cSrcweir #include "vcl/bmpacc.hxx"
33cdf0e10cSrcweir #include "vcl/graph.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include "svdata.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include "unotools/streamwrap.hxx"
38cdf0e10cSrcweir #include "unotools/processfactory.hxx"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include "comphelper/processfactory.hxx"
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include "com/sun/star/beans/PropertyValue.hpp"
43cdf0e10cSrcweir #include "com/sun/star/io/XSeekable.hpp"
44cdf0e10cSrcweir #include "com/sun/star/graphic/XGraphicProvider.hpp"
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include <rtl/digest.h>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #undef USE_PDFGRADIENTS
51cdf0e10cSrcweir 
52cdf0e10cSrcweir using namespace vcl;
53cdf0e10cSrcweir using namespace rtl;
54cdf0e10cSrcweir using namespace com::sun::star;
55cdf0e10cSrcweir using namespace com::sun::star::uno;
56cdf0e10cSrcweir using namespace com::sun::star::beans;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir // -----------------------------------------------------------------------------
59cdf0e10cSrcweir 
implWriteGradient(const PolyPolygon & i_rPolyPoly,const Gradient & i_rGradient,VirtualDevice * i_pDummyVDev,const vcl::PDFWriter::PlayMetafileContext & i_rContext)60cdf0e10cSrcweir void PDFWriterImpl::implWriteGradient( const PolyPolygon& i_rPolyPoly, const Gradient& i_rGradient,
61cdf0e10cSrcweir                                        VirtualDevice* i_pDummyVDev, const vcl::PDFWriter::PlayMetafileContext& i_rContext )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     GDIMetaFile        aTmpMtf;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     i_pDummyVDev->AddGradientActions( i_rPolyPoly.GetBoundRect(), i_rGradient, aTmpMtf );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     m_rOuterFace.Push();
68cdf0e10cSrcweir     m_rOuterFace.IntersectClipRegion( i_rPolyPoly.getB2DPolyPolygon() );
69cdf0e10cSrcweir     playMetafile( aTmpMtf, NULL, i_rContext, i_pDummyVDev );
70cdf0e10cSrcweir     m_rOuterFace.Pop();
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir // -----------------------------------------------------------------------------
74cdf0e10cSrcweir 
implWriteBitmapEx(const Point & i_rPoint,const Size & i_rSize,const BitmapEx & i_rBitmapEx,VirtualDevice * i_pDummyVDev,const vcl::PDFWriter::PlayMetafileContext & i_rContext)75cdf0e10cSrcweir void PDFWriterImpl::implWriteBitmapEx( const Point& i_rPoint, const Size& i_rSize, const BitmapEx& i_rBitmapEx,
76cdf0e10cSrcweir                                        VirtualDevice* i_pDummyVDev, const vcl::PDFWriter::PlayMetafileContext& i_rContext )
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 	if ( !i_rBitmapEx.IsEmpty() && i_rSize.Width() && i_rSize.Height() )
79cdf0e10cSrcweir 	{
80cdf0e10cSrcweir 		BitmapEx		aBitmapEx( i_rBitmapEx );
81cdf0e10cSrcweir         Point			aPoint( i_rPoint );
82cdf0e10cSrcweir         Size			aSize( i_rSize );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         // #i19065# Negative sizes have mirror semantics on
85cdf0e10cSrcweir         // OutputDevice. BitmapEx and co. have no idea about that, so
86cdf0e10cSrcweir         // perform that _before_ doing anything with aBitmapEx.
87cdf0e10cSrcweir         sal_uLong nMirrorFlags(BMP_MIRROR_NONE);
88cdf0e10cSrcweir         if( aSize.Width() < 0 )
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             aSize.Width() *= -1;
91cdf0e10cSrcweir             aPoint.X() -= aSize.Width();
92cdf0e10cSrcweir             nMirrorFlags |= BMP_MIRROR_HORZ;
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir         if( aSize.Height() < 0 )
95cdf0e10cSrcweir         {
96cdf0e10cSrcweir             aSize.Height() *= -1;
97cdf0e10cSrcweir             aPoint.Y() -= aSize.Height();
98cdf0e10cSrcweir             nMirrorFlags |= BMP_MIRROR_VERT;
99cdf0e10cSrcweir         }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         if( nMirrorFlags != BMP_MIRROR_NONE )
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir             aBitmapEx.Mirror( nMirrorFlags );
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir 		if( i_rContext.m_nMaxImageResolution > 50 )
106cdf0e10cSrcweir 		{
10786e1cf34SPedro Giffuni 			// do downsampling if necessary
108cdf0e10cSrcweir 			const Size      aDstSizeTwip( i_pDummyVDev->PixelToLogic( i_pDummyVDev->LogicToPixel( aSize ), MAP_TWIP ) );
109cdf0e10cSrcweir 			const Size      aBmpSize( aBitmapEx.GetSizePixel() );
110cdf0e10cSrcweir 			const double    fBmpPixelX = aBmpSize.Width();
111cdf0e10cSrcweir 			const double    fBmpPixelY = aBmpSize.Height();
112cdf0e10cSrcweir 			const double    fMaxPixelX = aDstSizeTwip.Width() * i_rContext.m_nMaxImageResolution / 1440.0;
113cdf0e10cSrcweir 			const double    fMaxPixelY = aDstSizeTwip.Height() * i_rContext.m_nMaxImageResolution / 1440.0;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 			// check, if the bitmap DPI exceeds the maximum DPI (allow 4 pixel rounding tolerance)
116cdf0e10cSrcweir 			if( ( ( fBmpPixelX > ( fMaxPixelX + 4 ) ) ||
117cdf0e10cSrcweir 				( fBmpPixelY > ( fMaxPixelY + 4 ) ) ) &&
118cdf0e10cSrcweir 				( fBmpPixelY > 0.0 ) && ( fMaxPixelY > 0.0 ) )
119cdf0e10cSrcweir 			{
120cdf0e10cSrcweir 				// do scaling
121cdf0e10cSrcweir 				Size            aNewBmpSize;
122cdf0e10cSrcweir 				const double    fBmpWH = fBmpPixelX / fBmpPixelY;
123cdf0e10cSrcweir 				const double    fMaxWH = fMaxPixelX / fMaxPixelY;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 				if( fBmpWH < fMaxWH )
126cdf0e10cSrcweir 				{
127cdf0e10cSrcweir 					aNewBmpSize.Width() = FRound( fMaxPixelY * fBmpWH );
128cdf0e10cSrcweir 					aNewBmpSize.Height() = FRound( fMaxPixelY );
129cdf0e10cSrcweir 				}
130cdf0e10cSrcweir 				else if( fBmpWH > 0.0 )
131cdf0e10cSrcweir 				{
132cdf0e10cSrcweir 					aNewBmpSize.Width() = FRound( fMaxPixelX );
133cdf0e10cSrcweir 					aNewBmpSize.Height() = FRound( fMaxPixelX / fBmpWH);
134cdf0e10cSrcweir 				}
13537ab0f2dSArmin Le Grand 
13637ab0f2dSArmin Le Grand                 if( aNewBmpSize.Width() && aNewBmpSize.Height() )
13737ab0f2dSArmin Le Grand                 {
13837ab0f2dSArmin Le Grand                     // #121233# Use best quality for PDF exports
13937ab0f2dSArmin Le Grand 					aBitmapEx.Scale( aNewBmpSize, BMP_SCALE_BESTQUALITY );
14037ab0f2dSArmin Le Grand                 }
141cdf0e10cSrcweir 				else
14237ab0f2dSArmin Le Grand                 {
143cdf0e10cSrcweir 					aBitmapEx.SetEmpty();
14437ab0f2dSArmin Le Grand                 }
145cdf0e10cSrcweir 			}
146cdf0e10cSrcweir 		}
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 		const Size aSizePixel( aBitmapEx.GetSizePixel() );
149cdf0e10cSrcweir 		if ( aSizePixel.Width() && aSizePixel.Height() )
150cdf0e10cSrcweir 		{
151cdf0e10cSrcweir             if( m_aContext.ColorMode == PDFWriter::DrawGreyscale )
152cdf0e10cSrcweir             {
153cdf0e10cSrcweir                 BmpConversion eConv = BMP_CONVERSION_8BIT_GREYS;
154cdf0e10cSrcweir                 int nDepth = aBitmapEx.GetBitmap().GetBitCount();
155cdf0e10cSrcweir                 if( nDepth <= 4 )
156cdf0e10cSrcweir                     eConv = BMP_CONVERSION_4BIT_GREYS;
157cdf0e10cSrcweir                 if( nDepth > 1 )
158cdf0e10cSrcweir                     aBitmapEx.Convert( eConv );
159cdf0e10cSrcweir             }
160cdf0e10cSrcweir 			sal_Bool bUseJPGCompression = !i_rContext.m_bOnlyLosslessCompression;
161cdf0e10cSrcweir 			if ( ( aSizePixel.Width() < 32 ) || ( aSizePixel.Height() < 32 ) )
162cdf0e10cSrcweir 				bUseJPGCompression = sal_False;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 			SvMemoryStream	aStrm;
165cdf0e10cSrcweir 			Bitmap			aMask;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 			bool bTrueColorJPG = true;
168cdf0e10cSrcweir 			if ( bUseJPGCompression )
169cdf0e10cSrcweir 			{
170cdf0e10cSrcweir 				sal_uInt32 nZippedFileSize;		// sj: we will calculate the filesize of a zipped bitmap
17186e1cf34SPedro Giffuni 				{								// to determine if jpeg compression is useful
172cdf0e10cSrcweir 					SvMemoryStream aTemp;
173cdf0e10cSrcweir 					aTemp.SetCompressMode( aTemp.GetCompressMode() | COMPRESSMODE_ZBITMAP );
174cdf0e10cSrcweir 					aTemp.SetVersion( SOFFICE_FILEFORMAT_40 );	// sj: up from version 40 our bitmap stream operator
17545fd3b9aSArmin Le Grand                     WriteDIBBitmapEx(aBitmapEx, aTemp); // is capable of zlib stream compression
176cdf0e10cSrcweir 					aTemp.Seek( STREAM_SEEK_TO_END );
177cdf0e10cSrcweir 					nZippedFileSize = aTemp.Tell();
178cdf0e10cSrcweir 				}
179cdf0e10cSrcweir 				if ( aBitmapEx.IsTransparent() )
180cdf0e10cSrcweir 				{
181cdf0e10cSrcweir 					if ( aBitmapEx.IsAlpha() )
182cdf0e10cSrcweir 						aMask = aBitmapEx.GetAlpha().GetBitmap();
183cdf0e10cSrcweir 					else
184cdf0e10cSrcweir 						aMask = aBitmapEx.GetMask();
185cdf0e10cSrcweir 				}
186cdf0e10cSrcweir 				Graphic			aGraphic( aBitmapEx.GetBitmap() );
187cdf0e10cSrcweir 				sal_Int32		nColorMode = 0;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 				Sequence< PropertyValue > aFilterData( 2 );
190cdf0e10cSrcweir 				aFilterData[ 0 ].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "Quality" ) );
191cdf0e10cSrcweir 				aFilterData[ 0 ].Value <<= sal_Int32(i_rContext.m_nJPEGQuality);
192cdf0e10cSrcweir 				aFilterData[ 1 ].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "ColorMode" ) );
193cdf0e10cSrcweir 				aFilterData[ 1 ].Value <<= nColorMode;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 				try
196cdf0e10cSrcweir 				{
197cdf0e10cSrcweir 					uno::Reference < io::XStream > xStream = new utl::OStreamWrapper( aStrm );
198cdf0e10cSrcweir 					uno::Reference< io::XSeekable > xSeekable( xStream, UNO_QUERY_THROW );
199cdf0e10cSrcweir 					uno::Reference< graphic::XGraphicProvider > xGraphicProvider( ImplGetSVData()->maAppData.mxMSF->createInstance(
200cdf0e10cSrcweir 						OUString::createFromAscii( "com.sun.star.graphic.GraphicProvider" ) ), UNO_QUERY );
201cdf0e10cSrcweir 					if ( xGraphicProvider.is() )
202cdf0e10cSrcweir 					{
203cdf0e10cSrcweir 						uno::Reference< graphic::XGraphic > xGraphic( aGraphic.GetXGraphic() );
204cdf0e10cSrcweir 						uno::Reference < io::XOutputStream > xOut( xStream->getOutputStream() );
205cdf0e10cSrcweir 						rtl::OUString aMimeType( ::rtl::OUString::createFromAscii( "image/jpeg" ) );
206cdf0e10cSrcweir 						uno::Sequence< beans::PropertyValue > aOutMediaProperties( 3 );
207cdf0e10cSrcweir 						aOutMediaProperties[0].Name = ::rtl::OUString::createFromAscii( "OutputStream" );
208cdf0e10cSrcweir 						aOutMediaProperties[0].Value <<= xOut;
209cdf0e10cSrcweir 						aOutMediaProperties[1].Name = ::rtl::OUString::createFromAscii( "MimeType" );
210cdf0e10cSrcweir 						aOutMediaProperties[1].Value <<= aMimeType;
211cdf0e10cSrcweir 						aOutMediaProperties[2].Name = ::rtl::OUString::createFromAscii( "FilterData" );
212cdf0e10cSrcweir 						aOutMediaProperties[2].Value <<= aFilterData;
213cdf0e10cSrcweir 						xGraphicProvider->storeGraphic( xGraphic, aOutMediaProperties );
214cdf0e10cSrcweir 						xOut->flush();
215cdf0e10cSrcweir 						if ( xSeekable->getLength() > nZippedFileSize )
216cdf0e10cSrcweir 						{
217cdf0e10cSrcweir 							bUseJPGCompression = sal_False;
218cdf0e10cSrcweir 						}
219cdf0e10cSrcweir 						else
220cdf0e10cSrcweir 						{
221cdf0e10cSrcweir                             aStrm.Seek( STREAM_SEEK_TO_END );
222cdf0e10cSrcweir 
223cdf0e10cSrcweir                             xSeekable->seek( 0 );
224cdf0e10cSrcweir                             Sequence< PropertyValue > aArgs( 1 );
225cdf0e10cSrcweir                             aArgs[ 0 ].Name = ::rtl::OUString::createFromAscii( "InputStream" );
226cdf0e10cSrcweir                             aArgs[ 0 ].Value <<= xStream;
227cdf0e10cSrcweir                             uno::Reference< XPropertySet > xPropSet( xGraphicProvider->queryGraphicDescriptor( aArgs ) );
228cdf0e10cSrcweir                             if ( xPropSet.is() )
229cdf0e10cSrcweir                             {
230cdf0e10cSrcweir                                 sal_Int16 nBitsPerPixel = 24;
231cdf0e10cSrcweir                                 if ( xPropSet->getPropertyValue( ::rtl::OUString::createFromAscii( "BitsPerPixel" ) ) >>= nBitsPerPixel )
232cdf0e10cSrcweir                                 {
233cdf0e10cSrcweir                                     bTrueColorJPG = nBitsPerPixel != 8;
234cdf0e10cSrcweir                                 }
235cdf0e10cSrcweir                             }
236cdf0e10cSrcweir                         }
237cdf0e10cSrcweir 					}
238cdf0e10cSrcweir 					else
239cdf0e10cSrcweir 					    bUseJPGCompression = sal_False;
240cdf0e10cSrcweir 				}
241cdf0e10cSrcweir 				catch( uno::Exception& )
242cdf0e10cSrcweir 				{
243cdf0e10cSrcweir 				    bUseJPGCompression = sal_False;
244cdf0e10cSrcweir 				}
245cdf0e10cSrcweir 			}
246cdf0e10cSrcweir 			if ( bUseJPGCompression )
247cdf0e10cSrcweir 				m_rOuterFace.DrawJPGBitmap( aStrm, bTrueColorJPG, aSizePixel, Rectangle( aPoint, aSize ), aMask );
248cdf0e10cSrcweir 			else if ( aBitmapEx.IsTransparent() )
249cdf0e10cSrcweir 				m_rOuterFace.DrawBitmapEx( aPoint, aSize, aBitmapEx );
250cdf0e10cSrcweir 			else
251cdf0e10cSrcweir 				m_rOuterFace.DrawBitmap( aPoint, aSize, aBitmapEx.GetBitmap() );
252cdf0e10cSrcweir 		}
253cdf0e10cSrcweir 	}
254cdf0e10cSrcweir }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 
257cdf0e10cSrcweir // -----------------------------------------------------------------------------
258cdf0e10cSrcweir 
playMetafile(const GDIMetaFile & i_rMtf,vcl::PDFExtOutDevData * i_pOutDevData,const vcl::PDFWriter::PlayMetafileContext & i_rContext,VirtualDevice * pDummyVDev)259cdf0e10cSrcweir void PDFWriterImpl::playMetafile( const GDIMetaFile& i_rMtf, vcl::PDFExtOutDevData* i_pOutDevData, const vcl::PDFWriter::PlayMetafileContext& i_rContext, VirtualDevice* pDummyVDev )
260cdf0e10cSrcweir {
261cdf0e10cSrcweir     bool bAssertionFired( false );
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     VirtualDevice* pPrivateDevice = NULL;
264cdf0e10cSrcweir     if( ! pDummyVDev )
265cdf0e10cSrcweir     {
266cdf0e10cSrcweir         pPrivateDevice = pDummyVDev = new VirtualDevice();
267cdf0e10cSrcweir         pDummyVDev->EnableOutput( sal_False );
268cdf0e10cSrcweir         pDummyVDev->SetMapMode( i_rMtf.GetPrefMapMode() );
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir     GDIMetaFile aMtf( i_rMtf );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 	for( sal_uInt32 i = 0, nCount = aMtf.GetActionCount(); i < nCount; )
273cdf0e10cSrcweir 	{
274cdf0e10cSrcweir 		if ( !i_pOutDevData || !i_pOutDevData->PlaySyncPageAct( m_rOuterFace, i ) )
275cdf0e10cSrcweir 		{
276cdf0e10cSrcweir 			const MetaAction*	pAction = aMtf.GetAction( i );
277cdf0e10cSrcweir 			const sal_uInt16		nType = pAction->GetType();
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 			switch( nType )
280cdf0e10cSrcweir 			{
281cdf0e10cSrcweir 				case( META_PIXEL_ACTION	):
282cdf0e10cSrcweir 				{
283cdf0e10cSrcweir 					const MetaPixelAction* pA = (const MetaPixelAction*) pAction;
284cdf0e10cSrcweir 					m_rOuterFace.DrawPixel( pA->GetPoint(), pA->GetColor() );
285cdf0e10cSrcweir 				}
286cdf0e10cSrcweir 				break;
287cdf0e10cSrcweir 
288cdf0e10cSrcweir 				case( META_POINT_ACTION	):
289cdf0e10cSrcweir 				{
290cdf0e10cSrcweir 					const MetaPointAction* pA = (const MetaPointAction*) pAction;
291cdf0e10cSrcweir 					m_rOuterFace.DrawPixel( pA->GetPoint() );
292cdf0e10cSrcweir 				}
293cdf0e10cSrcweir 				break;
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 				case( META_LINE_ACTION ):
296cdf0e10cSrcweir 				{
297cdf0e10cSrcweir 					const MetaLineAction* pA = (const MetaLineAction*) pAction;
298cdf0e10cSrcweir 					if ( pA->GetLineInfo().IsDefault() )
299cdf0e10cSrcweir 						m_rOuterFace.DrawLine( pA->GetStartPoint(), pA->GetEndPoint() );
300cdf0e10cSrcweir 					else
301cdf0e10cSrcweir 						m_rOuterFace.DrawLine( pA->GetStartPoint(), pA->GetEndPoint(), pA->GetLineInfo() );
302cdf0e10cSrcweir 				}
303cdf0e10cSrcweir 				break;
304cdf0e10cSrcweir 
305cdf0e10cSrcweir 				case( META_RECT_ACTION ):
306cdf0e10cSrcweir 				{
307cdf0e10cSrcweir 					const MetaRectAction* pA = (const MetaRectAction*) pAction;
308cdf0e10cSrcweir 					m_rOuterFace.DrawRect( pA->GetRect() );
309cdf0e10cSrcweir 				}
310cdf0e10cSrcweir 				break;
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 				case( META_ROUNDRECT_ACTION	):
313cdf0e10cSrcweir 				{
314cdf0e10cSrcweir 					const MetaRoundRectAction* pA = (const MetaRoundRectAction*) pAction;
315cdf0e10cSrcweir 					m_rOuterFace.DrawRect( pA->GetRect(), pA->GetHorzRound(), pA->GetVertRound() );
316cdf0e10cSrcweir 				}
317cdf0e10cSrcweir 				break;
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 				case( META_ELLIPSE_ACTION ):
320cdf0e10cSrcweir 				{
321cdf0e10cSrcweir 					const MetaEllipseAction* pA = (const MetaEllipseAction*) pAction;
322cdf0e10cSrcweir 					m_rOuterFace.DrawEllipse( pA->GetRect() );
323cdf0e10cSrcweir 				}
324cdf0e10cSrcweir 				break;
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 				case( META_ARC_ACTION ):
327cdf0e10cSrcweir 				{
328cdf0e10cSrcweir 					const MetaArcAction* pA = (const MetaArcAction*) pAction;
329cdf0e10cSrcweir 					m_rOuterFace.DrawArc( pA->GetRect(), pA->GetStartPoint(), pA->GetEndPoint() );
330cdf0e10cSrcweir 				}
331cdf0e10cSrcweir 				break;
332cdf0e10cSrcweir 
333cdf0e10cSrcweir 				case( META_PIE_ACTION ):
334cdf0e10cSrcweir 				{
335cdf0e10cSrcweir 					const MetaArcAction* pA = (const MetaArcAction*) pAction;
336cdf0e10cSrcweir 					m_rOuterFace.DrawPie( pA->GetRect(), pA->GetStartPoint(), pA->GetEndPoint() );
337cdf0e10cSrcweir 				}
338cdf0e10cSrcweir 				break;
339cdf0e10cSrcweir 
340cdf0e10cSrcweir 				case( META_CHORD_ACTION	):
341cdf0e10cSrcweir 				{
342cdf0e10cSrcweir 					const MetaChordAction* pA = (const MetaChordAction*) pAction;
343cdf0e10cSrcweir 					m_rOuterFace.DrawChord( pA->GetRect(), pA->GetStartPoint(), pA->GetEndPoint() );
344cdf0e10cSrcweir 				}
345cdf0e10cSrcweir 				break;
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 				case( META_POLYGON_ACTION ):
348cdf0e10cSrcweir 				{
349cdf0e10cSrcweir 					const MetaPolygonAction* pA = (const MetaPolygonAction*) pAction;
350cdf0e10cSrcweir 					m_rOuterFace.DrawPolygon( pA->GetPolygon() );
351cdf0e10cSrcweir 				}
352cdf0e10cSrcweir 				break;
353cdf0e10cSrcweir 
354cdf0e10cSrcweir 				case( META_POLYLINE_ACTION ):
355cdf0e10cSrcweir     			{
356cdf0e10cSrcweir 					const MetaPolyLineAction* pA = (const MetaPolyLineAction*) pAction;
357cdf0e10cSrcweir 					if ( pA->GetLineInfo().IsDefault() )
358cdf0e10cSrcweir 						m_rOuterFace.DrawPolyLine( pA->GetPolygon() );
359cdf0e10cSrcweir 					else
360cdf0e10cSrcweir 						m_rOuterFace.DrawPolyLine( pA->GetPolygon(), pA->GetLineInfo() );
361cdf0e10cSrcweir 				}
362cdf0e10cSrcweir 				break;
363cdf0e10cSrcweir 
364cdf0e10cSrcweir 				case( META_POLYPOLYGON_ACTION ):
365cdf0e10cSrcweir 				{
366cdf0e10cSrcweir 					const MetaPolyPolygonAction* pA = (const MetaPolyPolygonAction*) pAction;
367cdf0e10cSrcweir 					m_rOuterFace.DrawPolyPolygon( pA->GetPolyPolygon() );
368cdf0e10cSrcweir 				}
369cdf0e10cSrcweir 				break;
370cdf0e10cSrcweir 
371cdf0e10cSrcweir 				case( META_GRADIENT_ACTION ):
372cdf0e10cSrcweir 				{
373cdf0e10cSrcweir 					const MetaGradientAction* pA = (const MetaGradientAction*) pAction;
374cdf0e10cSrcweir 					#ifdef USE_PDFGRADIENTS
375cdf0e10cSrcweir 					m_rOuterFace.DrawGradient( pA->GetRect(), pA->GetGradient() );
376cdf0e10cSrcweir 					#else
377cdf0e10cSrcweir 					const PolyPolygon         aPolyPoly( pA->GetRect() );
378cdf0e10cSrcweir 					implWriteGradient( aPolyPoly, pA->GetGradient(), pDummyVDev, i_rContext );
379cdf0e10cSrcweir 					#endif
380cdf0e10cSrcweir 				}
381cdf0e10cSrcweir 				break;
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 				case( META_GRADIENTEX_ACTION ):
384cdf0e10cSrcweir 				{
385cdf0e10cSrcweir 					const MetaGradientExAction*	pA = (const MetaGradientExAction*) pAction;
386cdf0e10cSrcweir 					#ifdef USE_PDFGRADIENTS
387cdf0e10cSrcweir 					m_rOuterFace.DrawGradient( pA->GetPolyPolygon(), pA->GetGradient() );
388cdf0e10cSrcweir 					#else
389cdf0e10cSrcweir 					implWriteGradient( pA->GetPolyPolygon(), pA->GetGradient(), pDummyVDev, i_rContext );
390cdf0e10cSrcweir 					#endif
391cdf0e10cSrcweir 				}
392cdf0e10cSrcweir 				break;
393cdf0e10cSrcweir 
394cdf0e10cSrcweir 				case META_HATCH_ACTION:
395cdf0e10cSrcweir 				{
396cdf0e10cSrcweir 					const MetaHatchAction*	pA = (const MetaHatchAction*) pAction;
397cdf0e10cSrcweir 					m_rOuterFace.DrawHatch( pA->GetPolyPolygon(), pA->GetHatch() );
398cdf0e10cSrcweir 				}
399cdf0e10cSrcweir 				break;
400cdf0e10cSrcweir 
401cdf0e10cSrcweir 				case( META_TRANSPARENT_ACTION ):
402cdf0e10cSrcweir 				{
403cdf0e10cSrcweir 					const MetaTransparentAction* pA = (const MetaTransparentAction*) pAction;
404cdf0e10cSrcweir 					m_rOuterFace.DrawTransparent( pA->GetPolyPolygon(), pA->GetTransparence() );
405cdf0e10cSrcweir 				}
406cdf0e10cSrcweir 				break;
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 				case( META_FLOATTRANSPARENT_ACTION ):
409cdf0e10cSrcweir 				{
410cdf0e10cSrcweir 					const MetaFloatTransparentAction* pA = (const MetaFloatTransparentAction*) pAction;
411cdf0e10cSrcweir 
412cdf0e10cSrcweir 					GDIMetaFile		aTmpMtf( pA->GetGDIMetaFile() );
413cdf0e10cSrcweir 					const Point&	rPos = pA->GetPoint();
414cdf0e10cSrcweir 					const Size&		rSize= pA->GetSize();
415cdf0e10cSrcweir 					const Gradient&	rTransparenceGradient = pA->GetGradient();
416cdf0e10cSrcweir 
417cdf0e10cSrcweir                     // special case constant alpha value
418cdf0e10cSrcweir                     if( rTransparenceGradient.GetStartColor() == rTransparenceGradient.GetEndColor() )
419cdf0e10cSrcweir                     {
420cdf0e10cSrcweir                         const Color aTransCol( rTransparenceGradient.GetStartColor() );
421cdf0e10cSrcweir                         const sal_uInt16 nTransPercent = aTransCol.GetLuminance() * 100 / 255;
422cdf0e10cSrcweir                         m_rOuterFace.BeginTransparencyGroup();
423cdf0e10cSrcweir                         playMetafile( aTmpMtf, NULL, i_rContext, pDummyVDev );
424cdf0e10cSrcweir                         m_rOuterFace.EndTransparencyGroup( Rectangle( rPos, rSize ), nTransPercent );
425cdf0e10cSrcweir                     }
426cdf0e10cSrcweir                     else
427cdf0e10cSrcweir                     {
428cdf0e10cSrcweir                         const Size	aDstSizeTwip( pDummyVDev->PixelToLogic( pDummyVDev->LogicToPixel( rSize ), MAP_TWIP ) );
4297b6ba143SArmin Le Grand 
4307b6ba143SArmin Le Grand                         // #115962# Always use at least 300 DPI for bitmap conversion of transparence gradients,
4317b6ba143SArmin Le Grand                         // else the quality is not acceptable (see bugdoc as example)
4327b6ba143SArmin Le Grand                         // sal_Int32	nMaxBmpDPI = i_rContext.m_bOnlyLosslessCompression ? 300 : 72;
4337b6ba143SArmin Le Grand                         sal_Int32 nMaxBmpDPI(300);
4347b6ba143SArmin Le Grand 
435cdf0e10cSrcweir                         if( i_rContext.m_nMaxImageResolution > 50 )
436cdf0e10cSrcweir                         {
437cdf0e10cSrcweir                             if ( nMaxBmpDPI > i_rContext.m_nMaxImageResolution )
438cdf0e10cSrcweir                                 nMaxBmpDPI = i_rContext.m_nMaxImageResolution;
439cdf0e10cSrcweir                         }
440cdf0e10cSrcweir                         const sal_Int32	nPixelX = (sal_Int32)((double)aDstSizeTwip.Width() * (double)nMaxBmpDPI / 1440.0);
441cdf0e10cSrcweir                         const sal_Int32 nPixelY = (sal_Int32)((double)aDstSizeTwip.Height() * (double)nMaxBmpDPI / 1440.0);
442cdf0e10cSrcweir                         if ( nPixelX && nPixelY )
443cdf0e10cSrcweir                         {
444cdf0e10cSrcweir                             Size aDstSizePixel( nPixelX, nPixelY );
445cdf0e10cSrcweir                             VirtualDevice* pVDev = new VirtualDevice;
446cdf0e10cSrcweir                             if( pVDev->SetOutputSizePixel( aDstSizePixel ) )
447cdf0e10cSrcweir                             {
448cdf0e10cSrcweir                                 Bitmap			aPaint, aMask;
449cdf0e10cSrcweir                                 AlphaMask		aAlpha;
450cdf0e10cSrcweir                                 Point			aPoint;
451cdf0e10cSrcweir 
452cdf0e10cSrcweir                                 MapMode aMapMode( pDummyVDev->GetMapMode() );
453cdf0e10cSrcweir                                 aMapMode.SetOrigin( aPoint );
454cdf0e10cSrcweir                                 pVDev->SetMapMode( aMapMode );
455cdf0e10cSrcweir                                 Size aDstSize( pVDev->PixelToLogic( aDstSizePixel ) );
456cdf0e10cSrcweir 
457cdf0e10cSrcweir                                 Point	aMtfOrigin( aTmpMtf.GetPrefMapMode().GetOrigin() );
458cdf0e10cSrcweir                                 if ( aMtfOrigin.X() || aMtfOrigin.Y() )
459cdf0e10cSrcweir                                     aTmpMtf.Move( -aMtfOrigin.X(), -aMtfOrigin.Y() );
460cdf0e10cSrcweir                                 double	fScaleX = (double)aDstSize.Width() / (double)aTmpMtf.GetPrefSize().Width();
461cdf0e10cSrcweir                                 double	fScaleY = (double)aDstSize.Height() / (double)aTmpMtf.GetPrefSize().Height();
462cdf0e10cSrcweir                                 if( fScaleX != 1.0 || fScaleY != 1.0 )
463cdf0e10cSrcweir                                     aTmpMtf.Scale( fScaleX, fScaleY );
464cdf0e10cSrcweir                                 aTmpMtf.SetPrefMapMode( aMapMode );
465cdf0e10cSrcweir 
466cdf0e10cSrcweir                                 // create paint bitmap
467cdf0e10cSrcweir                                 aTmpMtf.WindStart();
468cdf0e10cSrcweir                                 aTmpMtf.Play( pVDev, aPoint, aDstSize );
469cdf0e10cSrcweir                                 aTmpMtf.WindStart();
470cdf0e10cSrcweir 
471cdf0e10cSrcweir                                 pVDev->EnableMapMode( sal_False );
472cdf0e10cSrcweir                                 aPaint = pVDev->GetBitmap( aPoint, aDstSizePixel );
473cdf0e10cSrcweir                                 pVDev->EnableMapMode( sal_True );
474cdf0e10cSrcweir 
475cdf0e10cSrcweir                                 // create mask bitmap
476cdf0e10cSrcweir                                 pVDev->SetLineColor( COL_BLACK );
477cdf0e10cSrcweir                                 pVDev->SetFillColor( COL_BLACK );
478cdf0e10cSrcweir                                 pVDev->DrawRect( Rectangle( aPoint, aDstSize ) );
479cdf0e10cSrcweir                                 pVDev->SetDrawMode( DRAWMODE_WHITELINE | DRAWMODE_WHITEFILL | DRAWMODE_WHITETEXT |
480cdf0e10cSrcweir                                                     DRAWMODE_WHITEBITMAP | DRAWMODE_WHITEGRADIENT );
481cdf0e10cSrcweir                                 aTmpMtf.WindStart();
482cdf0e10cSrcweir                                 aTmpMtf.Play( pVDev, aPoint, aDstSize );
483cdf0e10cSrcweir                                 aTmpMtf.WindStart();
484cdf0e10cSrcweir                                 pVDev->EnableMapMode( sal_False );
485cdf0e10cSrcweir                                 aMask = pVDev->GetBitmap( aPoint, aDstSizePixel );
486cdf0e10cSrcweir                                 pVDev->EnableMapMode( sal_True );
487cdf0e10cSrcweir 
488cdf0e10cSrcweir                                 // create alpha mask from gradient
489cdf0e10cSrcweir                                 pVDev->SetDrawMode( DRAWMODE_GRAYGRADIENT );
490cdf0e10cSrcweir                                 pVDev->DrawGradient( Rectangle( aPoint, aDstSize ), rTransparenceGradient );
491cdf0e10cSrcweir                                 pVDev->SetDrawMode( DRAWMODE_DEFAULT );
492cdf0e10cSrcweir                                 pVDev->EnableMapMode( sal_False );
493cdf0e10cSrcweir                                 pVDev->DrawMask( aPoint, aDstSizePixel, aMask, Color( COL_WHITE ) );
494cdf0e10cSrcweir                                 aAlpha = pVDev->GetBitmap( aPoint, aDstSizePixel );
495cdf0e10cSrcweir                                 implWriteBitmapEx( rPos, rSize, BitmapEx( aPaint, aAlpha ), pDummyVDev, i_rContext );
496cdf0e10cSrcweir                             }
497cdf0e10cSrcweir                             delete pVDev;
498cdf0e10cSrcweir                         }
499cdf0e10cSrcweir                     }
500cdf0e10cSrcweir 				}
501cdf0e10cSrcweir 				break;
502cdf0e10cSrcweir 
503cdf0e10cSrcweir 				case( META_EPS_ACTION ):
504cdf0e10cSrcweir 				{
505cdf0e10cSrcweir 					const MetaEPSAction*	pA = (const MetaEPSAction*) pAction;
506cdf0e10cSrcweir 					const GDIMetaFile		aSubstitute( pA->GetSubstitute() );
507cdf0e10cSrcweir 
508cdf0e10cSrcweir 					m_rOuterFace.Push();
509cdf0e10cSrcweir 					pDummyVDev->Push();
510cdf0e10cSrcweir 
511cdf0e10cSrcweir 					MapMode	aMapMode( aSubstitute.GetPrefMapMode() );
512cdf0e10cSrcweir 					Size aOutSize( pDummyVDev->LogicToLogic( pA->GetSize(), pDummyVDev->GetMapMode(), aMapMode ) );
513cdf0e10cSrcweir 					aMapMode.SetScaleX( Fraction( aOutSize.Width(), aSubstitute.GetPrefSize().Width() ) );
514cdf0e10cSrcweir 					aMapMode.SetScaleY( Fraction( aOutSize.Height(), aSubstitute.GetPrefSize().Height() ) );
515cdf0e10cSrcweir 					aMapMode.SetOrigin( pDummyVDev->LogicToLogic( pA->GetPoint(), pDummyVDev->GetMapMode(), aMapMode ) );
516cdf0e10cSrcweir 
517cdf0e10cSrcweir 					m_rOuterFace.SetMapMode( aMapMode );
518cdf0e10cSrcweir 					pDummyVDev->SetMapMode( aMapMode );
519cdf0e10cSrcweir 					playMetafile( aSubstitute, NULL, i_rContext, pDummyVDev );
520cdf0e10cSrcweir 					pDummyVDev->Pop();
521cdf0e10cSrcweir 					m_rOuterFace.Pop();
522cdf0e10cSrcweir 				}
523cdf0e10cSrcweir 				break;
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 				case( META_COMMENT_ACTION ):
526cdf0e10cSrcweir                 if( ! i_rContext.m_bTransparenciesWereRemoved )
527cdf0e10cSrcweir 				{
528cdf0e10cSrcweir 					const MetaCommentAction*	pA = (const MetaCommentAction*) pAction;
529cdf0e10cSrcweir 					String						aSkipComment;
530cdf0e10cSrcweir 
531cdf0e10cSrcweir 					if( pA->GetComment().CompareIgnoreCaseToAscii( "XGRAD_SEQ_BEGIN" ) == COMPARE_EQUAL )
532cdf0e10cSrcweir 					{
533cdf0e10cSrcweir 						const MetaGradientExAction*	pGradAction = NULL;
534cdf0e10cSrcweir 						sal_Bool					bDone = sal_False;
535cdf0e10cSrcweir 
536cdf0e10cSrcweir 						while( !bDone && ( ++i < nCount ) )
537cdf0e10cSrcweir 						{
538cdf0e10cSrcweir 							pAction = aMtf.GetAction( i );
539cdf0e10cSrcweir 
540cdf0e10cSrcweir 							if( pAction->GetType() == META_GRADIENTEX_ACTION )
541cdf0e10cSrcweir 								pGradAction = (const MetaGradientExAction*) pAction;
542cdf0e10cSrcweir 							else if( ( pAction->GetType() == META_COMMENT_ACTION ) &&
543cdf0e10cSrcweir 									( ( (const MetaCommentAction*) pAction )->GetComment().CompareIgnoreCaseToAscii( "XGRAD_SEQ_END" ) == COMPARE_EQUAL ) )
544cdf0e10cSrcweir 							{
545cdf0e10cSrcweir 								bDone = sal_True;
546cdf0e10cSrcweir 							}
547cdf0e10cSrcweir 						}
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 						if( pGradAction )
550cdf0e10cSrcweir 						{
551cdf0e10cSrcweir 						    #if USE_PDFGRADIENTS
552cdf0e10cSrcweir 							m_rOuterFace.DrawGradient( pGradAction->GetPolyPolygon(), pGradAction->GetGradient() );
553cdf0e10cSrcweir 							#else
554cdf0e10cSrcweir 							implWriteGradient( pGradAction->GetPolyPolygon(), pGradAction->GetGradient(), pDummyVDev, i_rContext );
555cdf0e10cSrcweir 							#endif
556cdf0e10cSrcweir 						}
557cdf0e10cSrcweir 					}
558cdf0e10cSrcweir 					else
559cdf0e10cSrcweir 					{
560cdf0e10cSrcweir 						const sal_uInt8* pData = pA->GetData();
561cdf0e10cSrcweir 						if ( pData )
562cdf0e10cSrcweir 						{
563cdf0e10cSrcweir 							SvMemoryStream	aMemStm( (void*)pData, pA->GetDataSize(), STREAM_READ );
564cdf0e10cSrcweir 							sal_Bool		bSkipSequence = sal_False;
565cdf0e10cSrcweir 							ByteString		sSeqEnd;
566cdf0e10cSrcweir 
567cdf0e10cSrcweir 							if( pA->GetComment().Equals( "XPATHSTROKE_SEQ_BEGIN" ) )
568cdf0e10cSrcweir 							{
569cdf0e10cSrcweir 								sSeqEnd = ByteString( "XPATHSTROKE_SEQ_END" );
570cdf0e10cSrcweir 								SvtGraphicStroke aStroke;
571cdf0e10cSrcweir 								aMemStm >> aStroke;
572cdf0e10cSrcweir 
573cdf0e10cSrcweir 								Polygon aPath;
574cdf0e10cSrcweir 								aStroke.getPath( aPath );
575cdf0e10cSrcweir 
576cdf0e10cSrcweir 								PolyPolygon aStartArrow;
577cdf0e10cSrcweir 								PolyPolygon aEndArrow;
578cdf0e10cSrcweir 								double fTransparency( aStroke.getTransparency() );
579cdf0e10cSrcweir 								double fStrokeWidth( aStroke.getStrokeWidth() );
580cdf0e10cSrcweir 								SvtGraphicStroke::DashArray aDashArray;
581cdf0e10cSrcweir 
582cdf0e10cSrcweir 								aStroke.getStartArrow( aStartArrow );
583cdf0e10cSrcweir 								aStroke.getEndArrow( aEndArrow );
584cdf0e10cSrcweir 								aStroke.getDashArray( aDashArray );
585cdf0e10cSrcweir 
586cdf0e10cSrcweir 								bSkipSequence = sal_True;
587cdf0e10cSrcweir 								if ( aStartArrow.Count() || aEndArrow.Count() )
588cdf0e10cSrcweir 									bSkipSequence = sal_False;
589cdf0e10cSrcweir 								if ( aDashArray.size() && ( fStrokeWidth != 0.0 ) && ( fTransparency == 0.0 ) )
590cdf0e10cSrcweir 									bSkipSequence = sal_False;
591cdf0e10cSrcweir 								if ( bSkipSequence )
592cdf0e10cSrcweir 								{
593cdf0e10cSrcweir 									PDFWriter::ExtLineInfo aInfo;
594cdf0e10cSrcweir                                     aInfo.m_fLineWidth      = fStrokeWidth;
595cdf0e10cSrcweir                                     aInfo.m_fTransparency   = fTransparency;
596cdf0e10cSrcweir                                     aInfo.m_fMiterLimit     = aStroke.getMiterLimit();
597cdf0e10cSrcweir                                     switch( aStroke.getCapType() )
598cdf0e10cSrcweir                                     {
599cdf0e10cSrcweir                                         default:
600cdf0e10cSrcweir                                         case SvtGraphicStroke::capButt:   aInfo.m_eCap = PDFWriter::capButt;break;
601cdf0e10cSrcweir                                         case SvtGraphicStroke::capRound:  aInfo.m_eCap = PDFWriter::capRound;break;
602cdf0e10cSrcweir                                         case SvtGraphicStroke::capSquare: aInfo.m_eCap = PDFWriter::capSquare;break;
603cdf0e10cSrcweir                                     }
604cdf0e10cSrcweir                                     switch( aStroke.getJoinType() )
605cdf0e10cSrcweir                                     {
606cdf0e10cSrcweir                                         default:
607cdf0e10cSrcweir                                         case SvtGraphicStroke::joinMiter: aInfo.m_eJoin = PDFWriter::joinMiter;break;
608cdf0e10cSrcweir                                         case SvtGraphicStroke::joinRound: aInfo.m_eJoin = PDFWriter::joinRound;break;
609cdf0e10cSrcweir                                         case SvtGraphicStroke::joinBevel: aInfo.m_eJoin = PDFWriter::joinBevel;break;
610cdf0e10cSrcweir                                         case SvtGraphicStroke::joinNone:
611cdf0e10cSrcweir                                             aInfo.m_eJoin = PDFWriter::joinMiter;
612cdf0e10cSrcweir                                             aInfo.m_fMiterLimit = 0.0;
613cdf0e10cSrcweir                                             break;
614cdf0e10cSrcweir                                     }
615cdf0e10cSrcweir                                     aInfo.m_aDashArray = aDashArray;
616cdf0e10cSrcweir 
617cdf0e10cSrcweir                                     if(SvtGraphicStroke::joinNone == aStroke.getJoinType()
618cdf0e10cSrcweir                                         && fStrokeWidth > 0.0)
619cdf0e10cSrcweir                                     {
620cdf0e10cSrcweir                                         // emulate no edge rounding by handling single edges
621cdf0e10cSrcweir                                         const sal_uInt16 nPoints(aPath.GetSize());
622cdf0e10cSrcweir                                         const bool bCurve(aPath.HasFlags());
623cdf0e10cSrcweir 
624cdf0e10cSrcweir                                         for(sal_uInt16 a(0); a + 1 < nPoints; a++)
625cdf0e10cSrcweir                                         {
626cdf0e10cSrcweir                                             if(bCurve
627cdf0e10cSrcweir                                                 && POLY_NORMAL != aPath.GetFlags(a + 1)
628cdf0e10cSrcweir                                                 && a + 2 < nPoints
629cdf0e10cSrcweir                                                 && POLY_NORMAL != aPath.GetFlags(a + 2)
630cdf0e10cSrcweir                                                 && a + 3 < nPoints)
631cdf0e10cSrcweir                                             {
632cdf0e10cSrcweir                 								const Polygon aSnippet(4,
633cdf0e10cSrcweir                                                     aPath.GetConstPointAry() + a,
634cdf0e10cSrcweir                                                     aPath.GetConstFlagAry() + a);
635cdf0e10cSrcweir                                                 m_rOuterFace.DrawPolyLine( aSnippet, aInfo );
636cdf0e10cSrcweir                                                 a += 2;
637cdf0e10cSrcweir                                             }
638cdf0e10cSrcweir                                             else
639cdf0e10cSrcweir                                             {
640cdf0e10cSrcweir                 								const Polygon aSnippet(2,
641cdf0e10cSrcweir                                                     aPath.GetConstPointAry() + a);
642cdf0e10cSrcweir                                                 m_rOuterFace.DrawPolyLine( aSnippet, aInfo );
643cdf0e10cSrcweir                                             }
644cdf0e10cSrcweir                                         }
645cdf0e10cSrcweir                                     }
646cdf0e10cSrcweir                                     else
647cdf0e10cSrcweir                                     {
648cdf0e10cSrcweir                                         m_rOuterFace.DrawPolyLine( aPath, aInfo );
649cdf0e10cSrcweir                                     }
650cdf0e10cSrcweir 								}
651cdf0e10cSrcweir 							}
652cdf0e10cSrcweir 							else if ( pA->GetComment().Equals( "XPATHFILL_SEQ_BEGIN" ) )
653cdf0e10cSrcweir 							{
654cdf0e10cSrcweir 								sSeqEnd = ByteString( "XPATHFILL_SEQ_END" );
655cdf0e10cSrcweir 								SvtGraphicFill aFill;
656cdf0e10cSrcweir 								aMemStm >> aFill;
657cdf0e10cSrcweir 
658cdf0e10cSrcweir 								if ( ( aFill.getFillType() == SvtGraphicFill::fillSolid ) && ( aFill.getFillRule() == SvtGraphicFill::fillEvenOdd ) )
659cdf0e10cSrcweir 								{
660cdf0e10cSrcweir 									double fTransparency = aFill.getTransparency();
661cdf0e10cSrcweir 									if ( fTransparency == 0.0 )
662cdf0e10cSrcweir 									{
663cdf0e10cSrcweir 										PolyPolygon aPath;
664cdf0e10cSrcweir 										aFill.getPath( aPath );
665cdf0e10cSrcweir 
666cdf0e10cSrcweir 										bSkipSequence = sal_True;
667cdf0e10cSrcweir 										m_rOuterFace.DrawPolyPolygon( aPath );
668cdf0e10cSrcweir 									}
669cdf0e10cSrcweir 									else if ( fTransparency == 1.0 )
670cdf0e10cSrcweir 										bSkipSequence = sal_True;
671cdf0e10cSrcweir 								}
672cdf0e10cSrcweir /* #i81548# removing optimization for fill textures, because most of the texture settings are not
673cdf0e10cSrcweir    exported properly. In OpenOffice 3.1 the drawing layer will support graphic primitives, then it
674cdf0e10cSrcweir    will not be a problem to optimize the filltexture export. But for wysiwyg is more important than
675cdf0e10cSrcweir    filesize.
676cdf0e10cSrcweir                                 else if( aFill.getFillType() == SvtGraphicFill::fillTexture && aFill.isTiling() )
677cdf0e10cSrcweir                                 {
678cdf0e10cSrcweir                                     sal_Int32 nPattern = mnCachePatternId;
679cdf0e10cSrcweir                                     Graphic aPatternGraphic;
680cdf0e10cSrcweir                                     aFill.getGraphic( aPatternGraphic );
681cdf0e10cSrcweir                                     bool bUseCache = false;
682cdf0e10cSrcweir                                     SvtGraphicFill::Transform aPatTransform;
683cdf0e10cSrcweir                                     aFill.getTransform( aPatTransform );
684cdf0e10cSrcweir 
685cdf0e10cSrcweir                                     if(  mnCachePatternId >= 0 )
686cdf0e10cSrcweir                                     {
687cdf0e10cSrcweir                                         SvtGraphicFill::Transform aCacheTransform;
688cdf0e10cSrcweir                                         maCacheFill.getTransform( aCacheTransform );
689cdf0e10cSrcweir                                         if( aCacheTransform.matrix[0] == aPatTransform.matrix[0] &&
690cdf0e10cSrcweir                                             aCacheTransform.matrix[1] == aPatTransform.matrix[1] &&
691cdf0e10cSrcweir                                             aCacheTransform.matrix[2] == aPatTransform.matrix[2] &&
692cdf0e10cSrcweir                                             aCacheTransform.matrix[3] == aPatTransform.matrix[3] &&
693cdf0e10cSrcweir                                             aCacheTransform.matrix[4] == aPatTransform.matrix[4] &&
694cdf0e10cSrcweir                                             aCacheTransform.matrix[5] == aPatTransform.matrix[5]
695cdf0e10cSrcweir                                             )
696cdf0e10cSrcweir                                         {
697cdf0e10cSrcweir                                             Graphic aCacheGraphic;
698cdf0e10cSrcweir                                             maCacheFill.getGraphic( aCacheGraphic );
699cdf0e10cSrcweir                                             if( aCacheGraphic == aPatternGraphic )
700cdf0e10cSrcweir                                                 bUseCache = true;
701cdf0e10cSrcweir                                         }
702cdf0e10cSrcweir                                     }
703cdf0e10cSrcweir 
704cdf0e10cSrcweir                                     if( ! bUseCache )
705cdf0e10cSrcweir                                     {
706cdf0e10cSrcweir 
707cdf0e10cSrcweir                                         // paint graphic to metafile
708cdf0e10cSrcweir                                         GDIMetaFile aPattern;
709cdf0e10cSrcweir                                         pDummyVDev->SetConnectMetaFile( &aPattern );
710cdf0e10cSrcweir                                         pDummyVDev->Push();
711cdf0e10cSrcweir                                         pDummyVDev->SetMapMode( aPatternGraphic.GetPrefMapMode() );
712cdf0e10cSrcweir 
713cdf0e10cSrcweir                                         aPatternGraphic.Draw( &rDummyVDev, Point( 0, 0 ) );
714cdf0e10cSrcweir                                         pDummyVDev->Pop();
715cdf0e10cSrcweir                                         pDummyVDev->SetConnectMetaFile( NULL );
716cdf0e10cSrcweir                                         aPattern.WindStart();
717cdf0e10cSrcweir 
718cdf0e10cSrcweir                                         MapMode	aPatternMapMode( aPatternGraphic.GetPrefMapMode() );
719cdf0e10cSrcweir                                         // prepare pattern from metafile
720cdf0e10cSrcweir                                         Size aPrefSize( aPatternGraphic.GetPrefSize() );
721cdf0e10cSrcweir                                         // FIXME: this magic -1 shouldn't be necessary
722cdf0e10cSrcweir                                         aPrefSize.Width() -= 1;
723cdf0e10cSrcweir                                         aPrefSize.Height() -= 1;
724cdf0e10cSrcweir                                         aPrefSize = m_rOuterFace.GetReferenceDevice()->
725cdf0e10cSrcweir                                             LogicToLogic( aPrefSize,
726cdf0e10cSrcweir                                                           &aPatternMapMode,
727cdf0e10cSrcweir                                                           &m_rOuterFace.GetReferenceDevice()->GetMapMode() );
728cdf0e10cSrcweir                                         // build bounding rectangle of pattern
729cdf0e10cSrcweir                                         Rectangle aBound( Point( 0, 0 ), aPrefSize );
730cdf0e10cSrcweir                                         m_rOuterFace.BeginPattern( aBound );
731cdf0e10cSrcweir                                         m_rOuterFace.Push();
732cdf0e10cSrcweir                                         pDummyVDev->Push();
733cdf0e10cSrcweir                                         m_rOuterFace.SetMapMode( aPatternMapMode );
734cdf0e10cSrcweir                                         pDummyVDev->SetMapMode( aPatternMapMode );
735cdf0e10cSrcweir                                         ImplWriteActions( m_rOuterFace, NULL, aPattern, rDummyVDev );
736cdf0e10cSrcweir                                         pDummyVDev->Pop();
737cdf0e10cSrcweir                                         m_rOuterFace.Pop();
738cdf0e10cSrcweir 
739cdf0e10cSrcweir                                         nPattern = m_rOuterFace.EndPattern( aPatTransform );
740cdf0e10cSrcweir 
741cdf0e10cSrcweir                                         // try some caching and reuse pattern
742cdf0e10cSrcweir                                         mnCachePatternId = nPattern;
743cdf0e10cSrcweir                                         maCacheFill = aFill;
744cdf0e10cSrcweir                                     }
745cdf0e10cSrcweir 
746cdf0e10cSrcweir                                     // draw polypolygon with pattern fill
747cdf0e10cSrcweir                                     PolyPolygon aPath;
748cdf0e10cSrcweir                                     aFill.getPath( aPath );
749cdf0e10cSrcweir                                     m_rOuterFace.DrawPolyPolygon( aPath, nPattern, aFill.getFillRule() == SvtGraphicFill::fillEvenOdd );
750cdf0e10cSrcweir 
751cdf0e10cSrcweir                                     bSkipSequence = sal_True;
752cdf0e10cSrcweir                                 }
753cdf0e10cSrcweir */
754cdf0e10cSrcweir 							}
755cdf0e10cSrcweir 							if ( bSkipSequence )
756cdf0e10cSrcweir 							{
757cdf0e10cSrcweir 								while( ++i < nCount )
758cdf0e10cSrcweir 								{
759cdf0e10cSrcweir 									pAction = aMtf.GetAction( i );
760cdf0e10cSrcweir 									if ( pAction->GetType() == META_COMMENT_ACTION )
761cdf0e10cSrcweir 									{
762cdf0e10cSrcweir 										ByteString sComment( ((MetaCommentAction*)pAction)->GetComment() );
763cdf0e10cSrcweir 										if ( sComment.Equals( sSeqEnd ) )
764cdf0e10cSrcweir 											break;
765cdf0e10cSrcweir 									}
766cdf0e10cSrcweir                                     // #i44496#
767cdf0e10cSrcweir                                     // the replacement action for stroke is a filled rectangle
768cdf0e10cSrcweir                                     // the set fillcolor of the replacement is part of the graphics
769cdf0e10cSrcweir                                     // state and must not be skipped
770cdf0e10cSrcweir                                     else if( pAction->GetType() == META_FILLCOLOR_ACTION )
771cdf0e10cSrcweir                                     {
772cdf0e10cSrcweir                                         const MetaFillColorAction* pMA = (const MetaFillColorAction*) pAction;
773cdf0e10cSrcweir                                         if( pMA->IsSetting() )
774cdf0e10cSrcweir                                             m_rOuterFace.SetFillColor( pMA->GetColor() );
775cdf0e10cSrcweir                                         else
776cdf0e10cSrcweir                                             m_rOuterFace.SetFillColor();
777cdf0e10cSrcweir                                     }
778cdf0e10cSrcweir 								}
779cdf0e10cSrcweir 							}
780cdf0e10cSrcweir 						}
781cdf0e10cSrcweir 					}
782cdf0e10cSrcweir 				}
783cdf0e10cSrcweir 				break;
784cdf0e10cSrcweir 
785cdf0e10cSrcweir 				case( META_BMP_ACTION ):
786cdf0e10cSrcweir 				{
787cdf0e10cSrcweir 					const MetaBmpAction* pA = (const MetaBmpAction*) pAction;
788cdf0e10cSrcweir 					BitmapEx aBitmapEx( pA->GetBitmap() );
789cdf0e10cSrcweir 					Size aSize( OutputDevice::LogicToLogic( aBitmapEx.GetPrefSize(),
790cdf0e10cSrcweir 							aBitmapEx.GetPrefMapMode(), pDummyVDev->GetMapMode() ) );
791cdf0e10cSrcweir                     if( ! ( aSize.Width() && aSize.Height() ) )
792cdf0e10cSrcweir                         aSize = pDummyVDev->PixelToLogic( aBitmapEx.GetSizePixel() );
793cdf0e10cSrcweir 					implWriteBitmapEx( pA->GetPoint(), aSize, aBitmapEx, pDummyVDev, i_rContext );
794cdf0e10cSrcweir 				}
795cdf0e10cSrcweir 				break;
796cdf0e10cSrcweir 
797cdf0e10cSrcweir 				case( META_BMPSCALE_ACTION ):
798cdf0e10cSrcweir 				{
799cdf0e10cSrcweir 					const MetaBmpScaleAction* pA = (const MetaBmpScaleAction*) pAction;
800cdf0e10cSrcweir 					implWriteBitmapEx( pA->GetPoint(), pA->GetSize(), BitmapEx( pA->GetBitmap() ), pDummyVDev, i_rContext );
801cdf0e10cSrcweir 				}
802cdf0e10cSrcweir 				break;
803cdf0e10cSrcweir 
804cdf0e10cSrcweir 				case( META_BMPSCALEPART_ACTION ):
805cdf0e10cSrcweir 				{
806cdf0e10cSrcweir 					const MetaBmpScalePartAction* pA = (const MetaBmpScalePartAction*) pAction;
807cdf0e10cSrcweir 					BitmapEx aBitmapEx( pA->GetBitmap() );
808cdf0e10cSrcweir 					aBitmapEx.Crop( Rectangle( pA->GetSrcPoint(), pA->GetSrcSize() ) );
809cdf0e10cSrcweir 					implWriteBitmapEx( pA->GetDestPoint(), pA->GetDestSize(), aBitmapEx, pDummyVDev, i_rContext );
810cdf0e10cSrcweir 				}
811cdf0e10cSrcweir 				break;
812cdf0e10cSrcweir 
813cdf0e10cSrcweir 				case( META_BMPEX_ACTION	):
814cdf0e10cSrcweir 				{
815cdf0e10cSrcweir 					const MetaBmpExAction*	pA = (const MetaBmpExAction*) pAction;
816cdf0e10cSrcweir 					BitmapEx aBitmapEx( pA->GetBitmapEx() );
817cdf0e10cSrcweir 					Size aSize( OutputDevice::LogicToLogic( aBitmapEx.GetPrefSize(),
818cdf0e10cSrcweir 							aBitmapEx.GetPrefMapMode(), pDummyVDev->GetMapMode() ) );
819cdf0e10cSrcweir 					implWriteBitmapEx( pA->GetPoint(), aSize, aBitmapEx, pDummyVDev, i_rContext );
820cdf0e10cSrcweir 				}
821cdf0e10cSrcweir 				break;
822cdf0e10cSrcweir 
823cdf0e10cSrcweir 				case( META_BMPEXSCALE_ACTION ):
824cdf0e10cSrcweir 				{
825cdf0e10cSrcweir 					const MetaBmpExScaleAction* pA = (const MetaBmpExScaleAction*) pAction;
826cdf0e10cSrcweir 					implWriteBitmapEx( pA->GetPoint(), pA->GetSize(), pA->GetBitmapEx(), pDummyVDev, i_rContext );
827cdf0e10cSrcweir 				}
828cdf0e10cSrcweir 				break;
829cdf0e10cSrcweir 
830cdf0e10cSrcweir 				case( META_BMPEXSCALEPART_ACTION ):
831cdf0e10cSrcweir 				{
832cdf0e10cSrcweir 					const MetaBmpExScalePartAction* pA = (const MetaBmpExScalePartAction*) pAction;
833cdf0e10cSrcweir 					BitmapEx aBitmapEx( pA->GetBitmapEx() );
834cdf0e10cSrcweir 					aBitmapEx.Crop( Rectangle( pA->GetSrcPoint(), pA->GetSrcSize() ) );
835cdf0e10cSrcweir 					implWriteBitmapEx( pA->GetDestPoint(), pA->GetDestSize(), aBitmapEx, pDummyVDev, i_rContext );
836cdf0e10cSrcweir 				}
837cdf0e10cSrcweir 				break;
838cdf0e10cSrcweir 
839cdf0e10cSrcweir 				case( META_MASK_ACTION ):
840cdf0e10cSrcweir 				case( META_MASKSCALE_ACTION	):
841cdf0e10cSrcweir 				case( META_MASKSCALEPART_ACTION	):
842cdf0e10cSrcweir 				{
843cdf0e10cSrcweir 					DBG_ERROR( "MetaMask...Action not supported yet" );
844cdf0e10cSrcweir 				}
845cdf0e10cSrcweir 				break;
846cdf0e10cSrcweir 
847cdf0e10cSrcweir 				case( META_TEXT_ACTION ):
848cdf0e10cSrcweir 				{
849cdf0e10cSrcweir 					const MetaTextAction* pA = (const MetaTextAction*) pAction;
850cdf0e10cSrcweir 					m_rOuterFace.DrawText( pA->GetPoint(), String( pA->GetText(), pA->GetIndex(), pA->GetLen() ) );
851cdf0e10cSrcweir 				}
852cdf0e10cSrcweir 				break;
853cdf0e10cSrcweir 
854cdf0e10cSrcweir 				case( META_TEXTRECT_ACTION ):
855cdf0e10cSrcweir 				{
856cdf0e10cSrcweir 					const MetaTextRectAction* pA = (const MetaTextRectAction*) pAction;
857cdf0e10cSrcweir 					m_rOuterFace.DrawText( pA->GetRect(), String( pA->GetText() ), pA->GetStyle() );
858cdf0e10cSrcweir 				}
859cdf0e10cSrcweir 				break;
860cdf0e10cSrcweir 
861cdf0e10cSrcweir 				case( META_TEXTARRAY_ACTION	):
862cdf0e10cSrcweir 				{
863cdf0e10cSrcweir 					const MetaTextArrayAction* pA = (const MetaTextArrayAction*) pAction;
864cdf0e10cSrcweir 					m_rOuterFace.DrawTextArray( pA->GetPoint(), pA->GetText(), pA->GetDXArray(), pA->GetIndex(), pA->GetLen() );
865cdf0e10cSrcweir 				}
866cdf0e10cSrcweir 				break;
867cdf0e10cSrcweir 
868cdf0e10cSrcweir 				case( META_STRETCHTEXT_ACTION ):
869cdf0e10cSrcweir 				{
870cdf0e10cSrcweir 					const MetaStretchTextAction* pA = (const MetaStretchTextAction*) pAction;
871cdf0e10cSrcweir 					m_rOuterFace.DrawStretchText( pA->GetPoint(), pA->GetWidth(), pA->GetText(), pA->GetIndex(), pA->GetLen() );
872cdf0e10cSrcweir 				}
873cdf0e10cSrcweir 				break;
874cdf0e10cSrcweir 
875cdf0e10cSrcweir 
876cdf0e10cSrcweir 				case( META_TEXTLINE_ACTION ):
877cdf0e10cSrcweir 				{
878cdf0e10cSrcweir 					const MetaTextLineAction* pA = (const MetaTextLineAction*) pAction;
879cdf0e10cSrcweir 					m_rOuterFace.DrawTextLine( pA->GetStartPoint(), pA->GetWidth(), pA->GetStrikeout(), pA->GetUnderline(), pA->GetOverline() );
880cdf0e10cSrcweir 
881cdf0e10cSrcweir 				}
882cdf0e10cSrcweir 				break;
883cdf0e10cSrcweir 
884cdf0e10cSrcweir 				case( META_CLIPREGION_ACTION ):
885cdf0e10cSrcweir 				{
886cdf0e10cSrcweir 					const MetaClipRegionAction* pA = (const MetaClipRegionAction*) pAction;
887cdf0e10cSrcweir 
888cdf0e10cSrcweir 					if( pA->IsClipping() )
889cdf0e10cSrcweir 					{
890cdf0e10cSrcweir 					    if( pA->GetRegion().IsEmpty() )
891cdf0e10cSrcweir 					        m_rOuterFace.SetClipRegion( basegfx::B2DPolyPolygon() );
892cdf0e10cSrcweir 					    else
893cdf0e10cSrcweir 					    {
894cdf0e10cSrcweir 					        Region aReg( pA->GetRegion() );
895e6f63103SArmin Le Grand 					        m_rOuterFace.SetClipRegion( aReg.GetAsB2DPolyPolygon() );
896cdf0e10cSrcweir 					    }
897cdf0e10cSrcweir 					}
898cdf0e10cSrcweir 					else
899cdf0e10cSrcweir 						m_rOuterFace.SetClipRegion();
900cdf0e10cSrcweir 				}
901cdf0e10cSrcweir 				break;
902cdf0e10cSrcweir 
903cdf0e10cSrcweir 				case( META_ISECTRECTCLIPREGION_ACTION ):
904cdf0e10cSrcweir 				{
905cdf0e10cSrcweir 					const MetaISectRectClipRegionAction* pA = (const MetaISectRectClipRegionAction*) pAction;
906cdf0e10cSrcweir 					m_rOuterFace.IntersectClipRegion( pA->GetRect() );
907cdf0e10cSrcweir 				}
908cdf0e10cSrcweir 				break;
909cdf0e10cSrcweir 
910cdf0e10cSrcweir 				case( META_ISECTREGIONCLIPREGION_ACTION	):
911cdf0e10cSrcweir 				{
912cdf0e10cSrcweir 				    const MetaISectRegionClipRegionAction* pA = (const MetaISectRegionClipRegionAction*) pAction;
913cdf0e10cSrcweir 				    Region aReg( pA->GetRegion() );
914e6f63103SArmin Le Grand 				    m_rOuterFace.IntersectClipRegion( aReg.GetAsB2DPolyPolygon() );
915cdf0e10cSrcweir 				}
916cdf0e10cSrcweir 				break;
917cdf0e10cSrcweir 
918cdf0e10cSrcweir 				case( META_MOVECLIPREGION_ACTION ):
919cdf0e10cSrcweir 				{
920cdf0e10cSrcweir 					const MetaMoveClipRegionAction* pA = (const MetaMoveClipRegionAction*) pAction;
921cdf0e10cSrcweir 					m_rOuterFace.MoveClipRegion( pA->GetHorzMove(), pA->GetVertMove() );
922cdf0e10cSrcweir 				}
923cdf0e10cSrcweir 				break;
924cdf0e10cSrcweir 
925cdf0e10cSrcweir 				case( META_MAPMODE_ACTION ):
926cdf0e10cSrcweir 				{
927cdf0e10cSrcweir 					const_cast< MetaAction* >( pAction )->Execute( pDummyVDev );
928cdf0e10cSrcweir 					m_rOuterFace.SetMapMode( pDummyVDev->GetMapMode() );
929cdf0e10cSrcweir 				}
930cdf0e10cSrcweir 				break;
931cdf0e10cSrcweir 
932cdf0e10cSrcweir 				case( META_LINECOLOR_ACTION	):
933cdf0e10cSrcweir 				{
934cdf0e10cSrcweir 					const MetaLineColorAction* pA = (const MetaLineColorAction*) pAction;
935cdf0e10cSrcweir 
936cdf0e10cSrcweir 					if( pA->IsSetting() )
937cdf0e10cSrcweir 						m_rOuterFace.SetLineColor( pA->GetColor() );
938cdf0e10cSrcweir 					else
939cdf0e10cSrcweir 						m_rOuterFace.SetLineColor();
940cdf0e10cSrcweir 				}
941cdf0e10cSrcweir 				break;
942cdf0e10cSrcweir 
943cdf0e10cSrcweir 				case( META_FILLCOLOR_ACTION	):
944cdf0e10cSrcweir 				{
945cdf0e10cSrcweir 					const MetaFillColorAction* pA = (const MetaFillColorAction*) pAction;
946cdf0e10cSrcweir 
947cdf0e10cSrcweir 					if( pA->IsSetting() )
948cdf0e10cSrcweir 						m_rOuterFace.SetFillColor( pA->GetColor() );
949cdf0e10cSrcweir 					else
950cdf0e10cSrcweir 						m_rOuterFace.SetFillColor();
951cdf0e10cSrcweir 				}
952cdf0e10cSrcweir 				break;
953cdf0e10cSrcweir 
954cdf0e10cSrcweir 				case( META_TEXTLINECOLOR_ACTION ):
955cdf0e10cSrcweir 				{
956cdf0e10cSrcweir 					const MetaTextLineColorAction* pA = (const MetaTextLineColorAction*) pAction;
957cdf0e10cSrcweir 
958cdf0e10cSrcweir 					if( pA->IsSetting() )
959cdf0e10cSrcweir 						m_rOuterFace.SetTextLineColor( pA->GetColor() );
960cdf0e10cSrcweir 					else
961cdf0e10cSrcweir 						m_rOuterFace.SetTextLineColor();
962cdf0e10cSrcweir 				}
963cdf0e10cSrcweir 				break;
964cdf0e10cSrcweir 
965cdf0e10cSrcweir 				case( META_OVERLINECOLOR_ACTION ):
966cdf0e10cSrcweir 				{
967cdf0e10cSrcweir 					const MetaOverlineColorAction* pA = (const MetaOverlineColorAction*) pAction;
968cdf0e10cSrcweir 
969cdf0e10cSrcweir 					if( pA->IsSetting() )
970cdf0e10cSrcweir 						m_rOuterFace.SetOverlineColor( pA->GetColor() );
971cdf0e10cSrcweir 					else
972cdf0e10cSrcweir 						m_rOuterFace.SetOverlineColor();
973cdf0e10cSrcweir 				}
974cdf0e10cSrcweir 				break;
975cdf0e10cSrcweir 
976cdf0e10cSrcweir 				case( META_TEXTFILLCOLOR_ACTION	):
977cdf0e10cSrcweir 				{
978cdf0e10cSrcweir 					const MetaTextFillColorAction* pA = (const MetaTextFillColorAction*) pAction;
979cdf0e10cSrcweir 
980cdf0e10cSrcweir 					if( pA->IsSetting() )
981cdf0e10cSrcweir 						m_rOuterFace.SetTextFillColor( pA->GetColor() );
982cdf0e10cSrcweir 					else
983cdf0e10cSrcweir 						m_rOuterFace.SetTextFillColor();
984cdf0e10cSrcweir 				}
985cdf0e10cSrcweir 				break;
986cdf0e10cSrcweir 
987cdf0e10cSrcweir 				case( META_TEXTCOLOR_ACTION	):
988cdf0e10cSrcweir 				{
989cdf0e10cSrcweir 					const MetaTextColorAction* pA = (const MetaTextColorAction*) pAction;
990cdf0e10cSrcweir 					m_rOuterFace.SetTextColor( pA->GetColor() );
991cdf0e10cSrcweir 				}
992cdf0e10cSrcweir 				break;
993cdf0e10cSrcweir 
994cdf0e10cSrcweir 				case( META_TEXTALIGN_ACTION	):
995cdf0e10cSrcweir 				{
996cdf0e10cSrcweir 					const MetaTextAlignAction* pA = (const MetaTextAlignAction*) pAction;
997cdf0e10cSrcweir 					m_rOuterFace.SetTextAlign( pA->GetTextAlign() );
998cdf0e10cSrcweir 				}
999cdf0e10cSrcweir 				break;
1000cdf0e10cSrcweir 
1001cdf0e10cSrcweir 				case( META_FONT_ACTION ):
1002cdf0e10cSrcweir 				{
1003cdf0e10cSrcweir 					const MetaFontAction* pA = (const MetaFontAction*) pAction;
1004cdf0e10cSrcweir 					m_rOuterFace.SetFont( pA->GetFont() );
1005cdf0e10cSrcweir 				}
1006cdf0e10cSrcweir 				break;
1007cdf0e10cSrcweir 
1008cdf0e10cSrcweir 				case( META_PUSH_ACTION ):
1009cdf0e10cSrcweir 				{
1010cdf0e10cSrcweir 					const MetaPushAction* pA = (const MetaPushAction*) pAction;
1011cdf0e10cSrcweir 
1012cdf0e10cSrcweir 					pDummyVDev->Push( pA->GetFlags() );
1013cdf0e10cSrcweir 					m_rOuterFace.Push( pA->GetFlags() );
1014cdf0e10cSrcweir 				}
1015cdf0e10cSrcweir 				break;
1016cdf0e10cSrcweir 
1017cdf0e10cSrcweir 				case( META_POP_ACTION ):
1018cdf0e10cSrcweir 				{
1019cdf0e10cSrcweir 					pDummyVDev->Pop();
1020cdf0e10cSrcweir 					m_rOuterFace.Pop();
1021cdf0e10cSrcweir 				}
1022cdf0e10cSrcweir 				break;
1023cdf0e10cSrcweir 
1024cdf0e10cSrcweir 				case( META_LAYOUTMODE_ACTION ):
1025cdf0e10cSrcweir 				{
1026cdf0e10cSrcweir 					const MetaLayoutModeAction* pA = (const MetaLayoutModeAction*) pAction;
1027cdf0e10cSrcweir 					m_rOuterFace.SetLayoutMode( pA->GetLayoutMode() );
1028cdf0e10cSrcweir 				}
1029cdf0e10cSrcweir 				break;
1030cdf0e10cSrcweir 
1031cdf0e10cSrcweir 				case META_TEXTLANGUAGE_ACTION:
1032cdf0e10cSrcweir 				{
1033cdf0e10cSrcweir 					const  MetaTextLanguageAction* pA = (const MetaTextLanguageAction*) pAction;
1034cdf0e10cSrcweir                     m_rOuterFace.SetDigitLanguage( pA->GetTextLanguage() );
1035cdf0e10cSrcweir 				}
1036cdf0e10cSrcweir 				break;
1037cdf0e10cSrcweir 
1038cdf0e10cSrcweir 				case( META_WALLPAPER_ACTION	):
1039cdf0e10cSrcweir 				{
1040cdf0e10cSrcweir 					const MetaWallpaperAction* pA = (const MetaWallpaperAction*) pAction;
1041cdf0e10cSrcweir 					m_rOuterFace.DrawWallpaper( pA->GetRect(), pA->GetWallpaper() );
1042cdf0e10cSrcweir 				}
1043cdf0e10cSrcweir 				break;
1044cdf0e10cSrcweir 
1045cdf0e10cSrcweir 				case( META_RASTEROP_ACTION ):
1046cdf0e10cSrcweir 				{
1047cdf0e10cSrcweir 					// !!! >>> we don't want to support this actions
1048cdf0e10cSrcweir 				}
1049cdf0e10cSrcweir 				break;
1050cdf0e10cSrcweir 
1051cdf0e10cSrcweir 				case( META_REFPOINT_ACTION ):
1052cdf0e10cSrcweir 				{
1053cdf0e10cSrcweir 					// !!! >>> we don't want to support this actions
1054cdf0e10cSrcweir 				}
1055cdf0e10cSrcweir 				break;
1056cdf0e10cSrcweir 
1057cdf0e10cSrcweir 				default:
1058cdf0e10cSrcweir 					// #i24604# Made assertion fire only once per
1059cdf0e10cSrcweir 					// metafile. The asserted actions here are all
1060cdf0e10cSrcweir 					// deprecated
1061cdf0e10cSrcweir 					if( !bAssertionFired )
1062cdf0e10cSrcweir 					{
1063cdf0e10cSrcweir 						bAssertionFired = true;
1064cdf0e10cSrcweir 						DBG_ERROR( "PDFExport::ImplWriteActions: deprecated and unsupported MetaAction encountered" );
1065cdf0e10cSrcweir 					}
1066cdf0e10cSrcweir 				break;
1067cdf0e10cSrcweir 			}
1068cdf0e10cSrcweir 			i++;
1069cdf0e10cSrcweir 		}
1070cdf0e10cSrcweir 	}
1071cdf0e10cSrcweir 
1072cdf0e10cSrcweir     delete pPrivateDevice;
1073cdf0e10cSrcweir }
1074cdf0e10cSrcweir 
1075cdf0e10cSrcweir // Encryption methods
1076cdf0e10cSrcweir 
1077cdf0e10cSrcweir /* a crutch to transport an rtlDigest safely though UNO API
1078cdf0e10cSrcweir    this is needed for the PDF export dialog, which otherwise would have to pass
1079cdf0e10cSrcweir    clear text passwords down till they can be used in PDFWriter. Unfortunately
1080cdf0e10cSrcweir    the MD5 sum of the password (which is needed to create the PDF encryption key)
1081cdf0e10cSrcweir    is not sufficient, since an rtl MD5 digest cannot be created in an arbitrary state
1082cdf0e10cSrcweir    which would be needed in PDFWriterImpl::computeEncryptionKey.
1083cdf0e10cSrcweir */
1084cdf0e10cSrcweir class EncHashTransporter : public cppu::WeakImplHelper1 < com::sun::star::beans::XMaterialHolder >
1085cdf0e10cSrcweir {
1086cdf0e10cSrcweir     rtlDigest                   maUDigest;
1087cdf0e10cSrcweir     sal_IntPtr                  maID;
1088cdf0e10cSrcweir     std::vector< sal_uInt8 >    maOValue;
1089cdf0e10cSrcweir 
1090cdf0e10cSrcweir     static std::map< sal_IntPtr, EncHashTransporter* >      sTransporters;
1091cdf0e10cSrcweir public:
EncHashTransporter()1092cdf0e10cSrcweir     EncHashTransporter()
1093cdf0e10cSrcweir     : maUDigest( rtl_digest_createMD5() )
1094cdf0e10cSrcweir     {
1095cdf0e10cSrcweir         maID = reinterpret_cast< sal_IntPtr >(this);
1096cdf0e10cSrcweir         while( sTransporters.find( maID ) != sTransporters.end() ) // paranoia mode
1097cdf0e10cSrcweir             maID++;
1098cdf0e10cSrcweir         sTransporters[ maID ] = this;
1099cdf0e10cSrcweir     }
1100cdf0e10cSrcweir 
~EncHashTransporter()1101cdf0e10cSrcweir     virtual ~EncHashTransporter()
1102cdf0e10cSrcweir     {
1103cdf0e10cSrcweir         sTransporters.erase( maID );
1104cdf0e10cSrcweir         if( maUDigest )
1105cdf0e10cSrcweir             rtl_digest_destroyMD5( maUDigest );
1106cdf0e10cSrcweir         OSL_TRACE( "EncHashTransporter freed\n" );
1107cdf0e10cSrcweir     }
1108cdf0e10cSrcweir 
getUDigest() const1109cdf0e10cSrcweir     rtlDigest getUDigest() const { return maUDigest; };
getOValue()1110cdf0e10cSrcweir     std::vector< sal_uInt8 >& getOValue() { return maOValue; }
invalidate()1111cdf0e10cSrcweir     void invalidate()
1112cdf0e10cSrcweir     {
1113cdf0e10cSrcweir         if( maUDigest )
1114cdf0e10cSrcweir         {
1115cdf0e10cSrcweir             rtl_digest_destroyMD5( maUDigest );
1116cdf0e10cSrcweir             maUDigest = NULL;
1117cdf0e10cSrcweir         }
1118cdf0e10cSrcweir     }
1119cdf0e10cSrcweir 
1120cdf0e10cSrcweir     // XMaterialHolder
getMaterial()1121cdf0e10cSrcweir     virtual uno::Any SAL_CALL getMaterial() throw()
1122cdf0e10cSrcweir     {
1123cdf0e10cSrcweir         return uno::makeAny( sal_Int64(maID) );
1124cdf0e10cSrcweir     }
1125cdf0e10cSrcweir 
1126cdf0e10cSrcweir     static EncHashTransporter* getEncHashTransporter( const uno::Reference< beans::XMaterialHolder >& );
1127cdf0e10cSrcweir 
1128cdf0e10cSrcweir };
1129cdf0e10cSrcweir 
1130cdf0e10cSrcweir std::map< sal_IntPtr, EncHashTransporter* > EncHashTransporter::sTransporters;
1131cdf0e10cSrcweir 
getEncHashTransporter(const uno::Reference<beans::XMaterialHolder> & xRef)1132cdf0e10cSrcweir EncHashTransporter* EncHashTransporter::getEncHashTransporter( const uno::Reference< beans::XMaterialHolder >& xRef )
1133cdf0e10cSrcweir {
1134cdf0e10cSrcweir     EncHashTransporter* pResult = NULL;
1135cdf0e10cSrcweir     if( xRef.is() )
1136cdf0e10cSrcweir     {
1137cdf0e10cSrcweir         uno::Any aMat( xRef->getMaterial() );
1138cdf0e10cSrcweir         sal_Int64 nMat = 0;
1139cdf0e10cSrcweir         if( aMat >>= nMat )
1140cdf0e10cSrcweir         {
1141cdf0e10cSrcweir             std::map< sal_IntPtr, EncHashTransporter* >::iterator it = sTransporters.find( static_cast<sal_IntPtr>(nMat) );
1142cdf0e10cSrcweir             if( it != sTransporters.end() )
1143cdf0e10cSrcweir                 pResult = it->second;
1144cdf0e10cSrcweir         }
1145cdf0e10cSrcweir     }
1146cdf0e10cSrcweir     return pResult;
1147cdf0e10cSrcweir }
1148cdf0e10cSrcweir 
checkEncryptionBufferSize(sal_Int32 newSize)1149c1e8cc3aSDon Lewis sal_Bool PDFWriterImpl::checkEncryptionBufferSize( sal_Int32 newSize )
1150cdf0e10cSrcweir {
1151cdf0e10cSrcweir     if( m_nEncryptionBufferSize < newSize )
1152cdf0e10cSrcweir     {
1153cdf0e10cSrcweir         /* reallocate the buffer, the used function allocate as rtl_allocateMemory
1154cdf0e10cSrcweir         if the pointer parameter is NULL */
1155cdf0e10cSrcweir         m_pEncryptionBuffer = (sal_uInt8*)rtl_reallocateMemory( m_pEncryptionBuffer, newSize );
1156cdf0e10cSrcweir         if( m_pEncryptionBuffer )
1157cdf0e10cSrcweir             m_nEncryptionBufferSize = newSize;
1158cdf0e10cSrcweir         else
1159cdf0e10cSrcweir             m_nEncryptionBufferSize = 0;
1160cdf0e10cSrcweir     }
1161cdf0e10cSrcweir     return ( m_nEncryptionBufferSize != 0 );
1162cdf0e10cSrcweir }
1163cdf0e10cSrcweir 
checkAndEnableStreamEncryption(sal_Int32 nObject)1164c1e8cc3aSDon Lewis void PDFWriterImpl::checkAndEnableStreamEncryption( sal_Int32 nObject )
1165cdf0e10cSrcweir {
1166cdf0e10cSrcweir     if( m_aContext.Encryption.Encrypt() )
1167cdf0e10cSrcweir     {
1168cdf0e10cSrcweir         m_bEncryptThisStream = true;
1169cdf0e10cSrcweir         sal_Int32 i = m_nKeyLength;
1170cdf0e10cSrcweir         m_aContext.Encryption.EncryptionKey[i++] = (sal_uInt8)nObject;
1171cdf0e10cSrcweir         m_aContext.Encryption.EncryptionKey[i++] = (sal_uInt8)( nObject >> 8 );
1172cdf0e10cSrcweir         m_aContext.Encryption.EncryptionKey[i++] = (sal_uInt8)( nObject >> 16 );
1173cdf0e10cSrcweir         //the other location of m_nEncryptionKey are already set to 0, our fixed generation number
1174cdf0e10cSrcweir         // do the MD5 hash
1175cdf0e10cSrcweir         sal_uInt8 nMD5Sum[ RTL_DIGEST_LENGTH_MD5 ];
1176cdf0e10cSrcweir         // the i+2 to take into account the generation number, always zero
1177cdf0e10cSrcweir         rtl_digest_MD5( &m_aContext.Encryption.EncryptionKey[0], i+2, nMD5Sum, sizeof(nMD5Sum) );
1178cdf0e10cSrcweir         // initialize the RC4 with the key
1179*f228387cSmseidel         // key length: see algorithm 3.1, step 4: (N+5) max 16
1180cdf0e10cSrcweir         rtl_cipher_initARCFOUR( m_aCipher, rtl_Cipher_DirectionEncode, nMD5Sum, m_nRC4KeyLength, NULL, 0 );
1181cdf0e10cSrcweir     }
1182cdf0e10cSrcweir }
1183cdf0e10cSrcweir 
enableStringEncryption(sal_Int32 nObject)1184c1e8cc3aSDon Lewis void PDFWriterImpl::enableStringEncryption( sal_Int32 nObject )
1185cdf0e10cSrcweir {
1186cdf0e10cSrcweir     if( m_aContext.Encryption.Encrypt() )
1187cdf0e10cSrcweir     {
1188cdf0e10cSrcweir         sal_Int32 i = m_nKeyLength;
1189cdf0e10cSrcweir         m_aContext.Encryption.EncryptionKey[i++] = (sal_uInt8)nObject;
1190cdf0e10cSrcweir         m_aContext.Encryption.EncryptionKey[i++] = (sal_uInt8)( nObject >> 8 );
1191cdf0e10cSrcweir         m_aContext.Encryption.EncryptionKey[i++] = (sal_uInt8)( nObject >> 16 );
1192cdf0e10cSrcweir         //the other location of m_nEncryptionKey are already set to 0, our fixed generation number
1193cdf0e10cSrcweir         // do the MD5 hash
1194cdf0e10cSrcweir         sal_uInt8 nMD5Sum[ RTL_DIGEST_LENGTH_MD5 ];
1195cdf0e10cSrcweir         // the i+2 to take into account the generation number, always zero
1196cdf0e10cSrcweir         rtl_digest_MD5( &m_aContext.Encryption.EncryptionKey[0], i+2, nMD5Sum, sizeof(nMD5Sum) );
1197cdf0e10cSrcweir         // initialize the RC4 with the key
1198*f228387cSmseidel         // key length: see algorithm 3.1, step 4: (N+5) max 16
1199cdf0e10cSrcweir         rtl_cipher_initARCFOUR( m_aCipher, rtl_Cipher_DirectionEncode, nMD5Sum, m_nRC4KeyLength, NULL, 0 );
1200cdf0e10cSrcweir     }
1201cdf0e10cSrcweir }
1202cdf0e10cSrcweir 
1203cdf0e10cSrcweir /* init the encryption engine
1204cdf0e10cSrcweir 1. init the document id, used both for building the document id and for building the encryption key(s)
1205cdf0e10cSrcweir 2. build the encryption key following algorithms described in the PDF specification
1206cdf0e10cSrcweir  */
initEncryption(const rtl::OUString & i_rOwnerPassword,const rtl::OUString & i_rUserPassword,bool b128Bit)1207cdf0e10cSrcweir uno::Reference< beans::XMaterialHolder > PDFWriterImpl::initEncryption( const rtl::OUString& i_rOwnerPassword,
1208cdf0e10cSrcweir                                                                         const rtl::OUString& i_rUserPassword,
1209cdf0e10cSrcweir                                                                         bool b128Bit
1210cdf0e10cSrcweir                                                                         )
1211cdf0e10cSrcweir {
1212cdf0e10cSrcweir     uno::Reference< beans::XMaterialHolder > xResult;
1213cdf0e10cSrcweir     if( i_rOwnerPassword.getLength() || i_rUserPassword.getLength() )
1214cdf0e10cSrcweir     {
1215cdf0e10cSrcweir         EncHashTransporter* pTransporter = new EncHashTransporter;
1216cdf0e10cSrcweir         xResult = pTransporter;
1217cdf0e10cSrcweir 
1218cdf0e10cSrcweir         // get padded passwords
1219cdf0e10cSrcweir         sal_uInt8 aPadUPW[ENCRYPTED_PWD_SIZE], aPadOPW[ENCRYPTED_PWD_SIZE];
1220cdf0e10cSrcweir         padPassword( i_rOwnerPassword.getLength() ? i_rOwnerPassword : i_rUserPassword, aPadOPW );
1221cdf0e10cSrcweir         padPassword( i_rUserPassword, aPadUPW );
1222cdf0e10cSrcweir         sal_Int32 nKeyLength = SECUR_40BIT_KEY;
1223cdf0e10cSrcweir         if( b128Bit )
1224cdf0e10cSrcweir             nKeyLength = SECUR_128BIT_KEY;
1225cdf0e10cSrcweir 
1226cdf0e10cSrcweir         if( computeODictionaryValue( aPadOPW, aPadUPW, pTransporter->getOValue(), nKeyLength ) )
1227cdf0e10cSrcweir         {
1228cdf0e10cSrcweir             rtlDigest aDig = pTransporter->getUDigest();
1229cdf0e10cSrcweir             if( rtl_digest_updateMD5( aDig, aPadUPW, ENCRYPTED_PWD_SIZE ) != rtl_Digest_E_None )
1230cdf0e10cSrcweir                 xResult.clear();
1231cdf0e10cSrcweir         }
1232cdf0e10cSrcweir         else
1233cdf0e10cSrcweir             xResult.clear();
1234cdf0e10cSrcweir 
1235cdf0e10cSrcweir         // trash temporary padded cleartext PWDs
1236cdf0e10cSrcweir         rtl_zeroMemory( aPadOPW, sizeof(aPadOPW) );
1237cdf0e10cSrcweir         rtl_zeroMemory( aPadUPW, sizeof(aPadUPW) );
1238cdf0e10cSrcweir 
1239cdf0e10cSrcweir     }
1240cdf0e10cSrcweir     return xResult;
1241cdf0e10cSrcweir }
1242cdf0e10cSrcweir 
prepareEncryption(const uno::Reference<beans::XMaterialHolder> & xEnc)1243cdf0e10cSrcweir bool PDFWriterImpl::prepareEncryption( const uno::Reference< beans::XMaterialHolder >& xEnc )
1244cdf0e10cSrcweir {
1245cdf0e10cSrcweir     bool bSuccess = false;
1246cdf0e10cSrcweir     EncHashTransporter* pTransporter = EncHashTransporter::getEncHashTransporter( xEnc );
1247cdf0e10cSrcweir     if( pTransporter )
1248cdf0e10cSrcweir     {
1249cdf0e10cSrcweir         sal_Int32 nKeyLength = 0, nRC4KeyLength = 0;
1250cdf0e10cSrcweir         sal_Int32 nAccessPermissions = computeAccessPermissions( m_aContext.Encryption, nKeyLength, nRC4KeyLength );
1251cdf0e10cSrcweir         m_aContext.Encryption.OValue = pTransporter->getOValue();
1252cdf0e10cSrcweir         bSuccess = computeUDictionaryValue( pTransporter, m_aContext.Encryption, nKeyLength, nAccessPermissions );
1253cdf0e10cSrcweir     }
1254cdf0e10cSrcweir     if( ! bSuccess )
1255cdf0e10cSrcweir     {
1256cdf0e10cSrcweir         m_aContext.Encryption.OValue.clear();
1257cdf0e10cSrcweir         m_aContext.Encryption.UValue.clear();
1258cdf0e10cSrcweir         m_aContext.Encryption.EncryptionKey.clear();
1259cdf0e10cSrcweir     }
1260cdf0e10cSrcweir     return bSuccess;
1261cdf0e10cSrcweir }
1262cdf0e10cSrcweir 
computeAccessPermissions(const vcl::PDFWriter::PDFEncryptionProperties & i_rProperties,sal_Int32 & o_rKeyLength,sal_Int32 & o_rRC4KeyLength)1263cdf0e10cSrcweir sal_Int32 PDFWriterImpl::computeAccessPermissions( const vcl::PDFWriter::PDFEncryptionProperties& i_rProperties,
1264cdf0e10cSrcweir 	                                               sal_Int32& o_rKeyLength, sal_Int32& o_rRC4KeyLength )
1265cdf0e10cSrcweir {
1266cdf0e10cSrcweir     /*
1267cdf0e10cSrcweir     2) compute the access permissions, in numerical form
1268cdf0e10cSrcweir 
1269cdf0e10cSrcweir     the default value depends on the revision 2 (40 bit) or 3 (128 bit security):
1270cdf0e10cSrcweir     - for 40 bit security the unused bit must be set to 1, since they are not used
1271cdf0e10cSrcweir     - for 128 bit security the same bit must be preset to 0 and set later if needed
1272cdf0e10cSrcweir     according to the table 3.15, pdf v 1.4 */
1273cdf0e10cSrcweir     sal_Int32 nAccessPermissions = ( i_rProperties.Security128bit ) ? 0xfffff0c0 : 0xffffffc0 ;
1274cdf0e10cSrcweir 
1275cdf0e10cSrcweir     /* check permissions for 40 bit security case */
1276cdf0e10cSrcweir     nAccessPermissions |= ( i_rProperties.CanPrintTheDocument ) ?  1 << 2 : 0;
1277cdf0e10cSrcweir     nAccessPermissions |= ( i_rProperties.CanModifyTheContent ) ? 1 << 3 : 0;
1278cdf0e10cSrcweir     nAccessPermissions |= ( i_rProperties.CanCopyOrExtract ) ?   1 << 4 : 0;
1279cdf0e10cSrcweir     nAccessPermissions |= ( i_rProperties.CanAddOrModify ) ? 1 << 5 : 0;
1280cdf0e10cSrcweir     o_rKeyLength = SECUR_40BIT_KEY;
1281cdf0e10cSrcweir     o_rRC4KeyLength = SECUR_40BIT_KEY+5; // for this value see PDF spec v 1.4, algorithm 3.1 step 4, where n is 5
1282cdf0e10cSrcweir 
1283cdf0e10cSrcweir     if( i_rProperties.Security128bit )
1284cdf0e10cSrcweir     {
1285cdf0e10cSrcweir         o_rKeyLength = SECUR_128BIT_KEY;
1286cdf0e10cSrcweir         o_rRC4KeyLength = 16; // for this value see PDF spec v 1.4, algorithm 3.1 step 4, where n is 16, thus maximum
1287cdf0e10cSrcweir         // permitted value is 16
1288cdf0e10cSrcweir         nAccessPermissions |= ( i_rProperties.CanFillInteractive ) ?         1 << 8 : 0;
1289cdf0e10cSrcweir         nAccessPermissions |= ( i_rProperties.CanExtractForAccessibility ) ? 1 << 9 : 0;
1290cdf0e10cSrcweir         nAccessPermissions |= ( i_rProperties.CanAssemble ) ?                1 << 10 : 0;
1291cdf0e10cSrcweir         nAccessPermissions |= ( i_rProperties.CanPrintFull ) ?               1 << 11 : 0;
1292cdf0e10cSrcweir     }
1293cdf0e10cSrcweir     return nAccessPermissions;
1294cdf0e10cSrcweir }
1295cdf0e10cSrcweir 
1296cdf0e10cSrcweir /*************************************************************
1297cdf0e10cSrcweir begin i12626 methods
1298cdf0e10cSrcweir 
1299cdf0e10cSrcweir Implements Algorithm 3.2, step 1 only
1300cdf0e10cSrcweir */
padPassword(const rtl::OUString & i_rPassword,sal_uInt8 * o_pPaddedPW)1301cdf0e10cSrcweir void PDFWriterImpl::padPassword( const rtl::OUString& i_rPassword, sal_uInt8* o_pPaddedPW )
1302cdf0e10cSrcweir {
1303cdf0e10cSrcweir     // get ansi-1252 version of the password string CHECKIT ! i12626
1304cdf0e10cSrcweir     rtl::OString aString( rtl::OUStringToOString( i_rPassword, RTL_TEXTENCODING_MS_1252 ) );
1305cdf0e10cSrcweir 
1306cdf0e10cSrcweir     //copy the string to the target
1307cdf0e10cSrcweir     sal_Int32 nToCopy = ( aString.getLength() < ENCRYPTED_PWD_SIZE ) ? aString.getLength() : ENCRYPTED_PWD_SIZE;
1308cdf0e10cSrcweir     sal_Int32 nCurrentChar;
1309cdf0e10cSrcweir 
1310cdf0e10cSrcweir     for( nCurrentChar = 0; nCurrentChar < nToCopy; nCurrentChar++ )
1311cdf0e10cSrcweir         o_pPaddedPW[nCurrentChar] = (sal_uInt8)( aString.getStr()[nCurrentChar] );
1312cdf0e10cSrcweir 
1313cdf0e10cSrcweir     //pad it with standard byte string
1314cdf0e10cSrcweir     sal_Int32 i,y;
1315cdf0e10cSrcweir     for( i = nCurrentChar, y = 0 ; i < ENCRYPTED_PWD_SIZE; i++, y++ )
1316cdf0e10cSrcweir         o_pPaddedPW[i] = s_nPadString[y];
1317cdf0e10cSrcweir 
1318cdf0e10cSrcweir     // trash memory of temporary clear text password
1319cdf0e10cSrcweir     rtl_zeroMemory( (sal_Char*)aString.getStr(), aString.getLength() );
1320cdf0e10cSrcweir }
1321cdf0e10cSrcweir 
1322cdf0e10cSrcweir /**********************************
1323cdf0e10cSrcweir Algorithm 3.2  Compute the encryption key used
1324cdf0e10cSrcweir 
1325cdf0e10cSrcweir step 1 should already be done before calling, the paThePaddedPassword parameter should contain
1326cdf0e10cSrcweir the padded password and must be 32 byte long, the encryption key is returned into the paEncryptionKey parameter,
1327cdf0e10cSrcweir it will be 16 byte long for 128 bit security; for 40 bit security only the first 5 bytes are used
1328cdf0e10cSrcweir 
1329cdf0e10cSrcweir TODO: in pdf ver 1.5 and 1.6 the step 6 is different, should be implemented. See spec.
1330cdf0e10cSrcweir 
1331cdf0e10cSrcweir */
computeEncryptionKey(EncHashTransporter * i_pTransporter,vcl::PDFWriter::PDFEncryptionProperties & io_rProperties,sal_Int32 i_nAccessPermissions)1332cdf0e10cSrcweir bool PDFWriterImpl::computeEncryptionKey( EncHashTransporter* i_pTransporter, vcl::PDFWriter::PDFEncryptionProperties& io_rProperties, sal_Int32 i_nAccessPermissions )
1333cdf0e10cSrcweir {
1334cdf0e10cSrcweir     bool bSuccess = true;
1335cdf0e10cSrcweir     sal_uInt8 nMD5Sum[ RTL_DIGEST_LENGTH_MD5 ];
1336cdf0e10cSrcweir 
1337cdf0e10cSrcweir     // transporter contains an MD5 digest with the padded user password already
1338cdf0e10cSrcweir     rtlDigest aDigest = i_pTransporter->getUDigest();
1339cdf0e10cSrcweir     rtlDigestError nError = rtl_Digest_E_None;
1340cdf0e10cSrcweir     if( aDigest )
1341cdf0e10cSrcweir     {
1342cdf0e10cSrcweir         //step 3
1343cdf0e10cSrcweir         if( ! io_rProperties.OValue.empty() )
1344cdf0e10cSrcweir             nError = rtl_digest_updateMD5( aDigest, &io_rProperties.OValue[0] , sal_Int32(io_rProperties.OValue.size()) );
1345cdf0e10cSrcweir         else
1346cdf0e10cSrcweir             bSuccess = false;
1347cdf0e10cSrcweir         //Step 4
1348cdf0e10cSrcweir         sal_uInt8 nPerm[4];
1349cdf0e10cSrcweir 
1350cdf0e10cSrcweir         nPerm[0] = (sal_uInt8)i_nAccessPermissions;
1351cdf0e10cSrcweir         nPerm[1] = (sal_uInt8)( i_nAccessPermissions >> 8 );
1352cdf0e10cSrcweir         nPerm[2] = (sal_uInt8)( i_nAccessPermissions >> 16 );
1353cdf0e10cSrcweir         nPerm[3] = (sal_uInt8)( i_nAccessPermissions >> 24 );
1354cdf0e10cSrcweir 
1355cdf0e10cSrcweir         if( nError == rtl_Digest_E_None )
1356cdf0e10cSrcweir             nError = rtl_digest_updateMD5( aDigest, nPerm , sizeof( nPerm ) );
1357cdf0e10cSrcweir 
1358cdf0e10cSrcweir         //step 5, get the document ID, binary form
1359cdf0e10cSrcweir         if( nError == rtl_Digest_E_None )
1360cdf0e10cSrcweir             nError = rtl_digest_updateMD5( aDigest, &io_rProperties.DocumentIdentifier[0], sal_Int32(io_rProperties.DocumentIdentifier.size()) );
1361cdf0e10cSrcweir         //get the digest
1362cdf0e10cSrcweir         if( nError == rtl_Digest_E_None )
1363cdf0e10cSrcweir         {
1364cdf0e10cSrcweir             rtl_digest_getMD5( aDigest, nMD5Sum, sizeof( nMD5Sum ) );
1365cdf0e10cSrcweir 
1366cdf0e10cSrcweir             //step 6, only if 128 bit
1367cdf0e10cSrcweir             if( io_rProperties.Security128bit )
1368cdf0e10cSrcweir             {
1369cdf0e10cSrcweir                 for( sal_Int32 i = 0; i < 50; i++ )
1370cdf0e10cSrcweir                 {
1371cdf0e10cSrcweir                     nError = rtl_digest_updateMD5( aDigest, &nMD5Sum, sizeof( nMD5Sum ) );
1372cdf0e10cSrcweir                     if( nError != rtl_Digest_E_None )
1373cdf0e10cSrcweir                     {
1374cdf0e10cSrcweir                         bSuccess =  false;
1375cdf0e10cSrcweir                         break;
1376cdf0e10cSrcweir                     }
1377cdf0e10cSrcweir                     rtl_digest_getMD5( aDigest, nMD5Sum, sizeof( nMD5Sum ) );
1378cdf0e10cSrcweir                 }
1379cdf0e10cSrcweir             }
1380cdf0e10cSrcweir         }
1381cdf0e10cSrcweir     }
1382cdf0e10cSrcweir     else
1383cdf0e10cSrcweir         bSuccess = false;
1384cdf0e10cSrcweir 
1385cdf0e10cSrcweir     i_pTransporter->invalidate();
1386cdf0e10cSrcweir 
1387cdf0e10cSrcweir     //Step 7
1388cdf0e10cSrcweir     if( bSuccess )
1389cdf0e10cSrcweir     {
1390cdf0e10cSrcweir         io_rProperties.EncryptionKey.resize( MAXIMUM_RC4_KEY_LENGTH );
1391cdf0e10cSrcweir         for( sal_Int32 i = 0; i < MD5_DIGEST_SIZE; i++ )
1392cdf0e10cSrcweir             io_rProperties.EncryptionKey[i] = nMD5Sum[i];
1393cdf0e10cSrcweir     }
1394cdf0e10cSrcweir     else
1395cdf0e10cSrcweir         io_rProperties.EncryptionKey.clear();
1396cdf0e10cSrcweir 
1397cdf0e10cSrcweir     return bSuccess;
1398cdf0e10cSrcweir }
1399cdf0e10cSrcweir 
1400cdf0e10cSrcweir /**********************************
1401cdf0e10cSrcweir Algorithm 3.3  Compute the encryption dictionary /O value, save into the class data member
1402cdf0e10cSrcweir the step numbers down here correspond to the ones in PDF v.1.4 specfication
1403cdf0e10cSrcweir */
computeODictionaryValue(const sal_uInt8 * i_pPaddedOwnerPassword,const sal_uInt8 * i_pPaddedUserPassword,std::vector<sal_uInt8> & io_rOValue,sal_Int32 i_nKeyLength)1404cdf0e10cSrcweir bool PDFWriterImpl::computeODictionaryValue( const sal_uInt8* i_pPaddedOwnerPassword,
1405cdf0e10cSrcweir                                              const sal_uInt8* i_pPaddedUserPassword,
1406cdf0e10cSrcweir                                              std::vector< sal_uInt8 >& io_rOValue,
1407cdf0e10cSrcweir                                              sal_Int32 i_nKeyLength
1408cdf0e10cSrcweir                                              )
1409cdf0e10cSrcweir {
1410cdf0e10cSrcweir     bool bSuccess = true;
1411cdf0e10cSrcweir 
1412cdf0e10cSrcweir     io_rOValue.resize( ENCRYPTED_PWD_SIZE );
1413cdf0e10cSrcweir 
1414cdf0e10cSrcweir     rtlDigest aDigest = rtl_digest_createMD5();
1415cdf0e10cSrcweir     rtlCipher aCipher = rtl_cipher_createARCFOUR( rtl_Cipher_ModeStream );
1416cdf0e10cSrcweir     if( aDigest && aCipher)
1417cdf0e10cSrcweir     {
1418cdf0e10cSrcweir         //step 1 already done, data is in i_pPaddedOwnerPassword
1419cdf0e10cSrcweir         //step 2
1420cdf0e10cSrcweir 
1421cdf0e10cSrcweir         rtlDigestError nError = rtl_digest_updateMD5( aDigest, i_pPaddedOwnerPassword, ENCRYPTED_PWD_SIZE );
1422cdf0e10cSrcweir         if( nError == rtl_Digest_E_None )
1423cdf0e10cSrcweir         {
1424cdf0e10cSrcweir             sal_uInt8 nMD5Sum[ RTL_DIGEST_LENGTH_MD5 ];
1425cdf0e10cSrcweir 
1426cdf0e10cSrcweir             rtl_digest_getMD5( aDigest, nMD5Sum, sizeof(nMD5Sum) );
1427cdf0e10cSrcweir //step 3, only if 128 bit
1428cdf0e10cSrcweir             if( i_nKeyLength == SECUR_128BIT_KEY )
1429cdf0e10cSrcweir             {
1430cdf0e10cSrcweir                 sal_Int32 i;
1431cdf0e10cSrcweir                 for( i = 0; i < 50; i++ )
1432cdf0e10cSrcweir                 {
1433cdf0e10cSrcweir                     nError = rtl_digest_updateMD5( aDigest, nMD5Sum, sizeof( nMD5Sum ) );
1434cdf0e10cSrcweir                     if( nError != rtl_Digest_E_None )
1435cdf0e10cSrcweir                     {
1436cdf0e10cSrcweir                         bSuccess = false;
1437cdf0e10cSrcweir                         break;
1438cdf0e10cSrcweir                     }
1439cdf0e10cSrcweir                     rtl_digest_getMD5( aDigest, nMD5Sum, sizeof( nMD5Sum ) );
1440cdf0e10cSrcweir                 }
1441cdf0e10cSrcweir             }
1442cdf0e10cSrcweir             //Step 4, the key is in nMD5Sum
1443cdf0e10cSrcweir             //step 5 already done, data is in i_pPaddedUserPassword
1444cdf0e10cSrcweir             //step 6
1445cdf0e10cSrcweir             rtl_cipher_initARCFOUR( aCipher, rtl_Cipher_DirectionEncode,
1446cdf0e10cSrcweir                                      nMD5Sum, i_nKeyLength , NULL, 0 );
1447cdf0e10cSrcweir             // encrypt the user password using the key set above
1448cdf0e10cSrcweir             rtl_cipher_encodeARCFOUR( aCipher, i_pPaddedUserPassword, ENCRYPTED_PWD_SIZE, // the data to be encrypted
1449cdf0e10cSrcweir                                       &io_rOValue[0], sal_Int32(io_rOValue.size()) ); //encrypted data
1450cdf0e10cSrcweir             //Step 7, only if 128 bit
1451cdf0e10cSrcweir             if( i_nKeyLength == SECUR_128BIT_KEY )
1452cdf0e10cSrcweir             {
1453cdf0e10cSrcweir                 sal_uInt32 i, y;
1454cdf0e10cSrcweir                 sal_uInt8 nLocalKey[ SECUR_128BIT_KEY ]; // 16 = 128 bit key
1455cdf0e10cSrcweir 
1456cdf0e10cSrcweir                 for( i = 1; i <= 19; i++ ) // do it 19 times, start with 1
1457cdf0e10cSrcweir                 {
1458cdf0e10cSrcweir                     for( y = 0; y < sizeof( nLocalKey ); y++ )
1459cdf0e10cSrcweir                         nLocalKey[y] = (sal_uInt8)( nMD5Sum[y] ^ i );
1460cdf0e10cSrcweir 
1461cdf0e10cSrcweir                     rtl_cipher_initARCFOUR( aCipher, rtl_Cipher_DirectionEncode,
1462cdf0e10cSrcweir                                             nLocalKey, SECUR_128BIT_KEY, NULL, 0 ); //destination data area, on init can be NULL
1463cdf0e10cSrcweir                     rtl_cipher_encodeARCFOUR( aCipher, &io_rOValue[0], sal_Int32(io_rOValue.size()), // the data to be encrypted
1464cdf0e10cSrcweir                                               &io_rOValue[0], sal_Int32(io_rOValue.size()) ); // encrypted data, can be the same as the input, encrypt "in place"
1465cdf0e10cSrcweir                     //step 8, store in class data member
1466cdf0e10cSrcweir                 }
1467cdf0e10cSrcweir             }
1468cdf0e10cSrcweir         }
1469cdf0e10cSrcweir         else
1470cdf0e10cSrcweir             bSuccess = false;
1471cdf0e10cSrcweir     }
1472cdf0e10cSrcweir     else
1473cdf0e10cSrcweir         bSuccess = false;
1474cdf0e10cSrcweir 
1475cdf0e10cSrcweir     if( aDigest )
1476cdf0e10cSrcweir         rtl_digest_destroyMD5( aDigest );
1477cdf0e10cSrcweir     if( aCipher )
1478cdf0e10cSrcweir         rtl_cipher_destroyARCFOUR( aCipher );
1479cdf0e10cSrcweir 
1480cdf0e10cSrcweir     if( ! bSuccess )
1481cdf0e10cSrcweir         io_rOValue.clear();
1482cdf0e10cSrcweir     return bSuccess;
1483cdf0e10cSrcweir }
1484cdf0e10cSrcweir 
1485cdf0e10cSrcweir /**********************************
1486cdf0e10cSrcweir Algorithms 3.4 and 3.5  Compute the encryption dictionary /U value, save into the class data member, revision 2 (40 bit) or 3 (128 bit)
1487cdf0e10cSrcweir */
computeUDictionaryValue(EncHashTransporter * i_pTransporter,vcl::PDFWriter::PDFEncryptionProperties & io_rProperties,sal_Int32 i_nKeyLength,sal_Int32 i_nAccessPermissions)1488cdf0e10cSrcweir bool PDFWriterImpl::computeUDictionaryValue( EncHashTransporter* i_pTransporter,
1489cdf0e10cSrcweir                                              vcl::PDFWriter::PDFEncryptionProperties& io_rProperties,
1490cdf0e10cSrcweir                                              sal_Int32 i_nKeyLength,
1491cdf0e10cSrcweir                                              sal_Int32 i_nAccessPermissions
1492cdf0e10cSrcweir                                              )
1493cdf0e10cSrcweir {
1494cdf0e10cSrcweir     bool bSuccess = true;
1495cdf0e10cSrcweir 
1496cdf0e10cSrcweir     io_rProperties.UValue.resize( ENCRYPTED_PWD_SIZE );
1497cdf0e10cSrcweir 
1498cdf0e10cSrcweir     rtlDigest aDigest = rtl_digest_createMD5();
1499cdf0e10cSrcweir     rtlCipher aCipher = rtl_cipher_createARCFOUR( rtl_Cipher_ModeStream );
1500cdf0e10cSrcweir     if( aDigest && aCipher )
1501cdf0e10cSrcweir     {
1502cdf0e10cSrcweir         //step 1, common to both 3.4 and 3.5
1503cdf0e10cSrcweir         if( computeEncryptionKey( i_pTransporter, io_rProperties, i_nAccessPermissions ) )
1504cdf0e10cSrcweir         {
1505cdf0e10cSrcweir             // prepare encryption key for object
1506cdf0e10cSrcweir             for( sal_Int32 i = i_nKeyLength, y = 0; y < 5 ; y++ )
1507cdf0e10cSrcweir                 io_rProperties.EncryptionKey[i++] = 0;
1508cdf0e10cSrcweir 
1509cdf0e10cSrcweir             if( io_rProperties.Security128bit == false )
1510cdf0e10cSrcweir             {
1511cdf0e10cSrcweir                 //3.4
1512cdf0e10cSrcweir                 //step 2 and 3
1513cdf0e10cSrcweir                 rtl_cipher_initARCFOUR( aCipher, rtl_Cipher_DirectionEncode,
1514cdf0e10cSrcweir                                         &io_rProperties.EncryptionKey[0], 5 , // key and key length
1515cdf0e10cSrcweir                                         NULL, 0 ); //destination data area
1516cdf0e10cSrcweir                 // encrypt the user password using the key set above, save for later use
1517cdf0e10cSrcweir                 rtl_cipher_encodeARCFOUR( aCipher, s_nPadString, sizeof( s_nPadString ), // the data to be encrypted
1518cdf0e10cSrcweir                                           &io_rProperties.UValue[0], sal_Int32(io_rProperties.UValue.size()) ); //encrypted data, stored in class data member
1519cdf0e10cSrcweir             }
1520cdf0e10cSrcweir             else
1521cdf0e10cSrcweir             {
1522cdf0e10cSrcweir                 //or 3.5, for 128 bit security
152386e1cf34SPedro Giffuni                 //step6, initialize the last 16 bytes of the encrypted user password to 0
1524cdf0e10cSrcweir                 for(sal_uInt32 i = MD5_DIGEST_SIZE; i < sal_uInt32(io_rProperties.UValue.size()); i++)
1525cdf0e10cSrcweir                     io_rProperties.UValue[i] = 0;
1526cdf0e10cSrcweir                 //step 2
1527cdf0e10cSrcweir                 rtlDigestError nError = rtl_digest_updateMD5( aDigest, s_nPadString, sizeof( s_nPadString ) );
1528cdf0e10cSrcweir                 //step 3
1529cdf0e10cSrcweir                 if( nError == rtl_Digest_E_None )
1530cdf0e10cSrcweir                     nError = rtl_digest_updateMD5( aDigest, &io_rProperties.DocumentIdentifier[0], sal_Int32(io_rProperties.DocumentIdentifier.size()) );
1531cdf0e10cSrcweir                 else
1532cdf0e10cSrcweir                     bSuccess = false;
1533cdf0e10cSrcweir 
1534cdf0e10cSrcweir                 sal_uInt8 nMD5Sum[ RTL_DIGEST_LENGTH_MD5 ];
1535cdf0e10cSrcweir                 rtl_digest_getMD5( aDigest, nMD5Sum, sizeof(nMD5Sum) );
1536cdf0e10cSrcweir                 //Step 4
1537cdf0e10cSrcweir                 rtl_cipher_initARCFOUR( aCipher, rtl_Cipher_DirectionEncode,
1538cdf0e10cSrcweir                                         &io_rProperties.EncryptionKey[0], SECUR_128BIT_KEY, NULL, 0 ); //destination data area
1539cdf0e10cSrcweir                 rtl_cipher_encodeARCFOUR( aCipher, nMD5Sum, sizeof( nMD5Sum ), // the data to be encrypted
1540cdf0e10cSrcweir                                           &io_rProperties.UValue[0], sizeof( nMD5Sum ) ); //encrypted data, stored in class data member
1541cdf0e10cSrcweir                 //step 5
1542cdf0e10cSrcweir                 sal_uInt32 i, y;
1543cdf0e10cSrcweir                 sal_uInt8 nLocalKey[SECUR_128BIT_KEY];
1544cdf0e10cSrcweir 
1545cdf0e10cSrcweir                 for( i = 1; i <= 19; i++ ) // do it 19 times, start with 1
1546cdf0e10cSrcweir                 {
1547cdf0e10cSrcweir                     for( y = 0; y < sizeof( nLocalKey ) ; y++ )
1548cdf0e10cSrcweir                         nLocalKey[y] = (sal_uInt8)( io_rProperties.EncryptionKey[y] ^ i );
1549cdf0e10cSrcweir 
1550cdf0e10cSrcweir                     rtl_cipher_initARCFOUR( aCipher, rtl_Cipher_DirectionEncode,
1551cdf0e10cSrcweir                                             nLocalKey, SECUR_128BIT_KEY, // key and key length
1552cdf0e10cSrcweir                                             NULL, 0 ); //destination data area, on init can be NULL
1553cdf0e10cSrcweir                     rtl_cipher_encodeARCFOUR( aCipher, &io_rProperties.UValue[0], SECUR_128BIT_KEY, // the data to be encrypted
1554cdf0e10cSrcweir                                               &io_rProperties.UValue[0], SECUR_128BIT_KEY ); // encrypted data, can be the same as the input, encrypt "in place"
1555cdf0e10cSrcweir                 }
1556cdf0e10cSrcweir             }
1557cdf0e10cSrcweir         }
1558cdf0e10cSrcweir         else
1559cdf0e10cSrcweir             bSuccess = false;
1560cdf0e10cSrcweir     }
1561cdf0e10cSrcweir     else
1562cdf0e10cSrcweir         bSuccess = false;
1563cdf0e10cSrcweir 
1564cdf0e10cSrcweir     if( aDigest )
1565cdf0e10cSrcweir         rtl_digest_destroyMD5( aDigest );
1566cdf0e10cSrcweir     if( aCipher )
1567cdf0e10cSrcweir         rtl_cipher_destroyARCFOUR( aCipher );
1568cdf0e10cSrcweir 
1569cdf0e10cSrcweir     if( ! bSuccess )
1570cdf0e10cSrcweir         io_rProperties.UValue.clear();
1571cdf0e10cSrcweir     return bSuccess;
1572cdf0e10cSrcweir }
1573cdf0e10cSrcweir 
1574cdf0e10cSrcweir /* end i12626 methods */
1575cdf0e10cSrcweir 
1576cdf0e10cSrcweir static const long unsetRun[256] =
1577cdf0e10cSrcweir {
1578cdf0e10cSrcweir     8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,	/* 0x00 - 0x0f */
1579cdf0e10cSrcweir     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,	/* 0x10 - 0x1f */
1580cdf0e10cSrcweir     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0x20 - 0x2f */
1581cdf0e10cSrcweir     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0x30 - 0x3f */
1582cdf0e10cSrcweir     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x40 - 0x4f */
1583cdf0e10cSrcweir     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x50 - 0x5f */
1584cdf0e10cSrcweir     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x60 - 0x6f */
1585cdf0e10cSrcweir     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x70 - 0x7f */
1586cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x80 - 0x8f */
1587cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x90 - 0x9f */
1588cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xa0 - 0xaf */
1589cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xb0 - 0xbf */
1590cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xc0 - 0xcf */
1591cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xd0 - 0xdf */
1592cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xe0 - 0xef */
1593cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xf0 - 0xff */
1594cdf0e10cSrcweir };
1595cdf0e10cSrcweir 
1596cdf0e10cSrcweir static const long setRun[256] =
1597cdf0e10cSrcweir {
1598cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x00 - 0x0f */
1599cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x10 - 0x1f */
1600cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x20 - 0x2f */
1601cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x30 - 0x3f */
1602cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x40 - 0x4f */
1603cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x50 - 0x5f */
1604cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x60 - 0x6f */
1605cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x70 - 0x7f */
1606cdf0e10cSrcweir     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x80 - 0x8f */
1607cdf0e10cSrcweir     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x90 - 0x9f */
1608cdf0e10cSrcweir     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0xa0 - 0xaf */
1609cdf0e10cSrcweir     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0xb0 - 0xbf */
1610cdf0e10cSrcweir     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0xc0 - 0xcf */
1611cdf0e10cSrcweir     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0xd0 - 0xdf */
1612cdf0e10cSrcweir     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,	/* 0xe0 - 0xef */
1613cdf0e10cSrcweir     4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8,	/* 0xf0 - 0xff */
1614cdf0e10cSrcweir };
1615cdf0e10cSrcweir 
isSet(const Scanline i_pLine,long i_nIndex)1616cdf0e10cSrcweir inline bool isSet( const Scanline i_pLine, long i_nIndex )
1617cdf0e10cSrcweir {
1618cdf0e10cSrcweir     return (i_pLine[ i_nIndex/8 ] & (0x80 >> (i_nIndex&7))) != 0;
1619cdf0e10cSrcweir }
1620cdf0e10cSrcweir 
findBitRun(const Scanline i_pLine,long i_nStartIndex,long i_nW,bool i_bSet)1621cdf0e10cSrcweir long findBitRun( const Scanline i_pLine, long i_nStartIndex, long i_nW, bool i_bSet )
1622cdf0e10cSrcweir {
1623cdf0e10cSrcweir     if( i_nStartIndex < 0 )
1624cdf0e10cSrcweir         return i_nW;
1625cdf0e10cSrcweir 
1626cdf0e10cSrcweir     long nIndex = i_nStartIndex;
1627cdf0e10cSrcweir     if( nIndex < i_nW )
1628cdf0e10cSrcweir     {
1629cdf0e10cSrcweir         const sal_uInt8 * pByte = static_cast<sal_uInt8*>(i_pLine) + (nIndex/8);
1630cdf0e10cSrcweir         sal_uInt8 nByte = *pByte;
1631cdf0e10cSrcweir 
1632cdf0e10cSrcweir         // run up to byte boundary
1633cdf0e10cSrcweir         long nBitInByte = (nIndex & 7);
1634cdf0e10cSrcweir         if( nBitInByte )
1635cdf0e10cSrcweir         {
1636cdf0e10cSrcweir             sal_uInt8 nMask = 0x80 >> nBitInByte;
1637cdf0e10cSrcweir             while( nBitInByte != 8 )
1638cdf0e10cSrcweir             {
1639cdf0e10cSrcweir                 if( (nByte & nMask) != (i_bSet ? nMask : 0) )
1640cdf0e10cSrcweir                     return nIndex < i_nW ? nIndex : i_nW;
1641cdf0e10cSrcweir                 nMask = nMask >> 1;
1642cdf0e10cSrcweir                 nBitInByte++;
1643cdf0e10cSrcweir                 nIndex++;
1644cdf0e10cSrcweir             }
1645cdf0e10cSrcweir             if( nIndex < i_nW )
1646cdf0e10cSrcweir             {
1647cdf0e10cSrcweir                 pByte++;
1648cdf0e10cSrcweir                 nByte = *pByte;
1649cdf0e10cSrcweir             }
1650cdf0e10cSrcweir         }
1651cdf0e10cSrcweir 
1652cdf0e10cSrcweir         sal_uInt8 nRunByte;
1653cdf0e10cSrcweir         const long* pRunTable;
1654cdf0e10cSrcweir         if( i_bSet )
1655cdf0e10cSrcweir         {
1656cdf0e10cSrcweir             nRunByte = 0xff;
1657cdf0e10cSrcweir             pRunTable = setRun;
1658cdf0e10cSrcweir         }
1659cdf0e10cSrcweir         else
1660cdf0e10cSrcweir         {
1661cdf0e10cSrcweir             nRunByte = 0;
1662cdf0e10cSrcweir             pRunTable = unsetRun;
1663cdf0e10cSrcweir         }
1664cdf0e10cSrcweir 
1665cdf0e10cSrcweir         while( nByte == nRunByte && nIndex < i_nW )
1666cdf0e10cSrcweir         {
1667cdf0e10cSrcweir             nIndex += 8;
1668cdf0e10cSrcweir             pByte++;
1669cdf0e10cSrcweir             nByte = *pByte;
1670cdf0e10cSrcweir         }
1671cdf0e10cSrcweir         if( nIndex < i_nW )
1672cdf0e10cSrcweir         {
1673cdf0e10cSrcweir             nIndex += pRunTable[nByte];
1674cdf0e10cSrcweir         }
1675cdf0e10cSrcweir     }
1676cdf0e10cSrcweir     return nIndex < i_nW ? nIndex : i_nW;
1677cdf0e10cSrcweir }
1678cdf0e10cSrcweir 
1679cdf0e10cSrcweir struct BitStreamState
1680cdf0e10cSrcweir {
1681cdf0e10cSrcweir     sal_uInt8       mnBuffer;
1682cdf0e10cSrcweir     sal_uInt32      mnNextBitPos;
1683cdf0e10cSrcweir 
BitStreamStateBitStreamState1684cdf0e10cSrcweir     BitStreamState()
1685cdf0e10cSrcweir     : mnBuffer( 0 )
1686cdf0e10cSrcweir     , mnNextBitPos( 8 )
1687cdf0e10cSrcweir     {
1688cdf0e10cSrcweir     }
1689cdf0e10cSrcweir 
getByteBitStreamState1690cdf0e10cSrcweir     const sal_uInt8* getByte() const { return &mnBuffer; }
flushBitStreamState1691cdf0e10cSrcweir     void flush() { mnNextBitPos = 8; mnBuffer = 0; }
1692cdf0e10cSrcweir };
1693cdf0e10cSrcweir 
putG4Bits(sal_uInt32 i_nLength,sal_uInt32 i_nCode,BitStreamState & io_rState)1694cdf0e10cSrcweir void PDFWriterImpl::putG4Bits( sal_uInt32 i_nLength, sal_uInt32 i_nCode, BitStreamState& io_rState )
1695cdf0e10cSrcweir {
1696cdf0e10cSrcweir     while( i_nLength > io_rState.mnNextBitPos )
1697cdf0e10cSrcweir     {
1698cdf0e10cSrcweir         io_rState.mnBuffer |= static_cast<sal_uInt8>( i_nCode >> (i_nLength - io_rState.mnNextBitPos) );
1699cdf0e10cSrcweir         i_nLength -= io_rState.mnNextBitPos;
1700cdf0e10cSrcweir         writeBuffer( io_rState.getByte(), 1 );
1701cdf0e10cSrcweir         io_rState.flush();
1702cdf0e10cSrcweir     }
1703cdf0e10cSrcweir     OSL_ASSERT( i_nLength < 9 );
1704cdf0e10cSrcweir     static const unsigned int msbmask[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
1705cdf0e10cSrcweir     io_rState.mnBuffer |= static_cast<sal_uInt8>( (i_nCode & msbmask[i_nLength]) << (io_rState.mnNextBitPos - i_nLength) );
1706cdf0e10cSrcweir     io_rState.mnNextBitPos -= i_nLength;
1707cdf0e10cSrcweir     if( io_rState.mnNextBitPos == 0 )
1708cdf0e10cSrcweir     {
1709cdf0e10cSrcweir         writeBuffer( io_rState.getByte(), 1 );
1710cdf0e10cSrcweir         io_rState.flush();
1711cdf0e10cSrcweir     }
1712cdf0e10cSrcweir }
1713cdf0e10cSrcweir 
1714cdf0e10cSrcweir struct PixelCode
1715cdf0e10cSrcweir {
1716cdf0e10cSrcweir     sal_uInt32      mnEncodedPixels;
1717cdf0e10cSrcweir     sal_uInt32      mnCodeBits;
1718cdf0e10cSrcweir     sal_uInt32      mnCode;
1719cdf0e10cSrcweir };
1720cdf0e10cSrcweir 
1721cdf0e10cSrcweir static const PixelCode WhitePixelCodes[] =
1722cdf0e10cSrcweir {
1723cdf0e10cSrcweir     { 0, 8, 0x35 },     // 0011 0101
1724cdf0e10cSrcweir     { 1, 6, 0x7 },      // 0001 11
1725cdf0e10cSrcweir     { 2, 4, 0x7 },      // 0111
1726cdf0e10cSrcweir     { 3, 4, 0x8 },      // 1000
1727cdf0e10cSrcweir     { 4, 4, 0xB },      // 1011
1728cdf0e10cSrcweir     { 5, 4, 0xC },      // 1100
1729cdf0e10cSrcweir     { 6, 4, 0xE },      // 1110
1730cdf0e10cSrcweir     { 7, 4, 0xF },      // 1111
1731cdf0e10cSrcweir     { 8, 5, 0x13 },     // 1001 1
1732cdf0e10cSrcweir     { 9, 5, 0x14 },     // 1010 0
1733cdf0e10cSrcweir     { 10, 5, 0x7 },     // 0011 1
1734cdf0e10cSrcweir     { 11, 5, 0x8 },     // 0100 0
1735cdf0e10cSrcweir     { 12, 6, 0x8 },     // 0010 00
1736cdf0e10cSrcweir     { 13, 6, 0x3 },     // 0000 11
1737cdf0e10cSrcweir     { 14, 6, 0x34 },    // 1101 00
1738cdf0e10cSrcweir     { 15, 6, 0x35 },    // 1101 01
1739cdf0e10cSrcweir     { 16, 6, 0x2A },    // 1010 10
1740cdf0e10cSrcweir     { 17, 6, 0x2B },    // 1010 11
1741cdf0e10cSrcweir     { 18, 7, 0x27 },    // 0100 111
1742cdf0e10cSrcweir     { 19, 7, 0xC },     // 0001 100
1743cdf0e10cSrcweir     { 20, 7, 0x8 },     // 0001 000
1744cdf0e10cSrcweir     { 21, 7, 0x17 },    // 0010 111
1745cdf0e10cSrcweir     { 22, 7, 0x3 },     // 0000 011
1746cdf0e10cSrcweir     { 23, 7, 0x4 },     // 0000 100
1747cdf0e10cSrcweir     { 24, 7, 0x28 },    // 0101 000
1748cdf0e10cSrcweir     { 25, 7, 0x2B },    // 0101 011
1749cdf0e10cSrcweir     { 26, 7, 0x13 },    // 0010 011
1750cdf0e10cSrcweir     { 27, 7, 0x24 },    // 0100 100
1751cdf0e10cSrcweir     { 28, 7, 0x18 },    // 0011 000
1752cdf0e10cSrcweir     { 29, 8, 0x2 },     // 0000 0010
1753cdf0e10cSrcweir     { 30, 8, 0x3 },     // 0000 0011
1754cdf0e10cSrcweir     { 31, 8, 0x1A },    // 0001 1010
1755cdf0e10cSrcweir     { 32, 8, 0x1B },    // 0001 1011
1756cdf0e10cSrcweir     { 33, 8, 0x12 },    // 0001 0010
1757cdf0e10cSrcweir     { 34, 8, 0x13 },    // 0001 0011
1758cdf0e10cSrcweir     { 35, 8, 0x14 },    // 0001 0100
1759cdf0e10cSrcweir     { 36, 8, 0x15 },    // 0001 0101
1760cdf0e10cSrcweir     { 37, 8, 0x16 },    // 0001 0110
1761cdf0e10cSrcweir     { 38, 8, 0x17 },    // 0001 0111
1762cdf0e10cSrcweir     { 39, 8, 0x28 },    // 0010 1000
1763cdf0e10cSrcweir     { 40, 8, 0x29 },    // 0010 1001
1764cdf0e10cSrcweir     { 41, 8, 0x2A },    // 0010 1010
1765cdf0e10cSrcweir     { 42, 8, 0x2B },    // 0010 1011
1766cdf0e10cSrcweir     { 43, 8, 0x2C },    // 0010 1100
1767cdf0e10cSrcweir     { 44, 8, 0x2D },    // 0010 1101
1768cdf0e10cSrcweir     { 45, 8, 0x4 },     // 0000 0100
1769cdf0e10cSrcweir     { 46, 8, 0x5 },     // 0000 0101
1770cdf0e10cSrcweir     { 47, 8, 0xA },     // 0000 1010
1771cdf0e10cSrcweir     { 48, 8, 0xB },     // 0000 1011
1772cdf0e10cSrcweir     { 49, 8, 0x52 },    // 0101 0010
1773cdf0e10cSrcweir     { 50, 8, 0x53 },    // 0101 0011
1774cdf0e10cSrcweir     { 51, 8, 0x54 },    // 0101 0100
1775cdf0e10cSrcweir     { 52, 8, 0x55 },    // 0101 0101
1776cdf0e10cSrcweir     { 53, 8, 0x24 },    // 0010 0100
1777cdf0e10cSrcweir     { 54, 8, 0x25 },    // 0010 0101
1778cdf0e10cSrcweir     { 55, 8, 0x58 },    // 0101 1000
1779cdf0e10cSrcweir     { 56, 8, 0x59 },    // 0101 1001
1780cdf0e10cSrcweir     { 57, 8, 0x5A },    // 0101 1010
1781cdf0e10cSrcweir     { 58, 8, 0x5B },    // 0101 1011
1782cdf0e10cSrcweir     { 59, 8, 0x4A },    // 0100 1010
1783cdf0e10cSrcweir     { 60, 8, 0x4B },    // 0100 1011
1784cdf0e10cSrcweir     { 61, 8, 0x32 },    // 0011 0010
1785cdf0e10cSrcweir     { 62, 8, 0x33 },    // 0011 0011
1786cdf0e10cSrcweir     { 63, 8, 0x34 },    // 0011 0100
1787cdf0e10cSrcweir     { 64, 5, 0x1B },    // 1101 1
1788cdf0e10cSrcweir     { 128, 5, 0x12 },   // 1001 0
1789cdf0e10cSrcweir     { 192, 6, 0x17 },   // 0101 11
1790cdf0e10cSrcweir     { 256, 7, 0x37 },   // 0110 111
1791cdf0e10cSrcweir     { 320, 8, 0x36 },   // 0011 0110
1792cdf0e10cSrcweir     { 384, 8, 0x37 },   // 0011 0111
1793cdf0e10cSrcweir     { 448, 8, 0x64 },   // 0110 0100
1794cdf0e10cSrcweir     { 512, 8, 0x65 },   // 0110 0101
1795cdf0e10cSrcweir     { 576, 8, 0x68 },   // 0110 1000
1796cdf0e10cSrcweir     { 640, 8, 0x67 },   // 0110 0111
1797cdf0e10cSrcweir     { 704, 9, 0xCC },   // 0110 0110 0
1798cdf0e10cSrcweir     { 768, 9, 0xCD },   // 0110 0110 1
1799cdf0e10cSrcweir     { 832, 9, 0xD2 },   // 0110 1001 0
1800cdf0e10cSrcweir     { 896, 9, 0xD3 },   // 0110 1001 1
1801cdf0e10cSrcweir     { 960, 9, 0xD4 },   // 0110 1010 0
1802cdf0e10cSrcweir     { 1024, 9, 0xD5 },  // 0110 1010 1
1803cdf0e10cSrcweir     { 1088, 9, 0xD6 },  // 0110 1011 0
1804cdf0e10cSrcweir     { 1152, 9, 0xD7 },  // 0110 1011 1
1805cdf0e10cSrcweir     { 1216, 9, 0xD8 },  // 0110 1100 0
1806cdf0e10cSrcweir     { 1280, 9, 0xD9 },  // 0110 1100 1
1807cdf0e10cSrcweir     { 1344, 9, 0xDA },  // 0110 1101 0
1808cdf0e10cSrcweir     { 1408, 9, 0xDB },  // 0110 1101 1
1809cdf0e10cSrcweir     { 1472, 9, 0x98 },  // 0100 1100 0
1810cdf0e10cSrcweir     { 1536, 9, 0x99 },  // 0100 1100 1
1811cdf0e10cSrcweir     { 1600, 9, 0x9A },  // 0100 1101 0
1812cdf0e10cSrcweir     { 1664, 6, 0x18 },  // 0110 00
1813cdf0e10cSrcweir     { 1728, 9, 0x9B },  // 0100 1101 1
1814cdf0e10cSrcweir     { 1792, 11, 0x8 },  // 0000 0001 000
1815cdf0e10cSrcweir     { 1856, 11, 0xC },  // 0000 0001 100
1816cdf0e10cSrcweir     { 1920, 11, 0xD },  // 0000 0001 101
1817cdf0e10cSrcweir     { 1984, 12, 0x12 }, // 0000 0001 0010
1818cdf0e10cSrcweir     { 2048, 12, 0x13 }, // 0000 0001 0011
1819cdf0e10cSrcweir     { 2112, 12, 0x14 }, // 0000 0001 0100
1820cdf0e10cSrcweir     { 2176, 12, 0x15 }, // 0000 0001 0101
1821cdf0e10cSrcweir     { 2240, 12, 0x16 }, // 0000 0001 0110
1822cdf0e10cSrcweir     { 2304, 12, 0x17 }, // 0000 0001 0111
1823cdf0e10cSrcweir     { 2368, 12, 0x1C }, // 0000 0001 1100
1824cdf0e10cSrcweir     { 2432, 12, 0x1D }, // 0000 0001 1101
1825cdf0e10cSrcweir     { 2496, 12, 0x1E }, // 0000 0001 1110
1826cdf0e10cSrcweir     { 2560, 12, 0x1F }  // 0000 0001 1111
1827cdf0e10cSrcweir };
1828cdf0e10cSrcweir 
1829cdf0e10cSrcweir static const PixelCode BlackPixelCodes[] =
1830cdf0e10cSrcweir {
1831cdf0e10cSrcweir     { 0, 10, 0x37 },    // 0000 1101 11
1832cdf0e10cSrcweir     { 1, 3, 0x2 },      // 010
1833cdf0e10cSrcweir     { 2, 2, 0x3 },      // 11
1834cdf0e10cSrcweir     { 3, 2, 0x2 },      // 10
1835cdf0e10cSrcweir     { 4, 3, 0x3 },      // 011
1836cdf0e10cSrcweir     { 5, 4, 0x3 },      // 0011
1837cdf0e10cSrcweir     { 6, 4, 0x2 },      // 0010
1838cdf0e10cSrcweir     { 7, 5, 0x3 },      // 0001 1
1839cdf0e10cSrcweir     { 8, 6, 0x5 },      // 0001 01
1840cdf0e10cSrcweir     { 9, 6, 0x4 },      // 0001 00
1841cdf0e10cSrcweir     { 10, 7, 0x4 },     // 0000 100
1842cdf0e10cSrcweir     { 11, 7, 0x5 },     // 0000 101
1843cdf0e10cSrcweir     { 12, 7, 0x7 },     // 0000 111
1844cdf0e10cSrcweir     { 13, 8, 0x4 },     // 0000 0100
1845cdf0e10cSrcweir     { 14, 8, 0x7 },     // 0000 0111
1846cdf0e10cSrcweir     { 15, 9, 0x18 },    // 0000 1100 0
1847cdf0e10cSrcweir     { 16, 10, 0x17 },   // 0000 0101 11
1848cdf0e10cSrcweir     { 17, 10, 0x18 },   // 0000 0110 00
1849cdf0e10cSrcweir     { 18, 10, 0x8 },    // 0000 0010 00
1850cdf0e10cSrcweir     { 19, 11, 0x67 },   // 0000 1100 111
1851cdf0e10cSrcweir     { 20, 11, 0x68 },   // 0000 1101 000
1852cdf0e10cSrcweir     { 21, 11, 0x6C },   // 0000 1101 100
1853cdf0e10cSrcweir     { 22, 11, 0x37 },   // 0000 0110 111
1854cdf0e10cSrcweir     { 23, 11, 0x28 },   // 0000 0101 000
1855cdf0e10cSrcweir     { 24, 11, 0x17 },   // 0000 0010 111
1856cdf0e10cSrcweir     { 25, 11, 0x18 },   // 0000 0011 000
1857cdf0e10cSrcweir     { 26, 12, 0xCA },   // 0000 1100 1010
1858cdf0e10cSrcweir     { 27, 12, 0xCB },   // 0000 1100 1011
1859cdf0e10cSrcweir     { 28, 12, 0xCC },   // 0000 1100 1100
1860cdf0e10cSrcweir     { 29, 12, 0xCD },   // 0000 1100 1101
1861cdf0e10cSrcweir     { 30, 12, 0x68 },   // 0000 0110 1000
1862cdf0e10cSrcweir     { 31, 12, 0x69 },   // 0000 0110 1001
1863cdf0e10cSrcweir     { 32, 12, 0x6A },   // 0000 0110 1010
1864cdf0e10cSrcweir     { 33, 12, 0x6B },   // 0000 0110 1011
1865cdf0e10cSrcweir     { 34, 12, 0xD2 },   // 0000 1101 0010
1866cdf0e10cSrcweir     { 35, 12, 0xD3 },   // 0000 1101 0011
1867cdf0e10cSrcweir     { 36, 12, 0xD4 },   // 0000 1101 0100
1868cdf0e10cSrcweir     { 37, 12, 0xD5 },   // 0000 1101 0101
1869cdf0e10cSrcweir     { 38, 12, 0xD6 },   // 0000 1101 0110
1870cdf0e10cSrcweir     { 39, 12, 0xD7 },   // 0000 1101 0111
1871cdf0e10cSrcweir     { 40, 12, 0x6C },   // 0000 0110 1100
1872cdf0e10cSrcweir     { 41, 12, 0x6D },   // 0000 0110 1101
1873cdf0e10cSrcweir     { 42, 12, 0xDA },   // 0000 1101 1010
1874cdf0e10cSrcweir     { 43, 12, 0xDB },   // 0000 1101 1011
1875cdf0e10cSrcweir     { 44, 12, 0x54 },   // 0000 0101 0100
1876cdf0e10cSrcweir     { 45, 12, 0x55 },   // 0000 0101 0101
1877cdf0e10cSrcweir     { 46, 12, 0x56 },   // 0000 0101 0110
1878cdf0e10cSrcweir     { 47, 12, 0x57 },   // 0000 0101 0111
1879cdf0e10cSrcweir     { 48, 12, 0x64 },   // 0000 0110 0100
1880cdf0e10cSrcweir     { 49, 12, 0x65 },   // 0000 0110 0101
1881cdf0e10cSrcweir     { 50, 12, 0x52 },   // 0000 0101 0010
1882cdf0e10cSrcweir     { 51, 12, 0x53 },   // 0000 0101 0011
1883cdf0e10cSrcweir     { 52, 12, 0x24 },   // 0000 0010 0100
1884cdf0e10cSrcweir     { 53, 12, 0x37 },   // 0000 0011 0111
1885cdf0e10cSrcweir     { 54, 12, 0x38 },   // 0000 0011 1000
1886cdf0e10cSrcweir     { 55, 12, 0x27 },   // 0000 0010 0111
1887cdf0e10cSrcweir     { 56, 12, 0x28 },   // 0000 0010 1000
1888cdf0e10cSrcweir     { 57, 12, 0x58 },   // 0000 0101 1000
1889cdf0e10cSrcweir     { 58, 12, 0x59 },   // 0000 0101 1001
1890cdf0e10cSrcweir     { 59, 12, 0x2B },   // 0000 0010 1011
1891cdf0e10cSrcweir     { 60, 12, 0x2C },   // 0000 0010 1100
1892cdf0e10cSrcweir     { 61, 12, 0x5A },   // 0000 0101 1010
1893cdf0e10cSrcweir     { 62, 12, 0x66 },   // 0000 0110 0110
1894cdf0e10cSrcweir     { 63, 12, 0x67 },   // 0000 0110 0111
1895cdf0e10cSrcweir     { 64, 10, 0xF },    // 0000 0011 11
1896cdf0e10cSrcweir     { 128, 12, 0xC8 },  // 0000 1100 1000
1897cdf0e10cSrcweir     { 192, 12, 0xC9 },  // 0000 1100 1001
1898cdf0e10cSrcweir     { 256, 12, 0x5B },  // 0000 0101 1011
1899cdf0e10cSrcweir     { 320, 12, 0x33 },  // 0000 0011 0011
1900cdf0e10cSrcweir     { 384, 12, 0x34 },  // 0000 0011 0100
1901cdf0e10cSrcweir     { 448, 12, 0x35 },  // 0000 0011 0101
1902cdf0e10cSrcweir     { 512, 13, 0x6C },  // 0000 0011 0110 0
1903cdf0e10cSrcweir     { 576, 13, 0x6D },  // 0000 0011 0110 1
1904cdf0e10cSrcweir     { 640, 13, 0x4A },  // 0000 0010 0101 0
1905cdf0e10cSrcweir     { 704, 13, 0x4B },  // 0000 0010 0101 1
1906cdf0e10cSrcweir     { 768, 13, 0x4C },  // 0000 0010 0110 0
1907cdf0e10cSrcweir     { 832, 13, 0x4D },  // 0000 0010 0110 1
1908cdf0e10cSrcweir     { 896, 13, 0x72 },  // 0000 0011 1001 0
1909cdf0e10cSrcweir     { 960, 13, 0x73 },  // 0000 0011 1001 1
1910cdf0e10cSrcweir     { 1024, 13, 0x74 }, // 0000 0011 1010 0
1911cdf0e10cSrcweir     { 1088, 13, 0x75 }, // 0000 0011 1010 1
1912cdf0e10cSrcweir     { 1152, 13, 0x76 }, // 0000 0011 1011 0
1913cdf0e10cSrcweir     { 1216, 13, 0x77 }, // 0000 0011 1011 1
1914cdf0e10cSrcweir     { 1280, 13, 0x52 }, // 0000 0010 1001 0
1915cdf0e10cSrcweir     { 1344, 13, 0x53 }, // 0000 0010 1001 1
1916cdf0e10cSrcweir     { 1408, 13, 0x54 }, // 0000 0010 1010 0
1917cdf0e10cSrcweir     { 1472, 13, 0x55 }, // 0000 0010 1010 1
1918cdf0e10cSrcweir     { 1536, 13, 0x5A }, // 0000 0010 1101 0
1919cdf0e10cSrcweir     { 1600, 13, 0x5B }, // 0000 0010 1101 1
1920cdf0e10cSrcweir     { 1664, 13, 0x64 }, // 0000 0011 0010 0
1921cdf0e10cSrcweir     { 1728, 13, 0x65 }, // 0000 0011 0010 1
1922cdf0e10cSrcweir     { 1792, 11, 0x8 },  // 0000 0001 000
1923cdf0e10cSrcweir     { 1856, 11, 0xC },  // 0000 0001 100
1924cdf0e10cSrcweir     { 1920, 11, 0xD },  // 0000 0001 101
1925cdf0e10cSrcweir     { 1984, 12, 0x12 }, // 0000 0001 0010
1926cdf0e10cSrcweir     { 2048, 12, 0x13 }, // 0000 0001 0011
1927cdf0e10cSrcweir     { 2112, 12, 0x14 }, // 0000 0001 0100
1928cdf0e10cSrcweir     { 2176, 12, 0x15 }, // 0000 0001 0101
1929cdf0e10cSrcweir     { 2240, 12, 0x16 }, // 0000 0001 0110
1930cdf0e10cSrcweir     { 2304, 12, 0x17 }, // 0000 0001 0111
1931cdf0e10cSrcweir     { 2368, 12, 0x1C }, // 0000 0001 1100
1932cdf0e10cSrcweir     { 2432, 12, 0x1D }, // 0000 0001 1101
1933cdf0e10cSrcweir     { 2496, 12, 0x1E }, // 0000 0001 1110
1934cdf0e10cSrcweir     { 2560, 12, 0x1F }  // 0000 0001 1111
1935cdf0e10cSrcweir };
1936cdf0e10cSrcweir 
1937cdf0e10cSrcweir 
putG4Span(long i_nSpan,bool i_bWhitePixel,BitStreamState & io_rState)1938cdf0e10cSrcweir void PDFWriterImpl::putG4Span( long i_nSpan, bool i_bWhitePixel, BitStreamState& io_rState )
1939cdf0e10cSrcweir {
1940cdf0e10cSrcweir     const PixelCode* pTable = i_bWhitePixel ? WhitePixelCodes : BlackPixelCodes;
1941cdf0e10cSrcweir     // maximum encoded span is 2560 consecutive pixels
1942cdf0e10cSrcweir     while( i_nSpan > 2623 )
1943cdf0e10cSrcweir     {
1944cdf0e10cSrcweir         // write 2560 bits, that is entry (63 + (2560 >> 6)) == 103 in the appropriate table
1945cdf0e10cSrcweir         putG4Bits( pTable[103].mnCodeBits, pTable[103].mnCode, io_rState );
1946cdf0e10cSrcweir         i_nSpan -= pTable[103].mnEncodedPixels;
1947cdf0e10cSrcweir     }
1948cdf0e10cSrcweir     // write multiples of 64 pixels up to 2560
1949cdf0e10cSrcweir     if( i_nSpan > 63 )
1950cdf0e10cSrcweir     {
1951cdf0e10cSrcweir         sal_uInt32 nTabIndex = 63 + (i_nSpan >> 6);
1952cdf0e10cSrcweir         OSL_ASSERT( pTable[nTabIndex].mnEncodedPixels == static_cast<sal_uInt32>(64*(i_nSpan >> 6)) );
1953cdf0e10cSrcweir         putG4Bits( pTable[nTabIndex].mnCodeBits, pTable[nTabIndex].mnCode, io_rState );
1954cdf0e10cSrcweir         i_nSpan -= pTable[nTabIndex].mnEncodedPixels;
1955cdf0e10cSrcweir     }
1956cdf0e10cSrcweir     putG4Bits( pTable[i_nSpan].mnCodeBits, pTable[i_nSpan].mnCode, io_rState );
1957cdf0e10cSrcweir }
1958cdf0e10cSrcweir 
writeG4Stream(BitmapReadAccess * i_pBitmap)1959cdf0e10cSrcweir void PDFWriterImpl::writeG4Stream( BitmapReadAccess* i_pBitmap )
1960cdf0e10cSrcweir {
1961cdf0e10cSrcweir     long nW = i_pBitmap->Width();
1962cdf0e10cSrcweir     long nH = i_pBitmap->Height();
1963cdf0e10cSrcweir     if( nW <= 0 || nH <= 0 )
1964cdf0e10cSrcweir         return;
1965cdf0e10cSrcweir     if( i_pBitmap->GetBitCount() != 1 )
1966cdf0e10cSrcweir         return;
1967cdf0e10cSrcweir 
1968cdf0e10cSrcweir     BitStreamState aBitState;
1969cdf0e10cSrcweir 
1970cdf0e10cSrcweir     // the first reference line is virtual and completely empty
1971cdf0e10cSrcweir     const Scanline pFirstRefLine = (Scanline)rtl_allocateZeroMemory( nW/8 + 1 );
1972cdf0e10cSrcweir     Scanline pRefLine = pFirstRefLine;
1973cdf0e10cSrcweir     for( long nY = 0; nY < nH; nY++ )
1974cdf0e10cSrcweir     {
1975cdf0e10cSrcweir         const Scanline pCurLine = i_pBitmap->GetScanline( nY );
1976cdf0e10cSrcweir         long nLineIndex = 0;
1977cdf0e10cSrcweir         bool bRunSet = (*pCurLine & 0x80) ? true : false;
1978cdf0e10cSrcweir         bool bRefSet = (*pRefLine & 0x80) ? true : false;
1979cdf0e10cSrcweir         long nRunIndex1 = bRunSet ? 0 : findBitRun( pCurLine, 0, nW, bRunSet );
1980cdf0e10cSrcweir         long nRefIndex1 = bRefSet ? 0 : findBitRun( pRefLine, 0, nW, bRefSet );
1981cdf0e10cSrcweir         for( ; nLineIndex < nW; )
1982cdf0e10cSrcweir         {
1983cdf0e10cSrcweir             long nRefIndex2 = findBitRun( pRefLine, nRefIndex1, nW, isSet( pRefLine, nRefIndex1 ) );
1984cdf0e10cSrcweir             if( nRefIndex2 >= nRunIndex1 )
1985cdf0e10cSrcweir             {
1986cdf0e10cSrcweir                 long nDiff = nRefIndex1 - nRunIndex1;
1987cdf0e10cSrcweir                 if( -3 <= nDiff && nDiff <= 3 )
1988cdf0e10cSrcweir                 {   // vertical coding
1989cdf0e10cSrcweir                     static const struct
1990cdf0e10cSrcweir                     {
1991cdf0e10cSrcweir                         sal_uInt32 mnCodeBits;
1992cdf0e10cSrcweir                         sal_uInt32 mnCode;
1993cdf0e10cSrcweir                     } VerticalCodes[7] = {
1994cdf0e10cSrcweir                         { 7, 0x03 },    // 0000 011
1995cdf0e10cSrcweir                         { 6, 0x03 },    // 0000 11
1996cdf0e10cSrcweir                         { 3, 0x03 },    // 011
1997cdf0e10cSrcweir                         { 1, 0x1 },     // 1
1998cdf0e10cSrcweir                         { 3, 0x2 },     // 010
1999cdf0e10cSrcweir                         { 6, 0x02 },    // 0000 10
2000cdf0e10cSrcweir                         { 7, 0x02 }     // 0000 010
2001cdf0e10cSrcweir                     };
2002cdf0e10cSrcweir                     // convert to index
2003cdf0e10cSrcweir                     nDiff += 3;
2004cdf0e10cSrcweir 
2005cdf0e10cSrcweir                     // emit diff code
2006cdf0e10cSrcweir                     putG4Bits( VerticalCodes[nDiff].mnCodeBits, VerticalCodes[nDiff].mnCode, aBitState );
2007cdf0e10cSrcweir                     nLineIndex = nRunIndex1;
2008cdf0e10cSrcweir                 }
2009cdf0e10cSrcweir                 else
2010cdf0e10cSrcweir                 {   // difference too large, horizontal coding
2011cdf0e10cSrcweir                     // emit horz code 001
2012cdf0e10cSrcweir                     putG4Bits( 3, 0x1, aBitState );
2013cdf0e10cSrcweir                     long nRunIndex2 = findBitRun( pCurLine, nRunIndex1, nW, isSet( pCurLine, nRunIndex1 ) );
2014cdf0e10cSrcweir                     bool bWhiteFirst = ( nLineIndex + nRunIndex1 == 0 || ! isSet( pCurLine, nLineIndex ) );
2015cdf0e10cSrcweir                     putG4Span( nRunIndex1 - nLineIndex, bWhiteFirst, aBitState );
2016cdf0e10cSrcweir                     putG4Span( nRunIndex2 - nRunIndex1, ! bWhiteFirst, aBitState );
2017cdf0e10cSrcweir                     nLineIndex = nRunIndex2;
2018cdf0e10cSrcweir                 }
2019cdf0e10cSrcweir             }
2020cdf0e10cSrcweir             else
2021cdf0e10cSrcweir             {   // emit pass code 0001
2022cdf0e10cSrcweir                 putG4Bits( 4, 0x1, aBitState );
2023cdf0e10cSrcweir                 nLineIndex = nRefIndex2;
2024cdf0e10cSrcweir             }
2025cdf0e10cSrcweir             if( nLineIndex < nW )
2026cdf0e10cSrcweir             {
2027cdf0e10cSrcweir                 bool bSet = isSet( pCurLine, nLineIndex );
2028cdf0e10cSrcweir                 nRunIndex1 = findBitRun( pCurLine, nLineIndex, nW, bSet );
2029cdf0e10cSrcweir                 nRefIndex1 = findBitRun( pRefLine, nLineIndex, nW, ! bSet );
2030cdf0e10cSrcweir                 nRefIndex1 = findBitRun( pRefLine, nRefIndex1, nW, bSet );
2031cdf0e10cSrcweir             }
2032cdf0e10cSrcweir         }
2033cdf0e10cSrcweir 
2034cdf0e10cSrcweir         // the current line is the reference for the next line
2035cdf0e10cSrcweir         pRefLine = pCurLine;
2036cdf0e10cSrcweir     }
2037cdf0e10cSrcweir     // terminate strip with EOFB
2038cdf0e10cSrcweir     putG4Bits( 12, 1, aBitState );
2039cdf0e10cSrcweir     putG4Bits( 12, 1, aBitState );
2040cdf0e10cSrcweir     if( aBitState.mnNextBitPos != 8 )
2041cdf0e10cSrcweir     {
2042cdf0e10cSrcweir         writeBuffer( aBitState.getByte(), 1 );
2043cdf0e10cSrcweir         aBitState.flush();
2044cdf0e10cSrcweir     }
2045cdf0e10cSrcweir 
2046cdf0e10cSrcweir     rtl_freeMemory( pFirstRefLine );
2047cdf0e10cSrcweir }
2048