Skip to content

Commit

Permalink
Add disable_word_break option to Text::Box and Formatted::Box
Browse files Browse the repository at this point in the history
  • Loading branch information
hidakatsuya committed Nov 11, 2023
1 parent e055b96 commit 8176c18
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
8 changes: 5 additions & 3 deletions lib/prawn/disable_word_break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
require 'prawn'

require_relative 'disable_word_break/version'
require_relative 'disable_word_break/wrap'
require_relative 'disable_word_break/formatted_wrap'

Prawn::Text::Box.extensions << Prawn::DisableWordBreak::Wrap
Prawn::Text::Formatted::Box.extensions << Prawn::DisableWordBreak::Wrap
Prawn::DisableWordBreak::FormattedWrap.tap { |ext|
Prawn::Text::Formatted::Box.prepend(ext) unless Prawn::Text::Formatted::Box.include?(ext)
Prawn::Text::Box.prepend(ext) unless Prawn::Text::Box.include?(ext)
}
21 changes: 21 additions & 0 deletions lib/prawn/disable_word_break/formatted_wrap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require_relative 'line_wrap_without_word_break'

module Prawn
module DisableWordBreak
module FormattedWrap
# override
def initialize(_, options)
super

@line_wrap = LineWrapWithoutWordBreak.new if options[:disable_word_break]
end

# override
def valid_options
super + %i(disable_word_break)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Prawn
module DisableWordBreak
class LineWrap < Text::Formatted::LineWrap
class LineWrapWithoutWordBreak < Text::Formatted::LineWrap
private

# Override
Expand Down
15 changes: 0 additions & 15 deletions lib/prawn/disable_word_break/wrap.rb

This file was deleted.

Binary file modified test/features/expects/text_line_wrapping.pdf
Binary file not shown.
33 changes: 18 additions & 15 deletions test/features/text_line_wrapping_test.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# frozen_string_literal: true

require 'features/test_helper'
require 'prawn/disable_word_break'

class TextLineWrappingTest < Test::Unit::TestCase
include TestHelper

test 'text line-wrapping' do
pdf = Prawn::Document.new do |doc|
doc.instance_eval(&renderer_on(word_break_disabled: false))

require 'prawn/disable_word_break'

doc.start_new_page
doc.instance_eval(&renderer_on(word_break_disabled: true))
end
Expand All @@ -35,11 +33,11 @@ def renderer_on(word_break_disabled:)

text_for_spaces = 'aaaaaa bbbbbb cccccccccccccccc'

text_box "#text_box:\n#{text_for_spaces}", at: [0, cursor], **box_size
text_box "#text_box:\n#{text_for_spaces}", at: [0, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [0, cursor], *box_size.values }

formatted_text_box [{ text: "#formatted_text_box:\n#{text_for_spaces}" }], at: [180, cursor], **box_size
formatted_text_box [{ text: "#formatted_text_box:\n#{text_for_spaces}" }], at: [180, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [180, cursor], *box_size.values }

Expand All @@ -55,11 +53,12 @@ def renderer_on(word_break_disabled:)

text_for_tabs = "aaaaaa\tbbbbbb\tcccccccccccccccc"

text_box "#text_box:\n#{text_for_tabs}", at: [0, cursor], **box_size
text_box "#text_box:\n#{text_for_tabs}", at: [0, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [0, cursor], *box_size.values }

formatted_text_box [{ text: "#formatted_text_box:\n#{text_for_tabs}" }], at: [180, cursor], **box_size
formatted_text_box [{ text: "#formatted_text_box:\n#{text_for_tabs}" }],
at: [180, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [180, cursor], *box_size.values }

Expand All @@ -75,11 +74,12 @@ def renderer_on(word_break_disabled:)

text_for_hard_hyphens = 'aaaaaa-bbbbbb-cccccccccccccccc'

text_box "#text_box:\n#{text_for_hard_hyphens}", at: [0, cursor], **box_size
text_box "#text_box:\n#{text_for_hard_hyphens}", at: [0, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [0, cursor], *box_size.values }

formatted_text_box [{ text: "#formatted_text_box:\n#{text_for_hard_hyphens}" }], at: [180, cursor], **box_size
formatted_text_box [{ text: "#formatted_text_box:\n#{text_for_hard_hyphens}" }],
at: [180, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [180, cursor], *box_size.values }

Expand All @@ -96,11 +96,12 @@ def renderer_on(word_break_disabled:)
shy = Prawn::Text::SHY
text_for_soft_hyphens = "aaaaaa#{shy}bbbbbb#{shy}cccccccccccccccc"

text_box "#text_box:\n#{text_for_soft_hyphens}", at: [0, cursor], **box_size
text_box "#text_box:\n#{text_for_soft_hyphens}", at: [0, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [0, cursor], *box_size.values }

formatted_text_box [{ text: "#formatted_text_box:\n#{text_for_soft_hyphens}" }], at: [180, cursor], **box_size
formatted_text_box [{ text: "#formatted_text_box:\n#{text_for_soft_hyphens}" }],
at: [180, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [180, cursor], *box_size.values }

Expand All @@ -118,11 +119,12 @@ def renderer_on(word_break_disabled:)
zwsp = Prawn::Text::ZWSP
text_for_zwsp = "aaaaaa#{zwsp}bbbbbb#{zwsp}cccccccccccccccc"

text_box "#text_box:\n#{text_for_zwsp}", at: [0, cursor], **box_size
text_box "#text_box:\n#{text_for_zwsp}", at: [0, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [0, cursor], *box_size.values }

formatted_text_box [{ text: "#formatted_text_box:\n#{text_for_zwsp}" }], at: [180, cursor], **box_size
formatted_text_box [{ text: "#formatted_text_box:\n#{text_for_zwsp}" }],
at: [180, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [180, cursor], *box_size.values }

Expand All @@ -137,11 +139,12 @@ def renderer_on(word_break_disabled:)
move_down 10

font font_dir.join('ipag.ttf') do
text_box "#text_box:\nああああああああ-いいいいいいい", at: [0, cursor], **box_size
text_box "#text_box:\nああああああああ-いいいいいいい", at: [0, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [0, cursor], *box_size.values }

formatted_text_box [{ text: "#formatted_text_box:\nああああああああ いいいいいいい" }], at: [180, cursor], **box_size
formatted_text_box [{ text: "#formatted_text_box:\nああああああああ いいいいいいい" }],
at: [180, cursor], disable_word_break: word_break_disabled, **box_size

stroke { rectangle [180, cursor], *box_size.values }

Expand Down

0 comments on commit 8176c18

Please sign in to comment.