msvcp140: Implement _Stat and _Lstat.
[wine.git] / dlls / wineps.drv / init.c
blob3b00343acc8a04411d3f852303723817ef7a4c4f
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "winreg.h"
35 #include "winnls.h"
36 #include "winuser.h"
37 #include "psdrv.h"
38 #include "winspool.h"
39 #include "wine/library.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
44 static const PSDRV_DEVMODE DefaultDevmode =
46 { /* dmPublic */
47 /* dmDeviceName */ {'W','i','n','e',' ','P','o','s','t','S','c','r','i','p','t',' ','D','r','i','v','e','r',0},
48 /* dmSpecVersion */ 0x30a,
49 /* dmDriverVersion */ 0x001,
50 /* dmSize */ sizeof(DEVMODEW),
51 /* dmDriverExtra */ sizeof(PSDRV_DEVMODE)-sizeof(DEVMODEW),
52 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH |
53 DM_SCALE | DM_COPIES | DM_DEFAULTSOURCE | DM_PRINTQUALITY |
54 DM_COLOR | DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION |
55 DM_COLLATE | DM_FORMNAME,
56 { /* u1 */
57 { /* s1 */
58 /* dmOrientation */ DMORIENT_PORTRAIT,
59 /* dmPaperSize */ DMPAPER_LETTER,
60 /* dmPaperLength */ 2794,
61 /* dmPaperWidth */ 2159,
62 /* dmScale */ 100,
63 /* dmCopies */ 1,
64 /* dmDefaultSource */ DMBIN_AUTO,
65 /* dmPrintQuality */ 300
68 /* dmColor */ DMCOLOR_COLOR,
69 /* dmDuplex */ DMDUP_SIMPLEX,
70 /* dmYResolution */ 300,
71 /* dmTTOption */ DMTT_SUBDEV,
72 /* dmCollate */ DMCOLLATE_FALSE,
73 /* dmFormName */ {'L','e','t','t','e','r',0},
74 /* dmLogPixels */ 0,
75 /* dmBitsPerPel */ 0,
76 /* dmPelsWidth */ 0,
77 /* dmPelsHeight */ 0,
78 { /* u2 */
79 /* dmDisplayFlags */ 0
81 /* dmDisplayFrequency */ 0,
82 /* dmICMMethod */ 0,
83 /* dmICMIntent */ 0,
84 /* dmMediaType */ 0,
85 /* dmDitherType */ 0,
86 /* dmReserved1 */ 0,
87 /* dmReserved2 */ 0,
88 /* dmPanningWidth */ 0,
89 /* dmPanningHeight */ 0
91 { /* dmDocPrivate */
92 /* dummy */ 0
94 { /* dmDrvPrivate */
95 /* numInstalledOptions */ 0
99 HINSTANCE PSDRV_hInstance = 0;
100 HANDLE PSDRV_Heap = 0;
102 static HFONT PSDRV_DefaultFont = 0;
103 static const LOGFONTA DefaultLogFont = {
104 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
105 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
108 static const struct gdi_dc_funcs psdrv_funcs;
110 /*********************************************************************
111 * DllMain
113 * Initializes font metrics and registers driver. wineps dll entry point.
116 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
118 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
120 switch(reason) {
122 case DLL_PROCESS_ATTACH:
123 PSDRV_hInstance = hinst;
124 DisableThreadLibraryCalls(hinst);
126 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
127 if (PSDRV_Heap == NULL)
128 return FALSE;
130 if (PSDRV_GetFontMetrics() == FALSE) {
131 HeapDestroy(PSDRV_Heap);
132 return FALSE;
135 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
136 if (PSDRV_DefaultFont == NULL) {
137 HeapDestroy(PSDRV_Heap);
138 return FALSE;
140 break;
142 case DLL_PROCESS_DETACH:
143 if (reserved) break;
144 DeleteObject( PSDRV_DefaultFont );
145 HeapDestroy( PSDRV_Heap );
146 break;
149 return TRUE;
152 static void dump_fields(DWORD fields)
154 int add_space = 0;
156 #define CHECK_FIELD(flag) \
157 do \
159 if (fields & flag) \
161 if (add_space++) DPRINTF(" "); \
162 TRACE(#flag); \
163 fields &= ~flag; \
166 while (0)
168 CHECK_FIELD(DM_ORIENTATION);
169 CHECK_FIELD(DM_PAPERSIZE);
170 CHECK_FIELD(DM_PAPERLENGTH);
171 CHECK_FIELD(DM_PAPERWIDTH);
172 CHECK_FIELD(DM_SCALE);
173 CHECK_FIELD(DM_POSITION);
174 CHECK_FIELD(DM_NUP);
175 CHECK_FIELD(DM_DISPLAYORIENTATION);
176 CHECK_FIELD(DM_COPIES);
177 CHECK_FIELD(DM_DEFAULTSOURCE);
178 CHECK_FIELD(DM_PRINTQUALITY);
179 CHECK_FIELD(DM_COLOR);
180 CHECK_FIELD(DM_DUPLEX);
181 CHECK_FIELD(DM_YRESOLUTION);
182 CHECK_FIELD(DM_TTOPTION);
183 CHECK_FIELD(DM_COLLATE);
184 CHECK_FIELD(DM_FORMNAME);
185 CHECK_FIELD(DM_LOGPIXELS);
186 CHECK_FIELD(DM_BITSPERPEL);
187 CHECK_FIELD(DM_PELSWIDTH);
188 CHECK_FIELD(DM_PELSHEIGHT);
189 CHECK_FIELD(DM_DISPLAYFLAGS);
190 CHECK_FIELD(DM_DISPLAYFREQUENCY);
191 CHECK_FIELD(DM_ICMMETHOD);
192 CHECK_FIELD(DM_ICMINTENT);
193 CHECK_FIELD(DM_MEDIATYPE);
194 CHECK_FIELD(DM_DITHERTYPE);
195 CHECK_FIELD(DM_PANNINGWIDTH);
196 CHECK_FIELD(DM_PANNINGHEIGHT);
197 if (fields) TRACE(" %#x", fields);
198 TRACE("\n");
199 #undef CHECK_FIELD
202 /* Dump DEVMODE structure without a device specific part.
203 * Some applications and drivers fail to specify correct field
204 * flags (like DM_FORMNAME), so dump everything.
206 static void dump_devmode(const DEVMODEW *dm)
208 if (!TRACE_ON(psdrv)) return;
210 TRACE("dmDeviceName: %s\n", debugstr_w(dm->dmDeviceName));
211 TRACE("dmSpecVersion: 0x%04x\n", dm->dmSpecVersion);
212 TRACE("dmDriverVersion: 0x%04x\n", dm->dmDriverVersion);
213 TRACE("dmSize: 0x%04x\n", dm->dmSize);
214 TRACE("dmDriverExtra: 0x%04x\n", dm->dmDriverExtra);
215 TRACE("dmFields: 0x%04x\n", dm->dmFields);
216 dump_fields(dm->dmFields);
217 TRACE("dmOrientation: %d\n", dm->u1.s1.dmOrientation);
218 TRACE("dmPaperSize: %d\n", dm->u1.s1.dmPaperSize);
219 TRACE("dmPaperLength: %d\n", dm->u1.s1.dmPaperLength);
220 TRACE("dmPaperWidth: %d\n", dm->u1.s1.dmPaperWidth);
221 TRACE("dmScale: %d\n", dm->u1.s1.dmScale);
222 TRACE("dmCopies: %d\n", dm->u1.s1.dmCopies);
223 TRACE("dmDefaultSource: %d\n", dm->u1.s1.dmDefaultSource);
224 TRACE("dmPrintQuality: %d\n", dm->u1.s1.dmPrintQuality);
225 TRACE("dmColor: %d\n", dm->dmColor);
226 TRACE("dmDuplex: %d\n", dm->dmDuplex);
227 TRACE("dmYResolution: %d\n", dm->dmYResolution);
228 TRACE("dmTTOption: %d\n", dm->dmTTOption);
229 TRACE("dmCollate: %d\n", dm->dmCollate);
230 TRACE("dmFormName: %s\n", debugstr_w(dm->dmFormName));
231 TRACE("dmLogPixels %u\n", dm->dmLogPixels);
232 TRACE("dmBitsPerPel %u\n", dm->dmBitsPerPel);
233 TRACE("dmPelsWidth %u\n", dm->dmPelsWidth);
234 TRACE("dmPelsHeight %u\n", dm->dmPelsHeight);
237 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
239 PAGESIZE *page;
240 RESOLUTION *res;
241 INT width = 0, height = 0, resx = 0, resy = 0;
243 dump_devmode(&physDev->Devmode->dmPublic);
245 if (physDev->Devmode->dmPublic.dmFields & (DM_PRINTQUALITY | DM_YRESOLUTION | DM_LOGPIXELS))
247 if (physDev->Devmode->dmPublic.dmFields & DM_PRINTQUALITY)
248 resx = resy = physDev->Devmode->dmPublic.u1.s1.dmPrintQuality;
250 if (physDev->Devmode->dmPublic.dmFields & DM_YRESOLUTION)
251 resy = physDev->Devmode->dmPublic.dmYResolution;
253 if (physDev->Devmode->dmPublic.dmFields & DM_LOGPIXELS)
254 resx = resy = physDev->Devmode->dmPublic.dmLogPixels;
256 LIST_FOR_EACH_ENTRY(res, &physDev->pi->ppd->Resolutions, RESOLUTION, entry)
258 if (res->resx == resx && res->resy == resy)
260 physDev->logPixelsX = resx;
261 physDev->logPixelsY = resy;
262 break;
266 if (&res->entry == &physDev->pi->ppd->Resolutions)
268 WARN("Requested resolution %dx%d is not supported by device\n", resx, resy);
269 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
270 physDev->logPixelsY = physDev->logPixelsX;
273 else
275 WARN("Using default device resolution %d\n", physDev->pi->ppd->DefaultResolution);
276 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
277 physDev->logPixelsY = physDev->logPixelsX;
280 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
281 LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
282 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
283 break;
286 if(&page->entry == &physDev->pi->ppd->PageSizes) {
287 FIXME("Can't find page\n");
288 SetRectEmpty(&physDev->ImageableArea);
289 physDev->PageSize.cx = 0;
290 physDev->PageSize.cy = 0;
291 } else if(page->ImageableArea) {
292 /* physDev sizes in device units; ppd sizes in 1/72" */
293 SetRect(&physDev->ImageableArea, page->ImageableArea->llx * physDev->logPixelsX / 72,
294 page->ImageableArea->ury * physDev->logPixelsY / 72,
295 page->ImageableArea->urx * physDev->logPixelsX / 72,
296 page->ImageableArea->lly * physDev->logPixelsY / 72);
297 physDev->PageSize.cx = page->PaperDimension->x *
298 physDev->logPixelsX / 72;
299 physDev->PageSize.cy = page->PaperDimension->y *
300 physDev->logPixelsY / 72;
301 } else {
302 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
303 physDev->ImageableArea.right = physDev->PageSize.cx =
304 page->PaperDimension->x * physDev->logPixelsX / 72;
305 physDev->ImageableArea.top = physDev->PageSize.cy =
306 page->PaperDimension->y * physDev->logPixelsY / 72;
308 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
309 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
310 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
311 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
312 physDev->ImageableArea.right = physDev->PageSize.cx =
313 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
314 physDev->logPixelsX / 254;
315 physDev->ImageableArea.top = physDev->PageSize.cy =
316 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
317 physDev->logPixelsY / 254;
318 } else {
319 FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
320 SetRectEmpty(&physDev->ImageableArea);
321 physDev->PageSize.cx = 0;
322 physDev->PageSize.cy = 0;
325 TRACE("ImageableArea = %s: PageSize = %dx%d\n", wine_dbgstr_rect(&physDev->ImageableArea),
326 physDev->PageSize.cx, physDev->PageSize.cy);
328 /* these are in device units */
329 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
330 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
332 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
333 physDev->horzRes = width;
334 physDev->vertRes = height;
335 } else {
336 physDev->horzRes = height;
337 physDev->vertRes = width;
340 /* these are in mm */
341 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
342 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
344 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
345 "horzRes = %d, vertRes = %d\n",
346 physDev->horzSize, physDev->vertSize,
347 physDev->horzRes, physDev->vertRes);
350 static PSDRV_PDEVICE *create_psdrv_physdev( PRINTERINFO *pi )
352 PSDRV_PDEVICE *physDev;
354 physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) );
355 if (!physDev) return NULL;
357 physDev->Devmode = HeapAlloc( GetProcessHeap(), 0, sizeof(PSDRV_DEVMODE) );
358 if (!physDev->Devmode)
360 HeapFree( GetProcessHeap(), 0, physDev );
361 return NULL;
364 *physDev->Devmode = *pi->Devmode;
365 physDev->pi = pi;
366 physDev->logPixelsX = pi->ppd->DefaultResolution;
367 physDev->logPixelsY = pi->ppd->DefaultResolution;
368 return physDev;
371 /**********************************************************************
372 * PSDRV_CreateDC
374 static BOOL PSDRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
375 LPCWSTR output, const DEVMODEW* initData )
377 PSDRV_PDEVICE *physDev;
378 PRINTERINFO *pi;
380 TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
381 debugstr_w(output), initData);
383 if (!device) return FALSE;
384 pi = PSDRV_FindPrinterInfo( device );
385 if(!pi) return FALSE;
387 if(!pi->Fonts) {
388 RASTERIZER_STATUS status;
389 if(!GetRasterizerCaps(&status, sizeof(status)) ||
390 !(status.wFlags & TT_AVAILABLE) ||
391 !(status.wFlags & TT_ENABLED)) {
392 MESSAGE("Disabling printer %s since it has no builtin fonts and there are no TrueType fonts available.\n",
393 debugstr_w(device));
394 return FALSE;
398 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
400 if (output && *output) physDev->job.output = strdupW( output );
402 if(initData)
404 dump_devmode(initData);
405 PSDRV_MergeDevmodes(physDev->Devmode, (const PSDRV_DEVMODE *)initData, pi);
408 PSDRV_UpdateDevCaps(physDev);
409 SelectObject( (*pdev)->hdc, PSDRV_DefaultFont );
410 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
411 return TRUE;
415 /**********************************************************************
416 * PSDRV_CreateCompatibleDC
418 static BOOL PSDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
420 HDC hdc = (*pdev)->hdc;
421 PSDRV_PDEVICE *physDev, *orig_dev = get_psdrv_dev( orig );
422 PRINTERINFO *pi = PSDRV_FindPrinterInfo( orig_dev->pi->friendly_name );
424 if (!pi) return FALSE;
425 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
426 PSDRV_MergeDevmodes( physDev->Devmode, orig_dev->Devmode, pi );
427 PSDRV_UpdateDevCaps(physDev);
428 SelectObject( hdc, PSDRV_DefaultFont );
429 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
430 return TRUE;
435 /**********************************************************************
436 * PSDRV_DeleteDC
438 static BOOL PSDRV_DeleteDC( PHYSDEV dev )
440 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
442 TRACE("\n");
444 HeapFree( GetProcessHeap(), 0, physDev->Devmode );
445 HeapFree( GetProcessHeap(), 0, physDev->job.output );
446 HeapFree( GetProcessHeap(), 0, physDev );
448 return TRUE;
452 /**********************************************************************
453 * ResetDC (WINEPS.@)
455 static HDC PSDRV_ResetDC( PHYSDEV dev, const DEVMODEW *lpInitData )
457 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
459 if (lpInitData)
461 PSDRV_MergeDevmodes(physDev->Devmode, (const PSDRV_DEVMODE *)lpInitData, physDev->pi);
462 PSDRV_UpdateDevCaps(physDev);
464 return dev->hdc;
467 /***********************************************************************
468 * GetDeviceCaps (WINEPS.@)
470 static INT PSDRV_GetDeviceCaps( PHYSDEV dev, INT cap )
472 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
474 TRACE("%p,%d\n", dev->hdc, cap);
476 switch(cap)
478 case DRIVERVERSION:
479 return 0;
480 case TECHNOLOGY:
481 return DT_RASPRINTER;
482 case HORZSIZE:
483 return MulDiv(physDev->horzSize, 100,
484 physDev->Devmode->dmPublic.u1.s1.dmScale);
485 case VERTSIZE:
486 return MulDiv(physDev->vertSize, 100,
487 physDev->Devmode->dmPublic.u1.s1.dmScale);
488 case HORZRES:
489 case DESKTOPHORZRES:
490 return physDev->horzRes;
491 case VERTRES:
492 case DESKTOPVERTRES:
493 return physDev->vertRes;
494 case BITSPIXEL:
495 /* Although Windows returns 1 for monochrome printers, we want
496 CreateCompatibleBitmap to provide something other than 1 bpp */
497 return 32;
498 case PLANES:
499 return 1;
500 case NUMBRUSHES:
501 return -1;
502 case NUMPENS:
503 return 10;
504 case NUMMARKERS:
505 return 0;
506 case NUMFONTS:
507 return 39;
508 case NUMCOLORS:
509 return -1;
510 case PDEVICESIZE:
511 return sizeof(PSDRV_PDEVICE);
512 case CURVECAPS:
513 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
514 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
515 case LINECAPS:
516 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
517 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
518 case POLYGONALCAPS:
519 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
520 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
521 case TEXTCAPS:
522 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
523 case CLIPCAPS:
524 return CP_RECTANGLE;
525 case RASTERCAPS:
526 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
527 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
528 case ASPECTX:
529 return physDev->logPixelsX;
530 case ASPECTY:
531 return physDev->logPixelsY;
532 case ASPECTXY:
533 return (int)hypot( (double)physDev->logPixelsX,
534 (double)physDev->logPixelsY );
535 case LOGPIXELSX:
536 return MulDiv(physDev->logPixelsX,
537 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
538 case LOGPIXELSY:
539 return MulDiv(physDev->logPixelsY,
540 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
541 case SIZEPALETTE:
542 return 0;
543 case NUMRESERVED:
544 return 0;
545 case COLORRES:
546 return 0;
547 case PHYSICALWIDTH:
548 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
549 physDev->PageSize.cy : physDev->PageSize.cx;
550 case PHYSICALHEIGHT:
551 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
552 physDev->PageSize.cx : physDev->PageSize.cy;
553 case PHYSICALOFFSETX:
554 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
555 if(physDev->pi->ppd->LandscapeOrientation == -90)
556 return physDev->PageSize.cy - physDev->ImageableArea.top;
557 else
558 return physDev->ImageableArea.bottom;
560 return physDev->ImageableArea.left;
562 case PHYSICALOFFSETY:
563 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
564 if(physDev->pi->ppd->LandscapeOrientation == -90)
565 return physDev->PageSize.cx - physDev->ImageableArea.right;
566 else
567 return physDev->ImageableArea.left;
569 return physDev->PageSize.cy - physDev->ImageableArea.top;
571 case SCALINGFACTORX:
572 case SCALINGFACTORY:
573 case VREFRESH:
574 case BLTALIGNMENT:
575 return 0;
576 case SHADEBLENDCAPS:
577 return SB_NONE;
578 default:
579 FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
580 return 0;
584 static PRINTER_ENUM_VALUESA *load_font_sub_table( HANDLE printer, DWORD *num_entries )
586 DWORD res, needed, num;
587 PRINTER_ENUM_VALUESA *table = NULL;
588 static const char fontsubkey[] = "PrinterDriverData\\FontSubTable";
590 *num_entries = 0;
592 res = EnumPrinterDataExA( printer, fontsubkey, NULL, 0, &needed, &num );
593 if (res != ERROR_MORE_DATA) return NULL;
595 table = HeapAlloc( PSDRV_Heap, 0, needed );
596 if (!table) return NULL;
598 res = EnumPrinterDataExA( printer, fontsubkey, (LPBYTE)table, needed, &needed, &num );
599 if (res != ERROR_SUCCESS)
601 HeapFree( PSDRV_Heap, 0, table );
602 return NULL;
605 *num_entries = num;
606 return table;
609 static PSDRV_DEVMODE *get_printer_devmode( HANDLE printer )
611 DWORD needed, dm_size;
612 BOOL res;
613 PRINTER_INFO_9W *info;
614 PSDRV_DEVMODE *dm;
616 GetPrinterW( printer, 9, NULL, 0, &needed );
617 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
619 info = HeapAlloc( PSDRV_Heap, 0, needed );
620 res = GetPrinterW( printer, 9, (BYTE *)info, needed, &needed );
621 if (!res || !info->pDevMode)
623 HeapFree( PSDRV_Heap, 0, info );
624 return NULL;
627 /* sanity check the sizes */
628 dm_size = info->pDevMode->dmSize + info->pDevMode->dmDriverExtra;
629 if ((char *)info->pDevMode - (char *)info + dm_size > needed)
631 HeapFree( PSDRV_Heap, 0, info );
632 return NULL;
635 dm = (PSDRV_DEVMODE*)info;
636 memmove( dm, info->pDevMode, dm_size );
637 return dm;
640 static PSDRV_DEVMODE *get_devmode( HANDLE printer, const WCHAR *name, BOOL *is_default )
642 PSDRV_DEVMODE *dm = get_printer_devmode( printer );
644 *is_default = FALSE;
646 if (dm && dm->dmPublic.dmSize + dm->dmPublic.dmDriverExtra >= sizeof(DefaultDevmode))
648 TRACE( "Retrieved devmode from winspool\n" );
649 return dm;
651 HeapFree( PSDRV_Heap, 0, dm );
653 TRACE( "Using default devmode\n" );
654 dm = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
655 if (dm)
657 *dm = DefaultDevmode;
658 lstrcpynW( (WCHAR *)dm->dmPublic.dmDeviceName, name, CCHDEVICENAME );
659 *is_default = TRUE;
661 return dm;
664 static BOOL set_devmode( HANDLE printer, PSDRV_DEVMODE *dm )
666 PRINTER_INFO_9W info;
667 info.pDevMode = &dm->dmPublic;
669 return SetPrinterW( printer, 9, (BYTE *)&info, 0 );
672 static WCHAR *get_ppd_filename( HANDLE printer )
674 DWORD needed;
675 DRIVER_INFO_2W *info;
676 WCHAR *name;
678 GetPrinterDriverW( printer, NULL, 2, NULL, 0, &needed );
679 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
680 info = HeapAlloc( GetProcessHeap(), 0, needed );
681 if (!info) return NULL;
682 GetPrinterDriverW( printer, NULL, 2, (BYTE*)info, needed, &needed );
683 name = (WCHAR *)info;
684 memmove( name, info->pDataFile, (strlenW( info->pDataFile ) + 1) * sizeof(WCHAR) );
685 return name;
688 static struct list printer_list = LIST_INIT( printer_list );
690 /**********************************************************************
691 * PSDRV_FindPrinterInfo
693 PRINTERINFO *PSDRV_FindPrinterInfo(LPCWSTR name)
695 PRINTERINFO *pi;
696 FONTNAME *font;
697 const AFM *afm;
698 HANDLE hPrinter = 0;
699 WCHAR *ppd_filename = NULL;
700 char *nameA = NULL;
701 BOOL using_default_devmode = FALSE;
702 int len;
704 TRACE("'%s'\n", debugstr_w(name));
706 LIST_FOR_EACH_ENTRY( pi, &printer_list, PRINTERINFO, entry )
708 if (!strcmpW( pi->friendly_name, name ))
709 return pi;
712 pi = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
713 if (pi == NULL) return NULL;
715 if (!(pi->friendly_name = HeapAlloc( PSDRV_Heap, 0, (strlenW(name)+1)*sizeof(WCHAR) ))) goto fail;
716 strcpyW( pi->friendly_name, name );
718 if (OpenPrinterW( pi->friendly_name, &hPrinter, NULL ) == 0) {
719 ERR ("OpenPrinter failed with code %i\n", GetLastError ());
720 goto fail;
723 len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
724 nameA = HeapAlloc( GetProcessHeap(), 0, len );
725 WideCharToMultiByte( CP_ACP, 0, name, -1, nameA, len, NULL, NULL );
727 pi->Devmode = get_devmode( hPrinter, name, &using_default_devmode );
728 if (!pi->Devmode) goto fail;
730 ppd_filename = get_ppd_filename( hPrinter );
731 if (!ppd_filename) goto fail;
733 pi->ppd = PSDRV_ParsePPD( ppd_filename, hPrinter );
734 if (!pi->ppd)
736 WARN( "Couldn't parse PPD file %s\n", debugstr_w(ppd_filename) );
737 goto fail;
740 if(using_default_devmode) {
741 DWORD papersize;
743 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
744 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
745 PSDRV_DEVMODE dm;
746 memset(&dm, 0, sizeof(dm));
747 dm.dmPublic.dmFields = DM_PAPERSIZE;
748 dm.dmPublic.u1.s1.dmPaperSize = papersize;
749 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
753 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
754 PSDRV_DEVMODE dm;
755 memset(&dm, 0, sizeof(dm));
756 dm.dmPublic.dmFields = DM_PAPERSIZE;
757 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
758 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
761 /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
762 WinDuplex == 0 is a special case which means that the ppd has a
763 *DefaultDuplex: NotCapable entry. In this case we'll try not to confuse
764 apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
765 PSDRV_WriteHeader understands this and copes. */
766 pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
767 if(pi->ppd->DefaultDuplex) {
768 pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
769 if(pi->Devmode->dmPublic.dmDuplex != 0)
770 pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
771 else
772 pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
775 set_devmode( hPrinter, pi->Devmode );
777 pi->FontSubTable = load_font_sub_table( hPrinter, &pi->FontSubTableSize );
779 LIST_FOR_EACH_ENTRY( font, &pi->ppd->InstalledFonts, FONTNAME, entry )
781 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
782 if(!afm) {
783 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
784 "ignoring\n", font->Name);
786 else {
787 BOOL added;
788 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
789 PSDRV_FreeAFMList(pi->Fonts);
790 goto fail;
795 ClosePrinter( hPrinter );
796 HeapFree( GetProcessHeap(), 0, nameA );
797 HeapFree( GetProcessHeap(), 0, ppd_filename );
798 list_add_head( &printer_list, &pi->entry );
799 return pi;
801 fail:
802 if (hPrinter) ClosePrinter( hPrinter );
803 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
804 HeapFree(PSDRV_Heap, 0, pi->friendly_name);
805 HeapFree(PSDRV_Heap, 0, pi->Devmode);
806 HeapFree(PSDRV_Heap, 0, pi);
807 HeapFree( GetProcessHeap(), 0, nameA );
808 HeapFree( GetProcessHeap(), 0, ppd_filename );
809 return NULL;
813 static const struct gdi_dc_funcs psdrv_funcs =
815 NULL, /* pAbortDoc */
816 NULL, /* pAbortPath */
817 NULL, /* pAlphaBlend */
818 NULL, /* pAngleArc */
819 PSDRV_Arc, /* pArc */
820 NULL, /* pArcTo */
821 NULL, /* pBeginPath */
822 NULL, /* pBlendImage */
823 PSDRV_Chord, /* pChord */
824 NULL, /* pCloseFigure */
825 PSDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
826 PSDRV_CreateDC, /* pCreateDC */
827 PSDRV_DeleteDC, /* pDeleteDC */
828 NULL, /* pDeleteObject */
829 PSDRV_DeviceCapabilities, /* pDeviceCapabilities */
830 PSDRV_Ellipse, /* pEllipse */
831 PSDRV_EndDoc, /* pEndDoc */
832 PSDRV_EndPage, /* pEndPage */
833 NULL, /* pEndPath */
834 PSDRV_EnumFonts, /* pEnumFonts */
835 NULL, /* pEnumICMProfiles */
836 NULL, /* pExcludeClipRect */
837 PSDRV_ExtDeviceMode, /* pExtDeviceMode */
838 PSDRV_ExtEscape, /* pExtEscape */
839 NULL, /* pExtFloodFill */
840 NULL, /* pExtSelectClipRgn */
841 PSDRV_ExtTextOut, /* pExtTextOut */
842 PSDRV_FillPath, /* pFillPath */
843 NULL, /* pFillRgn */
844 NULL, /* pFlattenPath */
845 NULL, /* pFontIsLinked */
846 NULL, /* pFrameRgn */
847 NULL, /* pGdiComment */
848 NULL, /* pGetBoundsRect */
849 NULL, /* pGetCharABCWidths */
850 NULL, /* pGetCharABCWidthsI */
851 PSDRV_GetCharWidth, /* pGetCharWidth */
852 PSDRV_GetDeviceCaps, /* pGetDeviceCaps */
853 NULL, /* pGetDeviceGammaRamp */
854 NULL, /* pGetFontData */
855 NULL, /* pGetFontRealizationInfo */
856 NULL, /* pGetFontUnicodeRanges */
857 NULL, /* pGetGlyphIndices */
858 NULL, /* pGetGlyphOutline */
859 NULL, /* pGetICMProfile */
860 NULL, /* pGetImage */
861 NULL, /* pGetKerningPairs */
862 NULL, /* pGetNearestColor */
863 NULL, /* pGetOutlineTextMetrics */
864 NULL, /* pGetPixel */
865 NULL, /* pGetSystemPaletteEntries */
866 NULL, /* pGetTextCharsetInfo */
867 PSDRV_GetTextExtentExPoint, /* pGetTextExtentExPoint */
868 NULL, /* pGetTextExtentExPointI */
869 NULL, /* pGetTextFace */
870 PSDRV_GetTextMetrics, /* pGetTextMetrics */
871 NULL, /* pGradientFill */
872 NULL, /* pIntersectClipRect */
873 NULL, /* pInvertRgn */
874 PSDRV_LineTo, /* pLineTo */
875 NULL, /* pModifyWorldTransform */
876 NULL, /* pMoveTo */
877 NULL, /* pOffsetClipRgn */
878 NULL, /* pOffsetViewportOrg */
879 NULL, /* pOffsetWindowOrg */
880 PSDRV_PaintRgn, /* pPaintRgn */
881 PSDRV_PatBlt, /* pPatBlt */
882 PSDRV_Pie, /* pPie */
883 PSDRV_PolyBezier, /* pPolyBezier */
884 PSDRV_PolyBezierTo, /* pPolyBezierTo */
885 NULL, /* pPolyDraw */
886 PSDRV_PolyPolygon, /* pPolyPolygon */
887 PSDRV_PolyPolyline, /* pPolyPolyline */
888 NULL, /* pPolygon */
889 NULL, /* pPolyline */
890 NULL, /* pPolylineTo */
891 PSDRV_PutImage, /* pPutImage */
892 NULL, /* pRealizeDefaultPalette */
893 NULL, /* pRealizePalette */
894 PSDRV_Rectangle, /* pRectangle */
895 PSDRV_ResetDC, /* pResetDC */
896 NULL, /* pRestoreDC */
897 PSDRV_RoundRect, /* pRoundRect */
898 NULL, /* pSaveDC */
899 NULL, /* pScaleViewportExt */
900 NULL, /* pScaleWindowExt */
901 NULL, /* pSelectBitmap */
902 PSDRV_SelectBrush, /* pSelectBrush */
903 NULL, /* pSelectClipPath */
904 PSDRV_SelectFont, /* pSelectFont */
905 NULL, /* pSelectPalette */
906 PSDRV_SelectPen, /* pSelectPen */
907 NULL, /* pSetArcDirection */
908 PSDRV_SetBkColor, /* pSetBkColor */
909 NULL, /* pSetBkMode */
910 NULL, /* pSetBoundsRect */
911 PSDRV_SetDCBrushColor, /* pSetDCBrushColor */
912 PSDRV_SetDCPenColor, /* pSetDCPenColor */
913 NULL, /* pSetDIBitsToDevice */
914 NULL, /* pSetDeviceClipping */
915 NULL, /* pSetDeviceGammaRamp */
916 NULL, /* pSetLayout */
917 NULL, /* pSetMapMode */
918 NULL, /* pSetMapperFlags */
919 PSDRV_SetPixel, /* pSetPixel */
920 NULL, /* pSetPolyFillMode */
921 NULL, /* pSetROP2 */
922 NULL, /* pSetRelAbs */
923 NULL, /* pSetStretchBltMode */
924 NULL, /* pSetTextAlign */
925 NULL, /* pSetTextCharacterExtra */
926 PSDRV_SetTextColor, /* pSetTextColor */
927 NULL, /* pSetTextJustification */
928 NULL, /* pSetViewportExt */
929 NULL, /* pSetViewportOrg */
930 NULL, /* pSetWindowExt */
931 NULL, /* pSetWindowOrg */
932 NULL, /* pSetWorldTransform */
933 PSDRV_StartDoc, /* pStartDoc */
934 PSDRV_StartPage, /* pStartPage */
935 NULL, /* pStretchBlt */
936 NULL, /* pStretchDIBits */
937 PSDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
938 PSDRV_StrokePath, /* pStrokePath */
939 NULL, /* pUnrealizePalette */
940 NULL, /* pWidenPath */
941 NULL, /* wine_get_wgl_driver */
942 GDI_PRIORITY_GRAPHICS_DRV /* priority */
946 /******************************************************************************
947 * PSDRV_get_gdi_driver
949 const struct gdi_dc_funcs * CDECL PSDRV_get_gdi_driver( unsigned int version )
951 if (version != WINE_GDI_DRIVER_VERSION)
953 ERR( "version mismatch, gdi32 wants %u but wineps has %u\n", version, WINE_GDI_DRIVER_VERSION );
954 return NULL;
956 return &psdrv_funcs;