Esempio n. 1
0
    def render(self, pagemgr):
        """
		Render the image at specified width, height
		An image can be positionned:
		- Within a frame
		- On the canvas at specified coords (x,y)
		- On the canvas at relative to another flowable
		"""
        start = time.time()

        frame = None
        canvas = None
        if pagemgr is not None:
            frame = pagemgr.getcurrentframe()
            canvas = pagemgr.getcanvas()

        # This might be a special image bound to a rect area
        # The rect is specified in XML and defines a text
        # field to be positionned on canvas at specified coords
        imgfield_rect = self._baseelement.getrect()
        if imgfield_rect is not None and canvas is not None:
            image = self._buildfromrect(canvas, self._baseelement._src,
                                        imgfield_rect)
        else:
            image = Image(self._baseelement._src, self._baseelement._width,
                          self._baseelement._height)
            if frame is not None:
                frame.addFromList([image], canvas)

        end = time.time()
        delta = end - start
        adddeltatimer('image', delta)

        return image
Esempio n. 2
0
	def render(self, pagemgr):
		"""
		Render the image at specified width, height
		An image can be positionned:
		- Within a frame
		- On the canvas at specified coords (x,y)
		- On the canvas at relative to another flowable
		"""
		start = time.time()

		frame = None
		canvas = None
		if pagemgr is not None:
			frame = pagemgr.getcurrentframe()
			canvas = pagemgr.getcanvas()

		# This might be a special image bound to a rect area
		# The rect is specified in XML and defines a text
		# field to be positionned on canvas at specified coords
		imgfield_rect = self._baseelement.getrect()
		if imgfield_rect is not None and canvas is not None:
			image = self._buildfromrect(canvas,
						                self._baseelement._src,
						                imgfield_rect)
		else:
			image =  Image(self._baseelement._src, self._baseelement._width, self._baseelement._height )
			if frame is not None:
				frame.addFromList([image], canvas)

		end = time.time()
		delta = end - start
		adddeltatimer('image',delta)

		return image
Esempio n. 3
0
    def render(self, pagemgr):
        """
		This is the 'generic' renderer. It simply calls render(...)
		on all its children
		"""
        rendered = []
        frame = None
        canvas = None
        if pagemgr is not None:
            frame = pagemgr.getcurrentframe()
            canvas = pagemgr.getcanvas()

        for child in self._baseelement.getchildren():
            docelementname = child.__class__.__name__

            newrlobject = rlObjectInstance(docelementname, child)
            if newrlobject is not None:
                # the child inherits the parent's rect
                newrlobject.setrect(self._rect)
                rendered.append(newrlobject.render(pagemgr))

        # Even if nothing is rendered we ship an empty text flowable
        if len(rendered) == 0:
            rendered = XPreformatted('', rlsamplestylesheet['BodyText'])

        return rendered
Esempio n. 4
0
	def render(self, pagemgr):
		"""
		This is the 'generic' renderer. It simply calls render(...)
		on all its children
		"""
		rendered = []
		frame = None
		canvas = None
		if pagemgr is not None:
			frame = pagemgr.getcurrentframe()
			canvas = pagemgr.getcanvas()

		for child in self._baseelement.getchildren():
			docelementname  = child.__class__.__name__

			newrlobject = rlObjectInstance(docelementname, child)
			if newrlobject is not None:
				# the child inherits the parent's rect
				newrlobject.setrect(self._rect)
				rendered.append(newrlobject.render(pagemgr))

		# Even if nothing is rendered we ship an empty text flowable
		if len(rendered) == 0:
			rendered = XPreformatted('', rlsamplestylesheet['BodyText'])

		return rendered
