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