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