Esempio n. 5
0
    def render(self, pagemgr):
        """
		We allow canvas transorms in Reporlab coordinates.
		Reportlab does not support saving/restoring canvas states
		across 2+ pages. Make sure all transforms are done BEFORE
		a pagebreak occurs
		"""

        rendered = None
        frame = None
        canvas = None
        if pagemgr is not None:
            frame = pagemgr.getcurrentframe()
            canvas = pagemgr.getcanvas()

        transform_action = None
        num_args = 0
        if canvas is not None:
            basestyle = self._baseelement.getstyle()
            trans_data = basestyle.getattribute('transform', str)
            if trans_data is not None:
                tranform_ctx = trans_data.split(':')
                if tranform_ctx is not None and len(tranform_ctx) > 0:
                    transform_type = tranform_ctx[0].lower()
                    if transform_type == 'rotate' and len(tranform_ctx) >= 2:
                        transform_action = NumberedCanvas.dorotate
                        num_args = 1
                    elif transform_type == 'translate' and len(
                            tranform_ctx) >= 3:
                        transform_action = NumberedCanvas.dotranslate
                        num_args = 2
                    elif transform_type == 'scale' and len(tranform_ctx) >= 3:
                        transform_action = NumberedCanvas.doscale
                        num_args = 2

        if transform_action is not None:
            pagemgr._canvas.saveState()

        if num_args == 1:
            transform_action(canvas, float(tranform_ctx[1]))
        elif num_args == 2:
            transform_action(canvas, float(tranform_ctx[1]),
                             float(tranform_ctx[2]))
        else:
            print "Warning: transform with no args?"

        rendered = rlobject.render(self, pagemgr)

        if transform_action is not None:
            pagemgr._canvas.restoreState()

        return rendered
Esempio n. 6
0
	def render(self, pagemgr):
		"""
		We allow canvas transorms in Reporlab coordinates.
		Reportlab does not support saving/restoring canvas states
		across 2+ pages. Make sure all transforms are done BEFORE
		a pagebreak occurs
		"""

		rendered = None
		frame = None
		canvas = None
		if pagemgr is not None:
			frame = pagemgr.getcurrentframe()
			canvas = pagemgr.getcanvas()                

		transform_action = None
		num_args = 0
		if canvas is not None:
			basestyle = self._baseelement.getstyle()
			trans_data = basestyle.getattribute('transform',str)
			if trans_data is not None:
				tranform_ctx = trans_data.split(':')
				if tranform_ctx is not None and len(tranform_ctx)>0:
					transform_type = tranform_ctx[0].lower()
					if transform_type == 'rotate' and len(tranform_ctx) >= 2:
						transform_action = NumberedCanvas.dorotate
						num_args = 1
					elif transform_type == 'translate' and len(tranform_ctx) >= 3:
						transform_action = NumberedCanvas.dotranslate
						num_args = 2
					elif transform_type == 'scale' and len(tranform_ctx) >= 3:
						transform_action = NumberedCanvas.doscale
						num_args = 2


		if transform_action is not None:
			pagemgr._canvas.saveState()

		if num_args == 1:
			transform_action(canvas,float(tranform_ctx[1]))
		elif num_args == 2:
			transform_action(canvas,float(tranform_ctx[1]),float(tranform_ctx[2]))
		else:
			print "Warning: transform with no args?"

		rendered = rlobject.render(self, pagemgr)

		if transform_action is not None:
			pagemgr._canvas.restoreState()    

		return rendered
Esempio n. 7
0
    def render(self, pagemgr):
        rendered = []
        frame = None
        canvas = None
        if pagemgr is not None:
            frame = pagemgr.getcurrentframe()
            canvas = pagemgr.getcanvas()

        basestyle = self._baseelement.getstyle()
        border = basestyle.getattribute('border', float)
        color = basestyle.getattribute('color', str)

        pos_xy, r_xy = self._baseelement.getellipse()
        canvas_height = canvas._pagesize[1]
        pos_y = canvas_height - pos_xy[1]

        canvas.saveState()
        canvas.setStrokeColor(color)
        canvas.setLineWidth(float(border))
        canvas.ellipse(pos_xy[0], pos_y, pos_xy[0] + r_xy[0], pos_y - r_xy[1])
        canvas.restoreState()
