@@ -199,6 +199,25 @@ def _fix_sentence_endings(self, chunks):
199199 else :
200200 i += 1
201201
202+ def _find_width_index (self , text , width ):
203+ """_find_length_index(text : string, width : int)
204+
205+ Find at which index the text has the required width, since when using a
206+ different text_len, this index will not be equal to the required width.
207+ """
208+ # When using default len as self.text_len, the required index and width
209+ # will be equal, this prevents calculation time.
210+ if self .text_len (text [:width ]) == width :
211+ # For character widths greater than one, width can be more than the
212+ # number of characters
213+ return min (width , len (text ))
214+ cur_text = ''
215+ for i , c in enumerate (text ):
216+ cur_text += c
217+ cur_width = self .text_len (cur_text )
218+ if cur_width > width :
219+ return max (i , 1 )
220+
202221 def _handle_long_word (self , reversed_chunks , cur_line , cur_len , width ):
203222 """_handle_long_word(chunks : [string],
204223 cur_line : [string],
@@ -217,12 +236,12 @@ def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
217236 # If we're allowed to break long words, then do so: put as much
218237 # of the next chunk onto the current line as will fit.
219238 if self .break_long_words :
220- end = space_left
221239 chunk = reversed_chunks [- 1 ]
240+ end = self ._find_width_index (chunk , space_left )
222241 if self .break_on_hyphens and self .text_len (chunk ) > space_left :
223242 # break after last hyphen, but only if there are
224243 # non-hyphens before it
225- hyphen = chunk .rfind ('-' , 0 , space_left )
244+ hyphen = chunk .rfind ('-' , 0 , end )
226245 if hyphen > 0 and any (c != '-' for c in chunk [:hyphen ]):
227246 end = hyphen + 1
228247 cur_line .append (chunk [:end ])
0 commit comments