Exemple #1
0
 def _path(self, node):
     self.path = self.canvas.beginPath()
     self.path.moveTo(**utils.getAttrsAsDict(node, ['x', 'y']))
     for n in node.childNodes:
         if n.nodeType == node.ELEMENT_NODE:
             if n.localName=='moveto':
                 vals = utils.getText(n).split()
                 self.path.moveTo(utils.as_pt(vals[0]), utils.as_pt(vals[1]))
             elif n.localName=='curvesto':
                 vals = utils.getText(n).split()
                 while len(vals)>5:
                     pos=[]
                     while len(pos)<6:
                         pos.append(utils.as_pt(vals.pop(0)))
                     self.path.curveTo(*pos)
         elif (n.nodeType == node.TEXT_NODE):
             data = n.data.split()    # Not sure if I must merge all TEXT_NODE ?
             while len(data)>1:
                 x = utils.as_pt(data.pop(0))
                 y = utils.as_pt(data.pop(0))
                 self.path.lineTo(x, y)
     if ((not node.hasAttribute('close'))
         or utils.as_bool(node.getAttribute('close'))):
         self.path.close()
     self.canvas.drawPath(
         self.path,
         **utils.getAttrsAsDict(node, [], {'fill':'bool','stroke':'bool'}))
Exemple #2
0
 def _path(self, node):
     self.path = self.canvas.beginPath()
     self.path.moveTo(**utils.getAttrsAsDict(node, ['x', 'y']))
     for n in node.childNodes:
         if n.nodeType == node.ELEMENT_NODE:
             if n.localName=='moveto':
                 vals = utils.getText(n).split()
                 self.path.moveTo(utils.as_pt(vals[0]), utils.as_pt(vals[1]))
             elif n.localName=='curvesto':
                 vals = utils.getText(n).split()
                 while len(vals)>5:
                     pos=[]
                     while len(pos)<6:
                         pos.append(utils.as_pt(vals.pop(0)))
                     self.path.curveTo(*pos)
         elif (n.nodeType == node.TEXT_NODE):
             data = n.data.split()    # Not sure if I must merge all TEXT_NODE ?
             while len(data)>1:
                 x = utils.as_pt(data.pop(0))
                 y = utils.as_pt(data.pop(0))
                 self.path.lineTo(x, y)
     if ((not node.hasAttribute('close'))
         or utils.as_bool(node.getAttribute('close'))):
         self.path.close()
     self.canvas.drawPath(
         self.path,
         **utils.getAttrsAsDict(node, [], {'fill':'bool','stroke':'bool'}))
Exemple #3
0
 def _rect(self, node):
     if node.hasAttribute('round'):
         self.canvas.roundRect(
             radius=utils.as_pt(node.getAttribute('round')),
             **utils.getAttrsAsDict(node,
                                    ['x', 'y', 'width', 'height'],
                                    {'fill': 'bool', 'stroke': 'bool'}))
     else:
         self.canvas.rect(
             **utils.getAttrsAsDict(node,
                                    ['x', 'y', 'width', 'height'],
                                    {'fill': 'bool', 'stroke': 'bool'}))
Exemple #4
0
 def _rect(self, node):
     if node.hasAttribute('round'):
         self.canvas.roundRect(
             radius=utils.as_pt(node.getAttribute('round')),
             **utils.getAttrsAsDict(node,
                                    ['x', 'y', 'width', 'height'],
                                    {'fill': 'bool', 'stroke': 'bool'}))
     else:
         self.canvas.rect(
             **utils.getAttrsAsDict(node,
                                    ['x', 'y', 'width', 'height'],
                                    {'fill': 'bool', 'stroke': 'bool'}))
Exemple #5
0
 def _ellipse(self, node):
     x1 = utils.as_pt(node.getAttribute('x'))
     x2 = utils.as_pt(node.getAttribute('width'))
     y1 = utils.as_pt(node.getAttribute('y'))
     y2 = utils.as_pt(node.getAttribute('height'))
     self.canvas.ellipse(
         x1, y1, x2, y2,
         **utils.getAttrsAsDict(node, [], {'fill': 'bool', 'stroke': 'bool'}))
Exemple #6
0
 def _ellipse(self, node):
     x1 = utils.as_pt(node.getAttribute('x'))
     x2 = utils.as_pt(node.getAttribute('width'))
     y1 = utils.as_pt(node.getAttribute('y'))
     y2 = utils.as_pt(node.getAttribute('height'))
     self.canvas.ellipse(
         x1, y1, x2, y2,
         **utils.getAttrsAsDict(node, [], {'fill': 'bool', 'stroke': 'bool'}))
