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