Esempio n. 8
0
	def render(self, pagemgr):
		rendered = []
		frame = None
		canvas = None
		if pagemgr is not None:
			frame = pagemgr.getcurrentframe()
			canvas = pagemgr.getcanvas()                

		basestyle = self._baseelement.getstyle()
		border = basestyle.getattribute('border',float)
		color = basestyle.getattribute('color',str)

		pos_xy, r_xy = self._baseelement.getellipse()                
		canvas_height = canvas._pagesize[1]
		pos_y = canvas_height - pos_xy[1]

		canvas.saveState()
		canvas.setStrokeColor(color)
		canvas.setLineWidth(float(border))
		canvas.ellipse(pos_xy[0], pos_y, pos_xy[0]+r_xy[0], pos_y-r_xy[1])
		canvas.restoreState()
Esempio n. 9
0
    def render(self, pagemgr):
        rendered = []
        frame = None
        canvas = None
        if pagemgr is not None:
            frame = pagemgr.getcurrentframe()
            canvas = pagemgr.getcanvas()

        basestyle = self._baseelement.getstyle()
        border = basestyle.getattribute('border', float)
        color = basestyle.getattribute('color', str)

        from_xy, to_xy = self._baseelement.getline()
        canvas_height = canvas._pagesize[1]
        from_y = canvas_height - from_xy[1]
        to_y = canvas_height - to_xy[1]

        canvas.saveState()
        canvas.setStrokeColor(color)
        canvas.setLineWidth(float(border))
        canvas.line(from_xy[0], from_y, to_xy[0], to_y)
        canvas.restoreState()
Esempio n. 10
0
    def render(self, pagemgr):
        rendered = []
        frame = None
        canvas = None
        if pagemgr is not None:
            frame = pagemgr.getcurrentframe()
            canvas = pagemgr.getcanvas()

        rect = self._baseelement.getrect()
        canvas_height = canvas._pagesize[1]

        # We're looking for the object we're supposed to be positionned
        # relative to, hopefully it exists and has been rendered already!
        anchor_id, anchor_data = self._baseelement.getanchor()
        rendered_anchor = findinstance(anchor_id)
        if rendered_anchor is not None:
            dest_rect = rendered_anchor.getrect()
            dest_rect[1] = canvas_height - (dest_rect[1] + dest_rect[3])
            rect = snapsrctodest(rect, dest_rect,
                                 rlobject.snapto_dict[anchor_data[1].upper()],
                                 rlobject.snapto_dict[anchor_data[2].upper()],
                                 [0, 0])

        rect[1] = canvas_height - (rect[1] + rect[3])
        basestyle = self._baseelement.getstyle()
        bckg_color = basestyle.getattribute('background-color', str)
        border = basestyle.getattribute('border', float)
        color = basestyle.getattribute('color', str)
        canvas.saveState()
        canvas.setStrokeColor(color)
        canvas.setLineWidth(border)
        dofill = 0
        if bckg_color.lower() != 'none':
            canvas.setFillColor(bckg_color)
            dofill = 1
        canvas.rect(rect[0], rect[1], rect[2], rect[3], stroke=1, fill=dofill)
        canvas.restoreState()
        register(self._baseelement.getid(),
                 RLFrame(rect[0], rect[1], rect[2], rect[3]))
Esempio n. 11
0
	def render(self, pagemgr):
		rendered = []
		frame = None
		canvas = None
		if pagemgr is not None:
			frame = pagemgr.getcurrentframe()
			canvas = pagemgr.getcanvas()                

		basestyle = self._baseelement.getstyle()
		border = basestyle.getattribute('border',float)
		color = basestyle.getattribute('color',str)

		from_xy, to_xy = self._baseelement.getline()                
		canvas_height = canvas._pagesize[1]
		from_y = canvas_height - from_xy[1]
		to_y = canvas_height - to_xy[1]

		canvas.saveState()
		canvas.setStrokeColor(color)
		canvas.setLineWidth(float(border))
		canvas.line(from_xy[0], from_y, to_xy[0], to_y)
		canvas.restoreState()
