Skip to content

Commit

Permalink
recent improvements to text left shrink to fit shrinking when unneces…
Browse files Browse the repository at this point in the history
…sary. dry run pre-calculation was resulting in an omitted last line in some situations because of limited floating point precision
  • Loading branch information
Daniel Nelson committed Mar 29, 2011
1 parent d96bb9e commit f2138a9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
8 changes: 5 additions & 3 deletions examples/text/text_box.rb
Expand Up @@ -66,21 +66,23 @@ def get_options(i, j)
options[:at][1] = bounds.top - (bounds.height - options[:height]) * 0.33 * j
box = Prawn::Text::Box.new(get_string(i, j), options)

fill_color("ffeeee")
if i == 1
if j >= 2
# bound with a box of a particular size, regardless of how
# much text it contains
fill_color("ffeeee")
fill_and_stroke_rectangle(options[:at],
options[:width],
options[:height])
else
# bound with a box that exactly fits the printed text using
# dry_run look-ahead
fill_color("eeeeff")
box.render(:dry_run => true)
fill_and_stroke_rectangle(options[:at],
fill_and_stroke_rectangle(box.at,
options[:width],
box.height)
end

fill_color("000000")
box.render
end
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/core/text/formatted/wrap.rb
Expand Up @@ -110,7 +110,7 @@ def enough_height_for_this_line?
diff = @descender + @line_height
end
required_total_height = @baseline_y.abs + diff
if required_total_height > @height
if required_total_height > @height + 0.0001
# no room for the full height of this line
@arranger.repack_unretrieved
false
Expand Down
2 changes: 2 additions & 0 deletions lib/prawn/text/formatted/box.rb
Expand Up @@ -452,9 +452,11 @@ def process_vertical_alignment(text)
# Decrease the font size until the text fits or the min font
# size is reached
def shrink_to_fit(text)
wrap(text)
until @everything_printed || @font_size <= @min_font_size
@font_size = [@font_size - 0.5, @min_font_size].max
@document.font_size = @font_size
wrap(text)
end
end

Expand Down
41 changes: 39 additions & 2 deletions spec/text_box_spec.rb
Expand Up @@ -258,6 +258,23 @@
end
end

describe "Text::Box#render(:valign => :bottom)" do
it "#at should be the same from one dry run to the next" do
create_pdf
text = "this is center text " * 12
options = { :width => 162,
:valign => :bottom,
:document => @pdf }
text_box = Prawn::Text::Box.new(text, options)

text_box.render(:dry_run => true)
original_at = text_box.at.dup

text_box.render(:dry_run => true)
text_box.at.should == original_at
end
end

describe "Text::Box#render with :rotate option of 30)" do
before(:each) do
create_pdf
Expand Down Expand Up @@ -660,16 +677,36 @@
context "shrink_to_fit overflow" do
it "should not drop below the minimum font size" do
@options[:overflow] = :shrink_to_fit
@options[:min_font_size] = 5.1
@options[:min_font_size] = 10.1
@text_box = Prawn::Text::Box.new(@text, @options)
@text_box.render

text = PDF::Inspector::Text.analyze(@pdf.render)
text.font_settings[0][:size].should == 5.1
text.font_settings[0][:size].should == 10.1
end
end
end

describe "Text::Box with enough space to fit the text but using the " +
"shrink_to_fit overflow" do
it "should not shrink the text when there is no need to" do
create_pdf
@bounding_height = 162.0
@options = {
:width => 162.0,
:height => @bounding_height,
:overflow => :shrink_to_fit,
:min_font_size => 5,
:document => @pdf
}
@text_box = Prawn::Text::Box.new("hello\nworld", @options)
@text_box.render

text = PDF::Inspector::Text.analyze(@pdf.render)
text.font_settings[0][:size].should == 12
end
end

describe "Text::Box with a solid block of Chinese characters" do
it "printed text should match requested text, except for newlines" do
create_pdf
Expand Down

0 comments on commit f2138a9

Please sign in to comment.