Also get font hinting config from font-config
This commit is contained in:
parent
67964962ae
commit
697db21c64
@ -991,6 +991,8 @@ class Face( object ):
|
|||||||
if self._FT_Face is not None:
|
if self._FT_Face is not None:
|
||||||
FT_Done_Face( self._FT_Face )
|
FT_Done_Face( self._FT_Face )
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return 'Face({})'.format(self.family_name)
|
||||||
|
|
||||||
def attach_file( self, filename ):
|
def attach_file( self, filename ):
|
||||||
'''
|
'''
|
||||||
|
|||||||
@ -6,8 +6,6 @@ import re
|
|||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
from .fonts import validate_monospace_font
|
|
||||||
|
|
||||||
|
|
||||||
key_pat = re.compile(r'([a-zA-Z][a-zA-Z0-9_-]*)\s+(.+)$')
|
key_pat = re.compile(r'([a-zA-Z][a-zA-Z0-9_-]*)\s+(.+)$')
|
||||||
# Color definitions {{{
|
# Color definitions {{{
|
||||||
@ -206,7 +204,6 @@ type_map = {
|
|||||||
'cursor_opacity': float,
|
'cursor_opacity': float,
|
||||||
'cursor_shape': to_cursor_shape,
|
'cursor_shape': to_cursor_shape,
|
||||||
'cursor_blink': to_bool,
|
'cursor_blink': to_bool,
|
||||||
'font_family': validate_monospace_font,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for name in 'foreground foreground_bold background cursor'.split():
|
for name in 'foreground foreground_bold background cursor'.split():
|
||||||
|
|||||||
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import re
|
import re
|
||||||
|
from collections import namedtuple
|
||||||
|
from functools import lru_cache
|
||||||
|
|
||||||
from freetype import Face
|
from freetype import Face
|
||||||
|
|
||||||
@ -11,43 +13,41 @@ from freetype import Face
|
|||||||
def escape_family_name(name):
|
def escape_family_name(name):
|
||||||
return re.sub(r'([-:,\\])', lambda m: '\\' + m.group(1), name)
|
return re.sub(r'([-:,\\])', lambda m: '\\' + m.group(1), name)
|
||||||
|
|
||||||
|
Font = namedtuple('Font', 'face hinting hintstyle bold italic')
|
||||||
|
|
||||||
def fc_match(q, bold=False, italic=False):
|
|
||||||
|
@lru_cache()
|
||||||
|
def get_font_information(q, bold=False, italic=False):
|
||||||
q = escape_family_name(q)
|
q = escape_family_name(q)
|
||||||
if bold:
|
if bold:
|
||||||
q += ':bold=200'
|
q += ':bold=200'
|
||||||
if italic:
|
if italic:
|
||||||
q += ':slant=100'
|
q += ':slant=100'
|
||||||
return subprocess.check_output(['fc-match', q, '-f', '%{file}']).decode('utf-8')
|
raw = subprocess.check_output(['fc-match', q, '-f', '%{file}\x31%{hinting}\x31%{hintstyle}']).decode('utf-8')
|
||||||
|
parts = raw.split('\x31')
|
||||||
|
hintstyle, hinting = 1, 'True'
|
||||||
def validate_monospace_font(raw_name):
|
if len(parts) == 3:
|
||||||
raw = fc_match(raw_name)
|
path, hinting, hintstyle = parts
|
||||||
if not raw:
|
else:
|
||||||
raise ValueError('Failed to find a font matching the name: {}'.format(raw_name))
|
path = parts[0]
|
||||||
f = Face(raw)
|
hinting = hinting.lower() == 'true'
|
||||||
if not f.is_fixed_width:
|
hintstyle = int(hintstyle)
|
||||||
raise ValueError('The font {} is not a monospace font'.format(raw_name))
|
return Font(path, hinting, hintstyle, bold, italic)
|
||||||
return f, raw_name
|
|
||||||
|
|
||||||
|
|
||||||
def get_font_files(family):
|
def get_font_files(family):
|
||||||
ans = {}
|
ans = {}
|
||||||
|
n = get_font_information(family)
|
||||||
|
ans['regular'] = Font(Face(n.face), n.hinting, n.hintstyle, n.bold, n.italic)
|
||||||
|
|
||||||
b = fc_match(family, bold=True)
|
def do(key):
|
||||||
if b:
|
b = get_font_information(family, bold=key in ('bold', 'bi'), italic=key in ('italic', 'bi'))
|
||||||
ans['bold'] = Face(b)
|
if b.face != n.face:
|
||||||
i = fc_match(family, italic=True)
|
ans[key] = Font(Face(b.face), b.hinting, b.hintstyle, b.bold, b.italic)
|
||||||
if i:
|
do('bold'), do('italic'), do('bi')
|
||||||
ans['italic'] = Face(i)
|
|
||||||
bi = fc_match(family, True, True)
|
|
||||||
if bi:
|
|
||||||
ans['bi'] = Face(bi)
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
def load_font_family(r):
|
def load_font_family(r):
|
||||||
face, raw_name = r
|
return get_font_files(r)
|
||||||
ans = get_font_files(raw_name)
|
|
||||||
ans['regular'] = face
|
|
||||||
return ans
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user