Release 961222
[wine/gsoc-2012-control.git] / objects / metafile.c
bloba912ce88015e1aeb877157c2d965d47991599416
1 /*
2 * Metafile functions
4 * Copyright David W. Metcalfe, 1994
5 * Niels de Carpentier, Albrecht Kleine, Huw Davies 1996
7 */
9 #include <string.h>
10 #include <fcntl.h>
11 #include "gdi.h"
12 #include "bitmap.h"
13 #include "file.h"
14 #include "heap.h"
15 #include "metafile.h"
16 #include "metafiledrv.h"
17 #include "stddebug.h"
18 #include "debug.h"
20 /******************************************************************
21 * MF_AddHandle
23 * Add a handle to an external handle table and return the index
26 static int MF_AddHandle(HANDLETABLE16 *ht, WORD htlen, HGDIOBJ16 hobj)
28 int i;
30 for (i = 0; i < htlen; i++)
32 if (*(ht->objectHandle + i) == 0)
34 *(ht->objectHandle + i) = hobj;
35 return i;
38 return -1;
42 /******************************************************************
43 * MF_AddHandleDC
45 * Note: this function assumes that we never delete objects.
46 * If we do someday, we'll need to maintain a table to re-use deleted
47 * handles.
49 static int MF_AddHandleDC( DC *dc )
51 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
52 physDev->mh->mtNoObjects++;
53 return physDev->nextHandle++;
57 /******************************************************************
58 * GetMetafile GDI.124 By Kenny MacDonald 30 Nov 94
61 HMETAFILE16 GetMetaFile(LPSTR lpFilename)
63 HMETAFILE16 hmf;
64 METAHEADER *mh;
65 HFILE hFile;
66 DWORD size;
68 dprintf_metafile(stddeb,"GetMetaFile: %s\n", lpFilename);
70 if (!lpFilename)
71 return 0;
73 hmf = GlobalAlloc16(GMEM_MOVEABLE, MFHEADERSIZE);
74 mh = (METAHEADER *)GlobalLock16(hmf);
76 if (!mh) {
77 GlobalFree16(hmf);
78 return 0;
81 if ((hFile = _lopen(lpFilename, OF_READ)) == HFILE_ERROR) {
82 GlobalFree16(hmf);
83 return 0;
86 if (_lread32(hFile, (char *)mh, MFHEADERSIZE) == HFILE_ERROR) {
87 GlobalFree16(hmf);
88 return 0;
91 size = mh->mtSize * 2; /* alloc memory for whole metafile */
92 GlobalUnlock16(hmf);
93 hmf = GlobalReAlloc16(hmf,size,GMEM_MOVEABLE);
94 mh = (METAHEADER *)GlobalLock16(hmf);
96 if (!mh) {
97 GlobalFree16(hmf);
98 return 0;
101 if (_lread32(hFile, (char*)mh + mh->mtHeaderSize * 2,
102 size - mh->mtHeaderSize * 2) == HFILE_ERROR) {
103 GlobalFree16(hmf);
104 return 0;
107 _lclose(hFile);
109 if (mh->mtType != 1) {
110 GlobalFree16(hmf);
111 return 0;
114 GlobalUnlock16(hmf);
115 return hmf;
120 /******************************************************************
121 * CopyMetafile GDI.151 Niels de Carpentier, April 1996
124 HMETAFILE16 CopyMetaFile(HMETAFILE16 hSrcMetaFile, LPCSTR lpFilename)
126 HMETAFILE16 handle = 0;
127 METAHEADER *mh;
128 METAHEADER *mh2;
129 HFILE hFile;
131 dprintf_metafile(stddeb,"CopyMetaFile: %s\n", lpFilename);
133 mh = (METAHEADER *)GlobalLock16(hSrcMetaFile);
135 if (!mh)
136 return 0;
138 if (lpFilename) /* disk based metafile */
140 int i,j;
141 hFile = _lcreat(lpFilename, 0);
142 j=mh->mtType;
143 mh->mtType=1; /* disk file version stores 1 here */
144 i=_lwrite32(hFile, (char *)mh, mh->mtSize * 2) ;
145 mh->mtType=j; /* restore old value [0 or 1] */
146 _lclose(hFile);
147 if (i == -1)
148 return 0;
150 else /* memory based metafile */
152 handle = GlobalAlloc16(GMEM_MOVEABLE,mh->mtSize * 2);
153 mh2 = (METAHEADER *)GlobalLock16(handle);
154 memcpy(mh2,mh, mh->mtSize * 2);
155 GlobalUnlock16(handle);
158 return handle;
161 /******************************************************************
162 * IsValidMetaFile (GDI.410)
163 * (This is not exactly what windows does, see "Undoc Win")
166 BOOL IsValidMetaFile(HMETAFILE16 hmf)
168 BOOL resu=FALSE;
169 METAHEADER *mh = (METAHEADER *)GlobalLock16(hmf);
170 if (mh) {
171 if (mh->mtType == 1 || mh->mtType == 0)
172 if (mh->mtHeaderSize == MFHEADERSIZE/sizeof(INT16))
173 if (mh->mtVersion == MFVERSION)
174 resu=TRUE;
175 GlobalUnlock16(hmf);
177 dprintf_metafile(stddeb,"IsValidMetaFile %x => %d\n",hmf,resu);
178 return resu;
182 #if 0
183 /******************************************************************
184 * CloseMetafile GDI.126
187 HMETAFILE16 CloseMetaFile(HDC16 hdc)
189 DC *dc;
190 METAHEADER *mh;
191 HMETAFILE16 hmf;
192 HFILE hFile;
193 METAFILEDRV_PDEVICE *physDev;
195 dprintf_metafile(stddeb,"CloseMetaFile\n");
197 if (!(dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC))) return 0;
199 physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
200 mh = (METAHEADER *)GlobalLock16( physDev->hMetafile );
202 /* Construct the end of metafile record - this is documented
203 * in SDK Knowledgebase Q99334.
206 if (!MF_MetaParam0(dc, META_EOF))
208 DeleteDC32( hdc );
209 return 0;
212 if (mh->mtType == 1) /* disk based metafile */
214 hFile = mh->mtNoParameters;
215 mh->mtNoParameters = 0;
216 if (_llseek(hFile, 0L, 0) == -1)
218 DeleteDC32( hdc );
219 return 0;
221 if (_lwrite32(hFile, (char *)mh, MFHEADERSIZE) == -1)
223 DeleteDC32( hdc );
224 return 0;
226 _lclose(hFile);
229 hmf = physDev->hMetafile;
230 GlobalUnlock16( hmf );
231 physDev->hMetafile = 0; /* So it won't be deleted */
232 DeleteDC32( hdc );
233 return hmf;
237 /******************************************************************
238 * DeleteMetafile GDI.127
241 BOOL DeleteMetaFile(HMETAFILE16 hmf)
243 METAHEADER *mh = (METAHEADER *)GlobalLock16(hmf);
245 if (!mh)
246 return FALSE;
248 GlobalFree16(hmf);
249 return TRUE;
251 #endif
253 /******************************************************************
254 * PlayMetafile GDI.123
257 BOOL PlayMetaFile(HDC16 hdc, HMETAFILE16 hmf)
259 METAHEADER *mh = (METAHEADER *)GlobalLock16(hmf);
260 METARECORD *mr;
261 HANDLETABLE16 *ht;
262 HGLOBAL16 hHT;
263 int offset = 0;
264 WORD i;
266 dprintf_metafile(stddeb,"PlayMetaFile(%04x %04x)\n",hdc,hmf);
268 /* create the handle table */
269 hHT = GlobalAlloc16(GMEM_MOVEABLE|GMEM_ZEROINIT,
270 sizeof(HANDLETABLE16) * mh->mtNoObjects);
271 ht = (HANDLETABLE16 *)GlobalLock16(hHT);
273 /* loop through metafile playing records */
274 offset = mh->mtHeaderSize * 2;
275 while (offset < mh->mtSize * 2)
277 mr = (METARECORD *)((char *)mh + offset);
278 dprintf_metafile(stddeb,"offset = %04x size = %08lx function = %04x\n",
279 offset,mr->rdSize,mr->rdFunction);
280 offset += mr->rdSize * 2;
281 PlayMetaFileRecord(hdc, ht, mr, mh->mtNoObjects);
284 /* free objects in handle table */
285 for(i = 0; i < mh->mtNoObjects; i++)
286 if(*(ht->objectHandle + i) != 0)
287 DeleteObject32(*(ht->objectHandle + i));
289 /* free handle table */
290 GlobalFree16(hHT);
292 return TRUE;
296 /******************************************************************
297 * EnumMetafile GDI.175
298 * Niels de carpentier, april 1996
301 BOOL EnumMetaFile(HDC16 hdc, HMETAFILE16 hmf, MFENUMPROC16 lpEnumFunc,LPARAM lpData)
303 METAHEADER *mh = (METAHEADER *)GlobalLock16(hmf);
304 METARECORD *mr;
305 HGLOBAL16 hHT;
306 SEGPTR ht, spRecord;
307 int offset = 0;
309 dprintf_metafile(stddeb,"EnumMetaFile(%04x, %04x, %08lx, %08lx)\n",
310 hdc, hmf, (DWORD)lpEnumFunc, lpData);
312 /* create the handle table */
314 hHT = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT,
315 sizeof(HANDLETABLE16) * mh->mtNoObjects);
316 ht = WIN16_GlobalLock16(hHT);
318 offset = mh->mtHeaderSize * 2;
320 /* loop through metafile records */
322 spRecord = WIN16_GlobalLock16(hmf);
323 while (offset < (mh->mtSize * 2))
325 mr = (METARECORD *)((char *)mh + offset);
326 if (!lpEnumFunc( hdc, (HANDLETABLE16 *)ht,
327 (METARECORD *)((UINT32)spRecord + offset),
328 mh->mtNoObjects, (LONG)lpData))
329 break;
331 offset += (mr->rdSize * 2);
334 /* free handle table */
335 GlobalFree16(hHT);
337 return TRUE;
340 /*******************************************************************
341 * MF_GetDIBitsPointer [internal helper for e.g. PlayMetaFileRecord]
343 * Returns offset to DIB bits or 0 if error
344 * (perhaps should be moved to (objects/dib.c ?)
346 static LPSTR MF_GetDIBitsPointer(LPBITMAPINFO info)
348 int offset;
349 if (info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
351 if (info->bmiHeader.biClrUsed)
353 if (info->bmiHeader.biClrUsed < (1 << info->bmiHeader.biBitCount))
354 offset = info->bmiHeader.biClrUsed * 4;
355 else
356 offset = (1 << info->bmiHeader.biBitCount) * 4;
358 else
359 offset = (1 << info->bmiHeader.biBitCount) * 4;
361 else if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
363 if (info->bmiHeader.biClrUsed)
365 if (info->bmiHeader.biClrUsed < (1 << info->bmiHeader.biBitCount))
366 offset = info->bmiHeader.biClrUsed * 3;
367 else
368 offset = (1 << info->bmiHeader.biBitCount) * 3;
370 else
371 offset = (1 << info->bmiHeader.biBitCount) * 3;
373 else
375 fprintf(stderr,"Unknown size for BITMAPHEADER in MetaFile!\n");
376 return NULL;
378 return (LPSTR)info + info->bmiHeader.biSize + offset;
382 /******************************************************************
383 * PlayMetaFileRecord GDI.176
386 void PlayMetaFileRecord(HDC16 hdc, HANDLETABLE16 *ht, METARECORD *mr,
387 WORD nHandles)
389 short s1;
390 HANDLE16 hndl;
391 char *ptr;
392 BITMAPINFOHEADER *infohdr;
394 dprintf_metafile(stddeb,"PlayMetaFileRecord(%04x %08lx %08lx %04x)\n",
395 hdc,(LONG)ht, (LONG)mr, nHandles);
397 switch (mr->rdFunction)
399 case META_EOF:
400 break;
402 case META_DELETEOBJECT:
403 DeleteObject32(*(ht->objectHandle + *(mr->rdParam)));
404 *(ht->objectHandle + *(mr->rdParam)) = 0;
405 break;
407 case META_SETBKCOLOR:
408 SetBkColor(hdc, *(mr->rdParam));
409 break;
411 case META_SETBKMODE:
412 SetBkMode16(hdc, *(mr->rdParam));
413 break;
415 case META_SETMAPMODE:
416 SetMapMode16(hdc, *(mr->rdParam));
417 break;
419 case META_SETROP2:
420 SetROP216(hdc, *(mr->rdParam));
421 break;
423 case META_SETRELABS:
424 SetRelAbs16(hdc, *(mr->rdParam));
425 break;
427 case META_SETPOLYFILLMODE:
428 SetPolyFillMode16(hdc, *(mr->rdParam));
429 break;
431 case META_SETSTRETCHBLTMODE:
432 SetStretchBltMode16(hdc, *(mr->rdParam));
433 break;
435 case META_SETTEXTCOLOR:
436 SetTextColor(hdc, MAKELONG(*(mr->rdParam), *(mr->rdParam + 1)));
437 break;
439 case META_SETWINDOWORG:
440 SetWindowOrg(hdc, *(mr->rdParam + 1), *(mr->rdParam));
441 break;
443 case META_SETWINDOWEXT:
444 SetWindowExt(hdc, *(mr->rdParam + 1), *(mr->rdParam));
445 break;
447 case META_SETVIEWPORTORG:
448 SetViewportOrg(hdc, *(mr->rdParam + 1), *(mr->rdParam));
449 break;
451 case META_SETVIEWPORTEXT:
452 SetViewportExt(hdc, *(mr->rdParam + 1), *(mr->rdParam));
453 break;
455 case META_OFFSETWINDOWORG:
456 OffsetWindowOrg(hdc, *(mr->rdParam + 1), *(mr->rdParam));
457 break;
459 case META_SCALEWINDOWEXT:
460 ScaleWindowExt(hdc, *(mr->rdParam + 3), *(mr->rdParam + 2),
461 *(mr->rdParam + 1), *(mr->rdParam));
462 break;
464 case META_OFFSETVIEWPORTORG:
465 OffsetViewportOrg(hdc, *(mr->rdParam + 1), *(mr->rdParam));
466 break;
468 case META_SCALEVIEWPORTEXT:
469 ScaleViewportExt(hdc, *(mr->rdParam + 3), *(mr->rdParam + 2),
470 *(mr->rdParam + 1), *(mr->rdParam));
471 break;
473 case META_LINETO:
474 LineTo32(hdc, *(mr->rdParam + 1), *(mr->rdParam));
475 break;
477 case META_MOVETO:
478 MoveTo(hdc, *(mr->rdParam + 1), *(mr->rdParam));
479 break;
481 case META_EXCLUDECLIPRECT:
482 ExcludeClipRect16( hdc, *(mr->rdParam + 3), *(mr->rdParam + 2),
483 *(mr->rdParam + 1), *(mr->rdParam) );
484 break;
486 case META_INTERSECTCLIPRECT:
487 IntersectClipRect16( hdc, *(mr->rdParam + 3), *(mr->rdParam + 2),
488 *(mr->rdParam + 1), *(mr->rdParam) );
489 break;
491 case META_ARC:
492 Arc32(hdc, *(mr->rdParam + 7), *(mr->rdParam + 6), *(mr->rdParam + 5),
493 *(mr->rdParam + 4), *(mr->rdParam + 3), *(mr->rdParam + 2),
494 *(mr->rdParam + 1), *(mr->rdParam));
495 break;
497 case META_ELLIPSE:
498 Ellipse32(hdc, *(mr->rdParam + 3), *(mr->rdParam + 2),
499 *(mr->rdParam + 1), *(mr->rdParam));
500 break;
502 case META_FLOODFILL:
503 FloodFill32(hdc, *(mr->rdParam + 3), *(mr->rdParam + 2),
504 MAKELONG(*(mr->rdParam + 1), *(mr->rdParam)));
505 break;
507 case META_PIE:
508 Pie32(hdc, *(mr->rdParam + 7), *(mr->rdParam + 6), *(mr->rdParam + 5),
509 *(mr->rdParam + 4), *(mr->rdParam + 3), *(mr->rdParam + 2),
510 *(mr->rdParam + 1), *(mr->rdParam));
511 break;
513 case META_RECTANGLE:
514 Rectangle32(hdc, *(mr->rdParam + 3), *(mr->rdParam + 2),
515 *(mr->rdParam + 1), *(mr->rdParam));
516 break;
518 case META_ROUNDRECT:
519 RoundRect32(hdc, *(mr->rdParam + 5), *(mr->rdParam + 4),
520 *(mr->rdParam + 3), *(mr->rdParam + 2),
521 *(mr->rdParam + 1), *(mr->rdParam));
522 break;
524 case META_PATBLT:
525 PatBlt16(hdc, *(mr->rdParam + 5), *(mr->rdParam + 4),
526 *(mr->rdParam + 3), *(mr->rdParam + 2),
527 MAKELONG(*(mr->rdParam), *(mr->rdParam + 1)));
528 break;
530 case META_SAVEDC:
531 SaveDC32(hdc);
532 break;
534 case META_SETPIXEL:
535 SetPixel32(hdc, *(mr->rdParam + 3), *(mr->rdParam + 2),
536 MAKELONG(*(mr->rdParam + 1), *(mr->rdParam)));
537 break;
539 case META_OFFSETCLIPRGN:
540 OffsetClipRgn16( hdc, *(mr->rdParam + 1), *(mr->rdParam) );
541 break;
543 case META_TEXTOUT:
544 s1 = *(mr->rdParam);
545 TextOut16(hdc, *(mr->rdParam + ((s1 + 1) >> 1) + 2),
546 *(mr->rdParam + ((s1 + 1) >> 1) + 1),
547 (char *)(mr->rdParam + 1), s1);
548 break;
550 case META_POLYGON:
551 Polygon16(hdc, (LPPOINT16)(mr->rdParam + 1), *(mr->rdParam));
552 break;
554 case META_POLYPOLYGON:
555 PolyPolygon16(hdc, (LPPOINT16)(mr->rdParam + *(mr->rdParam) + 1),
556 (LPINT16)(mr->rdParam + 1), *(mr->rdParam));
557 break;
559 case META_POLYLINE:
560 Polyline16(hdc, (LPPOINT16)(mr->rdParam + 1), *(mr->rdParam));
561 break;
563 case META_RESTOREDC:
564 RestoreDC32(hdc, *(mr->rdParam));
565 break;
567 case META_SELECTOBJECT:
568 SelectObject32(hdc, *(ht->objectHandle + *(mr->rdParam)));
569 break;
571 case META_CHORD:
572 Chord32(hdc, *(mr->rdParam + 7), *(mr->rdParam + 6), *(mr->rdParam+5),
573 *(mr->rdParam + 4), *(mr->rdParam + 3), *(mr->rdParam + 2),
574 *(mr->rdParam + 1), *(mr->rdParam));
575 break;
577 case META_CREATEPATTERNBRUSH:
578 switch (*(mr->rdParam))
580 case BS_PATTERN:
581 infohdr = (BITMAPINFOHEADER *)(mr->rdParam + 2);
582 MF_AddHandle(ht, nHandles,
583 CreatePatternBrush32(CreateBitmap(infohdr->biWidth,
584 infohdr->biHeight,
585 infohdr->biPlanes,
586 infohdr->biBitCount,
587 (LPSTR)(mr->rdParam +
588 (sizeof(BITMAPINFOHEADER) / 2) + 4))));
589 break;
591 case BS_DIBPATTERN:
592 s1 = mr->rdSize * 2 - sizeof(METARECORD) - 2;
593 hndl = GlobalAlloc16(GMEM_MOVEABLE, s1);
594 ptr = GlobalLock16(hndl);
595 memcpy(ptr, mr->rdParam + 2, s1);
596 GlobalUnlock16(hndl);
597 MF_AddHandle(ht, nHandles,
598 CreateDIBPatternBrush32(hndl, *(mr->rdParam + 1)));
599 GlobalFree16(hndl);
601 break;
603 case META_CREATEPENINDIRECT:
604 MF_AddHandle(ht, nHandles,
605 CreatePenIndirect16((LOGPEN16 *)(&(mr->rdParam))));
606 break;
608 case META_CREATEFONTINDIRECT:
609 MF_AddHandle(ht, nHandles,
610 CreateFontIndirect16((LOGFONT16 *)(&(mr->rdParam))));
611 break;
613 case META_CREATEBRUSHINDIRECT:
614 MF_AddHandle(ht, nHandles,
615 CreateBrushIndirect16((LOGBRUSH16 *)(&(mr->rdParam))));
616 break;
618 /* W. Magro: Some new metafile operations. Not all debugged. */
619 case META_CREATEPALETTE:
620 MF_AddHandle(ht, nHandles,
621 CreatePalette((LPLOGPALETTE)mr->rdParam));
622 break;
624 case META_SETTEXTALIGN:
625 SetTextAlign16(hdc, *(mr->rdParam));
626 break;
628 case META_SELECTPALETTE:
629 SelectPalette(hdc, *(ht->objectHandle + *(mr->rdParam+1)),*(mr->rdParam));
630 break;
632 case META_SETMAPPERFLAGS:
633 SetMapperFlags(hdc, *(mr->rdParam));
634 break;
636 case META_REALIZEPALETTE:
637 RealizePalette(hdc);
638 break;
640 case META_ESCAPE:
641 dprintf_metafile(stddeb,"PlayMetaFileRecord: META_ESCAPE unimplemented.\n");
642 break;
644 /* --- Begin of fixed or new metafile operations. July 1996 ----*/
645 case META_EXTTEXTOUT:
647 LPINT16 dxx;
648 DWORD len;
650 s1 = mr->rdParam[2]; /* String length */
651 len = sizeof(METARECORD) + (((s1 + 1) >> 1) * 2) + 2 * sizeof(short)
652 + sizeof(UINT16) + sizeof(RECT16);
653 if (mr->rdSize == len / 2)
654 dxx = NULL; /* No array present */
655 else if (mr->rdSize == (len + s1 * sizeof(INT16)) / 2)
656 dxx = &mr->rdParam[8+(s1+1)/2]; /* start of array */
657 else {
658 fprintf(stderr,
659 "PlayMetaFileRecord ExtTextOut mr->rdSize = %08lx, count = %x\n",
660 mr->rdSize, s1);
661 dxx = NULL;
663 ExtTextOut16( hdc, mr->rdParam[1], /* X position */
664 mr->rdParam[0], /* Y position */
665 mr->rdParam[3], /* options */
666 (LPRECT16) &mr->rdParam[4], /* rectangle */
667 (char *)(mr->rdParam + 8), /* string */
668 s1, dxx); /* length, dx array */
669 if (dxx)
670 dprintf_metafile(stddeb,"EXTTEXTOUT len: %ld (%hd %hd) [%s].\n",
671 mr->rdSize,dxx[0],dxx[1],(char*) &(mr->rdParam[8]) );
673 break;
675 case META_STRETCHDIB:
677 LPBITMAPINFO info = (LPBITMAPINFO) &(mr->rdParam[11]);
678 LPSTR bits = MF_GetDIBitsPointer(info);
679 if (bits)
680 StretchDIBits16(hdc,mr->rdParam[10],mr->rdParam[9],mr->rdParam[8],
681 mr->rdParam[7],mr->rdParam[6],mr->rdParam[5],
682 mr->rdParam[4],mr->rdParam[3],bits,info,
683 mr->rdParam[2],MAKELONG(mr->rdParam[0],mr->rdParam[1]));
685 break;
687 case META_DIBSTRETCHBLT:
689 LPBITMAPINFO info = (LPBITMAPINFO) &(mr->rdParam[10]);
690 LPSTR bits = MF_GetDIBitsPointer(info);
691 if (bits)
692 StretchDIBits16(hdc,mr->rdParam[9],mr->rdParam[8],mr->rdParam[7],
693 mr->rdParam[6],mr->rdParam[5],mr->rdParam[4],
694 mr->rdParam[3],mr->rdParam[2],bits,info,
695 DIB_RGB_COLORS,MAKELONG(mr->rdParam[0],mr->rdParam[1]));
697 break;
699 case META_STRETCHBLT:
701 HDC16 hdcSrc=CreateCompatibleDC16(hdc);
702 HBITMAP16 hbitmap=CreateBitmap(mr->rdParam[10], /*Width */
703 mr->rdParam[11], /*Height*/
704 mr->rdParam[13], /*Planes*/
705 mr->rdParam[14], /*BitsPixel*/
706 (LPSTR)&mr->rdParam[15]); /*bits*/
707 SelectObject32(hdcSrc,hbitmap);
708 StretchBlt16(hdc,mr->rdParam[9],mr->rdParam[8],
709 mr->rdParam[7],mr->rdParam[6],
710 hdcSrc,mr->rdParam[5],mr->rdParam[4],
711 mr->rdParam[3],mr->rdParam[2],
712 MAKELONG(mr->rdParam[0],mr->rdParam[1]));
713 DeleteDC32(hdcSrc);
715 break;
717 case META_BITBLT: /* <-- not yet debugged */
719 HDC16 hdcSrc=CreateCompatibleDC16(hdc);
720 HBITMAP16 hbitmap=CreateBitmap(mr->rdParam[7]/*Width */,mr->rdParam[8]/*Height*/,
721 mr->rdParam[10]/*Planes*/,mr->rdParam[11]/*BitsPixel*/,
722 (LPSTR)&mr->rdParam[12]/*bits*/);
723 SelectObject32(hdcSrc,hbitmap);
724 BitBlt32(hdc,mr->rdParam[6],mr->rdParam[5],
725 mr->rdParam[4],mr->rdParam[3],
726 hdcSrc, mr->rdParam[2],mr->rdParam[1],
727 MAKELONG(0,mr->rdParam[0]));
728 DeleteDC32(hdcSrc);
730 break;
732 default:
733 fprintf(stddeb,"PlayMetaFileRecord: Unknown record type %x\n",
734 mr->rdFunction);
739 /******************************************************************
740 * GetMetaFileBits by William Magro, 19 Sep 1995
742 * Trade in a meta file object handle for a handle to the meta file memory
745 HGLOBAL16 GetMetaFileBits(HMETAFILE16 hmf)
747 dprintf_metafile(stddeb,"GetMetaFileBits: hMem out: %04x\n", hmf);
749 return hmf;
752 /******************************************************************
753 * SetMetaFileBits by William Magro, 19 Sep 1995
755 * Trade in a meta file memory handle for a handle to a meta file object
758 HMETAFILE16 SetMetaFileBits( HGLOBAL16 hMem )
760 dprintf_metafile(stddeb,"SetMetaFileBits: hmf out: %04x\n", hMem);
762 return hMem;
765 /******************************************************************
766 * MF_WriteRecord
768 * Warning: this function can change the metafile handle.
771 static BOOL32 MF_WriteRecord( DC *dc, METARECORD *mr, WORD rlen)
773 DWORD len;
774 METAHEADER *mh;
775 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
777 switch(physDev->mh->mtType)
779 case METAFILE_MEMORY:
780 len = physDev->mh->mtSize * 2 + rlen;
781 mh = HeapReAlloc( SystemHeap, 0, physDev->mh, len );
782 if (!mh) return FALSE;
783 physDev->mh = mh;
784 memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
785 break;
786 case METAFILE_DISK:
787 dprintf_metafile(stddeb,"Writing record to disk\n");
788 if (_lwrite32(physDev->mh->mtNoParameters, (char *)mr, rlen) == -1)
789 return FALSE;
790 break;
791 default:
792 fprintf( stderr, "Uknown metafile type %d\n", physDev->mh->mtType );
793 return FALSE;
796 physDev->mh->mtSize += rlen / 2;
797 physDev->mh->mtMaxRecord = MAX(physDev->mh->mtMaxRecord, rlen / 2);
798 return TRUE;
802 /******************************************************************
803 * MF_MetaParam0
806 BOOL32 MF_MetaParam0(DC *dc, short func)
808 char buffer[8];
809 METARECORD *mr = (METARECORD *)&buffer;
811 mr->rdSize = 3;
812 mr->rdFunction = func;
813 return MF_WriteRecord( dc, mr, mr->rdSize * 2);
817 /******************************************************************
818 * MF_MetaParam1
820 BOOL32 MF_MetaParam1(DC *dc, short func, short param1)
822 char buffer[8];
823 METARECORD *mr = (METARECORD *)&buffer;
825 mr->rdSize = 4;
826 mr->rdFunction = func;
827 *(mr->rdParam) = param1;
828 return MF_WriteRecord( dc, mr, mr->rdSize * 2);
832 /******************************************************************
833 * MF_MetaParam2
835 BOOL32 MF_MetaParam2(DC *dc, short func, short param1, short param2)
837 char buffer[10];
838 METARECORD *mr = (METARECORD *)&buffer;
840 mr->rdSize = 5;
841 mr->rdFunction = func;
842 *(mr->rdParam) = param2;
843 *(mr->rdParam + 1) = param1;
844 return MF_WriteRecord( dc, mr, mr->rdSize * 2);
848 /******************************************************************
849 * MF_MetaParam4
852 BOOL32 MF_MetaParam4(DC *dc, short func, short param1, short param2,
853 short param3, short param4)
855 char buffer[14];
856 METARECORD *mr = (METARECORD *)&buffer;
858 mr->rdSize = 7;
859 mr->rdFunction = func;
860 *(mr->rdParam) = param4;
861 *(mr->rdParam + 1) = param3;
862 *(mr->rdParam + 2) = param2;
863 *(mr->rdParam + 3) = param1;
864 return MF_WriteRecord( dc, mr, mr->rdSize * 2);
868 /******************************************************************
869 * MF_MetaParam6
872 BOOL32 MF_MetaParam6(DC *dc, short func, short param1, short param2,
873 short param3, short param4, short param5, short param6)
875 char buffer[18];
876 METARECORD *mr = (METARECORD *)&buffer;
878 mr->rdSize = 9;
879 mr->rdFunction = func;
880 *(mr->rdParam) = param6;
881 *(mr->rdParam + 1) = param5;
882 *(mr->rdParam + 2) = param4;
883 *(mr->rdParam + 3) = param3;
884 *(mr->rdParam + 4) = param2;
885 *(mr->rdParam + 5) = param1;
886 return MF_WriteRecord( dc, mr, mr->rdSize * 2);
890 /******************************************************************
891 * MF_MetaParam8
893 BOOL32 MF_MetaParam8(DC *dc, short func, short param1, short param2,
894 short param3, short param4, short param5,
895 short param6, short param7, short param8)
897 char buffer[22];
898 METARECORD *mr = (METARECORD *)&buffer;
900 mr->rdSize = 11;
901 mr->rdFunction = func;
902 *(mr->rdParam) = param8;
903 *(mr->rdParam + 1) = param7;
904 *(mr->rdParam + 2) = param6;
905 *(mr->rdParam + 3) = param5;
906 *(mr->rdParam + 4) = param4;
907 *(mr->rdParam + 5) = param3;
908 *(mr->rdParam + 6) = param2;
909 *(mr->rdParam + 7) = param1;
910 return MF_WriteRecord( dc, mr, mr->rdSize * 2);
914 /******************************************************************
915 * MF_CreateBrushIndirect
918 BOOL32 MF_CreateBrushIndirect(DC *dc, HBRUSH16 hBrush, LOGBRUSH16 *logbrush)
920 int index;
921 char buffer[sizeof(METARECORD) - 2 + sizeof(*logbrush)];
922 METARECORD *mr = (METARECORD *)&buffer;
924 mr->rdSize = (sizeof(METARECORD) + sizeof(*logbrush) - 2) / 2;
925 mr->rdFunction = META_CREATEBRUSHINDIRECT;
926 memcpy(&(mr->rdParam), logbrush, sizeof(*logbrush));
927 if (!(MF_WriteRecord( dc, mr, mr->rdSize * 2))) return FALSE;
929 mr->rdSize = sizeof(METARECORD) / 2;
930 mr->rdFunction = META_SELECTOBJECT;
932 if ((index = MF_AddHandleDC( dc )) == -1) return FALSE;
933 *(mr->rdParam) = index;
934 return MF_WriteRecord( dc, mr, mr->rdSize * 2);
938 /******************************************************************
939 * MF_CreatePatternBrush
942 BOOL32 MF_CreatePatternBrush(DC *dc, HBRUSH16 hBrush, LOGBRUSH16 *logbrush)
944 DWORD len, bmSize, biSize;
945 HGLOBAL16 hmr;
946 METARECORD *mr;
947 BITMAPOBJ *bmp;
948 BITMAPINFO *info;
949 BITMAPINFOHEADER *infohdr;
950 int index;
951 char buffer[sizeof(METARECORD)];
953 switch (logbrush->lbStyle)
955 case BS_PATTERN:
956 bmp = (BITMAPOBJ *)GDI_GetObjPtr((HGDIOBJ16)logbrush->lbHatch, BITMAP_MAGIC);
957 if (!bmp) return FALSE;
958 len = sizeof(METARECORD) + sizeof(BITMAPINFOHEADER) +
959 (bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes) + 6;
960 if (!(hmr = GlobalAlloc16(GMEM_MOVEABLE, len)))
961 return FALSE;
962 mr = (METARECORD *)GlobalLock16(hmr);
963 memset(mr, 0, len);
964 mr->rdFunction = META_DIBCREATEPATTERNBRUSH;
965 mr->rdSize = len / 2;
966 *(mr->rdParam) = logbrush->lbStyle;
967 *(mr->rdParam + 1) = DIB_RGB_COLORS;
968 infohdr = (BITMAPINFOHEADER *)(mr->rdParam + 2);
969 infohdr->biSize = sizeof(BITMAPINFOHEADER);
970 infohdr->biWidth = bmp->bitmap.bmWidth;
971 infohdr->biHeight = bmp->bitmap.bmHeight;
972 infohdr->biPlanes = bmp->bitmap.bmPlanes;
973 infohdr->biBitCount = bmp->bitmap.bmBitsPixel;
974 memcpy(mr->rdParam + (sizeof(BITMAPINFOHEADER) / 2) + 4,
975 PTR_SEG_TO_LIN(bmp->bitmap.bmBits),
976 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes);
977 break;
979 case BS_DIBPATTERN:
980 info = (BITMAPINFO *)GlobalLock16((HGLOBAL16)logbrush->lbHatch);
981 if (info->bmiHeader.biCompression)
982 bmSize = info->bmiHeader.biSizeImage;
983 else
984 bmSize = (info->bmiHeader.biWidth * info->bmiHeader.biBitCount
985 + 31) / 32 * 8 * info->bmiHeader.biHeight;
986 biSize = DIB_BitmapInfoSize(info, LOWORD(logbrush->lbColor));
987 len = sizeof(METARECORD) + biSize + bmSize + 2;
988 if (!(hmr = GlobalAlloc16(GMEM_MOVEABLE, len)))
989 return FALSE;
990 mr = (METARECORD *)GlobalLock16(hmr);
991 memset(mr, 0, len);
992 mr->rdFunction = META_DIBCREATEPATTERNBRUSH;
993 mr->rdSize = len / 2;
994 *(mr->rdParam) = logbrush->lbStyle;
995 *(mr->rdParam + 1) = LOWORD(logbrush->lbColor);
996 memcpy(mr->rdParam + 2, info, biSize + bmSize);
997 break;
998 default:
999 return FALSE;
1001 if (!(MF_WriteRecord(dc, mr, len)))
1003 GlobalFree16(hmr);
1004 return FALSE;
1007 GlobalFree16(hmr);
1009 mr = (METARECORD *)&buffer;
1010 mr->rdSize = sizeof(METARECORD) / 2;
1011 mr->rdFunction = META_SELECTOBJECT;
1013 if ((index = MF_AddHandleDC( dc )) == -1) return FALSE;
1014 *(mr->rdParam) = index;
1015 return MF_WriteRecord( dc, mr, mr->rdSize * 2);
1019 /******************************************************************
1020 * MF_CreatePenIndirect
1023 BOOL32 MF_CreatePenIndirect(DC *dc, HPEN16 hPen, LOGPEN16 *logpen)
1025 int index;
1026 char buffer[sizeof(METARECORD) - 2 + sizeof(*logpen)];
1027 METARECORD *mr = (METARECORD *)&buffer;
1029 mr->rdSize = (sizeof(METARECORD) + sizeof(*logpen) - 2) / 2;
1030 mr->rdFunction = META_CREATEPENINDIRECT;
1031 memcpy(&(mr->rdParam), logpen, sizeof(*logpen));
1032 if (!(MF_WriteRecord( dc, mr, mr->rdSize * 2))) return FALSE;
1034 mr->rdSize = sizeof(METARECORD) / 2;
1035 mr->rdFunction = META_SELECTOBJECT;
1037 if ((index = MF_AddHandleDC( dc )) == -1) return FALSE;
1038 *(mr->rdParam) = index;
1039 return MF_WriteRecord( dc, mr, mr->rdSize * 2);
1043 /******************************************************************
1044 * MF_CreateFontIndirect
1047 BOOL32 MF_CreateFontIndirect(DC *dc, HFONT16 hFont, LOGFONT16 *logfont)
1049 int index;
1050 char buffer[sizeof(METARECORD) - 2 + sizeof(LOGFONT16)];
1051 METARECORD *mr = (METARECORD *)&buffer;
1053 mr->rdSize = (sizeof(METARECORD) + sizeof(LOGFONT16) - 2) / 2;
1054 mr->rdFunction = META_CREATEFONTINDIRECT;
1055 memcpy(&(mr->rdParam), logfont, sizeof(LOGFONT16));
1056 if (!(MF_WriteRecord( dc, mr, mr->rdSize * 2))) return FALSE;
1058 mr->rdSize = sizeof(METARECORD) / 2;
1059 mr->rdFunction = META_SELECTOBJECT;
1061 if ((index = MF_AddHandleDC( dc )) == -1) return FALSE;
1062 *(mr->rdParam) = index;
1063 return MF_WriteRecord( dc, mr, mr->rdSize * 2);
1067 /******************************************************************
1068 * MF_TextOut
1070 BOOL32 MF_TextOut(DC *dc, short x, short y, LPCSTR str, short count)
1072 BOOL32 ret;
1073 DWORD len;
1074 HGLOBAL16 hmr;
1075 METARECORD *mr;
1077 len = sizeof(METARECORD) + (((count + 1) >> 1) * 2) + 4;
1078 if (!(hmr = GlobalAlloc16(GMEM_MOVEABLE, len)))
1079 return FALSE;
1080 mr = (METARECORD *)GlobalLock16(hmr);
1081 memset(mr, 0, len);
1083 mr->rdSize = len / 2;
1084 mr->rdFunction = META_TEXTOUT;
1085 *(mr->rdParam) = count;
1086 memcpy(mr->rdParam + 1, str, count);
1087 *(mr->rdParam + ((count + 1) >> 1) + 1) = y;
1088 *(mr->rdParam + ((count + 1) >> 1) + 2) = x;
1089 ret = MF_WriteRecord( dc, mr, mr->rdSize * 2);
1090 GlobalFree16(hmr);
1091 return ret;
1094 /******************************************************************
1095 * MF_ExtTextOut
1097 BOOL32 MF_ExtTextOut(DC*dc, short x, short y, UINT16 flags, const RECT16 *rect,
1098 LPCSTR str, short count, const INT16 *lpDx)
1100 BOOL32 ret;
1101 DWORD len;
1102 HGLOBAL16 hmr;
1103 METARECORD *mr;
1105 len = sizeof(METARECORD) + (((count + 1) >> 1) * 2) + 2 * sizeof(short)
1106 + sizeof(UINT16) + sizeof(RECT16);
1107 if (lpDx)
1108 len+=count*sizeof(INT16);
1109 if (!(hmr = GlobalAlloc16(GMEM_MOVEABLE, len)))
1110 return FALSE;
1111 mr = (METARECORD *)GlobalLock16(hmr);
1112 memset(mr, 0, len);
1114 mr->rdSize = len / 2;
1115 mr->rdFunction = META_EXTTEXTOUT;
1116 *(mr->rdParam) = y;
1117 *(mr->rdParam + 1) = x;
1118 *(mr->rdParam + 2) = count;
1119 *(mr->rdParam + 3) = flags;
1120 if (rect) memcpy(mr->rdParam + 4, rect, sizeof(RECT16));
1121 memcpy(mr->rdParam + 8, str, count);
1122 if (lpDx)
1123 memcpy(mr->rdParam + 8+ ((count + 1) >> 1),lpDx,count*sizeof(INT16));
1124 ret = MF_WriteRecord( dc, mr, mr->rdSize * 2);
1125 GlobalFree16(hmr);
1126 return ret;
1129 /******************************************************************
1130 * MF_MetaPoly - implements Polygon and Polyline
1132 BOOL32 MF_MetaPoly(DC *dc, short func, LPPOINT16 pt, short count)
1134 BOOL32 ret;
1135 DWORD len;
1136 HGLOBAL16 hmr;
1137 METARECORD *mr;
1139 len = sizeof(METARECORD) + (count * 4);
1140 if (!(hmr = GlobalAlloc16(GMEM_MOVEABLE, len)))
1141 return FALSE;
1142 mr = (METARECORD *)GlobalLock16(hmr);
1143 memset(mr, 0, len);
1145 mr->rdSize = len / 2;
1146 mr->rdFunction = func;
1147 *(mr->rdParam) = count;
1148 memcpy(mr->rdParam + 1, pt, count * 4);
1149 ret = MF_WriteRecord( dc, mr, mr->rdSize * 2);
1150 GlobalFree16(hmr);
1151 return ret;
1155 /******************************************************************
1156 * MF_BitBlt
1158 BOOL32 MF_BitBlt(DC *dcDest, short xDest, short yDest, short width,
1159 short height, DC *dcSrc, short xSrc, short ySrc, DWORD rop)
1161 BOOL32 ret;
1162 DWORD len;
1163 HGLOBAL16 hmr;
1164 METARECORD *mr;
1165 BITMAP16 BM;
1167 GetObject16(dcSrc->w.hBitmap, sizeof(BITMAP16), &BM);
1168 len = sizeof(METARECORD) + 12 * sizeof(INT16) + BM.bmWidthBytes * BM.bmHeight;
1169 if (!(hmr = GlobalAlloc16(GMEM_MOVEABLE, len)))
1170 return FALSE;
1171 mr = (METARECORD *)GlobalLock16(hmr);
1172 mr->rdFunction = META_BITBLT;
1173 *(mr->rdParam + 7) = BM.bmWidth;
1174 *(mr->rdParam + 8) = BM.bmHeight;
1175 *(mr->rdParam + 9) = BM.bmWidthBytes;
1176 *(mr->rdParam +10) = BM.bmPlanes;
1177 *(mr->rdParam +11) = BM.bmBitsPixel;
1178 dprintf_metafile(stddeb,"MF_StretchBlt->len = %ld rop=%lx \n",len,rop);
1179 if (GetBitmapBits(dcSrc->w.hBitmap,BM.bmWidthBytes * BM.bmHeight,mr->rdParam +12))
1181 mr->rdSize = len / sizeof(INT16);
1182 *(mr->rdParam) = HIWORD(rop);
1183 *(mr->rdParam + 1) = ySrc;
1184 *(mr->rdParam + 2) = xSrc;
1185 *(mr->rdParam + 3) = height;
1186 *(mr->rdParam + 4) = width;
1187 *(mr->rdParam + 5) = yDest;
1188 *(mr->rdParam + 6) = xDest;
1189 ret = MF_WriteRecord( dcDest, mr, mr->rdSize * 2);
1191 else
1192 ret = FALSE;
1193 GlobalFree16(hmr);
1194 return ret;
1198 /**********************************************************************
1199 * MF_StretchBlt
1200 * this function contains TWO ways for procesing StretchBlt in metafiles,
1201 * decide between rdFunction values META_STRETCHBLT or META_DIBSTRETCHBLT
1202 * via #define STRETCH_VIA_DIB
1204 #define STRETCH_VIA_DIB
1205 #undef STRETCH_VIA_DIB
1206 BOOL32 MF_StretchBlt(DC *dcDest, short xDest, short yDest, short widthDest,
1207 short heightDest, DC *dcSrc, short xSrc, short ySrc,
1208 short widthSrc, short heightSrc, DWORD rop)
1210 BOOL32 ret;
1211 DWORD len;
1212 HGLOBAL16 hmr;
1213 METARECORD *mr;
1214 BITMAP16 BM;
1215 #ifdef STRETCH_VIA_DIB
1216 LPBITMAPINFOHEADER lpBMI;
1217 WORD nBPP;
1218 #endif
1219 GetObject16(dcSrc->w.hBitmap, sizeof(BITMAP16), &BM);
1220 #ifdef STRETCH_VIA_DIB
1221 nBPP = BM.bmPlanes * BM.bmBitsPixel;
1222 len = sizeof(METARECORD) + 10 * sizeof(INT16)
1223 + sizeof(BITMAPINFOHEADER) + (nBPP != 24 ? 1 << nBPP: 0) * sizeof(RGBQUAD)
1224 + ((BM.bmWidth * nBPP + 31) / 32) * 4 * BM.bmHeight;
1225 if (!(hmr = GlobalAlloc16(GMEM_MOVEABLE, len)))
1226 return FALSE;
1227 mr = (METARECORD *)GlobalLock16(hmr);
1228 mr->rdFunction = META_DIBSTRETCHBLT;
1229 lpBMI=(LPBITMAPINFOHEADER)(mr->rdParam+10);
1230 lpBMI->biSize = sizeof(BITMAPINFOHEADER);
1231 lpBMI->biWidth = BM.bmWidth;
1232 lpBMI->biHeight = BM.bmHeight;
1233 lpBMI->biPlanes = 1;
1234 lpBMI->biBitCount = nBPP; /* 1,4,8 or 24 */
1235 lpBMI->biClrUsed = nBPP != 24 ? 1 << nBPP : 0;
1236 lpBMI->biSizeImage = ((lpBMI->biWidth * nBPP + 31) / 32) * 4 * lpBMI->biHeight;
1237 lpBMI->biCompression = BI_RGB;
1238 lpBMI->biXPelsPerMeter = MulDiv32(GetDeviceCaps(dcSrc->hSelf,LOGPIXELSX),3937,100);
1239 lpBMI->biYPelsPerMeter = MulDiv32(GetDeviceCaps(dcSrc->hSelf,LOGPIXELSY),3937,100);
1240 lpBMI->biClrImportant = 0; /* 1 meter = 39.37 inch */
1242 dprintf_metafile(stddeb,"MF_StretchBltViaDIB->len = %ld rop=%lx PixYPM=%ld Caps=%d\n",
1243 len,rop,lpBMI->biYPelsPerMeter,GetDeviceCaps(hdcSrc,LOGPIXELSY));
1244 if (GetDIBits(hdcSrc,dcSrc->w.hBitmap,0,(UINT)lpBMI->biHeight,
1245 MF_GetDIBitsPointer((LPBITMAPINFO)lpBMI), /* DIB bits */
1246 (LPBITMAPINFO)lpBMI,DIB_RGB_COLORS)) /* DIB info structure */
1247 #else
1248 len = sizeof(METARECORD) + 15 * sizeof(INT16) + BM.bmWidthBytes * BM.bmHeight;
1249 if (!(hmr = GlobalAlloc16(GMEM_MOVEABLE, len)))
1250 return FALSE;
1251 mr = (METARECORD *)GlobalLock16(hmr);
1252 mr->rdFunction = META_STRETCHBLT;
1253 *(mr->rdParam +10) = BM.bmWidth;
1254 *(mr->rdParam +11) = BM.bmHeight;
1255 *(mr->rdParam +12) = BM.bmWidthBytes;
1256 *(mr->rdParam +13) = BM.bmPlanes;
1257 *(mr->rdParam +14) = BM.bmBitsPixel;
1258 dprintf_metafile(stddeb,"MF_StretchBlt->len = %ld rop=%lx \n",len,rop);
1259 if (GetBitmapBits(dcSrc->w.hBitmap,BM.bmWidthBytes * BM.bmHeight,mr->rdParam +15))
1260 #endif
1262 mr->rdSize = len / sizeof(INT16);
1263 *(mr->rdParam) = LOWORD(rop);
1264 *(mr->rdParam + 1) = HIWORD(rop);
1265 *(mr->rdParam + 2) = heightSrc;
1266 *(mr->rdParam + 3) = widthSrc;
1267 *(mr->rdParam + 4) = ySrc;
1268 *(mr->rdParam + 5) = xSrc;
1269 *(mr->rdParam + 6) = heightDest;
1270 *(mr->rdParam + 7) = widthDest;
1271 *(mr->rdParam + 8) = yDest;
1272 *(mr->rdParam + 9) = xDest;
1273 ret = MF_WriteRecord( dcDest, mr, mr->rdSize * 2);
1275 else
1276 ret = FALSE;
1277 GlobalFree16(hmr);
1278 return ret;