LCOV - code coverage report
Current view: top level - filter/source/flash - swfwriter1.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 3 884 0.3 %
Date: 2015-06-13 12:38:46 Functions: 2 37 5.4 %
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 <com/sun/star/i18n/BreakIterator.hpp>
      21             : #include <com/sun/star/i18n/ScriptType.hpp>
      22             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      23             : #include <comphelper/processfactory.hxx>
      24             : #include "swfwriter.hxx"
      25             : #include <vcl/metaact.hxx>
      26             : #include <vcl/gdimtf.hxx>
      27             : #include <vcl/bmpacc.hxx>
      28             : #include <vcl/virdev.hxx>
      29             : #include <vcl/metric.hxx>
      30             : #include <basegfx/matrix/b2dhommatrixtools.hxx>
      31             : #include <vcl/graphicfilter.hxx>
      32             : #include <vcl/graphictools.hxx>
      33             : 
      34             : #include <zlib.h>
      35             : 
      36             : #include <vcl/salbtype.hxx>
      37             : #include <basegfx/polygon/b2dpolygon.hxx>
      38             : #include <basegfx/polygon/b2dpolypolygon.hxx>
      39             : #include <boost/scoped_array.hpp>
      40             : 
      41             : using namespace ::swf;
      42             : using namespace ::std;
      43             : using namespace ::com::sun::star::i18n;
      44             : using namespace ::com::sun::star::uno;
      45             : using namespace ::com::sun::star::lang;
      46             : using namespace ::com::sun::star::io;
      47             : using namespace ::com::sun::star::beans;
      48             : 
      49           1 : static MapMode aTWIPSMode( MAP_TWIP );
      50           1 : static MapMode a100thmmMode( MAP_100TH_MM );
      51             : 
      52             : 
      53             : 
      54           0 : Point Writer::map( const Point& rPoint ) const
      55             : {
      56           0 :     const MapMode& aSourceMapMode = mpVDev->GetMapMode();
      57             : 
      58           0 :     Point retPoint = mpVDev->LogicToLogic( rPoint,  &aSourceMapMode, &aTWIPSMode );
      59             : 
      60             :     // AS: Produces a 'possible loss of data' warning that we can't fix without
      61             :     //  hurting code readability.
      62           0 :     retPoint.X() = (long)( retPoint.X() * mnDocXScale );
      63           0 :     retPoint.Y() = (long)( retPoint.Y() * mnDocYScale );
      64             : 
      65           0 :     return retPoint;
      66             : }
      67             : 
      68             : 
      69             : 
      70           0 : Size Writer::map( const Size& rSize ) const
      71             : {
      72           0 :     const MapMode& aSourceMapMode = mpVDev->GetMapMode();
      73             : 
      74           0 :     Size retSize = mpVDev->LogicToLogic( rSize,  &aSourceMapMode, &aTWIPSMode );
      75             : 
      76             :     // AS: Produces a 'possible loss of data' warning that we can't fix without
      77             :     //  hurting code readability.
      78           0 :     retSize.Width() = (long)( retSize.Width() * mnDocXScale );
      79           0 :     retSize.Height() = (long)( retSize.Height() * mnDocYScale );
      80             : 
      81           0 :     return retSize;
      82             : }
      83             : 
      84             : 
      85             : 
      86           0 : void Writer::map( tools::PolyPolygon& rPolyPolygon ) const
      87             : {
      88           0 :     const sal_uInt16 nPolyCount = rPolyPolygon.Count();
      89           0 :     if( nPolyCount )
      90             :     {
      91             :         sal_uInt16 nPoly, nPoint, nPointCount;
      92           0 :         for( nPoly = 0; nPoly < nPolyCount; nPoly++ )
      93             :         {
      94           0 :             Polygon& rPoly = rPolyPolygon[nPoly];
      95           0 :             nPointCount = rPoly.GetSize();
      96             : 
      97           0 :             for( nPoint = 0; nPoint < nPointCount; nPoint++ )
      98             :             {
      99           0 :                 rPoly[nPoint] = map( rPoly[nPoint] );
     100             :             }
     101             :         }
     102             :     }
     103           0 : }
     104             : 
     105             : 
     106             : 
     107           0 : sal_Int32 Writer::mapRelative( sal_Int32 n100thMM ) const
     108             : {
     109           0 :     MapMode aSourceMapMode( mpVDev->GetMapMode() );
     110           0 :     aSourceMapMode.SetOrigin( Point() );
     111             : 
     112           0 :     sal_Int32 nTwips = mpVDev->LogicToLogic( Point( n100thMM, n100thMM ),  &aSourceMapMode, &aTWIPSMode ).X();
     113           0 :     return nTwips;
     114             : }
     115             : 
     116             : 
     117             : 
     118           0 : void Writer::Impl_addPolygon( BitStream& rBits, const Polygon& rPoly, bool bFilled )
     119             : {
     120           0 :     Point aLastPoint( rPoly[0] );
     121             : 
     122           0 :     Impl_addShapeRecordChange( rBits, _Int16(aLastPoint.X()),_Int16(aLastPoint.Y()), bFilled );
     123             : 
     124           0 :     sal_uInt16 i = 0, nSize = rPoly.GetSize();
     125             : 
     126           0 :     double d = 16.0f;
     127             : 
     128             :     // points
     129           0 :     while( ( i + 1 ) < nSize )
     130             :     {
     131           0 :         if( ( i + 3 ) < nSize )
     132             :         {
     133           0 :             PolyFlags P1( rPoly.GetFlags( i ) );
     134           0 :             PolyFlags P4( rPoly.GetFlags( i + 3 ) );
     135             : 
     136           0 :             if( ( POLY_NORMAL == P1 || POLY_SMOOTH == P1 || POLY_SYMMTR == P1 ) &&
     137           0 :                 ( POLY_CONTROL == rPoly.GetFlags( i + 1 ) ) &&
     138           0 :                 ( POLY_CONTROL == rPoly.GetFlags( i + 2 ) ) &&
     139           0 :                 ( POLY_NORMAL == P4 || POLY_SMOOTH == P4 || POLY_SYMMTR == P4 ) )
     140             :             {
     141             :                 Impl_quadBezierApprox( rBits, aLastPoint, d*d,
     142           0 :                                       rPoly.GetPoint( i ).X(),   rPoly.GetPoint( i ).Y(),
     143           0 :                                       rPoly.GetPoint( i+1 ).X(), rPoly.GetPoint( i+1 ).Y(),
     144           0 :                                       rPoly.GetPoint( i+2 ).X(), rPoly.GetPoint( i+2 ).Y(),
     145           0 :                                       rPoly.GetPoint( i+3 ).X(), rPoly.GetPoint( i+3 ).Y() );
     146           0 :                 i += 3;
     147           0 :                 continue;
     148             :             }
     149             :         }
     150             : 
     151           0 :         ++i;
     152             : 
     153           0 :         const Point aPolyPoint( rPoly[ i ] );
     154           0 :         if( aPolyPoint != aLastPoint )
     155             :         {
     156           0 :             Impl_addStraightEdgeRecord( rBits, _Int16(aPolyPoint.X() - aLastPoint.X()),_Int16(aPolyPoint.Y() - aLastPoint.Y()));
     157           0 :             aLastPoint = aPolyPoint;
     158             :         }
     159             :     }
     160             : 
     161           0 :     if( bFilled && (rPoly[0] != rPoly[nSize-1]))
     162             :     {
     163           0 :         const Point aPolyPoint( rPoly[ 0 ] );
     164           0 :         if( aPolyPoint != aLastPoint )
     165             :         {
     166           0 :             Impl_addStraightEdgeRecord( rBits, _Int16(aPolyPoint.X() - aLastPoint.X()),_Int16(aPolyPoint.Y() - aLastPoint.Y()));
     167             :         }
     168             :     }
     169           0 : }
     170             : 
     171             : 
     172             : 
     173             : /** Exports a style change record with a move to (x,y) and depending on bFilled a line style 1 or fill style 1
     174             : */
     175           0 : void Writer::Impl_addShapeRecordChange( BitStream& rBits, sal_Int16 dx, sal_Int16 dy, bool bFilled )
     176             : {
     177           0 :     rBits.writeUB( 0, 1 );          // TypeFlag
     178           0 :     rBits.writeUB( 0, 1 );          // StateNewStyles
     179           0 :     rBits.writeUB( sal_uInt32(!bFilled), 1 ); // StateLineStyle
     180           0 :     rBits.writeUB( 0, 1 );          // StateFillStyle0
     181           0 :     rBits.writeUB( bFilled ? 1 : 0, 1 );        // StateFillStyle1
     182           0 :     rBits.writeUB( 1, 1 );          // StateMoveTo
     183             : 
     184           0 :     sal_uInt16 nMoveBits = max( getMaxBitsSigned( dx ), getMaxBitsSigned( dy ) );
     185             : 
     186           0 :     rBits.writeUB( nMoveBits, 5 );  // Number of bits per value
     187             :                                     // TODO: Optimize horizontal and vertical lines
     188           0 :     rBits.writeSB( dx, nMoveBits ); // DeltaX
     189           0 :     rBits.writeSB( dy, nMoveBits ); // DeltaY
     190             : 
     191           0 :     rBits.writeUB( 1, 1 );          // set FillStyle1 or LineStyle to 1
     192           0 : }
     193             : 
     194             : 
     195             : 
     196             : /** Exports a straight edge record
     197             : */
     198           0 : void Writer::Impl_addStraightEdgeRecord( BitStream& rBits, sal_Int16 dx, sal_Int16 dy )
     199             : {
     200           0 :     rBits.writeUB( 1, 1 );          // TypeFlag
     201           0 :     rBits.writeUB( 1, 1 );          // StraightFlag
     202             : 
     203           0 :     sal_uInt16 nBits = max( getMaxBitsSigned( dx ), getMaxBitsSigned( dy ) );
     204             : 
     205           0 :     rBits.writeUB( nBits - 2, 4 );  // Number of bits per value
     206             : 
     207           0 :     if( (dx != 0) && (dy != 0) )
     208             :     {
     209           0 :         rBits.writeUB( 1, 1 );          // GeneralLineFlag
     210           0 :         rBits.writeSB( dx, nBits );     // DeltaX
     211           0 :         rBits.writeSB( dy, nBits );     // DeltaY
     212             :     }
     213             :     else
     214             :     {
     215           0 :         rBits.writeUB( 0, 1 );
     216           0 :         rBits.writeUB( sal_uInt32( dx == 0 ), 1 );
     217           0 :         if( dx == 0 )
     218             :         {
     219           0 :             rBits.writeSB( dy, nBits );     // DeltaY
     220             :         }
     221             :         else
     222             :         {
     223           0 :             rBits.writeSB( dx, nBits );     // DeltaX
     224             :         }
     225             :     }
     226           0 : }
     227             : 
     228             : 
     229             : 
     230             : /** Exports a curved edge record
     231             : */
     232           0 : void Writer::Impl_addCurvedEdgeRecord( BitStream& rBits, sal_Int16 control_dx, sal_Int16 control_dy, sal_Int16 anchor_dx, sal_Int16 anchor_dy )
     233             : {
     234           0 :     rBits.writeUB( 1, 1 );          // TypeFlag
     235           0 :     rBits.writeUB( 0, 1 );          // CurvedFlag
     236             : 
     237             :     sal_uInt8 nBits = static_cast<sal_uInt8>(
     238           0 :         max( getMaxBitsSigned( control_dx ),
     239           0 :             max( getMaxBitsSigned( control_dy ),
     240           0 :                 max( getMaxBitsSigned( anchor_dx ),
     241           0 :                     max( getMaxBitsSigned( anchor_dy ), (sal_uInt16)3 ) ) ) ) );
     242             : 
     243           0 :     rBits.writeUB( nBits - 2, 4 );  // Number of bits per value
     244             : 
     245           0 :     rBits.writeSB( control_dx, nBits );     // DeltaX
     246           0 :     rBits.writeSB( control_dy, nBits );     // DeltaY
     247           0 :     rBits.writeSB( anchor_dx, nBits );      // DeltaX
     248           0 :     rBits.writeSB( anchor_dy, nBits );      // DeltaY
     249           0 : }
     250             : 
     251             : 
     252             : 
     253             : /** Exports a end shape record
     254             : */
     255           0 : void Writer::Impl_addEndShapeRecord( BitStream& rBits )
     256             : {
     257           0 :     rBits.writeUB( 0, 6 );
     258           0 : }
     259             : 
     260             : 
     261             : 
     262           0 : void Writer::Impl_writePolygon( const Polygon& rPoly, bool bFilled )
     263             : {
     264           0 :     tools::PolyPolygon aPolyPoly( rPoly );
     265           0 :     Impl_writePolyPolygon( aPolyPoly, bFilled );
     266           0 : }
     267             : 
     268             : 
     269             : 
     270           0 : void Writer::Impl_writePolygon( const Polygon& rPoly, bool bFilled, const Color& rFillColor, const Color& rLineColor )
     271             : {
     272           0 :     tools::PolyPolygon aPolyPoly( rPoly );
     273           0 :     Impl_writePolyPolygon( aPolyPoly, bFilled, rFillColor, rLineColor );
     274           0 : }
     275             : 
     276             : 
     277             : 
     278           0 : void Writer::Impl_writePolyPolygon( const tools::PolyPolygon& rPolyPoly, bool bFilled, sal_uInt8 nTransparence /* = 0 */ )
     279             : {
     280           0 :     Color aLineColor( mpVDev->GetLineColor() );
     281           0 :     if( 0 == aLineColor.GetTransparency() )
     282           0 :         aLineColor.SetTransparency( nTransparence );
     283           0 :     Color aFillColor( mpVDev->GetFillColor() );
     284           0 :     if( 0 == aFillColor.GetTransparency() )
     285           0 :         aFillColor.SetTransparency( nTransparence );
     286           0 :     Impl_writePolyPolygon(rPolyPoly, bFilled, aFillColor, aLineColor );
     287           0 : }
     288             : 
     289             : 
     290             : 
     291           0 : void Writer::Impl_writePolyPolygon( const tools::PolyPolygon& rPolyPoly, bool bFilled, const Color& rFillColor, const Color& rLineColor )
     292             : {
     293           0 :     tools::PolyPolygon aPolyPoly( rPolyPoly );
     294             : 
     295           0 :     if( aPolyPoly.Count() )
     296             :     {
     297           0 :         map( aPolyPoly );
     298             : 
     299           0 :         if( mpClipPolyPolygon )
     300           0 :             rPolyPoly.GetIntersection( *mpClipPolyPolygon, aPolyPoly );
     301             : 
     302             :         sal_uInt16 nID;
     303           0 :         if( bFilled )
     304             :         {
     305           0 :             Color aFillColor( rFillColor );
     306           0 :             if( 0 != mnGlobalTransparency )
     307           0 :                 aFillColor.SetTransparency( mnGlobalTransparency );
     308             : 
     309           0 :             FillStyle aStyle( aFillColor );
     310           0 :             nID = defineShape( aPolyPoly, aStyle );
     311             :         }
     312             :         else
     313             :         {
     314           0 :             Color aLineColor( rLineColor );
     315           0 :             if( 0 != mnGlobalTransparency )
     316           0 :                 aLineColor.SetTransparency( mnGlobalTransparency );
     317             : 
     318           0 :             nID = defineShape( aPolyPoly, 1, aLineColor );
     319             :         }
     320           0 :         maShapeIds.push_back( nID );
     321           0 :     }
     322           0 : }
     323             : 
     324             : 
     325             : 
     326             : /** A gradient is a transition from one color to another, rendered inside a given polypolygon */
     327           0 : void Writer::Impl_writeGradientEx( const tools::PolyPolygon& rPolyPoly, const Gradient& rGradient )
     328             : {
     329           0 :     if( rPolyPoly.Count() )
     330             :     {
     331           0 :         tools::PolyPolygon aPolyPolygon( rPolyPoly );
     332           0 :         map( aPolyPolygon );
     333             : 
     334           0 :         if( (rGradient.GetStyle() == GradientStyle_LINEAR && rGradient.GetAngle() == 900) || (rGradient.GetStyle() == GradientStyle_RADIAL)  )
     335             :         {
     336           0 :             const Rectangle aBoundRect( aPolyPolygon.GetBoundRect() );
     337             : 
     338           0 :             FillStyle aFillStyle( aBoundRect, rGradient );
     339             : 
     340           0 :             sal_uInt16 nShapeId = defineShape( aPolyPolygon, aFillStyle );
     341           0 :             maShapeIds.push_back( nShapeId );
     342             :         }
     343             :         else
     344             :         {
     345           0 :             setClipping( &aPolyPolygon );
     346             : 
     347             :             // render the gradient filling to simple polygons
     348             :             {
     349           0 :                 GDIMetaFile aTmpMtf;
     350           0 :                 mpVDev->AddGradientActions( aPolyPolygon.GetBoundRect(), rGradient, aTmpMtf );
     351           0 :                 Impl_writeActions( aTmpMtf );
     352             :             }
     353             : 
     354           0 :             setClipping( NULL );
     355           0 :         }
     356             :     }
     357           0 : }
     358             : 
     359             : 
     360             : 
     361           0 : void Writer::setClipping( const tools::PolyPolygon* pClipPolyPolygon )
     362             : {
     363           0 :     mpClipPolyPolygon = pClipPolyPolygon;
     364           0 : }
     365             : 
     366             : 
     367             : 
     368             : // AS: Just comparing fonts straight up is too literal.  There are some
     369             : //  differences in font that actually require different glyphs to be defined,
     370             : //  and some that don't.  This function is meant to capture all the differences
     371             : //  that we care about.
     372           0 : bool compare_fonts_for_me(const vcl::Font& rFont1, const vcl::Font& rFont2)
     373             : {
     374           0 :     return rFont1.GetName() == rFont2.GetName() &&
     375           0 :             rFont1.GetWeight() == rFont2.GetWeight() &&
     376           0 :             rFont1.GetItalic() == rFont2.GetItalic() &&
     377           0 :             rFont1.IsOutline() == rFont2.IsOutline() &&
     378           0 :             rFont1.IsShadow() == rFont2.IsShadow() &&
     379           0 :             rFont1.GetRelief() == rFont2.GetRelief();
     380             : }
     381             : 
     382             : 
     383             : 
     384           0 : FlashFont& Writer::Impl_getFont( const vcl::Font& rFont )
     385             : {
     386           0 :     FontMap::iterator aIter( maFonts.begin() );
     387           0 :     const FontMap::iterator aEnd( maFonts.end() );
     388             : 
     389           0 :     for(; aIter != aEnd; ++aIter)
     390             :     {
     391           0 :         const vcl::Font tempFont = (*aIter)->getFont();
     392           0 :         if( compare_fonts_for_me(tempFont, rFont) )
     393             :         {
     394           0 :             return **aIter;
     395             :         }
     396           0 :     }
     397             : 
     398           0 :     FlashFont* pFont = new FlashFont( rFont, createID() );
     399           0 :     maFonts.push_back( pFont );
     400           0 :     return *pFont;
     401             : }
     402             : 
     403             : 
     404             : 
     405           0 : void Writer::Impl_writeText( const Point& rPos, const OUString& rText, const long* pDXArray, long nWidth )
     406             : {
     407           0 :     const FontMetric aMetric( mpVDev->GetFontMetric() );
     408             : 
     409           0 :     bool bTextSpecial = aMetric.IsShadow() || aMetric.IsOutline() || (aMetric.GetRelief() != RELIEF_NONE);
     410             : 
     411           0 :     if( !bTextSpecial )
     412             :     {
     413           0 :         Impl_writeText( rPos, rText, pDXArray, nWidth, mpVDev->GetTextColor() );
     414             :     }
     415             :     else
     416             :     {
     417           0 :         if( aMetric.GetRelief() != RELIEF_NONE )
     418             :         {
     419           0 :             Color aReliefColor( COL_LIGHTGRAY );
     420           0 :             Color aTextColor( mpVDev->GetTextColor() );
     421             : 
     422           0 :             if ( aTextColor.GetColor() == COL_BLACK )
     423           0 :                 aTextColor = Color( COL_WHITE );
     424             : 
     425           0 :             if ( aTextColor.GetColor() == COL_WHITE )
     426           0 :                 aReliefColor = Color( COL_BLACK );
     427             : 
     428             : 
     429           0 :             Point aPos( rPos );
     430           0 :             Point aOffset( 6,6 );
     431             : 
     432           0 :             if ( aMetric.GetRelief() == RELIEF_ENGRAVED )
     433             :             {
     434           0 :                 aPos -= aOffset;
     435             :             }
     436             :             else
     437             :             {
     438           0 :                 aPos += aOffset;
     439             :             }
     440             : 
     441           0 :             Impl_writeText( aPos, rText, pDXArray, nWidth, aReliefColor );
     442           0 :             Impl_writeText( rPos, rText, pDXArray, nWidth, aTextColor );
     443             :         }
     444             :         else
     445             :         {
     446           0 :             if( aMetric.IsShadow() )
     447             :             {
     448           0 :                 long nOff = 1 + ((aMetric.GetLineHeight()-24)/24);
     449           0 :                 if ( aMetric.IsOutline() )
     450           0 :                     nOff += 6;
     451             : 
     452           0 :                 Color aTextColor( mpVDev->GetTextColor() );
     453           0 :                 Color aShadowColor = Color( COL_BLACK );
     454             : 
     455           0 :                 if ( (aTextColor.GetColor() == COL_BLACK) || (aTextColor.GetLuminance() < 8) )
     456           0 :                     aShadowColor = Color( COL_LIGHTGRAY );
     457             : 
     458           0 :                 Point aPos( rPos );
     459           0 :                 aPos += Point( nOff, nOff );
     460           0 :                 Impl_writeText( aPos, rText, pDXArray, nWidth, aShadowColor );
     461             : 
     462           0 :                 if( !aMetric.IsOutline() )
     463             :                 {
     464           0 :                     Impl_writeText( rPos, rText, pDXArray, nWidth, aTextColor );
     465             :                 }
     466             :             }
     467             : 
     468           0 :             if( aMetric.IsOutline() )
     469             :             {
     470           0 :                 Point aPos = rPos + Point( -6, -6 );
     471           0 :                 Impl_writeText( aPos, rText, pDXArray, nWidth, mpVDev->GetTextColor() );
     472           0 :                 aPos = rPos + Point(+6,+6);
     473           0 :                 Impl_writeText( aPos, rText, pDXArray, nWidth, mpVDev->GetTextColor() );
     474           0 :                 aPos = rPos + Point(-6,+0);
     475           0 :                 Impl_writeText( aPos, rText, pDXArray, nWidth, mpVDev->GetTextColor() );
     476           0 :                 aPos = rPos + Point(-6,+6);
     477           0 :                 Impl_writeText( aPos, rText, pDXArray, nWidth, mpVDev->GetTextColor() );
     478           0 :                 aPos = rPos + Point(+0,+6);
     479           0 :                 Impl_writeText( aPos, rText, pDXArray, nWidth, mpVDev->GetTextColor() );
     480           0 :                 aPos = rPos + Point(+0,-6);
     481           0 :                 Impl_writeText( aPos, rText, pDXArray, nWidth, mpVDev->GetTextColor() );
     482           0 :                 aPos = rPos + Point(+6,-1);
     483           0 :                 Impl_writeText( aPos, rText, pDXArray, nWidth, mpVDev->GetTextColor() );
     484           0 :                 aPos = rPos + Point(+6,+0);
     485           0 :                 Impl_writeText( aPos, rText, pDXArray, nWidth, mpVDev->GetTextColor() );
     486             : 
     487           0 :                 Impl_writeText( rPos, rText, pDXArray, nWidth, Color( COL_WHITE ) );
     488             :             }
     489             :         }
     490           0 :     }
     491           0 : }
     492             : 
     493           0 : void Writer::Impl_writeText( const Point& rPos, const OUString& rText, const long* pDXArray, long nWidth, Color aTextColor )
     494             : {
     495           0 :     sal_Int32 nLen = rText.getLength();
     496             : 
     497           0 :     if( !nLen )
     498           0 :         return;
     499             : 
     500           0 :     const bool bRTL = bool(mpVDev->GetLayoutMode() & TEXT_LAYOUT_BIDI_RTL);
     501             : 
     502           0 :     sal_Int16 nScriptType = ScriptType::LATIN;
     503           0 :     Reference < XBreakIterator > xBI( Impl_GetBreakIterator() );
     504           0 :     if( xBI.is() )
     505             :     {
     506           0 :         const OUString oText( rText );
     507           0 :         nScriptType = xBI->getScriptType( oText, 0 );
     508             :     }
     509             : 
     510             :     // if the text is either right to left or complex or asian, we
     511             :     // ask the output device for a polygon representation.
     512             :     // On complex and asian text, each unicode character can have
     513             :     // different glyph representation, based on context. Also positioning
     514             :     // is not trivial so we let the output device do it for us.
     515           0 :     if( bRTL || (nScriptType != ScriptType::LATIN) )
     516             :     {
     517             :         // todo: optimize me as this will generate a huge amount of duplicate polygons
     518           0 :         tools::PolyPolygon aPolyPoygon;
     519           0 :         mpVDev->GetTextOutline( aPolyPoygon, rText, 0, 0, (sal_uInt16)nLen, true, nWidth, pDXArray );
     520           0 :         aPolyPoygon.Translate( rPos );
     521           0 :         Impl_writePolyPolygon( aPolyPoygon, true, aTextColor, aTextColor );
     522             :     }
     523             :     else
     524             :     {
     525           0 :         Size    aNormSize;
     526           0 :         boost::scoped_array<long> pOwnArray;
     527             :         long* pDX;
     528             : 
     529             :         // get text sizes
     530           0 :         if( pDXArray )
     531             :         {
     532           0 :             aNormSize = Size( mpVDev->GetTextWidth( rText ), 0 );
     533           0 :             pDX = const_cast<long*>(pDXArray);
     534             :         }
     535             :         else
     536             :         {
     537           0 :             pOwnArray.reset(new long[ nLen ]);
     538           0 :             aNormSize = Size( mpVDev->GetTextArray( rText, pOwnArray.get() ), 0 );
     539           0 :             pDX = pOwnArray.get();
     540             :         }
     541             : 
     542           0 :         if( nLen > 1 )
     543             :         {
     544           0 :             aNormSize.Width() = pDX[ nLen - 2 ] + mpVDev->GetTextWidth( OUString(rText[nLen - 1]) );
     545             : 
     546           0 :             if( nWidth && aNormSize.Width() && ( nWidth != aNormSize.Width() ) )
     547             :             {
     548           0 :                 const double fFactor = (double) nWidth / aNormSize.Width();
     549             : 
     550           0 :                 for( sal_Int32 i = 0; i < ( nLen - 1 ); i++ )
     551           0 :                     pDX[ i ] = FRound( pDX[ i ] * fFactor );
     552             :             }
     553             :         }
     554             : 
     555           0 :         vcl::Font aOldFont( mpVDev->GetFont() );
     556           0 :         Point               aBaseLinePos( rPos );
     557             : 
     558           0 :         vcl::Font aFont(aOldFont);
     559           0 :         short nOrientation = aFont.GetOrientation();
     560           0 :         aFont.SetOrientation( 0 );
     561           0 :         aFont.SetUnderline(UNDERLINE_NONE);
     562           0 :         aFont.SetStrikeout(STRIKEOUT_NONE);
     563           0 :         mpVDev->SetFont( aFont );
     564             : 
     565           0 :         const FontMetric    aMetric( mpVDev->GetFontMetric() );
     566             : 
     567           0 :         FlashFont&          rFlashFont = Impl_getFont( aFont );
     568             : 
     569             :         // always adjust text position to match baseline alignment
     570           0 :         switch( aOldFont.GetAlign() )
     571             :         {
     572             :             case( ALIGN_TOP ):
     573           0 :                 aBaseLinePos.Y() += aMetric.GetAscent();
     574           0 :             break;
     575             : 
     576             :             case( ALIGN_BOTTOM ):
     577           0 :                 aBaseLinePos.Y() -= aMetric.GetDescent();
     578           0 :             break;
     579             : 
     580             :             default:
     581           0 :             break;
     582             :         }
     583             : 
     584             :         // get mapped text position
     585           0 :         const Point aPt( map( aBaseLinePos ) );
     586             : 
     587             :         // write text element
     588             : 
     589             : #if 0 // makes the calculated bound rect visible for debugging
     590             : {
     591             :         Polygon aTmpPoly( aPoly );
     592             :         sal_uInt16 nID = FlashGeometryExporter::writePolygonShape( aMovieStream, aTmpPoly, false, Color(COL_MAGENTA), Color(COL_MAGENTA), mpClipPolyPolygon  );
     593             :         ImplPlaceObject( nID );
     594             : }
     595             : #endif
     596             : 
     597             :         // CL: This is still a hack until we figure out how to calculate a correct bound rect
     598             :         //     for rotated text
     599           0 :         Rectangle textBounds( 0, 0, static_cast<long>(mnDocWidth*mnDocXScale), static_cast<long>(mnDocHeight*mnDocYScale) );
     600           0 :         double scale = 1.0;
     601             : 
     602             :         // scale width if we have a stretched text
     603           0 :         if( 0 != aFont.GetSize().Width() )
     604             :         {
     605           0 :             vcl::Font aTmpFont( aFont );
     606           0 :             aTmpFont.SetWidth(0);
     607           0 :             mpVDev->SetFont( aTmpFont );
     608             : 
     609           0 :             const FontMetric aMetric2( mpVDev->GetFontMetric() );
     610           0 :             mpVDev->SetFont( aFont );
     611             : 
     612           0 :             const long n1 = aFont.GetSize().Width();
     613           0 :             const long n2 = aMetric2.GetSize().Width();
     614           0 :             scale =  (double)n1 / (double)n2;
     615             :         }
     616             : 
     617           0 :         basegfx::B2DHomMatrix m(basegfx::tools::createRotateB2DHomMatrix(static_cast<double>(nOrientation) * F_PI1800));
     618           0 :         m.translate( double(aPt.X() / scale), double(aPt.Y()) );
     619           0 :         m.scale( scale, scale );
     620             : 
     621           0 :         sal_Int16 nHeight = _Int16( map( Size( 0, aFont.GetHeight() ) ).Height() );
     622             : 
     623           0 :         startTag( TAG_DEFINETEXT );
     624             : 
     625           0 :         sal_uInt16 nTextId = createID();
     626             : 
     627           0 :         mpTag->addUI16( nTextId );
     628           0 :         mpTag->addRect( textBounds );
     629           0 :         mpTag->addMatrix( m );
     630             : 
     631           0 :         sal_uInt8 nGlyphBits = 16;
     632           0 :         sal_uInt8 nAdvanceBits = 16;
     633             : 
     634           0 :         mpTag->addUI8( nGlyphBits );
     635           0 :         mpTag->addUI8( nAdvanceBits );
     636             : 
     637             :         // text style change record
     638           0 :         mpTag->addUI8( 0x8c );
     639           0 :         mpTag->addUI16( rFlashFont.getID() );
     640           0 :         mpTag->addRGB( aTextColor );
     641           0 :         mpTag->addUI16( _uInt16( nHeight ) );
     642             : 
     643             :         DBG_ASSERT( nLen <= 127, "TODO: handle text with more than 127 characters" );
     644             : 
     645             :         // Glyph record
     646           0 :         mpTag->addUI8( (sal_uInt8) nLen );
     647             : 
     648           0 :         BitStream aBits;
     649             : 
     650           0 :         sal_Int32 nLastDX = 0;
     651             :         sal_Int32 nAdvance;
     652           0 :         for( sal_Int32 i = 0; i < nLen; i++  )
     653             :         {
     654           0 :             if( i < (nLen-1) )
     655             :             {
     656           0 :                 nAdvance = pDX[i] - nLastDX;
     657           0 :                 nLastDX = pDX[i];
     658             :             }
     659             :             else
     660             :             {
     661           0 :                 nAdvance = 0;
     662             :             }
     663             : 
     664           0 :             aBits.writeUB( rFlashFont.getGlyph(rText[i],mpVDev), nGlyphBits );
     665           0 :             aBits.writeSB( _Int16(map( Size( (long)( nAdvance / scale ), 0 ) ).Width() ), nAdvanceBits );
     666             :         }
     667             : 
     668           0 :         mpTag->addBits( aBits );
     669           0 :         mpTag->addUI8( 0 );
     670             : 
     671           0 :         endTag();
     672             : 
     673           0 :         maShapeIds.push_back( nTextId );
     674             : 
     675             :         // AS: Write strikeout and underline, if necessary.  This code was originally taken from the SVG
     676             :         //  export facility, although the positioning had to be tweaked a little.  I can't explain the
     677             :         //  numbers, but the flash lines up very well with the original OOo document.  All of this should
     678             :         //  probably be converted to polygons as part of the meta file, though, as we don't handle any
     679             :         //  fancy lines (like dashes).
     680           0 :         if( ( aOldFont.GetStrikeout() != STRIKEOUT_NONE ) || ( aOldFont.GetUnderline() != UNDERLINE_NONE ) )
     681             :         {
     682           0 :             Polygon     aPoly( 4 );
     683           0 :             const long  nLineHeight = std::max( (long) FRound( aMetric.GetLineHeight() * 0.05 ), (long) 1 );
     684             : 
     685           0 :             if( aOldFont.GetStrikeout() != STRIKEOUT_NONE )
     686             :             {
     687           0 :                 aPoly[ 0 ].X() = aBaseLinePos.X();
     688           0 :                 aPoly[ 0 ].Y() = aBaseLinePos.Y() - FRound( aMetric.GetAscent() * 0.26 ) - nLineHeight;
     689           0 :                 aPoly[ 1 ].X() = aPoly[ 0 ].X() + aNormSize.Width() - 1;
     690           0 :                 aPoly[ 1 ].Y() = aPoly[ 0 ].Y();
     691           0 :                 aPoly[ 2 ].X() = aPoly[ 1 ].X();
     692           0 :                 aPoly[ 2 ].Y() = aPoly[ 1 ].Y() + nLineHeight - 1;
     693           0 :                 aPoly[ 3 ].X() = aPoly[ 0 ].X();
     694           0 :                 aPoly[ 3 ].Y() = aPoly[ 2 ].Y();
     695             : 
     696           0 :                 Impl_writePolygon( aPoly, true, aTextColor, aTextColor );
     697             :             }
     698             : 
     699             :             // AS: The factor of 1.5 on the nLineHeight is a magic number.  I'm not sure why it works,
     700             :             //  but it looks good to me.
     701           0 :             if( aOldFont.GetUnderline() != UNDERLINE_NONE )
     702             :             {
     703           0 :                 aPoly[ 0 ].X() = aBaseLinePos.X();
     704           0 :                 aPoly[ 0 ].Y() = static_cast<long>(aBaseLinePos.Y() + 1.5*nLineHeight);
     705           0 :                 aPoly[ 1 ].X() = aPoly[ 0 ].X() + aNormSize.Width() - 1;
     706           0 :                 aPoly[ 1 ].Y() = aPoly[ 0 ].Y();
     707           0 :                 aPoly[ 2 ].X() = aPoly[ 1 ].X();
     708           0 :                 aPoly[ 2 ].Y() = aPoly[ 1 ].Y() + nLineHeight - 1;
     709           0 :                 aPoly[ 3 ].X() = aPoly[ 0 ].X();
     710           0 :                 aPoly[ 3 ].Y() = aPoly[ 2 ].Y();
     711             : 
     712           0 :                 Impl_writePolygon( aPoly, true, aTextColor, aTextColor );
     713           0 :             }
     714             :         }
     715             : 
     716           0 :         mpVDev->SetFont( aOldFont );
     717           0 :     }
     718             : }
     719             : 
     720             : // AS: Because JPEGs require the alpha channel provided separately (JPEG does not
     721             : //  natively support alpha channel, but SWF lets you provide it separately), we
     722             : //  extract the alpha channel into a separate array here.
     723           0 : void getBitmapData( const BitmapEx& aBmpEx, sal_uInt8*& tgadata, sal_uInt8*& tgaAlphadata, sal_uInt32& nWidth, sal_uInt32& nHeight )
     724             : {
     725           0 :     if( !aBmpEx.IsEmpty() )
     726             :     {
     727           0 :         Bitmap              aBmp( aBmpEx.GetBitmap() );
     728           0 :         BitmapReadAccess*   pRAcc = aBmp.AcquireReadAccess();
     729             : 
     730           0 :         if( pRAcc )
     731             :         {
     732           0 :             AlphaMask   aAlpha;
     733           0 :             nWidth = pRAcc->Width();
     734           0 :             nHeight = pRAcc->Height();
     735           0 :             tgadata = new sal_uInt8[nWidth*nHeight*4];
     736           0 :             tgaAlphadata = new sal_uInt8[nWidth*nHeight];
     737           0 :             sal_uInt8* p = tgadata, *pAlpha = tgaAlphadata;
     738             : 
     739             : 
     740           0 :             if( aBmpEx.IsAlpha() )
     741           0 :                 aAlpha = aBmpEx.GetAlpha();
     742           0 :             else if( aBmpEx.IsTransparent() )
     743           0 :                 aAlpha = aBmpEx.GetMask();
     744             :             else
     745             :             {
     746           0 :                 sal_uInt8 cAlphaVal = 0;
     747           0 :                 aAlpha = AlphaMask( aBmp.GetSizePixel(), &cAlphaVal );
     748             :             }
     749             : 
     750           0 :             BitmapReadAccess* pAAcc = aAlpha.AcquireReadAccess();
     751             : 
     752           0 :             if( pAAcc )
     753             :             {
     754           0 :                 for( sal_uInt32 nY = 0; nY < nHeight; nY++ )
     755             :                 {
     756           0 :                     for( sal_uInt32 nX = 0; nX < nWidth; nX++ )
     757             :                     {
     758           0 :                         const sal_uInt8     nAlpha = pAAcc->GetPixel( nY, nX ).GetIndex();
     759           0 :                         const BitmapColor   aPixelColor( pRAcc->GetColor( nY, nX ) );
     760             : 
     761           0 :                         if( nAlpha == 0xff )
     762             :                         {
     763           0 :                             *p++ = 0;
     764           0 :                             *p++ = 0;
     765           0 :                             *p++ = 0;
     766           0 :                             *p++ = 0;
     767             :                         }
     768             :                         else
     769             :                         {
     770           0 :                             *p++ = 0xff-nAlpha;
     771           0 :                             *p++ = aPixelColor.GetRed();
     772           0 :                             *p++ = aPixelColor.GetGreen();
     773           0 :                             *p++ = aPixelColor.GetBlue();
     774             :                         }
     775           0 :                         *pAlpha++ = 0xff - nAlpha;
     776           0 :                     }
     777             :                 }
     778             : 
     779           0 :                 aAlpha.ReleaseAccess( pAAcc );
     780             :             }
     781             : 
     782           0 :             Bitmap::ReleaseAccess( pRAcc );
     783           0 :         }
     784             :     }
     785           0 : }
     786             : 
     787             : 
     788           0 : sal_uInt16 Writer::defineBitmap( const BitmapEx &bmpSource, sal_Int32 nJPEGQualityLevel )
     789             : {
     790           0 :     sal_uLong bmpChecksum = bmpSource.GetChecksum();
     791             : 
     792           0 :     ChecksumCache::iterator it = mBitmapCache.find(bmpChecksum);
     793             : 
     794             :     // AS: We already exported this bitmap, so just return its ID.
     795           0 :     if (mBitmapCache.end() != it)
     796           0 :         return it->second;
     797             : 
     798           0 :     sal_uInt16 nBitmapId = createID();
     799           0 :     mBitmapCache[bmpChecksum] = nBitmapId;
     800             : 
     801             :     // AS: OK, we have a good image, so now we decide whether or not to JPEG it or
     802             :     //  or Lossless compress it.
     803             : 
     804             :     // Figure out lossless size
     805             :     sal_uInt8 *pImageData, *pAlphaData;
     806           0 :     sal_uInt32 width(0), height(0);
     807             : 
     808           0 :     getBitmapData( bmpSource, pImageData, pAlphaData, width, height );
     809           0 :     sal_uInt32 raw_size = width * height * 4;
     810           0 :     uLongf compressed_size = raw_size + (sal_uInt32)(raw_size/100) + 12;
     811           0 :     boost::scoped_array<sal_uInt8> pCompressed(new sal_uInt8[ compressed_size ]);
     812             : 
     813             : #ifdef DBG_UTIL
     814             :     if(compress2(pCompressed.get(), &compressed_size, pImageData, raw_size, Z_BEST_COMPRESSION) != Z_OK)
     815             :     {
     816             :         DBG_ASSERT( false, "compress2 failed!" ); ((void)0);
     817             :     }
     818             : #else
     819           0 :     compress2(pCompressed.get(), &compressed_size, pImageData, raw_size, Z_BEST_COMPRESSION);
     820             : #endif
     821             : 
     822             :     // AS: SWF files let you provide an Alpha mask for JPEG images, but we have
     823             :     //  to ZLIB compress the alpha channel separately.
     824           0 :     uLong alpha_compressed_size = 0;
     825           0 :     boost::scoped_array<sal_uInt8> pAlphaCompressed;
     826           0 :     if (bmpSource.IsAlpha() || bmpSource.IsTransparent())
     827             :     {
     828           0 :         alpha_compressed_size = uLongf(width * height + (sal_uInt32)(raw_size/100) + 12);
     829           0 :         pAlphaCompressed.reset(new sal_uInt8[ compressed_size ]);
     830             : 
     831             : #ifdef DBG_UTIL
     832             :         if(compress2(pAlphaCompressed.get(), &alpha_compressed_size, pAlphaData, width * height, Z_BEST_COMPRESSION) != Z_OK)
     833             :         {
     834             :             DBG_ASSERT( false, "compress2 failed!" ); ((void)0);
     835             :         }
     836             : #else
     837           0 :         compress2(pAlphaCompressed.get(), &alpha_compressed_size, pAlphaData, width * height, Z_BEST_COMPRESSION);
     838             : #endif
     839             :     }
     840             : 
     841             :     // Figure out JPEG size
     842           0 :     const sal_uInt8* pJpgData = NULL;;
     843           0 :     sal_uInt32 nJpgDataLength = 0xffffffff;
     844             : 
     845           0 :     Graphic aGraphic( bmpSource );
     846           0 :     SvMemoryStream aDstStm( 65535, 65535 );
     847             : 
     848           0 :     GraphicFilter aFilter;
     849             : 
     850           0 :     Sequence< PropertyValue > aFilterData(sal_Int32(nJPEGQualityLevel != -1));
     851           0 :     if( nJPEGQualityLevel != -1 )
     852             :     {
     853           0 :         aFilterData[0].Name = "Quality";
     854           0 :         aFilterData[0].Value <<= nJPEGQualityLevel;
     855             :     }
     856             : 
     857           0 :     if( aFilter.ExportGraphic( aGraphic, OUString(), aDstStm,
     858           0 :                                 aFilter.GetExportFormatNumberForShortName( OUString(JPG_SHORTNAME) ), &aFilterData ) == ERRCODE_NONE )
     859             :     {
     860           0 :         pJpgData = static_cast<const sal_uInt8*>(aDstStm.GetData());
     861           0 :         nJpgDataLength = aDstStm.Seek( STREAM_SEEK_TO_END );
     862             :     }
     863             : 
     864             :     // AS: Ok, now go ahead and use whichever is smaller.  If JPEG is smaller, then
     865             :     //  we have to export as TAG_DEFINEBITSJPEG3 in the case that there is alpha
     866             :     //  channel data.
     867           0 :     if ( pJpgData && ( nJpgDataLength + alpha_compressed_size < compressed_size) )
     868           0 :         Impl_writeJPEG(nBitmapId, pJpgData, nJpgDataLength, pAlphaCompressed.get(), alpha_compressed_size );
     869             :     else
     870           0 :         Impl_writeBmp( nBitmapId, width, height, pCompressed.get(), compressed_size );
     871             : 
     872           0 :     delete[] pImageData;
     873           0 :     delete[] pAlphaData;
     874             : 
     875           0 :     return nBitmapId;
     876             : }
     877             : 
     878             : 
     879             : 
     880           0 : void Writer::Impl_writeImage( const BitmapEx& rBmpEx, const Point& rPt, const Size& rSz, const Point& /* rSrcPt */, const Size& /* rSrcSz */, const Rectangle& rClipRect, bool bNeedToMapClipRect )
     881             : {
     882           0 :     if( !!rBmpEx )
     883             :     {
     884           0 :         BitmapEx bmpSource( rBmpEx );
     885             : 
     886           0 :         Rectangle originalPixelRect = Rectangle(Point(), bmpSource.GetSizePixel());
     887             : 
     888           0 :         Point srcPt( map(rPt) );
     889           0 :         Size srcSize( map(rSz) );
     890           0 :         Rectangle destRect( srcPt, srcSize );
     891             : 
     892             :         // AS: Christian, my scaling factors are different than yours, and work better for me.
     893             :         //  However, I can't explain why exactly.  I got some of this by trial and error.
     894           0 :         double XScale = destRect.GetWidth() ? static_cast<double>(originalPixelRect.GetWidth())/destRect.GetWidth() : 1.0;;
     895           0 :         double YScale = destRect.GetHeight() ? static_cast<double>(originalPixelRect.GetHeight())/destRect.GetHeight() : 1.0;;
     896             : 
     897             :         // AS: If rClipRect has a value set, then we need to crop the bmp appropriately.
     898             :         //  If a map event already occurred in the metafile, then we do not need to map
     899             :         //  the clip rect as it's already been done.
     900           0 :         if (!rClipRect.IsEmpty())
     901             :         {
     902             :             // AS: Christian, I also don't understand why bNeedToMapClipRect is necessary, but it
     903             :             //  works like a charm.  Usually, the map event in the meta file does not cause the
     904             :             //  clipping rectangle to get mapped.  However, sometimes there are multiple layers
     905             :             //  of mapping which eventually do cause the clipping rect to be mapped.
     906           0 :             Size clipSize( bNeedToMapClipRect ? map(rClipRect.GetSize()) : rClipRect.GetSize() );
     907           0 :             Rectangle clipRect = Rectangle(Point(), clipSize);
     908           0 :             destRect.Intersection( clipRect );
     909             : 
     910           0 :             Rectangle cropRect(destRect);
     911             : 
     912             :             // AS: The bmp origin is always 0,0 so we have to adjust before we crop.
     913           0 :             cropRect.Move(-srcPt.X(), -srcPt.Y());
     914             :             // AS: Rectangle has no scale function (?!) so I do it manually...
     915           0 :             Rectangle cropPixelRect(static_cast<long>(cropRect.Left()*XScale),
     916           0 :                                     static_cast<long>(cropRect.Top()*YScale),
     917           0 :                                     static_cast<long>(cropRect.Right()*XScale),
     918           0 :                                     static_cast<long>(cropRect.Bottom()*YScale));
     919             : 
     920           0 :             bmpSource.Crop(cropPixelRect);
     921             :         }
     922             : 
     923           0 :         if( !!bmpSource )
     924             :         {
     925             :             // #105949# fix images that are under 16 pixels width or height by
     926             :             //          expanding them. Some swf players can't display such small
     927             :             //          bitmaps
     928           0 :             const Size& rSizePixel = bmpSource.GetSizePixel();
     929           0 :             if( (rSizePixel.Width() < 16) || (rSizePixel.Height() < 16) )
     930             :             {
     931           0 :                 const sal_uInt32 nDX = rSizePixel.Width() < 16 ? 16 - rSizePixel.Width() : 0;
     932           0 :                 const sal_uInt32 nDY = rSizePixel.Height() < 16 ? 16 - rSizePixel.Height() : 0;
     933           0 :                 bmpSource.Expand( nDX, nDY );
     934             :             }
     935             : 
     936           0 :             sal_Int32 nJPEGQuality = mnJPEGCompressMode;
     937             : 
     938           0 :             Size szDestPixel = mpVDev->LogicToPixel(srcSize, aTWIPSMode);
     939             : 
     940           0 :             double pixXScale = originalPixelRect.GetWidth() ? static_cast<double>(szDestPixel.Width()) / originalPixelRect.GetWidth() : 1.0;
     941           0 :             double pixYScale = originalPixelRect.GetHeight() ? static_cast<double>(szDestPixel.Height()) / originalPixelRect.GetHeight() : 1.0;
     942             : 
     943             :             // AS: If the image has been scaled down, then scale down the quality
     944             :             //   that we use for JPEG compression.
     945           0 :             if (pixXScale < 1.0 && pixYScale < 1.0)
     946             :             {
     947             : 
     948           0 :                 double qualityScale = (pixXScale + pixYScale)/2;
     949             : 
     950           0 :                 nJPEGQuality = (sal_Int32)( nJPEGQuality * qualityScale );
     951             : 
     952           0 :                 if (nJPEGQuality < 10)
     953           0 :                     nJPEGQuality += 3;
     954             :             }
     955             : 
     956           0 :             sal_uInt16 nBitmapId = defineBitmap(bmpSource, nJPEGQuality);
     957             : 
     958           0 :             Polygon aPoly( destRect );
     959             : 
     960             :             // AS: Since images are being cropped now, no translation is normally necessary.
     961             :             //  However, some things like graphical bullet points still get translated.
     962           0 :             ::basegfx::B2DHomMatrix m; // #i73264#
     963           0 :             m.scale(1.0/XScale, 1.0/YScale );
     964           0 :             if (destRect.Left() || destRect.Top())
     965           0 :                 m.translate(destRect.Left(), destRect.Top());
     966             : 
     967           0 :             FillStyle aFillStyle( nBitmapId, true, m );
     968             : 
     969           0 :             sal_uInt16 nShapeId = defineShape( aPoly, aFillStyle );
     970             : 
     971           0 :             maShapeIds.push_back( nShapeId );
     972           0 :         }
     973             :     }
     974           0 : }
     975             : 
     976             : 
     977           0 : void Writer::Impl_writeBmp( sal_uInt16 nBitmapId, sal_uInt32 width, sal_uInt32 height, sal_uInt8 *pCompressed, sal_uInt32 compressed_size )
     978             : {
     979           0 :     startTag( TAG_DEFINEBITSLOSSLESS2 );
     980             : 
     981           0 :     mpTag->addUI16( nBitmapId );
     982           0 :     mpTag->addUI8( 5 );
     983           0 :     mpTag->addUI16( _uInt16(width) );
     984           0 :     mpTag->addUI16( _uInt16(height) );
     985             : 
     986           0 :     mpTag->Write( pCompressed, compressed_size );
     987             : 
     988           0 :     endTag();
     989           0 : }
     990             : 
     991             : 
     992             : 
     993           0 : void Writer::Impl_writeJPEG(sal_uInt16 nBitmapId, const sal_uInt8* pJpgData, sal_uInt32 nJpgDataLength, sal_uInt8 *pAlphaCompressed, sal_uInt32 alpha_compressed_size )
     994             : {
     995             :     // AS: Go through the actual JPEG bits, separating out the
     996             :     //  header fields from the actual image fields.  Fields are
     997             :     //  identifed by 0xFFXX where XX is the field type.  Both
     998             :     //  the header and the image need start and stop (D8 and D9),
     999             :     //  so that's why you see those written to both.  I don't
    1000             :     //  really know what the rest of these are, I got it to work
    1001             :     //  kind of by trial and error and by comparing with known
    1002             :     //  good SWF files.
    1003           0 :     sal_uInt8 cType = 0x01;
    1004           0 :     const sal_uInt8* pJpgSearch = pJpgData;
    1005             : 
    1006           0 :     int nLength = 0;
    1007             : 
    1008           0 :     SvMemoryStream EncodingTableStream;
    1009           0 :     SvMemoryStream ImageBitsStream;
    1010           0 :     for (;pJpgSearch < pJpgData + nJpgDataLength; pJpgSearch += nLength)
    1011             :     {
    1012             : 
    1013             : #ifdef DBG_UTIL
    1014             :         if (0xFF != *pJpgSearch)
    1015             :         {
    1016             :             OSL_FAIL( "Expected JPEG marker." ); ((void)0);
    1017             :         }
    1018             : #endif
    1019             : 
    1020           0 :         cType = *(pJpgSearch + 1);
    1021             : 
    1022           0 :         if (0xD8 == cType || 0xD9 == cType)
    1023             :         {
    1024           0 :             nLength = 2;
    1025             :         }
    1026           0 :         else if (0xDA == cType)
    1027             :         {
    1028             :             //AS: This is the actual image data, and runs to the
    1029             :             // end of the file (as best I know), minus 2 bytes
    1030             :             // for the closing 0xFFD9.
    1031           0 :             nLength = nJpgDataLength - (pJpgSearch - pJpgData) - 2;
    1032             :         }
    1033             :         else
    1034             :         {
    1035             :             // AS: Lengths are big endian.
    1036             : 
    1037             :             // Beware. pJpgSearch is not necessarily word-aligned,
    1038             :             // so we access it byte-wise.
    1039             : 
    1040             :             // AS: Add 2 to the length to include the 0xFFXX itself.
    1041           0 :             nLength = 2 + (pJpgSearch[2]<<8) + pJpgSearch[3];
    1042             :         }
    1043             : 
    1044             :         // AS: I'm referring to libjpeg for a list of what these
    1045             :         //  markers are.  See jdmarker.c for a list.
    1046             :         // AS: I'm ignoring application specific markers 0xE1...0xEF
    1047             :         //  and comments 0xFE.  I don't know what
    1048             :         //  0xF0 or 0xFD are for, and they don't come up.
    1049             :         //  Additionally, 0xDE and 0xDF aren't clear to me.
    1050           0 :         switch(cType)
    1051             :         {
    1052             :         case 0xD8:
    1053             :         case 0xD9:
    1054           0 :             EncodingTableStream.Write( pJpgSearch, nLength );
    1055           0 :             ImageBitsStream.Write( pJpgSearch, nLength );
    1056           0 :             break;
    1057             : 
    1058             :         case 0x01:
    1059             :         case 0xDB:
    1060             :         case 0xDC:
    1061             :         case 0xDD:
    1062             :         case 0xC4:
    1063           0 :             EncodingTableStream.Write( pJpgSearch, nLength );
    1064           0 :             break;
    1065             : 
    1066             :         case 0xC0:
    1067             :         case 0xC1:
    1068             :         case 0xC2:
    1069             :         case 0xC3:
    1070             :         case 0xC5:
    1071             :         case 0xC6:
    1072             :         case 0xC7:
    1073             : //      case 0xC8: Apparently reserved for JPEG extensions?
    1074             :         case 0xC9:
    1075             :         case 0xCA:
    1076             :         case 0xCB:
    1077             :         case 0xCD:
    1078             :         case 0xCE:
    1079             :         case 0xCF:
    1080             :         case 0xDA:
    1081             :         case 0xE0:
    1082           0 :             ImageBitsStream.Write( pJpgSearch, nLength );
    1083           0 :             break;
    1084             : 
    1085             :         default:
    1086             :             OSL_FAIL( "JPEG marker I didn't handle!" );
    1087             : 
    1088             :         }
    1089             :     }
    1090             : 
    1091           0 :     EncodingTableStream.Seek( STREAM_SEEK_TO_END );
    1092           0 :     sal_uInt32 nEncodingTableSize = EncodingTableStream.Tell();
    1093           0 :     EncodingTableStream.Seek( STREAM_SEEK_TO_BEGIN );
    1094             : 
    1095           0 :     ImageBitsStream.Seek( STREAM_SEEK_TO_END );
    1096           0 :     sal_uInt32 nImageBitsSize = ImageBitsStream.Tell();
    1097           0 :     ImageBitsStream.Seek( STREAM_SEEK_TO_BEGIN );
    1098             : 
    1099             :     // AS: If we need alpha support, use TAG_DEFINEBITSJPEG3.
    1100           0 :     if (alpha_compressed_size > 0)
    1101             :     {
    1102           0 :         startTag( TAG_DEFINEBITSJPEG3 );
    1103             : 
    1104           0 :         mpTag->addUI16( nBitmapId );
    1105             : 
    1106           0 :         mpTag->addUI32( nEncodingTableSize + nImageBitsSize );
    1107             : 
    1108           0 :         mpTag->Write(EncodingTableStream.GetData(), nEncodingTableSize);
    1109           0 :         mpTag->Write(ImageBitsStream.GetData(), nImageBitsSize);
    1110             : 
    1111           0 :         mpTag->Write( pAlphaCompressed, alpha_compressed_size );
    1112             : 
    1113           0 :         endTag();
    1114             :     }
    1115             :     else
    1116             :     {
    1117           0 :         startTag( TAG_DEFINEBITSJPEG2 );
    1118             : 
    1119           0 :         mpTag->addUI16( nBitmapId );
    1120             : 
    1121           0 :         mpTag->Write(EncodingTableStream.GetData(), nEncodingTableSize);
    1122           0 :         mpTag->Write(ImageBitsStream.GetData(), nImageBitsSize);
    1123             : 
    1124           0 :         endTag();
    1125           0 :     }
    1126           0 : }
    1127             : 
    1128             : 
    1129             : 
    1130           0 : void Writer::Impl_writeLine( const Point& rPt1, const Point& rPt2, const Color* pLineColor )
    1131             : {
    1132           0 :     Color aOldColor( mpVDev->GetLineColor() );
    1133           0 :     if( pLineColor )
    1134           0 :         mpVDev->SetLineColor( *pLineColor );
    1135             : 
    1136           0 :     const Point aPtAry[2] = { rPt1, rPt2 };
    1137           0 :     Polygon aPoly( 2, aPtAry );
    1138           0 :     Impl_writePolyPolygon( aPoly, false );
    1139             : 
    1140           0 :     mpVDev->SetLineColor( aOldColor );
    1141           0 : }
    1142             : 
    1143             : 
    1144             : 
    1145           0 : void Writer::Impl_writeRect( const Rectangle& rRect, long nRadX, long nRadY )
    1146             : {
    1147           0 :     if( (rRect.Top() == rRect.Bottom()) || (rRect.Left() == rRect.Right()) )
    1148             :     {
    1149           0 :         Color aColor( mpVDev->GetFillColor() );
    1150           0 :         Impl_writeLine( rRect.TopLeft(), rRect.BottomRight(), &aColor );
    1151             :     }
    1152             :     else
    1153             :     {
    1154           0 :         Polygon aPoly( rRect, nRadX, nRadY );
    1155           0 :         Impl_writePolyPolygon( aPoly, true );
    1156             :     }
    1157           0 : }
    1158             : 
    1159             : 
    1160             : 
    1161           0 : void Writer::Impl_writeEllipse( const Point& rCenter, long nRadX, long nRadY )
    1162             : {
    1163           0 :     Polygon aPoly( rCenter, nRadX, nRadY );
    1164           0 :     Impl_writePolyPolygon( aPoly, false );
    1165           0 : }
    1166             : 
    1167             : 
    1168             : /** Writes the stroke defined by SvtGraphicStroke and returns true or it returns
    1169             :     false if it can't handle this stroke.
    1170             : */
    1171           0 : bool Writer::Impl_writeStroke( SvtGraphicStroke& rStroke )
    1172             : {
    1173           0 :     Polygon aPolygon;
    1174           0 :     rStroke.getPath( aPolygon );
    1175           0 :     tools::PolyPolygon aPolyPolygon( aPolygon );
    1176             : 
    1177           0 :     map( aPolyPolygon );
    1178             : 
    1179             :     // as log as not LINESTYLE2 and DefineShape4 is used (which
    1180             :     // added support for LineJoin), only round LineJoins are
    1181             :     // supported. Fallback to MetaActionType::POLYLINE and MetaActionType::LINE
    1182           0 :     if(SvtGraphicStroke::joinRound != rStroke.getJoinType())
    1183           0 :         return false;
    1184             : 
    1185           0 :     tools::PolyPolygon aStartArrow;
    1186           0 :     rStroke.getStartArrow( aStartArrow );
    1187           0 :     if( 0 != aStartArrow.Count() )
    1188           0 :         return false;       // todo: Implement line ends
    1189             : 
    1190           0 :     tools::PolyPolygon aEndArrow;
    1191           0 :     rStroke.getEndArrow( aEndArrow );
    1192           0 :     if( 0 != aEndArrow.Count() )
    1193           0 :         return false;       // todo: Implement line ends
    1194             : 
    1195           0 :     SvtGraphicStroke::DashArray aDashArray;
    1196           0 :     rStroke.getDashArray( aDashArray );
    1197           0 :     if( 0 != aDashArray.size() )
    1198           0 :         return false;       // todo: implement dashes
    1199             : 
    1200           0 :     Color aColor( mpVDev->GetLineColor() );
    1201             : 
    1202           0 :     if( 0.0 != rStroke.getTransparency() )
    1203           0 :         aColor.SetTransparency( sal::static_int_cast<sal_uInt8>( MinMax( (long int)( rStroke.getTransparency() * 0xff ), 0, 0xff ) ) );
    1204             : 
    1205           0 :     sal_uInt16 nShapeId = defineShape( aPolyPolygon, sal::static_int_cast<sal_uInt16>( mapRelative( (sal_Int32)( rStroke.getStrokeWidth() ) ) ), aColor );
    1206           0 :     maShapeIds.push_back( nShapeId );
    1207           0 :     return true;
    1208             : }
    1209             : 
    1210             : 
    1211             : 
    1212             : /** Writes the filling defined by SvtGraphicFill and returns true or it returns
    1213             :     false if it can't handle this filling.
    1214             : */
    1215           0 : bool Writer::Impl_writeFilling( SvtGraphicFill& rFilling )
    1216             : {
    1217           0 :     tools::PolyPolygon aPolyPolygon;
    1218           0 :     rFilling.getPath( aPolyPolygon );
    1219             : 
    1220           0 :     Rectangle aOldRect( aPolyPolygon.GetBoundRect() );
    1221             : 
    1222           0 :     map( aPolyPolygon );
    1223             : 
    1224           0 :     Rectangle aNewRect( aPolyPolygon.GetBoundRect() );
    1225             : 
    1226           0 :     switch( rFilling.getFillType() )
    1227             :     {
    1228             :     case SvtGraphicFill::fillSolid:
    1229             :         {
    1230           0 :             Color aColor( rFilling.getFillColor() );
    1231             : 
    1232           0 :             if( 0.0 != rFilling.getTransparency() )
    1233           0 :                 aColor.SetTransparency( sal::static_int_cast<sal_uInt8>( MinMax( (long int)( rFilling.getTransparency() * 0xff ) , 0, 0xff ) ) );
    1234             : 
    1235           0 :             FillStyle aFillStyle( aColor );
    1236             : 
    1237           0 :             sal_uInt16 nShapeId = defineShape( aPolyPolygon, aFillStyle );
    1238           0 :             maShapeIds.push_back( nShapeId );
    1239             :         }
    1240           0 :         break;
    1241             :     case SvtGraphicFill::fillGradient:
    1242           0 :         return false;
    1243             :     case SvtGraphicFill::fillHatch:
    1244           0 :         return false;
    1245             :     case SvtGraphicFill::fillTexture:
    1246             :         {
    1247           0 :             Graphic aGraphic;
    1248           0 :             rFilling.getGraphic( aGraphic );
    1249             : 
    1250             :             // CL->AS: Should we also scale down the quality here depending on image scale?
    1251           0 :             sal_uInt16 nBitmapId = defineBitmap( aGraphic.GetBitmapEx(), mnJPEGCompressMode );
    1252             : 
    1253           0 :             ::basegfx::B2DHomMatrix aMatrix; // #i73264#
    1254             : 
    1255           0 :             SvtGraphicFill::Transform aTransform;
    1256             : 
    1257           0 :             rFilling.getTransform( aTransform );
    1258             : 
    1259             :             sal_uInt16 a,b;
    1260           0 :             for( a = 0; a < 2; a++ )
    1261             :             {
    1262           0 :                 for( b = 0; b < 3; b++ )
    1263             :                 {
    1264           0 :                     aMatrix.set(a, b, aTransform.matrix[a*3+b]);
    1265             :                 }
    1266             :             }
    1267           0 :             aMatrix.set(2, 0, 0.0);
    1268           0 :             aMatrix.set(2, 1, 0.0);
    1269           0 :             aMatrix.set(2, 2, 1.0);
    1270             : 
    1271             :             // scale bitmap
    1272           0 :             double XScale = aOldRect.GetWidth() ? (double)aNewRect.GetWidth()/aOldRect.GetWidth() : 1.0;
    1273           0 :             double YScale = aOldRect.GetHeight() ? (double)aNewRect.GetHeight()/aOldRect.GetHeight() : 1.0;
    1274             : 
    1275           0 :             aMatrix.scale( XScale, YScale );
    1276             : 
    1277           0 :             FillStyle aFillStyle( nBitmapId, !rFilling.IsTiling(), aMatrix );
    1278             : 
    1279           0 :             sal_uInt16 nShapeId = defineShape( aPolyPolygon, aFillStyle );
    1280           0 :             maShapeIds.push_back( nShapeId );
    1281             :         }
    1282           0 :         break;
    1283             :     }
    1284           0 :     return true;
    1285             : }
    1286             : 
    1287             : 
    1288             : 
    1289             : /* CL: The idea was to export page fields as text fields that get theire
    1290             :    string from a variable set with actionscript by each page. This didn't
    1291             :    work out since the formatting is always wrong when text follows the
    1292             :    page number field since pages greater one may require more space than
    1293             :    page 1
    1294             : */
    1295             : #if 0
    1296             : bool Writer::Impl_writePageField( Rectangle& rTextBounds )
    1297             : {
    1298             :     startTag( TAG_DEFINEEDITTEXT );
    1299             : 
    1300             :     sal_uInt16 nTextId = createID();
    1301             : 
    1302             :     mpTag->addUI16( nTextId );
    1303             :     mpTag->addRect( rTextBounds );
    1304             : 
    1305             :     BitStream aBits;
    1306             :     aBits.writeUB( 1, 1 );                  // HasText
    1307             :     aBits.writeUB( 0, 1 );                  // WordWrap
    1308             :     aBits.writeUB( 0, 1 );                  // MultiLine
    1309             :     aBits.writeUB( 0, 1 );                  // Password
    1310             :     aBits.writeUB( 1, 1 );                  // HasTextColor
    1311             :     aBits.writeUB( 0, 1 );                  // HasMaxLength
    1312             :     aBits.writeUB( 0, 1 );                  // HasFont
    1313             :     aBits.writeUB( 0, 1 );                  // Reserved
    1314             :     aBits.writeUB( 0, 1 );                  // AutoSize
    1315             :     aBits.writeUB( 0, 1 );                  // HasLayout
    1316             :     aBits.writeUB( 1, 1 );                  // NoSelect
    1317             :     aBits.writeUB( 1, 1 );                  // Border
    1318             :     aBits.writeUB( 0, 1 );                  // Reserved
    1319             :     aBits.writeUB( 0, 1 );                  // HTML
    1320             :     aBits.writeUB( 0, 1 );                  // UseOutlines
    1321             :     mpTag->addBits( aBits );
    1322             : 
    1323             :     Color aColor( COL_BLACK );
    1324             :     mpTag->addRGB( aColor );
    1325             :     mpTag->addString( "PageNumber" );
    1326             :     mpTag->addString( "XXX" );
    1327             : 
    1328             :     endTag();
    1329             : 
    1330             :     maShapeIds.push_back( nTextId );
    1331             : 
    1332             :     return true;
    1333             : }
    1334             : #endif
    1335             : 
    1336             : 
    1337             : 
    1338           0 : void Writer::Impl_handleLineInfoPolyPolygons(const LineInfo& rInfo, const basegfx::B2DPolygon& rLinePolygon)
    1339             : {
    1340           0 :     if(rLinePolygon.count())
    1341             :     {
    1342           0 :         basegfx::B2DPolyPolygon aLinePolyPolygon(rLinePolygon);
    1343           0 :         basegfx::B2DPolyPolygon aFillPolyPolygon;
    1344             : 
    1345           0 :         rInfo.applyToB2DPolyPolygon(aLinePolyPolygon, aFillPolyPolygon);
    1346             : 
    1347           0 :         if(aLinePolyPolygon.count())
    1348             :         {
    1349           0 :             for(sal_uInt32 a(0); a < aLinePolyPolygon.count(); a++)
    1350             :             {
    1351           0 :                 const basegfx::B2DPolygon aCandidate(aLinePolyPolygon.getB2DPolygon(a));
    1352           0 :                 Impl_writePolygon(Polygon(aCandidate), false );
    1353           0 :             }
    1354             :         }
    1355             : 
    1356           0 :         if(aFillPolyPolygon.count())
    1357             :         {
    1358           0 :             const Color aOldLineColor(mpVDev->GetLineColor());
    1359           0 :             const Color aOldFillColor(mpVDev->GetFillColor());
    1360             : 
    1361           0 :             mpVDev->SetLineColor();
    1362           0 :             mpVDev->SetFillColor(aOldLineColor);
    1363             : 
    1364           0 :             for(sal_uInt32 a(0); a < aFillPolyPolygon.count(); a++)
    1365             :             {
    1366           0 :                 const Polygon aPolygon(aFillPolyPolygon.getB2DPolygon(a));
    1367           0 :                 Impl_writePolyPolygon(tools::PolyPolygon(Polygon(aPolygon)), true );
    1368           0 :             }
    1369             : 
    1370           0 :             mpVDev->SetLineColor(aOldLineColor);
    1371           0 :             mpVDev->SetFillColor(aOldFillColor);
    1372           0 :         }
    1373             :     }
    1374           0 : }
    1375             : 
    1376             : 
    1377             : 
    1378           0 : void Writer::Impl_writeActions( const GDIMetaFile& rMtf )
    1379             : {
    1380           0 :     Rectangle clipRect;
    1381           0 :     int bMap = 0;
    1382           0 :     for( size_t i = 0, nCount = rMtf.GetActionSize(); i < nCount; i++ )
    1383             :     {
    1384           0 :         const MetaAction*    pAction = rMtf.GetAction( i );
    1385           0 :         const MetaActionType nType = pAction->GetType();
    1386             : 
    1387           0 :         switch( nType )
    1388             :         {
    1389             :             case( MetaActionType::PIXEL ):
    1390             :             {
    1391           0 :                 const MetaPixelAction* pA = static_cast<const MetaPixelAction*>(pAction);
    1392             : 
    1393           0 :                 Impl_writeLine( pA->GetPoint(), pA->GetPoint(), &pA->GetColor() );
    1394             :             }
    1395           0 :             break;
    1396             : 
    1397             :             case( MetaActionType::POINT ):
    1398             :             {
    1399           0 :                 const MetaPointAction* pA = static_cast<const MetaPointAction*>(pAction);
    1400             : 
    1401           0 :                 Impl_writeLine( pA->GetPoint(), pA->GetPoint() );
    1402             :             }
    1403           0 :             break;
    1404             : 
    1405             :             case( MetaActionType::LINE ):
    1406             :             {
    1407           0 :                 const MetaLineAction* pA = static_cast<const MetaLineAction*>(pAction);
    1408             : 
    1409           0 :                 if(pA->GetLineInfo().IsDefault())
    1410             :                 {
    1411           0 :                     Impl_writeLine( pA->GetStartPoint(), pA->GetEndPoint() );
    1412             :                 }
    1413             :                 else
    1414             :                 {
    1415             :                     // LineInfo used; handle Dash/Dot and fat lines
    1416           0 :                     basegfx::B2DPolygon aPolygon;
    1417           0 :                     aPolygon.append(basegfx::B2DPoint(pA->GetStartPoint().X(), pA->GetStartPoint().Y()));
    1418           0 :                     aPolygon.append(basegfx::B2DPoint(pA->GetEndPoint().X(), pA->GetEndPoint().Y()));
    1419           0 :                     Impl_handleLineInfoPolyPolygons(pA->GetLineInfo(), aPolygon);
    1420             :                 }
    1421             :             }
    1422           0 :             break;
    1423             : 
    1424             :             case( MetaActionType::RECT ):
    1425             :             {
    1426           0 :                 Impl_writeRect( static_cast<const MetaRectAction*>(pAction)->GetRect(), 0, 0 );
    1427             :             }
    1428           0 :             break;
    1429             : 
    1430             :             case( MetaActionType::ROUNDRECT ):
    1431             :             {
    1432           0 :                 const MetaRoundRectAction* pA = static_cast<const MetaRoundRectAction*>(pAction);
    1433             : 
    1434           0 :                 Impl_writeRect( pA->GetRect(), pA->GetHorzRound(), pA->GetVertRound() );
    1435             :             }
    1436           0 :             break;
    1437             : 
    1438             :             case( MetaActionType::ELLIPSE ):
    1439             :             {
    1440           0 :                 const MetaEllipseAction*    pA = static_cast<const MetaEllipseAction*>(pAction);
    1441           0 :                 const Rectangle&            rRect = pA->GetRect();
    1442             : 
    1443           0 :                 Impl_writeEllipse( rRect.Center(), rRect.GetWidth() >> 1, rRect.GetHeight() >> 1 );
    1444             :             }
    1445           0 :             break;
    1446             : 
    1447             :             case( MetaActionType::ARC ):
    1448             :             case( MetaActionType::PIE ):
    1449             :             case( MetaActionType::CHORD ):
    1450             :             case( MetaActionType::POLYGON ):
    1451             :             {
    1452           0 :                 Polygon aPoly;
    1453             : 
    1454           0 :                 switch( nType )
    1455             :                 {
    1456             :                     case( MetaActionType::ARC ):
    1457             :                     {
    1458           0 :                         const MetaArcAction* pA = static_cast<const MetaArcAction*>(pAction);
    1459           0 :                         aPoly = Polygon( pA->GetRect(), pA->GetStartPoint(), pA->GetEndPoint(), POLY_ARC );
    1460             :                     }
    1461           0 :                     break;
    1462             : 
    1463             :                     case( MetaActionType::PIE ):
    1464             :                     {
    1465           0 :                         const MetaPieAction* pA = static_cast<const MetaPieAction*>(pAction);
    1466           0 :                         aPoly = Polygon( pA->GetRect(), pA->GetStartPoint(), pA->GetEndPoint(), POLY_PIE );
    1467             :                     }
    1468           0 :                     break;
    1469             : 
    1470             :                     case( MetaActionType::CHORD ):
    1471             :                     {
    1472           0 :                         const MetaChordAction* pA = static_cast<const MetaChordAction*>(pAction);
    1473           0 :                         aPoly = Polygon( pA->GetRect(), pA->GetStartPoint(), pA->GetEndPoint(), POLY_CHORD );
    1474             :                     }
    1475           0 :                     break;
    1476             : 
    1477             :                     case( MetaActionType::POLYGON ):
    1478           0 :                         aPoly = static_cast<const MetaPolygonAction*>(pAction)->GetPolygon();
    1479           0 :                     break;
    1480           0 :                     default: break;
    1481             :                 }
    1482             : 
    1483           0 :                 if( aPoly.GetSize() )
    1484             :                 {
    1485           0 :                     Impl_writePolygon( aPoly, true );
    1486           0 :                 }
    1487             :             }
    1488           0 :             break;
    1489             : 
    1490             :             case( MetaActionType::POLYLINE ):
    1491             :             {
    1492           0 :                 const MetaPolyLineAction*   pA = static_cast<const MetaPolyLineAction*>(pAction);
    1493           0 :                 const Polygon&              rPoly = pA->GetPolygon();
    1494             : 
    1495           0 :                 if( rPoly.GetSize() )
    1496             :                 {
    1497           0 :                     if(pA->GetLineInfo().IsDefault())
    1498             :                     {
    1499           0 :                         Impl_writePolygon( rPoly, false );
    1500             :                     }
    1501             :                     else
    1502             :                     {
    1503             :                         // LineInfo used; handle Dash/Dot and fat lines
    1504           0 :                         Impl_handleLineInfoPolyPolygons(pA->GetLineInfo(), rPoly.getB2DPolygon());
    1505             :                     }
    1506             :                 }
    1507             :             }
    1508           0 :             break;
    1509             : 
    1510             :             case( MetaActionType::POLYPOLYGON ):
    1511             :             {
    1512           0 :                 const MetaPolyPolygonAction*    pA = static_cast<const MetaPolyPolygonAction*>(pAction);
    1513           0 :                 const tools::PolyPolygon&              rPolyPoly = pA->GetPolyPolygon();
    1514             : 
    1515           0 :                 if( rPolyPoly.Count() )
    1516           0 :                     Impl_writePolyPolygon( rPolyPoly, true );
    1517             :             }
    1518           0 :             break;
    1519             : 
    1520             :             case( MetaActionType::GRADIENT ):
    1521             :             {
    1522           0 :                 const MetaGradientAction*   pA = static_cast<const MetaGradientAction*>(pAction);
    1523             : 
    1524           0 :                 Polygon aPoly( pA->GetRect() );
    1525           0 :                 Impl_writeGradientEx( aPoly, pA->GetGradient() );
    1526             :             }
    1527           0 :             break;
    1528             : 
    1529             :             case( MetaActionType::GRADIENTEX ):
    1530             :             {
    1531           0 :                 const MetaGradientExAction* pA = static_cast<const MetaGradientExAction*>(pAction);
    1532           0 :                 Impl_writeGradientEx( pA->GetPolyPolygon(), pA->GetGradient() );
    1533             :             }
    1534           0 :             break;
    1535             : 
    1536             :             case MetaActionType::HATCH:
    1537             :             {
    1538           0 :                 const MetaHatchAction*  pA = static_cast<const MetaHatchAction*>(pAction);
    1539           0 :                 GDIMetaFile             aTmpMtf;
    1540             : 
    1541           0 :                 mpVDev->AddHatchActions( pA->GetPolyPolygon(), pA->GetHatch(), aTmpMtf );
    1542           0 :                 Impl_writeActions( aTmpMtf );
    1543             :             }
    1544           0 :             break;
    1545             : 
    1546             :             case( MetaActionType::Transparent ):
    1547             :             {
    1548           0 :                 const MetaTransparentAction*    pA = static_cast<const MetaTransparentAction*>(pAction);
    1549           0 :                 const tools::PolyPolygon&              rPolyPoly = pA->GetPolyPolygon();
    1550             : 
    1551           0 :                 if( rPolyPoly.Count() )
    1552             :                 {
    1553             :                     // convert transparence from percent into 0x00 - 0xff
    1554           0 :                     sal_uInt8 nTransparence = (sal_uInt8) MinMax( FRound( pA->GetTransparence() * 2.55 ), 0, 255 );
    1555           0 :                     Impl_writePolyPolygon( rPolyPoly, true, nTransparence );
    1556             :                 }
    1557             :             }
    1558           0 :             break;
    1559             : 
    1560             :             case( MetaActionType::FLOATTRANSPARENT ):
    1561             :             {
    1562           0 :                 const MetaFloatTransparentAction*   pA = static_cast<const MetaFloatTransparentAction*>(pAction);
    1563           0 :                 GDIMetaFile                         aTmpMtf( pA->GetGDIMetaFile() );
    1564           0 :                 Point                               aSrcPt( aTmpMtf.GetPrefMapMode().GetOrigin() );
    1565           0 :                 const Size                          aSrcSize( aTmpMtf.GetPrefSize() );
    1566           0 :                 const Point                         aDestPt( pA->GetPoint() );
    1567           0 :                 const Size                          aDestSize( pA->GetSize() );
    1568           0 :                 const double                        fScaleX = aSrcSize.Width() ? (double) aDestSize.Width() / aSrcSize.Width() : 1.0;
    1569           0 :                 const double                        fScaleY = aSrcSize.Height() ? (double) aDestSize.Height() / aSrcSize.Height() : 1.0;
    1570             :                 long                                nMoveX, nMoveY;
    1571             : 
    1572           0 :                 if( fScaleX != 1.0 || fScaleY != 1.0 )
    1573             :                 {
    1574           0 :                     aTmpMtf.Scale( fScaleX, fScaleY );
    1575           0 :                     aSrcPt.X() = FRound( aSrcPt.X() * fScaleX );
    1576           0 :                     aSrcPt.Y() = FRound( aSrcPt.Y() * fScaleY );
    1577             :                 }
    1578             : 
    1579           0 :                 nMoveX = aDestPt.X() - aSrcPt.X(), nMoveY = aDestPt.Y() - aSrcPt.Y();
    1580             : 
    1581           0 :                 if( nMoveX || nMoveY )
    1582           0 :                     aTmpMtf.Move( nMoveX, nMoveY );
    1583             : 
    1584           0 :                 const Gradient& rGradient = pA->GetGradient();
    1585           0 :                 sal_uInt32 nLuminance = ((sal_Int32)rGradient.GetStartColor().GetLuminance() + (sal_Int32)rGradient.GetEndColor().GetLuminance() ) >> 1;
    1586             : 
    1587           0 :                 sal_uInt8 nOldGlobalTransparency = mnGlobalTransparency;
    1588           0 :                 mnGlobalTransparency = (sal_uInt8)MinMax( nLuminance, 0, 0xff );
    1589             : 
    1590           0 :                 mpVDev->Push();
    1591           0 :                 Impl_writeActions( aTmpMtf );
    1592           0 :                 mpVDev->Pop();
    1593             : 
    1594           0 :                 mnGlobalTransparency = nOldGlobalTransparency;
    1595             :             }
    1596           0 :             break;
    1597             : 
    1598             :             case( MetaActionType::EPS ):
    1599             :             {
    1600           0 :                 const MetaEPSAction*    pA = static_cast<const MetaEPSAction*>(pAction);
    1601           0 :                 const GDIMetaFile       aGDIMetaFile( pA->GetSubstitute() );
    1602           0 :                 bool                bFound = false;
    1603             : 
    1604           0 :                 for( size_t j = 0, nC = aGDIMetaFile.GetActionSize(); ( j < nC ) && !bFound; j++ )
    1605             :                 {
    1606           0 :                     const MetaAction* pSubstAct = aGDIMetaFile.GetAction( j );
    1607             : 
    1608           0 :                     if( pSubstAct->GetType() == MetaActionType::BMPSCALE )
    1609             :                     {
    1610           0 :                         bFound = true;
    1611           0 :                         const MetaBmpScaleAction* pBmpScaleAction = static_cast<const MetaBmpScaleAction*>(pSubstAct);
    1612           0 :                         Impl_writeImage( pBmpScaleAction->GetBitmap(),
    1613           0 :                                       pA->GetPoint(), pA->GetSize(),
    1614           0 :                                       Point(), pBmpScaleAction->GetBitmap().GetSizePixel(), clipRect, 1 == bMap  );
    1615             :                     }
    1616           0 :                 }
    1617             :             }
    1618           0 :             break;
    1619             : 
    1620             :             case( MetaActionType::COMMENT ):
    1621             :             {
    1622           0 :                 const MetaCommentAction*    pA = static_cast<const MetaCommentAction*>(pAction);
    1623           0 :                 const sal_uInt8*                pData = pA->GetData();
    1624             : 
    1625           0 :                 if( pA->GetComment().equalsIgnoreAsciiCase("XGRAD_SEQ_BEGIN") )
    1626             :                 {
    1627           0 :                     const MetaGradientExAction* pGradAction = NULL;
    1628           0 :                     bool                    bDone = false;
    1629             : 
    1630           0 :                     while( !bDone && ( ++i < nCount ) )
    1631             :                     {
    1632           0 :                         pAction = rMtf.GetAction( i );
    1633             : 
    1634           0 :                         if( pAction->GetType() == MetaActionType::GRADIENTEX )
    1635           0 :                             pGradAction = static_cast<const MetaGradientExAction*>(pAction);
    1636           0 :                         else if( ( pAction->GetType() == MetaActionType::COMMENT ) &&
    1637           0 :                                  ( static_cast<const MetaCommentAction*>(pAction)->GetComment().equalsIgnoreAsciiCase("XGRAD_SEQ_END") ) )
    1638             :                         {
    1639           0 :                             bDone = true;
    1640             :                         }
    1641             :                     }
    1642             : 
    1643           0 :                     if( pGradAction )
    1644           0 :                         Impl_writeGradientEx( pGradAction->GetPolyPolygon(), pGradAction->GetGradient());
    1645             :                 }
    1646           0 :                 else if( pA->GetComment().equalsIgnoreAsciiCase("XPATHFILL_SEQ_BEGIN") &&  pData )
    1647             :                 {
    1648             : 
    1649             :                     // this comment encapsulates all high level information for a filling that caused
    1650             :                     // the meta actions between the "XPATHFILL_SEQ_BEGIN" and "XPATHFILL_SEQ_END" comment.
    1651             : 
    1652           0 :                     SvtGraphicFill  aFilling;
    1653           0 :                     SvMemoryStream  aMemStm( const_cast<sal_uInt8 *>(pData), pA->GetDataSize(), StreamMode::READ );
    1654             : 
    1655             :                     // read the fill info
    1656           0 :                     ReadSvtGraphicFill( aMemStm, aFilling );
    1657             : 
    1658             :                     // if impl_writeFilling can handle this high level filling, it returns true and we
    1659             :                     // skip all meta actions until "XPATHFILL_SEQ_END"
    1660           0 :                     if( Impl_writeFilling( aFilling ) )
    1661             :                     {
    1662           0 :                         bool bDone = false;
    1663             : 
    1664           0 :                         while( !bDone && ( ++i < nCount ) )
    1665             :                         {
    1666           0 :                             pAction = rMtf.GetAction( i );
    1667             : 
    1668           0 :                             if( ( pAction->GetType() == MetaActionType::COMMENT ) &&
    1669           0 :                                      ( static_cast<const MetaCommentAction*>(pAction)->GetComment().equalsIgnoreAsciiCase("XPATHFILL_SEQ_END") ) )
    1670             :                             {
    1671           0 :                                 bDone = true;
    1672             :                             }
    1673             :                         }
    1674           0 :                     }
    1675             :                 }
    1676           0 :                 else if( pA->GetComment().equalsIgnoreAsciiCase("XPATHSTROKE_SEQ_BEGIN") && pData )
    1677             :                 {
    1678             : 
    1679             :                     // this comment encapsulates all high level information for a filling that caused
    1680             :                     // the meta actions between the "XPATHFILL_SEQ_BEGIN" and "XPATHFILL_SEQ_END" comment.
    1681             : 
    1682           0 :                     SvtGraphicStroke aStroke;
    1683           0 :                     SvMemoryStream  aMemStm( const_cast<sal_uInt8 *>(pData), pA->GetDataSize(), StreamMode::READ );
    1684             : 
    1685             :                     // read the fill info
    1686           0 :                     ReadSvtGraphicStroke( aMemStm, aStroke );
    1687             : 
    1688             :                     // if impl_writeStroke can handle this high level stroke, it returns true and we
    1689             :                     // skip all meta actions until "XPATHSTROKE_SEQ_END"
    1690           0 :                     if( Impl_writeStroke( aStroke ) )
    1691             :                     {
    1692           0 :                         bool bDone = false;
    1693             : 
    1694           0 :                         while( !bDone && ( ++i < nCount ) )
    1695             :                         {
    1696           0 :                             pAction = rMtf.GetAction( i );
    1697             : 
    1698           0 :                             if( ( pAction->GetType() == MetaActionType::COMMENT ) &&
    1699           0 :                                      ( static_cast<const MetaCommentAction*>(pAction)->GetComment().equalsIgnoreAsciiCase("XPATHSTROKE_SEQ_END") ) )
    1700             :                             {
    1701           0 :                                 bDone = true;
    1702             :                             }
    1703             :                         }
    1704           0 :                     }
    1705             :                 }
    1706             :             }
    1707           0 :             break;
    1708             : 
    1709             :             case( MetaActionType::BMPSCALE ):
    1710             :             {
    1711           0 :                 const MetaBmpScaleAction* pA = static_cast<const MetaBmpScaleAction*>(pAction);
    1712             : 
    1713           0 :                 Impl_writeImage( pA->GetBitmap(),
    1714           0 :                           pA->GetPoint(), pA->GetSize(),
    1715           0 :                           Point(), pA->GetBitmap().GetSizePixel(), clipRect, 1 == bMap );
    1716             :             }
    1717           0 :             break;
    1718             : 
    1719             :             case( MetaActionType::BMP ):
    1720             :             {
    1721           0 :                 const MetaBmpAction* pA = static_cast<const MetaBmpAction*>(pAction);
    1722           0 :                 Impl_writeImage( pA->GetBitmap(),
    1723           0 :                           pA->GetPoint(), mpVDev->PixelToLogic( pA->GetBitmap().GetSizePixel()),
    1724           0 :                           Point(), pA->GetBitmap().GetSizePixel(), clipRect, 1 ==bMap );
    1725             :             }
    1726           0 :             break;
    1727             : 
    1728             :             case( MetaActionType::BMPSCALEPART ):
    1729             :             {
    1730           0 :                 const MetaBmpScalePartAction* pA = static_cast<const MetaBmpScalePartAction*>(pAction);
    1731           0 :                 Impl_writeImage( pA->GetBitmap(),
    1732           0 :                           pA->GetDestPoint(), pA->GetDestSize(),
    1733           0 :                           pA->GetSrcPoint(), pA->GetSrcSize(), clipRect, 1 == bMap );
    1734             :             }
    1735           0 :             break;
    1736             : 
    1737             :             case( MetaActionType::BMPEX ):
    1738             :             {
    1739           0 :                 const MetaBmpExAction*  pA = static_cast<const MetaBmpExAction*>(pAction);
    1740           0 :                 Impl_writeImage( pA->GetBitmapEx(),
    1741           0 :                           pA->GetPoint(), mpVDev->PixelToLogic( pA->GetBitmapEx().GetSizePixel() ),
    1742           0 :                           Point(), pA->GetBitmapEx().GetSizePixel(), clipRect, 1 == bMap );
    1743             :             }
    1744           0 :             break;
    1745             : 
    1746             :             case( MetaActionType::BMPEXSCALE ):
    1747             :             {
    1748           0 :                 const MetaBmpExScaleAction* pA = static_cast<const MetaBmpExScaleAction*>(pAction);
    1749           0 :                 Impl_writeImage( pA->GetBitmapEx(),
    1750           0 :                           pA->GetPoint(), pA->GetSize(),
    1751           0 :                           Point(), pA->GetBitmapEx().GetSizePixel(), clipRect, 1 == bMap );
    1752             :             }
    1753           0 :             break;
    1754             : 
    1755             :             case( MetaActionType::BMPEXSCALEPART ):
    1756             :             {
    1757           0 :                 const MetaBmpExScalePartAction* pA = static_cast<const MetaBmpExScalePartAction*>(pAction);
    1758           0 :                 Impl_writeImage( pA->GetBitmapEx(),
    1759           0 :                           pA->GetDestPoint(), pA->GetDestSize(),
    1760           0 :                           pA->GetSrcPoint(), pA->GetSrcSize(), clipRect, 1 == bMap );
    1761             :             }
    1762           0 :             break;
    1763             : 
    1764             :             case( MetaActionType::TEXT ):
    1765             :             {
    1766           0 :                 const MetaTextAction* pA = static_cast<const MetaTextAction*>(pAction);
    1767           0 :                 Impl_writeText( pA->GetPoint(),  pA->GetText().copy( pA->GetIndex(), pA->GetLen() ), NULL, 0);
    1768             :             }
    1769           0 :             break;
    1770             : 
    1771             :             case( MetaActionType::TEXTRECT ):
    1772             :             {
    1773           0 :                 const MetaTextRectAction* pA = static_cast<const MetaTextRectAction*>(pAction);
    1774           0 :                 Impl_writeText( pA->GetRect().TopLeft(), pA->GetText(), NULL, 0  );
    1775             :             }
    1776           0 :             break;
    1777             : 
    1778             :             case( MetaActionType::TEXTARRAY ):
    1779             :             {
    1780           0 :                 const MetaTextArrayAction*  pA = static_cast<const MetaTextArrayAction*>(pAction);
    1781           0 :                 Impl_writeText( pA->GetPoint(), pA->GetText().copy( pA->GetIndex(), pA->GetLen() ), pA->GetDXArray(), 0 );
    1782             :             }
    1783           0 :             break;
    1784             : 
    1785             :             case( MetaActionType::STRETCHTEXT ):
    1786             :             {
    1787           0 :                 const MetaStretchTextAction* pA = static_cast<const MetaStretchTextAction*>(pAction);
    1788           0 :                 Impl_writeText( pA->GetPoint(), pA->GetText().copy( pA->GetIndex(), pA->GetLen() ), NULL, pA->GetWidth() );
    1789             :             }
    1790           0 :             break;
    1791             : 
    1792             :             case( MetaActionType::ISECTRECTCLIPREGION ):
    1793             :             {
    1794           0 :                 const MetaISectRectClipRegionAction* pA = static_cast<const MetaISectRectClipRegionAction*>(pAction);
    1795           0 :                 clipRect = pA->GetRect();
    1796             :             }
    1797             :             // fall-through
    1798             :             case( MetaActionType::CLIPREGION ):
    1799             :             case( MetaActionType::ISECTREGIONCLIPREGION ):
    1800             :             case( MetaActionType::MOVECLIPREGION ):
    1801             :             {
    1802           0 :                 const_cast<MetaAction*>(pAction)->Execute( mpVDev );
    1803             :             }
    1804           0 :             break;
    1805             : 
    1806             :             case( MetaActionType::MAPMODE ):
    1807             :             {
    1808           0 :                 bMap++;
    1809             :             }
    1810             :             // fall-through
    1811             :             case( MetaActionType::REFPOINT ):
    1812             :             case( MetaActionType::LINECOLOR ):
    1813             :             case( MetaActionType::FILLCOLOR ):
    1814             :             case( MetaActionType::TEXTLINECOLOR ):
    1815             :             case( MetaActionType::TEXTFILLCOLOR ):
    1816             :             case( MetaActionType::TEXTCOLOR ):
    1817             :             case( MetaActionType::TEXTALIGN ):
    1818             :             case( MetaActionType::FONT ):
    1819             :             case( MetaActionType::PUSH ):
    1820             :             case( MetaActionType::POP ):
    1821             :             case( MetaActionType::LAYOUTMODE ):
    1822             :             {
    1823           0 :                 const_cast<MetaAction*>(pAction)->Execute( mpVDev );
    1824             :             }
    1825           0 :             break;
    1826             : 
    1827             :             case( MetaActionType::RASTEROP ):
    1828             :             case( MetaActionType::MASK ):
    1829             :             case( MetaActionType::MASKSCALE ):
    1830             :             case( MetaActionType::MASKSCALEPART ):
    1831             :             case( MetaActionType::WALLPAPER ):
    1832             :             case( MetaActionType::TEXTLINE ):
    1833             :             {
    1834             :                 // !!! >>> we don't want to support these actions
    1835             :             }
    1836           0 :             break;
    1837             : 
    1838             :             default:
    1839           0 :             break;
    1840             :         }
    1841             :     }
    1842           0 : }
    1843             : 
    1844             : 
    1845             : 
    1846             : 
    1847             : 
    1848           0 : void Writer::Impl_addStraightLine( BitStream& rBits, Point& rLastPoint,
    1849             :                                    const double P2x, const double P2y )
    1850             : {
    1851           0 :     Point aPoint( FRound(P2x), FRound(P2y) );
    1852             : 
    1853           0 :     Impl_addStraightEdgeRecord( rBits, _Int16(aPoint.X() - rLastPoint.X()),_Int16(aPoint.Y() - rLastPoint.Y()));
    1854           0 :     rLastPoint = aPoint;
    1855             : 
    1856           0 : }
    1857             : 
    1858             : 
    1859             : 
    1860           0 : void Writer::Impl_addQuadBezier( BitStream& rBits, Point& rLastPoint,
    1861             :                                  const double P2x, const double P2y,
    1862             :                                  const double P3x, const double P3y )
    1863             : {
    1864             : 
    1865           0 :     Point aControlPoint( FRound(P2x), FRound(P2y) );
    1866           0 :     Point aAnchorPoint( FRound(P3x), FRound(P3y) );
    1867             : 
    1868             :     Impl_addCurvedEdgeRecord( rBits,
    1869           0 :                                 _Int16(aControlPoint.X() - rLastPoint.X()),_Int16(aControlPoint.Y() - rLastPoint.Y()),
    1870           0 :                                 _Int16(aAnchorPoint.X() - aControlPoint.X()),_Int16(aAnchorPoint.Y() - aControlPoint.Y()) );
    1871           0 :     rLastPoint = aAnchorPoint;
    1872           0 : }
    1873             : 
    1874             : 
    1875             : 
    1876             : /* Approximate given cubic bezier curve by quadratic bezier segments */
    1877           0 : void Writer::Impl_quadBezierApprox( BitStream& rBits,
    1878             :                                    Point& rLastPoint,
    1879             :                                    const double d2,
    1880             :                                    const double P1x, const double P1y,
    1881             :                                    const double P2x, const double P2y,
    1882             :                                    const double P3x, const double P3y,
    1883             :                                    const double P4x, const double P4y )
    1884             : {
    1885             :     // Check for degenerate case, where the given cubic bezier curve
    1886             :     // is already quadratic: P4 == 3P3 - 3P2 + P1
    1887           0 :     if( P4x == 3.0*P3x - 3.0*P2x + P1x &&
    1888           0 :         P4y == 3.0*P3y - 3.0*P2y + P1y )
    1889             :     {
    1890             :         Impl_addQuadBezier( rBits, rLastPoint,
    1891           0 :                            3.0/2.0*P2x - 1.0/2.0*P1x, 3.0/2.0*P2y - 1.0/2.0*P1y,
    1892           0 :                            P4x, P4y);
    1893             :     }
    1894             :     else
    1895             :     {
    1896             :         // Create quadratic segment for given cubic:
    1897             :         // Start and end point must coincide, determine quadratic control
    1898             :         // point in such a way that it lies on the intersection of the
    1899             :         // tangents at start and end point, resp. Thus, both cubic and
    1900             :         // quadratic curve segments will match in 0th and 1st derivative
    1901             :         // at the start and end points
    1902             : 
    1903             :         // Intersection of P2P1 and P4P3
    1904             :         //           (P2y-P4y)(P3x-P4x)-(P2x-P4x)(P3y-P4y)
    1905             :         //  lambda = -------------------------------------
    1906             :         //           (P1x-P2x)(P3y-P4y)-(P1y-P2y)(P3x-P4x)
    1907             :         //
    1908             :         // Intersection point IP is now
    1909             :         // IP = P2 + lambda(P1-P2)
    1910             :         //
    1911           0 :         const double nominator( (P2y-P4y)*(P3x-P4x) - (P2x-P4x)*(P3y-P4y) );
    1912           0 :         const double denominator( (P1x-P2x)*(P3y-P4y) - (P1y-P2y)*(P3x-P4x) );
    1913           0 :         const double lambda( nominator / denominator );
    1914             : 
    1915           0 :         const double IPx( P2x + lambda*( P1x - P2x) );
    1916           0 :         const double IPy( P2y + lambda*( P1y - P2y) );
    1917             : 
    1918             :         // Introduce some alias names: quadratic start point is P1, end
    1919             :         // point is P4, control point is IP
    1920           0 :         const double QP1x( P1x );
    1921           0 :         const double QP1y( P1y );
    1922           0 :         const double QP2x( IPx );
    1923           0 :         const double QP2y( IPy );
    1924           0 :         const double QP3x( P4x );
    1925           0 :         const double QP3y( P4y );
    1926             : 
    1927             :         // Adapted bezier flatness test (lecture notes from R. Schaback,
    1928             :         // Mathematics of Computer-Aided Design, Uni Goettingen, 2000)
    1929             :         //
    1930             :         // ||C(t) - Q(t)|| <= max     ||c_j - q_j||
    1931             :         //                    0<=j<=n
    1932             :         //
    1933             :         // In this case, we don't need the distance from the cubic bezier
    1934             :         // to a straight line, but to a quadratic bezier. The c_j's are
    1935             :         // the cubic bezier's bernstein coefficients, the q_j's the
    1936             :         // quadratic bezier's. We have the c_j's given, the q_j's can be
    1937             :         // calculated from QPi like this (sorry, mixed index notation, we
    1938             :         // use [1,n], formulas use [0,n-1]):
    1939             :         //
    1940             :         // q_0 = QP1 = P1
    1941             :         // q_1 = 1/3 QP1 + 2/3 QP2
    1942             :         // q_2 = 2/3 QP2 + 1/3 QP3
    1943             :         // q_3 = QP3 = P4
    1944             :         //
    1945             :         // We can drop case 0 and 3, since there the curves coincide
    1946             :         // (distance is zero)
    1947             : 
    1948             :         // calculate argument of max for j=1 and j=2
    1949           0 :         const double fJ1x( P2x - 1.0/3.0*QP1x - 2.0/3.0*QP2x );
    1950           0 :         const double fJ1y( P2y - 1.0/3.0*QP1y - 2.0/3.0*QP2y );
    1951           0 :         const double fJ2x( P3x - 2.0/3.0*QP2x - 1.0/3.0*QP3x );
    1952           0 :         const double fJ2y( P3y - 2.0/3.0*QP2y - 1.0/3.0*QP3y );
    1953             : 
    1954             :         // stop if distance from cubic curve is guaranteed to be bounded by d
    1955             :         // Should denominator be 0: then P1P2 and P3P4 are parallel (P1P2^T R[90,P3P4] = 0.0),
    1956             :         // meaning that either we have a straight line or an inflexion point (see else block below)
    1957           0 :         if( 0.0 != denominator &&
    1958           0 :             ::std::max( fJ1x*fJ1x + fJ1y*fJ1y,
    1959           0 :                         fJ2x*fJ2x + fJ2y*fJ2y) < d2 )
    1960             :         {
    1961             :             // requested resolution reached.
    1962             :             // Add end points to output file.
    1963             :             // order is preserved, since this is so to say depth first traversal.
    1964             :             Impl_addQuadBezier( rBits, rLastPoint,
    1965             :                                 QP2x, QP2y,
    1966           0 :                                 QP3x, QP3y);
    1967             :         }
    1968             :         else
    1969             :         {
    1970             :             // Maybe subdivide further
    1971             : 
    1972             :             // This is for robustness reasons, since the line intersection
    1973             :             // method below gets instable if the curve gets closer to a
    1974             :             // straight line. If the given cubic bezier does not deviate by
    1975             :             // more than d/4 from a straight line, either:
    1976             :             //  - take the line (that's what we do here)
    1977             :             //  - express the line by a quadratic bezier
    1978             : 
    1979             :             // Perform bezier flatness test (lecture notes from R. Schaback,
    1980             :             // Mathematics of Computer-Aided Design, Uni Goettingen, 2000)
    1981             :             //
    1982             :             // ||P(t) - L(t)|| <= max     ||b_j - b_0 - j/n(b_n - b_0)||
    1983             :             //                    0<=j<=n
    1984             :             //
    1985             :             // What is calculated here is an upper bound to the distance from
    1986             :             // a line through b_0 and b_3 (P1 and P4 in our notation) and the
    1987             :             // curve. We can drop 0 and n from the running indices, since the
    1988             :             // argument of max becomes zero for those cases.
    1989           0 :             const double fJ1x2( P2x - P1x - 1.0/3.0*(P4x - P1x) );
    1990           0 :             const double fJ1y2( P2y - P1y - 1.0/3.0*(P4y - P1y) );
    1991           0 :             const double fJ2x2( P3x - P1x - 2.0/3.0*(P4x - P1x) );
    1992           0 :             const double fJ2y2( P3y - P1y - 2.0/3.0*(P4y - P1y) );
    1993             : 
    1994             :             // stop if distance from line is guaranteed to be bounded by d/4
    1995           0 :             if( ::std::max( fJ1x2*fJ1x2 + fJ1y2*fJ1y2,
    1996           0 :                             fJ2x2*fJ2x2 + fJ2y2*fJ2y2) < d2/16.0 )
    1997             :             {
    1998             :                 // do not subdivide further, add straight line instead
    1999           0 :                 Impl_addStraightLine( rBits, rLastPoint, P4x, P4y);
    2000             :             }
    2001             :             else
    2002             :             {
    2003             :                 // deCasteljau bezier arc, split at t=0.5
    2004             :                 // Foley/vanDam, p. 508
    2005           0 :                 const double L1x( P1x ),             L1y( P1y );
    2006           0 :                 const double L2x( (P1x + P2x)*0.5 ), L2y( (P1y + P2y)*0.5 );
    2007           0 :                 const double Hx ( (P2x + P3x)*0.5 ), Hy ( (P2y + P3y)*0.5 );
    2008           0 :                 const double L3x( (L2x + Hx)*0.5 ),  L3y( (L2y + Hy)*0.5 );
    2009           0 :                 const double R4x( P4x ),             R4y( P4y );
    2010           0 :                 const double R3x( (P3x + P4x)*0.5 ), R3y( (P3y + P4y)*0.5 );
    2011           0 :                 const double R2x( (Hx + R3x)*0.5 ),  R2y( (Hy + R3y)*0.5 );
    2012           0 :                 const double R1x( (L3x + R2x)*0.5 ), R1y( (L3y + R2y)*0.5 );
    2013           0 :                 const double L4x( R1x ),             L4y( R1y );
    2014             : 
    2015             :                 // subdivide further
    2016           0 :                 Impl_quadBezierApprox(rBits, rLastPoint, d2, L1x, L1y, L2x, L2y, L3x, L3y, L4x, L4y);
    2017           0 :                 Impl_quadBezierApprox(rBits, rLastPoint, d2, R1x, R1y, R2x, R2y, R3x, R3y, R4x, R4y);
    2018             :             }
    2019             :         }
    2020             :     }
    2021           0 : }
    2022             : 
    2023           0 : Reference < XBreakIterator > Writer::Impl_GetBreakIterator()
    2024             : {
    2025           0 :     if ( !mxBreakIterator.is() )
    2026             :     {
    2027           0 :         Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
    2028           0 :         mxBreakIterator = BreakIterator::create(xContext);
    2029             :     }
    2030           0 :     return mxBreakIterator;
    2031           3 : }
    2032             : 
    2033             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11