~ireas/genpdf-rs#19: 
Explicit line breaks inside of paragraphs

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?

Status
REPORTED
Submitter
~soruh
Assigned to
Submitted
2 years ago
Updated
3 months ago
Labels
good first issue v0.4.0

~ireas 2 years ago

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 by Words and Wrapper 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.

~ireas 2 years ago

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 (in Wrapper::next) with the number of whitespace bytes that we removed from the line.

~soruh 2 years ago

Would it make sense / be possible to allow pushing any Element into a Paragraph?

~ireas 2 years ago

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.

~soruh 2 years ago

Ah, that makes sense.

~qualitymix 3 months ago

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.

~qualitymix 3 months ago

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));
}

~ireas 3 months ago

Yes, I think this is the best workaround at the moment.

Register here or Log in to comment, or comment via email.