vilcans / screenplain

Write your screenplay in plain text and run it through this program to make it look good
MIT License
163 stars 29 forks source link

Diacritic characters in generated PDFs? #35

Open mckea opened 7 years ago

mckea commented 7 years ago

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?

vilcans commented 7 years ago

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.

mckea commented 7 years ago

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

vilcans commented 7 years ago

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.

mckea commented 7 years ago

That's great! Thanks!

mckea commented 7 years ago

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

vilcans commented 7 years ago

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

lenormf commented 4 years ago

Any progress on supporting other fonts?

lenormf commented 4 years ago

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?