author | rptlab |
Tue, 30 Apr 2013 14:28:14 +0100 | |
branch | py33 |
changeset 3723 | 99aa837b6703 |
parent 3721 | 0c93dd8ff567 |
child 3738 | 6e5e6d64ab2d |
permissions | -rw-r--r-- |
3617 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2012 |
494 | 2 |
#see license.txt for license details |
2332 | 3 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/styles.py |
4 |
__version__=''' $Id$ ''' |
|
3029 | 5 |
__doc__='''Classes for ParagraphStyle and similar things. |
125 | 6 |
|
3029 | 7 |
A style is a collection of attributes, but with some extra features |
8 |
to allow 'inheritance' from a parent, and to ensure nobody makes |
|
9 |
changes after construction. |
|
10 |
||
11 |
ParagraphStyle shows all the attributes available for formatting |
|
12 |
paragraphs. |
|
13 |
||
14 |
getSampleStyleSheet() returns a stylesheet you can use for initial |
|
15 |
development, with a few basic heading and text styles. |
|
16 |
''' |
|
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
17 |
__all__=( |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
18 |
'PropertySet', |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
19 |
'ParagraphStyle', |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
20 |
'LineStyle', |
3479 | 21 |
'ListStyle', |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
22 |
'StyleSheet1', |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
23 |
'getSampleStyleSheet', |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
24 |
) |
128 | 25 |
from reportlab.lib.colors import white, black |
530 | 26 |
from reportlab.lib.enums import TA_LEFT, TA_CENTER |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
27 |
from reportlab.lib.fonts import tt2ps |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
28 |
from reportlab.rl_config import canvas_basefontname as _baseFontName |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
29 |
_baseFontNameB = tt2ps(_baseFontName,1,0) |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
30 |
_baseFontNameI = tt2ps(_baseFontName,0,1) |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
31 |
_baseFontNameBI = tt2ps(_baseFontName,1,1) |
125 | 32 |
|
33 |
########################################################### |
|
34 |
# This class provides an 'instance inheritance' |
|
35 |
# mechanism for its descendants, simpler than acquisition |
|
36 |
# but not as far-reaching |
|
37 |
########################################################### |
|
38 |
class PropertySet: |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
39 |
defaults = {} |
125 | 40 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
41 |
def __init__(self, name, parent=None, **kw): |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
42 |
"""When initialized, it copies the class defaults; |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
43 |
then takes a copy of the attributes of the parent |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
44 |
if any. All the work is done in init - styles |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
45 |
should cost little to use at runtime.""" |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
46 |
# step one - validate the hell out of it |
3326 | 47 |
assert 'name' not in self.defaults, "Class Defaults may not contain a 'name' attribute" |
48 |
assert 'parent' not in self.defaults, "Class Defaults may not contain a 'parent' attribute" |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
49 |
if parent: |
3506 | 50 |
assert parent.__class__ == self.__class__, "Parent style %s must have same class as new style %s" % (parent.__class__.__name__,self.__class__.__name__) |
125 | 51 |
|
1683 | 52 |
#step two |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
53 |
self.name = name |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
54 |
self.parent = parent |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
55 |
self.__dict__.update(self.defaults) |
125 | 56 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
57 |
#step two - copy from parent if any. Try to be |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
58 |
# very strict that only keys in class defaults are |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
59 |
# allowed, so they cannot inherit |
224 | 60 |
self.refresh() |
3479 | 61 |
self._setKwds(**kw) |
1683 | 62 |
|
3479 | 63 |
def _setKwds(self,**kw): |
1683 | 64 |
#step three - copy keywords if any |
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
65 |
for key, value in kw.items(): |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
66 |
self.__dict__[key] = value |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
67 |
|
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
68 |
def __repr__(self): |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
69 |
return "<%s '%s'>" % (self.__class__.__name__, self.name) |
125 | 70 |
|
224 | 71 |
def refresh(self): |
72 |
"""re-fetches attributes from the parent on demand; |
|
73 |
use if you have been hacking the styles. This is |
|
74 |
used by __init__""" |
|
75 |
if self.parent: |
|
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
76 |
for key, value in self.parent.__dict__.items(): |
224 | 77 |
if (key not in ['name','parent']): |
78 |
self.__dict__[key] = value |
|
79 |
||
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
80 |
def listAttrs(self, indent=''): |
3721 | 81 |
print(indent + 'name =', self.name) |
82 |
print(indent + 'parent =', self.parent) |
|
83 |
keylist = list(self.__dict__.keys()) |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
84 |
keylist.sort() |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
85 |
keylist.remove('name') |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
86 |
keylist.remove('parent') |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
87 |
for key in keylist: |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
88 |
value = self.__dict__.get(key, None) |
3721 | 89 |
print(indent + '%s = %s' % (key, value)) |
1683 | 90 |
|
3479 | 91 |
def clone(self, name, parent=None, **kwds): |
92 |
r = self.__class__(name,parent) |
|
93 |
r.__dict__ = self.__dict__.copy() |
|
94 |
r.parent = parent is None and self or parent |
|
95 |
r._setKwds(**kwds) |
|
96 |
return r |
|
97 |
||
125 | 98 |
class ParagraphStyle(PropertySet): |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
99 |
defaults = { |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
100 |
'fontName':_baseFontName, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
101 |
'fontSize':10, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
102 |
'leading':12, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
103 |
'leftIndent':0, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
104 |
'rightIndent':0, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
105 |
'firstLineIndent':0, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
106 |
'alignment':TA_LEFT, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
107 |
'spaceBefore':0, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
108 |
'spaceAfter':0, |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
109 |
'bulletFontName':_baseFontName, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
110 |
'bulletFontSize':10, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
111 |
'bulletIndent':0, |
3540 | 112 |
#'bulletColor':black, |
532 | 113 |
'textColor': black, |
2575 | 114 |
'backColor':None, |
115 |
'wordWrap':None, |
|
2921 | 116 |
'borderWidth': 0, |
117 |
'borderPadding': 0, |
|
118 |
'borderColor': None, |
|
119 |
'borderRadius': None, |
|
120 |
'allowWidows': 1, |
|
121 |
'allowOrphans': 0, |
|
2932
bcdff7b8f469
platypus: initial try at textTransform style attribute
rgbecker
parents:
2921
diff
changeset
|
122 |
'textTransform':None, #uppercase lowercase (captitalize not yet) or None or absent |
3575
6821eb805106
reportlab: added support for endDots in paragraphs styles
rgbecker
parents:
3567
diff
changeset
|
123 |
'endDots':None, #dots on the last line of left/right justified paras |
6821eb805106
reportlab: added support for endDots in paragraphs styles
rgbecker
parents:
3567
diff
changeset
|
124 |
#string or object with text and optional fontName, fontSize, textColor & backColor |
6821eb805106
reportlab: added support for endDots in paragraphs styles
rgbecker
parents:
3567
diff
changeset
|
125 |
#dy |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
126 |
} |
125 | 127 |
|
128 |
class LineStyle(PropertySet): |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
129 |
defaults = { |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
130 |
'width':1, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
131 |
'color': black |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
132 |
} |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
133 |
def prepareCanvas(self, canvas): |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
134 |
"""You can ask a LineStyle to set up the canvas for drawing |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
135 |
the lines.""" |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
136 |
canvas.setLineWidth(1) |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
137 |
#etc. etc. |
125 | 138 |
|
3479 | 139 |
class ListStyle(PropertySet): |
140 |
defaults = dict( |
|
141 |
leftIndent=18, |
|
142 |
rightIndent=0, |
|
143 |
bulletAlign='left', |
|
144 |
bulletType='1', |
|
3502 | 145 |
bulletColor=black, |
3479 | 146 |
bulletFontName='Helvetica', |
147 |
bulletFontSize=12, |
|
148 |
bulletOffsetY=0, |
|
149 |
bulletDedent='auto', |
|
150 |
bulletDir='ltr', |
|
151 |
bulletFormat=None, |
|
3567 | 152 |
start=None, #starting value for a list |
3479 | 153 |
) |
154 |
||
3433
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
155 |
_stylesheet1_undefined = object() |
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
156 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
157 |
class StyleSheet1: |
3028
082f5208644e
docstring modifications to adhere to restructuredtext
damian
parents:
2964
diff
changeset
|
158 |
""" |
082f5208644e
docstring modifications to adhere to restructuredtext
damian
parents:
2964
diff
changeset
|
159 |
This may or may not be used. The idea is to: |
082f5208644e
docstring modifications to adhere to restructuredtext
damian
parents:
2964
diff
changeset
|
160 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
161 |
1. slightly simplify construction of stylesheets; |
3028
082f5208644e
docstring modifications to adhere to restructuredtext
damian
parents:
2964
diff
changeset
|
162 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
163 |
2. enforce rules to validate styles when added |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
164 |
(e.g. we may choose to disallow having both |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
165 |
'heading1' and 'Heading1' - actual rules are |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
166 |
open to discussion); |
3028
082f5208644e
docstring modifications to adhere to restructuredtext
damian
parents:
2964
diff
changeset
|
167 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
168 |
3. allow aliases and alternate style lookup |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
169 |
mechanisms |
3028
082f5208644e
docstring modifications to adhere to restructuredtext
damian
parents:
2964
diff
changeset
|
170 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
171 |
4. Have a place to hang style-manipulation |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
172 |
methods (save, load, maybe support a GUI |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
173 |
editor) |
3028
082f5208644e
docstring modifications to adhere to restructuredtext
damian
parents:
2964
diff
changeset
|
174 |
|
082f5208644e
docstring modifications to adhere to restructuredtext
damian
parents:
2964
diff
changeset
|
175 |
Access is via getitem, so they can be |
082f5208644e
docstring modifications to adhere to restructuredtext
damian
parents:
2964
diff
changeset
|
176 |
compatible with plain old dictionaries. |
082f5208644e
docstring modifications to adhere to restructuredtext
damian
parents:
2964
diff
changeset
|
177 |
""" |
3433
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
178 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
179 |
def __init__(self): |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
180 |
self.byName = {} |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
181 |
self.byAlias = {} |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
182 |
|
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
183 |
def __getitem__(self, key): |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
184 |
try: |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
185 |
return self.byAlias[key] |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
186 |
except KeyError: |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
187 |
try: |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
188 |
return self.byName[key] |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
189 |
except KeyError: |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
190 |
raise KeyError("Style '%s' not found in stylesheet" % key) |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
191 |
|
3433
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
192 |
def get(self,key,default=_stylesheet1_undefined): |
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
193 |
try: |
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
194 |
return self[key] |
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
195 |
except KeyError: |
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
196 |
if default!=_stylesheet1_undefined: return default |
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
197 |
raise |
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
198 |
|
3326 | 199 |
def __contains__(self, key): |
200 |
return key in self.byAlias or key in self.byName |
|
460 | 201 |
|
3433
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
202 |
def has_key(self,key): |
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
203 |
return key in self |
0562ca2f5155
styles.py: new Stylesheet1 methods suggested by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents:
3368
diff
changeset
|
204 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
205 |
def add(self, style, alias=None): |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
206 |
key = style.name |
3326 | 207 |
if key in self.byName: |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
208 |
raise KeyError("Style '%s' already defined in stylesheet" % key) |
3326 | 209 |
if key in self.byAlias: |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
210 |
raise KeyError("Style name '%s' is already an alias in stylesheet" % key) |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
211 |
|
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
212 |
if alias: |
3326 | 213 |
if alias in self.byName: |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
214 |
raise KeyError("Style '%s' already defined in stylesheet" % alias) |
3326 | 215 |
if alias in self.byAlias: |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
216 |
raise KeyError("Alias name '%s' is already an alias in stylesheet" % alias) |
1683 | 217 |
#passed all tests? OK, add it |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
218 |
self.byName[key] = style |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
219 |
if alias: |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
220 |
self.byAlias[alias] = style |
1683 | 221 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
222 |
def list(self): |
3721 | 223 |
styles = list(self.byName.items()) |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
224 |
styles.sort() |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
225 |
alii = {} |
3721 | 226 |
for (alias, style) in list(self.byAlias.items()): |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
227 |
alii[style] = alias |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
228 |
for (name, style) in styles: |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
229 |
alias = alii.get(style, None) |
3721 | 230 |
print(name, alias) |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
231 |
style.listAttrs(' ') |
3721 | 232 |
print() |
1683 | 233 |
|
125 | 234 |
def testStyles(): |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
235 |
pNormal = ParagraphStyle('Normal',None) |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
236 |
pNormal.fontName = _baseFontName |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
237 |
pNormal.fontSize = 12 |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
238 |
pNormal.leading = 14.4 |
125 | 239 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
240 |
pNormal.listAttrs() |
3721 | 241 |
print() |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
242 |
pPre = ParagraphStyle('Literal', pNormal) |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
243 |
pPre.fontName = 'Courier' |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
244 |
pPre.listAttrs() |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
245 |
return pNormal, pPre |
125 | 246 |
|
247 |
def getSampleStyleSheet(): |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
248 |
"""Returns a stylesheet object""" |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
249 |
stylesheet = StyleSheet1() |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
250 |
|
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
251 |
stylesheet.add(ParagraphStyle(name='Normal', |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
252 |
fontName=_baseFontName, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
253 |
fontSize=10, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
254 |
leading=12) |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
255 |
) |
125 | 256 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
257 |
stylesheet.add(ParagraphStyle(name='BodyText', |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
258 |
parent=stylesheet['Normal'], |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
259 |
spaceBefore=6) |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
260 |
) |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
261 |
stylesheet.add(ParagraphStyle(name='Italic', |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
262 |
parent=stylesheet['BodyText'], |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
263 |
fontName = _baseFontNameI) |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
264 |
) |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
265 |
|
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
266 |
stylesheet.add(ParagraphStyle(name='Heading1', |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
267 |
parent=stylesheet['Normal'], |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
268 |
fontName = _baseFontNameB, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
269 |
fontSize=18, |
512 | 270 |
leading=22, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
271 |
spaceAfter=6), |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
272 |
alias='h1') |
125 | 273 |
|
530 | 274 |
stylesheet.add(ParagraphStyle(name='Title', |
275 |
parent=stylesheet['Normal'], |
|
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
276 |
fontName = _baseFontNameB, |
530 | 277 |
fontSize=18, |
278 |
leading=22, |
|
279 |
alignment=TA_CENTER, |
|
280 |
spaceAfter=6), |
|
281 |
alias='title') |
|
282 |
||
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
283 |
stylesheet.add(ParagraphStyle(name='Heading2', |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
284 |
parent=stylesheet['Normal'], |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
285 |
fontName = _baseFontNameB, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
286 |
fontSize=14, |
512 | 287 |
leading=18, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
288 |
spaceBefore=12, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
289 |
spaceAfter=6), |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
290 |
alias='h2') |
1683 | 291 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
292 |
stylesheet.add(ParagraphStyle(name='Heading3', |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
293 |
parent=stylesheet['Normal'], |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
294 |
fontName = _baseFontNameBI, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
295 |
fontSize=12, |
512 | 296 |
leading=14, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
297 |
spaceBefore=12, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
298 |
spaceAfter=6), |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
299 |
alias='h3') |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
300 |
|
3139 | 301 |
stylesheet.add(ParagraphStyle(name='Heading4', |
302 |
parent=stylesheet['Normal'], |
|
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
303 |
fontName = _baseFontNameBI, |
3139 | 304 |
fontSize=10, |
305 |
leading=12, |
|
306 |
spaceBefore=10, |
|
307 |
spaceAfter=4), |
|
308 |
alias='h4') |
|
309 |
||
310 |
stylesheet.add(ParagraphStyle(name='Heading5', |
|
311 |
parent=stylesheet['Normal'], |
|
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
312 |
fontName = _baseFontNameB, |
3139 | 313 |
fontSize=9, |
314 |
leading=10.8, |
|
315 |
spaceBefore=8, |
|
316 |
spaceAfter=4), |
|
317 |
alias='h5') |
|
318 |
||
319 |
stylesheet.add(ParagraphStyle(name='Heading6', |
|
320 |
parent=stylesheet['Normal'], |
|
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
321 |
fontName = _baseFontNameB, |
3139 | 322 |
fontSize=7, |
323 |
leading=8.4, |
|
324 |
spaceBefore=6, |
|
325 |
spaceAfter=2), |
|
326 |
alias='h6') |
|
327 |
||
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
328 |
stylesheet.add(ParagraphStyle(name='Bullet', |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
329 |
parent=stylesheet['Normal'], |
546 | 330 |
firstLineIndent=0, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
331 |
spaceBefore=3), |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
332 |
alias='bu') |
125 | 333 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
334 |
stylesheet.add(ParagraphStyle(name='Definition', |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
335 |
parent=stylesheet['Normal'], |
546 | 336 |
firstLineIndent=0, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
337 |
leftIndent=36, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
338 |
bulletIndent=0, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
339 |
spaceBefore=6, |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3326
diff
changeset
|
340 |
bulletFontName=_baseFontNameBI), |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
341 |
alias='df') |
125 | 342 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
343 |
stylesheet.add(ParagraphStyle(name='Code', |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
344 |
parent=stylesheet['Normal'], |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
345 |
fontName='Courier', |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
346 |
fontSize=8, |
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
347 |
leading=8.8, |
546 | 348 |
firstLineIndent=0, |
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
349 |
leftIndent=36)) |
1683 | 350 |
|
187
1f22787ae36b
Stylesheets become classes; styles copy data on __init__
andy_robinson
parents:
142
diff
changeset
|
351 |
return stylesheet |