prawn-icon 2.0.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,7 +19,7 @@ Prawn::Document.generate('paymentfont.pdf') do
19
19
  define_grid(columns: 6, rows: 12, gutter: 16)
20
20
 
21
21
  sub_header = 'PaymentFont'
22
- link = 'http://paymentfont.io'
22
+ link = 'https://paymentfont.com'
23
23
  page_header sub_header, link
24
24
 
25
25
  first_page_icons icons do |icon_key|
data/lib/prawn/icon.rb CHANGED
@@ -11,5 +11,6 @@ require_relative 'icon/base'
11
11
  require_relative 'icon/font_data'
12
12
  require_relative 'icon/parser'
13
13
  require_relative 'icon/interface'
14
+ require_relative 'icon/compatibility'
14
15
 
15
16
  Prawn::Document.extensions << Prawn::Icon::Interface
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ #
3
+ # compatibility.rb - Prawn::Icon FontAwesome 4/5 compatibility shim.
4
+ #
5
+ # Copyright March 2018, Jesse Doyle. All rights reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+
9
+ module Prawn
10
+ class Icon
11
+ class Compatibility
12
+ SHIMS = YAML.load_file(
13
+ File.join(
14
+ Base::FONTDIR,
15
+ 'fa4',
16
+ 'shims.yml'
17
+ )
18
+ ).freeze
19
+
20
+ attr_accessor :key
21
+
22
+ def initialize(opts = {})
23
+ self.key = opts.fetch(:key)
24
+ end
25
+
26
+ def translate(io = STDERR)
27
+ @translate ||= begin
28
+ if key.start_with?('fa-')
29
+ map.tap { |replaced| warning(replaced, key, io) }
30
+ else
31
+ key
32
+ end
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def map
39
+ SHIMS.fetch(key) do
40
+ # FontAwesome shim metadata assumes "fas" as the default
41
+ # font family if not explicity referenced.
42
+ "fas-#{key.sub(/fa-/, '')}"
43
+ end
44
+ end
45
+
46
+ def warning(new_key, old_key, io)
47
+ io.puts <<-DEPRECATION
48
+ [Prawn::Icon - DEPRECATION WARNING]
49
+ FontAwesome 4 icon was referenced as '#{old_key}'.
50
+ Use the FontAwesome 5 icon '#{new_key}' instead.
51
+ This compatibility layer will be removed in Prawn::Icon 3.0.0.
52
+ DEPRECATION
53
+ end
54
+ end
55
+ end
56
+ end
57
+ # rubocop:enable Metrics/ClassLength
@@ -62,6 +62,7 @@ module Prawn
62
62
  # inline_format: true
63
63
  #
64
64
  def icon(key, opts = {})
65
+ key = translate_key(key)
65
66
  make_icon(key, opts).tap(&:render)
66
67
  end
67
68
 
@@ -81,7 +82,8 @@ module Prawn
81
82
  # the underlying text method call.
82
83
  #
83
84
  def make_icon(key, opts = {})
84
- if opts[:inline_format]
85
+ key = translate_key(key)
86
+ if opts.fetch(:inline_format, false)
85
87
  inline_icon(key, opts)
86
88
  else
87
89
  Icon.new(key, self, opts)
@@ -151,6 +153,7 @@ module Prawn
151
153
  # pdf.table(data) => (2 x 2 table)
152
154
  #
153
155
  def table_icon(key, opts = {})
156
+ key = translate_key(key)
154
157
  if opts[:inline_format]
155
158
  content = Icon::Parser.format(self, key)
156
159
  opts.merge(content: content)
@@ -161,6 +164,10 @@ module Prawn
161
164
 
162
165
  private
163
166
 
167
+ def translate_key(key)
168
+ Compatibility.new(key: key).translate
169
+ end
170
+
164
171
  def icon_box(content, opts = {}) # :nodoc:
165
172
  Text::Formatted::Box.new(content, opts).tap do |box|
166
173
  box.render(dry_run: true)
@@ -176,8 +183,7 @@ module Prawn
176
183
 
177
184
  def initialize(key, document, opts = {})
178
185
  @pdf = document
