User:User A1/svgTinker.py
Appearance
#!/usr/bin/python
from BeautifulSoup import BeautifulStoneSoup
import string
import sys
import re
#Takes a stone-soup tag and applies various
#workaround fixes of dubious effectiveness
def fontFix(tag):
print type(tag);
bold=False;
italic=False;
dejavu=False;
dejaVuRe=re.compile("'?(?i)dejavusans.*")
boldRe=re.compile("(?i).*-bold.*");
italicRe=re.compile("(?i).*-italic.*");
fontRe=re.compile("(?i)-.*");
for i in tag.attrs :
print i
if i[0] == "font-family" :
#Check the font types and perform font substitution
bold=boldRe.match(i[1]);
italic=italicRe.match(i[1]);
dejavu=dejaVuRe.match(i[1]);
#Strip font bold/italic embed
tmp = fontRe.split(i[1]);
fontAttr=tmp[0];
i = (i[0],fontAttr)
break;
#if none of the above apply we can skip
if(not bold and not italic and not dejavu):
return;
str=""
if (bold) :
str=str+"bad bolding method ";
if(italic):
str=str+"bad italicising method ";
if(dejavu):
str=str+"wrong font name";
print "Fixing tag : " + str
print tag
#Otherwise we have work to do!
foundBold=False;
foundItalic=False;
for i in tag.attrs:
#find any bold font-weight tag
if i[0] == "font-weight":
#Check for bold
if(bold):
if not re.match(i[1],".*(?i)bold.*"):
#mutate(i);
i=(("font-weight", i[1] + ";Bold"));
foundBold=True;
#Check for bold
if(italic):
if not re.match(i[1],".*(?i)italic.*"):
#mutate(i);
i=(("font-weight", i[1] + ";Italic"));
foundItalic=True;
if i[0] == "font-family":
#Fix dejavu vs Deja Vu
if (dejavu):
#mutate(i);
i = (i[0],"DejaVu Sans");
if italic and not foundItalic:
tag.attrs.extend(("font-style","Italic"));
if bold and not foundBold:
tag.attrs.extend(("font-style","Bold"));
#Check to see if a small font is being used in conjunction with
def fontSizeFix(tag):
#TODO: Implement me
return;
#Crappy font substitution routine
def fontSub(tag):
preferredFont = [];
preferredFont.append((re.compile("(?i)Arial.*"),"DejaVu Sans"));
preferredFont.append((re.compile("(?i)Times new roman.*"),"Times"));
for i in tag.attrs :
if i[0] == "font-family" :
#Substitute fonts from our preferred font table
for j in preferredFont:
if j[0].match(i[1]):
i = ("font-family",j[1])
break;
return;
def main():
if(not len(sys.argv) == 3):
print "Usage: svgTinker.py inputFile outputFile"
quit(1);
f = open(sys.argv[1]);
if not f :
print "File does not exist or could not be read"
quit(1)
xmlText = f.read();
soup=BeautifulStoneSoup(xmlText);
tags=soup.findAll("text");
#Correct all font tags
for i in tags:
fontTag=False;
if(len(i.attrs)):
for j in i.attrs :
if re.match("(?i)font-family",j[0]):
fontTag=True;
break;
if fontTag :
fontFix(i);
fontSizeFix(i);
fontSub(i);
tags=soup.findAll("tspan");
#Nuke the tspans, preserving children
for i in tags:
i.extract();
try:
f=open(sys.argv[2],'w');
except:
print('Unable to open file for writing. aborting');
quit(1);
#save modified svg data
f.write(str(soup));
print("Wrote file : " + sys.argv[2]);
if __name__ == "__main__":
main()