forked from jdaly101/kassia
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrop_cap.py
72 lines (62 loc) · 2.66 KB
/
drop_cap.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
from reportlab.lib.geomutils import normalizeTRBL
from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Flowable
class Dropcap(Flowable):
def __init__(self, text: str = None, x_padding: float = 10, style: ParagraphStyle = ParagraphStyle('Dropcap')):
super().__init__()
self.text: str = text
self.x_padding: float = x_padding
self.style: ParagraphStyle = style
self.width: float = pdfmetrics.stringWidth(self.text, self.style.fontName, self.style.fontSize)
ascent, descent = pdfmetrics.getAscentDescent(self.style.fontName, self.style.fontSize)
self.height: float = max(ascent - descent, self.style.leading)
def wrap(self, *args):
return self.width + self.x_padding, self.height
def draw(self, canvas: Canvas = None):
"""This class is overloaded from Flowable's draw function.
:param canvas: The canvas. This only gets passed to draw when called by Score directly.
If a troparion gets split, platypus will treat the dropcap as a Flowable and call draw without
any parameters.
"""
if not canvas:
canvas = self.canv
if not self.text:
return
self._draw_background(canvas)
canvas.setFillColor(self.style.textColor)
canvas.setFont(self.style.fontName, self.style.fontSize)
canvas.drawString(0, 0, self.text)
def _draw_background(self, canvas: Canvas):
"""
Draws a dropcap background. Logic copied from drawPara() method in Paragraph class.
"""
style = self.style
left_indent = style.leftIndent
bw = getattr(style, 'borderWidth', None)
bc = getattr(style, 'borderColor', None)
bg = style.backColor
if bg or (bc and bw):
canvas.saveState()
op = canvas.rect
kwds = dict(fill=0, stroke=0)
if bc and bw:
canvas.setStrokeColor(bc)
canvas.setLineWidth(bw)
kwds['stroke'] = 1
br = getattr(style, 'borderRadius', 0)
if br:
op = canvas.roundRect
kwds['radius'] = br
if bg:
canvas.setFillColor(bg)
kwds['fill'] = 1
bp = getattr(style, 'borderPadding', 0)
tbp, rbp, bbp, lbp = normalizeTRBL(bp)
op(left_indent - lbp,
-bbp,
self.width - (left_indent + style.rightIndent) + lbp + rbp,
self.height + tbp + bbp,
**kwds)
canvas.restoreState()