2 * Enhanced MetaFile driver initialisation functions
4 * Copyright 1999 Huw D M Davies
12 #include "enhmetafile.h"
13 #include "enhmetafiledrv.h"
14 #include "wine/winestring.h"
15 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(enhmetafile
)
22 static const DC_FUNCTIONS EMFDRV_Funcs
=
25 EMFDRV_AbortPath
, /* pAbortPath */
27 EMFDRV_Arc
, /* pArc */
29 EMFDRV_BeginPath
, /* pBeginPath */
31 NULL
, /* pBitmapBits */
32 NULL
, /* pChoosePixelFormat */
33 EMFDRV_Chord
, /* pChord */
34 EMFDRV_CloseFigure
, /* pCloseFigure */
35 NULL
, /* pCreateBitmap */
36 NULL
, /* no implementation */ /* pCreateDC */
37 NULL
, /* pCreateDIBSection */
38 NULL
, /* pCreateDIBSection16 */
39 NULL
, /* no implementation */ /* pDeleteDC */
40 NULL
, /* pDeleteObject */
41 NULL
, /* pDescribePixelFormat */
42 NULL
, /* pDeviceCapabilities */
43 EMFDRV_Ellipse
, /* pEllipse */
46 EMFDRV_EndPath
, /* pEndPath */
47 NULL
, /* pEnumDeviceFonts */
49 EMFDRV_ExcludeClipRect
, /* pExcludeClipRect */
50 NULL
, /* pExtDeviceMode */
51 EMFDRV_ExtFloodFill
, /* pExtFloodFill */
52 NULL
, /* pExtTextOut */
53 EMFDRV_FillPath
, /* pFillPath */
54 EMFDRV_FillRgn
, /* pFillRgn */
55 EMFDRV_FlattenPath
, /* pFlattenPath */
56 EMFDRV_FrameRgn
, /* pFrameRgn */
57 NULL
, /* pGetCharWidth */
58 NULL
, /* pGetDCOrgEx */
59 NULL
, /* no implementation */ /* pGetPixel */
60 NULL
, /* pGetPixelFormat */
61 NULL
, /* pGetTextExtentPoint */
62 NULL
, /* pGetTextMetrics */
63 EMFDRV_IntersectClipRect
, /* pIntersectClipRect */
64 EMFDRV_InvertRgn
, /* pInvertRgn */
65 EMFDRV_LineTo
, /* pLineTo */
66 NULL
, /* pLoadOEMResource */
67 EMFDRV_MoveToEx
, /* pMoveToEx */
68 EMFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
69 NULL
, /* pOffsetViewportOrg */
70 NULL
, /* pOffsetWindowOrg */
71 EMFDRV_PaintRgn
, /* pPaintRgn */
73 EMFDRV_Pie
, /* pPie */
74 NULL
, /* pPolyBezier */
75 NULL
, /* pPolyBezierTo */
77 EMFDRV_PolyPolygon
, /* pPolyPolygon */
78 EMFDRV_PolyPolyline
, /* pPolyPolyline */
79 EMFDRV_Polygon
, /* pPolygon */
80 EMFDRV_Polyline
, /* pPolyline */
81 NULL
, /* pPolylineTo */
82 NULL
, /* pRealizePalette */
83 EMFDRV_Rectangle
, /* pRectangle */
84 EMFDRV_RestoreDC
, /* pRestoreDC */
85 EMFDRV_RoundRect
, /* pRoundRect */
86 EMFDRV_SaveDC
, /* pSaveDC */
87 EMFDRV_ScaleViewportExt
, /* pScaleViewportExt */
88 EMFDRV_ScaleWindowExt
, /* pScaleWindowExt */
89 EMFDRV_SelectClipPath
, /* pSelectClipPath */
90 NULL
, /* pSelectClipRgn */
91 EMFDRV_SelectObject
, /* pSelectObject */
92 NULL
, /* pSelectPalette */
93 EMFDRV_SetBkColor
, /* pSetBkColor */
94 EMFDRV_SetBkMode
, /* pSetBkMode */
95 NULL
, /* pSetDeviceClipping */
96 NULL
, /* pSetDIBitsToDevice */
97 EMFDRV_SetMapMode
, /* pSetMapMode */
98 EMFDRV_SetMapperFlags
, /* pSetMapperFlags */
100 NULL
, /* pSetPixelFormat */
101 EMFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
102 EMFDRV_SetROP2
, /* pSetROP2 */
103 NULL
, /* pSetRelAbs */
104 EMFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
105 EMFDRV_SetTextAlign
, /* pSetTextAlign */
106 NULL
, /* pSetTextCharacterExtra */
107 EMFDRV_SetTextColor
, /* pSetTextColor */
108 NULL
, /* pSetTextJustification */
109 EMFDRV_SetViewportExt
, /* pSetViewportExt */
110 EMFDRV_SetViewportOrg
, /* pSetViewportOrg */
111 EMFDRV_SetWindowExt
, /* pSetWindowExt */
112 EMFDRV_SetWindowOrg
, /* pSetWindowOrg */
113 NULL
, /* pStartDoc */
114 NULL
, /* pStartPage */
115 NULL
, /* pStretchBlt */
116 NULL
, /* pStretchDIBits */
117 EMFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
118 EMFDRV_StrokePath
, /* pStrokePath */
119 NULL
, /* pSwapBuffers */
120 EMFDRV_WidenPath
/* pWiddenPath */
124 /**********************************************************************
127 static BOOL
EMFDRV_DeleteDC( DC
*dc
)
129 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
131 if (physDev
->emh
) HeapFree( GetProcessHeap(), 0, physDev
->emh
);
132 HeapFree( GetProcessHeap(), 0, physDev
);
134 GDI_FreeObject(dc
->hSelf
);
139 /******************************************************************
142 * Warning: this function can change the pointer to the metafile header.
144 BOOL
EMFDRV_WriteRecord( DC
*dc
, EMR
*emr
)
148 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
150 physDev
->emh
->nBytes
+= emr
->nSize
;
151 physDev
->emh
->nRecords
++;
154 TRACE("Writing record to disk\n");
155 if (!WriteFile(physDev
->hFile
, (char *)emr
, emr
->nSize
, NULL
, NULL
))
158 len
= physDev
->emh
->nBytes
;
159 emh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->emh
, len
);
160 if (!emh
) return FALSE
;
162 memcpy((CHAR
*)physDev
->emh
+ physDev
->emh
->nBytes
- emr
->nSize
, emr
,
169 /******************************************************************
172 void EMFDRV_UpdateBBox( DC
*dc
, RECTL
*rect
)
174 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
175 RECTL
*bounds
= &physDev
->emh
->rclBounds
;
177 if(bounds
->left
> bounds
->right
) {/* first rect */
181 bounds
->left
= min(bounds
->left
, rect
->left
);
182 bounds
->top
= min(bounds
->top
, rect
->top
);
183 bounds
->right
= max(bounds
->right
, rect
->right
);
184 bounds
->bottom
= max(bounds
->bottom
, rect
->bottom
);
188 /******************************************************************
191 * Note: this function assumes that we never delete objects.
192 * If we do someday, we'll need to maintain a table to re-use deleted
195 int EMFDRV_AddHandleDC( DC
*dc
)
197 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
198 physDev
->emh
->nHandles
++;
199 return physDev
->nextHandle
++;
203 /**********************************************************************
204 * CreateEnhMetaFileA (GDI32.41)
206 HDC WINAPI
CreateEnhMetaFileA(
207 HDC hdc
, /* optional reference DC */
208 LPCSTR filename
, /* optional filename for disk metafiles */
209 const RECT
*rect
, /* optional bounding rectangle */
210 LPCSTR description
/* optional description */
213 LPWSTR filenameW
= NULL
;
214 LPWSTR descriptionW
= NULL
;
219 filenameW
= HEAP_strdupAtoW( GetProcessHeap(), 0, filename
);
222 len1
= strlen(description
);
223 len2
= strlen(description
+ len1
+ 1);
224 descriptionW
= HeapAlloc( GetProcessHeap(), 0, (len1
+ len2
+ 3) * 2);
225 lstrcpyAtoW(descriptionW
, description
);
226 lstrcpyAtoW(descriptionW
+ len1
+ 1 , description
+ len1
+ 1);
227 *(descriptionW
+ len1
+ len2
+ 2) = 0;
230 hReturnDC
= CreateEnhMetaFileW(hdc
, filenameW
, rect
, descriptionW
);
233 HeapFree( GetProcessHeap(), 0, filenameW
);
235 HeapFree( GetProcessHeap(), 0, descriptionW
);
240 /**********************************************************************
241 * CreateEnhMetaFileW (GDI32.42)
243 HDC WINAPI
CreateEnhMetaFileW(
244 HDC hdc
, /* optional reference DC */
245 LPCWSTR filename
, /* optional filename for disk metafiles */
246 const RECT
* rect
, /* optional bounding rectangle */
247 LPCWSTR description
/* optional description */
251 HDC hRefDC
= hdc
? hdc
: GetDC(0); /* If no ref, use current display */
252 EMFDRV_PDEVICE
*physDev
;
254 DWORD size
= 0, length
= 0;
256 TRACE("'%s'\n", debugstr_w(filename
) );
258 if (!(dc
= DC_AllocDC( &EMFDRV_Funcs
))) return 0;
259 dc
->header
.wMagic
= ENHMETAFILE_DC_MAGIC
;
261 physDev
= (EMFDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
263 GDI_HEAP_FREE( dc
->hSelf
);
266 dc
->physDev
= physDev
;
268 if(description
) { /* App name\0Title\0\0 */
269 length
= lstrlenW(description
);
270 length
+= lstrlenW(description
+ length
+ 1);
274 size
= sizeof(ENHMETAHEADER
) + (length
+ 3) / 4 * 4;
276 if (!(physDev
->emh
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
))) {
277 HeapFree( GetProcessHeap(), 0, physDev
);
278 GDI_HEAP_FREE( dc
->hSelf
);
282 physDev
->nextHandle
= 1;
285 physDev
->emh
->iType
= EMR_HEADER
;
286 physDev
->emh
->nSize
= size
;
288 physDev
->emh
->rclBounds
.left
= physDev
->emh
->rclBounds
.top
= 0;
289 physDev
->emh
->rclBounds
.right
= physDev
->emh
->rclBounds
.bottom
= -1;
292 physDev
->emh
->rclFrame
.left
= rect
->left
;
293 physDev
->emh
->rclFrame
.top
= rect
->top
;
294 physDev
->emh
->rclFrame
.right
= rect
->right
;
295 physDev
->emh
->rclFrame
.bottom
= rect
->bottom
;
296 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
297 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclFrame
.top
= 0;
298 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclFrame
.bottom
= -1;
301 physDev
->emh
->dSignature
= ENHMETA_SIGNATURE
;
302 physDev
->emh
->nVersion
= 0x10000;
303 physDev
->emh
->nBytes
= physDev
->emh
->nSize
;
304 physDev
->emh
->nRecords
= 1;
305 physDev
->emh
->nHandles
= 1;
307 physDev
->emh
->sReserved
= 0; /* According to docs, this is reserved and must be 0 */
308 physDev
->emh
->nDescription
= length
/ 2;
310 physDev
->emh
->offDescription
= length
? sizeof(ENHMETAHEADER
) : 0;
312 physDev
->emh
->nPalEntries
= 0; /* I guess this should start at 0 */
315 physDev
->emh
->szlDevice
.cx
= GetDeviceCaps( hRefDC
, HORZRES
);
316 physDev
->emh
->szlDevice
.cy
= GetDeviceCaps( hRefDC
, VERTRES
);
318 /* Size in millimeters */
319 physDev
->emh
->szlMillimeters
.cx
= GetDeviceCaps( hRefDC
, HORZSIZE
);
320 physDev
->emh
->szlMillimeters
.cy
= GetDeviceCaps( hRefDC
, VERTSIZE
);
322 memcpy((char *)physDev
->emh
+ sizeof(ENHMETAHEADER
), description
, length
);
324 if (filename
) /* disk based metafile */
326 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
| GENERIC_READ
, 0,
327 NULL
, CREATE_ALWAYS
, 0, -1)) == HFILE_ERROR
) {
328 EMFDRV_DeleteDC( dc
);
331 if (!WriteFile( hFile
, (LPSTR
)physDev
->emh
, size
, NULL
, NULL
)) {
332 EMFDRV_DeleteDC( dc
);
335 physDev
->hFile
= hFile
;
339 ReleaseDC( 0, hRefDC
);
341 TRACE("returning %04x\n", dc
->hSelf
);
346 /******************************************************************
349 HENHMETAFILE WINAPI
CloseEnhMetaFile( HDC hdc
/* metafile DC */ )
352 EMFDRV_PDEVICE
*physDev
;
357 TRACE("(%04x)\n", hdc
);
359 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, ENHMETAFILE_DC_MAGIC
))) return 0;
360 physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
362 emr
.emr
.iType
= EMR_EOF
;
363 emr
.emr
.nSize
= sizeof(emr
);
365 emr
.offPalEntries
= 0;
366 emr
.nSizeLast
= emr
.emr
.nSize
;
367 EMFDRV_WriteRecord( dc
, &emr
.emr
);
369 /* Update rclFrame if not initialized in CreateEnhMetaFile */
370 if(physDev
->emh
->rclFrame
.left
> physDev
->emh
->rclFrame
.right
) {
371 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclBounds
.left
*
372 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
373 physDev
->emh
->rclFrame
.top
= physDev
->emh
->rclBounds
.top
*
374 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
375 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclBounds
.right
*
376 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
377 physDev
->emh
->rclFrame
.bottom
= physDev
->emh
->rclBounds
.bottom
*
378 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
381 if (physDev
->hFile
) /* disk based metafile */
383 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
384 EMFDRV_DeleteDC( dc
);
388 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->emh
,
389 sizeof(*physDev
->emh
), NULL
, NULL
)) {
390 EMFDRV_DeleteDC( dc
);
393 HeapFree( GetProcessHeap(), 0, physDev
->emh
);
394 hMapping
= CreateFileMappingA(physDev
->hFile
, NULL
, PAGE_READONLY
, 0,
396 TRACE("hMapping = %08x\n", hMapping
);
397 physDev
->emh
= MapViewOfFile(hMapping
, FILE_MAP_READ
, 0, 0, 0);
398 TRACE("view = %p\n", physDev
->emh
);
402 hmf
= EMF_Create_HENHMETAFILE( physDev
->emh
, physDev
->hFile
, hMapping
);
403 physDev
->emh
= NULL
; /* So it won't be deleted */
404 EMFDRV_DeleteDC( dc
);