mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-10-31 16:07:30 +03:00 
			
		
		
		
	hoist tests out of try/except, use feature detection
This commit is contained in:
		
							parent
							
								
									42831098ac
								
							
						
					
					
						commit
						f371ca07f4
					
				|  | @ -1,139 +1,129 @@ | ||||||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||||||
| from helper import unittest, PillowTestCase | from helper import unittest, PillowTestCase | ||||||
| from PIL import Image | from PIL import Image, ImageDraw, ImageFont, features | ||||||
| from PIL import ImageDraw, ImageFont |  | ||||||
| 
 | 
 | ||||||
| #check if raqm installed |  | ||||||
| have_raqm = ImageFont.core.have_raqm |  | ||||||
| 
 | 
 | ||||||
| FONT_SIZE = 20 | FONT_SIZE = 20 | ||||||
| FONT_PATH = "Tests/fonts/DejaVuSans.ttf" | FONT_PATH = "Tests/fonts/DejaVuSans.ttf" | ||||||
| 
 | 
 | ||||||
| try: | @unittest.skipUnless(features.check('raqm'), "Raqm Library is not installed.") | ||||||
|     from PIL import ImageFont | class TestImagecomplextext(PillowTestCase): | ||||||
| 
 | 
 | ||||||
|     ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) |     def test_english(self): | ||||||
|     im = Image.new(mode='RGB', size=(300, 100)) |         #smoke test, this should not fail | ||||||
|     draw = ImageDraw.Draw(im) |         ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) | ||||||
|     draw.text((0, 0), 'TEST', font=ttf, fill=500, direction='ltr') |         im = Image.new(mode='RGB', size=(300, 100)) | ||||||
|     @unittest.skipIf(not have_raqm, "Raqm Library is not installed !") |         draw = ImageDraw.Draw(im) | ||||||
|     class TestImagecomplextext(PillowTestCase): |         draw.text((0, 0), 'TEST', font=ttf, fill=500, direction='ltr') | ||||||
|         def test_complex_text(self): |  | ||||||
|             ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) |  | ||||||
|          |          | ||||||
|             im = Image.new(mode='RGB', size=(300, 100)) |  | ||||||
|             draw = ImageDraw.Draw(im) |  | ||||||
|             draw.text((0, 0), 'اهلا عمان', font=ttf, fill=500) |  | ||||||
|      |      | ||||||
|             target = 'Tests/images/test_text.png' |     def test_complex_text(self): | ||||||
|             target_img = Image.open(target) |         ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) | ||||||
| 
 | 
 | ||||||
|             self.assert_image_similar(im, target_img, .5) |         im = Image.new(mode='RGB', size=(300, 100)) | ||||||
|  |         draw = ImageDraw.Draw(im) | ||||||
|  |         draw.text((0, 0), 'اهلا عمان', font=ttf, fill=500) | ||||||
| 
 | 
 | ||||||
|         def test_y_offset(self): |         target = 'Tests/images/test_text.png' | ||||||
|             ttf = ImageFont.truetype("Tests/fonts/NotoNastaliqUrdu-Regular.ttf", FONT_SIZE) |         target_img = Image.open(target) | ||||||
| 
 | 
 | ||||||
|             im = Image.new(mode='RGB', size=(300, 100)) |         self.assert_image_similar(im, target_img, .5) | ||||||
|             draw = ImageDraw.Draw(im) |  | ||||||
|             draw.text((0, 0), 'العالم العربي', font=ttf, fill=500) |  | ||||||
| 
 | 
 | ||||||
|             target = 'Tests/images/test_y_offset.png' |     def test_y_offset(self): | ||||||
|             target_img = Image.open(target) |         ttf = ImageFont.truetype("Tests/fonts/NotoNastaliqUrdu-Regular.ttf", FONT_SIZE) | ||||||
| 
 | 
 | ||||||
|             self.assert_image_similar(im, target_img, .5) |         im = Image.new(mode='RGB', size=(300, 100)) | ||||||
|  |         draw = ImageDraw.Draw(im) | ||||||
|  |         draw.text((0, 0), 'العالم العربي', font=ttf, fill=500) | ||||||
| 
 | 
 | ||||||
|         def test_complex_unicode_text(self): |         target = 'Tests/images/test_y_offset.png' | ||||||
|             ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) |         target_img = Image.open(target) | ||||||
| 
 | 
 | ||||||
|             im = Image.new(mode='RGB', size=(300, 100)) |         self.assert_image_similar(im, target_img, .5) | ||||||
|             draw = ImageDraw.Draw(im) |  | ||||||
|             draw.text((0, 0), u'السلام عليكم', font=ttf, fill=500) |  | ||||||
| 
 | 
 | ||||||
|             target = 'Tests/images/test_complex_unicode_text.png' |     def test_complex_unicode_text(self): | ||||||
|             target_img = Image.open(target) |         ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) | ||||||
| 
 | 
 | ||||||
|             self.assert_image_similar(im, target_img, .5) |         im = Image.new(mode='RGB', size=(300, 100)) | ||||||
|  |         draw = ImageDraw.Draw(im) | ||||||
|  |         draw.text((0, 0), u'السلام عليكم', font=ttf, fill=500) | ||||||
| 
 | 
 | ||||||
|         def test_text_direction_rtl(self): |         target = 'Tests/images/test_complex_unicode_text.png' | ||||||
|             ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) |         target_img = Image.open(target) | ||||||
| 
 | 
 | ||||||
|             im = Image.new(mode='RGB', size=(300, 100)) |         self.assert_image_similar(im, target_img, .5) | ||||||
|             draw = ImageDraw.Draw(im) |  | ||||||
|             draw.text((0, 0), 'English عربي', font=ttf, fill=500, direction='rtl') |  | ||||||
| 
 | 
 | ||||||
|             target = 'Tests/images/test_direction_rtl.png' |     def test_text_direction_rtl(self): | ||||||
|             target_img = Image.open(target) |         ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) | ||||||
| 
 | 
 | ||||||
|             self.assert_image_similar(im, target_img, .5) |         im = Image.new(mode='RGB', size=(300, 100)) | ||||||
|  |         draw = ImageDraw.Draw(im) | ||||||
|  |         draw.text((0, 0), 'English عربي', font=ttf, fill=500, direction='rtl') | ||||||
| 
 | 
 | ||||||
|         def test_text_direction_ltr(self): |         target = 'Tests/images/test_direction_rtl.png' | ||||||
|             ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) |         target_img = Image.open(target) | ||||||
| 
 | 
 | ||||||
|             im = Image.new(mode='RGB', size=(300, 100)) |         self.assert_image_similar(im, target_img, .5) | ||||||
|             draw = ImageDraw.Draw(im) |  | ||||||
|             draw.text((0, 0),  'سلطنة عمان Oman', font=ttf, fill=500, direction='ltr') |  | ||||||
| 
 | 
 | ||||||
|             target = 'Tests/images/test_direction_ltr.png' |     def test_text_direction_ltr(self): | ||||||
|             target_img = Image.open(target) |         ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) | ||||||
| 
 | 
 | ||||||
|             self.assert_image_similar(im, target_img, .5) |         im = Image.new(mode='RGB', size=(300, 100)) | ||||||
|  |         draw = ImageDraw.Draw(im) | ||||||
|  |         draw.text((0, 0),  'سلطنة عمان Oman', font=ttf, fill=500, direction='ltr') | ||||||
| 
 | 
 | ||||||
|         def test_text_direction_rtl2(self): |         target = 'Tests/images/test_direction_ltr.png' | ||||||
|             ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) |         target_img = Image.open(target) | ||||||
| 
 | 
 | ||||||
|             im = Image.new(mode='RGB', size=(300, 100)) |         self.assert_image_similar(im, target_img, .5) | ||||||
|             draw = ImageDraw.Draw(im) |  | ||||||
|             draw.text((0, 0), 'Oman سلطنة عمان', font=ttf, fill=500, direction='rtl') |  | ||||||
| 
 | 
 | ||||||
|             target = 'Tests/images/test_direction_ltr.png' |     def test_text_direction_rtl2(self): | ||||||
|             target_img = Image.open(target) |         ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) | ||||||
| 
 | 
 | ||||||
