Skip to content

Commit

Permalink
add failing spec for text box
Browse files Browse the repository at this point in the history
  • Loading branch information
unterkoefler committed Feb 16, 2023
1 parent 7d4f6b8 commit aca0044
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/prawn/text/formatted/box.rb
Expand Up @@ -226,7 +226,7 @@ def render(flags = {})
if @rotate != 0 && @inked
render_rotated(text)
else
wrap(text)
wrap(text, epsilon: -0.000001)
end
@inked = false
end
Expand Down Expand Up @@ -550,7 +550,7 @@ def shrink_to_fit(text)
loop do
if @disable_wrap_by_char && @font_size > @min_font_size
begin
wrap(text)
wrap(text, epsilon: 0.000001)
rescue Errors::CannotFit
# Ignore errors while we can still attempt smaller
# font sizes.
Expand Down
10 changes: 5 additions & 5 deletions lib/prawn/text/formatted/line_wrap.rb
Expand Up @@ -46,7 +46,7 @@ def wrap_line(options)
fragment.lstrip! if first_fragment_on_this_line?(fragment)
next if empty_line?(fragment)

unless apply_font_settings_and_add_fragment_to_line(fragment)
unless apply_font_settings_and_add_fragment_to_line(fragment, epsilon: options[:epsilon])
break
end
end
Expand Down Expand Up @@ -74,18 +74,18 @@ def next_string_newline?
@arranger.preview_next_string == "\n"
end

def apply_font_settings_and_add_fragment_to_line(fragment)
def apply_font_settings_and_add_fragment_to_line(fragment, epsilon: 0.0)
result = nil
@arranger.apply_font_settings do
result = add_fragment_to_line(fragment)
result = add_fragment_to_line(fragment, epsilon: epsilon)
end
result
end

# returns true if all text was printed without running into the end of
# the line
#
def add_fragment_to_line(fragment)
def add_fragment_to_line(fragment, epsilon: 0.0)
case fragment
when ''
true
Expand All @@ -101,7 +101,7 @@ def add_fragment_to_line(fragment)
@document.width_of(segment, kerning: @kerning)
end

if @accumulated_width + segment_width <= @width
if @accumulated_width + segment_width <= @width + epsilon
@accumulated_width += segment_width
shy = soft_hyphen(segment.encoding)
if segment[-1] == shy
Expand Down
5 changes: 3 additions & 2 deletions lib/prawn/text/formatted/wrap.rb
Expand Up @@ -44,7 +44,7 @@ def initialize(_array, options)
#
# Returns any formatted text that was not printed
#
def wrap(array) # :nodoc:
def wrap(array, epsilon: 0.0) # :nodoc:
initialize_wrap(array)

stop = false
Expand All @@ -57,7 +57,8 @@ def wrap(array) # :nodoc:
kerning: @kerning,
width: available_width,
arranger: @arranger,
disable_wrap_by_char: @disable_wrap_by_char
disable_wrap_by_char: @disable_wrap_by_char,
epsilon: epsilon
)

if enough_height_for_this_line?
Expand Down
19 changes: 19 additions & 0 deletions spec/prawn/text/box_spec.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'spec_helper'
require 'byebug'

describe Prawn::Text::Box do
let(:pdf) { create_pdf }
Expand Down Expand Up @@ -830,6 +831,24 @@
)
end

it 'displays the entire text when the box is just big enough' do
text = 'Two words'
size = 12.0
text_box = described_class.new(
text,
options.merge(
size: size,
width: 56.328, # pdf.width_of(text, size: size),
height: 12.0, # pdf.height_of(text, size: size),
overflow: :shrink_to_fit,
disable_wrap_by_char: true
)
)

overflow = text_box.render
expect(overflow).to eq('')
end

it 'displays the entire text' do
text_box.render
expect(text_box.text.tr("\n", ' ')).to eq(text.strip)
Expand Down

0 comments on commit aca0044

Please sign in to comment.