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
= bitmap_info_size(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
, LOGFONTW
*logfont
)
359 char buffer
[sizeof(METARECORD
) - 2 + sizeof(LOGFONT16
)];
360 METARECORD
*mr
= (METARECORD
*)&buffer
;
363 mr
->rdSize
= (sizeof(METARECORD
) + sizeof(LOGFONT16
) - 2) / 2;
364 mr
->rdFunction
= META_CREATEFONTINDIRECT
;
365 font16
= (LOGFONT16
*)&mr
->rdParm
;
367 font16
->lfHeight
= logfont
->lfHeight
;
368 font16
->lfWidth
= logfont
->lfWidth
;
369 font16
->lfEscapement
= logfont
->lfEscapement
;
370 font16
->lfOrientation
= logfont
->lfOrientation
;
371 font16
->lfWeight
= logfont
->lfWeight
;
372 font16
->lfItalic
= logfont
->lfItalic
;
373 font16
->lfUnderline
= logfont
->lfUnderline
;
374 font16
->lfStrikeOut
= logfont
->lfStrikeOut
;
375 font16
->lfCharSet
= logfont
->lfCharSet
;
376 font16
->lfOutPrecision
= logfont
->lfOutPrecision
;
377 font16
->lfClipPrecision
= logfont
->lfClipPrecision
;
378 font16
->lfQuality
= logfont
->lfQuality
;
379 font16
->lfPitchAndFamily
= logfont
->lfPitchAndFamily
;
380 WideCharToMultiByte( CP_ACP
, 0, logfont
->lfFaceName
, -1, font16
->lfFaceName
, LF_FACESIZE
, NULL
, NULL
);
381 font16
->lfFaceName
[LF_FACESIZE
-1] = 0;
383 if (!(MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2)))
385 return MFDRV_AddHandle( dev
, hFont
);
389 /***********************************************************************
392 HFONT
MFDRV_SelectFont( PHYSDEV dev
, HFONT hfont
, HANDLE gdiFont
)
394 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
398 index
= MFDRV_FindObject(dev
, hfont
);
401 if (!GetObjectW( hfont
, sizeof(font
), &font
))
403 index
= MFDRV_CreateFontIndirect(dev
, hfont
, &font
);
406 GDI_hdc_using_object(hfont
, physDev
->hdc
);
408 return MFDRV_SelectObject( dev
, index
) ? hfont
: HGDI_ERROR
;
411 /******************************************************************
412 * MFDRV_CreatePenIndirect
414 static UINT16
MFDRV_CreatePenIndirect(PHYSDEV dev
, HPEN hPen
, LOGPEN16
*logpen
)
416 char buffer
[sizeof(METARECORD
) - 2 + sizeof(*logpen
)];
417 METARECORD
*mr
= (METARECORD
*)&buffer
;
419 mr
->rdSize
= (sizeof(METARECORD
) + sizeof(*logpen
) - 2) / 2;
420 mr
->rdFunction
= META_CREATEPENINDIRECT
;
421 memcpy(&(mr
->rdParm
), logpen
, sizeof(*logpen
));
422 if (!(MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* 2)))
424 return MFDRV_AddHandle( dev
, hPen
);
428 /***********************************************************************
431 HPEN
MFDRV_SelectPen( PHYSDEV dev
, HPEN hpen
)
433 METAFILEDRV_PDEVICE
*physDev
= (METAFILEDRV_PDEVICE
*)dev
;
437 index
= MFDRV_FindObject(dev
, hpen
);
440 /* must be an extended pen */
441 INT size
= GetObjectW( hpen
, 0, NULL
);
445 if (size
== sizeof(LOGPEN
))
449 GetObjectW( hpen
, sizeof(pen
), &pen
);
450 logpen
.lopnStyle
= pen
.lopnStyle
;
451 logpen
.lopnWidth
.x
= pen
.lopnWidth
.x
;
452 logpen
.lopnWidth
.y
= pen
.lopnWidth
.y
;
453 logpen
.lopnColor
= pen
.lopnColor
;
455 else /* must be an extended pen */
457 EXTLOGPEN
*elp
= HeapAlloc( GetProcessHeap(), 0, size
);
459 GetObjectW( hpen
, size
, elp
);
460 /* FIXME: add support for user style pens */
461 logpen
.lopnStyle
= elp
->elpPenStyle
;
462 logpen
.lopnWidth
.x
= elp
->elpWidth
;
463 logpen
.lopnWidth
.y
= 0;
464 logpen
.lopnColor
= elp
->elpColor
;
466 HeapFree( GetProcessHeap(), 0, elp
);
469 index
= MFDRV_CreatePenIndirect( dev
, hpen
, &logpen
);
472 GDI_hdc_using_object(hpen
, physDev
->hdc
);
474 return MFDRV_SelectObject( dev
, index
) ? hpen
: HGDI_ERROR
;
478 /******************************************************************
479 * MFDRV_CreatePalette
481 static BOOL
MFDRV_CreatePalette(PHYSDEV dev
, HPALETTE hPalette
, LOGPALETTE
* logPalette
, int sizeofPalette
)
487 mr
= HeapAlloc( GetProcessHeap(), 0, sizeof(METARECORD
) + sizeofPalette
- sizeof(WORD
) );
488 mr
->rdSize
= (sizeof(METARECORD
) + sizeofPalette
- sizeof(WORD
)) / sizeof(WORD
);
489 mr
->rdFunction
= META_CREATEPALETTE
;
490 memcpy(&(mr
->rdParm
), logPalette
, sizeofPalette
);
491 if (!(MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* sizeof(WORD
))))
493 HeapFree(GetProcessHeap(), 0, mr
);
497 mr
->rdSize
= sizeof(METARECORD
) / sizeof(WORD
);
498 mr
->rdFunction
= META_SELECTPALETTE
;
500 if ((index
= MFDRV_AddHandle( dev
, hPalette
)) == -1) ret
= FALSE
;
503 *(mr
->rdParm
) = index
;
504 ret
= MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* sizeof(WORD
));
506 HeapFree(GetProcessHeap(), 0, mr
);
511 /***********************************************************************
512 * MFDRV_SelectPalette
514 HPALETTE
MFDRV_SelectPalette( PHYSDEV dev
, HPALETTE hPalette
, BOOL bForceBackground
)
516 #define PALVERSION 0x0300
518 PLOGPALETTE logPalette
;
519 WORD wNumEntries
= 0;
520 BOOL creationSucceed
;
523 GetObjectA(hPalette
, sizeof(WORD
), (LPSTR
) &wNumEntries
);
525 if (wNumEntries
== 0) return 0;
527 sizeofPalette
= sizeof(LOGPALETTE
) + ((wNumEntries
-1) * sizeof(PALETTEENTRY
));
528 logPalette
= HeapAlloc( GetProcessHeap(), 0, sizeofPalette
);
530 if (logPalette
== NULL
) return 0;
532 logPalette
->palVersion
= PALVERSION
;
533 logPalette
->palNumEntries
= wNumEntries
;
535 GetPaletteEntries(hPalette
, 0, wNumEntries
, logPalette
->palPalEntry
);
537 creationSucceed
= MFDRV_CreatePalette( dev
, hPalette
, logPalette
, sizeofPalette
);
539 HeapFree( GetProcessHeap(), 0, logPalette
);
547 /***********************************************************************
548 * MFDRV_RealizePalette
550 UINT
MFDRV_RealizePalette(PHYSDEV dev
, HPALETTE hPalette
, BOOL dummy
)
552 char buffer
[sizeof(METARECORD
) - sizeof(WORD
)];
553 METARECORD
*mr
= (METARECORD
*)&buffer
;
555 mr
->rdSize
= (sizeof(METARECORD
) - sizeof(WORD
)) / sizeof(WORD
);
556 mr
->rdFunction
= META_REALIZEPALETTE
;
558 if (!(MFDRV_WriteRecord( dev
, mr
, mr
->rdSize
* sizeof(WORD
)))) return 0;
560 /* The return value is suppose to be the number of entries
561 in the logical palette mapped to the system palette or 0
562 if the function failed. Since it's not trivial here to
563 get that kind of information and since it's of little
564 use in the case of metafiles, we'll always return 1. */