Skip to content

Commit

Permalink
Merge pull request #795 from prawnpdf/rtl_indent_paragraphs
Browse files Browse the repository at this point in the history
Indent right side on RTL text
  • Loading branch information
practicingruby committed Oct 30, 2014
2 parents b9fab26 + e456971 commit 0c583d8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/prawn/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module Text
# the current font familly. [current style]
# <tt>:indent_paragraphs</tt>:: <tt>number</tt>. The amount to indent the
# first line of each paragraph. Omit this
# option if you do not want indenting
# option if you do not want indenting.
# <tt>:direction</tt>::
# <tt>:ltr</tt>, <tt>:rtl</tt>, Direction of the text (left-to-right
# or right-to-left) [value of document.text_direction]
Expand Down Expand Up @@ -359,7 +359,10 @@ def draw_remaining_formatted_text_on_new_pages(remaining_text, options)
end

def draw_indented_formatted_line(string, options)
indent(@indent_paragraphs) do
gap = options.fetch(:direction, :ltr) == :ltr ?
[@indent_paragraphs, 0] : [0, @indent_paragraphs]

indent(*gap) do
fill_formatted_text_box(string, options.dup.merge(:single_line => true))
end
end
Expand Down
8 changes: 8 additions & 0 deletions manual/text/paragraph_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@
text "This paragraph will be indented. " * 10 +
"\n" + "This one will too. " * 10,
:indent_paragraphs => 60

move_down 20

text "FROM RIGHT TO LEFT:"
text "This paragraph will be indented. " * 10 +
"\n" + "This one will too. " * 10,
:indent_paragraphs => 60, :direction => :rtl

end
2 changes: 1 addition & 1 deletion prawn.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
spec.add_dependency('ttfunk', '~> 1.4.0')
spec.add_dependency('pdf-core', "~> 0.5.0")

spec.add_development_dependency('pdf-inspector', '~> 1.1.0')
spec.add_development_dependency('pdf-inspector', '~> 1.2.0')
spec.add_development_dependency('yard')
spec.add_development_dependency('rspec', '2.14.1')
spec.add_development_dependency('mocha')
Expand Down
51 changes: 51 additions & 0 deletions spec/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,57 @@
text.strings[3].should == ("hello " * 19).strip
text.strings[4].should == ("hello " * 21).strip
end


it "should indent from right side when using :rtl direction" do
para1 = "The rain in spain falls mainly on the plains " * 3
para2 = "The rain in spain falls mainly on the plains " * 3

@pdf.text(para1 + "\n" + para2, :indent_paragraphs => 60, :direction => :rtl)

text = PDF::Inspector::Text.analyze(@pdf.render)

lines = text.strings
x_positions = text.positions.map { |e| e[0] }

# NOTE: The code below reflects Prawn's current kerning behavior for RTL
# text, which isn't necessarily correct. If we change that behavior,
# this test will need to be updated.

x_positions[0].should(
be_within(0.001).of(@pdf.bounds.absolute_right - 60 -
@pdf.width_of(lines[0].reverse, :kerning => true)))

x_positions[1].should(
be_within(0.001).of(@pdf.bounds.absolute_right -
@pdf.width_of(lines[1].reverse, :kerning => true)))

x_positions[2].should(
be_within(0.001).of(@pdf.bounds.absolute_right - 60 -
@pdf.width_of(lines[2].reverse, :kerning => true)))

x_positions[3].should(
be_within(0.001).of(@pdf.bounds.absolute_right -
@pdf.width_of(lines[3].reverse, :kerning => true)))
end

it "should indent from right side when using :ltr direction" do
para1 = "The rain in spain falls mainly on the plains " * 3
para2 = "The rain in spain falls mainly on the plains " * 3

@pdf.text(para1 + "\n" + para2, :indent_paragraphs => 60, :direction => :ltr)

text = PDF::Inspector::Text.analyze(@pdf.render)

x_positions = text.positions.map { |e| e[0] }

x_positions[0].should == 60
x_positions[1].should == 0

x_positions[2].should == 60
x_positions[3].should == 0
end

describe "when wrap to new page, and first line of new page" +
" is not the start of a new paragraph, that line should" +
" not be indented" do
Expand Down

0 comments on commit 0c583d8

Please sign in to comment.