2 * Enhanced MetaFile driver initialisation functions
4 * Copyright 1999 Huw D M Davies
14 #include "enhmetafile.h"
15 #include "enhmetafiledrv.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(enhmetafile
);
20 static const DC_FUNCTIONS EMFDRV_Funcs
=
23 EMFDRV_AbortPath
, /* pAbortPath */
25 EMFDRV_Arc
, /* pArc */
27 EMFDRV_BeginPath
, /* pBeginPath */
29 NULL
, /* pBitmapBits */
30 NULL
, /* pChoosePixelFormat */
31 EMFDRV_Chord
, /* pChord */
32 EMFDRV_CloseFigure
, /* pCloseFigure */
33 NULL
, /* pCreateBitmap */
34 NULL
, /* no implementation */ /* pCreateDC */
35 NULL
, /* pCreateDIBSection */
36 NULL
, /* pCreateDIBSection16 */
37 NULL
, /* no implementation */ /* pDeleteDC */
38 NULL
, /* pDeleteObject */
39 NULL
, /* pDescribePixelFormat */
40 NULL
, /* pDeviceCapabilities */
41 EMFDRV_Ellipse
, /* pEllipse */
44 EMFDRV_EndPath
, /* pEndPath */
45 NULL
, /* pEnumDeviceFonts */
47 EMFDRV_ExcludeClipRect
, /* pExcludeClipRect */
48 NULL
, /* pExtDeviceMode */
49 EMFDRV_ExtFloodFill
, /* pExtFloodFill */
50 NULL
, /* pExtTextOut */
51 EMFDRV_FillPath
, /* pFillPath */
52 EMFDRV_FillRgn
, /* pFillRgn */
53 EMFDRV_FlattenPath
, /* pFlattenPath */
54 EMFDRV_FrameRgn
, /* pFrameRgn */
55 NULL
, /* pGetCharWidth */
56 NULL
, /* pGetDCOrgEx */
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
, /* pSetDIBitsToDevice */
94 EMFDRV_SetMapMode
, /* pSetMapMode */
95 EMFDRV_SetMapperFlags
, /* pSetMapperFlags */
97 NULL
, /* pSetPixelFormat */
98 EMFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
99 EMFDRV_SetROP2
, /* pSetROP2 */
100 NULL
, /* pSetRelAbs */
101 EMFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
102 EMFDRV_SetTextAlign
, /* pSetTextAlign */
103 NULL
, /* pSetTextCharacterExtra */
104 EMFDRV_SetTextColor
, /* pSetTextColor */
105 NULL
, /* pSetTextJustification */
106 EMFDRV_SetViewportExt
, /* pSetViewportExt */
107 EMFDRV_SetViewportOrg
, /* pSetViewportOrg */
108 EMFDRV_SetWindowExt
, /* pSetWindowExt */
109 EMFDRV_SetWindowOrg
, /* pSetWindowOrg */
110 NULL
, /* pStartDoc */
111 NULL
, /* pStartPage */
112 NULL
, /* pStretchBlt */
113 NULL
, /* pStretchDIBits */
114 EMFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
115 EMFDRV_StrokePath
, /* pStrokePath */
116 NULL
, /* pSwapBuffers */
117 EMFDRV_WidenPath
/* pWiddenPath */
121 /**********************************************************************
124 static BOOL
EMFDRV_DeleteDC( DC
*dc
)
126 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
128 if (physDev
->emh
) HeapFree( GetProcessHeap(), 0, physDev
->emh
);
129 HeapFree( GetProcessHeap(), 0, physDev
);
131 GDI_FreeObject( dc
->hSelf
, dc
);
136 /******************************************************************
139 * Warning: this function can change the pointer to the metafile header.
141 BOOL
EMFDRV_WriteRecord( DC
*dc
, EMR
*emr
)
145 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
147 physDev
->emh
->nBytes
+= emr
->nSize
;
148 physDev
->emh
->nRecords
++;
151 TRACE("Writing record to disk\n");
152 if (!WriteFile(physDev
->hFile
, (char *)emr
, emr
->nSize
, NULL
, NULL
))
155 len
= physDev
->emh
->nBytes
;
156 emh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->emh
, len
);
157 if (!emh
) return FALSE
;
159 memcpy((CHAR
*)physDev
->emh
+ physDev
->emh
->nBytes
- emr
->nSize
, emr
,
166 /******************************************************************
169 void EMFDRV_UpdateBBox( DC
*dc
, RECTL
*rect
)
171 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
172 RECTL
*bounds
= &physDev
->emh
->rclBounds
;
174 if(bounds
->left
> bounds
->right
) {/* first rect */
178 bounds
->left
= min(bounds
->left
, rect
->left
);
179 bounds
->top
= min(bounds
->top
, rect
->top
);
180 bounds
->right
= max(bounds
->right
, rect
->right
);
181 bounds
->bottom
= max(bounds
->bottom
, rect
->bottom
);
185 /******************************************************************
188 * Note: this function assumes that we never delete objects.
189 * If we do someday, we'll need to maintain a table to re-use deleted
192 int EMFDRV_AddHandleDC( DC
*dc
)
194 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
195 physDev
->emh
->nHandles
++;
196 return physDev
->nextHandle
++;
200 /**********************************************************************
201 * CreateEnhMetaFileA (GDI32.41)
203 HDC WINAPI
CreateEnhMetaFileA(
204 HDC hdc
, /* [in] optional reference DC */
205 LPCSTR filename
, /* [in] optional filename for disk metafiles */
206 const RECT
*rect
, /* [in] optional bounding rectangle */
207 LPCSTR description
/* [in] optional description */
210 LPWSTR filenameW
= NULL
;
211 LPWSTR descriptionW
= NULL
;
213 DWORD len1
, len2
, total
;
216 filenameW
= HEAP_strdupAtoW( GetProcessHeap(), 0, filename
);
219 len1
= strlen(description
);
220 len2
= strlen(description
+ len1
+ 1);
221 total
= MultiByteToWideChar( CP_ACP
, 0, description
, len1
+ len2
+ 3, NULL
, 0 );
222 descriptionW
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) );
223 MultiByteToWideChar( CP_ACP
, 0, description
, len1
+ len2
+ 3, descriptionW
, total
);
226 hReturnDC
= CreateEnhMetaFileW(hdc
, filenameW
, rect
, descriptionW
);
229 HeapFree( GetProcessHeap(), 0, filenameW
);
231 HeapFree( GetProcessHeap(), 0, descriptionW
);
236 /**********************************************************************
237 * CreateEnhMetaFileW (GDI32.42)
239 HDC WINAPI
CreateEnhMetaFileW(
240 HDC hdc
, /* [in] optional reference DC */
241 LPCWSTR filename
, /* [in] optional filename for disk metafiles */
242 const RECT
* rect
, /* [in] optional bounding rectangle */
243 LPCWSTR description
/* [in] optional description */
248 HDC hRefDC
= hdc
? hdc
: CreateDCA("DISPLAY",NULL
,NULL
,NULL
); /* If no ref, use current display */
249 EMFDRV_PDEVICE
*physDev
;
251 DWORD size
= 0, length
= 0;
253 TRACE("'%s'\n", debugstr_w(filename
) );
255 if (!(dc
= DC_AllocDC( &EMFDRV_Funcs
))) return 0;
256 dc
->header
.wMagic
= ENHMETAFILE_DC_MAGIC
;
258 physDev
= (EMFDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
260 GDI_FreeObject( dc
->hSelf
, dc
);
263 dc
->physDev
= physDev
;
265 if(description
) { /* App name\0Title\0\0 */
266 length
= lstrlenW(description
);
267 length
+= lstrlenW(description
+ length
+ 1);
271 size
= sizeof(ENHMETAHEADER
) + (length
+ 3) / 4 * 4;
273 if (!(physDev
->emh
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
))) {
274 HeapFree( GetProcessHeap(), 0, physDev
);
275 GDI_FreeObject( dc
->hSelf
, dc
);
279 physDev
->nextHandle
= 1;
282 physDev
->emh
->iType
= EMR_HEADER
;
283 physDev
->emh
->nSize
= size
;
285 physDev
->emh
->rclBounds
.left
= physDev
->emh
->rclBounds
.top
= 0;
286 physDev
->emh
->rclBounds
.right
= physDev
->emh
->rclBounds
.bottom
= -1;
289 physDev
->emh
->rclFrame
.left
= rect
->left
;
290 physDev
->emh
->rclFrame
.top
= rect
->top
;
291 physDev
->emh
->rclFrame
.right
= rect
->right
;
292 physDev
->emh
->rclFrame
.bottom
= rect
->bottom
;
293 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
294 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclFrame
.top
= 0;
295 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclFrame
.bottom
= -1;
298 physDev
->emh
->dSignature
= ENHMETA_SIGNATURE
;
299 physDev
->emh
->nVersion
= 0x10000;
300 physDev
->emh
->nBytes
= physDev
->emh
->nSize
;
301 physDev
->emh
->nRecords
= 1;
302 physDev
->emh
->nHandles
= 1;
304 physDev
->emh
->sReserved
= 0; /* According to docs, this is reserved and must be 0 */
305 physDev
->emh
->nDescription
= length
/ 2;
307 physDev
->emh
->offDescription
= length
? sizeof(ENHMETAHEADER
) : 0;
309 physDev
->emh
->nPalEntries
= 0; /* I guess this should start at 0 */
312 physDev
->emh
->szlDevice
.cx
= GetDeviceCaps( hRefDC
, HORZRES
);
313 physDev
->emh
->szlDevice
.cy
= GetDeviceCaps( hRefDC
, VERTRES
);
315 /* Size in millimeters */
316 physDev
->emh
->szlMillimeters
.cx
= GetDeviceCaps( hRefDC
, HORZSIZE
);
317 physDev
->emh
->szlMillimeters
.cy
= GetDeviceCaps( hRefDC
, VERTSIZE
);
319 memcpy((char *)physDev
->emh
+ sizeof(ENHMETAHEADER
), description
, length
);
321 if (filename
) /* disk based metafile */
323 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
| GENERIC_READ
, 0,
324 NULL
, CREATE_ALWAYS
, 0, -1)) == HFILE_ERROR
) {
325 EMFDRV_DeleteDC( dc
);
328 if (!WriteFile( hFile
, (LPSTR
)physDev
->emh
, size
, NULL
, NULL
)) {
329 EMFDRV_DeleteDC( dc
);
332 physDev
->hFile
= hFile
;
338 TRACE("returning %04x\n", dc
->hSelf
);
340 GDI_ReleaseObj( dc
->hSelf
);
344 /******************************************************************
347 HENHMETAFILE WINAPI
CloseEnhMetaFile(HDC hdc
) /* [in] metafile DC */
350 EMFDRV_PDEVICE
*physDev
;
355 TRACE("(%04x)\n", hdc
);
357 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, ENHMETAFILE_DC_MAGIC
))) return 0;
358 physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
360 emr
.emr
.iType
= EMR_EOF
;
361 emr
.emr
.nSize
= sizeof(emr
);
363 emr
.offPalEntries
= 0;
364 emr
.nSizeLast
= emr
.emr
.nSize
;
365 EMFDRV_WriteRecord( dc
, &emr
.emr
);
367 /* Update rclFrame if not initialized in CreateEnhMetaFile */
368 if(physDev
->emh
->rclFrame
.left
> physDev
->emh
->rclFrame
.right
) {
369 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclBounds
.left
*
370 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
371 physDev
->emh
->rclFrame
.top
= physDev
->emh
->rclBounds
.top
*
372 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
373 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclBounds
.right
*
374 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
375 physDev
->emh
->rclFrame
.bottom
= physDev
->emh
->rclBounds
.bottom
*
376 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
379 if (physDev
->hFile
) /* disk based metafile */
381 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
382 EMFDRV_DeleteDC( dc
);
386 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->emh
,
387 sizeof(*physDev
->emh
), NULL
, NULL
)) {
388 EMFDRV_DeleteDC( dc
);
391 HeapFree( GetProcessHeap(), 0, physDev
->emh
);
392 hMapping
= CreateFileMappingA(physDev
->hFile
, NULL
, PAGE_READONLY
, 0,
394 TRACE("hMapping = %08x\n", hMapping
);
395 physDev
->emh
= MapViewOfFile(hMapping
, FILE_MAP_READ
, 0, 0, 0);
396 TRACE("view = %p\n", physDev
->emh
);
400 hmf
= EMF_Create_HENHMETAFILE( physDev
->emh
, physDev
->hFile
, hMapping
);
401 physDev
->emh
= NULL
; /* So it won't be deleted */
402 EMFDRV_DeleteDC( dc
);