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 */
96 EMFDRV_RestoreDC, /* pRestoreDC */
97 EMFDRV_RoundRect, /* pRoundRect */
98 EMFDRV_SaveDC, /* pSaveDC */
99 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
100 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
101 EMFDRV_SelectBitmap, /* pSelectBitmap */
102 EMFDRV_SelectBrush, /* pSelectBrush */
103 EMFDRV_SelectClipPath, /* pSelectClipPath */
104 NULL, /* pSelectClipRgn */
105 EMFDRV_SelectFont, /* pSelectFont */
106 NULL, /* pSelectPalette */
107 EMFDRV_SelectPen, /* pSelectPen */
108 EMFDRV_SetBkColor, /* pSetBkColor */
109 EMFDRV_SetBkMode, /* pSetBkMode */
110 NULL, /* pSetDIBColorTable */
111 NULL, /* pSetDIBits */
112 NULL, /* pSetDIBitsToDevice */
113 NULL, /* pSetDeviceClipping */
114 NULL, /* pSetDeviceGammaRamp */
115 EMFDRV_SetMapMode, /* pSetMapMode */
116 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
117 NULL, /* pSetPixel */
118 NULL, /* pSetPixelFormat */
119 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
120 EMFDRV_SetROP2, /* pSetROP2 */
121 NULL, /* pSetRelAbs */
122 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
123 EMFDRV_SetTextAlign, /* pSetTextAlign */
124 NULL, /* pSetTextCharacterExtra */
125 EMFDRV_SetTextColor, /* pSetTextColor */
126 NULL, /* pSetTextJustification */
127 EMFDRV_SetViewportExt, /* pSetViewportExt */
128 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
129 EMFDRV_SetWindowExt, /* pSetWindowExt */
130 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
131 NULL, /* pStartDoc */
132 NULL, /* pStartPage */
133 NULL, /* pStretchBlt */
134 NULL, /* pStretchDIBits */
135 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
136 EMFDRV_StrokePath, /* pStrokePath */
137 NULL, /* pSwapBuffers */
138 EMFDRV_WidenPath /* pWiddenPath */
142 /**********************************************************************
145 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
147 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
148 DC *dc = physDev->dc;
150 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
151 HeapFree( GetProcessHeap(), 0, physDev );
153 GDI_FreeObject( dc->hSelf, dc );
158 /******************************************************************
161 * Warning: this function can change the pointer to the metafile header.
163 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
167 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
169 physDev->emh->nBytes += emr->nSize;
170 physDev->emh->nRecords++;
173 TRACE("Writing record to disk\n");
174 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
177 len = physDev->emh->nBytes;
178 emh = HeapReAlloc( GetProcessHeap(), 0, physDev->emh, len );
179 if (!emh) return FALSE;
181 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
188 /******************************************************************
191 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
193 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
194 RECTL *bounds = &physDev->emh->rclBounds;
196 if(bounds->left > bounds->right) {/* first rect */
200 bounds->left = min(bounds->left, rect->left);
201 bounds->top = min(bounds->top, rect->top);
202 bounds->right = max(bounds->right, rect->right);
203 bounds->bottom = max(bounds->bottom, rect->bottom);
207 /******************************************************************
210 * Note: this function assumes that we never delete objects.
211 * If we do someday, we'll need to maintain a table to re-use deleted
214 int EMFDRV_AddHandleDC( PHYSDEV dev )
216 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
217 physDev->emh->nHandles++;
218 return physDev->nextHandle++;
222 /**********************************************************************
223 * CreateEnhMetaFileA (GDI32.@)
225 HDC WINAPI CreateEnhMetaFileA(
226 HDC hdc, /* [in] optional reference DC */
227 LPCSTR filename, /* [in] optional filename for disk metafiles */
228 const RECT *rect, /* [in] optional bounding rectangle */
229 LPCSTR description /* [in] optional description */
232 LPWSTR filenameW = NULL;
233 LPWSTR descriptionW = NULL;
235 DWORD len1, len2, total;
239 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
240 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
241 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
244 len1 = strlen(description);
245 len2 = strlen(description + len1 + 1);
246 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
247 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
248 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
251 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
254 HeapFree( GetProcessHeap(), 0, filenameW );
256 HeapFree( GetProcessHeap(), 0, descriptionW );
261 /**********************************************************************
262 * CreateEnhMetaFileW (GDI32.@)
264 HDC WINAPI CreateEnhMetaFileW(
265 HDC hdc, /* [in] optional reference DC */
266 LPCWSTR filename, /* [in] optional filename for disk metafiles */
267 const RECT* rect, /* [in] optional bounding rectangle */
268 LPCWSTR description /* [in] optional description */
273 HDC hRefDC = hdc ? hdc : CreateDCA("DISPLAY",NULL,NULL,NULL); /* If no ref, use current display */
274 EMFDRV_PDEVICE *physDev;
276 DWORD size = 0, length = 0;
278 TRACE("%s\n", debugstr_w(filename) );
280 if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
281 dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
283 physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
285 GDI_FreeObject( dc->hSelf, dc );
288 dc->physDev = (PHYSDEV)physDev;
289 physDev->hdc = dc->hSelf;
292 if(description) { /* App name\0Title\0\0 */
293 length = lstrlenW(description);
294 length += lstrlenW(description + length + 1);
298 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
300 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
301 HeapFree( GetProcessHeap(), 0, physDev );
302 GDI_FreeObject( dc->hSelf, dc );
306 physDev->nextHandle = 1;
309 physDev->emh->iType = EMR_HEADER;
310 physDev->emh->nSize = size;
312 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
313 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
316 physDev->emh->rclFrame.left = rect->left;
317 physDev->emh->rclFrame.top = rect->top;
318 physDev->emh->rclFrame.right = rect->right;
319 physDev->emh->rclFrame.bottom = rect->bottom;
320 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
321 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
322 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
325 physDev->emh->dSignature = ENHMETA_SIGNATURE;
326 physDev->emh->nVersion = 0x10000;
327 physDev->emh->nBytes = physDev->emh->nSize;
328 physDev->emh->nRecords = 1;
329 physDev->emh->nHandles = 1;
331 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
332 physDev->emh->nDescription = length / 2;
334 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
336 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
339 physDev->emh->szlDevice.cx = GetDeviceCaps( hRefDC, HORZRES );
340 physDev->emh->szlDevice.cy = GetDeviceCaps( hRefDC, VERTRES );
342 /* Size in millimeters */
343 physDev->emh->szlMillimeters.cx = GetDeviceCaps( hRefDC, HORZSIZE );
344 physDev->emh->szlMillimeters.cy = GetDeviceCaps( hRefDC, VERTSIZE );
346 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
348 if (filename) /* disk based metafile */
350 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
351 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
352 EMFDRV_DeleteDC( dc->physDev );
355 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
356 EMFDRV_DeleteDC( dc->physDev );
359 physDev->hFile = hFile;
362 TRACE("returning %04x\n", dc->hSelf);
364 GDI_ReleaseObj( dc->hSelf );
372 /******************************************************************
373 * CloseEnhMetaFile (GDI32.@)
375 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
378 EMFDRV_PDEVICE *physDev;
383 TRACE("(%04x)\n", hdc );
385 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
386 physDev = (EMFDRV_PDEVICE *)dc->physDev;
388 emr.emr.iType = EMR_EOF;
389 emr.emr.nSize = sizeof(emr);
391 emr.offPalEntries = 0;
392 emr.nSizeLast = emr.emr.nSize;
393 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
395 /* Update rclFrame if not initialized in CreateEnhMetaFile */
396 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
397 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
398 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
399 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
400 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
401 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
402 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
403 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
404 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
407 if (physDev->hFile) /* disk based metafile */
409 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
411 CloseHandle( physDev->hFile );
412 EMFDRV_DeleteDC( dc->physDev );
416 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
417 sizeof(*physDev->emh), NULL, NULL))
419 CloseHandle( physDev->hFile );
420 EMFDRV_DeleteDC( dc->physDev );
423 HeapFree( GetProcessHeap(), 0, physDev->emh );
424 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
426 TRACE("hMapping = %08x\n", hMapping );
427 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
428 TRACE("view = %p\n", physDev->emh );
429 CloseHandle( hMapping );
430 CloseHandle( physDev->hFile );
433 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
434 physDev->emh = NULL; /* So it won't be deleted */
435 EMFDRV_DeleteDC( dc->physDev );