Class: Pdfrb::Layout::TextLayouter
- Inherits:
-
Object
- Object
- Pdfrb::Layout::TextLayouter
- Defined in:
- lib/pdfrb/layout/text_layouter.rb
Overview
Breaks a string into Layout::Line instances that fit a given width. Uses real per-glyph widths from the font's AFM or hmtx table when available, falling back to a heuristic estimate.
Instance Attribute Summary collapse
-
#style ⇒ Object
readonly
Returns the value of attribute style.
Instance Method Summary collapse
-
#initialize(style) ⇒ TextLayouter
constructor
A new instance of TextLayouter.
-
#layout(text, width) ⇒ Array<Line>
Lines, each fitting in
width.
Constructor Details
#initialize(style) ⇒ TextLayouter
Returns a new instance of TextLayouter.
11 12 13 14 |
# File 'lib/pdfrb/layout/text_layouter.rb', line 11 def initialize(style) @style = style @metrics = lookup_metrics end |
Instance Attribute Details
#style ⇒ Object (readonly)
Returns the value of attribute style.
9 10 11 |
# File 'lib/pdfrb/layout/text_layouter.rb', line 9 def style @style end |
Instance Method Details
#layout(text, width) ⇒ Array<Line>
Returns lines, each fitting in width.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pdfrb/layout/text_layouter.rb', line 19 def layout(text, width) reordered = reorder_for_layout(text.to_s) clusters = reordered.grapheme_clusters return [] if clusters.empty? lines = [] current = [] current_width = 0.0 clusters.each do |cluster| cw = cluster_width(cluster) if current_width + cw > width && !current.empty? lines << build_line(current) current = [] current_width = 0.0 end current << cluster current_width += cw end lines << build_line(current) unless current.empty? lines end |