/
pdfOut.py
executable file
·188 lines (145 loc) · 7.27 KB
/
pdfOut.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/python
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.pagesizes import A4
import fparser
from outGeneral import *
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import inch, cm
from reportlab.lib.enums import TA_CENTER, TA_RIGHT
from reportlab.platypus import Paragraph
from reportlab.lib.styles import ParagraphStyle
fontDir = os.path.dirname(os.path.realpath(sys.argv[0])) + "/"
pdfmetrics.registerFont(TTFont('CourierPrime', fontDir+'CourierPrime.ttf'))
pdfmetrics.registerFont(TTFont('CourierPrimeB', fontDir+'CourierPrimeBold.ttf'))
pdfmetrics.registerFont(TTFont('CourierPrimeI', fontDir+'CourierPrimeItalic.ttf'))
pdfmetrics.registerFont(TTFont('CourierPrimeBI', fontDir+'CourierPrimeBoldItalic.ttf'))
pstyle = ParagraphStyle('Normal')
pstyle.textColor = 'black'
pstyle.fontSize = 12
pstyle.fontName = "CourierPrime"
pstyleCenter = ParagraphStyle('Center')
pstyleCenter.textColor = 'black'
pstyleCenter.alignment = TA_CENTER
pstyleCenter.fontSize = 12
pstyleCenter.fontName = "CourierPrime"
pstyleRight = ParagraphStyle('Right')
pstyleRight.textColor = 'black'
pstyleRight.alignment = TA_RIGHT
pstyleRight.fontSize = 12
pstyleRight.fontName = "CourierPrime"
def drawMultiString(canvas, x, y, s):
for ln in s.split('\n'):
canvas.drawString(x, y, ln)
y -= canvas._leading
return y
def drawMultiStringRight(canvas, x, y, s):
for ln in s.split('\n'):
canvas.drawRightString(x, y, ln)
y -= canvas._leading
return y
def drawMultiStringCentred(canvas, x, y, s):
for ln in s.split('\n'):
canvas.drawCentredString(x, y, ln)
y -= canvas._leading
return y
def pdfTitlePage(canvas, parse, page, font):
ypos = 9*inch
if "title" in parse.titlePage:
ypos = drawMultiStringCentred(canvas, page.width*0.5*inch, ypos, "\n".join(parse.titlePage["title"]))
else:
ypos = drawMultiStringCentred(canvas, page.width*0.5*inch, ypos, "Untitled")
ypos = ypos - 2 * font.lineHeight*font.heightEM
if "credit" in parse.titlePage or "authors" in parse.titlePage:
if "credit" in parse.titlePage:
ypos = drawMultiStringCentred(canvas, page.width*0.5*inch, ypos, "\n".join(parse.titlePage["credit"]))
else:
ypos = drawMultiStringCentred(canvas, page.width*0.5*inch, ypos, "written by")
ypos = ypos - 0.5 * font.lineHeight*font.heightEM
if "authors" in parse.titlePage:
ypos = drawMultiStringCentred(canvas, page.width*0.5*inch, ypos, "\n".join(parse.titlePage["authors"]))
else:
ypos = drawMultiStringCentred(canvas, page.width*0.5*inch, ypos, "\n".join(parse.titlePage["Anonymous"]))
ypos = ypos - 2 * font.lineHeight*font.heightEM
if "source" in parse.titlePage:
ypos = drawMultiStringCentred(canvas, page.width*0.5*inch, ypos, "\n".join(parse.titlePage["source"]))
ypos = ypos - 2 * font.lineHeight*font.heightEM
if "draft date" in parse.titlePage:
ypos = drawMultiStringCentred(canvas, page.width*0.5*inch, ypos, "\n".join(parse.titlePage["draft date"]))
ypos = ypos - 2 * font.lineHeight*font.heightEM
if "copyright" in parse.titlePage:
ypos = ypos - 3 * font.lineHeight*font.heightEM
ypos = drawMultiString(canvas, page.marginLeft*inch, ypos, "\n".join(parse.titlePage["copyright"]))
contactPos = 0
if "contact" in parse.titlePage:
contactPos = page.marginBottom*inch + font.lineHeight*font.heightEM * len(parse.titlePage["contact"])
drawMultiString(canvas, page.marginLeft*inch, contactPos, "\n".join(parse.titlePage["contact"]))
if "notes" in parse.titlePage:
y = contactPos + (len(parse.titlePage["notes"])+2)*font.lineHeight*font.heightEM
drawMultiStringRight(canvas, (page.width-page.marginRight) * inch, y, "\n".join(parse.titlePage["notes"]))
def processPDFLine(canvas, type, text, lineno, page, font, width, height):
text = re.sub("("+CHAR_BOLDITALUNDER+")([^/]+)(/"+CHAR_BOLDITALUNDER+")", r"<font name=CourierPrimeBI size=12><u>\2</u></font>", text)
text = re.sub("("+CHAR_BOLDITAL+")([^/]+)(/"+CHAR_BOLDITAL+")", r"<font name=CourierPrimeBI size=12>\2</font>", text)
text = re.sub("("+CHAR_BOLDUNDER+")([^/]+)(/"+CHAR_BOLDUNDER+")", r"<font name=CourierPrimeB size=12><u>\2</u></font>", text)
text = re.sub("("+CHAR_ITALUNDER+")([^/]+)(/"+CHAR_ITALUNDER+")", r"<font name=CourierPrimeI size=12><u>\2</u></font>", text)
text = re.sub("("+CHAR_BOLD+")([^/]+)(/"+CHAR_BOLD+")", r"<font name=CourierPrimeB size=12>\2</font>", text)
text = re.sub("("+CHAR_ITAL+")([^/]+)(/"+CHAR_ITAL+")", r"<font name=CourierPrimeI size=12>\2</font>", text)
text = re.sub("("+CHAR_UNDER+")([^/]+)(/"+CHAR_UNDER+")", r"<u>\2</u>", text)
text = re.sub("("+CHAR_COMMENT+")([^/]+)(/"+CHAR_COMMENT+")", r"</para><para bg=#FAFAD2><u>\2</u></para><para>", text)
elm = fparser.fromTypeTextToElm(type, text)
marginLeft = leftMarginForElement(elm, page)
marginRight = rightMarginForElement(elm, page)
# Styling Additions
if elm.elmType == "Scene Heading":
text = "<font name=CourierPrimeB size=12>" + text + "</font>"
if elm.elmType == "Lyrics":
text = "<font name=CourierPrimeI size=12>" + text + "</font>"
if elm.elmType == "Comment":
text = "</para><para bg=#FAFAD2><u>" + text + "</u></para><para>"
if elm.elmType == "Transition": # right align
para = Paragraph(text, pstyleRight)
para.wrapOn(canvas, width, pstyleRight.fontSize*font.heightEM)
para.drawOn(canvas,-marginRight*inch, height - (lineno+1)*pstyle.fontSize*font.heightEM - page.marginTop*inch)
else:
para = Paragraph(text, pstyle)
para.wrapOn(canvas, width, pstyle.fontSize*font.heightEM)
para.drawOn(canvas, marginLeft*inch, height - (lineno+1)*pstyle.fontSize*font.heightEM - page.marginTop*inch)
def pdfout(parse, outfl, enableComments, font, page):
if page.name == "A4":
c = Canvas(outfl, pagesize=A4)
width, height = A4
else:
c = Canvas(outfl, pagesize=letter)
width, height = letter
c.setFont('CourierPrime', 12)
pgs = splitScriptText(parse, page, font, enableComments)
if len(parse.titlePage) > 0:
pdfTitlePage(c, parse, page, font)
c.showPage()
title = "UNTITLED"
if "title" in parse.titlePage:
title = " ".join(parse.titlePage["title"]).upper()
for pgno in range(0, len(pgs)):
c.setFont('CourierPrime', 12)
if pgno > 0:
c.drawRightString((page.width - page.marginRight) * inch, height - 0.5*inch, str(pgno+1)+".")
c.drawString(page.marginLeft*inch, height - 0.5*inch, title)
lineno = 0
for e in pgs[pgno]:
processPDFLine(c, e[0], e[1], lineno, page, font, width, height)
lineno = lineno + 1
c.showPage()
c.save()
if __name__ == '__main__':
enableComments = False
if "comments" in sys.argv:
enableComments = True
if "A4" in sys.argv or "a4" in sys.argv:
page = Page("A4")
else:
page = Page("Letter")
font = Font()
s = sys.argv[1]
parse = fparser.FParser(open(s, "r", encoding="utf-8").read())
html = pdfout(parse, s+".pdf", enableComments, font, page)