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
29 #include "wine/debug.h"
35 # include <cups/cups.h>
38 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
40 static PSDRV_DEVMODEA DefaultDevmode =
43 /* dmDeviceName */ "Wine PostScript Driver",
44 /* dmSpecVersion */ 0x30a,
45 /* dmDriverVersion */ 0x001,
46 /* dmSize */ sizeof(DEVMODEA),
47 /* dmDriverExtra */ 0,
48 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE |
49 DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR |
50 DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION,
53 /* dmOrientation */ DMORIENT_PORTRAIT,
54 /* dmPaperSize */ DMPAPER_LETTER,
55 /* dmPaperLength */ 2794,
56 /* dmPaperWidth */ 2159
59 /* dmScale */ 100, /* ?? */
61 /* dmDefaultSource */ DMBIN_AUTO,
62 /* dmPrintQuality */ 0,
63 /* dmColor */ DMCOLOR_COLOR,
65 /* dmYResolution */ 0,
66 /* dmTTOption */ DMTT_SUBDEV,
69 /* dmUnusedPadding */ 0,
73 /* dmDisplayFlags */ 0,
74 /* dmDisplayFrequency */ 0,
81 /* dmPanningWidth */ 0,
82 /* dmPanningHeight */ 0
88 /* numInstalledOptions */ 0
92 HANDLE PSDRV_Heap = 0;
94 static HANDLE PSDRV_DefaultFont = 0;
95 static LOGFONTA DefaultLogFont = {
96 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
97 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
100 /*********************************************************************
103 * Initializes font metrics and registers driver. Called from GDI_Init()
106 BOOL WINAPI PSDRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
108 TRACE("(0x%4x, 0x%08lx, %p)\n", hinst, reason, reserved);
112 case DLL_PROCESS_ATTACH:
114 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
115 if (PSDRV_Heap == (HANDLE)NULL)
118 if (PSDRV_GetFontMetrics() == FALSE) {
119 HeapDestroy(PSDRV_Heap);
123 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
124 if (PSDRV_DefaultFont == (HANDLE)NULL) {
125 HeapDestroy(PSDRV_Heap);
130 case DLL_PROCESS_DETACH:
132 DeleteObject( PSDRV_DefaultFont );
133 HeapDestroy( PSDRV_Heap );
141 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
144 INT width = 0, height = 0;
146 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
147 for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
148 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
153 FIXME("Can't find page\n");
154 physDev->ImageableArea.left = 0;
155 physDev->ImageableArea.right = 0;
156 physDev->ImageableArea.bottom = 0;
157 physDev->ImageableArea.top = 0;
158 physDev->PageSize.cx = 0;
159 physDev->PageSize.cy = 0;
160 } else if(page->ImageableArea) {
161 /* physDev sizes in device units; ppd sizes in 1/72" */
162 physDev->ImageableArea.left = page->ImageableArea->llx *
163 physDev->logPixelsX / 72;
164 physDev->ImageableArea.right = page->ImageableArea->urx *
165 physDev->logPixelsX / 72;
166 physDev->ImageableArea.bottom = page->ImageableArea->lly *
167 physDev->logPixelsY / 72;
168 physDev->ImageableArea.top = page->ImageableArea->ury *
169 physDev->logPixelsY / 72;
170 physDev->PageSize.cx = page->PaperDimension->x *
171 physDev->logPixelsX / 72;
172 physDev->PageSize.cy = page->PaperDimension->y *
173 physDev->logPixelsY / 72;
175 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
176 physDev->ImageableArea.right = physDev->PageSize.cx =
177 page->PaperDimension->x * physDev->logPixelsX / 72;
178 physDev->ImageableArea.top = physDev->PageSize.cy =
179 page->PaperDimension->y * physDev->logPixelsY / 72;
181 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
182 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
183 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
184 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
185 physDev->ImageableArea.right = physDev->PageSize.cx =
186 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
187 physDev->logPixelsX / 254;
188 physDev->ImageableArea.top = physDev->PageSize.cy =
189 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
190 physDev->logPixelsY / 254;
192 FIXME("Odd dmFields %lx\n", physDev->Devmode->dmPublic.dmFields);
193 physDev->ImageableArea.left = 0;
194 physDev->ImageableArea.right = 0;
195 physDev->ImageableArea.bottom = 0;
196 physDev->ImageableArea.top = 0;
197 physDev->PageSize.cx = 0;
198 physDev->PageSize.cy = 0;
201 TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %ldx%ld\n",
202 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
203 physDev->ImageableArea.right, physDev->ImageableArea.top,
204 physDev->PageSize.cx, physDev->PageSize.cy);
206 /* these are in device units */
207 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
208 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
210 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
211 physDev->horzRes = width;
212 physDev->vertRes = height;
214 physDev->horzRes = height;
215 physDev->vertRes = width;
218 /* these are in mm */
219 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
220 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
222 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
223 "horzRes = %d, vertRes = %d\n",
224 physDev->horzSize, physDev->vertSize,
225 physDev->horzRes, physDev->vertRes);
229 /**********************************************************************
232 BOOL PSDRV_CreateDC( DC *dc, PSDRV_PDEVICE **pdev, LPCSTR driver, LPCSTR device,
233 LPCSTR output, const DEVMODEA* initData )
235 PSDRV_PDEVICE *physDev;
238 /* If no device name was specified, retrieve the device name
239 * from the DEVMODE structure from the DC's physDev.
240 * (See CreateCompatibleDC) */
241 if ( !device && *pdev )
244 device = physDev->Devmode->dmPublic.dmDeviceName;
246 pi = PSDRV_FindPrinterInfo(device);
248 TRACE("(%s %s %s %p)\n", driver, device, output, initData);
250 if(!pi) return FALSE;
253 MESSAGE("To use WINEPS you need to install some AFM files.\n");
257 physDev = (PSDRV_PDEVICE *)HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
259 if (!physDev) return FALSE;
261 physDev->hdc = dc->hSelf;
266 physDev->Devmode = (PSDRV_DEVMODEA *)HeapAlloc( PSDRV_Heap, 0,
267 sizeof(PSDRV_DEVMODEA) );
268 if(!physDev->Devmode) {
269 HeapFree( PSDRV_Heap, 0, physDev );
273 memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
275 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
276 physDev->logPixelsY = physDev->pi->ppd->DefaultResolution;
278 if (!output) output = "LPT1:"; /* HACK */
279 physDev->job.output = HeapAlloc( PSDRV_Heap, 0, strlen(output)+1 );
280 strcpy( physDev->job.output, output );
281 physDev->job.hJob = 0;
284 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)initData, pi);
287 PSDRV_UpdateDevCaps(physDev);
288 dc->hFont = PSDRV_DefaultFont;
294 /**********************************************************************
297 BOOL PSDRV_DeleteDC( PSDRV_PDEVICE *physDev )
301 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
302 HeapFree( PSDRV_Heap, 0, physDev->job.output );
303 HeapFree( PSDRV_Heap, 0, physDev );
309 /**********************************************************************
312 HDC PSDRV_ResetDC( PSDRV_PDEVICE *physDev, const DEVMODEA *lpInitData )
315 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)lpInitData, physDev->pi);
316 PSDRV_UpdateDevCaps(physDev);
322 /***********************************************************************
323 * GetDeviceCaps (WINEPS.@)
325 INT PSDRV_GetDeviceCaps( PSDRV_PDEVICE *physDev, INT cap )
332 return DT_RASPRINTER;
334 return physDev->horzSize;
336 return physDev->vertSize;
338 return physDev->horzRes;
340 return physDev->vertRes;
342 return (physDev->pi->ppd->ColorDevice ? 8 : 1);
354 return (physDev->pi->ppd->ColorDevice ? 256 : -1);
356 return sizeof(PSDRV_PDEVICE);
358 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
359 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
361 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
362 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
364 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
365 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
367 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
371 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
372 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
373 /* Are aspect[XY] and logPixels[XY] correct? */
374 /* Need to handle different res in x and y => fix ppd */
377 return physDev->pi->ppd->DefaultResolution;
379 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
380 (double)physDev->pi->ppd->DefaultResolution );
382 return physDev->logPixelsX;
384 return physDev->logPixelsY;
392 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
393 physDev->PageSize.cy : physDev->PageSize.cx;
395 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
396 physDev->PageSize.cx : physDev->PageSize.cy;
397 case PHYSICALOFFSETX:
398 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
399 if(physDev->pi->ppd->LandscapeOrientation == -90)
400 return physDev->PageSize.cy - physDev->ImageableArea.top;
402 return physDev->ImageableArea.bottom;
404 return physDev->ImageableArea.left;
406 case PHYSICALOFFSETY:
407 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
408 if(physDev->pi->ppd->LandscapeOrientation == -90)
409 return physDev->PageSize.cx - physDev->ImageableArea.right;
411 return physDev->ImageableArea.left;
413 return physDev->PageSize.cy - physDev->ImageableArea.top;
423 FIXME("(%04x): unsupported capability %d, will return 0\n", physDev->hdc, cap );
429 /**********************************************************************
430 * PSDRV_FindPrinterInfo
432 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
434 static PRINTERINFO *PSDRV_PrinterList;
435 DWORD type = REG_BINARY, needed, res, dwPaperSize;
436 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
440 const char *ppd = NULL;
442 char* ppdFileName = NULL;
445 TRACE("'%s'\n", name);
448 * If this loop completes, last will point to the 'next' element of the
449 * final PRINTERINFO in the list
451 for( ; pi; last = &pi->next, pi = pi->next)
452 if(!strcmp(pi->FriendlyName, name))
455 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
459 if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
460 strcpy( pi->FriendlyName, name );
462 /* Use Get|SetPrinterDataExA instead? */
464 res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
467 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
468 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
469 if (pi->Devmode == NULL)
471 memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
472 strcpy(pi->Devmode->dmPublic.dmDeviceName,name);
473 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
474 REG_BINARY, (LPBYTE)&DefaultDevmode, sizeof(DefaultDevmode) );
476 /* need to do something here AddPrinter?? */
479 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
480 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
481 (LPBYTE)pi->Devmode, needed, &needed);
484 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
485 ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
491 ppd = cupsGetPPD(name);
494 needed=strlen(ppd)+1;
495 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
496 memcpy(ppdFileName, ppd, needed);
499 /* we should unlink() that file later */
501 res = ERROR_FILE_NOT_FOUND;
502 WARN("Did not find ppd for %s\n",name);
508 res = GetPrinterDataA(hPrinter, "PPD File", NULL, NULL, 0, &needed);
509 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
510 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
511 res = GetPrinterDataA(hPrinter, "PPD File", &ppdType, ppdFileName, needed, &needed);
514 /* Look for a ppd file for this printer in the config file.
515 * First look under that printer's name, and then under 'generic'
517 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppd", &hkey))
519 const char* value_name;
521 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
523 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
524 value_name="generic";
529 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
530 RegQueryValueExA(hkey, value_name, 0, &ppdType, ppdFileName, &needed);
536 res = ERROR_FILE_NOT_FOUND;
537 ERR ("Error %li getting PPD file name for printer '%s'\n", res, name);
541 if (ppdType==REG_EXPAND_SZ) {
544 /* Expand environment variable references */
545 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
546 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
547 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
548 HeapFree(PSDRV_Heap, 0, ppdFileName);
553 pi->ppd = PSDRV_ParsePPD(ppdFileName);
555 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
561 * This is a hack. The default paper size should be read in as part of
562 * the Devmode structure, but Wine doesn't currently provide a convenient
563 * way to configure printers.
565 res = GetPrinterDataA (hPrinter, "Paper Size", NULL, (LPBYTE) &dwPaperSize,
566 sizeof (DWORD), &needed);
567 if (res == ERROR_SUCCESS)
568 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
569 else if (res == ERROR_FILE_NOT_FOUND)
570 TRACE ("No 'Paper Size' for printer '%s'\n", name);
572 ERR ("GetPrinterDataA returned %li\n", res);
576 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
577 0, &needed, &pi->FontSubTableSize);
578 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
579 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
581 else if (res == ERROR_MORE_DATA) {
582 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
583 if (pi->FontSubTable == NULL) {
584 ERR ("Failed to allocate %li bytes from heap\n", needed);
588 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
589 (LPBYTE) pi->FontSubTable, needed, &needed,
590 &pi->FontSubTableSize);
591 if (res != ERROR_SUCCESS) {
592 ERR ("EnumPrinterDataExA returned %li\n", res);
597 ERR("EnumPrinterDataExA returned %li\n", res);
601 if (ClosePrinter (hPrinter) == 0) {
602 ERR ("ClosePrinter failed with code %li\n", GetLastError ());
609 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
610 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
612 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
613 "ignoring\n", font->Name);
617 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
618 PSDRV_FreeAFMList(pi->Fonts);
624 if (ppd) unlink(ppd);
628 ClosePrinter(hPrinter);
631 HeapFree(PSDRV_Heap, 0, ppdFileName);
632 if (pi->FontSubTable)
633 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
634 if (pi->FriendlyName)
635 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
637 HeapFree(PSDRV_Heap, 0, pi->Devmode);
639 HeapFree(PSDRV_Heap, 0, pi);
640 if (ppd) unlink(ppd);