source: issm/trunk-jpl/externalpackages/export_fig/pdftops.m@ 21671

Last change on this file since 21671 was 21671, checked in by Mathieu Morlighem, 8 years ago

CHG: updated export_fig

File size: 6.0 KB
Line 
1function varargout = pdftops(cmd)
2%PDFTOPS Calls a local pdftops executable with the input command
3%
4% Example:
5% [status result] = pdftops(cmd)
6%
7% Attempts to locate a pdftops executable, finally asking the user to
8% specify the directory pdftops was installed into. The resulting path is
9% stored for future reference.
10%
11% Once found, the executable is called with the input command string.
12%
13% This function requires that you have pdftops (from the Xpdf package)
14% installed on your system. You can download this from:
15% http://www.foolabs.com/xpdf
16%
17% IN:
18% cmd - Command string to be passed into pdftops (e.g. '-help').
19%
20% OUT:
21% status - 0 iff command ran without problem.
22% result - Output from pdftops.
23
24% Copyright: Oliver Woodford, 2009-2010
25
26% Thanks to Jonas Dorn for the fix for the title of the uigetdir window on Mac OS.
27% Thanks to Christoph Hertel for pointing out a bug in check_xpdf_path under linux.
28% 23/01/2014 - Add full path to pdftops.txt in warning.
29% 27/05/2015 - Fixed alert in case of missing pdftops; fixed code indentation
30% 02/05/2016 - Added possible error explanation suggested by Michael Pacer (issue #137)
31% 02/05/2016 - Search additional possible paths suggested by Jonas Stein (issue #147)
32% 03/05/2016 - Display the specific error message if pdftops fails for some reason (issue #148)
33
34 % Call pdftops
35 [varargout{1:nargout}] = system([xpdf_command(xpdf_path()) cmd]);
36end
37
38function path_ = xpdf_path
39 % Return a valid path
40 % Start with the currently set path
41 path_ = user_string('pdftops');
42 % Check the path works
43 if check_xpdf_path(path_)
44 return
45 end
46 % Check whether the binary is on the path
47 if ispc
48 bin = 'pdftops.exe';
49 else
50 bin = 'pdftops';
51 end
52 if check_store_xpdf_path(bin)
53 path_ = bin;
54 return
55 end
56 % Search the obvious places
57 if ispc
58 paths = {'C:\Program Files\xpdf\pdftops.exe', 'C:\Program Files (x86)\xpdf\pdftops.exe'};
59 else
60 paths = {'/usr/bin/pdftops', '/usr/local/bin/pdftops'};
61 end
62 for a = 1:numel(paths)
63 path_ = paths{a};
64 if check_store_xpdf_path(path_)
65 return
66 end
67 end
68
69 % Ask the user to enter the path
70 errMsg1 = 'Pdftops not found. Please locate the program, or install xpdf-tools from ';
71 url1 = 'http://foolabs.com/xpdf';
72 fprintf(2, '%s\n', [errMsg1 '<a href="matlab:web(''-browser'',''' url1 ''');">' url1 '</a>']);
73 errMsg1 = [errMsg1 url1];
74 %if strncmp(computer,'MAC',3) % Is a Mac
75 % % Give separate warning as the MacOS uigetdir dialogue box doesn't have a title
76 % uiwait(warndlg(errMsg1))
77 %end
78
79 % Provide an alternative possible explanation as per issue #137
80 errMsg2 = 'If you have pdftops installed, perhaps Matlab is shaddowing it as described in ';
81 url2 = 'https://github.com/altmany/export_fig/issues/137';
82 fprintf(2, '%s\n', [errMsg2 '<a href="matlab:web(''-browser'',''' url2 ''');">issue #137</a>']);
83 errMsg2 = [errMsg2 url1];
84
85 state = 0;
86 while 1
87 if state
88 option1 = 'Install pdftops';
89 else
90 option1 = 'Issue #137';
91 end
92 answer = questdlg({errMsg1,'',errMsg2},'Pdftops error',option1,'Locate pdftops','Cancel','Cancel');
93 drawnow; % prevent a Matlab hang: http://undocumentedmatlab.com/blog/solving-a-matlab-hang-problem
94 switch answer
95 case 'Install pdftops'
96 web('-browser',url1);
97 case 'Issue #137'
98 web('-browser',url2);
99 state = 1;
100 case 'Locate pdftops'
101 base = uigetdir('/', errMsg1);
102 if isequal(base, 0)
103 % User hit cancel or closed window
104 break
105 end
106 base = [base filesep]; %#ok<AGROW>
107 bin_dir = {'', ['bin' filesep], ['lib' filesep]};
108 for a = 1:numel(bin_dir)
109 path_ = [base bin_dir{a} bin];
110 if exist(path_, 'file') == 2
111 break
112 end
113 end
114 if check_store_xpdf_path(path_)
115 return
116 end
117
118 otherwise % User hit Cancel or closed window
119 break
120 end
121 end
122 error('pdftops executable not found.');
123end
124
125function good = check_store_xpdf_path(path_)
126 % Check the path is valid
127 good = check_xpdf_path(path_);
128 if ~good
129 return
130 end
131 % Update the current default path to the path found
132 if ~user_string('pdftops', path_)
133 warning('Path to pdftops executable could not be saved. Enter it manually in %s.', fullfile(fileparts(which('user_string.m')), '.ignore', 'pdftops.txt'));
134 return
135 end
136end
137
138function good = check_xpdf_path(path_)
139 % Check the path is valid
140 [good, message] = system([xpdf_command(path_) '-h']); %#ok<ASGLU>
141 % system returns good = 1 even when the command runs
142 % Look for something distinct in the help text
143 good = ~isempty(strfind(message, 'PostScript'));
144
145 % Display the error message if the pdftops executable exists but fails for some reason
146 if ~good && exist(path_,'file') % file exists but generates an error
147 fprintf('Error running %s:\n', path_);
148 fprintf(2,'%s\n\n',message);
149 end
150end
151
152function cmd = xpdf_command(path_)
153 % Initialize any required system calls before calling ghostscript
154 % TODO: in Unix/Mac, find a way to determine whether to use "export" (bash) or "setenv" (csh/tcsh)
155 shell_cmd = '';
156 if isunix
157 % Avoids an error on Linux with outdated MATLAB lib files
158 % R20XXa/bin/glnxa64/libtiff.so.X
159 % R20XXa/sys/os/glnxa64/libstdc++.so.X
160 shell_cmd = 'export LD_LIBRARY_PATH=""; ';
161 end
162 if ismac
163 shell_cmd = 'export DYLD_LIBRARY_PATH=""; ';
164 end
165 % Construct the command string
166 cmd = sprintf('%s"%s" ', shell_cmd, path_);
167end
Note: See TracBrowser for help on using the repository browser.