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 25, 2023
1 parent f1a7d8f commit f645a90
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 28 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Gem Version](https://badge.fury.io/rb/prawn-disable_word_break.svg)](https://badge.fury.io/rb/prawn-disable_word_break)
[![Test](https://github.com/hidakatsuya/prawn-disable_word_break/actions/workflows/test.yml/badge.svg)](https://github.com/hidakatsuya/prawn-disable_word_break/actions/workflows/test.yml)

Prawn::DisableWordBreak is an extension that disables word-breaking by character such as space and hyphen to [Prawn](https://github.com/prawnpdf/prawn).
Prawn::DisableWordBreak is an extension to [Prawn](https://github.com/prawnpdf/prawn) that provides an option to disable line breaking on characters such as spaces and hyphens.

![](https://raw.githubusercontent.com/hidakatsuya/prawn-disable_word_break/master/doc/comparison-of-word-breaking.png)

Expand Down Expand Up @@ -31,6 +31,27 @@ Or install it yourself as:
require 'prawn/disable_word_break'
```

Or, you can set it for each method such as `text_box`.

```ruby
# Disable the default setting.
Prawn::DisableWordBreak.config.default = false
```

```ruby
Prawn::Document.generate "prawn.pdf" do
text_box "text",
at: [0, cursor], width: 150, height: 50, disable_word_break: true

formatted_text_box [{ text: "text" }],
at: [0, cursor], width: 150, height: 50, disable_word_break: true

bounding_box [0, cursor], width: 150, height: 50 do
text "text", disable_word_break: true
end
end
```

## Supported Versions

### Ruby
Expand Down
18 changes: 18 additions & 0 deletions lib/prawn/disable_word_break.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
# frozen_string_literal: true

require 'prawn'
require 'forwardable'

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

module Prawn
module DisableWordBreak
extend Forwardable

Config = Struct.new(
# Sets the default value for the disable_word_break option. Default is false.
:default,

keyword_init: true
)

def self.config
@config ||= Config.new(default: false)
end
end
end

Prawn::DisableWordBreak::Wrap.tap do |mod|
Prawn::Text::Formatted::Wrap.prepend(mod) unless Prawn::Text::Formatted::Wrap.include?(mod)
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 LineBreakAnywhere < Text::Formatted::LineWrap
private

# Override
Expand Down
11 changes: 8 additions & 3 deletions lib/prawn/disable_word_break/wrap.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# frozen_string_literal: true

require_relative 'line_wrap'
require_relative 'line_break_anywhere'

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

@line_wrap = DisableWordBreak::LineWrap.new
@line_wrap = LineBreakAnywhere.new if DisableWordBreak.config.default || options[:disable_word_break]
end

# override
def valid_options
super + %i(disable_word_break)
end
end
end
Expand Down
Binary file modified test/features/expects/text_line_wrapping.pdf
Binary file not shown.
54 changes: 31 additions & 23 deletions test/features/text_line_wrapping_test.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
# 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))
doc.instance_eval(&renderer_on("DisabledWordBreak is disabled", disable_word_break_option: false))

require 'prawn/disable_word_break'
doc.start_new_page
doc.instance_eval(&renderer_on("DisabledWordBreak is enabled", disable_word_break_option: true))

Prawn::DisableWordBreak.config.default = true
doc.start_new_page
doc.instance_eval(&renderer_on(word_break_disabled: true))
doc.instance_eval(&renderer_on("DisabledWordBreak is enabled by config.default", disable_word_break_option: false))
end

assert_expected_pdf 'text_line_wrapping', pdf.render
end

private

def renderer_on(word_break_disabled:)
def renderer_on(title, disable_word_break_option: false)
font_dir = root_dir.join('../fonts')
box_size = { width: 150, height: 50 }

proc do
font font_dir.join('DejaVuSans.ttf')

font_size(20) { text "Word-breaking is #{word_break_disabled ? 'disabled' : 'enabled' }" }
font_size(20) { text title }
move_down 10

text 'Spaces'
move_down 10

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: disable_word_break_option, **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: disable_word_break_option, **box_size

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

bounding_box [360, cursor], **box_size do
text "#bounding_box:\n#{text_for_spaces}"
text "#bounding_box:\n#{text_for_spaces}", disable_word_break: disable_word_break_option
stroke_bounds
end

Expand All @@ -55,16 +58,17 @@ 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: disable_word_break_option, **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: disable_word_break_option, **box_size

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

bounding_box [360, cursor], **box_size do
text "#bounding_box:\n#{text_for_tabs}"
text "#bounding_box:\n#{text_for_tabs}", disable_word_break: disable_word_break_option
stroke_bounds
end

Expand All @@ -75,16 +79,17 @@ 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: disable_word_break_option, **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: disable_word_break_option, **box_size

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

bounding_box [360, cursor], **box_size do
text "#bounding_box:\n#{text_for_hard_hyphens}"
text "#bounding_box:\n#{text_for_hard_hyphens}", disable_word_break: disable_word_break_option
stroke_bounds
end

Expand All @@ -96,16 +101,17 @@ 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: disable_word_break_option, **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: disable_word_break_option, **box_size

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

bounding_box [360, cursor], **box_size do
text "#bounding_box:\n#{text_for_soft_hyphens}"
text "#bounding_box:\n#{text_for_soft_hyphens}", disable_word_break: disable_word_break_option
stroke_bounds
end

Expand All @@ -118,16 +124,17 @@ 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: disable_word_break_option, **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: disable_word_break_option, **box_size

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

bounding_box [360, cursor], **box_size do
text "#bounding_box:\n#{text_for_zwsp}"
text "#bounding_box:\n#{text_for_zwsp}", disable_word_break: disable_word_break_option
stroke_bounds
end

Expand All @@ -137,16 +144,17 @@ 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: disable_word_break_option, **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: disable_word_break_option, **box_size

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

bounding_box [360, cursor], **box_size do
text "#bounding_box:\nああああああああ#{zwsp}いいいいいいい"
text "#bounding_box:\nああああああああ#{zwsp}いいいいいいい", disable_word_break: disable_word_break_option
stroke_bounds
end
end
Expand Down

0 comments on commit f645a90

Please sign in to comment.