Reportlab无法从新注册的字体中获取粗体或斜体

时间:2018-02-12 12:34:10

标签: python-3.x fonts reportlab

在我的应用程序中,我需要一个可以使用罗马尼亚变音符号的字体,所以我找到了一个包含它们的times.ttf文件,我现在的问题是我不能使用斜体,粗体或粗体字。

这是我的代码:

registerFont(TTFont('Times_new', '/usr/share/fonts/truetype/msttcorefonts/times.ttf', subfontIndex=0))
registerFont(TTFont('Times_new-Bold', '/usr/share/fonts/truetype/msttcorefonts/times.ttf', subfontIndex=1))
registerFont(TTFont('Times_new-Italic', '/usr/share/fonts/truetype/msttcorefonts/times.ttf', subfontIndex=2))
registerFont(TTFont('Times_new-BoldItalic', '/usr/share/fonts/truetype/msttcorefonts/times.ttf', subfontIndex=3))

registerFontFamily('Times_new', normal='Times_new', bold='Times_new-B', italic='Times_new-I',
                       boldItalic='Times_new-BI')


styles.add(ParagraphStyle(name="Times", fontName='Times_new'))
styles.add(ParagraphStyle(name="Times-indent-italic", fontName='Times_new-I'))
styles.add(ParagraphStyle(name="Times-center", fontName='Times_new-BI'))
styles.add(ParagraphStyle(name="Times-BoldItalic", fontName='Times_new-BI'))

例如:

如果我写

p_text = "Hello"
report.append(Paragraph(p_text, styles["Times-indent-italic"]))
report.append(Spacer(1, 5))

它会像这样写:你好,我希望它是这样的:你好。基本上我想要使用的任何字体选项都将使用普通字体。任何想法,我能做什么?

1 个答案:

答案 0 :(得分:1)

您确定要引用正确的文件名吗?在我的系统上,所有变体都在他们自己的TTF文件中:

$ locate times | grep ttf

/usr/local/share/fonts/webfonts/times.ttf
/usr/local/share/fonts/webfonts/timesbd.ttf
/usr/local/share/fonts/webfonts/timesbi.ttf
/usr/local/share/fonts/webfonts/timesi.ttf

我看到你只引用'times.ttf'。也许你的代码应该是:

registerFont(TTFont('Times_new', '/usr/share/fonts/truetype/msttcorefonts/times.ttf', subfontIndex=0))
registerFont(TTFont('Times_new-Bold', '/usr/share/fonts/truetype/msttcorefonts/timesbd.ttf', subfontIndex=1))
registerFont(TTFont('Times_new-Italic', '/usr/share/fonts/truetype/msttcorefonts/timesi.ttf', subfontIndex=2))
registerFont(TTFont('Times_new-BoldItalic', '/usr/share/fonts/truetype/msttcorefonts/timesbi.ttf', subfontIndex=3))