def test_fit_text_smoke_test_one_word(self): font_path = "static/fonts/PatrickHand-Regular.ttf" font_size = 10 text = "word" font = ImageFont.truetype(font_path, font_size) text_width, text_height = font.getsize(text) new_font_size, lines, new_text_width, new_text_height = TextBoxDrawer.fit_text( text, text_width, text_height, font_path, font_size, 4)
def test_fit_text_valid_fontsize(self): font_path = "static/fonts/PatrickHand-Regular.ttf" font_size = 10 text = "This is a string" font = ImageFont.truetype(font_path, font_size * 2) text_width, text_height = font.getsize(text) new_font_size, lines, new_text_width, new_text_height = TextBoxDrawer.fit_text( text, text_width, text_height, font_path, font_size, 4) self.assertEqual(new_font_size, font_size)
def test_fit_text_multiple_decrease_loop(self): font_path = "static/fonts/PatrickHand-Regular.ttf" font_size = 100 text = "This is a string" font = ImageFont.truetype(font_path, font_size) text_width, text_height = font.getsize(text) new_font_size, lines, new_text_width, new_text_height = TextBoxDrawer.fit_text( text, text_width * 0.5, # Force many decrease loops text_height, font_path, font_size, 4) self.assertTrue(new_font_size < font_size)
def test_fit_text_one_line(self): font_path = "static/fonts/PatrickHand-Regular.ttf" font_size = 50 font = ImageFont.truetype(font_path, font_size) text = "This is a string" text_width, text_height = font.getsize(text) new_font_size, lines, new_text_width, new_text_height = TextBoxDrawer.fit_text( text, text_width * 2, text_height * 2, font_path, font_size, 4) self.assertEqual(1, len(lines)) self.assertEqual(text, lines[0]) self.assertEqual(font_size, new_font_size) self.assertIsInstance(new_text_width, int) self.assertIsInstance(new_text_height, int)
def test_fit_text_decrease_fontsize(self): font_path = "static/fonts/PatrickHand-Regular.ttf" font_size = 50 font = ImageFont.truetype(font_path, font_size) text = "This is a string" text_width, text_height = font.getsize(text) new_font_size, lines, new_text_width, new_text_height = TextBoxDrawer.fit_text( text, text_width * 0.8, # Make box slightly narrower than text, to force 2 lines text_height, # Make box too short to force smaller text font_path, font_size, 4) self.assertEqual(text, "".join(lines)) self.assertTrue(new_font_size < font_size) self.assertTrue(new_font_size > 1) self.assertIsInstance(new_text_width, int) self.assertIsInstance(new_text_height, int)