aboutsummaryrefslogtreecommitdiff
path: root/plugins/fontconfig.lua
blob: 03a2f95bc6ca2f5a6b0d8b21fddec467dedd0815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
-- mod-version:3
local subprocess = require "process"

local core = require "core"
local style = require "core.style"
local config = require "core.config"
local common = require "core.common"

config.plugins.fontconfig = common.merge({ prefix = "" }, config.plugins.fontconfig)

--[[
  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({ config.plugins.fontconfig.prefix .. "fc-match", "-s", "-f", "%{file}\n", spec }, {
    stdin = subprocess.REDIRECT_DISCARD,
    stdout = subprocess.REDIRECT_PIPE,
    stderr = subprocess.REDIRECT_STDOUT
  })
  local prev
  local lines = {}
  while true do
    coroutine.yield(scan_rate)
    local buf = proc:read_stdout()
    if not buf then break end

    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
  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.load_group_blocking(group, font_size, font_opt)
  local fonts = {}
  for i in ipairs(group) do
    local co = coroutine.create(function()
      return M.load(group[i], 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
    table.insert(fonts, result)
  end

  return renderer.font.group(fonts)
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
    local font
    if value.group then
      font = M.load_group_blocking(value.group, value.size, value)
    else
      font = M.load_blocking(value.name, value.size, value)
    end
    style[key] = font
  end
end

return M