2 * Enhanced MetaFile driver initialisation functions
4 * Copyright 1999 Huw D M Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "enhmfdrv/enhmetafiledrv.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
31 static const DC_FUNCTIONS EMFDRV_Funcs =
34 EMFDRV_AbortPath, /* pAbortPath */
36 EMFDRV_Arc, /* pArc */
38 EMFDRV_BeginPath, /* pBeginPath */
40 NULL, /* pBitmapBits */
41 NULL, /* pChoosePixelFormat */
42 EMFDRV_Chord, /* pChord */
43 EMFDRV_CloseFigure, /* pCloseFigure */
44 NULL, /* pCreateBitmap */
46 NULL, /* pCreateDIBSection */
48 NULL, /* pDeleteObject */
49 NULL, /* pDescribePixelFormat */
50 NULL, /* pDeviceCapabilities */
51 EMFDRV_Ellipse, /* pEllipse */
54 EMFDRV_EndPath, /* pEndPath */
55 NULL, /* pEnumDeviceFonts */
56 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
57 NULL, /* pExtDeviceMode */
58 NULL, /* pExtEscape */
59 EMFDRV_ExtFloodFill, /* pExtFloodFill */
60 NULL, /* pExtTextOut */
61 EMFDRV_FillPath, /* pFillPath */
62 EMFDRV_FillRgn, /* pFillRgn */
63 EMFDRV_FlattenPath, /* pFlattenPath */
64 EMFDRV_FrameRgn, /* pFrameRgn */
65 NULL, /* pGetCharWidth */
66 NULL, /* pGetDCOrgEx */
67 NULL, /* pGetDIBColorTable */
68 NULL, /* pGetDIBits */
69 NULL, /* pGetDeviceCaps */
70 NULL, /* pGetDeviceGammaRamp */
72 NULL, /* pGetPixelFormat */
73 NULL, /* pGetTextExtentPoint */
74 NULL, /* pGetTextMetrics */
75 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
76 EMFDRV_InvertRgn, /* pInvertRgn */
77 EMFDRV_LineTo, /* pLineTo */
78 EMFDRV_MoveTo, /* pMoveTo */
79 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
80 NULL, /* pOffsetViewportOrg */
81 NULL, /* pOffsetWindowOrg */
82 EMFDRV_PaintRgn, /* pPaintRgn */
84 EMFDRV_Pie, /* pPie */
85 NULL, /* pPolyBezier */
86 NULL, /* pPolyBezierTo */
88 EMFDRV_PolyPolygon, /* pPolyPolygon */
89 EMFDRV_PolyPolyline, /* pPolyPolyline */
90 EMFDRV_Polygon, /* pPolygon */
91 EMFDRV_Polyline, /* pPolyline */
92 NULL, /* pPolylineTo */
93 NULL, /* pRealizePalette */
94 EMFDRV_Rectangle, /* pRectangle */
95 EMFDRV_RestoreDC, /* pRestoreDC */
96 EMFDRV_RoundRect, /* pRoundRect */
97 EMFDRV_SaveDC, /* pSaveDC */
98 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
99 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
100 EMFDRV_SelectBitmap, /* pSelectBitmap */
101 EMFDRV_SelectBrush, /* pSelectBrush */
102 EMFDRV_SelectClipPath, /* pSelectClipPath */
103 NULL, /* pSelectClipRgn */
104 EMFDRV_SelectFont, /* pSelectFont */
105 NULL, /* pSelectPalette */
106 EMFDRV_SelectPen, /* pSelectPen */
107 EMFDRV_SetBkColor, /* pSetBkColor */
108 EMFDRV_SetBkMode, /* pSetBkMode */
109 NULL, /* pSetDIBColorTable */
110 NULL, /* pSetDIBits */
111 NULL, /* pSetDIBitsToDevice */
112 NULL, /* pSetDeviceClipping */
113 NULL, /* pSetDeviceGammaRamp */
114 EMFDRV_SetMapMode, /* pSetMapMode */
115 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
116 NULL, /* pSetPixel */
117 NULL, /* pSetPixelFormat */
118 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
119 EMFDRV_SetROP2, /* pSetROP2 */
120 NULL, /* pSetRelAbs */
121 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
122 EMFDRV_SetTextAlign, /* pSetTextAlign */
123 NULL, /* pSetTextCharacterExtra */
124 EMFDRV_SetTextColor, /* pSetTextColor */
125 NULL, /* pSetTextJustification */
126 EMFDRV_SetViewportExt, /* pSetViewportExt */
127 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
128 EMFDRV_SetWindowExt, /* pSetWindowExt */
129 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
130 NULL, /* pStartDoc */
131 NULL, /* pStartPage */
132 NULL, /* pStretchBlt */
133 NULL, /* pStretchDIBits */
134 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
135 EMFDRV_StrokePath, /* pStrokePath */
136 NULL, /* pSwapBuffers */
137 EMFDRV_WidenPath /* pWiddenPath */
141 /**********************************************************************
144 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
146 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
147 DC *dc = physDev->dc;
149 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
150 HeapFree( GetProcessHeap(), 0, physDev );
152 GDI_FreeObject( dc->hSelf, dc );
157 /******************************************************************
160 * Warning: this function can change the pointer to the metafile header.
162 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
166 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
168 physDev->emh->nBytes += emr->nSize;
169 physDev->emh->nRecords++;
172 TRACE("Writing record to disk\n");
173 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
176 len = physDev->emh->nBytes;
177 emh = HeapReAlloc( GetProcessHeap(), 0, physDev->emh, len );
178 if (!emh) return FALSE;
180 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
187 /******************************************************************
190 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
192 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
193 RECTL *bounds = &physDev->emh->rclBounds;
195 if(bounds->left > bounds->right) {/* first rect */
199 bounds->left = min(bounds->left, rect->left);
200 bounds->top = min(bounds->top, rect->top);
201 bounds->right = max(bounds->right, rect->right);
202 bounds->bottom = max(bounds->bottom, rect->bottom);
206 /******************************************************************
209 * Note: this function assumes that we never delete objects.
210 * If we do someday, we'll need to maintain a table to re-use deleted
213 int EMFDRV_AddHandleDC( PHYSDEV dev )
215 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
216 physDev->emh->nHandles++;
217 return physDev->nextHandle++;
221 /**********************************************************************
222 * CreateEnhMetaFileA (GDI32.@)
224 HDC WINAPI CreateEnhMetaFileA(
225 HDC hdc, /* [in] optional reference DC */
226 LPCSTR filename, /* [in] optional filename for disk metafiles */
227 const RECT *rect, /* [in] optional bounding rectangle */
228 LPCSTR description /* [in] optional description */
231 LPWSTR filenameW = NULL;
232 LPWSTR descriptionW = NULL;
234 DWORD len1, len2, total;
238 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
239 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
240 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
243 len1 = strlen(description);
244 len2 = strlen(description + len1 + 1);
245 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
246 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
247 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
250 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
253 HeapFree( GetProcessHeap(), 0, filenameW );
255 HeapFree( GetProcessHeap(), 0, descriptionW );
260 /**********************************************************************
261 * CreateEnhMetaFileW (GDI32.@)
263 HDC WINAPI CreateEnhMetaFileW(
264 HDC hdc, /* [in] optional reference DC */
265 LPCWSTR filename, /* [in] optional filename for disk metafiles */
266 const RECT* rect, /* [in] optional bounding rectangle */
267 LPCWSTR description /* [in] optional description */
272 HDC hRefDC = hdc ? hdc : CreateDCA("DISPLAY",NULL,NULL,NULL); /* If no ref, use current display */
273 EMFDRV_PDEVICE *physDev;
275 DWORD size = 0, length = 0;
277 TRACE("%s\n", debugstr_w(filename) );
279 if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
280 dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
282 physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
284 GDI_FreeObject( dc->hSelf, dc );
287 dc->physDev = (PHYSDEV)physDev;
288 physDev->hdc = dc->hSelf;
291 if(description) { /* App name\0Title\0\0 */
292 length = lstrlenW(description);
293 length += lstrlenW(description + length + 1);
297 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
299 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
300 HeapFree( GetProcessHeap(), 0, physDev );
301 GDI_FreeObject( dc->hSelf, dc );
305 physDev->nextHandle = 1;
308 physDev->emh->iType = EMR_HEADER;
309 physDev->emh->nSize = size;
311 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
312 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
315 physDev->emh->rclFrame.left = rect->left;
316 physDev->emh->rclFrame.top = rect->top;
317 physDev->emh->rclFrame.right = rect->right;
318 physDev->emh->rclFrame.bottom = rect->bottom;
319 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
320 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
321 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
324 physDev->emh->dSignature = ENHMETA_SIGNATURE;
325 physDev->emh->nVersion = 0x10000;
326 physDev->emh->nBytes = physDev->emh->nSize;
327 physDev->emh->nRecords = 1;
328 physDev->emh->nHandles = 1;
330 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
331 physDev->emh->nDescription = length / 2;
333 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
335 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
338 physDev->emh->szlDevice.cx = GetDeviceCaps( hRefDC, HORZRES );
339 physDev->emh->szlDevice.cy = GetDeviceCaps( hRefDC, VERTRES );
341 /* Size in millimeters */
342 physDev->emh->szlMillimeters.cx = GetDeviceCaps( hRefDC, HORZSIZE );
343 physDev->emh->szlMillimeters.cy = GetDeviceCaps( hRefDC, VERTSIZE );
345 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
347 if (filename) /* disk based metafile */
349 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
350 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
351 EMFDRV_DeleteDC( dc->physDev );
354 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
355 EMFDRV_DeleteDC( dc->physDev );
358 physDev->hFile = hFile;
361 TRACE("returning %04x\n", dc->hSelf);
363 GDI_ReleaseObj( dc->hSelf );
371 /******************************************************************
372 * CloseEnhMetaFile (GDI32.@)
374 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
377 EMFDRV_PDEVICE *physDev;
382 TRACE("(%04x)\n", hdc );
384 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
385 physDev = (EMFDRV_PDEVICE *)dc->physDev;
387 emr.emr.iType = EMR_EOF;
388 emr.emr.nSize = sizeof(emr);
390 emr.offPalEntries = 0;
391 emr.nSizeLast = emr.emr.nSize;
392 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
394 /* Update rclFrame if not initialized in CreateEnhMetaFile */
395 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
396 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
397 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
398 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
399 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
400 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
401 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
402 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
403 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
406 if (physDev->hFile) /* disk based metafile */
408 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
410 CloseHandle( physDev->hFile );
411 EMFDRV_DeleteDC( dc->physDev );
415 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
416 sizeof(*physDev->emh), NULL, NULL))
418 CloseHandle( physDev->hFile );
419 EMFDRV_DeleteDC( dc->physDev );
422 HeapFree( GetProcessHeap(), 0, physDev->emh );
423 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
425 TRACE("hMapping = %08x\n", hMapping );
426 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
427 TRACE("view = %p\n", physDev->emh );
428 CloseHandle( hMapping );
429 CloseHandle( physDev->hFile );
432 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
433 physDev->emh = NULL; /* So it won't be deleted */
434 EMFDRV_DeleteDC( dc->physDev );