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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "enhmfdrv/enhmetafiledrv.h"
27 #include "gdi_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
33 /******************************************************************
36 static UINT EMFDRV_AddHandle( PHYSDEV dev, HGDIOBJ obj )
38 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
41 for(index = 0; index < physDev->handles_size; index++)
42 if(physDev->handles[index] == 0) break;
43 if(index == physDev->handles_size) {
44 physDev->handles_size += HANDLE_LIST_INC;
45 physDev->handles = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
47 physDev->handles_size * sizeof(physDev->handles[0]));
49 physDev->handles[index] = obj;
51 physDev->cur_handles++;
52 if(physDev->cur_handles > physDev->emh->nHandles)
53 physDev->emh->nHandles++;
55 return index + 1; /* index 0 is reserved for the hmf, so we increment everything by 1 */
58 /******************************************************************
61 static UINT EMFDRV_FindObject( PHYSDEV dev, HGDIOBJ obj )
63 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
66 for(index = 0; index < physDev->handles_size; index++)
67 if(physDev->handles[index] == obj) break;
69 if(index == physDev->handles_size) return 0;
75 /******************************************************************
78 BOOL EMFDRV_DeleteObject( PHYSDEV dev, HGDIOBJ obj )
81 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*) dev;
85 if(!(index = EMFDRV_FindObject(dev, obj))) return 0;
87 emr.emr.iType = EMR_DELETEOBJECT;
88 emr.emr.nSize = sizeof(emr);
91 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
94 physDev->handles[index - 1] = 0;
95 physDev->cur_handles--;
100 /***********************************************************************
101 * EMFDRV_SelectBitmap
103 HBITMAP EMFDRV_SelectBitmap( PHYSDEV dev, HBITMAP hbitmap )
109 /* Internal helper for EMFDRV_CreateBrushIndirect():
110 * Change the padding of a bitmap from 16 (BMP) to 32 (DIB) bits.
112 static inline void EMFDRV_PadTo32(LPBYTE lpRows, int height, int width)
114 int bytes16 = 2 * ((width + 15) / 16);
115 int bytes32 = 4 * ((width + 31) / 32);
122 height = abs(height) - 1;
123 lpSrc = lpRows + height * bytes16;
124 lpDst = lpRows + height * bytes32;
126 /* Note that we work backwards so we can re-pad in place */
129 for (i = bytes32; i > bytes16; i--)
130 lpDst[i - 1] = 0; /* Zero the padding bytes */
132 lpDst[i - 1] = lpSrc[i - 1]; /* Move image bytes into alignment */
139 /***********************************************************************
140 * EMFDRV_CreateBrushIndirect
142 DWORD EMFDRV_CreateBrushIndirect( PHYSDEV dev, HBRUSH hBrush )
147 if (!GetObjectA( hBrush, sizeof(logbrush), &logbrush )) return 0;
149 switch (logbrush.lbStyle) {
154 EMRCREATEBRUSHINDIRECT emr;
155 emr.emr.iType = EMR_CREATEBRUSHINDIRECT;
156 emr.emr.nSize = sizeof(emr);
157 emr.ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
160 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
166 EMRCREATEDIBPATTERNBRUSHPT *emr;
167 DWORD bmSize, biSize, size;
168 BITMAPINFO *info = GlobalLock16(logbrush.lbHatch);
170 if (info->bmiHeader.biCompression)
171 bmSize = info->bmiHeader.biSizeImage;
173 bmSize = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
174 info->bmiHeader.biHeight,
175 info->bmiHeader.biBitCount);
176 biSize = DIB_BitmapInfoSize(info, LOWORD(logbrush.lbColor));
177 size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize;
178 emr = HeapAlloc( GetProcessHeap(), 0, size );
180 emr->emr.iType = EMR_CREATEDIBPATTERNBRUSHPT;
181 emr->emr.nSize = size;
182 emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
183 emr->iUsage = LOWORD(logbrush.lbColor);
184 emr->offBmi = sizeof(EMRCREATEDIBPATTERNBRUSHPT);
186 emr->offBits = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize;
187 emr->cbBits = bmSize;
188 memcpy((char *)emr + sizeof(EMRCREATEDIBPATTERNBRUSHPT), info,
191 if(!EMFDRV_WriteRecord( dev, &emr->emr ))
193 HeapFree( GetProcessHeap(), 0, emr );
194 GlobalUnlock16(logbrush.lbHatch);
200 EMRCREATEDIBPATTERNBRUSHPT *emr;
201 BITMAPINFOHEADER *info;
203 DWORD bmSize, biSize, size;
205 GetObjectA((HANDLE)logbrush.lbHatch, sizeof(bm), &bm);
207 if (bm.bmBitsPixel != 1 || bm.bmPlanes != 1)
209 FIXME("Trying to create a color pattern brush\n");
213 /* BMP will be aligned to 32 bits, not 16 */
214 bmSize = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, bm.bmBitsPixel);
216 biSize = sizeof(BITMAPINFOHEADER);
217 /* FIXME: There is an extra DWORD written by native before the BMI.
218 * Not sure what its meant to contain.
220 size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize + sizeof(DWORD);
222 emr = HeapAlloc( GetProcessHeap(), 0, size );
226 info = (BITMAPINFOHEADER *)((LPBYTE)emr +
227 sizeof(EMRCREATEDIBPATTERNBRUSHPT) + sizeof(DWORD));
228 info->biSize = sizeof(BITMAPINFOHEADER);
229 info->biWidth = bm.bmWidth;
230 info->biHeight = bm.bmHeight;
231 info->biPlanes = bm.bmPlanes;
232 info->biBitCount = bm.bmBitsPixel;
233 info->biSizeImage = bmSize;
234 GetBitmapBits((HANDLE)logbrush.lbHatch,
235 bm.bmHeight * BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel),
236 (LPBYTE)info + sizeof(BITMAPINFOHEADER));
238 /* Change the padding to be DIB compatible if needed */
240 EMFDRV_PadTo32((LPBYTE)info + sizeof(BITMAPINFOHEADER), bm.bmWidth, bm.bmHeight);
242 emr->emr.iType = EMR_CREATEMONOBRUSH;
243 emr->emr.nSize = size;
244 emr->ihBrush = index = EMFDRV_AddHandle( dev, hBrush );
245 /* Presumably to reduce the size of the written EMF, MS supports an
246 * undocumented iUsage value of 2, indicating a mono bitmap without the
247 * 8 byte 2 entry black/white palette. Stupidly, they could have saved
248 * over 20 bytes more by also ignoring the BITMAPINFO fields that are
249 * irrelevant/constant for monochrome bitmaps.
250 * FIXME: It may be that the DIB functions themselves accept this value.
252 emr->iUsage = DIB_PAL_MONO;
253 emr->offBmi = (LPBYTE)info - (LPBYTE)emr;
255 emr->offBits = emr->offBmi + biSize;
256 emr->cbBits = bmSize;
258 if(!EMFDRV_WriteRecord( dev, &emr->emr ))
260 HeapFree( GetProcessHeap(), 0, emr );
265 FIXME("Unknown style %x\n", logbrush.lbStyle);
272 /***********************************************************************
275 HBRUSH EMFDRV_SelectBrush(PHYSDEV dev, HBRUSH hBrush )
277 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
282 /* If the object is a stock brush object, do not need to create it.
283 * See definitions in wingdi.h for range of stock brushes.
284 * We do however have to handle setting the higher order bit to
285 * designate that this is a stock object.
287 for (i = WHITE_BRUSH; i <= NULL_BRUSH; i++)
289 if (hBrush == GetStockObject(i))
291 index = i | 0x80000000;
295 if((index = EMFDRV_FindObject(dev, hBrush)) != 0)
298 if (!(index = EMFDRV_CreateBrushIndirect(dev, hBrush ))) return 0;
299 GDI_hdc_using_object(hBrush, physDev->hdc);
302 emr.emr.iType = EMR_SELECTOBJECT;
303 emr.emr.nSize = sizeof(emr);
304 emr.ihObject = index;
305 return EMFDRV_WriteRecord( dev, &emr.emr ) ? hBrush : 0;
309 /******************************************************************
310 * EMFDRV_CreateFontIndirect
312 static BOOL EMFDRV_CreateFontIndirect(PHYSDEV dev, HFONT hFont )
315 EMREXTCREATEFONTINDIRECTW emr;
318 if (!GetObjectW( hFont, sizeof(emr.elfw.elfLogFont), &emr.elfw.elfLogFont )) return 0;
320 emr.emr.iType = EMR_EXTCREATEFONTINDIRECTW;
321 emr.emr.nSize = (sizeof(emr) + 3) / 4 * 4;
322 emr.ihFont = index = EMFDRV_AddHandle( dev, hFont );
323 emr.elfw.elfFullName[0] = '\0';
324 emr.elfw.elfStyle[0] = '\0';
325 emr.elfw.elfVersion = 0;
326 emr.elfw.elfStyleSize = 0;
327 emr.elfw.elfMatch = 0;
328 emr.elfw.elfReserved = 0;
329 for(i = 0; i < ELF_VENDOR_SIZE; i++)
330 emr.elfw.elfVendorId[i] = 0;
331 emr.elfw.elfCulture = PAN_CULTURE_LATIN;
332 emr.elfw.elfPanose.bFamilyType = PAN_NO_FIT;
333 emr.elfw.elfPanose.bSerifStyle = PAN_NO_FIT;
334 emr.elfw.elfPanose.bWeight = PAN_NO_FIT;
335 emr.elfw.elfPanose.bProportion = PAN_NO_FIT;
336 emr.elfw.elfPanose.bContrast = PAN_NO_FIT;
337 emr.elfw.elfPanose.bStrokeVariation = PAN_NO_FIT;
338 emr.elfw.elfPanose.bArmStyle = PAN_NO_FIT;
339 emr.elfw.elfPanose.bLetterform = PAN_NO_FIT;
340 emr.elfw.elfPanose.bMidline = PAN_NO_FIT;
341 emr.elfw.elfPanose.bXHeight = PAN_NO_FIT;
343 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
349 /***********************************************************************
352 HFONT EMFDRV_SelectFont( PHYSDEV dev, HFONT hFont, HANDLE gdiFont )
354 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
359 /* If the object is a stock font object, do not need to create it.
360 * See definitions in wingdi.h for range of stock fonts.
361 * We do however have to handle setting the higher order bit to
362 * designate that this is a stock object.
365 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
367 if (i != DEFAULT_PALETTE && hFont == GetStockObject(i))
369 index = i | 0x80000000;
374 if((index = EMFDRV_FindObject(dev, hFont)) != 0)
377 if (!(index = EMFDRV_CreateFontIndirect(dev, hFont ))) return HGDI_ERROR;
378 GDI_hdc_using_object(hFont, physDev->hdc);
381 emr.emr.iType = EMR_SELECTOBJECT;
382 emr.emr.nSize = sizeof(emr);
383 emr.ihObject = index;
384 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
391 /******************************************************************
392 * EMFDRV_CreatePenIndirect
394 static HPEN EMFDRV_CreatePenIndirect(PHYSDEV dev, HPEN hPen )
399 if (!GetObjectA( hPen, sizeof(emr.lopn), &emr.lopn )) return 0;
401 emr.emr.iType = EMR_CREATEPEN;
402 emr.emr.nSize = sizeof(emr);
403 emr.ihPen = index = EMFDRV_AddHandle( dev, hPen );
405 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
410 /******************************************************************
413 HPEN EMFDRV_SelectPen(PHYSDEV dev, HPEN hPen )
415 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE*)dev;
420 /* If the object is a stock pen object, do not need to create it.
421 * See definitions in wingdi.h for range of stock pens.
422 * We do however have to handle setting the higher order bit to
423 * designate that this is a stock object.
426 for (i = WHITE_PEN; i <= NULL_PEN; i++)
428 if (hPen == GetStockObject(i))
430 index = i | 0x80000000;
434 if((index = EMFDRV_FindObject(dev, hPen)) != 0)
437 if (!(index = (DWORD)EMFDRV_CreatePenIndirect(dev, hPen ))) return 0;
438 GDI_hdc_using_object(hPen, physDev->hdc);
441 emr.emr.iType = EMR_SELECTOBJECT;
442 emr.emr.nSize = sizeof(emr);
443 emr.ihObject = index;
444 return EMFDRV_WriteRecord( dev, &emr.emr ) ? hPen : 0;
448 /******************************************************************
451 BOOL EMFDRV_GdiComment(PHYSDEV dev, UINT bytes, CONST BYTE *buffer)
454 UINT total, rounded_size;
457 rounded_size = (bytes+3) & ~3;
458 total = offsetof(EMRGDICOMMENT,Data) + rounded_size;
460 emr = HeapAlloc(GetProcessHeap(), 0, total);
461 emr->emr.iType = EMR_GDICOMMENT;
462 emr->emr.nSize = total;
464 memset(&emr->Data[bytes], 0, rounded_size - bytes);
465 memcpy(&emr->Data[0], buffer, bytes);
467 ret = EMFDRV_WriteRecord( dev, &emr->emr );
469 HeapFree(GetProcessHeap(), 0, emr);