prawn-emoji 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 26bd2353da780fc31e8ebd11751a1156916d7cea
4
- data.tar.gz: 359e3538740e6c1e14a90ad6a399bb5be5c94624
3
+ metadata.gz: 30464f704625995fdd24c34529781eb09f5cc988
4
+ data.tar.gz: 730b6f231a9db3d31f560372bbffcc1e45da50cf
5
5
  SHA512:
6
- metadata.gz: e408960124f71a8c5151ac0ac12a767a47a992e9b819d35a932d7201f21887556f5a17ede3551dc5823900cdff03faf67601b884987f5d1242ef6d32299be72d
7
- data.tar.gz: f21a974d7dcdf8e954b64639f9c33b7320d829e1467654e3f7ac0b0190d9b79b32587c57a336a8cd37a1a984e2dbaa12c6f8da868f6d41e7ed01eaae22a7b78d
6
+ metadata.gz: 00c4a04c30c7c42b529a16a0368e2f9d689dcfece6adb14c2b8d2eb339de921175e8ed469f0415fe0a21f77624ff35de9401293c9e64959002d1726c7263e02f
7
+ data.tar.gz: 871b4cdf8e5af80ac414160bd2603884900438746f08b9a43da551e90c04d25b465c54f1a5dba4f6f939219e0e96b14680e181e6354959decfce767cf30d6c21
@@ -1,3 +1,11 @@
1
+ ## 2.1.0
2
+
3
+ ### Breaking Changes
4
+
5
+ * Support variation selector
6
+ * An emoji with text presentation selector such as ☀︎ (U+2600 U+FE0E) will be drawn as text
7
+ * See also http://www.unicode.org/reports/tr51/tr51-12.html#def_text_presentation_selector
8
+
1
9
  ## 2.0.1
2
10
 
3
11
  ### Bug Fixes:
@@ -3,6 +3,7 @@
3
3
  require_relative 'substitution'
4
4
  require_relative 'index'
5
5
  require_relative 'image'
6
+ require_relative 'unicode'
6
7
 
7
8
  module Prawn
8
9
  module Emoji
@@ -22,25 +23,54 @@ module Prawn
22
23
  private
23
24
 
24
25
  def draw_emoji(text, text_options)
25
- left_text, emoji_unicode, remaining_text = text.partition(@emoji_index.to_regexp)
26
+ return text unless @emoji_index.to_regexp =~ text
27
+
28
+ result = []
29
+ target = text
30
+
31
+ while target do
32
+ left_text, emoji_unicode, remaining_text = partition_emoji(target)
26
33
 
27
- return text if emoji_unicode.empty?
34
+ if emoji_unicode.nil?
35
+ result << target
36
+ break
37
+ end
28
38
 
29
- emoji_image = Emoji::Image.new(emoji_unicode)
39
+ result << left_text
40
+ result << if emoji_unicode.text?
41
+ emoji_unicode.to_s
42
+ else
43
+ emoji_image = Emoji::Image.new(emoji_unicode)
30
44
 
31
- emoji_x, emoji_y = text_options[:at]
32
- emoji_x += @document.width_of(left_text, text_options)
33
- emoji_y += @document.font_size
45
+ emoji_x, emoji_y = text_options[:at]
46
+ emoji_x += @document.width_of(left_text, text_options)
47
+ emoji_y += @document.font_size
34
48
 
35
- draw_emoji_image emoji_image, at: [emoji_x, emoji_y], width: @document.font_size
49
+ draw_emoji_image emoji_image, at: [emoji_x, emoji_y], width: @document.font_size
36
50
 
37
- substitution_of_emoji = Substitution.new(@document)
38
- draw_emoji "#{left_text}#{substitution_of_emoji}#{remaining_text}", text_options
51
+ Emoji::Substitution.new(@document).to_s
52
+ end
53
+ target = remaining_text
54
+ end
55
+
56
+ result.join
39
57
  end
40
58
 
41
59
  def draw_emoji_image(emoji_image, at:, width:)
42
60
  @document.image emoji_image.path, at: at, width: width
43
61
  end