Esempio n. 12
0
	def render(self, pagemgr):
		rendered = []
		frame = None
		canvas = None
		if pagemgr is not None:
			frame = pagemgr.getcurrentframe()
			canvas = pagemgr.getcanvas()                

		rect = self._baseelement.getrect()
		canvas_height = canvas._pagesize[1]

		# We're looking for the object we're supposed to be positionned
		# relative to, hopefully it exists and has been rendered already!
		anchor_id, anchor_data = self._baseelement.getanchor()
		rendered_anchor = findinstance(anchor_id)                
		if rendered_anchor is not None:
			dest_rect = rendered_anchor.getrect()   
			dest_rect[1] = canvas_height - (dest_rect[1]+dest_rect[3])                        
			rect = snapsrctodest(rect, 
						         dest_rect, 
						         rlobject.snapto_dict[anchor_data[1].upper()],
						         rlobject.snapto_dict[anchor_data[2].upper()], 
						         [0,0])

		rect[1] = canvas_height - (rect[1]+rect[3])
		basestyle = self._baseelement.getstyle()
		bckg_color = basestyle.getattribute('background-color',str)
		border = basestyle.getattribute('border',float)
		color = basestyle.getattribute('color',str)
		canvas.saveState()
		canvas.setStrokeColor(color)
		canvas.setLineWidth(border)
		dofill = 0
		if bckg_color.lower() != 'none':
			canvas.setFillColor(bckg_color)
			dofill = 1
		canvas.rect(rect[0], rect[1], rect[2], rect[3], stroke=1, fill=dofill)
		canvas.restoreState()
		register(self._baseelement.getid(), RLFrame(rect[0], rect[1], rect[2], rect[3]))                
