gdi32: Always use proper RGB colors for monochrome DDB pattern brushes.
[wine/multimedia.git] / dlls / gdi32 / dibdrv / objects.c
blob96596dfe6428474db87e22194e72c7f77e3fc8fd
1 /*
2 * DIB driver GDI objects.
4 * Copyright 2011 Huw Davies
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.
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.
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
21 #include <assert.h>
22 #include <stdlib.h>
24 #include "gdi_private.h"
25 #include "dibdrv.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dib);
33 * Decompose the 16 ROP2s into an expression of the form
35 * D = (D & A) ^ X
37 * Where A and X depend only on P (and so can be precomputed).
39 * A X
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
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}
66 static const DWORD rop2_and_array[16][2] =
68 ZERO, NOT_P, NOT_P, ZERO,
69 P, ONE, ONE, P,
70 P, ONE, ONE, P,
71 ZERO, NOT_P, NOT_P, ZERO
74 /* X = (P & X1) ^ X2 */
75 static const DWORD rop2_xor_array[16][2] =
77 ZERO, NOT_P, ZERO, NOT_P,
78 P, ONE, P, ONE,
79 ZERO, NOT_P, ZERO, NOT_P,
80 P, ONE, P, ONE
83 #undef NOT_P
84 #undef P
85 #undef ONE
86 #undef ZERO
88 void get_rop_codes(INT rop, struct rop_codes *codes)
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];
97 static inline void calc_and_xor_masks(INT rop, DWORD color, DWORD *and, DWORD *xor)
99 struct rop_codes codes;
100 get_rop_codes( rop, &codes );
102 *and = (color & codes.a1) ^ codes.a2;
103 *xor = (color & codes.x1) ^ codes.x2;
106 static inline void calc_rop_masks(INT rop, DWORD color, rop_mask *masks)
108 calc_and_xor_masks( rop, color, &masks->and, &masks->xor );
111 static inline RGBQUAD rgbquad_from_colorref(COLORREF c)
113 RGBQUAD ret;
115 ret.rgbRed = GetRValue(c);
116 ret.rgbGreen = GetGValue(c);
117 ret.rgbBlue = GetBValue(c);
118 ret.rgbReserved = 0;
119 return ret;
122 static inline BOOL rgbquad_equal(const RGBQUAD *a, const RGBQUAD *b)
124 if(a->rgbRed == b->rgbRed &&
125 a->rgbGreen == b->rgbGreen &&
126 a->rgbBlue == b->rgbBlue)
127 return TRUE;
128 return FALSE;
131 COLORREF make_rgb_colorref( HDC hdc, dib_info *dib, COLORREF color, BOOL *got_pixel, DWORD *pixel )
133 *pixel = 0;
134 *got_pixel = FALSE;
136 if (color & (1 << 24)) /* PALETTEINDEX */
138 HPALETTE pal = GetCurrentObject( hdc, OBJ_PAL );
139 PALETTEENTRY pal_ent;
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 );
146 if (color >> 16 == 0x10ff) /* DIBINDEX */
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 );
157 return color & 0xffffff;
160 /******************************************************************
161 * get_pixel_color
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.
169 DWORD get_pixel_color( dibdrv_physdev *pdev, COLORREF color, BOOL mono_fixup )
171 RGBQUAD fg_quad;
172 BOOL got_pixel;
173 DWORD pixel;
174 COLORREF rgb_ref;
176 rgb_ref = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, color, &got_pixel, &pixel );
177 if (got_pixel) return pixel;
179 if (pdev->dib.bit_count != 1 || !mono_fixup)
180 return pdev->dib.funcs->colorref_to_pixel( &pdev->dib, rgb_ref );
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;
188 pixel = get_pixel_color( pdev, GetBkColor(pdev->dev.hdc), FALSE );
189 if (color == GetBkColor(pdev->dev.hdc)) return pixel;
190 else return !pixel;
193 /***************************************************************************
194 * get_color_masks
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).
200 static inline void get_color_masks( dibdrv_physdev *pdev, UINT rop, COLORREF colorref,
201 rop_mask *fg_mask, rop_mask *bg_mask )
203 DWORD color = get_pixel_color( pdev, colorref, TRUE );
205 calc_rop_masks( rop, color, fg_mask );
207 if (GetBkMode(pdev->dev.hdc) == TRANSPARENT)
209 bg_mask->and = ~0u;
210 bg_mask->xor = 0;
211 return;
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;
217 calc_rop_masks( rop, color, bg_mask );
220 static inline void order_end_points(int *s, int *e)
222 if(*s > *e)
224 int tmp;
225 tmp = *s + 1;
226 *s = *e + 1;
227 *e = tmp;
231 #define Y_INCREASING_MASK 0x0f
232 #define X_INCREASING_MASK 0xc3
233 #define X_MAJOR_MASK 0x99
234 #define POS_SLOPE_MASK 0x33
236 static inline BOOL is_xmajor(DWORD octant)
238 return octant & X_MAJOR_MASK;
241 static inline BOOL is_pos_slope(DWORD octant)
243 return octant & POS_SLOPE_MASK;
246 static inline BOOL is_x_increasing(DWORD octant)
248 return octant & X_INCREASING_MASK;
251 static inline BOOL is_y_increasing(DWORD octant)
253 return octant & Y_INCREASING_MASK;
256 /**********************************************************************
257 * get_octant_number
259 * Return the octant number starting clockwise from the +ve x-axis.
261 static inline int get_octant_number(int dx, int dy)
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;
275 static inline DWORD get_octant_mask(int dx, int dy)
277 return 1 << (get_octant_number(dx, dy) - 1);
280 static inline int get_bias( DWORD mask )
282 /* Octants 3, 5, 6 and 8 take a bias */
283 return (mask & 0xb4) ? 1 : 0;
286 #define OUT_LEFT 1
287 #define OUT_RIGHT 2
288 #define OUT_TOP 4
289 #define OUT_BOTTOM 8
291 static inline DWORD calc_outcode(const POINT *pt, const RECT *clip)
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;
299 return out;
302 /******************************************************************************
303 * clip_line
305 * Clips the start and end points to a rectangle.
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.
312 * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
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.
318 * For x major lines we have 2dy >= err + bias > 2dy - 2dx
319 * 0 >= err + bias - 2dy > -2dx
321 * Note dx, dy, m and n are all +ve.
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)
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
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
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
358 * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
360 * Moving start point from y1 to y1 + n find x1 + m
361 * m = (2ndx + bias + dy - 1) / 2dy
363 * Moving start point from x1 to x1 + m find y1 + n
364 * n = (2mdy - bias - dy) / 2ndx + 1
366 * Moving end point from y2 to y2 - n find x1 - m
367 * m = (2ndx - bias + dy) / 2dy
369 * Moving end point from x2 to x2 - m find y2 - n
370 * n = (2mdy + bias - dy - 1) / 2dx + 1
372 int clip_line(const POINT *start, const POINT *end, const RECT *clip,
373 const bres_params *params, POINT *pt1, POINT *pt2)
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);
386 *pt1 = *start;
387 *pt2 = *end;
389 start_oc = calc_outcode(start, clip);
390 end_oc = calc_outcode(end, clip);
392 while(1)
394 if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
395 if(start_oc & end_oc) return 0; /* trivial reject */
397 clipped = TRUE;
398 if(start_oc & OUT_LEFT)
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;
406 pt1->x = clip->left;
407 if(neg_slope) n = -n;
408 pt1->y = start->y + n;
409 start_oc = calc_outcode(pt1, clip);
411 else if(start_oc & OUT_RIGHT)
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;
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);
424 else if(start_oc & OUT_TOP)
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;
432 pt1->y = clip->top;
433 if(neg_slope) m = -m;
434 pt1->x = start->x + m;
435 start_oc = calc_outcode(pt1, clip);
437 else if(start_oc & OUT_BOTTOM)
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;
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);
450 else if(end_oc & OUT_LEFT)
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;
458 pt2->x = clip->left;
459 if(neg_slope) n = -n;
460 pt2->y = end->y + n;
461 end_oc = calc_outcode(pt2, clip);
463 else if(end_oc & OUT_RIGHT)
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;
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);
476 else if(end_oc & OUT_TOP)
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;
484 pt2->y = clip->top;
485 if(neg_slope) m = -m;
486 pt2->x = end->x + m;
487 end_oc = calc_outcode(pt2, clip);
489 else if(end_oc & OUT_BOTTOM)
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;
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);
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)
508 POINT pt = *start;
509 int len = params->length, err = params->err_start;
511 if (params->x_major)
513 while(len--)
515 callback(pdev, pt.x, pt.y);
516 if (err + params->bias > 0)
518 pt.y += params->y_inc;
519 err += params->err_add_1;
521 else err += params->err_add_2;
522 pt.x += params->x_inc;
525 else
527 while(len--)
529 callback(pdev, pt.x, pt.y);
530 if (err + params->bias > 0)
532 pt.x += params->x_inc;
533 err += params->err_add_1;
535 else err += params->err_add_2;
536 pt.y += params->y_inc;
541 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end, DWORD and, DWORD xor)
543 const WINEREGION *clip = get_wine_region(pdev->clip);
545 if(start->y == end->y)
547 RECT rect;
548 int i;
550 rect.left = start->x;
551 rect.top = start->y;
552 rect.right = end->x;
553 rect.bottom = end->y + 1;
554 order_end_points(&rect.left, &rect.right);
555 for(i = 0; i < clip->numRects; i++)
557 if(clip->rects[i].top >= rect.bottom) break;
558 if(clip->rects[i].bottom <= rect.top) continue;
559 /* Optimize the unclipped case */
560 if(clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
562 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
563 break;
565 if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
567 RECT tmp = rect;
568 tmp.left = max(rect.left, clip->rects[i].left);
569 tmp.right = min(rect.right, clip->rects[i].right);
570 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, and, xor);
574 else if(start->x == end->x)
576 RECT rect;
577 int i;
579 rect.left = start->x;
580 rect.top = start->y;
581 rect.right = end->x + 1;
582 rect.bottom = end->y;
583 order_end_points(&rect.top, &rect.bottom);
584 for(i = 0; i < clip->numRects; i++)
586 /* Optimize unclipped case */
587 if(clip->rects[i].top <= rect.top && clip->rects[i].bottom >= rect.bottom &&
588 clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
590 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
591 break;
593 if(clip->rects[i].top >= rect.bottom) break;
594 if(clip->rects[i].bottom <= rect.top) continue;
595 if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
597 RECT tmp = rect;
598 tmp.top = max(rect.top, clip->rects[i].top);
599 tmp.bottom = min(rect.bottom, clip->rects[i].bottom);
600 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, and, xor);
604 else
606 bres_params clip_params;
607 struct line_params line_params;
608 INT dx = end->x - start->x, dy = end->y - start->y;
609 INT abs_dx = abs(dx), abs_dy = abs(dy);
610 INT i;
612 clip_params.dx = abs_dx;
613 clip_params.dy = abs_dy;
614 clip_params.octant = get_octant_mask(dx, dy);
615 clip_params.bias = get_bias( clip_params.octant );
617 line_params.bias = clip_params.bias;
618 line_params.x_major = is_xmajor( clip_params.octant );
619 line_params.x_inc = is_x_increasing( clip_params.octant ) ? 1 : -1;
620 line_params.y_inc = is_y_increasing( clip_params.octant ) ? 1 : -1;
622 if (line_params.x_major)
624 line_params.err_add_1 = 2 * abs_dy - 2 * abs_dx;
625 line_params.err_add_2 = 2 * abs_dy;
627 else
629 line_params.err_add_1 = 2 * abs_dx - 2 * abs_dy;
630 line_params.err_add_2 = 2 * abs_dx;
633 for(i = 0; i < clip->numRects; i++)
635 POINT clipped_start, clipped_end;
636 int clip_status;
637 clip_status = clip_line(start, end, clip->rects + i, &clip_params, &clipped_start, &clipped_end);
639 if(clip_status)
641 int m = abs(clipped_start.x - start->x);
642 int n = abs(clipped_start.y - start->y);
644 if (line_params.x_major)
646 line_params.err_start = 2 * abs_dy - abs_dx + m * 2 * abs_dy - n * 2 * abs_dx;
647 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
649 else
651 line_params.err_start = 2 * abs_dx - abs_dy + n * 2 * abs_dx - m * 2 * abs_dy;
652 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
655 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
657 pdev->dib.funcs->solid_line( &pdev->dib, &clipped_start, &line_params, and, xor );
659 if(clip_status == 2) break; /* completely unclipped, so we can finish */
664 release_wine_region(pdev->clip);
665 return TRUE;
668 static BOOL solid_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close)
670 int i;
671 DWORD color, and, xor;
673 color = get_pixel_color( pdev, pdev->pen_colorref, TRUE );
674 calc_and_xor_masks( GetROP2(pdev->dev.hdc), color, &and, &xor );
676 assert( num >= 2 );
677 for (i = 0; i < num - 1; i++)
678 if (!solid_pen_line( pdev, pts + i, pts + i + 1, and, xor ))
679 return FALSE;
681 if (close) return solid_pen_line( pdev, pts + num - 1, pts, and, xor );
683 return TRUE;
686 void reset_dash_origin(dibdrv_physdev *pdev)
688 pdev->dash_pos.cur_dash = 0;
689 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
690 pdev->dash_pos.mark = TRUE;
693 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
695 skip %= pdev->pen_pattern.total_len;
696 while(skip)
698 if(pdev->dash_pos.left_in_dash > skip)
700 pdev->dash_pos.left_in_dash -= skip;
701 return;
703 skip -= pdev->dash_pos.left_in_dash;
704 pdev->dash_pos.cur_dash++;
705 if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
706 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
707 pdev->dash_pos.mark = !pdev->dash_pos.mark;
711 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
713 RECT rect;
714 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
716 skip_dash(pdev, 1);
717 rect.left = x;
718 rect.right = x + 1;
719 rect.top = y;
720 rect.bottom = y + 1;
721 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
722 return;
725 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
727 const WINEREGION *clip = get_wine_region(pdev->clip);
728 int i, dash_len;
729 RECT rect;
730 const dash_pos start_pos = pdev->dash_pos;
732 if(start->y == end->y) /* hline */
734 BOOL l_to_r;
735 INT left, right, cur_x;
737 rect.top = start->y;
738 rect.bottom = start->y + 1;
740 if(start->x <= end->x)
742 left = start->x;
743 right = end->x - 1;
744 l_to_r = TRUE;
746 else
748 left = end->x + 1;
749 right = start->x;
750 l_to_r = FALSE;
753 for(i = 0; i < clip->numRects; i++)
755 if(clip->rects[i].top > start->y) break;
756 if(clip->rects[i].bottom <= start->y) continue;
758 if(clip->rects[i].right > left && clip->rects[i].left <= right)
760 int clipped_left = max(clip->rects[i].left, left);
761 int clipped_right = min(clip->rects[i].right - 1, right);
763 pdev->dash_pos = start_pos;
765 if(l_to_r)
767 cur_x = clipped_left;
768 if(cur_x != left)
769 skip_dash(pdev, clipped_left - left);
771 while(cur_x <= clipped_right)
773 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
774 dash_len = pdev->dash_pos.left_in_dash;
775 if(cur_x + dash_len > clipped_right + 1)
776 dash_len = clipped_right - cur_x + 1;
777 rect.left = cur_x;
778 rect.right = cur_x + dash_len;
780 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
781 cur_x += dash_len;
782 skip_dash(pdev, dash_len);
785 else
787 cur_x = clipped_right;
788 if(cur_x != right)
789 skip_dash(pdev, right - clipped_right);
791 while(cur_x >= clipped_left)
793 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
794 dash_len = pdev->dash_pos.left_in_dash;
795 if(cur_x - dash_len < clipped_left - 1)
796 dash_len = cur_x - clipped_left + 1;
797 rect.left = cur_x - dash_len + 1;
798 rect.right = cur_x + 1;
800 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
801 cur_x -= dash_len;
802 skip_dash(pdev, dash_len);
807 pdev->dash_pos = start_pos;
808 skip_dash(pdev, right - left + 1);
810 else if(start->x == end->x) /* vline */
812 BOOL t_to_b;
813 INT top, bottom, cur_y;
815 rect.left = start->x;
816 rect.right = start->x + 1;
818 if(start->y <= end->y)
820 top = start->y;
821 bottom = end->y - 1;
822 t_to_b = TRUE;
824 else
826 top = end->y + 1;
827 bottom = start->y;
828 t_to_b = FALSE;
831 for(i = 0; i < clip->numRects; i++)
833 if(clip->rects[i].top > bottom) break;
834 if(clip->rects[i].bottom <= top) continue;
835 if(clip->rects[i].right > start->x && clip->rects[i].left <= start->x)
837 int clipped_top = max(clip->rects[i].top, top);
838 int clipped_bottom = min(clip->rects[i].bottom - 1, bottom);
840 pdev->dash_pos = start_pos;
842 if(t_to_b)
844 cur_y = clipped_top;
845 if(cur_y != top)
846 skip_dash(pdev, clipped_top - top);
848 while(cur_y <= clipped_bottom)
850 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
851 dash_len = pdev->dash_pos.left_in_dash;
852 if(cur_y + dash_len > clipped_bottom + 1)
853 dash_len = clipped_bottom - cur_y + 1;
854 rect.top = cur_y;
855 rect.bottom = cur_y + dash_len;
857 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
858 cur_y += dash_len;
859 skip_dash(pdev, dash_len);
862 else
864 cur_y = clipped_bottom;
865 if(cur_y != bottom)
866 skip_dash(pdev, bottom - clipped_bottom);
868 while(cur_y >= clipped_top)
870 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
871 dash_len = pdev->dash_pos.left_in_dash;
872 if(cur_y - dash_len < clipped_top - 1)
873 dash_len = cur_y - clipped_top + 1;
874 rect.top = cur_y - dash_len + 1;
875 rect.bottom = cur_y + 1;
877 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
878 cur_y -= dash_len;
879 skip_dash(pdev, dash_len);
884 pdev->dash_pos = start_pos;
885 skip_dash(pdev, bottom - top + 1);
887 else
889 bres_params clip_params;
890 struct line_params line_params;
891 INT dx = end->x - start->x, dy = end->y - start->y;
892 INT abs_dx = abs(dx), abs_dy = abs(dy);
893 INT i;
895 clip_params.dx = abs_dx;
896 clip_params.dy = abs_dy;
897 clip_params.octant = get_octant_mask(dx, dy);
898 clip_params.bias = get_bias( clip_params.octant );
900 line_params.bias = clip_params.bias;
901 line_params.x_major = is_xmajor( clip_params.octant );
902 line_params.x_inc = is_x_increasing( clip_params.octant ) ? 1 : -1;
903 line_params.y_inc = is_y_increasing( clip_params.octant ) ? 1 : -1;
905 if (line_params.x_major)
907 line_params.err_add_1 = 2 * abs_dy - 2 * abs_dx;
908 line_params.err_add_2 = 2 * abs_dy;
910 else
912 line_params.err_add_1 = 2 * abs_dx - 2 * abs_dy;
913 line_params.err_add_2 = 2 * abs_dx;
916 for(i = 0; i < clip->numRects; i++)
918 POINT clipped_start, clipped_end;
919 int clip_status;
920 clip_status = clip_line(start, end, clip->rects + i, &clip_params, &clipped_start, &clipped_end);
922 if(clip_status)
924 int m = abs(clipped_start.x - start->x);
925 int n = abs(clipped_start.y - start->y);
927 pdev->dash_pos = start_pos;
929 if (line_params.x_major)
931 line_params.err_start = 2 * abs_dy - abs_dx + m * 2 * abs_dy - n * 2 * abs_dx;
932 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
933 skip_dash(pdev, m);
935 else
937 line_params.err_start = 2 * abs_dx - abs_dy + n * 2 * abs_dx - m * 2 * abs_dy;
938 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
939 skip_dash(pdev, n);
941 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
943 bres_line_with_bias( &clipped_start, &line_params, dashed_pen_line_callback, pdev );
945 if(clip_status == 2) break; /* completely unclipped, so we can finish */
948 pdev->dash_pos = start_pos;
949 if(line_params.x_major)
950 skip_dash(pdev, abs_dx);
951 else
952 skip_dash(pdev, abs_dy);
955 release_wine_region(pdev->clip);
956 return TRUE;
959 static BOOL dashed_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close)
961 int i;
963 get_color_masks( pdev, GetROP2(pdev->dev.hdc), pdev->pen_colorref,
964 &pdev->dash_masks[1], &pdev->dash_masks[0] );
966 assert( num >= 2 );
967 for (i = 0; i < num - 1; i++)
968 if (!dashed_pen_line( pdev, pts + i, pts + i + 1 ))
969 return FALSE;
971 if (close) return dashed_pen_line( pdev, pts + num - 1, pts );
973 return TRUE;
976 static BOOL null_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close)
978 return TRUE;
981 struct face
983 POINT start, end;
984 int dx, dy;
987 static void add_cap( dibdrv_physdev *pdev, HRGN region, const POINT *pt )
989 HRGN cap;
991 switch (pdev->pen_endcap)
993 default: FIXME( "Unknown end cap %x\n", pdev->pen_endcap );
994 /* fall through */
995 case PS_ENDCAP_ROUND:
996 cap = CreateEllipticRgn( pt->x - pdev->pen_width / 2, pt->y - pdev->pen_width / 2,
997 pt->x + (pdev->pen_width + 1) / 2, pt->y + (pdev->pen_width + 1) / 2 );
998 break;
1000 case PS_ENDCAP_SQUARE: /* already been handled */
1001 case PS_ENDCAP_FLAT:
1002 return;
1005 CombineRgn( region, region, cap, RGN_OR );
1006 DeleteObject( cap );
1007 return;
1010 #define round( f ) (((f) > 0) ? (f) + 0.5 : (f) - 0.5)
1012 /*******************************************************************************
1013 * create_miter_region
1015 * We need to calculate the intersection of two lines. We know a point
1016 * on each line (a face start and the other face end point) and
1017 * the direction vector of each line eg. (dx_1, dy_1).
1019 * (x, y) = (x_1, y_1) + u * (dx_1, dy_1) = (x_2, y_2) + v * (dx_2, dy_2)
1020 * solving (eg using Cramer's rule) gives:
1021 * u = ((x_2 - x_1) dy_2 - (y_2 - y_1) dx_2) / det
1022 * with det = dx_1 dy_2 - dx_2 dy_1
1023 * substituting back in and simplifying gives
1024 * (x, y) = a (dx_1, dy_1) - b (dx_2, dy_2)
1025 * with a = (x_2 dy_2 - y_2 dx_2) / det
1026 * and b = (x_1 dy_1 - y_1 dx_1) / det
1028 static HRGN create_miter_region( dibdrv_physdev *pdev, const POINT *pt,
1029 const struct face *face_1, const struct face *face_2 )
1031 int det = face_1->dx * face_2->dy - face_1->dy * face_2->dx;
1032 POINT pt_1, pt_2, pts[5];
1033 double a, b, x, y;
1034 FLOAT limit;
1036 if (det == 0) return 0;
1038 if (det < 0)
1040 const struct face *tmp = face_1;
1041 face_1 = face_2;
1042 face_2 = tmp;
1043 det = -det;
1046 pt_1 = face_1->start;
1047 pt_2 = face_2->end;
1049 a = (double)((pt_2.x * face_2->dy - pt_2.y * face_2->dx)) / det;
1050 b = (double)((pt_1.x * face_1->dy - pt_1.y * face_1->dx)) / det;
1052 x = a * face_1->dx - b * face_2->dx;
1053 y = a * face_1->dy - b * face_2->dy;
1055 GetMiterLimit( pdev->dev.hdc, &limit );
1057 if (((x - pt->x) * (x - pt->x) + (y - pt->y) * (y - pt->y)) * 4 > limit * limit * pdev->pen_width * pdev->pen_width)
1058 return 0;
1060 pts[0] = face_2->start;
1061 pts[1] = face_1->start;
1062 pts[2].x = round( x );
1063 pts[2].y = round( y );
1064 pts[3] = face_2->end;
1065 pts[4] = face_1->end;
1067 return CreatePolygonRgn( pts, 5, ALTERNATE );
1070 static void add_join( dibdrv_physdev *pdev, HRGN region, const POINT *pt,
1071 const struct face *face_1, const struct face *face_2 )
1073 HRGN join;
1074 POINT pts[4];
1076 switch (pdev->pen_join)
1078 default: FIXME( "Unknown line join %x\n", pdev->pen_join );
1079 /* fall through */
1080 case PS_JOIN_ROUND:
1081 join = CreateEllipticRgn( pt->x - pdev->pen_width / 2, pt->y - pdev->pen_width / 2,
1082 pt->x + (pdev->pen_width + 1) / 2, pt->y + (pdev->pen_width + 1) / 2 );
1083 break;
1085 case PS_JOIN_MITER:
1086 join = create_miter_region( pdev, pt, face_1, face_2 );
1087 if (join) break;
1088 /* fall through */
1089 case PS_JOIN_BEVEL:
1090 pts[0] = face_1->start;
1091 pts[1] = face_2->end;
1092 pts[2] = face_1->end;
1093 pts[3] = face_2->start;
1094 join = CreatePolygonRgn( pts, 4, ALTERNATE );
1095 break;
1098 CombineRgn( region, region, join, RGN_OR );
1099 DeleteObject( join );
1100 return;
1103 static HRGN get_wide_lines_region( dibdrv_physdev *pdev, int num, POINT *pts, BOOL close )
1105 int i;
1106 HRGN total, segment;
1108 assert( num >= 2 );
1110 total = CreateRectRgn( 0, 0, 0, 0 );
1112 if (!close) num--;
1113 for (i = 0; i < num; i++)
1115 const POINT *pt_1 = pts + i;
1116 const POINT *pt_2 = pts + ((close && i == num - 1) ? 0 : i + 1);
1117 int dx = pt_2->x - pt_1->x;
1118 int dy = pt_2->y - pt_1->y;
1119 RECT rect;
1120 struct face face_1, face_2, prev_face, first_face;
1121 BOOL need_cap_1 = !close && (i == 0);
1122 BOOL need_cap_2 = !close && (i == num - 1);
1123 BOOL sq_cap_1 = need_cap_1 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1124 BOOL sq_cap_2 = need_cap_2 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1126 if (dx == 0 && dy == 0) continue;
1128 if (dy == 0)
1130 rect.left = min( pt_1->x, pt_2->x );
1131 rect.right = rect.left + abs( dx );
1132 rect.top = pt_1->y - pdev->pen_width / 2;
1133 rect.bottom = rect.top + pdev->pen_width;
1134 if ((sq_cap_1 && dx > 0) || (sq_cap_2 && dx < 0)) rect.left -= pdev->pen_width / 2;
1135 if ((sq_cap_2 && dx > 0) || (sq_cap_1 && dx < 0)) rect.right += pdev->pen_width / 2;
1136 segment = CreateRectRgnIndirect( &rect );
1137 if (dx > 0)
1139 face_1.start.x = face_1.end.x = rect.left;
1140 face_1.start.y = face_2.end.y = rect.bottom;
1141 face_1.end.y = face_2.start.y = rect.top;
1142 face_2.start.x = face_2.end.x = rect.right - 1;
1144 else
1146 face_1.start.x = face_1.end.x = rect.right;
1147 face_1.start.y = face_2.end.y = rect.top;
1148 face_1.end.y = face_2.start.y = rect.bottom;
1149 face_2.start.x = face_2.end.x = rect.left + 1;
1152 else if (dx == 0)
1154 rect.top = min( pt_1->y, pt_2->y );
1155 rect.bottom = rect.top + abs( dy );
1156 rect.left = pt_1->x - pdev->pen_width / 2;
1157 rect.right = rect.left + pdev->pen_width;
1158 if ((sq_cap_1 && dy > 0) || (sq_cap_2 && dy < 0)) rect.top -= pdev->pen_width / 2;
1159 if ((sq_cap_2 && dy > 0) || (sq_cap_1 && dy < 0)) rect.bottom += pdev->pen_width / 2;
1160 segment = CreateRectRgnIndirect( &rect );
1161 if (dy > 0)
1163 face_1.start.x = face_2.end.x = rect.left;
1164 face_1.start.y = face_1.end.y = rect.top;
1165 face_1.end.x = face_2.start.x = rect.right;
1166 face_2.start.y = face_2.end.y = rect.bottom - 1;
1168 else
1170 face_1.start.x = face_2.end.x = rect.right;
1171 face_1.start.y = face_1.end.y = rect.bottom;
1172 face_1.end.x = face_2.start.x = rect.left;
1173 face_2.start.y = face_2.end.y = rect.top + 1;
1176 else
1178 double len = hypot( dx, dy );
1179 double width_x, width_y;
1180 POINT seg_pts[4];
1181 POINT wide_half, narrow_half;
1183 width_x = pdev->pen_width * abs( dy ) / len;
1184 width_y = pdev->pen_width * abs( dx ) / len;
1186 narrow_half.x = round( width_x / 2 );
1187 narrow_half.y = round( width_y / 2 );
1188 wide_half.x = round( (width_x + 1) / 2 );
1189 wide_half.y = round( (width_y + 1) / 2 );
1191 if (dx < 0)
1193 wide_half.y = -wide_half.y;
1194 narrow_half.y = -narrow_half.y;
1197 if (dy < 0)
1199 POINT tmp = narrow_half; narrow_half = wide_half; wide_half = tmp;
1200 wide_half.x = -wide_half.x;
1201 narrow_half.x = -narrow_half.x;
1204 seg_pts[0].x = pt_1->x - narrow_half.x;
1205 seg_pts[0].y = pt_1->y + narrow_half.y;
1206 seg_pts[1].x = pt_1->x + wide_half.x;
1207 seg_pts[1].y = pt_1->y - wide_half.y;
1208 seg_pts[2].x = pt_2->x + wide_half.x;
1209 seg_pts[2].y = pt_2->y - wide_half.y;
1210 seg_pts[3].x = pt_2->x - narrow_half.x;
1211 seg_pts[3].y = pt_2->y + narrow_half.y;
1213 if (sq_cap_1)
1215 seg_pts[0].x -= narrow_half.y;
1216 seg_pts[1].x -= narrow_half.y;
1217 seg_pts[0].y -= narrow_half.x;
1218 seg_pts[1].y -= narrow_half.x;
1221 if (sq_cap_2)
1223 seg_pts[2].x += wide_half.y;
1224 seg_pts[3].x += wide_half.y;
1225 seg_pts[2].y += wide_half.x;
1226 seg_pts[3].y += wide_half.x;
1229 segment = CreatePolygonRgn( seg_pts, 4, ALTERNATE );
1231 face_1.start = seg_pts[0];
1232 face_1.end = seg_pts[1];
1233 face_2.start = seg_pts[2];
1234 face_2.end = seg_pts[3];
1237 CombineRgn( total, total, segment, RGN_OR );
1238 DeleteObject( segment );
1240 if (need_cap_1) add_cap( pdev, total, pt_1 );
1241 if (need_cap_2) add_cap( pdev, total, pt_2 );
1243 face_1.dx = face_2.dx = dx;
1244 face_1.dy = face_2.dy = dy;
1246 if (i == 0) first_face = face_1;
1247 else add_join( pdev, total, pt_1, &prev_face, &face_1 );
1249 if (i == num - 1 && close)
1250 add_join( pdev, total, pt_2, &face_2, &first_face );
1252 prev_face = face_2;
1254 return total;
1257 static BOOL wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close)
1259 const WINEREGION *data;
1260 rop_mask color;
1261 HRGN region;
1262 DWORD pen_color = get_pixel_color( pdev, pdev->pen_colorref, TRUE );
1264 calc_rop_masks( GetROP2(pdev->dev.hdc), pen_color, &color );
1266 region = get_wide_lines_region( pdev, num, pts, close );
1268 if (CombineRgn( region, region, pdev->clip, RGN_AND ) != ERROR)
1270 data = get_wine_region( region );
1271 solid_rects( &pdev->dib, data->numRects, data->rects, &color, NULL );
1272 release_wine_region( region );
1275 DeleteObject( region );
1276 return TRUE;
1279 static const dash_pattern dash_patterns[5] =
1281 {0, {0}, 0}, /* PS_SOLID - a pseudo-pattern used to initialise unpatterned pens. */
1282 {2, {18, 6}, 24}, /* PS_DASH */
1283 {2, {3, 3}, 6}, /* PS_DOT */
1284 {4, {9, 6, 3, 6}, 24}, /* PS_DASHDOT */
1285 {6, {9, 3, 3, 3, 3, 3}, 24} /* PS_DASHDOTDOT */
1288 static inline int get_pen_device_width( dibdrv_physdev *pdev, LOGPEN *pen )
1290 int width = pen->lopnWidth.x;
1292 if (pen->lopnStyle & PS_GEOMETRIC && width > 1)
1294 POINT pts[2];
1295 pts[0].x = pts[0].y = pts[1].y = 0;
1296 pts[1].x = width;
1297 LPtoDP( pdev->dev.hdc, pts, 2 );
1298 width = max( abs( pts[1].x - pts[0].x ), 1 );
1300 return width;
1303 /***********************************************************************
1304 * dibdrv_SelectPen
1306 HPEN dibdrv_SelectPen( PHYSDEV dev, HPEN hpen )
1308 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectPen );
1309 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1310 LOGPEN logpen;
1311 DWORD style;
1313 TRACE("(%p, %p)\n", dev, hpen);
1315 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
1317 /* must be an extended pen */
1318 EXTLOGPEN *elp;
1319 INT size = GetObjectW( hpen, 0, NULL );
1321 if (!size) return 0;
1323 elp = HeapAlloc( GetProcessHeap(), 0, size );
1325 GetObjectW( hpen, size, elp );
1326 /* FIXME: add support for user style pens */
1327 logpen.lopnStyle = elp->elpPenStyle;
1328 logpen.lopnWidth.x = elp->elpWidth;
1329 logpen.lopnWidth.y = 0;
1330 logpen.lopnColor = elp->elpColor;
1332 HeapFree( GetProcessHeap(), 0, elp );
1335 pdev->pen_join = logpen.lopnStyle & PS_JOIN_MASK;
1336 pdev->pen_endcap = logpen.lopnStyle & PS_ENDCAP_MASK;
1337 pdev->pen_width = get_pen_device_width( pdev, &logpen );
1339 if (hpen == GetStockObject( DC_PEN ))
1340 logpen.lopnColor = GetDCPenColor( dev->hdc );
1342 pdev->pen_colorref = logpen.lopnColor;
1343 pdev->pen_pattern = dash_patterns[PS_SOLID];
1345 pdev->defer |= DEFER_PEN;
1347 style = logpen.lopnStyle & PS_STYLE_MASK;
1349 switch(style)
1351 case PS_SOLID:
1352 if(pdev->pen_width <= 1)
1353 pdev->pen_lines = solid_pen_lines;
1354 else
1355 pdev->pen_lines = wide_pen_lines;
1356 pdev->defer &= ~DEFER_PEN;
1357 break;
1359 case PS_DASH:
1360 case PS_DOT:
1361 case PS_DASHDOT:
1362 case PS_DASHDOTDOT:
1363 if(logpen.lopnStyle & PS_GEOMETRIC) break;
1364 if(logpen.lopnWidth.x > 1) break;
1365 pdev->pen_lines = dashed_pen_lines;
1366 pdev->pen_pattern = dash_patterns[style];
1367 pdev->defer &= ~DEFER_PEN;
1368 break;
1370 case PS_NULL:
1371 pdev->pen_lines = null_pen_lines;
1372 pdev->defer &= ~DEFER_PEN;
1373 break;
1375 default:
1376 break;
1379 return next->funcs->pSelectPen( next, hpen );
1382 /***********************************************************************
1383 * dibdrv_SetDCPenColor
1385 COLORREF dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1387 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCPenColor );
1388 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1390 if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
1391 pdev->pen_colorref = color;
1393 return next->funcs->pSetDCPenColor( next, color );
1396 void solid_rects( dib_info *dib, int num, const RECT *rects, const rop_mask *color, HRGN region )
1398 int i, j;
1399 const WINEREGION *clip;
1401 if (!region)
1403 dib->funcs->solid_rects( dib, num, rects, color->and, color->xor );
1404 return;
1407 clip = get_wine_region( region );
1409 for(i = 0; i < num; i++)
1411 for(j = 0; j < clip->numRects; j++)
1413 RECT clipped_rect;
1415 if (intersect_rect( &clipped_rect, rects + i, clip->rects + j ))
1416 dib->funcs->solid_rects( dib, 1, &clipped_rect, color->and, color->xor );
1419 release_wine_region( region );
1422 /**********************************************************************
1423 * solid_brush
1425 * Fill a number of rectangles with the solid brush
1427 static BOOL solid_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1429 rop_mask brush_color;
1430 DWORD color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
1432 calc_rop_masks( pdev->brush_rop, color, &brush_color );
1433 solid_rects( dib, num, rects, &brush_color, region );
1434 return TRUE;
1437 static void free_pattern_brush_bits( dibdrv_physdev *pdev )
1439 HeapFree(GetProcessHeap(), 0, pdev->brush_and_bits);
1440 HeapFree(GetProcessHeap(), 0, pdev->brush_xor_bits);
1441 pdev->brush_and_bits = NULL;
1442 pdev->brush_xor_bits = NULL;
1445 void free_pattern_brush( dibdrv_physdev *pdev )
1447 free_pattern_brush_bits( pdev );
1448 free_dib_info( &pdev->brush_dib );
1451 static BOOL create_pattern_brush_bits(dibdrv_physdev *pdev)
1453 DWORD size = pdev->brush_dib.height * abs(pdev->brush_dib.stride);
1454 DWORD *brush_bits = pdev->brush_dib.bits.ptr;
1455 DWORD *and_bits, *xor_bits;
1457 assert(pdev->brush_and_bits == NULL);
1458 assert(pdev->brush_xor_bits == NULL);
1460 assert(pdev->brush_dib.stride > 0);
1462 and_bits = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1463 xor_bits = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1465 if(!and_bits || !xor_bits)
1467 ERR("Failed to create pattern brush bits\n");
1468 free_pattern_brush_bits( pdev );
1469 return FALSE;
1472 while(size)
1474 calc_and_xor_masks(pdev->brush_rop, *brush_bits++, and_bits++, xor_bits++);
1475 size -= 4;
1478 return TRUE;
1481 static const DWORD hatches[6][8] =
1483 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1484 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
1485 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL */
1486 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL */
1487 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
1488 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
1491 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev)
1493 dib_info hatch;
1494 rop_mask fg_mask, bg_mask;
1495 rop_mask_bits mask_bits;
1496 DWORD size;
1497 BOOL ret;
1499 assert(pdev->brush_and_bits == NULL);
1500 assert(pdev->brush_xor_bits == NULL);
1502 /* Just initialise brush_dib with the color / sizing info. We don't
1503 need the bits as we'll calculate the rop masks straight from
1504 the hatch patterns. */
1506 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1507 pdev->brush_dib.width = 8;
1508 pdev->brush_dib.height = 8;
1509 pdev->brush_dib.stride = get_dib_stride( pdev->brush_dib.width, pdev->brush_dib.bit_count );
1511 size = pdev->brush_dib.height * pdev->brush_dib.stride;
1513 mask_bits.and = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1514 mask_bits.xor = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1516 if(!mask_bits.and || !mask_bits.xor)
1518 ERR("Failed to create pattern brush bits\n");
1519 free_pattern_brush_bits( pdev );
1520 return FALSE;
1523 hatch.bit_count = 1;
1524 hatch.height = hatch.width = 8;
1525 hatch.stride = 4;
1526 hatch.bits.ptr = (void *) hatches[pdev->brush_hatch];
1527 hatch.bits.free = hatch.bits.param = NULL;
1528 hatch.bits.is_copy = FALSE;
1530 get_color_masks( pdev, pdev->brush_rop, pdev->brush_colorref, &fg_mask, &bg_mask );
1532 ret = pdev->brush_dib.funcs->create_rop_masks( &pdev->brush_dib, &hatch, &fg_mask, &bg_mask, &mask_bits );
1533 if(!ret) free_pattern_brush_bits( pdev );
1535 return ret;
1538 static BOOL matching_pattern_format( dib_info *dib, dib_info *pattern )
1540 if (dib->bit_count != pattern->bit_count) return FALSE;
1541 if (dib->stride != pattern->stride) return FALSE;
1543 switch (dib->bit_count)
1545 case 1:
1546 case 4:
1547 case 8:
1548 if (dib->color_table_size != pattern->color_table_size) return FALSE;
1549 return !memcmp( dib->color_table, pattern->color_table, dib->color_table_size * sizeof(RGBQUAD) );
1550 case 16:
1551 case 32:
1552 return (dib->red_mask == pattern->red_mask &&
1553 dib->green_mask == pattern->green_mask &&
1554 dib->blue_mask == pattern->blue_mask);
1556 return TRUE;
1559 static BOOL select_pattern_brush( dibdrv_physdev *pdev, BOOL *needs_reselect )
1561 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1562 BITMAPINFO *info = (BITMAPINFO *)buffer;
1563 RGBQUAD color_table[2];
1564 RECT rect;
1565 dib_info pattern;
1567 if (!pdev->brush_pattern_info)
1569 BITMAPOBJ *bmp = GDI_GetObjPtr( pdev->brush_pattern_bitmap, OBJ_BITMAP );
1570 BOOL ret;
1572 if (!bmp) return FALSE;
1573 ret = init_dib_info_from_bitmapobj( &pattern, bmp, 0 );
1574 GDI_ReleaseObj( pdev->brush_pattern_bitmap );
1575 if (!ret) return FALSE;
1577 else if (pdev->brush_pattern_info->bmiHeader.biClrUsed && pdev->brush_pattern_usage == DIB_PAL_COLORS)
1579 copy_bitmapinfo( info, pdev->brush_pattern_info );
1580 fill_color_table_from_pal_colors( info, pdev->dev.hdc );
1581 init_dib_info_from_bitmapinfo( &pattern, info, pdev->brush_pattern_bits, 0 );
1582 *needs_reselect = TRUE;
1584 else
1586 init_dib_info_from_bitmapinfo( &pattern, pdev->brush_pattern_info, pdev->brush_pattern_bits, 0 );
1589 if (pattern.bit_count == 1 && !pattern.color_table)
1591 /* monochrome DDB pattern uses DC colors */
1592 DWORD pixel;
1593 BOOL got_pixel;
1594 COLORREF color;
1596 color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetTextColor( pdev->dev.hdc ),
1597 &got_pixel, &pixel );
1598 color_table[0].rgbRed = GetRValue( color );
1599 color_table[0].rgbGreen = GetGValue( color );
1600 color_table[0].rgbBlue = GetBValue( color );
1601 color_table[0].rgbReserved = 0;
1603 color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetBkColor( pdev->dev.hdc ),
1604 &got_pixel, &pixel );
1605 color_table[1].rgbRed = GetRValue( color );
1606 color_table[1].rgbGreen = GetGValue( color );
1607 color_table[1].rgbBlue = GetBValue( color );
1608 color_table[1].rgbReserved = 0;
1610 pattern.color_table = color_table;
1611 pattern.color_table_size = 2;
1612 *needs_reselect = TRUE;
1615 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1617 pdev->brush_dib.height = pattern.height;
1618 pdev->brush_dib.width = pattern.width;
1619 pdev->brush_dib.stride = get_dib_stride( pdev->brush_dib.width, pdev->brush_dib.bit_count );
1621 if (matching_pattern_format( &pdev->brush_dib, &pattern ))
1623 pdev->brush_dib.bits.ptr = pattern.bits.ptr;
1624 pdev->brush_dib.bits.is_copy = FALSE;
1625 pdev->brush_dib.bits.free = NULL;
1627 else
1629 pdev->brush_dib.bits.ptr = HeapAlloc( GetProcessHeap(), 0,
1630 pdev->brush_dib.height * pdev->brush_dib.stride );
1631 pdev->brush_dib.bits.is_copy = TRUE;
1632 pdev->brush_dib.bits.free = free_heap_bits;
1634 rect.left = rect.top = 0;
1635 rect.right = pattern.width;
1636 rect.bottom = pattern.height;
1638 pdev->brush_dib.funcs->convert_to(&pdev->brush_dib, &pattern, &rect);
1640 return TRUE;
1643 /**********************************************************************
1644 * pattern_brush
1646 * Fill a number of rectangles with the pattern brush
1647 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1649 static BOOL pattern_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1651 int i, j;
1652 const WINEREGION *clip;
1653 POINT origin;
1654 BOOL needs_reselect = FALSE;
1656 if(pdev->brush_and_bits == NULL)
1658 switch(pdev->brush_style)
1660 case BS_DIBPATTERN:
1661 if (!pdev->brush_dib.bits.ptr && !select_pattern_brush( pdev, &needs_reselect ))
1662 return FALSE;
1663 if(!create_pattern_brush_bits(pdev))
1664 return FALSE;
1665 break;
1667 case BS_HATCHED:
1668 if(!create_hatch_brush_bits(pdev))
1669 return FALSE;
1670 break;
1672 default:
1673 ERR("Unexpected brush style %d\n", pdev->brush_style);
1674 return FALSE;
1678 GetBrushOrgEx(pdev->dev.hdc, &origin);
1680 clip = get_wine_region( region );
1682 if (!clip)
1684 dib->funcs->pattern_rects( dib, num, rects, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1685 goto done;
1688 for(i = 0; i < num; i++)
1690 for(j = 0; j < clip->numRects; j++)
1692 RECT rect = rects[i];
1694 /* Optimize unclipped case */
1695 if(clip->rects[j].top <= rect.top && clip->rects[j].bottom >= rect.bottom &&
1696 clip->rects[j].left <= rect.left && clip->rects[j].right >= rect.right)
1698 dib->funcs->pattern_rects( dib, 1, &rect, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1699 break;
1702 if(clip->rects[j].top >= rect.bottom) break;
1703 if(clip->rects[j].bottom <= rect.top) continue;
1705 if(clip->rects[j].right > rect.left && clip->rects[j].left < rect.right)
1707 rect.left = max(rect.left, clip->rects[j].left);
1708 rect.top = max(rect.top, clip->rects[j].top);
1709 rect.right = min(rect.right, clip->rects[j].right);
1710 rect.bottom = min(rect.bottom, clip->rects[j].bottom);
1712 dib->funcs->pattern_rects( dib, 1, &rect, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1716 release_wine_region( region );
1718 done:
1719 if (needs_reselect) free_pattern_brush( pdev );
1720 return TRUE;
1723 static BOOL null_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1725 return TRUE;
1728 void update_brush_rop( dibdrv_physdev *pdev, INT rop )
1730 pdev->brush_rop = rop;
1731 free_pattern_brush_bits( pdev );
1734 /***********************************************************************
1735 * dibdrv_SelectBrush
1737 HBRUSH dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush, HBITMAP bitmap,
1738 const BITMAPINFO *info, void *bits, UINT usage )
1740 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBrush );
1741 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1742 LOGBRUSH logbrush;
1744 TRACE("(%p, %p)\n", dev, hbrush);
1746 free_pattern_brush( pdev );
1748 if (bitmap || info) /* pattern brush */
1750 pdev->brush_rects = pattern_brush;
1751 pdev->brush_style = BS_DIBPATTERN;
1752 pdev->brush_pattern_info = info;
1753 pdev->brush_pattern_bits = bits;
1754 pdev->brush_pattern_usage = usage;
1755 pdev->brush_pattern_bitmap = bitmap;
1756 /* brush is actually selected only when it's used */
1758 return next->funcs->pSelectBrush( next, hbrush, bitmap, info, bits, usage );
1761 GetObjectW( hbrush, sizeof(logbrush), &logbrush );
1763 if (hbrush == GetStockObject( DC_BRUSH ))
1764 logbrush.lbColor = GetDCBrushColor( dev->hdc );
1766 pdev->brush_style = logbrush.lbStyle;
1768 switch(logbrush.lbStyle)
1770 case BS_SOLID:
1771 pdev->brush_colorref = logbrush.lbColor;
1772 pdev->brush_rop = GetROP2( dev->hdc );
1773 pdev->brush_rects = solid_brush;
1774 break;
1776 case BS_NULL:
1777 pdev->brush_rects = null_brush;
1778 break;
1780 case BS_HATCHED:
1781 if(logbrush.lbHatch > HS_DIAGCROSS) return 0;
1782 pdev->brush_hatch = logbrush.lbHatch;
1783 pdev->brush_colorref = logbrush.lbColor;
1784 pdev->brush_rop = GetROP2( dev->hdc );
1785 pdev->brush_rects = pattern_brush;
1786 break;
1788 default:
1789 return 0;
1792 return next->funcs->pSelectBrush( next, hbrush, bitmap, info, bits, usage );
1795 /***********************************************************************
1796 * dibdrv_SetDCBrushColor
1798 COLORREF dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
1800 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCBrushColor );
1801 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1803 if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
1804 pdev->brush_colorref = color;
1806 return next->funcs->pSetDCBrushColor( next, color );
1809 BOOL brush_rects(dibdrv_physdev *pdev, int num, const RECT *rects)
1811 return pdev->brush_rects( pdev, &pdev->dib, num, rects, pdev->clip );