cvs: pecl /printer php_printer.h printer.c
| From: | Frank M. Kromann | Date: | Mon, 02 Aug 2004 20:31:24 +0000 |
| Subject: | cvs: pecl /printer php_printer.h printer.c | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-1618@lists.php.net to get a copy of this message | ||
fmk Mon Aug 2 16:31:24 2004 EDT
Modified files:
/pecl/printer php_printer.h printer.c
Log:
Allow stretching of bitmaps by specifying width and height.
Patch by Philippe MAES<luckyluke@dlfp.org>
http://cvs.php.net/diff.php/pecl/printer/php_printer.h?r1=1.15&r2=1.16&ty=u Index: pecl/printer/php_printer.h diff -u pecl/printer/php_printer.h:1.15 pecl/printer/php_printer.h:1.16 --- pecl/printer/php_printer.h:1.15 Sat Jul 31 13:58:57 2004 +++ pecl/printer/php_printer.h Mon Aug 2 16:31:23 2004 @@ -14,10 +14,12 @@ +----------------------------------------------------------------------+ | Authors: Frank M. Kromann <frank@kromann.info> | | Daniel Beulshausen <daniel@php4win.de> | + | Contribution: | + | Philippe MAES <luckyluke@dlfp.org> | +----------------------------------------------------------------------+ */ -/* $Id: php_printer.h,v 1.15 2004/07/31 17:58:57 fmk Exp $ */ +/* $Id: php_printer.h,v 1.16 2004/08/02 20:31:23 fmk Exp $ */ #ifndef PHP_PRINTER_H #define PHP_PRINTER_H @@ -66,9 +68,10 @@ typedef struct { HANDLE handle; LPTSTR name; - LPDEVMODE device; DOCINFO info; HDC dc; + PRINTER_INFO_2 *pi2; + DWORD dmModifiedFields; } printer; ZEND_BEGIN_MODULE_GLOBALS(printer) http://cvs.php.net/diff.php/pecl/printer/printer.c?r1=1.29&r2=1.30&ty=u Index: pecl/printer/printer.c diff -u pecl/printer/printer.c:1.29 pecl/printer/printer.c:1.30 --- pecl/printer/printer.c:1.29 Sat Jul 31 13:58:57 2004 +++ pecl/printer/printer.c Mon Aug 2 16:31:23 2004 @@ -14,10 +14,12 @@ +----------------------------------------------------------------------+ | Authors: Frank M. Kromann <frank@kromann.info> | | Daniel Beulshausen <daniel@php4win.de> | + | Contribution: | + | Philippe MAES <luckyluke@dlfp.org> | +----------------------------------------------------------------------+ */ -/* $Id: printer.c,v 1.29 2004/07/31 17:58:57 fmk Exp $ */ +/* $Id: printer.c,v 1.30 2004/08/02 20:31:23 fmk Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -31,8 +33,6 @@ #ifdef HAVE_PRINTER -#include "php_printer.h" - static int le_printer, le_brush, le_pen, le_font; #ifdef PHP_WIN32 @@ -40,6 +40,8 @@ #include <wingdi.h> #include <winspool.h> +#include "php_printer.h" + COLORREF hex_to_rgb(char * hex); char *rgb_to_hex(COLORREF rgb); static void printer_close(zend_rsrc_list_entry *resource TSRMLS_DC); @@ -107,7 +109,7 @@ php_info_print_table_row(2, "Printer Support", "enabled"); php_info_print_table_row(2, "Default printing device", PRINTERG(default_printer) ? PRINTERG(default_printer) : "<b>not detected</b>"); php_info_print_table_row(2, "Module state", "working"); - php_info_print_table_row(2, "RCS Version", "$Id: printer.c,v 1.29 2004/07/31 17:58:57 fmk Exp $"); + php_info_print_table_row(2, "RCS Version", "$Id: printer.c,v 1.30 2004/08/02 20:31:23 fmk Exp $"); php_info_print_table_end(); DISPLAY_INI_ENTRIES(); } @@ -143,6 +145,7 @@ #define DEVICENAME 13 #define DRIVER_VERSION 14 #define OUTPUT_FILE 15 +#define VALID_OPTIONS 16 #define BRUSH_SOLID -1 #define BRUSH_CUSTOM -2 @@ -256,9 +259,11 @@ { pval **arg1; printer *resource; + int argc = ZEND_NUM_ARGS(); resource = (printer *)emalloc(sizeof(printer)); + resource->dmModifiedFields = 0; if( argc == 1 && zend_get_parameters_ex(1, &arg1) != FAILURE ) { convert_to_string_ex(arg1); @@ -271,21 +276,21 @@ WRONG_PARAM_COUNT; } - - if( OpenPrinter(resource->name, &resource->handle, NULL) != 0 ) { - resource->device = emalloc(DocumentProperties(NULL, NULL, resource->name, NULL, NULL, 0)); - DocumentProperties(NULL, resource->handle, resource->name, resource->device, NULL, DM_OUT_BUFFER); - - resource->info.lpszDocName = estrdup("PHP generated Document"); - resource->info.lpszOutput = NULL; - resource->info.lpszDatatype = estrdup("TEXT"); - resource->info.fwType = 0; - resource->info.cbSize = sizeof(resource->info); - resource->dc = CreateDC(NULL, resource->name, NULL, resource->device); - ZEND_REGISTER_RESOURCE(return_value, resource, le_printer); + if (OpenPrinter(resource->name, &resource->handle, NULL) != 0) { + resource->pi2 = (PRINTER_INFO_2 *)emalloc(sizeof(PRINTER_INFO_2)); + resource->pi2->pDevMode = (DEVMODE *)emalloc(DocumentProperties(NULL, NULL, resource->name, NULL, NULL, 0)); + if (DocumentProperties(NULL, resource->handle, resource->name, resource->pi2->pDevMode, NULL, DM_OUT_BUFFER) == IDOK) { + resource->info.lpszDocName = estrdup("PHP generated Document"); + resource->info.lpszOutput = NULL; + resource->info.lpszDatatype = estrdup("TEXT"); + resource->info.fwType = 0; + resource->info.cbSize = sizeof(resource->info); + resource->dc = CreateDC(NULL, resource->name, NULL, resource->pi2->pDevMode); + ZEND_REGISTER_RESOURCE(return_value, resource, le_printer); + } } else { - php_error(E_WARNING,"couldn't connect to the printer [%s]", resource->name); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't connect to the printer [%s]", resource->name); RETURN_FALSE; } } @@ -341,7 +346,7 @@ RETURN_TRUE; } else { - php_error(E_WARNING,"couldn't allocate the printerjob [%d]", GetLastError()); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't allocate the printerjob [%d]", GetLastError()); RETURN_FALSE; } } @@ -372,7 +377,7 @@ convert_to_long_ex(arg3); Level = (int)Z_LVAL_PP(arg3); if(!LevvelsAllowed[Level]) { - php_error(E_WARNING, "Level not allowed"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Level not allowed"); RETURN_FALSE; } case 2: @@ -464,6 +469,8 @@ Configure the printer device */ PHP_FUNCTION(printer_set_option) { + DWORD dwNeeded =0; + PRINTER_DEFAULTS pd; pval **arg1, **arg2, **arg3; printer *resource; @@ -474,10 +481,15 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); convert_to_long_ex(arg2); + memset(&pd,0,sizeof(pd)); + pd.DesiredAccess = PRINTER_ALL_ACCESS; + SetLastError(0); + switch(Z_LVAL_PP(arg2)) { case COPIES: convert_to_long_ex(arg3); - resource->device->dmCopies = (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmCopies=(short)Z_LVAL_PP(arg3); + resource->dmModifiedFields|=DM_COPIES; break; case MODE: @@ -509,37 +521,44 @@ case ORIENTATION: convert_to_long_ex(arg3); - resource->device->dmOrientation = (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmOrientation=(short)Z_LVAL_PP(arg3); + resource->dmModifiedFields|=DM_ORIENTATION; break; case YRESOLUTION: convert_to_long_ex(arg3); - resource->device->dmYResolution = (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmYResolution = (short)Z_LVAL_PP(arg3); + resource->dmModifiedFields|=DM_YRESOLUTION; break; case XRESOLUTION: convert_to_long_ex(arg3); - resource->device->dmPrintQuality= (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmPrintQuality= (short)Z_LVAL_PP(arg3); + resource->dmModifiedFields|=DM_PRINTQUALITY; break; case PAPER_FORMAT: convert_to_long_ex(arg3); - resource->device->dmPaperSize = (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmPaperSize = (short)Z_LVAL_PP(arg3); + resource->dmModifiedFields|=DM_PAPERSIZE; break; case PAPER_LENGTH: convert_to_long_ex(arg3); - resource->device->dmPaperLength = Z_LVAL_PP(arg3) * 10; + resource->pi2->pDevMode->dmPaperLength = Z_LVAL_PP(arg3) * 10; + resource->dmModifiedFields|=DM_PAPERLENGTH; break; case PAPER_WIDTH: convert_to_long_ex(arg3); - resource->device->dmPaperWidth = Z_LVAL_PP(arg3) * 10; + resource->pi2->pDevMode->dmPaperWidth = Z_LVAL_PP(arg3) * 10; + resource->dmModifiedFields |= DM_PAPERWIDTH; break; case SCALE: convert_to_long_ex(arg3); - resource->device->dmScale = (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmScale = (short)Z_LVAL_PP(arg3); + resource->dmModifiedFields |= DM_SCALE; break; case BG_COLOR: @@ -556,9 +575,16 @@ convert_to_string_ex(arg3); SetTextAlign(resource->dc, Z_LVAL_PP(arg3)); break; - + case VALID_OPTIONS: + resource->pi2->pSecurityDescriptor=NULL; + resource->pi2->pDevMode->dmFields=resource->dmModifiedFields; + resource->dmModifiedFields=0; + DocumentProperties(NULL,resource->handle,resource->name,resource->pi2->pDevMode,resource->pi2->pDevMode,DM_IN_BUFFER | DM_OUT_BUFFER); + SetPrinter(resource->handle,2,(LPBYTE)resource->pi2,0); + SendMessageTimeout(HWND_BROADCAST,WM_DEVMODECHANGE,0L,(LPARAM)(LPCSTR)resource->name,SMTO_NORMAL,1000,NULL); + break; default: - php_error(E_WARNING,"unknown option passed to printer_set_option()"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknown option passed to printer_set_option()"); RETURN_FALSE; } @@ -584,7 +610,7 @@ switch(Z_LVAL_PP(arg2)) { case COPIES: - RETURN_LONG(resource->device->dmCopies); + RETURN_LONG(resource->pi2->pDevMode->dmCopies); case MODE: RETURN_STRING((char*)resource->info.lpszDatatype,1); @@ -601,25 +627,25 @@ } case ORIENTATION: - RETURN_LONG(resource->device->dmOrientation); + RETURN_LONG(resource->pi2->pDevMode->dmOrientation); case YRESOLUTION: - RETURN_LONG(resource->device->dmYResolution); + RETURN_LONG(resource->pi2->pDevMode->dmYResolution); case XRESOLUTION: - RETURN_LONG(resource->device->dmPrintQuality); + RETURN_LONG(resource->pi2->pDevMode->dmPrintQuality); case PAPER_FORMAT: - RETURN_LONG(resource->device->dmPaperSize); + RETURN_LONG(resource->pi2->pDevMode->dmPaperSize); case PAPER_LENGTH: - RETURN_LONG(resource->device->dmPaperLength / 10); + RETURN_LONG(resource->pi2->pDevMode->dmPaperLength / 10); case PAPER_WIDTH: - RETURN_LONG(resource->device->dmPaperWidth / 10); + RETURN_LONG(resource->pi2->pDevMode->dmPaperWidth / 10); case SCALE: - RETURN_LONG(resource->device->dmScale); + RETURN_LONG(resource->pi2->pDevMode->dmScale); case BG_COLOR: RETURN_STRING(rgb_to_hex(GetBkColor(resource->dc)), 0); @@ -634,10 +660,10 @@ RETURN_STRING(resource->name, 1); case DRIVER_VERSION: - RETURN_LONG(resource->device->dmDriverVersion); + RETURN_LONG(resource->pi2->pDevMode->dmDriverVersion); default: - php_error(E_WARNING,"unknown option passed to printer_get_option()"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknown option passed to printer_get_option()"); RETURN_FALSE; } } @@ -658,11 +684,11 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); if( resource->dc != NULL ) { - php_error(E_NOTICE, "Deleting old DeviceContext"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Deleting old DeviceContext"); DeleteDC(resource->dc); } - resource->dc = CreateDC(NULL, resource->name, NULL, resource->device); + resource->dc = CreateDC(NULL, resource->name, NULL, resource->pi2->pDevMode); } /* }}} */ @@ -686,7 +712,7 @@ RETURN_TRUE; } else { - php_error(E_WARNING,"No DeviceContext created"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "No DeviceContext created"); RETURN_FALSE; } } @@ -717,7 +743,7 @@ } if(StartDoc(resource->dc, &resource->info) < 0) { - php_error(E_WARNING, "couldn't allocate new print job"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't allocate new print job"); RETURN_FALSE; } @@ -740,7 +766,7 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); if(EndDoc(resource->dc) < 0) { - php_error(E_ERROR,"couldn't terminate print job"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't terminate print job"); RETURN_FALSE; } @@ -763,7 +789,7 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); if(StartPage(resource->dc) < 0) { - php_error(E_WARNING, "couldn't start a new page"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't start a new page"); RETURN_FALSE; } @@ -786,7 +812,7 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); if(EndPage(resource->dc) < 0) { - php_error(E_WARNING, "couldn't end the page"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't end the page"); RETURN_FALSE; } @@ -1198,19 +1224,34 @@ /* }}} */ -/* {{{ proto mixed printer_draw_bmp(resource handle, string filename, int x, int y) +/* {{{ proto mixed printer_draw_bmp(resource handle, string filename, int x, int y [, int width, int height]) Draw a bitmap */ PHP_FUNCTION(printer_draw_bmp) { - pval **arg1, **arg2, **arg3, **arg4; + pval **arg1, **arg2, **arg3, **arg4, **arg5 = NULL, **arg6 = NULL; printer *resource; - HBITMAP bmp; + HBITMAP hbmp; BITMAP bmp_property; HDC dummy; char* path; - if(zend_get_parameters_ex(4, &arg1, &arg2, &arg3, &arg4) == FAILURE ) { - WRONG_PARAM_COUNT; + int args = ZEND_NUM_ARGS(); + switch (args) { + case 4: + if (zend_get_parameters_ex(4, &arg1, &arg2, &arg3, &arg4) == FAILURE ) { + RETURN_FALSE; + } + break; + case 6: + if (zend_get_parameters_ex(6, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE ) { + RETURN_FALSE; + } + convert_to_long_ex(arg5); + convert_to_long_ex(arg6); + break; + default: + WRONG_PARAM_COUNT; + break; } ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); @@ -1220,48 +1261,56 @@ virtual_filepath(Z_STRVAL_PP(arg2), &path TSRMLS_CC); - if ((bmp = LoadImage(0, path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)) == NULL) { + hbmp = (HBITMAP)LoadImage(0, path, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_LOADFROMFILE); + + if (hbmp == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to load bitmap %s", Z_STRVAL_PP(arg2)); RETURN_FALSE; } - SelectObject(resource->dc, bmp); - - if (!(GetDeviceCaps(resource->dc, RASTERCAPS) & RC_BITBLT)) - { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Printer does not support bitmaps"); - DeleteObject(bmp); + if (GetObject(hbmp, sizeof(BITMAP), &bmp_property) == 0) { + if(hbmp) + DeleteObject(hbmp); RETURN_FALSE; } - - if((dummy = CreateCompatibleDC(NULL)) == NULL) { - DeleteObject(bmp); + + if (!(GetDeviceCaps(resource->dc, RASTERCAPS) & RC_STRETCHBLT)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Printer does not support bitmaps"); + DeleteObject(hbmp); RETURN_FALSE; } - if(SelectObject(dummy, bmp) == NULL) { - DeleteDC(dummy); - DeleteObject(bmp); + if ((dummy = CreateCompatibleDC(resource->dc)) == NULL) { + DeleteObject(hbmp); RETURN_FALSE; } - if (GetObject(bmp, sizeof(BITMAP), &bmp_property) == 0) { + if (SelectObject(dummy, hbmp) == NULL) { DeleteDC(dummy); - DeleteObject(bmp); + DeleteObject(hbmp); RETURN_FALSE; } - if (!BitBlt(resource->dc, Z_LVAL_PP(arg3), Z_LVAL_PP(arg4), bmp_property.bmWidth, bmp_property.bmHeight, dummy, 0, 0, SRCCOPY)) - { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Printer failed to accept bitmap"); - - DeleteDC(dummy); - DeleteObject(bmp); - RETURN_FALSE; + + if (args > 4) { + if (!StretchBlt(resource->dc,(int)Z_LVAL_PP(arg3), (int)Z_LVAL_PP(arg4), (int)Z_LVAL_PP(arg5), (int)Z_LVAL_PP(arg6), dummy, 0, 0, bmp_property.bmWidth,bmp_property.bmHeight, SRCCOPY)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Printer failed to accept bitmap"); + DeleteDC(dummy); + DeleteObject(hbmp); + RETURN_FALSE; + } + } + else { + if (!BitBlt(resource->dc, Z_LVAL_PP(arg3), Z_LVAL_PP(arg4), bmp_property.bmWidth, bmp_property.bmHeight, dummy, 0, 0, SRCCOPY)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Printer failed to accept bitmap"); + DeleteDC(dummy); + DeleteObject(hbmp); + RETURN_FALSE; + } } DeleteDC(dummy); - DeleteObject(bmp); + DeleteObject(hbmp); RETURN_TRUE; } /* }}} */
http://cvs.php.net/diff.php/pecl/printer/php_printer.h?r1=1.15&r2=1.16&ty=u Index: pecl/printer/php_printer.h diff -u pecl/printer/php_printer.h:1.15 pecl/printer/php_printer.h:1.16 --- pecl/printer/php_printer.h:1.15 Sat Jul 31 13:58:57 2004 +++ pecl/printer/php_printer.h Mon Aug 2 16:31:23 2004 @@ -14,10 +14,12 @@ +----------------------------------------------------------------------+ | Authors: Frank M. Kromann <frank@kromann.info> | | Daniel Beulshausen <daniel@php4win.de> | + | Contribution: | + | Philippe MAES <luckyluke@dlfp.org> | +----------------------------------------------------------------------+ */ -/* $Id: php_printer.h,v 1.15 2004/07/31 17:58:57 fmk Exp $ */ +/* $Id: php_printer.h,v 1.16 2004/08/02 20:31:23 fmk Exp $ */ #ifndef PHP_PRINTER_H #define PHP_PRINTER_H @@ -66,9 +68,10 @@ typedef struct { HANDLE handle; LPTSTR name; - LPDEVMODE device; DOCINFO info; HDC dc; + PRINTER_INFO_2 *pi2; + DWORD dmModifiedFields; } printer; ZEND_BEGIN_MODULE_GLOBALS(printer) http://cvs.php.net/diff.php/pecl/printer/printer.c?r1=1.29&r2=1.30&ty=u Index: pecl/printer/printer.c diff -u pecl/printer/printer.c:1.29 pecl/printer/printer.c:1.30 --- pecl/printer/printer.c:1.29 Sat Jul 31 13:58:57 2004 +++ pecl/printer/printer.c Mon Aug 2 16:31:23 2004 @@ -14,10 +14,12 @@ +----------------------------------------------------------------------+ | Authors: Frank M. Kromann <frank@kromann.info> | | Daniel Beulshausen <daniel@php4win.de> | + | Contribution: | + | Philippe MAES <luckyluke@dlfp.org> | +----------------------------------------------------------------------+ */ -/* $Id: printer.c,v 1.29 2004/07/31 17:58:57 fmk Exp $ */ +/* $Id: printer.c,v 1.30 2004/08/02 20:31:23 fmk Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -31,8 +33,6 @@ #ifdef HAVE_PRINTER -#include "php_printer.h" - static int le_printer, le_brush, le_pen, le_font; #ifdef PHP_WIN32 @@ -40,6 +40,8 @@ #include <wingdi.h> #include <winspool.h> +#include "php_printer.h" + COLORREF hex_to_rgb(char * hex); char *rgb_to_hex(COLORREF rgb); static void printer_close(zend_rsrc_list_entry *resource TSRMLS_DC); @@ -107,7 +109,7 @@ php_info_print_table_row(2, "Printer Support", "enabled"); php_info_print_table_row(2, "Default printing device", PRINTERG(default_printer) ? PRINTERG(default_printer) : "<b>not detected</b>"); php_info_print_table_row(2, "Module state", "working"); - php_info_print_table_row(2, "RCS Version", "$Id: printer.c,v 1.29 2004/07/31 17:58:57 fmk Exp $"); + php_info_print_table_row(2, "RCS Version", "$Id: printer.c,v 1.30 2004/08/02 20:31:23 fmk Exp $"); php_info_print_table_end(); DISPLAY_INI_ENTRIES(); } @@ -143,6 +145,7 @@ #define DEVICENAME 13 #define DRIVER_VERSION 14 #define OUTPUT_FILE 15 +#define VALID_OPTIONS 16 #define BRUSH_SOLID -1 #define BRUSH_CUSTOM -2 @@ -256,9 +259,11 @@ { pval **arg1; printer *resource; + int argc = ZEND_NUM_ARGS(); resource = (printer *)emalloc(sizeof(printer)); + resource->dmModifiedFields = 0; if( argc == 1 && zend_get_parameters_ex(1, &arg1) != FAILURE ) { convert_to_string_ex(arg1); @@ -271,21 +276,21 @@ WRONG_PARAM_COUNT; } - - if( OpenPrinter(resource->name, &resource->handle, NULL) != 0 ) { - resource->device = emalloc(DocumentProperties(NULL, NULL, resource->name, NULL, NULL, 0)); - DocumentProperties(NULL, resource->handle, resource->name, resource->device, NULL, DM_OUT_BUFFER); - - resource->info.lpszDocName = estrdup("PHP generated Document"); - resource->info.lpszOutput = NULL; - resource->info.lpszDatatype = estrdup("TEXT"); - resource->info.fwType = 0; - resource->info.cbSize = sizeof(resource->info); - resource->dc = CreateDC(NULL, resource->name, NULL, resource->device); - ZEND_REGISTER_RESOURCE(return_value, resource, le_printer); + if (OpenPrinter(resource->name, &resource->handle, NULL) != 0) { + resource->pi2 = (PRINTER_INFO_2 *)emalloc(sizeof(PRINTER_INFO_2)); + resource->pi2->pDevMode = (DEVMODE *)emalloc(DocumentProperties(NULL, NULL, resource->name, NULL, NULL, 0)); + if (DocumentProperties(NULL, resource->handle, resource->name, resource->pi2->pDevMode, NULL, DM_OUT_BUFFER) == IDOK) { + resource->info.lpszDocName = estrdup("PHP generated Document"); + resource->info.lpszOutput = NULL; + resource->info.lpszDatatype = estrdup("TEXT"); + resource->info.fwType = 0; + resource->info.cbSize = sizeof(resource->info); + resource->dc = CreateDC(NULL, resource->name, NULL, resource->pi2->pDevMode); + ZEND_REGISTER_RESOURCE(return_value, resource, le_printer); + } } else { - php_error(E_WARNING,"couldn't connect to the printer [%s]", resource->name); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't connect to the printer [%s]", resource->name); RETURN_FALSE; } } @@ -341,7 +346,7 @@ RETURN_TRUE; } else { - php_error(E_WARNING,"couldn't allocate the printerjob [%d]", GetLastError()); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't allocate the printerjob [%d]", GetLastError()); RETURN_FALSE; } } @@ -372,7 +377,7 @@ convert_to_long_ex(arg3); Level = (int)Z_LVAL_PP(arg3); if(!LevvelsAllowed[Level]) { - php_error(E_WARNING, "Level not allowed"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Level not allowed"); RETURN_FALSE; } case 2: @@ -464,6 +469,8 @@ Configure the printer device */ PHP_FUNCTION(printer_set_option) { + DWORD dwNeeded =0; + PRINTER_DEFAULTS pd; pval **arg1, **arg2, **arg3; printer *resource; @@ -474,10 +481,15 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); convert_to_long_ex(arg2); + memset(&pd,0,sizeof(pd)); + pd.DesiredAccess = PRINTER_ALL_ACCESS; + SetLastError(0); + switch(Z_LVAL_PP(arg2)) { case COPIES: convert_to_long_ex(arg3); - resource->device->dmCopies = (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmCopies=(short)Z_LVAL_PP(arg3); + resource->dmModifiedFields|=DM_COPIES; break; case MODE: @@ -509,37 +521,44 @@ case ORIENTATION: convert_to_long_ex(arg3); - resource->device->dmOrientation = (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmOrientation=(short)Z_LVAL_PP(arg3); + resource->dmModifiedFields|=DM_ORIENTATION; break; case YRESOLUTION: convert_to_long_ex(arg3); - resource->device->dmYResolution = (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmYResolution = (short)Z_LVAL_PP(arg3); + resource->dmModifiedFields|=DM_YRESOLUTION; break; case XRESOLUTION: convert_to_long_ex(arg3); - resource->device->dmPrintQuality= (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmPrintQuality= (short)Z_LVAL_PP(arg3); + resource->dmModifiedFields|=DM_PRINTQUALITY; break; case PAPER_FORMAT: convert_to_long_ex(arg3); - resource->device->dmPaperSize = (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmPaperSize = (short)Z_LVAL_PP(arg3); + resource->dmModifiedFields|=DM_PAPERSIZE; break; case PAPER_LENGTH: convert_to_long_ex(arg3); - resource->device->dmPaperLength = Z_LVAL_PP(arg3) * 10; + resource->pi2->pDevMode->dmPaperLength = Z_LVAL_PP(arg3) * 10; + resource->dmModifiedFields|=DM_PAPERLENGTH; break; case PAPER_WIDTH: convert_to_long_ex(arg3); - resource->device->dmPaperWidth = Z_LVAL_PP(arg3) * 10; + resource->pi2->pDevMode->dmPaperWidth = Z_LVAL_PP(arg3) * 10; + resource->dmModifiedFields |= DM_PAPERWIDTH; break; case SCALE: convert_to_long_ex(arg3); - resource->device->dmScale = (short)Z_LVAL_PP(arg3); + resource->pi2->pDevMode->dmScale = (short)Z_LVAL_PP(arg3); + resource->dmModifiedFields |= DM_SCALE; break; case BG_COLOR: @@ -556,9 +575,16 @@ convert_to_string_ex(arg3); SetTextAlign(resource->dc, Z_LVAL_PP(arg3)); break; - + case VALID_OPTIONS: + resource->pi2->pSecurityDescriptor=NULL; + resource->pi2->pDevMode->dmFields=resource->dmModifiedFields; + resource->dmModifiedFields=0; + DocumentProperties(NULL,resource->handle,resource->name,resource->pi2->pDevMode,resource->pi2->pDevMode,DM_IN_BUFFER | DM_OUT_BUFFER); + SetPrinter(resource->handle,2,(LPBYTE)resource->pi2,0); + SendMessageTimeout(HWND_BROADCAST,WM_DEVMODECHANGE,0L,(LPARAM)(LPCSTR)resource->name,SMTO_NORMAL,1000,NULL); + break; default: - php_error(E_WARNING,"unknown option passed to printer_set_option()"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknown option passed to printer_set_option()"); RETURN_FALSE; } @@ -584,7 +610,7 @@ switch(Z_LVAL_PP(arg2)) { case COPIES: - RETURN_LONG(resource->device->dmCopies); + RETURN_LONG(resource->pi2->pDevMode->dmCopies); case MODE: RETURN_STRING((char*)resource->info.lpszDatatype,1); @@ -601,25 +627,25 @@ } case ORIENTATION: - RETURN_LONG(resource->device->dmOrientation); + RETURN_LONG(resource->pi2->pDevMode->dmOrientation); case YRESOLUTION: - RETURN_LONG(resource->device->dmYResolution); + RETURN_LONG(resource->pi2->pDevMode->dmYResolution); case XRESOLUTION: - RETURN_LONG(resource->device->dmPrintQuality); + RETURN_LONG(resource->pi2->pDevMode->dmPrintQuality); case PAPER_FORMAT: - RETURN_LONG(resource->device->dmPaperSize); + RETURN_LONG(resource->pi2->pDevMode->dmPaperSize); case PAPER_LENGTH: - RETURN_LONG(resource->device->dmPaperLength / 10); + RETURN_LONG(resource->pi2->pDevMode->dmPaperLength / 10); case PAPER_WIDTH: - RETURN_LONG(resource->device->dmPaperWidth / 10); + RETURN_LONG(resource->pi2->pDevMode->dmPaperWidth / 10); case SCALE: - RETURN_LONG(resource->device->dmScale); + RETURN_LONG(resource->pi2->pDevMode->dmScale); case BG_COLOR: RETURN_STRING(rgb_to_hex(GetBkColor(resource->dc)), 0); @@ -634,10 +660,10 @@ RETURN_STRING(resource->name, 1); case DRIVER_VERSION: - RETURN_LONG(resource->device->dmDriverVersion); + RETURN_LONG(resource->pi2->pDevMode->dmDriverVersion); default: - php_error(E_WARNING,"unknown option passed to printer_get_option()"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknown option passed to printer_get_option()"); RETURN_FALSE; } } @@ -658,11 +684,11 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); if( resource->dc != NULL ) { - php_error(E_NOTICE, "Deleting old DeviceContext"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Deleting old DeviceContext"); DeleteDC(resource->dc); } - resource->dc = CreateDC(NULL, resource->name, NULL, resource->device); + resource->dc = CreateDC(NULL, resource->name, NULL, resource->pi2->pDevMode); } /* }}} */ @@ -686,7 +712,7 @@ RETURN_TRUE; } else { - php_error(E_WARNING,"No DeviceContext created"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "No DeviceContext created"); RETURN_FALSE; } } @@ -717,7 +743,7 @@ } if(StartDoc(resource->dc, &resource->info) < 0) { - php_error(E_WARNING, "couldn't allocate new print job"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't allocate new print job"); RETURN_FALSE; } @@ -740,7 +766,7 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); if(EndDoc(resource->dc) < 0) { - php_error(E_ERROR,"couldn't terminate print job"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't terminate print job"); RETURN_FALSE; } @@ -763,7 +789,7 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); if(StartPage(resource->dc) < 0) { - php_error(E_WARNING, "couldn't start a new page"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't start a new page"); RETURN_FALSE; } @@ -786,7 +812,7 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); if(EndPage(resource->dc) < 0) { - php_error(E_WARNING, "couldn't end the page"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't end the page"); RETURN_FALSE; } @@ -1198,19 +1224,34 @@ /* }}} */ -/* {{{ proto mixed printer_draw_bmp(resource handle, string filename, int x, int y) +/* {{{ proto mixed printer_draw_bmp(resource handle, string filename, int x, int y [, int width, int height]) Draw a bitmap */ PHP_FUNCTION(printer_draw_bmp) { - pval **arg1, **arg2, **arg3, **arg4; + pval **arg1, **arg2, **arg3, **arg4, **arg5 = NULL, **arg6 = NULL; printer *resource; - HBITMAP bmp; + HBITMAP hbmp; BITMAP bmp_property; HDC dummy; char* path; - if(zend_get_parameters_ex(4, &arg1, &arg2, &arg3, &arg4) == FAILURE ) { - WRONG_PARAM_COUNT; + int args = ZEND_NUM_ARGS(); + switch (args) { + case 4: + if (zend_get_parameters_ex(4, &arg1, &arg2, &arg3, &arg4) == FAILURE ) { + RETURN_FALSE; + } + break; + case 6: + if (zend_get_parameters_ex(6, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE ) { + RETURN_FALSE; + } + convert_to_long_ex(arg5); + convert_to_long_ex(arg6); + break; + default: + WRONG_PARAM_COUNT; + break; } ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); @@ -1220,48 +1261,56 @@ virtual_filepath(Z_STRVAL_PP(arg2), &path TSRMLS_CC); - if ((bmp = LoadImage(0, path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)) == NULL) { + hbmp = (HBITMAP)LoadImage(0, path, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_LOADFROMFILE); + + if (hbmp == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to load bitmap %s", Z_STRVAL_PP(arg2)); RETURN_FALSE; } - SelectObject(resource->dc, bmp); - - if (!(GetDeviceCaps(resource->dc, RASTERCAPS) & RC_BITBLT)) - { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Printer does not support bitmaps"); - DeleteObject(bmp); + if (GetObject(hbmp, sizeof(BITMAP), &bmp_property) == 0) { + if(hbmp) + DeleteObject(hbmp); RETURN_FALSE; } - - if((dummy = CreateCompatibleDC(NULL)) == NULL) { - DeleteObject(bmp); + + if (!(GetDeviceCaps(resource->dc, RASTERCAPS) & RC_STRETCHBLT)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Printer does not support bitmaps"); + DeleteObject(hbmp); RETURN_FALSE; } - if(SelectObject(dummy, bmp) == NULL) { - DeleteDC(dummy); - DeleteObject(bmp); + if ((dummy = CreateCompatibleDC(resource->dc)) == NULL) { + DeleteObject(hbmp); RETURN_FALSE; } - if (GetObject(bmp, sizeof(BITMAP), &bmp_property) == 0) { + if (SelectObject(dummy, hbmp) == NULL) { DeleteDC(dummy); - DeleteObject(bmp); + DeleteObject(hbmp); RETURN_FALSE; } - if (!BitBlt(resource->dc, Z_LVAL_PP(arg3), Z_LVAL_PP(arg4), bmp_property.bmWidth, bmp_property.bmHeight, dummy, 0, 0, SRCCOPY)) - { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Printer failed to accept bitmap"); - - DeleteDC(dummy); - DeleteObject(bmp); - RETURN_FALSE; + + if (args > 4) { + if (!StretchBlt(resource->dc,(int)Z_LVAL_PP(arg3), (int)Z_LVAL_PP(arg4), (int)Z_LVAL_PP(arg5), (int)Z_LVAL_PP(arg6), dummy, 0, 0, bmp_property.bmWidth,bmp_property.bmHeight, SRCCOPY)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Printer failed to accept bitmap"); + DeleteDC(dummy); + DeleteObject(hbmp); + RETURN_FALSE; + } + } + else { + if (!BitBlt(resource->dc, Z_LVAL_PP(arg3), Z_LVAL_PP(arg4), bmp_property.bmWidth, bmp_property.bmHeight, dummy, 0, 0, SRCCOPY)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Printer failed to accept bitmap"); + DeleteDC(dummy); + DeleteObject(hbmp); + RETURN_FALSE; + } } DeleteDC(dummy); - DeleteObject(bmp); + DeleteObject(hbmp); RETURN_TRUE; } /* }}} */