winealsa.drv: Implement volume control interfaces.
[wine] / dlls / gdi32 / dibdrv / objects.c
1 /*
2  * DIB driver GDI objects.
3  *
4  * Copyright 2011 Huw Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdlib.h>
22
23 #include "gdi_private.h"
24 #include "dibdrv.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(dib);
29
30 /*
31  *
32  * Decompose the 16 ROP2s into an expression of the form
33  *
34  * D = (D & A) ^ X
35  *
36  * Where A and X depend only on P (and so can be precomputed).
37  *
38  *                                       A    X
39  *
40  * R2_BLACK         0                    0    0
41  * R2_NOTMERGEPEN   ~(D | P)            ~P   ~P
42  * R2_MASKNOTPEN    ~P & D              ~P    0
43  * R2_NOTCOPYPEN    ~P                   0   ~P
44  * R2_MASKPENNOT    P & ~D               P    P
45  * R2_NOT           ~D                   1    1
46  * R2_XORPEN        P ^ D                1    P
47  * R2_NOTMASKPEN    ~(P & D)             P    1
48  * R2_MASKPEN       P & D                P    0
49  * R2_NOTXORPEN     ~(P ^ D)             1   ~P
50  * R2_NOP           D                    1    0
51  * R2_MERGENOTPEN   ~P | D               P   ~P
52  * R2_COPYPEN       P                    0    P
53  * R2_MERGEPENNOT   P | ~D              ~P    1
54  * R2_MERGEPEN      P | D               ~P    P
55  * R2_WHITE         1                    0    1
56  *
57  */
58
59 /* A = (P & A1) | (~P & A2) */
60 #define ZERO {0, 0}
61 #define ONE {0xffffffff, 0xffffffff}
62 #define P {0xffffffff, 0}
63 #define NOT_P {0, 0xffffffff}
64
65 static const DWORD rop2_and_array[16][2] =
66 {
67     ZERO, NOT_P, NOT_P, ZERO,
68     P,    ONE,   ONE,   P,
69     P,    ONE,   ONE,   P,
70     ZERO, NOT_P, NOT_P, ZERO
71 };
72
73 /* X = (P & X1) | (~P & X2) */
74 static const DWORD rop2_xor_array[16][2] =
75 {
76     ZERO, NOT_P, ZERO, NOT_P,
77     P,    ONE,   P,    ONE,
78     ZERO, NOT_P, ZERO, NOT_P,
79     P,    ONE,   P,    ONE
80 };
81
82 #undef NOT_P
83 #undef P
84 #undef ONE
85 #undef ZERO
86
87 void calc_and_xor_masks(INT rop, DWORD color, DWORD *and, DWORD *xor)
88 {
89     /* NB The ROP2 codes start at one and the arrays are zero-based */
90     *and = (color & rop2_and_array[rop-1][0]) | ((~color) & rop2_and_array[rop-1][1]);
91     *xor = (color & rop2_xor_array[rop-1][0]) | ((~color) & rop2_xor_array[rop-1][1]);
92 }
93
94 static inline void order_end_points(int *s, int *e)
95 {
96     if(*s > *e)
97     {
98         int tmp;
99         tmp = *s + 1;
100         *s = *e + 1;
101         *e = tmp;
102     }
103 }
104
105 static inline BOOL pt_in_rect( const RECT *rect, const POINT *pt )
106 {
107     return ((pt->x >= rect->left) && (pt->x < rect->right) &&
108             (pt->y >= rect->top) && (pt->y < rect->bottom));
109 }
110
111 #define Y_INCREASING_MASK 0x0f
112 #define X_INCREASING_MASK 0xc3
113 #define X_MAJOR_MASK      0x99
114 #define POS_SLOPE_MASK    0x33
115
116 static inline BOOL is_xmajor(DWORD octant)
117 {
118     return octant & X_MAJOR_MASK;
119 }
120
121 static inline BOOL is_pos_slope(DWORD octant)
122 {
123     return octant & POS_SLOPE_MASK;
124 }
125
126 static inline BOOL is_x_increasing(DWORD octant)
127 {
128     return octant & X_INCREASING_MASK;
129 }
130
131 static inline BOOL is_y_increasing(DWORD octant)
132 {
133     return octant & Y_INCREASING_MASK;
134 }
135
136 /**********************************************************************
137  *                  get_octant_number
138  *
139  * Return the octant number starting clockwise from the +ve x-axis.
140  */
141 static inline int get_octant_number(int dx, int dy)
142 {
143     if(dy > 0)
144         if(dx > 0)
145             return ( dx >  dy) ? 1 : 2;
146         else
147             return (-dx >  dy) ? 4 : 3;
148     else
149         if(dx < 0)
150             return (-dx > -dy) ? 5 : 6;
151         else
152             return ( dx > -dy) ? 8 : 7;
153 }
154
155 static inline DWORD get_octant_mask(int dx, int dy)
156 {
157     return 1 << (get_octant_number(dx, dy) - 1);
158 }
159
160 static void solid_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
161 {
162     RECT rect;
163
164     rect.left   = x;
165     rect.right  = x + 1;
166     rect.top    = y;
167     rect.bottom = y + 1;
168     pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
169     return;
170 }
171
172 #define OUT_LEFT    1
173 #define OUT_RIGHT   2
174 #define OUT_TOP     4
175 #define OUT_BOTTOM  8
176
177 static inline DWORD calc_outcode(const POINT *pt, const RECT *clip)
178 {
179     DWORD out = 0;
180     if(pt->x < clip->left)         out |= OUT_LEFT;
181     else if(pt->x >= clip->right)  out |= OUT_RIGHT;
182     if(pt->y < clip->top)          out |= OUT_TOP;
183     else if(pt->y >= clip->bottom) out |= OUT_BOTTOM;
184
185     return out;
186 }
187
188 typedef struct
189 {
190     unsigned int dx, dy;
191     int bias;
192     DWORD octant;
193 } bres_params;
194
195 /******************************************************************************
196  *                clip_line
197  *
198  * Clips the start and end points to a rectangle.
199  *
200  * Note, this treats the end point like the start point.  If the
201  * caller doesn't want it displayed, it should exclude it.  If the end
202  * point is clipped out, then the likelihood is that the new end point
203  * should be displayed.
204  *
205  * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
206  *
207  * This derivation is based on the comments in X.org's xserver/mi/mizerclip.c,
208  * however the Bresenham error term is defined differently so the equations
209  * will also differ.
210  *
211  * For x major lines we have 2dy >= err + bias > 2dy - 2dx
212  *                           0   >= err + bias - 2dy > -2dx
213  *
214  * Note dx, dy, m and n are all +ve.
215  *
216  * Moving the start pt from x1 to x1 + m, we need to figure out y1 + n.
217  *                     err = 2dy - dx + 2mdy - 2ndx
218  *                      0 >= 2dy - dx + 2mdy - 2ndx + bias - 2dy > -2dx
219  *                      0 >= 2mdy - 2ndx + bias - dx > -2dx
220  *                      which of course will give exactly one solution for n,
221  *                      so looking at the >= inequality
222  *                      n >= (2mdy + bias - dx) / 2dx
223  *                      n = ceiling((2mdy + bias - dx) / 2dx)
224  *                        = (2mdy + bias + dx - 1) / 2dx (assuming division truncation)
225  *
226  * Moving start pt from y1 to y1 + n we need to figure out x1 + m - there may be several
227  * solutions we pick the one that minimizes m (ie that first unlipped pt). As above:
228  *                     0 >= 2mdy - 2ndx + bias - dx > -2dx
229  *                  2mdy > 2ndx - bias - dx
230  *                     m > (2ndx - bias - dx) / 2dy
231  *                     m = floor((2ndx - bias - dx) / 2dy) + 1
232  *                     m = (2ndx - bias - dx) / 2dy + 1
233  *
234  * Moving end pt from x2 to x2 - m, we need to figure out y2 - n
235  *                  err = 2dy - dx + 2(dx - m)dy - 2(dy - n)dx
236  *                      = 2dy - dx - 2mdy + 2ndx
237  *                   0 >= 2dy - dx - 2mdy + 2ndx + bias - 2dy > -2dx
238  *                   0 >= 2ndx - 2mdy + bias - dx > -2dx
239  *                   again exactly one solution.
240  *                   2ndx <= 2mdy - bias + dx
241  *                   n = floor((2mdy - bias + dx) / 2dx)
242  *                     = (2mdy - bias + dx) / 2dx
243  *
244  * Moving end pt from y2 to y2 - n when need x2 - m this time maximizing x2 - m so
245  * mininizing m to include all of the points at y = y2 - n.  As above:
246  *                  0 >= 2ndx - 2mdy + bias - dx > -2dx
247  *               2mdy >= 2ndx + bias - dx
248  *                   m = ceiling((2ndx + bias - dx) / 2dy)
249  *                     = (2ndx + bias - dx - 1) / 2dy + 1
250  *
251  * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
252  *
253  * Moving start point from y1 to y1 + n find x1 + m
254  *                     m = (2ndx + bias + dy - 1) / 2dy
255  *
256  * Moving start point from x1 to x1 + m find y1 + n
257  *                     n = (2mdy - bias - dy) / 2ndx + 1
258  *
259  * Moving end point from y2 to y2 - n find x1 - m
260  *                     m = (2ndx - bias + dy) / 2dy
261  *
262  * Moving end point from x2 to x2 - m find y2 - n
263  *                     n = (2mdy + bias - dy - 1) / 2dx + 1
264  */
265 static int clip_line(const POINT *start, const POINT *end, const RECT *clip,
266                      const bres_params *params, POINT *pt1, POINT *pt2)
267 {
268     unsigned int n, m;
269     BOOL clipped = FALSE;
270     DWORD start_oc, end_oc;
271     const int bias = params->bias;
272     const unsigned int dx = params->dx;
273     const unsigned int dy = params->dy;
274     const unsigned int two_dx = params->dx * 2;
275     const unsigned int two_dy = params->dy * 2;
276     const BOOL xmajor = is_xmajor(params->octant);
277     const BOOL neg_slope = !is_pos_slope(params->octant);
278
279     *pt1 = *start;
280     *pt2 = *end;
281
282     start_oc = calc_outcode(start, clip);
283     end_oc = calc_outcode(end, clip);
284
285     while(1)
286     {
287         if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
288         if(start_oc & end_oc)            return 0; /* trivial reject */
289
290         clipped = TRUE;
291         if(start_oc & OUT_LEFT)
292         {
293             m = clip->left - start->x;
294             if(xmajor)
295                 n = (m * two_dy + bias + dx - 1) / two_dx;
296             else
297                 n = (m * two_dy - bias - dy) / two_dx + 1;
298
299             pt1->x = clip->left;
300             if(neg_slope) n = -n;
301             pt1->y = start->y + n;
302             start_oc = calc_outcode(pt1, clip);
303         }
304         else if(start_oc & OUT_RIGHT)
305         {
306             m = start->x - clip->right + 1;
307             if(xmajor)
308                 n = (m * two_dy + bias + dx - 1) / two_dx;
309             else
310                 n = (m * two_dy - bias - dy) / two_dx + 1;
311
312             pt1->x = clip->right - 1;
313             if(neg_slope) n = -n;
314             pt1->y = start->y - n;
315             start_oc = calc_outcode(pt1, clip);
316         }
317         else if(start_oc & OUT_TOP)
318         {
319             n = clip->top - start->y;
320             if(xmajor)
321                 m = (n * two_dx - bias - dx) / two_dy + 1;
322             else
323                 m = (n * two_dx + bias + dy - 1) / two_dy;
324
325             pt1->y = clip->top;
326             if(neg_slope) m = -m;
327             pt1->x = start->x + m;
328             start_oc = calc_outcode(pt1, clip);
329         }
330         else if(start_oc & OUT_BOTTOM)
331         {
332             n = start->y - clip->bottom + 1;
333             if(xmajor)
334                 m = (n * two_dx - bias - dx) / two_dy + 1;
335             else
336                 m = (n * two_dx + bias + dy - 1) / two_dy;
337
338             pt1->y = clip->bottom - 1;
339             if(neg_slope) m = -m;
340             pt1->x = start->x - m;
341             start_oc = calc_outcode(pt1, clip);
342         }
343         else if(end_oc & OUT_LEFT)
344         {
345             m = clip->left - end->x;
346             if(xmajor)
347                 n = (m * two_dy - bias + dx) / two_dx;
348             else
349                 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
350
351             pt2->x = clip->left;
352             if(neg_slope) n = -n;
353             pt2->y = end->y + n;
354             end_oc = calc_outcode(pt2, clip);
355         }
356         else if(end_oc & OUT_RIGHT)
357         {
358             m = end->x - clip->right + 1;
359             if(xmajor)
360                 n = (m * two_dy - bias + dx) / two_dx;
361             else
362                 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
363
364             pt2->x = clip->right - 1;
365             if(neg_slope) n = -n;
366             pt2->y = end->y - n;
367             end_oc = calc_outcode(pt2, clip);
368         }
369         else if(end_oc & OUT_TOP)
370         {
371             n = clip->top - end->y;
372             if(xmajor)
373                 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
374             else
375                 m = (n * two_dx - bias + dy) / two_dy;
376
377             pt2->y = clip->top;
378             if(neg_slope) m = -m;
379             pt2->x = end->x + m;
380             end_oc = calc_outcode(pt2, clip);
381         }
382         else if(end_oc & OUT_BOTTOM)
383         {
384             n = end->y - clip->bottom + 1;
385             if(xmajor)
386                 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
387             else
388                 m = (n * two_dx - bias + dy) / two_dy;
389
390             pt2->y = clip->bottom - 1;
391             if(neg_slope) m = -m;
392             pt2->x = end->x - m;
393             end_oc = calc_outcode(pt2, clip);
394         }
395     }
396 }
397
398 static void bres_line_with_bias(INT x1, INT y1, INT x2, INT y2, const bres_params *params, INT err,
399                                 BOOL last_pt, void (* callback)(dibdrv_physdev*,INT,INT), dibdrv_physdev *pdev)
400 {
401     const int xadd = is_x_increasing(params->octant) ? 1 : -1;
402     const int yadd = is_y_increasing(params->octant) ? 1 : -1;
403     INT erradd;
404
405     if (is_xmajor(params->octant))  /* line is "more horizontal" */
406     {
407         erradd = 2*params->dy - 2*params->dx;
408         while(x1 != x2)
409         {
410             callback(pdev, x1, y1);
411             if (err + params->bias > 0)
412             {
413                 y1 += yadd;
414                 err += erradd;
415             }
416             else err += 2*params->dy;
417             x1 += xadd;
418         }
419         if(last_pt) callback(pdev, x1, y1);
420     }
421     else   /* line is "more vertical" */
422     {
423         erradd = 2*params->dx - 2*params->dy;
424         while(y1 != y2)
425         {
426             callback(pdev, x1, y1);
427             if (err + params->bias > 0)
428             {
429                 x1 += xadd;
430                 err += erradd;
431             }
432             else err += 2*params->dx;
433             y1 += yadd;
434         }
435         if(last_pt) callback(pdev, x1, y1);
436     }
437 }
438
439 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
440 {
441     const WINEREGION *clip = get_wine_region(pdev->clip);
442
443     if(start->y == end->y)
444     {
445         RECT rect;
446         int i;
447
448         rect.left   = start->x;
449         rect.top    = start->y;
450         rect.right  = end->x;
451         rect.bottom = end->y + 1;
452         order_end_points(&rect.left, &rect.right);
453         for(i = 0; i < clip->numRects; i++)
454         {
455             if(clip->rects[i].top >= rect.bottom) break;
456             if(clip->rects[i].bottom <= rect.top) continue;
457             /* Optimize the unclipped case */
458             if(clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
459             {
460                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
461                 break;
462             }
463             if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
464             {
465                 RECT tmp = rect;
466                 tmp.left = max(rect.left, clip->rects[i].left);
467                 tmp.right = min(rect.right, clip->rects[i].right);
468                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
469             }
470         }
471     }
472     else if(start->x == end->x)
473     {
474         RECT rect;
475         int i;
476
477         rect.left   = start->x;
478         rect.top    = start->y;
479         rect.right  = end->x + 1;
480         rect.bottom = end->y;
481         order_end_points(&rect.top, &rect.bottom);
482         for(i = 0; i < clip->numRects; i++)
483         {
484             /* Optimize unclipped case */
485             if(clip->rects[i].top <= rect.top && clip->rects[i].bottom >= rect.bottom &&
486                clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
487             {
488                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
489                 break;
490             }
491             if(clip->rects[i].top >= rect.bottom) break;
492             if(clip->rects[i].bottom <= rect.top) continue;
493             if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
494             {
495                 RECT tmp = rect;
496                 tmp.top = max(rect.top, clip->rects[i].top);
497                 tmp.bottom = min(rect.bottom, clip->rects[i].bottom);
498                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
499             }
500         }
501     }
502     else
503     {
504         bres_params params;
505         INT dx = end->x - start->x;
506         INT dy = end->y - start->y;
507         INT i;
508
509         params.dx = abs(dx);
510         params.dy = abs(dy);
511         params.octant = get_octant_mask(dx, dy);
512         /* Octants 3, 5, 6 and 8 take a bias */
513         params.bias = (params.octant & 0xb4) ? 1 : 0;
514
515         for(i = 0; i < clip->numRects; i++)
516         {
517             POINT clipped_start, clipped_end;
518             int clip_status;
519             clip_status = clip_line(start, end, clip->rects + i, &params, &clipped_start, &clipped_end);
520
521             if(clip_status)
522             {
523                 int m = abs(clipped_start.x - start->x);
524                 int n = abs(clipped_start.y - start->y);
525                 int err;
526                 BOOL last_pt = FALSE;
527
528                 if(is_xmajor(params.octant))
529                     err = 2 * params.dy - params.dx + m * 2 * params.dy - n * 2 * params.dx;
530                 else
531                     err = 2 * params.dx - params.dy + n * 2 * params.dx - m * 2 * params.dy;
532
533                 if(clip_status == 1 && (end->x != clipped_end.x || end->y != clipped_end.y)) last_pt = TRUE;
534
535                 bres_line_with_bias(clipped_start.x, clipped_start.y, clipped_end.x, clipped_end.y, &params,
536                                     err, last_pt, solid_pen_line_callback, pdev);
537
538                 if(clip_status == 2) break; /* completely unclipped, so we can finish */
539             }
540         }
541
542     }
543     release_wine_region(pdev->clip);
544     return TRUE;
545 }
546
547 void reset_dash_origin(dibdrv_physdev *pdev)
548 {
549     pdev->dash_pos.cur_dash = 0;
550     pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
551     pdev->dash_pos.mark = TRUE;
552 }
553
554 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
555 {
556     skip %= pdev->pen_pattern.total_len;
557     while(skip)
558     {
559         if(pdev->dash_pos.left_in_dash > skip)
560         {
561             pdev->dash_pos.left_in_dash -= skip;
562             return;
563         }
564         skip -= pdev->dash_pos.left_in_dash;
565         pdev->dash_pos.cur_dash++;
566         if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
567         pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
568         pdev->dash_pos.mark = !pdev->dash_pos.mark;
569     }
570 }
571
572 static inline void get_dash_colors(const dibdrv_physdev *pdev, DWORD *and, DWORD *xor)
573 {
574     if(pdev->dash_pos.mark)
575     {
576         *and = pdev->pen_and;
577         *xor = pdev->pen_xor;
578     }
579     else /* space */
580     {
581         *and = pdev->bkgnd_and;
582         *xor = pdev->bkgnd_xor;
583     }
584 }
585
586 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
587 {
588     RECT rect;
589     DWORD and, xor;
590
591     get_dash_colors(pdev, &and, &xor);
592     skip_dash(pdev, 1);
593     rect.left   = x;
594     rect.right  = x + 1;
595     rect.top    = y;
596     rect.bottom = y + 1;
597     pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
598     return;
599 }
600
601 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
602 {
603     const WINEREGION *clip = get_wine_region(pdev->clip);
604     DWORD and, xor;
605     int i, dash_len;
606     RECT rect;
607     const dash_pos start_pos = pdev->dash_pos;
608
609     if(start->y == end->y) /* hline */
610     {
611         BOOL l_to_r;
612         INT left, right, cur_x;
613
614         rect.top = start->y;
615         rect.bottom = start->y + 1;
616
617         if(start->x <= end->x)
618         {
619             left = start->x;
620             right = end->x - 1;
621             l_to_r = TRUE;
622         }
623         else
624         {
625             left = end->x + 1;
626             right = start->x;
627             l_to_r = FALSE;
628         }
629
630         for(i = 0; i < clip->numRects; i++)
631         {
632             if(clip->rects[i].top > start->y) break;
633             if(clip->rects[i].bottom <= start->y) continue;
634
635             if(clip->rects[i].right > left && clip->rects[i].left <= right)
636             {
637                 int clipped_left  = max(clip->rects[i].left, left);
638                 int clipped_right = min(clip->rects[i].right - 1, right);
639
640                 pdev->dash_pos = start_pos;
641
642                 if(l_to_r)
643                 {
644                     cur_x = clipped_left;
645                     if(cur_x != left)
646                         skip_dash(pdev, clipped_left - left);
647
648                     while(cur_x <= clipped_right)
649                     {
650                         get_dash_colors(pdev, &and, &xor);
651                         dash_len = pdev->dash_pos.left_in_dash;
652                         if(cur_x + dash_len > clipped_right + 1)
653                             dash_len = clipped_right - cur_x + 1;
654                         rect.left = cur_x;
655                         rect.right = cur_x + dash_len;
656
657                         pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
658                         cur_x += dash_len;
659                         skip_dash(pdev, dash_len);
660                     }
661                 }
662                 else
663                 {
664                     cur_x = clipped_right;
665                     if(cur_x != right)
666                         skip_dash(pdev, right - clipped_right);
667
668                     while(cur_x >= clipped_left)
669                     {
670                         get_dash_colors(pdev, &and, &xor);
671                         dash_len = pdev->dash_pos.left_in_dash;
672                         if(cur_x - dash_len < clipped_left - 1)
673                             dash_len = cur_x - clipped_left + 1;
674                         rect.left = cur_x - dash_len + 1;
675                         rect.right = cur_x + 1;
676
677                         pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
678                         cur_x -= dash_len;
679                         skip_dash(pdev, dash_len);
680                     }
681                 }
682             }
683         }
684         pdev->dash_pos = start_pos;
685         skip_dash(pdev, right - left + 1);
686     }
687     else if(start->x == end->x) /* vline */
688     {
689         BOOL t_to_b;
690         INT top, bottom, cur_y;
691
692         rect.left = start->x;
693         rect.right = start->x + 1;
694
695         if(start->y <= end->y)
696         {
697             top = start->y;
698             bottom = end->y - 1;
699             t_to_b = TRUE;
700         }
701         else
702         {
703             top = end->y + 1;
704             bottom = start->y;
705             t_to_b = FALSE;
706         }
707
708         for(i = 0; i < clip->numRects; i++)
709         {
710             if(clip->rects[i].top > bottom) break;
711             if(clip->rects[i].bottom <= top) continue;
712             if(clip->rects[i].right > start->x && clip->rects[i].left <= start->x)
713             {
714                 int clipped_top    = max(clip->rects[i].top, top);
715                 int clipped_bottom = min(clip->rects[i].bottom - 1, bottom);
716
717                 pdev->dash_pos = start_pos;
718
719                 if(t_to_b)
720                 {
721                     cur_y = clipped_top;
722                     if(cur_y != top)
723                         skip_dash(pdev, clipped_top - top);
724
725                     while(cur_y <= clipped_bottom)
726                     {
727                         get_dash_colors(pdev, &and, &xor);
728                         dash_len = pdev->dash_pos.left_in_dash;
729                         if(cur_y + dash_len > clipped_bottom + 1)
730                             dash_len = clipped_bottom - cur_y + 1;
731                         rect.top = cur_y;
732                         rect.bottom = cur_y + dash_len;
733
734                         pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
735                         cur_y += dash_len;
736                         skip_dash(pdev, dash_len);
737                     }
738                 }
739                 else
740                 {
741                     cur_y = clipped_bottom;
742                     if(cur_y != bottom)
743                         skip_dash(pdev, bottom - clipped_bottom);
744
745                     while(cur_y >= clipped_top)
746                     {
747                         get_dash_colors(pdev, &and, &xor);
748                         dash_len = pdev->dash_pos.left_in_dash;
749                         if(cur_y - dash_len < clipped_top - 1)
750                             dash_len = cur_y - clipped_top + 1;
751                         rect.top = cur_y - dash_len + 1;
752                         rect.bottom = cur_y + 1;
753
754                         pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
755                         cur_y -= dash_len;
756                         skip_dash(pdev, dash_len);
757                     }
758                 }
759             }
760         }
761         pdev->dash_pos = start_pos;
762         skip_dash(pdev, bottom - top + 1);
763     }
764     else
765     {
766         bres_params params;
767         INT dx = end->x - start->x;
768         INT dy = end->y - start->y;
769         INT i;
770
771         params.dx = abs(dx);
772         params.dy = abs(dy);
773         params.octant = get_octant_mask(dx, dy);
774         /* Octants 3, 5, 6 and 8 take a bias */
775         params.bias = (params.octant & 0xb4) ? 1 : 0;
776
777         for(i = 0; i < clip->numRects; i++)
778         {
779             POINT clipped_start, clipped_end;
780             int clip_status;
781             clip_status = clip_line(start, end, clip->rects + i, &params, &clipped_start, &clipped_end);
782
783             if(clip_status)
784             {
785                 int m = abs(clipped_start.x - start->x);
786                 int n = abs(clipped_start.y - start->y);
787                 int err;
788                 BOOL last_pt = FALSE;
789
790                 pdev->dash_pos = start_pos;
791
792                 if(is_xmajor(params.octant))
793                 {
794                     err = 2 * params.dy - params.dx + m * 2 * params.dy - n * 2 * params.dx;
795                     skip_dash(pdev, m);
796                 }
797                 else
798                 {
799                     err = 2 * params.dx - params.dy + n * 2 * params.dx - m * 2 * params.dy;
800                     skip_dash(pdev, n);
801                 }
802                 if(clip_status == 1 && (end->x != clipped_end.x || end->y != clipped_end.y)) last_pt = TRUE;
803
804                 bres_line_with_bias(clipped_start.x, clipped_start.y, clipped_end.x, clipped_end.y, &params,
805                                     err, last_pt, dashed_pen_line_callback, pdev);
806
807                 if(clip_status == 2) break; /* completely unclipped, so we can finish */
808             }
809         }
810         pdev->dash_pos = start_pos;
811         if(is_xmajor(params.octant))
812             skip_dash(pdev, params.dx);
813         else
814             skip_dash(pdev, params.dy);
815     }
816
817     release_wine_region(pdev->clip);
818     return TRUE;
819 }
820
821 static const dash_pattern dash_patterns[4] =
822 {
823     {2, {18, 6}, 24},             /* PS_DASH */
824     {2, {3,  3}, 6},              /* PS_DOT */
825     {4, {9, 6, 3, 6}, 24},        /* PS_DASHDOT */
826     {6, {9, 3, 3, 3, 3, 3}, 24}   /* PS_DASHDOTDOT */
827 };
828
829 /***********************************************************************
830  *           dibdrv_SelectPen
831  */
832 HPEN CDECL dibdrv_SelectPen( PHYSDEV dev, HPEN hpen )
833 {
834     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectPen );
835     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
836     LOGPEN logpen;
837     DWORD style;
838
839     TRACE("(%p, %p)\n", dev, hpen);
840
841     if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
842     {
843         /* must be an extended pen */
844         EXTLOGPEN *elp;
845         INT size = GetObjectW( hpen, 0, NULL );
846
847         if (!size) return 0;
848
849         elp = HeapAlloc( GetProcessHeap(), 0, size );
850
851         GetObjectW( hpen, size, elp );
852         /* FIXME: add support for user style pens */
853         logpen.lopnStyle = elp->elpPenStyle;
854         logpen.lopnWidth.x = elp->elpWidth;
855         logpen.lopnWidth.y = 0;
856         logpen.lopnColor = elp->elpColor;
857
858         HeapFree( GetProcessHeap(), 0, elp );
859     }
860
861     if (hpen == GetStockObject( DC_PEN ))
862         logpen.lopnColor = GetDCPenColor( dev->hdc );
863
864     pdev->pen_color = pdev->dib.funcs->colorref_to_pixel(&pdev->dib, logpen.lopnColor);
865     calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
866
867     pdev->defer |= DEFER_PEN;
868
869     style = logpen.lopnStyle & PS_STYLE_MASK;
870
871     switch(style)
872     {
873     case PS_SOLID:
874         if(logpen.lopnStyle & PS_GEOMETRIC) break;
875         if(logpen.lopnWidth.x > 1) break;
876         pdev->pen_line = solid_pen_line;
877         pdev->pen_pattern.count = 0;
878         pdev->defer &= ~DEFER_PEN;
879         break;
880
881     case PS_DASH:
882     case PS_DOT:
883     case PS_DASHDOT:
884     case PS_DASHDOTDOT:
885         if(logpen.lopnStyle & PS_GEOMETRIC) break;
886         if(logpen.lopnWidth.x > 1) break;
887         pdev->pen_line = dashed_pen_line;
888         pdev->pen_pattern = dash_patterns[style - PS_DASH];
889         pdev->defer &= ~DEFER_PEN;
890         break;
891
892     default:
893         break;
894     }
895
896     return next->funcs->pSelectPen( next, hpen );
897 }
898
899 /***********************************************************************
900  *           dibdrv_SetDCPenColor
901  */
902 COLORREF CDECL dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
903 {
904     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCPenColor );
905     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
906
907     if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
908     {
909         pdev->pen_color = pdev->dib.funcs->colorref_to_pixel(&pdev->dib, color);
910         calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
911     }
912
913     return next->funcs->pSetDCPenColor( next, color );
914 }
915
916 /**********************************************************************
917  *             solid_brush
918  *
919  * Fill a number of rectangles with the solid brush
920  * FIXME: Should we insist l < r && t < b?  Currently we assume this.
921  */
922 static BOOL solid_brush(dibdrv_physdev *pdev, int num, RECT *rects)
923 {
924     int i, j;
925     const WINEREGION *clip = get_wine_region(pdev->clip);
926
927     for(i = 0; i < num; i++)
928     {
929         for(j = 0; j < clip->numRects; j++)
930         {
931             RECT rect = rects[i];
932
933             /* Optimize unclipped case */
934             if(clip->rects[j].top <= rect.top && clip->rects[j].bottom >= rect.bottom &&
935                clip->rects[j].left <= rect.left && clip->rects[j].right >= rect.right)
936             {
937                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->brush_and, pdev->brush_xor);
938                 break;
939             }
940
941             if(clip->rects[j].top >= rect.bottom) break;
942             if(clip->rects[j].bottom <= rect.top) continue;
943
944             if(clip->rects[j].right > rect.left && clip->rects[j].left < rect.right)
945             {
946                 rect.left   = max(rect.left,   clip->rects[j].left);
947                 rect.top    = max(rect.top,    clip->rects[j].top);
948                 rect.right  = min(rect.right,  clip->rects[j].right);
949                 rect.bottom = min(rect.bottom, clip->rects[j].bottom);
950
951                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->brush_and, pdev->brush_xor);
952             }
953         }
954     }
955     release_wine_region(pdev->clip);
956     return TRUE;
957 }
958
959 void update_brush_rop( dibdrv_physdev *pdev, INT rop )
960 {
961     if(pdev->brush_style == BS_SOLID)
962         calc_and_xor_masks(rop, pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
963 }
964
965 /***********************************************************************
966  *           dibdrv_SelectBrush
967  */
968 HBRUSH CDECL dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush )
969 {
970     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBrush );
971     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
972     LOGBRUSH logbrush;
973
974     TRACE("(%p, %p)\n", dev, hbrush);
975
976     if (!GetObjectW( hbrush, sizeof(logbrush), &logbrush )) return 0;
977
978     if (hbrush == GetStockObject( DC_BRUSH ))
979         logbrush.lbColor = GetDCBrushColor( dev->hdc );
980
981     pdev->brush_style = logbrush.lbStyle;
982
983     pdev->defer |= DEFER_BRUSH;
984
985     switch(logbrush.lbStyle)
986     {
987     case BS_SOLID:
988         pdev->brush_color = pdev->dib.funcs->colorref_to_pixel(&pdev->dib, logbrush.lbColor);
989         calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
990         pdev->brush_rects = solid_brush;
991         pdev->defer &= ~DEFER_BRUSH;
992         break;
993     default:
994         break;
995     }
996
997     return next->funcs->pSelectBrush( next, hbrush );
998 }
999
1000 /***********************************************************************
1001  *           dibdrv_SetDCBrushColor
1002  */
1003 COLORREF CDECL dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
1004 {
1005     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCBrushColor );
1006     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1007
1008     if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
1009     {
1010         pdev->brush_color = pdev->dib.funcs->colorref_to_pixel(&pdev->dib, color);
1011         calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1012     }
1013
1014     return next->funcs->pSetDCBrushColor( next, color );
1015 }