Skip to content

Commit 84ea3cf

Browse files
authored
Merge pull request #35 from hidakatsuya/add-prawn-2.3.0-support
Add prawn 2.3.0 support
2 parents 1474e7d + caed7f6 commit 84ea3cf

9 files changed

Lines changed: 120 additions & 25 deletions

File tree

.github/workflows/test.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,36 @@ on: [push, pull_request]
44

55
jobs:
66
test:
7-
name: Test on ruby ${{ matrix.ruby_version }}
7+
name: Test on ruby ${{ matrix.ruby_version }} and prawn ${{ matrix.prawn_version }}
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
1111
ruby_version:
1212
- 2.5.x
1313
- 2.6.x
1414
- 2.7.x
15+
prawn_version:
16+
- 2.2
17+
- 2.3
18+
1519
steps:
1620
- uses: actions/checkout@v1
21+
1722
- name: Set up diff-pdf
1823
run: |
1924
sudo apt-get update
2025
sudo apt-get install make automake g++ libpoppler-glib-dev poppler-utils libwxgtk3.0-dev
2126
git clone https://github.com/vslavik/diff-pdf.git -b v0.4.1 --depth 1 /tmp/diff-pdf-src
2227
cd /tmp/diff-pdf-src
2328
./bootstrap && ./configure && make && sudo make install
29+
2430
- name: Set up Ruby ${{ matrix.ruby_version }}
2531
uses: actions/setup-ruby@v1
2632
with:
2733
ruby-version: ${{ matrix.ruby_version }}
34+
2835
- name: Build and test with Rake
2936
run: |
3037
gem install bundler
31-
bundle install --jobs 4 --retry 3
38+
bundle install --gemfile test/gemfiles/prawn-${{ matrix.prawn_version }} --jobs 4 --retry 3
3239
bundle exec rake test

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Prawn::Emoji.config.regex = ::Unicode::Emoji::REGEX_INCLUDE_TEXT
9090

9191
### Prawn
9292

93-
2.2+
93+
2.2, 2.3
9494

9595
## Contributing
9696

lib/prawn/emoji/drawer.rb

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,23 @@ def draw_text!(text, at:, text_options:)
4444

4545
def draw_text(text, at:, text_options:)
4646
draw_text!(text, at: at, text_options: text_options)
47-
document.width_of(text, text_options)
47+
48+
width = document.width_of(text, text_options)
49+
50+
if Prawn::VERSION >= '2.3.0'
51+
# In prawn v2.3.0, the character spacing at the end of the text is not included in the calculated text width.
52+
# https://github.com/prawnpdf/prawn/pull/1117
53+
width > 0 ? width + document.character_spacing : width
54+
else
55+
width
56+
end
4857
end
4958

5059
def draw_emoji(emoji_char, at:)
5160
emoji_image = Emoji::Image.new(emoji_char)
61+
emoji_image.render(document, at: at)
5262

53-
x, y = at
54-
55-
# Prawn 2.2 does not close the image file when Pathname is passed to Document#image method.
56-
#
57-
# FIXME: This issue has been fixed by https://github.com/prawnpdf/prawn/pull/1090 but not released.
58-
# Fix as follows after the PR released.
59-
#
60-
# @document.image(image_file.path, at: [x, y], width: image.width)
61-
#
62-
File.open(emoji_image.path, 'rb') do |image_file|
63-
@document.image(image_file, at: [x, y + emoji_char.height], width: emoji_char.width)
64-
end
65-
66-
emoji_char.width + document.character_spacing
63+
emoji_image.width + document.character_spacing
6764
end
6865
end
6966
end

lib/prawn/emoji/image.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
# frozen_string_literal: true
22

3+
require 'forwardable'
4+
35
module Prawn
46
module Emoji
57
class Image
8+
extend Forwardable
9+
610
STORE = Emoji.root.join 'emoji', 'images'
711

12+
def_delegators :emoji_char, :width, :height
13+
814
def initialize(emoji_char)
915
@emoji_char = emoji_char
1016
end
1117

