Skip to content

Commit

Permalink
Merge pull request #2357 from OsaAjani/issue-2260
Browse files Browse the repository at this point in the history
Fix text without spaces overflowing TextClip boundaries
  • Loading branch information
OsaAjani authored Jan 31, 2025
2 parents 71eb429 + 9d7d668 commit ff4dca0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix GPU h264_nvenc encoding not working.
- Improve perfs of decorator by pre-computing arguments
- Fix textclip being cut or of impredictable height (see issues #2325, #2260 and #2268)
- TextClip now properly breaklines on text without spaces, such as chinese (see #2260)
- Fix TimeMirror and TimeSymmetrize cutting last second of clip
- ImageSequenceClip was wrong when calculating fps with duration and no fps (see issue #2351)
- More consistent frame seek (see issue #2115 and PR #2117)
Expand Down
33 changes: 25 additions & 8 deletions moviepy/video/VideoClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,9 @@ def __init__(
_ = ImageFont.truetype(font)
except Exception as e:
raise ValueError(
"Invalid font {}, pillow failed to use it with error {}".format(font, e)
"Invalid font {}, pillow failed to use it with error {}".format(
font, e
)
)

if filename:
Expand Down Expand Up @@ -1777,9 +1779,15 @@ def __break_text(

lines = []
current_line = ""
words = text.split(" ")
for word in words:
temp_line = current_line + " " + word if current_line else word

# We try to break on spaces as much as possible
# if a text dont contain spaces (ex chinese), we will break when possible
last_space = 0
for index, char in enumerate(text):
if char == " ":
last_space = index

temp_line = current_line + char
temp_left, temp_top, temp_right, temp_bottom = draw.multiline_textbbox(
(0, 0),
temp_line,
Expand All @@ -1790,11 +1798,20 @@ def __break_text(
)
temp_width = temp_right - temp_left

if temp_width <= width:
current_line = temp_line
if temp_width >= width:
# If we had a space previously, add everything up to the space
# and reset last_space and current_line else add everything up
# to previous char
if last_space:
lines.append(temp_line[0:last_space])
current_line = temp_line[last_space + 1 : index + 1]
last_space = 0
else:
lines.append(current_line[0:index])
current_line = char
last_space = 0
else:
lines.append(current_line)
current_line = word
current_line = temp_line

if current_line:
lines.append(current_line)
Expand Down
12 changes: 6 additions & 6 deletions moviepy/video/io/ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,12 @@ def parse(self):
# for default streams, set their numbers globally, so it's
# easy to get without iterating all
if self._current_stream["default"]:
self.result[f"default_{stream_type_lower}_input_number"] = (
input_number
)
self.result[f"default_{stream_type_lower}_stream_number"] = (
stream_number
)
self.result[
f"default_{stream_type_lower}_input_number"
] = input_number
self.result[
f"default_{stream_type_lower}_stream_number"
] = stream_number

# exit chapter
if self._current_chapter:
Expand Down

0 comments on commit ff4dca0

Please sign in to comment.