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) ^ A2 */
61 #define ZERO { 0u, 0u}
62 #define ONE { 0u, ~0u}
64 #define NOT_P {~0u, ~0u}
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) ^ 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 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 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 RGBQUAD rgbquad_from_colorref(COLORREF c)
110 ret.rgbRed = GetRValue(c);
111 ret.rgbGreen = GetGValue(c);
112 ret.rgbBlue = GetBValue(c);
117 static inline BOOL rgbquad_equal(const RGBQUAD *a, const RGBQUAD *b)
119 if(a->rgbRed == b->rgbRed &&
120 a->rgbGreen == b->rgbGreen &&
121 a->rgbBlue == b->rgbBlue)
126 static COLORREF make_rgb_colorref( dibdrv_physdev *pdev, COLORREF color,
127 BOOL *got_pixel, DWORD *pixel )
129 BYTE type = color >> 24;
130 WORD index = LOWORD( color );
131 HPALETTE pal = GetCurrentObject( pdev->dev.hdc, OBJ_PAL );
132 PALETTEENTRY pal_ent;
141 case 0x10: /* DIBINDEX */
144 color = RGB(0, 0, 0);
146 if (pdev->dib.bit_count <= 8 && index < (1 << pdev->dib.bit_count))
149 if (index < pdev->dib.color_table_size)
150 color = RGB( pdev->dib.color_table[index].rgbRed,
151 pdev->dib.color_table[index].rgbGreen,
152 pdev->dib.color_table[index].rgbBlue );
156 case 2: /* PALETTERGB */
160 case 1: /* PALETTEINDEX */
161 if (!GetPaletteEntries( pal, index, 1, &pal_ent ))
162 GetPaletteEntries( pal, 0, 1, &pal_ent );
163 color = RGB( pal_ent.peRed, pal_ent.peGreen, pal_ent.peBlue );
167 FIXME("Unhandled color type %08x\n", color);
174 /******************************************************************
177 * 1 bit bitmaps map the fg/bg colors as follows:
178 * If the fg colorref exactly matches one of the color table entries then
179 * that entry is the fg color and the other is the bg.
180 * Otherwise the bg color is mapped to the closest entry in the table and
181 * the fg takes the other one.
183 DWORD get_pixel_color( dibdrv_physdev *pdev, COLORREF color, BOOL mono_fixup )
190 rgb_ref = make_rgb_colorref( pdev, color, &got_pixel, &pixel );
191 if (got_pixel) return pixel;
193 if (pdev->dib.bit_count != 1 || !mono_fixup)
194 return pdev->dib.funcs->colorref_to_pixel( &pdev->dib, rgb_ref );
196 fg_quad = rgbquad_from_colorref( rgb_ref );
197 if(rgbquad_equal(&fg_quad, pdev->dib.color_table))
199 if(rgbquad_equal(&fg_quad, pdev->dib.color_table + 1))
202 if(color == GetBkColor(pdev->dev.hdc)) return pdev->bkgnd_color;
203 else return pdev->bkgnd_color ? 0 : 1;
206 /***************************************************************************
207 * get_pen_bkgnd_masks
209 * Returns the pre-calculated bkgnd color masks unless the dib is 1 bpp.
210 * In this case since there are several fg sources (pen, brush, text)
211 * this makes pdev->bkgnd_color unusable. So here we take the inverse
212 * of the relevant fg color (which is always set up correctly).
214 static inline void get_pen_bkgnd_masks(const dibdrv_physdev *pdev, DWORD *and, DWORD *xor)
216 if(pdev->dib.bit_count != 1 || GetBkMode(pdev->dev.hdc) == TRANSPARENT)
218 *and = pdev->bkgnd_and;
219 *xor = pdev->bkgnd_xor;
223 DWORD color = ~pdev->pen_color;
224 if(pdev->pen_colorref == GetBkColor(pdev->dev.hdc)) color = pdev->pen_color;
225 calc_and_xor_masks( GetROP2(pdev->dev.hdc), color, and, xor );
229 static inline void get_brush_bkgnd_masks(const dibdrv_physdev *pdev, DWORD *and, DWORD *xor)
231 if(GetBkMode(pdev->dev.hdc) == TRANSPARENT)
233 *and = pdev->bkgnd_and;
234 *xor = pdev->bkgnd_xor;
238 DWORD color = pdev->bkgnd_color;
240 if(pdev->dib.bit_count == 1)
242 if(pdev->brush_colorref == GetBkColor(pdev->dev.hdc))
243 color = pdev->brush_color;
245 color = ~pdev->brush_color;
247 calc_and_xor_masks( pdev->brush_rop, color, and, xor );
251 static inline void order_end_points(int *s, int *e)
262 #define Y_INCREASING_MASK 0x0f
263 #define X_INCREASING_MASK 0xc3
264 #define X_MAJOR_MASK 0x99
265 #define POS_SLOPE_MASK 0x33
267 static inline BOOL is_xmajor(DWORD octant)
269 return octant & X_MAJOR_MASK;
272 static inline BOOL is_pos_slope(DWORD octant)
274 return octant & POS_SLOPE_MASK;
277 static inline BOOL is_x_increasing(DWORD octant)
279 return octant & X_INCREASING_MASK;
282 static inline BOOL is_y_increasing(DWORD octant)
284 return octant & Y_INCREASING_MASK;
287 /**********************************************************************
290 * Return the octant number starting clockwise from the +ve x-axis.
292 static inline int get_octant_number(int dx, int dy)
296 return ( dx > dy) ? 1 : 2;
298 return (-dx > dy) ? 4 : 3;
301 return (-dx > -dy) ? 5 : 6;
303 return ( dx > -dy) ? 8 : 7;
306 static inline DWORD get_octant_mask(int dx, int dy)
308 return 1 << (get_octant_number(dx, dy) - 1);
311 static void solid_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
319 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
328 static inline DWORD calc_outcode(const POINT *pt, const RECT *clip)
331 if(pt->x < clip->left) out |= OUT_LEFT;
332 else if(pt->x >= clip->right) out |= OUT_RIGHT;
333 if(pt->y < clip->top) out |= OUT_TOP;
334 else if(pt->y >= clip->bottom) out |= OUT_BOTTOM;
339 /******************************************************************************
342 * Clips the start and end points to a rectangle.
344 * Note, this treats the end point like the start point. If the
345 * caller doesn't want it displayed, it should exclude it. If the end
346 * point is clipped out, then the likelihood is that the new end point
347 * should be displayed.
349 * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
351 * This derivation is based on the comments in X.org's xserver/mi/mizerclip.c,
352 * however the Bresenham error term is defined differently so the equations
355 * For x major lines we have 2dy >= err + bias > 2dy - 2dx
356 * 0 >= err + bias - 2dy > -2dx
358 * Note dx, dy, m and n are all +ve.
360 * Moving the start pt from x1 to x1 + m, we need to figure out y1 + n.
361 * err = 2dy - dx + 2mdy - 2ndx
362 * 0 >= 2dy - dx + 2mdy - 2ndx + bias - 2dy > -2dx
363 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
364 * which of course will give exactly one solution for n,
365 * so looking at the >= inequality
366 * n >= (2mdy + bias - dx) / 2dx
367 * n = ceiling((2mdy + bias - dx) / 2dx)
368 * = (2mdy + bias + dx - 1) / 2dx (assuming division truncation)
370 * Moving start pt from y1 to y1 + n we need to figure out x1 + m - there may be several
371 * solutions we pick the one that minimizes m (ie that first unlipped pt). As above:
372 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
373 * 2mdy > 2ndx - bias - dx
374 * m > (2ndx - bias - dx) / 2dy
375 * m = floor((2ndx - bias - dx) / 2dy) + 1
376 * m = (2ndx - bias - dx) / 2dy + 1
378 * Moving end pt from x2 to x2 - m, we need to figure out y2 - n
379 * err = 2dy - dx + 2(dx - m)dy - 2(dy - n)dx
380 * = 2dy - dx - 2mdy + 2ndx
381 * 0 >= 2dy - dx - 2mdy + 2ndx + bias - 2dy > -2dx
382 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
383 * again exactly one solution.
384 * 2ndx <= 2mdy - bias + dx
385 * n = floor((2mdy - bias + dx) / 2dx)
386 * = (2mdy - bias + dx) / 2dx
388 * Moving end pt from y2 to y2 - n when need x2 - m this time maximizing x2 - m so
389 * mininizing m to include all of the points at y = y2 - n. As above:
390 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
391 * 2mdy >= 2ndx + bias - dx
392 * m = ceiling((2ndx + bias - dx) / 2dy)
393 * = (2ndx + bias - dx - 1) / 2dy + 1
395 * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
397 * Moving start point from y1 to y1 + n find x1 + m
398 * m = (2ndx + bias + dy - 1) / 2dy
400 * Moving start point from x1 to x1 + m find y1 + n
401 * n = (2mdy - bias - dy) / 2ndx + 1
403 * Moving end point from y2 to y2 - n find x1 - m
404 * m = (2ndx - bias + dy) / 2dy
406 * Moving end point from x2 to x2 - m find y2 - n
407 * n = (2mdy + bias - dy - 1) / 2dx + 1
409 int clip_line(const POINT *start, const POINT *end, const RECT *clip,
410 const bres_params *params, POINT *pt1, POINT *pt2)
413 BOOL clipped = FALSE;
414 DWORD start_oc, end_oc;
415 const int bias = params->bias;
416 const unsigned int dx = params->dx;
417 const unsigned int dy = params->dy;
418 const unsigned int two_dx = params->dx * 2;
419 const unsigned int two_dy = params->dy * 2;
420 const BOOL xmajor = is_xmajor(params->octant);
421 const BOOL neg_slope = !is_pos_slope(params->octant);
426 start_oc = calc_outcode(start, clip);
427 end_oc = calc_outcode(end, clip);
431 if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
432 if(start_oc & end_oc) return 0; /* trivial reject */
435 if(start_oc & OUT_LEFT)
437 m = clip->left - start->x;
439 n = (m * two_dy + bias + dx - 1) / two_dx;
441 n = (m * two_dy - bias - dy) / two_dx + 1;
444 if(neg_slope) n = -n;
445 pt1->y = start->y + n;
446 start_oc = calc_outcode(pt1, clip);
448 else if(start_oc & OUT_RIGHT)
450 m = start->x - clip->right + 1;
452 n = (m * two_dy + bias + dx - 1) / two_dx;
454 n = (m * two_dy - bias - dy) / two_dx + 1;
456 pt1->x = clip->right - 1;
457 if(neg_slope) n = -n;
458 pt1->y = start->y - n;
459 start_oc = calc_outcode(pt1, clip);
461 else if(start_oc & OUT_TOP)
463 n = clip->top - start->y;
465 m = (n * two_dx - bias - dx) / two_dy + 1;
467 m = (n * two_dx + bias + dy - 1) / two_dy;
470 if(neg_slope) m = -m;
471 pt1->x = start->x + m;
472 start_oc = calc_outcode(pt1, clip);
474 else if(start_oc & OUT_BOTTOM)
476 n = start->y - clip->bottom + 1;
478 m = (n * two_dx - bias - dx) / two_dy + 1;
480 m = (n * two_dx + bias + dy - 1) / two_dy;
482 pt1->y = clip->bottom - 1;
483 if(neg_slope) m = -m;
484 pt1->x = start->x - m;
485 start_oc = calc_outcode(pt1, clip);
487 else if(end_oc & OUT_LEFT)
489 m = clip->left - end->x;
491 n = (m * two_dy - bias + dx) / two_dx;
493 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
496 if(neg_slope) n = -n;
498 end_oc = calc_outcode(pt2, clip);
500 else if(end_oc & OUT_RIGHT)
502 m = end->x - clip->right + 1;
504 n = (m * two_dy - bias + dx) / two_dx;
506 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
508 pt2->x = clip->right - 1;
509 if(neg_slope) n = -n;
511 end_oc = calc_outcode(pt2, clip);
513 else if(end_oc & OUT_TOP)
515 n = clip->top - end->y;
517 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
519 m = (n * two_dx - bias + dy) / two_dy;
522 if(neg_slope) m = -m;
524 end_oc = calc_outcode(pt2, clip);
526 else if(end_oc & OUT_BOTTOM)
528 n = end->y - clip->bottom + 1;
530 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
532 m = (n * two_dx - bias + dy) / two_dy;
534 pt2->y = clip->bottom - 1;
535 if(neg_slope) m = -m;
537 end_oc = calc_outcode(pt2, clip);
542 static void bres_line_with_bias(INT x1, INT y1, INT x2, INT y2, const bres_params *params, INT err,
543 BOOL last_pt, void (* callback)(dibdrv_physdev*,INT,INT), dibdrv_physdev *pdev)
545 const int xadd = is_x_increasing(params->octant) ? 1 : -1;
546 const int yadd = is_y_increasing(params->octant) ? 1 : -1;
549 if (is_xmajor(params->octant)) /* line is "more horizontal" */
551 erradd = 2*params->dy - 2*params->dx;
554 callback(pdev, x1, y1);
555 if (err + params->bias > 0)
560 else err += 2*params->dy;
563 if(last_pt) callback(pdev, x1, y1);
565 else /* line is "more vertical" */
567 erradd = 2*params->dx - 2*params->dy;
570 callback(pdev, x1, y1);
571 if (err + params->bias > 0)
576 else err += 2*params->dx;
579 if(last_pt) callback(pdev, x1, y1);
583 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
585 const WINEREGION *clip = get_wine_region(pdev->clip);
587 if(start->y == end->y)
592 rect.left = start->x;
595 rect.bottom = end->y + 1;
596 order_end_points(&rect.left, &rect.right);
597 for(i = 0; i < clip->numRects; i++)
599 if(clip->rects[i].top >= rect.bottom) break;
600 if(clip->rects[i].bottom <= rect.top) continue;
601 /* Optimize the unclipped case */
602 if(clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
604 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
607 if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
610 tmp.left = max(rect.left, clip->rects[i].left);
611 tmp.right = min(rect.right, clip->rects[i].right);
612 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
616 else if(start->x == end->x)
621 rect.left = start->x;
623 rect.right = end->x + 1;
624 rect.bottom = end->y;
625 order_end_points(&rect.top, &rect.bottom);
626 for(i = 0; i < clip->numRects; i++)
628 /* Optimize unclipped case */
629 if(clip->rects[i].top <= rect.top && clip->rects[i].bottom >= rect.bottom &&
630 clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
632 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
635 if(clip->rects[i].top >= rect.bottom) break;
636 if(clip->rects[i].bottom <= rect.top) continue;
637 if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
640 tmp.top = max(rect.top, clip->rects[i].top);
641 tmp.bottom = min(rect.bottom, clip->rects[i].bottom);
642 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
649 INT dx = end->x - start->x;
650 INT dy = end->y - start->y;
655 params.octant = get_octant_mask(dx, dy);
656 /* Octants 3, 5, 6 and 8 take a bias */
657 params.bias = (params.octant & 0xb4) ? 1 : 0;
659 for(i = 0; i < clip->numRects; i++)
661 POINT clipped_start, clipped_end;
663 clip_status = clip_line(start, end, clip->rects + i, ¶ms, &clipped_start, &clipped_end);
667 int m = abs(clipped_start.x - start->x);
668 int n = abs(clipped_start.y - start->y);
670 BOOL last_pt = FALSE;
672 if(is_xmajor(params.octant))
673 err = 2 * params.dy - params.dx + m * 2 * params.dy - n * 2 * params.dx;
675 err = 2 * params.dx - params.dy + n * 2 * params.dx - m * 2 * params.dy;
677 if(clip_status == 1 && (end->x != clipped_end.x || end->y != clipped_end.y)) last_pt = TRUE;
679 bres_line_with_bias(clipped_start.x, clipped_start.y, clipped_end.x, clipped_end.y, ¶ms,
680 err, last_pt, solid_pen_line_callback, pdev);
682 if(clip_status == 2) break; /* completely unclipped, so we can finish */
687 release_wine_region(pdev->clip);
691 static BOOL solid_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts)
696 for (i = 0; i < num - 1; i++)
697 if (!solid_pen_line( pdev, pts + i, pts + i + 1 ))
703 void reset_dash_origin(dibdrv_physdev *pdev)
705 pdev->dash_pos.cur_dash = 0;
706 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
707 pdev->dash_pos.mark = TRUE;
710 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
712 skip %= pdev->pen_pattern.total_len;
715 if(pdev->dash_pos.left_in_dash > skip)
717 pdev->dash_pos.left_in_dash -= skip;
720 skip -= pdev->dash_pos.left_in_dash;
721 pdev->dash_pos.cur_dash++;
722 if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
723 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
724 pdev->dash_pos.mark = !pdev->dash_pos.mark;
728 static inline void get_dash_colors(const dibdrv_physdev *pdev, DWORD *and, DWORD *xor)
730 if(pdev->dash_pos.mark)
732 *and = pdev->pen_and;
733 *xor = pdev->pen_xor;
737 get_pen_bkgnd_masks( pdev, and, xor );
741 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
746 get_dash_colors(pdev, &and, &xor);
752 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
756 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
758 const WINEREGION *clip = get_wine_region(pdev->clip);
762 const dash_pos start_pos = pdev->dash_pos;
764 if(start->y == end->y) /* hline */
767 INT left, right, cur_x;
770 rect.bottom = start->y + 1;
772 if(start->x <= end->x)
785 for(i = 0; i < clip->numRects; i++)
787 if(clip->rects[i].top > start->y) break;
788 if(clip->rects[i].bottom <= start->y) continue;
790 if(clip->rects[i].right > left && clip->rects[i].left <= right)
792 int clipped_left = max(clip->rects[i].left, left);
793 int clipped_right = min(clip->rects[i].right - 1, right);
795 pdev->dash_pos = start_pos;
799 cur_x = clipped_left;
801 skip_dash(pdev, clipped_left - left);
803 while(cur_x <= clipped_right)
805 get_dash_colors(pdev, &and, &xor);
806 dash_len = pdev->dash_pos.left_in_dash;
807 if(cur_x + dash_len > clipped_right + 1)
808 dash_len = clipped_right - cur_x + 1;
810 rect.right = cur_x + dash_len;
812 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
814 skip_dash(pdev, dash_len);
819 cur_x = clipped_right;
821 skip_dash(pdev, right - clipped_right);
823 while(cur_x >= clipped_left)
825 get_dash_colors(pdev, &and, &xor);
826 dash_len = pdev->dash_pos.left_in_dash;
827 if(cur_x - dash_len < clipped_left - 1)
828 dash_len = cur_x - clipped_left + 1;
829 rect.left = cur_x - dash_len + 1;
830 rect.right = cur_x + 1;
832 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
834 skip_dash(pdev, dash_len);
839 pdev->dash_pos = start_pos;
840 skip_dash(pdev, right - left + 1);
842 else if(start->x == end->x) /* vline */
845 INT top, bottom, cur_y;
847 rect.left = start->x;
848 rect.right = start->x + 1;
850 if(start->y <= end->y)
863 for(i = 0; i < clip->numRects; i++)
865 if(clip->rects[i].top > bottom) break;
866 if(clip->rects[i].bottom <= top) continue;
867 if(clip->rects[i].right > start->x && clip->rects[i].left <= start->x)
869 int clipped_top = max(clip->rects[i].top, top);
870 int clipped_bottom = min(clip->rects[i].bottom - 1, bottom);
872 pdev->dash_pos = start_pos;
878 skip_dash(pdev, clipped_top - top);
880 while(cur_y <= clipped_bottom)
882 get_dash_colors(pdev, &and, &xor);
883 dash_len = pdev->dash_pos.left_in_dash;
884 if(cur_y + dash_len > clipped_bottom + 1)
885 dash_len = clipped_bottom - cur_y + 1;
887 rect.bottom = cur_y + dash_len;
889 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
891 skip_dash(pdev, dash_len);
896 cur_y = clipped_bottom;
898 skip_dash(pdev, bottom - clipped_bottom);
900 while(cur_y >= clipped_top)
902 get_dash_colors(pdev, &and, &xor);
903 dash_len = pdev->dash_pos.left_in_dash;
904 if(cur_y - dash_len < clipped_top - 1)
905 dash_len = cur_y - clipped_top + 1;
906 rect.top = cur_y - dash_len + 1;
907 rect.bottom = cur_y + 1;
909 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
911 skip_dash(pdev, dash_len);
916 pdev->dash_pos = start_pos;
917 skip_dash(pdev, bottom - top + 1);
922 INT dx = end->x - start->x;
923 INT dy = end->y - start->y;
928 params.octant = get_octant_mask(dx, dy);
929 /* Octants 3, 5, 6 and 8 take a bias */
930 params.bias = (params.octant & 0xb4) ? 1 : 0;
932 for(i = 0; i < clip->numRects; i++)
934 POINT clipped_start, clipped_end;
936 clip_status = clip_line(start, end, clip->rects + i, ¶ms, &clipped_start, &clipped_end);
940 int m = abs(clipped_start.x - start->x);
941 int n = abs(clipped_start.y - start->y);
943 BOOL last_pt = FALSE;
945 pdev->dash_pos = start_pos;
947 if(is_xmajor(params.octant))
949 err = 2 * params.dy - params.dx + m * 2 * params.dy - n * 2 * params.dx;
954 err = 2 * params.dx - params.dy + n * 2 * params.dx - m * 2 * params.dy;
957 if(clip_status == 1 && (end->x != clipped_end.x || end->y != clipped_end.y)) last_pt = TRUE;
959 bres_line_with_bias(clipped_start.x, clipped_start.y, clipped_end.x, clipped_end.y, ¶ms,
960 err, last_pt, dashed_pen_line_callback, pdev);
962 if(clip_status == 2) break; /* completely unclipped, so we can finish */
965 pdev->dash_pos = start_pos;
966 if(is_xmajor(params.octant))
967 skip_dash(pdev, params.dx);
969 skip_dash(pdev, params.dy);
972 release_wine_region(pdev->clip);
976 static BOOL dashed_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts)
981 for (i = 0; i < num - 1; i++)
982 if (!dashed_pen_line( pdev, pts + i, pts + i + 1 ))
988 static BOOL null_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts)
993 static const dash_pattern dash_patterns[5] =
995 {0, {0}, 0}, /* PS_SOLID - a pseudo-pattern used to initialise unpatterned pens. */
996 {2, {18, 6}, 24}, /* PS_DASH */
997 {2, {3, 3}, 6}, /* PS_DOT */
998 {4, {9, 6, 3, 6}, 24}, /* PS_DASHDOT */
999 {6, {9, 3, 3, 3, 3, 3}, 24} /* PS_DASHDOTDOT */
1002 /***********************************************************************
1005 HPEN dibdrv_SelectPen( PHYSDEV dev, HPEN hpen )
1007 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectPen );
1008 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1012 TRACE("(%p, %p)\n", dev, hpen);
1014 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
1016 /* must be an extended pen */
1018 INT size = GetObjectW( hpen, 0, NULL );
1020 if (!size) return 0;
1022 elp = HeapAlloc( GetProcessHeap(), 0, size );
1024 GetObjectW( hpen, size, elp );
1025 /* FIXME: add support for user style pens */
1026 logpen.lopnStyle = elp->elpPenStyle;
1027 logpen.lopnWidth.x = elp->elpWidth;
1028 logpen.lopnWidth.y = 0;
1029 logpen.lopnColor = elp->elpColor;
1031 HeapFree( GetProcessHeap(), 0, elp );
1034 if (hpen == GetStockObject( DC_PEN ))
1035 logpen.lopnColor = GetDCPenColor( dev->hdc );
1037 pdev->pen_colorref = logpen.lopnColor;
1038 pdev->pen_color = get_pixel_color( pdev, pdev->pen_colorref, TRUE );
1039 calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
1041 pdev->pen_pattern = dash_patterns[PS_SOLID];
1043 pdev->defer |= DEFER_PEN;
1045 style = logpen.lopnStyle & PS_STYLE_MASK;
1050 if(logpen.lopnStyle & PS_GEOMETRIC) break;
1051 if(logpen.lopnWidth.x > 1) break;
1052 pdev->pen_lines = solid_pen_lines;
1053 pdev->defer &= ~DEFER_PEN;
1060 if(logpen.lopnStyle & PS_GEOMETRIC) break;
1061 if(logpen.lopnWidth.x > 1) break;
1062 pdev->pen_lines = dashed_pen_lines;
1063 pdev->pen_pattern = dash_patterns[style];
1064 pdev->defer &= ~DEFER_PEN;
1068 pdev->pen_lines = null_pen_lines;
1069 pdev->defer &= ~DEFER_PEN;
1076 return next->funcs->pSelectPen( next, hpen );
1079 /***********************************************************************
1080 * dibdrv_SetDCPenColor
1082 COLORREF dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1084 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCPenColor );
1085 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1087 if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
1089 pdev->pen_colorref = color;
1090 pdev->pen_color = get_pixel_color( pdev, pdev->pen_colorref, TRUE );
1091 calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
1094 return next->funcs->pSetDCPenColor( next, color );
1097 /**********************************************************************
1100 * Fill a number of rectangles with the solid brush
1101 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1103 static BOOL solid_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1106 const WINEREGION *clip = get_wine_region( region );
1110 dib->funcs->solid_rects( dib, num, rects, pdev->brush_and, pdev->brush_xor );
1114 for(i = 0; i < num; i++)
1116 for(j = 0; j < clip->numRects; j++)
1118 RECT rect = rects[i];
1120 /* Optimize unclipped case */
1121 if(clip->rects[j].top <= rect.top && clip->rects[j].bottom >= rect.bottom &&
1122 clip->rects[j].left <= rect.left && clip->rects[j].right >= rect.right)
1124 dib->funcs->solid_rects( dib, 1, &rect, pdev->brush_and, pdev->brush_xor );
1128 if(clip->rects[j].top >= rect.bottom) break;
1129 if(clip->rects[j].bottom <= rect.top) continue;
1131 if(clip->rects[j].right > rect.left && clip->rects[j].left < rect.right)
1133 rect.left = max(rect.left, clip->rects[j].left);
1134 rect.top = max(rect.top, clip->rects[j].top);
1135 rect.right = min(rect.right, clip->rects[j].right);
1136 rect.bottom = min(rect.bottom, clip->rects[j].bottom);
1138 dib->funcs->solid_rects( dib, 1, &rect, pdev->brush_and, pdev->brush_xor );
1142 release_wine_region( region );
1146 static void free_pattern_brush_bits( dibdrv_physdev *pdev )
1148 HeapFree(GetProcessHeap(), 0, pdev->brush_and_bits);
1149 HeapFree(GetProcessHeap(), 0, pdev->brush_xor_bits);
1150 pdev->brush_and_bits = NULL;
1151 pdev->brush_xor_bits = NULL;
1154 void free_pattern_brush( dibdrv_physdev *pdev )
1156 free_pattern_brush_bits( pdev );
1157 free_dib_info( &pdev->brush_dib );
1160 static BOOL create_pattern_brush_bits(dibdrv_physdev *pdev)
1162 DWORD size = pdev->brush_dib.height * abs(pdev->brush_dib.stride);
1163 DWORD *brush_bits = pdev->brush_dib.bits.ptr;
1164 DWORD *and_bits, *xor_bits;
1166 assert(pdev->brush_and_bits == NULL);
1167 assert(pdev->brush_xor_bits == NULL);
1169 assert(pdev->brush_dib.stride > 0);
1171 and_bits = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1172 xor_bits = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1174 if(!and_bits || !xor_bits)
1176 ERR("Failed to create pattern brush bits\n");
1177 free_pattern_brush_bits( pdev );
1183 calc_and_xor_masks(pdev->brush_rop, *brush_bits++, and_bits++, xor_bits++);
1190 static const DWORD hatches[6][8] =
1192 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1193 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
1194 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL */
1195 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL */
1196 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
1197 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
1200 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev)
1203 rop_mask fg_mask, bg_mask;
1204 rop_mask_bits mask_bits;
1208 assert(pdev->brush_and_bits == NULL);
1209 assert(pdev->brush_xor_bits == NULL);
1211 /* Just initialise brush_dib with the color / sizing info. We don't
1212 need the bits as we'll calculate the rop masks straight from
1213 the hatch patterns. */
1215 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1216 pdev->brush_dib.width = 8;
1217 pdev->brush_dib.height = 8;
1218 pdev->brush_dib.stride = get_dib_stride( pdev->brush_dib.width, pdev->brush_dib.bit_count );
1220 size = pdev->brush_dib.height * pdev->brush_dib.stride;
1222 mask_bits.and = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1223 mask_bits.xor = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1225 if(!mask_bits.and || !mask_bits.xor)
1227 ERR("Failed to create pattern brush bits\n");
1228 free_pattern_brush_bits( pdev );
1232 hatch.bit_count = 1;
1233 hatch.height = hatch.width = 8;
1235 hatch.bits.ptr = (void *) hatches[pdev->brush_hatch];
1236 hatch.bits.free = hatch.bits.param = NULL;
1237 hatch.bits.is_copy = FALSE;
1239 fg_mask.and = pdev->brush_and;
1240 fg_mask.xor = pdev->brush_xor;
1241 get_brush_bkgnd_masks( pdev, &bg_mask.and, &bg_mask.xor );
1243 ret = pdev->brush_dib.funcs->create_rop_masks( &pdev->brush_dib, &hatch, &fg_mask, &bg_mask, &mask_bits );
1244 if(!ret) free_pattern_brush_bits( pdev );
1249 /**********************************************************************
1252 * Fill a number of rectangles with the pattern brush
1253 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1255 static BOOL pattern_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1258 const WINEREGION *clip;
1261 if(pdev->brush_and_bits == NULL)
1263 switch(pdev->brush_style)
1266 if(!create_pattern_brush_bits(pdev))
1271 if(!create_hatch_brush_bits(pdev))
1276 ERR("Unexpected brush style %d\n", pdev->brush_style);
1281 GetBrushOrgEx(pdev->dev.hdc, &origin);
1283 clip = get_wine_region( region );
1287 dib->funcs->pattern_rects( dib, num, rects, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1291 for(i = 0; i < num; i++)
1293 for(j = 0; j < clip->numRects; j++)
1295 RECT rect = rects[i];
1297 /* Optimize unclipped case */
1298 if(clip->rects[j].top <= rect.top && clip->rects[j].bottom >= rect.bottom &&
1299 clip->rects[j].left <= rect.left && clip->rects[j].right >= rect.right)
1301 dib->funcs->pattern_rects( dib, 1, &rect, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1305 if(clip->rects[j].top >= rect.bottom) break;
1306 if(clip->rects[j].bottom <= rect.top) continue;
1308 if(clip->rects[j].right > rect.left && clip->rects[j].left < rect.right)
1310 rect.left = max(rect.left, clip->rects[j].left);
1311 rect.top = max(rect.top, clip->rects[j].top);
1312 rect.right = min(rect.right, clip->rects[j].right);
1313 rect.bottom = min(rect.bottom, clip->rects[j].bottom);
1315 dib->funcs->pattern_rects( dib, 1, &rect, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1319 release_wine_region( region );
1323 static BOOL null_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1328 void update_brush_rop( dibdrv_physdev *pdev, INT rop )
1330 pdev->brush_rop = rop;
1331 if(pdev->brush_style == BS_SOLID || pdev->brush_style == BS_HATCHED)
1332 calc_and_xor_masks(rop, pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1333 free_pattern_brush_bits( pdev );
1336 /***********************************************************************
1337 * dibdrv_SelectBrush
1339 HBRUSH dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush )
1341 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBrush );
1342 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1345 TRACE("(%p, %p)\n", dev, hbrush);
1347 if (!GetObjectW( hbrush, sizeof(logbrush), &logbrush )) return 0;
1349 if (hbrush == GetStockObject( DC_BRUSH ))
1350 logbrush.lbColor = GetDCBrushColor( dev->hdc );
1352 pdev->brush_style = logbrush.lbStyle;
1354 pdev->defer |= DEFER_BRUSH;
1356 free_pattern_brush( pdev );
1358 switch(logbrush.lbStyle)
1361 pdev->brush_colorref = logbrush.lbColor;
1362 pdev->brush_color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
1363 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1364 pdev->brush_rects = solid_brush;
1365 pdev->defer &= ~DEFER_BRUSH;
1369 pdev->brush_rects = null_brush;
1370 pdev->defer &= ~DEFER_BRUSH;
1375 BITMAPINFOHEADER *bi = GlobalLock((HGLOBAL)logbrush.lbHatch);
1377 WORD usage = LOWORD(logbrush.lbColor);
1378 HPALETTE pal = (usage == DIB_PAL_COLORS) ? GetCurrentObject(dev->hdc, OBJ_PAL) : NULL;
1381 if(!bi) return NULL;
1382 if(init_dib_info_from_packed(&orig_dib, bi, usage, pal))
1384 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1386 pdev->brush_dib.height = orig_dib.height;
1387 pdev->brush_dib.width = orig_dib.width;
1388 pdev->brush_dib.stride = get_dib_stride( pdev->brush_dib.width, pdev->brush_dib.bit_count );
1390 pdev->brush_dib.bits.param = NULL;
1391 pdev->brush_dib.bits.ptr = HeapAlloc( GetProcessHeap(), 0,
1392 pdev->brush_dib.height * pdev->brush_dib.stride );
1393 pdev->brush_dib.bits.is_copy = TRUE;
1394 pdev->brush_dib.bits.free = free_heap_bits;
1396 rect.left = rect.top = 0;
1397 rect.right = orig_dib.width;
1398 rect.bottom = orig_dib.height;
1400 pdev->brush_dib.funcs->convert_to(&pdev->brush_dib, &orig_dib, &rect);
1401 pdev->brush_rects = pattern_brush;
1402 pdev->defer &= ~DEFER_BRUSH;
1403 free_dib_info(&orig_dib);
1405 GlobalUnlock((HGLOBAL)logbrush.lbHatch);
1411 if(logbrush.lbHatch > HS_DIAGCROSS) return 0;
1412 pdev->brush_hatch = logbrush.lbHatch;
1413 pdev->brush_colorref = logbrush.lbColor;
1414 pdev->brush_color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
1415 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1416 pdev->brush_rects = pattern_brush;
1417 pdev->defer &= ~DEFER_BRUSH;
1425 return next->funcs->pSelectBrush( next, hbrush );
1428 /***********************************************************************
1429 * dibdrv_SetDCBrushColor
1431 COLORREF dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
1433 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCBrushColor );
1434 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1436 if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
1438 pdev->brush_colorref = color;
1439 pdev->brush_color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
1440 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1443 return next->funcs->pSetDCBrushColor( next, color );
1446 BOOL brush_rects(dibdrv_physdev *pdev, int num, const RECT *rects)
1448 return pdev->brush_rects( pdev, &pdev->dib, num, rects, pdev->clip );