-
Notifications
You must be signed in to change notification settings - Fork 44
/
pdf_arcs.py
64 lines (53 loc) · 1.5 KB
/
pdf_arcs.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
from numpy import pi
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import letter
from kiva.pdf import GraphicsContext
def draw_ellipse(gc, x, y, major, minor, angle):
""" Draws an ellipse given major and minor axis lengths. **angle** is
the angle between the major axis and the X axis, in radians.
"""
with gc:
gc.translate_ctm(x, y)
ratio = float(major) / minor
gc.rotate_ctm(angle)
gc.scale_ctm(ratio, 1.0)
gc.arc(0, 0, minor, 0.0, 2 * pi)
gc.stroke_path()
gc.move_to(-minor, 0)
gc.line_to(minor, 0)
gc.move_to(0, -minor)
gc.line_to(0, minor)
gc.stroke_path()
def draw_round_rect(gc):
""" Draws a black rect with round corners.
"""
w = 500
h = 500
r = max(1, min(w,h)/10)
gc.set_line_width(2.0)
gc.set_stroke_color((0.0, 0.0, 0.0, 1.0))
gc.move_to(w/3, h/2)
gc.arc_to(w/3, 2*h/3,
w/2, 2*h/3,
r)
gc.arc_to(2*w/3, 2*h/3,
2*w/3, h/2,
r)
gc.arc_to(2*w/3, h/3,
w/2, h/3,
r)
gc.arc_to(w/3, h/3,
w/3, h/2,
r)
gc.line_to(w/3, h/2)
gc.stroke_path()
canvas = Canvas(filename='arcs.pdf', pagesize=letter)
gc = GraphicsContext(canvas)
gc.set_alpha(0.3)
gc.set_stroke_color((1.0,0.0,0.0))
gc.set_fill_color((0.0,1.0,0.0))
gc.rect(95, 95, 10, 10)
gc.fill_path()
draw_ellipse(gc, 100, 100, 35.0, 25.0, pi / 6)
draw_round_rect(gc)
gc.save()