forked from fontify/fontify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
executable file
·268 lines (216 loc) · 8.86 KB
/
hello.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#! /usr/bin/env python
# -*- coding: utf8 -*-
from __future__ import print_function
import os
import subprocess
import tempfile
import glob
try:
from StringIO import StringIO
except ImportError: # in Python 3
from io import StringIO
from flask import Flask
from flask import request
from flask import send_from_directory
from flask import url_for
from flask import redirect
from flask import make_response
from flask import render_template
from flask import jsonify
from pdfkit import from_string
from PyPDF2 import PdfFileMerger
# local imports
from data import TMPL_OPTIONS
from data import get_sample_chars, get_chars, get_chars_by_page
from data import get_sample_ligatures, get_ligatures, get_ligatures_by_page # FIXME add support for ligatures
# --- configuration for the application
HOST = '0.0.0.0'
PORT = 5000
UPLOAD_FOLDER = './upload'
DOWNLOAD_FOLDER = './download'
ALLOWED_EXTENSIONS = set(['jpg', 'png', 'jpeg', 'pdf'])
DPI = 300 # for PDF to PNG conversion
THRESHOLD = 75 # for gray-scale to black-white conversion
# --- create the Flask app and start to configure it
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER
# --- Flask app routes
@app.route("/")
def index():
return render_template('index.html')
@app.route("/finish")
def finish():
key = request.args.get('key')
font_name = request.args.get('font_name')
font_variant = request.args.get('font_variant')
return render_template(
'finish.html',
key=key,
font_name=font_name,
font_variant=font_variant
)
# --- DEBUG just to debug the template
def _template_html(get_inputs, get_sample_inputs):
extended = request.args.get('e') == 'true'
full = request.args.get('F') == 'true'
greek = request.args.get('g') == 'true'
accents = request.args.get('a') == 'true'
frenchspecials = request.args.get('f') == 'true'
spanishspecials = request.args.get('s') == 'true'
kwargs_get_data = dict(extended=extended, full=full, greek=greek, accents=accents, frenchspecials=frenchspecials, spanishspecials=spanishspecials)
print("kwargs_get_data =", kwargs_get_data) # DEBUG
return render_template(
'template.html',
chars=get_inputs(**kwargs_get_data),
sample=get_sample_inputs(),
css='static/css/template.css'
)
@app.route("/template_html")
def template_html():
return _template_html(get_chars, get_sample_chars)
@app.route("/template_ligatures_html")
def template_ligatures_html():
return _template_html(get_ligatures, get_sample_ligatures)
# --- PDF template
def _template(get_inputs_by_page, get_sample_inputs):
extended = request.args.get('e') == 'true'
full = request.args.get('F') == 'true'
greek = request.args.get('g') == 'true'
accents = request.args.get('a') == 'true'
frenchspecials = request.args.get('f') == 'true'
spanishspecials = request.args.get('s') == 'true'
kwargs_get_data = dict(extended=extended, full=full, greek=greek, accents=accents, frenchspecials=frenchspecials, spanishspecials=spanishspecials)
print("kwargs_get_data =", kwargs_get_data) # DEBUG
chars_by_page = get_inputs_by_page(**kwargs_get_data)
assert isinstance(chars_by_page, list)
assert isinstance(chars_by_page[0], list)
print("Number of pages:", len(chars_by_page))
pages = []
# create a fake PDF for each page
for page, chars in enumerate(chars_by_page):
print("Using chars =\n", chars) # DEBUG
html = render_template(
'template.html',
chars=chars,
sample=get_sample_inputs()
)
pdf = from_string(
html,
False,
options=TMPL_OPTIONS,
css='static/css/template.css',
)
pages.append(pdf)
# now merge the PDF
pdf = StringIO()
merger = PdfFileMerger()
for pgnb, page in enumerate(pages):
fakepdf = StringIO(page)
merger.append(fakepdf)
merger.write(pdf)
pdf_string = pdf.getvalue()
response = make_response(pdf_string)
response.headers['Content-Disposition'] = "filename=template.pdf"
response.mimetype = 'application/pdf'
return response
@app.route("/template")
def template():
return _template(get_chars_by_page, get_sample_chars)
@app.route("/template_ligatures")
def template_ligatures():
return _template(get_ligatures_by_page, get_sample_ligatures)
# --- demo of LaTeX & PDF document
@app.route('/test.tex')
def test_tex():
return app.send_static_file('test.tex')
@app.route('/test.pdf')
def test_pdf():
return app.send_static_file('test.pdf')
@app.route("/download/<key>/<full_font_name>")
def download(key, full_font_name):
return send_from_directory(
os.path.join(app.config['DOWNLOAD_FOLDER'], key),
full_font_name,
as_attachment=True
)
# --- first non trivial method
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route("/upload-file", methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
_, ext = os.path.splitext(file.filename)
_, filename = tempfile.mkstemp(
prefix='',
suffix=ext,
dir=app.config['UPLOAD_FOLDER']
)
file.save(filename)
filenames = [filename]
if filename.endswith('.pdf'):
filename_png = filename.replace('.pdf', '.png')
print("Converting '{}' to '{}'...".format(filename, filename_png)) # DEBUG
return_code = subprocess.call([
"convert",
"-density", str(DPI),
"-quality", "100",
filename,
filename_png
])
if return_code != 0:
raise ValueError("Call to 'convert -density 300 -quality 100 {} {}' failed... do you have 'convert' from imagemagick installed?".format(filename, filename_png))
filename = filename_png
filenames = [filename]
if not os.path.exists(filename):
filenames = sorted(glob.glob(filename.replace('.png', '-[0-9]*.png')))
for image in filenames:
print("Converting '{}' with colours to '{}' in gray scale...".format(image, image)) # DEBUG
return_code = subprocess.call([
"convert",
"-set", "colorspace", "Gray",
"-separate", "-average",
image,
image
])
if return_code != 0:
raise ValueError("Call to 'convert -set colorspace Gray -separate -average {} {}' failed... do you have 'convert' from imagemagick installed?".format(image, image))
# now convert to black-white only!
for image in filenames:
print("Converting '{}' in gray scale to '{}' in black and white (threshold = {})...".format(image, image, THRESHOLD)) # DEBUG
return_code = subprocess.call([
"convert",
"-threshold", "{}%".format(THRESHOLD),
image,
image
])
if return_code != 0:
raise ValueError("Call to 'convert -threshold {}% {} {}' failed... do you have 'convert' from imagemagick installed?".format(THRESHOLD, image, image))
font_name = request.form['font_name']
print("Using font_name =", font_name) # DEBUG
font_variant = request.form['font_variant']
print("Using font_variant =", font_variant) # DEBUG
key = filenames[0].split('/')[-1].split('.')[0]
print("Using key =", key) # DEBUG
os.mkdir(os.path.join(app.config['DOWNLOAD_FOLDER'], key))
call_to_fontify = [
"python",
"scripts/fontify.py",
"-n", font_name,
"-v", font_variant,
"-o", os.path.join(app.config['DOWNLOAD_FOLDER'], key, "fontify.ttf"),
] + filenames
return_code = subprocess.call(call_to_fontify)
if return_code != 0:
raise ValueError("Call to 'scripts/fontify.py' failed... Check log above.")
return jsonify(font_name=font_name, key=key, font_variant=font_variant)
return ''
# --- launch the application
if __name__ == "__main__":
for dirname in [ UPLOAD_FOLDER, DOWNLOAD_FOLDER ]:
if not os.path.isdir(dirname):
print("Directory {} is absent... creating it.".format(dirname)) # DEBUG
os.mkdir(dirname)
app.run(debug=True, host=HOST, port=PORT)