| 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 | |
| 30 | static inkgroup_t *default_black_inkgroup; |
| 31 | |
| 32 | static void |
| 33 | load_subchannel(stp_mxml_node_t *node, stp_mxml_node_t *root, physical_subchannel_t *icl) |
| 34 | { |
| 35 | const char *name; |
| 36 | stp_mxml_node_t *child = node->child; |
| 37 | (void)root; |
| 38 | |
| 39 | name = stp_mxmlElementGetAttr(node, "color"); |
| 40 | if (name) |
| 41 | icl->color = stp_xmlstrtol(name); |
| 42 | name = stp_mxmlElementGetAttr(node, "subchannel"); |
| 43 | if (name) |
| 44 | icl->subchannel = stp_xmlstrtol(name); |
| 45 | else |
| 46 | icl->subchannel = -1; |
| 47 | name = stp_mxmlElementGetAttr(node, "headOffset"); |
| 48 | if (name) |
| 49 | icl->head_offset = stp_xmlstrtol(name); |
| 50 | name = stp_mxmlElementGetAttr(node, "name"); |
| 51 | if (name) |
| 52 | icl->name = stp_strdup(name); |
| 53 | name = stp_mxmlElementGetAttr(node, "text"); |
| 54 | if (name) |
| 55 | icl->text = stp_strdup(name); |
| 56 | while (child) |
| 57 | { |
| 58 | if (child->type == STP_MXML_ELEMENT) |
| 59 | { |
| 60 | const char *param = child->value.element.name; |
| 61 | name = stp_mxmlElementGetAttr(child, "name"); |
| 62 | if (name && !strcmp(param, "ChannelDensityParam")) |
| 63 | icl->channel_density = stp_strdup(name); |
| 64 | else if (name && !strcmp(param, "SubchannelTransitionParam")) |
| 65 | icl->subchannel_transition = stp_strdup(name); |
| 66 | else if (name && !strcmp(param, "SubchannelValueParam")) |
| 67 | icl->subchannel_value = stp_strdup(name); |
| 68 | else if (name && !strcmp(param, "SubchannelScaleParam")) |
| 69 | icl->subchannel_scale = stp_strdup(name); |
| 70 | else if (!strcmp(param, "SplitChannels")) |
| 71 | { |
| 72 | if (stp_mxmlElementGetAttr(child, "count")) |
| 73 | icl->split_channel_count = |
| 74 | stp_xmlstrtoul(stp_mxmlElementGetAttr(child, "count")); |
| 75 | if (icl->split_channel_count > 0) |
| 76 | { |
| 77 | char *endptr; |
| 78 | int count = 0; |
| 79 | stp_mxml_node_t *cchild = child->child; |
| 80 | icl->split_channels = |
| 81 | stp_zalloc(sizeof(short) * icl->split_channel_count); |
| 82 | while (cchild && count < icl->split_channel_count) |
| 83 | { |
| 84 | if (cchild->type == STP_MXML_TEXT) |
| 85 | { |
| 86 | unsigned val = |
| 87 | strtoul(cchild->value.text.string, &endptr, 0); |
| 88 | if (endptr) |
| 89 | icl->split_channels[count++] = val; |
| 90 | } |
| 91 | cchild = cchild->next; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | child = child->next; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | load_channel(stp_mxml_node_t *node, stp_mxml_node_t *root, ink_channel_t *icl) |
| 102 | { |
| 103 | const char *name; |
| 104 | stp_mxml_node_t *child = node->child; |
| 105 | int count = 0; |
| 106 | while (child) |
| 107 | { |
| 108 | if (child->type == STP_MXML_ELEMENT && |
| 109 | !strcmp(child->value.element.name, "subchannel")) |
| 110 | count++; |
| 111 | child = child->next; |
| 112 | } |
| 113 | name = stp_mxmlElementGetAttr(node, "name"); |
| 114 | if (name) |
| 115 | icl->name = stp_strdup(name); |
| 116 | icl->n_subchannels = count; |
| 117 | icl->subchannels = stp_zalloc(sizeof(physical_subchannel_t) * count); |
| 118 | count = 0; |
| 119 | child = node->child; |
| 120 | while (child) |
| 121 | { |
| 122 | if (child->type == STP_MXML_ELEMENT) |
| 123 | { |
| 124 | if (!strcmp(child->value.element.name, "subchannel")) |
| 125 | load_subchannel(child, root, &(icl->subchannels[count++])); |
| 126 | else if (!strcmp(child->value.element.name, "HueCurve")) |
| 127 | { |
| 128 | stp_mxml_node_t *cchild = child->child; |
| 129 | stp_curve_t *curve; |
| 130 | const char *cref = stp_mxmlElementGetAttr(child, "ref"); |
| 131 | if (cref) |
| 132 | { |
| 133 | cchild = stp_mxmlFindElement(root, root, "curve", "name", |
| 134 | cref, STP_MXML_DESCEND); |
| 135 | STPI_ASSERT(cchild, NULL); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | while (cchild && cchild->type != STP_MXML_ELEMENT) |
| 140 | cchild = cchild->next; |
| 141 | STPI_ASSERT(cchild, NULL); |
| 142 | } |
| 143 | curve = stp_curve_create_from_xmltree(cchild); |
| 144 | icl->hue_curve = curve; |
| 145 | } |
| 146 | else if (!strcmp(child->value.element.name, "HueCurveParam")) |
| 147 | { |
| 148 | name = stp_mxmlElementGetAttr(child, "name"); |
| 149 | if (name) |
| 150 | icl->hue_curve_name = stp_strdup(name); |
| 151 | } |
| 152 | } |
| 153 | child = child->next; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static void |
| 158 | load_inkname(stp_mxml_node_t *node, stp_mxml_node_t *root, inkname_t *inl, |
| 159 | inklist_t *ikl) |
| 160 | { |
| 161 | const char *name; |
| 162 | stp_mxml_node_t *child = node->child; |
| 163 | unsigned int channel_count = 0; |
| 164 | unsigned int aux_channel_count = 0; |
| 165 | |
| 166 | while (child) |
| 167 | { |
| 168 | if (child->type == STP_MXML_ELEMENT) |
| 169 | { |
| 170 | if (!strcmp(child->value.element.name, "Channels")) |
| 171 | { |
| 172 | stp_mxml_node_t *cchild = child->child; |
| 173 | while (cchild) |
| 174 | { |
| 175 | if (cchild->type == STP_MXML_ELEMENT && |
| 176 | !strcmp(cchild->value.element.name, "channel")) |
| 177 | { |
| 178 | name = stp_mxmlElementGetAttr(cchild, "index"); |
| 179 | if (name) |
| 180 | { |
| 181 | unsigned idx = stp_xmlstrtoul(name); |
| 182 | if (idx + 1 > channel_count) |
| 183 | channel_count = idx + 1; |
| 184 | } |
| 185 | } |
| 186 | cchild = cchild->next; |
| 187 | } |
| 188 | } |
| 189 | else if (!strcmp(child->value.element.name, "AuxChannels")) |
| 190 | { |
| 191 | stp_mxml_node_t *cchild = child->child; |
| 192 | while (cchild) |
| 193 | { |
| 194 | if (cchild->type == STP_MXML_ELEMENT && |
| 195 | !strcmp(cchild->value.element.name, "channel")) |
| 196 | { |
| 197 | name = stp_mxmlElementGetAttr(cchild, "index"); |
| 198 | if (name) |
| 199 | { |
| 200 | unsigned idx = stp_xmlstrtoul(name); |
| 201 | if (idx + 1 > aux_channel_count) |
| 202 | aux_channel_count = idx + 1; |
| 203 | } |
| 204 | } |
| 205 | cchild = cchild->next; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | child = child->next; |
| 210 | } |
| 211 | inl->channel_count = channel_count; |
| 212 | if (channel_count > 0) |
| 213 | inl->channels = stp_zalloc(sizeof(ink_channel_t) * channel_count); |
| 214 | inl->aux_channel_count = aux_channel_count; |
| 215 | if (aux_channel_count > 0) |
| 216 | inl->aux_channels = stp_zalloc(sizeof(ink_channel_t) * aux_channel_count); |
| 217 | name = stp_mxmlElementGetAttr(node, "name"); |
| 218 | if (name) |
| 219 | inl->name = stp_strdup(name); |
| 220 | name = stp_mxmlElementGetAttr(node, "text"); |
| 221 | if (name) |
| 222 | inl->text = stp_strdup(name); |
| 223 | name = stp_mxmlElementGetAttr(node, "InkID"); |
| 224 | if (name) |
| 225 | { |
| 226 | if (!strcmp(name, "CMYK")) |
| 227 | inl->inkset = INKSET_CMYK; |
| 228 | else if (!strcmp(name, "CcMmYK")) |
| 229 | inl->inkset = INKSET_CcMmYK; |
| 230 | else if (!strcmp(name, "CcMmYyK")) |
| 231 | inl->inkset = INKSET_CcMmYyK; |
| 232 | else if (!strcmp(name, "CcMmYKk")) |
| 233 | inl->inkset = INKSET_CcMmYKk; |
| 234 | else if (!strcmp(name, "Quadtone")) |
| 235 | inl->inkset = INKSET_QUADTONE; |
| 236 | else if (!strcmp(name, "Hextone")) |
| 237 | inl->inkset = INKSET_HEXTONE; |
| 238 | else if (!strcmp(name, "OTHER")) |
| 239 | inl->inkset = INKSET_OTHER; |
| 240 | else if (!strcmp(name, "Extended")) |
| 241 | inl->inkset = INKSET_EXTENDED; |
| 242 | } |
| 243 | |
| 244 | channel_count = 0; |
| 245 | aux_channel_count = 0; |
| 246 | child = node->child; |
| 247 | while (child) |
| 248 | { |
| 249 | if (child->type == STP_MXML_ELEMENT) |
| 250 | { |
| 251 | if (!strcmp(child->value.element.name, "Channels")) |
| 252 | { |
| 253 | stp_mxml_node_t *cchild = child->child; |
| 254 | while (cchild) |
| 255 | { |
| 256 | if (cchild->type == STP_MXML_ELEMENT && |
| 257 | !strcmp(cchild->value.element.name, "channel")) |
| 258 | { |
| 259 | name = stp_mxmlElementGetAttr(cchild, "index"); |
| 260 | if (name) |
| 261 | { |
| 262 | unsigned idx = stp_xmlstrtoul(name); |
| 263 | load_channel(cchild, root, &(inl->channels[idx])); |
| 264 | } |
| 265 | } |
| 266 | cchild = cchild->next; |
| 267 | } |
| 268 | } |
| 269 | else if (!strcmp(child->value.element.name, "AuxChannels")) |
| 270 | { |
| 271 | stp_mxml_node_t *cchild = child->child; |
| 272 | while (cchild) |
| 273 | { |
| 274 | if (cchild->type == STP_MXML_ELEMENT && |
| 275 | !strcmp(cchild->value.element.name, "channel")) |
| 276 | { |
| 277 | name = stp_mxmlElementGetAttr(cchild, "index"); |
| 278 | if (name) |
| 279 | { |
| 280 | unsigned idx = stp_xmlstrtoul(name); |
| 281 | load_channel(cchild, root, &(inl->aux_channels[idx])); |
| 282 | } |
| 283 | } |
| 284 | cchild = cchild->next; |
| 285 | } |
| 286 | } |
| 287 | else if (!strcmp(child->value.element.name, "initSequence") && |
| 288 | child->child && child->child->type == STP_MXML_TEXT) |
| 289 | ikl->init_sequence = stp_xmlstrtoraw(child->child->value.text.string); |
| 290 | else if (!strcmp(child->value.element.name, "deinitSequence") && |
| 291 | child->child && child->child->type == STP_MXML_TEXT) |
| 292 | ikl->deinit_sequence = stp_xmlstrtoraw(child->child->value.text.string); |
| 293 | } |
| 294 | child = child->next; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | static void |
| 299 | load_shades(stp_mxml_node_t *node, stp_mxml_node_t *root, inklist_t *ikl) |
| 300 | { |
| 301 | stp_mxml_node_t *child = node->child; |
| 302 | int count = 0; |
| 303 | (void)root; |
| 304 | |
| 305 | while (child) |
| 306 | { |
| 307 | if (child->type == STP_MXML_ELEMENT && |
| 308 | !strcmp(child->value.element.name, "shade")) |
| 309 | count++; |
| 310 | child = child->next; |
| 311 | } |
| 312 | ikl->n_shades = count; |
| 313 | ikl->shades = stp_zalloc(sizeof(shade_t) * count); |
| 314 | count = 0; |
| 315 | child = node->child; |
| 316 | while (child) |
| 317 | { |
| 318 | if (child->type == STP_MXML_ELEMENT && |
| 319 | !strcmp(child->value.element.name, "shade")) |
| 320 | { |
| 321 | if (stp_mxmlElementGetAttr(child, "count")) |
| 322 | { |
| 323 | unsigned nshades = |
| 324 | stp_xmlstrtoul(stp_mxmlElementGetAttr(child, "count")); |
| 325 | ikl->shades[count].n_shades = nshades; |
| 326 | if (nshades > 0) |
| 327 | { |
| 328 | char *endptr; |
| 329 | stp_mxml_node_t *cchild = child->child; |
| 330 | ikl->shades[count].shades = stp_zalloc(sizeof(double) * nshades); |
| 331 | nshades = 0; |
| 332 | while (cchild && nshades < ikl->shades[count].n_shades) |
| 333 | { |
| 334 | if (cchild->type == STP_MXML_TEXT) |
| 335 | { |
| 336 | double val = |
| 337 | strtod(cchild->value.text.string, &endptr); |
| 338 | if (endptr) |
| 339 | ikl->shades[count].shades[nshades++] = val; |
| 340 | } |
| 341 | cchild = cchild->next; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | count++; |
| 346 | } |
| 347 | child = child->next; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | static void |
| 352 | load_inklist(stp_mxml_node_t *node, stp_mxml_node_t *root, inklist_t *ikl) |
| 353 | { |
| 354 | const char *name; |
| 355 | stp_mxml_node_t *child = node->child; |
| 356 | int count = 0; |
| 357 | |
| 358 | while (child) |
| 359 | { |
| 360 | if (child->type == STP_MXML_ELEMENT) |
| 361 | { |
| 362 | if (!strcmp(child->value.element.name, "InkName")) |
| 363 | count++; |
| 364 | else if (!strcmp(child->value.element.name, "initSequence") && |
| 365 | child->child && child->child->type == STP_MXML_TEXT) |
| 366 | ikl->init_sequence = stp_xmlstrtoraw(child->child->value.text.string); |
| 367 | else if (!strcmp(child->value.element.name, "deinitSequence") && |
| 368 | child->child && child->child->type == STP_MXML_TEXT) |
| 369 | ikl->deinit_sequence = stp_xmlstrtoraw(child->child->value.text.string); |
| 370 | } |
| 371 | child = child->next; |
| 372 | } |
| 373 | name = stp_mxmlElementGetAttr(node, "name"); |
| 374 | if (name) |
| 375 | ikl->name = stp_strdup(name); |
| 376 | name = stp_mxmlElementGetAttr(node, "text"); |
| 377 | if (name) |
| 378 | ikl->text = stp_strdup(name); |
| 379 | ikl->n_inks = count; |
| 380 | ikl->inknames = stp_zalloc(sizeof(inkname_t) * count); |
| 381 | count = 0; |
| 382 | child = node->child; |
| 383 | while (child) |
| 384 | { |
| 385 | if (child->type == STP_MXML_ELEMENT) |
| 386 | { |
| 387 | if (!strcmp(child->value.element.name, "InkName")) |
| 388 | { |
| 389 | inkname_t *inl = &(ikl->inknames[count++]); |
| 390 | inl->init_sequence = ikl->init_sequence; |
| 391 | inl->deinit_sequence = ikl->deinit_sequence; |
| 392 | load_inkname(child, root, inl, ikl); |
| 393 | } |
| 394 | else if (!strcmp(child->value.element.name, "Shades")) |
| 395 | load_shades(child, root, ikl); |
| 396 | } |
| 397 | child = child->next; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * This is structured differently from the other XML loaders because of |
| 403 | * the default black inkset |
| 404 | */ |
| 405 | static inkgroup_t * |
| 406 | load_inkgroup(const char *name) |
| 407 | { |
| 408 | inkgroup_t *igl = (inkgroup_t *) stp_refcache_find_item("escp2Inkgroup", name); |
| 409 | if (! igl) |
| 410 | { |
| 411 | stp_mxml_node_t *node = |
| 412 | stp_xml_parse_file_from_path_uncached_safe(name, "escp2InkGroup", NULL); |
| 413 | stp_mxml_node_t *child = node->child; |
| 414 | igl = stp_zalloc(sizeof(inkgroup_t)); |
| 415 | stp_refcache_add_item("escp2Inkgroup", name, igl); |
| 416 | size_t count = 0; |
| 417 | while (child) |
| 418 | { |
| 419 | if (child->type == STP_MXML_ELEMENT && |
| 420 | !strcmp(child->value.element.name, "InkList")) |
| 421 | count++; |
| 422 | child = child->next; |
| 423 | } |
| 424 | igl->n_inklists = count; |
| 425 | if (stp_mxmlElementGetAttr(node, "name")) |
| 426 | igl->name = stp_strdup(stp_mxmlElementGetAttr(node, "name")); |
| 427 | else |
| 428 | igl->name = stp_strdup(name); |
| 429 | igl->inklists = stp_zalloc(sizeof(inklist_t) * count); |
| 430 | child = node->child; |
| 431 | count = 0; |
| 432 | while (child) |
| 433 | { |
| 434 | if (child->type == STP_MXML_ELEMENT && |
| 435 | !strcmp(child->value.element.name, "InkList")) |
| 436 | load_inklist(child, node, &(igl->inklists[count++])); |
| 437 | child = child->next; |
| 438 | } |
| 439 | stp_xml_free_parsed_file(node); |
| 440 | } |
| 441 | return igl; |
| 442 | } |
| 443 | |
| 444 | int |
| 445 | stpi_escp2_load_inkgroup(const stp_vars_t *v, const char *name) |
| 446 | { |
| 447 | stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v); |
| 448 | inkgroup_t *igl = load_inkgroup(name); |
| 449 | printdef->inkgroup = igl; |
| 450 | return 1; |
| 451 | } |
| 452 | |
| 453 | const inkname_t * |
| 454 | stpi_escp2_get_default_black_inkset(void) |
| 455 | { |
| 456 | if (! default_black_inkgroup) |
| 457 | { |
| 458 | default_black_inkgroup = load_inkgroup("escp2/inks/defaultblack.xml"); |
| 459 | STPI_ASSERT(default_black_inkgroup && |
| 460 | default_black_inkgroup->n_inklists >= 1 && |
| 461 | default_black_inkgroup->inklists[0].n_inks >= 1, NULL); |
| 462 | } |
| 463 | return &(default_black_inkgroup->inklists[0].inknames[0]); |
| 464 | } |