@@ -1,13 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fractions import Fraction
|
||||
import fractions
|
||||
|
||||
from generator.model import LETTERS, TYPE_LENGTHS, alteration, fraction, key_from_fifths
|
||||
from generator.engraving.fonts import Fonts
|
||||
from generator.engraving.notation import ScoreIndex, accidental
|
||||
from generator.engraving.types import Primitive, bounds, line
|
||||
import generator.engraving.fonts
|
||||
import generator.engraving.notation
|
||||
import generator.engraving.types
|
||||
import generator.model
|
||||
|
||||
def staff_attributes(index: ScoreIndex, state: dict, time: Fraction, fonts: Fonts, space: float, source: str, fields: tuple[str, ...]) -> list[Primitive]:
|
||||
def staff_attributes(index: generator.engraving.notation.ScoreIndex, state: dict, time: fractions.Fraction, fonts: generator.engraving.fonts.Fonts, space: float, source: str, fields: tuple[str, ...]) -> list[generator.engraving.types.Primitive]:
|
||||
result = []
|
||||
x = 0
|
||||
references = (source,)
|
||||
@@ -22,18 +22,18 @@ def staff_attributes(index: ScoreIndex, state: dict, time: Fraction, fonts: Font
|
||||
if octave := clef.get('octave_change', 0):
|
||||
label = str(abs(octave) * 7 + 1)
|
||||
result.append(fonts.text(label, (glyph.box.left + glyph.box.right) / 2, glyph.box.top - 2 if octave > 0 else glyph.box.bottom + 14, 14, 'clef-octave', references, True))
|
||||
x = bounds(result).right + space
|
||||
x = generator.engraving.types.bounds(result).right + space
|
||||
if 'key' in fields and clef['sign'] != 'percussion':
|
||||
key = state['key']
|
||||
steps = key.get('steps')
|
||||
if steps is None:
|
||||
order = 'fcgdaeb' if key.get('fifths', 0) >= 0 else 'beadgcf'
|
||||
steps = [{'letter': c, 'alter': key['alterations'][LETTERS.index(c)]} for c in order if alteration(key['alterations'][LETTERS.index(c)])]
|
||||
steps = [{'letter': c, 'alter': key['alterations'][generator.model.LETTERS.index(c)]} for c in order if generator.model.alteration(key['alterations'][generator.model.LETTERS.index(c)])]
|
||||
cancellation = key.get('cancel', 0)
|
||||
if cancellation:
|
||||
order = 'fcgdaeb' if cancellation > 0 else 'beadgcf'
|
||||
old = key_from_fifths(cancellation)['alterations']
|
||||
steps = [{'letter': c, 'alter': 0} for c in order if alteration(old[LETTERS.index(c)]) and alteration(old[LETTERS.index(c)]) != alteration(key['alterations'][LETTERS.index(c)])] + steps
|
||||
old = generator.model.key_from_fifths(cancellation)['alterations']
|
||||
steps = [{'letter': c, 'alter': 0} for c in order if generator.model.alteration(old[generator.model.LETTERS.index(c)]) and generator.model.alteration(old[generator.model.LETTERS.index(c)]) != generator.model.alteration(key['alterations'][generator.model.LETTERS.index(c)])] + steps
|
||||
for step in steps:
|
||||
pitch = {'letter': step['letter'], 'octave': step.get('octave', 5), 'id': source}
|
||||
position = index.pitch_position(pitch, time)
|
||||
@@ -42,9 +42,9 @@ def staff_attributes(index: ScoreIndex, state: dict, time: Fraction, fonts: Font
|
||||
position -= 7
|
||||
while position < 2:
|
||||
position += 7
|
||||
items = accidental(fonts, alteration(step['alter']), {'name': step.get('accidental', '')}, space, references)
|
||||
items = generator.engraving.notation.accidental(fonts, generator.model.alteration(step['alter']), {'name': step.get('accidental', '')}, space, references)
|
||||
result.extend(p.moved(x, -position * space / 2) for p in items)
|
||||
x += bounds(items).right - bounds(items).left + 4
|
||||
x += generator.engraving.types.bounds(items).right - generator.engraving.types.bounds(items).left + 4
|
||||
if steps:
|
||||
x += space / 2
|
||||
if 'meter' in fields:
|
||||
@@ -67,8 +67,8 @@ def staff_attributes(index: ScoreIndex, state: dict, time: Fraction, fonts: Font
|
||||
items = []
|
||||
cursor = 0
|
||||
if y == -space and meter.get('symbol') in ('note', 'dotted-note'):
|
||||
unit = Fraction(4, group['unit'])
|
||||
kind = next((name for name, length in TYPE_LENGTHS.items() if length == unit), None)
|
||||
unit = fractions.Fraction(4, group['unit'])
|
||||
kind = next((name for name, length in generator.model.TYPE_LENGTHS.items() if length == unit), None)
|
||||
glyph_name = {'whole': 'noteWhole', 'half': 'noteHalfUp', 'quarter': 'noteQuarterUp', 'eighth': 'note8thUp'}.get(kind, 'note' + str(kind) + 'Up')
|
||||
if kind and glyph_name in fonts.names:
|
||||
glyph = fonts.glyph(glyph_name, 0, y + space, space * 0.55, 'meter', references)
|
||||
@@ -82,36 +82,35 @@ def staff_attributes(index: ScoreIndex, state: dict, time: Fraction, fonts: Font
|
||||
items.append(glyph)
|
||||
cursor = glyph.box.right + 1
|
||||
groups.append(items)
|
||||
width = max(bounds(items).right for items in groups)
|
||||
width = max(generator.engraving.types.bounds(items).right for items in groups)
|
||||
for items in groups:
|
||||
result.extend(p.moved(x + (width - bounds(items).right) / 2, 0) for p in items)
|
||||
result.extend(p.moved(x + (width - generator.engraving.types.bounds(items).right) / 2, 0) for p in items)
|
||||
x += width + 6
|
||||
if 'staff_details' in fields and state['staff_details'].get('capo'):
|
||||
result.append(fonts.text(f'Capo {state["staff_details"]["capo"]}', x, -space * (lines + 1), 16, 'staff-details', references))
|
||||
return result
|
||||
|
||||
def barline(style: str, x: float, top: float, source: str) -> list[Primitive]:
|
||||
def barline(style: str, x: float, top: float, source: str) -> list[generator.engraving.types.Primitive]:
|
||||
if style == 'none':
|
||||
return []
|
||||
references = (source,)
|
||||
if style in ('dashed', 'dotted'):
|
||||
dash, gap = (5, 4) if style == 'dashed' else (1, 4)
|
||||
return [line('barline', references, ((x, y), (x, min(y + dash, 0)))) for y in range(round(top), 0, dash + gap)]
|
||||
return [generator.engraving.types.line('barline', references, ((x, y), (x, min(y + dash, 0)))) for y in range(round(top), 0, dash + gap)]
|
||||
if style in ('tick', 'short'):
|
||||
return [line('barline', references, ((x, top - 4 if style == 'tick' else top * 0.75), (x, top + 5 if style == 'tick' else top * 0.25)))]
|
||||
return [generator.engraving.types.line('barline', references, ((x, top - 4 if style == 'tick' else top * 0.75), (x, top + 5 if style == 'tick' else top * 0.25)))]
|
||||
weights = {'regular': [1.4], 'heavy': [4], 'light-light': [1.4, 1.4], 'light-heavy': [1.4, 4], 'heavy-light': [4, 1.4], 'heavy-heavy': [4, 4]}[style]
|
||||
return [line('barline', references, ((x - (len(weights) - 1 - i) * 5, top), (x - (len(weights) - 1 - i) * 5, 0)), width) for i, width in enumerate(weights)]
|
||||
return [generator.engraving.types.line('barline', references, ((x - (len(weights) - 1 - i) * 5, top), (x - (len(weights) - 1 - i) * 5, 0)), width) for i, width in enumerate(weights)]
|
||||
|
||||
def tempo(value: dict, fonts: Fonts, x: float, y: float, space: float, source: str) -> list[Primitive]:
|
||||
from generator.engraving.notation import flags, head_glyph
|
||||
def tempo(value: dict, fonts: generator.engraving.fonts.Fonts, x: float, y: float, space: float, source: str) -> list[generator.engraving.types.Primitive]:
|
||||
written = value['written']
|
||||
references = (source,)
|
||||
glyph = fonts.glyph(head_glyph(written, None), x, y, space * 0.7, 'tempo', references)
|
||||
glyph = fonts.glyph(generator.engraving.notation.head_glyph(written, None), x, y, space * 0.7, 'tempo', references)
|
||||
items = [glyph]
|
||||
stem_x = glyph.box.right - 0.5
|
||||
if written['type'] not in ('whole', 'breve', 'long', 'maxima'):
|
||||
items.append(line('tempo', references, ((stem_x, y), (stem_x, y - 23)), 1))
|
||||
if flags(written):
|
||||
items.append(generator.engraving.types.line('tempo', references, ((stem_x, y), (stem_x, y - 23)), 1))
|
||||
if generator.engraving.notation.flags(written):
|
||||
name = 'flag' + ('8th' if written['type'] == 'eighth' else written['type']) + 'Up'
|
||||
items.append(fonts.glyph(name, stem_x, y - 23, space * 0.7, 'tempo', references))
|
||||
cursor = max(p.box.right for p in items) + 3
|
||||
@@ -119,5 +118,5 @@ def tempo(value: dict, fonts: Fonts, x: float, y: float, space: float, source: s
|
||||
dot = fonts.glyph('augmentationDot', cursor, y - 3, space * 0.7, 'tempo', references)
|
||||
items.append(dot)
|
||||
cursor = dot.box.right + 3
|
||||
items.append(fonts.text(' = ' + str(fraction(value['bpm'])), cursor, y + 3, 16, 'tempo', references))
|
||||
items.append(fonts.text(' = ' + str(generator.model.fraction(value['bpm'])), cursor, y + 3, 16, 'tempo', references))
|
||||
return items
|
||||
|
||||
Reference in New Issue
Block a user