gdi32: Get rid of the pen deferring support, all pen styles are implemented now.
[wine/multimedia.git] / dlls / gdi32 / dibdrv / objects.c
blobe18d715866ef161ddb65042e0b7573874e70b90b
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_brush.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;
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;
795 while (skip);
798 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
800 RECT rect;
801 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
803 skip_dash(pdev, 1);
804 rect.left = x;
805 rect.right = x + 1;
806 rect.top = y;
807 rect.bottom = y + 1;
808 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
809 return;
812 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
814 struct clipped_rects clipped_rects;
815 int i, dash_len;
816 RECT rect;
817 const dash_pos start_pos = pdev->dash_pos;
819 if(start->y == end->y) /* hline */
821 BOOL l_to_r;
822 INT left, right, cur_x;
824 rect.top = start->y;
825 rect.bottom = start->y + 1;
827 if(start->x <= end->x)
829 left = start->x;
830 right = end->x - 1;
831 l_to_r = TRUE;
833 else
835 left = end->x + 1;
836 right = start->x;
837 l_to_r = FALSE;
840 rect.left = min( start->x, end->x );
841 rect.right = max( start->x, end->x ) + 1;
842 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
843 for (i = 0; i < clipped_rects.count; i++)
845 if(clipped_rects.rects[i].right > left && clipped_rects.rects[i].left <= right)
847 int clipped_left = max(clipped_rects.rects[i].left, left);
848 int clipped_right = min(clipped_rects.rects[i].right - 1, right);
850 pdev->dash_pos = start_pos;
852 if(l_to_r)
854 cur_x = clipped_left;
855 if(cur_x != left)
856 skip_dash(pdev, clipped_left - left);
858 while(cur_x <= clipped_right)
860 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
861 dash_len = pdev->dash_pos.left_in_dash;
862 if(cur_x + dash_len > clipped_right + 1)
863 dash_len = clipped_right - cur_x + 1;
864 rect.left = cur_x;
865 rect.right = cur_x + dash_len;
867 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
868 cur_x += dash_len;
869 skip_dash(pdev, dash_len);
872 else
874 cur_x = clipped_right;
875 if(cur_x != right)
876 skip_dash(pdev, right - clipped_right);
878 while(cur_x >= clipped_left)
880 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
881 dash_len = pdev->dash_pos.left_in_dash;
882 if(cur_x - dash_len < clipped_left - 1)
883 dash_len = cur_x - clipped_left + 1;
884 rect.left = cur_x - dash_len + 1;
885 rect.right = cur_x + 1;
887 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
888 cur_x -= dash_len;
889 skip_dash(pdev, dash_len);
894 pdev->dash_pos = start_pos;
895 skip_dash(pdev, right - left + 1);
897 else if(start->x == end->x) /* vline */
899 BOOL t_to_b;
900 INT top, bottom, cur_y;
902 rect.left = start->x;
903 rect.right = start->x + 1;
905 if(start->y <= end->y)
907 top = start->y;
908 bottom = end->y - 1;
909 t_to_b = TRUE;
911 else
913 top = end->y + 1;
914 bottom = start->y;
915 t_to_b = FALSE;
918 rect.top = min( start->y, end->y );
919 rect.bottom = max( start->y, end->y ) + 1;
920 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
921 for (i = 0; i < clipped_rects.count; i++)
923 if(clipped_rects.rects[i].right > start->x && clipped_rects.rects[i].left <= start->x)
925 int clipped_top = max(clipped_rects.rects[i].top, top);
926 int clipped_bottom = min(clipped_rects.rects[i].bottom - 1, bottom);
928 pdev->dash_pos = start_pos;
930 if(t_to_b)
932 cur_y = clipped_top;
933 if(cur_y != top)
934 skip_dash(pdev, clipped_top - top);
936 while(cur_y <= clipped_bottom)
938 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
939 dash_len = pdev->dash_pos.left_in_dash;
940 if(cur_y + dash_len > clipped_bottom + 1)
941 dash_len = clipped_bottom - cur_y + 1;
942 rect.top = cur_y;
943 rect.bottom = cur_y + dash_len;
945 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
946 cur_y += dash_len;
947 skip_dash(pdev, dash_len);
950 else
952 cur_y = clipped_bottom;
953 if(cur_y != bottom)
954 skip_dash(pdev, bottom - clipped_bottom);
956 while(cur_y >= clipped_top)
958 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
959 dash_len = pdev->dash_pos.left_in_dash;
960 if(cur_y - dash_len < clipped_top - 1)
961 dash_len = cur_y - clipped_top + 1;
962 rect.top = cur_y - dash_len + 1;
963 rect.bottom = cur_y + 1;
965 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
966 cur_y -= dash_len;
967 skip_dash(pdev, dash_len);
972 pdev->dash_pos = start_pos;
973 skip_dash(pdev, bottom - top + 1);
975 else
977 bres_params clip_params;
978 struct line_params line_params;
979 INT dx = end->x - start->x, dy = end->y - start->y;
980 INT abs_dx = abs(dx), abs_dy = abs(dy);
982 clip_params.dx = abs_dx;
983 clip_params.dy = abs_dy;
984 clip_params.octant = get_octant_mask(dx, dy);
985 clip_params.bias = get_bias( clip_params.octant );
987 line_params.bias = clip_params.bias;
988 line_params.x_major = is_xmajor( clip_params.octant );
989 line_params.x_inc = is_x_increasing( clip_params.octant ) ? 1 : -1;
990 line_params.y_inc = is_y_increasing( clip_params.octant ) ? 1 : -1;
992 if (line_params.x_major)
994 line_params.err_add_1 = 2 * abs_dy - 2 * abs_dx;
995 line_params.err_add_2 = 2 * abs_dy;
997 else
999 line_params.err_add_1 = 2 * abs_dx - 2 * abs_dy;
1000 line_params.err_add_2 = 2 * abs_dx;
1003 rect.left = min( start->x, end->x );
1004 rect.top = min( start->y, end->y );
1005 rect.right = max( start->x, end->x ) + 1;
1006 rect.bottom = max( start->y, end->y ) + 1;
1007 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
1008 for (i = 0; i < clipped_rects.count; i++)
1010 POINT clipped_start, clipped_end;
1011 int clip_status;
1012 clip_status = clip_line(start, end, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
1014 if(clip_status)
1016 int m = abs(clipped_start.x - start->x);
1017 int n = abs(clipped_start.y - start->y);
1019 pdev->dash_pos = start_pos;
1021 if (line_params.x_major)
1023 line_params.err_start = 2 * abs_dy - abs_dx + m * 2 * abs_dy - n * 2 * abs_dx;
1024 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
1025 skip_dash(pdev, m);
1027 else
1029 line_params.err_start = 2 * abs_dx - abs_dy + n * 2 * abs_dx - m * 2 * abs_dy;
1030 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
1031 skip_dash(pdev, n);
1033 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
1035 bres_line_with_bias( &clipped_start, &line_params, dashed_pen_line_callback, pdev );
1037 if(clip_status == 2) break; /* completely unclipped, so we can finish */
1040 pdev->dash_pos = start_pos;
1041 if(line_params.x_major)
1042 skip_dash(pdev, abs_dx);
1043 else
1044 skip_dash(pdev, abs_dy);
1047 free_clipped_rects( &clipped_rects );
1048 return TRUE;
1051 static BOOL dashed_pen_line_region(dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region)
1053 int i, dash_len;
1054 RECT rect;
1056 rect.left = start->x;
1057 rect.top = start->y;
1058 rect.right = start->x + 1;
1059 rect.bottom = start->y + 1;
1061 if (start->y == end->y) /* hline */
1063 if (start->x <= end->x)
1065 for (i = start->x; i < end->x; i += dash_len)
1067 dash_len = min( pdev->dash_pos.left_in_dash, end->x - i );
1068 if (pdev->dash_pos.mark)
1070 rect.left = i;
1071 rect.right = i + dash_len;
1072 add_rect_to_region( region, &rect );
1074 skip_dash(pdev, dash_len);
1077 else
1079 for (i = start->x; i > end->x; i -= dash_len)
1081 dash_len = min( pdev->dash_pos.left_in_dash, i - end->x );
1082 if (pdev->dash_pos.mark)
1084 rect.left = i - dash_len + 1;
1085 rect.right = i + 1;
1086 add_rect_to_region( region, &rect );
1088 skip_dash(pdev, dash_len);
1092 else if (start->x == end->x) /* vline */
1094 if (start->y <= end->y)
1096 for (i = start->y; i < end->y; i += dash_len)
1098 dash_len = min( pdev->dash_pos.left_in_dash, end->y - i );
1099 if (pdev->dash_pos.mark)
1101 rect.top = i;
1102 rect.bottom = i + dash_len;
1103 add_rect_to_region( region, &rect );
1105 skip_dash(pdev, dash_len);
1108 else
1110 for (i = start->y; i > end->y; i -= dash_len)
1112 dash_len = min( pdev->dash_pos.left_in_dash, i - end->y );
1113 if (pdev->dash_pos.mark)
1115 rect.top = i - dash_len + 1;
1116 rect.bottom = i + 1;
1117 add_rect_to_region( region, &rect );
1119 skip_dash(pdev, dash_len);
1123 else
1125 INT dx = end->x - start->x, dy = end->y - start->y;
1126 INT abs_dx = abs(dx), abs_dy = abs(dy);
1127 DWORD octant = get_octant_mask(dx, dy);
1128 INT bias = get_bias( octant );
1129 int x_inc = is_x_increasing( octant ) ? 1 : -1;
1130 int y_inc = is_y_increasing( octant ) ? 1 : -1;
1132 if (is_xmajor( octant ))
1134 int err_add_1 = 2 * abs_dy - 2 * abs_dx;
1135 int err_add_2 = 2 * abs_dy;
1136 int err = 2 * abs_dy - abs_dx;
1138 while (abs_dx--)
1140 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1141 skip_dash(pdev, 1);
1142 rect.left += x_inc;
1143 rect.right += x_inc;
1144 if (err + bias > 0)
1146 rect.top += y_inc;
1147 rect.bottom += y_inc;
1148 err += err_add_1;
1150 else err += err_add_2;
1154 else
1156 int err_add_1 = 2 * abs_dx - 2 * abs_dy;
1157 int err_add_2 = 2 * abs_dx;
1158 int err = 2 * abs_dx - abs_dy;
1160 while (abs_dy--)
1162 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1163 skip_dash(pdev, 1);
1164 rect.top += y_inc;
1165 rect.bottom += y_inc;
1166 if (err + bias > 0)
1168 rect.left += x_inc;
1169 rect.right += x_inc;
1170 err += err_add_1;
1172 else err += err_add_2;
1176 return TRUE;
1179 static BOOL dashed_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1181 int i;
1183 assert( num >= 2 );
1185 if (region)
1187 for (i = 0; i < num - 1; i++)
1188 if (!dashed_pen_line_region( pdev, pts + i, pts + i + 1, region ))
1189 return FALSE;
1190 if (close) return dashed_pen_line_region( pdev, pts + num - 1, pts, region );
1192 else
1194 get_color_masks( pdev, GetROP2(pdev->dev.hdc), pdev->pen_brush.colorref,
1195 pdev->pen_is_ext ? TRANSPARENT : GetBkMode(pdev->dev.hdc),
1196 &pdev->dash_masks[1], &pdev->dash_masks[0] );
1198 for (i = 0; i < num - 1; i++)
1199 if (!dashed_pen_line( pdev, pts + i, pts + i + 1 ))
1200 return FALSE;
1201 if (close) return dashed_pen_line( pdev, pts + num - 1, pts );
1203 return TRUE;
1206 static BOOL null_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1208 return TRUE;
1211 struct face
1213 POINT start, end;
1214 int dx, dy;
1217 static void add_cap( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt )
1219 switch (pdev->pen_endcap)
1221 default: FIXME( "Unknown end cap %x\n", pdev->pen_endcap );
1222 /* fall through */
1223 case PS_ENDCAP_ROUND:
1224 OffsetRgn( round_cap, pt->x, pt->y );
1225 CombineRgn( region, region, round_cap, RGN_OR );
1226 OffsetRgn( round_cap, -pt->x, -pt->y );
1227 return;
1229 case PS_ENDCAP_SQUARE: /* already been handled */
1230 case PS_ENDCAP_FLAT:
1231 return;
1235 #define round( f ) (((f) > 0) ? (f) + 0.5 : (f) - 0.5)
1237 /*******************************************************************************
1238 * create_miter_region
1240 * We need to calculate the intersection of two lines. We know a point
1241 * on each line (a face start and the other face end point) and
1242 * the direction vector of each line eg. (dx_1, dy_1).
1244 * (x, y) = (x_1, y_1) + u * (dx_1, dy_1) = (x_2, y_2) + v * (dx_2, dy_2)
1245 * solving (eg using Cramer's rule) gives:
1246 * u = ((x_2 - x_1) dy_2 - (y_2 - y_1) dx_2) / det
1247 * with det = dx_1 dy_2 - dx_2 dy_1
1248 * substituting back in and simplifying gives
1249 * (x, y) = a (dx_1, dy_1) - b (dx_2, dy_2)
1250 * with a = (x_2 dy_2 - y_2 dx_2) / det
1251 * and b = (x_1 dy_1 - y_1 dx_1) / det
1253 static HRGN create_miter_region( dibdrv_physdev *pdev, const POINT *pt,
1254 const struct face *face_1, const struct face *face_2 )
1256 int det = face_1->dx * face_2->dy - face_1->dy * face_2->dx;
1257 POINT pt_1, pt_2, pts[5];
1258 double a, b, x, y;
1259 FLOAT limit;
1261 if (det == 0) return 0;
1263 if (det < 0)
1265 const struct face *tmp = face_1;
1266 face_1 = face_2;
1267 face_2 = tmp;
1268 det = -det;
1271 pt_1 = face_1->start;
1272 pt_2 = face_2->end;
1274 a = (double)((pt_2.x * face_2->dy - pt_2.y * face_2->dx)) / det;
1275 b = (double)((pt_1.x * face_1->dy - pt_1.y * face_1->dx)) / det;
1277 x = a * face_1->dx - b * face_2->dx;
1278 y = a * face_1->dy - b * face_2->dy;
1280 GetMiterLimit( pdev->dev.hdc, &limit );
1282 if (((x - pt->x) * (x - pt->x) + (y - pt->y) * (y - pt->y)) * 4 > limit * limit * pdev->pen_width * pdev->pen_width)
1283 return 0;
1285 pts[0] = face_2->start;
1286 pts[1] = face_1->start;
1287 pts[2].x = round( x );
1288 pts[2].y = round( y );
1289 pts[3] = face_2->end;
1290 pts[4] = face_1->end;
1292 return CreatePolygonRgn( pts, 5, ALTERNATE );
1295 static void add_join( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt,
1296 const struct face *face_1, const struct face *face_2 )
1298 HRGN join;
1299 POINT pts[4];
1301 switch (pdev->pen_join)
1303 default: FIXME( "Unknown line join %x\n", pdev->pen_join );
1304 /* fall through */
1305 case PS_JOIN_ROUND:
1306 OffsetRgn( round_cap, pt->x, pt->y );
1307 CombineRgn( region, region, round_cap, RGN_OR );
1308 OffsetRgn( round_cap, -pt->x, -pt->y );
1309 return;
1311 case PS_JOIN_MITER:
1312 join = create_miter_region( pdev, pt, face_1, face_2 );
1313 if (join) break;
1314 /* fall through */
1315 case PS_JOIN_BEVEL:
1316 pts[0] = face_1->start;
1317 pts[1] = face_2->end;
1318 pts[2] = face_1->end;
1319 pts[3] = face_2->start;
1320 join = CreatePolygonRgn( pts, 4, ALTERNATE );
1321 break;
1324 CombineRgn( region, region, join, RGN_OR );
1325 DeleteObject( join );
1326 return;
1329 static void wide_line_segment( dibdrv_physdev *pdev, HRGN total,
1330 const POINT *pt_1, const POINT *pt_2, int dx, int dy,
1331 BOOL need_cap_1, BOOL need_cap_2, struct face *face_1, struct face *face_2 )
1333 RECT rect;
1334 BOOL sq_cap_1 = need_cap_1 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1335 BOOL sq_cap_2 = need_cap_2 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1337 if (dx == 0 && dy == 0) return;
1339 if (dy == 0)
1341 rect.left = min( pt_1->x, pt_2->x );
1342 rect.right = max( pt_1->x, pt_2->x );
1343 rect.top = pt_1->y - pdev->pen_width / 2;
1344 rect.bottom = rect.top + pdev->pen_width;
1345 if ((sq_cap_1 && dx > 0) || (sq_cap_2 && dx < 0)) rect.left -= pdev->pen_width / 2;
1346 if ((sq_cap_2 && dx > 0) || (sq_cap_1 && dx < 0)) rect.right += pdev->pen_width / 2;
1347 add_rect_to_region( total, &rect );
1348 if (dx > 0)
1350 face_1->start.x = face_1->end.x = rect.left;
1351 face_1->start.y = face_2->end.y = rect.bottom;
1352 face_1->end.y = face_2->start.y = rect.top;
1353 face_2->start.x = face_2->end.x = rect.right - 1;
1355 else
1357 face_1->start.x = face_1->end.x = rect.right;
1358 face_1->start.y = face_2->end.y = rect.top;
1359 face_1->end.y = face_2->start.y = rect.bottom;
1360 face_2->start.x = face_2->end.x = rect.left + 1;
1363 else if (dx == 0)
1365 rect.top = min( pt_1->y, pt_2->y );
1366 rect.bottom = max( pt_1->y, pt_2->y );
1367 rect.left = pt_1->x - pdev->pen_width / 2;
1368 rect.right = rect.left + pdev->pen_width;
1369 if ((sq_cap_1 && dy > 0) || (sq_cap_2 && dy < 0)) rect.top -= pdev->pen_width / 2;
1370 if ((sq_cap_2 && dy > 0) || (sq_cap_1 && dy < 0)) rect.bottom += pdev->pen_width / 2;
1371 add_rect_to_region( total, &rect );
1372 if (dy > 0)
1374 face_1->start.x = face_2->end.x = rect.left;
1375 face_1->start.y = face_1->end.y = rect.top;
1376 face_1->end.x = face_2->start.x = rect.right;
1377 face_2->start.y = face_2->end.y = rect.bottom - 1;
1379 else
1381 face_1->start.x = face_2->end.x = rect.right;
1382 face_1->start.y = face_1->end.y = rect.bottom;
1383 face_1->end.x = face_2->start.x = rect.left;
1384 face_2->start.y = face_2->end.y = rect.top + 1;
1387 else
1389 double len = hypot( dx, dy );
1390 double width_x, width_y;
1391 POINT seg_pts[4];
1392 POINT wide_half, narrow_half;
1393 HRGN segment;
1395 width_x = pdev->pen_width * abs( dy ) / len;
1396 width_y = pdev->pen_width * abs( dx ) / len;
1398 narrow_half.x = round( width_x / 2 );
1399 narrow_half.y = round( width_y / 2 );
1400 wide_half.x = round( (width_x + 1) / 2 );
1401 wide_half.y = round( (width_y + 1) / 2 );
1403 if (dx < 0)
1405 wide_half.y = -wide_half.y;
1406 narrow_half.y = -narrow_half.y;
1409 if (dy < 0)
1411 POINT tmp = narrow_half; narrow_half = wide_half; wide_half = tmp;
1412 wide_half.x = -wide_half.x;
1413 narrow_half.x = -narrow_half.x;
1416 seg_pts[0].x = pt_1->x - narrow_half.x;
1417 seg_pts[0].y = pt_1->y + narrow_half.y;
1418 seg_pts[1].x = pt_1->x + wide_half.x;
1419 seg_pts[1].y = pt_1->y - wide_half.y;
1420 seg_pts[2].x = pt_2->x + wide_half.x;
1421 seg_pts[2].y = pt_2->y - wide_half.y;
1422 seg_pts[3].x = pt_2->x - narrow_half.x;
1423 seg_pts[3].y = pt_2->y + narrow_half.y;
1425 if (sq_cap_1)
1427 seg_pts[0].x -= narrow_half.y;
1428 seg_pts[1].x -= narrow_half.y;
1429 seg_pts[0].y -= narrow_half.x;
1430 seg_pts[1].y -= narrow_half.x;
1433 if (sq_cap_2)
1435 seg_pts[2].x += wide_half.y;
1436 seg_pts[3].x += wide_half.y;
1437 seg_pts[2].y += wide_half.x;
1438 seg_pts[3].y += wide_half.x;
1441 segment = CreatePolygonRgn( seg_pts, 4, ALTERNATE );
1442 CombineRgn( total, total, segment, RGN_OR );
1443 DeleteObject( segment );
1445 face_1->start = seg_pts[0];
1446 face_1->end = seg_pts[1];
1447 face_2->start = seg_pts[2];
1448 face_2->end = seg_pts[3];
1451 face_1->dx = face_2->dx = dx;
1452 face_1->dy = face_2->dy = dy;
1455 static void wide_line_segments( dibdrv_physdev *pdev, int num, const POINT *pts, BOOL close,
1456 int start, int count, const POINT *first_pt, const POINT *last_pt,
1457 HRGN round_cap, HRGN total )
1459 int i;
1460 struct face face_1, face_2, prev_face, first_face;
1461 const POINT *pt_1, *pt_2;
1463 if (!close)
1465 add_cap( pdev, total, round_cap, first_pt );
1466 add_cap( pdev, total, round_cap, last_pt );
1469 if (count == 1)
1471 pt_1 = &pts[start];
1472 pt_2 = &pts[(start + 1) % num];
1473 wide_line_segment( pdev, total, first_pt, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1474 TRUE, TRUE, &face_1, &face_2 );
1475 return;
1478 pt_1 = &pts[start];
1479 pt_2 = &pts[(start + 1) % num];
1480 wide_line_segment( pdev, total, first_pt, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1481 !close, FALSE, &first_face, &prev_face );
1484 for (i = 1; i < count - 1; i++)
1486 pt_1 = &pts[(start + i) % num];
1487 pt_2 = &pts[(start + i + 1) % num];
1488 wide_line_segment( pdev, total, pt_1, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1489 FALSE, FALSE, &face_1, &face_2 );
1490 add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1491 prev_face = face_2;
1494 pt_1 = &pts[(start + count - 1) % num];
1495 pt_2 = &pts[(start + count) % num];
1496 wide_line_segment( pdev, total, pt_1, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1497 FALSE, !close, &face_1, &face_2 );
1498 add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1499 if (close) add_join( pdev, total, round_cap, last_pt, &face_2, &first_face );
1502 static BOOL wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1504 HRGN round_cap = 0;
1506 assert( total != 0 ); /* wide pens should always be drawn through a region */
1507 assert( num >= 2 );
1509 /* skip empty segments */
1510 while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1511 while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1513 if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1514 round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1515 (pdev->pen_width + 1) / 2, (pdev->pen_width + 1) / 2 );
1517 if (close)
1518 wide_line_segments( pdev, num, pts, TRUE, 0, num, &pts[0], &pts[0], round_cap, total );
1519 else
1520 wide_line_segments( pdev, num, pts, FALSE, 0, num - 1, &pts[0], &pts[num - 1], round_cap, total );
1522 if (round_cap) DeleteObject( round_cap );
1523 return TRUE;
1526 static BOOL dashed_wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1528 int i, start, cur_len, initial_num = 0;
1529 POINT initial_point, start_point, end_point;
1530 HRGN round_cap = 0;
1532 assert( total != 0 ); /* wide pens should always be drawn through a region */
1533 assert( num >= 2 );
1535 /* skip empty segments */
1536 while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1537 while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1539 if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1540 round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1541 (pdev->pen_width + 1) / 2, (pdev->pen_width + 1) / 2 );
1543 start = 0;
1544 cur_len = 0;
1545 start_point = pts[0];
1547 for (i = 0; i < (close ? num : num - 1); i++)
1549 const POINT *pt_1 = pts + i;
1550 const POINT *pt_2 = pts + ((close && i == num - 1) ? 0 : i + 1);
1551 int dx = pt_2->x - pt_1->x;
1552 int dy = pt_2->y - pt_1->y;
1554 if (dx == 0 && dy == 0) continue;
1556 if (dy == 0)
1558 if (abs( dx ) - cur_len < pdev->dash_pos.left_in_dash)
1560 skip_dash( pdev, abs( dx ) - cur_len );
1561 cur_len = 0;
1562 continue;
1564 cur_len += pdev->dash_pos.left_in_dash;
1565 dx = (dx > 0) ? cur_len : -cur_len;
1567 else if (dx == 0)
1569 if (abs( dy ) - cur_len < pdev->dash_pos.left_in_dash)
1571 skip_dash( pdev, abs( dy ) - cur_len );
1572 cur_len = 0;
1573 continue;
1575 cur_len += pdev->dash_pos.left_in_dash;
1576 dy = (dy > 0) ? cur_len : -cur_len;
1578 else
1580 double len = hypot( dx, dy );
1582 if (len - cur_len < pdev->dash_pos.left_in_dash)
1584 skip_dash( pdev, len - cur_len );
1585 cur_len = 0;
1586 continue;
1588 cur_len += pdev->dash_pos.left_in_dash;
1589 dx = dx * cur_len / len;
1590 dy = dy * cur_len / len;
1592 end_point.x = pt_1->x + dx;
1593 end_point.y = pt_1->y + dy;
1595 if (pdev->dash_pos.mark)
1597 if (!initial_num && close) /* this is the first dash, save it for later */
1599 initial_num = i - start + 1;
1600 initial_point = end_point;
1602 else wide_line_segments( pdev, num, pts, FALSE, start, i - start + 1,
1603 &start_point, &end_point, round_cap, total );
1605 if (!initial_num) initial_num = -1; /* no need to close it */
1607 skip_dash( pdev, pdev->dash_pos.left_in_dash );
1608 start_point = end_point;
1609 start = i;
1610 i--; /* go on with the same segment */
1613 if (pdev->dash_pos.mark) /* we have a final dash */
1615 int count;
1617 if (initial_num > 0)
1619 count = num - start + initial_num;
1620 end_point = initial_point;
1622 else if (close)
1624 count = num - start;
1625 end_point = pts[0];
1627 else
1629 count = num - start - 1;
1630 end_point = pts[num - 1];
1632 wide_line_segments( pdev, num, pts, FALSE, start, count,
1633 &start_point, &end_point, round_cap, total );
1635 else if (initial_num > 0) /* initial dash only */
1637 wide_line_segments( pdev, num, pts, FALSE, 0, initial_num,
1638 &pts[0], &initial_point, round_cap, total );
1641 if (round_cap) DeleteObject( round_cap );
1642 return TRUE;
1645 static const dash_pattern dash_patterns_cosmetic[4] =
1647 {2, {18, 6}, 24}, /* PS_DASH */
1648 {2, {3, 3}, 6}, /* PS_DOT */
1649 {4, {9, 6, 3, 6}, 24}, /* PS_DASHDOT */
1650 {6, {9, 3, 3, 3, 3, 3}, 24} /* PS_DASHDOTDOT */
1653 static const dash_pattern dash_patterns_geometric[4] =
1655 {2, {3, 1}, 4}, /* PS_DASH */
1656 {2, {1, 1}, 2}, /* PS_DOT */
1657 {4, {3, 1, 1, 1}, 6}, /* PS_DASHDOT */
1658 {6, {3, 1, 1, 1, 1, 1}, 8} /* PS_DASHDOTDOT */
1661 static inline void set_dash_pattern( dash_pattern *pattern, DWORD count, DWORD *dashes )
1663 DWORD i;
1665 pattern->count = count;
1666 pattern->total_len = 0;
1667 memcpy( pattern->dashes, dashes, count * sizeof(DWORD) );
1668 for (i = 0; i < count; i++) pattern->total_len += dashes[i];
1669 if (pattern->count % 2) pattern->total_len *= 2;
1672 static inline void scale_dash_pattern( dash_pattern *pattern, DWORD scale, DWORD endcap )
1674 DWORD i;
1676 for (i = 0; i < pattern->count; i++) pattern->dashes[i] *= scale;
1677 pattern->total_len *= scale;
1679 if (endcap != PS_ENDCAP_FLAT) /* shrink the dashes to leave room for the caps */
1681 for (i = 0; i < pattern->count; i += 2)
1683 pattern->dashes[i] -= scale;
1684 pattern->dashes[i + 1] += scale;
1689 static inline int get_pen_device_width( dibdrv_physdev *pdev, int width )
1691 POINT pts[2];
1693 if (!width) return 1;
1694 pts[0].x = pts[0].y = pts[1].y = 0;
1695 pts[1].x = width;
1696 LPtoDP( pdev->dev.hdc, pts, 2 );
1697 width = abs( pts[1].x - pts[0].x );
1698 return max( width, 1 );
1701 /***********************************************************************
1702 * dibdrv_SetDCPenColor
1704 COLORREF dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1706 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCPenColor );
1707 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1709 if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
1710 pdev->pen_brush.colorref = color;
1712 return next->funcs->pSetDCPenColor( next, color );
1715 /**********************************************************************
1716 * solid_brush
1718 * Fill a number of rectangles with the solid brush
1720 static BOOL solid_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1721 int num, const RECT *rects, INT rop)
1723 rop_mask brush_color;
1724 DWORD color = get_pixel_color( pdev, brush->colorref, TRUE );
1726 calc_rop_masks( rop, color, &brush_color );
1727 dib->funcs->solid_rects( dib, num, rects, brush_color.and, brush_color.xor );
1728 return TRUE;
1731 static void free_pattern_brush_bits( dib_brush *brush )
1733 HeapFree(GetProcessHeap(), 0, brush->and_bits);
1734 HeapFree(GetProcessHeap(), 0, brush->xor_bits);
1735 brush->and_bits = NULL;
1736 brush->xor_bits = NULL;
1739 void free_pattern_brush( dib_brush *brush )
1741 free_pattern_brush_bits( brush );
1742 free_dib_info( &brush->dib );
1745 static BOOL create_pattern_brush_bits( dib_brush *brush )
1747 DWORD size = brush->dib.height * abs(brush->dib.stride);
1748 DWORD *brush_bits = brush->dib.bits.ptr;
1749 DWORD *and_bits, *xor_bits;
1751 assert(brush->and_bits == NULL);
1752 assert(brush->xor_bits == NULL);
1754 assert(brush->dib.stride > 0);
1756 and_bits = brush->and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1757 xor_bits = brush->xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1759 if(!and_bits || !xor_bits)
1761 ERR("Failed to create pattern brush bits\n");
1762 free_pattern_brush_bits( brush );
1763 return FALSE;
1766 while(size)
1768 calc_and_xor_masks(brush->rop, *brush_bits++, and_bits++, xor_bits++);
1769 size -= 4;
1772 return TRUE;
1775 static const DWORD hatches[6][8] =
1777 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1778 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
1779 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL */
1780 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL */
1781 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
1782 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
1785 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect)
1787 dib_info hatch;
1788 rop_mask fg_mask, bg_mask;
1789 rop_mask_bits mask_bits;
1790 DWORD size;
1791 BOOL ret;
1793 assert(brush->and_bits == NULL);
1794 assert(brush->xor_bits == NULL);
1796 /* Just initialise brush dib with the color / sizing info. We don't
1797 need the bits as we'll calculate the rop masks straight from
1798 the hatch patterns. */
1800 copy_dib_color_info(&brush->dib, &pdev->dib);
1801 brush->dib.width = 8;
1802 brush->dib.height = 8;
1803 brush->dib.stride = get_dib_stride( brush->dib.width, brush->dib.bit_count );
1805 size = brush->dib.height * brush->dib.stride;
1807 mask_bits.and = brush->and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1808 mask_bits.xor = brush->xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1810 if(!mask_bits.and || !mask_bits.xor)
1812 ERR("Failed to create pattern brush bits\n");
1813 free_pattern_brush_bits( brush );
1814 return FALSE;
1817 hatch.bit_count = 1;
1818 hatch.height = hatch.width = 8;
1819 hatch.stride = 4;
1820 hatch.bits.ptr = (void *) hatches[brush->hatch];
1821 hatch.bits.free = hatch.bits.param = NULL;
1822 hatch.bits.is_copy = FALSE;
1824 get_color_masks( pdev, brush->rop, brush->colorref, GetBkMode(pdev->dev.hdc),
1825 &fg_mask, &bg_mask );
1827 if (brush->colorref & (1 << 24)) /* PALETTEINDEX */
1828 *needs_reselect = TRUE;
1829 if (GetBkMode(pdev->dev.hdc) != TRANSPARENT && (GetBkColor(pdev->dev.hdc) & (1 << 24)))
1830 *needs_reselect = TRUE;
1832 ret = brush->dib.funcs->create_rop_masks( &brush->dib, &hatch, &fg_mask, &bg_mask, &mask_bits );
1833 if(!ret) free_pattern_brush_bits( brush );
1835 return ret;
1838 static BOOL matching_pattern_format( dib_info *dib, dib_info *pattern )
1840 if (dib->bit_count != pattern->bit_count) return FALSE;
1841 if (dib->stride != pattern->stride) return FALSE;
1843 switch (dib->bit_count)
1845 case 1:
1846 case 4:
1847 case 8:
1848 if (dib->color_table_size != pattern->color_table_size) return FALSE;
1849 return !memcmp( dib->color_table, pattern->color_table, dib->color_table_size * sizeof(RGBQUAD) );
1850 case 16:
1851 case 32:
1852 return (dib->red_mask == pattern->red_mask &&
1853 dib->green_mask == pattern->green_mask &&
1854 dib->blue_mask == pattern->blue_mask);
1856 return TRUE;
1859 static BOOL select_pattern_brush( dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect )
1861 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1862 BITMAPINFO *info = (BITMAPINFO *)buffer;
1863 RGBQUAD color_table[2];
1864 RECT rect;
1865 dib_info pattern;
1867 if (!brush->pattern.info)
1869 BITMAPOBJ *bmp = GDI_GetObjPtr( brush->pattern.bitmap, OBJ_BITMAP );
1870 BOOL ret;
1872 if (!bmp) return FALSE;
1873 ret = init_dib_info_from_bitmapobj( &pattern, bmp, 0 );
1874 GDI_ReleaseObj( brush->pattern.bitmap );
1875 if (!ret) return FALSE;
1877 else if (brush->pattern.info->bmiHeader.biClrUsed && brush->pattern.usage == DIB_PAL_COLORS)
1879 copy_bitmapinfo( info, brush->pattern.info );
1880 fill_color_table_from_pal_colors( info, pdev->dev.hdc );
1881 init_dib_info_from_bitmapinfo( &pattern, info, brush->pattern.bits.ptr, 0 );
1882 *needs_reselect = TRUE;
1884 else
1886 init_dib_info_from_bitmapinfo( &pattern, brush->pattern.info, brush->pattern.bits.ptr, 0 );
1889 if (pattern.bit_count == 1 && !pattern.color_table)
1891 /* monochrome DDB pattern uses DC colors */
1892 DWORD pixel;
1893 BOOL got_pixel;
1894 COLORREF color;
1896 color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetTextColor( pdev->dev.hdc ),
1897 &got_pixel, &pixel );
1898 color_table[0].rgbRed = GetRValue( color );
1899 color_table[0].rgbGreen = GetGValue( color );
1900 color_table[0].rgbBlue = GetBValue( color );
1901 color_table[0].rgbReserved = 0;
1903 color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetBkColor( pdev->dev.hdc ),
1904 &got_pixel, &pixel );
1905 color_table[1].rgbRed = GetRValue( color );
1906 color_table[1].rgbGreen = GetGValue( color );
1907 color_table[1].rgbBlue = GetBValue( color );
1908 color_table[1].rgbReserved = 0;
1910 pattern.color_table = color_table;
1911 pattern.color_table_size = 2;
1912 *needs_reselect = TRUE;
1915 copy_dib_color_info(&brush->dib, &pdev->dib);
1917 brush->dib.height = pattern.height;
1918 brush->dib.width = pattern.width;
1919 brush->dib.stride = get_dib_stride( brush->dib.width, brush->dib.bit_count );
1921 if (matching_pattern_format( &brush->dib, &pattern ))
1923 brush->dib.bits.ptr = pattern.bits.ptr;
1924 brush->dib.bits.is_copy = FALSE;
1925 brush->dib.bits.free = NULL;
1927 else
1929 brush->dib.bits.ptr = HeapAlloc( GetProcessHeap(), 0, brush->dib.height * brush->dib.stride );
1930 brush->dib.bits.is_copy = TRUE;
1931 brush->dib.bits.free = free_heap_bits;
1933 rect.left = rect.top = 0;
1934 rect.right = pattern.width;
1935 rect.bottom = pattern.height;
1937 brush->dib.funcs->convert_to(&brush->dib, &pattern, &rect);
1939 return TRUE;
1942 /**********************************************************************
1943 * pattern_brush
1945 * Fill a number of rectangles with the pattern brush
1946 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1948 static BOOL pattern_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1949 int num, const RECT *rects, INT rop)
1951 POINT origin;
1952 BOOL needs_reselect = FALSE;
1954 if (rop != brush->rop)
1956 free_pattern_brush_bits( brush );
1957 brush->rop = rop;
1960 if(brush->and_bits == NULL)
1962 switch(brush->style)
1964 case BS_DIBPATTERN:
1965 if (!brush->dib.bits.ptr && !select_pattern_brush( pdev, brush, &needs_reselect ))
1966 return FALSE;
1967 if(!create_pattern_brush_bits( brush ))
1968 return FALSE;
1969 break;
1971 case BS_HATCHED:
1972 if(!create_hatch_brush_bits(pdev, brush, &needs_reselect))
1973 return FALSE;
1974 break;
1976 default:
1977 ERR("Unexpected brush style %d\n", brush->style);
1978 return FALSE;
1982 GetBrushOrgEx(pdev->dev.hdc, &origin);
1984 dib->funcs->pattern_rects( dib, num, rects, &origin, &brush->dib, brush->and_bits, brush->xor_bits );
1986 if (needs_reselect) free_pattern_brush( brush );
1987 return TRUE;
1990 static BOOL null_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1991 int num, const RECT *rects, INT rop)
1993 return TRUE;
1996 static void select_brush( dib_brush *brush, const LOGBRUSH *logbrush, const struct brush_pattern *pattern )
1998 free_pattern_brush( brush );
2000 if (pattern)
2002 brush->style = BS_DIBPATTERN;
2003 brush->pattern = *pattern; /* brush is actually selected only when it's used */
2004 brush->rects = pattern_brush;
2006 else
2008 brush->style = logbrush->lbStyle;
2009 brush->colorref = logbrush->lbColor;
2010 brush->hatch = logbrush->lbHatch;
2012 switch (logbrush->lbStyle)
2014 case BS_SOLID: brush->rects = solid_brush; break;
2015 case BS_NULL: brush->rects = null_brush; break;
2016 case BS_HATCHED: brush->rects = pattern_brush; break;
2021 /***********************************************************************
2022 * dibdrv_SelectBrush
2024 HBRUSH dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush, const struct brush_pattern *pattern )
2026 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBrush );
2027 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2028 LOGBRUSH logbrush;
2030 TRACE("(%p, %p)\n", dev, hbrush);
2032 GetObjectW( hbrush, sizeof(logbrush), &logbrush );
2034 if (hbrush == GetStockObject( DC_BRUSH ))
2035 logbrush.lbColor = GetDCBrushColor( dev->hdc );
2037 select_brush( &pdev->brush, &logbrush, pattern );
2039 return next->funcs->pSelectBrush( next, hbrush, pattern );
2042 /***********************************************************************
2043 * dibdrv_SelectPen
2045 HPEN dibdrv_SelectPen( PHYSDEV dev, HPEN hpen, const struct brush_pattern *pattern )
2047 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectPen );
2048 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2049 LOGPEN logpen;
2050 LOGBRUSH logbrush;
2051 EXTLOGPEN *elp = NULL;
2053 TRACE("(%p, %p)\n", dev, hpen);
2055 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
2057 /* must be an extended pen */
2058 INT size = GetObjectW( hpen, 0, NULL );
2060 if (!size) return 0;
2062 elp = HeapAlloc( GetProcessHeap(), 0, size );
2064 GetObjectW( hpen, size, elp );
2065 logpen.lopnStyle = elp->elpPenStyle;
2066 logpen.lopnWidth.x = elp->elpWidth;
2067 /* cosmetic ext pens are always 1-pixel wide */
2068 if (!(logpen.lopnStyle & PS_GEOMETRIC)) logpen.lopnWidth.x = 0;
2070 logbrush.lbStyle = elp->elpBrushStyle;
2071 logbrush.lbColor = elp->elpColor;
2072 logbrush.lbHatch = elp->elpHatch;
2074 else
2076 logbrush.lbStyle = BS_SOLID;
2077 logbrush.lbColor = logpen.lopnColor;
2078 logbrush.lbHatch = 0;
2081 pdev->pen_join = logpen.lopnStyle & PS_JOIN_MASK;
2082 pdev->pen_endcap = logpen.lopnStyle & PS_ENDCAP_MASK;
2083 pdev->pen_width = get_pen_device_width( pdev, logpen.lopnWidth.x );
2085 if (hpen == GetStockObject( DC_PEN ))
2086 logbrush.lbColor = GetDCPenColor( dev->hdc );
2088 set_dash_pattern( &pdev->pen_pattern, 0, NULL );
2089 select_brush( &pdev->pen_brush, &logbrush, pattern );
2091 pdev->pen_style = logpen.lopnStyle & PS_STYLE_MASK;
2093 switch (pdev->pen_style)
2095 case PS_DASH:
2096 case PS_DOT:
2097 case PS_DASHDOT:
2098 case PS_DASHDOTDOT:
2099 if (logpen.lopnStyle & PS_GEOMETRIC)
2101 pdev->pen_pattern = dash_patterns_geometric[pdev->pen_style - 1];
2102 if (pdev->pen_width > 1)
2104 scale_dash_pattern( &pdev->pen_pattern, pdev->pen_width, pdev->pen_endcap );
2105 pdev->pen_lines = dashed_wide_pen_lines;
2107 else pdev->pen_lines = dashed_pen_lines;
2108 break;
2110 if (pdev->pen_width == 1) /* wide cosmetic pens are not dashed */
2112 pdev->pen_lines = dashed_pen_lines;
2113 pdev->pen_pattern = dash_patterns_cosmetic[pdev->pen_style - 1];
2114 break;
2116 /* fall through */
2117 case PS_SOLID:
2118 case PS_INSIDEFRAME:
2119 pdev->pen_lines = (pdev->pen_width == 1) ? solid_pen_lines : wide_pen_lines;
2120 break;
2122 case PS_NULL:
2123 pdev->pen_width = 0;
2124 pdev->pen_lines = null_pen_lines;
2125 break;
2127 case PS_ALTERNATE:
2128 pdev->pen_lines = dashed_pen_lines;
2129 pdev->pen_pattern = dash_patterns_geometric[PS_DOT - 1];
2130 break;
2132 case PS_USERSTYLE:
2133 pdev->pen_lines = (pdev->pen_width == 1) ? dashed_pen_lines : dashed_wide_pen_lines;
2134 set_dash_pattern( &pdev->pen_pattern, elp->elpNumEntries, elp->elpStyleEntry );
2135 if (!(logpen.lopnStyle & PS_GEOMETRIC)) scale_dash_pattern( &pdev->pen_pattern, 3, PS_ENDCAP_FLAT );
2136 break;
2139 pdev->pen_uses_region = (logpen.lopnStyle & PS_GEOMETRIC || pdev->pen_width > 1);
2140 pdev->pen_is_ext = (elp != NULL);
2141 HeapFree( GetProcessHeap(), 0, elp );
2143 return next->funcs->pSelectPen( next, hpen, pattern );
2146 /***********************************************************************
2147 * dibdrv_SetDCBrushColor
2149 COLORREF dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
2151 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCBrushColor );
2152 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2154 if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
2155 pdev->brush.colorref = color;
2157 return next->funcs->pSetDCBrushColor( next, color );
2160 BOOL brush_rect(dibdrv_physdev *pdev, dib_brush *brush, const RECT *rect, HRGN clip, INT rop)
2162 struct clipped_rects clipped_rects;
2163 BOOL ret;
2165 if (!get_clipped_rects( &pdev->dib, rect, clip, &clipped_rects )) return TRUE;
2166 ret = brush->rects( pdev, brush, &pdev->dib, clipped_rects.count, clipped_rects.rects, rop );
2167 free_clipped_rects( &clipped_rects );
2168 return ret;
2171 /* paint a region with the brush (note: the region can be modified) */
2172 BOOL brush_region( dibdrv_physdev *pdev, HRGN region )
2174 if (pdev->clip) CombineRgn( region, region, pdev->clip, RGN_AND );
2175 return brush_rect( pdev, &pdev->brush, NULL, region, GetROP2( pdev->dev.hdc ));
2178 /* paint a region with the pen (note: the region can be modified) */
2179 BOOL pen_region( dibdrv_physdev *pdev, HRGN region )
2181 if (pdev->clip) CombineRgn( region, region, pdev->clip, RGN_AND );
2182 return brush_rect( pdev, &pdev->pen_brush, NULL, region, GetROP2( pdev->dev.hdc ));