Changeset 19571 for issm/trunk-jpl/externalpackages/export_fig/eps2pdf.m
- Timestamp:
- 09/21/15 13:50:48 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/externalpackages/export_fig/eps2pdf.m
r18904 r19571 1 function eps2pdf(source, dest, crop, append, gray, quality, gs_options) 1 2 %EPS2PDF Convert an eps file to pdf format using ghostscript 2 3 % … … 7 8 % eps2pdf(source, dest, crop, append, gray) 8 9 % eps2pdf(source, dest, crop, append, gray, quality) 10 % eps2pdf(source, dest, crop, append, gray, quality, gs_options) 9 11 % 10 12 % This function converts an eps file to pdf format. The output can be … … 17 19 % system. Ghostscript can be downloaded from: http://www.ghostscript.com 18 20 % 19 % IN:20 % source - filename of the source eps file to convert. The filename is21 % assumed to already have the extension ".eps".22 % dest - filename of the destination pdf file. The filename is assumed to23 % already have the extension ".pdf".24 % crop - boolean indicating whether to crop the borders off the pdf.25 % Default: true.26 % append - boolean indicating whether the eps should be appended to the27 % end of the pdf as a new page (if the pdf exists already).28 % Default: false.29 % gray - boolean indicating whether the output pdf should be grayscale or30 % not. Default: false.21 % Inputs: 22 % source - filename of the source eps file to convert. The filename is 23 % assumed to already have the extension ".eps". 24 % dest - filename of the destination pdf file. The filename is assumed 25 % to already have the extension ".pdf". 26 % crop - boolean indicating whether to crop the borders off the pdf. 27 % Default: true. 28 % append - boolean indicating whether the eps should be appended to the 29 % end of the pdf as a new page (if the pdf exists already). 30 % Default: false. 31 % gray - boolean indicating whether the output pdf should be grayscale 32 % or not. Default: false. 31 33 % quality - scalar indicating the level of image bitmap quality to 32 34 % output. A larger value gives a higher quality. quality > 100 33 35 % gives lossless output. Default: ghostscript prepress default. 36 % gs_options - optional ghostscript options (e.g.: '-dNoOutputFonts'). If 37 % multiple options are needed, enclose in call array: {'-a','-b'} 34 38 35 % Copyright (C) Oliver Woodford 2009-201 139 % Copyright (C) Oliver Woodford 2009-2014, Yair Altman 2015- 36 40 37 41 % Suggestion of appending pdf files provided by Matt C at: … … 44 48 45 49 % 9/12/2011 Pass font path to ghostscript. 50 % 26/02/15: If temp dir is not writable, use the dest folder for temp 51 % destination files (Javier Paredes) 52 % 28/02/15: Enable users to specify optional ghostscript options (issue #36) 53 % 01/03/15: Upon GS error, retry without the -sFONTPATH= option (this might solve 54 % some /findfont errors according to James Rankin, FEX Comment 23/01/15) 55 % 23/06/15: Added extra debug info in case of ghostscript error; code indentation 46 56 47 function eps2pdf(source, dest, crop, append, gray, quality) 48 % Intialise the options string for ghostscript 49 options = ['-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="' dest '"']; 50 % Set crop option 51 if nargin < 3 || crop 52 options = [options ' -dEPSCrop']; 53 end 54 % Set the font path 55 fp = font_path(); 56 if ~isempty(fp) 57 options = [options ' -sFONTPATH="' fp '"']; 58 end 59 % Set the grayscale option 60 if nargin > 4 && gray 61 options = [options ' -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray']; 62 end 63 % Set the bitmap quality 64 if nargin > 5 && ~isempty(quality) 65 options = [options ' -dAutoFilterColorImages=false -dAutoFilterGrayImages=false']; 66 if quality > 100 67 options = [options ' -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -c ".setpdfwrite << /ColorImageDownsampleThreshold 10 /GrayImageDownsampleThreshold 10 >> setdistillerparams"']; 57 % Intialise the options string for ghostscript 58 options = ['-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="' dest '"']; 59 % Set crop option 60 if nargin < 3 || crop 61 options = [options ' -dEPSCrop']; 62 end 63 % Set the font path 64 fp = font_path(); 65 if ~isempty(fp) 66 options = [options ' -sFONTPATH="' fp '"']; 67 end 68 % Set the grayscale option 69 if nargin > 4 && gray 70 options = [options ' -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray']; 71 end 72 % Set the bitmap quality 73 if nargin > 5 && ~isempty(quality) 74 options = [options ' -dAutoFilterColorImages=false -dAutoFilterGrayImages=false']; 75 if quality > 100 76 options = [options ' -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -c ".setpdfwrite << /ColorImageDownsampleThreshold 10 /GrayImageDownsampleThreshold 10 >> setdistillerparams"']; 77 else 78 options = [options ' -dColorImageFilter=/DCTEncode -dGrayImageFilter=/DCTEncode']; 79 v = 1 + (quality < 80); 80 quality = 1 - quality / 100; 81 s = sprintf('<< /QFactor %.2f /Blend 1 /HSample [%d 1 1 %d] /VSample [%d 1 1 %d] >>', quality, v, v, v, v); 82 options = sprintf('%s -c ".setpdfwrite << /ColorImageDict %s /GrayImageDict %s >> setdistillerparams"', options, s, s); 83 end 84 end 85 % Enable users to specify optional ghostscript options (issue #36) 86 if nargin > 6 && ~isempty(gs_options) 87 if iscell(gs_options) 88 gs_options = sprintf(' %s',gs_options{:}); 89 elseif ~ischar(gs_options) 90 error('gs_options input argument must be a string or cell-array of strings'); 91 else 92 gs_options = [' ' gs_options]; 93 end 94 options = [options gs_options]; 95 end 96 % Check if the output file exists 97 if nargin > 3 && append && exist(dest, 'file') == 2 98 % File exists - append current figure to the end 99 tmp_nam = tempname; 100 try 101 % Ensure that the temp dir is writable (Javier Paredes 26/2/15) 102 fid = fopen(tmp_nam,'w'); 103 fwrite(fid,1); 104 fclose(fid); 105 delete(tmp_nam); 106 catch 107 % Temp dir is not writable, so use the dest folder 108 [dummy,fname,fext] = fileparts(tmp_nam); %#ok<ASGLU> 109 fpath = fileparts(dest); 110 tmp_nam = fullfile(fpath,[fname fext]); 111 end 112 % Copy the file 113 copyfile(dest, tmp_nam); 114 % Add the output file names 115 options = [options ' -f "' tmp_nam '" "' source '"']; 116 try 117 % Convert to pdf using ghostscript 118 [status, message] = ghostscript(options); 119 catch me 120 % Delete the intermediate file 121 delete(tmp_nam); 122 rethrow(me); 123 end 124 % Delete the intermediate file 125 delete(tmp_nam); 68 126 else 69 options = [options ' -dColorImageFilter=/DCTEncode -dGrayImageFilter=/DCTEncode']; 70 v = 1 + (quality < 80); 71 quality = 1 - quality / 100; 72 s = sprintf('<< /QFactor %.2f /Blend 1 /HSample [%d 1 1 %d] /VSample [%d 1 1 %d] >>', quality, v, v, v, v); 73 options = sprintf('%s -c ".setpdfwrite << /ColorImageDict %s /GrayImageDict %s >> setdistillerparams"', options, s, s); 74 end 75 end 76 % Check if the output file exists 77 if nargin > 3 && append && exist(dest, 'file') == 2 78 % File exists - append current figure to the end 79 tmp_nam = tempname; 80 % Copy the file 81 copyfile(dest, tmp_nam); 82 % Add the output file names 83 options = [options ' -f "' tmp_nam '" "' source '"']; 84 try 127 % File doesn't exist or should be over-written 128 % Add the output file names 129 options = [options ' -f "' source '"']; 85 130 % Convert to pdf using ghostscript 86 131 [status, message] = ghostscript(options); 87 catch me88 % Delete the intermediate file89 delete(tmp_nam);90 rethrow(me);91 132 end 92 % Delete the intermediate file 93 delete(tmp_nam); 94 else 95 % File doesn't exist or should be over-written 96 % Add the output file names 97 options = [options ' -f "' source '"']; 98 % Convert to pdf using ghostscript 99 [status, message] = ghostscript(options); 100 end 101 % Check for error 102 if status 103 % Report error 104 if isempty(message) 105 error('Unable to generate pdf. Check destination directory is writable.'); 106 else 107 error(message); 133 % Check for error 134 if status 135 % Retry without the -sFONTPATH= option (this might solve some GS 136 % /findfont errors according to James Rankin, FEX Comment 23/01/15) 137 orig_options = options; 138 if ~isempty(fp) 139 options = regexprep(options, ' -sFONTPATH=[^ ]+ ',' '); 140 status = ghostscript(options); 141 if ~status, return; end % hurray! (no error) 142 end 143 % Report error 144 if isempty(message) 145 error('Unable to generate pdf. Check destination directory is writable.'); 146 else 147 fprintf(2, 'Ghostscript error: perhaps %s is open by another application\n', dest); 148 if ~isempty(gs_options) 149 fprintf(2, ' or maybe the%s option(s) are not accepted by your GS version\n', gs_options); 150 end 151 fprintf(2, 'Ghostscript options: %s\n\n', orig_options); 152 error(message); 153 end 108 154 end 109 end110 155 end 111 156 112 157 % Function to return (and create, where necessary) the font path 113 158 function fp = font_path() 114 fp = user_string('gs_font_path'); 115 if ~isempty(fp) 116 return 159 fp = user_string('gs_font_path'); 160 if ~isempty(fp) 161 return 162 end 163 % Create the path 164 % Start with the default path 165 fp = getenv('GS_FONTPATH'); 166 % Add on the typical directories for a given OS 167 if ispc 168 if ~isempty(fp) 169 fp = [fp ';']; 170 end 171 fp = [fp getenv('WINDIR') filesep 'Fonts']; 172 else 173 if ~isempty(fp) 174 fp = [fp ':']; 175 end 176 fp = [fp '/usr/share/fonts:/usr/local/share/fonts:/usr/share/fonts/X11:/usr/local/share/fonts/X11:/usr/share/fonts/truetype:/usr/local/share/fonts/truetype']; 177 end 178 user_string('gs_font_path', fp); 117 179 end 118 % Create the path119 % Start with the default path120 fp = getenv('GS_FONTPATH');121 % Add on the typical directories for a given OS122 if ispc123 if ~isempty(fp)124 fp = [fp ';'];125 end126 fp = [fp getenv('WINDIR') filesep 'Fonts'];127 else128 if ~isempty(fp)129 fp = [fp ':'];130 end131 fp = [fp '/usr/share/fonts:/usr/local/share/fonts:/usr/share/fonts/X11:/usr/local/share/fonts/X11:/usr/share/fonts/truetype:/usr/local/share/fonts/truetype'];132 end133 user_string('gs_font_path', fp);134 end
Note:
See TracChangeset
for help on using the changeset viewer.