metafile.c 43.2 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Metafile functions
 *
 * Copyright  David W. Metcalfe, 1994
5 6 7
 * Copyright  Niels de Carpentier, 1996
 * Copyright  Albrecht Kleine, 1996
 * Copyright  Huw Davies, 1996
Alexandre Julliard's avatar
Alexandre Julliard committed
8
 *
9 10 11 12 13 14 15 16 17 18 19 20
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
22 23 24
 *
 * NOTES
 *
25 26 27 28
 * These functions are primarily involved with metafile playback or anything
 * that touches a HMETAFILE.
 * For recording of metafiles look in graphics/metafiledrv/
 *
29
 * Note that (32 bit) HMETAFILEs are GDI objects, while HMETAFILE16s are
30 31 32 33
 * global memory handles so these cannot be interchanged.
 *
 * Memory-based metafiles are just stored as a continuous block of memory with
 * a METAHEADER at the head with METARECORDs appended to it.  mtType is
Austin English's avatar
Austin English committed
34
 * METAFILE_MEMORY (1).  Note this is identical to the disk image of a
35 36 37 38 39 40 41 42 43 44 45
 * disk-based metafile - even mtType is METAFILE_MEMORY.
 * 16bit HMETAFILE16s are global handles to this block
 * 32bit HMETAFILEs are GDI handles METAFILEOBJs, which contains a ptr to
 * the memory.
 * Disk-based metafiles are rather different. HMETAFILE16s point to a
 * METAHEADER which has mtType equal to METAFILE_DISK (2).  Following the 9
 * WORDs of the METAHEADER there are a further 3 WORDs of 0, 1 of 0x117, 1
 * more 0, then 2 which may be a time stamp of the file and then the path of
 * the file (METAHEADERDISK). I've copied this for 16bit compatibility.
 *
 * HDMD - 14/4/1999
46
 */
47

48
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
49
#include <string.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
50
#include <fcntl.h>
51

52 53 54
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
55
#include "winreg.h"
56
#include "winnls.h"
57
#include "winternl.h"
58
#include "gdi_private.h"
59

60
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
61

62
WINE_DEFAULT_DEBUG_CHANNEL(metafile);
63

64 65 66

static CRITICAL_SECTION metafile_cs;
static CRITICAL_SECTION_DEBUG critsect_debug =
67
{
68 69 70
    0, 0, &metafile_cs,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": metafile_cs") }
71
};
72
static CRITICAL_SECTION metafile_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
73

Alexandre Julliard's avatar
Alexandre Julliard committed
74 75 76 77 78
/******************************************************************
 *         MF_AddHandle
 *
 *    Add a handle to an external handle table and return the index
 */
79
static int MF_AddHandle(HANDLETABLE *ht, UINT htlen, HGDIOBJ hobj)
Alexandre Julliard's avatar
Alexandre Julliard committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
{
    int i;

    for (i = 0; i < htlen; i++)
    {
	if (*(ht->objectHandle + i) == 0)
	{
	    *(ht->objectHandle + i) = hobj;
	    return i;
	}
    }
    return -1;
}


/******************************************************************
96 97 98 99 100 101 102 103
 *         MF_Create_HMETATFILE
 *
 * Creates a (32 bit) HMETAFILE object from a METAHEADER
 *
 * HMETAFILEs are GDI objects.
 */
HMETAFILE MF_Create_HMETAFILE(METAHEADER *mh)
{
104
    HANDLE handle;
105

106 107
    if (!(handle = NtGdiCreateClientObj( NTGDI_OBJ_METAFILE )))
        return 0;
108

109 110
    set_gdi_client_ptr( handle, mh );
    return handle;
111 112
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
static int bitmap_info_size( const BITMAPINFO *info, WORD coloruse )
{
    unsigned int colors, size, masks = 0;

    if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
    {
        const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
        colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
        return sizeof(BITMAPCOREHEADER) + colors *
             ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
    }
    else  /* assume BITMAPINFOHEADER */
    {
        if (info->bmiHeader.biClrUsed) colors = min( info->bmiHeader.biClrUsed, 256 );
        else colors = info->bmiHeader.biBitCount > 8 ? 0 : 1 << info->bmiHeader.biBitCount;
        if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
        size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
        return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
    }
}

134 135 136
/******************************************************************
 *         convert_points
 *
137
 * Convert an array of POINTS to an array of POINT.
138 139
 * Result must be freed by caller.
 */
140
static POINT *convert_points( UINT count, const POINTS *pts )
141 142 143 144 145 146 147
{
    UINT i;
    POINT *ret = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*ret) );
    if (ret)
    {
        for (i = 0; i < count; i++)
        {
148 149
            ret[i].x = pts[i].x;
            ret[i].y = pts[i].y;
150 151 152 153 154
        }
    }
    return ret;
}

155
/******************************************************************
156
 *          DeleteMetaFile  (GDI32.@)
157 158 159 160 161 162
 *
 *  Delete a memory-based metafile.
 */

BOOL WINAPI DeleteMetaFile( HMETAFILE hmf )
{
163 164
    METAHEADER *data;
    BOOL ret = FALSE;
165

166 167 168 169 170 171 172 173 174 175
    EnterCriticalSection( &metafile_cs );
    if ((data = get_gdi_client_ptr( hmf, NTGDI_OBJ_METAFILE )))
    {
        ret = NtGdiDeleteClientObj( hmf );
        if (ret) HeapFree( GetProcessHeap(), 0, data );
    }
    LeaveCriticalSection( &metafile_cs );

    if (!ret) SetLastError( ERROR_INVALID_HANDLE );
    return ret;
176 177 178 179 180 181 182 183
}

/******************************************************************
 *         MF_ReadMetaFile
 *
 * Returns a pointer to a memory based METAHEADER read in from file HFILE
 *
 */
