Skip to content

Commit

Permalink
Add overflow_wrap to wrap lines based on its value
Browse files Browse the repository at this point in the history
  • Loading branch information
hidakatsuya committed Nov 25, 2023
1 parent 7bf9528 commit a129d0f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,32 @@ def build_text_attributes(style, &block)
letter_spacing: letter_spacing(style['letter-spacing']),
line_height: line_height(style['line-height']),
overflow: text_overflow(style['overflow']),
word_wrap: word_wrap(style['word-wrap'])
# Deprecated: Use overflow_wrap instead of word_wrap
word_wrap: word_wrap(style['word-wrap']),
overflow_wrap: overflow_wrap(style['overflow-wrap'], style['word-wrap'])
}
block.call(text_attributes) if block_given?
text_attributes
end

def overflow_wrap(wrap, word_wrap)
case wrap || migrate_overflow_wrap_from_word_wrap(word_wrap)
when 'normal', nil then :normal
when 'anywhere' then :anywhere
# Deprecated: This is a temporary value for migrating from word_wrap.
when 'disable-break-word-by-space' then :disable_break_word_by_space
else :normal
end
end

def migrate_overflow_wrap_from_word_wrap(word_wrap)
case word_wrap
when 'none', nil then 'disable-break-word-by-space'
when 'break-word' then 'normal'
else 'normal'
end
end

# @param [Array<String>] font_names
# @return [String]
def font_family(font_names)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Graphics
# @option attrs [Boolean] :single (false)
# @option attrs [:trancate, :shrink_to_fit, :expand] :overflow (:trancate)
# @option attrs [:none, :break_word] :word_wrap (:none)
# @option attrs [:normal, :anywhere, :disable_word_wrap_by_space] :overflow_wrap (:normal)
def text_box(content, x, y, w, h, attrs = {}, &block)
w, h = s2f(w, h)

Expand All @@ -32,8 +33,7 @@ def text_box(content, x, y, w, h, attrs = {}, &block)
overflow: attrs[:overflow]
)

# Do not break by word unless :word_wrap is :break_word
content = text_without_line_wrap(content) if attrs[:word_wrap] == :none
content = disable_word_wrap_by_space(content) if attrs[:overflow_wrap] == :disable_word_wrap_by_space

with_text_styles(attrs) do |built_attrs, font_styles|
if block
Expand Down Expand Up @@ -112,6 +112,9 @@ def with_text_styles(attrs, &block)
spacing = attrs.delete(:letter_spacing)
attrs[:character_spacing] = s2f(spacing) if spacing

# Disable line breaking on chars such as spaces and hyphens
attrs[:disable_word_break] = true if attrs[:overflow_wrap] == :anywhere

# Or... with_font_styles(attrs, fontinfo, &block)
with_font_styles(attrs, fontinfo) do |modified_attrs, styles|
block.call(modified_attrs, styles)
Expand All @@ -131,7 +134,7 @@ def text_line_leading(line_height, font)

# @param [String] content
# @return [String]
def text_without_line_wrap(content)
def disable_word_wrap_by_space(content)
content.gsub(/ /, Prawn::Text::NBSP)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,28 @@ def test_image_position_y
assert_equal :top, @pdf.image_position_y('')
assert_equal :top, @pdf.image_position_y(nil)
end

def test_text_overflow
assert_equal :anywhere, @pdf.overflow_wrap('anywhere', nil)
assert_equal :disable_word_wrap_by_space, @pdf.overflow_wrap('disable_word_wrap_by_space', 'break-word')

assert_equal :normal, @pdf.overflow_wrap('normal', nil)

# When value is unexpected value
assert_equal :normal, @pdf.overflow_wrap('', nil)

# When value is nil
# word_wrap is none
assert_equal :disable_break_word_by_space, @pdf.overflow_wrap(nil, 'none')
# word_wrap is break_word
assert_equal :normal, @pdf.overflow_wrap(nil, 'break-word')
# word_wrap is nil
assert_equal :normal, @pdf.overflow_wrap(nil, nil)
end

def test_migrate_overflow_wrap_from_word_wrap
assert_equal 'normal', @pdf.migrate_overflow_wrap_from_word_wrap('break-word')
assert_equal 'disable-break-word-by-space', @pdf.migrate_overflow_wrap_from_word_wrap('none')
assert_equal 'disable-break-word-by-space', @pdf.migrate_overflow_wrap_from_word_wrap(nil)
end
end

0 comments on commit a129d0f

Please sign in to comment.