Espri is now part of Objavi, so change comments
[objavi2.git] / font-list.cgi.pdf
blob488f46fde84a0afd2561f8d8c9c545ecd0b0ae53
1 #!/usr/bin/python
3 # Part of Objavi2, which turns html manuals into books.
4 # This python script generates lists of fonts in pdf and html form.
6 # Copyright (C) 2009 Douglas Bagnall
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 from objavi import config
23 from objavi.book_utils import log
24 import tempfile, os, re, sys
25 import cgi
26 import hashlib
27 from subprocess import Popen, check_call, PIPE
29 def get_font_list():
30     p = Popen(['fc-list'], stdout=PIPE, stderr=PIPE)
31     out, err = p.communicate()
32     fonts = set(re.findall(r"^([^:,]+)", out.strip().replace('\-', '-'), re.M))
33     return sorted(fonts, key=str.lower)
36 def font_html(fonts, example_template, dir="LTR"):
37     html = ['<html dir="%s"><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>'
38             '.font-name {background: #ffc; padding: 0.25em; font-family: "Dejavu Sans", sans-serif}'
39             ' div{padding:0.9em 0}'
40             '</style><body>' % dir]
41     for f in fonts:
42         html.append(example_template % {'font':f})
43     html.append('</body></html>')
44     return '\n'.join(html)
46 def font_pdf(html, pdfname):
47     fh, htmlname = tempfile.mkstemp(suffix='.html', dir=config.TMPDIR)
48     os.write(fh, html)
49     os.close(fh)
50     cmd = ['xvfb-run', config.WKHTMLTOPDF, '-q', '-s', 'A4',
51            htmlname, pdfname]
53     p = Popen(cmd, stdout=PIPE, stderr=PIPE)
54     r = p.communicate()
55     log(r)
57 def html_font_list(fonts, name):
58     f = open(name, 'w')
59     for x in fonts:
60         f.write(x + '\n')
61     f.close()
63 #Instead of regenerating the pdf every time, which is expensive, keep
64 #a cached version indexed by the font list and script version
65 fonts = get_font_list()
66 h = hashlib.sha1(str(fonts))
68 #Any change to this file will effectively clear the cache.
69 f = open(__file__)
70 h.update(f.read())
71 f.close()
73 #XXX should also include the script example include in the hash
75 form = cgi.FieldStorage()
76 script = form.getfirst('script')
77 if script not in (x for x in os.listdir(config.FONT_EXAMPLE_SCRIPT_DIR) if x.isalnum()):
78     script = 'latin'
80 pdfname = os.path.join(config.BOOK_LIST_CACHE_DIR, 'font-list-%s-%s.pdf' %(script, h.hexdigest()))
82 if not os.path.exists(pdfname):
83     #So this particular font list has not been made before
84     f = open(os.path.join(config.FONT_EXAMPLE_SCRIPT_DIR, script))
85     example_template = f.read()
86     f.close()
87     dir = 'LTR'
88     if script in config.RTL_SCRIPTS:
89         dir = 'RTL'
90     html = font_html(fonts, example_template, dir)
91     font_pdf(html, pdfname)
93     include_name = os.path.join(config.BOOK_LIST_CACHE_DIR, 'font-list.inc')
94     html_font_list(fonts, include_name)
98 print "Content-type: application/pdf\n"
99 f = open(pdfname)
100 print f.read()
101 f.close()
102 sys.exit()