77from .helper import (
88 assert_image_equal_tofile ,
99 assert_image_similar_tofile ,
10+ has_feature_version ,
1011 skip_unless_feature ,
1112)
1213
@@ -104,11 +105,9 @@ def test_text_direction_ttb() -> None:
104105
105106 im = Image .new (mode = "RGB" , size = (100 , 300 ))
106107 draw = ImageDraw .Draw (im )
107- try :
108- draw .text ((0 , 0 ), "English あい" , font = ttf , fill = 500 , direction = "ttb" )
109- except ValueError as ex :
110- if str (ex ) == "libraqm 0.7 or greater required for 'ttb' direction" :
111- pytest .skip ("libraqm 0.7 or greater not available" )
108+ if not has_feature_version ("raqm" , "0.7" ):
109+ pytest .skip ("libraqm 0.7 or greater not available" )
110+ draw .text ((0 , 0 ), "English あい" , font = ttf , fill = 500 , direction = "ttb" )
112111
113112 target = "Tests/images/test_direction_ttb.png"
114113 assert_image_similar_tofile (im , target , 2.8 )
@@ -119,19 +118,17 @@ def test_text_direction_ttb_stroke() -> None:
119118
120119 im = Image .new (mode = "RGB" , size = (100 , 300 ))
121120 draw = ImageDraw .Draw (im )
122- try :
123- draw .text (
124- (27 , 27 ),
125- "あい" ,
126- font = ttf ,
127- fill = 500 ,
128- direction = "ttb" ,
129- stroke_width = 2 ,
130- stroke_fill = "#0f0" ,
131- )
132- except ValueError as ex :
133- if str (ex ) == "libraqm 0.7 or greater required for 'ttb' direction" :
134- pytest .skip ("libraqm 0.7 or greater not available" )
121+ if not has_feature_version ("raqm" , "0.7" ):
122+ pytest .skip ("libraqm 0.7 or greater not available" )
123+ draw .text (
124+ (27 , 27 ),
125+ "あい" ,
126+ font = ttf ,
127+ fill = 500 ,
128+ direction = "ttb" ,
129+ stroke_width = 2 ,
130+ stroke_fill = "#0f0" ,
131+ )
135132
136133 target = "Tests/images/test_direction_ttb_stroke.png"
137134 assert_image_similar_tofile (im , target , 19.4 )
@@ -219,14 +216,9 @@ def test_getlength(
219216 im = Image .new (mode , (1 , 1 ), 0 )
220217 d = ImageDraw .Draw (im )
221218
222- try :
223- assert d .textlength (text , ttf , direction ) == expected
224- except ValueError as ex :
225- if (
226- direction == "ttb"
227- and str (ex ) == "libraqm 0.7 or greater required for 'ttb' direction"
228- ):
229- pytest .skip ("libraqm 0.7 or greater not available" )
219+ if direction == "ttb" and not has_feature_version ("raqm" , "0.7" ):
220+ pytest .skip ("libraqm 0.7 or greater not available" )
221+ assert d .textlength (text , ttf , direction ) == expected
230222
231223
232224@pytest .mark .parametrize ("mode" , ("L" , "1" ))
@@ -242,17 +234,12 @@ def test_getlength_combine(mode: str, direction: str, text: str) -> None:
242234
243235 ttf = ImageFont .truetype ("Tests/fonts/NotoSans-Regular.ttf" , 48 )
244236
245- try :
246- target = ttf .getlength ("ii" , mode , direction )
247- actual = ttf .getlength (text , mode , direction )
237+ if direction == "ttb" and not has_feature_version ("raqm" , "0.7" ):
238+ pytest .skip ("libraqm 0.7 or greater not available" )
239+ target = ttf .getlength ("ii" , mode , direction )
240+ actual = ttf .getlength (text , mode , direction )
248241
249- assert actual == target
250- except ValueError as ex :
251- if (
252- direction == "ttb"
253- and str (ex ) == "libraqm 0.7 or greater required for 'ttb' direction"
254- ):
255- pytest .skip ("libraqm 0.7 or greater not available" )
242+ assert actual == target
256243
257244
258245@pytest .mark .parametrize ("anchor" , ("lt" , "mm" , "rb" , "sm" ))
@@ -265,11 +252,9 @@ def test_anchor_ttb(anchor: str) -> None:
265252 d = ImageDraw .Draw (im )
266253 d .line (((0 , 200 ), (200 , 200 )), "gray" )
267254 d .line (((100 , 0 ), (100 , 400 )), "gray" )
268- try :
269- d .text ((100 , 200 ), text , fill = "black" , anchor = anchor , direction = "ttb" , font = f )
270- except ValueError as ex :
271- if str (ex ) == "libraqm 0.7 or greater required for 'ttb' direction" :
272- pytest .skip ("libraqm 0.7 or greater not available" )
255+ if not has_feature_version ("raqm" , "0.7" ):
256+ pytest .skip ("libraqm 0.7 or greater not available" )
257+ d .text ((100 , 200 ), text , fill = "black" , anchor = anchor , direction = "ttb" , font = f )
273258
274259 assert_image_similar_tofile (im , path , 1 ) # fails at 5
275260
@@ -310,10 +295,12 @@ def test_anchor_ttb(anchor: str) -> None:
310295
311296# this tests various combining characters for anchor alignment and clipping
312297@pytest .mark .parametrize (
313- "name, text, anchor, dir, epsilon" , combine_tests , ids = [r [0 ] for r in combine_tests ]
298+ "name, text, anchor, direction, epsilon" ,
299+ combine_tests ,
300+ ids = [r [0 ] for r in combine_tests ],
314301)
315302def test_combine (
316- name : str , text : str , dir : str | None , anchor : str | None , epsilon : float
303+ name : str , text : str , direction : str | None , anchor : str | None , epsilon : float
317304) -> None :
318305 path = f"Tests/images/test_combine_{ name } .png"
319306 f = ImageFont .truetype ("Tests/fonts/NotoSans-Regular.ttf" , 48 )
@@ -322,11 +309,9 @@ def test_combine(
322309 d = ImageDraw .Draw (im )
323310 d .line (((0 , 200 ), (400 , 200 )), "gray" )
324311 d .line (((200 , 0 ), (200 , 400 )), "gray" )
325- try :
326- d .text ((200 , 200 ), text , fill = "black" , anchor = anchor , direction = dir , font = f )
327- except ValueError as ex :
328- if str (ex ) == "libraqm 0.7 or greater required for 'ttb' direction" :
329- pytest .skip ("libraqm 0.7 or greater not available" )
312+ if direction == "ttb" and not has_feature_version ("raqm" , "0.7" ):
313+ pytest .skip ("libraqm 0.7 or greater not available" )
314+ d .text ((200 , 200 ), text , fill = "black" , anchor = anchor , direction = direction , font = f )
330315
331316 assert_image_similar_tofile (im , path , epsilon )
332317
0 commit comments