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
24 #include "gdi_private.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dib);
33 * Decompose the 16 ROP2s into an expression of the form
37 * Where A and X depend only on P (and so can be precomputed).
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
48 * R2_NOTMASKPEN ~(P & D) P 1
49 * R2_MASKPEN P & D P 0
50 * R2_NOTXORPEN ~(P ^ D) 1 ~P
52 * R2_MERGENOTPEN ~P | D P ~P
54 * R2_MERGEPENNOT P | ~D ~P 1
55 * R2_MERGEPEN P | D ~P P
60 /* A = (P & A1) | (~P & A2) */
62 #define ONE {0xffffffff, 0xffffffff}
63 #define P {0xffffffff, 0}
64 #define NOT_P {0, 0xffffffff}
66 static const DWORD rop2_and_array[16][2] =
68 ZERO, NOT_P, NOT_P, ZERO,
71 ZERO, NOT_P, NOT_P, ZERO
74 /* X = (P & X1) | (~P & X2) */
75 static const DWORD rop2_xor_array[16][2] =
77 ZERO, NOT_P, ZERO, NOT_P,
79 ZERO, NOT_P, ZERO, NOT_P,
88 void calc_and_xor_masks(INT rop, DWORD color, DWORD *and, DWORD *xor)
90 /* NB The ROP2 codes start at one and the arrays are zero-based */
91 *and = (color & rop2_and_array[rop-1][0]) | ((~color) & rop2_and_array[rop-1][1]);
92 *xor = (color & rop2_xor_array[rop-1][0]) | ((~color) & rop2_xor_array[rop-1][1]);
95 static inline RGBQUAD rgbquad_from_colorref(COLORREF c)
99 ret.rgbRed = GetRValue(c);
100 ret.rgbGreen = GetGValue(c);
101 ret.rgbBlue = GetBValue(c);
106 static inline BOOL rgbquad_equal(const RGBQUAD *a, const RGBQUAD *b)
108 if(a->rgbRed == b->rgbRed &&
109 a->rgbGreen == b->rgbGreen &&
110 a->rgbBlue == b->rgbBlue)
115 /******************************************************************
118 * 1 bit bitmaps map the fg/bg colors as follows:
119 * If the fg colorref exactly matches one of the color table entries then
120 * that entry is the fg color and the other is the bg.
121 * Otherwise the bg color is mapped to the closest entry in the table and
122 * the fg takes the other one.
124 DWORD get_fg_color( dibdrv_physdev *pdev, COLORREF fg )
128 if(pdev->dib.bit_count != 1)
129 return pdev->dib.funcs->colorref_to_pixel( &pdev->dib, fg );
131 fg_quad = rgbquad_from_colorref( fg );
132 if(rgbquad_equal(&fg_quad, pdev->dib.color_table))
134 if(rgbquad_equal(&fg_quad, pdev->dib.color_table + 1))
137 if(fg == GetBkColor(pdev->dev.hdc)) return pdev->bkgnd_color;
138 else return pdev->bkgnd_color ? 0 : 1;
141 /***************************************************************************
142 * get_pen_bkgnd_masks
144 * Returns the pre-calculated bkgnd color masks unless the dib is 1 bpp.
145 * In this case since there are several fg sources (pen, brush, text)
146 * this makes pdev->bkgnd_color unusable. So here we take the inverse
147 * of the relevant fg color (which is always set up correctly).
149 static inline void get_pen_bkgnd_masks(const dibdrv_physdev *pdev, DWORD *and, DWORD *xor)
151 if(pdev->dib.bit_count != 1 || GetBkMode(pdev->dev.hdc) == TRANSPARENT)
153 *and = pdev->bkgnd_and;
154 *xor = pdev->bkgnd_xor;
158 DWORD color = ~pdev->pen_color;
159 if(pdev->pen_colorref == GetBkColor(pdev->dev.hdc)) color = pdev->pen_color;
160 calc_and_xor_masks( GetROP2(pdev->dev.hdc), color, and, xor );
164 static inline void get_brush_bkgnd_masks(const dibdrv_physdev *pdev, DWORD *and, DWORD *xor)
166 if(GetBkMode(pdev->dev.hdc) == TRANSPARENT)
168 *and = pdev->bkgnd_and;
169 *xor = pdev->bkgnd_xor;
173 DWORD color = pdev->bkgnd_color;
175 if(pdev->dib.bit_count == 1)
177 if(pdev->brush_colorref == GetBkColor(pdev->dev.hdc))
178 color = pdev->brush_color;
180 color = ~pdev->brush_color;
182 calc_and_xor_masks( pdev->brush_rop, color, and, xor );
186 static inline void order_end_points(int *s, int *e)
197 static inline BOOL pt_in_rect( const RECT *rect, const POINT *pt )
199 return ((pt->x >= rect->left) && (pt->x < rect->right) &&
200 (pt->y >= rect->top) && (pt->y < rect->bottom));
203 #define Y_INCREASING_MASK 0x0f
204 #define X_INCREASING_MASK 0xc3
205 #define X_MAJOR_MASK 0x99
206 #define POS_SLOPE_MASK 0x33
208 static inline BOOL is_xmajor(DWORD octant)
210 return octant & X_MAJOR_MASK;
213 static inline BOOL is_pos_slope(DWORD octant)
215 return octant & POS_SLOPE_MASK;
218 static inline BOOL is_x_increasing(DWORD octant)
220 return octant & X_INCREASING_MASK;
223 static inline BOOL is_y_increasing(DWORD octant)
225 return octant & Y_INCREASING_MASK;
228 /**********************************************************************
231 * Return the octant number starting clockwise from the +ve x-axis.
233 static inline int get_octant_number(int dx, int dy)
237 return ( dx > dy) ? 1 : 2;
239 return (-dx > dy) ? 4 : 3;
242 return (-dx > -dy) ? 5 : 6;
244 return ( dx > -dy) ? 8 : 7;
247 static inline DWORD get_octant_mask(int dx, int dy)
249 return 1 << (get_octant_number(dx, dy) - 1);
252 static void solid_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
260 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
269 static inline DWORD calc_outcode(const POINT *pt, const RECT *clip)
272 if(pt->x < clip->left) out |= OUT_LEFT;
273 else if(pt->x >= clip->right) out |= OUT_RIGHT;
274 if(pt->y < clip->top) out |= OUT_TOP;
275 else if(pt->y >= clip->bottom) out |= OUT_BOTTOM;
287 /******************************************************************************
290 * Clips the start and end points to a rectangle.
292 * Note, this treats the end point like the start point. If the
293 * caller doesn't want it displayed, it should exclude it. If the end
294 * point is clipped out, then the likelihood is that the new end point
295 * should be displayed.
297 * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
299 * This derivation is based on the comments in X.org's xserver/mi/mizerclip.c,
300 * however the Bresenham error term is defined differently so the equations
303 * For x major lines we have 2dy >= err + bias > 2dy - 2dx
304 * 0 >= err + bias - 2dy > -2dx
306 * Note dx, dy, m and n are all +ve.
308 * Moving the start pt from x1 to x1 + m, we need to figure out y1 + n.
309 * err = 2dy - dx + 2mdy - 2ndx
310 * 0 >= 2dy - dx + 2mdy - 2ndx + bias - 2dy > -2dx
311 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
312 * which of course will give exactly one solution for n,
313 * so looking at the >= inequality
314 * n >= (2mdy + bias - dx) / 2dx
315 * n = ceiling((2mdy + bias - dx) / 2dx)
316 * = (2mdy + bias + dx - 1) / 2dx (assuming division truncation)
318 * Moving start pt from y1 to y1 + n we need to figure out x1 + m - there may be several
319 * solutions we pick the one that minimizes m (ie that first unlipped pt). As above:
320 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
321 * 2mdy > 2ndx - bias - dx
322 * m > (2ndx - bias - dx) / 2dy
323 * m = floor((2ndx - bias - dx) / 2dy) + 1
324 * m = (2ndx - bias - dx) / 2dy + 1
326 * Moving end pt from x2 to x2 - m, we need to figure out y2 - n
327 * err = 2dy - dx + 2(dx - m)dy - 2(dy - n)dx
328 * = 2dy - dx - 2mdy + 2ndx
329 * 0 >= 2dy - dx - 2mdy + 2ndx + bias - 2dy > -2dx
330 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
331 * again exactly one solution.
332 * 2ndx <= 2mdy - bias + dx
333 * n = floor((2mdy - bias + dx) / 2dx)
334 * = (2mdy - bias + dx) / 2dx
336 * Moving end pt from y2 to y2 - n when need x2 - m this time maximizing x2 - m so
337 * mininizing m to include all of the points at y = y2 - n. As above:
338 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
339 * 2mdy >= 2ndx + bias - dx
340 * m = ceiling((2ndx + bias - dx) / 2dy)
341 * = (2ndx + bias - dx - 1) / 2dy + 1
343 * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
345 * Moving start point from y1 to y1 + n find x1 + m
346 * m = (2ndx + bias + dy - 1) / 2dy
348 * Moving start point from x1 to x1 + m find y1 + n
349 * n = (2mdy - bias - dy) / 2ndx + 1
351 * Moving end point from y2 to y2 - n find x1 - m
352 * m = (2ndx - bias + dy) / 2dy
354 * Moving end point from x2 to x2 - m find y2 - n
355 * n = (2mdy + bias - dy - 1) / 2dx + 1
357 static int clip_line(const POINT *start, const POINT *end, const RECT *clip,
358 const bres_params *params, POINT *pt1, POINT *pt2)
361 BOOL clipped = FALSE;
362 DWORD start_oc, end_oc;
363 const int bias = params->bias;
364 const unsigned int dx = params->dx;
365 const unsigned int dy = params->dy;
366 const unsigned int two_dx = params->dx * 2;
367 const unsigned int two_dy = params->dy * 2;
368 const BOOL xmajor = is_xmajor(params->octant);
369 const BOOL neg_slope = !is_pos_slope(params->octant);
374 start_oc = calc_outcode(start, clip);
375 end_oc = calc_outcode(end, clip);
379 if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
380 if(start_oc & end_oc) return 0; /* trivial reject */
383 if(start_oc & OUT_LEFT)
385 m = clip->left - start->x;
387 n = (m * two_dy + bias + dx - 1) / two_dx;
389 n = (m * two_dy - bias - dy) / two_dx + 1;
392 if(neg_slope) n = -n;
393 pt1->y = start->y + n;
394 start_oc = calc_outcode(pt1, clip);
396 else if(start_oc & OUT_RIGHT)
398 m = start->x - clip->right + 1;
400 n = (m * two_dy + bias + dx - 1) / two_dx;
402 n = (m * two_dy - bias - dy) / two_dx + 1;
404 pt1->x = clip->right - 1;
405 if(neg_slope) n = -n;
406 pt1->y = start->y - n;
407 start_oc = calc_outcode(pt1, clip);
409 else if(start_oc & OUT_TOP)
411 n = clip->top - start->y;
413 m = (n * two_dx - bias - dx) / two_dy + 1;
415 m = (n * two_dx + bias + dy - 1) / two_dy;
418 if(neg_slope) m = -m;
419 pt1->x = start->x + m;
420 start_oc = calc_outcode(pt1, clip);
422 else if(start_oc & OUT_BOTTOM)
424 n = start->y - clip->bottom + 1;
426 m = (n * two_dx - bias - dx) / two_dy + 1;
428 m = (n * two_dx + bias + dy - 1) / two_dy;
430 pt1->y = clip->bottom - 1;
431 if(neg_slope) m = -m;
432 pt1->x = start->x - m;
433 start_oc = calc_outcode(pt1, clip);
435 else if(end_oc & OUT_LEFT)
437 m = clip->left - end->x;
439 n = (m * two_dy - bias + dx) / two_dx;
441 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
444 if(neg_slope) n = -n;
446 end_oc = calc_outcode(pt2, clip);
448 else if(end_oc & OUT_RIGHT)
450 m = end->x - clip->right + 1;
452 n = (m * two_dy - bias + dx) / two_dx;
454 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
456 pt2->x = clip->right - 1;
457 if(neg_slope) n = -n;
459 end_oc = calc_outcode(pt2, clip);
461 else if(end_oc & OUT_TOP)
463 n = clip->top - end->y;
465 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
467 m = (n * two_dx - bias + dy) / two_dy;
470 if(neg_slope) m = -m;
472 end_oc = calc_outcode(pt2, clip);
474 else if(end_oc & OUT_BOTTOM)
476 n = end->y - clip->bottom + 1;
478 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
480 m = (n * two_dx - bias + dy) / two_dy;
482 pt2->y = clip->bottom - 1;
483 if(neg_slope) m = -m;
485 end_oc = calc_outcode(pt2, clip);
490 static void bres_line_with_bias(INT x1, INT y1, INT x2, INT y2, const bres_params *params, INT err,
491 BOOL last_pt, void (* callback)(dibdrv_physdev*,INT,INT), dibdrv_physdev *pdev)
493 const int xadd = is_x_increasing(params->octant) ? 1 : -1;
494 const int yadd = is_y_increasing(params->octant) ? 1 : -1;
497 if (is_xmajor(params->octant)) /* line is "more horizontal" */
499 erradd = 2*params->dy - 2*params->dx;
502 callback(pdev, x1, y1);
503 if (err + params->bias > 0)
508 else err += 2*params->dy;
511 if(last_pt) callback(pdev, x1, y1);
513 else /* line is "more vertical" */
515 erradd = 2*params->dx - 2*params->dy;
518 callback(pdev, x1, y1);
519 if (err + params->bias > 0)
524 else err += 2*params->dx;
527 if(last_pt) callback(pdev, x1, y1);
531 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
533 const WINEREGION *clip = get_wine_region(pdev->clip);
535 if(start->y == end->y)
540 rect.left = start->x;
543 rect.bottom = end->y + 1;
544 order_end_points(&rect.left, &rect.right);
545 for(i = 0; i < clip->numRects; i++)
547 if(clip->rects[i].top >= rect.bottom) break;
548 if(clip->rects[i].bottom <= rect.top) continue;
549 /* Optimize the unclipped case */
550 if(clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
552 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
555 if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
558 tmp.left = max(rect.left, clip->rects[i].left);
559 tmp.right = min(rect.right, clip->rects[i].right);
560 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
564 else if(start->x == end->x)
569 rect.left = start->x;
571 rect.right = end->x + 1;
572 rect.bottom = end->y;
573 order_end_points(&rect.top, &rect.bottom);
574 for(i = 0; i < clip->numRects; i++)
576 /* Optimize unclipped case */
577 if(clip->rects[i].top <= rect.top && clip->rects[i].bottom >= rect.bottom &&
578 clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
580 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
583 if(clip->rects[i].top >= rect.bottom) break;
584 if(clip->rects[i].bottom <= rect.top) continue;
585 if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
588 tmp.top = max(rect.top, clip->rects[i].top);
589 tmp.bottom = min(rect.bottom, clip->rects[i].bottom);
590 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
597 INT dx = end->x - start->x;
598 INT dy = end->y - start->y;
603 params.octant = get_octant_mask(dx, dy);
604 /* Octants 3, 5, 6 and 8 take a bias */
605 params.bias = (params.octant & 0xb4) ? 1 : 0;
607 for(i = 0; i < clip->numRects; i++)
609 POINT clipped_start, clipped_end;
611 clip_status = clip_line(start, end, clip->rects + i, ¶ms, &clipped_start, &clipped_end);
615 int m = abs(clipped_start.x - start->x);
616 int n = abs(clipped_start.y - start->y);
618 BOOL last_pt = FALSE;
620 if(is_xmajor(params.octant))
621 err = 2 * params.dy - params.dx + m * 2 * params.dy - n * 2 * params.dx;
623 err = 2 * params.dx - params.dy + n * 2 * params.dx - m * 2 * params.dy;
625 if(clip_status == 1 && (end->x != clipped_end.x || end->y != clipped_end.y)) last_pt = TRUE;
627 bres_line_with_bias(clipped_start.x, clipped_start.y, clipped_end.x, clipped_end.y, ¶ms,
628 err, last_pt, solid_pen_line_callback, pdev);
630 if(clip_status == 2) break; /* completely unclipped, so we can finish */
635 release_wine_region(pdev->clip);
639 void reset_dash_origin(dibdrv_physdev *pdev)
641 pdev->dash_pos.cur_dash = 0;
642 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
643 pdev->dash_pos.mark = TRUE;
646 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
648 skip %= pdev->pen_pattern.total_len;
651 if(pdev->dash_pos.left_in_dash > skip)
653 pdev->dash_pos.left_in_dash -= skip;
656 skip -= pdev->dash_pos.left_in_dash;
657 pdev->dash_pos.cur_dash++;
658 if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
659 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
660 pdev->dash_pos.mark = !pdev->dash_pos.mark;
664 static inline void get_dash_colors(const dibdrv_physdev *pdev, DWORD *and, DWORD *xor)
666 if(pdev->dash_pos.mark)
668 *and = pdev->pen_and;
669 *xor = pdev->pen_xor;
673 get_pen_bkgnd_masks( pdev, and, xor );
677 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
682 get_dash_colors(pdev, &and, &xor);
688 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
692 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
694 const WINEREGION *clip = get_wine_region(pdev->clip);
698 const dash_pos start_pos = pdev->dash_pos;
700 if(start->y == end->y) /* hline */
703 INT left, right, cur_x;
706 rect.bottom = start->y + 1;
708 if(start->x <= end->x)
721 for(i = 0; i < clip->numRects; i++)
723 if(clip->rects[i].top > start->y) break;
724 if(clip->rects[i].bottom <= start->y) continue;
726 if(clip->rects[i].right > left && clip->rects[i].left <= right)
728 int clipped_left = max(clip->rects[i].left, left);
729 int clipped_right = min(clip->rects[i].right - 1, right);
731 pdev->dash_pos = start_pos;
735 cur_x = clipped_left;
737 skip_dash(pdev, clipped_left - left);
739 while(cur_x <= clipped_right)
741 get_dash_colors(pdev, &and, &xor);
742 dash_len = pdev->dash_pos.left_in_dash;
743 if(cur_x + dash_len > clipped_right + 1)
744 dash_len = clipped_right - cur_x + 1;
746 rect.right = cur_x + dash_len;
748 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
750 skip_dash(pdev, dash_len);
755 cur_x = clipped_right;
757 skip_dash(pdev, right - clipped_right);
759 while(cur_x >= clipped_left)
761 get_dash_colors(pdev, &and, &xor);
762 dash_len = pdev->dash_pos.left_in_dash;
763 if(cur_x - dash_len < clipped_left - 1)
764 dash_len = cur_x - clipped_left + 1;
765 rect.left = cur_x - dash_len + 1;
766 rect.right = cur_x + 1;
768 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
770 skip_dash(pdev, dash_len);
775 pdev->dash_pos = start_pos;
776 skip_dash(pdev, right - left + 1);
778 else if(start->x == end->x) /* vline */
781 INT top, bottom, cur_y;
783 rect.left = start->x;
784 rect.right = start->x + 1;
786 if(start->y <= end->y)
799 for(i = 0; i < clip->numRects; i++)
801 if(clip->rects[i].top > bottom) break;
802 if(clip->rects[i].bottom <= top) continue;
803 if(clip->rects[i].right > start->x && clip->rects[i].left <= start->x)
805 int clipped_top = max(clip->rects[i].top, top);
806 int clipped_bottom = min(clip->rects[i].bottom - 1, bottom);
808 pdev->dash_pos = start_pos;
814 skip_dash(pdev, clipped_top - top);
816 while(cur_y <= clipped_bottom)
818 get_dash_colors(pdev, &and, &xor);
819 dash_len = pdev->dash_pos.left_in_dash;
820 if(cur_y + dash_len > clipped_bottom + 1)
821 dash_len = clipped_bottom - cur_y + 1;
823 rect.bottom = cur_y + dash_len;
825 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
827 skip_dash(pdev, dash_len);
832 cur_y = clipped_bottom;
834 skip_dash(pdev, bottom - clipped_bottom);
836 while(cur_y >= clipped_top)
838 get_dash_colors(pdev, &and, &xor);
839 dash_len = pdev->dash_pos.left_in_dash;
840 if(cur_y - dash_len < clipped_top - 1)
841 dash_len = cur_y - clipped_top + 1;
842 rect.top = cur_y - dash_len + 1;
843 rect.bottom = cur_y + 1;
845 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
847 skip_dash(pdev, dash_len);
852 pdev->dash_pos = start_pos;
853 skip_dash(pdev, bottom - top + 1);
858 INT dx = end->x - start->x;
859 INT dy = end->y - start->y;
864 params.octant = get_octant_mask(dx, dy);
865 /* Octants 3, 5, 6 and 8 take a bias */
866 params.bias = (params.octant & 0xb4) ? 1 : 0;
868 for(i = 0; i < clip->numRects; i++)
870 POINT clipped_start, clipped_end;
872 clip_status = clip_line(start, end, clip->rects + i, ¶ms, &clipped_start, &clipped_end);
876 int m = abs(clipped_start.x - start->x);
877 int n = abs(clipped_start.y - start->y);
879 BOOL last_pt = FALSE;
881 pdev->dash_pos = start_pos;
883 if(is_xmajor(params.octant))
885 err = 2 * params.dy - params.dx + m * 2 * params.dy - n * 2 * params.dx;
890 err = 2 * params.dx - params.dy + n * 2 * params.dx - m * 2 * params.dy;
893 if(clip_status == 1 && (end->x != clipped_end.x || end->y != clipped_end.y)) last_pt = TRUE;
895 bres_line_with_bias(clipped_start.x, clipped_start.y, clipped_end.x, clipped_end.y, ¶ms,
896 err, last_pt, dashed_pen_line_callback, pdev);
898 if(clip_status == 2) break; /* completely unclipped, so we can finish */
901 pdev->dash_pos = start_pos;
902 if(is_xmajor(params.octant))
903 skip_dash(pdev, params.dx);
905 skip_dash(pdev, params.dy);
908 release_wine_region(pdev->clip);
912 static BOOL null_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
917 static const dash_pattern dash_patterns[5] =
919 {0, {0}, 0}, /* PS_SOLID - a pseudo-pattern used to initialise unpatterned pens. */
920 {2, {18, 6}, 24}, /* PS_DASH */
921 {2, {3, 3}, 6}, /* PS_DOT */
922 {4, {9, 6, 3, 6}, 24}, /* PS_DASHDOT */
923 {6, {9, 3, 3, 3, 3, 3}, 24} /* PS_DASHDOTDOT */
926 /***********************************************************************
929 HPEN CDECL dibdrv_SelectPen( PHYSDEV dev, HPEN hpen )
931 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectPen );
932 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
936 TRACE("(%p, %p)\n", dev, hpen);
938 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
940 /* must be an extended pen */
942 INT size = GetObjectW( hpen, 0, NULL );
946 elp = HeapAlloc( GetProcessHeap(), 0, size );
948 GetObjectW( hpen, size, elp );
949 /* FIXME: add support for user style pens */
950 logpen.lopnStyle = elp->elpPenStyle;
951 logpen.lopnWidth.x = elp->elpWidth;
952 logpen.lopnWidth.y = 0;
953 logpen.lopnColor = elp->elpColor;
955 HeapFree( GetProcessHeap(), 0, elp );
958 if (hpen == GetStockObject( DC_PEN ))
959 logpen.lopnColor = GetDCPenColor( dev->hdc );
961 pdev->pen_colorref = logpen.lopnColor;
962 pdev->pen_color = get_fg_color( pdev, pdev->pen_colorref );
963 calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
965 pdev->pen_pattern = dash_patterns[PS_SOLID];
967 pdev->defer |= DEFER_PEN;
969 style = logpen.lopnStyle & PS_STYLE_MASK;
974 if(logpen.lopnStyle & PS_GEOMETRIC) break;
975 if(logpen.lopnWidth.x > 1) break;
976 pdev->pen_line = solid_pen_line;
977 pdev->defer &= ~DEFER_PEN;
984 if(logpen.lopnStyle & PS_GEOMETRIC) break;
985 if(logpen.lopnWidth.x > 1) break;
986 pdev->pen_line = dashed_pen_line;
987 pdev->pen_pattern = dash_patterns[style];
988 pdev->defer &= ~DEFER_PEN;
992 pdev->pen_line = null_pen_line;
993 pdev->defer &= ~DEFER_PEN;
1000 return next->funcs->pSelectPen( next, hpen );
1003 /***********************************************************************
1004 * dibdrv_SetDCPenColor
1006 COLORREF CDECL dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1008 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCPenColor );
1009 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1011 if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
1013 pdev->pen_colorref = color;
1014 pdev->pen_color = get_fg_color( pdev, pdev->pen_colorref );
1015 calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
1018 return next->funcs->pSetDCPenColor( next, color );
1021 /**********************************************************************
1024 * Fill a number of rectangles with the solid brush
1025 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1027 static BOOL solid_brush(dibdrv_physdev *pdev, int num, RECT *rects)
1030 const WINEREGION *clip = get_wine_region(pdev->clip);
1032 for(i = 0; i < num; i++)
1034 for(j = 0; j < clip->numRects; j++)
1036 RECT rect = rects[i];
1038 /* Optimize unclipped case */
1039 if(clip->rects[j].top <= rect.top && clip->rects[j].bottom >= rect.bottom &&
1040 clip->rects[j].left <= rect.left && clip->rects[j].right >= rect.right)
1042 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->brush_and, pdev->brush_xor);
1046 if(clip->rects[j].top >= rect.bottom) break;
1047 if(clip->rects[j].bottom <= rect.top) continue;
1049 if(clip->rects[j].right > rect.left && clip->rects[j].left < rect.right)
1051 rect.left = max(rect.left, clip->rects[j].left);
1052 rect.top = max(rect.top, clip->rects[j].top);
1053 rect.right = min(rect.right, clip->rects[j].right);
1054 rect.bottom = min(rect.bottom, clip->rects[j].bottom);
1056 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->brush_and, pdev->brush_xor);
1060 release_wine_region(pdev->clip);
1064 static void free_pattern_brush_bits( dibdrv_physdev *pdev )
1066 HeapFree(GetProcessHeap(), 0, pdev->brush_and_bits);
1067 HeapFree(GetProcessHeap(), 0, pdev->brush_xor_bits);
1068 pdev->brush_and_bits = NULL;
1069 pdev->brush_xor_bits = NULL;
1072 void free_pattern_brush( dibdrv_physdev *pdev )
1074 free_pattern_brush_bits( pdev );
1075 free_dib_info( &pdev->brush_dib, TRUE );
1078 static BOOL create_pattern_brush_bits(dibdrv_physdev *pdev)
1080 DWORD size = pdev->brush_dib.height * abs(pdev->brush_dib.stride);
1081 DWORD *brush_bits = pdev->brush_dib.bits;
1082 DWORD *and_bits, *xor_bits;
1084 assert(pdev->brush_and_bits == NULL);
1085 assert(pdev->brush_xor_bits == NULL);
1087 assert(pdev->brush_dib.stride > 0);
1089 and_bits = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1090 xor_bits = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1092 if(!and_bits || !xor_bits)
1094 ERR("Failed to create pattern brush bits\n");
1095 free_pattern_brush_bits( pdev );
1101 calc_and_xor_masks(pdev->brush_rop, *brush_bits++, and_bits++, xor_bits++);
1108 static const DWORD hatches[6][8] =
1110 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1111 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
1112 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL */
1113 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL */
1114 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
1115 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
1118 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev)
1121 rop_mask fg_mask, bg_mask;
1122 rop_mask_bits mask_bits;
1126 assert(pdev->brush_and_bits == NULL);
1127 assert(pdev->brush_xor_bits == NULL);
1129 /* Just initialise brush_dib with the color / sizing info. We don't
1130 need the bits as we'll calculate the rop masks straight from
1131 the hatch patterns. */
1133 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1134 pdev->brush_dib.width = 8;
1135 pdev->brush_dib.height = 8;
1136 pdev->brush_dib.stride = ((pdev->brush_dib.width * pdev->brush_dib.bit_count + 31) >> 3) & ~3;
1138 size = pdev->brush_dib.height * pdev->brush_dib.stride;
1140 mask_bits.and = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1141 mask_bits.xor = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1143 if(!mask_bits.and || !mask_bits.xor)
1145 ERR("Failed to create pattern brush bits\n");
1146 free_pattern_brush_bits( pdev );
1150 hatch.bit_count = 1;
1151 hatch.height = hatch.width = 8;
1153 hatch.bits = (void *) hatches[pdev->brush_hatch];
1155 fg_mask.and = pdev->brush_and;
1156 fg_mask.xor = pdev->brush_xor;
1157 get_brush_bkgnd_masks( pdev, &bg_mask.and, &bg_mask.xor );
1159 ret = pdev->brush_dib.funcs->create_rop_masks( &pdev->brush_dib, &hatch, &fg_mask, &bg_mask, &mask_bits );
1160 if(!ret) free_pattern_brush_bits( pdev );
1165 /**********************************************************************
1168 * Fill a number of rectangles with the pattern brush
1169 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1171 static BOOL pattern_brush(dibdrv_physdev *pdev, int num, RECT *rects)
1174 const WINEREGION *clip;
1177 if(pdev->brush_and_bits == NULL)
1179 switch(pdev->brush_style)
1182 if(!create_pattern_brush_bits(pdev))
1187 if(!create_hatch_brush_bits(pdev))
1192 ERR("Unexpected brush style %d\n", pdev->brush_style);
1197 GetBrushOrgEx(pdev->dev.hdc, &origin);
1199 clip = get_wine_region(pdev->clip);
1200 for(i = 0; i < num; i++)
1202 for(j = 0; j < clip->numRects; j++)
1204 RECT rect = rects[i];
1206 /* Optimize unclipped case */
1207 if(clip->rects[j].top <= rect.top && clip->rects[j].bottom >= rect.bottom &&
1208 clip->rects[j].left <= rect.left && clip->rects[j].right >= rect.right)
1210 pdev->dib.funcs->pattern_rects(&pdev->dib, 1, &rect, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits);
1214 if(clip->rects[j].top >= rect.bottom) break;
1215 if(clip->rects[j].bottom <= rect.top) continue;
1217 if(clip->rects[j].right > rect.left && clip->rects[j].left < rect.right)
1219 rect.left = max(rect.left, clip->rects[j].left);
1220 rect.top = max(rect.top, clip->rects[j].top);
1221 rect.right = min(rect.right, clip->rects[j].right);
1222 rect.bottom = min(rect.bottom, clip->rects[j].bottom);
1224 pdev->dib.funcs->pattern_rects(&pdev->dib, 1, &rect, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits);
1228 release_wine_region(pdev->clip);
1232 static BOOL null_brush(dibdrv_physdev *pdev, int num, RECT *rects)
1237 void update_brush_rop( dibdrv_physdev *pdev, INT rop )
1239 pdev->brush_rop = rop;
1240 if(pdev->brush_style == BS_SOLID || pdev->brush_style == BS_HATCHED)
1241 calc_and_xor_masks(rop, pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1242 free_pattern_brush_bits( pdev );
1245 /***********************************************************************
1246 * dibdrv_SelectBrush
1248 HBRUSH CDECL dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush )
1250 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBrush );
1251 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1254 TRACE("(%p, %p)\n", dev, hbrush);
1256 if (!GetObjectW( hbrush, sizeof(logbrush), &logbrush )) return 0;
1258 if (hbrush == GetStockObject( DC_BRUSH ))
1259 logbrush.lbColor = GetDCBrushColor( dev->hdc );
1261 pdev->brush_style = logbrush.lbStyle;
1263 pdev->defer |= DEFER_BRUSH;
1265 free_pattern_brush( pdev );
1267 switch(logbrush.lbStyle)
1270 pdev->brush_colorref = logbrush.lbColor;
1271 pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
1272 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1273 pdev->brush_rects = solid_brush;
1274 pdev->defer &= ~DEFER_BRUSH;
1278 pdev->brush_rects = null_brush;
1279 pdev->defer &= ~DEFER_BRUSH;
1284 BITMAPINFOHEADER *bi = GlobalLock((HGLOBAL)logbrush.lbHatch);
1286 WORD usage = LOWORD(logbrush.lbColor);
1287 HPALETTE pal = (usage == DIB_PAL_COLORS) ? GetCurrentObject(dev->hdc, OBJ_PAL) : NULL;
1289 if(!bi) return NULL;
1290 if(init_dib_info_from_packed(&orig_dib, bi, usage, pal))
1292 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1293 if(convert_dib(&pdev->brush_dib, &orig_dib))
1295 pdev->brush_rects = pattern_brush;
1296 pdev->defer &= ~DEFER_BRUSH;
1298 free_dib_info(&orig_dib, FALSE);
1300 GlobalUnlock((HGLOBAL)logbrush.lbHatch);
1306 if(logbrush.lbHatch > HS_DIAGCROSS) return 0;
1307 pdev->brush_hatch = logbrush.lbHatch;
1308 pdev->brush_colorref = logbrush.lbColor;
1309 pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
1310 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1311 pdev->brush_rects = pattern_brush;
1312 pdev->defer &= ~DEFER_BRUSH;
1320 return next->funcs->pSelectBrush( next, hbrush );
1323 /***********************************************************************
1324 * dibdrv_SetDCBrushColor
1326 COLORREF CDECL dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
1328 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCBrushColor );
1329 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1331 if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
1333 pdev->brush_colorref = color;
1334 pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
1335 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1338 return next->funcs->pSetDCBrushColor( next, color );