diff --git a/src/gui/shell.cpp b/src/gui/shell.cpp index d652c0cbc..c340519fd 100644 --- a/src/gui/shell.cpp +++ b/src/gui/shell.cpp @@ -8,6 +8,8 @@ #include #include "input.h" +static QString GUIFONT_CMD = "command! -nargs=? Guifont call rpcnotify(%1, 'setguifont', \"\")"; + namespace NeovimQt { Shell::Shell(NeovimConnector *nvim, QWidget *parent) @@ -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); @@ -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) { @@ -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) @@ -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; } diff --git a/src/gui/shell.h b/src/gui/shell.h index 80d0a392d..ab3f68e02 100644 --- a/src/gui/shell.h +++ b/src/gui/shell.h @@ -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();