Fonts 使用wxFont和fontconfig检索字体路径

Fonts 使用wxFont和fontconfig检索字体路径,fonts,wxpython,fontconfig,Fonts,Wxpython,Fontconfig,我的名字是法比奥,我真的很高兴认识你们的社区! 我在使用wxPython、PIL和fontconfig处理字体时遇到了一个问题。起初,我想使用PIL在tiff图像中写入文本,然后使用ImageFont.truetype将字体加载到变量中。据我所知,truetype方法需要ttf字体的完整路径 我不喜欢硬编码,所以我使用wx.FontDialog类让用户选择字体,并将fontpath保存在一个简单的二进制文件中。问题在于,方法FontDialog.GetFontData()只返回字体面,而不是字体

我的名字是法比奥,我真的很高兴认识你们的社区! 我在使用wxPython、PIL和fontconfig处理字体时遇到了一个问题。起初,我想使用PIL在tiff图像中写入文本,然后使用ImageFont.truetype将字体加载到变量中。据我所知,truetype方法需要ttf字体的完整路径

我不喜欢硬编码,所以我使用wx.FontDialog类让用户选择字体,并将fontpath保存在一个简单的二进制文件中。问题在于,方法FontDialog.GetFontData()只返回字体面,而不是字体路径

解决方案似乎是fontconfig模块:使用fontface,fontconfig.query方法几乎完全返回我需要的内容,我指的是一个包含字体完整路径的列表。然后又出现了另一个问题:我必须在列表中选择一个路径,所以我想过滤列表,选择正确的字体权重和样式

FontDialog返回的属性似乎很混乱,例如,我发现wxnormal可以表示“正常”、“常规”、“中等”、“书本”、“罗马”、“浓缩”、“线条变形”、“哥特式”;wxbold可以用于“粗体”、“半粗体”、“半粗体”、“重体”。很难设置一个好的过滤器,两个类之间的常量也完全不同

综上所述,我想按照这些步骤来获得我的结果:

  • 通过FontDialog(GetFontData()方法)对字体进行处理

  • 通过fontconfig.query()方法检索字体路径列表

  • 筛选与用户选择的字体、大小、重量和样式完全匹配的路径

  • 将字体路径返回到Imagefont.truetype方法

  • 下面你可以阅读我写的一个简短的例子来更好地解释我的意思。它求解第1点和第2点,但在第3点停止。 如有任何建议,我们将不胜感激

    法比奥

    import wx
    import fontconfig
    #os.path.join(os.environ['WINDIR'], 'Fonts')
    
    class TF(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, -1, "", size=(800, 500), pos=(400, 400))
            pan = wx.Panel(self, -1)
            self.lbl = wx.TextCtrl(pan, -1, pos=(0,30), size=(750, 210), style=wx.TE_MULTILINE|wx.TE_AUTO_SCROLL)
            self.txt = wx.TextCtrl(pan, -1, pos=(0,240), size=(750, 250), style=wx.TE_MULTILINE|wx.TE_AUTO_SCROLL)
            self.cmdFontDialog = wx.Button(pan, -1, "A", size=(30,30))
            self.Bind(wx.EVT_BUTTON, self.onFontDialog, self.cmdFontDialog)
    
        def propertiesWX(self, fontData):
            strLbl = ""
            strLbl += "-" * 50 + "\n"
            strLbl += fontData.GetChosenFont().GetFaceName() + "\n"
            strLbl += fontData.GetChosenFont().GetFamilyString() + "\n"
            strLbl += str(fontData.GetChosenFont().GetFamily()) + "\n"
            strLbl += fontData.GetChosenFont().GetWeightString() + "\n"
            self.lbl.SetValue(strLbl)
    
        def propertiesFC(self, family):
            lst = fontconfig.query(family=family)
            obj = []
            for f in lst:
                obj.append(fontconfig.FcFont(f))
            strLbl = ""
    
            for font in obj:            
                strLbl += "*" * 50 + "\n"
                strLbl += "FONT FAMILY: " + font.family[0][1] + "\n"
                strLbl += "FONT FILE: " + font.file + "\n"
                strLbl += "FONT FULLNAME: " + font.fullname[0][1] + "\n"
                strLbl += "FONT FORMAT: " + font.fontformat + "\n"
                strLbl += "FONT STYLE: " + font.style[0][1] + "\n"
                strLbl += "FONT WEIGHT: " + str(font.weight) + "\n"
                strLbl += "SLANT: " + str(font.slant) + "\n"
                strLbl += "CAPABILITY: " + font.capability + "\n"
                strLbl += "WIDTH: " + str(font.width) + "\n"
            self.txt.SetValue(strLbl)
    
        def onFontDialog(self, evt):
            dlg = wx.FontDialog(None, wx.FontData())        
            if dlg.ShowModal() == wx.ID_OK:
                fontData = dlg.GetFontData()
                tf.propertiesWX(fontData)
                tf.propertiesFC(fontData.GetChosenFont().GetFaceName())
    
    if __name__ == '__main__':
        app = wx.PySimpleApp()
        tf = TF()
        tf.Show()
        app.MainLoop()