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 /**********************************************************************
144 BOOL PSDRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
145 LPCSTR output, const DEVMODEA* initData )
147 PSDRV_PDEVICE *physDev;
150 INT width = 0, height = 0;
152 /* If no device name was specified, retrieve the device name
153 * from the DEVMODE structure from the DC's physDev.
154 * (See CreateCompatibleDC) */
155 if ( !device && dc->physDev )
157 physDev = (PSDRV_PDEVICE *)dc->physDev;
158 device = physDev->Devmode->dmPublic.dmDeviceName;
160 pi = PSDRV_FindPrinterInfo(device);
162 TRACE("(%s %s %s %p)\n", driver, device, output, initData);
164 if(!pi) return FALSE;
167 MESSAGE("To use WINEPS you need to install some AFM files.\n");
171 physDev = (PSDRV_PDEVICE *)HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
173 if (!physDev) return FALSE;
174 dc->physDev = (PHYSDEV)physDev;
175 physDev->hdc = dc->hSelf;
180 physDev->Devmode = (PSDRV_DEVMODEA *)HeapAlloc( PSDRV_Heap, 0,
181 sizeof(PSDRV_DEVMODEA) );
182 if(!physDev->Devmode) {
183 HeapFree( PSDRV_Heap, 0, physDev );
187 memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
190 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)initData, pi);
193 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
194 physDev->logPixelsY = physDev->pi->ppd->DefaultResolution;
196 for(page = pi->ppd->PageSizes; page; page = page->next) {
197 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
202 FIXME("Can't find page\n");
203 physDev->PageSize.left = 0;
204 physDev->PageSize.right = 0;
205 physDev->PageSize.bottom = 0;
206 physDev->PageSize.top = 0;
207 } else if(page->ImageableArea) { /* PageSize is in device units */
208 physDev->PageSize.left = page->ImageableArea->llx * physDev->logPixelsX / 72;
209 physDev->PageSize.right = page->ImageableArea->urx * physDev->logPixelsX / 72;
210 physDev->PageSize.bottom = page->ImageableArea->lly * physDev->logPixelsY / 72;
211 physDev->PageSize.top = page->ImageableArea->ury * physDev->logPixelsY / 72;
213 physDev->PageSize.left = physDev->PageSize.bottom = 0;
214 physDev->PageSize.right = page->PaperDimension->x * physDev->logPixelsX / 72;
215 physDev->PageSize.top = page->PaperDimension->y * physDev->logPixelsY / 72;
217 TRACE("PageSize = (%d,%d - %d,%d)\n",physDev->PageSize.left, physDev->PageSize.bottom, physDev->PageSize.right, physDev->PageSize.top);
219 /* these are in device units */
220 width = physDev->PageSize.right - physDev->PageSize.left;
221 height = physDev->PageSize.top - physDev->PageSize.bottom;
223 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
224 physDev->horzRes = width;
225 physDev->vertRes = height;
227 physDev->horzRes = height;
228 physDev->vertRes = width;
231 /* these are in mm */
232 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
233 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
235 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
236 "horzRes = %d, vertRes = %d\n",
237 physDev->horzSize, physDev->vertSize,
238 physDev->horzRes, physDev->vertRes);
242 dc->hVisRgn = CreateRectRgn(0, 0, physDev->horzRes, physDev->vertRes);
243 dc->hFont = PSDRV_DefaultFont;
245 if (!output) output = "LPT1:"; /* HACK */
246 physDev->job.output = HeapAlloc( PSDRV_Heap, 0, strlen(output)+1 );
247 strcpy( physDev->job.output, output );
248 physDev->job.hJob = 0;
253 /**********************************************************************
256 BOOL PSDRV_DeleteDC( PSDRV_PDEVICE *physDev )
260 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
261 HeapFree( PSDRV_Heap, 0, physDev->job.output );
262 physDev->dc->physDev = NULL;
263 HeapFree( PSDRV_Heap, 0, physDev );
269 /***********************************************************************
272 * Helper function to compute PHYSICALWIDTH and PHYSICALHEIGHT dev caps.
274 static void get_phys_page_size( const PSDRV_PDEVICE *pdev, POINT *p )
278 if ((pdev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) != 0 &&
279 pdev->Devmode->dmPublic.u1.s1.dmPaperSize != 0)
281 PAGESIZE *page = pdev->pi->ppd->PageSizes;
285 if (page->WinPage == pdev->Devmode->dmPublic.u1.s1.dmPaperSize)
292 ERR("No entry for papersize %u in PPD file for '%s'\n",
293 pdev->Devmode->dmPublic.u1.s1.dmPaperSize,
294 pdev->pi->FriendlyName);
298 TRACE("Found '%s' for paper size %u\n", page->FullName,
299 pdev->Devmode->dmPublic.u1.s1.dmPaperSize);
301 p->x = page->PaperDimension->x * pdev->logPixelsX / 72;
302 p->y = page->PaperDimension->y * pdev->logPixelsY / 72;
304 TRACE("%fx%f PostScript points = %lix%li device units\n",
305 page->PaperDimension->x, page->PaperDimension->y,
309 /* These are in tenths of a millimeter */
310 if ((pdev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH) != 0 &&
311 pdev->Devmode->dmPublic.u1.s1.dmPaperWidth != 0)
313 p->x = (pdev->Devmode->dmPublic.u1.s1.dmPaperWidth *
314 pdev->logPixelsX) / 254;
315 TRACE("dmPaperWidth = %li device units\n", p->x);
318 if ((pdev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) != 0 &&
319 pdev->Devmode->dmPublic.u1.s1.dmPaperLength != 0)
321 p->y = (pdev->Devmode->dmPublic.u1.s1.dmPaperLength *
322 pdev->logPixelsY) / 254;
323 TRACE("dmPaperLength = %li device units\n", p->y);
326 if (p->x == 0 || p->y == 0)
328 ERR("Paper size not properly set for '%s'\n", pdev->pi->FriendlyName);
332 if ((pdev->Devmode->dmPublic.dmFields & DM_ORIENTATION) != 0 &&
333 pdev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE)
342 /***********************************************************************
343 * GetDeviceCaps (WINEPS.@)
345 INT PSDRV_GetDeviceCaps( PSDRV_PDEVICE *physDev, INT cap )
354 return DT_RASPRINTER;
356 return physDev->horzSize;
358 return physDev->vertSize;
360 return physDev->horzRes;
362 return physDev->vertRes;
364 return (physDev->pi->ppd->ColorDevice ? 8 : 1);
376 return (physDev->pi->ppd->ColorDevice ? 256 : -1);
378 return sizeof(PSDRV_PDEVICE);
380 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
381 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
383 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
384 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
386 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
387 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
389 return TC_CR_ANY; /* psdrv 0x59f7 */
393 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
394 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
395 /* Are aspect[XY] and logPixels[XY] correct? */
396 /* Need to handle different res in x and y => fix ppd */
399 return physDev->pi->ppd->DefaultResolution;
401 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
402 (double)physDev->pi->ppd->DefaultResolution );
404 return physDev->logPixelsX;
406 return physDev->logPixelsY;
414 get_phys_page_size( physDev, &pt );
417 get_phys_page_size( physDev, &pt );
419 case PHYSICALOFFSETX:
420 case PHYSICALOFFSETY:
429 FIXME("(%04x): unsupported capability %d, will return 0\n", physDev->hdc, cap );
435 /**********************************************************************
436 * PSDRV_FindPrinterInfo
438 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
440 static PRINTERINFO *PSDRV_PrinterList;
441 DWORD type = REG_BINARY, needed, res, dwPaperSize;
442 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
446 const char *ppd = NULL;
448 char* ppdFileName = NULL;
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 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
480 REG_BINARY, (LPBYTE)&DefaultDevmode, sizeof(DefaultDevmode) );
482 /* need to do something here AddPrinter?? */
485 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
486 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
487 (LPBYTE)pi->Devmode, needed, &needed);
490 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
491 ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
497 ppd = cupsGetPPD(name);
500 needed=strlen(ppd)+1;
501 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
502 memcpy(ppdFileName, ppd, needed);
505 /* we should unlink() that file later */
507 res = ERROR_FILE_NOT_FOUND;
508 WARN("Did not find ppd for %s\n",name);
514 res = GetPrinterDataA(hPrinter, "PPD File", NULL, NULL, 0, &needed);
515 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
516 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
517 res = GetPrinterDataA(hPrinter, "PPD File", &ppdType, ppdFileName, needed, &needed);
520 /* Look for a ppd file for this printer in the config file.
521 * First look under that printer's name, and then under 'generic'
523 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppd", &hkey))
525 const char* value_name;
527 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
529 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
530 value_name="generic";
535 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
536 RegQueryValueExA(hkey, value_name, 0, &ppdType, ppdFileName, &needed);
542 res = ERROR_FILE_NOT_FOUND;
543 ERR ("Error %li getting PPD file name for printer '%s'\n", res, name);
547 if (ppdType==REG_EXPAND_SZ) {
550 /* Expand environment variable references */
551 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
552 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
553 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
554 HeapFree(PSDRV_Heap, 0, ppdFileName);
559 pi->ppd = PSDRV_ParsePPD(ppdFileName);
561 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
567 * This is a hack. The default paper size should be read in as part of
568 * the Devmode structure, but Wine doesn't currently provide a convenient
569 * way to configure printers.
571 res = GetPrinterDataA (hPrinter, "Paper Size", NULL, (LPBYTE) &dwPaperSize,
572 sizeof (DWORD), &needed);
573 if (res == ERROR_SUCCESS)
574 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
575 else if (res == ERROR_FILE_NOT_FOUND)
576 TRACE ("No 'Paper Size' for printer '%s'\n", name);
578 ERR ("GetPrinterDataA returned %li\n", res);
582 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
583 0, &needed, &pi->FontSubTableSize);
584 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
585 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
587 else if (res == ERROR_MORE_DATA) {
588 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
589 if (pi->FontSubTable == NULL) {
590 ERR ("Failed to allocate %li bytes from heap\n", needed);
594 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
595 (LPBYTE) pi->FontSubTable, needed, &needed,
596 &pi->FontSubTableSize);
597 if (res != ERROR_SUCCESS) {
598 ERR ("EnumPrinterDataExA returned %li\n", res);
603 ERR("EnumPrinterDataExA returned %li\n", res);
607 if (ClosePrinter (hPrinter) == 0) {
608 ERR ("ClosePrinter failed with code %li\n", GetLastError ());
615 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
616 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
618 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
619 "ignoring\n", font->Name);
623 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
624 PSDRV_FreeAFMList(pi->Fonts);
630 if (ppd) unlink(ppd);
634 ClosePrinter(hPrinter);
637 HeapFree(PSDRV_Heap, 0, ppdFileName);
638 if (pi->FontSubTable)
639 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
640 if (pi->FriendlyName)
641 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
643 HeapFree(PSDRV_Heap, 0, pi->Devmode);
645 HeapFree(PSDRV_Heap, 0, pi);
646 if (ppd) unlink(ppd);