gdi32: Fix the sign of the vertical text position in ExtTextOutW.
[wine] / dlls / gdi32 / painting.c
1 /*
2  * GDI drawing functions.
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  * Copyright 1997 Bertho A. Stultiens
6  *           1999 Huw D M Davies
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winerror.h"
34 #include "gdi_private.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
38
39
40 /***********************************************************************
41  *           LineTo    (GDI32.@)
42  */
43 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
44 {
45     DC * dc = get_dc_ptr( hdc );
46     BOOL ret;
47
48     if(!dc) return FALSE;
49
50     update_dc( dc );
51     if(PATH_IsPathOpen(dc->path))
52         ret = PATH_LineTo(dc, x, y);
53     else
54         ret = dc->funcs->pLineTo && dc->funcs->pLineTo(dc->physDev,x,y);
55     if(ret) {
56         dc->CursPosX = x;
57         dc->CursPosY = y;
58     }
59     release_dc_ptr( dc );
60     return ret;
61 }
62
63
64 /***********************************************************************
65  *           MoveToEx    (GDI32.@)
66  */
67 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
68 {
69     BOOL ret = TRUE;
70     DC * dc = get_dc_ptr( hdc );
71
72     if(!dc) return FALSE;
73
74     if(pt) {
75         pt->x = dc->CursPosX;
76         pt->y = dc->CursPosY;
77     }
78     dc->CursPosX = x;
79     dc->CursPosY = y;
80
81     if(PATH_IsPathOpen(dc->path)) ret = PATH_MoveTo(dc);
82     else if (dc->funcs->pMoveTo) ret = dc->funcs->pMoveTo(dc->physDev,x,y);
83     release_dc_ptr( dc );
84     return ret;
85 }
86
87
88 /***********************************************************************
89  *           Arc    (GDI32.@)
90  */
91 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
92                      INT bottom, INT xstart, INT ystart,
93                      INT xend, INT yend )
94 {
95     BOOL ret = FALSE;
96     DC * dc = get_dc_ptr( hdc );
97
98     if (dc)
99     {
100         update_dc( dc );
101         if(PATH_IsPathOpen(dc->path))
102             ret = PATH_Arc(dc, left, top, right, bottom, xstart, ystart, xend, yend,0);
103         else if (dc->funcs->pArc)
104             ret = dc->funcs->pArc(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
105         release_dc_ptr( dc );
106     }
107     return ret;
108 }
109
110 /***********************************************************************
111  *           ArcTo    (GDI32.@)
112  */
113 BOOL WINAPI ArcTo( HDC hdc,
114                      INT left,   INT top,
115                      INT right,  INT bottom,
116                      INT xstart, INT ystart,
117                      INT xend,   INT yend )
118 {
119     double width = fabs(right-left),
120         height = fabs(bottom-top),
121         xradius = width/2,
122         yradius = height/2,
123         xcenter = right > left ? left+xradius : right+xradius,
124         ycenter = bottom > top ? top+yradius : bottom+yradius,
125         angle;
126     BOOL result;
127     DC * dc = get_dc_ptr( hdc );
128     if(!dc) return FALSE;
129
130     update_dc( dc );
131     if(PATH_IsPathOpen(dc->path))
132         result = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,-1);
133     else if(dc->funcs->pArcTo)
134         result = dc->funcs->pArcTo( dc->physDev, left, top, right, bottom,
135                                   xstart, ystart, xend, yend );
136     else /* We'll draw a line from the current position to the starting point of the arc, then draw the arc */
137     {
138         angle = atan2(((ystart-ycenter)/height),
139                       ((xstart-xcenter)/width));
140         LineTo(hdc, GDI_ROUND(xcenter+(cos(angle)*xradius)),
141                GDI_ROUND(ycenter+(sin(angle)*yradius)));
142         result = Arc(hdc, left, top, right, bottom, xstart, ystart, xend, yend);
143     }
144     if (result) {
145         angle = atan2(((yend-ycenter)/height),
146                       ((xend-xcenter)/width));
147         dc->CursPosX = GDI_ROUND(xcenter+(cos(angle)*xradius));
148         dc->CursPosY = GDI_ROUND(ycenter+(sin(angle)*yradius));
149     }
150     release_dc_ptr( dc );
151     return result;
152 }
153
154
155 /***********************************************************************
156  *           Pie   (GDI32.@)
157  */
158 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
159                      INT right, INT bottom, INT xstart, INT ystart,
160                      INT xend, INT yend )
161 {
162     BOOL ret = FALSE;
163     DC * dc = get_dc_ptr( hdc );
164     if (!dc) return FALSE;
165
166     update_dc( dc );
167     if(PATH_IsPathOpen(dc->path))
168         ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,2);
169     else if(dc->funcs->pPie)
170         ret = dc->funcs->pPie(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
171
172     release_dc_ptr( dc );
173     return ret;
174 }
175
176
177 /***********************************************************************
178  *           Chord    (GDI32.@)
179  */
180 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
181                        INT right, INT bottom, INT xstart, INT ystart,
182                        INT xend, INT yend )
183 {
184     BOOL ret = FALSE;
185     DC * dc = get_dc_ptr( hdc );
186     if (!dc) return FALSE;
187
188     update_dc( dc );
189     if(PATH_IsPathOpen(dc->path))
190         ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,1);
191     else if(dc->funcs->pChord)
192         ret = dc->funcs->pChord(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
193
194     release_dc_ptr( dc );
195     return ret;
196 }
197
198
199 /***********************************************************************
200  *           Ellipse    (GDI32.@)
201  */
202 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
203                          INT right, INT bottom )
204 {
205     BOOL ret = FALSE;
206     DC * dc = get_dc_ptr( hdc );
207     if (!dc) return FALSE;
208
209     update_dc( dc );
210     if(PATH_IsPathOpen(dc->path))
211         ret = PATH_Ellipse(dc,left,top,right,bottom);
212     else if (dc->funcs->pEllipse)
213         ret = dc->funcs->pEllipse(dc->physDev,left,top,right,bottom);
214
215     release_dc_ptr( dc );
216     return ret;
217 }
218
219
220 /***********************************************************************
221  *           Rectangle    (GDI32.@)
222  */
223 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
224                            INT right, INT bottom )
225 {
226     BOOL ret = FALSE;
227     DC * dc = get_dc_ptr( hdc );
228
229     if (dc)
230     {
231         update_dc( dc );
232         if(PATH_IsPathOpen(dc->path))
233             ret = PATH_Rectangle(dc, left, top, right, bottom);
234         else if (dc->funcs->pRectangle)
235             ret = dc->funcs->pRectangle(dc->physDev,left,top,right,bottom);
236         release_dc_ptr( dc );
237     }
238     return ret;
239 }
240
241
242 /***********************************************************************
243  *           RoundRect    (GDI32.@)
244  */
245 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
246                            INT bottom, INT ell_width, INT ell_height )
247 {
248     BOOL ret = FALSE;
249     DC *dc = get_dc_ptr( hdc );
250
251     if (dc)
252     {
253         update_dc( dc );
254         if(PATH_IsPathOpen(dc->path))
255             ret = PATH_RoundRect(dc,left,top,right,bottom,ell_width,ell_height);
256         else if (dc->funcs->pRoundRect)
257             ret = dc->funcs->pRoundRect(dc->physDev,left,top,right,bottom,ell_width,ell_height);
258         release_dc_ptr( dc );
259     }
260     return ret;
261 }
262
263 /***********************************************************************
264  *           SetPixel    (GDI32.@)
265  */
266 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
267 {
268     COLORREF ret = 0;
269     DC * dc = get_dc_ptr( hdc );
270
271     if (dc)
272     {
273         update_dc( dc );
274         if (dc->funcs->pSetPixel) ret = dc->funcs->pSetPixel(dc->physDev,x,y,color);
275         release_dc_ptr( dc );
276     }
277     return ret;
278 }
279
280 /***********************************************************************
281  *           SetPixelV    (GDI32.@)
282  */
283 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
284 {
285     BOOL ret = FALSE;
286     DC * dc = get_dc_ptr( hdc );
287
288     if (dc)
289     {
290         update_dc( dc );
291         if (dc->funcs->pSetPixel)
292         {
293             dc->funcs->pSetPixel(dc->physDev,x,y,color);
294             ret = TRUE;
295         }
296         release_dc_ptr( dc );
297     }
298     return ret;
299 }
300
301 /***********************************************************************
302  *           GetPixel    (GDI32.@)
303  */
304 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
305 {
306     COLORREF ret = CLR_INVALID;
307     DC * dc = get_dc_ptr( hdc );
308
309     if (dc)
310     {
311         update_dc( dc );
312         /* FIXME: should this be in the graphics driver? */
313         if (PtVisible( hdc, x, y ))
314         {
315             if (dc->funcs->pGetPixel) ret = dc->funcs->pGetPixel(dc->physDev,x,y);
316         }
317         release_dc_ptr( dc );
318     }
319     return ret;
320 }
321
322
323 /******************************************************************************
324  * ChoosePixelFormat [GDI32.@]
325  * Matches a pixel format to given format
326  *
327  * PARAMS
328  *    hdc  [I] Device context to search for best pixel match
329  *    ppfd [I] Pixel format for which a match is sought
330  *
331  * RETURNS
332  *    Success: Pixel format index closest to given format
333  *    Failure: 0
334  */
335 INT WINAPI ChoosePixelFormat( HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd )
336 {
337     INT ret = 0;
338     DC * dc = get_dc_ptr( hdc );
339
340     TRACE("(%p,%p)\n",hdc,ppfd);
341
342     if (!dc) return 0;
343
344     if (!dc->funcs->pChoosePixelFormat) FIXME(" :stub\n");
345     else ret = dc->funcs->pChoosePixelFormat(dc->physDev,ppfd);
346
347     release_dc_ptr( dc );
348     return ret;
349 }
350
351
352 /******************************************************************************
353  * SetPixelFormat [GDI32.@]
354  * Sets pixel format of device context
355  *
356  * PARAMS
357  *    hdc          [I] Device context to search for best pixel match
358  *    iPixelFormat [I] Pixel format index
359  *    ppfd         [I] Pixel format for which a match is sought
360  *
361  * RETURNS
362  *    Success: TRUE
363  *    Failure: FALSE
364  */
365 BOOL WINAPI SetPixelFormat( HDC hdc, INT iPixelFormat,
366                             const PIXELFORMATDESCRIPTOR *ppfd)
367 {
368     INT bRet = FALSE;
369     DC * dc = get_dc_ptr( hdc );
370
371     TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
372
373     if (!dc) return 0;
374
375     update_dc( dc );
376     if (!dc->funcs->pSetPixelFormat) FIXME(" :stub\n");
377     else bRet = dc->funcs->pSetPixelFormat(dc->physDev,iPixelFormat,ppfd);
378
379     release_dc_ptr( dc );
380     return bRet;
381 }
382
383
384 /******************************************************************************
385  * GetPixelFormat [GDI32.@]
386  * Gets index of pixel format of DC
387  *
388  * PARAMETERS
389  *    hdc [I] Device context whose pixel format index is sought
390  *
391  * RETURNS
392  *    Success: Currently selected pixel format
393  *    Failure: 0
394  */
395 INT WINAPI GetPixelFormat( HDC hdc )
396 {
397     INT ret = 0;
398     DC * dc = get_dc_ptr( hdc );
399
400     TRACE("(%p)\n",hdc);
401
402     if (!dc) return 0;
403
404     update_dc( dc );
405     if (!dc->funcs->pGetPixelFormat) FIXME(" :stub\n");
406     else ret = dc->funcs->pGetPixelFormat(dc->physDev);
407
408     release_dc_ptr( dc );
409     return ret;
410 }
411
412
413 /******************************************************************************
414  * DescribePixelFormat [GDI32.@]
415  * Gets info about pixel format from DC
416  *
417  * PARAMS
418  *    hdc          [I] Device context
419  *    iPixelFormat [I] Pixel format selector
420  *    nBytes       [I] Size of buffer
421  *    ppfd         [O] Pointer to structure to receive pixel format data
422  *
423  * RETURNS
424  *    Success: Maximum pixel format index of the device context
425  *    Failure: 0
426  */
427 INT WINAPI DescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
428                                 LPPIXELFORMATDESCRIPTOR ppfd )
429 {
430     INT ret = 0;
431     DC * dc = get_dc_ptr( hdc );
432
433     TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
434
435     if (!dc) return 0;
436
437     update_dc( dc );
438     if (!dc->funcs->pDescribePixelFormat)
439     {
440         FIXME(" :stub\n");
441         ppfd->nSize = nBytes;
442         ppfd->nVersion = 1;
443         ret = 3;
444     }
445     else ret = dc->funcs->pDescribePixelFormat(dc->physDev,iPixelFormat,nBytes,ppfd);
446
447     release_dc_ptr( dc );
448     return ret;
449 }
450
451
452 /******************************************************************************
453  * SwapBuffers [GDI32.@]
454  * Exchanges front and back buffers of window
455  *
456  * PARAMS
457  *    hdc [I] Device context whose buffers get swapped
458  *
459  * RETURNS
460  *    Success: TRUE
461  *    Failure: FALSE
462  */
463 BOOL WINAPI SwapBuffers( HDC hdc )
464 {
465     INT bRet = FALSE;
466     DC * dc = get_dc_ptr( hdc );
467
468     TRACE("(%p)\n",hdc);
469
470     if (!dc) return TRUE;
471
472     update_dc( dc );
473     if (!dc->funcs->pSwapBuffers)
474     {
475         FIXME(" :stub\n");
476         bRet = TRUE;
477     }
478     else bRet = dc->funcs->pSwapBuffers(dc->physDev);
479
480     release_dc_ptr( dc );
481     return bRet;
482 }
483
484
485 /***********************************************************************
486  *           PaintRgn    (GDI32.@)
487  */
488 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
489 {
490     BOOL ret = FALSE;
491     DC * dc = get_dc_ptr( hdc );
492
493     if (dc)
494     {
495         update_dc( dc );
496         if (dc->funcs->pPaintRgn) ret = dc->funcs->pPaintRgn(dc->physDev,hrgn);
497         release_dc_ptr( dc );
498     }
499     return ret;
500 }
501
502
503 /***********************************************************************
504  *           FillRgn    (GDI32.@)
505  */
506 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
507 {
508     BOOL retval = FALSE;
509     HBRUSH prevBrush;
510     DC * dc = get_dc_ptr( hdc );
511
512     if (!dc) return FALSE;
513     if(dc->funcs->pFillRgn)
514     {
515         update_dc( dc );
516         retval = dc->funcs->pFillRgn(dc->physDev, hrgn, hbrush);
517     }
518     else if ((prevBrush = SelectObject( hdc, hbrush )))
519     {
520         retval = PaintRgn( hdc, hrgn );
521         SelectObject( hdc, prevBrush );
522     }
523     release_dc_ptr( dc );
524     return retval;
525 }
526
527
528 /***********************************************************************
529  *           FrameRgn     (GDI32.@)
530  */
531 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
532                           INT nWidth, INT nHeight )
533 {
534     BOOL ret = FALSE;
535     DC *dc = get_dc_ptr( hdc );
536
537     if (!dc) return FALSE;
538
539     if(dc->funcs->pFrameRgn)
540     {
541         update_dc( dc );
542         ret = dc->funcs->pFrameRgn( dc->physDev, hrgn, hbrush, nWidth, nHeight );
543     }
544     else
545     {
546         HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
547         if (tmp)
548         {
549             if (REGION_FrameRgn( tmp, hrgn, nWidth, nHeight ))
550             {
551                 FillRgn( hdc, tmp, hbrush );
552                 ret = TRUE;
553             }
554             DeleteObject( tmp );
555         }
556     }
557     release_dc_ptr( dc );
558     return ret;
559 }
560
561
562 /***********************************************************************
563  *           InvertRgn    (GDI32.@)
564  */
565 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
566 {
567     HBRUSH prevBrush;
568     INT prevROP;
569     BOOL retval;
570     DC *dc = get_dc_ptr( hdc );
571     if (!dc) return FALSE;
572
573     if(dc->funcs->pInvertRgn)
574     {
575         update_dc( dc );
576         retval = dc->funcs->pInvertRgn( dc->physDev, hrgn );
577     }
578     else
579     {
580         prevBrush = SelectObject( hdc, GetStockObject(BLACK_BRUSH) );
581         prevROP = SetROP2( hdc, R2_NOT );
582         retval = PaintRgn( hdc, hrgn );
583         SelectObject( hdc, prevBrush );
584         SetROP2( hdc, prevROP );
585     }
586     release_dc_ptr( dc );
587     return retval;
588 }
589
590
591 /**********************************************************************
592  *          Polyline   (GDI32.@)
593  */
594 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
595 {
596     BOOL ret = FALSE;
597     DC * dc = get_dc_ptr( hdc );
598
599     if (dc)
600     {
601         update_dc( dc );
602         if (PATH_IsPathOpen(dc->path)) ret = PATH_Polyline(dc, pt, count);
603         else if (dc->funcs->pPolyline) ret = dc->funcs->pPolyline(dc->physDev,pt,count);
604         release_dc_ptr( dc );
605     }
606     return ret;
607 }
608
609 /**********************************************************************
610  *          PolylineTo   (GDI32.@)
611  */
612 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
613 {
614     DC * dc = get_dc_ptr( hdc );
615     BOOL ret = FALSE;
616
617     if(!dc) return FALSE;
618
619     if(PATH_IsPathOpen(dc->path))
620     {
621         update_dc( dc );
622         ret = PATH_PolylineTo(dc, pt, cCount);
623     }
624     else if(dc->funcs->pPolylineTo)
625     {
626         update_dc( dc );
627         ret = dc->funcs->pPolylineTo(dc->physDev, pt, cCount);
628     }
629     else /* do it using Polyline */
630     {
631         POINT *pts = HeapAlloc( GetProcessHeap(), 0,
632                                 sizeof(POINT) * (cCount + 1) );
633         if (pts)
634         {
635             pts[0].x = dc->CursPosX;
636             pts[0].y = dc->CursPosY;
637             memcpy( pts + 1, pt, sizeof(POINT) * cCount );
638             ret = Polyline( hdc, pts, cCount + 1 );
639             HeapFree( GetProcessHeap(), 0, pts );
640         }
641     }
642     if(ret) {
643         dc->CursPosX = pt[cCount-1].x;
644         dc->CursPosY = pt[cCount-1].y;
645     }
646     release_dc_ptr( dc );
647     return ret;
648 }
649
650
651 /**********************************************************************
652  *          Polygon  (GDI32.@)
653  */
654 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
655 {
656     BOOL ret = FALSE;
657     DC * dc = get_dc_ptr( hdc );
658
659     if (dc)
660     {
661         update_dc( dc );
662         if (PATH_IsPathOpen(dc->path)) ret = PATH_Polygon(dc, pt, count);
663         else if (dc->funcs->pPolygon) ret = dc->funcs->pPolygon(dc->physDev,pt,count);
664         release_dc_ptr( dc );
665     }
666     return ret;
667 }
668
669
670 /**********************************************************************
671  *          PolyPolygon  (GDI32.@)
672  */
673 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
674                              UINT polygons )
675 {
676     BOOL ret = FALSE;
677     DC * dc = get_dc_ptr( hdc );
678
679     if (dc)
680     {
681         update_dc( dc );
682         if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolygon(dc, pt, counts, polygons);
683         else if (dc->funcs->pPolyPolygon) ret = dc->funcs->pPolyPolygon(dc->physDev,pt,counts,polygons);
684         release_dc_ptr( dc );
685     }
686     return ret;
687 }
688
689 /**********************************************************************
690  *          PolyPolyline  (GDI32.@)
691  */
692 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
693                             DWORD polylines )
694 {
695     BOOL ret = FALSE;
696     DC * dc = get_dc_ptr( hdc );
697
698     if (dc)
699     {
700         update_dc( dc );
701         if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolyline(dc, pt, counts, polylines);
702         else if (dc->funcs->pPolyPolyline) ret = dc->funcs->pPolyPolyline(dc->physDev,pt,counts,polylines);
703         release_dc_ptr( dc );
704     }
705     return ret;
706 }
707
708 /**********************************************************************
709  *          ExtFloodFill   (GDI32.@)
710  */
711 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
712                               UINT fillType )
713 {
714     BOOL ret = FALSE;
715     DC * dc = get_dc_ptr( hdc );
716
717     if (dc)
718     {
719         update_dc( dc );
720         if (dc->funcs->pExtFloodFill) ret = dc->funcs->pExtFloodFill(dc->physDev,x,y,color,fillType);
721         release_dc_ptr( dc );
722     }
723     return ret;
724 }
725
726
727 /**********************************************************************
728  *          FloodFill   (GDI32.@)
729  */
730 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
731 {
732     return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
733 }
734
735
736 /******************************************************************************
737  * PolyBezier [GDI32.@]
738  * Draws one or more Bezier curves
739  *
740  * PARAMS
741  *    hDc     [I] Handle to device context
742  *    lppt    [I] Pointer to endpoints and control points
743  *    cPoints [I] Count of endpoints and control points
744  *
745  * RETURNS
746  *    Success: TRUE
747  *    Failure: FALSE
748  */
749 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
750 {
751     BOOL ret = FALSE;
752     DC * dc;
753
754     /* cPoints must be 3 * n + 1 (where n>=1) */
755     if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
756
757     dc = get_dc_ptr( hdc );
758     if(!dc) return FALSE;
759
760     if(PATH_IsPathOpen(dc->path))
761     {
762         update_dc( dc );
763         ret = PATH_PolyBezier(dc, lppt, cPoints);
764     }
765     else if (dc->funcs->pPolyBezier)
766     {
767         update_dc( dc );
768         ret = dc->funcs->pPolyBezier(dc->physDev, lppt, cPoints);
769     }
770     else  /* We'll convert it into line segments and draw them using Polyline */
771     {
772         POINT *Pts;
773         INT nOut;
774
775         if ((Pts = GDI_Bezier( lppt, cPoints, &nOut )))
776         {
777             TRACE("Pts = %p, no = %d\n", Pts, nOut);
778             ret = Polyline( hdc, Pts, nOut );
779             HeapFree( GetProcessHeap(), 0, Pts );
780         }
781     }
782
783     release_dc_ptr( dc );
784     return ret;
785 }
786
787 /******************************************************************************
788  * PolyBezierTo [GDI32.@]
789  * Draws one or more Bezier curves
790  *
791  * PARAMS
792  *    hDc     [I] Handle to device context
793  *    lppt    [I] Pointer to endpoints and control points
794  *    cPoints [I] Count of endpoints and control points
795  *
796  * RETURNS
797  *    Success: TRUE
798  *    Failure: FALSE
799  */
800 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
801 {
802     DC * dc;
803     BOOL ret = FALSE;
804
805     /* cbPoints must be 3 * n (where n>=1) */
806     if (!cPoints || (cPoints % 3) != 0) return FALSE;
807
808     dc = get_dc_ptr( hdc );
809     if(!dc) return FALSE;
810
811     if(PATH_IsPathOpen(dc->path))
812     {
813         update_dc( dc );
814         ret = PATH_PolyBezierTo(dc, lppt, cPoints);
815     }
816     else if(dc->funcs->pPolyBezierTo)
817     {
818         update_dc( dc );
819         ret = dc->funcs->pPolyBezierTo(dc->physDev, lppt, cPoints);
820     }
821     else  /* We'll do it using PolyBezier */
822     {
823         POINT *pt = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (cPoints + 1) );
824         if(pt)
825         {
826             pt[0].x = dc->CursPosX;
827             pt[0].y = dc->CursPosY;
828             memcpy(pt + 1, lppt, sizeof(POINT) * cPoints);
829             ret = PolyBezier(hdc, pt, cPoints+1);
830             HeapFree( GetProcessHeap(), 0, pt );
831         }
832     }
833     if(ret) {
834         dc->CursPosX = lppt[cPoints-1].x;
835         dc->CursPosY = lppt[cPoints-1].y;
836     }
837     release_dc_ptr( dc );
838     return ret;
839 }
840
841 /***********************************************************************
842  *      AngleArc (GDI32.@)
843  */
844 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
845 {
846     INT x1,y1,x2,y2, arcdir;
847     BOOL result;
848     DC *dc;
849
850     if( (signed int)dwRadius < 0 )
851         return FALSE;
852
853     dc = get_dc_ptr( hdc );
854     if(!dc) return FALSE;
855
856     /* Calculate the end point */
857     x2 = GDI_ROUND( x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius );
858     y2 = GDI_ROUND( y - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius );
859
860     if(!PATH_IsPathOpen(dc->path) && dc->funcs->pAngleArc)
861     {
862         update_dc( dc );
863         result = dc->funcs->pAngleArc( dc->physDev, x, y, dwRadius, eStartAngle, eSweepAngle );
864     }
865     else { /* do it using ArcTo */
866         x1 = GDI_ROUND( x + cos(eStartAngle*M_PI/180) * dwRadius );
867         y1 = GDI_ROUND( y - sin(eStartAngle*M_PI/180) * dwRadius );
868
869         arcdir = SetArcDirection( hdc, eSweepAngle >= 0 ? AD_COUNTERCLOCKWISE : AD_CLOCKWISE);
870         result = ArcTo( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
871                         x1, y1, x2, y2 );
872         SetArcDirection( hdc, arcdir );
873     }
874     if (result) {
875         dc->CursPosX = x2;
876         dc->CursPosY = y2;
877     }
878     release_dc_ptr( dc );
879     return result;
880 }
881
882 /***********************************************************************
883  *      PolyDraw (GDI32.@)
884  */
885 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
886                        DWORD cCount)
887 {
888     DC *dc;
889     BOOL result = FALSE;
890     POINT * line_pts = NULL, * bzr_pts = NULL, bzr[4];
891     INT i, num_pts, num_bzr_pts, space, size;
892
893     dc = get_dc_ptr( hdc );
894     if(!dc) return FALSE;
895
896     if( PATH_IsPathOpen( dc->path ) )
897     {
898         update_dc( dc );
899         result = PATH_PolyDraw(dc, lppt, lpbTypes, cCount);
900     }
901     else if(dc->funcs->pPolyDraw)
902     {
903         update_dc( dc );
904         result = dc->funcs->pPolyDraw( dc->physDev, lppt, lpbTypes, cCount );
905     }
906     else {
907         /* check for valid point types */
908         for(i = 0; i < cCount; i++) {
909             switch(lpbTypes[i]) {
910                 case PT_MOVETO:
911                 case PT_LINETO | PT_CLOSEFIGURE:
912                 case PT_LINETO:
913                     break;
914                 case PT_BEZIERTO:
915                     if((i + 2 < cCount) && (lpbTypes[i + 1] == PT_BEZIERTO) &&
916                         ((lpbTypes[i + 2] & ~PT_CLOSEFIGURE) == PT_BEZIERTO)){
917                         i += 2;
918                         break;
919                     }
920                 default:
921                     goto end;
922             }
923         }
924
925         space = cCount + 300;
926         line_pts = HeapAlloc(GetProcessHeap(), 0, space * sizeof(POINT));
927         num_pts = 1;
928
929         line_pts[0].x = dc->CursPosX;
930         line_pts[0].y = dc->CursPosY;
931
932         for(i = 0; i < cCount; i++) {
933             switch(lpbTypes[i]) {
934                 case PT_MOVETO:
935                     if(num_pts >= 2)
936                         Polyline(hdc, line_pts, num_pts);
937                     num_pts = 0;
938                     line_pts[num_pts++] = lppt[i];
939                     break;
940                 case PT_LINETO:
941                 case (PT_LINETO | PT_CLOSEFIGURE):
942                     line_pts[num_pts++] = lppt[i];
943                     break;
944                 case PT_BEZIERTO:
945                     bzr[0].x = line_pts[num_pts - 1].x;
946                     bzr[0].y = line_pts[num_pts - 1].y;
947                     memcpy(&bzr[1], &lppt[i], 3 * sizeof(POINT));
948
949                     bzr_pts = GDI_Bezier(bzr, 4, &num_bzr_pts);
950
951                     size = num_pts + (cCount - i) + num_bzr_pts;
952                     if(space < size){
953                         space = size * 2;
954                         line_pts = HeapReAlloc(GetProcessHeap(), 0, line_pts,
955                             space * sizeof(POINT));
956                     }
957                     memcpy(&line_pts[num_pts], &bzr_pts[1],
958                         (num_bzr_pts - 1) * sizeof(POINT));
959                     num_pts += num_bzr_pts - 1;
960                     HeapFree(GetProcessHeap(), 0, bzr_pts);
961                     i += 2;
962                     break;
963                 default:
964                     goto end;
965             }
966
967             if(lpbTypes[i] & PT_CLOSEFIGURE)
968                 line_pts[num_pts++] = line_pts[0];
969         }
970
971         if(num_pts >= 2)
972             Polyline(hdc, line_pts, num_pts);
973
974         MoveToEx(hdc, line_pts[num_pts - 1].x, line_pts[num_pts - 1].y, NULL);
975         HeapFree(GetProcessHeap(), 0, line_pts);
976         result = TRUE;
977     }
978
979 end:
980     release_dc_ptr( dc );
981     return result;
982 }
983
984
985 /**********************************************************************
986  *           LineDDA   (GDI32.@)
987  */
988 BOOL WINAPI LineDDA(INT nXStart, INT nYStart, INT nXEnd, INT nYEnd,
989                     LINEDDAPROC callback, LPARAM lParam )
990 {
991     INT xadd = 1, yadd = 1;
992     INT err,erradd;
993     INT cnt;
994     INT dx = nXEnd - nXStart;
995     INT dy = nYEnd - nYStart;
996
997     if (dx < 0)
998     {
999         dx = -dx;
1000         xadd = -1;
1001     }
1002     if (dy < 0)
1003     {
1004         dy = -dy;
1005         yadd = -1;
1006     }
1007     if (dx > dy)  /* line is "more horizontal" */
1008     {
1009         err = 2*dy - dx; erradd = 2*dy - 2*dx;
1010         for(cnt = 0;cnt < dx; cnt++)
1011         {
1012             callback(nXStart,nYStart,lParam);
1013             if (err > 0)
1014             {
1015                 nYStart += yadd;
1016                 err += erradd;
1017             }
1018             else err += 2*dy;
1019             nXStart += xadd;
1020         }
1021     }
1022     else   /* line is "more vertical" */
1023     {
1024         err = 2*dx - dy; erradd = 2*dx - 2*dy;
1025         for(cnt = 0;cnt < dy; cnt++)
1026         {
1027             callback(nXStart,nYStart,lParam);
1028             if (err > 0)
1029             {
1030                 nXStart += xadd;
1031                 err += erradd;
1032             }
1033             else err += 2*dx;
1034             nYStart += yadd;
1035         }
1036     }
1037     return TRUE;
1038 }
1039
1040
1041 /******************************************************************
1042  *
1043  *   *Very* simple bezier drawing code,
1044  *
1045  *   It uses a recursive algorithm to divide the curve in a series
1046  *   of straight line segments. Not ideal but sufficient for me.
1047  *   If you are in need for something better look for some incremental
1048  *   algorithm.
1049  *
1050  *   7 July 1998 Rein Klazes
1051  */
1052
1053  /*
1054   * some macro definitions for bezier drawing
1055   *
1056   * to avoid truncation errors the coordinates are
1057   * shifted upwards. When used in drawing they are
1058   * shifted down again, including correct rounding
1059   * and avoiding floating point arithmetic
1060   * 4 bits should allow 27 bits coordinates which I saw
1061   * somewhere in the win32 doc's
1062   *
1063   */
1064
1065 #define BEZIERSHIFTBITS 4
1066 #define BEZIERSHIFTUP(x)    ((x)<<BEZIERSHIFTBITS)
1067 #define BEZIERPIXEL        BEZIERSHIFTUP(1)
1068 #define BEZIERSHIFTDOWN(x)  (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
1069 /* maximum depth of recursion */
1070 #define BEZIERMAXDEPTH  8
1071
1072 /* size of array to store points on */
1073 /* enough for one curve */
1074 #define BEZIER_INITBUFSIZE    (150)
1075
1076 /* calculate Bezier average, in this case the middle
1077  * correctly rounded...
1078  * */
1079
1080 #define BEZIERMIDDLE(Mid, P1, P2) \
1081     (Mid).x=((P1).x+(P2).x + 1)/2;\
1082     (Mid).y=((P1).y+(P2).y + 1)/2;
1083
1084 /**********************************************************
1085 * BezierCheck helper function to check
1086 * that recursion can be terminated
1087 *       Points[0] and Points[3] are begin and endpoint
1088 *       Points[1] and Points[2] are control points
1089 *       level is the recursion depth
1090 *       returns true if the recursion can be terminated
1091 */
1092 static BOOL BezierCheck( int level, POINT *Points)
1093 {
1094     INT dx, dy;
1095     dx=Points[3].x-Points[0].x;
1096     dy=Points[3].y-Points[0].y;
1097     if(abs(dy)<=abs(dx)){/* shallow line */
1098         /* check that control points are between begin and end */
1099         if(Points[1].x < Points[0].x){
1100             if(Points[1].x < Points[3].x)
1101                 return FALSE;
1102         }else
1103             if(Points[1].x > Points[3].x)
1104                 return FALSE;
1105         if(Points[2].x < Points[0].x){
1106             if(Points[2].x < Points[3].x)
1107                 return FALSE;
1108         }else
1109             if(Points[2].x > Points[3].x)
1110                 return FALSE;
1111         dx=BEZIERSHIFTDOWN(dx);
1112         if(!dx) return TRUE;
1113         if(abs(Points[1].y-Points[0].y-(dy/dx)*
1114                 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1115            abs(Points[2].y-Points[0].y-(dy/dx)*
1116                    BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1117             return FALSE;
1118         else
1119             return TRUE;
1120     }else{ /* steep line */
1121         /* check that control points are between begin and end */
1122         if(Points[1].y < Points[0].y){
1123             if(Points[1].y < Points[3].y)
1124                 return FALSE;
1125         }else
1126             if(Points[1].y > Points[3].y)
1127                 return FALSE;
1128         if(Points[2].y < Points[0].y){
1129             if(Points[2].y < Points[3].y)
1130                 return FALSE;
1131         }else
1132             if(Points[2].y > Points[3].y)
1133                 return FALSE;
1134         dy=BEZIERSHIFTDOWN(dy);
1135         if(!dy) return TRUE;
1136         if(abs(Points[1].x-Points[0].x-(dx/dy)*
1137                 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1138            abs(Points[2].x-Points[0].x-(dx/dy)*
1139                    BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1140             return FALSE;
1141         else
1142             return TRUE;
1143     }
1144 }
1145
1146 /* Helper for GDI_Bezier.
1147  * Just handles one Bezier, so Points should point to four POINTs
1148  */
1149 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
1150                                 INT *nPtsOut, INT level )
1151 {
1152     if(*nPtsOut == *dwOut) {
1153         *dwOut *= 2;
1154         *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
1155                                *dwOut * sizeof(POINT) );
1156     }
1157
1158     if(!level || BezierCheck(level, Points)) {
1159         if(*nPtsOut == 0) {
1160             (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1161             (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1162             *nPtsOut = 1;
1163         }
1164         (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1165         (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1166         (*nPtsOut) ++;
1167     } else {
1168         POINT Points2[4]; /* for the second recursive call */
1169         Points2[3]=Points[3];
1170         BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1171         BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1172         BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1173
1174         BEZIERMIDDLE(Points[1], Points[0],  Points[1]);
1175         BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1176         BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1177
1178         Points2[0]=Points[3];
1179
1180         /* do the two halves */
1181         GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1182         GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1183     }
1184 }
1185
1186
1187
1188 /***********************************************************************
1189  *           GDI_Bezier   [INTERNAL]
1190  *   Calculate line segments that approximate -what microsoft calls- a bezier
1191  *   curve.
1192  *   The routine recursively divides the curve in two parts until a straight
1193  *   line can be drawn
1194  *
1195  *  PARAMS
1196  *
1197  *  Points  [I] Ptr to count POINTs which are the end and control points
1198  *              of the set of Bezier curves to flatten.
1199  *  count   [I] Number of Points.  Must be 3n+1.
1200  *  nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1201  *              lines+1).
1202  *
1203  *  RETURNS
1204  *
1205  *  Ptr to an array of POINTs that contain the lines that approximate the
1206  *  Beziers.  The array is allocated on the process heap and it is the caller's
1207  *  responsibility to HeapFree it. [this is not a particularly nice interface
1208  *  but since we can't know in advance how many points we will generate, the
1209  *  alternative would be to call the function twice, once to determine the size
1210  *  and a second time to do the work - I decided this was too much of a pain].
1211  */
1212 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1213 {
1214     POINT *out;
1215     INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1216
1217     if (count == 1 || (count - 1) % 3 != 0) {
1218         ERR("Invalid no. of points %d\n", count);
1219         return NULL;
1220     }
1221     *nPtsOut = 0;
1222     out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1223     for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1224         POINT ptBuf[4];
1225         memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1226         for(i = 0; i < 4; i++) {
1227             ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1228             ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1229         }
1230         GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1231     }
1232     TRACE("Produced %d points\n", *nPtsOut);
1233     return out;
1234 }
1235
1236 /******************************************************************************
1237  *           GdiGradientFill   (GDI32.@)
1238  *
1239  *  FIXME: we don't support the Alpha channel properly
1240  */
1241 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1242                           void * grad_array, ULONG ngrad, ULONG mode )
1243 {
1244   unsigned int i;
1245
1246   TRACE("vert_array:%p nvert:%d grad_array:%p ngrad:%d\n",
1247         vert_array, nvert, grad_array, ngrad);
1248
1249   switch(mode) 
1250     {
1251     case GRADIENT_FILL_RECT_H:
1252       for(i = 0; i < ngrad; i++) 
1253         {
1254           GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1255           TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1256           TRIVERTEX *v2 = vert_array + rect->LowerRight;
1257           int y1 = v1->y < v2->y ? v1->y : v2->y;
1258           int y2 = v2->y > v1->y ? v2->y : v1->y;
1259           int x, dx;
1260           if (v1->x > v2->x)
1261             {
1262               TRIVERTEX *t = v2;
1263               v2 = v1;
1264               v1 = t;
1265             }
1266           dx = v2->x - v1->x;
1267           for (x = 0; x < dx; x++)
1268             {
1269               POINT pts[2];
1270               HPEN hPen, hOldPen;
1271               
1272               hPen = CreatePen( PS_SOLID, 1, RGB(
1273                   (v1->Red   * (dx - x) + v2->Red   * x) / dx >> 8,
1274                   (v1->Green * (dx - x) + v2->Green * x) / dx >> 8,
1275                   (v1->Blue  * (dx - x) + v2->Blue  * x) / dx >> 8));
1276               hOldPen = SelectObject( hdc, hPen );
1277               pts[0].x = v1->x + x;
1278               pts[0].y = y1;
1279               pts[1].x = v1->x + x;
1280               pts[1].y = y2;
1281               Polyline( hdc, &pts[0], 2 );
1282               DeleteObject( SelectObject(hdc, hOldPen ) );
1283             }
1284         }
1285       break;
1286     case GRADIENT_FILL_RECT_V:
1287       for(i = 0; i < ngrad; i++) 
1288         {
1289           GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1290           TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1291           TRIVERTEX *v2 = vert_array + rect->LowerRight;
1292           int x1 = v1->x < v2->x ? v1->x : v2->x;
1293           int x2 = v2->x > v1->x ? v2->x : v1->x;
1294           int y, dy;
1295           if (v1->y > v2->y)
1296             {
1297               TRIVERTEX *t = v2;
1298               v2 = v1;
1299               v1 = t;
1300             }
1301           dy = v2->y - v1->y;
1302           for (y = 0; y < dy; y++)
1303             {
1304               POINT pts[2];
1305               HPEN hPen, hOldPen;
1306               
1307               hPen = CreatePen( PS_SOLID, 1, RGB(
1308                   (v1->Red   * (dy - y) + v2->Red   * y) / dy >> 8,
1309                   (v1->Green * (dy - y) + v2->Green * y) / dy >> 8,
1310                   (v1->Blue  * (dy - y) + v2->Blue  * y) / dy >> 8));
1311               hOldPen = SelectObject( hdc, hPen );
1312               pts[0].x = x1;
1313               pts[0].y = v1->y + y;
1314               pts[1].x = x2;
1315               pts[1].y = v1->y + y;
1316               Polyline( hdc, &pts[0], 2 );
1317               DeleteObject( SelectObject(hdc, hOldPen ) );
1318             }
1319         }
1320       break;
1321     case GRADIENT_FILL_TRIANGLE:
1322       for (i = 0; i < ngrad; i++)  
1323         {
1324           GRADIENT_TRIANGLE *tri = ((GRADIENT_TRIANGLE *)grad_array) + i;
1325           TRIVERTEX *v1 = vert_array + tri->Vertex1;
1326           TRIVERTEX *v2 = vert_array + tri->Vertex2;
1327           TRIVERTEX *v3 = vert_array + tri->Vertex3;
1328           int y, dy;
1329           
1330           if (v1->y > v2->y)
1331             { TRIVERTEX *t = v1; v1 = v2; v2 = t; }
1332           if (v2->y > v3->y)
1333             {
1334               TRIVERTEX *t = v2; v2 = v3; v3 = t;
1335               if (v1->y > v2->y)
1336                 { t = v1; v1 = v2; v2 = t; }
1337             }
1338           /* v1->y <= v2->y <= v3->y */
1339
1340           dy = v3->y - v1->y;
1341           for (y = 0; y < dy; y++)
1342             {
1343               /* v1->y <= y < v3->y */
1344               TRIVERTEX *v = y < (v2->y - v1->y) ? v1 : v3;
1345               /* (v->y <= y < v2->y) || (v2->y <= y < v->y) */
1346               int dy2 = v2->y - v->y;
1347               int y2 = y + v1->y - v->y;
1348
1349               int x1 = (v3->x     * y  + v1->x     * (dy  - y )) / dy;
1350               int x2 = (v2->x     * y2 + v-> x     * (dy2 - y2)) / dy2;
1351               int r1 = (v3->Red   * y  + v1->Red   * (dy  - y )) / dy;
1352               int r2 = (v2->Red   * y2 + v-> Red   * (dy2 - y2)) / dy2;
1353               int g1 = (v3->Green * y  + v1->Green * (dy  - y )) / dy;
1354               int g2 = (v2->Green * y2 + v-> Green * (dy2 - y2)) / dy2;
1355               int b1 = (v3->Blue  * y  + v1->Blue  * (dy  - y )) / dy;
1356               int b2 = (v2->Blue  * y2 + v-> Blue  * (dy2 - y2)) / dy2;
1357                
1358               int x;
1359               if (x1 < x2)
1360                 {
1361                   int dx = x2 - x1;
1362                   for (x = 0; x < dx; x++)
1363                     SetPixel (hdc, x + x1, y + v1->y, RGB(
1364                       (r1 * (dx - x) + r2 * x) / dx >> 8,
1365                       (g1 * (dx - x) + g2 * x) / dx >> 8,
1366                       (b1 * (dx - x) + b2 * x) / dx >> 8));
1367                 }
1368               else
1369                 {
1370                   int dx = x1 - x2;
1371                   for (x = 0; x < dx; x++)
1372                     SetPixel (hdc, x + x2, y + v1->y, RGB(
1373                       (r2 * (dx - x) + r1 * x) / dx >> 8,
1374                       (g2 * (dx - x) + g1 * x) / dx >> 8,
1375                       (b2 * (dx - x) + b1 * x) / dx >> 8));
1376                 }
1377             }
1378         }
1379       break;
1380     default:
1381       return FALSE;
1382   }
1383
1384   return TRUE;
1385 }