4 * Copyright David W. Metcalfe, 1994
5 * Copyright Niels de Carpentier, 1996
6 * Copyright Albrecht Kleine, 1996
7 * Copyright Huw Davies, 1996
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * These functions are primarily involved with metafile playback or anything
26 * that touches a HMETAFILE.
27 * For recording of metafiles look in graphics/metafiledrv/
29 * Note that (32 bit) HMETAFILEs are GDI objects, while HMETAFILE16s are
30 * global memory handles so these cannot be interchanged.
32 * Memory-based metafiles are just stored as a continuous block of memory with
33 * a METAHEADER at the head with METARECORDs appended to it. mtType is
34 * METAFILE_MEMORY (1). Note this is identical to the disk image of a
35 * disk-based metafile - even mtType is METAFILE_MEMORY.
36 * 16bit HMETAFILE16s are global handles to this block
37 * 32bit HMETAFILEs are GDI handles METAFILEOBJs, which contains a ptr to
39 * Disk-based metafiles are rather different. HMETAFILE16s point to a
40 * METAHEADER which has mtType equal to METAFILE_DISK (2). Following the 9
41 * WORDs of the METAHEADER there are a further 3 WORDs of 0, 1 of 0x117, 1
42 * more 0, then 2 which may be a time stamp of the file and then the path of
43 * the file (METAHEADERDISK). I've copied this for 16bit compatibility.
60 #include "gdi_private.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
81 /******************************************************************
84 * Add a handle to an external handle table and return the index
86 static int MF_AddHandle(HANDLETABLE *ht, UINT htlen, HGDIOBJ hobj)
90 for (i = 0; i < htlen; i++)
92 if (*(ht->objectHandle + i) == 0)
94 *(ht->objectHandle + i) = hobj;
102 /******************************************************************
103 * MF_Create_HMETATFILE
105 * Creates a (32 bit) HMETAFILE object from a METAHEADER
107 * HMETAFILEs are GDI objects.
109 HMETAFILE MF_Create_HMETAFILE(METAHEADER *mh)
112 METAFILEOBJ *metaObj;
114 if (!(metaObj = HeapAlloc( GetProcessHeap(), 0, sizeof(*metaObj) ))) return 0;
116 if (!(hmf = alloc_gdi_handle( &metaObj->header, OBJ_METAFILE, NULL )))
117 HeapFree( GetProcessHeap(), 0, metaObj );
121 /******************************************************************
124 * Returns ptr to METAHEADER associated with HMETAFILE
126 static METAHEADER *MF_GetMetaHeader( HMETAFILE hmf )
128 METAHEADER *ret = NULL;
129 METAFILEOBJ * metaObj = GDI_GetObjPtr( hmf, OBJ_METAFILE );
133 GDI_ReleaseObj( hmf );
138 /******************************************************************
141 * Convert an array of POINTS to an array of POINT.
142 * Result must be freed by caller.
144 static POINT *convert_points( UINT count, const POINTS *pts )
147 POINT *ret = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*ret) );
150 for (i = 0; i < count; i++)
159 /******************************************************************
160 * DeleteMetaFile (GDI32.@)
162 * Delete a memory-based metafile.
165 BOOL WINAPI DeleteMetaFile( HMETAFILE hmf )
167 METAFILEOBJ * metaObj = free_gdi_handle( hmf );
168 if (!metaObj) return FALSE;
169 HeapFree( GetProcessHeap(), 0, metaObj->mh );
170 return HeapFree( GetProcessHeap(), 0, metaObj );
173 /******************************************************************
176 * Returns a pointer to a memory based METAHEADER read in from file HFILE
179 static METAHEADER *MF_ReadMetaFile(HANDLE hfile)
182 DWORD BytesRead, size;
184 size = sizeof(METAHEADER);
185 mh = HeapAlloc( GetProcessHeap(), 0, size );
187 if(ReadFile( hfile, mh, size, &BytesRead, NULL) == 0 ||
189 HeapFree( GetProcessHeap(), 0, mh );
192 if (mh->mtType != METAFILE_MEMORY || mh->mtVersion != MFVERSION ||
193 mh->mtHeaderSize != size / 2)
195 HeapFree( GetProcessHeap(), 0, mh );
198 size = mh->mtSize * 2;
199 mh = HeapReAlloc( GetProcessHeap(), 0, mh, size );
201 size -= sizeof(METAHEADER);
202 if(ReadFile( hfile, (char *)mh + sizeof(METAHEADER), size, &BytesRead,
205 HeapFree( GetProcessHeap(), 0, mh );
209 if (mh->mtType != METAFILE_MEMORY) {
210 WARN("Disk metafile had mtType = %04x\n", mh->mtType);
211 mh->mtType = METAFILE_MEMORY;
216 /******************************************************************
217 * GetMetaFileA (GDI32.@)
219 * Read a metafile from a file. Returns handle to a memory-based metafile.
221 HMETAFILE WINAPI GetMetaFileA( LPCSTR lpFilename )
226 TRACE("%s\n", lpFilename);
231 if((hFile = CreateFileA(lpFilename, GENERIC_READ, FILE_SHARE_READ, NULL,
232 OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
235 mh = MF_ReadMetaFile(hFile);
238 return MF_Create_HMETAFILE( mh );
241 /******************************************************************
242 * GetMetaFileW (GDI32.@)
244 HMETAFILE WINAPI GetMetaFileW( LPCWSTR lpFilename )
249 TRACE("%s\n", debugstr_w(lpFilename));
254 if((hFile = CreateFileW(lpFilename, GENERIC_READ, FILE_SHARE_READ, NULL,
255 OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
258 mh = MF_ReadMetaFile(hFile);
261 return MF_Create_HMETAFILE( mh );
265 /******************************************************************
266 * MF_LoadDiskBasedMetaFile
268 * Creates a new memory-based metafile from a disk-based one.
270 static METAHEADER *MF_LoadDiskBasedMetaFile(METAHEADER *mh)
276 if(mh->mtType != METAFILE_DISK) {
277 ERR("Not a disk based metafile\n");
280 mhd = (METAHEADERDISK *)((char *)mh + sizeof(METAHEADER));
282 if((hfile = CreateFileA(mhd->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
283 OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE) {
284 WARN("Can't open file of disk based metafile\n");
287 mh2 = MF_ReadMetaFile(hfile);
292 /******************************************************************
293 * MF_CreateMetaHeaderDisk
295 * Take a memory based METAHEADER and change it to a disk based METAHEADER
296 * associated with filename. Note: Trashes contents of old one.
298 METAHEADER *MF_CreateMetaHeaderDisk(METAHEADER *mh, LPCVOID filename, BOOL uni )
302 mh = HeapReAlloc( GetProcessHeap(), 0, mh,
303 sizeof(METAHEADER) + sizeof(METAHEADERDISK));
304 mh->mtType = METAFILE_DISK;
305 mhd = (METAHEADERDISK *)((char *)mh + sizeof(METAHEADER));
308 WideCharToMultiByte(CP_ACP, 0, filename, -1,
309 mhd->filename, sizeof mhd->filename, NULL, NULL);
311 lstrcpynA( mhd->filename, filename, sizeof mhd->filename );
315 /******************************************************************
316 * CopyMetaFileW (GDI32.@)
318 * Copies the metafile corresponding to hSrcMetaFile to either
319 * a disk file, if a filename is given, or to a new memory based
320 * metafile, if lpFileName is NULL.
323 * hSrcMetaFile [I] handle of metafile to copy
324 * lpFilename [I] filename if copying to a file
327 * Handle to metafile copy on success, NULL on failure.
330 * Copying to disk returns NULL even if successful.
332 HMETAFILE WINAPI CopyMetaFileW( HMETAFILE hSrcMetaFile, LPCWSTR lpFilename )
334 METAHEADER *mh = MF_GetMetaHeader( hSrcMetaFile );
335 METAHEADER *mh2 = NULL;
338 TRACE("(%p,%s)\n", hSrcMetaFile, debugstr_w(lpFilename));
342 if(mh->mtType == METAFILE_DISK)
343 mh2 = MF_LoadDiskBasedMetaFile(mh);
345 mh2 = HeapAlloc( GetProcessHeap(), 0, mh->mtSize * 2 );
346 memcpy( mh2, mh, mh->mtSize * 2 );
349 if(lpFilename) { /* disk based metafile */
351 if((hFile = CreateFileW(lpFilename, GENERIC_WRITE, 0, NULL,
352 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
353 HeapFree( GetProcessHeap(), 0, mh2 );
356 WriteFile(hFile, mh2, mh2->mtSize * 2, &w, NULL);
360 return MF_Create_HMETAFILE( mh2 );
364 /******************************************************************
365 * CopyMetaFileA (GDI32.@)
369 HMETAFILE WINAPI CopyMetaFileA( HMETAFILE hSrcMetaFile, LPCSTR lpFilename )
371 UNICODE_STRING lpFilenameW;
374 if (lpFilename) RtlCreateUnicodeStringFromAsciiz(&lpFilenameW, lpFilename);
375 else lpFilenameW.Buffer = NULL;
377 ret = CopyMetaFileW( hSrcMetaFile, lpFilenameW.Buffer );
378 if (lpFilenameW.Buffer)
379 RtlFreeUnicodeString(&lpFilenameW);
383 /*******************************************************************
386 * Helper for PlayMetaFile
388 static BOOL MF_PlayMetaFile( HDC hdc, METAHEADER *mh)
393 unsigned int offset = 0;
402 if (!mh) return FALSE;
403 if(mh->mtType == METAFILE_DISK) { /* Create a memory-based copy */
404 mh = MF_LoadDiskBasedMetaFile(mh);
405 if(!mh) return FALSE;
410 hPen = GetCurrentObject(hdc, OBJ_PEN);
411 hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
412 hFont = GetCurrentObject(hdc, OBJ_FONT);
413 hPal = GetCurrentObject(hdc, OBJ_PAL);
415 hRgn = CreateRectRgn(0, 0, 0, 0);
416 if (!GetClipRgn(hdc, hRgn))
422 /* create the handle table */
423 ht = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
424 sizeof(HANDLETABLE) * mh->mtNoObjects);
425 if(!ht) return FALSE;
427 /* loop through metafile playing records */
428 offset = mh->mtHeaderSize * 2;
429 while (offset < mh->mtSize * 2)
431 mr = (METARECORD *)((char *)mh + offset);
432 TRACE("offset=%04x,size=%08x\n",
434 if (mr->rdSize < 3) { /* catch illegal record sizes */
435 TRACE("Entry got size %d at offset %d, total mf length is %d\n",
436 mr->rdSize,offset,mh->mtSize*2);
440 offset += mr->rdSize * 2;
441 if (mr->rdFunction == META_EOF) {
442 TRACE("Got META_EOF so stopping\n");
445 PlayMetaFileRecord( hdc, ht, mr, mh->mtNoObjects );
449 SelectObject(hdc, hPen);
450 SelectObject(hdc, hBrush);
451 SelectPalette(hdc, hPal, FALSE);
452 ExtSelectClipRgn(hdc, hRgn, RGN_COPY);
455 /* free objects in handle table */
456 for(i = 0; i < mh->mtNoObjects; i++)
457 if(*(ht->objectHandle + i) != 0)
458 DeleteObject(*(ht->objectHandle + i));
460 /* free handle table */
461 HeapFree( GetProcessHeap(), 0, ht );
463 HeapFree( GetProcessHeap(), 0, mh );
467 /******************************************************************
468 * PlayMetaFile (GDI32.@)
470 * Renders the metafile specified by hmf in the DC specified by
471 * hdc. Returns FALSE on failure, TRUE on success.
474 * hdc [I] handle of DC to render in
475 * hmf [I] handle of metafile to render
481 BOOL WINAPI PlayMetaFile( HDC hdc, HMETAFILE hmf )
483 METAHEADER *mh = MF_GetMetaHeader( hmf );
484 return MF_PlayMetaFile( hdc, mh );
487 /******************************************************************
488 * EnumMetaFile (GDI32.@)
490 * Loop through the metafile records in hmf, calling the user-specified
491 * function for each one, stopping when the user's function returns FALSE
492 * (which is considered to be failure)
493 * or when no records are left (which is considered to be success).
496 * TRUE on success, FALSE on failure.
498 BOOL WINAPI EnumMetaFile(HDC hdc, HMETAFILE hmf, MFENUMPROC lpEnumFunc, LPARAM lpData)
500 METAHEADER *mhTemp = NULL, *mh = MF_GetMetaHeader(hmf);
505 unsigned int offset = 0;
510 TRACE("(%p,%p,%p,%p)\n", hdc, hmf, lpEnumFunc, (void*)lpData);
512 if(mh->mtType == METAFILE_DISK)
514 /* Create a memory-based copy */
515 if (!(mhTemp = MF_LoadDiskBasedMetaFile(mh))) return FALSE;
519 /* save the current pen, brush and font */
520 hPen = GetCurrentObject(hdc, OBJ_PEN);
521 hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
522 hFont = GetCurrentObject(hdc, OBJ_FONT);
524 ht = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
525 sizeof(HANDLETABLE) * mh->mtNoObjects);
527 /* loop through metafile records */
528 offset = mh->mtHeaderSize * 2;
530 while (offset < (mh->mtSize * 2))
532 mr = (METARECORD *)((char *)mh + offset);
533 if(mr->rdFunction == META_EOF) {
534 TRACE("Got META_EOF so stopping\n");
537 TRACE("Calling EnumFunc with record type %x\n",
539 if (!lpEnumFunc( hdc, ht, mr, mh->mtNoObjects, (LONG)lpData ))
545 offset += (mr->rdSize * 2);
548 /* restore pen, brush and font */
549 SelectObject(hdc, hBrush);
550 SelectObject(hdc, hPen);
551 SelectObject(hdc, hFont);
553 /* free objects in handle table */
554 for(i = 0; i < mh->mtNoObjects; i++)
555 if(*(ht->objectHandle + i) != 0)
556 DeleteObject(*(ht->objectHandle + i));
558 /* free handle table */
559 HeapFree( GetProcessHeap(), 0, ht);
560 /* free a copy of metafile */
561 HeapFree( GetProcessHeap(), 0, mhTemp );
565 static BOOL MF_Play_MetaCreateRegion( METARECORD *mr, HRGN hrgn );
566 static BOOL MF_Play_MetaExtTextOut(HDC hdc, METARECORD *mr);
567 /******************************************************************
568 * PlayMetaFileRecord (GDI32.@)
570 * Render a single metafile record specified by *mr in the DC hdc, while
571 * using the handle table *ht, of length handles,
572 * to store metafile objects.
575 * The following metafile records are unimplemented:
577 * DRAWTEXT, ANIMATEPALETTE, SETPALENTRIES,
578 * RESIZEPALETTE, EXTFLOODFILL, RESETDC, STARTDOC, STARTPAGE, ENDPAGE,
579 * ABORTDOC, ENDDOC, CREATEBRUSH, CREATEBITMAPINDIRECT, and CREATEBITMAP.
581 BOOL WINAPI PlayMetaFileRecord( HDC hdc, HANDLETABLE *ht, METARECORD *mr, UINT handles )
585 BITMAPINFOHEADER *infohdr;
587 TRACE("(%p %p %p %u) function %04x\n", hdc, ht, mr, handles, mr->rdFunction);
589 switch (mr->rdFunction)
594 case META_DELETEOBJECT:
595 DeleteObject(*(ht->objectHandle + mr->rdParm[0]));
596 *(ht->objectHandle + mr->rdParm[0]) = 0;
599 case META_SETBKCOLOR:
600 SetBkColor(hdc, MAKELONG(mr->rdParm[0], mr->rdParm[1]));
604 SetBkMode(hdc, mr->rdParm[0]);
607 case META_SETMAPMODE:
608 SetMapMode(hdc, mr->rdParm[0]);
612 SetROP2(hdc, mr->rdParm[0]);
616 SetRelAbs(hdc, mr->rdParm[0]);
619 case META_SETPOLYFILLMODE:
620 SetPolyFillMode(hdc, mr->rdParm[0]);
623 case META_SETSTRETCHBLTMODE:
624 SetStretchBltMode(hdc, mr->rdParm[0]);
627 case META_SETTEXTCOLOR:
628 SetTextColor(hdc, MAKELONG(mr->rdParm[0], mr->rdParm[1]));
631 case META_SETWINDOWORG:
632 SetWindowOrgEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
635 case META_SETWINDOWEXT:
636 SetWindowExtEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
639 case META_SETVIEWPORTORG:
640 SetViewportOrgEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
643 case META_SETVIEWPORTEXT:
644 SetViewportExtEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
647 case META_OFFSETWINDOWORG:
648 OffsetWindowOrgEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
651 case META_SCALEWINDOWEXT:
652 ScaleWindowExtEx(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
653 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
656 case META_OFFSETVIEWPORTORG:
657 OffsetViewportOrgEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
660 case META_SCALEVIEWPORTEXT:
661 ScaleViewportExtEx(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
662 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
666 LineTo(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
670 MoveToEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
673 case META_EXCLUDECLIPRECT:
674 ExcludeClipRect( hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
675 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0] );
678 case META_INTERSECTCLIPRECT:
679 IntersectClipRect( hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
680 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0] );
684 Arc(hdc, (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6],
685 (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
686 (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
687 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
691 Ellipse(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
692 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
696 FloodFill(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
697 MAKELONG(mr->rdParm[0], mr->rdParm[1]));
701 Pie(hdc, (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6],
702 (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
703 (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
704 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
708 Rectangle(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
709 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
713 RoundRect(hdc, (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
714 (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
715 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
719 PatBlt(hdc, (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
720 (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
721 MAKELONG(mr->rdParm[0], mr->rdParm[1]));
729 SetPixel(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
730 MAKELONG(mr->rdParm[0], mr->rdParm[1]));
733 case META_OFFSETCLIPRGN:
734 OffsetClipRgn( hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0] );
739 TextOutA(hdc, (SHORT)mr->rdParm[((s1 + 1) >> 1) + 2],
740 (SHORT)mr->rdParm[((s1 + 1) >> 1) + 1],
741 (char *)(mr->rdParm + 1), s1);
745 if ((pt = convert_points( mr->rdParm[0], (POINTS *)(mr->rdParm + 1))))
747 Polygon(hdc, pt, mr->rdParm[0]);
748 HeapFree( GetProcessHeap(), 0, pt );
752 case META_POLYPOLYGON:
755 SHORT *counts = (SHORT *)(mr->rdParm + 1);
757 for (i = total = 0; i < mr->rdParm[0]; i++) total += counts[i];
758 pt = convert_points( total, (POINTS *)(counts + mr->rdParm[0]) );
761 INT *cnt32 = HeapAlloc( GetProcessHeap(), 0, mr->rdParm[0] * sizeof(*cnt32) );
764 for (i = 0; i < mr->rdParm[0]; i++) cnt32[i] = counts[i];
765 PolyPolygon( hdc, pt, cnt32, mr->rdParm[0]);
766 HeapFree( GetProcessHeap(), 0, cnt32 );
769 HeapFree( GetProcessHeap(), 0, pt );
774 if ((pt = convert_points( mr->rdParm[0], (POINTS *)(mr->rdParm + 1))))
776 Polyline( hdc, pt, mr->rdParm[0] );
777 HeapFree( GetProcessHeap(), 0, pt );
782 RestoreDC(hdc, (SHORT)mr->rdParm[0]);
785 case META_SELECTOBJECT:
786 SelectObject(hdc, *(ht->objectHandle + mr->rdParm[0]));
790 Chord(hdc, (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6],
791 (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
792 (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
793 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
796 case META_CREATEPATTERNBRUSH:
797 switch (mr->rdParm[0])
800 infohdr = (BITMAPINFOHEADER *)(mr->rdParm + 2);
801 MF_AddHandle(ht, handles,
802 CreatePatternBrush(CreateBitmap(infohdr->biWidth,
807 (sizeof(BITMAPINFOHEADER) / 2) + 4)));
811 infohdr = (BITMAPINFOHEADER *)(mr->rdParm + 2);
812 MF_AddHandle(ht, handles, CreateDIBPatternBrushPt( infohdr, mr->rdParm[1] ));
816 ERR("META_CREATEPATTERNBRUSH: Unknown pattern type %d\n",
822 case META_CREATEPENINDIRECT:
825 pen.lopnStyle = mr->rdParm[0];
826 pen.lopnWidth.x = (SHORT)mr->rdParm[1];
827 pen.lopnWidth.y = (SHORT)mr->rdParm[2];
828 pen.lopnColor = MAKELONG( mr->rdParm[3], mr->rdParm[4] );
829 MF_AddHandle(ht, handles, CreatePenIndirect( &pen ));
833 case META_CREATEFONTINDIRECT:
836 font.lfHeight = (SHORT)mr->rdParm[0];
837 font.lfWidth = (SHORT)mr->rdParm[1];
838 font.lfEscapement = (SHORT)mr->rdParm[2];
839 font.lfOrientation = (SHORT)mr->rdParm[3];
840 font.lfWeight = (SHORT)mr->rdParm[4];
841 font.lfItalic = LOBYTE(mr->rdParm[5]);
842 font.lfUnderline = HIBYTE(mr->rdParm[5]);
843 font.lfStrikeOut = LOBYTE(mr->rdParm[6]);
844 font.lfCharSet = HIBYTE(mr->rdParm[6]);
845 font.lfOutPrecision = LOBYTE(mr->rdParm[7]);
846 font.lfClipPrecision = HIBYTE(mr->rdParm[7]);
847 font.lfQuality = LOBYTE(mr->rdParm[8]);
848 font.lfPitchAndFamily = HIBYTE(mr->rdParm[8]);
849 memcpy( font.lfFaceName, mr->rdParm + 9, LF_FACESIZE );
850 MF_AddHandle(ht, handles, CreateFontIndirectA( &font ));
854 case META_CREATEBRUSHINDIRECT:
857 brush.lbStyle = mr->rdParm[0];
858 brush.lbColor = MAKELONG( mr->rdParm[1], mr->rdParm[2] );
859 brush.lbHatch = mr->rdParm[3];
860 MF_AddHandle(ht, handles, CreateBrushIndirect( &brush ));
864 case META_CREATEPALETTE:
865 MF_AddHandle(ht, handles, CreatePalette((LPLOGPALETTE)mr->rdParm));
868 case META_SETTEXTALIGN:
869 SetTextAlign(hdc, mr->rdParm[0]);
872 case META_SELECTPALETTE:
873 GDISelectPalette(hdc, *(ht->objectHandle + mr->rdParm[1]), mr->rdParm[0]);
876 case META_SETMAPPERFLAGS:
877 SetMapperFlags(hdc, MAKELONG(mr->rdParm[0],mr->rdParm[1]));
880 case META_REALIZEPALETTE:
881 GDIRealizePalette(hdc);
885 switch (mr->rdParm[0]) {
886 case GETSCALINGFACTOR: /* get function ... would just NULL dereference */
887 case GETPHYSPAGESIZE:
888 case GETPRINTINGOFFSET:
891 FIXME("Filtering Escape(SETABORTPROC), possible virus?\n");
894 Escape(hdc, mr->rdParm[0], mr->rdParm[1], (LPCSTR)&mr->rdParm[2], NULL);
897 case META_EXTTEXTOUT:
898 MF_Play_MetaExtTextOut( hdc, mr );
901 case META_STRETCHDIB:
903 LPBITMAPINFO info = (LPBITMAPINFO) &(mr->rdParm[11]);
904 LPSTR bits = (LPSTR)info + bitmap_info_size( info, mr->rdParm[2] );
905 StretchDIBits( hdc, (SHORT)mr->rdParm[10], (SHORT)mr->rdParm[9], (SHORT)mr->rdParm[8],
906 (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6], (SHORT)mr->rdParm[5],
907 (SHORT)mr->rdParm[4], (SHORT)mr->rdParm[3], bits, info,
908 mr->rdParm[2],MAKELONG(mr->rdParm[0],mr->rdParm[1]));
912 case META_DIBSTRETCHBLT:
914 LPBITMAPINFO info = (LPBITMAPINFO) &(mr->rdParm[10]);
915 LPSTR bits = (LPSTR)info + bitmap_info_size( info, DIB_RGB_COLORS );
916 StretchDIBits( hdc, (SHORT)mr->rdParm[9], (SHORT)mr->rdParm[8], (SHORT)mr->rdParm[7],
917 (SHORT)mr->rdParm[6], (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
918 (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2], bits, info,
919 DIB_RGB_COLORS,MAKELONG(mr->rdParm[0],mr->rdParm[1]));
923 case META_STRETCHBLT:
925 HDC hdcSrc = CreateCompatibleDC(hdc);
926 HBITMAP hbitmap = CreateBitmap(mr->rdParm[10], /*Width */
927 mr->rdParm[11], /*Height*/
928 mr->rdParm[13], /*Planes*/
929 mr->rdParm[14], /*BitsPixel*/
930 &mr->rdParm[15]); /*bits*/
931 SelectObject(hdcSrc,hbitmap);
932 StretchBlt(hdc, (SHORT)mr->rdParm[9], (SHORT)mr->rdParm[8],
933 (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6],
934 hdcSrc, (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
935 (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
936 MAKELONG(mr->rdParm[0],mr->rdParm[1]));
943 HDC hdcSrc = CreateCompatibleDC(hdc);
944 HBITMAP hbitmap = CreateBitmap(mr->rdParm[7]/*Width */,
945 mr->rdParm[8]/*Height*/,
946 mr->rdParm[10]/*Planes*/,
947 mr->rdParm[11]/*BitsPixel*/,
948 &mr->rdParm[12]/*bits*/);
949 SelectObject(hdcSrc,hbitmap);
950 BitBlt(hdc,(SHORT)mr->rdParm[6],(SHORT)mr->rdParm[5],
951 (SHORT)mr->rdParm[4],(SHORT)mr->rdParm[3],
952 hdcSrc, (SHORT)mr->rdParm[2],(SHORT)mr->rdParm[1],
953 MAKELONG(0,mr->rdParm[0]));
958 case META_CREATEREGION:
960 HRGN hrgn = CreateRectRgn(0,0,0,0);
962 MF_Play_MetaCreateRegion(mr, hrgn);
963 MF_AddHandle(ht, handles, hrgn);
967 case META_FILLREGION:
968 FillRgn(hdc, *(ht->objectHandle + mr->rdParm[1]),
969 *(ht->objectHandle + mr->rdParm[0]));
972 case META_FRAMEREGION:
973 FrameRgn(hdc, *(ht->objectHandle + mr->rdParm[3]),
974 *(ht->objectHandle + mr->rdParm[2]),
975 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
978 case META_INVERTREGION:
979 InvertRgn(hdc, *(ht->objectHandle + mr->rdParm[0]));
982 case META_PAINTREGION:
983 PaintRgn(hdc, *(ht->objectHandle + mr->rdParm[0]));
986 case META_SELECTCLIPREGION:
990 if (mr->rdParm[0]) hrgn = *(ht->objectHandle + mr->rdParm[0]);
991 SelectClipRgn(hdc, hrgn);
995 case META_DIBCREATEPATTERNBRUSH:
996 /* mr->rdParm[0] may be BS_PATTERN or BS_DIBPATTERN:
997 but there's no difference */
998 MF_AddHandle(ht, handles, CreateDIBPatternBrushPt( mr->rdParm + 2, mr->rdParm[1] ));
1001 case META_DIBBITBLT:
1002 /* In practice I've found that there are two layouts for
1003 META_DIBBITBLT, one (the first here) is the usual one when a src
1004 dc is actually passed to it, the second occurs when the src dc is
1005 passed in as NULL to the creating BitBlt. As the second case has
1006 no dib, a size check will suffice to distinguish.
1008 Caolan.McNamara@ul.ie */
1010 if (mr->rdSize > 12) {
1011 LPBITMAPINFO info = (LPBITMAPINFO) &(mr->rdParm[8]);
1012 LPSTR bits = (LPSTR)info + bitmap_info_size(info, mr->rdParm[0]);
1014 StretchDIBits(hdc, (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6], (SHORT)mr->rdParm[5],
1015 (SHORT)mr->rdParm[4], (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
1016 (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4], bits, info,
1017 DIB_RGB_COLORS, MAKELONG(mr->rdParm[0], mr->rdParm[1]));
1019 else /* equivalent to a PatBlt */
1020 PatBlt(hdc, (SHORT)mr->rdParm[8], (SHORT)mr->rdParm[7],
1021 (SHORT)mr->rdParm[6], (SHORT)mr->rdParm[5],
1022 MAKELONG(mr->rdParm[0], mr->rdParm[1]));
1025 case META_SETTEXTCHAREXTRA:
1026 SetTextCharacterExtra(hdc, (SHORT)mr->rdParm[0]);
1029 case META_SETTEXTJUSTIFICATION:
1030 SetTextJustification(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
1033 case META_EXTFLOODFILL:
1034 ExtFloodFill(hdc, (SHORT)mr->rdParm[4], (SHORT)mr->rdParm[3],
1035 MAKELONG(mr->rdParm[1], mr->rdParm[2]),
1039 case META_SETDIBTODEV:
1041 BITMAPINFO *info = (BITMAPINFO *) &(mr->rdParm[9]);
1042 char *bits = (char *)info + bitmap_info_size( info, mr->rdParm[0] );
1043 SetDIBitsToDevice(hdc, (SHORT)mr->rdParm[8], (SHORT)mr->rdParm[7],
1044 (SHORT)mr->rdParm[6], (SHORT)mr->rdParm[5],
1045 (SHORT)mr->rdParm[4], (SHORT)mr->rdParm[3],
1046 mr->rdParm[2], mr->rdParm[1], bits, info,
1051 #define META_UNIMP(x) case x: \
1052 FIXME("PlayMetaFileRecord:record type "#x" not implemented.\n"); \
1054 META_UNIMP(META_DRAWTEXT)
1055 META_UNIMP(META_ANIMATEPALETTE)
1056 META_UNIMP(META_SETPALENTRIES)
1057 META_UNIMP(META_RESIZEPALETTE)
1058 META_UNIMP(META_RESETDC)
1059 META_UNIMP(META_STARTDOC)
1060 META_UNIMP(META_STARTPAGE)
1061 META_UNIMP(META_ENDPAGE)
1062 META_UNIMP(META_ABORTDOC)
1063 META_UNIMP(META_ENDDOC)
1064 META_UNIMP(META_CREATEBRUSH)
1065 META_UNIMP(META_CREATEBITMAPINDIRECT)
1066 META_UNIMP(META_CREATEBITMAP)
1070 WARN("PlayMetaFileRecord: Unknown record type %x\n", mr->rdFunction);
1076 /******************************************************************
1077 * SetMetaFileBitsEx (GDI32.@)
1079 * Create a metafile from raw data. No checking of the data is performed.
1080 * Use GetMetaFileBitsEx() to get raw data from a metafile.
1083 * size [I] size of metafile, in bytes
1084 * lpData [I] pointer to metafile data
1087 * Success: Handle to metafile.
1090 HMETAFILE WINAPI SetMetaFileBitsEx( UINT size, const BYTE *lpData )
1092 const METAHEADER *mh_in = (const METAHEADER *)lpData;
1095 if (size & 1) return 0;
1097 if (!size || mh_in->mtType != METAFILE_MEMORY || mh_in->mtVersion != MFVERSION ||
1098 mh_in->mtHeaderSize != sizeof(METAHEADER) / 2)
1100 SetLastError(ERROR_INVALID_DATA);
1104 mh_out = HeapAlloc( GetProcessHeap(), 0, size );
1107 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1111 memcpy(mh_out, mh_in, size);
1112 mh_out->mtSize = size / 2;
1113 return MF_Create_HMETAFILE(mh_out);
1116 /*****************************************************************
1117 * GetMetaFileBitsEx (GDI32.@)
1119 * Get raw metafile data.
1121 * Copies the data from metafile _hmf_ into the buffer _buf_.
1125 * nSize [I] size of buf
1126 * buf [O] buffer to receive raw metafile data
1129 * If _buf_ is zero, returns size of buffer required. Otherwise,
1130 * returns number of bytes copied.
1132 UINT WINAPI GetMetaFileBitsEx( HMETAFILE hmf, UINT nSize, LPVOID buf )
1134 METAHEADER *mh = MF_GetMetaHeader(hmf);
1137 TRACE("(%p,%d,%p)\n", hmf, nSize, buf);
1138 if (!mh) return 0; /* FIXME: error code */
1139 if(mh->mtType == METAFILE_DISK)
1140 FIXME("Disk-based metafile?\n");
1141 mfSize = mh->mtSize * 2;
1143 TRACE("returning size %d\n", mfSize);
1146 if(mfSize > nSize) mfSize = nSize;
1147 memmove(buf, mh, mfSize);
1151 #include <pshpack2.h>
1154 DWORD magic; /* WMFC */
1160 DWORD unk0e; /* 0 */
1163 DWORD remaining_size;
1167 #include <poppack.h>
1169 static const DWORD wmfc_magic = 0x43464d57;
1171 /******************************************************************
1174 * Helper for GetWinMetaFileBits
1176 * Add the MFCOMMENT record[s] which is essentially a copy
1177 * of the original emf.
1179 static BOOL add_mf_comment(HDC hdc, HENHMETAFILE emf)
1181 DWORD size = GetEnhMetaFileBits(emf, 0, NULL), i;
1182 BYTE *bits, *chunk_data;
1183 mf_comment_chunk *chunk = NULL;
1185 static const DWORD max_chunk_size = 0x2000;
1187 if(!size) return FALSE;
1188 chunk_data = bits = HeapAlloc(GetProcessHeap(), 0, size);
1189 if(!bits) return FALSE;
1190 if(!GetEnhMetaFileBits(emf, size, bits)) goto end;
1192 chunk = HeapAlloc(GetProcessHeap(), 0, max_chunk_size + FIELD_OFFSET(mf_comment_chunk, emf_data));
1193 if(!chunk) goto end;
1195 chunk->magic = wmfc_magic;
1200 chunk->checksum = 0; /* We fixup the first chunk's checksum before returning from GetWinMetaFileBits */
1202 chunk->num_chunks = (size + max_chunk_size - 1) / max_chunk_size;
1203 chunk->chunk_size = max_chunk_size;
1204 chunk->remaining_size = size;
1205 chunk->emf_size = size;
1207 for(i = 0; i < chunk->num_chunks; i++)
1209 if(i == chunk->num_chunks - 1) /* last chunk */
1210 chunk->chunk_size = chunk->remaining_size;
1212 chunk->remaining_size -= chunk->chunk_size;
1213 memcpy(&chunk->emf_data, chunk_data, chunk->chunk_size);
1214 chunk_data += chunk->chunk_size;
1216 if(!Escape(hdc, MFCOMMENT, chunk->chunk_size + FIELD_OFFSET(mf_comment_chunk, emf_data), (char*)chunk, NULL))
1221 HeapFree(GetProcessHeap(), 0, chunk);
1222 HeapFree(GetProcessHeap(), 0, bits);
1226 /*******************************************************************
1229 * Behaves somewhat differently to MulDiv when the answer is -ve
1230 * and also rounds n.5 towards zero
1232 static INT muldiv(INT m1, INT m2, INT d)
1236 ret = ((LONGLONG)m1 * m2 + d/2) / d; /* Always add d/2 even if ret will be -ve */
1238 if((LONGLONG)m1 * m2 * 2 == (2 * ret - 1) * d) /* If the answer is exactly n.5 round towards zero */
1246 /******************************************************************
1249 * Helper for GetWinMetaFileBits
1251 * Add the SetWindowOrg and SetWindowExt records
1253 static BOOL set_window(HDC hdc, HENHMETAFILE emf, HDC ref_dc, INT map_mode)
1255 ENHMETAHEADER header;
1256 INT horz_res, vert_res, horz_size, vert_size;
1259 if(!GetEnhMetaFileHeader(emf, sizeof(header), &header)) return FALSE;
1261 horz_res = GetDeviceCaps(ref_dc, HORZRES);
1262 vert_res = GetDeviceCaps(ref_dc, VERTRES);
1263 horz_size = GetDeviceCaps(ref_dc, HORZSIZE);
1264 vert_size = GetDeviceCaps(ref_dc, VERTSIZE);
1270 case MM_ANISOTROPIC:
1271 pt.y = muldiv(header.rclFrame.top, vert_res, vert_size * 100);
1272 pt.x = muldiv(header.rclFrame.left, horz_res, horz_size * 100);
1275 pt.y = muldiv(-header.rclFrame.top, 1, 10) + 1;
1276 pt.x = muldiv( header.rclFrame.left, 1, 10);
1279 pt.y = -header.rclFrame.top + 1;
1280 pt.x = (header.rclFrame.left >= 0) ? header.rclFrame.left : header.rclFrame.left + 1; /* See the tests */
1283 pt.y = muldiv(-header.rclFrame.top, 10, 254) + 1;
1284 pt.x = muldiv( header.rclFrame.left, 10, 254);
1287 pt.y = muldiv(-header.rclFrame.top, 100, 254) + 1;
1288 pt.x = muldiv( header.rclFrame.left, 100, 254);
1291 pt.y = muldiv(-header.rclFrame.top, 72 * 20, 2540) + 1;
1292 pt.x = muldiv( header.rclFrame.left, 72 * 20, 2540);
1295 WARN("Unknown map mode %d\n", map_mode);
1298 SetWindowOrgEx(hdc, pt.x, pt.y, NULL);
1300 pt.x = muldiv(header.rclFrame.right - header.rclFrame.left, horz_res, horz_size * 100);
1301 pt.y = muldiv(header.rclFrame.bottom - header.rclFrame.top, vert_res, vert_size * 100);
1302 SetWindowExtEx(hdc, pt.x, pt.y, NULL);
1306 /******************************************************************
1307 * GetWinMetaFileBits [GDI32.@]
1309 UINT WINAPI GetWinMetaFileBits(HENHMETAFILE hemf,
1310 UINT cbBuffer, LPBYTE lpbBuffer,
1311 INT map_mode, HDC hdcRef)
1315 UINT ret, full_size;
1318 GetClipBox(hdcRef, &rc);
1320 TRACE("(%p,%d,%p,%d,%p) rc=%s\n", hemf, cbBuffer, lpbBuffer,
1321 map_mode, hdcRef, wine_dbgstr_rect(&rc));
1323 hdcmf = CreateMetaFileW(NULL);
1325 add_mf_comment(hdcmf, hemf);
1326 SetMapMode(hdcmf, map_mode);
1327 if(!set_window(hdcmf, hemf, hdcRef, map_mode))
1330 PlayEnhMetaFile(hdcmf, hemf, &rc);
1331 hmf = CloseMetaFile(hdcmf);
1332 full_size = GetMetaFileBitsEx(hmf, 0, NULL);
1333 ret = GetMetaFileBitsEx(hmf, cbBuffer, lpbBuffer);
1334 DeleteMetaFile(hmf);
1336 if(ret && ret == full_size && lpbBuffer) /* fixup checksum, but only if retrieving all of the bits */
1339 METARECORD *comment_rec = (METARECORD*)(lpbBuffer + sizeof(METAHEADER));
1342 for(i = 0; i < full_size / 2; i++)
1343 checksum += ((WORD*)lpbBuffer)[i];
1344 comment_rec->rdParm[8] = ~checksum + 1;
1349 DeleteMetaFile(CloseMetaFile(hdcmf));
1353 /******************************************************************
1354 * MF_Play_MetaCreateRegion
1356 * Handles META_CREATEREGION for PlayMetaFileRecord().
1358 * The layout of the record looks something like this:
1363 * 2 Looks like a handle? - not constant
1365 * 4 Total number of bytes
1366 * 5 No. of separate bands = n [see below]
1367 * 6 Largest number of x co-ords in a band
1368 * 7-10 Bounding box x1 y1 x2 y2
1371 * Regions are divided into bands that are uniform in the
1372 * y-direction. Each band consists of pairs of on/off x-coords and is
1374 * m y0 y1 x1 x2 x3 ... xm m
1375 * into successive rdParm[]s.
1377 * This is probably just a dump of the internal RGNOBJ?
1383 static BOOL MF_Play_MetaCreateRegion( METARECORD *mr, HRGN hrgn )
1388 HRGN hrgn2 = CreateRectRgn( 0, 0, 0, 0 );
1390 for(band = 0, start = &(mr->rdParm[11]); band < mr->rdParm[5];
1391 band++, start = end + 1) {
1392 if(*start / 2 != (*start + 1) / 2) {
1393 WARN("Delimiter not even.\n");
1394 DeleteObject( hrgn2 );
1398 end = start + *start + 3;
1399 if(end > (WORD *)mr + mr->rdSize) {
1400 WARN("End points outside record.\n");
1401 DeleteObject( hrgn2 );
1405 if(*start != *end) {
1406 WARN("Mismatched delimiters.\n");
1407 DeleteObject( hrgn2 );
1411 y0 = *(INT16 *)(start + 1);
1412 y1 = *(INT16 *)(start + 2);
1413 for(pair = 0; pair < *start / 2; pair++) {
1414 SetRectRgn( hrgn2, *(INT16 *)(start + 3 + 2*pair), y0,
1415 *(INT16 *)(start + 4 + 2*pair), y1 );
1416 CombineRgn(hrgn, hrgn, hrgn2, RGN_OR);
1419 DeleteObject( hrgn2 );
1424 /******************************************************************
1425 * MF_Play_MetaExtTextOut
1427 * Handles META_EXTTEXTOUT for PlayMetaFileRecord().
1430 static BOOL MF_Play_MetaExtTextOut(HDC hdc, METARECORD *mr)
1439 BOOL isrect = mr->rdParm[3] & (ETO_OPAQUE | ETO_CLIPPED);
1441 s1 = mr->rdParm[2]; /* String length */
1442 len = sizeof(METARECORD) + (((s1 + 1) >> 1) * 2) + 2 * sizeof(short)
1443 + sizeof(UINT16) + (isrect ? 4 * sizeof(SHORT) : 0);
1444 /* rec len without dx array */
1446 sot = (LPSTR)&mr->rdParm[4]; /* start_of_text */
1449 rect.left = (SHORT)mr->rdParm[4];
1450 rect.top = (SHORT)mr->rdParm[5];
1451 rect.right = (SHORT)mr->rdParm[6];
1452 rect.bottom = (SHORT)mr->rdParm[7];
1453 sot += 4 * sizeof(SHORT); /* there is a rectangle, so add offset */
1456 if (mr->rdSize == len / 2)
1457 dxx = NULL; /* determine if array is present */
1459 if (mr->rdSize == (len + s1 * sizeof(INT16)) / 2)
1461 dxx = (SHORT *)(sot+(((s1+1)>>1)*2));
1462 dx = HeapAlloc( GetProcessHeap(), 0, s1*sizeof(INT));
1463 if (dx) for (i = 0; i < s1; i++) dx[i] = dxx[i];
1466 TRACE("%s len: %d\n", sot, mr->rdSize);
1467 WARN("Please report: ExtTextOut len=%d slen=%d rdSize=%d opt=%04x\n",
1468 len, s1, mr->rdSize, mr->rdParm[3]);
1469 dxx = NULL; /* shouldn't happen -- but if, we continue with NULL */
1472 (SHORT)mr->rdParm[1], /* X position */
1473 (SHORT)mr->rdParm[0], /* Y position */
1474 mr->rdParm[3], /* options */
1475 &rect, /* rectangle */
1477 s1, dx); /* length, dx array */
1480 TRACE("%s len: %d dx0: %d\n", sot, mr->rdSize, dx[0]);
1481 HeapFree( GetProcessHeap(), 0, dx );