author | andy_robinson |
Sat, 08 Jul 2000 12:52:25 +0000 | |
changeset 336 | c0fcae4341cf |
parent 320 | 7a4ca2b5bb89 |
child 494 | 54257447cfe9 |
permissions | -rw-r--r-- |
256 | 1 |
#platdemos.py |
2 |
"""This includes some demos of platypus for use in the API proposal""" |
|
3 |
from reportlab.lib import colors |
|
4 |
from reportlab.pdfgen.canvas import Canvas |
|
5 |
from reportlab.lib.styles import ParagraphStyle |
|
6 |
from reportlab.platypus import Frame |
|
7 |
from reportlab.platypus import Flowable |
|
8 |
from reportlab.platypus import Paragraph |
|
9 |
from reportlab.lib.units import inch |
|
10 |
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER |
|
11 |
||
12 |
captionStyle = ParagraphStyle('Caption', |
|
13 |
fontName='Times-Italic', |
|
14 |
fontSize=10, |
|
15 |
alignment=TA_CENTER) |
|
16 |
||
17 |
||
18 |
class Figure(Flowable): |
|
336
c0fcae4341cf
Separated out actual text from genuserguide, so it can be broken into
andy_robinson
parents:
320
diff
changeset
|
19 |
def __init__(self, width, height, caption=""): |
256 | 20 |
Flowable.__init__(self) |
21 |
self.width = width |
|
22 |
self.figureHeight = height |
|
23 |
self.captionHeight = 0 # work out later |
|
336
c0fcae4341cf
Separated out actual text from genuserguide, so it can be broken into
andy_robinson
parents:
320
diff
changeset
|
24 |
self.caption = caption |
320
7a4ca2b5bb89
Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents:
256
diff
changeset
|
25 |
self.captionStyle = ParagraphStyle('Caption', |
7a4ca2b5bb89
Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents:
256
diff
changeset
|
26 |
fontName='Times-Italic', |
7a4ca2b5bb89
Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents:
256
diff
changeset
|
27 |
fontSize=12, |
7a4ca2b5bb89
Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents:
256
diff
changeset
|
28 |
spaceBefore=6, |
7a4ca2b5bb89
Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents:
256
diff
changeset
|
29 |
alignment=TA_CENTER) |
336
c0fcae4341cf
Separated out actual text from genuserguide, so it can be broken into
andy_robinson
parents:
320
diff
changeset
|
30 |
#must build paragraph now to get sequencing in synch |
c0fcae4341cf
Separated out actual text from genuserguide, so it can be broken into
andy_robinson
parents:
320
diff
changeset
|
31 |
#with rest of story |
c0fcae4341cf
Separated out actual text from genuserguide, so it can be broken into
andy_robinson
parents:
320
diff
changeset
|
32 |
self.captionPara = Paragraph(self.caption, self.captionStyle) |
c0fcae4341cf
Separated out actual text from genuserguide, so it can be broken into
andy_robinson
parents:
320
diff
changeset
|
33 |
|
256 | 34 |
self.spaceBefore = 12 |
35 |
self.spaceAfter = 12 |
|
36 |
||
37 |
def wrap(self, availWidth, availHeight): |
|
38 |
# try to get the caption aligned |
|
39 |
(w, h) = self.captionPara.wrap(self.width, availHeight - self.figureHeight) |
|
40 |
self.captionHeight = h |
|
41 |
self.height = self.captionHeight + self.figureHeight |
|
42 |
self.dx = 0.5 * (availWidth - self.width) |
|
43 |
return (self.width, self.height) |
|
44 |
||
45 |
def draw(self): |
|
46 |
self.canv.translate(self.dx, 0) |
|
47 |
self.drawCaption() |
|
48 |
self.canv.translate(0, self.captionHeight) |
|
49 |
self.drawBorder() |
|
50 |
self.drawFigure() |
|
51 |
||
52 |
def drawBorder(self): |
|
53 |
self.canv.rect(0, 0, self.width, self.figureHeight) |
|
54 |
||
55 |
def drawCaption(self): |
|
56 |
self.captionPara.drawOn(self.canv, 0, 0) |
|
57 |
||
58 |
def drawFigure(self): |
|
59 |
pass |
|
60 |
||
61 |
def drawPage(canvas,x, y, width, height): |
|
62 |
#draws something which looks like a page |
|
63 |
pth = canvas.beginPath() |
|
64 |
corner = 0.05*width |
|
65 |
||
66 |
# shaded backdrop offset a little |
|
67 |
canvas.setFillColorRGB(0.5,0.5,0.5) |
|
68 |
canvas.rect(x + corner, y - corner, width, height, stroke=0, fill=1) |
|
69 |
||
70 |
#'sheet of paper' in light yellow |
|
71 |
canvas.setFillColorRGB(1,1,0.9) |
|
72 |
canvas.setLineWidth(0) |
|
73 |
canvas.rect(x, y, width, height, stroke=1, fill=1) |
|
74 |
||
75 |
#reset |
|
76 |
canvas.setFillColorRGB(0,0,0) |
|
77 |
canvas.setStrokeColorRGB(0,0,0) |
|
78 |
||
79 |
||
80 |
||
81 |
class PageFigure(Figure): |
|
82 |
"""Shows a blank page in a frame, and draws on that. Used in |
|
320
7a4ca2b5bb89
Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents:
256
diff
changeset
|
83 |
illustrations of how PLATYPUS works.""" |
256 | 84 |
def __init__(self): |
85 |
Figure.__init__(self, 3*inch, 3*inch) |
|
86 |
self.caption = 'Figure 1 - a blank page' |
|
87 |
self.captionStyle = captionStyle |
|
88 |
||
89 |
def drawVirtualPage(self): |
|
90 |
pass |
|
91 |
||
92 |
def drawFigure(self): |
|
93 |
drawPage(self.canv, 0.625*inch, 0.25*inch, 1.75*inch, 2.5*inch) |
|
94 |
self.canv.translate(0.625*inch, 0.25*inch) |
|
95 |
self.canv.scale(1.75/8.27, 2.5/11.69) |
|
96 |
self.drawVirtualPage() |
|
97 |
||
98 |
class PlatPropFigure1(PageFigure): |
|
99 |
"""This shows a page with a frame on it""" |
|
100 |
def __init__(self): |
|
101 |
PageFigure.__init__(self) |
|
102 |
self.caption = "Figure 1 - a page with a simple frame" |
|
103 |
def drawVirtualPage(self): |
|
104 |
demo1(self.canv) |
|
105 |
||
106 |
||
107 |
def demo1(canvas): |
|
108 |
frame = Frame( |
|
109 |
2*inch, # x |
|
110 |
4*inch, # y at bottom |
|
111 |
4*inch, # width |
|
112 |
5*inch, # height |
|
113 |
showBoundary = 1 # helps us see what's going on |
|
114 |
) |
|
115 |
||
116 |
bodyStyle = ParagraphStyle('Body', |
|
117 |
fontName='Times-Roman', |
|
118 |
fontSize=24, |
|
119 |
leading=28, |
|
120 |
spaceBefore=6) |
|
121 |
||
122 |
para1 = Paragraph('Spam spam spam spam. ' * 5, bodyStyle) |
|
123 |
para2 = Paragraph('Eggs eggs eggs. ' * 5, bodyStyle) |
|
124 |
||
125 |
mydata = [para1, para2] |
|
126 |
||
127 |
#this does the packing and drawing. The frame will consume |
|
128 |
#items from the front of the list as it prints them |
|
129 |
frame.addFromList(mydata,canvas) |
|
130 |
||
131 |
||
132 |
def test1(): |
|
133 |
c = Canvas('platdemos.pdf') |
|
134 |
f = Frame(inch, inch, 6*inch, 9*inch, showBoundary=1) |
|
135 |
v = PlatPropFigure1() |
|
136 |
f.addFromList([v],c) |
|
137 |
c.save() |
|
138 |
||
139 |
if __name__ == '__main__': |
|
140 |
test1() |