prawnpdf/prawn

Formatted Text Box excess removes color with dry_run render

pdmallaband opened this issue · 0 comments

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