ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
ghostscript.m
Go to the documentation of this file.
1function varargout = ghostscript(cmd)
2%GHOSTSCRIPT Calls a local GhostScript executable with the input command
3%
4% Example:
5% [status result] = ghostscript(cmd)
6%
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.
10%
11% Once found, the executable is called with the input command string.
12%
13% This function requires a Ghostscript installation on your system.
14% You can download Ghostscript from http://ghostscript.com (Windows/Linux)
15% or http://pages.uoregon.edu/koch (MacOS).
16%
17% IN:
18% cmd - Command string to be passed into ghostscript.
19%
20% OUT:
21% status - 0 iff command ran without problem.
22% result - Output from ghostscript.
23
24% Copyright: Oliver Woodford, 2009-2015, Yair Altman 2015-
25%{
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
37% for the fix.
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
46%}
47
48 try
49 % Call ghostscript
50 [varargout{1:nargout}] = system([gs_command(gs_path()) cmd]);
51 catch err
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));
57 if using_hg2
58 fprintf(2, '(GS 9.10)\n * %s (R2014a)', hyperlink(url2));
59 end
60 fprintf('\n\n');
61 if ismac || isunix
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));
64 % issue #20
65 % TODO: in Unix/Mac, find a way to automatically determine whether to use "export" (bash) or "setenv" (csh/tcsh)
66 if isdeployed
67 url = [mfilename '.m'];
68 else
69 fpath = which(mfilename);
70 if isempty(fpath), fpath = [mfilename('fullpath') '.m']; end
71 url = ['<a href="matlab:opentoline(''' fpath ''',201)">' fpath '</a>'];
72 end
73 fprintf(2, 'Alternatively, if you are using csh, modify shell_cmd from "export ..." to "setenv ..."\nat the bottom of %s\n\n', url);
74 end
75 rethrow(err);
76 end
77end
78
79function path_ = gs_path
80 % Return a valid path
81 % Start with the currently set path
82 path_ = user_string('ghostscript');
83 % Check the path works
84 if check_gs_path(path_)
85 return
86 end
87 % Check whether the binary is on the path
88 if ispc
89 bin = {'gswin32c.exe', 'gswin64c.exe', 'gs'};
90 else
91 bin = {'gs'};
92 end
93 for a = 1:numel(bin)
94 path_ = bin{a};
95 if check_store_gs_path(path_)
96 return
97 end
98 end
99 % Search the obvious places
100 if ispc
101 default_location = 'C:\Program Files\gs\';
102 dir_list = dir(default_location);
103 if isempty(dir_list)
104 default_location = 'C:\Program Files (x86)\gs\'; % Possible location on 64-bit systems
105 dir_list = dir(default_location);
106 end
107 executable = {'\bin\gswin32c.exe', '\bin\gswin64c.exe'};
108 ver_num = 0;
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
116 path_ = path2;
117 ver_num = ver_num2;
118 end
119 end
120 end
121 end
122 if check_store_gs_path(path_)
123 return
124 end
125 else
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_)
130 return
131 end
132 end
133 end
134 % Ask the user to enter the path
135 while true
136 if strncmp(computer, 'MAC', 3) % Is a Mac
137 % Give separate warning as the uigetdir dialogue box doesn't have a
138 % title on MacOS
139 uiwait(warndlg('Ghostscript installation not found - please locate the program.', 'Ghostscript'))
140 base = uigetdir('/', 'Ghostcript program location');
141 else
142 base = uigetdir('/', 'Ghostcript program not found - please locate it');
143 end
144 if isequal(base, 0)
145 % User hit cancel or closed window
146 break;
147 end
148 base = [base filesep]; %#ok<AGROW>
149 bin_dir = {'', ['bin' filesep], ['lib' filesep]};
150 for a = 1:numel(bin_dir)
151 for b = 1:numel(bin)
152 path_ = [base bin_dir{a} bin{b}];
153 if exist(path_, 'file') == 2
154 if check_store_gs_path(path_)
155 return
156 end
157 end
158 end
159 end
160 end
161 if ismac
162 url = 'http://pages.uoregon.edu/koch';
163 else
164 url = 'http://ghostscript.com';
165 end
166 error('Ghostscript:NotFound', 'Ghostscript not found. Have you installed it from %s ?', hyperlink(url));
167end
168
169function good = check_store_gs_path(path_)
170 % Check the path is valid
171 good = check_gs_path(path_);
172 if ~good
173 return
174 end
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_);
180 return
181 end
182end
183
184function good = check_gs_path(path_)
185 persistent isOk
186 if isempty(path_)
187 isOk = false;
188 elseif ~isequal(isOk,true)
189 % Check whether the path is valid
190 [status, message] = system([gs_command(path_) '-h']); %#ok<ASGLU>
191 isOk = status == 0;
192 end
193 good = isOk;
194end
195
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)
199 shell_cmd = '';
200 if isunix
201 shell_cmd = 'export LD_LIBRARY_PATH=""; '; % Avoids an error on Linux with GS 9.07
202 end
203 if ismac
204 shell_cmd = 'export DYLD_LIBRARY_PATH=""; '; % Avoids an error on Mac with GS 9.07
205 end
206 % Construct the command string
207 cmd = sprintf('%s"%s" ', shell_cmd, path_);
208end
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared libraries.
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_path()
function gs_command(in path_)
function check_store_gs_path(in path_)
function check_gs_path(in path_)
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...