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