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

Reportlab can't can't get bold or italic from newly registered font

在我的应用程序中,我需要一种可以包含罗马尼亚变音符号的字体,所以我找到了一个包含它们的 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))

它会这样写:你好,当我期望它是这样的:你好。基本上我想使用的任何字体选项都会使用普通字体。知道我能做什么吗?

您确定引用了正确的文件名吗?在我的系统上,所有变体都在它们自己的 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))