Skip to main content

normalize_breaks

Function normalize_breaks 

Source
pub fn normalize_breaks(text: &str, multi_line: bool) -> String
Expand description

The text as the layout will hold it, given whether the field is multiline.

A single-line field has no way to represent a line break, so every one is removed — not replaced with a space, and not treated as a terminator that truncates the rest. "Foo\nBar" is "FooBar", and "Foo\n" is "Foo". A \r\n or \n\r pair is one break rather than two, which is why the pairs are consumed together.

A multiline field keeps its breaks, normalized to '\n' so that the four spellings of one break compare equal.

This is not a convenience: the layout engine already applies exactly this rule when it lays the text out, so a control that stored the raw string would disagree with its own layout about how many characters it has.

use pdfrum_form::edit::ops::normalize_breaks;

// A single-line field REMOVES the break; it does not become a space and
// it does not terminate the text.
assert_eq!(normalize_breaks("Foo\nBar", false), "FooBar");
assert_eq!(normalize_breaks("Foo\n", false), "Foo");

// A multiline field keeps it, normalized to `'\n'`, and a `\r\n` pair is
// one break rather than two.
assert_eq!(normalize_breaks("Foo\r\nBar", true), "Foo\nBar");

// A tab is set as a space either way, which is what the layout does.
assert_eq!(normalize_breaks("a\tb", false), "a b");