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