forked from t-bullock/kassia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neume_chunk.py
72 lines (54 loc) · 2.25 KB
/
neume_chunk.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
import collections
from reportlab.pdfbase import pdfmetrics
import neume_dict
class NeumeChunk(collections.MutableSequence):
"""A collection of Neumes, but with a calculated width and height.
"""
def __init__(self, *args):
self.width = 0
self.height = 0
self.list = list()
self.extend(list(args))
def __len__(self):
return len(self.list)
def __getitem__(self, i):
return self.list[i]
def __delitem__(self, i):
del self.list[i]
self.set_width()
def __setitem__(self, i, v):
self.set_width()
self.list[i] = v
def insert(self, i, v):
self.add_width(v)
if self.height == 0:
self.set_height(v)
self.list.insert(i, v)
def __str__(self):
return str(self.list)
def set_width(self):
max_neume_width = 0
for i, neume in enumerate(self.list):
neume_width = pdfmetrics.stringWidth(neume.char, neume.font_family, neume.font_size)
if neume_width == 0:
continue
# If kentima (or similar), add width of oligon that came before it.
# Kentima may come at the end of the chunk, after a psefeston, etc.,
# so we can't just check i-1.
# Just add oligon and assume it is part of the chunk.
# TODO: Find a better way to do this
if neume.char in neume_dict.nonPostBreakingNeumes:
neume_width += pdfmetrics.stringWidth('1', neume.font_family, neume.font_size)
# If vareia (or similar), add width of next neume.
# This prevents vareia from being at the end of a line.
if neume.char in neume_dict.nonPreBreakingNeumes and (i + 1) < len(self.list):
next_neume = self.list[i + 1]
neume_width += pdfmetrics.stringWidth(next_neume.char, next_neume.font_family, next_neume.font_size)
if max_neume_width < neume_width:
max_neume_width = neume_width
self.width = max_neume_width
def set_height(self, neume):
ascent, descent = pdfmetrics.getAscentDescent(neume.font_family, neume.font_size)
self.height = ascent - descent
def add_width(self, neume):
self.width += neume.width