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
, /* pGetDeviceGammaRamp */
57 NULL
, /* no implementation */ /* pGetPixel */
58 NULL
, /* pGetPixelFormat */
59 NULL
, /* pGetTextExtentPoint */
60 NULL
, /* pGetTextMetrics */
61 EMFDRV_IntersectClipRect
, /* pIntersectClipRect */
62 EMFDRV_InvertRgn
, /* pInvertRgn */
63 EMFDRV_LineTo
, /* pLineTo */
64 EMFDRV_MoveTo
, /* pMoveTo */
65 EMFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
66 NULL
, /* pOffsetViewportOrg */
67 NULL
, /* pOffsetWindowOrg */
68 EMFDRV_PaintRgn
, /* pPaintRgn */
70 EMFDRV_Pie
, /* pPie */
71 NULL
, /* pPolyBezier */
72 NULL
, /* pPolyBezierTo */
74 EMFDRV_PolyPolygon
, /* pPolyPolygon */
75 EMFDRV_PolyPolyline
, /* pPolyPolyline */
76 EMFDRV_Polygon
, /* pPolygon */
77 EMFDRV_Polyline
, /* pPolyline */
78 NULL
, /* pPolylineTo */
79 NULL
, /* pRealizePalette */
80 EMFDRV_Rectangle
, /* pRectangle */
81 EMFDRV_RestoreDC
, /* pRestoreDC */
82 EMFDRV_RoundRect
, /* pRoundRect */
83 EMFDRV_SaveDC
, /* pSaveDC */
84 EMFDRV_ScaleViewportExt
, /* pScaleViewportExt */
85 EMFDRV_ScaleWindowExt
, /* pScaleWindowExt */
86 EMFDRV_SelectClipPath
, /* pSelectClipPath */
87 NULL
, /* pSelectClipRgn */
88 EMFDRV_SelectObject
, /* pSelectObject */
89 NULL
, /* pSelectPalette */
90 EMFDRV_SetBkColor
, /* pSetBkColor */
91 EMFDRV_SetBkMode
, /* pSetBkMode */
92 NULL
, /* pSetDeviceClipping */
93 NULL
, /* pSetDeviceGammaRamp */
94 NULL
, /* pSetDIBitsToDevice */
95 EMFDRV_SetMapMode
, /* pSetMapMode */
96 EMFDRV_SetMapperFlags
, /* pSetMapperFlags */
98 NULL
, /* pSetPixelFormat */
99 EMFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
100 EMFDRV_SetROP2
, /* pSetROP2 */
101 NULL
, /* pSetRelAbs */
102 EMFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
103 EMFDRV_SetTextAlign
, /* pSetTextAlign */
104 NULL
, /* pSetTextCharacterExtra */
105 EMFDRV_SetTextColor
, /* pSetTextColor */
106 NULL
, /* pSetTextJustification */
107 EMFDRV_SetViewportExt
, /* pSetViewportExt */
108 EMFDRV_SetViewportOrg
, /* pSetViewportOrg */
109 EMFDRV_SetWindowExt
, /* pSetWindowExt */
110 EMFDRV_SetWindowOrg
, /* pSetWindowOrg */
111 NULL
, /* pStartDoc */
112 NULL
, /* pStartPage */
113 NULL
, /* pStretchBlt */
114 NULL
, /* pStretchDIBits */
115 EMFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
116 EMFDRV_StrokePath
, /* pStrokePath */
117 NULL
, /* pSwapBuffers */
118 EMFDRV_WidenPath
/* pWiddenPath */
122 /**********************************************************************
125 static BOOL
EMFDRV_DeleteDC( DC
*dc
)
127 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
129 if (physDev
->emh
) HeapFree( GetProcessHeap(), 0, physDev
->emh
);
130 HeapFree( GetProcessHeap(), 0, physDev
);
132 GDI_FreeObject( dc
->hSelf
, dc
);
137 /******************************************************************
140 * Warning: this function can change the pointer to the metafile header.
142 BOOL
EMFDRV_WriteRecord( DC
*dc
, EMR
*emr
)
146 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
148 physDev
->emh
->nBytes
+= emr
->nSize
;
149 physDev
->emh
->nRecords
++;
152 TRACE("Writing record to disk\n");
153 if (!WriteFile(physDev
->hFile
, (char *)emr
, emr
->nSize
, NULL
, NULL
))
156 len
= physDev
->emh
->nBytes
;
157 emh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->emh
, len
);
158 if (!emh
) return FALSE
;
160 memcpy((CHAR
*)physDev
->emh
+ physDev
->emh
->nBytes
- emr
->nSize
, emr
,
167 /******************************************************************
170 void EMFDRV_UpdateBBox( DC
*dc
, RECTL
*rect
)
172 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
173 RECTL
*bounds
= &physDev
->emh
->rclBounds
;
175 if(bounds
->left
> bounds
->right
) {/* first rect */
179 bounds
->left
= min(bounds
->left
, rect
->left
);
180 bounds
->top
= min(bounds
->top
, rect
->top
);
181 bounds
->right
= max(bounds
->right
, rect
->right
);
182 bounds
->bottom
= max(bounds
->bottom
, rect
->bottom
);
186 /******************************************************************
189 * Note: this function assumes that we never delete objects.
190 * If we do someday, we'll need to maintain a table to re-use deleted
193 int EMFDRV_AddHandleDC( DC
*dc
)
195 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
196 physDev
->emh
->nHandles
++;
197 return physDev
->nextHandle
++;
201 /**********************************************************************
202 * CreateEnhMetaFileA (GDI32.@)
204 HDC WINAPI
CreateEnhMetaFileA(
205 HDC hdc
, /* [in] optional reference DC */
206 LPCSTR filename
, /* [in] optional filename for disk metafiles */
207 const RECT
*rect
, /* [in] optional bounding rectangle */
208 LPCSTR description
/* [in] optional description */
211 LPWSTR filenameW
= NULL
;
212 LPWSTR descriptionW
= NULL
;
214 DWORD len1
, len2
, total
;
217 filenameW
= HEAP_strdupAtoW( GetProcessHeap(), 0, filename
);
220 len1
= strlen(description
);
221 len2
= strlen(description
+ len1
+ 1);
222 total
= MultiByteToWideChar( CP_ACP
, 0, description
, len1
+ len2
+ 3, NULL
, 0 );
223 descriptionW
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) );
224 MultiByteToWideChar( CP_ACP
, 0, description
, len1
+ len2
+ 3, descriptionW
, total
);
227 hReturnDC
= CreateEnhMetaFileW(hdc
, filenameW
, rect
, descriptionW
);
230 HeapFree( GetProcessHeap(), 0, filenameW
);
232 HeapFree( GetProcessHeap(), 0, descriptionW
);
237 /**********************************************************************
238 * CreateEnhMetaFileW (GDI32.@)
240 HDC WINAPI
CreateEnhMetaFileW(
241 HDC hdc
, /* [in] optional reference DC */
242 LPCWSTR filename
, /* [in] optional filename for disk metafiles */
243 const RECT
* rect
, /* [in] optional bounding rectangle */
244 LPCWSTR description
/* [in] optional description */
249 HDC hRefDC
= hdc
? hdc
: CreateDCA("DISPLAY",NULL
,NULL
,NULL
); /* If no ref, use current display */
250 EMFDRV_PDEVICE
*physDev
;
252 DWORD size
= 0, length
= 0;
254 TRACE("%s\n", debugstr_w(filename
) );
256 if (!(dc
= DC_AllocDC( &EMFDRV_Funcs
))) return 0;
257 dc
->header
.wMagic
= ENHMETAFILE_DC_MAGIC
;
259 physDev
= (EMFDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
261 GDI_FreeObject( dc
->hSelf
, dc
);
264 dc
->physDev
= physDev
;
266 if(description
) { /* App name\0Title\0\0 */
267 length
= lstrlenW(description
);
268 length
+= lstrlenW(description
+ length
+ 1);
272 size
= sizeof(ENHMETAHEADER
) + (length
+ 3) / 4 * 4;
274 if (!(physDev
->emh
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
))) {
275 HeapFree( GetProcessHeap(), 0, physDev
);
276 GDI_FreeObject( dc
->hSelf
, dc
);
280 physDev
->nextHandle
= 1;
283 physDev
->emh
->iType
= EMR_HEADER
;
284 physDev
->emh
->nSize
= size
;
286 physDev
->emh
->rclBounds
.left
= physDev
->emh
->rclBounds
.top
= 0;
287 physDev
->emh
->rclBounds
.right
= physDev
->emh
->rclBounds
.bottom
= -1;
290 physDev
->emh
->rclFrame
.left
= rect
->left
;
291 physDev
->emh
->rclFrame
.top
= rect
->top
;
292 physDev
->emh
->rclFrame
.right
= rect
->right
;
293 physDev
->emh
->rclFrame
.bottom
= rect
->bottom
;
294 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
295 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclFrame
.top
= 0;
296 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclFrame
.bottom
= -1;
299 physDev
->emh
->dSignature
= ENHMETA_SIGNATURE
;
300 physDev
->emh
->nVersion
= 0x10000;
301 physDev
->emh
->nBytes
= physDev
->emh
->nSize
;
302 physDev
->emh
->nRecords
= 1;
303 physDev
->emh
->nHandles
= 1;
305 physDev
->emh
->sReserved
= 0; /* According to docs, this is reserved and must be 0 */
306 physDev
->emh
->nDescription
= length
/ 2;
308 physDev
->emh
->offDescription
= length
? sizeof(ENHMETAHEADER
) : 0;
310 physDev
->emh
->nPalEntries
= 0; /* I guess this should start at 0 */
313 physDev
->emh
->szlDevice
.cx
= GetDeviceCaps( hRefDC
, HORZRES
);
314 physDev
->emh
->szlDevice
.cy
= GetDeviceCaps( hRefDC
, VERTRES
);
316 /* Size in millimeters */
317 physDev
->emh
->szlMillimeters
.cx
= GetDeviceCaps( hRefDC
, HORZSIZE
);
318 physDev
->emh
->szlMillimeters
.cy
= GetDeviceCaps( hRefDC
, VERTSIZE
);
320 memcpy((char *)physDev
->emh
+ sizeof(ENHMETAHEADER
), description
, length
);
322 if (filename
) /* disk based metafile */
324 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
| GENERIC_READ
, 0,
325 NULL
, CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
326 EMFDRV_DeleteDC( dc
);
329 if (!WriteFile( hFile
, (LPSTR
)physDev
->emh
, size
, NULL
, NULL
)) {
330 EMFDRV_DeleteDC( dc
);
333 physDev
->hFile
= hFile
;
339 TRACE("returning %04x\n", dc
->hSelf
);
341 GDI_ReleaseObj( dc
->hSelf
);
345 /******************************************************************
346 * CloseEnhMetaFile (GDI32.@)
348 HENHMETAFILE WINAPI
CloseEnhMetaFile(HDC hdc
) /* [in] metafile DC */
351 EMFDRV_PDEVICE
*physDev
;
356 TRACE("(%04x)\n", hdc
);
358 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, ENHMETAFILE_DC_MAGIC
))) return 0;
359 physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
361 emr
.emr
.iType
= EMR_EOF
;
362 emr
.emr
.nSize
= sizeof(emr
);
364 emr
.offPalEntries
= 0;
365 emr
.nSizeLast
= emr
.emr
.nSize
;
366 EMFDRV_WriteRecord( dc
, &emr
.emr
);
368 /* Update rclFrame if not initialized in CreateEnhMetaFile */
369 if(physDev
->emh
->rclFrame
.left
> physDev
->emh
->rclFrame
.right
) {
370 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclBounds
.left
*
371 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
372 physDev
->emh
->rclFrame
.top
= physDev
->emh
->rclBounds
.top
*
373 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
374 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclBounds
.right
*
375 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
376 physDev
->emh
->rclFrame
.bottom
= physDev
->emh
->rclBounds
.bottom
*
377 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
380 if (physDev
->hFile
) /* disk based metafile */
382 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
383 EMFDRV_DeleteDC( dc
);
387 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->emh
,
388 sizeof(*physDev
->emh
), NULL
, NULL
)) {
389 EMFDRV_DeleteDC( dc
);
392 HeapFree( GetProcessHeap(), 0, physDev
->emh
);
393 hMapping
= CreateFileMappingA(physDev
->hFile
, NULL
, PAGE_READONLY
, 0,
395 TRACE("hMapping = %08x\n", hMapping
);
396 physDev
->emh
= MapViewOfFile(hMapping
, FILE_MAP_READ
, 0, 0, 0);
397 TRACE("view = %p\n", physDev
->emh
);
401 hmf
= EMF_Create_HENHMETAFILE( physDev
->emh
, physDev
->hFile
, hMapping
);
402 physDev
->emh
= NULL
; /* So it won't be deleted */
403 EMFDRV_DeleteDC( dc
);