From 3c598a00e2f252802fb44e892086aca66bd5a718 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Fri, 12 Sep 2014 18:17:49 +0930 Subject: [PATCH 6/9] make pdftocairo-win32.cc a standalone .cc file instead of #including it in pdftocairo.cc --- utils/CMakeLists.txt | 1 + utils/Makefile.am | 1 + utils/pdftocairo-win32.cc | 74 +++++++++++++++++++++++------------------------ utils/pdftocairo-win32.h | 22 ++++++++++++++ utils/pdftocairo.cc | 24 +++++++++------ 5 files changed, 76 insertions(+), 46 deletions(-) create mode 100644 utils/pdftocairo-win32.h diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 2f04b39..9c16018 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -21,6 +21,7 @@ if (HAVE_CAIRO) # pdftocairo set(pdftocairo_SOURCES ${common_srcs} pdftocairo.cc + pdftocairo-win32.cc ${CMAKE_SOURCE_DIR}/poppler/CairoFontEngine.cc ${CMAKE_SOURCE_DIR}/poppler/CairoOutputDev.cc ${CMAKE_SOURCE_DIR}/poppler/CairoRescaleBox.cc diff --git a/utils/Makefile.am b/utils/Makefile.am index be6aa6d..e6d209b 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -115,6 +115,7 @@ pdftoppm_SOURCES = \ pdftocairo_SOURCES = \ pdftocairo.cc \ + pdftocairo-win32.cc \ $(common) if BUILD_CAIRO_OUTPUT diff --git a/utils/pdftocairo-win32.cc b/utils/pdftocairo-win32.cc index 195e6ad..7ad5615 100644 --- a/utils/pdftocairo-win32.cc +++ b/utils/pdftocairo-win32.cc @@ -1,11 +1,15 @@ +#include +#ifdef CAIRO_HAS_WIN32_SURFACE + #include -static void win32GetFitToPageTransform(cairo_matrix_t *m) -{ - if (!print) - return; +#include "parseargs.h" +#include "pdftocairo-win32.h" + +static HDC hdc; - HDC hdc = cairo_win32_surface_get_dc(surface); +void win32GetFitToPageTransform(cairo_matrix_t *m) +{ int logx = GetDeviceCaps(hdc, LOGPIXELSX); int logy = GetDeviceCaps(hdc, LOGPIXELSY); cairo_matrix_scale (m, logx / 72.0, logy / 72.0); @@ -68,9 +72,6 @@ static const Win32Option win32DuplexMode[] = static void parseDuplex(DEVMODEA *devmode, GooString *mode) { - if (duplex) - fprintf(stderr, "Warning: duplex mode is specified both as standalone and printer options\n"); - int win32Duplex; const Win32Option *option = win32DuplexMode; while (option->name) { @@ -88,7 +89,7 @@ static void parseDuplex(DEVMODEA *devmode, GooString *mode) devmode->dmFields |= DM_DUPLEX; } -static void fillCommonPrinterOptions(DEVMODEA *devmode, double w, double h) +static void fillCommonPrinterOptions(DEVMODEA *devmode, double w, double h, GBool duplex) { devmode->dmPaperWidth = w * 254.0 / 72.0; devmode->dmPaperLength = h * 254.0 / 72.0; @@ -100,10 +101,10 @@ static void fillCommonPrinterOptions(DEVMODEA *devmode, double w, double h) } } -static void fillPrinterOptions(DEVMODEA *devmode) +static void fillPrinterOptions(DEVMODEA *devmode, GBool duplex, GooString *printOpt) { //printOpt format is: =,=,... - const char *nextOpt = printOpt.getCString(); + const char *nextOpt = printOpt->getCString(); while (nextOpt && *nextOpt) { const char *comma = strchr(nextOpt, ','); @@ -129,28 +130,33 @@ static void fillPrinterOptions(DEVMODEA *devmode) if (opt.cmp("source") == 0) { parseSource(devmode, &value); } else if (opt.cmp("duplex") == 0) { - parseDuplex(devmode, &value); + if (duplex) { + fprintf(stderr, "Warning: duplex mode is specified both as standalone and printer options\n"); + } else { + parseDuplex(devmode, &value); + } } else { fprintf(stderr, "Warning: unknown printer option \"%s\"\n", opt.getCString()); } } } -static void win32BeginDocument(GooString *inputFileName, GooString *outputFileName, double w, double h) +cairo_surface_t *win32BeginDocument(GooString *inputFileName, GooString *outputFileName, + double w, double h, + GooString *printer, + GooString *printOpt, + GBool duplex) { - if (!print) - return; - - if (printer.getCString()[0] == 0) + if (printer->getCString()[0] == 0) { DWORD szName = 0; GetDefaultPrinterA(NULL, &szName); char *devname = (char*)gmalloc(szName); GetDefaultPrinterA(devname, &szName); - printer.Set(devname); + printer->Set(devname); gfree(devname); } - char *cPrinter = printer.getCString(); + char *cPrinter = printer->getCString(); //Query the size of the DEVMODE struct LONG szProp = DocumentPropertiesA(NULL, NULL, cPrinter, NULL, NULL, 0); @@ -169,9 +175,9 @@ static void win32BeginDocument(GooString *inputFileName, GooString *outputFileNa fprintf(stderr, "Error: Printer \"%s\" not found", cPrinter); exit(99); } - fillCommonPrinterOptions(devmode, w, h); - fillPrinterOptions(devmode); - HDC hdc = CreateDCA(NULL, cPrinter, NULL, devmode); + fillCommonPrinterOptions(devmode, w, h, duplex); + fillPrinterOptions(devmode, duplex, printOpt); + hdc = CreateDCA(NULL, cPrinter, NULL, devmode); gfree(devmode); if (!hdc) { @@ -194,30 +200,24 @@ static void win32BeginDocument(GooString *inputFileName, GooString *outputFileNa fprintf(stderr, "Error: StartDoc failed"); exit(99); } - - surface = cairo_win32_printing_surface_create(hdc); + + return cairo_win32_printing_surface_create(hdc); } -static void win32BeginPage(double w, double h) +void win32BeginPage(double w, double h) { - if (!print) - return; - StartPage(cairo_win32_surface_get_dc(surface)); + StartPage(hdc); } -static void win32EndPage(GooString *imageFileName) +void win32EndPage(GooString *imageFileName) { - if (!print) - return; - EndPage(cairo_win32_surface_get_dc(surface)); + EndPage(hdc); } -static void win32EndDocument() +void win32EndDocument() { - if (!print) - return; - - HDC hdc = cairo_win32_surface_get_dc(surface); EndDoc(hdc); DeleteDC(hdc); } + +#endif // CAIRO_HAS_WIN32_SURFACE diff --git a/utils/pdftocairo-win32.h b/utils/pdftocairo-win32.h new file mode 100644 index 0000000..8ecd84c --- /dev/null +++ b/utils/pdftocairo-win32.h @@ -0,0 +1,22 @@ +#include +#include "goo/gmem.h" +#include "goo/gtypes.h" +#include "goo/gtypes_p.h" +#include "goo/GooString.h" + + +#ifdef CAIRO_HAS_WIN32_SURFACE + +#include + +void win32GetFitToPageTransform(cairo_matrix_t *m); +cairo_surface_t *win32BeginDocument(GooString *inputFileName, GooString *outputFileName, + double w, double h, + GooString *printer, + GooString *printOpt, + GBool duplex); +void win32BeginPage(double w, double h); +void win32EndPage(GooString *imageFileName); +void win32EndDocument(); + +#endif // CAIRO_HAS_WIN32_SURFACE diff --git a/utils/pdftocairo.cc b/utils/pdftocairo.cc index 65d7c23..aa76226 100644 --- a/utils/pdftocairo.cc +++ b/utils/pdftocairo.cc @@ -71,6 +71,8 @@ #include #endif +#include "pdftocairo-win32.h" + static GBool png = gFalse; static GBool jpeg = gFalse; @@ -267,10 +269,6 @@ static int icc_data_size; static cmsHPROFILE profile; #endif -#ifdef CAIRO_HAS_WIN32_SURFACE -#include "pdftocairo-win32.cc" -#endif - void writePageImage(GooString *filename) { ImgWriter *writer = 0; @@ -486,7 +484,8 @@ static void getFitToPageTransform(double page_w, double page_h, cairo_matrix_scale (m, scale, scale); } #ifdef CAIRO_HAS_WIN32_SURFACE - win32GetFitToPageTransform(m); + if (print) + win32GetFitToPageTransform(m); #endif } @@ -541,7 +540,8 @@ static void beginDocument(GooString *inputFileName, GooString *outputFileName, d #endif } #ifdef CAIRO_HAS_WIN32_SURFACE - win32BeginDocument(inputFileName, outputFileName, w, h); + if (print) + surface = win32BeginDocument(inputFileName, outputFileName, w, h, &printer, &printOpt, duplex); #endif } } @@ -565,8 +565,10 @@ static void beginPage(double w, double h) if (pdf) cairo_pdf_surface_set_size (surface, w, h); #endif + #ifdef CAIRO_HAS_WIN32_SURFACE - win32BeginPage(w, h); + if (print) + win32BeginPage(w, h); #endif cairo_surface_set_fallback_resolution (surface, x_resolution, y_resolution); @@ -638,9 +640,12 @@ static void endPage(GooString *imageFileName) if (printing) { cairo_surface_show_page(surface); + #ifdef CAIRO_HAS_WIN32_SURFACE - win32EndPage(imageFileName); + if (print) + win32EndPage(imageFileName); #endif + } else { writePageImage(imageFileName); cairo_surface_finish(surface); @@ -658,7 +663,8 @@ static void endDocument() if (printing) { #ifdef CAIRO_HAS_WIN32_SURFACE - win32EndDocument(); + if (print) + win32EndDocument(); #endif cairo_surface_finish(surface); status = cairo_surface_status(surface); -- 2.1.0