LCOV - code coverage report
Current view: top level - canvas/source/vcl - canvashelper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 516 0.0 %
Date: 2015-06-13 12:38:46 Functions: 0 38 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <canvas/debug.hxx>
      21             : #include <tools/diagnose_ex.h>
      22             : 
      23             : #include <rtl/math.hxx>
      24             : 
      25             : #include <com/sun/star/rendering/CompositeOperation.hpp>
      26             : #include <com/sun/star/util/Endianness.hpp>
      27             : #include <com/sun/star/rendering/TextDirection.hpp>
      28             : #include <com/sun/star/rendering/TexturingMode.hpp>
      29             : #include <com/sun/star/rendering/PathCapType.hpp>
      30             : #include <com/sun/star/rendering/PathJoinType.hpp>
      31             : #include <com/sun/star/drawing/LineCap.hpp>
      32             : 
      33             : #include <tools/poly.hxx>
      34             : #include <vcl/window.hxx>
      35             : #include <vcl/bitmapex.hxx>
      36             : #include <vcl/bmpacc.hxx>
      37             : #include <vcl/canvastools.hxx>
      38             : 
      39             : #include <basegfx/matrix/b2dhommatrix.hxx>
      40             : #include <basegfx/range/b2drectangle.hxx>
      41             : #include <basegfx/point/b2dpoint.hxx>
      42             : #include <basegfx/vector/b2dsize.hxx>
      43             : #include <basegfx/polygon/b2dpolygon.hxx>
      44             : #include <basegfx/polygon/b2dpolygontools.hxx>
      45             : #include <basegfx/polygon/b2dpolypolygontools.hxx>
      46             : #include <basegfx/polygon/b2dlinegeometry.hxx>
      47             : #include <basegfx/tools/canvastools.hxx>
      48             : #include <basegfx/numeric/ftools.hxx>
      49             : 
      50             : #include <utility>
      51             : 
      52             : #include <comphelper/sequence.hxx>
      53             : #include <canvas/canvastools.hxx>
      54             : 
      55             : #include "textlayout.hxx"
      56             : #include "canvashelper.hxx"
      57             : #include "canvasbitmap.hxx"
      58             : #include "impltools.hxx"
      59             : #include "canvasfont.hxx"
      60             : 
      61             : 
      62             : using namespace ::com::sun::star;
      63             : 
      64             : namespace vclcanvas
      65             : {
      66             :     namespace
      67             :     {
      68           0 :         basegfx::B2DLineJoin b2DJoineFromJoin( sal_Int8 nJoinType )
      69             :         {
      70           0 :             switch( nJoinType )
      71             :             {
      72             :                 case rendering::PathJoinType::NONE:
      73           0 :                     return basegfx::B2DLineJoin::NONE;
      74             : 
      75             :                 case rendering::PathJoinType::MITER:
      76           0 :                     return basegfx::B2DLineJoin::Miter;
      77             : 
      78             :                 case rendering::PathJoinType::ROUND:
      79           0 :                     return basegfx::B2DLineJoin::Round;
      80             : 
      81             :                 case rendering::PathJoinType::BEVEL:
      82           0 :                     return basegfx::B2DLineJoin::Bevel;
      83             : 
      84             :                 default:
      85           0 :                     ENSURE_OR_THROW( false,
      86             :                                       "b2DJoineFromJoin(): Unexpected join type" );
      87             :             }
      88             : 
      89             :             return basegfx::B2DLineJoin::NONE;
      90             :         }
      91             : 
      92           0 :         drawing::LineCap unoCapeFromCap( sal_Int8 nCapType)
      93             :         {
      94           0 :             switch ( nCapType)
      95             :             {
      96             :                 case rendering::PathCapType::BUTT:
      97           0 :                     return drawing::LineCap_BUTT;
      98             : 
      99             :                 case rendering::PathCapType::ROUND:
     100           0 :                     return drawing::LineCap_ROUND;
     101             : 
     102             :                 case rendering::PathCapType::SQUARE:
     103           0 :                     return drawing::LineCap_SQUARE;
     104             : 
     105             :                 default:
     106           0 :                     ENSURE_OR_THROW( false,
     107             :                                       "unoCapeFromCap(): Unexpected cap type" );
     108             :             }
     109             :             return drawing::LineCap_BUTT;
     110             :         }
     111             :     }
     112             : 
     113           0 :     CanvasHelper::CanvasHelper() :
     114             :         mpDevice(),
     115             :         mpProtectedOutDev(),
     116             :         mpOutDev(),
     117             :         mp2ndOutDev(),
     118           0 :         mbHaveAlpha( false )
     119             :     {
     120           0 :     }
     121             : 
     122           0 :     void CanvasHelper::disposing()
     123             :     {
     124           0 :         mpDevice = NULL;
     125           0 :         mpProtectedOutDev.reset();
     126           0 :         mpOutDev.reset();
     127           0 :         mp2ndOutDev.reset();
     128           0 :     }
     129             : 
     130           0 :     void CanvasHelper::init( rendering::XGraphicDevice&     rDevice,
     131             :                              const OutDevProviderSharedPtr& rOutDev,
     132             :                              bool                           bProtect,
     133             :                              bool                           bHaveAlpha )
     134             :     {
     135             :         // cast away const, need to change refcount (as this is
     136             :         // ~invisible to client code, still logically const)
     137           0 :         mpDevice    = &rDevice;
     138           0 :         mbHaveAlpha = bHaveAlpha;
     139             : 
     140           0 :         setOutDev( rOutDev, bProtect );
     141           0 :     }
     142             : 
     143           0 :     void CanvasHelper::setOutDev( const OutDevProviderSharedPtr& rOutDev,
     144             :                                   bool                           bProtect )
     145             :     {
     146           0 :         if( bProtect )
     147           0 :             mpProtectedOutDev = rOutDev;
     148             :         else
     149           0 :             mpProtectedOutDev.reset();
     150             : 
     151           0 :         mpOutDev = rOutDev;
     152           0 :     }
     153             : 
     154           0 :     void CanvasHelper::setBackgroundOutDev( const OutDevProviderSharedPtr& rOutDev )
     155             :     {
     156           0 :         mp2ndOutDev = rOutDev;
     157           0 :         mp2ndOutDev->getOutDev().EnableMapMode( false );
     158           0 :         mp2ndOutDev->getOutDev().SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
     159           0 :     }
     160             : 
     161           0 :     void CanvasHelper::clear()
     162             :     {
     163             :         // are we disposed?
     164           0 :         if( mpOutDev )
     165             :         {
     166           0 :             OutputDevice& rOutDev( mpOutDev->getOutDev() );
     167           0 :             tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     168             : 
     169           0 :             rOutDev.EnableMapMode( false );
     170           0 :             rOutDev.SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
     171           0 :             rOutDev.SetLineColor( COL_WHITE );
     172           0 :             rOutDev.SetFillColor( COL_WHITE );
     173           0 :             rOutDev.SetClipRegion();
     174             :             rOutDev.DrawRect( Rectangle( Point(),
     175           0 :                                          rOutDev.GetOutputSizePixel()) );
     176             : 
     177           0 :             if( mp2ndOutDev )
     178             :             {
     179           0 :                 OutputDevice& rOutDev2( mp2ndOutDev->getOutDev() );
     180             : 
     181           0 :                 rOutDev2.SetDrawMode( DrawModeFlags::Default );
     182           0 :                 rOutDev2.EnableMapMode( false );
     183           0 :                 rOutDev2.SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
     184           0 :                 rOutDev2.SetLineColor( COL_WHITE );
     185           0 :                 rOutDev2.SetFillColor( COL_WHITE );
     186           0 :                 rOutDev2.SetClipRegion();
     187             :                 rOutDev2.DrawRect( Rectangle( Point(),
     188           0 :                                               rOutDev2.GetOutputSizePixel()) );
     189             :                 rOutDev2.SetDrawMode( DrawModeFlags::BlackLine | DrawModeFlags::BlackFill | DrawModeFlags::BlackText |
     190           0 :                                       DrawModeFlags::BlackGradient | DrawModeFlags::BlackBitmap );
     191           0 :             }
     192             :         }
     193           0 :     }
     194             : 
     195           0 :     void CanvasHelper::drawPoint( const rendering::XCanvas*     ,
     196             :                                   const geometry::RealPoint2D&  aPoint,
     197             :                                   const rendering::ViewState&   viewState,
     198             :                                   const rendering::RenderState& renderState )
     199             :     {
     200             :         // are we disposed?
     201           0 :         if( mpOutDev )
     202             :         {
     203             :             // nope, render
     204           0 :             tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     205           0 :             setupOutDevState( viewState, renderState, LINE_COLOR );
     206             : 
     207             :             const Point aOutPoint( tools::mapRealPoint2D( aPoint,
     208           0 :                                                           viewState, renderState ) );
     209             :             // TODO(F1): alpha
     210           0 :             mpOutDev->getOutDev().DrawPixel( aOutPoint );
     211             : 
     212           0 :             if( mp2ndOutDev )
     213           0 :                 mp2ndOutDev->getOutDev().DrawPixel( aOutPoint );
     214             :         }
     215           0 :     }
     216             : 
     217           0 :     void CanvasHelper::drawLine( const rendering::XCanvas*      ,
     218             :                                  const geometry::RealPoint2D&   aStartRealPoint2D,
     219             :                                  const geometry::RealPoint2D&   aEndRealPoint2D,
     220             :                                  const rendering::ViewState&    viewState,
     221             :                                  const rendering::RenderState&  renderState )
     222             :     {
     223             :         // are we disposed?
     224           0 :         if( mpOutDev )
     225             :         {
     226             :             // nope, render
     227           0 :             tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     228           0 :             setupOutDevState( viewState, renderState, LINE_COLOR );
     229             : 
     230             :             const Point aStartPoint( tools::mapRealPoint2D( aStartRealPoint2D,
     231           0 :                                                             viewState, renderState ) );
     232             :             const Point aEndPoint( tools::mapRealPoint2D( aEndRealPoint2D,
     233           0 :                                                           viewState, renderState ) );
     234             :             // TODO(F2): alpha
     235           0 :             mpOutDev->getOutDev().DrawLine( aStartPoint, aEndPoint );
     236             : 
     237           0 :             if( mp2ndOutDev )
     238           0 :                 mp2ndOutDev->getOutDev().DrawLine( aStartPoint, aEndPoint );
     239             :         }
     240           0 :     }
     241             : 
     242           0 :     void CanvasHelper::drawBezier( const rendering::XCanvas*            ,
     243             :                                    const geometry::RealBezierSegment2D& aBezierSegment,
     244             :                                    const geometry::RealPoint2D&         _aEndPoint,
     245             :                                    const rendering::ViewState&          viewState,
     246             :                                    const rendering::RenderState&        renderState )
     247             :     {
     248           0 :         if( mpOutDev )
     249             :         {
     250           0 :             tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     251           0 :             setupOutDevState( viewState, renderState, LINE_COLOR );
     252             : 
     253             :             const Point& rStartPoint( tools::mapRealPoint2D( geometry::RealPoint2D(aBezierSegment.Px,
     254             :                                                                                    aBezierSegment.Py),
     255           0 :                                                             viewState, renderState ) );
     256             :             const Point& rCtrlPoint1( tools::mapRealPoint2D( geometry::RealPoint2D(aBezierSegment.C1x,
     257             :                                                                                    aBezierSegment.C1y),
     258           0 :                                                             viewState, renderState ) );
     259             :             const Point& rCtrlPoint2( tools::mapRealPoint2D( geometry::RealPoint2D(aBezierSegment.C2x,
     260             :                                                                                    aBezierSegment.C2y),
     261           0 :                                                              viewState, renderState ) );
     262             :             const Point& rEndPoint( tools::mapRealPoint2D( _aEndPoint,
     263           0 :                                                            viewState, renderState ) );
     264             : 
     265           0 :             ::Polygon aPoly(4);
     266           0 :             aPoly.SetPoint( rStartPoint, 0 );
     267           0 :             aPoly.SetFlags( 0, POLY_NORMAL );
     268           0 :             aPoly.SetPoint( rCtrlPoint1, 1 );
     269           0 :             aPoly.SetFlags( 1, POLY_CONTROL );
     270           0 :             aPoly.SetPoint( rCtrlPoint2, 2 );
     271           0 :             aPoly.SetFlags( 2, POLY_CONTROL );
     272           0 :             aPoly.SetPoint( rEndPoint, 3 );
     273           0 :             aPoly.SetFlags( 3, POLY_NORMAL );
     274             : 
     275             :             // TODO(F2): alpha
     276           0 :             mpOutDev->getOutDev().DrawPolygon( aPoly );
     277           0 :             if( mp2ndOutDev )
     278           0 :                 mp2ndOutDev->getOutDev().DrawPolygon( aPoly );
     279             :         }
     280           0 :     }
     281             : 
     282           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawPolyPolygon( const rendering::XCanvas*                          ,
     283             :                                                                                  const uno::Reference< rendering::XPolyPolygon2D >& xPolyPolygon,
     284             :                                                                                  const rendering::ViewState&                        viewState,
     285             :                                                                                  const rendering::RenderState&                      renderState )
     286             :     {
     287           0 :         ENSURE_ARG_OR_THROW( xPolyPolygon.is(),
     288             :                          "polygon is NULL");
     289             : 
     290           0 :         if( mpOutDev )
     291             :         {
     292           0 :             tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     293           0 :             setupOutDevState( viewState, renderState, LINE_COLOR );
     294             : 
     295             :             const ::basegfx::B2DPolyPolygon& rPolyPoly(
     296           0 :                 ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(xPolyPolygon) );
     297           0 :             const ::tools::PolyPolygon aPolyPoly( tools::mapPolyPolygon( rPolyPoly, viewState, renderState ) );
     298             : 
     299           0 :             if( rPolyPoly.isClosed() )
     300             :             {
     301           0 :                 mpOutDev->getOutDev().DrawPolyPolygon( aPolyPoly );
     302             : 
     303           0 :                 if( mp2ndOutDev )
     304           0 :                     mp2ndOutDev->getOutDev().DrawPolyPolygon( aPolyPoly );
     305             :             }
     306             :             else
     307             :             {
     308             :                 // mixed open/closed state. Cannot render open polygon
     309             :                 // via DrawPolyPolygon(), since that implicitley
     310             :                 // closed every polygon. OTOH, no need to distinguish
     311             :                 // further and render closed polygons via
     312             :                 // DrawPolygon(), and open ones via DrawPolyLine():
     313             :                 // closed polygons will simply already contain the
     314             :                 // closing segment.
     315           0 :                 sal_uInt16 nSize( aPolyPoly.Count() );
     316             : 
     317           0 :                 for( sal_uInt16 i=0; i<nSize; ++i )
     318             :                 {
     319           0 :                     mpOutDev->getOutDev().DrawPolyLine( aPolyPoly[i] );
     320             : 
     321           0 :                     if( mp2ndOutDev )
     322           0 :                         mp2ndOutDev->getOutDev().DrawPolyLine( aPolyPoly[i] );
     323             :                 }
     324           0 :             }
     325             :         }
     326             : 
     327             :         // TODO(P1): Provide caching here.
     328           0 :         return uno::Reference< rendering::XCachedPrimitive >(NULL);
     329             :     }
     330             : 
     331           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::strokePolyPolygon( const rendering::XCanvas*                            ,
     332             :                                                                                    const uno::Reference< rendering::XPolyPolygon2D >&   xPolyPolygon,
     333             :                                                                                    const rendering::ViewState&                          viewState,
     334             :                                                                                    const rendering::RenderState&                        renderState,
     335             :                                                                                    const rendering::StrokeAttributes&                   strokeAttributes )
     336             :     {
     337           0 :         ENSURE_ARG_OR_THROW( xPolyPolygon.is(),
     338             :                          "polygon is NULL");
     339             : 
     340           0 :         if( mpOutDev )
     341             :         {
     342           0 :             tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     343             : 
     344           0 :             ::basegfx::B2DHomMatrix aMatrix;
     345           0 :             ::canvas::tools::mergeViewAndRenderTransform(aMatrix, viewState, renderState);
     346             : 
     347             :             ::basegfx::B2DSize aLinePixelSize(strokeAttributes.StrokeWidth,
     348           0 :                                               strokeAttributes.StrokeWidth);
     349           0 :             aLinePixelSize *= aMatrix;
     350             : 
     351             :             ::basegfx::B2DPolyPolygon aPolyPoly(
     352           0 :                 ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(xPolyPolygon) );
     353             : 
     354           0 :             if( aPolyPoly.areControlPointsUsed() )
     355             :             {
     356             :                 // AW: Not needed for ApplyLineDashing anymore; should be removed
     357           0 :                 aPolyPoly = ::basegfx::tools::adaptiveSubdivideByAngle(aPolyPoly);
     358             :             }
     359             : 
     360             :             // apply dashing, if any
     361           0 :             if( strokeAttributes.DashArray.getLength() )
     362             :             {
     363             :                 const ::std::vector<double>& aDashArray(
     364           0 :                     ::comphelper::sequenceToContainer< ::std::vector<double> >(strokeAttributes.DashArray) );
     365             : 
     366           0 :                 ::basegfx::B2DPolyPolygon aDashedPolyPoly;
     367             : 
     368           0 :                 for( sal_uInt32 i=0; i<aPolyPoly.count(); ++i )
     369             :                 {
     370             :                     // AW: new interface; You may also get gaps in the same run now
     371           0 :                     basegfx::tools::applyLineDashing(aPolyPoly.getB2DPolygon(i), aDashArray, &aDashedPolyPoly);
     372             :                     //aDashedPolyPoly.append(
     373             :                     //    ::basegfx::tools::applyLineDashing( aPolyPoly.getB2DPolygon(i),
     374             :                     //                                        aDashArray ) );
     375             :                 }
     376             : 
     377           0 :                 aPolyPoly = aDashedPolyPoly;
     378             :             }
     379             : 
     380           0 :             ::basegfx::B2DPolyPolygon aStrokedPolyPoly;
     381           0 :             if( aLinePixelSize.getLength() < 1.42 )
     382             :             {
     383             :                 // line width < 1.0 in device pixel, thus, output as a
     384             :                 // simple hairline poly-polygon
     385           0 :                 setupOutDevState( viewState, renderState, LINE_COLOR );
     386             : 
     387           0 :                 aStrokedPolyPoly = aPolyPoly;
     388             :             }
     389             :             else
     390             :             {
     391             :                 // render as a 'thick' line
     392           0 :                 setupOutDevState( viewState, renderState, FILL_COLOR );
     393             : 
     394           0 :                 for( sal_uInt32 i=0; i<aPolyPoly.count(); ++i )
     395             :                 {
     396             :                     // TODO(F2): Use MiterLimit from StrokeAttributes,
     397             :                     // need to convert it here to angle.
     398             : 
     399             :                     // TODO(F2): Also use Cap settings from
     400             :                     // StrokeAttributes, the
     401             :                     // createAreaGeometryForLineStartEnd() method does not
     402             :                     // seem to fit very well here
     403             : 
     404             :                     // AW: New interface, will create bezier polygons now
     405             :                     aStrokedPolyPoly.append(basegfx::tools::createAreaGeometry(
     406             :                         aPolyPoly.getB2DPolygon(i),
     407             :                         strokeAttributes.StrokeWidth*0.5,
     408             :                         b2DJoineFromJoin(strokeAttributes.JoinType),
     409             :                         unoCapeFromCap(strokeAttributes.StartCapType)
     410           0 :                         ));
     411             :                     //aStrokedPolyPoly.append(
     412             :                     //    ::basegfx::tools::createAreaGeometryForPolygon( aPolyPoly.getB2DPolygon(i),
     413             :                     //                                                    strokeAttributes.StrokeWidth*0.5,
     414             :                     //                                                    b2DJoineFromJoin(strokeAttributes.JoinType) ) );
     415             :                 }
     416             :             }
     417             : 
     418             :             // transform only _now_, all the StrokeAttributes are in
     419             :             // user coordinates.
     420           0 :             aStrokedPolyPoly.transform( aMatrix );
     421             : 
     422           0 :             const ::tools::PolyPolygon aVCLPolyPoly( aStrokedPolyPoly );
     423             : 
     424             :             // TODO(F2): When using alpha here, must handle that via
     425             :             // temporary surface or somesuch.
     426             : 
     427             :             // Note: the generated stroke poly-polygon is NOT free of
     428             :             // self-intersections. Therefore, if we would render it
     429             :             // via OutDev::DrawPolyPolygon(), on/off fill would
     430             :             // generate off areas on those self-intersections.
     431           0 :             sal_uInt16 nSize( aVCLPolyPoly.Count() );
     432             : 
     433           0 :             for( sal_uInt16 i=0; i<nSize; ++i )
     434             :             {
     435           0 :                 if( aStrokedPolyPoly.getB2DPolygon( i ).isClosed() ) {
     436           0 :                     mpOutDev->getOutDev().DrawPolygon( aVCLPolyPoly[i] );
     437           0 :                     if( mp2ndOutDev )
     438           0 :                         mp2ndOutDev->getOutDev().DrawPolygon( aVCLPolyPoly[i] );
     439             :                 } else {
     440           0 :                     const sal_uInt16 nPolySize = aVCLPolyPoly[i].GetSize();
     441           0 :                     if( nPolySize ) {
     442           0 :                         Point rPrevPoint = aVCLPolyPoly[i].GetPoint( 0 );
     443           0 :                         Point rPoint;
     444             : 
     445           0 :                         for( sal_uInt16 j=1; j<nPolySize; j++ ) {
     446           0 :                             rPoint = aVCLPolyPoly[i].GetPoint( j );
     447           0 :                             mpOutDev->getOutDev().DrawLine( rPrevPoint, rPoint );
     448           0 :                             if( mp2ndOutDev )
     449           0 :                                 mp2ndOutDev->getOutDev().DrawLine( rPrevPoint, rPoint );
     450           0 :                             rPrevPoint = rPoint;
     451             :                         }
     452             :                     }
     453             :                 }
     454           0 :             }
     455             :         }
     456             : 
     457             :         // TODO(P1): Provide caching here.
     458           0 :         return uno::Reference< rendering::XCachedPrimitive >(NULL);
     459             :     }
     460             : 
     461           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::strokeTexturedPolyPolygon( const rendering::XCanvas*                            ,
     462             :                                                                                            const uno::Reference< rendering::XPolyPolygon2D >&   ,
     463             :                                                                                            const rendering::ViewState&                          ,
     464             :                                                                                            const rendering::RenderState&                        ,
     465             :                                                                                            const uno::Sequence< rendering::Texture >&           ,
     466             :                                                                                            const rendering::StrokeAttributes&                    )
     467             :     {
     468           0 :         return uno::Reference< rendering::XCachedPrimitive >(NULL);
     469             :     }
     470             : 
     471           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::strokeTextureMappedPolyPolygon( const rendering::XCanvas*                           ,
     472             :                                                                                                 const uno::Reference< rendering::XPolyPolygon2D >&  ,
     473             :                                                                                                 const rendering::ViewState&                         ,
     474             :                                                                                                 const rendering::RenderState&                       ,
     475             :                                                                                                 const uno::Sequence< rendering::Texture >&          ,
     476             :                                                                                                 const uno::Reference< geometry::XMapping2D >&       ,
     477             :                                                                                                 const rendering::StrokeAttributes&                   )
     478             :     {
     479           0 :         return uno::Reference< rendering::XCachedPrimitive >(NULL);
     480             :     }
     481             : 
     482           0 :     uno::Reference< rendering::XPolyPolygon2D >   CanvasHelper::queryStrokeShapes( const rendering::XCanvas*                            ,
     483             :                                                                                    const uno::Reference< rendering::XPolyPolygon2D >&   ,
     484             :                                                                                    const rendering::ViewState&                          ,
     485             :                                                                                    const rendering::RenderState&                        ,
     486             :                                                                                    const rendering::StrokeAttributes&                    )
     487             :     {
     488           0 :         return uno::Reference< rendering::XPolyPolygon2D >(NULL);
     489             :     }
     490             : 
     491           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::fillPolyPolygon( const rendering::XCanvas*                          ,
     492             :                                                                                  const uno::Reference< rendering::XPolyPolygon2D >& xPolyPolygon,
     493             :                                                                                  const rendering::ViewState&                        viewState,
     494             :                                                                                  const rendering::RenderState&                      renderState )
     495             :     {
     496           0 :         ENSURE_ARG_OR_THROW( xPolyPolygon.is(),
     497             :                          "polygon is NULL");
     498             : 
     499           0 :         if( mpOutDev )
     500             :         {
     501           0 :             tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     502             : 
     503           0 :             const int nTransparency( setupOutDevState( viewState, renderState, FILL_COLOR ) );
     504             :             ::basegfx::B2DPolyPolygon aB2DPolyPoly(
     505           0 :                 ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(xPolyPolygon));
     506           0 :             aB2DPolyPoly.setClosed(true); // ensure closed poly, otherwise VCL does not fill
     507             :             const ::tools::PolyPolygon aPolyPoly( tools::mapPolyPolygon(
     508             :                                              aB2DPolyPoly,
     509           0 :                                              viewState, renderState ) );
     510           0 :             const bool bSourceAlpha( renderState.CompositeOperation == rendering::CompositeOperation::SOURCE );
     511           0 :             if( !nTransparency || bSourceAlpha )
     512             :             {
     513           0 :                 mpOutDev->getOutDev().DrawPolyPolygon( aPolyPoly );
     514             :             }
     515             :             else
     516             :             {
     517           0 :                 const int nTransPercent( (nTransparency * 100 + 128) / 255 );  // normal rounding, no truncation here
     518           0 :                 mpOutDev->getOutDev().DrawTransparent( aPolyPoly, (sal_uInt16)nTransPercent );
     519             :             }
     520             : 
     521           0 :             if( mp2ndOutDev )
     522             :             {
     523             :                 // HACK. Normally, CanvasHelper does not care about
     524             :                 // actually what mp2ndOutDev is...  well, here we do &
     525             :                 // assume a 1bpp target - everything beyond 97%
     526             :                 // transparency is fully transparent
     527           0 :                 if( nTransparency < 253 )
     528             :                 {
     529           0 :                     mp2ndOutDev->getOutDev().SetFillColor( COL_BLACK );
     530           0 :                     mp2ndOutDev->getOutDev().DrawPolyPolygon( aPolyPoly );
     531             :                 }
     532           0 :             }
     533             :         }
     534             : 
     535             :         // TODO(P1): Provide caching here.
     536           0 :         return uno::Reference< rendering::XCachedPrimitive >(NULL);
     537             :     }
     538             : 
     539           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::fillTextureMappedPolyPolygon( const rendering::XCanvas*                             ,
     540             :                                                                                               const uno::Reference< rendering::XPolyPolygon2D >&    ,
     541             :                                                                                               const rendering::ViewState&                           ,
     542             :                                                                                               const rendering::RenderState&                         ,
     543             :                                                                                               const uno::Sequence< rendering::Texture >&            ,
     544             :                                                                                               const uno::Reference< geometry::XMapping2D >&              )
     545             :     {
     546           0 :         return uno::Reference< rendering::XCachedPrimitive >(NULL);
     547             :     }
     548             : 
     549           0 :     uno::Reference< rendering::XCanvasFont > CanvasHelper::createFont( const rendering::XCanvas*                        ,
     550             :                                                                        const rendering::FontRequest&                    fontRequest,
     551             :                                                                        const uno::Sequence< beans::PropertyValue >&     extraFontProperties,
     552             :                                                                        const geometry::Matrix2D&                        fontMatrix )
     553             :     {
     554           0 :         if( mpOutDev && mpDevice )
     555             :         {
     556             :             // TODO(F2): font properties and font matrix
     557             :             return uno::Reference< rendering::XCanvasFont >(
     558             :                     new CanvasFont(fontRequest, extraFontProperties, fontMatrix,
     559           0 :                                    *mpDevice, mpOutDev) );
     560             :         }
     561             : 
     562           0 :         return uno::Reference< rendering::XCanvasFont >();
     563             :     }
     564             : 
     565           0 :     uno::Sequence< rendering::FontInfo > CanvasHelper::queryAvailableFonts( const rendering::XCanvas*                       ,
     566             :                                                                             const rendering::FontInfo&                      ,
     567             :                                                                             const uno::Sequence< beans::PropertyValue >&     )
     568             :     {
     569             :         // TODO(F2)
     570           0 :         return uno::Sequence< rendering::FontInfo >();
     571             :     }
     572             : 
     573           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawText( const rendering::XCanvas*                         ,
     574             :                                                                           const rendering::StringContext&                   text,
     575             :                                                                           const uno::Reference< rendering::XCanvasFont >&   xFont,
     576             :                                                                           const rendering::ViewState&                       viewState,
     577             :                                                                           const rendering::RenderState&                     renderState,
     578             :                                                                           sal_Int8                                          textDirection )
     579             :     {
     580           0 :         ENSURE_ARG_OR_THROW( xFont.is(),
     581             :                          "font is NULL");
     582             : 
     583           0 :         if( mpOutDev )
     584             :         {
     585           0 :             tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     586             : 
     587           0 :             ::Point aOutpos;
     588           0 :             if( !setupTextOutput( aOutpos, viewState, renderState, xFont ) )
     589           0 :                 return uno::Reference< rendering::XCachedPrimitive >(NULL); // no output necessary
     590             : 
     591             :             // change text direction and layout mode
     592           0 :             ComplexTextLayoutMode nLayoutMode(TEXT_LAYOUT_DEFAULT);
     593           0 :             switch( textDirection )
     594             :             {
     595             :                 case rendering::TextDirection::WEAK_LEFT_TO_RIGHT:
     596             :                     // FALLTHROUGH intended
     597             :                 case rendering::TextDirection::STRONG_LEFT_TO_RIGHT:
     598           0 :                     nLayoutMode |= TEXT_LAYOUT_BIDI_STRONG;
     599           0 :                     nLayoutMode |= TEXT_LAYOUT_TEXTORIGIN_LEFT;
     600           0 :                     break;
     601             : 
     602             :                 case rendering::TextDirection::WEAK_RIGHT_TO_LEFT:
     603           0 :                     nLayoutMode |= TEXT_LAYOUT_BIDI_RTL;
     604             :                     // FALLTHROUGH intended
     605             :                 case rendering::TextDirection::STRONG_RIGHT_TO_LEFT:
     606           0 :                     nLayoutMode |= TEXT_LAYOUT_BIDI_RTL | TEXT_LAYOUT_BIDI_STRONG;
     607           0 :                     nLayoutMode |= TEXT_LAYOUT_TEXTORIGIN_RIGHT;
     608           0 :                     break;
     609             :             }
     610             : 
     611             :             // TODO(F2): alpha
     612           0 :             mpOutDev->getOutDev().SetLayoutMode( nLayoutMode );
     613           0 :             mpOutDev->getOutDev().DrawText( aOutpos,
     614             :                                             text.Text,
     615           0 :                                             ::canvas::tools::numeric_cast<sal_uInt16>(text.StartPosition),
     616           0 :                                             ::canvas::tools::numeric_cast<sal_uInt16>(text.Length) );
     617             : 
     618           0 :             if( mp2ndOutDev )
     619             :             {
     620           0 :                 mp2ndOutDev->getOutDev().SetLayoutMode( nLayoutMode );
     621           0 :                 mp2ndOutDev->getOutDev().DrawText( aOutpos,
     622             :                                                    text.Text,
     623           0 :                                                    ::canvas::tools::numeric_cast<sal_uInt16>(text.StartPosition),
     624           0 :                                                    ::canvas::tools::numeric_cast<sal_uInt16>(text.Length) );
     625           0 :             }
     626             :         }
     627             : 
     628           0 :         return uno::Reference< rendering::XCachedPrimitive >(NULL);
     629             :     }
     630             : 
     631           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawTextLayout( const rendering::XCanvas*                       ,
     632             :                                                                                 const uno::Reference< rendering::XTextLayout >& xLayoutedText,
     633             :                                                                                 const rendering::ViewState&                     viewState,
     634             :                                                                                 const rendering::RenderState&                   renderState )
     635             :     {
     636           0 :         ENSURE_ARG_OR_THROW( xLayoutedText.is(),
     637             :                          "layout is NULL");
     638             : 
     639           0 :         TextLayout* pTextLayout = dynamic_cast< TextLayout* >( xLayoutedText.get() );
     640             : 
     641           0 :         if( pTextLayout )
     642             :         {
     643           0 :             if( mpOutDev )
     644             :             {
     645           0 :                 tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     646             : 
     647             :                 // TODO(T3): Race condition. We're taking the font
     648             :                 // from xLayoutedText, and then calling draw() at it,
     649             :                 // without exclusive access. Move setupTextOutput(),
     650             :                 // e.g. to impltools?
     651             : 
     652           0 :                 ::Point aOutpos;
     653           0 :                 if( !setupTextOutput( aOutpos, viewState, renderState, xLayoutedText->getFont() ) )
     654           0 :                     return uno::Reference< rendering::XCachedPrimitive >(NULL); // no output necessary
     655             : 
     656             :                 // TODO(F2): What about the offset scalings?
     657             :                 // TODO(F2): alpha
     658           0 :                 pTextLayout->draw( mpOutDev->getOutDev(), aOutpos, viewState, renderState );
     659             : 
     660           0 :                 if( mp2ndOutDev )
     661           0 :                     pTextLayout->draw( mp2ndOutDev->getOutDev(), aOutpos, viewState, renderState );
     662             :             }
     663             :         }
     664             :         else
     665             :         {
     666           0 :             ENSURE_ARG_OR_THROW( false,
     667             :                                  "TextLayout not compatible with this canvas" );
     668             :         }
     669             : 
     670           0 :         return uno::Reference< rendering::XCachedPrimitive >(NULL);
     671             :     }
     672             : 
     673           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::implDrawBitmap( const rendering::XCanvas*                   pCanvas,
     674             :                                                                                 const uno::Reference< rendering::XBitmap >& xBitmap,
     675             :                                                                                 const rendering::ViewState&                 viewState,
     676             :                                                                                 const rendering::RenderState&               renderState,
     677             :                                                                                 bool                                        bModulateColors )
     678             :     {
     679           0 :         ENSURE_ARG_OR_THROW( xBitmap.is(),
     680             :                              "bitmap is NULL");
     681             : 
     682             :         ::canvas::tools::verifyInput( renderState,
     683             :                                       BOOST_CURRENT_FUNCTION,
     684             :                                       mpDevice,
     685             :                                       4,
     686           0 :                                       bModulateColors ? 3 : 0 );
     687             : 
     688           0 :         if( mpOutDev )
     689             :         {
     690           0 :             tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     691           0 :             setupOutDevState( viewState, renderState, IGNORE_COLOR );
     692             : 
     693           0 :             ::basegfx::B2DHomMatrix aMatrix;
     694           0 :             ::canvas::tools::mergeViewAndRenderTransform(aMatrix, viewState, renderState);
     695             : 
     696           0 :             ::basegfx::B2DPoint aOutputPos( 0.0, 0.0 );
     697           0 :             aOutputPos *= aMatrix;
     698             : 
     699           0 :             BitmapEx aBmpEx( tools::bitmapExFromXBitmap(xBitmap) );
     700             : 
     701             :             // TODO(F2): Implement modulation again for other color
     702             :             // channels (currently, works only for alpha). Note: this
     703             :             // is already implemented in transformBitmap()
     704           0 :             if( bModulateColors &&
     705           0 :                 renderState.DeviceColor.getLength() > 3 )
     706             :             {
     707             :                 // optimize away the case where alpha modulation value
     708             :                 // is 1.0 - we then simply switch off modulation at all
     709             :                 bModulateColors = !::rtl::math::approxEqual(
     710           0 :                     renderState.DeviceColor[3], 1.0);
     711             :             }
     712             : 
     713             :             // check whether we can render bitmap as-is: must not
     714             :             // modulate colors, matrix must either be the identity
     715             :             // transform (that's clear), _or_ contain only
     716             :             // translational components.
     717           0 :             if( !bModulateColors &&
     718           0 :                 (aMatrix.isIdentity() ||
     719           0 :                  (::basegfx::fTools::equalZero( aMatrix.get(0,1) ) &&
     720           0 :                   ::basegfx::fTools::equalZero( aMatrix.get(1,0) ) &&
     721           0 :                   ::rtl::math::approxEqual(aMatrix.get(0,0), 1.0) &&
     722           0 :                   ::rtl::math::approxEqual(aMatrix.get(1,1), 1.0)) ) )
     723             :             {
     724             :                 // optimized case: identity matrix, or only
     725             :                 // translational components.
     726           0 :                 mpOutDev->getOutDev().DrawBitmapEx( vcl::unotools::pointFromB2DPoint( aOutputPos ),
     727           0 :                                                     aBmpEx );
     728             : 
     729           0 :                 if( mp2ndOutDev )
     730             :                 {
     731             :                     // HACK. Normally, CanvasHelper does not care about
     732             :                     // actually what mp2ndOutDev is...  well, here we do &
     733             :                     // assume a 1bpp target - everything beyond 97%
     734             :                     // transparency is fully transparent
     735           0 :                     if( aBmpEx.IsAlpha() )
     736             :                     {
     737           0 :                         Bitmap aMask( aBmpEx.GetAlpha().GetBitmap() );
     738           0 :                         aMask.MakeMono( 253 );
     739           0 :                         aBmpEx = BitmapEx( aBmpEx.GetBitmap(), aMask );
     740             :                     }
     741           0 :                     else if( aBmpEx.IsTransparent() )
     742             :                     {
     743           0 :                         aBmpEx = BitmapEx( aBmpEx.GetBitmap(), aBmpEx.GetMask() );
     744             :                     }
     745             : 
     746           0 :                     mp2ndOutDev->getOutDev().DrawBitmapEx( vcl::unotools::pointFromB2DPoint( aOutputPos ),
     747           0 :                                                            aBmpEx );
     748             :                 }
     749             : 
     750             :                 // Returning a cache object is not useful, the XBitmap
     751             :                 // itself serves this purpose
     752           0 :                 return uno::Reference< rendering::XCachedPrimitive >(NULL);
     753             :             }
     754             :             else
     755             :             {
     756             :                 // Matrix contains non-trivial transformation (or
     757             :                 // color modulation is requested), decompose to check
     758             :                 // whether GraphicObject suffices
     759           0 :                 ::basegfx::B2DVector aScale;
     760             :                 double               nRotate;
     761             :                 double               nShearX;
     762           0 :                 aMatrix.decompose( aScale, aOutputPos, nRotate, nShearX );
     763             : 
     764           0 :                 GraphicAttr             aGrfAttr;
     765           0 :                 GraphicObjectSharedPtr  pGrfObj;
     766             : 
     767           0 :                 ::Size aBmpSize( aBmpEx.GetSizePixel() );
     768             : 
     769             :                 // setup alpha modulation
     770           0 :                 if( bModulateColors )
     771             :                 {
     772           0 :                     const double nAlphaModulation( renderState.DeviceColor[3] );
     773             : 
     774             :                     // TODO(F1): Note that the GraphicManager has a
     775             :                     // subtle difference in how it calculates the
     776             :                     // resulting alpha value: it's using the inverse
     777             :                     // alpha values (i.e. 'transparency'), and
     778             :                     // calculates transOrig + transModulate, instead
     779             :                     // of transOrig + transModulate -
     780             :                     // transOrig*transModulate (which would be
     781             :                     // equivalent to the origAlpha*modulateAlpha the
     782             :                     // DX canvas performs)
     783             :                     aGrfAttr.SetTransparency(
     784             :                         static_cast< sal_uInt8 >(
     785           0 :                             ::basegfx::fround( 255.0*( 1.0 - nAlphaModulation ) ) ) );
     786             :                 }
     787             : 
     788           0 :                 if( ::basegfx::fTools::equalZero( nShearX ) )
     789             :                 {
     790             :                     // no shear, GraphicObject is enough (the
     791             :                     // GraphicObject only supports scaling, rotation
     792             :                     // and translation)
     793             : 
     794             :                     // #i75339# don't apply mirror flags, having
     795             :                     // negative size values is enough to make
     796             :                     // GraphicObject flip the bitmap
     797             : 
     798             :                     // The angle has to be mapped from radian to tenths of
     799             :                     // degress with the orientation reversed: [0,2Pi) ->
     800             :                     // (3600,0].  Note that the original angle may have
     801             :                     // values outside the [0,2Pi) interval.
     802           0 :                     const double nAngleInTenthOfDegrees (3600.0 - nRotate * 3600.0 / (2*M_PI));
     803           0 :                     aGrfAttr.SetRotation( static_cast< sal_uInt16 >(::basegfx::fround(nAngleInTenthOfDegrees)) );
     804             : 
     805           0 :                     pGrfObj.reset( new GraphicObject( aBmpEx ) );
     806             :                 }
     807             :                 else
     808             :                 {
     809             :                     // modify output position, to account for the fact
     810             :                     // that transformBitmap() always normalizes its output
     811             :                     // bitmap into the smallest enclosing box.
     812           0 :                     ::basegfx::B2DRectangle aDestRect;
     813             :                     ::canvas::tools::calcTransformedRectBounds( aDestRect,
     814             :                                                                 ::basegfx::B2DRectangle(0,
     815             :                                                                                         0,
     816           0 :                                                                                         aBmpSize.Width(),
     817           0 :                                                                                         aBmpSize.Height()),
     818           0 :                                                                 aMatrix );
     819             : 
     820           0 :                     aOutputPos.setX( aDestRect.getMinX() );
     821           0 :                     aOutputPos.setY( aDestRect.getMinY() );
     822             : 
     823             :                     // complex transformation, use generic affine bitmap
     824             :                     // transformation
     825           0 :                     aBmpEx = tools::transformBitmap( aBmpEx,
     826             :                                                      aMatrix,
     827             :                                                      renderState.DeviceColor,
     828           0 :                                                      tools::MODULATE_NONE );
     829             : 
     830           0 :                     pGrfObj.reset( new GraphicObject( aBmpEx ) );
     831             : 
     832             :                     // clear scale values, generated bitmap already
     833             :                     // contains scaling
     834           0 :                     aScale.setX( 1.0 ); aScale.setY( 1.0 );
     835             : 
     836             :                     // update bitmap size, bitmap has changed above.
     837           0 :                     aBmpSize = aBmpEx.GetSizePixel();
     838             :                 }
     839             : 
     840             :                 // output GraphicObject
     841           0 :                 const ::Point aPt( vcl::unotools::pointFromB2DPoint( aOutputPos ) );
     842           0 :                 const ::Size  aSz( ::basegfx::fround( aScale.getX() * aBmpSize.Width() ),
     843           0 :                                    ::basegfx::fround( aScale.getY() * aBmpSize.Height() ) );
     844             : 
     845           0 :                 pGrfObj->Draw( &mpOutDev->getOutDev(),
     846             :                                aPt,
     847             :                                aSz,
     848           0 :                                &aGrfAttr );
     849             : 
     850           0 :                 if( mp2ndOutDev )
     851           0 :                     pGrfObj->Draw( &mp2ndOutDev->getOutDev(),
     852             :                                    aPt,
     853             :                                    aSz,
     854           0 :                                    &aGrfAttr );
     855             : 
     856             :                 // created GraphicObject, which possibly cached
     857             :                 // display bitmap - return cache object, to retain
     858             :                 // that information.
     859             :                 return uno::Reference< rendering::XCachedPrimitive >(
     860             :                     new CachedBitmap( pGrfObj,
     861             :                                       aPt,
     862             :                                       aSz,
     863             :                                       aGrfAttr,
     864             :                                       viewState,
     865             :                                       renderState,
     866             :                                       // cast away const, need to
     867             :                                       // change refcount (as this is
     868             :                                       // ~invisible to client code,
     869             :                                       // still logically const)
     870           0 :                                       const_cast< rendering::XCanvas* >(pCanvas)) );
     871           0 :             }
     872             :         }
     873             : 
     874             :         // Nothing rendered
     875           0 :         return uno::Reference< rendering::XCachedPrimitive >(NULL);
     876             :     }
     877             : 
     878           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawBitmap( const rendering::XCanvas*                   pCanvas,
     879             :                                                                             const uno::Reference< rendering::XBitmap >& xBitmap,
     880             :                                                                             const rendering::ViewState&                 viewState,
     881             :                                                                             const rendering::RenderState&               renderState )
     882             :     {
     883             :         return implDrawBitmap( pCanvas,
     884             :                                xBitmap,
     885             :                                viewState,
     886             :                                renderState,
     887           0 :                                false );
     888             :     }
     889             : 
     890           0 :     uno::Reference< rendering::XCachedPrimitive > CanvasHelper::drawBitmapModulated( const rendering::XCanvas*                      pCanvas,
     891             :                                                                                      const uno::Reference< rendering::XBitmap >&    xBitmap,
     892             :                                                                                      const rendering::ViewState&                    viewState,
     893             :                                                                                      const rendering::RenderState&                  renderState )
     894             :     {
     895             :         return implDrawBitmap( pCanvas,
     896             :                                xBitmap,
     897             :                                viewState,
     898             :                                renderState,
     899           0 :                                true );
     900             :     }
     901             : 
     902           0 :     geometry::IntegerSize2D CanvasHelper::getSize()
     903             :     {
     904           0 :         if( !mpOutDev.get() )
     905           0 :             return geometry::IntegerSize2D(); // we're disposed
     906             : 
     907           0 :         return vcl::unotools::integerSize2DFromSize( mpOutDev->getOutDev().GetOutputSizePixel() );
     908             :     }
     909             : 
     910           0 :     uno::Reference< rendering::XBitmap > CanvasHelper::getScaledBitmap( const geometry::RealSize2D& newSize,
     911             :                                                                         bool                        beFast )
     912             :     {
     913           0 :         if( !mpOutDev.get() || !mpDevice )
     914           0 :             return uno::Reference< rendering::XBitmap >(); // we're disposed
     915             : 
     916           0 :         OutputDevice& rOutDev( mpOutDev->getOutDev() );
     917             : 
     918           0 :         tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     919           0 :         rOutDev.EnableMapMode( false );
     920           0 :         rOutDev.SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
     921             : 
     922             :         // TODO(F2): Support alpha vdev canvas here
     923           0 :         const Point aEmptyPoint(0,0);
     924           0 :         const Size  aBmpSize( rOutDev.GetOutputSizePixel() );
     925             : 
     926           0 :         Bitmap aBitmap( rOutDev.GetBitmap(aEmptyPoint, aBmpSize) );
     927             : 
     928             :         aBitmap.Scale( vcl::unotools::sizeFromRealSize2D(newSize),
     929           0 :                        beFast ? BmpScaleFlag::Default : BmpScaleFlag::BestQuality );
     930             : 
     931             :         return uno::Reference< rendering::XBitmap >(
     932           0 :             new CanvasBitmap( aBitmap, *mpDevice, mpOutDev ) );
     933             :     }
     934             : 
     935           0 :     uno::Sequence< sal_Int8 > CanvasHelper::getData( rendering::IntegerBitmapLayout&     rLayout,
     936             :                                                      const geometry::IntegerRectangle2D& rect )
     937             :     {
     938           0 :         if( !mpOutDev.get() )
     939           0 :             return uno::Sequence< sal_Int8 >(); // we're disposed
     940             : 
     941           0 :         rLayout = getMemoryLayout();
     942             : 
     943             :         // TODO(F2): Support alpha canvas here
     944           0 :         const Rectangle aRect( vcl::unotools::rectangleFromIntegerRectangle2D(rect) );
     945             : 
     946           0 :         OutputDevice& rOutDev( mpOutDev->getOutDev() );
     947             : 
     948           0 :         tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
     949           0 :         rOutDev.EnableMapMode( false );
     950           0 :         rOutDev.SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
     951             : 
     952             :         Bitmap aBitmap( rOutDev.GetBitmap(aRect.TopLeft(),
     953           0 :                                           aRect.GetSize()) );
     954             : 
     955           0 :         Bitmap::ScopedReadAccess pReadAccess( aBitmap );
     956             : 
     957           0 :         ENSURE_OR_THROW( pReadAccess.get() != NULL,
     958             :                          "Could not acquire read access to OutDev bitmap" );
     959             : 
     960           0 :         const sal_Int32 nWidth( rect.X2 - rect.X1 );
     961           0 :         const sal_Int32 nHeight( rect.Y2 - rect.Y1 );
     962             : 
     963           0 :         rLayout.ScanLines = nHeight;
     964           0 :         rLayout.ScanLineBytes = nWidth*4;
     965           0 :         rLayout.ScanLineStride = rLayout.ScanLineBytes;
     966             : 
     967           0 :         uno::Sequence< sal_Int8 > aRes( 4*nWidth*nHeight );
     968           0 :         sal_Int8* pRes = aRes.getArray();
     969             : 
     970           0 :         int nCurrPos(0);
     971           0 :         for( int y=0; y<nHeight; ++y )
     972             :         {
     973           0 :             for( int x=0; x<nWidth; ++x )
     974             :             {
     975           0 :                 pRes[ nCurrPos++ ] = pReadAccess->GetColor( y, x ).GetRed();
     976           0 :                 pRes[ nCurrPos++ ] = pReadAccess->GetColor( y, x ).GetGreen();
     977           0 :                 pRes[ nCurrPos++ ] = pReadAccess->GetColor( y, x ).GetBlue();
     978           0 :                 pRes[ nCurrPos++ ] = -1;
     979             :             }
     980             :         }
     981             : 
     982           0 :         return aRes;
     983             :     }
     984             : 
     985           0 :     void CanvasHelper::setData( const uno::Sequence< sal_Int8 >&        data,
     986             :                                 const rendering::IntegerBitmapLayout&   aLayout,
     987             :                                 const geometry::IntegerRectangle2D&     rect )
     988             :     {
     989           0 :         if( !mpOutDev.get() )
     990           0 :             return; // we're disposed
     991             : 
     992           0 :         const rendering::IntegerBitmapLayout aRefLayout( getMemoryLayout() );
     993           0 :         ENSURE_ARG_OR_THROW( aRefLayout.PlaneStride != aLayout.PlaneStride ||
     994             :                              aRefLayout.ColorSpace  != aLayout.ColorSpace ||
     995             :                              aRefLayout.Palette     != aLayout.Palette ||
     996             :                              aRefLayout.IsMsbFirst  != aLayout.IsMsbFirst,
     997             :                              "Mismatching memory layout" );
     998             : 
     999           0 :         OutputDevice& rOutDev( mpOutDev->getOutDev() );
    1000             : 
    1001           0 :         tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
    1002           0 :         rOutDev.EnableMapMode( false );
    1003           0 :         rOutDev.SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
    1004             : 
    1005           0 :         const Rectangle aRect( vcl::unotools::rectangleFromIntegerRectangle2D(rect) );
    1006             :         const sal_uInt16    nBitCount( ::std::min( (sal_uInt16)24U,
    1007           0 :                                                (sal_uInt16)rOutDev.GetBitCount() ) );
    1008           0 :         const BitmapPalette* pPalette = NULL;
    1009             : 
    1010           0 :         if( nBitCount <= 8 )
    1011             :         {
    1012             :             // TODO(Q1): Extract this to a common place, e.g. GraphicDevice
    1013             : 
    1014             :             // try to determine palette from output device (by
    1015             :             // extracting a 1,1 bitmap, and querying it)
    1016           0 :             const Point aEmptyPoint;
    1017           0 :             const Size  aSize(1,1);
    1018             :             Bitmap aTmpBitmap( rOutDev.GetBitmap( aEmptyPoint,
    1019           0 :                                                   aSize ) );
    1020             : 
    1021           0 :             Bitmap::ScopedReadAccess pReadAccess( aTmpBitmap );
    1022             : 
    1023           0 :             pPalette = &pReadAccess->GetPalette();
    1024             :         }
    1025             : 
    1026             :         // TODO(F2): Support alpha canvas here
    1027           0 :         Bitmap aBitmap( aRect.GetSize(), nBitCount, pPalette );
    1028             : 
    1029           0 :         bool bCopyBack( false ); // only copy something back, if we
    1030             :                                  // actually changed some pixel
    1031             :         {
    1032           0 :             Bitmap::ScopedWriteAccess pWriteAccess( aBitmap );
    1033             : 
    1034           0 :             ENSURE_OR_THROW( pWriteAccess.get() != NULL,
    1035             :                              "Could not acquire write access to OutDev bitmap" );
    1036             : 
    1037             :             // for the time being, always read as RGB
    1038           0 :             const sal_Int32 nWidth( rect.X2 - rect.X1 );
    1039           0 :             const sal_Int32 nHeight( rect.Y2 - rect.Y1 );
    1040           0 :             int x, y, nCurrPos(0);
    1041           0 :             for( y=0; y<nHeight; ++y )
    1042             :             {
    1043           0 :                 switch( pWriteAccess->GetScanlineFormat() )
    1044             :                 {
    1045             :                     case BMP_FORMAT_8BIT_PAL:
    1046             :                     {
    1047           0 :                         Scanline pScan = pWriteAccess->GetScanline( y );
    1048             : 
    1049           0 :                         for( x=0; x<nWidth; ++x )
    1050             :                         {
    1051           0 :                             *pScan++ = (sal_uInt8)pWriteAccess->GetBestPaletteIndex(
    1052           0 :                                 BitmapColor( data[ nCurrPos ],
    1053           0 :                                              data[ nCurrPos+1 ],
    1054           0 :                                              data[ nCurrPos+2 ] ) );
    1055             : 
    1056           0 :                             nCurrPos += 4;
    1057             :                         }
    1058             :                     }
    1059           0 :                     break;
    1060             : 
    1061             :                     case BMP_FORMAT_24BIT_TC_BGR:
    1062             :                     {
    1063           0 :                         Scanline pScan = pWriteAccess->GetScanline( y );
    1064             : 
    1065           0 :                         for( x=0; x<nWidth; ++x )
    1066             :                         {
    1067           0 :                             *pScan++ = data[ nCurrPos+2 ];
    1068           0 :                             *pScan++ = data[ nCurrPos+1 ];
    1069           0 :                             *pScan++ = data[ nCurrPos   ];
    1070             : 
    1071           0 :                             nCurrPos += 4;
    1072             :                         }
    1073             :                     }
    1074           0 :                     break;
    1075             : 
    1076             :                     case BMP_FORMAT_24BIT_TC_RGB:
    1077             :                     {
    1078           0 :                         Scanline pScan = pWriteAccess->GetScanline( y );
    1079             : 
    1080           0 :                         for( x=0; x<nWidth; ++x )
    1081             :                         {
    1082           0 :                             *pScan++ = data[ nCurrPos   ];
    1083           0 :                             *pScan++ = data[ nCurrPos+1 ];
    1084           0 :                             *pScan++ = data[ nCurrPos+2 ];
    1085             : 
    1086           0 :                             nCurrPos += 4;
    1087             :                         }
    1088             :                     }
    1089           0 :                     break;
    1090             : 
    1091             :                     default:
    1092             :                     {
    1093           0 :                         for( x=0; x<nWidth; ++x )
    1094             :                         {
    1095           0 :                             pWriteAccess->SetPixel( y, x, BitmapColor( data[ nCurrPos   ],
    1096           0 :                                                                        data[ nCurrPos+1 ],
    1097           0 :                                                                        data[ nCurrPos+2 ] ) );
    1098           0 :                             nCurrPos += 4;
    1099             :                         }
    1100             :                     }
    1101           0 :                     break;
    1102             :                 }
    1103             :             }
    1104             : 
    1105           0 :             bCopyBack = true;
    1106             :         }
    1107             : 
    1108             :         // copy back only here, since the BitmapAccessors must be
    1109             :         // destroyed beforehand
    1110           0 :         if( bCopyBack )
    1111             :         {
    1112             :             // TODO(F2): Support alpha canvas here
    1113           0 :             rOutDev.DrawBitmap(aRect.TopLeft(), aBitmap);
    1114           0 :         }
    1115             :     }
    1116             : 
    1117           0 :     void CanvasHelper::setPixel( const uno::Sequence< sal_Int8 >&       color,
    1118             :                                  const rendering::IntegerBitmapLayout&  rLayout,
    1119             :                                  const geometry::IntegerPoint2D&        pos )
    1120             :     {
    1121           0 :         if( !mpOutDev.get() )
    1122           0 :             return; // we're disposed
    1123             : 
    1124           0 :         OutputDevice& rOutDev( mpOutDev->getOutDev() );
    1125             : 
    1126           0 :         tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
    1127           0 :         rOutDev.EnableMapMode( false );
    1128           0 :         rOutDev.SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
    1129             : 
    1130           0 :         const Size aBmpSize( rOutDev.GetOutputSizePixel() );
    1131             : 
    1132           0 :         ENSURE_ARG_OR_THROW( pos.X >= 0 && pos.X < aBmpSize.Width(),
    1133             :                              "X coordinate out of bounds" );
    1134           0 :         ENSURE_ARG_OR_THROW( pos.Y >= 0 && pos.Y < aBmpSize.Height(),
    1135             :                              "Y coordinate out of bounds" );
    1136           0 :         ENSURE_ARG_OR_THROW( color.getLength() > 3,
    1137             :                              "not enough color components" );
    1138             : 
    1139           0 :         const rendering::IntegerBitmapLayout aRefLayout( getMemoryLayout() );
    1140           0 :         ENSURE_ARG_OR_THROW( aRefLayout.PlaneStride != rLayout.PlaneStride ||
    1141             :                              aRefLayout.ColorSpace  != rLayout.ColorSpace ||
    1142             :                              aRefLayout.Palette     != rLayout.Palette ||
    1143             :                              aRefLayout.IsMsbFirst  != rLayout.IsMsbFirst,
    1144             :                              "Mismatching memory layout" );
    1145             : 
    1146             :         // TODO(F2): Support alpha canvas here
    1147             :         rOutDev.DrawPixel( vcl::unotools::pointFromIntegerPoint2D( pos ),
    1148           0 :                            ::canvas::tools::stdIntSequenceToColor( color ));
    1149             :     }
    1150             : 
    1151           0 :     uno::Sequence< sal_Int8 > CanvasHelper::getPixel( rendering::IntegerBitmapLayout& rLayout,
    1152             :                                                       const geometry::IntegerPoint2D& pos )
    1153             :     {
    1154           0 :         if( !mpOutDev.get() )
    1155           0 :             return uno::Sequence< sal_Int8 >(); // we're disposed
    1156             : 
    1157           0 :         rLayout = getMemoryLayout();
    1158           0 :         rLayout.ScanLines = 1;
    1159           0 :         rLayout.ScanLineBytes = 4;
    1160           0 :         rLayout.ScanLineStride = rLayout.ScanLineBytes;
    1161             : 
    1162           0 :         OutputDevice& rOutDev( mpOutDev->getOutDev() );
    1163             : 
    1164           0 :         tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
    1165           0 :         rOutDev.EnableMapMode( false );
    1166           0 :         rOutDev.SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
    1167             : 
    1168           0 :         const Size aBmpSize( rOutDev.GetOutputSizePixel() );
    1169             : 
    1170           0 :         ENSURE_ARG_OR_THROW( pos.X >= 0 && pos.X < aBmpSize.Width(),
    1171             :                              "X coordinate out of bounds" );
    1172           0 :         ENSURE_ARG_OR_THROW( pos.Y >= 0 && pos.Y < aBmpSize.Height(),
    1173             :                              "Y coordinate out of bounds" );
    1174             : 
    1175             :         // TODO(F2): Support alpha canvas here
    1176             :         return ::canvas::tools::colorToStdIntSequence(
    1177             :             rOutDev.GetPixel(
    1178           0 :                 vcl::unotools::pointFromIntegerPoint2D( pos )));
    1179             :     }
    1180             : 
    1181           0 :     rendering::IntegerBitmapLayout CanvasHelper::getMemoryLayout()
    1182             :     {
    1183           0 :         if( !mpOutDev.get() )
    1184           0 :             return rendering::IntegerBitmapLayout(); // we're disposed
    1185             : 
    1186           0 :         rendering::IntegerBitmapLayout xBitmapLayout( ::canvas::tools::getStdMemoryLayout(getSize()) );
    1187           0 :         if ( !mbHaveAlpha )
    1188           0 :             xBitmapLayout.ColorSpace = canvas::tools::getStdColorSpaceWithoutAlpha();
    1189             : 
    1190           0 :         return xBitmapLayout;
    1191             :     }
    1192             : 
    1193           0 :     int CanvasHelper::setupOutDevState( const rendering::ViewState&     viewState,
    1194             :                                         const rendering::RenderState&   renderState,
    1195             :                                         ColorType                       eColorType ) const
    1196             :     {
    1197           0 :         ENSURE_OR_THROW( mpOutDev.get(),
    1198             :                          "outdev null. Are we disposed?" );
    1199             : 
    1200             :         ::canvas::tools::verifyInput( renderState,
    1201             :                                       BOOST_CURRENT_FUNCTION,
    1202             :                                       mpDevice,
    1203             :                                       2,
    1204           0 :                                       eColorType == IGNORE_COLOR ? 0 : 3 );
    1205             : 
    1206           0 :         OutputDevice& rOutDev( mpOutDev->getOutDev() );
    1207           0 :         OutputDevice* p2ndOutDev = NULL;
    1208             : 
    1209           0 :         rOutDev.EnableMapMode( false );
    1210           0 :         rOutDev.SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
    1211             : 
    1212           0 :         if( mp2ndOutDev )
    1213           0 :             p2ndOutDev = &mp2ndOutDev->getOutDev();
    1214             : 
    1215           0 :         int nTransparency(0);
    1216             : 
    1217             :         // TODO(P2): Don't change clipping all the time, maintain current clip
    1218             :         // state and change only when update is necessary
    1219           0 :         ::canvas::tools::clipOutDev(viewState, renderState, rOutDev, p2ndOutDev);
    1220             : 
    1221           0 :         Color aColor( COL_WHITE );
    1222             : 
    1223           0 :         if( renderState.DeviceColor.getLength() > 2 )
    1224             :         {
    1225             :             aColor = vcl::unotools::stdColorSpaceSequenceToColor(
    1226           0 :                 renderState.DeviceColor );
    1227             :         }
    1228             : 
    1229             :         // extract alpha, and make color opaque
    1230             :         // afterwards. Otherwise, OutputDevice won't draw anything
    1231           0 :         nTransparency = aColor.GetTransparency();
    1232           0 :         aColor.SetTransparency(0);
    1233             : 
    1234           0 :         if( eColorType != IGNORE_COLOR )
    1235             :         {
    1236           0 :             switch( eColorType )
    1237             :             {
    1238             :                 case LINE_COLOR:
    1239           0 :                     rOutDev.SetLineColor( aColor );
    1240           0 :                     rOutDev.SetFillColor();
    1241             : 
    1242           0 :                     if( p2ndOutDev )
    1243             :                     {
    1244           0 :                         p2ndOutDev->SetLineColor( aColor );
    1245           0 :                         p2ndOutDev->SetFillColor();
    1246             :                     }
    1247           0 :                     break;
    1248             : 
    1249             :                 case FILL_COLOR:
    1250           0 :                     rOutDev.SetFillColor( aColor );
    1251           0 :                     rOutDev.SetLineColor();
    1252             : 
    1253           0 :                     if( p2ndOutDev )
    1254             :                     {
    1255           0 :                         p2ndOutDev->SetFillColor( aColor );
    1256           0 :                         p2ndOutDev->SetLineColor();
    1257             :                     }
    1258           0 :                     break;
    1259             : 
    1260             :                 case TEXT_COLOR:
    1261           0 :                     rOutDev.SetTextColor( aColor );
    1262             : 
    1263           0 :                     if( p2ndOutDev )
    1264           0 :                         p2ndOutDev->SetTextColor( aColor );
    1265           0 :                     break;
    1266             : 
    1267             :                 default:
    1268           0 :                     ENSURE_OR_THROW( false,
    1269             :                                      "Unexpected color type");
    1270             :                     break;
    1271             :             }
    1272             :         }
    1273             : 
    1274           0 :         return nTransparency;
    1275             :     }
    1276             : 
    1277           0 :     bool CanvasHelper::setupTextOutput( ::Point&                                        o_rOutPos,
    1278             :                                         const rendering::ViewState&                     viewState,
    1279             :                                         const rendering::RenderState&                   renderState,
    1280             :                                         const uno::Reference< rendering::XCanvasFont >& xFont   ) const
    1281             :     {
    1282           0 :         ENSURE_OR_THROW( mpOutDev.get(),
    1283             :                          "outdev null. Are we disposed?" );
    1284             : 
    1285           0 :         OutputDevice& rOutDev( mpOutDev->getOutDev() );
    1286             : 
    1287           0 :         setupOutDevState( viewState, renderState, TEXT_COLOR );
    1288             : 
    1289           0 :         CanvasFont* pFont = dynamic_cast< CanvasFont* >( xFont.get() );
    1290             : 
    1291           0 :         ENSURE_ARG_OR_THROW( pFont,
    1292             :                              "Font not compatible with this canvas" );
    1293             : 
    1294           0 :         vcl::Font aVCLFont = pFont->getVCLFont();
    1295             : 
    1296           0 :         Color aColor( COL_BLACK );
    1297             : 
    1298           0 :         if( renderState.DeviceColor.getLength() > 2 )
    1299             :         {
    1300             :             aColor = vcl::unotools::stdColorSpaceSequenceToColor(
    1301           0 :                 renderState.DeviceColor );
    1302             :         }
    1303             : 
    1304             :         // setup font color
    1305           0 :         aVCLFont.SetColor( aColor );
    1306           0 :         aVCLFont.SetFillColor( aColor );
    1307             : 
    1308             :         // no need to replicate this for mp2ndOutDev, we're modifying only aVCLFont here.
    1309           0 :         if( !tools::setupFontTransform( o_rOutPos, aVCLFont, viewState, renderState, rOutDev ) )
    1310           0 :             return false;
    1311             : 
    1312           0 :         rOutDev.SetFont( aVCLFont );
    1313             : 
    1314           0 :         if( mp2ndOutDev )
    1315           0 :             mp2ndOutDev->getOutDev().SetFont( aVCLFont );
    1316             : 
    1317           0 :         return true;
    1318             :     }
    1319             : 
    1320           0 :     bool CanvasHelper::repaint( const GraphicObjectSharedPtr&   rGrf,
    1321             :                                 const rendering::ViewState&     viewState,
    1322             :                                 const rendering::RenderState&   renderState,
    1323             :                                 const ::Point&                  rPt,
    1324             :                                 const ::Size&                   rSz,
    1325             :                                 const GraphicAttr&              rAttr ) const
    1326             :     {
    1327           0 :         ENSURE_OR_RETURN_FALSE( rGrf,
    1328             :                           "Invalid Graphic" );
    1329             : 
    1330           0 :         if( !mpOutDev )
    1331           0 :             return false; // disposed
    1332             :         else
    1333             :         {
    1334           0 :             tools::OutDevStateKeeper aStateKeeper( mpProtectedOutDev );
    1335           0 :             setupOutDevState( viewState, renderState, IGNORE_COLOR );
    1336             : 
    1337           0 :             if( !rGrf->Draw( &mpOutDev->getOutDev(), rPt, rSz, &rAttr ) )
    1338           0 :                 return false;
    1339             : 
    1340             :             // #i80779# Redraw also into mask outdev
    1341           0 :             if( mp2ndOutDev )
    1342           0 :                 return rGrf->Draw( &mp2ndOutDev->getOutDev(), rPt, rSz, &rAttr );
    1343             : 
    1344           0 :             return true;
    1345             :         }
    1346             :     }
    1347             : 
    1348           0 :     void CanvasHelper::flush() const
    1349             :     {
    1350           0 :         if( mpOutDev && mpOutDev->getOutDev().GetOutDevType() == OUTDEV_WINDOW )
    1351             :         {
    1352             :             // TODO(Q3): Evil downcast. And what's more, Window::Flush is
    1353             :             // not even const. Wah.
    1354           0 :             static_cast<vcl::Window&>(mpOutDev->getOutDev()).Flush();
    1355             :         }
    1356             : 
    1357           0 :         if( mp2ndOutDev && mp2ndOutDev->getOutDev().GetOutDevType() == OUTDEV_WINDOW )
    1358             :         {
    1359             :             // TODO(Q3): Evil downcast. And what's more, Window::Flush is
    1360             :             // not even const. Wah.
    1361           0 :             static_cast<vcl::Window&>(mp2ndOutDev->getOutDev()).Flush();
    1362             :         }
    1363           0 :     }
    1364             : 
    1365           0 : }
    1366             : 
    1367             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11