2 # -*- coding: utf-8 -*-
4 # file generate_symbols_images.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
10 # Full author contact details are available in file CREDITS
12 # This script generates a toolbar image for each missing math symbol
13 # It needs the template document generate_symbols_images.lyx, which must
14 # contain the placeholder formula '$a$' for generating the png image via
15 # preview.sty and dvipng.
16 # The created images are not always optimal, therefore the existing manually
17 # created images should never be replaced by automatically created ones.
20 import os, re, string, sys, subprocess, tempfile, shutil
24 return ("Usage: %s lyxexe outputpath\n" % prog_name)
28 sys.stderr.write(message + '\n')
32 def getlist(lyxexe, lyxfile):
33 """ Call LyX and get a list of symbols from mathed debug output.
34 This way, we can re-use the symbols file parser of LyX, and do not
35 need to reimplement it in python. """
37 # The debug is only generated if lyxfile contains a formula
38 cmd = "%s %s -dbg mathed -x lyx-quit" % (lyxexe, lyxfile)
39 proc = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
40 (stdout, stderr) = proc.communicate()
41 regexp = re.compile(r'.*: read symbol \'(\S+)\s+inset:\s+(\S+)')
42 # These insets are more complex than simply symbols, so the images need to
44 skipinsets = ['big', 'font', 'matrix', 'mbox', 'oldfont', 'ref', 'space']
46 for line in stderr.split('\n'):
47 m = regexp.match(line)
50 if not inset in skipinsets:
51 symbols.append(m.group(1))
55 def createimage(name, path, template, lyxexe, tempdir):
56 """ Create the image file for symbol name in path. """
63 filename = 'backslash'
66 elif name.startswith('lyx'):
67 print 'Skipping ' + name
70 skipchars = ['|', '/', '\\', '*', '!', '?', ':', ';', '^', '<', '>']
73 print 'Skipping ' + name
76 pngname = os.path.join(path, filename + '.png')
77 if os.path.exists(pngname):
78 print 'Skipping ' + name
80 print 'Generating ' + name
81 lyxname = os.path.join(tempdir, filename)
82 lyxfile = open(lyxname + '.lyx', 'wt')
83 lyxfile.write(template.replace('$a$', '$\\' + name + '$'))
85 cmd = "%s %s.lyx -e dvi" % (lyxexe, lyxname)
86 proc = subprocess.Popen(cmd, shell=True)
88 if proc.returncode != 0:
89 print 'Error in DVI creation for ' + name
91 # The magnifaction factor is calculated such that we get an image of
92 # height 18 px for most symbols and document font size 11. Then we can
93 # add a small border to get the standard math image height of 20 px.
94 cmd = "dvipng %s.dvi -bg Transparent -D 115 -o %s" % (lyxname, pngname)
95 proc = subprocess.Popen(cmd, shell=True)
97 if proc.returncode != 0:
98 print 'Error in PNG creation for ' + name
100 image = Image.open(pngname)
101 (width, height) = image.size
102 if width < 20 and height < 20:
103 if width == 19 and height == 19:
104 padded = Image.new('RGBA', (width+1, height+1), (0, 0, 0, 0))
105 padded.paste(image, (0, 0))
107 padded = Image.new('RGBA', (width+1, height+2), (0, 0, 0, 0))
108 padded.paste(image, (0, 1))
110 padded = Image.new('RGBA', (width+2, height+1), (0, 0, 0, 0))
111 padded.paste(image, (1, 0))
113 padded = Image.new('RGBA', (width+2, height+2), (0, 0, 0, 0))
114 padded.paste(image, (1, 1))
115 padded.convert(image.mode)
116 padded.save(pngname, "PNG")
122 (base, ext) = os.path.splitext(argv[0])
123 symbols = getlist(argv[1], base)
124 lyxtemplate = base + '.lyx'
125 templatefile = open(base + '.lyx', 'rt')
126 template = templatefile.read()
128 tempdir = tempfile.mkdtemp()
130 createimage(i, argv[2], template, argv[1], tempdir)
131 shutil.rmtree(tempdir)
133 error(usage(argv[0]))
138 if __name__ == "__main__":