xref: /illumos-gate/usr/src/cmd/lp/filter/postscript/dpost/color.c (revision 90221f9148b67fdc90178b67f9600b7bd4e3bc7c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  *
34  * Routines that handle color requests passed through as device control commands
35  * in the form "x X SetColor:red". The following PostScript procedures are needed:
36  *
37  *	setcolor
38  *
39  *	  mark /color setcolor mark
40  *	  mark /color1 /color2 setcolor mark
41  *
42  *	    Called whenever we want to change PostScript's current color graphics
43  *	    state parameter. One or two color arguments can be given. In each case
44  *	    the colors are looked up in the PostScript colordict dictionary that's
45  *	    defined in *colorfile. Two named colors implies reverse video printing
46  *	    with the background given in /color2 and the text printed in /color1.
47  *	    Unknown colors are mapped into defaults - black for a single color and
48  *	    white on black for reverse video.
49  *
50  *	drawrvbox
51  *
52  *	  leftx rightx drawrvbox -
53  *
54  *	    Fills a box that extends from leftx to rightx with the background color
55  *	    that was requested when setcolor set things up for reverse video mode.
56  *	    The vertical extent of the box is determined using FontBBox just before
57  *	    the first string is printed, and the height remains in effect until
58  *	    there's an explicit color change. In otherwords font or size changes
59  *	    won't always produce correct result in reverse video mode.
60  *
61  *	setdecoding
62  *
63  *	  num setdecoding -
64  *
65  *	    Selects the text decoding procedure (ie. what's assigned to PostScript
66  *	    procedure t) from the decodingdefs array defined in the prologue. num
67  *	    should be the value assigned to variable encoding (in dpost) and will
68  *	    remain constant throughout a job, unless special features, like reverse
69  *	    video printing, are requested. The text encoding scheme can be set on
70  *	    the command line using the -e option. Print time and the size of the
71  *	    output file will usually decrease as the value assigned to encoding
72  *	    increases.
73  *
74  *
75  * The recognized collection of "x X SetColor:" commands are:
76  *
77  *	x X SetColor:				selects black
78  *	x X SetColor:color			selects color
79  *	x X SetColor:color1 on color2		reverse video
80  *	x X SetColor:color1 color2		reverse video again
81  *	x X SetColor:num1 num2 num3 rgb		explicit rgb color request
82  *	x X SetColor:num1 num2 num3 hsb		explicit hsb color request
83  *
84  * In the last three examples num1, num2, and num3 should be numbers between 0 and
85  * 1 inclusive and are passed on as aguments to the approrpriate PostScript color
86  * command (eg. setrgbcolor). Unknown color names (ie. the ones that setcolor
87  * doesn't find in colordict) are mapped into defaults. For one color the default
88  * is black, while for reverse video it's white text on a black background.
89  *
90  * dpost makes sure the current color is maintained across page boundaries, which
91  * may not be what you want if you're using a macro package like mm that puts out
92  * page footers and headers. Adding a color request to troff and keeping track of
93  * the color in each environment may be the best solution.
94  *
95  * To get reverse video printing follow the "x X SetColor:" command with two or
96  * three arguments. "x X SetColor:white on black" or "x X SetColor:white black"
97  * both produce white text on a black background. Any two colors named in colordict
98  * (in file *colorfile) can be chosen so "x X SetColor:yellow on blue" also works.
99  * Each reverse video mode request selects the vertical extent of the background
100  * box based on the font and size in use just before the first string is printed.
101  * Font and/or size changes aren't guaranteed to work properly in reverse video
102  * printing.
103  *
104  */
105 
106 
107 #include <stdio.h>
108 #include <ctype.h>
109 
110 #include "gen.h"			/* general purpose definitions */
111 #include "ext.h"			/* external variable definitions */
112 
113 
114 #define DEFAULTCOLOR	"black"
115 
116 char	color[50] = DEFAULTCOLOR;	/* current color */
117 int	gotcolor = FALSE;		/* TRUE after *colorfile is downloaded */
118 int	wantcolor = FALSE;		/* TRUE if we really ask for a color */
119 
120 
121 /*
122  *
123  * All these should be defined in dpost.c.
124  *
125  */
126 
127 
128 extern int	lastend;
129 extern int	encoding;
130 extern int	maxencoding;
131 extern int	realencoding;
132 
133 extern char	*colorfile;
134 extern FILE	*tf;
135 
136 
137 /*****************************************************************************/
138 
139 
140 void
141 getcolor(void)
142 {
143 
144 /*
145  *
146  * Responsible for making sure the PostScript color procedures are downloaded from
147  * *colorfile. Done at most once per job, and only if the job really uses color.
148  * For now I've decided not to quit if we can't read the color file.
149  *
150  */
151 
152 
153     if ( gotcolor == FALSE && access(colorfile, 04) == 0 )
154 	doglobal(colorfile);
155 
156     if ( tf == stdout )
157 	gotcolor = TRUE;
158 
159 }   /* End of getcolor */
160 
161 
162 /*****************************************************************************/
163 
164 
165 void
166 newcolor(char *name)
167     /* of the color */
168 {
169     char	*p;			/* next character in *name */
170     int		i;			/* goes in color[i] */
171 
172 /*
173  *
174  * Converts *name to lower case and saves the result in color[] for use as the
175  * current color. The first time something other than DEFAULTCOLOR is requested
176  * sets wantcolor to TRUE. Characters are converted to lower case as they're put
177  * in color[] and we quit when we find a newline or get to the end of *name. The
178  * isupper() test is for Berkley systems.
179  *
180  */
181 
182 
183     for ( p = name; *p && (*p == ' ' || *p == ':'); p++ ) ;
184 
185     for ( i = 0; i < sizeof(color) - 1 && *p != '\n' && *p; i++, p++ )
186 	if ( isupper(*p) )
187 	    color[i] = tolower(*p);
188 	else color[i] = *p;
189 
190     if ( i == 0 )
191 	strcpy(color, DEFAULTCOLOR);
192     else color[i] = '\0';
193 
194     if ( strcmp(color, DEFAULTCOLOR) != 0 )
195 	wantcolor = TRUE;
196 
197 }   /* End of newcolor */
198 
199 
200 /*****************************************************************************/
201 
202 
203 void
204 setcolor(void)
205 {
206     int		newencoding;		/* text encoding scheme that's needed */
207     char	*p;			/* for converting what's in color[] */
208 
209 /*
210  *
211  * Sets the color being used by the printer to whatever's stored as the current
212  * color (ie. the string in color[]). wantcolor is only set to TRUE if we've been
213  * through newcolor() and asked for something other than DEFAULTCOLOR (probably
214  * black). While in reverse video mode encoding gets set to maxencoding + 1 in
215  * dpost and 0 on the printer. Didn't see much point in trying to extend reverse
216  * video to all the different encoding schemes. realencoding is restored when we
217  * leave reverse video mode.
218  *
219  */
220 
221 
222     if ( wantcolor == TRUE )  {
223 	endtext();
224 	getcolor();
225 
226 	lastend = -1;
227 	newencoding = realencoding;
228 
229 	if ( islower(color[0]) == 0 )		/* explicit rgb or hsb request */
230 	    fprintf(tf, "%s\n", color);
231 	else {
232 	    putc('/', tf);
233 	    for ( p = color; *p && *p != ' '; p++ )
234 		putc(*p, tf);
235 	    for ( ; *p && *p == ' '; p++ ) ;
236 	    if ( strncmp(p, "on ", 3) == 0 ) p += 3;
237 	    if ( *p != '\0' )  {
238 		fprintf(tf, " /%s", p);
239 		newencoding = maxencoding + 1;
240 	    }	/* End if */
241 	    fprintf(tf, " setcolor\n");
242 	}   /* End else */
243 
244 	if ( newencoding != encoding )  {
245 	    encoding = newencoding;
246 	    fprintf(tf, "%d setdecoding\n", encoding);
247 	    resetpos();
248 	}   /* End if */
249     }	/* End if */
250 
251 }   /* End of setcolor */
252