~openerp-groupes/openobject-server/6.0-fix-setup-windows

« back to all changes in this revision

Viewing changes to bin/reportlab/lib/styles.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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$ '''
 
5
 
 
6
from reportlab.lib.colors import white, black
 
7
from reportlab.lib.enums import TA_LEFT, TA_CENTER
 
8
 
 
9
###########################################################
 
10
# This class provides an 'instance inheritance'
 
11
# mechanism for its descendants, simpler than acquisition
 
12
# but not as far-reaching
 
13
###########################################################
 
14
class PropertySet:
 
15
    defaults = {}
 
16
 
 
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"
 
25
        if parent:
 
26
            assert parent.__class__ == self.__class__, "Parent style must have same class as new style"
 
27
 
 
28
        #step two
 
29
        self.name = name
 
30
        self.parent = parent
 
31
        self.__dict__.update(self.defaults)
 
32
 
 
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
 
36
        self.refresh()
 
37
 
 
38
        #step three - copy keywords if any
 
39
        for (key, value) in kw.items():
 
40
             self.__dict__[key] = value
 
41
 
 
42
 
 
43
    def __repr__(self):
 
44
        return "<%s '%s'>" % (self.__class__.__name__, self.name)
 
45
 
 
46
    def refresh(self):
 
47
        """re-fetches attributes from the parent on demand;
 
48
        use if you have been hacking the styles.  This is
 
49
        used by __init__"""
 
50
        if self.parent:
 
51
            for (key, value) in self.parent.__dict__.items():
 
52
                if (key not in ['name','parent']):
 
53
                    self.__dict__[key] = value
 
54
 
 
55
 
 
56
    def listAttrs(self, indent=''):
 
57
        print indent + 'name =', self.name
 
58
        print indent + 'parent =', self.parent
 
59
        keylist = self.__dict__.keys()
 
60
        keylist.sort()
 
61
        keylist.remove('name')
 
62
        keylist.remove('parent')
 
63
        for key in keylist:
 
64
            value = self.__dict__.get(key, None)
 
65
            print indent + '%s = %s' % (key, value)
 
66
 
 
67
class ParagraphStyle(PropertySet):
 
68
    defaults = {
 
69
        'fontName':'Times-Roman',
 
70
        'fontSize':10,
 
71
        'leading':12,
 
72
        'leftIndent':0,
 
73
        'rightIndent':0,
 
74
        'firstLineIndent':0,
 
75
        'alignment':TA_LEFT,
 
76
        'spaceBefore':0,
 
77
        'spaceAfter':0,
 
78
        'bulletFontName':'Times-Roman',
 
79
        'bulletFontSize':10,
 
80
        'bulletIndent':0,
 
81
        'textColor': black,
 
82
        'backColor':None
 
83
        }
 
84
 
 
85
class LineStyle(PropertySet):
 
86
    defaults = {
 
87
        'width':1,
 
88
        'color': black
 
89
        }
 
90
    def prepareCanvas(self, canvas):
 
91
        """You can ask a LineStyle to set up the canvas for drawing
 
92
        the lines."""
 
93
        canvas.setLineWidth(1)
 
94
        #etc. etc.
 
95
 
 
96
class StyleSheet1:
 
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
 
102
       open to discussion);
 
103
    3. allow aliases and alternate style lookup
 
104
       mechanisms
 
105
    4. Have a place to hang style-manipulation
 
106
       methods (save, load, maybe support a GUI
 
107
       editor)
 
108
       Access is via getitem, so they can be
 
109
       compatible with plain old dictionaries.
 
110
       """
 
111
    def __init__(self):
 
112
        self.byName = {}
 
113
        self.byAlias = {}
 
114
 
 
115
 
 
116
    def __getitem__(self, key):
 
117
        try:
 
118
            return self.byAlias[key]
 
119
        except KeyError:
 
120
            try:
 
121
                return self.byName[key]
 
122
            except KeyError:
 
123
                raise KeyError, "Style '%s' not found in stylesheet" % key
 
124
 
 
125
    def has_key(self, key):
 
126
        if self.byAlias.has_key(key):
 
127
            return 1
 
128
        elif self.byName.has_key(key):
 
129
            return 1
 
130
        else:
 
131
            return 0
 
132
 
 
133
    def add(self, style, alias=None):
 
134
        key = style.name
 
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
 
139
 
 
140
        if alias:
 
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
 
147
        if alias:
 
148
            self.byAlias[alias] = style
 
149
 
 
150
    def list(self):
 
151
        styles = self.byName.items()
 
152
        styles.sort()
 
153
        alii = {}
 
154
        for (alias, style) in self.byAlias.items():
 
155
            alii[style] = alias
 
156
        for (name, style) in styles:
 
157
            alias = alii.get(style, None)
 
158
            print name, alias
 
159
            style.listAttrs('    ')
 
160
            print
 
161
 
 
162
 
 
163
 
 
164
 
 
165
def testStyles():
 
166
    pNormal = ParagraphStyle('Normal',None)
 
167
    pNormal.fontName = 'Times-Roman'
 
168
    pNormal.fontSize = 12
 
169
    pNormal.leading = 14.4
 
170
 
 
171
    pNormal.listAttrs()
 
172
    print
 
173
    pPre = ParagraphStyle('Literal', pNormal)
 
174
    pPre.fontName = 'Courier'
 
175
    pPre.listAttrs()
 
176
    return pNormal, pPre
 
177
 
 
178
def getSampleStyleSheet():
 
179
    """Returns a stylesheet object"""
 
180
    stylesheet = StyleSheet1()
 
181
 
 
182
    stylesheet.add(ParagraphStyle(name='Normal',
 
183
                                  fontName='Times-Roman',
 
184
                                  fontSize=10,
 
185
                                  leading=12)
 
186
                   )
 
187
 
 
188
    stylesheet.add(ParagraphStyle(name='BodyText',
 
189
                                  parent=stylesheet['Normal'],
 
190
                                  spaceBefore=6)
 
191
                   )
 
192
    stylesheet.add(ParagraphStyle(name='Italic',
 
193
                                  parent=stylesheet['BodyText'],
 
194
                                  fontName = 'Times-Italic')
 
195
                   )
 
196
 
 
197
    stylesheet.add(ParagraphStyle(name='Heading1',
 
198
                                  parent=stylesheet['Normal'],
 
199
                                  fontName = 'Times-Bold',
 
200
                                  fontSize=18,
 
201
                                  leading=22,
 
202
                                  spaceAfter=6),
 
203
                   alias='h1')
 
204
 
 
205
    stylesheet.add(ParagraphStyle(name='Title',
 
206
                                  parent=stylesheet['Normal'],
 
207
                                  fontName = 'Times-Bold',
 
208
                                  fontSize=18,
 
209
                                  leading=22,
 
210
                                  alignment=TA_CENTER,
 
211
                                  spaceAfter=6),
 
212
                   alias='title')
 
213
 
 
214
    stylesheet.add(ParagraphStyle(name='Heading2',
 
215
                                  parent=stylesheet['Normal'],
 
216
                                  fontName = 'Times-Bold',
 
217
                                  fontSize=14,
 
218
                                  leading=18,
 
219
                                  spaceBefore=12,
 
220
                                  spaceAfter=6),
 
221
                   alias='h2')
 
222
 
 
223
    stylesheet.add(ParagraphStyle(name='Heading3',
 
224
                                  parent=stylesheet['Normal'],
 
225
                                  fontName = 'Times-BoldItalic',
 
226
                                  fontSize=12,
 
227
                                  leading=14,
 
228
                                  spaceBefore=12,
 
229
                                  spaceAfter=6),
 
230
                   alias='h3')
 
231
 
 
232
    stylesheet.add(ParagraphStyle(name='Bullet',
 
233
                                  parent=stylesheet['Normal'],
 
234
                                  firstLineIndent=0,
 
235
                                  spaceBefore=3),
 
236
                   alias='bu')
 
237
 
 
238
    stylesheet.add(ParagraphStyle(name='Definition',
 
239
                                  parent=stylesheet['Normal'],
 
240
                                  firstLineIndent=0,
 
241
                                  leftIndent=36,
 
242
                                  bulletIndent=0,
 
243
                                  spaceBefore=6,
 
244
                                  bulletFontName='Times-BoldItalic'),
 
245
                   alias='df')
 
246
 
 
247
    stylesheet.add(ParagraphStyle(name='Code',
 
248
                                  parent=stylesheet['Normal'],
 
249
                                  fontName='Courier',
 
250
                                  fontSize=8,
 
251
                                  leading=8.8,
 
252
                                  firstLineIndent=0,
 
253
                                  leftIndent=36))
 
254
 
 
255
 
 
256
    return stylesheet