[cairo-commit] xsvg/src args.c, 1.9, 1.10 args.h, 1.7, 1.8 xsvg.c, 1.25, 1.26

Carl Worth commit at pdx.freedesktop.org
Tue Feb 15 19:47:42 PST 2005


Committed by: cworth

Update of /cvs/cairo/xsvg/src
In directory gabe:/tmp/cvs-serv21816/src

Modified Files:
	args.c args.h xsvg.c 
Log Message:

        * src/xsvg.c: Fix file encoding to utf-8.
        (main): Rip out all support for rendering to a PNG image. This
        functionality is already more completely in svg2png, (and there's
        no good reason to also maintain that code here).

        * src/args.c: Fix file encoding to utf-8.
        (args_parse): Switch argument parsing from argp to getopt for
        better portability. Remove all PNG-generation-related arguments.


Index: args.c
===================================================================
RCS file: /cvs/cairo/xsvg/src/args.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- args.c	30 Apr 2004 12:08:59 -0000	1.9
+++ args.c	16 Feb 2005 03:47:36 -0000	1.10
@@ -1,6 +1,6 @@
 /* args.c - Parse command-line argument for xsvg using argp
  *
- * Copyright © 2002 USC/Information Sciences Institute
+ * Copyright © 2002 USC/Information Sciences Institute
  *
  * Permission to use, copy, modify, distribute, and sell this software
  * and its documentation for any purpose is hereby granted without
@@ -26,118 +26,164 @@
  * Author: Carl Worth <cworth at isi.edu>
  */
 
+#include <stdio.h>
 #include <stdlib.h>
-#include <argp.h>
+#include <string.h>
+#include <libgen.h>
+#include <getopt.h>
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 #include "args.h"
 
-const char *argp_program_version = "xsvg " VERSION;
+static const char ARGS_PROGRAM_VERSION[] = VERSION;
+static const char ARGS_PROGRAM_DESCRIPTION[] = "X11 SVG viewer";
+static const char ARGS_PROGRAM_BUG_ADDRESS[] = "<cworth at cworth.org>";
 
-const char *argp_program_bug_address = "<cworth at isi.edu>";
+static const char ARGS_PROGRAM_ARGDOC[] = "<SVG file> (\"-\" for stdin)";
 
-static char doc[] = "xsvg - X11 SVG viewer";
+/* XXX: getopt is rather annoying in that you must maintain a
+ * string-encoding of the options in addition to the table. For
+ * example, to enable the sample display option below, one would have
+ * to add "d:" to the optstring.
+ *
+ * A useful exercise would be to write a function to generate the
+ * optstring from the table.
+ *
+ * The other annoying thing is that the table does not include help
+ * strings, so the args_help function below must also be maintained
+ * manually.
+ */
 
-static char args_doc[] = "<SVG file> (\"-\" for stdin)";
+enum {
+    ARGS_VAL_FLIPX = 256,
+    ARGS_VAL_FLIPY,
+    ARGS_VAL_HELP
+};
 
