Back to index

texmacs  1.0.7.15
ghostscript.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : ghostscript.cpp
00004 * DESCRIPTION: interface with ghostscript
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "Ghostscript/ghostscript.hpp"
00013 #ifdef USE_GS
00014 #include "Ghostscript/gs_utilities.hpp"
00015 #endif
00016 
00017 #ifdef X11TEXMACS
00018 
00019 #include "file.hpp"
00020 #include "image_files.hpp"
00021 
00022 /******************************************************************************
00023 * Special hack for a bug in Ghostscript 6.50, 6.51 and 6.52
00024 ******************************************************************************/
00025 
00026 static int gs_type= -1;
00027   // -1: uninitialized
00028   //  0: OK
00029   //  1: with pixmap bug
00030 
00031 bool
00032 ghostscript_bugged () {
00033 #ifdef OS_WIN32
00034   return false;
00035 #else
00036   if (gs_type == -1) {
00037     string gs_version= var_eval_system ("gs --version");
00038     gs_type= 0;
00039     if ((gs_version == "6.50") ||
00040        (gs_version == "6.51") ||
00041        (gs_version == "6.52"))
00042       gs_type= 1;
00043   }
00044   return (gs_type == 1);
00045 #endif
00046 }
00047 
00048 /******************************************************************************
00049 * Drawing a postscript image file in a pixmap
00050 ******************************************************************************/
00051 
00052 static string
00053 encapsulate_postscript (string s) {
00054   int i, n= N(s);
00055   int last_begin= 0;
00056   string r;
00057   for (i=0; i<n; i++) {
00058     if ((s[i] != 's') || (i>(n-8)) || (s(i,i+8) != "showpage")) continue;
00059     if (i > last_begin) r << s (last_begin, i);
00060     i += 8;
00061     last_begin= i;
00062   }
00063   r << s (last_begin, i);
00064   return r;
00065 }
00066 
00067 void
00068 ghostscript_run (Display* dpy, Window gs_win, Pixmap pm,
00069                url image, SI w, SI h,
00070                double cx1, double cy1, double cx2, double cy2)
00071 {
00072   if (DEBUG_VERBOSE)
00073     cout << "TeXmacs] Running ghostscript " << image << "\n";
00074 
00075   int bx1, by1, bx2, by2;
00076   ps_bounding_box (image, bx1, by1, bx2, by2);
00077   int x1= bx1 + as_int (cx1 * (bx2 - bx1));
00078   int y1= by1 + as_int (cy1 * (by2 - by1));
00079   int x2= bx1 + as_int (cx2 * (bx2 - bx1));
00080   int y2= by1 + as_int (cy2 * (by2 - by1));
00081   // FIXME: this dirty hack is needed as a correction for the rendering
00082   // of patterns with a non-entire width or height. Without the hack,
00083   // white lines may appear at the borders of the patterns.
00084   // However the hack also causes normal images to be rendered
00085   // slightly incorrectly
00086   if (x1+1 < x2) x1++;
00087   if (y1+1 < y2) y1++;
00088   // End dirty hack
00089 
00090   if (ghostscript_bugged ()) {
00091     int scr  = DefaultScreen (dpy);
00092     int max_w= 2 * DisplayWidth (dpy, scr);
00093     int max_h= 2 * DisplayHeight (dpy, scr);
00094     w= min (w, max_w);
00095     h= min (h, max_h);
00096   }
00097 
00098 #ifndef OS_WIN32
00099   int win_id= (int) gs_win;
00100   int pix_id= (int) pm;
00101   if (ghostscript_bugged ()) set_env ("GHOSTVIEW", as_string (win_id));
00102   else set_env ("GHOSTVIEW", as_string (win_id) * " " * as_string (pix_id));
00103   Atom gh= XInternAtom (dpy, "GHOSTVIEW", false);
00104   Atom st= XA_STRING;
00105   double dpi_x= ((double) (w*72))/((double) (x2-x1));
00106   double dpi_y= ((double) (h*72))/((double) (y2-y1));
00107   string data=
00108     (ghostscript_bugged ()? as_string (pix_id): string ("0")) * " 0 " *
00109     as_string (x1) * " " * as_string (y1) * " " *
00110     as_string (x2) * " " * as_string (y2) * " " *
00111     as_string (dpi_x) * " " * as_string (dpi_y) * " " *
00112     "0 0 0 0";
00113   unsigned char* _data= (unsigned char*) as_charp (data);
00114   int _n= N(data);
00115   XChangeProperty (dpy, gs_win, gh, st, 8, PropModeReplace, _data, _n);
00116   tm_delete_array (_data);
00117   XSync(dpy, false);
00118 #endif
00119 
00120   string raw_ps, nice_ps;
00121   raw_ps= ps_load (image);
00122   nice_ps= encapsulate_postscript (raw_ps);
00123   url temp_name= url_temp ();
00124   save_string (temp_name, nice_ps, true);
00125 #ifdef USE_GS
00126   tm_gs (temp_name);
00127 #else
00128   system ("tm_gs", temp_name);
00129 #endif
00130   remove (temp_name);
00131 }
00132 
00133 #endif // X11TEXMACS
00134