Exemple #7
0
 def __init__(self, out, node, doc):
     if not node.hasAttribute('pageSize'):
         pageSize = (utils.as_pt('21cm'), utils.as_pt('29.7cm'))
     else:
         ps = map(lambda x:x.strip(),
                  (node.getAttribute('pageSize').replace(')', '')
                   .replace('(', '').split(',')))
         pageSize = (utils.as_pt(ps[0]), utils.as_pt(ps[1]))
     cm = reportlab.lib.units.cm
     self.doc_tmpl = platypus.BaseDocTemplate(
         out, pagesize=pageSize,
         **utils.getAttrsAsDict(node,
                                ['leftMargin', 'rightMargin', 'topMargin',
                                 'bottomMargin'],
                                {'allowSplitting': 'int',
                                 'showBoundary': 'bool',
                                 'title': 'str', 'author': 'str'}))
     self.page_templates = []
     self.styles = doc.styles
     self.doc = doc
     pts = node.getElementsByTagName('pageTemplate')
     for pt in pts:
         frames = []
         for frame_el in pt.getElementsByTagName('frame'):
             frame = platypus.Frame(
                 **(utils.getAttrsAsDict(frame_el,
                                         ['x1','y1', 'width', 'height',
                                          'leftPadding', 'rightPadding',
                                          'bottomPadding', 'topPadding'],
                                         {'id': 'text', 'showBoundary': 'bool'})) )
             frames.append( frame )
         gr = pt.getElementsByTagName('pageGraphics')
         if len(gr):
             drw = _rml_draw(gr[0], self.styles)
             self.page_templates.append(
                 platypus.PageTemplate(frames=frames, onPage=drw.render,
                                       **utils.getAttrsAsDict(pt, [], {'id': 'str'})))
         else:
             self.page_templates.append(
                 platypus.PageTemplate(frames=frames,
                                       **utils.getAttrsAsDict(pt, [], {'id': 'str'})))
     self.doc_tmpl.addPageTemplates(self.page_templates)
Exemple #8
0
 def __init__(self, out, node, doc):
     if not node.hasAttribute('pageSize'):
         pageSize = (utils.as_pt('21cm'), utils.as_pt('29.7cm'))
     else:
         ps = map(lambda x:x.strip(),
                  (node.getAttribute('pageSize').replace(')', '')
                   .replace('(', '').split(',')))
         pageSize = (utils.as_pt(ps[0]), utils.as_pt(ps[1]))
     cm = reportlab.lib.units.cm
     self.doc_tmpl = platypus.BaseDocTemplate(
         out, pagesize=pageSize,
         **utils.getAttrsAsDict(node,
                                ['leftMargin', 'rightMargin', 'topMargin',
                                 'bottomMargin'],
                                {'allowSplitting': 'int',
                                 'showBoundary': 'bool',
                                 'title': 'str', 'author': 'str'}))
     self.page_templates = []
     self.styles = doc.styles
     self.doc = doc
     pts = node.getElementsByTagName('pageTemplate')
     for pt in pts:
         frames = []
         for frame_el in pt.getElementsByTagName('frame'):
             frame = platypus.Frame(
                 **(utils.getAttrsAsDict(frame_el,
                                         ['x1','y1', 'width', 'height',
                                          'leftPadding', 'rightPadding',
                                          'bottomPadding', 'topPadding'],
                                         {'id': 'text', 'showBoundary': 'bool'})) )
             frames.append( frame )
         gr = pt.getElementsByTagName('pageGraphics')
         if len(gr):
             drw = _rml_draw(gr[0], self.doc)
             self.page_templates.append(
                 platypus.PageTemplate(frames=frames, onPage=drw.render,
                                       **utils.getAttrsAsDict(pt, [], {'id': 'str'})))
         else:
             self.page_templates.append(
                 platypus.PageTemplate(frames=frames,
                                       **utils.getAttrsAsDict(pt, [], {'id': 'str'})))
     self.doc_tmpl.addPageTemplates(self.page_templates)
Exemple #9
0
    def _place(self, node):
        flows = _rml_flowable(self.doc).render(node)
        infos = utils.getAttrsAsDict(node, ['x', 'y', 'width', 'height'])

        infos['y']+=infos['height']
        for flow in flows:
            w,h = flow.wrap(infos['width'], infos['height'])
            if w<=infos['width'] and h<=infos['height']:
                infos['y']-=h
                flow.drawOn(self.canvas, infos['x'], infos['y'])
                infos['height']-=h
            else:
                raise ValueError, "Not enough space"
