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
26 #include "enhmfdrv/enhmetafiledrv.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile
);
31 static const DC_FUNCTIONS EMFDRV_Funcs
=
34 EMFDRV_AbortPath
, /* pAbortPath */
36 EMFDRV_Arc
, /* pArc */
38 EMFDRV_BeginPath
, /* pBeginPath */
40 NULL
, /* pChoosePixelFormat */
41 EMFDRV_Chord
, /* pChord */
42 EMFDRV_CloseFigure
, /* pCloseFigure */
43 NULL
, /* pCreateBitmap */
45 NULL
, /* pCreateDIBSection */
46 NULL
, /* pDeleteBitmap */
48 NULL
, /* pDescribePixelFormat */
49 NULL
, /* pDeviceCapabilities */
50 EMFDRV_Ellipse
, /* pEllipse */
53 EMFDRV_EndPath
, /* pEndPath */
54 NULL
, /* pEnumDeviceFonts */
55 EMFDRV_ExcludeClipRect
, /* pExcludeClipRect */
56 NULL
, /* pExtDeviceMode */
57 NULL
, /* pExtEscape */
58 EMFDRV_ExtFloodFill
, /* pExtFloodFill */
59 EMFDRV_ExtSelectClipRgn
, /* pExtSelectClipRgn */
60 EMFDRV_ExtTextOut
, /* pExtTextOut */
61 EMFDRV_FillPath
, /* pFillPath */
62 EMFDRV_FillRgn
, /* pFillRgn */
63 EMFDRV_FlattenPath
, /* pFlattenPath */
64 EMFDRV_FrameRgn
, /* pFrameRgn */
65 NULL
, /* pGetBitmapBits */
66 NULL
, /* pGetCharWidth */
67 NULL
, /* pGetDCOrgEx */
68 NULL
, /* pGetDIBColorTable */
69 NULL
, /* pGetDIBits */
70 EMFDRV_GetDeviceCaps
, /* pGetDeviceCaps */
71 NULL
, /* pGetDeviceGammaRamp */
72 NULL
, /* pGetNearestColor */
74 NULL
, /* pGetPixelFormat */
75 NULL
, /* pGetSystemPaletteEntries */
76 NULL
, /* pGetTextExtentPoint */
77 NULL
, /* pGetTextMetrics */
78 EMFDRV_IntersectClipRect
, /* pIntersectClipRect */
79 EMFDRV_InvertRgn
, /* pInvertRgn */
80 EMFDRV_LineTo
, /* pLineTo */
81 EMFDRV_MoveTo
, /* pMoveTo */
82 EMFDRV_OffsetClipRgn
, /* pOffsetClipRgn */
83 NULL
, /* pOffsetViewportOrg */
84 NULL
, /* pOffsetWindowOrg */
85 EMFDRV_PaintRgn
, /* pPaintRgn */
86 EMFDRV_PatBlt
, /* pPatBlt */
87 EMFDRV_Pie
, /* pPie */
88 NULL
, /* pPolyBezier */
89 NULL
, /* pPolyBezierTo */
91 EMFDRV_PolyPolygon
, /* pPolyPolygon */
92 EMFDRV_PolyPolyline
, /* pPolyPolyline */
93 EMFDRV_Polygon
, /* pPolygon */
94 EMFDRV_Polyline
, /* pPolyline */
95 NULL
, /* pPolylineTo */
96 NULL
, /* pRealizeDefaultPalette */
97 NULL
, /* pRealizePalette */
98 EMFDRV_Rectangle
, /* pRectangle */
100 EMFDRV_RestoreDC
, /* pRestoreDC */
101 EMFDRV_RoundRect
, /* pRoundRect */
102 EMFDRV_SaveDC
, /* pSaveDC */
103 EMFDRV_ScaleViewportExt
, /* pScaleViewportExt */
104 EMFDRV_ScaleWindowExt
, /* pScaleWindowExt */
105 EMFDRV_SelectBitmap
, /* pSelectBitmap */
106 EMFDRV_SelectBrush
, /* pSelectBrush */
107 EMFDRV_SelectClipPath
, /* pSelectClipPath */
108 EMFDRV_SelectFont
, /* pSelectFont */
109 NULL
, /* pSelectPalette */
110 EMFDRV_SelectPen
, /* pSelectPen */
111 NULL
, /* pSetBitmapBits */
112 EMFDRV_SetBkColor
, /* pSetBkColor */
113 EMFDRV_SetBkMode
, /* pSetBkMode */
114 NULL
, /* pSetDCOrg */
115 NULL
, /* pSetDIBColorTable */
116 NULL
, /* pSetDIBits */
117 NULL
, /* pSetDIBitsToDevice */
118 NULL
, /* pSetDeviceClipping */
119 NULL
, /* pSetDeviceGammaRamp */
120 EMFDRV_SetMapMode
, /* pSetMapMode */
121 EMFDRV_SetMapperFlags
, /* pSetMapperFlags */
122 NULL
, /* pSetPixel */
123 NULL
, /* pSetPixelFormat */
124 EMFDRV_SetPolyFillMode
, /* pSetPolyFillMode */
125 EMFDRV_SetROP2
, /* pSetROP2 */
126 NULL
, /* pSetRelAbs */
127 EMFDRV_SetStretchBltMode
, /* pSetStretchBltMode */
128 EMFDRV_SetTextAlign
, /* pSetTextAlign */
129 NULL
, /* pSetTextCharacterExtra */
130 EMFDRV_SetTextColor
, /* pSetTextColor */
131 NULL
, /* pSetTextJustification */
132 EMFDRV_SetViewportExt
, /* pSetViewportExt */
133 EMFDRV_SetViewportOrg
, /* pSetViewportOrg */
134 EMFDRV_SetWindowExt
, /* pSetWindowExt */
135 EMFDRV_SetWindowOrg
, /* pSetWindowOrg */
136 NULL
, /* pStartDoc */
137 NULL
, /* pStartPage */
138 NULL
, /* pStretchBlt */
139 NULL
, /* pStretchDIBits */
140 EMFDRV_StrokeAndFillPath
, /* pStrokeAndFillPath */
141 EMFDRV_StrokePath
, /* pStrokePath */
142 NULL
, /* pSwapBuffers */
143 EMFDRV_WidenPath
/* pWiddenPath */
147 /**********************************************************************
150 static BOOL
EMFDRV_DeleteDC( PHYSDEV dev
)
152 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
153 DC
*dc
= physDev
->dc
;
155 if (physDev
->emh
) HeapFree( GetProcessHeap(), 0, physDev
->emh
);
156 HeapFree( GetProcessHeap(), 0, physDev
);
158 GDI_FreeObject( dc
->hSelf
, dc
);
163 /******************************************************************
166 * Warning: this function can change the pointer to the metafile header.
168 BOOL
EMFDRV_WriteRecord( PHYSDEV dev
, EMR
*emr
)
172 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
174 physDev
->emh
->nBytes
+= emr
->nSize
;
175 physDev
->emh
->nRecords
++;
178 TRACE("Writing record to disk\n");
179 if (!WriteFile(physDev
->hFile
, (char *)emr
, emr
->nSize
, NULL
, NULL
))
182 len
= physDev
->emh
->nBytes
;
183 emh
= HeapReAlloc( GetProcessHeap(), 0, physDev
->emh
, len
);
184 if (!emh
) return FALSE
;
186 memcpy((CHAR
*)physDev
->emh
+ physDev
->emh
->nBytes
- emr
->nSize
, emr
,
193 /******************************************************************
196 void EMFDRV_UpdateBBox( PHYSDEV dev
, RECTL
*rect
)
198 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
199 RECTL
*bounds
= &physDev
->emh
->rclBounds
;
201 if(bounds
->left
> bounds
->right
) {/* first rect */
205 bounds
->left
= min(bounds
->left
, rect
->left
);
206 bounds
->top
= min(bounds
->top
, rect
->top
);
207 bounds
->right
= max(bounds
->right
, rect
->right
);
208 bounds
->bottom
= max(bounds
->bottom
, rect
->bottom
);
212 /******************************************************************
215 * Note: this function assumes that we never delete objects.
216 * If we do someday, we'll need to maintain a table to re-use deleted
219 int EMFDRV_AddHandleDC( PHYSDEV dev
)
221 EMFDRV_PDEVICE
*physDev
= (EMFDRV_PDEVICE
*)dev
;
222 physDev
->emh
->nHandles
++;
223 return physDev
->nextHandle
++;
227 /**********************************************************************
228 * CreateEnhMetaFileA (GDI32.@)
230 HDC WINAPI
CreateEnhMetaFileA(
231 HDC hdc
, /* [in] optional reference DC */
232 LPCSTR filename
, /* [in] optional filename for disk metafiles */
233 const RECT
*rect
, /* [in] optional bounding rectangle */
234 LPCSTR description
/* [in] optional description */
237 LPWSTR filenameW
= NULL
;
238 LPWSTR descriptionW
= NULL
;
240 DWORD len1
, len2
, total
;
244 total
= MultiByteToWideChar( CP_ACP
, 0, filename
, -1, NULL
, 0 );
245 filenameW
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) );
246 MultiByteToWideChar( CP_ACP
, 0, filename
, -1, filenameW
, total
);
249 len1
= strlen(description
);
250 len2
= strlen(description
+ len1
+ 1);
251 total
= MultiByteToWideChar( CP_ACP
, 0, description
, len1
+ len2
+ 3, NULL
, 0 );
252 descriptionW
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) );
253 MultiByteToWideChar( CP_ACP
, 0, description
, len1
+ len2
+ 3, descriptionW
, total
);
256 hReturnDC
= CreateEnhMetaFileW(hdc
, filenameW
, rect
, descriptionW
);
259 HeapFree( GetProcessHeap(), 0, filenameW
);
261 HeapFree( GetProcessHeap(), 0, descriptionW
);
266 /**********************************************************************
267 * CreateEnhMetaFileW (GDI32.@)
269 HDC WINAPI
CreateEnhMetaFileW(
270 HDC hdc
, /* [in] optional reference DC */
271 LPCWSTR filename
, /* [in] optional filename for disk metafiles */
272 const RECT
* rect
, /* [in] optional bounding rectangle */
273 LPCWSTR description
/* [in] optional description */
276 static const WCHAR displayW
[] = {'D','I','S','P','L','A','Y',0};
279 HDC hRefDC
= hdc
? hdc
: CreateDCW(displayW
,NULL
,NULL
,NULL
);
280 /* If no ref, use current display */
281 EMFDRV_PDEVICE
*physDev
;
283 DWORD size
= 0, length
= 0;
285 TRACE("%s\n", debugstr_w(filename
) );
287 if (!(dc
= DC_AllocDC( &EMFDRV_Funcs
))) return 0;
288 dc
->header
.wMagic
= ENHMETAFILE_DC_MAGIC
;
290 physDev
= (EMFDRV_PDEVICE
*)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev
));
292 GDI_FreeObject( dc
->hSelf
, dc
);
295 dc
->physDev
= (PHYSDEV
)physDev
;
296 physDev
->hdc
= dc
->hSelf
;
299 if(description
) { /* App name\0Title\0\0 */
300 length
= lstrlenW(description
);
301 length
+= lstrlenW(description
+ length
+ 1);
305 size
= sizeof(ENHMETAHEADER
) + (length
+ 3) / 4 * 4;
307 if (!(physDev
->emh
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
))) {
308 HeapFree( GetProcessHeap(), 0, physDev
);
309 GDI_FreeObject( dc
->hSelf
, dc
);
313 physDev
->nextHandle
= 1;
316 physDev
->horzres
= GetDeviceCaps(hRefDC
, HORZRES
);
317 physDev
->vertres
= GetDeviceCaps(hRefDC
, VERTRES
);
318 physDev
->logpixelsx
= GetDeviceCaps(hRefDC
, LOGPIXELSX
);
319 physDev
->logpixelsy
= GetDeviceCaps(hRefDC
, LOGPIXELSY
);
320 physDev
->horzsize
= GetDeviceCaps(hRefDC
, HORZSIZE
);
321 physDev
->vertsize
= GetDeviceCaps(hRefDC
, VERTSIZE
);
322 physDev
->bitspixel
= GetDeviceCaps(hRefDC
, BITSPIXEL
);
324 physDev
->emh
->iType
= EMR_HEADER
;
325 physDev
->emh
->nSize
= size
;
327 physDev
->emh
->rclBounds
.left
= physDev
->emh
->rclBounds
.top
= 0;
328 physDev
->emh
->rclBounds
.right
= physDev
->emh
->rclBounds
.bottom
= -1;
331 physDev
->emh
->rclFrame
.left
= rect
->left
;
332 physDev
->emh
->rclFrame
.top
= rect
->top
;
333 physDev
->emh
->rclFrame
.right
= rect
->right
;
334 physDev
->emh
->rclFrame
.bottom
= rect
->bottom
;
335 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
336 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclFrame
.top
= 0;
337 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclFrame
.bottom
= -1;
340 physDev
->emh
->dSignature
= ENHMETA_SIGNATURE
;
341 physDev
->emh
->nVersion
= 0x10000;
342 physDev
->emh
->nBytes
= physDev
->emh
->nSize
;
343 physDev
->emh
->nRecords
= 1;
344 physDev
->emh
->nHandles
= 1;
346 physDev
->emh
->sReserved
= 0; /* According to docs, this is reserved and must be 0 */
347 physDev
->emh
->nDescription
= length
/ 2;
349 physDev
->emh
->offDescription
= length
? sizeof(ENHMETAHEADER
) : 0;
351 physDev
->emh
->nPalEntries
= 0; /* I guess this should start at 0 */
354 physDev
->emh
->szlDevice
.cx
= physDev
->horzres
;
355 physDev
->emh
->szlDevice
.cy
= physDev
->vertres
;
357 /* Size in millimeters */
358 physDev
->emh
->szlMillimeters
.cx
= physDev
->horzsize
;
359 physDev
->emh
->szlMillimeters
.cy
= physDev
->vertsize
;
361 memcpy((char *)physDev
->emh
+ sizeof(ENHMETAHEADER
), description
, length
);
363 if (filename
) /* disk based metafile */
365 if ((hFile
= CreateFileW(filename
, GENERIC_WRITE
| GENERIC_READ
, 0,
366 NULL
, CREATE_ALWAYS
, 0, 0)) == INVALID_HANDLE_VALUE
) {
367 EMFDRV_DeleteDC( dc
->physDev
);
370 if (!WriteFile( hFile
, (LPSTR
)physDev
->emh
, size
, NULL
, NULL
)) {
371 EMFDRV_DeleteDC( dc
->physDev
);
374 physDev
->hFile
= hFile
;
377 TRACE("returning %p\n", dc
->hSelf
);
379 GDI_ReleaseObj( dc
->hSelf
);
387 /******************************************************************
388 * CloseEnhMetaFile (GDI32.@)
390 HENHMETAFILE WINAPI
CloseEnhMetaFile(HDC hdc
) /* [in] metafile DC */
393 EMFDRV_PDEVICE
*physDev
;
398 TRACE("(%p)\n", hdc
);
400 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, ENHMETAFILE_DC_MAGIC
))) return 0;
401 physDev
= (EMFDRV_PDEVICE
*)dc
->physDev
;
403 emr
.emr
.iType
= EMR_EOF
;
404 emr
.emr
.nSize
= sizeof(emr
);
406 emr
.offPalEntries
= 0;
407 emr
.nSizeLast
= emr
.emr
.nSize
;
408 EMFDRV_WriteRecord( dc
->physDev
, &emr
.emr
);
410 /* Update rclFrame if not initialized in CreateEnhMetaFile */
411 if(physDev
->emh
->rclFrame
.left
> physDev
->emh
->rclFrame
.right
) {
412 physDev
->emh
->rclFrame
.left
= physDev
->emh
->rclBounds
.left
*
413 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
414 physDev
->emh
->rclFrame
.top
= physDev
->emh
->rclBounds
.top
*
415 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
416 physDev
->emh
->rclFrame
.right
= physDev
->emh
->rclBounds
.right
*
417 physDev
->emh
->szlMillimeters
.cx
* 100 / physDev
->emh
->szlDevice
.cx
;
418 physDev
->emh
->rclFrame
.bottom
= physDev
->emh
->rclBounds
.bottom
*
419 physDev
->emh
->szlMillimeters
.cy
* 100 / physDev
->emh
->szlDevice
.cy
;
422 if (physDev
->hFile
) /* disk based metafile */
424 if (SetFilePointer(physDev
->hFile
, 0, NULL
, FILE_BEGIN
) != 0)
426 CloseHandle( physDev
->hFile
);
427 EMFDRV_DeleteDC( dc
->physDev
);
431 if (!WriteFile(physDev
->hFile
, (LPSTR
)physDev
->emh
,
432 sizeof(*physDev
->emh
), NULL
, NULL
))
434 CloseHandle( physDev
->hFile
);
435 EMFDRV_DeleteDC( dc
->physDev
);
438 HeapFree( GetProcessHeap(), 0, physDev
->emh
);
439 hMapping
= CreateFileMappingA(physDev
->hFile
, NULL
, PAGE_READONLY
, 0,
441 TRACE("hMapping = %p\n", hMapping
);
442 physDev
->emh
= MapViewOfFile(hMapping
, FILE_MAP_READ
, 0, 0, 0);
443 TRACE("view = %p\n", physDev
->emh
);
444 CloseHandle( hMapping
);
445 CloseHandle( physDev
->hFile
);
448 hmf
= EMF_Create_HENHMETAFILE( physDev
->emh
, (physDev
->hFile
!= 0) );
449 physDev
->emh
= NULL
; /* So it won't be deleted */
450 EMFDRV_DeleteDC( dc
->physDev
);