Release 20021125.
[wine/multimedia.git] / dlls / wineps / init.c
blobfa6e9da300a74834e74c1b0e2886349817b9de8f
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
30 #include "gdi.h"
31 #include "psdrv.h"
32 #include "wine/debug.h"
33 #include "winreg.h"
34 #include "winspool.h"
35 #include "winerror.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
39 #ifdef HAVE_CUPS_CUPS_H
40 #include <cups/cups.h>
42 #ifndef CUPS_SONAME
43 #define CUPS_SONAME "libcups.so"
44 #endif
46 static void *cupshandle = NULL;
47 #endif
49 static PSDRV_DEVMODEA DefaultDevmode =
51 { /* dmPublic */
52 /* dmDeviceName */ "Wine PostScript Driver",
53 /* dmSpecVersion */ 0x30a,
54 /* dmDriverVersion */ 0x001,
55 /* dmSize */ sizeof(DEVMODEA),
56 /* dmDriverExtra */ sizeof(PSDRV_DEVMODEA)-sizeof(DEVMODEA),
57 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE |
58 DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR |
59 DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION,
60 { /* u1 */
61 { /* s1 */
62 /* dmOrientation */ DMORIENT_PORTRAIT,
63 /* dmPaperSize */ DMPAPER_LETTER,
64 /* dmPaperLength */ 2794,
65 /* dmPaperWidth */ 2159
68 /* dmScale */ 100, /* ?? */
69 /* dmCopies */ 1,
70 /* dmDefaultSource */ DMBIN_AUTO,
71 /* dmPrintQuality */ 0,
72 /* dmColor */ DMCOLOR_COLOR,
73 /* dmDuplex */ 0,
74 /* dmYResolution */ 0,
75 /* dmTTOption */ DMTT_SUBDEV,
76 /* dmCollate */ 0,
77 /* dmFormName */ "",
78 /* dmUnusedPadding */ 0,
79 /* dmBitsPerPel */ 0,
80 /* dmPelsWidth */ 0,
81 /* dmPelsHeight */ 0,
82 /* dmDisplayFlags */ 0,
83 /* dmDisplayFrequency */ 0,
84 /* dmICMMethod */ 0,
85 /* dmICMIntent */ 0,
86 /* dmMediaType */ 0,
87 /* dmDitherType */ 0,
88 /* dmReserved1 */ 0,
89 /* dmReserved2 */ 0,
90 /* dmPanningWidth */ 0,
91 /* dmPanningHeight */ 0
93 { /* dmDocPrivate */
94 /* dummy */ 0
96 { /* dmDrvPrivate */
97 /* numInstalledOptions */ 0
101 HANDLE PSDRV_Heap = 0;
103 static HANDLE PSDRV_DefaultFont = 0;
104 static LOGFONTA DefaultLogFont = {
105 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
106 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
109 /*********************************************************************
110 * DllMain
112 * Initializes font metrics and registers driver. wineps dll entry point.
115 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
117 TRACE("(%p, 0x%08lx, %p)\n", hinst, reason, reserved);
119 switch(reason) {
121 case DLL_PROCESS_ATTACH:
123 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
124 if (PSDRV_Heap == (HANDLE)NULL)
125 return FALSE;
127 if (PSDRV_GetFontMetrics() == FALSE) {
128 HeapDestroy(PSDRV_Heap);
129 return FALSE;
132 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
133 if (PSDRV_DefaultFont == (HANDLE)NULL) {
134 HeapDestroy(PSDRV_Heap);
135 return FALSE;
137 #ifdef HAVE_CUPS_CUPS_H
138 /* dynamically load CUPS if not yet loaded */
139 if (!cupshandle) {
140 cupshandle = wine_dlopen(CUPS_SONAME, RTLD_NOW, NULL, 0);
141 if (!cupshandle) cupshandle = (void*)-1;
143 #endif
144 break;
146 case DLL_PROCESS_DETACH:
148 DeleteObject( PSDRV_DefaultFont );
149 HeapDestroy( PSDRV_Heap );
150 #ifdef HAVE_CUPS_CUPS_H
151 if (cupshandle && (cupshandle != (void*)-1)) {
152 wine_dlclose(cupshandle, NULL, 0);
153 cupshandle = NULL;
155 #endif
156 break;
159 return TRUE;
163 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
165 PAGESIZE *page;
166 INT width = 0, height = 0;
168 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
169 for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
170 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
171 break;
174 if(!page) {
175 FIXME("Can't find page\n");
176 physDev->ImageableArea.left = 0;
177 physDev->ImageableArea.right = 0;
178 physDev->ImageableArea.bottom = 0;
179 physDev->ImageableArea.top = 0;
180 physDev->PageSize.cx = 0;
181 physDev->PageSize.cy = 0;
182 } else if(page->ImageableArea) {
183 /* physDev sizes in device units; ppd sizes in 1/72" */
184 physDev->ImageableArea.left = page->ImageableArea->llx *
185 physDev->logPixelsX / 72;
186 physDev->ImageableArea.right = page->ImageableArea->urx *
187 physDev->logPixelsX / 72;
188 physDev->ImageableArea.bottom = page->ImageableArea->lly *
189 physDev->logPixelsY / 72;
190 physDev->ImageableArea.top = page->ImageableArea->ury *
191 physDev->logPixelsY / 72;
192 physDev->PageSize.cx = page->PaperDimension->x *
193 physDev->logPixelsX / 72;
194 physDev->PageSize.cy = page->PaperDimension->y *
195 physDev->logPixelsY / 72;
196 } else {
197 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
198 physDev->ImageableArea.right = physDev->PageSize.cx =
199 page->PaperDimension->x * physDev->logPixelsX / 72;
200 physDev->ImageableArea.top = physDev->PageSize.cy =
201 page->PaperDimension->y * physDev->logPixelsY / 72;
203 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
204 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
205 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
206 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
207 physDev->ImageableArea.right = physDev->PageSize.cx =
208 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
209 physDev->logPixelsX / 254;
210 physDev->ImageableArea.top = physDev->PageSize.cy =
211 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
212 physDev->logPixelsY / 254;
213 } else {
214 FIXME("Odd dmFields %lx\n", physDev->Devmode->dmPublic.dmFields);
215 physDev->ImageableArea.left = 0;
216 physDev->ImageableArea.right = 0;
217 physDev->ImageableArea.bottom = 0;
218 physDev->ImageableArea.top = 0;
219 physDev->PageSize.cx = 0;
220 physDev->PageSize.cy = 0;
223 TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %ldx%ld\n",
224 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
225 physDev->ImageableArea.right, physDev->ImageableArea.top,
226 physDev->PageSize.cx, physDev->PageSize.cy);
228 /* these are in device units */
229 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
230 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
232 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
233 physDev->horzRes = width;
234 physDev->vertRes = height;
235 } else {
236 physDev->horzRes = height;
237 physDev->vertRes = width;
240 /* these are in mm */
241 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
242 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
244 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
245 "horzRes = %d, vertRes = %d\n",
246 physDev->horzSize, physDev->vertSize,
247 physDev->horzRes, physDev->vertRes);
251 /**********************************************************************
252 * PSDRV_CreateDC
254 BOOL PSDRV_CreateDC( DC *dc, PSDRV_PDEVICE **pdev, LPCSTR driver, LPCSTR device,
255 LPCSTR output, const DEVMODEA* initData )
257 PSDRV_PDEVICE *physDev;
258 PRINTERINFO *pi;
260 /* If no device name was specified, retrieve the device name
261 * from the DEVMODE structure from the DC's physDev.
262 * (See CreateCompatibleDC) */
263 if ( !device && *pdev )
265 physDev = *pdev;
266 device = physDev->Devmode->dmPublic.dmDeviceName;
268 pi = PSDRV_FindPrinterInfo(device);
270 TRACE("(%s %s %s %p)\n", driver, device, output, initData);
272 if(!pi) return FALSE;
274 if(!pi->Fonts) {
275 MESSAGE("To use WINEPS you need to install some AFM files.\n");
276 return FALSE;
279 physDev = (PSDRV_PDEVICE *)HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
280 sizeof(*physDev) );
281 if (!physDev) return FALSE;
282 *pdev = physDev;
283 physDev->hdc = dc->hSelf;
284 physDev->dc = dc;
286 physDev->pi = pi;
288 physDev->Devmode = (PSDRV_DEVMODEA *)HeapAlloc( PSDRV_Heap, 0,
289 sizeof(PSDRV_DEVMODEA) );
290 if(!physDev->Devmode) {
291 HeapFree( PSDRV_Heap, 0, physDev );
292 return FALSE;
295 memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
297 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
298 physDev->logPixelsY = physDev->pi->ppd->DefaultResolution;
300 if (!output) output = "LPT1:"; /* HACK */
301 physDev->job.output = HeapAlloc( PSDRV_Heap, 0, strlen(output)+1 );
302 strcpy( physDev->job.output, output );
303 physDev->job.hJob = 0;
305 if(initData) {
306 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)initData, pi);
309 PSDRV_UpdateDevCaps(physDev);
310 dc->hFont = PSDRV_DefaultFont;
311 return TRUE;
316 /**********************************************************************
317 * PSDRV_DeleteDC
319 BOOL PSDRV_DeleteDC( PSDRV_PDEVICE *physDev )
321 TRACE("\n");
323 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
324 HeapFree( PSDRV_Heap, 0, physDev->job.output );
325 HeapFree( PSDRV_Heap, 0, physDev );
327 return TRUE;
331 /**********************************************************************
332 * ResetDC (WINEPS.@)
334 HDC PSDRV_ResetDC( PSDRV_PDEVICE *physDev, const DEVMODEA *lpInitData )
336 if(lpInitData) {
337 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)lpInitData, physDev->pi);
338 PSDRV_UpdateDevCaps(physDev);
340 return physDev->hdc;
343 /***********************************************************************
344 * GetDeviceCaps (WINEPS.@)
346 INT PSDRV_GetDeviceCaps( PSDRV_PDEVICE *physDev, INT cap )
348 switch(cap)
350 case DRIVERVERSION:
351 return 0;
352 case TECHNOLOGY:
353 return DT_RASPRINTER;
354 case HORZSIZE:
355 return MulDiv(physDev->horzSize, 100,
356 physDev->Devmode->dmPublic.dmScale);
357 case VERTSIZE:
358 return MulDiv(physDev->vertSize, 100,
359 physDev->Devmode->dmPublic.dmScale);
360 case HORZRES:
361 return physDev->horzRes;
362 case VERTRES:
363 return physDev->vertRes;
364 case BITSPIXEL:
365 return (physDev->pi->ppd->ColorDevice ? 8 : 1);
366 case PLANES:
367 return 1;
368 case NUMBRUSHES:
369 return -1;
370 case NUMPENS:
371 return 10;
372 case NUMMARKERS:
373 return 0;
374 case NUMFONTS:
375 return 39;
376 case NUMCOLORS:
377 return (physDev->pi->ppd->ColorDevice ? 256 : -1);
378 case PDEVICESIZE:
379 return sizeof(PSDRV_PDEVICE);
380 case CURVECAPS:
381 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
382 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
383 case LINECAPS:
384 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
385 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
386 case POLYGONALCAPS:
387 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
388 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
389 case TEXTCAPS:
390 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
391 case CLIPCAPS:
392 return CP_RECTANGLE;
393 case RASTERCAPS:
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 */
398 case ASPECTX:
399 case ASPECTY:
400 return physDev->pi->ppd->DefaultResolution;
401 case ASPECTXY:
402 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
403 (double)physDev->pi->ppd->DefaultResolution );
404 case LOGPIXELSX:
405 return MulDiv(physDev->logPixelsX,
406 physDev->Devmode->dmPublic.dmScale, 100);
407 case LOGPIXELSY:
408 return MulDiv(physDev->logPixelsY,
409 physDev->Devmode->dmPublic.dmScale, 100);
410 case SIZEPALETTE:
411 return 0;
412 case NUMRESERVED:
413 return 0;
414 case COLORRES:
415 return 0;
416 case PHYSICALWIDTH:
417 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
418 physDev->PageSize.cy : physDev->PageSize.cx;
419 case PHYSICALHEIGHT:
420 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
421 physDev->PageSize.cx : physDev->PageSize.cy;
422 case PHYSICALOFFSETX:
423 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
424 if(physDev->pi->ppd->LandscapeOrientation == -90)
425 return physDev->PageSize.cy - physDev->ImageableArea.top;
426 else
427 return physDev->ImageableArea.bottom;
429 return physDev->ImageableArea.left;
431 case PHYSICALOFFSETY:
432 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
433 if(physDev->pi->ppd->LandscapeOrientation == -90)
434 return physDev->PageSize.cx - physDev->ImageableArea.right;
435 else
436 return physDev->ImageableArea.left;
438 return physDev->PageSize.cy - physDev->ImageableArea.top;
440 case SCALINGFACTORX:
441 case SCALINGFACTORY:
442 case VREFRESH:
443 case DESKTOPVERTRES:
444 case DESKTOPHORZRES:
445 case BTLALIGNMENT:
446 return 0;
447 default:
448 FIXME("(%p): unsupported capability %d, will return 0\n", physDev->hdc, cap );
449 return 0;
454 /**********************************************************************
455 * PSDRV_FindPrinterInfo
457 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
459 static PRINTERINFO *PSDRV_PrinterList;
460 DWORD type = REG_BINARY, needed, res, dwPaperSize;
461 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
462 FONTNAME *font;
463 const AFM *afm;
464 HANDLE hPrinter;
465 const char *ppd = NULL;
466 DWORD ppdType;
467 char* ppdFileName = NULL;
468 HKEY hkey;
469 BOOL using_default_devmode = FALSE;
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))
479 return pi;
481 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
482 if (pi == NULL)
483 return NULL;
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,
491 NULL, 0, &needed );
493 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
494 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
495 if (pi->Devmode == NULL)
496 goto cleanup;
497 memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
498 strcpy(pi->Devmode->dmPublic.dmDeviceName,name);
499 using_default_devmode = TRUE;
501 /* need to do something here AddPrinter?? */
503 else {
504 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
505 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
506 (LPBYTE)pi->Devmode, needed, &needed);
509 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
510 ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
511 goto cleanup;
514 #ifdef HAVE_CUPS_CUPS_H
515 if (cupshandle != (void*)-1) {
516 typeof(cupsGetPPD) * pcupsGetPPD = NULL;
518 pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
519 if (pcupsGetPPD) {
520 ppd = pcupsGetPPD(name);
522 if (ppd) {
523 needed=strlen(ppd)+1;
524 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
525 memcpy(ppdFileName, ppd, needed);
526 ppdType=REG_SZ;
527 res = ERROR_SUCCESS;
528 /* we should unlink() that file later */
529 } else {
530 res = ERROR_FILE_NOT_FOUND;
531 WARN("Did not find ppd for %s\n",name);
535 #endif
536 if (!ppdFileName) {
537 res = GetPrinterDataA(hPrinter, "PPD File", NULL, NULL, 0, &needed);
538 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
539 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
540 res = GetPrinterDataA(hPrinter, "PPD File", &ppdType, ppdFileName, needed, &needed);
543 /* Look for a ppd file for this printer in the config file.
544 * First look under that printer's name, and then under 'generic'
546 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppd", &hkey))
548 const char* value_name;
550 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
551 value_name=name;
552 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
553 value_name="generic";
554 } else {
555 value_name=NULL;
557 if (value_name) {
558 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
559 RegQueryValueExA(hkey, value_name, 0, &ppdType, ppdFileName, &needed);
561 RegCloseKey(hkey);
564 if (!ppdFileName) {
565 res = ERROR_FILE_NOT_FOUND;
566 ERR ("Error %li getting PPD file name for printer '%s'\n", res, name);
567 goto closeprinter;
568 } else {
569 res = ERROR_SUCCESS;
570 if (ppdType==REG_EXPAND_SZ) {
571 char* tmp;
573 /* Expand environment variable references */
574 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
575 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
576 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
577 HeapFree(PSDRV_Heap, 0, ppdFileName);
578 ppdFileName=tmp;
582 pi->ppd = PSDRV_ParsePPD(ppdFileName);
583 if(!pi->ppd) {
584 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
585 ppdFileName);
586 goto closeprinter;
590 if(using_default_devmode) {
591 DWORD papersize;
593 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
594 (LPWSTR)&papersize, sizeof(papersize))) {
595 PSDRV_DEVMODEA dm;
596 memset(&dm, 0, sizeof(dm));
597 dm.dmPublic.dmFields = DM_PAPERSIZE;
598 dm.dmPublic.u1.s1.dmPaperSize = papersize;
599 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
601 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
602 REG_BINARY, (LPBYTE)pi->Devmode, sizeof(DefaultDevmode) );
607 * This is a hack. The default paper size should be read in as part of
608 * the Devmode structure, but Wine doesn't currently provide a convenient
609 * way to configure printers.
611 res = GetPrinterDataA (hPrinter, "Paper Size", NULL, (LPBYTE) &dwPaperSize,
612 sizeof (DWORD), &needed);
613 if (res == ERROR_SUCCESS)
614 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
615 else if (res == ERROR_FILE_NOT_FOUND)
616 TRACE ("No 'Paper Size' for printer '%s'\n", name);
617 else {
618 ERR ("GetPrinterDataA returned %li\n", res);
619 goto closeprinter;
622 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
623 0, &needed, &pi->FontSubTableSize);
624 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
625 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
627 else if (res == ERROR_MORE_DATA) {
628 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
629 if (pi->FontSubTable == NULL) {
630 ERR ("Failed to allocate %li bytes from heap\n", needed);
631 goto closeprinter;
634 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
635 (LPBYTE) pi->FontSubTable, needed, &needed,
636 &pi->FontSubTableSize);
637 if (res != ERROR_SUCCESS) {
638 ERR ("EnumPrinterDataExA returned %li\n", res);
639 goto closeprinter;
642 else {
643 ERR("EnumPrinterDataExA returned %li\n", res);
644 goto closeprinter;
647 if (ClosePrinter (hPrinter) == 0) {
648 ERR ("ClosePrinter failed with code %li\n", GetLastError ());
649 goto cleanup;
652 pi->next = NULL;
653 pi->Fonts = NULL;
655 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
656 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
657 if(!afm) {
658 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
659 "ignoring\n", font->Name);
661 else {
662 BOOL added;
663 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
664 PSDRV_FreeAFMList(pi->Fonts);
665 goto cleanup;
670 if (ppd) unlink(ppd);
671 return pi;
673 closeprinter:
674 ClosePrinter(hPrinter);
675 cleanup:
676 if (ppdFileName)
677 HeapFree(PSDRV_Heap, 0, ppdFileName);
678 if (pi->FontSubTable)
679 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
680 if (pi->FriendlyName)
681 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
682 if (pi->Devmode)
683 HeapFree(PSDRV_Heap, 0, pi->Devmode);
684 fail:
685 HeapFree(PSDRV_Heap, 0, pi);
686 if (ppd) unlink(ppd);
687 *last = NULL;
688 return NULL;