Exemple #10
0
    def _place(self, node):
        flows = _rml_flowable(self.doc).render(node)
        infos = utils.getAttrsAsDict(node, ['x', 'y', 'width', 'height'])

        infos['y']+=infos['height']
        for flow in flows:
            w,h = flow.wrap(infos['width'], infos['height'])
            if w<=infos['width'] and h<=infos['height']:
                infos['y']-=h
                flow.drawOn(self.canvas, infos['x'], infos['y'])
                infos['height']-=h
            else:
                raise ValueError, "Not enough space"
Exemple #11
0
 def _table(self, node):
     length = 0
     colwidths = None
     rowheights = None
     data = []
     for tr in _child_get(node,'tr'):
         data2 = []
         for td in _child_get(tr, 'td'):
             flow = []
             for n in td.childNodes:
                 if n.nodeType==node.ELEMENT_NODE:
                     flow.append( self._flowable(n) )
             if not len(flow):
                 flow = self._textual(td)
             data2.append( flow )
         if len(data2)>length:
             length=len(data2)
             for ab in data:
                 while len(ab)<length:
                     ab.append('')
         while len(data2)<length:
             data2.append('')
         data.append( data2 )
     if node.hasAttribute('colWidths'):
         assert length == len(node.getAttribute('colWidths').split(','))
         colwidths = [utils.as_pt(f.strip())
                      for f in node.getAttribute('colWidths').split(',')]
     if node.hasAttribute('rowHeights'):
         rowheights = [utils.as_pt(f.strip())
                       for f in node.getAttribute('rowHeights').split(',')]
     table = platypus.Table(
         data=data, colWidths=colwidths, rowHeights=rowheights,
         **(utils.getAttrsAsDict(node,
                                 ['splitByRow'],
                                 {'repeatRows':'int','repeatCols':'int'})))
     if node.hasAttribute('style'):
         table.setStyle(self.styles.table_styles[node.getAttribute('style')])
     return table
Exemple #12
0
 def _table(self, node):
     length = 0
     colwidths = None
     rowheights = None
     data = []
     for tr in _child_get(node,'tr'):
         data2 = []
         for td in _child_get(tr, 'td'):
             flow = []
             for n in td.childNodes:
                 if n.nodeType==node.ELEMENT_NODE:
                     flow.append( self._flowable(n) )
             if not len(flow):
                 flow = self._textual(td)
             data2.append( flow )
         if len(data2)>length:
             length=len(data2)
             for ab in data:
                 while len(ab)<length:
                     ab.append('')
         while len(data2)<length:
             data2.append('')
         data.append( data2 )
     if node.hasAttribute('colWidths'):
         assert length == len(node.getAttribute('colWidths').split(','))
         colwidths = [utils.as_pt(f.strip())
                      for f in node.getAttribute('colWidths').split(',')]
     if node.hasAttribute('rowHeights'):
         rowheights = [utils.as_pt(f.strip())
                       for f in node.getAttribute('rowHeights').split(',')]
     table = platypus.Table(
         data=data, colWidths=colwidths, rowHeights=rowheights,
         **(utils.getAttrsAsDict(node,
                                 ['splitByRow'],
                                 {'repeatRows':'int','repeatCols':'int'})))
     if node.hasAttribute('style'):
         table.setStyle(self.styles.table_styles[node.getAttribute('style')])
     return table
Exemple #13
0
 def _circle(self, node):
     self.canvas.circle(
         x_cen=utils.as_pt(node.getAttribute('x')),
         y_cen=utils.as_pt(node.getAttribute('y')),
         r=utils.as_pt(node.getAttribute('radius')),
         **utils.getAttrsAsDict(node, [], {'fill': 'bool', 'stroke': 'bool'}))
