[英]How to get X font path from Python script?
From a Python script that is started under X, I want to get the font path. 从X下启动的Python脚本中,我想要获取字体路径。
The script passes the font path to Xvnc
(part of TightVNC and TigerVNC). 该脚本将字体路径传递到
Xvnc
(TightVNC和TigerVNC的一部分)。 Yes, I know, to start a VNC server, one can use startvnc
, which takes care of setting up the font path. 是的,我知道,要启动VNC服务器,可以使用
startvnc
,它负责设置字体路径。 However, startvnc
does some things that are not desired in this case. 但是,在这种情况下,
startvnc
执行某些不需要的操作。 Also, I want to use the exact same font path as on the X server that the script is started on. 另外,我想使用与启动脚本的X服务器上完全相同的字体路径。
I considered parsing the output of xset q
. 我考虑过解析
xset q
的输出。 However: 然而:
I don't know how reliable that is, ie if output is always formatted the same way. 我不知道那是多么可靠,即输出是否总是以相同的方式格式化。
The output may contain placeholders that are not actually paths. 输出中可能包含实际上不是路径的占位符。 For example,
built-ins
in: /usr/share/fonts/misc,/usr/share/fonts/Type1,built-ins
例如,
built-ins
于: /usr/share/fonts/misc,/usr/share/fonts/Type1,built-ins
From reading the manual for xorg.conf, your safest bet would be to parse the configuration file (which I'm assuming setx q
parses) for FontPath
lines, which look like: 通过阅读xorg.conf 手册 ,最安全的选择就是解析
FontPath
行的配置文件(我假设是setx q
解析),如下所示:
FontPath "path" sets the search path for fonts.
FontPath“ path”设置字体的搜索路径。 This path is a comma separated list of font path elements which the Xorg server searches for font databases.
此路径是Xorg服务器搜索字体数据库的字体路径元素的逗号分隔列表。
Using python's re module, the font paths (because multiple FontPath
s can be specified) can be parsed like this: 使用python的re模块,可以按如下方式解析字体路径(因为可以指定多个
FontPath
):
import re
regex = re.compile(r'^\s*FontPath\s+(.*)\s*$', re.MULTILINE)
with open('xorg.conf') as f:
data = f.read()
matches = regex.findall(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.