mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-06 06:19:52 +08:00
bug-fix
- Caculate font height every time on painting line number area; - Support specifying multiple fonts in font-family in qss; - Add margin for "li ol" in CSS;
This commit is contained in:
parent
8dfcda0e51
commit
45526cc0a8
@ -68,8 +68,8 @@ li {
|
||||
line-height: 24px;
|
||||
}
|
||||
|
||||
li ul, li ul {
|
||||
margin-left: 24px;
|
||||
li ul, li ol {
|
||||
margin-left: 16px;
|
||||
}
|
||||
|
||||
p, ul, ol {
|
||||
|
@ -68,8 +68,8 @@ li {
|
||||
line-height: 24px;
|
||||
}
|
||||
|
||||
li ul, li ul {
|
||||
margin-left: 24px;
|
||||
li ul, li ol {
|
||||
margin-left: 16px;
|
||||
}
|
||||
|
||||
p, ul, ol {
|
||||
|
@ -67,8 +67,8 @@ li {
|
||||
line-height: 24px;
|
||||
}
|
||||
|
||||
li ul, li ul {
|
||||
margin-left: 24px;
|
||||
li ul, li ol {
|
||||
margin-left: 16px;
|
||||
}
|
||||
|
||||
p, ul, ol {
|
||||
|
@ -1167,3 +1167,28 @@ QString VUtils::getCaptainShortcutSequenceText(const QString &p_operation)
|
||||
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString VUtils::getAvailableFontFamily(const QStringList &p_families)
|
||||
{
|
||||
QStringList availFamilies = QFontDatabase().families();
|
||||
|
||||
for (int i = 0; i < p_families.size(); ++i) {
|
||||
QString family = p_families[i].trimmed();
|
||||
if (family.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int j = 0; j < availFamilies.size(); ++j) {
|
||||
QString availFamily = availFamilies[j];
|
||||
availFamily.remove(QRegExp("\\[.*\\]"));
|
||||
availFamily = availFamily.trimmed();
|
||||
if (family == availFamily
|
||||
|| family.toLower() == availFamily.toLower()) {
|
||||
qDebug() << "matched font family" << availFamilies[j];
|
||||
return availFamilies[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return QString();
|
||||
}
|
||||
|
@ -285,6 +285,8 @@ public:
|
||||
|
||||
static QString getCaptainShortcutSequenceText(const QString &p_operation);
|
||||
|
||||
static QString getAvailableFontFamily(const QStringList &p_families);
|
||||
|
||||
// Regular expression for image link.
|
||||
// 
|
||||
// Captured texts (need to be trimmed):
|
||||
|
@ -6,7 +6,6 @@
|
||||
VLineNumberArea::VLineNumberArea(VTextEditWithLineNumber *p_editor,
|
||||
const QTextDocument *p_document,
|
||||
int p_digitWidth,
|
||||
int p_digitHeight,
|
||||
QWidget *p_parent)
|
||||
: QWidget(p_parent),
|
||||
m_editor(p_editor),
|
||||
@ -14,7 +13,6 @@ VLineNumberArea::VLineNumberArea(VTextEditWithLineNumber *p_editor,
|
||||
m_width(0),
|
||||
m_blockCount(-1),
|
||||
m_digitWidth(p_digitWidth),
|
||||
m_digitHeight(p_digitHeight),
|
||||
m_foregroundColor("black"),
|
||||
m_backgroundColor("grey")
|
||||
{
|
||||
|
@ -35,7 +35,6 @@ public:
|
||||
VLineNumberArea(VTextEditWithLineNumber *p_editor,
|
||||
const QTextDocument *p_document,
|
||||
int p_digitWidth,
|
||||
int p_digitHeight,
|
||||
QWidget *p_parent = nullptr);
|
||||
|
||||
QSize sizeHint() const Q_DECL_OVERRIDE
|
||||
@ -45,11 +44,6 @@ public:
|
||||
|
||||
int calculateWidth() const;
|
||||
|
||||
int getDigitHeight() const
|
||||
{
|
||||
return m_digitHeight;
|
||||
}
|
||||
|
||||
const QColor &getBackgroundColor() const;
|
||||
void setBackgroundColor(const QColor &p_color);
|
||||
|
||||
@ -68,7 +62,6 @@ private:
|
||||
int m_width;
|
||||
int m_blockCount;
|
||||
int m_digitWidth;
|
||||
int m_digitHeight;
|
||||
QColor m_foregroundColor;
|
||||
QColor m_backgroundColor;
|
||||
};
|
||||
|
@ -116,6 +116,7 @@ QString VPalette::fetchQtStyleSheet() const
|
||||
QString style = VUtils::readFileFromDisk(m_data.m_qssFile);
|
||||
fillStyle(style);
|
||||
fillAbsoluteUrl(style);
|
||||
fillFontFamily(style);
|
||||
|
||||
return style;
|
||||
}
|
||||
@ -262,3 +263,32 @@ QString VPalette::themeCodeBlockCssStyle(const QString &p_paletteFile)
|
||||
VPaletteMetaData data = getPaletteMetaData(p_paletteFile);
|
||||
return themeName(p_paletteFile) + "/" + QFileInfo(data.m_codeBlockCssFile).completeBaseName();
|
||||
}
|
||||
|
||||
void VPalette::fillFontFamily(QString &p_text) const
|
||||
{
|
||||
QRegExp reg("(\\s|^)font-family:([^;]+);");
|
||||
|
||||
int pos = 0;
|
||||
while (pos < p_text.size()) {
|
||||
int idx = p_text.indexOf(reg, pos);
|
||||
if (idx == -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
QString familyList = reg.cap(2).trimmed();
|
||||
familyList.remove('"');
|
||||
QString family = VUtils::getAvailableFontFamily(familyList.split(','));
|
||||
if (!family.isEmpty() && family != familyList) {
|
||||
if (family.contains(' ')) {
|
||||
family = "\"" + family + "\"";
|
||||
}
|
||||
|
||||
QString str = QString("%1font-family: %2;").arg(reg.cap(1)).arg(family);
|
||||
p_text.replace(idx, reg.matchedLength(), str);
|
||||
|
||||
pos = idx + str.size();
|
||||
} else {
|
||||
pos = idx + reg.matchedLength();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -44,6 +44,10 @@ public:
|
||||
// Fill "@xxx" in @p_text with corresponding style.
|
||||
void fillStyle(QString &p_text) const;
|
||||
|
||||
// QSS seems not to recognize multiple font-family values.
|
||||
// We will choose the first existing one.
|
||||
void fillFontFamily(QString &p_text) const;
|
||||
|
||||
const QHash<QString, QString> &getColorMapping() const;
|
||||
|
||||
// Read themes and return the mappings of editor styles.
|
||||
|
@ -64,7 +64,6 @@ void VPlainTextEdit::init()
|
||||
m_lineNumberArea = new VLineNumberArea(this,
|
||||
document(),
|
||||
fontMetrics().width(QLatin1Char('8')),
|
||||
fontMetrics().height(),
|
||||
this);
|
||||
connect(document(), &QTextDocument::blockCountChanged,
|
||||
this, &VPlainTextEdit::updateLineNumberAreaMargin);
|
||||
@ -393,7 +392,7 @@ void VPlainTextEdit::paintLineNumberArea(QPaintEvent *p_event)
|
||||
int bottom = top + (int)rect.height();
|
||||
int eventTop = p_event->rect().top();
|
||||
int eventBtm = p_event->rect().bottom();
|
||||
const int digitHeight = m_lineNumberArea->getDigitHeight();
|
||||
const int digitHeight = painter.fontMetrics().height();
|
||||
const int curBlockNumber = textCursor().block().blockNumber();
|
||||
painter.setPen(m_lineNumberArea->getForegroundColor());
|
||||
|
||||
|
@ -10,6 +10,8 @@
|
||||
#include <QtDebug>
|
||||
#include <QStringList>
|
||||
|
||||
#include "utils/vutils.h"
|
||||
|
||||
VStyleParser::VStyleParser()
|
||||
{
|
||||
markdownStyles = NULL;
|
||||
@ -73,7 +75,7 @@ QTextCharFormat VStyleParser::QTextCharFormatFromAttrs(pmh_style_attribute *attr
|
||||
case pmh_attr_type_font_family:
|
||||
{
|
||||
QString familyList(attrs->value->font_family);
|
||||
QString finalFamily = filterAvailableFontFamily(familyList);
|
||||
QString finalFamily = VUtils::getAvailableFontFamily(familyList.split(','));
|
||||
if (!finalFamily.isEmpty()) {
|
||||
format.setFontFamily(finalFamily);
|
||||
}
|
||||
@ -211,7 +213,7 @@ void VStyleParser::fetchMarkdownEditorStyles(QPalette &palette, QFont &font,
|
||||
case pmh_attr_type_font_family:
|
||||
{
|
||||
QString familyList(editorStyles->value->font_family);
|
||||
QString finalFamily = filterAvailableFontFamily(familyList);
|
||||
QString finalFamily = VUtils::getAvailableFontFamily(familyList.split(','));
|
||||
if (!finalFamily.isEmpty()) {
|
||||
font.setFamily(finalFamily);
|
||||
}
|
||||
@ -299,27 +301,3 @@ void VStyleParser::fetchMarkdownEditorStyles(QPalette &palette, QFont &font,
|
||||
selStyles = selStyles->next;
|
||||
}
|
||||
}
|
||||
|
||||
// @familyList is a comma separated string
|
||||
QString VStyleParser::filterAvailableFontFamily(const QString &familyList) const
|
||||
{
|
||||
QStringList families = familyList.split(',', QString::SkipEmptyParts);
|
||||
QStringList availFamilies = QFontDatabase().families();
|
||||
|
||||
qDebug() << "family:" << familyList;
|
||||
for (int i = 0; i < families.size(); ++i) {
|
||||
QString family = families[i].trimmed();
|
||||
for (int j = 0; j < availFamilies.size(); ++j) {
|
||||
QString availFamily = availFamilies[j];
|
||||
availFamily.remove(QRegExp("\\[.*\\]"));
|
||||
availFamily = availFamily.trimmed();
|
||||
if (family == availFamily
|
||||
|| family.toLower() == availFamily.toLower()) {
|
||||
qDebug() << "matched family:" << availFamilies[j];
|
||||
return availFamilies[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return QString();
|
||||
}
|
||||
|
@ -36,7 +36,6 @@ private:
|
||||
QBrush QBrushFromPmhAttr(pmh_attr_argb_color *attr) const;
|
||||
QTextCharFormat QTextCharFormatFromAttrs(pmh_style_attribute *attrs,
|
||||
const QFont &baseFont) const;
|
||||
QString filterAvailableFontFamily(const QString &familyList) const;
|
||||
pmh_style_collection *markdownStyles;
|
||||
};
|
||||
|
||||
|
@ -73,7 +73,6 @@ void VTextEdit::init()
|
||||
m_lineNumberArea = new VLineNumberArea(this,
|
||||
document(),
|
||||
fontMetrics().width(QLatin1Char('8')),
|
||||
fontMetrics().height(),
|
||||
this);
|
||||
connect(doc, &QTextDocument::blockCountChanged,
|
||||
this, &VTextEdit::updateLineNumberAreaMargin);
|
||||
@ -140,7 +139,7 @@ void VTextEdit::paintLineNumberArea(QPaintEvent *p_event)
|
||||
int bottom = top + (int)rect.height();
|
||||
int eventTop = p_event->rect().top();
|
||||
int eventBtm = p_event->rect().bottom();
|
||||
const int digitHeight = m_lineNumberArea->getDigitHeight();
|
||||
const int digitHeight = painter.fontMetrics().height();
|
||||
const int curBlockNumber = textCursor().block().blockNumber();
|
||||
painter.setPen(m_lineNumberArea->getForegroundColor());
|
||||
const int leading = (int)layout->getLineLeading();
|
||||
@ -187,7 +186,7 @@ void VTextEdit::paintLineNumberArea(QPaintEvent *p_event)
|
||||
top + leading,
|
||||
m_lineNumberArea->width(),
|
||||
digitHeight,
|
||||
Qt::AlignRight,
|
||||
Qt::AlignRight | Qt::AlignTop,
|
||||
numberStr);
|
||||
}
|
||||
|
||||
@ -233,7 +232,7 @@ void VTextEdit::paintLineNumberArea(QPaintEvent *p_event)
|
||||
top + leading,
|
||||
m_lineNumberArea->width(),
|
||||
digitHeight,
|
||||
Qt::AlignRight,
|
||||
Qt::AlignRight | Qt::AlignTop,
|
||||
numberStr);
|
||||
|
||||
if (currentLine) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user