Skip to content

Commit

Permalink
Interface - v 1.3.0
Browse files Browse the repository at this point in the history
* Break out Prawn::Icon::Interface into its own file [#27].
* Add simplecov as a development dependency.
* Update rubocop to 0.44.1.
* Bump version to 1.3.0.
  • Loading branch information
jessedoyle committed Oct 15, 2016
1 parent 1e4522d commit f59f681
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 204 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
Gemfile.lock
/doc
.yardoc/
coverage/
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.3.0 - Oct 15, 2016

- Update rubocop developement dependency (to `0.44.1`).
- Add `simplecov` as a development dependency and require 100% coverage.
- Break out `Prawn::Icon::Interface` into its own file. This resolves issue [#27](https://github.com/jessedoyle/prawn-icon/issues/27).

# 1.2.0 - Sept 12, 2016

- Update FontAwesome from v4.5.0 to v4.6.3. See [changelog](http://fontawesome.io/icons#new).
Expand Down
203 changes: 1 addition & 202 deletions lib/prawn/icon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,207 +9,6 @@
require_relative 'icon/base'
require_relative 'icon/font_data'
require_relative 'icon/parser'

module Prawn
# Easy icon font usage within Prawn. Currently
# supported icon fonts include: FontAwesome,
# Zurb Foundicons, GitHub Octicons, as well as
# PaymentFont.
#
# = Icon Keys
#
# Icon keys must be supplied to most +Prawn::Icon+
# methods. Keys map directly to a unicode character
# within the font that produces a given icon. As a
# rule, included icon keys should match the keys from
# the font provider. The icon key mapping is specified
# in the font's +legend_file+, which is a +YAML+ file
# located in Prawn::Icon::Base::FONTDIR/font/font.yml.
#
# Prawn::Icon::
# Houses the methods and interfaces necessary for
# rendering icons to the Prawn::Document.
#
# Prawn::Icon::FontData::
# Used to store various information about an icon font,
# including the key-to-unicode mapping information.
# Also houses methods to cache and lazily load the
# requested font data on a document basis.
#
# Prawn::Icon::Parser::
# Used to initially parse icons that are used with the
# inline_format: true option. The input string is parsed
# once for <icon></icon> tags, then the output is provided
# to Prawn's internal formatted text parser.
#
class Icon
FONTDIR = Icon::Base::FONTDIR

module Interface
# 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 'fa-beer'
# pdf.icon '<icon color="0099FF">fa-arrows</icon>',
# inline_format: true
#
def icon(key, opts = {})
make_icon(key, opts).tap(&:render)
end

# Initialize a new icon object, but do
# not render it to the document.
#
# == 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.
#
def make_icon(key, opts = {})
if opts[:inline_format]
inline_icon(key, opts)
else
Icon.new(key, self, opts)
end
end

# Initialize a new formatted text box containing
# icon information, but don't render it to the
# document.
#
# == 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.
#
def inline_icon(text, opts = {})
parsed = Icon::Parser.format(self, text)
content = Text::Formatted::Parser.format(parsed)
box_options = opts.merge(
inline_format: true,
document: self,
at: [bounds.left, cursor]
)
icon_box(content, box_options)
end

# 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: 'fa', content: '\uf047' }
#
# 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('fa-arrows'), 'Cell 1'],
# ['Cell 2', 'Cell 3']
# ]
#
# pdf.table(data) => (2 x 2 table)
#
def table_icon(key, opts = {})
if opts[:inline_format]
content = Icon::Parser.format(self, key)
opts.merge(content: content)
else
make_icon(key, opts).format_hash
end
end

private

def icon_box(content, opts = {}) # :nodoc:
Text::Formatted::Box.new(content, opts).tap do |box|
box.render(dry_run: true)
self.y -= box.height
unless opts.fetch(:final_gap, true)
self.y -= box.line_gap + box.leading
end
end
end
end

attr_reader :set, :unicode

def initialize(key, document, opts = {})
@pdf = document
@set = opts[:set] ||
FontData.specifier_from_key(key)
@data = FontData.load(document, @set)
@key = strip_specifier_from_key(key)
@unicode = @data.unicode(@key)
@options = opts
end

def format_hash
base = { font: @set.to_s, content: @unicode }
opts = @options.dup
# Prawn::Table renames :color to :text_color
opts[:text_color] = opts.delete(:color)
base.merge(opts)
end

def render
@pdf.font(@data.path) do
@pdf.text @unicode, @options
end
end

private

def strip_specifier_from_key(key) # :nodoc:
reg = Regexp.new "#{@data.specifier}-"
key.sub(reg, '') # Only one specifier
end
end
end
require_relative 'icon/interface'

Prawn::Document.extensions << Prawn::Icon::Interface
Loading

0 comments on commit f59f681

Please sign in to comment.