From f59f681f83b1aab588d75ba2258c977171fa18e0 Mon Sep 17 00:00:00 2001 From: Jesse Doyle Date: Sat, 15 Oct 2016 12:06:07 -0600 Subject: [PATCH] Interface - v 1.3.0 * 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. --- .gitignore | 1 + CHANGELOG.md | 6 ++ lib/prawn/icon.rb | 203 +--------------------------------- lib/prawn/icon/interface.rb | 209 ++++++++++++++++++++++++++++++++++++ lib/prawn/icon/version.rb | 2 +- prawn-icon.gemspec | 4 +- spec/spec_helper.rb | 4 + 7 files changed, 225 insertions(+), 204 deletions(-) create mode 100644 lib/prawn/icon/interface.rb diff --git a/.gitignore b/.gitignore index dfd6065..aae3ee2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ Gemfile.lock /doc .yardoc/ +coverage/ diff --git a/CHANGELOG.md b/CHANGELOG.md index d869a1e..99ecf79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/lib/prawn/icon.rb b/lib/prawn/icon.rb index 7c3fa0f..3c3b7f7 100644 --- a/lib/prawn/icon.rb +++ b/lib/prawn/icon.rb @@ -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 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 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 'fa-arrows', - # 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 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 - # 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 diff --git a/lib/prawn/icon/interface.rb b/lib/prawn/icon/interface.rb new file mode 100644 index 0000000..b4e7af1 --- /dev/null +++ b/lib/prawn/icon/interface.rb @@ -0,0 +1,209 @@ +# encoding: utf-8 +# +# interface.rb: Prawn extension module and logic. +# +# Copyright October 2016, Jesse Doyle. All rights reserved. +# +# This is free software. Please see the LICENSE and COPYING files for details. + +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 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 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 'fa-arrows', + # 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 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 + # 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 diff --git a/lib/prawn/icon/version.rb b/lib/prawn/icon/version.rb index bd68491..a6a0fd1 100644 --- a/lib/prawn/icon/version.rb +++ b/lib/prawn/icon/version.rb @@ -8,6 +8,6 @@ module Prawn class Icon - VERSION = '1.2.0'.freeze + VERSION = '1.3.0'.freeze end end diff --git a/prawn-icon.gemspec b/prawn-icon.gemspec index 476e1e6..d88823b 100644 --- a/prawn-icon.gemspec +++ b/prawn-icon.gemspec @@ -1,6 +1,7 @@ basedir = File.expand_path(File.dirname(__FILE__)) require "#{basedir}/lib/prawn/icon/version" +# rubocop:disable Metrics/BlockLength Gem::Specification.new do |spec| spec.name = 'prawn-icon' spec.version = Prawn::Icon::VERSION @@ -26,9 +27,10 @@ Gem::Specification.new do |spec| spec.add_development_dependency('pdf-inspector', '~> 1.2.1') spec.add_development_dependency('rspec', '~> 3.5.0') - spec.add_development_dependency('rubocop', '~> 0.38.0') + spec.add_development_dependency('rubocop', '~> 0.44.1') spec.add_development_dependency('rake') spec.add_development_dependency('pdf-reader', '~> 1.4') + spec.add_development_dependency('simplecov', '~> 0.12') spec.description = <<-END_DESC Prawn::Icon provides various icon fonts including diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b1ef468..18088b7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,10 @@ # Copyright October 2014, Jesse Doyle. All rights reserved. # # This is free software. Please see the LICENSE and COPYING files for details. +# +# +require 'simplecov' +SimpleCov.start require "bundler" Bundler.setup