diff options
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | plugins/fontconfig.lua | 91 |
2 files changed, 92 insertions, 0 deletions
@@ -35,6 +35,7 @@ Plugin | Description [`exec`](plugins/exec.lua?raw=1) | Runs selected text through shell command and replaces with result [`ephemeraldocviews`](plugins/ephemeraldocviews.lua?raw=1) | Preview tabs. Opening a doc will replace the contents of the preview tab. Marks tabs as non-preview on any change. [`fallbackfonts`](https://github.com/takase1121/lite-fallback-fonts)* | Adds support for fallback fonts *([gif](https://raw.githubusercontent.com/takase1121/lite-fallback-fonts/master/assets/Iw18fI57J0.gif))* +[`fontconfig`](plugins/fontconfig.lua?raw=1) | Allows users to load fonts with [fontconfig](https://www.freedesktop.org/software/fontconfig/fontconfig-user.html). [`formatter`](https://github.com/vincens2005/lite-formatters)* | formatters for various languages [`ghmarkdown`](plugins/ghmarkdown.lua?raw=1) | Opens a preview of the current markdown file in a browser window *([screenshot](https://user-images.githubusercontent.com/3920290/82754898-f7394600-9dc7-11ea-8278-2305363ed372.png))* *[`gitdiff_highlight`](https://github.com/vincens2005/lite-xl-gitdiff-highlight)** | highlight changed lines from git *([screenshot](https://raw.githubusercontent.com/vincens2005/lite-xl-gitdiff-highlight/master/screenshot.png))* diff --git a/plugins/fontconfig.lua b/plugins/fontconfig.lua new file mode 100644 index 0000000..a3e0f57 --- /dev/null +++ b/plugins/fontconfig.lua @@ -0,0 +1,91 @@ +-- mod-version:2 -- lite-xl 2.0 +local subprocess = require "process" + +local core = require "core" +local style = require "core.style" +local config = require "core.config" + +--[[ + Example config (put it in user module): + + ``` + local fontconfig = require "plugins.fontconfig" + fontconfig.use { + font = { name = 'sans', size = 13 * SCALE }, + code_font = { name = 'monospace', size = 13 * SCALE }, + } + ``` + + if you want the fonts to load instantaneously on startup, + you can try your luck on fontconfig.use_blocking. I won't be responsible for + the slow startup time. +]] + +local function resolve_font(spec) + local scan_rate = 1 / config.fps + local proc = subprocess.start({ "fc-match", "-s", "-f", "%{file}\n", spec }, { + stdin = subprocess.REDIRECT_DISCARD, + stdout = subprocess.REDIRECT_PIPE, + stderr = subprocess.REDIRECT_STDOUT + }) + local prev + local lines = {} + while proc:running() do + coroutine.yield(scan_rate) + local buf = proc:read_stdout() + if type(buf) == "string" then + local last_line_start = 1 + for line, ln in string.gmatch(buf, "([^\n]-)\n()") do + last_line_start = ln + if prev then line = prev .. line end + table.insert(lines, line) + end + prev = last_line_start < #buf and string.sub(buf, last_line_start) + end + end + if prev then table.insert(lines, prev) end + + if proc:returncode() ~= 0 or #lines < 1 then + error(string.format("Cannot find a font matching the given specs: %q", spec), 0) + end + return lines[1] +end + + +local M = {} + +function M.load(font_name, font_size, font_opt) + local font_file = resolve_font(font_name) + return renderer.font.load(font_file, font_size, font_opt) +end + +function M.load_blocking(font_name, font_size, font_opt) + local co = coroutine.create(function() + return M.load(font_name, font_size, font_opt) + end) + local result + while coroutine.status(co) ~= "dead" do + local ok, err = coroutine.resume(co) + if not ok then error(err) end + result = err + end + return result +end + +function M.use(spec) + core.add_thread(function() + for key, value in pairs(spec) do + style[key] = M.load(value.name, value.size, value) + end + end) +end + +-- there is basically no need for this, but for the sake of completeness +-- I'll leave this here +function M.use_blocking(spec) + for key, value in pairs(spec) do + style[key] = M.load_blocking(value.name, value.size, value) + end +end + +return M |