-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathrvcprint.m
188 lines (161 loc) · 5.12 KB
/
rvcprint.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
%RVCPRINT Print figure for RVC book
%
% rvcprint(name, options) from console write to file
% rvcprint(name, options) from script write relative to calling script
% path and use its name
% rvcprint('fig', name, options) as above but use this name
%
% If fig name not given take it from name of calling script
function rvcprint(varargin)
global rvcprintopts
% outputFolder = '../matfigs';
outputFolder = '.';
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
opt.subfig = '';
opt.simulink = [];
opt.fig = [];
opt.thicken = [];
opt.format = {'eps', 'svg', 'pdf', 'jpg'};
opt.tall = false;
opt.normal = false;
opt.grid = true;
opt.force = false;
opt.hidden = [];
opt.cmyk = false; % By default, save figures in RGB.
opt.here = false;
opt.opengl = false;
opt.painters = false;
opt.bgfix = true;
opt.axes = true;
opt.figy = 0;
[opt,args] = tb_optparse(opt, [varargin rvcprintopts]);
if ~isempty(opt.hidden)
set(opt.hidden.Number, 'HandleVisibility', 'on');
opts = { sprintf('-f%d', opt.hidden.Number) };
else
opts = {};
end
if strcmp(opt.format, 'svg')
opt.cmyk = false; % MATLAB gives huge error dump with this set of options
end
if opt.bgfix
set(gca, 'color', 'none') % remove background patch
end
if opt.opengl
opts = [opts '-opengl' '-r600'];
end
% Enforce that the resulting graphics is a vector graphics.
% As of 20a, recommended option is "-vector", not "-painters".
if opt.painters
opts = [opts '-vector'];
end
if opt.cmyk
opts = [opts '-cmyk'];
end
if opt.simulink
% simulink can't write EPS files
opts = [opts sprintf('-s%s', opt.simulink)];
opt.format = 'pdf';
end
switch opt.format
case 'pdf'
opts = [opts '-dpdf' ];
ext = '.pdf';
case 'eps'
opts = [opts '-depsc' ];
ext = '.eps';
case 'svg'
opts = [opts '-dsvg' ];
ext = '.svg';
otherwise
opts = [opts ['-d' opt.format] ];
ext = ['.' opt.format];
end
if opt.tall
p = get(gcf, 'Position');
p(4)=p(4)*1.5;
set(gcf, 'Position', p)
end
if opt.thicken
lines = findobj(gcf, 'Type', 'line');
for line = lines'
if ~strcmp(get(line, 'LineStyle'), 'none')
set(line, 'LineWidth', opt.thicken);
end
end
end
if ~opt.axes
axis off
end
if isempty(opt.hidden)
% when making a hidden handle figure visible, any grid operation seems to
% overlay it with a blank set of axes, clf makes it go away, very odd
if opt.grid
grid on
else
grid off
end
end
% find who called us
% - first element is self, second element is caller, etc
st = dbstack('-completenames');
if length(st) == 1
% called from the console
% use the name given
if length(args) < 1
% this is useful when testing figure scripts from the command line
fprintf('rvcprint: ignoring request\n');
pause(2)
return
end
filename = args{1};
args = args{2:end};
else
% called from a script, write relative to script path and use its name
% build a path from script name
[pth, name] = fileparts(st(2).file);
if ~isempty(opt.fig)
% no file name given, try to be tricky and get it from the name of the
% calling script
name = opt.fig;
end
if opt.here
filename = fullfile(pth, [name opt.subfig ext]);
else
filename = fullfile(pth, outputFolder, [name opt.subfig ext]);
end
end
opts = [opts args];
% make it so
fig = gcf;
if ~opt.simulink
fig.PaperPositionMode = 'auto';
end
if opt.figy ~= 0
% Change figure y size. To avoid axes scaling, set units to pixels.
ax = gca;
ax.Units = "pixels";
fig.Position(2) = fig.Position(2) - opt.figy;
fig.Position(4) = fig.Position(4) + opt.figy;
ax.Position(2) = ax.Position(2) + round(opt.figy/2);
end
fprintf('writing file [%s] --> %s\n', strjoin(opts), filename);
if strcmp(opt.format, 'jpg')
% For jpeg export use exportgraphics to get tighter margins
% Export JPEG images at 600 dpi
exportgraphics(gca, filename, Resolution=600);
else
print(opts{:}, args{:}, filename);
end
% Automatically crop PDF to remove any margins that MATLAB added
% This only works if "pdfcrop" is on the path. If you installed
% TeXLive, this should be the case.
if strcmp(opt.format, 'pdf')
[status,result] = system("pdfcrop """ + filename + """ """ + filename + """");
if status
disp("Call to pdfcrop failed with error message: " + result);
end
end
end