msdmo: Actually copy output string in IEnumDMO_fnNext.
[wine.git] / dlls / wineps.drv / init.c
blobfde65c2e7d285861d64e785fbf40814481384933
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 "psdrv.h"
37 #include "winspool.h"
38 #include "wine/library.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
43 static const PSDRV_DEVMODE DefaultDevmode =
45 { /* dmPublic */
46 /* dmDeviceName */ {'W','i','n','e',' ','P','o','s','t','S','c','r','i','p','t',' ','D','r','i','v','e','r',0},
47 /* dmSpecVersion */ 0x30a,
48 /* dmDriverVersion */ 0x001,
49 /* dmSize */ sizeof(DEVMODEW),
50 /* dmDriverExtra */ sizeof(PSDRV_DEVMODE)-sizeof(DEVMODEW),
51 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH |
52 DM_SCALE | DM_COPIES | DM_DEFAULTSOURCE | DM_PRINTQUALITY |
53 DM_COLOR | DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION |
54 DM_COLLATE | DM_FORMNAME,
55 { /* u1 */
56 { /* s1 */
57 /* dmOrientation */ DMORIENT_PORTRAIT,
58 /* dmPaperSize */ DMPAPER_LETTER,
59 /* dmPaperLength */ 2794,
60 /* dmPaperWidth */ 2159,
61 /* dmScale */ 100,
62 /* dmCopies */ 1,
63 /* dmDefaultSource */ DMBIN_AUTO,
64 /* dmPrintQuality */ 300
67 /* dmColor */ DMCOLOR_COLOR,
68 /* dmDuplex */ DMDUP_SIMPLEX,
69 /* dmYResolution */ 300,
70 /* dmTTOption */ DMTT_SUBDEV,
71 /* dmCollate */ DMCOLLATE_FALSE,
72 /* dmFormName */ {'L','e','t','t','e','r',0},
73 /* dmLogPixels */ 0,
74 /* dmBitsPerPel */ 0,
75 /* dmPelsWidth */ 0,
76 /* dmPelsHeight */ 0,
77 { /* u2 */
78 /* dmDisplayFlags */ 0
80 /* dmDisplayFrequency */ 0,
81 /* dmICMMethod */ 0,
82 /* dmICMIntent */ 0,
83 /* dmMediaType */ 0,
84 /* dmDitherType */ 0,
85 /* dmReserved1 */ 0,
86 /* dmReserved2 */ 0,
87 /* dmPanningWidth */ 0,
88 /* dmPanningHeight */ 0
90 { /* dmDocPrivate */
91 /* dummy */ 0
93 { /* dmDrvPrivate */
94 /* numInstalledOptions */ 0
98 HINSTANCE PSDRV_hInstance = 0;
99 HANDLE PSDRV_Heap = 0;
101 static HFONT PSDRV_DefaultFont = 0;
102 static const LOGFONTA DefaultLogFont = {
103 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
104 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
107 static const struct gdi_dc_funcs psdrv_funcs;
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, %d, %p)\n", hinst, reason, reserved);
119 switch(reason) {
121 case DLL_PROCESS_ATTACH:
122 PSDRV_hInstance = hinst;
123 DisableThreadLibraryCalls(hinst);
125 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
126 if (PSDRV_Heap == NULL)
127 return FALSE;
129 if (PSDRV_GetFontMetrics() == FALSE) {
130 HeapDestroy(PSDRV_Heap);
131 return FALSE;
134 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
135 if (PSDRV_DefaultFont == NULL) {
136 HeapDestroy(PSDRV_Heap);
137 return FALSE;
139 break;
141 case DLL_PROCESS_DETACH:
142 if (reserved) break;
143 DeleteObject( PSDRV_DefaultFont );
144 HeapDestroy( PSDRV_Heap );
145 break;
148 return TRUE;
151 static void dump_fields(DWORD fields)
153 int add_space = 0;
155 #define CHECK_FIELD(flag) \
156 do \
158 if (fields & flag) \
160 if (add_space++) DPRINTF(" "); \
161 TRACE(#flag); \
162 fields &= ~flag; \
165 while (0)
167 CHECK_FIELD(DM_ORIENTATION);
168 CHECK_FIELD(DM_PAPERSIZE);
169 CHECK_FIELD(DM_PAPERLENGTH);
170 CHECK_FIELD(DM_PAPERWIDTH);
171 CHECK_FIELD(DM_SCALE);
172 CHECK_FIELD(DM_POSITION);
173 CHECK_FIELD(DM_NUP);
174 CHECK_FIELD(DM_DISPLAYORIENTATION);
175 CHECK_FIELD(DM_COPIES);
176 CHECK_FIELD(DM_DEFAULTSOURCE);
177 CHECK_FIELD(DM_PRINTQUALITY);
178 CHECK_FIELD(DM_COLOR);
179 CHECK_FIELD(DM_DUPLEX);
180 CHECK_FIELD(DM_YRESOLUTION);
181 CHECK_FIELD(DM_TTOPTION);
182 CHECK_FIELD(DM_COLLATE);
183 CHECK_FIELD(DM_FORMNAME);
184 CHECK_FIELD(DM_LOGPIXELS);
185 CHECK_FIELD(DM_BITSPERPEL);
186 CHECK_FIELD(DM_PELSWIDTH);
187 CHECK_FIELD(DM_PELSHEIGHT);
188 CHECK_FIELD(DM_DISPLAYFLAGS);
189 CHECK_FIELD(DM_DISPLAYFREQUENCY);
190 CHECK_FIELD(DM_ICMMETHOD);
191 CHECK_FIELD(DM_ICMINTENT);
192 CHECK_FIELD(DM_MEDIATYPE);
193 CHECK_FIELD(DM_DITHERTYPE);
194 CHECK_FIELD(DM_PANNINGWIDTH);
195 CHECK_FIELD(DM_PANNINGHEIGHT);
196 if (fields) TRACE(" %#x", fields);
197 TRACE("\n");
198 #undef CHECK_FIELD
201 /* Dump DEVMODE structure without a device specific part.
202 * Some applications and drivers fail to specify correct field
203 * flags (like DM_FORMNAME), so dump everything.
205 static void dump_devmode(const DEVMODEW *dm)
207 if (!TRACE_ON(psdrv)) return;
209 TRACE("dmDeviceName: %s\n", debugstr_w(dm->dmDeviceName));
210 TRACE("dmSpecVersion: 0x%04x\n", dm->dmSpecVersion);
211 TRACE("dmDriverVersion: 0x%04x\n", dm->dmDriverVersion);
212 TRACE("dmSize: 0x%04x\n", dm->dmSize);
213 TRACE("dmDriverExtra: 0x%04x\n", dm->dmDriverExtra);
214 TRACE("dmFields: 0x%04x\n", dm->dmFields);
215 dump_fields(dm->dmFields);
216 TRACE("dmOrientation: %d\n", dm->u1.s1.dmOrientation);
217 TRACE("dmPaperSize: %d\n", dm->u1.s1.dmPaperSize);
218 TRACE("dmPaperLength: %d\n", dm->u1.s1.dmPaperLength);
219 TRACE("dmPaperWidth: %d\n", dm->u1.s1.dmPaperWidth);
220 TRACE("dmScale: %d\n", dm->u1.s1.dmScale);
221 TRACE("dmCopies: %d\n", dm->u1.s1.dmCopies);
222 TRACE("dmDefaultSource: %d\n", dm->u1.s1.dmDefaultSource);
223 TRACE("dmPrintQuality: %d\n", dm->u1.s1.dmPrintQuality);
224 TRACE("dmColor: %d\n", dm->dmColor);
225 TRACE("dmDuplex: %d\n", dm->dmDuplex);
226 TRACE("dmYResolution: %d\n", dm->dmYResolution);
227 TRACE("dmTTOption: %d\n", dm->dmTTOption);
228 TRACE("dmCollate: %d\n", dm->dmCollate);
229 TRACE("dmFormName: %s\n", debugstr_w(dm->dmFormName));
230 TRACE("dmLogPixels %u\n", dm->dmLogPixels);
231 TRACE("dmBitsPerPel %u\n", dm->dmBitsPerPel);
232 TRACE("dmPelsWidth %u\n", dm->dmPelsWidth);
233 TRACE("dmPelsHeight %u\n", dm->dmPelsHeight);
236 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
238 PAGESIZE *page;
239 RESOLUTION *res;
240 INT width = 0, height = 0, resx = 0, resy = 0;
242 dump_devmode(&physDev->Devmode->dmPublic);
244 if (physDev->Devmode->dmPublic.dmFields & (DM_PRINTQUALITY | DM_YRESOLUTION | DM_LOGPIXELS))
246 if (physDev->Devmode->dmPublic.dmFields & DM_PRINTQUALITY)
247 resx = resy = physDev->Devmode->dmPublic.u1.s1.dmPrintQuality;
249 if (physDev->Devmode->dmPublic.dmFields & DM_YRESOLUTION)
250 resy = physDev->Devmode->dmPublic.dmYResolution;
252 if (physDev->Devmode->dmPublic.dmFields & DM_LOGPIXELS)
253 resx = resy = physDev->Devmode->dmPublic.dmLogPixels;
255 LIST_FOR_EACH_ENTRY(res, &physDev->pi->ppd->Resolutions, RESOLUTION, entry)
257 if (res->resx == resx && res->resy == resy)
259 physDev->logPixelsX = resx;
260 physDev->logPixelsY = resy;
261 break;
265 if (&res->entry == &physDev->pi->ppd->Resolutions)
267 WARN("Requested resolution %dx%d is not supported by device\n", resx, resy);
268 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
269 physDev->logPixelsY = physDev->logPixelsX;
272 else
274 WARN("Using default device resolution %d\n", physDev->pi->ppd->DefaultResolution);
275 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
276 physDev->logPixelsY = physDev->logPixelsX;
279 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
280 LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
281 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
282 break;
285 if(&page->entry == &physDev->pi->ppd->PageSizes) {
286 FIXME("Can't find page\n");
287 physDev->ImageableArea.left = 0;
288 physDev->ImageableArea.right = 0;
289 physDev->ImageableArea.bottom = 0;
290 physDev->ImageableArea.top = 0;
291 physDev->PageSize.cx = 0;
292 physDev->PageSize.cy = 0;
293 } else if(page->ImageableArea) {
294 /* physDev sizes in device units; ppd sizes in 1/72" */
295 physDev->ImageableArea.left = page->ImageableArea->llx *
296 physDev->logPixelsX / 72;
297 physDev->ImageableArea.right = page->ImageableArea->urx *
298 physDev->logPixelsX / 72;
299 physDev->ImageableArea.bottom = page->ImageableArea->lly *
300 physDev->logPixelsY / 72;
301 physDev->ImageableArea.top = page->ImageableArea->ury *
302 physDev->logPixelsY / 72;
303 physDev->PageSize.cx = page->PaperDimension->x *
304 physDev->logPixelsX / 72;
305 physDev->PageSize.cy = page->PaperDimension->y *
306 physDev->logPixelsY / 72;
307 } else {
308 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
309 physDev->ImageableArea.right = physDev->PageSize.cx =
310 page->PaperDimension->x * physDev->logPixelsX / 72;
311 physDev->ImageableArea.top = physDev->PageSize.cy =
312 page->PaperDimension->y * physDev->logPixelsY / 72;
314 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
315 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
316 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
317 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
318 physDev->ImageableArea.right = physDev->PageSize.cx =
319 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
320 physDev->logPixelsX / 254;
321 physDev->ImageableArea.top = physDev->PageSize.cy =
322 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
323 physDev->logPixelsY / 254;
324 } else {
325 FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
326 physDev->ImageableArea.left = 0;
327 physDev->ImageableArea.right = 0;
328 physDev->ImageableArea.bottom = 0;
329 physDev->ImageableArea.top = 0;
330 physDev->PageSize.cx = 0;
331 physDev->PageSize.cy = 0;
334 TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %dx%d\n",
335 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
336 physDev->ImageableArea.right, physDev->ImageableArea.top,
337 physDev->PageSize.cx, physDev->PageSize.cy);
339 /* these are in device units */
340 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
341 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
343 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
344 physDev->horzRes = width;
345 physDev->vertRes = height;
346 } else {
347 physDev->horzRes = height;
348 physDev->vertRes = width;
351 /* these are in mm */
352 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
353 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
355 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
356 "horzRes = %d, vertRes = %d\n",
357 physDev->horzSize, physDev->vertSize,
358 physDev->horzRes, physDev->vertRes);
361 static PSDRV_PDEVICE *create_psdrv_physdev( PRINTERINFO *pi )
363 PSDRV_PDEVICE *physDev;
365 physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) );
366 if (!physDev) return NULL;
368 physDev->Devmode = HeapAlloc( GetProcessHeap(), 0, sizeof(PSDRV_DEVMODE) );
369 if (!physDev->Devmode)
371 HeapFree( GetProcessHeap(), 0, physDev );
372 return NULL;
375 *physDev->Devmode = *pi->Devmode;
376 physDev->pi = pi;
377 physDev->logPixelsX = pi->ppd->DefaultResolution;
378 physDev->logPixelsY = pi->ppd->DefaultResolution;
379 return physDev;
382 /**********************************************************************
383 * PSDRV_CreateDC
385 static BOOL PSDRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
386 LPCWSTR output, const DEVMODEW* initData )
388 PSDRV_PDEVICE *physDev;
389 PRINTERINFO *pi;
391 TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
392 debugstr_w(output), initData);
394 if (!device) return FALSE;
395 pi = PSDRV_FindPrinterInfo( device );
396 if(!pi) return FALSE;
398 if(!pi->Fonts) {
399 RASTERIZER_STATUS status;
400 if(!GetRasterizerCaps(&status, sizeof(status)) ||
401 !(status.wFlags & TT_AVAILABLE) ||
402 !(status.wFlags & TT_ENABLED)) {
403 MESSAGE("Disabling printer %s since it has no builtin fonts and there are no TrueType fonts available.\n",
404 debugstr_w(device));
405 return FALSE;
409 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
411 if (output && *output) physDev->job.output = strdupW( output );
413 if(initData)
415 dump_devmode(initData);
416 PSDRV_MergeDevmodes(physDev->Devmode, (const PSDRV_DEVMODE *)initData, pi);
419 PSDRV_UpdateDevCaps(physDev);
420 SelectObject( (*pdev)->hdc, PSDRV_DefaultFont );
421 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
422 return TRUE;
426 /**********************************************************************
427 * PSDRV_CreateCompatibleDC
429 static BOOL PSDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
431 HDC hdc = (*pdev)->hdc;
432 PSDRV_PDEVICE *physDev, *orig_dev = get_psdrv_dev( orig );
433 PRINTERINFO *pi = PSDRV_FindPrinterInfo( orig_dev->pi->friendly_name );
435 if (!pi) return FALSE;
436 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
437 PSDRV_MergeDevmodes( physDev->Devmode, orig_dev->Devmode, pi );
438 PSDRV_UpdateDevCaps(physDev);
439 SelectObject( hdc, PSDRV_DefaultFont );
440 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
441 return TRUE;
446 /**********************************************************************
447 * PSDRV_DeleteDC
449 static BOOL PSDRV_DeleteDC( PHYSDEV dev )
451 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
453 TRACE("\n");
455 HeapFree( GetProcessHeap(), 0, physDev->Devmode );
456 HeapFree( GetProcessHeap(), 0, physDev->job.output );
457 HeapFree( GetProcessHeap(), 0, physDev );
459 return TRUE;
463 /**********************************************************************
464 * ResetDC (WINEPS.@)
466 static HDC PSDRV_ResetDC( PHYSDEV dev, const DEVMODEW *lpInitData )
468 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
470 if (lpInitData)
472 PSDRV_MergeDevmodes(physDev->Devmode, (const PSDRV_DEVMODE *)lpInitData, physDev->pi);
473 PSDRV_UpdateDevCaps(physDev);
475 return dev->hdc;
478 /***********************************************************************
479 * GetDeviceCaps (WINEPS.@)
481 static INT PSDRV_GetDeviceCaps( PHYSDEV dev, INT cap )
483 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
485 TRACE("%p,%d\n", dev->hdc, cap);
487 switch(cap)
489 case DRIVERVERSION:
490 return 0;
491 case TECHNOLOGY:
492 return DT_RASPRINTER;
493 case HORZSIZE:
494 return MulDiv(physDev->horzSize, 100,
495 physDev->Devmode->dmPublic.u1.s1.dmScale);
496 case VERTSIZE:
497 return MulDiv(physDev->vertSize, 100,
498 physDev->Devmode->dmPublic.u1.s1.dmScale);
499 case HORZRES:
500 case DESKTOPHORZRES:
501 return physDev->horzRes;
502 case VERTRES:
503 case DESKTOPVERTRES:
504 return physDev->vertRes;
505 case BITSPIXEL:
506 /* Although Windows returns 1 for monochrome printers, we want
507 CreateCompatibleBitmap to provide something other than 1 bpp */
508 return 32;
509 case PLANES:
510 return 1;
511 case NUMBRUSHES:
512 return -1;
513 case NUMPENS:
514 return 10;
515 case NUMMARKERS:
516 return 0;
517 case NUMFONTS:
518 return 39;
519 case NUMCOLORS:
520 return -1;
521 case PDEVICESIZE:
522 return sizeof(PSDRV_PDEVICE);
523 case CURVECAPS:
524 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
525 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
526 case LINECAPS:
527 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
528 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
529 case POLYGONALCAPS:
530 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
531 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
532 case TEXTCAPS:
533 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
534 case CLIPCAPS:
535 return CP_RECTANGLE;
536 case RASTERCAPS:
537 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
538 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
539 case ASPECTX:
540 return physDev->logPixelsX;
541 case ASPECTY:
542 return physDev->logPixelsY;
543 case ASPECTXY:
544 return (int)hypot( (double)physDev->logPixelsX,
545 (double)physDev->logPixelsY );
546 case LOGPIXELSX:
547 return MulDiv(physDev->logPixelsX,
548 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
549 case LOGPIXELSY:
550 return MulDiv(physDev->logPixelsY,
551 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
552 case SIZEPALETTE:
553 return 0;
554 case NUMRESERVED:
555 return 0;
556 case COLORRES:
557 return 0;
558 case PHYSICALWIDTH:
559 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
560 physDev->PageSize.cy : physDev->PageSize.cx;
561 case PHYSICALHEIGHT:
562 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
563 physDev->PageSize.cx : physDev->PageSize.cy;
564 case PHYSICALOFFSETX:
565 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
566 if(physDev->pi->ppd->LandscapeOrientation == -90)
567 return physDev->PageSize.cy - physDev->ImageableArea.top;
568 else
569 return physDev->ImageableArea.bottom;
571 return physDev->ImageableArea.left;
573 case PHYSICALOFFSETY:
574 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
575 if(physDev->pi->ppd->LandscapeOrientation == -90)
576 return physDev->PageSize.cx - physDev->ImageableArea.right;
577 else
578 return physDev->ImageableArea.left;
580 return physDev->PageSize.cy - physDev->ImageableArea.top;
582 case SCALINGFACTORX:
583 case SCALINGFACTORY:
584 case VREFRESH:
585 case BLTALIGNMENT:
586 return 0;
587 case SHADEBLENDCAPS:
588 return SB_NONE;
589 default:
590 FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
591 return 0;
595 static PRINTER_ENUM_VALUESA *load_font_sub_table( HANDLE printer, DWORD *num_entries )
597 DWORD res, needed, num;
598 PRINTER_ENUM_VALUESA *table = NULL;
599 static const char fontsubkey[] = "PrinterDriverData\\FontSubTable";
601 *num_entries = 0;
603 res = EnumPrinterDataExA( printer, fontsubkey, NULL, 0, &needed, &num );
604 if (res != ERROR_MORE_DATA) return NULL;
606 table = HeapAlloc( PSDRV_Heap, 0, needed );
607 if (!table) return NULL;
609 res = EnumPrinterDataExA( printer, fontsubkey, (LPBYTE)table, needed, &needed, &num );
610 if (res != ERROR_SUCCESS)
612 HeapFree( PSDRV_Heap, 0, table );
613 return NULL;
616 *num_entries = num;
617 return table;
620 static PSDRV_DEVMODE *get_printer_devmode( HANDLE printer )
622 DWORD needed, dm_size;
623 BOOL res;
624 PRINTER_INFO_9W *info;
625 PSDRV_DEVMODE *dm;
627 GetPrinterW( printer, 9, NULL, 0, &needed );
628 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
630 info = HeapAlloc( PSDRV_Heap, 0, needed );
631 res = GetPrinterW( printer, 9, (BYTE *)info, needed, &needed );
632 if (!res || !info->pDevMode)
634 HeapFree( PSDRV_Heap, 0, info );
635 return NULL;
638 /* sanity check the sizes */
639 dm_size = info->pDevMode->dmSize + info->pDevMode->dmDriverExtra;
640 if ((char *)info->pDevMode - (char *)info + dm_size > needed)
642 HeapFree( PSDRV_Heap, 0, info );
643 return NULL;
646 dm = (PSDRV_DEVMODE*)info;
647 memmove( dm, info->pDevMode, dm_size );
648 return dm;
651 static PSDRV_DEVMODE *get_devmode( HANDLE printer, const WCHAR *name, BOOL *is_default )
653 PSDRV_DEVMODE *dm = get_printer_devmode( printer );
655 *is_default = FALSE;
657 if (dm && dm->dmPublic.dmSize + dm->dmPublic.dmDriverExtra >= sizeof(DefaultDevmode))
659 TRACE( "Retrieved devmode from winspool\n" );
660 return dm;
662 HeapFree( PSDRV_Heap, 0, dm );
664 TRACE( "Using default devmode\n" );
665 dm = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
666 if (dm)
668 *dm = DefaultDevmode;
669 lstrcpynW( (WCHAR *)dm->dmPublic.dmDeviceName, name, CCHDEVICENAME );
670 *is_default = TRUE;
672 return dm;
675 static BOOL set_devmode( HANDLE printer, PSDRV_DEVMODE *dm )
677 PRINTER_INFO_9W info;
678 info.pDevMode = &dm->dmPublic;
680 return SetPrinterW( printer, 9, (BYTE *)&info, 0 );
683 static WCHAR *get_ppd_filename( HANDLE printer )
685 DWORD needed;
686 DRIVER_INFO_2W *info;
687 WCHAR *name;
689 GetPrinterDriverW( printer, NULL, 2, NULL, 0, &needed );
690 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
691 info = HeapAlloc( GetProcessHeap(), 0, needed );
692 if (!info) return NULL;
693 GetPrinterDriverW( printer, NULL, 2, (BYTE*)info, needed, &needed );
694 name = (WCHAR *)info;
695 memmove( name, info->pDataFile, (strlenW( info->pDataFile ) + 1) * sizeof(WCHAR) );
696 return name;
699 static struct list printer_list = LIST_INIT( printer_list );
701 /**********************************************************************
702 * PSDRV_FindPrinterInfo
704 PRINTERINFO *PSDRV_FindPrinterInfo(LPCWSTR name)
706 PRINTERINFO *pi;
707 FONTNAME *font;
708 const AFM *afm;
709 HANDLE hPrinter = 0;
710 WCHAR *ppd_filename = NULL;
711 char *nameA = NULL;
712 BOOL using_default_devmode = FALSE;
713 int len;
715 TRACE("'%s'\n", debugstr_w(name));
717 LIST_FOR_EACH_ENTRY( pi, &printer_list, PRINTERINFO, entry )
719 if (!strcmpW( pi->friendly_name, name ))
720 return pi;
723 pi = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
724 if (pi == NULL) return NULL;
726 if (!(pi->friendly_name = HeapAlloc( PSDRV_Heap, 0, (strlenW(name)+1)*sizeof(WCHAR) ))) goto fail;
727 strcpyW( pi->friendly_name, name );
729 if (OpenPrinterW( pi->friendly_name, &hPrinter, NULL ) == 0) {
730 ERR ("OpenPrinter failed with code %i\n", GetLastError ());
731 goto fail;
734 len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
735 nameA = HeapAlloc( GetProcessHeap(), 0, len );
736 WideCharToMultiByte( CP_ACP, 0, name, -1, nameA, len, NULL, NULL );
738 pi->Devmode = get_devmode( hPrinter, name, &using_default_devmode );
739 if (!pi->Devmode) goto fail;
741 ppd_filename = get_ppd_filename( hPrinter );
742 if (!ppd_filename) goto fail;
744 pi->ppd = PSDRV_ParsePPD( ppd_filename, hPrinter );
745 if (!pi->ppd)
747 WARN( "Couldn't parse PPD file %s\n", debugstr_w(ppd_filename) );
748 goto fail;
751 if(using_default_devmode) {
752 DWORD papersize;
754 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
755 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
756 PSDRV_DEVMODE dm;
757 memset(&dm, 0, sizeof(dm));
758 dm.dmPublic.dmFields = DM_PAPERSIZE;
759 dm.dmPublic.u1.s1.dmPaperSize = papersize;
760 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
764 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
765 PSDRV_DEVMODE dm;
766 memset(&dm, 0, sizeof(dm));
767 dm.dmPublic.dmFields = DM_PAPERSIZE;
768 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
769 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
772 /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
773 WinDuplex == 0 is a special case which means that the ppd has a
774 *DefaultDuplex: NotCapable entry. In this case we'll try not to confuse
775 apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
776 PSDRV_WriteHeader understands this and copes. */
777 pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
778 if(pi->ppd->DefaultDuplex) {
779 pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
780 if(pi->Devmode->dmPublic.dmDuplex != 0)
781 pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
782 else
783 pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
786 set_devmode( hPrinter, pi->Devmode );
788 pi->FontSubTable = load_font_sub_table( hPrinter, &pi->FontSubTableSize );
790 LIST_FOR_EACH_ENTRY( font, &pi->ppd->InstalledFonts, FONTNAME, entry )
792 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
793 if(!afm) {
794 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
795 "ignoring\n", font->Name);
797 else {
798 BOOL added;
799 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
800 PSDRV_FreeAFMList(pi->Fonts);
801 goto fail;
806 ClosePrinter( hPrinter );
807 HeapFree( GetProcessHeap(), 0, nameA );
808 HeapFree( GetProcessHeap(), 0, ppd_filename );
809 list_add_head( &printer_list, &pi->entry );
810 return pi;
812 fail:
813 if (hPrinter) ClosePrinter( hPrinter );
814 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
815 HeapFree(PSDRV_Heap, 0, pi->friendly_name);
816 HeapFree(PSDRV_Heap, 0, pi->Devmode);
817 HeapFree(PSDRV_Heap, 0, pi);
818 HeapFree( GetProcessHeap(), 0, nameA );
819 HeapFree( GetProcessHeap(), 0, ppd_filename );
820 return NULL;
824 static const struct gdi_dc_funcs psdrv_funcs =
826 NULL, /* pAbortDoc */
827 NULL, /* pAbortPath */
828 NULL, /* pAlphaBlend */
829 NULL, /* pAngleArc */
830 PSDRV_Arc, /* pArc */
831 NULL, /* pArcTo */
832 NULL, /* pBeginPath */
833 NULL, /* pBlendImage */
834 PSDRV_Chord, /* pChord */
835 NULL, /* pCloseFigure */
836 PSDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
837 PSDRV_CreateDC, /* pCreateDC */
838 PSDRV_DeleteDC, /* pDeleteDC */
839 NULL, /* pDeleteObject */
840 PSDRV_DeviceCapabilities, /* pDeviceCapabilities */
841 PSDRV_Ellipse, /* pEllipse */
842 PSDRV_EndDoc, /* pEndDoc */
843 PSDRV_EndPage, /* pEndPage */
844 NULL, /* pEndPath */
845 PSDRV_EnumFonts, /* pEnumFonts */
846 NULL, /* pEnumICMProfiles */
847 NULL, /* pExcludeClipRect */
848 PSDRV_ExtDeviceMode, /* pExtDeviceMode */
849 PSDRV_ExtEscape, /* pExtEscape */
850 NULL, /* pExtFloodFill */
851 NULL, /* pExtSelectClipRgn */
852 PSDRV_ExtTextOut, /* pExtTextOut */
853 PSDRV_FillPath, /* pFillPath */
854 NULL, /* pFillRgn */
855 NULL, /* pFlattenPath */
856 NULL, /* pFontIsLinked */
857 NULL, /* pFrameRgn */
858 NULL, /* pGdiComment */
859 NULL, /* pGdiRealizationInfo */
860 NULL, /* pGetBoundsRect */
861 NULL, /* pGetCharABCWidths */
862 NULL, /* pGetCharABCWidthsI */
863 PSDRV_GetCharWidth, /* pGetCharWidth */
864 PSDRV_GetDeviceCaps, /* pGetDeviceCaps */
865 NULL, /* pGetDeviceGammaRamp */
866 NULL, /* pGetFontData */
867 NULL, /* pGetFontUnicodeRanges */
868 NULL, /* pGetGlyphIndices */
869 NULL, /* pGetGlyphOutline */
870 NULL, /* pGetICMProfile */
871 NULL, /* pGetImage */
872 NULL, /* pGetKerningPairs */
873 NULL, /* pGetNearestColor */
874 NULL, /* pGetOutlineTextMetrics */
875 NULL, /* pGetPixel */
876 NULL, /* pGetSystemPaletteEntries */
877 NULL, /* pGetTextCharsetInfo */
878 PSDRV_GetTextExtentExPoint, /* pGetTextExtentExPoint */
879 NULL, /* pGetTextExtentExPointI */
880 NULL, /* pGetTextFace */
881 PSDRV_GetTextMetrics, /* pGetTextMetrics */
882 NULL, /* pGradientFill */
883 NULL, /* pIntersectClipRect */
884 NULL, /* pInvertRgn */
885 PSDRV_LineTo, /* pLineTo */
886 NULL, /* pModifyWorldTransform */
887 NULL, /* pMoveTo */
888 NULL, /* pOffsetClipRgn */
889 NULL, /* pOffsetViewportOrg */
890 NULL, /* pOffsetWindowOrg */
891 PSDRV_PaintRgn, /* pPaintRgn */
892 PSDRV_PatBlt, /* pPatBlt */
893 PSDRV_Pie, /* pPie */
894 PSDRV_PolyBezier, /* pPolyBezier */
895 PSDRV_PolyBezierTo, /* pPolyBezierTo */
896 NULL, /* pPolyDraw */
897 PSDRV_PolyPolygon, /* pPolyPolygon */
898 PSDRV_PolyPolyline, /* pPolyPolyline */
899 NULL, /* pPolygon */
900 NULL, /* pPolyline */
901 NULL, /* pPolylineTo */
902 PSDRV_PutImage, /* pPutImage */
903 NULL, /* pRealizeDefaultPalette */
904 NULL, /* pRealizePalette */
905 PSDRV_Rectangle, /* pRectangle */
906 PSDRV_ResetDC, /* pResetDC */
907 NULL, /* pRestoreDC */
908 PSDRV_RoundRect, /* pRoundRect */
909 NULL, /* pSaveDC */
910 NULL, /* pScaleViewportExt */
911 NULL, /* pScaleWindowExt */
912 NULL, /* pSelectBitmap */
913 PSDRV_SelectBrush, /* pSelectBrush */
914 NULL, /* pSelectClipPath */
915 PSDRV_SelectFont, /* pSelectFont */
916 NULL, /* pSelectPalette */
917 PSDRV_SelectPen, /* pSelectPen */
918 NULL, /* pSetArcDirection */
919 PSDRV_SetBkColor, /* pSetBkColor */
920 NULL, /* pSetBkMode */
921 NULL, /* pSetBoundsRect */
922 PSDRV_SetDCBrushColor, /* pSetDCBrushColor */
923 PSDRV_SetDCPenColor, /* pSetDCPenColor */
924 NULL, /* pSetDIBitsToDevice */
925 NULL, /* pSetDeviceClipping */
926 NULL, /* pSetDeviceGammaRamp */
927 NULL, /* pSetLayout */
928 NULL, /* pSetMapMode */
929 NULL, /* pSetMapperFlags */
930 PSDRV_SetPixel, /* pSetPixel */
931 NULL, /* pSetPolyFillMode */
932 NULL, /* pSetROP2 */
933 NULL, /* pSetRelAbs */
934 NULL, /* pSetStretchBltMode */
935 NULL, /* pSetTextAlign */
936 NULL, /* pSetTextCharacterExtra */
937 PSDRV_SetTextColor, /* pSetTextColor */
938 NULL, /* pSetTextJustification */
939 NULL, /* pSetViewportExt */
940 NULL, /* pSetViewportOrg */
941 NULL, /* pSetWindowExt */
942 NULL, /* pSetWindowOrg */
943 NULL, /* pSetWorldTransform */
944 PSDRV_StartDoc, /* pStartDoc */
945 PSDRV_StartPage, /* pStartPage */
946 NULL, /* pStretchBlt */
947 NULL, /* pStretchDIBits */
948 PSDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
949 PSDRV_StrokePath, /* pStrokePath */
950 NULL, /* pUnrealizePalette */
951 NULL, /* pWidenPath */
952 NULL, /* wine_get_wgl_driver */
953 GDI_PRIORITY_GRAPHICS_DRV /* priority */
957 /******************************************************************************
958 * PSDRV_get_gdi_driver
960 const struct gdi_dc_funcs * CDECL PSDRV_get_gdi_driver( unsigned int version )
962 if (version != WINE_GDI_DRIVER_VERSION)
964 ERR( "version mismatch, gdi32 wants %u but wineps has %u\n", version, WINE_GDI_DRIVER_VERSION );
965 return NULL;
967 return &psdrv_funcs;