2 * PostScript driver initialization functions
4 * Copyright 1998 Huw D M Davies
5 * Copyright 2001 Marcus Meissner
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "wine/debug.h"
37 # include <cups/cups.h>
40 WINE_DEFAULT_DEBUG_CHANNEL(psdrv
);
42 static PSDRV_DEVMODEA DefaultDevmode
=
45 /* dmDeviceName */ "Wine PostScript Driver",
46 /* dmSpecVersion */ 0x30a,
47 /* dmDriverVersion */ 0x001,
48 /* dmSize */ sizeof(DEVMODEA
),
49 /* dmDriverExtra */ 0,
50 /* dmFields */ DM_ORIENTATION
| DM_PAPERSIZE
| DM_SCALE
|
51 DM_COPIES
| DM_DEFAULTSOURCE
| DM_COLOR
|
52 DM_DUPLEX
| DM_YRESOLUTION
| DM_TTOPTION
,
55 /* dmOrientation */ DMORIENT_PORTRAIT
,
56 /* dmPaperSize */ DMPAPER_LETTER
,
57 /* dmPaperLength */ 2794,
58 /* dmPaperWidth */ 2159
61 /* dmScale */ 100, /* ?? */
63 /* dmDefaultSource */ DMBIN_AUTO
,
64 /* dmPrintQuality */ 0,
65 /* dmColor */ DMCOLOR_COLOR
,
67 /* dmYResolution */ 0,
68 /* dmTTOption */ DMTT_SUBDEV
,
71 /* dmUnusedPadding */ 0,
75 /* dmDisplayFlags */ 0,
76 /* dmDisplayFrequency */ 0,
83 /* dmPanningWidth */ 0,
84 /* dmPanningHeight */ 0
90 /* numInstalledOptions */ 0
94 HANDLE PSDRV_Heap
= 0;
96 static HANDLE PSDRV_DefaultFont
= 0;
97 static LOGFONTA DefaultLogFont
= {
98 100, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
, 0, 0,
99 DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, ""
102 /*********************************************************************
105 * Initializes font metrics and registers driver. Called from GDI_Init()
108 BOOL WINAPI
PSDRV_Init( HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
110 TRACE("(0x%4x, 0x%08lx, %p)\n", hinst
, reason
, reserved
);
114 case DLL_PROCESS_ATTACH
:
116 PSDRV_Heap
= HeapCreate(0, 0x10000, 0);
117 if (PSDRV_Heap
== (HANDLE
)NULL
)
120 if (PSDRV_GetFontMetrics() == FALSE
) {
121 HeapDestroy(PSDRV_Heap
);
125 PSDRV_DefaultFont
= CreateFontIndirectA(&DefaultLogFont
);
126 if (PSDRV_DefaultFont
== (HANDLE
)NULL
) {
127 HeapDestroy(PSDRV_Heap
);
132 case DLL_PROCESS_DETACH
:
134 DeleteObject( PSDRV_DefaultFont
);
135 HeapDestroy( PSDRV_Heap
);
143 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE
*physDev
)
146 INT width
= 0, height
= 0;
148 if(physDev
->Devmode
->dmPublic
.dmFields
& DM_PAPERSIZE
) {
149 for(page
= physDev
->pi
->ppd
->PageSizes
; page
; page
= page
->next
) {
150 if(page
->WinPage
== physDev
->Devmode
->dmPublic
.u1
.s1
.dmPaperSize
)
155 FIXME("Can't find page\n");
156 physDev
->ImageableArea
.left
= 0;
157 physDev
->ImageableArea
.right
= 0;
158 physDev
->ImageableArea
.bottom
= 0;
159 physDev
->ImageableArea
.top
= 0;
160 physDev
->PageSize
.cx
= 0;
161 physDev
->PageSize
.cy
= 0;
162 } else if(page
->ImageableArea
) {
163 /* physDev sizes in device units; ppd sizes in 1/72" */
164 physDev
->ImageableArea
.left
= page
->ImageableArea
->llx
*
165 physDev
->logPixelsX
/ 72;
166 physDev
->ImageableArea
.right
= page
->ImageableArea
->urx
*
167 physDev
->logPixelsX
/ 72;
168 physDev
->ImageableArea
.bottom
= page
->ImageableArea
->lly
*
169 physDev
->logPixelsY
/ 72;
170 physDev
->ImageableArea
.top
= page
->ImageableArea
->ury
*
171 physDev
->logPixelsY
/ 72;
172 physDev
->PageSize
.cx
= page
->PaperDimension
->x
*
173 physDev
->logPixelsX
/ 72;
174 physDev
->PageSize
.cy
= page
->PaperDimension
->y
*
175 physDev
->logPixelsY
/ 72;
177 physDev
->ImageableArea
.left
= physDev
->ImageableArea
.bottom
= 0;
178 physDev
->ImageableArea
.right
= physDev
->PageSize
.cx
=
179 page
->PaperDimension
->x
* physDev
->logPixelsX
/ 72;
180 physDev
->ImageableArea
.top
= physDev
->PageSize
.cy
=
181 page
->PaperDimension
->y
* physDev
->logPixelsY
/ 72;
183 } else if((physDev
->Devmode
->dmPublic
.dmFields
& DM_PAPERLENGTH
) &&
184 (physDev
->Devmode
->dmPublic
.dmFields
& DM_PAPERWIDTH
)) {
185 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
186 physDev
->ImageableArea
.left
= physDev
->ImageableArea
.bottom
= 0;
187 physDev
->ImageableArea
.right
= physDev
->PageSize
.cx
=
188 physDev
->Devmode
->dmPublic
.u1
.s1
.dmPaperWidth
*
189 physDev
->logPixelsX
/ 254;
190 physDev
->ImageableArea
.top
= physDev
->PageSize
.cy
=
191 physDev
->Devmode
->dmPublic
.u1
.s1
.dmPaperLength
*
192 physDev
->logPixelsY
/ 254;
194 FIXME("Odd dmFields %lx\n", physDev
->Devmode
->dmPublic
.dmFields
);
195 physDev
->ImageableArea
.left
= 0;
196 physDev
->ImageableArea
.right
= 0;
197 physDev
->ImageableArea
.bottom
= 0;
198 physDev
->ImageableArea
.top
= 0;
199 physDev
->PageSize
.cx
= 0;
200 physDev
->PageSize
.cy
= 0;
203 TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %ldx%ld\n",
204 physDev
->ImageableArea
.left
, physDev
->ImageableArea
.bottom
,
205 physDev
->ImageableArea
.right
, physDev
->ImageableArea
.top
,
206 physDev
->PageSize
.cx
, physDev
->PageSize
.cy
);
208 /* these are in device units */
209 width
= physDev
->ImageableArea
.right
- physDev
->ImageableArea
.left
;
210 height
= physDev
->ImageableArea
.top
- physDev
->ImageableArea
.bottom
;
212 if(physDev
->Devmode
->dmPublic
.u1
.s1
.dmOrientation
== DMORIENT_PORTRAIT
) {
213 physDev
->horzRes
= width
;
214 physDev
->vertRes
= height
;
216 physDev
->horzRes
= height
;
217 physDev
->vertRes
= width
;
220 /* these are in mm */
221 physDev
->horzSize
= (physDev
->horzRes
* 25.4) / physDev
->logPixelsX
;
222 physDev
->vertSize
= (physDev
->vertRes
* 25.4) / physDev
->logPixelsY
;
224 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
225 "horzRes = %d, vertRes = %d\n",
226 physDev
->horzSize
, physDev
->vertSize
,
227 physDev
->horzRes
, physDev
->vertRes
);
231 /**********************************************************************
234 BOOL
PSDRV_CreateDC( DC
*dc
, PSDRV_PDEVICE
**pdev
, LPCSTR driver
, LPCSTR device
,
235 LPCSTR output
, const DEVMODEA
* initData
)
237 PSDRV_PDEVICE
*physDev
;
240 /* If no device name was specified, retrieve the device name
241 * from the DEVMODE structure from the DC's physDev.
242 * (See CreateCompatibleDC) */
243 if ( !device
&& *pdev
)
246 device
= physDev
->Devmode
->dmPublic
.dmDeviceName
;
248 pi
= PSDRV_FindPrinterInfo(device
);
250 TRACE("(%s %s %s %p)\n", driver
, device
, output
, initData
);
252 if(!pi
) return FALSE
;
255 MESSAGE("To use WINEPS you need to install some AFM files.\n");
259 physDev
= (PSDRV_PDEVICE
*)HeapAlloc( PSDRV_Heap
, HEAP_ZERO_MEMORY
,
261 if (!physDev
) return FALSE
;
263 physDev
->hdc
= dc
->hSelf
;
268 physDev
->Devmode
= (PSDRV_DEVMODEA
*)HeapAlloc( PSDRV_Heap
, 0,
269 sizeof(PSDRV_DEVMODEA
) );
270 if(!physDev
->Devmode
) {
271 HeapFree( PSDRV_Heap
, 0, physDev
);
275 memcpy( physDev
->Devmode
, pi
->Devmode
, sizeof(PSDRV_DEVMODEA
) );
277 physDev
->logPixelsX
= physDev
->pi
->ppd
->DefaultResolution
;
278 physDev
->logPixelsY
= physDev
->pi
->ppd
->DefaultResolution
;
280 if (!output
) output
= "LPT1:"; /* HACK */
281 physDev
->job
.output
= HeapAlloc( PSDRV_Heap
, 0, strlen(output
)+1 );
282 strcpy( physDev
->job
.output
, output
);
283 physDev
->job
.hJob
= 0;
286 PSDRV_MergeDevmodes(physDev
->Devmode
, (PSDRV_DEVMODEA
*)initData
, pi
);
289 PSDRV_UpdateDevCaps(physDev
);
290 dc
->hFont
= PSDRV_DefaultFont
;
296 /**********************************************************************
299 BOOL
PSDRV_DeleteDC( PSDRV_PDEVICE
*physDev
)
303 HeapFree( PSDRV_Heap
, 0, physDev
->Devmode
);
304 HeapFree( PSDRV_Heap
, 0, physDev
->job
.output
);
305 HeapFree( PSDRV_Heap
, 0, physDev
);
311 /**********************************************************************
314 HDC
PSDRV_ResetDC( PSDRV_PDEVICE
*physDev
, const DEVMODEA
*lpInitData
)
317 PSDRV_MergeDevmodes(physDev
->Devmode
, (PSDRV_DEVMODEA
*)lpInitData
, physDev
->pi
);
318 PSDRV_UpdateDevCaps(physDev
);
323 /***********************************************************************
324 * GetDeviceCaps (WINEPS.@)
326 INT
PSDRV_GetDeviceCaps( PSDRV_PDEVICE
*physDev
, INT cap
)
333 return DT_RASPRINTER
;
335 return MulDiv(physDev
->horzSize
, 100,
336 physDev
->Devmode
->dmPublic
.dmScale
);
338 return MulDiv(physDev
->vertSize
, 100,
339 physDev
->Devmode
->dmPublic
.dmScale
);
341 return physDev
->horzRes
;
343 return physDev
->vertRes
;
345 return (physDev
->pi
->ppd
->ColorDevice
? 8 : 1);
357 return (physDev
->pi
->ppd
->ColorDevice
? 256 : -1);
359 return sizeof(PSDRV_PDEVICE
);
361 return (CC_CIRCLES
| CC_PIE
| CC_CHORD
| CC_ELLIPSES
| CC_WIDE
|
362 CC_STYLED
| CC_WIDESTYLED
| CC_INTERIORS
| CC_ROUNDRECT
);
364 return (LC_POLYLINE
| LC_MARKER
| LC_POLYMARKER
| LC_WIDE
|
365 LC_STYLED
| LC_WIDESTYLED
| LC_INTERIORS
);
367 return (PC_POLYGON
| PC_RECTANGLE
| PC_WINDPOLYGON
| PC_SCANLINE
|
368 PC_WIDE
| PC_STYLED
| PC_WIDESTYLED
| PC_INTERIORS
);
370 return TC_CR_ANY
| TC_VA_ABLE
; /* psdrv 0x59f7 */
374 return (RC_BITBLT
| RC_BITMAP64
| RC_GDI20_OUTPUT
| RC_DIBTODEV
|
375 RC_STRETCHBLT
| RC_STRETCHDIB
); /* psdrv 0x6e99 */
376 /* Are aspect[XY] and logPixels[XY] correct? */
377 /* Need to handle different res in x and y => fix ppd */
380 return physDev
->pi
->ppd
->DefaultResolution
;
382 return (int)hypot( (double)physDev
->pi
->ppd
->DefaultResolution
,
383 (double)physDev
->pi
->ppd
->DefaultResolution
);
385 return MulDiv(physDev
->logPixelsX
,
386 physDev
->Devmode
->dmPublic
.dmScale
, 100);
388 return MulDiv(physDev
->logPixelsY
,
389 physDev
->Devmode
->dmPublic
.dmScale
, 100);
397 return (physDev
->Devmode
->dmPublic
.u1
.s1
.dmOrientation
== DMORIENT_LANDSCAPE
) ?
398 physDev
->PageSize
.cy
: physDev
->PageSize
.cx
;
400 return (physDev
->Devmode
->dmPublic
.u1
.s1
.dmOrientation
== DMORIENT_LANDSCAPE
) ?
401 physDev
->PageSize
.cx
: physDev
->PageSize
.cy
;
402 case PHYSICALOFFSETX
:
403 if(physDev
->Devmode
->dmPublic
.u1
.s1
.dmOrientation
== DMORIENT_LANDSCAPE
) {
404 if(physDev
->pi
->ppd
->LandscapeOrientation
== -90)
405 return physDev
->PageSize
.cy
- physDev
->ImageableArea
.top
;
407 return physDev
->ImageableArea
.bottom
;
409 return physDev
->ImageableArea
.left
;
411 case PHYSICALOFFSETY
:
412 if(physDev
->Devmode
->dmPublic
.u1
.s1
.dmOrientation
== DMORIENT_LANDSCAPE
) {
413 if(physDev
->pi
->ppd
->LandscapeOrientation
== -90)
414 return physDev
->PageSize
.cx
- physDev
->ImageableArea
.right
;
416 return physDev
->ImageableArea
.left
;
418 return physDev
->PageSize
.cy
- physDev
->ImageableArea
.top
;
428 FIXME("(%04x): unsupported capability %d, will return 0\n", physDev
->hdc
, cap
);
434 /**********************************************************************
435 * PSDRV_FindPrinterInfo
437 PRINTERINFO
*PSDRV_FindPrinterInfo(LPCSTR name
)
439 static PRINTERINFO
*PSDRV_PrinterList
;
440 DWORD type
= REG_BINARY
, needed
, res
, dwPaperSize
;
441 PRINTERINFO
*pi
= PSDRV_PrinterList
, **last
= &PSDRV_PrinterList
;
445 const char *ppd
= NULL
;
447 char* ppdFileName
= NULL
;
449 BOOL using_default_devmode
= FALSE
;
451 TRACE("'%s'\n", name
);
454 * If this loop completes, last will point to the 'next' element of the
455 * final PRINTERINFO in the list
457 for( ; pi
; last
= &pi
->next
, pi
= pi
->next
)
458 if(!strcmp(pi
->FriendlyName
, name
))
461 pi
= *last
= HeapAlloc( PSDRV_Heap
, HEAP_ZERO_MEMORY
, sizeof(*pi
) );
465 if (!(pi
->FriendlyName
= HeapAlloc( PSDRV_Heap
, 0, strlen(name
)+1 ))) goto fail
;
466 strcpy( pi
->FriendlyName
, name
);
468 /* Use Get|SetPrinterDataExA instead? */
470 res
= DrvGetPrinterData16((LPSTR
)name
, (LPSTR
)INT_PD_DEFAULT_DEVMODE
, &type
,
473 if(res
== ERROR_INVALID_PRINTER_NAME
|| needed
!= sizeof(DefaultDevmode
)) {
474 pi
->Devmode
= HeapAlloc( PSDRV_Heap
, 0, sizeof(DefaultDevmode
) );
475 if (pi
->Devmode
== NULL
)
477 memcpy(pi
->Devmode
, &DefaultDevmode
, sizeof(DefaultDevmode
) );
478 strcpy(pi
->Devmode
->dmPublic
.dmDeviceName
,name
);
479 using_default_devmode
= TRUE
;
481 /* need to do something here AddPrinter?? */
484 pi
->Devmode
= HeapAlloc( PSDRV_Heap
, 0, needed
);
485 DrvGetPrinterData16((LPSTR
)name
, (LPSTR
)INT_PD_DEFAULT_DEVMODE
, &type
,
486 (LPBYTE
)pi
->Devmode
, needed
, &needed
);
489 if (OpenPrinterA (pi
->FriendlyName
, &hPrinter
, NULL
) == 0) {
490 ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
496 ppd
= cupsGetPPD(name
);
499 needed
=strlen(ppd
)+1;
500 ppdFileName
=HeapAlloc(PSDRV_Heap
, 0, needed
);
501 memcpy(ppdFileName
, ppd
, needed
);
504 /* we should unlink() that file later */
506 res
= ERROR_FILE_NOT_FOUND
;
507 WARN("Did not find ppd for %s\n",name
);
513 res
= GetPrinterDataA(hPrinter
, "PPD File", NULL
, NULL
, 0, &needed
);
514 if ((res
==ERROR_SUCCESS
) || (res
==ERROR_MORE_DATA
)) {
515 ppdFileName
=HeapAlloc(PSDRV_Heap
, 0, needed
);
516 res
= GetPrinterDataA(hPrinter
, "PPD File", &ppdType
, ppdFileName
, needed
, &needed
);
519 /* Look for a ppd file for this printer in the config file.
520 * First look under that printer's name, and then under 'generic'
522 if((res
!= ERROR_SUCCESS
) && !RegOpenKeyA(HKEY_LOCAL_MACHINE
, "Software\\Wine\\Wine\\Config\\ppd", &hkey
))
524 const char* value_name
;
526 if (RegQueryValueExA(hkey
, name
, 0, NULL
, NULL
, &needed
) == ERROR_SUCCESS
) {
528 } else if (RegQueryValueExA(hkey
, "generic", 0, NULL
, NULL
, &needed
) == ERROR_SUCCESS
) {
529 value_name
="generic";
534 ppdFileName
=HeapAlloc(PSDRV_Heap
, 0, needed
);
535 RegQueryValueExA(hkey
, value_name
, 0, &ppdType
, ppdFileName
, &needed
);
541 res
= ERROR_FILE_NOT_FOUND
;
542 ERR ("Error %li getting PPD file name for printer '%s'\n", res
, name
);
546 if (ppdType
==REG_EXPAND_SZ
) {
549 /* Expand environment variable references */
550 needed
=ExpandEnvironmentStringsA(ppdFileName
,NULL
,0);
551 tmp
=HeapAlloc(PSDRV_Heap
, 0, needed
);
552 ExpandEnvironmentStringsA(ppdFileName
,tmp
,needed
);
553 HeapFree(PSDRV_Heap
, 0, ppdFileName
);
558 pi
->ppd
= PSDRV_ParsePPD(ppdFileName
);
560 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
566 if(using_default_devmode
) {
569 if(GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_IPAPERSIZE
| LOCALE_RETURN_NUMBER
,
570 (LPWSTR
)&papersize
, sizeof(papersize
))) {
572 memset(&dm
, 0, sizeof(dm
));
573 dm
.dmPublic
.dmFields
= DM_PAPERSIZE
;
574 dm
.dmPublic
.u1
.s1
.dmPaperSize
= papersize
;
575 PSDRV_MergeDevmodes(pi
->Devmode
, &dm
, pi
);
577 DrvSetPrinterData16((LPSTR
)name
, (LPSTR
)INT_PD_DEFAULT_DEVMODE
,
578 REG_BINARY
, (LPBYTE
)pi
->Devmode
, sizeof(DefaultDevmode
) );
583 * This is a hack. The default paper size should be read in as part of
584 * the Devmode structure, but Wine doesn't currently provide a convenient
585 * way to configure printers.
587 res
= GetPrinterDataA (hPrinter
, "Paper Size", NULL
, (LPBYTE
) &dwPaperSize
,
588 sizeof (DWORD
), &needed
);
589 if (res
== ERROR_SUCCESS
)
590 pi
->Devmode
->dmPublic
.u1
.s1
.dmPaperSize
= (SHORT
) dwPaperSize
;
591 else if (res
== ERROR_FILE_NOT_FOUND
)
592 TRACE ("No 'Paper Size' for printer '%s'\n", name
);
594 ERR ("GetPrinterDataA returned %li\n", res
);
598 res
= EnumPrinterDataExA (hPrinter
, "PrinterDriverData\\FontSubTable", NULL
,
599 0, &needed
, &pi
->FontSubTableSize
);
600 if (res
== ERROR_SUCCESS
|| res
== ERROR_FILE_NOT_FOUND
) {
601 TRACE ("No 'FontSubTable' for printer '%s'\n", name
);
603 else if (res
== ERROR_MORE_DATA
) {
604 pi
->FontSubTable
= HeapAlloc (PSDRV_Heap
, 0, needed
);
605 if (pi
->FontSubTable
== NULL
) {
606 ERR ("Failed to allocate %li bytes from heap\n", needed
);
610 res
= EnumPrinterDataExA (hPrinter
, "PrinterDriverData\\FontSubTable",
611 (LPBYTE
) pi
->FontSubTable
, needed
, &needed
,
612 &pi
->FontSubTableSize
);
613 if (res
!= ERROR_SUCCESS
) {
614 ERR ("EnumPrinterDataExA returned %li\n", res
);
619 ERR("EnumPrinterDataExA returned %li\n", res
);
623 if (ClosePrinter (hPrinter
) == 0) {
624 ERR ("ClosePrinter failed with code %li\n", GetLastError ());
631 for(font
= pi
->ppd
->InstalledFonts
; font
; font
= font
->next
) {
632 afm
= PSDRV_FindAFMinList(PSDRV_AFMFontList
, font
->Name
);
634 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
635 "ignoring\n", font
->Name
);
639 if (PSDRV_AddAFMtoList(&pi
->Fonts
, afm
, &added
) == FALSE
) {
640 PSDRV_FreeAFMList(pi
->Fonts
);
646 if (ppd
) unlink(ppd
);
650 ClosePrinter(hPrinter
);
653 HeapFree(PSDRV_Heap
, 0, ppdFileName
);
654 if (pi
->FontSubTable
)
655 HeapFree(PSDRV_Heap
, 0, pi
->FontSubTable
);
656 if (pi
->FriendlyName
)
657 HeapFree(PSDRV_Heap
, 0, pi
->FriendlyName
);
659 HeapFree(PSDRV_Heap
, 0, pi
->Devmode
);
661 HeapFree(PSDRV_Heap
, 0, pi
);
662 if (ppd
) unlink(ppd
);