2963
|
1 |
#Copyright ReportLab Europe Ltd. 2000-2004
|
|
2 |
#see license.txt for license details
|
|
3 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/docs/userguide/ch5_paragraphs.py
|
|
4 |
from reportlab.tools.docco.rl_doc_utils import *
|
|
5 |
|
|
6 |
#begin chapter oon paragraphs
|
|
7 |
heading1("Paragraphs")
|
|
8 |
disc("""
|
|
9 |
The $reportlab.platypus.Paragraph$ class is one of the most useful of the Platypus $Flowables$;
|
|
10 |
it can format fairly arbitrary text and provides for inline font style and colour changes using
|
|
11 |
an XML style markup. The overall shape of the formatted text can be justified, right or left ragged
|
|
12 |
or centered. The XML markup can even be used to insert greek characters or to do subscripts.
|
|
13 |
""")
|
|
14 |
disc("""The following text creates an instance of the $Paragraph$ class:""")
|
|
15 |
eg("""Paragraph(text, style, bulletText=None)""")
|
|
16 |
disc("""The $text$ argument contains the text of the
|
|
17 |
paragraph; excess white space is removed from the text at the ends and internally after
|
|
18 |
linefeeds. This allows easy use of indented triple quoted text in <b>Python</b> scripts.
|
|
19 |
The $bulletText$ argument provides the text of a default bullet for the paragraph.
|
|
20 |
The font and other properties for the paragraph text and bullet are set using the style argument.
|
|
21 |
""")
|
|
22 |
disc("""
|
|
23 |
The $style$ argument should be an instance of class $ParagraphStyle$ obtained typically
|
|
24 |
using""")
|
|
25 |
eg("""
|
|
26 |
from reportlab.lib.styles import ParagraphStyle
|
|
27 |
""")
|
|
28 |
disc("""
|
|
29 |
this container class provides for the setting of multiple default paragraph attributes
|
|
30 |
in a structured way. The styles are arranged in a dictionary style object called a $stylesheet$
|
|
31 |
which allows for the styles to be accessed as $stylesheet['BodyText']$. A sample style
|
|
32 |
sheet is provided.
|
|
33 |
""")
|
|
34 |
eg("""
|
|
35 |
from reportlab.lib.styles import getSampleStyleSheet
|
|
36 |
stylesheet=getSampleStyleSheet()
|
|
37 |
normalStyle = stylesheet['Normal']
|
|
38 |
""")
|
|
39 |
disc("""
|
|
40 |
The options which can be set for a $Paragraph$ can be seen from the $ParagraphStyle$ defaults.
|
|
41 |
""")
|
|
42 |
heading4("$class ParagraphStyle$")
|
|
43 |
eg("""
|
|
44 |
class ParagraphStyle(PropertySet):
|
|
45 |
defaults = {
|
|
46 |
'fontName':'Times-Roman',
|
|
47 |
'fontSize':10,
|
|
48 |
'leading':12,
|
|
49 |
'leftIndent':0,
|
|
50 |
'rightIndent':0,
|
|
51 |
'firstLineIndent':0,
|
|
52 |
'alignment':TA_LEFT,
|
|
53 |
'spaceBefore':0,
|
|
54 |
'spaceAfter':0,
|
|
55 |
'bulletFontName':'Times-Roman',
|
|
56 |
'bulletFontSize':10,
|
|
57 |
'bulletIndent':0,
|
|
58 |
'textColor': black,
|
|
59 |
'backColor':None,
|
|
60 |
'wordWrap':None,
|
|
61 |
'borderWidth': 0,
|
|
62 |
'borderPadding': 0,
|
|
63 |
'borderColor': None,
|
|
64 |
'borderRadius': None,
|
|
65 |
'allowWidows': 1,
|
|
66 |
'allowOrphans': 0,
|
|
67 |
}
|
|
68 |
""")
|
|
69 |
|
|
70 |
heading2("Using Paragraph Styles")
|
|
71 |
|
|
72 |
#this will be used in the ParaBox demos.
|
|
73 |
sample = """You are hereby charged that on the 28th day of May, 1970, you did
|
|
74 |
willfully, unlawfully, and with malice of forethought, publish an
|
|
75 |
alleged English-Hungarian phrase book with intent to cause a breach
|
|
76 |
of the peace. How do you plead?"""
|
|
77 |
|
|
78 |
|
|
79 |
disc("""The $Paragraph$ and $ParagraphStyle$ classes together
|
|
80 |
handle most common formatting needs. The following examples
|
|
81 |
draw paragraphs in various styles, and add a bounding box
|
|
82 |
so that you can see exactly what space is taken up.""")
|
|
83 |
|
|
84 |
s1 = ParagraphStyle('Normal')
|
|
85 |
parabox(sample, s1, 'The default $ParagraphStyle$')
|
|
86 |
|
|
87 |
disc("""The two attributes $spaceBefore$ and $spaceAfter$ do what they
|
|
88 |
say, except at the top or bottom of a frame. At the top of a frame,
|
|
89 |
$spaceBefore$ is ignored, and at the bottom, $spaceAfter$ is ignored.
|
|
90 |
This means that you could specify that a 'Heading2' style had two
|
|
91 |
inches of space before when it occurs in mid-page, but will not
|
|
92 |
get acres of whitespace at the top of a page. These two attributes
|
|
93 |
should be thought of as 'requests' to the Frame and are not part
|
|
94 |
of the space occupied by the Paragraph itself.""")
|
|
95 |
|
|
96 |
disc("""The $fontSize$ and $fontName$ tags are obvious, but it is
|
|
97 |
important to set the $leading$. This is the spacing between
|
|
98 |
adjacent lines of text; a good rule of thumb is to make this
|
|
99 |
20% larger than the point size. To get double-spaced text,
|
|
100 |
use a high $leading$. If you set $autoLeading$(default $"off"$) to $"min"$(use observed leading even if smaller than specified) or $"max"$(use the larger of observed and specified) then an attempt is made to determine the leading
|
|
101 |
on a line by line basis. This may be useful if the lines contain different font sizes etc.""")
|
|
102 |
|
|
103 |
disc("""The figure below shows space before and after and an
|
|
104 |
increased leading:""")
|
|
105 |
|
|
106 |
parabox(sample,
|
|
107 |
ParagraphStyle('Spaced',
|
|
108 |
spaceBefore=6,
|
|
109 |
spaceAfter=6,
|
|
110 |
leading=16),
|
|
111 |
'Space before and after and increased leading'
|
|
112 |
)
|
|
113 |
|
|
114 |
disc("""The $leftIndent$ and $rightIndent$ attributes do exactly
|
|
115 |
what you would expect; $firstLineIndent$ is added to the $leftIndent$ of the
|
|
116 |
first line. If you want a straight left edge, remember
|
|
117 |
to set $firstLineIndent$ equal to 0.""")
|
|
118 |
|
|
119 |
parabox(sample,
|
|
120 |
ParagraphStyle('indented',
|
|
121 |
firstLineIndent=+24,
|
|
122 |
leftIndent=24,
|
|
123 |
rightIndent=24),
|
|
124 |
'one third inch indents at left and right, two thirds on first line'
|
|
125 |
)
|
|
126 |
|
|
127 |
disc("""Setting $firstLineIndent$ equal to a negative number, $leftIndent$
|
|
128 |
much higher, and using a
|
|
129 |
different font (we'll show you how later!) can give you a
|
|
130 |
definition list:.""")
|
|
131 |
|
|
132 |
parabox('<b><i>Judge Pickles: </i></b>' + sample,
|
|
133 |
ParagraphStyle('dl',
|
|
134 |
leftIndent=36),
|
|
135 |
'Definition Lists'
|
|
136 |
)
|
|
137 |
|
|
138 |
disc("""There are four possible values of $alignment$, defined as
|
|
139 |
constants in the module <i>reportlab.lib.enums</i>. These are
|
|
140 |
TA_LEFT, TA_CENTER or TA_CENTRE, TA_RIGHT and
|
|
141 |
TA_JUSTIFY, with values of 0, 1, 2 and 4 respectively. These
|
|
142 |
do exactly what you would expect.""")
|
|
143 |
|
|
144 |
disc("""Set $wordWrap$ to $'CJK'$ to get Asian language linewrapping. For normal western text you can change the way
|
|
145 |
the line breaking algorithm handles <i>widows</i> and <i>orphans</i> with the $allowWidows$ and $allowOrphans$ values.
|
|
146 |
Both should normally be set to $0$, but for historical reasons we have allowed <i>widows</i>.
|
|
147 |
The default color of the text can be set with $textColor$ and the paragraph background
|
|
148 |
colour can be set with $backColor$. The paragraph's border properties may be changed using
|
|
149 |
$borderWidth$, $borderPadding$, $borderColor$ and $borderRadius$.""")
|
|
150 |
|
|
151 |
|
|
152 |
heading2("Paragraph XML Markup Tags")
|
|
153 |
disc("""XML markup can be used to modify or specify the
|
|
154 |
overall paragraph style, and also to specify intra-
|
|
155 |
paragraph markup.""")
|
|
156 |
|
|
157 |
heading3("The outermost < para > tag")
|
|
158 |
|
|
159 |
|
|
160 |
disc("""
|
|
161 |
The paragraph text may optionally be surrounded by
|
|
162 |
<para attributes....>
|
|
163 |
</para>
|
|
164 |
tags. The attributes if any of the opening <para> tag affect the style that is used
|
|
165 |
with the $Paragraph$ $text$ and/or $bulletText$.
|
|
166 |
""")
|
|
167 |
disc(" ")
|
|
168 |
|
|
169 |
from reportlab.platypus.paraparser import _addAttributeNames, _paraAttrMap, _bulletAttrMap
|
|
170 |
|
|
171 |
def getAttrs(A):
|
|
172 |
_addAttributeNames(A)
|
|
173 |
S={}
|
|
174 |
for k, v in A.items():
|
|
175 |
a = v[0]
|
|
176 |
if not S.has_key(a):
|
|
177 |
S[a] = k
|
|
178 |
else:
|
|
179 |
S[a] = "%s, %s" %(S[a],k)
|
|
180 |
|
|
181 |
K = S.keys()
|
|
182 |
K.sort()
|
|
183 |
D=[('Attribute','Synonyms')]
|
|
184 |
for k in K:
|
|
185 |
D.append((k,S[k]))
|
|
186 |
cols=2*[None]
|
|
187 |
rows=len(D)*[None]
|
|
188 |
return D,cols,rows
|
|
189 |
|
|
190 |
t=apply(Table,getAttrs(_paraAttrMap))
|
|
191 |
t.setStyle(TableStyle([
|
|
192 |
('FONT',(0,0),(-1,1),'Times-Bold',10,12),
|
|
193 |
('FONT',(0,1),(-1,-1),'Courier',8,8),
|
|
194 |
('VALIGN',(0,0),(-1,-1),'MIDDLE'),
|
|
195 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
|
|
196 |
('BOX', (0,0), (-1,-1), 0.25, colors.black),
|
|
197 |
]))
|
|
198 |
getStory().append(t)
|
|
199 |
caption("""Table <seq template="%(Chapter)s-%(Table+)s"/> - Synonyms for style attributes""")
|
|
200 |
|
|
201 |
disc("""Some useful synonyms have been provided for our Python attribute
|
|
202 |
names, including lowercase versions, and the equivalent properties
|
|
203 |
from the HTML standard where they exist. These additions make
|
|
204 |
it much easier to build XML-printing applications, since
|
|
205 |
much intra-paragraph markup may not need translating. The
|
|
206 |
table below shows the allowed attributes and synonyms in the
|
|
207 |
outermost paragraph tag.""")
|
|
208 |
|
|
209 |
|
|
210 |
heading2("Intra-paragraph markup")
|
|
211 |
disc("""'<![CDATA[Within each paragraph, we use a basic set of XML tags
|
|
212 |
to provide markup. The most basic of these are bold (<b>...</b>),
|
|
213 |
italic (<i>...</i>) and underline (<u>...</u>).
|
|
214 |
Other tags which are allowed are strong (<strong>...</strong>), and strike through (<strike>...</strike>). The <link> and <a> tags
|
|
215 |
may be used to refer to URIs, documents or bookmarks in the current document. The a variant of the <a> tag can be used to
|
|
216 |
mark a position in a document.
|
|
217 |
A break (<br/>) tag is also allowed.]]>
|
|
218 |
""")
|
|
219 |
|
|
220 |
parabox2("""<b>You are hereby charged</b> that on the 28th day of May, 1970, you did
|
|
221 |
willfully, unlawfully, and <i>with malice of forethought</i>, publish an
|
|
222 |
alleged English-Hungarian phrase book with intent to cause a breach
|
|
223 |
of the peace. <u>How do you plead</u>?""", "Simple bold and italic tags")
|
|
224 |
parabox2("""This <a href="#MYANCHOR" color="blue">is a link to</a> an anchor tag ie <a name="MYANCHOR"/><font color="green">here</font>.
|
|
225 |
This <link href="#MYANCHOR" color="blue" fontName="Helvetica">is another link to</link> the same anchor tag.""",
|
|
226 |
"anchors and links")
|
|
227 |
disc("""The <b>link</b> tag can be used as a reference, but
|
|
228 |
not as an anchor. The a and link hyperlink tags have additional attributes <i>fontName</i>,
|
|
229 |
<i>fontSize</i>, <i>color</i> & <i>backColor</i> attributes.
|
|
230 |
The hyperlink reference can have a scheme of <b>http:</b><i>(external webpage)</i>, <b>pdf:</b><i>(different pdf document)</i> or
|
|
231 |
<b>document:</b><i>(same pdf document)</i>; a missing scheme is treated as <b>document</b> as is the case when the reference starts with # (in which case the anchor should omit it). Any other scheme is treated as some kind of URI.
|
|
232 |
""")
|
|
233 |
|
|
234 |
parabox2("""<strong>You are hereby charged</strong> that on the 28th day of May, 1970, you did
|
|
235 |
willfully, unlawfully, <strike>and with malice of forethought</strike>, <br/>publish an
|
|
236 |
alleged English-Hungarian phrase book with intent to cause a breach
|
|
237 |
of the peace. How do you plead?""", "Strong, strike, and break tags")
|
|
238 |
|
|
239 |
heading3("The $<font>$ tag")
|
|
240 |
disc("""The $<font>$ tag can be used to change the font name,
|
|
241 |
size and text color for any substring within the paragraph.
|
|
242 |
Legal attributes are $size$, $face$, $name$ (which is the same as $face$),
|
|
243 |
$color$, and $fg$ (which is the same as $color$). The $name$ is
|
|
244 |
the font family name, without any 'bold' or 'italic' suffixes.
|
|
245 |
Colors may be
|
|
246 |
HTML color names or a hex string encoded in a variety of ways;
|
|
247 |
see ^reportlab.lib.colors^ for the formats allowed.""")
|
|
248 |
|
|
249 |
parabox2("""<font face="times" color="red">You are hereby charged</font> that on the 28th day of May, 1970, you did
|
|
250 |
willfully, unlawfully, and <font size=14>with malice of forethought</font>,
|
|
251 |
publish an
|
|
252 |
alleged English-Hungarian phrase book with intent to cause a breach
|
|
253 |
of the peace. How do you plead?""", "The $font$ tag")
|
|
254 |
|
|
255 |
heading3("Superscripts and Subscripts")
|
|
256 |
disc("""Superscripts and subscripts are supported with the
|
|
257 |
<![CDATA[<super> and <sub> tags, which work exactly
|
|
258 |
as you might expect. In addition, most greek letters
|
|
259 |
can be accessed by using the <greek></greek>
|
|
260 |
tag, or with mathML entity names.]]>""")
|
|
261 |
|
|
262 |
##parabox2("""<greek>epsilon</greek><super><greek>iota</greek>
|
|
263 |
##<greek>pi</greek></super> = -1""", "Greek letters and subscripts")
|
|
264 |
|
|
265 |
parabox2("""Equation (α): <greek>e</greek> <super><greek>ip</greek></super> = -1""",
|
|
266 |
"Greek letters and superscripts")
|
|
267 |
|
|
268 |
heading3("Inline Images")
|
|
269 |
disc("""We can embed images in a paragraph with the
|
|
270 |
<img/> tag which has attributes $src$, $width$, $height$ whose meanings are obvious. The $valign$ attribute may be set to a css like value from
|
|
271 |
"baseline", "sub", "super", "top", "text-top", "middle", "bottom", "text-bottom"; the value may also be a numeric percentage or an absolute value.
|
|
272 |
""")
|
|
273 |
parabox2("""<para autoLeading="off" fontSize=12>This <img/> <img src="../images/testimg.gif" valign="top"/> is aligned <b>top</b>.<br/><br/>
|
|
274 |
This <img/> <img src="../images/testimg.gif" valign="bottom"/> is aligned <b>bottom</b>.<br/><br/>
|
|
275 |
This <img/> <img src="../images/testimg.gif" valign="middle"/> is aligned <b>middle</b>.<br/><br/>
|
|
276 |
This <img/> <img src="../images/testimg.gif" valign="-4"/> is aligned <b>-4</b>.<br/><br/>
|
|
277 |
This <img/> <img src="../images/testimg.gif" valign="+4"/> is aligned <b>+4</b>.<br/><br/>
|
|
278 |
This <img/> <img src="../images/testimg.gif" width="10"/> has width <b>10</b>.<br/><br/>
|
|
279 |
</para>""","Inline images")
|
|
280 |
|
|
281 |
heading3("Numbering Paragraphs and Lists")
|
|
282 |
disc("""The $<seq>$ tag provides comprehensive support
|
|
283 |
for numbering lists, chapter headings and so on. It acts as
|
|
284 |
an interface to the $Sequencer$ class in ^reportlab.lib.sequencer^.
|
|
285 |
These are used to number headings and figures throughout this
|
|
286 |
document.
|
|
287 |
You may create as many separate 'counters' as you wish, accessed
|
|
288 |
with the $id$ attribute; these will be incremented by one each
|
|
289 |
time they are accessed. The $seqreset$ tag resets a counter.
|
|
290 |
If you want it to resume from a number other than 1, use
|
|
291 |
the syntax <seqreset id="mycounter" base="42">.
|
|
292 |
Let's have a go:""")
|
|
293 |
|
|
294 |
parabox2("""<seq id="spam"/>, <seq id="spam"/>, <seq id="spam"/>.
|
|
295 |
Reset<seqreset id="spam"/>. <seq id="spam"/>, <seq id="spam"/>,
|
|
296 |
<seq id="spam"/>.""", "Basic sequences")
|
|
297 |
|
|
298 |
disc("""You can save specifying an ID by designating a counter ID
|
|
299 |
as the <i>default</i> using the <seqdefault id="Counter">
|
|
300 |
tag; it will then be used whenever a counter ID
|
|
301 |
is not specified. This saves some typing, especially when
|
|
302 |
doing multi-level lists; you just change counter ID when
|
|
303 |
stepping in or out a level.""")
|
|
304 |
|
|
305 |
parabox2("""<seqdefault id="spam"/>Continued... <seq/>,
|
|
306 |
<seq/>, <seq/>, <seq/>, <seq/>, <seq/>, <seq/>.""",
|
|
307 |
"The default sequence")
|
|
308 |
|
|
309 |
disc("""Finally, one can access multi-level sequences using
|
|
310 |
a variation of Python string formatting and the $template$
|
|
311 |
attribute in a <seq> tags. This is used to do the
|
|
312 |
captions in all of the figures, as well as the level two
|
|
313 |
headings. The substring $%(counter)s$ extracts the current
|
|
314 |
value of a counter without incrementing it; appending a
|
|
315 |
plus sign as in $%(counter)s$ increments the counter.
|
|
316 |
The figure captions use a pattern like the one below:""")
|
|
317 |
|
|
318 |
parabox2("""Figure <seq template="%(Chapter)s-%(FigureNo+)s"/> - Multi-level templates""",
|
|
319 |
"Multi-level templates")
|
|
320 |
|
|
321 |
disc("""We cheated a little - the real document used 'Figure',
|
|
322 |
but the text above uses 'FigureNo' - otherwise we would have
|
|
323 |
messed up our numbering!""")
|
|
324 |
|
|
325 |
heading2("Bullets and Paragraph Numbering")
|
|
326 |
disc("""In addition to the three indent properties, some other
|
|
327 |
parameters are needed to correctly handle bulleted and numbered
|
|
328 |
lists. We discuss this here because you have now seen how
|
|
329 |
to handle numbering. A paragraph may have an optional
|
|
330 |
^bulletText^ argument passed to its constructor; alternatively,
|
|
331 |
bullet text may be placed in a $<![CDATA[<bullet>..</bullet>]]>$
|
|
332 |
tag at its head. The text will be drawn on the first line of
|
|
333 |
the paragraph, with its x origin determined by the $bulletIndent$
|
|
334 |
attribute of the style, and in the font given in the
|
|
335 |
$bulletFontName$ attribute. For genuine bullets, a good
|
|
336 |
idea is to select the Times-Roman font in the style, and
|
|
337 |
use a character such as $\\xe2\\x80\\xa2)$:""")
|
|
338 |
|
|
339 |
t=apply(Table,getAttrs(_bulletAttrMap))
|
|
340 |
t.setStyle([
|
|
341 |
('FONT',(0,0),(-1,1),'Times-Bold',10,12),
|
|
342 |
('FONT',(0,1),(-1,-1),'Courier',8,8),
|
|
343 |
('VALIGN',(0,0),(-1,-1),'MIDDLE'),
|
|
344 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
|
|
345 |
('BOX', (0,0), (-1,-1), 0.25, colors.black),
|
|
346 |
])
|
|
347 |
getStory().append(t)
|
|
348 |
|
|
349 |
caption("""Table <seq template="%(Chapter)s-%(Table+)s"/> - <bullet> attributes & synonyms""")
|
|
350 |
disc("""The <bullet> tag is only allowed once in a given paragraph and its use
|
|
351 |
overrides the implied bullet style and ^bulletText^ specified in the ^Paragraph^
|
|
352 |
creation.
|
|
353 |
""")
|
|
354 |
parabox("""<bullet>\xe2\x80\xa2</bullet>this is a bullet point. Spam
|
|
355 |
spam spam spam spam spam spam spam spam spam spam spam
|
|
356 |
spam spam spam spam spam spam spam spam spam spam """,
|
|
357 |
styleSheet['Bullet'],
|
|
358 |
'Basic use of bullet points')
|
|
359 |
|
|
360 |
disc("""Exactly the same technique is used for numbers,
|
|
361 |
except that a sequence tag is used. It is also possible
|
|
362 |
to put a multi-character string in the bullet; with a deep
|
|
363 |
indent and bold bullet font, you can make a compact
|
|
364 |
definition list.""")
|