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
23 #include "wine/port.h"
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
34 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
41 #ifdef HAVE_CUPS_CUPS_H
42 #include <cups/cups.h>
44 #ifndef SONAME_LIBCUPS
45 #define SONAME_LIBCUPS "libcups.so"
48 static void *cupshandle = NULL;
51 static PSDRV_DEVMODEA DefaultDevmode =
54 /* dmDeviceName */ "Wine PostScript Driver",
55 /* dmSpecVersion */ 0x30a,
56 /* dmDriverVersion */ 0x001,
57 /* dmSize */ sizeof(DEVMODEA),
58 /* dmDriverExtra */ sizeof(PSDRV_DEVMODEA)-sizeof(DEVMODEA),
59 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE |
60 DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR |
61 DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION,
64 /* dmOrientation */ DMORIENT_PORTRAIT,
65 /* dmPaperSize */ DMPAPER_LETTER,
66 /* dmPaperLength */ 2794,
67 /* dmPaperWidth */ 2159
70 /* dmScale */ 100, /* ?? */
72 /* dmDefaultSource */ DMBIN_AUTO,
73 /* dmPrintQuality */ 0,
74 /* dmColor */ DMCOLOR_COLOR,
76 /* dmYResolution */ 0,
77 /* dmTTOption */ DMTT_SUBDEV,
80 /* dmUnusedPadding */ 0,
84 /* dmDisplayFlags */ 0,
85 /* dmDisplayFrequency */ 0,
92 /* dmPanningWidth */ 0,
93 /* dmPanningHeight */ 0
99 /* numInstalledOptions */ 0
103 HANDLE PSDRV_Heap = 0;
105 static HANDLE PSDRV_DefaultFont = 0;
106 static LOGFONTA DefaultLogFont = {
107 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
108 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
111 /*********************************************************************
114 * Initializes font metrics and registers driver. wineps dll entry point.
117 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
119 TRACE("(%p, 0x%08lx, %p)\n", hinst, reason, reserved);
123 case DLL_PROCESS_ATTACH:
125 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
126 if (PSDRV_Heap == NULL)
129 if (PSDRV_GetFontMetrics() == FALSE) {
130 HeapDestroy(PSDRV_Heap);
134 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
135 if (PSDRV_DefaultFont == NULL) {
136 HeapDestroy(PSDRV_Heap);
139 #ifdef HAVE_CUPS_CUPS_H
140 /* dynamically load CUPS if not yet loaded */
142 cupshandle = wine_dlopen(SONAME_LIBCUPS, RTLD_NOW, NULL, 0);
143 if (!cupshandle) cupshandle = (void*)-1;
148 case DLL_PROCESS_DETACH:
150 DeleteObject( PSDRV_DefaultFont );
151 HeapDestroy( PSDRV_Heap );
152 #ifdef HAVE_CUPS_CUPS_H
153 if (cupshandle && (cupshandle != (void*)-1)) {
154 wine_dlclose(cupshandle, NULL, 0);
165 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
168 INT width = 0, height = 0;
170 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
171 for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
172 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
177 FIXME("Can't find page\n");
178 physDev->ImageableArea.left = 0;
179 physDev->ImageableArea.right = 0;
180 physDev->ImageableArea.bottom = 0;
181 physDev->ImageableArea.top = 0;
182 physDev->PageSize.cx = 0;
183 physDev->PageSize.cy = 0;
184 } else if(page->ImageableArea) {
185 /* physDev sizes in device units; ppd sizes in 1/72" */
186 physDev->ImageableArea.left = page->ImageableArea->llx *
187 physDev->logPixelsX / 72;
188 physDev->ImageableArea.right = page->ImageableArea->urx *
189 physDev->logPixelsX / 72;
190 physDev->ImageableArea.bottom = page->ImageableArea->lly *
191 physDev->logPixelsY / 72;
192 physDev->ImageableArea.top = page->ImageableArea->ury *
193 physDev->logPixelsY / 72;
194 physDev->PageSize.cx = page->PaperDimension->x *
195 physDev->logPixelsX / 72;
196 physDev->PageSize.cy = page->PaperDimension->y *
197 physDev->logPixelsY / 72;
199 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
200 physDev->ImageableArea.right = physDev->PageSize.cx =
201 page->PaperDimension->x * physDev->logPixelsX / 72;
202 physDev->ImageableArea.top = physDev->PageSize.cy =
203 page->PaperDimension->y * physDev->logPixelsY / 72;
205 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
206 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
207 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
208 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
209 physDev->ImageableArea.right = physDev->PageSize.cx =
210 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
211 physDev->logPixelsX / 254;
212 physDev->ImageableArea.top = physDev->PageSize.cy =
213 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
214 physDev->logPixelsY / 254;
216 FIXME("Odd dmFields %lx\n", physDev->Devmode->dmPublic.dmFields);
217 physDev->ImageableArea.left = 0;
218 physDev->ImageableArea.right = 0;
219 physDev->ImageableArea.bottom = 0;
220 physDev->ImageableArea.top = 0;
221 physDev->PageSize.cx = 0;
222 physDev->PageSize.cy = 0;
225 TRACE("ImageableArea = %ld,%ld - %ld,%ld: PageSize = %ldx%ld\n",
226 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
227 physDev->ImageableArea.right, physDev->ImageableArea.top,
228 physDev->PageSize.cx, physDev->PageSize.cy);
230 /* these are in device units */
231 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
232 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
234 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
235 physDev->horzRes = width;
236 physDev->vertRes = height;
238 physDev->horzRes = height;
239 physDev->vertRes = width;
242 /* these are in mm */
243 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
244 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
246 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
247 "horzRes = %d, vertRes = %d\n",
248 physDev->horzSize, physDev->vertSize,
249 physDev->horzRes, physDev->vertRes);
253 /**********************************************************************
256 BOOL PSDRV_CreateDC( DC *dc, PSDRV_PDEVICE **pdev, LPCSTR driver, LPCSTR device,
257 LPCSTR output, const DEVMODEA* initData )
259 PSDRV_PDEVICE *physDev;
262 /* If no device name was specified, retrieve the device name
263 * from the DEVMODE structure from the DC's physDev.
264 * (See CreateCompatibleDC) */
265 if ( !device && *pdev )
268 device = physDev->Devmode->dmPublic.dmDeviceName;
270 pi = PSDRV_FindPrinterInfo(device);
272 TRACE("(%s %s %s %p)\n", driver, device, output, initData);
274 if(!pi) return FALSE;
277 MESSAGE("To use WINEPS you need to install some AFM files.\n");
281 physDev = (PSDRV_PDEVICE *)HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
283 if (!physDev) return FALSE;
285 physDev->hdc = dc->hSelf;
290 physDev->Devmode = (PSDRV_DEVMODEA *)HeapAlloc( PSDRV_Heap, 0,
291 sizeof(PSDRV_DEVMODEA) );
292 if(!physDev->Devmode) {
293 HeapFree( PSDRV_Heap, 0, physDev );
297 memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
299 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
300 physDev->logPixelsY = physDev->pi->ppd->DefaultResolution;
303 physDev->job.output = HeapAlloc( PSDRV_Heap, 0, strlen(output)+1 );
304 strcpy( physDev->job.output, output );
306 physDev->job.output = NULL;
307 physDev->job.hJob = 0;
310 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)initData, pi);
313 PSDRV_UpdateDevCaps(physDev);
314 dc->hFont = PSDRV_DefaultFont;
320 /**********************************************************************
323 BOOL PSDRV_DeleteDC( PSDRV_PDEVICE *physDev )
327 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
328 if(physDev->job.output)
329 HeapFree( PSDRV_Heap, 0, physDev->job.output );
330 HeapFree( PSDRV_Heap, 0, physDev );
336 /**********************************************************************
339 HDC PSDRV_ResetDC( PSDRV_PDEVICE *physDev, const DEVMODEA *lpInitData )
342 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)lpInitData, physDev->pi);
343 PSDRV_UpdateDevCaps(physDev);
348 /***********************************************************************
349 * GetDeviceCaps (WINEPS.@)
351 INT PSDRV_GetDeviceCaps( PSDRV_PDEVICE *physDev, INT cap )
358 return DT_RASPRINTER;
360 return MulDiv(physDev->horzSize, 100,
361 physDev->Devmode->dmPublic.dmScale);
363 return MulDiv(physDev->vertSize, 100,
364 physDev->Devmode->dmPublic.dmScale);
366 return physDev->horzRes;
368 return physDev->vertRes;
370 return (physDev->pi->ppd->ColorDevice ? 8 : 1);
382 return (physDev->pi->ppd->ColorDevice ? 256 : -1);
384 return sizeof(PSDRV_PDEVICE);
386 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
387 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
389 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
390 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
392 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
393 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
395 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
399 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
400 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
401 /* Are aspect[XY] and logPixels[XY] correct? */
402 /* Need to handle different res in x and y => fix ppd */
405 return physDev->pi->ppd->DefaultResolution;
407 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
408 (double)physDev->pi->ppd->DefaultResolution );
410 return MulDiv(physDev->logPixelsX,
411 physDev->Devmode->dmPublic.dmScale, 100);
413 return MulDiv(physDev->logPixelsY,
414 physDev->Devmode->dmPublic.dmScale, 100);
422 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
423 physDev->PageSize.cy : physDev->PageSize.cx;
425 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
426 physDev->PageSize.cx : physDev->PageSize.cy;
427 case PHYSICALOFFSETX:
428 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
429 if(physDev->pi->ppd->LandscapeOrientation == -90)
430 return physDev->PageSize.cy - physDev->ImageableArea.top;
432 return physDev->ImageableArea.bottom;
434 return physDev->ImageableArea.left;
436 case PHYSICALOFFSETY:
437 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
438 if(physDev->pi->ppd->LandscapeOrientation == -90)
439 return physDev->PageSize.cx - physDev->ImageableArea.right;
441 return physDev->ImageableArea.left;
443 return physDev->PageSize.cy - physDev->ImageableArea.top;
453 FIXME("(%p): unsupported capability %d, will return 0\n", physDev->hdc, cap );
459 /**********************************************************************
460 * PSDRV_FindPrinterInfo
462 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
464 static PRINTERINFO *PSDRV_PrinterList;
465 DWORD type = REG_BINARY, needed, res, dwPaperSize;
466 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
470 const char *ppd = NULL;
472 char* ppdFileName = NULL;
474 BOOL using_default_devmode = FALSE;
476 TRACE("'%s'\n", name);
479 * If this loop completes, last will point to the 'next' element of the
480 * final PRINTERINFO in the list
482 for( ; pi; last = &pi->next, pi = pi->next)
483 if(!strcmp(pi->FriendlyName, name))
486 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
490 if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
491 strcpy( pi->FriendlyName, name );
493 /* Use Get|SetPrinterDataExA instead? */
495 res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
498 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
499 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
500 if (pi->Devmode == NULL)
502 memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
503 strcpy(pi->Devmode->dmPublic.dmDeviceName,name);
504 using_default_devmode = TRUE;
506 /* need to do something here AddPrinter?? */
509 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
510 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
511 (LPBYTE)pi->Devmode, needed, &needed);
514 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
515 ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
519 #ifdef HAVE_CUPS_CUPS_H
520 if (cupshandle != (void*)-1) {
521 typeof(cupsGetPPD) * pcupsGetPPD = NULL;
523 pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
525 ppd = pcupsGetPPD(name);
528 needed=strlen(ppd)+1;
529 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
530 memcpy(ppdFileName, ppd, needed);
533 /* we should unlink() that file later */
535 res = ERROR_FILE_NOT_FOUND;
536 WARN("Did not find ppd for %s\n",name);
542 res = GetPrinterDataA(hPrinter, "PPD File", NULL, NULL, 0, &needed);
543 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
544 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
545 res = GetPrinterDataA(hPrinter, "PPD File", &ppdType, ppdFileName, needed, &needed);
548 /* Look for a ppd file for this printer in the config file.
549 * First look under that printer's name, and then under 'generic'
551 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppd", &hkey))
553 const char* value_name;
555 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
557 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
558 value_name="generic";
563 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
564 RegQueryValueExA(hkey, value_name, 0, &ppdType, ppdFileName, &needed);
570 res = ERROR_FILE_NOT_FOUND;
571 ERR ("Error %li getting PPD file name for printer '%s'\n", res, name);
575 if (ppdType==REG_EXPAND_SZ) {
578 /* Expand environment variable references */
579 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
580 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
581 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
582 HeapFree(PSDRV_Heap, 0, ppdFileName);
587 pi->ppd = PSDRV_ParsePPD(ppdFileName);
589 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
594 /* Some gimp-print ppd files don't contain a DefaultResolution line
595 set it to 300 if it's not specified */
596 if(pi->ppd->DefaultResolution == 0)
597 pi->ppd->DefaultResolution = 300;
599 if(using_default_devmode) {
602 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
603 (LPWSTR)&papersize, sizeof(papersize))) {
605 memset(&dm, 0, sizeof(dm));
606 dm.dmPublic.dmFields = DM_PAPERSIZE;
607 dm.dmPublic.u1.s1.dmPaperSize = papersize;
608 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
610 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
611 REG_BINARY, (LPBYTE)pi->Devmode, sizeof(DefaultDevmode) );
614 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
616 memset(&dm, 0, sizeof(dm));
617 dm.dmPublic.dmFields = DM_PAPERSIZE;
618 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
619 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
623 * This is a hack. The default paper size should be read in as part of
624 * the Devmode structure, but Wine doesn't currently provide a convenient
625 * way to configure printers.
627 res = GetPrinterDataA (hPrinter, "Paper Size", NULL, (LPBYTE) &dwPaperSize,
628 sizeof (DWORD), &needed);
629 if (res == ERROR_SUCCESS)
630 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
631 else if (res == ERROR_FILE_NOT_FOUND)
632 TRACE ("No 'Paper Size' for printer '%s'\n", name);
634 ERR ("GetPrinterDataA returned %li\n", res);
638 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
639 0, &needed, &pi->FontSubTableSize);
640 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
641 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
643 else if (res == ERROR_MORE_DATA) {
644 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
645 if (pi->FontSubTable == NULL) {
646 ERR ("Failed to allocate %li bytes from heap\n", needed);
650 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
651 (LPBYTE) pi->FontSubTable, needed, &needed,
652 &pi->FontSubTableSize);
653 if (res != ERROR_SUCCESS) {
654 ERR ("EnumPrinterDataExA returned %li\n", res);
659 ERR("EnumPrinterDataExA returned %li\n", res);
663 if (ClosePrinter (hPrinter) == 0) {
664 ERR ("ClosePrinter failed with code %li\n", GetLastError ());
671 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
672 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
674 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
675 "ignoring\n", font->Name);
679 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
680 PSDRV_FreeAFMList(pi->Fonts);
686 if (ppd) unlink(ppd);
690 ClosePrinter(hPrinter);
693 HeapFree(PSDRV_Heap, 0, ppdFileName);
694 if (pi->FontSubTable)
695 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
696 if (pi->FriendlyName)
697 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
699 HeapFree(PSDRV_Heap, 0, pi->Devmode);
701 HeapFree(PSDRV_Heap, 0, pi);
702 if (ppd) unlink(ppd);