def resizePDF(self, pdf, x = 0, y = 0): self.rect = Rect(x,y,x,y) dx = pdf.get_string_width(' ') dx *= self.style[1] rectList = [] width = 0.0 style = '' for item in self.items: if item: if hasattr(item,'style') and item.style != style: setFontPDF(pdf, item.style, self.styles) item.resizePDF(pdf,x,y) rectList.append(item.rect) width += item.rect.width() + dx rect.alignLeft(rectList, x, x+width, dx) for r in rectList: self.rect.unite(r) self.refit()
def resizePDF(self, pdf, x = 0, y = 0): """Calculate the sizes of all items in the paragraph. Args: pdf (FPDF): the pdf object. x, y (float): coordinates of the top-left corner of the paragraph relative to the page's margins. """ style = self.style setFontPDF(pdf, style, self.styles) if self.width <= 0: self.width = pdf.fw - pdf.l_margin - pdf.r_margin - x y += pdf.t_margin xstart = pdf.l_margin + x xend = pdf.l_margin + x + self.width else: xstart = x xend = x + self.width self.space = pdf.get_string_width(' ') self.lineHeight = self.getLineHeight(pdf) * 1.2 if self.t_margin < 0: self.t_margin = self.lineHeight * 0.5 if self.b_margin < 0: self.b_margin = self.lineHeight y += self.t_margin rectList = [] # resize individual items and collect their Rects for item in self.items: if item: if item.style != style: style = item.style self.setFontPDF(pdf, item) item.resizePDF(pdf, 0, y) rectList.append( item.rect ) # position the items on the page alignFunction = textAlignments[self.textAlignment] onFirstLine = True while len( rectList ) > 0: n = alignFunction(rectList, xstart, xend, self.space) if n == len( rectList ) and self.textAlignment == 'j': rect.alignLeft(rectList, xstart, xend, self.space) if not onFirstLine: for r in rectList: r.translate(rect.Point(0,self.lineHeight)) # for r in rectList[:n]: # self.rect.unite(r) onFirstLine = False del rectList[:n] #self.rect = Rect( xstart, y, xend, y ) self.rect = self.getUnionRect() # add top and bottom margins self.margins = rect.Point(0,self.t_margin) self.rect.adjust(rect.Point(0,-self.t_margin), rect.Point(0,self.b_margin)) self.refit()