Back to index

tetex-bin  3.0
psimage.c
Go to the documentation of this file.
00001 /*  $Header$
00002 
00003     This is dvipdfm, a DVI to PDF translator.
00004     Copyright (C) 1998, 1999 by Mark A. Wicks
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019     
00020     The author may be contacted via the e-mail address
00021 
00022        mwicks@kettering.edu
00023 */
00024 
00025 #include <stdio.h>
00026 #include "system.h"
00027 #include "config.h"
00028 #include "mem.h"
00029 #include "mfileio.h"
00030 #include "pdfobj.h"
00031 #include "psimage.h"
00032 #include "epdf.h"
00033 
00034 static char * distiller_template = NULL;
00035 
00036 void set_distiller_template (char *s) 
00037 {
00038   distiller_template = NEW (strlen(s)+1, char);
00039   strcpy (distiller_template, s);
00040   return;
00041 }
00042 
00043 #define need(n) { unsigned k=(n); \
00044                  if (size+k>max_size) { \
00045                      max_size += k+128; \
00046                      result=RENEW(result,max_size,char); \
00047                        }}
00048 
00049 static char *last_dot (char *s)
00050 {
00051   char *end;
00052   end = s+strlen(s);
00053   while (--end > s) {
00054     if (*end == '.')
00055       return end;
00056   }
00057   return NULL;
00058 }
00059 
00060 #ifdef HAVE_SYSTEM  /* No need to build a command line if we don't
00061                      have system() */
00062 static char *build_command_line (char *psname, char *pdfname)
00063 {
00064   char *result = NULL, *current;
00065   int size = 0, max_size = 0;
00066   if (distiller_template) {
00067     need(strlen(distiller_template)+1);
00068     for (current =distiller_template; *current != 0; current ++) {
00069       if (*current == '%') {
00070        switch (*(++current)) {
00071        case 'o': /* Output file name */
00072          need(strlen(pdfname));
00073          strcpy (result+size, pdfname);
00074          size+=strlen(pdfname);
00075          break;
00076        case 'i': /* Input filename */
00077          need(strlen(psname));
00078          strcpy (result+size, psname);
00079          size+=strlen(psname);
00080          break;
00081        case 'b': 
00082          {
00083            char *last;
00084            need(strlen(psname));
00085            if ((last = last_dot (psname))) {
00086              strncpy (result+size, psname, last-psname);
00087              size += last-psname;
00088            } else {
00089              strcpy (result+size, psname);
00090              size += strlen(psname);
00091            }
00092          }
00093        case 0:
00094          break;
00095        case '%':
00096          result[size++] = '%';
00097        }
00098       } else {
00099        result[size++] = *current;
00100       }
00101       result[size] = 0;
00102     }
00103   } else {
00104     fprintf (stderr, "\nConfig file contains no template to perform PS -> PDF conversion\n");
00105   }
00106   return result;
00107 }
00108 #endif
00109 
00110 pdf_obj *ps_include (char *file_name, 
00111                    struct xform_info *p,
00112                    char *res_name, double x_user, double y_user)
00113 {
00114 #ifdef HAVE_SYSTEM
00115   pdf_obj *result = NULL;
00116   char *tmp, *cmd;
00117   FILE *pdf_file = NULL;
00118   /* Get a full qualified tmp name */
00119   tmp = tmpnam (NULL);
00120   if ((cmd = build_command_line (file_name, tmp))) {
00121     if (!system (cmd) && (pdf_file = MFOPEN (tmp, FOPEN_RBIN_MODE))) {
00122       result = pdf_include_page (pdf_file, p, res_name);
00123     } else {
00124       fprintf (stderr, "\nConversion via ->%s<- failed\n", cmd);
00125     }
00126     if (pdf_file) {
00127       MFCLOSE (pdf_file);
00128       remove (tmp);
00129     }
00130     RELEASE (cmd);
00131   }
00132   return result;
00133 #else
00134   fprintf (stderr, "\n\nCannot include PS/EPS files unless you have and enable system() command.\n");
00135   return NULL;
00136 #endif
00137 }
00138 
00139 int check_for_ps (FILE *image_file) 
00140 {
00141   rewind (image_file);
00142   mfgets (work_buffer, WORK_BUFFER_SIZE, image_file);
00143   if (!strncmp (work_buffer, "%!", 2))
00144     return 1;
00145   return 0;
00146 }
00147 
00148 void psimage_close(void)
00149 {
00150   if (distiller_template)
00151     RELEASE (distiller_template);
00152 }