Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in a used_width method to the Formatted::Box class #1259

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/prawn/text/formatted/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ def render(flags = {})
end
end

# Calculate the used width for the text in the box
#
def used_width
# dry run the render loop to calculate the width
render(dry_run: true)

@max_line_width
end

# The width available at this point in the box
#
def available_width
Expand Down
6 changes: 5 additions & 1 deletion lib/prawn/text/formatted/wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ def initialize(_array, options)
# set to true until something is printed, then false
# <tt>@everything_printed</tt>::
# set to false until everything printed, then true
# <tt>@max_line_width</tt>::
# set to the maximum line width of all lines processed
#
# Returns any formatted text that was not printed
#
def wrap(array) # :nodoc:
initialize_wrap(array)

@max_line_width = 0
stop = false
until stop
# wrap before testing if enough height for this line because the
Expand All @@ -60,6 +62,8 @@ def wrap(array) # :nodoc:
disable_wrap_by_char: @disable_wrap_by_char
)

@max_line_width = [@max_line_width, @arranger.line_width].max

if enough_height_for_this_line?
move_baseline_down
print_line
Expand Down
63 changes: 63 additions & 0 deletions spec/prawn/text/formatted/box_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,69 @@
end
end

describe 'Text::Formatted::Box#used_width' do
subject(:used_width) { text_box.used_width }

let(:text_box) { described_class.new(array, box_options) }
let(:box_options) { base_box_options }
let(:base_box_options) { { document: pdf } }
let(:array) { [{ text: 'hello world' }] }

context 'with basic text' do
it 'returns the max width of the text in the box' do
expect(used_width).to be_within(0.1).of(57.408)
end
end

context 'with formatted text' do
let(:array) { [{ text: 'hello world', size: 20 }] }

it 'returns the max width of the text in the box' do
expect(used_width).to be_within(0.1).of(95.68)
end
end

context 'with text containing a line break' do
let(:array) { [{ text: "hello\nworld" }] }

it 'returns the max width of the text in the box' do
expect(used_width).to be_within(0.1).of(28.728)
end

context 'with first line being longer' do
let(:array) { [{ text: "world\nhello" }] }

it 'returns the max width of the text in the box' do
expect(used_width).to be_within(0.1).of(28.728)
end
end
end

context 'when the text would wrap in the box' do
let(:array) { [{text: 'hello world ' * 11}]}

it 'returns the width of the text in the box after wrapping' do
expect(used_width).to be_within(0.1).of(604.104)
end
end

context 'with fallback fonts and UTF-8 text' do
before do
file = "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf"
pdf.font_families['DejaVu Sans'] = {
normal: { file: file }
}
end

let(:box_options) { base_box_options.merge(fallback_fonts: ['DejaVu Sans']) }
let(:array) { [{text: 'Hello ≦≧ world'}]}

it 'returns the max width of the text in the box' do
expect(used_width).to be_within(0.1).of(82.824)
end
end
end

describe 'Text::Formatted::Box#extensions' do
let(:formatted_wrap_override) do
Module.new do
Expand Down