2%GHOSTSCRIPT Calls a local GhostScript executable with the input command
7% Attempts to locate a
ghostscript executable,
finally asking the user to
8% specify the directory ghostcript was installed into. The resulting path
9% is stored
for future reference.
11% Once found, the executable is called with the input command
string.
13% This function
requires a Ghostscript installation on your system.
14% You can download Ghostscript from http:
18% cmd - Command
string to be passed into
ghostscript.
21% status - 0 iff command ran without problem.
24% Copyright: Oliver Woodford, 2009-2015, Yair Altman 2015-
26% Thanks to Jonas Dorn
for the fix
for the title of the uigetdir window on Mac OS.
27% Thanks to Nathan Childress
for the fix to
default location on 64-bit Windows systems.
28% 27/04/11 - Find 64-bit Ghostscript on Windows. Thanks to Paul Durack and
29% Shaun Kline
for pointing out the issue
30% 04/05/11 - Thanks to David Chorlian
for pointing out an alternative
31% location
for gs on linux.
32% 12/12/12 - Add extra executable
name on Windows. Thanks to Ratish
33% Punnoose
for highlighting the issue.
34% 28/06/13 - Fix error
using GS 9.07 in Linux. Many thanks to Jannick
35% Steinbring
for proposing the fix.
36% 24/10/13 - Fix error
using GS 9.07 in Linux. Many thanks to Johannes
38% 23/01/14 - Add full path to
ghostscript.txt in warning. Thanks to Koen
39% Vermeer
for raising the issue.
40% 27/02/15 - If Ghostscript croaks, display suggested workarounds
41% 30/03/15 - Improved performance by caching status of GS path check,
if ok
42% 14/05/15 - Clarified warning message in
case GS path could not be saved
43% 29/05/15 - Avoid cryptic error in
case the ghostscipt path cannot be saved (issue #74)
44% 10/11/15 - Custom GS installation webpage
for MacOS. Thanks to Andy Hueni via FEX
45% 15/01/20 - Various message cleanups/fixes in
case of errors
52 % Display possible workarounds
for Ghostscript croaks
53 url1 =
'https://github.com/altmany/export_fig/issues/12#issuecomment-61467998'; % issue #12
54 url2 =
'https://github.com/altmany/export_fig/issues/20#issuecomment-63826270'; % issue #20
55 hg2_str =
'';
if using_hg2, hg2_str =
' or Matlab R2014a'; end
56 fprintf(2,
'Ghostscript error. Rolling back to GS 9.10%s may possibly solve this:\n * %s ', hg2_str,
hyperlink(url1));
58 fprintf(2,
'(GS 9.10)\n * %s (R2014a)',
hyperlink(url2));
62 url3 =
'https://github.com/altmany/export_fig/issues/27'; % issue #27
63 fprintf(2,
'Alternatively, this may possibly be due to a font path issue:\n * %s\n\n',
hyperlink(url3));
65 % TODO: in Unix/Mac,
find a way to automatically determine whether to use
"export" (bash) or
"setenv" (csh/tcsh)
67 url = [mfilename
'.m'];
69 fpath =
which(mfilename);
70 if isempty(fpath), fpath = [mfilename(
'fullpath')
'.m']; end
71 url = [
'<a href="matlab:opentoline(''' fpath
''',201)
">' fpath '</a>'];
73 fprintf(2, 'Alternatively, if you are using csh, modify shell_cmd from "export ...
" to "setenv ...
"\nat the bottom of %s\n\n', url);
79function path_ = gs_path
81 % Start with the currently set path
82 path_ = user_string('ghostscript');
83 % Check the path works
84 if check_gs_path(path_)
87 % Check whether the binary is on the path
89 bin = {'gswin32c.exe', 'gswin64c.exe', 'gs'};
95 if check_store_gs_path(path_)
99 % Search the obvious places
101 default_location = 'C:\Program Files\gs\';
102 dir_list = dir(default_location);
104 default_location = 'C:\Program Files (x86)\gs\'; % Possible location on 64-bit systems
105 dir_list = dir(default_location);
107 executable = {'\bin\gswin32c.exe', '\bin\gswin64c.exe'};
109 % If there are multiple versions, use the newest
110 for a = 1:numel(dir_list)
111 ver_num2 = sscanf(dir_list(a).name, 'gs%g');
112 if ~isempty(ver_num2) && ver_num2 > ver_num
113 for b = 1:numel(executable)
114 path2 = [default_location dir_list(a).name executable{b}];
115 if exist(path2, 'file') == 2
122 if check_store_gs_path(path_)
126 executable = {'/usr/bin/gs', '/usr/local/bin/gs'};
127 for a = 1:numel(executable)
128 path_ = executable{a};
129 if check_store_gs_path(path_)
134 % Ask the user to enter the path
136 if strncmp(computer, 'MAC', 3) % Is a Mac
137 % Give separate warning as the uigetdir dialogue box doesn't have a
139 uiwait(warndlg('Ghostscript installation not found - please locate the program.', 'Ghostscript'))
140 base = uigetdir('/', 'Ghostcript program location');
142 base = uigetdir('/', 'Ghostcript program not found - please locate it');
145 % User hit cancel or closed window
148 base = [base filesep]; %#ok<AGROW>
149 bin_dir = {'', ['bin' filesep], ['lib' filesep]};
150 for a = 1:numel(bin_dir)
152 path_ = [base bin_dir{a} bin{b}];
153 if exist(path_, 'file') == 2
154 if check_store_gs_path(path_)
162 url = 'http://pages.uoregon.edu/koch';
164 url = 'http://ghostscript.com';
166 error('Ghostscript:NotFound', 'Ghostscript not found. Have you installed it from %s ?', hyperlink(url));
169function good = check_store_gs_path(path_)
170 % Check the path is valid
171 good = check_gs_path(path_);
175 % Update the current default path to the path found
176 if ~user_string('ghostscript', path_)
177 %filename = fullfile(fileparts(which('user_string.m')), '.ignore', 'ghostscript.txt');
178 [unused, filename] = user_string('ghostscript'); %#ok<ASGLU>
179 warning('Ghostscript:path', '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_);
184function good = check_gs_path(path_)
188 elseif ~isequal(isOk,true)
189 % Check whether the path is valid
190 [status, message] = system([gs_command(path_) '-h']); %#ok<ASGLU>
196function cmd = gs_command(path_)
197 % Initialize any required system calls before calling ghostscript
198 % TODO: in Unix/Mac, find a way to automatically determine whether to use "export
" (bash) or "setenv
" (csh/tcsh)
201 shell_cmd = 'export LD_LIBRARY_PATH=""; '; % Avoids an error on Linux with GS 9.07
204 shell_cmd = 'export DYLD_LIBRARY_PATH=""; '; % Avoids an error on Mac with GS 9.07
206 % Construct the command string
207 cmd = sprintf('%s"%s
" ', shell_cmd, path_);
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared library directories.
function find(in vendor)
Return a list of scalar MATLAB strings containing the paths to all detected mpiexec binaries installe...
function ghostscript(in cmd)
function gs_command(in path_)
function check_store_gs_path(in path_)
function check_gs_path(in path_)
function hyperlink(in url, in label, in msg)
function using_hg2(in fig)
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...