screenplain icon indicating copy to clipboard operation
screenplain copied to clipboard

Diacritic characters in generated PDFs?

Open mckea opened this issue 8 years ago • 8 comments

I have some scripts that contain some diacritic characters (with circumflexes etc.). These render as a black box in the PDF file. Possible to fix?

mckea avatar Feb 18 '17 21:02 mckea

First make sure that the file is in utf-8 format. It may be a setting in your editor when you save the file.

Also check if those characters work if you convert to HTML instead of PDF, because then we know it's a PDF problem. If so, please send me a file that reproduces the problem so I can have a look at it.

vilcans avatar Feb 19 '17 00:02 vilcans

I'm using vim - encoding and fileencoding are both set to "utf-8". The characters do render correctly in vim.

I have attached a sample file. diacritic.fountain.txt

mckea avatar Feb 19 '17 00:02 mckea

Thanks, I've found the problem. It's that the default Courier font doesn't include those characters. I've started working on support for other fonts.

vilcans avatar Feb 20 '17 00:02 vilcans

That's great! Thanks!

mckea avatar Feb 28 '17 18:02 mckea

FWIW, I've modified my screenplain/export/pdf.py to use FreeMono instead of Courier, and it does render properly for my needs now.

mckea avatar May 06 '17 21:05 mckea

Awesome! I have started the work on supporting different fonts, and possibly using Courier Prime by default.

vilcans avatar May 07 '17 17:05 vilcans

Any progress on supporting other fonts?

lenormf avatar May 12 '20 15:05 lenormf

For the record, here is the patch I used, the font files were installed in ~/.fonts:

diff --git a/screenplain/export/pdf.py b/screenplain/export/pdf.py
index 6955616..86d7a00 100644
--- a/screenplain/export/pdf.py
+++ b/screenplain/export/pdf.py
@@ -23,6 +23,8 @@ from reportlab import platypus
 from reportlab.lib.units import inch
 from reportlab.lib.styles import ParagraphStyle
 from reportlab.lib.enums import TA_CENTER, TA_RIGHT
+from reportlab.pdfbase import pdfmetrics
+from reportlab.pdfbase.ttfonts import TTFont
 
 from screenplain.types import (
     Action, Dialog, DualDialog, Transition, Slug
@@ -46,7 +48,7 @@ bottom_margin = page_height - top_margin - frame_height
 
 default_style = ParagraphStyle(
     'default',
-    fontName='Courier',
+    fontName='Courier Prime',
     fontSize=font_size,
     leading=line_height,
     spaceBefore=0,
@@ -126,7 +128,7 @@ class DocTemplate(BaseDocTemplate):
         )
 
     def handle_pageBegin(self):
-        self.canv.setFont('Courier', font_size, leading=line_height)
+        self.canv.setFont('Courier Prime', font_size, leading=line_height)
         if self.has_title_page:
             page = self.page  # self.page is 0 on first page
         else:
@@ -246,6 +248,11 @@ def to_pdf(
     template_constructor=DocTemplate,
     is_strong=False,
 ):
+    pdfmetrics.registerFont(TTFont('Courier Prime', 'courier-prime.ttf'))
+    pdfmetrics.registerFont(TTFont('Courier Prime Bold', 'courier-prime-bold.ttf'))
+    pdfmetrics.registerFont(TTFont('Courier Prime Italic', 'courier-prime-italic.ttf'))
+    pdfmetrics.registerFont(TTFont('Courier Prime Bold Italic', 'courier-prime-bold-italic.ttf'))
+
     story = get_title_page_story(screenplay)
     has_title_page = bool(story)
 

I could create a PR to allow users to select a font from a configuration file, if needed?

lenormf avatar May 13 '20 16:05 lenormf