~ubuntu-branches/debian/experimental/cups-filters/experimental

« back to all changes in this revision

Viewing changes to filter/foomatic-rip/pdf.c

  • Committer: Package Import Robot
  • Author(s): Didier Raboud
  • Date: 2015-01-15 18:06:05 UTC
  • mfrom: (1.2.25)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: package-import@ubuntu.com-20150115180605-fnfbqv85k3y5zggk
Tags: upstream-1.0.62
ImportĀ upstreamĀ versionĀ 1.0.62

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* pdf.c
 
2
 *
 
3
 * Copyright (C) 2008 Till Kamppeter <till.kamppeter@gmail.com>
 
4
 * Copyright (C) 2008 Lars Uebernickel <larsuebernickel@gmx.de>
 
5
 *
 
6
 * This file is part of foomatic-rip.
 
7
 *
 
8
 * Foomatic-rip is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * Foomatic-rip is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this library; if not, write to the
 
20
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
21
 * Boston, MA 02111-1307, USA.
 
22
 */
 
23
 
 
24
#include "foomaticrip.h"
 
25
#include "util.h"
 
26
#include "options.h"
 
27
#include "process.h"
 
28
#include "renderer.h"
 
29
 
 
30
#include <stdlib.h>
 
31
#include <ctype.h>
 
32
#include <sys/wait.h>
 
33
#include <unistd.h>
 
34
#include <errno.h>
 
35
 
 
36
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
 
37
 
 
38
 
 
39
static int wait_for_renderer();
 
40
 
 
41
 
 
42
static int pdf_count_pages(const char *filename)
 
43
{
 
44
    char gscommand[CMDLINE_MAX];
 
45
    char output[31] = "";
 
46
    int pagecount;
 
47
    size_t bytes;
 
48
 
 
49
    snprintf(gscommand, CMDLINE_MAX, "%s -dNODISPLAY -q -c "
 
50
             "'/pdffile (%s) (r) file def pdfdict begin pdffile pdfopen begin "
 
51
             "(PageCount: ) print pdfpagecount == flush currentdict pdfclose "
 
52
             "end end quit'",
 
53
             gspath, filename);
 
54
 
 
55
    FILE *pd = popen(gscommand, "r");
 
56
    if (!pd)
 
57
      rip_die(EXIT_STARVED, "Failed to execute ghostscript to determine number of input pages!\n");
 
58
 
 
59
    bytes = fread(output, 1, 31, pd);
 
60
    pclose(pd);
 
61
 
 
62
    if (bytes <= 0 || sscanf(output, "PageCount: %d", &pagecount) < 1)
 
63
      pagecount = -1;
 
64
 
 
65
    return pagecount;
 
66
}
 
67
 
 
68
pid_t kid3 = 0;
 
69
 
 
70
 
 
71
static int start_renderer(const char *cmd)
 
72
{
 
73
    if (kid3 != 0)
 
74
        wait_for_renderer();
 
75
 
 
76
    _log("Starting renderer with command: %s\n", cmd);
 
77
    kid3 = start_process("kid3", exec_kid3, (void *)cmd, NULL, NULL);
 
78
    if (kid3 < 0)
 
79
        rip_die(EXIT_STARVED, "Could not start renderer\n");
 
80
 
 
81
    return 1;
 
82
}
 
83
 
 
84
static int wait_for_renderer()
 
85
{
 
86
    int status;
 
87
 
 
88
    waitpid(kid3, &status, 0);
 
89
 
 
90
    if (!WIFEXITED(status)) {
 
91
        _log("Kid3 did not finish normally.\n");
 
92
        exit(EXIT_PRNERR_NORETRY_BAD_SETTINGS);
 
93
    }
 
94
 
 
95
    _log("Kid3 exit status: %d\n", WEXITSTATUS(status));
 
96
    if (WEXITSTATUS(status) != 0)
 
97
        exit(EXIT_PRNERR_NORETRY_BAD_SETTINGS);
 
98
 
 
99
    kid3 = 0;
 
100
    return 1;
 
101
}
 
