Changeset 10635 for issm/trunk


Ignore:
Timestamp:
11/14/11 15:29:23 (13 years ago)
Author:
Mathieu Morlighem
Message:

New export_fig bug with text color fixed

Location:
issm/trunk/externalpackages/export_fig
Files:
2 added
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk/externalpackages/export_fig/export_fig.m

    r9572 r10635  
    77%   export_fig filename -format1 -format2
    88%   export_fig ... -nocrop
     9%   export_fig ... -transparent
    910%   export_fig ... -native
    1011%   export_fig ... -m<val>
     
    4142% (the latter approximately, and ignoring cropping & magnification) in the
    4243% output file. For transparent background (and semi-transparent patch
    43 % objects), set the figure (and axes) 'Color' property to 'none'; pdf, eps
    44 % and png are the only file formats to support a transparent background,
    45 % whilst the png format alone supports transparency of patch objects.
     44% objects), use the -transparent option or set the figure 'Color' property
     45% to 'none'. To make axes transparent set the axes 'Color' property to
     46% 'none'. Pdf, eps and png are the only file formats to support a
     47% transparent background, whilst the png format alone supports transparency
     48% of patch objects.
    4649%
    4750% The choice of renderer (opengl, zbuffer or painters) has a large impact
     
    7881%   -nocrop - option indicating that the borders of the output are not to
    7982%             be cropped.
     83%   -transparent - option indicating that the figure background is to be
     84%                  made transparent (png, pdf and eps output only).
    8085%   -m<val> - option where val indicates the factor to magnify the
    8186%             on-screen figure dimensions by when generating bitmap
     
    115120%             exists, the figure is to be appended as a new page, instead
    116121%             of being overwritten (default).
    117 %   handle - The handle of the figure or axes to be saved. Default: gcf.
     122%   handle - The handle of the figure or axes (can be an array of handles
     123%            of several axes, but these must be in the same figure) to be
     124%            saved. Default: gcf.
    118125%
    119126%OUT:
     
    127134%   See also PRINT, SAVEAS.
    128135
    129 % Copyright (C) Oliver Woodford 2008-2010
     136% Copyright (C) Oliver Woodford 2008-2011
    130137
    131138% The idea of using ghostscript is inspired by Peder Axensten's SAVEFIG
     
    153160[fig options] = parse_args(nargout, varargin{:});
    154161% Isolate the subplot, if it is one
    155 cls = strcmp(get(fig, 'Type'), 'axes');
     162cls = strcmp(get(fig(1), 'Type'), 'axes');
    156163if cls
    157     % Given a handle of a single set of axes
    158     fig = isolate_subplot(fig);
     164    % Given handles of one or more axes, so isolate them from the rest
     165    fig = isolate_axes(fig);
    159166else
    160167    old_mode = get(fig, 'InvertHardcopy');
     
    193200if isbitmap(options)
    194201    % Get the background colour
    195     tcol = get(fig, 'Color');
    196     if isequal(tcol, 'none') && (options.png || options.alpha)
     202    if options.transparent && (options.png || options.alpha)
    197203        % Get out an alpha channel
    198204        % MATLAB "feature": black colorbar axes can change to white and vice versa!
     
    216222        % Set the background colour to black, and set size in case it was
    217223        % changed internally
     224        tcol = get(fig, 'Color');
    218225        set(fig, 'Color', 'k', 'Position', pos);
    219226        % Correct the colorbar axes colours
     
    223230        B = print2array(fig, magnify, renderer);
    224231        % Downscale the image
    225         B = downsize(single(B), 0, options.aa_factor);
     232        B = downsize(single(B), options.aa_factor);
    226233        % Set background to white (and set size)
    227234        set(fig, 'Color', 'w', 'Position', pos);
     
    232239        A = print2array(fig, magnify, renderer);
    233240        % Downscale the image
    234         A = downsize(single(A), 255, options.aa_factor);
     241        A = downsize(single(A), options.aa_factor);
    235242        % Set the background colour (and size) back to normal
    236         set(fig, 'Color', 'none', 'Position', pos);
     243        set(fig, 'Color', tcol, 'Position', pos);
    237244        % Compute the alpha map
    238         alpha = sum(B - A, 3) / (255*3) + 1;
     245        alpha = round(sum(B - A, 3)) / (255 * 3) + 1;
    239246        A = alpha;
    240247        A(A==0) = 1;
     
    281288    else
    282289        % Print large version to array
    283         if isequal(tcol, 'none')
     290        if options.transparent
    284291            % MATLAB "feature": apparently figure size can change when changing
    285292            % colour in -nodisplay mode
    286293            pos = get(fig, 'Position');
     294            tcol = get(fig, 'Color');
    287295            set(fig, 'Color', 'w', 'Position', pos);
    288296            A = print2array(fig, magnify, renderer);
    289             set(fig, 'Color', 'none', 'Position', pos);
     297            set(fig, 'Color', tcol, 'Position', pos);
    290298            tcol = 255;
    291299        else
    292             A = print2array(fig, magnify, renderer);
    293             tcol = tcol * 255;
    294             if ~isequal(tcol, round(tcol))
    295                 tcol = squeeze(A(1,1,:));
    296             end
     300            [A tcol] = print2array(fig, magnify, renderer);
    297301        end
    298302        % Crop the background
     
    301305        end
    302306        % Downscale the image
    303         A = downsize(A, tcol, options.aa_factor);
     307        A = downsize(A, options.aa_factor);
    304308        if options.colourspace == 2
    305309            % Convert to greyscale
     
    378382        % Generate an eps
    379383        print2eps(tmp_nam, fig, p2eArgs{:});
     384        % Remove the background, if desired
     385        if options.transparent && ~isequal(get(fig, 'Color'), 'none')
     386            eps_remove_background(tmp_nam);
     387        end
    380388        % Generate a pdf
    381389        eps2pdf(tmp_nam, pdf_nam, 1, options.append, options.colourspace==2, options.quality);
    382     catch
     390    catch ex
    383391        % Delete the eps
    384392        delete(tmp_nam);
    385         rethrow(lasterror);
     393        rethrow(ex);
    386394    end
    387395    % Delete the eps
     
    391399            % Generate an eps from the pdf
    392400            pdf2eps(pdf_nam, [options.name '.eps']);
    393         catch
     401        catch ex
    394402            if ~options.pdf
    395403                % Delete the pdf
    396404                delete(pdf_nam);
    397405            end
    398             rethrow(lasterror);
     406            rethrow(ex);
    399407        end
    400408        if ~options.pdf
     
    419427options = struct('name', 'export_fig_out', ...
    420428                 'crop', true, ...
     429                 'transparent', false, ...
    421430                 'renderer', 0, ... % 0: default, 1: OpenGL, 2: ZBuffer, 3: Painters
    422431                 'pdf', false, ...
     
    426435                 'jpg', false, ...
    427436                 'bmp', false, ...
    428                  'colourspace', 0, ... % ): RGB/gray, 1: CMYK, 2: gray
     437                 'colourspace', 0, ... % 0: RGB/gray, 1: CMYK, 2: gray
    429438                 'append', false, ...
    430439                 'im', nout == 1, ...
    431440                 'alpha', nout == 2, ...
    432                  'aa_factor', 4, ...
     441                 'aa_factor', 3, ...
    433442                 'magnify', 1, ...
    434443                 'quality', []);
     
    437446% Go through the other arguments
    438447for a = 1:nargin-1
    439     if ishandle(varargin{a})
     448    if all(ishandle(varargin{a}))
    440449        fig = varargin{a};
    441450    elseif ischar(varargin{a}) && ~isempty(varargin{a})
     
    444453                case 'nocrop'
    445454                    options.crop = false;
     455                case {'trans', 'transparent'}
     456                    options.transparent = true;
    446457                case 'opengl'
    447458                    options.renderer = 1;
     
    489500            end
    490501        else
    491             name = varargin{a};
    492             if numel(name) > 3 && name(end-3) == '.' && any(strcmpi(name(end-2:end), {'pdf', 'eps', 'png', 'tif', 'jpg', 'bmp'}))
    493                 options.(lower(name(end-2:end))) = true;
    494                 name = name(1:end-4);
     502            [p options.name ext] = fileparts(varargin{a});
     503            if ~isempty(p)
     504                options.name = [p filesep options.name];
    495505            end
    496             options.name = name;
     506            switch lower(ext)
     507                case {'.tif', '.tiff'}
     508                    options.tif = true;
     509                case {'.jpg', '.jpeg'}
     510                    options.jpg = true;
     511                case '.png'
     512                    options.png = true;
     513                case '.bmp'
     514                    options.bmp = true;
     515                case '.eps'
     516                    options.eps = true;
     517                case '.pdf'
     518                    options.pdf = true;
     519                otherwise
     520                    options.name = varargin{a};
     521            end
    497522        end
    498523    end
     
    507532if ~isvector(options) && ~isbitmap(options)
    508533    options.png = true;
     534end
     535
     536% Check whether transparent background is wanted (old way)
     537if isequal(get(fig, 'Color'), 'none')
     538    options.transparent = true;
    509539end
    510540
     
    555585return
    556586
    557 function fh = isolate_subplot(ah, vis)
    558 % Isolate the axes in a figure on their own
    559 % Tag the axes so we can find them in the copy
    560 old_tag = get(ah, 'Tag');
    561 set(ah, 'Tag', 'ObjectToCopy');
    562 % Create a new figure exactly the same as the old one
    563 fh = copyfig(ancestor(ah, 'figure')); %copyobj(ancestor(ah, 'figure'), 0);
    564 if nargin < 2 || ~vis
    565     set(fh, 'Visible', 'off');
    566 end
    567 % Reset the axes tag
    568 set(ah, 'Tag', old_tag);
    569 % Get all the axes and gui objects
    570 axs = get(fh, 'Children');
    571 % Find the objects to save
    572 ah = findobj(axs, 'Tag', 'ObjectToCopy');
    573 if numel(ah) ~= 1
    574     close(fh);
    575     error('Too many axes found');
    576 end
    577 I = true(size(axs));
    578 I(axs==ah) = false;
    579 % Set the axes tag to what it should be
    580 set(ah, 'Tag', old_tag);
    581 % Keep any legends which overlap the subplot
    582 ax_pos = get(ah, 'OuterPosition');
    583 ax_pos(3:4) = ax_pos(3:4) + ax_pos(1:2);
    584 for ah = findobj(axs, 'Tag', 'legend', '-or', 'Tag', 'Colorbar')'
    585     leg_pos = get(ah, 'OuterPosition');
    586     leg_pos(3:4) = leg_pos(3:4) + leg_pos(1:2);
    587     % Overlap test
    588     if leg_pos(1) < ax_pos(3) && leg_pos(2) < ax_pos(4) &&...
    589        leg_pos(3) > ax_pos(1) && leg_pos(4) > ax_pos(2)
    590         I(axs==ah) = false;
    591     end
    592 end
    593 % Delete all axes except for the input axes and associated items
    594 delete(axs(I));
    595 return
    596 
    597 function fh = copyfig(fh)
    598 % Is there a legend?
    599 if isempty(findobj(fh, 'Type', 'axes', 'Tag', 'legend'))
    600     % Safe to copy using copyobj
    601     fh = copyobj(fh, 0);
    602 else
    603     % copyobj will change the figure, so save and then load it instead
    604     tmp_nam = [tempname '.fig'];
    605     hgsave(fh, tmp_nam);
    606     fh = hgload(tmp_nam);
    607     delete(tmp_nam);
    608 end
    609 return
    610 
    611 function A = downsize(A, padval, factor)
     587function A = downsize(A, factor)
    612588% Downsample an image
    613589if factor == 1
     
    621597    % No image processing toolbox - resize manually
    622598    % Lowpass filter - use Gaussian as is separable, so faster
    623     switch factor
    624         case 4
    625             % sigma: 1.7
    626             filt = single([0.0148395 0.0498173 0.118323 0.198829 0.236384 0.198829 0.118323 0.0498173 0.0148395]);
    627         case 3
    628             % sigma: 1.35
    629             filt = single([0.025219 0.099418 0.226417 0.297892 0.226417 0.099418 0.025219]);
    630         case 2
    631             % sigma: 1.0
    632             filt = single([0.054489 0.244201 0.40262 0.244201 0.054489]);
    633     end
     599    % Compute the 1d Gaussian filter
     600    filt = (-factor-1:factor+1) / (factor * 0.6);
     601    filt = exp(-filt .* filt);
     602    % Normalize the filter
     603    filt = single(filt / sum(filt));
     604    % Filter the image
    634605    padding = floor(numel(filt) / 2);
    635     if numel(padval) == 3 && padval(1) == padval(2) && padval(2) == padval(3)
    636         padval = padval(1);
    637     end
    638     if numel(padval) == 1
    639         B = repmat(single(padval), [size(A, 1) size(A, 2)] + (2 * padding));
    640     end
    641606    for a = 1:size(A, 3)
    642         if numel(padval) == 3
    643             B = repmat(single(padval(a)), [size(A, 1) size(A, 2)] + (2 * padding));
    644         end
    645         B(padding+1:end-padding,padding+1:end-padding) = A(:,:,a);
    646         A(:,:,a) = conv2(filt, filt', B, 'valid');
    647     end
    648     clear B
     607        A(:,:,a) = conv2(filt, filt', single(A([ones(1, padding) 1:end repmat(end, 1, padding)],[ones(1, padding) 1:end repmat(end, 1, padding)],a)), 'valid');
     608    end
    649609    % Subsample
    650     A = A(2:factor:end,2:factor:end,:);
     610    A = A(1+floor(mod(end-1, factor)/2):factor:end,1+floor(mod(end-1, factor)/2):factor:end,:);
    651611end
    652612return
     
    719679    end
    720680end
    721 % Crop the background
    722 A = A(t:b,l:r,:);
    723 v = [t b l r];
     681% Crop the background, leaving one boundary pixel to avoid bleeding on
     682% resize
     683v = [max(t-1, 1) min(b+1, h) max(l-1, 1) min(r+1, w)];
     684A = A(v(1):v(2),v(3):v(4),:);
     685return
     686
     687function eps_remove_background(fname)
     688% Remove the background of an eps file
     689% Open the file
     690fh = fopen(fname, 'r+');
     691if fh == -1
     692    error('Not able to open file %s.', fname);
     693end
     694% Read the file line by line
     695while true
     696    % Get the next line
     697    l = fgets(fh);
     698    if isequal(l, -1)
     699        break; % Quit, no rectangle found
     700    end
     701    % Check if the line contains the background rectangle
     702    if isequal(regexp(l, ' *0 +0 +\d+ +\d+ +rf *[\n\r]+', 'start'), 1)
     703        % Set the line to whitespace and quit
     704        l(1:regexp(l, '[\n\r]', 'start', 'once')-1) = ' ';
     705        fseek(fh, -numel(l), 0);
     706        fprintf(fh, l);
     707        break;
     708    end
     709end
     710% Close the file
     711fclose(fh);
    724712return
    725713
  • issm/trunk/externalpackages/export_fig/fix_lines.m

    r7182 r10635  
    4141try
    4242    fstrm = fread(fh, '*char')';
    43 catch
     43catch ex
    4444    fclose(fh);
    45     rethrow(lasterror);
     45    rethrow(ex);
    4646end
    4747fclose(fh);
     
    113113
    114114% Isolate line style definition section
    115 first_sec = findstr(fstrm, '% line types:');
     115first_sec = strfind(fstrm, '% line types:');
    116116[second_sec remaining] = strtok(fstrm(first_sec+1:end), '/');
    117 [dummy remaining] = strtok(remaining, '%');
     117[remaining remaining] = strtok(remaining, '%');
    118118
    119119% Define the new styles, including the new GR format
     
    147147    fprintf(fh, '%s\r', new_style{:});
    148148    fwrite(fh, remaining, 'char*1');
    149 catch
     149catch ex
    150150    fclose(fh);
    151     rethrow(lasterror);
     151    rethrow(ex);
    152152end
    153153fclose(fh);
  • issm/trunk/externalpackages/export_fig/ghostscript.m

    r7810 r10635  
    2929% Windows systems.
    3030
     31% 27/4/11 - Find 64-bit Ghostscript on Windows. Thanks to Paul Durack and
     32% Shaun Kline for pointing out the issue
     33
     34% 4/5/11 - Thanks to David Chorlian for pointing out an alternative
     35% location for gs on linux.
     36
    3137% Call ghostscript
    3238[varargout{1:nargout}] = system(sprintf('"%s" %s', gs_path, cmd));
     
    3642% Return a valid path
    3743% Start with the currently set path
    38 path = current_gs_path;
     44path = user_string('ghostscript');
    3945% Check the path works
    4046if check_gs_path(path)
     
    4349% Check whether the binary is on the path
    4450if ispc
    45     bin = 'gswin32c.exe';
     51    bin = {'gswin32c.exe', 'gswin64c.exe'};
    4652else
    47     bin = 'gs';
     53    bin = {'gs'};
    4854end
    49 if check_store_gs_path(bin)
    50     path = bin;
    51     return
     55for a = 1:numel(bin)
     56    path = bin{a};
     57    if check_store_gs_path(path)
     58        return
     59    end
    5260end
    5361% Search the obvious places
     
    5967        dir_list = dir(default_location);
    6068    end
    61     executable = '\bin\gswin32c.exe';
     69    executable = {'\bin\gswin32c.exe', '\bin\gswin64c.exe'};
    6270    ver_num = 0;
    6371    % If there are multiple versions, use the newest
     
    6573        ver_num2 = sscanf(dir_list(a).name, 'gs%g');
    6674        if ~isempty(ver_num2) && ver_num2 > ver_num
    67             path2 = [default_location dir_list(a).name executable];
    68             if exist(path2, 'file') == 2
    69                 path = path2;
    70                 ver_num = ver_num2;
     75            for b = 1:numel(executable)
     76                path2 = [default_location dir_list(a).name executable{b}];
     77                if exist(path2, 'file') == 2
     78                    path = path2;
     79                    ver_num = ver_num2;
     80                end
    7181            end
    7282        end
    7383    end
     84    if check_store_gs_path(path)
     85        return
     86    end
    7487else
    75     path = '/usr/local/bin/gs';
    76 end
    77 if check_store_gs_path(path)
    78     return
     88    bin = {'/usr/bin/gs', '/usr/local/bin/gs'};
     89    for a = 1:numel(bin)
     90        path = bin{a};
     91        if check_store_gs_path(path)
     92            return
     93        end
     94    end
    7995end
    8096% Ask the user to enter the path
     
    93109    bin_dir = {'', ['bin' filesep], ['lib' filesep]};
    94110    for a = 1:numel(bin_dir)
    95         path = [base bin_dir{a} bin];
    96         if exist(path, 'file') == 2
    97             break;
     111        for b = 1:numel(bin)
     112            path = [base bin_dir{a} bin{b}];
     113            if exist(path, 'file') == 2
     114                if check_store_gs_path(path)
     115                    return
     116                end
     117            end
    98118        end
    99119    end
    100     if check_store_gs_path(path)
    101         return
    102     end
    103120end
    104 error('Ghostscript not found.');
     121error('Ghostscript not found. Have you installed it from www.ghostscript.com?');
    105122
    106123function good = check_store_gs_path(path)
     
    111128end
    112129% Update the current default path to the path found
    113 if change_value(path, 'current_gs_path_str', [mfilename('fullpath') '.m'])
    114     warning('Path to ghostscript installation could not be saved. Enter it manually in ghostscript.m.');
     130if ~user_string('ghostscript', path)
     131    warning('Path to ghostscript installation could not be saved. Enter it manually in ghostscript.txt.');
    115132    return
    116133end
     
    122139good = good == 0;
    123140return
    124 
    125 function current_gs_path_str = current_gs_path
    126 current_gs_path_str = 'gs';
    127 return
  • issm/trunk/externalpackages/export_fig/license.txt

    r7182 r10635  
    1 Copyright (c) 20010, Oliver Woodford
     1Copyright (c) 2011, Oliver Woodford
    22All rights reserved.
    33
  • issm/trunk/externalpackages/export_fig/pdftops.m

    r7182 r10635  
    3636% Return a valid path
    3737% Start with the currently set path
    38 path = current_xpdf_path;
     38path = user_string('pdftops');
    3939% Check the path works
    4040if check_xpdf_path(path)
     
    9393end
    9494% Update the current default path to the path found
    95 if change_value(path, 'current_xpdf_path_str', [mfilename('fullpath') '.m'])
    96     warning('Path to pdftops executable could not be saved. Enter it manually in pdftops.m.');
     95if ~user_string('pdftops', path)
     96    warning('Path to pdftops executable could not be saved. Enter it manually in pdftops.txt.');
    9797    return
    9898end
     
    106106good = ~isempty(strfind(message, 'PostScript'));
    107107return
    108 
    109 function current_xpdf_path_str = current_xpdf_path
    110 current_xpdf_path_str = 'C:\Program Files\xpdf-3.02pl4-win32\pdftops.exe';
    111 return
  • issm/trunk/externalpackages/export_fig/print2array.m

    r7182 r10635  
    66%   A = print2array(figure_handle, resolution)
    77%   A = print2array(figure_handle, resolution, renderer)
     8%   [A bcol] = print2array(...)
    89%
    910% This function outputs a bitmap image of the given figure, at the desired
     
    2223% OUT:
    2324%   A - MxNx3 uint8 image of the figure.
     25%   bcol - 1x3 uint8 vector of the background color
    2426
    25 % Copyright (C) Oliver Woodford 2008-2010
     27% Copyright (C) Oliver Woodford 2008-2011
    2628
    27 function A = print2array(fig, res, renderer)
     29% 5/9/2011 Set EraseModes to normal when using opengl or zbuffer renderers.
     30% Thanks to Pawel Kocieniewski for reporting the issue.
     31
     32% 21/9/2011 Bug fix: unit8 -> uint8!
     33% Thanks to Tobias Lamour for reporting the issue.
     34
     35% 14/11/2011 Bug fix: stop using hardcopy(), as it interfered with figure
     36% size and erasemode settings. Makes it a bit slower, but more reliable.
     37% Thanks to Phil Trinh and Meelis Lootus for reporting the issues.
     38
     39function [A bcol] = print2array(fig, res, renderer)
    2840% Generate default input arguments, if needed
    2941if nargin < 2
     
    3850px = get(fig, 'Position');
    3951set(fig, 'Units', old_mode);
    40 px = prod(px(3:4)*res)/1e6;
    41 if px > 30
     52npx = prod(px(3:4)*res)/1e6;
     53if npx > 30
    4254    % 30M pixels or larger!
    43     warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', px);
     55    warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', npx);
    4456end
     57% Retrieve the background colour
     58bcol = get(fig, 'Color');
    4559% Set the resolution parameter
    46 res = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
     60res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
    4761% Generate temporary file name
    4862tmp_nam = [tempname '.tif'];
     
    5367    try
    5468        % Export to tiff using ghostscript
    55         ghostscript(['-dEPSCrop -q -dNOPAUSE -dBATCH ' res ' -sDEVICE=tiff24nc -sOutputFile="' tmp_nam '" "' tmp_eps '"']);
     69        ghostscript(['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc -sOutputFile="' tmp_nam '" "' tmp_eps '"']);
    5670    catch
    5771        % Delete the intermediate file
     
    6579    % Delete the temporary bitmap file
    6680    delete(tmp_nam);
    67     % Retrieve the background colour
    68     bcol = get(fig, 'Color');
    6981    % Set border pixels to the correct colour
    70     if ~isequal(bcol, 'none') && ~isequal(bcol, [1 1 1])
     82    if isequal(bcol, 'none')
     83        bcol = [];
     84    elseif isequal(bcol, [1 1 1])
     85        bcol = uint8([255 255 255]);
     86    else
    7187        for l = 1:size(A, 2)
    7288            if ~all(reshape(A(:,l,:) == 255, [], 1))
     
    89105            end
    90106        end
    91         bcol = round(bcol * 255);
     107        bcol = median([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A(:,[t b],:), [], size(A, 3))], 1);
    92108        for c = 1:size(A, 3)
    93109            A(:,[1:l-1, r+1:end],c) = bcol(c);
     
    99115        renderer = '-opengl';
    100116    end
     117    err = false;
    101118    % Set paper size
    102119    old_mode = get(fig, 'PaperPositionMode');
    103120    set(fig, 'PaperPositionMode', 'auto');
    104121    try
    105         % Try hardcopy first - undocumented MATLAB function!
    106         A = hardcopy(fig, ['-D' renderer(2:end)], res);
    107     catch
    108122        % Print to tiff file
    109         print(fig, renderer, res, '-dtiff', tmp_nam);
     123        print(fig, renderer, res_str, '-dtiff', tmp_nam);
    110124        % Read in the printed file
    111125        A = imread(tmp_nam);
    112126        % Delete the temporary file
    113127        delete(tmp_nam);
     128    catch ex
     129        err = true;
    114130    end
    115131    % Reset paper size
    116132    set(fig, 'PaperPositionMode', old_mode);
     133    % Throw any error that occurred
     134    if err
     135        rethrow(ex);
     136    end
     137    % Set the background color
     138    if isequal(bcol, 'none')
     139        bcol = [];
     140    else
     141        bcol = bcol * 255;
     142        if isequal(bcol, round(bcol))
     143            bcol = uint8(bcol);
     144        else
     145            bcol = squeeze(A(1,1,:));
     146        end
     147    end
     148end
     149% Check the output size is correct
     150if isequal(res, round(res))
     151    px = [px([4 3])*res 3];
     152    if ~isequal(size(A), px)
     153        % Correct the output size
     154        A = A(1:min(end,px(1)),1:min(end,px(2)),:);
     155    end
    117156end
    118157return
  • issm/trunk/externalpackages/export_fig/print2eps.m

    r7182 r10635  
    1818%   options - Additional parameter strings to be passed to print.
    1919
    20 % Copyright (C) Oliver Woodford 2008-2009
     20% Copyright (C) Oliver Woodford 2008-2011
    2121
    2222% The idea of editing the EPS file to change line styles comes from Jiro
     
    2424% The idea of changing dash length with line width came from comments on
    2525% fex id: 5743, but the implementation is mine :)
     26
     27% 14/11/2011 Fix a MATLAB bug rendering black or white text incorrectly.
     28% Thanks to Mathieu Morlighem for reporting the issue and obtaining a fix
     29% from TMW.
    2630
    2731function print2eps(name, fig, varargin)
     
    3943old_mode = get(fig, 'PaperPositionMode');
    4044set(fig, 'PaperPositionMode', 'auto');
     45% MATLAB bug fix - black and white text can come out inverted sometimes
     46% Find the white and black text
     47white_text_handles = findobj(fig, 'Type', 'text');
     48M = get(white_text_handles, 'Color');
     49if iscell(M)
     50    M = cell2mat(M);
     51end
     52M = sum(M, 2);
     53black_text_handles = white_text_handles(M == 0);
     54white_text_handles = white_text_handles(M == 3);
     55% Set the font colors slightly off their correct values
     56set(black_text_handles, 'Color', [0 0 0] + eps);
     57set(white_text_handles, 'Color', [1 1 1] - eps);
    4158% Print to eps file
    4259print(fig, options{:}, name);
     60% Reset the font colors
     61set(black_text_handles, 'Color', [0 0 0]);
     62set(white_text_handles, 'Color', [1 1 1]);
    4363% Reset paper size
    4464set(fig, 'PaperPositionMode', old_mode);
     
    4767    fix_lines(name);
    4868catch
    49     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.');
     69    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.');
    5070end
    5171return
Note: See TracChangeset for help on using the changeset viewer.