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"
19 DEFAULT_DEBUG_CHANNEL(enhmetafile
)
21 static const DC_FUNCTIONS EMFDRV_Funcs
=
24 EMFDRV_AbortPath
, /* pAbortPath */
26 EMFDRV_Arc
, /* pArc */
28 EMFDRV_BeginPath
, /* pBeginPath */
30 NULL
, /* pBitmapBits */
31 NULL
, /* pChoosePixelFormat */
32 EMFDRV_Chord
, /* pChord */
33 EMFDRV_CloseFigure
, /* pCloseFigure */
34 NULL
, /* pCreateBitmap */
35 NULL
, /* no implementation */ /* pCreateDC */
36 NULL
, /* pCreateDIBSection */
37 NULL
, /* pCreateDIBSection16 */
38 NULL
, /* no implementation */ /* pDeleteDC */
39 NULL
, /* pDeleteObject */
40 NULL
, /* pDescribePixelFormat */
41 NULL
, /* pDeviceCapabilities */
42 EMFDRV_Ellipse
, /* pEllipse */
45 EMFDRV_EndPath
, /* pEndPath */
46 NULL
, /* pEnumDeviceFonts */
48 EMFDRV_ExcludeClipRect
, /* pExcludeClipRect */
49 NULL
, /* pExtDeviceMode */
50 EMFDRV_ExtFloodFill
, /* pExtFloodFill */
51 NULL
, /* pExtTextOut */
52 EMFDRV_FillPath
, /* pFillPath */
53 EMFDRV_FillRgn
, /* pFillRgn */
54 EMFDRV_FlattenPath
, /* pFlattenPath */
55 EMFDRV_FrameRgn
, /* pFrameRgn */
56 NULL
, /* pGetCharWidth */
57 NULL
, /* pGetDCOrgEx */
58 NULL
, /* no implementation */ /* pGetPixel */
59 NULL
, /* pGetPixelFormat */
60 NULL
, /* pGetTextExtentPoint */
61 NULL
, /* pGetTextMetrics */
62 EMFDRV_IntersectClipRect
, /* pIntersectClipRect */
63 EMFDRV_InvertRgn
, /* pInvertRgn */
64 EMFDRV_LineTo
, /* pLineTo */
65 NULL
, /* pLoadOEMResource */
66 EMFDRV_MoveToEx
, /* pMoveToEx */
67 EMFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
68 NULL
, /* pOffsetViewportOrg */
69 NULL
, /* pOffsetWindowOrg */
70 EMFDRV_PaintRgn
, /* pPaintRgn */
72 EMFDRV_Pie
, /* pPie */
73 NULL
, /* pPolyBezier */
74 NULL
, /* pPolyBezierTo */
76 EMFDRV_PolyPolygon
, /* pPolyPolygon */
77 EMFDRV_PolyPolyline
, /* pPolyPolyline */
78 EMFDRV_Polygon
, /* pPolygon */
79 EMFDRV_Polyline
, /* pPolyline */
80 NULL
, /* pPolylineTo */
81 NULL
, /* pRealizePalette */
82 EMFDRV_Rectangle
, /* pRectangle */
83 EMFDRV_RestoreDC
, /* pRestoreDC */
84 EMFDRV_RoundRect
, /* pRoundRect */
85 EMFDRV_SaveDC
, /* pSaveDC */
86 EMFDRV_ScaleViewportExt
, /* pScaleViewportExt */
87 EMFDRV_ScaleWindowExt
, /* pScaleWindowExt */
88 EMFDRV_SelectClipPath
, /* pSelectClipPath */
89 NULL
, /* pSelectClipRgn */
90 EMFDRV_SelectObject
, /* pSelectObject */
91 NULL
, /* pSelectPalette */
92 EMFDRV_SetBkColor
, /* pSetBkColor */
93 EMFDRV_SetBkMode
, /* pSetBkMode */
94 NULL
, /* pSetDeviceClipping */
95 NULL
, /* pSetDIBitsToDevice */
96 EMFDRV_SetMapMode
, /* pSetMapMode */
97 EMFDRV_SetMapperFlags
, /* pSetMapperFlags */
99 NULL
, /* pSetPixelFormat */
100 EMFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
101 EMFDRV_SetROP2
, /* pSetROP2 */
102 NULL
, /* pSetRelAbs */
103 EMFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
104 EMFDRV_SetTextAlign
, /* pSetTextAlign */
105 NULL
, /* pSetTextCharacterExtra */
106 EMFDRV_SetTextColor
, /* pSetTextColor */
107 NULL
, /* pSetTextJustification */
108 EMFDRV_SetViewportExt
, /* pSetViewportExt */
109 EMFDRV_SetViewportOrg
, /* pSetViewportOrg */
110 EMFDRV_SetWindowExt
, /* pSetWindowExt */
111 EMFDRV_SetWindowOrg
, /* pSetWindowOrg */
112 NULL
, /* pStartDoc */
113 NULL
, /* pStartPage */
114 NULL
, /* pStretchBlt */
115 NULL
, /* pStretchDIBits */
116 EMFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
117 EMFDRV_StrokePath
, /* pStrokePath */
118 NULL
, /* pSwapBuffers */
119 EMFDRV_WidenPath
/* pWiddenPath */
123 /**********************************************************************
126 static BOOL
EMFDRV_DeleteDC( DC
*dc
)
128 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
130 if (physDev
->emh
) HeapFree( GetProcessHeap(), 0, physDev
->emh
);
131 HeapFree( GetProcessHeap(), 0, physDev
);
133 GDI_FreeObject(dc
->hSelf
);
138 /******************************************************************
141 * Warning: this function can change the pointer to the metafile header.
143 BOOL
EMFDRV_WriteRecord( DC
*dc
, EMR
*emr
)
147 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
149 physDev
->emh
->nBytes
+= emr
->nSize
;
150 physDev
->emh
->nRecords
++;
153 TRACE("Writing record to disk\n");
154 if (!WriteFile(physDev
->hFile
, (char *)emr
, emr
->nSize
, NULL
, NULL
))
157 len
= physDev
->emh
->nBytes
;
158 emh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->emh
, len
);
159 if (!emh
) return FALSE
;
161 memcpy((CHAR
*)physDev
->emh
+ physDev
->emh
->nBytes
- emr
->nSize
, emr
,
168 /******************************************************************
171 void EMFDRV_UpdateBBox( DC
*dc
, RECTL
*rect
)
173 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
174 RECTL
*bounds
= &physDev
->emh
->rclBounds
;
176 if(bounds
->left
> bounds
->right
) {/* first rect */
180 bounds
->left
= min(bounds
->left
, rect
->left
);
181 bounds
->top
= min(bounds
->top
, rect
->top
);
182 bounds
->right
= max(bounds
->right
, rect
->right
);
183 bounds
->bottom
= max(bounds
->bottom
, rect
->bottom
);
187 /******************************************************************
190 * Note: this function assumes that we never delete objects.
191 * If we do someday, we'll need to maintain a table to re-use deleted
194 int EMFDRV_AddHandleDC( DC
*dc
)
196 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
197 physDev
->emh
->nHandles
++;
198 return physDev
->nextHandle
++;
202 /**********************************************************************
203 * CreateEnhMetaFileA (GDI32.41)
205 HDC WINAPI
CreateEnhMetaFileA(
206 HDC hdc
, /* optional reference DC */
207 LPCSTR filename
, /* optional filename for disk metafiles */
208 const RECT
*rect
, /* optional bounding rectangle */
209 LPCSTR description
/* optional description */
212 LPWSTR filenameW
= NULL
;
213 LPWSTR descriptionW
= NULL
;
218 filenameW
= HEAP_strdupAtoW( GetProcessHeap(), 0, filename
);
221 len1
= strlen(description
);
222 len2
= strlen(description
+ len1
+ 1);
223 descriptionW
= HeapAlloc( GetProcessHeap(), 0, (len1
+ len2
+ 3) * 2);
224 lstrcpyAtoW(descriptionW
, description
);
225 lstrcpyAtoW(descriptionW
+ len1
+ 1 , description
+ len1
+ 1);
226 *(descriptionW
+ len1
+ len2
+ 2) = 0;
229 hReturnDC
= CreateEnhMetaFileW(hdc
, filenameW
, rect
, descriptionW
);
232 HeapFree( GetProcessHeap(), 0, filenameW
);
234 HeapFree( GetProcessHeap(), 0, descriptionW
);
239 /**********************************************************************
240 * CreateEnhMetaFileW (GDI32.42)
242 HDC WINAPI
CreateEnhMetaFileW(
243 HDC hdc
, /* optional reference DC */
244 LPCWSTR filename
, /* optional filename for disk metafiles */
245 const RECT
* rect
, /* optional bounding rectangle */
246 LPCWSTR description
/* optional description */
250 HDC hRefDC
= hdc
? hdc
: CreateDCA("DISPLAY",NULL
,NULL
,NULL
); /* If no ref, use current display */
251 EMFDRV_PDEVICE
*physDev
;
253 DWORD size
= 0, length
= 0;
255 TRACE("'%s'\n", debugstr_w(filename
) );
257 if (!(dc
= DC_AllocDC( &EMFDRV_Funcs
))) return 0;
258 dc
->header
.wMagic
= ENHMETAFILE_DC_MAGIC
;
260 physDev
= (EMFDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
262 GDI_HEAP_FREE( dc
->hSelf
);
265 dc
->physDev
= physDev
;
267 if(description
) { /* App name\0Title\0\0 */
268 length
= lstrlenW(description
);
269 length
+= lstrlenW(description
+ length
+ 1);
273 size
= sizeof(ENHMETAHEADER
) + (length
+ 3) / 4 * 4;
275 if (!(physDev
->emh
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
))) {
276 HeapFree( GetProcessHeap(), 0, physDev
);
277 GDI_HEAP_FREE( dc
->hSelf
);
281 physDev
->nextHandle
= 1;
284 physDev
->emh
->iType
= EMR_HEADER
;
285 physDev
->emh
->nSize
= size
;
287 physDev
->emh
->rclBounds
.left
= physDev
->emh
->rclBounds
.top
= 0;
288 physDev
->emh
->rclBounds
.right
= physDev
->emh
->rclBounds
.bottom
= -1;
291 physDev
->emh
->rclFrame
.left
= rect
->left
;
292 physDev
->emh
->rclFrame
.top
= rect
->top
;
293 physDev
->emh
->rclFrame
.right
= rect
->right
;
294 physDev
->emh
->rclFrame
.bottom
= rect
->bottom
;
295 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
296 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclFrame
.top
= 0;
297 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclFrame
.bottom
= -1;
300 physDev
->emh
->dSignature
= ENHMETA_SIGNATURE
;
301 physDev
->emh
->nVersion
= 0x10000;
302 physDev
->emh
->nBytes
= physDev
->emh
->nSize
;
303 physDev
->emh
->nRecords
= 1;
304 physDev
->emh
->nHandles
= 1;
306 physDev
->emh
->sReserved
= 0; /* According to docs, this is reserved and must be 0 */
307 physDev
->emh
->nDescription
= length
/ 2;
309 physDev
->emh
->offDescription
= length
? sizeof(ENHMETAHEADER
) : 0;
311 physDev
->emh
->nPalEntries
= 0; /* I guess this should start at 0 */
314 physDev
->emh
->szlDevice
.cx
= GetDeviceCaps( hRefDC
, HORZRES
);
315 physDev
->emh
->szlDevice
.cy
= GetDeviceCaps( hRefDC
, VERTRES
);
317 /* Size in millimeters */
318 physDev
->emh
->szlMillimeters
.cx
= GetDeviceCaps( hRefDC
, HORZSIZE
);
319 physDev
->emh
->szlMillimeters
.cy
= GetDeviceCaps( hRefDC
, VERTSIZE
);
321 memcpy((char *)physDev
->emh
+ sizeof(ENHMETAHEADER
), description
, length
);
323 if (filename
) /* disk based metafile */
325 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
| GENERIC_READ
, 0,
326 NULL
, CREATE_ALWAYS
, 0, -1)) == HFILE_ERROR
) {
327 EMFDRV_DeleteDC( dc
);
330 if (!WriteFile( hFile
, (LPSTR
)physDev
->emh
, size
, NULL
, NULL
)) {
331 EMFDRV_DeleteDC( dc
);
334 physDev
->hFile
= hFile
;
340 TRACE("returning %04x\n", dc
->hSelf
);
345 /******************************************************************
348 HENHMETAFILE WINAPI
CloseEnhMetaFile( HDC hdc
/* 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
);