File: latexicons.py

package info (click to toggle)
scribus 1.5.8%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 179,080 kB
  • sloc: cpp: 483,300; python: 65,124; xml: 14,334; ansic: 2,484; sh: 192; perl: 95; makefile: 79
file content (61 lines) | stat: -rwxr-xr-x 1,525 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
"""Create icons from list of latex symbols."""
import sys
import string
import subprocess

retcode = None
cmd_output = None

def conv_filename(name):
	"""Remove all bad characters and convert to all lowercase."""
	badchars = "\\/!\"$%&{}=?[]* "
	for char in badchars:
		name = name.replace(char, '')
	for char in string.ascii_uppercase:
		name = name.replace(char, char.lower()+'_')
	return name
	
	
def run_command(command):
	global retcode, cmd_output
	process = subprocess.Popen(command.split(' '), stderr=subprocess.PIPE, stdout=subprocess.PIPE)
	cmd_output = process.communicate()
	retcode = process.returncode
	return retcode

if len(sys.argv) < 3:
	print "usage: icons.py iconlist template"
	sys.exit(1)
	
symbols = open(sys.argv[1]).readlines()
template = open(sys.argv[2]).read()
xmlsample = open("xmlsample", 'w')

i = 0
for symbol in symbols:
	i += 1
	symbol = symbol.strip()
	if symbol.startswith('#') or symbol == "":
		continue
	print "%5.1f%%   %-30s ." % (float(i)/len(symbols)*100, symbol),
	source = template % symbol
	#print source
	f = open("tmp", 'w')
	f.write(source)
	f.close()
	
	if run_command("latex --interaction batchmode tmp") != 0:
		print "Error", cmd_output
		continue
	else:
		print ".",
		
	filename = conv_filename(symbol)
	if run_command("dvipng -D 200 -T tight -pp 1 -bg Transparent -o out/%s.png tmp.dvi" % filename) != 0:
		print "Error", cmd_output
		continue
	else:
		print "."
	xmlsample.write(r'<item value="%s"  image="%s.png" />' %(symbol, filename)+"\n")