[PRINTING]
[reactos.git] / reactos / win32ss / gdi / gdi32 / wine / enhmetafile.c
1 /*
2 * Enhanced metafile functions
3 * Copyright 1998 Douglas Ridgway
4 * 1999 Huw D M Davies
5 *
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.
10 *
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.
15 *
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
19 *
20 * NOTES:
21 *
22 * The enhanced format consists of the following elements:
23 *
24 * A header
25 * A table of handles to GDI objects
26 * An array of metafile records
27 * A private palette
28 *
29 *
30 * The standard format consists of a header and an array of metafile records.
31 *
32 */
33
34 #include "config.h"
35 #include "wine/port.h"
36
37 #include <stdarg.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <assert.h>
41 #include "windef.h"
42 #include "winbase.h"
43 #include "wingdi.h"
44 #include "winnls.h"
45 #include "winerror.h"
46 #include "gdi_private.h"
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
50
51 typedef struct
52 {
53 ENHMETAHEADER *emh;
54 BOOL on_disk; /* true if metafile is on disk */
55 } ENHMETAFILEOBJ;
56
57 static const struct emr_name {
58 DWORD type;
59 const char *name;
60 } emr_names[] = {
61 #define X(p) {p, #p}
62 X(EMR_HEADER),
63 X(EMR_POLYBEZIER),
64 X(EMR_POLYGON),
65 X(EMR_POLYLINE),
66 X(EMR_POLYBEZIERTO),
67 X(EMR_POLYLINETO),
68 X(EMR_POLYPOLYLINE),
69 X(EMR_POLYPOLYGON),
70 X(EMR_SETWINDOWEXTEX),
71 X(EMR_SETWINDOWORGEX),
72 X(EMR_SETVIEWPORTEXTEX),
73 X(EMR_SETVIEWPORTORGEX),
74 X(EMR_SETBRUSHORGEX),
75 X(EMR_EOF),
76 X(EMR_SETPIXELV),
77 X(EMR_SETMAPPERFLAGS),
78 X(EMR_SETMAPMODE),
79 X(EMR_SETBKMODE),
80 X(EMR_SETPOLYFILLMODE),
81 X(EMR_SETROP2),
82 X(EMR_SETSTRETCHBLTMODE),
83 X(EMR_SETTEXTALIGN),
84 X(EMR_SETCOLORADJUSTMENT),
85 X(EMR_SETTEXTCOLOR),
86 X(EMR_SETBKCOLOR),
87 X(EMR_OFFSETCLIPRGN),
88 X(EMR_MOVETOEX),
89 X(EMR_SETMETARGN),
90 X(EMR_EXCLUDECLIPRECT),
91 X(EMR_INTERSECTCLIPRECT),
92 X(EMR_SCALEVIEWPORTEXTEX),
93 X(EMR_SCALEWINDOWEXTEX),
94 X(EMR_SAVEDC),
95 X(EMR_RESTOREDC),
96 X(EMR_SETWORLDTRANSFORM),
97 X(EMR_MODIFYWORLDTRANSFORM),
98 X(EMR_SELECTOBJECT),
99 X(EMR_CREATEPEN),
100 X(EMR_CREATEBRUSHINDIRECT),
101 X(EMR_DELETEOBJECT),
102 X(EMR_ANGLEARC),
103 X(EMR_ELLIPSE),
104 X(EMR_RECTANGLE),
105 X(EMR_ROUNDRECT),
106 X(EMR_ARC),
107 X(EMR_CHORD),
108 X(EMR_PIE),
109 X(EMR_SELECTPALETTE),
110 X(EMR_CREATEPALETTE),
111 X(EMR_SETPALETTEENTRIES),
112 X(EMR_RESIZEPALETTE),
113 X(EMR_REALIZEPALETTE),
114 X(EMR_EXTFLOODFILL),
115 X(EMR_LINETO),
116 X(EMR_ARCTO),
117 X(EMR_POLYDRAW),
118 X(EMR_SETARCDIRECTION),
119 X(EMR_SETMITERLIMIT),
120 X(EMR_BEGINPATH),
121 X(EMR_ENDPATH),
122 X(EMR_CLOSEFIGURE),
123 X(EMR_FILLPATH),
124 X(EMR_STROKEANDFILLPATH),
125 X(EMR_STROKEPATH),
126 X(EMR_FLATTENPATH),
127 X(EMR_WIDENPATH),
128 X(EMR_SELECTCLIPPATH),
129 X(EMR_ABORTPATH),
130 X(EMR_GDICOMMENT),
131 X(EMR_FILLRGN),
132 X(EMR_FRAMERGN),
133 X(EMR_INVERTRGN),
134 X(EMR_PAINTRGN),
135 X(EMR_EXTSELECTCLIPRGN),
136 X(EMR_BITBLT),
137 X(EMR_STRETCHBLT),
138 X(EMR_MASKBLT),
139 X(EMR_PLGBLT),
140 X(EMR_SETDIBITSTODEVICE),
141 X(EMR_STRETCHDIBITS),
142 X(EMR_EXTCREATEFONTINDIRECTW),
143 X(EMR_EXTTEXTOUTA),
144 X(EMR_EXTTEXTOUTW),
145 X(EMR_POLYBEZIER16),
146 X(EMR_POLYGON16),
147 X(EMR_POLYLINE16),
148 X(EMR_POLYBEZIERTO16),
149 X(EMR_POLYLINETO16),
150 X(EMR_POLYPOLYLINE16),
151 X(EMR_POLYPOLYGON16),
152 X(EMR_POLYDRAW16),
153 X(EMR_CREATEMONOBRUSH),
154 X(EMR_CREATEDIBPATTERNBRUSHPT),
155 X(EMR_EXTCREATEPEN),
156 X(EMR_POLYTEXTOUTA),
157 X(EMR_POLYTEXTOUTW),
158 X(EMR_SETICMMODE),
159 X(EMR_CREATECOLORSPACE),
160 X(EMR_SETCOLORSPACE),
161 X(EMR_DELETECOLORSPACE),
162 X(EMR_GLSRECORD),
163 X(EMR_GLSBOUNDEDRECORD),
164 X(EMR_PIXELFORMAT),
165 X(EMR_DRAWESCAPE),
166 X(EMR_EXTESCAPE),
167 X(EMR_STARTDOC),
168 X(EMR_SMALLTEXTOUT),
169 X(EMR_FORCEUFIMAPPING),
170 X(EMR_NAMEDESCAPE),
171 X(EMR_COLORCORRECTPALETTE),
172 X(EMR_SETICMPROFILEA),
173 X(EMR_SETICMPROFILEW),
174 X(EMR_ALPHABLEND),
175 X(EMR_SETLAYOUT),
176 X(EMR_TRANSPARENTBLT),
177 X(EMR_RESERVED_117),
178 X(EMR_GRADIENTFILL),
179 X(EMR_SETLINKEDUFI),
180 X(EMR_SETTEXTJUSTIFICATION),
181 X(EMR_COLORMATCHTOTARGETW),
182 X(EMR_CREATECOLORSPACEW)
183 #undef X
184 };
185
186 /****************************************************************************
187 * get_emr_name
188 */
189 static const char *get_emr_name(DWORD type)
190 {
191 unsigned int i;
192 for(i = 0; i < sizeof(emr_names) / sizeof(emr_names[0]); i++)
193 if(type == emr_names[i].type) return emr_names[i].name;
194 TRACE("Unknown record type %d\n", type);
195 return NULL;
196 }
197
198 /***********************************************************************
199 * is_dib_monochrome
200 *
201 * Returns whether a DIB can be converted to a monochrome DDB.
202 *
203 * A DIB can be converted if its color table contains only black and
204 * white. Black must be the first color in the color table.
205 *
206 * Note : If the first color in the color table is white followed by
207 * black, we can't convert it to a monochrome DDB with
208 * SetDIBits, because black and white would be inverted.
209 */
210 static inline BOOL is_dib_monochrome( const BITMAPINFO* info )
211 {
212 if (info->bmiHeader.biBitCount != 1) return FALSE;
213
214 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
215 {
216 const RGBTRIPLE *rgb = ((const BITMAPCOREINFO *) info)->bmciColors;
217
218 /* Check if the first color is black */
219 if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
220 {
221 rgb++;
222 /* Check if the second color is white */
223 return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
224 && (rgb->rgbtBlue == 0xff));
225 }
226 else return FALSE;
227 }
228 else /* assume BITMAPINFOHEADER */
229 {
230 const RGBQUAD *rgb = info->bmiColors;
231
232 /* Check if the first color is black */
233 if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
234 (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
235 {
236 rgb++;
237
238 /* Check if the second color is white */
239 return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
240 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
241 }
242 else return FALSE;
243 }
244 }
245
246 /****************************************************************************
247 * EMF_Create_HENHMETAFILE
248 */
249 HENHMETAFILE EMF_Create_HENHMETAFILE(ENHMETAHEADER *emh, BOOL on_disk )
250 {
251 HENHMETAFILE hmf;
252 ENHMETAFILEOBJ *metaObj;
253
254 if (emh->iType != EMR_HEADER)
255 {
256 SetLastError(ERROR_INVALID_DATA);
257 return 0;
258 }
259 if (emh->dSignature != ENHMETA_SIGNATURE ||
260 (emh->nBytes & 3)) /* refuse to load unaligned EMF as Windows does */
261 {
262 WARN("Invalid emf header type 0x%08x sig 0x%08x.\n",
263 emh->iType, emh->dSignature);
264 return 0;
265 }
266
267 if (!(metaObj = HeapAlloc( GetProcessHeap(), 0, sizeof(*metaObj) ))) return 0;
268
269 metaObj->emh = emh;
270 metaObj->on_disk = on_disk;
271
272 if (!(hmf = alloc_gdi_handle( metaObj, OBJ_ENHMETAFILE, NULL )))
273 HeapFree( GetProcessHeap(), 0, metaObj );
274 return hmf;
275 }
276
277 /****************************************************************************
278 * EMF_Delete_HENHMETAFILE
279 */
280 static BOOL EMF_Delete_HENHMETAFILE( HENHMETAFILE hmf )
281 {
282 ENHMETAFILEOBJ *metaObj = free_gdi_handle( hmf );
283
284 if(!metaObj) return FALSE;
285
286 if(metaObj->on_disk)
287 UnmapViewOfFile( metaObj->emh );
288 else
289 HeapFree( GetProcessHeap(), 0, metaObj->emh );
290 return HeapFree( GetProcessHeap(), 0, metaObj );
291 }
292
293 /******************************************************************
294 * EMF_GetEnhMetaHeader
295 *
296 * Returns ptr to ENHMETAHEADER associated with HENHMETAFILE
297 */
298 static ENHMETAHEADER *EMF_GetEnhMetaHeader( HENHMETAFILE hmf )
299 {
300 ENHMETAHEADER *ret = NULL;
301 ENHMETAFILEOBJ *metaObj = GDI_GetObjPtr( hmf, OBJ_ENHMETAFILE );
302 TRACE("hmf %p -> enhmetaObj %p\n", hmf, metaObj);
303 if (metaObj)
304 {
305 ret = metaObj->emh;
306 GDI_ReleaseObj( hmf );
307 }
308 return ret;
309 }
310
311 /*****************************************************************************
312 * EMF_GetEnhMetaFile
313 *
314 */
315 static HENHMETAFILE EMF_GetEnhMetaFile( HANDLE hFile )
316 {
317 ENHMETAHEADER *emh;
318 HANDLE hMapping;
319 HENHMETAFILE hemf;
320
321 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
322 emh = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
323 CloseHandle( hMapping );
324
325 if (!emh) return 0;
326
327 hemf = EMF_Create_HENHMETAFILE( emh, TRUE );
328 if (!hemf)
329 UnmapViewOfFile( emh );
330 return hemf;
331 }
332
333
334 /*****************************************************************************
335 * GetEnhMetaFileA (GDI32.@)
336 *
337 *
338 */
339 HENHMETAFILE WINAPI GetEnhMetaFileA(
340 LPCSTR lpszMetaFile /* [in] filename of enhanced metafile */
341 )
342 {
343 HENHMETAFILE hmf;
344 HANDLE hFile;
345
346 hFile = CreateFileA(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
347 OPEN_EXISTING, 0, 0);
348 if (hFile == INVALID_HANDLE_VALUE) {
349 WARN("could not open %s\n", lpszMetaFile);
350 return 0;
351 }
352 hmf = EMF_GetEnhMetaFile( hFile );
353 CloseHandle( hFile );
354 return hmf;
355 }
356
357 /*****************************************************************************
358 * GetEnhMetaFileW (GDI32.@)
359 */
360 HENHMETAFILE WINAPI GetEnhMetaFileW(
361 LPCWSTR lpszMetaFile) /* [in] filename of enhanced metafile */
362 {
363 HENHMETAFILE hmf;
364 HANDLE hFile;
365
366 hFile = CreateFileW(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
367 OPEN_EXISTING, 0, 0);
368 if (hFile == INVALID_HANDLE_VALUE) {
369 WARN("could not open %s\n", debugstr_w(lpszMetaFile));
370 return 0;
371 }
372 hmf = EMF_GetEnhMetaFile( hFile );
373 CloseHandle( hFile );
374 return hmf;
375 }
376
377 /*****************************************************************************
378 * GetEnhMetaFileHeader (GDI32.@)
379 *
380 * Retrieves the record containing the header for the specified
381 * enhanced-format metafile.
382 *
383 * RETURNS
384 * If buf is NULL, returns the size of buffer required.
385 * Otherwise, copy up to bufsize bytes of enhanced metafile header into
386 * buf.
387 */
388 UINT WINAPI GetEnhMetaFileHeader(
389 HENHMETAFILE hmf, /* [in] enhanced metafile */
390 UINT bufsize, /* [in] size of buffer */
391 LPENHMETAHEADER buf /* [out] buffer */
392 )
393 {
394 LPENHMETAHEADER emh;
395 UINT size;
396
397 emh = EMF_GetEnhMetaHeader(hmf);
398 if(!emh) return FALSE;
399 size = emh->nSize;
400 if (!buf) return size;
401 size = min(size, bufsize);
402 memmove(buf, emh, size);
403 return size;
404 }
405
406
407 /*****************************************************************************
408 * GetEnhMetaFileDescriptionA (GDI32.@)
409 *
410 * See GetEnhMetaFileDescriptionW.
411 */
412 UINT WINAPI GetEnhMetaFileDescriptionA(
413 HENHMETAFILE hmf, /* [in] enhanced metafile */
414 UINT size, /* [in] size of buf */
415 LPSTR buf /* [out] buffer to receive description */
416 )
417 {
418 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
419 DWORD len;
420 WCHAR *descrW;
421
422 if(!emh) return FALSE;
423 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
424 descrW = (WCHAR *) ((char *) emh + emh->offDescription);
425 len = WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, NULL, 0, NULL, NULL );
426
427 if (!buf || !size ) return len;
428
429 len = min( size, len );
430 WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, buf, len, NULL, NULL );
431 return len;
432 }
433
434 /*****************************************************************************
435 * GetEnhMetaFileDescriptionW (GDI32.@)
436 *
437 * Copies the description string of an enhanced metafile into a buffer
438 * _buf_.
439 *
440 * RETURNS
441 * If _buf_ is NULL, returns size of _buf_ required. Otherwise, returns
442 * number of characters copied.
443 */
444 UINT WINAPI GetEnhMetaFileDescriptionW(
445 HENHMETAFILE hmf, /* [in] enhanced metafile */
446 UINT size, /* [in] size of buf */
447 LPWSTR buf /* [out] buffer to receive description */
448 )
449 {
450 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
451
452 if(!emh) return FALSE;
453 if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
454 if (!buf || !size ) return emh->nDescription;
455
456 memmove(buf, (char *) emh + emh->offDescription, min(size,emh->nDescription)*sizeof(WCHAR));
457 return min(size, emh->nDescription);
458 }
459
460 /****************************************************************************
461 * SetEnhMetaFileBits (GDI32.@)
462 *
463 * Creates an enhanced metafile by copying _bufsize_ bytes from _buf_.
464 */
465 HENHMETAFILE WINAPI SetEnhMetaFileBits(UINT bufsize, const BYTE *buf)
466 {
467 ENHMETAHEADER *emh = HeapAlloc( GetProcessHeap(), 0, bufsize );
468 HENHMETAFILE hmf;
469 memmove(emh, buf, bufsize);
470 hmf = EMF_Create_HENHMETAFILE( emh, FALSE );
471 if (!hmf)
472 HeapFree( GetProcessHeap(), 0, emh );
473 return hmf;
474 }
475
476 /*****************************************************************************
477 * GetEnhMetaFileBits (GDI32.@)
478 *
479 */
480 UINT WINAPI GetEnhMetaFileBits(
481 HENHMETAFILE hmf,
482 UINT bufsize,
483 LPBYTE buf
484 )
485 {
486 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader( hmf );
487 UINT size;
488
489 if(!emh) return 0;
490
491 size = emh->nBytes;
492 if( buf == NULL ) return size;
493
494 size = min( size, bufsize );
495 memmove(buf, emh, size);
496 return size;
497 }
498
499 typedef struct EMF_dc_state
500 {
501 INT mode;
502 XFORM world_transform;
503 INT wndOrgX;
504 INT wndOrgY;
505 INT wndExtX;
506 INT wndExtY;
507 INT vportOrgX;
508 INT vportOrgY;
509 INT vportExtX;
510 INT vportExtY;
511 struct EMF_dc_state *next;
512 } EMF_dc_state;
513
514 typedef struct enum_emh_data
515 {
516 XFORM init_transform;
517 EMF_dc_state state;
518 INT save_level;
519 EMF_dc_state *saved_state;
520 } enum_emh_data;
521
522 #define ENUM_GET_PRIVATE_DATA(ht) \
523 ((enum_emh_data*)(((unsigned char*)(ht))-sizeof (enum_emh_data)))
524
525 #define WIDTH(rect) ( (rect).right - (rect).left )
526 #define HEIGHT(rect) ( (rect).bottom - (rect).top )
527
528 #define IS_WIN9X() (GetVersion()&0x80000000)
529
530 static void EMF_Update_MF_Xform(HDC hdc, const enum_emh_data *info)
531 {
532 XFORM mapping_mode_trans, final_trans;
533 double scaleX, scaleY;
534
535 scaleX = (double)info->state.vportExtX / (double)info->state.wndExtX;
536 scaleY = (double)info->state.vportExtY / (double)info->state.wndExtY;
537 mapping_mode_trans.eM11 = scaleX;
538 mapping_mode_trans.eM12 = 0.0;
539 mapping_mode_trans.eM21 = 0.0;
540 mapping_mode_trans.eM22 = scaleY;
541 mapping_mode_trans.eDx = (double)info->state.vportOrgX - scaleX * (double)info->state.wndOrgX;
542 mapping_mode_trans.eDy = (double)info->state.vportOrgY - scaleY * (double)info->state.wndOrgY;
543
544 CombineTransform(&final_trans, &info->state.world_transform, &mapping_mode_trans);
545 CombineTransform(&final_trans, &final_trans, &info->init_transform);
546
547 if (!SetWorldTransform(hdc, &final_trans))
548 {
549 __debugbreak();
550 SetWorldTransform(hdc, &final_trans);
551 ERR("World transform failed!\n");
552 }
553 }
554
555 static void EMF_RestoreDC( enum_emh_data *info, INT level )
556 {
557 if (abs(level) > info->save_level || level == 0) return;
558
559 if (level < 0) level = info->save_level + level + 1;
560
561 while (info->save_level >= level)
562 {
563 EMF_dc_state *state = info->saved_state;
564 info->saved_state = state->next;
565 state->next = NULL;
566 if (--info->save_level < level)
567 info->state = *state;
568 HeapFree( GetProcessHeap(), 0, state );
569 }
570 }
571
572 static void EMF_SaveDC( enum_emh_data *info )
573 {
574 EMF_dc_state *state = HeapAlloc( GetProcessHeap(), 0, sizeof(*state));
575 if (state)
576 {
577 *state = info->state;
578 state->next = info->saved_state;
579 info->saved_state = state;
580 info->save_level++;
581 TRACE("save_level %d\n", info->save_level);
582 }
583 }
584
585 static void EMF_SetMapMode(HDC hdc, enum_emh_data *info)
586 {
587 INT horzSize = GetDeviceCaps( hdc, HORZSIZE );
588 INT vertSize = GetDeviceCaps( hdc, VERTSIZE );
589 INT horzRes = GetDeviceCaps( hdc, HORZRES );
590 INT vertRes = GetDeviceCaps( hdc, VERTRES );
591
592 TRACE("%d\n", info->state.mode);
593
594 switch(info->state.mode)
595 {
596 case MM_TEXT:
597 info->state.wndExtX = 1;
598 info->state.wndExtY = 1;
599 info->state.vportExtX = 1;
600 info->state.vportExtY = 1;
601 break;
602 case MM_LOMETRIC:
603 case MM_ISOTROPIC:
604 info->state.wndExtX = horzSize * 10;
605 info->state.wndExtY = vertSize * 10;
606 info->state.vportExtX = horzRes;
607 info->state.vportExtY = -vertRes;
608 break;
609 case MM_HIMETRIC:
610 info->state.wndExtX = horzSize * 100;
611 info->state.wndExtY = vertSize * 100;
612 info->state.vportExtX = horzRes;
613 info->state.vportExtY = -vertRes;
614 break;
615 case MM_LOENGLISH:
616 info->state.wndExtX = MulDiv(1000, horzSize, 254);
617 info->state.wndExtY = MulDiv(1000, vertSize, 254);
618 info->state.vportExtX = horzRes;
619 info->state.vportExtY = -vertRes;
620 break;
621 case MM_HIENGLISH:
622 info->state.wndExtX = MulDiv(10000, horzSize, 254);
623 info->state.wndExtY = MulDiv(10000, vertSize, 254);
624 info->state.vportExtX = horzRes;
625 info->state.vportExtY = -vertRes;
626 break;
627 case MM_TWIPS:
628 info->state.wndExtX = MulDiv(14400, horzSize, 254);
629 info->state.wndExtY = MulDiv(14400, vertSize, 254);
630 info->state.vportExtX = horzRes;
631 info->state.vportExtY = -vertRes;
632 break;
633 case MM_ANISOTROPIC:
634 break;
635 default:
636 return;
637 }
638 }
639
640 /***********************************************************************
641 * EMF_FixIsotropic
642 *
643 * Fix viewport extensions for isotropic mode.
644 */
645
646 static void EMF_FixIsotropic(HDC hdc, enum_emh_data *info)
647 {
648 double xdim = fabs((double)info->state.vportExtX * GetDeviceCaps( hdc, HORZSIZE ) /
649 (GetDeviceCaps( hdc, HORZRES ) * info->state.wndExtX));
650 double ydim = fabs((double)info->state.vportExtY * GetDeviceCaps( hdc, VERTSIZE ) /
651 (GetDeviceCaps( hdc, VERTRES ) * info->state.wndExtY));
652
653 if (xdim > ydim)
654 {
655 INT mincx = (info->state.vportExtX >= 0) ? 1 : -1;
656 info->state.vportExtX = floor(info->state.vportExtX * ydim / xdim + 0.5);
657 if (!info->state.vportExtX) info->state.vportExtX = mincx;
658 }
659 else
660 {
661 INT mincy = (info->state.vportExtY >= 0) ? 1 : -1;
662 info->state.vportExtY = floor(info->state.vportExtY * xdim / ydim + 0.5);
663 if (!info->state.vportExtY) info->state.vportExtY = mincy;
664 }
665 }
666
667 /*****************************************************************************
668 * emr_produces_output
669 *
670 * Returns TRUE if the record type writes something to the dc. Used by
671 * PlayEnhMetaFileRecord to determine whether it needs to update the
672 * dc's xform when in win9x mode.
673 *
674 * FIXME: need to test which records should be here.
675 */
676 static BOOL emr_produces_output(int type)
677 {
678 switch(type) {
679 case EMR_POLYBEZIER:
680 case EMR_POLYGON:
681 case EMR_POLYLINE:
682 case EMR_POLYBEZIERTO:
683 case EMR_POLYLINETO:
684 case EMR_POLYPOLYLINE:
685 case EMR_POLYPOLYGON:
686 case EMR_SETPIXELV:
687 case EMR_MOVETOEX:
688 case EMR_EXCLUDECLIPRECT:
689 case EMR_INTERSECTCLIPRECT:
690 case EMR_SELECTOBJECT:
691 case EMR_ANGLEARC:
692 case EMR_ELLIPSE:
693 case EMR_RECTANGLE:
694 case EMR_ROUNDRECT:
695 case EMR_ARC:
696 case EMR_CHORD:
697 case EMR_PIE:
698 case EMR_EXTFLOODFILL:
699 case EMR_LINETO:
700 case EMR_ARCTO:
701 case EMR_POLYDRAW:
702 case EMR_GDICOMMENT:
703 case EMR_FILLRGN:
704 case EMR_FRAMERGN:
705 case EMR_INVERTRGN:
706 case EMR_PAINTRGN:
707 case EMR_BITBLT:
708 case EMR_STRETCHBLT:
709 case EMR_MASKBLT:
710 case EMR_PLGBLT:
711 case EMR_SETDIBITSTODEVICE:
712 case EMR_STRETCHDIBITS:
713 case EMR_EXTTEXTOUTA:
714 case EMR_EXTTEXTOUTW:
715 case EMR_POLYBEZIER16:
716 case EMR_POLYGON16:
717 case EMR_POLYLINE16:
718 case EMR_POLYBEZIERTO16:
719 case EMR_POLYLINETO16:
720 case EMR_POLYPOLYLINE16:
721 case EMR_POLYPOLYGON16:
722 case EMR_POLYDRAW16:
723 case EMR_POLYTEXTOUTA:
724 case EMR_POLYTEXTOUTW:
725 case EMR_SMALLTEXTOUT:
726 case EMR_ALPHABLEND:
727 case EMR_TRANSPARENTBLT:
728 return TRUE;
729 default:
730 return FALSE;
731 }
732 }
733
734
735 /*****************************************************************************
736 * PlayEnhMetaFileRecord (GDI32.@)
737 *
738 * Render a single enhanced metafile record in the device context hdc.
739 *
740 * RETURNS
741 * TRUE (non zero) on success, FALSE on error.
742 * BUGS
743 * Many unimplemented records.
744 * No error handling on record play failures (ie checking return codes)
745 *
746 * NOTES
747 * WinNT actually updates the current world transform in this function
748 * whereas Win9x does not.
749 */
750 BOOL WINAPI PlayEnhMetaFileRecord(
751 HDC hdc, /* [in] device context in which to render EMF record */
752 LPHANDLETABLE handletable, /* [in] array of handles to be used in rendering record */
753 const ENHMETARECORD *mr, /* [in] EMF record to render */
754 UINT handles /* [in] size of handle array */
755 )
756 {
757 int type;
758 RECT tmprc;
759 enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
760
761 TRACE("hdc = %p, handletable = %p, record = %p, numHandles = %d\n",
762 hdc, handletable, mr, handles);
763 if (!mr) return FALSE;
764
765 type = mr->iType;
766
767 TRACE("record %s\n", get_emr_name(type));
768 switch(type)
769 {
770 case EMR_HEADER:
771 break;
772 case EMR_EOF:
773 break;
774 case EMR_GDICOMMENT:
775 {
776 const EMRGDICOMMENT *lpGdiComment = (const EMRGDICOMMENT *)mr;
777 /* In an enhanced metafile, there can be both public and private GDI comments */
778 GdiComment( hdc, lpGdiComment->cbData, lpGdiComment->Data );
779 break;
780 }
781 case EMR_SETMAPMODE:
782 {
783 const EMRSETMAPMODE *pSetMapMode = (const EMRSETMAPMODE *)mr;
784
785 if (info->state.mode == pSetMapMode->iMode &&
786 (info->state.mode == MM_ISOTROPIC || info->state.mode == MM_ANISOTROPIC))
787 break;
788 info->state.mode = pSetMapMode->iMode;
789 EMF_SetMapMode(hdc, info);
790 break;
791 }
792 case EMR_SETBKMODE:
793 {
794 const EMRSETBKMODE *pSetBkMode = (const EMRSETBKMODE *)mr;
795 SetBkMode(hdc, pSetBkMode->iMode);
796 break;
797 }
798 case EMR_SETBKCOLOR:
799 {
800 const EMRSETBKCOLOR *pSetBkColor = (const EMRSETBKCOLOR *)mr;
801 SetBkColor(hdc, pSetBkColor->crColor);
802 break;
803 }
804 case EMR_SETPOLYFILLMODE:
805 {
806 const EMRSETPOLYFILLMODE *pSetPolyFillMode = (const EMRSETPOLYFILLMODE *)mr;
807 SetPolyFillMode(hdc, pSetPolyFillMode->iMode);
808 break;
809 }
810 case EMR_SETROP2:
811 {
812 const EMRSETROP2 *pSetROP2 = (const EMRSETROP2 *)mr;
813 SetROP2(hdc, pSetROP2->iMode);
814 break;
815 }
816 case EMR_SETSTRETCHBLTMODE:
817 {
818 const EMRSETSTRETCHBLTMODE *pSetStretchBltMode = (const EMRSETSTRETCHBLTMODE *)mr;
819 SetStretchBltMode(hdc, pSetStretchBltMode->iMode);
820 break;
821 }
822 case EMR_SETTEXTALIGN:
823 {
824 const EMRSETTEXTALIGN *pSetTextAlign = (const EMRSETTEXTALIGN *)mr;
825 SetTextAlign(hdc, pSetTextAlign->iMode);
826 break;
827 }
828 case EMR_SETTEXTCOLOR:
829 {
830 const EMRSETTEXTCOLOR *pSetTextColor = (const EMRSETTEXTCOLOR *)mr;
831 SetTextColor(hdc, pSetTextColor->crColor);
832 break;
833 }
834 case EMR_SAVEDC:
835 {
836 if (SaveDC( hdc ))
837 EMF_SaveDC( info );
838 break;
839 }
840 case EMR_RESTOREDC:
841 {
842 const EMRRESTOREDC *pRestoreDC = (const EMRRESTOREDC *)mr;
843 TRACE("EMR_RESTORE: %d\n", pRestoreDC->iRelative);
844 if (RestoreDC( hdc, pRestoreDC->iRelative ))
845 EMF_RestoreDC( info, pRestoreDC->iRelative );
846 break;
847 }
848 case EMR_INTERSECTCLIPRECT:
849 {
850 const EMRINTERSECTCLIPRECT *pClipRect = (const EMRINTERSECTCLIPRECT *)mr;
851 TRACE("EMR_INTERSECTCLIPRECT: rect %d,%d - %d, %d\n",
852 pClipRect->rclClip.left, pClipRect->rclClip.top,
853 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
854 IntersectClipRect(hdc, pClipRect->rclClip.left, pClipRect->rclClip.top,
855 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
856 break;
857 }
858 case EMR_SELECTOBJECT:
859 {
860 const EMRSELECTOBJECT *pSelectObject = (const EMRSELECTOBJECT *)mr;
861 if( pSelectObject->ihObject & 0x80000000 ) {
862 /* High order bit is set - it's a stock object
863 * Strip the high bit to get the index.
864 * See MSDN article Q142319
865 */
866 SelectObject( hdc, GetStockObject( pSelectObject->ihObject &
867 0x7fffffff ) );
868 } else {
869 /* High order bit wasn't set - not a stock object
870 */
871 SelectObject( hdc,
872 (handletable->objectHandle)[pSelectObject->ihObject] );
873 }
874 break;
875 }
876 case EMR_DELETEOBJECT:
877 {
878 const EMRDELETEOBJECT *pDeleteObject = (const EMRDELETEOBJECT *)mr;
879 DeleteObject( (handletable->objectHandle)[pDeleteObject->ihObject]);
880 (handletable->objectHandle)[pDeleteObject->ihObject] = 0;
881 break;
882 }
883 case EMR_SETWINDOWORGEX:
884 {
885 const EMRSETWINDOWORGEX *pSetWindowOrgEx = (const EMRSETWINDOWORGEX *)mr;
886
887 info->state.wndOrgX = pSetWindowOrgEx->ptlOrigin.x;
888 info->state.wndOrgY = pSetWindowOrgEx->ptlOrigin.y;
889
890 TRACE("SetWindowOrgEx: %d,%d\n", info->state.wndOrgX, info->state.wndOrgY);
891 break;
892 }
893 case EMR_SETWINDOWEXTEX:
894 {
895 const EMRSETWINDOWEXTEX *pSetWindowExtEx = (const EMRSETWINDOWEXTEX *)mr;
896
897 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
898 break;
899 info->state.wndExtX = pSetWindowExtEx->szlExtent.cx;
900 info->state.wndExtY = pSetWindowExtEx->szlExtent.cy;
901 if (info->state.mode == MM_ISOTROPIC)
902 EMF_FixIsotropic(hdc, info);
903
904 TRACE("SetWindowExtEx: %d,%d\n",info->state.wndExtX, info->state.wndExtY);
905 break;
906 }
907 case EMR_SETVIEWPORTORGEX:
908 {
909 const EMRSETVIEWPORTORGEX *pSetViewportOrgEx = (const EMRSETVIEWPORTORGEX *)mr;
910
911 info->state.vportOrgX = pSetViewportOrgEx->ptlOrigin.x;
912 info->state.vportOrgY = pSetViewportOrgEx->ptlOrigin.y;
913 TRACE("SetViewportOrgEx: %d,%d\n", info->state.vportOrgX, info->state.vportOrgY);
914 break;
915 }
916 case EMR_SETVIEWPORTEXTEX:
917 {
918 const EMRSETVIEWPORTEXTEX *pSetViewportExtEx = (const EMRSETVIEWPORTEXTEX *)mr;
919
920 if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
921 break;
922 info->state.vportExtX = pSetViewportExtEx->szlExtent.cx;
923 info->state.vportExtY = pSetViewportExtEx->szlExtent.cy;
924 if (info->state.mode == MM_ISOTROPIC)
925 EMF_FixIsotropic(hdc, info);
926 TRACE("SetViewportExtEx: %d,%d\n", info->state.vportExtX, info->state.vportExtY);
927 break;
928 }
929 case EMR_CREATEPEN:
930 {
931 const EMRCREATEPEN *pCreatePen = (const EMRCREATEPEN *)mr;
932 (handletable->objectHandle)[pCreatePen->ihPen] =
933 CreatePenIndirect(&pCreatePen->lopn);
934 break;
935 }
936 case EMR_EXTCREATEPEN:
937 {
938 const EMREXTCREATEPEN *pPen = (const EMREXTCREATEPEN *)mr;
939 LOGBRUSH lb;
940 lb.lbStyle = pPen->elp.elpBrushStyle;
941 lb.lbColor = pPen->elp.elpColor;
942 lb.lbHatch = pPen->elp.elpHatch;
943
944 if(pPen->offBmi || pPen->offBits)
945 FIXME("EMR_EXTCREATEPEN: Need to copy brush bitmap\n");
946
947 (handletable->objectHandle)[pPen->ihPen] =
948 ExtCreatePen(pPen->elp.elpPenStyle, pPen->elp.elpWidth, &lb,
949 pPen->elp.elpNumEntries, pPen->elp.elpNumEntries ? pPen->elp.elpStyleEntry : NULL);
950 break;
951 }
952 case EMR_CREATEBRUSHINDIRECT:
953 {
954 const EMRCREATEBRUSHINDIRECT *pBrush = (const EMRCREATEBRUSHINDIRECT *)mr;
955 LOGBRUSH brush;
956 brush.lbStyle = pBrush->lb.lbStyle;
957 brush.lbColor = pBrush->lb.lbColor;
958 brush.lbHatch = pBrush->lb.lbHatch;
959 (handletable->objectHandle)[pBrush->ihBrush] = CreateBrushIndirect(&brush);
960 break;
961 }
962 case EMR_EXTCREATEFONTINDIRECTW:
963 {
964 const EMREXTCREATEFONTINDIRECTW *pFont = (const EMREXTCREATEFONTINDIRECTW *)mr;
965 (handletable->objectHandle)[pFont->ihFont] =
966 CreateFontIndirectW(&pFont->elfw.elfLogFont);
967 break;
968 }
969 case EMR_MOVETOEX:
970 {
971 const EMRMOVETOEX *pMoveToEx = (const EMRMOVETOEX *)mr;
972 MoveToEx(hdc, pMoveToEx->ptl.x, pMoveToEx->ptl.y, NULL);
973 break;
974 }
975 case EMR_LINETO:
976 {
977 const EMRLINETO *pLineTo = (const EMRLINETO *)mr;
978 LineTo(hdc, pLineTo->ptl.x, pLineTo->ptl.y);
979 break;
980 }
981 case EMR_RECTANGLE:
982 {
983 const EMRRECTANGLE *pRect = (const EMRRECTANGLE *)mr;
984 Rectangle(hdc, pRect->rclBox.left, pRect->rclBox.top,
985 pRect->rclBox.right, pRect->rclBox.bottom);
986 break;
987 }
988 case EMR_ELLIPSE:
989 {
990 const EMRELLIPSE *pEllipse = (const EMRELLIPSE *)mr;
991 Ellipse(hdc, pEllipse->rclBox.left, pEllipse->rclBox.top,
992 pEllipse->rclBox.right, pEllipse->rclBox.bottom);
993 break;
994 }
995 case EMR_POLYGON16:
996 {
997 const EMRPOLYGON16 *pPoly = (const EMRPOLYGON16 *)mr;
998 /* Shouldn't use Polygon16 since pPoly->cpts is DWORD */
999 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1000 pPoly->cpts * sizeof(POINT) );
1001 DWORD i;
1002 for(i = 0; i < pPoly->cpts; i++)
1003 {
1004 pts[i].x = pPoly->apts[i].x;
1005 pts[i].y = pPoly->apts[i].y;
1006 }
1007 Polygon(hdc, pts, pPoly->cpts);
1008 HeapFree( GetProcessHeap(), 0, pts );
1009 break;
1010 }
1011 case EMR_POLYLINE16:
1012 {
1013 const EMRPOLYLINE16 *pPoly = (const EMRPOLYLINE16 *)mr;
1014 /* Shouldn't use Polyline16 since pPoly->cpts is DWORD */
1015 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1016 pPoly->cpts * sizeof(POINT) );
1017 DWORD i;
1018 for(i = 0; i < pPoly->cpts; i++)
1019 {
1020 pts[i].x = pPoly->apts[i].x;
1021 pts[i].y = pPoly->apts[i].y;
1022 }
1023 Polyline(hdc, pts, pPoly->cpts);
1024 HeapFree( GetProcessHeap(), 0, pts );
1025 break;
1026 }
1027 case EMR_POLYLINETO16:
1028 {
1029 const EMRPOLYLINETO16 *pPoly = (const EMRPOLYLINETO16 *)mr;
1030 /* Shouldn't use PolylineTo16 since pPoly->cpts is DWORD */
1031 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1032 pPoly->cpts * sizeof(POINT) );
1033 DWORD i;
1034 for(i = 0; i < pPoly->cpts; i++)
1035 {
1036 pts[i].x = pPoly->apts[i].x;
1037 pts[i].y = pPoly->apts[i].y;
1038 }
1039 PolylineTo(hdc, pts, pPoly->cpts);
1040 HeapFree( GetProcessHeap(), 0, pts );
1041 break;
1042 }
1043 case EMR_POLYBEZIER16:
1044 {
1045 const EMRPOLYBEZIER16 *pPoly = (const EMRPOLYBEZIER16 *)mr;
1046 /* Shouldn't use PolyBezier16 since pPoly->cpts is DWORD */
1047 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1048 pPoly->cpts * sizeof(POINT) );
1049 DWORD i;
1050 for(i = 0; i < pPoly->cpts; i++)
1051 {
1052 pts[i].x = pPoly->apts[i].x;
1053 pts[i].y = pPoly->apts[i].y;
1054 }
1055 PolyBezier(hdc, pts, pPoly->cpts);
1056 HeapFree( GetProcessHeap(), 0, pts );
1057 break;
1058 }
1059 case EMR_POLYBEZIERTO16:
1060 {
1061 const EMRPOLYBEZIERTO16 *pPoly = (const EMRPOLYBEZIERTO16 *)mr;
1062 /* Shouldn't use PolyBezierTo16 since pPoly->cpts is DWORD */
1063 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
1064 pPoly->cpts * sizeof(POINT) );
1065 DWORD i;
1066 for(i = 0; i < pPoly->cpts; i++)
1067 {
1068 pts[i].x = pPoly->apts[i].x;
1069 pts[i].y = pPoly->apts[i].y;
1070 }
1071 PolyBezierTo(hdc, pts, pPoly->cpts);
1072 HeapFree( GetProcessHeap(), 0, pts );
1073 break;
1074 }
1075 case EMR_POLYPOLYGON16:
1076 {
1077 const EMRPOLYPOLYGON16 *pPolyPoly = (const EMRPOLYPOLYGON16 *)mr;
1078 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1079 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1080
1081 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1082 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1083 DWORD i;
1084 for(i = 0; i < pPolyPoly->cpts; i++)
1085 {
1086 pt[i].x = pts[i].x;
1087 pt[i].y = pts[i].y;
1088 }
1089 PolyPolygon(hdc, pt, (const INT*)pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1090 HeapFree( GetProcessHeap(), 0, pt );
1091 break;
1092 }
1093 case EMR_POLYPOLYLINE16:
1094 {
1095 const EMRPOLYPOLYLINE16 *pPolyPoly = (const EMRPOLYPOLYLINE16 *)mr;
1096 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
1097 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1098
1099 const POINTS *pts = (const POINTS *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
1100 POINT *pt = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1101 DWORD i;
1102 for(i = 0; i < pPolyPoly->cpts; i++)
1103 {
1104 pt[i].x = pts[i].x;
1105 pt[i].y = pts[i].y;
1106 }
1107 PolyPolyline(hdc, pt, pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
1108 HeapFree( GetProcessHeap(), 0, pt );
1109 break;
1110 }
1111
1112 case EMR_STRETCHDIBITS:
1113 {
1114 const EMRSTRETCHDIBITS *pStretchDIBits = (const EMRSTRETCHDIBITS *)mr;
1115
1116 StretchDIBits(hdc,
1117 pStretchDIBits->xDest,
1118 pStretchDIBits->yDest,
1119 pStretchDIBits->cxDest,
1120 pStretchDIBits->cyDest,
1121 pStretchDIBits->xSrc,
1122 pStretchDIBits->ySrc,
1123 pStretchDIBits->cxSrc,
1124 pStretchDIBits->cySrc,
1125 (const BYTE *)mr + pStretchDIBits->offBitsSrc,
1126 (const BITMAPINFO *)((const BYTE *)mr + pStretchDIBits->offBmiSrc),
1127 pStretchDIBits->iUsageSrc,
1128 pStretchDIBits->dwRop);
1129 break;
1130 }
1131
1132 case EMR_EXTTEXTOUTA:
1133 {
1134 const EMREXTTEXTOUTA *pExtTextOutA = (const EMREXTTEXTOUTA *)mr;
1135 RECT rc;
1136 const INT *dx = NULL;
1137 int old_mode;
1138
1139 rc.left = pExtTextOutA->emrtext.rcl.left;
1140 rc.top = pExtTextOutA->emrtext.rcl.top;
1141 rc.right = pExtTextOutA->emrtext.rcl.right;
1142 rc.bottom = pExtTextOutA->emrtext.rcl.bottom;
1143 TRACE("EMR_EXTTEXTOUTA: x,y = %d, %d. rect = %d, %d - %d, %d. flags %08x\n",
1144 pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1145 rc.left, rc.top, rc.right, rc.bottom, pExtTextOutA->emrtext.fOptions);
1146
1147 old_mode = SetGraphicsMode(hdc, pExtTextOutA->iGraphicsMode);
1148 /* Reselect the font back into the dc so that the transformation
1149 gets updated. */
1150 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1151
1152 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1153 * These files can be enumerated and played under Win98 just
1154 * fine, but at least Win2k chokes on them.
1155 */
1156 if (pExtTextOutA->emrtext.offDx)
1157 dx = (const INT *)((const BYTE *)mr + pExtTextOutA->emrtext.offDx);
1158
1159 ExtTextOutA(hdc, pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
1160 pExtTextOutA->emrtext.fOptions, &rc,
1161 (LPCSTR)((const BYTE *)mr + pExtTextOutA->emrtext.offString), pExtTextOutA->emrtext.nChars,
1162 dx);
1163
1164 SetGraphicsMode(hdc, old_mode);
1165 break;
1166 }
1167
1168 case EMR_EXTTEXTOUTW:
1169 {
1170 const EMREXTTEXTOUTW *pExtTextOutW = (const EMREXTTEXTOUTW *)mr;
1171 RECT rc;
1172 const INT *dx = NULL;
1173 int old_mode;
1174
1175 rc.left = pExtTextOutW->emrtext.rcl.left;
1176 rc.top = pExtTextOutW->emrtext.rcl.top;
1177 rc.right = pExtTextOutW->emrtext.rcl.right;
1178 rc.bottom = pExtTextOutW->emrtext.rcl.bottom;
1179 TRACE("EMR_EXTTEXTOUTW: x,y = %d, %d. rect = %d, %d - %d, %d. flags %08x\n",
1180 pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1181 rc.left, rc.top, rc.right, rc.bottom, pExtTextOutW->emrtext.fOptions);
1182
1183 old_mode = SetGraphicsMode(hdc, pExtTextOutW->iGraphicsMode);
1184 /* Reselect the font back into the dc so that the transformation
1185 gets updated. */
1186 SelectObject(hdc, GetCurrentObject(hdc, OBJ_FONT));
1187
1188 /* Linux version of pstoedit produces EMFs with offDx set to 0.
1189 * These files can be enumerated and played under Win98 just
1190 * fine, but at least Win2k chokes on them.
1191 */
1192 if (pExtTextOutW->emrtext.offDx)
1193 dx = (const INT *)((const BYTE *)mr + pExtTextOutW->emrtext.offDx);
1194
1195 ExtTextOutW(hdc, pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
1196 pExtTextOutW->emrtext.fOptions, &rc,
1197 (LPCWSTR)((const BYTE *)mr + pExtTextOutW->emrtext.offString), pExtTextOutW->emrtext.nChars,
1198 dx);
1199
1200 SetGraphicsMode(hdc, old_mode);
1201 break;
1202 }
1203
1204 case EMR_CREATEPALETTE:
1205 {
1206 const EMRCREATEPALETTE *lpCreatePal = (const EMRCREATEPALETTE *)mr;
1207
1208 (handletable->objectHandle)[ lpCreatePal->ihPal ] =
1209 CreatePalette( &lpCreatePal->lgpl );
1210
1211 break;
1212 }
1213
1214 case EMR_SELECTPALETTE:
1215 {
1216 const EMRSELECTPALETTE *lpSelectPal = (const EMRSELECTPALETTE *)mr;
1217
1218 if( lpSelectPal->ihPal & 0x80000000 ) {
1219 SelectPalette( hdc, GetStockObject(lpSelectPal->ihPal & 0x7fffffff), TRUE);
1220 } else {
1221 SelectPalette( hdc, (handletable->objectHandle)[lpSelectPal->ihPal], TRUE);
1222 }
1223 break;
1224 }
1225
1226 case EMR_REALIZEPALETTE:
1227 {
1228 RealizePalette( hdc );
1229 break;
1230 }
1231
1232 case EMR_EXTSELECTCLIPRGN:
1233 {
1234 const EMREXTSELECTCLIPRGN *lpRgn = (const EMREXTSELECTCLIPRGN *)mr;
1235 const RGNDATA *pRgnData = (const RGNDATA *)lpRgn->RgnData;
1236 DWORD dwSize = sizeof(RGNDATAHEADER) + pRgnData->rdh.nCount * sizeof(RECT);
1237 HRGN hRgn = 0;
1238
1239 if (mr->nSize >= sizeof(*lpRgn) + sizeof(RGNDATAHEADER))
1240 hRgn = ExtCreateRegion( &info->init_transform, dwSize, pRgnData );
1241
1242 ExtSelectClipRgn(hdc, hRgn, (INT)(lpRgn->iMode));
1243 /* ExtSelectClipRgn created a copy of the region */
1244 DeleteObject(hRgn);
1245 break;
1246 }
1247
1248 case EMR_SETMETARGN:
1249 {
1250 SetMetaRgn( hdc );
1251 break;
1252 }
1253
1254 case EMR_SETWORLDTRANSFORM:
1255 {
1256 const EMRSETWORLDTRANSFORM *lpXfrm = (const EMRSETWORLDTRANSFORM *)mr;
1257 info->state.world_transform = lpXfrm->xform;
1258 break;
1259 }
1260
1261 case EMR_POLYBEZIER:
1262 {
1263 const EMRPOLYBEZIER *lpPolyBez = (const EMRPOLYBEZIER *)mr;
1264 PolyBezier(hdc, (const POINT*)lpPolyBez->aptl, (UINT)lpPolyBez->cptl);
1265 break;
1266 }
1267
1268 case EMR_POLYGON:
1269 {
1270 const EMRPOLYGON *lpPoly = (const EMRPOLYGON *)mr;
1271 Polygon( hdc, (const POINT*)lpPoly->aptl, (UINT)lpPoly->cptl );
1272 break;
1273 }
1274
1275 case EMR_POLYLINE:
1276 {
1277 const EMRPOLYLINE *lpPolyLine = (const EMRPOLYLINE *)mr;
1278 Polyline(hdc, (const POINT*)lpPolyLine->aptl, (UINT)lpPolyLine->cptl);
1279 break;
1280 }
1281
1282 case EMR_POLYBEZIERTO:
1283 {
1284 const EMRPOLYBEZIERTO *lpPolyBezierTo = (const EMRPOLYBEZIERTO *)mr;
1285 PolyBezierTo( hdc, (const POINT*)lpPolyBezierTo->aptl,
1286 (UINT)lpPolyBezierTo->cptl );
1287 break;
1288 }
1289
1290 case EMR_POLYLINETO:
1291 {
1292 const EMRPOLYLINETO *lpPolyLineTo = (const EMRPOLYLINETO *)mr;
1293 PolylineTo( hdc, (const POINT*)lpPolyLineTo->aptl,
1294 (UINT)lpPolyLineTo->cptl );
1295 break;
1296 }
1297
1298 case EMR_POLYPOLYLINE:
1299 {
1300 const EMRPOLYPOLYLINE *pPolyPolyline = (const EMRPOLYPOLYLINE *)mr;
1301 /* NB Points at pPolyPolyline->aPolyCounts + pPolyPolyline->nPolys */
1302
1303 PolyPolyline(hdc, (const POINT*)(pPolyPolyline->aPolyCounts +
1304 pPolyPolyline->nPolys),
1305 pPolyPolyline->aPolyCounts,
1306 pPolyPolyline->nPolys );
1307
1308 break;
1309 }
1310
1311 case EMR_POLYPOLYGON:
1312 {
1313 const EMRPOLYPOLYGON *pPolyPolygon = (const EMRPOLYPOLYGON *)mr;
1314
1315 /* NB Points at pPolyPolygon->aPolyCounts + pPolyPolygon->nPolys */
1316
1317 PolyPolygon(hdc, (const POINT*)(pPolyPolygon->aPolyCounts +
1318 pPolyPolygon->nPolys),
1319 (const INT*)pPolyPolygon->aPolyCounts, pPolyPolygon->nPolys );
1320 break;
1321 }
1322
1323 case EMR_SETBRUSHORGEX:
1324 {
1325 const EMRSETBRUSHORGEX *lpSetBrushOrgEx = (const EMRSETBRUSHORGEX *)mr;
1326
1327 SetBrushOrgEx( hdc,
1328 (INT)lpSetBrushOrgEx->ptlOrigin.x,
1329 (INT)lpSetBrushOrgEx->ptlOrigin.y,
1330 NULL );
1331
1332 break;
1333 }
1334
1335 case EMR_SETPIXELV:
1336 {
1337 const EMRSETPIXELV *lpSetPixelV = (const EMRSETPIXELV *)mr;
1338
1339 SetPixelV( hdc,
1340 (INT)lpSetPixelV->ptlPixel.x,
1341 (INT)lpSetPixelV->ptlPixel.y,
1342 lpSetPixelV->crColor );
1343
1344 break;
1345 }
1346
1347 case EMR_SETMAPPERFLAGS:
1348 {
1349 const EMRSETMAPPERFLAGS *lpSetMapperFlags = (const EMRSETMAPPERFLAGS *)mr;
1350
1351 SetMapperFlags( hdc, lpSetMapperFlags->dwFlags );
1352
1353 break;
1354 }
1355
1356 case EMR_SETCOLORADJUSTMENT:
1357 {
1358 const EMRSETCOLORADJUSTMENT *lpSetColorAdjust = (const EMRSETCOLORADJUSTMENT *)mr;
1359
1360 SetColorAdjustment( hdc, &lpSetColorAdjust->ColorAdjustment );
1361
1362 break;
1363 }
1364
1365 case EMR_OFFSETCLIPRGN:
1366 {
1367 const EMROFFSETCLIPRGN *lpOffsetClipRgn = (const EMROFFSETCLIPRGN *)mr;
1368
1369 OffsetClipRgn( hdc,
1370 (INT)lpOffsetClipRgn->ptlOffset.x,
1371 (INT)lpOffsetClipRgn->ptlOffset.y );
1372 FIXME("OffsetClipRgn\n");
1373
1374 break;
1375 }
1376
1377 case EMR_EXCLUDECLIPRECT:
1378 {
1379 const EMREXCLUDECLIPRECT *lpExcludeClipRect = (const EMREXCLUDECLIPRECT *)mr;
1380
1381 ExcludeClipRect( hdc,
1382 lpExcludeClipRect->rclClip.left,
1383 lpExcludeClipRect->rclClip.top,
1384 lpExcludeClipRect->rclClip.right,
1385 lpExcludeClipRect->rclClip.bottom );
1386 FIXME("ExcludeClipRect\n");
1387
1388 break;
1389 }
1390
1391 case EMR_SCALEVIEWPORTEXTEX:
1392 {
1393 const EMRSCALEVIEWPORTEXTEX *lpScaleViewportExtEx = (const EMRSCALEVIEWPORTEXTEX *)mr;
1394
1395 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1396 break;
1397 if (!lpScaleViewportExtEx->xNum || !lpScaleViewportExtEx->xDenom ||
1398 !lpScaleViewportExtEx->yNum || !lpScaleViewportExtEx->yDenom)
1399 break;
1400 info->state.vportExtX = MulDiv(info->state.vportExtX, lpScaleViewportExtEx->xNum,
1401 lpScaleViewportExtEx->xDenom);
1402 info->state.vportExtY = MulDiv(info->state.vportExtY, lpScaleViewportExtEx->yNum,
1403 lpScaleViewportExtEx->yDenom);
1404 if (info->state.vportExtX == 0) info->state.vportExtX = 1;
1405 if (info->state.vportExtY == 0) info->state.vportExtY = 1;
1406 if (info->state.mode == MM_ISOTROPIC)
1407 EMF_FixIsotropic(hdc, info);
1408
1409 TRACE("EMRSCALEVIEWPORTEXTEX %d/%d %d/%d\n",
1410 lpScaleViewportExtEx->xNum,lpScaleViewportExtEx->xDenom,
1411 lpScaleViewportExtEx->yNum,lpScaleViewportExtEx->yDenom);
1412
1413 break;
1414 }
1415
1416 case EMR_SCALEWINDOWEXTEX:
1417 {
1418 const EMRSCALEWINDOWEXTEX *lpScaleWindowExtEx = (const EMRSCALEWINDOWEXTEX *)mr;
1419
1420 if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1421 break;
1422 if (!lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->xDenom ||
1423 !lpScaleWindowExtEx->yNum || !lpScaleWindowExtEx->yDenom)
1424 break;
1425 info->state.wndExtX = MulDiv(info->state.wndExtX, lpScaleWindowExtEx->xNum,
1426 lpScaleWindowExtEx->xDenom);
1427 info->state.wndExtY = MulDiv(info->state.wndExtY, lpScaleWindowExtEx->yNum,
1428 lpScaleWindowExtEx->yDenom);
1429 if (info->state.wndExtX == 0) info->state.wndExtX = 1;
1430 if (info->state.wndExtY == 0) info->state.wndExtY = 1;
1431 if (info->state.mode == MM_ISOTROPIC)
1432 EMF_FixIsotropic(hdc, info);
1433
1434 TRACE("EMRSCALEWINDOWEXTEX %d/%d %d/%d\n",
1435 lpScaleWindowExtEx->xNum,lpScaleWindowExtEx->xDenom,
1436 lpScaleWindowExtEx->yNum,lpScaleWindowExtEx->yDenom);
1437
1438 break;
1439 }
1440
1441 case EMR_MODIFYWORLDTRANSFORM:
1442 {
1443 const EMRMODIFYWORLDTRANSFORM *lpModifyWorldTrans = (const EMRMODIFYWORLDTRANSFORM *)mr;
1444
1445 switch(lpModifyWorldTrans->iMode) {
1446 case MWT_IDENTITY:
1447 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
1448 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
1449 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
1450 break;
1451 case MWT_LEFTMULTIPLY:
1452 CombineTransform(&info->state.world_transform, &lpModifyWorldTrans->xform,
1453 &info->state.world_transform);
1454 break;
1455 case MWT_RIGHTMULTIPLY:
1456 CombineTransform(&info->state.world_transform, &info->state.world_transform,
1457 &lpModifyWorldTrans->xform);
1458 break;
1459 default:
1460 FIXME("Unknown imode %d\n", lpModifyWorldTrans->iMode);
1461 break;
1462 }
1463 break;
1464 }
1465
1466 case EMR_ANGLEARC:
1467 {
1468 const EMRANGLEARC *lpAngleArc = (const EMRANGLEARC *)mr;
1469
1470 AngleArc( hdc,
1471 (INT)lpAngleArc->ptlCenter.x, (INT)lpAngleArc->ptlCenter.y,
1472 lpAngleArc->nRadius, lpAngleArc->eStartAngle,
1473 lpAngleArc->eSweepAngle );
1474
1475 break;
1476 }
1477
1478 case EMR_ROUNDRECT:
1479 {
1480 const EMRROUNDRECT *lpRoundRect = (const EMRROUNDRECT *)mr;
1481
1482 RoundRect( hdc,
1483 lpRoundRect->rclBox.left,
1484 lpRoundRect->rclBox.top,
1485 lpRoundRect->rclBox.right,
1486 lpRoundRect->rclBox.bottom,
1487 lpRoundRect->szlCorner.cx,
1488 lpRoundRect->szlCorner.cy );
1489
1490 break;
1491 }
1492
1493 case EMR_ARC:
1494 {
1495 const EMRARC *lpArc = (const EMRARC *)mr;
1496
1497 Arc( hdc,
1498 (INT)lpArc->rclBox.left,
1499 (INT)lpArc->rclBox.top,
1500 (INT)lpArc->rclBox.right,
1501 (INT)lpArc->rclBox.bottom,
1502 (INT)lpArc->ptlStart.x,
1503 (INT)lpArc->ptlStart.y,
1504 (INT)lpArc->ptlEnd.x,
1505 (INT)lpArc->ptlEnd.y );
1506
1507 break;
1508 }
1509
1510 case EMR_CHORD:
1511 {
1512 const EMRCHORD *lpChord = (const EMRCHORD *)mr;
1513
1514 Chord( hdc,
1515 (INT)lpChord->rclBox.left,
1516 (INT)lpChord->rclBox.top,
1517 (INT)lpChord->rclBox.right,
1518 (INT)lpChord->rclBox.bottom,
1519 (INT)lpChord->ptlStart.x,
1520 (INT)lpChord->ptlStart.y,
1521 (INT)lpChord->ptlEnd.x,
1522 (INT)lpChord->ptlEnd.y );
1523
1524 break;
1525 }
1526
1527 case EMR_PIE:
1528 {
1529 const EMRPIE *lpPie = (const EMRPIE *)mr;
1530
1531 Pie( hdc,
1532 (INT)lpPie->rclBox.left,
1533 (INT)lpPie->rclBox.top,
1534 (INT)lpPie->rclBox.right,
1535 (INT)lpPie->rclBox.bottom,
1536 (INT)lpPie->ptlStart.x,
1537 (INT)lpPie->ptlStart.y,
1538 (INT)lpPie->ptlEnd.x,
1539 (INT)lpPie->ptlEnd.y );
1540
1541 break;
1542 }
1543
1544 case EMR_ARCTO:
1545 {
1546 const EMRARC *lpArcTo = (const EMRARC *)mr;
1547
1548 ArcTo( hdc,
1549 (INT)lpArcTo->rclBox.left,
1550 (INT)lpArcTo->rclBox.top,
1551 (INT)lpArcTo->rclBox.right,
1552 (INT)lpArcTo->rclBox.bottom,
1553 (INT)lpArcTo->ptlStart.x,
1554 (INT)lpArcTo->ptlStart.y,
1555 (INT)lpArcTo->ptlEnd.x,
1556 (INT)lpArcTo->ptlEnd.y );
1557
1558 break;
1559 }
1560
1561 case EMR_EXTFLOODFILL:
1562 {
1563 const EMREXTFLOODFILL *lpExtFloodFill = (const EMREXTFLOODFILL *)mr;
1564
1565 ExtFloodFill( hdc,
1566 (INT)lpExtFloodFill->ptlStart.x,
1567 (INT)lpExtFloodFill->ptlStart.y,
1568 lpExtFloodFill->crColor,
1569 (UINT)lpExtFloodFill->iMode );
1570
1571 break;
1572 }
1573
1574 case EMR_POLYDRAW:
1575 {
1576 const EMRPOLYDRAW *lpPolyDraw = (const EMRPOLYDRAW *)mr;
1577 PolyDraw( hdc,
1578 (const POINT*)lpPolyDraw->aptl,
1579 lpPolyDraw->abTypes,
1580 (INT)lpPolyDraw->cptl );
1581
1582 break;
1583 }
1584
1585 case EMR_SETARCDIRECTION:
1586 {
1587 const EMRSETARCDIRECTION *lpSetArcDirection = (const EMRSETARCDIRECTION *)mr;
1588 SetArcDirection( hdc, (INT)lpSetArcDirection->iArcDirection );
1589 break;
1590 }
1591
1592 case EMR_SETMITERLIMIT:
1593 {
1594 const EMRSETMITERLIMIT *lpSetMiterLimit = (const EMRSETMITERLIMIT *)mr;
1595 SetMiterLimit( hdc, lpSetMiterLimit->eMiterLimit, NULL );
1596 break;
1597 }
1598
1599 case EMR_BEGINPATH:
1600 {
1601 BeginPath( hdc );
1602 break;
1603 }
1604
1605 case EMR_ENDPATH:
1606 {
1607 EndPath( hdc );
1608 break;
1609 }
1610
1611 case EMR_CLOSEFIGURE:
1612 {
1613 CloseFigure( hdc );
1614 break;
1615 }
1616
1617 case EMR_FILLPATH:
1618 {
1619 /*const EMRFILLPATH lpFillPath = (const EMRFILLPATH *)mr;*/
1620 FillPath( hdc );
1621 break;
1622 }
1623
1624 case EMR_STROKEANDFILLPATH:
1625 {
1626 /*const EMRSTROKEANDFILLPATH lpStrokeAndFillPath = (const EMRSTROKEANDFILLPATH *)mr;*/
1627 StrokeAndFillPath( hdc );
1628 break;
1629 }
1630
1631 case EMR_STROKEPATH:
1632 {
1633 /*const EMRSTROKEPATH lpStrokePath = (const EMRSTROKEPATH *)mr;*/
1634 StrokePath( hdc );
1635 break;
1636 }
1637
1638 case EMR_FLATTENPATH:
1639 {
1640 FlattenPath( hdc );
1641 break;
1642 }
1643
1644 case EMR_WIDENPATH:
1645 {
1646 WidenPath( hdc );
1647 break;
1648 }
1649
1650 case EMR_SELECTCLIPPATH:
1651 {
1652 const EMRSELECTCLIPPATH *lpSelectClipPath = (const EMRSELECTCLIPPATH *)mr;
1653 SelectClipPath( hdc, (INT)lpSelectClipPath->iMode );
1654 break;
1655 }
1656
1657 case EMR_ABORTPATH:
1658 {
1659 AbortPath( hdc );
1660 break;
1661 }
1662
1663 case EMR_CREATECOLORSPACE:
1664 {
1665 PEMRCREATECOLORSPACE lpCreateColorSpace = (PEMRCREATECOLORSPACE)mr;
1666 (handletable->objectHandle)[lpCreateColorSpace->ihCS] =
1667 CreateColorSpaceA( &lpCreateColorSpace->lcs );
1668 break;
1669 }
1670
1671 case EMR_SETCOLORSPACE:
1672 {
1673 const EMRSETCOLORSPACE *lpSetColorSpace = (const EMRSETCOLORSPACE *)mr;
1674 SetColorSpace( hdc,
1675 (handletable->objectHandle)[lpSetColorSpace->ihCS] );
1676 break;
1677 }
1678
1679 case EMR_DELETECOLORSPACE:
1680 {
1681 const EMRDELETECOLORSPACE *lpDeleteColorSpace = (const EMRDELETECOLORSPACE *)mr;
1682 DeleteColorSpace( (handletable->objectHandle)[lpDeleteColorSpace->ihCS] );
1683 break;
1684 }
1685
1686 case EMR_SETICMMODE:
1687 {
1688 const EMRSETICMMODE *lpSetICMMode = (const EMRSETICMMODE *)mr;
1689 SetICMMode( hdc, (INT)lpSetICMMode->iMode );
1690 break;
1691 }
1692
1693 case EMR_PIXELFORMAT:
1694 {
1695 INT iPixelFormat;
1696 const EMRPIXELFORMAT *lpPixelFormat = (const EMRPIXELFORMAT *)mr;
1697
1698 iPixelFormat = ChoosePixelFormat( hdc, &lpPixelFormat->pfd );
1699 SetPixelFormat( hdc, iPixelFormat, &lpPixelFormat->pfd );
1700
1701 break;
1702 }
1703
1704 case EMR_SETPALETTEENTRIES:
1705 {
1706 const EMRSETPALETTEENTRIES *lpSetPaletteEntries = (const EMRSETPALETTEENTRIES *)mr;
1707
1708 SetPaletteEntries( (handletable->objectHandle)[lpSetPaletteEntries->ihPal],
1709 (UINT)lpSetPaletteEntries->iStart,
1710 (UINT)lpSetPaletteEntries->cEntries,
1711 lpSetPaletteEntries->aPalEntries );
1712
1713 break;
1714 }
1715
1716 case EMR_RESIZEPALETTE:
1717 {
1718 const EMRRESIZEPALETTE *lpResizePalette = (const EMRRESIZEPALETTE *)mr;
1719
1720 ResizePalette( (handletable->objectHandle)[lpResizePalette->ihPal],
1721 (UINT)lpResizePalette->cEntries );
1722
1723 break;
1724 }
1725
1726 case EMR_CREATEDIBPATTERNBRUSHPT:
1727 {
1728 const EMRCREATEDIBPATTERNBRUSHPT *lpCreate = (const EMRCREATEDIBPATTERNBRUSHPT *)mr;
1729 LPVOID lpPackedStruct;
1730
1731 /* Check that offsets and data are contained within the record
1732 * (including checking for wrap-arounds).
1733 */
1734 if ( lpCreate->offBmi + lpCreate->cbBmi > mr->nSize
1735 || lpCreate->offBits + lpCreate->cbBits > mr->nSize
1736 || lpCreate->offBmi + lpCreate->cbBmi < lpCreate->offBmi
1737 || lpCreate->offBits + lpCreate->cbBits < lpCreate->offBits )
1738 {
1739 ERR("Invalid EMR_CREATEDIBPATTERNBRUSHPT record\n");
1740 break;
1741 }
1742
1743 /* This is a BITMAPINFO struct followed directly by bitmap bits */
1744 lpPackedStruct = HeapAlloc( GetProcessHeap(), 0,
1745 lpCreate->cbBmi + lpCreate->cbBits );
1746 if(!lpPackedStruct)
1747 {
1748 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1749 break;
1750 }
1751
1752 /* Now pack this structure */
1753 memcpy( lpPackedStruct,
1754 ((const BYTE *)lpCreate) + lpCreate->offBmi,
1755 lpCreate->cbBmi );
1756 memcpy( ((BYTE*)lpPackedStruct) + lpCreate->cbBmi,
1757 ((const BYTE *)lpCreate) + lpCreate->offBits,
1758 lpCreate->cbBits );
1759
1760 (handletable->objectHandle)[lpCreate->ihBrush] =
1761 CreateDIBPatternBrushPt( lpPackedStruct,
1762 (UINT)lpCreate->iUsage );
1763
1764 HeapFree(GetProcessHeap(), 0, lpPackedStruct);
1765 break;
1766 }
1767
1768 case EMR_CREATEMONOBRUSH:
1769 {
1770 const EMRCREATEMONOBRUSH *pCreateMonoBrush = (const EMRCREATEMONOBRUSH *)mr;
1771 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pCreateMonoBrush->offBmi);
1772 HBITMAP hBmp;
1773
1774 /* Need to check if the bitmap is monochrome, and if the
1775 two colors are really black and white */
1776 if (pCreateMonoBrush->iUsage == DIB_PAL_MONO)
1777 {
1778 BITMAP bm;
1779
1780 /* Undocumented iUsage indicates a mono bitmap with no palette table,
1781 * aligned to 32 rather than 16 bits.
1782 */
1783 bm.bmType = 0;
1784 bm.bmWidth = pbi->bmiHeader.biWidth;
1785 bm.bmHeight = abs(pbi->bmiHeader.biHeight);
1786 bm.bmWidthBytes = 4 * ((pbi->bmiHeader.biWidth + 31) / 32);
1787 bm.bmPlanes = pbi->bmiHeader.biPlanes;
1788 bm.bmBitsPixel = pbi->bmiHeader.biBitCount;
1789 bm.bmBits = (BYTE *)mr + pCreateMonoBrush->offBits;
1790 hBmp = CreateBitmapIndirect(&bm);
1791 }
1792 else if (is_dib_monochrome(pbi))
1793 {
1794 /* Top-down DIBs have a negative height */
1795 LONG height = pbi->bmiHeader.biHeight;
1796
1797 hBmp = CreateBitmap(pbi->bmiHeader.biWidth, abs(height), 1, 1, NULL);
1798 SetDIBits(hdc, hBmp, 0, pbi->bmiHeader.biHeight,
1799 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1800 }
1801 else
1802 {
1803 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1804 (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1805 }
1806
1807 (handletable->objectHandle)[pCreateMonoBrush->ihBrush] = CreatePatternBrush(hBmp);
1808
1809 /* CreatePatternBrush created a copy of the bitmap */
1810 DeleteObject(hBmp);
1811 break;
1812 }
1813
1814 case EMR_BITBLT:
1815 {
1816 const EMRBITBLT *pBitBlt = (const EMRBITBLT *)mr;
1817
1818 if(pBitBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1819 PatBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1820 pBitBlt->dwRop);
1821 } else { /* BitBlt */
1822 HDC hdcSrc = CreateCompatibleDC(hdc);
1823 HBRUSH hBrush, hBrushOld;
1824 HBITMAP hBmp = 0, hBmpOld = 0;
1825 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pBitBlt->offBmiSrc);
1826
1827 SetWorldTransform(hdcSrc, &pBitBlt->xformSrc);
1828
1829 hBrush = CreateSolidBrush(pBitBlt->crBkColorSrc);
1830 hBrushOld = SelectObject(hdcSrc, hBrush);
1831 PatBlt(hdcSrc, pBitBlt->rclBounds.left, pBitBlt->rclBounds.top,
1832 pBitBlt->rclBounds.right - pBitBlt->rclBounds.left,
1833 pBitBlt->rclBounds.bottom - pBitBlt->rclBounds.top, PATCOPY);
1834 SelectObject(hdcSrc, hBrushOld);
1835 DeleteObject(hBrush);
1836
1837 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1838 (const BYTE *)mr + pBitBlt->offBitsSrc, pbi, pBitBlt->iUsageSrc);
1839 hBmpOld = SelectObject(hdcSrc, hBmp);
1840
1841 BitBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
1842 hdcSrc, pBitBlt->xSrc, pBitBlt->ySrc, pBitBlt->dwRop);
1843
1844 SelectObject(hdcSrc, hBmpOld);
1845 DeleteObject(hBmp);
1846 DeleteDC(hdcSrc);
1847 }
1848 break;
1849 }
1850
1851 case EMR_STRETCHBLT:
1852 {
1853 const EMRSTRETCHBLT *pStretchBlt = (const EMRSTRETCHBLT *)mr;
1854
1855 TRACE("EMR_STRETCHBLT: %d, %d %dx%d -> %d, %d %dx%d. rop %08x offBitsSrc %d\n",
1856 pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1857 pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1858 pStretchBlt->dwRop, pStretchBlt->offBitsSrc);
1859
1860 if(pStretchBlt->offBmiSrc == 0) { /* Record is a PatBlt */
1861 PatBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1862 pStretchBlt->dwRop);
1863 } else { /* StretchBlt */
1864 HDC hdcSrc = CreateCompatibleDC(hdc);
1865 HBRUSH hBrush, hBrushOld;
1866 HBITMAP hBmp = 0, hBmpOld = 0;
1867 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pStretchBlt->offBmiSrc);
1868
1869 SetWorldTransform(hdcSrc, &pStretchBlt->xformSrc);
1870
1871 hBrush = CreateSolidBrush(pStretchBlt->crBkColorSrc);
1872 hBrushOld = SelectObject(hdcSrc, hBrush);
1873 PatBlt(hdcSrc, pStretchBlt->rclBounds.left, pStretchBlt->rclBounds.top,
1874 pStretchBlt->rclBounds.right - pStretchBlt->rclBounds.left,
1875 pStretchBlt->rclBounds.bottom - pStretchBlt->rclBounds.top, PATCOPY);
1876 SelectObject(hdcSrc, hBrushOld);
1877 DeleteObject(hBrush);
1878
1879 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1880 (const BYTE *)mr + pStretchBlt->offBitsSrc, pbi, pStretchBlt->iUsageSrc);
1881 hBmpOld = SelectObject(hdcSrc, hBmp);
1882
1883 StretchBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
1884 hdcSrc, pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
1885 pStretchBlt->dwRop);
1886
1887 SelectObject(hdcSrc, hBmpOld);
1888 DeleteObject(hBmp);
1889 DeleteDC(hdcSrc);
1890 }
1891 break;
1892 }
1893
1894 case EMR_ALPHABLEND:
1895 {
1896 const EMRALPHABLEND *pAlphaBlend = (const EMRALPHABLEND *)mr;
1897
1898 TRACE("EMR_ALPHABLEND: %d, %d %dx%d -> %d, %d %dx%d. blendfn %08x offBitsSrc %d\n",
1899 pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1900 pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1901 pAlphaBlend->dwRop, pAlphaBlend->offBitsSrc);
1902
1903 if(pAlphaBlend->offBmiSrc == 0) {
1904 FIXME("EMR_ALPHABLEND: offBmiSrc == 0\n");
1905 } else {
1906 HDC hdcSrc = CreateCompatibleDC(hdc);
1907 HBITMAP hBmp = 0, hBmpOld = 0;
1908 const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pAlphaBlend->offBmiSrc);
1909 void *bits;
1910
1911 SetWorldTransform(hdcSrc, &pAlphaBlend->xformSrc);
1912
1913 hBmp = CreateDIBSection(hdc, pbi, pAlphaBlend->iUsageSrc, &bits, NULL, 0);
1914 memcpy(bits, (const BYTE *)mr + pAlphaBlend->offBitsSrc, pAlphaBlend->cbBitsSrc);
1915 hBmpOld = SelectObject(hdcSrc, hBmp);
1916
1917 GdiAlphaBlend(hdc, pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
1918 hdcSrc, pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
1919 *(BLENDFUNCTION *)&pAlphaBlend->dwRop);
1920
1921 SelectObject(hdcSrc, hBmpOld);
1922 DeleteObject(hBmp);
1923 DeleteDC(hdcSrc);
1924 }
1925 break;
1926 }
1927
1928 case EMR_MASKBLT:
1929 {
1930 const EMRMASKBLT *pMaskBlt = (const EMRMASKBLT *)mr;
1931 HDC hdcSrc = CreateCompatibleDC(hdc);
1932 HBRUSH hBrush, hBrushOld;
1933 HBITMAP hBmp, hBmpOld, hBmpMask;
1934 const BITMAPINFO *pbi;
1935
1936 SetWorldTransform(hdcSrc, &pMaskBlt->xformSrc);
1937
1938 hBrush = CreateSolidBrush(pMaskBlt->crBkColorSrc);
1939 hBrushOld = SelectObject(hdcSrc, hBrush);
1940 PatBlt(hdcSrc, pMaskBlt->rclBounds.left, pMaskBlt->rclBounds.top,
1941 pMaskBlt->rclBounds.right - pMaskBlt->rclBounds.left,
1942 pMaskBlt->rclBounds.bottom - pMaskBlt->rclBounds.top, PATCOPY);
1943 SelectObject(hdcSrc, hBrushOld);
1944 DeleteObject(hBrush);
1945
1946 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiMask);
1947 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
1948 1, 1, NULL);
1949 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
1950 (const BYTE *)mr + pMaskBlt->offBitsMask, pbi, pMaskBlt->iUsageMask);
1951
1952 pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiSrc);
1953 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
1954 (const BYTE *)mr + pMaskBlt->offBitsSrc, pbi, pMaskBlt->iUsageSrc);
1955 hBmpOld = SelectObject(hdcSrc, hBmp);
1956 MaskBlt(hdc,
1957 pMaskBlt->xDest,
1958 pMaskBlt->yDest,
1959 pMaskBlt->cxDest,
1960 pMaskBlt->cyDest,
1961 hdcSrc,
1962 pMaskBlt->xSrc,
1963 pMaskBlt->ySrc,
1964 hBmpMask,
1965 pMaskBlt->xMask,
1966 pMaskBlt->yMask,
1967 pMaskBlt->dwRop);
1968 SelectObject(hdcSrc, hBmpOld);
1969 DeleteObject(hBmp);
1970 DeleteObject(hBmpMask);
1971 DeleteDC(hdcSrc);
1972 break;
1973 }
1974
1975 case EMR_PLGBLT:
1976 {
1977 const EMRPLGBLT *pPlgBlt = (const EMRPLGBLT *)mr;
1978 HDC hdcSrc = CreateCompatibleDC(hdc);
1979 HBRUSH hBrush, hBrushOld;
1980 HBITMAP hBmp, hBmpOld, hBmpMask;
1981 const BITMAPINFO *pbi;
1982 POINT pts[3];
1983
1984 SetWorldTransform(hdcSrc, &pPlgBlt->xformSrc);
1985
1986 pts[0].x = pPlgBlt->aptlDest[0].x; pts[0].y = pPlgBlt->aptlDest[0].y;
1987 pts[1].x = pPlgBlt->aptlDest[1].x; pts[1].y = pPlgBlt->aptlDest[1].y;
1988 pts[2].x = pPlgBlt->aptlDest[2].x; pts[2].y = pPlgBlt->aptlDest[2].y;
1989
1990 hBrush = CreateSolidBrush(pPlgBlt->crBkColorSrc);
1991 hBrushOld = SelectObject(hdcSrc, hBrush);
1992 PatBlt(hdcSrc, pPlgBlt->rclBounds.left, pPlgBlt->rclBounds.top,
1993 pPlgBlt->rclBounds.right - pPlgBlt->rclBounds.left,
1994 pPlgBlt->rclBounds.bottom - pPlgBlt->rclBounds.top, PATCOPY);
1995 SelectObject(hdcSrc, hBrushOld);
1996 DeleteObject(hBrush);
1997
1998 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiMask);
1999 hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
2000 1, 1, NULL);
2001 SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
2002 (const BYTE *)mr + pPlgBlt->offBitsMask, pbi, pPlgBlt->iUsageMask);
2003
2004 pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiSrc);
2005 hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
2006 (const BYTE *)mr + pPlgBlt->offBitsSrc, pbi, pPlgBlt->iUsageSrc);
2007 hBmpOld = SelectObject(hdcSrc, hBmp);
2008 PlgBlt(hdc,
2009 pts,
2010 hdcSrc,
2011 pPlgBlt->xSrc,
2012 pPlgBlt->ySrc,
2013 pPlgBlt->cxSrc,
2014 pPlgBlt->cySrc,
2015 hBmpMask,
2016 pPlgBlt->xMask,
2017 pPlgBlt->yMask);
2018 SelectObject(hdcSrc, hBmpOld);
2019 DeleteObject(hBmp);
2020 DeleteObject(hBmpMask);
2021 DeleteDC(hdcSrc);
2022 break;
2023 }
2024
2025 case EMR_SETDIBITSTODEVICE:
2026 {
2027 const EMRSETDIBITSTODEVICE *pSetDIBitsToDevice = (const EMRSETDIBITSTODEVICE *)mr;
2028
2029 SetDIBitsToDevice(hdc,
2030 pSetDIBitsToDevice->xDest,
2031 pSetDIBitsToDevice->yDest,
2032 pSetDIBitsToDevice->cxSrc,
2033 pSetDIBitsToDevice->cySrc,
2034 pSetDIBitsToDevice->xSrc,
2035 pSetDIBitsToDevice->ySrc,
2036 pSetDIBitsToDevice->iStartScan,
2037 pSetDIBitsToDevice->cScans,
2038 (const BYTE *)mr + pSetDIBitsToDevice->offBitsSrc,
2039 (const BITMAPINFO *)((const BYTE *)mr + pSetDIBitsToDevice->offBmiSrc),
2040 pSetDIBitsToDevice->iUsageSrc);
2041 break;
2042 }
2043
2044 case EMR_POLYTEXTOUTA:
2045 {
2046 const EMRPOLYTEXTOUTA *pPolyTextOutA = (const EMRPOLYTEXTOUTA *)mr;
2047 POLYTEXTA *polytextA = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutA->cStrings * sizeof(POLYTEXTA));
2048 LONG i;
2049 XFORM xform, xformOld;
2050 int gModeOld;
2051
2052 gModeOld = SetGraphicsMode(hdc, pPolyTextOutA->iGraphicsMode);
2053 GetWorldTransform(hdc, &xformOld);
2054
2055 xform.eM11 = pPolyTextOutA->exScale;
2056 xform.eM12 = 0.0;
2057 xform.eM21 = 0.0;
2058 xform.eM22 = pPolyTextOutA->eyScale;
2059 xform.eDx = 0.0;
2060 xform.eDy = 0.0;
2061 SetWorldTransform(hdc, &xform);
2062
2063 /* Set up POLYTEXTA structures */
2064 for(i = 0; i < pPolyTextOutA->cStrings; i++)
2065 {
2066 polytextA[i].x = pPolyTextOutA->aemrtext[i].ptlReference.x;
2067 polytextA[i].y = pPolyTextOutA->aemrtext[i].ptlReference.y;
2068 polytextA[i].n = pPolyTextOutA->aemrtext[i].nChars;
2069 polytextA[i].lpstr = (LPCSTR)((const BYTE *)mr + pPolyTextOutA->aemrtext[i].offString);
2070 polytextA[i].uiFlags = pPolyTextOutA->aemrtext[i].fOptions;
2071 polytextA[i].rcl.left = pPolyTextOutA->aemrtext[i].rcl.left;
2072 polytextA[i].rcl.right = pPolyTextOutA->aemrtext[i].rcl.right;
2073 polytextA[i].rcl.top = pPolyTextOutA->aemrtext[i].rcl.top;
2074 polytextA[i].rcl.bottom = pPolyTextOutA->aemrtext[i].rcl.bottom;
2075 polytextA[i].pdx = (int *)((BYTE *)mr + pPolyTextOutA->aemrtext[i].offDx);
2076 }
2077 PolyTextOutA(hdc, polytextA, pPolyTextOutA->cStrings);
2078 HeapFree(GetProcessHeap(), 0, polytextA);
2079
2080 SetWorldTransform(hdc, &xformOld);
2081 SetGraphicsMode(hdc, gModeOld);
2082 break;
2083 }
2084
2085 case EMR_POLYTEXTOUTW:
2086 {
2087 const EMRPOLYTEXTOUTW *pPolyTextOutW = (const EMRPOLYTEXTOUTW *)mr;
2088 POLYTEXTW *polytextW = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutW->cStrings * sizeof(POLYTEXTW));
2089 LONG i;
2090 XFORM xform, xformOld;
2091 int gModeOld;
2092
2093 gModeOld = SetGraphicsMode(hdc, pPolyTextOutW->iGraphicsMode);
2094 GetWorldTransform(hdc, &xformOld);
2095
2096 xform.eM11 = pPolyTextOutW->exScale;
2097 xform.eM12 = 0.0;
2098 xform.eM21 = 0.0;
2099 xform.eM22 = pPolyTextOutW->eyScale;
2100 xform.eDx = 0.0;
2101 xform.eDy = 0.0;
2102 SetWorldTransform(hdc, &xform);
2103
2104 /* Set up POLYTEXTW structures */
2105 for(i = 0; i < pPolyTextOutW->cStrings; i++)
2106 {
2107 polytextW[i].x = pPolyTextOutW->aemrtext[i].ptlReference.x;
2108 polytextW[i].y = pPolyTextOutW->aemrtext[i].ptlReference.y;
2109 polytextW[i].n = pPolyTextOutW->aemrtext[i].nChars;
2110 polytextW[i].lpstr = (LPCWSTR)((const BYTE *)mr + pPolyTextOutW->aemrtext[i].offString);
2111 polytextW[i].uiFlags = pPolyTextOutW->aemrtext[i].fOptions;
2112 polytextW[i].rcl.left = pPolyTextOutW->aemrtext[i].rcl.left;
2113 polytextW[i].rcl.right = pPolyTextOutW->aemrtext[i].rcl.right;
2114 polytextW[i].rcl.top = pPolyTextOutW->aemrtext[i].rcl.top;
2115 polytextW[i].rcl.bottom = pPolyTextOutW->aemrtext[i].rcl.bottom;
2116 polytextW[i].pdx = (int *)((BYTE *)mr + pPolyTextOutW->aemrtext[i].offDx);
2117 }
2118 PolyTextOutW(hdc, polytextW, pPolyTextOutW->cStrings);
2119 HeapFree(GetProcessHeap(), 0, polytextW);
2120
2121 SetWorldTransform(hdc, &xformOld);
2122 SetGraphicsMode(hdc, gModeOld);
2123 break;
2124 }
2125
2126 case EMR_FILLRGN:
2127 {
2128 const EMRFILLRGN *pFillRgn = (const EMRFILLRGN *)mr;
2129 HRGN hRgn = ExtCreateRegion(NULL, pFillRgn->cbRgnData, (const RGNDATA *)pFillRgn->RgnData);
2130 FillRgn(hdc,
2131 hRgn,
2132 (handletable->objectHandle)[pFillRgn->ihBrush]);
2133 DeleteObject(hRgn);
2134 break;
2135 }
2136
2137 case EMR_FRAMERGN:
2138 {
2139 const EMRFRAMERGN *pFrameRgn = (const EMRFRAMERGN *)mr;
2140 HRGN hRgn = ExtCreateRegion(NULL, pFrameRgn->cbRgnData, (const RGNDATA *)pFrameRgn->RgnData);
2141 FrameRgn(hdc,
2142 hRgn,
2143 (handletable->objectHandle)[pFrameRgn->ihBrush],
2144 pFrameRgn->szlStroke.cx,
2145 pFrameRgn->szlStroke.cy);
2146 DeleteObject(hRgn);
2147 break;
2148 }
2149
2150 case EMR_INVERTRGN:
2151 {
2152 const EMRINVERTRGN *pInvertRgn = (const EMRINVERTRGN *)mr;
2153 HRGN hRgn = ExtCreateRegion(NULL, pInvertRgn->cbRgnData, (const RGNDATA *)pInvertRgn->RgnData);
2154 InvertRgn(hdc, hRgn);
2155 DeleteObject(hRgn);
2156 break;
2157 }
2158
2159 case EMR_PAINTRGN:
2160 {
2161 const EMRPAINTRGN *pPaintRgn = (const EMRPAINTRGN *)mr;
2162 HRGN hRgn = ExtCreateRegion(NULL, pPaintRgn->cbRgnData, (const RGNDATA *)pPaintRgn->RgnData);
2163 PaintRgn(hdc, hRgn);
2164 DeleteObject(hRgn);
2165 break;
2166 }
2167
2168 case EMR_SETTEXTJUSTIFICATION:
2169 {
2170 const EMRSETTEXTJUSTIFICATION *pSetTextJust = (const EMRSETTEXTJUSTIFICATION *)mr;
2171 SetTextJustification(hdc, pSetTextJust->nBreakExtra, pSetTextJust->nBreakCount);
2172 break;
2173 }
2174
2175 case EMR_SETLAYOUT:
2176 {
2177 const EMRSETLAYOUT *pSetLayout = (const EMRSETLAYOUT *)mr;
2178 SetLayout(hdc, pSetLayout->iMode);
2179 break;
2180 }
2181
2182 case EMR_POLYDRAW16:
2183 case EMR_GLSRECORD:
2184 case EMR_GLSBOUNDEDRECORD:
2185 case EMR_DRAWESCAPE:
2186 case EMR_EXTESCAPE:
2187 case EMR_STARTDOC:
2188 case EMR_SMALLTEXTOUT:
2189 case EMR_FORCEUFIMAPPING:
2190 case EMR_NAMEDESCAPE:
2191 case EMR_COLORCORRECTPALETTE:
2192 case EMR_SETICMPROFILEA:
2193 case EMR_SETICMPROFILEW:
2194 case EMR_TRANSPARENTBLT:
2195 case EMR_GRADIENTFILL:
2196 case EMR_SETLINKEDUFI:
2197 case EMR_COLORMATCHTOTARGETW:
2198 case EMR_CREATECOLORSPACEW:
2199
2200 default:
2201 /* From docs: If PlayEnhMetaFileRecord doesn't recognize a
2202 record then ignore and return TRUE. */
2203 FIXME("type %d is unimplemented\n", type);
2204 break;
2205 }
2206 tmprc.left = tmprc.top = 0;
2207 tmprc.right = tmprc.bottom = 1000;
2208 LPtoDP(hdc, (POINT*)&tmprc, 2);
2209 TRACE("L:0,0 - 1000,1000 -> D:%d,%d - %d,%d\n", tmprc.left,
2210 tmprc.top, tmprc.right, tmprc.bottom);
2211
2212 return TRUE;
2213 }
2214
2215
2216 /*****************************************************************************
2217 *
2218 * EnumEnhMetaFile (GDI32.@)
2219 *
2220 * Walk an enhanced metafile, calling a user-specified function _EnhMetaFunc_
2221 * for each
2222 * record. Returns when either every record has been used or
2223 * when _EnhMetaFunc_ returns FALSE.
2224 *
2225 *
2226 * RETURNS
2227 * TRUE if every record is used, FALSE if any invocation of _EnhMetaFunc_
2228 * returns FALSE.
2229 *
2230 * BUGS
2231 * Ignores rect.
2232 *
2233 * NOTES
2234 * This function behaves differently in Win9x and WinNT.
2235 *
2236 * In WinNT, the DC's world transform is updated as the EMF changes
2237 * the Window/Viewport Extent and Origin or its world transform.
2238 * The actual Window/Viewport Extent and Origin are left untouched.
2239 *
2240 * In Win9x, the DC is left untouched, and PlayEnhMetaFileRecord
2241 * updates the scaling itself but only just before a record that
2242 * writes anything to the DC.
2243 *
2244 * I'm not sure where the data (enum_emh_data) is stored in either
2245 * version. For this implementation, it is stored before the handle
2246 * table, but it could be stored in the DC, in the EMF handle or in
2247 * TLS.
2248 * MJM 5 Oct 2002
2249 */
2250 BOOL WINAPI EnumEnhMetaFile(
2251 HDC hdc, /* [in] device context to pass to _EnhMetaFunc_ */
2252 HENHMETAFILE hmf, /* [in] EMF to walk */
2253 ENHMFENUMPROC callback, /* [in] callback function */
2254 LPVOID data, /* [in] optional data for callback function */
2255 const RECT *lpRect /* [in] bounding rectangle for rendered metafile */
2256 )
2257 {
2258 BOOL ret;
2259 ENHMETAHEADER *emh;
2260 ENHMETARECORD *emr;
2261 DWORD offset;
2262 UINT i;
2263 HANDLETABLE *ht;
2264 INT savedMode = 0;
2265 XFORM savedXform;
2266 HPEN hPen = NULL;
2267 HBRUSH hBrush = NULL;
2268 HFONT hFont = NULL;
2269 HRGN hRgn = NULL;
2270 enum_emh_data *info;
2271 SIZE vp_size, win_size;
2272 POINT vp_org, win_org;
2273 INT mapMode = MM_TEXT, old_align = 0, old_rop2 = 0, old_arcdir = 0, old_polyfill = 0, old_stretchblt = 0;
2274 COLORREF old_text_color = 0, old_bk_color = 0;
2275
2276 if(!lpRect && hdc)
2277 {
2278 SetLastError(ERROR_INVALID_PARAMETER);
2279 return FALSE;
2280 }
2281
2282 emh = EMF_GetEnhMetaHeader(hmf);
2283 if(!emh) {
2284 SetLastError(ERROR_INVALID_HANDLE);
2285 return FALSE;
2286 }
2287
2288 info = HeapAlloc( GetProcessHeap(), 0,
2289 sizeof (enum_emh_data) + sizeof(HANDLETABLE) * emh->nHandles );
2290 if(!info)
2291 {
2292 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2293 return FALSE;
2294 }
2295 info->state.mode = MM_TEXT;
2296 info->state.wndOrgX = 0;
2297 info->state.wndOrgY = 0;
2298 info->state.wndExtX = 1;
2299 info->state.wndExtY = 1;
2300 info->state.vportOrgX = 0;
2301 info->state.vportOrgY = 0;
2302 info->state.vportExtX = 1;
2303 info->state.vportExtY = 1;
2304 info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
2305 info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
2306 info->state.world_transform.eDx = info->state.world_transform.eDy = 0;
2307
2308 info->state.next = NULL;
2309 info->save_level = 0;
2310 info->saved_state = NULL;
2311
2312 ht = (HANDLETABLE*) &info[1];
2313 ht->objectHandle[0] = hmf;
2314 for(i = 1; i < emh->nHandles; i++)
2315 ht->objectHandle[i] = NULL;
2316
2317 if(hdc)
2318 {
2319 savedMode = SetGraphicsMode(hdc, GM_ADVANCED);
2320 GetWorldTransform(hdc, &savedXform);
2321 GetViewportExtEx(hdc, &vp_size);
2322 GetWindowExtEx(hdc, &win_size);
2323 GetViewportOrgEx(hdc, &vp_org);
2324 GetWindowOrgEx(hdc, &win_org);
2325 mapMode = GetMapMode(hdc);
2326
2327 /* save DC */
2328 hPen = GetCurrentObject(hdc, OBJ_PEN);
2329 hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
2330 hFont = GetCurrentObject(hdc, OBJ_FONT);
2331
2332 hRgn = CreateRectRgn(0, 0, 0, 0);
2333 if (!GetClipRgn(hdc, hRgn))
2334 {
2335 DeleteObject(hRgn);
2336 hRgn = 0;
2337 }
2338
2339 old_text_color = SetTextColor(hdc, RGB(0,0,0));
2340 old_bk_color = SetBkColor(hdc, RGB(0xff, 0xff, 0xff));
2341 old_align = SetTextAlign(hdc, 0);
2342 old_rop2 = SetROP2(hdc, R2_COPYPEN);
2343 old_arcdir = SetArcDirection(hdc, AD_COUNTERCLOCKWISE);
2344 old_polyfill = SetPolyFillMode(hdc, ALTERNATE);
2345 old_stretchblt = SetStretchBltMode(hdc, BLACKONWHITE);
2346
2347 if ( IS_WIN9X() )
2348 {
2349 /* Win95 leaves the vp/win ext/org info alone */
2350 info->init_transform.eM11 = 1.0;
2351 info->init_transform.eM12 = 0.0;
2352 info->init_transform.eM21 = 0.0;
2353 info->init_transform.eM22 = 1.0;
2354 info->init_transform.eDx = 0.0;
2355 info->init_transform.eDy = 0.0;
2356 }
2357 else
2358 {
2359 /* WinNT combines the vp/win ext/org info into a transform */
2360 double xscale, yscale;
2361 xscale = (double)vp_size.cx / (double)win_size.cx;
2362 yscale = (double)vp_size.cy / (double)win_size.cy;
2363 info->init_transform.eM11 = xscale;
2364 info->init_transform.eM12 = 0.0;
2365 info->init_transform.eM21 = 0.0;
2366 info->init_transform.eM22 = yscale;
2367 info->init_transform.eDx = (double)vp_org.x - xscale * (double)win_org.x;
2368 info->init_transform.eDy = (double)vp_org.y - yscale * (double)win_org.y;
2369
2370 CombineTransform(&info->init_transform, &savedXform, &info->init_transform);
2371 }
2372
2373 if ( lpRect && WIDTH(emh->rclFrame) && HEIGHT(emh->rclFrame) )
2374 {
2375 double xSrcPixSize, ySrcPixSize, xscale, yscale;
2376 XFORM xform;
2377
2378 TRACE("rect: %d,%d - %d,%d. rclFrame: %d,%d - %d,%d\n",
2379 lpRect->left, lpRect->top, lpRect->right, lpRect->bottom,
2380 emh->rclFrame.left, emh->rclFrame.top, emh->rclFrame.right,
2381 emh->rclFrame.bottom);
2382
2383 xSrcPixSize = (double) emh->szlMillimeters.cx / emh->szlDevice.cx;
2384 ySrcPixSize = (double) emh->szlMillimeters.cy / emh->szlDevice.cy;
2385 xscale = (double) WIDTH(*lpRect) * 100.0 /
2386 WIDTH(emh->rclFrame) * xSrcPixSize;
2387 yscale = (double) HEIGHT(*lpRect) * 100.0 /
2388 HEIGHT(emh->rclFrame) * ySrcPixSize;
2389 TRACE("xscale = %f, yscale = %f\n", xscale, yscale);
2390
2391 xform.eM11 = xscale;
2392 xform.eM12 = 0;
2393 xform.eM21 = 0;
2394 xform.eM22 = yscale;
2395 xform.eDx = (double) lpRect->left - (double) WIDTH(*lpRect) / WIDTH(emh->rclFrame) * emh->rclFrame.left;
2396 xform.eDy = (double) lpRect->top - (double) HEIGHT(*lpRect) / HEIGHT(emh->rclFrame) * emh->rclFrame.top;
2397
2398 CombineTransform(&info->init_transform, &xform, &info->init_transform);
2399 }
2400
2401 /* WinNT resets the current vp/win org/ext */
2402 if ( !IS_WIN9X() )
2403 {
2404 SetMapMode(hdc, MM_TEXT);
2405 SetWindowOrgEx(hdc, 0, 0, NULL);
2406 SetViewportOrgEx(hdc, 0, 0, NULL);
2407 EMF_Update_MF_Xform(hdc, info);
2408 }
2409 }
2410
2411 ret = TRUE;
2412 offset = 0;
2413 while(ret && offset < emh->nBytes)
2414 {
2415 emr = (ENHMETARECORD *)((char *)emh + offset);
2416
2417 /* In Win9x mode we update the xform if the record will produce output */
2418 if (hdc && IS_WIN9X() && emr_produces_output(emr->iType))
2419 EMF_Update_MF_Xform(hdc, info);
2420
2421 TRACE("Calling EnumFunc with record %s, size %d\n", get_emr_name(emr->iType), emr->nSize);
2422 ret = (*callback)(hdc, ht, emr, emh->nHandles, (LPARAM)data);
2423 offset += emr->nSize;
2424
2425 /* WinNT - update the transform (win9x updates when the next graphics
2426 output record is played). */
2427 if (hdc && !IS_WIN9X())
2428 EMF_Update_MF_Xform(hdc, info);
2429 }
2430
2431 if (hdc)
2432 {
2433 SetStretchBltMode(hdc, old_stretchblt);
2434 SetPolyFillMode(hdc, old_polyfill);
2435 SetArcDirection(hdc, old_arcdir);
2436 SetROP2(hdc, old_rop2);
2437 SetTextAlign(hdc, old_align);
2438 SetBkColor(hdc, old_bk_color);
2439 SetTextColor(hdc, old_text_color);
2440
2441 /* restore DC */
2442 SelectObject(hdc, hBrush);
2443 SelectObject(hdc, hPen);
2444 SelectObject(hdc, hFont);
2445 ExtSelectClipRgn(hdc, hRgn, RGN_COPY);
2446 DeleteObject(hRgn);
2447
2448 SetWorldTransform(hdc, &savedXform);
2449 if (savedMode)
2450 SetGraphicsMode(hdc, savedMode);
2451 SetMapMode(hdc, mapMode);
2452 SetWindowOrgEx(hdc, win_org.x, win_org.y, NULL);
2453 SetWindowExtEx(hdc, win_size.cx, win_size.cy, NULL);
2454 SetViewportOrgEx(hdc, vp_org.x, vp_org.y, NULL);
2455 SetViewportExtEx(hdc, vp_size.cx, vp_size.cy, NULL);
2456 }
2457
2458 for(i = 1; i < emh->nHandles; i++) /* Don't delete element 0 (hmf) */
2459 if( (ht->objectHandle)[i] )
2460 DeleteObject( (ht->objectHandle)[i] );
2461
2462 while (info->saved_state)
2463 {
2464 EMF_dc_state *state = info->saved_state;
2465 info->saved_state = info->saved_state->next;
2466 HeapFree( GetProcessHeap(), 0, state );
2467 }
2468 HeapFree( GetProcessHeap(), 0, info );
2469 return ret;
2470 }
2471
2472 static INT CALLBACK EMF_PlayEnhMetaFileCallback(HDC hdc, HANDLETABLE *ht,
2473 const ENHMETARECORD *emr,
2474 INT handles, LPARAM data)
2475 {
2476 return PlayEnhMetaFileRecord(hdc, ht, emr, handles);
2477 }
2478
2479 /**************************************************************************
2480 * PlayEnhMetaFile (GDI32.@)
2481 *
2482 * Renders an enhanced metafile into a specified rectangle *lpRect
2483 * in device context hdc.
2484 *
2485 * RETURNS
2486 * Success: TRUE
2487 * Failure: FALSE
2488 */
2489 BOOL WINAPI PlayEnhMetaFile(
2490 HDC hdc, /* [in] DC to render into */
2491 HENHMETAFILE hmf, /* [in] metafile to render */
2492 const RECT *lpRect /* [in] rectangle to place metafile inside */
2493 )
2494 {
2495 return EnumEnhMetaFile(hdc, hmf, EMF_PlayEnhMetaFileCallback, NULL,
2496 lpRect);
2497 }
2498
2499 /*****************************************************************************
2500 * DeleteEnhMetaFile (GDI32.@)
2501 *
2502 * Deletes an enhanced metafile and frees the associated storage.
2503 */
2504 BOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf)
2505 {
2506 return EMF_Delete_HENHMETAFILE( hmf );
2507 }
2508
2509 /*****************************************************************************
2510 * CopyEnhMetaFileA (GDI32.@)
2511 *
2512 * Duplicate an enhanced metafile.
2513 *
2514 *
2515 */
2516 HENHMETAFILE WINAPI CopyEnhMetaFileA(
2517 HENHMETAFILE hmfSrc,
2518 LPCSTR file)
2519 {
2520 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2521 HENHMETAFILE hmfDst;
2522
2523 if(!emrSrc) return FALSE;
2524 if (!file) {
2525 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2526 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2527 hmfDst = EMF_Create_HENHMETAFILE( emrDst, FALSE );
2528 if (!hmfDst)
2529 HeapFree( GetProcessHeap(), 0, emrDst );
2530 } else {
2531 HANDLE hFile;
2532 DWORD w;
2533 hFile = CreateFileA( file, GENERIC_WRITE | GENERIC_READ, 0,
2534 NULL, CREATE_ALWAYS, 0, 0);
2535 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2536 CloseHandle( hFile );
2537 /* Reopen file for reading only, so that apps can share
2538 read access to the file while hmf is still valid */
2539 hFile = CreateFileA( file, GENERIC_READ, FILE_SHARE_READ,
2540 NULL, OPEN_EXISTING, 0, 0);
2541 if(hFile == INVALID_HANDLE_VALUE) {
2542 ERR("Can't reopen emf for reading\n");
2543 return 0;
2544 }
2545 hmfDst = EMF_GetEnhMetaFile( hFile );
2546 CloseHandle( hFile );
2547 }
2548 return hmfDst;
2549 }
2550
2551 /*****************************************************************************
2552 * CopyEnhMetaFileW (GDI32.@)
2553 *
2554 * See CopyEnhMetaFileA.
2555 *
2556 *
2557 */
2558 HENHMETAFILE WINAPI CopyEnhMetaFileW(
2559 HENHMETAFILE hmfSrc,
2560 LPCWSTR file)
2561 {
2562 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
2563 HENHMETAFILE hmfDst;
2564
2565 if(!emrSrc) return FALSE;
2566 if (!file) {
2567 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2568 memcpy( emrDst, emrSrc, emrSrc->nBytes );
2569 hmfDst = EMF_Create_HENHMETAFILE( emrDst, FALSE );
2570 if (!hmfDst)
2571 HeapFree( GetProcessHeap(), 0, emrDst );
2572 } else {
2573 HANDLE hFile;
2574 DWORD w;
2575 hFile = CreateFileW( file, GENERIC_WRITE | GENERIC_READ, 0,
2576 NULL, CREATE_ALWAYS, 0, 0);
2577 WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2578 CloseHandle( hFile );
2579 /* Reopen file for reading only, so that apps can share
2580 read access to the file while hmf is still valid */
2581 hFile = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ,
2582 NULL, OPEN_EXISTING, 0, 0);
2583 if(hFile == INVALID_HANDLE_VALUE) {
2584 ERR("Can't reopen emf for reading\n");
2585 return 0;
2586 }
2587 hmfDst = EMF_GetEnhMetaFile( hFile );
2588 CloseHandle( hFile );
2589 }
2590 return hmfDst;
2591 }
2592
2593
2594 /* Struct to be used to be passed in the LPVOID parameter for cbEnhPaletteCopy */
2595 typedef struct tagEMF_PaletteCopy
2596 {
2597 UINT cEntries;
2598 LPPALETTEENTRY lpPe;
2599 } EMF_PaletteCopy;
2600
2601 /***************************************************************
2602 * Find the EMR_EOF record and then use it to find the
2603 * palette entries for this enhanced metafile.
2604 * The lpData is actually a pointer to an EMF_PaletteCopy struct
2605 * which contains the max number of elements to copy and where
2606 * to copy them to.
2607 *
2608 * NOTE: To be used by GetEnhMetaFilePaletteEntries only!
2609 */
2610 static INT CALLBACK cbEnhPaletteCopy( HDC a,
2611 HANDLETABLE *b,
2612 const ENHMETARECORD *lpEMR,
2613 INT c,
2614 LPARAM lpData )
2615 {
2616
2617 if ( lpEMR->iType == EMR_EOF )
2618 {
2619 const EMREOF *lpEof = (const EMREOF *)lpEMR;
2620 EMF_PaletteCopy* info = (EMF_PaletteCopy*)lpData;
2621 DWORD dwNumPalToCopy = min( lpEof->nPalEntries, info->cEntries );
2622
2623 TRACE( "copying 0x%08x palettes\n", dwNumPalToCopy );
2624
2625 memcpy( info->lpPe, (LPCSTR)lpEof + lpEof->offPalEntries,
2626 sizeof( *(info->lpPe) ) * dwNumPalToCopy );
2627
2628 /* Update the passed data as a return code */
2629 info->lpPe = NULL; /* Palettes were copied! */
2630 info->cEntries = dwNumPalToCopy;
2631
2632 return FALSE; /* That's all we need */
2633 }
2634
2635 return TRUE;
2636 }
2637
2638 /*****************************************************************************
2639 * GetEnhMetaFilePaletteEntries (GDI32.@)
2640 *
2641 * Copy the palette and report size
2642 *
2643 * BUGS: Error codes (SetLastError) are not set on failures
2644 */
2645 UINT WINAPI GetEnhMetaFilePaletteEntries( HENHMETAFILE hEmf,
2646 UINT cEntries,
2647 LPPALETTEENTRY lpPe )
2648 {
2649 ENHMETAHEADER* enhHeader = EMF_GetEnhMetaHeader( hEmf );
2650 EMF_PaletteCopy infoForCallBack;
2651
2652 TRACE( "(%p,%d,%p)\n", hEmf, cEntries, lpPe );
2653
2654 if (!enhHeader) return 0;
2655
2656 /* First check if there are any palettes associated with
2657 this metafile. */
2658 if ( enhHeader->nPalEntries == 0 ) return 0;
2659
2660 /* Is the user requesting the number of palettes? */
2661 if ( lpPe == NULL ) return enhHeader->nPalEntries;
2662
2663 /* Copy cEntries worth of PALETTEENTRY structs into the buffer */
2664 infoForCallBack.cEntries = cEntries;
2665 infoForCallBack.lpPe = lpPe;
2666
2667 if ( !EnumEnhMetaFile( 0, hEmf, cbEnhPaletteCopy,
2668 &infoForCallBack, 0 ) )
2669 return GDI_ERROR;
2670
2671 /* Verify that the callback executed correctly */
2672 if ( infoForCallBack.lpPe != NULL )
2673 {
2674 /* Callback proc had error! */
2675 ERR( "cbEnhPaletteCopy didn't execute correctly\n" );
2676 return GDI_ERROR;
2677 }
2678
2679 return infoForCallBack.cEntries;
2680 }
2681
2682 /******************************************************************
2683 * extract_emf_from_comment
2684 *
2685 * If the WMF was created by GetWinMetaFileBits, then extract the
2686 * original EMF that is stored in MFCOMMENT chunks.
2687 */
2688 static HENHMETAFILE extract_emf_from_comment( const BYTE *buf, UINT mf_size )
2689 {
2690 METAHEADER *mh = (METAHEADER *)buf;
2691 METARECORD *mr;
2692 emf_in_wmf_comment *chunk;
2693 WORD checksum = 0;
2694 DWORD size = 0, remaining, chunks;
2695 BYTE *emf_bits = NULL, *ptr;
2696 UINT offset;
2697 HENHMETAFILE emf = NULL;
2698
2699 if (mf_size < sizeof(*mh)) return NULL;
2700
2701 for (offset = mh->mtHeaderSize * 2; offset < mf_size; offset += (mr->rdSize * 2))
2702 {
2703 mr = (METARECORD *)((char *)mh + offset);
2704 chunk = (emf_in_wmf_comment *)(mr->rdParm + 2);
2705
2706 if (mr->rdFunction != META_ESCAPE || mr->rdParm[0] != MFCOMMENT) goto done;
2707 if (chunk->magic != WMFC_MAGIC) goto done;
2708
2709 if (!emf_bits)
2710 {
2711 size = remaining = chunk->emf_size;
2712 chunks = chunk->num_chunks;
2713 emf_bits = ptr = HeapAlloc( GetProcessHeap(), 0, size );
2714 if (!emf_bits) goto done;
2715 }
2716 if (chunk->chunk_size > remaining) goto done;
2717 remaining -= chunk->chunk_size;
2718 if (chunk->remaining_size != remaining) goto done;
2719 memcpy( ptr, chunk->emf_data, chunk->chunk_size );
2720 ptr += chunk->chunk_size;
2721 if (--chunks == 0) break;
2722 }
2723
2724 for (offset = 0; offset < mf_size / 2; offset++)
2725 checksum += *((WORD *)buf + offset);
2726 if (checksum) goto done;
2727
2728 emf = SetEnhMetaFileBits( size, emf_bits );
2729
2730 done:
2731 HeapFree( GetProcessHeap(), 0, emf_bits );
2732 return emf;
2733 }
2734
2735 typedef struct wmf_in_emf_comment
2736 {
2737 DWORD ident;
2738 DWORD iComment;
2739 DWORD nVersion;
2740 DWORD nChecksum;
2741 DWORD fFlags;
2742 DWORD cbWinMetaFile;
2743 } wmf_in_emf_comment;
2744
2745 /******************************************************************
2746 * SetWinMetaFileBits (GDI32.@)
2747 *
2748 * Translate from old style to new style.
2749 *
2750 */
2751 HENHMETAFILE WINAPI SetWinMetaFileBits(UINT cbBuffer, const BYTE *lpbBuffer, HDC hdcRef,
2752 const METAFILEPICT *lpmfp)
2753 {
2754 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
2755 HMETAFILE hmf = NULL;
2756 HENHMETAFILE ret = NULL;
2757 HDC hdc = NULL, hdcdisp = NULL;
2758 RECT rc, *prcFrame = NULL;
2759 LONG mm, xExt, yExt;
2760 INT horzsize, vertsize, horzres, vertres;
2761
2762 TRACE("(%d, %p, %p, %p)\n", cbBuffer, lpbBuffer, hdcRef, lpmfp);
2763
2764 hmf = SetMetaFileBitsEx(cbBuffer, lpbBuffer);
2765 if(!hmf)
2766 {
2767 WARN("SetMetaFileBitsEx failed\n");
2768 return NULL;
2769 }
2770
2771 ret = extract_emf_from_comment( lpbBuffer, cbBuffer );
2772 if (ret) return ret;
2773
2774 if(!hdcRef)
2775 hdcRef = hdcdisp = CreateDCW(szDisplayW, NULL, NULL, NULL);
2776
2777 if (lpmfp)
2778 {
2779 TRACE("mm = %d %dx%d\n", lpmfp->mm, lpmfp->xExt, lpmfp->yExt);
2780
2781 mm = lpmfp->mm;
2782 xExt = lpmfp->xExt;
2783 yExt = lpmfp->yExt;
2784 }
2785 else
2786 {
2787 TRACE("lpmfp == NULL\n");
2788
2789 /* Use the whole device surface */
2790 mm = MM_ANISOTROPIC;
2791 xExt = 0;
2792 yExt = 0;
2793 }
2794
2795 if (mm == MM_ISOTROPIC || mm == MM_ANISOTROPIC)
2796 {
2797 if (xExt < 0 || yExt < 0)
2798 {
2799 /* Use the whole device surface */
2800 xExt = 0;
2801 yExt = 0;
2802 }
2803
2804 /* Use the x and y extents as the frame box */
2805 if (xExt && yExt)
2806 {
2807 rc.left = rc.top = 0;
2808 rc.right = xExt;
2809 rc.bottom = yExt;
2810 prcFrame = &rc;
2811 }
2812 }
2813
2814 if(!(hdc = CreateEnhMetaFileW(hdcRef, NULL, prcFrame, NULL)))
2815 {
2816 ERR("CreateEnhMetaFile failed\n");
2817 goto end;
2818 }
2819
2820 /*
2821 * Write the original METAFILE into the enhanced metafile.
2822 * It is encapsulated in a GDICOMMENT_WINDOWS_METAFILE record.
2823 */
2824 if (mm != MM_TEXT)
2825 {
2826 wmf_in_emf_comment *mfcomment;
2827 UINT mfcomment_size;
2828
2829 mfcomment_size = sizeof (*mfcomment) + cbBuffer;
2830 mfcomment = HeapAlloc(GetProcessHeap(), 0, mfcomment_size);
2831 if (mfcomment)
2832 {
2833 mfcomment->ident = GDICOMMENT_IDENTIFIER;
2834 mfcomment->iComment = GDICOMMENT_WINDOWS_METAFILE;
2835 mfcomment->nVersion = 0x00000300;
2836 mfcomment->nChecksum = 0; /* FIXME */
2837 mfcomment->fFlags = 0;
2838 mfcomment->cbWinMetaFile = cbBuffer;
2839 memcpy(&mfcomment[1], lpbBuffer, cbBuffer);
2840 GdiComment(hdc, mfcomment_size, (BYTE*) mfcomment);
2841 HeapFree(GetProcessHeap(), 0, mfcomment);
2842 }
2843 SetMapMode(hdc, mm);
2844 }
2845
2846
2847 horzsize = GetDeviceCaps(hdcRef, HORZSIZE);
2848 vertsize = GetDeviceCaps(hdcRef, VERTSIZE);
2849 horzres = GetDeviceCaps(hdcRef, HORZRES);
2850 vertres = GetDeviceCaps(hdcRef, VERTRES);
2851
2852 if (!xExt || !yExt)
2853 {
2854 /* Use the whole device surface */
2855 xExt = horzres;
2856 yExt = vertres;
2857 }
2858 else
2859 {
2860 xExt = MulDiv(xExt, horzres, 100 * horzsize);
2861 yExt = MulDiv(yExt, vertres, 100 * vertsize);
2862 }
2863
2864 /* set the initial viewport:window ratio as 1:1 */
2865 SetViewportExtEx(hdc, xExt, yExt, NULL);
2866 SetWindowExtEx(hdc, xExt, yExt, NULL);
2867
2868 PlayMetaFile(hdc, hmf);
2869
2870 ret = CloseEnhMetaFile(hdc);
2871 end:
2872 if (hdcdisp) DeleteDC(hdcdisp);
2873 DeleteMetaFile(hmf);
2874 return ret;
2875 }