LCOV - code coverage report
Current view: top level - vcl/source/filter/wmf - winwmf.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 579 854 67.8 %
Date: 2014-11-03 Functions: 14 14 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "winmtf.hxx"
      21             : 
      22             : #include <boost/scoped_array.hpp>
      23             : #include <boost/optional.hpp>
      24             : #include <vcl/gdimtf.hxx>
      25             : #include <vcl/wmf.hxx>
      26             : #include <rtl/crc.h>
      27             : #include <rtl/tencinfo.h>
      28             : #include <osl/endian.h>
      29             : #include <vcl/svapp.hxx>
      30             : #include <vcl/dibtools.hxx>
      31             : 
      32             : // MS Windows defines
      33             : 
      34             : #define W_META_SETBKCOLOR           0x0201
      35             : #define W_META_SETBKMODE            0x0102
      36             : #define W_META_SETMAPMODE           0x0103
      37             : #define W_META_SETROP2              0x0104
      38             : #define W_META_SETRELABS            0x0105
      39             : #define W_META_SETPOLYFILLMODE      0x0106
      40             : #define W_META_SETSTRETCHBLTMODE    0x0107
      41             : #define W_META_SETTEXTCHAREXTRA     0x0108
      42             : #define W_META_SETTEXTCOLOR         0x0209
      43             : #define W_META_SETTEXTJUSTIFICATION 0x020A
      44             : #define W_META_SETWINDOWORG         0x020B
      45             : #define W_META_SETWINDOWEXT         0x020C
      46             : #define W_META_SETVIEWPORTORG       0x020D
      47             : #define W_META_SETVIEWPORTEXT       0x020E
      48             : #define W_META_OFFSETWINDOWORG      0x020F
      49             : #define W_META_SCALEWINDOWEXT       0x0410
      50             : #define W_META_OFFSETVIEWPORTORG    0x0211
      51             : #define W_META_SCALEVIEWPORTEXT     0x0412
      52             : #define W_META_LINETO               0x0213
      53             : #define W_META_MOVETO               0x0214
      54             : #define W_META_EXCLUDECLIPRECT      0x0415
      55             : #define W_META_INTERSECTCLIPRECT    0x0416
      56             : #define W_META_ARC                  0x0817
      57             : #define W_META_ELLIPSE              0x0418
      58             : #define W_META_FLOODFILL            0x0419
      59             : #define W_META_PIE                  0x081A
      60             : #define W_META_RECTANGLE            0x041B
      61             : #define W_META_ROUNDRECT            0x061C
      62             : #define W_META_PATBLT               0x061D
      63             : #define W_META_SAVEDC               0x001E
      64             : #define W_META_SETPIXEL             0x041F
      65             : #define W_META_OFFSETCLIPRGN        0x0220
      66             : #define W_META_TEXTOUT              0x0521
      67             : #define W_META_BITBLT               0x0922
      68             : #define W_META_STRETCHBLT           0x0B23
      69             : #define W_META_POLYGON              0x0324
      70             : #define W_META_POLYLINE             0x0325
      71             : #define W_META_ESCAPE               0x0626
      72             : #define W_META_RESTOREDC            0x0127
      73             : #define W_META_FILLREGION           0x0228
      74             : #define W_META_FRAMEREGION          0x0429
      75             : #define W_META_INVERTREGION         0x012A
      76             : #define W_META_PAINTREGION          0x012B
      77             : #define W_META_SELECTCLIPREGION     0x012C
      78             : #define W_META_SELECTOBJECT         0x012D
      79             : #define W_META_SETTEXTALIGN         0x012E
      80             : #define W_META_DRAWTEXT             0x062F
      81             : #define W_META_CHORD                0x0830
      82             : #define W_META_SETMAPPERFLAGS       0x0231
      83             : #define W_META_EXTTEXTOUT           0x0a32
      84             : #define W_META_SETDIBTODEV          0x0d33
      85             : #define W_META_SELECTPALETTE        0x0234
      86             : #define W_META_REALIZEPALETTE       0x0035
      87             : #define W_META_ANIMATEPALETTE       0x0436
      88             : #define W_META_SETPALENTRIES        0x0037
      89             : #define W_META_POLYPOLYGON          0x0538
      90             : #define W_META_RESIZEPALETTE        0x0139
      91             : #define W_META_DIBBITBLT            0x0940
      92             : #define W_META_DIBSTRETCHBLT        0x0b41
      93             : #define W_META_DIBCREATEPATTERNBRUSH 0x0142
      94             : #define W_META_STRETCHDIB           0x0f43
      95             : #define W_META_EXTFLOODFILL         0x0548
      96             : #define W_META_RESETDC              0x014C
      97             : #define W_META_STARTDOC             0x014D
      98             : #define W_META_STARTPAGE            0x004F
      99             : #define W_META_ENDPAGE              0x0050
     100             : #define W_META_ABORTDOC             0x0052
     101             : #define W_META_ENDDOC               0x005E
     102             : #define W_META_DELETEOBJECT         0x01f0
     103             : #define W_META_CREATEPALETTE        0x00f7
     104             : #define W_META_CREATEBRUSH          0x00F8
     105             : #define W_META_CREATEPATTERNBRUSH   0x01F9
     106             : #define W_META_CREATEPENINDIRECT    0x02FA
     107             : #define W_META_CREATEFONTINDIRECT   0x02FB
     108             : #define W_META_CREATEBRUSHINDIRECT  0x02FC
     109             : #define W_META_CREATEBITMAPINDIRECT 0x02FD
     110             : #define W_META_CREATEBITMAP         0x06FE
     111             : #define W_META_CREATEREGION         0x06FF
     112             : 
     113       17988 : static void GetWinExtMax( const Point& rSource, Rectangle& rPlaceableBound, const sal_Int16 nMapMode )
     114             : {
     115       17988 :     Point aSource( rSource );
     116       17988 :     if ( nMapMode == MM_HIMETRIC )
     117           0 :         aSource.Y() = -rSource.Y();
     118       17988 :     if ( aSource.X() < rPlaceableBound.Left() )
     119         146 :         rPlaceableBound.Left() = aSource.X();
     120       17988 :     if ( aSource.X() > rPlaceableBound.Right() )
     121         130 :         rPlaceableBound.Right() = aSource.X();
     122       17988 :     if ( aSource.Y() < rPlaceableBound.Top() )
     123         122 :         rPlaceableBound.Top() = aSource.Y();
     124       17988 :     if ( aSource.Y() > rPlaceableBound.Bottom() )
     125         136 :         rPlaceableBound.Bottom() = aSource.Y();
     126       17988 : }
     127             : 
     128         238 : static void GetWinExtMax( const Rectangle& rSource, Rectangle& rPlaceableBound, const sal_Int16 nMapMode )
     129             : {
     130         238 :     GetWinExtMax( rSource.TopLeft(), rPlaceableBound, nMapMode );
     131         238 :     GetWinExtMax( rSource.BottomRight(), rPlaceableBound, nMapMode );
     132         238 : }
     133             : 
     134      116822 : inline Point WMFReader::ReadPoint()
     135             : {
     136      116822 :     short nX = 0, nY = 0;
     137      116822 :     pWMF->ReadInt16( nX ).ReadInt16( nY );
     138      116822 :     return Point( nX, nY );
     139             : }
     140             : 
     141        1552 : inline Point WMFReader::ReadYX()
     142             : {
     143        1552 :     short nX = 0, nY = 0;
     144        1552 :     pWMF->ReadInt16( nY ).ReadInt16( nX );
     145        1552 :     return Point( nX, nY );
     146             : }
     147             : 
     148         152 : Rectangle WMFReader::ReadRectangle()
     149             : {
     150         152 :     Point aBR, aTL;
     151         152 :     aBR = ReadYX();
     152         152 :     aTL = ReadYX();
     153         152 :     aBR.X()--;
     154         152 :     aBR.Y()--;
     155         152 :     return Rectangle( aTL, aBR );
     156             : }
     157             : 
     158         676 : Size WMFReader::ReadYXExt()
     159             : {
     160         676 :     short nW=0, nH=0;
     161         676 :     pWMF->ReadInt16( nH ).ReadInt16( nW );
     162         676 :     return Size( nW, nH );
     163             : }
     164             : 
     165       12876 : void WMFReader::ReadRecordParams( sal_uInt16 nFunc )
     166             : {
     167       12876 :     switch( nFunc )
     168             :     {
     169             :         case W_META_SETBKCOLOR:
     170             :         {
     171         122 :             pOut->SetBkColor( ReadColor() );
     172             :         }
     173         122 :         break;
     174             : 
     175             :         case W_META_SETBKMODE:
     176             :         {
     177         170 :             sal_uInt16 nDat = 0;
     178         170 :             pWMF->ReadUInt16( nDat );
     179         170 :             pOut->SetBkMode( static_cast<BkMode>(nDat) );
     180             :         }
     181         170 :         break;
     182             : 
     183             :         // !!!
     184             :         case W_META_SETMAPMODE:
     185             :         {
     186         112 :             sal_Int16 nMapMode = 0;
     187         112 :             pWMF->ReadInt16( nMapMode );
     188         112 :             pOut->SetMapMode( nMapMode );
     189             :         }
     190         112 :         break;
     191             : 
     192             :         case W_META_SETROP2:
     193             :         {
     194          26 :             sal_uInt16 nROP2 = 0;
     195          26 :             pWMF->ReadUInt16( nROP2 );
     196          26 :             pOut->SetRasterOp( nROP2 );
     197             :         }
     198          26 :         break;
     199             : 
     200             :         case W_META_SETTEXTCOLOR:
     201             :         {
     202         180 :             pOut->SetTextColor( ReadColor() );
     203             :         }
     204         180 :         break;
     205             : 
     206             :         case W_META_SETWINDOWORG:
     207             :         {
     208         280 :             pOut->SetWinOrg( ReadYX() );
     209             :         }
     210         280 :         break;
     211             : 
     212             :         case W_META_SETWINDOWEXT:
     213             :         {
     214         278 :             short nWidth = 0, nHeight = 0;
     215         278 :             pWMF->ReadInt16( nHeight ).ReadInt16( nWidth );
     216         278 :             pOut->SetWinExt( Size( nWidth, nHeight ) );
     217             :         }
     218         278 :         break;
     219             : 
     220             :         case W_META_OFFSETWINDOWORG:
     221             :         {
     222           0 :             short nXAdd = 0, nYAdd = 0;
     223           0 :             pWMF->ReadInt16( nYAdd ).ReadInt16( nXAdd );
     224           0 :             pOut->SetWinOrgOffset( nXAdd, nYAdd );
     225             :         }
     226           0 :         break;
     227             : 
     228             :         case W_META_SCALEWINDOWEXT:
     229             :         {
     230           0 :             short nXNum = 0, nXDenom = 0, nYNum = 0, nYDenom = 0;
     231           0 :             pWMF->ReadInt16( nYDenom ).ReadInt16( nYNum ).ReadInt16( nXDenom ).ReadInt16( nXNum );
     232           0 :             pOut->ScaleWinExt( (double)nXNum / nXDenom, (double)nYNum / nYDenom );
     233             :         }
     234           0 :         break;
     235             : 
     236             :         case W_META_SETVIEWPORTORG:
     237             :         case W_META_SETVIEWPORTEXT:
     238           0 :         break;
     239             : 
     240             :         case W_META_OFFSETVIEWPORTORG:
     241             :         {
     242           0 :             short nXAdd = 0, nYAdd = 0;
     243           0 :             pWMF->ReadInt16( nYAdd ).ReadInt16( nXAdd );
     244           0 :             pOut->SetDevOrgOffset( nXAdd, nYAdd );
     245             :         }
     246           0 :         break;
     247             : 
     248             :         case W_META_SCALEVIEWPORTEXT:
     249             :         {
     250           0 :             short nXNum = 0, nXDenom = 0, nYNum = 0, nYDenom = 0;
     251           0 :             pWMF->ReadInt16( nYDenom ).ReadInt16( nYNum ).ReadInt16( nXDenom ).ReadInt16( nXNum );
     252           0 :             pOut->ScaleDevExt( (double)nXNum / nXDenom, (double)nYNum / nYDenom );
     253             :         }
     254           0 :         break;
     255             : 
     256             :         case W_META_LINETO:
     257             :         {
     258           6 :             pOut->LineTo( ReadYX() );
     259             :         }
     260           6 :         break;
     261             : 
     262             :         case W_META_MOVETO:
     263             :         {
     264          36 :             pOut->MoveTo( ReadYX() );
     265             :         }
     266          36 :         break;
     267             : 
     268             :         case W_META_INTERSECTCLIPRECT:
     269             :         {
     270           6 :             pOut->IntersectClipRect( ReadRectangle() );
     271             :         }
     272           6 :         break;
     273             : 
     274             :         case W_META_RECTANGLE:
     275             :         {
     276           0 :             pOut->DrawRect( ReadRectangle() );
     277             :         }
     278           0 :         break;
     279             : 
     280             :         case W_META_ROUNDRECT:
     281             :         {
     282           0 :             Size aSize( ReadYXExt() );
     283           0 :             pOut->DrawRoundRect( ReadRectangle(), Size( aSize.Width() / 2, aSize.Height() / 2 ) );
     284             :         }
     285           0 :         break;
     286             : 
     287             :         case W_META_ELLIPSE:
     288             :         {
     289           0 :             pOut->DrawEllipse( ReadRectangle() );
     290             :         }
     291           0 :         break;
     292             : 
     293             :         case W_META_ARC:
     294             :         {
     295           0 :             Point aEnd( ReadYX() );
     296           0 :             Point aStart( ReadYX() );
     297           0 :             Rectangle aRect( ReadRectangle() );
     298           0 :             aRect.Justify();
     299           0 :             pOut->DrawArc( aRect, aStart, aEnd );
     300             :         }
     301           0 :         break;
     302             : 
     303             :         case W_META_PIE:
     304             :         {
     305           0 :             Point     aEnd( ReadYX() );
     306           0 :             Point     aStart( ReadYX() );
     307           0 :             Rectangle aRect( ReadRectangle() );
     308           0 :             aRect.Justify();
     309             : 
     310             :             // #i73608# OutputDevice deviates from WMF
     311             :             // semantics. start==end means full ellipse here.
     312           0 :             if( aStart == aEnd )
     313           0 :                 pOut->DrawEllipse( aRect );
     314             :             else
     315           0 :                 pOut->DrawPie( aRect, aStart, aEnd );
     316             :         }
     317           0 :         break;
     318             : 
     319             :         case W_META_CHORD:
     320             :         {
     321           0 :             Point aEnd( ReadYX() );
     322           0 :             Point aStart( ReadYX() );
     323           0 :             Rectangle aRect( ReadRectangle() );
     324           0 :             aRect.Justify();
     325           0 :             pOut->DrawChord( aRect, aStart, aEnd );
     326             :         }
     327           0 :         break;
     328             : 
     329             :         case W_META_POLYGON:
     330             :         {
     331        1080 :             bool bRecordOk = true;
     332             : 
     333        1080 :             sal_uInt16 nPoints(0);
     334        1080 :             pWMF->ReadUInt16(nPoints);
     335             : 
     336        1080 :             if (nPoints > pWMF->remainingSize() / (2 * sizeof(sal_uInt16)))
     337             :             {
     338           2 :                 bRecordOk = false;
     339             :             }
     340             :             else
     341             :             {
     342        1078 :                 Polygon aPoly(nPoints);
     343       78980 :                 for (sal_uInt16 i(0); i < nPoints && pWMF->good(); ++i)
     344       77902 :                     aPoly[ i ] = ReadPoint();
     345        1078 :                 pOut->DrawPolygon(aPoly);
     346             :             }
     347             : 
     348             :             SAL_WARN_IF(!bRecordOk, "vcl.filter", "polygon record has more points than we can handle");
     349             : 
     350        1080 :             bRecordOk &= pWMF->good();
     351             : 
     352        1080 :             if (!bRecordOk)
     353             :             {
     354           2 :                 pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
     355           2 :                 break;
     356             :             }
     357             :         }
     358        1078 :         break;
     359             : 
     360             :         case W_META_POLYPOLYGON:
     361             :         {
     362         120 :             bool bRecordOk = true;
     363         120 :             sal_uInt16 nPolyCount(0);
     364             :             // Number of polygons:
     365         120 :             pWMF->ReadUInt16( nPolyCount );
     366         120 :             if (nPolyCount && pWMF->good())
     367             :             {
     368             :                 // Number of points of each polygon. Determine total number of points
     369         120 :                 boost::scoped_array<sal_uInt16> xPolygonPointCounts(new sal_uInt16[nPolyCount]);
     370         120 :                 sal_uInt16* pnPoints = xPolygonPointCounts.get();
     371         240 :                 tools::PolyPolygon aPolyPoly(nPolyCount, nPolyCount);
     372         120 :                 sal_uInt16 nPoints = 0;
     373         648 :                 for (sal_uInt16 a = 0; a < nPolyCount && pWMF->good(); ++a)
     374             :                 {
     375         528 :                     pWMF->ReadUInt16( pnPoints[a] );
     376             : 
     377         528 :                     if (pnPoints[a] > SAL_MAX_UINT16 - nPoints)
     378             :                     {
     379           0 :                         bRecordOk = false;
     380           0 :                         break;
     381             :                     }
     382             : 
     383         528 :                     nPoints += pnPoints[a];
     384             :                 }
     385             : 
     386             :                 SAL_WARN_IF(!bRecordOk, "vcl.filter", "polypolygon record has more polygons than we can handle");
     387             : 
     388         120 :                 bRecordOk &= pWMF->good();
     389             : 
     390         120 :                 if (!bRecordOk)
     391             :                 {
     392           0 :                     pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
     393           0 :                     break;
     394             :                 }
     395             : 
     396             :                 // Polygon points are:
     397         648 :                 for (sal_uInt16 a = 0; a < nPolyCount && pWMF->good(); ++a)
     398             :                 {
     399         528 :                     const sal_uInt16 nPointCount(pnPoints[a]);
     400         528 :                     boost::scoped_array<Point> xPolygonPoints(new Point[nPointCount]);
     401         528 :                     Point* pPtAry = xPolygonPoints.get();
     402             : 
     403       21744 :                     for(sal_uInt16 b(0); b < nPointCount && pWMF->good(); ++b)
     404             :                     {
     405       21216 :                         pPtAry[b] = ReadPoint();
     406             :                     }
     407             : 
     408         528 :                     aPolyPoly.Insert(Polygon(nPointCount, pPtAry));
     409         528 :                 }
     410             : 
     411         120 :                 bRecordOk &= pWMF->good();
     412             : 
     413         120 :                 if (!bRecordOk)
     414             :                 {
     415           0 :                     pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
     416           0 :                     break;
     417             :                 }
     418             : 
     419         240 :                 pOut->DrawPolyPolygon( aPolyPoly );
     420             :             }
     421             :         }
     422         120 :         break;
     423             : 
     424             :         case W_META_POLYLINE:
     425             :         {
     426          50 :             bool bRecordOk = true;
     427             : 
     428          50 :             sal_uInt16 nPoints(0);
     429          50 :             pWMF->ReadUInt16(nPoints);
     430             : 
     431          50 :             if (nPoints > pWMF->remainingSize() / (2 * sizeof(sal_uInt16)))
     432             :             {
     433           0 :                 bRecordOk = false;
     434             :             }
     435             :             else
     436             :             {
     437          50 :                 Polygon aPoly(nPoints);
     438         152 :                 for (sal_uInt16 i(0); i < nPoints && pWMF->good(); ++i)
     439         102 :                     aPoly[ i ] = ReadPoint();
     440          50 :                 pOut->DrawPolyLine( aPoly );
     441             :             }
     442             : 
     443             :             SAL_WARN_IF(!bRecordOk, "vcl.filter", "polyline record has more points than we can handle");
     444             : 
     445          50 :             bRecordOk &= pWMF->good();
     446             : 
     447          50 :             if (!bRecordOk)
     448             :             {
     449           0 :                 pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
     450           0 :                 break;
     451             :             }
     452             :         }
     453          50 :         break;
     454             : 
     455             :         case W_META_SAVEDC:
     456             :         {
     457         102 :             pOut->Push();
     458             :         }
     459         102 :         break;
     460             : 
     461             :         case W_META_RESTOREDC:
     462             :         {
     463         102 :             pOut->Pop();
     464             :         }
     465         102 :         break;
     466             : 
     467             :         case W_META_SETPIXEL:
     468             :         {
     469           0 :             const Color aColor = ReadColor();
     470           0 :             pOut->DrawPixel( ReadYX(), aColor );
     471             :         }
     472           0 :         break;
     473             : 
     474             :         case W_META_OFFSETCLIPRGN:
     475             :         {
     476           0 :             pOut->MoveClipRegion( ReadYXExt() );
     477             :         }
     478           0 :         break;
     479             : 
     480             :         case W_META_TEXTOUT:
     481             :         {
     482           4 :             sal_uInt16 nLength = 0;
     483           4 :             pWMF->ReadUInt16( nLength );
     484           4 :             if ( nLength )
     485             :             {
     486           4 :                 boost::scoped_array<char> pChar(new char[ ( nLength + 1 ) &~ 1 ]);
     487           4 :                 pWMF->Read( pChar.get(), ( nLength + 1 ) &~ 1 );
     488           8 :                 OUString aText( pChar.get(), nLength, pOut->GetCharSet() );
     489           4 :                 pChar.reset();
     490           4 :                 Point aPosition( ReadYX() );
     491           8 :                 pOut->DrawText( aPosition, aText );
     492             :             }
     493             :         }
     494           4 :         break;
     495             : 
     496             :         case W_META_EXTTEXTOUT:
     497             :         {
     498         302 :             sal_uInt16  nLen = 0, nOptions = 0;
     499         302 :             sal_Int32   nRecordPos, nRecordSize = 0, nOriginalTextLen, nNewTextLen;
     500         302 :             Point       aPosition;
     501         302 :             Rectangle   aRect;
     502         302 :             boost::scoped_array<long> pDXAry;
     503             : 
     504         302 :             pWMF->SeekRel(-6);
     505         302 :             nRecordPos = pWMF->Tell();
     506         302 :             pWMF->ReadInt32( nRecordSize );
     507         302 :             pWMF->SeekRel(2);
     508         302 :             aPosition = ReadYX();
     509         302 :             pWMF->ReadUInt16( nLen );
     510         302 :             pWMF->ReadUInt16( nOptions );
     511             : 
     512         302 :             ComplexTextLayoutMode nTextLayoutMode = TEXT_LAYOUT_DEFAULT;
     513         302 :             if ( nOptions & ETO_RTLREADING )
     514           0 :                 nTextLayoutMode = TEXT_LAYOUT_BIDI_RTL | TEXT_LAYOUT_TEXTORIGIN_LEFT;
     515         302 :             pOut->SetTextLayoutMode( nTextLayoutMode );
     516             :             DBG_ASSERT( ( nOptions & ( ETO_PDY | ETO_GLYPH_INDEX ) ) == 0, "SJ: ETO_PDY || ETO_GLYPH_INDEX in WMF" );
     517             : 
     518             :             // output only makes sense if the text contains characters
     519         302 :             if( nLen )
     520             :             {
     521         302 :                 nOriginalTextLen = nLen;
     522         302 :                 if( nOptions & ETO_CLIPPED )
     523             :                 {
     524         106 :                     const Point aPt1( ReadPoint() );
     525         106 :                     const Point aPt2( ReadPoint() );
     526         106 :                     aRect = Rectangle( aPt1, aPt2 );
     527             :                 }
     528         302 :                 boost::scoped_array<char> pChar(new char[ ( nOriginalTextLen + 1 ) &~ 1 ]);
     529         302 :                 pWMF->Read( pChar.get(), ( nOriginalTextLen + 1 ) &~ 1 );
     530         604 :                 OUString aText( pChar.get(), (sal_uInt16)nOriginalTextLen, pOut->GetCharSet() );// after this conversion the text may contain
     531         302 :                 nNewTextLen = aText.getLength();                                          // less character (japanese version), so the
     532         302 :                 pChar.reset();                                                         // dxAry will not fit
     533             : 
     534         302 :                 if ( nNewTextLen )
     535             :                 {
     536         302 :                     sal_uInt32  nMaxStreamPos = nRecordPos + ( nRecordSize << 1 );
     537         302 :                     sal_Int32   nDxArySize =  nMaxStreamPos - pWMF->Tell();
     538         302 :                     sal_Int32   nDxAryEntries = nDxArySize >> 1;
     539         302 :                     bool        bUseDXAry = false;
     540             : 
     541         302 :                     if ( ( ( nDxAryEntries % nOriginalTextLen ) == 0 ) && ( nNewTextLen <= nOriginalTextLen ) )
     542             :                     {
     543         302 :                         sal_Int16 nDx = 0, nDxTmp = 0;
     544             :                         sal_uInt16 i; //needed just outside the for
     545         302 :                         pDXAry.reset(new long[ nNewTextLen ]);
     546        1442 :                         for (i = 0; i < nNewTextLen; i++ )
     547             :                         {
     548        1274 :                             if ( pWMF->Tell() >= nMaxStreamPos )
     549         134 :                                 break;
     550        1140 :                             pWMF->ReadInt16( nDx );
     551        1140 :                             if ( nNewTextLen != nOriginalTextLen )
     552             :                             {
     553           0 :                                 sal_Unicode nUniChar = aText[i];
     554           0 :                                 OString aTmp(&nUniChar, 1, pOut->GetCharSet());
     555           0 :                                 if ( aTmp.getLength() > 1 )
     556             :                                 {
     557           0 :                                     sal_Int32 nDxCount = aTmp.getLength() - 1;
     558           0 :                                     if ( ( ( nDxCount * 2 ) + pWMF->Tell() ) > nMaxStreamPos )
     559           0 :                                         break;
     560           0 :                                     while ( nDxCount-- )
     561             :                                     {
     562           0 :                                         pWMF->ReadInt16( nDxTmp );
     563           0 :                                         nDx = nDx + nDxTmp;
     564             :                                     }
     565           0 :                                 }
     566             :                             }
     567        1140 :                             pDXAry[ i ] = nDx;
     568             :                         }
     569         302 :                         if ( i == nNewTextLen )
     570         168 :                             bUseDXAry = true;
     571             :                     }
     572         302 :                     if ( pDXAry && bUseDXAry )
     573         168 :                         pOut->DrawText( aPosition, aText, pDXAry.get() );
     574             :                     else
     575         134 :                         pOut->DrawText( aPosition, aText );
     576         302 :                 }
     577         302 :             }
     578             :         }
     579         302 :         break;
     580             : 
     581             :         case W_META_SELECTOBJECT:
     582             :         {
     583        5612 :             sal_Int16   nObjIndex = 0;
     584        5612 :             pWMF->ReadInt16( nObjIndex );
     585        5612 :             pOut->SelectObject( nObjIndex );
     586             :         }
     587        5612 :         break;
     588             : 
     589             :         case W_META_SETTEXTALIGN:
     590             :         {
     591         156 :             sal_uInt16  nAlign = 0;
     592         156 :             pWMF->ReadUInt16( nAlign );
     593         156 :             pOut->SetTextAlign( nAlign );
     594             :         }
     595         156 :         break;
     596             : 
     597             :         case W_META_BITBLT:
     598             :         {
     599             :             // 0-3   : nWinROP                      #93454#
     600             :             // 4-5   : y offset of source bitmap
     601             :             // 6-7   : x offset of source bitmap
     602             :             // 8-9   : used height of source bitmap
     603             :             // 10-11 : used width  of source bitmap
     604             :             // 12-13 : destination position y (in pixel)
     605             :             // 14-15 : destination position x (in pixel)
     606             :             // 16-17 : dont know
     607             :             // 18-19 : Width Bitmap in Pixel
     608             :             // 20-21 : Height Bitmap in Pixel
     609             :             // 22-23 : bytes per scanline
     610             :             // 24    : planes
     611             :             // 25    : bitcount
     612             : 
     613           0 :             sal_Int32   nWinROP = 0;
     614           0 :             sal_uInt16  nSx = 0, nSy = 0, nSxe = 0, nSye = 0, nDontKnow = 0, nWidth = 0, nHeight = 0, nBytesPerScan = 0;
     615             :             sal_uInt8   nPlanes, nBitCount;
     616             : 
     617           0 :             pWMF->ReadInt32( nWinROP )
     618           0 :                  .ReadUInt16( nSy ).ReadUInt16( nSx ).ReadUInt16( nSye ).ReadUInt16( nSxe );
     619           0 :             Point aPoint( ReadYX() );
     620           0 :             pWMF->ReadUInt16( nDontKnow ).ReadUInt16( nWidth ).ReadUInt16( nHeight ).ReadUInt16( nBytesPerScan ).ReadUChar( nPlanes ).ReadUChar( nBitCount );
     621             : 
     622           0 :             if ( nWidth && nHeight && ( nPlanes == 1 ) && ( nBitCount == 1 ) )
     623             :             {
     624           0 :                 Bitmap aBmp( Size( nWidth, nHeight ), nBitCount );
     625             :                 BitmapWriteAccess* pAcc;
     626           0 :                 pAcc = aBmp.AcquireWriteAccess();
     627           0 :                 if ( pAcc )
     628             :                 {
     629           0 :                     for (sal_uInt16 y = 0; y < nHeight; y++ )
     630             :                     {
     631           0 :                         sal_uInt16 x = 0;
     632           0 :                         for (sal_uInt16 scan = 0; scan < nBytesPerScan; scan++ )
     633             :                         {
     634           0 :                             sal_Int8 nEightPixels = 0;
     635           0 :                             pWMF->ReadSChar( nEightPixels );
     636           0 :                             for (sal_Int8 i = 7; i >= 0; i-- )
     637             :                             {
     638           0 :                                 if ( x < nWidth )
     639             :                                 {
     640           0 :                                     pAcc->SetPixelIndex( y, x, (nEightPixels>>i)&1 );
     641             :                                 }
     642           0 :                                 x++;
     643             :                             }
     644             :                         }
     645             :                     }
     646           0 :                     aBmp.ReleaseAccess( pAcc );
     647           0 :                     if ( nSye && nSxe &&
     648           0 :                         ( ( nSx + nSxe ) <= aBmp.GetSizePixel().Width() ) &&
     649           0 :                             ( ( nSy + nSye <= aBmp.GetSizePixel().Height() ) ) )
     650             :                     {
     651           0 :                         Rectangle aCropRect( Point( nSx, nSy ), Size( nSxe, nSye ) );
     652           0 :                         aBmp.Crop( aCropRect );
     653             :                     }
     654           0 :                     Rectangle aDestRect( aPoint, Size( nSxe, nSye ) );
     655           0 :                     aBmpSaveList.push_back( new BSaveStruct( aBmp, aDestRect, nWinROP, pOut->GetFillStyle () ) );
     656           0 :                 }
     657             :             }
     658             :         }
     659           0 :         break;
     660             : 
     661             :         case W_META_STRETCHBLT:
     662             :         case W_META_DIBBITBLT:
     663             :         case W_META_DIBSTRETCHBLT:
     664             :         case W_META_STRETCHDIB:
     665             :         {
     666         112 :             sal_Int32   nWinROP = 0;
     667         112 :             sal_uInt16  nSx = 0, nSy = 0, nSxe = 0, nSye = 0, nUsage = 0;
     668         112 :             Bitmap      aBmp;
     669             : 
     670         112 :             pWMF->ReadInt32( nWinROP );
     671             : 
     672         112 :             if( nFunc == W_META_STRETCHDIB )
     673           8 :                 pWMF->ReadUInt16( nUsage );
     674             : 
     675             :             // nSye and nSxe is the number of pixels that has to been used
     676             :             // If they are set to zero, it is as indicator not to scale the bitmap later
     677             : 
     678         112 :             if( nFunc == W_META_STRETCHDIB || nFunc == W_META_STRETCHBLT || nFunc == W_META_DIBSTRETCHBLT )
     679          18 :                 pWMF->ReadUInt16( nSye ).ReadUInt16( nSxe );
     680             : 
     681             :             // nSy and nx is the offset of the first pixel
     682         112 :             pWMF->ReadUInt16( nSy ).ReadUInt16( nSx );
     683             : 
     684         112 :             if( nFunc == W_META_STRETCHDIB || nFunc == W_META_DIBBITBLT || nFunc == W_META_DIBSTRETCHBLT )
     685             :             {
     686         112 :                 if ( nWinROP == PATCOPY )
     687           2 :                     pWMF->ReadUInt16( nUsage );    // i don't know anything of this parameter, so its called nUsage
     688             :                                         // pOut->DrawRect( Rectangle( ReadYX(), aDestSize ), false );
     689             : 
     690         112 :                 Size aDestSize( ReadYXExt() );
     691         112 :                 if ( aDestSize.Width() && aDestSize.Height() )  // #92623# do not try to read buggy bitmaps
     692             :                 {
     693         112 :                     Rectangle aDestRect( ReadYX(), aDestSize );
     694         112 :                     if ( nWinROP != PATCOPY )
     695         110 :                         ReadDIB(aBmp, *pWMF, false);
     696             : 
     697             :                     // test if it is sensible to crop
     698         372 :                     if ( nSye && nSxe &&
     699         314 :                         ( ( nSx + nSxe ) <= aBmp.GetSizePixel().Width() ) &&
     700         148 :                             ( ( nSy + nSye <= aBmp.GetSizePixel().Height() ) ) )
     701             :                     {
     702          18 :                         Rectangle aCropRect( Point( nSx, nSy ), Size( nSxe, nSye ) );
     703          18 :                         aBmp.Crop( aCropRect );
     704             :                     }
     705         112 :                     aBmpSaveList.push_back( new BSaveStruct( aBmp, aDestRect, nWinROP, pOut->GetFillStyle () ) );
     706             :                 }
     707         112 :             }
     708             :         }
     709         112 :         break;
     710             : 
     711             :         case W_META_DIBCREATEPATTERNBRUSH:
     712             :         {
     713           0 :             Bitmap  aBmp;
     714             :             BitmapReadAccess* pBmp;
     715           0 :             sal_uInt32  nRed = 0, nGreen = 0, nBlue = 0, nCount = 1;
     716           0 :             sal_uInt16  nFunction = 0;
     717             : 
     718           0 :             pWMF->ReadUInt16( nFunction ).ReadUInt16( nFunction );
     719             : 
     720           0 :             ReadDIB(aBmp, *pWMF, false);
     721           0 :             pBmp = aBmp.AcquireReadAccess();
     722           0 :             if ( pBmp )
     723             :             {
     724           0 :                 for ( sal_Int32 y = 0; y < pBmp->Height(); y++ )
     725             :                 {
     726           0 :                     for ( sal_Int32 x = 0; x < pBmp->Width(); x++ )
     727             :                     {
     728           0 :                         const BitmapColor aColor( pBmp->GetColor( y, x ) );
     729             : 
     730           0 :                         nRed += aColor.GetRed();
     731           0 :                         nGreen += aColor.GetGreen();
     732           0 :                         nBlue += aColor.GetBlue();
     733           0 :                     }
     734             :                 }
     735           0 :                 nCount = pBmp->Height() * pBmp->Width();
     736           0 :                 if ( !nCount )
     737           0 :                     nCount++;
     738           0 :                 aBmp.ReleaseAccess( pBmp );
     739             :             }
     740           0 :             Color aColor( (sal_uInt8)( nRed / nCount ), (sal_uInt8)( nGreen / nCount ), (sal_uInt8)( nBlue / nCount ) );
     741           0 :             pOut->CreateObject( GDI_BRUSH, new WinMtfFillStyle( aColor, false ) );
     742             :         }
     743           0 :         break;
     744             : 
     745             :         case W_META_DELETEOBJECT:
     746             :         {
     747        1042 :             sal_Int16 nIndex = 0;
     748        1042 :             pWMF->ReadInt16( nIndex );
     749        1042 :             pOut->DeleteObject( nIndex );
     750             :         }
     751        1042 :         break;
     752             : 
     753             :         case W_META_CREATEPALETTE:
     754             :         {
     755           0 :             pOut->CreateObject( GDI_DUMMY );
     756             :         }
     757           0 :         break;
     758             : 
     759             :         case W_META_CREATEBRUSH:
     760             :         {
     761           0 :             pOut->CreateObject( GDI_BRUSH, new WinMtfFillStyle( Color( COL_WHITE ), false ) );
     762             :         }
     763           0 :         break;
     764             : 
     765             :         case W_META_CREATEPATTERNBRUSH:
     766             :         {
     767           0 :             pOut->CreateObject( GDI_BRUSH, new WinMtfFillStyle( Color( COL_WHITE ), false ) );
     768             :         }
     769           0 :         break;
     770             : 
     771             :         case W_META_CREATEPENINDIRECT:
     772             :         {
     773         152 :             LineInfo   aLineInfo;
     774         152 :             sal_uInt16 nStyle = 0;
     775         152 :             sal_uInt16 nWidth = 0;
     776         152 :             sal_uInt16 nHeight = 0;
     777             : 
     778         152 :             pWMF->ReadUInt16(nStyle);
     779         152 :             pWMF->ReadUInt16(nWidth);
     780         152 :             pWMF->ReadUInt16(nHeight);
     781             : 
     782         152 :             if (nWidth > 0)
     783          78 :                 aLineInfo.SetWidth(nWidth);
     784             : 
     785         152 :             bool bTransparent = false;
     786         152 :             switch( nStyle & 0xFF )
     787             :             {
     788             :                 case PS_DASHDOTDOT :
     789           0 :                     aLineInfo.SetStyle( LINE_DASH );
     790           0 :                     aLineInfo.SetDashCount( 1 );
     791           0 :                     aLineInfo.SetDotCount( 2 );
     792           0 :                     aLineInfo.SetDashLen( 150 );
     793           0 :                     aLineInfo.SetDotLen( 30 );
     794           0 :                     aLineInfo.SetDistance( 50 );
     795           0 :                 break;
     796             :                 case PS_DASHDOT :
     797           0 :                     aLineInfo.SetStyle( LINE_DASH );
     798           0 :                     aLineInfo.SetDashCount( 1 );
     799           0 :                     aLineInfo.SetDotCount( 1 );
     800           0 :                     aLineInfo.SetDashLen( 150 );
     801           0 :                     aLineInfo.SetDotLen( 30 );
     802           0 :                     aLineInfo.SetDistance( 90 );
     803           0 :                 break;
     804             :                 case PS_DOT :
     805           0 :                     aLineInfo.SetStyle( LINE_DASH );
     806           0 :                     aLineInfo.SetDashCount( 0 );
     807           0 :                     aLineInfo.SetDotCount( 1 );
     808           0 :                     aLineInfo.SetDotLen( 30 );
     809           0 :                     aLineInfo.SetDistance( 50 );
     810           0 :                 break;
     811             :                 case PS_DASH :
     812           0 :                     aLineInfo.SetStyle( LINE_DASH );
     813           0 :                     aLineInfo.SetDashCount( 1 );
     814           0 :                     aLineInfo.SetDotCount( 0 );
     815           0 :                     aLineInfo.SetDashLen( 225 );
     816           0 :                     aLineInfo.SetDistance( 100 );
     817           0 :                 break;
     818             :                 case PS_NULL :
     819          40 :                     bTransparent = true;
     820          40 :                     aLineInfo.SetStyle( LINE_NONE );
     821          40 :                 break;
     822             :                 default :
     823             :                 case PS_INSIDEFRAME :
     824             :                 case PS_SOLID :
     825         112 :                     aLineInfo.SetStyle( LINE_SOLID );
     826             :             }
     827         152 :             switch( nStyle & 0xF00 )
     828             :             {
     829             :                 case PS_ENDCAP_ROUND :
     830         152 :                     aLineInfo.SetLineCap( com::sun::star::drawing::LineCap_ROUND );
     831         152 :                 break;
     832             :                 case PS_ENDCAP_SQUARE :
     833           0 :                     aLineInfo.SetLineCap( com::sun::star::drawing::LineCap_SQUARE );
     834           0 :                 break;
     835             :                 case PS_ENDCAP_FLAT :
     836             :                 default :
     837           0 :                     aLineInfo.SetLineCap( com::sun::star::drawing::LineCap_BUTT );
     838             :             }
     839         152 :             switch( nStyle & 0xF000 )
     840             :             {
     841             :                 case PS_JOIN_ROUND :
     842         152 :                     aLineInfo.SetLineJoin ( basegfx::B2DLINEJOIN_ROUND );
     843         152 :                 break;
     844             :                 case PS_JOIN_MITER :
     845           0 :                     aLineInfo.SetLineJoin ( basegfx::B2DLINEJOIN_MITER );
     846           0 :                 break;
     847             :                 case PS_JOIN_BEVEL :
     848           0 :                     aLineInfo.SetLineJoin ( basegfx::B2DLINEJOIN_BEVEL );
     849           0 :                 break;
     850             :                 default :
     851           0 :                     aLineInfo.SetLineJoin ( basegfx::B2DLINEJOIN_NONE );
     852             :             }
     853         152 :             pOut->CreateObject( GDI_PEN, new WinMtfLineStyle( ReadColor(), aLineInfo, bTransparent ) );
     854             :         }
     855         152 :         break;
     856             : 
     857             :         case W_META_CREATEBRUSHINDIRECT:
     858             :         {
     859        1016 :             sal_uInt16  nStyle = 0;
     860        1016 :             pWMF->ReadUInt16( nStyle );
     861        1016 :             pOut->CreateObject( GDI_BRUSH, new WinMtfFillStyle( ReadColor(), ( nStyle == BS_HOLLOW ) ) );
     862             :         }
     863        1016 :         break;
     864             : 
     865             :         case W_META_CREATEFONTINDIRECT:
     866             :         {
     867         294 :             Size aFontSize;
     868             :             char lfFaceName[LF_FACESIZE];
     869         294 :             sal_Int16 lfEscapement = 0;
     870         294 :             sal_Int16 lfOrientation = 0;
     871         294 :             sal_Int16 lfWeight = 0;
     872             : 
     873         294 :             LOGFONTW aLogFont;
     874         294 :             aFontSize = ReadYXExt();
     875         294 :             pWMF->ReadInt16( lfEscapement );
     876         294 :             pWMF->ReadInt16( lfOrientation );
     877         294 :             pWMF->ReadInt16( lfWeight );
     878         294 :             pWMF->ReadUChar( aLogFont.lfItalic );
     879         294 :             pWMF->ReadUChar( aLogFont.lfUnderline );
     880         294 :             pWMF->ReadUChar( aLogFont.lfStrikeOut );
     881         294 :             pWMF->ReadUChar( aLogFont.lfCharSet );
     882         294 :             pWMF->ReadUChar( aLogFont.lfOutPrecision );
     883         294 :             pWMF->ReadUChar( aLogFont.lfClipPrecision );
     884         294 :             pWMF->ReadUChar( aLogFont.lfQuality );
     885         294 :             pWMF->ReadUChar( aLogFont.lfPitchAndFamily );
     886         294 :             pWMF->Read( lfFaceName, LF_FACESIZE );
     887         294 :             aLogFont.lfWidth = aFontSize.Width();
     888         294 :             aLogFont.lfHeight = aFontSize.Height();
     889         294 :             aLogFont.lfEscapement = lfEscapement;
     890         294 :             aLogFont.lfOrientation = lfOrientation;
     891         294 :             aLogFont.lfWeight = lfWeight;
     892             : 
     893             :             rtl_TextEncoding eCharSet;
     894         294 :             if ( ( aLogFont.lfCharSet == OEM_CHARSET ) || ( aLogFont.lfCharSet == DEFAULT_CHARSET ) )
     895          10 :                 eCharSet = osl_getThreadTextEncoding();
     896             :             else
     897         284 :                 eCharSet = rtl_getTextEncodingFromWindowsCharset( aLogFont.lfCharSet );
     898         294 :             if ( eCharSet == RTL_TEXTENCODING_DONTKNOW )
     899           0 :                 eCharSet = osl_getThreadTextEncoding();
     900         294 :             if ( eCharSet == RTL_TEXTENCODING_SYMBOL )
     901          16 :                 eCharSet = RTL_TEXTENCODING_MS_1252;
     902         294 :             aLogFont.alfFaceName = OUString( lfFaceName, strlen(lfFaceName), eCharSet );
     903             : 
     904         294 :             pOut->CreateObject( GDI_FONT, new WinMtfFontStyle( aLogFont ) );
     905             :         }
     906         294 :         break;
     907             : 
     908             :         case W_META_CREATEBITMAPINDIRECT:
     909             :         {
     910           0 :             pOut->CreateObject( GDI_DUMMY );
     911             :         }
     912           0 :         break;
     913             : 
     914             :         case W_META_CREATEBITMAP:
     915             :         {
     916           0 :             pOut->CreateObject( GDI_DUMMY );
     917             :         }
     918           0 :         break;
     919             : 
     920             :         case W_META_CREATEREGION:
     921             :         {
     922           0 :             pOut->CreateObject( GDI_DUMMY );
     923             :         }
     924           0 :         break;
     925             : 
     926             :         case W_META_EXCLUDECLIPRECT :
     927             :         {
     928           0 :             pOut->ExcludeClipRect( ReadRectangle() );
     929             :         }
     930           0 :         break;
     931             : 
     932             :         case W_META_PATBLT:
     933             :         {
     934         176 :             sal_uInt32 nROP = 0, nOldROP = 0;
     935         176 :             pWMF->ReadUInt32( nROP );
     936         176 :             Size aSize = ReadYXExt();
     937         176 :             nOldROP = pOut->SetRasterOp( nROP );
     938         176 :             pOut->DrawRect( Rectangle( ReadYX(), aSize ), false );
     939         176 :             pOut->SetRasterOp( nOldROP );
     940             :         }
     941         176 :         break;
     942             : 
     943             :         case W_META_SELECTCLIPREGION:
     944             :         {
     945           2 :             sal_Int16 nObjIndex = 0;
     946           2 :             pWMF->ReadInt16( nObjIndex );
     947           2 :             if ( !nObjIndex )
     948             :             {
     949           2 :                 tools::PolyPolygon aEmptyPolyPoly;
     950           2 :                 pOut->SetClipPath( aEmptyPolyPoly, RGN_COPY, true );
     951             :             }
     952             :         }
     953           2 :         break;
     954             : 
     955             :         case W_META_ESCAPE :
     956             :         {
     957             :             // nRecSize has been checked previously to be greater than 3
     958         114 :             sal_uInt64 nMetaRecSize = static_cast< sal_uInt64 >( nRecSize - 2 ) * 2;
     959         114 :             sal_uInt64 nMetaRecEndPos = pWMF->Tell() + nMetaRecSize;
     960             : 
     961             :             // taking care that nRecSize does not exceed the maximal stream position
     962         114 :             if ( nMetaRecEndPos > nEndPos )
     963             :             {
     964           0 :                 pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
     965           0 :                 break;
     966             :             }
     967         114 :             if ( nRecSize >= 4 )    // minimal escape length
     968             :             {
     969         114 :                 sal_uInt16  nMode = 0, nLen = 0;
     970         114 :                 pWMF->ReadUInt16( nMode )
     971         114 :                      .ReadUInt16( nLen );
     972         114 :                 if ( ( nMode == W_MFCOMMENT ) && ( nLen >= 4 ) )
     973             :                 {
     974         114 :                     sal_uInt32 nNewMagic = 0; // we have to read int32 for
     975         114 :                     pWMF->ReadUInt32( nNewMagic );   // META_ESCAPE_ENHANCED_METAFILE CommentIdentifier
     976             : 
     977         114 :                     if( nNewMagic == 0x2c2a4f4f &&  nLen >= 14 )
     978             :                     {
     979           0 :                         sal_uInt16 nMagic2 = 0;
     980           0 :                         pWMF->ReadUInt16( nMagic2 );
     981           0 :                         if( nMagic2 == 0x0a ) // 2nd half of magic
     982             :                         {                     // continue with private escape
     983           0 :                             sal_uInt32 nCheck = 0, nEsc = 0;
     984           0 :                             pWMF->ReadUInt32( nCheck )
     985           0 :                                  .ReadUInt32( nEsc );
     986             : 
     987           0 :                             sal_uInt32 nEscLen = nLen - 14;
     988           0 :                             if ( nEscLen <= ( nRecSize * 2 ) )
     989             :                             {
     990             : #ifdef OSL_BIGENDIAN
     991             :                                 sal_uInt32 nTmp = OSL_SWAPDWORD( nEsc );
     992             :                                 sal_uInt32 nCheckSum = rtl_crc32( 0, &nTmp, 4 );
     993             : #else
     994           0 :                                 sal_uInt32 nCheckSum = rtl_crc32( 0, &nEsc, 4 );
     995             : #endif
     996           0 :                                 boost::scoped_array<sal_Int8> pData;
     997             : 
     998           0 :                                 if ( ( static_cast< sal_uInt64 >( nEscLen ) + pWMF->Tell() ) > nMetaRecEndPos )
     999             :                                 {
    1000           0 :                                     pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
    1001           0 :                                     break;
    1002             :                                 }
    1003           0 :                                 if ( nEscLen > 0 )
    1004             :                                 {
    1005           0 :                                     pData.reset(new sal_Int8[ nEscLen ]);
    1006           0 :                                     pWMF->Read( pData.get(), nEscLen );
    1007           0 :                                     nCheckSum = rtl_crc32( nCheckSum, pData.get(), nEscLen );
    1008             :                                 }
    1009           0 :                                 if ( nCheck == nCheckSum )
    1010             :                                 {
    1011           0 :                                     switch( nEsc )
    1012             :                                     {
    1013             :                                         case PRIVATE_ESCAPE_UNICODE :
    1014             :                                         {
    1015             :                                             // we will use text instead of polygons only if we have the correct font
    1016           0 :                                             if ( Application::GetDefaultDevice()->IsFontAvailable( pOut->GetFont().GetName() ) )
    1017             :                                             {
    1018           0 :                                                 Point  aPt;
    1019           0 :                                                 OUString aString;
    1020             :                                                 sal_uInt32  nStringLen, nDXCount;
    1021           0 :                                                 boost::scoped_array<long> pDXAry;
    1022           0 :                                                 SvMemoryStream aMemoryStream( nEscLen );
    1023           0 :                                                 aMemoryStream.Write( pData.get(), nEscLen );
    1024           0 :                                                 aMemoryStream.Seek( STREAM_SEEK_TO_BEGIN );
    1025           0 :                                                 sal_Int32 nTmpX(0), nTmpY(0);
    1026           0 :                                                 aMemoryStream.ReadInt32( nTmpX )
    1027           0 :                                                              .ReadInt32( nTmpY )
    1028           0 :                                                              .ReadUInt32( nStringLen );
    1029           0 :                                                  aPt.X() = nTmpX;
    1030           0 :                                                  aPt.Y() = nTmpY;
    1031             : 
    1032           0 :                                                 if ( ( static_cast< sal_uInt64 >( nStringLen ) * sizeof( sal_Unicode ) ) < ( nEscLen - aMemoryStream.Tell() ) )
    1033             :                                                 {
    1034             : 
    1035           0 :                                                     aString = read_uInt16s_ToOUString(aMemoryStream, nStringLen);
    1036           0 :                                                     aMemoryStream.ReadUInt32( nDXCount );
    1037           0 :                                                     if ( ( static_cast< sal_uInt64 >( nDXCount ) * sizeof( sal_Int32 ) ) >= ( nEscLen - aMemoryStream.Tell() ) )
    1038           0 :                                                         nDXCount = 0;
    1039           0 :                                                     if ( nDXCount )
    1040           0 :                                                         pDXAry.reset(new long[ nDXCount ]);
    1041           0 :                                                     for  (sal_uInt32 i = 0; i < nDXCount; i++ )
    1042             :                                                     {
    1043             :                                                         sal_Int32 val;
    1044           0 :                                                         aMemoryStream.ReadInt32( val);
    1045           0 :                                                         pDXAry[ i ] = val;
    1046             :                                                     }
    1047           0 :                                                     aMemoryStream.ReadUInt32( nSkipActions );
    1048           0 :                                                     pOut->DrawText( aPt, aString, pDXAry.get() );
    1049           0 :                                                 }
    1050             :                                             }
    1051             :                                         }
    1052           0 :                                         break;
    1053             :                                     }
    1054           0 :                                 }
    1055             :                             }
    1056           0 :                         }
    1057             :                     }
    1058         114 :                     else if ( (nNewMagic == static_cast< sal_uInt32 >(0x43464D57)) && (nLen >= 34) && ( (sal_Int32)(nLen + 10) <= (sal_Int32)(nRecSize * 2) ))
    1059             :                     {
    1060           4 :                         sal_uInt32 nComType = 0, nVersion = 0, nFlags = 0, nComRecCount = 0,
    1061           4 :                                    nCurRecSize = 0, nRemainingSize = 0, nEMFTotalSize = 0;
    1062           4 :                         sal_uInt16 nCheck = 0;
    1063             : 
    1064           4 :                         pWMF->ReadUInt32( nComType ).ReadUInt32( nVersion ).ReadUInt16( nCheck ).ReadUInt32( nFlags )
    1065           4 :                              .ReadUInt32( nComRecCount ).ReadUInt32( nCurRecSize )
    1066           4 :                              .ReadUInt32( nRemainingSize ).ReadUInt32( nEMFTotalSize ); // the nRemainingSize is not mentioned in MSDN documentation
    1067             :                                                                   // but it seems to be required to read in data produced by OLE
    1068             : 
    1069           4 :                         if( nComType == 0x01 && nVersion == 0x10000 && nComRecCount )
    1070             :                         {
    1071           4 :                             if( !nEMFRec )
    1072             :                             {   // first EMF comment
    1073           4 :                                 nEMFRecCount    = nComRecCount;
    1074           4 :                                 nEMFSize        = nEMFTotalSize;
    1075           4 :                                 pEMFStream = new SvMemoryStream( nEMFSize );
    1076             :                             }
    1077           0 :                             else if( ( nEMFRecCount != nComRecCount ) || ( nEMFSize != nEMFTotalSize ) ) // add additional checks here
    1078             :                             {
    1079             :                                 // total records should be the same as in previous comments
    1080           0 :                                 nEMFRecCount = 0xFFFFFFFF;
    1081           0 :                                 delete pEMFStream;
    1082           0 :                                 pEMFStream = NULL;
    1083             :                             }
    1084           4 :                             nEMFRec++;
    1085             : 
    1086           4 :                             if( pEMFStream && nCurRecSize + 34 > nLen )
    1087             :                             {
    1088           0 :                                 nEMFRecCount = 0xFFFFFFFF;
    1089           0 :                                 delete pEMFStream;
    1090           0 :                                 pEMFStream = NULL;
    1091             :                             }
    1092             : 
    1093           4 :                             if( pEMFStream )
    1094             :                             {
    1095           4 :                                 boost::scoped_array<sal_Int8> pBuf(new sal_Int8[ nCurRecSize ]);
    1096           4 :                                 sal_uInt32 nCount = pWMF->Read( pBuf.get(), nCurRecSize );
    1097           4 :                                 if( nCount == nCurRecSize )
    1098           4 :                                     pEMFStream->Write( pBuf.get(), nCount );
    1099             :                             }
    1100             :                         }
    1101             :                     }
    1102             :                 }
    1103             :             }
    1104             :         }
    1105         114 :         break;
    1106             : 
    1107             :         case W_META_SETRELABS:
    1108             :         case W_META_SETPOLYFILLMODE:
    1109             :         case W_META_SETSTRETCHBLTMODE:
    1110             :         case W_META_SETTEXTCHAREXTRA:
    1111             :         case W_META_SETTEXTJUSTIFICATION:
    1112             :         case W_META_FLOODFILL :
    1113             :         case W_META_FILLREGION:
    1114             :         case W_META_FRAMEREGION:
    1115             :         case W_META_INVERTREGION:
    1116             :         case W_META_PAINTREGION:
    1117             :         case W_META_DRAWTEXT:
    1118             :         case W_META_SETMAPPERFLAGS:
    1119             :         case W_META_SETDIBTODEV:
    1120             :         case W_META_SELECTPALETTE:
    1121             :         case W_META_REALIZEPALETTE:
    1122             :         case W_META_ANIMATEPALETTE:
    1123             :         case W_META_SETPALENTRIES:
    1124             :         case W_META_RESIZEPALETTE:
    1125             :         case W_META_EXTFLOODFILL:
    1126             :         case W_META_RESETDC:
    1127             :         case W_META_STARTDOC:
    1128             :         case W_META_STARTPAGE:
    1129             :         case W_META_ENDPAGE:
    1130             :         case W_META_ABORTDOC:
    1131             :         case W_META_ENDDOC:
    1132        1220 :         break;
    1133             :     }
    1134       12876 : }
    1135             : 
    1136             : static const long   aMaxWidth = 1024;
    1137             : 
    1138         196 : bool WMFReader::ReadHeader()
    1139             : {
    1140         196 :     sal_Size nStrmPos = pWMF->Tell();
    1141             : 
    1142         196 :     sal_uInt32 nPlaceableMetaKey(0);
    1143             :     // if available read the METAFILEHEADER
    1144         196 :     pWMF->ReadUInt32( nPlaceableMetaKey );
    1145         196 :     if (!pWMF->good())
    1146           0 :         return false;
    1147             : 
    1148         196 :     Rectangle aPlaceableBound;
    1149             : 
    1150         196 :     bool bPlaceable = nPlaceableMetaKey == 0x9ac6cdd7L;
    1151             : 
    1152             :     SAL_INFO("vcl.wmf", "Placeable: \"" << (bPlaceable ? "yes" : "no") << "\"");
    1153             : 
    1154         196 :     if (bPlaceable)
    1155             :     {
    1156             :         //TODO do some real error handling here
    1157             :         sal_Int16 nVal;
    1158             : 
    1159             :         // Skip reserved bytes
    1160         112 :         pWMF->SeekRel(2);
    1161             : 
    1162             :         // BoundRect
    1163         112 :         pWMF->ReadInt16( nVal );
    1164         112 :         aPlaceableBound.Left() = nVal;
    1165         112 :         pWMF->ReadInt16( nVal );
    1166         112 :         aPlaceableBound.Top() = nVal;
    1167         112 :         pWMF->ReadInt16( nVal );
    1168         112 :         aPlaceableBound.Right() = nVal;
    1169         112 :         pWMF->ReadInt16( nVal );
    1170         112 :         aPlaceableBound.Bottom() = nVal;
    1171             : 
    1172             :         // inch
    1173         112 :         pWMF->ReadUInt16( nUnitsPerInch );
    1174             : 
    1175             :         // reserved
    1176         112 :         pWMF->SeekRel( 4 );
    1177             : 
    1178             :         // Skip and don't check the checksum
    1179         112 :         pWMF->SeekRel( 2 );
    1180             :     }
    1181             :     else
    1182             :     {
    1183          84 :         nUnitsPerInch = 96;
    1184             : 
    1185             : 
    1186          84 :         if (   pExternalHeader != NULL
    1187           8 :             && pExternalHeader->xExt > 0
    1188           6 :             && pExternalHeader->yExt > 0
    1189           6 :             && (pExternalHeader->mapMode == MM_ISOTROPIC || pExternalHeader->mapMode == MM_ANISOTROPIC))
    1190             :         {
    1191             :             // #n417818#: If we have an external header then overwrite the bounds!
    1192             :             Rectangle aExtRect(0, 0,
    1193           6 :                           (double) pExternalHeader->xExt * 567 * nUnitsPerInch / 1440000,
    1194          12 :                           (double) pExternalHeader->yExt * 567 * nUnitsPerInch / 1440000);
    1195           6 :             aPlaceableBound = aExtRect;
    1196             : 
    1197             :             SAL_INFO("vcl.wmf", "External header size "
    1198             :                        " t: " << aPlaceableBound.Left()  << " l: " << aPlaceableBound.Top()
    1199             :                     << " b: " << aPlaceableBound.Right() << " r: " << aPlaceableBound.Bottom());
    1200             : 
    1201           6 :             pOut->SetMapMode( pExternalHeader->mapMode );
    1202             :         }
    1203             :         else
    1204             :         {
    1205          78 :             pWMF->Seek( nStrmPos + 18 );    // set the streampos to the start of the metaactions
    1206          78 :             GetPlaceableBound( aPlaceableBound, pWMF );
    1207             : 
    1208             :             // The image size is not known so normalize the calculated bounds so that the
    1209             :             // resulting image is not too big
    1210          78 :             const double fMaxWidth = static_cast<double>(aMaxWidth);
    1211          78 :             if (aPlaceableBound.GetWidth() > aMaxWidth)
    1212             :             {
    1213          10 :                 double fRatio = aPlaceableBound.GetWidth() / fMaxWidth;
    1214             : 
    1215             :                 aPlaceableBound = Rectangle(
    1216          10 :                                     aPlaceableBound.Left()   / fRatio,
    1217          10 :                                     aPlaceableBound.Top()    / fRatio,
    1218          10 :                                     aPlaceableBound.Right()  / fRatio,
    1219          40 :                                     aPlaceableBound.Bottom() / fRatio);
    1220             : 
    1221             :                 SAL_INFO("vcl.wmf", "Placeable bounds "
    1222             :                        " t: " << aPlaceableBound.Left()  << " l: " << aPlaceableBound.Top()
    1223             :                     << " b: " << aPlaceableBound.Right() << " r: " << aPlaceableBound.Bottom());
    1224             :             }
    1225             :         }
    1226             : 
    1227          84 :         pWMF->Seek( nStrmPos );
    1228             :     }
    1229             : 
    1230         196 :     pOut->SetWinOrg( aPlaceableBound.TopLeft() );
    1231         196 :     Size aWMFSize( labs( aPlaceableBound.GetWidth() ), labs( aPlaceableBound.GetHeight() ) );
    1232         196 :     pOut->SetWinExt( aWMFSize );
    1233             : 
    1234             :     SAL_INFO("vcl.wmf", "WMF size  w: " << aWMFSize.Width()    << " h: " << aWMFSize.Height());
    1235             : 
    1236         196 :     Size aDevExt( 10000, 10000 );
    1237         196 :     if( ( labs( aWMFSize.Width() ) > 1 ) && ( labs( aWMFSize.Height() ) > 1 ) )
    1238             :     {
    1239         176 :         const Fraction  aFrac( 1, nUnitsPerInch );
    1240         176 :         MapMode         aWMFMap( MAP_INCH, Point(), aFrac, aFrac );
    1241         176 :         Size            aSize100( OutputDevice::LogicToLogic( aWMFSize, aWMFMap, MAP_100TH_MM ) );
    1242         176 :         aDevExt = Size( labs( aSize100.Width() ), labs( aSize100.Height() ) );
    1243             :     }
    1244         196 :     pOut->SetDevExt( aDevExt );
    1245             : 
    1246             :     SAL_INFO("vcl.wmf", "Dev size  w: " << aDevExt.Width()    << " h: " << aDevExt.Height());
    1247             : 
    1248             :     // read the METAHEADER
    1249         196 :     sal_uInt32 nMetaKey(0);
    1250         196 :     pWMF->ReadUInt32( nMetaKey ); // type and headersize
    1251         196 :     if (!pWMF->good())
    1252          18 :         return false;
    1253         178 :     if (nMetaKey != 0x00090001)
    1254             :     {
    1255           0 :         sal_uInt16 aNextWord(0);
    1256           0 :         pWMF->ReadUInt16( aNextWord );
    1257           0 :         if (nMetaKey != 0x10000 || aNextWord != 0x09)
    1258             :         {
    1259           0 :             pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
    1260           0 :             return false;
    1261             :         }
    1262             :     }
    1263             : 
    1264         178 :     pWMF->SeekRel( 2 ); // Version (of Windows)
    1265         178 :     pWMF->SeekRel( 4 ); // Size (of file in words)
    1266         178 :     pWMF->SeekRel( 2 ); // NoObjects (maximum number of simultaneous objects)
    1267         178 :     pWMF->SeekRel( 4 ); // MaxRecord (size of largets record in words)
    1268         178 :     pWMF->SeekRel( 2 ); // NoParameters (Unused
    1269             : 
    1270         178 :     return pWMF->good();
    1271             : }
    1272             : 
    1273         196 : void WMFReader::ReadWMF()
    1274             : {
    1275             :     sal_uInt16  nFunction;
    1276             :     sal_uLong   nPos, nPercent, nLastPercent;
    1277             : 
    1278         196 :     nSkipActions = 0;
    1279         196 :     nCurrentAction = 0;
    1280         196 :     nUnicodeEscapeAction = 0;
    1281             : 
    1282         196 :     pEMFStream      = NULL;
    1283         196 :     nEMFRecCount    = 0;
    1284         196 :     nEMFRec         = 0;
    1285         196 :     nEMFSize        = 0;
    1286             : 
    1287         196 :     bool bEMFAvailable = false;
    1288             : 
    1289         196 :     pOut->SetMapMode( MM_ANISOTROPIC );
    1290         196 :     pOut->SetWinOrg( Point() );
    1291         196 :     pOut->SetWinExt( Size( 1, 1 ) );
    1292         196 :     pOut->SetDevExt( Size( 10000, 10000 ) );
    1293             : 
    1294         196 :     nEndPos=pWMF->Seek( STREAM_SEEK_TO_END );
    1295         196 :     pWMF->Seek( nStartPos );
    1296         196 :     Callback( (sal_uInt16) ( nLastPercent = 0 ) );
    1297             : 
    1298         196 :     if ( ReadHeader( ) )
    1299             :     {
    1300             : 
    1301         178 :         nPos = pWMF->Tell();
    1302             : 
    1303         178 :         if( nEndPos - nStartPos )
    1304             :         {
    1305             :             while( true )
    1306             :             {
    1307       13144 :                 nCurrentAction++;
    1308       13144 :                 nPercent = ( nPos - nStartPos ) * 100 / ( nEndPos - nStartPos );
    1309             : 
    1310       13144 :                 if( nLastPercent + 4 <= nPercent )
    1311             :                 {
    1312        1800 :                     Callback( (sal_uInt16) nPercent );
    1313        1800 :                     nLastPercent = nPercent;
    1314             :                 }
    1315       13144 :                 pWMF->ReadUInt32( nRecSize ).ReadUInt16( nFunction );
    1316             : 
    1317       26288 :                 if(  pWMF->GetError()
    1318       13142 :                   || ( nRecSize < 3 )
    1319       13140 :                   || (  nRecSize  == 3
    1320         286 :                      && nFunction == 0
    1321             :                      )
    1322       26110 :                   || pWMF->IsEof()
    1323             :                   )
    1324             :                 {
    1325         178 :                     if( pWMF->IsEof() )
    1326           2 :                         pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
    1327             : 
    1328         178 :                     break;
    1329             :                 }
    1330       12966 :                 if ( !bEMFAvailable )
    1331             :                 {
    1332       25752 :                     if(   !aBmpSaveList.empty()
    1333         112 :                       && ( nFunction != W_META_STRETCHDIB    )
    1334         112 :                       && ( nFunction != W_META_DIBBITBLT     )
    1335       12984 :                       && ( nFunction != W_META_DIBSTRETCHBLT )
    1336             :                       )
    1337             :                     {
    1338         104 :                         pOut->ResolveBitmapActions( aBmpSaveList );
    1339             :                     }
    1340             : 
    1341       12876 :                     if ( !nSkipActions )
    1342       12876 :                         ReadRecordParams( nFunction );
    1343             :                     else
    1344           0 :                         nSkipActions--;
    1345             : 
    1346       12876 :                     if( pEMFStream && nEMFRecCount == nEMFRec )
    1347             :                     {
    1348           4 :                         GDIMetaFile aMeta;
    1349           4 :                         pEMFStream->Seek( 0 );
    1350           4 :                         EnhWMFReader* pEMFReader = new EnhWMFReader ( *pEMFStream, aMeta );
    1351           4 :                         bEMFAvailable = pEMFReader->ReadEnhWMF();
    1352           4 :                         delete pEMFReader; // destroy first!!!
    1353             : 
    1354           4 :                         if( bEMFAvailable )
    1355             :                         {
    1356           4 :                             pOut->AddFromGDIMetaFile( aMeta );
    1357           4 :                             pOut->SetrclFrame( Rectangle( Point(0, 0), aMeta.GetPrefSize()));
    1358             : 
    1359             :                             // the stream needs to be set to the wmf end position,
    1360             :                             // otherwise the GfxLink that is created will be incorrect
    1361             :                             // (leading to graphic loss after swapout/swapin).
    1362             :                             // so we will proceed normally, but are ignoring further wmf
    1363             :                             // records
    1364             :                         }
    1365             :                         else
    1366             :                         {
    1367             :                             // something went wrong
    1368             :                             // continue with WMF, don't try this again
    1369           0 :                             delete pEMFStream;
    1370           0 :                             pEMFStream = NULL;
    1371           4 :                         }
    1372             :                     }
    1373             :                 }
    1374       12966 :                 nPos += nRecSize * 2;
    1375       12966 :                 if ( nPos <= nEndPos )
    1376       12966 :                     pWMF->Seek( nPos  );
    1377             :                 else
    1378           0 :                     pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
    1379       12966 :             }
    1380             :         }
    1381             :         else
    1382           0 :             pWMF->SetError( SVSTREAM_GENERALERROR );
    1383             : 
    1384         178 :         if( !pWMF->GetError() && !aBmpSaveList.empty() )
    1385           0 :             pOut->ResolveBitmapActions( aBmpSaveList );
    1386             :     }
    1387         196 :     if ( pWMF->GetError() )
    1388          22 :         pWMF->Seek( nStartPos );
    1389         196 : }
    1390             : 
    1391          78 : bool WMFReader::GetPlaceableBound( Rectangle& rPlaceableBound, SvStream* pStm )
    1392             : {
    1393          78 :     bool bRet = true;
    1394             : 
    1395          78 :     Rectangle aBound;
    1396          78 :     aBound.Left()   = RECT_MAX;
    1397          78 :     aBound.Top()    = RECT_MAX;
    1398          78 :     aBound.Right()  = RECT_MIN;
    1399          78 :     aBound.Bottom() = RECT_MIN;
    1400          78 :     bool bBoundsDetermined = false;
    1401             : 
    1402          78 :     sal_uInt32 nPos = pStm->Tell();
    1403          78 :     sal_uInt32 nEnd = pStm->Seek( STREAM_SEEK_TO_END );
    1404             : 
    1405          78 :     pStm->Seek( nPos );
    1406             : 
    1407          78 :     Point aWinOrg(0,0);
    1408          78 :     boost::optional<Size>  aWinExt;
    1409             : 
    1410          78 :     Point aViewportOrg(0,0);
    1411         156 :     boost::optional<Size>  aViewportExt;
    1412             : 
    1413          78 :     if( nEnd - nPos )
    1414             :     {
    1415          78 :         sal_Int16 nMapMode = MM_ANISOTROPIC;
    1416             :         sal_uInt16 nFunction;
    1417             :         sal_uInt32 nRSize;
    1418             : 
    1419        7156 :         while( bRet )
    1420             :         {
    1421        7064 :             pStm->ReadUInt32( nRSize ).ReadUInt16( nFunction );
    1422             : 
    1423        7064 :             if( pStm->GetError() )
    1424             :             {
    1425           0 :                 bRet = false;
    1426           0 :                 break;
    1427             :             }
    1428        7064 :             else if ( nRSize==3 && nFunction==0 )
    1429             :             {
    1430             :                 break;
    1431             :             }
    1432        7004 :             else if ( nRSize < 3 || pStm->IsEof() )
    1433             :             {
    1434           4 :                 pStm->SetError( SVSTREAM_FILEFORMAT_ERROR );
    1435           4 :                 bRet = false;
    1436           4 :                 break;
    1437             :             }
    1438        7000 :             switch( nFunction )
    1439             :             {
    1440             :                 case W_META_SETWINDOWORG:
    1441             :                 {
    1442         118 :                     aWinOrg = ReadYX();
    1443             :                 }
    1444         118 :                 break;
    1445             : 
    1446             :                 case W_META_SETWINDOWEXT:
    1447             :                 {
    1448         114 :                     sal_Int16 nWidth(0), nHeight(0);
    1449         114 :                     pStm->ReadInt16(nHeight);
    1450         114 :                     pStm->ReadInt16(nWidth);
    1451         114 :                     aWinExt = Size(nWidth, nHeight);
    1452             :                 }
    1453         114 :                 break;
    1454             : 
    1455             :                 case W_META_SETVIEWPORTORG:
    1456             :                 {
    1457           0 :                     aViewportOrg = ReadYX();
    1458             :                 }
    1459           0 :                 break;
    1460             : 
    1461             :                 case W_META_SETVIEWPORTEXT:
    1462             :                 {
    1463           0 :                     sal_Int16 nWidth(0), nHeight(0);
    1464           0 :                     pStm->ReadInt16(nHeight);
    1465           0 :                     pStm->ReadInt16(nWidth);
    1466           0 :                     aViewportExt = Size(nWidth, nHeight);
    1467             :                 }
    1468           0 :                 break;
    1469             : 
    1470             :                 case W_META_SETMAPMODE :
    1471          48 :                     pStm->ReadInt16( nMapMode );
    1472          48 :                 break;
    1473             : 
    1474             :                 case W_META_MOVETO:
    1475             :                 case W_META_LINETO:
    1476           2 :                     GetWinExtMax( ReadYX(), aBound, nMapMode );
    1477           2 :                     bBoundsDetermined = true;
    1478           2 :                 break;
    1479             : 
    1480             :                 case W_META_RECTANGLE:
    1481             :                 case W_META_INTERSECTCLIPRECT:
    1482             :                 case W_META_EXCLUDECLIPRECT :
    1483             :                 case W_META_ELLIPSE:
    1484         146 :                     GetWinExtMax( ReadRectangle(), aBound, nMapMode );
    1485         146 :                     bBoundsDetermined = true;
    1486         146 :                 break;
    1487             : 
    1488             :                 case W_META_ROUNDRECT:
    1489           0 :                     ReadYXExt(); // size
    1490           0 :                     GetWinExtMax( ReadRectangle(), aBound, nMapMode );
    1491           0 :                     bBoundsDetermined = true;
    1492           0 :                 break;
    1493             : 
    1494             :                 case W_META_ARC:
    1495             :                 case W_META_PIE:
    1496             :                 case W_META_CHORD:
    1497           0 :                     ReadYX(); // end
    1498           0 :                     ReadYX(); // start
    1499           0 :                     GetWinExtMax( ReadRectangle(), aBound, nMapMode );
    1500           0 :                     bBoundsDetermined = true;
    1501           0 :                 break;
    1502             : 
    1503             :                 case W_META_POLYGON:
    1504             :                 {
    1505         112 :                     bool bRecordOk = true;
    1506             : 
    1507         112 :                     sal_uInt16 nPoints(0);
    1508         112 :                     pStm->ReadUInt16( nPoints );
    1509             : 
    1510         112 :                     if (nPoints > pStm->remainingSize() / (2 * sizeof(sal_uInt16)))
    1511             :                     {
    1512           2 :                         bRecordOk = false;
    1513             :                     }
    1514             :                     else
    1515             :                     {
    1516        1872 :                         for(sal_uInt16 i = 0; i < nPoints; i++ )
    1517             :                         {
    1518        1762 :                             GetWinExtMax( ReadPoint(), aBound, nMapMode );
    1519        1762 :                             bBoundsDetermined = true;
    1520             :                         }
    1521             :                     }
    1522             : 
    1523             :                     SAL_WARN_IF(!bRecordOk, "vcl.wmf", "polyline record claimed more points than the stream can provide");
    1524             : 
    1525         112 :                     if (!bRecordOk)
    1526             :                     {
    1527           2 :                         pStm->SetError( SVSTREAM_FILEFORMAT_ERROR );
    1528           2 :                         bRet = false;
    1529           2 :                         break;
    1530             :                     }
    1531             :                 }
    1532         110 :                 break;
    1533             : 
    1534             :                 case W_META_POLYPOLYGON:
    1535             :                 {
    1536         318 :                     bool bRecordOk = true;
    1537         318 :                     sal_uInt16 nPoly(0), nPoints(0);
    1538         318 :                     pStm->ReadUInt16(nPoly);
    1539         318 :                     if (nPoly > pStm->remainingSize() / sizeof(sal_uInt16))
    1540             :                     {
    1541           0 :                         bRecordOk = false;
    1542             :                     }
    1543             :                     else
    1544             :                     {
    1545         830 :                         for(sal_uInt16 i = 0; i < nPoly; i++ )
    1546             :                         {
    1547         516 :                             sal_uInt16 nP = 0;
    1548         516 :                             pStm->ReadUInt16( nP );
    1549         516 :                             if (nP > SAL_MAX_UINT16 - nPoints)
    1550             :                             {
    1551           4 :                                 bRecordOk = false;
    1552           4 :                                 break;
    1553             :                             }
    1554         512 :                             nPoints += nP;
    1555             :                         }
    1556             :                     }
    1557             : 
    1558             :                     SAL_WARN_IF(!bRecordOk, "vcl.wmf", "polypolygon record has more polygons than we can handle");
    1559             : 
    1560         318 :                     bRecordOk = bRecordOk && pStm->good();
    1561             : 
    1562         318 :                     if (!bRecordOk)
    1563             :                     {
    1564           4 :                         pStm->SetError( SVSTREAM_FILEFORMAT_ERROR );
    1565           4 :                         bRet = false;
    1566          10 :                         break;
    1567             :                     }
    1568             : 
    1569         314 :                     if (nPoints > pStm->remainingSize() / (2 * sizeof(sal_uInt16)))
    1570             :                     {
    1571           2 :                         bRecordOk = false;
    1572             :                     }
    1573             :                     else
    1574             :                     {
    1575       15520 :                         for (sal_uInt16 i = 0; i < nPoints; i++ )
    1576             :                         {
    1577       15208 :                             GetWinExtMax( ReadPoint(), aBound, nMapMode );
    1578       15208 :                             bBoundsDetermined = true;
    1579             :                         }
    1580             :                     }
    1581             : 
    1582             :                     SAL_WARN_IF(!bRecordOk, "vcl.wmf", "polypolygon record claimed more points than the stream can provide");
    1583             : 
    1584         314 :                     bRecordOk &= pStm->good();
    1585             : 
    1586         314 :                     if (!bRecordOk)
    1587             :                     {
    1588           2 :                         pStm->SetError( SVSTREAM_FILEFORMAT_ERROR );
    1589           2 :                         bRet = false;
    1590           2 :                         break;
    1591             :                     }
    1592             :                 }
    1593         312 :                 break;
    1594             : 
    1595             :                 case W_META_POLYLINE:
    1596             :                 {
    1597         200 :                     bool bRecordOk = true;
    1598             : 
    1599         200 :                     sal_uInt16 nPoints(0);
    1600         200 :                     pStm->ReadUInt16(nPoints);
    1601         200 :                     if (nPoints > pStm->remainingSize() / (2 * sizeof(sal_uInt16)))
    1602             :                     {
    1603           0 :                         bRecordOk = false;
    1604             :                     }
    1605             :                     else
    1606             :                     {
    1607         620 :                         for (sal_uInt16 i = 0; i < nPoints; ++i)
    1608             :                         {
    1609         420 :                             GetWinExtMax( ReadPoint(), aBound, nMapMode );
    1610         420 :                             bBoundsDetermined = true;
    1611             :                         }
    1612             :                     }
    1613             : 
    1614             :                     SAL_WARN_IF(!bRecordOk, "vcl.wmf", "polyline record claimed more points than the stream can provide");
    1615             : 
    1616         200 :                     if (!bRecordOk)
    1617             :                     {
    1618           0 :                         pStm->SetError( SVSTREAM_FILEFORMAT_ERROR );
    1619           0 :                         bRet = false;
    1620           0 :                         break;
    1621             :                     }
    1622             :                 }
    1623         200 :                 break;
    1624             : 
    1625             :                 case W_META_SETPIXEL:
    1626             :                 {
    1627           0 :                     ReadColor();
    1628           0 :                     GetWinExtMax( ReadYX(), aBound, nMapMode );
    1629           0 :                     bBoundsDetermined = true;
    1630             :                 }
    1631           0 :                 break;
    1632             : 
    1633             :                 case W_META_TEXTOUT:
    1634             :                 {
    1635             :                     sal_uInt16 nLength;
    1636           0 :                     pStm->ReadUInt16( nLength );
    1637             :                     // todo: we also have to take care of the text width
    1638           0 :                     if ( nLength )
    1639             :                     {
    1640           0 :                         pStm->SeekRel( ( nLength + 1 ) &~ 1 );
    1641           0 :                         GetWinExtMax( ReadYX(), aBound, nMapMode );
    1642           0 :                         bBoundsDetermined = true;
    1643             :                     }
    1644             :                 }
    1645           0 :                 break;
    1646             : 
    1647             :                 case W_META_EXTTEXTOUT:
    1648             :                 {
    1649             :                     sal_uInt16  nLen, nOptions;
    1650         120 :                     Point       aPosition;
    1651             : 
    1652         120 :                     aPosition = ReadYX();
    1653         120 :                     pStm->ReadUInt16( nLen ).ReadUInt16( nOptions );
    1654             :                     // todo: we also have to take care of the text width
    1655         120 :                     if( nLen )
    1656             :                     {
    1657         120 :                         GetWinExtMax( aPosition, aBound, nMapMode );
    1658         120 :                         bBoundsDetermined = true;
    1659             :                     }
    1660             :                 }
    1661         120 :                 break;
    1662             :                 case W_META_BITBLT:
    1663             :                 case W_META_STRETCHBLT:
    1664             :                 case W_META_DIBBITBLT:
    1665             :                 case W_META_DIBSTRETCHBLT:
    1666             :                 case W_META_STRETCHDIB:
    1667             :                 {
    1668             :                     sal_Int32   nWinROP;
    1669             :                     sal_uInt16  nSx, nSy, nSxe, nSye, nUsage;
    1670          38 :                     pStm->ReadInt32( nWinROP );
    1671             : 
    1672          38 :                     if( nFunction == W_META_STRETCHDIB )
    1673           2 :                         pStm->ReadUInt16( nUsage );
    1674             : 
    1675             :                     // nSye and nSxe is the number of pixels that has to been used
    1676          38 :                     if( nFunction == W_META_STRETCHDIB || nFunction == W_META_STRETCHBLT || nFunction == W_META_DIBSTRETCHBLT )
    1677           4 :                         pStm->ReadUInt16( nSye ).ReadUInt16( nSxe );
    1678             :                     else
    1679          34 :                         nSye = nSxe = 0;    // set this to zero as indicator not to scale the bitmap later
    1680             : 
    1681             :                     // nSy and nx is the offset of the first pixel
    1682          38 :                     pStm->ReadUInt16( nSy ).ReadUInt16( nSx );
    1683             : 
    1684          38 :                     if( nFunction == W_META_STRETCHDIB || nFunction == W_META_DIBBITBLT || nFunction == W_META_DIBSTRETCHBLT )
    1685             :                     {
    1686          38 :                         if ( nWinROP == PATCOPY )
    1687           2 :                             pStm->ReadUInt16( nUsage );    // i don't know anything of this parameter, so its called nUsage
    1688             :                                                 // pOut->DrawRect( Rectangle( ReadYX(), aDestSize ), false );
    1689             : 
    1690          38 :                         Size aDestSize( ReadYXExt() );
    1691          38 :                         if ( aDestSize.Width() && aDestSize.Height() )  // #92623# do not try to read buggy bitmaps
    1692             :                         {
    1693          36 :                             Rectangle aDestRect( ReadYX(), aDestSize );
    1694          36 :                             GetWinExtMax( aDestRect, aBound, nMapMode );
    1695          36 :                             bBoundsDetermined = true;
    1696             :                         }
    1697             :                     }
    1698             :                 }
    1699          38 :                 break;
    1700             : 
    1701             :                 case W_META_PATBLT:
    1702             :                 {
    1703             :                     sal_uInt32 nROP;
    1704          56 :                     pStm->ReadUInt32( nROP );
    1705          56 :                     Size aSize = ReadYXExt();
    1706          56 :                     GetWinExtMax( Rectangle( ReadYX(), aSize ), aBound, nMapMode );
    1707          56 :                     bBoundsDetermined = true;
    1708             :                 }
    1709          56 :                 break;
    1710             :             }
    1711        7000 :             nPos += nRSize * 2;
    1712        7000 :              if ( nPos <= nEnd )
    1713        6992 :                  pStm->Seek( nPos );
    1714             :              else
    1715             :              {
    1716           8 :                  pStm->SetError( SVSTREAM_FILEFORMAT_ERROR );
    1717           8 :                  bRet = false;
    1718             :              }
    1719             :         }
    1720             :     }
    1721             :     else
    1722             :     {
    1723           0 :         pStm->SetError( SVSTREAM_GENERALERROR );
    1724           0 :         bRet = false;
    1725             :     }
    1726             : 
    1727          78 :     if (bRet)
    1728             :     {
    1729          60 :         if (aWinExt)
    1730             :         {
    1731          58 :             rPlaceableBound = Rectangle(aWinOrg, *aWinExt);
    1732             :             SAL_INFO("vcl.wmf", "Window dimension "
    1733             :                        " t: " << rPlaceableBound.Left()  << " l: " << rPlaceableBound.Top()
    1734             :                     << " b: " << rPlaceableBound.Right() << " r: " << rPlaceableBound.Bottom());
    1735             :         }
    1736           2 :         else if (aViewportExt)
    1737             :         {
    1738           0 :             rPlaceableBound = Rectangle(aViewportOrg, *aViewportExt);
    1739             :             SAL_INFO("vcl.wmf", "Viewport dimension "
    1740             :                        " t: " << rPlaceableBound.Left()  << " l: " << rPlaceableBound.Top()
    1741             :                     << " b: " << rPlaceableBound.Right() << " r: " << rPlaceableBound.Bottom());
    1742             :         }
    1743           2 :         else if (bBoundsDetermined)
    1744             :         {
    1745           0 :             rPlaceableBound = aBound;
    1746             :             SAL_INFO("vcl.wmf", "Determined dimension "
    1747             :                        " t: " << rPlaceableBound.Left()  << " l: " << rPlaceableBound.Top()
    1748             :                     << " b: " << rPlaceableBound.Right() << " r: " << rPlaceableBound.Bottom());
    1749             :         }
    1750             :         else
    1751             :         {
    1752           2 :             rPlaceableBound.Left() = 0;
    1753           2 :             rPlaceableBound.Top() = 0;
    1754           2 :             rPlaceableBound.Right() = aMaxWidth;
    1755           2 :             rPlaceableBound.Bottom() = aMaxWidth;
    1756             :             SAL_INFO("vcl.wmf", "Default dimension "
    1757             :                        " t: " << rPlaceableBound.Left()  << " l: " << rPlaceableBound.Top()
    1758             :                     << " b: " << rPlaceableBound.Right() << " r: " << rPlaceableBound.Bottom());
    1759             :         }
    1760             :     }
    1761             : 
    1762         156 :     return bRet;
    1763             : }
    1764             : 
    1765         196 : WMFReader::WMFReader(SvStream& rStreamWMF, GDIMetaFile& rGDIMetaFile,
    1766             :                      FilterConfigItem* pConfigItem, WMF_EXTERNALHEADER* pExtHeader)
    1767         196 :     : WinMtf(new WinMtfOutput(rGDIMetaFile) , rStreamWMF, pConfigItem)
    1768             :     , nUnitsPerInch(96)
    1769             :     , nRecSize(0)
    1770             :     , pEMFStream(NULL)
    1771             :     , nEMFRecCount(0)
    1772             :     , nEMFRec(0)
    1773             :     , nEMFSize(0)
    1774             :     , nSkipActions(0)
    1775             :     , nCurrentAction(0)
    1776             :     , nUnicodeEscapeAction(0)
    1777         392 :     , pExternalHeader(pExtHeader)
    1778         196 : {}
    1779             : 
    1780         392 : WMFReader::~WMFReader()
    1781             : {
    1782         196 :     delete pEMFStream;
    1783        1429 : }
    1784             : 
    1785             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10