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
28 #include "enhmfdrv/enhmetafiledrv.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
33 static const DC_FUNCTIONS EMFDRV_Funcs =
36 EMFDRV_AbortPath, /* pAbortPath */
38 EMFDRV_Arc, /* pArc */
40 EMFDRV_BeginPath, /* pBeginPath */
41 EMFDRV_BitBlt, /* pBitBlt */
42 NULL, /* pChoosePixelFormat */
43 EMFDRV_Chord, /* pChord */
44 EMFDRV_CloseFigure, /* pCloseFigure */
45 NULL, /* pCreateBitmap */
47 NULL, /* pCreateDIBSection */
48 NULL, /* pDeleteBitmap */
50 EMFDRV_DeleteObject, /* pDeleteObject */
51 NULL, /* pDescribePixelFormat */
52 NULL, /* pDeviceCapabilities */
53 EMFDRV_Ellipse, /* pEllipse */
56 EMFDRV_EndPath, /* pEndPath */
57 NULL, /* pEnumDeviceFonts */
58 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
59 NULL, /* pExtDeviceMode */
60 NULL, /* pExtEscape */
61 EMFDRV_ExtFloodFill, /* pExtFloodFill */
62 EMFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
63 EMFDRV_ExtTextOut, /* pExtTextOut */
64 EMFDRV_FillPath, /* pFillPath */
65 EMFDRV_FillRgn, /* pFillRgn */
66 EMFDRV_FlattenPath, /* pFlattenPath */
67 EMFDRV_FrameRgn, /* pFrameRgn */
68 EMFDRV_GdiComment, /* pGdiComment */
69 NULL, /* pGetBitmapBits */
70 NULL, /* pGetCharWidth */
71 NULL, /* pGetDCOrgEx */
72 NULL, /* pGetDIBColorTable */
73 NULL, /* pGetDIBits */
74 EMFDRV_GetDeviceCaps, /* pGetDeviceCaps */
75 NULL, /* pGetDeviceGammaRamp */
76 NULL, /* pGetNearestColor */
78 NULL, /* pGetPixelFormat */
79 NULL, /* pGetSystemPaletteEntries */
80 NULL, /* pGetTextExtentPoint */
81 NULL, /* pGetTextMetrics */
82 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
83 EMFDRV_InvertRgn, /* pInvertRgn */
84 EMFDRV_LineTo, /* pLineTo */
85 EMFDRV_ModifyWorldTransform, /* pModifyWorldTransform */
86 EMFDRV_MoveTo, /* pMoveTo */
87 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
88 EMFDRV_OffsetViewportOrg, /* pOffsetViewportOrg */
89 EMFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
90 EMFDRV_PaintRgn, /* pPaintRgn */
91 EMFDRV_PatBlt, /* pPatBlt */
92 EMFDRV_Pie, /* pPie */
93 NULL, /* pPolyBezier */
94 NULL, /* pPolyBezierTo */
96 EMFDRV_PolyPolygon, /* pPolyPolygon */
97 EMFDRV_PolyPolyline, /* pPolyPolyline */
98 EMFDRV_Polygon, /* pPolygon */
99 EMFDRV_Polyline, /* pPolyline */
100 NULL, /* pPolylineTo */
101 NULL, /* pRealizeDefaultPalette */
102 NULL, /* pRealizePalette */
103 EMFDRV_Rectangle, /* pRectangle */
105 EMFDRV_RestoreDC, /* pRestoreDC */
106 EMFDRV_RoundRect, /* pRoundRect */
107 EMFDRV_SaveDC, /* pSaveDC */
108 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
109 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
110 EMFDRV_SelectBitmap, /* pSelectBitmap */
111 EMFDRV_SelectBrush, /* pSelectBrush */
112 EMFDRV_SelectClipPath, /* pSelectClipPath */
113 EMFDRV_SelectFont, /* pSelectFont */
114 NULL, /* pSelectPalette */
115 EMFDRV_SelectPen, /* pSelectPen */
116 EMFDRV_SetArcDirection, /* pSetArcDirection */
117 NULL, /* pSetBitmapBits */
118 EMFDRV_SetBkColor, /* pSetBkColor */
119 EMFDRV_SetBkMode, /* pSetBkMode */
120 NULL, /* pSetDCBrushColor */
121 NULL, /* pSetDCOrg */
122 NULL, /* pSetDCPenColor */
123 NULL, /* pSetDIBColorTable */
124 NULL, /* pSetDIBits */
125 EMFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
126 NULL, /* pSetDeviceClipping */
127 NULL, /* pSetDeviceGammaRamp */
128 EMFDRV_SetMapMode, /* pSetMapMode */
129 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
130 NULL, /* pSetPixel */
131 NULL, /* pSetPixelFormat */
132 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
133 EMFDRV_SetROP2, /* pSetROP2 */
134 NULL, /* pSetRelAbs */
135 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
136 EMFDRV_SetTextAlign, /* pSetTextAlign */
137 NULL, /* pSetTextCharacterExtra */
138 EMFDRV_SetTextColor, /* pSetTextColor */
139 EMFDRV_SetTextJustification, /* pSetTextJustification */
140 EMFDRV_SetViewportExt, /* pSetViewportExt */
141 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
142 EMFDRV_SetWindowExt, /* pSetWindowExt */
143 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
144 EMFDRV_SetWorldTransform, /* pSetWorldTransform */
145 NULL, /* pStartDoc */
146 NULL, /* pStartPage */
147 EMFDRV_StretchBlt, /* pStretchBlt */
148 EMFDRV_StretchDIBits, /* pStretchDIBits */
149 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
150 EMFDRV_StrokePath, /* pStrokePath */
151 NULL, /* pSwapBuffers */
152 EMFDRV_WidenPath /* pWidenPath */
156 /**********************************************************************
159 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
161 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
162 DC *dc = physDev->dc;
165 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
166 for(index = 0; index < physDev->handles_size; index++)
167 if(physDev->handles[index])
168 GDI_hdc_not_using_object(physDev->handles[index], physDev->hdc);
169 HeapFree( GetProcessHeap(), 0, physDev->handles );
170 HeapFree( GetProcessHeap(), 0, physDev );
172 GDI_FreeObject( dc->hSelf, dc );
177 /******************************************************************
180 * Warning: this function can change the pointer to the metafile header.
182 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
186 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
188 physDev->emh->nBytes += emr->nSize;
189 physDev->emh->nRecords++;
192 TRACE("Writing record to disk\n");
193 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
196 DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
197 len = physDev->emh->nBytes;
198 if (len > nEmfSize) {
199 nEmfSize += (nEmfSize / 2) + emr->nSize;
200 emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
201 if (!emh) return FALSE;
204 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
211 /******************************************************************
214 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
216 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
217 RECTL *bounds = &physDev->emh->rclBounds;
218 RECTL vportRect = *rect;
220 LPtoDP(physDev->hdc, (LPPOINT)&vportRect, 2);
222 if(bounds->left > bounds->right) {/* first rect */
226 bounds->left = min(bounds->left, vportRect.left);
227 bounds->top = min(bounds->top, vportRect.top);
228 bounds->right = max(bounds->right, vportRect.right);
229 bounds->bottom = max(bounds->bottom, vportRect.bottom);
233 /**********************************************************************
234 * CreateEnhMetaFileA (GDI32.@)
236 HDC WINAPI CreateEnhMetaFileA(
237 HDC hdc, /* [in] optional reference DC */
238 LPCSTR filename, /* [in] optional filename for disk metafiles */
239 const RECT *rect, /* [in] optional bounding rectangle */
240 LPCSTR description /* [in] optional description */
243 LPWSTR filenameW = NULL;
244 LPWSTR descriptionW = NULL;
246 DWORD len1, len2, total;
250 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
251 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
252 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
255 len1 = strlen(description);
256 len2 = strlen(description + len1 + 1);
257 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
258 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
259 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
262 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
265 HeapFree( GetProcessHeap(), 0, filenameW );
267 HeapFree( GetProcessHeap(), 0, descriptionW );
272 /**********************************************************************
273 * CreateEnhMetaFileW (GDI32.@)
275 HDC WINAPI CreateEnhMetaFileW(
276 HDC hdc, /* [in] optional reference DC */
277 LPCWSTR filename, /* [in] optional filename for disk metafiles */
278 const RECT* rect, /* [in] optional bounding rectangle */
279 LPCWSTR description /* [in] optional description */
282 static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
285 HDC hRefDC = hdc ? hdc : CreateDCW(displayW,NULL,NULL,NULL);
286 /* If no ref, use current display */
287 EMFDRV_PDEVICE *physDev;
289 DWORD size = 0, length = 0;
291 TRACE("%s\n", debugstr_w(filename) );
293 if (!(dc = DC_AllocDC( &EMFDRV_Funcs, ENHMETAFILE_DC_MAGIC ))) return 0;
295 physDev = (EMFDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
297 GDI_FreeObject( dc->hSelf, dc );
300 dc->physDev = (PHYSDEV)physDev;
301 physDev->hdc = dc->hSelf;
304 if(description) { /* App name\0Title\0\0 */
305 length = lstrlenW(description);
306 length += lstrlenW(description + length + 1);
310 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
312 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
313 HeapFree( GetProcessHeap(), 0, physDev );
314 GDI_FreeObject( dc->hSelf, dc );
318 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
319 physDev->handles_size = HANDLE_LIST_INC;
320 physDev->cur_handles = 1;
323 physDev->horzres = GetDeviceCaps(hRefDC, HORZRES);
324 physDev->vertres = GetDeviceCaps(hRefDC, VERTRES);
325 physDev->logpixelsx = GetDeviceCaps(hRefDC, LOGPIXELSX);
326 physDev->logpixelsy = GetDeviceCaps(hRefDC, LOGPIXELSY);
327 physDev->horzsize = GetDeviceCaps(hRefDC, HORZSIZE);
328 physDev->vertsize = GetDeviceCaps(hRefDC, VERTSIZE);
329 physDev->bitspixel = GetDeviceCaps(hRefDC, BITSPIXEL);
330 physDev->textcaps = GetDeviceCaps(hRefDC, TEXTCAPS);
331 physDev->rastercaps = GetDeviceCaps(hRefDC, RASTERCAPS);
332 physDev->technology = GetDeviceCaps(hRefDC, TECHNOLOGY);
333 physDev->planes = GetDeviceCaps(hRefDC, PLANES);
335 physDev->emh->iType = EMR_HEADER;
336 physDev->emh->nSize = size;
338 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
339 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
342 physDev->emh->rclFrame.left = rect->left;
343 physDev->emh->rclFrame.top = rect->top;
344 physDev->emh->rclFrame.right = rect->right;
345 physDev->emh->rclFrame.bottom = rect->bottom;
346 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
347 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
348 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
351 physDev->emh->dSignature = ENHMETA_SIGNATURE;
352 physDev->emh->nVersion = 0x10000;
353 physDev->emh->nBytes = physDev->emh->nSize;
354 physDev->emh->nRecords = 1;
355 physDev->emh->nHandles = 1;
357 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
358 physDev->emh->nDescription = length / 2;
360 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
362 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
365 physDev->emh->szlDevice.cx = physDev->horzres;
366 physDev->emh->szlDevice.cy = physDev->vertres;
368 /* Size in millimeters */
369 physDev->emh->szlMillimeters.cx = physDev->horzsize;
370 physDev->emh->szlMillimeters.cy = physDev->vertsize;
372 /* Size in micrometers */
373 physDev->emh->szlMicrometers.cx = physDev->horzsize * 1000;
374 physDev->emh->szlMicrometers.cy = physDev->vertsize * 1000;
376 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
378 if (filename) /* disk based metafile */
380 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
381 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
382 EMFDRV_DeleteDC( dc->physDev );
385 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
386 EMFDRV_DeleteDC( dc->physDev );
389 physDev->hFile = hFile;
392 TRACE("returning %p\n", dc->hSelf);
394 GDI_ReleaseObj( dc->hSelf );
402 /******************************************************************
403 * CloseEnhMetaFile (GDI32.@)
405 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
408 EMFDRV_PDEVICE *physDev;
413 TRACE("(%p)\n", hdc );
415 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
416 physDev = (EMFDRV_PDEVICE *)dc->physDev;
418 emr.emr.iType = EMR_EOF;
419 emr.emr.nSize = sizeof(emr);
421 emr.offPalEntries = 0;
422 emr.nSizeLast = emr.emr.nSize;
423 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
425 /* Update rclFrame if not initialized in CreateEnhMetaFile */
426 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
427 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
428 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
429 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
430 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
431 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
432 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
433 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
434 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
437 if (physDev->hFile) /* disk based metafile */
439 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
441 CloseHandle( physDev->hFile );
442 EMFDRV_DeleteDC( dc->physDev );
446 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
447 sizeof(*physDev->emh), NULL, NULL))
449 CloseHandle( physDev->hFile );
450 EMFDRV_DeleteDC( dc->physDev );
453 HeapFree( GetProcessHeap(), 0, physDev->emh );
454 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
456 TRACE("hMapping = %p\n", hMapping );
457 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
458 TRACE("view = %p\n", physDev->emh );
459 CloseHandle( hMapping );
460 CloseHandle( physDev->hFile );
463 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
464 physDev->emh = NULL; /* So it won't be deleted */
465 EMFDRV_DeleteDC( dc->physDev );