Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/prawn/text/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ module Text
# <tt>:valign</tt>::
# <tt>:top</tt>, <tt>:center</tt>, or <tt>:bottom</tt>. Vertical
# alignment within the bounding box [:top]
#
# <tt>:rotate</tt>::
# <tt>number</tt>. The angle to rotate the text
# <tt>:rotate_around</tt>::
Expand Down Expand Up @@ -106,7 +105,7 @@ module Text
#
# == Exceptions
#
# Raises <tt>Prawn::Errrors::CannotFit</tt> if not wide enough to print
# Raises <tt>Prawn::Errors::CannotFit</tt> if not wide enough to print
# any text
#
def text_box(string, options={})
Expand Down
8 changes: 5 additions & 3 deletions lib/prawn/text/formatted/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module Formatted
#
# Raises "Bad font family" if no font family is defined for the current font
#
# Raises <tt>Prawn::Errrors::CannotFit</tt> if not wide enough to print
# Raises <tt>Prawn::Errors::CannotFit</tt> if not wide enough to print
# any text
#
def formatted_text_box(array, options={})
Expand Down Expand Up @@ -484,14 +484,16 @@ 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) * 0.5
@at[1] -= (@height - height + @descender) * 0.5
when :bottom
@at[1] = @at[1] - (@height - height) + @descender
@at[1] -= (@height - height)
end

@height = height
end

Expand Down
45 changes: 45 additions & 0 deletions spec/formatted_text_box_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,51 @@
end
end

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,y],
:width => 100,
:height => box_height,
:size => 16
}
text_box = Prawn::Text::Formatted::Box.new(array, options)
text_box.render
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 not render a gap between the text and bottom of box" do
create_pdf
box_height = 100
y = 450
array = [{ :text => 'Vertical Align' }]
options = {
:document => @pdf,
:valign => :bottom,
:at => [0,y],
:width => 100,
:height => box_height,
:size => 16
}
text_box = Prawn::Text::Formatted::Box.new(array, options)
text_box.render
top_padding = y - (box_height - text_box.height)

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

class TestFragmentCallback
def initialize(string, number, options)
@document = options[:document]
Expand Down