62
+
63
+ def partition_emoji(text)
64
+ left_text, emoji_unicode, remaining_text = text.partition(@emoji_index.to_regexp)
65
+
66
+ return nil if emoji_unicode.empty?
67
+
68
+ [
69
+ left_text,
70
+ Emoji::Unicode.new(emoji_unicode),
71
+ remaining_text
72
+ ]
73
+ end
44
74
  end
45
75
  end
46
76
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'yaml'
4
+ require_relative 'unicode'
4
5
 
5
6
  module Prawn
6
7
  module Emoji
@@ -14,18 +15,12 @@ module Prawn
14
15
  end
15
16
 
16
17
  def path
17
- STORE.join("#{codepoint}.png").to_s
18
+ STORE.join("#{unicode.codepoint}.png").to_s
18
19
  end
19
20
 
20
21
  def ==(other)
21
22
  unicode == other.unicode
22
23
  end
23
-
24
- private
25
-
26
- def codepoint
27
- @codepoint ||= @unicode.codepoints.map { |c| '%04x' % c.to_s }.join('-').downcase
28
- end
29
24
  end
30
25
  end
31
26
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'unicode'
4
+
3
5
  module Prawn
4
6
  module Emoji
5
7
  class Index
@@ -18,7 +20,10 @@ module Prawn
18
20
  private
19
21
 
20
22
  def build_regexp
21
- Regexp.new(codepoints.map { |codepoint| unicode(codepoint) }.join('|'))
23
+ emojis = codepoints.map { |codepoint| unicode(codepoint) }
24
+ variation_selectors = Emoji::Unicode::VARIATION_SELECTORS.values
25
+
26
+ Regexp.new("(#{emojis.join('|')})[#{variation_selectors.join}]?")
22
27
  end
23
28
 
