1 /* Copyright (C) 1998 artofcode LLC.  All rights reserved.
2 
3   This program is free software; you can redistribute it and/or modify it
4   under the terms of the GNU General Public License as published by the
5   Free Software Foundation; either version 2 of the License, or (at your
6   option) any later version.
7 
8   This program is distributed in the hope that it will be useful, but
9   WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   General Public License for more details.
12 
13   You should have received a copy of the GNU General Public License along
14   with this program; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA, 02111-1307.
16 
17 */
18 
19 /*$Id: gdevbjcl.c,v 1.2.6.1.2.1 2003/01/17 00:49:00 giles Exp $*/
20 /* Canon BJC command generation library */
21 #include "std.h"
22 #include "gdevbjcl.h"
23 
24 /****** PRELIMINARY, SUBJECT TO CHANGE WITHOUT NOTICE. ******/
25 
26 /* ---------------- Utilities ---------------- */
27 
28 private void
bjc_put_bytes(stream * s,const byte * data,uint count)29 bjc_put_bytes(stream *s, const byte *data, uint count)
30 {
31     uint ignore;
32 
33     sputs(s, data, count, &ignore);
34 }
35 
36 private void
bjc_put_hi_lo(stream * s,int value)37 bjc_put_hi_lo(stream *s, int value)
38 {
39     spputc(s, value >> 8);
40     spputc(s, value & 0xff);
41 }
42 
43 private void
bjc_put_lo_hi(stream * s,int value)44 bjc_put_lo_hi(stream *s, int value)
45 {
46     spputc(s, value & 0xff);
47     spputc(s, value >> 8);
48 }
49 
50 private void
bjc_put_command(stream * s,int ch,int count)51 bjc_put_command(stream *s, int ch, int count)
52 {
53     spputc(s, 033 /*ESC*/);
54     spputc(s, '(');
55     spputc(s, ch);
56     bjc_put_lo_hi(s, count);
57 }
58 
59 /* ---------------- Commands ---------------- */
60 
61 /* Line feed (^J) */
62 void
bjc_put_LF(stream * s)63 bjc_put_LF(stream *s)
64 {
65     spputc(s, 0x0a);
66 }
67 
68 /* Form feed (^L) */
69 void
bjc_put_FF(stream * s)70 bjc_put_FF(stream *s)
71 {
72     spputc(s, 0x0c);
73 }
74 
75 /* Carriage return (^M) */
76 void
bjc_put_CR(stream * s)77 bjc_put_CR(stream *s)
78 {
79     spputc(s, 0x0d);
80 }
81 
82 /* Return to initial condition (ESC @) */
83 void
bjc_put_initialize(stream * s)84 bjc_put_initialize(stream *s)
85 {
86     bjc_put_bytes(s, (const byte *)"\033@", 2);
87 }
88 
89 /* Set initial condition (ESC [ K <count> <init> <id> <parm1> <parm2>) */
90 void
bjc_put_set_initial(stream * s)91 bjc_put_set_initial(stream *s)
92 {
93     bjc_put_bytes(s, (const byte *)"\033[K\002\000\000\017", 7);
94 }
95 
96 /* Set data compression (ESC [ b <count> <state>) */
97 void
bjc_put_set_compression(stream * s,bjc_raster_compression_t compression)98 bjc_put_set_compression(stream *s, bjc_raster_compression_t compression)
99 {
100     bjc_put_command(s, 'b', 1);
101     spputc(s, compression);
102 }
103 
104 /* Select print method (ESC ( c <count> <parm1> <parm2> [<parm3>]) */
105 void
bjc_put_print_method_short(stream * s,bjc_print_color_short_t color)106 bjc_put_print_method_short(stream *s, bjc_print_color_short_t color)
107 {
108     bjc_put_command(s, 'c', 1);
109     spputc(s, color);
110 }
111 void
bjc_put_print_method(stream * s,bjc_print_color_t color,bjc_print_media_t media,bjc_print_quality_t quality,bjc_black_density_t density)112 bjc_put_print_method(stream *s, bjc_print_color_t color,
113 		     bjc_print_media_t media, bjc_print_quality_t quality,
114 		     bjc_black_density_t density)
115 {
116     bjc_put_command(s, 'c', 2 + (density != 0));
117     spputc(s, 0x10 | color);
118     spputc(s, (media << 4) | quality);
119     if (density)
120 	spputc(s, density << 4);
121 }
122 
123 /* Set raster resolution (ESC ( d <count> <y_res> [<x_res>]) */
124 void
bjc_put_raster_resolution(stream * s,int x_resolution,int y_resolution)125 bjc_put_raster_resolution(stream *s, int x_resolution, int y_resolution)
126 {
127     if (x_resolution == y_resolution) {
128 	bjc_put_command(s, 'd', 2);
129     } else {
130 	bjc_put_command(s, 'd', 4);
131 	bjc_put_hi_lo(s, y_resolution);
132     }
133     bjc_put_hi_lo(s, x_resolution);
134 }
135 
136 /* Raster skip (ESC ( e <count> <skip>) */
137 void
bjc_put_raster_skip(stream * s,int skip)138 bjc_put_raster_skip(stream *s, int skip)
139 {
140     bjc_put_command(s, 'e', 2);
141     bjc_put_hi_lo(s, skip);
142 }
143 
144 /* Set page margins (ESC ( g <count> <length> <lm> <rm> <top>) */
145 void
bjc_put_page_margins(stream * s,int length,int lm,int rm,int top)146 bjc_put_page_margins(stream *s, int length, int lm, int rm, int top)
147 {
148     byte parms[4];
149     int count;
150 
151     parms[0] = length, parms[1] = lm, parms[2] = rm, parms[3] = top;
152     count = 4;		/* could be 1..3 */
153     bjc_put_command(s, 'g', count);
154     bjc_put_bytes(s, parms, count);
155 }
156 
157 /* Set media supply method (ESC * l <count> <parm1> <parm2>) */
158 void
bjc_put_media_supply(stream * s,bjc_media_supply_t supply,bjc_media_type_t type)159 bjc_put_media_supply(stream *s, bjc_media_supply_t supply,
160 		     bjc_media_type_t type)
161 {
162     bjc_put_command(s, 'l', 2);
163     spputc(s, 0x10 | supply);
164     spputc(s, type << 4);
165 }
166 
167 /* Identify ink cartridge (ESC ( m <count> <type>) */
168 void
bjc_put_identify_cartridge(stream * s,bjc_identify_cartridge_command_t command)169 bjc_put_identify_cartridge(stream *s,
170 			   bjc_identify_cartridge_command_t command)
171 {
172     bjc_put_command(s, 'm', 1);
173     spputc(s, command);
174 }
175 
176 /* CMYK raster image (ESC ( A <count> <color>) */
177 void
bjc_put_cmyk_image(stream * s,bjc_cmyk_image_component_t component,const byte * data,int count)178 bjc_put_cmyk_image(stream *s, bjc_cmyk_image_component_t component,
179 		   const byte *data, int count)
180 {
181     bjc_put_command(s, 'A', count + 1);
182     spputc(s, component);
183     bjc_put_bytes(s, data, count);
184 }
185 
186 /* Move by raster lines (ESC ( n <count> <lines>) */
187 void
bjc_put_move_lines(stream * s,int lines)188 bjc_put_move_lines(stream *s, int lines)
189 {
190     bjc_put_command(s, 'n', 2);
191     bjc_put_hi_lo(s, lines);
192 }
193 
194 /* Set unit for movement by raster lines (ESC ( o <count> <unit>) */
195 void
bjc_put_move_lines_unit(stream * s,int unit)196 bjc_put_move_lines_unit(stream *s, int unit)
197 {
198     bjc_put_command(s, 'o', 2);
199     bjc_put_hi_lo(s, unit);
200 }
201 
202 /* Set extended margins (ESC ( p <count> <length60ths> <lm60ths> */
203 /*   <rm60ths> <top60ths>) */
204 void
bjc_put_extended_margins(stream * s,int length,int lm,int rm,int top)205 bjc_put_extended_margins(stream *s, int length, int lm, int rm, int top)
206 {
207     bjc_put_command(s, 'p', 8);
208     bjc_put_hi_lo(s, length);
209     bjc_put_hi_lo(s, lm);
210     bjc_put_hi_lo(s, rm);
211     bjc_put_hi_lo(s, top);
212 }
213 
214 /* Set image format (ESC ( t <count> <depth> <format> <ink>) */
215 void
bjc_put_image_format(stream * s,int depth,bjc_image_format_t format,bjc_ink_system_t ink)216 bjc_put_image_format(stream *s, int depth, bjc_image_format_t format,
217 			     bjc_ink_system_t ink)
218 
219 {
220     bjc_put_command(s, 't', 3);
221     spputc(s, depth);
222     spputc(s, format);
223     spputc(s, ink);
224 }
225 
226 /* Page ID (ESC ( q <count> <id>) */
227 void
bjc_put_page_id(stream * s,int id)228 bjc_put_page_id(stream *s, int id)
229 {
230     bjc_put_command(s, 'q', 1);
231     spputc(s, id);
232 }
233 
234 /* Continue raster image (ESC ( F <count> <data>) */
235 void
bjc_put_continue_image(stream * s,const byte * data,int count)236 bjc_put_continue_image(stream *s, const byte *data, int count)
237 {
238     bjc_put_command(s, 'F', count);
239     bjc_put_bytes(s, data, count);
240 }
241 
242 /* BJ indexed image (ESC ( f <count> R <dot_rows> <dot_cols> <layers> */
243 /*   <index>) */
244 void
bjc_put_indexed_image(stream * s,int dot_rows,int dot_cols,int layers)245 bjc_put_indexed_image(stream *s, int dot_rows, int dot_cols, int layers)
246 {
247     bjc_put_command(s, 'f', 5);
248     spputc(s, 'R');			/* per spec */
249     spputc(s, dot_rows);
250     spputc(s, dot_cols);
251     spputc(s, layers);
252 }
253