def split_text(self, text: str, typo: bool = True): """Split the text so that it fits into the images""" if typo: text_to_process = ru_typus(text).splitlines() else: text_to_process = text.splitlines() parts = [] lines = [] text_y = self.base_font_height for i_line in text_to_process: if self.font.getsize(i_line)[0] <= self._max_width: lines.append(i_line.replace(NNBSP, NBSP)) text_y += self.base_font_height + 2 if text_y > self.height - self.base_font_height * 2: text_y = self.base_font_height parts.append(lines) lines = [] else: words = i_line.replace(NNBSP, NBSP).split(' ') i = 0 while i < len(words): line = '' while i < len(words) and self.font.getsize( line + words[i])[0] <= self._max_width: line = line + words[i] + " " i += 1 if not line: line = words[i] i += 1 lines.append(line) text_y += self.base_font_height + 2 if text_y > self.height - self.base_font_height * 2: text_y = self.base_font_height parts.append(lines) lines = [] parts.append(lines) return parts
def render_item(meta: dict, item: dict): created = meta[item['id']] item.update( price=item['price'] and format_price(item['price']), title=ru_typus(item['title'].strip('.')), norm=normalize(item['title']), new=created >= NEW_INT, created=created, ) # This must be called after all updates # to create valid tags item_tags = get_tags(item) item.update( tags=item_tags, for_kids=item_tags[3] & KIDS, ) # fixme: time for pydantic item.pop('new') return item
def rutypography(value): return ru_typus(value)
def test_debug(self): self.assertEqual(ru_typus('2mm', debug=True), '2_mm')
def test_empty(self, mock_process): self.assertEqual(ru_typus(''), '') mock_process.assert_not_called()
def testcase(text, test, debug=False): return self.assertEqual(ru_typus(text, debug), test)