-static struct argp_option options[] = {
-    /* name,		key, arg,	 flags, doc */
-    {"display",		'd', "DISPLAY",  0, "X server to contact" },
-    {"geometry",	'g', "GEOMETRY", 0, "Size and location of window" },
-    {"scale",		's', "FACTOR",   0, "Scale image by FACTOR" },
-    {"png",		'p', "FILENAME", 0, "Render as PNG to FILENAME (\"-\" for stdout) instead of a window (EXPERIMENTAL)" }, 
-    {"width",		'w', "WIDTH",	 0, "Width for output PNG"},
-    {"height",		'h', "HEIGHT",	 0, "Height for output PNG"},
-    {"flipx",		'x', 0,		 0, "Flip X coordinates"},
-    {"flipy",		'y', 0,		 0, "Flip Y coordinates"},
-    {"fit",             'f', 0,          0, "Zoom to fit in window" },
-    {"full",            'F', 0,          0, "Full screen" },
-    {"argb",		'a', 0,		 0, "Use an ARGB window" },
+static char args_optstring[] = "d:g:s:w:h:fFav";
+static struct option args_options[] = {
+    /* name,		has_arg,	flag,	val */
+    {"display",		1,		0,	'd'},
+    {"geometry",	1,		0,	'g'},
+    {"scale",		1,		0,	's'},
+    {"flipx",		0,		0,	ARGS_VAL_FLIPX},
+    {"flipy",		0,		0,	ARGS_VAL_FLIPY},
+    {"fit",             0,		0,	'f'},
+    {"argb",		0,		0,	'a'},
+    {"help",		0,		0,	ARGS_VAL_HELP},
+    {"version",		0,		0,	'V'},
     { 0 }
 };
 
-static error_t
-parse_opt (int key, char *arg, struct argp_state *state)
+static void
+args_help (const char *argv0)
 {
-    struct args *args = state->input;
-
-    switch (key) {
-    case 'd':
-	args->display = arg;
-	break;
-    case 'g':
-	args->geometry = arg;
-	break;
-    case 'p':
-	args->png_file = arg;
-	break;
-    case 's':
-	args->scale = atof(arg);
-	break;
-    case 'w':
-	args->width = atoi(arg);
-	break;
-    case 'h':
-	args->height = atoi(arg);
-	break;
-    case 'x':
-	args->flipx = 1;
-	break;
-    case 'y':
-	args->flipy = 1;
-	break;
-    case 'f':
-        args->fit = 1;
-        break;
-    case 'F':
-	args->full = 1;
-	break;
+    char *argv0_copy = strdup (argv0);
+    char *argv0_base = basename (argv0_copy);
 
-    case ARGP_KEY_ARG:
-	args->svg_files[args->svg_nfile++] = arg;
+    printf ("Usage: %s [OPTIONS] %s\n", argv0_base, ARGS_PROGRAM_ARGDOC);
+    printf ("%s - %s\n", argv0_base, ARGS_PROGRAM_DESCRIPTION);
+    puts ("");
+    printf ("  -d, --display=DISPLAY  \tX server to contact\n");
+    printf ("  -g, --geometry=GEOMETRY\tSize and location of window\n");
+    printf ("  -s, --scale=FACTOR     \tScale image by FACTOR\n");
+    puts ("");
+    printf ("      --flipx            \tFlip X coordinates\n");
+    printf ("      --flipy            \tFlip Y coordinates\n");
+    printf ("  -f, --fit              \tZoom to fit in window\n");
+    printf ("  -F, --full             \tFull screen\n");
+    printf ("  -a, --argb             \tUse an ARGB window\n");
+    puts ("");
+    printf ("      --help             \tGive this help list\n");
+    printf ("  -V, --version          \tPrint program version\n");
 
-	break;
-    case 'a':
-	args->argb = 1;
-	break;
+    free (argv0_copy);
+};
 
-    case ARGP_KEY_END:
-	if (state->arg_num < 1)
-	    /* Didn't see a file option */
-	    argp_usage (state);
-	break;
+static void
+args_usage (const char *argv0)
+{
+    char *argv0_copy = strdup (argv0);
+    char *argv0_base = basename (argv0_copy);
 
-    default:
-	return ARGP_ERR_UNKNOWN;
-    }
+    printf ("Usage: %s [OPTION] %s\n", argv0_base, ARGS_PROGRAM_ARGDOC);
+    printf ("Try `%s --help' for more information.\n", argv0_base);
 
-    return 0;
+    free (argv0_copy);
 }
-	    
-static struct argp argp = { options, parse_opt, args_doc, doc };
 
-error_t
-args_parse(args_t *args, int argc, char *argv[])
+int
+args_parse (args_t *args, int argc, char *argv[])
 {
+    int c;
+
     args->display = NULL;
     args->geometry = NULL;
-    args->png_file = NULL;
     args->scale = 1.0;
     args->svg_files = malloc (argc * sizeof (char *));
     args->svg_nfile = 0;
 
-    args->width = -1;
-    args->height = -1;
     args->flipx = 0;
     args->flipy = 0;
     args->argb = 0;
 
     args->fit = 0;
-    args->full = 0;
 
-    return argp_parse (&argp, argc, argv,
-		       ARGP_LONG_ONLY,
-		       NULL, args);
+    while (1) {
+	c = getopt_long (argc, argv, args_optstring, args_options, NULL);
+	if (c == -1)
+	    break;
+
+	switch (c) {
+	case 'd':
+	    args->display = optarg;
+	    break;
+	case 'g':
+	    args->geometry = optarg;
+	    break;
+	case 's':
+	    args->scale = atof (optarg);
+	    break;
+	case ARGS_VAL_FLIPX:
+	    args->flipx = 1;
+	    break;
+	case ARGS_VAL_FLIPY:
+	    args->flipy = 1;
+	    break;
+	case 'f':
+	    args->fit = 1;
+	    break;
+	case 'a':
+	    args->argb = 1;
+	    break;
+	case 'V':
+	    printf ("%s\n", ARGS_PROGRAM_VERSION);
+	    exit (0);
+	    break;
+	case ARGS_VAL_HELP:
+	    args_help (argv[0]);
+	    exit (0);
+	    break;
+	case '?':
+	    args_help (argv[0]);
+	    exit (1);
+	    break;
+	default:
+	    fprintf (stderr, "Unhandled option: %d\n", c);
+	    exit (1);
+	    break;
+	}
+    }
+
+    if (optind == argc) {
+	args_usage (argv[0]);
+	exit (1);
+    }
+
+    while (optind < argc)
+	args->svg_files[args->svg_nfile++] = argv[optind++];
+
+    return 0;
 }

Index: args.h
===================================================================
RCS file: /cvs/cairo/xsvg/src/args.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- args.h	30 Apr 2004 12:08:59 -0000	1.7
+++ args.h	16 Feb 2005 03:47:36 -0000	1.8
@@ -1,6 +1,6 @@
 /* args.h - Parse command-line arguments for xsvg using argp
  *
- * Copyright © 2002 USC/Information Sciences Institute
+ * Copyright © 2002 USC/Information Sciences Institute
  *
  * Permission to use, copy, modify, distribute, and sell this software
  * and its documentation for any purpose is hereby granted without
@@ -29,8 +29,6 @@
 #ifndef ARGS_H
 #define ARGS_H
 
-#include <argp.h>
-
 typedef struct args
 {
     char *display;
@@ -38,20 +36,16 @@
 
     char **svg_files;
     int svg_nfile;
-    char *png_file;
 
     double scale;
-    int width;
-    int height;
     int flipx;
     int flipy;
 
     int fit;
-    int full;
     int argb;
 } args_t;
 
-error_t
+int
 args_parse(args_t *args, int argc, char *argv[]);
 
 #endif

Index: xsvg.c
===================================================================
RCS file: /cvs/cairo/xsvg/src/xsvg.c,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- xsvg.c	20 Jan 2005 16:40:23 -0000	1.25
+++ xsvg.c	16 Feb 2005 03:47:36 -0000	1.26
@@ -1,6 +1,6 @@
 /* xsvg - SVG viewer application for the X Window System
  *
- * Copyright © 2002 USC/Information Sciences Institute
+ * Copyright © 2002 USC/Information Sciences Institute
  *
  * Permission to use, copy, modify, distribute, and sell this software
  * and its documentation for any purpose is hereby granted without
@@ -124,13 +124,6 @@
 win_handle_events(win_t *win);
 
 static void
-render_to_png (const char *svg_file, const char *png_file, double scale, int width, int height);
-
-static svg_status_t
-write_png_argb32 (char *buffer, const char* filename,
-		  int width, int height, int stride);
-
-static void
 win_reconfigure_normal (win_t *win, unsigned int width, unsigned int height);
 
 static void
@@ -213,88 +206,31 @@
 {
     args_t args;
     win_t win;
-    args_parse (&args, argc, argv);
-
-    if (args.png_file) {
-	render_to_png (args.svg_files[0], args.png_file, args.scale, args.width, args.height);
-    } else {
-	Display *dpy;
-
-	dpy = XOpenDisplay (args.display);
-	if (dpy == NULL) {
-	    fprintf (stderr, "Failed to open display: %s\n", XDisplayName(args.display));
-	    return 1;
-	}
-
-	win.full_mode = args.full;
-	win_init (&win, dpy, args.argb, args.geometry, 
-		  args.svg_files, args.svg_nfile);
-	win.zoom *= args.scale;
-	win.x_flip = args.flipx;
-	win.y_flip = args.flipy;
-        win.fit_mode = args.fit;
-
-	win_handle_events (&win);
-
-	win_deinit (&win);
-
-	XCloseDisplay (dpy);
-    }
-
-    return 0;
-}
-
-/* XXX: We should make the -fit option work for scaling the PNG output. */
-static void
-render_to_png (const char *svg_file, const char *png_file, double scale, int width, int height)
-{
-    svg_cairo_status_t status;
-    cairo_t *cr;
-    svg_cairo_t *svgc;
-    char *image;
-
-    status = svg_cairo_create (&svgc);
-    if (status) {
-	fprintf (stderr, "Failed to create svg_cairo_t. Exiting.\n");
-	exit(1);
-    }
+    Display *dpy;
 
-    /* special case filename "-" means stdin */
-    if (strcmp (svg_file, "-") == 0)
-	status = svg_cairo_parse_file (svgc, stdin);
-    else
-	status = svg_cairo_parse(svgc, svg_file);
-    if (status) {
-	fprintf (stderr, "Failed to parse SVG file: %s. Exiting.\n", svg_file);
-	exit(1);
-    }
+    args_parse (&args, argc, argv);
 
-    if (width < 0 || height < 0) {
-	svg_cairo_get_size (svgc, &width, &height);
-	width = (width * scale + 0.5);
-	height = (height * scale + 0.5);
+    dpy = XOpenDisplay (args.display);
+    if (dpy == NULL) {
+	fprintf (stderr, "Failed to open display: %s\n", XDisplayName(args.display));
+	return 1;
     }
 
-    image = calloc (width * height, 4);
-    if (! image)
-	return;
-
-    cr = cairo_create ();
-    cairo_scale (cr, scale, scale);
-
-    cairo_set_target_image (cr, image, CAIRO_FORMAT_ARGB32, width, height, width * 4);
-
-    /* XXX: This probably doesn't need to be here (eventually) */
-    cairo_set_rgb_color (cr, 1, 1, 1);
-
-    svg_cairo_render (svgc, cr);
-
-    write_png_argb32 (image, png_file, width, height, width * 4);
+    /* win.full_mode = args.full; */
+    win_init (&win, dpy, args.argb, args.geometry, 
+	      args.svg_files, args.svg_nfile);
+    win.zoom *= args.scale;
+    win.x_flip = args.flipx;
+    win.y_flip = args.flipy;
+    win.fit_mode = args.fit;
+    
+    win_handle_events (&win);
+    
+    win_deinit (&win);
 
-    svg_cairo_destroy (svgc);
-    svgc = NULL;
+    XCloseDisplay (dpy);
 
-    free (image);
+    return 0;
 }
 
 static Visual *
@@ -961,84 +897,3 @@
     XMapWindow (win->dpy, win->win);
     return 0;
 }
-
-static void
-unpremultiply_data (png_structp png, png_row_infop row_info, png_bytep data)
-{
-    int i;
-
-    for (i = 0; i < row_info->rowbytes; i += 4) {
-        unsigned char *b = &data[i];
-        unsigned char alpha = b[3];
-        unsigned long pixel;
-        unsigned long *p;
-        if (alpha == 0)
-            pixel = 0;
-        else
-            pixel = ((((b[0] * 255) / alpha) << 0) |
-                     (((b[1] * 255) / alpha) << 8) |
-                     (((b[2] * 255) / alpha) << 16) |
-                     (alpha << 24));
-        p = (unsigned long *) b;
-        *p = pixel;
-    }
-}
-
-static svg_status_t
-write_png_argb32 (char *buffer, const char* filename,
-		  int width, int height, int stride)
-{
-    FILE* f;
-    png_struct* png;
-    png_info* info;
-    png_byte** rows;
-    int       i;
-    png_color_16 white;
-    
-    /* special case filename "-" means stdout */
-    if (strcmp (filename, "-") == 0)
-	f = stdout;
-    else
-    	f = fopen (filename, "w");
-    if (!f)
-	return SVG_STATUS_IO_ERROR;
-
-    rows = malloc (height*sizeof (png_byte*));
-
-    for (i=0;i<height;i++) {
-	rows[i]=buffer+i*stride;
-    }
-
-    png = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
-    info = png_create_info_struct (png);
-
-    png_init_io (png, f);
-    png_set_IHDR (png, info,
-		  width, height, 8,
-		  PNG_COLOR_TYPE_RGB_ALPHA, 
-		  PNG_INTERLACE_NONE,
-		  PNG_COMPRESSION_TYPE_DEFAULT,
-		  PNG_FILTER_TYPE_DEFAULT);
-
-    white.red=0xff;
-    white.blue=0xff;
-    white.green=0xff;
-    png_set_bKGD (png, info, &white);
-
-    png_set_write_user_transform_fn (png, unpremultiply_data);
-    png_set_bgr (png);
-
-    png_write_info (png, info);
-    png_write_image (png, rows);
-    png_write_end (png, info);
-
-    png_destroy_write_struct (&png, &info);
-
-    free (rows);
-    if (f == stdout)
-	fflush(stdout);
-    else
-    	fclose (f);
-
-    return SVG_STATUS_SUCCESS;
-}




More information about the cairo-commit mailing list