enhmetafile.c 84.2 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
/*
2 3
 * Enhanced metafile functions
 * Copyright 1998 Douglas Ridgway
4
 *           1999 Huw D M Davies
5
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21
 *
 * NOTES:
 *
22
 * The enhanced format consists of the following elements:
23
 *
24 25 26 27
 *    A header
 *    A table of handles to GDI objects
 *    An array of metafile records
 *    A private palette
28 29
 *
 *
30 31 32
 *  The standard format consists of a header and an array of metafile records.
 *
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
33

34 35 36
#include "config.h"
#include "wine/port.h"

37
#include <stdarg.h>
38
#include <stdlib.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
39
#include <string.h>
40
#include <assert.h>
41
#include "windef.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
42
#include "winbase.h"
43
#include "wingdi.h"
44
#include "winnls.h"
45
#include "winerror.h"
46
#include "gdi_private.h"
47
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
48

49
WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
50

51 52 53 54 55 56 57
typedef struct
{
    GDIOBJHDR      header;
    ENHMETAHEADER  *emh;
    BOOL           on_disk;   /* true if metafile is on disk */
} ENHMETAFILEOBJ;

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
static const struct emr_name {
    DWORD type;
    const char *name;
} emr_names[] = {
#define X(p) {p, #p}
X(EMR_HEADER),
X(EMR_POLYBEZIER),
X(EMR_POLYGON),
X(EMR_POLYLINE),
X(EMR_POLYBEZIERTO),
X(EMR_POLYLINETO),
X(EMR_POLYPOLYLINE),
X(EMR_POLYPOLYGON),
X(EMR_SETWINDOWEXTEX),
X(EMR_SETWINDOWORGEX),
X(EMR_SETVIEWPORTEXTEX),
X(EMR_SETVIEWPORTORGEX),
X(EMR_SETBRUSHORGEX),
X(EMR_EOF),
X(EMR_SETPIXELV),
X(EMR_SETMAPPERFLAGS),
X(EMR_SETMAPMODE),
X(EMR_SETBKMODE),
X(EMR_SETPOLYFILLMODE),
X(EMR_SETROP2),
X(EMR_SETSTRETCHBLTMODE),
X(EMR_SETTEXTALIGN),
X(EMR_SETCOLORADJUSTMENT),
X(EMR_SETTEXTCOLOR),
X(EMR_SETBKCOLOR),
X(EMR_OFFSETCLIPRGN),
X(EMR_MOVETOEX),
X(EMR_SETMETARGN),
X(EMR_EXCLUDECLIPRECT),
X(EMR_INTERSECTCLIPRECT),
X(EMR_SCALEVIEWPORTEXTEX),
X(EMR_SCALEWINDOWEXTEX),
X(EMR_SAVEDC),
X(EMR_RESTOREDC),
X(EMR_SETWORLDTRANSFORM),
X(EMR_MODIFYWORLDTRANSFORM),
X(EMR_SELECTOBJECT),
X(EMR_CREATEPEN),
X(EMR_CREATEBRUSHINDIRECT),
X(EMR_DELETEOBJECT),
X(EMR_ANGLEARC),
X(EMR_ELLIPSE),
X(EMR_RECTANGLE),
X(EMR_ROUNDRECT),
X(EMR_ARC),
X(EMR_CHORD),
X(EMR_PIE),
X(EMR_SELECTPALETTE),
X(EMR_CREATEPALETTE),
X(EMR_SETPALETTEENTRIES),
X(EMR_RESIZEPALETTE),
X(EMR_REALIZEPALETTE),
X(EMR_EXTFLOODFILL),
X(EMR_LINETO),
X(EMR_ARCTO),
X(EMR_POLYDRAW),
X(EMR_SETARCDIRECTION),
X(EMR_SETMITERLIMIT),
X(EMR_BEGINPATH),
X(EMR_ENDPATH),
X(EMR_CLOSEFIGURE),
X(EMR_FILLPATH),
X(EMR_STROKEANDFILLPATH),
X(EMR_STROKEPATH),
X(EMR_FLATTENPATH),
X(EMR_WIDENPATH),
X(EMR_SELECTCLIPPATH),
X(EMR_ABORTPATH),
X(EMR_GDICOMMENT),
X(EMR_FILLRGN),
X(EMR_FRAMERGN),
X(EMR_INVERTRGN),
X(EMR_PAINTRGN),
X(EMR_EXTSELECTCLIPRGN),
X(EMR_BITBLT),
X(EMR_STRETCHBLT),
X(EMR_MASKBLT),
X(EMR_PLGBLT),
X(EMR_SETDIBITSTODEVICE),
X(EMR_STRETCHDIBITS),
X(EMR_EXTCREATEFONTINDIRECTW),
X(EMR_EXTTEXTOUTA),
X(EMR_EXTTEXTOUTW),
X(EMR_POLYBEZIER16),
X(EMR_POLYGON16),
X(EMR_POLYLINE16),
X(EMR_POLYBEZIERTO16),
X(EMR_POLYLINETO16),
X(EMR_POLYPOLYLINE16),
X(EMR_POLYPOLYGON16),
X(EMR_POLYDRAW16),
X(EMR_CREATEMONOBRUSH),
X(EMR_CREATEDIBPATTERNBRUSHPT),
X(EMR_EXTCREATEPEN),
X(EMR_POLYTEXTOUTA),
X(EMR_POLYTEXTOUTW),
X(EMR_SETICMMODE),
X(EMR_CREATECOLORSPACE),
X(EMR_SETCOLORSPACE),
X(EMR_DELETECOLORSPACE),
X(EMR_GLSRECORD),
X(EMR_GLSBOUNDEDRECORD),
X(EMR_PIXELFORMAT),
X(EMR_DRAWESCAPE),
X(EMR_EXTESCAPE),
X(EMR_STARTDOC),
X(EMR_SMALLTEXTOUT),
X(EMR_FORCEUFIMAPPING),
X(EMR_NAMEDESCAPE),
X(EMR_COLORCORRECTPALETTE),
X(EMR_SETICMPROFILEA),
X(EMR_SETICMPROFILEW),
X(EMR_ALPHABLEND),
X(EMR_SETLAYOUT),
X(EMR_TRANSPARENTBLT),
X(EMR_RESERVED_117),
X(EMR_GRADIENTFILL),
X(EMR_SETLINKEDUFI),
X(EMR_SETTEXTJUSTIFICATION),
X(EMR_COLORMATCHTOTARGETW),
X(EMR_CREATECOLORSPACEW)
#undef X
};

/****************************************************************************
 *         get_emr_name
 */
static const char *get_emr_name(DWORD type)
{
192
    unsigned int i;
193 194
    for(i = 0; i < sizeof(emr_names) / sizeof(emr_names[0]); i++)
        if(type == emr_names[i].type) return emr_names[i].name;
195
    TRACE("Unknown record type %d\n", type);
196 197
   return NULL;
}
198

199 200 201 202 203 204 205 206 207 208 209 210
/***********************************************************************
 *          is_dib_monochrome
 *
 * Returns whether a DIB can be converted to a monochrome DDB.
 *
 * A DIB can be converted if its color table contains only black and
 * white. Black must be the first color in the color table.
 *
 * Note : If the first color in the color table is white followed by
 *        black, we can't convert it to a monochrome DDB with
 *        SetDIBits, because black and white would be inverted.
 */
211
static inline BOOL is_dib_monochrome( const BITMAPINFO* info )
212 213 214 215 216
{
    if (info->bmiHeader.biBitCount != 1) return FALSE;

    if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
    {
217
        const RGBTRIPLE *rgb = ((const BITMAPCOREINFO *) info)->bmciColors;
218

219 220 221 222 223 224 225 226 227 228 229 230
        /* Check if the first color is black */
        if ((rgb->rgbtRed == 0) && (rgb->rgbtGreen == 0) && (rgb->rgbtBlue == 0))
        {
            rgb++;
            /* Check if the second color is white */
            return ((rgb->rgbtRed == 0xff) && (rgb->rgbtGreen == 0xff)
                 && (rgb->rgbtBlue == 0xff));
        }
        else return FALSE;
    }
    else  /* assume BITMAPINFOHEADER */
    {
231
        const RGBQUAD *rgb = info->bmiColors;
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

        /* Check if the first color is black */
        if ((rgb->rgbRed == 0) && (rgb->rgbGreen == 0) &&
            (rgb->rgbBlue == 0) && (rgb->rgbReserved == 0))
        {
            rgb++;

            /* Check if the second color is white */
            return ((rgb->rgbRed == 0xff) && (rgb->rgbGreen == 0xff)
                 && (rgb->rgbBlue == 0xff) && (rgb->rgbReserved == 0));
        }
        else return FALSE;
    }
}

247 248 249
/****************************************************************************
 *          EMF_Create_HENHMETAFILE
 */
250
HENHMETAFILE EMF_Create_HENHMETAFILE(ENHMETAHEADER *emh, BOOL on_disk )
251
{
252
    HENHMETAFILE hmf;
253 254 255 256 257 258 259 260 261 262 263
    ENHMETAFILEOBJ *metaObj;

    if (emh->iType != EMR_HEADER || emh->dSignature != ENHMETA_SIGNATURE ||
        (emh->nBytes & 3)) /* refuse to load unaligned EMF as Windows does */
    {
        WARN("Invalid emf header type 0x%08x sig 0x%08x.\n",
             emh->iType, emh->dSignature);
        SetLastError(ERROR_INVALID_DATA);
        return 0;
    }

264 265 266 267 268 269 270
    if (!(metaObj = HeapAlloc( GetProcessHeap(), 0, sizeof(*metaObj) ))) return 0;

    metaObj->emh = emh;
    metaObj->on_disk = on_disk;

    if (!(hmf = alloc_gdi_handle( &metaObj->header, OBJ_ENHMETAFILE, NULL )))
        HeapFree( GetProcessHeap(), 0, metaObj );
271 272 273 274 275 276 277 278
    return hmf;
}

/****************************************************************************
 *          EMF_Delete_HENHMETAFILE
 */
static BOOL EMF_Delete_HENHMETAFILE( HENHMETAFILE hmf )
{
279
    ENHMETAFILEOBJ *metaObj = free_gdi_handle( hmf );
280

281
    if(!metaObj) return FALSE;
282 283

    if(metaObj->on_disk)
284
        UnmapViewOfFile( metaObj->emh );
285
    else
286
        HeapFree( GetProcessHeap(), 0, metaObj->emh );
287
    return HeapFree( GetProcessHeap(), 0, metaObj );
288 289 290 291 292 293 294 295 296
}

/******************************************************************
 *         EMF_GetEnhMetaHeader
 *
 * Returns ptr to ENHMETAHEADER associated with HENHMETAFILE
 */
static ENHMETAHEADER *EMF_GetEnhMetaHeader( HENHMETAFILE hmf )
{
297
    ENHMETAHEADER *ret = NULL;
298
    ENHMETAFILEOBJ *metaObj = GDI_GetObjPtr( hmf, OBJ_ENHMETAFILE );
299
    TRACE("hmf %p -> enhmetaObj %p\n", hmf, metaObj);
300 301 302 303 304 305
    if (metaObj)
    {
        ret = metaObj->emh;
        GDI_ReleaseObj( hmf );
    }
    return ret;
306
}
307

Alexandre Julliard's avatar
Alexandre Julliard committed
308
/*****************************************************************************
309 310 311
 *         EMF_GetEnhMetaFile
 *
 */
312
static HENHMETAFILE EMF_GetEnhMetaFile( HANDLE hFile )
313 314 315
{
    ENHMETAHEADER *emh;
    HANDLE hMapping;
316
    HENHMETAFILE hemf;
317

318 319
    hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
    emh = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
320 321 322
    CloseHandle( hMapping );

    if (!emh) return 0;
323

324 325 326 327
    hemf = EMF_Create_HENHMETAFILE( emh, TRUE );
    if (!hemf)
        UnmapViewOfFile( emh );
    return hemf;
328 329 330 331
}


/*****************************************************************************
332
 *          GetEnhMetaFileA (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
333 334 335
 *
 *
 */
336
HENHMETAFILE WINAPI GetEnhMetaFileA(
337
	     LPCSTR lpszMetaFile  /* [in] filename of enhanced metafile */
Alexandre Julliard's avatar
Alexandre Julliard committed
338 339
    )
{
340
    HENHMETAFILE hmf;
341
    HANDLE hFile;
342

343 344
    hFile = CreateFileA(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
			OPEN_EXISTING, 0, 0);
345
    if (hFile == INVALID_HANDLE_VALUE) {
346
        WARN("could not open %s\n", lpszMetaFile);
347 348 349
	return 0;
    }
    hmf = EMF_GetEnhMetaFile( hFile );
350
    CloseHandle( hFile );
351
    return hmf;
Alexandre Julliard's avatar
Alexandre Julliard committed
352 353
}

354
/*****************************************************************************
355
 *          GetEnhMetaFileW  (GDI32.@)
356
 */
357
HENHMETAFILE WINAPI GetEnhMetaFileW(
358
             LPCWSTR lpszMetaFile)  /* [in] filename of enhanced metafile */
359
{
360
    HENHMETAFILE hmf;
361
    HANDLE hFile;
362

363 364
    hFile = CreateFileW(lpszMetaFile, GENERIC_READ, FILE_SHARE_READ, 0,
			OPEN_EXISTING, 0, 0);
365
    if (hFile == INVALID_HANDLE_VALUE) {
366
        WARN("could not open %s\n", debugstr_w(lpszMetaFile));
367 368 369
	return 0;
    }
    hmf = EMF_GetEnhMetaFile( hFile );
370
    CloseHandle( hFile );
371
    return hmf;
372 373
}

Alexandre Julliard's avatar
Alexandre Julliard committed
374
/*****************************************************************************
375
 *        GetEnhMetaFileHeader  (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
376
 *
377 378 379 380
 * Retrieves the record containing the header for the specified
 * enhanced-format metafile.
 *
 * RETURNS
381
 *  If buf is NULL, returns the size of buffer required.
382
 *  Otherwise, copy up to bufsize bytes of enhanced metafile header into
383
 *  buf.
Alexandre Julliard's avatar
Alexandre Julliard committed
384
 */
385
UINT WINAPI GetEnhMetaFileHeader(
386 387
       HENHMETAFILE hmf,   /* [in] enhanced metafile */
       UINT bufsize,       /* [in] size of buffer */
388
       LPENHMETAHEADER buf /* [out] buffer */
Alexandre Julliard's avatar
Alexandre Julliard committed
389 390
    )
{
391
    LPENHMETAHEADER emh;
392
    UINT size;
393 394

    emh = EMF_GetEnhMetaHeader(hmf);
395
    if(!emh) return FALSE;
396
    size = emh->nSize;
397
    if (!buf) return size;
398 399 400
    size = min(size, bufsize);
    memmove(buf, emh, size);
    return size;
Alexandre Julliard's avatar
Alexandre Julliard committed
401 402 403 404
}


/*****************************************************************************
405
 *          GetEnhMetaFileDescriptionA  (GDI32.@)
406 407
 *
 * See GetEnhMetaFileDescriptionW.
Alexandre Julliard's avatar
Alexandre Julliard committed
408
 */
409
UINT WINAPI GetEnhMetaFileDescriptionA(
410
       HENHMETAFILE hmf, /* [in] enhanced metafile */
411
       UINT size,        /* [in] size of buf */
412
       LPSTR buf         /* [out] buffer to receive description */
Alexandre Julliard's avatar
Alexandre Julliard committed
413 414
    )
{
415
     LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
416 417 418
     DWORD len;
     WCHAR *descrW;

419
     if(!emh) return FALSE;
420
     if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
421 422 423
     descrW = (WCHAR *) ((char *) emh + emh->offDescription);
     len = WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, NULL, 0, NULL, NULL );

424
     if (!buf || !size ) return len;
425 426 427 428

     len = min( size, len );
     WideCharToMultiByte( CP_ACP, 0, descrW, emh->nDescription, buf, len, NULL, NULL );
     return len;
