Module: Prawn::Icon::Interface

Defined in:
lib/prawn/icon/interface.rb

Instance Method Summary collapse

Instance Method Details

#formatted_icon_box(text, opts = {}) ⇒ Object

Initialize a formatted icon box from an icon-conatining string. Content is not directly rendered to the document, instead a Prawn::Text::Formatted::Box instance is returned that responds to the render method.

Parameters:

text

Input text to be parsed initially for <icon> tags, then passed to Prawn’s formatted text parser.

opts

A hash of options that may be supplied to the underlying text call.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/prawn/icon/interface.rb', line 128

def formatted_icon_box(text, opts = {})
  parsed = Icon::Parser.format(self, text)
  content = Text::Formatted::Parser.format(parsed)
  position = opts.fetch(:at) do
    [
      opts.fetch(:x) { bounds.left },
      opts.fetch(:y) { cursor }
    ]
  end
  box_options = opts.merge(
    inline_format: true,
    document: self,
    at: position
  )
  icon_box(content, box_options)
end

#icon(key, opts = {}) ⇒ Object

Set up and draw an icon on this document. This method operates much like Prawn::Text::Box.

Parameters:

key

Contains the key to a particular icon within a font family. If :inline_format is true, then key may contain formatted text marked with <icon></icon> tags and any tag supported by Prawn’s parser.

opts

A hash of options that may be supplied to the underlying text method call.

Examples:

pdf.icon 'fas-beer'
pdf.icon '<icon color="0099FF">fas-user-circle</icon>',
inline_format: true


65
66
67
68
# File 'lib/prawn/icon/interface.rb', line 65

def icon(key, opts = {})
  key = translate_key(key)
  make_icon(key, opts).tap { |i| i && i.render }
end

#inline_icon(text, opts = {}) ⇒ Object

Render formatted icon content to the document from a string containing icons. Content will correctly transition to a new page when necessary.

Parameters:

text

Input text to be parsed initially for <icon> tags, then passed to Prawn’s formatted text parser.

opts

A hash of options that may be supplied to the underlying text call.



107
108
109
110
111
# File 'lib/prawn/icon/interface.rb', line 107

def inline_icon(text, opts = {})
  parsed = Icon::Parser.format(self, text)
  content = Text::Formatted::Parser.format(parsed)
  formatted_text(content, opts)
end

#make_icon(key, opts = {}) ⇒ Object

Initialize a new icon object.

Parameters:

key

Contains the key to a particular icon within a font family. If :inline_format is true, then key may contain formatted text marked with <icon></icon> tags and any tag supported by Prawn’s parser.

opts

A hash of options that may be supplied to the underlying text method call.



84
85
86
87
88
89
90
91
# File 'lib/prawn/icon/interface.rb', line 84

def make_icon(key, opts = {})
  key = translate_key(key)
  if opts.fetch(:inline_format, false)
    inline_icon(key, opts)
  else
    Icon.new(key, self, opts)
  end
end

#table_icon(key, opts = {}) ⇒ Object

Initialize a new Prawn::Icon, but don’t render the icon to a document. Intended to be used as an entry of a data array when combined with Prawn::Table.

Parameters:

key

Contains the key to a particular icon within a font family. The key may contain a string with format tags if inline_format: true in the opts hash.

opts

A hash of options that may be supplied to the underlying text call.

Returns:

A Hash containing +font+ and +content+ keys
that match the data necessary for the
specified icon.

eg. { font: 'fas', content: "\uf2b9" }

Note that the +font+ key will not be set
if +inline_format: true+.

Examples:

require 'prawn/table'

data = [
  # Explicit brackets must be used here
  [pdf.table_icon('fas-coffee'), 'Cell 1'],
  ['Cell 2', 'Cell 3']
]

pdf.table(data) => (2 x 2 table)


182
183
184
185
186
187
188
189
190
# File 'lib/prawn/icon/interface.rb', line 182

def table_icon(key, opts = {})
  key = translate_key(key)
  if opts[:inline_format]
    content = Icon::Parser.format(self, key)
    opts.merge(content: content)
  else
    make_icon(key, opts).format_hash
  end
end