printing from command line patch

Emile Snyder <[email protected]>
Newsgroups gmane.comp.graphics.sodipodi
Message-ID <1075140490.2308.21.camel@basalt>
Hi,

If you get a chance, please look over the attached patch to enable
printing from the command line using the ps print module.  It's against
the CVS HEAD version of sodipodi as of the morning of Jan. 25.  Printing
to a file (sodipodi --print=">foo.ps" foo.svg), a pipe (sodipodi
--print="| cat > foo.ps" foo.svg) and a printer (sodipodi --print=lpr
foo.svg) work for me with no errors or warnings.

Some confusions:
* I can't seem to print to a file from the gui using the unmodified CVS
code.  The file is created, but viewing it in gv shows an empty page. 
(Tried with various of the lab hazard sign clip art.)

* Changing the dpi doesn't seem to do anything.  I have 300 dpi hard
coded in the modules/ps.c::setup_file() function, and changing to to
other values seemed to have no effect.

* Should I have made similar modifications to the other print systems
(gnome, kde, ...?)  If so, what determines which system is being used
when it's called from the command line?

Thanks for writing sodipodi!
-emile

On Wed, 2004-01-21 at 13:44, Lauris Kaplinski wrote:
> Hello!
> 
> Yes, unfotunately print-to-file got out of sync while moving printing
> to module.
> To enable it properly, ::setup_file or some similar method should
> be added to SPModulePrint, that sets printing module up to send
> output to file.
> Modules have also to be set up in main_console. It may require minor
> tweaking to work proiperly.
> 
> I'll try to look into it before next release.
> 
> Best wishes,
> Lauris Kaplinski
sodipodi-print.patch (text/plain, 13.9 KB)
cvs server: Diffing .
cvs server: Diffing build
cvs server: Diffing build/vc7
cvs server: Diffing debian
cvs server: Diffing doc
cvs server: Diffing extensions
cvs server: Diffing glade
cvs server: Diffing po
cvs server: Diffing samples
cvs server: Diffing src
Index: src/file.c
===================================================================
RCS file: /cvs/gnome/sodipodi/src/file.c,v
retrieving revision 1.93
diff -c -r1.93 file.c
*** src/file.c	2 Jan 2004 13:20:19 -0000	1.93
--- src/file.c	25 Jan 2004 18:07:33 -0000
***************
*** 508,514 ****
  {
  	SPDocument *doc;
  	doc = SP_ACTIVE_DOCUMENT;
! 	if (doc) sp_print_document (doc, FALSE);
  }
  
  void
--- 508,514 ----
  {
  	SPDocument *doc;
  	doc = SP_ACTIVE_DOCUMENT;
! 	if (doc) sp_print_document (doc, FALSE, NULL);
  }
  
  void
***************
*** 516,522 ****
  {
  	SPDocument *doc;
  	doc = SP_ACTIVE_DOCUMENT;
! 	if (doc) sp_print_document (doc, TRUE);
  }
  
  void
--- 516,522 ----
  {
  	SPDocument *doc;
  	doc = SP_ACTIVE_DOCUMENT;
! 	if (doc) sp_print_document (doc, TRUE, NULL);
  }
  
  void
Index: src/main.c
===================================================================
RCS file: /cvs/gnome/sodipodi/src/main.c,v
retrieving revision 1.93
diff -c -r1.93 main.c
*** src/main.c	2 Jan 2004 13:20:19 -0000	1.93
--- src/main.c	25 Jan 2004 18:07:36 -0000
***************
*** 343,348 ****
--- 343,360 ----
  	fl = sp_process_args (argc, argv);
  	if (stop) exit (1);
  
+ 	/* Start up g type system, without requiring X, do this before modules init */
+         g_type_init();
+         sodipodi_application_new();
+ 
+ #ifdef _DEBUG
+ 	g_print ("Initializing modules...\n");
+ #endif
+ 	sp_modules_init (&argc, argv, TRUE);
+ 
+ #ifdef _DEBUG
+ 	g_print ("Registering objects...\n");
+ #endif
  	/* fixme: Move these to some centralized location (Lauris) */
  	sp_object_type_register ("sodipodi:namedview", SP_TYPE_NAMEDVIEW);
  	sp_object_type_register ("sodipodi:guide", SP_TYPE_GUIDE);
***************
*** 354,374 ****
  
  	/* Check for and set up printing path */
  	printer = NULL;
! 	if (sp_global_printer != NULL) {
! 		if ((sp_global_printer[0] != '|') && (sp_global_printer[0] != '/')) {
! 			gchar *cwd;
! 			/* Gnome-print appends relative paths to $HOME by default */
! 			cwd = g_get_current_dir ();
! 			printer = g_build_filename (cwd, sp_global_printer, NULL);
! 			g_free (cwd);
! 		} else {
! 			printer = g_strdup (sp_global_printer);
! 		}
! 	}
  
