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, /* pChoosePixelFormat */
41 EMFDRV_Chord, /* pChord */
42 EMFDRV_CloseFigure, /* pCloseFigure */
43 NULL, /* pCreateBitmap */
45 NULL, /* pCreateDIBSection */
46 NULL, /* pDeleteBitmap */
48 NULL, /* pDescribePixelFormat */
49 NULL, /* pDeviceCapabilities */
50 EMFDRV_Ellipse, /* pEllipse */
53 EMFDRV_EndPath, /* pEndPath */
54 NULL, /* pEnumDeviceFonts */
55 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
56 NULL, /* pExtDeviceMode */
57 NULL, /* pExtEscape */
58 EMFDRV_ExtFloodFill, /* pExtFloodFill */
59 EMFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
60 NULL, /* pExtTextOut */
61 EMFDRV_FillPath, /* pFillPath */
62 EMFDRV_FillRgn, /* pFillRgn */
63 EMFDRV_FlattenPath, /* pFlattenPath */
64 EMFDRV_FrameRgn, /* pFrameRgn */
65 NULL, /* pGetBitmapBits */
66 NULL, /* pGetCharWidth */
67 NULL, /* pGetDCOrgEx */
68 NULL, /* pGetDIBColorTable */
69 NULL, /* pGetDIBits */
70 NULL, /* pGetDeviceCaps */
71 NULL, /* pGetDeviceGammaRamp */
72 NULL, /* pGetNearestColor */
74 NULL, /* pGetPixelFormat */
75 NULL, /* pGetSystemPaletteEntries */
76 NULL, /* pGetTextExtentPoint */
77 NULL, /* pGetTextMetrics */
78 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
79 EMFDRV_InvertRgn, /* pInvertRgn */
80 EMFDRV_LineTo, /* pLineTo */
81 EMFDRV_MoveTo, /* pMoveTo */
82 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
83 NULL, /* pOffsetViewportOrg */
84 NULL, /* pOffsetWindowOrg */
85 EMFDRV_PaintRgn, /* pPaintRgn */
86 EMFDRV_PatBlt, /* pPatBlt */
87 EMFDRV_Pie, /* pPie */
88 NULL, /* pPolyBezier */
89 NULL, /* pPolyBezierTo */
91 EMFDRV_PolyPolygon, /* pPolyPolygon */
92 EMFDRV_PolyPolyline, /* pPolyPolyline */
93 EMFDRV_Polygon, /* pPolygon */
94 EMFDRV_Polyline, /* pPolyline */
95 NULL, /* pPolylineTo */
96 NULL, /* pRealizeDefaultPalette */
97 NULL, /* pRealizePalette */
98 EMFDRV_Rectangle, /* pRectangle */
100 EMFDRV_RestoreDC, /* pRestoreDC */
101 EMFDRV_RoundRect, /* pRoundRect */
102 EMFDRV_SaveDC, /* pSaveDC */
103 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
104 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
105 EMFDRV_SelectBitmap, /* pSelectBitmap */
106 EMFDRV_SelectBrush, /* pSelectBrush */
107 EMFDRV_SelectClipPath, /* pSelectClipPath */
108 EMFDRV_SelectFont, /* pSelectFont */
109 NULL, /* pSelectPalette */
110 EMFDRV_SelectPen, /* pSelectPen */
111 NULL, /* pSetBitmapBits */
112 EMFDRV_SetBkColor, /* pSetBkColor */
113 EMFDRV_SetBkMode, /* pSetBkMode */
114 NULL, /* pSetDCOrg */
115 NULL, /* pSetDIBColorTable */
116 NULL, /* pSetDIBits */
117 NULL, /* pSetDIBitsToDevice */
118 NULL, /* pSetDeviceClipping */
119 NULL, /* pSetDeviceGammaRamp */
120 EMFDRV_SetMapMode, /* pSetMapMode */
121 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
122 NULL, /* pSetPixel */
123 NULL, /* pSetPixelFormat */
124 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
125 EMFDRV_SetROP2, /* pSetROP2 */
126 NULL, /* pSetRelAbs */
127 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
128 EMFDRV_SetTextAlign, /* pSetTextAlign */
129 NULL, /* pSetTextCharacterExtra */
130 EMFDRV_SetTextColor, /* pSetTextColor */
131 NULL, /* pSetTextJustification */
132 EMFDRV_SetViewportExt, /* pSetViewportExt */
133 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
134 EMFDRV_SetWindowExt, /* pSetWindowExt */
135 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
136 NULL, /* pStartDoc */
137 NULL, /* pStartPage */
138 NULL, /* pStretchBlt */
139 NULL, /* pStretchDIBits */
140 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
141 EMFDRV_StrokePath, /* pStrokePath */
142 NULL, /* pSwapBuffers */
143 EMFDRV_WidenPath /* pWiddenPath */
147 /**********************************************************************
150 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
152 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
153 DC *dc = physDev->dc;
155 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
156 HeapFree( GetProcessHeap(), 0, physDev );
158 GDI_FreeObject( dc->hSelf, dc );
163 /******************************************************************
166 * Warning: this function can change the pointer to the metafile header.
168 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
172 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
174 physDev->emh->nBytes += emr->nSize;
175 physDev->emh->nRecords++;
178 TRACE("Writing record to disk\n");
179 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
182 len = physDev->emh->nBytes;
183 emh = HeapReAlloc( GetProcessHeap(), 0, physDev->emh, len );
184 if (!emh) return FALSE;
186 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
193 /******************************************************************
196 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
198 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
199 RECTL *bounds = &physDev->emh->rclBounds;
201 if(bounds->left > bounds->right) {/* first rect */
205 bounds->left = min(bounds->left, rect->left);
206 bounds->top = min(bounds->top, rect->top);
207 bounds->right = max(bounds->right, rect->right);
208 bounds->bottom = max(bounds->bottom, rect->bottom);
212 /******************************************************************
215 * Note: this function assumes that we never delete objects.
216 * If we do someday, we'll need to maintain a table to re-use deleted
219 int EMFDRV_AddHandleDC( PHYSDEV dev )
221 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
222 physDev->emh->nHandles++;
223 return physDev->nextHandle++;
227 /**********************************************************************
228 * CreateEnhMetaFileA (GDI32.@)
230 HDC WINAPI CreateEnhMetaFileA(
231 HDC hdc, /* [in] optional reference DC */
232 LPCSTR filename, /* [in] optional filename for disk metafiles */
233 const RECT *rect, /* [in] optional bounding rectangle */
234 LPCSTR description /* [in] optional description */
237 LPWSTR filenameW = NULL;
238 LPWSTR descriptionW = NULL;
240 DWORD len1, len2, total;
244 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
245 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
246 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
249 len1 = strlen(description);
250 len2 = strlen(description + len1 + 1);
251 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
252 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
253 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
256 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
259 HeapFree( GetProcessHeap(), 0, filenameW );
261 HeapFree( GetProcessHeap(), 0, descriptionW );
266 /**********************************************************************
267 * CreateEnhMetaFileW (GDI32.@)
269 HDC WINAPI CreateEnhMetaFileW(
270 HDC hdc, /* [in] optional reference DC */
271 LPCWSTR filename, /* [in] optional filename for disk metafiles */
272 const RECT* rect, /* [in] optional bounding rectangle */
273 LPCWSTR description /* [in] optional description */
278 HDC hRefDC = hdc ? hdc : CreateDCA("DISPLAY",NULL,NULL,NULL); /* If no ref, use current display */
279 EMFDRV_PDEVICE *physDev;
281 DWORD size = 0, length = 0;
283 TRACE("%s\n", debugstr_w(filename) );
285 if (!(dc = DC_AllocDC( &EMFDRV_Funcs ))) return 0;
286 dc->header.wMagic = ENHMETAFILE_DC_MAGIC;
288 physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
290 GDI_FreeObject( dc->hSelf, dc );
293 dc->physDev = (PHYSDEV)physDev;
294 physDev->hdc = dc->hSelf;
297 if(description) { /* App name\0Title\0\0 */
298 length = lstrlenW(description);
299 length += lstrlenW(description + length + 1);
303 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
305 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
306 HeapFree( GetProcessHeap(), 0, physDev );
307 GDI_FreeObject( dc->hSelf, dc );
311 physDev->nextHandle = 1;
314 physDev->emh->iType = EMR_HEADER;
315 physDev->emh->nSize = size;
317 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
318 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
321 physDev->emh->rclFrame.left = rect->left;
322 physDev->emh->rclFrame.top = rect->top;
323 physDev->emh->rclFrame.right = rect->right;
324 physDev->emh->rclFrame.bottom = rect->bottom;
325 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
326 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
327 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
330 physDev->emh->dSignature = ENHMETA_SIGNATURE;
331 physDev->emh->nVersion = 0x10000;
332 physDev->emh->nBytes = physDev->emh->nSize;
333 physDev->emh->nRecords = 1;
334 physDev->emh->nHandles = 1;
336 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
337 physDev->emh->nDescription = length / 2;
339 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
341 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
344 physDev->emh->szlDevice.cx = GetDeviceCaps( hRefDC, HORZRES );
345 physDev->emh->szlDevice.cy = GetDeviceCaps( hRefDC, VERTRES );
347 /* Size in millimeters */
348 physDev->emh->szlMillimeters.cx = GetDeviceCaps( hRefDC, HORZSIZE );
349 physDev->emh->szlMillimeters.cy = GetDeviceCaps( hRefDC, VERTSIZE );
351 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
353 if (filename) /* disk based metafile */
355 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
356 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
357 EMFDRV_DeleteDC( dc->physDev );
360 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
361 EMFDRV_DeleteDC( dc->physDev );
364 physDev->hFile = hFile;
367 TRACE("returning %04x\n", dc->hSelf);
369 GDI_ReleaseObj( dc->hSelf );
377 /******************************************************************
378 * CloseEnhMetaFile (GDI32.@)
380 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
383 EMFDRV_PDEVICE *physDev;
388 TRACE("(%04x)\n", hdc );
390 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
391 physDev = (EMFDRV_PDEVICE *)dc->physDev;
393 emr.emr.iType = EMR_EOF;
394 emr.emr.nSize = sizeof(emr);
396 emr.offPalEntries = 0;
397 emr.nSizeLast = emr.emr.nSize;
398 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
400 /* Update rclFrame if not initialized in CreateEnhMetaFile */
401 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
402 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
403 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
404 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
405 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
406 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
407 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
408 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
409 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
412 if (physDev->hFile) /* disk based metafile */
414 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
416 CloseHandle( physDev->hFile );
417 EMFDRV_DeleteDC( dc->physDev );
421 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
422 sizeof(*physDev->emh), NULL, NULL))
424 CloseHandle( physDev->hFile );
425 EMFDRV_DeleteDC( dc->physDev );
428 HeapFree( GetProcessHeap(), 0, physDev->emh );
429 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
431 TRACE("hMapping = %08x\n", hMapping );
432 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
433 TRACE("view = %p\n", physDev->emh );
434 CloseHandle( hMapping );
435 CloseHandle( physDev->hFile );
438 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
439 physDev->emh = NULL; /* So it won't be deleted */
440 EMFDRV_DeleteDC( dc->physDev );