| 1 | /* |
| 2 | * |
| 3 | * Print plug-in Adobe PostScript driver for the GIMP. |
| 4 | * |
| 5 | * Copyright 1997-2002 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 <time.h> |
| 34 | #include <string.h> |
| 35 | #include <math.h> |
| 36 | #ifdef HAVE_LIMITS_H |
| 37 | #include <limits.h> |
| 38 | #endif |
| 39 | #include <stdio.h> |
| 40 | #include <unistd.h> |
| 41 | #include <strings.h> |
| 42 | #include "xmlppd.h" |
| 43 | |
| 44 | #ifdef _MSC_VER |
| 45 | #define strncasecmp(s,t,n) _strnicmp(s,t,n) |
| 46 | #define strcasecmp(s,t) _stricmp(s,t) |
| 47 | #endif |
| 48 | |
| 49 | /* |
| 50 | * Local variables... |
| 51 | */ |
| 52 | |
| 53 | static char *m_ppd_file = NULL; |
| 54 | static stp_mxml_node_t *m_ppd = NULL; |
| 55 | |
| 56 | |
| 57 | /* |
| 58 | * Local functions... |
| 59 | */ |
| 60 | |
| 61 | static void ps_hex(const stp_vars_t *, unsigned short *, int); |
| 62 | static void ps_ascii85(const stp_vars_t *, unsigned short *, int, int); |
| 63 | |
| 64 | static const stp_parameter_t the_parameters[] = |
| 65 | { |
| 66 | { |
| 67 | "PPDFile", N_("PPDFile"), "Color=Yes,Category=Basic Printer Setup", |
| 68 | N_("PPD File"), |
| 69 | STP_PARAMETER_TYPE_FILE, STP_PARAMETER_CLASS_FEATURE, |
| 70 | STP_PARAMETER_LEVEL_BASIC, 1, 1, STP_CHANNEL_NONE, 1, 0 |
| 71 | }, |
| 72 | { |
| 73 | "PageSize", N_("Page Size"), "Color=No,Category=Basic Printer Setup", |
| 74 | N_("Size of the paper being printed to"), |
| 75 | STP_PARAMETER_TYPE_STRING_LIST, STP_PARAMETER_CLASS_CORE, |
| 76 | STP_PARAMETER_LEVEL_BASIC, 1, 1, STP_CHANNEL_NONE, 1, 0 |
| 77 | }, |
| 78 | { |
| 79 | "ModelName", N_("Model Name"), "Color=Yes,Category=Basic Printer Setup", |
| 80 | N_("PPD File Model Name"), |
| 81 | STP_PARAMETER_TYPE_STRING_LIST, STP_PARAMETER_CLASS_CORE, |
| 82 | STP_PARAMETER_LEVEL_INTERNAL, 0, 0, STP_CHANNEL_NONE, 0, 0 |
| 83 | }, |
| 84 | { |
| 85 | "PrintingMode", N_("Printing Mode"), "Color=Yes,Category=Core Parameter", |
| 86 | N_("Printing Output Mode"), |
| 87 | STP_PARAMETER_TYPE_STRING_LIST, STP_PARAMETER_CLASS_CORE, |
| 88 | STP_PARAMETER_LEVEL_BASIC, 1, 1, STP_CHANNEL_NONE, 1, 0 |
| 89 | }, |
| 90 | }; |
| 91 | |
| 92 | static const int the_parameter_count = |
| 93 | sizeof(the_parameters) / sizeof(const stp_parameter_t); |
| 94 | |
| 95 | static int |
| 96 | ps_option_to_param(const stp_vars_t *v, stp_parameter_t *param, |
| 97 | stp_mxml_node_t *option) |
| 98 | { |
| 99 | const char *group_text = stp_mxmlElementGetAttr(option, "grouptext"); |
| 100 | |
| 101 | if (group_text != NULL) |
| 102 | param->category = group_text; |
| 103 | else |
| 104 | param->category = NULL; |
| 105 | |
| 106 | param->text = stp_mxmlElementGetAttr(option, "text"); |
| 107 | param->help = stp_mxmlElementGetAttr(option, "text"); |
| 108 | if (stp_mxmlElementGetAttr(option, "stptype")) |
| 109 | { |
| 110 | const char *default_value = stp_mxmlElementGetAttr(option, "default"); |
| 111 | double stp_default_value = strtod(stp_mxmlElementGetAttr(option, "stpdefault"), 0); |
| 112 | double lower_bound = strtod(stp_mxmlElementGetAttr(option, "stplower"), NULL); |
| 113 | double upper_bound = strtod(stp_mxmlElementGetAttr(option, "stpupper"), NULL); |
| 114 | param->p_type = atoi(stp_mxmlElementGetAttr(option, "stptype")); |
| 115 | param->is_mandatory = atoi(stp_mxmlElementGetAttr(option, "stpmandatory")); |
| 116 | param->p_class = atoi(stp_mxmlElementGetAttr(option, "stpclass")); |
| 117 | param->p_level = atoi(stp_mxmlElementGetAttr(option, "stplevel")); |
| 118 | param->channel = (unsigned char) atoi(stp_mxmlElementGetAttr(option, "stpchannel")); |
| 119 | param->read_only = 0; |
| 120 | param->is_active = 1; |
| 121 | param->verify_this_parameter = 1; |
| 122 | param->name = stp_mxmlElementGetAttr(option, "stpname"); |
| 123 | stp_dprintf(STP_DBG_PS, v, |
| 124 | "Gutenprint parameter %s type %d mandatory %d class %d level %d channel %d default %s %f", |
| 125 | param->name, param->p_type, param->is_mandatory, |
| 126 | param->p_class, param->p_level, param->channel, |
| 127 | default_value, stp_default_value); |
| 128 | switch (param->p_type) |
| 129 | { |
| 130 | case STP_PARAMETER_TYPE_DOUBLE: |
| 131 | param->deflt.dbl = stp_default_value; |
| 132 | param->bounds.dbl.upper = upper_bound; |
| 133 | param->bounds.dbl.lower = lower_bound; |
| 134 | stp_dprintf(STP_DBG_PS, v, " %.3f %.3f %.3f\n", |
| 135 | param->deflt.dbl, param->bounds.dbl.upper, |
| 136 | param->bounds.dbl.lower); |
| 137 | break; |
| 138 | case STP_PARAMETER_TYPE_DIMENSION: |
| 139 | param->deflt.dimension = atoi(default_value); |
| 140 | param->bounds.dimension.upper = (stp_dimension_t) upper_bound; |
| 141 | param->bounds.dimension.lower = (stp_dimension_t) lower_bound; |
| 142 | stp_dprintf(STP_DBG_PS, v, " %f %f %f\n", |
| 143 | param->deflt.dimension, param->bounds.dimension.upper, |
| 144 | param->bounds.dimension.lower); |
| 145 | break; |
| 146 | case STP_PARAMETER_TYPE_INT: |
| 147 | param->deflt.integer = atoi(default_value); |
| 148 | param->bounds.integer.upper = (int) upper_bound; |
| 149 | param->bounds.integer.lower = (int) lower_bound; |
| 150 | stp_dprintf(STP_DBG_PS, v, " %d %d %d\n", |
| 151 | param->deflt.integer, param->bounds.integer.upper, |
| 152 | param->bounds.integer.lower); |
| 153 | break; |
| 154 | case STP_PARAMETER_TYPE_BOOLEAN: |
| 155 | param->deflt.boolean = strcasecmp(default_value, "true") == 0 ? 1 : 0; |
| 156 | stp_dprintf(STP_DBG_PS, v, " %d\n", param->deflt.boolean); |
| 157 | break; |
| 158 | default: |
| 159 | stp_dprintf(STP_DBG_PS, v, "\n"); |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | const char *ui = stp_mxmlElementGetAttr(option, "ui"); |
| 166 | param->name = stp_mxmlElementGetAttr(option, "name"); |
| 167 | if (strcasecmp(ui, "Boolean") == 0) |
| 168 | param->p_type = STP_PARAMETER_TYPE_BOOLEAN; |
| 169 | else |
| 170 | param->p_type = STP_PARAMETER_TYPE_STRING_LIST; |
| 171 | if (strcmp(param->name, "PageSize") == 0) |
| 172 | param->p_class = STP_PARAMETER_CLASS_CORE; |
| 173 | else |
| 174 | param->p_class = STP_PARAMETER_CLASS_FEATURE; |
| 175 | param->p_level = STP_PARAMETER_LEVEL_BASIC; |
| 176 | param->is_mandatory = 1; |
| 177 | param->is_active = 1; |
| 178 | param->channel = -1; |
| 179 | param->verify_this_parameter = 1; |
| 180 | param->read_only = 0; |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * 'ps_parameters()' - Return the parameter values for the given parameter. |
| 188 | */ |
| 189 | |
| 190 | static int |
| 191 | ppd_whitespace_callback(stp_mxml_node_t *node, int where) |
| 192 | { |
| 193 | (void)node; |
| 194 | (void)where; |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static int |
| 199 | check_ppd_file(const stp_vars_t *v) |
| 200 | { |
| 201 | const char *ppd_file = stp_get_file_parameter(v, "PPDFile"); |
| 202 | |
| 203 | if (ppd_file == NULL || ppd_file[0] == 0) |
| 204 | { |
| 205 | stp_dprintf(STP_DBG_PS, v, "Empty PPD file\n"); |
| 206 | return 0; |
| 207 | } |
| 208 | else if (m_ppd_file && strcmp(m_ppd_file, ppd_file) == 0) |
| 209 | { |
| 210 | stp_dprintf(STP_DBG_PS, v, "Not replacing PPD file %s\n", m_ppd_file); |
| 211 | return 1; |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | stp_dprintf(STP_DBG_PS, v, "Replacing PPD file %s with %s\n", |
| 216 | m_ppd_file ? m_ppd_file : "(null)", |
| 217 | ppd_file ? ppd_file : "(null)"); |
| 218 | if (m_ppd != NULL) |
| 219 | stp_mxmlDelete(m_ppd); |
| 220 | m_ppd = NULL; |
| 221 | |
| 222 | if (m_ppd_file) |
| 223 | stp_free(m_ppd_file); |
| 224 | m_ppd_file = NULL; |
| 225 | |
| 226 | if ((m_ppd = stpi_xmlppd_read_ppd_file(ppd_file)) == NULL) |
| 227 | { |
| 228 | stp_eprintf(v, "Unable to open PPD file %s\n", ppd_file); |
| 229 | return 0; |
| 230 | } |
| 231 | if (stp_get_debug_level() & STP_DBG_PS) |
| 232 | { |
| 233 | char *ppd_stuff = stp_mxmlSaveAllocString(m_ppd, ppd_whitespace_callback); |
| 234 | stp_dprintf(STP_DBG_PS, v, "%s", ppd_stuff); |
| 235 | stp_free(ppd_stuff); |
| 236 | } |
| 237 | |
| 238 | m_ppd_file = stp_strdup(ppd_file); |
| 239 | return 1; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | static stp_parameter_list_t |
| 245 | ps_list_parameters(const stp_vars_t *v) |
| 246 | { |
| 247 | stp_parameter_list_t *ret = stp_parameter_list_create(); |
| 248 | stp_mxml_node_t *option; |
| 249 | int i; |
| 250 | int status = check_ppd_file(v); |
| 251 | stp_dprintf(STP_DBG_PS, v, "Adding parameters from %s (%d)\n", |
| 252 | m_ppd_file ? m_ppd_file : "(null)", status); |
| 253 | |
| 254 | for (i = 0; i < the_parameter_count; i++) |
| 255 | stp_parameter_list_add_param(ret, &(the_parameters[i])); |
| 256 | |
| 257 | if (status) |
| 258 | { |
| 259 | int num_options = stpi_xmlppd_find_option_count(m_ppd); |
| 260 | stp_dprintf(STP_DBG_PS, v, "Found %d parameters\n", num_options); |
| 261 | for (i=0; i < num_options; i++) |
| 262 | { |
| 263 | /* MEMORY LEAK!!! */ |
| 264 | stp_parameter_t *param = stp_malloc(sizeof(stp_parameter_t)); |
| 265 | option = stpi_xmlppd_find_option_index(m_ppd, i); |
| 266 | if (option) |
| 267 | { |
| 268 | ps_option_to_param(v, param, option); |
| 269 | if (param->p_type != STP_PARAMETER_TYPE_INVALID && |
| 270 | strcmp(param->name, "PageRegion") != 0 && |
| 271 | strcmp(param->name, "PageSize") != 0) |
| 272 | { |
| 273 | stp_dprintf(STP_DBG_PS, v, "Adding parameter %s %s\n", |
| 274 | param->name, param->text); |
| 275 | stp_parameter_list_add_param(ret, param); |
| 276 | } |
| 277 | else |
| 278 | stp_free(param); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | static void |
| 286 | ps_parameters_internal(const stp_vars_t *v, const char *name, |
| 287 | stp_parameter_t *description) |
| 288 | { |
| 289 | unsigned int i; |
| 290 | stp_mxml_node_t *option; |
| 291 | int status = 0; |
| 292 | unsigned int num_choices; |
| 293 | const char *defchoice; |
| 294 | |
| 295 | description->p_type = STP_PARAMETER_TYPE_INVALID; |
| 296 | description->deflt.str = 0; |
| 297 | description->is_active = 0; |
| 298 | |
| 299 | if (name == NULL) |
| 300 | return; |
| 301 | |
| 302 | status = check_ppd_file(v); |
| 303 | |
| 304 | for (i = 0; i < the_parameter_count; i++) |
| 305 | { |
| 306 | if (strcmp(name, the_parameters[i].name) == 0) |
| 307 | { |
| 308 | stp_fill_parameter_settings(description, &(the_parameters[i])); |
| 309 | if (strcmp(name, "PPDFile") == 0) |
| 310 | description->is_active = 1; |
| 311 | else if (strcmp(name, "ModelName") == 0) |
| 312 | { |
| 313 | const char *nickname; |
| 314 | description->bounds.str = stp_string_list_create(); |
| 315 | if (m_ppd && stp_mxmlElementGetAttr(m_ppd, "nickname")) |
| 316 | nickname = stp_mxmlElementGetAttr(m_ppd, "nickname"); |
| 317 | else |
| 318 | nickname = _("None; please provide a PPD file"); |
| 319 | stp_string_list_add_string_unsafe(description->bounds.str, |
| 320 | nickname, nickname); |
| 321 | description->deflt.str = nickname; |
| 322 | description->is_active = 1; |
| 323 | return; |
| 324 | } |
| 325 | else if (strcmp(name, "PrintingMode") == 0) |
| 326 | { |
| 327 | if (! m_ppd || strcmp(stp_mxmlElementGetAttr(m_ppd, "color"), "1") == 0) |
| 328 | { |
| 329 | description->bounds.str = stp_string_list_create(); |
| 330 | stp_string_list_add_string |
| 331 | (description->bounds.str, "Color", _("Color")); |
| 332 | stp_string_list_add_string |
| 333 | (description->bounds.str, "BW", _("Black and White")); |
| 334 | description->deflt.str = |
| 335 | stp_string_list_param(description->bounds.str, 0)->name; |
| 336 | description->is_active = 1; |
| 337 | } |
| 338 | else |
| 339 | description->is_active = 0; |
| 340 | return; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (!status && strcmp(name, "PageSize") != 0) |
| 346 | return; |
| 347 | if ((option = stpi_xmlppd_find_option_named(m_ppd, name)) == NULL) |
| 348 | { |
| 349 | if (strcmp(name, "PageSize") == 0) |
| 350 | { |
| 351 | /* Provide a default set of page sizes */ |
| 352 | description->bounds.str = stp_string_list_create(); |
| 353 | stp_string_list_add_string |
| 354 | (description->bounds.str, "Letter", _("Letter")); |
| 355 | stp_string_list_add_string |
| 356 | (description->bounds.str, "A4", _("A4")); |
| 357 | stp_string_list_add_string |
| 358 | (description->bounds.str, "Custom", _("Custom")); |
| 359 | description->deflt.str = |
| 360 | stp_string_list_param(description->bounds.str, 0)->name; |
| 361 | description->is_active = 1; |
| 362 | return; |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | int tmplen = strlen(name) + 3; |
| 367 | char *tmp = stp_malloc(tmplen + 1); |
| 368 | strcpy(tmp, "Stp"); |
| 369 | strncat(tmp, name, tmplen); |
| 370 | if ((option = stpi_xmlppd_find_option_named(m_ppd, tmp)) == NULL) |
| 371 | { |
| 372 | stp_dprintf(STP_DBG_PS, v, "no parameter %s", name); |
| 373 | stp_free(tmp); |
| 374 | return; |
| 375 | } |
| 376 | stp_free(tmp); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | ps_option_to_param(v, description, option); |
| 381 | if (description->p_type != STP_PARAMETER_TYPE_STRING_LIST) |
| 382 | return; |
| 383 | num_choices = atoi(stp_mxmlElementGetAttr(option, "num_choices")); |
| 384 | defchoice = stp_mxmlElementGetAttr(option, "default"); |
| 385 | description->bounds.str = stp_string_list_create(); |
| 386 | |
| 387 | stp_dprintf(STP_DBG_PS, v, "describe parameter %s, output name=[%s] text=[%s] category=[%s] choices=[%d] default=[%s]\n", |
| 388 | name, description->name, description->text, |
| 389 | description->category, num_choices, defchoice); |
| 390 | |
| 391 | /* Describe all choices for specified option. */ |
| 392 | for (i=0; i < num_choices; i++) |
| 393 | { |
| 394 | stp_mxml_node_t *choice = stpi_xmlppd_find_choice_index(option, i); |
| 395 | const char *choice_name = stp_mxmlElementGetAttr(choice, "name"); |
| 396 | const char *choice_text = stp_mxmlElementGetAttr(choice, "text"); |
| 397 | stp_string_list_add_string(description->bounds.str, choice_name, choice_text); |
| 398 | stp_dprintf(STP_DBG_PS, v, " parameter %s, choice %d [%s] [%s]", |
| 399 | name, i, choice_name, choice_text); |
| 400 | if (strcmp(choice_name, defchoice) == 0) |
| 401 | { |
| 402 | stp_dprintf(STP_DBG_PS, v, |
| 403 | " parameter %s, choice %d [%s] DEFAULT\n", |
| 404 | name, i, choice_name); |
| 405 | description->deflt.str = choice_name; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if (!description->deflt.str) |
| 410 | { |
| 411 | stp_dprintf(STP_DBG_PS, v, |
| 412 | " parameter %s, defaulting to [%s]", |
| 413 | name, stp_string_list_param(description->bounds.str, 0)->name); |
| 414 | description->deflt.str = stp_string_list_param(description->bounds.str, 0)->name; |
| 415 | } |
| 416 | if (stp_string_list_count(description->bounds.str) > 0) |
| 417 | description->is_active = 1; |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | static void |
| 422 | ps_parameters(const stp_vars_t *v, const char *name, |
| 423 | stp_parameter_t *description) |
| 424 | { |
| 425 | #ifdef HAVE_LOCALE_H |
| 426 | char *locale = stp_strdup(setlocale(LC_ALL, NULL)); |
| 427 | setlocale(LC_ALL, "C"); |
| 428 | #endif |
| 429 | ps_parameters_internal(v, name, description); |
| 430 | #ifdef HAVE_LOCALE_H |
| 431 | setlocale(LC_ALL, locale); |
| 432 | stp_free(locale); |
| 433 | #endif |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * 'ps_media_size()' - Return the size of the page. |
| 438 | */ |
| 439 | |
| 440 | static void |
| 441 | ps_media_size_internal(const stp_vars_t *v, /* I */ |
| 442 | stp_dimension_t *width, /* O - Width in points */ |
| 443 | stp_dimension_t *height) /* O - Height in points */ |
| 444 | { |
| 445 | const char *pagesize = stp_get_string_parameter(v, "PageSize"); |
| 446 | int status = check_ppd_file(v); |
| 447 | if (!pagesize) |
| 448 | pagesize = ""; |
| 449 | |
| 450 | stp_dprintf(STP_DBG_PS, v, |
| 451 | "ps_media_size(%d, \'%s\', \'%s\', %p, %p)\n", |
| 452 | stp_get_model_id(v), m_ppd_file, pagesize, |
| 453 | (void *) width, (void *) height); |
| 454 | |
| 455 | stp_default_media_size(v, width, height); |
| 456 | |
| 457 | if (status) |
| 458 | { |
| 459 | stp_mxml_node_t *paper = stpi_xmlppd_find_page_size(m_ppd, pagesize); |
| 460 | if (paper) |
| 461 | { |
| 462 | *width = atoi(stp_mxmlElementGetAttr(paper, "width")); |
| 463 | *height = atoi(stp_mxmlElementGetAttr(paper, "height")); |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | *width = 0; |
| 468 | *height = 0; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | stp_dprintf(STP_DBG_PS, v, "dimensions %f %f\n", *width, *height); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | static const stp_papersize_t * |
| 477 | ps_describe_papersize(const stp_vars_t *v, const char *name) |
| 478 | { |
| 479 | int status = check_ppd_file(v); |
| 480 | if (status) |
| 481 | { |
| 482 | stp_mxml_node_t *paper = stpi_xmlppd_find_page_size(m_ppd, name); |
| 483 | if (paper) |
| 484 | { |
| 485 | const char *papersize_list_name = m_ppd_file ? m_ppd_file : "NOPPD"; |
| 486 | stp_papersize_list_t *ourlist = |
| 487 | stpi_find_papersize_list_named(papersize_list_name); |
| 488 | const stp_papersize_t *papersize; |
| 489 | const stp_papersize_t *standard_papersize = |
| 490 | stpi_get_listed_papersize(name, "standard"); |
| 491 | |
| 492 | if (! ourlist) |
| 493 | ourlist = stpi_new_papersize_list(papersize_list_name); |
| 494 | |
| 495 | papersize = stpi_get_papersize_by_name(ourlist, name); |
| 496 | if (! papersize) |
| 497 | { |
| 498 | stp_papersize_t *npapersize = stp_malloc(sizeof(stp_papersize_t)); |
| 499 | npapersize->name = stp_strdup(name); |
| 500 | npapersize->text = stp_strdup(name); |
| 501 | npapersize->comment = NULL; |
| 502 | /* |
| 503 | * Note that we used the width and height from the PPD file, |
| 504 | * not from the standard definition. This is so that if the |
| 505 | * PPD file is for another driver that uses slightly different |
| 506 | * dimensions than we do that our description matches that of |
| 507 | * driver in use. |
| 508 | */ |
| 509 | npapersize->width = atof(stp_mxmlElementGetAttr(paper, "width")); |
| 510 | npapersize->height = atof(stp_mxmlElementGetAttr(paper, "height")); |
| 511 | /* |
| 512 | * Only use auxiliary information from our list if our paper size |
| 513 | * really is substantially the same as what the PPD file says! |
| 514 | */ |
| 515 | if (standard_papersize && |
| 516 | fabs(npapersize->width - standard_papersize->width) < 1 && |
| 517 | fabs(npapersize->height - standard_papersize->height) < 1) |
| 518 | { |
| 519 | npapersize->paper_unit = standard_papersize->paper_unit; |
| 520 | npapersize->paper_size_type = standard_papersize->paper_size_type; |
| 521 | npapersize->top = standard_papersize->top; |
| 522 | npapersize->left = standard_papersize->left; |
| 523 | npapersize->bottom = standard_papersize->bottom; |
| 524 | npapersize->right = standard_papersize->right; |
| 525 | } |
| 526 | else |
| 527 | { |
| 528 | npapersize->top = 0; |
| 529 | npapersize->left = 0; |
| 530 | npapersize->bottom = 0; |
| 531 | npapersize->right = 0; |
| 532 | npapersize->paper_unit = PAPERSIZE_ENGLISH_STANDARD; |
| 533 | npapersize->paper_size_type = PAPERSIZE_TYPE_STANDARD; |
| 534 | } |
| 535 | if (stpi_papersize_create(ourlist, npapersize)) |
| 536 | return npapersize; |
| 537 | } |
| 538 | return papersize; |
| 539 | } |
| 540 | } |
| 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | static void |
| 545 | ps_media_size(const stp_vars_t *v, stp_dimension_t *width, stp_dimension_t *height) |
| 546 | { |
| 547 | #ifdef HAVE_LOCALE_H |
| 548 | char *locale = stp_strdup(setlocale(LC_ALL, NULL)); |
| 549 | setlocale(LC_ALL, "C"); |
| 550 | #endif |
| 551 | ps_media_size_internal(v, width, height); |
| 552 | #ifdef HAVE_LOCALE_H |
| 553 | setlocale(LC_ALL, locale); |
| 554 | stp_free(locale); |
| 555 | #endif |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * 'ps_imageable_area()' - Return the imageable area of the page. |
| 560 | */ |
| 561 | |
| 562 | static void |
| 563 | ps_imageable_area_internal(const stp_vars_t *v, /* I */ |
| 564 | int use_max_area, /* I - Use maximum area */ |
| 565 | stp_dimension_t *left, /* O - Left position in points */ |
| 566 | stp_dimension_t *right, /* O - Right position in points */ |
| 567 | stp_dimension_t *bottom, /* O - Bottom position in points */ |
| 568 | stp_dimension_t *top) /* O - Top position in points */ |
| 569 | { |
| 570 | stp_dimension_t width, height; |
| 571 | const char *pagesize = stp_get_string_parameter(v, "PageSize"); |
| 572 | if (!pagesize) |
| 573 | pagesize = ""; |
| 574 | |
| 575 | /* Set some defaults. */ |
| 576 | ps_media_size_internal(v, &width, &height); |
| 577 | *left = 0; |
| 578 | *right = width; |
| 579 | *top = 0; |
| 580 | *bottom = height; |
| 581 | |
| 582 | if (check_ppd_file(v)) |
| 583 | { |
| 584 | stp_mxml_node_t *paper = stpi_xmlppd_find_page_size(m_ppd, pagesize); |
| 585 | if (paper) |
| 586 | { |
| 587 | double pleft = atoi(stp_mxmlElementGetAttr(paper, "left")); |
| 588 | double pright = atoi(stp_mxmlElementGetAttr(paper, "right")); |
| 589 | double ptop = atoi(stp_mxmlElementGetAttr(paper, "top")); |
| 590 | double pbottom = atoi(stp_mxmlElementGetAttr(paper, "bottom")); |
| 591 | stp_dprintf(STP_DBG_PS, v, "size=l %f r %f b %f t %f h %f w %f\n", |
| 592 | pleft, pright, pbottom, ptop, height, width); |
| 593 | *left = (stp_dimension_t) pleft; |
| 594 | *right = (stp_dimension_t) pright; |
| 595 | *top = height - (stp_dimension_t) ptop; |
| 596 | *bottom = height - (stp_dimension_t) pbottom; |
| 597 | stp_dprintf(STP_DBG_PS, v, ">>>> l %f r %f b %f t %f h %f w %f\n", |
| 598 | *left, *right, *bottom, *top, height, width); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | if (use_max_area) |
| 603 | { |
| 604 | if (*left > 0) |
| 605 | *left = 0; |
| 606 | if (*right < width) |
| 607 | *right = width; |
| 608 | if (*top > 0) |
| 609 | *top = 0; |
| 610 | if (*bottom < height) |
| 611 | *bottom = height; |
| 612 | } |
| 613 | |
| 614 | stp_dprintf(STP_DBG_PS, v, "pagesize %s max_area=%d l %f r %f b %f t %f h %f w %f\n", |
| 615 | pagesize ? pagesize : "(null)", |
| 616 | use_max_area, *left, *right, *bottom, *top, width, height); |
| 617 | |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | static void |
| 622 | ps_imageable_area(const stp_vars_t *v, /* I */ |
| 623 | stp_dimension_t *left, /* O - Left position in points */ |
| 624 | stp_dimension_t *right, /* O - Right position in points */ |
| 625 | stp_dimension_t *bottom, /* O - Bottom position in points */ |
| 626 | stp_dimension_t *top) /* O - Top position in points */ |
| 627 | { |
| 628 | #ifdef HAVE_LOCALE_H |
| 629 | char *locale = stp_strdup(setlocale(LC_ALL, NULL)); |
| 630 | setlocale(LC_ALL, "C"); |
| 631 | #endif |
| 632 | ps_imageable_area_internal(v, 0, left, right, bottom, top); |
| 633 | #ifdef HAVE_LOCALE_H |
| 634 | setlocale(LC_ALL, locale); |
| 635 | stp_free(locale); |
| 636 | #endif |
| 637 | } |
| 638 | |
| 639 | static void |
| 640 | ps_maximum_imageable_area(const stp_vars_t *v, /* I */ |
| 641 | stp_dimension_t *left, /* O - Left position in points */ |
| 642 | stp_dimension_t *right, /* O - Right position in points */ |
| 643 | stp_dimension_t *bottom, /* O - Bottom position in points */ |
| 644 | stp_dimension_t *top) /* O - Top position in points */ |
| 645 | { |
| 646 | #ifdef HAVE_LOCALE_H |
| 647 | char *locale = stp_strdup(setlocale(LC_ALL, NULL)); |
| 648 | setlocale(LC_ALL, "C"); |
| 649 | #endif |
| 650 | ps_imageable_area_internal(v, 1, left, right, bottom, top); |
| 651 | #ifdef HAVE_LOCALE_H |
| 652 | setlocale(LC_ALL, locale); |
| 653 | stp_free(locale); |
| 654 | #endif |
| 655 | } |
| 656 | |
| 657 | static void |
| 658 | ps_limit(const stp_vars_t *v, /* I */ |
| 659 | stp_dimension_t *width, |
| 660 | stp_dimension_t *height, |
| 661 | stp_dimension_t *min_width, |
| 662 | stp_dimension_t *min_height) |
| 663 | { |
| 664 | (void)v; |
| 665 | *width = (stp_dimension_t) INT_MAX; |
| 666 | *height = (stp_dimension_t) INT_MAX; |
| 667 | *min_width = 1; |
| 668 | *min_height = 1; |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * This is really bogus... |
| 673 | */ |
| 674 | static void |
| 675 | ps_describe_resolution_internal(const stp_vars_t *v, stp_resolution_t *x, stp_resolution_t *y) |
| 676 | { |
| 677 | const char *resolution = stp_get_string_parameter(v, "Resolution"); |
| 678 | *x = -1; |
| 679 | *y = -1; |
| 680 | if (resolution) |
| 681 | { |
| 682 | int tx = -1; |
| 683 | int ty = -1; |
| 684 | sscanf(resolution, "%dx%d", &tx, &ty); |
| 685 | *x = (stp_resolution_t) tx; |
| 686 | *y = (stp_resolution_t) ty; |
| 687 | } |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | static void |
| 692 | ps_describe_resolution(const stp_vars_t *v, stp_resolution_t *x, stp_resolution_t *y) |
| 693 | { |
| 694 | #ifdef HAVE_LOCALE_H |
| 695 | char *locale = stp_strdup(setlocale(LC_ALL, NULL)); |
| 696 | setlocale(LC_ALL, "C"); |
| 697 | #endif |
| 698 | ps_describe_resolution_internal(v, x, y); |
| 699 | #ifdef HAVE_LOCALE_H |
| 700 | setlocale(LC_ALL, locale); |
| 701 | stp_free(locale); |
| 702 | #endif |
| 703 | } |
| 704 | |
| 705 | static const char * |
| 706 | ps_describe_output(const stp_vars_t *v) |
| 707 | { |
| 708 | const char *print_mode = stp_get_string_parameter(v, "PrintingMode"); |
| 709 | const char *input_image_type = stp_get_string_parameter(v, "InputImageType"); |
| 710 | if (print_mode && strcmp(print_mode, "Color") == 0) |
| 711 | { |
| 712 | if (input_image_type && (strcmp(input_image_type, "CMYK") == 0 || |
| 713 | strcmp(input_image_type, "KCMY") == 0)) |
| 714 | return "CMYK"; |
| 715 | else |
| 716 | return "RGB"; |
| 717 | } |
| 718 | else |
| 719 | return "Whitescale"; |
| 720 | } |
| 721 | |
| 722 | static stp_string_list_t * |
| 723 | ps_external_options(const stp_vars_t *v) |
| 724 | { |
| 725 | stp_parameter_list_t param_list = ps_list_parameters(v); |
| 726 | stp_string_list_t *answer; |
| 727 | char *tmp; |
| 728 | char *ppd_name = NULL; |
| 729 | unsigned int i; |
| 730 | #ifdef HAVE_LOCALE_H |
| 731 | char *locale; |
| 732 | #endif |
| 733 | if (! param_list) |
| 734 | return NULL; |
| 735 | answer = stp_string_list_create(); |
| 736 | #ifdef HAVE_LOCALE_H |
| 737 | locale = stp_strdup(setlocale(LC_ALL, NULL)); |
| 738 | setlocale(LC_ALL, "C"); |
| 739 | #endif |
| 740 | for (i = 0; i < stp_parameter_list_count(param_list); i++) |
| 741 | { |
| 742 | const stp_parameter_t *param = stp_parameter_list_param(param_list, i); |
| 743 | stp_parameter_t desc; |
| 744 | stp_describe_parameter(v, param->name, &desc); |
| 745 | if (desc.is_active) |
| 746 | { |
| 747 | stp_mxml_node_t *option; |
| 748 | if (m_ppd && |
| 749 | (option = stpi_xmlppd_find_option_named(m_ppd, desc.name)) == NULL) |
| 750 | { |
| 751 | int tmplen = strlen(desc.name) + 3; |
| 752 | ppd_name = stp_malloc(tmplen + 1); |
| 753 | strcpy(ppd_name, "Stp"); |
| 754 | strncat(ppd_name, desc.name, tmplen); |
| 755 | if ((option = stpi_xmlppd_find_option_named(m_ppd, ppd_name)) == NULL) |
| 756 | { |
| 757 | stp_dprintf(STP_DBG_PS, v, "no parameter %s", desc.name); |
| 758 | STP_SAFE_FREE(ppd_name); |
| 759 | } |
| 760 | } |
| 761 | switch (desc.p_type) |
| 762 | { |
| 763 | case STP_PARAMETER_TYPE_STRING_LIST: |
| 764 | if (stp_get_string_parameter(v, desc.name) && |
| 765 | strcmp(stp_get_string_parameter(v, desc.name), |
| 766 | desc.deflt.str)) |
| 767 | { |
| 768 | stp_dprintf(STP_DBG_PS, v, "Adding string parameter %s (%s): %s %s\n", |
| 769 | desc.name, ppd_name ? ppd_name : "(null)", |
| 770 | stp_get_string_parameter(v, desc.name), |
| 771 | desc.deflt.str); |
| 772 | stp_string_list_add_string(answer, |
| 773 | ppd_name ? ppd_name : desc.name, |
| 774 | stp_get_string_parameter(v, desc.name)); |
| 775 | } |
| 776 | break; |
| 777 | case STP_PARAMETER_TYPE_INT: |
| 778 | if (stp_get_int_parameter(v, desc.name) != desc.deflt.integer) |
| 779 | { |
| 780 | stp_dprintf(STP_DBG_PS, v, "Adding integer parameter %s (%s): %d %d\n", |
| 781 | desc.name, ppd_name ? ppd_name : "(null)", |
| 782 | stp_get_int_parameter(v, desc.name), |
| 783 | desc.deflt.integer); |
| 784 | stp_asprintf(&tmp, "%d", stp_get_int_parameter(v, desc.name)); |
| 785 | stp_string_list_add_string(answer, |
| 786 | ppd_name ? ppd_name : desc.name, |
| 787 | tmp); |
| 788 | stp_free(tmp); |
| 789 | } |
| 790 | break; |
| 791 | case STP_PARAMETER_TYPE_BOOLEAN: |
| 792 | if (stp_get_boolean_parameter(v, desc.name) != desc.deflt.boolean) |
| 793 | { |
| 794 | stp_dprintf(STP_DBG_PS, v, "Adding boolean parameter %s (%s): %d %d\n", |
| 795 | desc.name, ppd_name ? ppd_name : "(null)", |
| 796 | stp_get_boolean_parameter(v, desc.name), |
| 797 | desc.deflt.boolean); |
| 798 | stp_asprintf(&tmp, "%s", |
| 799 | stp_get_boolean_parameter(v, desc.name) ? |
| 800 | "True" : "False"); |
| 801 | stp_string_list_add_string(answer, |
| 802 | ppd_name ? ppd_name : desc.name, |
| 803 | tmp); |
| 804 | stp_free(tmp); |
| 805 | } |
| 806 | break; |
| 807 | case STP_PARAMETER_TYPE_DOUBLE: |
| 808 | if (fabs(stp_get_float_parameter(v, desc.name) - desc.deflt.dbl) > .00001) |
| 809 | { |
| 810 | stp_dprintf(STP_DBG_PS, v, "Adding float parameter %s (%s): %.3f %.3f\n", |
| 811 | desc.name, ppd_name ? ppd_name : "(null)", |
| 812 | stp_get_float_parameter(v, desc.name), |
| 813 | desc.deflt.dbl); |
| 814 | stp_asprintf(&tmp, "%.3f", |
| 815 | stp_get_float_parameter(v, desc.name)); |
| 816 | stp_string_list_add_string(answer, |
| 817 | ppd_name ? ppd_name : desc.name, |
| 818 | tmp); |
| 819 | stp_free(tmp); |
| 820 | } |
| 821 | break; |
| 822 | case STP_PARAMETER_TYPE_DIMENSION: |
| 823 | if (stp_get_dimension_parameter(v, desc.name) != |
| 824 | desc.deflt.dimension) |
| 825 | { |
| 826 | stp_dprintf(STP_DBG_PS, v, "Adding dimension parameter %s (%s): %f %f\n", |
| 827 | desc.name, ppd_name ? ppd_name : "(null)", |
| 828 | stp_get_dimension_parameter(v, desc.name), |
| 829 | desc.deflt.dimension); |
| 830 | stp_asprintf(&tmp, "%f", |
| 831 | stp_get_dimension_parameter(v, desc.name)); |
| 832 | stp_string_list_add_string(answer, |
| 833 | ppd_name ? ppd_name : desc.name, |
| 834 | tmp); |
| 835 | stp_free(tmp); |
| 836 | } |
| 837 | break; |
| 838 | default: |
| 839 | break; |
| 840 | } |
| 841 | STP_SAFE_FREE(ppd_name); |
| 842 | } |
| 843 | stp_parameter_description_destroy(&desc); |
| 844 | } |
| 845 | #ifdef HAVE_LOCALE_H |
| 846 | setlocale(LC_ALL, locale); |
| 847 | stp_free(locale); |
| 848 | #endif |
| 849 | return answer; |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * 'ps_print_device_settings()' - output postscript code from PPD into the |
| 854 | * postscript stream. |
| 855 | */ |
| 856 | |
| 857 | static void |
| 858 | ps_print_device_settings(stp_vars_t *v) |
| 859 | { |
| 860 | unsigned int i; |
| 861 | stp_parameter_list_t param_list = ps_list_parameters(v); |
| 862 | if (! param_list) |
| 863 | return; |
| 864 | stp_puts("%%BeginSetup\n", v); |
| 865 | for (i = 0; i < stp_parameter_list_count(param_list); i++) |
| 866 | { |
| 867 | const stp_parameter_t *param = stp_parameter_list_param(param_list, i); |
| 868 | stp_parameter_t desc; |
| 869 | stp_describe_parameter(v, param->name, &desc); |
| 870 | if (desc.is_active) |
| 871 | { |
| 872 | switch (desc.p_type) |
| 873 | { |
| 874 | case STP_PARAMETER_TYPE_STRING_LIST: |
| 875 | case STP_PARAMETER_TYPE_BOOLEAN: |
| 876 | { |
| 877 | const char *val=NULL; |
| 878 | const char *defval=NULL; |
| 879 | |
| 880 | /* If this is a bool parameter, set val to "True" or "False" - otherwise fetch from string parameter. */ |
| 881 | if(desc.p_type==STP_PARAMETER_TYPE_BOOLEAN) |
| 882 | { |
| 883 | val=stp_get_boolean_parameter(v,desc.name) ? "True" : "False"; |
| 884 | defval=desc.deflt.boolean ? "True" : "False"; |
| 885 | } |
| 886 | else |
| 887 | { |
| 888 | val=stp_get_string_parameter(v,desc.name); |
| 889 | defval=desc.deflt.str; |
| 890 | } |
| 891 | |
| 892 | /* We only include the option's code if it's set to a value other than the default. */ |
| 893 | if(val && defval && (strcmp(val,defval)!=0)) |
| 894 | { |
| 895 | if(m_ppd) |
| 896 | { |
| 897 | /* If we have a PPD xml tree we hunt for the appropriate "option" and "choice"... */ |
| 898 | stp_mxml_node_t *node=m_ppd; |
| 899 | node=stp_mxmlFindElement(node,node, "option", "name", desc.name, STP_MXML_DESCEND); |
| 900 | if(node) |
| 901 | { |
| 902 | node=stp_mxmlFindElement(node,node, "choice", "name", val, STP_MXML_DESCEND); |
| 903 | if(node && node->child) |
| 904 | { |
| 905 | if(node->child->value.opaque && (strlen(node->child->value.opaque)>1)) |
| 906 | { |
| 907 | /* If we have opaque data for the child, we use %%BeginFeature and copy the code verbatim. */ |
| 908 | stp_puts("[{\n", v); |
| 909 | stp_zprintf(v, "%%%%BeginFeature: *%s %s\n", desc.name, val); |
| 910 | if(node->child->value.opaque) |
| 911 | stp_puts(node->child->value.opaque,v); |
| 912 | stp_puts("\n%%EndFeature\n", v); |
| 913 | stp_puts("} stopped cleartomark\n", v); |
| 914 | } |
| 915 | else |
| 916 | { |
| 917 | /* If we don't have code, we use %%IncludeFeature instead. */ |
| 918 | stp_puts("[{\n", v); |
| 919 | stp_zprintf(v, "%%%%IncludeFeature: *%s %s\n", desc.name, val); |
| 920 | if(node->child->value.opaque) |
| 921 | stp_puts(node->child->value.opaque,v); |
| 922 | stp_puts("} stopped cleartomark\n", v); |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | break; |
| 930 | case STP_PARAMETER_TYPE_INT: |
| 931 | if(stp_get_int_parameter(v,desc.name)!=desc.deflt.integer) |
| 932 | { |
| 933 | stp_puts("[{\n", v); |
| 934 | stp_zprintf(v, "%%%%IncludeFeature: *%s %d\n", desc.name, |
| 935 | stp_get_int_parameter(v, desc.name)); |
| 936 | stp_puts("} stopped cleartomark\n", v); |
| 937 | } |
| 938 | break; |
| 939 | case STP_PARAMETER_TYPE_DOUBLE: |
| 940 | if(stp_get_float_parameter(v,desc.name)!=desc.deflt.dbl) |
| 941 | { |
| 942 | stp_puts("[{\n", v); |
| 943 | stp_zprintf(v, "%%%%IncludeFeature: *%s %f\n", desc.name, |
| 944 | stp_get_float_parameter(v, desc.name)); |
| 945 | stp_puts("} stopped cleartomark\n", v); |
| 946 | } |
| 947 | break; |
| 948 | case STP_PARAMETER_TYPE_DIMENSION: |
| 949 | if(stp_get_dimension_parameter(v,desc.name)!=desc.deflt.dimension) |
| 950 | { |
| 951 | stp_puts("[{\n", v); |
| 952 | stp_zprintf(v, "%%%%IncludeFeature: *%s %f\n", desc.name, |
| 953 | stp_get_dimension_parameter(v, desc.name)); |
| 954 | stp_puts("} stopped cleartomark\n", v); |
| 955 | } |
| 956 | break; |
| 957 | default: |
| 958 | break; |
| 959 | } |
| 960 | } |
| 961 | stp_parameter_description_destroy(&desc); |
| 962 | } |
| 963 | stp_puts("%%EndSetup\n", v); |
| 964 | stp_parameter_list_destroy(param_list); |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * 'ps_print()' - Print an image to a PostScript printer. |
| 969 | */ |
| 970 | |
| 971 | static int |
| 972 | ps_print_internal(stp_vars_t *v, stp_image_t *image) |
| 973 | { |
| 974 | int status = 1; |
| 975 | int model = stp_get_model_id(v); |
| 976 | const char *print_mode = stp_get_string_parameter(v, "PrintingMode"); |
| 977 | const char *input_image_type = stp_get_string_parameter(v, "InputImageType"); |
| 978 | unsigned short *out = NULL; |
| 979 | stp_dimension_t top = stp_get_top(v); |
| 980 | stp_dimension_t left = stp_get_left(v); |
| 981 | int y; /* Looping vars */ |
| 982 | stp_dimension_t page_left, /* Left margin of page */ |
| 983 | page_right, /* Right margin of page */ |
| 984 | page_top, /* Top of page */ |
| 985 | page_bottom, /* Bottom of page */ |
| 986 | page_width, /* Width of page */ |
| 987 | page_height, /* Height of page */ |
| 988 | paper_width, /* Width of physical page */ |
| 989 | paper_height; /* Height of physical page */ |
| 990 | int out_width, /* Width of image on page */ |
| 991 | out_height, /* Height of image on page */ |
| 992 | out_channels, /* Output bytes per pixel */ |
| 993 | out_ps_height, /* Output height (Level 2 output) */ |
| 994 | out_offset; /* Output offset (Level 2 output) */ |
| 995 | time_t curtime; /* Current time of day */ |
| 996 | unsigned zero_mask; |
| 997 | int image_height, |
| 998 | image_width; |
| 999 | int color_out = 0; |
| 1000 | int cmyk_out = 0; |
| 1001 | |
| 1002 | if (print_mode && strcmp(print_mode, "Color") == 0) |
| 1003 | color_out = 1; |
| 1004 | if (color_out && |
| 1005 | input_image_type && (strcmp(input_image_type, "CMYK") == 0 || |
| 1006 | strcmp(input_image_type, "KCMY") == 0)) |
| 1007 | cmyk_out = 1; |
| 1008 | |
| 1009 | stp_image_init(image); |
| 1010 | |
| 1011 | /* |
| 1012 | * Compute the output size... |
| 1013 | */ |
| 1014 | |
| 1015 | out_width = stp_get_width(v); |
| 1016 | out_height = stp_get_height(v); |
| 1017 | |
| 1018 | ps_imageable_area_internal(v, 0, &page_left, &page_right, &page_bottom, &page_top); |
| 1019 | ps_media_size_internal(v, &paper_width, &paper_height); |
| 1020 | page_width = page_right - page_left; |
| 1021 | page_height = page_bottom - page_top; |
| 1022 | |
| 1023 | image_height = stp_image_height(image); |
| 1024 | image_width = stp_image_width(image); |
| 1025 | |
| 1026 | /* |
| 1027 | * Output a standard PostScript header with DSC comments... |
| 1028 | */ |
| 1029 | |
| 1030 | curtime = stpi_time(NULL); |
| 1031 | |
| 1032 | top = paper_height - top; |
| 1033 | |
| 1034 | stp_dprintf(STP_DBG_PS, v, |
| 1035 | "out_width = %d, out_height = %d\n", out_width, out_height); |
| 1036 | stp_dprintf(STP_DBG_PS, v, |
| 1037 | "page_left = %f, page_right = %f, page_bottom = %f, page_top = %f\n", |
| 1038 | page_left, page_right, page_bottom, page_top); |
| 1039 | stp_dprintf(STP_DBG_PS, v, "left = %f, top = %f\n", left, top); |
| 1040 | stp_dprintf(STP_DBG_PS, v, "page_width = %f, page_height = %f\n", |
| 1041 | page_width, page_height); |
| 1042 | |
| 1043 | stp_dprintf(STP_DBG_PS, v, "bounding box l %f b %f r %f t %f\n", |
| 1044 | page_left, paper_height - page_bottom, |
| 1045 | page_right, paper_height - page_top); |
| 1046 | |
| 1047 | stp_puts("%!PS-Adobe-3.0\n", v); |
| 1048 | #ifdef HAVE_CONFIG_H |
| 1049 | stp_zprintf(v, "%%%%Creator: %s/Gutenprint %s (%s)\n", |
| 1050 | stp_image_get_appname(image), VERSION, RELEASE_DATE); |
| 1051 | #else |
| 1052 | stp_zprintf(v, "%%%%Creator: %s/Gutenprint\n", stp_image_get_appname(image)); |
| 1053 | #endif |
| 1054 | stp_zprintf(v, "%%%%CreationDate: %s", ctime(&curtime)); |
| 1055 | stp_zprintf(v, "%%%%BoundingBox: %f %f %f %f\n", |
| 1056 | page_left, paper_height - page_bottom, |
| 1057 | page_right, paper_height - page_top); |
| 1058 | stp_puts("%%DocumentData: Clean7Bit\n", v); |
| 1059 | stp_zprintf(v, "%%%%LanguageLevel: %d\n", model + 1); |
| 1060 | stp_puts("%%Pages: 1\n", v); |
| 1061 | stp_puts("%%Orientation: Portrait\n", v); |
| 1062 | stp_puts("%%EndComments\n", v); |
| 1063 | |
| 1064 | ps_print_device_settings(v); |
| 1065 | |
| 1066 | /* |
| 1067 | * Output the page... |
| 1068 | */ |
| 1069 | |
| 1070 | stp_puts("%%Page: 1 1\n", v); |
| 1071 | stp_puts("gsave\n", v); |
| 1072 | |
| 1073 | stp_zprintf(v, "%f %f translate\n", left, top); |
| 1074 | |
| 1075 | /* Force locale to "C", because decimal numbers in Postscript must |
| 1076 | always be printed with a decimal point rather than the |
| 1077 | locale-specific setting. */ |
| 1078 | |
| 1079 | stp_zprintf(v, "%.3f %.3f scale\n", |
| 1080 | (double)out_width / ((double)image_width), |
| 1081 | (double)out_height / ((double)image_height)); |
| 1082 | |
| 1083 | stp_channel_reset(v); |
| 1084 | stp_channel_add(v, 0, 0, 1.0); |
| 1085 | if (color_out) |
| 1086 | { |
| 1087 | stp_channel_add(v, 1, 0, 1.0); |
| 1088 | stp_channel_add(v, 2, 0, 1.0); |
| 1089 | if (cmyk_out) |
| 1090 | { |
| 1091 | stp_channel_add(v, 3, 0, 1.0); |
| 1092 | stp_set_string_parameter(v, "STPIOutputType", "CMYK"); |
| 1093 | } |
| 1094 | else |
| 1095 | stp_set_string_parameter(v, "STPIOutputType", "RGB"); |
| 1096 | } |
| 1097 | else |
| 1098 | stp_set_string_parameter(v, "STPIOutputType", "Whitescale"); |
| 1099 | |
| 1100 | stp_set_boolean_parameter(v, "SimpleGamma", 1); |
| 1101 | |
| 1102 | out_channels = stp_color_init(v, image, 256); |
| 1103 | |
| 1104 | if (model == 0) |
| 1105 | { |
| 1106 | stp_zprintf(v, "/picture %d string def\n", image_width * out_channels); |
| 1107 | |
| 1108 | stp_zprintf(v, "%d %d 8\n", image_width, image_height); |
| 1109 | |
| 1110 | stp_puts("[ 1 0 0 -1 0 1 ]\n", v); |
| 1111 | |
| 1112 | if (cmyk_out) |
| 1113 | stp_puts("{currentfile picture readhexstring pop} false 4 colorimage\n", v); |
| 1114 | else if (color_out) |
| 1115 | stp_puts("{currentfile picture readhexstring pop} false 3 colorimage\n", v); |
| 1116 | else |
| 1117 | stp_puts("{currentfile picture readhexstring pop} image\n", v); |
| 1118 | |
| 1119 | for (y = 0; y < image_height; y ++) |
| 1120 | { |
| 1121 | if (stp_color_get_row(v, image, y, &zero_mask)) |
| 1122 | { |
| 1123 | status = 2; |
| 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | out = stp_channel_get_input(v); |
| 1128 | |
| 1129 | /* Convert from KCMY to CMYK */ |
| 1130 | if (cmyk_out) |
| 1131 | { |
| 1132 | int x; |
| 1133 | unsigned short *pos = out; |
| 1134 | for (x = 0; x < image_width; x++, pos += 4) |
| 1135 | { |
| 1136 | unsigned short p0 = pos[0]; |
| 1137 | pos[0] = pos[1]; |
| 1138 | pos[1] = pos[2]; |
| 1139 | pos[2] = pos[3]; |
| 1140 | pos[3] = p0; |
| 1141 | } |
| 1142 | } |
| 1143 | ps_hex(v, out, image_width * out_channels); |
| 1144 | } |
| 1145 | } |
| 1146 | else |
| 1147 | { |
| 1148 | unsigned short *tmp_buf = |
| 1149 | stp_malloc(sizeof(unsigned short) * (image_width * out_channels + 4)); |
| 1150 | if (cmyk_out) |
| 1151 | stp_puts("/DeviceCMYK setcolorspace\n", v); |
| 1152 | else if (color_out) |
| 1153 | stp_puts("/DeviceRGB setcolorspace\n", v); |
| 1154 | else |
| 1155 | stp_puts("/DeviceGray setcolorspace\n", v); |
| 1156 | |
| 1157 | stp_puts("<<\n", v); |
| 1158 | stp_puts("\t/ImageType 1\n", v); |
| 1159 | |
| 1160 | stp_zprintf(v, "\t/Width %d\n", image_width); |
| 1161 | stp_zprintf(v, "\t/Height %d\n", image_height); |
| 1162 | stp_puts("\t/BitsPerComponent 8\n", v); |
| 1163 | |
| 1164 | if (cmyk_out) |
| 1165 | stp_puts("\t/Decode [ 0 1 0 1 0 1 0 1 ]\n", v); |
| 1166 | else if (color_out) |
| 1167 | stp_puts("\t/Decode [ 0 1 0 1 0 1 ]\n", v); |
| 1168 | else |
| 1169 | stp_puts("\t/Decode [ 0 1 ]\n", v); |
| 1170 | |
| 1171 | stp_puts("\t/DataSource currentfile /ASCII85Decode filter\n", v); |
| 1172 | |
| 1173 | if ((image_width * 72 / out_width) < 100) |
| 1174 | stp_puts("\t/Interpolate true\n", v); |
| 1175 | |
| 1176 | stp_puts("\t/ImageMatrix [ 1 0 0 -1 0 1 ]\n", v); |
| 1177 | |
| 1178 | stp_puts(">>\n", v); |
| 1179 | stp_puts("image\n", v); |
| 1180 | |
| 1181 | for (y = 0, out_offset = 0; y < image_height; y ++) |
| 1182 | { |
| 1183 | unsigned short *where; |
| 1184 | /* FIXME!!! */ |
| 1185 | if (stp_color_get_row(v, image, y /*, out + out_offset */ , &zero_mask)) |
| 1186 | { |
| 1187 | status = 2; |
| 1188 | break; |
| 1189 | } |
| 1190 | out = stp_channel_get_input(v); |
| 1191 | if (out_offset > 0) |
| 1192 | { |
| 1193 | memcpy(tmp_buf + out_offset, out, |
| 1194 | image_width * out_channels * sizeof(unsigned short)); |
| 1195 | where = tmp_buf; |
| 1196 | } |
| 1197 | else |
| 1198 | where = out; |
| 1199 | |
| 1200 | /* Convert from KCMY to CMYK */ |
| 1201 | if (cmyk_out) |
| 1202 | { |
| 1203 | int x; |
| 1204 | unsigned short *pos = where; |
| 1205 | for (x = 0; x < image_width; x++, pos += 4) |
| 1206 | { |
| 1207 | unsigned short p0 = pos[0]; |
| 1208 | pos[0] = pos[1]; |
| 1209 | pos[1] = pos[2]; |
| 1210 | pos[2] = pos[3]; |
| 1211 | pos[3] = p0; |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | out_ps_height = out_offset + image_width * out_channels; |
| 1216 | |
| 1217 | if (y < (image_height - 1)) |
| 1218 | { |
| 1219 | ps_ascii85(v, where, out_ps_height & ~3, 0); |
| 1220 | out_offset = out_ps_height & 3; |
| 1221 | } |
| 1222 | else |
| 1223 | { |
| 1224 | ps_ascii85(v, where, out_ps_height, 1); |
| 1225 | out_offset = 0; |
| 1226 | } |
| 1227 | |
| 1228 | if (out_offset > 0) |
| 1229 | memcpy(tmp_buf, where + out_ps_height - out_offset, |
| 1230 | out_offset * sizeof(unsigned short)); |
| 1231 | } |
| 1232 | stp_free(tmp_buf); |
| 1233 | } |
| 1234 | stp_image_conclude(image); |
| 1235 | |
| 1236 | stp_puts("grestore\n", v); |
| 1237 | stp_puts("showpage\n", v); |
| 1238 | stp_puts("%%Trailer\n", v); |
| 1239 | stp_puts("%%EOF\n", v); |
| 1240 | return status; |
| 1241 | } |
| 1242 | |
| 1243 | static int |
| 1244 | ps_print(const stp_vars_t *v, stp_image_t *image) |
| 1245 | { |
| 1246 | int status; |
| 1247 | #ifdef HAVE_LOCALE_H |
| 1248 | char *locale; |
| 1249 | #endif |
| 1250 | stp_vars_t *nv = stp_vars_create_copy(v); |
| 1251 | if (!stp_verify(nv)) |
| 1252 | { |
| 1253 | stp_eprintf(nv, "Print options not verified; cannot print.\n"); |
| 1254 | return 0; |
| 1255 | } |
| 1256 | #ifdef HAVE_LOCALE_H |
| 1257 | locale = stp_strdup(setlocale(LC_ALL, NULL)); |
| 1258 | setlocale(LC_ALL, "C"); |
| 1259 | #endif |
| 1260 | status = ps_print_internal(nv, image); |
| 1261 | #ifdef HAVE_LOCALE_H |
| 1262 | setlocale(LC_ALL, locale); |
| 1263 | stp_free(locale); |
| 1264 | #endif |
| 1265 | stp_vars_destroy(nv); |
| 1266 | return status; |
| 1267 | } |
| 1268 | |
| 1269 | |
| 1270 | /* |
| 1271 | * 'ps_hex()' - Print binary data as a series of hexadecimal numbers. |
| 1272 | */ |
| 1273 | |
| 1274 | static void |
| 1275 | ps_hex(const stp_vars_t *v, /* I - File to print to */ |
| 1276 | unsigned short *data, /* I - Data to print */ |
| 1277 | int length) /* I - Number of bytes to print */ |
| 1278 | { |
| 1279 | int col; /* Current column */ |
| 1280 | static const char *hex = "0123456789ABCDEF"; |
| 1281 | |
| 1282 | col = 0; |
| 1283 | while (length > 0) |
| 1284 | { |
| 1285 | unsigned char pixel = (*data & 0xff00) >> 8; |
| 1286 | /* |
| 1287 | * Put the hex chars out to the file; note that we don't use stp_zprintf() |
| 1288 | * for speed reasons... |
| 1289 | */ |
| 1290 | |
| 1291 | stp_putc(hex[pixel >> 4], v); |
| 1292 | stp_putc(hex[pixel & 15], v); |
| 1293 | |
| 1294 | data ++; |
| 1295 | length --; |
| 1296 | |
| 1297 | col += 2; |
| 1298 | if (col >= 72) |
| 1299 | { |
| 1300 | col = 0; |
| 1301 | stp_putc('\n', v); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | if (col > 0) |
| 1306 | stp_putc('\n', v); |
| 1307 | } |
| 1308 | |
| 1309 | |
| 1310 | /* |
| 1311 | * 'ps_ascii85()' - Print binary data as a series of base-85 numbers. |
| 1312 | */ |
| 1313 | |
| 1314 | static void |
| 1315 | ps_ascii85(const stp_vars_t *v, /* I - File to print to */ |
| 1316 | unsigned short *data, /* I - Data to print */ |
| 1317 | int length, /* I - Number of bytes to print */ |
| 1318 | int last_line) /* I - Last line of raster data? */ |
| 1319 | { |
| 1320 | int i; /* Looping var */ |
| 1321 | unsigned b; /* Binary data word */ |
| 1322 | unsigned char c[5]; /* ASCII85 encoded chars */ |
| 1323 | static int column = 0; /* Current column */ |
| 1324 | |
| 1325 | #define OUTBUF_SIZE 4096 |
| 1326 | unsigned char outbuffer[OUTBUF_SIZE+10]; |
| 1327 | int outp=0; |
| 1328 | |
| 1329 | while (length > 3) |
| 1330 | { |
| 1331 | unsigned char d0 = (data[0] & 0xff00) >> 8; |
| 1332 | unsigned char d1 = (data[1] & 0xff00) >> 8; |
| 1333 | unsigned char d2 = (data[2] & 0xff00) >> 8; |
| 1334 | unsigned char d3 = (data[3] & 0xff00) >> 8; |
| 1335 | b = (((((d0 << 8) | d1) << 8) | d2) << 8) | d3; |
| 1336 | |
| 1337 | if (b == 0) |
| 1338 | { |
| 1339 | outbuffer[outp++]='z'; |
| 1340 | column ++; |
| 1341 | } |
| 1342 | else |
| 1343 | { |
| 1344 | outbuffer[outp+4] = (b % 85) + '!'; |
| 1345 | b /= 85; |
| 1346 | outbuffer[outp+3] = (b % 85) + '!'; |
| 1347 | b /= 85; |
| 1348 | outbuffer[outp+2] = (b % 85) + '!'; |
| 1349 | b /= 85; |
| 1350 | outbuffer[outp+1] = (b % 85) + '!'; |
| 1351 | b /= 85; |
| 1352 | outbuffer[outp] = b + '!'; |
| 1353 | |
| 1354 | outp+=5; |
| 1355 | column += 5; |
| 1356 | } |
| 1357 | |
| 1358 | if (column > 72) |
| 1359 | { |
| 1360 | outbuffer[outp++]='\n'; |
| 1361 | column = 0; |
| 1362 | } |
| 1363 | |
| 1364 | if(outp>=OUTBUF_SIZE) |
| 1365 | { |
| 1366 | stp_zfwrite((const char *)outbuffer, outp, 1, v); |
| 1367 | outp=0; |
| 1368 | } |
| 1369 | |
| 1370 | data += 4; |
| 1371 | length -= 4; |
| 1372 | } |
| 1373 | |
| 1374 | if(outp) |
| 1375 | stp_zfwrite((const char *)outbuffer, outp, 1, v); |
| 1376 | |
| 1377 | if (last_line) |
| 1378 | { |
| 1379 | if (length > 0) |
| 1380 | { |
| 1381 | for (b = 0, i = length; i > 0; b = (b << 8) | data[0], data ++, i --); |
| 1382 | |
| 1383 | c[4] = (b % 85) + '!'; |
| 1384 | b /= 85; |
| 1385 | c[3] = (b % 85) + '!'; |
| 1386 | b /= 85; |
| 1387 | c[2] = (b % 85) + '!'; |
| 1388 | b /= 85; |
| 1389 | c[1] = (b % 85) + '!'; |
| 1390 | b /= 85; |
| 1391 | c[0] = b + '!'; |
| 1392 | |
| 1393 | stp_zfwrite((const char *)c, length + 1, 1, v); |
| 1394 | } |
| 1395 | |
| 1396 | stp_puts("~>\n", v); |
| 1397 | column = 0; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | |
| 1402 | static const stp_printfuncs_t print_ps_printfuncs = |
| 1403 | { |
| 1404 | ps_list_parameters, |
| 1405 | ps_parameters, |
| 1406 | ps_media_size, |
| 1407 | ps_imageable_area, |
| 1408 | ps_maximum_imageable_area, |
| 1409 | ps_limit, |
| 1410 | ps_print, |
| 1411 | ps_describe_resolution, |
| 1412 | ps_describe_output, |
| 1413 | stp_verify_printer_params, |
| 1414 | NULL, |
| 1415 | NULL, |
| 1416 | ps_external_options, |
| 1417 | ps_describe_papersize |
| 1418 | }; |
| 1419 | |
| 1420 | |
| 1421 | static stp_family_t print_ps_module_data = |
| 1422 | { |
| 1423 | &print_ps_printfuncs, |
| 1424 | NULL |
| 1425 | }; |
| 1426 | |
| 1427 | |
| 1428 | static int |
| 1429 | print_ps_module_init(void) |
| 1430 | { |
| 1431 | return stpi_family_register(print_ps_module_data.printer_list); |
| 1432 | } |
| 1433 | |
| 1434 | |
| 1435 | static int |
| 1436 | print_ps_module_exit(void) |
| 1437 | { |
| 1438 | return stpi_family_unregister(print_ps_module_data.printer_list); |
| 1439 | } |
| 1440 | |
| 1441 | |
| 1442 | /* Module header */ |
| 1443 | #define stp_module_version print_ps_LTX_stp_module_version |
| 1444 | #define stp_module_data print_ps_LTX_stp_module_data |
| 1445 | |
| 1446 | stp_module_version_t stp_module_version = {0, 0}; |
| 1447 | |
| 1448 | stp_module_t stp_module_data = |
| 1449 | { |
| 1450 | "ps", |
| 1451 | VERSION, |
| 1452 | "Postscript family driver", |
| 1453 | STP_MODULE_CLASS_FAMILY, |
| 1454 | NULL, |
| 1455 | print_ps_module_init, |
| 1456 | print_ps_module_exit, |
| 1457 | (void *) &print_ps_module_data |
| 1458 | }; |