-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
fix #307721: blank lines ignored at top of text elements #6328
Conversation
libmscore/textbase.cpp
Outdated
if (_fragments.empty()) { | ||
QFontMetricsF fm = t->fontMetrics(); | ||
_bbox.setRect(0.0, -fm.ascent(), 1.0, fm.descent()); | ||
_lineSpacing = fm.lineSpacing(); | ||
} | ||
else if (_fragments.size() == 1 && _fragments.at(0).text == "") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I trust you on this, but, why does this turn out to only cause probems for the leading blank lines, not trailing ones?
Also, is comparing against "" really OK here, what if that initial line actually has spaces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's comparing against both size of 1 and ""
. If there is only one fragment and it is empty then we have an empty line that has a fragment to save formatting information.
If you have a line of pure spaces it enters the 'else' block which calculates the bounding box based on the text. But since spaces have no bounding box the line "does not exist".
As far as why it does not cause problems for trailing lines -> 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow up for completeness: genText
ignored formatting information for empty lines. That meant that when createLayout
deleted all fragments and reinserted them it did not have the information provided by empty text fragments. That's why leading fragments did not have the desired effect but trailing ones did (since the information was provided by the line of text whose formatting information was saved by genText
).
a996de9
to
44ac994
Compare
libmscore/textbase.cpp
Outdated
if (_fragments.empty()) { | ||
QFontMetricsF fm = t->fontMetrics(); | ||
_bbox.setRect(0.0, -fm.ascent(), 1.0, fm.descent()); | ||
_lineSpacing = fm.lineSpacing(); | ||
} | ||
else if (_fragments.size() == 1 && _fragments.at(0).text == "") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to compare against a literal C-style string. Just call isEmpty()
:
else if ((_fragments.size() == 1) && _fragments.at(0).text.isEmpty()) {
(I also added parentheses to the first expression to conform to the coding guidelines.)
commit title should start with "fix #307721:" |
Yes, the fact that 13 lines were required before was the bug that was fixed originally, and it is good that is fixed. It was clear with elements other than instrument names that the blank lines were way too small, because you could see the size while editing then the layout would change when you left edit mode. With instrument names you didn’t have that same feedback. But it’s pretty clear just by eyeballing it that by all rights it should not have taken 13 blank lines. Should take the same number of blank ones as it would lines containing actual text. |
Something is still wrong, though, the blank lines change size when I leave edit mode. The effect is small if I add only one blank line, but try adding six (say, to the front of the "Title" on the default empty score), then hit Esc, and you see a very big jump. I think the blank lines being added are too small at first, the size on hitting Esc is probably more correct. So, basically, the exact opposite of the original problem. Again, trailing blank lines seem to work perfectly. I think understanding why that is and hopefully leveraging the same would be better. |
Is that that 'magic factor' |
44ac994
to
e1a0235
Compare
e1a0235
to
80ac680
Compare
80ac680
to
f80a331
Compare
It should be good now. I was missing a pretty important part, every time |
_fragments.insert(0, TextFragment(cursor, "")); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why braces here... ((I'm not a friend of excessive use of redundant braces)
@@ -1148,7 +1190,7 @@ void TextBlock::insertEmptyFragmentIfNeeded(TextCursor* cursor) | |||
|
|||
void TextBlock::removeEmptyFragment() | |||
{ | |||
if (_fragments.size() > 0 && _fragments.at(0).text == "") | |||
if (_fragments.size() > 0 && _fragments.at(0).text.isEmpty()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...but not here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me (except for a couple stylistic remarks), I've build and tests and it does work for the cases I tested it with (title text and instrument names with leading blank lines)
QList<TextFragment>* TextBlock::fragmentsWithoutEmpty() | ||
{ | ||
QList<TextFragment>* list = new QList<TextFragment>(); | ||
for (auto x :_fragments) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space between :
and _fragments
for (auto x :_fragments) { | ||
if (x.text.isEmpty()) { | ||
continue; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these braces seem redundant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also a simple
for (auto x : _fragments)
if (!x.text.isEmpty())
list->append(x);
Should be sufficient? No else
, no continue
needed (braces for the for loop optional)
… top of text elements
Resolves: Blank lines added before the first line of text would disappear when not editing.
Edit: The source of the problem is that blank lines have no bounding boxes, since they are empty. Essentially they are ignored during layout. I added handling for that. I also improved the handling of empty text fragments and modified genText (which saves all information about the text as text) to include them.