Alexandre Julliard's avatar
Alexandre Julliard committed
429 430 431
}

/*****************************************************************************
432
 *          GetEnhMetaFileDescriptionW  (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
433
 *
434
 *  Copies the description string of an enhanced metafile into a buffer
Alexandre Julliard's avatar
Alexandre Julliard committed
435 436
 *  _buf_.
 *
437
 * RETURNS
Alexandre Julliard's avatar
Alexandre Julliard committed
438 439
 *  If _buf_ is NULL, returns size of _buf_ required. Otherwise, returns
 *  number of characters copied.
Alexandre Julliard's avatar
Alexandre Julliard committed
440
 */
441
UINT WINAPI GetEnhMetaFileDescriptionW(
442
       HENHMETAFILE hmf, /* [in] enhanced metafile */
443
       UINT size,        /* [in] size of buf */
444
       LPWSTR buf        /* [out] buffer to receive description */
Alexandre Julliard's avatar
Alexandre Julliard committed
445 446
    )
{
447
     LPENHMETAHEADER emh = EMF_GetEnhMetaHeader(hmf);
448 449

     if(!emh) return FALSE;
450 451 452 453
     if(emh->nDescription == 0 || emh->offDescription == 0) return 0;
     if (!buf || !size ) return emh->nDescription;

     memmove(buf, (char *) emh + emh->offDescription, min(size,emh->nDescription)*sizeof(WCHAR));
454
     return min(size, emh->nDescription);
Alexandre Julliard's avatar
Alexandre Julliard committed
455 456
}

Alexandre Julliard's avatar
Alexandre Julliard committed
457
/****************************************************************************
458
 *    SetEnhMetaFileBits (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
459 460 461
 *
 *  Creates an enhanced metafile by copying _bufsize_ bytes from _buf_.
 */
462
HENHMETAFILE WINAPI SetEnhMetaFileBits(UINT bufsize, const BYTE *buf)
Alexandre Julliard's avatar
Alexandre Julliard committed
463
{
464
    ENHMETAHEADER *emh = HeapAlloc( GetProcessHeap(), 0, bufsize );
465
    memmove(emh, buf, bufsize);
466
    return EMF_Create_HENHMETAFILE( emh, FALSE );
Alexandre Julliard's avatar
Alexandre Julliard committed
467 468 469
}

/*****************************************************************************
470
 *  GetEnhMetaFileBits (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
471 472
 *
 */
473
UINT WINAPI GetEnhMetaFileBits(
474 475 476 477
    HENHMETAFILE hmf,
    UINT bufsize,
    LPBYTE buf
)
478
{
479 480
    LPENHMETAHEADER emh = EMF_GetEnhMetaHeader( hmf );
    UINT size;
481

482
    if(!emh) return 0;
483

484
    size = emh->nBytes;
485
    if( buf == NULL ) return size;
486

487 488 489
    size = min( size, bufsize );
    memmove(buf, emh, size);
    return size;
Alexandre Julliard's avatar
Alexandre Julliard committed
490 491
}

492
typedef struct EMF_dc_state
493 494 495 496 497 498 499 500 501 502 503
{
    INT   mode;
    XFORM world_transform;
    INT   wndOrgX;
    INT   wndOrgY;
    INT   wndExtX;
    INT   wndExtY;
    INT   vportOrgX;
    INT   vportOrgY;
    INT   vportExtX;
    INT   vportExtY;
504 505 506 507 508 509 510 511 512
    struct EMF_dc_state *next;
} EMF_dc_state;

typedef struct enum_emh_data
{
    XFORM init_transform;
    EMF_dc_state state;
    INT save_level;
    EMF_dc_state *saved_state;
513 514 515 516 517 518 519 520 521 522
} enum_emh_data;

#define ENUM_GET_PRIVATE_DATA(ht) \
    ((enum_emh_data*)(((unsigned char*)(ht))-sizeof (enum_emh_data)))

#define WIDTH(rect) ( (rect).right - (rect).left )
#define HEIGHT(rect) ( (rect).bottom - (rect).top )

#define IS_WIN9X() (GetVersion()&0x80000000)

523
static void EMF_Update_MF_Xform(HDC hdc, const enum_emh_data *info)
524 525
{
    XFORM mapping_mode_trans, final_trans;
526
    double scaleX, scaleY;
527

528 529
    scaleX = (double)info->state.vportExtX / (double)info->state.wndExtX;
    scaleY = (double)info->state.vportExtY / (double)info->state.wndExtY;
530 531 532 533
    mapping_mode_trans.eM11 = scaleX;
    mapping_mode_trans.eM12 = 0.0;
    mapping_mode_trans.eM21 = 0.0;
    mapping_mode_trans.eM22 = scaleY;
534 535
    mapping_mode_trans.eDx  = (double)info->state.vportOrgX - scaleX * (double)info->state.wndOrgX;
    mapping_mode_trans.eDy  = (double)info->state.vportOrgY - scaleY * (double)info->state.wndOrgY;
536

537
    CombineTransform(&final_trans, &info->state.world_transform, &mapping_mode_trans);
538 539 540 541 542 543 544 545
    CombineTransform(&final_trans, &final_trans, &info->init_transform);
 
    if (!SetWorldTransform(hdc, &final_trans))
    {
        ERR("World transform failed!\n");
    }
}

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
static void EMF_RestoreDC( enum_emh_data *info, INT level )
{
    if (abs(level) > info->save_level || level == 0) return;

    if (level < 0) level = info->save_level + level + 1;

    while (info->save_level >= level)
    {
        EMF_dc_state *state = info->saved_state;
        info->saved_state = state->next;
        state->next = NULL;
        if (--info->save_level < level)
            info->state = *state;
        HeapFree( GetProcessHeap(), 0, state );
    }
}

static void EMF_SaveDC( enum_emh_data *info )
{
    EMF_dc_state *state = HeapAlloc( GetProcessHeap(), 0, sizeof(*state));
    if (state)
    {
        *state = info->state;
        state->next = info->saved_state;
        info->saved_state = state;
        info->save_level++;
        TRACE("save_level %d\n", info->save_level);
    }
}

576
static void EMF_SetMapMode(HDC hdc, enum_emh_data *info)
577 578 579 580 581 582
{
    INT horzSize = GetDeviceCaps( hdc, HORZSIZE );
    INT vertSize = GetDeviceCaps( hdc, VERTSIZE );
    INT horzRes  = GetDeviceCaps( hdc, HORZRES );
    INT vertRes  = GetDeviceCaps( hdc, VERTRES );

583
    TRACE("%d\n", info->state.mode);
584

585
    switch(info->state.mode)
586 587
    {
    case MM_TEXT:
588 589 590 591
        info->state.wndExtX   = 1;
        info->state.wndExtY   = 1;
        info->state.vportExtX = 1;
        info->state.vportExtY = 1;
592 593 594
        break;
    case MM_LOMETRIC:
    case MM_ISOTROPIC:
595 596 597 598
        info->state.wndExtX   = horzSize * 10;
        info->state.wndExtY   = vertSize * 10;
        info->state.vportExtX = horzRes;
        info->state.vportExtY = -vertRes;
599 600
        break;
    case MM_HIMETRIC:
601 602 603 604
        info->state.wndExtX   = horzSize * 100;
        info->state.wndExtY   = vertSize * 100;
        info->state.vportExtX = horzRes;
        info->state.vportExtY = -vertRes;
605 606
        break;
    case MM_LOENGLISH:
607 608 609 610
        info->state.wndExtX   = MulDiv(1000, horzSize, 254);
        info->state.wndExtY   = MulDiv(1000, vertSize, 254);
        info->state.vportExtX = horzRes;
        info->state.vportExtY = -vertRes;
611 612
        break;
    case MM_HIENGLISH:
613 614 615 616
        info->state.wndExtX   = MulDiv(10000, horzSize, 254);
        info->state.wndExtY   = MulDiv(10000, vertSize, 254);
        info->state.vportExtX = horzRes;
        info->state.vportExtY = -vertRes;
617 618
        break;
    case MM_TWIPS:
619 620 621 622
        info->state.wndExtX   = MulDiv(14400, horzSize, 254);
        info->state.wndExtY   = MulDiv(14400, vertSize, 254);
        info->state.vportExtX = horzRes;
        info->state.vportExtY = -vertRes;
623 624 625 626 627 628 629 630
        break;
    case MM_ANISOTROPIC:
        break;
    default:
        return;
    }
}

631 632 633 634 635 636 637 638
/***********************************************************************
 *           EMF_FixIsotropic
 *
 * Fix viewport extensions for isotropic mode.
 */

static void EMF_FixIsotropic(HDC hdc, enum_emh_data *info)
{
639 640 641 642
    double xdim = fabs((double)info->state.vportExtX * GetDeviceCaps( hdc, HORZSIZE ) /
                  (GetDeviceCaps( hdc, HORZRES ) * info->state.wndExtX));
    double ydim = fabs((double)info->state.vportExtY * GetDeviceCaps( hdc, VERTSIZE ) /
                  (GetDeviceCaps( hdc, VERTRES ) * info->state.wndExtY));
643 644 645

    if (xdim > ydim)
    {
646 647 648
        INT mincx = (info->state.vportExtX >= 0) ? 1 : -1;
        info->state.vportExtX = floor(info->state.vportExtX * ydim / xdim + 0.5);
        if (!info->state.vportExtX) info->state.vportExtX = mincx;
649 650 651
    }
    else
    {
652 653 654
        INT mincy = (info->state.vportExtY >= 0) ? 1 : -1;
        info->state.vportExtY = floor(info->state.vportExtY * xdim / ydim + 0.5);
        if (!info->state.vportExtY) info->state.vportExtY = mincy;
655 656 657
    }
}

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
/*****************************************************************************
 *       emr_produces_output
 *
 * Returns TRUE if the record type writes something to the dc.  Used by
 * PlayEnhMetaFileRecord to determine whether it needs to update the
 * dc's xform when in win9x mode.
 *
 * FIXME: need to test which records should be here.
 */
static BOOL emr_produces_output(int type)
{
    switch(type) {
    case EMR_POLYBEZIER:
    case EMR_POLYGON:
    case EMR_POLYLINE:
    case EMR_POLYBEZIERTO:
    case EMR_POLYLINETO:
    case EMR_POLYPOLYLINE:
    case EMR_POLYPOLYGON:
    case EMR_SETPIXELV:
    case EMR_MOVETOEX:
    case EMR_EXCLUDECLIPRECT:
    case EMR_INTERSECTCLIPRECT:
    case EMR_SELECTOBJECT:
    case EMR_ANGLEARC:
    case EMR_ELLIPSE:
    case EMR_RECTANGLE:
    case EMR_ROUNDRECT:
    case EMR_ARC:
    case EMR_CHORD:
    case EMR_PIE:
    case EMR_EXTFLOODFILL:
    case EMR_LINETO:
    case EMR_ARCTO:
    case EMR_POLYDRAW:
693
    case EMR_GDICOMMENT:
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    case EMR_FILLRGN:
    case EMR_FRAMERGN:
    case EMR_INVERTRGN:
    case EMR_PAINTRGN:
    case EMR_BITBLT:
    case EMR_STRETCHBLT:
    case EMR_MASKBLT:
    case EMR_PLGBLT:
    case EMR_SETDIBITSTODEVICE:
    case EMR_STRETCHDIBITS:
    case EMR_EXTTEXTOUTA:
    case EMR_EXTTEXTOUTW:
    case EMR_POLYBEZIER16:
    case EMR_POLYGON16:
    case EMR_POLYLINE16:
    case EMR_POLYBEZIERTO16:
    case EMR_POLYLINETO16:
    case EMR_POLYPOLYLINE16:
    case EMR_POLYPOLYGON16:
    case EMR_POLYDRAW16:
    case EMR_POLYTEXTOUTA:
    case EMR_POLYTEXTOUTW:
    case EMR_SMALLTEXTOUT:
717
    case EMR_ALPHABLEND:
718 719 720 721 722 723 724 725
    case EMR_TRANSPARENTBLT:
        return TRUE;
    default:
        return FALSE;
    }
}


Alexandre Julliard's avatar
Alexandre Julliard committed
726
/*****************************************************************************
727
 *           PlayEnhMetaFileRecord  (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
728 729 730 731
 *
 *  Render a single enhanced metafile record in the device context hdc.
 *
 *  RETURNS
732
 *    TRUE (non zero) on success, FALSE on error.
Alexandre Julliard's avatar
Alexandre Julliard committed
733
 *  BUGS
Alexandre Julliard's avatar
Alexandre Julliard committed
734
 *    Many unimplemented records.
735
 *    No error handling on record play failures (ie checking return codes)
736 737 738 739
 *
 * NOTES
 *    WinNT actually updates the current world transform in this function
 *     whereas Win9x does not.
Alexandre Julliard's avatar
Alexandre Julliard committed
740
 */
741
BOOL WINAPI PlayEnhMetaFileRecord(
742 743 744 745
     HDC hdc,                   /* [in] device context in which to render EMF record */
     LPHANDLETABLE handletable, /* [in] array of handles to be used in rendering record */
     const ENHMETARECORD *mr,   /* [in] EMF record to render */
     UINT handles               /* [in] size of handle array */
746
     )
Alexandre Julliard's avatar
Alexandre Julliard committed
747 748
{
  int type;
749 750
  RECT tmprc;
  enum_emh_data *info = ENUM_GET_PRIVATE_DATA(handletable);
751

752 753
  TRACE("hdc = %p, handletable = %p, record = %p, numHandles = %d\n",
        hdc, handletable, mr, handles);
Alexandre Julliard's avatar
Alexandre Julliard committed
754
  if (!mr) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
755

Alexandre Julliard's avatar
Alexandre Julliard committed
756
  type = mr->iType;
Alexandre Julliard's avatar
Alexandre Julliard committed
757

758
  TRACE("record %s\n", get_emr_name(type));
759
  switch(type)
Alexandre Julliard's avatar
Alexandre Julliard committed
760 761
    {
    case EMR_HEADER:
762
      break;
Alexandre Julliard's avatar
Alexandre Julliard committed
763
    case EMR_EOF:
Alexandre Julliard's avatar
Alexandre Julliard committed
764 765
      break;
    case EMR_GDICOMMENT:
766
      {
767
        const EMRGDICOMMENT *lpGdiComment = (const EMRGDICOMMENT *)mr;
768 769 770
        /* In an enhanced metafile, there can be both public and private GDI comments */
        GdiComment( hdc, lpGdiComment->cbData, lpGdiComment->Data );
        break;
771
      }
Alexandre Julliard's avatar
Alexandre Julliard committed
772 773
    case EMR_SETMAPMODE:
      {
774
        const EMRSETMAPMODE *pSetMapMode = (const EMRSETMAPMODE *)mr;
775

776 777
        if (info->state.mode == pSetMapMode->iMode &&
            (info->state.mode == MM_ISOTROPIC || info->state.mode == MM_ANISOTROPIC))
778
            break;
779
        info->state.mode = pSetMapMode->iMode;
780
        EMF_SetMapMode(hdc, info);
Alexandre Julliard's avatar
Alexandre Julliard committed
781 782 783 784
	break;
      }
    case EMR_SETBKMODE:
      {
785
        const EMRSETBKMODE *pSetBkMode = (const EMRSETBKMODE *)mr;
786
	SetBkMode(hdc, pSetBkMode->iMode);
Alexandre Julliard's avatar
Alexandre Julliard committed
787 788 789 790
	break;
      }
    case EMR_SETBKCOLOR:
      {
791
        const EMRSETBKCOLOR *pSetBkColor = (const EMRSETBKCOLOR *)mr;
792
	SetBkColor(hdc, pSetBkColor->crColor);
Alexandre Julliard's avatar
Alexandre Julliard committed
793 794 795 796
	break;
      }
    case EMR_SETPOLYFILLMODE:
      {
797
        const EMRSETPOLYFILLMODE *pSetPolyFillMode = (const EMRSETPOLYFILLMODE *)mr;
798
	SetPolyFillMode(hdc, pSetPolyFillMode->iMode);
Alexandre Julliard's avatar
Alexandre Julliard committed
799 800 801 802
	break;
      }
    case EMR_SETROP2:
      {
803
        const EMRSETROP2 *pSetROP2 = (const EMRSETROP2 *)mr;
804
	SetROP2(hdc, pSetROP2->iMode);
Alexandre Julliard's avatar
Alexandre Julliard committed
805 806 807 808
	break;
      }
    case EMR_SETSTRETCHBLTMODE:
      {
809
	const EMRSETSTRETCHBLTMODE *pSetStretchBltMode = (const EMRSETSTRETCHBLTMODE *)mr;
810
	SetStretchBltMode(hdc, pSetStretchBltMode->iMode);
Alexandre Julliard's avatar
Alexandre Julliard committed
811 812 813 814
	break;
      }
    case EMR_SETTEXTALIGN:
      {
815
	const EMRSETTEXTALIGN *pSetTextAlign = (const EMRSETTEXTALIGN *)mr;
816
	SetTextAlign(hdc, pSetTextAlign->iMode);
Alexandre Julliard's avatar
Alexandre Julliard committed
817 818 819 820
	break;
      }
    case EMR_SETTEXTCOLOR:
      {
821
	const EMRSETTEXTCOLOR *pSetTextColor = (const EMRSETTEXTCOLOR *)mr;
822
	SetTextColor(hdc, pSetTextColor->crColor);
Alexandre Julliard's avatar
Alexandre Julliard committed
823 824 825 826
	break;
      }
    case EMR_SAVEDC:
      {
827 828
        if (SaveDC( hdc ))
            EMF_SaveDC( info );
Alexandre Julliard's avatar
Alexandre Julliard committed
829 830 831 832
	break;
      }
    case EMR_RESTOREDC:
      {
833
	const EMRRESTOREDC *pRestoreDC = (const EMRRESTOREDC *)mr;
834
        TRACE("EMR_RESTORE: %d\n", pRestoreDC->iRelative);
835 836
        if (RestoreDC( hdc, pRestoreDC->iRelative ))
            EMF_RestoreDC( info, pRestoreDC->iRelative );
Alexandre Julliard's avatar
Alexandre Julliard committed
837 838 839 840
	break;
      }
    case EMR_INTERSECTCLIPRECT:
      {
841
	const EMRINTERSECTCLIPRECT *pClipRect = (const EMRINTERSECTCLIPRECT *)mr;
842
        TRACE("EMR_INTERSECTCLIPRECT: rect %d,%d - %d, %d\n",
843 844 845 846
              pClipRect->rclClip.left, pClipRect->rclClip.top,
              pClipRect->rclClip.right, pClipRect->rclClip.bottom);
        IntersectClipRect(hdc, pClipRect->rclClip.left, pClipRect->rclClip.top,
                          pClipRect->rclClip.right, pClipRect->rclClip.bottom);
Alexandre Julliard's avatar
Alexandre Julliard committed
847 848 849 850
	break;
      }
    case EMR_SELECTOBJECT:
      {
851
	const EMRSELECTOBJECT *pSelectObject = (const EMRSELECTOBJECT *)mr;
852 853 854 855 856 857 858 859 860 861 862 863 864
	if( pSelectObject->ihObject & 0x80000000 ) {
	  /* High order bit is set - it's a stock object
	   * Strip the high bit to get the index.
	   * See MSDN article Q142319
	   */
	  SelectObject( hdc, GetStockObject( pSelectObject->ihObject &
					     0x7fffffff ) );
	} else {
	  /* High order bit wasn't set - not a stock object
	   */
	      SelectObject( hdc,
			(handletable->objectHandle)[pSelectObject->ihObject] );
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
865 866 867 868
	break;
      }
    case EMR_DELETEOBJECT:
      {
869
	const EMRDELETEOBJECT *pDeleteObject = (const EMRDELETEOBJECT *)mr;
870 871
	DeleteObject( (handletable->objectHandle)[pDeleteObject->ihObject]);
	(handletable->objectHandle)[pDeleteObject->ihObject] = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
872 873 874 875
	break;
      }
    case EMR_SETWINDOWORGEX:
      {
876
    	const EMRSETWINDOWORGEX *pSetWindowOrgEx = (const EMRSETWINDOWORGEX *)mr;
877

878 879
        info->state.wndOrgX = pSetWindowOrgEx->ptlOrigin.x;
        info->state.wndOrgY = pSetWindowOrgEx->ptlOrigin.y;
880

881
        TRACE("SetWindowOrgEx: %d,%d\n", info->state.wndOrgX, info->state.wndOrgY);
882
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
883 884 885
      }
    case EMR_SETWINDOWEXTEX:
      {
886
	const EMRSETWINDOWEXTEX *pSetWindowExtEx = (const EMRSETWINDOWEXTEX *)mr;
887
	
888
        if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
889
	    break;
890 891 892
        info->state.wndExtX = pSetWindowExtEx->szlExtent.cx;
        info->state.wndExtY = pSetWindowExtEx->szlExtent.cy;
        if (info->state.mode == MM_ISOTROPIC)
893
            EMF_FixIsotropic(hdc, info);
894

895
        TRACE("SetWindowExtEx: %d,%d\n",info->state.wndExtX, info->state.wndExtY);
Alexandre Julliard's avatar
Alexandre Julliard committed
896 897 898 899
	break;
      }
    case EMR_SETVIEWPORTORGEX:
      {
900
	const EMRSETVIEWPORTORGEX *pSetViewportOrgEx = (const EMRSETVIEWPORTORGEX *)mr;
901

902 903 904
        info->state.vportOrgX = pSetViewportOrgEx->ptlOrigin.x;
        info->state.vportOrgY = pSetViewportOrgEx->ptlOrigin.y;
        TRACE("SetViewportOrgEx: %d,%d\n", info->state.vportOrgX, info->state.vportOrgY);
Alexandre Julliard's avatar
Alexandre Julliard committed
905 906 907 908
	break;
      }
    case EMR_SETVIEWPORTEXTEX:
      {
909
	const EMRSETVIEWPORTEXTEX *pSetViewportExtEx = (const EMRSETVIEWPORTEXTEX *)mr;
910

911
        if (info->state.mode != MM_ISOTROPIC && info->state.mode != MM_ANISOTROPIC)
912
	    break;
913 914 915
        info->state.vportExtX = pSetViewportExtEx->szlExtent.cx;
        info->state.vportExtY = pSetViewportExtEx->szlExtent.cy;
        if (info->state.mode == MM_ISOTROPIC)
916
            EMF_FixIsotropic(hdc, info);
917
        TRACE("SetViewportExtEx: %d,%d\n", info->state.vportExtX, info->state.vportExtY);
Alexandre Julliard's avatar
Alexandre Julliard committed
918 919 920 921
	break;
      }
    case EMR_CREATEPEN:
      {
922
	const EMRCREATEPEN *pCreatePen = (const EMRCREATEPEN *)mr;
923
	(handletable->objectHandle)[pCreatePen->ihPen] =
924
	  CreatePenIndirect(&pCreatePen->lopn);
Alexandre Julliard's avatar
Alexandre Julliard committed
925 926 927 928
	break;
      }
    case EMR_EXTCREATEPEN:
      {
929
	const EMREXTCREATEPEN *pPen = (const EMREXTCREATEPEN *)mr;
930 931 932 933 934 935 936 937
	LOGBRUSH lb;
	lb.lbStyle = pPen->elp.elpBrushStyle;
	lb.lbColor = pPen->elp.elpColor;
	lb.lbHatch = pPen->elp.elpHatch;

	if(pPen->offBmi || pPen->offBits)
	  FIXME("EMR_EXTCREATEPEN: Need to copy brush bitmap\n");

938 939
	(handletable->objectHandle)[pPen->ihPen] =
	  ExtCreatePen(pPen->elp.elpPenStyle, pPen->elp.elpWidth, &lb,
940
		       pPen->elp.elpNumEntries, pPen->elp.elpStyleEntry);
Alexandre Julliard's avatar
Alexandre Julliard committed
941 942 943 944
	break;
      }
    case EMR_CREATEBRUSHINDIRECT:
      {
945
	const EMRCREATEBRUSHINDIRECT *pBrush = (const EMRCREATEBRUSHINDIRECT *)mr;
946 947 948 949 950
        LOGBRUSH brush;
        brush.lbStyle = pBrush->lb.lbStyle;
        brush.lbColor = pBrush->lb.lbColor;
        brush.lbHatch = pBrush->lb.lbHatch;
        (handletable->objectHandle)[pBrush->ihBrush] = CreateBrushIndirect(&brush);
Alexandre Julliard's avatar
Alexandre Julliard committed
951 952 953
	break;
      }
    case EMR_EXTCREATEFONTINDIRECTW:
954
      {
955
	const EMREXTCREATEFONTINDIRECTW *pFont = (const EMREXTCREATEFONTINDIRECTW *)mr;
956
	(handletable->objectHandle)[pFont->ihFont] =
957
	  CreateFontIndirectW(&pFont->elfw.elfLogFont);
Alexandre Julliard's avatar
Alexandre Julliard committed
958
	break;
959
      }
Alexandre Julliard's avatar
Alexandre Julliard committed
960 961
    case EMR_MOVETOEX:
      {
962
	const EMRMOVETOEX *pMoveToEx = (const EMRMOVETOEX *)mr;
963
	MoveToEx(hdc, pMoveToEx->ptl.x, pMoveToEx->ptl.y, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
964 965 966 967
	break;
      }
    case EMR_LINETO:
      {
968
	const EMRLINETO *pLineTo = (const EMRLINETO *)mr;
969
        LineTo(hdc, pLineTo->ptl.x, pLineTo->ptl.y);
Alexandre Julliard's avatar
Alexandre Julliard committed
970 971 972 973
	break;
      }
    case EMR_RECTANGLE:
      {
974
	const EMRRECTANGLE *pRect = (const EMRRECTANGLE *)mr;
975 976
	Rectangle(hdc, pRect->rclBox.left, pRect->rclBox.top,
		  pRect->rclBox.right, pRect->rclBox.bottom);
Alexandre Julliard's avatar
Alexandre Julliard committed
977 978 979 980
	break;
      }
    case EMR_ELLIPSE:
      {
981
	const EMRELLIPSE *pEllipse = (const EMRELLIPSE *)mr;
982 983
	Ellipse(hdc, pEllipse->rclBox.left, pEllipse->rclBox.top,
		pEllipse->rclBox.right, pEllipse->rclBox.bottom);
Alexandre Julliard's avatar
Alexandre Julliard committed
984 985 986 987
	break;
      }
    case EMR_POLYGON16:
      {
988
	const EMRPOLYGON16 *pPoly = (const EMRPOLYGON16 *)mr;
989 990 991 992 993
	/* Shouldn't use Polygon16 since pPoly->cpts is DWORD */
	POINT *pts = HeapAlloc( GetProcessHeap(), 0,
				pPoly->cpts * sizeof(POINT) );
	DWORD i;
	for(i = 0; i < pPoly->cpts; i++)
994 995 996 997
	{
	    pts[i].x = pPoly->apts[i].x;
	    pts[i].y = pPoly->apts[i].y;
	}
998 999
	Polygon(hdc, pts, pPoly->cpts);
	HeapFree( GetProcessHeap(), 0, pts );
Alexandre Julliard's avatar
Alexandre Julliard committed
1000 1001
	break;
      }
Alexandre Julliard's avatar
Alexandre Julliard committed
1002 1003
    case EMR_POLYLINE16:
      {
1004
	const EMRPOLYLINE16 *pPoly = (const EMRPOLYLINE16 *)mr;
1005 1006 1007 1008 1009
	/* Shouldn't use Polyline16 since pPoly->cpts is DWORD */
	POINT *pts = HeapAlloc( GetProcessHeap(), 0,
				pPoly->cpts * sizeof(POINT) );
	DWORD i;
	for(i = 0; i < pPoly->cpts; i++)
1010 1011 1012 1013
	{
	    pts[i].x = pPoly->apts[i].x;
	    pts[i].y = pPoly->apts[i].y;
	}
1014 1015
	Polyline(hdc, pts, pPoly->cpts);
	HeapFree( GetProcessHeap(), 0, pts );
Alexandre Julliard's avatar
Alexandre Julliard committed
1016 1017
	break;
      }
1018 1019
    case EMR_POLYLINETO16:
      {
1020
	const EMRPOLYLINETO16 *pPoly = (const EMRPOLYLINETO16 *)mr;
1021 1022 1023 1024 1025
	/* Shouldn't use PolylineTo16 since pPoly->cpts is DWORD */
	POINT *pts = HeapAlloc( GetProcessHeap(), 0,
				pPoly->cpts * sizeof(POINT) );
	DWORD i;
	for(i = 0; i < pPoly->cpts; i++)
1026 1027 1028 1029
	{
	    pts[i].x = pPoly->apts[i].x;
	    pts[i].y = pPoly->apts[i].y;
	}
1030 1031 1032 1033 1034 1035
	PolylineTo(hdc, pts, pPoly->cpts);
	HeapFree( GetProcessHeap(), 0, pts );
	break;
      }
    case EMR_POLYBEZIER16:
      {
1036
	const EMRPOLYBEZIER16 *pPoly = (const EMRPOLYBEZIER16 *)mr;
1037 1038 1039 1040 1041
	/* Shouldn't use PolyBezier16 since pPoly->cpts is DWORD */
	POINT *pts = HeapAlloc( GetProcessHeap(), 0,
				pPoly->cpts * sizeof(POINT) );
	DWORD i;
	for(i = 0; i < pPoly->cpts; i++)
1042 1043 1044 1045
	{
	    pts[i].x = pPoly->apts[i].x;
	    pts[i].y = pPoly->apts[i].y;
	}
1046 1047 1048 1049 1050 1051
	PolyBezier(hdc, pts, pPoly->cpts);
	HeapFree( GetProcessHeap(), 0, pts );
	break;
      }
    case EMR_POLYBEZIERTO16:
      {
1052
	const EMRPOLYBEZIERTO16 *pPoly = (const EMRPOLYBEZIERTO16 *)mr;
1053 1054 1055 1056 1057
	/* Shouldn't use PolyBezierTo16 since pPoly->cpts is DWORD */
	POINT *pts = HeapAlloc( GetProcessHeap(), 0,
				pPoly->cpts * sizeof(POINT) );
	DWORD i;
	for(i = 0; i < pPoly->cpts; i++)
1058 1059 1060 1061
	{
	    pts[i].x = pPoly->apts[i].x;
	    pts[i].y = pPoly->apts[i].y;
	}
1062 1063 1064 1065
	PolyBezierTo(hdc, pts, pPoly->cpts);
	HeapFree( GetProcessHeap(), 0, pts );
	break;
      }
Alexandre Julliard's avatar
Alexandre Julliard committed
1066 1067
    case EMR_POLYPOLYGON16:
      {
1068
        const EMRPOLYPOLYGON16 *pPolyPoly = (const EMRPOLYPOLYGON16 *)mr;
1069 1070
	/* NB POINTS array doesn't start at pPolyPoly->apts it's actually
	   pPolyPoly->aPolyCounts + pPolyPoly->nPolys */
1071

1072 1073
        POINT16 *pts16 = (POINT16 *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
        POINT *pts = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1074 1075
	DWORD i;
	for(i = 0; i < pPolyPoly->cpts; i++)
1076 1077 1078 1079
        {
            pts[i].x = pts16[i].x;
            pts[i].y = pts16[i].y;
        }
1080 1081
	PolyPolygon(hdc, pts, (INT*)pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
	HeapFree( GetProcessHeap(), 0, pts );
Alexandre Julliard's avatar
Alexandre Julliard committed
1082 1083
	break;
      }
1084 1085
    case EMR_POLYPOLYLINE16:
      {
1086
        const EMRPOLYPOLYLINE16 *pPolyPoly = (const EMRPOLYPOLYLINE16 *)mr;
1087 1088 1089
	/* NB POINTS array doesn't start at pPolyPoly->apts it's actually
	   pPolyPoly->aPolyCounts + pPolyPoly->nPolys */

1090 1091
        POINT16 *pts16 = (POINT16 *)(pPolyPoly->aPolyCounts + pPolyPoly->nPolys);
        POINT *pts = HeapAlloc( GetProcessHeap(), 0, pPolyPoly->cpts * sizeof(POINT) );
1092 1093
	DWORD i;
	for(i = 0; i < pPolyPoly->cpts; i++)
1094 1095 1096 1097
        {
            pts[i].x = pts16[i].x;
            pts[i].y = pts16[i].y;
        }
1098 1099 1100 1101
	PolyPolyline(hdc, pts, pPolyPoly->aPolyCounts, pPolyPoly->nPolys);
	HeapFree( GetProcessHeap(), 0, pts );
	break;
      }
1102

1103 1104
    case EMR_STRETCHDIBITS:
      {
1105
	const EMRSTRETCHDIBITS *pStretchDIBits = (const EMRSTRETCHDIBITS *)mr;
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115

	StretchDIBits(hdc,
		      pStretchDIBits->xDest,
		      pStretchDIBits->yDest,
		      pStretchDIBits->cxDest,
		      pStretchDIBits->cyDest,
		      pStretchDIBits->xSrc,
		      pStretchDIBits->ySrc,
		      pStretchDIBits->cxSrc,
		      pStretchDIBits->cySrc,
1116 1117
		      (const BYTE *)mr + pStretchDIBits->offBitsSrc,
		      (const BITMAPINFO *)((const BYTE *)mr + pStretchDIBits->offBmiSrc),
1118 1119
		      pStretchDIBits->iUsageSrc,
		      pStretchDIBits->dwRop);
1120
	break;
1121
      }
1122 1123 1124

    case EMR_EXTTEXTOUTA:
    {
1125
	const EMREXTTEXTOUTA *pExtTextOutA = (const EMREXTTEXTOUTA *)mr;
1126
	RECT rc;
1127
        const INT *dx = NULL;
1128 1129 1130 1131 1132

	rc.left = pExtTextOutA->emrtext.rcl.left;
	rc.top = pExtTextOutA->emrtext.rcl.top;
	rc.right = pExtTextOutA->emrtext.rcl.right;
	rc.bottom = pExtTextOutA->emrtext.rcl.bottom;
1133
        TRACE("EMR_EXTTEXTOUTA: x,y = %d, %d. rect = %d, %d - %d, %d. flags %08x\n",
1134 1135 1136 1137 1138 1139 1140 1141
              pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
              rc.left, rc.top, rc.right, rc.bottom, pExtTextOutA->emrtext.fOptions);

        /* Linux version of pstoedit produces EMFs with offDx set to 0.
         * These files can be enumerated and played under Win98 just
         * fine, but at least Win2k chokes on them.
         */
        if (pExtTextOutA->emrtext.offDx)
1142
            dx = (const INT *)((const BYTE *)mr + pExtTextOutA->emrtext.offDx);
1143

1144 1145
	ExtTextOutA(hdc, pExtTextOutA->emrtext.ptlReference.x, pExtTextOutA->emrtext.ptlReference.y,
	    pExtTextOutA->emrtext.fOptions, &rc,
1146
	    (LPCSTR)((const BYTE *)mr + pExtTextOutA->emrtext.offString), pExtTextOutA->emrtext.nChars,
1147
	    dx);
1148 1149 1150
	break;
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
1151
    case EMR_EXTTEXTOUTW:
1152
    {
1153
	const EMREXTTEXTOUTW *pExtTextOutW = (const EMREXTTEXTOUTW *)mr;
1154
	RECT rc;
1155
        const INT *dx = NULL;
1156 1157 1158 1159 1160

	rc.left = pExtTextOutW->emrtext.rcl.left;
	rc.top = pExtTextOutW->emrtext.rcl.top;
	rc.right = pExtTextOutW->emrtext.rcl.right;
	rc.bottom = pExtTextOutW->emrtext.rcl.bottom;
1161
        TRACE("EMR_EXTTEXTOUTW: x,y = %d, %d.  rect = %d, %d - %d, %d. flags %08x\n",
1162 1163 1164
              pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
              rc.left, rc.top, rc.right, rc.bottom, pExtTextOutW->emrtext.fOptions);

1165 1166 1167 1168 1169
        /* Linux version of pstoedit produces EMFs with offDx set to 0.
         * These files can be enumerated and played under Win98 just
         * fine, but at least Win2k chokes on them.
         */
        if (pExtTextOutW->emrtext.offDx)
1170
            dx = (const INT *)((const BYTE *)mr + pExtTextOutW->emrtext.offDx);
1171

1172 1173
	ExtTextOutW(hdc, pExtTextOutW->emrtext.ptlReference.x, pExtTextOutW->emrtext.ptlReference.y,
	    pExtTextOutW->emrtext.fOptions, &rc,
1174
	    (LPCWSTR)((const BYTE *)mr + pExtTextOutW->emrtext.offString), pExtTextOutW->emrtext.nChars,
1175
	    dx);
Alexandre Julliard's avatar
Alexandre Julliard committed
1176
	break;
1177 1178
    }

1179 1180
    case EMR_CREATEPALETTE:
      {
1181
	const EMRCREATEPALETTE *lpCreatePal = (const EMRCREATEPALETTE *)mr;
1182

1183
	(handletable->objectHandle)[ lpCreatePal->ihPal ] =
1184 1185 1186 1187 1188 1189 1190
		CreatePalette( &lpCreatePal->lgpl );

	break;
      }

    case EMR_SELECTPALETTE:
      {
1191
	const EMRSELECTPALETTE *lpSelectPal = (const EMRSELECTPALETTE *)mr;
1192

1193 1194 1195 1196 1197
	if( lpSelectPal->ihPal & 0x80000000 ) {
		SelectPalette( hdc, GetStockObject(lpSelectPal->ihPal & 0x7fffffff), TRUE);
	} else {
		SelectPalette( hdc, (handletable->objectHandle)[lpSelectPal->ihPal], TRUE);
	}
1198 1199
	break;
      }
Alexandre Julliard's avatar
Alexandre Julliard committed
1200

1201 1202 1203 1204 1205 1206 1207 1208
    case EMR_REALIZEPALETTE:
      {
	RealizePalette( hdc );
	break;
      }

    case EMR_EXTSELECTCLIPRGN:
      {
1209 1210 1211 1212 1213
	const EMREXTSELECTCLIPRGN *lpRgn = (const EMREXTSELECTCLIPRGN *)mr;
	HRGN hRgn = 0;

        if (mr->nSize >= sizeof(*lpRgn) + sizeof(RGNDATAHEADER))
            hRgn = ExtCreateRegion( &info->init_transform, 0, (RGNDATA *)lpRgn->RgnData );
1214

1215 1216 1217
	ExtSelectClipRgn(hdc, hRgn, (INT)(lpRgn->iMode));
	/* ExtSelectClipRgn created a copy of the region */
	DeleteObject(hRgn);
1218
        break;
1219
      }
1220

1221 1222 1223 1224 1225
    case EMR_SETMETARGN:
      {
        SetMetaRgn( hdc );
        break;
      }
1226

1227 1228
    case EMR_SETWORLDTRANSFORM:
      {
1229
        const EMRSETWORLDTRANSFORM *lpXfrm = (const EMRSETWORLDTRANSFORM *)mr;
1230
        info->state.world_transform = lpXfrm->xform;
1231 1232
        break;
      }
1233

1234
    case EMR_POLYBEZIER:
1235
      {
1236
        const EMRPOLYBEZIER *lpPolyBez = (const EMRPOLYBEZIER *)mr;
1237
        PolyBezier(hdc, (const POINT*)lpPolyBez->aptl, (UINT)lpPolyBez->cptl);
1238 1239 1240
        break;
      }

1241
    case EMR_POLYGON:
1242
      {
1243
        const EMRPOLYGON *lpPoly = (const EMRPOLYGON *)mr;
1244
        Polygon( hdc, (const POINT*)lpPoly->aptl, (UINT)lpPoly->cptl );
1245 1246 1247
        break;
      }

1248
    case EMR_POLYLINE:
1249
      {
1250
        const EMRPOLYLINE *lpPolyLine = (const EMRPOLYLINE *)mr;
1251
        Polyline(hdc, (const POINT*)lpPolyLine->aptl, (UINT)lpPolyLine->cptl);
1252
        break;
1253 1254
      }

1255
    case EMR_POLYBEZIERTO:
1256
      {
1257
        const EMRPOLYBEZIERTO *lpPolyBezierTo = (const EMRPOLYBEZIERTO *)mr;
1258
        PolyBezierTo( hdc, (const POINT*)lpPolyBezierTo->aptl,
1259
		      (UINT)lpPolyBezierTo->cptl );
1260
        break;
1261 1262
      }

1263
    case EMR_POLYLINETO:
1264
      {
1265
        const EMRPOLYLINETO *lpPolyLineTo = (const EMRPOLYLINETO *)mr;
1266
        PolylineTo( hdc, (const POINT*)lpPolyLineTo->aptl,
1267
		    (UINT)lpPolyLineTo->cptl );
1268 1269 1270
        break;
      }

1271
    case EMR_POLYPOLYLINE:
1272
      {
1273
        const EMRPOLYPOLYLINE *pPolyPolyline = (const EMRPOLYPOLYLINE *)mr;
1274
	/* NB Points at pPolyPolyline->aPolyCounts + pPolyPolyline->nPolys */
1275

1276
        PolyPolyline(hdc, (LPPOINT)(pPolyPolyline->aPolyCounts +
1277 1278 1279
				    pPolyPolyline->nPolys),
		     pPolyPolyline->aPolyCounts,
		     pPolyPolyline->nPolys );
1280 1281 1282 1283

        break;
      }

1284
    case EMR_POLYPOLYGON:
1285
      {
1286
        const EMRPOLYPOLYGON *pPolyPolygon = (const EMRPOLYPOLYGON *)mr;
1287

1288
	/* NB Points at pPolyPolygon->aPolyCounts + pPolyPolygon->nPolys */
1289

1290 1291 1292
        PolyPolygon(hdc, (LPPOINT)(pPolyPolygon->aPolyCounts +
				   pPolyPolygon->nPolys),
		    (INT*)pPolyPolygon->aPolyCounts, pPolyPolygon->nPolys );
1293 1294 1295
        break;
      }

1296
    case EMR_SETBRUSHORGEX:
1297
      {
1298
        const EMRSETBRUSHORGEX *lpSetBrushOrgEx = (const EMRSETBRUSHORGEX *)mr;
1299

1300 1301 1302
        SetBrushOrgEx( hdc,
                       (INT)lpSetBrushOrgEx->ptlOrigin.x,
                       (INT)lpSetBrushOrgEx->ptlOrigin.y,
1303 1304 1305 1306
                       NULL );

        break;
      }
1307

1308
    case EMR_SETPIXELV:
1309
      {
1310
        const EMRSETPIXELV *lpSetPixelV = (const EMRSETPIXELV *)mr;
1311

1312 1313 1314
        SetPixelV( hdc,
                   (INT)lpSetPixelV->ptlPixel.x,
                   (INT)lpSetPixelV->ptlPixel.y,
1315 1316 1317 1318 1319
                   lpSetPixelV->crColor );

        break;
      }

1320
    case EMR_SETMAPPERFLAGS:
1321
      {
1322
        const EMRSETMAPPERFLAGS *lpSetMapperFlags = (const EMRSETMAPPERFLAGS *)mr;
1323

1324 1325 1326 1327 1328
        SetMapperFlags( hdc, lpSetMapperFlags->dwFlags );

        break;
      }

1329
    case EMR_SETCOLORADJUSTMENT:
1330
      {
1331
        const EMRSETCOLORADJUSTMENT *lpSetColorAdjust = (const EMRSETCOLORADJUSTMENT *)mr;
1332 1333 1334 1335 1336 1337

        SetColorAdjustment( hdc, &lpSetColorAdjust->ColorAdjustment );

        break;
      }

1338
    case EMR_OFFSETCLIPRGN:
1339
      {
1340
        const EMROFFSETCLIPRGN *lpOffsetClipRgn = (const EMROFFSETCLIPRGN *)mr;
1341

1342
        OffsetClipRgn( hdc,
1343 1344
                       (INT)lpOffsetClipRgn->ptlOffset.x,
                       (INT)lpOffsetClipRgn->ptlOffset.y );
1345
        FIXME("OffsetClipRgn\n");
1346 1347

        break;
1348
      }
1349

1350
    case EMR_EXCLUDECLIPRECT:
1351
      {
1352
        const EMREXCLUDECLIPRECT *lpExcludeClipRect = (const EMREXCLUDECLIPRECT *)mr;
1353

1354 1355 1356 1357
        ExcludeClipRect( hdc,
                         lpExcludeClipRect->rclClip.left,
                         lpExcludeClipRect->rclClip.top,
                         lpExcludeClipRect->rclClip.right,
1358
                         lpExcludeClipRect->rclClip.bottom  );
1359
        FIXME("ExcludeClipRect\n");
1360 1361 1362 1363

         break;
      }

1364
    case EMR_SCALEVIEWPORTEXTEX:
1365
      {
1366
        const EMRSCALEVIEWPORTEXTEX *lpScaleViewportExtEx = (const EMRSCALEVIEWPORTEXTEX *)mr;
1367

1368
        if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1369 1370
	    break;
        if (!lpScaleViewportExtEx->xNum || !lpScaleViewportExtEx->xDenom || 
1371
            !lpScaleViewportExtEx->yNum || !lpScaleViewportExtEx->yDenom)
1372
            break;
1373
        info->state.vportExtX = MulDiv(info->state.vportExtX, lpScaleViewportExtEx->xNum,
1374
                                 lpScaleViewportExtEx->xDenom);
1375
        info->state.vportExtY = MulDiv(info->state.vportExtY, lpScaleViewportExtEx->yNum,
1376
                                 lpScaleViewportExtEx->yDenom);
1377 1378 1379
        if (info->state.vportExtX == 0) info->state.vportExtX = 1;
        if (info->state.vportExtY == 0) info->state.vportExtY = 1;
        if (info->state.mode == MM_ISOTROPIC)
1380
            EMF_FixIsotropic(hdc, info);
1381

1382
        TRACE("EMRSCALEVIEWPORTEXTEX %d/%d %d/%d\n",
1383 1384
             lpScaleViewportExtEx->xNum,lpScaleViewportExtEx->xDenom,
             lpScaleViewportExtEx->yNum,lpScaleViewportExtEx->yDenom);
1385

1386 1387
        break;
      }
1388

1389
    case EMR_SCALEWINDOWEXTEX:
1390
      {
1391
        const EMRSCALEWINDOWEXTEX *lpScaleWindowExtEx = (const EMRSCALEWINDOWEXTEX *)mr;
1392

1393
        if ((info->state.mode != MM_ISOTROPIC) && (info->state.mode != MM_ANISOTROPIC))
1394 1395 1396 1397
	    break;
        if (!lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->xDenom || 
            !lpScaleWindowExtEx->xNum || !lpScaleWindowExtEx->yDenom)
            break;
1398
        info->state.wndExtX = MulDiv(info->state.wndExtX, lpScaleWindowExtEx->xNum,
1399
                               lpScaleWindowExtEx->xDenom);
1400
        info->state.wndExtY = MulDiv(info->state.wndExtY, lpScaleWindowExtEx->yNum,
1401
                               lpScaleWindowExtEx->yDenom);
1402 1403 1404
        if (info->state.wndExtX == 0) info->state.wndExtX = 1;
        if (info->state.wndExtY == 0) info->state.wndExtY = 1;
        if (info->state.mode == MM_ISOTROPIC)
1405
            EMF_FixIsotropic(hdc, info);
1406

1407
        TRACE("EMRSCALEWINDOWEXTEX %d/%d %d/%d\n",
1408 1409
             lpScaleWindowExtEx->xNum,lpScaleWindowExtEx->xDenom,
             lpScaleWindowExtEx->yNum,lpScaleWindowExtEx->yDenom);
1410 1411 1412 1413

        break;
      }

1414
    case EMR_MODIFYWORLDTRANSFORM:
1415
      {
1416
        const EMRMODIFYWORLDTRANSFORM *lpModifyWorldTrans = (const EMRMODIFYWORLDTRANSFORM *)mr;
1417

1418 1419
        switch(lpModifyWorldTrans->iMode) {
        case MWT_IDENTITY:
1420 1421 1422
            info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
            info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
            info->state.world_transform.eDx  = info->state.world_transform.eDy  = 0;
1423 1424
            break;
        case MWT_LEFTMULTIPLY:
1425 1426
            CombineTransform(&info->state.world_transform, &lpModifyWorldTrans->xform,
                             &info->state.world_transform);
1427 1428
            break;
        case MWT_RIGHTMULTIPLY:
1429
            CombineTransform(&info->state.world_transform, &info->state.world_transform,
1430 1431 1432
                             &lpModifyWorldTrans->xform);
            break;
        default:
1433
            FIXME("Unknown imode %d\n", lpModifyWorldTrans->iMode);
1434 1435
            break;
        }
1436 1437 1438
        break;
      }

1439
    case EMR_ANGLEARC:
1440
      {
1441
        const EMRANGLEARC *lpAngleArc = (const EMRANGLEARC *)mr;
1442

1443
        AngleArc( hdc,
1444
                 (INT)lpAngleArc->ptlCenter.x, (INT)lpAngleArc->ptlCenter.y,
1445
                 lpAngleArc->nRadius, lpAngleArc->eStartAngle,
1446 1447 1448 1449
                 lpAngleArc->eSweepAngle );

        break;
      }
1450 1451

    case EMR_ROUNDRECT:
1452
      {
1453
        const EMRROUNDRECT *lpRoundRect = (const EMRROUNDRECT *)mr;
1454

1455
        RoundRect( hdc,
1456 1457 1458 1459 1460 1461 1462
                   lpRoundRect->rclBox.left,
                   lpRoundRect->rclBox.top,
                   lpRoundRect->rclBox.right,
                   lpRoundRect->rclBox.bottom,
                   lpRoundRect->szlCorner.cx,
                   lpRoundRect->szlCorner.cy );

1463
        break;
1464 1465
      }

1466
    case EMR_ARC:
1467
      {
1468
        const EMRARC *lpArc = (const EMRARC *)mr;
1469

1470
        Arc( hdc,
1471 1472 1473 1474
             (INT)lpArc->rclBox.left,
             (INT)lpArc->rclBox.top,
             (INT)lpArc->rclBox.right,
             (INT)lpArc->rclBox.bottom,
1475
             (INT)lpArc->ptlStart.x,
1476 1477 1478 1479
             (INT)lpArc->ptlStart.y,
             (INT)lpArc->ptlEnd.x,
             (INT)lpArc->ptlEnd.y );

1480
        break;
1481 1482
      }

1483
    case EMR_CHORD:
1484
      {
1485
        const EMRCHORD *lpChord = (const EMRCHORD *)mr;
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499

        Chord( hdc,
             (INT)lpChord->rclBox.left,
             (INT)lpChord->rclBox.top,
             (INT)lpChord->rclBox.right,
             (INT)lpChord->rclBox.bottom,
             (INT)lpChord->ptlStart.x,
             (INT)lpChord->ptlStart.y,
             (INT)lpChord->ptlEnd.x,
             (INT)lpChord->ptlEnd.y );

        break;
      }

1500
    case EMR_PIE:
1501
      {
1502
        const EMRPIE *lpPie = (const EMRPIE *)mr;
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516

        Pie( hdc,
             (INT)lpPie->rclBox.left,
             (INT)lpPie->rclBox.top,
             (INT)lpPie->rclBox.right,
             (INT)lpPie->rclBox.bottom,
             (INT)lpPie->ptlStart.x,
             (INT)lpPie->ptlStart.y,
             (INT)lpPie->ptlEnd.x,
             (INT)lpPie->ptlEnd.y );

       break;
      }

1517
    case EMR_ARCTO:
1518
      {
1519
        const EMRARC *lpArcTo = (const EMRARC *)mr;
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535

        ArcTo( hdc,
               (INT)lpArcTo->rclBox.left,
               (INT)lpArcTo->rclBox.top,
               (INT)lpArcTo->rclBox.right,
               (INT)lpArcTo->rclBox.bottom,
               (INT)lpArcTo->ptlStart.x,
               (INT)lpArcTo->ptlStart.y,
               (INT)lpArcTo->ptlEnd.x,
               (INT)lpArcTo->ptlEnd.y );

        break;
      }

    case EMR_EXTFLOODFILL:
      {
1536
        const EMREXTFLOODFILL *lpExtFloodFill = (const EMREXTFLOODFILL *)mr;
1537

1538
        ExtFloodFill( hdc,
1539 1540 1541 1542 1543 1544 1545 1546
                      (INT)lpExtFloodFill->ptlStart.x,
                      (INT)lpExtFloodFill->ptlStart.y,
                      lpExtFloodFill->crColor,
                      (UINT)lpExtFloodFill->iMode );

        break;
      }

1547
    case EMR_POLYDRAW:
1548
      {
1549
        const EMRPOLYDRAW *lpPolyDraw = (const EMRPOLYDRAW *)mr;
1550
        PolyDraw( hdc,
1551
                  (const POINT*)lpPolyDraw->aptl,
1552 1553
                  lpPolyDraw->abTypes,
                  (INT)lpPolyDraw->cptl );
1554

1555
        break;
1556
      }
1557

1558
    case EMR_SETARCDIRECTION:
1559
      {
1560
        const EMRSETARCDIRECTION *lpSetArcDirection = (const EMRSETARCDIRECTION *)mr;
1561 1562 1563 1564
        SetArcDirection( hdc, (INT)lpSetArcDirection->iArcDirection );
        break;
      }

1565
    case EMR_SETMITERLIMIT:
1566
      {
1567
        const EMRSETMITERLIMIT *lpSetMiterLimit = (const EMRSETMITERLIMIT *)mr;
1568 1569
        SetMiterLimit( hdc, lpSetMiterLimit->eMiterLimit, NULL );
        break;
1570
      }
1571

1572
    case EMR_BEGINPATH:
1573 1574 1575 1576 1577
      {
        BeginPath( hdc );
        break;
      }

1578
    case EMR_ENDPATH:
1579 1580 1581 1582 1583
      {
        EndPath( hdc );
        break;
      }

1584
    case EMR_CLOSEFIGURE:
1585 1586 1587 1588 1589
      {
        CloseFigure( hdc );
        break;
      }

1590
    case EMR_FILLPATH:
1591
      {
1592
        /*const EMRFILLPATH lpFillPath = (const EMRFILLPATH *)mr;*/
1593 1594 1595 1596
        FillPath( hdc );
        break;
      }

1597
    case EMR_STROKEANDFILLPATH:
1598
      {
1599
        /*const EMRSTROKEANDFILLPATH lpStrokeAndFillPath = (const EMRSTROKEANDFILLPATH *)mr;*/
1600 1601 1602 1603
        StrokeAndFillPath( hdc );
        break;
      }

1604
    case EMR_STROKEPATH:
1605
      {
1606
        /*const EMRSTROKEPATH lpStrokePath = (const EMRSTROKEPATH *)mr;*/
1607 1608 1609 1610
        StrokePath( hdc );
        break;
      }

1611
    case EMR_FLATTENPATH:
1612
      {
1613
        FlattenPath( hdc );
1614 1615 1616
        break;
      }

1617
    case EMR_WIDENPATH:
1618 1619 1620 1621 1622
      {
        WidenPath( hdc );
        break;
      }

1623
    case EMR_SELECTCLIPPATH:
1624
      {
1625
        const EMRSELECTCLIPPATH *lpSelectClipPath = (const EMRSELECTCLIPPATH *)mr;
1626 1627 1628
        SelectClipPath( hdc, (INT)lpSelectClipPath->iMode );
        break;
      }
1629

1630
    case EMR_ABORTPATH:
1631 1632 1633 1634 1635
      {
        AbortPath( hdc );
        break;
      }

1636 1637 1638
    case EMR_CREATECOLORSPACE:
      {
        PEMRCREATECOLORSPACE lpCreateColorSpace = (PEMRCREATECOLORSPACE)mr;
1639 1640
        (handletable->objectHandle)[lpCreateColorSpace->ihCS] =
           CreateColorSpaceA( &lpCreateColorSpace->lcs );
1641 1642 1643 1644 1645
        break;
      }

    case EMR_SETCOLORSPACE:
      {
1646
        const EMRSETCOLORSPACE *lpSetColorSpace = (const EMRSETCOLORSPACE *)mr;
1647
        SetColorSpace( hdc,
1648 1649 1650 1651 1652 1653
                       (handletable->objectHandle)[lpSetColorSpace->ihCS] );
        break;
      }

    case EMR_DELETECOLORSPACE:
      {
1654
        const EMRDELETECOLORSPACE *lpDeleteColorSpace = (const EMRDELETECOLORSPACE *)mr;
1655
        DeleteColorSpace( (handletable->objectHandle)[lpDeleteColorSpace->ihCS] );
1656
        break;
1657 1658 1659 1660
      }

    case EMR_SETICMMODE:
      {
1661
        const EMRSETICMMODE *lpSetICMMode = (const EMRSETICMMODE *)mr;
1662
        SetICMMode( hdc, (INT)lpSetICMMode->iMode );
1663 1664 1665
        break;
      }

1666
    case EMR_PIXELFORMAT:
1667 1668
      {
        INT iPixelFormat;
1669
        const EMRPIXELFORMAT *lpPixelFormat = (const EMRPIXELFORMAT *)mr;
1670 1671

        iPixelFormat = ChoosePixelFormat( hdc, &lpPixelFormat->pfd );
1672 1673
        SetPixelFormat( hdc, iPixelFormat, &lpPixelFormat->pfd );

1674 1675 1676
        break;
      }

1677
    case EMR_SETPALETTEENTRIES:
1678
      {
1679
        const EMRSETPALETTEENTRIES *lpSetPaletteEntries = (const EMRSETPALETTEENTRIES *)mr;
1680 1681 1682 1683

        SetPaletteEntries( (handletable->objectHandle)[lpSetPaletteEntries->ihPal],
                           (UINT)lpSetPaletteEntries->iStart,
                           (UINT)lpSetPaletteEntries->cEntries,
1684 1685
                           lpSetPaletteEntries->aPalEntries );

1686 1687 1688 1689 1690
        break;
      }

    case EMR_RESIZEPALETTE:
      {
1691
        const EMRRESIZEPALETTE *lpResizePalette = (const EMRRESIZEPALETTE *)mr;
1692 1693 1694 1695 1696 1697 1698 1699 1700

        ResizePalette( (handletable->objectHandle)[lpResizePalette->ihPal],
                       (UINT)lpResizePalette->cEntries );

        break;
      }

    case EMR_CREATEDIBPATTERNBRUSHPT:
      {
1701
        const EMRCREATEDIBPATTERNBRUSHPT *lpCreate = (const EMRCREATEDIBPATTERNBRUSHPT *)mr;
1702 1703
        LPVOID lpPackedStruct;

1704
        /* Check that offsets and data are contained within the record
Austin English's avatar
Austin English committed
1705
         * (including checking for wrap-arounds).
1706 1707 1708 1709 1710
         */
        if (    lpCreate->offBmi  + lpCreate->cbBmi  > mr->nSize
             || lpCreate->offBits + lpCreate->cbBits > mr->nSize
             || lpCreate->offBmi  + lpCreate->cbBmi  < lpCreate->offBmi
             || lpCreate->offBits + lpCreate->cbBits < lpCreate->offBits )
1711 1712 1713 1714
        {
            ERR("Invalid EMR_CREATEDIBPATTERNBRUSHPT record\n");
            break;
        }
1715 1716

        /* This is a BITMAPINFO struct followed directly by bitmap bits */
1717 1718 1719 1720
        lpPackedStruct = HeapAlloc( GetProcessHeap(), 0,
                                    lpCreate->cbBmi + lpCreate->cbBits );
        if(!lpPackedStruct)
        {
1721
            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1722 1723 1724
            break;
        }

1725
        /* Now pack this structure */
1726
        memcpy( lpPackedStruct,
1727
                ((const BYTE *)lpCreate) + lpCreate->offBmi,
1728
                lpCreate->cbBmi );
1729
        memcpy( ((BYTE*)lpPackedStruct) + lpCreate->cbBmi,
1730
                ((const BYTE *)lpCreate) + lpCreate->offBits,
1731 1732
                lpCreate->cbBits );

1733
        (handletable->objectHandle)[lpCreate->ihBrush] =
1734
           CreateDIBPatternBrushPt( lpPackedStruct,
1735
                                    (UINT)lpCreate->iUsage );
1736

1737
        HeapFree(GetProcessHeap(), 0, lpPackedStruct);
1738
        break;
1739
      }
1740

1741 1742
    case EMR_CREATEMONOBRUSH:
    {
1743
        const EMRCREATEMONOBRUSH *pCreateMonoBrush = (const EMRCREATEMONOBRUSH *)mr;
1744
        const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pCreateMonoBrush->offBmi);
1745 1746 1747 1748
        HBITMAP hBmp;

        /* Need to check if the bitmap is monochrome, and if the
           two colors are really black and white */
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
        if (pCreateMonoBrush->iUsage == DIB_PAL_MONO)
        {
            BITMAP bm;

            /* Undocumented iUsage indicates a mono bitmap with no palette table,
             * aligned to 32 rather than 16 bits.
             */
            bm.bmType = 0;
            bm.bmWidth = pbi->bmiHeader.biWidth;
            bm.bmHeight = abs(pbi->bmiHeader.biHeight);
            bm.bmWidthBytes = 4 * ((pbi->bmiHeader.biWidth + 31) / 32);
            bm.bmPlanes = pbi->bmiHeader.biPlanes;
            bm.bmBitsPixel = pbi->bmiHeader.biBitCount;
            bm.bmBits = (BYTE *)mr + pCreateMonoBrush->offBits;
            hBmp = CreateBitmapIndirect(&bm);
        }
        else if (is_dib_monochrome(pbi))
1766 1767 1768 1769
        {
          /* Top-down DIBs have a negative height */
          LONG height = pbi->bmiHeader.biHeight;

1770
          hBmp = CreateBitmap(pbi->bmiHeader.biWidth, abs(height), 1, 1, NULL);
1771
          SetDIBits(hdc, hBmp, 0, pbi->bmiHeader.biHeight,
1772
              (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1773
        }
1774 1775
        else
        {
1776 1777
            hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
              (const BYTE *)mr + pCreateMonoBrush->offBits, pbi, pCreateMonoBrush->iUsage);
1778 1779
        }

1780
	(handletable->objectHandle)[pCreateMonoBrush->ihBrush] = CreatePatternBrush(hBmp);
1781

1782 1783 1784 1785 1786
	/* CreatePatternBrush created a copy of the bitmap */
	DeleteObject(hBmp);
	break;
    }

1787
    case EMR_BITBLT:
1788
    {
1789
	const EMRBITBLT *pBitBlt = (const EMRBITBLT *)mr;
1790

1791 1792 1793 1794 1795 1796 1797
        if(pBitBlt->offBmiSrc == 0) { /* Record is a PatBlt */
            PatBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
                   pBitBlt->dwRop);
        } else { /* BitBlt */
            HDC hdcSrc = CreateCompatibleDC(hdc);
            HBRUSH hBrush, hBrushOld;
            HBITMAP hBmp = 0, hBmpOld = 0;
1798
            const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pBitBlt->offBmiSrc);
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809

            SetWorldTransform(hdcSrc, &pBitBlt->xformSrc);

            hBrush = CreateSolidBrush(pBitBlt->crBkColorSrc);
            hBrushOld = SelectObject(hdcSrc, hBrush);
            PatBlt(hdcSrc, pBitBlt->rclBounds.left, pBitBlt->rclBounds.top,
                   pBitBlt->rclBounds.right - pBitBlt->rclBounds.left,
                   pBitBlt->rclBounds.bottom - pBitBlt->rclBounds.top, PATCOPY);
            SelectObject(hdcSrc, hBrushOld);
            DeleteObject(hBrush);

1810 1811
            hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
                                  (const BYTE *)mr + pBitBlt->offBitsSrc, pbi, pBitBlt->iUsageSrc);
1812 1813 1814 1815 1816 1817 1818 1819 1820
            hBmpOld = SelectObject(hdcSrc, hBmp);

            BitBlt(hdc, pBitBlt->xDest, pBitBlt->yDest, pBitBlt->cxDest, pBitBlt->cyDest,
                   hdcSrc, pBitBlt->xSrc, pBitBlt->ySrc, pBitBlt->dwRop);

            SelectObject(hdcSrc, hBmpOld);
            DeleteObject(hBmp);
            DeleteDC(hdcSrc);
        }
1821 1822 1823
	break;
    }

1824
    case EMR_STRETCHBLT:
1825
    {
1826
	const EMRSTRETCHBLT *pStretchBlt = (const EMRSTRETCHBLT *)mr;
1827

1828
        TRACE("EMR_STRETCHBLT: %d, %d %dx%d -> %d, %d %dx%d. rop %08x offBitsSrc %d\n",
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
	       pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
	       pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
	       pStretchBlt->dwRop, pStretchBlt->offBitsSrc);

        if(pStretchBlt->offBmiSrc == 0) { /* Record is a PatBlt */
            PatBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
                   pStretchBlt->dwRop);
        } else { /* StretchBlt */
            HDC hdcSrc = CreateCompatibleDC(hdc);
            HBRUSH hBrush, hBrushOld;
            HBITMAP hBmp = 0, hBmpOld = 0;
1840
            const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pStretchBlt->offBmiSrc);
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851

            SetWorldTransform(hdcSrc, &pStretchBlt->xformSrc);

            hBrush = CreateSolidBrush(pStretchBlt->crBkColorSrc);
            hBrushOld = SelectObject(hdcSrc, hBrush);
            PatBlt(hdcSrc, pStretchBlt->rclBounds.left, pStretchBlt->rclBounds.top,
                   pStretchBlt->rclBounds.right - pStretchBlt->rclBounds.left,
                   pStretchBlt->rclBounds.bottom - pStretchBlt->rclBounds.top, PATCOPY);
            SelectObject(hdcSrc, hBrushOld);
            DeleteObject(hBrush);

1852 1853
            hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
                                  (const BYTE *)mr + pStretchBlt->offBitsSrc, pbi, pStretchBlt->iUsageSrc);
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
            hBmpOld = SelectObject(hdcSrc, hBmp);

            StretchBlt(hdc, pStretchBlt->xDest, pStretchBlt->yDest, pStretchBlt->cxDest, pStretchBlt->cyDest,
                       hdcSrc, pStretchBlt->xSrc, pStretchBlt->ySrc, pStretchBlt->cxSrc, pStretchBlt->cySrc,
                       pStretchBlt->dwRop);

            SelectObject(hdcSrc, hBmpOld);
            DeleteObject(hBmp);
            DeleteDC(hdcSrc);
        }
1864 1865 1866
	break;
    }

1867 1868
    case EMR_ALPHABLEND:
    {
1869
	const EMRALPHABLEND *pAlphaBlend = (const EMRALPHABLEND *)mr;
1870

1871
        TRACE("EMR_ALPHABLEND: %d, %d %dx%d -> %d, %d %dx%d. blendfn %08x offBitsSrc %d\n",
1872 1873 1874 1875 1876 1877 1878 1879 1880
	       pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
	       pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
	       pAlphaBlend->dwRop, pAlphaBlend->offBitsSrc);

        if(pAlphaBlend->offBmiSrc == 0) {
            FIXME("EMR_ALPHABLEND: offBmiSrc == 0\n");
        } else {
            HDC hdcSrc = CreateCompatibleDC(hdc);
            HBITMAP hBmp = 0, hBmpOld = 0;
1881
            const BITMAPINFO *pbi = (const BITMAPINFO *)((const BYTE *)mr + pAlphaBlend->offBmiSrc);
1882 1883 1884 1885 1886 1887
            BLENDFUNCTION blendfn;
            void *bits;

            SetWorldTransform(hdcSrc, &pAlphaBlend->xformSrc);

            hBmp = CreateDIBSection(hdc, pbi, pAlphaBlend->iUsageSrc, &bits, NULL, 0);
1888
            memcpy(bits, (const BYTE *)mr + pAlphaBlend->offBitsSrc, pAlphaBlend->cbBitsSrc);
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
            hBmpOld = SelectObject(hdcSrc, hBmp);

            blendfn.BlendOp             = (pAlphaBlend->dwRop >> 24) & 0xff;
            blendfn.BlendFlags          = (pAlphaBlend->dwRop >> 16) & 0xff;
            blendfn.SourceConstantAlpha = (pAlphaBlend->dwRop >>  8) & 0xff;
            blendfn.AlphaFormat         = (pAlphaBlend->dwRop) & 0xff;

            GdiAlphaBlend(hdc, pAlphaBlend->xDest, pAlphaBlend->yDest, pAlphaBlend->cxDest, pAlphaBlend->cyDest,
                       hdcSrc, pAlphaBlend->xSrc, pAlphaBlend->ySrc, pAlphaBlend->cxSrc, pAlphaBlend->cySrc,
                       blendfn);

            SelectObject(hdcSrc, hBmpOld);
            DeleteObject(hBmp);
            DeleteDC(hdcSrc);
        }
	break;
    }

1907
    case EMR_MASKBLT:
1908
    {
1909
	const EMRMASKBLT *pMaskBlt = (const EMRMASKBLT *)mr;
1910 1911 1912
	HDC hdcSrc = CreateCompatibleDC(hdc);
	HBRUSH hBrush, hBrushOld;
	HBITMAP hBmp, hBmpOld, hBmpMask;
1913
	const BITMAPINFO *pbi;
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924

	SetWorldTransform(hdcSrc, &pMaskBlt->xformSrc);

	hBrush = CreateSolidBrush(pMaskBlt->crBkColorSrc);
	hBrushOld = SelectObject(hdcSrc, hBrush);
	PatBlt(hdcSrc, pMaskBlt->rclBounds.left, pMaskBlt->rclBounds.top,
	       pMaskBlt->rclBounds.right - pMaskBlt->rclBounds.left,
	       pMaskBlt->rclBounds.bottom - pMaskBlt->rclBounds.top, PATCOPY);
	SelectObject(hdcSrc, hBrushOld);
	DeleteObject(hBrush);

1925
	pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiMask);
1926 1927 1928
	hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
	             1, 1, NULL);
	SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
1929
	  (const BYTE *)mr + pMaskBlt->offBitsMask, pbi, pMaskBlt->iUsageMask);
1930

1931 1932 1933
	pbi = (const BITMAPINFO *)((const BYTE *)mr + pMaskBlt->offBmiSrc);
	hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
			      (const BYTE *)mr + pMaskBlt->offBitsSrc, pbi, pMaskBlt->iUsageSrc);
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
	hBmpOld = SelectObject(hdcSrc, hBmp);
	MaskBlt(hdc,
		pMaskBlt->xDest,
	        pMaskBlt->yDest,
	        pMaskBlt->cxDest,
	        pMaskBlt->cyDest,
	        hdcSrc,
	        pMaskBlt->xSrc,
	        pMaskBlt->ySrc,
	        hBmpMask,
		pMaskBlt->xMask,
		pMaskBlt->yMask,
	        pMaskBlt->dwRop);
	SelectObject(hdcSrc, hBmpOld);
	DeleteObject(hBmp);
	DeleteObject(hBmpMask);
	DeleteDC(hdcSrc);
	break;
    }

1954
    case EMR_PLGBLT:
1955
    {
1956
	const EMRPLGBLT *pPlgBlt = (const EMRPLGBLT *)mr;
1957 1958 1959
	HDC hdcSrc = CreateCompatibleDC(hdc);
	HBRUSH hBrush, hBrushOld;
	HBITMAP hBmp, hBmpOld, hBmpMask;
1960
	const BITMAPINFO *pbi;
1961 1962 1963 1964
	POINT pts[3];

	SetWorldTransform(hdcSrc, &pPlgBlt->xformSrc);

1965 1966 1967
	pts[0].x = pPlgBlt->aptlDest[0].x; pts[0].y = pPlgBlt->aptlDest[0].y;
	pts[1].x = pPlgBlt->aptlDest[1].x; pts[1].y = pPlgBlt->aptlDest[1].y;
	pts[2].x = pPlgBlt->aptlDest[2].x; pts[2].y = pPlgBlt->aptlDest[2].y;
1968 1969 1970 1971 1972 1973 1974 1975 1976

	hBrush = CreateSolidBrush(pPlgBlt->crBkColorSrc);
	hBrushOld = SelectObject(hdcSrc, hBrush);
	PatBlt(hdcSrc, pPlgBlt->rclBounds.left, pPlgBlt->rclBounds.top,
	       pPlgBlt->rclBounds.right - pPlgBlt->rclBounds.left,
	       pPlgBlt->rclBounds.bottom - pPlgBlt->rclBounds.top, PATCOPY);
	SelectObject(hdcSrc, hBrushOld);
	DeleteObject(hBrush);

1977
	pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiMask);
