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
, /* pGetDCOrgEx */
57 NULL
, /* no implementation */ /* pGetPixel */
58 NULL
, /* pGetTextExtentPoint */
59 NULL
, /* pGetTextMetrics */
60 EMFDRV_IntersectClipRect
, /* pIntersectClipRect */
61 EMFDRV_InvertRgn
, /* pInvertRgn */
62 EMFDRV_LineTo
, /* pLineTo */
63 NULL
, /* pLoadOEMResource */
64 EMFDRV_MoveToEx
, /* pMoveToEx */
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 EMFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
98 EMFDRV_SetROP2
, /* pSetROP2 */
99 NULL
, /* pSetRelAbs */
100 EMFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
101 EMFDRV_SetTextAlign
, /* pSetTextAlign */
102 NULL
, /* pSetTextCharacterExtra */
103 EMFDRV_SetTextColor
, /* pSetTextColor */
104 NULL
, /* pSetTextJustification */
105 EMFDRV_SetViewportExt
, /* pSetViewportExt */
106 EMFDRV_SetViewportOrg
, /* pSetViewportOrg */
107 EMFDRV_SetWindowExt
, /* pSetWindowExt */
108 EMFDRV_SetWindowOrg
, /* pSetWindowOrg */
109 NULL
, /* pStartDoc */
110 NULL
, /* pStartPage */
111 NULL
, /* pStretchBlt */
112 NULL
, /* pStretchDIBits */
113 EMFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
114 EMFDRV_StrokePath
, /* pStrokePath */
115 EMFDRV_WidenPath
/* pWiddenPath */
119 /**********************************************************************
122 static BOOL
EMFDRV_DeleteDC( DC
*dc
)
124 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
126 if (physDev
->emh
) HeapFree( GetProcessHeap(), 0, physDev
->emh
);
127 HeapFree( GetProcessHeap(), 0, physDev
);
129 GDI_FreeObject(dc
->hSelf
);
134 /******************************************************************
137 * Warning: this function can change the pointer to the metafile header.
139 BOOL
EMFDRV_WriteRecord( DC
*dc
, EMR
*emr
)
143 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
145 physDev
->emh
->nBytes
+= emr
->nSize
;
146 physDev
->emh
->nRecords
++;
149 TRACE("Writing record to disk\n");
150 if (!WriteFile(physDev
->hFile
, (char *)emr
, emr
->nSize
, NULL
, NULL
))
153 len
= physDev
->emh
->nBytes
;
154 emh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->emh
, len
);
155 if (!emh
) return FALSE
;
157 memcpy((CHAR
*)physDev
->emh
+ physDev
->emh
->nBytes
- emr
->nSize
, emr
,
164 /******************************************************************
167 void EMFDRV_UpdateBBox( DC
*dc
, RECTL
*rect
)
169 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
170 RECTL
*bounds
= &physDev
->emh
->rclBounds
;
172 if(bounds
->left
> bounds
->right
) {/* first rect */
176 bounds
->left
= min(bounds
->left
, rect
->left
);
177 bounds
->top
= min(bounds
->top
, rect
->top
);
178 bounds
->right
= max(bounds
->right
, rect
->right
);
179 bounds
->bottom
= max(bounds
->bottom
, rect
->bottom
);
183 /******************************************************************
186 * Note: this function assumes that we never delete objects.
187 * If we do someday, we'll need to maintain a table to re-use deleted
190 int EMFDRV_AddHandleDC( DC
*dc
)
192 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
193 physDev
->emh
->nHandles
++;
194 return physDev
->nextHandle
++;
198 /**********************************************************************
199 * CreateEnhMetaFileA (GDI32.41)
201 HDC WINAPI
CreateEnhMetaFileA(
202 HDC hdc
, /* optional reference DC */
203 LPCSTR filename
, /* optional filename for disk metafiles */
204 const RECT
*rect
, /* optional bounding rectangle */
205 LPCSTR description
/* optional description */
208 LPWSTR filenameW
= NULL
;
209 LPWSTR descriptionW
= NULL
;
214 filenameW
= HEAP_strdupAtoW( GetProcessHeap(), 0, filename
);
217 len1
= strlen(description
);
218 len2
= strlen(description
+ len1
+ 1);
219 descriptionW
= HeapAlloc( GetProcessHeap(), 0, (len1
+ len2
+ 3) * 2);
220 lstrcpyAtoW(descriptionW
, description
);
221 lstrcpyAtoW(descriptionW
+ len1
+ 1 , description
+ len1
+ 1);
222 *(descriptionW
+ len1
+ len2
+ 2) = 0;
225 hReturnDC
= CreateEnhMetaFileW(hdc
, filenameW
, rect
, descriptionW
);
228 HeapFree( GetProcessHeap(), 0, filenameW
);
230 HeapFree( GetProcessHeap(), 0, descriptionW
);
235 /**********************************************************************
236 * CreateEnhMetaFileW (GDI32.42)
238 HDC WINAPI
CreateEnhMetaFileW(
239 HDC hdc
, /* optional reference DC */
240 LPCWSTR filename
, /* optional filename for disk metafiles */
241 const RECT
* rect
, /* optional bounding rectangle */
242 LPCWSTR description
/* optional description */
246 HDC hRefDC
= hdc
? hdc
: GetDC(0); /* If no ref, use current display */
247 EMFDRV_PDEVICE
*physDev
;
249 DWORD size
= 0, length
= 0;
251 TRACE("'%s'\n", debugstr_w(filename
) );
253 if (!(dc
= DC_AllocDC( &EMFDRV_Funcs
))) return 0;
254 dc
->header
.wMagic
= ENHMETAFILE_DC_MAGIC
;
256 physDev
= (EMFDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
258 GDI_HEAP_FREE( dc
->hSelf
);
261 dc
->physDev
= physDev
;
263 if(description
) { /* App name\0Title\0\0 */
264 length
= lstrlenW(description
);
265 length
+= lstrlenW(description
+ length
+ 1);
269 size
= sizeof(ENHMETAHEADER
) + (length
+ 3) / 4 * 4;
271 if (!(physDev
->emh
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
))) {
272 HeapFree( GetProcessHeap(), 0, physDev
);
273 GDI_HEAP_FREE( dc
->hSelf
);
277 physDev
->nextHandle
= 1;
280 physDev
->emh
->iType
= EMR_HEADER
;
281 physDev
->emh
->nSize
= size
;
283 physDev
->emh
->rclBounds
.left
= physDev
->emh
->rclBounds
.top
= 0;
284 physDev
->emh
->rclBounds
.right
= physDev
->emh
->rclBounds
.bottom
= -1;
286 physDev
->emh
->dSignature
= ENHMETA_SIGNATURE
;
287 physDev
->emh
->nVersion
= 0x10000;
288 physDev
->emh
->nBytes
= physDev
->emh
->nSize
;
289 physDev
->emh
->nRecords
= 1;
290 physDev
->emh
->nHandles
= 1;
292 physDev
->emh
->sReserved
= 0; /* According to docs, this is reserved and must be 0 */
293 physDev
->emh
->nDescription
= length
/ 2;
295 physDev
->emh
->offDescription
= length
? sizeof(ENHMETAHEADER
) : 0;
297 physDev
->emh
->nPalEntries
= 0; /* I guess this should start at 0 */
300 physDev
->emh
->szlDevice
.cx
= GetDeviceCaps( hRefDC
, HORZRES
);
301 physDev
->emh
->szlDevice
.cy
= GetDeviceCaps( hRefDC
, VERTRES
);
303 /* Size in millimeters */
304 physDev
->emh
->szlMillimeters
.cx
= GetDeviceCaps( hRefDC
, HORZSIZE
);
305 physDev
->emh
->szlMillimeters
.cy
= GetDeviceCaps( hRefDC
, VERTSIZE
);
307 memcpy((char *)physDev
->emh
+ sizeof(ENHMETAHEADER
), description
, length
);
309 if (filename
) /* disk based metafile */
311 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
| GENERIC_READ
, 0,
312 NULL
, CREATE_ALWAYS
, 0, -1)) == HFILE_ERROR
) {
313 EMFDRV_DeleteDC( dc
);
316 if (!WriteFile( hFile
, (LPSTR
)physDev
->emh
, size
, NULL
, NULL
)) {
317 EMFDRV_DeleteDC( dc
);
320 physDev
->hFile
= hFile
;
324 ReleaseDC( 0, hRefDC
);
326 TRACE("returning %04x\n", dc
->hSelf
);
331 /******************************************************************
334 HENHMETAFILE WINAPI
CloseEnhMetaFile( HDC hdc
/* metafile DC */ )
337 EMFDRV_PDEVICE
*physDev
;
342 TRACE("(%04x)\n", hdc
);
344 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, ENHMETAFILE_DC_MAGIC
))) return 0;
345 physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
347 emr
.emr
.iType
= EMR_EOF
;
348 emr
.emr
.nSize
= sizeof(emr
);
350 emr
.offPalEntries
= 0;
351 emr
.nSizeLast
= emr
.emr
.nSize
;
352 EMFDRV_WriteRecord( dc
, &emr
.emr
);
354 if (physDev
->hFile
) /* disk based metafile */
356 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
357 EMFDRV_DeleteDC( dc
);
361 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->emh
,
362 sizeof(*physDev
->emh
), NULL
, NULL
)) {
363 EMFDRV_DeleteDC( dc
);
366 HeapFree( GetProcessHeap(), 0, physDev
->emh
);
367 hMapping
= CreateFileMappingA(physDev
->hFile
, NULL
, PAGE_READONLY
, 0,
369 TRACE("hMapping = %08x\n", hMapping
);
370 physDev
->emh
= MapViewOfFile(hMapping
, FILE_MAP_READ
, 0, 0, 0);
371 TRACE("view = %p\n", physDev
->emh
);
375 hmf
= EMF_Create_HENHMETAFILE( physDev
->emh
, physDev
->hFile
, hMapping
);
376 physDev
->emh
= NULL
; /* So it won't be deleted */
377 EMFDRV_DeleteDC( dc
);