184
static METAHEADER *MF_ReadMetaFile(HANDLE hfile)
185 186 187 188 189
{
    METAHEADER *mh;
    DWORD BytesRead, size;

    size = sizeof(METAHEADER);
190
    mh = HeapAlloc( GetProcessHeap(), 0, size );
191 192 193
    if(!mh) return NULL;
    if(ReadFile( hfile, mh, size, &BytesRead, NULL) == 0 ||
       BytesRead != size) {
194
        HeapFree( GetProcessHeap(), 0, mh );
195 196
	return NULL;
    }
197 198 199
    if (mh->mtType != METAFILE_MEMORY || mh->mtVersion != MFVERSION ||
        mh->mtHeaderSize != size / 2)
    {
200 201 202
        HeapFree( GetProcessHeap(), 0, mh );
        return NULL;
    }
203
    size = mh->mtSize * 2;
204
    mh = HeapReAlloc( GetProcessHeap(), 0, mh, size );
205 206 207 208 209
    if(!mh) return NULL;
    size -= sizeof(METAHEADER);
    if(ReadFile( hfile, (char *)mh + sizeof(METAHEADER), size, &BytesRead,
		 NULL) == 0 ||
       BytesRead != size) {
210
        HeapFree( GetProcessHeap(), 0, mh );
211 212 213 214
	return NULL;
    }

    if (mh->mtType != METAFILE_MEMORY) {
215
        WARN("Disk metafile had mtType = %04x\n", mh->mtType);
216 217 218 219 220
	mh->mtType = METAFILE_MEMORY;
    }
    return mh;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
221
/******************************************************************
222
 *         GetMetaFileA   (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
223
 *
224
 *  Read a metafile from a file. Returns handle to a memory-based metafile.
Alexandre Julliard's avatar
Alexandre Julliard committed
225
 */
226
HMETAFILE WINAPI GetMetaFileA( LPCSTR lpFilename )
Alexandre Julliard's avatar
Alexandre Julliard committed
227
{
228
    METAHEADER *mh;
229
    HANDLE hFile;
230

231
    TRACE("%s\n", lpFilename);
232 233 234

    if(!lpFilename)
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
235

236
    if((hFile = CreateFileA(lpFilename, GENERIC_READ, FILE_SHARE_READ, NULL,
237
			    OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
238 239 240 241 242 243
        return 0;

    mh = MF_ReadMetaFile(hFile);
    CloseHandle(hFile);
    if(!mh) return 0;
    return MF_Create_HMETAFILE( mh );
Alexandre Julliard's avatar
Alexandre Julliard committed
244 245
}

Alexandre Julliard's avatar
Alexandre Julliard committed
246
/******************************************************************
247
 *         GetMetaFileW   (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
248
 */
249
HMETAFILE WINAPI GetMetaFileW( LPCWSTR lpFilename )
Alexandre Julliard's avatar
Alexandre Julliard committed
250
{
251
    METAHEADER *mh;
252
    HANDLE hFile;
253

254
    TRACE("%s\n", debugstr_w(lpFilename));
255 256 257 258

    if(!lpFilename)
        return 0;

259
    if((hFile = CreateFileW(lpFilename, GENERIC_READ, FILE_SHARE_READ, NULL,
260
			    OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
261 262 263 264 265 266
        return 0;

    mh = MF_ReadMetaFile(hFile);
    CloseHandle(hFile);
    if(!mh) return 0;
    return MF_Create_HMETAFILE( mh );
Alexandre Julliard's avatar
Alexandre Julliard committed
267 268 269
}


270 271 272
/* return a copy of the metafile bits, to be freed with HeapFree */
static METAHEADER *get_metafile_bits( HMETAFILE hmf )
{
273
    METAHEADER *ret = NULL, *metafile;
274

275 276 277 278 279 280 281 282
    EnterCriticalSection( &metafile_cs );
    if ((metafile = get_gdi_client_ptr( hmf, NTGDI_OBJ_METAFILE )))
    {
        ret = HeapAlloc( GetProcessHeap(), 0, metafile->mtSize * 2 );
        if (ret) memcpy( ret, metafile, metafile->mtSize * 2 );
    }
    else SetLastError( ERROR_INVALID_HANDLE );
    LeaveCriticalSection( &metafile_cs );
283 284 285 286

    return ret;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
287
/******************************************************************
288
 *         CopyMetaFileW   (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
289 290 291 292 293
 *
 *  Copies the metafile corresponding to hSrcMetaFile to either
 *  a disk file, if a filename is given, or to a new memory based
 *  metafile, if lpFileName is NULL.
 *
294 295 296
 * PARAMS
 *  hSrcMetaFile [I] handle of metafile to copy
 *  lpFilename   [I] filename if copying to a file
Alexandre Julliard's avatar
Alexandre Julliard committed
297
 *
298
 * RETURNS
Alexandre Julliard's avatar
Alexandre Julliard committed
299 300 301 302
 *  Handle to metafile copy on success, NULL on failure.
 *
 * BUGS
 *  Copying to disk returns NULL even if successful.
Alexandre Julliard's avatar
Alexandre Julliard committed
303
 */
304
HMETAFILE WINAPI CopyMetaFileW( HMETAFILE hSrcMetaFile, LPCWSTR lpFilename )
Jon Griffiths's avatar
Jon Griffiths committed
305
{
306
    METAHEADER *mh = get_metafile_bits( hSrcMetaFile );
307
    HANDLE hFile;
308

309
    TRACE("(%p,%s)\n", hSrcMetaFile, debugstr_w(lpFilename));
310

311
    if(!mh) return 0;
312

313
    if(lpFilename) {         /* disk based metafile */
314
        DWORD w;
315
        if((hFile = CreateFileW(lpFilename, GENERIC_WRITE, 0, NULL,
316
				CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
317
	    HeapFree( GetProcessHeap(), 0, mh );
Alexandre Julliard's avatar
Alexandre Julliard committed
318
	    return 0;
319
	}
320
	WriteFile(hFile, mh, mh->mtSize * 2, &w, NULL);
321 322
	CloseHandle(hFile);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
323

324
    return MF_Create_HMETAFILE( mh );
Alexandre Julliard's avatar
Alexandre Julliard committed
325 326
}

Alexandre Julliard's avatar
Alexandre Julliard committed
327 328

/******************************************************************
329
 *         CopyMetaFileA   (GDI32.@)
330 331
 *
 * See CopyMetaFileW.
Alexandre Julliard's avatar
Alexandre Julliard committed
332
 */
333
HMETAFILE WINAPI CopyMetaFileA( HMETAFILE hSrcMetaFile, LPCSTR lpFilename )
Alexandre Julliard's avatar
Alexandre Julliard committed
334
{
335
    UNICODE_STRING lpFilenameW;
336 337
    HMETAFILE ret = 0;

338 339 340
    if (lpFilename) RtlCreateUnicodeStringFromAsciiz(&lpFilenameW, lpFilename);
    else lpFilenameW.Buffer = NULL;

341 342 343
    ret = CopyMetaFileW( hSrcMetaFile, lpFilenameW.Buffer );
    if (lpFilenameW.Buffer)
        RtlFreeUnicodeString(&lpFilenameW);
Alexandre Julliard's avatar
Alexandre Julliard committed
344 345 346
    return ret;
}

347 348 349 350 351 352 353 354 355
/******************************************************************
 *         PlayMetaFile   (GDI32.@)
 *
 *  Renders the metafile specified by hmf in the DC specified by
 *  hdc. Returns FALSE on failure, TRUE on success.
 *
 * PARAMS
 *  hdc [I] handle of DC to render in
 *  hmf [I] handle of metafile to render
Alexandre Julliard's avatar
Alexandre Julliard committed
356
 *
357 358 359
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
Alexandre Julliard's avatar
Alexandre Julliard committed
360
 */
361
BOOL WINAPI PlayMetaFile( HDC hdc, HMETAFILE hmf )
Alexandre Julliard's avatar
Alexandre Julliard committed
362
{
363
    BOOL metadc = GetObjectType(hdc) == OBJ_METADC;
364
    METAHEADER *mh = get_metafile_bits( hmf );
Alexandre Julliard's avatar
Alexandre Julliard committed
365
    METARECORD *mr;
366
    HANDLETABLE *ht;
367
    unsigned int offset = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
368
    WORD i;
369 370
    HPEN hPen;
    HBRUSH hBrush;
371 372
    HPALETTE hPal;
    HRGN hRgn;
373

Alexandre Julliard's avatar
Alexandre Julliard committed
374
    if (!mh) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
375

376
    if (!metadc)
377
    {
378 379 380 381 382 383 384 385 386 387 388
        /* save DC */
        hPen = GetCurrentObject(hdc, OBJ_PEN);
        hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
        hPal = GetCurrentObject(hdc, OBJ_PAL);

        hRgn = NtGdiCreateRectRgn(0, 0, 0, 0);
        if (!GetClipRgn(hdc, hRgn))
        {
            DeleteObject(hRgn);
            hRgn = 0;
        }
389
    }
390

Alexandre Julliard's avatar
Alexandre Julliard committed
391
    /* create the handle table */
392
    ht = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
393
		    sizeof(HANDLETABLE) * mh->mtNoObjects);
394 395 396 397 398
    if(!ht)
    {
        HeapFree( GetProcessHeap(), 0, mh );
        return FALSE;
    }
399

Alexandre Julliard's avatar
Alexandre Julliard committed
400
    /* loop through metafile playing records */
Alexandre Julliard's avatar
Alexandre Julliard committed
401 402
    offset = mh->mtHeaderSize * 2;
    while (offset < mh->mtSize * 2)
Alexandre Julliard's avatar
Alexandre Julliard committed
403
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
404
        mr = (METARECORD *)((char *)mh + offset);
405
	TRACE("offset=%04x,size=%08lx\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
406
            offset, mr->rdSize);
407
	if (mr->rdSize < 3) { /* catch illegal record sizes */
408
            TRACE("Entry got size %ld at offset %d, total mf length is %ld\n",
409 410
                  mr->rdSize,offset,mh->mtSize*2);
            break;
Alexandre Julliard's avatar
Alexandre Julliard committed
411
	}
412

Alexandre Julliard's avatar
Alexandre Julliard committed
413
	offset += mr->rdSize * 2;
414 415 416 417
	if (mr->rdFunction == META_EOF) {
	    TRACE("Got META_EOF so stopping\n");
	    break;
	}
418
	PlayMetaFileRecord( hdc, ht, mr, mh->mtNoObjects );
Alexandre Julliard's avatar
Alexandre Julliard committed
419 420
    }

421 422 423 424 425 426 427 428 429
    if (!metadc)
    {
        /* restore DC */
        SelectObject(hdc, hPen);
        SelectObject(hdc, hBrush);
        SelectPalette(hdc, hPal, FALSE);
        ExtSelectClipRgn(hdc, hRgn, RGN_COPY);
        DeleteObject(hRgn);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
430

Alexandre Julliard's avatar
Alexandre Julliard committed
431 432 433
    /* free objects in handle table */
    for(i = 0; i < mh->mtNoObjects; i++)
      if(*(ht->objectHandle + i) != 0)
434
        DeleteObject(*(ht->objectHandle + i));
435

436
    HeapFree( GetProcessHeap(), 0, ht );
437
    HeapFree( GetProcessHeap(), 0, mh );
Alexandre Julliard's avatar
Alexandre Julliard committed
438 439 440
    return TRUE;
}

441
/******************************************************************
442
 *            EnumMetaFile   (GDI32.@)
443 444 445 446
 *
 *  Loop through the metafile records in hmf, calling the user-specified
 *  function for each one, stopping when the user's function returns FALSE
 *  (which is considered to be failure)
447
 *  or when no records are left (which is considered to be success).
448 449 450 451
 *
 * RETURNS
 *  TRUE on success, FALSE on failure.
 */
452
BOOL WINAPI EnumMetaFile(HDC hdc, HMETAFILE hmf, MFENUMPROC lpEnumFunc, LPARAM lpData)
Jon Griffiths's avatar
Jon Griffiths committed
453
{
454
    METAHEADER *mh = get_metafile_bits( hmf );
Alexandre Julliard's avatar
Alexandre Julliard committed
455
    METARECORD *mr;
456
    HANDLETABLE *ht;
Alexandre Julliard's avatar
Alexandre Julliard committed
457
    BOOL result = TRUE;
458 459
    int i;
    unsigned int offset = 0;
460 461 462
    HPEN hPen;
    HBRUSH hBrush;
    HFONT hFont;
Alexandre Julliard's avatar
Alexandre Julliard committed
463

464
    TRACE("(%p,%p,%p,%Ix)\n", hdc, hmf, lpEnumFunc, lpData);
465

466
    if (!mh) return FALSE;
467

Alexandre Julliard's avatar
Alexandre Julliard committed
468
    /* save the current pen, brush and font */
469 470 471
    hPen = GetCurrentObject(hdc, OBJ_PEN);
    hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
    hFont = GetCurrentObject(hdc, OBJ_FONT);
Alexandre Julliard's avatar
Alexandre Julliard committed
472

473
    ht = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
474
			    sizeof(HANDLETABLE) * mh->mtNoObjects);
475

Alexandre Julliard's avatar
Alexandre Julliard committed
476 477
    /* loop through metafile records */
    offset = mh->mtHeaderSize * 2;
478

Alexandre Julliard's avatar
Alexandre Julliard committed
479 480 481
    while (offset < (mh->mtSize * 2))
    {
	mr = (METARECORD *)((char *)mh + offset);
482 483 484 485
	if(mr->rdFunction == META_EOF) {
	    TRACE("Got META_EOF so stopping\n");
	    break;
	}
486
	TRACE("Calling EnumFunc with record type %x\n",
487
	      mr->rdFunction);
488
        if (!lpEnumFunc( hdc, ht, mr, mh->mtNoObjects, lpData ))
Alexandre Julliard's avatar
Alexandre Julliard committed
489 490 491 492
	{
	    result = FALSE;
	    break;
	}
493

Alexandre Julliard's avatar
Alexandre Julliard committed
494 495 496 497
	offset += (mr->rdSize * 2);
    }

    /* restore pen, brush and font */
498 499 500
    SelectObject(hdc, hBrush);
    SelectObject(hdc, hPen);
    SelectObject(hdc, hFont);
Alexandre Julliard's avatar
Alexandre Julliard committed
501 502 503 504

    /* free objects in handle table */
    for(i = 0; i < mh->mtNoObjects; i++)
      if(*(ht->objectHandle + i) != 0)
505
        DeleteObject(*(ht->objectHandle + i));
Alexandre Julliard's avatar
Alexandre Julliard committed
506

507
    HeapFree( GetProcessHeap(), 0, ht);
508
    HeapFree( GetProcessHeap(), 0, mh);
Alexandre Julliard's avatar
Alexandre Julliard committed
509 510 511
    return result;
}

512
static BOOL MF_Play_MetaCreateRegion( METARECORD *mr, HRGN hrgn );
513
static BOOL MF_Play_MetaExtTextOut(HDC hdc, METARECORD *mr);
Alexandre Julliard's avatar
Alexandre Julliard committed
514
/******************************************************************
515
 *         PlayMetaFileRecord   (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
516 517
 *
 *   Render a single metafile record specified by *mr in the DC hdc, while
518
 *   using the handle table *ht, of length handles,
Alexandre Julliard's avatar
Alexandre Julliard committed
519 520 521 522 523
 *   to store metafile objects.
 *
 * BUGS
 *  The following metafile records are unimplemented:
 *
524
 *  DRAWTEXT, ANIMATEPALETTE, SETPALENTRIES,
Alexandre Julliard's avatar
Alexandre Julliard committed
525 526
 *  RESIZEPALETTE, EXTFLOODFILL, RESETDC, STARTDOC, STARTPAGE, ENDPAGE,
 *  ABORTDOC, ENDDOC, CREATEBRUSH, CREATEBITMAPINDIRECT, and CREATEBITMAP.
Alexandre Julliard's avatar
Alexandre Julliard committed
527
 */
528 529
BOOL WINAPI PlayMetaFileRecord( HDC hdc,  HANDLETABLE *ht, METARECORD *mr, UINT handles )
{
Alexandre Julliard's avatar
Alexandre Julliard committed
530
    short s1;
531
    POINT *pt;
Alexandre Julliard's avatar
Alexandre Julliard committed
532 533
    BITMAPINFOHEADER *infohdr;

534
    TRACE("(%p %p %p %u) function %04x\n", hdc, ht, mr, handles, mr->rdFunction);
535

Alexandre Julliard's avatar
Alexandre Julliard committed
536 537
    switch (mr->rdFunction)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
538
    case META_EOF:
539
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
540 541

    case META_DELETEOBJECT:
542 543 544
        DeleteObject(*(ht->objectHandle + mr->rdParm[0]));
        *(ht->objectHandle + mr->rdParm[0]) = 0;
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
545

Alexandre Julliard's avatar
Alexandre Julliard committed
546
    case META_SETBKCOLOR:
547 548
        SetBkColor(hdc, MAKELONG(mr->rdParm[0], mr->rdParm[1]));
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
549 550

    case META_SETBKMODE:
551 552
        SetBkMode(hdc, mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
553 554

    case META_SETMAPMODE:
555 556
        SetMapMode(hdc, mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
557 558

    case META_SETROP2:
559 560
        SetROP2(hdc, mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
561 562

    case META_SETRELABS:
563 564
        SetRelAbs(hdc, mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
565 566

    case META_SETPOLYFILLMODE:
567 568
        SetPolyFillMode(hdc, mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
569 570

    case META_SETSTRETCHBLTMODE:
571 572
        SetStretchBltMode(hdc, mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
573

Alexandre Julliard's avatar
Alexandre Julliard committed
574
    case META_SETTEXTCOLOR:
575 576
        SetTextColor(hdc, MAKELONG(mr->rdParm[0], mr->rdParm[1]));
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
577 578

    case META_SETWINDOWORG:
579 580
        SetWindowOrgEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
581 582

    case META_SETWINDOWEXT:
583 584
        SetWindowExtEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
585 586

    case META_SETVIEWPORTORG:
587 588
        SetViewportOrgEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
589 590

    case META_SETVIEWPORTEXT:
591 592
        SetViewportExtEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
593 594

    case META_OFFSETWINDOWORG:
595 596
        OffsetWindowOrgEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
597 598

    case META_SCALEWINDOWEXT:
599 600 601
        ScaleWindowExtEx(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                              (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
602 603

    case META_OFFSETVIEWPORTORG:
604 605
        OffsetViewportOrgEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
606 607

    case META_SCALEVIEWPORTEXT:
608 609 610
        ScaleViewportExtEx(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                                (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
611 612

    case META_LINETO:
613 614
        LineTo(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
615 616

    case META_MOVETO:
617 618
        MoveToEx(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0], NULL);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
619 620

    case META_EXCLUDECLIPRECT:
621 622 623
        ExcludeClipRect( hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                              (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0] );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
624 625

    case META_INTERSECTCLIPRECT:
626 627 628
        IntersectClipRect( hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                                (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0] );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
629 630

    case META_ARC:
631 632 633 634 635
        Arc(hdc, (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6],
                 (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
                 (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
636 637

    case META_ELLIPSE:
638 639 640
        Ellipse(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                     (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
641 642

    case META_FLOODFILL:
643 644 645
        FloodFill(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                    MAKELONG(mr->rdParm[0], mr->rdParm[1]));
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
646 647

    case META_PIE:
648 649 650 651 652
        Pie(hdc, (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6],
                 (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
                 (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
653 654

    case META_RECTANGLE:
655 656 657
        Rectangle(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                       (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
658 659

    case META_ROUNDRECT:
660 661 662 663
        RoundRect(hdc, (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
                       (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                       (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
664 665

    case META_PATBLT:
666 667 668 669
        PatBlt(hdc, (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
                    (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                    MAKELONG(mr->rdParm[0], mr->rdParm[1]));
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
670 671

    case META_SAVEDC:
672 673
        SaveDC(hdc);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
674 675

    case META_SETPIXEL:
676 677 678
        SetPixel(hdc, (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                 MAKELONG(mr->rdParm[0], mr->rdParm[1]));
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
679 680

    case META_OFFSETCLIPRGN:
681 682
        OffsetClipRgn( hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0] );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
683 684

    case META_TEXTOUT:
685 686 687 688 689
        s1 = mr->rdParm[0];
        TextOutA(hdc, (SHORT)mr->rdParm[((s1 + 1) >> 1) + 2],
                 (SHORT)mr->rdParm[((s1 + 1) >> 1) + 1],
                 (char *)(mr->rdParm + 1), s1);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
690 691

    case META_POLYGON:
692
        if ((pt = convert_points( mr->rdParm[0], (POINTS *)(mr->rdParm + 1))))
693 694 695 696 697
        {
            Polygon(hdc, pt, mr->rdParm[0]);
            HeapFree( GetProcessHeap(), 0, pt );
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
698

Alexandre Julliard's avatar
Alexandre Julliard committed
699
    case META_POLYPOLYGON:
700 701 702 703 704
        {
            UINT i, total;
            SHORT *counts = (SHORT *)(mr->rdParm + 1);

            for (i = total = 0; i < mr->rdParm[0]; i++) total += counts[i];
705
            pt = convert_points( total, (POINTS *)(counts + mr->rdParm[0]) );
706 707 708 709 710 711 712 713 714 715 716 717 718
            if (pt)
            {
                INT *cnt32 = HeapAlloc( GetProcessHeap(), 0, mr->rdParm[0] * sizeof(*cnt32) );
                if (cnt32)
                {
                    for (i = 0; i < mr->rdParm[0]; i++) cnt32[i] = counts[i];
                    PolyPolygon( hdc, pt, cnt32, mr->rdParm[0]);
                    HeapFree( GetProcessHeap(), 0, cnt32 );
                }
            }
            HeapFree( GetProcessHeap(), 0, pt );
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
719

Alexandre Julliard's avatar
Alexandre Julliard committed
720
    case META_POLYLINE:
721
        if ((pt = convert_points( mr->rdParm[0], (POINTS *)(mr->rdParm + 1))))
722 723 724 725 726
        {
            Polyline( hdc, pt, mr->rdParm[0] );
            HeapFree( GetProcessHeap(), 0, pt );
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
727 728

    case META_RESTOREDC:
729 730
        RestoreDC(hdc, (SHORT)mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
731 732

    case META_SELECTOBJECT:
733 734
        SelectObject(hdc, *(ht->objectHandle + mr->rdParm[0]));
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
735 736

    case META_CHORD:
737 738 739 740 741
        Chord(hdc, (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6],
                   (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
                   (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                   (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
742 743

    case META_CREATEPATTERNBRUSH:
744 745 746 747 748 749 750 751 752
        switch (mr->rdParm[0])
        {
        case BS_PATTERN:
            infohdr = (BITMAPINFOHEADER *)(mr->rdParm + 2);
            MF_AddHandle(ht, handles,
                         CreatePatternBrush(CreateBitmap(infohdr->biWidth,
                                      infohdr->biHeight,
                                      infohdr->biPlanes,
                                      infohdr->biBitCount,
753 754
                                      mr->rdParm +
                                      (sizeof(BITMAPINFOHEADER) / 2) + 4)));
755 756 757 758 759 760 761 762 763 764 765 766 767
            break;

        case BS_DIBPATTERN:
            infohdr = (BITMAPINFOHEADER *)(mr->rdParm + 2);
            MF_AddHandle(ht, handles, CreateDIBPatternBrushPt( infohdr, mr->rdParm[1] ));
            break;

        default:
            ERR("META_CREATEPATTERNBRUSH: Unknown pattern type %d\n",
                mr->rdParm[0]);
            break;
        }
        break;
768

Alexandre Julliard's avatar
Alexandre Julliard committed
769
    case META_CREATEPENINDIRECT:
770 771 772 773 774 775 776 777 778
        {
            LOGPEN pen;
            pen.lopnStyle = mr->rdParm[0];
            pen.lopnWidth.x = (SHORT)mr->rdParm[1];
            pen.lopnWidth.y = (SHORT)mr->rdParm[2];
            pen.lopnColor = MAKELONG( mr->rdParm[3], mr->rdParm[4] );
            MF_AddHandle(ht, handles, CreatePenIndirect( &pen ));
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
779

Alexandre Julliard's avatar
Alexandre Julliard committed
780
    case META_CREATEFONTINDIRECT:
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
        {
            LOGFONTA font;
            font.lfHeight         = (SHORT)mr->rdParm[0];
            font.lfWidth          = (SHORT)mr->rdParm[1];
            font.lfEscapement     = (SHORT)mr->rdParm[2];
            font.lfOrientation    = (SHORT)mr->rdParm[3];
            font.lfWeight         = (SHORT)mr->rdParm[4];
            font.lfItalic         = LOBYTE(mr->rdParm[5]);
            font.lfUnderline      = HIBYTE(mr->rdParm[5]);
            font.lfStrikeOut      = LOBYTE(mr->rdParm[6]);
            font.lfCharSet        = HIBYTE(mr->rdParm[6]);
            font.lfOutPrecision   = LOBYTE(mr->rdParm[7]);
            font.lfClipPrecision  = HIBYTE(mr->rdParm[7]);
            font.lfQuality        = LOBYTE(mr->rdParm[8]);
            font.lfPitchAndFamily = HIBYTE(mr->rdParm[8]);
            memcpy( font.lfFaceName, mr->rdParm + 9, LF_FACESIZE );
            MF_AddHandle(ht, handles, CreateFontIndirectA( &font ));
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
800

Alexandre Julliard's avatar
Alexandre Julliard committed
801
    case META_CREATEBRUSHINDIRECT:
802 803 804 805 806 807 808 809
        {
            LOGBRUSH brush;
            brush.lbStyle = mr->rdParm[0];
            brush.lbColor = MAKELONG( mr->rdParm[1], mr->rdParm[2] );
            brush.lbHatch = mr->rdParm[3];
            MF_AddHandle(ht, handles, CreateBrushIndirect( &brush ));
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
810

Alexandre Julliard's avatar
Alexandre Julliard committed
811
    case META_CREATEPALETTE:
812 813
        MF_AddHandle(ht, handles, CreatePalette((LPLOGPALETTE)mr->rdParm));
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
814 815

    case META_SETTEXTALIGN:
816 817
        SetTextAlign(hdc, mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
818 819

    case META_SELECTPALETTE:
820
        SelectPalette( hdc, *(ht->objectHandle + mr->rdParm[1]), mr->rdParm[0] );
821
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
822 823

    case META_SETMAPPERFLAGS:
824 825
        SetMapperFlags(hdc, MAKELONG(mr->rdParm[0],mr->rdParm[1]));
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
826 827

    case META_REALIZEPALETTE:
828
        RealizePalette(hdc);
829
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
830 831

    case META_ESCAPE:
832 833
        switch (mr->rdParm[0]) {
        case GETSCALINGFACTOR: /* get function ... would just NULL dereference */
834 835
        case GETPHYSPAGESIZE:
        case GETPRINTINGOFFSET:
836 837 838 839 840
             return FALSE;
        case SETABORTPROC:
             FIXME("Filtering Escape(SETABORTPROC), possible virus?\n");
             return FALSE;
        }
841
        Escape(hdc, mr->rdParm[0], mr->rdParm[1], (LPCSTR)&mr->rdParm[2], NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
842 843
        break;

Alexandre Julliard's avatar
Alexandre Julliard committed
844
    case META_EXTTEXTOUT:
845
        MF_Play_MetaExtTextOut( hdc, mr );
846
        break;
847

Alexandre Julliard's avatar
Alexandre Julliard committed
848 849
    case META_STRETCHDIB:
      {
850
        LPBITMAPINFO info = (LPBITMAPINFO) &(mr->rdParm[11]);
851
        LPSTR bits = (LPSTR)info + bitmap_info_size( info, mr->rdParm[2] );
852 853 854
        StretchDIBits( hdc, (SHORT)mr->rdParm[10], (SHORT)mr->rdParm[9], (SHORT)mr->rdParm[8],
                       (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6], (SHORT)mr->rdParm[5],
                       (SHORT)mr->rdParm[4], (SHORT)mr->rdParm[3], bits, info,
855
                       mr->rdParm[2],MAKELONG(mr->rdParm[0],mr->rdParm[1]));
Alexandre Julliard's avatar
Alexandre Julliard committed
856 857
      }
      break;
Alexandre Julliard's avatar
Alexandre Julliard committed
858 859 860

    case META_DIBSTRETCHBLT:
      {
861
        LPBITMAPINFO info = (LPBITMAPINFO) &(mr->rdParm[10]);
862
        LPSTR bits = (LPSTR)info + bitmap_info_size( info, DIB_RGB_COLORS );
863 864 865
        StretchDIBits( hdc, (SHORT)mr->rdParm[9], (SHORT)mr->rdParm[8], (SHORT)mr->rdParm[7],
                       (SHORT)mr->rdParm[6], (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
                       (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2], bits, info,
866
                       DIB_RGB_COLORS,MAKELONG(mr->rdParm[0],mr->rdParm[1]));
Alexandre Julliard's avatar
Alexandre Julliard committed
867
      }
868
      break;
Alexandre Julliard's avatar
Alexandre Julliard committed
869 870 871

    case META_STRETCHBLT:
      {
872
        HDC hdcSrc = NtGdiCreateCompatibleDC( hdc );
873 874 875 876
        HBITMAP hbitmap = CreateBitmap(mr->rdParm[10], /*Width */
                                       mr->rdParm[11], /*Height*/
                                       mr->rdParm[13], /*Planes*/
                                       mr->rdParm[14], /*BitsPixel*/
877
                                       &mr->rdParm[15]); /*bits*/
878 879 880 881 882 883 884
        SelectObject(hdcSrc,hbitmap);
        StretchBlt(hdc, (SHORT)mr->rdParm[9], (SHORT)mr->rdParm[8],
                   (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6],
                   hdcSrc, (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4],
                   (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                   MAKELONG(mr->rdParm[0],mr->rdParm[1]));
        DeleteDC(hdcSrc);
Alexandre Julliard's avatar
Alexandre Julliard committed
885 886 887
      }
      break;

888
    case META_BITBLT:
Alexandre Julliard's avatar
Alexandre Julliard committed
889
      {
890
        HDC hdcSrc = NtGdiCreateCompatibleDC( hdc );
891
        HBITMAP hbitmap = CreateBitmap(mr->rdParm[7]/*Width */,
892 893 894
                                        mr->rdParm[8]/*Height*/,
                                        mr->rdParm[10]/*Planes*/,
                                        mr->rdParm[11]/*BitsPixel*/,
895
                                        &mr->rdParm[12]/*bits*/);
896 897 898 899
        SelectObject(hdcSrc,hbitmap);
        BitBlt(hdc,(SHORT)mr->rdParm[6],(SHORT)mr->rdParm[5],
                (SHORT)mr->rdParm[4],(SHORT)mr->rdParm[3],
                hdcSrc, (SHORT)mr->rdParm[2],(SHORT)mr->rdParm[1],
900
                MAKELONG(0,mr->rdParm[0]));
901
        DeleteDC(hdcSrc);
Alexandre Julliard's avatar
Alexandre Julliard committed
902 903
      }
      break;
Alexandre Julliard's avatar
Alexandre Julliard committed
904 905

    case META_CREATEREGION:
Alexandre Julliard's avatar
Alexandre Julliard committed
906
      {
907
        HRGN hrgn = NtGdiCreateRectRgn(0,0,0,0);
908

909 910
        MF_Play_MetaCreateRegion(mr, hrgn);
        MF_AddHandle(ht, handles, hrgn);
Alexandre Julliard's avatar
Alexandre Julliard committed
911 912
      }
      break;
Alexandre Julliard's avatar
Alexandre Julliard committed
913

914
    case META_FILLREGION:
915 916
        FillRgn(hdc, *(ht->objectHandle + mr->rdParm[1]),
                *(ht->objectHandle + mr->rdParm[0]));
917 918 919
        break;

    case META_FRAMEREGION:
920 921 922
        FrameRgn(hdc, *(ht->objectHandle + mr->rdParm[3]),
                 *(ht->objectHandle + mr->rdParm[2]),
                 (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
Alexandre Julliard's avatar
Alexandre Julliard committed
923 924
        break;

925
    case META_INVERTREGION:
926
        InvertRgn(hdc, *(ht->objectHandle + mr->rdParm[0]));
927
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
928

929
    case META_PAINTREGION:
930
        PaintRgn(hdc, *(ht->objectHandle + mr->rdParm[0]));
Alexandre Julliard's avatar
Alexandre Julliard committed
931 932
        break;

933
    case META_SELECTCLIPREGION:
934 935 936 937 938 939
        {
            HRGN hrgn = 0;

            if (mr->rdParm[0]) hrgn = *(ht->objectHandle + mr->rdParm[0]);
            SelectClipRgn(hdc, hrgn);
        }
940
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
941

942
    case META_DIBCREATEPATTERNBRUSH:
943 944 945 946
        /*  mr->rdParm[0] may be BS_PATTERN or BS_DIBPATTERN:
            but there's no difference */
        MF_AddHandle(ht, handles, CreateDIBPatternBrushPt( mr->rdParm + 2, mr->rdParm[1] ));
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
947

948 949
    case META_DIBBITBLT:
      /* In practice I've found that there are two layouts for
950 951 952 953
         META_DIBBITBLT, one (the first here) is the usual one when a src
         dc is actually passed to it, the second occurs when the src dc is
         passed in as NULL to the creating BitBlt. As the second case has
         no dib, a size check will suffice to distinguish.
954

955
         Caolan.McNamara@ul.ie */
956 957

        if (mr->rdSize > 12) {
958
            LPBITMAPINFO info = (LPBITMAPINFO) &(mr->rdParm[8]);
959
            LPSTR bits = (LPSTR)info + bitmap_info_size(info, mr->rdParm[0]);
960 961 962 963 964 965 966 967 968 969 970

            StretchDIBits(hdc, (SHORT)mr->rdParm[7], (SHORT)mr->rdParm[6], (SHORT)mr->rdParm[5],
                          (SHORT)mr->rdParm[4], (SHORT)mr->rdParm[3], (SHORT)mr->rdParm[2],
                          (SHORT)mr->rdParm[5], (SHORT)mr->rdParm[4], bits, info,
                          DIB_RGB_COLORS, MAKELONG(mr->rdParm[0], mr->rdParm[1]));
        }
        else /* equivalent to a PatBlt */
            PatBlt(hdc, (SHORT)mr->rdParm[8], (SHORT)mr->rdParm[7],
                   (SHORT)mr->rdParm[6], (SHORT)mr->rdParm[5],
                   MAKELONG(mr->rdParm[0], mr->rdParm[1]));
        break;
971

972
    case META_SETTEXTCHAREXTRA:
973 974
        SetTextCharacterExtra(hdc, (SHORT)mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
975

976
    case META_SETTEXTJUSTIFICATION:
977 978
        SetTextJustification(hdc, (SHORT)mr->rdParm[1], (SHORT)mr->rdParm[0]);
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
979

980
    case META_EXTFLOODFILL:
981 982 983
        ExtFloodFill(hdc, (SHORT)mr->rdParm[4], (SHORT)mr->rdParm[3],
                     MAKELONG(mr->rdParm[1], mr->rdParm[2]),
                     mr->rdParm[0]);
984 985
        break;

986
    case META_SETDIBTODEV:
987 988
        {
            BITMAPINFO *info = (BITMAPINFO *) &(mr->rdParm[9]);
989
            char *bits = (char *)info + bitmap_info_size( info, mr->rdParm[0] );
990 991 992 993 994 995 996
            SetDIBitsToDevice(hdc, (SHORT)mr->rdParm[8], (SHORT)mr->rdParm[7],
                              (SHORT)mr->rdParm[6], (SHORT)mr->rdParm[5],
                              (SHORT)mr->rdParm[4], (SHORT)mr->rdParm[3],
                              mr->rdParm[2], mr->rdParm[1], bits, info,
                              mr->rdParm[0]);
            break;
        }
997 998

#define META_UNIMP(x) case x: \
999
FIXME("PlayMetaFileRecord:record type "#x" not implemented.\n"); \
1000
break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
    META_UNIMP(META_DRAWTEXT)
    META_UNIMP(META_ANIMATEPALETTE)
    META_UNIMP(META_SETPALENTRIES)
    META_UNIMP(META_RESIZEPALETTE)
    META_UNIMP(META_RESETDC)
    META_UNIMP(META_STARTDOC)
    META_UNIMP(META_STARTPAGE)
    META_UNIMP(META_ENDPAGE)
    META_UNIMP(META_ABORTDOC)
    META_UNIMP(META_ENDDOC)
    META_UNIMP(META_CREATEBRUSH)
    META_UNIMP(META_CREATEBITMAPINDIRECT)
    META_UNIMP(META_CREATEBITMAP)
#undef META_UNIMP
Alexandre Julliard's avatar
Alexandre Julliard committed
1015

Alexandre Julliard's avatar
Alexandre Julliard committed
1016
    default:
1017 1018
        WARN("PlayMetaFileRecord: Unknown record type %x\n", mr->rdFunction);
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1019
    }
1020
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1021 1022 1023
}

/******************************************************************
1024
 *         SetMetaFileBitsEx    (GDI32.@)
1025
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1026
 *  Create a metafile from raw data. No checking of the data is performed.
1027 1028 1029 1030 1031
 *  Use GetMetaFileBitsEx() to get raw data from a metafile.
 *
 * PARAMS
 *  size   [I] size of metafile, in bytes
 *  lpData [I] pointer to metafile data
1032 1033 1034 1035
 *
 * RETURNS
 *  Success: Handle to metafile.
 *  Failure: NULL.
Alexandre Julliard's avatar
Alexandre Julliard committed
1036
 */
1037
HMETAFILE WINAPI SetMetaFileBitsEx( UINT size, const BYTE *lpData )
Alexandre Julliard's avatar
Alexandre Julliard committed
1038
{
1039 1040
    const METAHEADER *mh_in = (const METAHEADER *)lpData;
    METAHEADER *mh_out;
1041 1042 1043

    if (size & 1) return 0;

1044 1045
    if (!size || mh_in->mtType != METAFILE_MEMORY || mh_in->mtVersion != MFVERSION ||
        mh_in->mtHeaderSize != sizeof(METAHEADER) / 2)
1046 1047 1048 1049 1050
    {
        SetLastError(ERROR_INVALID_DATA);
        return 0;
    }

1051 1052
    mh_out = HeapAlloc( GetProcessHeap(), 0, size );
    if (!mh_out)
1053 1054 1055 1056 1057
    {
        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
        return 0;
    }

1058 1059 1060
    memcpy(mh_out, mh_in, size);
    mh_out->mtSize = size / 2;
    return MF_Create_HMETAFILE(mh_out);
Alexandre Julliard's avatar
Alexandre Julliard committed
1061 1062
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1063
/*****************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
1064 1065 1066
 *  GetMetaFileBitsEx     (GDI32.@)
 *
 * Get raw metafile data.
1067
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1068
 *  Copies the data from metafile _hmf_ into the buffer _buf_.
1069 1070 1071 1072 1073
 *
 * PARAMS
 *  hmf   [I] metafile
 *  nSize [I] size of buf
 *  buf   [O] buffer to receive raw metafile data
1074 1075 1076 1077
 *
 * RETURNS
 *  If _buf_ is zero, returns size of buffer required. Otherwise,
 *  returns number of bytes copied.
Alexandre Julliard's avatar
Alexandre Julliard committed
1078
 */
1079
UINT WINAPI GetMetaFileBitsEx( HMETAFILE hmf, UINT buf_size, void *buf )
Jon Griffiths's avatar
Jon Griffiths committed
1080
{
1081 1082
    METAHEADER *metafile;
    UINT size = 0;
1083

1084
    TRACE( "(%p,%d,%p)\n", hmf, buf_size, buf );
1085

1086 1087
    EnterCriticalSection( &metafile_cs );
    if ((metafile = get_gdi_client_ptr( hmf, NTGDI_OBJ_METAFILE )))
1088
    {
1089 1090 1091 1092 1093 1094
        size = metafile->mtSize * 2;
        if (buf)
        {
            if(size > buf_size) size = buf_size;
            memmove( buf, metafile, size );
        }
1095
    }
1096 1097 1098 1099 1100
    else SetLastError( ERROR_INVALID_HANDLE );
    LeaveCriticalSection( &metafile_cs );

    TRACE( "returning size %d\n", size );
    return size;
Alexandre Julliard's avatar
Alexandre Julliard committed
1101 1102
}

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
/******************************************************************
 *         add_mf_comment
 *
 * Helper for GetWinMetaFileBits
 *
 * Add the MFCOMMENT record[s] which is essentially a copy
 * of the original emf.
 */
static BOOL add_mf_comment(HDC hdc, HENHMETAFILE emf)
{
    DWORD size = GetEnhMetaFileBits(emf, 0, NULL), i;
    BYTE *bits, *chunk_data;
1115
    emf_in_wmf_comment *chunk = NULL;
1116 1117 1118 1119 1120 1121 1122 1123
    BOOL ret = FALSE;
    static const DWORD max_chunk_size = 0x2000;

    if(!size) return FALSE;
    chunk_data = bits = HeapAlloc(GetProcessHeap(), 0, size);
    if(!bits) return FALSE;
    if(!GetEnhMetaFileBits(emf, size, bits)) goto end;

1124
    chunk = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(emf_in_wmf_comment, emf_data[max_chunk_size]));
1125 1126
    if(!chunk) goto end;

1127 1128 1129
    chunk->comment_id = WMFC_MAGIC;
    chunk->comment_type = 0x1;
    chunk->version = 0x00010000;
1130
    chunk->checksum = 0; /* We fixup the first chunk's checksum before returning from GetWinMetaFileBits */
1131
    chunk->flags = 0;
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
    chunk->num_chunks = (size + max_chunk_size - 1) / max_chunk_size;
    chunk->chunk_size = max_chunk_size;
    chunk->remaining_size = size;
    chunk->emf_size = size;

    for(i = 0; i < chunk->num_chunks; i++)
    {
        if(i == chunk->num_chunks - 1) /* last chunk */
            chunk->chunk_size = chunk->remaining_size;

        chunk->remaining_size -= chunk->chunk_size;
1143
        memcpy(chunk->emf_data, chunk_data, chunk->chunk_size);
1144 1145
        chunk_data += chunk->chunk_size;

1146
        if(!Escape(hdc, MFCOMMENT, FIELD_OFFSET(emf_in_wmf_comment, emf_data[chunk->chunk_size]), (char*)chunk, NULL))
1147 1148 1149 1150 1151 1152 1153 1154 1155
            goto end;
    }
    ret = TRUE;
end:
    HeapFree(GetProcessHeap(), 0, chunk);
    HeapFree(GetProcessHeap(), 0, bits);
    return ret;
}

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
/*******************************************************************
 *        muldiv
 *
 * Behaves somewhat differently to MulDiv when the answer is -ve
 * and also rounds n.5 towards zero
 */
static INT muldiv(INT m1, INT m2, INT d)
{
    LONGLONG ret;

    ret = ((LONGLONG)m1 * m2 + d/2) / d; /* Always add d/2 even if ret will be -ve */

    if((LONGLONG)m1 * m2 * 2 == (2 * ret - 1) * d) /* If the answer is exactly n.5 round towards zero */
    {
        if(ret > 0) ret--;
        else ret++;
    }
    return ret;
}

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
/******************************************************************
 *         set_window
 *
 * Helper for GetWinMetaFileBits
 *
 * Add the SetWindowOrg and SetWindowExt records
 */
static BOOL set_window(HDC hdc, HENHMETAFILE emf, HDC ref_dc, INT map_mode)
{
    ENHMETAHEADER header;
    INT horz_res, vert_res, horz_size, vert_size;
    POINT pt;

    if(!GetEnhMetaFileHeader(emf, sizeof(header), &header)) return FALSE;

    horz_res = GetDeviceCaps(ref_dc, HORZRES);
    vert_res = GetDeviceCaps(ref_dc, VERTRES);
    horz_size = GetDeviceCaps(ref_dc, HORZSIZE);
    vert_size = GetDeviceCaps(ref_dc, VERTSIZE);

    switch(map_mode)
    {
    case MM_TEXT:
    case MM_ISOTROPIC:
    case MM_ANISOTROPIC:
1201 1202
        pt.y = muldiv(header.rclFrame.top, vert_res, vert_size * 100);
        pt.x = muldiv(header.rclFrame.left, horz_res, horz_size * 100);
1203 1204
        break;
    case MM_LOMETRIC:
1205 1206
        pt.y = muldiv(-header.rclFrame.top, 1, 10) + 1;
        pt.x = muldiv( header.rclFrame.left, 1, 10);
1207 1208 1209
        break;
    case MM_HIMETRIC:
        pt.y = -header.rclFrame.top + 1;
1210
        pt.x = (header.rclFrame.left >= 0) ? header.rclFrame.left : header.rclFrame.left + 1; /* See the tests */
1211 1212
        break;
    case MM_LOENGLISH:
1213 1214
        pt.y = muldiv(-header.rclFrame.top, 10, 254) + 1;
        pt.x = muldiv( header.rclFrame.left, 10, 254);
1215 1216
        break;
    case MM_HIENGLISH:
1217 1218
        pt.y = muldiv(-header.rclFrame.top, 100, 254) + 1;
        pt.x = muldiv( header.rclFrame.left, 100, 254);
1219 1220
        break;
    case MM_TWIPS:
1221 1222
        pt.y = muldiv(-header.rclFrame.top, 72 * 20, 2540) + 1;
        pt.x = muldiv( header.rclFrame.left, 72 * 20, 2540);
1223 1224 1225 1226 1227 1228 1229
        break;
    default:
        WARN("Unknown map mode %d\n", map_mode);
        return FALSE;
    }
    SetWindowOrgEx(hdc, pt.x, pt.y, NULL);

1230 1231
    pt.x = muldiv(header.rclFrame.right - header.rclFrame.left, horz_res, horz_size * 100);
    pt.y = muldiv(header.rclFrame.bottom - header.rclFrame.top, vert_res, vert_size * 100);
1232 1233 1234 1235
    SetWindowExtEx(hdc, pt.x, pt.y, NULL);
    return TRUE;
}

1236
/******************************************************************
1237
 *         GetWinMetaFileBits [GDI32.@]
1238
 */
1239 1240
UINT WINAPI GetWinMetaFileBits(HENHMETAFILE hemf,
                                UINT cbBuffer, LPBYTE lpbBuffer,
1241
                                INT map_mode, HDC hdcRef)
1242
{
1243 1244
    HDC hdcmf;
    HMETAFILE hmf;
1245
    UINT ret, full_size;
1246 1247
    RECT rc;

1248
    NtGdiGetAppClipBox(hdcRef, &rc);
1249 1250

    TRACE("(%p,%d,%p,%d,%p) rc=%s\n", hemf, cbBuffer, lpbBuffer,
1251
          map_mode, hdcRef, wine_dbgstr_rect(&rc));
1252

1253
    hdcmf = CreateMetaFileW(NULL);
1254 1255

    add_mf_comment(hdcmf, hemf);
1256 1257 1258
    SetMapMode(hdcmf, map_mode);
    if(!set_window(hdcmf, hemf, hdcRef, map_mode))
        goto error;
1259

1260
    PlayEnhMetaFile(hdcmf, hemf, &rc);
1261
    hmf = CloseMetaFile(hdcmf);
1262
    full_size = GetMetaFileBitsEx(hmf, 0, NULL);
1263 1264
    ret = GetMetaFileBitsEx(hmf, cbBuffer, lpbBuffer);
    DeleteMetaFile(hmf);
1265

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    if(ret && ret == full_size && lpbBuffer) /* fixup checksum, but only if retrieving all of the bits */
    {
        WORD checksum = 0;
        METARECORD *comment_rec = (METARECORD*)(lpbBuffer + sizeof(METAHEADER));
        UINT i;

        for(i = 0; i < full_size / 2; i++)
            checksum += ((WORD*)lpbBuffer)[i];
        comment_rec->rdParm[8] = ~checksum + 1;
    }
1276
    return ret;
1277 1278 1279 1280

error:
    DeleteMetaFile(CloseMetaFile(hdcmf));
    return 0;
1281
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1282

Alexandre Julliard's avatar
Alexandre Julliard committed
1283
/******************************************************************
1284
 *         MF_Play_MetaCreateRegion
Alexandre Julliard's avatar
Alexandre Julliard committed
1285 1286
 *
 *  Handles META_CREATEREGION for PlayMetaFileRecord().
1287
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1288
 *	The layout of the record looks something like this:
1289
 *
1290
 *	 rdParm	meaning
Alexandre Julliard's avatar
Alexandre Julliard committed
1291 1292 1293 1294 1295
 *	 0		Always 0?
 *	 1		Always 6?
 *	 2		Looks like a handle? - not constant
 *	 3		0 or 1 ??
 *	 4		Total number of bytes
1296
 *	 5		No. of separate bands = n [see below]
Alexandre Julliard's avatar
Alexandre Julliard committed
1297 1298 1299 1300 1301 1302 1303 1304
 *	 6		Largest number of x co-ords in a band
 *	 7-10		Bounding box x1 y1 x2 y2
 *	 11-...		n bands
 *
 *	 Regions are divided into bands that are uniform in the
 *	 y-direction. Each band consists of pairs of on/off x-coords and is
 *	 written as
 *		m y0 y1 x1 x2 x3 ... xm m
1305
 *	 into successive rdParm[]s.
Alexandre Julliard's avatar
Alexandre Julliard committed
1306 1307 1308 1309 1310 1311 1312
 *
 *	 This is probably just a dump of the internal RGNOBJ?
 *
 *	 HDMD - 18/12/97
 *
 */

1313
static BOOL MF_Play_MetaCreateRegion( METARECORD *mr, HRGN hrgn )
Alexandre Julliard's avatar
Alexandre Julliard committed
1314 1315 1316 1317
{
    WORD band, pair;
    WORD *start, *end;
    INT16 y0, y1;
1318
    HRGN hrgn2 = NtGdiCreateRectRgn( 0, 0, 0, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1319

1320
    for(band  = 0, start = &(mr->rdParm[11]); band < mr->rdParm[5];
Alexandre Julliard's avatar
Alexandre Julliard committed
1321 1322
 					        band++, start = end + 1) {
        if(*start / 2 != (*start + 1) / 2) {
1323
 	    WARN("Delimiter not even.\n");
1324
	    DeleteObject( hrgn2 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1325 1326 1327 1328 1329
 	    return FALSE;
        }

	end = start + *start + 3;
	if(end > (WORD *)mr + mr->rdSize) {
1330
	    WARN("End points outside record.\n");
1331
	    DeleteObject( hrgn2 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1332 1333 1334 1335
	    return FALSE;
        }

	if(*start != *end) {
1336
	    WARN("Mismatched delimiters.\n");
1337
	    DeleteObject( hrgn2 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1338 1339 1340 1341 1342 1343
	    return FALSE;
	}

	y0 = *(INT16 *)(start + 1);
	y1 = *(INT16 *)(start + 2);
	for(pair = 0; pair < *start / 2; pair++) {
1344 1345 1346
	    NtGdiSetRectRgn( hrgn2, *(INT16 *)(start + 3 + 2*pair), y0,
                             *(INT16 *)(start + 4 + 2*pair), y1 );
	    NtGdiCombineRgn( hrgn, hrgn, hrgn2, RGN_OR );
Alexandre Julliard's avatar
Alexandre Julliard committed
1347 1348
        }
    }
1349
    DeleteObject( hrgn2 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1350 1351
    return TRUE;
 }
1352

Alexandre Julliard's avatar
Alexandre Julliard committed
1353

1354 1355 1356 1357
/******************************************************************
 *         MF_Play_MetaExtTextOut
 *
 *  Handles META_EXTTEXTOUT for PlayMetaFileRecord().
Alexandre Julliard's avatar
Alexandre Julliard committed
1358
 */
1359

1360
static BOOL MF_Play_MetaExtTextOut(HDC hdc, METARECORD *mr)
1361
{
1362 1363
    INT *dx = NULL;
    int i;
1364
    SHORT *dxx;
1365
    LPSTR sot;
1366 1367
    DWORD len;
    WORD s1;
1368
    RECT rect;
1369
    BOOL isrect = mr->rdParm[3] & (ETO_OPAQUE | ETO_CLIPPED);
1370 1371 1372

    s1 = mr->rdParm[2];                              /* String length */
    len = sizeof(METARECORD) + (((s1 + 1) >> 1) * 2) + 2 * sizeof(short)
1373
        + sizeof(UINT16) + (isrect ? 4 * sizeof(SHORT) : 0);
1374 1375 1376
                                           /* rec len without dx array */

    sot = (LPSTR)&mr->rdParm[4];		      /* start_of_text */
1377
    if (isrect)
1378 1379 1380 1381 1382
    {
        rect.left   = (SHORT)mr->rdParm[4];
        rect.top    = (SHORT)mr->rdParm[5];
        rect.right  = (SHORT)mr->rdParm[6];
        rect.bottom = (SHORT)mr->rdParm[7];
1383
        sot += 4 * sizeof(SHORT);  /* there is a rectangle, so add offset */
1384
    }
1385

1386
    if (mr->rdSize == len / 2)
Austin English's avatar
Austin English committed
1387
        dxx = NULL;                      /* determine if array is present */
1388
    else
1389
        if (mr->rdSize == (len + s1 * sizeof(INT16)) / 2)
1390
        {
1391
            dxx = (SHORT *)(sot+(((s1+1)>>1)*2));
1392
            dx = HeapAlloc( GetProcessHeap(), 0, s1*sizeof(INT));
1393
            if (dx) for (i = 0; i < s1; i++) dx[i] = dxx[i];
1394
        }
1395
	else {
1396 1397
            TRACE("%s  len: %ld\n",  sot, mr->rdSize);
            WARN("Please report: ExtTextOut len=%ld slen=%d rdSize=%ld opt=%04x\n",
1398
		 len, s1, mr->rdSize, mr->rdParm[3]);
Austin English's avatar
Austin English committed
1399
	    dxx = NULL; /* shouldn't happen -- but if, we continue with NULL */
1400
	}
1401 1402 1403 1404 1405 1406
    ExtTextOutA( hdc,
                 (SHORT)mr->rdParm[1],       /* X position */
                 (SHORT)mr->rdParm[0],       /* Y position */
                 mr->rdParm[3],              /* options */
                 &rect,                      /* rectangle */
                 sot,                        /* string */
1407
                 s1, dx);                    /* length, dx array */
1408 1409
    if (dx)
    {
1410
        TRACE("%s  len: %ld  dx0: %d\n", sot, mr->rdSize, dx[0]);
1411 1412
        HeapFree( GetProcessHeap(), 0, dx );
    }
1413 1414
    return TRUE;
}