-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathprint2.m
441 lines (393 loc) · 17.5 KB
/
print2.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
function varargout = print( varargin )
%PRINT Print figure or model. Save to disk as image or MATLAB file.
% SYNTAX:
% print
% PRINT alone sends the current figure to your current printer.
% The size and position of the printed output depends on the figure's
% PaperPosition[mode] properties and your default print command
% as specified in your PRINTOPT.M file.
%
% print -s
% Same as above but prints the current Simulink model.
%
% print -device -options
% You can optionally specify a print device (i.e., an output format such
% as tiff or PostScript or a print driver that controls what is sent to
% your printer) and options that control various characteristics of the
% printed file (i.e., the resolution, the figure to print
% etc.). Available devices and options are described below.
%
% print -device -options filename
% If you specify a filename, MATLAB directs output to a file instead of
% a printer. PRINT adds the appropriate file extension if you do not
% specify one.
%
% print( ... )
% Same as above but this calls PRINT as a MATLAB function instead of
% a MATLAB command. The difference is only in the parenthesized argument
% list. It allows the passing of variables for any of the input
% arguments and is especially useful for passing the handles
% of figures and/or models to print and filenames.
%
% Note: PRINT will produce a warning when printing a figure with a
% ResizeFcn. To avoid the warning, set the PaperPositionMode to 'auto'
% or match figure screen size in the PageSetup dialog.
%
% BATCH PROCESSING:
% You can use the function form of PRINT, which is useful for batch
% printing. For example, you can use a for loop to create different
% graphs and print a series of files whose names are stored in an array:
%
% for i=1:length(fnames)
% print('-dpsc','-r200',fnames(i))
% end
%
% SPECIFYING THE WINDOW TO PRINT
% -f<handle> % Handle Graphics handle of figure to print
% -s<name> % Name of an open Simulink model to print
% h % Figure or model handle when using function form of PRINT
%
% Examples:
% print -f2 % Both commands print Figure 2 using the default driver
% print( 2 ) % and operating system command specified in PRINTOPT.
%
% print -svdp % prints the open Simulink model named vdp
%
% SPECIFYING THE OUTPUT FILE:
% <filename> % String on the command line
% '<filename>' % String passed in when using function form of PRINT
%
% Examples:
% print -dps foo
% fn = 'foo'; print( gcf, '-dps', fn )
% Both save the current figure to a file named 'foo.ps' in the current
% working directory. This file can now be printed to a
% PostScript-compatible printer.
%
% COMMON DEVICE DRIVERS
% Output format is specified by the device driver input argument. This
% argument always starts with '-d' and falls into one of several
% categories:
% Microsoft Windows system device driver options:
% -dwin % Send figure to current printer in monochrome
% -dwinc % Send figure to current printer in color
% -dmeta % Send figure to clipboard (or file) in Metafile format
% -dbitmap % Send figure to clipboard (or file) in bitmap format
% -v % Verbose mode, bring up the Print dialog box
% which is normally suppressed.
%
% Built-in MATLAB Drivers:
% -dps % PostScript for black and white printers
% -dpsc % PostScript for color printers
% -dps2 % Level 2 PostScript for black and white printers
% -dpsc2 % Level 2 PostScript for color printers
%
% -deps % Encapsulated PostScript
% -depsc % Encapsulated Color PostScript
% -deps2 % Encapsulated Level 2 PostScript
% -depsc2 % Encapsulated Level 2 Color PostScript
%
% -dpdf % Color PDF file format
% -dsvg % Scalable Vector Graphics
%
% -djpeg<nn> % JPEG image, quality level of nn (figures only)
% E.g., -djpeg90 gives a quality level of 90.
% Quality level defaults to 75 if nn is omitted.
% -dtiff % TIFF with packbits (lossless run-length encoding)
% compression (figures only)
% -dtiffnocompression % TIFF without compression (figures only)
% -dpng % Portable Network Graphic 24-bit truecolor image
% (figures only)
% -dbmpmono % Monochrome .BMP file format
% -dbmp256 % 8-bit (256-color) .BMP file format
% -dbmp16m % 24-bit .BMP file format
% -dpcxmono % Monochrome PCX file format
% -dpcx16 % Older color PCX file format (EGA/VGA, 16-color)
% -dpcx256 % Newer color PCX file format (256-color)
% -dpcx24b % 24-bit color PCX file format, 3 8-bit planes
% -dpbm % Portable Bitmap (plain format)
% -dpbmraw % Portable Bitmap (raw format)
% -dpgm % Portable Graymap (plain format)
% -dpgmraw % Portable Graymap (raw format)
% -dppm % Portable Pixmap (plain format)
% -dppmraw % Portable Pixmap (raw format)
%
% Examples:
% print -dwinc % Prints current Figure to current printer in color
% print( h, '-djpeg', 'foo') % Prints Figure/model h to foo.jpg
%
% PRINTING OPTIONS
% Options only for use with PostScript and GhostScript drivers:
% -loose % Use Figure's PaperPosition as PostScript BoundingBox
% -append % Append, not overwrite, the graph to PostScript file
% -tiff % Add TIFF preview, EPS files only (implies -loose)
% -cmyk % Use CMYK colors instead of RGB
%
% Options for PostScript, GhostScript, Tiff, Jpeg, and Metafile:
% -r<number> % Dots-per-inch resolution. Defaults to 90 for Simulink,
% 150 for figures in image formats and when
% printing in Z-buffer or OpenGL mode, screen
% resolution for Metafiles and 864 otherwise.
% Use -r0 to specify screen resolution.
% Example:
% print -depsc -tiff -r300 matilda
% Saves current figure at 300 dpi in color EPS to matilda.eps
% with a TIFF preview (at 72 dpi for Simulink models and 150 dpi
% for figures). This TIFF preview will show up on screen if
% matilda.eps is inserted as a Picture in a Word document, but
% the EPS will be used if the Word document is printed on a
% PostScript printer.
%
% Other options for figure windows:
% -Pprinter % Specify the printer. On Windows and Unix.
% -noui % Do not print UI control objects
% -painters % Rendering for printing to be done in Painters mode
% -opengl % Rendering for printing to be done in OpenGL mode
%
% See the Using MATLAB Graphics manual for more information on printing.
%
% See also PRINTOPT, PRINTDLG, ORIENT, IMWRITE, HGSAVE, SAVEAS.
% Copyright 1984-2014 The MathWorks, Inc.
[pj, inputargs] = LocalCreatePrintJob(varargin{:});
%Check the input arguments and flesh out settings of PrintJob
[pj, devices, options ] = inputcheck( pj, inputargs{:} );
try
if LocalHandleSimulink(pj)
return
end
catch me
% We want to see the complete stack in debug mode...
if(pj.DebugMode)
rethrow(me);
else % ...and a simple one in non-debug
throwAsCaller(me);
end
end
%User can find out what devices and options are supported by
%asking for output and giving just the input argument '-d'.
%Do it here rather then inputcheck so stack trace makes more sense.
if strcmp( pj.Driver, '-d' )
if nargout == 0
disp(getString(message('MATLAB:uistring:print:SupportedDevices')))
for i=1:length(devices)
disp([' -d' devices{i}])
end
else
varargout{1} = devices;
varargout{2} = options;
end
%Don't actually print anything if user is inquiring.
return
end
%Be sure to restore pointers on exit.
if pj.UseOriginalHGPrinting
pj = preparepointers( pj );
cleanupHandler = onCleanup(@() restorepointers(pj));
end
%Validate that PrintJob state is ok, that input arguments
%and defaults work together.
pj = validate( pj );
%Handle missing or illegal filename.
%Save possible name first for potential use in later warning.
pj = name( pj );
%If only want to setup the output, do it and early exit.
%Currently this is a PC only thing, opens standard Windows Print dialog.
if setup( pj )
return
end
%Sometimes need help tracking down problems...
if pj.DebugMode
disp(getString(message('MATLAB:uistring:print:PrintJobObject')))
disp(pj)
end
%If handled via new path just return from here, otherwise fall through
pj = alternatePrintPath(pj);
if pj.donePrinting
if pj.RGBImage
varargout(1) = {pj.Return};
end
return;
end
LocalPrint(pj);
end
%--------------------------------------------------------------------------
function pj = LocalPrint(pj)
%Turning on the printjob structure
pj.Active = 1;
% rquist - moved here from above
% Printing parameters like paper position, paper size etc. needs to be
% adjusted for ModelDependencyViewer figure.
LocalAdjustPrintParamsForDepViewer(pj);
%Objects to print have their handles in a cell-array of vectors.
%Each vector is for one page; all objects in that vector
%are printed on the same page. The caller must have set their
%PaperPosition's up so they do not overlap, or do so gracefully.
numPages = length( pj.Handles );
for i = 1 : numPages
%numObjs = length( pj.Handles{i} );
pj = positions( pj, pj.Handles{i} );
%Only prepare and restore the first object.
h = pj.Handles{i}(1);
%Add object to current page
pj.Error = 0;
pj.Exception = [];
%May want to change various properties before printing depending
%upon input arguments and state of other HG/Simulink properties.
pj = prepare( pj, h );
%Call the output driver and render Figure/model to device, file, or clipboard.
%Save erroring out until we restore the objects.
try
if ishghandle(h) && ~localIsPrintHeaderHeaderSpecEmpty(h) && ...
(~pj.DriverExport || ...
strcmp(pj.DriverClass,'MW'))
ph=paperfig(h);
pj=render(pj,ph);
delete(ph);
else
pj = render(pj,pj.Handles{i});
end
if ~isempty(pj.Exception) %pj.Error
rethrow(pj.Exception)
end
catch ex
pj.Error = 1;
pj.Exception = ex;
end
%Reset the properties of a Figure/model after printing.
pj = restore( pj, h );
if pj.Error
if ~isempty(pj.Exception)
throw(pj.Exception);
else
throw(MException('MATLAB:print:PrintFailedException',getString(message('MATLAB:uistring:print:PrintFailedDueToAnUnknownReason'))));
end
end
if i < numPages
%Start a new page for next vector of handles
pj.PageNumber = pj.PageNumber + 1;
end
end
%Close connection with printer or file system.
pj.Active = 0;
if pj.GhostDriver
pj = ghostscript( pj );
elseif strcmp( pj.Driver, 'hpgl' )
hpgl( pj );
end
if pj.PrintOutput
send( pj );
end
end
%--------------------------------------------------------------------------
function done = LocalHandleSimulink(pj)
% Function handle to be used for Simulink/Stateflow printing.
% Make persistent for efficiency (sramaswa)
persistent slSfPrintFcnHandle;
done = false;
if(isSLorSF(pj))
% Printer dialog
if (ispc() && strcmp(pj.Driver,'setup'))
eval('SLM3I.SLDomain.showPrintSetupDialog(pj.Handles{1}(1))');
done = true;
return;
end
if(isempty(slSfPrintFcnHandle))
slSfPrintFcnHandle = LocalGetSlSfPrintFcnHandle;
end
%pj = name(pj);
feval(slSfPrintFcnHandle, pj);
done = true;
end % if(isSLorSF(pj))
end
%--------------------------------------------------------------------------
function [pj, varargin] = LocalCreatePrintJob(varargin)
if ~nargin
varargin = {};
end
handles = checkArgsForHandleToPrint(0, varargin{:});
pj = printjob([handles{:}]);
if ~pj.UseOriginalHGPrinting && ~isempty(varargin)
for idx = 1:length(varargin)
if ischar(varargin{idx}) && strcmp('-printjob', varargin{idx}) && ...
(idx+1) <= length(varargin)
userSuppliedPJ = varargin{idx+1};
pj = pj.updateFromPrintjob(userSuppliedPJ);
varargin = {varargin{1:idx-1} varargin{idx+2:end}};
break;
end
end
end
end
%--------------------------------------------------------------------------
function LocalAdjustPrintParamsForDepViewer(pj)
% Special case to handle the Printing parameters for ModelDependencyViewer
% (a tool used to view model/library dependencies for Simulink Models)
if( length(pj.Handles) == 1 && ...
strcmpi(get(pj.Handles{1},'Tag'),'DAStudio.DepViewer') )
h = pj.Handles{1};
set(h,'PaperUnits','points');
pSize = get(h,'PaperSize');
pPos = get(h,'PaperPosition');
paddingMarginPoints = 10; % printing margin in points
ax = get(h,'CurrentAxes');
set(ax,'Units','points'); % make sure we are all in points
axPos = get(ax,'Position');
% Set the target with and target height and adjust for marigins
targetWidth = min(pSize(1),pPos(3))-paddingMarginPoints;
targetHeight = min(pSize(2),pPos(4))-paddingMarginPoints;
% Get the ratio of width
widthRatio = targetWidth/axPos(3);
heightRatio = targetHeight/axPos(4);
% Get the minimum of the ratio ...
minScale = min(widthRatio,heightRatio);
% .. and apply to the targetWidth and targetHeight
newWidth = axPos(3)*minScale;
newHeight = axPos(4)*minScale;
% Set the new width and height of the axes
set(ax,'Position',[(targetWidth-newWidth)/2 (targetHeight-newHeight)/2 newWidth-2*paddingMarginPoints newHeight-2*paddingMarginPoints]);
% Set the paper position of the figure such that there is 10 pixel
% margin from left and bottom.
set(h,'PaperPosition',[((pSize(1)-targetWidth)/2) + paddingMarginPoints ((pSize(2)-targetHeight)/2) + paddingMarginPoints ...
min(pSize(1),pPos(3)) min(pSize(2),pPos(4))]);
% apply scale to all the texts in the model
texts = findall(h,'type','text');
for k = 1:length(texts)
fontSize = get(texts(k),'FontSize');
set(texts(k),'FontSize',fontSize*minScale);
end
end
end
%--------------------------------------------------------------------------
% This function is needed because the PrintHeaderHeaderSpec may be
% technically empty (string is '' and date is 'none'), but still exist, and
% if it is in this state we do not want to call paperfig and do a copyobj.
% Fix for g408962.
function reallyEmpty = localIsPrintHeaderHeaderSpecEmpty(fig)
reallyEmpty = false; % assume the headerspec is valid and not completely empty
hs = getappdata(fig,'PrintHeaderHeaderSpec');
if isempty(hs) || (~isempty(hs) && strcmp(hs.dateformat,'none') && isempty(hs.string))
reallyEmpty = true; % either no header spec or it doesn't specify any header
end
% end of function localCheckHeaderSpec
end
%--------------------------------------------------------------------------
function fHandle = LocalGetSlSfPrintFcnHandle
cwd = pwd;
try
cd(fullfile(matlabroot,'toolbox','simulink','simulink','private')); %#ok<*MCCD,MCMLR,MCTBX>
fHandle = str2func('slsf_print');
if(~isa(fHandle, 'function_handle'))
throw(MException('MATLAB:UndefinedFunction',sprintf('%s',getString(message('MATLAB:uistring:print:UndefinedFunctionOrVariable','slsf_print')))));
end
catch me
cd(cwd);
rethrow(me);
end
cd(cwd);
end
% [EOF]
% LocalWords: fnames dpsc svdp vdp dps fn ps dwin dwinc dmeta Metafile dbitmap
% LocalWords: dsetup deps depsc dhpgl HPGL djpeg nn dtiff packbits lossless
% LocalWords: dtiffnocompression dpng truecolor online GHOSTSCRIPT ghostscript
% LocalWords: dljet ddeskjet dcdj Deskjet dpaintjet dpcx PCX dppm Pixmap cmyk
% LocalWords: adobecset Jpeg GL