Skip to content

Commit

Permalink
Merge pull request #1331 from jacobtylerwalls/credit-words-fix
Browse files Browse the repository at this point in the history
[Musicxml export] Preserve whitespace in TextBox
  • Loading branch information
mscuthbert authored Jul 4, 2022
2 parents dfb7300 + 8b29eee commit 2dccb5b
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions music21/musicxml/m21ToXml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ def setScoreHeader(self):

def textBoxToXmlCredit(self, textBox):
# noinspection PyShadowingNames
'''
r'''
Convert a music21 TextBox to a MusicXML Credit.
>>> tb = text.TextBox('testing')
Expand All @@ -1800,8 +1800,19 @@ def textBoxToXmlCredit(self, textBox):
>>> mxCredit = SX.textBoxToXmlCredit(tb)
>>> SX.dump(mxCredit)
<credit page="1">...</credit>
Changed in v.8 -- Multi-line text now exports as one `<credit-words>`
element (preserving newlines).
>>> tb = text.TextBox('Snare\nCymbals')
>>> mxCredit = SX.textBoxToXmlCredit(tb)
>>> SX.dump(mxCredit)
<credit page="1">
<credit-words default-x="500" default-y="500" halign="center" valign="top"
xml:space="preserve">Snare
Cymbals</credit-words>
</credit>
'''
# use line carriages to separate messages
mxCredit = Element('credit')
# TODO: credit-type
# TODO: link
Expand All @@ -1813,19 +1824,15 @@ def textBoxToXmlCredit(self, textBox):
else:
mxCredit.set('page', '1')

# add all credit words to components
count = 0

for line in textBox.content.split('\n'):
mxCreditWords = Element('credit-words')
mxCreditWords.text = line
# TODO: link/bookmark in credit-words
if count == 0: # on first, configure properties
self.setPrintStyleAlign(mxCreditWords, textBox)
if textBox.hasStyleInformation and textBox.style.justify is not None:
mxCreditWords.set('justify', textBox.style.justify)
mxCredit.append(mxCreditWords)
count += 1
mxCreditWords = Element('credit-words')
if '\n' in textBox.content:
mxCreditWords.set('xml:space', 'preserve')
mxCreditWords.text = textBox.content
# TODO: link/bookmark in credit-words
self.setPrintStyleAlign(mxCreditWords, textBox)
if textBox.hasStyleInformation and textBox.style.justify is not None:
mxCreditWords.set('justify', textBox.style.justify)
mxCredit.append(mxCreditWords)
return mxCredit

def setDefaults(self):
Expand Down

0 comments on commit 2dccb5b

Please sign in to comment.