Class: Prawn::Text::Formatted::LineWrap

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/nabetani/prawn-linewrap-ja.rb

Constant Summary collapse

PROHIBIT_LINE_BREAK_BEFORE_CHARS =
'‐〜゠–' + # ハイフン類(cl-03)※ 普通の半角マイナスはエスケープが必要なので別途で
'·・:;' + # 中点類(cl-05)
'!?‼⁇⁈⁉' + # 区切り約物(cl-04)※ 半角の ? ! はエスケープが必要なので別途で
'・・:;' + # 中点類(cl-05)※ 半角の : ; はエスケープが必要なので別途で
'。.。' + # 句点類(cl-06)※ 半角ピリオドはエスケープが必要なので別途で
'、,、' + # 読点類(cl-07)※ 半角コンマはエスケープが必要なので別途で
'ヽヾゝゞ々〻' + # 繰返し記号(cl-09)
'ーー' + # 長音記号(cl-10)
'ぁぃぅぇぉァィゥェォっゃゅょゎゕゖッャュョヮヵヶㇰㇱㇲㇳㇴㇵㇶㇷㇸㇹㇺㇻㇼㇽㇾㇿ'.freeze
PROHIBIT_LINE_BREAK_BEFORE =

行頭禁則

/.[\-\.\,\!\?\:\;\p{Terminal_Punctuation}\p{Close_Punctuation}\p{Final_Punctuation}#{PROHIBIT_LINE_BREAK_BEFORE_CHARS}]/.freeze
PROHIBIT_LINE_BREAK_AFTER_CHARS =
"¿¡⸘"
PROHIBIT_LINE_BREAK_AFTER =

行末禁則

/[\p{Initial_Punctuation}\p{Open_Punctuation}#{PROHIBIT_LINE_BREAK_AFTER_CHARS}]./.freeze
DASH_PATTERN =

各種ダッシュ。種類が異なってもよい。

/[\u{2012}\u{2013}\u{2014}\u{2015}\u{2E3A}\u{2E3B}]{2}/
LEADER_PATTERN =

同一種類のリーダーの2個繰り返し

/\u{2026}\u{2026}|\u{2025}\u{2025}|\u{22EF}\u{22EF}/
ATMARKS =
"\u{0040}\u{FE6B}\u{FF20}"
UNDERSCORES =
"\u{005F}\u{0332}\u{FF3F}"
ALNUM_PATTERN =

英数字・キリル文字・ギリシャ文字・コンマ・ピリオド の繰り返し

/[\p{Latin}\p{Greek}\p{Cyrillic}0-90-9\.\,#{ATMARKS}#{UNDERSCORES}]{2}/
EXTRA_SPLITTABLE_CHAR =
'ーー' + # 音引き
'〇∞∴♂♀&*☆★○●◎◇◆□■△▲▽▼※♪◯©®'+ # 色々
"\u{2026}\u{2025}\u{22EF}" + # リーダー
"\u{2012}\u{2013}\u{2014}\u{2015}\u{2E3A}\u{2E3B}"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.or_rgexp(chars) ⇒ Object



27
28
29
30
# File 'lib/asciidoctor/nabetani/prawn-linewrap-ja.rb', line 27

def self.or_rgexp( chars )
  s = chars.chars.map{ |e| Regexp.escape(e) }.join
  /[#{s}]/
end

Instance Method Details

#end_of_the_line_reached(segment) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/asciidoctor/nabetani/prawn-linewrap-ja.rb', line 105

def end_of_the_line_reached(segment)
  if !@fragment_output.strip.empty? && !segment.strip.empty?
    @line_contains_more_than_one_word = true
  end

  update_line_status_based_on_last_output
  unless @line_contains_more_than_one_word
    wrap_by_char(segment)
  end
  @line_full = true
end

#get_last_token_of(text) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/asciidoctor/nabetani/prawn-linewrap-ja.rb', line 121

def get_last_token_of(text)
  if text
    tokenize(text).last
  else
    ""
  end
end

#initialize_line(options) ⇒ Object



22
23
24
25
# File 'lib/asciidoctor/nabetani/prawn-linewrap-ja.rb', line 22

def initialize_line(options)
  original_initialize_line(options)
  @disable_wrap_by_char = true
end

#original_initialize_lineObject



20
# File 'lib/asciidoctor/nabetani/prawn-linewrap-ja.rb', line 20

alias_method :original_initialize_line, :initialize_line

#remember_this_fragment_for_backward_looking_opsObject



135
136
137
138
139
140
# File 'lib/asciidoctor/nabetani/prawn-linewrap-ja.rb', line 135

def remember_this_fragment_for_backward_looking_ops
  @previous_fragment = @fragment_output.dup
  pf = @previous_fragment
  @previous_fragment_ended_with_breakable = text_ended_with_breakable(pf)
  @previous_fragment_output_without_last_word = get_last_token_of pf
end

#split_pattern(s) ⇒ Object

各種ダッシュ



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/asciidoctor/nabetani/prawn-linewrap-ja.rb', line 71

def split_pattern(s)
  case s
  when PROHIBIT_LINE_BREAK_BEFORE,
       PROHIBIT_LINE_BREAK_AFTER,
       DASH_PATTERN,
       LEADER_PATTERN,
       ALNUM_PATTERN
    false
  when /[\-\s\p{Space}\u{200B}\u{00ad}]/, # 空白(200b は、ゼロ幅空白。00ad は、soft-hyphen)
       /[\p{Hiragana}\p{Katakana}#{EXTRA_SPLITTABLE_CHAR}\p{Han}]/, # 日本語等
       /[\p{Initial_Punctuation}\p{Open_Punctuation}]/, # 開き括弧等
       /[\p{Terminal_Punctuation}\p{Close_Punctuation}\p{Final_Punctuation}]/ # 閉じ括弧等
    true
  else
    false
  end
end

#text_ended_with_breakable(text) ⇒ Object



117
118
119
# File 'lib/asciidoctor/nabetani/prawn-linewrap-ja.rb', line 117

def text_ended_with_breakable( text )
  true
end

#tokenize(fragment) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/asciidoctor/nabetani/prawn-linewrap-ja.rb', line 89

def tokenize(fragment)
  if //===fragment
    x=0
  end
  fragment.size.times.with_object(["".clone]) do |ix,s|
    cur = fragment[ix]
    if s.last.empty?
      s.last << cur
    elsif split_pattern(s.last[-1]+cur)
      s.push cur
    else
      s.last << cur
    end
  end
end

#update_line_status_based_on_last_outputObject



129
130
131
132
133
# File 'lib/asciidoctor/nabetani/prawn-linewrap-ja.rb', line 129

def update_line_status_based_on_last_output
  if 1<tokenize(@fragment_output).size
    @line_contains_more_than_one_word = true
  end
end