Skip to content

Commit

Permalink
Merge pull request #54 from petergoldstein/feature/add_rise_and_leading
Browse files Browse the repository at this point in the history
Add rise to the set of text state operator helpers
  • Loading branch information
pointlessone authored Dec 8, 2023
2 parents 7fd7c0b + af6a22a commit 7455037
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 10 deletions.
48 changes: 38 additions & 10 deletions lib/pdf/core/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,22 @@ def horizontal_text_scaling(amount = nil, &block)
end
end

# Move the baseline up or down from its default location.
# Positive values move the baseline up, negative values
# move it down, and a zero value resets the baseline to
# its default location.
def rise(amount = nil, &block)
if amount.nil?
return defined?(@rise) && @rise || 0
end

if rise == amount
yield
else
wrap_and_restore_rise(amount, &block)
end
end

def add_text_content(text, x, y, options)
chunks = font.encode_text(text, options)

Expand Down Expand Up @@ -316,6 +332,10 @@ def wrap_and_restore_character_spacing(block_value)
end
end

def update_character_spacing_state
add_content "\n#{PDF::Core.real(character_spacing)} Tc"
end

def wrap_and_restore_word_spacing(block_value)
original_value = word_spacing
@word_spacing = block_value
Expand All @@ -328,6 +348,10 @@ def wrap_and_restore_word_spacing(block_value)
end
end

def update_word_spacing_state
add_content "\n#{PDF::Core.real(word_spacing)} Tw"
end

def wrap_and_restore_horizontal_text_scaling(block_value)
original_value = horizontal_text_scaling
@horizontal_text_scaling = block_value
Expand All @@ -340,20 +364,24 @@ def wrap_and_restore_horizontal_text_scaling(block_value)
end
end

def update_character_spacing_state
update_real_text_state(character_spacing, 'Tc')
end

def update_word_spacing_state
update_real_text_state(word_spacing, 'Tw')
def update_horizontal_text_scaling_state
add_content "\n#{PDF::Core.real(horizontal_text_scaling)} Tz"
end

def update_horizontal_text_scaling_state
update_real_text_state(horizontal_text_scaling, 'Tz')
def wrap_and_restore_rise(block_value)
original_value = rise
@rise = block_value
update_rise_state
begin
yield
ensure
@rise = original_value
update_rise_state
end
end

def update_real_text_state(amount, operator)
add_content "\n#{PDF::Core.real(amount)} #{operator}"
def update_rise_state
add_content "\n#{PDF::Core.real(rise)} Ts"
end
end
end
Expand Down
50 changes: 50 additions & 0 deletions spec/pdf/core/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,56 @@ def font_size
end
end

describe '#rise' do
describe 'called without argument' do
let(:result) { mock.rise }

it 'functions as accessor' do
expect(result).to eq(0)
end
end

describe 'called with argument' do
context 'when the block does not raise an error' do
before do
mock.rise(2.3) do
mock.add_content('TEST')
end
end

it 'resets rise to original value' do
expect(mock.rise).to eq(0)
end

it 'outputs correct PDF content' do
expect(mock.text).to eq("\n2.3 TsTEST\n0.0 Ts")
end
end

context 'when the block raises an error' do
let(:error_message) { SecureRandom.hex(5) }

# rubocop:disable RSpec/ExpectInHook
before do
expect do
mock.rise(5) do
raise StandardError, error_message
end
end.to raise_error StandardError, error_message
end
# rubocop:enable RSpec/ExpectInHook

it 'resets rise to original value' do
expect(mock.rise).to eq(0)
end

it 'outputs correct PDF content' do
expect(mock.text).to eq("\n5.0 Ts\n0.0 Ts")
end
end
end
end

describe '#add_text_content' do
it 'handles frozen strings' do
expect { mock.add_text_content 'text', 0, 0, {} }
Expand Down

0 comments on commit 7455037

Please sign in to comment.