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/lib/styles.py
4
__version__=''' $Id$ '''
6
from reportlab.lib.colors import white, black
7
from reportlab.lib.enums import TA_LEFT, TA_CENTER
9
###########################################################
10
# This class provides an 'instance inheritance'
11
# mechanism for its descendants, simpler than acquisition
12
# but not as far-reaching
13
###########################################################
17
def __init__(self, name, parent=None, **kw):
18
"""When initialized, it copies the class defaults;
19
then takes a copy of the attributes of the parent
20
if any. All the work is done in init - styles
21
should cost little to use at runtime."""
22
# step one - validate the hell out of it
23
assert not self.defaults.has_key('name'), "Class Defaults may not contain a 'name' attribute"
24
assert not self.defaults.has_key('parent'), "Class Defaults may not contain a 'parent' attribute"
26
assert parent.__class__ == self.__class__, "Parent style must have same class as new style"
31
self.__dict__.update(self.defaults)
33
#step two - copy from parent if any. Try to be
34
# very strict that only keys in class defaults are
35
# allowed, so they cannot inherit
38
#step three - copy keywords if any
39
for (key, value) in kw.items():
40
self.__dict__[key] = value
44
return "<%s '%s'>" % (self.__class__.__name__, self.name)
47
"""re-fetches attributes from the parent on demand;
48
use if you have been hacking the styles. This is
51
for (key, value) in self.parent.__dict__.items():
52
if (key not in ['name','parent']):
53
self.__dict__[key] = value
56
def listAttrs(self, indent=''):
57
print indent + 'name =', self.name
58
print indent + 'parent =', self.parent
59
keylist = self.__dict__.keys()
61
keylist.remove('name')
62
keylist.remove('parent')
64
value = self.__dict__.get(key, None)
65
print indent + '%s = %s' % (key, value)
67
class ParagraphStyle(PropertySet):
69
'fontName':'Times-Roman',
78
'bulletFontName':'Times-Roman',
85
class LineStyle(PropertySet):
90
def prepareCanvas(self, canvas):
91
"""You can ask a LineStyle to set up the canvas for drawing
93
canvas.setLineWidth(1)
97
"""This may or may not be used. The idea is to
98
1. slightly simplify construction of stylesheets;
99
2. enforce rules to validate styles when added
100
(e.g. we may choose to disallow having both
101
'heading1' and 'Heading1' - actual rules are
103
3. allow aliases and alternate style lookup
105
4. Have a place to hang style-manipulation
106
methods (save, load, maybe support a GUI
108
Access is via getitem, so they can be
109
compatible with plain old dictionaries.
116
def __getitem__(self, key):
118
return self.byAlias[key]
121
return self.byName[key]
123
raise KeyError, "Style '%s' not found in stylesheet" % key
125
def has_key(self, key):
126
if self.byAlias.has_key(key):
128
elif self.byName.has_key(key):
133
def add(self, style, alias=None):
135
if self.byName.has_key(key):
136
raise KeyError, "Style '%s' already defined in stylesheet" % key
137
if self.byAlias.has_key(key):
138
raise KeyError, "Style name '%s' is already an alias in stylesheet" % key
141
if self.byName.has_key(alias):
142
raise KeyError, "Style '%s' already defined in stylesheet" % alias
143
if self.byAlias.has_key(alias):
144
raise KeyError, "Alias name '%s' is already an alias in stylesheet" % alias
145
#passed all tests? OK, add it
146
self.byName[key] = style
148
self.byAlias[alias] = style
151
styles = self.byName.items()
154
for (alias, style) in self.byAlias.items():
156
for (name, style) in styles:
157
alias = alii.get(style, None)
166
pNormal = ParagraphStyle('Normal',None)
167
pNormal.fontName = 'Times-Roman'
168
pNormal.fontSize = 12
169
pNormal.leading = 14.4
173
pPre = ParagraphStyle('Literal', pNormal)
174
pPre.fontName = 'Courier'
178
def getSampleStyleSheet():
179
"""Returns a stylesheet object"""
180
stylesheet = StyleSheet1()
182
stylesheet.add(ParagraphStyle(name='Normal',
183
fontName='Times-Roman',
188
stylesheet.add(ParagraphStyle(name='BodyText',
189
parent=stylesheet['Normal'],
192
stylesheet.add(ParagraphStyle(name='Italic',
193
parent=stylesheet['BodyText'],
194
fontName = 'Times-Italic')
197
stylesheet.add(ParagraphStyle(name='Heading1',
198
parent=stylesheet['Normal'],
199
fontName = 'Times-Bold',
205
stylesheet.add(ParagraphStyle(name='Title',
206
parent=stylesheet['Normal'],
207
fontName = 'Times-Bold',
214
stylesheet.add(ParagraphStyle(name='Heading2',
215
parent=stylesheet['Normal'],
216
fontName = 'Times-Bold',
223
stylesheet.add(ParagraphStyle(name='Heading3',
224
parent=stylesheet['Normal'],
225
fontName = 'Times-BoldItalic',
232
stylesheet.add(ParagraphStyle(name='Bullet',
233
parent=stylesheet['Normal'],
238
stylesheet.add(ParagraphStyle(name='Definition',
239
parent=stylesheet['Normal'],
244
bulletFontName='Times-BoldItalic'),
247
stylesheet.add(ParagraphStyle(name='Code',
248
parent=stylesheet['Normal'],