Use QTextCharFormat::fontFixedPitch to remember Markdown backtick spans
If the editing app (like qtbase/examples/widgets/richtext/textedit)
has controls only for setting a specific font, and someone uses it to
write markdown "from scratch", then we need to detect that they chose
Courier or some other fixed-pitch font, and write the backticks,
because Markdown has no syntax for selecting a specific font family.
If the user loads markdown into such an application, the font
is set to QFontDatabase::systemFont(QFontDatabase::FixedFont).
Round-trip editing was already working, as long as such a font exists.
QTextCharFormat::setFont() calls setFontFixedPitch(font.fixedPitch()),
but for the chosen "mono" font, font.fixedPitch() can be false.
For semantic completeness and separation of concerns, we now
set fontFixedPitch explicitly if a `backtick` span is encountered.
As a followup to f1e60de665 this
should get its autotest passing reliably.
Fixes: QTBUG-99676
Pick-to: 6.3 6.2 5.15
Change-Id: I4987a1f0f819f82ec64546bdc3ef53e7d29933de
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
bb10
parent
2a086a9857
commit
17dca04a61
|
|
@ -426,6 +426,7 @@ int QTextMarkdownImporter::cbEnterSpan(int spanType, void *det)
|
|||
}
|
||||
case MD_SPAN_CODE:
|
||||
charFmt.setFont(m_monoFont);
|
||||
charFmt.setFontFixedPitch(true);
|
||||
break;
|
||||
case MD_SPAN_DEL:
|
||||
charFmt.setFontStrikeOut(true);
|
||||
|
|
|
|||
|
|
@ -532,7 +532,7 @@ int QTextMarkdownWriter::writeBlock(const QTextBlock &block, bool wrap, bool ign
|
|||
col += s.length();
|
||||
} else {
|
||||
QFontInfo fontInfo(fmt.font());
|
||||
bool monoFrag = fontInfo.fixedPitch();
|
||||
bool monoFrag = fontInfo.fixedPitch() || fmt.fontFixedPitch();
|
||||
QString markers;
|
||||
if (!ignoreFormat) {
|
||||
if (monoFrag != mono && !m_indentedCodeBlock && !m_fencedCodeBlock) {
|
||||
|
|
|
|||
|
|
@ -77,6 +77,7 @@ public:
|
|||
Mono = 0x10,
|
||||
Link = 0x20
|
||||
};
|
||||
Q_ENUM(CharFormat)
|
||||
Q_DECLARE_FLAGS(CharFormats, CharFormat)
|
||||
};
|
||||
|
||||
|
|
@ -364,13 +365,15 @@ void tst_QTextMarkdownImporter::nestedSpans()
|
|||
<< "underlined" << fmt.fontUnderline()
|
||||
<< "strikeout" << fmt.fontStrikeOut() << "anchor" << fmt.isAnchor()
|
||||
<< "monospace" << QFontInfo(fmt.font()).fixedPitch() // depends on installed fonts (QTBUG-75649)
|
||||
<< fmt.fontFixedPitch() // returns false even when font family is "monospace"
|
||||
<< fmt.hasProperty(QTextFormat::FontFixedPitch); // works
|
||||
<< fmt.fontFixedPitch()
|
||||
<< fmt.hasProperty(QTextFormat::FontFixedPitch)
|
||||
<< "expected" << expectedFormat;
|
||||
QCOMPARE(fmt.fontWeight() > QFont::Normal, expectedFormat.testFlag(Bold));
|
||||
QCOMPARE(fmt.fontItalic(), expectedFormat.testFlag(Italic));
|
||||
QCOMPARE(fmt.fontUnderline(), expectedFormat.testFlag(Underlined));
|
||||
QCOMPARE(fmt.fontStrikeOut(), expectedFormat.testFlag(Strikeout));
|
||||
QCOMPARE(fmt.isAnchor(), expectedFormat.testFlag(Link));
|
||||
QCOMPARE(fmt.fontFixedPitch(), expectedFormat.testFlag(Mono));
|
||||
QCOMPARE(fmt.hasProperty(QTextFormat::FontFixedPitch), expectedFormat.testFlag(Mono));
|
||||
++iterator;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue