[comp.text.tex] colored postscript via dvips

aslam@sp1.csrd.uiuc.edu (Sohail Aslam) (11/21/90)

Does someone have a TeX macro(s) that can set text (word, line or paragraph)
in color when dvi file is printed as postscript? The macro will send 
postscript code via \special that will change the color to the specified
rgb values, e.g.,

	\setincolor{.1,.25,.35}{this line is colored}
	\setincolor{0,0,0}{Back in black}

We have QMS PS printer; I'd like to use it to produce colored text for
slides etc. I use Rokiki's excellent dvips.

Advance thanks.

Sohail Aslam
Center for Supercomputing Research and Development
University of Illinois
aslam@csrd.uiuc.edu

--
Sohail Aslam
Center for Supercomputing Research and Development
University of Illinois
aslam@csrd.uiuc.edu

sheffler@colt.CS.CMU.EDU (Thomas Sheffler) (11/22/90)

In response to Sohail Aslam's request:
----------------------------------------------------------------


I developed a set of LaTeX macros for generating colored frames around
boxes.  The toughest part of the process is taking a TeX box and
figuring out where it will be placed on the page.  Then, in
PostScript, setting the current path to this outline.  

These macros allow you to set a TeX box in a specified FOREGROUND
color, with a specified BACKGROUND color and an optional OUTLINE
color.  These are by no means complete, but are a place to start for
your application.

The \specials{} used are for DVI3PS and involve two forms:
	pstext='string'		insert the string at the current
				position in a save (save/restore)
				environment.
	psraw='string'		Just insert the string with no prolog/
				postlog.

----------------------------------------------------------------
%
% psframe.sty 
%
% These macros use PostScript commands to change the color of the text
% and its background.  The simplest form of usage is the PSFRAME
% environment.  It sets the text of its environment in a box whose
% background color foreground color and outline color are given by the
% commands \psframebackground, \psframeforeground and \psframeoutline.
% See the example at the end of the file.
%
% The environment form is built from the more primitive command
% PSPATH.
%
% CAVEAT: PSFRAME environment does not nest properly.
%
% WARNING! do not put too much PostScript code in the arguments.  Many
% 	dvi->ps converters don't allow any more than 256 bytes in
% 	\specials. 
%
% Tom Sheffler, CMU
% May 1990

\catcode`\@=11\relax

%%% SOME SIMPLE COLORS - actual PostScript code
%%%
\def\black-color{0 setgray}
\def\white-color{1 setgray}
\def\light-gray-color{0.75 setgray}
\def\med-gray-color{0.5 setgray}
\def\dark-gray-color{0.25 setgray}

\def\red-color{1 0 0 setrgbcolor}
\def\green-color{0 1 0 setrgbcolor}
\def\blue-color{0 0 1 setrgbcolor}


%%
%% PSPATH
%%
%% \pspath{preamble PostScript}{postamble PostScript}{TeX stuff}
%%
%% The bounding box of the TeX stuff is set as the CURRENT-PATH and
%% the preamble PostScript is executed.  Next, the TeX box is typeset.
%% Finally, the postamble PostScript is executed with the bounding box
%% of the TeX box restored as the CURRENT path.  Many interesting
%% effects are possible.
%%
%% EXAMPLE: set some text on a gray background, Use the SPBOX macro to
%% give some space around the text.
%%
%% 	\pspath{0.5 setgray fill}{}{\spbox{Some Text}}
%%

\long\def\pspath#1#2#3{%
\begingroup\setbox0=\hbox{#3}%
\dimen0=\ht0 \advance\dimen0 by \dp0
    % Write out the PS code to set the current path using HEIGHT,
    % WIDTH , DEPTH of box0.
    \hbox{%
    \special{pstext='/@s@ {65536 div} def 	% scale factor function
	newpath 0 0 moveto			% goto ORIGIN
	0 \number\ht0 \space @s@ rlineto 	% move UP
	\number\wd0 \space @s@ 0 rlineto	% move RIGHT
	0 \number\dimen0 \space neg \space @s@ rlineto % DOWN
	\number\wd0 \space @s@ neg 0 rlineto 	% move LEFT
	closepath #1'}%
    \copy0\kern-\wd0
%% Draw the box again
    \special{pstext='/@s@ {65536 div} def 	% scale factor function
	newpath 0 0 moveto			% goto ORIGIN
	0 \number\ht0 \space @s@ rlineto 	% move UP
	\number\wd0 \space @s@ 0 rlineto	% move RIGHT
	0 \number\dimen0 \space neg \space @s@ rlineto % DOWN
	\number\wd0 \space @s@ neg 0 rlineto 	% move LEFT
	closepath #2'}%
    \kern\wd0
} %HBOX
\endgroup
}


% SPACEBOX
%
% This macro simply takes some TeX stuff, and puts FOUR sides on it
% so that the box is the same size as the thing you'd get with
% an \fbox{} command.  (All I did was modify the code for \fbox{}
% so that all rules were replaced with struts).  
%
% USAGE: \spbox{text} is just like \fbox{text} but makes no rules
%
% REASON: so that if using \pspath{...}{\fbox{stuff}} 
%         there is a way to get another box the same size:
%	  \pspath{...}{\spbox{stuff}}
%
\long\def\spbox#1{\leavevmode\setbox1\hbox{#1}
    \dimen0\fboxrule \advance\dimen0 \fboxsep 
    \advance\dimen0 \dp1
    \hbox{\lower \dimen0\hbox
    {\vbox{\hrule height \fboxrule width 0pt
          \hbox{\vrule width \fboxrule height 0pt \hskip\fboxsep
          \vbox{\vskip\fboxsep \box1\vskip\fboxsep}\hskip 
                 \fboxsep\vrule width \fboxrule height 0pt}
                 \hrule height \fboxrule width 0pt}}}}



%% COMMANDS FOR SETTING THE COLORS FOR PSFRAME
\def\psframeforeground#1{\gdef\@psframeforeground{#1}}
\def\psframebackground#1{\gdef\@psframebackground{#1}}
\def\psframeoutline#1{\gdef\@psframeoutline{#1}}

%% Default colors
\psframeforeground{\white-color}
\psframebackground{\med-gray-color}
\psframeoutline{\black-color}

%% The sequence of operations in 'endpsframe' is kinda complicated.
%% The 'psraw' phrase saves the current rgb color in an executable
%% array named '@@c@@', this is done immediately before setting the
%% box.  PSPATH is called with PostScript that first draws the
%% background color and the frame.  The postlog argument simply resets
%% the foreground color.

\def\psframe{\begingroup\setbox1=\hbox\bgroup}

\long\def\endpsframe{\egroup
\pspath{gsave \@psframebackground \space %
    fill grestore \@psframeoutline \space  stroke}  %
    {}
    {\special{psraw='/@@c@@[currentrgbcolor]cvx def
     \@psframeforeground'}\box1\special{psraw='@@c@@ setrgbcolor'}}
\endgroup
}
    


% Restore '@'
\catcode`\@=12\relax

%%% EXAMPLE of PSFRAME:
%%%	Try running this through LaTeX and dvi3ps to view the result.
%%%	To make the box the same size as an \fbox{} use the \spbox{} command.

%	\psframebackground{\light-gray-color}
%	\psframeforeground{\red-color}
%	\psframeoutline{\blue-color}
%	
%	This line shows
%	\begin{psframe}
%		\parbox{2in}{
%		how the contents of a {\sc psframe} 
%		environment may be displayed in a
%		colorful and eye-catching}
%	\end{psframe}
%	manner.