Skip to content

Commit 267739a

Browse files
committed
Add overflow_wrap to wrap lines based on its value
1 parent 9855232 commit 267739a

4 files changed

Lines changed: 81 additions & 10 deletions

File tree

lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def build_graphic_attributes(style, &block)
2525
# @yieldparam [Hash] attrs
2626
# @return [Hash]
2727
def build_text_attributes(style, &block)
28+
word_wrap = word_wrap(style['word-wrap'])
29+
2830
text_attributes = {
2931
font: font_family(style['font-family']),
3032
size: style['font-size'],
@@ -35,12 +37,32 @@ def build_text_attributes(style, &block)
3537
letter_spacing: letter_spacing(style['letter-spacing']),
3638
line_height: line_height(style['line-height']),
3739
overflow: text_overflow(style['overflow']),
38-
word_wrap: word_wrap(style['word-wrap'])
40+
word_wrap: word_wrap,
41+
# Deprecated: Use overflow_wrap instead of word_wrap
42+
overflow_wrap: overflow_wrap(style['overflow-wrap'], word_wrap)
3943
}
4044
block.call(text_attributes) if block_given?
4145
text_attributes
4246
end
4347

48+
def overflow_wrap(style, computed_word_wrap)
49+
case style || migrate_overflow_wrap_from_word_wrap(computed_word_wrap)
50+
when 'normal', nil then :normal
51+
when 'anywhere' then :anywhere
52+
# Deprecated: This is a temporary value for migrating from word_wrap.
53+
when 'disable-break-word-by-space' then :disable_break_word_by_space
54+
else :normal
55+
end
56+
end
57+
58+
def migrate_overflow_wrap_from_word_wrap(computed_word_wrap)
59+
case computed_word_wrap
60+
when :none then 'disable-break-word-by-space'
61+
when :break_word then 'normal'
62+
else raise ArgumentError, 'Invalid computed word_wrap value'
63+
end
64+
end
65+
4466
# @param [Array<String>] font_names
4567
# @return [String]
4668
def font_family(font_names)

lib/thinreports/basic_report/generator/pdf/document/graphics/text.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Graphics
2323
# @option attrs [Boolean] :single (false)
2424
# @option attrs [:trancate, :shrink_to_fit, :expand] :overflow (:trancate)
2525
# @option attrs [:none, :break_word] :word_wrap (:none)
26+
# @option attrs [:normal, :anywhere, :disable_break_word_by_space] :overflow_wrap (:normal)
2627
def text_box(content, x, y, w, h, attrs = {}, &block)
2728
w, h = s2f(w, h)
2829

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

35-
# Do not break by word unless :word_wrap is :break_word
36-
content = text_without_line_wrap(content) if attrs[:word_wrap] == :none
36+
content = replace_space_to_nbsp(content) if attrs[:overflow_wrap] == :disable_break_word_by_space
3737

3838
with_text_styles(attrs) do |built_attrs, font_styles|
3939
if block
@@ -112,6 +112,12 @@ def with_text_styles(attrs, &block)
112112
spacing = attrs.delete(:letter_spacing)
113113
attrs[:character_spacing] = s2f(spacing) if spacing
114114

115+
# Disable line breaking on chars such as spaces and hyphens
116+
attrs[:disable_word_break] = true if attrs.delete(:overflow_wrap) == :anywhere
117+
118+
# Delete unnecessary attributes
119+
attrs.delete(:word_wrap)
120+
115121
# Or... with_font_styles(attrs, fontinfo, &block)
116122
with_font_styles(attrs, fontinfo) do |modified_attrs, styles|
117123
block.call(modified_attrs, styles)
@@ -131,7 +137,7 @@ def text_line_leading(line_height, font)
131137

132138
# @param [String] content
133139
# @return [String]
134-
def text_without_line_wrap(content)
140+
def replace_space_to_nbsp(content)
135141
content.gsub(/ /, Prawn::Text::NBSP)
136142
end
137143

test/basic_report/units/generator/pdf/document/graphics/test_attributes.rb

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_build_graphic_attributes
3232
end
3333

