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', 'lyxblacktext', 'matrix', 'mbox', 'oldfont', \
45 'ref', 'split', 'space', 'style']
47 for line in stderr.split('\n'):
48 m = regexp.match(line)
51 if not inset in skipinsets:
52 symbols.append(m.group(1))
56 def getreplacements(filename):
58 replacements['|'] = 'vert'
59 replacements['/'] = 'slash'
60 replacements['\\'] = 'backslash'
61 replacements['*'] = 'ast'
62 replacements['AA'] = 'textrm_AA'
63 replacements['O'] = 'textrm_O'
64 cppfile = open(filename, 'rt')
65 regexp = re.compile(r'.*"([^"]+)",\s*"([^"]+)"')
67 for line in cppfile.readlines():
69 m = regexp.match(line)
71 replacements[m.group(1)] = m.group(2)
74 elif line.find('PngMap sorted_png_map') == 0:
78 def gettoolbaritems(filename):
80 uifile = open(filename, 'rt')
81 regexp = re.compile(r'.*Item "([^"\[]+)(\[\[[^\]]+\]\])?"\s*"math-insert\s+([^"]+)"')
82 for line in uifile.readlines():
83 m = regexp.match(line)
85 if '\\' + m.group(1) == m.group(3):
86 items.append(m.group(1))
90 def getmakefileentries(filename):
92 makefile = open(filename, 'rt')
93 regexp = re.compile(r'.*images/math/(.+)\.png')
94 for line in makefile.readlines():
95 m = regexp.match(line)
97 items.append(m.group(1))
101 def createimage(name, path, template, lyxexe, tempdir, replacements, toolbaritems, makefileentries):
102 """ Create the image file for symbol name in path. """
104 if name in replacements.keys():
105 filename = replacements[name]
106 elif name.startswith('lyx'):
107 print 'Skipping ' + name
110 skipchars = ['|', '/', '\\', '*', '!', '?', ':', ';', '^', '<', '>']
112 if name.find(i) >= 0:
113 print 'Skipping ' + name
116 pngname = os.path.join(path, filename + '.png')
117 if name in toolbaritems:
118 if filename in makefileentries:
119 suffix = ' (found in toolbar and makefile)'
121 suffix = ' (found in only in toolbar)'
123 if filename in makefileentries:
124 suffix = ' (found only in makefile)'
126 suffix = ' (not found)'
127 if os.path.exists(pngname):
128 print 'Skipping ' + name + suffix
130 print 'Generating ' + name + suffix
131 lyxname = os.path.join(tempdir, filename)
132 lyxfile = open(lyxname + '.lyx', 'wt')
133 lyxfile.write(template.replace('$a$', '$\\' + name + '$'))
135 cmd = "%s %s.lyx -e dvi" % (lyxexe, lyxname)
136 proc = subprocess.Popen(cmd, shell=True)
138 if proc.returncode != 0:
139 print 'Error in DVI creation for ' + name
141 # The magnifaction factor is calculated such that we get an image of
142 # height 18 px for most symbols and document font size 11. Then we can
143 # add a small border to get the standard math image height of 20 px.
144 cmd = "dvipng %s.dvi -bg Transparent -D 115 -o %s" % (lyxname, pngname)
145 proc = subprocess.Popen(cmd, shell=True)
147 if proc.returncode != 0:
148 print 'Error in PNG creation for ' + name
150 image = Image.open(pngname)
151 (width, height) = image.size
152 if width < 20 and height < 20:
153 if width == 19 and height == 19:
154 padded = Image.new('RGBA', (width+1, height+1), (0, 0, 0, 0))
155 padded.paste(image, (0, 0))
157 padded = Image.new('RGBA', (width+1, height+2), (0, 0, 0, 0))
158 padded.paste(image, (0, 1))
160 padded = Image.new('RGBA', (width+2, height+1), (0, 0, 0, 0))
161 padded.paste(image, (1, 0))
163 padded = Image.new('RGBA', (width+2, height+2), (0, 0, 0, 0))
164 padded.paste(image, (1, 1))
165 padded.convert(image.mode)
166 padded.save(pngname, "PNG")
172 (base, ext) = os.path.splitext(argv[0])
173 symbols = getlist(argv[1], base)
174 cppfile = os.path.join(os.path.dirname(base), '../../src/frontends/qt4/GuiApplication.cpp')
175 replacements = getreplacements(cppfile)
176 uifile = os.path.join(os.path.dirname(base), '../../lib/ui/stdtoolbars.inc')
177 toolbaritems = gettoolbaritems(uifile)
178 makefile = os.path.join(os.path.dirname(base), '../../lib/Makefile.am')
179 makefileentries = getmakefileentries(makefile)
180 lyxtemplate = base + '.lyx'
181 templatefile = open(base + '.lyx', 'rt')
182 template = templatefile.read()
184 tempdir = tempfile.mkdtemp()
186 createimage(i, argv[2], template, argv[1], tempdir, replacements, toolbaritems, makefileentries)
187 shutil.rmtree(tempdir)
189 error(usage(argv[0]))
194 if __name__ == "__main__":