8ae51f
diff -pruN paps-0.6.8.orig/configure.in paps-0.6.8/configure.in
8ae51f
--- paps-0.6.8.orig/configure.in	2007-01-19 20:06:10.000000000 +0900
8ae51f
+++ paps-0.6.8/configure.in	2008-11-07 08:21:19.000000000 +0900
8ae51f
@@ -7,6 +7,19 @@ AC_LANG_C
8ae51f
 AC_PROG_CC
8ae51f
 AM_PROG_LIBTOOL
8ae51f
 
8ae51f
+dnl ======================================================
8ae51f
+dnl check for CUPS
8ae51f
+dnl ======================================================
8ae51f
+AC_PATH_PROG(CUPS_CONFIG, cups-config, no)
8ae51f
+if test "$CUPS_CONFIG" = "no"; then
8ae51f
+  AC_MSG_ERROR([Please install cups development packages/files])
8ae51f
+fi
8ae51f
+CUPS_CFLAGS=`$CUPS_CONFIG --cflags | sed -e 's/-O[0-9]*//' -e 's/-m[^\t]*//g'`
8ae51f
+CUPS_LIBS=`$CUPS_CONFIG --libs`
8ae51f
+
8ae51f
+AC_SUBST(CUPS_CFLAGS)
8ae51f
+AC_SUBST(CUPS_LIBS)
8ae51f
+
8ae51f
 DX_HTML_FEATURE(ON)
8ae51f
 DX_CHM_FEATURE(OFF)
8ae51f
 DX_CHI_FEATURE(OFF)
8ae51f
diff -pruN paps-0.6.8.orig/src/Makefile.am paps-0.6.8/src/Makefile.am
8ae51f
--- paps-0.6.8.orig/src/Makefile.am	2008-11-07 08:21:05.000000000 +0900
8ae51f
+++ paps-0.6.8/src/Makefile.am	2008-11-07 08:21:19.000000000 +0900
8ae51f
@@ -5,10 +5,10 @@ libpapsinc_HEADERS = libpaps.h
8ae51f
 libpapsincdir = $(includedir)
8ae51f
 
8ae51f
 bin_PROGRAMS = paps
8ae51f
-paps_CFLAGS  = -Wall 
8ae51f
+paps_CFLAGS  = -Wall $(CUPS_CFLAGS)
8ae51f
 paps_SOURCES = paps.c
8ae51f
 paps_LDADD = $(lib_LTLIBRARIES) $(all_libraries)
8ae51f
-paps_LDFLAGS = `pkg-config --libs pangoft2`
8ae51f
+paps_LDFLAGS = `pkg-config --libs pangoft2` $(CUPS_LIBS)
8ae51f
 paps_DEPENDENCIES = $(lib_LTLIBRARIES)
8ae51f
 
8ae51f
 EXTRA_DIST = test_libpaps.c paps.1
8ae51f
diff -pruN paps-0.6.8.orig/src/paps.c paps-0.6.8/src/paps.c
8ae51f
--- paps-0.6.8.orig/src/paps.c	2008-11-07 08:21:05.000000000 +0900
8ae51f
+++ paps-0.6.8/src/paps.c	2008-11-07 08:26:28.000000000 +0900
8ae51f
@@ -31,6 +31,8 @@
8ae51f
 #include <string.h>
8ae51f
 #include <time.h>
8ae51f
 #include <locale.h>
8ae51f
+#include <cups/cups.h>
8ae51f
+#include <cups/ppd.h>
8ae51f
 
8ae51f
 #define BUFSIZE 1024
8ae51f
 #define DEFAULT_FONT_FAMILY	"Monospace"
8ae51f
@@ -86,9 +88,11 @@ typedef struct {
8ae51f
   gboolean do_wordwrap;
8ae51f
   gboolean do_use_markup;
8ae51f
   gboolean do_stretch_chars;
8ae51f
+  gboolean cups_mode;
8ae51f
   PangoDirection pango_dir;
8ae51f
   gchar *filename;
8ae51f
   gchar *header_font_desc;
8ae51f
+  gchar *owner;
8ae51f
   gint lpi;
8ae51f
   gint cpi;
8ae51f
 } page_layout_t;
8ae51f
@@ -320,8 +324,8 @@ int main(int argc, char *argv[])
8ae51f
   int num_pages = 1;
8ae51f
   int gutter_width = 40;
8ae51f
   int total_gutter_width;
8ae51f
-  int page_width = paper_sizes[0].width;
8ae51f
-  int page_height = paper_sizes[0].height;
8ae51f
+  int page_width = -1;
8ae51f
+  int page_height = -1;
8ae51f
   int do_tumble = -1;   /* -1 means not initialized */
8ae51f
   int do_duplex = -1;
