Dear developers of Tex4ht and pgfplot,
I now fixed the script I wrote last night to work with all my graphs to make
all legends legible when converting LateX to HTML through TeX4ht.
It does depend on the font-size of the legend being 10.95, as I hardcoded
lineheight to be 12.95. Also notice that the original SVGs claim to be
encoded in ISO-8859-1, whereas they really are encoded in UTF-8. This causes
problems with some viewers. Using this script it simply exchanges the
encoding that is claimed to be used.
This is only meant as a reference to whoever wants to try to fix the
original sourcecode.
I hope this will help someone some day.
--
Johannes Wilm
http://www.johanneswilm.org
tel: +1 (520) 399 8880
#!/usr/bin/python
from xml import dom
from xml.dom.minidom import parseString
import sys
def xml_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():
xml_remove_whitespace_nodes(child, unlink)
for node in remove_list:
node.parentNode.removeChild(node)
if unlink:
node.unlink()
def fix_svg_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'# Change text-node to a g-node
main_text_node.removeAttribute('transform')# Remove all the attributes of the node
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(19,2)") # Move them 19 to the right and two down. That way they stand right next to the symbol-line
legend_text.setAttribute("text-anchor","left")# set their text anchor to the left rather then center. This is the only thing that seems to work with all labels
return True
import codecs
def fix_svg(command_option):
svg_file = codecs.open(command_option, 'r', 'utf-8')
svg_file_contents = svg_file.read()
svg_file.close()
fixed_svg_file_contents=svg_file_contents.replace('encoding="iso-8859-1"','encoding="utf-8"').encode('utf-8')#The encodign that the svg claims to have is false, it really is utf-8.
svg_contents = parseString(fixed_svg_file_contents)
xml_remove_whitespace_nodes(svg_contents)
legends = [node for node in svg_contents.getElementsByTagName("text") if node.hasChildNodes() and 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_svg_legend(legend)
svg_file = codecs.open(command_option, 'w', 'utf-8-sig') # open svg file in overwrite mode
#svg_contents_string =
svg_contents.writexml(svg_file)
#svg_file.write(svg_contents_string)
svg_file.close()
return True
if __name__ == "__main__":
sys_argv=sys.argv
if len(sys_argv) > 1:
command_option=sys_argv.pop(1)
fix_svg(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