Esempio n. 13
0
    def render(self, pagemgr):
        """
		Tables seem to be the most expensive elements to render, proportionately
		to their number of cells. TODO: A faster implementation of tables using
		a different XML representation, which could be closer to ReportLab tables
		"""
        def makeRLTablestyle(tableelement, basestyle, styles, start, end):

            color = basestyle.getattribute('color', str)
            backgcolor = basestyle.getattribute('background-color', str)
            vertical_alignment = basestyle.getattribute('vertical-align', str)
            top_padding = basestyle.getattribute('top-padding', float)
            bottom_padding = basestyle.getattribute('bottom-padding', float)
            left_padding = basestyle.getattribute('left-padding', float)
            right_padding = basestyle.getattribute('right-padding', float)
            if backgcolor.lower() != 'none':
                styles.append(('BACKGROUND', start, end, backgcolor))
            styles.append(('TOPPADDING', start, end, top_padding))
            styles.append(('BOTTOMPADDING', start, end, bottom_padding))
            styles.append(('LEFTPADDING', start, end, left_padding))
            styles.append(('RIGHTPADDING', start, end, right_padding))
            styles.append(('VALIGN', start, end, vertical_alignment.upper()))
            styles.append(('COLOR', start, end, color))
            if start != end:
                styles.append(('SPAN', start, end))

        frame = None
        canvas = None
        if pagemgr is not None:
            frame = pagemgr.getcurrentframe()
            canvas = pagemgr.getcanvas()

        colwidths, rowheights = self._baseelement.getdimensions()
        rowcells = self._baseelement.getrowcells()

        num_cols = len(colwidths)
        num_rows = len(rowheights)
        ori_num_cols = num_cols

        data_array = []
        for i in range(num_rows):
            data_array.append(num_cols * [None])

        basestyle = self._baseelement.getstyle()
        border = basestyle.getattribute('border', float)
        grid = basestyle.getattribute('grid', float)
        color = basestyle.getattribute('color', str)
        border_frame = basestyle.getattribute('frame', str)

        stylecmds = []

        if border_frame == 'above':
            stylecmds.append(('LINEABOVE', (0, 0), (-1, 0), border, color))
        elif border_frame == 'below':
            stylecmds.append(('LINEBELOW', (0, -1), (-1, -1), border, color))
        elif border_frame == 'left':
            stylecmds.append(('LINEBEFORE', (0, 0), (0, -1), border, color))
        elif border_frame == 'right':
            stylecmds.append(('LINEAFTER', (-1, 0), (-1, -1), border, color))
        elif border > 0.0:
            stylecmds.append(('BOX', (0, 0), (-1, -1), border, color))

        if grid > 0.0:
            stylecmds.append(('INNERGRID', (0, 0), (-1, -1), grid, color))

        start = 0
        delta_loop = 0
        start = time.time()

        row_index = 0
        # Go through each row for the table
        for row in rowcells:
            col_index = 0
            cell_index = 0
            # Each row is composed of cells which may span several columns, rows
            for cell in row:
                addstyle = True
                subrow_index = 0
                # Each cell possibly spans several rows
                for subrow in cell:
                    subcol_index = 0
                    # Each cell possibly spans several columns
                    for subcol in subrow:
                        docelementname = subcol.__class__.__name__
                        # This is the object contained withing a table cell
                        rlobject = rlObjectInstance(docelementname, subcol)
                        final_row_index = row_index + subrow_index
                        final_col_index = col_index + subcol_index
                        row_span = len(cell)
                        col_span = len(subrow)
                        cell_width = None
                        cell_height = None

                        # We dont want to overwrite existing cells from previous rows.
                        # Skip until we find empty cells. Sometimes this results in
                        # a table that needs to be adjusted and we'll add extra columns.
                        while data_array[final_row_index][
                                final_col_index] is not None:
                            subcol_index += 1
                            final_col_index = col_index + subcol_index
                            if final_col_index >= len(
                                    data_array[final_row_index]):
                                data_array[final_row_index].append(None)
                                num_cols += 1

                        #Calculate actual width, height of a cell. Cells can span several columns, rows
                        if colwidths[col_index] is not None:
                            cell_width = col_span * colwidths[
                                col_index]  # TODO: Test we dont get out bounds of colwidths
                        if rowheights[row_index] is not None:
                            cell_height = row_span * rowheights[
                                row_index]  # TODO: Test we dont get out bounds of rowheights

                        # Give the object contained with the table cell it's boundaries
                        rlobject.setrect((0, 0, cell_width, cell_height))

                        loop_start = time.time()
                        data_array[final_row_index][
                            final_col_index] = rlobject.render(
                                None)  # render the table cell object
                        loop_end = time.time()
                        delta_loop += (loop_end - loop_start)

                        basestyle = rlobject._baseelement.getstyle()

                        if addstyle == True:
                            addstyle = False
                            makeRLTablestyle(
                                rlobject, basestyle, stylecmds,
                                (final_col_index, final_row_index),
                                (final_col_index + col_span - 1,
                                 final_row_index + row_span - 1))
                        subcol_index += 1
                    subrow_index += 1
                col_index += len(subrow)
                cell_index += 1
            row_index += 1

        # if the table needs to be adjusted and add extra colum(s)
        if num_cols > ori_num_cols:
            for i in range(num_rows):
                adjust_cols = num_cols - len(data_array[i])
                new_cols = adjust_cols * [None]
                data_array[i] += new_cols

        style = TableStyle(stylecmds)
        table = Table(data_array, colwidths, rowheights, style)

        if frame is not None:
            frame.addFromList([table], canvas)

        end = time.time()
        delta = end - start
        adddeltatimer('table', delta - (delta_loop))

        return table
