Changeset 11803 for issm/trunk-jpl/externalpackages/export_fig/print2eps.m
- Timestamp:
- 03/28/12 13:43:15 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/externalpackages/export_fig/print2eps.m
r10635 r11803 6 6 % print2eps(filename, fig_handle, options) 7 7 % 8 % This function saves a figure as an eps file, and improves the line style, 9 % making dashed lines more like those on screen and giving grid lines their 10 % own dotted style. 8 % This function saves a figure as an eps file, with two improvements over 9 % MATLAB's print command. First, it improves the line style, making dashed 10 % lines more like those on screen and giving grid lines their own dotted 11 % style. Secondly, it substitutes original font names back into the eps 12 % file, where these have been changed by MATLAB, for up to 11 different 13 % fonts. 11 14 % 12 15 %IN: … … 18 21 % options - Additional parameter strings to be passed to print. 19 22 20 % Copyright (C) Oliver Woodford 2008-201 123 % Copyright (C) Oliver Woodford 2008-2012 21 24 22 25 % The idea of editing the EPS file to change line styles comes from Jiro … … 28 31 % Thanks to Mathieu Morlighem for reporting the issue and obtaining a fix 29 32 % from TMW. 33 34 % 8/12/2011 Added ability to correct fonts. Several people have requested 35 % this at one time or another, and also pointed me to printeps (fex id: 36 % 7501), so thank you to them. My implementation (which was not inspired by 37 % printeps - I'd already had the idea for my approach) goes 38 % slightly further in that it allows multiple fonts to be swapped. 39 40 % 14/12/2011 Fix bug affecting font names containing spaces. Many thanks to 41 % David Szwer for reporting the issue. 42 43 % 25/1/2012 Add a font not to be swapped. Thanks to Anna Rafferty and Adam 44 % Jackson for reporting the issue. Also fix a bug whereby using a font 45 % alias can lead to another font being swapped in. 30 46 31 47 function print2eps(name, fig, varargin) … … 39 55 if numel(name) < 5 || ~strcmpi(name(end-3:end), '.eps') 40 56 name = [name '.eps']; % Add the missing extension 57 end 58 % Find all the used fonts in the figure 59 fonts = get(findall(fig, '-property', 'FontName'), 'FontName'); 60 if ~iscell(fonts) 61 fonts = {fonts}; 62 end 63 fonts = unique(fonts); 64 % Map supported font aliases onto the correct name 65 for a = 1:numel(fonts) 66 f = lower(fonts{a}); 67 f(f==' ') = []; 68 switch f 69 case {'times', 'timesnewroman', 'times-roman'} 70 fonts{a} = 'Times-Roman'; 71 case {'arial', 'helvetica'} 72 fonts{a} = 'Helvetica'; 73 case {'newcenturyschoolbook', 'newcenturyschlbk'} 74 fonts{a} = 'NewCenturySchlbk'; 75 otherwise 76 end 77 end 78 % Determine the font swap table 79 matlab_fonts = {'Helvetica', 'Times-Roman', 'Palatino', 'Bookman', 'Helvetica-Narrow', 'Symbol', ... 80 'AvantGarde', 'NewCenturySchlbk', 'Courier', 'ZapfChancery', 'ZapfDingbats'}; 81 require_swap = find(~ismember(fonts, matlab_fonts)); 82 unused_fonts = find(~ismember(matlab_fonts, fonts)); 83 font_swap = min(numel(require_swap), numel(unused_fonts)); 84 font_swap = [reshape(matlab_fonts(unused_fonts(1:font_swap)), 1, font_swap); reshape(fonts(require_swap(1:font_swap)), 1, font_swap)]; 85 % Swap the fonts 86 for a = 1:size(font_swap, 2) 87 set(findall(fig, 'FontName', font_swap{2,a}), 'FontName', font_swap{1,a}); 41 88 end 42 89 % Set paper size … … 63 110 % Reset paper size 64 111 set(fig, 'PaperPositionMode', old_mode); 112 % Correct the fonts 113 if ~isempty(font_swap) 114 % Reset the font names in the figure 115 for a = 1:size(font_swap, 2) 116 set(findall(fig, 'FontName', font_swap{1,a}), 'FontName', font_swap{2,a}); 117 end 118 % Replace the font names in the eps file 119 try 120 swap_fonts(name, font_swap{:}); 121 catch 122 warning('swap_fonts() failed. This is usually because the figure contains a large number of patch objects. Consider exporting to a bitmap format in this case.'); 123 return 124 end 125 end 65 126 % Fix the line styles 66 127 try … … 70 131 end 71 132 return 133 134 function swap_fonts(fname, varargin) 135 % Read in the file 136 fh = fopen(fname, 'r'); 137 if fh == -1 138 error('File %s not found.', fname); 139 end 140 try 141 fstrm = fread(fh, '*char')'; 142 catch ex 143 fclose(fh); 144 rethrow(ex); 145 end 146 fclose(fh); 147 148 % Replace the font names 149 for a = 1:2:numel(varargin) 150 fstrm = regexprep(fstrm, [varargin{a} '-?[a-zA-Z]*\>'], varargin{a+1}(~isspace(varargin{a+1}))); 151 end 152 153 % Write out the updated file 154 fh = fopen(fname, 'w'); 155 if fh == -1 156 error('Unable to open %s for writing.', fname2); 157 end 158 try 159 fwrite(fh, fstrm, 'char*1'); 160 catch ex 161 fclose(fh); 162 rethrow(ex); 163 end 164 fclose(fh); 165 return
Note:
See TracChangeset
for help on using the changeset viewer.