gdi32: Use a region to render geometric and wide pens in PolyPolyline.
[wine.git] / dlls / gdi32 / dibdrv / objects.c
blob197b220d0ba072186ae3655b5f918650a9fa28b5
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 INT bkgnd_mode, 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 (bkgnd_mode == 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 struct clipped_rects clipped_rects;
544 RECT rect;
545 int i;
547 if(start->y == end->y)
549 rect.left = start->x;
550 rect.top = start->y;
551 rect.right = end->x;
552 rect.bottom = end->y + 1;
553 order_end_points(&rect.left, &rect.right);
554 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
555 pdev->dib.funcs->solid_rects(&pdev->dib, clipped_rects.count, clipped_rects.rects, and, xor);
557 else if(start->x == end->x)
559 rect.left = start->x;
560 rect.top = start->y;
561 rect.right = end->x + 1;
562 rect.bottom = end->y;
563 order_end_points(&rect.top, &rect.bottom);
564 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
565 pdev->dib.funcs->solid_rects(&pdev->dib, clipped_rects.count, clipped_rects.rects, and, xor);
567 else
569 bres_params clip_params;
570 struct line_params line_params;
571 INT dx = end->x - start->x, dy = end->y - start->y;
572 INT abs_dx = abs(dx), abs_dy = abs(dy);
574 clip_params.dx = abs_dx;
575 clip_params.dy = abs_dy;
576 clip_params.octant = get_octant_mask(dx, dy);
577 clip_params.bias = get_bias( clip_params.octant );
579 line_params.bias = clip_params.bias;
580 line_params.x_major = is_xmajor( clip_params.octant );
581 line_params.x_inc = is_x_increasing( clip_params.octant ) ? 1 : -1;
582 line_params.y_inc = is_y_increasing( clip_params.octant ) ? 1 : -1;
584 if (line_params.x_major)
586 line_params.err_add_1 = 2 * abs_dy - 2 * abs_dx;
587 line_params.err_add_2 = 2 * abs_dy;
589 else
591 line_params.err_add_1 = 2 * abs_dx - 2 * abs_dy;
592 line_params.err_add_2 = 2 * abs_dx;
595 rect.left = min( start->x, end->x );
596 rect.top = min( start->y, end->y );
597 rect.right = max( start->x, end->x ) + 1;
598 rect.bottom = max( start->y, end->y ) + 1;
599 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
600 for (i = 0; i < clipped_rects.count; i++)
602 POINT clipped_start, clipped_end;
603 int clip_status;
605 clip_status = clip_line(start, end, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
606 if(clip_status)
608 int m = abs(clipped_start.x - start->x);
609 int n = abs(clipped_start.y - start->y);
611 if (line_params.x_major)
613 line_params.err_start = 2 * abs_dy - abs_dx + m * 2 * abs_dy - n * 2 * abs_dx;
614 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
616 else
618 line_params.err_start = 2 * abs_dx - abs_dy + n * 2 * abs_dx - m * 2 * abs_dy;
619 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
622 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
624 pdev->dib.funcs->solid_line( &pdev->dib, &clipped_start, &line_params, and, xor );
626 if(clip_status == 2) break; /* completely unclipped, so we can finish */
630 free_clipped_rects( &clipped_rects );
631 return TRUE;
634 static BOOL solid_pen_line_region( dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region )
636 RECT rect;
638 rect.left = start->x;
639 rect.top = start->y;
640 rect.right = start->x + 1;
641 rect.bottom = start->y + 1;
643 if (start->y == end->y)
645 rect.right = end->x;
646 order_end_points(&rect.left, &rect.right);
647 add_rect_to_region( region, &rect );
649 else if(start->x == end->x)
651 rect.bottom = end->y;
652 order_end_points(&rect.top, &rect.bottom);
653 add_rect_to_region( region, &rect );
655 else
657 INT dx = end->x - start->x, dy = end->y - start->y;
658 INT abs_dx = abs(dx), abs_dy = abs(dy);
659 DWORD octant = get_octant_mask(dx, dy);
660 INT bias = get_bias( octant );
662 if (is_xmajor( octant ))
664 int y_inc = is_y_increasing( octant ) ? 1 : -1;
665 int err_add_1 = 2 * abs_dy - 2 * abs_dx;
666 int err_add_2 = 2 * abs_dy;
667 int err = 2 * abs_dy - abs_dx;
669 if (is_x_increasing( octant ))
671 for (rect.right = start->x + 1; rect.right <= end->x; rect.right++)
673 if (err + bias > 0)
675 add_rect_to_region( region, &rect );
676 rect.left = rect.right;
677 rect.top += y_inc;
678 rect.bottom += y_inc;
679 err += err_add_1;
681 else err += err_add_2;
684 else
686 for (rect.left = start->x; rect.left > end->x; rect.left--)
688 if (err + bias > 0)
690 add_rect_to_region( region, &rect );
691 rect.right = rect.left;
692 rect.top += y_inc;
693 rect.bottom += y_inc;
694 err += err_add_1;
696 else err += err_add_2;
700 else
702 int x_inc = is_x_increasing( octant ) ? 1 : -1;
703 int err_add_1 = 2 * abs_dx - 2 * abs_dy;
704 int err_add_2 = 2 * abs_dx;
705 int err = 2 * abs_dx - abs_dy;
707 if (is_y_increasing( octant ))
709 for (rect.bottom = start->y + 1; rect.bottom <= end->y; rect.bottom++)
711 if (err + bias > 0)
713 add_rect_to_region( region, &rect );
714 rect.top = rect.bottom;
715 rect.left += x_inc;
716 rect.right += x_inc;
717 err += err_add_1;
719 else err += err_add_2;
722 else
724 for (rect.top = start->y; rect.top > end->y; rect.top--)
726 if (err + bias > 0)
728 add_rect_to_region( region, &rect );
729 rect.bottom = rect.top;
730 rect.left += x_inc;
731 rect.right += x_inc;
732 err += err_add_1;
734 else err += err_add_2;
738 /* add final rect */
739 add_rect_to_region( region, &rect );
741 return TRUE;
744 static BOOL solid_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
746 int i;
748 assert( num >= 2 );
750 if (region)
752 for (i = 0; i < num - 1; i++)
753 if (!solid_pen_line_region( pdev, pts + i, pts + i + 1, region ))
754 return FALSE;
755 if (close) return solid_pen_line_region( pdev, pts + num - 1, pts, region );
757 else
759 DWORD color, and, xor;
761 color = get_pixel_color( pdev, pdev->pen_colorref, TRUE );
762 calc_and_xor_masks( GetROP2(pdev->dev.hdc), color, &and, &xor );
764 for (i = 0; i < num - 1; i++)
765 if (!solid_pen_line( pdev, pts + i, pts + i + 1, and, xor ))
766 return FALSE;
767 if (close) return solid_pen_line( pdev, pts + num - 1, pts, and, xor );
769 return TRUE;
772 void reset_dash_origin(dibdrv_physdev *pdev)
774 pdev->dash_pos.cur_dash = 0;
775 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
776 pdev->dash_pos.mark = TRUE;
779 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
781 skip %= pdev->pen_pattern.total_len;
782 while(skip)
784 if(pdev->dash_pos.left_in_dash > skip)
786 pdev->dash_pos.left_in_dash -= skip;
787 return;
789 skip -= pdev->dash_pos.left_in_dash;
790 pdev->dash_pos.cur_dash++;
791 if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
792 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
793 pdev->dash_pos.mark = !pdev->dash_pos.mark;
797 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
799 RECT rect;
800 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
802 skip_dash(pdev, 1);
803 rect.left = x;
804 rect.right = x + 1;
805 rect.top = y;
806 rect.bottom = y + 1;
807 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
808 return;
811 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
813 struct clipped_rects clipped_rects;
814 int i, dash_len;
815 RECT rect;
816 const dash_pos start_pos = pdev->dash_pos;
818 if(start->y == end->y) /* hline */
820 BOOL l_to_r;
821 INT left, right, cur_x;
823 rect.top = start->y;
824 rect.bottom = start->y + 1;
826 if(start->x <= end->x)
828 left = start->x;
829 right = end->x - 1;
830 l_to_r = TRUE;
832 else
834 left = end->x + 1;
835 right = start->x;
836 l_to_r = FALSE;
839 rect.left = min( start->x, end->x );
840 rect.right = max( start->x, end->x ) + 1;
841 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
842 for (i = 0; i < clipped_rects.count; i++)
844 if(clipped_rects.rects[i].right > left && clipped_rects.rects[i].left <= right)
846 int clipped_left = max(clipped_rects.rects[i].left, left);
847 int clipped_right = min(clipped_rects.rects[i].right - 1, right);
849 pdev->dash_pos = start_pos;
851 if(l_to_r)
853 cur_x = clipped_left;
854 if(cur_x != left)
855 skip_dash(pdev, clipped_left - left);
857 while(cur_x <= clipped_right)
859 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
860 dash_len = pdev->dash_pos.left_in_dash;
861 if(cur_x + dash_len > clipped_right + 1)
862 dash_len = clipped_right - cur_x + 1;
863 rect.left = cur_x;
864 rect.right = cur_x + dash_len;
866 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
867 cur_x += dash_len;
868 skip_dash(pdev, dash_len);
871 else
873 cur_x = clipped_right;
874 if(cur_x != right)
875 skip_dash(pdev, right - clipped_right);
877 while(cur_x >= clipped_left)
879 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
880 dash_len = pdev->dash_pos.left_in_dash;
881 if(cur_x - dash_len < clipped_left - 1)
882 dash_len = cur_x - clipped_left + 1;
883 rect.left = cur_x - dash_len + 1;
884 rect.right = cur_x + 1;
886 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
887 cur_x -= dash_len;
888 skip_dash(pdev, dash_len);
893 pdev->dash_pos = start_pos;
894 skip_dash(pdev, right - left + 1);
896 else if(start->x == end->x) /* vline */
898 BOOL t_to_b;
899 INT top, bottom, cur_y;
901 rect.left = start->x;
902 rect.right = start->x + 1;
904 if(start->y <= end->y)
906 top = start->y;
907 bottom = end->y - 1;
908 t_to_b = TRUE;
910 else
912 top = end->y + 1;
913 bottom = start->y;
914 t_to_b = FALSE;
917 rect.top = min( start->y, end->y );
918 rect.bottom = max( start->y, end->y ) + 1;
919 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
920 for (i = 0; i < clipped_rects.count; i++)
922 if(clipped_rects.rects[i].right > start->x && clipped_rects.rects[i].left <= start->x)
924 int clipped_top = max(clipped_rects.rects[i].top, top);
925 int clipped_bottom = min(clipped_rects.rects[i].bottom - 1, bottom);
927 pdev->dash_pos = start_pos;
929 if(t_to_b)
931 cur_y = clipped_top;
932 if(cur_y != top)
933 skip_dash(pdev, clipped_top - top);
935 while(cur_y <= clipped_bottom)
937 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
938 dash_len = pdev->dash_pos.left_in_dash;
939 if(cur_y + dash_len > clipped_bottom + 1)
940 dash_len = clipped_bottom - cur_y + 1;
941 rect.top = cur_y;
942 rect.bottom = cur_y + dash_len;
944 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
945 cur_y += dash_len;
946 skip_dash(pdev, dash_len);
949 else
951 cur_y = clipped_bottom;
952 if(cur_y != bottom)
953 skip_dash(pdev, bottom - clipped_bottom);
955 while(cur_y >= clipped_top)
957 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
958 dash_len = pdev->dash_pos.left_in_dash;
959 if(cur_y - dash_len < clipped_top - 1)
960 dash_len = cur_y - clipped_top + 1;
961 rect.top = cur_y - dash_len + 1;
962 rect.bottom = cur_y + 1;
964 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
965 cur_y -= dash_len;
966 skip_dash(pdev, dash_len);
971 pdev->dash_pos = start_pos;
972 skip_dash(pdev, bottom - top + 1);
974 else
976 bres_params clip_params;
977 struct line_params line_params;
978 INT dx = end->x - start->x, dy = end->y - start->y;
979 INT abs_dx = abs(dx), abs_dy = abs(dy);
981 clip_params.dx = abs_dx;
982 clip_params.dy = abs_dy;
983 clip_params.octant = get_octant_mask(dx, dy);
984 clip_params.bias = get_bias( clip_params.octant );
986 line_params.bias = clip_params.bias;
987 line_params.x_major = is_xmajor( clip_params.octant );
988 line_params.x_inc = is_x_increasing( clip_params.octant ) ? 1 : -1;
989 line_params.y_inc = is_y_increasing( clip_params.octant ) ? 1 : -1;
991 if (line_params.x_major)
993 line_params.err_add_1 = 2 * abs_dy - 2 * abs_dx;
994 line_params.err_add_2 = 2 * abs_dy;
996 else
998 line_params.err_add_1 = 2 * abs_dx - 2 * abs_dy;
999 line_params.err_add_2 = 2 * abs_dx;
1002 rect.left = min( start->x, end->x );
1003 rect.top = min( start->y, end->y );
1004 rect.right = max( start->x, end->x ) + 1;
1005 rect.bottom = max( start->y, end->y ) + 1;
1006 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
1007 for (i = 0; i < clipped_rects.count; i++)
1009 POINT clipped_start, clipped_end;
1010 int clip_status;
1011 clip_status = clip_line(start, end, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
1013 if(clip_status)
1015 int m = abs(clipped_start.x - start->x);
1016 int n = abs(clipped_start.y - start->y);
1018 pdev->dash_pos = start_pos;
1020 if (line_params.x_major)
1022 line_params.err_start = 2 * abs_dy - abs_dx + m * 2 * abs_dy - n * 2 * abs_dx;
1023 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
1024 skip_dash(pdev, m);
1026 else
1028 line_params.err_start = 2 * abs_dx - abs_dy + n * 2 * abs_dx - m * 2 * abs_dy;
1029 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
1030 skip_dash(pdev, n);
1032 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
1034 bres_line_with_bias( &clipped_start, &line_params, dashed_pen_line_callback, pdev );
1036 if(clip_status == 2) break; /* completely unclipped, so we can finish */
1039 pdev->dash_pos = start_pos;
1040 if(line_params.x_major)
1041 skip_dash(pdev, abs_dx);
1042 else
1043 skip_dash(pdev, abs_dy);
1046 free_clipped_rects( &clipped_rects );
1047 return TRUE;
1050 static BOOL dashed_pen_line_region(dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region)
1052 int i, dash_len;
1053 RECT rect;
1055 rect.left = start->x;
1056 rect.top = start->y;
1057 rect.right = start->x + 1;
1058 rect.bottom = start->y + 1;
1060 if (start->y == end->y) /* hline */
1062 if (start->x <= end->x)
1064 for (i = start->x; i < end->x; i += dash_len)
1066 dash_len = min( pdev->dash_pos.left_in_dash, end->x - i );
1067 if (pdev->dash_pos.mark)
1069 rect.left = i;
1070 rect.right = i + dash_len;
1071 add_rect_to_region( region, &rect );
1073 skip_dash(pdev, dash_len);
1076 else
1078 for (i = start->x; i > end->x; i -= dash_len)
1080 dash_len = min( pdev->dash_pos.left_in_dash, i - end->x );
1081 if (pdev->dash_pos.mark)
1083 rect.left = i - dash_len + 1;
1084 rect.right = i + 1;
1085 add_rect_to_region( region, &rect );
1087 skip_dash(pdev, dash_len);
1091 else if (start->x == end->x) /* vline */
1093 if (start->y <= end->y)
1095 for (i = start->y; i < end->y; i += dash_len)
1097 dash_len = min( pdev->dash_pos.left_in_dash, end->y - i );
1098 if (pdev->dash_pos.mark)
1100 rect.top = i;
1101 rect.bottom = i + dash_len;
1102 add_rect_to_region( region, &rect );
1104 skip_dash(pdev, dash_len);
1107 else
1109 for (i = start->y; i > end->y; i -= dash_len)
1111 dash_len = min( pdev->dash_pos.left_in_dash, i - end->y );
1112 if (pdev->dash_pos.mark)
1114 rect.top = i - dash_len + 1;
1115 rect.bottom = i + 1;
1116 add_rect_to_region( region, &rect );
1118 skip_dash(pdev, dash_len);
1122 else
1124 INT dx = end->x - start->x, dy = end->y - start->y;
1125 INT abs_dx = abs(dx), abs_dy = abs(dy);
1126 DWORD octant = get_octant_mask(dx, dy);
1127 INT bias = get_bias( octant );
1128 int x_inc = is_x_increasing( octant ) ? 1 : -1;
1129 int y_inc = is_y_increasing( octant ) ? 1 : -1;
1131 if (is_xmajor( octant ))
1133 int err_add_1 = 2 * abs_dy - 2 * abs_dx;
1134 int err_add_2 = 2 * abs_dy;
1135 int err = 2 * abs_dy - abs_dx;
1137 while (abs_dx--)
1139 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1140 skip_dash(pdev, 1);
1141 rect.left += x_inc;
1142 rect.right += x_inc;
1143 if (err + bias > 0)
1145 rect.top += y_inc;
1146 rect.bottom += y_inc;
1147 err += err_add_1;
1149 else err += err_add_2;
1153 else
1155 int err_add_1 = 2 * abs_dx - 2 * abs_dy;
1156 int err_add_2 = 2 * abs_dx;
1157 int err = 2 * abs_dx - abs_dy;
1159 while (abs_dy--)
1161 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1162 skip_dash(pdev, 1);
1163 rect.top += y_inc;
1164 rect.bottom += y_inc;
1165 if (err + bias > 0)
1167 rect.left += x_inc;
1168 rect.right += x_inc;
1169 err += err_add_1;
1171 else err += err_add_2;
1175 return TRUE;
1178 static BOOL dashed_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1180 int i;
1182 assert( num >= 2 );
1184 if (region)
1186 for (i = 0; i < num - 1; i++)
1187 if (!dashed_pen_line_region( pdev, pts + i, pts + i + 1, region ))
1188 return FALSE;
1189 if (close) return dashed_pen_line_region( pdev, pts + num - 1, pts, region );
1191 else
1193 get_color_masks( pdev, GetROP2(pdev->dev.hdc), pdev->pen_colorref,
1194 pdev->pen_is_ext ? TRANSPARENT : GetBkMode(pdev->dev.hdc),
1195 &pdev->dash_masks[1], &pdev->dash_masks[0] );
1197 for (i = 0; i < num - 1; i++)
1198 if (!dashed_pen_line( pdev, pts + i, pts + i + 1 ))
1199 return FALSE;
1200 if (close) return dashed_pen_line( pdev, pts + num - 1, pts );
1202 return TRUE;
1205 static BOOL null_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1207 return TRUE;
1210 struct face
1212 POINT start, end;
1213 int dx, dy;
1216 static void add_cap( dibdrv_physdev *pdev, HRGN region, const POINT *pt )
1218 HRGN cap;
1220 switch (pdev->pen_endcap)
1222 default: FIXME( "Unknown end cap %x\n", pdev->pen_endcap );
1223 /* fall through */
1224 case PS_ENDCAP_ROUND:
1225 cap = CreateEllipticRgn( pt->x - pdev->pen_width / 2, pt->y - pdev->pen_width / 2,
1226 pt->x + (pdev->pen_width + 1) / 2, pt->y + (pdev->pen_width + 1) / 2 );
1227 break;
1229 case PS_ENDCAP_SQUARE: /* already been handled */
1230 case PS_ENDCAP_FLAT:
1231 return;
1234 CombineRgn( region, region, cap, RGN_OR );
1235 DeleteObject( cap );
1236 return;
1239 #define round( f ) (((f) > 0) ? (f) + 0.5 : (f) - 0.5)
1241 /*******************************************************************************
1242 * create_miter_region
1244 * We need to calculate the intersection of two lines. We know a point
1245 * on each line (a face start and the other face end point) and
1246 * the direction vector of each line eg. (dx_1, dy_1).
1248 * (x, y) = (x_1, y_1) + u * (dx_1, dy_1) = (x_2, y_2) + v * (dx_2, dy_2)
1249 * solving (eg using Cramer's rule) gives:
1250 * u = ((x_2 - x_1) dy_2 - (y_2 - y_1) dx_2) / det
1251 * with det = dx_1 dy_2 - dx_2 dy_1
1252 * substituting back in and simplifying gives
1253 * (x, y) = a (dx_1, dy_1) - b (dx_2, dy_2)
1254 * with a = (x_2 dy_2 - y_2 dx_2) / det
1255 * and b = (x_1 dy_1 - y_1 dx_1) / det
1257 static HRGN create_miter_region( dibdrv_physdev *pdev, const POINT *pt,
1258 const struct face *face_1, const struct face *face_2 )
1260 int det = face_1->dx * face_2->dy - face_1->dy * face_2->dx;
1261 POINT pt_1, pt_2, pts[5];
1262 double a, b, x, y;
1263 FLOAT limit;
1265 if (det == 0) return 0;
1267 if (det < 0)
1269 const struct face *tmp = face_1;
1270 face_1 = face_2;
1271 face_2 = tmp;
1272 det = -det;
1275 pt_1 = face_1->start;
1276 pt_2 = face_2->end;
1278 a = (double)((pt_2.x * face_2->dy - pt_2.y * face_2->dx)) / det;
1279 b = (double)((pt_1.x * face_1->dy - pt_1.y * face_1->dx)) / det;
1281 x = a * face_1->dx - b * face_2->dx;
1282 y = a * face_1->dy - b * face_2->dy;
1284 GetMiterLimit( pdev->dev.hdc, &limit );
1286 if (((x - pt->x) * (x - pt->x) + (y - pt->y) * (y - pt->y)) * 4 > limit * limit * pdev->pen_width * pdev->pen_width)
1287 return 0;
1289 pts[0] = face_2->start;
1290 pts[1] = face_1->start;
1291 pts[2].x = round( x );
1292 pts[2].y = round( y );
1293 pts[3] = face_2->end;
1294 pts[4] = face_1->end;
1296 return CreatePolygonRgn( pts, 5, ALTERNATE );
1299 static void add_join( dibdrv_physdev *pdev, HRGN region, const POINT *pt,
1300 const struct face *face_1, const struct face *face_2 )
1302 HRGN join;
1303 POINT pts[4];
1305 switch (pdev->pen_join)
1307 default: FIXME( "Unknown line join %x\n", pdev->pen_join );
1308 /* fall through */
1309 case PS_JOIN_ROUND:
1310 join = CreateEllipticRgn( pt->x - pdev->pen_width / 2, pt->y - pdev->pen_width / 2,
1311 pt->x + (pdev->pen_width + 1) / 2, pt->y + (pdev->pen_width + 1) / 2 );
1312 break;
1314 case PS_JOIN_MITER:
1315 join = create_miter_region( pdev, pt, face_1, face_2 );
1316 if (join) break;
1317 /* fall through */
1318 case PS_JOIN_BEVEL:
1319 pts[0] = face_1->start;
1320 pts[1] = face_2->end;
1321 pts[2] = face_1->end;
1322 pts[3] = face_2->start;
1323 join = CreatePolygonRgn( pts, 4, ALTERNATE );
1324 break;
1327 CombineRgn( region, region, join, RGN_OR );
1328 DeleteObject( join );
1329 return;
1332 static BOOL get_wide_lines_region( dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total )
1334 int i;
1335 HRGN segment;
1337 assert( num >= 2 );
1339 if (!close) num--;
1340 for (i = 0; i < num; i++)
1342 const POINT *pt_1 = pts + i;
1343 const POINT *pt_2 = pts + ((close && i == num - 1) ? 0 : i + 1);
1344 int dx = pt_2->x - pt_1->x;
1345 int dy = pt_2->y - pt_1->y;
1346 RECT rect;
1347 struct face face_1, face_2, prev_face, first_face;
1348 BOOL need_cap_1 = !close && (i == 0);
1349 BOOL need_cap_2 = !close && (i == num - 1);
1350 BOOL sq_cap_1 = need_cap_1 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1351 BOOL sq_cap_2 = need_cap_2 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1353 if (dx == 0 && dy == 0) continue;
1355 if (dy == 0)
1357 rect.left = min( pt_1->x, pt_2->x );
1358 rect.right = rect.left + abs( dx );
1359 rect.top = pt_1->y - pdev->pen_width / 2;
1360 rect.bottom = rect.top + pdev->pen_width;
1361 if ((sq_cap_1 && dx > 0) || (sq_cap_2 && dx < 0)) rect.left -= pdev->pen_width / 2;
1362 if ((sq_cap_2 && dx > 0) || (sq_cap_1 && dx < 0)) rect.right += pdev->pen_width / 2;
1363 segment = CreateRectRgnIndirect( &rect );
1364 if (dx > 0)
1366 face_1.start.x = face_1.end.x = rect.left;
1367 face_1.start.y = face_2.end.y = rect.bottom;
1368 face_1.end.y = face_2.start.y = rect.top;
1369 face_2.start.x = face_2.end.x = rect.right - 1;
1371 else
1373 face_1.start.x = face_1.end.x = rect.right;
1374 face_1.start.y = face_2.end.y = rect.top;
1375 face_1.end.y = face_2.start.y = rect.bottom;
1376 face_2.start.x = face_2.end.x = rect.left + 1;
1379 else if (dx == 0)
1381 rect.top = min( pt_1->y, pt_2->y );
1382 rect.bottom = rect.top + abs( dy );
1383 rect.left = pt_1->x - pdev->pen_width / 2;
1384 rect.right = rect.left + pdev->pen_width;
1385 if ((sq_cap_1 && dy > 0) || (sq_cap_2 && dy < 0)) rect.top -= pdev->pen_width / 2;
1386 if ((sq_cap_2 && dy > 0) || (sq_cap_1 && dy < 0)) rect.bottom += pdev->pen_width / 2;
1387 segment = CreateRectRgnIndirect( &rect );
1388 if (dy > 0)
1390 face_1.start.x = face_2.end.x = rect.left;
1391 face_1.start.y = face_1.end.y = rect.top;
1392 face_1.end.x = face_2.start.x = rect.right;
1393 face_2.start.y = face_2.end.y = rect.bottom - 1;
1395 else
1397 face_1.start.x = face_2.end.x = rect.right;
1398 face_1.start.y = face_1.end.y = rect.bottom;
1399 face_1.end.x = face_2.start.x = rect.left;
1400 face_2.start.y = face_2.end.y = rect.top + 1;
1403 else
1405 double len = hypot( dx, dy );
1406 double width_x, width_y;
1407 POINT seg_pts[4];
1408 POINT wide_half, narrow_half;
1410 width_x = pdev->pen_width * abs( dy ) / len;
1411 width_y = pdev->pen_width * abs( dx ) / len;
1413 narrow_half.x = round( width_x / 2 );
1414 narrow_half.y = round( width_y / 2 );
1415 wide_half.x = round( (width_x + 1) / 2 );
1416 wide_half.y = round( (width_y + 1) / 2 );
1418 if (dx < 0)
1420 wide_half.y = -wide_half.y;
1421 narrow_half.y = -narrow_half.y;
1424 if (dy < 0)
1426 POINT tmp = narrow_half; narrow_half = wide_half; wide_half = tmp;
1427 wide_half.x = -wide_half.x;
1428 narrow_half.x = -narrow_half.x;
1431 seg_pts[0].x = pt_1->x - narrow_half.x;
1432 seg_pts[0].y = pt_1->y + narrow_half.y;
1433 seg_pts[1].x = pt_1->x + wide_half.x;
1434 seg_pts[1].y = pt_1->y - wide_half.y;
1435 seg_pts[2].x = pt_2->x + wide_half.x;
1436 seg_pts[2].y = pt_2->y - wide_half.y;
1437 seg_pts[3].x = pt_2->x - narrow_half.x;
1438 seg_pts[3].y = pt_2->y + narrow_half.y;
1440 if (sq_cap_1)
1442 seg_pts[0].x -= narrow_half.y;
1443 seg_pts[1].x -= narrow_half.y;
1444 seg_pts[0].y -= narrow_half.x;
1445 seg_pts[1].y -= narrow_half.x;
1448 if (sq_cap_2)
1450 seg_pts[2].x += wide_half.y;
1451 seg_pts[3].x += wide_half.y;
1452 seg_pts[2].y += wide_half.x;
1453 seg_pts[3].y += wide_half.x;
1456 segment = CreatePolygonRgn( seg_pts, 4, ALTERNATE );
1458 face_1.start = seg_pts[0];
1459 face_1.end = seg_pts[1];
1460 face_2.start = seg_pts[2];
1461 face_2.end = seg_pts[3];
1464 CombineRgn( total, total, segment, RGN_OR );
1465 DeleteObject( segment );
1467 if (need_cap_1) add_cap( pdev, total, pt_1 );
1468 if (need_cap_2) add_cap( pdev, total, pt_2 );
1470 face_1.dx = face_2.dx = dx;
1471 face_1.dy = face_2.dy = dy;
1473 if (i == 0) first_face = face_1;
1474 else add_join( pdev, total, pt_1, &prev_face, &face_1 );
1476 if (i == num - 1 && close)
1477 add_join( pdev, total, pt_2, &face_2, &first_face );
1479 prev_face = face_2;
1481 return TRUE;
1484 static BOOL wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1486 if (region) return get_wide_lines_region( pdev, num, pts, close, region );
1488 if (!(region = CreateRectRgn( 0, 0, 0, 0 ))) return FALSE;
1489 get_wide_lines_region( pdev, num, pts, close, region );
1490 if (pdev->clip) CombineRgn( region, region, pdev->clip, RGN_AND );
1491 pen_rect( pdev, NULL, region, GetROP2( pdev->dev.hdc ) );
1492 DeleteObject( region );
1493 return TRUE;
1496 static const dash_pattern dash_patterns_cosmetic[4] =
1498 {2, {18, 6}, 24}, /* PS_DASH */
1499 {2, {3, 3}, 6}, /* PS_DOT */
1500 {4, {9, 6, 3, 6}, 24}, /* PS_DASHDOT */
1501 {6, {9, 3, 3, 3, 3, 3}, 24} /* PS_DASHDOTDOT */
1504 static const dash_pattern dash_patterns_geometric[4] =
1506 {2, {3, 1}, 4}, /* PS_DASH */
1507 {2, {1, 1}, 2}, /* PS_DOT */
1508 {4, {3, 1, 1, 1}, 6}, /* PS_DASHDOT */
1509 {6, {3, 1, 1, 1, 1, 1}, 8} /* PS_DASHDOTDOT */
1512 static inline void set_dash_pattern( dash_pattern *pattern, DWORD count, DWORD *dashes )
1514 DWORD i;
1516 pattern->count = count;
1517 pattern->total_len = 0;
1518 memcpy( pattern->dashes, dashes, count * sizeof(DWORD) );
1519 for (i = 0; i < count; i++) pattern->total_len += dashes[i];
1520 if (pattern->count % 2) pattern->total_len *= 2;
1523 static inline void scale_dash_pattern( dash_pattern *pattern, DWORD scale )
1525 DWORD i;
1526 for (i = 0; i < pattern->count; i++) pattern->dashes[i] *= scale;
1527 pattern->total_len *= scale;
1530 static inline int get_pen_device_width( dibdrv_physdev *pdev, int width )
1532 POINT pts[2];
1534 if (!width) return 1;
1535 pts[0].x = pts[0].y = pts[1].y = 0;
1536 pts[1].x = width;
1537 LPtoDP( pdev->dev.hdc, pts, 2 );
1538 width = abs( pts[1].x - pts[0].x );
1539 return max( width, 1 );
1542 /***********************************************************************
1543 * dibdrv_SelectPen
1545 HPEN dibdrv_SelectPen( PHYSDEV dev, HPEN hpen )
1547 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectPen );
1548 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1549 LOGPEN logpen;
1550 EXTLOGPEN *elp = NULL;
1552 TRACE("(%p, %p)\n", dev, hpen);
1554 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
1556 /* must be an extended pen */
1557 INT size = GetObjectW( hpen, 0, NULL );
1559 if (!size) return 0;
1561 elp = HeapAlloc( GetProcessHeap(), 0, size );
1563 GetObjectW( hpen, size, elp );
1564 /* FIXME: add support for user style pens */
1565 logpen.lopnStyle = elp->elpPenStyle;
1566 logpen.lopnWidth.x = elp->elpWidth;
1567 logpen.lopnColor = elp->elpColor;
1568 /* cosmetic ext pens are always 1-pixel wide */
1569 if (!(logpen.lopnStyle & PS_GEOMETRIC)) logpen.lopnWidth.x = 0;
1572 pdev->pen_join = logpen.lopnStyle & PS_JOIN_MASK;
1573 pdev->pen_endcap = logpen.lopnStyle & PS_ENDCAP_MASK;
1574 pdev->pen_width = get_pen_device_width( pdev, logpen.lopnWidth.x );
1576 if (hpen == GetStockObject( DC_PEN ))
1577 logpen.lopnColor = GetDCPenColor( dev->hdc );
1579 pdev->pen_colorref = logpen.lopnColor;
1580 set_dash_pattern( &pdev->pen_pattern, 0, NULL );
1582 pdev->defer |= DEFER_PEN;
1584 pdev->pen_style = logpen.lopnStyle & PS_STYLE_MASK;
1586 switch (pdev->pen_style)
1588 case PS_DASH:
1589 case PS_DOT:
1590 case PS_DASHDOT:
1591 case PS_DASHDOTDOT:
1592 if (logpen.lopnStyle & PS_GEOMETRIC)
1594 if (pdev->pen_width > 1) break; /* not supported yet */
1595 pdev->pen_lines = dashed_pen_lines;
1596 pdev->pen_pattern = dash_patterns_geometric[pdev->pen_style - 1];
1597 pdev->defer &= ~DEFER_PEN;
1598 break;
1600 if (pdev->pen_width == 1) /* wide cosmetic pens are not dashed */
1602 pdev->pen_lines = dashed_pen_lines;
1603 pdev->pen_pattern = dash_patterns_cosmetic[pdev->pen_style - 1];
1604 pdev->defer &= ~DEFER_PEN;
1605 break;
1607 /* fall through */
1608 case PS_SOLID:
1609 case PS_INSIDEFRAME:
1610 if(pdev->pen_width == 1)
1611 pdev->pen_lines = solid_pen_lines;
1612 else
1613 pdev->pen_lines = wide_pen_lines;
1614 pdev->defer &= ~DEFER_PEN;
1615 break;
1617 case PS_NULL:
1618 pdev->pen_width = 0;
1619 pdev->pen_lines = null_pen_lines;
1620 pdev->defer &= ~DEFER_PEN;
1621 break;
1623 case PS_ALTERNATE:
1624 pdev->pen_lines = dashed_pen_lines;
1625 pdev->pen_pattern = dash_patterns_geometric[PS_DOT - 1];
1626 pdev->defer &= ~DEFER_PEN;
1627 break;
1629 case PS_USERSTYLE:
1630 if (pdev->pen_width > 1) break; /* not supported yet */
1631 pdev->pen_lines = dashed_pen_lines;
1632 set_dash_pattern( &pdev->pen_pattern, elp->elpNumEntries, elp->elpStyleEntry );
1633 if (!(logpen.lopnStyle & PS_GEOMETRIC)) scale_dash_pattern( &pdev->pen_pattern, 3 );
1634 pdev->defer &= ~DEFER_PEN;
1635 break;
1637 default:
1638 break;
1641 pdev->pen_uses_region = (logpen.lopnStyle & PS_GEOMETRIC || pdev->pen_width > 1);
1642 pdev->pen_is_ext = (elp != NULL);
1643 HeapFree( GetProcessHeap(), 0, elp );
1645 return next->funcs->pSelectPen( next, hpen );
1648 /***********************************************************************
1649 * dibdrv_SetDCPenColor
1651 COLORREF dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1653 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCPenColor );
1654 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1656 if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
1657 pdev->pen_colorref = color;
1659 return next->funcs->pSetDCPenColor( next, color );
1662 /**********************************************************************
1663 * solid_brush
1665 * Fill a number of rectangles with the solid brush
1667 static BOOL solid_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, INT rop)
1669 rop_mask brush_color;
1670 DWORD color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
1672 calc_rop_masks( rop, color, &brush_color );
1673 dib->funcs->solid_rects( dib, num, rects, brush_color.and, brush_color.xor );
1674 return TRUE;
1677 static void free_pattern_brush_bits( dibdrv_physdev *pdev )
1679 HeapFree(GetProcessHeap(), 0, pdev->brush_and_bits);
1680 HeapFree(GetProcessHeap(), 0, pdev->brush_xor_bits);
1681 pdev->brush_and_bits = NULL;
1682 pdev->brush_xor_bits = NULL;
1685 void free_pattern_brush( dibdrv_physdev *pdev )
1687 free_pattern_brush_bits( pdev );
1688 free_dib_info( &pdev->brush_dib );
1691 static BOOL create_pattern_brush_bits(dibdrv_physdev *pdev)
1693 DWORD size = pdev->brush_dib.height * abs(pdev->brush_dib.stride);
1694 DWORD *brush_bits = pdev->brush_dib.bits.ptr;
1695 DWORD *and_bits, *xor_bits;
1697 assert(pdev->brush_and_bits == NULL);
1698 assert(pdev->brush_xor_bits == NULL);
1700 assert(pdev->brush_dib.stride > 0);
1702 and_bits = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1703 xor_bits = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1705 if(!and_bits || !xor_bits)
1707 ERR("Failed to create pattern brush bits\n");
1708 free_pattern_brush_bits( pdev );
1709 return FALSE;
1712 while(size)
1714 calc_and_xor_masks(pdev->brush_rop, *brush_bits++, and_bits++, xor_bits++);
1715 size -= 4;
1718 return TRUE;
1721 static const DWORD hatches[6][8] =
1723 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1724 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
1725 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL */
1726 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL */
1727 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
1728 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
1731 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev, BOOL *needs_reselect)
1733 dib_info hatch;
1734 rop_mask fg_mask, bg_mask;
1735 rop_mask_bits mask_bits;
1736 DWORD size;
1737 BOOL ret;
1739 assert(pdev->brush_and_bits == NULL);
1740 assert(pdev->brush_xor_bits == NULL);
1742 /* Just initialise brush_dib with the color / sizing info. We don't
1743 need the bits as we'll calculate the rop masks straight from
1744 the hatch patterns. */
1746 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1747 pdev->brush_dib.width = 8;
1748 pdev->brush_dib.height = 8;
1749 pdev->brush_dib.stride = get_dib_stride( pdev->brush_dib.width, pdev->brush_dib.bit_count );
1751 size = pdev->brush_dib.height * pdev->brush_dib.stride;
1753 mask_bits.and = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1754 mask_bits.xor = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1756 if(!mask_bits.and || !mask_bits.xor)
1758 ERR("Failed to create pattern brush bits\n");
1759 free_pattern_brush_bits( pdev );
1760 return FALSE;
1763 hatch.bit_count = 1;
1764 hatch.height = hatch.width = 8;
1765 hatch.stride = 4;
1766 hatch.bits.ptr = (void *) hatches[pdev->brush_hatch];
1767 hatch.bits.free = hatch.bits.param = NULL;
1768 hatch.bits.is_copy = FALSE;
1770 get_color_masks( pdev, pdev->brush_rop, pdev->brush_colorref, GetBkMode(pdev->dev.hdc),
1771 &fg_mask, &bg_mask );
1773 if (pdev->brush_colorref & (1 << 24)) /* PALETTEINDEX */
1774 *needs_reselect = TRUE;
1775 if (GetBkMode(pdev->dev.hdc) != TRANSPARENT && (GetBkColor(pdev->dev.hdc) & (1 << 24)))
1776 *needs_reselect = TRUE;
1778 ret = pdev->brush_dib.funcs->create_rop_masks( &pdev->brush_dib, &hatch, &fg_mask, &bg_mask, &mask_bits );
1779 if(!ret) free_pattern_brush_bits( pdev );
1781 return ret;
1784 static BOOL matching_pattern_format( dib_info *dib, dib_info *pattern )
1786 if (dib->bit_count != pattern->bit_count) return FALSE;
1787 if (dib->stride != pattern->stride) return FALSE;
1789 switch (dib->bit_count)
1791 case 1:
1792 case 4:
1793 case 8:
1794 if (dib->color_table_size != pattern->color_table_size) return FALSE;
1795 return !memcmp( dib->color_table, pattern->color_table, dib->color_table_size * sizeof(RGBQUAD) );
1796 case 16:
1797 case 32:
1798 return (dib->red_mask == pattern->red_mask &&
1799 dib->green_mask == pattern->green_mask &&
1800 dib->blue_mask == pattern->blue_mask);
1802 return TRUE;
1805 static BOOL select_pattern_brush( dibdrv_physdev *pdev, BOOL *needs_reselect )
1807 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1808 BITMAPINFO *info = (BITMAPINFO *)buffer;
1809 RGBQUAD color_table[2];
1810 RECT rect;
1811 dib_info pattern;
1813 if (!pdev->brush_pattern_info)
1815 BITMAPOBJ *bmp = GDI_GetObjPtr( pdev->brush_pattern_bitmap, OBJ_BITMAP );
1816 BOOL ret;
1818 if (!bmp) return FALSE;
1819 ret = init_dib_info_from_bitmapobj( &pattern, bmp, 0 );
1820 GDI_ReleaseObj( pdev->brush_pattern_bitmap );
1821 if (!ret) return FALSE;
1823 else if (pdev->brush_pattern_info->bmiHeader.biClrUsed && pdev->brush_pattern_usage == DIB_PAL_COLORS)
1825 copy_bitmapinfo( info, pdev->brush_pattern_info );
1826 fill_color_table_from_pal_colors( info, pdev->dev.hdc );
1827 init_dib_info_from_bitmapinfo( &pattern, info, pdev->brush_pattern_bits, 0 );
1828 *needs_reselect = TRUE;
1830 else
1832 init_dib_info_from_bitmapinfo( &pattern, pdev->brush_pattern_info, pdev->brush_pattern_bits, 0 );
1835 if (pattern.bit_count == 1 && !pattern.color_table)
1837 /* monochrome DDB pattern uses DC colors */
1838 DWORD pixel;
1839 BOOL got_pixel;
1840 COLORREF color;
1842 color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetTextColor( pdev->dev.hdc ),
1843 &got_pixel, &pixel );
1844 color_table[0].rgbRed = GetRValue( color );
1845 color_table[0].rgbGreen = GetGValue( color );
1846 color_table[0].rgbBlue = GetBValue( color );
1847 color_table[0].rgbReserved = 0;
1849 color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetBkColor( pdev->dev.hdc ),
1850 &got_pixel, &pixel );
1851 color_table[1].rgbRed = GetRValue( color );
1852 color_table[1].rgbGreen = GetGValue( color );
1853 color_table[1].rgbBlue = GetBValue( color );
1854 color_table[1].rgbReserved = 0;
1856 pattern.color_table = color_table;
1857 pattern.color_table_size = 2;
1858 *needs_reselect = TRUE;
1861 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1863 pdev->brush_dib.height = pattern.height;
1864 pdev->brush_dib.width = pattern.width;
1865 pdev->brush_dib.stride = get_dib_stride( pdev->brush_dib.width, pdev->brush_dib.bit_count );
1867 if (matching_pattern_format( &pdev->brush_dib, &pattern ))
1869 pdev->brush_dib.bits.ptr = pattern.bits.ptr;
1870 pdev->brush_dib.bits.is_copy = FALSE;
1871 pdev->brush_dib.bits.free = NULL;
1873 else
1875 pdev->brush_dib.bits.ptr = HeapAlloc( GetProcessHeap(), 0,
1876 pdev->brush_dib.height * pdev->brush_dib.stride );
1877 pdev->brush_dib.bits.is_copy = TRUE;
1878 pdev->brush_dib.bits.free = free_heap_bits;
1880 rect.left = rect.top = 0;
1881 rect.right = pattern.width;
1882 rect.bottom = pattern.height;
1884 pdev->brush_dib.funcs->convert_to(&pdev->brush_dib, &pattern, &rect);
1886 return TRUE;
1889 /**********************************************************************
1890 * pattern_brush
1892 * Fill a number of rectangles with the pattern brush
1893 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1895 static BOOL pattern_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, INT rop)
1897 POINT origin;
1898 BOOL needs_reselect = FALSE;
1900 if (rop != pdev->brush_rop)
1902 free_pattern_brush_bits( pdev );
1903 pdev->brush_rop = rop;
1906 if(pdev->brush_and_bits == NULL)
1908 switch(pdev->brush_style)
1910 case BS_DIBPATTERN:
1911 if (!pdev->brush_dib.bits.ptr && !select_pattern_brush( pdev, &needs_reselect ))
1912 return FALSE;
1913 if(!create_pattern_brush_bits(pdev))
1914 return FALSE;
1915 break;
1917 case BS_HATCHED:
1918 if(!create_hatch_brush_bits(pdev, &needs_reselect))
1919 return FALSE;
1920 break;
1922 default:
1923 ERR("Unexpected brush style %d\n", pdev->brush_style);
1924 return FALSE;
1928 GetBrushOrgEx(pdev->dev.hdc, &origin);
1930 dib->funcs->pattern_rects( dib, num, rects, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1932 if (needs_reselect) free_pattern_brush( pdev );
1933 return TRUE;
1936 static BOOL null_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, INT rop)
1938 return TRUE;
1941 /***********************************************************************
1942 * dibdrv_SelectBrush
1944 HBRUSH dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush, HBITMAP bitmap,
1945 const BITMAPINFO *info, void *bits, UINT usage )
1947 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBrush );
1948 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1949 LOGBRUSH logbrush;
1951 TRACE("(%p, %p)\n", dev, hbrush);
1953 free_pattern_brush( pdev );
1955 if (bitmap || info) /* pattern brush */
1957 pdev->brush_rects = pattern_brush;
1958 pdev->brush_style = BS_DIBPATTERN;
1959 pdev->brush_pattern_info = info;
1960 pdev->brush_pattern_bits = bits;
1961 pdev->brush_pattern_usage = usage;
1962 pdev->brush_pattern_bitmap = bitmap;
1963 /* brush is actually selected only when it's used */
1965 return next->funcs->pSelectBrush( next, hbrush, bitmap, info, bits, usage );
1968 GetObjectW( hbrush, sizeof(logbrush), &logbrush );
1970 if (hbrush == GetStockObject( DC_BRUSH ))
1971 logbrush.lbColor = GetDCBrushColor( dev->hdc );
1973 pdev->brush_style = logbrush.lbStyle;
1975 switch(logbrush.lbStyle)
1977 case BS_SOLID:
1978 pdev->brush_colorref = logbrush.lbColor;
1979 pdev->brush_rects = solid_brush;
1980 break;
1982 case BS_NULL:
1983 pdev->brush_rects = null_brush;
1984 break;
1986 case BS_HATCHED:
1987 if(logbrush.lbHatch > HS_DIAGCROSS) return 0;
1988 pdev->brush_hatch = logbrush.lbHatch;
1989 pdev->brush_colorref = logbrush.lbColor;
1990 pdev->brush_rects = pattern_brush;
1991 break;
1993 default:
1994 return 0;
1997 return next->funcs->pSelectBrush( next, hbrush, bitmap, info, bits, usage );
2000 /***********************************************************************
2001 * dibdrv_SetDCBrushColor
2003 COLORREF dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
2005 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCBrushColor );
2006 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2008 if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
2009 pdev->brush_colorref = color;
2011 return next->funcs->pSetDCBrushColor( next, color );
2014 BOOL brush_rect(dibdrv_physdev *pdev, const RECT *rect, HRGN clip, INT rop)
2016 struct clipped_rects clipped_rects;
2017 BOOL ret;
2019 if (!get_clipped_rects( &pdev->dib, rect, clip, &clipped_rects )) return TRUE;
2020 ret = pdev->brush_rects( pdev, &pdev->dib, clipped_rects.count, clipped_rects.rects, rop );
2021 free_clipped_rects( &clipped_rects );
2022 return ret;
2025 BOOL pen_rect(dibdrv_physdev *pdev, const RECT *rect, HRGN clip, INT rop)
2027 struct clipped_rects clipped_rects;
2028 rop_mask color;
2029 DWORD pen_color = get_pixel_color( pdev, pdev->pen_colorref, TRUE );
2031 if (!get_clipped_rects( &pdev->dib, rect, clip, &clipped_rects )) return TRUE;
2033 calc_rop_masks( rop, pen_color, &color );
2034 pdev->dib.funcs->solid_rects( &pdev->dib, clipped_rects.count, clipped_rects.rects,
2035 color.and, color.xor );
2036 free_clipped_rects( &clipped_rects );
2037 return TRUE;