8ae51f
   gchar *paps_header = NULL;
8ae51f
@@ -331,6 +335,8 @@ int main(int argc, char *argv[])
8ae51f
   int max_width = 0, w;
8ae51f
   GIConv cvh = NULL;
8ae51f
   GOptionGroup *options;
8ae51f
+  gboolean cups_mode = FALSE;
8ae51f
+  gchar *page_owner = NULL;
8ae51f
 
8ae51f
   /* Set locale from environment. */
8ae51f
   setlocale(LC_ALL, "");
8ae51f
@@ -348,6 +354,130 @@ int main(int argc, char *argv[])
8ae51f
   g_option_context_add_main_entries(ctxt, entries, NULL);
8ae51f
 #endif
8ae51f
   
8ae51f
+  /* check if the process is being invoked as CUPS filter */
8ae51f
+  G_STMT_START {
8ae51f
+	gchar *prgname = g_path_get_basename(argv[0]);
8ae51f
+	cups_option_t *options = NULL;
8ae51f
+	ppd_file_t *ppd;
8ae51f
+	ppd_size_t *pagesize;
8ae51f
+	int num_options;
8ae51f
+	const char *val;
8ae51f
+
8ae51f
+	if (strncmp(prgname, "texttopaps", 10) == 0 ||
8ae51f
+	    getenv("CUPS_SERVER") != NULL) {
8ae51f
+		g_set_prgname(prgname);
8ae51f
+		/* argument format should be job-id user title copies options [file] */
8ae51f
+		cups_mode = TRUE;
8ae51f
+		/* set default values */
8ae51f
+		page_layout.lpi = 6.0L;
8ae51f
+		page_layout.cpi = 10.0L;
8ae51f
+		left_margin = 18;
8ae51f
+		right_margin = 18;
8ae51f
+		top_margin = 36;
8ae51f
+		bottom_margin = 36;
8ae51f
+		page_width = 612;
8ae51f
+		page_height = 792;
8ae51f
+		font = g_strdup(MAKE_FONT_NAME ("Courier", DEFAULT_FONT_SIZE));
8ae51f
+		header_font_desc = g_strdup(MAKE_FONT_NAME ("Courier", HEADER_FONT_SCALE));
8ae51f
+
8ae51f
+		if (argc < 6 || argc > 7) {
8ae51f
+			fprintf(stderr, "ERROR: %s job-id user title copies options [file]\n", prgname);
8ae51f
+			exit(1);
8ae51f
+		}
8ae51f
+		if (argc == 6) {
8ae51f
+			filename_in = "stdin";
8ae51f
+			IN = stdin;
8ae51f
+		} else {
8ae51f
+			filename_in = argv[6];
8ae51f
+			if ((IN = fopen(filename_in, "rb")) == NULL) {
8ae51f
+				fprintf(stderr, "ERROR: unable to open print file -\n");
8ae51f
+				exit(1);
8ae51f
+			}
8ae51f
+		}
8ae51f
+		title = argv[3];
8ae51f
+		page_owner = argv[2];
8ae51f
+		num_options = cupsParseOptions(argv[5], 0, &options);
8ae51f
+
8ae51f
+		if ((val = cupsGetOption("prettyprint", num_options, options)) != NULL &&
8ae51f
+		    g_ascii_strcasecmp(val, "no") &&
8ae51f
+		    g_ascii_strcasecmp(val, "off") &&
8ae51f
+		    g_ascii_strcasecmp(val, "false")) {
8ae51f
+			/* XXX: need to support the keywords highlighting */
8ae51f
+		}
8ae51f
+		ppd = ppdOpenFile(getenv("PPD"));
8ae51f
+		ppdMarkDefaults(ppd);
8ae51f
+		cupsMarkOptions(ppd, num_options, options);
8ae51f
+
8ae51f
+		if ((pagesize = ppdPageSize(ppd, NULL)) != NULL) {
8ae51f
+			page_width = pagesize->width;
8ae51f
+			page_height = pagesize->length;
8ae51f
+			top_margin = pagesize->length - pagesize->top;
8ae51f
+			bottom_margin = pagesize->bottom;
8ae51f
+			left_margin = pagesize->left;
8ae51f
+			right_margin = pagesize->width - pagesize->right;
8ae51f
+		}
8ae51f
+
8ae51f
+		if ((val = cupsGetOption("landscape", num_options, options)) != NULL) {
8ae51f
+			if (g_ascii_strcasecmp(val, "no") &&
8ae51f
+			    g_ascii_strcasecmp(val, "off") &&
8ae51f
+			    g_ascii_strcasecmp(val, "false"))
8ae51f
+				do_landscape = TRUE;
8ae51f
+		}
8ae51f
+		/* XXX: need to support orientation-requested? */
8ae51f
+		if ((val = cupsGetOption("page-left", num_options, options)) != NULL) {
8ae51f
+			left_margin = (int)atof(val);
8ae51f
+		}
8ae51f
+		if ((val = cupsGetOption("page-right", num_options, options)) != NULL) {
8ae51f
+			right_margin = (int)atof(val);
8ae51f
+		}
8ae51f
+		if ((val = cupsGetOption("page-bottom", num_options, options)) != NULL) {
8ae51f
+			bottom_margin = (int)atof(val);
8ae51f
+		}
8ae51f
+		if ((val = cupsGetOption("page-top", num_options, options)) != NULL) {
8ae51f
+			top_margin = (int)atof(val);
8ae51f
+		}
8ae51f
+		if (ppdIsMarked(ppd, "Duplex", "DuplexNoTumble") ||
8ae51f
+		    ppdIsMarked(ppd, "Duplex", "DuplexTumble") ||
8ae51f
+		    ppdIsMarked(ppd, "JCLDuplex", "DuplexNoTumble") ||
8ae51f
+		    ppdIsMarked(ppd, "JCLDuplex", "DuplexTumble") ||
8ae51f
+		    ppdIsMarked(ppd, "EFDuplex", "DuplexNoTumble") ||
8ae51f
+		    ppdIsMarked(ppd, "EFDuplex", "DuplexTumble") ||
8ae51f
+		    ppdIsMarked(ppd, "KD03Duplex", "DuplexNoTumble") ||
8ae51f
+		    ppdIsMarked(ppd, "KD03Duplex", "DuplexTumble")) {
8ae51f
+			do_duplex = TRUE;
8ae51f
+		}
8ae51f
+		if ((val = cupsGetOption("wrap", num_options, options)) != NULL) {
8ae51f
+			do_wordwrap = !g_ascii_strcasecmp(val, "true") ||
8ae51f
+				      !g_ascii_strcasecmp(val, "on") ||
8ae51f
+				      !g_ascii_strcasecmp(val, "yes");
8ae51f
+		}
8ae51f
+		if ((val = cupsGetOption("columns", num_options, options)) != NULL) {
8ae51f
+			num_columns = atoi(val);
8ae51f
+		}
8ae51f
+		if ((val = cupsGetOption("cpi", num_options, options)) != NULL) {
8ae51f
+			page_layout.cpi = atof(val);
8ae51f
+		}
8ae51f
+		if ((val = cupsGetOption("lpi", num_options, options)) != NULL) {
8ae51f
+			page_layout.lpi = atof(val);
8ae51f
+		}
8ae51f
+		if (getenv("CHARSET") != NULL) {
8ae51f
+			char *charset = getenv("CHARSET");
8ae51f
+			/* Map CUPS charset names to real ones.
8ae51f
+			 * http://cups.org/newsgroups.php?s9797+gcups.general+v9797+T1
8ae51f
+			 */
8ae51f
+			if (!g_ascii_strcasecmp(charset, "windows-932")) {
8ae51f
+				charset = "WINDOWS-31J";
8ae51f
+			}
8ae51f
+			if (g_ascii_strcasecmp(charset, "utf-8") &&
8ae51f
+			    g_ascii_strcasecmp(charset, "utf8")) {
8ae51f
+				encoding = g_strdup(charset);
8ae51f
+			}
8ae51f
+		}
8ae51f
+	}
8ae51f
+  } G_STMT_END;
8ae51f
+
8ae51f
+  if (!cups_mode) {
8ae51f
+
8ae51f
   /* Parse command line */
8ae51f
   if (!g_option_context_parse(ctxt, &argc, &argv, &error))
8ae51f
     {
8ae51f
@@ -374,6 +504,8 @@ int main(int argc, char *argv[])
8ae51f
       IN = stdin;
8ae51f
     }
8ae51f
   title = filename_in;
8ae51f
+
8ae51f
+  } /* if (!cups_mode) */
8ae51f
   
8ae51f
   paps = paps_new();
8ae51f
   pango_context = paps_get_pango_context (paps);
8ae51f
@@ -392,8 +524,10 @@ int main(int argc, char *argv[])
8ae51f
   pango_context_set_font_description (pango_context, font_description);
8ae51f
 
8ae51f
   /* Page layout */
8ae51f
-  page_width = paper_sizes[(int)paper_type].width;
8ae51f
-  page_height = paper_sizes[(int)paper_type].height;
8ae51f
+  if (page_width < 0)
8ae51f
+    page_width = paper_sizes[(int)paper_type].width;
8ae51f
+  if (page_height < 0)
8ae51f
+    page_height = paper_sizes[(int)paper_type].height;
8ae51f
   
8ae51f
   if (num_columns == 1)
8ae51f
     total_gutter_width = 0;
8ae51f
@@ -456,6 +590,8 @@ int main(int argc, char *argv[])
8ae51f
   page_layout.pango_dir = pango_dir;
8ae51f
   page_layout.filename = filename_in;
8ae51f
   page_layout.header_font_desc = header_font_desc;
8ae51f
+  page_layout.owner = page_owner;
8ae51f
+  page_layout.cups_mode = cups_mode;
8ae51f
 
8ae51f
   /* calculate x-coordinate scale */
