void gs_to_ps (url doc, url ps, bool landscape, double paper_h, double paper_w) { if (DEBUG_CONVERT) debug_convert << "gs_to_ps"<<LF; string cmd= gs_prefix (); cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=ps2write "; if (landscape) cmd << "-dDEVICEWIDTHPOINTS=" << as_string ((int) (28.36*paper_h+ 0.5)) << " -dDEVICEHEIGHTPOINTS=" << as_string ((int) (28.36*paper_w+ 0.5)); else cmd << "-dDEVICEWIDTHPOINTS=" << as_string ((int) (28.36*paper_w+ 0.5)) << " -dDEVICEHEIGHTPOINTS=" << as_string ((int) (28.36*paper_h+ 0.5)); cmd << " -sOutputFile=" << sys_concretize (ps) << " "; cmd << sys_concretize (doc); cmd << " -c \"[ /Title (" << as_string (tail(ps)) << ") /DOCINFO pdfmark\" "; // NOTE: when converting from pdf to ps the title of the document is // incorrectly referring to the name of the temporary file // so we add some PS code to override the PS document title with // the name of the PS file. system (cmd); if (DEBUG_CONVERT) debug_convert << cmd <<LF <<"ps generated? "<< exists(ps)<<LF; }
void gs_to_png (url image, url png, int w, int h) { if (use_converts (image)) { string cmd= "convert "; cmd << "-density 300x300 -geometry " << as_string (w) << "x" << as_string (h) << "! "; cmd << sys_concretize (image) << " "; cmd << sys_concretize (png); system (cmd); } else { string cmd= gs_prefix (); cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER "; cmd << "-sDEVICE=png16m -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -dEPSCrop "; cmd << "-g" << as_string (w) << "x" << as_string (h) << " "; int bbw, bbh; int rw, rh; gs_image_size (image, bbw, bbh); rw= (w*72-1)/bbw+1; rh= (h*72-1)/bbh+1; cmd << "-r" << as_string (rw) << "x" << as_string (rh) << " "; cmd << "-sOutputFile=" << sys_concretize (png) << " "; cmd << sys_concretize (image); system (cmd); } }
// This conversion is appropriate for eps images // (originally implemented in pdf_image_rep::flush) void gs_to_pdf (url image, url pdf, int w, int h) { string cmd; if (DEBUG_CONVERT) debug_convert << "(eps) gs_to_pdf"<<LF; string s= suffix (image); // take care of properly handling the bounding box // the resulting pdf image will always start at 0,0. int bx1, by1, bx2, by2; // bounding box ps_bounding_box(image, bx1, by1, bx2, by2); double scale_x = w/((double)(bx2-bx1)); double scale_y = h/((double)(by2-by1)); cmd= gs_prefix(); cmd << " -dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite "; cmd << "-dAutoRotatePages=/None "; cmd << "-dCompatibilityLevel=" << pdf_version () << " "; cmd << " -sOutputFile=" << sys_concretize(pdf) << " "; cmd << " -c \" << /PageSize [ " << as_string(bx2-bx1) << " " << as_string(by2-by1) << " ] >> setpagedevice gsave " << as_string(-bx1) << " " << as_string(-by1) << " translate " << as_string(scale_x) << " " << as_string(scale_y) << " scale \""; cmd << " -f " << sys_concretize (image); cmd << " -c \" grestore \" "; // debug_convert << cmd << LF; system(cmd); if (DEBUG_CONVERT) debug_convert << cmd << LF << "pdf generated? " << exists(pdf) << LF; }
void tm_gs (url image) { string cmd= gs_prefix (); cmd << "-q -sDEVICE=x11alpha -dBATCH -dNOPAUSE -dSAFER -dNOEPS "; cmd << sys_concretize (image); system (cmd); }
bool gs_to_png (url image, url png, int w, int h) { //Achtung! w,h in pixels string cmd; if (DEBUG_CONVERT) debug_convert << "gs_to_png using gs"<<LF; cmd= gs_prefix (); cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER "; cmd << "-sDEVICE=png16m -dGraphicsAlphaBits=4 -dTextAlphaBits=4 "; cmd << "-g" << as_string (w) << "x" << as_string (h) << " "; cmd << "-sOutputFile=" << sys_concretize (png) << " "; int bbw, bbh; int rw, rh; int bx1, by1, bx2, by2; if (suffix(image) == "pdf") image_size (image, bbw, bbh); //don't call gs_PDFimage_size //in order to benefit from caching else { ps_bounding_box (image, bx1, by1, bx2, by2); //same comment bbw=bx2-bx1; bbh=by2-by1; } rw=(w*72)/bbw; rh=(h*72)/bbh; cmd << "-r" << as_string (rw) << "x" << as_string (rh) << " "; if (DEBUG_CONVERT) debug_convert << "w="<<w<<" h="<<h<<LF << "bbw="<<bbw<<" bbh="<<bbh<<LF <<" res ="<<rw<<" * "<<rh <<LF; if (suffix(image) == "pdf") { cmd << "-dUseCropBox "; // old gs versions (<9.0 ?) fail if CropBox not explicitly defined cmd << sys_concretize (image); } else { //don't use -dEPSCrop which works incorrectly if (bx1 != 0 || by1 != 0) cmd << "-c \" "<< as_string (-bx1) << " "<< as_string (-by1) <<" translate gsave \" -f " << sys_concretize (image) << " -c \" grestore \""; } string ans= eval_system (cmd); if (DEBUG_CONVERT) debug_convert << cmd <<LF <<"answer :"<<ans <<LF; if (! exists(png)) { convert_error << "gs_to_png failed for "<< image <<LF; return false; } return true; }
void gs_to_eps (url image, url eps) { if (use_converts (image)) { string cmd= "convert "; cmd << sys_concretize (image) << " "; cmd << sys_concretize (eps); system (cmd); } else { string cmd= gs_prefix (); cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER "; cmd << "-sDEVICE=epswrite -dEPSCrop "; cmd << "-sOutputFile=" << sys_concretize (eps) << " "; cmd << sys_concretize (image); system (cmd); } }
void gs_to_pdf (url doc, url pdf, bool landscape, double paper_h, double paper_w) { string cmd= gs_prefix (); cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite "; if (landscape) cmd << "-dDEVICEWIDTHPOINTS=" << as_string ((int) (28.36*paper_h+ 0.5)) << " -dDEVICEHEIGHTPOINTS=" << as_string ((int) (28.36*paper_w+ 0.5)); else cmd << "-dDEVICEWIDTHPOINTS=" << as_string ((int) (28.36*paper_w+ 0.5)) << " -dDEVICEHEIGHTPOINTS=" << as_string ((int) (28.36*paper_h+ 0.5)); cmd << " -sOutputFile=" << sys_concretize (pdf) << " "; cmd << sys_concretize (doc); cmd << " -c \"[ /Title (" << as_string (tail(pdf)) << ") /DOCINFO pdfmark\" "; // NOTE: when converting from ps to pdf the title of the document is // incorrectly referring to the name of the temporary file // so we add some PS code to override the PDF document title with // the name of the PDF file. system (cmd); }
void gs_to_eps (url image, url eps) { //this should be used mostly for pdf->eps conversion. string cmd; int bx1, by1, bx2, by2; // bounding box if (DEBUG_CONVERT) debug_convert << "gs_to_eps"<<LF; cmd= gs_prefix (); cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER "; cmd << "-sDEVICE="<<eps_device (); cmd << " -sOutputFile=" << sys_concretize (eps) << " "; if (suffix(image) == "pdf") { image_size (image, bx2, by2); bx1=by1=0; cmd << "-dUseCropBox " << " -dDEVICEWIDTHPOINTS=" << as_string (bx2) << " -dDEVICEHEIGHTPOINTS=" << as_string (by2)<<" " << sys_concretize (image); } else { ps_bounding_box (image, bx1, by1, bx2, by2); cmd << " -dDEVICEWIDTHPOINTS=" << as_string (bx2-bx1) << " -dDEVICEHEIGHTPOINTS=" << as_string (by2-by1)<<" "; //don't use -dEPSCrop which works incorrectly if (bx1 != 0 || by1 != 0) cmd << "-c \" "<< as_string (-bx1) << " " << as_string (-by1) << " translate gsave \" " << sys_concretize (image) << " -c \" grestore \""; } string ans= eval_system (cmd); if (DEBUG_CONVERT) debug_convert << cmd <<LF <<"answer :"<<ans <<LF <<"eps generated? "<< exists(eps)<<LF; // eps(2)write and bbox devices do a "smart" job of finding the boundingbox on their own, // possibly changing the original margins/aspect ratio defined by the pdf CropBox|MediaBox // here were restore the original size. gs_fix_bbox (eps, 0, 0, bx2-bx1, by2-by1); }
string gs_prefix () { #if defined (__MINGW__) || defined (__MINGW32__) static string cmd; // no need to resolve each time if (cmd == "") { url gs= resolve_in_path ("gswin32c"); if(is_none(gs)) gs= url_system (get_env ("TEXMACS_PATH")) * "bin" * "gswin32c"; cmd= sys_concretize (gs) * " "; } return copy (cmd); #else return "gs "; #endif }
void gs_image_size (url image, int& w_pt, int& h_pt) { string buf; bool err= load_string (image, buf, false); if (!err && gs_image_size_sub (buf, w_pt, h_pt)) return; if (!err) { string cmd= gs_prefix (); cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER -dEPSCrop -sDEVICE=bbox "; cmd << sys_concretize (image); buf= eval_system (cmd); } if (!err && gs_image_size_sub (buf, w_pt, h_pt)) return; convert_error << "Cannot read image file '" << image << "'" << " in gs_image_size" << LF; w_pt= 0; h_pt= 0; }
void gs_image_size (url image, int& w_pt, int& h_pt) { bool ok; if (suffix (image) == "pdf") ok= gs_PDFimage_size (image, w_pt, h_pt); else { if (DEBUG_CONVERT) debug_convert << "gs eps image size :"<<LF; int x1,y1,x2,y2; string buf; ok= !load_string (image, buf, false); if (ok) { //try finding Bounding box in file: ok= ps_read_bbox (buf, x1, y1, x2, y2); if (!ok) { // bbox not found ask gs to compute one : string cmd= gs_prefix (); cmd << "-dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=bbox "; //Note: bbox device does a "smart" job of finding the cropbox on its own removing blank margins (*even* on eps files) //this is ok if we are reading a ps page // real eps pages with proper bounding boxes have been recognized before this and will have their BoundingBox respected cmd << sys_concretize (image); buf= eval_system (cmd); if (DEBUG_CONVERT) debug_convert << "gs cmd :"<<cmd<<LF <<"answer :"<< buf ; ok= ps_read_bbox (buf, x1, y1, x2, y2); } if (ok) { w_pt= x2-x1; h_pt= y2-y1; set_imgbox_cache(image->t, w_pt, h_pt, x1, y1); } } } if (!ok) { convert_error << "Cannot read image file '" << image << "'" << " in gs_image_size" << LF; w_pt= 0; h_pt= 0; } }
inline void system (string which, url u1, const char* sep, url u2) { system (which * " " * sys_concretize (u1) * " " * sep * " " * sys_concretize (u2)); }
inline void system (string which, url u1, const char* post) { system (which * " " * sys_concretize (u1) * " " * post); }
inline void system (string which, url u1, url u2) { system (which * " " * sys_concretize (u1) * " " * sys_concretize (u2)); }
bool gs_PDFimage_size (url image, int& w_pt, int& h_pt) { if (DEBUG_CONVERT) debug_convert << "gs PDF image size :"<<LF; string buf; string cmd= gs_prefix (); cmd << "-dNODISPLAY -q -sFile="; cmd << sys_concretize (image); cmd <<" "<<sys_concretize ("$TEXMACS_PATH/misc/convert/pdf_info.ps"); buf= eval_system (cmd); if (DEBUG_CONVERT) debug_convert << "gs cmd :"<<cmd<<LF <<"answer :"<< buf ; //if CropBox is defined, then use it, else Mediabox string type="CropBox"; int pos= search_forwards ("CropBox: [", buf); if (pos < 0) { type="MediaBox"; pos= search_forwards ("MediaBox: [", buf); if (pos < 0) { if (DEBUG_CONVERT) debug_convert << "CropBox|MediaBox not found"<<LF; return false; } } bool ok= read (buf, pos, type*": ["); double X1, Y1, X2, Y2; int x1, y1, x2, y2; skip_spaces (buf, pos); ok= read_double (buf, pos, X1) && ok; x1= (int) floor (X1); skip_spaces (buf, pos); ok= read_double (buf, pos, Y1) && ok; y1= (int) floor (Y1); skip_spaces (buf, pos); ok= read_double (buf, pos, X2) && ok; x2= (int) ceil (X2); skip_spaces (buf, pos); ok= read_double (buf, pos, Y2) && ok; y2= (int) ceil (Y2); if (!ok) { if (DEBUG_CONVERT) debug_convert << "box dims not found"<<LF; return false; } w_pt= x2-x1; h_pt= y2-y1; pos= search_forwards ("Rotate =", buf); ok= read (buf, pos, "Rotate ="); int rot; if (ok) { skip_spaces (buf, pos); ok = read_int (buf, pos, rot) ; if (ok) { rot = rot%360; if (rot < 0) rot +=360; if ((rot % 180) == 90 ) {//the image is rotated : swap axes lengths if (DEBUG_CONVERT) debug_convert << "Rotate ="<<rot<<LF; h_pt= x2-x1; w_pt= y2-y1; } } else { if (DEBUG_CONVERT) debug_convert << "Rotate not found"<<LF; return false; } } if (DEBUG_CONVERT) debug_convert << type<< " size ="<<w_pt<<" x "<< h_pt <<LF; return true; }