- enhancement: setting non-ascii font for editors

This commit is contained in:
Roy Qu 2022-01-26 14:31:26 +08:00
parent 894e587f7d
commit a7557899d1
12 changed files with 675 additions and 551 deletions

View File

@ -7,6 +7,7 @@ Red Panda C++ Version 0.13.4
- fix: static members are not correct shown after Classname + '::'
- enhancement: show parameter tips for class constructors
- enhancement: when there are tips showing, don't show mouse tips
- enhancement: setting non-ascii font for editors
Red Panda C++ Version 0.13.3
- enhancement: restore editor position after rename symbol

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -4170,6 +4170,10 @@ void Editor::applySettings()
f.setPixelSize(pointToPixel(pSettings->editor().fontSize()));
f.setStyleStrategy(QFont::PreferAntialias);
setFont(f);
QFont f2=QFont(pSettings->editor().nonAsciiFontName());
f2.setPixelSize(pointToPixel(pSettings->editor().fontSize()));
f2.setStyleStrategy(QFont::PreferAntialias);
setFontForNonAscii(f2);
// Set gutter properties
gutter().setLeftOffset(pointToPixel(pSettings->editor().fontSize()) + pSettings->editor().gutterLeftOffset());

View File

@ -69,6 +69,8 @@ SynEdit::SynEdit(QWidget *parent) : QAbstractScrollArea(parent)
mFontDummy.setStyleStrategy(QFont::PreferAntialias);
setFont(mFontDummy);
setFontForNonAscii(mFontDummy);
mUndoList = std::make_shared<SynEditUndoList>();
mUndoList->connect(mUndoList.get(), &SynEditUndoList::addedUndo, this, &SynEdit::onUndoAdded);
mOrigUndoList = mUndoList;
@ -3103,14 +3105,21 @@ void SynEdit::recalcCharExtent()
mTextHeight = 0;
mCharWidth = 0;
QFontMetrics fm(font());
mTextHeight = fm.lineSpacing();
QFontMetrics fm2(font());
mTextHeight = std::max(fm.lineSpacing(),fm2.lineSpacing());
mCharWidth = fm.horizontalAdvance("M");
if (hasStyles[0]) { // has bold font
QFont f = font();
f.setBold(true);
QFontMetrics fm(f);
QFont f2 = font();
f2.setBold(true);
QFontMetrics fm2(f);
if (fm.lineSpacing()>mTextHeight)
mTextHeight=fm.lineSpacing();
if (fm2.lineSpacing()>mTextHeight)
mTextHeight=fm2.lineSpacing();
if (fm.horizontalAdvance("M")>mCharWidth)
mCharWidth = fm.horizontalAdvance("M");
}
@ -3118,8 +3127,13 @@ void SynEdit::recalcCharExtent()
QFont f = font();
f.setItalic(true);
QFontMetrics fm(f);
QFont f2 = font();
f2.setItalic(true);
QFontMetrics fm2(f);
if (fm.lineSpacing()>mTextHeight)
mTextHeight=fm.lineSpacing();
if (fm2.lineSpacing()>mTextHeight)
mTextHeight=fm2.lineSpacing();
if (fm.horizontalAdvance("M")>mCharWidth)
mCharWidth = fm.horizontalAdvance("M");
}
@ -3127,8 +3141,13 @@ void SynEdit::recalcCharExtent()
QFont f = font();
f.setStrikeOut(true);
QFontMetrics fm(f);
QFont f2 = font();
f2.setStrikeOut(true);
QFontMetrics fm2(f);
if (fm.lineSpacing()>mTextHeight)
mTextHeight=fm.lineSpacing();
if (fm2.lineSpacing()>mTextHeight)
mTextHeight=fm2.lineSpacing();
if (fm.horizontalAdvance("M")>mCharWidth)
mCharWidth = fm.horizontalAdvance("M");
}
@ -3136,8 +3155,13 @@ void SynEdit::recalcCharExtent()
QFont f = font();
f.setUnderline(true);
QFontMetrics fm(f);
QFont f2 = font();
f2.setUnderline(true);
QFontMetrics fm2(f);
if (fm.lineSpacing()>mTextHeight)
mTextHeight=fm.lineSpacing();
if (fm2.lineSpacing()>mTextHeight)
mTextHeight=fm2.lineSpacing();
if (fm.horizontalAdvance("M")>mCharWidth)
mCharWidth = fm.horizontalAdvance("M");
}
@ -3736,6 +3760,17 @@ void SynEdit::onScrolled(int)
invalidate();
}
const QFont &SynEdit::fontForNonAscii() const
{
return mFontForNonAscii;
}
void SynEdit::setFontForNonAscii(const QFont &newFontForNonAscii)
{
mFontForNonAscii = newFontForNonAscii;
mFontForNonAscii.setStyleStrategy(QFont::PreferAntialias);
}
const QColor &SynEdit::backgroundColor() const
{
return mBackgroundColor;

View File

@ -619,6 +619,7 @@ private:
int mCharsInWindow;
int mCharWidth;
QFont mFontDummy;
QFont mFontForNonAscii;
SynFontSmoothMethod mFontSmoothing;
bool mMouseMoved;
/* IME input */
@ -756,6 +757,9 @@ public:
QVariant inputMethodQuery(Qt::InputMethodQuery property) const override;
// QWidget interface
const QFont &fontForNonAscii() const;
void setFontForNonAscii(const QFont &newFontForNonAscii);
protected:
void dragEnterEvent(QDragEnterEvent *event) override;
void dropEvent(QDropEvent *event) override;

View File

@ -381,7 +381,9 @@ int SynEditTextPainter::ColumnToXValue(int Col)
return edit->textOffset() + (Col - 1) * edit->mCharWidth;
}
void SynEditTextPainter::PaintToken(const QString &Token, int TokenCols, int ColumnsBefore, int First, int Last, bool)
void SynEditTextPainter::PaintToken(const QString &Token, int TokenCols, int ColumnsBefore,
int First, int Last, bool /*isSelection*/, const QFont& font,
const QFont& fontForNonAscii)
{
bool startPaint;
int nX;
@ -446,7 +448,13 @@ void SynEditTextPainter::PaintToken(const QString &Token, int TokenCols, int Col
}
}
if (!drawed) {
painter->drawText(nX,rcToken.bottom()-painter->fontMetrics().descent() , Token[i]);
if (Token[i].unicode()<=255)
painter->drawText(nX,rcToken.bottom()-painter->fontMetrics().descent() , Token[i]);
else {
painter->setFont(fontForNonAscii);
painter->drawText(nX,rcToken.bottom()-painter->fontMetrics().descent() , Token[i]);
painter->setFont(font);
}
drawed = true;
}
nX += charCols * edit->mCharWidth;
@ -552,30 +560,36 @@ void SynEditTextPainter::PaintHighlightToken(bool bFillToEOL)
font.setStrikeOut(TokenAccu.Style & SynFontStyle::fsStrikeOut);
font.setUnderline(TokenAccu.Style & SynFontStyle::fsUnderline);
painter->setFont(font);
QFont nonAsciiFont = edit->fontForNonAscii();
nonAsciiFont.setBold(TokenAccu.Style & SynFontStyle::fsBold);
nonAsciiFont.setItalic(TokenAccu.Style & SynFontStyle::fsItalic);
nonAsciiFont.setStrikeOut(TokenAccu.Style & SynFontStyle::fsStrikeOut);
nonAsciiFont.setUnderline(TokenAccu.Style & SynFontStyle::fsUnderline);
// Paint the chars
if (bComplexToken) {
// first unselected part of the token
if (bU1) {
setDrawingColors(false);
rcToken.setRight(ColumnToXValue(nLineSelStart));
PaintToken(TokenAccu.s,TokenAccu.Columns,TokenAccu.ColumnsBefore,nC1,nLineSelStart,false);
PaintToken(TokenAccu.s,TokenAccu.Columns,TokenAccu.ColumnsBefore,nC1,nLineSelStart,false,font,nonAsciiFont);
}
// selected part of the token
setDrawingColors(true);
nC1Sel = std::max(nLineSelStart, nC1);
nC2Sel = std::min(nLineSelEnd, nC2);
rcToken.setRight(ColumnToXValue(nC2Sel));
PaintToken(TokenAccu.s, TokenAccu.Columns, TokenAccu.ColumnsBefore, nC1Sel, nC2Sel,true);
PaintToken(TokenAccu.s, TokenAccu.Columns, TokenAccu.ColumnsBefore, nC1Sel, nC2Sel,true,font,nonAsciiFont);
// second unselected part of the token
if (bU2) {
setDrawingColors(false);
rcToken.setRight(ColumnToXValue(nC2));
PaintToken(TokenAccu.s, TokenAccu.Columns, TokenAccu.ColumnsBefore,nLineSelEnd, nC2,false);
PaintToken(TokenAccu.s, TokenAccu.Columns, TokenAccu.ColumnsBefore,nLineSelEnd, nC2,false,font,nonAsciiFont);
}
} else {
setDrawingColors(bSel);
rcToken.setRight(ColumnToXValue(nC2));
PaintToken(TokenAccu.s, TokenAccu.Columns, TokenAccu.ColumnsBefore, nC1, nC2,bSel);
PaintToken(TokenAccu.s, TokenAccu.Columns, TokenAccu.ColumnsBefore, nC1, nC2,bSel,font,nonAsciiFont);
}
}
@ -941,17 +955,17 @@ void SynEditTextPainter::PaintLines()
setDrawingColors(true);
rcToken.setLeft(std::max(rcLine.left(), ColumnToXValue(nLineSelStart)));
rcToken.setRight(std::min(rcLine.right(), ColumnToXValue(nLineSelEnd)));
PaintToken(sToken, nTokenColumnLen, 0, nLineSelStart, nLineSelEnd,false);
PaintToken(sToken, nTokenColumnLen, 0, nLineSelStart, nLineSelEnd,false,edit->font(),edit->fontForNonAscii());
setDrawingColors(false);
rcToken.setLeft(std::max(rcLine.left(), ColumnToXValue(FirstCol)));
rcToken.setRight(std::min(rcLine.right(), ColumnToXValue(nLineSelStart)));
PaintToken(sToken, nTokenColumnLen, 0, FirstCol, nLineSelStart,false);
PaintToken(sToken, nTokenColumnLen, 0, FirstCol, nLineSelStart,false,edit->font(),edit->fontForNonAscii());
rcToken.setLeft(std::max(rcLine.left(), ColumnToXValue(nLineSelEnd)));
rcToken.setRight(std::min(rcLine.right(), ColumnToXValue(LastCol)));
PaintToken(sToken, nTokenColumnLen, 0, nLineSelEnd, LastCol,true);
PaintToken(sToken, nTokenColumnLen, 0, nLineSelEnd, LastCol,true,edit->font(),edit->fontForNonAscii());
} else {
setDrawingColors(bLineSelected);
PaintToken(sToken, nTokenColumnLen, 0, FirstCol, LastCol,bLineSelected);
PaintToken(sToken, nTokenColumnLen, 0, FirstCol, LastCol,bLineSelected,edit->font(),edit->fontForNonAscii());
}
//Paint editingAreaBorders
if (bCurrentLine && edit->mInputPreeditString.length()>0) {

View File

@ -49,7 +49,8 @@ private:
void setDrawingColors(bool Selected);
int ColumnToXValue(int Col);
void PaintToken(const QString& Token, int TokenLen, int ColumnsBefore,
int First, int Last, bool isSelection);
int First, int Last, bool isSelection, const QFont& font,
const QFont& fontForNonAscii);
void PaintEditAreas(const SynEditingAreaList& areaList);
void PaintHighlightToken(bool bFillToEOL);
bool TokenIsSpaces(bool& bSpacesTest, const QString& Token, bool& bIsSpaces);

