mshtml: Moved detaching inner window to separated function.
[wine/multimedia.git] / dlls / gdi32 / dibdrv / objects.c
blob35164e6a2900cf9329f17876d534fb91a34b4f59
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 const RGBQUAD *color_table = get_dib_color_table( dib );
149 WORD index = LOWORD( color );
150 *got_pixel = TRUE;
151 if (!color_table || index >= (1 << dib->bit_count)) return 0;
152 *pixel = index;
153 return RGB( color_table[index].rgbRed, color_table[index].rgbGreen, color_table[index].rgbBlue );
156 return color & 0xffffff;
159 /******************************************************************
160 * get_pixel_color
162 * 1 bit bitmaps map the fg/bg colors as follows:
163 * If the fg colorref exactly matches one of the color table entries then
164 * that entry is the fg color and the other is the bg.
165 * Otherwise the bg color is mapped to the closest entry in the table and
166 * the fg takes the other one.
168 DWORD get_pixel_color( dibdrv_physdev *pdev, COLORREF color, BOOL mono_fixup )
170 RGBQUAD fg_quad;
171 BOOL got_pixel;
172 DWORD pixel;
173 COLORREF rgb_ref;
174 const RGBQUAD *color_table;
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 color_table = get_dib_color_table( &pdev->dib );
183 fg_quad = rgbquad_from_colorref( rgb_ref );
184 if(rgbquad_equal(&fg_quad, color_table))
185 return 0;
186 if(rgbquad_equal(&fg_quad, color_table + 1))
187 return 1;
189 pixel = get_pixel_color( pdev, GetBkColor(pdev->dev.hdc), FALSE );
190 if (color == GetBkColor(pdev->dev.hdc)) return pixel;
191 else return !pixel;
194 /***************************************************************************
195 * get_color_masks
197 * Returns the color masks unless the dib is 1 bpp. In this case since
198 * there are several fg sources (pen, brush, text) we take as bg the inverse
199 * of the relevant fg color (which is always set up correctly).
201 static inline void get_color_masks( dibdrv_physdev *pdev, UINT rop, COLORREF colorref,
202 INT bkgnd_mode, rop_mask *fg_mask, rop_mask *bg_mask )
204 DWORD color = get_pixel_color( pdev, colorref, TRUE );
206 calc_rop_masks( rop, color, fg_mask );
208 if (bkgnd_mode == TRANSPARENT)
210 bg_mask->and = ~0u;
211 bg_mask->xor = 0;
212 return;
215 if (pdev->dib.bit_count != 1) color = get_pixel_color( pdev, GetBkColor(pdev->dev.hdc), FALSE );
216 else if (colorref != GetBkColor(pdev->dev.hdc)) color = !color;
218 calc_rop_masks( rop, color, bg_mask );
221 static inline void order_end_points(int *s, int *e)
223 if(*s > *e)
225 int tmp;
226 tmp = *s + 1;
227 *s = *e + 1;
228 *e = tmp;
232 #define Y_INCREASING_MASK 0x0f
233 #define X_INCREASING_MASK 0xc3
234 #define X_MAJOR_MASK 0x99
235 #define POS_SLOPE_MASK 0x33
237 static inline BOOL is_xmajor(DWORD octant)
239 return octant & X_MAJOR_MASK;
242 static inline BOOL is_pos_slope(DWORD octant)
244 return octant & POS_SLOPE_MASK;
247 static inline BOOL is_x_increasing(DWORD octant)
249 return octant & X_INCREASING_MASK;
252 static inline BOOL is_y_increasing(DWORD octant)
254 return octant & Y_INCREASING_MASK;
257 /**********************************************************************
258 * get_octant_number
260 * Return the octant number starting clockwise from the +ve x-axis.
262 static inline int get_octant_number(int dx, int dy)
264 if(dy > 0)
265 if(dx > 0)
266 return ( dx > dy) ? 1 : 2;
267 else
268 return (-dx > dy) ? 4 : 3;
269 else
270 if(dx < 0)
271 return (-dx > -dy) ? 5 : 6;
272 else
273 return ( dx > -dy) ? 8 : 7;
276 static inline DWORD get_octant_mask(int dx, int dy)
278 return 1 << (get_octant_number(dx, dy) - 1);
281 static inline int get_bias( DWORD mask )
283 /* Octants 3, 5, 6 and 8 take a bias */
284 return (mask & 0xb4) ? 1 : 0;
287 #define OUT_LEFT 1
288 #define OUT_RIGHT 2
289 #define OUT_TOP 4
290 #define OUT_BOTTOM 8
292 static inline DWORD calc_outcode(const POINT *pt, const RECT *clip)
294 DWORD out = 0;
295 if(pt->x < clip->left) out |= OUT_LEFT;
296 else if(pt->x >= clip->right) out |= OUT_RIGHT;
297 if(pt->y < clip->top) out |= OUT_TOP;
298 else if(pt->y >= clip->bottom) out |= OUT_BOTTOM;
300 return out;
303 /******************************************************************************
304 * clip_line
306 * Clips the start and end points to a rectangle.
308 * Note, this treats the end point like the start point. If the
309 * caller doesn't want it displayed, it should exclude it. If the end
310 * point is clipped out, then the likelihood is that the new end point
311 * should be displayed.
313 * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
315 * This derivation is based on the comments in X.org's xserver/mi/mizerclip.c,
316 * however the Bresenham error term is defined differently so the equations
317 * will also differ.
319 * For x major lines we have 2dy >= err + bias > 2dy - 2dx
320 * 0 >= err + bias - 2dy > -2dx
322 * Note dx, dy, m and n are all +ve.
324 * Moving the start pt from x1 to x1 + m, we need to figure out y1 + n.
325 * err = 2dy - dx + 2mdy - 2ndx
326 * 0 >= 2dy - dx + 2mdy - 2ndx + bias - 2dy > -2dx
327 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
328 * which of course will give exactly one solution for n,
329 * so looking at the >= inequality
330 * n >= (2mdy + bias - dx) / 2dx
331 * n = ceiling((2mdy + bias - dx) / 2dx)
332 * = (2mdy + bias + dx - 1) / 2dx (assuming division truncation)
334 * Moving start pt from y1 to y1 + n we need to figure out x1 + m - there may be several
335 * solutions we pick the one that minimizes m (ie that first unlipped pt). As above:
336 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
337 * 2mdy > 2ndx - bias - dx
338 * m > (2ndx - bias - dx) / 2dy
339 * m = floor((2ndx - bias - dx) / 2dy) + 1
340 * m = (2ndx - bias - dx) / 2dy + 1
342 * Moving end pt from x2 to x2 - m, we need to figure out y2 - n
343 * err = 2dy - dx + 2(dx - m)dy - 2(dy - n)dx
344 * = 2dy - dx - 2mdy + 2ndx
345 * 0 >= 2dy - dx - 2mdy + 2ndx + bias - 2dy > -2dx
346 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
347 * again exactly one solution.
348 * 2ndx <= 2mdy - bias + dx
349 * n = floor((2mdy - bias + dx) / 2dx)
350 * = (2mdy - bias + dx) / 2dx
352 * Moving end pt from y2 to y2 - n when need x2 - m this time maximizing x2 - m so
353 * mininizing m to include all of the points at y = y2 - n. As above:
354 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
355 * 2mdy >= 2ndx + bias - dx
356 * m = ceiling((2ndx + bias - dx) / 2dy)
357 * = (2ndx + bias - dx - 1) / 2dy + 1
359 * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
361 * Moving start point from y1 to y1 + n find x1 + m
362 * m = (2ndx + bias + dy - 1) / 2dy
364 * Moving start point from x1 to x1 + m find y1 + n
365 * n = (2mdy - bias - dy) / 2ndx + 1
367 * Moving end point from y2 to y2 - n find x1 - m
368 * m = (2ndx - bias + dy) / 2dy
370 * Moving end point from x2 to x2 - m find y2 - n
371 * n = (2mdy + bias - dy - 1) / 2dx + 1
373 int clip_line(const POINT *start, const POINT *end, const RECT *clip,
374 const bres_params *params, POINT *pt1, POINT *pt2)
377 INT64 m, n; /* 64-bit to avoid overflows (FIXME: find a more efficient way) */
378 BOOL clipped = FALSE;
379 DWORD start_oc, end_oc;
380 const int bias = params->bias;
381 const unsigned int dx = params->dx;
382 const unsigned int dy = params->dy;
383 const unsigned int two_dx = params->dx * 2;
384 const unsigned int two_dy = params->dy * 2;
385 const BOOL xmajor = is_xmajor(params->octant);
386 const BOOL neg_slope = !is_pos_slope(params->octant);
388 *pt1 = *start;
389 *pt2 = *end;
391 start_oc = calc_outcode(start, clip);
392 end_oc = calc_outcode(end, clip);
394 while(1)
396 if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
397 if(start_oc & end_oc) return 0; /* trivial reject */
399 clipped = TRUE;
400 if(start_oc & OUT_LEFT)
402 m = clip->left - start->x;
403 if(xmajor)
404 n = (m * two_dy + bias + dx - 1) / two_dx;
405 else
406 n = (m * two_dy - bias - dy) / two_dx + 1;
408 pt1->x = clip->left;
409 if(neg_slope) n = -n;
410 pt1->y = start->y + n;
411 start_oc = calc_outcode(pt1, clip);
413 else if(start_oc & OUT_RIGHT)
415 m = start->x - clip->right + 1;
416 if(xmajor)
417 n = (m * two_dy + bias + dx - 1) / two_dx;
418 else
419 n = (m * two_dy - bias - dy) / two_dx + 1;
421 pt1->x = clip->right - 1;
422 if(neg_slope) n = -n;
423 pt1->y = start->y - n;
424 start_oc = calc_outcode(pt1, clip);
426 else if(start_oc & OUT_TOP)
428 n = clip->top - start->y;
429 if(xmajor)
430 m = (n * two_dx - bias - dx) / two_dy + 1;
431 else
432 m = (n * two_dx + bias + dy - 1) / two_dy;
434 pt1->y = clip->top;
435 if(neg_slope) m = -m;
436 pt1->x = start->x + m;
437 start_oc = calc_outcode(pt1, clip);
439 else if(start_oc & OUT_BOTTOM)
441 n = start->y - clip->bottom + 1;
442 if(xmajor)
443 m = (n * two_dx - bias - dx) / two_dy + 1;
444 else
445 m = (n * two_dx + bias + dy - 1) / two_dy;
447 pt1->y = clip->bottom - 1;
448 if(neg_slope) m = -m;
449 pt1->x = start->x - m;
450 start_oc = calc_outcode(pt1, clip);
452 else if(end_oc & OUT_LEFT)
454 m = clip->left - end->x;
455 if(xmajor)
456 n = (m * two_dy - bias + dx) / two_dx;
457 else
458 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
460 pt2->x = clip->left;
461 if(neg_slope) n = -n;
462 pt2->y = end->y + n;
463 end_oc = calc_outcode(pt2, clip);
465 else if(end_oc & OUT_RIGHT)
467 m = end->x - clip->right + 1;
468 if(xmajor)
469 n = (m * two_dy - bias + dx) / two_dx;
470 else
471 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
473 pt2->x = clip->right - 1;
474 if(neg_slope) n = -n;
475 pt2->y = end->y - n;
476 end_oc = calc_outcode(pt2, clip);
478 else if(end_oc & OUT_TOP)
480 n = clip->top - end->y;
481 if(xmajor)
482 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
483 else
484 m = (n * two_dx - bias + dy) / two_dy;
486 pt2->y = clip->top;
487 if(neg_slope) m = -m;
488 pt2->x = end->x + m;
489 end_oc = calc_outcode(pt2, clip);
491 else if(end_oc & OUT_BOTTOM)
493 n = end->y - clip->bottom + 1;
494 if(xmajor)
495 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
496 else
497 m = (n * two_dx - bias + dy) / two_dy;
499 pt2->y = clip->bottom - 1;
500 if(neg_slope) m = -m;
501 pt2->x = end->x - m;
502 end_oc = calc_outcode(pt2, clip);
507 static void bres_line_with_bias(const POINT *start, const struct line_params *params,
508 void (* callback)(dibdrv_physdev*,INT,INT), dibdrv_physdev *pdev)
510 POINT pt = *start;
511 int len = params->length, err = params->err_start;
513 if (params->x_major)
515 while(len--)
517 callback(pdev, pt.x, pt.y);
518 if (err + params->bias > 0)
520 pt.y += params->y_inc;
521 err += params->err_add_1;
523 else err += params->err_add_2;
524 pt.x += params->x_inc;
527 else
529 while(len--)
531 callback(pdev, pt.x, pt.y);
532 if (err + params->bias > 0)
534 pt.x += params->x_inc;
535 err += params->err_add_1;
537 else err += params->err_add_2;
538 pt.y += params->y_inc;
543 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end, DWORD and, DWORD xor)
545 struct clipped_rects clipped_rects;
546 RECT rect;
547 int i;
549 if(start->y == end->y)
551 rect.left = start->x;
552 rect.top = start->y;
553 rect.right = end->x;
554 rect.bottom = end->y + 1;
555 order_end_points(&rect.left, &rect.right);
556 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
557 pdev->dib.funcs->solid_rects(&pdev->dib, clipped_rects.count, clipped_rects.rects, and, xor);
559 else if(start->x == end->x)
561 rect.left = start->x;
562 rect.top = start->y;
563 rect.right = end->x + 1;
564 rect.bottom = end->y;
565 order_end_points(&rect.top, &rect.bottom);
566 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
567 pdev->dib.funcs->solid_rects(&pdev->dib, clipped_rects.count, clipped_rects.rects, and, xor);
569 else
571 bres_params clip_params;
572 struct line_params line_params;
573 INT dx = end->x - start->x, dy = end->y - start->y;
574 INT abs_dx = abs(dx), abs_dy = abs(dy);
576 clip_params.dx = abs_dx;
577 clip_params.dy = abs_dy;
578 clip_params.octant = get_octant_mask(dx, dy);
579 clip_params.bias = get_bias( clip_params.octant );
581 line_params.bias = clip_params.bias;
582 line_params.x_major = is_xmajor( clip_params.octant );
583 line_params.x_inc = is_x_increasing( clip_params.octant ) ? 1 : -1;
584 line_params.y_inc = is_y_increasing( clip_params.octant ) ? 1 : -1;
586 if (line_params.x_major)
588 line_params.err_add_1 = 2 * abs_dy - 2 * abs_dx;
589 line_params.err_add_2 = 2 * abs_dy;
591 else
593 line_params.err_add_1 = 2 * abs_dx - 2 * abs_dy;
594 line_params.err_add_2 = 2 * abs_dx;
597 rect.left = min( start->x, end->x );
598 rect.top = min( start->y, end->y );
599 rect.right = max( start->x, end->x ) + 1;
600 rect.bottom = max( start->y, end->y ) + 1;
601 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
602 for (i = 0; i < clipped_rects.count; i++)
604 POINT clipped_start, clipped_end;
605 int clip_status;
607 clip_status = clip_line(start, end, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
608 if(clip_status)
610 int m = abs(clipped_start.x - start->x);
611 int n = abs(clipped_start.y - start->y);
613 if (line_params.x_major)
615 line_params.err_start = 2 * abs_dy - abs_dx + m * 2 * abs_dy - n * 2 * abs_dx;
616 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
618 else
620 line_params.err_start = 2 * abs_dx - abs_dy + n * 2 * abs_dx - m * 2 * abs_dy;
621 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
624 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
626 pdev->dib.funcs->solid_line( &pdev->dib, &clipped_start, &line_params, and, xor );
628 if(clip_status == 2) break; /* completely unclipped, so we can finish */
632 free_clipped_rects( &clipped_rects );
633 return TRUE;
636 static BOOL solid_pen_line_region( dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region )
638 RECT rect;
640 rect.left = start->x;
641 rect.top = start->y;
642 rect.right = start->x + 1;
643 rect.bottom = start->y + 1;
645 if (start->y == end->y)
647 rect.right = end->x;
648 order_end_points(&rect.left, &rect.right);
649 add_rect_to_region( region, &rect );
651 else if(start->x == end->x)
653 rect.bottom = end->y;
654 order_end_points(&rect.top, &rect.bottom);
655 add_rect_to_region( region, &rect );
657 else
659 INT dx = end->x - start->x, dy = end->y - start->y;
660 INT abs_dx = abs(dx), abs_dy = abs(dy);
661 DWORD octant = get_octant_mask(dx, dy);
662 INT bias = get_bias( octant );
664 if (is_xmajor( octant ))
666 int y_inc = is_y_increasing( octant ) ? 1 : -1;
667 int err_add_1 = 2 * abs_dy - 2 * abs_dx;
668 int err_add_2 = 2 * abs_dy;
669 int err = 2 * abs_dy - abs_dx;
671 if (is_x_increasing( octant ))
673 for (rect.right = start->x + 1; rect.right <= end->x; rect.right++)
675 if (err + bias > 0)
677 add_rect_to_region( region, &rect );
678 rect.left = rect.right;
679 rect.top += y_inc;
680 rect.bottom += y_inc;
681 err += err_add_1;
683 else err += err_add_2;
686 else
688 for (rect.left = start->x; rect.left > end->x; rect.left--)
690 if (err + bias > 0)
692 add_rect_to_region( region, &rect );
693 rect.right = rect.left;
694 rect.top += y_inc;
695 rect.bottom += y_inc;
696 err += err_add_1;
698 else err += err_add_2;
702 else
704 int x_inc = is_x_increasing( octant ) ? 1 : -1;
705 int err_add_1 = 2 * abs_dx - 2 * abs_dy;
706 int err_add_2 = 2 * abs_dx;
707 int err = 2 * abs_dx - abs_dy;
709 if (is_y_increasing( octant ))
711 for (rect.bottom = start->y + 1; rect.bottom <= end->y; rect.bottom++)
713 if (err + bias > 0)
715 add_rect_to_region( region, &rect );
716 rect.top = rect.bottom;
717 rect.left += x_inc;
718 rect.right += x_inc;
719 err += err_add_1;
721 else err += err_add_2;
724 else
726 for (rect.top = start->y; rect.top > end->y; rect.top--)
728 if (err + bias > 0)
730 add_rect_to_region( region, &rect );
731 rect.bottom = rect.top;
732 rect.left += x_inc;
733 rect.right += x_inc;
734 err += err_add_1;
736 else err += err_add_2;
740 /* add final rect */
741 add_rect_to_region( region, &rect );
743 return TRUE;
746 static BOOL solid_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
748 int i;
750 assert( num >= 2 );
752 if (region)
754 for (i = 0; i < num - 1; i++)
755 if (!solid_pen_line_region( pdev, pts + i, pts + i + 1, region ))
756 return FALSE;
757 if (close) return solid_pen_line_region( pdev, pts + num - 1, pts, region );
759 else
761 DWORD color, and, xor;
763 color = get_pixel_color( pdev, pdev->pen_brush.colorref, TRUE );
764 calc_and_xor_masks( GetROP2(pdev->dev.hdc), color, &and, &xor );
766 for (i = 0; i < num - 1; i++)
767 if (!solid_pen_line( pdev, pts + i, pts + i + 1, and, xor ))
768 return FALSE;
769 if (close) return solid_pen_line( pdev, pts + num - 1, pts, and, xor );
771 return TRUE;
774 void reset_dash_origin(dibdrv_physdev *pdev)
776 pdev->dash_pos.cur_dash = 0;
777 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
778 pdev->dash_pos.mark = TRUE;
781 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
783 skip %= pdev->pen_pattern.total_len;
786 if(pdev->dash_pos.left_in_dash > skip)
788 pdev->dash_pos.left_in_dash -= skip;
789 return;
791 skip -= pdev->dash_pos.left_in_dash;
792 pdev->dash_pos.cur_dash++;
793 if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
794 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
795 pdev->dash_pos.mark = !pdev->dash_pos.mark;
797 while (skip);
800 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
802 RECT rect;
803 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
805 skip_dash(pdev, 1);
806 rect.left = x;
807 rect.right = x + 1;
808 rect.top = y;
809 rect.bottom = y + 1;
810 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
811 return;
814 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
816 struct clipped_rects clipped_rects;
817 int i, dash_len;
818 RECT rect;
819 const dash_pos start_pos = pdev->dash_pos;
821 if(start->y == end->y) /* hline */
823 BOOL l_to_r;
824 INT left, right, cur_x;
826 rect.top = start->y;
827 rect.bottom = start->y + 1;
829 if(start->x <= end->x)
831 left = start->x;
832 right = end->x - 1;
833 l_to_r = TRUE;
835 else
837 left = end->x + 1;
838 right = start->x;
839 l_to_r = FALSE;
842 rect.left = min( start->x, end->x );
843 rect.right = max( start->x, end->x ) + 1;
844 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
845 for (i = 0; i < clipped_rects.count; i++)
847 if(clipped_rects.rects[i].right > left && clipped_rects.rects[i].left <= right)
849 int clipped_left = max(clipped_rects.rects[i].left, left);
850 int clipped_right = min(clipped_rects.rects[i].right - 1, right);
852 pdev->dash_pos = start_pos;
854 if(l_to_r)
856 cur_x = clipped_left;
857 if(cur_x != left)
858 skip_dash(pdev, clipped_left - left);
860 while(cur_x <= clipped_right)
862 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
863 dash_len = pdev->dash_pos.left_in_dash;
864 if(cur_x + dash_len > clipped_right + 1)
865 dash_len = clipped_right - cur_x + 1;
866 rect.left = cur_x;
867 rect.right = cur_x + dash_len;
869 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
870 cur_x += dash_len;
871 skip_dash(pdev, dash_len);
874 else
876 cur_x = clipped_right;
877 if(cur_x != right)
878 skip_dash(pdev, right - clipped_right);
880 while(cur_x >= clipped_left)
882 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
883 dash_len = pdev->dash_pos.left_in_dash;
884 if(cur_x - dash_len < clipped_left - 1)
885 dash_len = cur_x - clipped_left + 1;
886 rect.left = cur_x - dash_len + 1;
887 rect.right = cur_x + 1;
889 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
890 cur_x -= dash_len;
891 skip_dash(pdev, dash_len);
896 pdev->dash_pos = start_pos;
897 skip_dash(pdev, right - left + 1);
899 else if(start->x == end->x) /* vline */
901 BOOL t_to_b;
902 INT top, bottom, cur_y;
904 rect.left = start->x;
905 rect.right = start->x + 1;
907 if(start->y <= end->y)
909 top = start->y;
910 bottom = end->y - 1;
911 t_to_b = TRUE;
913 else
915 top = end->y + 1;
916 bottom = start->y;
917 t_to_b = FALSE;
920 rect.top = min( start->y, end->y );
921 rect.bottom = max( start->y, end->y ) + 1;
922 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
923 for (i = 0; i < clipped_rects.count; i++)
925 if(clipped_rects.rects[i].right > start->x && clipped_rects.rects[i].left <= start->x)
927 int clipped_top = max(clipped_rects.rects[i].top, top);
928 int clipped_bottom = min(clipped_rects.rects[i].bottom - 1, bottom);
930 pdev->dash_pos = start_pos;
932 if(t_to_b)
934 cur_y = clipped_top;
935 if(cur_y != top)
936 skip_dash(pdev, clipped_top - top);
938 while(cur_y <= clipped_bottom)
940 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
941 dash_len = pdev->dash_pos.left_in_dash;
942 if(cur_y + dash_len > clipped_bottom + 1)
943 dash_len = clipped_bottom - cur_y + 1;
944 rect.top = cur_y;
945 rect.bottom = cur_y + dash_len;
947 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
948 cur_y += dash_len;
949 skip_dash(pdev, dash_len);
952 else
954 cur_y = clipped_bottom;
955 if(cur_y != bottom)
956 skip_dash(pdev, bottom - clipped_bottom);
958 while(cur_y >= clipped_top)
960 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
961 dash_len = pdev->dash_pos.left_in_dash;
962 if(cur_y - dash_len < clipped_top - 1)
963 dash_len = cur_y - clipped_top + 1;
964 rect.top = cur_y - dash_len + 1;
965 rect.bottom = cur_y + 1;
967 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
968 cur_y -= dash_len;
969 skip_dash(pdev, dash_len);
974 pdev->dash_pos = start_pos;
975 skip_dash(pdev, bottom - top + 1);
977 else
979 bres_params clip_params;
980 struct line_params line_params;
981 INT dx = end->x - start->x, dy = end->y - start->y;
982 INT abs_dx = abs(dx), abs_dy = abs(dy);
984 clip_params.dx = abs_dx;
985 clip_params.dy = abs_dy;
986 clip_params.octant = get_octant_mask(dx, dy);
987 clip_params.bias = get_bias( clip_params.octant );
989 line_params.bias = clip_params.bias;
990 line_params.x_major = is_xmajor( clip_params.octant );
991 line_params.x_inc = is_x_increasing( clip_params.octant ) ? 1 : -1;
992 line_params.y_inc = is_y_increasing( clip_params.octant ) ? 1 : -1;
994 if (line_params.x_major)
996 line_params.err_add_1 = 2 * abs_dy - 2 * abs_dx;
997 line_params.err_add_2 = 2 * abs_dy;
999 else
1001 line_params.err_add_1 = 2 * abs_dx - 2 * abs_dy;
1002 line_params.err_add_2 = 2 * abs_dx;
1005 rect.left = min( start->x, end->x );
1006 rect.top = min( start->y, end->y );
1007 rect.right = max( start->x, end->x ) + 1;
1008 rect.bottom = max( start->y, end->y ) + 1;
1009 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
1010 for (i = 0; i < clipped_rects.count; i++)
1012 POINT clipped_start, clipped_end;
1013 int clip_status;
1014 clip_status = clip_line(start, end, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
1016 if(clip_status)
1018 int m = abs(clipped_start.x - start->x);
1019 int n = abs(clipped_start.y - start->y);
1021 pdev->dash_pos = start_pos;
1023 if (line_params.x_major)
1025 line_params.err_start = 2 * abs_dy - abs_dx + m * 2 * abs_dy - n * 2 * abs_dx;
1026 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
1027 skip_dash(pdev, m);
1029 else
1031 line_params.err_start = 2 * abs_dx - abs_dy + n * 2 * abs_dx - m * 2 * abs_dy;
1032 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
1033 skip_dash(pdev, n);
1035 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
1037 bres_line_with_bias( &clipped_start, &line_params, dashed_pen_line_callback, pdev );
1039 if(clip_status == 2) break; /* completely unclipped, so we can finish */
1042 pdev->dash_pos = start_pos;
1043 if(line_params.x_major)
1044 skip_dash(pdev, abs_dx);
1045 else
1046 skip_dash(pdev, abs_dy);
1049 free_clipped_rects( &clipped_rects );
1050 return TRUE;
1053 static BOOL dashed_pen_line_region(dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region)
1055 int i, dash_len;
1056 RECT rect;
1058 rect.left = start->x;
1059 rect.top = start->y;
1060 rect.right = start->x + 1;
1061 rect.bottom = start->y + 1;
1063 if (start->y == end->y) /* hline */
1065 if (start->x <= end->x)
1067 for (i = start->x; i < end->x; i += dash_len)
1069 dash_len = min( pdev->dash_pos.left_in_dash, end->x - i );
1070 if (pdev->dash_pos.mark)
1072 rect.left = i;
1073 rect.right = i + dash_len;
1074 add_rect_to_region( region, &rect );
1076 skip_dash(pdev, dash_len);
1079 else
1081 for (i = start->x; i > end->x; i -= dash_len)
1083 dash_len = min( pdev->dash_pos.left_in_dash, i - end->x );
1084 if (pdev->dash_pos.mark)
1086 rect.left = i - dash_len + 1;
1087 rect.right = i + 1;
1088 add_rect_to_region( region, &rect );
1090 skip_dash(pdev, dash_len);
1094 else if (start->x == end->x) /* vline */
1096 if (start->y <= end->y)
1098 for (i = start->y; i < end->y; i += dash_len)
1100 dash_len = min( pdev->dash_pos.left_in_dash, end->y - i );
1101 if (pdev->dash_pos.mark)
1103 rect.top = i;
1104 rect.bottom = i + dash_len;
1105 add_rect_to_region( region, &rect );
1107 skip_dash(pdev, dash_len);
1110 else
1112 for (i = start->y; i > end->y; i -= dash_len)
1114 dash_len = min( pdev->dash_pos.left_in_dash, i - end->y );
1115 if (pdev->dash_pos.mark)
1117 rect.top = i - dash_len + 1;
1118 rect.bottom = i + 1;
1119 add_rect_to_region( region, &rect );
1121 skip_dash(pdev, dash_len);
1125 else
1127 INT dx = end->x - start->x, dy = end->y - start->y;
1128 INT abs_dx = abs(dx), abs_dy = abs(dy);
1129 DWORD octant = get_octant_mask(dx, dy);
1130 INT bias = get_bias( octant );
1131 int x_inc = is_x_increasing( octant ) ? 1 : -1;
1132 int y_inc = is_y_increasing( octant ) ? 1 : -1;
1134 if (is_xmajor( octant ))
1136 int err_add_1 = 2 * abs_dy - 2 * abs_dx;
1137 int err_add_2 = 2 * abs_dy;
1138 int err = 2 * abs_dy - abs_dx;
1140 while (abs_dx--)
1142 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1143 skip_dash(pdev, 1);
1144 rect.left += x_inc;
1145 rect.right += x_inc;
1146 if (err + bias > 0)
1148 rect.top += y_inc;
1149 rect.bottom += y_inc;
1150 err += err_add_1;
1152 else err += err_add_2;
1156 else
1158 int err_add_1 = 2 * abs_dx - 2 * abs_dy;
1159 int err_add_2 = 2 * abs_dx;
1160 int err = 2 * abs_dx - abs_dy;
1162 while (abs_dy--)
1164 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1165 skip_dash(pdev, 1);
1166 rect.top += y_inc;
1167 rect.bottom += y_inc;
1168 if (err + bias > 0)
1170 rect.left += x_inc;
1171 rect.right += x_inc;
1172 err += err_add_1;
1174 else err += err_add_2;
1178 return TRUE;
1181 static BOOL dashed_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1183 int i;
1185 assert( num >= 2 );
1187 if (region)
1189 for (i = 0; i < num - 1; i++)
1190 if (!dashed_pen_line_region( pdev, pts + i, pts + i + 1, region ))
1191 return FALSE;
1192 if (close) return dashed_pen_line_region( pdev, pts + num - 1, pts, region );
1194 else
1196 get_color_masks( pdev, GetROP2(pdev->dev.hdc), pdev->pen_brush.colorref,
1197 pdev->pen_is_ext ? TRANSPARENT : GetBkMode(pdev->dev.hdc),
1198 &pdev->dash_masks[1], &pdev->dash_masks[0] );
1200 for (i = 0; i < num - 1; i++)
1201 if (!dashed_pen_line( pdev, pts + i, pts + i + 1 ))
1202 return FALSE;
1203 if (close) return dashed_pen_line( pdev, pts + num - 1, pts );
1205 return TRUE;
1208 static BOOL null_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1210 return TRUE;
1213 struct face
1215 POINT start, end;
1216 int dx, dy;
1219 static void add_cap( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt )
1221 switch (pdev->pen_endcap)
1223 default: FIXME( "Unknown end cap %x\n", pdev->pen_endcap );
1224 /* fall through */
1225 case PS_ENDCAP_ROUND:
1226 OffsetRgn( round_cap, pt->x, pt->y );
1227 CombineRgn( region, region, round_cap, RGN_OR );
1228 OffsetRgn( round_cap, -pt->x, -pt->y );
1229 return;
1231 case PS_ENDCAP_SQUARE: /* already been handled */
1232 case PS_ENDCAP_FLAT:
1233 return;
1237 #define round( f ) (((f) > 0) ? (f) + 0.5 : (f) - 0.5)
1239 /*******************************************************************************
1240 * create_miter_region
1242 * We need to calculate the intersection of two lines. We know a point
1243 * on each line (a face start and the other face end point) and
1244 * the direction vector of each line eg. (dx_1, dy_1).
1246 * (x, y) = (x_1, y_1) + u * (dx_1, dy_1) = (x_2, y_2) + v * (dx_2, dy_2)
1247 * solving (eg using Cramer's rule) gives:
1248 * u = ((x_2 - x_1) dy_2 - (y_2 - y_1) dx_2) / det
1249 * with det = dx_1 dy_2 - dx_2 dy_1
1250 * substituting back in and simplifying gives
1251 * (x, y) = a (dx_1, dy_1) - b (dx_2, dy_2)
1252 * with a = (x_2 dy_2 - y_2 dx_2) / det
1253 * and b = (x_1 dy_1 - y_1 dx_1) / det
1255 static HRGN create_miter_region( dibdrv_physdev *pdev, const POINT *pt,
1256 const struct face *face_1, const struct face *face_2 )
1258 int det = face_1->dx * face_2->dy - face_1->dy * face_2->dx;
1259 POINT pt_1, pt_2, pts[5];
1260 double a, b, x, y;
1261 FLOAT limit;
1263 if (det == 0) return 0;
1265 if (det < 0)
1267 const struct face *tmp = face_1;
1268 face_1 = face_2;
1269 face_2 = tmp;
1270 det = -det;
1273 pt_1 = face_1->start;
1274 pt_2 = face_2->end;
1276 a = (double)((pt_2.x * face_2->dy - pt_2.y * face_2->dx)) / det;
1277 b = (double)((pt_1.x * face_1->dy - pt_1.y * face_1->dx)) / det;
1279 x = a * face_1->dx - b * face_2->dx;
1280 y = a * face_1->dy - b * face_2->dy;
1282 GetMiterLimit( pdev->dev.hdc, &limit );
1284 if (((x - pt->x) * (x - pt->x) + (y - pt->y) * (y - pt->y)) * 4 > limit * limit * pdev->pen_width * pdev->pen_width)
1285 return 0;
1287 pts[0] = face_2->start;
1288 pts[1] = face_1->start;
1289 pts[2].x = round( x );
1290 pts[2].y = round( y );
1291 pts[3] = face_2->end;
1292 pts[4] = face_1->end;
1294 return CreatePolygonRgn( pts, 5, ALTERNATE );
1297 static void add_join( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt,
1298 const struct face *face_1, const struct face *face_2 )
1300 HRGN join;
1301 POINT pts[4];
1303 switch (pdev->pen_join)
1305 default: FIXME( "Unknown line join %x\n", pdev->pen_join );
1306 /* fall through */
1307 case PS_JOIN_ROUND:
1308 OffsetRgn( round_cap, pt->x, pt->y );
1309 CombineRgn( region, region, round_cap, RGN_OR );
1310 OffsetRgn( round_cap, -pt->x, -pt->y );
1311 return;
1313 case PS_JOIN_MITER:
1314 join = create_miter_region( pdev, pt, face_1, face_2 );
1315 if (join) break;
1316 /* fall through */
1317 case PS_JOIN_BEVEL:
1318 pts[0] = face_1->start;
1319 pts[1] = face_2->end;
1320 pts[2] = face_1->end;
1321 pts[3] = face_2->start;
1322 join = CreatePolygonRgn( pts, 4, ALTERNATE );
1323 break;
1326 CombineRgn( region, region, join, RGN_OR );
1327 DeleteObject( join );
1328 return;
1331 static int wide_line_segment( dibdrv_physdev *pdev, HRGN total,
1332 const POINT *pt_1, const POINT *pt_2, int dx, int dy,
1333 BOOL need_cap_1, BOOL need_cap_2, struct face *face_1, struct face *face_2 )
1335 RECT rect;
1336 BOOL sq_cap_1 = need_cap_1 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1337 BOOL sq_cap_2 = need_cap_2 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1339 if (dx == 0 && dy == 0) return 0;
1341 if (dy == 0)
1343 rect.left = min( pt_1->x, pt_2->x );
1344 rect.right = max( pt_1->x, pt_2->x );
1345 rect.top = pt_1->y - pdev->pen_width / 2;
1346 rect.bottom = rect.top + pdev->pen_width;
1347 if ((sq_cap_1 && dx > 0) || (sq_cap_2 && dx < 0)) rect.left -= pdev->pen_width / 2;
1348 if ((sq_cap_2 && dx > 0) || (sq_cap_1 && dx < 0)) rect.right += pdev->pen_width / 2;
1349 add_rect_to_region( total, &rect );
1350 if (dx > 0)
1352 face_1->start.x = face_1->end.x = rect.left;
1353 face_1->start.y = face_2->end.y = rect.bottom;
1354 face_1->end.y = face_2->start.y = rect.top;
1355 face_2->start.x = face_2->end.x = rect.right - 1;
1357 else
1359 face_1->start.x = face_1->end.x = rect.right;
1360 face_1->start.y = face_2->end.y = rect.top;
1361 face_1->end.y = face_2->start.y = rect.bottom;
1362 face_2->start.x = face_2->end.x = rect.left + 1;
1365 else if (dx == 0)
1367 rect.top = min( pt_1->y, pt_2->y );
1368 rect.bottom = max( pt_1->y, pt_2->y );
1369 rect.left = pt_1->x - pdev->pen_width / 2;
1370 rect.right = rect.left + pdev->pen_width;
1371 if ((sq_cap_1 && dy > 0) || (sq_cap_2 && dy < 0)) rect.top -= pdev->pen_width / 2;
1372 if ((sq_cap_2 && dy > 0) || (sq_cap_1 && dy < 0)) rect.bottom += pdev->pen_width / 2;
1373 add_rect_to_region( total, &rect );
1374 if (dy > 0)
1376 face_1->start.x = face_2->end.x = rect.left;
1377 face_1->start.y = face_1->end.y = rect.top;
1378 face_1->end.x = face_2->start.x = rect.right;
1379 face_2->start.y = face_2->end.y = rect.bottom - 1;
1381 else
1383 face_1->start.x = face_2->end.x = rect.right;
1384 face_1->start.y = face_1->end.y = rect.bottom;
1385 face_1->end.x = face_2->start.x = rect.left;
1386 face_2->start.y = face_2->end.y = rect.top + 1;
1389 else
1391 double len = hypot( dx, dy );
1392 double width_x, width_y;
1393 POINT seg_pts[4];
1394 POINT wide_half, narrow_half;
1395 HRGN segment;
1397 width_x = pdev->pen_width * abs( dy ) / len;
1398 width_y = pdev->pen_width * abs( dx ) / len;
1400 narrow_half.x = round( width_x / 2 );
1401 narrow_half.y = round( width_y / 2 );
1402 wide_half.x = round( (width_x + 1) / 2 );
1403 wide_half.y = round( (width_y + 1) / 2 );
1405 if (dx < 0)
1407 wide_half.y = -wide_half.y;
1408 narrow_half.y = -narrow_half.y;
1411 if (dy < 0)
1413 POINT tmp = narrow_half; narrow_half = wide_half; wide_half = tmp;
1414 wide_half.x = -wide_half.x;
1415 narrow_half.x = -narrow_half.x;
1418 seg_pts[0].x = pt_1->x - narrow_half.x;
1419 seg_pts[0].y = pt_1->y + narrow_half.y;
1420 seg_pts[1].x = pt_1->x + wide_half.x;
1421 seg_pts[1].y = pt_1->y - wide_half.y;
1422 seg_pts[2].x = pt_2->x + wide_half.x;
1423 seg_pts[2].y = pt_2->y - wide_half.y;
1424 seg_pts[3].x = pt_2->x - narrow_half.x;
1425 seg_pts[3].y = pt_2->y + narrow_half.y;
1427 if (sq_cap_1)
1429 seg_pts[0].x -= narrow_half.y;
1430 seg_pts[1].x -= narrow_half.y;
1431 seg_pts[0].y -= narrow_half.x;
1432 seg_pts[1].y -= narrow_half.x;
1435 if (sq_cap_2)
1437 seg_pts[2].x += wide_half.y;
1438 seg_pts[3].x += wide_half.y;
1439 seg_pts[2].y += wide_half.x;
1440 seg_pts[3].y += wide_half.x;
1443 segment = CreatePolygonRgn( seg_pts, 4, ALTERNATE );
1444 CombineRgn( total, total, segment, RGN_OR );
1445 DeleteObject( segment );
1447 face_1->start = seg_pts[0];
1448 face_1->end = seg_pts[1];
1449 face_2->start = seg_pts[2];
1450 face_2->end = seg_pts[3];
1453 face_1->dx = face_2->dx = dx;
1454 face_1->dy = face_2->dy = dy;
1456 return 1;
1459 static void wide_line_segments( dibdrv_physdev *pdev, int num, const POINT *pts, BOOL close,
1460 int start, int count, const POINT *first_pt, const POINT *last_pt,
1461 HRGN round_cap, HRGN total )
1463 int i;
1464 struct face face_1, face_2, prev_face, first_face;
1465 const POINT *pt_1, *pt_2;
1467 if (!close)
1469 add_cap( pdev, total, round_cap, first_pt );
1470 add_cap( pdev, total, round_cap, last_pt );
1473 if (count == 1)
1475 pt_1 = &pts[start];
1476 pt_2 = &pts[(start + 1) % num];
1477 wide_line_segment( pdev, total, first_pt, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1478 TRUE, TRUE, &face_1, &face_2 );
1479 return;
1482 pt_1 = &pts[start];
1483 pt_2 = &pts[(start + 1) % num];
1484 wide_line_segment( pdev, total, first_pt, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1485 !close, FALSE, &first_face, &prev_face );
1488 for (i = 1; i < count - 1; i++)
1490 pt_1 = &pts[(start + i) % num];
1491 pt_2 = &pts[(start + i + 1) % num];
1492 if (wide_line_segment( pdev, total, pt_1, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1493 FALSE, FALSE, &face_1, &face_2 ))
1495 add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1496 prev_face = face_2;
1500 pt_1 = &pts[(start + count - 1) % num];
1501 pt_2 = &pts[(start + count) % num];
1502 wide_line_segment( pdev, total, pt_1, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1503 FALSE, !close, &face_1, &face_2 );
1504 add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1505 if (close) add_join( pdev, total, round_cap, last_pt, &face_2, &first_face );
1508 static BOOL wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1510 HRGN round_cap = 0;
1512 assert( total != 0 ); /* wide pens should always be drawn through a region */
1513 assert( num >= 2 );
1515 /* skip empty segments */
1516 while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1517 while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1519 if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1520 round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1521 (pdev->pen_width + 1) / 2 + 1, (pdev->pen_width + 1) / 2 + 1 );
1523 if (close)
1524 wide_line_segments( pdev, num, pts, TRUE, 0, num, &pts[0], &pts[0], round_cap, total );
1525 else
1526 wide_line_segments( pdev, num, pts, FALSE, 0, num - 1, &pts[0], &pts[num - 1], round_cap, total );
1528 if (round_cap) DeleteObject( round_cap );
1529 return TRUE;
1532 static BOOL dashed_wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1534 int i, start, cur_len, initial_num = 0;
1535 POINT initial_point, start_point, end_point;
1536 HRGN round_cap = 0;
1538 assert( total != 0 ); /* wide pens should always be drawn through a region */
1539 assert( num >= 2 );
1541 /* skip empty segments */
1542 while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1543 while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1545 if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1546 round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1547 (pdev->pen_width + 1) / 2 + 1, (pdev->pen_width + 1) / 2 + 1);
1549 start = 0;
1550 cur_len = 0;
1551 start_point = pts[0];
1553 for (i = 0; i < (close ? num : num - 1); i++)
1555 const POINT *pt_1 = pts + i;
1556 const POINT *pt_2 = pts + ((close && i == num - 1) ? 0 : i + 1);
1557 int dx = pt_2->x - pt_1->x;
1558 int dy = pt_2->y - pt_1->y;
1560 if (dx == 0 && dy == 0) continue;
1562 if (dy == 0)
1564 if (abs( dx ) - cur_len < pdev->dash_pos.left_in_dash)
1566 skip_dash( pdev, abs( dx ) - cur_len );
1567 cur_len = 0;
1568 continue;
1570 cur_len += pdev->dash_pos.left_in_dash;
1571 dx = (dx > 0) ? cur_len : -cur_len;
1573 else if (dx == 0)
1575 if (abs( dy ) - cur_len < pdev->dash_pos.left_in_dash)
1577 skip_dash( pdev, abs( dy ) - cur_len );
1578 cur_len = 0;
1579 continue;
1581 cur_len += pdev->dash_pos.left_in_dash;
1582 dy = (dy > 0) ? cur_len : -cur_len;
1584 else
1586 double len = hypot( dx, dy );
1588 if (len - cur_len < pdev->dash_pos.left_in_dash)
1590 skip_dash( pdev, len - cur_len );
1591 cur_len = 0;
1592 continue;
1594 cur_len += pdev->dash_pos.left_in_dash;
1595 dx = dx * cur_len / len;
1596 dy = dy * cur_len / len;
1598 end_point.x = pt_1->x + dx;
1599 end_point.y = pt_1->y + dy;
1601 if (pdev->dash_pos.mark)
1603 if (!initial_num && close) /* this is the first dash, save it for later */
1605 initial_num = i - start + 1;
1606 initial_point = end_point;
1608 else wide_line_segments( pdev, num, pts, FALSE, start, i - start + 1,
1609 &start_point, &end_point, round_cap, total );
1611 if (!initial_num) initial_num = -1; /* no need to close it */
1613 skip_dash( pdev, pdev->dash_pos.left_in_dash );
1614 start_point = end_point;
1615 start = i;
1616 i--; /* go on with the same segment */
1619 if (pdev->dash_pos.mark) /* we have a final dash */
1621 int count;
1623 if (initial_num > 0)
1625 count = num - start + initial_num;
1626 end_point = initial_point;
1628 else if (close)
1630 count = num - start;
1631 end_point = pts[0];
1633 else
1635 count = num - start - 1;
1636 end_point = pts[num - 1];
1638 wide_line_segments( pdev, num, pts, FALSE, start, count,
1639 &start_point, &end_point, round_cap, total );
1641 else if (initial_num > 0) /* initial dash only */
1643 wide_line_segments( pdev, num, pts, FALSE, 0, initial_num,
1644 &pts[0], &initial_point, round_cap, total );
1647 if (round_cap) DeleteObject( round_cap );
1648 return TRUE;
1651 static const dash_pattern dash_patterns_cosmetic[4] =
1653 {2, {18, 6}, 24}, /* PS_DASH */
1654 {2, {3, 3}, 6}, /* PS_DOT */
1655 {4, {9, 6, 3, 6}, 24}, /* PS_DASHDOT */
1656 {6, {9, 3, 3, 3, 3, 3}, 24} /* PS_DASHDOTDOT */
1659 static const dash_pattern dash_patterns_geometric[4] =
1661 {2, {3, 1}, 4}, /* PS_DASH */
1662 {2, {1, 1}, 2}, /* PS_DOT */
1663 {4, {3, 1, 1, 1}, 6}, /* PS_DASHDOT */
1664 {6, {3, 1, 1, 1, 1, 1}, 8} /* PS_DASHDOTDOT */
1667 static inline void set_dash_pattern( dash_pattern *pattern, DWORD count, DWORD *dashes )
1669 DWORD i;
1671 pattern->count = count;
1672 pattern->total_len = 0;
1673 memcpy( pattern->dashes, dashes, count * sizeof(DWORD) );
1674 for (i = 0; i < count; i++) pattern->total_len += dashes[i];
1675 if (pattern->count % 2) pattern->total_len *= 2;
1678 static inline void scale_dash_pattern( dash_pattern *pattern, DWORD scale, DWORD endcap )
1680 DWORD i;
1682 for (i = 0; i < pattern->count; i++) pattern->dashes[i] *= scale;
1683 pattern->total_len *= scale;
1685 if (endcap != PS_ENDCAP_FLAT) /* shrink the dashes to leave room for the caps */
1687 for (i = 0; i < pattern->count; i += 2)
1689 pattern->dashes[i] -= scale;
1690 pattern->dashes[i + 1] += scale;
1695 static inline int get_pen_device_width( dibdrv_physdev *pdev, int width )
1697 POINT pts[2];
1699 if (!width) return 1;
1700 pts[0].x = pts[0].y = pts[1].y = 0;
1701 pts[1].x = width;
1702 LPtoDP( pdev->dev.hdc, pts, 2 );
1703 width = abs( pts[1].x - pts[0].x );
1704 return max( width, 1 );
1707 /***********************************************************************
1708 * dibdrv_SetDCPenColor
1710 COLORREF dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1712 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1714 if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
1715 pdev->pen_brush.colorref = color;
1717 return color;
1720 /**********************************************************************
1721 * solid_brush
1723 * Fill a number of rectangles with the solid brush
1725 static BOOL solid_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1726 int num, const RECT *rects, INT rop)
1728 rop_mask brush_color;
1729 DWORD color = get_pixel_color( pdev, brush->colorref, TRUE );
1731 calc_rop_masks( rop, color, &brush_color );
1732 dib->funcs->solid_rects( dib, num, rects, brush_color.and, brush_color.xor );
1733 return TRUE;
1736 static BOOL alloc_brush_mask_bits( dib_brush *brush )
1738 DWORD size = brush->dib.height * abs(brush->dib.stride);
1740 assert(brush->masks.and == NULL);
1741 assert(brush->masks.xor == NULL);
1742 assert(brush->dib.stride > 0);
1744 if (!(brush->masks.and = HeapAlloc(GetProcessHeap(), 0, 2 * size))) return FALSE;
1745 brush->masks.xor = (char *)brush->masks.and + size;
1746 return TRUE;
1749 static void free_brush_mask_bits( dib_brush *brush )
1751 HeapFree(GetProcessHeap(), 0, brush->masks.and);
1752 brush->masks.and = brush->masks.xor = NULL;
1755 void free_pattern_brush( dib_brush *brush )
1757 free_brush_mask_bits( brush );
1758 free_dib_info( &brush->dib );
1761 static BOOL create_pattern_brush_bits( dib_brush *brush )
1763 DWORD size = brush->dib.height * abs(brush->dib.stride);
1764 DWORD *brush_bits = brush->dib.bits.ptr;
1765 DWORD *and_bits, *xor_bits;
1767 if (!alloc_brush_mask_bits( brush )) return FALSE;
1769 and_bits = brush->masks.and;
1770 xor_bits = brush->masks.xor;
1772 while(size)
1774 calc_and_xor_masks(brush->rop, *brush_bits++, and_bits++, xor_bits++);
1775 size -= 4;
1778 return TRUE;
1781 static const BYTE hatches[6][8] =
1783 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1784 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
1785 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL */
1786 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL */
1787 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
1788 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
1791 static BOOL init_hatch_brush( dibdrv_physdev *pdev, dib_brush *brush )
1793 /* Just initialise brush dib with the color / sizing info. We don't
1794 need the bits as we'll calculate the rop masks straight from
1795 the hatch patterns. */
1797 copy_dib_color_info(&brush->dib, &pdev->dib);
1798 brush->dib.width = 8;
1799 brush->dib.height = 8;
1800 brush->dib.stride = get_dib_stride( brush->dib.width, brush->dib.bit_count );
1801 brush->dib.rect.left = 0;
1802 brush->dib.rect.top = 0;
1803 brush->dib.rect.right = 8;
1804 brush->dib.rect.bottom = 8;
1805 return alloc_brush_mask_bits( brush );
1808 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect)
1810 rop_mask fg_mask, bg_mask;
1812 if (!init_hatch_brush( pdev, brush )) return FALSE;
1814 get_color_masks( pdev, brush->rop, brush->colorref, GetBkMode(pdev->dev.hdc),
1815 &fg_mask, &bg_mask );
1817 if (brush->colorref & (1 << 24)) /* PALETTEINDEX */
1818 *needs_reselect = TRUE;
1819 if (GetBkMode(pdev->dev.hdc) != TRANSPARENT && (GetBkColor(pdev->dev.hdc) & (1 << 24)))
1820 *needs_reselect = TRUE;
1822 brush->dib.funcs->create_rop_masks( &brush->dib, hatches[brush->hatch],
1823 &fg_mask, &bg_mask, &brush->masks );
1824 return TRUE;
1827 static BOOL create_dither_brush_bits(dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect)
1829 COLORREF rgb;
1830 DWORD pixel;
1831 BOOL got_pixel;
1833 if (!init_hatch_brush( pdev, brush )) return FALSE;
1835 if (brush->colorref & (1 << 24)) /* PALETTEINDEX */
1836 *needs_reselect = TRUE;
1838 rgb = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, brush->colorref, &got_pixel, &pixel );
1840 brush->dib.funcs->create_dither_masks( &brush->dib, brush->rop, rgb, &brush->masks );
1841 return TRUE;
1844 static BOOL matching_pattern_format( dib_info *dib, dib_info *pattern )
1846 if (dib->bit_count != pattern->bit_count) return FALSE;
1847 if (dib->stride != pattern->stride) return FALSE;
1849 switch (dib->bit_count)
1851 case 1:
1852 case 4:
1853 case 8:
1854 if (dib->color_table_size != pattern->color_table_size) return FALSE;
1855 return !memcmp( dib->color_table, pattern->color_table, dib->color_table_size * sizeof(RGBQUAD) );
1856 case 16:
1857 case 32:
1858 return (dib->red_mask == pattern->red_mask &&
1859 dib->green_mask == pattern->green_mask &&
1860 dib->blue_mask == pattern->blue_mask);
1862 return TRUE;
1865 static BOOL select_pattern_brush( dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect )
1867 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1868 BITMAPINFO *info = (BITMAPINFO *)buffer;
1869 RGBQUAD color_table[2];
1870 dib_info pattern;
1871 BOOL dither = (brush->dib.bit_count == 1);
1873 if (pattern.bit_count == 1 && !pattern.color_table)
1874 dither = FALSE; /* monochrome DDB pattern brushes don't get dithered */
1876 if (brush->pattern.info->bmiHeader.biClrUsed && brush->pattern.usage == DIB_PAL_COLORS)
1878 copy_bitmapinfo( info, brush->pattern.info );
1879 fill_color_table_from_pal_colors( info, pdev->dev.hdc );
1880 init_dib_info_from_bitmapinfo( &pattern, info, brush->pattern.bits.ptr );
1881 *needs_reselect = TRUE;
1883 else
1885 init_dib_info_from_bitmapinfo( &pattern, brush->pattern.info, brush->pattern.bits.ptr );
1888 if (pattern.bit_count == 1 && !pattern.color_table &&
1889 (pdev->dib.bit_count != 1 || pdev->dib.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 );
1920 brush->dib.rect = pattern.rect;
1922 if (!dither && matching_pattern_format( &brush->dib, &pattern ))
1924 brush->dib.bits.ptr = pattern.bits.ptr;
1925 brush->dib.bits.is_copy = FALSE;
1926 brush->dib.bits.free = NULL;
1928 else
1930 brush->dib.bits.ptr = HeapAlloc( GetProcessHeap(), 0, brush->dib.height * brush->dib.stride );
1931 brush->dib.bits.is_copy = TRUE;
1932 brush->dib.bits.free = free_heap_bits;
1933 brush->dib.funcs->convert_to(&brush->dib, &pattern, &pattern.rect, dither);
1935 return TRUE;
1938 /**********************************************************************
1939 * pattern_brush
1941 * Fill a number of rectangles with the pattern brush
1942 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1944 static BOOL pattern_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1945 int num, const RECT *rects, INT rop)
1947 POINT origin;
1948 BOOL needs_reselect = FALSE;
1950 if (rop != brush->rop)
1952 free_brush_mask_bits( brush );
1953 brush->rop = rop;
1956 if(brush->masks.and == NULL)
1958 switch(brush->style)
1960 case BS_DIBPATTERN:
1961 if (!brush->dib.bits.ptr && !select_pattern_brush( pdev, brush, &needs_reselect ))
1962 return FALSE;
1963 if(!create_pattern_brush_bits( brush ))
1964 return FALSE;
1965 break;
1967 case BS_SOLID:
1968 if(!create_dither_brush_bits(pdev, brush, &needs_reselect))
1969 return FALSE;
1970 break;
1972 case BS_HATCHED:
1973 if(!create_hatch_brush_bits(pdev, brush, &needs_reselect))
1974 return FALSE;
1975 break;
1977 default:
1978 ERR("Unexpected brush style %d\n", brush->style);
1979 return FALSE;
1983 GetBrushOrgEx(pdev->dev.hdc, &origin);
1985 dib->funcs->pattern_rects( dib, num, rects, &origin, &brush->dib, &brush->masks );
1987 if (needs_reselect) free_pattern_brush( brush );
1988 return TRUE;
1991 static BOOL null_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1992 int num, const RECT *rects, INT rop)
1994 return TRUE;
1997 static BOOL brush_needs_dithering( dibdrv_physdev *pdev, COLORREF color )
1999 int i;
2000 RGBQUAD rgb;
2001 const RGBQUAD *color_table = get_default_color_table( pdev->dib.bit_count );
2003 if (!color_table) return FALSE;
2004 if (pdev->dib.color_table) return FALSE;
2005 if (color & (1 << 24)) return TRUE; /* PALETTEINDEX */
2006 if (color >> 16 == 0x10ff) return FALSE; /* DIBINDEX */
2008 rgb = rgbquad_from_colorref( color );
2009 for (i = 0; i < (1 << pdev->dib.bit_count); i++)
2010 if (rgbquad_equal( &color_table[i], &rgb )) return FALSE;
2012 return TRUE;
2015 static void select_brush( dibdrv_physdev *pdev, dib_brush *brush,
2016 const LOGBRUSH *logbrush, const struct brush_pattern *pattern )
2018 free_pattern_brush( brush );
2020 if (pattern)
2022 brush->style = BS_DIBPATTERN;
2023 brush->pattern = *pattern; /* brush is actually selected only when it's used */
2024 brush->rects = pattern_brush;
2026 else
2028 brush->style = logbrush->lbStyle;
2029 brush->colorref = logbrush->lbColor;
2030 brush->hatch = logbrush->lbHatch;
2032 switch (logbrush->lbStyle)
2034 case BS_NULL: brush->rects = null_brush; break;
2035 case BS_HATCHED: brush->rects = pattern_brush; break;
2036 case BS_SOLID:
2037 brush->rects = brush_needs_dithering( pdev, brush->colorref ) ? pattern_brush : solid_brush;
2038 break;
2043 /***********************************************************************
2044 * dibdrv_SelectBrush
2046 HBRUSH dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush, const struct brush_pattern *pattern )
2048 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2049 LOGBRUSH logbrush;
2051 TRACE("(%p, %p)\n", dev, hbrush);
2053 GetObjectW( hbrush, sizeof(logbrush), &logbrush );
2055 if (hbrush == GetStockObject( DC_BRUSH ))
2056 logbrush.lbColor = GetDCBrushColor( dev->hdc );
2058 select_brush( pdev, &pdev->brush, &logbrush, pattern );
2059 return hbrush;
2062 /***********************************************************************
2063 * dibdrv_SelectPen
2065 HPEN dibdrv_SelectPen( PHYSDEV dev, HPEN hpen, const struct brush_pattern *pattern )
2067 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2068 LOGPEN logpen;
2069 LOGBRUSH logbrush;
2070 EXTLOGPEN *elp = NULL;
2072 TRACE("(%p, %p)\n", dev, hpen);
2074 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
2076 /* must be an extended pen */
2077 INT size = GetObjectW( hpen, 0, NULL );
2079 if (!size) return 0;
2081 elp = HeapAlloc( GetProcessHeap(), 0, size );
2083 GetObjectW( hpen, size, elp );
2084 logpen.lopnStyle = elp->elpPenStyle;
2085 logpen.lopnWidth.x = elp->elpWidth;
2086 /* cosmetic ext pens are always 1-pixel wide */
2087 if (!(logpen.lopnStyle & PS_GEOMETRIC)) logpen.lopnWidth.x = 0;
2089 logbrush.lbStyle = elp->elpBrushStyle;
2090 logbrush.lbColor = elp->elpColor;
2091 logbrush.lbHatch = elp->elpHatch;
2093 else
2095 logbrush.lbStyle = BS_SOLID;
2096 logbrush.lbColor = logpen.lopnColor;
2097 logbrush.lbHatch = 0;
2100 pdev->pen_join = logpen.lopnStyle & PS_JOIN_MASK;
2101 pdev->pen_endcap = logpen.lopnStyle & PS_ENDCAP_MASK;
2102 pdev->pen_width = get_pen_device_width( pdev, logpen.lopnWidth.x );
2104 if (hpen == GetStockObject( DC_PEN ))
2105 logbrush.lbColor = GetDCPenColor( dev->hdc );
2107 set_dash_pattern( &pdev->pen_pattern, 0, NULL );
2108 select_brush( pdev, &pdev->pen_brush, &logbrush, pattern );
2110 pdev->pen_style = logpen.lopnStyle & PS_STYLE_MASK;
2112 switch (pdev->pen_style)
2114 case PS_DASH:
2115 case PS_DOT:
2116 case PS_DASHDOT:
2117 case PS_DASHDOTDOT:
2118 if (logpen.lopnStyle & PS_GEOMETRIC)
2120 pdev->pen_pattern = dash_patterns_geometric[pdev->pen_style - 1];
2121 if (pdev->pen_width > 1)
2123 scale_dash_pattern( &pdev->pen_pattern, pdev->pen_width, pdev->pen_endcap );
2124 pdev->pen_lines = dashed_wide_pen_lines;
2126 else pdev->pen_lines = dashed_pen_lines;
2127 break;
2129 if (pdev->pen_width == 1) /* wide cosmetic pens are not dashed */
2131 pdev->pen_lines = dashed_pen_lines;
2132 pdev->pen_pattern = dash_patterns_cosmetic[pdev->pen_style - 1];
2133 break;
2135 /* fall through */
2136 case PS_SOLID:
2137 case PS_INSIDEFRAME:
2138 pdev->pen_lines = (pdev->pen_width == 1) ? solid_pen_lines : wide_pen_lines;
2139 break;
2141 case PS_NULL:
2142 pdev->pen_width = 0;
2143 pdev->pen_lines = null_pen_lines;
2144 break;
2146 case PS_ALTERNATE:
2147 pdev->pen_lines = dashed_pen_lines;
2148 pdev->pen_pattern = dash_patterns_geometric[PS_DOT - 1];
2149 break;
2151 case PS_USERSTYLE:
2152 pdev->pen_lines = (pdev->pen_width == 1) ? dashed_pen_lines : dashed_wide_pen_lines;
2153 set_dash_pattern( &pdev->pen_pattern, elp->elpNumEntries, elp->elpStyleEntry );
2154 if (!(logpen.lopnStyle & PS_GEOMETRIC)) scale_dash_pattern( &pdev->pen_pattern, 3, PS_ENDCAP_FLAT );
2155 break;
2158 pdev->pen_uses_region = (logpen.lopnStyle & PS_GEOMETRIC || pdev->pen_width > 1);
2159 pdev->pen_is_ext = (elp != NULL);
2160 HeapFree( GetProcessHeap(), 0, elp );
2161 return hpen;
2164 /***********************************************************************
2165 * dibdrv_SetDCBrushColor
2167 COLORREF dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
2169 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2171 if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
2173 LOGBRUSH logbrush = { BS_SOLID, color, 0 };
2174 select_brush( pdev, &pdev->brush, &logbrush, NULL );
2176 return color;
2179 BOOL brush_rect(dibdrv_physdev *pdev, dib_brush *brush, const RECT *rect, HRGN clip, INT rop)
2181 struct clipped_rects clipped_rects;
2182 BOOL ret;
2184 if (!get_clipped_rects( &pdev->dib, rect, clip, &clipped_rects )) return TRUE;
2185 ret = brush->rects( pdev, brush, &pdev->dib, clipped_rects.count, clipped_rects.rects, rop );
2186 free_clipped_rects( &clipped_rects );
2187 return ret;