def draw(self, canvas): top = canvas.h() if self.head: top -= self.head.height() head_canvas = box.canvas(canvas, 0, top, canvas.w(), top, border=False, clip=False) canvas.append(head_canvas) used_space, done = self.head.draw(head_canvas) self.head.reset() while self.rows: req = self.rows[0].height() if len(self.rows) == 2: req += self.rows[1].height() if top < req: return canvas.h() - top, False top -= self.rows[0].height() row_canvas = box.canvas(canvas, 0, top, canvas.w(), top, border=False, clip=False) canvas.append(row_canvas) self.rows[0].draw(row_canvas) del self.rows[0] return canvas.h() - top, True
def draw(self, canvas): head = self.head_object() if head is not None: head_canvas = box.canvas(canvas, canvas.x(), canvas.y(), canvas.w(), canvas.h()) header_height, done = head.draw(head_canvas) canvas.append(head_canvas) head.reset() if not done: raise ValueError("Not enough space to draw the header.") else: header_height = 0 self.start = False subcanvas = box.canvas(canvas, 0, 0, canvas.w(), canvas.h() - header_height, border=False, clip=False) canvas.append(subcanvas) space_used, done = self.body.draw(subcanvas) return ( header_height + space_used, done, )
def canvas(self, margin=0, border=False, clip=False): """ Return a canvas object for the whole page except a predefined set of margins. The margin parameter may be either: - an integer - all four margins the same - a pair - ( horizontal, vertical, ) - a 4-tuple - ( left, top, right, bottom, ) """ if type(margin) == TupleType: if len(margin) == 2: h, v = margin margin = ( h, v, h, v, ) else: m = float(margin) margin = ( m, m, m, m, ) l, t, r, b = margin from t4.psg.drawing.box import canvas ret = canvas(self, l, b, self.w() - r - l, self.h() - t - b, border, clip) return ret
def draw(self, canvas): if self.start: margin_top = self.style.margin_top else: margin_top = 0 inner = box.canvas(canvas, self.style.margin_left, 0, canvas.w()- self.style.h_margin(), canvas.h()- margin_top, border=False, clip=False) used_space, done = self.subsection.draw(inner) if self.start: used_space += self.style.margin_top self.start = False if done: used_space += self.style.margin_bottom if used_space > canvas.h(): used_space = canvas.h() canvas.append(inner) return ( used_space, done, )
def column_factory(document): while True: page = document.page("a4", None) canvas = box.canvas(page, mm(20), mm(20), page.w() - mm(40), page.h() - mm(40)) page.append(canvas) yield engine.rectangular_column(canvas)
def column_factory(document): while True: page = document.page("a4", None) h = mm(242.99) left = rectangular_column( canvas(page, mm(40), mm(25), mm(75), h, border=True)) right = rectangular_column( canvas(page, mm(124.75), mm(25), mm(75), h, border=True)) page.append(left.box()) yield left page.append(right.box()) yield right
def draw(self, canvas): if self.style.background_color: ax, ay = 0, 0 bx, by = canvas.w(), self.height() canvas._( "gsave", "newpath", ax, ay, "moveto", bx, ay, "lineto", bx, by, "lineto", ax, by, "lineto", "closepath", self.style.background_color, "fill grestore") left = 0 for cell in self.cells: cell_canvas = box.canvas(canvas, left, 0, cell.width(), self.height(), border=False, clip=False) cell.draw(cell_canvas) canvas.append(cell_canvas) left += cell.width() return self.height(), True
def draw(self, canvas): inner = box.canvas(canvas, 0, 0, self.width() + self.style.h_fringe(), canvas.h(), border=False, clip=False) canvas.append(inner) return self.subsection.draw(inner)
def draw(self, canvas): if len(self.sections) == 0: raise ValueError("Container is empty.") space_used = 0 idx = 0 while self.sections: inner = box.canvas(canvas, 0, 0, canvas.w(), canvas.h() - space_used, border=False, clip=False) canvas.append(inner) used, done = car(self.sections).draw(inner) space_used += used if done: self.sections = cdr(self.sections) # If the remaining space on this canvas is smaller # than the next sections's minimum height, # we're done here. if len(self.sections) > 0 and canvas.h() - space_used < \ car(self.sections).minimum_height(null_canvas(canvas)): return space_used, False else: return space_used, False return space_used, True
def predraw(self, canvas): if self.canvas is not None: return inner_canvas = box.canvas(canvas, 0, 0, self.table.width()-self.style.h_fringe(), 10000, border=False, clip=False) self.section.minimum_height(null_canvas(inner_canvas)) space_used, done = self.section.draw(inner_canvas) self.canvas = box.canvas(canvas, 0, -(inner_canvas.h() - space_used), self.table.width(), space_used, border=False, clip=False) self.canvas.append(inner_canvas)
def render(self, layout_box): if self.splitable: # Check if there is room in the layout box for at least one line min_height = self.style.v_padding() + self.style.v_margin() + \ self.style.font_size * self.style.line_height else: min_height = self.height(layout_box.w()) if layout_box.y_cursor() < min_height: return self needed_height = self.height(layout_box.w()) if layout_box.y_cursor() < needed_height: height = layout_box.y_cursor() else: height = needed_height margin_box = canvas( layout_box, self.style.margin_left, layout_box.y_cursor() - height + self.style.margin_bottom, layout_box.w() - self.style.h_margin(), height - self.style.v_margin(), debug.verbose) layout_box.append(margin_box) self.border_and_background(margin_box) if self.style.hv_padding() == 0: x = 0 y = 0 w = margin_box.w() h = margin_box.h() else: x = self.style.padding_left y = self.style.padding_bottom w = margin_box.w() - self.style.h_padding() h = margin_box.h() - self.style.v_padding() if self.style.font.metrics.descender is not None: y -= float(self.style.font.metrics.descender) \ * self.style.font_size / 1000 # Create a textbox rest = self.typeset(margin_box, x, y, w, h) if rest != "": rest = div(rest, self.style) else: rest = None layout_box.y_advance(min(height, layout_box.y_cursor())) return rest
def draw(self, canvas): used_space = 0 if self.start: height = canvas.h() - self.style.border_top used_space = self.style.border_top else: height = canvas.h() inner = box.canvas(canvas, self.style.border_left, 0, canvas.w() - self.style.h_border(), height, border=False, clip=False) used, done = self.subsection.draw(inner) used_space += used if done: used_space += self.style.border_bottom if used_space > canvas.h(): used_space = canvas.h() if self.style.border_color: if self.start and self.style.border_top: self.rect( canvas, 0, canvas.h(), canvas.w(), canvas.h()-self.style.border_top ) if self.style.border_left: self.rect( canvas, 0, canvas.h(), self.style.border_left, canvas.h()-used_space ) if self.style.border_right: self.rect( canvas, canvas.w(), canvas.h(), canvas.w() - self.style.border_right, canvas.h()- used_space ) if done and self.style.border_bottom: self.rect( canvas, canvas.w() - self.style.border_right, canvas.h() - used_space, 0, canvas.h() - used_space + self.style.border_bottom) canvas.append(inner) self.start = False return ( used_space, done, )
def thumb_box_factory(output_document, paper_size_name, margin_width, thumbs_per_page, thumb_width, thumb_height,thumb_spacing, verbose, **kw): while True: page = output_document.page(page_size=paper_size_name) canvas = page.canvas(margin_width, border=False) for a in range(thumbs_per_page-1, -1, -1): thumb_box = box.canvas(canvas, 0, a * ( thumb_height + thumb_spacing ), thumb_width, thumb_height, border=verbose, clipping=True) canvas.append(thumb_box) yield thumb_box
def draw(self, canvas): if self.style.vertical_align == "top": padding_top = 0.0 elif self.style.vertical_align == "middle": padding_top = ( canvas.h() - self.height() ) / 2 elif self.style.vertical_align == "bottom": padding_top = ( canvas.h() - self.height() ) else: ValueError(self.style.vertical_align) new_canvas = box.canvas(canvas, 0, canvas.h() - padding_top - self.cell.canvas.h(), self.cell.width(), self.cell.canvas.h(), border=False, clip=False) canvas.append(new_canvas) new_canvas.append(self.cell.canvas) return self.cell.canvas.h(), True
def draw(self, canvas): if self.start: height = canvas.h() - self.style.padding_top used_space = self.style.padding_top else: height = canvas.h() used_space = 0 inner = box.canvas(canvas, self.style.padding_left, 0, canvas.w() - self.style.h_padding(), height, border=False, clip=False) used, done = self.subsection.draw(inner) used_space += used if done: used_space += self.style.padding_bottom if used_space > canvas.h(): used_space = canvas.h() if self.style.background_color: ax, ay = 0, canvas.h() bx, by = canvas.w(), canvas.h() - used_space canvas._( "gsave", "newpath", ax, ay, "moveto", bx, ay, "lineto", bx, by, "lineto", ax, by, "lineto", "closepath", self.style.background_color, "fill", "grestore" ) canvas.append(inner) self.start = False return ( used_space, done, )
def add_section(self, section): # The test_canvas will not be connected and any PS created will # be discarded. minimum = section.minimum_height(null_canvas(self._canvas)) if minimum > self.remainder(): if self.is_empty(): # It these columns are something else than whole # page-sized columns, this may not be what we want to # do. raise ValueError("Section requested more vertical " "space than available in an empty " "column.") # We're full by definition self._is_full = True # And return the section as only partially rendered. return section if self.remainder() == self.height(): canvas = self._canvas else: canvas = box.canvas(self._canvas, 0, 0, self._canvas.w(), self.remainder(), border=False, clip=False) self._canvas.append(canvas) self._is_empty = False used_vertical_space, done = section.draw(canvas) if used_vertical_space > self.remainder(): raise ValueError("Section used more vertical space than provided " "by the column.") self._remainder -= used_vertical_space if not done: return section else: return None
def null_canvas(canvas): return box.canvas(canvas, 0, 0, canvas.w(), canvas.h(), False, False)