-
Notifications
You must be signed in to change notification settings - Fork 25
/
optimize.py
278 lines (204 loc) · 8.02 KB
/
optimize.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
269
270
271
272
273
274
275
276
277
278
import json
from functools import partial
from itertools import product
from math import ceil
from multiprocessing import Pool
from optparse import Values
from xml.dom import minidom, NotFoundErr, Node
from os import walk, path
from scour import scour
from svgelements import Path
from decimal import Decimal
svg_dir = path.join(path.dirname(path.realpath(__file__)), "svg")
output_dir = path.join(path.dirname(path.realpath(__file__)), "output")
def is_path(node):
return (
node.nodeType == Node.ELEMENT_NODE
and node.tagName == "path"
and node.hasAttribute("d")
)
def is_group(node):
return node.nodeType == Node.ELEMENT_NODE and node.tagName == "g"
def remove_tags(tag_names, doc):
for tag_name in tag_names:
elements = doc.getElementsByTagName(tag_name)
for element in elements:
parent = element.parentNode
parent.removeChild(element)
def remove_nodes(nodes):
for node in nodes:
parent = node.parentNode
parent.removeChild(node)
def move_node(node, new_parent):
moving_node = node.parentNode.removeChild(node)
new_parent.appendChild(moving_node)
def adjust_root_transform(node, page_number):
horizontal_offset = "-115" if page_number % 2 == 0 else "-55"
node.setAttribute(
"transform", f"matrix(1.3333333,0,0,-1.3333333,{horizontal_offset},640)"
)
def set_viewbox(node, width, height):
node.setAttribute("width", f"{width}")
node.setAttribute("height", f"{height}")
node.setAttribute("viewBox", f"0 0 {width} {height}")
def optimize_opening_page(doc):
set_viewbox(doc.firstChild, 235, 235)
root_group = [x for x in doc.firstChild.childNodes if x.tagName == "g"][0]
root_group.setAttribute("transform", "matrix(1.3333333,0,0,-1.3333333,-136,482)")
root_children = root_group.childNodes
content_group = root_children[-1]
content_group.setAttribute("id", "content")
ayah_marker_group = root_children[-2]
ayah_marker_group.setAttribute("id", "ayah_markers")
# remove decorations
decorative_nodes = root_children[0:-2]
remove_nodes(decorative_nodes)
# remove nested surah title
remove_nodes([content_group.firstChild.firstChild.lastChild])
def optimize_standard_page(doc, page_number, surahs):
set_viewbox(doc.firstChild, 345, 550)
root_group = [x for x in doc.firstChild.childNodes if x.tagName == "g"][0]
adjust_root_transform(root_group, page_number)
root_children = root_group.childNodes
content_group = root_children[-1]
content_group.setAttribute("id", "content")
ayah_marker_group = root_children[2]
ayah_marker_group.setAttribute("id", "ayah_markers")
content_children = content_group.childNodes
decorative_nodes = root_children[0:2] + root_children[3:-1] + content_children[0:-1]
# figure out surah header position
page_surahs = [x for x in surahs if x["pageNumber"] == page_number]
if len(page_surahs) > 0:
found = get_surah_header_positions(decorative_nodes)
if len(found) != len(page_surahs):
raise Exception("surah header count mismatch")
found_sorted = sorted(found, key=lambda pair: pair[1])
for i in range(len(page_surahs)):
s = page_surahs[i]
f = found_sorted[i]
s["headerPosition"] = float(round(f[1], 2))
# remove missed white background elements
bad_paths = [
x
for x in root_group.getElementsByTagName("path")
if "fill:#ffffff" in x.getAttribute("style")
]
# remove decorations
remove_nodes(decorative_nodes + bad_paths)
return page_surahs
def get_surah_header_positions(nodes):
found = []
for node in nodes:
if is_group(node):
found.extend(get_surah_header_positions(node.childNodes))
if is_path(node):
path_definition = node.getAttribute("d")
xmin, ymin, xmax, ymax = Path(path_definition).bbox()
width, height = xmax - xmin, ymax - ymin
if round(width) in range(245, 250) and round(height) in range(25, 30):
x, y = get_offset(node.parentNode)
found.append((x, y))
return found
def set_ayah_numbers(doc):
ayah_marker_group = doc.getElementById("ayah_markers")
ayah_markers = ayah_marker_group.childNodes
for node in ayah_markers:
# print ayah offset
x, y = get_offset(node)
node.setAttribute("ayah:x", str(round(x, 2)))
node.setAttribute("ayah:y", str(round(y, 2)))
def get_offset(node, x=0, y=0):
if node.firstChild is not None and is_path(node.firstChild):
path_definition = node.firstChild.getAttribute("d")
xmin, ymin, xmax, ymax = Path(path_definition).bbox()
# use path's bounding box to get the center of the ayah marker
x += Decimal(xmin + ((xmax - xmin) / 2))
y += Decimal(ymin + ((ymax - ymin) / 2))
try:
transform = scour.svg_transform_parser.parse(node.getAttribute("transform"))
while len(transform) > 0:
tr, vals = transform.pop()
if tr == "translate":
x += vals[0]
y += vals[1]
if tr == "matrix":
x = vals[0] * x + vals[2] * y + vals[4]
y = vals[1] * y + vals[3] * y + vals[5]
except NotFoundErr:
pass
except AttributeError:
pass
if node.parentNode is not None:
return get_offset(node.parentNode, x, y)
return x, y
def scour_xml(doc):
in_string = doc.toxml()
doc.unlink()
options = scour.sanitizeOptions(
Values(
{
"remove_descriptive_elements": True,
"enable_viewboxing": True,
"strip_ids": True,
"protect_ids_list": "ayah_markers,content",
}
)
)
# scour the string
out_string = scour.scourString(in_string, options)
# prepare the output xml.dom.minidom object
doc = minidom.parseString(out_string.encode("utf-8"))
# since minidom does not seem to parse DTDs properly
# manually declare all attributes with name "id" to be of type ID
# (otherwise things like doc.getElementById() won't work)
all_nodes = doc.getElementsByTagName("*")
for node in all_nodes:
try:
node.setIdAttribute("id")
except NotFoundErr:
pass
return doc
def process_file(filename, surahs):
print(f"Opening {filename}")
filepath = path.join(svg_dir, filename)
page_number = int(path.splitext(filename)[0])
doc = minidom.parse(filepath)
if filename in ["001.svg", "002.svg"]:
optimize_opening_page(doc)
out = None
else:
out = optimize_standard_page(doc, page_number, surahs)
doc.firstChild.setAttribute("xmlns:ayah", "https://quranapp.com")
# remove all clip-path attributes
all_nodes = doc.getElementsByTagName("*")
for node in all_nodes:
try:
node.removeAttribute("clip-path")
except NotFoundErr:
pass
doc = scour_xml(doc)
doc.firstChild.setAttribute("xmlns:ayah", "https://quranapp.com/svg")
set_ayah_numbers(doc)
out_string = doc.toxml()
doc.unlink()
with open(path.join(output_dir, filename), "w") as file:
file.write(out_string)
print(f"Processed {filename}")
return out
def optimize_svgs():
surahs_file = path.join(path.dirname(path.realpath(__file__)), "surah.json")
with open(surahs_file) as fp:
surahs = json.load(fp)
files = []
for (_, _, filenames) in walk(svg_dir):
svg_files = [file for file in filenames if file[-4:] == ".svg"]
files.extend(svg_files)
with Pool() as p:
updated_surahs = p.map(partial(process_file, surahs=surahs), files)
for page_surahs in [x for x in updated_surahs if x is not None]:
for surah in page_surahs:
surahs[surah["number"] - 1] = surah
with open(path.join(output_dir, "surah.json"), "w") as fp:
json.dump(surahs, fp, ensure_ascii=False, indent=4, sort_keys=True)
if __name__ == "__main__":
optimize_svgs()