Start work on rendering fonts using FreeType

This commit is contained in:
Kovid Goyal
2016-10-23 23:12:00 +05:30
parent 6f81e12041
commit 6a23dbe1ec
37 changed files with 6064 additions and 19 deletions
+3 -1
View File
@@ -52,7 +52,7 @@ class Boss(Thread):
def on_wakeup(self):
try:
os.read(self.read_wakeup_fd, 1024)
except Exception:
except (EnvironmentError, BlockingIOError):
pass
buf = b''
while True:
@@ -96,6 +96,8 @@ class Boss(Thread):
return
try:
data = os.read(self.child_fd, io.DEFAULT_BUFFER_SIZE)
except BlockingIOError:
return
except EnvironmentError:
data = b''
if not data:
+5 -6
View File
@@ -6,6 +6,9 @@ import re
from collections import namedtuple
from typing import Tuple
from .fonts import validate_monospace_font
key_pat = re.compile(r'([a-zA-Z][a-zA-Z0-9_-]*)\s+(.+)$')
# Color definitions {{{
color_pat = re.compile(r'^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$')
@@ -203,7 +206,9 @@ type_map = {
'cursor_opacity': float,
'cursor_shape': to_cursor_shape,
'cursor_blink': to_bool,
'font_family': validate_monospace_font,
}
for name in 'foreground foreground_bold background cursor'.split():
type_map[name] = lambda x: to_color(x, validate=True)
for i in range(16):
@@ -215,7 +220,6 @@ term xterm-kitty
foreground #dddddd
foreground_bold #ffffff
cursor #eeeeee
cursor_opacity 1.0
cursor_shape block
cursor_blink no
background #000000
@@ -293,11 +297,6 @@ def load_config(path: str) -> Options:
return Options(**ans)
def validate_font(opts: Options):
if False: # TODO: Implement this
raise ValueError('The font specified in the configuration "{}" is not a monospace font'.format(opts.font_family))
def build_ansi_color_tables(opts: Options) -> Tuple[dict, dict]:
def col(i):
return getattr(opts, 'color{}'.format(i))
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
import subprocess
import re
from freetype import Face
def escape_family_name(name):
return re.sub(r'([-:,\\])', lambda m: '\\' + m.group(1), name)
def fc_match(q, bold=False, italic=False):
q = escape_family_name(q)
if bold:
q += ':bold=200'
if italic:
q += ':slant=100'
return subprocess.check_output(['fc-match', q, '-f', '%{file}']).decode('utf-8')
def validate_monospace_font(raw_name):
raw = fc_match(raw_name)
if not raw:
raise ValueError('Failed to find a font matching the name: {}'.format(raw_name))
f = Face(raw)
if not f.is_fixed_width:
raise ValueError('The font {} is not a monospace font'.format(raw_name))
return f, raw_name
def get_font_files(family):
ans = {}
b = fc_match(family, bold=True)
if b:
ans['bold'] = Face(b)
i = fc_match(family, italic=True)
if i:
ans['italic'] = Face(i)
bi = fc_match(family, True, True)
if bi:
ans['bi'] = Face(bi)
return ans
def load_font_family(r):
face, raw_name = r
ans = get_font_files(raw_name)
ans['regular'] = face
return ans
+1 -5
View File
@@ -9,7 +9,7 @@ import pwd
from gettext import gettext as _
from .config import load_config, validate_font
from .config import load_config
from .constants import appname, str_version, config_dir
from .boss import Boss
from .utils import fork_child, hangup
@@ -78,10 +78,6 @@ def main():
exec(args.cmd)
return
opts = load_config(args.config)
try:
validate_font(opts)
except ValueError as err:
raise SystemExit(str(err)) from None
glfw.glfwSetErrorCallback(on_glfw_error)
if not glfw.glfwInit():
raise SystemExit('GLFW initialization failed')
+17
View File
@@ -4,6 +4,7 @@
import os
import re
import subprocess
import sys
import termios
import struct
@@ -15,6 +16,8 @@ from contextlib import contextmanager
from functools import lru_cache
from time import monotonic
import glfw
from .constants import terminfo_dir
libc = ctypes.CDLL(None)
@@ -103,3 +106,17 @@ def timeit(name, do_timing=False):
def sanitize_title(x):
return re.sub(r'\s+', ' ', re.sub(r'[\0-\x19]', '', x))
def get_dpi():
if not hasattr(get_dpi, 'ans'):
m = glfw.glfwGetPrimaryMonitor()
width, height = glfw.glfwGetMonitorPhysicalSize(m)
vmode = glfw.glfwGetVideoMode(m)
dpix = vmode.width / (width / 25.4)
dpiy = vmode.height / (height / 25.4)
get_dpi.ans = {'physical': (dpix, dpiy)}
raw = subprocess.check_output(['xdpyinfo']).decode('utf-8')
m = re.search(r'^\s*resolution:\s*(\d+)+x(\d+)', raw, flags=re.MULTILINE)
get_dpi.ans['logical'] = (int(m.group(1)), int(m.group(2)))
return get_dpi.ans