View File

@ -588,6 +588,16 @@ void Settings::Editor::setEnableLigaturesSupport(bool newEnableLigaturesSupport)
mEnableLigaturesSupport = newEnableLigaturesSupport;
}
const QString &Settings::Editor::nonAsciiFontName() const
{
return mNonAsciiFontName;
}
void Settings::Editor::setNonAsciiFontName(const QString &newNonAsciiFontName)
{
mNonAsciiFontName = newNonAsciiFontName;
}
bool Settings::Editor::highlightCurrentWord() const
{
return mHighlightCurrentWord;
@ -1097,9 +1107,10 @@ void Settings::Editor::doSave()
//Font
//font
saveValue("font_name",mFontName);
saveValue("font_name", mFontName);
saveValue("non_ascii_font_name", mNonAsciiFontName);
saveValue("font_size", mFontSize);
saveValue("font_only_monospaced",mFontOnlyMonospaced);
saveValue("font_only_monospaced", mFontOnlyMonospaced);
saveValue("enable_ligatures_support", mEnableLigaturesSupport);
//gutter
@ -1215,8 +1226,10 @@ void Settings::Editor::doLoad()
//Font
#ifdef Q_OS_WIN
mFontName = stringValue("font_name","consolas");
mNonAsciiFontName = stringValue("non_ascii_font_name","consolas");
#else
mFontName = stringValue("font_name","Dejavu Sans Mono");
mNonAsciiFontName = stringValue("non_ascii_font_name","Dejavu Sans Mono");
#endif
mFontSize = intValue("font_size",14);
mFontOnlyMonospaced = boolValue("font_only_monospaced",true);

View File

@ -366,6 +366,9 @@ public:
bool enableLigaturesSupport() const;
void setEnableLigaturesSupport(bool newEnableLigaturesSupport);
const QString &nonAsciiFontName() const;
void setNonAsciiFontName(const QString &newNonAsciiFontName);
private:
//General
// indents
@ -405,6 +408,7 @@ public:
//Font
//font
QString mFontName;
QString mNonAsciiFontName;
int mFontSize;
bool mFontOnlyMonospaced;

View File

@ -56,6 +56,7 @@ void EditorFontWidget::doLoad()
//font
ui->chkOnlyMonospacedFonts->setChecked(pSettings->editor().fontOnlyMonospaced());
ui->cbFont->setCurrentFont(QFont(pSettings->editor().fontName()));
ui->cbNonAsciiFont->setCurrentFont(QFont(pSettings->editor().nonAsciiFontName()));
ui->spinFontSize->setValue(pSettings->editor().fontSize());
ui->chkLigature->setChecked(pSettings->editor().enableLigaturesSupport());
@ -79,6 +80,7 @@ void EditorFontWidget::doSave()
//font
pSettings->editor().setFontOnlyMonospaced(ui->chkOnlyMonospacedFonts->isChecked());
pSettings->editor().setFontName(ui->cbFont->currentFont().family());
pSettings->editor().setNonAsciiFontName(ui->cbNonAsciiFont->currentFont().family());
pSettings->editor().setFontSize(ui->spinFontSize->value());
pSettings->editor().setEnableLigaturesSupport(ui->chkLigature->isChecked());

View File

@ -133,6 +133,47 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Font for non-ascii Text:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QWidget" name="widget_9" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_7">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QFontComboBox" name="cbNonAsciiFont"/>
</item>
<item>
<spacer name="horizontalSpacer_7">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>