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 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
, /* 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
, /* no implementation */ /* pGetPixel */
57 NULL
, /* pGetTextExtentPoint */
58 NULL
, /* pGetTextMetrics */
59 EMFDRV_IntersectClipRect
, /* pIntersectClipRect */
60 EMFDRV_InvertRgn
, /* pInvertRgn */
61 EMFDRV_LineTo
, /* pLineTo */
62 NULL
, /* pLoadOEMResource */
63 EMFDRV_MoveToEx
, /* pMoveToEx */
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 EMFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
97 EMFDRV_SetROP2
, /* pSetROP2 */
98 NULL
, /* pSetRelAbs */
99 EMFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
100 EMFDRV_SetTextAlign
, /* pSetTextAlign */
101 NULL
, /* pSetTextCharacterExtra */
102 EMFDRV_SetTextColor
, /* pSetTextColor */
103 NULL
, /* pSetTextJustification */
104 EMFDRV_SetViewportExt
, /* pSetViewportExt */
105 EMFDRV_SetViewportOrg
, /* pSetViewportOrg */
106 EMFDRV_SetWindowExt
, /* pSetWindowExt */
107 EMFDRV_SetWindowOrg
, /* pSetWindowOrg */
108 NULL
, /* pStartDoc */
109 NULL
, /* pStartPage */
110 NULL
, /* pStretchBlt */
111 NULL
, /* pStretchDIBits */
112 EMFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
113 EMFDRV_StrokePath
, /* pStrokePath */
114 EMFDRV_WidenPath
/* pWiddenPath */
118 /**********************************************************************
121 static BOOL
EMFDRV_DeleteDC( DC
*dc
)
123 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
125 if (physDev
->emh
) HeapFree( GetProcessHeap(), 0, physDev
->emh
);
126 HeapFree( GetProcessHeap(), 0, physDev
);
128 GDI_FreeObject(dc
->hSelf
);
133 /******************************************************************
136 * Warning: this function can change the pointer to the metafile header.
138 BOOL
EMFDRV_WriteRecord( DC
*dc
, EMR
*emr
)
142 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
144 physDev
->emh
->nBytes
+= emr
->nSize
;
145 physDev
->emh
->nRecords
++;
148 TRACE("Writing record to disk\n");
149 if (!WriteFile(physDev
->hFile
, (char *)emr
, emr
->nSize
, NULL
, NULL
))
152 len
= physDev
->emh
->nBytes
;
153 emh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->emh
, len
);
154 if (!emh
) return FALSE
;
156 memcpy((CHAR
*)physDev
->emh
+ physDev
->emh
->nBytes
- emr
->nSize
, emr
,
163 /******************************************************************
166 void EMFDRV_UpdateBBox( DC
*dc
, RECTL
*rect
)
168 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
169 RECTL
*bounds
= &physDev
->emh
->rclBounds
;
171 if(bounds
->left
> bounds
->right
) {/* first rect */
175 bounds
->left
= MIN(bounds
->left
, rect
->left
);
176 bounds
->top
= MIN(bounds
->top
, rect
->top
);
177 bounds
->right
= MAX(bounds
->right
, rect
->right
);
178 bounds
->bottom
= MAX(bounds
->bottom
, rect
->bottom
);
182 /******************************************************************
185 * Note: this function assumes that we never delete objects.
186 * If we do someday, we'll need to maintain a table to re-use deleted
189 int EMFDRV_AddHandleDC( DC
*dc
)
191 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
192 physDev
->emh
->nHandles
++;
193 return physDev
->nextHandle
++;
197 /**********************************************************************
198 * CreateEnhMetaFileA (GDI32.41)
200 HDC WINAPI
CreateEnhMetaFileA(
201 HDC hdc
, /* optional reference DC */
202 LPCSTR filename
, /* optional filename for disk metafiles */
203 const RECT
*rect
, /* optional bounding rectangle */
204 LPCSTR description
/* optional description */
207 LPWSTR filenameW
= NULL
;
208 LPWSTR descriptionW
= NULL
;
213 filenameW
= HEAP_strdupAtoW( GetProcessHeap(), 0, filename
);
216 len1
= strlen(description
);
217 len2
= strlen(description
+ len1
+ 1);
218 descriptionW
= HeapAlloc( GetProcessHeap(), 0, (len1
+ len2
+ 3) * 2);
219 lstrcpyAtoW(descriptionW
, description
);
220 lstrcpyAtoW(descriptionW
+ len1
+ 1 , description
+ len1
+ 1);
221 *(descriptionW
+ len1
+ len2
+ 2) = 0;
224 hReturnDC
= CreateEnhMetaFileW(hdc
, filenameW
, rect
, descriptionW
);
227 HeapFree( GetProcessHeap(), 0, filenameW
);
229 HeapFree( GetProcessHeap(), 0, descriptionW
);
234 /**********************************************************************
235 * CreateEnhMetaFileW (GDI32.42)
237 HDC WINAPI
CreateEnhMetaFileW(
238 HDC hdc
, /* optional reference DC */
239 LPCWSTR filename
, /* optional filename for disk metafiles */
240 const RECT
* rect
, /* optional bounding rectangle */
241 LPCWSTR description
/* optional description */
245 HDC hRefDC
= hdc
? hdc
: GetDC(0); /* If no ref, use current display */
246 EMFDRV_PDEVICE
*physDev
;
248 DWORD size
= 0, length
= 0;
250 TRACE("'%s'\n", debugstr_w(filename
) );
252 if (!(dc
= DC_AllocDC( &EMFDRV_Funcs
))) return 0;
253 dc
->header
.wMagic
= ENHMETAFILE_DC_MAGIC
;
255 physDev
= (EMFDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
257 GDI_HEAP_FREE( dc
->hSelf
);
260 dc
->physDev
= physDev
;
262 if(description
) { /* App name\0Title\0\0 */
263 length
= lstrlenW(description
);
264 length
+= lstrlenW(description
+ length
+ 1);
268 size
= sizeof(ENHMETAHEADER
) + (length
+ 3) / 4 * 4;
270 if (!(physDev
->emh
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
))) {
271 HeapFree( GetProcessHeap(), 0, physDev
);
272 GDI_HEAP_FREE( dc
->hSelf
);
276 physDev
->nextHandle
= 1;
279 physDev
->emh
->iType
= EMR_HEADER
;
280 physDev
->emh
->nSize
= size
;
282 physDev
->emh
->rclBounds
.left
= physDev
->emh
->rclBounds
.top
= 0;
283 physDev
->emh
->rclBounds
.right
= physDev
->emh
->rclBounds
.bottom
= -1;
285 physDev
->emh
->dSignature
= ENHMETA_SIGNATURE
;
286 physDev
->emh
->nVersion
= 0x10000;
287 physDev
->emh
->nBytes
= physDev
->emh
->nSize
;
288 physDev
->emh
->nRecords
= 1;
289 physDev
->emh
->nHandles
= 1;
291 physDev
->emh
->sReserved
= 0; /* According to docs, this is reserved and must be 0 */
292 physDev
->emh
->nDescription
= length
/ 2;
294 physDev
->emh
->offDescription
= length
? sizeof(ENHMETAHEADER
) : 0;
296 physDev
->emh
->nPalEntries
= 0; /* I guess this should start at 0 */
299 physDev
->emh
->szlDevice
.cx
= GetDeviceCaps( hRefDC
, HORZRES
);
300 physDev
->emh
->szlDevice
.cy
= GetDeviceCaps( hRefDC
, VERTRES
);
302 /* Size in millimeters */
303 physDev
->emh
->szlMillimeters
.cx
= GetDeviceCaps( hRefDC
, HORZSIZE
);
304 physDev
->emh
->szlMillimeters
.cy
= GetDeviceCaps( hRefDC
, VERTSIZE
);
306 memcpy((char *)physDev
->emh
+ sizeof(ENHMETAHEADER
), description
, length
);
308 if (filename
) /* disk based metafile */
310 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
| GENERIC_READ
, 0,
311 NULL
, CREATE_ALWAYS
, 0, -1)) == HFILE_ERROR
) {
312 EMFDRV_DeleteDC( dc
);
315 if (!WriteFile( hFile
, (LPSTR
)physDev
->emh
, size
, NULL
, NULL
)) {
316 EMFDRV_DeleteDC( dc
);
319 physDev
->hFile
= hFile
;
323 ReleaseDC( 0, hRefDC
);
325 TRACE("returning %04x\n", dc
->hSelf
);
330 /******************************************************************
333 HENHMETAFILE WINAPI
CloseEnhMetaFile( HDC hdc
/* metafile DC */ )
336 EMFDRV_PDEVICE
*physDev
;
341 TRACE("(%04x)\n", hdc
);
343 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, ENHMETAFILE_DC_MAGIC
))) return 0;
344 physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
346 emr
.emr
.iType
= EMR_EOF
;
347 emr
.emr
.nSize
= sizeof(emr
);
349 emr
.offPalEntries
= 0;
350 emr
.nSizeLast
= emr
.emr
.nSize
;
351 EMFDRV_WriteRecord( dc
, &emr
.emr
);
353 if (physDev
->hFile
) /* disk based metafile */
355 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
356 EMFDRV_DeleteDC( dc
);
360 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->emh
,
361 sizeof(*physDev
->emh
), NULL
, NULL
)) {
362 EMFDRV_DeleteDC( dc
);
365 HeapFree( GetProcessHeap(), 0, physDev
->emh
);
366 hMapping
= CreateFileMappingA(physDev
->hFile
, NULL
, PAGE_READONLY
, 0,
368 TRACE("hMapping = %08x\n", hMapping
);
369 physDev
->emh
= MapViewOfFile(hMapping
, FILE_MAP_READ
, 0, 0, 0);
370 TRACE("view = %p\n", physDev
->emh
);
374 hmf
= EMF_Create_HENHMETAFILE( physDev
->emh
, physDev
->hFile
, hMapping
);
375 physDev
->emh
= NULL
; /* So it won't be deleted */
376 EMFDRV_DeleteDC( dc
);