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