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