Skip to content

Commit

Permalink
fix #279770: fix aligning of lyrics with leading/trailing numbers
Browse files Browse the repository at this point in the history
The previously used method did not take into account the fact that
width of some characters (e.g. whitespaces) may vary depending on
the surrounding context. This commit corrects leading/trailing parts
widths calculation by making use of TextBlock::xpos() function which
is used also for text cursor placement in text editing mode.
  • Loading branch information
dmitrio95 committed Jun 15, 2019
1 parent f49e61a commit e129be0
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions libmscore/lyrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,38 +234,27 @@ void Lyrics::layout()
// parse leading verse number and/or punctuation, so we can factor it into layout separately
//
bool hasNumber = false; // _verseNumber;
qreal centerAdjust = 0.0;
qreal leftAdjust = 0.0;

// find:
// 1) string of numbers and non-word characters at start of syllable
// 2) at least one other character (indicating start of actual lyric)
// 3) string of non-word characters at end of syllable
//QRegularExpression leadingPattern("(^[\\d\\W]+)([^\\d\\W]+)");

const QString text = plainText();
QString leading;
QString trailing;

if (score()->styleB(Sid::lyricsAlignVerseNumber)) {
QString s = plainText();
QRegularExpression punctuationPattern("(^[\\d\\W]*)([^\\d\\W].*?)([\\d\\W]*$)", QRegularExpression::UseUnicodePropertiesOption);
QRegularExpressionMatch punctuationMatch = punctuationPattern.match(s);
QRegularExpressionMatch punctuationMatch = punctuationPattern.match(text);
if (punctuationMatch.hasMatch()) {
// leading and trailing punctuation
QString lp = punctuationMatch.captured(1);
QString tp = punctuationMatch.captured(3);
// actual lyric
leading = punctuationMatch.captured(1);
trailing = punctuationMatch.captured(3);
//QString actualLyric = punctuationMatch.captured(2);
if (!lp.isEmpty() || !tp.isEmpty()) {
// qDebug("create leading, trailing <%s> -- <%s><%s>", qPrintable(s), qPrintable(lp), qPrintable(tp));
Lyrics leading(*this);
leading.setPlainText(lp);
leading.layout1();
Lyrics trailing(*this);
trailing.setPlainText(tp);
trailing.layout1();
leftAdjust = leading.width();
centerAdjust = leading.width() - trailing.width();
if (!lp.isEmpty() && lp[0].isDigit())
hasNumber = true;
}
if (!leading.isEmpty() && leading[0].isDigit())
hasNumber = true;
}
}

Expand Down Expand Up @@ -293,6 +282,26 @@ void Lyrics::layout()
qreal x = pos().x();
TextBase::layout1();

qreal centerAdjust = 0.0;
qreal leftAdjust = 0.0;

if (score()->styleB(Sid::lyricsAlignVerseNumber)) {
// Calculate leading and trailing parts widths. Lyrics
// should have text layout to be able to do it correctly.
Q_ASSERT(rows() != 0);
if (!leading.isEmpty() || !trailing.isEmpty()) {
// qDebug("create leading, trailing <%s> -- <%s><%s>", qPrintable(text), qPrintable(leading), qPrintable(trailing));
const TextBlock& tb = textBlock(0);

const qreal leadingWidth = tb.xpos(leading.length(), this) - tb.boundingRect().x();
const int trailingPos = text.length() - trailing.length();
const qreal trailingWidth = tb.boundingRect().right() - tb.xpos(trailingPos, this);

leftAdjust = leadingWidth;
centerAdjust = leadingWidth - trailingWidth;
}
}

ChordRest* cr = chordRest();

if (align() & Align::HCENTER) {
Expand Down

0 comments on commit e129be0

Please sign in to comment.