2 * Enhanced MetaFile objects
4 * Copyright 1999 Huw D M Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * 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
25 #include "enhmfdrv/enhmetafiledrv.h"
26 #include "gdi_private.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
32 /******************************************************************
35 static UINT EMFDRV_AddHandle( PHYSDEV dev, HGDIOBJ obj )
37 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
40 for(index = 0; index < physDev->handles_size; index++)
41 if(physDev->handles[index] == 0) break;
42 if(index == physDev->handles_size) {
43 physDev->handles_size += HANDLE_LIST_INC;
44 physDev->handles = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
46 physDev->handles_size * sizeof(physDev->handles[0]));
48 physDev->handles[index] = obj;
50 physDev->cur_handles++;
51 if(physDev->cur_handles > physDev->emh->nHandles)
52 physDev->emh->nHandles++;
54 return index + 1; /* index 0 is reserved for the hmf, so we increment everything by 1 */
57 /******************************************************************
60 static UINT EMFDRV_FindObject( PHYSDEV dev, HGDIOBJ obj )
62 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
65 for(index = 0; index < physDev->handles_size; index++)
66 if(physDev->handles[index] == obj) break;
68 if(index == physDev->handles_size) return 0;
74 /******************************************************************
77 BOOL CDECL EMFDRV_DeleteObject( PHYSDEV dev, HGDIOBJ obj )
80 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
84 if(!(index = EMFDRV_FindObject(dev, obj))) return 0;
86 emr.emr.iType = EMR_DELETEOBJECT;
87 emr.emr.nSize = sizeof(emr);
90 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
93 physDev->handles[index - 1] = 0;
94 physDev->cur_handles--;
99 /***********************************************************************
100 * EMFDRV_SelectBitmap
102 HBITMAP CDECL EMFDRV_SelectBitmap( PHYSDEV dev, HBITMAP hbitmap )
108 /* Internal helper for EMFDRV_CreateBrushIndirect():
109 * Change the padding of a bitmap from 16 (BMP) to 32 (DIB) bits.
111 static inline void EMFDRV_PadTo32(LPBYTE lpRows, int height, int width)
113 int bytes16 = 2 * ((width + 15) / 16);
114 int bytes32 = 4 * ((width + 31) / 32);
121 height = abs(height) - 1;
122 lpSrc = lpRows + height * bytes16;
123 lpDst = lpRows + height * bytes32;
125 /* Note that we work backwards so we can re-pad in place */
128 for (i = bytes32; i > bytes16; i--)
129 lpDst[i - 1] = 0; /* Zero the padding bytes */
131 lpDst[i - 1] = lpSrc[i - 1]; /* Move image bytes into alignment */
138 /***********************************************************************
139 * EMFDRV_CreateBrushIndirect
141 DWORD EMFDRV_CreateBrushIndirect( PHYSDEV dev, HBRUSH hBrush )
146 if (!GetObjectA( hBrush, sizeof(logbrush), &logbrush )) return 0;
148 switch (logbrush.lbStyle) {
153 EMRCREATEBRUSHINDIRECT emr;
154 emr.emr.iType = EMR_CREATEBRUSHINDIRECT;
155 emr.emr.nSize = sizeof(emr);
156 emr.ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
157 emr.lb.lbStyle = logbrush.lbStyle;
158 emr.lb.lbColor = logbrush.lbColor;
159 emr.lb.lbHatch = logbrush.lbHatch;
161 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
167 EMRCREATEDIBPATTERNBRUSHPT *emr;
168 DWORD bmSize, biSize, size;
169 BITMAPINFO *info = GlobalLock16(logbrush.lbHatch);
171 if (info->bmiHeader.biCompression)
172 bmSize = info->bmiHeader.biSizeImage;
174 bmSize = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
175 info->bmiHeader.biHeight,
176 info->bmiHeader.biBitCount);
177 biSize = bitmap_info_size(info, LOWORD(logbrush.lbColor));
178 size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize;
179 emr = HeapAlloc( GetProcessHeap(), 0, size );
181 emr->emr.iType = EMR_CREATEDIBPATTERNBRUSHPT;
182 emr->emr.nSize = size;
183 emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
184 emr->iUsage = LOWORD(logbrush.lbColor);
185 emr->offBmi = sizeof(EMRCREATEDIBPATTERNBRUSHPT);
187 emr->offBits = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize;
188 emr->cbBits = bmSize;
189 memcpy((char *)emr + sizeof(EMRCREATEDIBPATTERNBRUSHPT), info,
192 if(!EMFDRV_WriteRecord( dev, &emr->emr ))
194 HeapFree( GetProcessHeap(), 0, emr );
195 GlobalUnlock16(logbrush.lbHatch);
201 EMRCREATEDIBPATTERNBRUSHPT *emr;
202 BITMAPINFOHEADER *info;
204 DWORD bmSize, biSize, size;
206 GetObjectA((HANDLE)logbrush.lbHatch, sizeof(bm), &bm);
208 if (bm.bmBitsPixel != 1 || bm.bmPlanes != 1)
210 FIXME("Trying to create a color pattern brush\n");
214 /* BMP will be aligned to 32 bits, not 16 */
215 bmSize = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, bm.bmBitsPixel);
217 biSize = sizeof(BITMAPINFOHEADER);
218 /* FIXME: There is an extra DWORD written by native before the BMI.
219 * Not sure what its meant to contain.
221 size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize + sizeof(DWORD);
223 emr = HeapAlloc( GetProcessHeap(), 0, size );
227 info = (BITMAPINFOHEADER *)((LPBYTE)emr +
228 sizeof(EMRCREATEDIBPATTERNBRUSHPT) + sizeof(DWORD));
229 info->biSize = sizeof(BITMAPINFOHEADER);
230 info->biWidth = bm.bmWidth;
231 info->biHeight = bm.bmHeight;
232 info->biPlanes = bm.bmPlanes;
233 info->biBitCount = bm.bmBitsPixel;
234 info->biSizeImage = bmSize;
235 GetBitmapBits((HANDLE)logbrush.lbHatch,
236 bm.bmHeight * BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel),
237 (LPBYTE)info + sizeof(BITMAPINFOHEADER));
239 /* Change the padding to be DIB compatible if needed */
241 EMFDRV_PadTo32((LPBYTE)info + sizeof(BITMAPINFOHEADER), bm.bmWidth, bm.bmHeight);
243 emr->emr.iType = EMR_CREATEMONOBRUSH;
244 emr->emr.nSize = size;
245 emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
246 /* Presumably to reduce the size of the written EMF, MS supports an
247 * undocumented iUsage value of 2, indicating a mono bitmap without the
248 * 8 byte 2 entry black/white palette. Stupidly, they could have saved
249 * over 20 bytes more by also ignoring the BITMAPINFO fields that are
250 * irrelevant/constant for monochrome bitmaps.
251 * FIXME: It may be that the DIB functions themselves accept this value.
253 emr->iUsage = DIB_PAL_MONO;
254 emr->offBmi = (LPBYTE)info - (LPBYTE)emr;
256 emr->offBits = emr->offBmi + biSize;
257 emr->cbBits = bmSize;
259 if(!EMFDRV_WriteRecord( dev, &emr->emr ))
261 HeapFree( GetProcessHeap(), 0, emr );
266 FIXME("Unknown style %x\n", logbrush.lbStyle);
273 /***********************************************************************
276 HBRUSH CDECL EMFDRV_SelectBrush(PHYSDEV dev, HBRUSH hBrush )
278 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
283 if (physDev->restoring) return hBrush; /* don't output SelectObject records during RestoreDC */
285 /* If the object is a stock brush object, do not need to create it.
286 * See definitions in wingdi.h for range of stock brushes.
287 * We do however have to handle setting the higher order bit to
288 * designate that this is a stock object.
290 for (i = WHITE_BRUSH; i <= NULL_BRUSH; i++)
292 if (hBrush == GetStockObject(i))
294 index = i | 0x80000000;
298 if((index = EMFDRV_FindObject(dev, hBrush)) != 0)
301 if (!(index = EMFDRV_CreateBrushIndirect(dev, hBrush ))) return 0;
302 GDI_hdc_using_object(hBrush, physDev->hdc);
305 emr.emr.iType = EMR_SELECTOBJECT;
306 emr.emr.nSize = sizeof(emr);
307 emr.ihObject = index;
308 return EMFDRV_WriteRecord( dev, &emr.emr ) ? hBrush : 0;
312 /******************************************************************
313 * EMFDRV_CreateFontIndirect
315 static BOOL EMFDRV_CreateFontIndirect(PHYSDEV dev, HFONT hFont )
318 EMREXTCREATEFONTINDIRECTW emr;
321 if (!GetObjectW( hFont, sizeof(emr.elfw.elfLogFont), &emr.elfw.elfLogFont )) return 0;
323 emr.emr.iType = EMR_EXTCREATEFONTINDIRECTW;
324 emr.emr.nSize = (sizeof(emr) + 3) / 4 * 4;
325 emr.ihFont = index = EMFDRV_AddHandle( dev, hFont );
326 emr.elfw.elfFullName[0] = '\0';
327 emr.elfw.elfStyle[0] = '\0';
328 emr.elfw.elfVersion = 0;
329 emr.elfw.elfStyleSize = 0;
330 emr.elfw.elfMatch = 0;
331 emr.elfw.elfReserved = 0;
332 for(i = 0; i < ELF_VENDOR_SIZE; i++)
333 emr.elfw.elfVendorId[i] = 0;
334 emr.elfw.elfCulture = PAN_CULTURE_LATIN;
335 emr.elfw.elfPanose.bFamilyType = PAN_NO_FIT;
336 emr.elfw.elfPanose.bSerifStyle = PAN_NO_FIT;
337 emr.elfw.elfPanose.bWeight = PAN_NO_FIT;
338 emr.elfw.elfPanose.bProportion = PAN_NO_FIT;
339 emr.elfw.elfPanose.bContrast = PAN_NO_FIT;
340 emr.elfw.elfPanose.bStrokeVariation = PAN_NO_FIT;
341 emr.elfw.elfPanose.bArmStyle = PAN_NO_FIT;
342 emr.elfw.elfPanose.bLetterform = PAN_NO_FIT;
343 emr.elfw.elfPanose.bMidline = PAN_NO_FIT;
344 emr.elfw.elfPanose.bXHeight = PAN_NO_FIT;
346 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
352 /***********************************************************************
355 HFONT CDECL EMFDRV_SelectFont( PHYSDEV dev, HFONT hFont, HANDLE gdiFont )
357 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
362 if (physDev->restoring) return 0; /* don't output SelectObject records during RestoreDC */
364 /* If the object is a stock font object, do not need to create it.
365 * See definitions in wingdi.h for range of stock fonts.
366 * We do however have to handle setting the higher order bit to
367 * designate that this is a stock object.
370 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
372 if (i != DEFAULT_PALETTE && hFont == GetStockObject(i))
374 index = i | 0x80000000;
379 if((index = EMFDRV_FindObject(dev, hFont)) != 0)
382 if (!(index = EMFDRV_CreateFontIndirect(dev, hFont ))) return HGDI_ERROR;
383 GDI_hdc_using_object(hFont, physDev->hdc);
386 emr.emr.iType = EMR_SELECTOBJECT;
387 emr.emr.nSize = sizeof(emr);
388 emr.ihObject = index;
389 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
396 /******************************************************************
397 * EMFDRV_CreatePenIndirect
399 static DWORD EMFDRV_CreatePenIndirect(PHYSDEV dev, HPEN hPen)
404 if (!GetObjectW( hPen, sizeof(emr.lopn), &emr.lopn ))
406 /* must be an extended pen */
408 INT size = GetObjectW( hPen, 0, NULL );
412 elp = HeapAlloc( GetProcessHeap(), 0, size );
414 GetObjectW( hPen, size, elp );
415 /* FIXME: add support for user style pens */
416 emr.lopn.lopnStyle = elp->elpPenStyle;
417 emr.lopn.lopnWidth.x = elp->elpWidth;
418 emr.lopn.lopnWidth.y = 0;
419 emr.lopn.lopnColor = elp->elpColor;
421 HeapFree( GetProcessHeap(), 0, elp );
424 emr.emr.iType = EMR_CREATEPEN;
425 emr.emr.nSize = sizeof(emr);
426 emr.ihPen = index = EMFDRV_AddHandle( dev, hPen );
428 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
433 /******************************************************************
436 HPEN CDECL EMFDRV_SelectPen(PHYSDEV dev, HPEN hPen )
438 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
443 if (physDev->restoring) return hPen; /* don't output SelectObject records during RestoreDC */
445 /* If the object is a stock pen object, do not need to create it.
446 * See definitions in wingdi.h for range of stock pens.
447 * We do however have to handle setting the higher order bit to
448 * designate that this is a stock object.
451 for (i = WHITE_PEN; i <= NULL_PEN; i++)
453 if (hPen == GetStockObject(i))
455 index = i | 0x80000000;
459 if((index = EMFDRV_FindObject(dev, hPen)) != 0)
462 if (!(index = EMFDRV_CreatePenIndirect(dev, hPen))) return 0;
463 GDI_hdc_using_object(hPen, physDev->hdc);
466 emr.emr.iType = EMR_SELECTOBJECT;
467 emr.emr.nSize = sizeof(emr);
468 emr.ihObject = index;
469 return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPen : 0;
473 /******************************************************************
474 * EMFDRV_CreatePalette
476 static DWORD EMFDRV_CreatePalette(PHYSDEV dev, HPALETTE hPal)
480 EMRCREATEPALETTE hdr;
481 PALETTEENTRY entry[255];
484 memset( &pal, 0, sizeof(pal) );
486 if (!GetObjectW( hPal, sizeof(pal.hdr.lgpl) + sizeof(pal.entry), &pal.hdr.lgpl ))
489 for (i = 0; i < pal.hdr.lgpl.palNumEntries; i++)
490 pal.hdr.lgpl.palPalEntry[i].peFlags = 0;
492 pal.hdr.emr.iType = EMR_CREATEPALETTE;
493 pal.hdr.emr.nSize = sizeof(pal.hdr) + pal.hdr.lgpl.palNumEntries * sizeof(PALETTEENTRY);
494 pal.hdr.ihPal = EMFDRV_AddHandle( dev, hPal );
496 if (!EMFDRV_WriteRecord( dev, &pal.hdr.emr ))
498 return pal.hdr.ihPal;
501 /******************************************************************
502 * EMFDRV_SelectPalette
504 HPALETTE CDECL EMFDRV_SelectPalette( PHYSDEV dev, HPALETTE hPal, BOOL force )
506 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
507 EMRSELECTPALETTE emr;
510 if (physDev->restoring) return hPal; /* don't output SelectObject records during RestoreDC */
512 if (hPal == GetStockObject( DEFAULT_PALETTE ))
514 index = DEFAULT_PALETTE | 0x80000000;
518 if ((index = EMFDRV_FindObject( dev, hPal )) != 0)
521 if (!(index = EMFDRV_CreatePalette( dev, hPal ))) return 0;
522 GDI_hdc_using_object( hPal, physDev->hdc );
525 emr.emr.iType = EMR_SELECTPALETTE;
526 emr.emr.nSize = sizeof(emr);
528 return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPal : 0;
532 /******************************************************************
535 BOOL CDECL EMFDRV_GdiComment(PHYSDEV dev, UINT bytes, CONST BYTE *buffer)
538 UINT total, rounded_size;
541 rounded_size = (bytes+3) & ~3;
542 total = offsetof(EMRGDICOMMENT,Data) + rounded_size;
544 emr = HeapAlloc(GetProcessHeap(), 0, total);
545 emr->emr.iType = EMR_GDICOMMENT;
546 emr->emr.nSize = total;
548 memset(&emr->Data[bytes], 0, rounded_size - bytes);
549 memcpy(&emr->Data[0], buffer, bytes);
551 ret = EMFDRV_WriteRecord( dev, &emr->emr );
553 HeapFree(GetProcessHeap(), 0, emr);