Mercurial > MadButterfly
annotate pyink/mbbbox.py @ 1393:2d56ed5b0995
Add exporter for export svg with bbox information.
All children of svg:g node are attributed with bbox information. It
means all graphic nodes, except top most svg:g nodes, are wroute out
with bbox information.
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Wed, 30 Mar 2011 13:42:30 +0800 |
parents | |
children | 4a786de1d62f |
rev | line source |
---|---|
1393
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
1 import pybExtension |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
2 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
3 def _print_subtree(node, lvl, out): |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
4 def _print_level(txt, lvl, out): |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
5 indent = ' ' * lvl |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
6 print >> out, '%s%s' % (indent, txt) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
7 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
8 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
9 def _print_node_open(node, lvl, out): |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
10 node_name = node.name() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
11 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
12 attrs = [] |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
13 for attrname in node.allAttributes(): |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
14 attrvalue = node.getAttribute(attrname) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
15 attr = '%s="%s"' % (attrname, attrvalue) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
16 attrs.append(attr) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
17 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
18 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
19 parent_node = node.parent() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
20 if parent_node: |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
21 parent_name = parent_node.name() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
22 if parent_name == 'svg:g': |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
23 bbox = node.getBBox() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
24 attr = 'inkscape:bbox-x="%f"' % (bbox[0]) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
25 attrs.append(attr) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
26 bbox = node.getBBox() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
27 attr = 'inkscape:bbox-y="%f"' % (bbox[1]) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
28 attrs.append(attr) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
29 bbox = node.getBBox() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
30 attr = 'inkscape:bbox-width="%f"' % (bbox[2]) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
31 attrs.append(attr) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
32 bbox = node.getBBox() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
33 attr = 'inkscape:bbox-height="%f"' % (bbox[3]) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
34 attrs.append(attr) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
35 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
36 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
37 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
38 if attrs: |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
39 attrs_str = ' '.join(attrs) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
40 line = '<%s %s>' % (node_name, attrs_str) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
41 else: |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
42 line = '<%s>' % (node_name) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
43 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
44 _print_level(line, lvl, out) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
45 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
46 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
47 def _print_node_close(node, lvl, out): |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
48 node_name = node.name() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
49 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
50 line = '</%s>' % (node_name) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
51 _print_level(line, lvl, out) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
52 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
53 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
54 def _print_node_single(node, lvl, out): |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
55 node_name = node.name() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
56 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
57 attrs = [] |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
58 for attrname in node.allAttributes(): |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
59 attrvalue = node.getAttribute(attrname) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
60 attr = '%s="%s"' % (attrname, attrvalue) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
61 attrs.append(attr) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
62 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
63 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
64 parent_node = node.parent() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
65 if parent_node: |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
66 parent_name = parent_node.name() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
67 if parent_name == 'svg:g': |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
68 bbox = node.getBBox() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
69 attr = 'inkscape:bbox-x="%f"' % (bbox[0]) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
70 attrs.append(attr) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
71 bbox = node.getBBox() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
72 attr = 'inkscape:bbox-y="%f"' % (bbox[1]) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
73 attrs.append(attr) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
74 bbox = node.getBBox() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
75 attr = 'inkscape:bbox-width="%f"' % (bbox[2]) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
76 attrs.append(attr) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
77 bbox = node.getBBox() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
78 attr = 'inkscape:bbox-height="%f"' % (bbox[3]) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
79 attrs.append(attr) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
80 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
81 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
82 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
83 if attrs: |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
84 attrs_str = ' '.join(attrs) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
85 line = '<%s %s/>' % (node_name, attrs_str) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
86 else: |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
87 line = '<%s/>' % (node_name) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
88 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
89 _print_level(line, lvl, out) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
90 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
91 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
92 def _print_node_content(node, lvl, out): |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
93 line = node.content() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
94 _print_level(line, lvl, out) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
95 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
96 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
97 children = node.childList() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
98 if not children: |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
99 if node.name() != 'string': |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
100 _print_node_single(node, lvl, out) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
101 else: |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
102 _print_node_content(node, lvl, out) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
103 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
104 return |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
105 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
106 _print_node_open(node, lvl, out) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
107 for child in children: |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
108 _print_subtree(child, lvl + 1, out) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
109 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
110 _print_node_close(node, lvl, out) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
111 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
112 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
113 class mbbbox_ext(pybExtension.PYBindExtImp): |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
114 def save(self, module, doc, filename): |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
115 out = file(filename, 'w+') |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
116 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
117 root = doc.rdoc.root() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
118 _print_subtree(root, 0, out) |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
119 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
120 out.close() |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
121 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
122 pass |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
123 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
124 |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
125 extension = (mbbbox_ext(), |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
126 'net.scribboo.mbbbox', |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
127 'SVG with BBox exporter', |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
128 'output', |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
129 {'extension': '.svg', |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
130 'mimetype': 'image/svg+xml', |
2d56ed5b0995
Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
131 '_filetypename': 'SVG+BBox (*.svg)'}) |