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