#include #include #include #include //#define PRINT_DLG 1 void draw (cairo_t *cr) { cairo_rectangle (cr, 100, 100, 300, 200); cairo_stroke (cr); } static void cairo_draw (HDC hdc) { cairo_surface_t *surface; cairo_t *cr; int x, y, x_dpi, y_dpi, x_off, y_off, depth; XFORM xform; cairo_status_t status; x = GetDeviceCaps (hdc, HORZRES); y = GetDeviceCaps (hdc, VERTRES); printf(" x = %d y = %d\n", x, y); x_dpi = GetDeviceCaps (hdc, LOGPIXELSX); y_dpi = GetDeviceCaps (hdc, LOGPIXELSY); printf("dpi x = %d y = %d\n", x_dpi, y_dpi); x_off = GetDeviceCaps (hdc, PHYSICALOFFSETX); y_off = GetDeviceCaps (hdc, PHYSICALOFFSETY); printf("offset x = %d y = %d\n", x_off, y_off); depth = GetDeviceCaps(hdc, BITSPIXEL); printf("depth = %d\n", depth); SetGraphicsMode (hdc, GM_ADVANCED); xform.eM11 = x_dpi/72.0; xform.eM12 = 0; xform.eM21 = 0; xform.eM22 = y_dpi/72.0; xform.eDx = -x_off; xform.eDy = -y_off; SetWorldTransform (hdc, &xform); surface = cairo_win32_printing_surface_create (hdc); //cairo_surface_set_fallback_resolution (surface, x_dpi, y_dpi); cr = cairo_create (surface); //cairo_translate (cr, -x_off, -y_off); //cairo_scale (cr, x_dpi/72.0, y_dpi/72.0); draw (cr); cairo_show_page (cr); status = cairo_status (cr); if (status) printf("Win cr returned error status %d\n", status); cairo_destroy (cr); cairo_surface_finish (surface); status = cairo_surface_status (surface); if (status) printf("Win surface returned error status %d\n", status); cairo_surface_destroy (surface); } static HDC create_printer_dc () { HDC hdc; #ifdef PRINT_DLG PRINTDLG pd; pd.lStructSize = sizeof (PRINTDLG); pd.hwndOwner = NULL; pd.hDevMode = NULL; pd.hDevNames = NULL; pd.hDC = NULL; pd.Flags = PD_ALLPAGES|PD_NOSELECTION|PD_PRINTTOFILE|PD_RETURNDC; pd.nFromPage = 0; pd.nToPage = 0; pd.nMinPage = 0; pd.nMaxPage = 0; pd.nCopies = 1; pd.hInstance = NULL; pd.lCustData = 0L; pd.lpfnPrintHook = NULL; pd.lpfnSetupHook = NULL;; pd.lpPrintTemplateName = NULL; pd.lpSetupTemplateName = NULL; pd.hPrintTemplate = NULL; pd.hSetupTemplate = NULL; if (PrintDlg (&pd) == 0) hdc = NULL; else hdc = pd.hDC; #else DWORD needed, returned; PRINTER_INFO_4 *info4; HANDLE printer; DEVMODE *dev; long size; int i; int n = 0; EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 4, NULL, 0, &needed, &returned); info4 = malloc (needed); EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 4, (LPBYTE) info4, needed, &needed, &returned); for (i = 0; i < returned; i++) printf("printer: %s\n", info4[i].pPrinterName); printf("\nPrinting to : %s\n", info4[n].pPrinterName); hdc = CreateDC (NULL, info4[n].pPrinterName, NULL, NULL); #endif return hdc; } void win_print () { #ifdef PRINT_DLG DOCINFO di = { sizeof (DOCINFO), TEXT ("Printing") }; #else DOCINFO di = { sizeof (DOCINFO), TEXT ("Printing"), TEXT ("cairo.ps") }; #endif HDC hdc; hdc = create_printer_dc (); if (hdc == NULL) { printf("hdc == NULL\n"); return; } StartDoc (hdc, &di); StartPage (hdc); cairo_draw (hdc); EndPage (hdc); EndDoc (hdc); DeleteDC (hdc); }