| 1 | /* |
| 2 | * |
| 3 | * Print plug-in driver utility functions for the GIMP. |
| 4 | * |
| 5 | * Copyright 1997-2000 Michael Sweet ([email protected]) and |
| 6 | * Robert Krawitz ([email protected]) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 | * for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * This file must include only standard C header files. The core code must |
| 24 | * compile on generic platforms that don't support glib, gimp, gtk, etc. |
| 25 | */ |
| 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | #include <config.h> |
| 29 | #endif |
| 30 | #include <gutenprint/gutenprint.h> |
| 31 | #include "gutenprint-internal.h" |
| 32 | #include <gutenprint/gutenprint-intl-internal.h> |
| 33 | #include <math.h> |
| 34 | #ifdef HAVE_LIMITS_H |
| 35 | #include <limits.h> |
| 36 | #endif |
| 37 | #include <string.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <sys/param.h> |
| 40 | |
| 41 | typedef struct |
| 42 | { |
| 43 | char *name; |
| 44 | stp_papersize_list_t *list; |
| 45 | } papersize_list_impl_t; |
| 46 | |
| 47 | static stp_list_t *list_of_papersize_lists = NULL; |
| 48 | |
| 49 | static void |
| 50 | papersize_list_impl_freefunc(void *item) |
| 51 | { |
| 52 | papersize_list_impl_t *papersize_list = (papersize_list_impl_t *) item; |
| 53 | stp_list_destroy(papersize_list->list); |
| 54 | STP_SAFE_FREE(papersize_list->name); |
| 55 | STP_SAFE_FREE(papersize_list); |
| 56 | } |
| 57 | |
| 58 | static const char * |
| 59 | papersize_list_impl_namefunc(const void *item) |
| 60 | { |
| 61 | return ((const papersize_list_impl_t *) item)->name; |
| 62 | } |
| 63 | |
| 64 | static const char * |
| 65 | papersize_list_impl_long_namefunc(const void *item) |
| 66 | { |
| 67 | return ((const papersize_list_impl_t *) item)->name; |
| 68 | } |
| 69 | |
| 70 | static void |
| 71 | check_list_of_papersize_lists(void) |
| 72 | { |
| 73 | if (! list_of_papersize_lists) |
| 74 | { |
| 75 | stp_deprintf(STP_DBG_PAPER, "Initializing...\n"); |
| 76 | list_of_papersize_lists = stp_list_create(); |
| 77 | stp_list_set_freefunc(list_of_papersize_lists, papersize_list_impl_freefunc); |
| 78 | stp_list_set_namefunc(list_of_papersize_lists, papersize_list_impl_namefunc); |
| 79 | stp_list_set_long_namefunc(list_of_papersize_lists, papersize_list_impl_long_namefunc); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | stpi_papersize_freefunc(void *item) |
| 85 | { |
| 86 | stp_papersize_t *paper = (stp_papersize_t *) (item); |
| 87 | #pragma GCC diagnostic push |
| 88 | #pragma GCC diagnostic ignored "-Wcast-qual" |
| 89 | STP_SAFE_FREE(paper->name); |
| 90 | STP_SAFE_FREE(paper->text); |
| 91 | STP_SAFE_FREE(paper->comment); |
| 92 | #pragma GCC diagnostic pop |
| 93 | STP_SAFE_FREE(paper); |
| 94 | } |
| 95 | |
| 96 | static const char * |
| 97 | stpi_papersize_namefunc(const void *item) |
| 98 | { |
| 99 | const stp_papersize_t *paper = (const stp_papersize_t *) (item); |
| 100 | return paper->name; |
| 101 | } |
| 102 | |
| 103 | static const char * |
| 104 | stpi_papersize_long_namefunc(const void *item) |
| 105 | { |
| 106 | const stp_papersize_t *paper = (const stp_papersize_t *) (item); |
| 107 | return paper->text; |
| 108 | } |
| 109 | |
| 110 | stp_papersize_list_t * |
| 111 | stpi_create_papersize_list(void) |
| 112 | { |
| 113 | stp_list_t *papersize_list = stp_list_create(); |
| 114 | stp_list_set_freefunc(papersize_list, stpi_papersize_freefunc); |
| 115 | stp_list_set_namefunc(papersize_list, stpi_papersize_namefunc); |
| 116 | stp_list_set_long_namefunc(papersize_list, stpi_papersize_long_namefunc); |
| 117 | return (stp_papersize_list_t *) papersize_list; |
| 118 | } |
| 119 | |
| 120 | int |
| 121 | stpi_papersize_create(stp_papersize_list_t *list, stp_papersize_t *p) |
| 122 | { |
| 123 | stp_list_item_t *papersize_item; |
| 124 | |
| 125 | /* |
| 126 | * Check the paper does not already exist |
| 127 | * Not the most efficient way of doing it, but the number of papers |
| 128 | * is not large enough to be a significant bottleneck. |
| 129 | */ |
| 130 | papersize_item = stp_list_get_start(list); |
| 131 | while (papersize_item) |
| 132 | { |
| 133 | const stp_papersize_t *ep = |
| 134 | (const stp_papersize_t *) stp_list_item_get_data(papersize_item); |
| 135 | if (ep && !strcmp(p->name, ep->name)) |
| 136 | { |
| 137 | stp_erprintf("Duplicate paper size `%s'\n", p->name); |
| 138 | stpi_papersize_freefunc(p); |
| 139 | return 1; |
| 140 | } |
| 141 | papersize_item = stp_list_item_next(papersize_item); |
| 142 | } |
| 143 | |
| 144 | /* Add paper to list */ |
| 145 | stp_list_item_create(list, NULL, (void *) p); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int |
| 151 | stpi_papersize_count(const stp_papersize_list_t *paper_size_list) |
| 152 | { |
| 153 | return stp_list_get_length(paper_size_list); |
| 154 | } |
| 155 | |
| 156 | const stp_papersize_t * |
| 157 | stpi_get_papersize_by_name(const stp_papersize_list_t *list, const char *name) |
| 158 | { |
| 159 | stp_list_item_t *paper; |
| 160 | |
| 161 | paper = stp_list_get_item_by_name(list, name); |
| 162 | if (!paper) |
| 163 | return NULL; |
| 164 | else |
| 165 | return (const stp_papersize_t *) stp_list_item_get_data(paper); |
| 166 | } |
| 167 | |
| 168 | const stp_papersize_t * |
| 169 | stpi_get_listed_papersize(const char *name, const char *papersize_list) |
| 170 | { |
| 171 | const stp_papersize_list_t *list = |
| 172 | stpi_get_papersize_list_named(papersize_list, ""); |
| 173 | if (list) |
| 174 | return stpi_get_papersize_by_name(list, name); |
| 175 | else |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | const stp_papersize_t * |
| 180 | stpi_standard_describe_papersize(const stp_vars_t *v, const char *name) |
| 181 | { |
| 182 | STPI_ASSERT(v, NULL); |
| 183 | return stpi_get_listed_papersize(name, "standard"); |
| 184 | } |
| 185 | |
| 186 | const stp_papersize_t * |
| 187 | stp_describe_papersize(const stp_vars_t *v, const char *name) |
| 188 | { |
| 189 | return stpi_printer_describe_papersize(v, name); |
| 190 | } |
| 191 | |
| 192 | static int |
| 193 | papersize_size_mismatch(stp_dimension_t l, stp_dimension_t w, |
| 194 | const stp_papersize_t *val) |
| 195 | { |
| 196 | stp_dimension_t hdiff = STP_DABS(l - (stp_dimension_t) val->height); |
| 197 | stp_dimension_t vdiff = STP_DABS(w - (stp_dimension_t) val->width); |
| 198 | return hdiff > vdiff ? hdiff : vdiff; |
| 199 | } |
| 200 | |
| 201 | static const stp_papersize_t * |
| 202 | get_papersize_by_size_internal(const stp_papersize_list_t *list, |
| 203 | stp_dimension_t l, stp_dimension_t w, |
| 204 | int exact) |
| 205 | { |
| 206 | int score = INT_MAX; |
| 207 | const stp_papersize_t *ref = NULL; |
| 208 | const stp_papersize_t *val = NULL; |
| 209 | const stp_papersize_list_item_t *ptli = |
| 210 | stpi_papersize_list_get_start(list); |
| 211 | STPI_ASSERT(list, NULL); |
| 212 | while (ptli) |
| 213 | { |
| 214 | val = stpi_paperlist_item_get_data(ptli); |
| 215 | |
| 216 | if (val->width == w && val->height == l) |
| 217 | { |
| 218 | if (val->top == 0 && val->left == 0 && |
| 219 | val->bottom == 0 && val->right == 0) |
| 220 | return val; |
| 221 | else |
| 222 | ref = val; |
| 223 | } |
| 224 | else if (!exact) |
| 225 | { |
| 226 | int myscore = papersize_size_mismatch(l, w, val); |
| 227 | if (myscore < score && myscore < 5) |
| 228 | { |
| 229 | ref = val; |
| 230 | score = myscore; |
| 231 | } |
| 232 | } |
| 233 | ptli = stpi_paperlist_item_next(ptli); |
| 234 | } |
| 235 | return ref; |
| 236 | } |
| 237 | |
| 238 | const stp_papersize_t * |
| 239 | stpi_get_papersize_by_size(const stp_papersize_list_t *list, |
| 240 | stp_dimension_t l, stp_dimension_t w) |
| 241 | { |
| 242 | return get_papersize_by_size_internal(list, l, w, 0); |
| 243 | } |
| 244 | |
| 245 | const stp_papersize_t * |
| 246 | stpi_get_papersize_by_size_exact(const stp_papersize_list_t *list, |
| 247 | stp_dimension_t l, stp_dimension_t w) |
| 248 | { |
| 249 | return get_papersize_by_size_internal(list, l, w, 1); |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | stp_default_media_size(const stp_vars_t *v, /* I */ |
| 254 | stp_dimension_t *width, /* O - Width in points */ |
| 255 | stp_dimension_t *height) /* O - Height in points */ |
| 256 | { |
| 257 | if (stp_get_page_width(v) > 0 && stp_get_page_height(v) > 0) |
| 258 | { |
| 259 | *width = stp_get_page_width(v); |
| 260 | *height = stp_get_page_height(v); |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | const char *page_size = stp_get_string_parameter(v, "PageSize"); |
| 265 | const stp_papersize_t *papersize = NULL; |
| 266 | if (page_size) |
| 267 | papersize = stp_describe_papersize(v, page_size); |
| 268 | if (!papersize) |
| 269 | { |
| 270 | *width = 1; |
| 271 | *height = 1; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | *width = papersize->width; |
| 276 | *height = papersize->height; |
| 277 | } |
| 278 | if (*width == 0) |
| 279 | *width = 612; |
| 280 | if (*height == 0) |
| 281 | *height = 792; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Process the <paper> node. |
| 287 | */ |
| 288 | static stp_papersize_t * |
| 289 | stp_xml_process_paper(stp_mxml_node_t *paper) /* The paper node */ |
| 290 | { |
| 291 | stp_mxml_node_t *prop; /* Temporary node pointer */ |
| 292 | const char *stmp; /* Temporary string */ |
| 293 | /* props[] (unused) is the correct tag sequence */ |
| 294 | /* const char *props[] = |
| 295 | { |
| 296 | "name", |
| 297 | "description", |
| 298 | "width", |
| 299 | "height", |
| 300 | "left", |
| 301 | "right", |
| 302 | "bottom", |
| 303 | "top", |
| 304 | "unit", |
| 305 | "type", |
| 306 | NULL |
| 307 | };*/ |
| 308 | stp_papersize_t *outpaper; /* Generated paper */ |
| 309 | int |
| 310 | id = 0, /* Check id is present */ |
| 311 | name = 0, /* Check name is present */ |
| 312 | height = 0, /* Check height is present */ |
| 313 | width = 0, /* Check width is present */ |
| 314 | unit = 0; /* Check unit is present */ |
| 315 | |
| 316 | if (stp_get_debug_level() & STP_DBG_XML) |
| 317 | { |
| 318 | stmp = stp_mxmlElementGetAttr(paper, (const char*) "name"); |
| 319 | stp_erprintf("stp_xml_process_paper: name: %s\n", stmp); |
| 320 | } |
| 321 | |
| 322 | outpaper = stp_zalloc(sizeof(stp_papersize_t)); |
| 323 | if (!outpaper) |
| 324 | return NULL; |
| 325 | |
| 326 | outpaper->name = stp_strdup(stp_mxmlElementGetAttr(paper, "name")); |
| 327 | |
| 328 | outpaper->top = 0; |
| 329 | outpaper->left = 0; |
| 330 | outpaper->bottom = 0; |
| 331 | outpaper->right = 0; |
| 332 | outpaper->paper_size_type = PAPERSIZE_TYPE_STANDARD; |
| 333 | if (outpaper->name) |
| 334 | id = 1; |
| 335 | |
| 336 | prop = paper->child; |
| 337 | while(prop) |
| 338 | { |
| 339 | if (prop->type == STP_MXML_ELEMENT) |
| 340 | { |
| 341 | const char *prop_name = prop->value.element.name; |
| 342 | |
| 343 | if (!strcmp(prop_name, "description")) |
| 344 | { |
| 345 | outpaper->text = stp_strdup(stp_mxmlElementGetAttr(prop, "value")); |
| 346 | name = 1; |
| 347 | } |
| 348 | if (!strcmp(prop_name, "comment")) |
| 349 | outpaper->comment = stp_strdup(stp_mxmlElementGetAttr(prop, "value")); |
| 350 | if (!strcmp(prop_name, "width")) |
| 351 | { |
| 352 | stmp = stp_mxmlElementGetAttr(prop, "value"); |
| 353 | if (stmp) |
| 354 | { |
| 355 | outpaper->width = stp_xmlstrtodim(stmp); |
| 356 | width = 1; |
| 357 | } |
| 358 | } |
| 359 | if (!strcmp(prop_name, "height")) |
| 360 | { |
| 361 | stmp = stp_mxmlElementGetAttr(prop, "value"); |
| 362 | if (stmp) |
| 363 | { |
| 364 | outpaper->height = stp_xmlstrtodim(stmp); |
| 365 | height = 1; |
| 366 | } |
| 367 | } |
| 368 | if (!strcmp(prop_name, "left")) |
| 369 | { |
| 370 | stmp = stp_mxmlElementGetAttr(prop, "value"); |
| 371 | outpaper->left = stp_xmlstrtodim(stmp); |
| 372 | } |
| 373 | if (!strcmp(prop_name, "right")) |
| 374 | { |
| 375 | stmp = stp_mxmlElementGetAttr(prop, "value"); |
| 376 | outpaper->right = stp_xmlstrtodim(stmp); |
| 377 | } |
| 378 | if (!strcmp(prop_name, "bottom")) |
| 379 | { |
| 380 | stmp = stp_mxmlElementGetAttr(prop, "value"); |
| 381 | outpaper->bottom = stp_xmlstrtodim(stmp); |
| 382 | } |
| 383 | if (!strcmp(prop_name, "top")) |
| 384 | { |
| 385 | stmp = stp_mxmlElementGetAttr(prop, "value"); |
| 386 | outpaper->top = stp_xmlstrtodim(stmp); |
| 387 | } |
| 388 | if (!strcmp(prop_name, "unit")) |
| 389 | { |
| 390 | stmp = stp_mxmlElementGetAttr(prop, "value"); |
| 391 | if (stmp) |
| 392 | { |
| 393 | if (!strcmp(stmp, "english")) |
| 394 | outpaper->paper_unit = PAPERSIZE_ENGLISH_STANDARD; |
| 395 | else if (!strcmp(stmp, "english-extended")) |
| 396 | outpaper->paper_unit = PAPERSIZE_ENGLISH_EXTENDED; |
| 397 | else if (!strcmp(stmp, "metric")) |
| 398 | outpaper->paper_unit = PAPERSIZE_METRIC_STANDARD; |
| 399 | else if (!strcmp(stmp, "metric-extended")) |
| 400 | outpaper->paper_unit = PAPERSIZE_METRIC_EXTENDED; |
| 401 | /* Default unit */ |
| 402 | else |
| 403 | outpaper->paper_unit = PAPERSIZE_METRIC_EXTENDED; |
| 404 | unit = 1; |
| 405 | } |
| 406 | } |
| 407 | if (!strcmp(prop_name, "type")) |
| 408 | { |
| 409 | stmp = stp_mxmlElementGetAttr(prop, "value"); |
| 410 | if (stmp) |
| 411 | { |
| 412 | if (!strcmp(stmp, "envelope")) |
| 413 | outpaper->paper_size_type = PAPERSIZE_TYPE_ENVELOPE; |
| 414 | else |
| 415 | outpaper->paper_size_type = PAPERSIZE_TYPE_STANDARD; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | prop = prop->next; |
| 420 | } |
| 421 | if (id && name && width && height && unit) /* Margins and type are optional */ |
| 422 | return outpaper; |
| 423 | stp_free(outpaper); |
| 424 | outpaper = NULL; |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * Parse the <paperdef> node. |
| 430 | */ |
| 431 | static int |
| 432 | stp_xml_process_papersize_def(stp_mxml_node_t *paperdef, const char *file, |
| 433 | stp_papersize_list_t *papersize_list) |
| 434 | { |
| 435 | stp_mxml_node_t *paper; /* paper node pointer */ |
| 436 | stp_papersize_t *outpaper; /* Generated paper */ |
| 437 | |
| 438 | (void)file; |
| 439 | |
| 440 | paper = paperdef->child; |
| 441 | while (paper) |
| 442 | { |
| 443 | if (paper->type == STP_MXML_ELEMENT) |
| 444 | { |
| 445 | const char *papersize_name = paper->value.element.name; |
| 446 | if (!strcmp(papersize_name, "paper")) |
| 447 | { |
| 448 | outpaper = stp_xml_process_paper(paper); |
| 449 | if (outpaper) |
| 450 | stpi_papersize_create(papersize_list, outpaper); |
| 451 | } |
| 452 | } |
| 453 | paper = paper->next; |
| 454 | } |
| 455 | return 1; |
| 456 | } |
| 457 | |
| 458 | const stp_papersize_list_t * |
| 459 | stpi_get_papersize_list_named(const char *name, const char *file) |
| 460 | { |
| 461 | stp_list_item_t *item; |
| 462 | papersize_list_impl_t *impl; |
| 463 | |
| 464 | check_list_of_papersize_lists(); |
| 465 | item = stp_list_get_item_by_name(list_of_papersize_lists, name); |
| 466 | if (item) |
| 467 | { |
| 468 | impl = (papersize_list_impl_t *) stp_list_item_get_data(item); |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | char buf[MAXPATHLEN+1]; |
| 473 | stp_deprintf(STP_DBG_PAPER, "Loading paper list %s from %s\n", |
| 474 | name, file ? file : "(null)"); |
| 475 | if (! file) |
| 476 | return NULL; |
| 477 | else if (!strcmp(file, "")) |
| 478 | (void) snprintf(buf, MAXPATHLEN, "papers/%s.xml", name); |
| 479 | else |
| 480 | strncpy(buf, file, MAXPATHLEN); |
| 481 | stp_mxml_node_t *node = |
| 482 | stp_xml_parse_file_from_path_safe(buf, "paperdef", NULL); |
| 483 | const char *stmp = stp_mxmlElementGetAttr(node, "name"); |
| 484 | STPI_ASSERT(stmp && !strcmp(name, stmp), NULL); |
| 485 | impl = stp_malloc(sizeof(papersize_list_impl_t)); |
| 486 | impl->name = stp_strdup(name); |
| 487 | impl->list = stpi_create_papersize_list(); |
| 488 | stp_deprintf(STP_DBG_PAPER, " Loading %s\n", stmp); |
| 489 | stp_list_item_create(list_of_papersize_lists, NULL, impl); |
| 490 | stp_xml_process_papersize_def(node, buf, impl->list); |
| 491 | } |
| 492 | return impl->list; |
| 493 | } |
| 494 | |
| 495 | stp_papersize_list_t * |
| 496 | stpi_find_papersize_list_named(const char *name) |
| 497 | { |
| 498 | stp_list_item_t *item; |
| 499 | |
| 500 | check_list_of_papersize_lists(); |
| 501 | item = stp_list_get_item_by_name(list_of_papersize_lists, name); |
| 502 | if (item) |
| 503 | { |
| 504 | papersize_list_impl_t *impl = |
| 505 | (papersize_list_impl_t *) stp_list_item_get_data(item); |
| 506 | if (impl) |
| 507 | return impl->list; |
| 508 | } |
| 509 | return NULL; |
| 510 | } |
| 511 | |
| 512 | stp_papersize_list_t * |
| 513 | stpi_new_papersize_list(const char *name) |
| 514 | { |
| 515 | stp_list_item_t *item; |
| 516 | papersize_list_impl_t *impl; |
| 517 | |
| 518 | check_list_of_papersize_lists(); |
| 519 | item = stp_list_get_item_by_name(list_of_papersize_lists, name); |
| 520 | if (item) |
| 521 | return NULL; |
| 522 | impl = stp_malloc(sizeof(papersize_list_impl_t)); |
| 523 | impl->name = stp_strdup(name); |
| 524 | impl->list = stpi_create_papersize_list(); |
| 525 | stp_list_item_create(list_of_papersize_lists, NULL, impl); |
| 526 | return impl->list; |
| 527 | } |
| 528 | |
| 529 | const stp_papersize_list_t * |
| 530 | stpi_get_standard_papersize_list(void) |
| 531 | { |
| 532 | return stpi_get_papersize_list_named("standard", ""); |
| 533 | } |