8ae51f
   if (page_layout.cpi > 0.0L)
8ae51f
@@ -756,6 +892,12 @@ split_text_into_paragraphs (PangoContext
8ae51f
           if (wc == (gunichar)-1)
8ae51f
             {
8ae51f
               fprintf (stderr, "%s: Invalid character in input\n", g_get_prgname ());
8ae51f
+              if (page_layout->cups_mode)
8ae51f
+                {
8ae51f
+                  /* try to continue parsing texts */
8ae51f
+                  p = next;
8ae51f
+                  continue;
8ae51f
+                }
8ae51f
               wc = 0;
8ae51f
             }
8ae51f
           if (!*p || !wc || wc == '\n' || wc == '\f')
8ae51f
@@ -925,21 +1067,32 @@ void print_postscript_header(FILE *OUT,
8ae51f
   int orientation = page_layout->page_width > page_layout->page_height;
8ae51f
   int bb_page_width = page_layout->page_width;
8ae51f
   int bb_page_height = page_layout->page_height;
8ae51f
+  char *owner = NULL;
8ae51f
   
8ae51f
   /* Keep bounding box non-rotated to make ggv happy */
8ae51f
-  if (orientation)
8ae51f
+  /* ensure the correct bounding box for CUPS */
8ae51f
+  if (orientation && !page_layout->cups_mode)
8ae51f
     {
8ae51f
       int tmp = bb_page_width;
8ae51f
       bb_page_width = bb_page_height;
8ae51f
       bb_page_height = tmp;
8ae51f
     }
8ae51f
   
8ae51f
+  if (page_layout->owner)
8ae51f
+    {
8ae51f
+      owner = g_strdup_printf("%%%%For: %s\n", page_layout->owner);
8ae51f
+    }
8ae51f
+  else
8ae51f
+    {
8ae51f
+      owner = g_strdup("");
8ae51f
+    }
8ae51f
   fprintf(OUT,
8ae51f
           "%%!PS-Adobe-3.0\n"
8ae51f
+	  "%s"
8ae51f
           "%%%%Title: %s\n"
8ae51f
           "%%%%Creator: paps version 0.6.7 by Dov Grobgeld\n"
8ae51f
           "%%%%Pages: (atend)\n"
8ae51f
-          "%%%%BoundingBox: 0 0 %d %d\n"
8ae51f
+          "%%%%BoundingBox: 0 0 %d %d\n%s"
8ae51f
           "%%%%BeginProlog\n"
8ae51f
           "%%%%Orientation: %s\n"
8ae51f
           "/papsdict 1 dict def\n"
8ae51f
@@ -961,7 +1114,7 @@ void print_postscript_header(FILE *OUT,
8ae51f
           "             pagewidth\n"
8ae51f
           "             /pagewidth pageheight def\n"
8ae51f
           "             /pageheight exch def\n"
8ae51f
-          "             /orientation 3 def\n"
8ae51f
+          "             /orientation %d def\n"
8ae51f
           "         } if\n"
8ae51f
           "         2 dict\n"
8ae51f
           "         dup /PageSize [pagewidth pageheight] put\n"
8ae51f
@@ -986,11 +1139,21 @@ void print_postscript_header(FILE *OUT,
8ae51f
           "  90 rotate\n"
8ae51f
           "  0 pageheight neg translate\n"
8ae51f
           "} def\n",
8ae51f
+	  /*
8ae51f
+	   * Put %%cupsRotation tag to prevent the rotation in pstops.
8ae51f
+	   * This breaks paps's behavior to make it in landscape say.
8ae51f
+	   * (RH#222137)
8ae51f
+	   */
8ae51f
+	  (page_layout->cups_mode ? "%cupsRotation: 0\n" : ""),
8ae51f
           title,
8ae51f
           bb_page_width,
8ae51f
           bb_page_height,
8ae51f
-          orientation_names[orientation]
8ae51f
+          owner,
8ae51f
+          orientation_names[orientation],
8ae51f
+          /* For landscape, rotate page to portrait orientation for CUPS (RH#222137) */
8ae51f
+          page_layout->cups_mode ? 2 : 3
8ae51f
           );
8ae51f
+  g_free(owner);
8ae51f
   
8ae51f
   fprintf(OUT,
8ae51f
           "%% User settings\n"