It seems to me like there is no way to explicitly terminate a line inside of a paragraph, as "\n" characters are rendered as broken glyphs and there is no way to push
a Break
Element
.
Is this due to requirements of the layout process and I should just create a new paragraph if I need a line break or is it possible to implement a way to do this?
Thanks for the report!
Both options should be possible and have a different meaning: I’m planning to add a justified paragraph alignment (#13) that makes all but the last line fill the available width. So a justified paragraph with the text
"1 2 3\n4 5"
would be rendered as:|1 2 3| |4 5 |
Whereas two justified paragraphs with the content
"1 2 3"
and"4 5"
would be rendered like this:|1 2 3 | |4 5 |
It should be easy to implement this in two steps (all in
src/wrap.rs
):
- Instead of only splitting on spaces in
Words::next
, split on all types of whitespace.- In
Wrapper::next
, check if the word ends with a newline. If yes, break the line.When implementing this, we should also strip trailing whitespace from the last word of a line in
Wrapper::next
.But: Currently,
Paragraph
assumes that the strings created byWords
andWrapper
have exactly the same length as the input strings. This is already violated by the hyphenation feature that I added recently, and it will also be violated if we start to strip whitespace. I’ll look into this issue and report back once it is fixed.
I’ve fixed the bug that blocked this issue in commit 0d8e5177, so this can be implemented now. We just have to make sure that we update
delta
(inWrapper::next
) with the number of whitespace bytes that we removed from the line.
Would it make sense / be possible to allow
push
ing anyElement
into a Paragraph?
The paragraph layout is already quite complex: We have to calculate the line height, split the input into words, measure the word width, group the words into lines and hyphenate words. We currently don’t have the means to do that for arbitrary elements.
It would be nice to be able to push elements into a paragraph, but it will require lots of conceptual work. For this specific issue, it is much easier to use newline characters.
Ah, that makes sense.
For the time being, is there anything that genpdf does respond to as a line break? I can easily parse my text and replace the \n's with anything else.
I also just wrote this short solution that works for my documents:
let split_text = p_entry.desc.split("\n"); let vec: Vec<&str> = split_text.collect(); for cleaned_text in vec { doc.push(elements::Paragraph::new(cleaned_text)); }
Yes, I think this is the best workaround at the moment.