lib/thinreports/basic_report/generator/pdf/document/graphics/text.rb in thinreports-0.14.0 vs lib/thinreports/basic_report/generator/pdf/document/graphics/text.rb in thinreports-0.14.1
- old
+ new
@@ -21,21 +21,21 @@
# @option attrs [Numeric, String] :line_height The total height of an text line.
# @option attrs [Numeric, String] :letter_spacing
# @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_break_word_by_space] :overflow_wrap (:normal)
def text_box(content, x, y, w, h, attrs = {}, &block)
w, h = s2f(w, h)
box_attrs = text_box_attrs(
x, y, w, h,
single: attrs.delete(:single),
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 = replace_space_to_nbsp(content) if attrs[:overflow_wrap] == :disable_break_word_by_space
with_text_styles(attrs) do |built_attrs, font_styles|
if block
block.call [{ text: content, styles: font_styles }],
built_attrs.merge(box_attrs)
@@ -110,10 +110,16 @@
# Set the :character_spacing option.
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.delete(:overflow_wrap) == :anywhere
+
+ # Delete unnecessary attributes
+ attrs.delete(:word_wrap)
+
# Or... with_font_styles(attrs, fontinfo, &block)
with_font_styles(attrs, fontinfo) do |modified_attrs, styles|
block.call(modified_attrs, styles)
end
@@ -129,10 +135,10 @@
line_height - pdf.font(font[:name], size: font[:size]).height
end
# @param [String] content
# @return [String]
- def text_without_line_wrap(content)
+ def replace_space_to_nbsp(content)
content.gsub(/ /, Prawn::Text::NBSP)
end
# @param [Hash] attrs
# @param [Hash] font