annotate pyink/mbbbox.py @ 1407:f19121bd6a6c

Check if the name is defined for old SVG file.
author wycc
date Wed, 06 Apr 2011 07:48:29 +0800
parents 4a786de1d62f
children ce981aa3fbf2
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)
1394
4a786de1d62f Prevent the inkscape:bbox-* from duplicated. This will crash the libxml parser.
wycc
parents: 1393
diff changeset
15 if attrname[0:13] == 'inkscape:bbox':
4a786de1d62f Prevent the inkscape:bbox-* from duplicated. This will crash the libxml parser.
wycc
parents: 1393
diff changeset
16 continue
1393
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
17 attr = '%s="%s"' % (attrname, attrvalue)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
18 attrs.append(attr)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
19 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
20
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
21 parent_node = node.parent()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
22 if parent_node:
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
23 parent_name = parent_node.name()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
24 if parent_name == 'svg:g':
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
25 bbox = node.getBBox()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
26 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
27 attrs.append(attr)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
28 bbox = node.getBBox()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
29 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
30 attrs.append(attr)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
31 bbox = node.getBBox()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
32 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
33 attrs.append(attr)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
34 bbox = node.getBBox()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
35 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
36 attrs.append(attr)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
37 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
38 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
39
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
40 if attrs:
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
41 attrs_str = ' '.join(attrs)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
42 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
43 else:
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
44 line = '<%s>' % (node_name)
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 _print_level(line, lvl, out)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
47 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
48
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
49 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
50 node_name = node.name()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
51
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
52 line = '</%s>' % (node_name)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
53 _print_level(line, lvl, out)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
54 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
55
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
56 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
57 node_name = node.name()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
58
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
59 attrs = []
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
60 for attrname in node.allAttributes():
1394
4a786de1d62f Prevent the inkscape:bbox-* from duplicated. This will crash the libxml parser.
wycc
parents: 1393
diff changeset
61 if attrname[0:13] == 'inkscape:bbox':
4a786de1d62f Prevent the inkscape:bbox-* from duplicated. This will crash the libxml parser.
wycc
parents: 1393
diff changeset
62 continue
1393
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
63 attrvalue = node.getAttribute(attrname)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
64 attr = '%s="%s"' % (attrname, attrvalue)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
65 attrs.append(attr)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
66 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
67
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
68 parent_node = node.parent()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
69 if parent_node:
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
70 parent_name = parent_node.name()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
71 if parent_name == 'svg:g':
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
72 bbox = node.getBBox()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
73 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
74 attrs.append(attr)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
75 bbox = node.getBBox()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
76 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
77 attrs.append(attr)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
78 bbox = node.getBBox()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
79 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
80 attrs.append(attr)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
81 bbox = node.getBBox()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
82 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
83 attrs.append(attr)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
84 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
85 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
86
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
87 if attrs:
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
88 attrs_str = ' '.join(attrs)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
89 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
90 else:
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
91 line = '<%s/>' % (node_name)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
92 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
93 _print_level(line, lvl, out)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
94 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
95
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
96 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
97 line = node.content()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
98 _print_level(line, lvl, out)
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
99 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
100
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
101 children = node.childList()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
102 if not children:
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
103 if node.name() != 'string':
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
104 _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
105 else:
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
106 _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
107 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
108 return
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
109
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
110 _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
111 for child in children:
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
112 _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
113 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
114 _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
115 pass
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 class mbbbox_ext(pybExtension.PYBindExtImp):
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
118 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
119 out = file(filename, 'w+')
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
120
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
121 root = doc.rdoc.root()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
122 _print_subtree(root, 0, out)
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 out.close()
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
125 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
126 pass
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
127
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
128
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
129 extension = (mbbbox_ext(),
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
130 'net.scribboo.mbbbox',
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
131 'SVG with BBox exporter',
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
132 'output',
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
133 {'extension': '.svg',
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
134 'mimetype': 'image/svg+xml',
2d56ed5b0995 Add exporter for export svg with bbox information.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
135 '_filetypename': 'SVG+BBox (*.svg)'})