2 * PostScript driver initialization functions
4 * Copyright 1998 Huw D M Davies
5 * Copyright 2001 Marcus Meissner
16 #include "debugtools.h"
24 # include <cups/cups.h>
27 DEFAULT_DEBUG_CHANNEL(psdrv);
29 static BOOL PSDRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
30 LPCSTR output, const DEVMODEA* initData );
31 static BOOL PSDRV_DeleteDC( DC *dc );
33 static const DC_FUNCTIONS PSDRV_Funcs =
36 NULL, /* pAbortPath */
40 NULL, /* pBeginPath */
42 NULL, /* pBitmapBits */
43 NULL, /* pChoosePixelFormat */
44 PSDRV_Chord, /* pChord */
45 NULL, /* pCloseFigure */
46 NULL, /* pCreateBitmap */
47 PSDRV_CreateDC, /* pCreateDC */
48 NULL, /* pCreateDIBSection */
49 NULL, /* pCreateDIBSection16 */
50 PSDRV_DeleteDC, /* pDeleteDC */
51 NULL, /* pDeleteObject */
52 NULL, /* pDescribePixelFormat */
53 PSDRV_DeviceCapabilities, /* pDeviceCapabilities */
54 PSDRV_Ellipse, /* pEllipse */
55 PSDRV_EndDoc, /* pEndDoc */
56 PSDRV_EndPage, /* pEndPage */
58 PSDRV_EnumDeviceFonts, /* pEnumDeviceFonts */
59 PSDRV_Escape, /* pEscape */
60 NULL, /* pExcludeClipRect */
61 PSDRV_ExtDeviceMode, /* pExtDeviceMode */
62 NULL, /* pExtFloodFill */
63 PSDRV_ExtTextOut, /* pExtTextOut */
66 NULL, /* pFlattenPath */
68 PSDRV_GetCharWidth, /* pGetCharWidth */
69 NULL, /* pGetDCOrgEx */
70 NULL, /* pGetDeviceGammaRamp */
72 NULL, /* pGetPixelFormat */
73 PSDRV_GetTextExtentPoint, /* pGetTextExtentPoint */
74 PSDRV_GetTextMetrics, /* pGetTextMetrics */
75 NULL, /* pIntersectClipRect */
76 NULL, /* pInvertRgn */
77 PSDRV_LineTo, /* pLineTo */
79 NULL, /* pOffsetClipRgn */
80 NULL, /* pOffsetViewportOrg (optional) */
81 NULL, /* pOffsetWindowOrg (optional) */
83 PSDRV_PatBlt, /* pPatBlt */
85 NULL, /* pPolyBezier */
86 NULL, /* pPolyBezierTo */
88 PSDRV_PolyPolygon, /* pPolyPolygon */
89 PSDRV_PolyPolyline, /* pPolyPolyline */
90 PSDRV_Polygon, /* pPolygon */
91 PSDRV_Polyline, /* pPolyline */
92 NULL, /* pPolylineTo */
93 NULL, /* pRealizePalette */
94 PSDRV_Rectangle, /* pRectangle */
95 NULL, /* pRestoreDC */
96 PSDRV_RoundRect, /* pRoundRect */
98 NULL, /* pScaleViewportExt (optional) */
99 NULL, /* pScaleWindowExt (optional) */
100 NULL, /* pSelectClipPath */
101 NULL, /* pSelectClipRgn */
102 PSDRV_SelectObject, /* pSelectObject */
103 NULL, /* pSelectPalette */
104 PSDRV_SetBkColor, /* pSetBkColor */
105 NULL, /* pSetBkMode */
106 PSDRV_SetDeviceClipping, /* pSetDeviceClipping */
107 NULL, /* pSetDeviceGammaRamp */
108 NULL, /* pSetDIBitsToDevice */
109 NULL, /* pSetMapMode (optional) */
110 NULL, /* pSetMapperFlags */
111 PSDRV_SetPixel, /* pSetPixel */
112 NULL, /* pSetPixelFormat */
113 NULL, /* pSetPolyFillMode */
115 NULL, /* pSetRelAbs */
116 NULL, /* pSetStretchBltMode */
117 NULL, /* pSetTextAlign */
118 NULL, /* pSetTextCharacterExtra */
119 PSDRV_SetTextColor, /* pSetTextColor */
120 NULL, /* pSetTextJustification */
121 NULL, /* pSetViewportExt (optional) */
122 NULL, /* pSetViewportOrg (optional) */
123 NULL, /* pSetWindowExt (optional) */
124 NULL, /* pSetWindowOrg (optional) */
125 PSDRV_StartDoc, /* pStartDoc */
126 PSDRV_StartPage, /* pStartPage */
127 NULL, /* pStretchBlt */
128 PSDRV_StretchDIBits, /* pStretchDIBits */
129 NULL, /* pStrokeAndFillPath */
130 NULL, /* pStrokePath */
131 NULL, /* pSwapBuffers */
132 NULL /* pWidenPath */
136 /* Default entries for devcaps */
138 static DeviceCaps PSDRV_DevCaps = {
140 /* technology */ DT_RASPRINTER,
151 /* numColors */ 0xffff,
153 /* curveCaps */ CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES |
154 CC_WIDE | CC_STYLED | CC_WIDESTYLED | CC_INTERIORS |
156 /* lineCaps */ LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
157 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS,
158 /* polygoalnCaps */ PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON |
159 PC_SCANLINE | PC_WIDE | PC_STYLED | PC_WIDESTYLED |
161 /* textCaps */ TC_CR_ANY, /* psdrv 0x59f7 */
162 /* clipCaps */ CP_RECTANGLE,
163 /* rasterCaps */ RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT |
164 RC_DIBTODEV | RC_STRETCHBLT |
165 RC_STRETCHDIB, /* psdrv 0x6e99 */
170 /* logPixelsX */ 600,
171 /* logPixelsY */ 600,
173 /* palette size */ 0,
176 static PSDRV_DEVMODEA DefaultDevmode =
179 /* dmDeviceName */ "Wine PostScript Driver",
180 /* dmSpecVersion */ 0x30a,
181 /* dmDriverVersion */ 0x001,
182 /* dmSize */ sizeof(DEVMODEA),
183 /* dmDriverExtra */ 0,
184 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE |
185 DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR |
186 DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION,
189 /* dmOrientation */ DMORIENT_PORTRAIT,
190 /* dmPaperSize */ DMPAPER_A4,
191 /* dmPaperLength */ 2969,
192 /* dmPaperWidth */ 2101
195 /* dmScale */ 100, /* ?? */
197 /* dmDefaultSource */ DMBIN_AUTO,
198 /* dmPrintQuality */ 0,
199 /* dmColor */ DMCOLOR_COLOR,
201 /* dmYResolution */ 0,
202 /* dmTTOption */ DMTT_SUBDEV,
205 /* dmUnusedPadding */ 0,
206 /* dmBitsPerPel */ 0,
208 /* dmPelsHeight */ 0,
209 /* dmDisplayFlags */ 0,
210 /* dmDisplayFrequency */ 0,
214 /* dmDitherType */ 0,
217 /* dmPanningWidth */ 0,
218 /* dmPanningHeight */ 0
224 /* numInstalledOptions */ 0
228 HANDLE PSDRV_Heap = 0;
230 static HANDLE PSDRV_DefaultFont = 0;
231 static LOGFONTA DefaultLogFont = {
232 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
233 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
236 /*********************************************************************
239 * Initializes font metrics and registers driver. Called from GDI_Init()
242 BOOL WINAPI PSDRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
244 TRACE("(0x%4x, 0x%08lx, %p)\n", hinst, reason, reserved);
248 case DLL_PROCESS_ATTACH:
250 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
251 if (PSDRV_Heap == (HANDLE)NULL)
254 if (PSDRV_GetFontMetrics() == FALSE) {
255 HeapDestroy(PSDRV_Heap);
259 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
260 if (PSDRV_DefaultFont == (HANDLE)NULL) {
261 HeapDestroy(PSDRV_Heap);
265 /* Register driver as "WINEPS", "WINEPS.DLL" and "WINEPS.DRV"
266 to allow an easy configuring for users */
268 if (DRIVER_RegisterDriver("WINEPS", &PSDRV_Funcs) == FALSE) {
269 HeapDestroy(PSDRV_Heap);
273 if (DRIVER_RegisterDriver("WINEPS.DLL", &PSDRV_Funcs) == FALSE) {
274 DRIVER_UnregisterDriver("WINEPS");
275 HeapDestroy(PSDRV_Heap);
279 if (DRIVER_RegisterDriver("WINEPS.DRV", &PSDRV_Funcs) == FALSE) {
280 DRIVER_UnregisterDriver("WINEPS");
281 DRIVER_UnregisterDriver("WINEPS.DLL");
282 HeapDestroy(PSDRV_Heap);
288 case DLL_PROCESS_DETACH:
290 DeleteObject( PSDRV_DefaultFont );
291 HeapDestroy( PSDRV_Heap );
292 DRIVER_UnregisterDriver( "WINEPS" );
293 DRIVER_UnregisterDriver( "WINEPS.DLL" );
294 DRIVER_UnregisterDriver( "WINEPS.DRV" );
303 /**********************************************************************
306 static BOOL PSDRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
307 LPCSTR output, const DEVMODEA* initData )
309 PSDRV_PDEVICE *physDev;
313 INT width = 0, height = 0;
315 /* If no device name was specified, retrieve the device name
316 * from the DEVMODE structure from the DC's physDev.
317 * (See CreateCompatibleDC) */
318 if ( !device && dc->physDev )
320 physDev = (PSDRV_PDEVICE *)dc->physDev;
321 device = physDev->Devmode->dmPublic.dmDeviceName;
323 pi = PSDRV_FindPrinterInfo(device);
325 TRACE("(%s %s %s %p)\n", driver, device, output, initData);
327 if(!pi) return FALSE;
330 MESSAGE("To use WINEPS you need to install some AFM files.\n");
334 physDev = (PSDRV_PDEVICE *)HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
336 if (!physDev) return FALSE;
337 dc->physDev = physDev;
341 physDev->Devmode = (PSDRV_DEVMODEA *)HeapAlloc( PSDRV_Heap, 0,
342 sizeof(PSDRV_DEVMODEA) );
343 if(!physDev->Devmode) {
344 HeapFree( PSDRV_Heap, 0, physDev );
348 memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
351 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)initData, pi);
355 devCaps = HeapAlloc( PSDRV_Heap, 0, sizeof(PSDRV_DevCaps) );
356 memcpy(devCaps, &PSDRV_DevCaps, sizeof(PSDRV_DevCaps));
358 /* Are aspect[XY] and logPixels[XY] correct? */
359 /* Need to handle different res in x and y => fix ppd */
360 devCaps->aspectX = devCaps->logPixelsX =
361 physDev->pi->ppd->DefaultResolution;
362 devCaps->aspectY = devCaps->logPixelsY =
363 physDev->pi->ppd->DefaultResolution;
364 devCaps->aspectXY = (int)hypot( (double)devCaps->aspectX,
365 (double)devCaps->aspectY );
368 for(page = pi->ppd->PageSizes; page; page = page->next) {
369 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
373 FIXME("Can't find page\n");
374 physDev->PageSize.left = 0;
375 physDev->PageSize.right = 0;
376 physDev->PageSize.bottom = 0;
377 physDev->PageSize.top = 0;
378 } else if(page->ImageableArea) { /* PageSize is in device units */
379 physDev->PageSize.left = page->ImageableArea->llx *
380 devCaps->logPixelsX / 72;
381 physDev->PageSize.right = page->ImageableArea->urx *
382 devCaps->logPixelsX / 72;
383 physDev->PageSize.bottom = page->ImageableArea->lly *
384 devCaps->logPixelsY / 72;
385 physDev->PageSize.top = page->ImageableArea->ury *
386 devCaps->logPixelsY / 72;
388 physDev->PageSize.left = physDev->PageSize.bottom = 0;
389 physDev->PageSize.right = page->PaperDimension->x *
390 devCaps->logPixelsX / 72;
391 physDev->PageSize.top = page->PaperDimension->y *
392 devCaps->logPixelsY / 72;
394 TRACE("PageSize = (%d,%d - %d,%d)\n", physDev->PageSize.left, physDev->PageSize.bottom, physDev->PageSize.right, physDev->PageSize.top);
396 /* these are in device units */
397 width = physDev->PageSize.right - physDev->PageSize.left;
398 height = physDev->PageSize.top - physDev->PageSize.bottom;
400 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
401 devCaps->horzRes = width;
402 devCaps->vertRes = height;
404 devCaps->horzRes = height;
405 devCaps->vertRes = width;
408 /* these are in mm */
409 devCaps->horzSize = (devCaps->horzRes * 25.4) / devCaps->logPixelsX;
410 devCaps->vertSize = (devCaps->vertRes * 25.4) / devCaps->logPixelsY;
412 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
413 "horzRes = %d, vertRes = %d\n",
414 devCaps->horzSize, devCaps->vertSize,
415 devCaps->horzRes, devCaps->vertRes);
417 if(physDev->pi->ppd->ColorDevice) {
418 devCaps->bitsPixel = 8;
419 devCaps->numColors = 256;
420 /* FIXME are these values OK? */
425 dc->devCaps = devCaps;
427 dc->hVisRgn = CreateRectRgn(0, 0, dc->devCaps->horzRes,
428 dc->devCaps->vertRes);
430 dc->hFont = PSDRV_DefaultFont;
431 physDev->job.output = output ?
432 HEAP_strdupA( PSDRV_Heap, 0, output ) :
433 HEAP_strdupA( PSDRV_Heap, 0, "LPT1:" ); /* HACK */
434 physDev->job.hJob = 0;
439 /**********************************************************************
442 static BOOL PSDRV_DeleteDC( DC *dc )
444 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
448 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
449 HeapFree( PSDRV_Heap, 0, physDev->job.output );
450 HeapFree( PSDRV_Heap, 0, (void *)dc->devCaps );
451 HeapFree( PSDRV_Heap, 0, physDev );
458 /**********************************************************************
459 * PSDRV_FindPrinterInfo
461 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
463 static PRINTERINFO *PSDRV_PrinterList;
464 DWORD type = REG_BINARY, needed, res, dwPaperSize;
465 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
469 const char *ppd = NULL;
470 char ppdFileName[256];
472 TRACE("'%s'\n", name);
475 * If this loop completes, last will point to the 'next' element of the
476 * final PRINTERINFO in the list
478 for( ; pi; last = &pi->next, pi = pi->next)
479 if(!strcmp(pi->FriendlyName, name))
482 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
486 pi->FriendlyName = HEAP_strdupA( PSDRV_Heap, 0, name );
487 if (pi->FriendlyName == NULL)
490 /* Use Get|SetPrinterDataExA instead? */
492 res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
495 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
496 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
497 if (pi->Devmode == NULL)
499 memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
500 strcpy(pi->Devmode->dmPublic.dmDeviceName,name);
501 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
502 REG_BINARY, (LPBYTE)&DefaultDevmode, sizeof(DefaultDevmode) );
504 /* need to do something here AddPrinter?? */
507 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
508 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
509 (LPBYTE)pi->Devmode, needed, &needed);
512 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
513 ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
521 ppd = cupsGetPPD(name);
524 strncpy(ppdFileName, ppd, sizeof(ppdFileName));
526 /* we should unlink() that file later */
528 res = ERROR_FILE_NOT_FOUND;
529 WARN("Did not find ppd for %s\n",name);
534 if (!ppdFileName[0]) {
535 res = GetPrinterDataA (hPrinter, "PPD File", NULL, ppdFileName,
536 sizeof(ppdFileName), &needed);
538 /* Look for a ppd file for this printer in the config file.
539 * First look for the names of the printer, then for 'generic'
541 if ((res!=ERROR_SUCCESS) &&
542 !PROFILE_GetWineIniString("ppd",name,"",ppdFileName,sizeof(ppdFileName)) &&
543 !PROFILE_GetWineIniString("ppd","generic","",ppdFileName,sizeof(ppdFileName))
545 res = ERROR_FILE_NOT_FOUND;
549 if (res != ERROR_SUCCESS) {
550 ERR ("Error %li getting PPD file name for printer '%s'\n", res, name);
554 ppdFileName[sizeof(ppdFileName) - 1] = '\0';
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);
619 if (PSDRV_AddAFMtoList(&pi->Fonts, afm) == FALSE) {
620 PSDRV_FreeAFMList(pi->Fonts);
626 if (ppd) unlink(ppd);
630 ClosePrinter(hPrinter);
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);