diff --git a/lib/prawn/text/formatted/fragment.rb b/lib/prawn/text/formatted/fragment.rb index 3e7e2cf53..e01d423d2 100644 --- a/lib/prawn/text/formatted/fragment.rb +++ b/lib/prawn/text/formatted/fragment.rb @@ -209,9 +209,19 @@ def absolute_bottom_right def process_text(text) string = strip_zero_width_spaces(text) + if exclude_trailing_white_space? - string = process_soft_hyphens(string.rstrip) + string = string.rstrip + + if soft_hyphens_need_processing?(string) + string = process_soft_hyphens(string[0..-2]) + string[-1..-1] + end + else + if soft_hyphens_need_processing?(string) + string = process_soft_hyphens(string) + end end + case direction when :rtl string.reverse @@ -224,19 +234,20 @@ def exclude_trailing_white_space? @format_state[:exclude_trailing_white_space] end + def soft_hyphens_need_processing?(string) + string.length > 0 && normalized_soft_hyphen + end + def normalized_soft_hyphen @format_state[:normalized_soft_hyphen] end def process_soft_hyphens(string) - if string.length > 0 && normalized_soft_hyphen - if string.encoding != normalized_soft_hyphen.encoding - string.force_encoding(normalized_soft_hyphen.encoding) - end - string[0..-2].gsub(normalized_soft_hyphen, "") + string[-1..-1] - else - string + if string.encoding != normalized_soft_hyphen.encoding + string.force_encoding(normalized_soft_hyphen.encoding) end + + string.gsub(normalized_soft_hyphen, "") end def strip_zero_width_spaces(string) diff --git a/spec/line_wrap_spec.rb b/spec/line_wrap_spec.rb index 7b8e1d224..484953f30 100644 --- a/spec/line_wrap_spec.rb +++ b/spec/line_wrap_spec.rb @@ -128,25 +128,28 @@ string.should == "hello#{Prawn::Text::SHY}" end - it "should not display soft hyphens except at the end of a line" do - string = @pdf.font.normalize_encoding("hello#{Prawn::Text::SHY}world") - array = [{ :text => string }] + it "should not display soft hyphens except at the end of a line " + + "for more than one element in format_array", :issue => 347 do + string1 = @pdf.font.normalize_encoding("hello#{Prawn::Text::SHY}world ") + string2 = @pdf.font.normalize_encoding("hi#{Prawn::Text::SHY}earth") + array = [{ :text => string1 }, { :text => string2 }] @arranger.format_array = array string = @line_wrap.wrap_line(:arranger => @arranger, :width => 300, :document => @pdf) - string.should == "helloworld" + string.should == "helloworld hiearth" @pdf.font("#{Prawn::DATADIR}/fonts/DejaVuSans.ttf") @line_wrap = Prawn::Text::Formatted::LineWrap.new - string = "hello#{Prawn::Text::SHY}world" - array = [{ :text => string }] + string1 = "hello#{Prawn::Text::SHY}world " + string2 = @pdf.font.normalize_encoding("hi#{Prawn::Text::SHY}earth") + array = [{ :text => string1 }, { :text => string2 }] @arranger.format_array = array string = @line_wrap.wrap_line(:arranger => @arranger, :width => 300, :document => @pdf) - string.should == "helloworld" + string.should == "helloworld hiearth" end it "should not break before a hard hyphen that follows a word" do