Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soft hyphens #774

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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