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

fix #307721: blank lines ignored at top of text elements #6328

Merged
merged 1 commit into from
Jul 14, 2020

Conversation

SKefalidis
Copy link
Contributor

@SKefalidis SKefalidis commented Jul 10, 2020

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.

  • I signed CLA
  • I made sure the code in the PR follows the coding rules
  • I made sure the code compiles on my machine
  • I made sure there are no unnecessary changes in the code
  • I made sure the title of the PR reflects the core meaning of the issue you are solving
  • I made sure the commit message(s) contain a description and answer the question "Why do those changes fix that particular issue?" or "Why are those changes really necessary as improvements?"
  • I made sure the commit message title starts with "fix #424242:" if there is a related issue
  • [n\a] I created the test (mtest, vtest, script test) to verify the changes I made

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 == "") {
Copy link
Contributor

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?

Copy link
Contributor Author

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 -> 🤷‍♂️

Copy link
Contributor Author

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).

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 == "") {
Copy link
Contributor

@Spire42 Spire42 Jul 11, 2020

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.)

@Jojo-Schmitz
Copy link
Contributor

Jojo-Schmitz commented Jul 11, 2020

The code does work, but places the text much lower than before:
image
That text "Chor" looks centered to the bracket (which is 2 'instruments, not a grand staff) in 3.4.2. It uses 13 empty lines to get the position right in 3.4.2, now it needs just 4. Not necessarily a bad thing, just a lame workaround needed earlier now biting back I guess ;-)

@Jojo-Schmitz
Copy link
Contributor

commit title should start with "fix #307721:"

@MarcSabatella
Copy link
Contributor

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.

@MarcSabatella
Copy link
Contributor

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.

@Jojo-Schmitz
Copy link
Contributor

Jojo-Schmitz commented Jul 12, 2020

Is that that 'magic factor' 0.27? Maybe not playing along with the screens's DPI setting or the score's spatium?

@SKefalidis SKefalidis changed the title fix blank lines (empty text fragments) incorrect bounding box fix #307721: blank lines ignored at top of text elements Jul 13, 2020
@SKefalidis
Copy link
Contributor Author

It should be good now. I was missing a pretty important part, every time createLayout is called all the text fragments are destroyed so all information was lost and it was not saved by genText because it explicitly didn't bother with empty fragments.

_fragments.insert(0, TextFragment(cursor, ""));
}
Copy link
Contributor

@Jojo-Schmitz Jojo-Schmitz Jul 14, 2020

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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...but not here?

Copy link
Contributor

@Jojo-Schmitz Jojo-Schmitz left a 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) {
Copy link
Contributor

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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these braces seem redundant

Copy link
Contributor

@Jojo-Schmitz Jojo-Schmitz Jul 14, 2020

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)

@vpereverzev vpereverzev merged commit 25f92af into musescore:3.x Jul 14, 2020
anatoly-os added a commit that referenced this pull request Aug 3, 2020
igorkorsukov added a commit to igorkorsukov/MuseScore that referenced this pull request Feb 12, 2021
vpereverzev pushed a commit that referenced this pull request Feb 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants