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, /* pGetBitmapBits */
75 NULL, /* pGetCharWidth */
76 NULL, /* pGetDIBits */
77 EMFDRV_GetDeviceCaps, /* pGetDeviceCaps */
78 NULL, /* pGetDeviceGammaRamp */
79 NULL, /* pGetICMProfile */
81 NULL, /* pGetNearestColor */
83 NULL, /* pGetPixelFormat */
84 NULL, /* pGetSystemPaletteEntries */
85 NULL, /* pGetTextExtentExPoint */
86 NULL, /* pGetTextMetrics */
87 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
88 EMFDRV_InvertRgn, /* pInvertRgn */
89 EMFDRV_LineTo, /* pLineTo */
90 EMFDRV_ModifyWorldTransform, /* pModifyWorldTransform */
91 EMFDRV_MoveTo, /* pMoveTo */
92 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
93 EMFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
94 EMFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
95 EMFDRV_PaintRgn, /* pPaintRgn */
96 EMFDRV_PatBlt, /* pPatBlt */
97 EMFDRV_Pie, /* pPie */
98 NULL, /* pPolyBezier */
99 NULL, /* pPolyBezierTo */
100 NULL, /* pPolyDraw */
101 EMFDRV_PolyPolygon, /* pPolyPolygon */
102 EMFDRV_PolyPolyline, /* pPolyPolyline */
103 EMFDRV_Polygon, /* pPolygon */
104 EMFDRV_Polyline, /* pPolyline */
105 NULL, /* pPolylineTo */
106 NULL, /* pPutImage */
107 NULL, /* pRealizeDefaultPalette */
108 NULL, /* pRealizePalette */
109 EMFDRV_Rectangle, /* pRectangle */
111 EMFDRV_RestoreDC, /* pRestoreDC */
112 EMFDRV_RoundRect, /* pRoundRect */
113 EMFDRV_SaveDC, /* pSaveDC */
114 EMFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
115 EMFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
116 EMFDRV_SelectBitmap, /* pSelectBitmap */
117 EMFDRV_SelectBrush, /* pSelectBrush */
118 EMFDRV_SelectClipPath, /* pSelectClipPath */
119 EMFDRV_SelectFont, /* pSelectFont */
120 EMFDRV_SelectPalette, /* pSelectPalette */
121 EMFDRV_SelectPen, /* pSelectPen */
122 EMFDRV_SetArcDirection, /* pSetArcDirection */
123 NULL, /* pSetBitmapBits */
124 EMFDRV_SetBkColor, /* pSetBkColor */
125 EMFDRV_SetBkMode, /* pSetBkMode */
126 EMFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
127 EMFDRV_SetDCPenColor, /* pSetDCPenColor*/
128 NULL, /* pSetDIBColorTable */
129 NULL, /* pSetDIBits */
130 EMFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
131 NULL, /* pSetDeviceClipping */
132 NULL, /* pSetDeviceGammaRamp */
133 EMFDRV_SetLayout, /* pSetLayout */
134 EMFDRV_SetMapMode, /* pSetMapMode */
135 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
136 EMFDRV_SetPixel, /* pSetPixel */
137 NULL, /* pSetPixelFormat */
138 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
139 EMFDRV_SetROP2, /* pSetROP2 */
140 NULL, /* pSetRelAbs */
141 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
142 EMFDRV_SetTextAlign, /* pSetTextAlign */
143 NULL, /* pSetTextCharacterExtra */
144 EMFDRV_SetTextColor, /* pSetTextColor */
145 EMFDRV_SetTextJustification, /* pSetTextJustification */
146 EMFDRV_SetViewportExtEx, /* pSetViewportExtEx */
147 EMFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
148 EMFDRV_SetWindowExtEx, /* pSetWindowExtEx */
149 EMFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
150 EMFDRV_SetWorldTransform, /* pSetWorldTransform */
151 NULL, /* pStartDoc */
152 NULL, /* pStartPage */
153 EMFDRV_StretchBlt, /* pStretchBlt */
154 EMFDRV_StretchDIBits, /* pStretchDIBits */
155 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
156 EMFDRV_StrokePath, /* pStrokePath */
157 NULL, /* pSwapBuffers */
158 NULL, /* pUnrealizePalette */
159 EMFDRV_WidenPath /* pWidenPath */
163 /**********************************************************************
166 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
168 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
171 if (physDev->emh) HeapFree( GetProcessHeap(), 0, physDev->emh );
172 for(index = 0; index < physDev->handles_size; index++)
173 if(physDev->handles[index])
174 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
175 HeapFree( GetProcessHeap(), 0, physDev->handles );
176 HeapFree( GetProcessHeap(), 0, physDev );
181 /******************************************************************
184 * Warning: this function can change the pointer to the metafile header.
186 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
190 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
192 TRACE("record %d, size %d %s\n",
193 emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
195 assert( !(emr->nSize & 3) );
197 physDev->emh->nBytes += emr->nSize;
198 physDev->emh->nRecords++;
201 if (!WriteFile(physDev->hFile, emr, emr->nSize, NULL, NULL))
204 DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
205 len = physDev->emh->nBytes;
206 if (len > nEmfSize) {
207 nEmfSize += (nEmfSize / 2) + emr->nSize;
208 emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
209 if (!emh) return FALSE;
212 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
219 /******************************************************************
222 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
224 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
225 RECTL *bounds = &physDev->emh->rclBounds;
226 RECTL vportRect = *rect;
228 LPtoDP( dev->hdc, (LPPOINT)&vportRect, 2 );
230 /* The coordinate systems may be mirrored
231 (LPtoDP handles points, not rectangles) */
232 if (vportRect.left > vportRect.right)
234 LONG temp = vportRect.right;
235 vportRect.right = vportRect.left;
236 vportRect.left = temp;
238 if (vportRect.top > vportRect.bottom)
240 LONG temp = vportRect.bottom;
241 vportRect.bottom = vportRect.top;
242 vportRect.top = temp;
245 if (bounds->left > bounds->right)
247 /* first bounding rectangle */
252 bounds->left = min(bounds->left, vportRect.left);
253 bounds->top = min(bounds->top, vportRect.top);
254 bounds->right = max(bounds->right, vportRect.right);
255 bounds->bottom = max(bounds->bottom, vportRect.bottom);
259 /**********************************************************************
260 * CreateEnhMetaFileA (GDI32.@)
262 HDC WINAPI CreateEnhMetaFileA(
263 HDC hdc, /* [in] optional reference DC */
264 LPCSTR filename, /* [in] optional filename for disk metafiles */
265 const RECT *rect, /* [in] optional bounding rectangle */
266 LPCSTR description /* [in] optional description */
269 LPWSTR filenameW = NULL;
270 LPWSTR descriptionW = NULL;
272 DWORD len1, len2, total;
276 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
277 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
278 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
281 len1 = strlen(description);
282 len2 = strlen(description + len1 + 1);
283 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
284 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
285 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
288 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
290 HeapFree( GetProcessHeap(), 0, filenameW );
291 HeapFree( GetProcessHeap(), 0, descriptionW );
296 /**********************************************************************
297 * CreateEnhMetaFileW (GDI32.@)
299 HDC WINAPI CreateEnhMetaFileW(
300 HDC hdc, /* [in] optional reference DC */
301 LPCWSTR filename, /* [in] optional filename for disk metafiles */
302 const RECT* rect, /* [in] optional bounding rectangle */
303 LPCWSTR description /* [in] optional description */
306 static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
309 HDC hRefDC = hdc ? hdc : CreateDCW(displayW,NULL,NULL,NULL);
310 /* If no ref, use current display */
311 EMFDRV_PDEVICE *physDev;
313 DWORD size = 0, length = 0;
315 TRACE("%s\n", debugstr_w(filename) );
317 if (!(dc = alloc_dc_ptr( OBJ_ENHMETADC ))) return 0;
319 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
324 if(description) { /* App name\0Title\0\0 */
325 length = lstrlenW(description);
326 length += lstrlenW(description + length + 1);
330 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
332 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
333 HeapFree( GetProcessHeap(), 0, physDev );
338 push_dc_driver( dc, &physDev->dev, &EMFDRV_Funcs );
340 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
341 physDev->handles_size = HANDLE_LIST_INC;
342 physDev->cur_handles = 1;
344 physDev->dc_brush = 0;
346 physDev->horzres = GetDeviceCaps(hRefDC, HORZRES);
347 physDev->vertres = GetDeviceCaps(hRefDC, VERTRES);
348 physDev->logpixelsx = GetDeviceCaps(hRefDC, LOGPIXELSX);
349 physDev->logpixelsy = GetDeviceCaps(hRefDC, LOGPIXELSY);
350 physDev->horzsize = GetDeviceCaps(hRefDC, HORZSIZE);
351 physDev->vertsize = GetDeviceCaps(hRefDC, VERTSIZE);
352 physDev->bitspixel = GetDeviceCaps(hRefDC, BITSPIXEL);
353 physDev->textcaps = GetDeviceCaps(hRefDC, TEXTCAPS);
354 physDev->rastercaps = GetDeviceCaps(hRefDC, RASTERCAPS);
355 physDev->technology = GetDeviceCaps(hRefDC, TECHNOLOGY);
356 physDev->planes = GetDeviceCaps(hRefDC, PLANES);
357 physDev->numcolors = GetDeviceCaps(hRefDC, NUMCOLORS);
358 physDev->restoring = 0;
360 SetVirtualResolution(physDev->dev.hdc, 0, 0, 0, 0);
362 physDev->emh->iType = EMR_HEADER;
363 physDev->emh->nSize = size;
365 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
366 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
369 physDev->emh->rclFrame.left = rect->left;
370 physDev->emh->rclFrame.top = rect->top;
371 physDev->emh->rclFrame.right = rect->right;
372 physDev->emh->rclFrame.bottom = rect->bottom;
373 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
374 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
375 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
378 physDev->emh->dSignature = ENHMETA_SIGNATURE;
379 physDev->emh->nVersion = 0x10000;
380 physDev->emh->nBytes = physDev->emh->nSize;
381 physDev->emh->nRecords = 1;
382 physDev->emh->nHandles = 1;
384 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
385 physDev->emh->nDescription = length / 2;
387 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
389 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
392 physDev->emh->szlDevice.cx = physDev->horzres;
393 physDev->emh->szlDevice.cy = physDev->vertres;
395 /* Size in millimeters */
396 physDev->emh->szlMillimeters.cx = physDev->horzsize;
397 physDev->emh->szlMillimeters.cy = physDev->vertsize;
399 /* Size in micrometers */
400 physDev->emh->szlMicrometers.cx = physDev->horzsize * 1000;
401 physDev->emh->szlMicrometers.cy = physDev->vertsize * 1000;
403 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
405 if (filename) /* disk based metafile */
407 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
408 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
412 if (!WriteFile( hFile, physDev->emh, size, NULL, NULL )) {
416 physDev->hFile = hFile;
419 TRACE("returning %p\n", physDev->dev.hdc);
420 ret = physDev->dev.hdc;
421 release_dc_ptr( dc );
429 /******************************************************************
430 * CloseEnhMetaFile (GDI32.@)
432 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
435 EMFDRV_PDEVICE *physDev;
440 TRACE("(%p)\n", hdc );
442 if (!(dc = get_dc_ptr( hdc ))) return NULL;
443 if (dc->header.type != OBJ_ENHMETADC)
445 release_dc_ptr( dc );
448 if (dc->refcount != 1)
450 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
451 release_dc_ptr( dc );
454 physDev = (EMFDRV_PDEVICE *)dc->physDev;
459 if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
460 if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
462 emr.emr.iType = EMR_EOF;
463 emr.emr.nSize = sizeof(emr);
465 emr.offPalEntries = FIELD_OFFSET(EMREOF, nSizeLast);
466 emr.nSizeLast = emr.emr.nSize;
467 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
469 /* Update rclFrame if not initialized in CreateEnhMetaFile */
470 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
471 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
472 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
473 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
474 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
475 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
476 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
477 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
478 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
481 if (physDev->hFile) /* disk based metafile */
483 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
485 CloseHandle( physDev->hFile );
490 if (!WriteFile(physDev->hFile, physDev->emh, sizeof(*physDev->emh),
493 CloseHandle( physDev->hFile );
497 HeapFree( GetProcessHeap(), 0, physDev->emh );
498 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
500 TRACE("hMapping = %p\n", hMapping );
501 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
502 TRACE("view = %p\n", physDev->emh );
503 CloseHandle( hMapping );
504 CloseHandle( physDev->hFile );
507 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
508 physDev->emh = NULL; /* So it won't be deleted */