X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fcups.git;a=blobdiff_plain;f=cups%2Fdest-localization.c;fp=cups%2Fdest-localization.c;h=f2bb9eb066ae93de5abb78b3fa2d68ce1bde91b4;hp=9c3cfae15061538fc4c16a09a55d69208cee4eb7;hb=1a18c85cd15c6af88ec2c3a5ece0d67add6f56a7;hpb=b60086f8ae7ff126ecb97cf6d400d6d4a5c6f571 diff --git a/cups/dest-localization.c b/cups/dest-localization.c index 9c3cfae15..f2bb9eb06 100644 --- a/cups/dest-localization.c +++ b/cups/dest-localization.c @@ -1,28 +1,17 @@ /* - * "$Id: dest-localization.c 4216 2013-03-11 13:57:36Z msweet $" + * "$Id: dest-localization.c 11894 2014-05-23 03:18:00Z msweet $" * - * Destination localization support for CUPS. + * Destination localization support for CUPS. * - * Copyright 2012-2013 by Apple Inc. + * Copyright 2012-2014 by Apple Inc. * - * These coded instructions, statements, and computer programs are the - * property of Apple Inc. and are protected by Federal copyright - * law. Distribution and use rights are outlined in the file "LICENSE.txt" - * which should have been included with this file. If this file is - * file is missing or damaged, see the license at "http://www.cups.org/". + * These coded instructions, statements, and computer programs are the + * property of Apple Inc. and are protected by Federal copyright + * law. Distribution and use rights are outlined in the file "LICENSE.txt" + * which should have been included with this file. If this file is + * file is missing or damaged, see the license at "http://www.cups.org/". * - * This file is subject to the Apple OS-Developed Software exception. - * - * Contents: - * - * cupsLocalizeDestOption() - Get the localized string for a destination - * option. - * cupsLocalizeDestValue() - Get the localized string for a destination - * option+value pair. - * cups_create_localizations() - Create the localizations array for a - * destination. - * cups_read_strings() - Read a pair of strings from a .strings file. - * cups_scan_strings() - Scan a quoted string. + * This file is subject to the Apple OS-Developed Software exception. */ /* @@ -42,6 +31,170 @@ static int cups_read_strings(cups_file_t *fp, char *buffer, size_t bufsize, static char *cups_scan_strings(char *buffer); +/* + * 'cupsLocalizeDestMedia()' - Get the localized string for a destination media + * size. + * + * The returned string is stored in the destination information and will become + * invalid if the destination information is deleted. + * + * @since CUPS 2.0@ + */ + +const char * /* O - Localized string */ +cupsLocalizeDestMedia( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dinfo_t *dinfo, /* I - Destination information */ + unsigned flags, /* I - Media flags */ + cups_size_t *size) /* I - Media size */ +{ + cups_lang_t *lang; /* Standard localizations */ + _cups_message_t key, /* Search key */ + *match; /* Matching entry */ + pwg_media_t *pwg; /* PWG media information */ + cups_array_t *db; /* Media database */ + _cups_media_db_t *mdb; /* Media database entry */ + char name[1024], /* Size name */ + temp[256]; /* Temporary string */ + const char *lsize, /* Localized media size */ + *lsource, /* Localized media source */ + *ltype; /* Localized media type */ + + + /* + * Range check input... + */ + + if (!http || !dest || !dinfo || !size) + { + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); + return (NULL); + } + + /* + * See if the localization is cached... + */ + + if (!dinfo->localizations) + cups_create_localizations(http, dinfo); + + key.id = size->media; + if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL) + return (match->str); + + /* + * If not, get the localized size, source, and type strings... + */ + + lang = cupsLangDefault(); + pwg = pwgMediaForSize(size->width, size->length); + + if (pwg->ppd) + lsize = _cupsLangString(lang, pwg->ppd); + else + lsize = NULL; + + if (!lsize) + { + if ((size->width % 635) == 0 && (size->length % 635) == 0) + { + /* + * Use inches since the size is a multiple of 1/4 inch. + */ + + snprintf(temp, sizeof(temp), _cupsLangString(lang, _("%g x %g")), size->width / 2540.0, size->length / 2540.0); + } + else + { + /* + * Use millimeters since the size is not a multiple of 1/4 inch. + */ + + snprintf(temp, sizeof(temp), _cupsLangString(lang, _("%d x %d mm")), (size->width + 50) / 100, (size->length + 50) / 100); + } + + lsize = temp; + } + + if (flags & CUPS_MEDIA_FLAGS_READY) + db = dinfo->ready_db; + else + db = dinfo->media_db; + + DEBUG_printf(("1cupsLocalizeDestMedia: size->media=\"%s\"", size->media)); + + for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db)) + { + if (mdb->key && !strcmp(mdb->key, size->media)) + break; + else if (mdb->size_name && !strcmp(mdb->size_name, size->media)) + break; + } + + if (!mdb) + { + for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db)) + { + if (mdb->width == size->width && mdb->length == size->length && mdb->bottom == size->bottom && mdb->left == size->left && mdb->right == size->right && mdb->top == size->top) + break; + } + } + + if (mdb) + { + DEBUG_printf(("1cupsLocalizeDestMedia: MATCH mdb%p [key=\"%s\" size_name=\"%s\" source=\"%s\" type=\"%s\" width=%d length=%d B%d L%d R%d T%d]", mdb, mdb->key, mdb->size_name, mdb->source, mdb->type, mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top)); + + lsource = cupsLocalizeDestValue(http, dest, dinfo, "media-source", mdb->source); + ltype = cupsLocalizeDestValue(http, dest, dinfo, "media-type", mdb->type); + } + else + { + lsource = NULL; + ltype = NULL; + } + + if (!lsource && !ltype) + { + if (size->bottom || size->left || size->right || size->top) + snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (Borderless)")), lsize); + else + strlcpy(name, lsize, sizeof(name)); + } + else if (!lsource) + { + if (size->bottom || size->left || size->right || size->top) + snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (Borderless, %s)")), lsize, ltype); + else + snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (%s)")), lsize, ltype); + } + else if (!ltype) + { + if (size->bottom || size->left || size->right || size->top) + snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (Borderless, %s)")), lsize, lsource); + else + snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (%s)")), lsize, lsource); + } + else + { + if (size->bottom || size->left || size->right || size->top) + snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (Borderless, %s, %s)")), lsize, ltype, lsource); + else + snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (%s, %s)")), lsize, ltype, lsource); + } + + if ((match = (_cups_message_t *)calloc(1, sizeof(_cups_message_t))) == NULL) + return (NULL); + + match->id = strdup(size->media); + match->str = strdup(name); + + cupsArrayAdd(dinfo->localizations, match); + + return (match->str); +} + + /* * 'cupsLocalizeDestOption()' - Get the localized string for a destination * option. @@ -356,8 +509,7 @@ cups_scan_strings(char *buffer) /* I - Start of string */ * Decode \nnn octal escape... */ - *bufptr = ((((bufptr[1] - '0') << 3) | (bufptr[2] - '0')) << 3) | - (bufptr[3] - '0'); + *bufptr = (char)(((((bufptr[1] - '0') << 3) | (bufptr[2] - '0')) << 3) | (bufptr[3] - '0')); _cups_strcpy(bufptr + 1, bufptr + 4); } else @@ -383,5 +535,5 @@ cups_scan_strings(char *buffer) /* I - Start of string */ /* - * End of "$Id: dest-localization.c 4216 2013-03-11 13:57:36Z msweet $". + * End of "$Id: dest-localization.c 11894 2014-05-23 03:18:00Z msweet $". */