-
Notifications
You must be signed in to change notification settings - Fork 0
/
raqq.py
587 lines (455 loc) · 17.8 KB
/
raqq.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import logging
import math
import os
import unicodedata
import harfbuzz as hb
import qahirah as qh
import linebreak
from number import format_number
ft = qh.get_ft_lib()
logging.basicConfig(format="%(asctime)s - %(message)s")
logger = logging.getLogger("typesetter")
logger.setLevel(logging.INFO)
DIGITS = ("٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩")
RIGH_JOINING = ("ا", "آ", "أ", "إ", "د", "ذ", "ر", "ز", "و", "ؤ")
GID_OFFSET = 0x10FFFF
# Make Cairo produces diff-able PDFs
os.environ["CAIRO_DEBUG_PDF"] = "1"
class Document:
"""Class representing the main document and holding document-wide settings
and state."""
def __init__(self, chapters, filename, debug):
logger.info("Initializing the document: %s", filename)
self.debug = debug
# Settings
# The defaults here roughly match “the 12-lines Mushaf”.
self.body_font = "Raqq.otf"
self.body_font_size = 125
self.lines_per_page = 5
self.leading = 102
self.text_width = 717
self.page_width = 1024
self.page_height = 755
# From top of page to first baseline.
self.top_margin = 193
self.text_start_pos = self.text_width + (self.page_width - self.text_width) / 2
self.shaper = Shaper(self)
self.surface = qh.PDFSurface.create(
filename, (self.page_width, self.page_height)
)
cr = self.cr = qh.Context.create(self.surface)
cr.set_font_face(self.shaper.make_qahira_face())
cr.set_font_size(self.body_font_size)
cr.set_source_colour(qh.Colour.grey(0))
self.chapters = chapters
def save(self):
lines = self._create_lines()
pages = self._create_pages(lines)
logger.info("Drawing pages…")
for page in pages:
page.draw(self.cr)
del self.cr
del self.surface
def _create_lines(self):
"""Processes each chapter and creates lines for the whole document."""
logger.info("Breaking text into lines…")
lines = []
for chapter in self.chapters:
lines.extend(self._process_chapter(chapter))
return lines
def _create_pages(self, lines):
"""Breaks the lines into pages"""
logger.info("Breaking lines into pages…")
npages = len(lines) // self.lines_per_page
if len(lines) % self.lines_per_page:
npages += 1
pages = [Page(self, [], 1)]
for i in range(npages):
page = Page(self, [], len(pages) + 1)
start = i * self.lines_per_page
end = min((i + 1) * self.lines_per_page, len(lines))
page.lines = lines[start:end]
pages.append(page)
return pages
def _create_heading(self, chapter):
boxes = self.shaper.shape_paragraph(chapter.get_heading_text())
return Heading(self, boxes)
def _process_chapter(self, chapter):
"""Shapes the text and breaks it into lines."""
logger.info("Chapter %d…", chapter.number)
lengths = [self.text_width]
text = ""
if chapter.opening:
text = "بسمِ الله الرَحمنِ الرحيمِ؞ "
nodes = self.shaper.shape_paragraph(text + chapter.text)
breaks = nodes.compute_breakpoints(lengths, tolerance=4, looseness=10)
assert breaks[-1] == len(nodes) - 1
lines = [self._create_heading(chapter)]
start = 0
for i, breakpoint in enumerate(breaks[1:]):
ratio = nodes.compute_adjustment_ratio(start, breakpoint, i, lengths)
boxes = []
for j in range(start, breakpoint):
box = nodes[j]
box.ratio = ratio
boxes.append(box)
lines.append(Line(self, boxes))
start = breakpoint + 1
return lines
class Chapter:
"""Class holding input text and metadata for a chapter."""
def __init__(self, text, number, name, place, opening, verses):
self.text = text
self.number = number
self.name = name
self.place = place
self.opening = opening
self.verses = verses
def get_heading_text(self):
verses = format_number(self.verses)
text = f"{self.name} {verses}"
return text
def _get_glyph(font, font_data, unicode, user_data):
if unicode > GID_OFFSET:
return unicode - GID_OFFSET
return font.parent.get_nominal_glyph(unicode)
class Shaper:
"""Class for turning text into boxes and glue."""
def __init__(self, doc):
self.cache = {"hb": {}, "ft": {}}
self._font_funcs = hb.FontFuncs.create(True)
self._font_funcs.set_nominal_glyph_func(_get_glyph, None, None)
self.doc = doc
blob = hb.Blob.create_from_file(doc.body_font)
self.face = hb.Face.create(blob, 0, True)
self.font = self.make_font()
self.buffer = hb.Buffer.create()
self.minfont, self.minaxis = self.make_var_font("ASHR")
self.maxfont, self.maxaxis = self.make_var_font("ASTR")
def make_font(self, variations=None, funcs=None):
cache = self.cache["hb"]
key = f"{variations}:{funcs}"
if key not in cache:
font = hb.Font.create(self.face)
font.scale = (self.doc.body_font_size, self.doc.body_font_size)
if variations:
font.set_variations([hb.Variation.from_string(variations)])
if funcs:
font = font.create_sub_font()
font.set_funcs(funcs, None, None)
cache[key] = font
return cache[key]
def make_var_font(self, tag):
axis = self.face.ot_var_find_axis_info(hb.HARFBUZZ.TAG(tag))
axis.tag = tag
font = self.make_font(f"{tag}={axis.max_value}")
return font, axis
def make_qahira_face(self, variations=None):
cache = self.cache["ft"]
if variations not in cache:
ft_face = ft.new_face(self.doc.body_font)
if variations:
variation = hb.Variation.from_string(variations)
axes = ft_face.mm_var["axis"]
coords = []
for axis in axes:
tag = axis["tag"]
if tag == variation.tag.decode("ascii"):
coords.append(variation.value)
else:
coords.append(axis["default"])
ft_face.set_var_design_coordinates(coords)
cache[variations] = qh.FontFace.create_for_ft_face(ft_face)
return cache[variations]
def clear_buffer(self, direction=hb.HARFBUZZ.DIRECTION_RTL):
buf = self.buffer
buf.clear_contents()
buf.direction = direction
buf.script = hb.HARFBUZZ.SCRIPT_ARABIC
buf.language = hb.Language.from_string("ar")
return buf
def shape(self, text, direction):
buf = self.clear_buffer(direction)
buf.add_str(text)
hb.shape(self.font, buf)
return buf
def reshape(self, glyphs, variations):
font = self.make_font(variations, self._font_funcs)
buf = self.clear_buffer()
codepoints = [g.index + GID_OFFSET for g in reversed(glyphs)]
buf.add_codepoints(codepoints, len(codepoints), 0, len(codepoints))
hb.shape(font, buf)
return buf.get_glyphs()[0]
@staticmethod
def next_is_nonjoining(text, infos, index):
if index < len(infos):
cluster = infos[index].cluster
category = unicodedata.category(text[cluster])
return category[0] != "L"
return True
def shape_verse(self, verse, mark=None):
"""
Shapes a single verse and returns the corresponding nodes.
"""
buf = self.shape(verse, hb.HARFBUZZ.DIRECTION_RTL)
nodes = []
infos = buf.glyph_infos
positions = buf.glyph_positions
flip = qh.Vector(1, -1)
i = len(infos) - 1
while i >= 0:
# Find all indices with same cluster
j = i
while j >= 0 and infos[i].cluster == infos[j].cluster:
j -= 1
# Collect all glyphs in this cluster, iterating backwards to get
# glyphs in the visual order.
pos = qh.Vector(0, 0)
glyphs = []
for k in reversed(range(i, j, -1)):
glyphs.append(
qh.Glyph(infos[k].codepoint, pos + flip * positions[k].offset)
)
pos += flip * positions[k].advance
# The chars in this cluster
chars = verse[infos[i].cluster : infos[j].cluster]
# We skip space since the font kerns with it and we will turn these
# kerns into glue below.
if chars != " ":
# Find the last non-combining mark char in the string, to check
# for joining behaviour.
for ch in chars:
if not unicodedata.combining(ch):
base = ch
adv = self.font.get_glyph_h_advance(glyphs[-1].index)
minadv = self.minfont.get_glyph_h_advance(glyphs[-1].index)
maxadv = self.maxfont.get_glyph_h_advance(glyphs[-1].index)
shrink = adv - minadv
stretch = maxadv - adv
if base in RIGH_JOINING or self.next_is_nonjoining(verse, infos, j):
# Get the difference between the original advance width and
# the advance width after OTL.
kern = positions[k].advance - qh.Vector(adv, 0)
# Re-adjust glyph positions.
glyphs = [qh.Glyph(g.index, g.pos - kern) for g in glyphs]
nodes.append(Box(self.doc, chars, glyphs, adv, stretch, shrink))
# Add glue with the kerning amount with minimal stretch and shrink.
nodes.append(Glue(self.doc, kern.x, kern.x / 8.5, kern.x / 8.5))
else:
nodes.append(Box(self.doc, chars, glyphs, pos.x, stretch, shrink))
elif pos.x != 0:
# If space is not zero-width, add glue for it.
nodes.append(Glue(self.doc, pos.x, pos.x / 8.5, pos.x / 8.5))
i = j
if mark:
buf = self.shape(mark, hb.HARFBUZZ.DIRECTION_LTR)
glyphs, pos = buf.get_glyphs()
nodes.append(Box(self.doc, mark, glyphs, pos.x))
return nodes
def shape_paragraph(self, text):
"""
Converts the text to a list of boxes and glues that the line breaker
will work on.
"""
nodes = linebreak.NodeList()
# Split the text into verses, using aya mark as seperator.
verse = ""
text = text.strip()
textlen = len(text)
i = 0
while i < textlen:
ch = text[i]
if ch == "\u06DD":
mark = ch
i += 1
while i < textlen and text[i] in DIGITS:
mark += text[i]
i += 1
nodes.extend(self.shape_verse(verse, mark))
verse = ""
else:
verse += ch
i += 1
nodes.extend(self.shape_verse(verse))
nodes.add_closing_penalty()
return nodes
class Page:
"""Class representing a page of text."""
def __init__(self, doc, lines, number):
self.doc = doc
self.lines = lines
self.number = number
def draw(self, cr):
logger.info("Page %d…", self.number)
shaper = self.doc.shaper
self.cr = cr
if not self.lines:
logger.debug("Leaving empty page blank")
cr.show_page()
return
lines = self.lines
pos = qh.Vector(self.doc.text_start_pos, self.doc.top_margin)
for i, line in enumerate(lines):
line.draw(cr, pos)
pos.y += line.height
cr.show_page()
class Glue(linebreak.Glue):
def __init__(self, doc, width, stretch, shrink):
super().__init__(width=width, stretch=stretch, shrink=shrink)
self.doc = doc
def draw(self, cr, pos, drawColorLayers):
width = self.compute_width()
x, y = pos.x - width, pos.y
if self.doc.debug and width != self.width:
cr.save()
if self.ratio > 0:
cr.set_source_colour((0, 1, 0, 0.2))
else:
cr.set_source_colour((0, 0, 1, 0.2))
cr.rectangle(qh.Rect(x, y, width, -5))
cr.fill()
cr.restore()
return x
class Box(linebreak.Box):
def __init__(self, doc, text, glyphs, width, stretch=0, shrink=0):
super().__init__(width=width, stretch=stretch, shrink=shrink)
self.doc = doc
self.text = text
self.glyphs = glyphs
def draw(self, cr, pos, drawColorLayers):
cr.save()
glyphs = self.glyphs
shaper = self.doc.shaper
face = shaper.font.face
width = self.compute_width()
x, y = pos.x - width, pos.y
cr.translate((x, y))
if width != self.width:
axis = shaper.maxaxis if self.ratio > 0 else shaper.minaxis
value = abs(self.ratio) * (axis.max_value - axis.default_value)
variations = f"{axis.tag}={value}"
glyphs = shaper.reshape(glyphs, variations)
cr.set_font_face(shaper.make_qahira_face(variations))
colors = face.ot_colour_palette_get_colours(0)
for glyph in glyphs:
layers = face.ot_colour_glyph_get_layers(glyph.index)
if layers and drawColorLayers:
for layer in layers:
color = colors[layer.colour_index]
color = [
hb.HARFBUZZ.colour_get_red(color),
hb.HARFBUZZ.colour_get_green(color),
hb.HARFBUZZ.colour_get_blue(color),
hb.HARFBUZZ.colour_get_alpha(color),
]
color = [c / 255 for c in color]
lglyph = qh.Glyph(layer.glyph, glyph.pos)
cr.save()
cr.set_source_colour(color)
cr.show_glyphs([lglyph])
cr.restore()
elif not layers and not drawColorLayers:
cr.show_glyphs([glyph])
cr.restore()
if self.doc.debug and width != self.width:
cr.save()
if self.ratio > 0:
cr.set_source_colour((0, 1, 0, 0.2))
else:
cr.set_source_colour((0, 0, 1, 0.2))
cr.rectangle(qh.Rect(x, y - self.doc.leading + 30, width, 5))
cr.fill()
cr.restore()
return x
class Line:
"""Class representing a line of text."""
def __init__(self, doc, boxes):
self.doc = doc
self.height = doc.leading
self.boxes = boxes
def draw(self, cr, pos):
self.strip()
for drawColorLayers in (False, True):
p = qh.Vector(pos.x, pos.y)
for box in self.boxes:
p.x = box.draw(cr, p, drawColorLayers)
def strip(self):
while self.boxes and not self.boxes[-1].is_box:
self.boxes.pop()
class Heading(Line):
"""Class representing a chapter heading."""
def __init__(self, doc, boxes):
super().__init__(doc, boxes)
def draw(self, cr, pos):
cr.save()
cr.set_source_colour((0.83, 0.68, 0.21)) # XXX
super().draw(cr, pos)
cr.restore()
def read_data(datadir):
path = os.path.join(datadir, "meta.txt")
if os.path.isfile(path):
with open(path, "r", encoding="utf-8") as textfile:
metadata = {}
lines = [l.strip().split("\t") for l in textfile.readlines()]
for num, line in enumerate(lines):
num += 1
metadata[num] = [line[0], line[1], True]
if len(line) >= 3:
metadata[num][2] = int(line[2])
else:
logger.error("File not found: %s", path)
return
chapters = {}
for i in range(1, 115):
path = os.path.join(datadir, "%03d.txt" % i)
if os.path.isfile(path):
with open(path, "r", encoding="utf-8") as textfile:
lines = [l.strip("\n") for l in textfile.readlines()]
chapter = Chapter(" ".join(lines), i, *metadata[i], len(lines))
chapters[i] = chapter
else:
pass
return chapters
def main(chapters, filename, debug):
document = Document(chapters, filename, debug)
document.save()
if __name__ == "__main__":
import argparse
import os
import sys
parser = argparse.ArgumentParser(description="Quran Typesetter.")
parser.add_argument(
"datadir", metavar="DATADIR", help="Directory containing input files to process"
)
parser.add_argument("outfile", metavar="OUTFILE", help="Output file")
parser.add_argument(
"--chapters",
"-c",
metavar="N",
nargs="*",
type=int,
choices=range(1, 115),
default=range(1, 115),
help="Which chapters to process (Default: all)",
)
parser.add_argument(
"--debug", "-d", action="store_true", help="Draw some debugging aids"
)
parser.add_argument(
"--quite", "-q", action="store_true", help="Don’t print normal messages"
)
parser.add_argument(
"--verbose", "-v", action="store_true", help="Print verbose messages"
)
args = parser.parse_args()
if args.verbose:
logger.setLevel(logging.DEBUG)
if args.quite:
logger.setLevel(logging.ERROR)
all_chapters = read_data(args.datadir)
if all_chapters is None:
sys.exit(1)
chapters = []
for i in args.chapters:
chapters.append(all_chapters[i])
main(chapters, args.outfile, args.debug)