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_A4,
55 /* dmPaperLength */ 2969,
56 /* dmPaperWidth */ 2101
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;
178 physDev->Devmode = (PSDRV_DEVMODEA *)HeapAlloc( PSDRV_Heap, 0,
179 sizeof(PSDRV_DEVMODEA) );
180 if(!physDev->Devmode) {
181 HeapFree( PSDRV_Heap, 0, physDev );
185 memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
188 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)initData, pi);
191 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
192 physDev->logPixelsY = physDev->pi->ppd->DefaultResolution;
194 for(page = pi->ppd->PageSizes; page; page = page->next) {
195 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
200 FIXME("Can't find page\n");
201 physDev->PageSize.left = 0;
202 physDev->PageSize.right = 0;
203 physDev->PageSize.bottom = 0;
204 physDev->PageSize.top = 0;
205 } else if(page->ImageableArea) { /* PageSize is in device units */
206 physDev->PageSize.left = page->ImageableArea->llx * physDev->logPixelsX / 72;
207 physDev->PageSize.right = page->ImageableArea->urx * physDev->logPixelsX / 72;
208 physDev->PageSize.bottom = page->ImageableArea->lly * physDev->logPixelsY / 72;
209 physDev->PageSize.top = page->ImageableArea->ury * physDev->logPixelsY / 72;
211 physDev->PageSize.left = physDev->PageSize.bottom = 0;
212 physDev->PageSize.right = page->PaperDimension->x * physDev->logPixelsX / 72;
213 physDev->PageSize.top = page->PaperDimension->y * physDev->logPixelsY / 72;
215 TRACE("PageSize = (%d,%d - %d,%d)\n",physDev->PageSize.left, physDev->PageSize.bottom, physDev->PageSize.right, physDev->PageSize.top);
217 /* these are in device units */
218 width = physDev->PageSize.right - physDev->PageSize.left;
219 height = physDev->PageSize.top - physDev->PageSize.bottom;
221 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
222 physDev->horzRes = width;
223 physDev->vertRes = height;
225 physDev->horzRes = height;
226 physDev->vertRes = width;
229 /* these are in mm */
230 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
231 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
233 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
234 "horzRes = %d, vertRes = %d\n",
235 physDev->horzSize, physDev->vertSize,
236 physDev->horzRes, physDev->vertRes);
240 dc->hVisRgn = CreateRectRgn(0, 0, physDev->horzRes, physDev->vertRes);
241 dc->hFont = PSDRV_DefaultFont;
243 if (!output) output = "LPT1:"; /* HACK */
244 physDev->job.output = HeapAlloc( PSDRV_Heap, 0, strlen(output)+1 );
245 strcpy( physDev->job.output, output );
246 physDev->job.hJob = 0;
251 /**********************************************************************
254 BOOL PSDRV_DeleteDC( DC *dc )
256 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
260 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
261 HeapFree( PSDRV_Heap, 0, physDev->job.output );
262 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( DC *dc, INT cap )
347 PSDRV_PDEVICE *physDev = dc->physDev;
355 return DT_RASPRINTER;
357 return physDev->horzSize;
359 return physDev->vertSize;
361 return physDev->horzRes;
363 return physDev->vertRes;
365 return (physDev->pi->ppd->ColorDevice ? 8 : 1);
377 return (physDev->pi->ppd->ColorDevice ? 256 : -1);
379 return sizeof(PSDRV_PDEVICE);
381 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
382 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
384 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
385 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
387 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
388 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
390 return TC_CR_ANY; /* psdrv 0x59f7 */
394 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
395 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
396 /* Are aspect[XY] and logPixels[XY] correct? */
397 /* Need to handle different res in x and y => fix ppd */
400 return physDev->pi->ppd->DefaultResolution;
402 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
403 (double)physDev->pi->ppd->DefaultResolution );
405 return physDev->logPixelsX;
407 return physDev->logPixelsY;
415 get_phys_page_size( physDev, &pt );
418 get_phys_page_size( physDev, &pt );
420 case PHYSICALOFFSETX:
421 case PHYSICALOFFSETY:
430 FIXME("(%04x): unsupported capability %d, will return 0\n", dc->hSelf, cap );
436 /**********************************************************************
437 * PSDRV_FindPrinterInfo
439 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
441 static PRINTERINFO *PSDRV_PrinterList;
442 DWORD type = REG_BINARY, needed, res, dwPaperSize;
443 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
447 const char *ppd = NULL;
449 char* ppdFileName = NULL;
452 TRACE("'%s'\n", name);
455 * If this loop completes, last will point to the 'next' element of the
456 * final PRINTERINFO in the list
458 for( ; pi; last = &pi->next, pi = pi->next)
459 if(!strcmp(pi->FriendlyName, name))
462 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
466 if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
467 strcpy( pi->FriendlyName, name );
469 /* Use Get|SetPrinterDataExA instead? */
471 res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
474 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
475 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
476 if (pi->Devmode == NULL)
478 memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
479 strcpy(pi->Devmode->dmPublic.dmDeviceName,name);
480 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
481 REG_BINARY, (LPBYTE)&DefaultDevmode, sizeof(DefaultDevmode) );
483 /* need to do something here AddPrinter?? */
486 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
487 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
488 (LPBYTE)pi->Devmode, needed, &needed);
491 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
492 ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
498 ppd = cupsGetPPD(name);
501 needed=strlen(ppd)+1;
502 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
503 memcpy(ppdFileName, ppd, needed);
506 /* we should unlink() that file later */
508 res = ERROR_FILE_NOT_FOUND;
509 WARN("Did not find ppd for %s\n",name);
515 res = GetPrinterDataA(hPrinter, "PPD File", NULL, NULL, 0, &needed);
516 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
517 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
518 res = GetPrinterDataA(hPrinter, "PPD File", &ppdType, ppdFileName, needed, &needed);
521 /* Look for a ppd file for this printer in the config file.
522 * First look under that printer's name, and then under 'generic'
524 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppd", &hkey))
526 const char* value_name;
528 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
530 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
531 value_name="generic";
536 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
537 RegQueryValueExA(hkey, value_name, 0, &ppdType, ppdFileName, &needed);
543 res = ERROR_FILE_NOT_FOUND;
544 ERR ("Error %li getting PPD file name for printer '%s'\n", res, name);
548 if (ppdType==REG_EXPAND_SZ) {
551 /* Expand environment variable references */
552 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
553 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
554 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
555 HeapFree(PSDRV_Heap, 0, ppdFileName);
560 pi->ppd = PSDRV_ParsePPD(ppdFileName);
562 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
568 * This is a hack. The default paper size should be read in as part of
569 * the Devmode structure, but Wine doesn't currently provide a convenient
570 * way to configure printers.
572 res = GetPrinterDataA (hPrinter, "Paper Size", NULL, (LPBYTE) &dwPaperSize,
573 sizeof (DWORD), &needed);
574 if (res == ERROR_SUCCESS)
575 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
576 else if (res == ERROR_FILE_NOT_FOUND)
577 TRACE ("No 'Paper Size' for printer '%s'\n", name);
579 ERR ("GetPrinterDataA returned %li\n", res);
583 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
584 0, &needed, &pi->FontSubTableSize);
585 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
586 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
588 else if (res == ERROR_MORE_DATA) {
589 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
590 if (pi->FontSubTable == NULL) {
591 ERR ("Failed to allocate %li bytes from heap\n", needed);
595 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
596 (LPBYTE) pi->FontSubTable, needed, &needed,
597 &pi->FontSubTableSize);
598 if (res != ERROR_SUCCESS) {
599 ERR ("EnumPrinterDataExA returned %li\n", res);
604 ERR("EnumPrinterDataExA returned %li\n", res);
608 if (ClosePrinter (hPrinter) == 0) {
609 ERR ("ClosePrinter failed with code %li\n", GetLastError ());
616 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
617 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
619 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
620 "ignoring\n", font->Name);
624 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
625 PSDRV_FreeAFMList(pi->Fonts);
631 if (ppd) unlink(ppd);
635 ClosePrinter(hPrinter);
638 HeapFree(PSDRV_Heap, 0, ppdFileName);
639 if (pi->FontSubTable)
640 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
641 if (pi->FriendlyName)
642 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
644 HeapFree(PSDRV_Heap, 0, pi->Devmode);
646 HeapFree(PSDRV_Heap, 0, pi);
647 if (ppd) unlink(ppd);