18+
def render(document, at:)
19+
x, y = at
20+
21+
position = [x, y + height]
22+
23+
if Prawn::VERSION >= '2.3.0'
24+
document.image(path, at: position, width: width)
25+
else
26+
# Prawn 2.2 does not close the image file when Pathname is passed to Document#image method.
27+
# https://github.com/prawnpdf/prawn/pull/1090
28+
File.open(path, 'rb') do |image_file|
29+
document.image(image_file, at: position, width: width)
30+
end
31+
end
32+
end
33+
1234
def path
1335
STORE.join("#{emoji_char.codepoint}.png").to_s
1436
end

prawn-emoji.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ Gem::Specification.new do |spec|
1818
end
1919
spec.require_path = 'lib'
2020

21-
spec.add_runtime_dependency 'prawn', '~> 2.2.0'
21+
spec.add_runtime_dependency 'prawn', '~> 2.2'
2222
spec.add_runtime_dependency 'unicode-emoji', '~> 2.5.0'
2323
end

test/gemfiles/prawn-2.2

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'prawn', '~> 2.2.0'
4+
gem 'unicode-emoji', '~> 2.5.0'
5+
6+
gem 'rake', '>= 0'
7+
gem 'minitest', '>= 5.12.2'
8+
gem 'rr', '>= 0'
9+
gem 'rubyzip', '>= 1.0.0'
10+

test/gemfiles/prawn-2.3

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'prawn', '~> 2.3.0'
4+
gem 'unicode-emoji', '~> 2.5.0'
5+
6+
gem 'rake', '>= 0'
7+
gem 'minitest', '>= 5.12.2'
8+
gem 'rr', '>= 0'
9+
gem 'rubyzip', '>= 1.0.0'
10+

test/units/prawn/emoji/drawer_test.rb

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
it do
2626
mock(document).draw_text!(/aaa|bbb|ccc|/, hash_including(emoji: false)).times(4)
27-
mock(document).image(is_a(File), is_a(Hash)).times(3)
27+
any_instance_of(Prawn::Emoji::Image) { |image| mock(image).render.with_any_args.times(3) }
28+
2829
subject
2930
end
3031
end
@@ -35,7 +36,8 @@
3536

3637
it do
3738
mock(document).draw_text!(text, text_options.merge(emoji: false)).once
38-
mock(document).image.never
39+
any_instance_of(Prawn::Emoji::Image) { |image| mock(image).render.with_any_args.never }
40+
3941
subject
4042
end
4143
end
@@ -84,11 +86,31 @@ def disable_gc
8486
describe '#draw_text' do
8587
subject { drawer.send(:draw_text, text, at: [100, 200], text_options: {}) }
8688

87-
let(:text) { 'text' }
89+
let(:character_spacing) { 5 }
90+
91+
before { stub(document).character_spacing { character_spacing } }
92+
93+
describe 'text is empty' do
94+
let(:text) { '' }
95+
96+
it 'returns 0 when the text is empty' do
97+
_(subject).must_equal 0
98+
end
99+
end
100+
101+
describe 'text is not empty' do
102+
let(:text) { 'text' }
103+
104+
it 'returns the text width including the character spacing at the end of the text' do
105+
expect_width =
106+
if Prawn::VERSION >= '2.3.0'
107+
document.width_of(text) + character_spacing
108+
else
109+
document.width_of(text)
110+
end
88111

89-
it 'returns the text width' do
90-
stub(document).character_spacing { 5 }
91-
_(subject).must_equal document.width_of(text)
112+
_(subject).must_equal expect_width
113+
end
92114
end
93115
end
94116
end

test/units/prawn/emoji/image_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,31 @@
1414
end
1515
end
1616
end
17+
18+
describe '#width and #height' do
19+
let(:emoji_image) { Prawn::Emoji::Image.new(emoji_char) }
20+
let(:emoji_char) { Prawn::Emoji::Char.new('🍣', 12) }
21+
22+
before do
23+
stub(emoji_char).width { 14 }
24+
stub(emoji_char).height { 18 }
25+
end
26+
27+
it do
28+
_(emoji_image.width).must_equal 14
29+
_(emoji_image.height).must_equal 18
30+
end
31+
end
32+
33+
describe '#render' do
34+
let(:emoji_image) { Prawn::Emoji::Image.new(Prawn::Emoji::Char.new('🍣', 12)) }
35+
36+
it do
37+
document = Prawn::Document.new
38+
39+
mock(document).image.with_any_args.times(1)
40+
41+
emoji_image.render(document, at: [100, 200])
42+
end
43+
end
1744
end

0 commit comments

Comments
 (0)