[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

patch - add size to your dia



Hello maintainers,

please apply this patch.  It currently works only for the
PNG export filter, and it does not yet distort the image if
you apply both width and height, but it should.  It is
however useful, if you use dia in your Makefiles and like
to generate images for HTML that are exactly 600 pixels in
width etc.  The manual page is updated, too.

Example:

$ dia --export-to-format=png --size=600x500 foo.dia

Cheers,
-- 
W. Borgert <debacle@debian.org>
diff -ruN dia/app/app_procs.c dia.new/app/app_procs.c
--- dia/app/app_procs.c	2003-11-25 23:56:45.000000000 +0000
+++ dia.new/app/app_procs.c	2003-11-25 23:56:29.000000000 +0000
@@ -85,7 +85,8 @@
 static gboolean
 handle_initial_diagram(const char *input_file_name, 
 		       const char *export_file_name,
-		       const char *export_file_format);
+		       const char *export_file_format,
+		       const char *size);
 
 static void create_user_dirs(void);
 static PluginInitResult internal_plugin_init(PluginInfo *info);
@@ -150,10 +151,12 @@
 
 /** Convert infname to outfname, using input filter inf and export filter
  * ef.  If either is null, try to guess them.
+ * size might be NULL.
  */
 gboolean
 do_convert(const char *infname, 
-	   const char *outfname, DiaExportFilter  *ef)
+	   const char *outfname, DiaExportFilter *ef,
+	   const char *size)
 {
   DiaImportFilter *inf;
   DiagramData *diagdata = NULL;
@@ -188,6 +191,9 @@
             argv0,infname);
             exit(1);
   }
+  /* is user_data supposed to work that way? */
+  if (size)
+    ef->user_data = (void *) size;
   ef->export(diagdata, outfname, infname, ef->user_data);
   /* if (!quiet) */ fprintf(stdout,
                       _("%s --> %s\n"),
@@ -217,7 +223,8 @@
 static gboolean
 handle_initial_diagram(const char *in_file_name, 
 		       const char *out_file_name,
-		       const char *export_file_format) {
+		       const char *export_file_format,
+		       const char *size) {
   DDisplay *ddisp = NULL;
   Diagram *diagram = NULL;
   gboolean made_conversions = FALSE;
@@ -238,10 +245,12 @@
       export_file_name = build_output_file_name(in_file_name,
 						ef->extensions[0]);
     }
-    made_conversions |= do_convert(in_file_name, (out_file_name != NULL?out_file_name:export_file_name), ef);
+    made_conversions |= do_convert(in_file_name,
+      (out_file_name != NULL?out_file_name:export_file_name), ef, size);
     g_free(export_file_name);
   } else if (out_file_name) {
-    made_conversions |= do_convert(in_file_name, out_file_name, NULL);
+    made_conversions |= do_convert(in_file_name, out_file_name, NULL,
+				   size);
   } else {
     if (g_file_test(in_file_name, G_FILE_TEST_EXISTS)) {
       diagram = diagram_load (in_file_name, NULL);
@@ -307,6 +316,7 @@
 #endif
   char *export_file_name = NULL;
   char *export_file_format = NULL;
+  char *size = NULL;
   gboolean made_conversions = FALSE;
 
 #ifdef HAVE_POPT
@@ -329,6 +339,8 @@
     {"export-to-format",'t', POPT_ARG_STRING, NULL /* &export_file_format */,
      0, export_format_string, N_("FORMAT")
     },
+    {"size", 's', POPT_ARG_STRING, NULL, 0,
+     N_("Export graphics size"), N_("WxH")},
     {"nosplash", 'n', POPT_ARG_NONE, &nosplash, 0,
      N_("Don't show the splash screen"), NULL },
     {"credits", 'c', POPT_ARG_NONE, &credits, 0,
@@ -345,6 +357,7 @@
 #ifdef HAVE_POPT
   options[0].arg = &export_file_name;
   options[1].arg = &export_file_format;
+  options[2].arg = &size;
 #endif
 
   argv0 = (argc > 0) ? argv[0] : "(none)";
@@ -525,10 +538,11 @@
 #ifdef HAVE_POPT
       while (poptPeekArg(poptCtx)) {
           char *in_file_name = (char *)poptGetArg(poptCtx);
-	  
+
 	  made_conversions |= handle_initial_diagram(in_file_name,
 						     export_file_name,
-						     export_file_format);
+						     export_file_format,
+						     size);
       }
       poptFreeContext(poptCtx);
 #else
@@ -549,11 +563,18 @@
                   export_file_name = argv[i];
                   continue;
               }
+          } else if (0 == strcmp(argv[i],"-s")) {
+              if (i < (argc-1)) {
+                  i++;
+                  size = argv[i];
+                  continue;
+              }
           }
           
 	  made_conversions |= handle_initial_diagram(in_file_name,
 						     export_file_name,
-						     export_file_format);
+						     export_file_format,
+						     size);
       }
 #endif
   }