179
- @set = opts[:set] ||
180
- FontData.specifier_from_key(key)
186
+ @set = opts.fetch(:set) { FontData.specifier_from_key(key) }
181
187
  @data = FontData.load(document, @set)
182
188
  @key = strip_specifier_from_key(key)
183
189
  @unicode = @data.unicode(@key)
@@ -57,7 +57,6 @@ module Prawn
57
57
  tokens.each do |token|
58
58
  # Skip the closing tag
59
59
  next if token =~ /<\/icon>/i
60
- icon = {}
61
60
 
62
61
  # Convert [[1,2], [3,4]] to { :1 => 2, :3 => 4 }
63
62
  attrs = token.scan(ATTR_REGEX).inject({}) do |k, v|
@@ -65,8 +64,7 @@ module Prawn
65
64
  k.merge!(val)
66
65
  end
67
66
 
68
- icon.merge!(attrs)
69
- array << icon
67
+ array << attrs
70
68
  end
71
69
  end
72
70
  end
@@ -101,13 +99,14 @@ module Prawn
101
99
  def keys_to_unicode(document, content, config)
102
100
  [].tap do |icons|
103
101
  content.each_with_index do |icon, index|
102
+ key = Compatibility.new(key: icon).translate
104
103
  options ||= {}
105
104
  options = config[index] if config.any?
106
105
  info = {
107
- set: FontData.specifier_from_key(icon),
106
+ set: FontData.specifier_from_key(key),
108
107
  size: options[:size],
109
108
  color: options[:color],
110
- content: FontData.unicode_from_key(document, icon)
109
+ content: FontData.unicode_from_key(document, key)
111
110
  }
112
111
  icons << info
113
112
  end
@@ -8,6 +8,6 @@
8
8
 
9
9
  module Prawn
10
10
  class Icon
11
- VERSION = '2.0.0'.freeze
11
+ VERSION = '2.5.0'.freeze
12
12
  end
13
13
  end
@@ -77,17 +77,17 @@ describe Prawn::Icon::Interface do
77
77
 
78
78
  context 'invalid icon key' do
79
79
  it 'should raise IconNotFound' do
80
- proc = Proc.new { pdf.icon 'far-__INVALID' }
81
-
82
- expect(proc).to raise_error(Prawn::Icon::Errors::IconNotFound)
80
+ expect { pdf.icon('far-__INVALID') }.to raise_error(
81
+ Prawn::Icon::Errors::IconNotFound
82
+ )
83
83
  end
84
84
  end
85
85
 
86
86
  context 'invalid specifier' do
87
87
  it 'should raise UnknownFont' do
88
- proc = Proc.new { pdf.icon '__INVALID__' }
89
-
90
- expect(proc).to raise_error(Prawn::Errors::UnknownFont)
88
+ expect { pdf.icon('__INVALID__') }.to raise_error(
89
+ Prawn::Errors::UnknownFont
90
+ )
91
91
  end
92
92
  end
93
93
  end
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright March 2018, Jesse Doyle. All rights reserved.
4
+ #
5
+ # This is free software. Please see the LICENSE and COPYING files for details.
6
+
7
+ describe Prawn::Icon::Compatibility do
8
+ describe '#translate' do
9
+ let(:stderr) { StringIO.new }
10
+ subject { described_class.new(key: key) }
11
+
12
+ context 'with a non-deprecated key' do
13
+ let(:key) { 'fas-adjust' }
14
+
15
+ it 'does not write to STDERR' do
16
+ value = subject.translate(stderr)
17
+ stderr.rewind
18
+ expect(stderr.read).to be_empty
19
+ end
20
+
21
+ it 'returns the original key' do
22
+ expect(subject.translate(stderr)).to eq(key)
23
+ end
24
+ end
25
+
26
+ context 'with a depreacted FontAwesome key' do
27
+ let(:key) { 'fa-birthday-cake' }
28
+ let(:mapped_key) { 'fas-birthday-cake' }
29
+
30
+ it 'writes a deprecation warning to STDERR' do
31
+ subject.translate(stderr)
32
+ stderr.rewind
33
+ errors = stderr.read
34
+ expect(errors).to include('DEPRECATION')
35
+ expect(errors).to include(key)
36
+ expect(errors).to include(mapped_key)
37
+ end
38
+
39
+ it 'returns the mapped key' do
40
+ expect(subject.translate(stderr)).to eq(mapped_key)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -12,8 +12,6 @@ describe Prawn::Icon::Errors::IconKeyEmpty do
12
12
  end
