[cairo-commit] svgslides/src Makefile.am, 1.2, 1.3 svgslides-output.c, NONE, 1.1

Kristian Hogsberg commit at pdx.freedesktop.org
Sun Feb 13 07:48:07 PST 2005


Committed by: krh

Update of /cvs/cairo/svgslides/src
In directory gabe:/tmp/cvs-serv19492/src

Modified Files:
	Makefile.am 
Added Files:
	svgslides-output.c 
Log Message:
2005-02-13  Kristian Høgsberg  <krh at redhat.com>

        * configure.in: Add check for libraries requires by svgslides-output.

        * src/Makefile.am (pkgdata_DATA): Add svgslides-output.

        * src/svgslides-output.c: New tool to output svg slides to other
        formats.  Currently only PDF.



Index: Makefile.am
===================================================================
RCS file: /cvs/cairo/svgslides/src/Makefile.am,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- Makefile.am	13 Feb 2005 03:42:34 -0000	1.2
+++ Makefile.am	13 Feb 2005 15:48:03 -0000	1.3
@@ -2,3 +2,10 @@
 
 pkgdata_DATA = svgslides.xsl svgslides.rnc svgslides-index.rnc
 
+
+INCLUDES = @SVGSLIDES_OUTPUT_CFLAGS@
+
+bin_PROGRAMS = svgslides-output
+
+svgslides_output_SOURCES = svgslides-output.c
+svgslides_output_LDADD = @SVGSLIDES_OUTPUT_LIBS@

--- NEW FILE: svgslides-output.c ---
#include <glib.h>
#include <cairo.h>
#include <cairo-pdf.h>
#include <svg-cairo.h>
#include <libxml/tree.h>

typedef struct _SVGSlidesSlide SVGSlidesSlide;
struct _SVGSlidesSlide 
{
    svg_cairo_t *svg;
    char *title;
};

typedef struct _SVGSlidesDocument SVGSlidesDocument;
struct _SVGSlidesDocument
{
    char *filename;
    char *dirname;
    char *title;
    GPtrArray *slides;
};

static int
parse_slides (SVGSlidesDocument *document, xmlNode *root)
{
    xmlNode *node;
    xmlChar *xml_filename, *xml_title;
    char *filename, *title;
    SVGSlidesSlide *slide;

    for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
	if (xmlStrcmp(node->name, (const xmlChar *) "slide"))
	    continue;

	xml_filename = xmlGetProp (node, "filename");
	if (xml_filename == NULL) {
	    g_printerr ("no filename for slide\n");
	    continue;
	}
			
	xml_title = xmlGetProp (node, "title");
	if (xml_title == NULL) {
	    title = g_strdup ("untitled");
	}
	else {
	    title = g_strdup (xml_title);
	    xmlFree (xml_title);
	}			
			
	if (g_path_is_absolute (xml_filename))
	    filename = g_strdup (xml_filename);
	else
	    filename = g_build_filename (document->dirname,
					 xml_filename, NULL);

	xmlFree (xml_filename);

	slide = g_new (SVGSlidesSlide, 1);
	slide->title = title;
			
	if (svg_cairo_create (&slide->svg) != SVG_CAIRO_STATUS_SUCCESS) {
	    g_printerr ("failed to create svg_cairo context\n");
	    goto bail1;
	}

	if (svg_cairo_parse (slide->svg, filename) != SVG_CAIRO_STATUS_SUCCESS) {
	    g_printerr ("could not parse \"%s\"\n", filename);
	    goto bail2;
	}
			
	g_ptr_array_add (document->slides, slide);
	g_free (filename);
	continue;

    bail2:
	svg_cairo_destroy (slide->svg);
    bail1:
	g_free (slide);
	g_free (filename);
	continue;
    }
}

static SVGSlidesDocument *
svgslides_document_load (const char  *filename)
{
    SVGSlidesDocument *document;
    xmlDoc *doc;
    xmlNode *root;
    xmlChar *xml_title;

    document = g_new (SVGSlidesDocument, 1);
    document->filename = g_strdup (filename);
    document->dirname = g_path_get_dirname (filename);
    document->slides = g_ptr_array_new ();

    doc = xmlParseFile(filename);
    if (doc == NULL ) {
	g_printerr ("failed to load document\n");
	goto bail1;
    }

    root = xmlDocGetRootElement(doc);
    if (root == NULL) {
	g_printerr ("empty document\n");
	goto bail2;
    }

    if (xmlStrcmp(root->name, (const xmlChar *) "svgslides-index")) {
	g_printerr ("document of the wrong type\n");
	goto bail2;
    }

    xml_title = xmlGetProp (root, "title");
    if (xml_title == NULL) {
	document->title = g_strdup ("untitled");
    }
    else {
	document->title = g_strdup (xml_title);
	xmlFree (xml_title);
    }

    parse_slides (document, root);

    return document;

 bail2:
    xmlFreeDoc(doc);
 bail1:
    g_free (document->filename);
    g_ptr_array_free (document->slides, FALSE);
    g_free (document);

    return NULL;
}

static void
svgslides_document_output_pdf (SVGSlidesDocument *document,
			       const char *filename)
{
    FILE *file;
    cairo_t *cr;
    SVGSlidesSlide *slide;
    int i, width, height;
    double scale;
    char *data;

    file = fopen (filename, "w");
    cr = cairo_create ();

    data = malloc (100 * 100 * 4);

    cairo_set_target_pdf (cr, file, 11, 8.5, 10, 10);
    scale = 1;

    for (i = 0; i < document->slides->len; i++) {
	slide = g_ptr_array_index (document->slides, i);

	printf ("slide %d, \"%s\"\n", i, slide->title);

	svg_cairo_get_size (slide->svg, &width, &height);

	cairo_scale (cr, scale, scale);

	svg_cairo_render (slide->svg, cr);

	cairo_show_page (cr);
    }

    cairo_destroy (cr); 
    fclose (file);
}

int
main (int argc, char *argv[])
{
    SVGSlidesDocument *document;

    if (argc != 3) {
	g_printerr ("usage: %s SVGSLIDES-INDEX-FILE PDF-FILE\n", argv[0]);
	return -1;
    }

    document = svgslides_document_load (argv[1]);
    if (document == NULL)
	return -1;

    svgslides_document_output_pdf (document, argv[2]);

    return 0;
}




More information about the cairo-commit mailing list