Search code examples
python-3.xfontsreportlab

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


In my applications i need a font that can have romanian diacritics, so i found a times.ttf file that has them, my problem now is that i can't use italic, bold or boldItalic from it.

Here's my code:

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'))

For example:

If i write

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

It will write it just like this: Hello, when i expect it like this: Hello. Basically whatever font option i want to use it will use the normal font. Any ideea what i can do?


Solution

  • Are you sure you're referencing the correct filenames? On my system, all the variants are in their own TTF file:

    $ 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
    

    I see that you only reference 'times.ttf'. Maybe your code should read:

    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))