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

Formatted Text Box excess removes color with dry_run render #1355

Open
pdmallaband opened this issue Aug 1, 2024 · 0 comments
Open

Formatted Text Box excess removes color with dry_run render #1355

pdmallaband opened this issue Aug 1, 2024 · 0 comments

Comments

@pdmallaband
Copy link

pdmallaband commented Aug 1, 2024

Repo:

  • Construct Formatted Text Box with a colored fragment and box size that will cause the text to truncate
  • render with dry_run: true option to obtain the "text that would not be printed"
  • text fragments in this "excess text" have the color property stripped.

excess = Prawn::Text::Formatted::Box.new([{ text: "text", styles: [], font: "OpenSans", size: 12 }, { text: "red text", styles: [], font: "OpenSans", size: 12, color: "e60000" }], { document: @document, at: [0, 0], width: 20, height: 17 }).render(dry_run: true)
pp excess

returns:
[{:styles=>[], :font=>"OpenSans", :size=>12, :normalized_soft_hyphen=>"", :text=>"t"}, {:styles=>[], :font=>"OpenSans", :size=>12, :normalized_soft_hyphen=>"", :text=>"re"}, {:styles=>[], :font=>"OpenSans", :size=>12, :text=>"d text"}]

The two fragments for "red text" are missing the color prop.

Running without dry_run: true, does not remove colouring.

I can't see anything in the docs that suggests this is expected behaviour, but the following code suggests this is a deliberate choice:

text.each { |t| t.delete(:color) } if flags[:dry_run]

For context my use case:

  • I'm constructing a set of lines to be printed from some rich text input where images and text (with various formatting) can be mixed.
  • To allow text to go before and after images and appear to wrap across the full line, I'm constraining the width and height of text boxes to force truncation at each line, and then passing the excess text recursively to construct each new line.
  • I therefore need to actually use the excess text to print the new line, not just for look ahead width calculations.
  1. What is the reason for this color stripping?
  2. Is there another obvious way to get the colored excess text fragments (or handle the use case discussed above)? - I'm trying to avoid having to compare the excess text fragments to the original to figure out where the break was and then reapply the colouring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant