Class: Idml::TextEngine::LineBreaker
- Inherits:
-
Object
- Object
- Idml::TextEngine::LineBreaker
- Defined in:
- lib/idml/text_engine/line_breaker.rb
Overview
Greedy word-wrap line breaker. Accumulates glyphs until exceeding the frame width, then breaks at the last space. CJK runs get a kinsoku shori post-pass (no line starts or ends with a forbidden character).
Class Method Summary collapse
Instance Method Summary collapse
- #break(glyphs) ⇒ Object
-
#initialize(frame_width) ⇒ LineBreaker
constructor
A new instance of LineBreaker.
Constructor Details
#initialize(frame_width) ⇒ LineBreaker
Returns a new instance of LineBreaker.
25 26 27 |
# File 'lib/idml/text_engine/line_breaker.rb', line 25 def initialize(frame_width) @frame_width = frame_width end |
Class Method Details
.break(glyphs:, frame_width:) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/idml/text_engine/line_breaker.rb', line 13 def self.break(glyphs:, frame_width:) lines = new(frame_width).break(glyphs) return lines unless cjk_run?(glyphs) CjkLayout.apply_kinsoku(lines) end |
Instance Method Details
#break(glyphs) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/idml/text_engine/line_breaker.rb', line 29 def break(glyphs) lines = [] current = [] current_width = 0 last_space_idx = -1 width_at_space = 0 glyphs.each_with_index do |glyph, _idx| current << glyph current_width += glyph.width if glyph.is_space last_space_idx = current.length - 1 width_at_space = current_width end next unless current_width > @frame_width && current.length > 1 if last_space_idx >= 0 line_glyphs = current[0..last_space_idx] lines << Line.new(line_glyphs, width_at_space, 0) current = current[(last_space_idx + 1)..] current_width = current.sum(&:width) last_space_idx = -1 else lines << Line.new(current, current_width, 0) current = [] current_width = 0 end end lines << Line.new(current, current_width, 0) if current.any? lines end |