- 	/* Start up g type system, without requiring X */
- 	g_type_init();
- 	sodipodi_application_new ();
  	sodipodi_load_preferences (sodipodi);
  	sodipodi_load_extensions (sodipodi);
  
--- 366,373 ----
  
  	/* Check for and set up printing path */
  	printer = NULL;
!         printer = g_strdup (sp_global_printer);
  
  	sodipodi_load_preferences (sodipodi);
  	sodipodi_load_extensions (sodipodi);
  
***************
*** 379,385 ****
  			g_warning ("Specified document %s cannot be opened (is it valid SVG file?)", (gchar *) fl->data);
  		} else {
  			if (printer) {
! 				sp_print_document_to_file (doc, printer);
  			}
  			if (sp_export_png) {
  				sp_do_export_png (doc);
--- 378,384 ----
  			g_warning ("Specified document %s cannot be opened (is it valid SVG file?)", (gchar *) fl->data);
  		} else {
  			if (printer) {
!                                 sp_print_document(doc, FALSE, printer);
  			}
  			if (sp_export_png) {
  				sp_do_export_png (doc);
Index: src/module.h
===================================================================
RCS file: /cvs/gnome/sodipodi/src/module.h,v
retrieving revision 1.8
diff -c -r1.8 module.h
*** src/module.h	15 Dec 2003 23:02:22 -0000	1.8
--- src/module.h	25 Jan 2004 18:07:36 -0000
***************
*** 181,186 ****
--- 181,187 ----
  
  	/* FALSE means user hit cancel */
  	unsigned int (* setup) (SPModulePrint *modp);
+         unsigned int (* setup_file) (SPModulePrint *modp, const unsigned char *filename);
  	unsigned int (* set_preview) (SPModulePrint *modp);
  
  	unsigned int (* begin) (SPModulePrint *modp, SPDocument *doc);
Index: src/print.c
===================================================================
RCS file: /cvs/gnome/sodipodi/src/print.c,v
retrieving revision 1.36
diff -c -r1.36 print.c
*** src/print.c	6 Dec 2003 23:01:25 -0000	1.36
--- src/print.c	25 Jan 2004 18:07:37 -0000
***************
*** 148,154 ****
  }
  
  void
! sp_print_document (SPDocument *doc, unsigned int direct)
  {
  	SPModulePrint *mod;
  	unsigned int ret;
--- 148,154 ----
  }
  
  void
! sp_print_document (SPDocument *doc, unsigned int direct, const unsigned char *filename)
  {
  	SPModulePrint *mod;
  	unsigned int ret;
***************
*** 169,176 ****
  	if (!mod) mod = (SPModulePrint *) sp_module_find (SP_MODULE_KEY_PRINT_PLAIN);
  
  	ret = FALSE;
! 	if (((SPModulePrintClass *) G_OBJECT_GET_CLASS (mod))->setup)
  		ret = ((SPModulePrintClass *) G_OBJECT_GET_CLASS (mod))->setup (mod);
  
  	if (ret) {
  		/* fixme: This has to go into module constructor somehow */
--- 169,181 ----
  	if (!mod) mod = (SPModulePrint *) sp_module_find (SP_MODULE_KEY_PRINT_PLAIN);
  
  	ret = FALSE;
!         if (NULL != filename) {
!             if (((SPModulePrintClass *) G_OBJECT_GET_CLASS (mod))->setup_file)
! 		ret = ((SPModulePrintClass *) G_OBJECT_GET_CLASS (mod))->setup_file (mod, filename);
!         } else {
!             if (((SPModulePrintClass *) G_OBJECT_GET_CLASS (mod))->setup)
  		ret = ((SPModulePrintClass *) G_OBJECT_GET_CLASS (mod))->setup (mod);
+         }
  
  	if (ret) {
  		/* fixme: This has to go into module constructor somehow */
***************
*** 197,245 ****
  	g_object_unref (G_OBJECT (mod));
  }
  
- void
- sp_print_document_to_file (SPDocument *doc, const unsigned char *filename)
- {
- #ifdef lalala
- #ifdef WITH_GNOME_PRINT
-         GnomePrintConfig *config;
- 	SPPrintContext ctx;
-         GnomePrintContext *gpc;
- 
- 	config = gnome_print_config_default ();
-         if (!gnome_print_config_set (config, "Settings.Engine.Backend.Driver", "gnome-print-ps")) return;
-         if (!gnome_print_config_set (config, "Settings.Transport.Backend", "file")) return;
-         if (!gnome_print_config_set (config, GNOME_PRINT_KEY_OUTPUT_FILENAME, filename)) return;
- 
- 	sp_document_ensure_up_to_date (doc);
- 
- 	gpc = gnome_print_context_new (config);
- 	ctx.gpc = gpc;
- 
- 	g_return_if_fail (gpc != NULL);
- 
- 	/* Print document */
- 	gnome_print_beginpage (gpc, SP_DOCUMENT_NAME (doc));
- 	gnome_print_translate (gpc, 0.0, sp_document_height (doc));
- 	/* From desktop points to document pixels */
- 	gnome_print_scale (gpc, 0.8, -0.8);
- 	sp_item_invoke_print (SP_ITEM (sp_document_root (doc)), &ctx);
-         gnome_print_showpage (gpc);
-         gnome_print_context_close (gpc);
- #else
- 	SPPrintContext ctx;
- 
- 	ctx.stream = fopen (filename, "w");
- 	if (ctx.stream) {
- 		sp_document_ensure_up_to_date (doc);
- 		fprintf (ctx.stream, "%g %g translate\n", 0.0, sp_document_height (doc));
- 		fprintf (ctx.stream, "0.8 -0.8 scale\n");
- 		sp_item_invoke_print (SP_ITEM (sp_document_root (doc)), &ctx);
- 		fprintf (ctx.stream, "showpage\n");
- 		fclose (ctx.stream);
- 	}
- #endif
- #endif
- }
  
  #endif
--- 202,206 ----
Index: src/print.h
===================================================================
RCS file: /cvs/gnome/sodipodi/src/print.h,v
retrieving revision 1.8
diff -c -r1.8 print.h
*** src/print.h	6 Dec 2003 23:01:25 -0000	1.8
--- src/print.h	25 Jan 2004 18:07:37 -0000
***************
*** 28,35 ****
  #ifndef WITHOUT_PRINT_UI
  /* UI frontend */
  void sp_print_preview_document (SPDocument *doc);
! void sp_print_document (SPDocument *doc, unsigned int direct);
! void sp_print_document_to_file (SPDocument *doc, const unsigned char *filename);
  #endif
  
  #endif
--- 28,34 ----
  #ifndef WITHOUT_PRINT_UI
  /* UI frontend */
  void sp_print_preview_document (SPDocument *doc);
! void sp_print_document (SPDocument *doc, unsigned int direct, const unsigned char *filename);
  #endif
  
  #endif
cvs server: Diffing src/bonobo
cvs server: Diffing src/dialogs
cvs server: Diffing src/display
cvs server: Diffing src/display/nr
cvs server: Diffing src/helper
cvs server: Diffing src/libarikkei
cvs server: Diffing src/libnr
cvs server: Diffing src/libnrtype
cvs server: Diffing src/modules
Index: src/modules/ps.c
===================================================================
RCS file: /cvs/gnome/sodipodi/src/modules/ps.c,v
retrieving revision 1.12
diff -c -r1.12 ps.c
*** src/modules/ps.c	22 Dec 2003 22:51:38 -0000	1.12
--- src/modules/ps.c	25 Jan 2004 18:07:40 -0000
***************
*** 60,65 ****
--- 60,66 ----
  static SPRepr *sp_module_print_plain_write (SPModule *module, SPRepr *root);
  
  static unsigned int sp_module_print_plain_setup (SPModulePrint *mod);
+ static unsigned int sp_module_print_plain_setup_file (SPModulePrint *mod, const unsigned char *filename);
  static unsigned int sp_module_print_plain_begin (SPModulePrint *mod, SPDocument *doc);
  static unsigned int sp_module_print_plain_finish (SPModulePrint *mod);
  static unsigned int sp_module_print_plain_bind (SPModulePrint *mod, const NRMatrixF *transform, float opacity);
***************
*** 114,119 ****
--- 115,121 ----
  	module_class->write = sp_module_print_plain_write;
  
  	module_print_class->setup = sp_module_print_plain_setup;
+ 	module_print_class->setup_file = sp_module_print_plain_setup_file;
  	module_print_class->begin = sp_module_print_plain_begin;
  	module_print_class->finish = sp_module_print_plain_finish;
  	module_print_class->bind = sp_module_print_plain_bind;
***************
*** 166,171 ****
--- 168,222 ----
  	return repr;
  }
  
+ 
+ unsigned int
+ private_sp_module_print_plain_setup_output (SPModulePrintPlain *pmod, const unsigned char *filename)
+ {
+         FILE *osf, *osp;
+         unsigned int ret;
+         
+         ret = FALSE;
+         osf = NULL;
+         osp = NULL;
+         if (filename) {
+                 if (*filename == '|') {
+                         filename += 1;
+                         while (isspace (*filename)) filename += 1;
+ #ifndef WIN32
+                         osp = popen (filename, "w");
+ #else
+                         osp = _popen (filename, "w");
+ #endif
+                         pmod->stream = osp;
+                 } else if (*filename == '>') {
+                         filename += 1;
+                         while (isspace (*filename)) filename += 1;
+                         osf = fopen (filename, "w+");
+                         pmod->stream = osf;
+                 } else {
+                         unsigned char *qn;
+                         qn = g_strdup_printf ("lpr -P %s", filename);
+ #ifndef WIN32
+                         osp = popen (qn, "w");
+ #else
+                         osp = _popen (qn, "w");
+ #endif
+                         g_free (qn);
+                         pmod->stream = osp;
+                 }
+         }
+         if (pmod->stream) {
+ #ifndef WIN32
+                 /* fixme: this is kinda icky */
+                 (void) signal(SIGPIPE, SIG_IGN);
+ #endif
+                 ret = TRUE;
+         }
+ 
+         return ret;
+ }
+ 
+ 
  static unsigned int
  sp_module_print_plain_setup (SPModulePrint *mod)
  {
***************
*** 280,286 ****
  	if (response == GTK_RESPONSE_OK) {
  		const unsigned char *fn;
  		const char *sstr;
! 		FILE *osf, *osp;
  		pmod->bitmap = gtk_toggle_button_get_active ((GtkToggleButton *) rb);
  		sstr = gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (combo)->entry));
  		pmod->dpi = MAX (atof (sstr), 1);
--- 331,337 ----
  	if (response == GTK_RESPONSE_OK) {
  		const unsigned char *fn;
  		const char *sstr;
! 		//FILE *osf, *osp;
  		pmod->bitmap = gtk_toggle_button_get_active ((GtkToggleButton *) rb);
  		sstr = gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (combo)->entry));
  		pmod->dpi = MAX (atof (sstr), 1);
***************
*** 292,339 ****
  			sp_repr_set_attr (repr, "resolution", sstr);
  			sp_repr_set_attr (repr, "destination", fn);
  		}
! 		osf = NULL;
! 		osp = NULL;
! 		if (fn) {
! 			if (*fn == '|') {
! 				fn += 1;
! 				while (isspace (*fn)) fn += 1;
! #ifndef WIN32
! 				osp = popen (fn, "w");
! #else
! 				osp = _popen (fn, "w");
! #endif
! 				pmod->stream = osp;
! 			} else if (*fn == '>') {
! 				fn += 1;
! 				while (isspace (*fn)) fn += 1;
! 				osf = fopen (fn, "w+");
! 				pmod->stream = osf;
! 			} else {
! 				unsigned char *qn;
! 				qn = g_strdup_printf ("lpr -P %s", fn);
! #ifndef WIN32
! 				osp = popen (qn, "w");
! #else
! 				osp = _popen (qn, "w");
! #endif
! 				g_free (qn);
! 				pmod->stream = osp;
! 			}
! 		}
! 		if (pmod->stream) {
! #ifndef WIN32
! 			/* fixme: this is kinda icky */
! 			(void) signal(SIGPIPE, SIG_IGN);
! #endif
! 			ret = TRUE;
! 		}
  	}
  
  	gtk_widget_destroy (dlg);
  
  	return ret;
  }
  
  static unsigned int
  sp_module_print_plain_begin (SPModulePrint *mod, SPDocument *doc)
--- 343,384 ----
  			sp_repr_set_attr (repr, "resolution", sstr);
  			sp_repr_set_attr (repr, "destination", fn);
  		}
!                 ret = private_sp_module_print_plain_setup_output(pmod, fn);
  	}
  
  	gtk_widget_destroy (dlg);
  
  	return ret;
  }
+ 
+ 
+ static unsigned int
+ sp_module_print_plain_setup_file (SPModulePrint *mod, const unsigned char *filename)
+ {
+ 	SPModulePrintPlain *pmod;
+ 	unsigned int ret;
+ 	SPRepr *repr;
+         char sstr[16];
+ 
+         ret = FALSE;
+ 	pmod = (SPModulePrintPlain *) mod;
+ 	repr = ((SPModule *) mod)->repr;
+ 
+         pmod->bitmap = 0;
+         pmod->dpi = 300;
+         sprintf(sstr, "%d", pmod->dpi);
+ 
+         if (repr) {
+                 sp_repr_set_attr (repr, "bitmap", (pmod->bitmap) ? "true" : "false");
+                 sp_repr_set_attr (repr, "resolution", sstr);
+                 sp_repr_set_attr (repr, "destination", filename);
+         }
+ 
+         ret = private_sp_module_print_plain_setup_output(pmod, filename);
+ 
+         return ret;
+ }
+ 
  
  static unsigned int
  sp_module_print_plain_begin (SPModulePrint *mod, SPDocument *doc)
cvs server: Diffing src/modules/mlview
cvs server: Diffing src/pixmaps
cvs server: Diffing src/svg
cvs server: Diffing src/widgets
cvs server: Diffing src/xml
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.