Back to index

texmacs  1.0.7.15
gs_utilities.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : gs_utilities.mm
00004 * DESCRIPTION: Utilities for Ghostscript
00005 * COPYRIGHT  : (C) 2010-2012 David Michel, Joris van der Hoeven, Denis Raux
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 "tm_configure.hpp"
00013 #ifdef USE_GS
00014 
00015 #include "gs_utilities.hpp"
00016 #include "analyze.hpp"
00017 #include "file.hpp"
00018 
00019 string
00020 gs_prefix () {
00021 #if defined (__MINGW__) || defined (__MINGW32__)
00022   static string cmd; // no need to resolve each time
00023   if (cmd == "") {
00024     url gs= resolve_in_path ("gswin32c");
00025     if(is_none(gs))
00026       gs= url_system (get_env ("TEXMACS_PATH")) * "bin" * "gswin32c";
00027     cmd= sys_concretize (gs) * " ";
00028   }
00029   return copy (cmd);
00030 #else
00031   return "gs ";
00032 #endif
00033 }
00034 
00035 bool
00036 gs_supports (url image) {
00037   string s= suffix (image);
00038   if (s == "ps" || s == "eps" || s == "pdf") return true;
00039   return false;
00040 }
00041 
00042 bool
00043 gs_image_size_sub (string buf, int& w_pt, int& h_pt) {
00044   int pos= search_forwards ("\n%%BoundingBox: ", buf);
00045   if (pos < 0) search_forwards ("%%BoundingBox: ", buf);
00046   if (pos < 0) return false;
00047   if (buf[pos] == '\n') pos++;
00048   bool ok= read (buf, pos, "%%BoundingBox: ");
00049   int x1, y1, x2, y2;
00050   skip_spaces (buf, pos);
00051   ok= ok && read_int (buf, pos, x1);
00052   skip_spaces (buf, pos);
00053   ok= ok && read_int (buf, pos, y1);
00054   skip_spaces (buf, pos);
00055   ok= ok && read_int (buf, pos, x2);
00056   skip_spaces (buf, pos);
00057   ok= ok && read_int (buf, pos, y2);
00058   if (!ok) return false;
00059   w_pt= x2-x1;
00060   h_pt= y2-y1;
00061   return true;
00062 }
00063 
00064 void
00065 gs_image_size (url image, int& w_pt, int& h_pt) {
00066   string buf;
00067   bool err= load_string (image, buf, false);
00068   if (!err && gs_image_size_sub (buf, w_pt, h_pt)) return;
00069   if (!err) {
00070     string cmd= gs_prefix ();
00071     cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER -dEPSCrop -sDEVICE=bbox ";
00072     cmd << sys_concretize (image);
00073     buf= eval_system (cmd);
00074   }
00075   if (!err && gs_image_size_sub (buf, w_pt, h_pt)) return;
00076   cerr << "TeXmacs Cannot read image file '" << image << "'"
00077        << " in gs_image_size" << LF;
00078   w_pt= 0; h_pt= 0;
00079 }
00080 
00081 void ps_bounding_box (url image, int& x1, int& y1, int& x2, int& y2);
00082 
00083 static bool
00084 use_converts (url image) {
00085 #ifdef __MINGW__
00086   (void) image; return false;
00087 #else
00088   // NOTE: determine whether we should use image magick.
00089   // Indeed, EPSCrop unfortunately does not correctly handle
00090   // non trivial offsets of bounding boxes
00091   static bool has_image_magick= exists_in_path ("convert");
00092   int bx1, by1, bx2, by2;
00093   ps_bounding_box (image, bx1, by1, bx2, by2);
00094   return has_image_magick && (bx1 != 0 || by1 != 0);
00095 #endif
00096 }
00097 
00098 void
00099 gs_to_png (url image, url png, int w, int h) {
00100   if (use_converts (image)) {
00101     string cmd= "convert ";
00102     cmd << "-geometry " << as_string (w) << "x" << as_string (h) << "! ";  
00103     cmd << sys_concretize (image) << " ";
00104     cmd << sys_concretize (png);
00105     system (cmd);
00106   }
00107   else {
00108     string cmd= gs_prefix ();
00109     cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER ";
00110     cmd << "-sDEVICE=png16m -dGraphicsAlphaBits=4 -dEPSCrop ";
00111     cmd << "-g" << as_string (w) << "x" << as_string (h) << " ";
00112     int bbw, bbh;
00113     int rw, rh;
00114     gs_image_size (image, bbw, bbh);
00115     rw= (w*72-1)/bbw+1;
00116     rh= (h*72-1)/bbh+1;
00117     cmd << "-r" << as_string (rw) << "x" << as_string (rh) << " ";  
00118     cmd << "-sOutputFile=" << sys_concretize (png) << " ";
00119     cmd << sys_concretize (image);
00120     system (cmd);
00121   }
00122 }
00123 
00124 void
00125 gs_to_eps (url image, url eps) {
00126   if (use_converts (image)) {
00127     string cmd= "convert ";
00128     cmd << sys_concretize (image) << " ";
00129     cmd << sys_concretize (eps);
00130     system (cmd);
00131   }
00132   else {
00133     string cmd= gs_prefix ();
00134     cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER ";
00135     cmd << "-sDEVICE=epswrite -dEPSCrop ";
00136     cmd << "-sOutputFile=" << sys_concretize (eps) << " ";
00137     cmd << sys_concretize (image);
00138     system (cmd);
00139   }
00140 }
00141 
00142 void
00143 gs_to_pdf (url doc, url pdf) {
00144   string cmd= gs_prefix ();
00145   cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite ";
00146   cmd << "-sOutputFile=" << sys_concretize (pdf) << " ";
00147   cmd << sys_concretize (doc);
00148   cmd << " -c \"[ /Title (" << as_string (tail(pdf)) << ") /DOCINFO pdfmark\" ";
00149 
00150   // NOTE: when converting from ps to pdf the title of the document is 
00151   // incorrectly referring to the name of the temporary file
00152   // so we add some PS code to override the PDF document title with
00153   // the name of the PDF file.
00154 
00155   system (cmd);
00156 }
00157 
00158 void
00159 tm_gs (url image) {
00160   string cmd= gs_prefix ();
00161   cmd << "-q -sDEVICE=x11alpha -dBATCH -dNOPAUSE -dSAFER -dNOEPS ";
00162   cmd << sys_concretize (image);
00163   system (cmd);
00164 }
00165 
00166 #endif