Debian Codegraph @

src/main/escp2-driver.c

raw symbols SCIP
1 /*
2 *
3 * Print plug-in EPSON ESC/P2 driver for the GIMP.
4 *
5 * Copyright 1997-2000 Michael Sweet ([email protected]) and
6 * Robert Krawitz ([email protected])
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 /*
23 * This file must include only standard C header files. The core code must
24 * compile on generic platforms that don't support glib, gimp, gtk, etc.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <gutenprint/gutenprint.h>
31 #include <gutenprint/gutenprint-intl-internal.h>
32 #include "gutenprint-internal.h"
33 #include <string.h>
34 #include "print-escp2.h"
35
36 #ifdef __GNUC__
37 #define inline __inline__
38 #endif
39
40 static escp2_privdata_t *
41 get_privdata(stp_vars_t *v)
42 {
43 return (escp2_privdata_t *) stp_get_component_data(v, "Driver");
44 }
45
46 static void
47 escp2_reset_printer(stp_vars_t *v)
48 {
49 escp2_privdata_t *pd = get_privdata(v);
50 /*
51 * Magic initialization string that's needed to take printer out of
52 * packet mode.
53 */
54 if (pd->preinit_sequence)
55 stp_write_raw(pd->preinit_sequence, v);
56
57 stp_send_command(v, "\033@", "");
58 }
59
60 static void
61 print_remote_param(stp_vars_t *v, const char *param, const char *value)
62 {
63 stp_send_command(v, "\033(R", "bcscs", '\0', param, ':',
64 value ? value : "NULL");
65 stp_send_command(v, "\033", "ccc", 0, 0, 0);
66 }
67
68 static void
69 print_remote_int_param(stp_vars_t *v, const char *param, int value)
70 {
71 char buf[64];
72 (void) snprintf(buf, 64, "%d", value);
73 print_remote_param(v, param, buf);
74 }
75
76 static void
77 print_remote_float_param(stp_vars_t *v, const char *param, double value)
78 {
79 char buf[64];
80 (void) snprintf(buf, 64, "%f", value);
81 print_remote_param(v, param, buf);
82 }
83
84 static void
85 print_remote_dim_param(stp_vars_t *v, const char *param, double value)
86 {
87 print_remote_float_param(v, param, value);
88 }
89
90 static void
91 print_debug_params(stp_vars_t *v)
92 {
93 escp2_privdata_t *pd = get_privdata(v);
94 stp_parameter_list_t params = stp_get_parameter_list(v);
95 int count = stp_parameter_list_count(params);
96 int i;
97 print_remote_param(v, "Package", PACKAGE);
98 print_remote_param(v, "Version", VERSION);
99 print_remote_param(v, "Release Date", RELEASE_DATE);
100 print_remote_param(v, "Driver", stp_get_driver(v));
101 print_remote_dim_param(v, "Left", stp_get_left(v));
102 print_remote_dim_param(v, "Top", stp_get_top(v));
103 print_remote_dim_param(v, "Page Width", stp_get_page_width(v));
104 print_remote_dim_param(v, "Page Height", stp_get_page_height(v));
105 print_remote_int_param(v, "Model", stp_get_model_id(v));
106 print_remote_int_param(v, "Ydpi", pd->res->vres);
107 print_remote_int_param(v, "Xdpi", pd->res->hres);
108 print_remote_int_param(v, "Printed_ydpi", pd->res->printed_vres);
109 print_remote_int_param(v, "Printed_xdpi", pd->res->printed_hres);
110 /*
111 print_remote_int_param(v, "Use_softweave", pd->res->softweave);
112 print_remote_int_param(v, "Printer_weave", pd->res->printer_weave);
113 */
114 print_remote_int_param(v, "Use_printer_weave", pd->use_printer_weave);
115 print_remote_int_param(v, "Duplex", pd->duplex);
116 print_remote_dim_param(v, "Page_left", pd->page_left);
117 print_remote_dim_param(v, "Page_right", pd->page_right);
118 print_remote_dim_param(v, "Page_top", pd->page_top);
119 print_remote_dim_param(v, "Page_bottom", pd->page_bottom);
120 print_remote_dim_param(v, "Page_width", pd->page_width);
121 print_remote_dim_param(v, "Page_height", pd->page_height);
122 print_remote_dim_param(v, "Page_true_height", pd->page_true_height);
123 print_remote_dim_param(v, "Page_extra_height", pd->page_extra_height);
124 print_remote_dim_param(v, "Paper_extra_bottom", pd->paper_extra_bottom);
125 print_remote_dim_param(v, "Image_left", pd->image_left);
126 print_remote_dim_param(v, "Image_top", pd->image_top);
127 print_remote_dim_param(v, "Image_width", pd->image_width);
128 print_remote_dim_param(v, "Image_height", pd->image_height);
129 print_remote_dim_param(v, "CD_X_offset", pd->cd_x_offset);
130 print_remote_dim_param(v, "CD_Y_offset", pd->cd_y_offset);
131 print_remote_dim_param(v, "CD_inner_radius", pd->cd_inner_radius);
132 print_remote_dim_param(v, "CD_outer_radius", pd->cd_outer_radius);
133 print_remote_int_param(v, "Image_scaled_width", pd->image_scaled_width);
134 print_remote_int_param(v, "Image_scaled_height", pd->image_scaled_height);
135 print_remote_int_param(v, "Image_printed_width", pd->image_printed_width);
136 print_remote_int_param(v, "Image_printed_height", pd->image_printed_height);
137 print_remote_int_param(v, "Image_left_position", pd->image_left_position);
138 print_remote_int_param(v, "Nozzles", pd->nozzles);
139 print_remote_int_param(v, "Nozzle_separation", pd->nozzle_separation);
140 print_remote_int_param(v, "Horizontal_passes", pd->horizontal_passes);
141 print_remote_int_param(v, "Vertical_passes", pd->res->vertical_passes);
142 print_remote_int_param(v, "Physical_xdpi", pd->physical_xdpi);
143 print_remote_int_param(v, "Page_management_units", pd->page_management_units);
144 print_remote_int_param(v, "Vertical_units", pd->vertical_units);
145 print_remote_int_param(v, "Horizontal_units", pd->horizontal_units);
146 print_remote_int_param(v, "Micro_units", pd->micro_units);
147 print_remote_int_param(v, "Unit_scale", pd->unit_scale);
148 print_remote_int_param(v, "Zero_advance", pd->send_zero_pass_advance);
149 print_remote_int_param(v, "Bits", pd->bitwidth);
150 print_remote_int_param(v, "Drop Size", pd->drop_size);
151 print_remote_int_param(v, "Initial_vertical_offset", pd->initial_vertical_offset);
152 print_remote_int_param(v, "Printing_initial_vertical_offset", pd->printing_initial_vertical_offset);
153 print_remote_int_param(v, "Channels_in_use", pd->channels_in_use);
154 print_remote_int_param(v, "Logical_channels", pd->logical_channels);
155 print_remote_int_param(v, "Physical_channels", pd->physical_channels);
156 print_remote_int_param(v, "Use_black_parameters", pd->use_black_parameters);
157 print_remote_int_param(v, "Use_fast_360", pd->use_fast_360);
158 print_remote_int_param(v, "Command_set", pd->command_set);
159 print_remote_int_param(v, "Variable_dots", pd->variable_dots);
160 print_remote_int_param(v, "Has_graymode", pd->has_graymode);
161 print_remote_int_param(v, "Base_separation", pd->base_separation);
162 print_remote_int_param(v, "Resolution_scale", pd->resolution_scale);
163 #if 0
164 print_remote_int_param(v, "Printing_resolution", pd->printing_resolution);
165 #endif
166 print_remote_int_param(v, "Separation_rows", pd->separation_rows);
167 print_remote_int_param(v, "Pseudo_separation_rows", pd->pseudo_separation_rows);
168 print_remote_int_param(v, "Extra_720dpi_separation", pd->extra_720dpi_separation);
169 print_remote_int_param(v, "Use_aux_channels", pd->use_aux_channels);
170 print_remote_param(v, "Ink name", pd->inkname->name);
171 print_remote_int_param(v, " channels", pd->inkname->channel_count);
172 print_remote_int_param(v, " inkset", pd->inkname->inkset);
173 for (i = 0; i < count; i++)
174 {
175 const stp_parameter_t *p = stp_parameter_list_param(params, i);
176 switch (p->p_type)
177 {
178 case STP_PARAMETER_TYPE_DOUBLE:
179 if (stp_check_float_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
180 print_remote_float_param(v, p->name,
181 stp_get_float_parameter(v, p->name));
182 break;
183 case STP_PARAMETER_TYPE_INT:
184 if (stp_check_int_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
185 print_remote_int_param(v, p->name,
186 stp_get_int_parameter(v, p->name));
187 break;
188 case STP_PARAMETER_TYPE_DIMENSION:
189 if (stp_check_dimension_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
190 print_remote_int_param(v, p->name,
191 stp_get_dimension_parameter(v, p->name));
192 break;
193 case STP_PARAMETER_TYPE_BOOLEAN:
194 if (stp_check_boolean_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
195 print_remote_int_param(v, p->name,
196 stp_get_boolean_parameter(v, p->name));
197 break;
198 case STP_PARAMETER_TYPE_STRING_LIST:
199 if (stp_check_string_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
200 print_remote_param(v, p->name,
201 stp_get_string_parameter(v, p->name));
202 break;
203 case STP_PARAMETER_TYPE_CURVE:
204 if (stp_check_curve_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
205 {
206 char *curve =
207 stp_curve_write_string(stp_get_curve_parameter(v, p->name));
208 print_remote_param(v, p->name, curve);
209 stp_free(curve);
210 }
211 break;
212 default:
213 break;
214 }
215 }
216 stp_parameter_list_destroy(params);
217 stp_send_command(v, "\033", "ccc", 0, 0, 0);
218 }
219
220 static void
221 escp2_set_remote_sequence(stp_vars_t *v)
222 {
223 /* Magic remote mode commands, whatever they do */
224 escp2_privdata_t *pd = get_privdata(v);
225 const stp_vars_t *pv = pd->media_settings;
226
227 if (stp_get_debug_level() & STP_DBG_MARK_FILE)
228 print_debug_params(v);
229 if (pd->advanced_command_set || pd->input_slot)
230 {
231 /* Enter remote mode */
232 stp_send_command(v, "\033(R", "bcs", 0, "REMOTE1");
233 /* Per the manual, job setup comes first, then SN command */
234 if (pd->input_slot &&
235 pd->input_slot->roll_feed_cut_flags == ROLL_FEED_CUT_ALL)
236 stp_send_command(v, "JS", "bh", 0);
237 if (pd->preinit_remote_sequence)
238 stp_write_raw(pd->preinit_remote_sequence, v);
239 if (stp_check_int_parameter(pv, "FeedAdjustment", STP_PARAMETER_ACTIVE))
240 stp_send_command(v, "SN", "bccc", 0, 4,
241 stp_get_int_parameter(pv, "FeedAdjustment"));
242 if (stp_check_int_parameter(pv, "VacuumIntensity", STP_PARAMETER_ACTIVE))
243 stp_send_command(v, "SN", "bccc", 0, 5,
244 stp_get_int_parameter(pv, "VacuumIntensity"));
245 if (stp_check_float_parameter(pv, "ScanDryTime", STP_PARAMETER_ACTIVE))
246 stp_send_command(v, "DR", "bcch", 0, 0,
247 (int) (stp_get_float_parameter(pv, "ScanDryTime") * 1000));
248 if (stp_check_float_parameter(pv, "ScanMinDryTime", STP_PARAMETER_ACTIVE))
249 stp_send_command(v, "DR", "bcch", 0, 0x40,
250 (int) (stp_get_float_parameter(pv, "ScanMinDryTime") * 1000));
251 if (stp_check_float_parameter(pv, "PageDryTime", STP_PARAMETER_ACTIVE))
252 stp_send_command(v, "DR", "bcch", 0, 1,
253 (int) stp_get_float_parameter(pv, "PageDryTime"));
254 if (stp_check_int_parameter(pv, "escp2_roll_lb", STP_PARAMETER_ACTIVE))
255 stp_send_command(v, "LB", "bccc", 0, 1,
256 stp_get_int_parameter(pv, "escp2_roll_lb"));
257 /* Next comes paper path */
258 if (pd->input_slot)
259 {
260 int divisor = pd->base_separation / 360;
261 int height = pd->page_true_height * 5 / divisor;
262 if (pd->input_slot->init_sequence)
263 stp_write_raw(pd->input_slot->init_sequence, v);
264 switch (pd->input_slot->roll_feed_cut_flags)
265 {
266 case ROLL_FEED_CUT_ALL:
267 stp_send_command(v, "CO", "bccccl", 0, 0, 1, 0, 0);
268 stp_send_command(v, "CO", "bccccl", 0, 0, 0, 0, height);
269 break;
270 case ROLL_FEED_CUT_LAST:
271 stp_send_command(v, "CO", "bccccl", 0, 0, 1, 0, 0);
272 stp_send_command(v, "CO", "bccccl", 0, 0, 2, 0, height);
273 break;
274 default:
275 break;
276 }
277 }
278 if (stp_check_int_parameter(pv, "PaperMedia", STP_PARAMETER_ACTIVE))
279 stp_send_command(v, "MI", "bcccc", 0, 1,
280 stp_get_int_parameter(pv, "PaperMedia"),
281 (stp_check_int_parameter(pv, "PaperMediaSize", STP_PARAMETER_ACTIVE) ?
282 stp_get_int_parameter(pv, "PaperMediaSize") :
283 99)); /* User-defined size (for now!) */
284 if (pd->duplex)
285 {
286 /* If there's ever duplex no tumble, we'll need to special
287 case it, too */
288 if (pd->duplex == DUPLEX_TUMBLE && pd->input_slot && (pd->input_slot->duplex & DUPLEX_TUMBLE))
289 stp_send_command(v, "DP", "bcc", 0, 2); /* Auto duplex */
290 else
291 stp_send_command(v, "DP", "bcc", 0, 2); /* Auto duplex */
292 }
293 if (stp_check_int_parameter(pv, "PaperThickness", STP_PARAMETER_ACTIVE))
294 stp_send_command(v, "PH", "bcc", 0,
295 stp_get_int_parameter(pv, "PaperThickness"));
296 if (stp_check_int_parameter(pv, "FeedSequence", STP_PARAMETER_ACTIVE))
297 stp_send_command(v, "SN", "bccc", 0, 0,
298 stp_get_int_parameter(pv, "FeedSequence"));
299 if (stp_check_int_parameter(pv, "PlatenGap", STP_PARAMETER_ACTIVE))
300 stp_send_command(v, "US", "bccc", 0, 1,
301 stp_get_int_parameter(pv, "PlatenGap"));
302 if (stp_get_boolean_parameter(v, "FullBleed"))
303 {
304 stp_send_command(v, "FP", "bch", 0,
305 (unsigned short) -pd->zero_margin_offset);
306 if (pd->borderless_sequence)
307 stp_write_raw(pd->borderless_sequence, v);
308 }
309 if (pd->inkname->init_sequence)
310 stp_write_raw(pd->inkname->init_sequence, v);
311 /* Exit remote mode */
312
313 stp_send_command(v, "\033", "ccc", 0, 0, 0);
314 }
315 }
316
317 static void
318 escp2_set_graphics_mode(stp_vars_t *v)
319 {
320 stp_send_command(v, "\033(G", "bc", 1);
321 }
322
323 static void
324 escp2_set_resolution(stp_vars_t *v)
325 {
326 escp2_privdata_t *pd = get_privdata(v);
327 if (pd->use_extended_commands)
328 stp_send_command(v, "\033(U", "bccch",
329 pd->unit_scale / pd->page_management_units,
330 pd->unit_scale / pd->vertical_units,
331 pd->unit_scale / pd->horizontal_units,
332 pd->unit_scale);
333 else
334 stp_send_command(v, "\033(U", "bc",
335 pd->unit_scale / pd->page_management_units);
336 }
337
338 static void
339 escp2_set_color(stp_vars_t *v)
340 {
341 escp2_privdata_t *pd = get_privdata(v);
342 if (pd->use_fast_360)
343 stp_send_command(v, "\033(K", "bcc", 0, 3);
344 else if (pd->has_graymode)
345 stp_send_command(v, "\033(K", "bcc", 0,
346 (pd->use_black_parameters ? 1 : 2));
347 }
348
349 static void
350 escp2_set_printer_weave(stp_vars_t *v)
351 {
352 escp2_privdata_t *pd = get_privdata(v);
353 if (pd->printer_weave)
354 stp_write_raw(pd->printer_weave, v);
355 else
356 stp_send_command(v, "\033(i", "bc", 0);
357 }
358
359 static void
360 escp2_set_printhead_speed(stp_vars_t *v)
361 {
362 escp2_privdata_t *pd = get_privdata(v);
363 const char *direction = stp_get_string_parameter(v, "PrintingDirection");
364 int unidirectional = -1;
365 if (direction && strcmp(direction, "Unidirectional") == 0)
366 unidirectional = 1;
367 else if (direction && strcmp(direction, "Bidirectional") == 0)
368 unidirectional = 0;
369 else if (pd->bidirectional_upper_limit >= 0 &&
370 pd->res->printed_hres * pd->res->printed_vres *
371 pd->res->vertical_passes >= pd->bidirectional_upper_limit)
372 {
373 stp_dprintf(STP_DBG_ESCP2, v,
374 "Setting unidirectional: hres %d vres %d passes %d total %d limit %d\n",
375 pd->res->printed_hres, pd->res->printed_vres,
376 pd->res->vertical_passes,
377 (pd->res->printed_hres * pd->res->printed_vres *
378 pd->res->vertical_passes),
379 pd->bidirectional_upper_limit);
380 unidirectional = 1;
381 }
382 else if (pd->bidirectional_upper_limit >= 0)
383 {
384 stp_dprintf(STP_DBG_ESCP2, v,
385 "Setting bidirectional: hres %d vres %d passes %d total %d limit %d\n",
386 pd->res->printed_hres, pd->res->printed_vres,
387 pd->res->vertical_passes,
388 (pd->res->printed_hres * pd->res->printed_vres *
389 pd->res->vertical_passes),
390 pd->bidirectional_upper_limit);
391 unidirectional = 0;
392 }
393 if (unidirectional == 1)
394 {
395 stp_send_command(v, "\033U", "c", 1);
396 if (pd->res->hres > pd->physical_xdpi)
397 stp_send_command(v, "\033(s", "bc", 2);
398 }
399 else if (unidirectional == 0)
400 stp_send_command(v, "\033U", "c", 0);
401 }
402
403 static void
404 escp2_set_dot_size(stp_vars_t *v)
405 {
406 escp2_privdata_t *pd = get_privdata(v);
407 /* Dot size */
408 if (pd->drop_size >= 0)
409 stp_send_command(v, "\033(e", "bcc", 0, pd->drop_size);
410 }
411
412 static void
413 escp2_set_cutter(stp_vars_t *v)
414 {
415 escp2_privdata_t *pd = get_privdata(v);
416
417 if (pd->roll_only)
418 {
419 int l = pd->page_true_height * 2880 / 72;
420 /* always 2880, independent of configured resolutions */
421
422 stp_send_command(v, "\033(g", "bl", l);
423 }
424 }
425
426 static void
427 escp2_set_page_height(stp_vars_t *v)
428 {
429 escp2_privdata_t *pd = get_privdata(v);
430 int l = (pd->page_true_height + pd->paper_extra_bottom) *
431 pd->page_management_units / 72;
432
433 if (pd->roll_only) return;
434
435 if (pd->use_extended_commands)
436 stp_send_command(v, "\033(C", "bl", l);
437 else
438 stp_send_command(v, "\033(C", "bh", l);
439 }
440
441 static void
442 escp2_set_margins(stp_vars_t *v)
443 {
444 escp2_privdata_t *pd = get_privdata(v);
445 int bot = pd->page_management_units * pd->page_bottom / 72;
446 int top = pd->page_management_units * pd->page_top / 72;
447
448 if (pd->roll_only) return;
449
450 top += pd->initial_vertical_offset;
451 top -= pd->page_extra_height;
452 bot += pd->page_extra_height;
453 if (pd->use_extended_commands &&
454 (pd->command_set == MODEL_COMMAND_2000 ||
455 pd->command_set == MODEL_COMMAND_PRO))
456 stp_send_command(v, "\033(c", "bll", top, bot);
457 else
458 stp_send_command(v, "\033(c", "bhh", top, bot);
459 }
460
461 static void
462 escp2_set_paper_dimensions(stp_vars_t *v)
463 {
464 escp2_privdata_t *pd = get_privdata(v);
465 if (pd->advanced_command_set)
466 {
467 const stp_vars_t *pv = pd->media_settings;
468 int w = pd->page_true_width * pd->page_management_units / 72;
469 int h = (pd->page_true_height + pd->paper_extra_bottom) *
470 pd->page_management_units / 72;
471 stp_send_command(v, "\033(S", "bll", w, h);
472 if (stp_check_int_parameter(pv, "PrintMethod", STP_PARAMETER_ACTIVE))
473 stp_send_command(v, "\033(m", "bc",
474 stp_get_int_parameter(pv, "PrintMethod"));
475 }
476 }
477
478 static void
479 escp2_set_printhead_resolution(stp_vars_t *v)
480 {
481 escp2_privdata_t *pd = get_privdata(v);
482 if (pd->use_extended_commands)
483 {
484 int xres;
485 int yres = pd->resolution_scale;
486
487 xres = pd->resolution_scale / pd->physical_xdpi;
488
489 if (pd->command_set == MODEL_COMMAND_PRO && pd->printer_weave)
490 yres = yres / pd->res->vres;
491 else if (pd->split_channel_count > 1)
492 yres = yres * pd->nozzle_separation / pd->base_separation *
493 pd->split_channel_count;
494 else
495 yres = yres * pd->nozzle_separation / pd->base_separation;
496
497 /* Magic resolution cookie */
498 stp_send_command(v, "\033(D", "bhcc", pd->resolution_scale, yres, xres);
499 }
500 }
501
502 static void
503 set_vertical_position(stp_vars_t *v, stp_pass_t *pass)
504 {
505 escp2_privdata_t *pd = get_privdata(v);
506 int advance = pass->logicalpassstart - pd->last_pass_offset -
507 (pd->separation_rows - 1);
508 advance = advance * pd->vertical_units / pd->res->printed_vres;
509 if (pass->logicalpassstart > pd->last_pass_offset ||
510 (pd->send_zero_pass_advance && pass->pass > pd->last_pass) ||
511 pd->printing_initial_vertical_offset != 0)
512 {
513 advance += pd->printing_initial_vertical_offset;
514 pd->printing_initial_vertical_offset = 0;
515 if (pd->use_extended_commands)
516 stp_send_command(v, "\033(v", "bl", advance);
517 else
518 stp_send_command(v, "\033(v", "bh", advance);
519 pd->last_pass_offset = pass->logicalpassstart;
520 pd->last_pass = pass->pass;
521 }
522 }
523
524 static void
525 set_color(stp_vars_t *v, stp_pass_t *pass, int color)
526 {
527 escp2_privdata_t *pd = get_privdata(v);
528 (void)pass;
529
530 if (pd->last_color != color && ! pd->use_extended_commands)
531 {
532 int ncolor = pd->channels[color]->color;
533 int subchannel = pd->channels[color]->subchannel;
534 if (subchannel >= 0)
535 stp_send_command(v, "\033(r", "bcc", subchannel, ncolor);
536 else
537 stp_send_command(v, "\033r", "c", ncolor);
538 pd->last_color = color;
539 }
540 }
541
542 static void
543 set_horizontal_position(stp_vars_t *v, stp_pass_t *pass, int vertical_subpass)
544 {
545 escp2_privdata_t *pd = get_privdata(v);
546 int microoffset = (vertical_subpass & (pd->horizontal_passes - 1)) *
547 pd->image_scaled_width / pd->image_printed_width;
548 int pos = pd->image_left_position + microoffset;
549 (void)pass;
550
551 if (pos != 0)
552 {
553 if (pd->command_set == MODEL_COMMAND_PRO || pd->variable_dots)
554 stp_send_command(v, "\033($", "bl", pos);
555 else if (pd->advanced_command_set || pd->res->hres > 720)
556 stp_send_command(v, "\033(\\", "bhh", pd->micro_units, pos);
557 else
558 stp_send_command(v, "\033\\", "h", pos);
559 }
560 }
561
562 static void
563 send_print_command(stp_vars_t *v, stp_pass_t *pass, int ncolor, int nlines)
564 {
565 escp2_privdata_t *pd = get_privdata(v);
566 (void)pass;
567
568 int lwidth = (pd->image_printed_width + (pd->horizontal_passes - 1)) /
569 pd->horizontal_passes;
570 if (pd->command_set == MODEL_COMMAND_PRO || pd->variable_dots)
571 {
572 int nwidth = pd->bitwidth * ((lwidth + 7) / 8);
573 stp_send_command(v, "\033i", "ccchh", ncolor,
574 (stp_get_debug_level() & STP_DBG_NO_COMPRESSION) ? 0 : 1,
575 pd->bitwidth, nwidth, nlines);
576 }
577 else
578 {
579 int ygap = 3600 / pd->vertical_units;
580 int xgap = 3600 / pd->physical_xdpi;
581 if (pd->nozzles == 1)
582 {
583 if (pd->vertical_units == 720 && pd->extra_720dpi_separation)
584 ygap *= pd->extra_720dpi_separation;
585 }
586 else if (pd->extra_720dpi_separation)
587 ygap *= pd->extra_720dpi_separation;
588 else if (pd->pseudo_separation_rows > 0)
589 ygap *= pd->pseudo_separation_rows;
590 else
591 ygap *= pd->separation_rows;
592 stp_send_command(v, "\033.", "cccch",
593 (stp_get_debug_level() & STP_DBG_NO_COMPRESSION) ? 0 : 1,
594 ygap, xgap, nlines, lwidth);
595 }
596 }
597
598 static void
599 send_extra_data(stp_vars_t *v, int extralines)
600 {
601 escp2_privdata_t *pd = get_privdata(v);
602 int lwidth = (pd->image_printed_width + (pd->horizontal_passes - 1)) /
603 pd->horizontal_passes;
604 if (stp_get_debug_level() & STP_DBG_NO_COMPRESSION)
605 {
606 int i, k;
607 for (k = 0; k < extralines; k++)
608 for (i = 0; i < pd->bitwidth * (lwidth + 7) / 8; i++)
609 stp_putc(0, v);
610 }
611 else
612 {
613 int k, l;
614 int bytes_to_fill = pd->bitwidth * ((lwidth + 7) / 8);
615 int full_blocks = bytes_to_fill / 128;
616 int leftover = bytes_to_fill % 128;
617 int total_bytes = extralines * (full_blocks + 1) * 2;
618 unsigned char *buf = stp_malloc(total_bytes);
619 total_bytes = 0;
620 for (k = 0; k < extralines; k++)
621 {
622 for (l = 0; l < full_blocks; l++)
623 {
624 buf[total_bytes++] = 129;
625 buf[total_bytes++] = 0;
626 }
627 if (leftover == 1)
628 {
629 buf[total_bytes++] = 1;
630 buf[total_bytes++] = 0;
631 }
632 else if (leftover > 0)
633 {
634 buf[total_bytes++] = 257 - leftover;
635 buf[total_bytes++] = 0;
636 }
637 }
638 stp_zfwrite((const char *) buf, total_bytes, 1, v);
639 stp_free(buf);
640 }
641 }
642
643 void
644 stpi_escp2_init_printer(stp_vars_t *v)
645 {
646 escp2_reset_printer(v);
647 escp2_set_remote_sequence(v);
648 escp2_set_graphics_mode(v);
649 escp2_set_resolution(v);
650 escp2_set_color(v);
651 escp2_set_printer_weave(v);
652 escp2_set_printhead_speed(v);
653 escp2_set_dot_size(v);
654 escp2_set_cutter(v);
655 escp2_set_printhead_resolution(v);
656 escp2_set_page_height(v);
657 escp2_set_margins(v);
658 escp2_set_paper_dimensions(v);
659 }
660
661 void
662 stpi_escp2_deinit_printer(stp_vars_t *v)
663 {
664 escp2_privdata_t *pd = get_privdata(v);
665 stp_puts("\033@", v); /* ESC/P2 reset */
666 if (pd->advanced_command_set || pd->input_slot)
667 {
668 stp_send_command(v, "\033(R", "bcs", 0, "REMOTE1");
669 if (pd->inkname->deinit_sequence)
670 stp_write_raw(pd->inkname->deinit_sequence, v);
671 if (pd->input_slot && pd->input_slot->deinit_sequence)
672 stp_write_raw(pd->input_slot->deinit_sequence, v);
673 /* Load settings from NVRAM */
674 stp_send_command(v, "LD", "b");
675
676 /* Magic deinit sequence reported by Simone Falsini */
677 if (pd->deinit_remote_sequence)
678 stp_write_raw(pd->deinit_remote_sequence, v);
679 /* Exit remote mode */
680 stp_send_command(v, "\033", "ccc", 0, 0, 0);
681 }
682 }
683
684 void
685 stpi_escp2_flush_pass(stp_vars_t *v, int passno, int vertical_subpass)
686 {
687 int j;
688 escp2_privdata_t *pd = get_privdata(v);
689 stp_lineoff_t *lineoffs = stp_get_lineoffsets_by_pass(v, passno);
690 stp_lineactive_t *lineactive = stp_get_lineactive_by_pass(v, passno);
691 const stp_linebufs_t *bufs = stp_get_linebases_by_pass(v, passno);
692 stp_pass_t *pass = stp_get_pass_by_pass(v, passno);
693 stp_linecount_t *linecount = stp_get_linecount_by_pass(v, passno);
694 int minlines = pd->min_nozzles;
695 int nozzle_start = pd->nozzle_start;
696
697 for (j = 0; j < pd->channels_in_use; j++)
698 {
699 if (lineactive->v[j] > 0)
700 {
701 int ncolor = pd->channels[j]->color;
702 int subchannel = pd->channels[j]->subchannel;
703 int nlines = linecount->v[j];
704 int extralines = 0;
705 set_vertical_position(v, pass);
706 set_color(v, pass, j);
707 if (subchannel >= 0)
708 ncolor |= (subchannel << 4);
709
710 if (pd->split_channels)
711 {
712 int sc = pd->split_channel_count;
713 int k, l;
714 int minlines_lo, nozzle_start_lo;
715 minlines /= sc;
716 nozzle_start /= sc;
717 minlines_lo = pd->min_nozzles - (minlines * sc);
718 nozzle_start_lo = pd->nozzle_start - (nozzle_start * sc);
719 for (k = 0; k < sc; k++)
720 {
721 int ml = minlines + (k < minlines_lo ? 1 : 0);
722 int ns = nozzle_start + (k < nozzle_start_lo ? 1 : 0);
723 int lc = ((nlines + (sc - k - 1)) / sc);
724 int base = (pd->nozzle_start + k) % sc;
725 if (lc < ml)
726 extralines = ml - lc;
727 else
728 extralines = 0;
729 extralines -= ns;
730 if (extralines < 0)
731 extralines = 0;
732 if (lc + extralines > 0)
733 {
734 int sc_off = k + j * sc;
735 set_horizontal_position(v, pass, vertical_subpass);
736 send_print_command(v, pass, pd->split_channels[sc_off],
737 lc + extralines + ns);
738 if (ns > 0)
739 send_extra_data(v, ns);
740 for (l = 0; l < lc; l++)
741 {
742 int sp = (l * sc) + base;
743 unsigned long offset = sp * pd->split_channel_width;
744 if (!(stp_get_debug_level() & STP_DBG_NO_COMPRESSION))
745 {
746 unsigned char *comp_ptr;
747 stp_pack_tiff(v, bufs->v[j] + offset,
748 pd->split_channel_width,
749 pd->comp_buf, &comp_ptr, NULL, NULL);
750 stp_zfwrite((const char *) pd->comp_buf,
751 comp_ptr - pd->comp_buf, 1, v);
752 }
753 else
754 stp_zfwrite((const char *) bufs->v[j] + offset,
755 pd->split_channel_width, 1, v);
756 }
757 if (extralines > 0)
758 send_extra_data(v, extralines);
759 stp_send_command(v, "\r", "");
760 }
761 }
762 }
763 else
764 {
765 set_horizontal_position(v, pass, vertical_subpass);
766 if (nlines < minlines)
767 {
768 extralines = minlines - nlines;
769 nlines = minlines;
770 }
771 send_print_command(v, pass, ncolor, nlines);
772 extralines -= nozzle_start;
773 /*
774 * Send the data
775 */
776 if (nozzle_start)
777 send_extra_data(v, nozzle_start);
778 stp_zfwrite((const char *)bufs->v[j], lineoffs->v[j], 1, v);
779 if (extralines > 0)
780 send_extra_data(v, extralines);
781 stp_send_command(v, "\r", "");
782 }
783 pd->printed_something = 1;
784 }
785 lineoffs->v[j] = 0;
786 linecount->v[j] = 0;
787 }
788 }
789
790 void
791 stpi_escp2_terminate_page(stp_vars_t *v)
792 {
793 escp2_privdata_t *pd = get_privdata(v);
794 if (!pd->input_slot ||
795 !(pd->input_slot->roll_feed_cut_flags & ROLL_FEED_DONT_EJECT))
796 {
797 if (!pd->printed_something)
798 stp_send_command(v, "\n", "");
799 stp_send_command(v, "\f", ""); /* Eject page */
800 }
801 }