@@ -725,3 +746,19 @@
   return DIA_PLUGIN_INIT_OK;
 }
 
+/* parses a string of the form "[0-9]*x[0-9]*" and transforms it into
+   two long values width and height. */
+void
+parse_size(gchar *size, long *width, long *height)
+{
+  if (size) {
+    gchar** array = g_strsplit(size, "x", 3);
+    *width  = (array[0])? strtol(array[0], NULL, 10): 0;
+    *height = (array[1])? strtol(array[1], NULL, 10): 0;
+    g_strfreev(array);
+  }
+  else {
+    *width  = 0;
+    *height = 0;
+  }
+}
diff -ruN dia/app/app_procs.h dia.new/app/app_procs.h
--- dia/app/app_procs.h	2003-11-25 23:56:45.000000000 +0000
+++ dia.new/app/app_procs.h	2003-11-25 23:56:29.000000000 +0000
@@ -25,7 +25,8 @@
 int app_is_embedded(void);
 
 gboolean do_convert(const char *infname,
-		    const char *outfname, DiaExportFilter *ef);
+		    const char *outfname, DiaExportFilter *ef,
+		    const char *size);
 char *build_output_file_name(const char *infname, const char *format);
 
 void app_splash_init(const gchar* name);
@@ -33,4 +34,8 @@
 
 gboolean app_is_interactive(void);
 
+/* parses a string of the form "[0-9]*x[0-9]*" and transforms it into
+   two long values width and height. */
+void parse_size(gchar *size, long *width, long *height);
+
 #endif /* APP_PROCS_H */
diff -ruN dia/app/diaconv.c dia.new/app/diaconv.c
--- dia/app/diaconv.c	2003-11-25 23:56:45.000000000 +0000
+++ dia.new/app/diaconv.c	2003-11-25 23:56:29.000000000 +0000
@@ -213,7 +213,7 @@
       export_file_name = build_output_file_name(in_file_name,
                                                 export_file_format);
       g_message("export_file_name = %s",export_file_name);
-      do_convert(in_file_name,export_file_name);
+      do_convert(in_file_name, export_file_name, NULL);
       g_free(export_file_name);
       in_file_name = poptGetArg(poptCtx);
     }
@@ -226,7 +226,7 @@
               argv[0]);
       exit(1);
     }
-    do_convert(in_file_name,export_file_name);
+    do_convert(in_file_name, export_file_name, NULL);
   }
   exit(0);
 }
diff -ruN dia/app/export_png.c dia.new/app/export_png.c
--- dia/app/export_png.c	2003-11-25 23:56:45.000000000 +0000
+++ dia.new/app/export_png.c	2003-11-25 23:56:29.000000000 +0000
@@ -48,6 +48,7 @@
 struct png_callback_data {
   DiagramData *data;
   gchar *filename;
+  gchar *size;			/* for command line option --size */
 };
 
 /* Static data.  When the dialog is not reentrant, you could have all data
@@ -75,6 +76,7 @@
   guint32 width, height, band, row, i;
   real band_height;
   guint32 imagewidth, imageheight;
+  long req_width, req_height;
   real imagezoom;
 
   FILE *fp;
@@ -93,8 +95,24 @@
     imagewidth = gtk_spin_button_get_value_as_int(export_png_width_entry);
     imageheight = gtk_spin_button_get_value_as_int(export_png_height_entry);
   } else {
-    imagewidth = width;
-    imageheight = height;
+    if (cbdata && cbdata->size) {
+      float ratio = (float) width/(float) height;
+
+      parse_size(cbdata->size, &req_width, &req_height);
+      if (req_width && !req_height) {
+	imagewidth  = req_width;
+	imageheight = req_width / ratio;
+      } else if (req_height && !req_width) {
+	imagewidth  = req_height * ratio;
+	imageheight = req_height;
+      } else if (req_width && req_height) {
+	imagewidth  = req_width;
+	imageheight = req_height;
+      }
+    } else {
+      imagewidth  = width;
+      imageheight = height;
+    }
   }
 
   imagezoom = ((real)imageheight/height) * DPCM * data->paper.scaling;
@@ -147,8 +165,24 @@
     imagewidth = gtk_spin_button_get_value_as_int(export_png_width_entry);
     imageheight = gtk_spin_button_get_value_as_int(export_png_height_entry);
   } else {
-    imagewidth = width;
-    imageheight = height;
+    if (cbdata && cbdata->size) {
+      float ratio = (float) width/(float) height;
+
+      parse_size(cbdata->size, &req_width, &req_height);
+      if (req_width && !req_height) {
+	imagewidth  = req_width;
+	imageheight = req_width / ratio;
+      } else if (req_height && !req_width) {
+	imagewidth  = req_height * ratio;
+	imageheight = req_height;
+      } else if (req_width && req_height) {
+	imagewidth  = req_width;
+	imageheight = req_height;
+      }
+    } else {
+      imagewidth  = width;
+      imageheight = height;
+    }
   }
   band = MIN(imageheight, BAND_HEIGHT);
 
@@ -251,7 +285,7 @@
      returns before the callback is called.  Must be freed by the
      final callbacks. */
   struct png_callback_data *cbdata = 
