2 * Enhanced MetaFile driver initialisation functions
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
28 #include "gdi_private.h"
29 #include "enhmfdrv/enhmetafiledrv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile
);
34 static const DC_FUNCTIONS EMFDRV_Funcs
=
37 EMFDRV_AbortPath
, /* pAbortPath */
38 NULL
, /* pAlphaBlend */
40 EMFDRV_Arc
, /* pArc */
42 EMFDRV_BeginPath
, /* pBeginPath */
43 EMFDRV_BitBlt
, /* pBitBlt */
44 NULL
, /* pChoosePixelFormat */
45 EMFDRV_Chord
, /* pChord */
46 EMFDRV_CloseFigure
, /* pCloseFigure */
47 NULL
, /* pCreateBitmap */
49 NULL
, /* pCreateDIBSection */
50 NULL
, /* pDeleteBitmap */
52 EMFDRV_DeleteObject
, /* pDeleteObject */
53 NULL
, /* pDescribePixelFormat */
54 NULL
, /* pDeviceCapabilities */
55 EMFDRV_Ellipse
, /* pEllipse */
58 EMFDRV_EndPath
, /* pEndPath */
59 NULL
, /* pEnumDeviceFonts */
60 EMFDRV_ExcludeClipRect
, /* pExcludeClipRect */
61 NULL
, /* pExtDeviceMode */
62 NULL
, /* pExtEscape */
63 EMFDRV_ExtFloodFill
, /* pExtFloodFill */
64 EMFDRV_ExtSelectClipRgn
, /* pExtSelectClipRgn */
65 EMFDRV_ExtTextOut
, /* pExtTextOut */
66 EMFDRV_FillPath
, /* pFillPath */
67 EMFDRV_FillRgn
, /* pFillRgn */
68 EMFDRV_FlattenPath
, /* pFlattenPath */
69 EMFDRV_FrameRgn
, /* pFrameRgn */
70 EMFDRV_GdiComment
, /* pGdiComment */
71 NULL
, /* pGetBitmapBits */
72 NULL
, /* pGetCharWidth */
73 NULL
, /* pGetDCOrgEx */
74 NULL
, /* pGetDIBColorTable */
75 NULL
, /* pGetDIBits */
76 EMFDRV_GetDeviceCaps
, /* pGetDeviceCaps */
77 NULL
, /* pGetDeviceGammaRamp */
78 NULL
, /* pGetNearestColor */
80 NULL
, /* pGetPixelFormat */
81 NULL
, /* pGetSystemPaletteEntries */
82 NULL
, /* pGetTextExtentExPoint */
83 NULL
, /* pGetTextMetrics */
84 EMFDRV_IntersectClipRect
, /* pIntersectClipRect */
85 EMFDRV_InvertRgn
, /* pInvertRgn */
86 EMFDRV_LineTo
, /* pLineTo */
87 EMFDRV_ModifyWorldTransform
, /* pModifyWorldTransform */
88 EMFDRV_MoveTo
, /* pMoveTo */
89 EMFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
90 EMFDRV_OffsetViewportOrg
, /* pOffsetViewportOrg */
91 EMFDRV_OffsetWindowOrg
, /* pOffsetWindowOrg */
92 EMFDRV_PaintRgn
, /* pPaintRgn */
93 EMFDRV_PatBlt
, /* pPatBlt */
94 EMFDRV_Pie
, /* pPie */
95 NULL
, /* pPolyBezier */
96 NULL
, /* pPolyBezierTo */
98 EMFDRV_PolyPolygon
, /* pPolyPolygon */
99 EMFDRV_PolyPolyline
, /* pPolyPolyline */
100 EMFDRV_Polygon
, /* pPolygon */
101 EMFDRV_Polyline
, /* pPolyline */
102 NULL
, /* pPolylineTo */
103 NULL
, /* pRealizeDefaultPalette */
104 NULL
, /* pRealizePalette */
105 EMFDRV_Rectangle
, /* pRectangle */
107 EMFDRV_RestoreDC
, /* pRestoreDC */
108 EMFDRV_RoundRect
, /* pRoundRect */
109 EMFDRV_SaveDC
, /* pSaveDC */
110 EMFDRV_ScaleViewportExt
, /* pScaleViewportExt */
111 EMFDRV_ScaleWindowExt
, /* pScaleWindowExt */
112 EMFDRV_SelectBitmap
, /* pSelectBitmap */
113 EMFDRV_SelectBrush
, /* pSelectBrush */
114 EMFDRV_SelectClipPath
, /* pSelectClipPath */
115 EMFDRV_SelectFont
, /* pSelectFont */
116 NULL
, /* pSelectPalette */
117 EMFDRV_SelectPen
, /* pSelectPen */
118 EMFDRV_SetArcDirection
, /* pSetArcDirection */
119 NULL
, /* pSetBitmapBits */
120 EMFDRV_SetBkColor
, /* pSetBkColor */
121 EMFDRV_SetBkMode
, /* pSetBkMode */
122 NULL
, /* pSetDCBrushColor */
123 NULL
, /* pSetDCOrg */
124 NULL
, /* pSetDCPenColor */
125 NULL
, /* pSetDIBColorTable */
126 NULL
, /* pSetDIBits */
127 EMFDRV_SetDIBitsToDevice
, /* pSetDIBitsToDevice */
128 NULL
, /* pSetDeviceClipping */
129 NULL
, /* pSetDeviceGammaRamp */
130 EMFDRV_SetMapMode
, /* pSetMapMode */
131 EMFDRV_SetMapperFlags
, /* pSetMapperFlags */
132 NULL
, /* pSetPixel */
133 NULL
, /* pSetPixelFormat */
134 EMFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
135 EMFDRV_SetROP2
, /* pSetROP2 */
136 NULL
, /* pSetRelAbs */
137 EMFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
138 EMFDRV_SetTextAlign
, /* pSetTextAlign */
139 NULL
, /* pSetTextCharacterExtra */
140 EMFDRV_SetTextColor
, /* pSetTextColor */
141 EMFDRV_SetTextJustification
, /* pSetTextJustification */
142 EMFDRV_SetViewportExt
, /* pSetViewportExt */
143 EMFDRV_SetViewportOrg
, /* pSetViewportOrg */
144 EMFDRV_SetWindowExt
, /* pSetWindowExt */
145 EMFDRV_SetWindowOrg
, /* pSetWindowOrg */
146 EMFDRV_SetWorldTransform
, /* pSetWorldTransform */
147 NULL
, /* pStartDoc */
148 NULL
, /* pStartPage */
149 EMFDRV_StretchBlt
, /* pStretchBlt */
150 EMFDRV_StretchDIBits
, /* pStretchDIBits */
151 EMFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
152 EMFDRV_StrokePath
, /* pStrokePath */
153 NULL
, /* pSwapBuffers */
154 NULL
, /* pUnrealizePalette */
155 EMFDRV_WidenPath
/* pWidenPath */
159 /**********************************************************************
162 static BOOL
EMFDRV_DeleteDC( PHYSDEV dev
)
164 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
165 DC
*dc
= physDev
->dc
;
168 if (physDev
->emh
) HeapFree( GetProcessHeap(), 0, physDev
->emh
);
169 for(index
= 0; index
< physDev
->handles_size
; index
++)
170 if(physDev
->handles
[index
])
171 GDI_hdc_not_using_object(physDev
->handles
[index
], physDev
->hdc
);
172 HeapFree( GetProcessHeap(), 0, physDev
->handles
);
173 HeapFree( GetProcessHeap(), 0, physDev
);
175 GDI_FreeObject( dc
->hSelf
, dc
);
180 /******************************************************************
183 * Warning: this function can change the pointer to the metafile header.
185 BOOL
EMFDRV_WriteRecord( PHYSDEV dev
, EMR
*emr
)
189 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
191 TRACE("record %d, size %d %s\n",
192 emr
->iType
, emr
->nSize
, physDev
->hFile
? "(to disk)" : "");
194 assert( !(emr
->nSize
& 3) );
196 physDev
->emh
->nBytes
+= emr
->nSize
;
197 physDev
->emh
->nRecords
++;
200 if (!WriteFile(physDev
->hFile
, (char *)emr
, emr
->nSize
, NULL
, NULL
))
203 DWORD nEmfSize
= HeapSize(GetProcessHeap(), 0, physDev
->emh
);
204 len
= physDev
->emh
->nBytes
;
205 if (len
> nEmfSize
) {
206 nEmfSize
+= (nEmfSize
/ 2) + emr
->nSize
;
207 emh
= HeapReAlloc(GetProcessHeap(), 0, physDev
->emh
, nEmfSize
);
208 if (!emh
) return FALSE
;
211 memcpy((CHAR
*)physDev
->emh
+ physDev
->emh
->nBytes
- emr
->nSize
, emr
,
218 /******************************************************************
221 void EMFDRV_UpdateBBox( PHYSDEV dev
, RECTL
*rect
)
223 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
224 RECTL
*bounds
= &physDev
->emh
->rclBounds
;
225 RECTL vportRect
= *rect
;
227 LPtoDP(physDev
->hdc
, (LPPOINT
)&vportRect
, 2);
229 /* The coordinate systems may be mirrored
230 (LPtoDP handles points, not rectangles) */
231 if (vportRect
.left
> vportRect
.right
)
233 LONG temp
= vportRect
.right
;
234 vportRect
.right
= vportRect
.left
;
235 vportRect
.left
= temp
;
237 if (vportRect
.top
> vportRect
.bottom
)
239 LONG temp
= vportRect
.bottom
;
240 vportRect
.bottom
= vportRect
.top
;
241 vportRect
.top
= temp
;
244 if (bounds
->left
> bounds
->right
)
246 /* first bounding rectangle */
251 bounds
->left
= min(bounds
->left
, vportRect
.left
);
252 bounds
->top
= min(bounds
->top
, vportRect
.top
);
253 bounds
->right
= max(bounds
->right
, vportRect
.right
);
254 bounds
->bottom
= max(bounds
->bottom
, vportRect
.bottom
);
258 /**********************************************************************
259 * CreateEnhMetaFileA (GDI32.@)
261 HDC WINAPI
CreateEnhMetaFileA(
262 HDC hdc
, /* [in] optional reference DC */
263 LPCSTR filename
, /* [in] optional filename for disk metafiles */
264 const RECT
*rect
, /* [in] optional bounding rectangle */
265 LPCSTR description
/* [in] optional description */
268 LPWSTR filenameW
= NULL
;
269 LPWSTR descriptionW
= NULL
;
271 DWORD len1
, len2
, total
;
275 total
= MultiByteToWideChar( CP_ACP
, 0, filename
, -1, NULL
, 0 );
276 filenameW
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) );
277 MultiByteToWideChar( CP_ACP
, 0, filename
, -1, filenameW
, total
);
280 len1
= strlen(description
);
281 len2
= strlen(description
+ len1
+ 1);
282 total
= MultiByteToWideChar( CP_ACP
, 0, description
, len1
+ len2
+ 3, NULL
, 0 );
283 descriptionW
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) );
284 MultiByteToWideChar( CP_ACP
, 0, description
, len1
+ len2
+ 3, descriptionW
, total
);
287 hReturnDC
= CreateEnhMetaFileW(hdc
, filenameW
, rect
, descriptionW
);
290 HeapFree( GetProcessHeap(), 0, filenameW
);
292 HeapFree( GetProcessHeap(), 0, descriptionW
);
297 /**********************************************************************
298 * CreateEnhMetaFileW (GDI32.@)
300 HDC WINAPI
CreateEnhMetaFileW(
301 HDC hdc
, /* [in] optional reference DC */
302 LPCWSTR filename
, /* [in] optional filename for disk metafiles */
303 const RECT
* rect
, /* [in] optional bounding rectangle */
304 LPCWSTR description
/* [in] optional description */
307 static const WCHAR displayW
[] = {'D','I','S','P','L','A','Y',0};
310 HDC hRefDC
= hdc
? hdc
: CreateDCW(displayW
,NULL
,NULL
,NULL
);
311 /* If no ref, use current display */
312 EMFDRV_PDEVICE
*physDev
;
314 DWORD size
= 0, length
= 0;
316 TRACE("%s\n", debugstr_w(filename
) );
318 if (!(dc
= DC_AllocDC( &EMFDRV_Funcs
, ENHMETAFILE_DC_MAGIC
))) return 0;
320 physDev
= HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
322 GDI_FreeObject( dc
->hSelf
, dc
);
325 dc
->physDev
= (PHYSDEV
)physDev
;
326 physDev
->hdc
= dc
->hSelf
;
329 if(description
) { /* App name\0Title\0\0 */
330 length
= lstrlenW(description
);
331 length
+= lstrlenW(description
+ length
+ 1);
335 size
= sizeof(ENHMETAHEADER
) + (length
+ 3) / 4 * 4;
337 if (!(physDev
->emh
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
))) {
338 HeapFree( GetProcessHeap(), 0, physDev
);
339 GDI_FreeObject( dc
->hSelf
, dc
);
343 physDev
->handles
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, HANDLE_LIST_INC
* sizeof(physDev
->handles
[0]));
344 physDev
->handles_size
= HANDLE_LIST_INC
;
345 physDev
->cur_handles
= 1;
348 physDev
->horzres
= GetDeviceCaps(hRefDC
, HORZRES
);
349 physDev
->vertres
= GetDeviceCaps(hRefDC
, VERTRES
);
350 physDev
->logpixelsx
= GetDeviceCaps(hRefDC
, LOGPIXELSX
);
351 physDev
->logpixelsy
= GetDeviceCaps(hRefDC
, LOGPIXELSY
);
352 physDev
->horzsize
= GetDeviceCaps(hRefDC
, HORZSIZE
);
353 physDev
->vertsize
= GetDeviceCaps(hRefDC
, VERTSIZE
);
354 physDev
->bitspixel
= GetDeviceCaps(hRefDC
, BITSPIXEL
);
355 physDev
->textcaps
= GetDeviceCaps(hRefDC
, TEXTCAPS
);
356 physDev
->rastercaps
= GetDeviceCaps(hRefDC
, RASTERCAPS
);
357 physDev
->technology
= GetDeviceCaps(hRefDC
, TECHNOLOGY
);
358 physDev
->planes
= GetDeviceCaps(hRefDC
, PLANES
);
359 physDev
->numcolors
= GetDeviceCaps(hRefDC
, NUMCOLORS
);
361 physDev
->emh
->iType
= EMR_HEADER
;
362 physDev
->emh
->nSize
= size
;
364 physDev
->emh
->rclBounds
.left
= physDev
->emh
->rclBounds
.top
= 0;
365 physDev
->emh
->rclBounds
.right
= physDev
->emh
->rclBounds
.bottom
= -1;
368 physDev
->emh
->rclFrame
.left
= rect
->left
;
369 physDev
->emh
->rclFrame
.top
= rect
->top
;
370 physDev
->emh
->rclFrame
.right
= rect
->right
;
371 physDev
->emh
->rclFrame
.bottom
= rect
->bottom
;
372 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
373 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclFrame
.top
= 0;
374 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclFrame
.bottom
= -1;
377 physDev
->emh
->dSignature
= ENHMETA_SIGNATURE
;
378 physDev
->emh
->nVersion
= 0x10000;
379 physDev
->emh
->nBytes
= physDev
->emh
->nSize
;
380 physDev
->emh
->nRecords
= 1;
381 physDev
->emh
->nHandles
= 1;
383 physDev
->emh
->sReserved
= 0; /* According to docs, this is reserved and must be 0 */
384 physDev
->emh
->nDescription
= length
/ 2;
386 physDev
->emh
->offDescription
= length
? sizeof(ENHMETAHEADER
) : 0;
388 physDev
->emh
->nPalEntries
= 0; /* I guess this should start at 0 */
391 physDev
->emh
->szlDevice
.cx
= physDev
->horzres
;
392 physDev
->emh
->szlDevice
.cy
= physDev
->vertres
;
394 /* Size in millimeters */
395 physDev
->emh
->szlMillimeters
.cx
= physDev
->horzsize
;
396 physDev
->emh
->szlMillimeters
.cy
= physDev
->vertsize
;
398 /* Size in micrometers */
399 physDev
->emh
->szlMicrometers
.cx
= physDev
->horzsize
* 1000;
400 physDev
->emh
->szlMicrometers
.cy
= physDev
->vertsize
* 1000;
402 memcpy((char *)physDev
->emh
+ sizeof(ENHMETAHEADER
), description
, length
);
404 if (filename
) /* disk based metafile */
406 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
| GENERIC_READ
, 0,
407 NULL
, CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
408 EMFDRV_DeleteDC( dc
->physDev
);
411 if (!WriteFile( hFile
, (LPSTR
)physDev
->emh
, size
, NULL
, NULL
)) {
412 EMFDRV_DeleteDC( dc
->physDev
);
415 physDev
->hFile
= hFile
;
418 TRACE("returning %p\n", dc
->hSelf
);
420 GDI_ReleaseObj( dc
->hSelf
);
428 /******************************************************************
429 * CloseEnhMetaFile (GDI32.@)
431 HENHMETAFILE WINAPI
CloseEnhMetaFile(HDC hdc
) /* [in] metafile DC */
434 EMFDRV_PDEVICE
*physDev
;
439 TRACE("(%p)\n", hdc
);
441 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, ENHMETAFILE_DC_MAGIC
))) return 0;
442 physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
447 emr
.emr
.iType
= EMR_EOF
;
448 emr
.emr
.nSize
= sizeof(emr
);
450 emr
.offPalEntries
= 0;
451 emr
.nSizeLast
= emr
.emr
.nSize
;
452 EMFDRV_WriteRecord( dc
->physDev
, &emr
.emr
);
454 /* Update rclFrame if not initialized in CreateEnhMetaFile */
455 if(physDev
->emh
->rclFrame
.left
> physDev
->emh
->rclFrame
.right
) {
456 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclBounds
.left
*
457 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
458 physDev
->emh
->rclFrame
.top
= physDev
->emh
->rclBounds
.top
*
459 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
460 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclBounds
.right
*
461 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
462 physDev
->emh
->rclFrame
.bottom
= physDev
->emh
->rclBounds
.bottom
*
463 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
466 if (physDev
->hFile
) /* disk based metafile */
468 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0)
470 CloseHandle( physDev
->hFile
);
471 EMFDRV_DeleteDC( dc
->physDev
);
475 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->emh
,
476 sizeof(*physDev
->emh
), NULL
, NULL
))
478 CloseHandle( physDev
->hFile
);
479 EMFDRV_DeleteDC( dc
->physDev
);
482 HeapFree( GetProcessHeap(), 0, physDev
->emh
);
483 hMapping
= CreateFileMappingA(physDev
->hFile
, NULL
, PAGE_READONLY
, 0,
485 TRACE("hMapping = %p\n", hMapping
);
486 physDev
->emh
= MapViewOfFile(hMapping
, FILE_MAP_READ
, 0, 0, 0);
487 TRACE("view = %p\n", physDev
->emh
);
488 CloseHandle( hMapping
);
489 CloseHandle( physDev
->hFile
);
492 hmf
= EMF_Create_HENHMETAFILE( physDev
->emh
, (physDev
->hFile
!= 0) );
493 physDev
->emh
= NULL
; /* So it won't be deleted */
494 EMFDRV_DeleteDC( dc
->physDev
);