xref: /AOO41X/main/vcl/win/source/gdi/salgdi_gdiplus.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_vcl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir #include <string.h>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <tools/svwin.h>
35*cdf0e10cSrcweir #include <tools/debug.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <win/wincomp.hxx>
38*cdf0e10cSrcweir #include <win/saldata.hxx>
39*cdf0e10cSrcweir #include <win/salgdi.h>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #ifndef min
42*cdf0e10cSrcweir #define min(a,b)	(((a) < (b)) ? (a) : (b))
43*cdf0e10cSrcweir #endif
44*cdf0e10cSrcweir #ifndef max
45*cdf0e10cSrcweir #define max(a,b)	(((a) > (b)) ? (a) : (b))
46*cdf0e10cSrcweir #endif
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #if defined _MSC_VER
49*cdf0e10cSrcweir #pragma warning(push, 1)
50*cdf0e10cSrcweir #endif
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include <GdiPlus.h>
53*cdf0e10cSrcweir #include <GdiPlusEnums.h>
54*cdf0e10cSrcweir #include <GdiPlusColor.h>
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir #if defined _MSC_VER
57*cdf0e10cSrcweir #pragma warning(pop)
58*cdf0e10cSrcweir #endif
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir // -----------------------------------------------------------------------
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir void impAddB2DPolygonToGDIPlusGraphicsPathReal(Gdiplus::GraphicsPath& rPath, const basegfx::B2DPolygon& rPolygon, bool bNoLineJoin)
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir     sal_uInt32 nCount(rPolygon.count());
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     if(nCount)
69*cdf0e10cSrcweir     {
70*cdf0e10cSrcweir         const sal_uInt32 nEdgeCount(rPolygon.isClosed() ? nCount : nCount - 1);
71*cdf0e10cSrcweir         const bool bControls(rPolygon.areControlPointsUsed());
72*cdf0e10cSrcweir         basegfx::B2DPoint aCurr(rPolygon.getB2DPoint(0));
73*cdf0e10cSrcweir         Gdiplus::PointF aFCurr(Gdiplus::REAL(aCurr.getX()), Gdiplus::REAL(aCurr.getY()));
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir         for(sal_uInt32 a(0); a < nEdgeCount; a++)
76*cdf0e10cSrcweir         {
77*cdf0e10cSrcweir 	        const sal_uInt32 nNextIndex((a + 1) % nCount);
78*cdf0e10cSrcweir 	        const basegfx::B2DPoint aNext(rPolygon.getB2DPoint(nNextIndex));
79*cdf0e10cSrcweir 	        const Gdiplus::PointF aFNext(Gdiplus::REAL(aNext.getX()), Gdiplus::REAL(aNext.getY()));
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	        if(bControls && (rPolygon.isNextControlPointUsed(a) || rPolygon.isPrevControlPointUsed(nNextIndex)))
82*cdf0e10cSrcweir 	        {
83*cdf0e10cSrcweir 		        const basegfx::B2DPoint aCa(rPolygon.getNextControlPoint(a));
84*cdf0e10cSrcweir 		        const basegfx::B2DPoint aCb(rPolygon.getPrevControlPoint(nNextIndex));
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 		        rPath.AddBezier(
87*cdf0e10cSrcweir 			        aFCurr,
88*cdf0e10cSrcweir 			        Gdiplus::PointF(Gdiplus::REAL(aCa.getX()), Gdiplus::REAL(aCa.getY())),
89*cdf0e10cSrcweir 			        Gdiplus::PointF(Gdiplus::REAL(aCb.getX()), Gdiplus::REAL(aCb.getY())),
90*cdf0e10cSrcweir 			        aFNext);
91*cdf0e10cSrcweir 	        }
92*cdf0e10cSrcweir 	        else
93*cdf0e10cSrcweir 	        {
94*cdf0e10cSrcweir 		        rPath.AddLine(aFCurr, aFNext);
95*cdf0e10cSrcweir 	        }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 	        if(a + 1 < nEdgeCount)
98*cdf0e10cSrcweir 	        {
99*cdf0e10cSrcweir 		        aFCurr = aFNext;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 			    if(bNoLineJoin)
102*cdf0e10cSrcweir 			    {
103*cdf0e10cSrcweir 				    rPath.StartFigure();
104*cdf0e10cSrcweir 			    }
105*cdf0e10cSrcweir 	        }
106*cdf0e10cSrcweir         }
107*cdf0e10cSrcweir     }
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir void impAddB2DPolygonToGDIPlusGraphicsPathInteger(Gdiplus::GraphicsPath& rPath, const basegfx::B2DPolygon& rPolygon, bool bNoLineJoin)
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir     sal_uInt32 nCount(rPolygon.count());
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     if(nCount)
115*cdf0e10cSrcweir     {
116*cdf0e10cSrcweir         const sal_uInt32 nEdgeCount(rPolygon.isClosed() ? nCount : nCount - 1);
117*cdf0e10cSrcweir         const bool bControls(rPolygon.areControlPointsUsed());
118*cdf0e10cSrcweir         basegfx::B2DPoint aCurr(rPolygon.getB2DPoint(0));
119*cdf0e10cSrcweir         Gdiplus::Point aICurr(INT(aCurr.getX()), INT(aCurr.getY()));
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir         for(sal_uInt32 a(0); a < nEdgeCount; a++)
122*cdf0e10cSrcweir         {
123*cdf0e10cSrcweir 	        const sal_uInt32 nNextIndex((a + 1) % nCount);
124*cdf0e10cSrcweir 	        const basegfx::B2DPoint aNext(rPolygon.getB2DPoint(nNextIndex));
125*cdf0e10cSrcweir 	        const Gdiplus::Point aINext(INT(aNext.getX()), INT(aNext.getY()));
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 	        if(bControls && (rPolygon.isNextControlPointUsed(a) || rPolygon.isPrevControlPointUsed(nNextIndex)))
128*cdf0e10cSrcweir 	        {
129*cdf0e10cSrcweir 		        const basegfx::B2DPoint aCa(rPolygon.getNextControlPoint(a));
130*cdf0e10cSrcweir 		        const basegfx::B2DPoint aCb(rPolygon.getPrevControlPoint(nNextIndex));
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 		        rPath.AddBezier(
133*cdf0e10cSrcweir 			        aICurr,
134*cdf0e10cSrcweir 			        Gdiplus::Point(INT(aCa.getX()), INT(aCa.getY())),
135*cdf0e10cSrcweir 			        Gdiplus::Point(INT(aCb.getX()), INT(aCb.getY())),
136*cdf0e10cSrcweir 			        aINext);
137*cdf0e10cSrcweir 	        }
138*cdf0e10cSrcweir 	        else
139*cdf0e10cSrcweir 	        {
140*cdf0e10cSrcweir 		        rPath.AddLine(aICurr, aINext);
141*cdf0e10cSrcweir 	        }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	        if(a + 1 < nEdgeCount)
144*cdf0e10cSrcweir 	        {
145*cdf0e10cSrcweir 		        aICurr = aINext;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 			    if(bNoLineJoin)
148*cdf0e10cSrcweir 			    {
149*cdf0e10cSrcweir 				    rPath.StartFigure();
150*cdf0e10cSrcweir 			    }
151*cdf0e10cSrcweir 	        }
152*cdf0e10cSrcweir         }
153*cdf0e10cSrcweir     }
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir bool WinSalGraphics::drawPolyPolygon( const ::basegfx::B2DPolyPolygon& rPolyPolygon, double fTransparency)
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir 	const sal_uInt32 nCount(rPolyPolygon.count());
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	if(mbBrush && nCount && (fTransparency >= 0.0 && fTransparency < 1.0))
161*cdf0e10cSrcweir 	{
162*cdf0e10cSrcweir 		Gdiplus::Graphics aGraphics(mhDC);
163*cdf0e10cSrcweir 		const sal_uInt8 aTrans((sal_uInt8)255 - (sal_uInt8)basegfx::fround(fTransparency * 255.0));
164*cdf0e10cSrcweir 		Gdiplus::Color aTestColor(aTrans, SALCOLOR_RED(maFillColor), SALCOLOR_GREEN(maFillColor), SALCOLOR_BLUE(maFillColor));
165*cdf0e10cSrcweir 		Gdiplus::SolidBrush aTestBrush(aTestColor);
166*cdf0e10cSrcweir 		Gdiplus::GraphicsPath aPath;
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 		for(sal_uInt32 a(0); a < nCount; a++)
169*cdf0e10cSrcweir 		{
170*cdf0e10cSrcweir             if(0 != a)
171*cdf0e10cSrcweir             {
172*cdf0e10cSrcweir                 aPath.StartFigure(); // #i101491# not needed for first run
173*cdf0e10cSrcweir             }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 			impAddB2DPolygonToGDIPlusGraphicsPathReal(aPath, rPolyPolygon.getB2DPolygon(a), false);
176*cdf0e10cSrcweir             aPath.CloseFigure();
177*cdf0e10cSrcweir 		}
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir         if(getAntiAliasB2DDraw())
180*cdf0e10cSrcweir         {
181*cdf0e10cSrcweir             aGraphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
182*cdf0e10cSrcweir         }
183*cdf0e10cSrcweir         else
184*cdf0e10cSrcweir         {
185*cdf0e10cSrcweir     		aGraphics.SetSmoothingMode(Gdiplus::SmoothingModeNone);
186*cdf0e10cSrcweir         }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 		aGraphics.FillPath(&aTestBrush, &aPath);
189*cdf0e10cSrcweir 	}
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir  	return true;
192*cdf0e10cSrcweir }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir bool WinSalGraphics::drawPolyLine( const basegfx::B2DPolygon& rPolygon, double fTransparency, const basegfx::B2DVector& rLineWidths, basegfx::B2DLineJoin eLineJoin )
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir     const sal_uInt32 nCount(rPolygon.count());
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 	if(mbPen && nCount)
199*cdf0e10cSrcweir 	{
200*cdf0e10cSrcweir 		Gdiplus::Graphics aGraphics(mhDC);
201*cdf0e10cSrcweir 		const sal_uInt8 aTrans = (sal_uInt8)basegfx::fround( 255 * (1.0 - fTransparency) );
202*cdf0e10cSrcweir 		Gdiplus::Color aTestColor(aTrans, SALCOLOR_RED(maLineColor), SALCOLOR_GREEN(maLineColor), SALCOLOR_BLUE(maLineColor));
203*cdf0e10cSrcweir 		Gdiplus::Pen aTestPen(aTestColor, Gdiplus::REAL(rLineWidths.getX()));
204*cdf0e10cSrcweir 		Gdiplus::GraphicsPath aPath;
205*cdf0e10cSrcweir 		bool bNoLineJoin(false);
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 		switch(eLineJoin)
208*cdf0e10cSrcweir 		{
209*cdf0e10cSrcweir 			default : // basegfx::B2DLINEJOIN_NONE :
210*cdf0e10cSrcweir 			{
211*cdf0e10cSrcweir 				if(basegfx::fTools::more(rLineWidths.getX(), 0.0))
212*cdf0e10cSrcweir 				{
213*cdf0e10cSrcweir 					bNoLineJoin = true;
214*cdf0e10cSrcweir 				}
215*cdf0e10cSrcweir 				break;
216*cdf0e10cSrcweir 			}
217*cdf0e10cSrcweir 			case basegfx::B2DLINEJOIN_BEVEL :
218*cdf0e10cSrcweir 			{
219*cdf0e10cSrcweir 				aTestPen.SetLineJoin(Gdiplus::LineJoinBevel);
220*cdf0e10cSrcweir 				break;
221*cdf0e10cSrcweir 			}
222*cdf0e10cSrcweir 			case basegfx::B2DLINEJOIN_MIDDLE :
223*cdf0e10cSrcweir 			case basegfx::B2DLINEJOIN_MITER :
224*cdf0e10cSrcweir 			{
225*cdf0e10cSrcweir 				const Gdiplus::REAL aMiterLimit(15.0);
226*cdf0e10cSrcweir 				aTestPen.SetMiterLimit(aMiterLimit);
227*cdf0e10cSrcweir 				aTestPen.SetLineJoin(Gdiplus::LineJoinMiter);
228*cdf0e10cSrcweir 				break;
229*cdf0e10cSrcweir 			}
230*cdf0e10cSrcweir 			case basegfx::B2DLINEJOIN_ROUND :
231*cdf0e10cSrcweir 			{
232*cdf0e10cSrcweir 				aTestPen.SetLineJoin(Gdiplus::LineJoinRound);
233*cdf0e10cSrcweir 				break;
234*cdf0e10cSrcweir 			}
235*cdf0e10cSrcweir 		}
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 		if(nCount > 250 && basegfx::fTools::more(rLineWidths.getX(), 1.5))
238*cdf0e10cSrcweir         {
239*cdf0e10cSrcweir     		impAddB2DPolygonToGDIPlusGraphicsPathInteger(aPath, rPolygon, bNoLineJoin);
240*cdf0e10cSrcweir         }
241*cdf0e10cSrcweir         else
242*cdf0e10cSrcweir         {
243*cdf0e10cSrcweir     		impAddB2DPolygonToGDIPlusGraphicsPathReal(aPath, rPolygon, bNoLineJoin);
244*cdf0e10cSrcweir         }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir         if(rPolygon.isClosed() && !bNoLineJoin)
247*cdf0e10cSrcweir         {
248*cdf0e10cSrcweir             // #i101491# needed to create the correct line joins
249*cdf0e10cSrcweir             aPath.CloseFigure();
250*cdf0e10cSrcweir         }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir         if(getAntiAliasB2DDraw())
253*cdf0e10cSrcweir         {
254*cdf0e10cSrcweir     		aGraphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
255*cdf0e10cSrcweir         }
256*cdf0e10cSrcweir         else
257*cdf0e10cSrcweir         {
258*cdf0e10cSrcweir     		aGraphics.SetSmoothingMode(Gdiplus::SmoothingModeNone);
259*cdf0e10cSrcweir         }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 		aGraphics.DrawPath(&aTestPen, &aPath);
262*cdf0e10cSrcweir 	}
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir 	return true;
265*cdf0e10cSrcweir }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir // -----------------------------------------------------------------------
268