From ea7ccbdfced0bfec45181c303c1ad7ca85254940 Mon Sep 17 00:00:00 2001 From: John Ford Date: Mon, 28 Sep 2015 00:18:51 +0900 Subject: [PATCH] Add :dry_run to text_box and formatted_text_box Changed text_box and formatted_text_box to return the box object and allow access font_size. --- lib/prawn/text/box.rb | 5 ++-- lib/prawn/text/formatted/box.rb | 16 ++++++++--- spec/formatted_text_box_spec.rb | 51 +++++++++++++++++++++++++++++++++ spec/text_box_spec.rb | 51 +++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 6 deletions(-) diff --git a/lib/prawn/text/box.rb b/lib/prawn/text/box.rb index 42c997267..3c04fdd53 100644 --- a/lib/prawn/text/box.rb +++ b/lib/prawn/text/box.rb @@ -94,7 +94,7 @@ module Text # # == Returns # - # Returns any text that did not print under the current settings. + # Returns the Text::Box object or Text::Formatted::Box if :inline_format is true # # == Exceptions # @@ -113,7 +113,8 @@ def text_box(string, options = {}) Text::Box.new(string, options) end - box.render + box.render(:dry_run => options[:dry_run]) + box end # @group Experimental API diff --git a/lib/prawn/text/formatted/box.rb b/lib/prawn/text/formatted/box.rb index 8b4e5949a..d25a5ddcc 100644 --- a/lib/prawn/text/formatted/box.rb +++ b/lib/prawn/text/formatted/box.rb @@ -64,6 +64,10 @@ module Formatted # #render_behind and #render_in_front, which are called immediately # prior to and immediately after rendring the text fragment and which # are passed the fragment as an argument + # :dry_run:: + # boolean. Prevents the text from being printed to the PDF + # and is useful in conjunction with :shrink_to_fit to + # determine the font size and height of the box # # == Example # @@ -78,8 +82,7 @@ module Formatted # # == Returns # - # Returns a formatted text array representing any text that did not print - # under the current settings. + # Returns the Text::Formatted::Box object after render is called # # == Exceptions # @@ -89,7 +92,9 @@ module Formatted # any text # def formatted_text_box(array, options = {}) - Text::Formatted::Box.new(array, options.merge(:document => self)).render + box = Text::Formatted::Box.new(array, options.merge(:document => self)) + box.render(:dry_run => options[:dry_run]) + box end # Generally, one would use the Prawn::Text::Formatted#formatted_text_box @@ -129,6 +134,8 @@ def everything_printed? attr_reader :descender # The leading used during printing attr_reader :leading + # The font size which may have changed if shrink_to_fit is true + attr_reader :font_size def line_gap line_height - (ascender + descender) @@ -341,7 +348,8 @@ def valid_options :document, :direction, :fallback_fonts, - :draw_text_callback + :draw_text_callback, + :dry_run ] end diff --git a/spec/formatted_text_box_spec.rb b/spec/formatted_text_box_spec.rb index c72c2956d..24abeaf00 100644 --- a/spec/formatted_text_box_spec.rb +++ b/spec/formatted_text_box_spec.rb @@ -671,6 +671,57 @@ end end +describe "formatted_text_box with :dry_run option" do + before(:each) do + create_pdf + @formatted_text = [{ :text => "This is amazing. " * 10 }] + end + + it "should return instance of Prawn::Text::Formatted::Box" do + box = @pdf.formatted_text_box(@formatted_text, :dry_run => true) + expect(box).to be_instance_of(Prawn::Text::Formatted::Box) + end + + it "should draw content to the page when :dry_run => nil" do + @pdf.formatted_text_box(@formatted_text) + text = PDF::Inspector::Text.analyze(@pdf.render) + expect(text.strings).not_to be_empty + end + + it "should not draw any content to the page when :dry_run => true" do + @pdf.formatted_text_box(@formatted_text, :dry_run => true) + text = PDF::Inspector::Text.analyze(@pdf.render) + expect(text.strings).to be_empty + end + + it "should allow accessing font_size when testing :shrink_to_fit" do + options = { + :at => [0, 300], + :disable_wrap_by_char => true, + :dry_run => true, + :overflow => :shrink_to_fit, + :size => 50, + :width => 70 + } + + box = @pdf.formatted_text_box([{ :text => "Some shorter text" }], options) + expect(box.font_size).to eq(22) + end + + it "should allow accessing height" do + options = { + :at => [0, @pdf.bounds.height], + :dry_run => true, + :width => @pdf.bounds.width + } + + box1 = @pdf.formatted_text_box([{ :text => "A lot of text. " * 100 }], options) + box2 = @pdf.formatted_text_box([{ :text => "A lot of text. " * 200 }], options) + + expect(box2.height).to be > box1.height + end +end + class TestFragmentCallback def initialize(string, number, options) @document = options[:document] diff --git a/spec/text_box_spec.rb b/spec/text_box_spec.rb index d4ca0328f..759e3bda7 100644 --- a/spec/text_box_spec.rb +++ b/spec/text_box_spec.rb @@ -1029,6 +1029,57 @@ end end +describe "text_box with :dry_run option" do + before(:each) do + create_pdf + @text = "This is amazing. " * 10 + end + + it "should return instance of Prawn::Text::Formatted::Box" do + box = @pdf.text_box(@text, :dry_run => true) + expect(box).to be_instance_of(Prawn::Text::Box) + end + + it "should draw content to the page when :dry_run => nil" do + @pdf.text_box(@text) + text = PDF::Inspector::Text.analyze(@pdf.render) + expect(text.strings).not_to be_empty + end + + it "should not draw any content to the page when :dry_run => true" do + @pdf.text_box(@text, :dry_run => true) + text = PDF::Inspector::Text.analyze(@pdf.render) + expect(text.strings).to be_empty + end + + it "should allow accessing font_size when testing :shrink_to_fit" do + options = { + :at => [0, 300], + :disable_wrap_by_char => true, + :dry_run => true, + :overflow => :shrink_to_fit, + :size => 50, + :width => 70 + } + + box = @pdf.text_box("Some shorter text", options) + expect(box.font_size).to eq(22) + end + + it "should allow accessing height" do + options = { + :at => [0, @pdf.bounds.height], + :dry_run => true, + :width => @pdf.bounds.width + } + + box1 = @pdf.text_box("A lot of text. " * 100, options) + box2 = @pdf.text_box("A lot of text " * 200, options) + + expect(box2.height).to be > box1.height + end +end + def reduce_precision(float) ("%.5f" % float).to_f end