Esempio n. 14
0
    def render(self, pagemgr):
        """
		Texts are either XPreformat class instances or Paragraph class instances
		from Reportlab package. An text can be positionned:
		- Within a frame
		- On the canvas at specified coords (x,y)
		- On the canvas at relative to another flowable
		"""
        start = time.time()

        frame = None
        canvas = None
        if pagemgr is not None:
            frame = pagemgr.getcurrentframe()
            canvas = pagemgr.getcanvas()

        rawtext = self._baseelement.gettext()

        # We allow empty text fields
        ##if len(rawtext.strip()) == 0:
        ##        return None

        basestyle = self._baseelement.getstyle()
        align = basestyle.getattribute('text-align', str)
        font_weight = basestyle.getattribute('font-weight', str)
        font_style = basestyle.getattribute('font-style', str)
        leadingpoints = basestyle.getattribute('leading', int)
        fgdgcolor = basestyle.getattribute('color', str)
        backcolor = basestyle.getattribute('background-color', str)

        wrap = basestyle.getattribute('wrap', int)
        if align == 'center':
            align = TA_CENTER
        elif align == 'right':
            align = TA_RIGHT
        else:
            align = TA_LEFT

        if font_weight == 'bold':
            rawtext = '%s%s%s' % ('<b>', rawtext, '</b>')
        if font_style == 'italic':
            rawtext = '%s%s%s' % ('<i>', rawtext, '</i>')
        elif font_style == 'underline':
            rawtext = '%s%s%s' % ('<u>', rawtext, '</u>')

        rawtext = rawtext.replace('\\n', '<br/>')

        rawtext = '<para>%s</para>' % (rawtext)

        # This might be a special text bound to a rect area
        # The rect is specified in XML and defines a text
        # field to be positionned on canvas at specified coords
        textfield_rect = self._baseelement.getrect()

        kwargs = {
            'name': '',
            'fontName': basestyle.getattribute('font', str),
            'fontSize': basestyle.getattribute('font-size', int),
            'leading': leadingpoints,
            'textColor': fgdgcolor,
            'alignment': align,
        }

        if backcolor.lower() != 'none':
            kwargs['backColor'] = backcolor

        if wrap == 0:
            # No line wrapping
            def_para_style = ParagraphStyle(**kwargs)
            if textfield_rect is None:
                formatted = XPreformatted(rawtext, def_para_style)
            else:
                self._rect = None  # Since there is a text rect, it ovverides the rendering rect
                formatted = self._buildfromrect(canvas, rawtext,
                                                def_para_style, textfield_rect,
                                                wrap)
        else:
            # Line wrapping!
            ##kwargs['wordWrap'] = 'CJK'
            def_para_style = ParagraphStyle(**kwargs)
            if textfield_rect is None:
                formatted = Paragraph(rawtext, def_para_style)
            else:
                self._rect = None  # Since there is a text rect, it ovverides the rendering rect
                formatted = self._buildfromrect(canvas, rawtext,
                                                def_para_style, textfield_rect,
                                                wrap)

        # The rendering rect is specified by an element which is within a table cell
        # This rect is not taken into account if the field is to be positionned on
        # specific coords on canvas (see 'self._baseelement.getrect()')
        if self._rect is not None and self._rect[2] is not None and self._rect[
                3] is not None:
            formatted = KeepInFrame(self._rect[2],
                                    self._rect[3],
                                    content=[formatted],
                                    mode='truncate')

        # If this is a 'normal' text flowable, it is simply displayed with the current frame
        # wherever that may be ...
        if textfield_rect is None and frame is not None:
            frame.addFromList([formatted], canvas)

        end = time.time()
        delta = end - start
        adddeltatimer('text', delta)

        return formatted
