aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authortakase1121 <20792268+takase1121@users.noreply.github.com>2021-08-13 20:51:58 +0800
committertakase1121 <20792268+takase1121@users.noreply.github.com>2021-08-21 09:43:23 +0800
commit4fac309dcb27cd7b535885868ee84e8e3dce2e9f (patch)
tree50a8e75fdcb71769fe3489fb043b7c1df94f608c /plugins
parenta14a839618700406a20746f13146a03648611bd7 (diff)
downloadlite-xl-plugins-4fac309dcb27cd7b535885868ee84e8e3dce2e9f.tar.gz
lite-xl-plugins-4fac309dcb27cd7b535885868ee84e8e3dce2e9f.zip
move fontconfig.lua into plugins
Diffstat (limited to 'plugins')
-rw-r--r--plugins/fontconfig.lua57
1 files changed, 57 insertions, 0 deletions
diff --git a/plugins/fontconfig.lua b/plugins/fontconfig.lua
new file mode 100644
index 0000000..3ef3de9
--- /dev/null
+++ b/plugins/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