13
13
 
14
14
  it 'is thrown on a missing icon key' do
15
- proc = Proc.new { pdf.icon '' }
16
-
17
- expect(proc).to raise_error(described_class)
15
+ expect { pdf.icon('') }.to raise_error(described_class)
18
16
  end
19
17
  end
@@ -12,8 +12,6 @@ describe Prawn::Icon::Errors::IconNotFound do
12
12
  end
13
13
 
14
14
  it 'is thrown on an invalid icon key' do
15
- proc = Proc.new { pdf.icon 'far-an invalid key' }
16
-
17
- expect(proc).to raise_error(described_class)
15
+ expect { pdf.icon('far-an invalid key') }.to raise_error(described_class)
18
16
  end
19
17
  end
@@ -72,15 +72,15 @@ describe Prawn::Icon::FontData do
72
72
  end
73
73
 
74
74
  it 'should error when key is nil' do
75
- proc = Proc.new { Prawn::Icon::FontData.specifier_from_key nil }
76
-
77
- expect(proc).to raise_error(Prawn::Icon::Errors::IconKeyEmpty)
75
+ expect { Prawn::Icon::FontData.specifier_from_key(nil) }.to raise_error(
76
+ Prawn::Icon::Errors::IconKeyEmpty
77
+ )
78
78
  end
79
79
 
80
80
  it 'should error when key is an empty string' do
81
- proc = Proc.new { Prawn::Icon::FontData.specifier_from_key '' }
82
-
83
- expect(proc).to raise_error(Prawn::Icon::Errors::IconKeyEmpty)
81
+ expect { Prawn::Icon::FontData.specifier_from_key('') }.to raise_error(
82
+ Prawn::Icon::Errors::IconKeyEmpty
83
+ )
84
84
  end
85
85
 
86
86
  it 'should handle strings without any dashes properly' do
@@ -141,9 +141,9 @@ describe Prawn::Icon::FontData do
141
141
  end
142
142
 
143
143
  it 'should raise an error if unable to match a key' do
144
- proc = Proc.new { fontawesome.unicode('an invalid sequence') }
145
-
146
- expect(proc).to raise_error(Prawn::Icon::Errors::IconNotFound)
144
+ expect { fontawesome.unicode('an invalid sequence') }.to raise_error(
145
+ Prawn::Icon::Errors::IconNotFound
146
+ )
147
147
  end
148
148
  end
149
149
 
@@ -19,25 +19,25 @@ describe Prawn::Icon do
19
19
  end
20
20
 
21
21
  it 'should raise an error if icon key is not found' do
22
- proc = Proc.new { Prawn::Icon.new 'far-__INVALID__', pdf }
23
-
24
- expect(proc).to raise_error(errors::IconNotFound)
22
+ expect { Prawn::Icon.new('far-__INVALID__', pdf) }.to raise_error(
23
+ errors::IconNotFound
24
+ )
25
25
  end
26
26
  end
27
27
 
28
28
  context 'invalid icon specifier' do
29
29
  it 'should raise an error' do
30
- proc = Proc.new { pdf.icon '__INVALID__ some text' }
31
-
32
- expect(proc).to raise_error(Prawn::Errors::UnknownFont)
30
+ expect { pdf.icon('__INVALID__ some text') }.to raise_error(
31
+ Prawn::Errors::UnknownFont
32
+ )
33
33
  end
34
34
  end
35
35
 
36
36
  context 'without a pdf object' do
37
37
  it 'should raise an ArgumentError' do
38
- proc = Proc.new { Prawn::Icon.new('far-address-book') }
39
-
40
- expect(proc).to raise_error(ArgumentError)
38
+ expect { Prawn::Icon.new('far-address-book') }.to raise_error(
39
+ ArgumentError
40
+ )
41
41
  end
