Changeset 18904
- Timestamp:
- 12/02/14 11:40:53 (10 years ago)
- Location:
- issm/trunk-jpl/externalpackages/export_fig
- Files:
-
- 7 added
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/externalpackages/export_fig/copyfig.m
r11803 r18904 21 21 end 22 22 % Is there a legend? 23 if isempty(find obj(fh, 'Type', 'axes', 'Tag', 'legend'))23 if isempty(findall(fh, 'Type', 'axes', 'Tag', 'legend')) 24 24 % Safe to copy using copyobj 25 25 fh = copyobj(fh, 0); … … 31 31 delete(tmp_nam); 32 32 end 33 return 33 end -
issm/trunk-jpl/externalpackages/export_fig/eps2pdf.m
r11803 r18904 84 84 try 85 85 % Convert to pdf using ghostscript 86 [status message] = ghostscript(options);87 catch 86 [status, message] = ghostscript(options); 87 catch me 88 88 % Delete the intermediate file 89 89 delete(tmp_nam); 90 rethrow( lasterror);90 rethrow(me); 91 91 end 92 92 % Delete the intermediate file … … 97 97 options = [options ' -f "' source '"']; 98 98 % Convert to pdf using ghostscript 99 [status message] = ghostscript(options);99 [status, message] = ghostscript(options); 100 100 end 101 101 % Check for error … … 108 108 end 109 109 end 110 return 110 end 111 111 112 112 % Function to return (and create, where necessary) the font path … … 132 132 end 133 133 user_string('gs_font_path', fp); 134 return 134 end -
issm/trunk-jpl/externalpackages/export_fig/export_fig.m
r14174 r18904 13 13 % export_fig ... -a<val> 14 14 % export_fig ... -q<val> 15 % export_fig ... -p<val> 15 16 % export_fig ... -<renderer> 16 17 % export_fig ... -<colorspace> … … 85 86 % made transparent (png, pdf and eps output only). 86 87 % -m<val> - option where val indicates the factor to magnify the 87 % on-screen figure dimensions by when generating bitmap88 % on-screen figure pixel dimensions by when generating bitmap 88 89 % outputs. Default: '-m1'. 89 90 % -r<val> - option val indicates the resolution (in pixels per inch) to 90 % export bitmap outputs at, keeping the dimensions of the91 % o n-screen figure. Default: sprintf('-r%g', get(0,92 % 'ScreenPixelsPerInch')). Note that the -m and -r options93 % change the same property.91 % export bitmap and vector outputs at, keeping the dimensions 92 % of the on-screen figure. Default: '-r864' (for vector output 93 % only). Note that the -m option overides the -r option for 94 % bitmap outputs only. 94 95 % -native - option indicating that the output resolution (when outputting 95 96 % a bitmap format) should be such that the vertical resolution … … 118 119 % sometimes give a smaller file size than the default lossy 119 120 % compression, depending on the type of images. 121 % -p<val> - option to add a border of width val to eps and pdf files, 122 % where val is in units of the intermediate eps file. Default: 123 % 0 (i.e. no padding). 120 124 % -append - option indicating that if the file (pdfs only) already 121 125 % exists, the figure is to be appended as a new page, instead … … 133 137 % 134 138 % Some helpful examples and tips can be found at: 135 % http ://sites.google.com/site/oliverwoodford/software/export_fig139 % https://github.com/ojwoodford/export_fig 136 140 % 137 141 % See also PRINT, SAVEAS. 138 142 139 % Copyright (C) Oliver Woodford 2008-201 2143 % Copyright (C) Oliver Woodford 2008-2014 140 144 141 145 % The idea of using ghostscript is inspired by Peder Axensten's SAVEFIG … … 168 172 % 12/12/12: Add support for isolating uipanels. Thanks to michael for 169 173 % suggesting it. 174 % 25/09/13: Add support for changing resolution in vector formats. Thanks 175 % to Jan Jaap Meijer for suggesting it. 176 % 07/05/14: Add support for '~' at start of path. Thanks to Sally Warner 177 % for suggesting it. 170 178 171 179 function [im, alpha] = export_fig(varargin) … … 217 225 end 218 226 % Set all axes limit and tick modes to manual, so the limits and ticks can't change 219 set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual', 'XTickMode', 'manual', 'YTickMode', 'manual', 'ZTickMode', 'manual'); 227 set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual'); 228 set_tick_mode(Hlims, 'X'); 229 set_tick_mode(Hlims, 'Y'); 230 set_tick_mode(Hlims, 'Z'); 220 231 % Set to print exactly what is there 221 232 set(fig, 'InvertHardcopy', 'off'); … … 289 300 % Crop the background 290 301 if options.crop 291 [alpha, v] = crop_b ackground(alpha, 0);302 [alpha, v] = crop_borders(alpha, 0, 1); 292 303 A = A(v(1):v(2),v(3):v(4),:); 293 304 end … … 336 347 % Crop the background 337 348 if options.crop 338 A = crop_b ackground(A, tcol);349 A = crop_borders(A, tcol, 1); 339 350 end 340 351 % Downscale the image … … 406 417 end 407 418 % Generate the options for print 408 p2eArgs = {renderer };419 p2eArgs = {renderer, sprintf('-r%d', options.resolution)}; 409 420 if options.colourspace == 1 410 421 p2eArgs = [p2eArgs {'-cmyk'}]; … … 415 426 try 416 427 % Generate an eps 417 print2eps(tmp_nam, fig, p2eArgs{:});428 print2eps(tmp_nam, fig, options.bb_padding, p2eArgs{:}); 418 429 % Remove the background, if desired 419 430 if options.transparent && ~isequal(get(fig, 'Color'), 'none') 420 eps_remove_background(tmp_nam );431 eps_remove_background(tmp_nam, 1 + using_hg2(fig)); 421 432 end 422 433 % Add a bookmark to the PDF if desired … … 465 476 end 466 477 end 467 return 478 end 468 479 469 480 function [fig, options] = parse_args(nout, varargin) … … 485 496 'im', nout == 1, ... 486 497 'alpha', nout == 2, ... 487 'aa_factor', 3, ... 488 'magnify', 1, ... 498 'aa_factor', 0, ... 499 'bb_padding', 0, ... 500 'magnify', [], ... 501 'resolution', [], ... 489 502 'bookmark', false, ... 490 503 'quality', []); … … 535 548 native = true; 536 549 otherwise 537 val = str2double(regexp(varargin{a}, '(?<=-(m|M|r|R|q|Q ))(\d*\.)?\d+(e-?\d+)?', 'match'));550 val = str2double(regexp(varargin{a}, '(?<=-(m|M|r|R|q|Q|p|P))-?\d*.?\d+', 'match')); 538 551 if ~isscalar(val) 539 552 error('option %s not recognised', varargin{a}); … … 543 556 options.magnify = val; 544 557 case 'r' 545 options. magnify = val ./ get(0, 'ScreenPixelsPerInch');558 options.resolution = val; 546 559 case 'q' 547 560 options.quality = max(val, 0); 561 case 'p' 562 options.bb_padding = val; 548 563 end 549 564 end … … 573 588 end 574 589 590 % Set default anti-aliasing now we know the renderer 591 if options.aa_factor == 0 592 options.aa_factor = 1 + 2 * (~(using_hg2(fig) && strcmp(get(ancestor(fig, 'figure'), 'GraphicsSmoothing'), 'on')) | (options.renderer == 3)); 593 end 594 595 % Convert user dir '~' to full path 596 if numel(options.name) > 2 && options.name(1) == '~' && (options.name(2) == '/' || options.name(2) == '\') 597 options.name = fullfile(char(java.lang.System.getProperty('user.home')), options.name(2:end)); 598 end 599 600 % Compute the magnification and resolution 601 if isempty(options.magnify) 602 if isempty(options.resolution) 603 options.magnify = 1; 604 options.resolution = 864; 605 else 606 options.magnify = options.resolution ./ get(0, 'ScreenPixelsPerInch'); 607 end 608 elseif isempty(options.resolution) 609 options.resolution = 864; 610 end 611 575 612 % Check we have a figure handle 576 613 if isempty(fig) … … 584 621 585 622 % Check whether transparent background is wanted (old way) 586 if isequal(get(ancestor(fig , 'figure'), 'Color'), 'none')623 if isequal(get(ancestor(fig(1), 'figure'), 'Color'), 'none') 587 624 options.transparent = true; 588 625 end … … 632 669 end 633 670 end 634 return 671 end 635 672 636 673 function A = downsize(A, factor) … … 659 696 A = A(1+floor(mod(end-1, factor)/2):factor:end,1+floor(mod(end-1, factor)/2):factor:end,:); 660 697 end 661 return 698 end 662 699 663 700 function A = rgb2grey(A) 664 701 A = cast(reshape(reshape(single(A), [], 3) * single([0.299; 0.587; 0.114]), size(A, 1), size(A, 2)), class(A)); 665 return 702 end 666 703 667 704 function A = check_greyscale(A) … … 672 709 A = A(:,:,1); % Save only one channel for 8-bit output 673 710 end 674 return 675 676 function [A, v] = crop_background(A, bcol) 677 % Map the foreground pixels 678 [h, w, c] = size(A); 679 if isscalar(bcol) && c > 1 680 bcol = bcol(ones(1, c)); 681 end 682 bail = false; 683 for l = 1:w 684 for a = 1:c 685 if ~all(A(:,l,a) == bcol(a)) 686 bail = true; 687 break; 688 end 689 end 690 if bail 691 break; 692 end 693 end 694 bail = false; 695 for r = w:-1:l 696 for a = 1:c 697 if ~all(A(:,r,a) == bcol(a)) 698 bail = true; 699 break; 700 end 701 end 702 if bail 703 break; 704 end 705 end 706 bail = false; 707 for t = 1:h 708 for a = 1:c 709 if ~all(A(t,:,a) == bcol(a)) 710 bail = true; 711 break; 712 end 713 end 714 if bail 715 break; 716 end 717 end 718 bail = false; 719 for b = h:-1:t 720 for a = 1:c 721 if ~all(A(b,:,a) == bcol(a)) 722 bail = true; 723 break; 724 end 725 end 726 if bail 727 break; 728 end 729 end 730 % Crop the background, leaving one boundary pixel to avoid bleeding on 731 % resize 732 v = [max(t-1, 1) min(b+1, h) max(l-1, 1) min(r+1, w)]; 733 A = A(v(1):v(2),v(3):v(4),:); 734 return 735 736 function eps_remove_background(fname) 711 end 712 713 function eps_remove_background(fname, count) 737 714 % Remove the background of an eps file 738 715 % Open the file … … 742 719 end 743 720 % Read the file line by line 744 while true721 while count 745 722 % Get the next line 746 723 l = fgets(fh); … … 749 726 end 750 727 % Check if the line contains the background rectangle 751 if isequal(regexp(l, ' *0 +0 +\d+ +\d+ +r f*[\n\r]+', 'start'), 1)728 if isequal(regexp(l, ' *0 +0 +\d+ +\d+ +r[fe] *[\n\r]+', 'start'), 1) 752 729 % Set the line to whitespace and quit 753 730 l(1:regexp(l, '[\n\r]', 'start', 'once')-1) = ' '; 754 731 fseek(fh, -numel(l), 0); 755 732 fprintf(fh, l); 756 break; 733 % Reduce the count 734 count = count - 1; 757 735 end 758 736 end 759 737 % Close the file 760 738 fclose(fh); 761 return 739 end 762 740 763 741 function b = isvector(options) 764 742 b = options.pdf || options.eps; 765 return 743 end 766 744 767 745 function b = isbitmap(options) 768 746 b = options.png || options.tif || options.jpg || options.bmp || options.im || options.alpha; 769 return 747 end 770 748 771 749 % Helper function … … 774 752 A = {A}; 775 753 end 776 return 754 end 777 755 778 756 function add_bookmark(fname, bookmark_text) … … 808 786 end 809 787 fclose(fh); 810 return 788 end 789 790 function set_tick_mode(Hlims, ax) 791 % Set the tick mode of linear axes to manual 792 % Leave log axes alone as these are tricky 793 M = get(Hlims, [ax 'Scale']); 794 if ~iscell(M) 795 M = {M}; 796 end 797 M = cellfun(@(c) strcmp(c, 'linear'), M); 798 set(Hlims(M), [ax 'TickMode'], 'manual'); 799 end -
issm/trunk-jpl/externalpackages/export_fig/fix_lines.m
r10635 r18904 1 function fix_lines(fname, fname2)2 1 %FIX_LINES Improves the line style of eps files generated by print 3 2 % … … 5 4 % fix_lines fname 6 5 % fix_lines fname fname2 6 % fstrm_out = fixlines(fstrm_in) 7 7 % 8 8 % This function improves the style of lines in eps files generated by … … 16 16 % information. 17 17 % 18 % 18 %IN: 19 19 % fname - Name or path of source eps file. 20 20 % fname2 - Name or path of destination eps file. Default: same as fname. 21 % fstrm_in - File contents of a MATLAB-generated eps file. 22 % 23 %OUT: 24 % fstrm_out - Contents of the eps file with line styles fixed. 21 25 22 % Copyright: (C) Oliver Woodford, 2008-201 026 % Copyright: (C) Oliver Woodford, 2008-2014 23 27 24 28 % The idea of editing the EPS file to change line styles comes from Jiro … … 34 38 % opened. 35 39 36 % Read in the file 37 fh = fopen(fname, 'r'); 38 if fh == -1 39 error('File %s not found.', fname); 40 function fstrm = fix_lines(fstrm, fname2) 41 42 if nargout == 0 || nargin > 1 43 if nargin < 2 44 % Overwrite the input file 45 fname2 = fstrm; 46 end 47 % Read in the file 48 fstrm = read_write_entire_textfile(fstrm); 40 49 end 41 try42 fstrm = fread(fh, '*char')';43 catch ex44 fclose(fh);45 rethrow(ex);46 end47 fclose(fh);48 50 49 51 % Move any embedded fonts after the postscript header … … 51 53 % Find the start and end of the header 52 54 ind = regexp(fstrm, '[\n\r]%!PS-Adobe-'); 53 [ind2 ind2] = regexp(fstrm, '[\n\r]%%EndComments[\n\r]+');55 [ind2, ind2] = regexp(fstrm, '[\n\r]%%EndComments[\n\r]+'); 54 56 % Put the header first 55 57 if ~isempty(ind) && ~isempty(ind2) && ind(1) < ind2(1) … … 67 69 ind = sort(ind); 68 70 % Find line width commands 69 [ind2 ind3] = regexp(fstrm, '[\n\r]\d* w[\n\r]');71 [ind2, ind3] = regexp(fstrm, '[\n\r]\d* w[\n\r]'); 70 72 % Go through each line style section and swap with any line width commands 71 73 % near by … … 114 116 % Isolate line style definition section 115 117 first_sec = strfind(fstrm, '% line types:'); 116 [second_sec remaining] = strtok(fstrm(first_sec+1:end), '/');117 [remaining remaining] = strtok(remaining, '%');118 [second_sec, remaining] = strtok(fstrm(first_sec+1:end), '/'); 119 [remaining, remaining] = strtok(remaining, '%'); 118 120 119 121 % Define the new styles, including the new GR format … … 132 134 '/GR { [0 dpi2point mul 4 dpi2point mul] 0 setdash 1 setlinecap } bdef'}; % Grid lines - dot spacing remains constant 133 135 134 if nargin < 2 135 % Overwrite the input file 136 fname2 = fname; 136 % Construct the output 137 fstrm = [fstrm(1:first_sec) second_sec sprintf('%s\r', new_style{:}) remaining]; 138 139 % Write the output file 140 if nargout == 0 || nargin > 1 141 read_write_entire_textfile(fname2, fstrm); 137 142 end 138 139 % Save the file with the section replaced140 fh = fopen(fname2, 'w');141 if fh == -1142 error('Unable to open %s for writing.', fname2);143 143 end 144 try145 fwrite(fh, fstrm(1:first_sec), 'char*1');146 fwrite(fh, second_sec, 'char*1');147 fprintf(fh, '%s\r', new_style{:});148 fwrite(fh, remaining, 'char*1');149 catch ex150 fclose(fh);151 rethrow(ex);152 end153 fclose(fh);154 return -
issm/trunk-jpl/externalpackages/export_fig/ghostscript.m
r14174 r18904 20 20 % result - Output from ghostscript. 21 21 22 % Copyright: Oliver Woodford, 2009-201 022 % Copyright: Oliver Woodford, 2009-2013 23 23 24 24 % Thanks to Jonas Dorn for the fix for the title of the uigetdir window on … … 32 32 % 12/12/12 - Add extra executable name on Windows. Thanks to Ratish 33 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. 34 40 35 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 36 50 % Call ghostscript 37 [varargout{1:nargout}] = system(sprintf(' "%s" %s', gs_path, cmd));38 return 51 [varargout{1:nargout}] = system(sprintf('%s"%s" %s', shell_cmd, gs_path, cmd)); 52 end 39 53 40 54 function path_ = gs_path … … 85 99 end 86 100 else 87 bin= {'/usr/bin/gs', '/usr/local/bin/gs'};88 for a = 1:numel( bin)89 path_ = bin{a};101 executable = {'/usr/bin/gs', '/usr/local/bin/gs'}; 102 for a = 1:numel(executable) 103 path_ = executable{a}; 90 104 if check_store_gs_path(path_) 91 105 return … … 119 133 end 120 134 error('Ghostscript not found. Have you installed it from www.ghostscript.com?'); 135 end 121 136 122 137 function good = check_store_gs_path(path_) … … 128 143 % Update the current default path to the path found 129 144 if ~user_string('ghostscript', path_) 130 warning('Path to ghostscript installation could not be saved. Enter it manually in ghostscript.txt.');145 warning('Path to ghostscript installation could not be saved. Enter it manually in %s.', fullfile(fileparts(which('user_string.m')), '.ignore', 'ghostscript.txt')); 131 146 return 132 147 end 133 return 148 end 134 149 135 150 function good = check_gs_path(path_) 136 151 % Check the path is valid 137 [good, message] = system(sprintf('"%s" -h', path_)); 152 shell_cmd = ''; 153 if ismac 154 shell_cmd = 'export DYLD_LIBRARY_PATH=""; '; % Avoids an error on Mac with GS 9.07 155 end 156 [good, message] = system(sprintf('%s"%s" -h', shell_cmd, path_)); 138 157 good = good == 0; 139 return 158 end -
issm/trunk-jpl/externalpackages/export_fig/isolate_axes.m
r14174 r18904 19 19 % fh - The handle of the created figure. 20 20 21 % Copyright (C) Oliver Woodford 2011-201 221 % Copyright (C) Oliver Woodford 2011-2013 22 22 23 23 % Thank you to Rosella Blatt for reporting a bug to do with axes in GUIs … … 26 26 % 12/12/12 - Add support for isolating uipanels. Thanks to michael for 27 27 % suggesting it. 28 % 08/10/13 - Bug fix to allchildren suggested by Will Grant (many thanks!). 29 % 05/12/13 - Bug fix to axes having different units. Thanks to Remington 30 % Reid for reporting the issue. 28 31 29 32 function fh = isolate_axes(ah, vis) … … 72 75 nLeg = numel(lh); 73 76 if nLeg > 0 77 set([ah(:); lh(:)], 'Units', 'normalized'); 74 78 ax_pos = get(ah, 'OuterPosition'); 75 79 if nAx > 1 … … 82 86 end 83 87 leg_pos(:,3:4) = leg_pos(:,3:4) + leg_pos(:,1:2); 84 for a = 1:nAx 85 % Overlap test 86 ah = [ah; lh(leg_pos(:,1) < ax_pos(a,3) & leg_pos(:,2) < ax_pos(a,4) &... 87 leg_pos(:,3) > ax_pos(a,1) & leg_pos(:,4) > ax_pos(a,2))]; 88 end 88 ax_pos = shiftdim(ax_pos, -1); 89 % Overlap test 90 M = bsxfun(@lt, leg_pos(:,1), ax_pos(:,:,3)) & ... 91 bsxfun(@lt, leg_pos(:,2), ax_pos(:,:,4)) & ... 92 bsxfun(@gt, leg_pos(:,3), ax_pos(:,:,1)) & ... 93 bsxfun(@gt, leg_pos(:,4), ax_pos(:,:,2)); 94 ah = [ah; lh(any(M, 2))]; 89 95 end 90 96 % Get all the objects in the figure … … 92 98 % Delete everything except for the input objects and associated items 93 99 delete(axs(~ismember(axs, [ah; allchildren(ah); allancestors(ah)]))); 94 return 100 end 95 101 96 102 function ah = allchildren(ah) 97 ah = allchild(ah);103 ah = findall(ah); 98 104 if iscell(ah) 99 105 ah = cell2mat(ah); 100 106 end 101 107 ah = ah(:); 102 return 108 end 103 109 104 110 function ph = allancestors(ah) … … 111 117 end 112 118 end 113 return 119 end -
issm/trunk-jpl/externalpackages/export_fig/pdf2eps.m
r7182 r18904 25 25 options = ['-q -paper match -eps -level2 "' source '" "' dest '"']; 26 26 % Convert to eps using pdftops 27 [status message] = pdftops(options);27 [status, message] = pdftops(options); 28 28 % Check for error 29 29 if status … … 48 48 end 49 49 fclose(fid); 50 return 50 end 51 51 -
issm/trunk-jpl/externalpackages/export_fig/pdftops.m
r14174 r18904 28 28 % Thanks to Christoph Hertel for pointing out a bug in check_xpdf_path 29 29 % under linux. 30 % 23/01/2014 - Add full path to pdftops.txt in warning. 30 31 31 32 % Call pdftops 32 33 [varargout{1:nargout}] = system(sprintf('"%s" %s', xpdf_path, cmd)); 33 return 34 end 34 35 35 36 function path_ = xpdf_path … … 85 86 end 86 87 error('pdftops executable not found.'); 88 end 87 89 88 90 function good = check_store_xpdf_path(path_) … … 94 96 % Update the current default path to the path found 95 97 if ~user_string('pdftops', path_) 96 warning('Path to pdftops executable could not be saved. Enter it manually in pdftops.txt.');98 warning('Path to pdftops executable could not be saved. Enter it manually in %s.', fullfile(fileparts(which('user_string.m')), '.ignore', 'pdftops.txt')); 97 99 return 98 100 end 99 return 101 end 100 102 101 103 function good = check_xpdf_path(path_) 102 104 % Check the path is valid 103 [good message] = system(sprintf('"%s" -h', path_));105 [good, message] = system(sprintf('"%s" -h', path_)); 104 106 % system returns good = 1 even when the command runs 105 107 % Look for something distinct in the help text 106 108 good = ~isempty(strfind(message, 'PostScript')); 107 return 109 end -
issm/trunk-jpl/externalpackages/export_fig/print2array.m
r14174 r18904 71 71 % Print to eps file 72 72 tmp_eps = [tempname '.eps']; 73 print2eps(tmp_eps, fig, renderer, '-loose');73 print2eps(tmp_eps, fig, 0, renderer, '-loose'); 74 74 try 75 75 % Initialize the command to export to tiff using ghostscript … … 172 172 end 173 173 end 174 return 174 end 175 175 176 176 % Function to return (and create, where necessary) the font path … … 196 196 end 197 197 user_string('gs_font_path', fp); 198 return 198 end -
issm/trunk-jpl/externalpackages/export_fig/print2eps.m
r15310 r18904 4 4 % print2eps filename 5 5 % print2eps(filename, fig_handle) 6 % print2eps(filename, fig_handle, options) 6 % print2eps(filename, fig_handle, bb_padding) 7 % print2eps(filename, fig_handle, bb_padding, options) 7 8 % 8 9 % This function saves a figure as an eps file, with two improvements over … … 18 19 % ".eps" extension is added if not there already. If a path is 19 20 % not specified, the figure is saved in the current directory. 20 % fig_handle - The handle of the figure to be saved. Default: gcf. 21 % fig_handle - The handle of the figure to be saved. Default: gcf(). 22 % bb_padding - Scalar value of amount of padding to add to border around 23 % the figure, in points. Can be negative as well as 24 % positive. Default: 0. 21 25 % options - Additional parameter strings to be passed to print. 22 26 23 % Copyright (C) Oliver Woodford 2008-201 327 % Copyright (C) Oliver Woodford 2008-2014 24 28 25 29 % The idea of editing the EPS file to change line styles comes from Jiro … … 50 54 % 22/03/13: Extend font swapping to axes labels. Thanks to Rasmus Ischebeck 51 55 % for reporting the issue. 52 53 function print2eps(name, fig, varargin) 56 % 23/07/13: Bug fix to font swapping. Thanks to George for reporting the 57 % issue. 58 % 13/08/13: Fix MATLAB feature of not exporting white lines correctly. 59 % Thanks to Sebastian Heßlinger for reporting it. 60 61 function print2eps(name, fig, bb_padding, varargin) 54 62 options = {'-depsc2'}; 55 if nargin < 2 56 fig = gcf; 57 elseif nargin > 2 63 if nargin > 3 58 64 options = [options varargin]; 65 elseif nargin < 3 66 bb_padding = 0; 67 if nargin < 2 68 fig = gcf(); 69 end 59 70 end 60 71 % Construct the filename … … 62 73 name = [name '.eps']; % Add the missing extension 63 74 end 75 % Set paper size 76 old_pos_mode = get(fig, 'PaperPositionMode'); 77 old_orientation = get(fig, 'PaperOrientation'); 78 set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait'); 64 79 % Find all the used fonts in the figure 65 80 font_handles = findall(fig, '-property', 'FontName'); … … 95 110 font_swap{1,a} = find(strcmp(fontslu{require_swap(a)}, fontsl)); 96 111 font_swap{2,a} = matlab_fonts{unused_fonts(a)}; 97 font_swap{3,a} = fonts{font_swap{1, end}(1)};112 font_swap{3,a} = fonts{font_swap{1,a}(1)}; 98 113 fonts_new(font_swap{1,a}) = {font_swap{2,a}}; 99 114 end … … 126 141 update = reshape(update(M), 1, []); 127 142 end 128 % Set paper size129 old_pos_mode = get(fig, 'PaperPositionMode');130 old_orientation = get(fig, 'PaperOrientation');131 set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait');132 143 % MATLAB bug fix - black and white text can come out inverted sometimes 133 144 % Find the white and black text … … 143 154 set(black_text_handles, 'Color', [0 0 0] + eps); 144 155 set(white_text_handles, 'Color', [1 1 1] - eps); 156 % MATLAB bug fix - white lines can come out funny sometimes 157 % Find the white lines 158 white_line_handles = findobj(fig, 'Type', 'line'); 159 M = get(white_line_handles, 'Color'); 160 if iscell(M) 161 M = cell2mat(M); 162 end 163 white_line_handles = white_line_handles(sum(M, 2) == 3); 164 % Set the line color slightly off white 165 set(white_line_handles, 'Color', [1 1 1] - 0.00001); 145 166 % Print to eps file 146 167 print(fig, options{:}, name); 147 % Reset the font colors168 % Reset the font and line colors 148 169 set(black_text_handles, 'Color', [0 0 0]); 149 170 set(white_text_handles, 'Color', [1 1 1]); 171 set(white_line_handles, 'Color', [1 1 1]); 150 172 % Reset paper size 151 173 set(fig, 'PaperPositionMode', old_pos_mode, 'PaperOrientation', old_orientation); 152 % Correct the fonts174 % Reset the font names in the figure 153 175 if ~isempty(font_swap) 154 % Reset the font names in the figure155 176 for a = update 156 177 set(font_handles(a), 'FontName', fonts{a}, 'FontSize', fonts_size(a)); 157 178 end 158 % Replace the font names in the eps file 159 font_swap = font_swap(2:3,:); 160 try 161 swap_fonts(name, font_swap{:}); 162 catch 163 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.'); 164 return 165 end 166 end 167 % Fix the line styles 179 end 180 % Do post-processing on the eps file 168 181 try 169 f ix_lines(name);182 fstrm = read_write_entire_textfile(name); 170 183 catch 171 warning('fix_lines() failed. This is usually because the figure contains a large number of patch objects. Consider exporting to a bitmap format in this case.'); 172 end 173 return 174 175 function swap_fonts(fname, varargin) 176 % Read in the file 177 fh = fopen(fname, 'r'); 178 if fh == -1 179 error('File %s not found.', fname); 180 end 181 try 182 fstrm = fread(fh, '*char')'; 183 catch ex 184 fclose(fh); 185 rethrow(ex); 186 end 187 fclose(fh); 188 184 warning('Loading EPS file failed, so unable to perform post-processing. This is usually because the figure contains a large number of patch objects. Consider exporting to a bitmap format in this case.'); 185 return 186 end 189 187 % Replace the font names 190 for a = 1:2:numel(varargin) 191 fstrm = regexprep(fstrm, [varargin{a} '-?[a-zA-Z]*\>'], varargin{a+1}(~isspace(varargin{a+1}))); 192 end 193 194 % Write out the updated file 195 fh = fopen(fname, 'w'); 196 if fh == -1 197 error('Unable to open %s for writing.', fname2); 198 end 199 try 200 fwrite(fh, fstrm, 'char*1'); 201 catch ex 202 fclose(fh); 203 rethrow(ex); 204 end 205 fclose(fh); 206 return 188 if ~isempty(font_swap) 189 for a = 1:size(font_swap, 2) 190 %fstrm = regexprep(fstrm, [font_swap{1,a} '-?[a-zA-Z]*\>'], font_swap{3,a}(~isspace(font_swap{3,a}))); 191 fstrm = regexprep(fstrm, font_swap{2,a}, font_swap{3,a}(~isspace(font_swap{3,a}))); 192 end 193 end 194 if using_hg2(fig) 195 % Convert miter joins to line joins 196 fstrm = regexprep(fstrm, '10.0 ML\n', '1 LJ\n'); 197 % Move the bounding box to the top of the file 198 [s, e] = regexp(fstrm, '%%BoundingBox: [\w\s()]*%%'); 199 if numel(s) == 2 200 fstrm = fstrm([1:s(1)-1 s(2):e(2)-2 e(1)-1:s(2)-1 e(2)-1:end]); 201 end 202 else 203 % Fix the line styles 204 fstrm = fix_lines(fstrm); 205 end 206 % Apply the bounding box padding 207 if bb_padding 208 add_padding = @(n1, n2, n3, n4) sprintf(' %d', str2double({n1, n2, n3, n4}) + [-bb_padding -bb_padding bb_padding bb_padding]); 209 fstrm = regexprep(fstrm, '%%BoundingBox:[ ]+([-]?\d+)[ ]+([-]?\d+)[ ]+([-]?\d+)[ ]+([-]?\d+)', '%%BoundingBox:${add_padding($1, $2, $3, $4)}'); 210 end 211 % Write out the fixed eps file 212 read_write_entire_textfile(name, fstrm); 213 end -
issm/trunk-jpl/externalpackages/export_fig/user_string.m
r15310 r18904 85 85 fclose(fid); 86 86 end 87 return 87 end
Note:
See TracChangeset
for help on using the changeset viewer.