Sync with trunk revision r58045 to bring the corrections on configure.cmd and on...
[reactos.git] / win32ss / gdi / ntgdi / coord.c
1 /*
2 * COPYRIGHT: GNU GPL, See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Coordinate systems
5 * FILE: subsystems/win32/win32k/objects/coord.c
6 * PROGRAMER: Timo Kreuzer (timo.kreuzer@rectos.org)
7 */
8
9 /* Coordinate translation overview
10 * -------------------------------
11 *
12 * Windows uses 3 different coordinate systems, referred to as world space,
13 * page space and device space.
14 *
15 * Device space:
16 * This is the coordinate system of the physical device that displays the
17 * graphics. One unit matches one pixel of the surface. The coordinate system
18 * is always orthogonal.
19 *
20 * Page space:
21 * This is the coordinate system on the screen or on the paper layout for
22 * printer devices. The coordinate system is also orthogonal but one unit
23 * does not neccesarily match one pixel. Instead there are different mapping
24 * modes that can be set using SetMapMode() that specify how page space units
25 * are transformed into device space units. These mapping modes are:
26 * - MM_TEXT: One unit matches one unit in device space (one pixel)
27 * - MM_TWIPS One unit matches 1/20 point (1/1440 inch)
28 * - MM_LOMETRIC: One unit matches 0.1 millimeter
29 * - MM_HIMETRIC: One unit matches 0.01 millimeter
30 * - MM_LOENGLISH: One unit matches 0.01 inch
31 * - MM_HIENGLISH: One unit matches 0.001 inch
32 * - MM_ISOTROPIC:
33 * - MM_ANISOTROPIC:
34 * If the mapping mode is either MM_ISOTROPIC or MM_ANISOTROPIC, the actual
35 * transormation is calculated from the window and viewport extension.
36 * The window extension can be set using SetWindowExtEx() and describes the
37 * extents of an arbitrary window (not to confuse with the gui element!) in
38 * page space coordinates.
39 * The viewport extension can be set using SetViewportExtEx() and describes
40 * the extent of the same window in device space coordinates. If the mapping
41 * mode is MM_ISOTROPIC one of the viewport extensions can be adjusted by GDI
42 * to make sure the mapping stays isotropic, that is it has the same x/y ratio
43 * as the window extension.
44 *
45 * World space:
46 * World space is the coordinate system that is used for all GDI drawing
47 * operations. The metrics of this coordinate system depend on the DCs
48 * graphics mode, which can be set using SetGraphicsMode().
49 * If the graphics mode is GM_COMPATIBLE, world space is identical to page
50 * space and no additional transformation is applied.
51 * If the graphics mode is GM_ADVANCED, an arbitrary coordinate transformation
52 * can be set using SetWorldTransform(), which is applied to transform world
53 * space coordinates into page space coordinates.
54 *
55 * User mode data
56 * All coordinate translation data is stored in the dc attribute, so the values
57 * might be invalid. This has to be taken into account. Integer values might be
58 * zero, so when a division is made, the value has to be read first and then
59 * checked! For floating point values (FLOATOBJ as well) these restrictions do
60 * not apply, since we cannot get dividy-by-zero exceptions.
61 * Though the result might be a completely random and invalid value, if it was
62 * messed with in an illegal way in user mode. This is not a problem, since the
63 * result of coordinate transformations are never expected to be "valid" values.
64 * In the worst case, the drawing operation draws rubbish into the DC.
65 */
66
67 /* INCLUDES ******************************************************************/
68
69 #include <win32k.h>
70
71 #define NDEBUG
72 #include <debug.h>
73 C_ASSERT(sizeof(XFORML) == sizeof(XFORM));
74
75
76 /* GLOBALS *******************************************************************/
77
78 const MATRIX gmxIdentity =
79 {
80 FLOATOBJ_1, FLOATOBJ_0,
81 FLOATOBJ_0, FLOATOBJ_1,
82 FLOATOBJ_0, FLOATOBJ_0,
83 0, 0, XFORM_NO_TRANSLATION|XFORM_FORMAT_LTOL|XFORM_UNITY|XFORM_SCALE
84 };
85
86
87 /* FUNCTIONS *****************************************************************/
88
89 VOID
90 FASTCALL
91 DC_vFixIsotropicMapping(PDC pdc)
92 {
93 PDC_ATTR pdcattr;
94 LONG64 fx, fy;
95 LONG s;
96 SIZEL szlWindowExt, szlViewportExt;
97 ASSERT(pdc->pdcattr->iMapMode == MM_ISOTROPIC);
98
99 /* Get a pointer to the DC_ATTR */
100 pdcattr = pdc->pdcattr;
101
102 /* Read the extents, we rely on non-null values */
103 szlWindowExt = pdcattr->szlWindowExt;
104 szlViewportExt = pdcattr->szlViewportExt;
105
106 /* Check if all values are valid */
107 if ((szlWindowExt.cx == 0) || (szlWindowExt.cy == 0) ||
108 (szlViewportExt.cx == 0) || (szlViewportExt.cy == 0))
109 {
110 /* Someone put rubbish into the fields, just ignore it. */
111 return;
112 }
113
114 fx = abs((LONG64)szlWindowExt.cx * szlViewportExt.cy);
115 fy = abs((LONG64)szlWindowExt.cy * szlViewportExt.cx);
116
117 if (fx < fy)
118 {
119 s = (szlWindowExt.cy ^ szlViewportExt.cx) > 0 ? 1 : -1;
120 pdcattr->szlViewportExt.cx = (LONG)(fx * s / szlWindowExt.cy);
121 }
122 else if (fx > fy)
123 {
124 s = (szlWindowExt.cx ^ szlViewportExt.cy) > 0 ? 1 : -1;
125 pdcattr->szlViewportExt.cy = (LONG)(fy * s / szlWindowExt.cx);
126 }
127
128 /* Reset the flag */
129 pdc->pdcattr->flXform &= ~PAGE_EXTENTS_CHANGED;
130 }
131
132 VOID
133 FASTCALL
134 DC_vGetPageToDevice(PDC pdc, MATRIX *pmx)
135 {
136 PDC_ATTR pdcattr = pdc->pdcattr;
137 PSIZEL pszlViewPortExt;
138
139 /* Get the viewport extension */
140 pszlViewPortExt = DC_pszlViewportExt(pdc);
141
142 /* No shearing / rotation */
143 FLOATOBJ_SetLong(&pmx->efM12, 0);
144 FLOATOBJ_SetLong(&pmx->efM21, 0);
145
146 /* Calculate scaling */
147 if (pdcattr->szlWindowExt.cx != 0)
148 {
149 FLOATOBJ_SetLong(&pmx->efM11, pszlViewPortExt->cx);
150 FLOATOBJ_DivLong(&pmx->efM11, pdcattr->szlWindowExt.cx);
151 }
152 else
153 FLOATOBJ_SetLong(&pmx->efM11, 1);
154
155 if (pdcattr->szlWindowExt.cy != 0)
156 {
157 FLOATOBJ_SetLong(&pmx->efM22, pszlViewPortExt->cy);
158 FLOATOBJ_DivLong(&pmx->efM22, pdcattr->szlWindowExt.cy);
159 }
160 else
161 FLOATOBJ_SetLong(&pmx->efM22, 1);
162
163 /* Calculate x offset */
164 FLOATOBJ_SetLong(&pmx->efDx, -pdcattr->ptlWindowOrg.x);
165 FLOATOBJ_Mul(&pmx->efDx, &pmx->efM11);
166 FLOATOBJ_AddLong(&pmx->efDx, pdcattr->ptlViewportOrg.x);
167
168 /* Calculate y offset */
169 FLOATOBJ_SetLong(&pmx->efDy, -pdcattr->ptlWindowOrg.y);
170 FLOATOBJ_Mul(&pmx->efDy, &pmx->efM22);
171 FLOATOBJ_AddLong(&pmx->efDy, pdcattr->ptlViewportOrg.y);
172 }
173
174 VOID
175 FASTCALL
176 DC_vUpdateWorldToDevice(PDC pdc)
177 {
178 XFORMOBJ xoPageToDevice, xoWorldToPage, xoWorldToDevice;
179 MATRIX mxPageToDevice;
180
181 // FIXME: make sure world-to-page is valid!
182
183 /* Construct a transformation to do the page-to-device conversion */
184 DC_vGetPageToDevice(pdc, &mxPageToDevice);
185 XFORMOBJ_vInit(&xoPageToDevice, &mxPageToDevice);
186
187 /* Recalculate the world-to-device xform */
188 XFORMOBJ_vInit(&xoWorldToPage, &pdc->pdcattr->mxWorldToPage);
189 XFORMOBJ_vInit(&xoWorldToDevice, &pdc->pdcattr->mxWorldToDevice);
190 XFORMOBJ_iCombine(&xoWorldToDevice, &xoWorldToPage, &xoPageToDevice);
191
192 /* Reset the flags */
193 pdc->pdcattr->flXform &= ~(PAGE_XLATE_CHANGED|PAGE_EXTENTS_CHANGED|WORLD_XFORM_CHANGED);
194 }
195
196 VOID
197 FASTCALL
198 DC_vUpdateDeviceToWorld(PDC pdc)
199 {
200 XFORMOBJ xoWorldToDevice, xoDeviceToWorld;
201 PMATRIX pmxWorldToDevice;
202
203 /* Get the world-to-device translation */
204 pmxWorldToDevice = DC_pmxWorldToDevice(pdc);
205 XFORMOBJ_vInit(&xoWorldToDevice, pmxWorldToDevice);
206
207 /* Create inverse of world-to-device transformation */
208 XFORMOBJ_vInit(&xoDeviceToWorld, &pdc->pdcattr->mxDeviceToWorld);
209 if (XFORMOBJ_iInverse(&xoDeviceToWorld, &xoWorldToDevice) == DDI_ERROR)
210 {
211 // FIXME: do we need to reset anything?
212 return;
213 }
214
215 /* Reset the flag */
216 pdc->pdcattr->flXform &= ~DEVICE_TO_WORLD_INVALID;
217 }
218
219 BOOL
220 NTAPI
221 GreCombineTransform(
222 XFORML *pxformDest,
223 XFORML *pxform1,
224 XFORML *pxform2)
225 {
226 MATRIX mxDest, mx1, mx2;
227 XFORMOBJ xoDest, xo1, xo2;
228
229 /* Check for illegal parameters */
230 if (!pxformDest || !pxform1 || !pxform2) return FALSE;
231
232 /* Initialize XFORMOBJs */
233 XFORMOBJ_vInit(&xoDest, &mxDest);
234 XFORMOBJ_vInit(&xo1, &mx1);
235 XFORMOBJ_vInit(&xo2, &mx2);
236
237 /* Convert the XFORMLs into XFORMOBJs */
238 XFORMOBJ_iSetXform(&xo1, pxform1);
239 XFORMOBJ_iSetXform(&xo2, pxform2);
240
241 /* Combine them */
242 XFORMOBJ_iCombine(&xoDest, &xo1, &xo2);
243
244 /* Translate back into XFORML */
245 XFORMOBJ_iGetXform(&xoDest, pxformDest);
246
247 return TRUE;
248 }
249
250 BOOL
251 APIENTRY
252 NtGdiCombineTransform(
253 LPXFORM UnsafeXFormResult,
254 LPXFORM Unsafexform1,
255 LPXFORM Unsafexform2)
256 {
257 BOOL Ret;
258
259 _SEH2_TRY
260 {
261 ProbeForWrite(UnsafeXFormResult, sizeof(XFORM), 1);
262 ProbeForRead(Unsafexform1, sizeof(XFORM), 1);
263 ProbeForRead(Unsafexform2, sizeof(XFORM), 1);
264 Ret = GreCombineTransform((XFORML*)UnsafeXFormResult,
265 (XFORML*)Unsafexform1,
266 (XFORML*)Unsafexform2);
267 }
268 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
269 {
270 Ret = FALSE;
271 }
272 _SEH2_END;
273
274 return Ret;
275 }
276
277 // FIXME: Should be XFORML and use XFORMOBJ functions directly
278 BOOL
279 APIENTRY
280 NtGdiGetTransform(
281 HDC hdc,
282 DWORD iXform,
283 LPXFORM pXForm)
284 {
285 PDC pdc;
286 BOOL ret = TRUE;
287 MATRIX mxPageToDevice;
288 XFORMOBJ xo;
289 PMATRIX pmx;
290
291 if (!pXForm)
292 {
293 EngSetLastError(ERROR_INVALID_PARAMETER);
294 return FALSE;
295 }
296
297 pdc = DC_LockDc(hdc);
298 if (!pdc)
299 {
300 EngSetLastError(ERROR_INVALID_HANDLE);
301 return FALSE;
302 }
303
304 switch (iXform)
305 {
306 case GdiWorldSpaceToPageSpace:
307 pmx = DC_pmxWorldToPage(pdc);
308 break;
309
310 case GdiWorldSpaceToDeviceSpace:
311 pmx = DC_pmxWorldToDevice(pdc);
312 break;
313
314 case GdiDeviceSpaceToWorldSpace:
315 pmx = DC_pmxDeviceToWorld(pdc);
316 break;
317
318 case GdiPageSpaceToDeviceSpace:
319 DC_vGetPageToDevice(pdc, &mxPageToDevice);
320 pmx = &mxPageToDevice;
321 break;
322
323 default:
324 DPRINT1("Unknown transform %lu\n", iXform);
325 ret = FALSE;
326 goto leave;
327 }
328
329 /* Initialize an XFORMOBJ */
330 XFORMOBJ_vInit(&xo, pmx);
331
332 _SEH2_TRY
333 {
334 ProbeForWrite(pXForm, sizeof(XFORML), 1);
335 XFORMOBJ_iGetXform(&xo, (XFORML*)pXForm);
336 }
337 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
338 {
339 ret = FALSE;
340 }
341 _SEH2_END;
342
343 leave:
344 DC_UnlockDc(pdc);
345 return ret;
346 }
347
348
349 /*!
350 * Converts points from logical coordinates into device coordinates.
351 * Conversion depends on the mapping mode,
352 * world transfrom, viewport origin settings for the given device context.
353 * \param hDC device context.
354 * \param Points an array of POINT structures (in/out).
355 * \param Count number of elements in the array of POINT structures.
356 * \return TRUE if success, FALSE otherwise.
357 */
358 BOOL
359 APIENTRY
360 NtGdiTransformPoints(
361 HDC hDC,
362 PPOINT UnsafePtsIn,
363 PPOINT UnsafePtOut,
364 INT Count,
365 INT iMode)
366 {
367 PDC pdc;
368 LPPOINT Points;
369 ULONG Size;
370 BOOL ret = TRUE;
371
372 if (Count <= 0)
373 return TRUE;
374
375 pdc = DC_LockDc(hDC);
376 if (!pdc)
377 {
378 EngSetLastError(ERROR_INVALID_PARAMETER);
379 return FALSE;
380 }
381
382 Size = Count * sizeof(POINT);
383
384 // FIXME: It would be wise to have a small stack buffer as optimization
385 Points = ExAllocatePoolWithTag(PagedPool, Size, GDITAG_TEMP);
386 if (!Points)
387 {
388 DC_UnlockDc(pdc);
389 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
390 return FALSE;
391 }
392
393 _SEH2_TRY
394 {
395 ProbeForWrite(UnsafePtOut, Size, 1);
396 ProbeForRead(UnsafePtsIn, Size, 1);
397 RtlCopyMemory(Points, UnsafePtsIn, Size);
398 }
399 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
400 {
401 /* Do not set last error */
402 _SEH2_YIELD(goto leave;)
403 }
404 _SEH2_END;
405
406 switch (iMode)
407 {
408 case GdiDpToLp:
409 DC_vXformDeviceToWorld(pdc, Count, Points, Points);
410 break;
411
412 case GdiLpToDp:
413 DC_vXformWorldToDevice(pdc, Count, Points, Points);
414 break;
415
416 case 2: // Not supported yet. Need testing.
417 default:
418 {
419 EngSetLastError(ERROR_INVALID_PARAMETER);
420 ret = FALSE;
421 goto leave;
422 }
423 }
424
425 _SEH2_TRY
426 {
427 /* Pointer was already probed! */
428 RtlCopyMemory(UnsafePtOut, Points, Size);
429 }
430 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
431 {
432 /* Do not set last error */
433 ret = 0;
434 }
435 _SEH2_END;
436
437 //
438 // If we are getting called that means User XForms is a mess!
439 //
440 leave:
441 DC_UnlockDc(pdc);
442 ExFreePoolWithTag(Points, GDITAG_TEMP);
443 return ret;
444 }
445
446 BOOL
447 NTAPI
448 GreModifyWorldTransform(
449 PDC pdc,
450 const XFORML *pxform,
451 DWORD dwMode)
452 {
453 MATRIX mxSrc;
454 XFORMOBJ xoSrc, xoDC;
455
456 switch (dwMode)
457 {
458 case MWT_IDENTITY:
459 pdc->pdcattr->mxWorldToPage = gmxIdentity;
460 break;
461
462 case MWT_LEFTMULTIPLY:
463 XFORMOBJ_vInit(&xoDC, &pdc->pdcattr->mxWorldToPage);
464 XFORMOBJ_vInit(&xoSrc, &mxSrc);
465 if (XFORMOBJ_iSetXform(&xoSrc, pxform) == DDI_ERROR)
466 return FALSE;
467 XFORMOBJ_iCombine(&xoDC, &xoSrc, &xoDC);
468 break;
469
470 case MWT_RIGHTMULTIPLY:
471 XFORMOBJ_vInit(&xoDC, &pdc->pdcattr->mxWorldToPage);
472 XFORMOBJ_vInit(&xoSrc, &mxSrc);
473 if (XFORMOBJ_iSetXform(&xoSrc, pxform) == DDI_ERROR)
474 return FALSE;
475 XFORMOBJ_iCombine(&xoDC, &xoDC, &xoSrc);
476 break;
477
478 case MWT_MAX+1: // Must be MWT_SET????
479 XFORMOBJ_vInit(&xoDC, &pdc->pdcattr->mxWorldToPage);
480 if (XFORMOBJ_iSetXform(&xoDC, pxform) == DDI_ERROR)
481 return FALSE;
482 break;
483
484 default:
485 return FALSE;
486 }
487
488 /*Set invalidation flags */
489 pdc->pdcattr->flXform |= WORLD_XFORM_CHANGED|DEVICE_TO_WORLD_INVALID;
490
491 return TRUE;
492 }
493
494 BOOL
495 APIENTRY
496 NtGdiModifyWorldTransform(
497 HDC hdc,
498 LPXFORM pxformUnsafe,
499 DWORD dwMode)
500 {
501 PDC pdc;
502 XFORML xformSafe;
503 BOOL Ret = TRUE;
504
505 pdc = DC_LockDc(hdc);
506 if (!pdc)
507 {
508 EngSetLastError(ERROR_INVALID_HANDLE);
509 return FALSE;
510 }
511
512 /* The xform is permitted to be NULL for MWT_IDENTITY.
513 * However, if it is not NULL, then it must be valid even
514 * though it is not used. */
515 if (pxformUnsafe != NULL || dwMode != MWT_IDENTITY)
516 {
517 _SEH2_TRY
518 {
519 ProbeForRead(pxformUnsafe, sizeof(XFORML), 1);
520 RtlCopyMemory(&xformSafe, pxformUnsafe, sizeof(XFORML));
521 }
522 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
523 {
524 Ret = FALSE;
525 }
526 _SEH2_END;
527 }
528
529 /* Safe to handle kernel mode data. */
530 if (Ret) Ret = GreModifyWorldTransform(pdc, &xformSafe, dwMode);
531 DC_UnlockDc(pdc);
532 return Ret;
533 }
534
535 BOOL
536 APIENTRY
537 NtGdiOffsetViewportOrgEx(
538 HDC hDC,
539 int XOffset,
540 int YOffset,
541 LPPOINT UnsafePoint)
542 {
543 PDC dc;
544 PDC_ATTR pdcattr;
545 NTSTATUS Status = STATUS_SUCCESS;
546
547 dc = DC_LockDc(hDC);
548 if (!dc)
549 {
550 EngSetLastError(ERROR_INVALID_HANDLE);
551 return FALSE;
552 }
553 pdcattr = dc->pdcattr;
554
555 if (UnsafePoint)
556 {
557 _SEH2_TRY
558 {
559 ProbeForWrite(UnsafePoint, sizeof(POINT), 1);
560 UnsafePoint->x = pdcattr->ptlViewportOrg.x;
561 UnsafePoint->y = pdcattr->ptlViewportOrg.y;
562 if (pdcattr->dwLayout & LAYOUT_RTL)
563 {
564 UnsafePoint->x = -UnsafePoint->x;
565 }
566 }
567 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
568 {
569 Status = _SEH2_GetExceptionCode();
570 }
571 _SEH2_END;
572
573 if (!NT_SUCCESS(Status))
574 {
575 SetLastNtError(Status);
576 DC_UnlockDc(dc);
577 return FALSE;
578 }
579 }
580
581 if (pdcattr->dwLayout & LAYOUT_RTL)
582 {
583 XOffset = -XOffset;
584 }
585 pdcattr->ptlViewportOrg.x += XOffset;
586 pdcattr->ptlViewportOrg.y += YOffset;
587 pdcattr->flXform |= PAGE_XLATE_CHANGED;
588
589 DC_UnlockDc(dc);
590
591 return TRUE;
592 }
593
594 BOOL
595 APIENTRY
596 NtGdiOffsetWindowOrgEx(
597 HDC hDC,
598 int XOffset,
599 int YOffset,
600 LPPOINT Point)
601 {
602 PDC dc;
603 PDC_ATTR pdcattr;
604
605 dc = DC_LockDc(hDC);
606 if (!dc)
607 {
608 EngSetLastError(ERROR_INVALID_HANDLE);
609 return FALSE;
610 }
611 pdcattr = dc->pdcattr;
612
613 if (Point)
614 {
615 NTSTATUS Status = STATUS_SUCCESS;
616
617 _SEH2_TRY
618 {
619 ProbeForWrite(Point, sizeof(POINT), 1);
620 Point->x = pdcattr->ptlWindowOrg.x;
621 Point->y = pdcattr->ptlWindowOrg.y;
622 }
623 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
624 {
625 Status = _SEH2_GetExceptionCode();
626 }
627 _SEH2_END;
628
629 if (!NT_SUCCESS(Status))
630 {
631 SetLastNtError(Status);
632 DC_UnlockDc(dc);
633 return FALSE;
634 }
635 }
636
637 pdcattr->ptlWindowOrg.x += XOffset;
638 pdcattr->ptlWindowOrg.y += YOffset;
639 pdcattr->flXform |= PAGE_XLATE_CHANGED|DEVICE_TO_WORLD_INVALID;
640
641 DC_UnlockDc(dc);
642
643 return TRUE;
644 }
645
646 BOOL
647 APIENTRY
648 NtGdiScaleViewportExtEx(
649 HDC hDC,
650 int Xnum,
651 int Xdenom,
652 int Ynum,
653 int Ydenom,
654 LPSIZE pSize)
655 {
656 PDC pDC;
657 PDC_ATTR pdcattr;
658 BOOL Ret = FALSE;
659 LONG X, Y;
660
661 pDC = DC_LockDc(hDC);
662 if (!pDC)
663 {
664 EngSetLastError(ERROR_INVALID_HANDLE);
665 return FALSE;
666 }
667 pdcattr = pDC->pdcattr;
668
669 if (pdcattr->iMapMode > MM_TWIPS)
670 {
671 if (Xdenom && Ydenom)
672 {
673 DC_pszlViewportExt(pDC);
674 X = Xnum * pdcattr->szlViewportExt.cx / Xdenom;
675 if (X)
676 {
677 Y = Ynum * pdcattr->szlViewportExt.cy / Ydenom;
678 if (Y)
679 {
680 pdcattr->szlViewportExt.cx = X;
681 pdcattr->szlViewportExt.cy = Y;
682 pdcattr->flXform |= PAGE_XLATE_CHANGED;
683
684 IntMirrorWindowOrg(pDC);
685
686 pdcattr->flXform |= (PAGE_EXTENTS_CHANGED |
687 INVALIDATE_ATTRIBUTES |
688 DEVICE_TO_WORLD_INVALID);
689
690 if (pdcattr->iMapMode == MM_ISOTROPIC)
691 {
692 DC_vFixIsotropicMapping(pDC);
693 }
694
695 Ret = TRUE;
696 }
697 }
698 }
699 }
700 else
701 Ret = TRUE;
702
703 if (pSize)
704 {
705 _SEH2_TRY
706 {
707 ProbeForWrite(pSize, sizeof(SIZE), 1);
708
709 pSize->cx = pdcattr->szlViewportExt.cx;
710 pSize->cy = pdcattr->szlViewportExt.cy;
711 }
712 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
713 {
714 SetLastNtError(_SEH2_GetExceptionCode());
715 Ret = FALSE;
716 }
717 _SEH2_END;
718 }
719
720 DC_UnlockDc(pDC);
721 return Ret;
722 }
723
724 BOOL
725 APIENTRY
726 NtGdiScaleWindowExtEx(
727 HDC hDC,
728 int Xnum,
729 int Xdenom,
730 int Ynum,
731 int Ydenom,
732 LPSIZE pSize)
733 {
734 PDC pDC;
735 PDC_ATTR pdcattr;
736 BOOL Ret = FALSE;
737 LONG X, Y;
738
739 pDC = DC_LockDc(hDC);
740 if (!pDC)
741 {
742 EngSetLastError(ERROR_INVALID_HANDLE);
743 return FALSE;
744 }
745 pdcattr = pDC->pdcattr;
746
747 if (pSize)
748 {
749 NTSTATUS Status = STATUS_SUCCESS;
750
751 _SEH2_TRY
752 {
753 ProbeForWrite(pSize, sizeof(SIZE), 1);
754
755 X = pdcattr->szlWindowExt.cx;
756 if (pdcattr->dwLayout & LAYOUT_RTL) X = -X;
757 pSize->cx = X;
758 pSize->cy = pdcattr->szlWindowExt.cy;
759 }
760 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
761 {
762 Status = _SEH2_GetExceptionCode();
763 }
764 _SEH2_END;
765
766 if (!NT_SUCCESS(Status))
767 {
768 SetLastNtError(Status);
769 DC_UnlockDc(pDC);
770 return FALSE;
771 }
772 }
773
774 if (pdcattr->iMapMode > MM_TWIPS)
775 {
776 if (Xdenom && Ydenom)
777 {
778 X = Xnum * pdcattr->szlWindowExt.cx / Xdenom;
779 if (X)
780 {
781 Y = Ynum * pdcattr->szlWindowExt.cy / Ydenom;
782 if (Y)
783 {
784 pdcattr->szlWindowExt.cx = X;
785 pdcattr->szlWindowExt.cy = Y;
786
787 IntMirrorWindowOrg(pDC);
788
789 pdcattr->flXform |= (PAGE_EXTENTS_CHANGED|INVALIDATE_ATTRIBUTES|DEVICE_TO_WORLD_INVALID);
790
791 Ret = TRUE;
792 }
793 }
794 }
795 }
796 else
797 Ret = TRUE;
798
799 DC_UnlockDc(pDC);
800 return Ret;
801 }
802
803 int
804 APIENTRY
805 IntGdiSetMapMode(
806 PDC dc,
807 int MapMode)
808 {
809 int PrevMapMode;
810 PDC_ATTR pdcattr = dc->pdcattr;
811
812 PrevMapMode = pdcattr->iMapMode;
813
814 pdcattr->iMapMode = MapMode;
815 pdcattr->flXform &= ~(ISO_OR_ANISO_MAP_MODE|PTOD_EFM22_NEGATIVE|
816 PTOD_EFM11_NEGATIVE|POSITIVE_Y_IS_UP|PAGE_TO_DEVICE_SCALE_IDENTITY|
817 PAGE_TO_DEVICE_IDENTITY);
818
819 switch (MapMode)
820 {
821 case MM_TEXT:
822 pdcattr->szlWindowExt.cx = 1;
823 pdcattr->szlWindowExt.cy = 1;
824 pdcattr->szlViewportExt.cx = 1;
825 pdcattr->szlViewportExt.cy = 1;
826 pdcattr->flXform |= PAGE_TO_DEVICE_SCALE_IDENTITY;
827 break;
828
829 case MM_ISOTROPIC:
830 pdcattr->flXform |= ISO_OR_ANISO_MAP_MODE;
831 /* Fall through */
832
833 case MM_LOMETRIC:
834 pdcattr->szlWindowExt.cx = pdcattr->szlVirtualDeviceMm.cx * 10;
835 pdcattr->szlWindowExt.cy = pdcattr->szlVirtualDeviceMm.cy * 10;
836 pdcattr->szlViewportExt.cx = pdcattr->szlVirtualDevicePixel.cx;
837 pdcattr->szlViewportExt.cy = -pdcattr->szlVirtualDevicePixel.cy;
838 break;
839
840 case MM_HIMETRIC:
841 pdcattr->szlWindowExt.cx = pdcattr->szlVirtualDeviceMm.cx * 100;
842 pdcattr->szlWindowExt.cy = pdcattr->szlVirtualDeviceMm.cy * 100;
843 pdcattr->szlViewportExt.cx = pdcattr->szlVirtualDevicePixel.cx;
844 pdcattr->szlViewportExt.cy = -pdcattr->szlVirtualDevicePixel.cy;
845 break;
846
847 case MM_LOENGLISH:
848 pdcattr->szlWindowExt.cx = MulDiv(1000, pdcattr->szlVirtualDeviceMm.cx, 254);
849 pdcattr->szlWindowExt.cy = MulDiv(1000, pdcattr->szlVirtualDeviceMm.cy, 254);
850 pdcattr->szlViewportExt.cx = pdcattr->szlVirtualDevicePixel.cx;
851 pdcattr->szlViewportExt.cy = -pdcattr->szlVirtualDevicePixel.cy;
852 break;
853
854 case MM_HIENGLISH:
855 pdcattr->szlWindowExt.cx = MulDiv(10000, pdcattr->szlVirtualDeviceMm.cx, 254);
856 pdcattr->szlWindowExt.cy = MulDiv(10000, pdcattr->szlVirtualDeviceMm.cy, 254);
857 pdcattr->szlViewportExt.cx = pdcattr->szlVirtualDevicePixel.cx;
858 pdcattr->szlViewportExt.cy = -pdcattr->szlVirtualDevicePixel.cy;
859 break;
860
861 case MM_TWIPS:
862 pdcattr->szlWindowExt.cx = MulDiv(14400, pdcattr->szlVirtualDeviceMm.cx, 254);
863 pdcattr->szlWindowExt.cy = MulDiv(14400, pdcattr->szlVirtualDeviceMm.cy, 254);
864 pdcattr->szlViewportExt.cx = pdcattr->szlVirtualDevicePixel.cx;
865 pdcattr->szlViewportExt.cy = -pdcattr->szlVirtualDevicePixel.cy;
866 break;
867
868 case MM_ANISOTROPIC:
869 pdcattr->flXform &= ~(PAGE_TO_DEVICE_IDENTITY|POSITIVE_Y_IS_UP);
870 pdcattr->flXform |= ISO_OR_ANISO_MAP_MODE;
871 break;
872
873 default:
874 pdcattr->iMapMode = PrevMapMode;
875 PrevMapMode = 0;
876 }
877
878 pdcattr->flXform |= (PAGE_XLATE_CHANGED|PAGE_EXTENTS_CHANGED|
879 INVALIDATE_ATTRIBUTES|DEVICE_TO_PAGE_INVALID|DEVICE_TO_WORLD_INVALID);
880
881 return PrevMapMode;
882 }
883
884
885 BOOL
886 APIENTRY
887 NtGdiSetViewportOrgEx(
888 HDC hDC,
889 int X,
890 int Y,
891 LPPOINT Point)
892 {
893 PDC dc;
894 PDC_ATTR pdcattr;
895
896 dc = DC_LockDc(hDC);
897 if (!dc)
898 {
899 EngSetLastError(ERROR_INVALID_HANDLE);
900 return FALSE;
901 }
902 pdcattr = dc->pdcattr;
903
904 if (Point)
905 {
906 NTSTATUS Status = STATUS_SUCCESS;
907
908 _SEH2_TRY
909 {
910 ProbeForWrite(Point, sizeof(POINT), 1);
911 Point->x = pdcattr->ptlViewportOrg.x;
912 Point->y = pdcattr->ptlViewportOrg.y;
913 }
914 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
915 {
916 Status = _SEH2_GetExceptionCode();
917 }
918 _SEH2_END;
919
920 if (!NT_SUCCESS(Status))
921 {
922 SetLastNtError(Status);
923 DC_UnlockDc(dc);
924 return FALSE;
925 }
926 }
927
928 pdcattr->ptlViewportOrg.x = X;
929 pdcattr->ptlViewportOrg.y = Y;
930 pdcattr->flXform |= PAGE_XLATE_CHANGED;
931
932 DC_UnlockDc(dc);
933
934 return TRUE;
935 }
936
937 BOOL
938 APIENTRY
939 NtGdiSetWindowOrgEx(
940 HDC hDC,
941 int X,
942 int Y,
943 LPPOINT Point)
944 {
945 PDC dc;
946 PDC_ATTR pdcattr;
947
948 dc = DC_LockDc(hDC);
949 if (!dc)
950 {
951 EngSetLastError(ERROR_INVALID_HANDLE);
952 return FALSE;
953 }
954 pdcattr = dc->pdcattr;
955
956 if (Point)
957 {
958 NTSTATUS Status = STATUS_SUCCESS;
959
960 _SEH2_TRY
961 {
962 ProbeForWrite(Point, sizeof(POINT), 1);
963 Point->x = pdcattr->ptlWindowOrg.x;
964 Point->y = pdcattr->ptlWindowOrg.y;
965 }
966 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
967 {
968 Status = _SEH2_GetExceptionCode();
969 }
970 _SEH2_END;
971
972 if (!NT_SUCCESS(Status))
973 {
974 SetLastNtError(Status);
975 DC_UnlockDc(dc);
976 return FALSE;
977 }
978 }
979
980 pdcattr->ptlWindowOrg.x = X;
981 pdcattr->ptlWindowOrg.y = Y;
982 pdcattr->flXform |= PAGE_XLATE_CHANGED;
983
984 DC_UnlockDc(dc);
985
986 return TRUE;
987 }
988
989 //
990 // Mirror Window function.
991 //
992 VOID
993 FASTCALL
994 IntMirrorWindowOrg(PDC dc)
995 {
996 PDC_ATTR pdcattr;
997 LONG X;
998
999 pdcattr = dc->pdcattr;
1000
1001 if (!(pdcattr->dwLayout & LAYOUT_RTL))
1002 {
1003 pdcattr->ptlWindowOrg.x = pdcattr->lWindowOrgx; // Flip it back.
1004 return;
1005 }
1006 if (!pdcattr->szlViewportExt.cx) return;
1007 //
1008 // WOrgx = wox - (Width - 1) * WExtx / VExtx
1009 //
1010 X = (dc->erclWindow.right - dc->erclWindow.left) - 1; // Get device width - 1
1011
1012 X = (X * pdcattr->szlWindowExt.cx) / pdcattr->szlViewportExt.cx;
1013
1014 pdcattr->ptlWindowOrg.x = pdcattr->lWindowOrgx - X; // Now set the inverted win origion.
1015 pdcattr->flXform |= PAGE_XLATE_CHANGED;
1016
1017 return;
1018 }
1019
1020 VOID
1021 NTAPI
1022 DC_vSetLayout(
1023 IN PDC pdc,
1024 IN LONG wox,
1025 IN DWORD dwLayout)
1026 {
1027 PDC_ATTR pdcattr = pdc->pdcattr;
1028
1029 pdcattr->dwLayout = dwLayout;
1030
1031 if (!(dwLayout & LAYOUT_ORIENTATIONMASK)) return;
1032
1033 if (dwLayout & LAYOUT_RTL)
1034 {
1035 pdcattr->iMapMode = MM_ANISOTROPIC;
1036 }
1037
1038 //pdcattr->szlWindowExt.cy = -pdcattr->szlWindowExt.cy;
1039 //pdcattr->ptlWindowOrg.x = -pdcattr->ptlWindowOrg.x;
1040
1041 //if (wox == -1)
1042 // IntMirrorWindowOrg(pdc);
1043 //else
1044 // pdcattr->ptlWindowOrg.x = wox - pdcattr->ptlWindowOrg.x;
1045
1046 if (!(pdcattr->flTextAlign & TA_CENTER)) pdcattr->flTextAlign |= TA_RIGHT;
1047
1048 if (pdc->dclevel.flPath & DCPATH_CLOCKWISE)
1049 pdc->dclevel.flPath &= ~DCPATH_CLOCKWISE;
1050 else
1051 pdc->dclevel.flPath |= DCPATH_CLOCKWISE;
1052
1053 pdcattr->flXform |= (PAGE_EXTENTS_CHANGED |
1054 INVALIDATE_ATTRIBUTES |
1055 DEVICE_TO_WORLD_INVALID);
1056 }
1057
1058 // NtGdiSetLayout
1059 //
1060 // The default is left to right. This function changes it to right to left, which
1061 // is the standard in Arabic and Hebrew cultures.
1062 //
1063 /*
1064 * @implemented
1065 */
1066 DWORD
1067 APIENTRY
1068 NtGdiSetLayout(
1069 IN HDC hdc,
1070 IN LONG wox,
1071 IN DWORD dwLayout)
1072 {
1073 PDC pdc;
1074 DWORD dwOldLayout;
1075
1076 pdc = DC_LockDc(hdc);
1077 if (!pdc)
1078 {
1079 EngSetLastError(ERROR_INVALID_HANDLE);
1080 return GDI_ERROR;
1081 }
1082
1083 dwOldLayout = pdc->pdcattr->dwLayout;
1084 DC_vSetLayout(pdc, wox, dwLayout);
1085
1086 DC_UnlockDc(pdc);
1087 return dwOldLayout;
1088 }
1089
1090 /*
1091 * @implemented
1092 */
1093 LONG
1094 APIENTRY
1095 NtGdiGetDeviceWidth(
1096 IN HDC hdc)
1097 {
1098 PDC dc;
1099 LONG Ret;
1100 dc = DC_LockDc(hdc);
1101 if (!dc)
1102 {
1103 EngSetLastError(ERROR_INVALID_HANDLE);
1104 return 0;
1105 }
1106 Ret = dc->erclWindow.right - dc->erclWindow.left;
1107 DC_UnlockDc(dc);
1108 return Ret;
1109 }
1110
1111 /*
1112 * @implemented
1113 */
1114 BOOL
1115 APIENTRY
1116 NtGdiMirrorWindowOrg(
1117 IN HDC hdc)
1118 {
1119 PDC dc;
1120 dc = DC_LockDc(hdc);
1121 if (!dc)
1122 {
1123 EngSetLastError(ERROR_INVALID_HANDLE);
1124 return FALSE;
1125 }
1126 IntMirrorWindowOrg(dc);
1127 DC_UnlockDc(dc);
1128 return TRUE;
1129 }
1130
1131 /*
1132 * @implemented
1133 */
1134 BOOL
1135 APIENTRY
1136 NtGdiSetSizeDevice(
1137 IN HDC hdc,
1138 IN INT cxVirtualDevice,
1139 IN INT cyVirtualDevice)
1140 {
1141 PDC dc;
1142 PDC_ATTR pdcattr;
1143
1144 if (!cxVirtualDevice || !cyVirtualDevice)
1145 {
1146 return FALSE;
1147 }
1148
1149 dc = DC_LockDc(hdc);
1150 if (!dc) return FALSE;
1151
1152 pdcattr = dc->pdcattr;
1153
1154 pdcattr->szlVirtualDeviceSize.cx = cxVirtualDevice;
1155 pdcattr->szlVirtualDeviceSize.cy = cyVirtualDevice;
1156
1157 DC_UnlockDc(dc);
1158
1159 return TRUE;
1160 }
1161
1162 /*
1163 * @implemented
1164 */
1165 BOOL
1166 APIENTRY
1167 NtGdiSetVirtualResolution(
1168 IN HDC hdc,
1169 IN INT cxVirtualDevicePixel,
1170 IN INT cyVirtualDevicePixel,
1171 IN INT cxVirtualDeviceMm,
1172 IN INT cyVirtualDeviceMm)
1173 {
1174 PDC dc;
1175 PDC_ATTR pdcattr;
1176
1177 /* Check parameters (all zeroes resets to real resolution) */
1178 if (cxVirtualDevicePixel == 0 && cyVirtualDevicePixel == 0 &&
1179 cxVirtualDeviceMm == 0 && cyVirtualDeviceMm == 0)
1180 {
1181 cxVirtualDevicePixel = NtGdiGetDeviceCaps(hdc, HORZRES);
1182 cyVirtualDevicePixel = NtGdiGetDeviceCaps(hdc, VERTRES);
1183 cxVirtualDeviceMm = NtGdiGetDeviceCaps(hdc, HORZSIZE);
1184 cyVirtualDeviceMm = NtGdiGetDeviceCaps(hdc, VERTSIZE);
1185 }
1186 else if (cxVirtualDevicePixel == 0 || cyVirtualDevicePixel == 0 ||
1187 cxVirtualDeviceMm == 0 || cyVirtualDeviceMm == 0)
1188 {
1189 return FALSE;
1190 }
1191
1192 dc = DC_LockDc(hdc);
1193 if (!dc) return FALSE;
1194
1195 pdcattr = dc->pdcattr;
1196
1197 pdcattr->szlVirtualDevicePixel.cx = cxVirtualDevicePixel;
1198 pdcattr->szlVirtualDevicePixel.cy = cyVirtualDevicePixel;
1199 pdcattr->szlVirtualDeviceMm.cx = cxVirtualDeviceMm;
1200 pdcattr->szlVirtualDeviceMm.cy = cyVirtualDeviceMm;
1201
1202 // DC_vUpdateXforms(dc);
1203 DC_UnlockDc(dc);
1204 return TRUE;
1205 }
1206
1207 static
1208 VOID FASTCALL
1209 DC_vGetAspectRatioFilter(PDC pDC, LPSIZE AspectRatio)
1210 {
1211 if (pDC->pdcattr->flFontMapper & 1) // TRUE assume 1.
1212 {
1213 // "This specifies that Windows should only match fonts that have the
1214 // same aspect ratio as the display.", Programming Windows, Fifth Ed.
1215 AspectRatio->cx = pDC->ppdev->gdiinfo.ulLogPixelsX;
1216 AspectRatio->cy = pDC->ppdev->gdiinfo.ulLogPixelsY;
1217 }
1218 else
1219 {
1220 AspectRatio->cx = 0;
1221 AspectRatio->cy = 0;
1222 }
1223 }
1224
1225 BOOL APIENTRY
1226 NtGdiGetDCPoint(
1227 HDC hDC,
1228 UINT iPoint,
1229 PPOINTL Point)
1230 {
1231 BOOL Ret = TRUE;
1232 DC *pdc;
1233 POINTL SafePoint;
1234 SIZE Size;
1235 PSIZEL pszlViewportExt;
1236
1237 if (!Point)
1238 {
1239 EngSetLastError(ERROR_INVALID_PARAMETER);
1240 return FALSE;
1241 }
1242
1243 pdc = DC_LockDc(hDC);
1244 if (!pdc)
1245 {
1246 EngSetLastError(ERROR_INVALID_HANDLE);
1247 return FALSE;
1248 }
1249
1250 switch (iPoint)
1251 {
1252 case GdiGetViewPortExt:
1253 pszlViewportExt = DC_pszlViewportExt(pdc);
1254 SafePoint.x = pszlViewportExt->cx;
1255 SafePoint.y = pszlViewportExt->cy;
1256 break;
1257
1258 case GdiGetWindowExt:
1259 SafePoint.x = pdc->pdcattr->szlWindowExt.cx;
1260 SafePoint.y = pdc->pdcattr->szlWindowExt.cy;
1261 break;
1262
1263 case GdiGetViewPortOrg:
1264 SafePoint = pdc->pdcattr->ptlViewportOrg;
1265 break;
1266
1267 case GdiGetWindowOrg:
1268 SafePoint = pdc->pdcattr->ptlWindowOrg;
1269 break;
1270
1271 case GdiGetDCOrg:
1272 SafePoint = pdc->ptlDCOrig;
1273 break;
1274
1275 case GdiGetAspectRatioFilter:
1276 DC_vGetAspectRatioFilter(pdc, &Size);
1277 SafePoint.x = Size.cx;
1278 SafePoint.y = Size.cy;
1279 break;
1280
1281 default:
1282 EngSetLastError(ERROR_INVALID_PARAMETER);
1283 Ret = FALSE;
1284 break;
1285 }
1286
1287 if (Ret)
1288 {
1289 _SEH2_TRY
1290 {
1291 ProbeForWrite(Point, sizeof(POINT), 1);
1292 *Point = SafePoint;
1293 }
1294 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1295 {
1296 Ret = FALSE;
1297 }
1298 _SEH2_END;
1299 }
1300
1301 DC_UnlockDc(pdc);
1302 return Ret;
1303 }
1304
1305 /* EOF */