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