Skip to content

Commit

Permalink
Made specs better for issue prawnpdf#169 fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessedoyle committed Oct 15, 2014
1 parent e2fe8fc commit 70bc235
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
9 changes: 5 additions & 4 deletions lib/prawn/text/formatted/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def initialize(formatted_text, options={})
@align = options[:align] ||
(@direction == :rtl ? :right : :left)
@vertical_align = options[:valign] || :top
@final_gap = options[:final_gap].nil? || false
@final_gap = options[:final_gap].nil? || options[:final_gap]
@leading = options[:leading] || @document.default_leading
@character_spacing = options[:character_spacing] ||
@document.character_spacing
Expand Down Expand Up @@ -485,14 +485,15 @@ def process_vertical_alignment(text)
@vertical_alignment_processed = true

return if @vertical_align == :top

wrap(text)

case @vertical_align
when :center
@at[1] = @at[1] - (@height - height + @descender) * 0.5
@at[1] -= (@height - height + @descender) * 0.5
when :bottom
@at[1] = @at[1] - (@height - height) + @descender
@at[1] -= line_gap unless @final_gap
@at[1] -= (@height - height)
@at[1] += line_gap if @final_gap
end

@height = height
Expand Down
32 changes: 23 additions & 9 deletions spec/formatted_text_box_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -619,53 +619,67 @@
describe "Text::Formatted::Box#render with :valign => :center" do
it "should have a bottom gap equal to baseline and bottom of box" do
create_pdf
box_height = 100
y = 450
array = [{ :text => 'Vertical Align' }]
options = {
:document => @pdf,
:valign => :center,
:at => [0,450],
:at => [0,y],
:width => 100,
:height => 100,
:height => box_height,
:size => 16
}
text_box = Prawn::Text::Formatted::Box.new(array, options)
text_box.render
text_box.at[1].should eq(405.744)
line_padding = (box_height - text_box.height + text_box.descender) * 0.5
baseline = y - line_padding

text_box.at[1].should be_within(0.01).of(baseline)
end
end

describe "Text::Formatted::Box#render with :valign => :bottom" do
it "should render a :final_gap on the bottom by default" do
create_pdf
box_height = 100
y = 450
array = [{ :text => 'Vertical Align' }]
options = {
:document => @pdf,
:valign => :bottom,
:at => [0,450],
:at => [0,y],
:width => 100,
:height => 100,
:height => box_height,
:size => 16
}
text_box = Prawn::Text::Formatted::Box.new(array, options)
text_box.render
text_box.at[1].should eq(368.112)
top_padding = y - (box_height - text_box.height)
top_padding += text_box.line_gap

text_box.at[1].should be_within(0.01).of(top_padding)
end

it "should not render a :final_gap if set to false" do
create_pdf
box_height = 100
y = 450
array = [{ :text => 'Vertical Align' }]
options = {
:document => @pdf,
:valign => :bottom,
:final_gap => false,
:at => [0,450],
:at => [0,y],
:width => 100,
:height => 100,
:height => box_height,
:size => 16
}
text_box = Prawn::Text::Formatted::Box.new(array, options)
text_box.render
text_box.at[1].should eq(364.416)
top_padding = y - (box_height - text_box.height)

text_box.at[1].should be_within(0.01).of(top_padding)
end
end

Expand Down

0 comments on commit 70bc235

Please sign in to comment.