1978 1979 1980
	hBmpMask = CreateBitmap(pbi->bmiHeader.biWidth, pbi->bmiHeader.biHeight,
	             1, 1, NULL);
	SetDIBits(hdc, hBmpMask, 0, pbi->bmiHeader.biHeight,
1981
	  (const BYTE *)mr + pPlgBlt->offBitsMask, pbi, pPlgBlt->iUsageMask);
1982

1983 1984 1985
	pbi = (const BITMAPINFO *)((const BYTE *)mr + pPlgBlt->offBmiSrc);
	hBmp = CreateDIBitmap(hdc, (const BITMAPINFOHEADER *)pbi, CBM_INIT,
			      (const BYTE *)mr + pPlgBlt->offBitsSrc, pbi, pPlgBlt->iUsageSrc);
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
	hBmpOld = SelectObject(hdcSrc, hBmp);
	PlgBlt(hdc,
	       pts,
	       hdcSrc,
	       pPlgBlt->xSrc,
	       pPlgBlt->ySrc,
	       pPlgBlt->cxSrc,
	       pPlgBlt->cySrc,
	       hBmpMask,
	       pPlgBlt->xMask,
	       pPlgBlt->yMask);
	SelectObject(hdcSrc, hBmpOld);
	DeleteObject(hBmp);
	DeleteObject(hBmpMask);
	DeleteDC(hdcSrc);
	break;
    }

