@@ -111,27 +111,120 @@ def test_stroke() -> None:
111111
112112
113113@pytest .mark .parametrize (
114- "text , width, expected" ,
114+ "data , width, expected" ,
115115 (
116116 ("Hello World!" , 100 , "Hello World!" ), # No wrap required
117117 ("Hello World!" , 50 , "Hello\n World!" ), # Wrap word to a new line
118- ("Hello World!" , 25 , "Hello\n Worl\n d!" ), # Split word across lines
119118 # Keep multiple spaces within a line
120- ("Keep multiple spaces" , 75 , "Keep multiple\n spaces" ),
119+ ("Keep multiple spaces" , 90 , "Keep multiple\n spaces" ),
120+ (" Keep\n leading space" , 100 , " Keep\n leading space" ),
121121 ),
122122)
123123@pytest .mark .parametrize ("string" , (True , False ))
124- def test_wrap (text : str , width : int , expected : str , string : bool ) -> None :
125- text = ImageText .Text (text if string else text .encode ())
126- assert text .wrap (width ) is None
127- assert text .text == expected if string else expected .encode ()
124+ def test_wrap (data : str , width : int , expected : str , string : bool ) -> None :
125+ if string :
126+ text = ImageText .Text (data )
127+ assert text .wrap (width ) is None
128+ assert text .text == expected
129+ else :
130+ text_bytes = ImageText .Text (data .encode ())
131+ assert text_bytes .wrap (width ) is None
132+ assert text_bytes .text == expected .encode ()
133+
134+
135+ def test_wrap_long_word () -> None :
136+ text = ImageText .Text ("Hello World!" )
137+ with pytest .raises (ValueError , match = "Word does not fit within line" ):
138+ text .wrap (25 )
139+
140+
141+ def test_wrap_unsupported (font : ImageFont .FreeTypeFont ) -> None :
142+ transposed_font = ImageFont .TransposedFont (font )
143+ text = ImageText .Text ("Hello World!" , transposed_font )
144+ with pytest .raises (ValueError , match = "TransposedFont not supported" ):
145+ text .wrap (50 )
146+
147+ text = ImageText .Text ("Hello World!" , direction = "ttb" )
148+ with pytest .raises (ValueError , match = "Only ltr direction supported" ):
149+ text .wrap (50 )
128150
129151
130152def test_wrap_height () -> None :
153+ width = 50 if features .check_module ("freetype2" ) else 60
131154 text = ImageText .Text ("Text does not fit within height" )
132- assert text .wrap (50 , 25 ).text == " within height"
155+ wrapped = text .wrap (width , 25 if features .check_module ("freetype2" ) else 40 )
156+ assert wrapped is not None
157+ assert wrapped .text == " within height"
133158 assert text .text == "Text does\n not fit"
134159
135- text = ImageText .Text ("Text does not fit singlelongword" )
136- assert text .wrap (50 , 25 ).text == " singlelongword"
160+ text = ImageText .Text ("Text does not fit\n within height" )
161+ wrapped = text .wrap (width , 20 )
162+ assert wrapped is not None
163+ assert wrapped .text == " not fit\n within height"
164+ assert text .text == "Text does"
165+
166+ text = ImageText .Text ("Text does not fit\n \n within height" )
167+ wrapped = text .wrap (width , 25 if features .check_module ("freetype2" ) else 40 )
168+ assert wrapped is not None
169+ assert wrapped .text == "\n within height"
137170 assert text .text == "Text does\n not fit"
171+
172+
173+ def test_wrap_scaling_unsupported () -> None :
174+ font = ImageFont .load_default_imagefont ()
175+ text = ImageText .Text ("Hello World!" , font )
176+ with pytest .raises (ValueError , match = "'scaling' only supports FreeTypeFont" ):
177+ text .wrap (50 , scaling = "shrink" )
178+
179+ if features .check_module ("freetype2" ):
180+ text = ImageText .Text ("Hello World!" )
181+ with pytest .raises (ValueError , match = "'scaling' requires 'height'" ):
182+ text .wrap (50 , scaling = "shrink" )
183+
184+
185+ @skip_unless_feature ("freetype2" )
186+ def test_wrap_shrink () -> None :
187+ # No scaling required
188+ text = ImageText .Text ("Hello World!" )
189+ assert isinstance (text .font , ImageFont .FreeTypeFont )
190+ assert text .font .size == 10
191+ assert text .wrap (50 , 50 , "shrink" ) is None
192+ assert isinstance (text .font , ImageFont .FreeTypeFont )
193+ assert text .font .size == 10
194+
195+ with pytest .raises (ValueError , match = "Text could not be scaled" ):
196+ text .wrap (50 , 15 , ("shrink" , 9 ))
197+
198+ assert text .wrap (50 , 15 , "shrink" ) is None
199+ assert text .font .size == 8
200+
201+ text = ImageText .Text ("Hello World!" )
202+ assert text .wrap (50 , 15 , ("shrink" , 7 )) is None
203+ assert isinstance (text .font , ImageFont .FreeTypeFont )
204+ assert text .font .size == 8
205+
206+
207+ @skip_unless_feature ("freetype2" )
208+ def test_wrap_grow () -> None :
209+ # No scaling required
210+ text = ImageText .Text ("Hello World!" )
211+ assert isinstance (text .font , ImageFont .FreeTypeFont )
212+ assert text .font .size == 10
213+ assert text .wrap (58 , 10 , "grow" ) is None
214+ assert isinstance (text .font , ImageFont .FreeTypeFont )
215+ assert text .font .size == 10
216+
217+ with pytest .raises (ValueError , match = "Text could not be scaled" ):
218+ text .wrap (50 , 50 , ("grow" , 12 ))
219+
220+ assert text .wrap (50 , 50 , "grow" ) is None
221+ assert text .font .size == 16
222+
223+ text = ImageText .Text ("A\n B" )
224+ with pytest .raises (ValueError , match = "Text could not be scaled" ):
225+ text .wrap (50 , 10 , "grow" )
226+
227+ text = ImageText .Text ("Hello World!" )
228+ assert text .wrap (50 , 50 , ("grow" , 18 )) is None
229+ assert isinstance (text .font , ImageFont .FreeTypeFont )
230+ assert text .font .size == 16
0 commit comments