19f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59f62ea84SAndrew Rist  * distributed with this work for additional information
69f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
99f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
109f62ea84SAndrew Rist  *
119f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129f62ea84SAndrew Rist  *
139f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149f62ea84SAndrew Rist  * software distributed under the License is distributed on an
159f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
179f62ea84SAndrew Rist  * specific language governing permissions and limitations
189f62ea84SAndrew Rist  * under the License.
199f62ea84SAndrew Rist  *
209f62ea84SAndrew Rist  *************************************************************/
219f62ea84SAndrew Rist 
229f62ea84SAndrew 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 
190*5aaf853bSArmin Le Grand bool WinSalGraphics::drawPolyLine(
191*5aaf853bSArmin Le Grand     const basegfx::B2DPolygon& rPolygon,
192*5aaf853bSArmin Le Grand     double fTransparency,
193*5aaf853bSArmin Le Grand     const basegfx::B2DVector& rLineWidths,
194*5aaf853bSArmin Le Grand     basegfx::B2DLineJoin eLineJoin,
195*5aaf853bSArmin Le Grand     com::sun::star::drawing::LineCap eLineCap)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir     const sal_uInt32 nCount(rPolygon.count());
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 	if(mbPen && nCount)
200cdf0e10cSrcweir 	{
201cdf0e10cSrcweir 		Gdiplus::Graphics aGraphics(mhDC);
202cdf0e10cSrcweir 		const sal_uInt8 aTrans = (sal_uInt8)basegfx::fround( 255 * (1.0 - fTransparency) );
203cdf0e10cSrcweir 		Gdiplus::Color aTestColor(aTrans, SALCOLOR_RED(maLineColor), SALCOLOR_GREEN(maLineColor), SALCOLOR_BLUE(maLineColor));
204cdf0e10cSrcweir 		Gdiplus::Pen aTestPen(aTestColor, Gdiplus::REAL(rLineWidths.getX()));
205cdf0e10cSrcweir 		Gdiplus::GraphicsPath aPath;
206cdf0e10cSrcweir 		bool bNoLineJoin(false);
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 		switch(eLineJoin)
209cdf0e10cSrcweir 		{
210cdf0e10cSrcweir 			default : // basegfx::B2DLINEJOIN_NONE :
211cdf0e10cSrcweir 			{
212cdf0e10cSrcweir 				if(basegfx::fTools::more(rLineWidths.getX(), 0.0))
213cdf0e10cSrcweir 				{
214cdf0e10cSrcweir 					bNoLineJoin = true;
215cdf0e10cSrcweir 				}
216cdf0e10cSrcweir 				break;
217cdf0e10cSrcweir 			}
218cdf0e10cSrcweir 			case basegfx::B2DLINEJOIN_BEVEL :
219cdf0e10cSrcweir 			{
220cdf0e10cSrcweir 				aTestPen.SetLineJoin(Gdiplus::LineJoinBevel);
221cdf0e10cSrcweir 				break;
222cdf0e10cSrcweir 			}
223cdf0e10cSrcweir 			case basegfx::B2DLINEJOIN_MIDDLE :
224cdf0e10cSrcweir 			case basegfx::B2DLINEJOIN_MITER :
225cdf0e10cSrcweir 			{
226cdf0e10cSrcweir 				const Gdiplus::REAL aMiterLimit(15.0);
227cdf0e10cSrcweir 				aTestPen.SetMiterLimit(aMiterLimit);
228cdf0e10cSrcweir 				aTestPen.SetLineJoin(Gdiplus::LineJoinMiter);
229cdf0e10cSrcweir 				break;
230cdf0e10cSrcweir 			}
231cdf0e10cSrcweir 			case basegfx::B2DLINEJOIN_ROUND :
232cdf0e10cSrcweir 			{
233cdf0e10cSrcweir 				aTestPen.SetLineJoin(Gdiplus::LineJoinRound);
234cdf0e10cSrcweir 				break;
235cdf0e10cSrcweir 			}
236cdf0e10cSrcweir 		}
237cdf0e10cSrcweir 
238*5aaf853bSArmin Le Grand         switch(eLineCap)
239*5aaf853bSArmin Le Grand         {
240*5aaf853bSArmin Le Grand             default: /*com::sun::star::drawing::LineCap_BUTT*/
241*5aaf853bSArmin Le Grand             {
242*5aaf853bSArmin Le Grand                 // nothing to do
243*5aaf853bSArmin Le Grand                 break;
244*5aaf853bSArmin Le Grand             }
245*5aaf853bSArmin Le Grand             case com::sun::star::drawing::LineCap_ROUND:
246*5aaf853bSArmin Le Grand             {
247*5aaf853bSArmin Le Grand                 aTestPen.SetStartCap(Gdiplus::LineCapRound);
248*5aaf853bSArmin Le Grand                 aTestPen.SetEndCap(Gdiplus::LineCapRound);
249*5aaf853bSArmin Le Grand                 break;
250*5aaf853bSArmin Le Grand             }
251*5aaf853bSArmin Le Grand             case com::sun::star::drawing::LineCap_SQUARE:
252*5aaf853bSArmin Le Grand             {
253*5aaf853bSArmin Le Grand                 aTestPen.SetStartCap(Gdiplus::LineCapSquare);
254*5aaf853bSArmin Le Grand                 aTestPen.SetEndCap(Gdiplus::LineCapSquare);
255*5aaf853bSArmin Le Grand                 break;
256*5aaf853bSArmin Le Grand             }
257*5aaf853bSArmin Le Grand         }
258*5aaf853bSArmin Le Grand 
259cdf0e10cSrcweir 		if(nCount > 250 && basegfx::fTools::more(rLineWidths.getX(), 1.5))
260cdf0e10cSrcweir         {
261cdf0e10cSrcweir     		impAddB2DPolygonToGDIPlusGraphicsPathInteger(aPath, rPolygon, bNoLineJoin);
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir         else
264cdf0e10cSrcweir         {
265cdf0e10cSrcweir     		impAddB2DPolygonToGDIPlusGraphicsPathReal(aPath, rPolygon, bNoLineJoin);
266cdf0e10cSrcweir         }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir         if(rPolygon.isClosed() && !bNoLineJoin)
269cdf0e10cSrcweir         {
270cdf0e10cSrcweir             // #i101491# needed to create the correct line joins
271cdf0e10cSrcweir             aPath.CloseFigure();
272cdf0e10cSrcweir         }
273cdf0e10cSrcweir 
274cdf0e10cSrcweir         if(getAntiAliasB2DDraw())
275cdf0e10cSrcweir         {
276cdf0e10cSrcweir     		aGraphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
277cdf0e10cSrcweir         }
278cdf0e10cSrcweir         else
279cdf0e10cSrcweir         {
280cdf0e10cSrcweir     		aGraphics.SetSmoothingMode(Gdiplus::SmoothingModeNone);
281cdf0e10cSrcweir         }
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 		aGraphics.DrawPath(&aTestPen, &aPath);
284cdf0e10cSrcweir 	}
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 	return true;
287cdf0e10cSrcweir }
288cdf0e10cSrcweir 
289cdf0e10cSrcweir // -----------------------------------------------------------------------
290