42
42
  end
43
43
  end
@@ -80,9 +80,9 @@ describe Prawn::Icon do
80
80
 
81
81
  context 'without dashes in key' do
82
82
  it 'raise an error about invalid keys' do
83
- proc = Proc.new { Prawn::Icon.new 'some invalid key', pdf }
84
-
85
- expect(proc).to raise_error(Prawn::Errors::UnknownFont)
83
+ expect { Prawn::Icon.new('some invalid key', pdf) }.to raise_error(
84
+ Prawn::Errors::UnknownFont
85
+ )
86
86
  end
87
87
  end
88
88
  end
@@ -98,9 +98,9 @@ describe Prawn::Icon do
98
98
 
99
99
  context 'invalid icon key' do
100
100
  it 'should raise IconNotFound' do
101
- proc = Proc.new { Prawn::Icon.new 'far-__INVALID__', pdf }
102
-
103
- expect(proc).to raise_error(errors::IconNotFound)
101
+ expect { Prawn::Icon.new('far-__INVALID__', pdf) }.to raise_error(
102
+ errors::IconNotFound
103
+ )
104
104
  end
105
105
  end
106
106
  end
@@ -38,16 +38,18 @@ describe Prawn::Icon::Parser do
38
38
 
39
39
  it 'should raise an error when an icon key is invalid' do
40
40
  string = '<icon>an invalid key</icon>'
41
- proc = Proc.new { Prawn::Icon::Parser.format(pdf, string) }
42
41
 
43
- expect(proc).to raise_error(Prawn::Errors::UnknownFont)
42
+ expect { Prawn::Icon::Parser.format(pdf, string) }.to raise_error(
43
+ Prawn::Errors::UnknownFont
44
+ )
44
45
  end
45
46
 
46
47
  it 'should raise an error when an icon is not found for valid set' do
47
48
  string = '<icon>far-__INVALID__</icon>'
48
- proc = Proc.new { Prawn::Icon::Parser.format(pdf, string) }
49
49
 
50
- expect(proc).to raise_error(Prawn::Icon::Errors::IconNotFound)
50
+ expect { Prawn::Icon::Parser.format(pdf, string) }.to raise_error(
51
+ Prawn::Icon::Errors::IconNotFound
52
+ )
51
53
  end
52
54
  end
53
55
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn-icon
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse Doyle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-08 00:00:00.000000000 Z
11
+ date: 2019-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prawn
@@ -133,6 +133,7 @@ files:
133
133
  - README.md
134
134
  - Rakefile
135
135
  - data/fonts/DejaVuSans.ttf
136
+ - data/fonts/fa4/shims.yml
136
137
  - data/fonts/fab/LICENSE
137
138
  - data/fonts/fab/fa-brands.ttf
138
139
  - data/fonts/fab/fab.yml
@@ -156,6 +157,7 @@ files:
156
157
  - examples/paymentfont.rb
157
158
  - lib/prawn/icon.rb
158
159
  - lib/prawn/icon/base.rb
160
+ - lib/prawn/icon/compatibility.rb
159
161
  - lib/prawn/icon/errors.rb
160
162
  - lib/prawn/icon/font_data.rb
161
163
  - lib/prawn/icon/interface.rb
@@ -167,6 +169,7 @@ files:
167
169
  - spec/support/parser_helper.rb
168
170
  - spec/support/pdf_helper.rb
169
171
  - spec/unit/base_spec.rb
172
+ - spec/unit/compatibility_spec.rb
170
173
  - spec/unit/errors/icon_key_empty_spec.rb
171
174
  - spec/unit/errors/icon_not_found_spec.rb
172
175
  - spec/unit/font_data_spec.rb
@@ -194,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
197
  version: 1.3.6
195
198
  requirements: []
196
199
  rubyforge_project:
197
- rubygems_version: 2.5.1
200
+ rubygems_version: 2.7.6.2
198
201
  signing_key:
199
202
  specification_version: 4
200
203
  summary: Provides icon fonts for PrawnPDF