Exemple #14
0
 def _flowable(self, node):
     if node.localName=='para':
         style = self.styles.para_style_get(node)
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='name':
         self.styles.names[ node.getAttribute('id')] = node.getAttribute('value')
         return None
     elif node.localName=='xpre':
         style = self.styles.para_style_get(node)
         return platypus.XPreformatted(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str','dedent':'int','frags':'int'})))
     elif node.localName=='pre':
         style = self.styles.para_style_get(node)
         return platypus.Preformatted(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str','dedent':'int'})))
     elif node.localName=='illustration':
         return  self._illustration(node)
     elif node.localName=='blockTable':
         return  self._table(node)
     elif node.localName=='title':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Title']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='h1':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading1']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='h2':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading2']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='h3':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading3']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='image':
         return platypus.Image(
             node.getAttribute('file'), mask=(250, 255, 250, 255, 250, 255),
             **(utils.getAttrsAsDict(node, ['width','height'])))
     elif node.localName=='spacer':
         if node.hasAttribute('width'):
             width = utils.as_pt(node.getAttribute('width'))
         else:
             width = utils.as_pt('1cm')
         length = utils.as_pt(node.getAttribute('length'))
         return platypus.Spacer(width=width, height=length)
     elif node.localName=='pageBreak':
         return platypus.PageBreak()
     elif node.localName=='condPageBreak':
         return platypus.CondPageBreak(**(utils.getAttrsAsDict(node, ['height'])))
     elif node.localName=='setNextTemplate':
         return platypus.NextPageTemplate(str(node.getAttribute('name')))
     elif node.localName=='nextFrame':
         return platypus.CondPageBreak(1000)           # TODO: change the 1000 !
     elif barcode_codes and node.localName=='barCode':
         code = barcode_codes.get(node.getAttribute('code'), Code128)
         attr_types = {'barWidth': 'pt', 'barHeight': 'pt',
                       'fontName': 'text', 'fontSize': 'pt',
                       'humanReadable': 'bool'}
         return code(
             self._textual(node),
             **utils.getAttrsAsDict(node, ['barWidth', 'barHeight'], attr_types))
     else:
         sys.stderr.write('Warning: flowable not yet implemented: %s !\n' % (node.localName,))
         return None
Exemple #15
0
 def _circle(self, node):
     self.canvas.circle(
         x_cen=utils.as_pt(node.getAttribute('x')),
         y_cen=utils.as_pt(node.getAttribute('y')),
         r=utils.as_pt(node.getAttribute('radius')),
         **utils.getAttrsAsDict(node, [], {'fill': 'bool', 'stroke': 'bool'}))
Exemple #16
0
 def _flowable(self, node):
     if node.localName=='para':
         style = self.styles.para_style_get(node)
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='name':
         self.styles.names[ node.getAttribute('id')] = node.getAttribute('value')
         return None
     elif node.localName=='xpre':
         style = self.styles.para_style_get(node)
         return platypus.XPreformatted(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str','dedent':'int','frags':'int'})))
     elif node.localName=='pre':
         style = self.styles.para_style_get(node)
         return platypus.Preformatted(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str','dedent':'int'})))
     elif node.localName=='illustration':
         return  self._illustration(node)
     elif node.localName=='blockTable':
         return  self._table(node)
     elif node.localName=='title':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Title']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='h1':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading1']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='h2':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading2']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='h3':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading3']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='image':
         return platypus.Image(
             node.getAttribute('file'), mask=(250, 255, 250, 255, 250, 255),
             **(utils.getAttrsAsDict(node, ['width','height'])))
     elif node.localName=='spacer':
         if node.hasAttribute('width'):
             width = utils.as_pt(node.getAttribute('width'))
         else:
             width = utils.as_pt('1cm')
         length = utils.as_pt(node.getAttribute('length'))
         return platypus.Spacer(width=width, height=length)
     elif node.localName=='pageBreak':
         return platypus.PageBreak()
     elif node.localName=='condPageBreak':
         return platypus.CondPageBreak(**(utils.getAttrsAsDict(node, ['height'])))
     elif node.localName=='setNextTemplate':
         return platypus.NextPageTemplate(str(node.getAttribute('name')))
     elif node.localName=='nextFrame':
         return platypus.CondPageBreak(1000)           # TODO: change the 1000 !
     elif barcode_codes and node.localName=='barCode':
         code = barcode_codes.get(node.getAttribute('code'), Code128)
         return code(self._textual(node),
                     **utils.getAttrsAsDict(node, ['barWidth', 'barHeight']))
     else:
         sys.stderr.write('Warning: flowable not yet implemented: %s !\n' % (node.localName,))
         return None
Exemple #17
0
 def _drawRightString(self, node):
     self.canvas.drawRightString(
         text=self._textual(node), **utils.getAttrsAsDict(node, ['x', 'y']))
Exemple #18
0
 def _drawRightString(self, node):
     self.canvas.drawRightString(
         text=self._textual(node), **utils.getAttrsAsDict(node, ['x', 'y']))