xref: /titanic_44/usr/src/cmd/lp/lib/lp/set_pitch.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.9	*/
27 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
28 
29 #include "string.h"
30 #include "stdlib.h"
31 
32 #include "lp.h"
33 #include "lp.set.h"
34 
35 extern char		*tparm();
36 
37 short			output_res_char		= -1,
38 			output_res_line		= -1,
39 			output_res_horz_inch	= -1,
40 			output_res_vert_inch	= -1;
41 
42 /**
43  ** set_pitch()
44  **/
45 
46 int
47 #if	defined(__STDC__)
set_pitch(char * str,int which,int putout)48 set_pitch (
49 	char *			str,
50 	int			which,
51 	int			putout
52 )
53 #else
54 set_pitch (str, which, putout)
55 	char			*str;
56 	int			which,
57 				putout;
58 #endif
59 {
60 	double			xpi;
61 
62 	int			ixpi;
63 
64 	short			*output_res_p,
65 				*output_res_inch_p;
66 
67 	unsigned short		xpi_changes_res;
68 
69 	char			*rest,
70 				*change_pitch,
71 				*change_res,
72 				*p;
73 
74 
75 	if (which == 'H') {
76 
77 		tidbit ((char *)0, "cpi", &change_pitch);
78 		tidbit ((char *)0, "chr", &change_res);
79 
80 		output_res_inch_p = &output_res_horz_inch;
81 		if (output_res_horz_inch == -1)
82 			tidbit ((char *)0, "orhi", output_res_inch_p);
83 
84 		output_res_p = &output_res_char;
85 		if (output_res_char == -1)
86 			tidbit ((char *)0, "orc", output_res_p);
87 
88 		tidbit ((char *)0, "cpix", &xpi_changes_res);
89 
90 	} else {
91 
92 		tidbit ((char *)0, "lpi", &change_pitch);;
93 		tidbit ((char *)0, "cvr", &change_res);;
94 
95 		output_res_inch_p = &output_res_vert_inch;
96 		if (output_res_vert_inch == -1)
97 			tidbit ((char *)0, "orvi", output_res_inch_p);
98 
99 		output_res_p = &output_res_line;
100 		if (output_res_line == -1)
101 			tidbit ((char *)0, "orl", output_res_p);
102 
103 		tidbit ((char *)0, "lpix", &xpi_changes_res);;
104 
105 	}
106 
107 	xpi = strtod(str, &rest);
108 	if (which == 'H' && STREQU(str, NAME_PICA))
109 		ixpi = R(xpi = 10);
110 
111 	else if (which == 'H' && STREQU(str, NAME_ELITE))
112 		ixpi = R(xpi = 12);
113 
114 	else if (
115 		which == 'H'
116 	     && (
117 			STREQU(str, NAME_COMPRESSED)
118 		     || xpi >= N_COMPRESSED
119 		)
120 	) {
121 		if (change_pitch) {
122 
123 			for (ixpi = MAX_COMPRESSED; ixpi; ixpi--)
124 				if ((p = tparm(change_pitch, ixpi)) && *p)
125 					break;
126 			if (!ixpi)
127 				ixpi = 10;
128 			xpi = (double)ixpi;
129 
130 		} else if (change_res && *output_res_inch_p != -1) {
131 
132 			for (xpi = MAX_COMPRESSED; xpi >= 1.; xpi -= 1.)
133 				if (
134 			(p = tparm(change_res, R(*output_res_inch_p / xpi)))
135 				     && *p
136 				)
137 					break;
138 			if (xpi < 1.)
139 				xpi = 10.;
140 			ixpi = R(xpi);
141 
142 		} else
143 			return (E_FAILURE);
144 
145 	} else {
146 
147 		if (xpi <= 0)
148 			return (E_BAD_ARGS);
149 
150 		switch (*rest) {
151 		case ' ':
152 		case 0:
153 			break;
154 		case 'c':
155 			/*
156 			 * Convert to [lines|chars] per inch.
157 			 */
158 			xpi *= 2.54;
159 			/* fall through */
160 		case 'i':
161 			break;
162 		default:
163 			return (E_BAD_ARGS);
164 		}
165 
166 		ixpi = R(xpi);
167 
168 	}
169 
170 	if (
171 		*output_res_inch_p != -1
172 	     && *output_res_p != -1
173 	     && R(*output_res_inch_p / (double)*output_res_p) == ixpi
174 	)
175 		return (E_SUCCESS);
176 
177 	else if (
178 		change_pitch
179 	     && (p = tparm(change_pitch, ixpi))
180 	     && *p
181 	) {
182 
183 		if (putout)
184 			putp (p);
185 		if (xpi_changes_res) {
186 			if (*output_res_inch_p != -1)
187 				*output_res_inch_p = R(*output_res_p * xpi);
188 		} else {
189 			if (*output_res_p != -1)
190 				*output_res_p = R(*output_res_inch_p / xpi);
191 		}
192 		return (E_SUCCESS);
193 
194 	} else if (
195 		change_res
196 	     && *output_res_inch_p != -1
197 	     && (p = tparm(change_res, R(*output_res_inch_p / xpi)))
198 	     && *p
199 	) {
200 
201 		if (putout)
202 			putp (p);
203 		if (*output_res_p != -1)
204 			*output_res_p = R(*output_res_inch_p / xpi);
205 		return (E_SUCCESS);
206 
207 	} else
208 
209 		return (E_FAILURE);
210 }
211