def __init__(self, content, contentSize, color=COLOR["LIGHT"]["MAIN_COLOR"], font_size=20, **kwargs): super(ShortenText, self).__init__(**kwargs) texture = Short(text=content, color=color, text_size=[contentSize[0], None], shorten=True, shorten_from="right", split_str="", font_size=font_size) texture.refresh() if texture.texture.height < contentSize[1]: texture.text_size = texture.texture.size else: texture.text_size = contentSize texture.max_lines = 5 texture.render() texture.refresh() self.texture = Image(texture=texture.texture, pos_hint={ "x": 0, "top": 1 }) self.size_hint = [None, None] self.size = texture.texture.size self.add_widget(self.texture)
def _calculate_optimum_font_size(self): #cw = self._calculate_widths() words = list(self._iterate_words()) text = ' '.join([x.text for x in words]) font_size = WORD_FONTSIZE corelabel = CoreLabel(text=text, font_name=words[0].font_name) padding = words[0].padding * len(words) # too many words ? if padding > self.width / 2: padding = int(self.width / 2 / len(words)) for word in words: word.padding = padding else: for word in words: word.padding = Word.padding.defaultvalue while True: corelabel.options['font_size'] = sp(font_size) w, h = corelabel.render() if w < self.width - padding: break font_size -= 1 font_size = sp(font_size) if font_size != words[0].font_size: for word in words: word.font_size = font_size word.texture_update()