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