Skip to content

Commit

Permalink
Allow fix_lines to work directly on the file contents
Browse files Browse the repository at this point in the history
  • Loading branch information
ojwoodford committed Nov 18, 2014
1 parent 06c06fc commit 671c3c1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 36 deletions.
61 changes: 25 additions & 36 deletions fix_lines.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function fix_lines(fname, fname2)
%FIX_LINES Improves the line style of eps files generated by print
%
% Examples:
% fix_lines fname
% fix_lines fname fname2
% fstrm_out = fixlines(fstrm_in)
%
% This function improves the style of lines in eps files generated by
% MATLAB's print function, making them more similar to those seen on
Expand All @@ -15,11 +15,15 @@ function fix_lines(fname, fname2)
% order to allow programs such as Ghostscript to find the bounding box
% information.
%
% IN:
%IN:
% fname - Name or path of source eps file.
% fname2 - Name or path of destination eps file. Default: same as fname.
% fstrm_in - File contents of a MATLAB-generated eps file.
%
%OUT:
% fstrm_out - Contents of the eps file with line styles fixed.

% Copyright: (C) Oliver Woodford, 2008-2010
% Copyright: (C) Oliver Woodford, 2008-2014

% The idea of editing the EPS file to change line styles comes from Jiro
% Doke's FIXPSLINESTYLE (fex id: 17928)
Expand All @@ -33,24 +37,22 @@ function fix_lines(fname, fname2)
% Thank you to Laurence K for suggesting the check to see if the file was
% opened.

% Read in the file
fh = fopen(fname, 'r');
if fh == -1
error('File %s not found.', fname);
end
try
fstrm = fread(fh, '*char')';
catch ex
fclose(fh);
rethrow(ex);
function fstrm = fix_lines(fstrm, fname2)

if nargout == 0 || nargin > 1
if nargin < 2
% Overwrite the input file
fname2 = fstrm;
end
% Read in the file
fstrm = read_write_entire_textfile(fstrm);
end
fclose(fh);

% Move any embedded fonts after the postscript header
if strcmp(fstrm(1:15), '%!PS-AdobeFont-')
% Find the start and end of the header
ind = regexp(fstrm, '[\n\r]%!PS-Adobe-');
[ind2 ind2] = regexp(fstrm, '[\n\r]%%EndComments[\n\r]+');
[ind2, ind2] = regexp(fstrm, '[\n\r]%%EndComments[\n\r]+');
% Put the header first
if ~isempty(ind) && ~isempty(ind2) && ind(1) < ind2(1)
fstrm = fstrm([ind(1)+1:ind2(1) 1:ind(1) ind2(1)+1:end]);
Expand All @@ -66,7 +68,7 @@ function fix_lines(fname, fname2)
regexp(fstrm, '[\n\r]DD[\n\r]')];
ind = sort(ind);
% Find line width commands
[ind2 ind3] = regexp(fstrm, '[\n\r]\d* w[\n\r]');
[ind2, ind3] = regexp(fstrm, '[\n\r]\d* w[\n\r]');
% Go through each line style section and swap with any line width commands
% near by
b = 1;
Expand Down Expand Up @@ -113,8 +115,8 @@ function fix_lines(fname, fname2)

% Isolate line style definition section
first_sec = strfind(fstrm, '% line types:');
[second_sec remaining] = strtok(fstrm(first_sec+1:end), '/');
[remaining remaining] = strtok(remaining, '%');
[second_sec, remaining] = strtok(fstrm(first_sec+1:end), '/');
[remaining, remaining] = strtok(remaining, '%');

% Define the new styles, including the new GR format
% Dot and dash lengths have two parts: a constant amount plus a line width
Expand All @@ -131,24 +133,11 @@ function fix_lines(fname, fname2)
'/DD { [1 dom 1.2 dom 4 dam 1.2 dom] 0 setdash 0 setlinecap } bdef',... % Dot dash lines
'/GR { [0 dpi2point mul 4 dpi2point mul] 0 setdash 1 setlinecap } bdef'}; % Grid lines - dot spacing remains constant

if nargin < 2
% Overwrite the input file
fname2 = fname;
end
% Construct the output
fstrm = [fstrm(1:first_sec) second_sec sprintf('%s\r', new_style{:}) remaining];

% Save the file with the section replaced
fh = fopen(fname2, 'w');
if fh == -1
error('Unable to open %s for writing.', fname2);
end
try
fwrite(fh, fstrm(1:first_sec), 'char*1');
fwrite(fh, second_sec, 'char*1');
fprintf(fh, '%s\r', new_style{:});
fwrite(fh, remaining, 'char*1');
catch ex
fclose(fh);
rethrow(ex);
% Write the output file
if nargout == 0 || nargin > 1
read_write_entire_textfile(fname2, fstrm);
end
fclose(fh);
end
37 changes: 37 additions & 0 deletions read_write_entire_textfile.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
%READ_WRITE_ENTIRE_TEXTFILE Read or write a whole text file to/from memory
%
% Read or write an entire text file to/from memory, without leaving the
% file open if an error occurs.
%
% Reading:
% fstrm = read_write_entire_textfile(fname)
% Writing:
% read_write_entire_textfile(fname, fstrm)
%
%IN:
% fname - Pathname of text file to be read in.
% fstrm - String to be written to the file, including carriage returns.
%
%OUT:
% fstrm - String read from the file. If an fstrm input is given the
% output is the same as that input.

function fstrm = read_write_entire_textfile(fname, fstrm)
modes = {'r', 'w'};
writing = nargin > 1;
fh = fopen(fname, modes{1+writing});
if fh == -1
error('Unable to open file %s.', fname);
end
try
if writing
fwrite(fh, fstrm, 'char*1');
else
fstrm = fread(fh, '*char')';
end
catch ex
fclose(fh);
rethrow(ex);
end
fclose(fh);
end

0 comments on commit 671c3c1

Please sign in to comment.