1#!/usr/local/bin/python3.8
2# coding: utf-8
3
4# Copyright (C) 2017, 2018 Robert Griesel
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>
17
18import gi
19
20gi.require_version('Poppler', '0.18')
21from gi.repository import Poppler
22
23import xml.etree.ElementTree as ET
24import subprocess, os, os.path
25import re
26
27width_regex = re.compile('exported to ([0-9]+) x ([0-9]+) pixels')
28
29folders = [
30            'arrows',
31            'greek_letters',
32            'operators',
33            'misc_math',
34            'misc_text',
35            'relations'
36          ]
37
38def generate_tex(border_h, border_v):
39        tex_file = '''\\documentclass[12pt, border={ ''' + str(border_h) + 'pt ' + str(border_v) + '''pt }]{standalone}\n
40%\\usepackage[T1]{fontenc}\n
41'''
42
43        try: tex_file += '\\usepackage{' + attrib['package'] + '}\n'
44        except KeyError: pass
45
46        tex_file += '\\begin{document}\n'
47
48        try: is_math = attrib['math']
49        except KeyError: is_math = '0'
50        try: command = attrib['gencommand']
51        except KeyError: command = attrib['command']
52
53        if is_math == '1':
54            tex_file += '\\ensuremath{' + command + '}\n'
55        else:
56            tex_file += command + '\n'
57
58        tex_file += '\\end{document}\n'
59
60        return tex_file
61
62
63for folder in folders:
64    tree = ET.parse('../setzer/resources/symbols/' + folder + '.xml')
65    root = tree.getroot()
66
67    for child in root:
68        attrib = child.attrib
69
70        # make pdf
71        tex_file = generate_tex(1, 1)
72        with open('./temp.tex', 'w') as f: f.write(tex_file)
73        arguments = ['xelatex', 'temp.tex']
74        process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
75        process.wait()
76        process.kill()
77
78        # get pdf size
79        document = Poppler.Document.new_from_file('file:' + os.path.dirname(os.path.realpath(__file__)) + '/temp.pdf')
80        page = document.get_page(0)
81        size = page.get_size()
82
83        # compute borders to make the image square
84        if size.width > size.height:
85            border_h = 1
86            border_v = 1 + 1.004 * (size.width - size.height) / 2
87        else:
88            border_h = 1 + (size.height - size.width) / 2
89            border_v = 1
90
91        # make pdf again with adapted borders
92        tex_file = generate_tex(border_h, border_v)
93        with open('./temp.tex', 'w') as f: f.write(tex_file)
94        arguments = ['xelatex', 'temp.tex']
95        process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
96        process.wait()
97        process.kill()
98
99        # make svg
100        try: os.mkdir('../setzer/resources/symbols')
101        except FileExistsError: pass
102        try: os.mkdir('../setzer/resources/symbols/' + folder)
103        except FileExistsError: pass
104
105        arguments = ['pdf2svg', 'temp.pdf', '../setzer/resources/symbols/' + folder + '/sidebar-' + attrib['file'][:-4] + '-symbolic.svg']
106        process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
107        process.wait()
108
109        # get image size
110        arguments = ['inkscape', '--export-png=temp.png', '../setzer/resources/symbols/' + folder + '/sidebar-' + attrib['file'][:-4] + '-symbolic.svg']
111        process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
112        process.wait()
113        output = process.communicate()
114        output = output[0].decode('utf8')
115        width_match = width_regex.search(output)
116
117        process.kill()
118        child.set('original_width', width_match.group(1))
119        child.set('original_height', width_match.group(2))
120        tree.write('../setzer/resources/symbols/' + folder + '.xml')
121
122        # delete helper files
123        os.remove('temp.tex')
124        os.remove('temp.log')
125        os.remove('temp.aux')
126        os.remove('temp.pdf')
127        os.remove('temp.png')
128
129
130