|             self.assert_image_similar(im, target_img, .5) |         im = Image.new(mode='RGB', size=(300, 100)) | ||||||
|  |         draw = ImageDraw.Draw(im) | ||||||
|  |         draw.text((0, 0), 'Oman سلطنة عمان', font=ttf, fill=500, direction='rtl') | ||||||
| 
 | 
 | ||||||
|         def test_ligature_features(self): |         target = 'Tests/images/test_direction_ltr.png' | ||||||
|             ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) |         target_img = Image.open(target) | ||||||
| 
 | 
 | ||||||
|             im = Image.new(mode='RGB', size=(300, 100)) |         self.assert_image_similar(im, target_img, .5) | ||||||
|             draw = ImageDraw.Draw(im) |  | ||||||
|             draw.text((0, 0), 'filling', font=ttf, fill=500, features=['-liga']) |  | ||||||
| 
 | 
 | ||||||
|             target = 'Tests/images/test_ligature_features.png' |     def test_ligature_features(self): | ||||||
|             target_img = Image.open(target) |         ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) | ||||||
| 
 | 
 | ||||||
|             self.assert_image_similar(im, target_img, .5) |         im = Image.new(mode='RGB', size=(300, 100)) | ||||||
|  |         draw = ImageDraw.Draw(im) | ||||||
|  |         draw.text((0, 0), 'filling', font=ttf, fill=500, features=['-liga']) | ||||||
| 
 | 
 | ||||||
|         def test_kerning_features(self): |         target = 'Tests/images/test_ligature_features.png' | ||||||
|             ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) |         target_img = Image.open(target) | ||||||
| 
 | 
 | ||||||
|             im = Image.new(mode='RGB', size=(300, 100)) |         self.assert_image_similar(im, target_img, .5) | ||||||
|             draw = ImageDraw.Draw(im) |  | ||||||
|             draw.text((0, 0), 'TeToAV', font=ttf, fill=500, features=['-kern']) |  | ||||||
| 
 | 
 | ||||||
|             target = 'Tests/images/test_kerning_features.png' |     def test_kerning_features(self): | ||||||
|             target_img = Image.open(target) |         ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) | ||||||
| 
 | 
 | ||||||
|             self.assert_image_similar(im, target_img, .5) |         im = Image.new(mode='RGB', size=(300, 100)) | ||||||
|  |         draw = ImageDraw.Draw(im) | ||||||
|  |         draw.text((0, 0), 'TeToAV', font=ttf, fill=500, features=['-kern']) | ||||||
| 
 | 
 | ||||||
|         def test_arabictext_features(self): |         target = 'Tests/images/test_kerning_features.png' | ||||||
|             ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) |         target_img = Image.open(target) | ||||||
| 
 | 
 | ||||||
|             im = Image.new(mode='RGB', size=(300, 100)) |         self.assert_image_similar(im, target_img, .5) | ||||||
|             draw = ImageDraw.Draw(im) |  | ||||||
|             draw.text((0, 0), 'اللغة العربية', font=ttf, fill=500, features=['-fina','-init','-medi']) |  | ||||||
| 
 | 
 | ||||||
|             target = 'Tests/images/test_arabictext_features.png' |     def test_arabictext_features(self): | ||||||
|             target_img = Image.open(target) |         ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) | ||||||
| 
 | 
 | ||||||
|             self.assert_image_similar(im, target_img, .5) |         im = Image.new(mode='RGB', size=(300, 100)) | ||||||
|  |         draw = ImageDraw.Draw(im) | ||||||
|  |         draw.text((0, 0), 'اللغة العربية', font=ttf, fill=500, features=['-fina','-init','-medi']) | ||||||
| 
 | 
 | ||||||
| except ImportError: |         target = 'Tests/images/test_arabictext_features.png' | ||||||
|     class TestImagecomplextext(PillowTestCase): |         target_img = Image.open(target) | ||||||
|         def test_skip(self): | 
 | ||||||
|             self.skipTest("ImportError") |         self.assert_image_similar(im, target_img, .5) | ||||||
| except KeyError: |  | ||||||
|     class TestImagecomplextext(PillowTestCase): |  | ||||||
|         def test_skip(self): |  | ||||||
|             self.skipTest("KeyError") |  | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     unittest.main() |     unittest.main() | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user