2 * Enhanced MetaFile driver initialisation functions
4 * Copyright 1999 Huw D M Davies
13 #include "enhmetafile.h"
14 #include "enhmetafiledrv.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(enhmetafile
);
19 static const DC_FUNCTIONS EMFDRV_Funcs
=
22 EMFDRV_AbortPath
, /* pAbortPath */
24 EMFDRV_Arc
, /* pArc */
26 EMFDRV_BeginPath
, /* pBeginPath */
28 NULL
, /* pBitmapBits */
29 NULL
, /* pChoosePixelFormat */
30 EMFDRV_Chord
, /* pChord */
31 EMFDRV_CloseFigure
, /* pCloseFigure */
32 NULL
, /* pCreateBitmap */
33 NULL
, /* no implementation */ /* pCreateDC */
34 NULL
, /* pCreateDIBSection */
35 NULL
, /* pCreateDIBSection16 */
36 NULL
, /* no implementation */ /* pDeleteDC */
37 NULL
, /* pDeleteObject */
38 NULL
, /* pDescribePixelFormat */
39 NULL
, /* pDeviceCapabilities */
40 EMFDRV_Ellipse
, /* pEllipse */
43 EMFDRV_EndPath
, /* pEndPath */
44 NULL
, /* pEnumDeviceFonts */
46 EMFDRV_ExcludeClipRect
, /* pExcludeClipRect */
47 NULL
, /* pExtDeviceMode */
48 EMFDRV_ExtFloodFill
, /* pExtFloodFill */
49 NULL
, /* pExtTextOut */
50 EMFDRV_FillPath
, /* pFillPath */
51 EMFDRV_FillRgn
, /* pFillRgn */
52 EMFDRV_FlattenPath
, /* pFlattenPath */
53 EMFDRV_FrameRgn
, /* pFrameRgn */
54 NULL
, /* pGetCharWidth */
55 NULL
, /* pGetDCOrgEx */
56 NULL
, /* no implementation */ /* pGetPixel */
57 NULL
, /* pGetPixelFormat */
58 NULL
, /* pGetTextExtentPoint */
59 NULL
, /* pGetTextMetrics */
60 EMFDRV_IntersectClipRect
, /* pIntersectClipRect */
61 EMFDRV_InvertRgn
, /* pInvertRgn */
62 EMFDRV_LineTo
, /* pLineTo */
63 EMFDRV_MoveTo
, /* pMoveTo */
64 EMFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
65 NULL
, /* pOffsetViewportOrg */
66 NULL
, /* pOffsetWindowOrg */
67 EMFDRV_PaintRgn
, /* pPaintRgn */
69 EMFDRV_Pie
, /* pPie */
70 NULL
, /* pPolyBezier */
71 NULL
, /* pPolyBezierTo */
73 EMFDRV_PolyPolygon
, /* pPolyPolygon */
74 EMFDRV_PolyPolyline
, /* pPolyPolyline */
75 EMFDRV_Polygon
, /* pPolygon */
76 EMFDRV_Polyline
, /* pPolyline */
77 NULL
, /* pPolylineTo */
78 NULL
, /* pRealizePalette */
79 EMFDRV_Rectangle
, /* pRectangle */
80 EMFDRV_RestoreDC
, /* pRestoreDC */
81 EMFDRV_RoundRect
, /* pRoundRect */
82 EMFDRV_SaveDC
, /* pSaveDC */
83 EMFDRV_ScaleViewportExt
, /* pScaleViewportExt */
84 EMFDRV_ScaleWindowExt
, /* pScaleWindowExt */
85 EMFDRV_SelectClipPath
, /* pSelectClipPath */
86 NULL
, /* pSelectClipRgn */
87 EMFDRV_SelectObject
, /* pSelectObject */
88 NULL
, /* pSelectPalette */
89 EMFDRV_SetBkColor
, /* pSetBkColor */
90 EMFDRV_SetBkMode
, /* pSetBkMode */
91 NULL
, /* pSetDeviceClipping */
92 NULL
, /* pSetDIBitsToDevice */
93 EMFDRV_SetMapMode
, /* pSetMapMode */
94 EMFDRV_SetMapperFlags
, /* pSetMapperFlags */
96 NULL
, /* pSetPixelFormat */
97 EMFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
98 EMFDRV_SetROP2
, /* pSetROP2 */
99 NULL
, /* pSetRelAbs */
100 EMFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
101 EMFDRV_SetTextAlign
, /* pSetTextAlign */
102 NULL
, /* pSetTextCharacterExtra */
103 EMFDRV_SetTextColor
, /* pSetTextColor */
104 NULL
, /* pSetTextJustification */
105 EMFDRV_SetViewportExt
, /* pSetViewportExt */
106 EMFDRV_SetViewportOrg
, /* pSetViewportOrg */
107 EMFDRV_SetWindowExt
, /* pSetWindowExt */
108 EMFDRV_SetWindowOrg
, /* pSetWindowOrg */
109 NULL
, /* pStartDoc */
110 NULL
, /* pStartPage */
111 NULL
, /* pStretchBlt */
112 NULL
, /* pStretchDIBits */
113 EMFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
114 EMFDRV_StrokePath
, /* pStrokePath */
115 NULL
, /* pSwapBuffers */
116 EMFDRV_WidenPath
/* pWiddenPath */
120 /**********************************************************************
123 static BOOL
EMFDRV_DeleteDC( DC
*dc
)
125 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
127 if (physDev
->emh
) HeapFree( GetProcessHeap(), 0, physDev
->emh
);
128 HeapFree( GetProcessHeap(), 0, physDev
);
130 GDI_FreeObject( dc
->hSelf
, dc
);
135 /******************************************************************
138 * Warning: this function can change the pointer to the metafile header.
140 BOOL
EMFDRV_WriteRecord( DC
*dc
, EMR
*emr
)
144 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
146 physDev
->emh
->nBytes
+= emr
->nSize
;
147 physDev
->emh
->nRecords
++;
150 TRACE("Writing record to disk\n");
151 if (!WriteFile(physDev
->hFile
, (char *)emr
, emr
->nSize
, NULL
, NULL
))
154 len
= physDev
->emh
->nBytes
;
155 emh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->emh
, len
);
156 if (!emh
) return FALSE
;
158 memcpy((CHAR
*)physDev
->emh
+ physDev
->emh
->nBytes
- emr
->nSize
, emr
,
165 /******************************************************************
168 void EMFDRV_UpdateBBox( DC
*dc
, RECTL
*rect
)
170 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
171 RECTL
*bounds
= &physDev
->emh
->rclBounds
;
173 if(bounds
->left
> bounds
->right
) {/* first rect */
177 bounds
->left
= min(bounds
->left
, rect
->left
);
178 bounds
->top
= min(bounds
->top
, rect
->top
);
179 bounds
->right
= max(bounds
->right
, rect
->right
);
180 bounds
->bottom
= max(bounds
->bottom
, rect
->bottom
);
184 /******************************************************************
187 * Note: this function assumes that we never delete objects.
188 * If we do someday, we'll need to maintain a table to re-use deleted
191 int EMFDRV_AddHandleDC( DC
*dc
)
193 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
194 physDev
->emh
->nHandles
++;
195 return physDev
->nextHandle
++;
199 /**********************************************************************
200 * CreateEnhMetaFileA (GDI32.@)
202 HDC WINAPI
CreateEnhMetaFileA(
203 HDC hdc
, /* [in] optional reference DC */
204 LPCSTR filename
, /* [in] optional filename for disk metafiles */
205 const RECT
*rect
, /* [in] optional bounding rectangle */
206 LPCSTR description
/* [in] optional description */
209 LPWSTR filenameW
= NULL
;
210 LPWSTR descriptionW
= NULL
;
212 DWORD len1
, len2
, total
;
215 filenameW
= HEAP_strdupAtoW( GetProcessHeap(), 0, filename
);
218 len1
= strlen(description
);
219 len2
= strlen(description
+ len1
+ 1);
220 total
= MultiByteToWideChar( CP_ACP
, 0, description
, len1
+ len2
+ 3, NULL
, 0 );
221 descriptionW
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) );
222 MultiByteToWideChar( CP_ACP
, 0, description
, len1
+ len2
+ 3, descriptionW
, total
);
225 hReturnDC
= CreateEnhMetaFileW(hdc
, filenameW
, rect
, descriptionW
);
228 HeapFree( GetProcessHeap(), 0, filenameW
);
230 HeapFree( GetProcessHeap(), 0, descriptionW
);
235 /**********************************************************************
236 * CreateEnhMetaFileW (GDI32.@)
238 HDC WINAPI
CreateEnhMetaFileW(
239 HDC hdc
, /* [in] optional reference DC */
240 LPCWSTR filename
, /* [in] optional filename for disk metafiles */
241 const RECT
* rect
, /* [in] optional bounding rectangle */
242 LPCWSTR description
/* [in] optional description */
247 HDC hRefDC
= hdc
? hdc
: CreateDCA("DISPLAY",NULL
,NULL
,NULL
); /* If no ref, use current display */
248 EMFDRV_PDEVICE
*physDev
;
250 DWORD size
= 0, length
= 0;
252 TRACE("'%s'\n", debugstr_w(filename
) );
254 if (!(dc
= DC_AllocDC( &EMFDRV_Funcs
))) return 0;
255 dc
->header
.wMagic
= ENHMETAFILE_DC_MAGIC
;
257 physDev
= (EMFDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
259 GDI_FreeObject( dc
->hSelf
, dc
);
262 dc
->physDev
= physDev
;
264 if(description
) { /* App name\0Title\0\0 */
265 length
= lstrlenW(description
);
266 length
+= lstrlenW(description
+ length
+ 1);
270 size
= sizeof(ENHMETAHEADER
) + (length
+ 3) / 4 * 4;
272 if (!(physDev
->emh
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
))) {
273 HeapFree( GetProcessHeap(), 0, physDev
);
274 GDI_FreeObject( dc
->hSelf
, dc
);
278 physDev
->nextHandle
= 1;
281 physDev
->emh
->iType
= EMR_HEADER
;
282 physDev
->emh
->nSize
= size
;
284 physDev
->emh
->rclBounds
.left
= physDev
->emh
->rclBounds
.top
= 0;
285 physDev
->emh
->rclBounds
.right
= physDev
->emh
->rclBounds
.bottom
= -1;
288 physDev
->emh
->rclFrame
.left
= rect
->left
;
289 physDev
->emh
->rclFrame
.top
= rect
->top
;
290 physDev
->emh
->rclFrame
.right
= rect
->right
;
291 physDev
->emh
->rclFrame
.bottom
= rect
->bottom
;
292 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
293 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclFrame
.top
= 0;
294 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclFrame
.bottom
= -1;
297 physDev
->emh
->dSignature
= ENHMETA_SIGNATURE
;
298 physDev
->emh
->nVersion
= 0x10000;
299 physDev
->emh
->nBytes
= physDev
->emh
->nSize
;
300 physDev
->emh
->nRecords
= 1;
301 physDev
->emh
->nHandles
= 1;
303 physDev
->emh
->sReserved
= 0; /* According to docs, this is reserved and must be 0 */
304 physDev
->emh
->nDescription
= length
/ 2;
306 physDev
->emh
->offDescription
= length
? sizeof(ENHMETAHEADER
) : 0;
308 physDev
->emh
->nPalEntries
= 0; /* I guess this should start at 0 */
311 physDev
->emh
->szlDevice
.cx
= GetDeviceCaps( hRefDC
, HORZRES
);
312 physDev
->emh
->szlDevice
.cy
= GetDeviceCaps( hRefDC
, VERTRES
);
314 /* Size in millimeters */
315 physDev
->emh
->szlMillimeters
.cx
= GetDeviceCaps( hRefDC
, HORZSIZE
);
316 physDev
->emh
->szlMillimeters
.cy
= GetDeviceCaps( hRefDC
, VERTSIZE
);
318 memcpy((char *)physDev
->emh
+ sizeof(ENHMETAHEADER
), description
, length
);
320 if (filename
) /* disk based metafile */
322 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
| GENERIC_READ
, 0,
323 NULL
, CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
324 EMFDRV_DeleteDC( dc
);
327 if (!WriteFile( hFile
, (LPSTR
)physDev
->emh
, size
, NULL
, NULL
)) {
328 EMFDRV_DeleteDC( dc
);
331 physDev
->hFile
= hFile
;
337 TRACE("returning %04x\n", dc
->hSelf
);
339 GDI_ReleaseObj( dc
->hSelf
);
343 /******************************************************************
344 * CloseEnhMetaFile (GDI32.@)
346 HENHMETAFILE WINAPI
CloseEnhMetaFile(HDC hdc
) /* [in] metafile DC */
349 EMFDRV_PDEVICE
*physDev
;
354 TRACE("(%04x)\n", hdc
);
356 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, ENHMETAFILE_DC_MAGIC
))) return 0;
357 physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
359 emr
.emr
.iType
= EMR_EOF
;
360 emr
.emr
.nSize
= sizeof(emr
);
362 emr
.offPalEntries
= 0;
363 emr
.nSizeLast
= emr
.emr
.nSize
;
364 EMFDRV_WriteRecord( dc
, &emr
.emr
);
366 /* Update rclFrame if not initialized in CreateEnhMetaFile */
367 if(physDev
->emh
->rclFrame
.left
> physDev
->emh
->rclFrame
.right
) {
368 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclBounds
.left
*
369 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
370 physDev
->emh
->rclFrame
.top
= physDev
->emh
->rclBounds
.top
*
371 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
372 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclBounds
.right
*
373 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
374 physDev
->emh
->rclFrame
.bottom
= physDev
->emh
->rclBounds
.bottom
*
375 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
378 if (physDev
->hFile
) /* disk based metafile */
380 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
381 EMFDRV_DeleteDC( dc
);
385 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->emh
,
386 sizeof(*physDev
->emh
), NULL
, NULL
)) {
387 EMFDRV_DeleteDC( dc
);
390 HeapFree( GetProcessHeap(), 0, physDev
->emh
);
391 hMapping
= CreateFileMappingA(physDev
->hFile
, NULL
, PAGE_READONLY
, 0,
393 TRACE("hMapping = %08x\n", hMapping
);
394 physDev
->emh
= MapViewOfFile(hMapping
, FILE_MAP_READ
, 0, 0, 0);
395 TRACE("view = %p\n", physDev
->emh
);
399 hmf
= EMF_Create_HENHMETAFILE( physDev
->emh
, physDev
->hFile
, hMapping
);
400 physDev
->emh
= NULL
; /* So it won't be deleted */
401 EMFDRV_DeleteDC( dc
);