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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#!/usr/bin/env python3
# coding: utf-8
# Copyright (C) 2017-present Robert Griesel
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
import gi
gi.require_version('Poppler', '0.18')
from gi.repository import Poppler
import xml.etree.ElementTree as ET
import subprocess, os, os.path
import re
folders = [
'arrows',
'greek_letters',
'operators',
'misc_math',
'misc_text',
'relations'
]
def generate_tex(border_h, border_v):
tex_file = '''\\documentclass[12pt, border={ ''' + str(border_h) + 'pt ' + str(border_v) + '''pt }]{standalone}\n
\\usepackage[T1]{fontenc}\n
'''
try: tex_file += '\\usepackage{' + attrib['package'] + '}\n'
except KeyError: pass
tex_file += '\\begin{document}\n'
try: is_math = attrib['math']
except KeyError: is_math = '0'
try: command = attrib['gencommand']
except KeyError: command = attrib['command']
if is_math == '1':
tex_file += '\\ensuremath{' + command + '}\n'
else:
tex_file += command + '\n'
tex_file += '\\end{document}\n'
return tex_file
for folder in folders:
tree = ET.parse('../data/resources/symbols/' + folder + '.xml')
root = tree.getroot()
for child in root:
attrib = child.attrib
# make pdf
tex_file = generate_tex(1, 1)
with open('./temp.tex', 'w') as f: f.write(tex_file)
arguments = ['xelatex', 'temp.tex']
process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait()
process.kill()
# get pdf size
document = Poppler.Document.new_from_file('file:' + os.path.dirname(os.path.realpath(__file__)) + '/temp.pdf')
page = document.get_page(0)
size = page.get_size()
# compute borders to make the image square
if size.width > size.height:
border_h = 1
border_v = 1 + 1.004 * (size.width - size.height) / 2
else:
border_h = 1 + (size.height - size.width) / 2
border_v = 1
# make pdf again with adapted borders
tex_file = generate_tex(border_h, border_v)
with open('./temp.tex', 'w') as f: f.write(tex_file)
arguments = ['xelatex', 'temp.tex']
process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait()
process.kill()
# make svg
try: os.mkdir('../data/resources/symbols')
except FileExistsError: pass
try: os.mkdir('../data/resources/symbols/' + folder)
except FileExistsError: pass
try: os.mkdir('../data/resources/symbols/' + folder + '/hicolor')
except FileExistsError: pass
try: os.mkdir('../data/resources/symbols/' + folder + '/hicolor/scalable')
except FileExistsError: pass
try: os.mkdir('../data/resources/symbols/' + folder + '/hicolor/scalable/actions')
except FileExistsError: pass
arguments = ['pdf2svg', 'temp.pdf', '../data/resources/symbols/' + folder + '/hicolor/scalable/actions/sidebar-' + attrib['file'][:-4] + '-symbolic.svg']
process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait()
# get image size
arguments = ['inkscape', '--export-filename=temp.png', '../data/resources/symbols/' + folder + '/hicolor/scalable/actions/sidebar-' + attrib['file'][:-4] + '-symbolic.svg']
process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait()
process.communicate()
process.kill()
arguments = ['file', 'temp.png']
process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait()
output = process.communicate()
output = output[0].decode('utf8')
width_match = re.search('PNG image data, ([0-9]+) x ([0-9]+),', output)
process.kill()
child.set('original_width', width_match.group(1))
child.set('original_height', width_match.group(2))
tree.write('../data/resources/symbols/' + folder + '.xml')
# delete helper files
os.remove('temp.tex')
os.remove('temp.log')
os.remove('temp.aux')
os.remove('temp.pdf')
os.remove('temp.png')
|