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 10, 2024
1 parent 368b1c0 commit c10c1cd
Showing 1 changed file with 66 additions and 22 deletions.
88 changes: 66 additions & 22 deletions libmscore/textbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,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 @@ -1706,12 +1708,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 @@ -1736,20 +1748,43 @@ 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);
token = token.mid(5).trimmed();
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 @@ -2071,10 +2106,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 @@ -3305,10 +3344,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 c10c1cd

Please sign in to comment.