gdiplus: Implement GdipSetPathGradientBlend, with tests.
[wine/multimedia.git] / dlls / gdi32 / mapping.c
blob42206584bb2f97169a2f1b18d981bccd4795027d
1 /*
2 * GDI mapping mode functions
4 * Copyright 1993 Alexandre Julliard
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 <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "gdi_private.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dc);
32 /***********************************************************************
33 * MAPPING_FixIsotropic
35 * Fix viewport extensions for isotropic mode.
37 static void MAPPING_FixIsotropic( DC * dc )
39 double xdim = fabs((double)dc->vportExtX * dc->virtual_size.cx /
40 (dc->virtual_res.cx * dc->wndExtX));
41 double ydim = fabs((double)dc->vportExtY * dc->virtual_size.cy /
42 (dc->virtual_res.cy * dc->wndExtY));
44 if (xdim > ydim)
46 INT mincx = (dc->vportExtX >= 0) ? 1 : -1;
47 dc->vportExtX = floor(dc->vportExtX * ydim / xdim + 0.5);
48 if (!dc->vportExtX) dc->vportExtX = mincx;
50 else
52 INT mincy = (dc->vportExtY >= 0) ? 1 : -1;
53 dc->vportExtY = floor(dc->vportExtY * xdim / ydim + 0.5);
54 if (!dc->vportExtY) dc->vportExtY = mincy;
59 /***********************************************************************
60 * null driver fallback implementations
63 BOOL nulldrv_OffsetViewportOrgEx( PHYSDEV dev, INT x, INT y, POINT *pt )
65 DC *dc = get_nulldrv_dc( dev );
67 if (pt)
69 pt->x = dc->vportOrgX;
70 pt->y = dc->vportOrgY;
72 dc->vportOrgX += x;
73 dc->vportOrgY += y;
74 DC_UpdateXforms( dc );
75 return TRUE;
78 BOOL nulldrv_OffsetWindowOrgEx( PHYSDEV dev, INT x, INT y, POINT *pt )
80 DC *dc = get_nulldrv_dc( dev );
82 if (pt)
84 pt->x = dc->wndOrgX;
85 pt->y = dc->wndOrgY;
87 dc->wndOrgX += x;
88 dc->wndOrgY += y;
89 DC_UpdateXforms( dc );
90 return TRUE;
93 BOOL nulldrv_ScaleViewportExtEx( PHYSDEV dev, INT x_num, INT x_denom, INT y_num, INT y_denom, SIZE *size )
95 DC *dc = get_nulldrv_dc( dev );
97 if (size)
99 size->cx = dc->vportExtX;
100 size->cy = dc->vportExtY;
102 if (dc->MapMode != MM_ISOTROPIC && dc->MapMode != MM_ANISOTROPIC) return TRUE;
103 if (!x_num || !x_denom || !y_num || !y_denom) return FALSE;
105 dc->vportExtX = (dc->vportExtX * x_num) / x_denom;
106 dc->vportExtY = (dc->vportExtY * y_num) / y_denom;
107 if (dc->vportExtX == 0) dc->vportExtX = 1;
108 if (dc->vportExtY == 0) dc->vportExtY = 1;
109 if (dc->MapMode == MM_ISOTROPIC) MAPPING_FixIsotropic( dc );
110 DC_UpdateXforms( dc );
111 return TRUE;
114 BOOL nulldrv_ScaleWindowExtEx( PHYSDEV dev, INT x_num, INT x_denom, INT y_num, INT y_denom, SIZE *size )
116 DC *dc = get_nulldrv_dc( dev );
118 if (size)
120 size->cx = dc->wndExtX;
121 size->cy = dc->wndExtY;
123 if (dc->MapMode != MM_ISOTROPIC && dc->MapMode != MM_ANISOTROPIC) return TRUE;
124 if (!x_num || !x_denom || !y_num || !y_denom) return FALSE;
126 dc->wndExtX = (dc->wndExtX * x_num) / x_denom;
127 dc->wndExtY = (dc->wndExtY * y_num) / y_denom;
128 if (dc->wndExtX == 0) dc->wndExtX = 1;
129 if (dc->wndExtY == 0) dc->wndExtY = 1;
130 if (dc->MapMode == MM_ISOTROPIC) MAPPING_FixIsotropic( dc );
131 DC_UpdateXforms( dc );
132 return TRUE;
135 INT nulldrv_SetMapMode( PHYSDEV dev, INT mode )
137 DC *dc = get_nulldrv_dc( dev );
138 INT ret = dc->MapMode;
139 INT horzSize, vertSize, horzRes, vertRes;
141 if (mode == dc->MapMode && (mode == MM_ISOTROPIC || mode == MM_ANISOTROPIC)) return ret;
143 horzSize = dc->virtual_size.cx;
144 vertSize = dc->virtual_size.cy;
145 horzRes = dc->virtual_res.cx;
146 vertRes = dc->virtual_res.cy;
147 switch (mode)
149 case MM_TEXT:
150 dc->wndExtX = 1;
151 dc->wndExtY = 1;
152 dc->vportExtX = 1;
153 dc->vportExtY = 1;
154 break;
155 case MM_LOMETRIC:
156 case MM_ISOTROPIC:
157 dc->wndExtX = horzSize * 10;
158 dc->wndExtY = vertSize * 10;
159 dc->vportExtX = horzRes;
160 dc->vportExtY = -vertRes;
161 break;
162 case MM_HIMETRIC:
163 dc->wndExtX = horzSize * 100;
164 dc->wndExtY = vertSize * 100;
165 dc->vportExtX = horzRes;
166 dc->vportExtY = -vertRes;
167 break;
168 case MM_LOENGLISH:
169 dc->wndExtX = MulDiv(1000, horzSize, 254);
170 dc->wndExtY = MulDiv(1000, vertSize, 254);
171 dc->vportExtX = horzRes;
172 dc->vportExtY = -vertRes;
173 break;
174 case MM_HIENGLISH:
175 dc->wndExtX = MulDiv(10000, horzSize, 254);
176 dc->wndExtY = MulDiv(10000, vertSize, 254);
177 dc->vportExtX = horzRes;
178 dc->vportExtY = -vertRes;
179 break;
180 case MM_TWIPS:
181 dc->wndExtX = MulDiv(14400, horzSize, 254);
182 dc->wndExtY = MulDiv(14400, vertSize, 254);
183 dc->vportExtX = horzRes;
184 dc->vportExtY = -vertRes;
185 break;
186 case MM_ANISOTROPIC:
187 break;
188 default:
189 return 0;
191 /* RTL layout is always MM_ANISOTROPIC */
192 if (!(dc->layout & LAYOUT_RTL)) dc->MapMode = mode;
193 DC_UpdateXforms( dc );
194 return ret;
197 BOOL nulldrv_SetViewportExtEx( PHYSDEV dev, INT cx, INT cy, SIZE *size )
199 DC *dc = get_nulldrv_dc( dev );
201 if (size)
203 size->cx = dc->vportExtX;
204 size->cy = dc->vportExtY;
206 if (dc->MapMode != MM_ISOTROPIC && dc->MapMode != MM_ANISOTROPIC) return TRUE;
207 if (!cx || !cy) return FALSE;
208 dc->vportExtX = cx;
209 dc->vportExtY = cy;
210 if (dc->MapMode == MM_ISOTROPIC) MAPPING_FixIsotropic( dc );
211 DC_UpdateXforms( dc );
212 return TRUE;
215 BOOL nulldrv_SetViewportOrgEx( PHYSDEV dev, INT x, INT y, POINT *pt )
217 DC *dc = get_nulldrv_dc( dev );
219 if (pt)
221 pt->x = dc->vportOrgX;
222 pt->y = dc->vportOrgY;
224 dc->vportOrgX = x;
225 dc->vportOrgY = y;
226 DC_UpdateXforms( dc );
227 return TRUE;
230 BOOL nulldrv_SetWindowExtEx( PHYSDEV dev, INT cx, INT cy, SIZE *size )
232 DC *dc = get_nulldrv_dc( dev );
234 if (size)
236 size->cx = dc->wndExtX;
237 size->cy = dc->wndExtY;
239 if (dc->MapMode != MM_ISOTROPIC && dc->MapMode != MM_ANISOTROPIC) return TRUE;
240 if (!cx || !cy) return FALSE;
241 dc->wndExtX = cx;
242 dc->wndExtY = cy;
243 /* The API docs say that you should call SetWindowExtEx before
244 SetViewportExtEx. This advice does not imply that Windows
245 doesn't ensure the isotropic mapping after SetWindowExtEx! */
246 if (dc->MapMode == MM_ISOTROPIC) MAPPING_FixIsotropic( dc );
247 DC_UpdateXforms( dc );
248 return TRUE;
251 BOOL nulldrv_SetWindowOrgEx( PHYSDEV dev, INT x, INT y, POINT *pt )
253 DC *dc = get_nulldrv_dc( dev );
255 if (pt)
257 pt->x = dc->wndOrgX;
258 pt->y = dc->wndOrgY;
260 dc->wndOrgX = x;
261 dc->wndOrgY = y;
262 DC_UpdateXforms( dc );
263 return TRUE;
266 BOOL nulldrv_ModifyWorldTransform( PHYSDEV dev, const XFORM *xform, DWORD mode )
268 DC *dc = get_nulldrv_dc( dev );
270 switch (mode)
272 case MWT_IDENTITY:
273 dc->xformWorld2Wnd.eM11 = 1.0f;
274 dc->xformWorld2Wnd.eM12 = 0.0f;
275 dc->xformWorld2Wnd.eM21 = 0.0f;
276 dc->xformWorld2Wnd.eM22 = 1.0f;
277 dc->xformWorld2Wnd.eDx = 0.0f;
278 dc->xformWorld2Wnd.eDy = 0.0f;
279 break;
280 case MWT_LEFTMULTIPLY:
281 CombineTransform( &dc->xformWorld2Wnd, xform, &dc->xformWorld2Wnd );
282 break;
283 case MWT_RIGHTMULTIPLY:
284 CombineTransform( &dc->xformWorld2Wnd, &dc->xformWorld2Wnd, xform );
285 break;
286 default:
287 return FALSE;
289 DC_UpdateXforms( dc );
290 return TRUE;
293 BOOL nulldrv_SetWorldTransform( PHYSDEV dev, const XFORM *xform )
295 DC *dc = get_nulldrv_dc( dev );
297 dc->xformWorld2Wnd = *xform;
298 DC_UpdateXforms( dc );
299 return TRUE;
302 /***********************************************************************
303 * DPtoLP (GDI32.@)
305 BOOL WINAPI DPtoLP( HDC hdc, LPPOINT points, INT count )
307 DC * dc = get_dc_ptr( hdc );
308 if (!dc) return FALSE;
310 if (dc->vport2WorldValid)
312 while (count--)
314 double x = points->x;
315 double y = points->y;
316 points->x = floor( x * dc->xformVport2World.eM11 +
317 y * dc->xformVport2World.eM21 +
318 dc->xformVport2World.eDx + 0.5 );
319 points->y = floor( x * dc->xformVport2World.eM12 +
320 y * dc->xformVport2World.eM22 +
321 dc->xformVport2World.eDy + 0.5 );
322 points++;
325 release_dc_ptr( dc );
326 return (count < 0);
330 /***********************************************************************
331 * LPtoDP (GDI32.@)
333 BOOL WINAPI LPtoDP( HDC hdc, LPPOINT points, INT count )
335 DC * dc = get_dc_ptr( hdc );
336 if (!dc) return FALSE;
338 while (count--)
340 double x = points->x;
341 double y = points->y;
342 points->x = floor( x * dc->xformWorld2Vport.eM11 +
343 y * dc->xformWorld2Vport.eM21 +
344 dc->xformWorld2Vport.eDx + 0.5 );
345 points->y = floor( x * dc->xformWorld2Vport.eM12 +
346 y * dc->xformWorld2Vport.eM22 +
347 dc->xformWorld2Vport.eDy + 0.5 );
348 points++;
350 release_dc_ptr( dc );
351 return TRUE;
355 /***********************************************************************
356 * SetMapMode (GDI32.@)
358 INT WINAPI SetMapMode( HDC hdc, INT mode )
360 INT ret = 0;
361 DC * dc = get_dc_ptr( hdc );
363 TRACE("%p %d\n", hdc, mode );
365 if (dc)
367 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetMapMode );
368 ret = physdev->funcs->pSetMapMode( physdev, mode );
369 release_dc_ptr( dc );
371 return ret;
375 /***********************************************************************
376 * SetViewportExtEx (GDI32.@)
378 BOOL WINAPI SetViewportExtEx( HDC hdc, INT x, INT y, LPSIZE size )
380 INT ret = FALSE;
381 DC * dc = get_dc_ptr( hdc );
383 if (dc)
385 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetViewportExtEx );
386 ret = physdev->funcs->pSetViewportExtEx( physdev, x, y, size );
387 release_dc_ptr( dc );
389 return ret;
393 /***********************************************************************
394 * SetViewportOrgEx (GDI32.@)
396 BOOL WINAPI SetViewportOrgEx( HDC hdc, INT x, INT y, LPPOINT pt )
398 INT ret = FALSE;
399 DC * dc = get_dc_ptr( hdc );
401 if (dc)
403 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetViewportOrgEx );
404 ret = physdev->funcs->pSetViewportOrgEx( physdev, x, y, pt );
405 release_dc_ptr( dc );
407 return ret;
411 /***********************************************************************
412 * SetWindowExtEx (GDI32.@)
414 BOOL WINAPI SetWindowExtEx( HDC hdc, INT x, INT y, LPSIZE size )
416 INT ret = FALSE;
417 DC * dc = get_dc_ptr( hdc );
419 if (dc)
421 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetWindowExtEx );
422 ret = physdev->funcs->pSetWindowExtEx( physdev, x, y, size );
423 release_dc_ptr( dc );
425 return ret;
429 /***********************************************************************
430 * SetWindowOrgEx (GDI32.@)
432 BOOL WINAPI SetWindowOrgEx( HDC hdc, INT x, INT y, LPPOINT pt )
434 INT ret = FALSE;
435 DC * dc = get_dc_ptr( hdc );
437 if (dc)
439 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetWindowOrgEx );
440 ret = physdev->funcs->pSetWindowOrgEx( physdev, x, y, pt );
441 release_dc_ptr( dc );
443 return ret;
447 /***********************************************************************
448 * OffsetViewportOrgEx (GDI32.@)
450 BOOL WINAPI OffsetViewportOrgEx( HDC hdc, INT x, INT y, LPPOINT pt)
452 INT ret = FALSE;
453 DC * dc = get_dc_ptr( hdc );
455 if (dc)
457 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pOffsetViewportOrgEx );
458 ret = physdev->funcs->pOffsetViewportOrgEx( physdev, x, y, pt );
459 release_dc_ptr( dc );
461 return ret;
465 /***********************************************************************
466 * OffsetWindowOrgEx (GDI32.@)
468 BOOL WINAPI OffsetWindowOrgEx( HDC hdc, INT x, INT y, LPPOINT pt )
470 INT ret = FALSE;
471 DC * dc = get_dc_ptr( hdc );
473 if (dc)
475 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pOffsetWindowOrgEx );
476 ret = physdev->funcs->pOffsetWindowOrgEx( physdev, x, y, pt );
477 release_dc_ptr( dc );
479 return ret;
483 /***********************************************************************
484 * ScaleViewportExtEx (GDI32.@)
486 BOOL WINAPI ScaleViewportExtEx( HDC hdc, INT xNum, INT xDenom,
487 INT yNum, INT yDenom, LPSIZE size )
489 INT ret = FALSE;
490 DC * dc = get_dc_ptr( hdc );
492 if (dc)
494 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pScaleViewportExtEx );
495 ret = physdev->funcs->pScaleViewportExtEx( physdev, xNum, xDenom, yNum, yDenom, size );
496 release_dc_ptr( dc );
498 return ret;
502 /***********************************************************************
503 * ScaleWindowExtEx (GDI32.@)
505 BOOL WINAPI ScaleWindowExtEx( HDC hdc, INT xNum, INT xDenom,
506 INT yNum, INT yDenom, LPSIZE size )
508 INT ret = FALSE;
509 DC * dc = get_dc_ptr( hdc );
511 if (dc)
513 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pScaleWindowExtEx );
514 ret = physdev->funcs->pScaleWindowExtEx( physdev, xNum, xDenom, yNum, yDenom, size );
515 release_dc_ptr( dc );
517 return ret;
521 /****************************************************************************
522 * ModifyWorldTransform (GDI32.@)
524 BOOL WINAPI ModifyWorldTransform( HDC hdc, const XFORM *xform, DWORD mode )
526 BOOL ret = FALSE;
527 DC *dc;
529 if (!xform && mode != MWT_IDENTITY) return FALSE;
530 if ((dc = get_dc_ptr( hdc )))
532 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pModifyWorldTransform );
533 if (dc->GraphicsMode == GM_ADVANCED)
534 ret = physdev->funcs->pModifyWorldTransform( physdev, xform, mode );
535 release_dc_ptr( dc );
537 return ret;
541 /***********************************************************************
542 * SetWorldTransform (GDI32.@)
544 BOOL WINAPI SetWorldTransform( HDC hdc, const XFORM *xform )
546 BOOL ret = FALSE;
547 DC *dc;
549 if (!xform) return FALSE;
550 /* The transform must conform to (eM11 * eM22 != eM12 * eM21) requirement */
551 if (xform->eM11 * xform->eM22 == xform->eM12 * xform->eM21) return FALSE;
553 TRACE("eM11 %f eM12 %f eM21 %f eM22 %f eDx %f eDy %f\n",
554 xform->eM11, xform->eM12, xform->eM21, xform->eM22, xform->eDx, xform->eDy);
556 if ((dc = get_dc_ptr( hdc )))
558 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetWorldTransform );
559 if (dc->GraphicsMode == GM_ADVANCED)
560 ret = physdev->funcs->pSetWorldTransform( physdev, xform );
561 release_dc_ptr( dc );
563 return ret;
567 /***********************************************************************
568 * SetVirtualResolution (GDI32.@)
570 * Undocumented on msdn.
572 * Changes the values of screen size in pixels and millimeters used by
573 * the mapping mode functions.
575 * PARAMS
576 * hdc [I] Device context
577 * horz_res [I] Width in pixels (equivalent to HORZRES device cap).
578 * vert_res [I] Height in pixels (equivalent to VERTRES device cap).
579 * horz_size [I] Width in mm (equivalent to HORZSIZE device cap).
580 * vert_size [I] Height in mm (equivalent to VERTSIZE device cap).
582 * RETURNS
583 * TRUE if successful.
584 * FALSE if any (but not all) of the last four params are zero.
586 * NOTES
587 * This doesn't change the values returned by GetDeviceCaps, just the
588 * scaling of the mapping modes.
590 * Calling with the last four params equal to zero sets the values
591 * back to their defaults obtained by calls to GetDeviceCaps.
593 BOOL WINAPI SetVirtualResolution(HDC hdc, DWORD horz_res, DWORD vert_res,
594 DWORD horz_size, DWORD vert_size)
596 DC * dc;
597 TRACE("(%p %d %d %d %d)\n", hdc, horz_res, vert_res, horz_size, vert_size);
599 if(horz_res == 0 && vert_res == 0 && horz_size == 0 && vert_size == 0)
601 horz_res = GetDeviceCaps(hdc, HORZRES);
602 vert_res = GetDeviceCaps(hdc, VERTRES);
603 horz_size = GetDeviceCaps(hdc, HORZSIZE);
604 vert_size = GetDeviceCaps(hdc, VERTSIZE);
606 else if(horz_res == 0 || vert_res == 0 || horz_size == 0 || vert_size == 0)
607 return FALSE;
609 dc = get_dc_ptr( hdc );
610 if (!dc) return FALSE;
612 dc->virtual_res.cx = horz_res;
613 dc->virtual_res.cy = vert_res;
614 dc->virtual_size.cx = horz_size;
615 dc->virtual_size.cy = vert_size;
617 release_dc_ptr( dc );
618 return TRUE;