[cairo-commit] [cairo-www] src/svgtopycairo.mdwn
Carl Worth
cworth at freedesktop.org
Thu Nov 8 21:37:08 PST 2007
src/svgtopycairo.mdwn | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
New commits:
commit 8b6f53a76caad882ecf32e932ef62220933b5d3d
Author: Donn <donn.ingle at gmail.com>
Date: Thu Nov 8 21:37:08 2007 -0800
More info.
diff --git a/src/svgtopycairo.mdwn b/src/svgtopycairo.mdwn
index 552a76b..2b3ac6a 100644
--- a/src/svgtopycairo.mdwn
+++ b/src/svgtopycairo.mdwn
@@ -1,8 +1,17 @@
##SVG paths can be parsed and turned into a seqence of cairo commands that re-draw them.
This took a while, the pyparsing had me in knots, but now it's short and sweet. A fuller implementation of what can be done in SVG would be really nice. (Hint...)
-Make sure you pass it a very simple SVG file -- one that has had all the shapes reduced to paths. Oh, and keep your canvas 400 by 400 or it may draw clear off the screen.
-It re-uses the framework from my other recipe. You should be able to copy/paste and run, but you may need elementree and pyparsing first.
+
+Make sure you pass it a very simple SVG file (from Inkscape is best) -- one that has had all the shapes reduced to paths. Oh, and keep your canvas 400 by 400 or it may draw clear off the screen.
+
+**Depends on**
+
+1. *elementree*: **import** elementree **as** myDearWatson :) It's a great module for slicing through XML.
+2. *pyparsing*: This module is deeply wonderful. I won't pretend to savvy even 1% of it, but it really does the job. They have a great mailing list where I got a lot of help. It let's you parse strings into lists and that is no small feat.
+
+**SVG Path element**
+
+To briefly explain, inside an svg file (which is just xml) you'll find a tag named 'g' and under that one or more tags named 'path'. Inside path there is an element called 'd'; that's the actual path. It's formed like this: "COMMAND NUMBER COMMA NUMBER Optionally[NUMBER COMMA NUMBER a few more times]", where COMMAND is M for move, L for line, C for curve and Z for close path. There may be others, but that's what I tackled. Have a look at the pyparsing grammar which makes it fairly clear how different commands have different numbers behind them.
Please forgive any bugs.
@@ -75,9 +84,7 @@ Please forgive any bugs.
window.present()
gtk.main()
- #######################
- ## Do all your testing in Shapes ##
- #######################
+ ## Do the drawing ##
class Shapes(Screen):
def draw(self, ctx, width, height):
@@ -111,6 +118,7 @@ Please forgive any bugs.
raise SystemExit(__doc__)
file = sys.argv[1]
+ ## Pyparsing grammar:
## With HUGE help from Paul McGuire <paul at alanweberassociates.com>
## Thanks!
dot = Literal(".")
@@ -139,6 +147,6 @@ Please forgive any bugs.
for e in group.getiterator('{%s}path' % ns):
p = e.get("d")
tokens = phrase.parseString(p.upper())
- paths.append(tokens)
+ paths.append(tokens) # paths is a global var.
run(Shapes)
More information about the cairo-commit
mailing list