def _trunc_label(self, text, period_size): """ trunc a period's label depending on the size of the period "period_size" is an integer: the number of scale items between period's start and period's end dates """ if text == None : return "" if len(text) <= 1: return text # find the width of a column columns_width = self._compute_columns_widths(self.table) column_width = columns_width[1] period_width = column_width * period_size string_width = stringWidth(text, self.DEFAULT_FONT, self.DEFAULT_FONT_SIZE) margin_width = stringWidth(" .", self.DEFAULT_FONT, self.DEFAULT_FONT_SIZE) cpt = len(text) #if the width of the text is greater than the width of the period, try to remove a char at the end and start again. #TOOD: could be improved to do less loops (dichotomic tests...) tmp_text = text while cpt > 1 and string_width + margin_width > period_width: cpt -= 1 #string from db are encoded in utf8.... tmp_text = text.decode('utf-8') #... but text operation are ascii... tmp_text= tmp_text[0:cpt] # ... and reportlab work in utf8 too.... tmp_text = tmp_text.encode('utf-8') string_width = stringWidth(tmp_text, self.DEFAULT_FONT, self.DEFAULT_FONT_SIZE) result = tmp_text #add a final . to truncated texts if cpt < len(text): result += "." return result
def _compute_columns_widths(self): """construct the list of column widths. """ columns_width = [] if self.title != None: title_length = stringWidth(self.title, self.DEFAULT_FONT, self.DEFAULT_FONT_SIZE)+10 #add 10 to include margins columns_width.append(title_length) total_width = self.width-title_length if len(self.items_order): columns_width += len(self.items_order) * [total_width / len(self.items_order)] else: columns_width = total_width return columns_width