Added support for OpenGL.
[wine] / graphics / psdrv / init.c
1 /*
2  *      PostScript driver initialization functions
3  *
4  *      Copyright 1998 Huw D M Davies
5  *
6  */
7 #include <string.h>
8
9 #include "gdi.h"
10 #include "psdrv.h"
11 #include "debugtools.h"
12 #include "heap.h"
13 #include "winreg.h"
14 #include "winspool.h"
15 #include "winerror.h"
16 #include "options.h"
17
18 DEFAULT_DEBUG_CHANNEL(psdrv)
19
20 static BOOL PSDRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
21                                LPCSTR output, const DEVMODEA* initData );
22 static BOOL PSDRV_DeleteDC( DC *dc );
23
24 static const DC_FUNCTIONS PSDRV_Funcs =
25 {
26     NULL,                            /* pAbortDoc */
27     NULL,                            /* pAbortPath */
28     NULL,                            /* pAngleArc */
29     PSDRV_Arc,                       /* pArc */
30     NULL,                            /* pArcTo */
31     NULL,                            /* pBeginPath */
32     NULL,                            /* pBitBlt */
33     NULL,                            /* pBitmapBits */
34     NULL,                            /* pChoosePixelFormat */
35     PSDRV_Chord,                     /* pChord */
36     NULL,                            /* pCloseFigure */
37     NULL,                            /* pCreateBitmap */
38     PSDRV_CreateDC,                  /* pCreateDC */
39     NULL,                            /* pCreateDIBSection */
40     NULL,                            /* pCreateDIBSection16 */
41     PSDRV_DeleteDC,                  /* pDeleteDC */
42     NULL,                            /* pDeleteObject */
43     NULL,                            /* pDescribePixelFormat */
44     PSDRV_DeviceCapabilities,        /* pDeviceCapabilities */
45     PSDRV_Ellipse,                   /* pEllipse */
46     PSDRV_EndDoc,                    /* pEndDoc */
47     PSDRV_EndPage,                   /* pEndPage */
48     NULL,                            /* pEndPath */
49     PSDRV_EnumDeviceFonts,           /* pEnumDeviceFonts */
50     PSDRV_Escape,                    /* pEscape */
51     NULL,                            /* pExcludeClipRect */
52     PSDRV_ExtDeviceMode,             /* pExtDeviceMode */
53     NULL,                            /* pExtFloodFill */
54     PSDRV_ExtTextOut,                /* pExtTextOut */
55     NULL,                            /* pFillPath */
56     NULL,                            /* pFillRgn */
57     NULL,                            /* pFlattenPath */
58     NULL,                            /* pFrameRgn */
59     PSDRV_GetCharWidth,              /* pGetCharWidth */
60     NULL,                            /* pGetDCOrgEx */
61     NULL,                            /* pGetPixel */
62     NULL,                            /* pGetPixelFormat */
63     PSDRV_GetTextExtentPoint,        /* pGetTextExtentPoint */
64     PSDRV_GetTextMetrics,            /* pGetTextMetrics */
65     NULL,                            /* pIntersectClipRect */
66     NULL,                            /* pInvertRgn */
67     PSDRV_LineTo,                    /* pLineTo */
68     NULL,                            /* pLoadOEMResource */
69     NULL,                            /* pMoveToEx */
70     NULL,                            /* pOffsetClipRgn */
71     NULL,                            /* pOffsetViewportOrg (optional) */
72     NULL,                            /* pOffsetWindowOrg (optional) */
73     NULL,                            /* pPaintRgn */
74     PSDRV_PatBlt,                    /* pPatBlt */
75     PSDRV_Pie,                       /* pPie */
76     NULL,                            /* pPolyBezier */
77     NULL,                            /* pPolyBezierTo */
78     NULL,                            /* pPolyDraw */
79     PSDRV_PolyPolygon,               /* pPolyPolygon */
80     PSDRV_PolyPolyline,              /* pPolyPolyline */
81     PSDRV_Polygon,                   /* pPolygon */
82     PSDRV_Polyline,                  /* pPolyline */
83     NULL,                            /* pPolylineTo */
84     NULL,                            /* pRealizePalette */
85     PSDRV_Rectangle,                 /* pRectangle */
86     NULL,                            /* pRestoreDC */
87     PSDRV_RoundRect,                 /* pRoundRect */
88     NULL,                            /* pSaveDC */
89     NULL,                            /* pScaleViewportExt (optional) */
90     NULL,                            /* pScaleWindowExt (optional) */
91     NULL,                            /* pSelectClipPath */
92     NULL,                            /* pSelectClipRgn */
93     PSDRV_SelectObject,              /* pSelectObject */
94     NULL,                            /* pSelectPalette */
95     PSDRV_SetBkColor,                /* pSetBkColor */
96     NULL,                            /* pSetBkMode */
97     PSDRV_SetDeviceClipping,         /* pSetDeviceClipping */
98     NULL,                            /* pSetDIBitsToDevice */
99     NULL,                            /* pSetMapMode (optional) */
100     NULL,                            /* pSetMapperFlags */
101     PSDRV_SetPixel,                  /* pSetPixel */
102     NULL,                            /* pSetPixelFormat */
103     NULL,                            /* pSetPolyFillMode */
104     NULL,                            /* pSetROP2 */
105     NULL,                            /* pSetRelAbs */
106     NULL,                            /* pSetStretchBltMode */
107     NULL,                            /* pSetTextAlign */
108     NULL,                            /* pSetTextCharacterExtra */
109     PSDRV_SetTextColor,              /* pSetTextColor */
110     NULL,                            /* pSetTextJustification */
111     NULL,                            /* pSetViewportExt (optional) */
112     NULL,                            /* pSetViewportOrg (optional) */
113     NULL,                            /* pSetWindowExt (optional) */
114     NULL,                            /* pSetWindowOrg (optional) */
115     PSDRV_StartDoc,                  /* pStartDoc */
116     PSDRV_StartPage,                 /* pStartPage */
117     NULL,                            /* pStretchBlt */
118     PSDRV_StretchDIBits,             /* pStretchDIBits */
119     NULL,                            /* pStrokeAndFillPath */
120     NULL,                            /* pStrokePath */
121     NULL,                            /* pSwapBuffers */
122     NULL                             /* pWidenPath */
123 };
124
125
126 /* Default entries for devcaps */
127
128 static DeviceCaps PSDRV_DevCaps = {
129 /* version */           0, 
130 /* technology */        DT_RASPRINTER,
131 /* horzSize */          210,
132 /* vertSize */          297,
133 /* horzRes */           4961,
134 /* vertRes */           7016, 
135 /* bitsPixel */         1,
136 /* planes */            1,
137 /* numBrushes */        -1,
138 /* numPens */           10,
139 /* numMarkers */        0,
140 /* numFonts */          39,
141 /* numColors */         2,
142 /* pdeviceSize */       0,      
143 /* curveCaps */         CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES |
144                         CC_WIDE | CC_STYLED | CC_WIDESTYLED | CC_INTERIORS |
145                         CC_ROUNDRECT,
146 /* lineCaps */          LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
147                         LC_STYLED | LC_WIDESTYLED | LC_INTERIORS,
148 /* polygoalnCaps */     PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON |
149                         PC_SCANLINE | PC_WIDE | PC_STYLED | PC_WIDESTYLED |
150                         PC_INTERIORS,
151 /* textCaps */          TC_CR_ANY, /* psdrv 0x59f7 */
152 /* clipCaps */          CP_RECTANGLE,
153 /* rasterCaps */        RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT |
154                         RC_DIBTODEV | RC_STRETCHBLT |
155                         RC_STRETCHDIB, /* psdrv 0x6e99 */
156 /* aspectX */           600,
157 /* aspectY */           600,
158 /* aspectXY */          848,
159 /* pad1 */              { 0 },
160 /* logPixelsX */        600,
161 /* logPixelsY */        600, 
162 /* pad2 */              { 0 },
163 /* palette size */      0,
164 /* ..etc */             0, 0 };
165
166 static PSDRV_DEVMODEA DefaultDevmode = 
167 {
168   { /* dmPublic */
169 /* dmDeviceName */      "Wine PostScript Driver",
170 /* dmSpecVersion */     0x30a,
171 /* dmDriverVersion */   0x001,
172 /* dmSize */            sizeof(DEVMODEA),
173 /* dmDriverExtra */     0,
174 /* dmFields */          DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE | 
175                         DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR | 
176                         DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION,
177    { /* u1 */
178      { /* s1 */
179 /* dmOrientation */     DMORIENT_PORTRAIT,
180 /* dmPaperSize */       DMPAPER_A4,
181 /* dmPaperLength */     2969,
182 /* dmPaperWidth */      2101
183      }
184    },
185 /* dmScale */           100, /* ?? */
186 /* dmCopies */          1,
187 /* dmDefaultSource */   DMBIN_AUTO,
188 /* dmPrintQuality */    0,
189 /* dmColor */           DMCOLOR_MONOCHROME,
190 /* dmDuplex */          0,
191 /* dmYResolution */     0,
192 /* dmTTOption */        DMTT_SUBDEV,
193 /* dmCollate */         0,
194 /* dmFormName */        "",
195 /* dmUnusedPadding */   0,
196 /* dmBitsPerPel */      0,
197 /* dmPelsWidth */       0,
198 /* dmPelsHeight */      0,
199 /* dmDisplayFlags */    0,
200 /* dmDisplayFrequency */ 0
201   },
202   { /* dmDocPrivate */
203     0 /* dummy */
204   },
205   { /* dmDrvPrivate */
206 /* ppdfilename */       "default.ppd"
207   }
208 };
209
210 HANDLE PSDRV_Heap = 0;
211
212 static HANDLE PSDRV_DefaultFont = 0;
213 static LOGFONTA DefaultLogFont = {
214     100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
215     DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
216 };
217
218 /*********************************************************************
219  *           PSDRV_Init
220  *
221  * Initializes font metrics and registers driver. Called from GDI_Init()
222  *
223  */
224 BOOL PSDRV_Init(void)
225 {
226     TRACE("\n");
227     PSDRV_Heap = HeapCreate(0, 0x10000, 0);
228     PSDRV_GetFontMetrics();
229     PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
230     return DRIVER_RegisterDriver( "WINEPS", &PSDRV_Funcs );
231 }
232
233 /**********************************************************************
234  *           PSDRV_CreateDC
235  */
236 static BOOL PSDRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
237                                LPCSTR output, const DEVMODEA* initData )
238 {
239     PSDRV_PDEVICE *physDev;
240     PRINTERINFO *pi;
241     DeviceCaps *devCaps;
242
243     /* If no device name was specified, retrieve the device name
244      * from the DEVMODE structure from the DC's physDev.
245      * (See CreateCompatibleDC) */
246     if ( !device && dc->physDev )
247     {
248         physDev = (PSDRV_PDEVICE *)dc->physDev;
249         device = physDev->Devmode->dmPublic.dmDeviceName;
250     }
251     pi = PSDRV_FindPrinterInfo(device);
252         
253     TRACE("(%s %s %s %p)\n", driver, device, output, initData);
254
255     if(!pi) return FALSE;
256
257     if(!pi->Fonts) {
258         MESSAGE("To use WINEPS you need to install some AFM files.\n");
259         return FALSE;
260     }
261
262     physDev = (PSDRV_PDEVICE *)HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
263                                                      sizeof(*physDev) );
264     if (!physDev) return FALSE;
265     dc->physDev = physDev;
266
267     physDev->pi = pi;
268
269     physDev->Devmode = (PSDRV_DEVMODEA *)HeapAlloc( PSDRV_Heap, 0,
270                                                      sizeof(PSDRV_DEVMODEA) );
271     if(!physDev->Devmode) {
272         HeapFree( PSDRV_Heap, 0, physDev );
273         return FALSE;
274     }
275     
276     memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
277
278     if(initData) {
279         PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)initData, pi);
280     }
281
282     
283     devCaps = HeapAlloc( PSDRV_Heap, 0, sizeof(PSDRV_DevCaps) );
284     memcpy(devCaps, &PSDRV_DevCaps, sizeof(PSDRV_DevCaps));
285
286     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
287         devCaps->horzSize = physDev->Devmode->dmPublic.u1.s1.dmPaperWidth / 10;
288         devCaps->vertSize = physDev->Devmode->dmPublic.u1.s1.dmPaperLength / 10;
289     } else {
290         devCaps->horzSize = physDev->Devmode->dmPublic.u1.s1.dmPaperLength / 10;
291         devCaps->vertSize = physDev->Devmode->dmPublic.u1.s1.dmPaperWidth / 10;
292     }
293
294     devCaps->horzRes = physDev->pi->ppd->DefaultResolution * 
295       devCaps->horzSize / 25.4;
296     devCaps->vertRes = physDev->pi->ppd->DefaultResolution * 
297       devCaps->vertSize / 25.4;
298
299     /* Are aspect[XY] and logPixels[XY] correct? */
300     /* Need to handle different res in x and y => fix ppd */
301     devCaps->aspectX = devCaps->logPixelsX = 
302                                 physDev->pi->ppd->DefaultResolution;
303     devCaps->aspectY = devCaps->logPixelsY = 
304                                 physDev->pi->ppd->DefaultResolution;
305     devCaps->aspectXY = (int)hypot( (double)devCaps->aspectX, 
306                                     (double)devCaps->aspectY );
307
308     if(physDev->pi->ppd->ColorDevice) {
309         devCaps->bitsPixel = 8;
310         devCaps->numColors = 256;
311         /* FIXME are these values OK? */
312     }
313
314     /* etc */
315
316     dc->w.devCaps = devCaps;
317
318     dc->w.hVisRgn = CreateRectRgn(0, 0, dc->w.devCaps->horzRes,
319                             dc->w.devCaps->vertRes);
320     
321     dc->w.hFont = PSDRV_DefaultFont;
322     physDev->job.output = output ?
323       HEAP_strdupA( PSDRV_Heap, 0, output ) :
324       HEAP_strdupA( PSDRV_Heap, 0, "LPT1:" );  /* HACK */
325     physDev->job.hJob = 0;
326     return TRUE;
327 }
328
329
330 /**********************************************************************
331  *           PSDRV_DeleteDC
332  */
333 static BOOL PSDRV_DeleteDC( DC *dc )
334 {
335     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
336     
337     TRACE("\n");
338
339     HeapFree( PSDRV_Heap, 0, physDev->Devmode );
340     HeapFree( PSDRV_Heap, 0, physDev->job.output );
341     HeapFree( PSDRV_Heap, 0, (void *)dc->w.devCaps );
342     HeapFree( PSDRV_Heap, 0, physDev );
343     dc->physDev = NULL;
344
345     return TRUE;
346 }
347
348
349         
350
351 /**********************************************************************
352  *              PSDRV_FindPrinterInfo
353  */
354 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name) 
355 {
356     static PRINTERINFO *PSDRV_PrinterList;
357     DWORD type = REG_BINARY, needed, res;
358     PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
359     FONTNAME *font;
360     AFM *afm;
361
362     TRACE("'%s'\n", name);
363     
364     for( ; pi; last = &pi->next, pi = pi->next) {
365         if(!strcmp(pi->FriendlyName, name))
366             return pi;
367     }
368
369     pi = *last = HeapAlloc( PSDRV_Heap, 0, sizeof(*pi) );
370     pi->FriendlyName = HEAP_strdupA( PSDRV_Heap, 0, name );
371     res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
372                             NULL, 0, &needed );
373
374     if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
375         pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
376         memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
377         DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
378                  REG_BINARY, (LPBYTE)&DefaultDevmode, sizeof(DefaultDevmode) );
379
380         /* need to do something here AddPrinter?? */
381     } else {
382         pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
383         DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
384                           (LPBYTE)pi->Devmode, needed, &needed);
385     }
386
387     PROFILE_GetWineIniString("psdrv", "ppdfile", "default.ppd",
388                              pi->Devmode->dmDrvPrivate.ppdFileName, 256);
389     pi->ppd = PSDRV_ParsePPD(pi->Devmode->dmDrvPrivate.ppdFileName);
390     if(!pi->ppd) {
391         HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
392         HeapFree(PSDRV_Heap, 0, pi->Devmode);
393         HeapFree(PSDRV_Heap, 0, pi);
394         *last = NULL;
395         MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
396             pi->Devmode->dmDrvPrivate.ppdFileName);
397         return NULL;
398     }
399
400     pi->next = NULL;
401     pi->Fonts = NULL;
402
403     for(font = pi->ppd->InstalledFonts; font; font = font->next) {
404         afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
405         if(!afm) {
406             MESSAGE(
407          "Couldn't find AFM file for installed printer font '%s' - ignoring\n",
408          font->Name);
409         } else {
410             PSDRV_AddAFMtoList(&pi->Fonts, afm);
411         }
412     }
413
414     return pi;
415 }