24
29
  def load_emoji_codepoints
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Prawn
4
+ module Emoji
5
+ class Unicode
6
+ VARIATION_SELECTORS = {
7
+ text: '\ufe0e',
8
+ emoji: '\ufe0f'
9
+ }.freeze
10
+
11
+ def initialize(char)
12
+ @present_as_text = !!char.match(VARIATION_SELECTORS[:text])
13
+ @unicode = delete_variation_selector(char)
14
+ end
15
+
16
+ def ==(other)
17
+ to_s == other.to_s
18
+ end
19
+
20
+ def codepoint
21
+ @codepoint ||= @unicode.codepoints.map { |c| '%04x' % c.to_s }.join('-').downcase
22
+ end
23
+
24
+ def text?
25
+ @present_as_text
26
+ end
27
+
28
+ def to_s
29
+ @unicode
30
+ end
31
+
32
+ private
33
+
34
+ def delete_variation_selector(char)
35
+ char.gsub(/[#{VARIATION_SELECTORS.values.join}]/, '')
36
+ end
37
+ end
38
+ end
39
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Prawn
4
4
  module Emoji
5
- VERSION = '2.0.1'.freeze
5
+ VERSION = '2.1.0'.freeze
6
6
  end
7
7
  end
@@ -7,7 +7,8 @@ describe Prawn::Emoji::Drawer do
7
7
  let(:drawer) { Prawn::Emoji::Drawer.new document: document }
8
8
 
9
9
  let(:sushi) { '🍣' }
10
- let(:sushi_image) { Prawn::Emoji::Image.new(sushi) }
10
+ let(:sushi_unicode) { Prawn::Emoji::Unicode.new(sushi) }
11
+ let(:sushi_image) { Prawn::Emoji::Image.new(sushi_unicode) }
11
12
 
12
13
  before do
13
14
  document.font Prawn::Emoji.root.join 'test', 'fonts', 'DejaVuSans.ttf'
@@ -48,7 +49,7 @@ describe Prawn::Emoji::Drawer do
48
49
  end
49
50
 
50
51
  describe 'when includes emoji' do
51
- let(:text) { "aaa#{sushi}bbb"}
52
+ let(:text) { "aaa#{sushi}bbb" }
52
53
 
53
54
  it 'draws image for included emoji' do
54
55
  image_width = 12
@@ -3,13 +3,20 @@
3
3
  require 'test_helper'
4
4
 
5
5
  describe Prawn::Emoji::Image do
6
- let(:emojis) { %w( 😀 © 🇯🇵 ) }
6
+ describe '#path' do
7
+ let(:emojis) { %w( 😀 © 🇯🇵 ) }
8
+ let(:emoji_unicodes) { emojis.map { |emoji| Prawn::Emoji::Unicode.new(emoji) } }
7
9
 
8
- it 'possible to find the image file' do
9
- emojis.each do |emoji|
10
- emoji_image = Prawn::Emoji::Image.new(emoji)
11
-
12
- assert File.exist?(emoji_image.path), "#{emoji} not found"
10
+ it 'possible to find the image file' do
11
+ emoji_unicodes.each do |emoji_unicode|
12
+ emoji_image = Prawn::Emoji::Image.new(emoji_unicode)
13
+ assert File.exist?(emoji_image.path), "#{emoji_unicode} not found"
14
+ end
13
15
  end
14
16
  end
17
+
18
+ describe '#==' do
19
+ it { Prawn::Emoji::Image.new('🍣').must_be :==, Prawn::Emoji::Image.new('🍣') }
20
+ it { Prawn::Emoji::Image.new('🍣').wont_be :==, Prawn::Emoji::Image.new('🐟') }
21
+ end
15
22
  end
@@ -20,6 +20,6 @@ describe Prawn::Emoji::Index do
20
20
  subject { index.to_regexp }
21
21
  before { stub(index).codepoints { %w( 00a9 00ae 002a-20e3 ) } }
22
22
 
23
- it { subject.must_equal(/\u{00a9}|\u{00ae}|\u{002a 20e3}/) }
23
+ it { subject.must_equal(/(\u{00a9}|\u{00ae}|\u{002a 20e3})[\ufe0e\ufe0f]?/) }
24
24
  end
25
25
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ describe Prawn::Emoji::Unicode do
6
+ let(:klass) { Prawn::Emoji::Unicode }
7
+
8
+ describe '#==' do
9
+ it { klass.new('🐟').must_be :==, klass.new('🐟') }
10
+ it { klass.new('🐟').wont_be :==, klass.new('🍣') }
11
+ end
12
+
13
+ describe '#codepoint' do
14
+ it { klass.new('🍣').codepoint.must_equal '1f363' }
15
+ it { klass.new('🇯🇵').codepoint.must_equal '1f1ef-1f1f5' }
16
+ end
17
+
18
+ describe '#to_s' do
19
+ it { klass.new('🍣').to_s.must_equal '🍣' }
20
+ it { klass.new('❤️').to_s.wont_match /[\ufe0e\ufe0f]/ }
21
+ end
22
+
23
+ describe '#text?' do
24
+ describe 'no selector' do
25
+ it { klass.new('☀').wont_be :text? }
26
+ end
27
+
28
+ describe 'with text presentation selector' do
29
+ it { klass.new('☀︎').must_be :text? }
30
+ end
31
+
32
+ describe 'with emoji presentation selector' do
33
+ it { klass.new('☀️').wont_be :text? }
34
+ end
35
+ end
36
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn-emoji
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katsuya HIDAKA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-21 00:00:00.000000000 Z
11
+ date: 2017-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prawn
@@ -1947,6 +1947,7 @@ files:
1947
1947
  - lib/prawn/emoji/image.rb
1948
1948
  - lib/prawn/emoji/index.rb
1949
1949
  - lib/prawn/emoji/substitution.rb
1950
+ - lib/prawn/emoji/unicode.rb
1950
1951
  - lib/prawn/emoji/version.rb
1951
1952
  - prawn-emoji.gemspec
1952
1953
  - test/fonts/DejaVuSans.ttf
@@ -1959,6 +1960,7 @@ files:
1959
1960
  - test/prawn/emoji/image_test.rb
1960
1961
  - test/prawn/emoji/index_test.rb
1961
1962
  - test/prawn/emoji/substitution_test.rb
1963
+ - test/prawn/emoji/unicode_test.rb
1962
1964
  - test/prawn/emoji_test.rb
1963
1965
  - test/test_helper.rb
1964
1966
  homepage: https://github.com/hidakatsuya/prawn-emoji