winealsa: Unify the checks for wBitsPerSample.
[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 <assert.h>
22 #include <stdlib.h>
23
24 #include "gdi_private.h"
25 #include "dibdrv.h"
26
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(dib);
30
31 /*
32  *
33  * Decompose the 16 ROP2s into an expression of the form
34  *
35  * D = (D & A) ^ X
36  *
37  * Where A and X depend only on P (and so can be precomputed).
38  *
39  *                                       A    X
40  *
41  * R2_BLACK         0                    0    0
42  * R2_NOTMERGEPEN   ~(D | P)            ~P   ~P
43  * R2_MASKNOTPEN    ~P & D              ~P    0
44  * R2_NOTCOPYPEN    ~P                   0   ~P
45  * R2_MASKPENNOT    P & ~D               P    P
46  * R2_NOT           ~D                   1    1
47  * R2_XORPEN        P ^ D                1    P
48  * R2_NOTMASKPEN    ~(P & D)             P    1
49  * R2_MASKPEN       P & D                P    0
50  * R2_NOTXORPEN     ~(P ^ D)             1   ~P
51  * R2_NOP           D                    1    0
52  * R2_MERGENOTPEN   ~P | D               P   ~P
53  * R2_COPYPEN       P                    0    P
54  * R2_MERGEPENNOT   P | ~D              ~P    1
55  * R2_MERGEPEN      P | D               ~P    P
56  * R2_WHITE         1                    0    1
57  *
58  */
59
60 /* A = (P & A1) ^ A2 */
61 #define ZERO  { 0u,  0u}
62 #define ONE   { 0u, ~0u}
63 #define P     {~0u,  0u}
64 #define NOT_P {~0u, ~0u}
65
66 static const DWORD rop2_and_array[16][2] =
67 {
68     ZERO, NOT_P, NOT_P, ZERO,
69     P,    ONE,   ONE,   P,
70     P,    ONE,   ONE,   P,
71     ZERO, NOT_P, NOT_P, ZERO
72 };
73
74 /* X = (P & X1) ^ X2 */
75 static const DWORD rop2_xor_array[16][2] =
76 {
77     ZERO, NOT_P, ZERO, NOT_P,
78     P,    ONE,   P,    ONE,
79     ZERO, NOT_P, ZERO, NOT_P,
80     P,    ONE,   P,    ONE
81 };
82
83 #undef NOT_P
84 #undef P
85 #undef ONE
86 #undef ZERO
87
88 void get_rop_codes(INT rop, struct rop_codes *codes)
89 {
90     /* NB The ROP2 codes start at one and the arrays are zero-based */
91     codes->a1 = rop2_and_array[rop-1][0];
92     codes->a2 = rop2_and_array[rop-1][1];
93     codes->x1 = rop2_xor_array[rop-1][0];
94     codes->x2 = rop2_xor_array[rop-1][1];
95 }
96
97 static inline void calc_and_xor_masks(INT rop, DWORD color, DWORD *and, DWORD *xor)
98 {
99     struct rop_codes codes;
100     get_rop_codes( rop, &codes );
101
102     *and = (color & codes.a1) ^ codes.a2;
103     *xor = (color & codes.x1) ^ codes.x2;
104 }
105
106 static inline void calc_rop_masks(INT rop, DWORD color, rop_mask *masks)
107 {
108     calc_and_xor_masks( rop, color, &masks->and, &masks->xor );
109 }
110
111 static inline RGBQUAD rgbquad_from_colorref(COLORREF c)
112 {
113     RGBQUAD ret;
114
115     ret.rgbRed      = GetRValue(c);
116     ret.rgbGreen    = GetGValue(c);
117     ret.rgbBlue     = GetBValue(c);
118     ret.rgbReserved = 0;
119     return ret;
120 }
121
122 static inline BOOL rgbquad_equal(const RGBQUAD *a, const RGBQUAD *b)
123 {
124     if(a->rgbRed   == b->rgbRed   &&
125        a->rgbGreen == b->rgbGreen &&
126        a->rgbBlue  == b->rgbBlue)
127         return TRUE;
128     return FALSE;
129 }
130
131 COLORREF make_rgb_colorref( HDC hdc, dib_info *dib, COLORREF color, BOOL *got_pixel, DWORD *pixel )
132 {
133     *pixel = 0;
134     *got_pixel = FALSE;
135
136     if (color & (1 << 24))  /* PALETTEINDEX */
137     {
138         HPALETTE pal = GetCurrentObject( hdc, OBJ_PAL );
139         PALETTEENTRY pal_ent;
140
141         if (!GetPaletteEntries( pal, LOWORD(color), 1, &pal_ent ))
142             GetPaletteEntries( pal, 0, 1, &pal_ent );
143         return RGB( pal_ent.peRed, pal_ent.peGreen, pal_ent.peBlue );
144     }
145
146     if (color >> 16 == 0x10ff)  /* DIBINDEX */
147     {
148         WORD index = LOWORD( color );
149         *got_pixel = TRUE;
150         if (!dib->color_table || index >= (1 << dib->bit_count)) return 0;
151         *pixel = index;
152         return RGB( dib->color_table[index].rgbRed,
153                     dib->color_table[index].rgbGreen,
154                     dib->color_table[index].rgbBlue );
155     }
156
157     return color & 0xffffff;
158 }
159
160 /******************************************************************
161  *                   get_pixel_color
162  *
163  * 1 bit bitmaps map the fg/bg colors as follows:
164  * If the fg colorref exactly matches one of the color table entries then
165  * that entry is the fg color and the other is the bg.
166  * Otherwise the bg color is mapped to the closest entry in the table and
167  * the fg takes the other one.
168  */
169 DWORD get_pixel_color( dibdrv_physdev *pdev, COLORREF color, BOOL mono_fixup )
170 {
171     RGBQUAD fg_quad;
172     BOOL got_pixel;
173     DWORD pixel;
174     COLORREF rgb_ref;
175
176     rgb_ref = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, color, &got_pixel, &pixel );
177     if (got_pixel) return pixel;
178
179     if (pdev->dib.bit_count != 1 || !mono_fixup)
180         return pdev->dib.funcs->colorref_to_pixel( &pdev->dib, rgb_ref );
181
182     fg_quad = rgbquad_from_colorref( rgb_ref );
183     if(rgbquad_equal(&fg_quad, pdev->dib.color_table))
184         return 0;
185     if(rgbquad_equal(&fg_quad, pdev->dib.color_table + 1))
186         return 1;
187
188     pixel = get_pixel_color( pdev, GetBkColor(pdev->dev.hdc), FALSE );
189     if (color == GetBkColor(pdev->dev.hdc)) return pixel;
190     else return !pixel;
191 }
192
193 /***************************************************************************
194  *                get_color_masks
195  *
196  * Returns the color masks unless the dib is 1 bpp.  In this case since
197  * there are several fg sources (pen, brush, text) we take as bg the inverse
198  * of the relevant fg color (which is always set up correctly).
199  */
200 static inline void get_color_masks( dibdrv_physdev *pdev, UINT rop, COLORREF colorref,
201                                     INT bkgnd_mode, rop_mask *fg_mask, rop_mask *bg_mask )
202 {
203     DWORD color = get_pixel_color( pdev, colorref, TRUE );
204
205     calc_rop_masks( rop, color, fg_mask );
206
207     if (bkgnd_mode == TRANSPARENT)
208     {
209         bg_mask->and = ~0u;
210         bg_mask->xor = 0;
211         return;
212     }
213
214     if (pdev->dib.bit_count != 1) color = get_pixel_color( pdev, GetBkColor(pdev->dev.hdc), FALSE );
215     else if (colorref != GetBkColor(pdev->dev.hdc)) color = !color;
216
217     calc_rop_masks( rop, color, bg_mask );
218 }
219
220 static inline void order_end_points(int *s, int *e)
221 {
222     if(*s > *e)
223     {
224         int tmp;
225         tmp = *s + 1;
226         *s = *e + 1;
227         *e = tmp;
228     }
229 }
230
231 #define Y_INCREASING_MASK 0x0f
232 #define X_INCREASING_MASK 0xc3
233 #define X_MAJOR_MASK      0x99
234 #define POS_SLOPE_MASK    0x33
235
236 static inline BOOL is_xmajor(DWORD octant)
237 {
238     return octant & X_MAJOR_MASK;
239 }
240
241 static inline BOOL is_pos_slope(DWORD octant)
242 {
243     return octant & POS_SLOPE_MASK;
244 }
245
246 static inline BOOL is_x_increasing(DWORD octant)
247 {
248     return octant & X_INCREASING_MASK;
249 }
250
251 static inline BOOL is_y_increasing(DWORD octant)
252 {
253     return octant & Y_INCREASING_MASK;
254 }
255
256 /**********************************************************************
257  *                  get_octant_number
258  *
259  * Return the octant number starting clockwise from the +ve x-axis.
260  */
261 static inline int get_octant_number(int dx, int dy)
262 {
263     if(dy > 0)
264         if(dx > 0)
265             return ( dx >  dy) ? 1 : 2;
266         else
267             return (-dx >  dy) ? 4 : 3;
268     else
269         if(dx < 0)
270             return (-dx > -dy) ? 5 : 6;
271         else
272             return ( dx > -dy) ? 8 : 7;
273 }
274
275 static inline DWORD get_octant_mask(int dx, int dy)
276 {
277     return 1 << (get_octant_number(dx, dy) - 1);
278 }
279
280 static inline int get_bias( DWORD mask )
281 {
282     /* Octants 3, 5, 6 and 8 take a bias */
283     return (mask & 0xb4) ? 1 : 0;
284 }
285
286 #define OUT_LEFT    1
287 #define OUT_RIGHT   2
288 #define OUT_TOP     4
289 #define OUT_BOTTOM  8
290
291 static inline DWORD calc_outcode(const POINT *pt, const RECT *clip)
292 {
293     DWORD out = 0;
294     if(pt->x < clip->left)         out |= OUT_LEFT;
295     else if(pt->x >= clip->right)  out |= OUT_RIGHT;
296     if(pt->y < clip->top)          out |= OUT_TOP;
297     else if(pt->y >= clip->bottom) out |= OUT_BOTTOM;
298
299     return out;
300 }
301
302 /******************************************************************************
303  *                clip_line
304  *
305  * Clips the start and end points to a rectangle.
306  *
307  * Note, this treats the end point like the start point.  If the
308  * caller doesn't want it displayed, it should exclude it.  If the end
309  * point is clipped out, then the likelihood is that the new end point
310  * should be displayed.
311  *
312  * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
313  *
314  * This derivation is based on the comments in X.org's xserver/mi/mizerclip.c,
315  * however the Bresenham error term is defined differently so the equations
316  * will also differ.
317  *
318  * For x major lines we have 2dy >= err + bias > 2dy - 2dx
319  *                           0   >= err + bias - 2dy > -2dx
320  *
321  * Note dx, dy, m and n are all +ve.
322  *
323  * Moving the start pt from x1 to x1 + m, we need to figure out y1 + n.
324  *                     err = 2dy - dx + 2mdy - 2ndx
325  *                      0 >= 2dy - dx + 2mdy - 2ndx + bias - 2dy > -2dx
326  *                      0 >= 2mdy - 2ndx + bias - dx > -2dx
327  *                      which of course will give exactly one solution for n,
328  *                      so looking at the >= inequality
329  *                      n >= (2mdy + bias - dx) / 2dx
330  *                      n = ceiling((2mdy + bias - dx) / 2dx)
331  *                        = (2mdy + bias + dx - 1) / 2dx (assuming division truncation)
332  *
333  * Moving start pt from y1 to y1 + n we need to figure out x1 + m - there may be several
334  * solutions we pick the one that minimizes m (ie that first unlipped pt). As above:
335  *                     0 >= 2mdy - 2ndx + bias - dx > -2dx
336  *                  2mdy > 2ndx - bias - dx
337  *                     m > (2ndx - bias - dx) / 2dy
338  *                     m = floor((2ndx - bias - dx) / 2dy) + 1
339  *                     m = (2ndx - bias - dx) / 2dy + 1
340  *
341  * Moving end pt from x2 to x2 - m, we need to figure out y2 - n
342  *                  err = 2dy - dx + 2(dx - m)dy - 2(dy - n)dx
343  *                      = 2dy - dx - 2mdy + 2ndx
344  *                   0 >= 2dy - dx - 2mdy + 2ndx + bias - 2dy > -2dx
345  *                   0 >= 2ndx - 2mdy + bias - dx > -2dx
346  *                   again exactly one solution.
347  *                   2ndx <= 2mdy - bias + dx
348  *                   n = floor((2mdy - bias + dx) / 2dx)
349  *                     = (2mdy - bias + dx) / 2dx
350  *
351  * Moving end pt from y2 to y2 - n when need x2 - m this time maximizing x2 - m so
352  * mininizing m to include all of the points at y = y2 - n.  As above:
353  *                  0 >= 2ndx - 2mdy + bias - dx > -2dx
354  *               2mdy >= 2ndx + bias - dx
355  *                   m = ceiling((2ndx + bias - dx) / 2dy)
356  *                     = (2ndx + bias - dx - 1) / 2dy + 1
357  *
358  * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
359  *
360  * Moving start point from y1 to y1 + n find x1 + m
361  *                     m = (2ndx + bias + dy - 1) / 2dy
362  *
363  * Moving start point from x1 to x1 + m find y1 + n
364  *                     n = (2mdy - bias - dy) / 2ndx + 1
365  *
366  * Moving end point from y2 to y2 - n find x1 - m
367  *                     m = (2ndx - bias + dy) / 2dy
368  *
369  * Moving end point from x2 to x2 - m find y2 - n
370  *                     n = (2mdy + bias - dy - 1) / 2dx + 1
371  */
372 int clip_line(const POINT *start, const POINT *end, const RECT *clip,
373               const bres_params *params, POINT *pt1, POINT *pt2)
374 {
375     int m, n;
376     BOOL clipped = FALSE;
377     DWORD start_oc, end_oc;
378     const int bias = params->bias;
379     const unsigned int dx = params->dx;
380     const unsigned int dy = params->dy;
381     const unsigned int two_dx = params->dx * 2;
382     const unsigned int two_dy = params->dy * 2;
383     const BOOL xmajor = is_xmajor(params->octant);
384     const BOOL neg_slope = !is_pos_slope(params->octant);
385
386     *pt1 = *start;
387     *pt2 = *end;
388
389     start_oc = calc_outcode(start, clip);
390     end_oc = calc_outcode(end, clip);
391
392     while(1)
393     {
394         if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
395         if(start_oc & end_oc)            return 0; /* trivial reject */
396
397         clipped = TRUE;
398         if(start_oc & OUT_LEFT)
399         {
400             m = clip->left - start->x;
401             if(xmajor)
402                 n = (m * two_dy + bias + dx - 1) / two_dx;
403             else
404                 n = (m * two_dy - bias - dy) / two_dx + 1;
405
406             pt1->x = clip->left;
407             if(neg_slope) n = -n;
408             pt1->y = start->y + n;
409             start_oc = calc_outcode(pt1, clip);
410         }
411         else if(start_oc & OUT_RIGHT)
412         {
413             m = start->x - clip->right + 1;
414             if(xmajor)
415                 n = (m * two_dy + bias + dx - 1) / two_dx;
416             else
417                 n = (m * two_dy - bias - dy) / two_dx + 1;
418
419             pt1->x = clip->right - 1;
420             if(neg_slope) n = -n;
421             pt1->y = start->y - n;
422             start_oc = calc_outcode(pt1, clip);
423         }
424         else if(start_oc & OUT_TOP)
425         {
426             n = clip->top - start->y;
427             if(xmajor)
428                 m = (n * two_dx - bias - dx) / two_dy + 1;
429             else
430                 m = (n * two_dx + bias + dy - 1) / two_dy;
431
432             pt1->y = clip->top;
433             if(neg_slope) m = -m;
434             pt1->x = start->x + m;
435             start_oc = calc_outcode(pt1, clip);
436         }
437         else if(start_oc & OUT_BOTTOM)
438         {
439             n = start->y - clip->bottom + 1;
440             if(xmajor)
441                 m = (n * two_dx - bias - dx) / two_dy + 1;
442             else
443                 m = (n * two_dx + bias + dy - 1) / two_dy;
444
445             pt1->y = clip->bottom - 1;
446             if(neg_slope) m = -m;
447             pt1->x = start->x - m;
448             start_oc = calc_outcode(pt1, clip);
449         }
450         else if(end_oc & OUT_LEFT)
451         {
452             m = clip->left - end->x;
453             if(xmajor)
454                 n = (m * two_dy - bias + dx) / two_dx;
455             else
456                 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
457
458             pt2->x = clip->left;
459             if(neg_slope) n = -n;
460             pt2->y = end->y + n;
461             end_oc = calc_outcode(pt2, clip);
462         }
463         else if(end_oc & OUT_RIGHT)
464         {
465             m = end->x - clip->right + 1;
466             if(xmajor)
467                 n = (m * two_dy - bias + dx) / two_dx;
468             else
469                 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
470
471             pt2->x = clip->right - 1;
472             if(neg_slope) n = -n;
473             pt2->y = end->y - n;
474             end_oc = calc_outcode(pt2, clip);
475         }
476         else if(end_oc & OUT_TOP)
477         {
478             n = clip->top - end->y;
479             if(xmajor)
480                 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
481             else
482                 m = (n * two_dx - bias + dy) / two_dy;
483
484             pt2->y = clip->top;
485             if(neg_slope) m = -m;
486             pt2->x = end->x + m;
487             end_oc = calc_outcode(pt2, clip);
488         }
489         else if(end_oc & OUT_BOTTOM)
490         {
491             n = end->y - clip->bottom + 1;
492             if(xmajor)
493                 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
494             else
495                 m = (n * two_dx - bias + dy) / two_dy;
496
497             pt2->y = clip->bottom - 1;
498             if(neg_slope) m = -m;
499             pt2->x = end->x - m;
500             end_oc = calc_outcode(pt2, clip);
501         }
502     }
503 }
504
505 static void bres_line_with_bias(const POINT *start, const struct line_params *params,
506                                 void (* callback)(dibdrv_physdev*,INT,INT), dibdrv_physdev *pdev)
507 {
508     POINT pt = *start;
509     int len = params->length, err = params->err_start;
510
511     if (params->x_major)
512     {
513         while(len--)
514         {
515             callback(pdev, pt.x, pt.y);
516             if (err + params->bias > 0)
517             {
518                 pt.y += params->y_inc;
519                 err += params->err_add_1;
520             }
521             else err += params->err_add_2;
522             pt.x += params->x_inc;
523         }
524     }
525     else
526     {
527         while(len--)
528         {
529             callback(pdev, pt.x, pt.y);
530             if (err + params->bias > 0)
531             {
532                 pt.x += params->x_inc;
533                 err += params->err_add_1;
534             }
535             else err += params->err_add_2;
536             pt.y += params->y_inc;
537         }
538     }
539 }
540
541 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end, DWORD and, DWORD xor)
542 {
543     struct clipped_rects clipped_rects;
544     RECT rect;
545     int i;
546
547     if(start->y == end->y)
548     {
549         rect.left   = start->x;
550         rect.top    = start->y;
551         rect.right  = end->x;
552         rect.bottom = end->y + 1;
553         order_end_points(&rect.left, &rect.right);
554         if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
555         pdev->dib.funcs->solid_rects(&pdev->dib, clipped_rects.count, clipped_rects.rects, and, xor);
556     }
557     else if(start->x == end->x)
558     {
559         rect.left   = start->x;
560         rect.top    = start->y;
561         rect.right  = end->x + 1;
562         rect.bottom = end->y;
563         order_end_points(&rect.top, &rect.bottom);
564         if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
565         pdev->dib.funcs->solid_rects(&pdev->dib, clipped_rects.count, clipped_rects.rects, and, xor);
566     }
567     else
568     {
569         bres_params clip_params;
570         struct line_params line_params;
571         INT dx = end->x - start->x, dy = end->y - start->y;
572         INT abs_dx = abs(dx), abs_dy = abs(dy);
573
574         clip_params.dx = abs_dx;
575         clip_params.dy = abs_dy;
576         clip_params.octant = get_octant_mask(dx, dy);
577         clip_params.bias   = get_bias( clip_params.octant );
578
579         line_params.bias    = clip_params.bias;
580         line_params.x_major = is_xmajor( clip_params.octant );
581         line_params.x_inc   = is_x_increasing( clip_params.octant ) ? 1 : -1;
582         line_params.y_inc   = is_y_increasing( clip_params.octant ) ? 1 : -1;
583
584         if (line_params.x_major)
585         {
586             line_params.err_add_1 = 2 * abs_dy - 2 * abs_dx;
587             line_params.err_add_2 = 2 * abs_dy;
588         }
589         else
590         {
591             line_params.err_add_1 = 2 * abs_dx - 2 * abs_dy;
592             line_params.err_add_2 = 2 * abs_dx;
593         }
594
595         rect.left   = min( start->x, end->x );
596         rect.top    = min( start->y, end->y );
597         rect.right  = max( start->x, end->x ) + 1;
598         rect.bottom = max( start->y, end->y ) + 1;
599         if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
600         for (i = 0; i < clipped_rects.count; i++)
601         {
602             POINT clipped_start, clipped_end;
603             int clip_status;
604
605             clip_status = clip_line(start, end, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
606             if(clip_status)
607             {
608                 int m = abs(clipped_start.x - start->x);
609                 int n = abs(clipped_start.y - start->y);
610
611                 if (line_params.x_major)
612                 {
613                     line_params.err_start = 2 * abs_dy - abs_dx + m * 2 * abs_dy - n * 2 * abs_dx;
614                     line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
615                 }
616                 else
617                 {
618                     line_params.err_start = 2 * abs_dx - abs_dy + n * 2 * abs_dx - m * 2 * abs_dy;
619                     line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
620                 }
621
622                 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
623
624                 pdev->dib.funcs->solid_line( &pdev->dib, &clipped_start, &line_params, and, xor );
625
626                 if(clip_status == 2) break; /* completely unclipped, so we can finish */
627             }
628         }
629     }
630     free_clipped_rects( &clipped_rects );
631     return TRUE;
632 }
633
634 static BOOL solid_pen_line_region( dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region )
635 {
636     RECT rect;
637
638     rect.left   = start->x;
639     rect.top    = start->y;
640     rect.right  = start->x + 1;
641     rect.bottom = start->y + 1;
642
643     if (start->y == end->y)
644     {
645         rect.right = end->x;
646         order_end_points(&rect.left, &rect.right);
647         add_rect_to_region( region, &rect );
648     }
649     else if(start->x == end->x)
650     {
651         rect.bottom = end->y;
652         order_end_points(&rect.top, &rect.bottom);
653         add_rect_to_region( region, &rect );
654     }
655     else
656     {
657         INT dx = end->x - start->x, dy = end->y - start->y;
658         INT abs_dx = abs(dx), abs_dy = abs(dy);
659         DWORD octant = get_octant_mask(dx, dy);
660         INT bias = get_bias( octant );
661
662         if (is_xmajor( octant ))
663         {
664             int y_inc = is_y_increasing( octant ) ? 1 : -1;
665             int err_add_1 = 2 * abs_dy - 2 * abs_dx;
666             int err_add_2 = 2 * abs_dy;
667             int err = 2 * abs_dy - abs_dx;
668
669             if (is_x_increasing( octant ))
670             {
671                 for (rect.right = start->x + 1; rect.right <= end->x; rect.right++)
672                 {
673                     if (err + bias > 0)
674                     {
675                         add_rect_to_region( region, &rect );
676                         rect.left = rect.right;
677                         rect.top += y_inc;
678                         rect.bottom += y_inc;
679                         err += err_add_1;
680                     }
681                     else err += err_add_2;
682                 }
683             }
684             else
685             {
686                 for (rect.left = start->x; rect.left > end->x; rect.left--)
687                 {
688                     if (err + bias > 0)
689                     {
690                         add_rect_to_region( region, &rect );
691                         rect.right = rect.left;
692                         rect.top += y_inc;
693                         rect.bottom += y_inc;
694                         err += err_add_1;
695                     }
696                     else err += err_add_2;
697                 }
698             }
699         }
700         else
701         {
702             int x_inc = is_x_increasing( octant ) ? 1 : -1;
703             int err_add_1 = 2 * abs_dx - 2 * abs_dy;
704             int err_add_2 = 2 * abs_dx;
705             int err = 2 * abs_dx - abs_dy;
706
707             if (is_y_increasing( octant ))
708             {
709                 for (rect.bottom = start->y + 1; rect.bottom <= end->y; rect.bottom++)
710                 {
711                     if (err + bias > 0)
712                     {
713                         add_rect_to_region( region, &rect );
714                         rect.top = rect.bottom;
715                         rect.left += x_inc;
716                         rect.right += x_inc;
717                         err += err_add_1;
718                     }
719                     else err += err_add_2;
720                 }
721             }
722             else
723             {
724                 for (rect.top = start->y; rect.top > end->y; rect.top--)
725                 {
726                     if (err + bias > 0)
727                     {
728                         add_rect_to_region( region, &rect );
729                         rect.bottom = rect.top;
730                         rect.left += x_inc;
731                         rect.right += x_inc;
732                         err += err_add_1;
733                     }
734                     else err += err_add_2;
735                 }
736             }
737         }
738         /* add final rect */
739         add_rect_to_region( region, &rect );
740     }
741     return TRUE;
742 }
743
744 static BOOL solid_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
745 {
746     int i;
747
748     assert( num >= 2 );
749
750     if (region)
751     {
752         for (i = 0; i < num - 1; i++)
753             if (!solid_pen_line_region( pdev, pts + i, pts + i + 1, region ))
754                 return FALSE;
755         if (close) return solid_pen_line_region( pdev, pts + num - 1, pts, region );
756     }
757     else
758     {
759         DWORD color, and, xor;
760
761         color = get_pixel_color( pdev, pdev->pen_brush.colorref, TRUE );
762         calc_and_xor_masks( GetROP2(pdev->dev.hdc), color, &and, &xor );
763
764         for (i = 0; i < num - 1; i++)
765             if (!solid_pen_line( pdev, pts + i, pts + i + 1, and, xor ))
766                 return FALSE;
767         if (close) return solid_pen_line( pdev, pts + num - 1, pts, and, xor );
768     }
769     return TRUE;
770 }
771
772 void reset_dash_origin(dibdrv_physdev *pdev)
773 {
774     pdev->dash_pos.cur_dash = 0;
775     pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
776     pdev->dash_pos.mark = TRUE;
777 }
778
779 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
780 {
781     skip %= pdev->pen_pattern.total_len;
782     do
783     {
784         if(pdev->dash_pos.left_in_dash > skip)
785         {
786             pdev->dash_pos.left_in_dash -= skip;
787             return;
788         }
789         skip -= pdev->dash_pos.left_in_dash;
790         pdev->dash_pos.cur_dash++;
791         if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
792         pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
793         pdev->dash_pos.mark = !pdev->dash_pos.mark;
794     }
795     while (skip);
796 }
797
798 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
799 {
800     RECT rect;
801     rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
802
803     skip_dash(pdev, 1);
804     rect.left   = x;
805     rect.right  = x + 1;
806     rect.top    = y;
807     rect.bottom = y + 1;
808     pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
809     return;
810 }
811
812 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
813 {
814     struct clipped_rects clipped_rects;
815     int i, dash_len;
816     RECT rect;
817     const dash_pos start_pos = pdev->dash_pos;
818
819     if(start->y == end->y) /* hline */
820     {
821         BOOL l_to_r;
822         INT left, right, cur_x;
823
824         rect.top = start->y;
825         rect.bottom = start->y + 1;
826
827         if(start->x <= end->x)
828         {
829             left = start->x;
830             right = end->x - 1;
831             l_to_r = TRUE;
832         }
833         else
834         {
835             left = end->x + 1;
836             right = start->x;
837             l_to_r = FALSE;
838         }
839
840         rect.left = min( start->x, end->x );
841         rect.right = max( start->x, end->x ) + 1;
842         get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
843         for (i = 0; i < clipped_rects.count; i++)
844         {
845             if(clipped_rects.rects[i].right > left && clipped_rects.rects[i].left <= right)
846             {
847                 int clipped_left  = max(clipped_rects.rects[i].left, left);
848                 int clipped_right = min(clipped_rects.rects[i].right - 1, right);
849
850                 pdev->dash_pos = start_pos;
851
852                 if(l_to_r)
853                 {
854                     cur_x = clipped_left;
855                     if(cur_x != left)
856                         skip_dash(pdev, clipped_left - left);
857
858                     while(cur_x <= clipped_right)
859                     {
860                         rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
861                         dash_len = pdev->dash_pos.left_in_dash;
862                         if(cur_x + dash_len > clipped_right + 1)
863                             dash_len = clipped_right - cur_x + 1;
864                         rect.left = cur_x;
865                         rect.right = cur_x + dash_len;
866
867                         pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
868                         cur_x += dash_len;
869                         skip_dash(pdev, dash_len);
870                     }
871                 }
872                 else
873                 {
874                     cur_x = clipped_right;
875                     if(cur_x != right)
876                         skip_dash(pdev, right - clipped_right);
877
878                     while(cur_x >= clipped_left)
879                     {
880                         rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
881                         dash_len = pdev->dash_pos.left_in_dash;
882                         if(cur_x - dash_len < clipped_left - 1)
883                             dash_len = cur_x - clipped_left + 1;
884                         rect.left = cur_x - dash_len + 1;
885                         rect.right = cur_x + 1;
886
887                         pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
888                         cur_x -= dash_len;
889                         skip_dash(pdev, dash_len);
890                     }
891                 }
892             }
893         }
894         pdev->dash_pos = start_pos;
895         skip_dash(pdev, right - left + 1);
896     }
897     else if(start->x == end->x) /* vline */
898     {
899         BOOL t_to_b;
900         INT top, bottom, cur_y;
901
902         rect.left = start->x;
903         rect.right = start->x + 1;
904
905         if(start->y <= end->y)
906         {
907             top = start->y;
908             bottom = end->y - 1;
909             t_to_b = TRUE;
910         }
911         else
912         {
913             top = end->y + 1;
914             bottom = start->y;
915             t_to_b = FALSE;
916         }
917
918         rect.top = min( start->y, end->y );
919         rect.bottom = max( start->y, end->y ) + 1;
920         get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
921         for (i = 0; i < clipped_rects.count; i++)
922         {
923             if(clipped_rects.rects[i].right > start->x && clipped_rects.rects[i].left <= start->x)
924             {
925                 int clipped_top    = max(clipped_rects.rects[i].top, top);
926                 int clipped_bottom = min(clipped_rects.rects[i].bottom - 1, bottom);
927
928                 pdev->dash_pos = start_pos;
929
930                 if(t_to_b)
931                 {
932                     cur_y = clipped_top;
933                     if(cur_y != top)
934                         skip_dash(pdev, clipped_top - top);
935
936                     while(cur_y <= clipped_bottom)
937                     {
938                         rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
939                         dash_len = pdev->dash_pos.left_in_dash;
940                         if(cur_y + dash_len > clipped_bottom + 1)
941                             dash_len = clipped_bottom - cur_y + 1;
942                         rect.top = cur_y;
943                         rect.bottom = cur_y + dash_len;
944
945                         pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
946                         cur_y += dash_len;
947                         skip_dash(pdev, dash_len);
948                     }
949                 }
950                 else
951                 {
952                     cur_y = clipped_bottom;
953                     if(cur_y != bottom)
954                         skip_dash(pdev, bottom - clipped_bottom);
955
956                     while(cur_y >= clipped_top)
957                     {
958                         rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
959                         dash_len = pdev->dash_pos.left_in_dash;
960                         if(cur_y - dash_len < clipped_top - 1)
961                             dash_len = cur_y - clipped_top + 1;
962                         rect.top = cur_y - dash_len + 1;
963                         rect.bottom = cur_y + 1;
964
965                         pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
966                         cur_y -= dash_len;
967                         skip_dash(pdev, dash_len);
968                     }
969                 }
970             }
971         }
972         pdev->dash_pos = start_pos;
973         skip_dash(pdev, bottom - top + 1);
974     }
975     else
976     {
977         bres_params clip_params;
978         struct line_params line_params;
979         INT dx = end->x - start->x, dy = end->y - start->y;
980         INT abs_dx = abs(dx), abs_dy = abs(dy);
981
982         clip_params.dx = abs_dx;
983         clip_params.dy = abs_dy;
984         clip_params.octant = get_octant_mask(dx, dy);
985         clip_params.bias   = get_bias( clip_params.octant );
986
987         line_params.bias    = clip_params.bias;
988         line_params.x_major = is_xmajor( clip_params.octant );
989         line_params.x_inc   = is_x_increasing( clip_params.octant ) ? 1 : -1;
990         line_params.y_inc   = is_y_increasing( clip_params.octant ) ? 1 : -1;
991
992         if (line_params.x_major)
993         {
994             line_params.err_add_1 = 2 * abs_dy - 2 * abs_dx;
995             line_params.err_add_2 = 2 * abs_dy;
996         }
997         else
998         {
999             line_params.err_add_1 = 2 * abs_dx - 2 * abs_dy;
1000             line_params.err_add_2 = 2 * abs_dx;
1001         }
1002
1003         rect.left   = min( start->x, end->x );
1004         rect.top    = min( start->y, end->y );
1005         rect.right  = max( start->x, end->x ) + 1;
1006         rect.bottom = max( start->y, end->y ) + 1;
1007         get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
1008         for (i = 0; i < clipped_rects.count; i++)
1009         {
1010             POINT clipped_start, clipped_end;
1011             int clip_status;
1012             clip_status = clip_line(start, end, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
1013
1014             if(clip_status)
1015             {
1016                 int m = abs(clipped_start.x - start->x);
1017                 int n = abs(clipped_start.y - start->y);
1018
1019                 pdev->dash_pos = start_pos;
1020
1021                 if (line_params.x_major)
1022                 {
1023                     line_params.err_start = 2 * abs_dy - abs_dx + m * 2 * abs_dy - n * 2 * abs_dx;
1024                     line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
1025                     skip_dash(pdev, m);
1026                 }
1027                 else
1028                 {
1029                     line_params.err_start = 2 * abs_dx - abs_dy + n * 2 * abs_dx - m * 2 * abs_dy;
1030                     line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
1031                     skip_dash(pdev, n);
1032                 }
1033                 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
1034
1035                 bres_line_with_bias( &clipped_start, &line_params, dashed_pen_line_callback, pdev );
1036
1037                 if(clip_status == 2) break; /* completely unclipped, so we can finish */
1038             }
1039         }
1040         pdev->dash_pos = start_pos;
1041         if(line_params.x_major)
1042             skip_dash(pdev, abs_dx);
1043         else
1044             skip_dash(pdev, abs_dy);
1045     }
1046
1047     free_clipped_rects( &clipped_rects );
1048     return TRUE;
1049 }
1050
1051 static BOOL dashed_pen_line_region(dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region)
1052 {
1053     int i, dash_len;
1054     RECT rect;
1055
1056     rect.left   = start->x;
1057     rect.top    = start->y;
1058     rect.right  = start->x + 1;
1059     rect.bottom = start->y + 1;
1060
1061     if (start->y == end->y) /* hline */
1062     {
1063         if (start->x <= end->x)
1064         {
1065             for (i = start->x; i < end->x; i += dash_len)
1066             {
1067                 dash_len = min( pdev->dash_pos.left_in_dash, end->x - i );
1068                 if (pdev->dash_pos.mark)
1069                 {
1070                     rect.left = i;
1071                     rect.right = i + dash_len;
1072                     add_rect_to_region( region, &rect );
1073                 }
1074                 skip_dash(pdev, dash_len);
1075             }
1076         }
1077         else
1078         {
1079             for (i = start->x; i > end->x; i -= dash_len)
1080             {
1081                 dash_len = min( pdev->dash_pos.left_in_dash, i - end->x );
1082                 if (pdev->dash_pos.mark)
1083                 {
1084                     rect.left = i - dash_len + 1;
1085                     rect.right = i + 1;
1086                     add_rect_to_region( region, &rect );
1087                 }
1088                 skip_dash(pdev, dash_len);
1089             }
1090         }
1091     }
1092     else if (start->x == end->x) /* vline */
1093     {
1094         if (start->y <= end->y)
1095         {
1096             for (i = start->y; i < end->y; i += dash_len)
1097             {
1098                 dash_len = min( pdev->dash_pos.left_in_dash, end->y - i );
1099                 if (pdev->dash_pos.mark)
1100                 {
1101                     rect.top = i;
1102                     rect.bottom = i + dash_len;
1103                     add_rect_to_region( region, &rect );
1104                 }
1105                 skip_dash(pdev, dash_len);
1106             }
1107         }
1108         else
1109         {
1110             for (i = start->y; i > end->y; i -= dash_len)
1111             {
1112                 dash_len = min( pdev->dash_pos.left_in_dash, i - end->y );
1113                 if (pdev->dash_pos.mark)
1114                 {
1115                     rect.top = i - dash_len + 1;
1116                     rect.bottom = i + 1;
1117                     add_rect_to_region( region, &rect );
1118                 }
1119                 skip_dash(pdev, dash_len);
1120             }
1121         }
1122     }
1123     else
1124     {
1125         INT dx = end->x - start->x, dy = end->y - start->y;
1126         INT abs_dx = abs(dx), abs_dy = abs(dy);
1127         DWORD octant = get_octant_mask(dx, dy);
1128         INT bias = get_bias( octant );
1129         int x_inc = is_x_increasing( octant ) ? 1 : -1;
1130         int y_inc = is_y_increasing( octant ) ? 1 : -1;
1131
1132         if (is_xmajor( octant ))
1133         {
1134             int err_add_1 = 2 * abs_dy - 2 * abs_dx;
1135             int err_add_2 = 2 * abs_dy;
1136             int err = 2 * abs_dy - abs_dx;
1137
1138             while (abs_dx--)
1139             {
1140                 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1141                 skip_dash(pdev, 1);
1142                 rect.left += x_inc;
1143                 rect.right += x_inc;
1144                 if (err + bias > 0)
1145                 {
1146                     rect.top += y_inc;
1147                     rect.bottom += y_inc;
1148                     err += err_add_1;
1149                 }
1150                 else err += err_add_2;
1151             }
1152
1153         }
1154         else
1155         {
1156             int err_add_1 = 2 * abs_dx - 2 * abs_dy;
1157             int err_add_2 = 2 * abs_dx;
1158             int err = 2 * abs_dx - abs_dy;
1159
1160             while (abs_dy--)
1161             {
1162                 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1163                 skip_dash(pdev, 1);
1164                 rect.top += y_inc;
1165                 rect.bottom += y_inc;
1166                 if (err + bias > 0)
1167                 {
1168                     rect.left += x_inc;
1169                     rect.right += x_inc;
1170                     err += err_add_1;
1171                 }
1172                 else err += err_add_2;
1173             }
1174         }
1175     }
1176     return TRUE;
1177 }
1178
1179 static BOOL dashed_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1180 {
1181     int i;
1182
1183     assert( num >= 2 );
1184
1185     if (region)
1186     {
1187         for (i = 0; i < num - 1; i++)
1188             if (!dashed_pen_line_region( pdev, pts + i, pts + i + 1, region ))
1189                 return FALSE;
1190         if (close) return dashed_pen_line_region( pdev, pts + num - 1, pts, region );
1191     }
1192     else
1193     {
1194         get_color_masks( pdev, GetROP2(pdev->dev.hdc), pdev->pen_brush.colorref,
1195                          pdev->pen_is_ext ? TRANSPARENT : GetBkMode(pdev->dev.hdc),
1196                          &pdev->dash_masks[1], &pdev->dash_masks[0] );
1197
1198         for (i = 0; i < num - 1; i++)
1199             if (!dashed_pen_line( pdev, pts + i, pts + i + 1 ))
1200                 return FALSE;
1201         if (close) return dashed_pen_line( pdev, pts + num - 1, pts );
1202     }
1203     return TRUE;
1204 }
1205
1206 static BOOL null_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1207 {
1208     return TRUE;
1209 }
1210
1211 struct face
1212 {
1213     POINT start, end;
1214     int dx, dy;
1215 };
1216
1217 static void add_cap( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt )
1218 {
1219     switch (pdev->pen_endcap)
1220     {
1221     default: FIXME( "Unknown end cap %x\n", pdev->pen_endcap );
1222         /* fall through */
1223     case PS_ENDCAP_ROUND:
1224         OffsetRgn( round_cap, pt->x, pt->y );
1225         CombineRgn( region, region, round_cap, RGN_OR );
1226         OffsetRgn( round_cap, -pt->x, -pt->y );
1227         return;
1228
1229     case PS_ENDCAP_SQUARE: /* already been handled */
1230     case PS_ENDCAP_FLAT:
1231         return;
1232     }
1233 }
1234
1235 #define round( f ) (((f) > 0) ? (f) + 0.5 : (f) - 0.5)
1236
1237 /*******************************************************************************
1238  *                 create_miter_region
1239  *
1240  * We need to calculate the intersection of two lines.  We know a point
1241  * on each line (a face start and the other face end point) and
1242  * the direction vector of each line eg. (dx_1, dy_1).
1243  *
1244  * (x, y) = (x_1, y_1) + u * (dx_1, dy_1) = (x_2, y_2) + v * (dx_2, dy_2)
1245  * solving (eg using Cramer's rule) gives:
1246  * u = ((x_2 - x_1) dy_2 - (y_2 - y_1) dx_2) / det
1247  * with det = dx_1 dy_2 - dx_2 dy_1
1248  * substituting back in and simplifying gives
1249  * (x, y) = a (dx_1, dy_1) - b (dx_2, dy_2)
1250  * with a = (x_2 dy_2 - y_2 dx_2) / det
1251  * and  b = (x_1 dy_1 - y_1 dx_1) / det
1252  */
1253 static HRGN create_miter_region( dibdrv_physdev *pdev, const POINT *pt,
1254                                  const struct face *face_1, const struct face *face_2 )
1255 {
1256     int det = face_1->dx * face_2->dy - face_1->dy * face_2->dx;
1257     POINT pt_1, pt_2, pts[5];
1258     double a, b, x, y;
1259     FLOAT limit;
1260
1261     if (det == 0) return 0;
1262
1263     if (det < 0)
1264     {
1265         const struct face *tmp = face_1;
1266         face_1 = face_2;
1267         face_2 = tmp;
1268         det = -det;
1269     }
1270
1271     pt_1 = face_1->start;
1272     pt_2 = face_2->end;
1273
1274     a = (double)((pt_2.x * face_2->dy - pt_2.y * face_2->dx)) / det;
1275     b = (double)((pt_1.x * face_1->dy - pt_1.y * face_1->dx)) / det;
1276
1277     x = a * face_1->dx - b * face_2->dx;
1278     y = a * face_1->dy - b * face_2->dy;
1279
1280     GetMiterLimit( pdev->dev.hdc, &limit );
1281
1282     if (((x - pt->x) * (x - pt->x) + (y - pt->y) * (y - pt->y)) * 4 > limit * limit * pdev->pen_width * pdev->pen_width)
1283         return 0;
1284
1285     pts[0] = face_2->start;
1286     pts[1] = face_1->start;
1287     pts[2].x = round( x );
1288     pts[2].y = round( y );
1289     pts[3] = face_2->end;
1290     pts[4] = face_1->end;
1291
1292     return CreatePolygonRgn( pts, 5, ALTERNATE );
1293 }
1294
1295 static void add_join( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt,
1296                       const struct face *face_1, const struct face *face_2 )
1297 {
1298     HRGN join;
1299     POINT pts[4];
1300
1301     switch (pdev->pen_join)
1302     {
1303     default: FIXME( "Unknown line join %x\n", pdev->pen_join );
1304         /* fall through */
1305     case PS_JOIN_ROUND:
1306         OffsetRgn( round_cap, pt->x, pt->y );
1307         CombineRgn( region, region, round_cap, RGN_OR );
1308         OffsetRgn( round_cap, -pt->x, -pt->y );
1309         return;
1310
1311     case PS_JOIN_MITER:
1312         join = create_miter_region( pdev, pt, face_1, face_2 );
1313         if (join) break;
1314         /* fall through */
1315     case PS_JOIN_BEVEL:
1316         pts[0] = face_1->start;
1317         pts[1] = face_2->end;
1318         pts[2] = face_1->end;
1319         pts[3] = face_2->start;
1320         join = CreatePolygonRgn( pts, 4, ALTERNATE );
1321         break;
1322     }
1323
1324     CombineRgn( region, region, join, RGN_OR );
1325     DeleteObject( join );
1326     return;
1327 }
1328
1329 static void wide_line_segment( dibdrv_physdev *pdev, HRGN total,
1330                                const POINT *pt_1, const POINT *pt_2, int dx, int dy,
1331                                BOOL need_cap_1, BOOL need_cap_2, struct face *face_1, struct face *face_2 )
1332 {
1333     RECT rect;
1334     BOOL sq_cap_1 = need_cap_1 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1335     BOOL sq_cap_2 = need_cap_2 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1336
1337     if (dx == 0 && dy == 0) return;
1338
1339     if (dy == 0)
1340     {
1341         rect.left = min( pt_1->x, pt_2->x );
1342         rect.right = max( pt_1->x, pt_2->x );
1343         rect.top = pt_1->y - pdev->pen_width / 2;
1344         rect.bottom = rect.top + pdev->pen_width;
1345         if ((sq_cap_1 && dx > 0) || (sq_cap_2 && dx < 0)) rect.left  -= pdev->pen_width / 2;
1346         if ((sq_cap_2 && dx > 0) || (sq_cap_1 && dx < 0)) rect.right += pdev->pen_width / 2;
1347         add_rect_to_region( total, &rect );
1348         if (dx > 0)
1349         {
1350             face_1->start.x = face_1->end.x   = rect.left;
1351             face_1->start.y = face_2->end.y   = rect.bottom;
1352             face_1->end.y   = face_2->start.y = rect.top;
1353             face_2->start.x = face_2->end.x   = rect.right - 1;
1354         }
1355         else
1356         {
1357             face_1->start.x = face_1->end.x   = rect.right;
1358             face_1->start.y = face_2->end.y   = rect.top;
1359             face_1->end.y   = face_2->start.y = rect.bottom;
1360             face_2->start.x = face_2->end.x   = rect.left + 1;
1361         }
1362     }
1363     else if (dx == 0)
1364     {
1365         rect.top = min( pt_1->y, pt_2->y );
1366         rect.bottom = max( pt_1->y, pt_2->y );
1367         rect.left = pt_1->x - pdev->pen_width / 2;
1368         rect.right = rect.left + pdev->pen_width;
1369         if ((sq_cap_1 && dy > 0) || (sq_cap_2 && dy < 0)) rect.top    -= pdev->pen_width / 2;
1370         if ((sq_cap_2 && dy > 0) || (sq_cap_1 && dy < 0)) rect.bottom += pdev->pen_width / 2;
1371         add_rect_to_region( total, &rect );
1372         if (dy > 0)
1373         {
1374             face_1->start.x = face_2->end.x   = rect.left;
1375             face_1->start.y = face_1->end.y   = rect.top;
1376             face_1->end.x   = face_2->start.x = rect.right;
1377             face_2->start.y = face_2->end.y   = rect.bottom - 1;
1378         }
1379         else
1380         {
1381             face_1->start.x = face_2->end.x   = rect.right;
1382             face_1->start.y = face_1->end.y   = rect.bottom;
1383             face_1->end.x   = face_2->start.x = rect.left;
1384             face_2->start.y = face_2->end.y   = rect.top + 1;
1385         }
1386     }
1387     else
1388     {
1389         double len = hypot( dx, dy );
1390         double width_x, width_y;
1391         POINT seg_pts[4];
1392         POINT wide_half, narrow_half;
1393         HRGN segment;
1394
1395         width_x = pdev->pen_width * abs( dy ) / len;
1396         width_y = pdev->pen_width * abs( dx ) / len;
1397
1398         narrow_half.x = round( width_x / 2 );
1399         narrow_half.y = round( width_y / 2 );
1400         wide_half.x   = round( (width_x + 1) / 2 );
1401         wide_half.y   = round( (width_y + 1) / 2 );
1402
1403         if (dx < 0)
1404         {
1405             wide_half.y   = -wide_half.y;
1406             narrow_half.y = -narrow_half.y;
1407         }
1408
1409         if (dy < 0)
1410         {
1411             POINT tmp = narrow_half; narrow_half = wide_half; wide_half = tmp;
1412             wide_half.x   = -wide_half.x;
1413             narrow_half.x = -narrow_half.x;
1414         }
1415
1416         seg_pts[0].x = pt_1->x - narrow_half.x;
1417         seg_pts[0].y = pt_1->y + narrow_half.y;
1418         seg_pts[1].x = pt_1->x + wide_half.x;
1419         seg_pts[1].y = pt_1->y - wide_half.y;
1420         seg_pts[2].x = pt_2->x + wide_half.x;
1421         seg_pts[2].y = pt_2->y - wide_half.y;
1422         seg_pts[3].x = pt_2->x - narrow_half.x;
1423         seg_pts[3].y = pt_2->y + narrow_half.y;
1424
1425         if (sq_cap_1)
1426         {
1427             seg_pts[0].x -= narrow_half.y;
1428             seg_pts[1].x -= narrow_half.y;
1429             seg_pts[0].y -= narrow_half.x;
1430             seg_pts[1].y -= narrow_half.x;
1431         }
1432
1433         if (sq_cap_2)
1434         {
1435             seg_pts[2].x += wide_half.y;
1436             seg_pts[3].x += wide_half.y;
1437             seg_pts[2].y += wide_half.x;
1438             seg_pts[3].y += wide_half.x;
1439         }
1440
1441         segment = CreatePolygonRgn( seg_pts, 4, ALTERNATE );
1442         CombineRgn( total, total, segment, RGN_OR );
1443         DeleteObject( segment );
1444
1445         face_1->start = seg_pts[0];
1446         face_1->end   = seg_pts[1];
1447         face_2->start = seg_pts[2];
1448         face_2->end   = seg_pts[3];
1449     }
1450
1451     face_1->dx = face_2->dx = dx;
1452     face_1->dy = face_2->dy = dy;
1453 }
1454
1455 static void wide_line_segments( dibdrv_physdev *pdev, int num, const POINT *pts, BOOL close,
1456                                 int start, int count, const POINT *first_pt, const POINT *last_pt,
1457                                 HRGN round_cap, HRGN total )
1458 {
1459     int i;
1460     struct face face_1, face_2, prev_face, first_face;
1461     const POINT *pt_1, *pt_2;
1462
1463     if (!close)
1464     {
1465         add_cap( pdev, total, round_cap, first_pt );
1466         add_cap( pdev, total, round_cap, last_pt );
1467     }
1468
1469     if (count == 1)
1470     {
1471         pt_1 = &pts[start];
1472         pt_2 = &pts[(start + 1) % num];
1473         wide_line_segment( pdev, total, first_pt, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1474                            TRUE, TRUE, &face_1, &face_2 );
1475         return;
1476     }
1477
1478     pt_1 = &pts[start];
1479     pt_2 = &pts[(start + 1) % num];
1480     wide_line_segment( pdev, total, first_pt, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1481                        !close, FALSE, &first_face, &prev_face );
1482
1483
1484     for (i = 1; i < count - 1; i++)
1485     {
1486         pt_1 = &pts[(start + i) % num];
1487         pt_2 = &pts[(start + i + 1) % num];
1488         wide_line_segment( pdev, total, pt_1, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1489                            FALSE, FALSE, &face_1, &face_2 );
1490         add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1491         prev_face = face_2;
1492     }
1493
1494     pt_1 = &pts[(start + count - 1) % num];
1495     pt_2 = &pts[(start + count) % num];
1496     wide_line_segment( pdev, total, pt_1, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1497                        FALSE, !close, &face_1, &face_2 );
1498     add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1499     if (close) add_join( pdev, total, round_cap, last_pt, &face_2, &first_face );
1500 }
1501
1502 static BOOL wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1503 {
1504     HRGN round_cap = 0;
1505
1506     assert( total != 0 );  /* wide pens should always be drawn through a region */
1507     assert( num >= 2 );
1508
1509     /* skip empty segments */
1510     while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1511     while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1512
1513     if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1514         round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1515                                        (pdev->pen_width + 1) / 2 + 1, (pdev->pen_width + 1) / 2 + 1 );
1516
1517     if (close)
1518         wide_line_segments( pdev, num, pts, TRUE, 0, num, &pts[0], &pts[0], round_cap, total );
1519     else
1520         wide_line_segments( pdev, num, pts, FALSE, 0, num - 1, &pts[0], &pts[num - 1], round_cap, total );
1521
1522     if (round_cap) DeleteObject( round_cap );
1523     return TRUE;
1524 }
1525
1526 static BOOL dashed_wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1527 {
1528     int i, start, cur_len, initial_num = 0;
1529     POINT initial_point, start_point, end_point;
1530     HRGN round_cap = 0;
1531
1532     assert( total != 0 );  /* wide pens should always be drawn through a region */
1533     assert( num >= 2 );
1534
1535     /* skip empty segments */
1536     while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1537     while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1538
1539     if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1540         round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1541                                        (pdev->pen_width + 1) / 2 + 1, (pdev->pen_width + 1) / 2 + 1);
1542
1543     start = 0;
1544     cur_len = 0;
1545     start_point = pts[0];
1546
1547     for (i = 0; i < (close ? num : num - 1); i++)
1548     {
1549         const POINT *pt_1 = pts + i;
1550         const POINT *pt_2 = pts + ((close && i == num - 1) ? 0 : i + 1);
1551         int dx = pt_2->x - pt_1->x;
1552         int dy = pt_2->y - pt_1->y;
1553
1554         if (dx == 0 && dy == 0) continue;
1555
1556         if (dy == 0)
1557         {
1558             if (abs( dx ) - cur_len < pdev->dash_pos.left_in_dash)
1559             {
1560                 skip_dash( pdev, abs( dx ) - cur_len );
1561                 cur_len = 0;
1562                 continue;
1563             }
1564             cur_len += pdev->dash_pos.left_in_dash;
1565             dx = (dx > 0) ? cur_len : -cur_len;
1566         }
1567         else if (dx == 0)
1568         {
1569             if (abs( dy ) - cur_len < pdev->dash_pos.left_in_dash)
1570             {
1571                 skip_dash( pdev, abs( dy ) - cur_len );
1572                 cur_len = 0;
1573                 continue;
1574             }
1575             cur_len += pdev->dash_pos.left_in_dash;
1576             dy = (dy > 0) ? cur_len : -cur_len;
1577         }
1578         else
1579         {
1580             double len = hypot( dx, dy );
1581
1582             if (len - cur_len < pdev->dash_pos.left_in_dash)
1583             {
1584                 skip_dash( pdev, len - cur_len );
1585                 cur_len = 0;
1586                 continue;
1587             }
1588             cur_len += pdev->dash_pos.left_in_dash;
1589             dx = dx * cur_len / len;
1590             dy = dy * cur_len / len;
1591         }
1592         end_point.x = pt_1->x + dx;
1593         end_point.y = pt_1->y + dy;
1594
1595         if (pdev->dash_pos.mark)
1596         {
1597             if (!initial_num && close)  /* this is the first dash, save it for later */
1598             {
1599                 initial_num = i - start + 1;
1600                 initial_point = end_point;
1601             }
1602             else wide_line_segments( pdev, num, pts, FALSE, start, i - start + 1,
1603                                      &start_point, &end_point, round_cap, total );
1604         }
1605         if (!initial_num) initial_num = -1;  /* no need to close it */
1606
1607         skip_dash( pdev, pdev->dash_pos.left_in_dash );
1608         start_point = end_point;
1609         start = i;
1610         i--;  /* go on with the same segment */
1611     }
1612
1613     if (pdev->dash_pos.mark)  /* we have a final dash */
1614     {
1615         int count;
1616
1617         if (initial_num > 0)
1618         {
1619             count = num - start + initial_num;
1620             end_point = initial_point;
1621         }
1622         else if (close)
1623         {
1624             count = num - start;
1625             end_point = pts[0];
1626         }
1627         else
1628         {
1629             count = num - start - 1;
1630             end_point = pts[num - 1];
1631         }
1632         wide_line_segments( pdev, num, pts, FALSE, start, count,
1633                             &start_point, &end_point, round_cap, total );
1634     }
1635     else if (initial_num > 0)  /* initial dash only */
1636     {
1637         wide_line_segments( pdev, num, pts, FALSE, 0, initial_num,
1638                             &pts[0], &initial_point, round_cap, total );
1639     }
1640
1641     if (round_cap) DeleteObject( round_cap );
1642     return TRUE;
1643 }
1644
1645 static const dash_pattern dash_patterns_cosmetic[4] =
1646 {
1647     {2, {18, 6}, 24},             /* PS_DASH */
1648     {2, {3,  3}, 6},              /* PS_DOT */
1649     {4, {9, 6, 3, 6}, 24},        /* PS_DASHDOT */
1650     {6, {9, 3, 3, 3, 3, 3}, 24}   /* PS_DASHDOTDOT */
1651 };
1652
1653 static const dash_pattern dash_patterns_geometric[4] =
1654 {
1655     {2, {3, 1}, 4},               /* PS_DASH */
1656     {2, {1, 1}, 2},               /* PS_DOT */
1657     {4, {3, 1, 1, 1}, 6},         /* PS_DASHDOT */
1658     {6, {3, 1, 1, 1, 1, 1}, 8}    /* PS_DASHDOTDOT */
1659 };
1660
1661 static inline void set_dash_pattern( dash_pattern *pattern, DWORD count, DWORD *dashes )
1662 {
1663     DWORD i;
1664
1665     pattern->count = count;
1666     pattern->total_len = 0;
1667     memcpy( pattern->dashes, dashes, count * sizeof(DWORD) );
1668     for (i = 0; i < count; i++) pattern->total_len += dashes[i];
1669     if (pattern->count % 2) pattern->total_len *= 2;
1670 }
1671
1672 static inline void scale_dash_pattern( dash_pattern *pattern, DWORD scale, DWORD endcap )
1673 {
1674     DWORD i;
1675
1676     for (i = 0; i < pattern->count; i++) pattern->dashes[i] *= scale;
1677     pattern->total_len *= scale;
1678
1679     if (endcap != PS_ENDCAP_FLAT)  /* shrink the dashes to leave room for the caps */
1680     {
1681         for (i = 0; i < pattern->count; i += 2)
1682         {
1683             pattern->dashes[i] -= scale;
1684             pattern->dashes[i + 1] += scale;
1685         }
1686     }
1687 }
1688
1689 static inline int get_pen_device_width( dibdrv_physdev *pdev, int width )
1690 {
1691     POINT pts[2];
1692
1693     if (!width) return 1;
1694     pts[0].x = pts[0].y = pts[1].y = 0;
1695     pts[1].x = width;
1696     LPtoDP( pdev->dev.hdc, pts, 2 );
1697     width = abs( pts[1].x - pts[0].x );
1698     return max( width, 1 );
1699 }
1700
1701 /***********************************************************************
1702  *           dibdrv_SetDCPenColor
1703  */
1704 COLORREF dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1705 {
1706     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCPenColor );
1707     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1708
1709     if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
1710         pdev->pen_brush.colorref = color;
1711
1712     return next->funcs->pSetDCPenColor( next, color );
1713 }
1714
1715 /**********************************************************************
1716  *             solid_brush
1717  *
1718  * Fill a number of rectangles with the solid brush
1719  */
1720 static BOOL solid_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1721                         int num, const RECT *rects, INT rop)
1722 {
1723     rop_mask brush_color;
1724     DWORD color = get_pixel_color( pdev, brush->colorref, TRUE );
1725
1726     calc_rop_masks( rop, color, &brush_color );
1727     dib->funcs->solid_rects( dib, num, rects, brush_color.and, brush_color.xor );
1728     return TRUE;
1729 }
1730
1731 static void free_pattern_brush_bits( dib_brush *brush )
1732 {
1733     HeapFree(GetProcessHeap(), 0, brush->and_bits);
1734     HeapFree(GetProcessHeap(), 0, brush->xor_bits);
1735     brush->and_bits = NULL;
1736     brush->xor_bits = NULL;
1737 }
1738
1739 void free_pattern_brush( dib_brush *brush )
1740 {
1741     free_pattern_brush_bits( brush );
1742     free_dib_info( &brush->dib );
1743 }
1744
1745 static BOOL create_pattern_brush_bits( dib_brush *brush )
1746 {
1747     DWORD size = brush->dib.height * abs(brush->dib.stride);
1748     DWORD *brush_bits = brush->dib.bits.ptr;
1749     DWORD *and_bits, *xor_bits;
1750
1751     assert(brush->and_bits == NULL);
1752     assert(brush->xor_bits == NULL);
1753
1754     assert(brush->dib.stride > 0);
1755
1756     and_bits = brush->and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1757     xor_bits = brush->xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1758
1759     if(!and_bits || !xor_bits)
1760     {
1761         ERR("Failed to create pattern brush bits\n");
1762         free_pattern_brush_bits( brush );
1763         return FALSE;
1764     }
1765
1766     while(size)
1767     {
1768         calc_and_xor_masks(brush->rop, *brush_bits++, and_bits++, xor_bits++);
1769         size -= 4;
1770     }
1771
1772     return TRUE;
1773 }
1774
1775 static const DWORD hatches[6][8] =
1776 {
1777     { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1778     { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL   */
1779     { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL  */
1780     { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL  */
1781     { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS      */
1782     { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }  /* HS_DIAGCROSS  */
1783 };
1784
1785 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect)
1786 {
1787     dib_info hatch;
1788     rop_mask fg_mask, bg_mask;
1789     rop_mask_bits mask_bits;
1790     DWORD size;
1791     BOOL ret;
1792
1793     assert(brush->and_bits == NULL);
1794     assert(brush->xor_bits == NULL);
1795
1796     /* Just initialise brush dib with the color / sizing info.  We don't
1797        need the bits as we'll calculate the rop masks straight from
1798        the hatch patterns. */
1799
1800     copy_dib_color_info(&brush->dib, &pdev->dib);
1801     brush->dib.width  = 8;
1802     brush->dib.height = 8;
1803     brush->dib.stride = get_dib_stride( brush->dib.width, brush->dib.bit_count );
1804
1805     size = brush->dib.height * brush->dib.stride;
1806
1807     mask_bits.and = brush->and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1808     mask_bits.xor = brush->xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1809
1810     if(!mask_bits.and || !mask_bits.xor)
1811     {
1812         ERR("Failed to create pattern brush bits\n");
1813         free_pattern_brush_bits( brush );
1814         return FALSE;
1815     }
1816
1817     hatch.bit_count = 1;
1818     hatch.height = hatch.width = 8;
1819     hatch.stride = 4;
1820     hatch.bits.ptr = (void *) hatches[brush->hatch];
1821     hatch.bits.free = hatch.bits.param = NULL;
1822     hatch.bits.is_copy = FALSE;
1823
1824     get_color_masks( pdev, brush->rop, brush->colorref, GetBkMode(pdev->dev.hdc),
1825                      &fg_mask, &bg_mask );
1826
1827     if (brush->colorref & (1 << 24))  /* PALETTEINDEX */
1828         *needs_reselect = TRUE;
1829     if (GetBkMode(pdev->dev.hdc) != TRANSPARENT && (GetBkColor(pdev->dev.hdc) & (1 << 24)))
1830         *needs_reselect = TRUE;
1831
1832     ret = brush->dib.funcs->create_rop_masks( &brush->dib, &hatch, &fg_mask, &bg_mask, &mask_bits );
1833     if(!ret) free_pattern_brush_bits( brush );
1834
1835     return ret;
1836 }
1837
1838 static BOOL matching_pattern_format( dib_info *dib, dib_info *pattern )
1839 {
1840     if (dib->bit_count != pattern->bit_count) return FALSE;
1841     if (dib->stride != pattern->stride) return FALSE;
1842
1843     switch (dib->bit_count)
1844     {
1845     case 1:
1846     case 4:
1847     case 8:
1848         if (dib->color_table_size != pattern->color_table_size) return FALSE;
1849         return !memcmp( dib->color_table, pattern->color_table, dib->color_table_size * sizeof(RGBQUAD) );
1850     case 16:
1851     case 32:
1852         return (dib->red_mask == pattern->red_mask &&
1853                 dib->green_mask == pattern->green_mask &&
1854                 dib->blue_mask == pattern->blue_mask);
1855     }
1856     return TRUE;
1857 }
1858
1859 static BOOL select_pattern_brush( dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect )
1860 {
1861     char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1862     BITMAPINFO *info = (BITMAPINFO *)buffer;
1863     RGBQUAD color_table[2];
1864     RECT rect;
1865     dib_info pattern;
1866
1867     if (!brush->pattern.info)
1868     {
1869         BITMAPOBJ *bmp = GDI_GetObjPtr( brush->pattern.bitmap, OBJ_BITMAP );
1870         BOOL ret;
1871
1872         if (!bmp) return FALSE;
1873         ret = init_dib_info_from_bitmapobj( &pattern, bmp, 0 );
1874         GDI_ReleaseObj( brush->pattern.bitmap );
1875         if (!ret) return FALSE;
1876     }
1877     else if (brush->pattern.info->bmiHeader.biClrUsed && brush->pattern.usage == DIB_PAL_COLORS)
1878     {
1879         copy_bitmapinfo( info, brush->pattern.info );
1880         fill_color_table_from_pal_colors( info, pdev->dev.hdc );
1881         init_dib_info_from_bitmapinfo( &pattern, info, brush->pattern.bits.ptr, 0 );
1882         *needs_reselect = TRUE;
1883     }
1884     else
1885     {
1886         init_dib_info_from_bitmapinfo( &pattern, brush->pattern.info, brush->pattern.bits.ptr, 0 );
1887     }
1888
1889     if (pattern.bit_count == 1 && !pattern.color_table)
1890     {
1891         /* monochrome DDB pattern uses DC colors */
1892         DWORD pixel;
1893         BOOL got_pixel;
1894         COLORREF color;
1895
1896         color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetTextColor( pdev->dev.hdc ),
1897                                    &got_pixel, &pixel );
1898         color_table[0].rgbRed      = GetRValue( color );
1899         color_table[0].rgbGreen    = GetGValue( color );
1900         color_table[0].rgbBlue     = GetBValue( color );
1901         color_table[0].rgbReserved = 0;
1902
1903         color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetBkColor( pdev->dev.hdc ),
1904                                    &got_pixel, &pixel );
1905         color_table[1].rgbRed      = GetRValue( color );
1906         color_table[1].rgbGreen    = GetGValue( color );
1907         color_table[1].rgbBlue     = GetBValue( color );
1908         color_table[1].rgbReserved = 0;
1909
1910         pattern.color_table = color_table;
1911         pattern.color_table_size = 2;
1912         *needs_reselect = TRUE;
1913     }
1914
1915     copy_dib_color_info(&brush->dib, &pdev->dib);
1916
1917     brush->dib.height = pattern.height;
1918     brush->dib.width  = pattern.width;
1919     brush->dib.stride = get_dib_stride( brush->dib.width, brush->dib.bit_count );
1920
1921     if (matching_pattern_format( &brush->dib, &pattern ))
1922     {
1923         brush->dib.bits.ptr     = pattern.bits.ptr;
1924         brush->dib.bits.is_copy = FALSE;
1925         brush->dib.bits.free    = NULL;
1926     }
1927     else
1928     {
1929         brush->dib.bits.ptr     = HeapAlloc( GetProcessHeap(), 0, brush->dib.height * brush->dib.stride );
1930         brush->dib.bits.is_copy = TRUE;
1931         brush->dib.bits.free    = free_heap_bits;
1932
1933         rect.left = rect.top = 0;
1934         rect.right = pattern.width;
1935         rect.bottom = pattern.height;
1936
1937         brush->dib.funcs->convert_to(&brush->dib, &pattern, &rect);
1938     }
1939     return TRUE;
1940 }
1941
1942 /**********************************************************************
1943  *             pattern_brush
1944  *
1945  * Fill a number of rectangles with the pattern brush
1946  * FIXME: Should we insist l < r && t < b?  Currently we assume this.
1947  */
1948 static BOOL pattern_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1949                           int num, const RECT *rects, INT rop)
1950 {
1951     POINT origin;
1952     BOOL needs_reselect = FALSE;
1953
1954     if (rop != brush->rop)
1955     {
1956         free_pattern_brush_bits( brush );
1957         brush->rop = rop;
1958     }
1959
1960     if(brush->and_bits == NULL)
1961     {
1962         switch(brush->style)
1963         {
1964         case BS_DIBPATTERN:
1965             if (!brush->dib.bits.ptr && !select_pattern_brush( pdev, brush, &needs_reselect ))
1966                 return FALSE;
1967             if(!create_pattern_brush_bits( brush ))
1968                 return FALSE;
1969             break;
1970
1971         case BS_HATCHED:
1972             if(!create_hatch_brush_bits(pdev, brush, &needs_reselect))
1973                 return FALSE;
1974             break;
1975
1976         default:
1977             ERR("Unexpected brush style %d\n", brush->style);
1978             return FALSE;
1979         }
1980     }
1981
1982     GetBrushOrgEx(pdev->dev.hdc, &origin);
1983
1984     dib->funcs->pattern_rects( dib, num, rects, &origin, &brush->dib, brush->and_bits, brush->xor_bits );
1985
1986     if (needs_reselect) free_pattern_brush( brush );
1987     return TRUE;
1988 }
1989
1990 static BOOL null_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1991                        int num, const RECT *rects, INT rop)
1992 {
1993     return TRUE;
1994 }
1995
1996 static void select_brush( dib_brush *brush, const LOGBRUSH *logbrush, const struct brush_pattern *pattern )
1997 {
1998     free_pattern_brush( brush );
1999
2000     if (pattern)
2001     {
2002         brush->style   = BS_DIBPATTERN;
2003         brush->pattern = *pattern;  /* brush is actually selected only when it's used */
2004         brush->rects   = pattern_brush;
2005     }
2006     else
2007     {
2008         brush->style    = logbrush->lbStyle;
2009         brush->colorref = logbrush->lbColor;
2010         brush->hatch    = logbrush->lbHatch;
2011
2012         switch (logbrush->lbStyle)
2013         {
2014         case BS_SOLID:   brush->rects = solid_brush; break;
2015         case BS_NULL:    brush->rects = null_brush; break;
2016         case BS_HATCHED: brush->rects = pattern_brush; break;
2017         }
2018     }
2019 }
2020
2021 /***********************************************************************
2022  *           dibdrv_SelectBrush
2023  */
2024 HBRUSH dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush, const struct brush_pattern *pattern )
2025 {
2026     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBrush );
2027     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2028     LOGBRUSH logbrush;
2029
2030     TRACE("(%p, %p)\n", dev, hbrush);
2031
2032     GetObjectW( hbrush, sizeof(logbrush), &logbrush );
2033
2034     if (hbrush == GetStockObject( DC_BRUSH ))
2035         logbrush.lbColor = GetDCBrushColor( dev->hdc );
2036
2037     select_brush( &pdev->brush, &logbrush, pattern );
2038
2039     return next->funcs->pSelectBrush( next, hbrush, pattern );
2040 }
2041
2042 /***********************************************************************
2043  *           dibdrv_SelectPen
2044  */
2045 HPEN dibdrv_SelectPen( PHYSDEV dev, HPEN hpen, const struct brush_pattern *pattern )
2046 {
2047     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectPen );
2048     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2049     LOGPEN logpen;
2050     LOGBRUSH logbrush;
2051     EXTLOGPEN *elp = NULL;
2052
2053     TRACE("(%p, %p)\n", dev, hpen);
2054
2055     if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
2056     {
2057         /* must be an extended pen */
2058         INT size = GetObjectW( hpen, 0, NULL );
2059
2060         if (!size) return 0;
2061
2062         elp = HeapAlloc( GetProcessHeap(), 0, size );
2063
2064         GetObjectW( hpen, size, elp );
2065         logpen.lopnStyle = elp->elpPenStyle;
2066         logpen.lopnWidth.x = elp->elpWidth;
2067         /* cosmetic ext pens are always 1-pixel wide */
2068         if (!(logpen.lopnStyle & PS_GEOMETRIC)) logpen.lopnWidth.x = 0;
2069
2070         logbrush.lbStyle = elp->elpBrushStyle;
2071         logbrush.lbColor = elp->elpColor;
2072         logbrush.lbHatch = elp->elpHatch;
2073     }
2074     else
2075     {
2076         logbrush.lbStyle = BS_SOLID;
2077         logbrush.lbColor = logpen.lopnColor;
2078         logbrush.lbHatch = 0;
2079     }
2080
2081     pdev->pen_join   = logpen.lopnStyle & PS_JOIN_MASK;
2082     pdev->pen_endcap = logpen.lopnStyle & PS_ENDCAP_MASK;
2083     pdev->pen_width  = get_pen_device_width( pdev, logpen.lopnWidth.x );
2084
2085     if (hpen == GetStockObject( DC_PEN ))
2086         logbrush.lbColor = GetDCPenColor( dev->hdc );
2087
2088     set_dash_pattern( &pdev->pen_pattern, 0, NULL );
2089     select_brush( &pdev->pen_brush, &logbrush, pattern );
2090
2091     pdev->pen_style = logpen.lopnStyle & PS_STYLE_MASK;
2092
2093     switch (pdev->pen_style)
2094     {
2095     case PS_DASH:
2096     case PS_DOT:
2097     case PS_DASHDOT:
2098     case PS_DASHDOTDOT:
2099         if (logpen.lopnStyle & PS_GEOMETRIC)
2100         {
2101             pdev->pen_pattern = dash_patterns_geometric[pdev->pen_style - 1];
2102             if (pdev->pen_width > 1)
2103             {
2104                 scale_dash_pattern( &pdev->pen_pattern, pdev->pen_width, pdev->pen_endcap );
2105                 pdev->pen_lines = dashed_wide_pen_lines;
2106             }
2107             else pdev->pen_lines = dashed_pen_lines;
2108             break;
2109         }
2110         if (pdev->pen_width == 1)  /* wide cosmetic pens are not dashed */
2111         {
2112             pdev->pen_lines = dashed_pen_lines;
2113             pdev->pen_pattern = dash_patterns_cosmetic[pdev->pen_style - 1];
2114             break;
2115         }
2116         /* fall through */
2117     case PS_SOLID:
2118     case PS_INSIDEFRAME:
2119         pdev->pen_lines = (pdev->pen_width == 1) ? solid_pen_lines : wide_pen_lines;
2120         break;
2121
2122     case PS_NULL:
2123         pdev->pen_width = 0;
2124         pdev->pen_lines = null_pen_lines;
2125         break;
2126
2127     case PS_ALTERNATE:
2128         pdev->pen_lines = dashed_pen_lines;
2129         pdev->pen_pattern = dash_patterns_geometric[PS_DOT - 1];
2130         break;
2131
2132     case PS_USERSTYLE:
2133         pdev->pen_lines = (pdev->pen_width == 1) ? dashed_pen_lines : dashed_wide_pen_lines;
2134         set_dash_pattern( &pdev->pen_pattern, elp->elpNumEntries, elp->elpStyleEntry );
2135         if (!(logpen.lopnStyle & PS_GEOMETRIC)) scale_dash_pattern( &pdev->pen_pattern, 3, PS_ENDCAP_FLAT );
2136         break;
2137     }
2138
2139     pdev->pen_uses_region = (logpen.lopnStyle & PS_GEOMETRIC || pdev->pen_width > 1);
2140     pdev->pen_is_ext = (elp != NULL);
2141     HeapFree( GetProcessHeap(), 0, elp );
2142
2143     return next->funcs->pSelectPen( next, hpen, pattern );
2144 }
2145
2146 /***********************************************************************
2147  *           dibdrv_SetDCBrushColor
2148  */
2149 COLORREF dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
2150 {
2151     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCBrushColor );
2152     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2153
2154     if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
2155         pdev->brush.colorref = color;
2156
2157     return next->funcs->pSetDCBrushColor( next, color );
2158 }
2159
2160 BOOL brush_rect(dibdrv_physdev *pdev, dib_brush *brush, const RECT *rect, HRGN clip, INT rop)
2161 {
2162     struct clipped_rects clipped_rects;
2163     BOOL ret;
2164
2165     if (!get_clipped_rects( &pdev->dib, rect, clip, &clipped_rects )) return TRUE;
2166     ret = brush->rects( pdev, brush, &pdev->dib, clipped_rects.count, clipped_rects.rects, rop );
2167     free_clipped_rects( &clipped_rects );
2168     return ret;
2169 }
2170
2171 /* paint a region with the brush (note: the region can be modified) */
2172 BOOL brush_region( dibdrv_physdev *pdev, HRGN region )
2173 {
2174     if (pdev->clip) CombineRgn( region, region, pdev->clip, RGN_AND );
2175     return brush_rect( pdev, &pdev->brush, NULL, region, GetROP2( pdev->dev.hdc ));
2176 }
2177
2178 /* paint a region with the pen (note: the region can be modified) */
2179 BOOL pen_region( dibdrv_physdev *pdev, HRGN region )
2180 {
2181     if (pdev->clip) CombineRgn( region, region, pdev->clip, RGN_AND );
2182     return brush_rect( pdev, &pdev->pen_brush, NULL, region, GetROP2( pdev->dev.hdc ));
2183 }