-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
172 lines (134 loc) · 5.84 KB
/
app.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
from flask import Flask, jsonify, request, send_file, make_response
from fontTools.ttLib import TTFont, newTable
import io
import requests
import json
from flask_cors import CORS # Import CORS from flask_cors
import mimetypes
app = Flask(__name__)
# Configure the first CORS instance
CORS(app)
# Helper function to serialize Panose data
def serialize_panose(panose_obj):
return {
'bFamilyType': panose_obj.bFamilyType,
'bSerifStyle': panose_obj.bSerifStyle,
# Add more properties as needed
}
# Custom JSON encoder to handle Panose objects
class FontDataEncoder(json.JSONEncoder):
def default(self, obj):
if hasattr(obj, '__dict__'):
return obj.__dict__
return super().default(obj)
@app.route('/api/get-font-data', methods=['POST'])
def get_font_data():
try:
font_url = request.json.get('fontUrl')
font = TTFont(io.BytesIO(requests.get(font_url).content))
# Extract font data (modify this part to extract specific data you need)
font_data = {
'hhea': font['hhea'].__dict__,
'head': font['head'].__dict__,
'os2': font['OS/2'].__dict__,
# Add more attributes as needed
}
# Serialize Panose data from the 'OS/2' table
os2_table = font['OS/2']
if hasattr(os2_table, 'panose'):
font_data['os2']['panose'] = serialize_panose(os2_table.panose)
return json.dumps(font_data, cls=FontDataEncoder), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/get-all-font-data', methods=['POST'])
def get_all_font_data():
try:
font_url = request.json.get('fontUrl')
font = TTFont(io.BytesIO(requests.get(font_url).content))
return json.dumps(font.toXML(), indent=4), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/get-font-data-buffer', methods=['POST'])
def extract_font_data_from_buffer():
try:
font_file = request.files['fontFile']
font_buffer = font_file.read()
font = TTFont(io.BytesIO(font_buffer))
# Extract font data (modify this part to extract specific data you need)
font_data = {
'hhea': font['hhea'].__dict__,
'head': font['head'].__dict__,
'os2': font['OS/2'].__dict__,
# Add more attributes as needed
}
# Serialize Panose data from the 'OS/2' table
os2_table = font['OS/2']
if hasattr(os2_table, 'panose'):
font_data['os2']['panose'] = os2_table.panose
return json.dumps(font_data, cls=FontDataEncoder), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/update-font-data', methods=['POST'])
def update_font_data():
try:
# Check if 'fontUrl' and 'newData' keys exist in the JSON data
if 'fontUrl' not in request.json or 'newData' not in request.json:
return jsonify({'error': 'fontUrl and/or newData missing in JSON data'}), 400
font_url = request.json.get('fontUrl')
new_data = request.json.get('newData')
usWinAscent = new_data['usWinAscent']
usWinDescent = new_data['usWinDescent']
font = TTFont(io.BytesIO(requests.get(font_url).content))
# Determine the MIME type based on the font_url
mime_type, _ = mimetypes.guess_type(font_url)
if 'OS/2' in font:
os2 = font['OS/2']
os2.usWinAscent = usWinAscent
os2.usWinDescent = usWinDescent
buffer = io.BytesIO()
font.save(buffer)
# Create a Flask HTTP response with the modified font
response = make_response(buffer.getvalue())
# Set the Content-Type header
response.headers['Content-Type'] = mime_type if mime_type else 'application/octet-stream'
# Set the Content-Disposition header with the correct filename
response.headers['Content-Disposition'] = 'attachment; filename=myfont_modified.ttf'
# Set the Content-Length header
response.headers['Content-Length'] = len(response.data)
# Close the buffer
buffer.close()
return response
except Exception as e:
return jsonify({'error': str(e)}), 400
@app.route('/api/update-font-data-from-file', methods=['POST'])
def update_font_data_from_file():
try:
# Check if 'fontFile' and 'newData' keys exist in the request
if 'fontFile' not in request.files or 'newData' not in request.form:
return jsonify({'error': 'fontFile and/or newData missing in the request'}), 400
font_file = request.files['fontFile']
new_data = json.loads(request.form['newData'])
usWinAscent = new_data['usWinAscent']
usWinDescent = new_data['usWinDescent']
font = TTFont(io.BytesIO(font_file.read()))
# Determine the MIME type based on the font file
mime_type, _ = mimetypes.guess_type(font_file.filename)
if 'OS/2' in font:
os2 = font['OS/2']
os2.usWinAscent = usWinAscent
os2.usWinDescent = usWinDescent
buffer = io.BytesIO()
font.save(buffer)
# Create a Flask HTTP response with the modified font
response = make_response(buffer.getvalue())
# Set the Content-Type header
response.headers['Content-Type'] = mime_type if mime_type else 'application/octet-stream'
# Set the Content-Disposition header with the correct filename
response.headers['Content-Disposition'] = f'attachment; filename={font_file.filename}_modified.ttf'
# Set the Content-Length header
response.headers['Content-Length'] = len(response.data)
# Close the buffer
buffer.close()
return response
except Exception as e:
return jsonify({'error': str(e)}), 400