102
 
 
103
/*
 
104
 * Extract pages 'first' through 'last' from the pdf and write them into a
 
105
 * temporary file.
 
106
 */
 
107
static int pdf_extract_pages(char filename[PATH_MAX],
 
108
                             const char *pdffilename,
 
109
                             int first,
 
110
                             int last)
 
111
{
 
112
    char gscommand[CMDLINE_MAX];
 
113
    char filename_arg[PATH_MAX], first_arg[50], last_arg[50];
 
114
    int fd;
 
115
 
 
116
    _log("Extracting pages %d through %d\n", first, last);
 
117
 
 
118
    snprintf(filename, PATH_MAX, "%s/foomatic-XXXXXX", temp_dir());
 
119
    if ((fd = mkstemp(filename)) == -1)
 
120
        rip_die(EXIT_STARVED, "Unable to create temporary file!\n");
 
121
    close (fd);
 
122
 
 
123
    snprintf(filename_arg, PATH_MAX, "-sOutputFile=%s", filename);
 
124
    snprintf(first_arg, 50, "-dFirstPage=%d", first);
 
125
    if (last > 0)
 
126
        snprintf(last_arg, 50, "-dLastPage=%d", last);
 
127
    else
 
128
        first_arg[0] = '\0';
 
129
 
 
130
    snprintf(gscommand, CMDLINE_MAX, "%s -q -dNOPAUSE -dBATCH -dPARANOIDSAFER -dNOINTERPOLATE"
 
131
             "-sDEVICE=pdfwrite %s %s %s %s",
 
132
             gspath, filename_arg, first_arg, last_arg, pdffilename);
 
133
 
 
134
    FILE *pd = popen(gscommand, "r");
 
135
    if (!pd)
 
136
        rip_die(EXIT_STARVED, "Could not run ghostscript to extract the pages!\n");
 
137
    pclose(pd);
 
138
 
 
139
    return 1;
 
140
}
 
141
 
 
142
static int render_pages_with_generic_command(dstr_t *cmd,
 
143
                                             const char *filename,
 
144
                                             int firstpage,
 
145
                                             int lastpage)
 
146
{
 
147
    char tmpfile[PATH_MAX];
 
148
    int result;
 
149
 
 
150
    /* TODO it might be a good idea to give pdf command lines the possibility
 
151
     * to get the file on the command line rather than piped through stdin
 
152
     * (maybe introduce a &filename; ??) */
 
153
 
 
154
    if (lastpage < 0)  /* i.e. print the whole document */
 
155
        dstrcatf(cmd, " < %s", filename);
 
156
    else
 
157
    {
 
158
        if (!pdf_extract_pages(tmpfile, filename, firstpage, lastpage))
 
159
            rip_die(EXIT_STARVED, "Could not run ghostscript to extract the pages!\n");
 
160
        dstrcatf(cmd, " < %s", tmpfile);
 
161
    }
 
162
 
 
163
    result = start_renderer(cmd->data);
 
164
 
 
165
    if (lastpage > 0)
 
166
        unlink(tmpfile);
 
167
 
 
168
    return result;
 
169
}
 
170
 
 
171
static int render_pages_with_ghostscript(dstr_t *cmd,
 
172
                                         size_t start_gs_cmd,
 
173
                                         size_t end_gs_cmd,
 
174
                                         const char *filename,
 
175
                                         int firstpage,
 
176
                                         int lastpage)
 
177
{
 
178
    char *p;
 
179
 
 
180
    /* No need to create a temporary file, just give ghostscript the file and
 
181
     * first/last page on the command line */
 
182
 
 
183
    /* Some command lines want to read from stdin */
 
184
    for (p = &cmd->data[end_gs_cmd -1]; isspace(*p); p--)
 
185
        ;
 
186
    if (*p == '-')
 
187
        *p = ' ';
 
188
 
 
189
    dstrinsertf(cmd, end_gs_cmd, " %s ", filename);
 
190
 
 
191
    if (lastpage > 0)
 
192
        dstrinsertf(cmd, start_gs_cmd +2,
 
193
                    " -dFirstPage=%d -dLastPage=%d ",
 
194
                    firstpage, lastpage);
 
195
    else
 
196
        dstrinsertf(cmd, start_gs_cmd +2,
 
197
                    " -dFirstPage=%d ", firstpage);
 
198
 
 
199
    return start_renderer(cmd->data);
 
200
}
 