Esempio n. 15
0
	def render(self, pagemgr):                
		"""
		Tables seem to be the most expensive elements to render, proportionately
		to their number of cells. TODO: A faster implementation of tables using
		a different XML representation, which could be closer to ReportLab tables
		"""
		def makeRLTablestyle(tableelement, basestyle, styles, start, end):

			color = basestyle.getattribute('color',str)
			backgcolor = basestyle.getattribute('background-color',str)
			vertical_alignment = basestyle.getattribute('vertical-align',str)                        
			top_padding = basestyle.getattribute('top-padding',float)
			bottom_padding = basestyle.getattribute('bottom-padding',float)
			left_padding = basestyle.getattribute('left-padding',float)
			right_padding = basestyle.getattribute('right-padding',float)
			if backgcolor.lower() != 'none':
				styles.append(('BACKGROUND',start, end, backgcolor))
			styles.append(('TOPPADDING',start, end, top_padding))
			styles.append(('BOTTOMPADDING',start, end, bottom_padding))
			styles.append(('LEFTPADDING',start, end, left_padding))
			styles.append(('RIGHTPADDING',start, end, right_padding))
			styles.append(('VALIGN', start, end, vertical_alignment.upper()))
			styles.append(('COLOR', start, end, color))
			if start != end:
				styles.append(('SPAN',start , end))

		frame = None
		canvas = None
		if pagemgr is not None:
			frame = pagemgr.getcurrentframe()
			canvas = pagemgr.getcanvas()

		colwidths, rowheights = self._baseelement.getdimensions()
		rowcells = self._baseelement.getrowcells()

		num_cols = len(colwidths)
		num_rows = len(rowheights)
		ori_num_cols = num_cols

		data_array = []
		for i in range(num_rows):
			data_array.append(num_cols*[None])

		basestyle = self._baseelement.getstyle()
		border = basestyle.getattribute('border',float)
		grid = basestyle.getattribute('grid',float)
		color = basestyle.getattribute('color',str)
		border_frame = basestyle.getattribute('frame',str)

		stylecmds = []

		if border_frame == 'above':
			stylecmds.append(('LINEABOVE',(0,0),(-1,0), border, color))
		elif border_frame == 'below':
			stylecmds.append(('LINEBELOW',(0,-1),(-1,-1), border, color))
		elif border_frame == 'left':
			stylecmds.append(('LINEBEFORE',(0,0),(0,-1), border, color))
		elif border_frame == 'right':
			stylecmds.append(('LINEAFTER',(-1,0),(-1,-1), border, color))
		elif border > 0.0:
			stylecmds.append(('BOX',(0,0),(-1,-1), border, color))

		if grid > 0.0:
			stylecmds.append(('INNERGRID',(0,0),(-1,-1), grid, color))


		start = 0
		delta_loop = 0
		start = time.time()

		row_index = 0
		# Go through each row for the table
		for row in rowcells:                        
			col_index = 0
			cell_index = 0                             
			# Each row is composed of cells which may span several columns, rows
			for cell in row:   
				addstyle = True
				subrow_index = 0
				# Each cell possibly spans several rows
				for subrow in cell:                                        
					subcol_index = 0
					# Each cell possibly spans several columns
					for subcol in subrow:
						docelementname  = subcol.__class__.__name__
						# This is the object contained withing a table cell
						rlobject = rlObjectInstance(docelementname, subcol) 
						final_row_index = row_index+subrow_index
						final_col_index = col_index+subcol_index
						row_span = len(cell)
						col_span = len(subrow)
						cell_width = None
						cell_height = None

						# We dont want to overwrite existing cells from previous rows.
						# Skip until we find empty cells. Sometimes this results in
						# a table that needs to be adjusted and we'll add extra columns.
						while data_array[final_row_index][final_col_index] is not None:
							subcol_index += 1
							final_col_index = col_index+subcol_index
							if final_col_index >= len(data_array[final_row_index]):
								data_array[final_row_index].append(None)
								num_cols += 1

						#Calculate actual width, height of a cell. Cells can span several columns, rows
						if colwidths[col_index] is not None:
							cell_width = col_span*colwidths[col_index]# TODO: Test we dont get out bounds of colwidths
						if rowheights[row_index] is not None:
							cell_height = row_span*rowheights[row_index]# TODO: Test we dont get out bounds of rowheights

						# Give the object contained with the table cell it's boundaries
						rlobject.setrect((0,0,cell_width,cell_height))

						loop_start = time.time()                                                
						data_array[final_row_index][final_col_index] = rlobject.render(None) # render the table cell object                                              
						loop_end = time.time()
						delta_loop += (loop_end-loop_start)

						basestyle = rlobject._baseelement.getstyle()                                                

						if addstyle == True:
							addstyle = False
							makeRLTablestyle( rlobject, basestyle, stylecmds,
											  (final_col_index, final_row_index),
											  (final_col_index+col_span-1,final_row_index+row_span-1))   
						subcol_index += 1
					subrow_index += 1
				col_index += len(subrow)
				cell_index += 1
			row_index += 1

		# if the table needs to be adjusted and add extra colum(s)
		if num_cols > ori_num_cols:
			for i in range(num_rows):
				adjust_cols = num_cols - len(data_array[i])
				new_cols = adjust_cols*[None]
				data_array[i] += new_cols

		style = TableStyle(stylecmds)
		table = Table(data_array, colwidths, rowheights, style)

		if frame is not None:
			frame.addFromList([table], canvas)                

		end = time.time()
		delta = end - start
		adddeltatimer('table',delta-(delta_loop))

		return table
