Skip to content

Commit

Permalink
Process soft-hyphens in blocks which do not exclude trailing white space
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario1988 authored and practicingruby committed Sep 28, 2014
1 parent e54a6e8 commit 143371d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
27 changes: 19 additions & 8 deletions lib/prawn/text/formatted/fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
17 changes: 10 additions & 7 deletions spec/line_wrap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 143371d

Please sign in to comment.