librsvg and Python

Vector Graphics and YOU!

Today I did some interesting work with SVG. I have an idea to use librsvg with libart and libxml2 to create a system that renders SVG efficiently on certain platforms for procedural rendering. I have a bunch of projects that seem to require SVG, so I'm glad to have made some progress on it today. It's a rather premature plan, so I won't recount my chickens for you. I'll simply tell you what I found today.

PyGTK is maturing quickly. It has win32 installer as well as a simple install process on most Linux or BSD machines. That's where our journey begins. If you download it on Slackware 11.0, you'll find that you need to compile some prerequisite libraries of higher version. First, get PyGobject 2.12 from Gnome FTP. Then we need glib 2.12.9. We also need an updated version of cairo 1.4.0 and PyCairo 1.2.6. Here's where it gets really interesting. If you want rsvg (the end goal), you need to get librsvg 2.16.1 and Gnome Python Desktop 2.17.93. For librsvg to work properly you'll also need libcroco 0.6.1.

Looking at the above directions, you're probably in version soup right now. You'd never be able to do all that, especially not in one day. In fact, I was trying to make a point by listing the requirements for a single useful program. The only reason you would actually decide to use Slackware is if you were a top notch coder. If you were a top notch coder, simply compiling and installing these libraries is a matter of running 20 commands each shorter than a sentence. Ask a economist to write 20 lines of a budget. Ask a reader to read 20 lines of William Gibson. Ask a writer to write 20 lines of prose. Slackware may restrict what an incompetent user does, but it enables an advanced user to get right to it. How often do I roll my own packages? I have successfully compiled 727 packages from source since 2002. I can list them because I can use the command
find ~/programs/ | grep -e .bz2 -e .gz | less
Can you use that command? If you can, you've got a grip on your computer that few others do.

What is the output of my installing these libraries? I was able to quickly write a program that uses nearly all the libraries at once.
def main():
    if(len(sys.argv) > 1):
        filename=sys.argv[1]
        svg = rsvg.Handle(file=filename)
    else:
        a = bldgs3c.bldg3c()
        s = str(a)
        svg = rsvg.Handle()
        svg.write(s)
        svg.close()
    #end if

    win = gtk.Window()
    drawingarea = gtk.DrawingArea()
    win.add(drawingarea)
    drawingarea.connect('expose_event', expose_cairo, svg)
    #drawingarea.connect('button_press_event', click_screen, svg)
    #drawingarea.set_events(gtk.gdk.BUTTON_PRESS_MASK)
    drawingarea.set_size_request(svg.props.width, svg.props.height)
    win.show_all()
    win.connect("destroy", lambda w: gtk.main_quit())
    gtk.main()
#end def main()


def expose_cairo(win, event, svg):
    x, y, w, h = win.allocation
    cr = win.window.cairo_create()
    if svg != None:
        svg.render_cairo(cr)
    #end if
    return True
#end def expose_cairo(win, event, svg)

if __name__ == '__main__':
    main()
#end if

The idea is that I can write a GUI program that renders SVG at a reasonable speed to be interactive. rsvg handles xml. Cairo handles drawing. GTK handles the window environment. Python makes it easy to code. The same C code would be twice as large with very little of the actual functions changing. You can tell that this code wasn't originally written by me. I never use lambda functions in my code. But I did write the else branch of the statement at the top.

Open source makes all this quite easy, right? They designed it that way. Imagine doing even a single part of this from scratch. You'd never get it done. On the other hand, let's think of what our closed source competitors would look like:
using System;
using System.Collections;
using System.Drawing;
using System.Xml;

namespace SvgProject
{

    public class SvgForm : System.UI.Form
    {
        public static int Main()
        {
            try {
                SvgImage svg = new SvgImage("bldg.svg");
                svg.Draw();
            }
            catch {}
        }
    }

}
Looks pretty nice huh? By the way in case you didn't guess, the above doesn't work. You see, certain technologies are too advanced for certain companies, so they just leave it out until 2 years too late. Any technology you want to write that doesn't fit their model either has to be: written from scratch (not likely), borrowed/stolen from open source projects (happens all the time), bought from an expensive third party provider, or simply not used at all.

Worse though is when Microsoft or Sun will implement exactly what you want exactly how you need it minus one function and plus one crash bug. You end up having to write workarounds for dozen of semi-billable hours. Then when the one crash bug turns into an exploitable worm, your project takes down the server turning your two nines of uptime into one nine of uptime. But you already got paid for the code you wrote. Fixing the mess will cost your customers extra.

Had you gone with an open source solution and been committed to supporting open source, you would have been able to write the function in one hour (or get someone else to write it), not have to worry about crash bugs, save time, and charge the customer a nominal rate for easy work.

Comments, questions, flames, suggestions? Send them or post them in the comments. You know you don't have to login to post a comment, right? RSS 2.0 is in full effect as soon as I post the link.

Permalink

Comments: 0

Leave a reply »

 
  • Leave a Reply
    Your gravatar
    Your Name