- gint new_llx = 0;
- gint new_lly = 0;
- gint new_urx = 0;
- gint new_ury = 0;
- GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
- int new_pagesize = -1;
-
- g_return_if_fail (PS_IS_DOCUMENT (gs));
-
- if (new_pagesize == -1) {
- new_pagesize = DEFAULT_PAGE_SIZE;
- if (gs->doc) {
- /* If we have a document:
- * We use -- the page size (if specified)
- * or the doc. size (if specified)
- * or the page bbox (if specified)
- * or the bounding box
- */
- if ((page >= 0) && (gs->doc->numpages > page) &&
- (gs->doc->pages) && (gs->doc->pages[page].size)) {
- new_pagesize = gs->doc->pages[page].size - gs->doc->size;
- } else if (gs->doc->default_page_size != NULL) {
- new_pagesize = gs->doc->default_page_size - gs->doc->size;
- } else if ((page >= 0) &&
- (gs->doc->numpages > page) &&
- (gs->doc->pages) &&
- (gs->doc->pages[page].boundingbox[URX] >
- gs->doc->pages[page].boundingbox[LLX]) &&
- (gs->doc->pages[page].boundingbox[URY] >
- gs->doc->pages[page].boundingbox[LLY])) {
- new_pagesize = -1;
- } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
- (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
- new_pagesize = -1;
- }
- }
- }
-
- /* Compute bounding box */
- if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
- if ((page >= 0) &&
- (gs->doc->pages) &&
- (gs->doc->pages[page].boundingbox[URX] >
- gs->doc->pages[page].boundingbox[LLX]) &&
- (gs->doc->pages[page].boundingbox[URY] >
- gs->doc->pages[page].boundingbox[LLY])) {
- /* use page bbox */
- new_llx = gs->doc->pages[page].boundingbox[LLX];
- new_lly = gs->doc->pages[page].boundingbox[LLY];
- new_urx = gs->doc->pages[page].boundingbox[URX];
- new_ury = gs->doc->pages[page].boundingbox[URY];
- } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
- (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
- /* use doc bbox */
- new_llx = gs->doc->boundingbox[LLX];
- new_lly = gs->doc->boundingbox[LLY];
- new_urx = gs->doc->boundingbox[URX];
- new_ury = gs->doc->boundingbox[URY];
- }
- } else {
- if (new_pagesize < 0)
- new_pagesize = DEFAULT_PAGE_SIZE;
- new_llx = new_lly = 0;
- if (gs->doc && gs->doc->size &&
- (new_pagesize < gs->doc->numsizes)) {
- new_urx = gs->doc->size[new_pagesize].width;
- new_ury = gs->doc->size[new_pagesize].height;
- } else {
- new_urx = papersizes[new_pagesize].width;
- new_ury = papersizes[new_pagesize].height;
- }
- }
-
- if (new_urx <= new_llx)
- new_urx = papersizes[12].width;
- if (new_ury <= new_lly)
- new_ury = papersizes[12].height;
-
- *urx = new_urx;
- *ury = new_ury;
- *llx = new_llx;
- *lly = new_lly;
-}
-
-static void
-setup_page (PSDocument *gs, int page, double scale, int rotation)
-{
- gchar *buf;
- char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];
- int urx, ury, llx, lly;
-
- get_page_box (gs, page, &urx, &ury, &llx, &lly);
- g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
-
- buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
- 0L, rotation, llx, lly, urx, ury,
- scaled_dpi, scaled_dpi,
- 0, 0, 0, 0);
-
- gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
- 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
- g_free (buf);
-
- gdk_flush ();
-}
-
-static void
-close_pipe (int p[2])
-{
- if (p[0] != -1) {
- close (p[0]);
- }
- if (p[1] != -1) {
- close (p[1]);
- }
-}
-
-static gboolean
-is_interpreter_ready (PSDocument *gs)
-{
- return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
-}
-
-static void
-output (gpointer data, gint source, GdkInputCondition condition)
-{
- char buf[MAX_BUFSIZE + 1];
- guint bytes = 0;
- PSDocument *gs = PS_DOCUMENT(data);
-
- if (source == gs->interpreter_output) {
- bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
- if (bytes == 0) { /* EOF occurred */
- close (gs->interpreter_output);
- gs->interpreter_output = -1;
- gdk_input_remove (gs->interpreter_output_id);
- return;
- } else if (bytes == -1) {
- /* trouble... */
- interpreter_failed (gs, NULL);
- return;
- }
- if (gs->interpreter_err == -1) {
- interpreter_failed (gs, NULL);
- }
- } else if (source == gs->interpreter_err) {
- bytes = read (gs->interpreter_err, buf, MAX_BUFSIZE);
- if (bytes == 0) { /* EOF occurred */
- close (gs->interpreter_err);
- gs->interpreter_err = -1;
- gdk_input_remove (gs->interpreter_error_id);
- return;
- } else if (bytes == -1) {
- /* trouble... */
- interpreter_failed (gs, NULL);
- return;
- }
- if (gs->interpreter_output == -1) {
- interpreter_failed(gs, NULL);
- }
- }
-
- if (bytes > 0) {
- buf[bytes] = '\0';
- printf ("%s", buf);
- }
-}
-
-static void
-catchPipe (int i)
-{
- broken_pipe = True;
-}
-
-static void
-input(gpointer data, gint source, GdkInputCondition condition)
-{
- PSDocument *gs = PS_DOCUMENT(data);
- int bytes_written;
- void (*oldsig) (int);
- oldsig = signal(SIGPIPE, catchPipe);
-
- do {
- if (gs->buffer_bytes_left == 0) {
- /* Get a new section if required */
- if (gs->ps_input && gs->bytes_left == 0) {
- struct record_list *ps_old = gs->ps_input;
- gs->ps_input = ps_old->next;
- if (ps_old->close && NULL != ps_old->fp)
- fclose (ps_old->fp);
- g_free (ps_old);
- }
-
- /* Have to seek at the beginning of each section */
- if (gs->ps_input && gs->ps_input->seek_needed) {
- fseek (gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
- gs->ps_input->seek_needed = FALSE;
- gs->bytes_left = gs->ps_input->len;
- }
-
- if (gs->bytes_left > MAX_BUFSIZE) {
- gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
- MAX_BUFSIZE, gs->ps_input->fp);
- } else if (gs->bytes_left > 0) {
- gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
- gs->bytes_left, gs->ps_input->fp);
- } else {
- gs->buffer_bytes_left = 0;
- }
- if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
- interpreter_failed (gs, NULL); /* Error occurred */
- }
- gs->input_buffer_ptr = gs->input_buffer;
- gs->bytes_left -= gs->buffer_bytes_left;
- }
-
- if (gs->buffer_bytes_left > 0) {
- bytes_written = write (gs->interpreter_input,
- gs->input_buffer_ptr, gs->buffer_bytes_left);
-
- if (broken_pipe) {
- interpreter_failed (gs, g_strdup(_("Broken pipe.")));
- broken_pipe = FALSE;
- interpreter_failed (gs, NULL);
- } else if (bytes_written == -1) {
- if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
- interpreter_failed (gs, NULL); /* Something bad happened */
- }
- } else {
- gs->buffer_bytes_left -= bytes_written;
- gs->input_buffer_ptr += bytes_written;
- }
- }
- } while (gs->ps_input && gs->buffer_bytes_left == 0);
-
- signal (SIGPIPE, oldsig);
-
- if (gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
- if (gs->interpreter_input_id != 0) {
- gdk_input_remove (gs->interpreter_input_id);
- gs->interpreter_input_id = 0;
- }
- }
-}
-
-static int
-start_interpreter (PSDocument *gs)
-{
- int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
- int std_out[2]; /* pipe from interp stdout */
- int std_err[2]; /* pipe from interp stderr */
-
-#define NUM_ARGS 100
-#define NUM_GS_ARGS (NUM_ARGS - 20)
-#define NUM_ALPHA_ARGS 10
-
- char *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
- char **gs_args, **alpha_args = NULL;
- char **gv_env_vars = NULL;
- int argc = 0, i;
-
- if(!gs->gs_filename)
- return 0;
-
- stop_interpreter(gs);
-
- /* set up the args... */
- gs_path = g_find_program_in_path ("gs");
- gs_args = g_strsplit (gs_path, " ", NUM_GS_ARGS);
- g_free (gs_path);
- for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
- argv[argc] = gs_args[i];
- }
-
- alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
- for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
- argv[argc] = alpha_args[i];
- }
-
- argv[argc++] = "-dNOPAUSE";
- argv[argc++] = "-dQUIET";
- argv[argc++] = "-dSAFER";
-
- /* set up the pipes */
- if (gs->send_filename_to_gs) {
- argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
- argv[argc++] = "-c";
- argv[argc++] = "quit";
- } else {
- argv[argc++] = "-";
- }
-
- argv[argc++] = NULL;
-
- if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
- if (pipe (std_in) == -1) {
- g_critical ("Unable to open pipe to Ghostscript.");
- return -1;
- }
- }
-
- if (pipe (std_out) == -1) {
- close_pipe (std_in);
- return -1;
- }
-
- if (pipe(std_err) == -1) {
- close_pipe (std_in);
- close_pipe (std_out);
- return -1;
- }
-
- gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld;DISPLAY=%s",
- gdk_x11_drawable_get_xid (gs->pstarget),
- gdk_x11_drawable_get_xid (gs->bpixmap),
- gdk_display_get_name (gdk_drawable_get_display (gs->pstarget)));
-
- gs->interpreter_pid = fork ();
- switch (gs->interpreter_pid) {
- case -1: /* error */
- close_pipe (std_in);
- close_pipe (std_out);
- close_pipe (std_err);
- return -2;
- break;
- case 0: /* child */
- close (std_out[0]);
- dup2 (std_out[1], 1);
- close (std_out[1]);
-
- close (std_err[0]);
- dup2 (std_err[1], 2);
- close (std_err[1]);
-
- if (!gs->reading_from_pipe) {
- if (gs->send_filename_to_gs) {
- int stdinfd;
- /* just in case gs tries to read from stdin */
- stdinfd = open("/dev/null", O_RDONLY);
- if (stdinfd != 0) {
- dup2(stdinfd, 0);
- close(stdinfd);
- }
- } else {
- close (std_in[1]);
- dup2 (std_in[0], 0);
- close (std_in[0]);
- }
- }
-
- gv_env_vars = g_strsplit (gv_env, ";", -1);
- g_free (gv_env);
- for (i = 0; gv_env_vars[i]; i++) {
- putenv (gv_env_vars[i]);
- }
-
- /* change to directory where the input file is. This helps
- * with postscript-files which include other files using
- * a relative path */
- dir = g_path_get_dirname (gs->gs_filename);
- chdir (dir);
- g_free (dir);
-
- execvp (argv[0], argv);
-
- /* Notify error */
- g_critical ("Unable to execute [%s]\n", argv[0]);
- g_strfreev (gs_args);
- g_strfreev (alpha_args);
- g_strfreev (gv_env_vars);
- _exit (1);
- break;
- default: /* parent */
- if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
- int result;
- close (std_in[0]);
- /* use non-blocking IO for pipe to ghostscript */
- result = fcntl (std_in[1], F_GETFL, 0);
- fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
- gs->interpreter_input = std_in[1];
- } else {
- gs->interpreter_input = -1;
- }
- close (std_out[1]);
-
- gs->interpreter_output = std_out[0];
- close (std_err[1]);
- gs->interpreter_err = std_err[0];
- gs->interpreter_output_id =
- gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
- gs->interpreter_error_id =
- gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
- break;
- }
-
- return TRUE;
-}
-
-static void
-stop_interpreter(PSDocument * gs)
-{
- if (gs->interpreter_pid > 0) {
- int status = 0;
- kill (gs->interpreter_pid, SIGTERM);
- while ((wait(&status) == -1) && (errno == EINTR));
- gs->interpreter_pid = -1;
- if (status == 1) {
- gs->gs_status = _("Interpreter failed.");
- }
- }
-
- if (gs->interpreter_input >= 0) {
- close (gs->interpreter_input);
- gs->interpreter_input = -1;
- if (gs->interpreter_input_id != 0) {
- gdk_input_remove(gs->interpreter_input_id);
- gs->interpreter_input_id = 0;
- }
- while (gs->ps_input) {
- struct record_list *ps_old = gs->ps_input;
- gs->ps_input = gs->ps_input->next;
- if (ps_old->close && NULL != ps_old->fp)
- fclose (ps_old->fp);
- g_free (ps_old);
- }
- }