home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / GRAPHICS.DI$ / PRINT.M < prev    next >
Encoding:
Text File  |  1993-03-26  |  14.7 KB  |  512 lines

  1. function print(arg1,arg2,arg3,arg4,arg5)
  2. %PRINT  Print graph or save graph to file.
  3. %       PRINT <filename> saves the current Figure window as PostScript
  4. %       or a printer specific format, as specified by PRINTOPT. If
  5. %       a filename is specified, the output is written to the designated file,
  6. %       overwriting it if it already exists. If the specified filename does not
  7. %       include an extension, an appropriate one is appended.
  8. %       If the filename is omitted, the Figure is sent directly to the
  9. %       printer as specified in PRINTOPT.
  10. %
  11. %       PRINT <filename> -f<system name> prints the specified SIMULINK
  12. %           system.
  13. %
  14. %       Syntax: PRINT [ -ddevice] [ -options ] <filename>
  15. %
  16. %       Available Windows device options are:
  17. %          -dwin    - Send figure to currently installed printer in monochrome
  18. %          -dwinc   - Send figure to currently installed printer in color
  19. %          -dmeta   - Send figure to clipboard in Metafile format
  20. %          -dbitmap - Send figure to clipboard in bitmap format
  21. %          -dsetup  - Bring up Print Setup dialog box, but do not print
  22. %
  23. %       Other Windows options are:
  24. %          -v       - Verbose mode, bring up the Print dialog box
  25. %                     which is normally suppressed.
  26. %
  27. %       Available Postscript devices are:
  28. %          -dps    - PostScript for black and white printers
  29. %          -dpsc   - PostScript for color printers
  30. %          -dps2   - Level 2 PostScript for black and white printers
  31. %          -dpsc2  - Level 2 PostScript for color printers
  32. %
  33. %          -deps   - Encapsulated PostScript (EPSF)
  34. %          -depsc  - Encapsulated Color PostScript (EPSF)
  35. %          -deps2  - Encapsulated Level 2 PostScript (EPSF)
  36. %          -depsc2 - Encapsulated Level 2 Color PostScript (EPSF)
  37. %
  38. %       Additional GhostScript devices are:
  39. %          -dlaserjet - HP LaserJet
  40. %          -dljetplus - HP LaserJet+
  41. %          -dljet2p   - HP LaserJet IIP
  42. %          -dljet3    - HP LaserJet III
  43. %          -dcdeskjet - HP DeskJet 500C with 1 bit/pixel color
  44. %          -dcdjcolor - HP DeskJet 500C with 24 bit/pixel color and
  45. %                           high-quality color (Floyd-Steinberg) dithering
  46. %          -dcdjmono  - HP DeskJet 500C printing black only
  47. %          -ddeskjet  - HP DeskJet and DeskJet Plus
  48. %          -dpaintjet - HP PaintJet color printer
  49. %          -dpjetxl   - HP PaintJet XL color printer
  50. %          -dbj10e    - Canon BubbleJet BJ10e
  51. %          -dln03     - DEC LN03 printer
  52. %          -depson    - Epson-compatible dot matrix printers (9- or 24-pin)
  53. %          -deps9high - Epson-compatible 9-pin, interleaved lines 
  54. %                          (triple resolution)
  55. %          -depsonc   - Epson LQ-2550 and Fujitsu 3400/2400/1200 color printers
  56. %          -dgif8     - 8-bit color GIF file format
  57. %          -dpcx16    - Older color PCX file format (EGA/VGA, 16-color)
  58. %          -dpcx256   - Newer color PCX file format (256-color)
  59. %
  60. %       Other options are:
  61. %          -append   - Append the graph to file, rather than overwriting
  62. %          -epsi     - Add 1-bit deep bitmap preview (EPSI format)
  63. %          -Pprinter - Specfiy the printer to use
  64. %          -fhandle  - Handle Graphics handle of figure to print
  65. %          -fname    - Name of SIMULINK system window to print
  66. %
  67. %       See also PRINTOPT, ORIENT.
  68.  
  69. %       Modified 1 March 93
  70. %       Copyright (c) 1984-93 by The MathWorks, Inc.
  71.  
  72. %
  73. % ---------------------------------------------- User modifiable values.
  74.  
  75. %maximum for width or height of preview window, in pixels
  76. maxPrevSize = 256; 
  77.  
  78. % ---------------------------------------------- End user modifiable values.
  79. %
  80.  
  81. %
  82. % List of all supported devices used for input validation.
  83. %
  84. % The first column contains the device name, the second column contains the
  85. % default filename extension, the third column indicates what type of output
  86. % device is employed, and the fourth indicates Monochrome or Color device.
  87. %
  88. device_table = [
  89. %Postscript device options
  90.     'ps      '      'ps '   'PS'    'M' 
  91.     'psc     '      'ps '   'PS'    'C'
  92.     'ps2     '      'ps '   'PS'    'M'
  93.     'ps2c    '      'ps '   'PS'    'C'
  94.     'psc2    '      'ps '   'PS'    'C'
  95.     'eps     '      'eps'   'EP'    'M'
  96.     'epsc    '      'eps'   'EP'    'C'
  97.     'eps2    '      'eps'   'EP'    'M'
  98.     'eps2c   '      'eps'   'EP'    'C'
  99.     'epsc2   '      'esp'   'EP'    'C'
  100. %GhostScript device options
  101.     'laserjet'      'jet'   'GS'    'M'
  102.     'ljetplus'      'jet'   'GS'    'M'
  103.     'ljet2p  '      'jet'   'GS'    'M'
  104.     'ljet3   '      'jet'   'GS'    'M'
  105.     'cdeskjet'      'jet'   'GS'    'C'
  106.     'cdjcolor'      'jet'   'GS'    'C'
  107.     'cdjmono '      'jet'   'GS'    'M'
  108.     'deskjet '      'jet'   'GS'    'M'
  109.     'paintjet'      'jet'   'GS'    'C'
  110.     'pjetxl  '      'jet'   'GS'    'C'
  111.     'bj10e   '      'jet'   'GS'    'M'
  112.     'ln03    '      'ln3'   'GS'    'M'
  113.     'epson   '      'ep '   'GS'    'M'
  114.     'eps9high'      'ep '   'GS'    'M'
  115.     'epsonc  '      'ep '   'GS'    'C'
  116.     'gif8    '      'gif'   'GS'    'C'
  117.     'pcx16   '      'pcx'   'GS'    'C'
  118.     'pcx256  '      'pcx'   'GS'    'C'
  119. % Microsoft Windows-specific options
  120.     'win     '      '   '   'MW'    'M'
  121.     'winc    '      '   '   'MW'    'C'
  122.     'meta    '      '   '   'MW'    'C'
  123.     'bitmap  '      '   '   'MW'    'C'
  124.     'setup   '      '   '   'MW'    'M'
  125. ];
  126.  
  127. options = [
  128.     'v     '
  129.     'epsi  '
  130.     'append'
  131. ];
  132.  
  133. devices = device_table(:, 1:8);
  134. extensions = device_table(:, 9:11);
  135. classes = device_table(:, 12:13);
  136. colorDevs = device_table(:, 14 );
  137.  
  138. comp = computer;
  139. ghostScriptDevice = [];
  140. printplot = 0;
  141. num_opt_args = 0;
  142. filename = [];
  143. printer = [];
  144. hasPreview = 0;
  145. prtSim = 0;
  146. window = get( 0, 'Children' );
  147. dev = [];
  148.  
  149. [lprcmd, defaultDevice ] = printopt;
  150.  
  151. for i=1:nargin
  152.     cur_arg = eval(['arg', num2str(i)]);
  153.     if (cur_arg(1) ~= '-')
  154.         if isempty(filename)
  155.             filename = cur_arg;
  156.         else
  157.             error( [ 'Multiple inputs that look like filenames: ''' ...
  158.                     filename ''' and ''' cur_arg '''' ] );
  159.         end
  160.  
  161.     elseif (cur_arg(2) == 'd')
  162.         %
  163.         % verify device given is supported, and only one given
  164.         % device proper starts after '-d', if only '-d'
  165.         % we echo out possible choices
  166.         %
  167.         if ~isempty( dev )
  168.             error( [ 'Multiple inputs that look like device names: ''' ...
  169.                     dev ''' and ''' cur_arg ''''] );
  170.         end
  171.  
  172.         match = 0;
  173.         wasError = 0;
  174.         if ( size(cur_arg, 2) > 2 )
  175.             wasError = 1;
  176.             % Find index of device in table, used a lot later on.
  177.             for devIndex = 1:size(devices,1)
  178.                 if strcmp( cur_arg(3:size(cur_arg,2)), ...
  179.                             deblank(devices(devIndex,:)) )
  180.                     dev = deblank( cur_arg );
  181.                     break;
  182.                 end
  183.             end
  184.             if isempty(dev)
  185.                 disp('Illegal device specified.');
  186.             end
  187.         end
  188.         if isempty(dev)
  189.             disp('Supported devices are:')
  190.             if (comp(1:2) =='PC')
  191.                 disp(devices);
  192.             else
  193.                 % Find first non PC specific device
  194.                 for i = size( classes, 1 ) : -1 : 1
  195.                     if ~strcmp( classes(i,:), 'MW' )
  196.                         break;
  197.                     end
  198.                 end
  199.                 disp( devices( 1 : i, : ) )
  200.             end
  201.             if wasError
  202.                 error(' ');
  203.             else
  204.                 return;
  205.             end
  206.         end
  207.     elseif (cur_arg(2) == 'P')
  208.         printer = cur_arg(3:length(cur_arg));
  209.         lprcmd = [lprcmd,' -P',printer];
  210.  
  211.     elseif (cur_arg(2) == 'f')
  212.         if ( exist('open_system') ~= 5 )
  213.             error('SIMULINK is not available in this version of MATLAB.');
  214.         end
  215.         window = cur_arg( 3:length(cur_arg) );
  216.         %SIMULINK model names are alphanumeric
  217.         if ~( all( window > 47 ) & all( window < 58 ) )
  218.             prtSim = 1;
  219.             if ( exist( window ) == 4 )
  220.                 openedSimWindow = 0;
  221.             else
  222.                 eval( window );
  223.                 openedSimWindow = 1;
  224.             end
  225.         else
  226.             window = eval( window );
  227.         end
  228.  
  229.     else
  230.         %
  231.         % verify any given options are supported
  232.         %
  233.         match = 0;
  234.         if size(cur_arg,2) > 1
  235.             op = cur_arg(2:size(cur_arg,2));
  236.             for r = 1:size(options,1)
  237.                 c = min(size(op,2), size(options(r,:),2));
  238.                 if strcmp(op(1:c), options(r,1:c))
  239.                     if ( strcmp( op, 'epsi' ) )
  240.                         hasPreview = 1;
  241.                         %
  242.                         % HARDCOPY code actually wants to see 'preview'.
  243.                         %
  244.                         cur_arg = '-preview';
  245.                     end
  246.                     match = 1;
  247.                     break;
  248.                 end
  249.             end
  250.         end
  251.         if ~match
  252.             error(['Illegal option ''' op ''' given.'])
  253.         end
  254.         num_opt_args = num_opt_args + 1;
  255.         eval(['opt_arg', num2str(num_opt_args), ' = cur_arg;']);
  256.     end
  257. end
  258.  
  259. % If no device given, use default from PRINTOPT
  260. if isempty( dev )
  261.  
  262.     % Find device name in list of devices to set the
  263.     % appropriate filename extension.
  264.     for devIndex = 1 : size(devices,1)
  265.         %skip '-d'
  266.         d = defaultDevice( 3 : size(defaultDevice,2) );
  267.         if strcmp( devices(devIndex, 1 : size(d,2)), d )
  268.             dev = defaultDevice;
  269.                break;
  270.         end
  271.     end
  272.     if isempty( dev )
  273.         error(['PRINTOPT specifies an unknown device type '''...
  274.             defaultDevice ''''])
  275.     end
  276. end
  277.  
  278. devClass = classes(devIndex, :);
  279. if ( devClass == 'GS' )
  280.     %
  281.     % Remember actual device, get MATLAB to produce PostScript for later
  282.     % conversion by GhostScript
  283.     %
  284.     ghostScriptDevice = deblank(devices(devIndex,:));
  285.     if ( colorDevs(devIndex ) == 'C' )
  286.         dev = '-dpsc';
  287.     else
  288.         dev = '-dps';
  289.     end
  290. end
  291.  
  292. extension = deblank(extensions( devIndex, : ));
  293.  
  294. if ~prtSim
  295.     %window is array of handles of all figure windows
  296.     if isempty( window )
  297.         error( 'No figure window to print' );
  298.     else
  299.         window = window(1);
  300.     end
  301.  
  302.     % Create Handle Graphics objects on screen if not already there;
  303.     % but, if going to change colors and bring it to front
  304.     % for EPSI preview, do not drawnow now, but after color changes.
  305.     if ~( hasPreview & ...
  306.         (strcmp( 'on', get(window,'InvertHardcopy') ) ...
  307.         | colorDevs( devIndex ) == 'M' ) )
  308.             drawnow
  309.     end
  310. end
  311.  
  312. if isempty(filename)
  313.     %EPS files shouldn't go to printer, generate file on disk
  314.     if ( devClass == 'EP' )
  315.         if prtSim
  316.             filename = window;
  317.         else
  318.             filename = [ 'figure' int2str( window ) ];
  319.         end
  320.         tellUserFilename = 1;
  321.     else
  322.         % Generate a unique name for temporary file
  323.         filename = tempname;
  324.         if (devClass ~= 'MW')
  325.             printplot = 1;
  326.         end
  327.     end
  328. else
  329.     tellUserFilename = 0;
  330.  
  331.     % Hack, if user specifies a filename while device is -dwin
  332.     % or -dwinc, either because the user gave that device or, more
  333.     % likely, its the default, and since the filename is useless
  334.     % with Windows driver anyway, we'll assume the user really wants
  335.     % a PostScript file. This is because 'print foo' is easier
  336.     % to type then 'print -dps foo' and probably more commonly
  337.     % meant if a filename is given.
  338.     if (devClass == 'MW') & (strcmp( dev, '-dwin' ) | strcmp( dev, '-dwinc' ))
  339.         if ( colorDevs(devIndex ) == 'C' )
  340.             dev = '-dpsc';
  341.         else
  342.             dev = '-dps';
  343.         end
  344.         extension = 'ps';
  345.     end
  346. end
  347.  
  348. % Append appropriate extension to filename if it doesn't have
  349. % one, and we've determined a good one.
  350. if ~any(filename == '.') & ~isempty( extension )
  351.     filename = [ filename '.' extension ];
  352. end
  353.  
  354. if tellUserFilename
  355.     disp( 'Encapsulated PostScript files can not be sent to printer.' );
  356.     disp( [ 'File saved to disk under name ''' filename '''.' ] );
  357. end
  358.  
  359. if (strcmp('setup', dev(3:size(dev,2))))
  360.     hardcopy( window , filename, dev)
  361.     return
  362. end
  363.  
  364. if ~prtSim
  365.     % Invert B&W color properties of Figure and child objects
  366.     if strcmp( 'on', get(window,'InvertHardcopy') )
  367.         invertedColor = 1;
  368.         cinvert( window );
  369.     else
  370.         invertedColor = 0;
  371.     end
  372.  
  373.     % if not color, set lines and text to a color contrasting background
  374.     if ( colorDevs(devIndex ) == 'M' )
  375.         lineTextColors = blt( window, get(window,'color') );
  376.     else
  377.         lineTextColors = [];
  378.     end
  379.  
  380.     % If including preview, and changed any of the colors, update figure
  381.     % raise it to top of stacking order for screen capture.
  382.     % Resize the figure if its larger then the biggest preview allowed
  383.     % while maintaining its current aspect ratio.
  384.     if hasPreview
  385.         %
  386.         % Get current size info in pixel coordinates.
  387.         %
  388.         origUnits = get( window, 'units' );
  389.         set( window, 'units', 'Pixels' );
  390.         frect = get( window,'pos');
  391.         fullW = frect(3);
  392.         fullH = frect(4);
  393.  
  394.         if fullW > maxPrevSize | fullH > maxPrevSize
  395.             %
  396.             % One dimension needs to shrink more then the other.
  397.             %
  398.             scale = min( [ maxPrevSize/fullW  maxPrevSize/fullH ] );
  399.             w = fix( fullW * scale );
  400.             h = fix( fullH * scale );
  401.             set( window, 'Position', [ frect(1) frect(2)+fullH w h ] )
  402.         end
  403.  
  404.         % Bring figure to front
  405.         figure( window );
  406.  
  407.         % Render Handle Graphics objects in new state for EPSI preview.
  408.         drawnow
  409.  
  410.     end % hasPreview
  411. end     % ~prtSim
  412.  
  413. if ( devClass == 'GS' )
  414.     orig_filename = filename;
  415.     filename = [tempname '.ps'];
  416. end
  417.  
  418. if (num_opt_args == 0)
  419.     hardcopy( window, filename, dev)
  420. elseif (num_opt_args == 1)
  421.     hardcopy( window, filename, dev, opt_arg1)
  422. elseif (num_opt_args == 2)
  423.     hardcopy( window, filename, dev, opt_arg1, opt_arg2)
  424. elseif (num_opt_args == 3)
  425.     hardcopy( window, filename, dev, opt_arg1, opt_arg2, opt_arg3)
  426. elseif (num_opt_args == 4)
  427.     hardcopy( window, filename, dev, opt_arg1, opt_arg2, opt_arg3, opt_arg4)
  428. end
  429.  
  430.  
  431. if ~prtSim
  432.     % set color of lines and text back to what they were
  433.     if ~isempty( lineTextColors )
  434.         blt( window, get(window,'color'), lineTextColors);
  435.     end
  436.  
  437.     % Invert back the W&B color properties of Figure and child objects
  438.     if invertedColor
  439.         cinvert( window );
  440.     end
  441.  
  442.     % Reconstruct figure the way it was.
  443.     if hasPreview 
  444.  
  445.         % Reconstruct figure the way it was.
  446.         if w ~= fullW | h ~= fullH
  447.             set( window, 'Position', frect )
  448.         end
  449.         set( window, 'units', origUnits );
  450.  
  451.         if w ~= fullW | h ~= fullH | invertedColor | ~isempty(lineTextColors)
  452.             drawnow;
  453.         end
  454.  
  455.     elseif ((invertedColor | ~isempty( lineTextColors )) & ~strcmp(dev, '-dbitmap'))
  456.         %
  457.         % Discard all the object invalidations that occured as a result of
  458.         % changing colors. All objects are back to their previous state,
  459.         % but they don't know that.
  460.         %
  461.         drawnow('discard')
  462.     end % hasPreview
  463.  
  464. else
  465.     if ( openedSimWindow )
  466.         eval('close_system( window );');
  467.     end
  468. end % ~prtSim
  469.  
  470. if ( devClass == 'GS' )
  471.     rsp_file = [tempname '.rsp'];
  472.     rsp_fid = fopen (rsp_file, 'w');
  473.     if (rsp_fid < 0)
  474.         error('Unable to create response file')
  475.     end
  476.     fprintf(rsp_fid, '-dNOPAUSE -q \n');
  477.     fprintf(rsp_fid, '-I%s/ghostscript/ps_files\n', matlabroot);
  478.     fprintf(rsp_fid, '-I%s/ghostscript/fonts\n', matlabroot);
  479.     fprintf(rsp_fid, '-sDEVICE=%s\n', ghostScriptDevice);
  480.     fprintf(rsp_fid, '-sOutputFile=%s\n', orig_filename );
  481.  
  482.     fclose(rsp_fid);
  483.     if (comp(1:2) =='PC')
  484.         dos( [ matlabroot '\ghostscr\bin\gs386 @' rsp_file ' ' filename ' < NUL |' ] );
  485.     else    
  486.         status = unix( [ matlabroot '/ghostscript/bin/' getenv('ARCH') '/gs @' rsp_file ' '...
  487.              filename ' < /dev/null > /dev/null' ] );
  488.         if (status)
  489.             error('GhostScript returned nonzero error status')
  490.         end
  491.     end
  492.  
  493.     delete(rsp_file)
  494.     delete(filename)
  495.     filename = orig_filename;
  496. end
  497.  
  498. if (printplot)
  499.     if (comp(1:2) == 'PC')
  500.         if (strcmp(lprcmd(1:4), 'COPY'))
  501.             cmd = sprintf(lprcmd, filename);
  502.             dos(cmd);
  503.         else
  504.             dos([lprcmd ' ' filename]);
  505.         end
  506.     else
  507.         unix([lprcmd ' ' filename]);
  508.     end
  509. end
  510.  
  511.  
  512.