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 struct gdi_dc_funcs EMFDRV_Funcs =
40 EMFDRV_AbortPath, /* pAbortPath */
41 NULL, /* pAlphaBlend */
43 EMFDRV_Arc, /* pArc */
45 EMFDRV_BeginPath, /* pBeginPath */
46 NULL, /* pBlendImage */
47 NULL, /* pChoosePixelFormat */
48 EMFDRV_Chord, /* pChord */
49 EMFDRV_CloseFigure, /* pCloseFigure */
50 NULL, /* pCreateBitmap */
51 NULL, /* pCreateCompatibleDC */
53 NULL, /* pDeleteBitmap */
54 EMFDRV_DeleteDC, /* pDeleteDC */
55 EMFDRV_DeleteObject, /* pDeleteObject */
56 NULL, /* pDescribePixelFormat */
57 NULL, /* pDeviceCapabilities */
58 EMFDRV_Ellipse, /* pEllipse */
61 EMFDRV_EndPath, /* pEndPath */
62 NULL, /* pEnumFonts */
63 NULL, /* pEnumICMProfiles */
64 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
65 NULL, /* pExtDeviceMode */
66 NULL, /* pExtEscape */
67 EMFDRV_ExtFloodFill, /* pExtFloodFill */
68 EMFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
69 EMFDRV_ExtTextOut, /* pExtTextOut */
70 EMFDRV_FillPath, /* pFillPath */
71 EMFDRV_FillRgn, /* pFillRgn */
72 EMFDRV_FlattenPath, /* pFlattenPath */
73 NULL, /* pFontIsLinked */
74 EMFDRV_FrameRgn, /* pFrameRgn */
75 EMFDRV_GdiComment, /* pGdiComment */
76 NULL, /* pGdiRealizationInfo */
77 NULL, /* pGetBoundsRect */
78 NULL, /* pGetCharABCWidths */
79 NULL, /* pGetCharABCWidthsI */
80 NULL, /* pGetCharWidth */
81 EMFDRV_GetDeviceCaps, /* pGetDeviceCaps */
82 NULL, /* pGetDeviceGammaRamp */
83 NULL, /* pGetFontData */
84 NULL, /* pGetFontUnicodeRanges */
85 NULL, /* pGetGlyphIndices */
86 NULL, /* pGetGlyphOutline */
87 NULL, /* pGetICMProfile */
89 NULL, /* pGetKerningPairs */
90 NULL, /* pGetNearestColor */
91 NULL, /* pGetOutlineTextMetrics */
93 NULL, /* pGetPixelFormat */
94 NULL, /* pGetSystemPaletteEntries */
95 NULL, /* pGetTextCharsetInfo */
96 NULL, /* pGetTextExtentExPoint */
97 NULL, /* pGetTextExtentExPointI */
98 NULL, /* pGetTextFace */
99 NULL, /* pGetTextMetrics */
100 NULL, /* pGradientFill */
101 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
102 EMFDRV_InvertRgn, /* pInvertRgn */
103 EMFDRV_LineTo, /* pLineTo */
104 EMFDRV_ModifyWorldTransform, /* pModifyWorldTransform */
105 EMFDRV_MoveTo, /* pMoveTo */
106 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
107 EMFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
108 EMFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
109 EMFDRV_PaintRgn, /* pPaintRgn */
110 EMFDRV_PatBlt, /* pPatBlt */
111 EMFDRV_Pie, /* pPie */
112 EMFDRV_PolyBezier, /* pPolyBezier */
113 EMFDRV_PolyBezierTo, /* pPolyBezierTo */
114 NULL, /* pPolyDraw */
115 EMFDRV_PolyPolygon, /* pPolyPolygon */
116 EMFDRV_PolyPolyline, /* pPolyPolyline */
117 EMFDRV_Polygon, /* pPolygon */
118 EMFDRV_Polyline, /* pPolyline */
119 NULL, /* pPolylineTo */
120 NULL, /* pPutImage */
121 NULL, /* pRealizeDefaultPalette */
122 NULL, /* pRealizePalette */
123 EMFDRV_Rectangle, /* pRectangle */
125 EMFDRV_RestoreDC, /* pRestoreDC */
126 EMFDRV_RoundRect, /* pRoundRect */
127 EMFDRV_SaveDC, /* pSaveDC */
128 EMFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
129 EMFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
130 EMFDRV_SelectBitmap, /* pSelectBitmap */
131 EMFDRV_SelectBrush, /* pSelectBrush */
132 EMFDRV_SelectClipPath, /* pSelectClipPath */
133 EMFDRV_SelectFont, /* pSelectFont */
134 EMFDRV_SelectPalette, /* pSelectPalette */
135 EMFDRV_SelectPen, /* pSelectPen */
136 EMFDRV_SetArcDirection, /* pSetArcDirection */
137 EMFDRV_SetBkColor, /* pSetBkColor */
138 EMFDRV_SetBkMode, /* pSetBkMode */
139 NULL, /* pSetBoundsRect */
140 EMFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
141 EMFDRV_SetDCPenColor, /* pSetDCPenColor*/
142 EMFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
143 NULL, /* pSetDeviceClipping */
144 NULL, /* pSetDeviceGammaRamp */
145 EMFDRV_SetLayout, /* pSetLayout */
146 EMFDRV_SetMapMode, /* pSetMapMode */
147 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
148 EMFDRV_SetPixel, /* pSetPixel */
149 NULL, /* pSetPixelFormat */
150 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
151 EMFDRV_SetROP2, /* pSetROP2 */
152 NULL, /* pSetRelAbs */
153 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
154 EMFDRV_SetTextAlign, /* pSetTextAlign */
155 NULL, /* pSetTextCharacterExtra */
156 EMFDRV_SetTextColor, /* pSetTextColor */
157 EMFDRV_SetTextJustification, /* pSetTextJustification */
158 EMFDRV_SetViewportExtEx, /* pSetViewportExtEx */
159 EMFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
160 EMFDRV_SetWindowExtEx, /* pSetWindowExtEx */
161 EMFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
162 EMFDRV_SetWorldTransform, /* pSetWorldTransform */
163 NULL, /* pStartDoc */
164 NULL, /* pStartPage */
165 EMFDRV_StretchBlt, /* pStretchBlt */
166 EMFDRV_StretchDIBits, /* pStretchDIBits */
167 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
168 EMFDRV_StrokePath, /* pStrokePath */
169 NULL, /* pSwapBuffers */
170 NULL, /* pUnrealizePalette */
171 EMFDRV_WidenPath, /* pWidenPath */
172 NULL, /* pwglCopyContext */
173 NULL, /* pwglCreateContext */
174 NULL, /* pwglCreateContextAttribsARB */
175 NULL, /* pwglDeleteContext */
176 NULL, /* pwglGetProcAddress */
177 NULL, /* pwglMakeContextCurrentARB */
178 NULL, /* pwglMakeCurrent */
179 NULL, /* pwglSetPixelFormatWINE */
180 NULL, /* pwglShareLists */
181 NULL, /* pwglUseFontBitmapsA */
182 NULL, /* pwglUseFontBitmapsW */
183 GDI_PRIORITY_GRAPHICS_DRV /* priority */
187 /**********************************************************************
190 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
192 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
195 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
196 for(index = 0; index < physDev->handles_size; index++)
197 if(physDev->handles[index])
198 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
199 HeapFree( GetProcessHeap(), 0, physDev->handles );
200 HeapFree( GetProcessHeap(), 0, physDev );
205 /******************************************************************
208 * Warning: this function can change the pointer to the metafile header.
210 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
214 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
216 TRACE("record %d, size %d %s\n",
217 emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
219 assert( !(emr->nSize & 3) );
221 physDev->emh->nBytes += emr->nSize;
222 physDev->emh->nRecords++;
225 if (!WriteFile(physDev->hFile, emr, emr->nSize, NULL, NULL))
228 DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
229 len = physDev->emh->nBytes;
230 if (len > nEmfSize) {
231 nEmfSize += (nEmfSize / 2) + emr->nSize;
232 emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
233 if (!emh) return FALSE;
236 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
243 /******************************************************************
246 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
248 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
249 RECTL *bounds = &physDev->emh->rclBounds;
250 RECTL vportRect = *rect;
252 LPtoDP( dev->hdc, (LPPOINT)&vportRect, 2 );
254 /* The coordinate systems may be mirrored
255 (LPtoDP handles points, not rectangles) */
256 if (vportRect.left > vportRect.right)
258 LONG temp = vportRect.right;
259 vportRect.right = vportRect.left;
260 vportRect.left = temp;
262 if (vportRect.top > vportRect.bottom)
264 LONG temp = vportRect.bottom;
265 vportRect.bottom = vportRect.top;
266 vportRect.top = temp;
269 if (bounds->left > bounds->right)
271 /* first bounding rectangle */
276 bounds->left = min(bounds->left, vportRect.left);
277 bounds->top = min(bounds->top, vportRect.top);
278 bounds->right = max(bounds->right, vportRect.right);
279 bounds->bottom = max(bounds->bottom, vportRect.bottom);
283 /**********************************************************************
284 * CreateEnhMetaFileA (GDI32.@)
286 HDC WINAPI CreateEnhMetaFileA(
287 HDC hdc, /* [in] optional reference DC */
288 LPCSTR filename, /* [in] optional filename for disk metafiles */
289 const RECT *rect, /* [in] optional bounding rectangle */
290 LPCSTR description /* [in] optional description */
293 LPWSTR filenameW = NULL;
294 LPWSTR descriptionW = NULL;
296 DWORD len1, len2, total;
300 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
301 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
302 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
305 len1 = strlen(description);
306 len2 = strlen(description + len1 + 1);
307 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
308 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
309 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
312 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
314 HeapFree( GetProcessHeap(), 0, filenameW );
315 HeapFree( GetProcessHeap(), 0, descriptionW );
320 /**********************************************************************
321 * CreateEnhMetaFileW (GDI32.@)
323 HDC WINAPI CreateEnhMetaFileW(
324 HDC hdc, /* [in] optional reference DC */
325 LPCWSTR filename, /* [in] optional filename for disk metafiles */
326 const RECT* rect, /* [in] optional bounding rectangle */
327 LPCWSTR description /* [in] optional description */
330 static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
333 EMFDRV_PDEVICE *physDev;
335 DWORD size = 0, length = 0;
337 TRACE("%s\n", debugstr_w(filename) );
339 if (!(dc = alloc_dc_ptr( OBJ_ENHMETADC ))) return 0;
341 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
346 if(description) { /* App name\0Title\0\0 */
347 length = lstrlenW(description);
348 length += lstrlenW(description + length + 1);
352 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
354 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
355 HeapFree( GetProcessHeap(), 0, physDev );
360 push_dc_driver( &dc->physDev, &physDev->dev, &EMFDRV_Funcs );
362 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
363 physDev->handles_size = HANDLE_LIST_INC;
364 physDev->cur_handles = 1;
366 physDev->dc_brush = 0;
368 physDev->screen_dc = 0;
369 physDev->restoring = 0;
370 if (hdc) /* if no ref, use current display */
371 physDev->ref_dc = hdc;
373 physDev->ref_dc = physDev->screen_dc = CreateDCW( displayW, NULL, NULL, NULL );
375 SetVirtualResolution(physDev->dev.hdc, 0, 0, 0, 0);
377 physDev->emh->iType = EMR_HEADER;
378 physDev->emh->nSize = size;
380 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
381 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
384 physDev->emh->rclFrame.left = rect->left;
385 physDev->emh->rclFrame.top = rect->top;
386 physDev->emh->rclFrame.right = rect->right;
387 physDev->emh->rclFrame.bottom = rect->bottom;
388 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
389 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
390 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
393 physDev->emh->dSignature = ENHMETA_SIGNATURE;
394 physDev->emh->nVersion = 0x10000;
395 physDev->emh->nBytes = physDev->emh->nSize;
396 physDev->emh->nRecords = 1;
397 physDev->emh->nHandles = 1;
399 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
400 physDev->emh->nDescription = length / 2;
402 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
404 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
407 physDev->emh->szlDevice.cx = GetDeviceCaps( physDev->ref_dc, HORZRES );
408 physDev->emh->szlDevice.cy = GetDeviceCaps( physDev->ref_dc, VERTRES );
410 /* Size in millimeters */
411 physDev->emh->szlMillimeters.cx = GetDeviceCaps( physDev->ref_dc, HORZSIZE );
412 physDev->emh->szlMillimeters.cy = GetDeviceCaps( physDev->ref_dc, VERTSIZE );
414 /* Size in micrometers */
415 physDev->emh->szlMicrometers.cx = physDev->emh->szlMillimeters.cx * 1000;
416 physDev->emh->szlMicrometers.cy = physDev->emh->szlMillimeters.cy * 1000;
418 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
420 if (filename) /* disk based metafile */
422 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
423 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
427 if (!WriteFile( hFile, physDev->emh, size, NULL, NULL )) {
431 physDev->hFile = hFile;
434 TRACE("returning %p\n", physDev->dev.hdc);
435 ret = physDev->dev.hdc;
436 release_dc_ptr( dc );
441 /******************************************************************
442 * CloseEnhMetaFile (GDI32.@)
444 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
447 EMFDRV_PDEVICE *physDev;
452 TRACE("(%p)\n", hdc );
454 if (!(dc = get_dc_ptr( hdc ))) return NULL;
455 if (dc->header.type != OBJ_ENHMETADC)
457 release_dc_ptr( dc );
460 if (dc->refcount != 1)
462 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
463 release_dc_ptr( dc );
466 physDev = (EMFDRV_PDEVICE *)dc->physDev;
471 if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
472 if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
473 if (physDev->screen_dc) DeleteDC( physDev->screen_dc );
475 emr.emr.iType = EMR_EOF;
476 emr.emr.nSize = sizeof(emr);
478 emr.offPalEntries = FIELD_OFFSET(EMREOF, nSizeLast);
479 emr.nSizeLast = emr.emr.nSize;
480 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
482 /* Update rclFrame if not initialized in CreateEnhMetaFile */
483 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
484 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
485 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
486 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
487 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
488 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
489 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
490 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
491 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
494 if (physDev->hFile) /* disk based metafile */
496 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
498 CloseHandle( physDev->hFile );
503 if (!WriteFile(physDev->hFile, physDev->emh, sizeof(*physDev->emh),
506 CloseHandle( physDev->hFile );
510 HeapFree( GetProcessHeap(), 0, physDev->emh );
511 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
513 TRACE("hMapping = %p\n", hMapping );
514 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
515 TRACE("view = %p\n", physDev->emh );
516 CloseHandle( hMapping );
517 CloseHandle( physDev->hFile );
520 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
521 physDev->emh = NULL; /* So it won't be deleted */