gdi32: Pass the brush origin explicitly to the brush backend functions.
[wine.git] / dlls / gdi32 / dibdrv / objects.c
blob736e52623bb52383c04422e9478d86bb09dcc2e0
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 BOOL rop_needs_and_mask( INT rop )
113 struct rop_codes codes;
114 get_rop_codes( rop, &codes );
115 return codes.a1 || codes.a2;
118 static inline RGBQUAD rgbquad_from_colorref(COLORREF c)
120 RGBQUAD ret;
122 ret.rgbRed = GetRValue(c);
123 ret.rgbGreen = GetGValue(c);
124 ret.rgbBlue = GetBValue(c);
125 ret.rgbReserved = 0;
126 return ret;
129 static inline BOOL rgbquad_equal(const RGBQUAD *a, const RGBQUAD *b)
131 if(a->rgbRed == b->rgbRed &&
132 a->rgbGreen == b->rgbGreen &&
133 a->rgbBlue == b->rgbBlue)
134 return TRUE;
135 return FALSE;
138 static COLORREF make_rgb_colorref( DC *dc, const dib_info *dib, COLORREF color,
139 BOOL *got_pixel, DWORD *pixel )
141 *pixel = 0;
142 *got_pixel = FALSE;
144 if (color & (1 << 24)) /* PALETTEINDEX */
146 PALETTEENTRY pal_ent;
148 if (!GetPaletteEntries( dc->hPalette, LOWORD(color), 1, &pal_ent ))
149 GetPaletteEntries( dc->hPalette, 0, 1, &pal_ent );
150 return RGB( pal_ent.peRed, pal_ent.peGreen, pal_ent.peBlue );
153 if (color >> 16 == 0x10ff) /* DIBINDEX */
155 const RGBQUAD *color_table = get_dib_color_table( dib );
156 WORD index = LOWORD( color );
157 *got_pixel = TRUE;
158 if (!color_table || index >= (1 << dib->bit_count)) return 0;
159 *pixel = index;
160 return RGB( color_table[index].rgbRed, color_table[index].rgbGreen, color_table[index].rgbBlue );
163 return color & 0xffffff;
166 /******************************************************************
167 * get_pixel_color
169 * 1 bit bitmaps map the fg/bg colors as follows:
170 * If the fg colorref exactly matches one of the color table entries then
171 * that entry is the fg color and the other is the bg.
172 * Otherwise the bg color is mapped to the closest entry in the table and
173 * the fg takes the other one.
175 DWORD get_pixel_color( DC *dc, const dib_info *dib, COLORREF color, BOOL mono_fixup )
177 RGBQUAD fg_quad;
178 BOOL got_pixel;
179 DWORD pixel;
180 COLORREF rgb_ref;
181 const RGBQUAD *color_table;
183 rgb_ref = make_rgb_colorref( dc, dib, color, &got_pixel, &pixel );
184 if (got_pixel) return pixel;
186 if (dib->bit_count != 1 || !mono_fixup)
187 return dib->funcs->colorref_to_pixel( dib, rgb_ref );
189 color_table = get_dib_color_table( dib );
190 fg_quad = rgbquad_from_colorref( rgb_ref );
191 if(rgbquad_equal(&fg_quad, color_table))
192 return 0;
193 if(rgbquad_equal(&fg_quad, color_table + 1))
194 return 1;
196 pixel = get_pixel_color( dc, dib, dc->backgroundColor, FALSE );
197 if (color == dc->backgroundColor) return pixel;
198 else return !pixel;
201 /***************************************************************************
202 * get_color_masks
204 * Returns the color masks unless the dib is 1 bpp. In this case since
205 * there are several fg sources (pen, brush, text) we take as bg the inverse
206 * of the relevant fg color (which is always set up correctly).
208 static inline void get_color_masks( DC *dc, const dib_info *dib, UINT rop, COLORREF colorref,
209 INT bkgnd_mode, rop_mask *fg_mask, rop_mask *bg_mask )
211 DWORD color = get_pixel_color( dc, dib, colorref, TRUE );
213 calc_rop_masks( rop, color, fg_mask );
215 if (bkgnd_mode == TRANSPARENT)
217 bg_mask->and = ~0u;
218 bg_mask->xor = 0;
219 return;
222 if (dib->bit_count != 1) color = get_pixel_color( dc, dib, dc->backgroundColor, FALSE );
223 else if (colorref != dc->backgroundColor) color = !color;
225 calc_rop_masks( rop, color, bg_mask );
228 static inline void order_end_points(int *s, int *e)
230 if(*s > *e)
232 int tmp;
233 tmp = *s + 1;
234 *s = *e + 1;
235 *e = tmp;
239 #define Y_INCREASING_MASK 0x0f
240 #define X_INCREASING_MASK 0xc3
241 #define X_MAJOR_MASK 0x99
242 #define POS_SLOPE_MASK 0x33
244 static inline BOOL is_xmajor(DWORD octant)
246 return octant & X_MAJOR_MASK;
249 static inline BOOL is_pos_slope(DWORD octant)
251 return octant & POS_SLOPE_MASK;
254 static inline BOOL is_x_increasing(DWORD octant)
256 return octant & X_INCREASING_MASK;
259 static inline BOOL is_y_increasing(DWORD octant)
261 return octant & Y_INCREASING_MASK;
264 /**********************************************************************
265 * get_octant_number
267 * Return the octant number starting clockwise from the +ve x-axis.
269 static inline int get_octant_number(int dx, int dy)
271 if(dy > 0)
272 if(dx > 0)
273 return ( dx > dy) ? 1 : 2;
274 else
275 return (-dx > dy) ? 4 : 3;
276 else
277 if(dx < 0)
278 return (-dx > -dy) ? 5 : 6;
279 else
280 return ( dx > -dy) ? 8 : 7;
283 static inline DWORD get_octant_mask(int dx, int dy)
285 return 1 << (get_octant_number(dx, dy) - 1);
288 static inline int get_bias( DWORD mask )
290 /* Octants 3, 5, 6 and 8 take a bias */
291 return (mask & 0xb4) ? 1 : 0;
294 #define OUT_LEFT 1
295 #define OUT_RIGHT 2
296 #define OUT_TOP 4
297 #define OUT_BOTTOM 8
299 static inline DWORD calc_outcode(const POINT *pt, const RECT *clip)
301 DWORD out = 0;
302 if(pt->x < clip->left) out |= OUT_LEFT;
303 else if(pt->x >= clip->right) out |= OUT_RIGHT;
304 if(pt->y < clip->top) out |= OUT_TOP;
305 else if(pt->y >= clip->bottom) out |= OUT_BOTTOM;
307 return out;
310 /* crop coordinates to a reasonable range to avoid overflows in calculations */
311 static inline POINT crop_coords( POINT pt )
313 if (pt.x >= 0x10000000 || pt.x <= -0x10000000 || pt.y >= 0x10000000 || pt.y <= -0x10000000)
315 pt.x /= 8;
316 pt.y /= 8;
318 return pt;
321 static void init_bres_params( const POINT *start, const POINT *end, bres_params *clip_params,
322 struct line_params *line_params, RECT *rect )
324 INT dx = end->x - start->x, dy = end->y - start->y;
325 INT abs_dx = abs(dx), abs_dy = abs(dy);
327 clip_params->dx = abs_dx;
328 clip_params->dy = abs_dy;
329 clip_params->octant = get_octant_mask(dx, dy);
330 clip_params->bias = get_bias( clip_params->octant );
332 line_params->bias = clip_params->bias;
333 line_params->x_major = is_xmajor( clip_params->octant );
334 line_params->x_inc = is_x_increasing( clip_params->octant ) ? 1 : -1;
335 line_params->y_inc = is_y_increasing( clip_params->octant ) ? 1 : -1;
337 if (line_params->x_major)
339 line_params->err_add_1 = 2 * abs_dy - 2 * abs_dx;
340 line_params->err_add_2 = 2 * abs_dy;
342 else
344 line_params->err_add_1 = 2 * abs_dx - 2 * abs_dy;
345 line_params->err_add_2 = 2 * abs_dx;
348 rect->left = min( start->x, end->x );
349 rect->top = min( start->y, end->y );
350 rect->right = max( start->x, end->x ) + 1;
351 rect->bottom = max( start->y, end->y ) + 1;
354 /******************************************************************************
355 * clip_line
357 * Clips the start and end points to a rectangle.
359 * Note, this treats the end point like the start point. If the
360 * caller doesn't want it displayed, it should exclude it. If the end
361 * point is clipped out, then the likelihood is that the new end point
362 * should be displayed.
364 * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
366 * This derivation is based on the comments in X.org's xserver/mi/mizerclip.c,
367 * however the Bresenham error term is defined differently so the equations
368 * will also differ.
370 * For x major lines we have 2dy >= err + bias > 2dy - 2dx
371 * 0 >= err + bias - 2dy > -2dx
373 * Note dx, dy, m and n are all +ve.
375 * Moving the start pt from x1 to x1 + m, we need to figure out y1 + n.
376 * err = 2dy - dx + 2mdy - 2ndx
377 * 0 >= 2dy - dx + 2mdy - 2ndx + bias - 2dy > -2dx
378 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
379 * which of course will give exactly one solution for n,
380 * so looking at the >= inequality
381 * n >= (2mdy + bias - dx) / 2dx
382 * n = ceiling((2mdy + bias - dx) / 2dx)
383 * = (2mdy + bias + dx - 1) / 2dx (assuming division truncation)
385 * Moving start pt from y1 to y1 + n we need to figure out x1 + m - there may be several
386 * solutions we pick the one that minimizes m (ie that first unlipped pt). As above:
387 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
388 * 2mdy > 2ndx - bias - dx
389 * m > (2ndx - bias - dx) / 2dy
390 * m = floor((2ndx - bias - dx) / 2dy) + 1
391 * m = (2ndx - bias - dx) / 2dy + 1
393 * Moving end pt from x2 to x2 - m, we need to figure out y2 - n
394 * err = 2dy - dx + 2(dx - m)dy - 2(dy - n)dx
395 * = 2dy - dx - 2mdy + 2ndx
396 * 0 >= 2dy - dx - 2mdy + 2ndx + bias - 2dy > -2dx
397 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
398 * again exactly one solution.
399 * 2ndx <= 2mdy - bias + dx
400 * n = floor((2mdy - bias + dx) / 2dx)
401 * = (2mdy - bias + dx) / 2dx
403 * Moving end pt from y2 to y2 - n when need x2 - m this time maximizing x2 - m so
404 * mininizing m to include all of the points at y = y2 - n. As above:
405 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
406 * 2mdy >= 2ndx + bias - dx
407 * m = ceiling((2ndx + bias - dx) / 2dy)
408 * = (2ndx + bias - dx - 1) / 2dy + 1
410 * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
412 * Moving start point from y1 to y1 + n find x1 + m
413 * m = (2ndx + bias + dy - 1) / 2dy
415 * Moving start point from x1 to x1 + m find y1 + n
416 * n = (2mdy - bias - dy) / 2ndx + 1
418 * Moving end point from y2 to y2 - n find x1 - m
419 * m = (2ndx - bias + dy) / 2dy
421 * Moving end point from x2 to x2 - m find y2 - n
422 * n = (2mdy + bias - dy - 1) / 2dx + 1
424 int clip_line(const POINT *start, const POINT *end, const RECT *clip,
425 const bres_params *params, POINT *pt1, POINT *pt2)
428 INT64 m, n; /* 64-bit to avoid overflows (FIXME: find a more efficient way) */
429 BOOL clipped = FALSE;
430 DWORD start_oc, end_oc;
431 const int bias = params->bias;
432 const unsigned int dx = params->dx;
433 const unsigned int dy = params->dy;
434 const unsigned int two_dx = params->dx * 2;
435 const unsigned int two_dy = params->dy * 2;
436 const BOOL xmajor = is_xmajor(params->octant);
437 const BOOL neg_slope = !is_pos_slope(params->octant);
439 *pt1 = *start;
440 *pt2 = *end;
442 start_oc = calc_outcode(start, clip);
443 end_oc = calc_outcode(end, clip);
445 while(1)
447 if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
448 if(start_oc & end_oc) return 0; /* trivial reject */
450 clipped = TRUE;
451 if(start_oc & OUT_LEFT)
453 m = clip->left - start->x;
454 if(xmajor)
455 n = (m * two_dy + bias + dx - 1) / two_dx;
456 else
457 n = (m * two_dy - bias - dy) / two_dx + 1;
459 pt1->x = clip->left;
460 if(neg_slope) n = -n;
461 pt1->y = start->y + n;
462 start_oc = calc_outcode(pt1, clip);
464 else if(start_oc & OUT_RIGHT)
466 m = start->x - clip->right + 1;
467 if(xmajor)
468 n = (m * two_dy + bias + dx - 1) / two_dx;
469 else
470 n = (m * two_dy - bias - dy) / two_dx + 1;
472 pt1->x = clip->right - 1;
473 if(neg_slope) n = -n;
474 pt1->y = start->y - n;
475 start_oc = calc_outcode(pt1, clip);
477 else if(start_oc & OUT_TOP)
479 n = clip->top - start->y;
480 if(xmajor)
481 m = (n * two_dx - bias - dx) / two_dy + 1;
482 else
483 m = (n * two_dx + bias + dy - 1) / two_dy;
485 pt1->y = clip->top;
486 if(neg_slope) m = -m;
487 pt1->x = start->x + m;
488 start_oc = calc_outcode(pt1, clip);
490 else if(start_oc & OUT_BOTTOM)
492 n = start->y - clip->bottom + 1;
493 if(xmajor)
494 m = (n * two_dx - bias - dx) / two_dy + 1;
495 else
496 m = (n * two_dx + bias + dy - 1) / two_dy;
498 pt1->y = clip->bottom - 1;
499 if(neg_slope) m = -m;
500 pt1->x = start->x - m;
501 start_oc = calc_outcode(pt1, clip);
503 else if(end_oc & OUT_LEFT)
505 m = clip->left - end->x;
506 if(xmajor)
507 n = (m * two_dy - bias + dx) / two_dx;
508 else
509 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
511 pt2->x = clip->left;
512 if(neg_slope) n = -n;
513 pt2->y = end->y + n;
514 end_oc = calc_outcode(pt2, clip);
516 else if(end_oc & OUT_RIGHT)
518 m = end->x - clip->right + 1;
519 if(xmajor)
520 n = (m * two_dy - bias + dx) / two_dx;
521 else
522 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
524 pt2->x = clip->right - 1;
525 if(neg_slope) n = -n;
526 pt2->y = end->y - n;
527 end_oc = calc_outcode(pt2, clip);
529 else if(end_oc & OUT_TOP)
531 n = clip->top - end->y;
532 if(xmajor)
533 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
534 else
535 m = (n * two_dx - bias + dy) / two_dy;
537 pt2->y = clip->top;
538 if(neg_slope) m = -m;
539 pt2->x = end->x + m;
540 end_oc = calc_outcode(pt2, clip);
542 else if(end_oc & OUT_BOTTOM)
544 n = end->y - clip->bottom + 1;
545 if(xmajor)
546 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
547 else
548 m = (n * two_dx - bias + dy) / two_dy;
550 pt2->y = clip->bottom - 1;
551 if(neg_slope) m = -m;
552 pt2->x = end->x - m;
553 end_oc = calc_outcode(pt2, clip);
558 static void bres_line_with_bias(const POINT *start, const struct line_params *params,
559 void (* callback)(dibdrv_physdev*,INT,INT), dibdrv_physdev *pdev)
561 POINT pt = *start;
562 int len = params->length, err = params->err_start;
564 if (params->x_major)
566 while(len--)
568 callback(pdev, pt.x, pt.y);
569 if (err + params->bias > 0)
571 pt.y += params->y_inc;
572 err += params->err_add_1;
574 else err += params->err_add_2;
575 pt.x += params->x_inc;
578 else
580 while(len--)
582 callback(pdev, pt.x, pt.y);
583 if (err + params->bias > 0)
585 pt.x += params->x_inc;
586 err += params->err_add_1;
588 else err += params->err_add_2;
589 pt.y += params->y_inc;
594 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end, DWORD and, DWORD xor)
596 struct clipped_rects clipped_rects;
597 RECT rect;
598 int i;
600 if(start->y == end->y)
602 rect.left = start->x;
603 rect.top = start->y;
604 rect.right = end->x;
605 rect.bottom = end->y + 1;
606 order_end_points(&rect.left, &rect.right);
607 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
608 pdev->dib.funcs->solid_rects(&pdev->dib, clipped_rects.count, clipped_rects.rects, and, xor);
610 else if(start->x == end->x)
612 rect.left = start->x;
613 rect.top = start->y;
614 rect.right = end->x + 1;
615 rect.bottom = end->y;
616 order_end_points(&rect.top, &rect.bottom);
617 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
618 pdev->dib.funcs->solid_rects(&pdev->dib, clipped_rects.count, clipped_rects.rects, and, xor);
620 else
622 bres_params clip_params;
623 struct line_params line_params;
624 POINT p1 = crop_coords( *start ), p2 = crop_coords( *end );
626 init_bres_params( &p1, &p2, &clip_params, &line_params, &rect );
627 if (!get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects )) return TRUE;
628 for (i = 0; i < clipped_rects.count; i++)
630 POINT clipped_start, clipped_end;
631 int clip_status;
633 clip_status = clip_line( &p1, &p2, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
634 if(clip_status)
636 int m = abs(clipped_start.x - p1.x);
637 int n = abs(clipped_start.y - p1.y);
639 if (line_params.x_major)
641 line_params.err_start = 2 * clip_params.dy - clip_params.dx
642 + m * 2 * clip_params.dy - n * 2 * clip_params.dx;
643 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
645 else
647 line_params.err_start = 2 * clip_params.dx - clip_params.dy
648 + n * 2 * clip_params.dx - m * 2 * clip_params.dy;
649 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
652 if (clipped_end.x == p2.x && clipped_end.y == p2.y) line_params.length--;
654 pdev->dib.funcs->solid_line( &pdev->dib, &clipped_start, &line_params, and, xor );
656 if(clip_status == 2) break; /* completely unclipped, so we can finish */
660 free_clipped_rects( &clipped_rects );
661 return TRUE;
664 static void solid_line_region( const dib_info *dib, const POINT *start, const struct line_params *params,
665 HRGN region )
667 int len, err = params->err_start;
668 RECT rect;
670 rect.left = start->x;
671 rect.top = start->y;
672 rect.right = start->x + 1;
673 rect.bottom = start->y + 1;
675 if (params->x_major)
677 if (params->x_inc > 0)
679 for (len = params->length; len; len--, rect.right++)
681 if (err + params->bias > 0)
683 add_rect_to_region( region, &rect );
684 rect.left = rect.right;
685 rect.top += params->y_inc;
686 rect.bottom += params->y_inc;
687 err += params->err_add_1;
689 else err += params->err_add_2;
692 else
694 for (len = params->length; len; len--, rect.left--)
696 if (err + params->bias > 0)
698 add_rect_to_region( region, &rect );
699 rect.right = rect.left;
700 rect.top += params->y_inc;
701 rect.bottom += params->y_inc;
702 err += params->err_add_1;
704 else err += params->err_add_2;
708 else
710 if (params->y_inc > 0)
712 for (len = params->length; len; len--, rect.bottom++)
714 if (err + params->bias > 0)
716 add_rect_to_region( region, &rect );
717 rect.top = rect.bottom;
718 rect.left += params->x_inc;
719 rect.right += params->x_inc;
720 err += params->err_add_1;
722 else err += params->err_add_2;
725 else
727 for (len = params->length; len; len--, rect.top--)
729 if (err + params->bias > 0)
731 add_rect_to_region( region, &rect );
732 rect.bottom = rect.top;
733 rect.left += params->x_inc;
734 rect.right += params->x_inc;
735 err += params->err_add_1;
737 else err += params->err_add_2;
741 /* add final rect */
742 add_rect_to_region( region, &rect );
745 static BOOL solid_pen_line_region( dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region )
747 RECT rect;
749 rect.left = start->x;
750 rect.top = start->y;
751 rect.right = start->x + 1;
752 rect.bottom = start->y + 1;
754 if (start->y == end->y)
756 rect.right = end->x;
757 order_end_points(&rect.left, &rect.right);
758 if (clip_rect_to_dib( &pdev->dib, &rect )) add_rect_to_region( region, &rect );
760 else if(start->x == end->x)
762 rect.bottom = end->y;
763 order_end_points(&rect.top, &rect.bottom);
764 if (clip_rect_to_dib( &pdev->dib, &rect )) add_rect_to_region( region, &rect );
766 else
768 bres_params clip_params;
769 struct line_params line_params;
770 POINT p1 = crop_coords( *start ), p2 = crop_coords( *end );
772 init_bres_params( &p1, &p2, &clip_params, &line_params, &rect );
773 if (clip_rect_to_dib( &pdev->dib, &rect ))
775 POINT clipped_start, clipped_end;
777 if (clip_line( &p1, &p2, &rect, &clip_params, &clipped_start, &clipped_end ))
779 int m = abs(clipped_start.x - p1.x);
780 int n = abs(clipped_start.y - p1.y);
782 if (line_params.x_major)
784 line_params.err_start = 2 * clip_params.dy - clip_params.dx
785 + m * 2 * clip_params.dy - n * 2 * clip_params.dx;
786 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
788 else
790 line_params.err_start = 2 * clip_params.dx - clip_params.dy
791 + n * 2 * clip_params.dx - m * 2 * clip_params.dy;
792 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
795 if (clipped_end.x == p2.x && clipped_end.y == p2.y) line_params.length--;
796 solid_line_region( &pdev->dib, &clipped_start, &line_params, region );
800 return TRUE;
803 static BOOL solid_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
805 DC *dc = get_physdev_dc( &pdev->dev );
806 int i;
808 assert( num >= 2 );
810 if (region)
812 for (i = 0; i < num - 1; i++)
813 if (!solid_pen_line_region( pdev, pts + i, pts + i + 1, region ))
814 return FALSE;
815 if (close) return solid_pen_line_region( pdev, pts + num - 1, pts, region );
817 else
819 DWORD color, and, xor;
821 color = get_pixel_color( dc, &pdev->dib, pdev->pen_brush.colorref, TRUE );
822 calc_and_xor_masks( dc->ROPmode, color, &and, &xor );
824 for (i = 0; i < num - 1; i++)
825 if (!solid_pen_line( pdev, pts + i, pts + i + 1, and, xor ))
826 return FALSE;
827 if (close) return solid_pen_line( pdev, pts + num - 1, pts, and, xor );
829 return TRUE;
832 void reset_dash_origin(dibdrv_physdev *pdev)
834 pdev->dash_pos.cur_dash = 0;
835 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
836 pdev->dash_pos.mark = TRUE;
839 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
841 skip %= pdev->pen_pattern.total_len;
844 if(pdev->dash_pos.left_in_dash > skip)
846 pdev->dash_pos.left_in_dash -= skip;
847 return;
849 skip -= pdev->dash_pos.left_in_dash;
850 pdev->dash_pos.cur_dash++;
851 if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
852 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
853 pdev->dash_pos.mark = !pdev->dash_pos.mark;
855 while (skip);
858 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
860 RECT rect;
861 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
863 skip_dash(pdev, 1);
864 rect.left = x;
865 rect.right = x + 1;
866 rect.top = y;
867 rect.bottom = y + 1;
868 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
869 return;
872 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
874 struct clipped_rects clipped_rects;
875 int i, dash_len;
876 RECT rect;
877 const dash_pos start_pos = pdev->dash_pos;
879 if(start->y == end->y) /* hline */
881 BOOL l_to_r;
882 INT left, right, cur_x;
884 rect.top = start->y;
885 rect.bottom = start->y + 1;
887 if(start->x <= end->x)
889 left = start->x;
890 right = end->x - 1;
891 l_to_r = TRUE;
893 else
895 left = end->x + 1;
896 right = start->x;
897 l_to_r = FALSE;
900 rect.left = min( start->x, end->x );
901 rect.right = max( start->x, end->x ) + 1;
902 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
903 for (i = 0; i < clipped_rects.count; i++)
905 if(clipped_rects.rects[i].right > left && clipped_rects.rects[i].left <= right)
907 int clipped_left = max(clipped_rects.rects[i].left, left);
908 int clipped_right = min(clipped_rects.rects[i].right - 1, right);
910 pdev->dash_pos = start_pos;
912 if(l_to_r)
914 cur_x = clipped_left;
915 if(cur_x != left)
916 skip_dash(pdev, clipped_left - left);
918 while(cur_x <= clipped_right)
920 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
921 dash_len = pdev->dash_pos.left_in_dash;
922 if(cur_x + dash_len > clipped_right + 1)
923 dash_len = clipped_right - cur_x + 1;
924 rect.left = cur_x;
925 rect.right = cur_x + dash_len;
927 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
928 cur_x += dash_len;
929 skip_dash(pdev, dash_len);
932 else
934 cur_x = clipped_right;
935 if(cur_x != right)
936 skip_dash(pdev, right - clipped_right);
938 while(cur_x >= clipped_left)
940 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
941 dash_len = pdev->dash_pos.left_in_dash;
942 if(cur_x - dash_len < clipped_left - 1)
943 dash_len = cur_x - clipped_left + 1;
944 rect.left = cur_x - dash_len + 1;
945 rect.right = cur_x + 1;
947 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
948 cur_x -= dash_len;
949 skip_dash(pdev, dash_len);
954 pdev->dash_pos = start_pos;
955 skip_dash(pdev, right - left + 1);
957 else if(start->x == end->x) /* vline */
959 BOOL t_to_b;
960 INT top, bottom, cur_y;
962 rect.left = start->x;
963 rect.right = start->x + 1;
965 if(start->y <= end->y)
967 top = start->y;
968 bottom = end->y - 1;
969 t_to_b = TRUE;
971 else
973 top = end->y + 1;
974 bottom = start->y;
975 t_to_b = FALSE;
978 rect.top = min( start->y, end->y );
979 rect.bottom = max( start->y, end->y ) + 1;
980 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
981 for (i = 0; i < clipped_rects.count; i++)
983 if(clipped_rects.rects[i].right > start->x && clipped_rects.rects[i].left <= start->x)
985 int clipped_top = max(clipped_rects.rects[i].top, top);
986 int clipped_bottom = min(clipped_rects.rects[i].bottom - 1, bottom);
988 pdev->dash_pos = start_pos;
990 if(t_to_b)
992 cur_y = clipped_top;
993 if(cur_y != top)
994 skip_dash(pdev, clipped_top - top);
996 while(cur_y <= clipped_bottom)
998 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
999 dash_len = pdev->dash_pos.left_in_dash;
1000 if(cur_y + dash_len > clipped_bottom + 1)
1001 dash_len = clipped_bottom - cur_y + 1;
1002 rect.top = cur_y;
1003 rect.bottom = cur_y + dash_len;
1005 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
1006 cur_y += dash_len;
1007 skip_dash(pdev, dash_len);
1010 else
1012 cur_y = clipped_bottom;
1013 if(cur_y != bottom)
1014 skip_dash(pdev, bottom - clipped_bottom);
1016 while(cur_y >= clipped_top)
1018 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
1019 dash_len = pdev->dash_pos.left_in_dash;
1020 if(cur_y - dash_len < clipped_top - 1)
1021 dash_len = cur_y - clipped_top + 1;
1022 rect.top = cur_y - dash_len + 1;
1023 rect.bottom = cur_y + 1;
1025 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
1026 cur_y -= dash_len;
1027 skip_dash(pdev, dash_len);
1032 pdev->dash_pos = start_pos;
1033 skip_dash(pdev, bottom - top + 1);
1035 else
1037 bres_params clip_params;
1038 struct line_params line_params;
1039 POINT p1 = crop_coords( *start ), p2 = crop_coords( *end );
1041 init_bres_params( &p1, &p2, &clip_params, &line_params, &rect );
1042 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
1043 for (i = 0; i < clipped_rects.count; i++)
1045 POINT clipped_start, clipped_end;
1046 int clip_status;
1047 clip_status = clip_line(&p1, &p2, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
1049 if(clip_status)
1051 int m = abs(clipped_start.x - p1.x);
1052 int n = abs(clipped_start.y - p1.y);
1054 pdev->dash_pos = start_pos;
1056 if (line_params.x_major)
1058 line_params.err_start = 2 * clip_params.dy - clip_params.dx
1059 + m * 2 * clip_params.dy - n * 2 * clip_params.dx;
1060 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
1061 skip_dash(pdev, m);
1063 else
1065 line_params.err_start = 2 * clip_params.dx - clip_params.dy
1066 + n * 2 * clip_params.dx - m * 2 * clip_params.dy;
1067 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
1068 skip_dash(pdev, n);
1070 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
1072 bres_line_with_bias( &clipped_start, &line_params, dashed_pen_line_callback, pdev );
1074 if(clip_status == 2) break; /* completely unclipped, so we can finish */
1077 pdev->dash_pos = start_pos;
1078 if(line_params.x_major)
1079 skip_dash(pdev, clip_params.dx);
1080 else
1081 skip_dash(pdev, clip_params.dy);
1084 free_clipped_rects( &clipped_rects );
1085 return TRUE;
1088 static BOOL dashed_pen_line_region(dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region)
1090 int i, dash_len;
1091 RECT rect;
1093 rect.left = start->x;
1094 rect.top = start->y;
1095 rect.right = start->x + 1;
1096 rect.bottom = start->y + 1;
1098 if (start->y == end->y) /* hline */
1100 if (start->x <= end->x)
1102 for (i = start->x; i < end->x; i += dash_len)
1104 dash_len = min( pdev->dash_pos.left_in_dash, end->x - i );
1105 if (pdev->dash_pos.mark)
1107 rect.left = i;
1108 rect.right = i + dash_len;
1109 add_rect_to_region( region, &rect );
1111 skip_dash(pdev, dash_len);
1114 else
1116 for (i = start->x; i > end->x; i -= dash_len)
1118 dash_len = min( pdev->dash_pos.left_in_dash, i - end->x );
1119 if (pdev->dash_pos.mark)
1121 rect.left = i - dash_len + 1;
1122 rect.right = i + 1;
1123 add_rect_to_region( region, &rect );
1125 skip_dash(pdev, dash_len);
1129 else if (start->x == end->x) /* vline */
1131 if (start->y <= end->y)
1133 for (i = start->y; i < end->y; i += dash_len)
1135 dash_len = min( pdev->dash_pos.left_in_dash, end->y - i );
1136 if (pdev->dash_pos.mark)
1138 rect.top = i;
1139 rect.bottom = i + dash_len;
1140 add_rect_to_region( region, &rect );
1142 skip_dash(pdev, dash_len);
1145 else
1147 for (i = start->y; i > end->y; i -= dash_len)
1149 dash_len = min( pdev->dash_pos.left_in_dash, i - end->y );
1150 if (pdev->dash_pos.mark)
1152 rect.top = i - dash_len + 1;
1153 rect.bottom = i + 1;
1154 add_rect_to_region( region, &rect );
1156 skip_dash(pdev, dash_len);
1160 else
1162 INT dx = end->x - start->x, dy = end->y - start->y;
1163 INT abs_dx = abs(dx), abs_dy = abs(dy);
1164 DWORD octant = get_octant_mask(dx, dy);
1165 INT bias = get_bias( octant );
1166 int x_inc = is_x_increasing( octant ) ? 1 : -1;
1167 int y_inc = is_y_increasing( octant ) ? 1 : -1;
1169 if (is_xmajor( octant ))
1171 int err_add_1 = 2 * abs_dy - 2 * abs_dx;
1172 int err_add_2 = 2 * abs_dy;
1173 int err = 2 * abs_dy - abs_dx;
1175 while (abs_dx--)
1177 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1178 skip_dash(pdev, 1);
1179 rect.left += x_inc;
1180 rect.right += x_inc;
1181 if (err + bias > 0)
1183 rect.top += y_inc;
1184 rect.bottom += y_inc;
1185 err += err_add_1;
1187 else err += err_add_2;
1191 else
1193 int err_add_1 = 2 * abs_dx - 2 * abs_dy;
1194 int err_add_2 = 2 * abs_dx;
1195 int err = 2 * abs_dx - abs_dy;
1197 while (abs_dy--)
1199 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1200 skip_dash(pdev, 1);
1201 rect.top += y_inc;
1202 rect.bottom += y_inc;
1203 if (err + bias > 0)
1205 rect.left += x_inc;
1206 rect.right += x_inc;
1207 err += err_add_1;
1209 else err += err_add_2;
1213 return TRUE;
1216 static BOOL dashed_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1218 DC *dc = get_physdev_dc( &pdev->dev );
1219 int i;
1221 assert( num >= 2 );
1223 if (region)
1225 for (i = 0; i < num - 1; i++)
1226 if (!dashed_pen_line_region( pdev, pts + i, pts + i + 1, region ))
1227 return FALSE;
1228 if (close) return dashed_pen_line_region( pdev, pts + num - 1, pts, region );
1230 else
1232 get_color_masks( dc, &pdev->dib, dc->ROPmode, pdev->pen_brush.colorref,
1233 pdev->pen_is_ext ? TRANSPARENT : dc->backgroundMode,
1234 &pdev->dash_masks[1], &pdev->dash_masks[0] );
1236 for (i = 0; i < num - 1; i++)
1237 if (!dashed_pen_line( pdev, pts + i, pts + i + 1 ))
1238 return FALSE;
1239 if (close) return dashed_pen_line( pdev, pts + num - 1, pts );
1241 return TRUE;
1244 static BOOL null_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1246 return TRUE;
1249 struct face
1251 POINT start, end;
1252 int dx, dy;
1255 static void add_cap( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt )
1257 switch (pdev->pen_endcap)
1259 default: FIXME( "Unknown end cap %x\n", pdev->pen_endcap );
1260 /* fall through */
1261 case PS_ENDCAP_ROUND:
1262 OffsetRgn( round_cap, pt->x, pt->y );
1263 CombineRgn( region, region, round_cap, RGN_OR );
1264 OffsetRgn( round_cap, -pt->x, -pt->y );
1265 return;
1267 case PS_ENDCAP_SQUARE: /* already been handled */
1268 case PS_ENDCAP_FLAT:
1269 return;
1273 #define round( f ) (((f) > 0) ? (f) + 0.5 : (f) - 0.5)
1275 /*******************************************************************************
1276 * create_miter_region
1278 * We need to calculate the intersection of two lines. We know a point
1279 * on each line (a face start and the other face end point) and
1280 * the direction vector of each line eg. (dx_1, dy_1).
1282 * (x, y) = (x_1, y_1) + u * (dx_1, dy_1) = (x_2, y_2) + v * (dx_2, dy_2)
1283 * solving (eg using Cramer's rule) gives:
1284 * u = ((x_2 - x_1) dy_2 - (y_2 - y_1) dx_2) / det
1285 * with det = dx_1 dy_2 - dx_2 dy_1
1286 * substituting back in and simplifying gives
1287 * (x, y) = a (dx_1, dy_1) - b (dx_2, dy_2)
1288 * with a = (x_2 dy_2 - y_2 dx_2) / det
1289 * and b = (x_1 dy_1 - y_1 dx_1) / det
1291 static HRGN create_miter_region( dibdrv_physdev *pdev, const POINT *pt,
1292 const struct face *face_1, const struct face *face_2 )
1294 DC *dc = get_physdev_dc( &pdev->dev );
1295 int det = face_1->dx * face_2->dy - face_1->dy * face_2->dx;
1296 POINT pt_1, pt_2, pts[5];
1297 double a, b, x, y;
1299 if (det == 0) return 0;
1301 if (det < 0)
1303 const struct face *tmp = face_1;
1304 face_1 = face_2;
1305 face_2 = tmp;
1306 det = -det;
1309 pt_1 = face_1->start;
1310 pt_2 = face_2->end;
1312 a = (double)(pt_2.x * face_2->dy - pt_2.y * face_2->dx) / det;
1313 b = (double)(pt_1.x * face_1->dy - pt_1.y * face_1->dx) / det;
1315 x = a * face_1->dx - b * face_2->dx;
1316 y = a * face_1->dy - b * face_2->dy;
1318 if (((x - pt->x) * (x - pt->x) + (y - pt->y) * (y - pt->y)) * 4 >
1319 dc->miterLimit * dc->miterLimit * pdev->pen_width * pdev->pen_width)
1320 return 0;
1322 pts[0] = face_2->start;
1323 pts[1] = face_1->start;
1324 pts[2].x = round( x );
1325 pts[2].y = round( y );
1326 pts[3] = face_2->end;
1327 pts[4] = face_1->end;
1329 return CreatePolygonRgn( pts, 5, ALTERNATE );
1332 static void add_join( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt,
1333 const struct face *face_1, const struct face *face_2 )
1335 HRGN join;
1336 POINT pts[4];
1338 switch (pdev->pen_join)
1340 default: FIXME( "Unknown line join %x\n", pdev->pen_join );
1341 /* fall through */
1342 case PS_JOIN_ROUND:
1343 OffsetRgn( round_cap, pt->x, pt->y );
1344 CombineRgn( region, region, round_cap, RGN_OR );
1345 OffsetRgn( round_cap, -pt->x, -pt->y );
1346 return;
1348 case PS_JOIN_MITER:
1349 join = create_miter_region( pdev, pt, face_1, face_2 );
1350 if (join) break;
1351 /* fall through */
1352 case PS_JOIN_BEVEL:
1353 pts[0] = face_1->start;
1354 pts[1] = face_2->end;
1355 pts[2] = face_1->end;
1356 pts[3] = face_2->start;
1357 join = CreatePolygonRgn( pts, 4, ALTERNATE );
1358 break;
1361 CombineRgn( region, region, join, RGN_OR );
1362 DeleteObject( join );
1363 return;
1366 static BOOL wide_line_segment( dibdrv_physdev *pdev, HRGN total,
1367 const POINT *pt_1, const POINT *pt_2, int dx, int dy,
1368 BOOL need_cap_1, BOOL need_cap_2, struct face *face_1, struct face *face_2 )
1370 RECT rect;
1371 BOOL sq_cap_1 = need_cap_1 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1372 BOOL sq_cap_2 = need_cap_2 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1374 if (dx == 0 && dy == 0) return FALSE;
1376 if (dy == 0)
1378 rect.left = min( pt_1->x, pt_2->x );
1379 rect.right = max( pt_1->x, pt_2->x );
1380 rect.top = pt_1->y - pdev->pen_width / 2;
1381 rect.bottom = rect.top + pdev->pen_width;
1382 if ((sq_cap_1 && dx > 0) || (sq_cap_2 && dx < 0)) rect.left -= pdev->pen_width / 2;
1383 if ((sq_cap_2 && dx > 0) || (sq_cap_1 && dx < 0)) rect.right += pdev->pen_width / 2;
1384 add_rect_to_region( total, &rect );
1385 if (dx > 0)
1387 face_1->start.x = face_1->end.x = rect.left;
1388 face_1->start.y = face_2->end.y = rect.bottom;
1389 face_1->end.y = face_2->start.y = rect.top;
1390 face_2->start.x = face_2->end.x = rect.right - 1;
1392 else
1394 face_1->start.x = face_1->end.x = rect.right;
1395 face_1->start.y = face_2->end.y = rect.top;
1396 face_1->end.y = face_2->start.y = rect.bottom;
1397 face_2->start.x = face_2->end.x = rect.left + 1;
1400 else if (dx == 0)
1402 rect.top = min( pt_1->y, pt_2->y );
1403 rect.bottom = max( pt_1->y, pt_2->y );
1404 rect.left = pt_1->x - pdev->pen_width / 2;
1405 rect.right = rect.left + pdev->pen_width;
1406 if ((sq_cap_1 && dy > 0) || (sq_cap_2 && dy < 0)) rect.top -= pdev->pen_width / 2;
1407 if ((sq_cap_2 && dy > 0) || (sq_cap_1 && dy < 0)) rect.bottom += pdev->pen_width / 2;
1408 add_rect_to_region( total, &rect );
1409 if (dy > 0)
1411 face_1->start.x = face_2->end.x = rect.left;
1412 face_1->start.y = face_1->end.y = rect.top;
1413 face_1->end.x = face_2->start.x = rect.right;
1414 face_2->start.y = face_2->end.y = rect.bottom - 1;
1416 else
1418 face_1->start.x = face_2->end.x = rect.right;
1419 face_1->start.y = face_1->end.y = rect.bottom;
1420 face_1->end.x = face_2->start.x = rect.left;
1421 face_2->start.y = face_2->end.y = rect.top + 1;
1424 else
1426 double len = hypot( dx, dy );
1427 double width_x, width_y;
1428 POINT seg_pts[4];
1429 POINT wide_half, narrow_half;
1430 HRGN segment;
1432 width_x = pdev->pen_width * abs( dy ) / len;
1433 width_y = pdev->pen_width * abs( dx ) / len;
1435 narrow_half.x = round( width_x / 2 );
1436 narrow_half.y = round( width_y / 2 );
1437 wide_half.x = round( (width_x + 1) / 2 );
1438 wide_half.y = round( (width_y + 1) / 2 );
1440 if (dx < 0)
1442 wide_half.y = -wide_half.y;
1443 narrow_half.y = -narrow_half.y;
1446 if (dy < 0)
1448 POINT tmp = narrow_half; narrow_half = wide_half; wide_half = tmp;
1449 wide_half.x = -wide_half.x;
1450 narrow_half.x = -narrow_half.x;
1453 seg_pts[0].x = pt_1->x - narrow_half.x;
1454 seg_pts[0].y = pt_1->y + narrow_half.y;
1455 seg_pts[1].x = pt_1->x + wide_half.x;
1456 seg_pts[1].y = pt_1->y - wide_half.y;
1457 seg_pts[2].x = pt_2->x + wide_half.x;
1458 seg_pts[2].y = pt_2->y - wide_half.y;
1459 seg_pts[3].x = pt_2->x - narrow_half.x;
1460 seg_pts[3].y = pt_2->y + narrow_half.y;
1462 if (sq_cap_1)
1464 seg_pts[0].x -= narrow_half.y;
1465 seg_pts[1].x -= narrow_half.y;
1466 seg_pts[0].y -= narrow_half.x;
1467 seg_pts[1].y -= narrow_half.x;
1470 if (sq_cap_2)
1472 seg_pts[2].x += wide_half.y;
1473 seg_pts[3].x += wide_half.y;
1474 seg_pts[2].y += wide_half.x;
1475 seg_pts[3].y += wide_half.x;
1478 segment = CreatePolygonRgn( seg_pts, 4, ALTERNATE );
1479 CombineRgn( total, total, segment, RGN_OR );
1480 DeleteObject( segment );
1482 face_1->start = seg_pts[0];
1483 face_1->end = seg_pts[1];
1484 face_2->start = seg_pts[2];
1485 face_2->end = seg_pts[3];
1488 face_1->dx = face_2->dx = dx;
1489 face_1->dy = face_2->dy = dy;
1491 return TRUE;
1494 static void wide_line_segments( dibdrv_physdev *pdev, int num, const POINT *pts, BOOL close,
1495 int start, int count, const POINT *first_pt, const POINT *last_pt,
1496 HRGN round_cap, HRGN total )
1498 int i;
1499 struct face face_1, face_2, prev_face, first_face;
1500 const POINT *pt_1, *pt_2;
1502 if (!close)
1504 add_cap( pdev, total, round_cap, first_pt );
1505 add_cap( pdev, total, round_cap, last_pt );
1508 if (count == 1)
1510 pt_1 = &pts[start];
1511 pt_2 = &pts[(start + 1) % num];
1512 wide_line_segment( pdev, total, first_pt, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1513 TRUE, TRUE, &face_1, &face_2 );
1514 return;
1517 pt_1 = &pts[start];
1518 pt_2 = &pts[(start + 1) % num];
1519 wide_line_segment( pdev, total, first_pt, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1520 !close, FALSE, &first_face, &prev_face );
1523 for (i = 1; i < count - 1; i++)
1525 pt_1 = &pts[(start + i) % num];
1526 pt_2 = &pts[(start + i + 1) % num];
1527 if (wide_line_segment( pdev, total, pt_1, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1528 FALSE, FALSE, &face_1, &face_2 ))
1530 add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1531 prev_face = face_2;
1535 pt_1 = &pts[(start + count - 1) % num];
1536 pt_2 = &pts[(start + count) % num];
1537 wide_line_segment( pdev, total, pt_1, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1538 FALSE, !close, &face_1, &face_2 );
1539 add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1540 if (close) add_join( pdev, total, round_cap, last_pt, &face_2, &first_face );
1543 static BOOL wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1545 HRGN round_cap = 0;
1547 assert( total != 0 ); /* wide pens should always be drawn through a region */
1548 assert( num >= 2 );
1550 /* skip empty segments */
1551 while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1552 while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1554 if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1555 round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1556 (pdev->pen_width + 1) / 2 + 1, (pdev->pen_width + 1) / 2 + 1 );
1558 if (close)
1559 wide_line_segments( pdev, num, pts, TRUE, 0, num, &pts[0], &pts[0], round_cap, total );
1560 else
1561 wide_line_segments( pdev, num, pts, FALSE, 0, num - 1, &pts[0], &pts[num - 1], round_cap, total );
1563 if (round_cap) DeleteObject( round_cap );
1564 return TRUE;
1567 static BOOL dashed_wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1569 int i, start, cur_len, initial_num = 0;
1570 POINT initial_point, start_point, end_point;
1571 HRGN round_cap = 0;
1573 assert( total != 0 ); /* wide pens should always be drawn through a region */
1574 assert( num >= 2 );
1576 /* skip empty segments */
1577 while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1578 while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1580 if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1581 round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1582 (pdev->pen_width + 1) / 2 + 1, (pdev->pen_width + 1) / 2 + 1);
1584 start = 0;
1585 cur_len = 0;
1586 start_point = pts[0];
1588 for (i = 0; i < (close ? num : num - 1); i++)
1590 const POINT *pt_1 = pts + i;
1591 const POINT *pt_2 = pts + ((close && i == num - 1) ? 0 : i + 1);
1592 int dx = pt_2->x - pt_1->x;
1593 int dy = pt_2->y - pt_1->y;
1595 if (dx == 0 && dy == 0) continue;
1597 if (dy == 0)
1599 if (abs( dx ) - cur_len < pdev->dash_pos.left_in_dash)
1601 skip_dash( pdev, abs( dx ) - cur_len );
1602 cur_len = 0;
1603 continue;
1605 cur_len += pdev->dash_pos.left_in_dash;
1606 dx = (dx > 0) ? cur_len : -cur_len;
1608 else if (dx == 0)
1610 if (abs( dy ) - cur_len < pdev->dash_pos.left_in_dash)
1612 skip_dash( pdev, abs( dy ) - cur_len );
1613 cur_len = 0;
1614 continue;
1616 cur_len += pdev->dash_pos.left_in_dash;
1617 dy = (dy > 0) ? cur_len : -cur_len;
1619 else
1621 double len = hypot( dx, dy );
1623 if (len - cur_len < pdev->dash_pos.left_in_dash)
1625 skip_dash( pdev, len - cur_len );
1626 cur_len = 0;
1627 continue;
1629 cur_len += pdev->dash_pos.left_in_dash;
1630 dx = dx * cur_len / len;
1631 dy = dy * cur_len / len;
1633 end_point.x = pt_1->x + dx;
1634 end_point.y = pt_1->y + dy;
1636 if (pdev->dash_pos.mark)
1638 if (!initial_num && close) /* this is the first dash, save it for later */
1640 initial_num = i - start + 1;
1641 initial_point = end_point;
1643 else wide_line_segments( pdev, num, pts, FALSE, start, i - start + 1,
1644 &start_point, &end_point, round_cap, total );
1646 if (!initial_num) initial_num = -1; /* no need to close it */
1648 skip_dash( pdev, pdev->dash_pos.left_in_dash );
1649 start_point = end_point;
1650 start = i;
1651 i--; /* go on with the same segment */
1654 if (pdev->dash_pos.mark) /* we have a final dash */
1656 int count;
1658 if (initial_num > 0)
1660 count = num - start + initial_num;
1661 end_point = initial_point;
1663 else if (close)
1665 count = num - start;
1666 end_point = pts[0];
1668 else
1670 count = num - start - 1;
1671 end_point = pts[num - 1];
1673 wide_line_segments( pdev, num, pts, FALSE, start, count,
1674 &start_point, &end_point, round_cap, total );
1676 else if (initial_num > 0) /* initial dash only */
1678 wide_line_segments( pdev, num, pts, FALSE, 0, initial_num,
1679 &pts[0], &initial_point, round_cap, total );
1682 if (round_cap) DeleteObject( round_cap );
1683 return TRUE;
1686 static const dash_pattern dash_patterns_cosmetic[4] =
1688 {2, {18, 6}, 24}, /* PS_DASH */
1689 {2, {3, 3}, 6}, /* PS_DOT */
1690 {4, {9, 6, 3, 6}, 24}, /* PS_DASHDOT */
1691 {6, {9, 3, 3, 3, 3, 3}, 24} /* PS_DASHDOTDOT */
1694 static const dash_pattern dash_patterns_geometric[4] =
1696 {2, {3, 1}, 4}, /* PS_DASH */
1697 {2, {1, 1}, 2}, /* PS_DOT */
1698 {4, {3, 1, 1, 1}, 6}, /* PS_DASHDOT */
1699 {6, {3, 1, 1, 1, 1, 1}, 8} /* PS_DASHDOTDOT */
1702 static inline void set_dash_pattern( dash_pattern *pattern, DWORD count, DWORD *dashes )
1704 DWORD i;
1706 pattern->count = count;
1707 pattern->total_len = 0;
1708 memcpy( pattern->dashes, dashes, count * sizeof(DWORD) );
1709 for (i = 0; i < count; i++) pattern->total_len += dashes[i];
1710 if (pattern->count % 2) pattern->total_len *= 2;
1713 static inline void scale_dash_pattern( dash_pattern *pattern, DWORD scale, DWORD endcap )
1715 DWORD i;
1717 for (i = 0; i < pattern->count; i++) pattern->dashes[i] *= scale;
1718 pattern->total_len *= scale;
1720 if (endcap != PS_ENDCAP_FLAT) /* shrink the dashes to leave room for the caps */
1722 for (i = 0; i < pattern->count; i += 2)
1724 pattern->dashes[i] -= scale;
1725 pattern->dashes[i + 1] += scale;
1730 static inline int get_pen_device_width( DC *dc, int width )
1732 POINT pts[2];
1734 if (!width) return 1;
1735 pts[0].x = pts[0].y = pts[1].y = 0;
1736 pts[1].x = width;
1737 lp_to_dp( dc, pts, 2 );
1738 width = floor( hypot( pts[1].x - pts[0].x, pts[1].y - pts[0].y ));
1739 return max( width, 1 );
1742 /***********************************************************************
1743 * dibdrv_SetDCPenColor
1745 COLORREF dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1747 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1748 DC *dc = get_physdev_dc( dev );
1750 if (dc->hPen == GetStockObject( DC_PEN ))
1751 pdev->pen_brush.colorref = color;
1753 return color;
1756 /**********************************************************************
1757 * fill_with_pixel
1759 * Fill a number of rectangles with a given pixel color and rop mode
1761 BOOL fill_with_pixel( DC *dc, dib_info *dib, DWORD pixel, int num, const RECT *rects, INT rop )
1763 rop_mask mask;
1765 calc_rop_masks( rop, pixel, &mask );
1766 dib->funcs->solid_rects( dib, num, rects, mask.and, mask.xor );
1767 return TRUE;
1770 /**********************************************************************
1771 * solid_brush
1773 * Fill a number of rectangles with the solid brush
1775 static BOOL solid_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1776 int num, const RECT *rects, const POINT *brush_org, INT rop)
1778 DC *dc = get_physdev_dc( &pdev->dev );
1779 DWORD color = get_pixel_color( dc, &pdev->dib, brush->colorref, TRUE );
1781 return fill_with_pixel( dc, dib, color, num, rects, rop );
1784 static BOOL alloc_brush_mask_bits( dib_brush *brush )
1786 DWORD size = brush->dib.height * abs(brush->dib.stride);
1788 assert(brush->masks.and == NULL);
1789 assert(brush->masks.xor == NULL);
1790 assert(brush->dib.stride > 0);
1792 if (!(brush->masks.xor = HeapAlloc(GetProcessHeap(), 0, 2 * size))) return FALSE;
1793 brush->masks.and = (char *)brush->masks.xor + size;
1794 return TRUE;
1797 static void free_brush_mask_bits( dib_brush *brush )
1799 if (brush->masks.xor != brush->dib.bits.ptr) HeapFree(GetProcessHeap(), 0, brush->masks.xor);
1800 brush->masks.and = brush->masks.xor = NULL;
1803 void free_pattern_brush( dib_brush *brush )
1805 free_brush_mask_bits( brush );
1806 free_dib_info( &brush->dib );
1809 static BOOL create_pattern_brush_bits( dib_brush *brush )
1811 DWORD size = brush->dib.height * abs(brush->dib.stride);
1812 DWORD *brush_bits = brush->dib.bits.ptr;
1813 DWORD *and_bits, *xor_bits;
1815 if (brush->rop == R2_COPYPEN)
1817 brush->masks.xor = brush_bits; /* use the pattern bits directly */
1818 return TRUE;
1821 if (!alloc_brush_mask_bits( brush )) return FALSE;
1823 and_bits = brush->masks.and;
1824 xor_bits = brush->masks.xor;
1826 while(size)
1828 calc_and_xor_masks(brush->rop, *brush_bits++, and_bits++, xor_bits++);
1829 size -= 4;
1832 if (!rop_needs_and_mask( brush->rop )) brush->masks.and = NULL; /* ignore the and mask */
1834 return TRUE;
1837 static const BYTE hatches[6][8] =
1839 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1840 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
1841 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL */
1842 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL */
1843 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
1844 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
1847 static BOOL init_hatch_brush( dibdrv_physdev *pdev, dib_brush *brush )
1849 /* Just initialise brush dib with the color / sizing info. We don't
1850 need the bits as we'll calculate the rop masks straight from
1851 the hatch patterns. */
1853 copy_dib_color_info(&brush->dib, &pdev->dib);
1854 brush->dib.width = 8;
1855 brush->dib.height = 8;
1856 brush->dib.stride = get_dib_stride( brush->dib.width, brush->dib.bit_count );
1857 brush->dib.rect.left = 0;
1858 brush->dib.rect.top = 0;
1859 brush->dib.rect.right = 8;
1860 brush->dib.rect.bottom = 8;
1861 return alloc_brush_mask_bits( brush );
1864 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect)
1866 DC *dc = get_physdev_dc( &pdev->dev );
1867 rop_mask fg_mask, bg_mask;
1869 if (!init_hatch_brush( pdev, brush )) return FALSE;
1871 get_color_masks( dc, &pdev->dib, brush->rop, brush->colorref, dc->backgroundMode,
1872 &fg_mask, &bg_mask );
1874 if (brush->colorref & (1 << 24)) /* PALETTEINDEX */
1875 *needs_reselect = TRUE;
1876 if (dc->backgroundMode != TRANSPARENT && (dc->backgroundColor & (1 << 24)))
1877 *needs_reselect = TRUE;
1879 brush->dib.funcs->create_rop_masks( &brush->dib, hatches[brush->hatch],
1880 &fg_mask, &bg_mask, &brush->masks );
1882 if (!fg_mask.and && !bg_mask.and) brush->masks.and = NULL; /* ignore the and mask */
1884 return TRUE;
1887 static BOOL create_dither_brush_bits(dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect)
1889 DC *dc = get_physdev_dc( &pdev->dev );
1890 COLORREF rgb;
1891 DWORD pixel;
1892 BOOL got_pixel;
1894 if (!init_hatch_brush( pdev, brush )) return FALSE;
1896 if (brush->colorref & (1 << 24)) /* PALETTEINDEX */
1897 *needs_reselect = TRUE;
1899 rgb = make_rgb_colorref( dc, &pdev->dib, brush->colorref, &got_pixel, &pixel );
1901 brush->dib.funcs->create_dither_masks( &brush->dib, brush->rop, rgb, &brush->masks );
1903 if (!rop_needs_and_mask( brush->rop )) brush->masks.and = NULL; /* ignore the and mask */
1905 return TRUE;
1908 static BOOL matching_pattern_format( dib_info *dib, dib_info *pattern )
1910 if (dib->bit_count != pattern->bit_count) return FALSE;
1911 if (dib->stride != pattern->stride) return FALSE;
1913 switch (dib->bit_count)
1915 case 1:
1916 case 4:
1917 case 8:
1918 if (dib->color_table_size != pattern->color_table_size) return FALSE;
1919 return !memcmp( dib->color_table, pattern->color_table, dib->color_table_size * sizeof(RGBQUAD) );
1920 case 16:
1921 case 32:
1922 return (dib->red_mask == pattern->red_mask &&
1923 dib->green_mask == pattern->green_mask &&
1924 dib->blue_mask == pattern->blue_mask);
1926 return TRUE;
1929 static BOOL select_pattern_brush( dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect )
1931 DC *dc = get_physdev_dc( &pdev->dev );
1932 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1933 BITMAPINFO *info = (BITMAPINFO *)buffer;
1934 RGBQUAD color_table[2];
1935 dib_info pattern;
1936 BOOL dither = (brush->dib.bit_count == 1) || (pdev->dib.bit_count == 1);
1938 if (brush->pattern.info->bmiHeader.biClrUsed && brush->pattern.usage == DIB_PAL_COLORS)
1940 copy_bitmapinfo( info, brush->pattern.info );
1941 fill_color_table_from_pal_colors( info, pdev->dev.hdc );
1942 init_dib_info_from_bitmapinfo( &pattern, info, brush->pattern.bits.ptr );
1943 *needs_reselect = TRUE;
1945 else
1947 init_dib_info_from_bitmapinfo( &pattern, brush->pattern.info, brush->pattern.bits.ptr );
1950 if (pattern.bit_count == 1 && !pattern.color_table)
1951 dither = FALSE; /* monochrome DDB pattern brushes don't get dithered */
1953 if (pattern.bit_count == 1 && !pattern.color_table &&
1954 (pdev->dib.bit_count != 1 || pdev->dib.color_table))
1956 /* monochrome DDB pattern uses DC colors */
1957 DWORD pixel;
1958 BOOL got_pixel;
1959 COLORREF color;
1961 color = make_rgb_colorref( dc, &pdev->dib, dc->textColor, &got_pixel, &pixel );
1962 color_table[0].rgbRed = GetRValue( color );
1963 color_table[0].rgbGreen = GetGValue( color );
1964 color_table[0].rgbBlue = GetBValue( color );
1965 color_table[0].rgbReserved = 0;
1967 color = make_rgb_colorref( dc, &pdev->dib, dc->backgroundColor, &got_pixel, &pixel );
1968 color_table[1].rgbRed = GetRValue( color );
1969 color_table[1].rgbGreen = GetGValue( color );
1970 color_table[1].rgbBlue = GetBValue( color );
1971 color_table[1].rgbReserved = 0;
1973 pattern.color_table = color_table;
1974 pattern.color_table_size = 2;
1975 *needs_reselect = TRUE;
1978 copy_dib_color_info(&brush->dib, &pdev->dib);
1980 brush->dib.height = pattern.height;
1981 brush->dib.width = pattern.width;
1982 brush->dib.stride = get_dib_stride( brush->dib.width, brush->dib.bit_count );
1983 brush->dib.rect = pattern.rect;
1985 if (!dither && matching_pattern_format( &brush->dib, &pattern ))
1987 brush->dib.bits.ptr = pattern.bits.ptr;
1988 brush->dib.bits.is_copy = FALSE;
1989 brush->dib.bits.free = NULL;
1991 else
1993 brush->dib.bits.ptr = HeapAlloc( GetProcessHeap(), 0, brush->dib.height * brush->dib.stride );
1994 brush->dib.bits.is_copy = TRUE;
1995 brush->dib.bits.free = free_heap_bits;
1996 brush->dib.funcs->convert_to(&brush->dib, &pattern, &pattern.rect, dither);
1998 return TRUE;
2001 /**********************************************************************
2002 * pattern_brush
2004 * Fill a number of rectangles with the pattern brush
2005 * FIXME: Should we insist l < r && t < b? Currently we assume this.
2007 static BOOL pattern_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
2008 int num, const RECT *rects, const POINT *brush_org, INT rop)
2010 BOOL needs_reselect = FALSE;
2012 if (rop != brush->rop)
2014 free_brush_mask_bits( brush );
2015 brush->rop = rop;
2018 if(brush->masks.xor == NULL)
2020 switch(brush->style)
2022 case BS_DIBPATTERN:
2023 if (!brush->dib.bits.ptr && !select_pattern_brush( pdev, brush, &needs_reselect ))
2024 return FALSE;
2025 if(!create_pattern_brush_bits( brush ))
2026 return FALSE;
2027 break;
2029 case BS_SOLID:
2030 if(!create_dither_brush_bits(pdev, brush, &needs_reselect))
2031 return FALSE;
2032 break;
2034 case BS_HATCHED:
2035 if(!create_hatch_brush_bits(pdev, brush, &needs_reselect))
2036 return FALSE;
2037 break;
2039 default:
2040 ERR("Unexpected brush style %d\n", brush->style);
2041 return FALSE;
2045 dib->funcs->pattern_rects( dib, num, rects, brush_org, &brush->dib, &brush->masks );
2047 if (needs_reselect) free_pattern_brush( brush );
2048 return TRUE;
2051 static BOOL null_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
2052 int num, const RECT *rects, const POINT *brush_org, INT rop)
2054 return TRUE;
2057 static BOOL brush_needs_dithering( dibdrv_physdev *pdev, COLORREF color )
2059 int i;
2060 RGBQUAD rgb;
2061 const RGBQUAD *color_table = get_default_color_table( pdev->dib.bit_count );
2063 if (!color_table) return FALSE;
2064 if (pdev->dib.color_table) return FALSE;
2065 if (color & (1 << 24)) return TRUE; /* PALETTEINDEX */
2066 if (color >> 16 == 0x10ff) return FALSE; /* DIBINDEX */
2068 rgb = rgbquad_from_colorref( color );
2069 for (i = 0; i < (1 << pdev->dib.bit_count); i++)
2070 if (rgbquad_equal( &color_table[i], &rgb )) return FALSE;
2072 return TRUE;
2075 static void select_brush( dibdrv_physdev *pdev, dib_brush *brush,
2076 const LOGBRUSH *logbrush, const struct brush_pattern *pattern )
2078 free_pattern_brush( brush );
2080 if (pattern)
2082 brush->style = BS_DIBPATTERN;
2083 brush->pattern = *pattern; /* brush is actually selected only when it's used */
2084 brush->rects = pattern_brush;
2086 else
2088 brush->style = logbrush->lbStyle;
2089 brush->colorref = logbrush->lbColor;
2090 brush->hatch = logbrush->lbHatch;
2092 switch (logbrush->lbStyle)
2094 case BS_NULL: brush->rects = null_brush; break;
2095 case BS_HATCHED: brush->rects = pattern_brush; break;
2096 case BS_SOLID:
2097 brush->rects = brush_needs_dithering( pdev, brush->colorref ) ? pattern_brush : solid_brush;
2098 break;
2103 /***********************************************************************
2104 * dibdrv_SelectBrush
2106 HBRUSH dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush, const struct brush_pattern *pattern )
2108 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2109 DC *dc = get_physdev_dc( dev );
2110 LOGBRUSH logbrush;
2112 TRACE("(%p, %p)\n", dev, hbrush);
2114 GetObjectW( hbrush, sizeof(logbrush), &logbrush );
2116 if (hbrush == GetStockObject( DC_BRUSH ))
2117 logbrush.lbColor = dc->dcBrushColor;
2119 select_brush( pdev, &pdev->brush, &logbrush, pattern );
2120 return hbrush;
2123 /***********************************************************************
2124 * dibdrv_SelectPen
2126 HPEN dibdrv_SelectPen( PHYSDEV dev, HPEN hpen, const struct brush_pattern *pattern )
2128 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2129 DC *dc = get_physdev_dc( dev );
2130 LOGPEN logpen;
2131 LOGBRUSH logbrush;
2132 EXTLOGPEN *elp = NULL;
2134 TRACE("(%p, %p)\n", dev, hpen);
2136 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
2138 /* must be an extended pen */
2139 INT size = GetObjectW( hpen, 0, NULL );
2141 if (!size) return 0;
2143 elp = HeapAlloc( GetProcessHeap(), 0, size );
2145 GetObjectW( hpen, size, elp );
2146 logpen.lopnStyle = elp->elpPenStyle;
2147 logpen.lopnWidth.x = elp->elpWidth;
2148 /* cosmetic ext pens are always 1-pixel wide */
2149 if (!(logpen.lopnStyle & PS_GEOMETRIC)) logpen.lopnWidth.x = 0;
2151 logbrush.lbStyle = elp->elpBrushStyle;
2152 logbrush.lbColor = elp->elpColor;
2153 logbrush.lbHatch = elp->elpHatch;
2155 else
2157 logbrush.lbStyle = BS_SOLID;
2158 logbrush.lbColor = logpen.lopnColor;
2159 logbrush.lbHatch = 0;
2162 pdev->pen_join = logpen.lopnStyle & PS_JOIN_MASK;
2163 pdev->pen_endcap = logpen.lopnStyle & PS_ENDCAP_MASK;
2164 pdev->pen_width = get_pen_device_width( dc, logpen.lopnWidth.x );
2166 if (hpen == GetStockObject( DC_PEN ))
2167 logbrush.lbColor = dc->dcPenColor;
2169 set_dash_pattern( &pdev->pen_pattern, 0, NULL );
2170 select_brush( pdev, &pdev->pen_brush, &logbrush, pattern );
2172 pdev->pen_style = logpen.lopnStyle & PS_STYLE_MASK;
2174 switch (pdev->pen_style)
2176 case PS_DASH:
2177 case PS_DOT:
2178 case PS_DASHDOT:
2179 case PS_DASHDOTDOT:
2180 if (logpen.lopnStyle & PS_GEOMETRIC)
2182 pdev->pen_pattern = dash_patterns_geometric[pdev->pen_style - 1];
2183 if (pdev->pen_width > 1)
2185 scale_dash_pattern( &pdev->pen_pattern, pdev->pen_width, pdev->pen_endcap );
2186 pdev->pen_lines = dashed_wide_pen_lines;
2188 else pdev->pen_lines = dashed_pen_lines;
2189 break;
2191 if (pdev->pen_width == 1) /* wide cosmetic pens are not dashed */
2193 pdev->pen_lines = dashed_pen_lines;
2194 pdev->pen_pattern = dash_patterns_cosmetic[pdev->pen_style - 1];
2195 break;
2197 /* fall through */
2198 case PS_SOLID:
2199 case PS_INSIDEFRAME:
2200 pdev->pen_lines = (pdev->pen_width == 1) ? solid_pen_lines : wide_pen_lines;
2201 break;
2203 case PS_NULL:
2204 pdev->pen_width = 0;
2205 pdev->pen_lines = null_pen_lines;
2206 break;
2208 case PS_ALTERNATE:
2209 pdev->pen_lines = dashed_pen_lines;
2210 pdev->pen_pattern = dash_patterns_geometric[PS_DOT - 1];
2211 break;
2213 case PS_USERSTYLE:
2214 pdev->pen_lines = (pdev->pen_width == 1) ? dashed_pen_lines : dashed_wide_pen_lines;
2215 set_dash_pattern( &pdev->pen_pattern, elp->elpNumEntries, elp->elpStyleEntry );
2216 if (!(logpen.lopnStyle & PS_GEOMETRIC)) scale_dash_pattern( &pdev->pen_pattern, 3, PS_ENDCAP_FLAT );
2217 break;
2220 pdev->pen_uses_region = (logpen.lopnStyle & PS_GEOMETRIC || pdev->pen_width > 1);
2221 pdev->pen_is_ext = (elp != NULL);
2222 HeapFree( GetProcessHeap(), 0, elp );
2223 return hpen;
2226 /***********************************************************************
2227 * dibdrv_SetDCBrushColor
2229 COLORREF dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
2231 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2232 DC *dc = get_physdev_dc( dev );
2234 if (dc->hBrush == GetStockObject( DC_BRUSH ))
2236 LOGBRUSH logbrush = { BS_SOLID, color, 0 };
2237 select_brush( pdev, &pdev->brush, &logbrush, NULL );
2239 return color;