Skip to content

Commit

Permalink
GUI: Neovim Guifont command
Browse files Browse the repository at this point in the history
After connecting to Neovim create a command Guifont that can be
used to set the GUI font. Internally it calls rpcnotify(channel, 'setguifont', )

The goal is resemble the `set guifont` mechanics:

1. The user calls Guifont without arguments, it display the current font
2. The user passes more arguments - used to determine the font to be used e.g.
   Monaco:h12 for 12pt Monaco (this follows the Vim font names in Windows/Mac)

When displaying the current font neovim-qt may display a different
font than the user passed to Guifont - this happens because it displaus
the actual font being used.
  • Loading branch information
equalsraf committed May 7, 2015
1 parent 39e1075 commit 05f61f4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
70 changes: 58 additions & 12 deletions src/gui/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <QKeyEvent>
#include "input.h"

static QString GUIFONT_CMD = "command! -nargs=? Guifont call rpcnotify(%1, 'setguifont', \"<args>\")";

namespace NeovimQt {

Shell::Shell(NeovimConnector *nvim, QWidget *parent)
Expand All @@ -19,17 +21,7 @@ Shell::Shell(NeovimConnector *nvim, QWidget *parent)
m_resizing(false), m_logo(QPixmap(":/neovim.png")),
m_neovimBusy(false)
{
QFont f;
f.setStyleStrategy(QFont::StyleStrategy(QFont::PreferDefault | QFont::ForceIntegerMetrics) );
f.setStyleHint(QFont::TypeWriter);
f.setFamily("DejaVu Sans Mono");
f.setFixedPitch(true);
f.setPointSize(11);
f.setKerning(false);
f.setFixedPitch(true);

m_font = f;
m_fm = new QFontMetrics(m_font);
setGuiFont("DejaVu Sans Mono:h11");

m_image = QImage(neovimSize(), QImage::Format_ARGB32_Premultiplied);

Expand Down Expand Up @@ -63,6 +55,53 @@ Shell::Shell(NeovimConnector *nvim, QWidget *parent)
}
}

/**
* Set the GUI font, or display the current font
*/
bool Shell::setGuiFont(const QString& fdesc)
{
if (fdesc.isEmpty()) {
QFontInfo fi(m_font);
QByteArray desc = m_nvim->encode(QString("%1:h%2\n").arg(fi.family()).arg(m_font.pointSize()));
m_nvim->neovimObject()->vim_out_write(desc);
return true;
}
QStringList attrs = fdesc.split(':');
if (attrs.size() < 1) {
m_nvim->neovimObject()->vim_report_error("Invalid font");
return false;
}

QFont f;
f.setStyleStrategy(QFont::StyleStrategy(QFont::PreferDefault | QFont::ForceIntegerMetrics) );
f.setStyleHint(QFont::TypeWriter);
f.setFamily(attrs.at(0));
f.setFixedPitch(true);
f.setKerning(false);
f.setFixedPitch(true);

foreach(QString attr, attrs) {
if (attr.size() >= 2 && attr[0] == 'h') {
bool ok = false;
int height = attr.mid(1).toInt(&ok);
if (!ok) {
m_nvim->neovimObject()->vim_report_error("Invalid font height");
return false;
}
f.setPointSize(height);
}
}

m_font = f;
m_fm = new QFontMetrics(m_font);

if (m_attached) {
requestResize(size());
}
return true;
}


Shell::~Shell()
{
if (m_nvim && m_attached) {
Expand Down Expand Up @@ -150,6 +189,9 @@ void Shell::neovimIsReady()

connect(m_nvim->neovimObject(), &Neovim::on_ui_try_resize,
this, &Shell::neovimResizeFinished);

// Setup Guifont command
m_nvim->neovimObject()->vim_command(m_nvim->encode(GUIFONT_CMD.arg(m_nvim->channel())));
}

void Shell::neovimError(NeovimConnector::NeovimError err)
Expand Down Expand Up @@ -460,7 +502,11 @@ void Shell::handleBusy(bool busy)
// FIXME: fix QVariant type conversions
void Shell::handleNeovimNotification(const QByteArray &name, const QVariantList& args)
{
if (name != "redraw") {
if (name == "setguifont" && args.size() == 1) {
QString fdesc = m_nvim->decode(args.at(0).toByteArray());
setGuiFont(fdesc);
return;
} else if (name != "redraw") {
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/gui/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Shell: public QWidget

public slots:
void handleNeovimNotification(const QByteArray &name, const QVariantList& args);
bool setGuiFont(const QString& fdesc);

protected slots:
void neovimIsReady();
Expand Down

0 comments on commit 05f61f4

Please sign in to comment.