From 1040f1b07033b430a8fb0531adc496488640120f Mon Sep 17 00:00:00 2001 From: Cody Wyatt Neiman Date: Sat, 21 Nov 2026 17:07:53 -0500 Subject: [PATCH] ppd-generator: let "stackingorder" decide the output order The generator wrote *DefaultOutputOrder and *PageStackOrder as Reverse only if exactly one of "pagedelivery=faceUp" and "stackingorder=lastToFirst" was in "printer-output-tray", treating them as two independent inversions. They are not: "stackingorder" is prtOutputStackingOrder of the Printer MIB (RFC 3805), where "lastToFirst" means that the back of each page is placed against the front of the previous page, which is what a face-up bin does. So a face-up bin reporting both, like the one of an Epson ET-4950, got Normal and printed the last page on top. Decide by "stackingorder" and use "pagedelivery" or the bin name only without it, as cfIPPReverseOutput() in libcupsfilters does. The PPD generator of CUPS also uses "stackingorder" alone. Both places now use the same helper. Add tests for face-up/lastToFirst, face-down/firstToLast, a "stackingorder" contradicting the bin name, and "pagedelivery" alone. The first and third fail without this change. --- ppd/ppd-generator.c | 103 +++++++++++++++++++-------------------- ppd/test_ppd_generator.c | 101 +++++++++++++++++++++++++++++++++++--- 2 files changed, 143 insertions(+), 61 deletions(-) diff --git a/ppd/ppd-generator.c b/ppd/ppd-generator.c index 3e288d6f..d4502cfc 100644 --- a/ppd/ppd-generator.c +++ b/ppd/ppd-generator.c @@ -72,6 +72,7 @@ typedef struct _ppd_size_s // **** Media Size (cups_size_t of lib static int http_connect(http_t **http, const char *url, char *resource, size_t ressize); +static int ppd_output_bin_reverse(const char *properties, const char *keyword); static void ppd_put_string(cups_file_t *fp, cups_lang_t *lang, const char *ppd_option, const char *ppd_choice, const char *pwg_msgid); @@ -335,9 +336,7 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer char outbin_properties[1024]; cups_len_t octet_str_len; void *outbin_properties_octet; - int outputorderinfofound = 0, - faceupdown = 1, - firsttolast = 1; + int reverse; // Reverse output order for bin? int manual_copies = -1, is_fax = 0; @@ -527,6 +526,7 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer break; } } + outbin_properties[0] = '\0'; if ((attr = ippFindAttribute(supported, "printer-output-tray", IPP_TAG_STRING)) != NULL && i < ippGetCount(attr)) @@ -536,23 +536,12 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer memcpy(outbin_properties, outbin_properties_octet, ((size_t)octet_str_len < sizeof(outbin_properties) - 1 ? (size_t)octet_str_len : sizeof(outbin_properties) - 1)); - if (strcasestr(outbin_properties, "pagedelivery=faceUp")) - { - outputorderinfofound = 1; - faceupdown = -1; - } - if (strcasestr(outbin_properties, "stackingorder=lastToFirst")) - firsttolast = -1; } - if (outputorderinfofound == 0 && defaultoutbin && - strcasestr(defaultoutbin, "face-up")) - faceupdown = -1; - if (defaultoutbin) - free (defaultoutbin); - if (firsttolast * faceupdown < 0) + if (ppd_output_bin_reverse(outbin_properties, defaultoutbin) == 1) cupsFilePuts(fp, "*DefaultOutputOrder: Reverse\n"); else cupsFilePuts(fp, "*DefaultOutputOrder: Normal\n"); + free(defaultoutbin); // Do we have a color printer? if (((attr = ippFindAttribute(supported, @@ -1917,9 +1906,7 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer printer_opt_strings_catalog); cupsFilePrintf(fp, "*OutputBin %s: \"\"\n", ppdname); ppd_put_string(fp, lang, "OutputBin", ppdname, human_readable); - outputorderinfofound = 0; - faceupdown = 1; - firsttolast = 1; + outbin_properties[0] = '\0'; if (attr2 && i < ippGetCount(attr2)) { outbin_properties_octet = ippGetOctetString(attr2, i, &octet_str_len); @@ -1927,43 +1914,10 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer memcpy(outbin_properties, outbin_properties_octet, ((size_t)octet_str_len < sizeof(outbin_properties) - 1 ? (size_t)octet_str_len : sizeof(outbin_properties) - 1)); - if (strcasestr(outbin_properties, "pagedelivery=faceUp")) - { - outputorderinfofound = 1; - faceupdown = -1; - } - else if (strcasestr(outbin_properties, "pagedelivery=faceDown")) - { - outputorderinfofound = 1; - faceupdown = 1; - } - if (strcasestr(outbin_properties, "stackingorder=lastToFirst")) - { - outputorderinfofound = 1; - firsttolast = -1; - } - else if (strcasestr(outbin_properties, "stackingorder=firstToLast")) - { - outputorderinfofound = 1; - firsttolast = 1; - } - } - if (outputorderinfofound == 0) { - if (strcasestr(keyword, "face-up")) - { - outputorderinfofound = 1; - faceupdown = -1; - } - if (strcasestr(keyword, "face-down")) - { - outputorderinfofound = 1; - faceupdown = 1; - } } - if (outputorderinfofound) + if ((reverse = ppd_output_bin_reverse(outbin_properties, keyword)) >= 0) cupsFilePrintf(fp, "*PageStackOrder %s: %s\n", - ppdname, - (firsttolast * faceupdown < 0 ? "Reverse" : "Normal")); + ppdname, (reverse ? "Reverse" : "Normal")); } cupsFilePuts(fp, "*CloseUI: *OutputBin\n"); } @@ -2968,6 +2922,47 @@ http_connect(http_t **http, // IO - Current HTTP connection } +// +// 'ppd_output_bin_reverse()' - Check whether an output bin needs the pages +// in reverse order. +// +// "stackingorder" in "printer-output-tray" is prtOutputStackingOrder of +// the Printer MIB (RFC 3805): with "lastToFirst" the back of each page is +// placed against the front of the previous page, so the pages have to be +// sent in reverse order, whether they are delivered face-up or face-down. +// Only without it "pagedelivery" or the bin name are used, as in +// cfIPPReverseOutput() of libcupsfilters. +// + +static int // O - 1 = reverse, 0 = normal, -1 = unknown +ppd_output_bin_reverse( + const char *properties, // I - "printer-output-tray" value or "" + const char *keyword) // I - "output-bin" keyword or NULL +{ + if (properties) + { + if (strcasestr(properties, "stackingorder=lastToFirst")) + return (1); + if (strcasestr(properties, "stackingorder=firstToLast")) + return (0); + if (strcasestr(properties, "pagedelivery=faceUp")) + return (1); + if (strcasestr(properties, "pagedelivery=faceDown")) + return (0); + } + + if (keyword) + { + if (strcasestr(keyword, "face-up")) + return (1); + if (strcasestr(keyword, "face-down")) + return (0); + } + + return (-1); +} + + /* * 'ppd_put_strings()' - Write localization attributes to a PPD file. */ diff --git a/ppd/test_ppd_generator.c b/ppd/test_ppd_generator.c index 949f80bf..0c74c808 100644 --- a/ppd/test_ppd_generator.c +++ b/ppd/test_ppd_generator.c @@ -6,7 +6,7 @@ // Licensed under Apache License v2.0. See the file "LICENSE" for more // information. // -// Tests covered (48 assertions across 13 groups): +// Tests covered (54 assertions across 13 groups): // // Group 1 (T01-T05) NULL / argument guards and smoke test. // ppdCreatePPDFromIPP2 returns NULL with errno @@ -42,9 +42,9 @@ // remainder is also stripped (line 363). // // Group 4 (T11-T13) Output order + colour + landscape. A default -// output bin whose name contains "face-up" -// flips faceupdown and yields *DefaultOutputOrder -// Reverse (line 467). IPP color-supported=True +// output bin whose name contains "face-up" (no +// printer-output-tray) yields *DefaultOutputOrder +// Reverse. IPP color-supported=True // emits *ColorDevice True (line 477); without it // AND a 0 color arg → *ColorDevice False (line // 479). IPP landscape-orientation-requested- @@ -128,6 +128,14 @@ // unconditionally after the if-chain). NULL // status_msg with size 0 must not crash. // +// Group 13 (T47-T50) Output order from printer-output-tray. +// "stackingorder" (prtOutputStackingOrder, RFC +// 3805) decides *DefaultOutputOrder and +// *PageStackOrder; "pagedelivery" and the bin +// name only when it is missing. A face-up bin +// with "stackingorder=lastToFirst" (as reported +// by e.g. an Epson ET-4950) needs Reverse. +// // Design notes: // // * Every test builds its IPP attribute set from scratch via ippNew(), @@ -219,6 +227,20 @@ add_format(ipp_t *resp, const char *mime) "document-format-supported", NULL, mime); } +// Add a single (default) output bin, optionally with its +// "printer-output-tray" value. +static void +add_output_bin(ipp_t *resp, const char *keyword, const char *tray) +{ + ippAddString(resp, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, + "output-bin-default", NULL, keyword); + ippAddString(resp, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, + "output-bin-supported", NULL, keyword); + if (tray) + ippAddOctetString(resp, IPP_TAG_PRINTER, "printer-output-tray", + tray, (int)strlen(tray)); +} + // Build an "absolute minimum" valid IPP attribute set (just enough to // pass the PDL gate at line 974). The PDF clause sets manual_copies=0 // and is_pdf=1, so no *cupsManualCopies appears in the output PPD. @@ -512,9 +534,8 @@ main(void) // Group 4: Output order, colour, landscape (T11-T13) // ========================================================================= - // T11 — Default output bin "face-up" sets faceupdown=-1 at line 463 - // → firsttolast*faceupdown<0 at line 467 → "*DefaultOutputOrder: - // Reverse". + // T11 — Default output bin "face-up" without printer-output-tray: + // the bin name decides → "*DefaultOutputOrder: Reverse". testBegin("output-bin-default \"face-up\" → *DefaultOutputOrder Reverse"); resp = make_pdf_ipp(); ippAddString(resp, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, @@ -1196,5 +1217,71 @@ main(void) ippDelete(resp); + // ========================================================================= + // Group 13: Output order from printer-output-tray (T47-T50) + // ========================================================================= + + // T47 — Face-up bin reporting "stackingorder=lastToFirst" (Epson ET-4950). + // Both values describe the same stack, they must not cancel out. + testBegin("face-up bin, stackingorder=lastToFirst → Reverse"); + resp = make_pdf_ipp(); + add_output_bin(resp, "face-up", "type=unRemovableBin;stackingorder=lastToFirst;pagedelivery=faceUp;"); + result = ppdCreatePPDFromIPP2(buffer, sizeof(buffer), resp, + "Test", "application/pdf", + 0, 0, NULL, NULL, NULL, NULL, NULL, 0); + ppd_text = result ? slurp_file(buffer) : NULL; + testEnd(ppd_text && + strstr(ppd_text, "*DefaultOutputOrder: Reverse\n") != NULL && + strstr(ppd_text, "*PageStackOrder FaceUp: Reverse\n") != NULL); + if (result == buffer) unlink(buffer); + free(ppd_text); + ippDelete(resp); + + // T48 — Usual face-down bin. + testBegin("face-down bin, stackingorder=firstToLast → Normal"); + resp = make_pdf_ipp(); + add_output_bin(resp, "face-down", "type=unRemovableBin;stackingorder=firstToLast;pagedelivery=faceDown;"); + result = ppdCreatePPDFromIPP2(buffer, sizeof(buffer), resp, + "Test", "application/pdf", + 0, 0, NULL, NULL, NULL, NULL, NULL, 0); + ppd_text = result ? slurp_file(buffer) : NULL; + testEnd(ppd_text && + strstr(ppd_text, "*DefaultOutputOrder: Normal\n") != NULL && + strstr(ppd_text, "*PageStackOrder FaceDown: Normal\n") != NULL); + if (result == buffer) unlink(buffer); + free(ppd_text); + ippDelete(resp); + + // T49 — "stackingorder" decides, not "pagedelivery" or the bin name. + testBegin("stackingorder=firstToLast wins over a face-up bin → Normal"); + resp = make_pdf_ipp(); + add_output_bin(resp, "face-up", "type=unRemovableBin;stackingorder=firstToLast;pagedelivery=faceUp;"); + result = ppdCreatePPDFromIPP2(buffer, sizeof(buffer), resp, + "Test", "application/pdf", + 0, 0, NULL, NULL, NULL, NULL, NULL, 0); + ppd_text = result ? slurp_file(buffer) : NULL; + testEnd(ppd_text && + strstr(ppd_text, "*DefaultOutputOrder: Normal\n") != NULL && + strstr(ppd_text, "*PageStackOrder FaceUp: Normal\n") != NULL); + if (result == buffer) unlink(buffer); + free(ppd_text); + ippDelete(resp); + + // T50 — Without "stackingorder", "pagedelivery" decides. + testBegin("pagedelivery=faceUp without stackingorder → Reverse"); + resp = make_pdf_ipp(); + add_output_bin(resp, "face-up", "type=unRemovableBin;pagedelivery=faceUp;"); + result = ppdCreatePPDFromIPP2(buffer, sizeof(buffer), resp, + "Test", "application/pdf", + 0, 0, NULL, NULL, NULL, NULL, NULL, 0); + ppd_text = result ? slurp_file(buffer) : NULL; + testEnd(ppd_text && + strstr(ppd_text, "*DefaultOutputOrder: Reverse\n") != NULL && + strstr(ppd_text, "*PageStackOrder FaceUp: Reverse\n") != NULL); + if (result == buffer) unlink(buffer); + free(ppd_text); + ippDelete(resp); + + return (testsPassed ? 0 : 1); }