Home > data-vis > supporting_functions > export_fig > ghostscript.m

ghostscript

PURPOSE ^

GHOSTSCRIPT Calls a local GhostScript executable with the input command

SYNOPSIS ^

function varargout = ghostscript(cmd)

DESCRIPTION ^

GHOSTSCRIPT  Calls a local GhostScript executable with the input command

 Example:
   [status result] = ghostscript(cmd)

 Attempts to locate a ghostscript executable, finally asking the user to
 specify the directory ghostcript was installed into. The resulting path
 is stored for future reference.
 
 Once found, the executable is called with the input command string.

 This function requires that you have Ghostscript installed on your
 system. You can download this from: http://www.ghostscript.com

 IN:
   cmd - Command string to be passed into ghostscript.

 OUT:
   status - 0 iff command ran without problem.
   result - Output from ghostscript.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = ghostscript(cmd)
0002 %GHOSTSCRIPT  Calls a local GhostScript executable with the input command
0003 %
0004 % Example:
0005 %   [status result] = ghostscript(cmd)
0006 %
0007 % Attempts to locate a ghostscript executable, finally asking the user to
0008 % specify the directory ghostcript was installed into. The resulting path
0009 % is stored for future reference.
0010 %
0011 % Once found, the executable is called with the input command string.
0012 %
0013 % This function requires that you have Ghostscript installed on your
0014 % system. You can download this from: http://www.ghostscript.com
0015 %
0016 % IN:
0017 %   cmd - Command string to be passed into ghostscript.
0018 %
0019 % OUT:
0020 %   status - 0 iff command ran without problem.
0021 %   result - Output from ghostscript.
0022 
0023 % Copyright: Oliver Woodford, 2009-2015, Yair Altman 2015-
0024 %{
0025 % Thanks to Jonas Dorn for the fix for the title of the uigetdir window on Mac OS.
0026 % Thanks to Nathan Childress for the fix to default location on 64-bit Windows systems.
0027 % 27/04/11 - Find 64-bit Ghostscript on Windows. Thanks to Paul Durack and
0028 %            Shaun Kline for pointing out the issue
0029 % 04/05/11 - Thanks to David Chorlian for pointing out an alternative
0030 %            location for gs on linux.
0031 % 12/12/12 - Add extra executable name on Windows. Thanks to Ratish
0032 %            Punnoose for highlighting the issue.
0033 % 28/06/13 - Fix error using GS 9.07 in Linux. Many thanks to Jannick
0034 %            Steinbring for proposing the fix.
0035 % 24/10/13 - Fix error using GS 9.07 in Linux. Many thanks to Johannes
0036 %            for the fix.
0037 % 23/01/14 - Add full path to ghostscript.txt in warning. Thanks to Koen
0038 %            Vermeer for raising the issue.
0039 % 27/02/15 - If Ghostscript croaks, display suggested workarounds
0040 % 30/03/15 - Improved performance by caching status of GS path check, if ok
0041 % 14/05/15 - Clarified warning message in case GS path could not be saved
0042 % 29/05/15 - Avoid cryptic error in case the ghostscipt path cannot be saved (issue #74)
0043 % 10/11/15 - Custom GS installation webpage for MacOS. Thanks to Andy Hueni via FEX
0044 %}
0045 
0046     try
0047         % Call ghostscript
0048         [varargout{1:nargout}] = system([gs_command(gs_path()) cmd]);
0049     catch err
0050         % Display possible workarounds for Ghostscript croaks
0051         url1 = 'https://github.com/altmany/export_fig/issues/12#issuecomment-61467998';  % issue #12
0052         url2 = 'https://github.com/altmany/export_fig/issues/20#issuecomment-63826270';  % issue #20
0053         hg2_str = ''; if using_hg2, hg2_str = ' or Matlab R2014a'; end
0054         fprintf(2, 'Ghostscript error. Rolling back to GS 9.10%s may possibly solve this:\n * <a href="%s">%s</a> ',hg2_str,url1,url1);
0055         if using_hg2
0056             fprintf(2, '(GS 9.10)\n * <a href="%s">%s</a> (R2014a)',url2,url2);
0057         end
0058         fprintf('\n\n');
0059         if ismac || isunix
0060             url3 = 'https://github.com/altmany/export_fig/issues/27';  % issue #27
0061             fprintf(2, 'Alternatively, this may possibly be due to a font path issue:\n * <a href="%s">%s</a>\n\n',url3,url3);
0062             % issue #20
0063             fpath = which(mfilename);
0064             if isempty(fpath), fpath = [mfilename('fullpath') '.m']; end
0065             fprintf(2, 'Alternatively, if you are using csh, modify shell_cmd from "export..." to "setenv ..."\nat the bottom of <a href="matlab:opentoline(''%s'',174)">%s</a>\n\n',fpath,fpath);
0066         end
0067         rethrow(err);
0068     end
0069 end
0070 
0071 function path_ = gs_path
0072     % Return a valid path
0073     % Start with the currently set path
0074     path_ = user_string('ghostscript');
0075     % Check the path works
0076     if check_gs_path(path_)
0077         return
0078     end
0079     % Check whether the binary is on the path
0080     if ispc
0081         bin = {'gswin32c.exe', 'gswin64c.exe', 'gs'};
0082     else
0083         bin = {'gs'};
0084     end
0085     for a = 1:numel(bin)
0086         path_ = bin{a};
0087         if check_store_gs_path(path_)
0088             return
0089         end
0090     end
0091     % Search the obvious places
0092     if ispc
0093         default_location = 'C:\Program Files\gs\';
0094         dir_list = dir(default_location);
0095         if isempty(dir_list)
0096             default_location = 'C:\Program Files (x86)\gs\'; % Possible location on 64-bit systems
0097             dir_list = dir(default_location);
0098         end
0099         executable = {'\bin\gswin32c.exe', '\bin\gswin64c.exe'};
0100         ver_num = 0;
0101         % If there are multiple versions, use the newest
0102         for a = 1:numel(dir_list)
0103             ver_num2 = sscanf(dir_list(a).name, 'gs%g');
0104             if ~isempty(ver_num2) && ver_num2 > ver_num
0105                 for b = 1:numel(executable)
0106                     path2 = [default_location dir_list(a).name executable{b}];
0107                     if exist(path2, 'file') == 2
0108                         path_ = path2;
0109                         ver_num = ver_num2;
0110                     end
0111                 end
0112             end
0113         end
0114         if check_store_gs_path(path_)
0115             return
0116         end
0117     else
0118         executable = {'/usr/bin/gs', '/usr/local/bin/gs'};
0119         for a = 1:numel(executable)
0120             path_ = executable{a};
0121             if check_store_gs_path(path_)
0122                 return
0123             end
0124         end
0125     end
0126     % Ask the user to enter the path
0127     while true
0128         if strncmp(computer, 'MAC', 3) % Is a Mac
0129             % Give separate warning as the uigetdir dialogue box doesn't have a
0130             % title
0131             uiwait(warndlg('Ghostscript not found. Please locate the program.'))
0132         end
0133         base = uigetdir('/', 'Ghostcript not found. Please locate the program.');
0134         if isequal(base, 0)
0135             % User hit cancel or closed window
0136             break;
0137         end
0138         base = [base filesep]; %#ok<AGROW>
0139         bin_dir = {'', ['bin' filesep], ['lib' filesep]};
0140         for a = 1:numel(bin_dir)
0141             for b = 1:numel(bin)
0142                 path_ = [base bin_dir{a} bin{b}];
0143                 if exist(path_, 'file') == 2
0144                     if check_store_gs_path(path_)
0145                         return
0146                     end
0147                 end
0148             end
0149         end
0150     end
0151     if ismac
0152         error('Ghostscript not found. Have you installed it (http://pages.uoregon.edu/koch)?');
0153     else
0154         error('Ghostscript not found. Have you installed it from www.ghostscript.com?');
0155     end
0156 end
0157 
0158 function good = check_store_gs_path(path_)
0159     % Check the path is valid
0160     good = check_gs_path(path_);
0161     if ~good
0162         return
0163     end
0164     % Update the current default path to the path found
0165     if ~user_string('ghostscript', path_)
0166         filename = fullfile(fileparts(which('user_string.m')), '.ignore', 'ghostscript.txt');
0167         warning('Path to ghostscript installation could not be saved in %s (perhaps a permissions issue). You can manually create this file and set its contents to %s, to improve performance in future invocations (this warning is safe to ignore).', filename, path_);
0168         return
0169     end
0170 end
0171 
0172 function good = check_gs_path(path_)
0173     persistent isOk
0174     if isempty(path_)
0175         isOk = false;
0176     elseif ~isequal(isOk,true)
0177         % Check whether the path is valid
0178         [status, message] = system([gs_command(path_) '-h']); %#ok<ASGLU>
0179         isOk = status == 0;
0180     end
0181     good = isOk;
0182 end
0183 
0184 function cmd = gs_command(path_)
0185     % Initialize any required system calls before calling ghostscript
0186     % TODO: in Unix/Mac, find a way to determine whether to use "export" (bash) or "setenv" (csh/tcsh)
0187     shell_cmd = '';
0188     if isunix
0189         shell_cmd = 'export LD_LIBRARY_PATH=""; '; % Avoids an error on Linux with GS 9.07
0190     end
0191     if ismac
0192         shell_cmd = 'export DYLD_LIBRARY_PATH=""; ';  % Avoids an error on Mac with GS 9.07
0193     end
0194     % Construct the command string
0195     cmd = sprintf('%s"%s" ', shell_cmd, path_);
0196 end

Generated on Wed 24-May-2017 00:00:56 by m2html © 2005