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