Rewrite TrueType font metric parsing code.
[wine] / dlls / wineps / init.c
1 /*
2  *      PostScript driver initialization functions
3  *
4  *      Copyright 1998 Huw D M Davies
5  *      Copyright 2001 Marcus Meissner
6  *
7  */
8
9 #include "config.h"
10
11 #include <string.h>
12 #include <unistd.h>
13
14 #include "gdi.h"
15 #include "psdrv.h"
16 #include "debugtools.h"
17 #include "winreg.h"
18 #include "winspool.h"
19 #include "winerror.h"
20
21 #ifdef HAVE_CUPS
22 # include <cups/cups.h>
23 #endif
24
25 DEFAULT_DEBUG_CHANNEL(psdrv);
26
27 static BOOL PSDRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
28                                LPCSTR output, const DEVMODEA* initData );
29 static BOOL PSDRV_DeleteDC( DC *dc );
30
31 static const DC_FUNCTIONS PSDRV_Funcs =
32 {
33     NULL,                            /* pAbortDoc */
34     NULL,                            /* pAbortPath */
35     NULL,                            /* pAngleArc */
36     PSDRV_Arc,                       /* pArc */
37     NULL,                            /* pArcTo */
38     NULL,                            /* pBeginPath */
39     NULL,                            /* pBitBlt */
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 */
54     NULL,                            /* pEndPath */
55     PSDRV_EnumDeviceFonts,           /* pEnumDeviceFonts */
56     PSDRV_Escape,                    /* pEscape */
57     NULL,                            /* pExcludeClipRect */
58     PSDRV_ExtDeviceMode,             /* pExtDeviceMode */
59     NULL,                            /* pExtFloodFill */
60     PSDRV_ExtTextOut,                /* pExtTextOut */
61     NULL,                            /* pFillPath */
62     NULL,                            /* pFillRgn */
63     NULL,                            /* pFlattenPath */
64     NULL,                            /* pFrameRgn */
65     PSDRV_GetCharWidth,              /* pGetCharWidth */
66     NULL,                            /* pGetDCOrgEx */
67     NULL,                            /* pGetDeviceGammaRamp */
68     NULL,                            /* pGetPixel */
69     NULL,                            /* pGetPixelFormat */
70     PSDRV_GetTextExtentPoint,        /* pGetTextExtentPoint */
71     PSDRV_GetTextMetrics,            /* pGetTextMetrics */
72     NULL,                            /* pIntersectClipRect */
73     NULL,                            /* pInvertRgn */
74     PSDRV_LineTo,                    /* pLineTo */
75     NULL,                            /* pMoveTo */
76     NULL,                            /* pOffsetClipRgn */
77     NULL,                            /* pOffsetViewportOrg (optional) */
78     NULL,                            /* pOffsetWindowOrg (optional) */
79     NULL,                            /* pPaintRgn */
80     PSDRV_PatBlt,                    /* pPatBlt */
81     PSDRV_Pie,                       /* pPie */
82     NULL,                            /* pPolyBezier */
83     NULL,                            /* pPolyBezierTo */
84     NULL,                            /* pPolyDraw */
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 */
94     NULL,                            /* pSaveDC */
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 */
111     NULL,                            /* pSetROP2 */
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 */
130 };
131
132
133 /* Default entries for devcaps */
134
135 static DeviceCaps PSDRV_DevCaps = {
136 /* version */           0, 
137 /* technology */        DT_RASPRINTER,
138 /* horzSize */          210,
139 /* vertSize */          297,
140 /* horzRes */           4961,
141 /* vertRes */           7016, 
142 /* bitsPixel */         1,
143 /* planes */            1,
144 /* numBrushes */        -1,
145 /* numPens */           10,
146 /* numMarkers */        0,
147 /* numFonts */          39,
148 /* numColors */         0xffff,
149 /* pdeviceSize */       0,      
150 /* curveCaps */         CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES |
151                         CC_WIDE | CC_STYLED | CC_WIDESTYLED | CC_INTERIORS |
152                         CC_ROUNDRECT,
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 |
157                         PC_INTERIORS,
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 */
163 /* aspectX */           600,
164 /* aspectY */           600,
165 /* aspectXY */          848,
166 /* pad1 */              { 0 },
167 /* logPixelsX */        600,
168 /* logPixelsY */        600, 
169 /* pad2 */              { 0 },
170 /* palette size */      0,
171 /* ..etc */             0, 0 };
172
173 static PSDRV_DEVMODEA DefaultDevmode = 
174 {
175   { /* dmPublic */
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,
184    { /* u1 */
185      { /* s1 */
186 /* dmOrientation */     DMORIENT_PORTRAIT,
187 /* dmPaperSize */       DMPAPER_A4,
188 /* dmPaperLength */     2969,
189 /* dmPaperWidth */      2101
190      }
191    },
192 /* dmScale */           100, /* ?? */
193 /* dmCopies */          1,
194 /* dmDefaultSource */   DMBIN_AUTO,
195 /* dmPrintQuality */    0,
196 /* dmColor */           DMCOLOR_COLOR,
197 /* dmDuplex */          0,
198 /* dmYResolution */     0,
199 /* dmTTOption */        DMTT_SUBDEV,
200 /* dmCollate */         0,
201 /* dmFormName */        "",
202 /* dmUnusedPadding */   0,
203 /* dmBitsPerPel */      0,
204 /* dmPelsWidth */       0,
205 /* dmPelsHeight */      0,
206 /* dmDisplayFlags */    0,
207 /* dmDisplayFrequency */ 0,
208 /* dmICMMethod */       0,
209 /* dmICMIntent */       0,
210 /* dmMediaType */       0,
211 /* dmDitherType */      0,
212 /* dmReserved1 */       0,
213 /* dmReserved2 */       0,
214 /* dmPanningWidth */    0,
215 /* dmPanningHeight */   0
216   },
217   { /* dmDocPrivate */
218     /* dummy */ 0
219   },
220   { /* dmDrvPrivate */
221     /* numInstalledOptions */ 0 
222   }
223 };
224
225 HANDLE PSDRV_Heap = 0;
226
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, ""
231 };
232
233 /*********************************************************************
234  *           PSDRV_Init
235  *
236  * Initializes font metrics and registers driver. Called from GDI_Init()
237  *
238  */
239 BOOL WINAPI PSDRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
240 {
241     TRACE("(0x%4x, 0x%08lx, %p)\n", hinst, reason, reserved);
242    
243     switch(reason) {
244
245         case DLL_PROCESS_ATTACH:
246
247             PSDRV_Heap = HeapCreate(0, 0x10000, 0);
248             if (PSDRV_Heap == (HANDLE)NULL)
249                 return FALSE;
250
251             if (PSDRV_GetFontMetrics() == FALSE) {
252                 HeapDestroy(PSDRV_Heap);
253                 return FALSE;
254             }
255
256             PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
257             if (PSDRV_DefaultFont == (HANDLE)NULL) {
258                 HeapDestroy(PSDRV_Heap);
259                 return FALSE;
260             }
261
262             /* Register driver as "WINEPS", "WINEPS.DLL" and "WINEPS.DRV"
263                 to allow an easy configuring for users */
264
265             if (DRIVER_RegisterDriver("WINEPS", &PSDRV_Funcs) == FALSE) {
266                 HeapDestroy(PSDRV_Heap);
267                 return FALSE;
268             }
269
270             if (DRIVER_RegisterDriver("WINEPS.DLL", &PSDRV_Funcs) == FALSE) {
271                 DRIVER_UnregisterDriver("WINEPS");
272                 HeapDestroy(PSDRV_Heap);
273                 return FALSE;
274             }
275
276             if (DRIVER_RegisterDriver("WINEPS.DRV", &PSDRV_Funcs) == FALSE) {
277                 DRIVER_UnregisterDriver("WINEPS");
278                 DRIVER_UnregisterDriver("WINEPS.DLL");
279                 HeapDestroy(PSDRV_Heap);
280                 return FALSE;
281             }
282
283             break;
284
285         case DLL_PROCESS_DETACH:
286
287             DeleteObject( PSDRV_DefaultFont );
288             HeapDestroy( PSDRV_Heap );
289             DRIVER_UnregisterDriver( "WINEPS" );
290             DRIVER_UnregisterDriver( "WINEPS.DLL" );
291             DRIVER_UnregisterDriver( "WINEPS.DRV" );
292
293             break;
294     }
295  
296     return TRUE;
297 }
298
299
300 /**********************************************************************
301  *           PSDRV_CreateDC
302  */
303 static BOOL PSDRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
304                                LPCSTR output, const DEVMODEA* initData )
305 {
306     PSDRV_PDEVICE *physDev;
307     PRINTERINFO *pi;
308     DeviceCaps *devCaps;
309     PAGESIZE *page;
310     INT width = 0, height = 0;
311
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 )
316     {
317         physDev = (PSDRV_PDEVICE *)dc->physDev;
318         device = physDev->Devmode->dmPublic.dmDeviceName;
319     }
320     pi = PSDRV_FindPrinterInfo(device);
321         
322     TRACE("(%s %s %s %p)\n", driver, device, output, initData);
323
324     if(!pi) return FALSE;
325
326     if(!pi->Fonts) {
327         MESSAGE("To use WINEPS you need to install some AFM files.\n");
328         return FALSE;
329     }
330
331     physDev = (PSDRV_PDEVICE *)HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
332                                                      sizeof(*physDev) );
333     if (!physDev) return FALSE;
334     dc->physDev = physDev;
335
336     physDev->pi = pi;
337
338     physDev->Devmode = (PSDRV_DEVMODEA *)HeapAlloc( PSDRV_Heap, 0,
339                                                      sizeof(PSDRV_DEVMODEA) );
340     if(!physDev->Devmode) {
341         HeapFree( PSDRV_Heap, 0, physDev );
342         return FALSE;
343     }
344     
345     memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
346
347     if(initData) {
348         PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)initData, pi);
349     }
350
351     
352     devCaps = HeapAlloc( PSDRV_Heap, 0, sizeof(PSDRV_DevCaps) );
353     memcpy(devCaps, &PSDRV_DevCaps, sizeof(PSDRV_DevCaps));
354
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 );
363
364
365     for(page = pi->ppd->PageSizes; page; page = page->next) {
366         if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
367             break;
368     }
369     if(!page) {
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;
384     } else {
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;
390     }
391     TRACE("PageSize = (%d,%d - %d,%d)\n", physDev->PageSize.left, physDev->PageSize.bottom, physDev->PageSize.right, physDev->PageSize.top);
392
393     /* these are in device units */
394     width = physDev->PageSize.right - physDev->PageSize.left;
395     height = physDev->PageSize.top - physDev->PageSize.bottom;
396
397     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
398         devCaps->horzRes = width;
399         devCaps->vertRes = height;
400     } else {
401         devCaps->horzRes = height;
402         devCaps->vertRes = width;
403     }
404
405     /* these are in mm */
406     devCaps->horzSize = (devCaps->horzRes * 25.4) / devCaps->logPixelsX;
407     devCaps->vertSize = (devCaps->vertRes * 25.4) / devCaps->logPixelsY;
408
409     TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
410           "horzRes = %d, vertRes = %d\n",
411           devCaps->horzSize, devCaps->vertSize,
412           devCaps->horzRes, devCaps->vertRes);
413
414     if(physDev->pi->ppd->ColorDevice) {
415         devCaps->bitsPixel = 8;
416         devCaps->numColors = 256;
417         /* FIXME are these values OK? */
418     }
419
420     /* etc */
421
422     dc->devCaps = devCaps;
423
424     dc->hVisRgn = CreateRectRgn(0, 0, dc->devCaps->horzRes,
425                             dc->devCaps->vertRes);
426     
427     dc->hFont = PSDRV_DefaultFont;
428
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;
433     return TRUE;
434 }
435
436
437 /**********************************************************************
438  *           PSDRV_DeleteDC
439  */
440 static BOOL PSDRV_DeleteDC( DC *dc )
441 {
442     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
443     
444     TRACE("\n");
445
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 );
450     dc->physDev = NULL;
451
452     return TRUE;
453 }
454
455
456 /**********************************************************************
457  *              PSDRV_FindPrinterInfo
458  */
459 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name) 
460 {
461     static PRINTERINFO *PSDRV_PrinterList;
462     DWORD type = REG_BINARY, needed, res, dwPaperSize;
463     PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
464     FONTNAME *font;
465     const AFM *afm;
466     HANDLE hPrinter;
467     const char *ppd = NULL;
468     char ppdFileName[256];
469     HKEY hkey;
470
471     TRACE("'%s'\n", name);
472     
473     /*
474      *  If this loop completes, last will point to the 'next' element of the
475      *  final PRINTERINFO in the list
476      */    
477     for( ; pi; last = &pi->next, pi = pi->next)
478         if(!strcmp(pi->FriendlyName, name))
479             return pi;
480
481     pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
482     if (pi == NULL)
483         return NULL;
484
485     if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
486     strcpy( pi->FriendlyName, name );
487
488     /* Use Get|SetPrinterDataExA instead? */
489     
490     res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
491                             NULL, 0, &needed );
492
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         DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
500                  REG_BINARY, (LPBYTE)&DefaultDevmode, sizeof(DefaultDevmode) );
501
502         /* need to do something here AddPrinter?? */
503     }
504     else {
505         pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
506         DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
507                           (LPBYTE)pi->Devmode, needed, &needed);
508     }
509
510     if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
511         ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
512         goto cleanup;
513     }
514     
515     ppdFileName[0]='\0';
516     
517 #ifdef HAVE_CUPS
518     {
519         ppd = cupsGetPPD(name);
520
521         if (ppd) {
522             strncpy(ppdFileName, ppd, sizeof(ppdFileName));
523             res = ERROR_SUCCESS;
524             /* we should unlink() that file later */
525         } else {
526             res = ERROR_FILE_NOT_FOUND;
527             WARN("Did not find ppd for %s\n",name);
528         }
529     }
530 #endif
531
532     if (!ppdFileName[0]) {
533         res = GetPrinterDataA (hPrinter, "PPD File", NULL, ppdFileName,
534                 sizeof(ppdFileName), &needed);
535     }
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'
538      */
539     if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppd", &hkey))
540     {
541         DWORD count = sizeof(ppdFileName);
542         ppdFileName[0] = 0;
543         if(RegQueryValueExA(hkey, name, 0, &type, ppdFileName, &count) != ERROR_SUCCESS)
544             RegQueryValueExA(hkey, "generic", 0, &type, ppdFileName, &count);
545         RegCloseKey(hkey);
546     }
547
548     if(!ppdFileName[0])
549         res = ERROR_FILE_NOT_FOUND;
550     else 
551         res = ERROR_SUCCESS;
552
553     if (res != ERROR_SUCCESS) {
554         ERR ("Error %li getting PPD file name for printer '%s'\n", res, name);
555         goto closeprinter;
556     }
557     
558     ppdFileName[sizeof(ppdFileName) - 1] = '\0';
559     
560     pi->ppd = PSDRV_ParsePPD(ppdFileName);
561     if(!pi->ppd) {
562         MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
563             ppdFileName);
564         goto closeprinter;
565     }
566     
567     /*
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.
571      */
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);
578     else {
579         ERR ("GetPrinterDataA returned %li\n", res);
580         goto closeprinter;
581     }
582
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);
587     }
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);
592             goto closeprinter;
593         }
594
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);
600             goto closeprinter;
601         }
602     }
603     else {
604         ERR("EnumPrinterDataExA returned %li\n", res);
605         goto closeprinter;
606     }
607
608     if (ClosePrinter (hPrinter) == 0) {
609         ERR ("ClosePrinter failed with code %li\n", GetLastError ());
610         goto cleanup;
611     }
612
613     pi->next = NULL;
614     pi->Fonts = NULL;
615
616     for(font = pi->ppd->InstalledFonts; font; font = font->next) {
617         afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
618         if(!afm) {
619             TRACE( "Couldn't find AFM file for installed printer font '%s' - "
620                     "ignoring\n", font->Name);
621         }
622         else {
623             BOOL added;
624             if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
625                 PSDRV_FreeAFMList(pi->Fonts);
626                 goto cleanup;
627             }
628         }
629
630     }
631     if (ppd) unlink(ppd);
632     return pi;
633
634 closeprinter:
635     ClosePrinter(hPrinter);
636 cleanup:
637     if (pi->FontSubTable)
638         HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
639     if (pi->FriendlyName)
640         HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
641     if (pi->Devmode)
642         HeapFree(PSDRV_Heap, 0, pi->Devmode);
643 fail:
644     HeapFree(PSDRV_Heap, 0, pi);
645     if (ppd) unlink(ppd);
646     *last = NULL;
647     return NULL;
648 }