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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added #link into text.rb + tests #764

Closed
wants to merge 6 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/prawn/text.rb
Expand Up @@ -149,7 +149,7 @@ module Text
#
# Raises <tt>ArgumentError</tt> if <tt>:at</tt> option included
#
# Raises <tt>Prawn::Errrors::CannotFit</tt> if not wide enough to print
# Raises <tt>Prawn::Errors::CannotFit</tt> if not wide enough to print
# any text
#
def text(string, options={})
Expand Down Expand Up @@ -223,6 +223,29 @@ def formatted_text(array, options={})
end
end

# Draws a link into a page.
# With this helper you can add a link to a page without having to use the tag format
#
# == Example
#
# link('Go to the wiki',
# 'https://github.com/prawnpdf/prawn/wiki',
# :size => 24,
# :styles => [:bold, :italic])
#
# == Options
#
# Accepts the same options as #text
#
# == Exceptions
#
# Same as for #text
#
def link(title, url, options={})
return false unless title && url
formatted_text([{ :text => title, :link => url }], options)
end

# Draws text on the page, beginning at the point specified by the :at option
# the string is assumed to be pre-formatted to properly fit the page.
#
Expand Down
36 changes: 36 additions & 0 deletions spec/text_spec.rb
Expand Up @@ -340,6 +340,42 @@
pages[1][:strings].should == ["World"]
end

describe "#link" do
it 'should generate a clickable link' do
link = 'http://example.com'
@pdf.link 'Example', link
@pdf.render.should =~ /^(\/URI \(#{link}\).*?)/
end

it 'should allow fonts' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for readability a blank line between examples would be nice.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed 馃槃

link = 'http://example.com'
@pdf.fallback_fonts ["Helvetica"]

@pdf.font "Times-Roman", :style => :italic do
@pdf.link 'Example', link
end

text = PDF::Inspector::Text.analyze(@pdf.render)
fonts_used = text.font_settings.map { |e| e[:name] }
fonts_used.length.should == 1
fonts_used[0].should == :"Times-Italic"
text.strings[0].should == "Example"
end

it 'should accept #text options' do
link = 'http://example.com'
@pdf.link 'Example', link, :size => 16
text = PDF::Inspector::Text.analyze(@pdf.render)
text.font_settings[0][:size].should == 16
end

it 'should return false without text or url' do
link = 'http://example.com'
@pdf.link(nil, link).should == false
@pdf.link('Example', nil).should == false
end
end

describe "with :indent_paragraphs option" do
it "should indent the paragraphs" do
hello = "hello " * 50
Expand Down