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_Arc
, /* pArc */
26 NULL
, /* pBitmapBits */
27 EMFDRV_Chord
, /* pChord */
28 NULL
, /* pCreateBitmap */
29 NULL
, /* no implementation */ /* pCreateDC */
30 NULL
, /* pCreateDIBSection */
31 NULL
, /* pCreateDIBSection16 */
32 NULL
, /* no implementation */ /* pDeleteDC */
33 NULL
, /* pDeleteObject */
34 NULL
, /* pDeviceCapabilities */
35 EMFDRV_Ellipse
, /* pEllipse */
38 NULL
, /* pEnumDeviceFonts */
40 EMFDRV_ExcludeClipRect
, /* pExcludeClipRect */
41 NULL
, /* pExtDeviceMode */
42 EMFDRV_ExtFloodFill
, /* pExtFloodFill */
43 NULL
, /* pExtTextOut */
44 EMFDRV_FillRgn
, /* pFillRgn */
45 EMFDRV_FrameRgn
, /* pFrameRgn */
46 NULL
, /* pGetCharWidth */
47 NULL
, /* no implementation */ /* pGetPixel */
48 NULL
, /* pGetTextExtentPoint */
49 NULL
, /* pGetTextMetrics */
50 EMFDRV_IntersectClipRect
, /* pIntersectClipRect */
51 EMFDRV_InvertRgn
, /* pInvertRgn */
52 EMFDRV_LineTo
, /* pLineTo */
53 NULL
, /* pLoadOEMResource */
54 EMFDRV_MoveToEx
, /* pMoveToEx */
55 EMFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
56 NULL
, /* pOffsetViewportOrg */
57 NULL
, /* pOffsetWindowOrg */
58 EMFDRV_PaintRgn
, /* pPaintRgn */
60 EMFDRV_Pie
, /* pPie */
61 EMFDRV_PolyPolygon
, /* pPolyPolygon */
62 EMFDRV_PolyPolyline
, /* pPolyPolyline */
63 EMFDRV_Polygon
, /* pPolygon */
64 EMFDRV_Polyline
, /* pPolyline */
65 NULL
, /* pPolyBezier */
66 NULL
, /* pRealizePalette */
67 EMFDRV_Rectangle
, /* pRectangle */
68 EMFDRV_RestoreDC
, /* pRestoreDC */
69 EMFDRV_RoundRect
, /* pRoundRect */
70 EMFDRV_SaveDC
, /* pSaveDC */
71 EMFDRV_ScaleViewportExt
, /* pScaleViewportExt */
72 EMFDRV_ScaleWindowExt
, /* pScaleWindowExt */
73 NULL
, /* pSelectClipRgn */
74 EMFDRV_SelectObject
, /* pSelectObject */
75 NULL
, /* pSelectPalette */
76 EMFDRV_SetBkColor
, /* pSetBkColor */
77 EMFDRV_SetBkMode
, /* pSetBkMode */
78 NULL
, /* pSetDeviceClipping */
79 NULL
, /* pSetDIBitsToDevice */
80 EMFDRV_SetMapMode
, /* pSetMapMode */
81 EMFDRV_SetMapperFlags
, /* pSetMapperFlags */
83 EMFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
84 EMFDRV_SetROP2
, /* pSetROP2 */
85 NULL
, /* pSetRelAbs */
86 EMFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
87 EMFDRV_SetTextAlign
, /* pSetTextAlign */
88 NULL
, /* pSetTextCharacterExtra */
89 EMFDRV_SetTextColor
, /* pSetTextColor */
90 NULL
, /* pSetTextJustification */
91 EMFDRV_SetViewportExt
, /* pSetViewportExt */
92 EMFDRV_SetViewportOrg
, /* pSetViewportOrg */
93 EMFDRV_SetWindowExt
, /* pSetWindowExt */
94 EMFDRV_SetWindowOrg
, /* pSetWindowOrg */
96 NULL
, /* pStartPage */
97 NULL
, /* pStretchBlt */
98 NULL
/* pStretchDIBits */
102 /**********************************************************************
105 static BOOL
EMFDRV_DeleteDC( DC
*dc
)
107 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
109 if (physDev
->emh
) HeapFree( SystemHeap
, 0, physDev
->emh
);
110 HeapFree( SystemHeap
, 0, physDev
);
112 GDI_FreeObject(dc
->hSelf
);
117 /******************************************************************
120 * Warning: this function can change the pointer to the metafile header.
122 BOOL
EMFDRV_WriteRecord( DC
*dc
, EMR
*emr
)
126 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
128 physDev
->emh
->nBytes
+= emr
->nSize
;
129 physDev
->emh
->nRecords
++;
132 TRACE("Writing record to disk\n");
133 if (!WriteFile(physDev
->hFile
, (char *)emr
, emr
->nSize
, NULL
, NULL
))
136 len
= physDev
->emh
->nBytes
;
137 emh
= HeapReAlloc( SystemHeap
, 0, physDev
->emh
, len
);
138 if (!emh
) return FALSE
;
140 memcpy((CHAR
*)physDev
->emh
+ physDev
->emh
->nBytes
- emr
->nSize
, emr
,
147 /******************************************************************
150 void EMFDRV_UpdateBBox( DC
*dc
, RECTL
*rect
)
152 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
153 RECTL
*bounds
= &physDev
->emh
->rclBounds
;
155 if(bounds
->left
> bounds
->right
) {/* first rect */
159 bounds
->left
= MIN(bounds
->left
, rect
->left
);
160 bounds
->top
= MIN(bounds
->top
, rect
->top
);
161 bounds
->right
= MAX(bounds
->right
, rect
->right
);
162 bounds
->bottom
= MAX(bounds
->bottom
, rect
->bottom
);
166 /******************************************************************
169 * Note: this function assumes that we never delete objects.
170 * If we do someday, we'll need to maintain a table to re-use deleted
173 int EMFDRV_AddHandleDC( DC
*dc
)
175 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
176 physDev
->emh
->nHandles
++;
177 return physDev
->nextHandle
++;
181 /**********************************************************************
182 * CreateEnhMetaFileA (GDI32.41)
184 HDC WINAPI
CreateEnhMetaFileA(
185 HDC hdc
, /* optional reference DC */
186 LPCSTR filename
, /* optional filename for disk metafiles */
187 const RECT
*rect
, /* optional bounding rectangle */
188 LPCSTR description
/* optional description */
191 LPWSTR filenameW
= NULL
;
192 LPWSTR descriptionW
= NULL
;
197 filenameW
= HEAP_strdupAtoW( GetProcessHeap(), 0, filename
);
200 len1
= strlen(description
);
201 len2
= strlen(description
+ len1
+ 1);
202 descriptionW
= HeapAlloc( GetProcessHeap(), 0, (len1
+ len2
+ 3) * 2);
203 lstrcpyAtoW(descriptionW
, description
);
204 lstrcpyAtoW(descriptionW
+ len1
+ 1 , description
+ len1
+ 1);
205 *(descriptionW
+ len1
+ len2
+ 2) = 0;
208 hReturnDC
= CreateEnhMetaFileW(hdc
, filenameW
, rect
, descriptionW
);
211 HeapFree( GetProcessHeap(), 0, filenameW
);
213 HeapFree( GetProcessHeap(), 0, descriptionW
);
218 /**********************************************************************
219 * CreateEnhMetaFileW (GDI32.42)
221 HDC WINAPI
CreateEnhMetaFileW(
222 HDC hdc
, /* optional reference DC */
223 LPCWSTR filename
, /* optional filename for disk metafiles */
224 const RECT
* rect
, /* optional bounding rectangle */
225 LPCWSTR description
/* optional description */
229 EMFDRV_PDEVICE
*physDev
;
231 DWORD size
= 0, length
= 0;
233 TRACE("'%s'\n", debugstr_w(filename
) );
235 if (!(dc
= DC_AllocDC( &EMFDRV_Funcs
))) return 0;
236 dc
->header
.wMagic
= ENHMETAFILE_DC_MAGIC
;
238 physDev
= (EMFDRV_PDEVICE
*)HeapAlloc(SystemHeap
,0,sizeof(*physDev
));
240 GDI_HEAP_FREE( dc
->hSelf
);
243 dc
->physDev
= physDev
;
245 if(description
) { /* App name\0Title\0\0 */
246 length
= lstrlenW(description
);
247 length
+= lstrlenW(description
+ length
+ 1);
251 size
= sizeof(ENHMETAHEADER
) + (length
+ 3) / 4 * 4;
253 if (!(physDev
->emh
= HeapAlloc( SystemHeap
, HEAP_ZERO_MEMORY
, size
))) {
254 HeapFree( SystemHeap
, 0, physDev
);
255 GDI_HEAP_FREE( dc
->hSelf
);
259 physDev
->nextHandle
= 1;
262 physDev
->emh
->iType
= EMR_HEADER
;
263 physDev
->emh
->nSize
= size
;
264 physDev
->emh
->dSignature
= ENHMETA_SIGNATURE
;
265 physDev
->emh
->nVersion
= 0x10000;
266 physDev
->emh
->nBytes
= physDev
->emh
->nSize
;
267 physDev
->emh
->nRecords
= 1;
268 physDev
->emh
->nHandles
= 1;
269 physDev
->emh
->rclBounds
.left
= physDev
->emh
->rclBounds
.top
= 0;
270 physDev
->emh
->rclBounds
.right
= physDev
->emh
->rclBounds
.bottom
= -1;
271 physDev
->emh
->nDescription
= length
/ 2;
272 physDev
->emh
->offDescription
= length
? sizeof(ENHMETAHEADER
) : 0;
273 memcpy((char *)physDev
->emh
+ sizeof(ENHMETAHEADER
), description
, length
);
275 if (filename
) /* disk based metafile */
277 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
| GENERIC_READ
, 0,
278 NULL
, CREATE_ALWAYS
, 0, -1)) == HFILE_ERROR
) {
279 EMFDRV_DeleteDC( dc
);
282 if (!WriteFile( hFile
, (LPSTR
)physDev
->emh
, size
, NULL
, NULL
)) {
283 EMFDRV_DeleteDC( dc
);
286 physDev
->hFile
= hFile
;
289 TRACE("returning %04x\n", dc
->hSelf
);
294 /******************************************************************
297 HENHMETAFILE WINAPI
CloseEnhMetaFile( HDC hdc
/* metafile DC */ )
300 EMFDRV_PDEVICE
*physDev
;
305 TRACE("(%04x)\n", hdc
);
307 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, ENHMETAFILE_DC_MAGIC
))) return 0;
308 physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
310 emr
.emr
.iType
= EMR_EOF
;
311 emr
.emr
.nSize
= sizeof(emr
);
313 emr
.offPalEntries
= 0;
314 emr
.nSizeLast
= emr
.emr
.nSize
;
315 EMFDRV_WriteRecord( dc
, &emr
.emr
);
317 if (physDev
->hFile
) /* disk based metafile */
319 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0) {
320 EMFDRV_DeleteDC( dc
);
324 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->emh
,
325 sizeof(*physDev
->emh
), NULL
, NULL
)) {
326 EMFDRV_DeleteDC( dc
);
329 HeapFree( SystemHeap
, 0, physDev
->emh
);
330 hMapping
= CreateFileMappingA(physDev
->hFile
, NULL
, PAGE_READONLY
, 0,
332 TRACE("hMapping = %08x\n", hMapping
);
333 physDev
->emh
= MapViewOfFile(hMapping
, FILE_MAP_READ
, 0, 0, 0);
334 TRACE("view = %p\n", physDev
->emh
);
338 hmf
= EMF_Create_HENHMETAFILE( physDev
->emh
, physDev
->hFile
, hMapping
);
339 physDev
->emh
= NULL
; /* So it won't be deleted */
340 EMFDRV_DeleteDC( dc
);