4 * Copyright 1993 Alexandre Julliard
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
30 #include "mfdrv/metafiledrv.h"
31 #include "gdi_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
36 /******************************************************************
39 UINT MFDRV_AddHandle( PHYSDEV dev, HGDIOBJ obj )
41 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
44 for(index = 0; index < physDev->handles_size; index++)
45 if(physDev->handles[index] == 0) break;
46 if(index == physDev->handles_size) {
47 physDev->handles_size += HANDLE_LIST_INC;
48 physDev->handles = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
50 physDev->handles_size * sizeof(physDev->handles[0]));
52 physDev->handles[index] = obj;
54 physDev->cur_handles++;
55 if(physDev->cur_handles > physDev->mh->mtNoObjects)
56 physDev->mh->mtNoObjects++;
58 return index ; /* index 0 is not reserved for metafiles */
61 /******************************************************************
64 BOOL MFDRV_RemoveHandle( PHYSDEV dev, UINT index )
66 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
69 if (index < physDev->handles_size && physDev->handles[index])
71 physDev->handles[index] = 0;
72 physDev->cur_handles--;
78 /******************************************************************
81 static INT16 MFDRV_FindObject( PHYSDEV dev, HGDIOBJ obj )
83 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
86 for(index = 0; index < physDev->handles_size; index++)
87 if(physDev->handles[index] == obj) break;
89 if(index == physDev->handles_size) return -1;
95 /******************************************************************
98 BOOL MFDRV_DeleteObject( PHYSDEV dev, HGDIOBJ obj )
101 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
105 index = MFDRV_FindObject(dev, obj);
109 mr.rdSize = sizeof mr / 2;
110 mr.rdFunction = META_DELETEOBJECT;
111 mr.rdParm[0] = index;
113 if(!MFDRV_WriteRecord( dev, &mr, mr.rdSize*2 ))
116 physDev->handles[index] = 0;
117 physDev->cur_handles--;
122 /***********************************************************************
125 static BOOL MFDRV_SelectObject( PHYSDEV dev, INT16 index)
129 mr.rdSize = sizeof mr / 2;
130 mr.rdFunction = META_SELECTOBJECT;
131 mr.rdParm[0] = index;
133 return MFDRV_WriteRecord( dev, &mr, mr.rdSize*2 );
137 /***********************************************************************
140 HBITMAP MFDRV_SelectBitmap( PHYSDEV dev, HBITMAP hbitmap )
145 /***********************************************************************
146 * Internal helper for MFDRV_CreateBrushIndirect():
147 * Change the padding of a bitmap from 16 (BMP) to 32 (DIB) bits.
149 static inline void MFDRV_PadTo32(LPBYTE lpRows, int height, int width)
151 int bytes16 = 2 * ((width + 15) / 16);
152 int bytes32 = 4 * ((width + 31) / 32);
159 height = abs(height) - 1;
160 lpSrc = lpRows + height * bytes16;
161 lpDst = lpRows + height * bytes32;
163 /* Note that we work backwards so we can re-pad in place */
166 for (i = bytes32; i > bytes16; i--)
167 lpDst[i - 1] = 0; /* Zero the padding bytes */
169 lpDst[i - 1] = lpSrc[i - 1]; /* Move image bytes into alignment */
176 /***********************************************************************
177 * Internal helper for MFDRV_CreateBrushIndirect():
178 * Reverse order of bitmap rows in going from BMP to DIB.
180 static inline void MFDRV_Reverse(LPBYTE lpRows, int height, int width)
182 int bytes = 4 * ((width + 31) / 32);
191 lpDst = lpRows + (height-1) * bytes;
196 for (i = 0; i < bytes; i++)
208 /******************************************************************
209 * MFDRV_CreateBrushIndirect
212 INT16 MFDRV_CreateBrushIndirect(PHYSDEV dev, HBRUSH hBrush )
217 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
220 if (!GetObjectA( hBrush, sizeof(logbrush), &logbrush )) return -1;
222 switch(logbrush.lbStyle)
230 lb16.lbStyle = logbrush.lbStyle;
231 lb16.lbColor = logbrush.lbColor;
232 lb16.lbHatch = logbrush.lbHatch;
233 size = sizeof(METARECORD) + sizeof(LOGBRUSH16) - 2;
234 mr = HeapAlloc( GetProcessHeap(), 0, size );
235 mr->rdSize = size / 2;
236 mr->rdFunction = META_CREATEBRUSHINDIRECT;
237 memcpy( mr->rdParm, &lb16, sizeof(LOGBRUSH16));
247 GetObjectA((HANDLE)logbrush.lbHatch, sizeof(bm), &bm);
248 if(bm.bmBitsPixel != 1 || bm.bmPlanes != 1) {
249 FIXME("Trying to store a colour pattern brush\n");
253 bmSize = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, DIB_PAL_COLORS);
255 size = sizeof(METARECORD) + sizeof(WORD) + sizeof(BITMAPINFO) +
256 sizeof(RGBQUAD) + bmSize;
258 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
260 mr->rdFunction = META_DIBCREATEPATTERNBRUSH;
261 mr->rdSize = size / 2;
262 mr->rdParm[0] = BS_PATTERN;
263 mr->rdParm[1] = DIB_RGB_COLORS;
264 info = (BITMAPINFO *)(mr->rdParm + 2);
266 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
267 info->bmiHeader.biWidth = bm.bmWidth;
268 info->bmiHeader.biHeight = bm.bmHeight;
269 info->bmiHeader.biPlanes = 1;
270 info->bmiHeader.biBitCount = 1;
271 info->bmiHeader.biSizeImage = bmSize;
273 GetBitmapBits((HANDLE)logbrush.lbHatch,
274 bm.bmHeight * BITMAP_GetWidthBytes (bm.bmWidth, bm.bmBitsPixel),
275 (LPBYTE)info + sizeof(BITMAPINFO) + sizeof(RGBQUAD));
277 /* Change the padding to be DIB compatible if needed */
279 MFDRV_PadTo32((LPBYTE)info + sizeof(BITMAPINFO) + sizeof(RGBQUAD),
280 bm.bmWidth, bm.bmHeight);
281 /* BMP and DIB have opposite row order conventions */
282 MFDRV_Reverse((LPBYTE)info + sizeof(BITMAPINFO) + sizeof(RGBQUAD),
283 bm.bmWidth, bm.bmHeight);
285 cref = GetTextColor(physDev->hdc);
286 info->bmiColors[0].rgbRed = GetRValue(cref);
287 info->bmiColors[0].rgbGreen = GetGValue(cref);
288 info->bmiColors[0].rgbBlue = GetBValue(cref);
289 info->bmiColors[0].rgbReserved = 0;
290 cref = GetBkColor(physDev->hdc);
291 info->bmiColors[1].rgbRed = GetRValue(cref);
292 info->bmiColors[1].rgbGreen = GetGValue(cref);
293 info->bmiColors[1].rgbBlue = GetBValue(cref);
294 info->bmiColors[1].rgbReserved = 0;
301 DWORD bmSize, biSize;
303 info = GlobalLock16((HGLOBAL16)logbrush.lbHatch);
304 if (info->bmiHeader.biCompression)
305 bmSize = info->bmiHeader.biSizeImage;
307 bmSize = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
308 info->bmiHeader.biHeight,
309 info->bmiHeader.biBitCount);
310 biSize = DIB_BitmapInfoSize(info, LOWORD(logbrush.lbColor));
311 size = sizeof(METARECORD) + biSize + bmSize + 2;
312 mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
314 mr->rdFunction = META_DIBCREATEPATTERNBRUSH;
315 mr->rdSize = size / 2;
316 *(mr->rdParm) = logbrush.lbStyle;
317 *(mr->rdParm + 1) = LOWORD(logbrush.lbColor);
318 memcpy(mr->rdParm + 2, info, biSize + bmSize);
322 FIXME("Unkonwn brush style %x\n", logbrush.lbStyle);
325 r = MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
326 HeapFree(GetProcessHeap(), 0, mr);
330 return MFDRV_AddHandle( dev, hBrush );
334 /***********************************************************************
337 HBRUSH MFDRV_SelectBrush( PHYSDEV dev, HBRUSH hbrush )
339 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
342 index = MFDRV_FindObject(dev, hbrush);
345 index = MFDRV_CreateBrushIndirect( dev, hbrush );
348 GDI_hdc_using_object(hbrush, physDev->hdc);
350 return MFDRV_SelectObject( dev, index ) ? hbrush : HGDI_ERROR;
353 /******************************************************************
354 * MFDRV_CreateFontIndirect
357 static UINT16 MFDRV_CreateFontIndirect(PHYSDEV dev, HFONT hFont, LOGFONT16 *logfont)
359 char buffer[sizeof(METARECORD) - 2 + sizeof(LOGFONT16)];
360 METARECORD *mr = (METARECORD *)&buffer;
362 mr->rdSize = (sizeof(METARECORD) + sizeof(LOGFONT16) - 2) / 2;
363 mr->rdFunction = META_CREATEFONTINDIRECT;
364 memcpy(&(mr->rdParm), logfont, sizeof(LOGFONT16));
365 if (!(MFDRV_WriteRecord( dev, mr, mr->rdSize * 2)))
367 return MFDRV_AddHandle( dev, hFont );
371 /***********************************************************************
374 HFONT MFDRV_SelectFont( PHYSDEV dev, HFONT hfont, HANDLE gdiFont )
376 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
380 index = MFDRV_FindObject(dev, hfont);
383 if (!GetObject16( HFONT_16(hfont), sizeof(lf16), &lf16 ))
385 index = MFDRV_CreateFontIndirect(dev, hfont, &lf16);
388 GDI_hdc_using_object(hfont, physDev->hdc);
390 return MFDRV_SelectObject( dev, index ) ? hfont : HGDI_ERROR;
393 /******************************************************************
394 * MFDRV_CreatePenIndirect
396 static UINT16 MFDRV_CreatePenIndirect(PHYSDEV dev, HPEN hPen, LOGPEN16 *logpen)
398 char buffer[sizeof(METARECORD) - 2 + sizeof(*logpen)];
399 METARECORD *mr = (METARECORD *)&buffer;
401 mr->rdSize = (sizeof(METARECORD) + sizeof(*logpen) - 2) / 2;
402 mr->rdFunction = META_CREATEPENINDIRECT;
403 memcpy(&(mr->rdParm), logpen, sizeof(*logpen));
404 if (!(MFDRV_WriteRecord( dev, mr, mr->rdSize * 2)))
406 return MFDRV_AddHandle( dev, hPen );
410 /***********************************************************************
413 HPEN MFDRV_SelectPen( PHYSDEV dev, HPEN hpen )
415 METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
419 index = MFDRV_FindObject(dev, hpen);
422 if (!GetObject16( HPEN_16(hpen), sizeof(logpen), &logpen ))
424 /* must be an extended pen */
426 INT size = GetObjectW( hpen, 0, NULL );
430 elp = HeapAlloc( GetProcessHeap(), 0, size );
432 GetObjectW( hpen, size, elp );
433 /* FIXME: add support for user style pens */
434 logpen.lopnStyle = elp->elpPenStyle;
435 logpen.lopnWidth.x = elp->elpWidth;
436 logpen.lopnWidth.y = 0;
437 logpen.lopnColor = elp->elpColor;
439 HeapFree( GetProcessHeap(), 0, elp );
442 index = MFDRV_CreatePenIndirect( dev, hpen, &logpen );
445 GDI_hdc_using_object(hpen, physDev->hdc);
447 return MFDRV_SelectObject( dev, index ) ? hpen : HGDI_ERROR;
451 /******************************************************************
452 * MFDRV_CreatePalette
454 static BOOL MFDRV_CreatePalette(PHYSDEV dev, HPALETTE hPalette, LOGPALETTE* logPalette, int sizeofPalette)
460 mr = HeapAlloc( GetProcessHeap(), 0, sizeof(METARECORD) + sizeofPalette - sizeof(WORD) );
461 mr->rdSize = (sizeof(METARECORD) + sizeofPalette - sizeof(WORD)) / sizeof(WORD);
462 mr->rdFunction = META_CREATEPALETTE;
463 memcpy(&(mr->rdParm), logPalette, sizeofPalette);
464 if (!(MFDRV_WriteRecord( dev, mr, mr->rdSize * sizeof(WORD))))
466 HeapFree(GetProcessHeap(), 0, mr);
470 mr->rdSize = sizeof(METARECORD) / sizeof(WORD);
471 mr->rdFunction = META_SELECTPALETTE;
473 if ((index = MFDRV_AddHandle( dev, hPalette )) == -1) ret = FALSE;
476 *(mr->rdParm) = index;
477 ret = MFDRV_WriteRecord( dev, mr, mr->rdSize * sizeof(WORD));
479 HeapFree(GetProcessHeap(), 0, mr);
484 /***********************************************************************
485 * MFDRV_SelectPalette
487 HPALETTE MFDRV_SelectPalette( PHYSDEV dev, HPALETTE hPalette, BOOL bForceBackground )
489 #define PALVERSION 0x0300
491 PLOGPALETTE logPalette;
492 WORD wNumEntries = 0;
493 BOOL creationSucceed;
496 GetObjectA(hPalette, sizeof(WORD), (LPSTR) &wNumEntries);
498 if (wNumEntries == 0) return 0;
500 sizeofPalette = sizeof(LOGPALETTE) + ((wNumEntries-1) * sizeof(PALETTEENTRY));
501 logPalette = HeapAlloc( GetProcessHeap(), 0, sizeofPalette );
503 if (logPalette == NULL) return 0;
505 logPalette->palVersion = PALVERSION;
506 logPalette->palNumEntries = wNumEntries;
508 GetPaletteEntries(hPalette, 0, wNumEntries, logPalette->palPalEntry);
510 creationSucceed = MFDRV_CreatePalette( dev, hPalette, logPalette, sizeofPalette );
512 HeapFree( GetProcessHeap(), 0, logPalette );
520 /***********************************************************************
521 * MFDRV_RealizePalette
523 UINT MFDRV_RealizePalette(PHYSDEV dev, HPALETTE hPalette, BOOL dummy)
525 char buffer[sizeof(METARECORD) - sizeof(WORD)];
526 METARECORD *mr = (METARECORD *)&buffer;
528 mr->rdSize = (sizeof(METARECORD) - sizeof(WORD)) / sizeof(WORD);
529 mr->rdFunction = META_REALIZEPALETTE;
531 if (!(MFDRV_WriteRecord( dev, mr, mr->rdSize * sizeof(WORD)))) return 0;
533 /* The return value is suppose to be the number of entries
534 in the logical palette mapped to the system palette or 0
535 if the function failed. Since it's not trivial here to
536 get that kind of information and since it's of little
537 use in the case of metafiles, we'll always return 1. */