2018-12-29 19:14:41 +00:00
|
|
|
#! /usr/bin/python3
|
2012-12-16 13:38:21 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# file generate_symbols_images.py
|
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
|
|
|
|
# author Georg Baum
|
2016-06-05 11:21:03 +00:00
|
|
|
# author Juergen Spitzmueller (adaptation for SVG)
|
2012-12-16 13:38:21 +00:00
|
|
|
|
|
|
|
# Full author contact details are available in file CREDITS
|
|
|
|
|
2016-06-05 11:21:03 +00:00
|
|
|
# This script generates a toolbar image for each missing math symbol.
|
|
|
|
# It needs the template document generate_symbols_images.lyx for generating
|
|
|
|
# the png image via preview.sty and dvipng, or the template document
|
|
|
|
# generate_symbols_svg.lyx for generating the SVG image via dvisvgm.
|
|
|
|
# Either document must contain the placeholder formula '$a$'.
|
2012-12-16 13:38:21 +00:00
|
|
|
# The created images are not always optimal, therefore the existing manually
|
|
|
|
# created images should never be replaced by automatically created ones.
|
|
|
|
|
|
|
|
|
2016-06-05 10:51:07 +00:00
|
|
|
from __future__ import print_function
|
2012-12-16 13:38:21 +00:00
|
|
|
import os, re, string, sys, subprocess, tempfile, shutil
|
|
|
|
import Image
|
2016-06-05 10:51:07 +00:00
|
|
|
import io
|
2012-12-16 13:38:21 +00:00
|
|
|
|
|
|
|
def usage(prog_name):
|
2016-06-05 11:21:03 +00:00
|
|
|
return ("Usage: %s png|svg lyxexe outputpath\n" % prog_name)
|
2012-12-16 13:38:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def error(message):
|
|
|
|
sys.stderr.write(message + '\n')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def getlist(lyxexe, lyxfile):
|
|
|
|
""" Call LyX and get a list of symbols from mathed debug output.
|
|
|
|
This way, we can re-use the symbols file parser of LyX, and do not
|
|
|
|
need to reimplement it in python. """
|
|
|
|
|
|
|
|
# The debug is only generated if lyxfile contains a formula
|
|
|
|
cmd = "%s %s -dbg mathed -x lyx-quit" % (lyxexe, lyxfile)
|
|
|
|
proc = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
|
|
|
|
(stdout, stderr) = proc.communicate()
|
2013-01-09 21:23:07 +00:00
|
|
|
regexp = re.compile(r'.*: read symbol \'(\S+)\s+inset:\s+(\S+)\s+draw:\s+(\S*)\s+extra:\s+(\S+)')
|
2012-12-16 13:38:21 +00:00
|
|
|
# These insets are more complex than simply symbols, so the images need to
|
|
|
|
# be created manually
|
2012-12-31 16:17:45 +00:00
|
|
|
skipinsets = ['big', 'font', 'lyxblacktext', 'matrix', 'mbox', 'oldfont', \
|
|
|
|
'ref', 'split', 'space', 'style']
|
2013-01-09 21:23:07 +00:00
|
|
|
mathsymbols = []
|
|
|
|
textsymbols = []
|
2012-12-16 13:38:21 +00:00
|
|
|
for line in stderr.split('\n'):
|
|
|
|
m = regexp.match(line)
|
|
|
|
if m:
|
|
|
|
inset = m.group(2)
|
|
|
|
if not inset in skipinsets:
|
2013-01-09 21:23:07 +00:00
|
|
|
if m.group(4) == 'textmode':
|
|
|
|
textsymbols.append(m.group(1))
|
|
|
|
else:
|
|
|
|
mathsymbols.append(m.group(1))
|
|
|
|
return (mathsymbols, textsymbols)
|
2012-12-16 13:38:21 +00:00
|
|
|
|
|
|
|
|
2012-12-31 16:17:45 +00:00
|
|
|
def getreplacements(filename):
|
|
|
|
replacements = {}
|
|
|
|
replacements['|'] = 'vert'
|
|
|
|
replacements['/'] = 'slash'
|
|
|
|
replacements['\\'] = 'backslash'
|
|
|
|
replacements['*'] = 'ast'
|
|
|
|
replacements['AA'] = 'textrm_AA'
|
|
|
|
replacements['O'] = 'textrm_O'
|
2016-06-05 10:51:07 +00:00
|
|
|
cppfile = io.open(filename, 'r', encoding='utf_8')
|
2012-12-31 16:17:45 +00:00
|
|
|
regexp = re.compile(r'.*"([^"]+)",\s*"([^"]+)"')
|
|
|
|
found = False
|
|
|
|
for line in cppfile.readlines():
|
|
|
|
if found:
|
|
|
|
m = regexp.match(line)
|
|
|
|
if m:
|
|
|
|
replacements[m.group(1)] = m.group(2)
|
|
|
|
else:
|
|
|
|
return replacements
|
2016-06-04 18:13:37 +00:00
|
|
|
elif line.find('ImgMap sorted_img_map') == 0:
|
2012-12-31 16:17:45 +00:00
|
|
|
found = True
|
|
|
|
|
|
|
|
|
2013-01-08 20:39:24 +00:00
|
|
|
def gettoolbaritems(filename):
|
|
|
|
items = []
|
2016-06-05 10:51:07 +00:00
|
|
|
uifile = io.open(filename, 'r', encoding='utf_8')
|
2013-01-08 20:39:24 +00:00
|
|
|
regexp = re.compile(r'.*Item "([^"\[]+)(\[\[[^\]]+\]\])?"\s*"math-insert\s+([^"]+)"')
|
|
|
|
for line in uifile.readlines():
|
|
|
|
m = regexp.match(line)
|
|
|
|
if m:
|
|
|
|
if '\\' + m.group(1) == m.group(3):
|
|
|
|
items.append(m.group(1))
|
|
|
|
return items
|
|
|
|
|
|
|
|
|
|
|
|
def getmakefileentries(filename):
|
|
|
|
items = []
|
2016-06-05 10:51:07 +00:00
|
|
|
makefile = io.open(filename, 'r', encoding='utf_8')
|
2016-06-05 10:25:48 +00:00
|
|
|
regexp = re.compile(r'.*images/math/(.+)\.(png|svgz)')
|
2013-01-08 20:39:24 +00:00
|
|
|
for line in makefile.readlines():
|
|
|
|
m = regexp.match(line)
|
|
|
|
if m:
|
|
|
|
items.append(m.group(1))
|
|
|
|
return items
|
|
|
|
|
|
|
|
|
2016-06-05 11:21:03 +00:00
|
|
|
def createimage(name, path, template, lyxexe, tempdir, math, replacements, toolbaritems, makefileentries, usepng):
|
2012-12-16 13:38:21 +00:00
|
|
|
""" Create the image file for symbol name in path. """
|
|
|
|
|
2012-12-31 16:17:45 +00:00
|
|
|
if name in replacements.keys():
|
|
|
|
filename = replacements[name]
|
2012-12-16 13:38:21 +00:00
|
|
|
elif name.startswith('lyx'):
|
2016-06-05 10:51:07 +00:00
|
|
|
print('Skipping ' + name)
|
2012-12-16 13:38:21 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
skipchars = ['|', '/', '\\', '*', '!', '?', ':', ';', '^', '<', '>']
|
|
|
|
for i in skipchars:
|
|
|
|
if name.find(i) >= 0:
|
2016-06-05 11:21:03 +00:00
|
|
|
print('Skipping ' + name)
|
2012-12-16 13:38:21 +00:00
|
|
|
return
|
|
|
|
filename = name
|
2016-06-05 11:21:03 +00:00
|
|
|
if usepng:
|
|
|
|
imgname = os.path.join(path, filename + '.png')
|
|
|
|
else:
|
|
|
|
imgname = os.path.join(path, filename + '.svgz')
|
2013-01-08 20:39:24 +00:00
|
|
|
if name in toolbaritems:
|
|
|
|
if filename in makefileentries:
|
|
|
|
suffix = ' (found in toolbar and makefile)'
|
|
|
|
else:
|
|
|
|
suffix = ' (found in only in toolbar)'
|
|
|
|
else:
|
|
|
|
if filename in makefileentries:
|
|
|
|
suffix = ' (found only in makefile)'
|
|
|
|
else:
|
|
|
|
suffix = ' (not found)'
|
2016-06-05 11:21:03 +00:00
|
|
|
if os.path.exists(imgname):
|
2016-06-05 10:51:07 +00:00
|
|
|
print('Skipping ' + name + suffix)
|
2012-12-16 13:38:21 +00:00
|
|
|
return
|
2016-06-05 10:51:07 +00:00
|
|
|
print('Generating ' + name + suffix)
|
2012-12-16 13:38:21 +00:00
|
|
|
lyxname = os.path.join(tempdir, filename)
|
2016-06-05 10:51:07 +00:00
|
|
|
lyxfile = io.open(lyxname + '.lyx', 'w', encoding='utf_8')
|
2013-01-09 21:23:07 +00:00
|
|
|
if math:
|
|
|
|
lyxfile.write(template.replace('$a$', '$\\' + name + '$'))
|
|
|
|
else:
|
|
|
|
lyxfile.write(template.replace('$a$', '$\\text{\\' + name + '}$'))
|
2012-12-16 13:38:21 +00:00
|
|
|
lyxfile.close()
|
|
|
|
cmd = "%s %s.lyx -e dvi" % (lyxexe, lyxname)
|
|
|
|
proc = subprocess.Popen(cmd, shell=True)
|
|
|
|
proc.wait()
|
|
|
|
if proc.returncode != 0:
|
2016-06-05 10:51:07 +00:00
|
|
|
print('Error in DVI creation for ' + name)
|
2012-12-16 13:38:21 +00:00
|
|
|
return
|
|
|
|
# The magnifaction factor is calculated such that we get an image of
|
|
|
|
# height 18 px for most symbols and document font size 11. Then we can
|
|
|
|
# add a small border to get the standard math image height of 20 px.
|
2016-06-05 11:21:03 +00:00
|
|
|
if usepng:
|
|
|
|
cmd = "dvipng %s.dvi -bg Transparent -D 115 -o %s" % (lyxname, imgname)
|
|
|
|
else:
|
2016-06-05 11:28:06 +00:00
|
|
|
cmd = "dvisvgm -z --no-fonts --exact --output=%s%s%%f %s.dvi" % (path, os.path.sep, lyxname)
|
2012-12-16 13:38:21 +00:00
|
|
|
proc = subprocess.Popen(cmd, shell=True)
|
|
|
|
proc.wait()
|
|
|
|
if proc.returncode != 0:
|
2016-06-05 11:21:03 +00:00
|
|
|
if png:
|
|
|
|
print('Error in PNG creation for ' + name)
|
|
|
|
else:
|
|
|
|
print('Error in SVG creation for ' + name)
|
2012-12-16 13:38:21 +00:00
|
|
|
return
|
2016-06-05 11:21:03 +00:00
|
|
|
if not usepng:
|
|
|
|
return
|
|
|
|
image = Image.open(imgname)
|
2012-12-16 13:38:21 +00:00
|
|
|
(width, height) = image.size
|
|
|
|
if width < 20 and height < 20:
|
|
|
|
if width == 19 and height == 19:
|
|
|
|
padded = Image.new('RGBA', (width+1, height+1), (0, 0, 0, 0))
|
|
|
|
padded.paste(image, (0, 0))
|
|
|
|
elif width == 19:
|
|
|
|
padded = Image.new('RGBA', (width+1, height+2), (0, 0, 0, 0))
|
|
|
|
padded.paste(image, (0, 1))
|
|
|
|
elif height == 19:
|
|
|
|
padded = Image.new('RGBA', (width+2, height+1), (0, 0, 0, 0))
|
|
|
|
padded.paste(image, (1, 0))
|
|
|
|
else:
|
|
|
|
padded = Image.new('RGBA', (width+2, height+2), (0, 0, 0, 0))
|
|
|
|
padded.paste(image, (1, 1))
|
|
|
|
padded.convert(image.mode)
|
2016-06-05 11:21:03 +00:00
|
|
|
padded.save(imgname, "PNG")
|
2012-12-16 13:38:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
|
2016-06-05 11:21:03 +00:00
|
|
|
if len(argv) == 4:
|
2012-12-16 13:38:21 +00:00
|
|
|
(base, ext) = os.path.splitext(argv[0])
|
2016-06-05 11:21:03 +00:00
|
|
|
(mathsymbols, textsymbols) = getlist(argv[2], base)
|
2019-07-20 21:06:05 +00:00
|
|
|
cppfile = os.path.join(os.path.dirname(base), '../../src/frontends/qt/GuiApplication.cpp')
|
2012-12-31 16:17:45 +00:00
|
|
|
replacements = getreplacements(cppfile)
|
2013-01-08 20:39:24 +00:00
|
|
|
uifile = os.path.join(os.path.dirname(base), '../../lib/ui/stdtoolbars.inc')
|
|
|
|
toolbaritems = gettoolbaritems(uifile)
|
|
|
|
makefile = os.path.join(os.path.dirname(base), '../../lib/Makefile.am')
|
|
|
|
makefileentries = getmakefileentries(makefile)
|
2016-06-05 11:21:03 +00:00
|
|
|
if argv[1] == 'png':
|
|
|
|
lyxtemplate = base + '.lyx'
|
|
|
|
usepng = True
|
|
|
|
elif argv[1] == 'svg':
|
|
|
|
lyxtemplate = os.path.join(os.path.dirname(base), 'generate_symbols_svg.lyx')
|
|
|
|
usepng = False
|
|
|
|
else:
|
|
|
|
error(usage(argv[0]))
|
2016-06-05 10:51:07 +00:00
|
|
|
templatefile = io.open(base + '.lyx', 'r', encoding='utf_8')
|
2012-12-16 13:38:21 +00:00
|
|
|
template = templatefile.read()
|
|
|
|
templatefile.close()
|
|
|
|
tempdir = tempfile.mkdtemp()
|
2013-01-09 21:23:07 +00:00
|
|
|
for i in mathsymbols:
|
2016-06-05 11:21:03 +00:00
|
|
|
createimage(i, argv[3], template, argv[2], tempdir, True, replacements, toolbaritems, makefileentries, usepng)
|
2013-01-09 21:23:07 +00:00
|
|
|
for i in textsymbols:
|
2016-06-05 11:21:03 +00:00
|
|
|
createimage(i, argv[3], template, argv[2], tempdir, False, replacements, toolbaritems, makefileentries, usepng)
|
2012-12-16 13:38:21 +00:00
|
|
|
shutil.rmtree(tempdir)
|
|
|
|
else:
|
|
|
|
error(usage(argv[0]))
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv)
|