diff --git a/lib/prawn/text/box.rb b/lib/prawn/text/box.rb
index d4f83263b..c0c447f0c 100644
--- a/lib/prawn/text/box.rb
+++ b/lib/prawn/text/box.rb
@@ -72,7 +72,6 @@ module Text
# :valign::
# :top, :center, or :bottom. Vertical
# alignment within the bounding box [:top]
- #
# :rotate::
# number. The angle to rotate the text
# :rotate_around::
@@ -106,7 +105,7 @@ module Text
#
# == Exceptions
#
- # Raises Prawn::Errrors::CannotFit if not wide enough to print
+ # Raises Prawn::Errors::CannotFit if not wide enough to print
# any text
#
def text_box(string, options={})
diff --git a/lib/prawn/text/formatted/box.rb b/lib/prawn/text/formatted/box.rb
index 7b4238668..55d4b627b 100644
--- a/lib/prawn/text/formatted/box.rb
+++ b/lib/prawn/text/formatted/box.rb
@@ -85,7 +85,7 @@ module Formatted
#
# Raises "Bad font family" if no font family is defined for the current font
#
- # Raises Prawn::Errrors::CannotFit if not wide enough to print
+ # Raises Prawn::Errors::CannotFit if not wide enough to print
# any text
#
def formatted_text_box(array, options={})
@@ -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
diff --git a/spec/formatted_text_box_spec.rb b/spec/formatted_text_box_spec.rb
index 157d9405c..69d046fd0 100644
--- a/spec/formatted_text_box_spec.rb
+++ b/spec/formatted_text_box_spec.rb
@@ -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]