-    (struct png_callback_data *)g_malloc(sizeof(struct png_callback_data));
+    (struct png_callback_data *) g_new0(struct png_callback_data, 1);
   Rectangle *ext = &data->extents;
   guint32 width, height;
 
@@ -310,6 +344,7 @@
     /* Show the whole thing */
     gtk_widget_show_all(export_png_dialog);
   } else {
+    cbdata->size = (gchar *) user_data;
     export_png_ok(NULL, cbdata);
   }
 }
diff -ruN dia/doc/en/dia.dbk dia.new/doc/en/dia.dbk
--- dia/doc/en/dia.dbk	2003-11-26 00:11:15.000000000 +0000
+++ dia.new/doc/en/dia.dbk	2003-11-26 00:13:06.000000000 +0000
@@ -6,7 +6,8 @@
   <refentryinfo>
     <copyright>
       <year>1999</year>
-      <holder>Fredrik Hallenberg</holder>
+      <year>2003</year>
+      <holder>Fredrik Hallenberg, W. Borgert</holder>
     </copyright>
     <date>1999-07-03</date>
     <authorgroup>
@@ -24,6 +25,12 @@
     <revhistory>
       <revision>
 	<revnumber>2</revnumber>
+	<date>2003-11-26</date>
+	<authorinitials>WB</authorinitials>
+	<revremark>added --size option</revremark>
+      </revision>
+      <revision>
+	<revnumber>2</revnumber>
 	<date>2003-10-16</date>
 	<authorinitials>WB</authorinitials>
 	<revremark>added different EPS variants and section on
@@ -70,6 +77,10 @@
 
       <arg><option>--nosplash</option></arg>
 
+      <arg><option>-s <replaceable>WxH</replaceable></option></arg>
+
+      <arg><option>--size=<replaceable>WxH</replaceable></option></arg>
+
       <arg><option>-t <replaceable>FORMAT</replaceable></option></arg>
 
       <arg><option>--export-to-format=<replaceable>FORMAT</replaceable></option></arg>
@@ -136,6 +147,23 @@
 	</listitem>
       </varlistentry>
       <varlistentry>
+	<term><option>-s <replaceable>WxH</replaceable></option>
+	  <option>--size=<replaceable>WxH</replaceable></option></term>
+	<listitem>
+	  <para>Export loaded file in decimal given width and/or
+	  height.  It is allowed to only select width or height.
+	  E.g. <option>--size=<replaceable>520x</replaceable></option>
+	  exports an image that is 520 pixels wide, while
+	  <option>--size=<replaceable>x900</replaceable></option>
+	  exports an image of 900 pixels height.</para>
+
+	  <note>
+	    <para>This option is currently only implemented for the
+	    PNG export filter.</para>
+	  </note>
+	</listitem>
+      </varlistentry>
+      <varlistentry>
 	<term><option>-t <replaceable>FORMAT</replaceable></option>
 	  <option>--export-to-format=<replaceable>FORMAT</replaceable></option></term>
 	<listitem>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index] Mail converted by Mofo Magic and the Flying D

 
All trademarks and copyrights are the property of their respective owners.

Other Directory Sites: SeekWonder | Directory Owners Forum

GuideSMACK