| 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 | #ifdef HAVE_CONFIG_H |
| 23 | #include <config.h> |
| 24 | #endif |
| 25 | #include <gutenprint/gutenprint.h> |
| 26 | #include "gutenprint-internal.h" |
| 27 | #include <gutenprint/gutenprint-intl-internal.h> |
| 28 | #include "print-escp2.h" |
| 29 | #include <limits.h> |
| 30 | #include <sys/param.h> |
| 31 | |
| 32 | typedef struct |
| 33 | { |
| 34 | const char *attr_name; |
| 35 | short bit_shift; |
| 36 | short bit_width; |
| 37 | } escp2_printer_attr_t; |
| 38 | |
| 39 | static const escp2_printer_attr_t escp2_printer_attrs[] = |
| 40 | { |
| 41 | { "command_mode", 0, 4 }, |
| 42 | { "zero_margin", 4, 3 }, |
| 43 | { "variable_mode", 7, 1 }, |
| 44 | { "graymode", 8, 1 }, |
| 45 | { "fast_360", 9, 1 }, |
| 46 | { "send_zero_advance", 10, 1 }, |
| 47 | { "supports_ink_change", 11, 1 }, |
| 48 | { "packet_mode", 12, 1 }, |
| 49 | { "interchangeable_ink", 13, 1 }, |
| 50 | { "envelope_landscape", 14, 1 }, |
| 51 | { "roll_only", 15, 1 }, |
| 52 | }; |
| 53 | |
| 54 | static stpi_escp2_printer_t *escp2_model_capabilities; |
| 55 | |
| 56 | static int escp2_model_count = 0; |
| 57 | |
| 58 | static int |
| 59 | load_model_from_file(const stp_vars_t *v, const char *filename, int depth) |
| 60 | { |
| 61 | int model = -1; |
| 62 | stp_mxml_node_t *xmod = |
| 63 | stp_xml_parse_file_from_path_uncached_safe(filename, "escp2Model", NULL); |
| 64 | const char *id = stp_mxmlElementGetAttr(xmod, "id"); |
| 65 | stp_mxml_node_t *tmp = xmod->child; |
| 66 | stpi_escp2_printer_t *p = stpi_escp2_get_printer(v); |
| 67 | const char *stmp = stp_mxmlElementGetAttr(xmod, "base"); |
| 68 | if (id) |
| 69 | model = stp_xmlstrtol(id); |
| 70 | if (depth == 0) |
| 71 | { |
| 72 | p->max_black_resolution = -1; |
| 73 | p->cd_x_offset = -1; |
| 74 | p->cd_y_offset = -1; |
| 75 | p->duplex_left_margin = SHRT_MIN; |
| 76 | p->duplex_right_margin = SHRT_MIN; |
| 77 | p->duplex_top_margin = SHRT_MIN; |
| 78 | p->duplex_bottom_margin = SHRT_MIN; |
| 79 | } |
| 80 | /* Allow recursive definitions */ |
| 81 | if (stmp) |
| 82 | { |
| 83 | load_model_from_file(v, stmp, depth + 1); |
| 84 | } |
| 85 | while (tmp) |
| 86 | { |
| 87 | if (tmp->type == STP_MXML_ELEMENT) |
| 88 | { |
| 89 | const char *name = tmp->value.element.name; |
| 90 | const char *target = stp_mxmlElementGetAttr(tmp, "src"); |
| 91 | if (target) |
| 92 | { |
| 93 | /* FIXME need to allow override of these! */ |
| 94 | if (!strcmp(name, "media")) |
| 95 | stpi_escp2_load_media(v, target); |
| 96 | else if (!strcmp(name, "inputSlots")) |
| 97 | stpi_escp2_load_input_slots(v, target); |
| 98 | else if (!strcmp(name, "mediaSizes")) |
| 99 | stpi_escp2_load_media_sizes(v, target); |
| 100 | else if (!strcmp(name, "printerWeaves")) |
| 101 | stpi_escp2_load_printer_weaves(v, target); |
| 102 | else if (!strcmp(name, "qualityPresets")) |
| 103 | stpi_escp2_load_quality_presets(v, target); |
| 104 | else if (!strcmp(name, "resolutions")) |
| 105 | stpi_escp2_load_resolutions(v, target, NULL); |
| 106 | else if (!strcmp(name, "inkGroup")) |
| 107 | stpi_escp2_load_inkgroup(v, target); |
| 108 | } |
| 109 | else if (tmp->child && tmp->child->type == STP_MXML_TEXT) |
| 110 | { |
| 111 | stp_mxml_node_t *child = tmp->child; |
| 112 | const char *val = child->value.text.string; |
| 113 | if (!strcmp(name, "verticalBorderlessSequence")) |
| 114 | { |
| 115 | STPI_ASSERT(!p->vertical_borderless_sequence, NULL); |
| 116 | p->vertical_borderless_sequence = stp_xmlstrtoraw(val); |
| 117 | } |
| 118 | else if (!strcmp(name, "preinitSequence")) |
| 119 | { |
| 120 | STPI_ASSERT(!p->preinit_sequence, NULL); |
| 121 | p->preinit_sequence = stp_xmlstrtoraw(val); |
| 122 | } |
| 123 | else if (!strcmp(name, "preinitRemoteSequence")) |
| 124 | { |
| 125 | STPI_ASSERT(!p->preinit_remote_sequence, NULL); |
| 126 | p->preinit_remote_sequence = stp_xmlstrtoraw(val); |
| 127 | } |
| 128 | else if (!strcmp(name, "postinitRemoteSequence")) |
| 129 | { |
| 130 | STPI_ASSERT(!p->postinit_remote_sequence, NULL); |
| 131 | p->postinit_remote_sequence = stp_xmlstrtoraw(val); |
| 132 | } |
| 133 | else if (!strcmp(name, "commandSet")) |
| 134 | { |
| 135 | if (!strcmp(val, "1998")) |
| 136 | p->flags |= MODEL_COMMAND_1998; |
| 137 | else if (!strcmp(val, "1999")) |
| 138 | p->flags |= MODEL_COMMAND_1999; |
| 139 | else if (!strcmp(val, "2000")) |
| 140 | p->flags |= MODEL_COMMAND_2000; |
| 141 | else if (!strcmp(val, "Pro")) |
| 142 | p->flags |= MODEL_COMMAND_PRO; |
| 143 | } |
| 144 | else if (!strcmp(name, "borderless")) |
| 145 | { |
| 146 | if (!strcmp(val, "No")) |
| 147 | p->flags |= MODEL_ZEROMARGIN_NO; |
| 148 | else if (!strcmp(val, "Yes")) |
| 149 | p->flags |= MODEL_ZEROMARGIN_YES; |
| 150 | else if (!strcmp(val, "Full")) |
| 151 | p->flags |= MODEL_ZEROMARGIN_FULL; |
| 152 | else if (!strcmp(val, "VerticalRestricted")) |
| 153 | p->flags |= MODEL_ZEROMARGIN_RESTR; |
| 154 | else if (!strcmp(val, "HorizontalOnly")) |
| 155 | p->flags |= MODEL_ZEROMARGIN_H_ONLY; |
| 156 | } |
| 157 | else if (!strcmp(name, "preferredEnvelopeOrientation") && |
| 158 | !strcmp(val, "Landscape")) |
| 159 | p->flags |= MODEL_ENVELOPE_LANDSCAPE_YES; |
| 160 | else if (!strcmp(name, "headConfiguration")) |
| 161 | { |
| 162 | const char *htype = stp_mxmlElementGetAttr(tmp, "type"); |
| 163 | unsigned long data[4] = { 0, 0, 0, 0 }; |
| 164 | while (child) |
| 165 | { |
| 166 | if (child->type == STP_MXML_ELEMENT && child->child && |
| 167 | child->child->type == STP_MXML_TEXT) |
| 168 | { |
| 169 | const char *cname = child->value.element.name; |
| 170 | const char *cval = child->child->value.text.string; |
| 171 | if (!strcmp(cname, "Nozzles")) |
| 172 | data[0] = stp_xmlstrtoul(cval); |
| 173 | else if (!strcmp(cname, "MinNozzles")) |
| 174 | data[1] = stp_xmlstrtoul(cval); |
| 175 | else if (!strcmp(cname, "FirstNozzle")) |
| 176 | data[2] = stp_xmlstrtoul(cval); |
| 177 | else if (!strcmp(cname, "NozzleSeparation")) |
| 178 | data[3] = stp_xmlstrtoul(cval); |
| 179 | } |
| 180 | child = child->next; |
| 181 | } |
| 182 | if (!strcmp(htype, "default")) |
| 183 | { |
| 184 | p->nozzles = data[0]; |
| 185 | p->min_nozzles = data[1]; |
| 186 | p->nozzle_start = data[2]; |
| 187 | p->nozzle_separation = data[3]; |
| 188 | if (p->black_nozzles == 0) |
| 189 | { |
| 190 | p->black_nozzles = data[0]; |
| 191 | p->min_black_nozzles = data[1]; |
| 192 | p->black_nozzle_start = data[2]; |
| 193 | p->black_nozzle_separation = data[3]; |
| 194 | } |
| 195 | if (p->fast_nozzles == 0) |
| 196 | { |
| 197 | p->fast_nozzles = data[0]; |
| 198 | p->min_fast_nozzles = data[1]; |
| 199 | p->fast_nozzle_start = data[2]; |
| 200 | p->fast_nozzle_separation = data[3]; |
| 201 | } |
| 202 | } |
| 203 | else if (!strcmp(htype, "black")) |
| 204 | { |
| 205 | p->black_nozzles = data[0]; |
| 206 | p->min_black_nozzles = data[1]; |
| 207 | p->black_nozzle_start = data[2]; |
| 208 | p->black_nozzle_separation = data[3]; |
| 209 | } |
| 210 | else if (!strcmp(htype, "fast")) |
| 211 | { |
| 212 | p->fast_nozzles = data[0]; |
| 213 | p->min_fast_nozzles = data[1]; |
| 214 | p->fast_nozzle_start = data[2]; |
| 215 | p->fast_nozzle_separation = data[3]; |
| 216 | } |
| 217 | } |
| 218 | else if (!strcmp(name, "margins")) |
| 219 | { |
| 220 | const char *itype = stp_mxmlElementGetAttr(tmp, "interleave"); |
| 221 | const char *mtype = stp_mxmlElementGetAttr(tmp, "media"); |
| 222 | const char *dtype = stp_mxmlElementGetAttr(tmp, "duplex"); |
| 223 | unsigned long data[4]; |
| 224 | int i = 0; |
| 225 | while (child && i < 4) |
| 226 | { |
| 227 | if (child->type == STP_MXML_TEXT) |
| 228 | data[i++] = stp_xmlstrtodim(child->value.text.string); |
| 229 | child = child->next; |
| 230 | } |
| 231 | if (dtype && !strcmp(dtype, "duplex")) |
| 232 | { |
| 233 | p->duplex_left_margin = data[0]; |
| 234 | p->duplex_right_margin = data[1]; |
| 235 | p->duplex_top_margin = data[2]; |
| 236 | p->duplex_bottom_margin = data[3]; |
| 237 | } |
| 238 | else if (itype && !strcmp(itype, "soft") && |
| 239 | mtype && !strcmp(mtype, "sheet")) |
| 240 | { |
| 241 | p->left_margin = data[0]; |
| 242 | p->right_margin = data[1]; |
| 243 | p->top_margin = data[2]; |
| 244 | p->bottom_margin = data[3]; |
| 245 | } |
| 246 | else if (itype && !strcmp(itype, "printer") && |
| 247 | mtype && !strcmp(mtype, "sheet")) |
| 248 | { |
| 249 | p->m_left_margin = data[0]; |
| 250 | p->m_right_margin = data[1]; |
| 251 | p->m_top_margin = data[2]; |
| 252 | p->m_bottom_margin = data[3]; |
| 253 | } |
| 254 | else if (itype && !strcmp(itype, "soft") && |
| 255 | mtype && !strcmp(mtype, "roll")) |
| 256 | { |
| 257 | p->roll_left_margin = data[0]; |
| 258 | p->roll_right_margin = data[1]; |
| 259 | p->roll_top_margin = data[2]; |
| 260 | p->roll_bottom_margin = data[3]; |
| 261 | } |
| 262 | else if (itype && !strcmp(itype, "printer") && |
| 263 | mtype && !strcmp(mtype, "roll")) |
| 264 | { |
| 265 | p->m_roll_left_margin = data[0]; |
| 266 | p->m_roll_right_margin = data[1]; |
| 267 | p->m_roll_top_margin = data[2]; |
| 268 | p->m_roll_bottom_margin = data[3]; |
| 269 | } |
| 270 | } |
| 271 | else if (!strcmp(name, "physicalChannels")) |
| 272 | p->physical_channels = stp_xmlstrtoul(val); |
| 273 | else if (!strcmp(name, "baseSeparation")) |
| 274 | p->base_separation = stp_xmlstrtoul(val); |
| 275 | else if (!strcmp(name, "resolutionScale")) |
| 276 | p->resolution_scale = stp_xmlstrtoul(val); |
| 277 | else if (!strcmp(name, "maxBlackResolution")) |
| 278 | p->max_black_resolution = stp_xmlstrtoul(val); |
| 279 | else if (!strcmp(name, "minimumResolution")) |
| 280 | { |
| 281 | p->min_hres = stp_xmlstrtoul(child->value.text.string); |
| 282 | child = child->next; |
| 283 | p->min_vres = stp_xmlstrtoul(child->value.text.string); |
| 284 | } |
| 285 | else if (!strcmp(name, "maximumResolution")) |
| 286 | { |
| 287 | p->max_hres = stp_xmlstrtoul(child->value.text.string); |
| 288 | child = child->next; |
| 289 | p->max_vres = stp_xmlstrtoul(child->value.text.string); |
| 290 | } |
| 291 | else if (!strcmp(name, "extraVerticalFeed")) |
| 292 | p->extra_feed = stp_xmlstrtoul(val); |
| 293 | else if (!strcmp(name, "separationRows")) |
| 294 | p->separation_rows = stp_xmlstrtoul(val); |
| 295 | else if (!strcmp(name, "pseudoSeparationRows")) |
| 296 | p->pseudo_separation_rows = stp_xmlstrtoul(val); |
| 297 | else if (!strcmp(name, "zeroMarginOffset")) |
| 298 | p->zero_margin_offset = stp_xmlstrtoul(val); |
| 299 | else if (!strcmp(name, "microLeftMargin")) |
| 300 | p->micro_left_margin = stp_xmlstrtoul(val); |
| 301 | else if (!strcmp(name, "initialVerticalOffset")) |
| 302 | p->initial_vertical_offset = stp_xmlstrtoul(val); |
| 303 | else if (!strcmp(name, "blackInitialVerticalOffset")) |
| 304 | p->black_initial_vertical_offset = stp_xmlstrtoul(val); |
| 305 | else if (!strcmp(name, "printingInitialVerticalOffset")) |
| 306 | p->printing_initial_vertical_offset = stp_xmlstrtoul(val); |
| 307 | else if (!strcmp(name, "extra720DPISeparation")) |
| 308 | p->extra_720dpi_separation = stp_xmlstrtoul(val); |
| 309 | else if (!strcmp(name, "minHorizontalAlignment")) |
| 310 | p->min_horizontal_position_alignment = stp_xmlstrtoul(val); |
| 311 | else if (!strcmp(name, "baseHorizontalAlignment")) |
| 312 | p->base_horizontal_position_alignment = stp_xmlstrtoul(val); |
| 313 | else if (!strcmp(name, "bidirectionalAutoUpperLimit")) |
| 314 | p->bidirectional_upper_limit = stp_xmlstrtoul(val); |
| 315 | else if (!strcmp(name, "minimumMediaSize")) |
| 316 | { |
| 317 | p->min_paper_width = stp_xmlstrtodim(child->value.text.string); |
| 318 | child = child->next; |
| 319 | p->min_paper_height = stp_xmlstrtodim(child->value.text.string); |
| 320 | } |
| 321 | else if (!strcmp(name, "maximumMediaSize")) |
| 322 | { |
| 323 | p->max_paper_width = stp_xmlstrtodim(child->value.text.string); |
| 324 | child = child->next; |
| 325 | p->max_paper_height = stp_xmlstrtodim(child->value.text.string); |
| 326 | } |
| 327 | else if (!strcmp(name, "maximumImageableArea")) |
| 328 | { |
| 329 | p->max_imageable_width = stp_xmlstrtodim(child->value.text.string); |
| 330 | child = child->next; |
| 331 | p->max_imageable_height = stp_xmlstrtodim(child->value.text.string); |
| 332 | } |
| 333 | else if (!strcmp(name, "CDOffset")) |
| 334 | { |
| 335 | p->cd_x_offset = stp_xmlstrtodim(child->value.text.string); |
| 336 | child = child->next; |
| 337 | p->cd_y_offset = stp_xmlstrtodim(child->value.text.string); |
| 338 | } |
| 339 | else if (!strcmp(name, "CDMediaSize")) |
| 340 | { |
| 341 | p->cd_page_width = stp_xmlstrtodim(child->value.text.string); |
| 342 | child = child->next; |
| 343 | p->cd_page_height = stp_xmlstrtodim(child->value.text.string); |
| 344 | } |
| 345 | else if (!strcmp(name, "extraBottom")) |
| 346 | p->paper_extra_bottom = stp_xmlstrtodim(val); |
| 347 | else if (!strcmp(name, "AlignmentChoices")) |
| 348 | { |
| 349 | p->alignment_passes = |
| 350 | stp_xmlstrtoul(child->value.text.string); |
| 351 | child = child->next; |
| 352 | p->alignment_choices = |
| 353 | stp_xmlstrtoul(child->value.text.string); |
| 354 | child = child->next; |
| 355 | p->alternate_alignment_passes = |
| 356 | stp_xmlstrtoul(child->value.text.string); |
| 357 | child = child->next; |
| 358 | p->alternate_alignment_choices = |
| 359 | stp_xmlstrtoul(child->value.text.string); |
| 360 | } |
| 361 | else if (!strcmp(name, "ChannelNames")) |
| 362 | { |
| 363 | p->channel_names = stp_string_list_create(); |
| 364 | while (child) |
| 365 | { |
| 366 | if (child->type == STP_MXML_ELEMENT && |
| 367 | !strcmp(child->value.element.name, "ChannelName")) |
| 368 | { |
| 369 | const char *cname = stp_mxmlElementGetAttr(child, "name"); |
| 370 | const char *ctext = stp_mxmlElementGetAttr(child, "text"); |
| 371 | stp_string_list_add_string(p->channel_names, cname, ctext); |
| 372 | } |
| 373 | child = child->next; |
| 374 | } |
| 375 | } |
| 376 | else if (!strcmp(name, "resolutions")) |
| 377 | { |
| 378 | stpi_escp2_load_resolutions(v, filename, tmp); |
| 379 | } |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | if (!strcmp(name, "supportsVariableDropsizes")) |
| 384 | p->flags |= MODEL_VARIABLE_YES; |
| 385 | else if (!strcmp(name, "hasFastGraymode")) |
| 386 | p->flags |= MODEL_GRAYMODE_YES; |
| 387 | else if (!strcmp(name, "hasFast360DPI")) |
| 388 | p->flags |= MODEL_FAST_360_YES; |
| 389 | else if (!strcmp(name, "sendZeroAdvance")) |
| 390 | p->flags |= MODEL_SEND_ZERO_ADVANCE_YES; |
| 391 | else if (!strcmp(name, "supportsInkChange")) |
| 392 | p->flags |= MODEL_SUPPORTS_INK_CHANGE_YES; |
| 393 | else if (!strcmp(name, "supportsD4Mode")) |
| 394 | p->flags |= MODEL_PACKET_MODE_YES; |
| 395 | else if (!strcmp(name, "hasInterchangeableInkCartridges")) |
| 396 | p->flags |= MODEL_INTERCHANGEABLE_INK_YES; |
| 397 | else if (!strcmp(name, "rollOnly")) |
| 398 | p->flags |= MODEL_ROLL_ONLY_YES; |
| 399 | else if (!strcmp(name, "resolutions")) |
| 400 | stpi_escp2_load_resolutions(v, filename, tmp); |
| 401 | } |
| 402 | } |
| 403 | tmp = tmp->next; |
| 404 | } |
| 405 | stp_xml_free_parsed_file(xmod); |
| 406 | return model; |
| 407 | } |
| 408 | |
| 409 | void |
| 410 | stpi_escp2_load_model(const stp_vars_t *v, int model) |
| 411 | { |
| 412 | char buf[MAXPATHLEN+1]; |
| 413 | stp_xml_init(); |
| 414 | snprintf(buf, MAXPATHLEN, "escp2/model/model_%d.xml", model); |
| 415 | int model_id_from_file = load_model_from_file(v, buf, 0); |
| 416 | stp_xml_exit(); |
| 417 | STPI_ASSERT(model_id_from_file == model, v); |
| 418 | } |
| 419 | |
| 420 | stpi_escp2_printer_t * |
| 421 | stpi_escp2_get_printer(const stp_vars_t *v) |
| 422 | { |
| 423 | int model = stp_get_model_id(v); |
| 424 | STPI_ASSERT(model >= 0, v); |
| 425 | if (!escp2_model_capabilities) |
| 426 | { |
| 427 | escp2_model_capabilities = |
| 428 | stp_zalloc(sizeof(stpi_escp2_printer_t) * (model + 1)); |
| 429 | escp2_model_count = model + 1; |
| 430 | } |
| 431 | else if (model >= escp2_model_count) |
| 432 | { |
| 433 | escp2_model_capabilities = |
| 434 | stp_realloc(escp2_model_capabilities, |
| 435 | sizeof(stpi_escp2_printer_t) * (model + 1)); |
| 436 | (void) memset(escp2_model_capabilities + escp2_model_count, 0, |
| 437 | sizeof(stpi_escp2_printer_t) * (model + 1 - escp2_model_count)); |
| 438 | escp2_model_count = model + 1; |
| 439 | } |
| 440 | if (!(escp2_model_capabilities[model].active)) |
| 441 | { |
| 442 | stp_xml_init(); |
| 443 | escp2_model_capabilities[model].active = 1; |
| 444 | stpi_escp2_load_model(v, model); |
| 445 | stp_xml_exit(); |
| 446 | } |
| 447 | return &(escp2_model_capabilities[model]); |
| 448 | } |
| 449 | |
| 450 | model_featureset_t |
| 451 | stpi_escp2_get_cap(const stp_vars_t *v, escp2_model_option_t feature) |
| 452 | { |
| 453 | const stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 454 | model_featureset_t featureset = |
| 455 | (((1ul << escp2_printer_attrs[feature].bit_width) - 1ul) << |
| 456 | escp2_printer_attrs[feature].bit_shift); |
| 457 | return printdef->flags & featureset; |
| 458 | } |
| 459 | |
| 460 | int |
| 461 | stpi_escp2_has_cap(const stp_vars_t *v, escp2_model_option_t feature, |
| 462 | model_featureset_t class) |
| 463 | { |
| 464 | const stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 465 | model_featureset_t featureset = |
| 466 | (((1ul << escp2_printer_attrs[feature].bit_width) - 1ul) << |
| 467 | escp2_printer_attrs[feature].bit_shift); |
| 468 | return ((printdef->flags & featureset) == class); |
| 469 | } |