Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix text without spaces overflowing TextClip boundaries #2357

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading