Skip to content

Commit

Permalink
Properly deal with </font>
Browse files Browse the repository at this point in the history
instead of ignoring it altogether, reset font face and -size back to what it was before `<font ...>`.
While at it add the tags for strikeout, as possible in Mu4 already.

Also fix some code warnings shown by QtCreator.
  • Loading branch information
Jojo-Schmitz committed Apr 7, 2024
1 parent 77914d6 commit 0725eaa
Showing 1 changed file with 65 additions and 28 deletions.
93 changes: 65 additions & 28 deletions libmscore/textbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
// the file LICENCE.GPL
//=============================================================================

#include "text.h"
#include "textedit.h"
#include "jump.h"
#include "marker.h"
#include "score.h"
#include "segment.h"
#include "measure.h"
Expand Down Expand Up @@ -1264,7 +1261,6 @@ QString TextBlock::remove(int column, TextCursor* cursor)
QString s;
for (auto i = _fragments.begin(); i != _fragments.end(); ++i) {
int idx = 0;
int rcol = 0;
for (const QChar& c : qAsConst(i->text)) {
if (col == column) {
if (c.isSurrogate()) {
Expand All @@ -1285,7 +1281,6 @@ QString TextBlock::remove(int column, TextCursor* cursor)
if (c.isHighSurrogate())
continue;
++col;
++rcol;
}
}
insertEmptyFragmentIfNeeded(cursor); // without this, cursorRect can't calculate the y position of the cursor correctly
Expand Down Expand Up @@ -1326,7 +1321,6 @@ QString TextBlock::remove(int start, int n, TextCursor* cursor)
int col = 0;
QString s;
for (auto i = _fragments.begin(); i != _fragments.end();) {
int rcol = 0;
bool inc = true;
for( int idx = 0; idx < i->text.length(); ) {
QChar c = i->text[idx];
Expand All @@ -1353,7 +1347,6 @@ QString TextBlock::remove(int start, int n, TextCursor* cursor)
if (c.isHighSurrogate())
continue;
++col;
++rcol;
}
if (inc)
++i;
Expand Down Expand Up @@ -1697,6 +1690,8 @@ void TextBase::createLayout()
}
else if (state == 1) {
if (c == '>') {
static QString prevFontFace;
static qreal prevFontSize = 0;
bool unstyleFontStyle = false;
state = 0;
if (token == "b") {
Expand All @@ -1717,12 +1712,22 @@ void TextBase::createLayout()
}
else if (token == "/u")
cursor.format()->setUnderline(false);
else if (token == "sub")
else if (token == "s") {
cursor.format()->setStrike(true);
unstyleFontStyle = true;
}
else if (token == "/s")
cursor.format()->setStrike(false);
else if (token == "sub") {
cursor.format()->setValign(VerticalAlignment::AlignSubScript);
unstyleFontStyle = true;
}
else if (token == "/sub")
cursor.format()->setValign(VerticalAlignment::AlignNormal);
else if (token == "sup")
else if (token == "sup") {
cursor.format()->setValign(VerticalAlignment::AlignSuperScript);
unstyleFontStyle = true;
}
else if (token == "/sup")
cursor.format()->setValign(VerticalAlignment::AlignNormal);
else if (token == "sym") {
Expand All @@ -1748,19 +1753,42 @@ void TextBase::createLayout()
}
else if (token.startsWith("font ")) {
token = token.mid(5);
if (token.startsWith("size=\"")) {
cursor.format()->setFontSize(parseNumProperty(token.mid(6)));
setPropertyFlags(Pid::FONT_SIZE, PropertyFlags::UNSTYLED);
while (!token.isEmpty()) {
if (token.startsWith("size=\"")) {
qreal size = parseNumProperty(token.mid(6));
token = token.mid(token.indexOf('"', 6) + 1).trimmed();
if (!token.endsWith("/"))
prevFontSize = cursor.format()->fontSize();
cursor.format()->setFontSize(size);
setPropertyFlags(Pid::FONT_SIZE, PropertyFlags::UNSTYLED);
}
else if (token.startsWith("face=\"")) {
QString face = parseStringProperty(token.mid(6));
face = unEscape(face);
token = token.mid(token.indexOf('"', 6) + 1).trimmed();
if (!token.endsWith("/"))
prevFontFace = cursor.format()->fontFamily();
cursor.format()->setFontFamily(face);
setPropertyFlags(Pid::FONT_FACE, PropertyFlags::UNSTYLED);
}
else if (token == "/")
break;
else {
qDebug("cannot parse html property <%s> in text <%s>",
qPrintable(token), qPrintable(_text));
break;
}
}
else if (token.startsWith("face=\"")) {
QString face = parseStringProperty(token.mid(6));
face = unEscape(face);
cursor.format()->setFontFamily(face);
setPropertyFlags(Pid::FONT_FACE, PropertyFlags::UNSTYLED);
}
else if ( token == "/font" ) {
if (prevFontSize) {
cursor.format()->setFontSize(prevFontSize);
prevFontSize = 0;
}
if (!prevFontFace.isEmpty()) {
cursor.format()->setFontFamily(prevFontFace);
prevFontFace = "";
}
else
qDebug("cannot parse html property <%s> in text <%s>",
qPrintable(token), qPrintable(_text));
}
if (unstyleFontStyle)
setPropertyFlags(Pid::FONT_STYLE, PropertyFlags::UNSTYLED);
Expand Down Expand Up @@ -2082,10 +2110,14 @@ void TextBase::genText() const
xmlNesting.popS();
}

if (format.fontSize() != fmt.fontSize())
_text += QString("<font size=\"%1\"/>").arg(format.fontSize());
if (format.fontFamily() != fmt.fontFamily())
_text += QString("<font face=\"%1\"/>").arg(TextBase::escape(format.fontFamily()));
if (fmt.fontSize() != format.fontSize() || fmt.fontFamily() != format.fontFamily()) {
_text += "<font";
if (fmt.fontSize() != format.fontSize())
_text += QString(" size=\"%1\"/>").arg(format.fontSize());
if (fmt.fontFamily() != format.fontFamily())
_text += QString(" face=\"%1\"/>").arg(TextBase::escape(format.fontFamily()));
_text += "/>";
}

VerticalAlignment va = format.valign();
VerticalAlignment cva = fmt.valign();
Expand Down Expand Up @@ -3316,10 +3348,15 @@ QString TextBase::stripText(bool removeStyle, bool removeSize, bool removeFace)
}
}

if (!removeSize && (format.fontSize() != fmt.fontSize()))
_txt += QString("<font size=\"%1\"/>").arg(format.fontSize());
if (!removeFace && (format.fontFamily() != fmt.fontFamily()))
_txt += QString("<font face=\"%1\"/>").arg(TextBase::escape(format.fontFamily()));
if ((!removeSize && fmt.fontSize() != format.fontSize()) ||
(!removeSize && fmt.fontSize() != format.fontSize())) {
_txt += "<font";
if (!removeSize && fmt.fontSize() != format.fontSize())
_txt += QString(" size=\"%1\"").arg(format.fontSize());
if (!removeFace && fmt.fontFamily() != format.fontFamily())
_txt += QString(" face=\"%1\"").arg(TextBase::escape(format.fontFamily()));
_txt += "/>"; // TODO: proper nesting with an end tag "</font>"
}

VerticalAlignment va = format.valign();
VerticalAlignment cva = fmt.valign();
Expand Down

0 comments on commit 0725eaa

Please sign in to comment.