aboutsummaryrefslogtreecommitdiff
path: root/fontconfig.lua
diff options
context:
space:
mode:
authortakase1121 <20792268+takase1121@users.noreply.github.com>2021-08-13 20:47:15 +0800
committertakase1121 <20792268+takase1121@users.noreply.github.com>2021-08-21 09:43:19 +0800
commita14a839618700406a20746f13146a03648611bd7 (patch)
tree8a0cc29a767d616440808c710f6f25b775628486 /fontconfig.lua
parentdb213743f769df28e370cbd49a73e147756612c8 (diff)
downloadlite-xl-plugins-a14a839618700406a20746f13146a03648611bd7.tar.gz
lite-xl-plugins-a14a839618700406a20746f13146a03648611bd7.zip
add fontconfig plugin
Diffstat (limited to 'fontconfig.lua')
-rw-r--r--fontconfig.lua57
1 files changed, 57 insertions, 0 deletions
diff --git a/fontconfig.lua b/fontconfig.lua
new file mode 100644
index 0000000..3ef3de9
--- /dev/null
+++ b/fontconfig.lua
@@ -0,0 +1,57 @@
+-- mod-version:1 -- lite-xl 1.16
+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 fc = require "plugins.fontconfig"
+ fc(
+ { name = "sans", size = 13 * SCALE }, -- UI font
+ { name = "monospace", size = 13 * SCALE } -- code font
+ )
+ ```
+]]
+
+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, lines = {}, {}
+ while proc:running() do
+ coroutine.yield(scan_rate)
+ local buf = proc:read_stdout()
+ local p, _, n = string.match(buf, "(.+)\n(.+)")
+ if p then
+ prev[#prev + 1] = p
+ lines[#lines + 1] = table.concat(prev, "")
+ prev = { n }
+ else
+ prev[#prev + 1] = buf
+ end
+ end
+ table.insert(lines, table.concat(prev, ""))
+
+ if proc:returncode() ~= 0 or #lines < 1 then
+ error(string.format("Cannot find a font matching the given specs: %q", spec), 0)
+ end
+ -- maybe in the future we can detect and do glyph substitution here...
+ return lines[1]
+end
+
+local function load_system_fonts(font, code_font)
+ core.add_thread(function()
+ local font_file = resolve_font(font.name)
+ local code_font_file = resolve_font(code_font.name)
+ style.font = renderer.font.load(font_file, font.size, font)
+ style.code_font = renderer.font.load(code_font_file, code_font.size, code_font)
+ end)
+end
+
+return load_system_fonts \ No newline at end of file