From 1a03de334fd4e6aafb91b806ea35092a7774a8d7 Mon Sep 17 00:00:00 2001 From: Didier Raboud Date: Mon, 19 Jul 2010 15:56:58 +0200 Subject: Imported Upstream version 4.0.4 --- pdf.c | 150 +++++++++++++++++++++--------------------------------------------- 1 file changed, 47 insertions(+), 103 deletions(-) (limited to 'pdf.c') diff --git a/pdf.c b/pdf.c index 53a02b0..968c885 100644 --- a/pdf.c +++ b/pdf.c @@ -1,3 +1,25 @@ +/* pdf.c + * + * Copyright (C) 2008 Till Kamppeter + * Copyright (C) 2008 Lars Uebernickel + * + * This file is part of foomatic-rip. + * + * Foomatic-rip is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Foomatic-rip is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ #include "foomaticrip.h" #include "util.h" @@ -11,104 +33,36 @@ #include #include -#include -#include - #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) -const char *pagecountcode = - "/pdffile (%s) (r) file def\n" - "pdfdict begin\n" - "pdffile pdfopen begin\n" - "(PageCount: ) print\n" - "pdfpagecount == flush\n" /* 'flush' makes sure that gs_stdout is called - before gsapi_run_string returns */ - "currentdict pdfclose\n" - "end end\n"; - -char gsout [256]; - static int wait_for_renderer(); -static const char * temp_dir() -{ - static const char *tmpdir = NULL; - - if (!tmpdir) - { - const char *dirs[] = { getenv("TMPDIR"), P_tmpdir, "/tmp", NULL }; - const char **dir; - - for (dir = dirs; *dir; dir++) - if (access(*dir, W_OK) == 0) { - tmpdir = *dir; - break; - } - if (tmpdir) - { - _log("Storing temporary files in %s\n", tmpdir); - setenv("TMPDIR", tmpdir, 1); /* for child processes */ - } - else - rip_die(EXIT_PRNERR_NORETRY_BAD_SETTINGS, - "Cannot find a writable temp dir."); - } - - return tmpdir; -} - -int gs_stdout(void *instance, const char *str, int len) -{ - int last; - if (isempty(gsout)) { - last = len < 256 ? len : 255; - strncpy(gsout, str, last +1); - gsout[last] = '\0'; - } - return len; /* ignore everything after the first few chars */ -} - -int gs_stderr(void *instance, const char *str, int len) -{ - char *buf = malloc(len +1); - strncpy(buf, str, len); - buf[len] = '\0'; - _log("Ghostscript: %s", buf); - free(buf); - return len; -} - static int pdf_count_pages(const char *filename) { - void *minst; - int gsargc = 3; - char *gsargv[] = { "", "-dNODISPLAY", "-q" }; + char gscommand[4095]; + char output[31] = ""; int pagecount; - int exit_code; - char code[2048]; - if (gsapi_new_instance(&minst, NULL) < 0) { - _log("Could not create ghostscript instance\n"); - return -1; - } - gsapi_set_stdio(minst, NULL, gs_stdout, gs_stderr); - if (gsapi_init_with_args(minst, gsargc, gsargv) < 0) { - _log("Could not init ghostscript\n"); - gsapi_exit(minst); - gsapi_delete_instance(minst); - return -1; - } + snprintf(gscommand, 4095, "%s -dNODISPLAY -q -c " + "'/pdffile (%s) (r) file def pdfdict begin pdffile pdfopen begin " + "(PageCount: ) print pdfpagecount == flush currentdict pdfclose " + "end end quit'", + gspath, filename); - snprintf(code, 2048, pagecountcode, filename); - if (gsapi_run_string(minst, code, 0, &exit_code) == 0) { - if (sscanf(gsout, "PageCount: %d", &pagecount) < 1) - pagecount = -1; + FILE *pd = popen(gscommand, "r"); + if (!pd) { + _log("Failed to execute ghostscript to determine number of input pages!\n"); + return 0; } - gsapi_exit(minst); - gsapi_delete_instance(minst); + fread(output, 1, 31, pd); + pclose(pd); + + if (sscanf(output, "PageCount: %d", &pagecount) < 1) + pagecount = -1; + return pagecount; } @@ -156,11 +110,8 @@ static int pdf_extract_pages(char filename[PATH_MAX], int first, int last) { - void *minst; + char gscommand[4095]; char filename_arg[PATH_MAX], first_arg[50], last_arg[50]; - const char *gs_args[] = { "", "-q", "-dNOPAUSE", "-dBATCH", - "-dPARANOIDSAFER", "-sDEVICE=pdfwrite", filename_arg, first_arg, - last_arg, pdffilename }; _log("Extracting pages %d through %d\n", first, last); @@ -169,12 +120,6 @@ static int pdf_extract_pages(char filename[PATH_MAX], if (!filename[0]) return 0; - if (gsapi_new_instance(&minst, NULL) < 0) - { - _log("Could not create ghostscript instance\n"); - return 0; - } - snprintf(filename_arg, PATH_MAX, "-sOutputFile=%s", filename); snprintf(first_arg, 50, "-dFirstPage=%d", first); if (last > 0) @@ -182,18 +127,17 @@ static int pdf_extract_pages(char filename[PATH_MAX], else first_arg[0] = '\0'; - gsapi_set_stdio(minst, NULL, NULL, gs_stderr); + snprintf(gscommand, 4095, "%s -q -dNOPAUSE -dBATCH -dPARANOIDSAFER" + "-sDEVICE=pdfwrite %s %s %s %s", + gspath, filename_arg, first_arg, last_arg, pdffilename); - if (gsapi_init_with_args(minst, ARRAY_LEN(gs_args), (char **)gs_args) < 0) - { - _log("Could not run ghostscript to extract the pages\n"); - gsapi_exit(minst); - gsapi_delete_instance(minst); + FILE *pd = popen(gscommand, "r"); + if (!pd) { + _log("Could not run ghostscript to extract the pages!\n"); return 0; } + pclose(pd); - gsapi_exit(minst); - gsapi_delete_instance(minst); return 1; } -- cgit v1.2.3