From 40dec1b9fffc9b1f051254bfcebda4841132e5a5 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Sun, 31 Jul 2022 11:51:34 +0800 Subject: [PATCH] - fix: (wrongly) use the default font to calculate non-ascii characters' width --- NEWS.md | 1 + RedPandaIDE/qsynedit/SynEdit.cpp | 6 ++++-- RedPandaIDE/qsynedit/TextBuffer.cpp | 13 ++++++++++--- RedPandaIDE/qsynedit/TextBuffer.h | 5 +++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index 29ac795b..9eafb18c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,7 @@ Red Panda C++ Version 1.1.6 - enhancement: disable column mode in readonly editors - fix: inefficient loop when render long lines - 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 diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index 00ae642f..9c66cc23 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -62,7 +62,7 @@ SynEdit::SynEdit(QWidget *parent) : QAbstractScrollArea(parent), #error "Not supported!" #endif mFontDummy.setStyleStrategy(QFont::PreferAntialias); - mDocument = std::make_shared(mFontDummy, this); + mDocument = std::make_shared(mFontDummy, mFontDummy, this); //fPlugins := TList.Create; mMouseMoved = false; mUndoing = false; @@ -4068,6 +4068,8 @@ void SynEdit::setFontForNonAscii(const QFont &newFontForNonAscii) { mFontForNonAscii = newFontForNonAscii; mFontForNonAscii.setStyleStrategy(QFont::PreferAntialias); + if (mDocument) + mDocument->setFontMetrics(font(),mFontForNonAscii); } const QColor &SynEdit::backgroundColor() const @@ -6197,7 +6199,7 @@ bool SynEdit::event(QEvent *event) case QEvent::FontChange: synFontChanged(); if (mDocument) - mDocument->setFontMetrics(font()); + mDocument->setFontMetrics(font(),mFontForNonAscii); break; case QEvent::MouseMove: { updateMouseCursor(); diff --git a/RedPandaIDE/qsynedit/TextBuffer.cpp b/RedPandaIDE/qsynedit/TextBuffer.cpp index 36ba8bfa..6689f24f 100644 --- a/RedPandaIDE/qsynedit/TextBuffer.cpp +++ b/RedPandaIDE/qsynedit/TextBuffer.cpp @@ -27,9 +27,10 @@ #include #include -SynDocument::SynDocument(const QFont& font, QObject *parent): +SynDocument::SynDocument(const QFont& font, const QFont& nonAsciiFont, QObject *parent): QObject(parent), mFontMetrics(font), + mNonAsciiFontMetrics(nonAsciiFont), mTabWidth(4), mMutex(QMutex::Recursive) { @@ -515,10 +516,11 @@ const QFontMetrics &SynDocument::fontMetrics() const return mFontMetrics; } -void SynDocument::setFontMetrics(const QFont &newFont) +void SynDocument::setFontMetrics(const QFont &newFont, const QFont& newNonAsciiFont) { mFontMetrics = QFontMetrics(newFont); mCharWidth = mFontMetrics.horizontalAdvance("M"); + mNonAsciiFontMetrics = QFontMetrics(newNonAsciiFont); } void SynDocument::setTabWidth(int newTabWidth) @@ -723,8 +725,13 @@ int SynDocument::charColumns(QChar ch) const { if (ch.unicode()<=32) 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)) / (double)mCharWidth); + return std::ceil(width / (double)mCharWidth); } void SynDocument::putTextStr(const QString &text) diff --git a/RedPandaIDE/qsynedit/TextBuffer.h b/RedPandaIDE/qsynedit/TextBuffer.h index 8369efc1..233c5057 100644 --- a/RedPandaIDE/qsynedit/TextBuffer.h +++ b/RedPandaIDE/qsynedit/TextBuffer.h @@ -60,7 +60,7 @@ class SynDocument : public QObject { Q_OBJECT 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 bracketLevels(int Index); @@ -116,7 +116,7 @@ public: void setTabWidth(int newTabWidth); const QFontMetrics &fontMetrics() const; - void setFontMetrics(const QFont &newFont); + void setFontMetrics(const QFont &newFont, const QFont& newNonAsciiFont); public slots: void invalidAllLineColumns(); @@ -144,6 +144,7 @@ private: //SynEdit* mEdit; QFontMetrics mFontMetrics; + QFontMetrics mNonAsciiFontMetrics; int mTabWidth; int mCharWidth; //int mCount;