2004
    case EMR_SETDIBITSTODEVICE:
2005
    {
2006
	const EMRSETDIBITSTODEVICE *pSetDIBitsToDevice = (const EMRSETDIBITSTODEVICE *)mr;
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016

	SetDIBitsToDevice(hdc,
			  pSetDIBitsToDevice->xDest,
			  pSetDIBitsToDevice->yDest,
			  pSetDIBitsToDevice->cxSrc,
			  pSetDIBitsToDevice->cySrc,
			  pSetDIBitsToDevice->xSrc,
			  pSetDIBitsToDevice->ySrc,
			  pSetDIBitsToDevice->iStartScan,
			  pSetDIBitsToDevice->cScans,
2017 2018
			  (const BYTE *)mr + pSetDIBitsToDevice->offBitsSrc,
			  (const BITMAPINFO *)((const BYTE *)mr + pSetDIBitsToDevice->offBmiSrc),
2019 2020 2021 2022
			  pSetDIBitsToDevice->iUsageSrc);
	break;
    }

2023
    case EMR_POLYTEXTOUTA:
2024
    {
2025
	const EMRPOLYTEXTOUTA *pPolyTextOutA = (const EMRPOLYTEXTOUTA *)mr;
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
	POLYTEXTA *polytextA = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutA->cStrings * sizeof(POLYTEXTA));
	LONG i;
	XFORM xform, xformOld;
	int gModeOld;

	gModeOld = SetGraphicsMode(hdc, pPolyTextOutA->iGraphicsMode);
	GetWorldTransform(hdc, &xformOld);

	xform.eM11 = pPolyTextOutA->exScale;
	xform.eM12 = 0.0;
	xform.eM21 = 0.0;
	xform.eM22 = pPolyTextOutA->eyScale;
	xform.eDx = 0.0;
	xform.eDy = 0.0;
	SetWorldTransform(hdc, &xform);

	/* Set up POLYTEXTA structures */
	for(i = 0; i < pPolyTextOutA->cStrings; i++)
	{
	    polytextA[i].x = pPolyTextOutA->aemrtext[i].ptlReference.x;
	    polytextA[i].y = pPolyTextOutA->aemrtext[i].ptlReference.y;
	    polytextA[i].n = pPolyTextOutA->aemrtext[i].nChars;
2048
	    polytextA[i].lpstr = (LPCSTR)((const BYTE *)mr + pPolyTextOutA->aemrtext[i].offString);
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
	    polytextA[i].uiFlags = pPolyTextOutA->aemrtext[i].fOptions;
	    polytextA[i].rcl.left = pPolyTextOutA->aemrtext[i].rcl.left;
	    polytextA[i].rcl.right = pPolyTextOutA->aemrtext[i].rcl.right;
	    polytextA[i].rcl.top = pPolyTextOutA->aemrtext[i].rcl.top;
	    polytextA[i].rcl.bottom = pPolyTextOutA->aemrtext[i].rcl.bottom;
	    polytextA[i].pdx = (int *)((BYTE *)mr + pPolyTextOutA->aemrtext[i].offDx);
	}
	PolyTextOutA(hdc, polytextA, pPolyTextOutA->cStrings);
	HeapFree(GetProcessHeap(), 0, polytextA);

	SetWorldTransform(hdc, &xformOld);
	SetGraphicsMode(hdc, gModeOld);
	break;
    }

2064
    case EMR_POLYTEXTOUTW:
2065
    {
2066
	const EMRPOLYTEXTOUTW *pPolyTextOutW = (const EMRPOLYTEXTOUTW *)mr;
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
	POLYTEXTW *polytextW = HeapAlloc(GetProcessHeap(), 0, pPolyTextOutW->cStrings * sizeof(POLYTEXTW));
	LONG i;
	XFORM xform, xformOld;
	int gModeOld;

	gModeOld = SetGraphicsMode(hdc, pPolyTextOutW->iGraphicsMode);
	GetWorldTransform(hdc, &xformOld);

	xform.eM11 = pPolyTextOutW->exScale;
	xform.eM12 = 0.0;
	xform.eM21 = 0.0;
	xform.eM22 = pPolyTextOutW->eyScale;
	xform.eDx = 0.0;
	xform.eDy = 0.0;
	SetWorldTransform(hdc, &xform);

	/* Set up POLYTEXTW structures */
	for(i = 0; i < pPolyTextOutW->cStrings; i++)
	{
	    polytextW[i].x = pPolyTextOutW->aemrtext[i].ptlReference.x;
	    polytextW[i].y = pPolyTextOutW->aemrtext[i].ptlReference.y;
	    polytextW[i].n = pPolyTextOutW->aemrtext[i].nChars;
2089
	    polytextW[i].lpstr = (LPCWSTR)((const BYTE *)mr + pPolyTextOutW->aemrtext[i].offString);
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
	    polytextW[i].uiFlags = pPolyTextOutW->aemrtext[i].fOptions;
	    polytextW[i].rcl.left = pPolyTextOutW->aemrtext[i].rcl.left;
	    polytextW[i].rcl.right = pPolyTextOutW->aemrtext[i].rcl.right;
	    polytextW[i].rcl.top = pPolyTextOutW->aemrtext[i].rcl.top;
	    polytextW[i].rcl.bottom = pPolyTextOutW->aemrtext[i].rcl.bottom;
	    polytextW[i].pdx = (int *)((BYTE *)mr + pPolyTextOutW->aemrtext[i].offDx);
	}
	PolyTextOutW(hdc, polytextW, pPolyTextOutW->cStrings);
	HeapFree(GetProcessHeap(), 0, polytextW);

	SetWorldTransform(hdc, &xformOld);
	SetGraphicsMode(hdc, gModeOld);
	break;
    }

2105
    case EMR_FILLRGN:
2106
    {
2107
	const EMRFILLRGN *pFillRgn = (const EMRFILLRGN *)mr;
2108 2109 2110 2111 2112 2113 2114 2115
	HRGN hRgn = ExtCreateRegion(NULL, pFillRgn->cbRgnData, (RGNDATA *)pFillRgn->RgnData);
	FillRgn(hdc,
		hRgn,
		(handletable->objectHandle)[pFillRgn->ihBrush]);
	DeleteObject(hRgn);
	break;
    }

2116
    case EMR_FRAMERGN:
2117
    {
2118
	const EMRFRAMERGN *pFrameRgn = (const EMRFRAMERGN *)mr;
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
	HRGN hRgn = ExtCreateRegion(NULL, pFrameRgn->cbRgnData, (RGNDATA *)pFrameRgn->RgnData);
	FrameRgn(hdc,
		 hRgn,
		 (handletable->objectHandle)[pFrameRgn->ihBrush],
		 pFrameRgn->szlStroke.cx,
		 pFrameRgn->szlStroke.cy);
	DeleteObject(hRgn);
	break;
    }

2129
    case EMR_INVERTRGN:
2130
    {
2131
	const EMRINVERTRGN *pInvertRgn = (const EMRINVERTRGN *)mr;
2132 2133 2134 2135 2136 2137
	HRGN hRgn = ExtCreateRegion(NULL, pInvertRgn->cbRgnData, (RGNDATA *)pInvertRgn->RgnData);
	InvertRgn(hdc, hRgn);
	DeleteObject(hRgn);
	break;
    }

2138
    case EMR_PAINTRGN:
2139
    {
2140
	const EMRPAINTRGN *pPaintRgn = (const EMRPAINTRGN *)mr;
2141 2142 2143 2144 2145 2146
	HRGN hRgn = ExtCreateRegion(NULL, pPaintRgn->cbRgnData, (RGNDATA *)pPaintRgn->RgnData);
	PaintRgn(hdc, hRgn);
	DeleteObject(hRgn);
	break;
    }

2147 2148
    case EMR_SETTEXTJUSTIFICATION:
    {
2149
	const EMRSETTEXTJUSTIFICATION *pSetTextJust = (const EMRSETTEXTJUSTIFICATION *)mr;
2150 2151 2152 2153 2154 2155
	SetTextJustification(hdc, pSetTextJust->nBreakExtra, pSetTextJust->nBreakCount);
	break;
    }

    case EMR_SETLAYOUT:
    {
2156
	const EMRSETLAYOUT *pSetLayout = (const EMRSETLAYOUT *)mr;
2157 2158 2159 2160
	SetLayout(hdc, pSetLayout->iMode);
	break;
    }

2161
    case EMR_POLYDRAW16:
2162 2163
    case EMR_GLSRECORD:
    case EMR_GLSBOUNDEDRECORD:
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
	case EMR_DRAWESCAPE :
	case EMR_EXTESCAPE:
	case EMR_STARTDOC:
	case EMR_SMALLTEXTOUT:
	case EMR_FORCEUFIMAPPING:
	case EMR_NAMEDESCAPE:
	case EMR_COLORCORRECTPALETTE:
	case EMR_SETICMPROFILEA:
	case EMR_SETICMPROFILEW:
	case EMR_TRANSPARENTBLT:
	case EMR_GRADIENTFILL:
	case EMR_SETLINKEDUFI:
	case EMR_COLORMATCHTOTARGETW:
	case EMR_CREATECOLORSPACEW:

Alexandre Julliard's avatar
Alexandre Julliard committed
2179
    default:
2180
      /* From docs: If PlayEnhMetaFileRecord doesn't recognize a
2181
                    record then ignore and return TRUE. */
2182
      FIXME("type %d is unimplemented\n", type);
Alexandre Julliard's avatar
Alexandre Julliard committed
2183 2184
      break;
    }
2185 2186 2187
  tmprc.left = tmprc.top = 0;
  tmprc.right = tmprc.bottom = 1000;
  LPtoDP(hdc, (POINT*)&tmprc, 2);
2188
  TRACE("L:0,0 - 1000,1000 -> D:%d,%d - %d,%d\n", tmprc.left,
2189 2190
	tmprc.top, tmprc.right, tmprc.bottom);

Alexandre Julliard's avatar
Alexandre Julliard committed
2191
  return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2192 2193 2194 2195 2196
}


/*****************************************************************************
 *
2197
 *        EnumEnhMetaFile  (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2198 2199 2200
 *
 *  Walk an enhanced metafile, calling a user-specified function _EnhMetaFunc_
 *  for each
2201
 *  record. Returns when either every record has been used or
Alexandre Julliard's avatar
Alexandre Julliard committed
2202 2203 2204 2205 2206 2207 2208 2209
 *  when _EnhMetaFunc_ returns FALSE.
 *
 *
 * RETURNS
 *  TRUE if every record is used, FALSE if any invocation of _EnhMetaFunc_
 *  returns FALSE.
 *
 * BUGS
Alexandre Julliard's avatar
Alexandre Julliard committed
2210
 *   Ignores rect.
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
 *
 * NOTES
 *   This function behaves differently in Win9x and WinNT.
 *
 *   In WinNT, the DC's world transform is updated as the EMF changes
 *    the Window/Viewport Extent and Origin or it's world transform.
 *    The actual Window/Viewport Extent and Origin are left untouched.
 *
 *   In Win9x, the DC is left untouched, and PlayEnhMetaFileRecord
 *    updates the scaling itself but only just before a record that
 *    writes anything to the DC.
 *
 *   I'm not sure where the data (enum_emh_data) is stored in either
 *    version. For this implementation, it is stored before the handle
 *    table, but it could be stored in the DC, in the EMF handle or in
 *    TLS.
 *             MJM  5 Oct 2002
Alexandre Julliard's avatar
Alexandre Julliard committed
2228
 */
2229
BOOL WINAPI EnumEnhMetaFile(
2230 2231
     HDC hdc,                /* [in] device context to pass to _EnhMetaFunc_ */
     HENHMETAFILE hmf,       /* [in] EMF to walk */
2232
     ENHMFENUMPROC callback, /* [in] callback function */
2233 2234
     LPVOID data,            /* [in] optional data for callback function */
     const RECT *lpRect      /* [in] bounding rectangle for rendered metafile */
Alexandre Julliard's avatar
Alexandre Julliard committed
2235 2236
    )
{
2237
    BOOL ret;
2238
    ENHMETAHEADER *emh;
2239 2240
    ENHMETARECORD *emr;
    DWORD offset;
2241
    UINT i;
2242
    HANDLETABLE *ht;
2243
    INT savedMode = 0;
2244
    XFORM savedXform;
2245 2246 2247
    HPEN hPen = NULL;
    HBRUSH hBrush = NULL;
    HFONT hFont = NULL;
2248
    HRGN hRgn = NULL;
2249 2250 2251
    enum_emh_data *info;
    SIZE vp_size, win_size;
    POINT vp_org, win_org;
2252
    INT mapMode = MM_TEXT, old_align = 0, old_rop2 = 0, old_arcdir = 0, old_polyfill = 0, old_stretchblt = 0;
2253
    COLORREF old_text_color = 0, old_bk_color = 0;
2254

2255
    if(!lpRect && hdc)
2256 2257 2258 2259 2260 2261
    {
	SetLastError(ERROR_INVALID_PARAMETER);
	return FALSE;
    }

    emh = EMF_GetEnhMetaHeader(hmf);
2262 2263 2264 2265
    if(!emh) {
        SetLastError(ERROR_INVALID_HANDLE);
        return FALSE;
    }
2266

2267
    info = HeapAlloc( GetProcessHeap(), 0,
2268 2269
		    sizeof (enum_emh_data) + sizeof(HANDLETABLE) * emh->nHandles );
    if(!info)
2270 2271 2272 2273
    {
	SetLastError(ERROR_NOT_ENOUGH_MEMORY);
	return FALSE;
    }
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
    info->state.wndOrgX = 0;
    info->state.wndOrgY = 0;
    info->state.wndExtX = 1;
    info->state.wndExtY = 1;
    info->state.vportOrgX = 0;
    info->state.vportOrgY = 0;
    info->state.vportExtX = 1;
    info->state.vportExtY = 1;
    info->state.world_transform.eM11 = info->state.world_transform.eM22 = 1;
    info->state.world_transform.eM12 = info->state.world_transform.eM21 = 0;
    info->state.world_transform.eDx  = info->state.world_transform.eDy =  0;

2286
    info->state.next = NULL;
2287 2288
    info->save_level = 0;
    info->saved_state = NULL;
2289 2290

    ht = (HANDLETABLE*) &info[1];
2291
    ht->objectHandle[0] = hmf;
2292 2293
    for(i = 1; i < emh->nHandles; i++)
        ht->objectHandle[i] = NULL;
2294

2295
    if(hdc)
2296 2297 2298
    {
	savedMode = SetGraphicsMode(hdc, GM_ADVANCED);
	GetWorldTransform(hdc, &savedXform);
2299 2300 2301 2302 2303
        GetViewportExtEx(hdc, &vp_size);
        GetWindowExtEx(hdc, &win_size);
        GetViewportOrgEx(hdc, &vp_org);
        GetWindowOrgEx(hdc, &win_org);
        mapMode = GetMapMode(hdc);
2304

2305
	/* save DC */
2306 2307 2308
	hPen = GetCurrentObject(hdc, OBJ_PEN);
	hBrush = GetCurrentObject(hdc, OBJ_BRUSH);
	hFont = GetCurrentObject(hdc, OBJ_FONT);
2309

2310 2311 2312 2313 2314 2315 2316
        hRgn = CreateRectRgn(0, 0, 0, 0);
        if (!GetClipRgn(hdc, hRgn))
        {
            DeleteObject(hRgn);
            hRgn = 0;
        }

2317 2318
        old_text_color = SetTextColor(hdc, RGB(0,0,0));
        old_bk_color = SetBkColor(hdc, RGB(0xff, 0xff, 0xff));
2319 2320 2321 2322 2323
        old_align = SetTextAlign(hdc, 0);
        old_rop2 = SetROP2(hdc, R2_COPYPEN);
        old_arcdir = SetArcDirection(hdc, AD_COUNTERCLOCKWISE);
        old_polyfill = SetPolyFillMode(hdc, ALTERNATE);
        old_stretchblt = SetStretchBltMode(hdc, BLACKONWHITE);
2324
    }
2325

2326
    info->state.mode = MM_TEXT;
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340

    if ( IS_WIN9X() )
    {
        /* Win95 leaves the vp/win ext/org info alone */
        info->init_transform.eM11 = 1.0;
        info->init_transform.eM12 = 0.0;
        info->init_transform.eM21 = 0.0;
        info->init_transform.eM22 = 1.0;
        info->init_transform.eDx  = 0.0;
        info->init_transform.eDy  = 0.0;
    }
    else
    {
        /* WinNT combines the vp/win ext/org info into a transform */
2341 2342 2343
        double xscale, yscale;
        xscale = (double)vp_size.cx / (double)win_size.cx;
        yscale = (double)vp_size.cy / (double)win_size.cy;
2344 2345 2346 2347
        info->init_transform.eM11 = xscale;
        info->init_transform.eM12 = 0.0;
        info->init_transform.eM21 = 0.0;
        info->init_transform.eM22 = yscale;
2348 2349
        info->init_transform.eDx  = (double)vp_org.x - xscale * (double)win_org.x;
        info->init_transform.eDy  = (double)vp_org.y - yscale * (double)win_org.y;
2350 2351 2352 2353

        CombineTransform(&info->init_transform, &savedXform, &info->init_transform);
    }

2354
    if ( lpRect && WIDTH(emh->rclFrame) && HEIGHT(emh->rclFrame) )
2355
    {
2356
        double xSrcPixSize, ySrcPixSize, xscale, yscale;
2357 2358
        XFORM xform;

2359
        TRACE("rect: %d,%d - %d,%d. rclFrame: %d,%d - %d,%d\n",
2360 2361 2362 2363
           lpRect->left, lpRect->top, lpRect->right, lpRect->bottom,
           emh->rclFrame.left, emh->rclFrame.top, emh->rclFrame.right,
           emh->rclFrame.bottom);

2364 2365 2366
        xSrcPixSize = (double) emh->szlMillimeters.cx / emh->szlDevice.cx;
        ySrcPixSize = (double) emh->szlMillimeters.cy / emh->szlDevice.cy;
        xscale = (double) WIDTH(*lpRect) * 100.0 /
2367
                 WIDTH(emh->rclFrame) * xSrcPixSize;
2368
        yscale = (double) HEIGHT(*lpRect) * 100.0 /
2369
                 HEIGHT(emh->rclFrame) * ySrcPixSize;
2370
        TRACE("xscale = %f, yscale = %f\n", xscale, yscale);
2371 2372 2373 2374 2375

        xform.eM11 = xscale;
        xform.eM12 = 0;
        xform.eM21 = 0;
        xform.eM22 = yscale;
2376 2377
        xform.eDx = (double) lpRect->left - (double) WIDTH(*lpRect) / WIDTH(emh->rclFrame) * emh->rclFrame.left;
        xform.eDy = (double) lpRect->top - (double) HEIGHT(*lpRect) / HEIGHT(emh->rclFrame) * emh->rclFrame.top;
2378 2379 2380 2381 2382

        CombineTransform(&info->init_transform, &xform, &info->init_transform);
    }

    /* WinNT resets the current vp/win org/ext */
2383
    if ( !IS_WIN9X() && hdc )
2384 2385 2386 2387 2388 2389
    {
        SetMapMode(hdc, MM_TEXT);
        SetWindowOrgEx(hdc, 0, 0, NULL);
        SetViewportOrgEx(hdc, 0, 0, NULL);
        EMF_Update_MF_Xform(hdc, info);
    }
2390 2391 2392 2393 2394 2395

    ret = TRUE;
    offset = 0;
    while(ret && offset < emh->nBytes)
    {
	emr = (ENHMETARECORD *)((char *)emh + offset);
2396 2397 2398 2399 2400

        /* In Win9x mode we update the xform if the record will produce output */
        if (hdc && IS_WIN9X() && emr_produces_output(emr->iType))
            EMF_Update_MF_Xform(hdc, info);

2401
	TRACE("Calling EnumFunc with record %s, size %d\n", get_emr_name(emr->iType), emr->nSize);
2402
	ret = (*callback)(hdc, ht, emr, emh->nHandles, (LPARAM)data);
2403
	offset += emr->nSize;
2404 2405 2406 2407 2408

        /* WinNT - update the transform (win9x updates when the next graphics
           output record is played). */
        if (hdc && !IS_WIN9X())
            EMF_Update_MF_Xform(hdc, info);
2409
    }
2410

2411 2412
    if (hdc)
    {
2413 2414 2415 2416 2417
        SetStretchBltMode(hdc, old_stretchblt);
        SetPolyFillMode(hdc, old_polyfill);
        SetArcDirection(hdc, old_arcdir);
        SetROP2(hdc, old_rop2);
        SetTextAlign(hdc, old_align);
2418 2419 2420
        SetBkColor(hdc, old_bk_color);
        SetTextColor(hdc, old_text_color);

2421
	/* restore DC */
2422 2423 2424
	SelectObject(hdc, hBrush);
	SelectObject(hdc, hPen);
	SelectObject(hdc, hFont);
2425 2426
        ExtSelectClipRgn(hdc, hRgn, RGN_COPY);
        DeleteObject(hRgn);
2427 2428 2429 2430

	SetWorldTransform(hdc, &savedXform);
	if (savedMode)
	    SetGraphicsMode(hdc, savedMode);
2431 2432 2433 2434 2435
        SetMapMode(hdc, mapMode);
        SetWindowOrgEx(hdc, win_org.x, win_org.y, NULL);
        SetWindowExtEx(hdc, win_size.cx, win_size.cy, NULL);
        SetViewportOrgEx(hdc, vp_org.x, vp_org.y, NULL);
        SetViewportExtEx(hdc, vp_size.cx, vp_size.cy, NULL);
2436
    }
2437

2438
    for(i = 1; i < emh->nHandles; i++) /* Don't delete element 0 (hmf) */
2439 2440
        if( (ht->objectHandle)[i] )
	    DeleteObject( (ht->objectHandle)[i] );
2441

2442 2443 2444 2445 2446 2447
    while (info->saved_state)
    {
        EMF_dc_state *state = info->saved_state;
        info->saved_state = info->saved_state->next;
        HeapFree( GetProcessHeap(), 0, state );
    }
2448
    HeapFree( GetProcessHeap(), 0, info );
2449
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
2450 2451
}

2452
static INT CALLBACK EMF_PlayEnhMetaFileCallback(HDC hdc, HANDLETABLE *ht,
2453 2454
						const ENHMETARECORD *emr,
						INT handles, LPARAM data)
2455 2456 2457
{
    return PlayEnhMetaFileRecord(hdc, ht, emr, handles);
}
2458

2459
/**************************************************************************
2460
 *    PlayEnhMetaFile  (GDI32.@)
2461 2462 2463 2464
 *
 *    Renders an enhanced metafile into a specified rectangle *lpRect
 *    in device context hdc.
 *
2465 2466 2467
 * RETURNS
 *    Success: TRUE
 *    Failure: FALSE
2468
 */
2469
BOOL WINAPI PlayEnhMetaFile(
2470 2471 2472
       HDC hdc,           /* [in] DC to render into */
       HENHMETAFILE hmf,  /* [in] metafile to render */
       const RECT *lpRect /* [in] rectangle to place metafile inside */
2473 2474 2475 2476 2477 2478
      )
{
    return EnumEnhMetaFile(hdc, hmf, EMF_PlayEnhMetaFileCallback, NULL,
			   lpRect);
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2479
/*****************************************************************************
2480
 *  DeleteEnhMetaFile (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2481 2482
 *
 *  Deletes an enhanced metafile and frees the associated storage.
Alexandre Julliard's avatar
Alexandre Julliard committed
2483
 */
2484 2485 2486
BOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf)
{
    return EMF_Delete_HENHMETAFILE( hmf );
Alexandre Julliard's avatar
Alexandre Julliard committed
2487 2488 2489
}

/*****************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
2490 2491 2492
 *  CopyEnhMetaFileA (GDI32.@)
 *
 * Duplicate an enhanced metafile.
Alexandre Julliard's avatar
Alexandre Julliard committed
2493
 *
2494
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
2495
 */
2496
HENHMETAFILE WINAPI CopyEnhMetaFileA(
2497
    HENHMETAFILE hmfSrc,
Alexandre Julliard's avatar
Alexandre Julliard committed
2498 2499
    LPCSTR file)
{
2500 2501 2502
    ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
    HENHMETAFILE hmfDst;

2503
    if(!emrSrc) return FALSE;
2504
    if (!file) {
2505
        emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
2506
	memcpy( emrDst, emrSrc, emrSrc->nBytes );
2507
	hmfDst = EMF_Create_HENHMETAFILE( emrDst, FALSE );
2508
    } else {
2509
        HANDLE hFile;
2510
        DWORD w;
2511 2512
        hFile = CreateFileA( file, GENERIC_WRITE | GENERIC_READ, 0,
			     NULL, CREATE_ALWAYS, 0, 0);
2513
	WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
2514 2515 2516 2517 2518 2519 2520 2521 2522
	CloseHandle( hFile );
	/* Reopen file for reading only, so that apps can share
	   read access to the file while hmf is still valid */
        hFile = CreateFileA( file, GENERIC_READ, FILE_SHARE_READ,
			     NULL, OPEN_EXISTING, 0, 0);
	if(hFile == INVALID_HANDLE_VALUE) {
	    ERR("Can't reopen emf for reading\n");
	    return 0;
	}
2523
	hmfDst = EMF_GetEnhMetaFile( hFile );
2524
        CloseHandle( hFile );
2525 2526
    }
    return hmfDst;
Alexandre Julliard's avatar
Alexandre Julliard committed
2527 2528
}

Huw Davies's avatar
Huw Davies committed
2529
/*****************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
2530 2531 2532
 *  CopyEnhMetaFileW (GDI32.@)
 *
 * See CopyEnhMetaFileA.
Huw Davies's avatar
Huw Davies committed
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549
 *
 *
 */
HENHMETAFILE WINAPI CopyEnhMetaFileW(
    HENHMETAFILE hmfSrc,
    LPCWSTR file)
{
    ENHMETAHEADER *emrSrc = EMF_GetEnhMetaHeader( hmfSrc ), *emrDst;
    HENHMETAFILE hmfDst;

    if(!emrSrc) return FALSE;
    if (!file) {
        emrDst = HeapAlloc( GetProcessHeap(), 0, emrSrc->nBytes );
	memcpy( emrDst, emrSrc, emrSrc->nBytes );
	hmfDst = EMF_Create_HENHMETAFILE( emrDst, FALSE );
    } else {
        HANDLE hFile;
2550
        DWORD w;
Huw Davies's avatar
Huw Davies committed
2551 2552
        hFile = CreateFileW( file, GENERIC_WRITE | GENERIC_READ, 0,
			     NULL, CREATE_ALWAYS, 0, 0);
2553
	WriteFile( hFile, emrSrc, emrSrc->nBytes, &w, NULL);
Huw Davies's avatar
Huw Davies committed
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568
	CloseHandle( hFile );
	/* Reopen file for reading only, so that apps can share
	   read access to the file while hmf is still valid */
        hFile = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ,
			     NULL, OPEN_EXISTING, 0, 0);
	if(hFile == INVALID_HANDLE_VALUE) {
	    ERR("Can't reopen emf for reading\n");
	    return 0;
	}
	hmfDst = EMF_GetEnhMetaFile( hFile );
        CloseHandle( hFile );
    }
    return hmfDst;
}

2569

2570 2571 2572 2573 2574 2575 2576
/* Struct to be used to be passed in the LPVOID parameter for cbEnhPaletteCopy */
typedef struct tagEMF_PaletteCopy
{
   UINT cEntries;
   LPPALETTEENTRY lpPe;
} EMF_PaletteCopy;

2577
/***************************************************************
2578
 * Find the EMR_EOF record and then use it to find the
2579
 * palette entries for this enhanced metafile.
2580
 * The lpData is actually a pointer to an EMF_PaletteCopy struct
2581 2582 2583 2584 2585
 * which contains the max number of elements to copy and where
 * to copy them to.
 *
 * NOTE: To be used by GetEnhMetaFilePaletteEntries only!
 */
2586
static INT CALLBACK cbEnhPaletteCopy( HDC a,
2587 2588
                               HANDLETABLE *b,
                               const ENHMETARECORD *lpEMR,
2589
                               INT c,
2590
                               LPARAM lpData )
2591
{
2592

2593 2594
  if ( lpEMR->iType == EMR_EOF )
  {
2595
    const EMREOF *lpEof = (const EMREOF *)lpEMR;
2596
    EMF_PaletteCopy* info = (EMF_PaletteCopy*)lpData;
2597
    DWORD dwNumPalToCopy = min( lpEof->nPalEntries, info->cEntries );
2598

2599
    TRACE( "copying 0x%08x palettes\n", dwNumPalToCopy );
2600

2601
    memcpy( info->lpPe, (LPCSTR)lpEof + lpEof->offPalEntries,
2602 2603 2604 2605
            sizeof( *(info->lpPe) ) * dwNumPalToCopy );

    /* Update the passed data as a return code */
    info->lpPe     = NULL; /* Palettes were copied! */
2606
    info->cEntries = dwNumPalToCopy;
2607 2608 2609

    return FALSE; /* That's all we need */
  }
2610

2611 2612 2613
  return TRUE;
}

2614
/*****************************************************************************
2615 2616 2617 2618
 *  GetEnhMetaFilePaletteEntries (GDI32.@)
 *
 *  Copy the palette and report size
 *
2619
 *  BUGS: Error codes (SetLastError) are not set on failures
2620
 */
2621 2622 2623
UINT WINAPI GetEnhMetaFilePaletteEntries( HENHMETAFILE hEmf,
					  UINT cEntries,
					  LPPALETTEENTRY lpPe )
2624
{
2625
  ENHMETAHEADER* enhHeader = EMF_GetEnhMetaHeader( hEmf );
2626
  EMF_PaletteCopy infoForCallBack;
2627

2628
  TRACE( "(%p,%d,%p)\n", hEmf, cEntries, lpPe );
2629

2630 2631
  if (!enhHeader) return 0;

2632 2633
  /* First check if there are any palettes associated with
     this metafile. */
2634
  if ( enhHeader->nPalEntries == 0 ) return 0;
2635 2636

  /* Is the user requesting the number of palettes? */
2637
  if ( lpPe == NULL ) return enhHeader->nPalEntries;
2638 2639 2640

  /* Copy cEntries worth of PALETTEENTRY structs into the buffer */
  infoForCallBack.cEntries = cEntries;
2641
  infoForCallBack.lpPe     = lpPe;
2642

2643
  if ( !EnumEnhMetaFile( 0, hEmf, cbEnhPaletteCopy,
2644
                         &infoForCallBack, 0 ) )
2645
      return GDI_ERROR;
2646 2647 2648 2649 2650 2651

  /* Verify that the callback executed correctly */
  if ( infoForCallBack.lpPe != NULL )
  {
     /* Callback proc had error! */
     ERR( "cbEnhPaletteCopy didn't execute correctly\n" );
2652
     return GDI_ERROR;
2653 2654
  }

2655
  return infoForCallBack.cEntries;
2656 2657
}

2658 2659 2660 2661 2662 2663 2664 2665 2666 2667
typedef struct gdi_mf_comment
{
    DWORD ident;
    DWORD iComment;
    DWORD nVersion;
    DWORD nChecksum;
    DWORD fFlags;
    DWORD cbWinMetaFile;
} gdi_mf_comment;

2668
/******************************************************************
2669
 *         SetWinMetaFileBits   (GDI32.@)
2670
 *
2671
 *         Translate from old style to new style.
2672
 *
2673
 */
2674
HENHMETAFILE WINAPI SetWinMetaFileBits(UINT cbBuffer,
2675
					   CONST BYTE *lpbBuffer,
2676 2677
					   HDC hdcRef,
					   CONST METAFILEPICT *lpmfp
2678
					   )
2679
{
Jon Griffiths's avatar
Jon Griffiths committed
2680
    static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
2681 2682 2683
    HMETAFILE hmf = NULL;
    HENHMETAFILE ret = NULL;
    HDC hdc = NULL, hdcdisp = NULL;
2684
    RECT rc, *prcFrame = NULL;
2685
    LONG mm, xExt, yExt;
2686
    INT horzsize, vertsize, horzres, vertres;
2687

2688
    TRACE("(%d, %p, %p, %p)\n", cbBuffer, lpbBuffer, hdcRef, lpmfp);
2689

2690 2691 2692 2693
    hmf = SetMetaFileBitsEx(cbBuffer, lpbBuffer);
    if(!hmf)
    {
        WARN("SetMetaFileBitsEx failed\n");
2694
        return NULL;
2695
    }
2696

2697
    if(!hdcRef)
Jon Griffiths's avatar
Jon Griffiths committed
2698
        hdcRef = hdcdisp = CreateDCW(szDisplayW, NULL, NULL, NULL);
2699

2700
    if (lpmfp)
2701
    {
2702
        TRACE("mm = %d %dx%d\n", lpmfp->mm, lpmfp->xExt, lpmfp->yExt);
2703 2704 2705 2706 2707 2708

        mm = lpmfp->mm;
        xExt = lpmfp->xExt;
        yExt = lpmfp->yExt;
    }
    else
2709
    {
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734
        TRACE("lpmfp == NULL\n");

        /* Use the whole device surface */
        mm = MM_ANISOTROPIC;
        xExt = 0;
        yExt = 0;
    }

    if (mm == MM_ISOTROPIC || mm == MM_ANISOTROPIC)
    {
        if (xExt < 0 || yExt < 0)
        {
          /* Use the whole device surface */
          xExt = 0;
          yExt = 0;
        }

        /* Use the x and y extents as the frame box */
        if (xExt && yExt)
        {
            rc.left = rc.top = 0;
            rc.right = xExt;
            rc.bottom = yExt;
            prcFrame = &rc;
        }
2735
    }
2736

2737 2738
    if(!(hdc = CreateEnhMetaFileW(hdcRef, NULL, prcFrame, NULL)))
    {
2739
        ERR("CreateEnhMetaFile failed\n");
2740
        goto end;
2741
    }
2742

2743 2744 2745 2746
    /*
     * Write the original METAFILE into the enhanced metafile.
     * It is encapsulated in a GDICOMMENT_WINDOWS_METAFILE record.
     */
2747
    if (mm != MM_TEXT)
2748
    {
2749 2750
        gdi_mf_comment *mfcomment;
        UINT mfcomment_size;
2751

2752 2753 2754
        mfcomment_size = sizeof (gdi_mf_comment) + cbBuffer;
        mfcomment = HeapAlloc(GetProcessHeap(), 0, mfcomment_size);
        if (mfcomment)
2755
        {
2756 2757 2758 2759 2760 2761 2762 2763 2764
            mfcomment->ident = GDICOMMENT_IDENTIFIER;
            mfcomment->iComment = GDICOMMENT_WINDOWS_METAFILE;
            mfcomment->nVersion = 0x00000300;
            mfcomment->nChecksum = 0; /* FIXME */
            mfcomment->fFlags = 0;
            mfcomment->cbWinMetaFile = cbBuffer;
            memcpy(&mfcomment[1], lpbBuffer, cbBuffer);
            GdiComment(hdc, mfcomment_size, (BYTE*) mfcomment);
            HeapFree(GetProcessHeap(), 0, mfcomment);
2765
        }
2766 2767
        SetMapMode(hdc, mm);
    }
2768

2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779

    horzsize = GetDeviceCaps(hdcRef, HORZSIZE);
    vertsize = GetDeviceCaps(hdcRef, VERTSIZE);
    horzres = GetDeviceCaps(hdcRef, HORZRES);
    vertres = GetDeviceCaps(hdcRef, VERTRES);

    if (!xExt || !yExt)
    {
        /* Use the whole device surface */
       xExt = horzres;
       yExt = vertres;
2780
    }
2781 2782 2783 2784 2785 2786 2787 2788 2789
    else
    {
        xExt = MulDiv(xExt, horzres, 100 * horzsize);
        yExt = MulDiv(yExt, vertres, 100 * vertsize);
    }

    /* set the initial viewport:window ratio as 1:1 */
    SetViewportExtEx(hdc, xExt, yExt, NULL);
    SetWindowExtEx(hdc,   xExt, yExt, NULL);
2790

2791
    PlayMetaFile(hdc, hmf);
2792

2793
    ret = CloseEnhMetaFile(hdc);
2794
end:
2795
    if (hdcdisp) DeleteDC(hdcdisp);
2796 2797
    DeleteMetaFile(hmf);
    return ret;
2798
}