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.
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.
15 16 17 |
# File 'lib/idml/text_engine/line_breaker.rb', line 15 def initialize(frame_width) @frame_width = frame_width end |
Class Method Details
.break(glyphs:, frame_width:) ⇒ Object
11 12 13 |
# File 'lib/idml/text_engine/line_breaker.rb', line 11 def self.break(glyphs:, frame_width:) new(frame_width).break(glyphs) end |
Instance Method Details
#break(glyphs) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/idml/text_engine/line_breaker.rb', line 19 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 |