user32: Don't crash in DrawTextEx when tab length is zero.
[wine.git] / dlls / gdi32 / dibdrv / objects.c
blobf954def5d3a9942ca95a5830e1f68a5e52586b59
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( HDC hdc, const dib_info *dib, COLORREF color, BOOL *got_pixel, DWORD *pixel )
140 *pixel = 0;
141 *got_pixel = FALSE;
143 if (color & (1 << 24)) /* PALETTEINDEX */
145 HPALETTE pal = GetCurrentObject( hdc, OBJ_PAL );
146 PALETTEENTRY pal_ent;
148 if (!GetPaletteEntries( pal, LOWORD(color), 1, &pal_ent ))
149 GetPaletteEntries( pal, 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( HDC hdc, 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( hdc, 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( hdc, dib, GetBkColor(hdc), FALSE );
197 if (color == GetBkColor(hdc)) 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( HDC hdc, 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( hdc, 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( hdc, dib, GetBkColor(hdc), FALSE );
223 else if (colorref != GetBkColor(hdc)) 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 int i;
807 assert( num >= 2 );
809 if (region)
811 for (i = 0; i < num - 1; i++)
812 if (!solid_pen_line_region( pdev, pts + i, pts + i + 1, region ))
813 return FALSE;
814 if (close) return solid_pen_line_region( pdev, pts + num - 1, pts, region );
816 else
818 DWORD color, and, xor;
820 color = get_pixel_color( pdev->dev.hdc, &pdev->dib, pdev->pen_brush.colorref, TRUE );
821 calc_and_xor_masks( GetROP2(pdev->dev.hdc), color, &and, &xor );
823 for (i = 0; i < num - 1; i++)
824 if (!solid_pen_line( pdev, pts + i, pts + i + 1, and, xor ))
825 return FALSE;
826 if (close) return solid_pen_line( pdev, pts + num - 1, pts, and, xor );
828 return TRUE;
831 void reset_dash_origin(dibdrv_physdev *pdev)
833 pdev->dash_pos.cur_dash = 0;
834 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
835 pdev->dash_pos.mark = TRUE;
838 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
840 skip %= pdev->pen_pattern.total_len;
843 if(pdev->dash_pos.left_in_dash > skip)
845 pdev->dash_pos.left_in_dash -= skip;
846 return;
848 skip -= pdev->dash_pos.left_in_dash;
849 pdev->dash_pos.cur_dash++;
850 if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
851 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
852 pdev->dash_pos.mark = !pdev->dash_pos.mark;
854 while (skip);
857 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
859 RECT rect;
860 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
862 skip_dash(pdev, 1);
863 rect.left = x;
864 rect.right = x + 1;
865 rect.top = y;
866 rect.bottom = y + 1;
867 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
868 return;
871 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
873 struct clipped_rects clipped_rects;
874 int i, dash_len;
875 RECT rect;
876 const dash_pos start_pos = pdev->dash_pos;
878 if(start->y == end->y) /* hline */
880 BOOL l_to_r;
881 INT left, right, cur_x;
883 rect.top = start->y;
884 rect.bottom = start->y + 1;
886 if(start->x <= end->x)
888 left = start->x;
889 right = end->x - 1;
890 l_to_r = TRUE;
892 else
894 left = end->x + 1;
895 right = start->x;
896 l_to_r = FALSE;
899 rect.left = min( start->x, end->x );
900 rect.right = max( start->x, end->x ) + 1;
901 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
902 for (i = 0; i < clipped_rects.count; i++)
904 if(clipped_rects.rects[i].right > left && clipped_rects.rects[i].left <= right)
906 int clipped_left = max(clipped_rects.rects[i].left, left);
907 int clipped_right = min(clipped_rects.rects[i].right - 1, right);
909 pdev->dash_pos = start_pos;
911 if(l_to_r)
913 cur_x = clipped_left;
914 if(cur_x != left)
915 skip_dash(pdev, clipped_left - left);
917 while(cur_x <= clipped_right)
919 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
920 dash_len = pdev->dash_pos.left_in_dash;
921 if(cur_x + dash_len > clipped_right + 1)
922 dash_len = clipped_right - cur_x + 1;
923 rect.left = cur_x;
924 rect.right = cur_x + dash_len;
926 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
927 cur_x += dash_len;
928 skip_dash(pdev, dash_len);
931 else
933 cur_x = clipped_right;
934 if(cur_x != right)
935 skip_dash(pdev, right - clipped_right);
937 while(cur_x >= clipped_left)
939 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
940 dash_len = pdev->dash_pos.left_in_dash;
941 if(cur_x - dash_len < clipped_left - 1)
942 dash_len = cur_x - clipped_left + 1;
943 rect.left = cur_x - dash_len + 1;
944 rect.right = cur_x + 1;
946 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
947 cur_x -= dash_len;
948 skip_dash(pdev, dash_len);
953 pdev->dash_pos = start_pos;
954 skip_dash(pdev, right - left + 1);
956 else if(start->x == end->x) /* vline */
958 BOOL t_to_b;
959 INT top, bottom, cur_y;
961 rect.left = start->x;
962 rect.right = start->x + 1;
964 if(start->y <= end->y)
966 top = start->y;
967 bottom = end->y - 1;
968 t_to_b = TRUE;
970 else
972 top = end->y + 1;
973 bottom = start->y;
974 t_to_b = FALSE;
977 rect.top = min( start->y, end->y );
978 rect.bottom = max( start->y, end->y ) + 1;
979 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
980 for (i = 0; i < clipped_rects.count; i++)
982 if(clipped_rects.rects[i].right > start->x && clipped_rects.rects[i].left <= start->x)
984 int clipped_top = max(clipped_rects.rects[i].top, top);
985 int clipped_bottom = min(clipped_rects.rects[i].bottom - 1, bottom);
987 pdev->dash_pos = start_pos;
989 if(t_to_b)
991 cur_y = clipped_top;
992 if(cur_y != top)
993 skip_dash(pdev, clipped_top - top);
995 while(cur_y <= clipped_bottom)
997 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
998 dash_len = pdev->dash_pos.left_in_dash;
999 if(cur_y + dash_len > clipped_bottom + 1)
1000 dash_len = clipped_bottom - cur_y + 1;
1001 rect.top = cur_y;
1002 rect.bottom = cur_y + dash_len;
1004 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
1005 cur_y += dash_len;
1006 skip_dash(pdev, dash_len);
1009 else
1011 cur_y = clipped_bottom;
1012 if(cur_y != bottom)
1013 skip_dash(pdev, bottom - clipped_bottom);
1015 while(cur_y >= clipped_top)
1017 rop_mask mask = pdev->dash_masks[pdev->dash_pos.mark];
1018 dash_len = pdev->dash_pos.left_in_dash;
1019 if(cur_y - dash_len < clipped_top - 1)
1020 dash_len = cur_y - clipped_top + 1;
1021 rect.top = cur_y - dash_len + 1;
1022 rect.bottom = cur_y + 1;
1024 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, mask.and, mask.xor);
1025 cur_y -= dash_len;
1026 skip_dash(pdev, dash_len);
1031 pdev->dash_pos = start_pos;
1032 skip_dash(pdev, bottom - top + 1);
1034 else
1036 bres_params clip_params;
1037 struct line_params line_params;
1038 POINT p1 = crop_coords( *start ), p2 = crop_coords( *end );
1040 init_bres_params( &p1, &p2, &clip_params, &line_params, &rect );
1041 get_clipped_rects( &pdev->dib, &rect, pdev->clip, &clipped_rects );
1042 for (i = 0; i < clipped_rects.count; i++)
1044 POINT clipped_start, clipped_end;
1045 int clip_status;
1046 clip_status = clip_line(&p1, &p2, clipped_rects.rects + i, &clip_params, &clipped_start, &clipped_end);
1048 if(clip_status)
1050 int m = abs(clipped_start.x - p1.x);
1051 int n = abs(clipped_start.y - p1.y);
1053 pdev->dash_pos = start_pos;
1055 if (line_params.x_major)
1057 line_params.err_start = 2 * clip_params.dy - clip_params.dx
1058 + m * 2 * clip_params.dy - n * 2 * clip_params.dx;
1059 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
1060 skip_dash(pdev, m);
1062 else
1064 line_params.err_start = 2 * clip_params.dx - clip_params.dy
1065 + n * 2 * clip_params.dx - m * 2 * clip_params.dy;
1066 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
1067 skip_dash(pdev, n);
1069 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
1071 bres_line_with_bias( &clipped_start, &line_params, dashed_pen_line_callback, pdev );
1073 if(clip_status == 2) break; /* completely unclipped, so we can finish */
1076 pdev->dash_pos = start_pos;
1077 if(line_params.x_major)
1078 skip_dash(pdev, clip_params.dx);
1079 else
1080 skip_dash(pdev, clip_params.dy);
1083 free_clipped_rects( &clipped_rects );
1084 return TRUE;
1087 static BOOL dashed_pen_line_region(dibdrv_physdev *pdev, POINT *start, POINT *end, HRGN region)
1089 int i, dash_len;
1090 RECT rect;
1092 rect.left = start->x;
1093 rect.top = start->y;
1094 rect.right = start->x + 1;
1095 rect.bottom = start->y + 1;
1097 if (start->y == end->y) /* hline */
1099 if (start->x <= end->x)
1101 for (i = start->x; i < end->x; i += dash_len)
1103 dash_len = min( pdev->dash_pos.left_in_dash, end->x - i );
1104 if (pdev->dash_pos.mark)
1106 rect.left = i;
1107 rect.right = i + dash_len;
1108 add_rect_to_region( region, &rect );
1110 skip_dash(pdev, dash_len);
1113 else
1115 for (i = start->x; i > end->x; i -= dash_len)
1117 dash_len = min( pdev->dash_pos.left_in_dash, i - end->x );
1118 if (pdev->dash_pos.mark)
1120 rect.left = i - dash_len + 1;
1121 rect.right = i + 1;
1122 add_rect_to_region( region, &rect );
1124 skip_dash(pdev, dash_len);
1128 else if (start->x == end->x) /* vline */
1130 if (start->y <= end->y)
1132 for (i = start->y; i < end->y; i += dash_len)
1134 dash_len = min( pdev->dash_pos.left_in_dash, end->y - i );
1135 if (pdev->dash_pos.mark)
1137 rect.top = i;
1138 rect.bottom = i + dash_len;
1139 add_rect_to_region( region, &rect );
1141 skip_dash(pdev, dash_len);
1144 else
1146 for (i = start->y; i > end->y; i -= dash_len)
1148 dash_len = min( pdev->dash_pos.left_in_dash, i - end->y );
1149 if (pdev->dash_pos.mark)
1151 rect.top = i - dash_len + 1;
1152 rect.bottom = i + 1;
1153 add_rect_to_region( region, &rect );
1155 skip_dash(pdev, dash_len);
1159 else
1161 INT dx = end->x - start->x, dy = end->y - start->y;
1162 INT abs_dx = abs(dx), abs_dy = abs(dy);
1163 DWORD octant = get_octant_mask(dx, dy);
1164 INT bias = get_bias( octant );
1165 int x_inc = is_x_increasing( octant ) ? 1 : -1;
1166 int y_inc = is_y_increasing( octant ) ? 1 : -1;
1168 if (is_xmajor( octant ))
1170 int err_add_1 = 2 * abs_dy - 2 * abs_dx;
1171 int err_add_2 = 2 * abs_dy;
1172 int err = 2 * abs_dy - abs_dx;
1174 while (abs_dx--)
1176 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1177 skip_dash(pdev, 1);
1178 rect.left += x_inc;
1179 rect.right += x_inc;
1180 if (err + bias > 0)
1182 rect.top += y_inc;
1183 rect.bottom += y_inc;
1184 err += err_add_1;
1186 else err += err_add_2;
1190 else
1192 int err_add_1 = 2 * abs_dx - 2 * abs_dy;
1193 int err_add_2 = 2 * abs_dx;
1194 int err = 2 * abs_dx - abs_dy;
1196 while (abs_dy--)
1198 if (pdev->dash_pos.mark) add_rect_to_region( region, &rect );
1199 skip_dash(pdev, 1);
1200 rect.top += y_inc;
1201 rect.bottom += y_inc;
1202 if (err + bias > 0)
1204 rect.left += x_inc;
1205 rect.right += x_inc;
1206 err += err_add_1;
1208 else err += err_add_2;
1212 return TRUE;
1215 static BOOL dashed_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1217 int i;
1219 assert( num >= 2 );
1221 if (region)
1223 for (i = 0; i < num - 1; i++)
1224 if (!dashed_pen_line_region( pdev, pts + i, pts + i + 1, region ))
1225 return FALSE;
1226 if (close) return dashed_pen_line_region( pdev, pts + num - 1, pts, region );
1228 else
1230 get_color_masks( pdev->dev.hdc, &pdev->dib, GetROP2(pdev->dev.hdc), pdev->pen_brush.colorref,
1231 pdev->pen_is_ext ? TRANSPARENT : GetBkMode(pdev->dev.hdc),
1232 &pdev->dash_masks[1], &pdev->dash_masks[0] );
1234 for (i = 0; i < num - 1; i++)
1235 if (!dashed_pen_line( pdev, pts + i, pts + i + 1 ))
1236 return FALSE;
1237 if (close) return dashed_pen_line( pdev, pts + num - 1, pts );
1239 return TRUE;
1242 static BOOL null_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN region)
1244 return TRUE;
1247 struct face
1249 POINT start, end;
1250 int dx, dy;
1253 static void add_cap( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt )
1255 switch (pdev->pen_endcap)
1257 default: FIXME( "Unknown end cap %x\n", pdev->pen_endcap );
1258 /* fall through */
1259 case PS_ENDCAP_ROUND:
1260 OffsetRgn( round_cap, pt->x, pt->y );
1261 CombineRgn( region, region, round_cap, RGN_OR );
1262 OffsetRgn( round_cap, -pt->x, -pt->y );
1263 return;
1265 case PS_ENDCAP_SQUARE: /* already been handled */
1266 case PS_ENDCAP_FLAT:
1267 return;
1271 #define round( f ) (((f) > 0) ? (f) + 0.5 : (f) - 0.5)
1273 /*******************************************************************************
1274 * create_miter_region
1276 * We need to calculate the intersection of two lines. We know a point
1277 * on each line (a face start and the other face end point) and
1278 * the direction vector of each line eg. (dx_1, dy_1).
1280 * (x, y) = (x_1, y_1) + u * (dx_1, dy_1) = (x_2, y_2) + v * (dx_2, dy_2)
1281 * solving (eg using Cramer's rule) gives:
1282 * u = ((x_2 - x_1) dy_2 - (y_2 - y_1) dx_2) / det
1283 * with det = dx_1 dy_2 - dx_2 dy_1
1284 * substituting back in and simplifying gives
1285 * (x, y) = a (dx_1, dy_1) - b (dx_2, dy_2)
1286 * with a = (x_2 dy_2 - y_2 dx_2) / det
1287 * and b = (x_1 dy_1 - y_1 dx_1) / det
1289 static HRGN create_miter_region( dibdrv_physdev *pdev, const POINT *pt,
1290 const struct face *face_1, const struct face *face_2 )
1292 int det = face_1->dx * face_2->dy - face_1->dy * face_2->dx;
1293 POINT pt_1, pt_2, pts[5];
1294 double a, b, x, y;
1295 FLOAT limit;
1297 if (det == 0) return 0;
1299 if (det < 0)
1301 const struct face *tmp = face_1;
1302 face_1 = face_2;
1303 face_2 = tmp;
1304 det = -det;
1307 pt_1 = face_1->start;
1308 pt_2 = face_2->end;
1310 a = (double)(pt_2.x * face_2->dy - pt_2.y * face_2->dx) / det;
1311 b = (double)(pt_1.x * face_1->dy - pt_1.y * face_1->dx) / det;
1313 x = a * face_1->dx - b * face_2->dx;
1314 y = a * face_1->dy - b * face_2->dy;
1316 GetMiterLimit( pdev->dev.hdc, &limit );
1318 if (((x - pt->x) * (x - pt->x) + (y - pt->y) * (y - pt->y)) * 4 > limit * limit * pdev->pen_width * pdev->pen_width)
1319 return 0;
1321 pts[0] = face_2->start;
1322 pts[1] = face_1->start;
1323 pts[2].x = round( x );
1324 pts[2].y = round( y );
1325 pts[3] = face_2->end;
1326 pts[4] = face_1->end;
1328 return CreatePolygonRgn( pts, 5, ALTERNATE );
1331 static void add_join( dibdrv_physdev *pdev, HRGN region, HRGN round_cap, const POINT *pt,
1332 const struct face *face_1, const struct face *face_2 )
1334 HRGN join;
1335 POINT pts[4];
1337 switch (pdev->pen_join)
1339 default: FIXME( "Unknown line join %x\n", pdev->pen_join );
1340 /* fall through */
1341 case PS_JOIN_ROUND:
1342 OffsetRgn( round_cap, pt->x, pt->y );
1343 CombineRgn( region, region, round_cap, RGN_OR );
1344 OffsetRgn( round_cap, -pt->x, -pt->y );
1345 return;
1347 case PS_JOIN_MITER:
1348 join = create_miter_region( pdev, pt, face_1, face_2 );
1349 if (join) break;
1350 /* fall through */
1351 case PS_JOIN_BEVEL:
1352 pts[0] = face_1->start;
1353 pts[1] = face_2->end;
1354 pts[2] = face_1->end;
1355 pts[3] = face_2->start;
1356 join = CreatePolygonRgn( pts, 4, ALTERNATE );
1357 break;
1360 CombineRgn( region, region, join, RGN_OR );
1361 DeleteObject( join );
1362 return;
1365 static BOOL wide_line_segment( dibdrv_physdev *pdev, HRGN total,
1366 const POINT *pt_1, const POINT *pt_2, int dx, int dy,
1367 BOOL need_cap_1, BOOL need_cap_2, struct face *face_1, struct face *face_2 )
1369 RECT rect;
1370 BOOL sq_cap_1 = need_cap_1 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1371 BOOL sq_cap_2 = need_cap_2 && (pdev->pen_endcap == PS_ENDCAP_SQUARE);
1373 if (dx == 0 && dy == 0) return FALSE;
1375 if (dy == 0)
1377 rect.left = min( pt_1->x, pt_2->x );
1378 rect.right = max( pt_1->x, pt_2->x );
1379 rect.top = pt_1->y - pdev->pen_width / 2;
1380 rect.bottom = rect.top + pdev->pen_width;
1381 if ((sq_cap_1 && dx > 0) || (sq_cap_2 && dx < 0)) rect.left -= pdev->pen_width / 2;
1382 if ((sq_cap_2 && dx > 0) || (sq_cap_1 && dx < 0)) rect.right += pdev->pen_width / 2;
1383 add_rect_to_region( total, &rect );
1384 if (dx > 0)
1386 face_1->start.x = face_1->end.x = rect.left;
1387 face_1->start.y = face_2->end.y = rect.bottom;
1388 face_1->end.y = face_2->start.y = rect.top;
1389 face_2->start.x = face_2->end.x = rect.right - 1;
1391 else
1393 face_1->start.x = face_1->end.x = rect.right;
1394 face_1->start.y = face_2->end.y = rect.top;
1395 face_1->end.y = face_2->start.y = rect.bottom;
1396 face_2->start.x = face_2->end.x = rect.left + 1;
1399 else if (dx == 0)
1401 rect.top = min( pt_1->y, pt_2->y );
1402 rect.bottom = max( pt_1->y, pt_2->y );
1403 rect.left = pt_1->x - pdev->pen_width / 2;
1404 rect.right = rect.left + pdev->pen_width;
1405 if ((sq_cap_1 && dy > 0) || (sq_cap_2 && dy < 0)) rect.top -= pdev->pen_width / 2;
1406 if ((sq_cap_2 && dy > 0) || (sq_cap_1 && dy < 0)) rect.bottom += pdev->pen_width / 2;
1407 add_rect_to_region( total, &rect );
1408 if (dy > 0)
1410 face_1->start.x = face_2->end.x = rect.left;
1411 face_1->start.y = face_1->end.y = rect.top;
1412 face_1->end.x = face_2->start.x = rect.right;
1413 face_2->start.y = face_2->end.y = rect.bottom - 1;
1415 else
1417 face_1->start.x = face_2->end.x = rect.right;
1418 face_1->start.y = face_1->end.y = rect.bottom;
1419 face_1->end.x = face_2->start.x = rect.left;
1420 face_2->start.y = face_2->end.y = rect.top + 1;
1423 else
1425 double len = hypot( dx, dy );
1426 double width_x, width_y;
1427 POINT seg_pts[4];
1428 POINT wide_half, narrow_half;
1429 HRGN segment;
1431 width_x = pdev->pen_width * abs( dy ) / len;
1432 width_y = pdev->pen_width * abs( dx ) / len;
1434 narrow_half.x = round( width_x / 2 );
1435 narrow_half.y = round( width_y / 2 );
1436 wide_half.x = round( (width_x + 1) / 2 );
1437 wide_half.y = round( (width_y + 1) / 2 );
1439 if (dx < 0)
1441 wide_half.y = -wide_half.y;
1442 narrow_half.y = -narrow_half.y;
1445 if (dy < 0)
1447 POINT tmp = narrow_half; narrow_half = wide_half; wide_half = tmp;
1448 wide_half.x = -wide_half.x;
1449 narrow_half.x = -narrow_half.x;
1452 seg_pts[0].x = pt_1->x - narrow_half.x;
1453 seg_pts[0].y = pt_1->y + narrow_half.y;
1454 seg_pts[1].x = pt_1->x + wide_half.x;
1455 seg_pts[1].y = pt_1->y - wide_half.y;
1456 seg_pts[2].x = pt_2->x + wide_half.x;
1457 seg_pts[2].y = pt_2->y - wide_half.y;
1458 seg_pts[3].x = pt_2->x - narrow_half.x;
1459 seg_pts[3].y = pt_2->y + narrow_half.y;
1461 if (sq_cap_1)
1463 seg_pts[0].x -= narrow_half.y;
1464 seg_pts[1].x -= narrow_half.y;
1465 seg_pts[0].y -= narrow_half.x;
1466 seg_pts[1].y -= narrow_half.x;
1469 if (sq_cap_2)
1471 seg_pts[2].x += wide_half.y;
1472 seg_pts[3].x += wide_half.y;
1473 seg_pts[2].y += wide_half.x;
1474 seg_pts[3].y += wide_half.x;
1477 segment = CreatePolygonRgn( seg_pts, 4, ALTERNATE );
1478 CombineRgn( total, total, segment, RGN_OR );
1479 DeleteObject( segment );
1481 face_1->start = seg_pts[0];
1482 face_1->end = seg_pts[1];
1483 face_2->start = seg_pts[2];
1484 face_2->end = seg_pts[3];
1487 face_1->dx = face_2->dx = dx;
1488 face_1->dy = face_2->dy = dy;
1490 return TRUE;
1493 static void wide_line_segments( dibdrv_physdev *pdev, int num, const POINT *pts, BOOL close,
1494 int start, int count, const POINT *first_pt, const POINT *last_pt,
1495 HRGN round_cap, HRGN total )
1497 int i;
1498 struct face face_1, face_2, prev_face, first_face;
1499 const POINT *pt_1, *pt_2;
1501 if (!close)
1503 add_cap( pdev, total, round_cap, first_pt );
1504 add_cap( pdev, total, round_cap, last_pt );
1507 if (count == 1)
1509 pt_1 = &pts[start];
1510 pt_2 = &pts[(start + 1) % num];
1511 wide_line_segment( pdev, total, first_pt, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1512 TRUE, TRUE, &face_1, &face_2 );
1513 return;
1516 pt_1 = &pts[start];
1517 pt_2 = &pts[(start + 1) % num];
1518 wide_line_segment( pdev, total, first_pt, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1519 !close, FALSE, &first_face, &prev_face );
1522 for (i = 1; i < count - 1; i++)
1524 pt_1 = &pts[(start + i) % num];
1525 pt_2 = &pts[(start + i + 1) % num];
1526 if (wide_line_segment( pdev, total, pt_1, pt_2, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1527 FALSE, FALSE, &face_1, &face_2 ))
1529 add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1530 prev_face = face_2;
1534 pt_1 = &pts[(start + count - 1) % num];
1535 pt_2 = &pts[(start + count) % num];
1536 wide_line_segment( pdev, total, pt_1, last_pt, pt_2->x - pt_1->x, pt_2->y - pt_1->y,
1537 FALSE, !close, &face_1, &face_2 );
1538 add_join( pdev, total, round_cap, pt_1, &prev_face, &face_1 );
1539 if (close) add_join( pdev, total, round_cap, last_pt, &face_2, &first_face );
1542 static BOOL wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1544 HRGN round_cap = 0;
1546 assert( total != 0 ); /* wide pens should always be drawn through a region */
1547 assert( num >= 2 );
1549 /* skip empty segments */
1550 while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1551 while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1553 if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1554 round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1555 (pdev->pen_width + 1) / 2 + 1, (pdev->pen_width + 1) / 2 + 1 );
1557 if (close)
1558 wide_line_segments( pdev, num, pts, TRUE, 0, num, &pts[0], &pts[0], round_cap, total );
1559 else
1560 wide_line_segments( pdev, num, pts, FALSE, 0, num - 1, &pts[0], &pts[num - 1], round_cap, total );
1562 if (round_cap) DeleteObject( round_cap );
1563 return TRUE;
1566 static BOOL dashed_wide_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts, BOOL close, HRGN total)
1568 int i, start, cur_len, initial_num = 0;
1569 POINT initial_point, start_point, end_point;
1570 HRGN round_cap = 0;
1572 assert( total != 0 ); /* wide pens should always be drawn through a region */
1573 assert( num >= 2 );
1575 /* skip empty segments */
1576 while (num > 2 && pts[0].x == pts[1].x && pts[0].y == pts[1].y) { pts++; num--; }
1577 while (num > 2 && pts[num - 1].x == pts[num - 2].x && pts[num - 1].y == pts[num - 2].y) num--;
1579 if (pdev->pen_join == PS_JOIN_ROUND || pdev->pen_endcap == PS_ENDCAP_ROUND)
1580 round_cap = CreateEllipticRgn( -(pdev->pen_width / 2), -(pdev->pen_width / 2),
1581 (pdev->pen_width + 1) / 2 + 1, (pdev->pen_width + 1) / 2 + 1);
1583 start = 0;
1584 cur_len = 0;
1585 start_point = pts[0];
1587 for (i = 0; i < (close ? num : num - 1); i++)
1589 const POINT *pt_1 = pts + i;
1590 const POINT *pt_2 = pts + ((close && i == num - 1) ? 0 : i + 1);
1591 int dx = pt_2->x - pt_1->x;
1592 int dy = pt_2->y - pt_1->y;
1594 if (dx == 0 && dy == 0) continue;
1596 if (dy == 0)
1598 if (abs( dx ) - cur_len < pdev->dash_pos.left_in_dash)
1600 skip_dash( pdev, abs( dx ) - cur_len );
1601 cur_len = 0;
1602 continue;
1604 cur_len += pdev->dash_pos.left_in_dash;
1605 dx = (dx > 0) ? cur_len : -cur_len;
1607 else if (dx == 0)
1609 if (abs( dy ) - cur_len < pdev->dash_pos.left_in_dash)
1611 skip_dash( pdev, abs( dy ) - cur_len );
1612 cur_len = 0;
1613 continue;
1615 cur_len += pdev->dash_pos.left_in_dash;
1616 dy = (dy > 0) ? cur_len : -cur_len;
1618 else
1620 double len = hypot( dx, dy );
1622 if (len - cur_len < pdev->dash_pos.left_in_dash)
1624 skip_dash( pdev, len - cur_len );
1625 cur_len = 0;
1626 continue;
1628 cur_len += pdev->dash_pos.left_in_dash;
1629 dx = dx * cur_len / len;
1630 dy = dy * cur_len / len;
1632 end_point.x = pt_1->x + dx;
1633 end_point.y = pt_1->y + dy;
1635 if (pdev->dash_pos.mark)
1637 if (!initial_num && close) /* this is the first dash, save it for later */
1639 initial_num = i - start + 1;
1640 initial_point = end_point;
1642 else wide_line_segments( pdev, num, pts, FALSE, start, i - start + 1,
1643 &start_point, &end_point, round_cap, total );
1645 if (!initial_num) initial_num = -1; /* no need to close it */
1647 skip_dash( pdev, pdev->dash_pos.left_in_dash );
1648 start_point = end_point;
1649 start = i;
1650 i--; /* go on with the same segment */
1653 if (pdev->dash_pos.mark) /* we have a final dash */
1655 int count;
1657 if (initial_num > 0)
1659 count = num - start + initial_num;
1660 end_point = initial_point;
1662 else if (close)
1664 count = num - start;
1665 end_point = pts[0];
1667 else
1669 count = num - start - 1;
1670 end_point = pts[num - 1];
1672 wide_line_segments( pdev, num, pts, FALSE, start, count,
1673 &start_point, &end_point, round_cap, total );
1675 else if (initial_num > 0) /* initial dash only */
1677 wide_line_segments( pdev, num, pts, FALSE, 0, initial_num,
1678 &pts[0], &initial_point, round_cap, total );
1681 if (round_cap) DeleteObject( round_cap );
1682 return TRUE;
1685 static const dash_pattern dash_patterns_cosmetic[4] =
1687 {2, {18, 6}, 24}, /* PS_DASH */
1688 {2, {3, 3}, 6}, /* PS_DOT */
1689 {4, {9, 6, 3, 6}, 24}, /* PS_DASHDOT */
1690 {6, {9, 3, 3, 3, 3, 3}, 24} /* PS_DASHDOTDOT */
1693 static const dash_pattern dash_patterns_geometric[4] =
1695 {2, {3, 1}, 4}, /* PS_DASH */
1696 {2, {1, 1}, 2}, /* PS_DOT */
1697 {4, {3, 1, 1, 1}, 6}, /* PS_DASHDOT */
1698 {6, {3, 1, 1, 1, 1, 1}, 8} /* PS_DASHDOTDOT */
1701 static inline void set_dash_pattern( dash_pattern *pattern, DWORD count, DWORD *dashes )
1703 DWORD i;
1705 pattern->count = count;
1706 pattern->total_len = 0;
1707 memcpy( pattern->dashes, dashes, count * sizeof(DWORD) );
1708 for (i = 0; i < count; i++) pattern->total_len += dashes[i];
1709 if (pattern->count % 2) pattern->total_len *= 2;
1712 static inline void scale_dash_pattern( dash_pattern *pattern, DWORD scale, DWORD endcap )
1714 DWORD i;
1716 for (i = 0; i < pattern->count; i++) pattern->dashes[i] *= scale;
1717 pattern->total_len *= scale;
1719 if (endcap != PS_ENDCAP_FLAT) /* shrink the dashes to leave room for the caps */
1721 for (i = 0; i < pattern->count; i += 2)
1723 pattern->dashes[i] -= scale;
1724 pattern->dashes[i + 1] += scale;
1729 static inline int get_pen_device_width( dibdrv_physdev *pdev, int width )
1731 POINT pts[2];
1733 if (!width) return 1;
1734 pts[0].x = pts[0].y = pts[1].y = 0;
1735 pts[1].x = width;
1736 LPtoDP( pdev->dev.hdc, pts, 2 );
1737 width = abs( pts[1].x - pts[0].x );
1738 return max( width, 1 );
1741 /***********************************************************************
1742 * dibdrv_SetDCPenColor
1744 COLORREF dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1746 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1748 if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
1749 pdev->pen_brush.colorref = color;
1751 return color;
1754 /**********************************************************************
1755 * solid_brush
1757 * Fill a number of rectangles with the solid brush
1759 static BOOL solid_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1760 int num, const RECT *rects, INT rop)
1762 rop_mask brush_color;
1763 DWORD color = get_pixel_color( pdev->dev.hdc, &pdev->dib, brush->colorref, TRUE );
1765 calc_rop_masks( rop, color, &brush_color );
1766 dib->funcs->solid_rects( dib, num, rects, brush_color.and, brush_color.xor );
1767 return TRUE;
1770 static BOOL alloc_brush_mask_bits( dib_brush *brush )
1772 DWORD size = brush->dib.height * abs(brush->dib.stride);
1774 assert(brush->masks.and == NULL);
1775 assert(brush->masks.xor == NULL);
1776 assert(brush->dib.stride > 0);
1778 if (!(brush->masks.xor = HeapAlloc(GetProcessHeap(), 0, 2 * size))) return FALSE;
1779 brush->masks.and = (char *)brush->masks.xor + size;
1780 return TRUE;
1783 static void free_brush_mask_bits( dib_brush *brush )
1785 if (brush->masks.xor != brush->dib.bits.ptr) HeapFree(GetProcessHeap(), 0, brush->masks.xor);
1786 brush->masks.and = brush->masks.xor = NULL;
1789 void free_pattern_brush( dib_brush *brush )
1791 free_brush_mask_bits( brush );
1792 free_dib_info( &brush->dib );
1795 static BOOL create_pattern_brush_bits( dib_brush *brush )
1797 DWORD size = brush->dib.height * abs(brush->dib.stride);
1798 DWORD *brush_bits = brush->dib.bits.ptr;
1799 DWORD *and_bits, *xor_bits;
1801 if (brush->rop == R2_COPYPEN)
1803 brush->masks.xor = brush_bits; /* use the pattern bits directly */
1804 return TRUE;
1807 if (!alloc_brush_mask_bits( brush )) return FALSE;
1809 and_bits = brush->masks.and;
1810 xor_bits = brush->masks.xor;
1812 while(size)
1814 calc_and_xor_masks(brush->rop, *brush_bits++, and_bits++, xor_bits++);
1815 size -= 4;
1818 if (!rop_needs_and_mask( brush->rop )) brush->masks.and = NULL; /* ignore the and mask */
1820 return TRUE;
1823 static const BYTE hatches[6][8] =
1825 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1826 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
1827 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL */
1828 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL */
1829 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
1830 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
1833 static BOOL init_hatch_brush( dibdrv_physdev *pdev, dib_brush *brush )
1835 /* Just initialise brush dib with the color / sizing info. We don't
1836 need the bits as we'll calculate the rop masks straight from
1837 the hatch patterns. */
1839 copy_dib_color_info(&brush->dib, &pdev->dib);
1840 brush->dib.width = 8;
1841 brush->dib.height = 8;
1842 brush->dib.stride = get_dib_stride( brush->dib.width, brush->dib.bit_count );
1843 brush->dib.rect.left = 0;
1844 brush->dib.rect.top = 0;
1845 brush->dib.rect.right = 8;
1846 brush->dib.rect.bottom = 8;
1847 return alloc_brush_mask_bits( brush );
1850 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect)
1852 rop_mask fg_mask, bg_mask;
1854 if (!init_hatch_brush( pdev, brush )) return FALSE;
1856 get_color_masks( pdev->dev.hdc, &pdev->dib, brush->rop, brush->colorref, GetBkMode(pdev->dev.hdc),
1857 &fg_mask, &bg_mask );
1859 if (brush->colorref & (1 << 24)) /* PALETTEINDEX */
1860 *needs_reselect = TRUE;
1861 if (GetBkMode(pdev->dev.hdc) != TRANSPARENT && (GetBkColor(pdev->dev.hdc) & (1 << 24)))
1862 *needs_reselect = TRUE;
1864 brush->dib.funcs->create_rop_masks( &brush->dib, hatches[brush->hatch],
1865 &fg_mask, &bg_mask, &brush->masks );
1867 if (!fg_mask.and && !bg_mask.and) brush->masks.and = NULL; /* ignore the and mask */
1869 return TRUE;
1872 static BOOL create_dither_brush_bits(dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect)
1874 COLORREF rgb;
1875 DWORD pixel;
1876 BOOL got_pixel;
1878 if (!init_hatch_brush( pdev, brush )) return FALSE;
1880 if (brush->colorref & (1 << 24)) /* PALETTEINDEX */
1881 *needs_reselect = TRUE;
1883 rgb = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, brush->colorref, &got_pixel, &pixel );
1885 brush->dib.funcs->create_dither_masks( &brush->dib, brush->rop, rgb, &brush->masks );
1887 if (!rop_needs_and_mask( brush->rop )) brush->masks.and = NULL; /* ignore the and mask */
1889 return TRUE;
1892 static BOOL matching_pattern_format( dib_info *dib, dib_info *pattern )
1894 if (dib->bit_count != pattern->bit_count) return FALSE;
1895 if (dib->stride != pattern->stride) return FALSE;
1897 switch (dib->bit_count)
1899 case 1:
1900 case 4:
1901 case 8:
1902 if (dib->color_table_size != pattern->color_table_size) return FALSE;
1903 return !memcmp( dib->color_table, pattern->color_table, dib->color_table_size * sizeof(RGBQUAD) );
1904 case 16:
1905 case 32:
1906 return (dib->red_mask == pattern->red_mask &&
1907 dib->green_mask == pattern->green_mask &&
1908 dib->blue_mask == pattern->blue_mask);
1910 return TRUE;
1913 static BOOL select_pattern_brush( dibdrv_physdev *pdev, dib_brush *brush, BOOL *needs_reselect )
1915 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1916 BITMAPINFO *info = (BITMAPINFO *)buffer;
1917 RGBQUAD color_table[2];
1918 dib_info pattern;
1919 BOOL dither = (brush->dib.bit_count == 1);
1921 if (brush->pattern.info->bmiHeader.biClrUsed && brush->pattern.usage == DIB_PAL_COLORS)
1923 copy_bitmapinfo( info, brush->pattern.info );
1924 fill_color_table_from_pal_colors( info, pdev->dev.hdc );
1925 init_dib_info_from_bitmapinfo( &pattern, info, brush->pattern.bits.ptr );
1926 *needs_reselect = TRUE;
1928 else
1930 init_dib_info_from_bitmapinfo( &pattern, brush->pattern.info, brush->pattern.bits.ptr );
1933 if (pattern.bit_count == 1 && !pattern.color_table)
1934 dither = FALSE; /* monochrome DDB pattern brushes don't get dithered */
1936 if (pattern.bit_count == 1 && !pattern.color_table &&
1937 (pdev->dib.bit_count != 1 || pdev->dib.color_table))
1939 /* monochrome DDB pattern uses DC colors */
1940 DWORD pixel;
1941 BOOL got_pixel;
1942 COLORREF color;
1944 color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetTextColor( pdev->dev.hdc ),
1945 &got_pixel, &pixel );
1946 color_table[0].rgbRed = GetRValue( color );
1947 color_table[0].rgbGreen = GetGValue( color );
1948 color_table[0].rgbBlue = GetBValue( color );
1949 color_table[0].rgbReserved = 0;
1951 color = make_rgb_colorref( pdev->dev.hdc, &pdev->dib, GetBkColor( pdev->dev.hdc ),
1952 &got_pixel, &pixel );
1953 color_table[1].rgbRed = GetRValue( color );
1954 color_table[1].rgbGreen = GetGValue( color );
1955 color_table[1].rgbBlue = GetBValue( color );
1956 color_table[1].rgbReserved = 0;
1958 pattern.color_table = color_table;
1959 pattern.color_table_size = 2;
1960 *needs_reselect = TRUE;
1963 copy_dib_color_info(&brush->dib, &pdev->dib);
1965 brush->dib.height = pattern.height;
1966 brush->dib.width = pattern.width;
1967 brush->dib.stride = get_dib_stride( brush->dib.width, brush->dib.bit_count );
1968 brush->dib.rect = pattern.rect;
1970 if (!dither && matching_pattern_format( &brush->dib, &pattern ))
1972 brush->dib.bits.ptr = pattern.bits.ptr;
1973 brush->dib.bits.is_copy = FALSE;
1974 brush->dib.bits.free = NULL;
1976 else
1978 brush->dib.bits.ptr = HeapAlloc( GetProcessHeap(), 0, brush->dib.height * brush->dib.stride );
1979 brush->dib.bits.is_copy = TRUE;
1980 brush->dib.bits.free = free_heap_bits;
1981 brush->dib.funcs->convert_to(&brush->dib, &pattern, &pattern.rect, dither);
1983 return TRUE;
1986 /**********************************************************************
1987 * pattern_brush
1989 * Fill a number of rectangles with the pattern brush
1990 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1992 static BOOL pattern_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
1993 int num, const RECT *rects, INT rop)
1995 POINT origin;
1996 BOOL needs_reselect = FALSE;
1998 if (rop != brush->rop)
2000 free_brush_mask_bits( brush );
2001 brush->rop = rop;
2004 if(brush->masks.xor == NULL)
2006 switch(brush->style)
2008 case BS_DIBPATTERN:
2009 if (!brush->dib.bits.ptr && !select_pattern_brush( pdev, brush, &needs_reselect ))
2010 return FALSE;
2011 if(!create_pattern_brush_bits( brush ))
2012 return FALSE;
2013 break;
2015 case BS_SOLID:
2016 if(!create_dither_brush_bits(pdev, brush, &needs_reselect))
2017 return FALSE;
2018 break;
2020 case BS_HATCHED:
2021 if(!create_hatch_brush_bits(pdev, brush, &needs_reselect))
2022 return FALSE;
2023 break;
2025 default:
2026 ERR("Unexpected brush style %d\n", brush->style);
2027 return FALSE;
2031 GetBrushOrgEx(pdev->dev.hdc, &origin);
2033 dib->funcs->pattern_rects( dib, num, rects, &origin, &brush->dib, &brush->masks );
2035 if (needs_reselect) free_pattern_brush( brush );
2036 return TRUE;
2039 static BOOL null_brush(dibdrv_physdev *pdev, dib_brush *brush, dib_info *dib,
2040 int num, const RECT *rects, INT rop)
2042 return TRUE;
2045 static BOOL brush_needs_dithering( dibdrv_physdev *pdev, COLORREF color )
2047 int i;
2048 RGBQUAD rgb;
2049 const RGBQUAD *color_table = get_default_color_table( pdev->dib.bit_count );
2051 if (!color_table) return FALSE;
2052 if (pdev->dib.color_table) return FALSE;
2053 if (color & (1 << 24)) return TRUE; /* PALETTEINDEX */
2054 if (color >> 16 == 0x10ff) return FALSE; /* DIBINDEX */
2056 rgb = rgbquad_from_colorref( color );
2057 for (i = 0; i < (1 << pdev->dib.bit_count); i++)
2058 if (rgbquad_equal( &color_table[i], &rgb )) return FALSE;
2060 return TRUE;
2063 static void select_brush( dibdrv_physdev *pdev, dib_brush *brush,
2064 const LOGBRUSH *logbrush, const struct brush_pattern *pattern )
2066 free_pattern_brush( brush );
2068 if (pattern)
2070 brush->style = BS_DIBPATTERN;
2071 brush->pattern = *pattern; /* brush is actually selected only when it's used */
2072 brush->rects = pattern_brush;
2074 else
2076 brush->style = logbrush->lbStyle;
2077 brush->colorref = logbrush->lbColor;
2078 brush->hatch = logbrush->lbHatch;
2080 switch (logbrush->lbStyle)
2082 case BS_NULL: brush->rects = null_brush; break;
2083 case BS_HATCHED: brush->rects = pattern_brush; break;
2084 case BS_SOLID:
2085 brush->rects = brush_needs_dithering( pdev, brush->colorref ) ? pattern_brush : solid_brush;
2086 break;
2091 /***********************************************************************
2092 * dibdrv_SelectBrush
2094 HBRUSH dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush, const struct brush_pattern *pattern )
2096 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2097 LOGBRUSH logbrush;
2099 TRACE("(%p, %p)\n", dev, hbrush);
2101 GetObjectW( hbrush, sizeof(logbrush), &logbrush );
2103 if (hbrush == GetStockObject( DC_BRUSH ))
2104 logbrush.lbColor = GetDCBrushColor( dev->hdc );
2106 select_brush( pdev, &pdev->brush, &logbrush, pattern );
2107 return hbrush;
2110 /***********************************************************************
2111 * dibdrv_SelectPen
2113 HPEN dibdrv_SelectPen( PHYSDEV dev, HPEN hpen, const struct brush_pattern *pattern )
2115 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2116 LOGPEN logpen;
2117 LOGBRUSH logbrush;
2118 EXTLOGPEN *elp = NULL;
2120 TRACE("(%p, %p)\n", dev, hpen);
2122 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
2124 /* must be an extended pen */
2125 INT size = GetObjectW( hpen, 0, NULL );
2127 if (!size) return 0;
2129 elp = HeapAlloc( GetProcessHeap(), 0, size );
2131 GetObjectW( hpen, size, elp );
2132 logpen.lopnStyle = elp->elpPenStyle;
2133 logpen.lopnWidth.x = elp->elpWidth;
2134 /* cosmetic ext pens are always 1-pixel wide */
2135 if (!(logpen.lopnStyle & PS_GEOMETRIC)) logpen.lopnWidth.x = 0;
2137 logbrush.lbStyle = elp->elpBrushStyle;
2138 logbrush.lbColor = elp->elpColor;
2139 logbrush.lbHatch = elp->elpHatch;
2141 else
2143 logbrush.lbStyle = BS_SOLID;
2144 logbrush.lbColor = logpen.lopnColor;
2145 logbrush.lbHatch = 0;
2148 pdev->pen_join = logpen.lopnStyle & PS_JOIN_MASK;
2149 pdev->pen_endcap = logpen.lopnStyle & PS_ENDCAP_MASK;
2150 pdev->pen_width = get_pen_device_width( pdev, logpen.lopnWidth.x );
2152 if (hpen == GetStockObject( DC_PEN ))
2153 logbrush.lbColor = GetDCPenColor( dev->hdc );
2155 set_dash_pattern( &pdev->pen_pattern, 0, NULL );
2156 select_brush( pdev, &pdev->pen_brush, &logbrush, pattern );
2158 pdev->pen_style = logpen.lopnStyle & PS_STYLE_MASK;
2160 switch (pdev->pen_style)
2162 case PS_DASH:
2163 case PS_DOT:
2164 case PS_DASHDOT:
2165 case PS_DASHDOTDOT:
2166 if (logpen.lopnStyle & PS_GEOMETRIC)
2168 pdev->pen_pattern = dash_patterns_geometric[pdev->pen_style - 1];
2169 if (pdev->pen_width > 1)
2171 scale_dash_pattern( &pdev->pen_pattern, pdev->pen_width, pdev->pen_endcap );
2172 pdev->pen_lines = dashed_wide_pen_lines;
2174 else pdev->pen_lines = dashed_pen_lines;
2175 break;
2177 if (pdev->pen_width == 1) /* wide cosmetic pens are not dashed */
2179 pdev->pen_lines = dashed_pen_lines;
2180 pdev->pen_pattern = dash_patterns_cosmetic[pdev->pen_style - 1];
2181 break;
2183 /* fall through */
2184 case PS_SOLID:
2185 case PS_INSIDEFRAME:
2186 pdev->pen_lines = (pdev->pen_width == 1) ? solid_pen_lines : wide_pen_lines;
2187 break;
2189 case PS_NULL:
2190 pdev->pen_width = 0;
2191 pdev->pen_lines = null_pen_lines;
2192 break;
2194 case PS_ALTERNATE:
2195 pdev->pen_lines = dashed_pen_lines;
2196 pdev->pen_pattern = dash_patterns_geometric[PS_DOT - 1];
2197 break;
2199 case PS_USERSTYLE:
2200 pdev->pen_lines = (pdev->pen_width == 1) ? dashed_pen_lines : dashed_wide_pen_lines;
2201 set_dash_pattern( &pdev->pen_pattern, elp->elpNumEntries, elp->elpStyleEntry );
2202 if (!(logpen.lopnStyle & PS_GEOMETRIC)) scale_dash_pattern( &pdev->pen_pattern, 3, PS_ENDCAP_FLAT );
2203 break;
2206 pdev->pen_uses_region = (logpen.lopnStyle & PS_GEOMETRIC || pdev->pen_width > 1);
2207 pdev->pen_is_ext = (elp != NULL);
2208 HeapFree( GetProcessHeap(), 0, elp );
2209 return hpen;
2212 /***********************************************************************
2213 * dibdrv_SetDCBrushColor
2215 COLORREF dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
2217 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
2219 if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
2221 LOGBRUSH logbrush = { BS_SOLID, color, 0 };
2222 select_brush( pdev, &pdev->brush, &logbrush, NULL );
2224 return color;