prawn-icon 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright October 2014, Jesse Doyle. All rights reserved.
4
+ #
5
+ # This is free software. Please see the LICENSE and COPYING files for details.
6
+
7
+ require "bundler"
8
+ Bundler.setup
9
+
10
+ require "prawn"
11
+ require "prawn/icon"
12
+ require 'pdf/inspector'
13
+ require "rspec"
14
+
15
+ # Requires supporting ruby files with custom matchers and macros, etc,
16
+ # in spec/extensions/ and its subdirectories.
17
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
18
+
19
+ RSpec.configure do |config|
20
+ config.include PDFHelper
21
+ config.include ParserHelper
22
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright October 2014, Jesse Doyle. All rights reserved.
4
+ #
5
+ # This is free software. Please see the LICENSE and COPYING files for details.
6
+
7
+ module ParserHelper
8
+ def tokenize_string(string)
9
+ regex = Prawn::Icon::Parser::PARSER_REGEX
10
+ string.scan(regex)
11
+ end
12
+
13
+ def contentize_string(string)
14
+ regex = Prawn::Icon::Parser::CONTENT_REGEX
15
+ string.scan(regex).flatten
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright October 2014, Jesse Doyle. All rights reserved.
4
+ #
5
+ # This is free software. Please see the LICENSE and COPYING files for details.
6
+
7
+ module PDFHelper
8
+ def create_pdf
9
+ Prawn::Document.new(margin: 0)
10
+ end
11
+
12
+ def valid_unicode?(string)
13
+ string.force_encoding('UTF-8').valid_encoding?
14
+ end
15
+ end
@@ -0,0 +1,190 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright October 2014, Jesse Doyle. All rights reserved.
4
+ #
5
+ # This is free software. Please see the LICENSE and COPYING files for details.
6
+
7
+ require 'spec_helper'
8
+
9
+ describe Prawn::Icon::FontData do
10
+ describe '#initialize' do
11
+ it 'should update font_families on initialization' do
12
+ pdf = create_pdf
13
+ data = Prawn::Icon::FontData.new(pdf, set: :fa)
14
+
15
+ expect(pdf.font_families['fa']).not_to be_nil
16
+ end
17
+ end
18
+
19
+ describe '::load' do
20
+ it 'should cache font data on a document basis' do
21
+ pdf = create_pdf
22
+ data = Prawn::Icon::FontData.load(pdf, 'fa')
23
+ obj_id_1 = data.__id__
24
+ data = Prawn::Icon::FontData.load(pdf, 'fa')
25
+ obj_id_2 = data.__id__
26
+
27
+ expect(obj_id_1).to eq(obj_id_2)
28
+ end
29
+
30
+ it 'should accept a string as a specifier' do
31
+ pdf = create_pdf
32
+ data = Prawn::Icon::FontData.load(pdf, 'fa')
33
+
34
+ expect(data).not_to be_nil
35
+ end
36
+
37
+ it 'should accept a symbol as a specifer' do
38
+ pdf = create_pdf
39
+ data = Prawn::Icon::FontData.load(pdf, :fa)
40
+
41
+ expect(data).not_to be_nil
42
+ end
43
+ end
44
+
45
+ describe '::release_data' do
46
+ it 'should remove all data references if requested' do
47
+ pdf = create_pdf
48
+ Prawn::Icon::FontData.load(pdf, :fa)
49
+ Prawn::Icon::FontData.load(pdf, :fi)
50
+ Prawn::Icon::FontData.load(pdf, :octicon)
51
+ data = Prawn::Icon::FontData.release_data
52
+
53
+ expect(data).to be_empty
54
+ end
55
+ end
56
+
57
+ describe '::unicode_from_key' do
58
+ it 'should provide a UTF-8 string for a valid key' do
59
+ pdf = create_pdf
60
+ unicode = Prawn::Icon::FontData.unicode_from_key(pdf, 'fa-arrows')
61
+ valid = unicode.force_encoding('UTF-8').valid_encoding?
62
+
63
+ expect(valid).to be_true
64
+ end
65
+ end
66
+
67
+ describe '::specifier_from_key' do
68
+ it 'should provide the font specifier from a valid key' do
69
+ specifier = Prawn::Icon::FontData.specifier_from_key('fa-arrows')
70
+ expect(specifier).to eq(:fa)
71
+ end
72
+
73
+ it 'should error when key is nil' do
74
+ proc = Proc.new { Prawn::Icon::FontData.specifier_from_key nil }
75
+
76
+ expect(proc).to raise_error(Prawn::Errors::IconKeyEmpty)
77
+ end
78
+
79
+ it 'should error when key is an empty string' do
80
+ proc = Proc.new { Prawn::Icon::FontData.specifier_from_key '' }
81
+
82
+ expect(proc).to raise_error(Prawn::Errors::IconKeyEmpty)
83
+ end
84
+
85
+ it 'should handle strings without any dashes properly' do
86
+ specifier = Prawn::Icon::FontData.specifier_from_key 'foo'
87
+
88
+ expect(specifier).to eq(:foo)
89
+ end
90
+ end
91
+
92
+ describe '#font_version' do
93
+ it 'should have a font version as a string' do
94
+ pdf = create_pdf
95
+ data = Prawn::Icon::FontData.new(pdf)
96
+ version = data.font_version
97
+
98
+ expect(version.is_a? String).to be_true
99
+ end
100
+ end
101
+
102
+ describe '#legend_path' do
103
+ it 'should have a valid path to a yml file for legend' do
104
+ pdf = create_pdf
105
+ data = Prawn::Icon::FontData.new(pdf)
106
+ path = data.legend_path
107
+ extname = File.extname(path)
108
+
109
+ expect(extname).to eq('.yml')
110
+ end
111
+ end
112
+
113
+ describe '#path' do
114
+ it 'should have a valid path to a TTF file' do
115
+ pdf = create_pdf
116
+ data = Prawn::Icon::FontData.new(pdf)
117
+ path = data.path
118
+ extname = File.extname(path)
119
+
120
+ expect(extname).to eq('.ttf')
121
+ end
122
+ end
123
+
124
+ describe '#specifier' do
125
+ it 'should retrieve the string specifier from the yaml legend file' do
126
+ pdf = create_pdf
127
+ data = Prawn::Icon::FontData.new(pdf, set: :fa)
128
+ specifier = data.specifier
129
+
130
+ expect(specifier).to eq('fa')
131
+ end
132
+ end
133
+
134
+ describe '#unicode' do
135
+ it 'should provide a valid UTF-8 encoded string for a valid key' do
136
+ pdf = create_pdf
137
+ data = Prawn::Icon::FontData.new(pdf, set: :fa)
138
+ unicode = data.unicode('arrows')
139
+ valid = unicode.force_encoding('UTF-8').valid_encoding?
140
+
141
+ expect(valid).to be_true
142
+ end
143
+
144
+ it 'should raise an error if unable to match a key' do
145
+ pdf = create_pdf
146
+ data = Prawn::Icon::FontData.new(pdf, set: :fa)
147
+ proc = Proc.new { data.unicode('an invalid sequence') }
148
+
149
+ expect(proc).to raise_error(Prawn::Errors::IconNotFound)
150
+ end
151
+ end
152
+
153
+ describe '#keys' do
154
+ it 'should return a non-empty array of strings' do
155
+ pdf = create_pdf
156
+ data = Prawn::Icon::FontData.new(pdf, set: :fa)
157
+ keys = data.keys
158
+
159
+ expect(keys).not_to be_empty
160
+ expect(keys.first.is_a? String).to be_true
161
+ end
162
+
163
+ it 'should not contain the version as a key' do
164
+ pdf = create_pdf
165
+ data = Prawn::Icon::FontData.new(pdf, set: :fa)
166
+ keys = data.keys
167
+
168
+ expect(keys.include?('__font_version__')).to be_false
169
+ end
170
+ end
171
+
172
+ describe '#yaml' do
173
+ it 'should return a hash with the specifier as the first key' do
174
+ pdf = create_pdf
175
+ data = Prawn::Icon::FontData.new(pdf, set: :fa)
176
+ yaml = data.yaml
177
+ key = yaml.keys.first
178
+ mapping = yaml['fa']
179
+ inner_key = mapping.keys.last
180
+ inner_value = mapping.values.last
181
+ proc = Proc.new { inner_value.force_encoding('UTF-8').valid_encoding? }
182
+
183
+ expect(yaml.is_a? Hash).to be_true
184
+ expect(key).to eq('fa')
185
+ expect(inner_key.is_a? String).to be_true
186
+ expect(inner_value.is_a? String).to be_true
187
+ expect(proc).to be_true
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,94 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright October 2014, Jesse Doyle. All rights reserved.
4
+ #
5
+ # This is free software. Please see the LICENSE and COPYING files for details.
6
+
7
+ require 'spec_helper'
8
+
9
+ describe Prawn::Icon do
10
+ describe '#initialize' do
11
+ context 'valid icon family specifier' do
12
+ it 'should be capable of creating icon instances' do
13
+ pdf = create_pdf
14
+ icon = Prawn::Icon.new 'fa-arrows', pdf
15
+
16
+ expect(icon.unicode).to eq("\uf047")
17
+ end
18
+
19
+ it 'should raise an error if icon key is not found' do
20
+ pdf = create_pdf
21
+ proc = Proc.new { Prawn::Icon.new 'fa-__INVALID__', pdf }
22
+
23
+ expect(proc).to raise_error(Prawn::Errors::IconNotFound)
24
+ end
25
+ end
26
+
27
+ context 'invalid icon specifier' do
28
+ it 'should raise an error' do
29
+ pdf = create_pdf
30
+ proc = Proc.new { pdf.icon '__INVALID__ some text' }
31
+
32
+ expect(proc).to raise_error(Prawn::Errors::UnknownFont)
33
+ end
34
+ end
35
+
36
+ context 'without a pdf object' do
37
+ it 'should raise an ArgumentError' do
38
+ proc = Proc.new { Prawn::Icon.new('fa-arrows') }
39
+
40
+ expect(proc).to raise_error(ArgumentError)
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#render' do
46
+ it 'should render an icon to the page' do
47
+ pdf = create_pdf
48
+ pdf.icon('fa-arrows').render
49
+ text = PDF::Inspector::Text.analyze(pdf.render)
50
+
51
+ expect(text.font_settings.first[:name]).to match(/FontAwesome/)
52
+ end
53
+ end
54
+
55
+ describe '#set' do
56
+ context 'with dashes in key' do
57
+ it 'should return the set as a symbol from key' do
58
+ pdf = create_pdf
59
+ set = Prawn::Icon.new('fa-arrows', pdf).set
60
+
61
+ expect(set).to eq(:fa)
62
+ end
63
+ end
64
+
65
+ context 'without dashes in key' do
66
+ it 'raise an error about invalid keys' do
67
+ pdf = create_pdf
68
+ proc = Proc.new { Prawn::Icon.new 'some invalid key', pdf }
69
+
70
+ expect(proc).to raise_error(Prawn::Errors::UnknownFont)
71
+ end
72
+ end
73
+ end
74
+
75
+ describe '#unicode' do
76
+ context 'valid icon key' do
77
+ it 'should return a unicode character' do
78
+ pdf = create_pdf
79
+ icon = Prawn::Icon.new 'fa-arrows', pdf
80
+
81
+ expect(valid_unicode?(icon.unicode)).to be_true
82
+ end
83
+ end
84
+
85
+ context 'invalid icon key' do
86
+ it 'should raise IconNotFound' do
87
+ pdf = create_pdf
88
+ proc = Proc.new { Prawn::Icon.new 'fa-__INVALID__', pdf }
89
+
90
+ expect(proc).to raise_error(Prawn::Errors::IconNotFound)
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,190 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright October 2014, Jesse Doyle. All rights reserved.
4
+ #
5
+ # This is free software. Please see the LICENSE and COPYING files for details.
6
+
7
+ require 'spec_helper'
8
+
9
+ describe Prawn::Icon::Parser do
10
+ describe '::format' do
11
+ it 'should return a raw prawn-formatted string on valid input' do
12
+ string = '<icon>fa-arrows</icon>'
13
+ pdf = create_pdf
14
+ formatted = Prawn::Icon::Parser.format(pdf, string)
15
+ match = "<font name=\"fa\"></font>"
16
+
17
+ expect(formatted).to eq(match)
18
+ end
19
+
20
+ it 'should return unchanged if no icon tags are found' do
21
+ string = <<-EOS
22
+ <link href="#">test</link>
23
+ Here's some sample text.
24
+ <i>more text</i>
25
+ EOS
26
+ pdf = create_pdf
27
+ formatted = Prawn::Icon::Parser.format(pdf, string)
28
+
29
+ expect(formatted).to eq(string)
30
+ end
31
+
32
+ it 'should return the empty string if given the empty string' do
33
+ string = ''
34
+ pdf = create_pdf
35
+ formatted = Prawn::Icon::Parser.format(pdf, string)
36
+
37
+ expect(formatted).to be_empty
38
+ end
39
+
40
+ it 'should raise an error when an icon key is invalid' do
41
+ string = '<icon>an invalid key</icon>'
42
+ pdf = create_pdf
43
+ proc = Proc.new { Prawn::Icon::Parser.format(pdf, string) }
44
+
45
+ expect(proc).to raise_error(Prawn::Errors::UnknownFont)
46
+ end
47
+
48
+ it 'should raise an error when an icon is not found for valid set' do
49
+ string = '<icon>fa-__INVALID__</icon>'
50
+ pdf = create_pdf
51
+ proc = Proc.new { Prawn::Icon::Parser.format(pdf, string) }
52
+
53
+ expect(proc).to raise_error(Prawn::Errors::IconNotFound)
54
+ end
55
+ end
56
+
57
+ describe '::config_from_tokens' do
58
+ it 'should return an array containing only an empty hash' do
59
+ string = '<icon>fa-arrows</icon>'
60
+ tokens = tokenize_string(string)
61
+ config = Prawn::Icon::Parser.config_from_tokens(tokens)
62
+ inner = config.first
63
+
64
+ expect(config.size).to eq(1)
65
+ expect(inner).to be_empty
66
+ end
67
+
68
+ it 'should return an array containing a signle hash of attrs' do
69
+ string = '<icon size="12" color="CCCCCC">fa-arrows</icon>'
70
+ tokens = tokenize_string(string)
71
+ config = Prawn::Icon::Parser.config_from_tokens(tokens)
72
+ inner = config.first
73
+
74
+ expect(config.size).to eq(1)
75
+ expect(inner[:size]).to eq(12.0)
76
+ expect(inner[:color]).to eq('CCCCCC')
77
+ end
78
+
79
+ it 'should return an array containing as many hashes as icons' do
80
+ string = <<-EOS
81
+ <icon>fa-arrows</icon>
82
+ <icon>fa-arrows</icon>
83
+ <icon>fa-arrows</icon>
84
+ EOS
85
+ tokens = tokenize_string(string)
86
+ config = Prawn::Icon::Parser.config_from_tokens(tokens)
87
+
88
+ expect(config.size).to eq(3)
89
+ end
90
+ end
91
+
92
+ describe '::keys_to_unicode' do
93
+ it 'should return an empty array for empty input' do
94
+ string = ''
95
+ config = []
96
+ content = contentize_string(string)
97
+ pdf = create_pdf
98
+ icons = Prawn::Icon::Parser.keys_to_unicode(pdf, content, config)
99
+
100
+ expect(icons).to be_empty
101
+ end
102
+
103
+ it 'should return an array with unicode content' do
104
+ string = '<icon>fa-arrows</icon>'
105
+ tokens = tokenize_string(string)
106
+ content = contentize_string(string)
107
+ config = Prawn::Icon::Parser.config_from_tokens(tokens)
108
+ pdf = create_pdf
109
+ icons = Prawn::Icon::Parser.keys_to_unicode(pdf, content, config)
110
+ icon = icons.first[:content]
111
+
112
+ expect(valid_unicode?(icon)).to be_true
113
+ end
114
+
115
+ it 'should return a single array containing attr hash of defaults' do
116
+ # Hash must contain :set and :content by default
117
+ string = '<icon>fa-arrows</icon>'
118
+ tokens = tokenize_string(string)
119
+ content = contentize_string(string)
120
+ config = Prawn::Icon::Parser.config_from_tokens(tokens)
121
+ pdf = create_pdf
122
+ icons = Prawn::Icon::Parser.keys_to_unicode(pdf, content, config)
123
+ icon = icons.first
124
+
125
+ expect(icon[:set]).to eq(:fa)
126
+ expect(icon[:content]).not_to be_empty
127
+ end
128
+
129
+ it 'should handle strings with multiple icons/attrs combinations' do
130
+ string = <<-EOS
131
+ <icon size="20">fa-arrows</icon>
132
+ some filler text
133
+ <icon>fa-arrows</icon>
134
+ <icon size="8" color="0099FF">fa-arrows</icon>
135
+ EOS
136
+ tokens = tokenize_string(string)
137
+ content = contentize_string(string)
138
+ config = Prawn::Icon::Parser.config_from_tokens(tokens)
139
+ pdf = create_pdf
140
+ icons = Prawn::Icon::Parser.keys_to_unicode(pdf, content, config)
141
+ first = icons.first
142
+ second = icons[1]
143
+ third = icons[2]
144
+
145
+ expect(icons.size).to eq(3)
146
+ expect(first[:size]).to eq(20.0)
147
+ expect(second[:size]).to be_nil
148
+ expect(third[:size]).to eq(8.0)
149
+ expect(third[:color]).to eq('0099FF')
150
+ end
151
+ end
152
+
153
+ describe '::icon_tags' do
154
+ it 'should return valid input as prawn formatted text tags wrapping color tags' do
155
+ icons = [
156
+ { set: :fa, color: 'CCCCCC', content: "\uf001" }
157
+ ]
158
+ tags = Prawn::Icon::Parser.icon_tags(icons)
159
+ match = "<font name=\"fa\"><color rgb=\"CCCCCC\">\uf001</color></font>"
160
+
161
+ expect(tags.size).to eq(1)
162
+ expect(tags.first).to eq(match)
163
+ end
164
+
165
+ it 'should return valid input as prawn formatted text tags without color' do
166
+ icons = [
167
+ { set: :fa, content: "\uf001" }
168
+ ]
169
+ tags = Prawn::Icon::Parser.icon_tags(icons)
170
+ match = "<font name=\"fa\">\uf001</font>"
171
+
172
+ expect(tags.size).to eq(1)
173
+ expect(tags.first).to eq(match)
174
+ end
175
+
176
+ it 'should be capable of handling multiple icon fonts' do
177
+ icons = [
178
+ { set: :fa, content: "\uf001" },
179
+ { set: :fi, content: "\uf001" }
180
+ ]
181
+ tags = Prawn::Icon::Parser.icon_tags(icons)
182
+ match1 = "<font name=\"fa\">\uf001</font>"
183
+ match2 = "<font name=\"fi\">\uf001</font>"
184
+
185
+ expect(tags.size).to eq(2)
186
+ expect(tags[0]).to eq(match1)
187
+ expect(tags[1]).to eq(match2)
188
+ end
189
+ end
190
+ end