diff --git a/lib/prawn/text/formatted/line_wrap.rb b/lib/prawn/text/formatted/line_wrap.rb index d4e986f1e..9928125d0 100644 --- a/lib/prawn/text/formatted/line_wrap.rb +++ b/lib/prawn/text/formatted/line_wrap.rb @@ -102,6 +102,10 @@ def add_fragment_to_line(fragment) if @accumulated_width + segment_width <= @width @accumulated_width += segment_width + if segment[-1] == soft_hyphen + sh_width = @document.width_of("#{soft_hyphen}", :kerning => @kerning) + @accumulated_width -= sh_width + end @fragment_output += segment else end_of_the_line_reached(segment) diff --git a/spec/line_wrap_spec.rb b/spec/line_wrap_spec.rb index 484953f30..3e6669706 100644 --- a/spec/line_wrap_spec.rb +++ b/spec/line_wrap_spec.rb @@ -128,21 +128,36 @@ string.should == "hello#{Prawn::Text::SHY}" end + it "should ignore width of a soft-hyphen during adding fragments to line", :issue =>775 do + hyphen_string = "Hy#{Prawn::Text::SHY}phe#{Prawn::Text::SHY}nat#{Prawn::Text::SHY}ions " + string1 = @pdf.font.normalize_encoding(hyphen_string * 5) + string2 = @pdf.font.normalize_encoding("Hyphenations " * 3 + hyphen_string) + + array1 = [{text: string1}] + array2 = [{text: string2}] + + @arranger.format_array = array1 + + res1 = @line_wrap.wrap_line(:arranger => @arranger, + :width => 300, + :document => @pdf) + + @line_wrap = Prawn::Text::Formatted::LineWrap.new + + @arranger.format_array = array2 + + res2 = @line_wrap.wrap_line(:arranger => @arranger, + :width => 300, + :document => @pdf) + res1.should == res2 + end + 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 hiearth" - @pdf.font("#{Prawn::DATADIR}/fonts/DejaVuSans.ttf") @line_wrap = Prawn::Text::Formatted::LineWrap.new - string1 = "hello#{Prawn::Text::SHY}world " + 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 @@ -349,3 +364,4 @@ @line_wrap.paragraph_finished?.should == true end end +