1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_drawinglayer.hxx"
30 
31 #include <vclhelperbufferdevice.hxx>
32 #include <basegfx/range/b2drange.hxx>
33 #include <vcl/bitmapex.hxx>
34 #include <basegfx/matrix/b2dhommatrix.hxx>
35 #include <tools/stream.hxx>
36 
37 //////////////////////////////////////////////////////////////////////////////
38 // support for rendering Bitmap and BitmapEx contents
39 
40 namespace drawinglayer
41 {
42 	impBufferDevice::impBufferDevice(
43 		OutputDevice& rOutDev,
44 		const basegfx::B2DRange& rRange,
45 		bool bAddOffsetToMapping)
46 	:	mrOutDev(rOutDev),
47 		maContent(rOutDev),
48 		mpMask(0L),
49 		mpAlpha(0L)
50 	{
51         basegfx::B2DRange aRangePixel(rRange);
52         aRangePixel.transform(rOutDev.GetViewTransformation());
53         const Rectangle aRectPixel(
54 			(sal_Int32)floor(aRangePixel.getMinX()), (sal_Int32)floor(aRangePixel.getMinY()),
55 			(sal_Int32)ceil(aRangePixel.getMaxX()), (sal_Int32)ceil(aRangePixel.getMaxY()));
56 		const Point aEmptyPoint;
57 		maDestPixel = Rectangle(aEmptyPoint, rOutDev.GetOutputSizePixel());
58 		maDestPixel.Intersection(aRectPixel);
59 
60 		if(isVisible())
61 		{
62 			maContent.SetOutputSizePixel(maDestPixel.GetSize(), false);
63 
64             // #i93485# assert when copying from window to VDev is used
65             OSL_ENSURE(rOutDev.GetOutDevType() != OUTDEV_WINDOW,
66                 "impBufferDevice render helper: Copying from Window to VDev, this should be avoided (!)");
67 
68 			const bool bWasEnabledSrc(rOutDev.IsMapModeEnabled());
69 			rOutDev.EnableMapMode(false);
70 			maContent.DrawOutDev(aEmptyPoint, maDestPixel.GetSize(), maDestPixel.TopLeft(), maDestPixel.GetSize(), rOutDev);
71 			rOutDev.EnableMapMode(bWasEnabledSrc);
72 
73 			MapMode aNewMapMode(rOutDev.GetMapMode());
74 
75 			if(bAddOffsetToMapping)
76 			{
77 				const Point aLogicTopLeft(rOutDev.PixelToLogic(maDestPixel.TopLeft()));
78 				aNewMapMode.SetOrigin(Point(-aLogicTopLeft.X(), -aLogicTopLeft.Y()));
79 			}
80 
81 			maContent.SetMapMode(aNewMapMode);
82 
83             // copy AA flag for new target
84             maContent.SetAntialiasing(mrOutDev.GetAntialiasing());
85 		}
86 	}
87 
88 	impBufferDevice::~impBufferDevice()
89 	{
90 		delete mpMask;
91 		delete mpAlpha;
92 	}
93 
94 	void impBufferDevice::paint(double fTrans)
95 	{
96 		const Point aEmptyPoint;
97 		const Size aSizePixel(maContent.GetOutputSizePixel());
98 		const bool bWasEnabledDst(mrOutDev.IsMapModeEnabled());
99     	static bool bDoSaveForVisualControl(false);
100 
101 		mrOutDev.EnableMapMode(false);
102 		maContent.EnableMapMode(false);
103 		Bitmap aContent(maContent.GetBitmap(aEmptyPoint, aSizePixel));
104 
105 		if(bDoSaveForVisualControl)
106 		{
107 			SvFileStream aNew((const String&)String(ByteString( "c:\\content.bmp" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
108 			aNew << aContent;
109 		}
110 
111         if(mpAlpha)
112 		{
113 			mpAlpha->EnableMapMode(false);
114 			const AlphaMask aAlphaMask(mpAlpha->GetBitmap(aEmptyPoint, aSizePixel));
115 
116             if(bDoSaveForVisualControl)
117 		    {
118 			    SvFileStream aNew((const String&)String(ByteString( "c:\\transparence.bmp" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
119 			    aNew << aAlphaMask.GetBitmap();
120 		    }
121 
122             mrOutDev.DrawBitmapEx(maDestPixel.TopLeft(), BitmapEx(aContent, aAlphaMask));
123 		}
124 		else if(mpMask)
125 		{
126 			mpMask->EnableMapMode(false);
127 			const Bitmap aMask(mpMask->GetBitmap(aEmptyPoint, aSizePixel));
128 
129             if(bDoSaveForVisualControl)
130 		    {
131 			    SvFileStream aNew((const String&)String(ByteString( "c:\\mask.bmp" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
132 			    aNew << aMask;
133 		    }
134 
135             mrOutDev.DrawBitmapEx(maDestPixel.TopLeft(), BitmapEx(aContent, aMask));
136 		}
137 		else if(0.0 != fTrans)
138 		{
139 			sal_uInt8 nMaskValue((sal_uInt8)basegfx::fround(fTrans * 255.0));
140 			const AlphaMask aAlphaMask(aSizePixel, &nMaskValue);
141 			mrOutDev.DrawBitmapEx(maDestPixel.TopLeft(), BitmapEx(aContent, aAlphaMask));
142 		}
143 		else
144 		{
145 			mrOutDev.DrawBitmap(maDestPixel.TopLeft(), aContent);
146 		}
147 
148 		mrOutDev.EnableMapMode(bWasEnabledDst);
149 	}
150 
151 	VirtualDevice& impBufferDevice::getMask()
152 	{
153 		if(!mpMask)
154 		{
155 			mpMask = new VirtualDevice(mrOutDev, 1);
156 			mpMask->SetOutputSizePixel(maDestPixel.GetSize(), true);
157 			mpMask->SetMapMode(maContent.GetMapMode());
158 
159             // do NOT copy AA flag for mask!
160 		}
161 
162 		return *mpMask;
163 	}
164 
165 	VirtualDevice& impBufferDevice::getTransparence()
166 	{
167 		if(!mpAlpha)
168 		{
169 			mpAlpha = new VirtualDevice();
170 			mpAlpha->SetOutputSizePixel(maDestPixel.GetSize(), true);
171 			mpAlpha->SetMapMode(maContent.GetMapMode());
172 
173             // copy AA flag for new target; masking needs to be smooth
174             mpAlpha->SetAntialiasing(maContent.GetAntialiasing());
175 		}
176 
177 		return *mpAlpha;
178 	}
179 } // end of namespace drawinglayer
180 
181 //////////////////////////////////////////////////////////////////////////////
182 // eof
183