201
 
 
202
static int render_pages(const char *filename, int firstpage, int lastpage)
 
203
{
 
204
    dstr_t *cmd = create_dstr();
 
205
    size_t start, end;
 
206
    int result;
 
207
 
 
208
    build_commandline(optionset("currentpage"), cmd, 1);
 
209
 
 
210
    extract_command(&start, &end, cmd->data, "gs");
 
211
    if (start == end)
 
212
        /* command is not Ghostscript */
 
213
        result = render_pages_with_generic_command(cmd,
 
214
                                                   filename,
 
215
                                                   firstpage,
 
216
                                                   lastpage);
 
217
    else
 
218
        /* Ghostscript command, tell it which pages we want to render */
 
219
        result = render_pages_with_ghostscript(cmd,
 
220
                                               start,
 
221
                                               end,
 
222
                                               filename,
 
223
                                               firstpage,
 
224
                                               lastpage);
 
225
 
 
226
    free_dstr(cmd);
 
227
    return result;
 
228
}
 
229
 
 
230
static int print_pdf_file(const char *filename)
 
231
{
 
232
    int page_count, i;
 
233
    int firstpage;
 
234
 
 
235
    page_count = pdf_count_pages(filename);
 
236
 
 
237
    if (page_count <= 0)
 
238
        rip_die(EXIT_JOBERR, "Unable to determine number of pages, page count: %d\n", page_count);
 
239
    _log("File contains %d pages\n", page_count);
 
240
 
 
241
    optionset_copy_values(optionset("header"), optionset("currentpage"));
 
242
    optionset_copy_values(optionset("currentpage"), optionset("previouspage"));
 
243
    firstpage = 1;
 
244
    for (i = 1; i <= page_count; i++)
 
245
    {
 
246
        set_options_for_page(optionset("currentpage"), i);
 
247
        if (!optionset_equal(optionset("currentpage"), optionset("previouspage"), 1))
 
248
        {
 
249
            render_pages(filename, firstpage, i);
 
250
            firstpage = i;
 
251
        }
 
252
        optionset_copy_values(optionset("currentpage"), optionset("previouspage"));
 
253
    }
 
254
    if (firstpage == 1)
 
255
        render_pages(filename, 1, -1); /* Render the whole document */
 
256
    else
 
257
        render_pages(filename, firstpage, page_count);
 
258
 
 
259
    wait_for_renderer();
 
260
 
 
261
    return 1;
 
262
}
 
263
 
 
264
int print_pdf(FILE *s,
 
265
              const char *alreadyread,
 
266
              size_t len,
 
267
              const char *filename,
 
268
              size_t startpos)
 
269
{
 
270
    char tmpfilename[PATH_MAX] = "";
 
271
    int result;
 
272
 
 
273
    /* If reading from stdin, write everything into a temporary file */
 
274
    /* TODO don't do this if there aren't any pagerange-limited options */
 
275
    if (s == stdin)
 
276
    {
 
277
        int fd;
 
278
        FILE *tmpfile;
 
279
 
 
280
        snprintf(tmpfilename, PATH_MAX, "%s/foomatic-XXXXXX", temp_dir());
 
281
        fd = mkstemp(tmpfilename);
 
282
        if (fd < 0) {
 
283
            _log("Could not create temporary file: %s\n", strerror(errno));
 
284
            return EXIT_PRNERR_NORETRY_BAD_SETTINGS;
 
285
        }
 
286
 
 
287
        tmpfile = fdopen(fd, "r+");
 
288
        copy_file(tmpfile, stdin, alreadyread, len);
 
289
        fclose(tmpfile);
 
290
 
 
291
        filename = tmpfilename;
 
292
    }
 
293
 
 
294
    result = print_pdf_file(filename);
 
295
 
 
296
    if (!isempty(tmpfilename))
 
297
        unlink(tmpfilename);
 
298
 
 
299
    return result;
 
300
}
 
301