indent_paragraphs and valign: :center or :bottom don't play well together #696

Open
nathancolgate opened this Issue Mar 14, 2014 · 4 comments

2 participants

@nathancolgate

Providing any value for :indent_paragraphs when using valign: center or valign: bottom pushes the first line down to either the center or bottom of the text.

require 'rubygems'
require 'prawn'

Prawn::Document.generate('vertical_align.pdf') do
  font_size 9
  bounding_box([30, 700], :width => 250, :height => 250) do
   text ('A'..'Z').to_a.map {|l| "TOP#{l}"}.join(' '),
    valign: :top
   text ('A'..'Z').to_a.map {|l| "CTR#{l}"}.join(' '),
    valign: :center
   text ('A'..'Z').to_a.map {|l| "BTM#{l}"}.join(' '),
    valign: :bottom
   transparent(0.5) { stroke_bounds }
  end

  bounding_box([310, 700], :width => 250, :height => 250) do
   text ('A'..'Z').to_a.map {|l| "TOP#{l}"}.join(' '),
    indent_paragraphs: 0,
    valign: :top
   text ('A'..'Z').to_a.map {|l| "CTR#{l}"}.join(' '),
    indent_paragraphs: 0,
    valign: :center
   text ('A'..'Z').to_a.map {|l| "BTM#{l}"}.join(' '),
    indent_paragraphs: 0,
    valign: :bottom
   transparent(0.5) { stroke_bounds }
  end
end

@nathancolgate

It may be safe to assume that using valign: top is technically pushing the first line to the top of the text as well, but this just looks as expected so it's not noticeable.

@practicingruby
prawnpdf member

@nathancolgate: I was able to reproduce this bug, and it looks like it's been around for a while.

It may be some time before I get to investigate this myself, so anyone is welcome to research further and put together a pull request. If someone else puts a fix together we can probably get it released reasonably quickly.

@nathancolgate

@sandal: Absolutely zero worries. To say that this is a fringe use case would be putting it mildly: the type-setters out there aren't really plugging for indented, bottom-aligned layouts.

Some context: I discovered this while building a PDF builder for a client wherein I tried to put all the options Prawn could offer into the system. The client caught this bug and I narrowed it down to a conflict between indent_paragraphs and valign.

Interim solution: I simply told the client he can't do both at the same time, and then wrote a method to set the indent to nil if it was zero:

  def pseudo_indent_paragraphs
    # when indent_paragraphs == 0, and you combine it with non-top valign
    # things get a little crazy. So for now, let's just set it to nil if it is 0
    (self.indent_paragraphs == 0) ? nil : self.indent_paragraphs
  end

Thanks again for an amazing gem.

@practicingruby
prawnpdf member

@nathancolgate: If you get a chance, please try the code on #758 and see if it fixes your problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment