| 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 eral 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 <strings.h> |
| 29 | #include "print-escp2.h" |
| 30 | |
| 31 | static stp_mxml_node_t * |
| 32 | get_media_size_xml(const stp_vars_t *v) |
| 33 | { |
| 34 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 35 | return printdef->media_sizes; |
| 36 | } |
| 37 | |
| 38 | int |
| 39 | stpi_escp2_load_media_sizes(const stp_vars_t *v, const char *name) |
| 40 | { |
| 41 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 42 | stp_mxml_node_t *node = |
| 43 | stp_xml_parse_file_from_path_safe(name, "escp2MediaSizes", NULL); |
| 44 | stp_mxml_node_t **xnode = (stp_mxml_node_t **) &(printdef->media_sizes); |
| 45 | *xnode = node; |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | stpi_escp2_set_media_size(stp_vars_t *v, const stp_vars_t *src) |
| 51 | { |
| 52 | const char *name = stp_get_string_parameter(src, "PageSize"); |
| 53 | if (name) |
| 54 | { |
| 55 | stp_mxml_node_t *node = get_media_size_xml(src); |
| 56 | stp_mxml_node_t *xnode = stp_mxmlFindElement(node, node, "MediaSize", |
| 57 | "name", name, STP_MXML_DESCEND); |
| 58 | if (xnode) |
| 59 | { |
| 60 | stp_vars_fill_from_xmltree_ref(xnode->child, node, v); |
| 61 | return; |
| 62 | } |
| 63 | xnode = stp_mxmlFindElement(node, node, "MediaSize", "type", "default", |
| 64 | STP_MXML_DESCEND); |
| 65 | if (xnode) |
| 66 | { |
| 67 | stp_vars_fill_from_xmltree_ref(xnode->child, node, v); |
| 68 | return; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static const char * |
| 74 | paper_namefunc(const void *item) |
| 75 | { |
| 76 | const paper_t *p = (const paper_t *) (item); |
| 77 | return p->cname; |
| 78 | } |
| 79 | |
| 80 | int |
| 81 | stpi_escp2_load_media(const stp_vars_t *v, const char *name) |
| 82 | { |
| 83 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 84 | stp_mxml_node_t *node = |
| 85 | stp_xml_parse_file_from_path_safe(name, "escp2Papers", NULL); |
| 86 | stp_mxml_node_t **xnode = (stp_mxml_node_t **) &(printdef->media); |
| 87 | stp_list_t **xcache = (stp_list_t **) &(printdef->media_cache); |
| 88 | stp_string_list_t **xpapers = (stp_string_list_t **) &(printdef->papers); |
| 89 | static const char *xcache_cache = "escp2Papers_xcache"; |
| 90 | static const char *xpapers_cache = "escp2Papers_xpapers"; |
| 91 | *xnode = node->parent; |
| 92 | *xcache = (stp_list_t *) stp_refcache_find_item(xcache_cache, name); |
| 93 | if (! *xcache) |
| 94 | { |
| 95 | *xcache = stp_list_create(); |
| 96 | stp_list_set_namefunc(*xcache, paper_namefunc); |
| 97 | stp_refcache_add_item(xcache_cache, name, *xcache); |
| 98 | } |
| 99 | *xpapers = (stp_string_list_t *) stp_refcache_find_item(xpapers_cache, name); |
| 100 | if (! *xpapers) |
| 101 | { |
| 102 | *xpapers = stp_string_list_create(); |
| 103 | stp_refcache_add_item(xpapers_cache, name, *xpapers); |
| 104 | node = node->child; |
| 105 | while (node) |
| 106 | { |
| 107 | if (node->type == STP_MXML_ELEMENT && |
| 108 | strcmp(node->value.element.name, "paper") == 0) |
| 109 | stp_string_list_add_string(*xpapers, |
| 110 | stp_mxmlElementGetAttr(node, "name"), |
| 111 | stp_mxmlElementGetAttr(node, "text")); |
| 112 | node = node->next; |
| 113 | } |
| 114 | } |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | static stp_mxml_node_t * |
| 119 | get_media_xml(const stp_vars_t *v) |
| 120 | { |
| 121 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 122 | return printdef->media; |
| 123 | } |
| 124 | |
| 125 | static stp_list_t * |
| 126 | get_media_cache(const stp_vars_t *v) |
| 127 | { |
| 128 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 129 | return printdef->media_cache; |
| 130 | } |
| 131 | |
| 132 | int |
| 133 | stpi_escp2_has_media_feature(const stp_vars_t *v, const char *name) |
| 134 | { |
| 135 | stp_mxml_node_t *doc = get_media_xml(v); |
| 136 | if (doc) |
| 137 | return (stp_mxmlFindElement(doc, doc, "feature", "name", name, |
| 138 | STP_MXML_DESCEND) != NULL); |
| 139 | else |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | static paper_t * |
| 145 | build_media_type(const stp_vars_t *v, const char *name, const inklist_t *ink, |
| 146 | const res_t *res) |
| 147 | { |
| 148 | stp_mxml_node_t *node; |
| 149 | stp_mxml_node_t *doc = get_media_xml(v); |
| 150 | const char *pclass; |
| 151 | paper_t *answer; |
| 152 | stp_vars_t *vv = stp_vars_create(); |
| 153 | if (!doc) |
| 154 | return NULL; |
| 155 | node = stp_mxmlFindElement(doc, doc, "paper", "name", name, STP_MXML_DESCEND); |
| 156 | if (!node) |
| 157 | return NULL; |
| 158 | answer = stp_zalloc(sizeof(paper_t)); |
| 159 | answer->name = stp_mxmlElementGetAttr(node, "name"); |
| 160 | answer->text = gettext(stp_mxmlElementGetAttr(node, "text")); |
| 161 | pclass = stp_mxmlElementGetAttr(node, "class"); |
| 162 | answer->v = vv; |
| 163 | if (! pclass || strcasecmp(pclass, "plain") == 0) |
| 164 | answer->paper_class = PAPER_PLAIN; |
| 165 | else if (strcasecmp(pclass, "good") == 0) |
| 166 | answer->paper_class = PAPER_GOOD; |
| 167 | else if (strcasecmp(pclass, "photo") == 0) |
| 168 | answer->paper_class = PAPER_PHOTO; |
| 169 | else if (strcasecmp(pclass, "premium") == 0) |
| 170 | answer->paper_class = PAPER_PREMIUM_PHOTO; |
| 171 | else if (strcasecmp(pclass, "transparency") == 0) |
| 172 | answer->paper_class = PAPER_TRANSPARENCY; |
| 173 | else |
| 174 | answer->paper_class = PAPER_PLAIN; |
| 175 | answer->preferred_ink_type = stp_mxmlElementGetAttr(node, "PreferredInktype"); |
| 176 | answer->preferred_ink_set = stp_mxmlElementGetAttr(node, "PreferredInkset"); |
| 177 | stp_vars_fill_from_xmltree_ref(node->child, doc, vv); |
| 178 | if (ink && ink->name) |
| 179 | { |
| 180 | stp_mxml_node_t *inknode = stp_mxmlFindElement(node, node, "ink", |
| 181 | "name", ink->name, |
| 182 | STP_MXML_DESCEND); |
| 183 | STPI_ASSERT(inknode, v); |
| 184 | stp_vars_fill_from_xmltree_ref(inknode->child, doc, vv); |
| 185 | } |
| 186 | if (res && res->name) |
| 187 | { |
| 188 | stp_mxml_node_t *resnode = stp_mxmlFindElement(node, node, "resolution", |
| 189 | "name", res->name, |
| 190 | STP_MXML_DESCEND); |
| 191 | if (resnode) |
| 192 | stp_vars_fill_from_xmltree_ref(resnode->child, doc, vv); |
| 193 | } |
| 194 | return answer; |
| 195 | } |
| 196 | |
| 197 | static char * |
| 198 | build_media_id(const char *name, const inklist_t *ink, const res_t *res) |
| 199 | { |
| 200 | char *answer; |
| 201 | stp_asprintf(&answer, "%s %s %s", |
| 202 | name, |
| 203 | ink ? ink->name : "", |
| 204 | res ? res->name : ""); |
| 205 | return answer; |
| 206 | } |
| 207 | |
| 208 | static const paper_t * |
| 209 | get_media_type_named(const stp_vars_t *v, const char *name, |
| 210 | int ignore_res) |
| 211 | { |
| 212 | paper_t *answer = NULL; |
| 213 | int i; |
| 214 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 215 | const stp_string_list_t *p = printdef->papers; |
| 216 | const res_t *res = ignore_res ? NULL : stpi_escp2_find_resolution(v); |
| 217 | const inklist_t *inklist = stpi_escp2_inklist(v); |
| 218 | char *media_id = build_media_id(name, inklist, res); |
| 219 | stp_list_t *cache = get_media_cache(v); |
| 220 | stp_list_item_t *li = stp_list_get_item_by_name(cache, media_id); |
| 221 | if (li) |
| 222 | { |
| 223 | stp_free(media_id); |
| 224 | answer = (paper_t *) stp_list_item_get_data(li); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | int paper_type_count = stp_string_list_count(p); |
| 229 | for (i = 0; i < paper_type_count; i++) |
| 230 | { |
| 231 | if (!strcmp(name, stp_string_list_param(p, i)->name)) |
| 232 | { |
| 233 | stp_xml_init(); |
| 234 | answer = build_media_type(v, name, inklist, res); |
| 235 | stp_xml_exit(); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | if (answer) |
| 240 | { |
| 241 | answer->cname = media_id; |
| 242 | stp_list_item_create(cache, NULL, answer); |
| 243 | } |
| 244 | } |
| 245 | return answer; |
| 246 | } |
| 247 | |
| 248 | const paper_t * |
| 249 | stpi_escp2_get_media_type(const stp_vars_t *v, int ignore_res) |
| 250 | { |
| 251 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 252 | const stp_string_list_t *p = printdef->papers; |
| 253 | if (p) |
| 254 | { |
| 255 | const char *name = stp_get_string_parameter(v, "MediaType"); |
| 256 | if (name) |
| 257 | return get_media_type_named(v, name, ignore_res); |
| 258 | } |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | const paper_t * |
| 263 | stpi_escp2_get_default_media_type(const stp_vars_t *v) |
| 264 | { |
| 265 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 266 | const stp_string_list_t *p = printdef->papers; |
| 267 | if (p) |
| 268 | { |
| 269 | int paper_type_count = stp_string_list_count(p); |
| 270 | if (paper_type_count >= 0) |
| 271 | return get_media_type_named(v, stp_string_list_param(p, 0)->name, 1); |
| 272 | } |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | static const char * |
| 278 | slots_namefunc(const void *item) |
| 279 | { |
| 280 | const input_slot_t *p = (const input_slot_t *) (item); |
| 281 | return p->name; |
| 282 | } |
| 283 | |
| 284 | int |
| 285 | stpi_escp2_load_input_slots(const stp_vars_t *v, const char *name) |
| 286 | { |
| 287 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 288 | stp_mxml_node_t *node = |
| 289 | stp_xml_parse_file_from_path_safe(name, "escp2InputSlots", NULL); |
| 290 | stp_mxml_node_t **xnode = (stp_mxml_node_t **) &(printdef->slots); |
| 291 | stp_list_t **xcache = (stp_list_t **) &(printdef->slots_cache); |
| 292 | stp_string_list_t **xslots = (stp_string_list_t **) &(printdef->input_slots); |
| 293 | static const char *xcache_cache = "escp2InputSlots_xcache"; |
| 294 | static const char *xslots_cache = "escp2InputSlots_slots"; |
| 295 | |
| 296 | *xnode = node->parent; |
| 297 | *xcache = (stp_list_t *) stp_refcache_find_item(xcache_cache, name); |
| 298 | if (! *xcache) |
| 299 | { |
| 300 | *xcache = stp_list_create(); |
| 301 | stp_list_set_namefunc(*xcache, slots_namefunc); |
| 302 | stp_refcache_add_item(xcache_cache, name, *xcache); |
| 303 | } |
| 304 | |
| 305 | *xslots = (stp_string_list_t *) stp_refcache_find_item(xslots_cache, name); |
| 306 | if (! *xslots) |
| 307 | { |
| 308 | *xslots = stp_string_list_create(); |
| 309 | stp_refcache_add_item(xslots_cache, name, *xslots); |
| 310 | node = node->child; |
| 311 | while (node) |
| 312 | { |
| 313 | if (node->type == STP_MXML_ELEMENT && |
| 314 | strcmp(node->value.element.name, "slot") == 0) |
| 315 | stp_string_list_add_string(*xslots, |
| 316 | stp_mxmlElementGetAttr(node, "name"), |
| 317 | stp_mxmlElementGetAttr(node, "text")); |
| 318 | node = node->next; |
| 319 | } |
| 320 | } |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | static stp_mxml_node_t * |
| 325 | get_slots_xml(const stp_vars_t *v) |
| 326 | { |
| 327 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 328 | return printdef->slots; |
| 329 | } |
| 330 | |
| 331 | static stp_list_t * |
| 332 | get_slots_cache(const stp_vars_t *v) |
| 333 | { |
| 334 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 335 | return printdef->slots_cache; |
| 336 | } |
| 337 | |
| 338 | static input_slot_t * |
| 339 | build_input_slot(const stp_vars_t *v, const char *name) |
| 340 | { |
| 341 | stp_mxml_node_t *node, *n1; |
| 342 | stp_mxml_node_t *doc = get_slots_xml(v); |
| 343 | input_slot_t *answer; |
| 344 | if (!doc) |
| 345 | return NULL; |
| 346 | node = stp_mxmlFindElement(doc, doc, "slot", "name", name, STP_MXML_DESCEND); |
| 347 | if (!node) |
| 348 | return NULL; |
| 349 | answer = stp_zalloc(sizeof(input_slot_t)); |
| 350 | answer->name = stp_mxmlElementGetAttr(node, "name"); |
| 351 | answer->text = gettext(stp_mxmlElementGetAttr(node, "text")); |
| 352 | n1 = stp_xml_get_node(node, "CD", NULL); |
| 353 | if (n1) |
| 354 | answer->is_cd = 1; |
| 355 | n1 = stp_xml_get_node(node, "RollFeed", NULL); |
| 356 | if (n1) |
| 357 | { |
| 358 | answer->is_roll_feed = 1; |
| 359 | if (stp_xml_get_node(n1, "CutAll", NULL)) |
| 360 | answer->roll_feed_cut_flags |= ROLL_FEED_CUT_ALL; |
| 361 | if (stp_xml_get_node(n1, "CutLast", NULL)) |
| 362 | answer->roll_feed_cut_flags |= ROLL_FEED_CUT_LAST; |
| 363 | if (stp_xml_get_node(n1, "DontEject", NULL)) |
| 364 | answer->roll_feed_cut_flags |= ROLL_FEED_DONT_EJECT; |
| 365 | } |
| 366 | n1 = stp_xml_get_node(node, "Duplex", NULL); |
| 367 | if (n1) |
| 368 | { |
| 369 | if (stp_xml_get_node(n1, "Tumble", NULL)) |
| 370 | answer->duplex |= DUPLEX_TUMBLE; |
| 371 | if (stp_xml_get_node(n1, "NoTumble", NULL)) |
| 372 | answer->duplex |= DUPLEX_NO_TUMBLE; |
| 373 | } |
| 374 | n1 = stp_xml_get_node(node, "InitSequence", NULL); |
| 375 | if (n1 && n1->child && n1->child->type == STP_MXML_TEXT) |
| 376 | answer->init_sequence = stp_xmlstrtoraw(n1->child->value.text.string); |
| 377 | n1 = stp_xml_get_node(node, "DeinitSequence", NULL); |
| 378 | if (n1 && n1->child && n1->child->type == STP_MXML_TEXT) |
| 379 | answer->deinit_sequence = stp_xmlstrtoraw(n1->child->value.text.string); |
| 380 | n1 = stp_xml_get_node(node, "ExtraHeight", NULL); |
| 381 | if (n1 && n1->child && n1->child->type == STP_MXML_TEXT) |
| 382 | answer->extra_height = stp_xmlstrtoul(n1->child->value.text.string); |
| 383 | return answer; |
| 384 | } |
| 385 | |
| 386 | int |
| 387 | stpi_escp2_printer_supports_rollfeed(const stp_vars_t *v) |
| 388 | { |
| 389 | stp_mxml_node_t *node = get_slots_xml(v); |
| 390 | if (stp_xml_get_node(node, "RollFeed", NULL)) |
| 391 | return 1; |
| 392 | else |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | int |
| 397 | stpi_escp2_printer_supports_print_to_cd(const stp_vars_t *v) |
| 398 | { |
| 399 | stp_mxml_node_t *node = get_slots_xml(v); |
| 400 | if (stp_xml_get_node(node, "CD", NULL)) |
| 401 | return 1; |
| 402 | else |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | int |
| 407 | stpi_escp2_printer_supports_duplex(const stp_vars_t *v) |
| 408 | { |
| 409 | stp_mxml_node_t *node = get_slots_xml(v); |
| 410 | if (stp_xml_get_node(node, "Duplex", NULL)) |
| 411 | return 1; |
| 412 | else |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static const input_slot_t * |
| 417 | get_input_slot_named(const stp_vars_t *v, const char *name) |
| 418 | { |
| 419 | input_slot_t *answer = NULL; |
| 420 | int i; |
| 421 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 422 | const stp_string_list_t *p = printdef->input_slots; |
| 423 | stp_list_t *cache = get_slots_cache(v); |
| 424 | stp_list_item_t *li = stp_list_get_item_by_name(cache, name); |
| 425 | if (li) |
| 426 | answer = (input_slot_t *) stp_list_item_get_data(li); |
| 427 | else |
| 428 | { |
| 429 | int slot_count = stp_string_list_count(p); |
| 430 | for (i = 0; i < slot_count; i++) |
| 431 | { |
| 432 | if (!strcmp(name, stp_string_list_param(p, i)->name)) |
| 433 | { |
| 434 | stp_xml_init(); |
| 435 | answer = build_input_slot(v, name); |
| 436 | stp_xml_exit(); |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | if (answer) |
| 441 | stp_list_item_create(cache, NULL, answer); |
| 442 | } |
| 443 | return answer; |
| 444 | } |
| 445 | |
| 446 | const input_slot_t * |
| 447 | stpi_escp2_get_input_slot(const stp_vars_t *v) |
| 448 | { |
| 449 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 450 | const stp_string_list_t *p = printdef->input_slots; |
| 451 | if (p) |
| 452 | { |
| 453 | const char *name = stp_get_string_parameter(v, "InputSlot"); |
| 454 | if (name) |
| 455 | return get_input_slot_named(v, name); |
| 456 | } |
| 457 | return NULL; |
| 458 | } |