- fix: (wrongly) use the default font to calculate non-ascii characters' width

This commit is contained in:
Roy Qu 2022-07-31 11:51:34 +08:00
parent 66746e8c3d
commit 40dec1b9ff
4 changed files with 18 additions and 7 deletions

View File

@ -9,6 +9,7 @@ Red Panda C++ Version 1.1.6
- enhancement: disable column mode in readonly editors - enhancement: disable column mode in readonly editors
- fix: inefficient loop when render long lines - fix: inefficient loop when render long lines
- fix: indents for "default" are not the same with "case" - fix: indents for "default" are not the same with "case"
- fix: (wrongly) use the default font to calculate non-ascii characters' width
Red Panda C++ Version 1.1.5 Red Panda C++ Version 1.1.5

View File

@ -62,7 +62,7 @@ SynEdit::SynEdit(QWidget *parent) : QAbstractScrollArea(parent),
#error "Not supported!" #error "Not supported!"
#endif #endif
mFontDummy.setStyleStrategy(QFont::PreferAntialias); mFontDummy.setStyleStrategy(QFont::PreferAntialias);
mDocument = std::make_shared<SynDocument>(mFontDummy, this); mDocument = std::make_shared<SynDocument>(mFontDummy, mFontDummy, this);
//fPlugins := TList.Create; //fPlugins := TList.Create;
mMouseMoved = false; mMouseMoved = false;
mUndoing = false; mUndoing = false;
@ -4068,6 +4068,8 @@ void SynEdit::setFontForNonAscii(const QFont &newFontForNonAscii)
{ {
mFontForNonAscii = newFontForNonAscii; mFontForNonAscii = newFontForNonAscii;
mFontForNonAscii.setStyleStrategy(QFont::PreferAntialias); mFontForNonAscii.setStyleStrategy(QFont::PreferAntialias);
if (mDocument)
mDocument->setFontMetrics(font(),mFontForNonAscii);
} }
const QColor &SynEdit::backgroundColor() const const QColor &SynEdit::backgroundColor() const
@ -6197,7 +6199,7 @@ bool SynEdit::event(QEvent *event)
case QEvent::FontChange: case QEvent::FontChange:
synFontChanged(); synFontChanged();
if (mDocument) if (mDocument)
mDocument->setFontMetrics(font()); mDocument->setFontMetrics(font(),mFontForNonAscii);
break; break;
case QEvent::MouseMove: { case QEvent::MouseMove: {
updateMouseCursor(); updateMouseCursor();

View File

@ -27,9 +27,10 @@
#include <QMessageBox> #include <QMessageBox>
#include <cmath> #include <cmath>
SynDocument::SynDocument(const QFont& font, QObject *parent): SynDocument::SynDocument(const QFont& font, const QFont& nonAsciiFont, QObject *parent):
QObject(parent), QObject(parent),
mFontMetrics(font), mFontMetrics(font),
mNonAsciiFontMetrics(nonAsciiFont),
mTabWidth(4), mTabWidth(4),
mMutex(QMutex::Recursive) mMutex(QMutex::Recursive)
{ {
@ -515,10 +516,11 @@ const QFontMetrics &SynDocument::fontMetrics() const
return mFontMetrics; return mFontMetrics;
} }
void SynDocument::setFontMetrics(const QFont &newFont) void SynDocument::setFontMetrics(const QFont &newFont, const QFont& newNonAsciiFont)
{ {
mFontMetrics = QFontMetrics(newFont); mFontMetrics = QFontMetrics(newFont);
mCharWidth = mFontMetrics.horizontalAdvance("M"); mCharWidth = mFontMetrics.horizontalAdvance("M");
mNonAsciiFontMetrics = QFontMetrics(newNonAsciiFont);
} }
void SynDocument::setTabWidth(int newTabWidth) void SynDocument::setTabWidth(int newTabWidth)
@ -723,8 +725,13 @@ int SynDocument::charColumns(QChar ch) const
{ {
if (ch.unicode()<=32) if (ch.unicode()<=32)
return 1; return 1;
int width;
if (ch.unicode()<0xFF)
width = mFontMetrics.horizontalAdvance(ch);
else
width = mNonAsciiFontMetrics.horizontalAdvance(ch);
//return std::ceil((int)(fontMetrics().horizontalAdvance(ch) * dpiFactor()) / (double)mCharWidth); //return std::ceil((int)(fontMetrics().horizontalAdvance(ch) * dpiFactor()) / (double)mCharWidth);
return std::ceil((int)(fontMetrics().horizontalAdvance(ch)) / (double)mCharWidth); return std::ceil(width / (double)mCharWidth);
} }
void SynDocument::putTextStr(const QString &text) void SynDocument::putTextStr(const QString &text)

View File

@ -60,7 +60,7 @@ class SynDocument : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit SynDocument(const QFont& font, QObject* parent=nullptr); explicit SynDocument(const QFont& font, const QFont& nonAsciiFont, QObject* parent=nullptr);
int parenthesisLevels(int Index); int parenthesisLevels(int Index);
int bracketLevels(int Index); int bracketLevels(int Index);
@ -116,7 +116,7 @@ public:
void setTabWidth(int newTabWidth); void setTabWidth(int newTabWidth);
const QFontMetrics &fontMetrics() const; const QFontMetrics &fontMetrics() const;
void setFontMetrics(const QFont &newFont); void setFontMetrics(const QFont &newFont, const QFont& newNonAsciiFont);
public slots: public slots:
void invalidAllLineColumns(); void invalidAllLineColumns();
@ -144,6 +144,7 @@ private:
//SynEdit* mEdit; //SynEdit* mEdit;
QFontMetrics mFontMetrics; QFontMetrics mFontMetrics;
QFontMetrics mNonAsciiFontMetrics;
int mTabWidth; int mTabWidth;
int mCharWidth; int mCharWidth;
//int mCount; //int mCount;