Hi again,
as previously announced, here a small annotated script that fixes most of my
labels. I may still need to do small adjustments. It is mainly meant as an
aid for those who will hopefully eventually fix the real backend. :)
--
Johannes Wilm
http://www.johanneswilm.org
tel: +1 (520) 399 8880
#!/usr/bin/python
from xml import dom
from xml.dom.minidom import parse
import sys
def remove_whitespace_nodes(node, unlink=False):
"""Removes all of the whitespace-only text decendants of a DOM node.
When creating a DOM from an XML source, XML parsers are required to
consider several conditions when deciding whether to include
whitespace-only text nodes. This function ignores all of those
conditions and removes all whitespace-only text decendants of the
specified node. If the unlink flag is specified, the removed text
nodes are unlinked so that their storage can be reclaimed. If the
specified node is a whitespace-only text node then it is left
unmodified. This is a standard method that i grabbed of the
Internet."""
remove_list = []
for child in node.childNodes:
if child.nodeType == dom.Node.TEXT_NODE and \
not child.data.strip():
remove_list.append(child)
elif child.hasChildNodes():
remove_whitespace_nodes(child, unlink)
for node in remove_list:
node.parentNode.removeChild(node)
if unlink:
node.unlink()
def fix_legend(main_text_node):
"""We need to get rid of the text node that contains g-nodes by
turning it into a g-node. This is against SVG-specification for
text-nodex to contain g-nodex. All labels seem to contain this.
Then we need to move some nodes around."""
main_text_node.tagName = u'g'
main_text_node.removeAttribute('transform')
main_text_node.removeAttribute('text-anchor')
main_text_node.removeAttribute('style')
main_text_node.removeAttribute('font-size')
legend_childNodes = main_text_node.childNodes
total_legend_entries = len(legend_childNodes)/2
li = 0
while li < len(legend_childNodes):
symbol_node = legend_childNodes[li].cloneNode(deep=True) # copy the part that describes the symbol part of the description
main_text_node.removeChild(legend_childNodes[li]) #remove it from the xml-tree
legend_childNodes[li].firstChild.firstChild.firstChild.insertBefore(symbol_node, legend_childNodes[li].firstChild.firstChild.firstChild.firstChild) # ...and re-insert it into the node of the description text
legend_childNodes[li].firstChild.firstChild.firstChild.setAttribute("transform","matrix(1.0,0.0,0.0,1.0,0,"+str((total_legend_entries-li-1)*12.95)+")")# The numbers are inversed, so we need to count from button up. Add label entries with 12.95 line distance. This works for font-size 10.95
li += 1
legend_texts = [node for node in main_text_node.getElementsByTagName("text")] # Choose all label texts nodes used within the legend
for legend_text in legend_texts:
legend_text.setAttribute("transform","scale(1,-1) translate(48,2)") # And move them 48 to the right and two down. That way they stand right next to the symbol-line
return True
def main(command_option):
orig_svg = parse(command_option)
remove_whitespace_nodes(orig_svg)
legends = [node for node in orig_svg.getElementsByTagName("text") if node.firstChild.nodeName == 'g'] # we assume that whenever a g-node is inside a text-node we have found a legend
for legend in legends:
fix_legend(legend)
return orig_svg.toxml()
if __name__ == "__main__":
sys_argv=sys.argv
if len(sys_argv) > 1:
command_option=sys_argv.pop(1)
print main(command_option)
else:
print
print "Usage:"
print "fix_svg_legend.py FILENAME.svg > OUTPUT.svg"
print
exit()
------------------------------------------------------------------------------
Storage Efficiency Calculator
This modeling tool is based on patent-pending intellectual property that
has been used successfully in hundreds of IBM storage optimization engage-
ments, worldwide. Store less, Store more with what you own, Move data to
the right place. Try It Now! http://www.accelacomm.com/jaw/sfnl/114/51427378/
_______________________________________________
Pgfplots-features mailing list
Pgfplots-features@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pgfplots-features