2 * PostScript driver initialization functions
4 * Copyright 1998 Huw D M Davies
5 * Copyright 2001 Marcus Meissner
16 #include "debugtools.h"
22 # include <cups/cups.h>
25 DEFAULT_DEBUG_CHANNEL(psdrv);
27 static BOOL PSDRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
28 LPCSTR output, const DEVMODEA* initData );
29 static BOOL PSDRV_DeleteDC( DC *dc );
31 static const DC_FUNCTIONS PSDRV_Funcs =
34 NULL, /* pAbortPath */
38 NULL, /* pBeginPath */
40 NULL, /* pBitmapBits */
41 NULL, /* pChoosePixelFormat */
42 PSDRV_Chord, /* pChord */
43 NULL, /* pCloseFigure */
44 NULL, /* pCreateBitmap */
45 PSDRV_CreateDC, /* pCreateDC */
46 NULL, /* pCreateDIBSection */
47 PSDRV_DeleteDC, /* pDeleteDC */
48 NULL, /* pDeleteObject */
49 NULL, /* pDescribePixelFormat */
50 PSDRV_DeviceCapabilities, /* pDeviceCapabilities */
51 PSDRV_Ellipse, /* pEllipse */
52 PSDRV_EndDoc, /* pEndDoc */
53 PSDRV_EndPage, /* pEndPage */
55 PSDRV_EnumDeviceFonts, /* pEnumDeviceFonts */
56 PSDRV_Escape, /* pEscape */
57 NULL, /* pExcludeClipRect */
58 PSDRV_ExtDeviceMode, /* pExtDeviceMode */
59 NULL, /* pExtFloodFill */
60 PSDRV_ExtTextOut, /* pExtTextOut */
63 NULL, /* pFlattenPath */
65 PSDRV_GetCharWidth, /* pGetCharWidth */
66 NULL, /* pGetDCOrgEx */
67 NULL, /* pGetDeviceGammaRamp */
69 NULL, /* pGetPixelFormat */
70 PSDRV_GetTextExtentPoint, /* pGetTextExtentPoint */
71 PSDRV_GetTextMetrics, /* pGetTextMetrics */
72 NULL, /* pIntersectClipRect */
73 NULL, /* pInvertRgn */
74 PSDRV_LineTo, /* pLineTo */
76 NULL, /* pOffsetClipRgn */
77 NULL, /* pOffsetViewportOrg (optional) */
78 NULL, /* pOffsetWindowOrg (optional) */
80 PSDRV_PatBlt, /* pPatBlt */
82 NULL, /* pPolyBezier */
83 NULL, /* pPolyBezierTo */
85 PSDRV_PolyPolygon, /* pPolyPolygon */
86 PSDRV_PolyPolyline, /* pPolyPolyline */
87 PSDRV_Polygon, /* pPolygon */
88 PSDRV_Polyline, /* pPolyline */
89 NULL, /* pPolylineTo */
90 NULL, /* pRealizePalette */
91 PSDRV_Rectangle, /* pRectangle */
92 NULL, /* pRestoreDC */
93 PSDRV_RoundRect, /* pRoundRect */
95 NULL, /* pScaleViewportExt (optional) */
96 NULL, /* pScaleWindowExt (optional) */
97 NULL, /* pSelectClipPath */
98 NULL, /* pSelectClipRgn */
99 PSDRV_SelectObject, /* pSelectObject */
100 NULL, /* pSelectPalette */
101 PSDRV_SetBkColor, /* pSetBkColor */
102 NULL, /* pSetBkMode */
103 PSDRV_SetDeviceClipping, /* pSetDeviceClipping */
104 NULL, /* pSetDeviceGammaRamp */
105 NULL, /* pSetDIBitsToDevice */
106 NULL, /* pSetMapMode (optional) */
107 NULL, /* pSetMapperFlags */
108 PSDRV_SetPixel, /* pSetPixel */
109 NULL, /* pSetPixelFormat */
110 NULL, /* pSetPolyFillMode */
112 NULL, /* pSetRelAbs */
113 NULL, /* pSetStretchBltMode */
114 NULL, /* pSetTextAlign */
115 NULL, /* pSetTextCharacterExtra */
116 PSDRV_SetTextColor, /* pSetTextColor */
117 NULL, /* pSetTextJustification */
118 NULL, /* pSetViewportExt (optional) */
119 NULL, /* pSetViewportOrg (optional) */
120 NULL, /* pSetWindowExt (optional) */
121 NULL, /* pSetWindowOrg (optional) */
122 PSDRV_StartDoc, /* pStartDoc */
123 PSDRV_StartPage, /* pStartPage */
124 NULL, /* pStretchBlt */
125 PSDRV_StretchDIBits, /* pStretchDIBits */
126 NULL, /* pStrokeAndFillPath */
127 NULL, /* pStrokePath */
128 NULL, /* pSwapBuffers */
129 NULL /* pWidenPath */
133 /* Default entries for devcaps */
135 static DeviceCaps PSDRV_DevCaps = {
137 /* technology */ DT_RASPRINTER,
148 /* numColors */ 0xffff,
150 /* curveCaps */ CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES |
151 CC_WIDE | CC_STYLED | CC_WIDESTYLED | CC_INTERIORS |
153 /* lineCaps */ LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
154 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS,
155 /* polygoalnCaps */ PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON |
156 PC_SCANLINE | PC_WIDE | PC_STYLED | PC_WIDESTYLED |
158 /* textCaps */ TC_CR_ANY, /* psdrv 0x59f7 */
159 /* clipCaps */ CP_RECTANGLE,
160 /* rasterCaps */ RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT |
161 RC_DIBTODEV | RC_STRETCHBLT |
162 RC_STRETCHDIB, /* psdrv 0x6e99 */
167 /* logPixelsX */ 600,
168 /* logPixelsY */ 600,
170 /* palette size */ 0,
173 static PSDRV_DEVMODEA DefaultDevmode =
176 /* dmDeviceName */ "Wine PostScript Driver",
177 /* dmSpecVersion */ 0x30a,
178 /* dmDriverVersion */ 0x001,
179 /* dmSize */ sizeof(DEVMODEA),
180 /* dmDriverExtra */ 0,
181 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE |
182 DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR |
183 DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION,
186 /* dmOrientation */ DMORIENT_PORTRAIT,
187 /* dmPaperSize */ DMPAPER_A4,
188 /* dmPaperLength */ 2969,
189 /* dmPaperWidth */ 2101
192 /* dmScale */ 100, /* ?? */
194 /* dmDefaultSource */ DMBIN_AUTO,
195 /* dmPrintQuality */ 0,
196 /* dmColor */ DMCOLOR_COLOR,
198 /* dmYResolution */ 0,
199 /* dmTTOption */ DMTT_SUBDEV,
202 /* dmUnusedPadding */ 0,
203 /* dmBitsPerPel */ 0,
205 /* dmPelsHeight */ 0,
206 /* dmDisplayFlags */ 0,
207 /* dmDisplayFrequency */ 0,
211 /* dmDitherType */ 0,
214 /* dmPanningWidth */ 0,
215 /* dmPanningHeight */ 0
221 /* numInstalledOptions */ 0
225 HANDLE PSDRV_Heap = 0;
227 static HANDLE PSDRV_DefaultFont = 0;
228 static LOGFONTA DefaultLogFont = {
229 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
230 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
233 /*********************************************************************
236 * Initializes font metrics and registers driver. Called from GDI_Init()
239 BOOL WINAPI PSDRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
241 TRACE("(0x%4x, 0x%08lx, %p)\n", hinst, reason, reserved);
245 case DLL_PROCESS_ATTACH:
247 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
248 if (PSDRV_Heap == (HANDLE)NULL)
251 if (PSDRV_GetFontMetrics() == FALSE) {
252 HeapDestroy(PSDRV_Heap);
256 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
257 if (PSDRV_DefaultFont == (HANDLE)NULL) {
258 HeapDestroy(PSDRV_Heap);
262 /* Register driver as "WINEPS", "WINEPS.DLL" and "WINEPS.DRV"
263 to allow an easy configuring for users */
265 if (DRIVER_RegisterDriver("WINEPS", &PSDRV_Funcs) == FALSE) {
266 HeapDestroy(PSDRV_Heap);
270 if (DRIVER_RegisterDriver("WINEPS.DLL", &PSDRV_Funcs) == FALSE) {
271 DRIVER_UnregisterDriver("WINEPS");
272 HeapDestroy(PSDRV_Heap);
276 if (DRIVER_RegisterDriver("WINEPS.DRV", &PSDRV_Funcs) == FALSE) {
277 DRIVER_UnregisterDriver("WINEPS");
278 DRIVER_UnregisterDriver("WINEPS.DLL");
279 HeapDestroy(PSDRV_Heap);
285 case DLL_PROCESS_DETACH:
287 DeleteObject( PSDRV_DefaultFont );
288 HeapDestroy( PSDRV_Heap );
289 DRIVER_UnregisterDriver( "WINEPS" );
290 DRIVER_UnregisterDriver( "WINEPS.DLL" );
291 DRIVER_UnregisterDriver( "WINEPS.DRV" );
300 /**********************************************************************
303 static BOOL PSDRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
304 LPCSTR output, const DEVMODEA* initData )
306 PSDRV_PDEVICE *physDev;
310 INT width = 0, height = 0;
312 /* If no device name was specified, retrieve the device name
313 * from the DEVMODE structure from the DC's physDev.
314 * (See CreateCompatibleDC) */
315 if ( !device && dc->physDev )
317 physDev = (PSDRV_PDEVICE *)dc->physDev;
318 device = physDev->Devmode->dmPublic.dmDeviceName;
320 pi = PSDRV_FindPrinterInfo(device);
322 TRACE("(%s %s %s %p)\n", driver, device, output, initData);
324 if(!pi) return FALSE;
327 MESSAGE("To use WINEPS you need to install some AFM files.\n");
331 physDev = (PSDRV_PDEVICE *)HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
333 if (!physDev) return FALSE;
334 dc->physDev = physDev;
338 physDev->Devmode = (PSDRV_DEVMODEA *)HeapAlloc( PSDRV_Heap, 0,
339 sizeof(PSDRV_DEVMODEA) );
340 if(!physDev->Devmode) {
341 HeapFree( PSDRV_Heap, 0, physDev );
345 memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
348 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)initData, pi);
352 devCaps = HeapAlloc( PSDRV_Heap, 0, sizeof(PSDRV_DevCaps) );
353 memcpy(devCaps, &PSDRV_DevCaps, sizeof(PSDRV_DevCaps));
355 /* Are aspect[XY] and logPixels[XY] correct? */
356 /* Need to handle different res in x and y => fix ppd */
357 devCaps->aspectX = devCaps->logPixelsX =
358 physDev->pi->ppd->DefaultResolution;
359 devCaps->aspectY = devCaps->logPixelsY =
360 physDev->pi->ppd->DefaultResolution;
361 devCaps->aspectXY = (int)hypot( (double)devCaps->aspectX,
362 (double)devCaps->aspectY );
365 for(page = pi->ppd->PageSizes; page; page = page->next) {
366 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
370 FIXME("Can't find page\n");
371 physDev->PageSize.left = 0;
372 physDev->PageSize.right = 0;
373 physDev->PageSize.bottom = 0;
374 physDev->PageSize.top = 0;
375 } else if(page->ImageableArea) { /* PageSize is in device units */
376 physDev->PageSize.left = page->ImageableArea->llx *
377 devCaps->logPixelsX / 72;
378 physDev->PageSize.right = page->ImageableArea->urx *
379 devCaps->logPixelsX / 72;
380 physDev->PageSize.bottom = page->ImageableArea->lly *
381 devCaps->logPixelsY / 72;
382 physDev->PageSize.top = page->ImageableArea->ury *
383 devCaps->logPixelsY / 72;
385 physDev->PageSize.left = physDev->PageSize.bottom = 0;
386 physDev->PageSize.right = page->PaperDimension->x *
387 devCaps->logPixelsX / 72;
388 physDev->PageSize.top = page->PaperDimension->y *
389 devCaps->logPixelsY / 72;
391 TRACE("PageSize = (%d,%d - %d,%d)\n", physDev->PageSize.left, physDev->PageSize.bottom, physDev->PageSize.right, physDev->PageSize.top);
393 /* these are in device units */
394 width = physDev->PageSize.right - physDev->PageSize.left;
395 height = physDev->PageSize.top - physDev->PageSize.bottom;
397 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
398 devCaps->horzRes = width;
399 devCaps->vertRes = height;
401 devCaps->horzRes = height;
402 devCaps->vertRes = width;
405 /* these are in mm */
406 devCaps->horzSize = (devCaps->horzRes * 25.4) / devCaps->logPixelsX;
407 devCaps->vertSize = (devCaps->vertRes * 25.4) / devCaps->logPixelsY;
409 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
410 "horzRes = %d, vertRes = %d\n",
411 devCaps->horzSize, devCaps->vertSize,
412 devCaps->horzRes, devCaps->vertRes);
414 if(physDev->pi->ppd->ColorDevice) {
415 devCaps->bitsPixel = 8;
416 devCaps->numColors = 256;
417 /* FIXME are these values OK? */
422 dc->devCaps = devCaps;
424 dc->hVisRgn = CreateRectRgn(0, 0, dc->devCaps->horzRes,
425 dc->devCaps->vertRes);
427 dc->hFont = PSDRV_DefaultFont;
429 if (!output) output = "LPT1:"; /* HACK */
430 physDev->job.output = HeapAlloc( PSDRV_Heap, 0, strlen(output)+1 );
431 strcpy( physDev->job.output, output );
432 physDev->job.hJob = 0;
437 /**********************************************************************
440 static BOOL PSDRV_DeleteDC( DC *dc )
442 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
446 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
447 HeapFree( PSDRV_Heap, 0, physDev->job.output );
448 HeapFree( PSDRV_Heap, 0, (void *)dc->devCaps );
449 HeapFree( PSDRV_Heap, 0, physDev );
456 /**********************************************************************
457 * PSDRV_FindPrinterInfo
459 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
461 static PRINTERINFO *PSDRV_PrinterList;
462 DWORD type = REG_BINARY, needed, res, dwPaperSize;
463 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
467 const char *ppd = NULL;
468 char ppdFileName[256];
471 TRACE("'%s'\n", name);
474 * If this loop completes, last will point to the 'next' element of the
475 * final PRINTERINFO in the list
477 for( ; pi; last = &pi->next, pi = pi->next)
478 if(!strcmp(pi->FriendlyName, name))
481 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
485 if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
486 strcpy( pi->FriendlyName, name );
488 /* Use Get|SetPrinterDataExA instead? */
490 res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
493 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
494 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
495 if (pi->Devmode == NULL)
497 memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
498 strcpy(pi->Devmode->dmPublic.dmDeviceName,name);
499 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
500 REG_BINARY, (LPBYTE)&DefaultDevmode, sizeof(DefaultDevmode) );
502 /* need to do something here AddPrinter?? */
505 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
506 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
507 (LPBYTE)pi->Devmode, needed, &needed);
510 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
511 ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
519 ppd = cupsGetPPD(name);
522 strncpy(ppdFileName, ppd, sizeof(ppdFileName));
524 /* we should unlink() that file later */
526 res = ERROR_FILE_NOT_FOUND;
527 WARN("Did not find ppd for %s\n",name);
532 if (!ppdFileName[0]) {
533 res = GetPrinterDataA (hPrinter, "PPD File", NULL, ppdFileName,
534 sizeof(ppdFileName), &needed);
536 /* Look for a ppd file for this printer in the config file.
537 * First look for the names of the printer, then for 'generic'
539 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppd", &hkey))
541 DWORD count = sizeof(ppdFileName);
543 if(RegQueryValueExA(hkey, name, 0, &type, ppdFileName, &count) != ERROR_SUCCESS)
544 RegQueryValueExA(hkey, "generic", 0, &type, ppdFileName, &count);
549 res = ERROR_FILE_NOT_FOUND;
553 if (res != ERROR_SUCCESS) {
554 ERR ("Error %li getting PPD file name for printer '%s'\n", res, name);
558 ppdFileName[sizeof(ppdFileName) - 1] = '\0';
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);
637 if (pi->FontSubTable)
638 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
639 if (pi->FriendlyName)
640 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
642 HeapFree(PSDRV_Heap, 0, pi->Devmode);
644 HeapFree(PSDRV_Heap, 0, pi);
645 if (ppd) unlink(ppd);