Esempio n. 16
0
	def render(self, pagemgr):
		"""
		Texts are either XPreformat class instances or Paragraph class instances
		from Reportlab package. An text can be positionned:
		- Within a frame
		- On the canvas at specified coords (x,y)
		- On the canvas at relative to another flowable
		"""
		start = time.time()

		frame = None
		canvas = None
		if pagemgr is not None:
			frame = pagemgr.getcurrentframe()
			canvas = pagemgr.getcanvas()

		rawtext = self._baseelement.gettext()   

		# We allow empty text fields
		##if len(rawtext.strip()) == 0:                        
		##        return None

		basestyle = self._baseelement.getstyle()
		align = basestyle.getattribute('text-align',str)
		font_weight = basestyle.getattribute('font-weight',str)
		font_style = basestyle.getattribute('font-style',str)
		leadingpoints = basestyle.getattribute('leading',int)
		fgdgcolor = basestyle.getattribute('color',str)
		backcolor = basestyle.getattribute('background-color',str)

		wrap = basestyle.getattribute('wrap',int)
		if align == 'center':
			align = TA_CENTER
		elif align == 'right':
			align = TA_RIGHT
		else:
			align = TA_LEFT                

		if font_weight == 'bold':
			rawtext = '%s%s%s'%('<b>',rawtext,'</b>')                        
		if font_style == 'italic':
			rawtext = '%s%s%s'%('<i>',rawtext,'</i>')
		elif font_style == 'underline':
			rawtext = '%s%s%s'%('<u>',rawtext,'</u>')

		rawtext = rawtext.replace('\\n','<br/>')

		rawtext = '<para>%s</para>'%(rawtext)

		# This might be a special text bound to a rect area
		# The rect is specified in XML and defines a text
		# field to be positionned on canvas at specified coords
		textfield_rect = self._baseelement.getrect()

		kwargs = {'name':'', 
				  'fontName':basestyle.getattribute('font',str),
				  'fontSize':basestyle.getattribute('font-size',int),
				  'leading':leadingpoints,
				  'textColor':fgdgcolor,
				  'alignment':align,}

		if backcolor.lower() != 'none':
			kwargs['backColor'] = backcolor

		if wrap == 0:
			# No line wrapping
			def_para_style = ParagraphStyle(**kwargs)   
			if textfield_rect is None:
				formatted = XPreformatted(rawtext, def_para_style)
			else:
				self._rect = None # Since there is a text rect, it ovverides the rendering rect
				formatted = self._buildfromrect(canvas, rawtext, def_para_style, textfield_rect, wrap)                                
		else:
			# Line wrapping!
			##kwargs['wordWrap'] = 'CJK'
			def_para_style = ParagraphStyle(**kwargs) 
			if textfield_rect is None:
				formatted = Paragraph(rawtext, def_para_style)
			else:
				self._rect = None # Since there is a text rect, it ovverides the rendering rect
				formatted = self._buildfromrect(canvas, rawtext, def_para_style, textfield_rect, wrap)



		# The rendering rect is specified by an element which is within a table cell
		# This rect is not taken into account if the field is to be positionned on
		# specific coords on canvas (see 'self._baseelement.getrect()')
		if self._rect is not None and self._rect[2] is not None and self._rect[3] is not None:                        
			formatted = KeepInFrame( self._rect[2], self._rect[3], content=[formatted], mode='truncate')

		# If this is a 'normal' text flowable, it is simply displayed with the current frame
		# wherever that may be ...
		if textfield_rect is None and frame is not None:
			frame.addFromList([formatted], canvas)

		end = time.time()
		delta = end - start
		adddeltatimer('text',delta)

		return formatted