3434
def test_build_text_attributes
35-
text_styles = {
35+
text_styles_without_overflow_wrap = {
3636
'font-family' => %w( Helvetica IPAMincho ),
3737
'font-size' => 18.0,
3838
'color' => 'red',
@@ -44,7 +44,26 @@ def test_build_text_attributes
4444
'overflow' => 'expand',
4545
'word-wrap' => 'break-word'
4646
}
47+
assert_equal(
48+
{
49+
font: 'Helvetica',
50+
size: 18.0,
51+
color: 'red',
52+
align: :right,
53+
valign: :bottom,
54+
styles: [:bold, :italic],
55+
letter_spacing: 2.0,
56+
line_height: 20.0,
57+
overflow: :expand,
58+
word_wrap: :break_word,
59+
overflow_wrap: :normal
60+
},
61+
@pdf.build_text_attributes(text_styles_without_overflow_wrap)
62+
)
4763

64+
text_styles_with_overflow_wrap = text_styles_without_overflow_wrap.merge(
65+
'overflow-wrap' => 'anywhere'
66+
)
4867
assert_equal(
4968
{
5069
font: 'Helvetica',
@@ -56,12 +75,15 @@ def test_build_text_attributes
5675
letter_spacing: 2.0,
5776
line_height: 20.0,
5877
overflow: :expand,
59-
word_wrap: :break_word
78+
word_wrap: :break_word,
79+
overflow_wrap: :anywhere
6080
},
61-
@pdf.build_text_attributes(text_styles)
81+
@pdf.build_text_attributes(text_styles_with_overflow_wrap)
6282
)
6383

64-
customized_attributes = @pdf.build_text_attributes(text_styles) { |attrs| attrs[:color] = 'blue' }
84+
customized_attributes = @pdf.build_text_attributes(text_styles_without_overflow_wrap) { |attrs|
85+
attrs[:color] = 'blue'
86+
}
6587
assert_equal 'blue', customized_attributes[:color]
6688
end
6789

@@ -132,4 +154,25 @@ def test_image_position_y
132154
assert_equal :top, @pdf.image_position_y('')
133155
assert_equal :top, @pdf.image_position_y(nil)
134156
end
157+
158+
def test_overflow_wrap
159+
assert_equal :anywhere, @pdf.overflow_wrap('anywhere', :none)
160+
assert_equal :disable_break_word_by_space, @pdf.overflow_wrap('disable-break-word-by-space', :break_word)
161+
assert_equal :normal, @pdf.overflow_wrap('normal', :none)
162+
163+
# When value is unexpected value
164+
assert_equal :normal, @pdf.overflow_wrap('', :none)
165+
end
166+
167+
def text_overflow_wrap_fallback_to_word_wrap
168+
# word_wrap: none fallbacks to :disable_break_word_by_space
169+
assert_equal :disable_break_word_by_space, @pdf.overflow_wrap(nil, :none)
170+
# word_wrap: break_word fallbacks to :normal
171+
assert_equal :normal, @pdf.overflow_wrap(nil, :break_word)
172+
end
173+
174+
def test_migrate_overflow_wrap_from_word_wrap
175+
assert_equal 'normal', @pdf.migrate_overflow_wrap_from_word_wrap(:break_word)
176+
assert_equal 'disable-break-word-by-space', @pdf.migrate_overflow_wrap_from_word_wrap(:none)
177+
end
135178
end

test/basic_report/units/generator/pdf/document/graphics/test_text.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ def test_text_line_leading_should_return_a_specified_leading_value_minus_the_fon
118118
assert_equal @pdf.send(:text_line_leading, 100, font), 100 - font_height
119119
end
120120

121-
def test_text_without_line_wrap_should_replace_the_spaces_NBSP
122-
assert_equal @pdf.send(:text_without_line_wrap, ' ' * 2), Prawn::Text::NBSP * 2
121+
def test_replace_space_to_nbsp_should_replace_the_spaces_NBSP
122+
assert_equal @pdf.send(:replace_space_to_nbsp, ' ' * 2), Prawn::Text::NBSP * 2
123123
end
124124

125125
def test_text_box_should_not_raise_PrawnCannotFitError

0 commit comments

Comments
 (0)