|
26 | 26 | from reportlab.platypus.tables import Table, TableStyle
|
27 | 27 | from xhtml2pdf.reportlab_paragraph import Paragraph
|
28 | 28 | from xhtml2pdf.util import getUID, getBorderStyle
|
| 29 | +from types import StringType, TupleType, ListType, IntType |
29 | 30 | import StringIO
|
30 | 31 | import cgi
|
31 | 32 | import copy
|
|
49 | 50 |
|
50 | 51 | MAX_IMAGE_RATIO = 0.95
|
51 | 52 |
|
| 53 | +class PTCycle(list): |
| 54 | + def __init__(self): |
| 55 | + self._restart = 0 |
| 56 | + self._idx = 0 |
| 57 | + list.__init__(self) |
| 58 | + |
| 59 | + def cyclicIterator(self): |
| 60 | + while 1: |
| 61 | + yield self[self._idx] |
| 62 | + self._idx += 1 |
| 63 | + if self._idx>=len(self): |
| 64 | + self._idx = self._restart |
| 65 | + |
52 | 66 | class PmlMaxHeightMixIn:
|
53 | 67 |
|
54 | 68 | def setMaxHeight(self, availHeight):
|
@@ -114,6 +128,51 @@ def afterFlowable(self, flowable):
|
114 | 128 | cgi.escape(copy.deepcopy(flowable.text), 1),
|
115 | 129 | self.page))
|
116 | 130 |
|
| 131 | + def handle_nextPageTemplate(self,pt): |
| 132 | + ''' if pt has also templates for even and odd page convert it to list ''' |
| 133 | + if self._has_template_for_name(pt + '_left') and self._has_template_for_name(pt + '_right'): |
| 134 | + pt = [pt + '_left', pt + '_right'] |
| 135 | + |
| 136 | + '''On endPage change to the page template with name or index pt''' |
| 137 | + if type(pt) is StringType: |
| 138 | + if hasattr(self, '_nextPageTemplateCycle'): del self._nextPageTemplateCycle |
| 139 | + for t in self.pageTemplates: |
| 140 | + if t.id == pt: |
| 141 | + self._nextPageTemplateIndex = self.pageTemplates.index(t) |
| 142 | + return |
| 143 | + raise ValueError, "can't find template('%s')"%pt |
| 144 | + elif type(pt) is IntType: |
| 145 | + if hasattr(self, '_nextPageTemplateCycle'): del self._nextPageTemplateCycle |
| 146 | + self._nextPageTemplateIndex = pt |
| 147 | + elif type(pt) in (ListType, TupleType): |
| 148 | + #used for alternating left/right pages |
| 149 | + #collect the refs to the template objects, complain if any are bad |
| 150 | + c = PTCycle() |
| 151 | + for ptn in pt: |
| 152 | + if ptn=='*': #special case name used to short circuit the iteration |
| 153 | + c._restart = len(c) |
| 154 | + continue |
| 155 | + for t in self.pageTemplates: |
| 156 | + if t.id == ptn.strip(): |
| 157 | + c.append(t) |
| 158 | + break |
| 159 | + if not c: |
| 160 | + raise ValueError("No valid page templates in cycle") |
| 161 | + elif c._restart>len(c): |
| 162 | + raise ValueError("Invalid cycle restart position") |
| 163 | + |
| 164 | + #ensure we start on the first one$ |
| 165 | + self._nextPageTemplateCycle = c.cyclicIterator() |
| 166 | + else: |
| 167 | + raise TypeError("Argument pt should be string or integer or list") |
| 168 | + |
| 169 | + def _has_template_for_name(self, name): |
| 170 | + result = False |
| 171 | + for template in self.pageTemplates: |
| 172 | + if template.id == name.strip(): |
| 173 | + result = True |
| 174 | + return result |
| 175 | + |
117 | 176 | class PmlPageTemplate(PageTemplate):
|
118 | 177 |
|
119 | 178 | def __init__(self, **kw):
|
|
0 commit comments