Skip to content

Commit

Permalink
Add support for guifontwide
Browse files Browse the repository at this point in the history
  • Loading branch information
yehuohan authored and jgehrig committed Jun 20, 2020
1 parent 66b3511 commit 746d7ad
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/gui/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,41 @@ bool Shell::setGuiFont(const QString& fdesc, bool force, bool updateOption)
return true;
}

bool Shell::setGuiFontWide(const QString& fdesc) noexcept
{
// An empty list is valid, use guifont
if (fdesc.isEmpty())
{
m_guifontwidelist = {};
update();
return true;
}

const QStringList fdescList{ fdesc.split(",") };
if (fdescList.size() < 1) {
return false;
}

std::vector<QFont> fontList;
fontList.reserve(fdescList.size());

for (const auto& strFont : fdescList) {
QVariant varFont{ TryGetQFontFromDescription(strFont) };

if (!ShellWidget::IsValidFont(varFont)) {
m_nvim->api0()->vim_report_error(
m_nvim->encode(varFont.toString()));
return false;
}

fontList.push_back(qvariant_cast<QFont>(varFont));
}

m_guifontwidelist = std::move(fontList);
update();
return true;
}

Shell::~Shell()
{
if (m_nvim && m_attached) {
Expand Down Expand Up @@ -821,6 +856,7 @@ void Shell::handleSetOption(const QString& name, const QVariant& value)
setGuiFont(value.toString(), false /*force*/, false /*setOption*/);
} else if (name == "guifontset") {
} else if (name == "guifontwide") {
handleGuiFontWide(value);
} else if (name == "linespace") {
// The conversion to string and then to int happens because of http://doc.qt.io/qt-5/qvariant.html#toUInt
// toUint() fails to detect an overflow i.e. it converts to ulonglong and then returns a MAX UINT
Expand Down Expand Up @@ -869,6 +905,19 @@ void Shell::handleGuiFontFunction(const QVariantList& args)
setGuiFont(fdesc, force, true /*setOption*/);
}

void Shell::handleGuiFontWide(const QVariant& value) noexcept
{
if (!value.canConvert<QByteArray>())
{
qWarning() << "Unexpected value for guifontwide:" << value;
return;
}

const QString fdesc{ m_nvim->decode(value.toByteArray()) };

setGuiFontWide(fdesc);
}

void Shell::handleGridResize(const QVariantList& opargs)
{
if (opargs.size() < 3
Expand Down
2 changes: 2 additions & 0 deletions src/gui/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public slots:
void resizeNeovim(const QSize&);
void resizeNeovim(int n_cols, int n_rows);
bool setGuiFont(const QString& fdesc, bool force, bool updateOption);
bool setGuiFontWide(const QString& fdesc) noexcept;
void updateGuiWindowState(Qt::WindowStates state);
void openFiles(const QList<QUrl> url);

Expand Down Expand Up @@ -134,6 +135,7 @@ protected slots:
virtual void handlePopupMenuSelect(const QVariantList& opargs);
virtual void handleMouse(bool);
virtual void handleGuiFontFunction(const QVariantList& args);
virtual void handleGuiFontWide(const QVariant& value) noexcept;

// Modern 'ext_linegrid' Grid UI Events
virtual void handleGridResize(const QVariantList& opargs);
Expand Down
19 changes: 19 additions & 0 deletions src/gui/shellwidget/shellwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ QFont ShellWidget::GetCellFont(const Cell& cell) const noexcept
{
QFont cellFont{ font() };

if (cell.IsDoubleWidth()) {

auto isCharacterInWideFont = [&](const QFont& wideFont) noexcept
{
return QFontMetrics(wideFont).inFontUcs4(cell.GetCharacter());
};

auto wideFont = std::find_if(m_guifontwidelist.begin(), m_guifontwidelist.end(), isCharacterInWideFont);

if (wideFont != m_guifontwidelist.end())
{
cellFont = *wideFont;
}
}

if (cell.IsBold() || cell.IsItalic()) {
cellFont.setBold(cell.IsBold());
cellFont.setItalic(cell.IsItalic());
Expand All @@ -214,6 +229,10 @@ QFont ShellWidget::GetCellFont(const Cell& cell) const noexcept
// but we want to match the family name with the bold/italic attributes.
cellFont.setStyleName(QStringLiteral(""));

cellFont.setStyleHint(QFont::TypeWriter, QFont::StyleStrategy(QFont::PreferDefault | QFont::ForceIntegerMetrics));
cellFont.setFixedPitch(true);
cellFont.setKerning(false);

return cellFont;
}

Expand Down
7 changes: 7 additions & 0 deletions src/gui/shellwidget/shellwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public slots:
/// Get the area filled by the cursor
QRect neovimCursorRect() const noexcept;

std::vector<QFont> m_guifontwidelist;

/// Move the neovim cursor for text insertion and display
void setNeovimCursor(uint64_t col, uint64_t row) noexcept;

Expand All @@ -129,6 +131,11 @@ public slots:
void setCellSize();
QRect absoluteShellRect(int row0, int col0, int rowcount, int colcount);

void setGuiFontList(const std::vector<QFont>&& fontList) noexcept
{
m_guifontwidelist = fontList;
}

private:
void setFont(const QFont&);
void handleCursorChanged();
Expand Down

0 comments on commit 746d7ad

Please sign in to comment.