#!/usr/bin/env python
import sys, re, os
svgheader = """<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 13.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 14948) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="2230px" height="3119px" viewBox="0 0 2230 3119" enable-background="new 0 0 2230 3119" xml:space="preserve">
<image overflow="visible" width="2230" height="3119" xlink:href="3E6C89C7.jpg" >
</image>
"""
svgfooter = """
</svg>
"""
height = 3119
offset = -38
if len(sys.argv) < 2:
print "Usage: box2svg.py <filename.box>"
sys.exit(2)
boxfile = sys.argv[1]
q = re.match("(.*).box", boxfile)
if not q:
print "Input is not a box file!"
sys.exit(2)
else:
basefile = q.groups(1)[0]
inf = open(basefile + '.box', 'r')
if os.path.exists(basefile + '.svg'):
# backup the original file
print "%s.svg has been saved to %s.svg.orig" % (basefile, basefile)
os.system('mv %s.svg %s.svg.orig' % (basefile, basefile))
outf = open(basefile + '.svg', 'w')
print "Converting %s.box to %s.svg" % (basefile, basefile)
outf.write(svgheader)
colors = [
"#ff0000",
"#ffff00",
"#00ff00",
"#00ffff",
"#0000ff",
"#ff00ff"
]
color = 0
for line in inf.readlines():
if line.startswith("#") or line == "\n":
pass
else:
char, startx, starty, endx, endy = line.split(" ")
startx = int(startx)
starty = int(starty)
endx = int(endx)
endy = int(endy)
# need to invert the y coords
starty = height - starty
endy = height - endy
rectwidth = endx - startx
rectheight = starty - endy
outf.write('<rect x="%s" y="%s" fill="none" stroke="%s" stroke-width="2" width="%s" height="%s"/>\n' % (startx, starty + offset, colors[color], rectwidth, rectheight))
color += 1
if color > 5:
color = 0
textx = startx + 0
#texty = starty + offset + rectheight + 0
texty = starty + offset
q = re.match("[A-Z]", char)
if not q:
char = "?"
outf.write('<text transform="matrix(1 0 0 1 %d %d)" font-family="\'ArialMT\'" font-size="24">%s</text>\n' % (textx, texty, char))
outf.write(svgfooter)
outf.close()
inf.close()