gdi32: Clip solid pen regions to the DIB rectangle to avoid overflows.
[wine.git] / dlls / gdi32 / dibdrv / objects.c
blobf9736f66a880ae0bae8ea437b3da1a8750af0cd2
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 /* crop coordinates to a reasonable range to avoid overflows in calculations */
304 static inline POINT crop_coords( POINT pt )
306 if (pt.x >= 0x10000000 || pt.x <= -0x10000000 || pt.y >= 0x10000000 || pt.y <= -0x10000000)
308 pt.x /= 8;
309 pt.y /= 8;
311 return pt;
314 static void init_bres_params( const POINT *start, const POINT *end, bres_params *clip_params,
315 struct line_params *line_params, RECT *rect )
317 INT dx = end->x - start->x, dy = end->y - start->y;
318 INT abs_dx = abs(dx), abs_dy = abs(dy);
320 clip_params->dx = abs_dx;
321 clip_params->dy = abs_dy;
322 clip_params->octant = get_octant_mask(dx, dy);
323 clip_params->bias = get_bias( clip_params->octant );
325 line_params->bias = clip_params->bias;
326 line_params->x_major = is_xmajor( clip_params->octant );
327 line_params->x_inc = is_x_increasing( clip_params->octant ) ? 1 : -1;
328 line_params->y_inc = is_y_increasing( clip_params->octant ) ? 1 : -1;
330 if (line_params->x_major)
332 line_params->err_add_1 = 2 * abs_dy - 2 * abs_dx;
333 line_params->err_add_2 = 2 * abs_dy;
335 else
337 line_params->err_add_1 = 2 * abs_dx - 2 * abs_dy;
338 line_params->err_add_2 = 2 * abs_dx;
341 rect->left = min( start->x, end->x );
342 rect->top = min( start->y, end->y );
343 rect->right = max( start->x, end->x ) + 1;
344 rect->bottom = max( start->y, end->y ) + 1;
347 /******************************************************************************
348 * clip_line
350 * Clips the start and end points to a rectangle.
352 * Note, this treats the end point like the start point. If the
353 * caller doesn't want it displayed, it should exclude it. If the end
354 * point is clipped out, then the likelihood is that the new end point
355 * should be displayed.
357 * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
359 * This derivation is based on the comments in X.org's xserver/mi/mizerclip.c,
360 * however the Bresenham error term is defined differently so the equations
361 * will also differ.
363 * For x major lines we have 2dy >= err + bias > 2dy - 2dx
364 * 0 >= err + bias - 2dy > -2dx
366 * Note dx, dy, m and n are all +ve.
368 * Moving the start pt from x1 to x1 + m, we need to figure out y1 + n.
369 * err = 2dy - dx + 2mdy - 2ndx
370 * 0 >= 2dy - dx + 2mdy - 2ndx + bias - 2dy > -2dx
371 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
372 * which of course will give exactly one solution for n,
373 * so looking at the >= inequality
374 * n >= (2mdy + bias - dx) / 2dx
375 * n = ceiling((2mdy + bias - dx) / 2dx)
376 * = (2mdy + bias + dx - 1) / 2dx (assuming division truncation)
378 * Moving start pt from y1 to y1 + n we need to figure out x1 + m - there may be several
379 * solutions we pick the one that minimizes m (ie that first unlipped pt). As above:
380 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
381 * 2mdy > 2ndx - bias - dx
382 * m > (2ndx - bias - dx) / 2dy
383 * m = floor((2ndx - bias - dx) / 2dy) + 1
384 * m = (2ndx - bias - dx) / 2dy + 1
386 * Moving end pt from x2 to x2 - m, we need to figure out y2 - n
387 * err = 2dy - dx + 2(dx - m)dy - 2(dy - n)dx
388 * = 2dy - dx - 2mdy + 2ndx
389 * 0 >= 2dy - dx - 2mdy + 2ndx + bias - 2dy > -2dx
390 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
391 * again exactly one solution.
392 * 2ndx <= 2mdy - bias + dx
393 * n = floor((2mdy - bias + dx) / 2dx)
394 * = (2mdy - bias + dx) / 2dx
396 * Moving end pt from y2 to y2 - n when need x2 - m this time maximizing x2 - m so
397 * mininizing m to include all of the points at y = y2 - n. As above:
398 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
399 * 2mdy >= 2ndx + bias - dx
400 * m = ceiling((2ndx + bias - dx) / 2dy)
401 * = (2ndx + bias - dx - 1) / 2dy + 1
403 * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
405 * Moving start point from y1 to y1 + n find x1 + m
406 * m = (2ndx + bias + dy - 1) / 2dy
408 * Moving start point from x1 to x1 + m find y1 + n
409 * n = (2mdy - bias - dy) / 2ndx + 1
411 * Moving end point from y2 to y2 - n find x1 - m
412 * m = (2ndx - bias + dy) / 2dy
414 * Moving end point from x2 to x2 - m find y2 - n
415 * n = (2mdy + bias - dy - 1) / 2dx + 1
417 int clip_line(const POINT *start, const POINT *end, const RECT *clip,
418 const bres_params *params, POINT *pt1, POINT *pt2)
421 INT64 m, n; /* 64-bit to avoid overflows (FIXME: find a more efficient way) */
422 BOOL clipped = FALSE;
423 DWORD start_oc, end_oc;
424 const int bias = params->bias;
425 const unsigned int dx = params->dx;
426 const unsigned int dy = params->dy;
427 const unsigned int two_dx = params->dx * 2;
428 const unsigned int two_dy = params->dy * 2;
429 const BOOL xmajor = is_xmajor(params->octant);
430 const BOOL neg_slope = !is_pos_slope(params->octant);
432 *pt1 = *start;
433 *pt2 = *end;
435 start_oc = calc_outcode(start, clip);
436 end_oc = calc_outcode(end, clip);
438 while(1)
440 if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
441 if(start_oc & end_oc) return 0; /* trivial reject */
443 clipped = TRUE;
444 if(start_oc & OUT_LEFT)
446 m = clip->left - start->x;
447 if(xmajor)
448 n = (m * two_dy + bias + dx - 1) / two_dx;
449 else
450 n = (m * two_dy - bias - dy) / two_dx + 1;
452 pt1->x = clip->left;
453 if(neg_slope) n = -n;
454 pt1->y = start->y + n;
455 start_oc = calc_outcode(pt1, clip);
457 else if(start_oc & OUT_RIGHT)
459 m = start->x - clip->right + 1;
460 if(xmajor)
461 n = (m * two_dy + bias + dx - 1) / two_dx;
462 else
463 n = (m * two_dy - bias - dy) / two_dx + 1;
465 pt1->x = clip->right - 1;
466 if(neg_slope) n = -n;
467 pt1->y = start->y - n;
468 start_oc = calc_outcode(pt1, clip);
470 else if(start_oc & OUT_TOP)
472 n = clip->top - start->y;
473 if(xmajor)
474 m = (n * two_dx - bias - dx) / two_dy + 1;
475 else
476 m = (n * two_dx + bias + dy - 1) / two_dy;
478 pt1->y = clip->top;
479 if(neg_slope) m = -m;
480 pt1->x = start->x + m;
481 start_oc = calc_outcode(pt1, clip);
483 else if(start_oc & OUT_BOTTOM)
485 n = start->y - clip->bottom + 1;
486 if(xmajor)
487 m = (n * two_dx - bias - dx) / two_dy + 1;
488 else
489 m = (n * two_dx + bias + dy - 1) / two_dy;
491 pt1->y = clip->bottom - 1;
492 if(neg_slope) m = -m;
493 pt1->x = start->x - m;
494 start_oc = calc_outcode(pt1, clip);
496 else if(end_oc & OUT_LEFT)
498 m = clip->left - end->x;
499 if(xmajor)
500 n = (m * two_dy - bias + dx) / two_dx;
501 else
502 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
504 pt2->x = clip->left;
505 if(neg_slope) n = -n;
506 pt2->y = end->y + n;
507 end_oc = calc_outcode(pt2, clip);
509 else if(end_oc & OUT_RIGHT)
511 m = end->x - clip->right + 1;
512 if(xmajor)
513 n = (m * two_dy - bias + dx) / two_dx;
514 else
515 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
517 pt2->x = clip->right - 1;
518 if(neg_slope) n = -n;
519 pt2->y = end->y - n;
520 end_oc = calc_outcode(pt2, clip);
522 else if(end_oc & OUT_TOP)
524 n = clip->top - end->y;
525 if(xmajor)
526 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
527 else
528 m = (n * two_dx - bias + dy) / two_dy;
530 pt2->y = clip->top;
531 if(neg_slope) m = -m;
532 pt2->x = end->x + m;
533 end_oc = calc_outcode(pt2, clip);
535 else if(end_oc & OUT_BOTTOM)
537 n = end->y - clip->bottom + 1;
538 if(xmajor)
539 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
540 else
541 m = (n * two_dx - bias + dy) / two_dy;
543 pt2->y = clip->bottom - 1;
544 if(neg_slope) m = -m;
545 pt2->x = end->x - m;
546 end_oc = calc_outcode(pt2, clip);
551 static void bres_line_with_bias(const POINT *start, const struct line_params *params,
552 void (* callback)(dibdrv_physdev*,INT,INT), dibdrv_physdev *pdev)
554 POINT pt = *start;
555 int len = params->length, err = params->err_start;
557 if (params->x_major)
559 while(len--)
561 callback(pdev, pt.x, pt.y);
562 if (err + params->bias > 0)
564 pt.y += params->y_inc;
565 err += params->err_add_1;
567 else err += params->err_add_2;
568 pt.x += params->x_inc;
571 else
573 while(len--)
575 callback(pdev, pt.x, pt.y);
576 if (err + params->bias > 0)
578 pt.x += params->x_inc;
579 err += params->err_add_1;
581 else err += params->err_add_2;
582 pt.y += params->y_inc;
587 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end, DWORD and, DWORD xor)
589 struct clipped_rects clipped_rects;
590 RECT rect;
591 int i;
593 if(start->y == end->y)
595 rect.left = start->x;
596 rect.top = start->y;
597 rect.right = end->x;
598 rect.bottom = end->y + 1;
599 order_end_points(&rect.left, &rect.right);
600 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
601 pdev->dib.funcs->solid_rects(&pdev->dib, clipped_rects.count, clipped_rects.rects, and, xor);
603 else if(start->x == end->x)
605 rect.left = start->x;
606 rect.top = start->y;
607 rect.right = end->x + 1;
608 rect.bottom = end->y;
609 order_end_points(&rect.top, &rect.bottom);
610 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
611 pdev->dib.funcs->solid_rects(&pdev->dib, clipped_rects.count, clipped_rects.rects, and, xor);
613 else
615 bres_params clip_params;
616 struct line_params line_params;
617 POINT p1 = crop_coords( *start ), p2 = crop_coords( *end );
619 init_bres_params( start, end, &clip_params, &line_params, &rect );
620 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
621 for (i = 0; i < clipped_rects.count; i++)
623 POINT clipped_start, clipped_end;
624 int clip_status;
626 clip_status = clip_line( &p1, &p2, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
627 if(clip_status)
629 int m = abs(clipped_start.x - p1.x);
630 int n = abs(clipped_start.y - p1.y);
632 if (line_params.x_major)
634 line_params.err_start = 2 * clip_params.dy - clip_params.dx
635 + m * 2 * clip_params.dy - n * 2 * clip_params.dx;
636 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
638 else
640 line_params.err_start = 2 * clip_params.dx - clip_params.dy
641 + n * 2 * clip_params.dx - m * 2 * clip_params.dy;
642 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
645 if (clipped_end.x == p2.x && clipped_end.y == p2.y) line_params.length--;
647 pdev->dib.funcs->solid_line( &pdev->dib, &clipped_start, &line_params, and, xor );
649 if(clip_status == 2) break; /* completely unclipped, so we can finish */
653 free_clipped_rects( &clipped_rects );
654 return TRUE;
657 static void solid_line_region( const dib_info *dib, const POINT *start, const struct line_params *params,
658 HRGN region )
660 int len, err = params->err_start;
661 RECT rect;
663 rect.left = start->x;
664 rect.top = start->y;
665 rect.right = start->x + 1;
666 rect.bottom = start->y + 1;
668 if (params->x_major)
670 if (params->x_inc > 0)
672 for (len = params->length; len; len--, rect.right++)
674 if (err + params->bias > 0)
676 add_rect_to_region( region, &rect );
677 rect.left = rect.right;
678 rect.top += params->y_inc;
679 rect.bottom += params->y_inc;
680 err += params->err_add_1;
682 else err += params->err_add_2;
685 else
687 for (len = params->length; len; len--, rect.left--)
689 if (err + params->bias > 0)
691 add_rect_to_region( region, &rect );
692 rect.right = rect.left;
693 rect.top += params->y_inc;
694 rect.bottom += params->y_inc;
695 err += params->err_add_1;
697 else err += params->err_add_2;
701 else
703 if (params->y_inc > 0)
705 for (len = params->length; len; len--, rect.bottom++)
707 if (err + params->bias > 0)
709 add_rect_to_region( region, &rect );
710 rect.top = rect.bottom;
711 rect.left += params->x_inc;
712 rect.right += params->x_inc;
713 err += params->err_add_1;
715 else err += params->err_add_2;
718 else
720 for (len = params->length; len; len--, rect.top--)
722 if (err + params->bias > 0)
724 add_rect_to_region( region, &rect );
725 rect.bottom = rect.top;
726 rect.left += params->x_inc;
727 rect.right += params->x_inc;
728 err += params->err_add_1;
730 else err += params->err_add_2;
734 /* add final rect */
735 add_rect_to_region( region, &rect );
738 static BOOL solid_pen_line_region( dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region )
740 RECT rect;
742 rect.left = start->x;
743 rect.top = start->y;
744 rect.right = start->x + 1;
745 rect.bottom = start->y + 1;
747 if (start->y == end->y)
749 rect.right = end->x;
750 order_end_points(&rect.left, &rect.right);
751 if (clip_rect_to_dib( &pdev->dib, &rect )) add_rect_to_region( region, &rect );
753 else if(start->x == end->x)
755 rect.bottom = end->y;
756 order_end_points(&rect.top, &rect.bottom);
757 if (clip_rect_to_dib( &pdev->dib, &rect )) add_rect_to_region( region, &rect );
759 else
761 bres_params clip_params;
762 struct line_params line_params;
763 POINT p1 = crop_coords( *start ), p2 = crop_coords( *end );
765 init_bres_params( start, end, &clip_params, &line_params, &rect );
766 if (clip_rect_to_dib( &pdev->dib, &rect ))
768 POINT clipped_start, clipped_end;
770 if (clip_line( &p1, &p2, &rect, &clip_params, &clipped_start, &clipped_end ))
772 int m = abs(clipped_start.x - p1.x);
773 int n = abs(clipped_start.y - p1.y);
775 if (line_params.x_major)
777 line_params.err_start = 2 * clip_params.dy - clip_params.dx
778 + m * 2 * clip_params.dy - n * 2 * clip_params.dx;
779 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
781 else
783 line_params.err_start = 2 * clip_params.dx - clip_params.dy
784 + n * 2 * clip_params.dx - m * 2 * clip_params.dy;
785 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
788 if (clipped_end.x == p2.x && clipped_end.y == p2.y) line_params.length--;
789 solid_line_region( &pdev->dib, &clipped_start, &line_params, region );
793 return TRUE;
796 static BOOL solid_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
798 int i;
800 assert( num >= 2 );
802 if (region)
804 for (i = 0; i < num - 1; i++)
805 if (!solid_pen_line_region( pdev, pts + i, pts + i + 1, region ))
806 return FALSE;
807 if (close) return solid_pen_line_region( pdev, pts + num - 1, pts, region );
809 else
811 DWORD color, and, xor;
813 color = get_pixel_color( pdev, pdev->pen_brush.colorref, TRUE );
814 calc_and_xor_masks( GetROP2(pdev->dev.hdc), color, &and, &xor );
816 for (i = 0; i < num - 1; i++)
817 if (!solid_pen_line( pdev, pts + i, pts + i + 1, and, xor ))
818 return FALSE;
819 if (close) return solid_pen_line( pdev, pts + num - 1, pts, and, xor );
821 return TRUE;
824 void reset_dash_origin(dibdrv_physdev *pdev)
826 pdev->dash_pos.cur_dash = 0;
827 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
828 pdev->dash_pos.mark = TRUE;
831 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
833 skip %= pdev->pen_pattern.total_len;
836 if(pdev->dash_pos.left_in_dash > skip)
838 pdev->dash_pos.left_in_dash -= skip;
839 return;
841 skip -= pdev->dash_pos.left_in_dash;
842 pdev->dash_pos.cur_dash++;
843 if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
844 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
845 pdev->dash_pos.mark = !pdev->dash_pos.mark;
847 while (skip);
850 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
852 RECT rect;
853 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
855 skip_dash(pdev, 1);
856 rect.left = x;
857 rect.right = x + 1;
858 rect.top = y;
859 rect.bottom = y + 1;
860 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
861 return;
864 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
866 struct clipped_rects clipped_rects;
867 int i, dash_len;
868 RECT rect;
869 const dash_pos start_pos = pdev->dash_pos;
871 if(start->y == end->y) /* hline */
873 BOOL l_to_r;
874 INT left, right, cur_x;
876 rect.top = start->y;
877 rect.bottom = start->y + 1;
879 if(start->x <= end->x)
881 left = start->x;
882 right = end->x - 1;
883 l_to_r = TRUE;
885 else
887 left = end->x + 1;
888 right = start->x;
889 l_to_r = FALSE;
892 rect.left = min( start->x, end->x );
893 rect.right = max( start->x, end->x ) + 1;
894 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
895 for (i = 0; i < clipped_rects.count; i++)
897 if(clipped_rects.rects[i].right > left && clipped_rects.rects[i].left <= right)
899 int clipped_left = max(clipped_rects.rects[i].left, left);
900 int clipped_right = min(clipped_rects.rects[i].right - 1, right);
902 pdev->dash_pos = start_pos;
904 if(l_to_r)
906 cur_x = clipped_left;
907 if(cur_x != left)
908 skip_dash(pdev, clipped_left - left);
910 while(cur_x <= clipped_right)
912 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
913 dash_len = pdev->dash_pos.left_in_dash;
914 if(cur_x + dash_len > clipped_right + 1)
915 dash_len = clipped_right - cur_x + 1;
916 rect.left = cur_x;
917 rect.right = cur_x + dash_len;
919 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
920 cur_x += dash_len;
921 skip_dash(pdev, dash_len);
924 else
926 cur_x = clipped_right;
927 if(cur_x != right)
928 skip_dash(pdev, right - clipped_right);
930 while(cur_x >= clipped_left)
932 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
933 dash_len = pdev->dash_pos.left_in_dash;
934 if(cur_x - dash_len < clipped_left - 1)
935 dash_len = cur_x - clipped_left + 1;
936 rect.left = cur_x - dash_len + 1;
937 rect.right = cur_x + 1;
939 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
940 cur_x -= dash_len;
941 skip_dash(pdev, dash_len);
946 pdev->dash_pos = start_pos;
947 skip_dash(pdev, right - left + 1);
949 else if(start->x == end->x) /* vline */
951 BOOL t_to_b;
952 INT top, bottom, cur_y;
954 rect.left = start->x;
955 rect.right = start->x + 1;
957 if(start->y <= end->y)
959 top = start->y;
960 bottom = end->y - 1;
961 t_to_b = TRUE;
963 else
965 top = end->y + 1;
966 bottom = start->y;
967 t_to_b = FALSE;
970 rect.top = min( start->y, end->y );
971 rect.bottom = max( start->y, end->y ) + 1;
972 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
973 for (i = 0; i < clipped_rects.count; i++)
975 if(clipped_rects.rects[i].right > start->x && clipped_rects.rects[i].left <= start->x)
977 int clipped_top = max(clipped_rects.rects[i].top, top);
978 int clipped_bottom = min(clipped_rects.rects[i].bottom - 1, bottom);
980 pdev->dash_pos = start_pos;
982 if(t_to_b)
984 cur_y = clipped_top;
985 if(cur_y != top)
986 skip_dash(pdev, clipped_top - top);
988 while(cur_y <= clipped_bottom)
990 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
991 dash_len = pdev->dash_pos.left_in_dash;
992 if(cur_y + dash_len > clipped_bottom + 1)
993 dash_len = clipped_bottom - cur_y + 1;
994 rect.top = cur_y;
995 rect.bottom = cur_y + dash_len;
997 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
998 cur_y += dash_len;
999 skip_dash(pdev, dash_len);
1002 else
1004 cur_y = clipped_bottom;
1005 if(cur_y != bottom)
1006 skip_dash(pdev, bottom - clipped_bottom);
1008 while(cur_y >= clipped_top)
1010 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
1011 dash_len = pdev->dash_pos.left_in_dash;
1012 if(cur_y - dash_len < clipped_top - 1)
1013 dash_len = cur_y - clipped_top + 1;
1014 rect.top = cur_y - dash_len + 1;
1015 rect.bottom = cur_y + 1;
1017 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
1018 cur_y -= dash_len;
1019 skip_dash(pdev, dash_len);
1024 pdev->dash_pos = start_pos;
1025 skip_dash(pdev, bottom - top + 1);
1027 else
1029 bres_params clip_params;
1030 struct line_params line_params;
1031 POINT p1 = crop_coords( *start ), p2 = crop_coords( *end );
1033 init_bres_params( start, end, &clip_params, &line_params, &rect );
1034 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
1035 for (i = 0; i < clipped_rects.count; i++)
1037 POINT clipped_start, clipped_end;
1038 int clip_status;
1039 clip_status = clip_line(&p1, &p2, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
1041 if(clip_status)
1043 int m = abs(clipped_start.x - p1.x);
1044 int n = abs(clipped_start.y - p1.y);
1046 pdev->dash_pos = start_pos;
1048 if (line_params.x_major)
1050 line_params.err_start = 2 * clip_params.dy - clip_params.dx
1051 + m * 2 * clip_params.dy - n * 2 * clip_params.dx;
1052 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
1053 skip_dash(pdev, m);
1055 else
1057 line_params.err_start = 2 * clip_params.dx - clip_params.dy
1058 + n * 2 * clip_params.dx - m * 2 * clip_params.dy;
1059 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
1060 skip_dash(pdev, n);
1062 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
1064 bres_line_with_bias( &clipped_start, &line_params, dashed_pen_line_callback, pdev );
1066 if(clip_status == 2) break; /* completely unclipped, so we can finish */
1069 pdev->dash_pos = start_pos;
1070 if(line_params.x_major)
1071 skip_dash(pdev, clip_params.dx);
1072 else
1073 skip_dash(pdev, clip_params.dy);
1076 free_clipped_rects( &clipped_rects );
1077 return TRUE;
1080 static BOOL dashed_pen_line_region(dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region)
1082 int i, dash_len;
1083 RECT rect;
1085 rect.left = start->x;
1086 rect.top = start->y;
1087 rect.right = start->x + 1;
1088 rect.bottom = start->y + 1;
1090 if (start->y == end->y) /* hline */
1092 if (start->x <= end->x)
1094 for (i = start->x; i < end->x; i += dash_len)
1096 dash_len = min( pdev->dash_pos.left_in_dash, end->x - i );
1097 if (pdev->dash_pos.mark)
1099 rect.left = i;
1100 rect.right = i + dash_len;
1101 add_rect_to_region( region, &rect );
1103 skip_dash(pdev, dash_len);
1106 else
1108 for (i = start->x; i > end->x; i -= dash_len)
1110 dash_len = min( pdev->dash_pos.left_in_dash, i - end->x );
1111 if (pdev->dash_pos.mark)
1113 rect.left = i - dash_len + 1;
1114 rect.right = i + 1;
1115 add_rect_to_region( region, &rect );
1117 skip_dash(pdev, dash_len);
1121 else if (start->x == end->x) /* vline */
1123 if (start->y <= end->y)
1125 for (i = start->y; i < end->y; i += dash_len)
1127 dash_len = min( pdev->dash_pos.left_in_dash, end->y - i );
1128 if (pdev->dash_pos.mark)
1130 rect.top = i;
1131 rect.bottom = i + dash_len;
1132 add_rect_to_region( region, &rect );
1134 skip_dash(pdev, dash_len);
1137 else
1139 for (i = start->y; i > end->y; i -= dash_len)
1141 dash_len = min( pdev->dash_pos.left_in_dash, i - end->y );
1142 if (pdev->dash_pos.mark)
1144 rect.top = i - dash_len + 1;
1145 rect.bottom = i + 1;
1146 add_rect_to_region( region, &rect );
1148 skip_dash(pdev, dash_len);
1152 else
1154 INT dx = end->x - start->x, dy = end->y - start->y;
1155 INT abs_dx = abs(dx), abs_dy = abs(dy);
1156 DWORD octant = get_octant_mask(dx, dy);
1157 INT bias = get_bias( octant );
1158 int x_inc = is_x_increasing( octant ) ? 1 : -1;
1159 int y_inc = is_y_increasing( octant ) ? 1 : -1;
1161 if (is_xmajor( octant ))
1163 int err_add_1 = 2 * abs_dy - 2 * abs_dx;
1164 int err_add_2 = 2 * abs_dy;
1165 int err = 2 * abs_dy - abs_dx;
1167 while (abs_dx--)
1169 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1170 skip_dash(pdev, 1);
1171 rect.left += x_inc;
1172 rect.right += x_inc;
1173 if (err + bias > 0)
1175 rect.top += y_inc;
1176 rect.bottom += y_inc;
1177 err += err_add_1;
1179 else err += err_add_2;
1183 else
1185 int err_add_1 = 2 * abs_dx - 2 * abs_dy;
1186 int err_add_2 = 2 * abs_dx;
1187 int err = 2 * abs_dx - abs_dy;
1189 while (abs_dy--)
1191 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1192 skip_dash(pdev, 1);
1193 rect.top += y_inc;
1194 rect.bottom += y_inc;
1195 if (err + bias > 0)
1197 rect.left += x_inc;
1198 rect.right += x_inc;
1199 err += err_add_1;
1201 else err += err_add_2;
1205 return TRUE;
1208 static BOOL dashed_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1210 int i;
1212 assert( num >= 2 );
1214 if (region)
1216 for (i = 0; i < num - 1; i++)
1217 if (!dashed_pen_line_region( pdev, pts + i, pts + i + 1, region ))
1218 return FALSE;
1219 if (close) return dashed_pen_line_region( pdev, pts + num - 1, pts, region );
1221 else
1223 get_color_masks( pdev, GetROP2(pdev->dev.hdc), pdev->pen_brush.colorref,
1224 pdev->pen_is_ext ? TRANSPARENT : GetBkMode(pdev->dev.hdc),
1225 &pdev->dash_masks[1], &pdev->dash_masks[0] );
1227 for (i = 0; i < num - 1; i++)
1228 if (!dashed_pen_line( pdev, pts + i, pts + i + 1 ))
1229 return FALSE;
1230 if (close) return dashed_pen_line( pdev, pts + num - 1, pts );
1232 return TRUE;
1235 static BOOL null_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1237 return TRUE;
1240 struct face
1242 POINT start, end;
1243 int dx, dy;
1246 static void add_cap( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt )
1248 switch (pdev->pen_endcap)
1250 default: FIXME( "Unknown end cap %x\n", pdev->pen_endcap );
1251 /* fall through */
1252 case PS_ENDCAP_ROUND:
1253 OffsetRgn( round_cap, pt->x, pt->y );
1254 CombineRgn( region, region, round_cap, RGN_OR );
1255 OffsetRgn( round_cap, -pt->x, -pt->y );
1256 return;
1258 case PS_ENDCAP_SQUARE: /* already been handled */
1259 case PS_ENDCAP_FLAT:
1260 return;
1264 #define round( f ) (((f) > 0) ? (f) + 0.5 : (f) - 0.5)
1266 /*******************************************************************************
1267 * create_miter_region
1269 * We need to calculate the intersection of two lines. We know a point
1270 * on each line (a face start and the other face end point) and
1271 * the direction vector of each line eg. (dx_1, dy_1).
1273 * (x, y) = (x_1, y_1) + u * (dx_1, dy_1) = (x_2, y_2) + v * (dx_2, dy_2)
1274 * solving (eg using Cramer's rule) gives:
1275 * u = ((x_2 - x_1) dy_2 - (y_2 - y_1) dx_2) / det
1276 * with det = dx_1 dy_2 - dx_2 dy_1
1277 * substituting back in and simplifying gives
1278 * (x, y) = a (dx_1, dy_1) - b (dx_2, dy_2)
1279 * with a = (x_2 dy_2 - y_2 dx_2) / det
1280 * and b = (x_1 dy_1 - y_1 dx_1) / det
1282 static HRGN create_miter_region( dibdrv_physdev *pdev, const POINT *pt,
1283 const struct face *face_1, const struct face *face_2 )
1285 int det = face_1->dx * face_2->dy - face_1->dy * face_2->dx;
1286 POINT pt_1, pt_2, pts[5];
1287 double a, b, x, y;
1288 FLOAT limit;
1290 if (det == 0) return 0;
1292 if (det < 0)
1294 const struct face *tmp = face_1;
1295 face_1 = face_2;
1296 face_2 = tmp;
1297 det = -det;
1300 pt_1 = face_1->start;
1301 pt_2 = face_2->end;
1303 a = (double)((pt_2.x * face_2->dy - pt_2.y * face_2->dx)) / det;
1304 b = (double)((pt_1.x * face_1->dy - pt_1.y * face_1->dx)) / det;
1306 x = a * face_1->dx - b * face_2->dx;
1307 y = a * face_1->dy - b * face_2->dy;
1309 GetMiterLimit( pdev->dev.hdc, &limit );
1311 if (((x - pt->x) * (x - pt->x) + (y - pt->y) * (y - pt->y)) * 4 > limit * limit * pdev->pen_width * pdev->pen_width)
1312 return 0;
1314 pts[0] = face_2->start;
1315 pts[1] = face_1->start;
1316 pts[2].x = round( x );
1317 pts[2].y = round( y );
1318 pts[3] = face_2->end;
1319 pts[4] = face_1->end;
1321 return CreatePolygonRgn( pts, 5, ALTERNATE );
1324 static void add_join( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt,
1325 const struct face *face_1, const struct face *face_2 )
1327 HRGN join;
1328 POINT pts[4];
1330 switch (pdev->pen_join)
1332 default: FIXME( "Unknown line join %x\n", pdev->pen_join );
1333 /* fall through */
1334 case PS_JOIN_ROUND:
1335 OffsetRgn( round_cap, pt->x, pt->y );
1336 CombineRgn( region, region, round_cap, RGN_OR );
1337 OffsetRgn( round_cap, -pt->x, -pt->y );
1338 return;
1340 case PS_JOIN_MITER:
1341 join = create_miter_region( pdev, pt, face_1, face_2 );
1342 if (join) break;
1343 /* fall through */
1344 case PS_JOIN_BEVEL:
1345 pts[0] = face_1->start;
1346 pts[1] = face_2->end;
1347 pts[2] = face_1->end;
1348 pts[3] = face_2->start;
1349 join = CreatePolygonRgn( pts, 4, ALTERNATE );
1350 break;
1353 CombineRgn( region, region, join, RGN_OR );
1354 DeleteObject( join );
1355 return;
1358 static int wide_line_segment( dibdrv_physdev *pdev, HRGN total,
1359 const POINT *pt_1, const POINT *pt_2, int dx, int dy,
1360 BOOL need_cap_1, BOOL need_cap_2, struct face *face_1, struct face *face_2 )
1362 RECT rect;
1363 BOOL sq_cap_1 = need_cap_1 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1364 BOOL sq_cap_2 = need_cap_2 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1366 if (dx == 0 && dy == 0) return 0;
1368 if (dy == 0)
1370 rect.left = min( pt_1->x, pt_2->x );
1371 rect.right = max( pt_1->x, pt_2->x );
1372 rect.top = pt_1->y - pdev->pen_width / 2;
1373 rect.bottom = rect.top + pdev->pen_width;
1374 if ((sq_cap_1 && dx > 0) || (sq_cap_2 && dx < 0)) rect.left -= pdev->pen_width / 2;
1375 if ((sq_cap_2 && dx > 0) || (sq_cap_1 && dx < 0)) rect.right += pdev->pen_width / 2;
1376 add_rect_to_region( total, &rect );
1377 if (dx > 0)
1379 face_1->start.x = face_1->end.x = rect.left;
1380 face_1->start.y = face_2->end.y = rect.bottom;
1381 face_1->end.y = face_2->start.y = rect.top;
1382 face_2->start.x = face_2->end.x = rect.right - 1;
1384 else
1386 face_1->start.x = face_1->end.x = rect.right;
1387 face_1->start.y = face_2->end.y = rect.top;
1388 face_1->end.y = face_2->start.y = rect.bottom;
1389 face_2->start.x = face_2->end.x = rect.left + 1;
1392 else if (dx == 0)
1394 rect.top = min( pt_1->y, pt_2->y );
1395 rect.bottom = max( pt_1->y, pt_2->y );
1396 rect.left = pt_1->x - pdev->pen_width / 2;
1397 rect.right = rect.left + pdev->pen_width;
1398 if ((sq_cap_1 && dy > 0) || (sq_cap_2 && dy < 0)) rect.top -= pdev->pen_width / 2;
1399 if ((sq_cap_2 && dy > 0) || (sq_cap_1 && dy < 0)) rect.bottom += pdev->pen_width / 2;
1400 add_rect_to_region( total, &rect );
1401 if (dy > 0)
1403 face_1->start.x = face_2->end.x = rect.left;
1404 face_1->start.y = face_1->end.y = rect.top;
1405 face_1->end.x = face_2->start.x = rect.right;
1406 face_2->start.y = face_2->end.y = rect.bottom - 1;
1408 else
1410 face_1->start.x = face_2->end.x = rect.right;
1411 face_1->start.y = face_1->end.y = rect.bottom;
1412 face_1->end.x = face_2->start.x = rect.left;
1413 face_2->start.y = face_2->end.y = rect.top + 1;
1416 else
1418 double len = hypot( dx, dy );
1419 double width_x, width_y;
1420 POINT seg_pts[4];
1421 POINT wide_half, narrow_half;
1422 HRGN segment;
1424 width_x = pdev->pen_width * abs( dy ) / len;
1425 width_y = pdev->pen_width * abs( dx ) / len;
1427 narrow_half.x = round( width_x / 2 );
1428 narrow_half.y = round( width_y / 2 );
1429 wide_half.x = round( (width_x + 1) / 2 );
1430 wide_half.y = round( (width_y + 1) / 2 );
1432 if (dx < 0)
1434 wide_half.y = -wide_half.y;
1435 narrow_half.y = -narrow_half.y;
1438 if (dy < 0)
1440 POINT tmp = narrow_half; narrow_half = wide_half; wide_half = tmp;
1441 wide_half.x = -wide_half.x;
1442 narrow_half.x = -narrow_half.x;
1445 seg_pts[0].x = pt_1->x - narrow_half.x;
1446 seg_pts[0].y = pt_1->y + narrow_half.y;
1447 seg_pts[1].x = pt_1->x + wide_half.x;
1448 seg_pts[1].y = pt_1->y - wide_half.y;
1449 seg_pts[2].x = pt_2->x + wide_half.x;
1450 seg_pts[2].y = pt_2->y - wide_half.y;
1451 seg_pts[3].x = pt_2->x - narrow_half.x;
1452 seg_pts[3].y = pt_2->y + narrow_half.y;
1454 if (sq_cap_1)
1456 seg_pts[0].x -= narrow_half.y;
1457 seg_pts[1].x -= narrow_half.y;
1458 seg_pts[0].y -= narrow_half.x;
1459 seg_pts[1].y -= narrow_half.x;
1462 if (sq_cap_2)
1464 seg_pts[2].x += wide_half.y;
1465 seg_pts[3].x += wide_half.y;
1466 seg_pts[2].y += wide_half.x;
1467 seg_pts[3].y += wide_half.x;
1470 segment = CreatePolygonRgn( seg_pts, 4, ALTERNATE );
1471 CombineRgn( total, total, segment, RGN_OR );
1472 DeleteObject( segment );
1474 face_1->start = seg_pts[0];
1475 face_1->end = seg_pts[1];
1476 face_2->start = seg_pts[2];
1477 face_2->end = seg_pts[3];
1480 face_1->dx = face_2->dx = dx;
1481 face_1->dy = face_2->dy = dy;
1483 return 1;
1486 static void wide_line_segments( dibdrv_physdev *pdev, int num, const POINT *pts, BOOL close,
1487 int start, int count, const POINT *first_pt, const POINT *last_pt,
1488 HRGN round_cap, HRGN total )
1490 int i;
1491 struct face face_1, face_2, prev_face, first_face;
1492 const POINT *pt_1, *pt_2;
1494 if (!close)
1496 add_cap( pdev, total, round_cap, first_pt );
1497 add_cap( pdev, total, round_cap, last_pt );
1500 if (count == 1)
1502 pt_1 = &pts[start];
1503 pt_2 = &pts[(start + 1) % num];
1504 wide_line_segment( pdev, total, first_pt, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1505 TRUE, TRUE, &face_1, &face_2 );
1506 return;
1509 pt_1 = &pts[start];
1510 pt_2 = &pts[(start + 1) % num];
1511 wide_line_segment( pdev, total, first_pt, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1512 !close, FALSE, &first_face, &prev_face );
1515 for (i = 1; i < count - 1; i++)
1517 pt_1 = &pts[(start + i) % num];
1518 pt_2 = &pts[(start + i + 1) % num];
1519 if (wide_line_segment( pdev, total, pt_1, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1520 FALSE, FALSE, &face_1, &face_2 ))
1522 add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1523 prev_face = face_2;
1527 pt_1 = &pts[(start + count - 1) % num];
1528 pt_2 = &pts[(start + count) % num];
1529 wide_line_segment( pdev, total, pt_1, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1530 FALSE, !close, &face_1, &face_2 );
1531 add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1532 if (close) add_join( pdev, total, round_cap, last_pt, &face_2, &first_face );
1535 static BOOL wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1537 HRGN round_cap = 0;
1539 assert( total != 0 ); /* wide pens should always be drawn through a region */
1540 assert( num >= 2 );
1542 /* skip empty segments */
1543 while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1544 while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1546 if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1547 round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1548 (pdev->pen_width + 1) / 2 + 1, (pdev->pen_width + 1) / 2 + 1 );
1550 if (close)
1551 wide_line_segments( pdev, num, pts, TRUE, 0, num, &pts[0], &pts[0], round_cap, total );
1552 else
1553 wide_line_segments( pdev, num, pts, FALSE, 0, num - 1, &pts[0], &pts[num - 1], round_cap, total );
1555 if (round_cap) DeleteObject( round_cap );
1556 return TRUE;
1559 static BOOL dashed_wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1561 int i, start, cur_len, initial_num = 0;
1562 POINT initial_point, start_point, end_point;
1563 HRGN round_cap = 0;
1565 assert( total != 0 ); /* wide pens should always be drawn through a region */
1566 assert( num >= 2 );
1568 /* skip empty segments */
1569 while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1570 while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1572 if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1573 round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1574 (pdev->pen_width + 1) / 2 + 1, (pdev->pen_width + 1) / 2 + 1);
1576 start = 0;
1577 cur_len = 0;
1578 start_point = pts[0];
1580 for (i = 0; i < (close ? num : num - 1); i++)
1582 const POINT *pt_1 = pts + i;
1583 const POINT *pt_2 = pts + ((close && i == num - 1) ? 0 : i + 1);
1584 int dx = pt_2->x - pt_1->x;
1585 int dy = pt_2->y - pt_1->y;
1587 if (dx == 0 && dy == 0) continue;
1589 if (dy == 0)
1591 if (abs( dx ) - cur_len < pdev->dash_pos.left_in_dash)
1593 skip_dash( pdev, abs( dx ) - cur_len );
1594 cur_len = 0;
1595 continue;
1597 cur_len += pdev->dash_pos.left_in_dash;
1598 dx = (dx > 0) ? cur_len : -cur_len;
1600 else if (dx == 0)
1602 if (abs( dy ) - cur_len < pdev->dash_pos.left_in_dash)
1604 skip_dash( pdev, abs( dy ) - cur_len );
1605 cur_len = 0;
1606 continue;
1608 cur_len += pdev->dash_pos.left_in_dash;
1609 dy = (dy > 0) ? cur_len : -cur_len;
1611 else
1613 double len = hypot( dx, dy );
1615 if (len - cur_len < pdev->dash_pos.left_in_dash)
1617 skip_dash( pdev, len - cur_len );
1618 cur_len = 0;
1619 continue;
1621 cur_len += pdev->dash_pos.left_in_dash;
1622 dx = dx * cur_len / len;
1623 dy = dy * cur_len / len;
1625 end_point.x = pt_1->x + dx;
1626 end_point.y = pt_1->y + dy;
1628 if (pdev->dash_pos.mark)
1630 if (!initial_num && close) /* this is the first dash, save it for later */
1632 initial_num = i - start + 1;
1633 initial_point = end_point;
1635 else wide_line_segments( pdev, num, pts, FALSE, start, i - start + 1,
1636 &start_point, &end_point, round_cap, total );
1638 if (!initial_num) initial_num = -1; /* no need to close it */
1640 skip_dash( pdev, pdev->dash_pos.left_in_dash );
1641 start_point = end_point;
1642 start = i;
1643 i--; /* go on with the same segment */
1646 if (pdev->dash_pos.mark) /* we have a final dash */
1648 int count;
1650 if (initial_num > 0)
1652 count = num - start + initial_num;
1653 end_point = initial_point;
1655 else if (close)
1657 count = num - start;
1658 end_point = pts[0];
1660 else
1662 count = num - start - 1;
1663 end_point = pts[num - 1];
1665 wide_line_segments( pdev, num, pts, FALSE, start, count,
1666 &start_point, &end_point, round_cap, total );
1668 else if (initial_num > 0) /* initial dash only */
1670 wide_line_segments( pdev, num, pts, FALSE, 0, initial_num,
1671 &pts[0], &initial_point, round_cap, total );
1674 if (round_cap) DeleteObject( round_cap );
1675 return TRUE;
1678 static const dash_pattern dash_patterns_cosmetic[4] =
1680 {2, {18, 6}, 24}, /* PS_DASH */
1681 {2, {3, 3}, 6}, /* PS_DOT */
1682 {4, {9, 6, 3, 6}, 24}, /* PS_DASHDOT */
1683 {6, {9, 3, 3, 3, 3, 3}, 24} /* PS_DASHDOTDOT */
1686 static const dash_pattern dash_patterns_geometric[4] =
1688 {2, {3, 1}, 4}, /* PS_DASH */
1689 {2, {1, 1}, 2}, /* PS_DOT */
1690 {4, {3, 1, 1, 1}, 6}, /* PS_DASHDOT */
1691 {6, {3, 1, 1, 1, 1, 1}, 8} /* PS_DASHDOTDOT */
1694 static inline void set_dash_pattern( dash_pattern *pattern, DWORD count, DWORD *dashes )
1696 DWORD i;
1698 pattern->count = count;
1699 pattern->total_len = 0;
1700 memcpy( pattern->dashes, dashes, count * sizeof(DWORD) );
1701 for (i = 0; i < count; i++) pattern->total_len += dashes[i];
1702 if (pattern->count % 2) pattern->total_len *= 2;
1705 static inline void scale_dash_pattern( dash_pattern *pattern, DWORD scale, DWORD endcap )
1707 DWORD i;
1709 for (i = 0; i < pattern->count; i++) pattern->dashes[i] *= scale;
1710 pattern->total_len *= scale;
1712 if (endcap != PS_ENDCAP_FLAT) /* shrink the dashes to leave room for the caps */
1714 for (i = 0; i < pattern->count; i += 2)
1716 pattern->dashes[i] -= scale;
1717 pattern->dashes[i + 1] += scale;
1722 static inline int get_pen_device_width( dibdrv_physdev *pdev, int width )
1724 POINT pts[2];
1726 if (!width) return 1;
1727 pts[0].x = pts[0].y = pts[1].y = 0;
1728 pts[1].x = width;
1729 LPtoDP( pdev->dev.hdc, pts, 2 );
1730 width = abs( pts[1].x - pts[0].x );
1731 return max( width, 1 );
1734 /***********************************************************************
1735 * dibdrv_SetDCPenColor
1737 COLORREF dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1739 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1741 if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
1742 pdev->pen_brush.colorref = color;
1744 return color;
1747 /**********************************************************************
1748 * solid_brush
1750 * Fill a number of rectangles with the solid brush
1752 static BOOL solid_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1753 int num, const RECT *rects, INT rop)
1755 rop_mask brush_color;
1756 DWORD color = get_pixel_color( pdev, brush->colorref, TRUE );
1758 calc_rop_masks( rop, color, &brush_color );
1759 dib->funcs->solid_rects( dib, num, rects, brush_color.and, brush_color.xor );
1760 return TRUE;
1763 static BOOL alloc_brush_mask_bits( dib_brush *brush )
1765 DWORD size = brush->dib.height * abs(brush->dib.stride);
1767 assert(brush->masks.and == NULL);
1768 assert(brush->masks.xor == NULL);
1769 assert(brush->dib.stride > 0);
1771 if (!(brush->masks.and = HeapAlloc(GetProcessHeap(), 0, 2 * size))) return FALSE;
1772 brush->masks.xor = (char *)brush->masks.and + size;
1773 return TRUE;
1776 static void free_brush_mask_bits( dib_brush *brush )
1778 HeapFree(GetProcessHeap(), 0, brush->masks.and);
1779 brush->masks.and = brush->masks.xor = NULL;
1782 void free_pattern_brush( dib_brush *brush )
1784 free_brush_mask_bits( brush );
1785 free_dib_info( &brush->dib );
1788 static BOOL create_pattern_brush_bits( dib_brush *brush )
1790 DWORD size = brush->dib.height * abs(brush->dib.stride);
1791 DWORD *brush_bits = brush->dib.bits.ptr;
1792 DWORD *and_bits, *xor_bits;
1794 if (!alloc_brush_mask_bits( brush )) return FALSE;
1796 and_bits = brush->masks.and;
1797 xor_bits = brush->masks.xor;
1799 while(size)
1801 calc_and_xor_masks(brush->rop, *brush_bits++, and_bits++, xor_bits++);
1802 size -= 4;
1805 return TRUE;
1808 static const BYTE hatches[6][8] =
1810 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1811 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
1812 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL */
1813 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL */
1814 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
1815 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
1818 static BOOL init_hatch_brush( dibdrv_physdev *pdev, dib_brush *brush )
1820 /* Just initialise brush dib with the color / sizing info. We don't
1821 need the bits as we'll calculate the rop masks straight from
1822 the hatch patterns. */
1824 copy_dib_color_info(&brush->dib, &pdev->dib);
1825 brush->dib.width = 8;
1826 brush->dib.height = 8;
1827 brush->dib.stride = get_dib_stride( brush->dib.width, brush->dib.bit_count );
1828 brush->dib.rect.left = 0;
1829 brush->dib.rect.top = 0;
1830 brush->dib.rect.right = 8;
1831 brush->dib.rect.bottom = 8;
1832 return alloc_brush_mask_bits( brush );
1835 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect)
1837 rop_mask fg_mask, bg_mask;
1839 if (!init_hatch_brush( pdev, brush )) return FALSE;
1841 get_color_masks( pdev, brush->rop, brush->colorref, GetBkMode(pdev->dev.hdc),
1842 &fg_mask, &bg_mask );
1844 if (brush->colorref & (1 << 24)) /* PALETTEINDEX */
1845 *needs_reselect = TRUE;
1846 if (GetBkMode(pdev->dev.hdc) != TRANSPARENT && (GetBkColor(pdev->dev.hdc) & (1 << 24)))
1847 *needs_reselect = TRUE;
1849 brush->dib.funcs->create_rop_masks( &brush->dib, hatches[brush->hatch],
1850 &fg_mask, &bg_mask, &brush->masks );
1851 return TRUE;
1854 static BOOL create_dither_brush_bits(dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect)
1856 COLORREF rgb;
1857 DWORD pixel;
1858 BOOL got_pixel;
1860 if (!init_hatch_brush( pdev, brush )) return FALSE;
1862 if (brush->colorref & (1 << 24)) /* PALETTEINDEX */
1863 *needs_reselect = TRUE;
1865 rgb = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, brush->colorref, &got_pixel, &pixel );
1867 brush->dib.funcs->create_dither_masks( &brush->dib, brush->rop, rgb, &brush->masks );
1868 return TRUE;
1871 static BOOL matching_pattern_format( dib_info *dib, dib_info *pattern )
1873 if (dib->bit_count != pattern->bit_count) return FALSE;
1874 if (dib->stride != pattern->stride) return FALSE;
1876 switch (dib->bit_count)
1878 case 1:
1879 case 4:
1880 case 8:
1881 if (dib->color_table_size != pattern->color_table_size) return FALSE;
1882 return !memcmp( dib->color_table, pattern->color_table, dib->color_table_size * sizeof(RGBQUAD) );
1883 case 16:
1884 case 32:
1885 return (dib->red_mask == pattern->red_mask &&
1886 dib->green_mask == pattern->green_mask &&
1887 dib->blue_mask == pattern->blue_mask);
1889 return TRUE;
1892 static BOOL select_pattern_brush( dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect )
1894 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1895 BITMAPINFO *info = (BITMAPINFO *)buffer;
1896 RGBQUAD color_table[2];
1897 dib_info pattern;
1898 BOOL dither = (brush->dib.bit_count == 1);
1900 if (pattern.bit_count == 1 && !pattern.color_table)
1901 dither = FALSE; /* monochrome DDB pattern brushes don't get dithered */
1903 if (brush->pattern.info->bmiHeader.biClrUsed && brush->pattern.usage == DIB_PAL_COLORS)
1905 copy_bitmapinfo( info, brush->pattern.info );
1906 fill_color_table_from_pal_colors( info, pdev->dev.hdc );
1907 init_dib_info_from_bitmapinfo( &pattern, info, brush->pattern.bits.ptr );
1908 *needs_reselect = TRUE;
1910 else
1912 init_dib_info_from_bitmapinfo( &pattern, brush->pattern.info, brush->pattern.bits.ptr );
1915 if (pattern.bit_count == 1 && !pattern.color_table &&
1916 (pdev->dib.bit_count != 1 || pdev->dib.color_table))
1918 /* monochrome DDB pattern uses DC colors */
1919 DWORD pixel;
1920 BOOL got_pixel;
1921 COLORREF color;
1923 color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetTextColor( pdev->dev.hdc ),
1924 &got_pixel, &pixel );
1925 color_table[0].rgbRed = GetRValue( color );
1926 color_table[0].rgbGreen = GetGValue( color );
1927 color_table[0].rgbBlue = GetBValue( color );
1928 color_table[0].rgbReserved = 0;
1930 color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetBkColor( pdev->dev.hdc ),
1931 &got_pixel, &pixel );
1932 color_table[1].rgbRed = GetRValue( color );
1933 color_table[1].rgbGreen = GetGValue( color );
1934 color_table[1].rgbBlue = GetBValue( color );
1935 color_table[1].rgbReserved = 0;
1937 pattern.color_table = color_table;
1938 pattern.color_table_size = 2;
1939 *needs_reselect = TRUE;
1942 copy_dib_color_info(&brush->dib, &pdev->dib);
1944 brush->dib.height = pattern.height;
1945 brush->dib.width = pattern.width;
1946 brush->dib.stride = get_dib_stride( brush->dib.width, brush->dib.bit_count );
1947 brush->dib.rect = pattern.rect;
1949 if (!dither && matching_pattern_format( &brush->dib, &pattern ))
1951 brush->dib.bits.ptr = pattern.bits.ptr;
1952 brush->dib.bits.is_copy = FALSE;
1953 brush->dib.bits.free = NULL;
1955 else
1957 brush->dib.bits.ptr = HeapAlloc( GetProcessHeap(), 0, brush->dib.height * brush->dib.stride );
1958 brush->dib.bits.is_copy = TRUE;
1959 brush->dib.bits.free = free_heap_bits;
1960 brush->dib.funcs->convert_to(&brush->dib, &pattern, &pattern.rect, dither);
1962 return TRUE;
1965 /**********************************************************************
1966 * pattern_brush
1968 * Fill a number of rectangles with the pattern brush
1969 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1971 static BOOL pattern_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1972 int num, const RECT *rects, INT rop)
1974 POINT origin;
1975 BOOL needs_reselect = FALSE;
1977 if (rop != brush->rop)
1979 free_brush_mask_bits( brush );
1980 brush->rop = rop;
1983 if(brush->masks.and == NULL)
1985 switch(brush->style)
1987 case BS_DIBPATTERN:
1988 if (!brush->dib.bits.ptr && !select_pattern_brush( pdev, brush, &needs_reselect ))
1989 return FALSE;
1990 if(!create_pattern_brush_bits( brush ))
1991 return FALSE;
1992 break;
1994 case BS_SOLID:
1995 if(!create_dither_brush_bits(pdev, brush, &needs_reselect))
1996 return FALSE;
1997 break;
1999 case BS_HATCHED:
2000 if(!create_hatch_brush_bits(pdev, brush, &needs_reselect))
2001 return FALSE;
2002 break;
2004 default:
2005 ERR("Unexpected brush style %d\n", brush->style);
2006 return FALSE;
2010 GetBrushOrgEx(pdev->dev.hdc, &origin);
2012 dib->funcs->pattern_rects( dib, num, rects, &origin, &brush->dib, &brush->masks );
2014 if (needs_reselect) free_pattern_brush( brush );
2015 return TRUE;
2018 static BOOL null_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
2019 int num, const RECT *rects, INT rop)
2021 return TRUE;
2024 static BOOL brush_needs_dithering( dibdrv_physdev *pdev, COLORREF color )
2026 int i;
2027 RGBQUAD rgb;
2028 const RGBQUAD *color_table = get_default_color_table( pdev->dib.bit_count );
2030 if (!color_table) return FALSE;
2031 if (pdev->dib.color_table) return FALSE;
2032 if (color & (1 << 24)) return TRUE; /* PALETTEINDEX */
2033 if (color >> 16 == 0x10ff) return FALSE; /* DIBINDEX */
2035 rgb = rgbquad_from_colorref( color );
2036 for (i = 0; i < (1 << pdev->dib.bit_count); i++)
2037 if (rgbquad_equal( &color_table[i], &rgb )) return FALSE;
2039 return TRUE;
2042 static void select_brush( dibdrv_physdev *pdev, dib_brush *brush,
2043 const LOGBRUSH *logbrush, const struct brush_pattern *pattern )
2045 free_pattern_brush( brush );
2047 if (pattern)
2049 brush->style = BS_DIBPATTERN;
2050 brush->pattern = *pattern; /* brush is actually selected only when it's used */
2051 brush->rects = pattern_brush;
2053 else
2055 brush->style = logbrush->lbStyle;
2056 brush->colorref = logbrush->lbColor;
2057 brush->hatch = logbrush->lbHatch;
2059 switch (logbrush->lbStyle)
2061 case BS_NULL: brush->rects = null_brush; break;
2062 case BS_HATCHED: brush->rects = pattern_brush; break;
2063 case BS_SOLID:
2064 brush->rects = brush_needs_dithering( pdev, brush->colorref ) ? pattern_brush : solid_brush;
2065 break;
2070 /***********************************************************************
2071 * dibdrv_SelectBrush
2073 HBRUSH dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush, const struct brush_pattern *pattern )
2075 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2076 LOGBRUSH logbrush;
2078 TRACE("(%p, %p)\n", dev, hbrush);
2080 GetObjectW( hbrush, sizeof(logbrush), &logbrush );
2082 if (hbrush == GetStockObject( DC_BRUSH ))
2083 logbrush.lbColor = GetDCBrushColor( dev->hdc );
2085 select_brush( pdev, &pdev->brush, &logbrush, pattern );
2086 return hbrush;
2089 /***********************************************************************
2090 * dibdrv_SelectPen
2092 HPEN dibdrv_SelectPen( PHYSDEV dev, HPEN hpen, const struct brush_pattern *pattern )
2094 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2095 LOGPEN logpen;
2096 LOGBRUSH logbrush;
2097 EXTLOGPEN *elp = NULL;
2099 TRACE("(%p, %p)\n", dev, hpen);
2101 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
2103 /* must be an extended pen */
2104 INT size = GetObjectW( hpen, 0, NULL );
2106 if (!size) return 0;
2108 elp = HeapAlloc( GetProcessHeap(), 0, size );
2110 GetObjectW( hpen, size, elp );
2111 logpen.lopnStyle = elp->elpPenStyle;
2112 logpen.lopnWidth.x = elp->elpWidth;
2113 /* cosmetic ext pens are always 1-pixel wide */
2114 if (!(logpen.lopnStyle & PS_GEOMETRIC)) logpen.lopnWidth.x = 0;
2116 logbrush.lbStyle = elp->elpBrushStyle;
2117 logbrush.lbColor = elp->elpColor;
2118 logbrush.lbHatch = elp->elpHatch;
2120 else
2122 logbrush.lbStyle = BS_SOLID;
2123 logbrush.lbColor = logpen.lopnColor;
2124 logbrush.lbHatch = 0;
2127 pdev->pen_join = logpen.lopnStyle & PS_JOIN_MASK;
2128 pdev->pen_endcap = logpen.lopnStyle & PS_ENDCAP_MASK;
2129 pdev->pen_width = get_pen_device_width( pdev, logpen.lopnWidth.x );
2131 if (hpen == GetStockObject( DC_PEN ))
2132 logbrush.lbColor = GetDCPenColor( dev->hdc );
2134 set_dash_pattern( &pdev->pen_pattern, 0, NULL );
2135 select_brush( pdev, &pdev->pen_brush, &logbrush, pattern );
2137 pdev->pen_style = logpen.lopnStyle & PS_STYLE_MASK;
2139 switch (pdev->pen_style)
2141 case PS_DASH:
2142 case PS_DOT:
2143 case PS_DASHDOT:
2144 case PS_DASHDOTDOT:
2145 if (logpen.lopnStyle & PS_GEOMETRIC)
2147 pdev->pen_pattern = dash_patterns_geometric[pdev->pen_style - 1];
2148 if (pdev->pen_width > 1)
2150 scale_dash_pattern( &pdev->pen_pattern, pdev->pen_width, pdev->pen_endcap );
2151 pdev->pen_lines = dashed_wide_pen_lines;
2153 else pdev->pen_lines = dashed_pen_lines;
2154 break;
2156 if (pdev->pen_width == 1) /* wide cosmetic pens are not dashed */
2158 pdev->pen_lines = dashed_pen_lines;
2159 pdev->pen_pattern = dash_patterns_cosmetic[pdev->pen_style - 1];
2160 break;
2162 /* fall through */
2163 case PS_SOLID:
2164 case PS_INSIDEFRAME:
2165 pdev->pen_lines = (pdev->pen_width == 1) ? solid_pen_lines : wide_pen_lines;
2166 break;
2168 case PS_NULL:
2169 pdev->pen_width = 0;
2170 pdev->pen_lines = null_pen_lines;
2171 break;
2173 case PS_ALTERNATE:
2174 pdev->pen_lines = dashed_pen_lines;
2175 pdev->pen_pattern = dash_patterns_geometric[PS_DOT - 1];
2176 break;
2178 case PS_USERSTYLE:
2179 pdev->pen_lines = (pdev->pen_width == 1) ? dashed_pen_lines : dashed_wide_pen_lines;
2180 set_dash_pattern( &pdev->pen_pattern, elp->elpNumEntries, elp->elpStyleEntry );
2181 if (!(logpen.lopnStyle & PS_GEOMETRIC)) scale_dash_pattern( &pdev->pen_pattern, 3, PS_ENDCAP_FLAT );
2182 break;
2185 pdev->pen_uses_region = (logpen.lopnStyle & PS_GEOMETRIC || pdev->pen_width > 1);
2186 pdev->pen_is_ext = (elp != NULL);
2187 HeapFree( GetProcessHeap(), 0, elp );
2188 return hpen;
2191 /***********************************************************************
2192 * dibdrv_SetDCBrushColor
2194 COLORREF dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
2196 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2198 if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
2200 LOGBRUSH logbrush = { BS_SOLID, color, 0 };
2201 select_brush( pdev, &pdev->brush, &logbrush, NULL );
2203 return color;