Skip to content

Commit

Permalink
Fix issue with soft hyphen width calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario1988 authored and practicingruby committed Oct 17, 2014
1 parent 75abbcf commit 766a534
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/prawn/text/formatted/line_wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 26 additions & 10 deletions spec/line_wrap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -349,3 +364,4 @@
@line_wrap.paragraph_finished?.should == true
end
end

0 comments on commit 766a534

Please sign in to comment.