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
29 #include "gdi_private.h"
30 #include "enhmfdrv/enhmetafiledrv.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
35 static const DC_FUNCTIONS EMFDRV_Funcs =
38 EMFDRV_AbortPath, /* pAbortPath */
39 NULL, /* pAlphaBlend */
41 EMFDRV_Arc, /* pArc */
43 EMFDRV_BeginPath, /* pBeginPath */
44 EMFDRV_BitBlt, /* pBitBlt */
45 NULL, /* pChoosePixelFormat */
46 EMFDRV_Chord, /* pChord */
47 EMFDRV_CloseFigure, /* pCloseFigure */
48 NULL, /* pCreateBitmap */
50 NULL, /* pCreateDIBSection */
51 NULL, /* pDeleteBitmap */
53 EMFDRV_DeleteObject, /* pDeleteObject */
54 NULL, /* pDescribePixelFormat */
55 NULL, /* pDeviceCapabilities */
56 EMFDRV_Ellipse, /* pEllipse */
59 EMFDRV_EndPath, /* pEndPath */
60 NULL, /* pEnumDeviceFonts */
61 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
62 NULL, /* pExtDeviceMode */
63 NULL, /* pExtEscape */
64 EMFDRV_ExtFloodFill, /* pExtFloodFill */
65 EMFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
66 EMFDRV_ExtTextOut, /* pExtTextOut */
67 EMFDRV_FillPath, /* pFillPath */
68 EMFDRV_FillRgn, /* pFillRgn */
69 EMFDRV_FlattenPath, /* pFlattenPath */
70 EMFDRV_FrameRgn, /* pFrameRgn */
71 EMFDRV_GdiComment, /* pGdiComment */
72 NULL, /* pGetBitmapBits */
73 NULL, /* pGetCharWidth */
74 NULL, /* pGetDCOrgEx */
75 NULL, /* pGetDIBColorTable */
76 NULL, /* pGetDIBits */
77 EMFDRV_GetDeviceCaps, /* pGetDeviceCaps */
78 NULL, /* pGetDeviceGammaRamp */
79 NULL, /* pGetNearestColor */
81 NULL, /* pGetPixelFormat */
82 NULL, /* pGetSystemPaletteEntries */
83 NULL, /* pGetTextExtentPoint */
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_OffsetViewportOrg, /* pOffsetViewportOrg */
92 EMFDRV_OffsetWindowOrg, /* pOffsetWindowOrg */
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, /* pRealizeDefaultPalette */
105 NULL, /* pRealizePalette */
106 EMFDRV_Rectangle, /* pRectangle */
108 EMFDRV_RestoreDC, /* pRestoreDC */
109 EMFDRV_RoundRect, /* pRoundRect */
110 EMFDRV_SaveDC, /* pSaveDC */
111 EMFDRV_ScaleViewportExt, /* pScaleViewportExt */
112 EMFDRV_ScaleWindowExt, /* pScaleWindowExt */
113 EMFDRV_SelectBitmap, /* pSelectBitmap */
114 EMFDRV_SelectBrush, /* pSelectBrush */
115 EMFDRV_SelectClipPath, /* pSelectClipPath */
116 EMFDRV_SelectFont, /* pSelectFont */
117 NULL, /* pSelectPalette */
118 EMFDRV_SelectPen, /* pSelectPen */
119 EMFDRV_SetArcDirection, /* pSetArcDirection */
120 NULL, /* pSetBitmapBits */
121 EMFDRV_SetBkColor, /* pSetBkColor */
122 EMFDRV_SetBkMode, /* pSetBkMode */
123 NULL, /* pSetDCBrushColor */
124 NULL, /* pSetDCOrg */
125 NULL, /* pSetDCPenColor */
126 NULL, /* pSetDIBColorTable */
127 NULL, /* pSetDIBits */
128 EMFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
129 NULL, /* pSetDeviceClipping */
130 NULL, /* pSetDeviceGammaRamp */
131 EMFDRV_SetMapMode, /* pSetMapMode */
132 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
133 NULL, /* 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_SetViewportExt, /* pSetViewportExt */
144 EMFDRV_SetViewportOrg, /* pSetViewportOrg */
145 EMFDRV_SetWindowExt, /* pSetWindowExt */
146 EMFDRV_SetWindowOrg, /* pSetWindowOrg */
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 EMFDRV_WidenPath /* pWidenPath */
159 /**********************************************************************
162 static BOOL EMFDRV_DeleteDC( PHYSDEV dev )
164 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
165 DC *dc = physDev->dc;
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], physDev->hdc);
172 HeapFree( GetProcessHeap(), 0, physDev->handles );
173 HeapFree( GetProcessHeap(), 0, physDev );
175 GDI_FreeObject( dc->hSelf, dc );
180 /******************************************************************
183 * Warning: this function can change the pointer to the metafile header.
185 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
189 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
191 TRACE("record %ld, size %ld %s\n",
192 emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
194 assert( !(emr->nSize & 3) );
196 physDev->emh->nBytes += emr->nSize;
197 physDev->emh->nRecords++;
200 if (!WriteFile(physDev->hFile, (char *)emr, emr->nSize, NULL, NULL))
203 DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
204 len = physDev->emh->nBytes;
205 if (len > nEmfSize) {
206 nEmfSize += (nEmfSize / 2) + emr->nSize;
207 emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
208 if (!emh) return FALSE;
211 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
218 /******************************************************************
221 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
223 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
224 RECTL *bounds = &physDev->emh->rclBounds;
225 RECTL vportRect = *rect;
227 LPtoDP(physDev->hdc, (LPPOINT)&vportRect, 2);
229 if(bounds->left > bounds->right) {/* first rect */
233 bounds->left = min(bounds->left, vportRect.left);
234 bounds->top = min(bounds->top, vportRect.top);
235 bounds->right = max(bounds->right, vportRect.right);
236 bounds->bottom = max(bounds->bottom, vportRect.bottom);
240 /**********************************************************************
241 * CreateEnhMetaFileA (GDI32.@)
243 HDC WINAPI CreateEnhMetaFileA(
244 HDC hdc, /* [in] optional reference DC */
245 LPCSTR filename, /* [in] optional filename for disk metafiles */
246 const RECT *rect, /* [in] optional bounding rectangle */
247 LPCSTR description /* [in] optional description */
250 LPWSTR filenameW = NULL;
251 LPWSTR descriptionW = NULL;
253 DWORD len1, len2, total;
257 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
258 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
259 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
262 len1 = strlen(description);
263 len2 = strlen(description + len1 + 1);
264 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
265 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
266 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
269 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
272 HeapFree( GetProcessHeap(), 0, filenameW );
274 HeapFree( GetProcessHeap(), 0, descriptionW );
279 /**********************************************************************
280 * CreateEnhMetaFileW (GDI32.@)
282 HDC WINAPI CreateEnhMetaFileW(
283 HDC hdc, /* [in] optional reference DC */
284 LPCWSTR filename, /* [in] optional filename for disk metafiles */
285 const RECT* rect, /* [in] optional bounding rectangle */
286 LPCWSTR description /* [in] optional description */
289 static const WCHAR displayW[] = {'D','I','S','P','L','A','Y',0};
292 HDC hRefDC = hdc ? hdc : CreateDCW(displayW,NULL,NULL,NULL);
293 /* If no ref, use current display */
294 EMFDRV_PDEVICE *physDev;
296 DWORD size = 0, length = 0;
298 TRACE("%s\n", debugstr_w(filename) );
300 if (!(dc = DC_AllocDC( &EMFDRV_Funcs, ENHMETAFILE_DC_MAGIC ))) return 0;
302 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
304 GDI_FreeObject( dc->hSelf, dc );
307 dc->physDev = (PHYSDEV)physDev;
308 physDev->hdc = dc->hSelf;
311 if(description) { /* App name\0Title\0\0 */
312 length = lstrlenW(description);
313 length += lstrlenW(description + length + 1);
317 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
319 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
320 HeapFree( GetProcessHeap(), 0, physDev );
321 GDI_FreeObject( dc->hSelf, dc );
325 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
326 physDev->handles_size = HANDLE_LIST_INC;
327 physDev->cur_handles = 1;
330 physDev->horzres = GetDeviceCaps(hRefDC, HORZRES);
331 physDev->vertres = GetDeviceCaps(hRefDC, VERTRES);
332 physDev->logpixelsx = GetDeviceCaps(hRefDC, LOGPIXELSX);
333 physDev->logpixelsy = GetDeviceCaps(hRefDC, LOGPIXELSY);
334 physDev->horzsize = GetDeviceCaps(hRefDC, HORZSIZE);
335 physDev->vertsize = GetDeviceCaps(hRefDC, VERTSIZE);
336 physDev->bitspixel = GetDeviceCaps(hRefDC, BITSPIXEL);
337 physDev->textcaps = GetDeviceCaps(hRefDC, TEXTCAPS);
338 physDev->rastercaps = GetDeviceCaps(hRefDC, RASTERCAPS);
339 physDev->technology = GetDeviceCaps(hRefDC, TECHNOLOGY);
340 physDev->planes = GetDeviceCaps(hRefDC, PLANES);
342 physDev->emh->iType = EMR_HEADER;
343 physDev->emh->nSize = size;
345 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
346 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
349 physDev->emh->rclFrame.left = rect->left;
350 physDev->emh->rclFrame.top = rect->top;
351 physDev->emh->rclFrame.right = rect->right;
352 physDev->emh->rclFrame.bottom = rect->bottom;
353 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
354 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
355 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
358 physDev->emh->dSignature = ENHMETA_SIGNATURE;
359 physDev->emh->nVersion = 0x10000;
360 physDev->emh->nBytes = physDev->emh->nSize;
361 physDev->emh->nRecords = 1;
362 physDev->emh->nHandles = 1;
364 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
365 physDev->emh->nDescription = length / 2;
367 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
369 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
372 physDev->emh->szlDevice.cx = physDev->horzres;
373 physDev->emh->szlDevice.cy = physDev->vertres;
375 /* Size in millimeters */
376 physDev->emh->szlMillimeters.cx = physDev->horzsize;
377 physDev->emh->szlMillimeters.cy = physDev->vertsize;
379 /* Size in micrometers */
380 physDev->emh->szlMicrometers.cx = physDev->horzsize * 1000;
381 physDev->emh->szlMicrometers.cy = physDev->vertsize * 1000;
383 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
385 if (filename) /* disk based metafile */
387 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
388 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
389 EMFDRV_DeleteDC( dc->physDev );
392 if (!WriteFile( hFile, (LPSTR)physDev->emh, size, NULL, NULL )) {
393 EMFDRV_DeleteDC( dc->physDev );
396 physDev->hFile = hFile;
399 TRACE("returning %p\n", dc->hSelf);
401 GDI_ReleaseObj( dc->hSelf );
409 /******************************************************************
410 * CloseEnhMetaFile (GDI32.@)
412 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
415 EMFDRV_PDEVICE *physDev;
420 TRACE("(%p)\n", hdc );
422 if (!(dc = (DC *) GDI_GetObjPtr( hdc, ENHMETAFILE_DC_MAGIC ))) return 0;
423 physDev = (EMFDRV_PDEVICE *)dc->physDev;
425 emr.emr.iType = EMR_EOF;
426 emr.emr.nSize = sizeof(emr);
428 emr.offPalEntries = 0;
429 emr.nSizeLast = emr.emr.nSize;
430 EMFDRV_WriteRecord( dc->physDev, &emr.emr );
432 /* Update rclFrame if not initialized in CreateEnhMetaFile */
433 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
434 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
435 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
436 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
437 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
438 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
439 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
440 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
441 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
444 if (physDev->hFile) /* disk based metafile */
446 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
448 CloseHandle( physDev->hFile );
449 EMFDRV_DeleteDC( dc->physDev );
453 if (!WriteFile(physDev->hFile, (LPSTR)physDev->emh,
454 sizeof(*physDev->emh), NULL, NULL))
456 CloseHandle( physDev->hFile );
457 EMFDRV_DeleteDC( dc->physDev );
460 HeapFree( GetProcessHeap(), 0, physDev->emh );
461 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
463 TRACE("hMapping = %p\n", hMapping );
464 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
465 TRACE("view = %p\n", physDev->emh );
466 CloseHandle( hMapping );
467 CloseHandle( physDev->hFile );
470 hmf = EMF_Create_HENHMETAFILE( physDev->emh, (physDev->hFile != 0) );
471 physDev->emh = NULL; /* So it won't be deleted */
472 EMFDRV_DeleteDC( dc->physDev );