mirror of
https://github.com/gryf/urxvt-wrapper.git
synced 2025-12-17 11:30:21 +01:00
Updated font parsing functions
This commit is contained in:
89
urxvt.py
89
urxvt.py
@@ -18,17 +18,32 @@ SIZE = os.environ.get('URXVT_SIZE', 14)
|
|||||||
FIXED_SIZE = os.environ.get('URXVT_FIXED_SIZE', 16)
|
FIXED_SIZE = os.environ.get('URXVT_FIXED_SIZE', 16)
|
||||||
ICON = os.environ.get('URXVT_ICON', 'tilda.png')
|
ICON = os.environ.get('URXVT_ICON', 'tilda.png')
|
||||||
ICON_PATH = os.environ.get('URXVT_ICON_PATH',
|
ICON_PATH = os.environ.get('URXVT_ICON_PATH',
|
||||||
os.path.expanduser('~/GNUstep/Library/Icons'))
|
os.path.expanduser('~/.urxvt/icons'))
|
||||||
DEFAULT_TTF = os.environ.get('URXVT_TTF', 'DejaVuSansMono Nerd Font Mono')
|
DEFAULT_FONT = os.environ.get('URXVT_TTF', 'DejaVuSansMono Nerd Font Mono')
|
||||||
DEFAULT_BITMAP = os.environ.get('URXVT_BMP', 'Misc Fixed')
|
DEFAULT_BITMAP = os.environ.get('URXVT_BMP', 'Misc Fixed')
|
||||||
|
PERLEXT = os.environ.get('URXVT_PERL_EXT',
|
||||||
|
"url-select,keyboard-select,font-size,color-themes")
|
||||||
|
|
||||||
# Arbitrary added fonts, that provides symbols, icons, emoji (besides those in
|
# Arbitrary added fonts, that provides symbols, icons, emoji (besides those in
|
||||||
# Nerd Font)
|
# Nerd Font)
|
||||||
_ADDITIONAL_FONTS = ['Symbola', 'Unifont Upper', 'DejaVu Sans']
|
_ADDITIONAL_FONTS = ['Symbola', 'Unifont Upper', 'DejaVu Sans']
|
||||||
_REGULAR = ['regular', 'normal', 'book', 'medium', 'bold']
|
_REGULAR = ['regular', 'normal', 'book', 'medium']
|
||||||
_XFT_TEMPLATE = 'xft:%s:style=%s:pixelsize=%d'
|
_XFT_TEMPLATE = 'xft:%s:style=%s:pixelsize=%d'
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_style(styles):
|
||||||
|
for reg_style in _REGULAR:
|
||||||
|
if reg_style in ''.join(styles).lower():
|
||||||
|
for style in styles:
|
||||||
|
if style.lower() == reg_style:
|
||||||
|
return style
|
||||||
|
|
||||||
|
if 'bold' in ''.join(styles).lower():
|
||||||
|
for style in styles:
|
||||||
|
if style.lower() == 'bold':
|
||||||
|
return style
|
||||||
|
|
||||||
|
|
||||||
def _get_all_suitable_fonts():
|
def _get_all_suitable_fonts():
|
||||||
"""
|
"""
|
||||||
Scan all available in the system fonts, where every line have format:
|
Scan all available in the system fonts, where every line have format:
|
||||||
@@ -45,57 +60,63 @@ def _get_all_suitable_fonts():
|
|||||||
|
|
||||||
Return a dictionary of styles associated to the font name, i.e.:
|
Return a dictionary of styles associated to the font name, i.e.:
|
||||||
|
|
||||||
{font_name1: [style],
|
{font_name1: (style),
|
||||||
font_name2: [style1, style2],
|
font_name2: (style1, style2),
|
||||||
font_name3: [style1, style2],
|
font_name3: (style1, style2),
|
||||||
font_name4: [style3, style4]}
|
font_name4: (style3, style4)}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
fonts = collections.defaultdict(list)
|
regular = {}
|
||||||
|
bold = {}
|
||||||
|
|
||||||
out = subprocess.check_output(['fc-list']).decode('utf-8')
|
out = subprocess.check_output(['fc-list']).decode('utf-8')
|
||||||
|
|
||||||
for line in out.split('\n'):
|
for line in out.split('\n'):
|
||||||
if line and ': ' in line and ':style=' in line:
|
if not line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if ': ' not in line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if ':style=' not in line:
|
||||||
|
continue
|
||||||
|
|
||||||
line = line.split(': ')[1]
|
line = line.split(': ')[1]
|
||||||
font_names = line.split(':')[0].split(',')
|
font_names = [n.strip() for n in line.split(':')[0].split(',')]
|
||||||
styles = line.split(':style=')[1].split(',')
|
styles = [s.strip() for s in line.split(':style=')[1].split(',')]
|
||||||
|
|
||||||
|
style = _parse_style(styles)
|
||||||
|
if not style:
|
||||||
|
continue
|
||||||
|
|
||||||
for name in font_names:
|
for name in font_names:
|
||||||
for style in styles:
|
if style.lower() == 'bold' and not bold.get(name):
|
||||||
if style.lower().strip() in _REGULAR:
|
bold[name] = style
|
||||||
fonts[name.strip()].append(style.strip())
|
elif style.lower() != 'bold' and not regular.get(name):
|
||||||
|
regular[name] = style
|
||||||
|
|
||||||
out = {}
|
return {'regular': regular, 'bold': bold}
|
||||||
for key, val in fonts.items():
|
|
||||||
out[key] = list(set(val))
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
_AVAILABLE_FONTS = _get_all_suitable_fonts()
|
_AVAILABLE_FONTS = _get_all_suitable_fonts()
|
||||||
|
|
||||||
|
|
||||||
def _get_style(name, bold=False):
|
def _get_style(name, bold=False):
|
||||||
|
key = 'bold' if bold else 'regular'
|
||||||
try:
|
try:
|
||||||
styles = _AVAILABLE_FONTS[name]
|
return _AVAILABLE_FONTS[key][name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print(f'There is no matching font for name "{name}".')
|
print(f'There is no matching font for name "{name}".')
|
||||||
sys.exit(1)
|
return None
|
||||||
|
|
||||||
for style in styles:
|
|
||||||
if bold and style.lower() == 'bold':
|
|
||||||
return style
|
|
||||||
|
|
||||||
if style.lower() in _REGULAR:
|
|
||||||
return style
|
|
||||||
|
|
||||||
|
|
||||||
def _get_font_list(ff_list, bold=False, bmp_first=False):
|
def _get_font_list(ff_list, size, bold=False):
|
||||||
fonts = []
|
fonts = []
|
||||||
|
|
||||||
for face in ff_list:
|
for face in ff_list:
|
||||||
fonts.append(_XFT_TEMPLATE % (face, _get_style(face), SIZE))
|
style = _get_style(face, bold)
|
||||||
if bmp_first:
|
if not style:
|
||||||
fonts.insert(0, _XFT_TEMPLATE %
|
continue
|
||||||
(DEFAULT_BITMAP, _get_style(DEFAULT_BITMAP), FIXED_SIZE))
|
fonts.append(_XFT_TEMPLATE % (face, _get_style(face, bold), size))
|
||||||
return ','.join(fonts)
|
return ','.join(fonts)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user