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

Last change on this file was 24460, checked in by Mathieu Morlighem, 5 years ago

CHG: upgrading exprt_fig

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