Added exception handling wrapper to a number of server requests.
[wine.git] / objects / enhmetafile.c
blobcb27f6875a2d306e1d2ae22c74d2e227e3ee8811
1 /*
2 * Enhanced metafile functions
3 * Copyright 1998 Douglas Ridgway
4 * 1999 Huw D M Davies
6 *
7 * The enhanced format consists of the following elements:
9 * A header
10 * A table of handles to GDI objects
11 * An array of metafile records
12 * A private palette
15 * The standard format consists of a header and an array of metafile records.
17 */
19 #include <string.h>
20 #include <assert.h>
21 #include "winbase.h"
22 #include "wingdi.h"
23 #include "wine/winestring.h"
24 #include "winerror.h"
25 #include "enhmetafile.h"
26 #include "debugtools.h"
27 #include "heap.h"
28 #include "metafile.h"
30 DEFAULT_DEBUG_CHANNEL(enhmetafile)
32 /****************************************************************************
33 * EMF_Create_HENHMETAFILE
35 HENHMETAFILE EMF_Create_HENHMETAFILE(ENHMETAHEADER *emh, HFILE hFile, HANDLE
36 hMapping )
38 HENHMETAFILE hmf = 0;
39 ENHMETAFILEOBJ *metaObj = GDI_AllocObject( sizeof(ENHMETAFILEOBJ),
40 ENHMETAFILE_MAGIC, &hmf );
41 if (metaObj)
43 metaObj->emh = emh;
44 metaObj->hFile = hFile;
45 metaObj->hMapping = hMapping;
46 GDI_ReleaseObj( hmf );
48 return hmf;
51 /****************************************************************************
52 * EMF_Delete_HENHMETAFILE
54 static BOOL EMF_Delete_HENHMETAFILE( HENHMETAFILE hmf )
56 ENHMETAFILEOBJ *metaObj = (ENHMETAFILEOBJ *)GDI_GetObjPtr( hmf,
57 ENHMETAFILE_MAGIC );
58 if(!metaObj) return FALSE;
59 if(metaObj->hMapping) {
60 UnmapViewOfFile( metaObj->emh );
61 CloseHandle( metaObj->hMapping );
62 CloseHandle( metaObj->hFile );
63 } else
64 HeapFree( GetProcessHeap(), 0, metaObj->emh );
65 return GDI_FreeObject( hmf, metaObj );
68 /******************************************************************
69 * EMF_GetEnhMetaHeader
71 * Returns ptr to ENHMETAHEADER associated with HENHMETAFILE
72 * Should be followed by call to EMF_ReleaseEnhMetaHeader
74 static ENHMETAHEADER *EMF_GetEnhMetaHeader( HENHMETAFILE hmf )
76 ENHMETAFILEOBJ *metaObj = (ENHMETAFILEOBJ *)GDI_GetObjPtr( hmf,
77 ENHMETAFILE_MAGIC );
78 TRACE("hmf %04x -> enhmetaObj %p\n", hmf, metaObj);
79 return metaObj ? metaObj->emh : NULL;
82 /******************************************************************
83 * EMF_ReleaseEnhMetaHeader
85 * Releases ENHMETAHEADER associated with HENHMETAFILE
87 static void EMF_ReleaseEnhMetaHeader( HENHMETAFILE hmf )
89 GDI_ReleaseObj( hmf );
92 /*****************************************************************************
93 * EMF_GetEnhMetaFile
96 static HENHMETAFILE EMF_GetEnhMetaFile( HFILE hFile )
98 ENHMETAHEADER *emh;
99 HANDLE hMapping;
101 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
102 emh = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
104 if (emh->iType != EMR_HEADER || emh->dSignature != ENHMETA_SIGNATURE) {
105 WARN("Invalid emf header type 0x%08lx sig 0x%08lx.\n",
106 emh->iType, emh->dSignature);
107 UnmapViewOfFile( emh );
108 CloseHandle( hMapping );
109 return 0;
111 return EMF_Create_HENHMETAFILE( emh, hFile, hMapping );
115 /*****************************************************************************
116 * GetEnhMetaFileA (GDI32.174)
120 HENHMETAFILE WINAPI GetEnhMetaFileA(
121 LPCSTR lpszMetaFile /* filename of enhanced metafile */
124 HENHMETAFILE hmf;
125 HFILE hFile;
127 hFile = CreateFileA(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
128 OPEN_EXISTING, 0, 0);
129 if (hFile == INVALID_HANDLE_VALUE) {
130 WARN("could not open %s\n", lpszMetaFile);
131 return 0;
133 hmf = EMF_GetEnhMetaFile( hFile );
134 if(!hmf)
135 CloseHandle( hFile );
136 return hmf;
139 /*****************************************************************************
140 * GetEnhMetaFileW (GDI32.180)
142 HENHMETAFILE WINAPI GetEnhMetaFileW(
143 LPCWSTR lpszMetaFile) /* filename of enhanced metafile */
145 HENHMETAFILE hmf;
146 HFILE hFile;
148 hFile = CreateFileW(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
149 OPEN_EXISTING, 0, 0);
150 if (hFile == INVALID_HANDLE_VALUE) {
151 WARN("could not open %s\n", debugstr_w(lpszMetaFile));
152 return 0;
154 hmf = EMF_GetEnhMetaFile( hFile );
155 if(!hmf)
156 CloseHandle( hFile );
157 return hmf;
160 /*****************************************************************************
161 * GetEnhMetaFileHeader (GDI32.178)
163 * If buf is NULL, returns the size of buffer required.
164 * Otherwise, copy up to bufsize bytes of enhanced metafile header into
165 * buf.
167 UINT WINAPI GetEnhMetaFileHeader(
168 HENHMETAFILE hmf, /* enhanced metafile */
169 UINT bufsize, /* size of buffer */
170 LPENHMETAHEADER buf /* buffer */
173 LPENHMETAHEADER emh;
174 UINT size;
176 emh = EMF_GetEnhMetaHeader(hmf);
177 if(!emh) return FALSE;
178 size = emh->nSize;
179 if (!buf) {
180 EMF_ReleaseEnhMetaHeader(hmf);
181 return size;
183 size = min(size, bufsize);
184 memmove(buf, emh, size);
185 EMF_ReleaseEnhMetaHeader(hmf);
186 return size;
190 /*****************************************************************************
191 * GetEnhMetaFileDescriptionA (GDI32.176)
193 UINT WINAPI GetEnhMetaFileDescriptionA(
194 HENHMETAFILE hmf, /* enhanced metafile */
195 UINT size, /* size of buf */
196 LPSTR buf /* buffer to receive description */
199 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
200 INT first, first_A;
202 if(!emh) return FALSE;
203 if(emh->nDescription == 0 || emh->offDescription == 0) {
204 EMF_ReleaseEnhMetaHeader(hmf);
205 return 0;
207 if (!buf || !size ) {
208 EMF_ReleaseEnhMetaHeader(hmf);
209 return emh->nDescription;
212 first = lstrlenW( (WCHAR *) ((char *) emh + emh->offDescription));
214 lstrcpynWtoA(buf, (WCHAR *) ((char *) emh + emh->offDescription), size);
215 first_A = strlen( buf );
216 buf += first_A + 1;
217 lstrcpynWtoA(buf, (WCHAR *) ((char *) emh + emh->offDescription+2*(first+1)),
218 size - first_A - 1); /* i18n ready */
219 first_A += strlen(buf) + 1;
221 EMF_ReleaseEnhMetaHeader(hmf);
222 return min(size, first_A);
225 /*****************************************************************************
226 * GetEnhMetaFileDescriptionW (GDI32.177)
228 * Copies the description string of an enhanced metafile into a buffer
229 * _buf_.
231 * If _buf_ is NULL, returns size of _buf_ required. Otherwise, returns
232 * number of characters copied.
234 UINT WINAPI GetEnhMetaFileDescriptionW(
235 HENHMETAFILE hmf, /* enhanced metafile */
236 UINT size, /* size of buf */
237 LPWSTR buf /* buffer to receive description */
240 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
242 if(!emh) return FALSE;
243 if(emh->nDescription == 0 || emh->offDescription == 0) {
244 EMF_ReleaseEnhMetaHeader(hmf);
245 return 0;
247 if (!buf || !size ) {
248 EMF_ReleaseEnhMetaHeader(hmf);
249 return emh->nDescription;
252 memmove(buf, (char *) emh + emh->offDescription,
253 min(size,emh->nDescription));
254 EMF_ReleaseEnhMetaHeader(hmf);
255 return min(size, emh->nDescription);
258 /****************************************************************************
259 * SetEnhMetaFileBits (GDI32.315)
261 * Creates an enhanced metafile by copying _bufsize_ bytes from _buf_.
263 HENHMETAFILE WINAPI SetEnhMetaFileBits(UINT bufsize, const BYTE *buf)
265 ENHMETAHEADER *emh = HeapAlloc( GetProcessHeap(), 0, bufsize );
266 memmove(emh, buf, bufsize);
267 return EMF_Create_HENHMETAFILE( emh, 0, 0 );
270 /*****************************************************************************
271 * GetEnhMetaFileBits (GDI32.175)
274 UINT WINAPI GetEnhMetaFileBits(
275 HENHMETAFILE hmf,
276 UINT bufsize,
277 LPBYTE buf
280 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader( hmf );
281 UINT size;
283 if(!emh) return 0;
285 size = emh->nBytes;
286 if( buf == NULL ) {
287 EMF_ReleaseEnhMetaHeader( hmf );
288 return size;
291 size = min( size, bufsize );
292 memmove(buf, emh, size);
294 EMF_ReleaseEnhMetaHeader( hmf );
295 return size;
298 /*****************************************************************************
299 * PlayEnhMetaFileRecord (GDI32.264)
301 * Render a single enhanced metafile record in the device context hdc.
303 * RETURNS
304 * TRUE (non zero) on success, FALSE on error.
305 * BUGS
306 * Many unimplemented records.
307 * No error handling on record play failures (ie checking return codes)
309 BOOL WINAPI PlayEnhMetaFileRecord(
310 HDC hdc, /* device context in which to render EMF record */
311 LPHANDLETABLE handletable, /* array of handles to be used in rendering record */
312 const ENHMETARECORD *mr, /* EMF record to render */
313 UINT handles /* size of handle array */
316 int type;
317 TRACE(
318 "hdc = %08x, handletable = %p, record = %p, numHandles = %d\n",
319 hdc, handletable, mr, handles);
320 if (!mr) return FALSE;
322 type = mr->iType;
324 TRACE(" type=%d\n", type);
325 switch(type)
327 case EMR_HEADER:
328 break;
329 case EMR_EOF:
330 break;
331 case EMR_GDICOMMENT:
333 PEMRGDICOMMENT lpGdiComment = (PEMRGDICOMMENT)mr;
334 /* In an enhanced metafile, there can be both public and private GDI comments */
335 GdiComment( hdc, lpGdiComment->cbData, lpGdiComment->Data );
336 break;
338 case EMR_SETMAPMODE:
340 PEMRSETMAPMODE pSetMapMode = (PEMRSETMAPMODE) mr;
341 SetMapMode(hdc, pSetMapMode->iMode);
342 break;
344 case EMR_SETBKMODE:
346 PEMRSETBKMODE pSetBkMode = (PEMRSETBKMODE) mr;
347 SetBkMode(hdc, pSetBkMode->iMode);
348 break;
350 case EMR_SETBKCOLOR:
352 PEMRSETBKCOLOR pSetBkColor = (PEMRSETBKCOLOR) mr;
353 SetBkColor(hdc, pSetBkColor->crColor);
354 break;
356 case EMR_SETPOLYFILLMODE:
358 PEMRSETPOLYFILLMODE pSetPolyFillMode = (PEMRSETPOLYFILLMODE) mr;
359 SetPolyFillMode(hdc, pSetPolyFillMode->iMode);
360 break;
362 case EMR_SETROP2:
364 PEMRSETROP2 pSetROP2 = (PEMRSETROP2) mr;
365 SetROP2(hdc, pSetROP2->iMode);
366 break;
368 case EMR_SETSTRETCHBLTMODE:
370 PEMRSETSTRETCHBLTMODE pSetStretchBltMode = (PEMRSETSTRETCHBLTMODE) mr;
371 SetStretchBltMode(hdc, pSetStretchBltMode->iMode);
372 break;
374 case EMR_SETTEXTALIGN:
376 PEMRSETTEXTALIGN pSetTextAlign = (PEMRSETTEXTALIGN) mr;
377 SetTextAlign(hdc, pSetTextAlign->iMode);
378 break;
380 case EMR_SETTEXTCOLOR:
382 PEMRSETTEXTCOLOR pSetTextColor = (PEMRSETTEXTCOLOR) mr;
383 SetTextColor(hdc, pSetTextColor->crColor);
384 break;
386 case EMR_SAVEDC:
388 SaveDC(hdc);
389 break;
391 case EMR_RESTOREDC:
393 PEMRRESTOREDC pRestoreDC = (PEMRRESTOREDC) mr;
394 RestoreDC(hdc, pRestoreDC->iRelative);
395 break;
397 case EMR_INTERSECTCLIPRECT:
399 PEMRINTERSECTCLIPRECT pClipRect = (PEMRINTERSECTCLIPRECT) mr;
400 IntersectClipRect(hdc, pClipRect->rclClip.left, pClipRect->rclClip.top,
401 pClipRect->rclClip.right, pClipRect->rclClip.bottom);
402 break;
404 case EMR_SELECTOBJECT:
406 PEMRSELECTOBJECT pSelectObject = (PEMRSELECTOBJECT) mr;
407 if( pSelectObject->ihObject & 0x80000000 ) {
408 /* High order bit is set - it's a stock object
409 * Strip the high bit to get the index.
410 * See MSDN article Q142319
412 SelectObject( hdc, GetStockObject( pSelectObject->ihObject &
413 0x7fffffff ) );
414 } else {
415 /* High order bit wasn't set - not a stock object
417 SelectObject( hdc,
418 (handletable->objectHandle)[pSelectObject->ihObject] );
420 break;
422 case EMR_DELETEOBJECT:
424 PEMRDELETEOBJECT pDeleteObject = (PEMRDELETEOBJECT) mr;
425 DeleteObject( (handletable->objectHandle)[pDeleteObject->ihObject]);
426 (handletable->objectHandle)[pDeleteObject->ihObject] = 0;
427 break;
429 case EMR_SETWINDOWORGEX:
431 PEMRSETWINDOWORGEX pSetWindowOrgEx = (PEMRSETWINDOWORGEX) mr;
432 SetWindowOrgEx(hdc, pSetWindowOrgEx->ptlOrigin.x,
433 pSetWindowOrgEx->ptlOrigin.y, NULL);
434 break;
436 case EMR_SETWINDOWEXTEX:
438 PEMRSETWINDOWEXTEX pSetWindowExtEx = (PEMRSETWINDOWEXTEX) mr;
439 SetWindowExtEx(hdc, pSetWindowExtEx->szlExtent.cx,
440 pSetWindowExtEx->szlExtent.cy, NULL);
441 break;
443 case EMR_SETVIEWPORTORGEX:
445 PEMRSETVIEWPORTORGEX pSetViewportOrgEx = (PEMRSETVIEWPORTORGEX) mr;
446 SetViewportOrgEx(hdc, pSetViewportOrgEx->ptlOrigin.x,
447 pSetViewportOrgEx->ptlOrigin.y, NULL);
448 break;
450 case EMR_SETVIEWPORTEXTEX:
452 PEMRSETVIEWPORTEXTEX pSetViewportExtEx = (PEMRSETVIEWPORTEXTEX) mr;
453 SetViewportExtEx(hdc, pSetViewportExtEx->szlExtent.cx,
454 pSetViewportExtEx->szlExtent.cy, NULL);
455 break;
457 case EMR_CREATEPEN:
459 PEMRCREATEPEN pCreatePen = (PEMRCREATEPEN) mr;
460 (handletable->objectHandle)[pCreatePen->ihPen] =
461 CreatePenIndirect(&pCreatePen->lopn);
462 break;
464 case EMR_EXTCREATEPEN:
466 PEMREXTCREATEPEN pPen = (PEMREXTCREATEPEN) mr;
467 LOGBRUSH lb;
468 lb.lbStyle = pPen->elp.elpBrushStyle;
469 lb.lbColor = pPen->elp.elpColor;
470 lb.lbHatch = pPen->elp.elpHatch;
472 if(pPen->offBmi || pPen->offBits)
473 FIXME("EMR_EXTCREATEPEN: Need to copy brush bitmap\n");
475 (handletable->objectHandle)[pPen->ihPen] =
476 ExtCreatePen(pPen->elp.elpPenStyle, pPen->elp.elpWidth, &lb,
477 pPen->elp.elpNumEntries, pPen->elp.elpStyleEntry);
478 break;
480 case EMR_CREATEBRUSHINDIRECT:
482 PEMRCREATEBRUSHINDIRECT pBrush = (PEMRCREATEBRUSHINDIRECT) mr;
483 (handletable->objectHandle)[pBrush->ihBrush] =
484 CreateBrushIndirect(&pBrush->lb);
485 break;
487 case EMR_EXTCREATEFONTINDIRECTW:
489 PEMREXTCREATEFONTINDIRECTW pFont = (PEMREXTCREATEFONTINDIRECTW) mr;
490 (handletable->objectHandle)[pFont->ihFont] =
491 CreateFontIndirectW(&pFont->elfw.elfLogFont);
492 break;
494 case EMR_MOVETOEX:
496 PEMRMOVETOEX pMoveToEx = (PEMRMOVETOEX) mr;
497 MoveToEx(hdc, pMoveToEx->ptl.x, pMoveToEx->ptl.y, NULL);
498 break;
500 case EMR_LINETO:
502 PEMRLINETO pLineTo = (PEMRLINETO) mr;
503 LineTo(hdc, pLineTo->ptl.x, pLineTo->ptl.y);
504 break;
506 case EMR_RECTANGLE:
508 PEMRRECTANGLE pRect = (PEMRRECTANGLE) mr;
509 Rectangle(hdc, pRect->rclBox.left, pRect->rclBox.top,
510 pRect->rclBox.right, pRect->rclBox.bottom);
511 break;
513 case EMR_ELLIPSE:
515 PEMRELLIPSE pEllipse = (PEMRELLIPSE) mr;
516 Ellipse(hdc, pEllipse->rclBox.left, pEllipse->rclBox.top,
517 pEllipse->rclBox.right, pEllipse->rclBox.bottom);
518 break;
520 case EMR_POLYGON16:
522 PEMRPOLYGON16 pPoly = (PEMRPOLYGON16) mr;
523 /* Shouldn't use Polygon16 since pPoly->cpts is DWORD */
524 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
525 pPoly->cpts * sizeof(POINT) );
526 DWORD i;
527 for(i = 0; i < pPoly->cpts; i++)
528 CONV_POINT16TO32(pPoly->apts + i, pts + i);
529 Polygon(hdc, pts, pPoly->cpts);
530 HeapFree( GetProcessHeap(), 0, pts );
531 break;
533 case EMR_POLYLINE16:
535 PEMRPOLYLINE16 pPoly = (PEMRPOLYLINE16) mr;
536 /* Shouldn't use Polyline16 since pPoly->cpts is DWORD */
537 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
538 pPoly->cpts * sizeof(POINT) );
539 DWORD i;
540 for(i = 0; i < pPoly->cpts; i++)
541 CONV_POINT16TO32(pPoly->apts + i, pts + i);
542 Polyline(hdc, pts, pPoly->cpts);
543 HeapFree( GetProcessHeap(), 0, pts );
544 break;
546 case EMR_POLYLINETO16:
548 PEMRPOLYLINETO16 pPoly = (PEMRPOLYLINETO16) mr;
549 /* Shouldn't use PolylineTo16 since pPoly->cpts is DWORD */
550 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
551 pPoly->cpts * sizeof(POINT) );
552 DWORD i;
553 for(i = 0; i < pPoly->cpts; i++)
554 CONV_POINT16TO32(pPoly->apts + i, pts + i);
555 PolylineTo(hdc, pts, pPoly->cpts);
556 HeapFree( GetProcessHeap(), 0, pts );
557 break;
559 case EMR_POLYBEZIER16:
561 PEMRPOLYBEZIER16 pPoly = (PEMRPOLYBEZIER16) mr;
562 /* Shouldn't use PolyBezier16 since pPoly->cpts is DWORD */
563 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
564 pPoly->cpts * sizeof(POINT) );
565 DWORD i;
566 for(i = 0; i < pPoly->cpts; i++)
567 CONV_POINT16TO32(pPoly->apts + i, pts + i);
568 PolyBezier(hdc, pts, pPoly->cpts);
569 HeapFree( GetProcessHeap(), 0, pts );
570 break;
572 case EMR_POLYBEZIERTO16:
574 PEMRPOLYBEZIERTO16 pPoly = (PEMRPOLYBEZIERTO16) mr;
575 /* Shouldn't use PolyBezierTo16 since pPoly->cpts is DWORD */
576 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
577 pPoly->cpts * sizeof(POINT) );
578 DWORD i;
579 for(i = 0; i < pPoly->cpts; i++)
580 CONV_POINT16TO32(pPoly->apts + i, pts + i);
581 PolyBezierTo(hdc, pts, pPoly->cpts);
582 HeapFree( GetProcessHeap(), 0, pts );
583 break;
585 case EMR_POLYPOLYGON16:
587 PEMRPOLYPOLYGON16 pPolyPoly = (PEMRPOLYPOLYGON16) mr;
588 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
589 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
591 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
592 pPolyPoly->cpts * sizeof(POINT) );
593 DWORD i;
594 for(i = 0; i < pPolyPoly->cpts; i++)
595 CONV_POINT16TO32((POINTS*) (pPolyPoly->aPolyCounts +
596 pPolyPoly->nPolys) + i, pts + i);
598 PolyPolygon(hdc, pts, (INT*)pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
599 HeapFree( GetProcessHeap(), 0, pts );
600 break;
602 case EMR_POLYPOLYLINE16:
604 PEMRPOLYPOLYLINE16 pPolyPoly = (PEMRPOLYPOLYLINE16) mr;
605 /* NB POINTS array doesn't start at pPolyPoly->apts it's actually
606 pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
608 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
609 pPolyPoly->cpts * sizeof(POINT) );
610 DWORD i;
611 for(i = 0; i < pPolyPoly->cpts; i++)
612 CONV_POINT16TO32((POINTS*) (pPolyPoly->aPolyCounts +
613 pPolyPoly->nPolys) + i, pts + i);
615 PolyPolyline(hdc, pts, pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
616 HeapFree( GetProcessHeap(), 0, pts );
617 break;
620 case EMR_STRETCHDIBITS:
622 LONG xDest = mr->dParm[4];
623 LONG yDest = mr->dParm[5];
624 LONG xSrc = mr->dParm[6];
625 LONG ySrc = mr->dParm[7];
626 LONG cxSrc = mr->dParm[8];
627 LONG cySrc = mr->dParm[9];
628 DWORD offBmiSrc = mr->dParm[10];
629 DWORD offBitsSrc = mr->dParm[12];
630 DWORD iUsageSrc = mr->dParm[14];
631 DWORD dwRop = mr->dParm[15];
632 LONG cxDest = mr->dParm[16];
633 LONG cyDest = mr->dParm[17];
635 StretchDIBits(hdc,xDest,yDest,cxDest,cyDest,
636 xSrc,ySrc,cxSrc,cySrc,
637 ((char *)mr)+offBitsSrc,
638 (const BITMAPINFO *)(((char *)mr)+offBmiSrc),
639 iUsageSrc,dwRop);
640 break;
642 case EMR_EXTTEXTOUTW:
644 /* 0-3: ??? */
645 DWORD flags = mr->dParm[4];
646 /* 5, 6: ??? */
647 DWORD x = mr->dParm[7], y = mr->dParm[8];
648 DWORD count = mr->dParm[9];
649 /* 10-16: ??? */
650 LPWSTR str = (LPWSTR)& mr->dParm[17];
651 /* trailing info: dx array? */
652 FIXME("Many ExtTextOut args not handled\n");
653 ExtTextOutW(hdc, x, y, flags, /* lpRect */ NULL,
654 str, count, /* lpDx */ NULL);
655 break;
658 case EMR_CREATEPALETTE:
660 PEMRCREATEPALETTE lpCreatePal = (PEMRCREATEPALETTE)mr;
662 (handletable->objectHandle)[ lpCreatePal->ihPal ] =
663 CreatePalette( &lpCreatePal->lgpl );
665 break;
668 case EMR_SELECTPALETTE:
670 PEMRSELECTPALETTE lpSelectPal = (PEMRSELECTPALETTE)mr;
672 /* FIXME: Should this be forcing background mode? */
673 (handletable->objectHandle)[ lpSelectPal->ihPal ] =
674 SelectPalette( hdc, lpSelectPal->ihPal, FALSE );
675 break;
678 case EMR_REALIZEPALETTE:
680 RealizePalette( hdc );
681 break;
684 #if 0
685 case EMR_EXTSELECTCLIPRGN:
687 PEMREXTSELECTCLIPRGN lpRgn = (PEMREXTSELECTCLIPRGN)mr;
689 /* Need to make a region out of the RGNDATA we have */
690 ExtSelectClipRgn( hdc, ..., (INT)(lpRgn->iMode) );
693 #endif
695 case EMR_SETMETARGN:
697 SetMetaRgn( hdc );
698 break;
701 case EMR_SETWORLDTRANSFORM:
703 PEMRSETWORLDTRANSFORM lpXfrm = (PEMRSETWORLDTRANSFORM)mr;
705 SetWorldTransform( hdc, &lpXfrm->xform );
707 break;
710 case EMR_POLYBEZIER:
712 PEMRPOLYBEZIER lpPolyBez = (PEMRPOLYBEZIER)mr;
713 PolyBezier(hdc, (const LPPOINT)lpPolyBez->aptl, (UINT)lpPolyBez->cptl);
714 break;
717 case EMR_POLYGON:
719 PEMRPOLYGON lpPoly = (PEMRPOLYGON)mr;
720 Polygon( hdc, (const LPPOINT)lpPoly->aptl, (UINT)lpPoly->cptl );
721 break;
724 case EMR_POLYLINE:
726 PEMRPOLYLINE lpPolyLine = (PEMRPOLYLINE)mr;
727 Polyline(hdc, (const LPPOINT)lpPolyLine->aptl, (UINT)lpPolyLine->cptl);
728 break;
731 case EMR_POLYBEZIERTO:
733 PEMRPOLYBEZIERTO lpPolyBezierTo = (PEMRPOLYBEZIERTO)mr;
734 PolyBezierTo( hdc, (const LPPOINT)lpPolyBezierTo->aptl,
735 (UINT)lpPolyBezierTo->cptl );
736 break;
739 case EMR_POLYLINETO:
741 PEMRPOLYLINETO lpPolyLineTo = (PEMRPOLYLINETO)mr;
742 PolylineTo( hdc, (const LPPOINT)lpPolyLineTo->aptl,
743 (UINT)lpPolyLineTo->cptl );
744 break;
747 case EMR_POLYPOLYLINE:
749 PEMRPOLYPOLYLINE pPolyPolyline = (PEMRPOLYPOLYLINE) mr;
750 /* NB Points at pPolyPolyline->aPolyCounts + pPolyPolyline->nPolys */
752 PolyPolyline(hdc, (LPPOINT)(pPolyPolyline->aPolyCounts +
753 pPolyPolyline->nPolys),
754 pPolyPolyline->aPolyCounts,
755 pPolyPolyline->nPolys );
757 break;
760 case EMR_POLYPOLYGON:
762 PEMRPOLYPOLYGON pPolyPolygon = (PEMRPOLYPOLYGON) mr;
764 /* NB Points at pPolyPolygon->aPolyCounts + pPolyPolygon->nPolys */
766 PolyPolygon(hdc, (LPPOINT)(pPolyPolygon->aPolyCounts +
767 pPolyPolygon->nPolys),
768 (INT*)pPolyPolygon->aPolyCounts, pPolyPolygon->nPolys );
769 break;
772 case EMR_SETBRUSHORGEX:
774 PEMRSETBRUSHORGEX lpSetBrushOrgEx = (PEMRSETBRUSHORGEX)mr;
776 SetBrushOrgEx( hdc,
777 (INT)lpSetBrushOrgEx->ptlOrigin.x,
778 (INT)lpSetBrushOrgEx->ptlOrigin.y,
779 NULL );
781 break;
784 case EMR_SETPIXELV:
786 PEMRSETPIXELV lpSetPixelV = (PEMRSETPIXELV)mr;
788 SetPixelV( hdc,
789 (INT)lpSetPixelV->ptlPixel.x,
790 (INT)lpSetPixelV->ptlPixel.y,
791 lpSetPixelV->crColor );
793 break;
796 case EMR_SETMAPPERFLAGS:
798 PEMRSETMAPPERFLAGS lpSetMapperFlags = (PEMRSETMAPPERFLAGS)mr;
800 SetMapperFlags( hdc, lpSetMapperFlags->dwFlags );
802 break;
805 case EMR_SETCOLORADJUSTMENT:
807 PEMRSETCOLORADJUSTMENT lpSetColorAdjust = (PEMRSETCOLORADJUSTMENT)mr;
809 SetColorAdjustment( hdc, &lpSetColorAdjust->ColorAdjustment );
811 break;
814 case EMR_OFFSETCLIPRGN:
816 PEMROFFSETCLIPRGN lpOffsetClipRgn = (PEMROFFSETCLIPRGN)mr;
818 OffsetClipRgn( hdc,
819 (INT)lpOffsetClipRgn->ptlOffset.x,
820 (INT)lpOffsetClipRgn->ptlOffset.y );
822 break;
825 case EMR_EXCLUDECLIPRECT:
827 PEMREXCLUDECLIPRECT lpExcludeClipRect = (PEMREXCLUDECLIPRECT)mr;
829 ExcludeClipRect( hdc,
830 lpExcludeClipRect->rclClip.left,
831 lpExcludeClipRect->rclClip.top,
832 lpExcludeClipRect->rclClip.right,
833 lpExcludeClipRect->rclClip.bottom );
835 break;
838 case EMR_SCALEVIEWPORTEXTEX:
840 PEMRSCALEVIEWPORTEXTEX lpScaleViewportExtEx = (PEMRSCALEVIEWPORTEXTEX)mr;
842 ScaleViewportExtEx( hdc,
843 lpScaleViewportExtEx->xNum,
844 lpScaleViewportExtEx->xDenom,
845 lpScaleViewportExtEx->yNum,
846 lpScaleViewportExtEx->yDenom,
847 NULL );
849 break;
852 case EMR_SCALEWINDOWEXTEX:
854 PEMRSCALEWINDOWEXTEX lpScaleWindowExtEx = (PEMRSCALEWINDOWEXTEX)mr;
856 ScaleWindowExtEx( hdc,
857 lpScaleWindowExtEx->xNum,
858 lpScaleWindowExtEx->xDenom,
859 lpScaleWindowExtEx->yNum,
860 lpScaleWindowExtEx->yDenom,
861 NULL );
863 break;
866 case EMR_MODIFYWORLDTRANSFORM:
868 PEMRMODIFYWORLDTRANSFORM lpModifyWorldTrans = (PEMRMODIFYWORLDTRANSFORM)mr;
870 ModifyWorldTransform( hdc, &lpModifyWorldTrans->xform,
871 lpModifyWorldTrans->iMode );
873 break;
876 case EMR_ANGLEARC:
878 PEMRANGLEARC lpAngleArc = (PEMRANGLEARC)mr;
880 AngleArc( hdc,
881 (INT)lpAngleArc->ptlCenter.x, (INT)lpAngleArc->ptlCenter.y,
882 lpAngleArc->nRadius, lpAngleArc->eStartAngle,
883 lpAngleArc->eSweepAngle );
885 break;
888 case EMR_ROUNDRECT:
890 PEMRROUNDRECT lpRoundRect = (PEMRROUNDRECT)mr;
892 RoundRect( hdc,
893 lpRoundRect->rclBox.left,
894 lpRoundRect->rclBox.top,
895 lpRoundRect->rclBox.right,
896 lpRoundRect->rclBox.bottom,
897 lpRoundRect->szlCorner.cx,
898 lpRoundRect->szlCorner.cy );
900 break;
903 case EMR_ARC:
905 PEMRARC lpArc = (PEMRARC)mr;
907 Arc( hdc,
908 (INT)lpArc->rclBox.left,
909 (INT)lpArc->rclBox.top,
910 (INT)lpArc->rclBox.right,
911 (INT)lpArc->rclBox.bottom,
912 (INT)lpArc->ptlStart.x,
913 (INT)lpArc->ptlStart.y,
914 (INT)lpArc->ptlEnd.x,
915 (INT)lpArc->ptlEnd.y );
917 break;
920 case EMR_CHORD:
922 PEMRCHORD lpChord = (PEMRCHORD)mr;
924 Chord( hdc,
925 (INT)lpChord->rclBox.left,
926 (INT)lpChord->rclBox.top,
927 (INT)lpChord->rclBox.right,
928 (INT)lpChord->rclBox.bottom,
929 (INT)lpChord->ptlStart.x,
930 (INT)lpChord->ptlStart.y,
931 (INT)lpChord->ptlEnd.x,
932 (INT)lpChord->ptlEnd.y );
934 break;
937 case EMR_PIE:
939 PEMRPIE lpPie = (PEMRPIE)mr;
941 Pie( hdc,
942 (INT)lpPie->rclBox.left,
943 (INT)lpPie->rclBox.top,
944 (INT)lpPie->rclBox.right,
945 (INT)lpPie->rclBox.bottom,
946 (INT)lpPie->ptlStart.x,
947 (INT)lpPie->ptlStart.y,
948 (INT)lpPie->ptlEnd.x,
949 (INT)lpPie->ptlEnd.y );
951 break;
954 case EMR_ARCTO:
956 PEMRARC lpArcTo = (PEMRARC)mr;
958 ArcTo( hdc,
959 (INT)lpArcTo->rclBox.left,
960 (INT)lpArcTo->rclBox.top,
961 (INT)lpArcTo->rclBox.right,
962 (INT)lpArcTo->rclBox.bottom,
963 (INT)lpArcTo->ptlStart.x,
964 (INT)lpArcTo->ptlStart.y,
965 (INT)lpArcTo->ptlEnd.x,
966 (INT)lpArcTo->ptlEnd.y );
968 break;
971 case EMR_EXTFLOODFILL:
973 PEMREXTFLOODFILL lpExtFloodFill = (PEMREXTFLOODFILL)mr;
975 ExtFloodFill( hdc,
976 (INT)lpExtFloodFill->ptlStart.x,
977 (INT)lpExtFloodFill->ptlStart.y,
978 lpExtFloodFill->crColor,
979 (UINT)lpExtFloodFill->iMode );
981 break;
984 case EMR_POLYDRAW:
986 PEMRPOLYDRAW lpPolyDraw = (PEMRPOLYDRAW)mr;
987 PolyDraw( hdc,
988 (const LPPOINT)lpPolyDraw->aptl,
989 lpPolyDraw->abTypes,
990 (INT)lpPolyDraw->cptl );
992 break;
995 case EMR_SETARCDIRECTION:
997 PEMRSETARCDIRECTION lpSetArcDirection = (PEMRSETARCDIRECTION)mr;
998 SetArcDirection( hdc, (INT)lpSetArcDirection->iArcDirection );
999 break;
1002 case EMR_SETMITERLIMIT:
1004 PEMRSETMITERLIMIT lpSetMiterLimit = (PEMRSETMITERLIMIT)mr;
1005 SetMiterLimit( hdc, lpSetMiterLimit->eMiterLimit, NULL );
1006 break;
1009 case EMR_BEGINPATH:
1011 BeginPath( hdc );
1012 break;
1015 case EMR_ENDPATH:
1017 EndPath( hdc );
1018 break;
1021 case EMR_CLOSEFIGURE:
1023 CloseFigure( hdc );
1024 break;
1027 case EMR_FILLPATH:
1029 /*PEMRFILLPATH lpFillPath = (PEMRFILLPATH)mr;*/
1030 FillPath( hdc );
1031 break;
1034 case EMR_STROKEANDFILLPATH:
1036 /*PEMRSTROKEANDFILLPATH lpStrokeAndFillPath = (PEMRSTROKEANDFILLPATH)mr;*/
1037 StrokeAndFillPath( hdc );
1038 break;
1041 case EMR_STROKEPATH:
1043 /*PEMRSTROKEPATH lpStrokePath = (PEMRSTROKEPATH)mr;*/
1044 StrokePath( hdc );
1045 break;
1048 case EMR_FLATTENPATH:
1050 FlattenPath( hdc );
1051 break;
1054 case EMR_WIDENPATH:
1056 WidenPath( hdc );
1057 break;
1060 case EMR_SELECTCLIPPATH:
1062 PEMRSELECTCLIPPATH lpSelectClipPath = (PEMRSELECTCLIPPATH)mr;
1063 SelectClipPath( hdc, (INT)lpSelectClipPath->iMode );
1064 break;
1067 case EMR_ABORTPATH:
1069 AbortPath( hdc );
1070 break;
1073 case EMR_CREATECOLORSPACE:
1075 PEMRCREATECOLORSPACE lpCreateColorSpace = (PEMRCREATECOLORSPACE)mr;
1076 (handletable->objectHandle)[lpCreateColorSpace->ihCS] =
1077 CreateColorSpaceA( &lpCreateColorSpace->lcs );
1078 break;
1081 case EMR_SETCOLORSPACE:
1083 PEMRSETCOLORSPACE lpSetColorSpace = (PEMRSETCOLORSPACE)mr;
1084 SetColorSpace( hdc,
1085 (handletable->objectHandle)[lpSetColorSpace->ihCS] );
1086 break;
1089 case EMR_DELETECOLORSPACE:
1091 PEMRDELETECOLORSPACE lpDeleteColorSpace = (PEMRDELETECOLORSPACE)mr;
1092 DeleteColorSpace( (handletable->objectHandle)[lpDeleteColorSpace->ihCS] );
1093 break;
1096 case EMR_SETICMMODE:
1098 PERMSETICMMODE lpSetICMMode = (PERMSETICMMODE)mr;
1099 SetICMMode( hdc, (INT)lpSetICMMode->iMode );
1100 break;
1103 case EMR_PIXELFORMAT:
1105 INT iPixelFormat;
1106 PEMRPIXELFORMAT lpPixelFormat = (PEMRPIXELFORMAT)mr;
1108 iPixelFormat = ChoosePixelFormat( hdc, &lpPixelFormat->pfd );
1109 SetPixelFormat( hdc, iPixelFormat, &lpPixelFormat->pfd );
1111 break;
1114 case EMR_SETPALETTEENTRIES:
1116 PEMRSETPALETTEENTRIES lpSetPaletteEntries = (PEMRSETPALETTEENTRIES)mr;
1118 SetPaletteEntries( (handletable->objectHandle)[lpSetPaletteEntries->ihPal],
1119 (UINT)lpSetPaletteEntries->iStart,
1120 (UINT)lpSetPaletteEntries->cEntries,
1121 lpSetPaletteEntries->aPalEntries );
1123 break;
1126 case EMR_RESIZEPALETTE:
1128 PEMRRESIZEPALETTE lpResizePalette = (PEMRRESIZEPALETTE)mr;
1130 ResizePalette( (handletable->objectHandle)[lpResizePalette->ihPal],
1131 (UINT)lpResizePalette->cEntries );
1133 break;
1136 case EMR_CREATEDIBPATTERNBRUSHPT:
1138 PEMRCREATEDIBPATTERNBRUSHPT lpCreate = (PEMRCREATEDIBPATTERNBRUSHPT)mr;
1140 /* This is a BITMAPINFO struct followed directly by bitmap bits */
1141 LPVOID lpPackedStruct = HeapAlloc( GetProcessHeap(),
1143 lpCreate->cbBmi + lpCreate->cbBits );
1144 /* Now pack this structure */
1145 memcpy( lpPackedStruct,
1146 ((BYTE*)lpCreate) + lpCreate->offBmi,
1147 lpCreate->cbBmi );
1148 memcpy( ((BYTE*)lpPackedStruct) + lpCreate->cbBmi,
1149 ((BYTE*)lpCreate) + lpCreate->offBits,
1150 lpCreate->cbBits );
1152 (handletable->objectHandle)[lpCreate->ihBrush] =
1153 CreateDIBPatternBrushPt( lpPackedStruct,
1154 (UINT)lpCreate->iUsage );
1156 break;
1159 case EMR_BITBLT:
1160 case EMR_STRETCHBLT:
1161 case EMR_MASKBLT:
1162 case EMR_PLGBLT:
1163 case EMR_SETDIBITSTODEVICE:
1164 case EMR_EXTTEXTOUTA:
1165 case EMR_POLYDRAW16:
1166 case EMR_CREATEMONOBRUSH:
1167 case EMR_POLYTEXTOUTA:
1168 case EMR_POLYTEXTOUTW:
1169 case EMR_FILLRGN:
1170 case EMR_FRAMERGN:
1171 case EMR_INVERTRGN:
1172 case EMR_PAINTRGN:
1173 case EMR_GLSRECORD:
1174 case EMR_GLSBOUNDEDRECORD:
1175 default:
1176 /* From docs: If PlayEnhMetaFileRecord doesn't recognize a
1177 record then ignore and return TRUE. */
1178 FIXME("type %d is unimplemented\n", type);
1179 break;
1181 return TRUE;
1185 /*****************************************************************************
1187 * EnumEnhMetaFile (GDI32.79)
1189 * Walk an enhanced metafile, calling a user-specified function _EnhMetaFunc_
1190 * for each
1191 * record. Returns when either every record has been used or
1192 * when _EnhMetaFunc_ returns FALSE.
1195 * RETURNS
1196 * TRUE if every record is used, FALSE if any invocation of _EnhMetaFunc_
1197 * returns FALSE.
1199 * BUGS
1200 * Ignores rect.
1202 BOOL WINAPI EnumEnhMetaFile(
1203 HDC hdc, /* device context to pass to _EnhMetaFunc_ */
1204 HENHMETAFILE hmf, /* EMF to walk */
1205 ENHMFENUMPROC callback, /* callback function */
1206 LPVOID data, /* optional data for callback function */
1207 const RECT *lpRect /* bounding rectangle for rendered metafile */
1210 BOOL ret = TRUE;
1211 LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
1212 INT count, i;
1213 HANDLETABLE *ht;
1214 INT savedMode = 0;
1215 FLOAT xSrcPixSize, ySrcPixSize, xscale, yscale;
1216 XFORM savedXform, xform;
1218 if(!emh) {
1219 SetLastError(ERROR_INVALID_HANDLE);
1220 return FALSE;
1222 if(!lpRect) {
1223 SetLastError(ERROR_INVALID_PARAMETER);
1224 return FALSE;
1226 count = emh->nHandles;
1227 ht = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1228 sizeof(HANDLETABLE) * count );
1229 ht->objectHandle[0] = hmf;
1231 xSrcPixSize = (FLOAT) emh->szlMillimeters.cx / emh->szlDevice.cx;
1232 ySrcPixSize = (FLOAT) emh->szlMillimeters.cy / emh->szlDevice.cy;
1233 xscale = (FLOAT)(lpRect->right - lpRect->left) * 100.0 /
1234 (emh->rclFrame.right - emh->rclFrame.left) * xSrcPixSize;
1235 yscale = (FLOAT)(lpRect->bottom - lpRect->top) * 100.0 /
1236 (emh->rclFrame.bottom - emh->rclFrame.top) * ySrcPixSize;
1238 xform.eM11 = xscale;
1239 xform.eM12 = 0;
1240 xform.eM21 = 0;
1241 xform.eM22 = yscale;
1242 if(emh->rclFrame.left || emh->rclFrame.top)
1243 FIXME("Can't cope with nonzero rclFrame origin yet\n");
1244 /* eDx = lpRect->left - (lpRect width) / (rclFrame width) * rclFrame.left ? */
1245 xform.eDx = lpRect->left;
1246 xform.eDy = lpRect->top;
1247 savedMode = SetGraphicsMode(hdc, GM_ADVANCED);
1248 GetWorldTransform(hdc, &savedXform);
1249 if (!ModifyWorldTransform(hdc, &xform, MWT_LEFTMULTIPLY)) {
1250 ERR("World transform failed!\n");
1253 while (ret) {
1254 ret = (*callback)(hdc, ht, (LPENHMETARECORD) emh, count, data);
1255 if (emh->iType == EMR_EOF) break;
1256 emh = (LPENHMETAHEADER) ((char *) emh + emh->nSize);
1258 for(i = 1; i < count; i++) /* Don't delete element 0 (hmf) */
1259 if( (ht->objectHandle)[i] )
1260 DeleteObject( (ht->objectHandle)[i] );
1261 HeapFree( GetProcessHeap(), 0, ht );
1262 EMF_ReleaseEnhMetaHeader(hmf);
1263 SetWorldTransform(hdc, &savedXform);
1264 if (savedMode) SetGraphicsMode(hdc, savedMode);
1265 return ret;
1268 static INT CALLBACK EMF_PlayEnhMetaFileCallback(HDC hdc, HANDLETABLE *ht,
1269 ENHMETARECORD *emr,
1270 INT handles, LPVOID data)
1272 return PlayEnhMetaFileRecord(hdc, ht, emr, handles);
1275 /**************************************************************************
1276 * PlayEnhMetaFile (GDI32.263)
1278 * Renders an enhanced metafile into a specified rectangle *lpRect
1279 * in device context hdc.
1282 BOOL WINAPI PlayEnhMetaFile(
1283 HDC hdc, /* DC to render into */
1284 HENHMETAFILE hmf, /* metafile to render */
1285 const RECT *lpRect /* rectangle to place metafile inside */
1288 return EnumEnhMetaFile(hdc, hmf, EMF_PlayEnhMetaFileCallback, NULL,
1289 lpRect);
1292 /*****************************************************************************
1293 * DeleteEnhMetaFile (GDI32.68)
1295 * Deletes an enhanced metafile and frees the associated storage.
1297 BOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf)
1299 return EMF_Delete_HENHMETAFILE( hmf );
1302 /*****************************************************************************
1303 * CopyEnhMetaFileA (GDI32.21) Duplicate an enhanced metafile
1307 HENHMETAFILE WINAPI CopyEnhMetaFileA(
1308 HENHMETAFILE hmfSrc,
1309 LPCSTR file)
1311 ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
1312 HENHMETAFILE hmfDst;
1314 if(!emrSrc) return FALSE;
1315 if (!file) {
1316 emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
1317 memcpy( emrDst, emrSrc, emrSrc->nBytes );
1318 hmfDst = EMF_Create_HENHMETAFILE( emrDst, 0, 0 );
1319 } else {
1320 HFILE hFile;
1321 hFile = CreateFileA( file, GENERIC_WRITE | GENERIC_READ, 0, NULL,
1322 CREATE_ALWAYS, 0, -1);
1323 WriteFile( hFile, emrSrc, emrSrc->nBytes, 0, 0);
1324 hmfDst = EMF_GetEnhMetaFile( hFile );
1326 EMF_ReleaseEnhMetaHeader( hmfSrc );
1327 return hmfDst;
1331 /* Struct to be used to be passed in the LPVOID parameter for cbEnhPaletteCopy */
1332 typedef struct tagEMF_PaletteCopy
1334 UINT cEntries;
1335 LPPALETTEENTRY lpPe;
1336 } EMF_PaletteCopy;
1338 /***************************************************************
1339 * Find the EMR_EOF record and then use it to find the
1340 * palette entries for this enhanced metafile.
1341 * The lpData is actually a pointer to a EMF_PaletteCopy struct
1342 * which contains the max number of elements to copy and where
1343 * to copy them to.
1345 * NOTE: To be used by GetEnhMetaFilePaletteEntries only!
1347 INT CALLBACK cbEnhPaletteCopy( HDC a,
1348 LPHANDLETABLE b,
1349 LPENHMETARECORD lpEMR,
1350 INT c,
1351 LPVOID lpData )
1354 if ( lpEMR->iType == EMR_EOF )
1356 PEMREOF lpEof = (PEMREOF)lpEMR;
1357 EMF_PaletteCopy* info = (EMF_PaletteCopy*)lpData;
1358 DWORD dwNumPalToCopy = min( lpEof->nPalEntries, info->cEntries );
1360 TRACE( "copying 0x%08lx palettes\n", dwNumPalToCopy );
1362 memcpy( (LPVOID)info->lpPe,
1363 (LPVOID)(((LPSTR)lpEof) + lpEof->offPalEntries),
1364 sizeof( *(info->lpPe) ) * dwNumPalToCopy );
1366 /* Update the passed data as a return code */
1367 info->lpPe = NULL; /* Palettes were copied! */
1368 info->cEntries = (UINT)dwNumPalToCopy;
1370 return FALSE; /* That's all we need */
1373 return TRUE;
1376 /*****************************************************************************
1377 * GetEnhMetaFilePaletteEntries (GDI32.179)
1379 * Copy the palette and report size
1381 * BUGS: Error codes (SetLastError) are not set on failures
1383 UINT WINAPI GetEnhMetaFilePaletteEntries( HENHMETAFILE hEmf,
1384 UINT cEntries,
1385 LPPALETTEENTRY lpPe )
1387 ENHMETAHEADER* enhHeader = EMF_GetEnhMetaHeader( hEmf );
1388 UINT uReturnValue = GDI_ERROR;
1389 EMF_PaletteCopy infoForCallBack;
1391 TRACE( "(%04x,%d,%p)\n", hEmf, cEntries, lpPe );
1393 /* First check if there are any palettes associated with
1394 this metafile. */
1395 if ( enhHeader->nPalEntries == 0 )
1397 /* No palette associated with this enhanced metafile */
1398 uReturnValue = 0;
1399 goto done;
1402 /* Is the user requesting the number of palettes? */
1403 if ( lpPe == NULL )
1405 uReturnValue = (UINT)enhHeader->nPalEntries;
1406 goto done;
1409 /* Copy cEntries worth of PALETTEENTRY structs into the buffer */
1410 infoForCallBack.cEntries = cEntries;
1411 infoForCallBack.lpPe = lpPe;
1413 if ( !EnumEnhMetaFile( 0, hEmf, cbEnhPaletteCopy,
1414 &infoForCallBack, NULL ) )
1416 goto done;
1419 /* Verify that the callback executed correctly */
1420 if ( infoForCallBack.lpPe != NULL )
1422 /* Callback proc had error! */
1423 ERR( "cbEnhPaletteCopy didn't execute correctly\n" );
1424 goto done;
1427 uReturnValue = infoForCallBack.cEntries;
1429 done:
1431 EMF_ReleaseEnhMetaHeader( hEmf );
1433 return uReturnValue;
1436 /******************************************************************
1437 * SetWinMetaFileBits (GDI32.343)
1439 * Translate from old style to new style.
1441 * BUGS: - This doesn't take the DC and scaling into account
1442 * - Most record conversions aren't implemented
1443 * - Handle slot assignement is primative and most likely doesn't work
1445 HENHMETAFILE WINAPI SetWinMetaFileBits(UINT cbBuffer,
1446 CONST BYTE *lpbBuffer,
1447 HDC hdcRef,
1448 CONST METAFILEPICT *lpmfp
1451 HENHMETAFILE hMf;
1452 LPVOID lpNewEnhMetaFileBuffer = NULL;
1453 UINT uNewEnhMetaFileBufferSize = 0;
1454 BOOL bFoundEOF = FALSE;
1456 FIXME( "(%d,%p,%04x,%p):stub\n", cbBuffer, lpbBuffer, hdcRef, lpmfp );
1458 /* 1. Get the header - skip over this and get straight to the records */
1460 uNewEnhMetaFileBufferSize = sizeof( ENHMETAHEADER );
1461 lpNewEnhMetaFileBuffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1462 uNewEnhMetaFileBufferSize );
1464 if( lpNewEnhMetaFileBuffer == NULL )
1466 goto error;
1469 /* Fill in the header record */
1471 LPENHMETAHEADER lpNewEnhMetaFileHeader = (LPENHMETAHEADER)lpNewEnhMetaFileBuffer;
1473 lpNewEnhMetaFileHeader->iType = EMR_HEADER;
1474 lpNewEnhMetaFileHeader->nSize = sizeof( ENHMETAHEADER );
1476 /* FIXME: Not right. Must be able to get this from the DC */
1477 lpNewEnhMetaFileHeader->rclBounds.left = 0;
1478 lpNewEnhMetaFileHeader->rclBounds.right = 0;
1479 lpNewEnhMetaFileHeader->rclBounds.top = 0;
1480 lpNewEnhMetaFileHeader->rclBounds.bottom = 0;
1482 /* FIXME: Not right. Must be able to get this from the DC */
1483 lpNewEnhMetaFileHeader->rclFrame.left = 0;
1484 lpNewEnhMetaFileHeader->rclFrame.right = 0;
1485 lpNewEnhMetaFileHeader->rclFrame.top = 0;
1486 lpNewEnhMetaFileHeader->rclFrame.bottom = 0;
1488 lpNewEnhMetaFileHeader->nHandles = 0; /* No handles yet */
1490 /* FIXME: Add in the rest of the fields to the header */
1491 /* dSignature
1492 nVersion
1493 nRecords
1494 sReserved
1495 nDescription
1496 offDescription
1497 nPalEntries
1498 szlDevice
1499 szlMillimeters
1500 cbPixelFormat
1501 offPixelFormat,
1502 bOpenGL */
1505 (char*)lpbBuffer += ((METAHEADER*)lpbBuffer)->mtHeaderSize * 2; /* Point past the header - FIXME: metafile quirk? */
1507 /* 2. Enum over individual records and convert them to the new type of records */
1508 while( !bFoundEOF )
1511 LPMETARECORD lpMetaRecord = (LPMETARECORD)lpbBuffer;
1513 #define EMF_ReAllocAndAdjustPointers( a , b ) \
1515 LPVOID lpTmp; \
1516 lpTmp = HeapReAlloc( GetProcessHeap(), 0, \
1517 lpNewEnhMetaFileBuffer, \
1518 uNewEnhMetaFileBufferSize + (b) ); \
1519 if( lpTmp == NULL ) { ERR( "No memory!\n" ); goto error; } \
1520 lpNewEnhMetaFileBuffer = lpTmp; \
1521 lpRecord = (a)( (char*)lpNewEnhMetaFileBuffer + uNewEnhMetaFileBufferSize ); \
1522 uNewEnhMetaFileBufferSize += (b); \
1525 switch( lpMetaRecord->rdFunction )
1527 case META_EOF:
1529 PEMREOF lpRecord;
1530 size_t uRecord = sizeof(*lpRecord);
1532 EMF_ReAllocAndAdjustPointers(PEMREOF,uRecord);
1534 /* Fill the new record - FIXME: This is not right */
1535 lpRecord->emr.iType = EMR_EOF;
1536 lpRecord->emr.nSize = sizeof( *lpRecord );
1537 lpRecord->nPalEntries = 0; /* FIXME */
1538 lpRecord->offPalEntries = 0; /* FIXME */
1539 lpRecord->nSizeLast = 0; /* FIXME */
1541 /* No more records after this one */
1542 bFoundEOF = TRUE;
1544 FIXME( "META_EOF conversion not correct\n" );
1545 break;
1548 case META_SETMAPMODE:
1550 PEMRSETMAPMODE lpRecord;
1551 size_t uRecord = sizeof(*lpRecord);
1553 EMF_ReAllocAndAdjustPointers(PEMRSETMAPMODE,uRecord);
1555 lpRecord->emr.iType = EMR_SETMAPMODE;
1556 lpRecord->emr.nSize = sizeof( *lpRecord );
1558 lpRecord->iMode = lpMetaRecord->rdParm[0];
1560 break;
1563 case META_DELETEOBJECT: /* Select and Delete structures are the same */
1564 case META_SELECTOBJECT:
1566 PEMRDELETEOBJECT lpRecord;
1567 size_t uRecord = sizeof(*lpRecord);
1569 EMF_ReAllocAndAdjustPointers(PEMRDELETEOBJECT,uRecord);
1571 if( lpMetaRecord->rdFunction == META_DELETEOBJECT )
1573 lpRecord->emr.iType = EMR_DELETEOBJECT;
1575 else
1577 lpRecord->emr.iType = EMR_SELECTOBJECT;
1579 lpRecord->emr.nSize = sizeof( *lpRecord );
1581 lpRecord->ihObject = lpMetaRecord->rdParm[0]; /* FIXME: Handle */
1583 break;
1586 case META_POLYGON: /* This is just plain busted. I don't know what I'm doing */
1588 PEMRPOLYGON16 lpRecord; /* FIXME: Should it be a poly or poly16? */
1589 size_t uRecord = sizeof(*lpRecord);
1591 EMF_ReAllocAndAdjustPointers(PEMRPOLYGON16,uRecord);
1593 /* FIXME: This is mostly all wrong */
1594 lpRecord->emr.iType = EMR_POLYGON16;
1595 lpRecord->emr.nSize = sizeof( *lpRecord );
1597 lpRecord->rclBounds.left = 0;
1598 lpRecord->rclBounds.right = 0;
1599 lpRecord->rclBounds.top = 0;
1600 lpRecord->rclBounds.bottom = 0;
1602 lpRecord->cpts = 0;
1603 lpRecord->apts[0].x = 0;
1604 lpRecord->apts[0].y = 0;
1606 FIXME( "META_POLYGON conversion not correct\n" );
1608 break;
1611 case META_SETPOLYFILLMODE:
1613 PEMRSETPOLYFILLMODE lpRecord;
1614 size_t uRecord = sizeof(*lpRecord);
1616 EMF_ReAllocAndAdjustPointers(PEMRSETPOLYFILLMODE,uRecord);
1618 lpRecord->emr.iType = EMR_SETPOLYFILLMODE;
1619 lpRecord->emr.nSize = sizeof( *lpRecord );
1621 lpRecord->iMode = lpMetaRecord->rdParm[0];
1623 break;
1626 case META_SETWINDOWORG:
1628 PEMRSETWINDOWORGEX lpRecord; /* Seems to be the closest thing */
1629 size_t uRecord = sizeof(*lpRecord);
1631 EMF_ReAllocAndAdjustPointers(PEMRSETWINDOWORGEX,uRecord);
1633 lpRecord->emr.iType = EMR_SETWINDOWORGEX;
1634 lpRecord->emr.nSize = sizeof( *lpRecord );
1636 lpRecord->ptlOrigin.x = lpMetaRecord->rdParm[1];
1637 lpRecord->ptlOrigin.y = lpMetaRecord->rdParm[0];
1639 break;
1642 case META_SETWINDOWEXT: /* Structure is the same for SETWINDOWEXT & SETVIEWPORTEXT */
1643 case META_SETVIEWPORTEXT:
1645 PEMRSETWINDOWEXTEX lpRecord;
1646 size_t uRecord = sizeof(*lpRecord);
1648 EMF_ReAllocAndAdjustPointers(PEMRSETWINDOWEXTEX,uRecord);
1650 if ( lpMetaRecord->rdFunction == META_SETWINDOWEXT )
1652 lpRecord->emr.iType = EMR_SETWINDOWORGEX;
1654 else
1656 lpRecord->emr.iType = EMR_SETVIEWPORTEXTEX;
1658 lpRecord->emr.nSize = sizeof( *lpRecord );
1660 lpRecord->szlExtent.cx = lpMetaRecord->rdParm[1];
1661 lpRecord->szlExtent.cy = lpMetaRecord->rdParm[0];
1663 break;
1666 case META_CREATEBRUSHINDIRECT:
1668 PEMRCREATEBRUSHINDIRECT lpRecord;
1669 size_t uRecord = sizeof(*lpRecord);
1671 EMF_ReAllocAndAdjustPointers(PEMRCREATEBRUSHINDIRECT,uRecord);
1673 lpRecord->emr.iType = EMR_CREATEBRUSHINDIRECT;
1674 lpRecord->emr.nSize = sizeof( *lpRecord );
1676 lpRecord->ihBrush = ((LPENHMETAHEADER)lpNewEnhMetaFileBuffer)->nHandles;
1677 lpRecord->lb.lbStyle = ((LPLOGBRUSH16)lpMetaRecord->rdParm)->lbStyle;
1678 lpRecord->lb.lbColor = ((LPLOGBRUSH16)lpMetaRecord->rdParm)->lbColor;
1679 lpRecord->lb.lbHatch = ((LPLOGBRUSH16)lpMetaRecord->rdParm)->lbHatch;
1681 ((LPENHMETAHEADER)lpNewEnhMetaFileBuffer)->nHandles += 1; /* New handle */
1683 break;
1687 /* These are all unimplemented and as such are intended to fall through to the default case */
1688 case META_SETBKCOLOR:
1689 case META_SETBKMODE:
1690 case META_SETROP2:
1691 case META_SETRELABS:
1692 case META_SETSTRETCHBLTMODE:
1693 case META_SETTEXTCOLOR:
1694 case META_SETVIEWPORTORG:
1695 case META_OFFSETWINDOWORG:
1696 case META_SCALEWINDOWEXT:
1697 case META_OFFSETVIEWPORTORG:
1698 case META_SCALEVIEWPORTEXT:
1699 case META_LINETO:
1700 case META_MOVETO:
1701 case META_EXCLUDECLIPRECT:
1702 case META_INTERSECTCLIPRECT:
1703 case META_ARC:
1704 case META_ELLIPSE:
1705 case META_FLOODFILL:
1706 case META_PIE:
1707 case META_RECTANGLE:
1708 case META_ROUNDRECT:
1709 case META_PATBLT:
1710 case META_SAVEDC:
1711 case META_SETPIXEL:
1712 case META_OFFSETCLIPRGN:
1713 case META_TEXTOUT:
1714 case META_POLYPOLYGON:
1715 case META_POLYLINE:
1716 case META_RESTOREDC:
1717 case META_CHORD:
1718 case META_CREATEPATTERNBRUSH:
1719 case META_CREATEPENINDIRECT:
1720 case META_CREATEFONTINDIRECT:
1721 case META_CREATEPALETTE:
1722 case META_SETTEXTALIGN:
1723 case META_SELECTPALETTE:
1724 case META_SETMAPPERFLAGS:
1725 case META_REALIZEPALETTE:
1726 case META_ESCAPE:
1727 case META_EXTTEXTOUT:
1728 case META_STRETCHDIB:
1729 case META_DIBSTRETCHBLT:
1730 case META_STRETCHBLT:
1731 case META_BITBLT:
1732 case META_CREATEREGION:
1733 case META_FILLREGION:
1734 case META_FRAMEREGION:
1735 case META_INVERTREGION:
1736 case META_PAINTREGION:
1737 case META_SELECTCLIPREGION:
1738 case META_DIBCREATEPATTERNBRUSH:
1739 case META_DIBBITBLT:
1740 case META_SETTEXTCHAREXTRA:
1741 case META_SETTEXTJUSTIFICATION:
1742 case META_EXTFLOODFILL:
1743 case META_SETDIBTODEV:
1744 case META_DRAWTEXT:
1745 case META_ANIMATEPALETTE:
1746 case META_SETPALENTRIES:
1747 case META_RESIZEPALETTE:
1748 case META_RESETDC:
1749 case META_STARTDOC:
1750 case META_STARTPAGE:
1751 case META_ENDPAGE:
1752 case META_ABORTDOC:
1753 case META_ENDDOC:
1754 case META_CREATEBRUSH:
1755 case META_CREATEBITMAPINDIRECT:
1756 case META_CREATEBITMAP:
1757 /* Fall through to unimplemented */
1758 default:
1760 /* Not implemented yet */
1761 FIXME( "Conversion of record type 0x%x not implemented.\n", lpMetaRecord->rdFunction );
1762 break;
1766 /* Move to the next record */
1767 (char*)lpbBuffer += ((LPMETARECORD)lpbBuffer)->rdSize * 2; /* FIXME: Seem to be doing this in metafile.c */
1769 #undef ReAllocAndAdjustPointers
1772 /* We know the last of the header information now */
1773 ((LPENHMETAHEADER)lpNewEnhMetaFileBuffer)->nBytes = uNewEnhMetaFileBufferSize;
1775 /* Create the enhanced metafile */
1776 hMf = SetEnhMetaFileBits( uNewEnhMetaFileBufferSize, (const BYTE*)lpNewEnhMetaFileBuffer );
1778 if( !hMf )
1779 ERR( "Problem creating metafile. Did the conversion fail somewhere?\n" );
1781 return hMf;
1783 error:
1784 /* Free the data associated with our copy since it's been copied */
1785 HeapFree( GetProcessHeap(), 0, lpNewEnhMetaFileBuffer );
1787 return 0;