1
# -*- coding: ISO-8859-1 -*-
2
#############################################
3
## (C)opyright by Dirk Holtwick, 2002-2007 ##
4
## All rights reserved ##
5
#############################################
7
__reversion__ = "$Revision: 20 $"
8
__author__ = "$Author: holtwick $"
9
__date__ = "$Date: 2007-10-09 12:58:24 +0200 (Di, 09 Okt 2007) $"
11
from pisa_tags import pisaTag
12
from pisa_util import *
13
from pisa_reportlab import PmlTable, TableStyle, PmlKeepInFrame
19
log = logging.getLogger("ho.pisa")
21
def _width(value=None):
25
if value.endswith("%"):
40
def add_cell(self, data=None):
42
self.data[len(self.data) - 1].append(data)
44
def add_style(self, data):
45
# print self.mode, data
46
# Do we have color and
48
#if data[0].startswith("LINE"):
52
self.styles.append(copy.copy(data))
54
def add_empty(self, x, y):
55
self.span.append((x, y))
59
for x, y in self.span:
66
def add_cell_styles(self, c, begin, end, mode="td"):
69
self.mode = mode.upper()
70
if c.frag.backColor and mode != "tr": # XXX Stimmt das so?
71
self.add_style(('BACKGROUND', begin, end, c.frag.backColor))
72
# print 'BACKGROUND', begin, end, c.frag.backColor
77
c.frag.borderTopWidth,
78
c.frag.borderTopStyle,
79
c.frag.borderTopColor,
80
c.frag.borderBottomWidth,
81
c.frag.borderBottomStyle,
82
c.frag.borderBottomColor,
83
c.frag.borderLeftWidth,
84
c.frag.borderLeftStyle,
85
c.frag.borderLeftColor,
86
c.frag.borderRightWidth,
87
c.frag.borderRightStyle,
88
c.frag.borderRightColor,
90
if getBorderStyle(c.frag.borderTopStyle) and c.frag.borderTopWidth and c.frag.borderTopColor is not None:
91
self.add_style(('LINEABOVE', begin, (end[0], begin[1]),
92
c.frag.borderTopWidth,
93
c.frag.borderTopColor,
95
if getBorderStyle(c.frag.borderLeftStyle) and c.frag.borderLeftWidth and c.frag.borderLeftColor is not None:
96
self.add_style(('LINEBEFORE', begin, (begin[0], end[1]),
97
c.frag.borderLeftWidth,
98
c.frag.borderLeftColor,
100
if getBorderStyle(c.frag.borderRightStyle) and c.frag.borderRightWidth and c.frag.borderRightColor is not None:
101
self.add_style(('LINEAFTER', (end[0], begin[1]), end,
102
c.frag.borderRightWidth,
103
c.frag.borderRightColor,
105
if getBorderStyle(c.frag.borderBottomStyle) and c.frag.borderBottomWidth and c.frag.borderBottomColor is not None:
106
self.add_style(('LINEBELOW', (begin[0], end[1]), end,
107
c.frag.borderBottomWidth,
108
c.frag.borderBottomColor,
110
self.add_style(('LEFTPADDING', begin, end, c.frag.paddingLeft or self.padding))
111
self.add_style(('RIGHTPADDING', begin, end, c.frag.paddingRight or self.padding))
112
self.add_style(('TOPPADDING', begin, end, c.frag.paddingTop or self.padding))
113
self.add_style(('BOTTOMPADDING', begin, end, c.frag.paddingBottom or self.padding))
115
class pisaTagTABLE(pisaTag):
123
c.tableData, self.tableData = TableData(), c.tableData
127
#tdata.border = attrs.border
128
#tdata.bordercolor = attrs.bordercolor
133
if attrs.border and attrs.bordercolor:
135
frag.borderLeftWidth = attrs.border
136
frag.borderLeftColor = attrs.bordercolor
137
frag.borderLeftStyle = "solid"
138
frag.borderRightWidth = attrs.border
139
frag.borderRightColor = attrs.bordercolor
140
frag.borderRightStyle = "solid"
141
frag.borderTopWidth = attrs.border
142
frag.borderTopColor = attrs.bordercolor
143
frag.borderTopStyle = "solid"
144
frag.borderBottomWidth = attrs.border
145
frag.borderBottomColor = attrs.bordercolor
146
frag.borderBottomStyle = "solid"
148
# tdata.add_style(("GRID", begin, end, attrs.border, attrs.bordercolor))
150
tdata.padding = attrs.cellpadding
152
#if 0: #attrs.cellpadding:
153
# tdata.add_style(('LEFTPADDING', begin, end, attrs.cellpadding))
154
# tdata.add_style(('RIGHTPADDING', begin, end, attrs.cellpadding))
155
# tdata.add_style(('TOPPADDING', begin, end, attrs.cellpadding))
156
# tdata.add_style(('BOTTOMPADDING', begin, end, attrs.cellpadding))
159
#~ tdata.add_style(('VALIGN', (0,0), (-1,-1), attrs.valign.upper()))
161
# Set Border and padding styles
163
tdata.add_cell_styles(c, (0, 0), (-1, - 1), "table")
166
#if attrs.bgcolor is not None:
167
# tdata.add_style(('BACKGROUND', (0, 0), (-1, -1), attrs.bgcolor))
169
tdata.align = attrs.align.upper()
174
tdata.repeat = attrs.repeat
175
tdata.width = _width(attrs.width)
177
# self.tabdata.append(tdata)
181
data = tdata.get_data()
183
# Add missing columns so that each row has the same count of columns
184
# This prevents errors in Reportlab table
185
maxcols = max([len(row) for row in data] or [0])
186
for i, row in enumerate(data):
187
data[i] += [''] * (maxcols - len(row))
191
# log.debug("Table sryles %r", tdata.styles)
194
colWidths=tdata.colw,
195
rowHeights=tdata.rowh,
196
# totalWidth = tdata.width,
199
repeatRows=tdata.repeat,
202
style=TableStyle(tdata.styles))
203
t.totalWidth = _width(tdata.width)
204
t.spaceBefore = c.frag.spaceBefore
205
t.spaceAfter = c.frag.spaceAfter
207
# XXX Maybe we need to copy some more properties?
208
t.keepWithNext = c.frag.keepWithNext
209
# t.hAlign = tdata.align
212
log.warn(c.warning("<table> is empty"))
214
log.warn(c.warning("<table>"), exc_info=1)
216
# Cleanup and re-swap table data
218
c.tableData, self.tableData = self.tableData, None
220
class pisaTagTR(pisaTag):
228
tdata.add_cell_styles(c, begin, end, "tr")
229
c.frag.vAlign = self.attr.valign or c.frag.vAlign
232
tdata.data.append([])
237
class pisaTagTD(pisaTag):
241
if self.attr.align is not None:
242
#print self.attr.align, getAlign(self.attr.align)
243
c.frag.alignment = getAlign(self.attr.align)
246
self.story = c.swapStory()
247
# print "#", len(c.story)
253
cspan = attrs.colspan
254
rspan = attrs.rowspan
259
for x, y in tdata.span:
260
if x == col and y == row:
270
end = (end[0] + cspan - 1, end[1])
272
end = (end[0], end[1] + rspan - 1)
275
tdata.add_style(('SPAN', begin, end))
276
for x in range(begin[0], end[0] + 1):
277
for y in range(begin[1], end[1] + 1):
278
if x != begin[0] or y != begin[1]:
279
tdata.add_empty(x, y)
281
# Set Border and padding styles
282
tdata.add_cell_styles(c, begin, end, "td")
285
# Add empty placeholders for new columns
286
if (col + 1) > len(tdata.colw):
287
tdata.colw = tdata.colw + ((col + 1 - len(tdata.colw)) * [_width()])
288
# Get value of with, if no spanning
291
width = c.frag.width or self.attr.width #self._getStyle(None, attrs, "width", "width", mode)
292
# If is value, the set it in the right place in the arry
293
# print width, _width(width)
294
if width is not None:
295
tdata.colw[col] = _width(width)
298
if row + 1 > len(tdata.rowh):
299
tdata.rowh = tdata.rowh + ((row + 1 - len(tdata.rowh)) * [_width()])
301
height = None #self._getStyle(None, attrs, "height", "height", mode)
302
if height is not None:
303
tdata.rowh[row] = _width(height)
304
tdata.add_style(('FONTSIZE', begin, end, 1.0))
305
tdata.add_style(('LEADING', begin, end, 1.0))
308
valign = self.attr.valign or c.frag.vAlign
309
if valign is not None:
310
tdata.add_style(('VALIGN', begin, end, valign.upper()))
312
# Reset border, otherwise the paragraph block will have borders too
314
frag.borderLeftWidth = 0
315
frag.borderLeftColor = None
316
frag.borderLeftStyle = None
317
frag.borderRightWidth = 0
318
frag.borderRightColor = None
319
frag.borderRightStyle = None
320
frag.borderTopWidth = 0
321
frag.borderTopColor = None
322
frag.borderTopStyle = None
323
frag.borderBottomWidth = 0
324
frag.borderBottomColor = None
325
frag.borderBottomStyle = None
333
# Handle empty cells, they otherwise collapse
337
# Keep in frame if needed since Reportlab does no split inside of cells
338
elif (not c.frag.insideStaticFrame) and (c.frag.keepInFrameMode is not None):
340
# tdata.keepinframe["content"] = cell
341
cell = PmlKeepInFrame(
344
mode=c.frag.keepInFrameMode,
347
c.swapStory(self.story)
351
class pisaTagTH(pisaTagTD):
357
def start_keeptogether(self, attrs):
358
self.story.append([])
361
def end_keeptogether(self):
362
if not self.story[-1]:
366
self.add_story(KeepTogether(s))
368
def start_keepinframe(self, attrs):
369
self.story.append([])
371
"maxWidth": attrs["maxwidth"],
372
"maxHeight": attrs["maxheight"],
373
"mode": attrs["mode"],
374
"name": attrs["name"],
375
"mergeSpace": attrs["mergespace"]
377
# print self.keepinframe
380
def end_keepinframe(self):
381
if not self.story[-1]:
384
self.keepinframe["content"] = self.story.pop()
385
self.add_story(KeepInFrame(**self.keepinframe))
b'\\ No newline at end of file'