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);
321 /***********************************************************************
322 * GetDeviceCaps (WINEPS.@)
324 INT PSDRV_GetDeviceCaps( PSDRV_PDEVICE *physDev, INT cap )
331 return DT_RASPRINTER;
333 return MulDiv(physDev->horzSize, 100,
334 physDev->Devmode->dmPublic.dmScale);
336 return MulDiv(physDev->vertSize, 100,
337 physDev->Devmode->dmPublic.dmScale);
339 return physDev->horzRes;
341 return physDev->vertRes;
343 return (physDev->pi->ppd->ColorDevice ? 8 : 1);
355 return (physDev->pi->ppd->ColorDevice ? 256 : -1);
357 return sizeof(PSDRV_PDEVICE);
359 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
360 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
362 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
363 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
365 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
366 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
368 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
372 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
373 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
374 /* Are aspect[XY] and logPixels[XY] correct? */
375 /* Need to handle different res in x and y => fix ppd */
378 return physDev->pi->ppd->DefaultResolution;
380 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
381 (double)physDev->pi->ppd->DefaultResolution );
383 return MulDiv(physDev->logPixelsX,
384 physDev->Devmode->dmPublic.dmScale, 100);
386 return MulDiv(physDev->logPixelsY,
387 physDev->Devmode->dmPublic.dmScale, 100);
395 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
396 physDev->PageSize.cy : physDev->PageSize.cx;
398 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
399 physDev->PageSize.cx : physDev->PageSize.cy;
400 case PHYSICALOFFSETX:
401 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
402 if(physDev->pi->ppd->LandscapeOrientation == -90)
403 return physDev->PageSize.cy - physDev->ImageableArea.top;
405 return physDev->ImageableArea.bottom;
407 return physDev->ImageableArea.left;
409 case PHYSICALOFFSETY:
410 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
411 if(physDev->pi->ppd->LandscapeOrientation == -90)
412 return physDev->PageSize.cx - physDev->ImageableArea.right;
414 return physDev->ImageableArea.left;
416 return physDev->PageSize.cy - physDev->ImageableArea.top;
426 FIXME("(%04x): unsupported capability %d, will return 0\n", physDev->hdc, cap );
432 /**********************************************************************
433 * PSDRV_FindPrinterInfo
435 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
437 static PRINTERINFO *PSDRV_PrinterList;
438 DWORD type = REG_BINARY, needed, res, dwPaperSize;
439 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
443 const char *ppd = NULL;
445 char* ppdFileName = NULL;
448 TRACE("'%s'\n", name);
451 * If this loop completes, last will point to the 'next' element of the
452 * final PRINTERINFO in the list
454 for( ; pi; last = &pi->next, pi = pi->next)
455 if(!strcmp(pi->FriendlyName, name))
458 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
462 if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
463 strcpy( pi->FriendlyName, name );
465 /* Use Get|SetPrinterDataExA instead? */
467 res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
470 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
471 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
472 if (pi->Devmode == NULL)
474 memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
475 strcpy(pi->Devmode->dmPublic.dmDeviceName,name);
476 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
477 REG_BINARY, (LPBYTE)&DefaultDevmode, sizeof(DefaultDevmode) );
479 /* need to do something here AddPrinter?? */
482 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
483 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
484 (LPBYTE)pi->Devmode, needed, &needed);
487 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
488 ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
494 ppd = cupsGetPPD(name);
497 needed=strlen(ppd)+1;
498 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
499 memcpy(ppdFileName, ppd, needed);
502 /* we should unlink() that file later */
504 res = ERROR_FILE_NOT_FOUND;
505 WARN("Did not find ppd for %s\n",name);
511 res = GetPrinterDataA(hPrinter, "PPD File", NULL, NULL, 0, &needed);
512 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
513 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
514 res = GetPrinterDataA(hPrinter, "PPD File", &ppdType, ppdFileName, needed, &needed);
517 /* Look for a ppd file for this printer in the config file.
518 * First look under that printer's name, and then under 'generic'
520 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppd", &hkey))
522 const char* value_name;
524 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
526 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
527 value_name="generic";
532 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
533 RegQueryValueExA(hkey, value_name, 0, &ppdType, ppdFileName, &needed);
539 res = ERROR_FILE_NOT_FOUND;
540 ERR ("Error %li getting PPD file name for printer '%s'\n", res, name);
544 if (ppdType==REG_EXPAND_SZ) {
547 /* Expand environment variable references */
548 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
549 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
550 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
551 HeapFree(PSDRV_Heap, 0, ppdFileName);
556 pi->ppd = PSDRV_ParsePPD(ppdFileName);
558 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
564 * This is a hack. The default paper size should be read in as part of
565 * the Devmode structure, but Wine doesn't currently provide a convenient
566 * way to configure printers.
568 res = GetPrinterDataA (hPrinter, "Paper Size", NULL, (LPBYTE) &dwPaperSize,
569 sizeof (DWORD), &needed);
570 if (res == ERROR_SUCCESS)
571 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
572 else if (res == ERROR_FILE_NOT_FOUND)
573 TRACE ("No 'Paper Size' for printer '%s'\n", name);
575 ERR ("GetPrinterDataA returned %li\n", res);
579 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
580 0, &needed, &pi->FontSubTableSize);
581 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
582 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
584 else if (res == ERROR_MORE_DATA) {
585 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
586 if (pi->FontSubTable == NULL) {
587 ERR ("Failed to allocate %li bytes from heap\n", needed);
591 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
592 (LPBYTE) pi->FontSubTable, needed, &needed,
593 &pi->FontSubTableSize);
594 if (res != ERROR_SUCCESS) {
595 ERR ("EnumPrinterDataExA returned %li\n", res);
600 ERR("EnumPrinterDataExA returned %li\n", res);
604 if (ClosePrinter (hPrinter) == 0) {
605 ERR ("ClosePrinter failed with code %li\n", GetLastError ());
612 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
613 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
615 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
616 "ignoring\n", font->Name);
620 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
621 PSDRV_FreeAFMList(pi->Fonts);
627 if (ppd) unlink(ppd);
631 ClosePrinter(hPrinter);
634 HeapFree(PSDRV_Heap, 0, ppdFileName);
635 if (pi->FontSubTable)
636 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
637 if (pi->FriendlyName)
638 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
640 HeapFree(PSDRV_Heap, 0, pi->Devmode);
642 HeapFree(PSDRV_Heap, 0, pi);
643 if (ppd) unlink(ppd);