Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Musicxml export] Preserve whitespace in TextBox #1331

Merged
merged 3 commits into from
Jul 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions music21/musicxml/m21ToXml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,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 @@ -1758,8 +1758,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 @@ -1771,19 +1782,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