-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Automatic text wrapping/text box filling #6201
Comments
also options to align text left, right, center, or justify would be useful as well. This script implements that and top/middle/bottom text alignment. another example script |
Sounds like a reasonable enhancement request. Note that there might be issues with mixed LTR/RTL text which will need extra tests. A few notes:
Please use the
Word breaking is quite a difficult task, I'd suggest to constrain this to spaces to start with.
Not possible within the current API, a font is created at a given size and cannot be easily changed.
Already possible using the
Already possible, use the |
oh, good to know. probably should add that to the docs?
right yeah I forgot CJK and other languages don't have definite characters at word boundaries, I just meant to break at whitespace which as I understand it shouldn't be too hard and might actually be faster than individual character breaking. i'd add support for zero-width spaces to allow some external library or native speaker to mark word boundaries for pillow assuming its a non-trivial task
ah so that's why the script I linked loads from file every change. is there an existing way to regenerate fonts without reloading from file or would that need to be an entire API change? thanks for the quick and detailed response! |
I think it will be deprecated soon, it's just a matter of working out the replacement (
That part was just a suggestion to avoid overcomplicating things. Sure, zero-width spaces can probably be supported.
I think you can load a font file in Python and then pass the bytes as input to |
I see. Thank you!
|
I've made a first attempt at implementing this, using a greedy algorithm: Example output here. Issues:
Current blind spots and possible improvements:
|
If you are referring to this:
then I would not worry about it. Similar rules exist in some European languages and even MS Word doesn't really help there.
The text height is calculated here: Lines 514 to 516 in 1340237
where spacing is a parameter defaulting to 4. This is not really accurate for some fonts, but it is used for historical reasons.
Do not use |
I feel that getting it working with “easier” languages first (ones that use white space or other characters to break words) would be the best thing to do right now as CJK word-breaking seems like a non-trivial task that could be hacked in by adding zero-width spaces. Is there an existing library that can determine word boundaries that could be included by PIL as an extra? |
I may have misunderstood the Wikipedia article. The Unicode Line Breaking Algorithm is more helpful. I think that it is probably sufficient to implement the non-tailorable part of the algorithm (see start of Table 1), which is just that line break characters are a mandatory break and spaces/zero-width spaces are an optional break. According to LineData.txt, this means it is sufficient to consider replacing the SPACE (U+20) and ZERO-WIDTH SPACE (U+200B) characters with |
After a brief search, I couldn't find one that is freely available.
I'm going to give this a shot! I'll start with Table 1 and leave all of the other character classes as break-allowed for now, though I think I'd like to try to implement the others as well. |
If you're going to implement the entire Unicode Line Breaking Algorithm, I recommend making it its own library |
If you want to implement the full algorithm, it might make sense to add it to Raqm (which Pillow uses internally), or make it a separate library that Raqm can use. See HOST-Oman/libraqm#50
The |
I wasn’t familiar enough with PIL’s internals to suggest that but that is a good idea |
The Raqm issue mentions https://github.com/adah1972/libunibreak. I haven't looked at it too closely, but it seems to be a C library implementing the Unicode algorithm that returns a list of valid break positions. |
I am exploring adding a
Edit: Somehow I completely missed the part about adding this as a feature to |
Any news on this? Would love this feature. |
Looks like a stalled attempt at greatness, maybe someone can pick up the effort: https://github.com/atomicparade/pil_autowrap |
This comment was marked as off-topic.
This comment was marked as off-topic.
Any progress on this topic? It would be an important feature. |
many people have written scripts to do this and it's relatively easy with the library's
getsize
func but I feel like it really should be a built-in feature.i think there should be a
draw_text_box
or similar function which has these properties on top of the existingdraw_multiline_text
:I'm not very experienced with the library internals or C in general so for now I won't make a pull, I just want to throw the idea out there to the devs
The text was updated successfully, but these errors were encountered: