Robochameleon  v1.0
ghostscript.m
1 %GHOSTSCRIPT Calls a local GhostScript executable with the input command
2 %
3 % Example:
4 % [status result] = ghostscript(cmd)
5 %
6 % Attempts to locate a ghostscript executable, finally asking the user to
7 % specify the directory ghostcript was installed into. The resulting path
8 % is stored for future reference.
9 %
10 % Once found, the executable is called with the input command string.
11 %
12 % This function requires that you have Ghostscript installed on your
13 % system. You can download this from: http://www.ghostscript.com
14 %
15 % IN:
16 % cmd - Command string to be passed into ghostscript.
17 %
18 % OUT:
19 % status - 0 iff command ran without problem.
20 % result - Output from ghostscript.
21 
22 % Copyright: Oliver Woodford, 2009-2013
23 
24 % Thanks to Jonas Dorn for the fix for the title of the uigetdir window on
25 % Mac OS.
26 % Thanks to Nathan Childress for the fix to the default location on 64-bit
27 % Windows systems.
28 % 27/4/11 - Find 64-bit Ghostscript on Windows. Thanks to Paul Durack and
29 % Shaun Kline for pointing out the issue
30 % 4/5/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/6/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/2014 - Add full path to ghostscript.txt in warning. Thanks to Koen
39 % Vermeer for raising the issue.
40 
41 function varargout = ghostscript(cmd)
42 % Initialize any required system calls before calling ghostscript
43 shell_cmd = '';
44 if isunix
45  shell_cmd = 'export LD_LIBRARY_PATH=""; '; % Avoids an error on Linux with GS 9.07
46 end
47 if ismac
48  shell_cmd = 'export DYLD_LIBRARY_PATH=""; '; % Avoids an error on Mac with GS 9.07
49 end
50 % Call ghostscript
51 [varargout{1:nargout}] = system(sprintf('%s"%s" %s', shell_cmd, gs_path, cmd));
52 return
53 
54 function path_ = gs_path
55 % Return a valid path
56 % Start with the currently set path
57 path_ = user_string('ghostscript');
58 % Check the path works
59 if check_gs_path(path_)
60  return
61 end
62 % Check whether the binary is on the path
63 if ispc
64  bin = {'gswin32c.exe', 'gswin64c.exe', 'gs'};
65 else
66  bin = {'gs'};
67 end
68 for a = 1:numel(bin)
69  path_ = bin{a};
70  if check_store_gs_path(path_)
71  return
72  end
73 end
74 % Search the obvious places
75 if ispc
76  default_location = 'C:\Program Files\gs\';
77  dir_list = dir(default_location);
78  if isempty(dir_list)
79  default_location = 'C:\Program Files (x86)\gs\'; % Possible location on 64-bit systems
80  dir_list = dir(default_location);
81  end
82  executable = {'\bin\gswin32c.exe', '\bin\gswin64c.exe'};
83  ver_num = 0;
84  % If there are multiple versions, use the newest
85  for a = 1:numel(dir_list)
86  ver_num2 = sscanf(dir_list(a).name, 'gs%g');
87  if ~isempty(ver_num2) && ver_num2 > ver_num
88  for b = 1:numel(executable)
89  path2 = [default_location dir_list(a).name executable{b}];
90  if exist(path2, 'file') == 2
91  path_ = path2;
92  ver_num = ver_num2;
93  end
94  end
95  end
96  end
97  if check_store_gs_path(path_)
98  return
99  end
100 else
101  executable = {'/usr/bin/gs', '/usr/local/bin/gs'};
102  for a = 1:numel(executable)
103  path_ = executable{a};
104  if check_store_gs_path(path_)
105  return
106  end
107  end
108 end
109 % Ask the user to enter the path
110 while 1
111  if strncmp(computer, 'MAC', 3) % Is a Mac
112  % Give separate warning as the uigetdir dialogue box doesn't have a
113  % title
114  uiwait(warndlg('Ghostscript not found. Please locate the program.'))
115  end
116  base = uigetdir('/', 'Ghostcript not found. Please locate the program.');
117  if isequal(base, 0)
118  % User hit cancel or closed window
119  break;
120  end
121  base = [base filesep];
122  bin_dir = {'', ['bin' filesep], ['lib' filesep]};
123  for a = 1:numel(bin_dir)
124  for b = 1:numel(bin)
125  path_ = [base bin_dir{a} bin{b}];
126  if exist(path_, 'file') == 2
127  if check_store_gs_path(path_)
128  return
129  end
130  end
131  end
132  end
133 end
134 error('Ghostscript not found. Have you installed it from www.ghostscript.com?');
135 
136 function good = check_store_gs_path(path_)
137 % Check the path is valid
138 good = check_gs_path(path_);
139 if ~good
140  return
141 end
142 % Update the current default path to the path found
143 if ~user_string('ghostscript', path_)
144  warning('Path to ghostscript installation could not be saved. Enter it manually in %s.', fullfile(fileparts(which('user_string.m')), '.ignore', 'ghostscript.txt'));
145  return
146 end
147 return
148 
149 function good = check_gs_path(path_)
150 % Check the path is valid
151 shell_cmd = '';
152 if ismac
153  shell_cmd = 'export DYLD_LIBRARY_PATH=""; '; % Avoids an error on Mac with GS 9.07
154 end
155 [good, message] = system(sprintf('%s"%s" -h', shell_cmd, path_));
156 good = good == 0;
157 return