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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "gdi_private.h"
30 #include "enhmfdrv/enhmetafiledrv.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
35 static BOOL EMFDRV_DeleteDC( PHYSDEV dev );
37 static const DC_FUNCTIONS EMFDRV_Funcs =
40 EMFDRV_AbortPath, /* pAbortPath */
41 NULL, /* pAlphaBlend */
43 EMFDRV_Arc, /* pArc */
45 EMFDRV_BeginPath, /* pBeginPath */
46 NULL, /* pChoosePixelFormat */
47 EMFDRV_Chord, /* pChord */
48 EMFDRV_CloseFigure, /* pCloseFigure */
49 NULL, /* pCreateBitmap */
51 NULL, /* pCreateDIBSection */
52 NULL, /* pDeleteBitmap */
53 EMFDRV_DeleteDC, /* pDeleteDC */
54 EMFDRV_DeleteObject, /* pDeleteObject */
55 NULL, /* pDescribePixelFormat */
56 NULL, /* pDeviceCapabilities */
57 EMFDRV_Ellipse, /* pEllipse */
60 EMFDRV_EndPath, /* pEndPath */
61 NULL, /* pEnumDeviceFonts */
62 NULL, /* pEnumICMProfiles */
63 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
64 NULL, /* pExtDeviceMode */
65 NULL, /* pExtEscape */
66 EMFDRV_ExtFloodFill, /* pExtFloodFill */
67 EMFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
68 EMFDRV_ExtTextOut, /* pExtTextOut */
69 EMFDRV_FillPath, /* pFillPath */
70 EMFDRV_FillRgn, /* pFillRgn */
71 EMFDRV_FlattenPath, /* pFlattenPath */
72 EMFDRV_FrameRgn, /* pFrameRgn */
73 EMFDRV_GdiComment, /* pGdiComment */
74 NULL, /* pGetCharWidth */
75 EMFDRV_GetDeviceCaps, /* pGetDeviceCaps */
76 NULL, /* pGetDeviceGammaRamp */
77 NULL, /* pGetICMProfile */
79 NULL, /* pGetNearestColor */
81 NULL, /* pGetPixelFormat */
82 NULL, /* pGetSystemPaletteEntries */
83 NULL, /* pGetTextExtentExPoint */
84 NULL, /* pGetTextMetrics */
85 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
86 EMFDRV_InvertRgn, /* pInvertRgn */
87 EMFDRV_LineTo, /* pLineTo */
88 EMFDRV_ModifyWorldTransform, /* pModifyWorldTransform */
89 EMFDRV_MoveTo, /* pMoveTo */
90 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
91 EMFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
92 EMFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
93 EMFDRV_PaintRgn, /* pPaintRgn */
94 EMFDRV_PatBlt, /* pPatBlt */
95 EMFDRV_Pie, /* pPie */
96 NULL, /* pPolyBezier */
97 NULL, /* pPolyBezierTo */
99 EMFDRV_PolyPolygon, /* pPolyPolygon */
100 EMFDRV_PolyPolyline, /* pPolyPolyline */
101 EMFDRV_Polygon, /* pPolygon */
102 EMFDRV_Polyline, /* pPolyline */
103 NULL, /* pPolylineTo */
104 NULL, /* pPutImage */
105 NULL, /* pRealizeDefaultPalette */
106 NULL, /* pRealizePalette */
107 EMFDRV_Rectangle, /* pRectangle */
109 EMFDRV_RestoreDC, /* pRestoreDC */
110 EMFDRV_RoundRect, /* pRoundRect */
111 EMFDRV_SaveDC, /* pSaveDC */
112 EMFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
113 EMFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
114 EMFDRV_SelectBitmap, /* pSelectBitmap */
115 EMFDRV_SelectBrush, /* pSelectBrush */
116 EMFDRV_SelectClipPath, /* pSelectClipPath */
117 EMFDRV_SelectFont, /* pSelectFont */
118 EMFDRV_SelectPalette, /* pSelectPalette */
119 EMFDRV_SelectPen, /* pSelectPen */
120 EMFDRV_SetArcDirection, /* pSetArcDirection */
121 NULL, /* pSetBitmapBits */
122 EMFDRV_SetBkColor, /* pSetBkColor */
123 EMFDRV_SetBkMode, /* pSetBkMode */
124 EMFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
125 EMFDRV_SetDCPenColor, /* pSetDCPenColor*/
126 NULL, /* pSetDIBColorTable */
127 EMFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
128 NULL, /* pSetDeviceClipping */
129 NULL, /* pSetDeviceGammaRamp */
130 EMFDRV_SetLayout, /* pSetLayout */
131 EMFDRV_SetMapMode, /* pSetMapMode */
132 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
133 EMFDRV_SetPixel, /* pSetPixel */
134 NULL, /* pSetPixelFormat */
135 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
136 EMFDRV_SetROP2, /* pSetROP2 */
137 NULL, /* pSetRelAbs */
138 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
139 EMFDRV_SetTextAlign, /* pSetTextAlign */
140 NULL, /* pSetTextCharacterExtra */
141 EMFDRV_SetTextColor, /* pSetTextColor */
142 EMFDRV_SetTextJustification, /* pSetTextJustification */
143 EMFDRV_SetViewportExtEx, /* pSetViewportExtEx */
144 EMFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
145 EMFDRV_SetWindowExtEx, /* pSetWindowExtEx */
146 EMFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
147 EMFDRV_SetWorldTransform, /* pSetWorldTransform */
148 NULL, /* pStartDoc */
149 NULL, /* pStartPage */
150 EMFDRV_StretchBlt, /* pStretchBlt */
151 EMFDRV_StretchDIBits, /* pStretchDIBits */
152 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
153 EMFDRV_StrokePath, /* pStrokePath */
154 NULL, /* pSwapBuffers */
155 NULL, /* pUnrealizePalette */
156 EMFDRV_WidenPath /* pWidenPath */
160 /**********************************************************************
163 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
165 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
168 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
169 for(index = 0; index < physDev->handles_size; index++)
170 if(physDev->handles[index])
171 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
172 HeapFree( GetProcessHeap(), 0, physDev->handles );
173 HeapFree( GetProcessHeap(), 0, physDev );
178 /******************************************************************
181 * Warning: this function can change the pointer to the metafile header.
183 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
187 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
189 TRACE("record %d, size %d %s\n",
190 emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
192 assert( !(emr->nSize & 3) );
194 physDev->emh->nBytes += emr->nSize;
195 physDev->emh->nRecords++;
198 if (!WriteFile(physDev->hFile, emr, emr->nSize, NULL, NULL))
201 DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
202 len = physDev->emh->nBytes;
203 if (len > nEmfSize) {
204 nEmfSize += (nEmfSize / 2) + emr->nSize;
205 emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
206 if (!emh) return FALSE;
209 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
216 /******************************************************************
219 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
221 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
222 RECTL *bounds = &physDev->emh->rclBounds;
223 RECTL vportRect = *rect;
225 LPtoDP( dev->hdc, (LPPOINT)&vportRect, 2 );
227 /* The coordinate systems may be mirrored
228 (LPtoDP handles points, not rectangles) */
229 if (vportRect.left > vportRect.right)
231 LONG temp = vportRect.right;
232 vportRect.right = vportRect.left;
233 vportRect.left = temp;
235 if (vportRect.top > vportRect.bottom)
237 LONG temp = vportRect.bottom;
238 vportRect.bottom = vportRect.top;
239 vportRect.top = temp;
242 if (bounds->left > bounds->right)
244 /* first bounding rectangle */
249 bounds->left = min(bounds->left, vportRect.left);
250 bounds->top = min(bounds->top, vportRect.top);
251 bounds->right = max(bounds->right, vportRect.right);
252 bounds->bottom = max(bounds->bottom, vportRect.bottom);
256 /**********************************************************************
257 * CreateEnhMetaFileA (GDI32.@)
259 HDC WINAPI CreateEnhMetaFileA(
260 HDC hdc, /* [in] optional reference DC */
261 LPCSTR filename, /* [in] optional filename for disk metafiles */
262 const RECT *rect, /* [in] optional bounding rectangle */
263 LPCSTR description /* [in] optional description */
266 LPWSTR filenameW = NULL;
267 LPWSTR descriptionW = NULL;
269 DWORD len1, len2, total;
273 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
274 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
275 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
278 len1 = strlen(description);
279 len2 = strlen(description + len1 + 1);
280 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
281 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
282 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
285 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
287 HeapFree( GetProcessHeap(), 0, filenameW );
288 HeapFree( GetProcessHeap(), 0, descriptionW );
293 /**********************************************************************
294 * CreateEnhMetaFileW (GDI32.@)
296 HDC WINAPI CreateEnhMetaFileW(
297 HDC hdc, /* [in] optional reference DC */
298 LPCWSTR filename, /* [in] optional filename for disk metafiles */
299 const RECT* rect, /* [in] optional bounding rectangle */
300 LPCWSTR description /* [in] optional description */
303 static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
306 HDC hRefDC = hdc ? hdc : CreateDCW(displayW,NULL,NULL,NULL);
307 /* If no ref, use current display */
308 EMFDRV_PDEVICE *physDev;
310 DWORD size = 0, length = 0;
312 TRACE("%s\n", debugstr_w(filename) );
314 if (!(dc = alloc_dc_ptr( OBJ_ENHMETADC ))) return 0;
316 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
321 if(description) { /* App name\0Title\0\0 */
322 length = lstrlenW(description);
323 length += lstrlenW(description + length + 1);
327 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
329 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
330 HeapFree( GetProcessHeap(), 0, physDev );
335 push_dc_driver( dc, &physDev->dev, &EMFDRV_Funcs );
337 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
338 physDev->handles_size = HANDLE_LIST_INC;
339 physDev->cur_handles = 1;
341 physDev->dc_brush = 0;
343 physDev->horzres = GetDeviceCaps(hRefDC, HORZRES);
344 physDev->vertres = GetDeviceCaps(hRefDC, VERTRES);
345 physDev->logpixelsx = GetDeviceCaps(hRefDC, LOGPIXELSX);
346 physDev->logpixelsy = GetDeviceCaps(hRefDC, LOGPIXELSY);
347 physDev->horzsize = GetDeviceCaps(hRefDC, HORZSIZE);
348 physDev->vertsize = GetDeviceCaps(hRefDC, VERTSIZE);
349 physDev->bitspixel = GetDeviceCaps(hRefDC, BITSPIXEL);
350 physDev->textcaps = GetDeviceCaps(hRefDC, TEXTCAPS);
351 physDev->rastercaps = GetDeviceCaps(hRefDC, RASTERCAPS);
352 physDev->technology = GetDeviceCaps(hRefDC, TECHNOLOGY);
353 physDev->planes = GetDeviceCaps(hRefDC, PLANES);
354 physDev->numcolors = GetDeviceCaps(hRefDC, NUMCOLORS);
355 physDev->restoring = 0;
357 SetVirtualResolution(physDev->dev.hdc, 0, 0, 0, 0);
359 physDev->emh->iType = EMR_HEADER;
360 physDev->emh->nSize = size;
362 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
363 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
366 physDev->emh->rclFrame.left = rect->left;
367 physDev->emh->rclFrame.top = rect->top;
368 physDev->emh->rclFrame.right = rect->right;
369 physDev->emh->rclFrame.bottom = rect->bottom;
370 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
371 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
372 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
375 physDev->emh->dSignature = ENHMETA_SIGNATURE;
376 physDev->emh->nVersion = 0x10000;
377 physDev->emh->nBytes = physDev->emh->nSize;
378 physDev->emh->nRecords = 1;
379 physDev->emh->nHandles = 1;
381 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
382 physDev->emh->nDescription = length / 2;
384 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
386 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
389 physDev->emh->szlDevice.cx = physDev->horzres;
390 physDev->emh->szlDevice.cy = physDev->vertres;
392 /* Size in millimeters */
393 physDev->emh->szlMillimeters.cx = physDev->horzsize;
394 physDev->emh->szlMillimeters.cy = physDev->vertsize;
396 /* Size in micrometers */
397 physDev->emh->szlMicrometers.cx = physDev->horzsize * 1000;
398 physDev->emh->szlMicrometers.cy = physDev->vertsize * 1000;
400 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
402 if (filename) /* disk based metafile */
404 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
405 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
409 if (!WriteFile( hFile, physDev->emh, size, NULL, NULL )) {
413 physDev->hFile = hFile;
416 TRACE("returning %p\n", physDev->dev.hdc);
417 ret = physDev->dev.hdc;
418 release_dc_ptr( dc );
426 /******************************************************************
427 * CloseEnhMetaFile (GDI32.@)
429 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
432 EMFDRV_PDEVICE *physDev;
437 TRACE("(%p)\n", hdc );
439 if (!(dc = get_dc_ptr( hdc ))) return NULL;
440 if (dc->header.type != OBJ_ENHMETADC)
442 release_dc_ptr( dc );
445 if (dc->refcount != 1)
447 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
448 release_dc_ptr( dc );
451 physDev = (EMFDRV_PDEVICE *)dc->physDev;
456 if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
457 if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
459 emr.emr.iType = EMR_EOF;
460 emr.emr.nSize = sizeof(emr);
462 emr.offPalEntries = FIELD_OFFSET(EMREOF, nSizeLast);
463 emr.nSizeLast = emr.emr.nSize;
464 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
466 /* Update rclFrame if not initialized in CreateEnhMetaFile */
467 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
468 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
469 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
470 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
471 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
472 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
473 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
474 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
475 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
478 if (physDev->hFile) /* disk based metafile */
480 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
482 CloseHandle( physDev->hFile );
487 if (!WriteFile(physDev->hFile, physDev->emh, sizeof(*physDev->emh),
490 CloseHandle( physDev->hFile );
494 HeapFree( GetProcessHeap(), 0, physDev->emh );
495 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
497 TRACE("hMapping = %p\n", hMapping );
498 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
499 TRACE("view = %p\n", physDev->emh );
500 CloseHandle( hMapping );
501 CloseHandle( physDev->hFile );
504 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
505 physDev->emh = NULL; /* So it won't be deleted */